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,31 +932,33 @@ 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 info.version < 3 { if !relocations.is_empty() {
// Version 1 and 2 RELs write relocations before the import table. if info.version < 3 {
header.rel_offset = offset; // Version 1 and 2 RELs write relocations before the import table.
do_relocation_layout( header.rel_offset = offset;
&relocations, do_relocation_layout(
&mut header, &relocations,
&mut imp_entries, &mut header,
&mut raw_relocations, &mut imp_entries,
&mut offset, &mut raw_relocations,
)?; &mut offset,
} )?;
header.imp_offset = offset; }
header.imp_size = imp_count as u32 * RelImport::STATIC_SIZE as u32; header.imp_offset = offset;
offset += header.imp_size; header.imp_size = imp_count as u32 * RelImport::STATIC_SIZE as u32;
if info.version >= 3 { offset += header.imp_size;
// Version 3 RELs write relocations after the import table, if info.version >= 3 {
// so that the import table isn't clobbered by OSLinkFixed. // Version 3 RELs write relocations after the import table,
header.rel_offset = offset; // so that the import table isn't clobbered by OSLinkFixed.
do_relocation_layout( header.rel_offset = offset;
&relocations, do_relocation_layout(
&mut header, &relocations,
&mut imp_entries, &mut header,
&mut raw_relocations, &mut imp_entries,
&mut offset, &mut raw_relocations,
)?; &mut offset,
)?;
}
} }
for symbol in file.symbols().filter(|s| s.is_definition()) { for symbol in file.symbols().filter(|s| s.is_definition()) {
@ -1035,22 +1037,24 @@ where
} }
w.write_all(&section_data)?; w.write_all(&section_data)?;
} }
if info.version < 3 { if !relocations.is_empty() {
// Version 1 and 2 RELs write relocations before the import table. if info.version < 3 {
ensure!(w.stream_position()? as u32 == header.rel_offset); // Version 1 and 2 RELs write relocations before the import table.
for reloc in &raw_relocations { ensure!(w.stream_position()? as u32 == header.rel_offset);
reloc.to_writer(w, Endian::Big)?; for reloc in &raw_relocations {
reloc.to_writer(w, Endian::Big)?;
}
} }
} ensure!(w.stream_position()? as u32 == header.imp_offset);
ensure!(w.stream_position()? as u32 == header.imp_offset); for entry in &imp_entries {
for entry in &imp_entries { entry.to_writer(w, Endian::Big)?;
entry.to_writer(w, Endian::Big)?; }
} if info.version >= 3 {
if info.version >= 3 { // Version 3 RELs write relocations after the import table. See above.
// Version 3 RELs write relocations after the import table. See above. ensure!(w.stream_position()? as u32 == header.rel_offset);
ensure!(w.stream_position()? as u32 == header.rel_offset); for reloc in &raw_relocations {
for reloc in &raw_relocations { 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);