Fix REL alignment after section data

This commit is contained in:
Luke Street 2024-06-10 00:38:06 -06:00
parent 4ea4ec86d0
commit b44aa78c49
1 changed files with 11 additions and 4 deletions

View File

@ -849,6 +849,8 @@ where
offset = (offset + align) & !align; offset = (offset + align) & !align;
offset += section.size() as u32; offset += section.size() as u32;
} }
// Align to 4 after section data
offset = (offset + 3) & !3;
fn do_relocation_layout( fn do_relocation_layout(
relocations: &[RelReloc], relocations: &[RelReloc],
@ -1013,16 +1015,16 @@ where
} }
} }
ensure!(w.stream_position()? as u32 == section_data_offset); ensure!(w.stream_position()? as u32 == section_data_offset);
fn calculate_padding(position: u64, align: u64) -> u64 {
let align = align - 1;
((position + align) & !align) - position
}
for (idx, section) in file for (idx, section) in file
.sections() .sections()
.filter(is_permitted_section) .filter(is_permitted_section)
.enumerate() .enumerate()
.filter(|(_, s)| should_write_section(s)) .filter(|(_, s)| should_write_section(s))
{ {
fn calculate_padding(position: u64, align: u64) -> u64 {
let align = align - 1;
((position + align) & !align) - position
}
let position = w.stream_position()?; let position = w.stream_position()?;
let align = section_align(idx, &section, info); let align = section_align(idx, &section, info);
w.write_all(&vec![0u8; calculate_padding(position, align as u64) as usize])?; w.write_all(&vec![0u8; calculate_padding(position, align as u64) as usize])?;
@ -1038,6 +1040,11 @@ where
} }
w.write_all(&section_data)?; w.write_all(&section_data)?;
} }
// Align to 4 after section data
{
let position = w.stream_position()?;
w.write_all(&vec![0u8; calculate_padding(position, 4) as usize])?;
}
if !relocations.is_empty() { if !relocations.is_empty() {
if info.version < 3 { if info.version < 3 {
// Version 1 and 2 RELs write relocations before the import table. // Version 1 and 2 RELs write relocations before the import table.