Skip to content

Commit fc6289c

Browse files
authored
write/elf: add Writer::reserve_null_symbol_index (#402)
Use this to ensure `Object` creates the null symbol when no symbols are defined.
1 parent 4261d56 commit fc6289c

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/write/elf/object.rs

+1
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ impl<'a> Object<'a> {
215215

216216
// Calculate index of symbols and add symbol strings to strtab.
217217
let mut symbol_offsets = vec![SymbolOffsets::default(); self.symbols.len()];
218+
writer.reserve_null_symbol_index();
218219
// Local symbols must come before global.
219220
for (index, symbol) in self.symbols.iter().enumerate() {
220221
if symbol.is_local() {

src/write/elf/writer.rs

+17
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,23 @@ impl<'a> Writer<'a> {
699699
});
700700
}
701701

702+
/// Reserve the null symbol table entry.
703+
///
704+
/// This will be stored in the `.symtab` section.
705+
///
706+
/// The null symbol table entry is usually automatically reserved,
707+
/// but this can be used to force an empty symbol table.
708+
///
709+
/// This must be called before [`Self::reserve_symtab`].
710+
pub fn reserve_null_symbol_index(&mut self) -> SymbolIndex {
711+
debug_assert_eq!(self.symtab_offset, 0);
712+
debug_assert_eq!(self.symtab_num, 0);
713+
self.symtab_num = 1;
714+
// The symtab must link to a strtab.
715+
self.need_strtab = true;
716+
SymbolIndex(0)
717+
}
718+
702719
/// Reserve a symbol table entry.
703720
///
704721
/// This will be stored in the `.symtab` section.

0 commit comments

Comments
 (0)