write_rel: Skip setting reloc/imp offset with empty relocations

This commit is contained in:
Luke Street 2023-11-18 23:33:18 -05:00
parent 8659b56da4
commit 38c692650f

View File

@ -932,6 +932,7 @@ where
let imp_count = relocations.iter().map(|r| r.module_id).dedup().count(); let imp_count = relocations.iter().map(|r| r.module_id).dedup().count();
let mut imp_entries = Vec::<RelImport>::with_capacity(imp_count); let mut imp_entries = Vec::<RelImport>::with_capacity(imp_count);
let mut raw_relocations = vec![]; let mut raw_relocations = vec![];
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.
header.rel_offset = offset; header.rel_offset = offset;
@ -958,6 +959,7 @@ where
&mut offset, &mut offset,
)?; )?;
} }
}
for symbol in file.symbols().filter(|s| s.is_definition()) { for symbol in file.symbols().filter(|s| s.is_definition()) {
let Some(symbol_section) = symbol.section_index() else { let Some(symbol_section) = symbol.section_index() else {
@ -1035,6 +1037,7 @@ where
} }
w.write_all(&section_data)?; w.write_all(&section_data)?;
} }
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.
ensure!(w.stream_position()? as u32 == header.rel_offset); ensure!(w.stream_position()? as u32 == header.rel_offset);
@ -1053,6 +1056,7 @@ where
reloc.to_writer(w, Endian::Big)?; reloc.to_writer(w, Endian::Big)?;
} }
} }
}
ensure!(w.stream_position()? as u32 == offset); ensure!(w.stream_position()? as u32 == offset);
Ok(()) Ok(())
} }