From eb98ae4fd834701e6de68d59fe7be8442f5abcf0 Mon Sep 17 00:00:00 2001 From: "381848900@qq.com" Date: Fri, 13 Dec 2024 15:18:11 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8DSizeOfImage=E7=9A=84?= =?UTF-8?q?=E8=AE=A1=E7=AE=97=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src-tauri/src/app_state.rs | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/src-tauri/src/app_state.rs b/src-tauri/src/app_state.rs index bee9d17..5674812 100644 --- a/src-tauri/src/app_state.rs +++ b/src-tauri/src/app_state.rs @@ -113,16 +113,8 @@ impl AppState { // 3. 判断是否可以添加节表 let section_table_size = 40 * number_of_sections as usize; - // TODO: 这里有问题 - // 要判断第一个节表的pointer_to_raw_data - size_of_headers是否大于40 - let first_section_header_offset = section_table_offset; - let first_pointer_to_raw_data = u32::from_le_bytes( - mmap[first_section_header_offset + 20..first_section_header_offset + 24] - .try_into() - .unwrap(), - ); - if first_pointer_to_raw_data - size_of_headers < 40 { + if size_of_headers - section_table_offset as u32 - (section_table_size as u32) < 40 { // 需要看一下是否可以拓展文件大小 // 2. 计算size_of_headers对齐之后的大小 例如:0x400 -> 0x1000 @@ -194,7 +186,6 @@ impl AppState { } // 4. 添加节表 // 新的节区文件偏移是文件末尾 - // TODO: 如果增加的大小为0是否有问题? let new_section_offset = self.get_mmap_ref()?.len(); let new_section_header_offset = section_table_offset + section_table_size; { @@ -214,8 +205,18 @@ impl AppState { // 1.1 写入节名 rw_mmap[new_section_header_offset..new_section_header_offset + 8] .copy_from_slice(§ion_name_bytes); + + // virtual_size需要对齐 + let mut virtual_size = ((section_size as u32) + section_alignment - 1) & !(section_alignment - 1); + if virtual_size == 0 { + virtual_size = section_alignment; + } + + rw_mmap[new_section_header_offset + 8..new_section_header_offset + 12] + .copy_from_slice(&(virtual_size as u32).to_le_bytes()); + + // 写入节的VA - // TODO: 新节的VA应该是最后一个节的VA + 最后一个节的SizeOfRawData对齐后的大小 let last_section_header_offset = section_table_offset + 40 * (number_of_sections - 1) as usize; let last_section_va = u32::from_le_bytes( @@ -259,7 +260,6 @@ impl AppState { rw_mmap[optional_header_offset + 0x3C..optional_header_offset + 0x40] .copy_from_slice(&new_size_of_headers.to_le_bytes()); - // TODO: 修改SizeOfImage let origin_size_of_image = unsafe { // 在0x38的位置 u32::from_le_bytes( @@ -268,10 +268,13 @@ impl AppState { .unwrap(), ) }; - // TODO: 增加的大小应该是section_size对齐后的大小 - let add_image_size = + let mut add_image_size = (section_size as u32 + section_alignment - 1) & !(section_alignment - 1); + if add_image_size == 0 { + add_image_size = section_alignment; + } + rw_mmap[optional_header_offset + 0x38..optional_header_offset + 0x3C] .copy_from_slice(&(origin_size_of_image + add_image_size).to_le_bytes());