fix: 修复SizeOfImage的计算错误

This commit is contained in:
381848900@qq.com 2024-12-13 15:18:11 +08:00
parent 9f6c18949e
commit eb98ae4fd8

View File

@ -113,16 +113,8 @@ impl AppState {
// 3. 判断是否可以添加节表 // 3. 判断是否可以添加节表
let section_table_size = 40 * number_of_sections as usize; 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 // 2. 计算size_of_headers对齐之后的大小 例如:0x400 -> 0x1000
@ -194,7 +186,6 @@ impl AppState {
} }
// 4. 添加节表 // 4. 添加节表
// 新的节区文件偏移是文件末尾 // 新的节区文件偏移是文件末尾
// TODO: 如果增加的大小为0是否有问题?
let new_section_offset = self.get_mmap_ref()?.len(); let new_section_offset = self.get_mmap_ref()?.len();
let new_section_header_offset = section_table_offset + section_table_size; let new_section_header_offset = section_table_offset + section_table_size;
{ {
@ -214,8 +205,18 @@ impl AppState {
// 1.1 写入节名 // 1.1 写入节名
rw_mmap[new_section_header_offset..new_section_header_offset + 8] rw_mmap[new_section_header_offset..new_section_header_offset + 8]
.copy_from_slice(&section_name_bytes); .copy_from_slice(&section_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 // 写入节的VA
// TODO: 新节的VA应该是最后一个节的VA + 最后一个节的SizeOfRawData对齐后的大小
let last_section_header_offset = let last_section_header_offset =
section_table_offset + 40 * (number_of_sections - 1) as usize; section_table_offset + 40 * (number_of_sections - 1) as usize;
let last_section_va = u32::from_le_bytes( let last_section_va = u32::from_le_bytes(
@ -259,7 +260,6 @@ impl AppState {
rw_mmap[optional_header_offset + 0x3C..optional_header_offset + 0x40] rw_mmap[optional_header_offset + 0x3C..optional_header_offset + 0x40]
.copy_from_slice(&new_size_of_headers.to_le_bytes()); .copy_from_slice(&new_size_of_headers.to_le_bytes());
// TODO: 修改SizeOfImage
let origin_size_of_image = unsafe { let origin_size_of_image = unsafe {
// 在0x38的位置 // 在0x38的位置
u32::from_le_bytes( u32::from_le_bytes(
@ -268,10 +268,13 @@ impl AppState {
.unwrap(), .unwrap(),
) )
}; };
// TODO: 增加的大小应该是section_size对齐后的大小 let mut add_image_size =
let add_image_size =
(section_size as u32 + section_alignment - 1) & !(section_alignment - 1); (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] rw_mmap[optional_header_offset + 0x38..optional_header_offset + 0x3C]
.copy_from_slice(&(origin_size_of_image + add_image_size).to_le_bytes()); .copy_from_slice(&(origin_size_of_image + add_image_size).to_le_bytes());