Skip to content

Commit

Permalink
write/elf: add Writer::reserve_null_symbol_index (#402)
Browse files Browse the repository at this point in the history
Use this to ensure `Object` creates the null symbol
when no symbols are defined.
  • Loading branch information
philipc authored Dec 4, 2021
1 parent 4261d56 commit fc6289c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/write/elf/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ impl<'a> Object<'a> {

// Calculate index of symbols and add symbol strings to strtab.
let mut symbol_offsets = vec![SymbolOffsets::default(); self.symbols.len()];
writer.reserve_null_symbol_index();
// Local symbols must come before global.
for (index, symbol) in self.symbols.iter().enumerate() {
if symbol.is_local() {
Expand Down
17 changes: 17 additions & 0 deletions src/write/elf/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,23 @@ impl<'a> Writer<'a> {
});
}

/// Reserve the null symbol table entry.
///
/// This will be stored in the `.symtab` section.
///
/// The null symbol table entry is usually automatically reserved,
/// but this can be used to force an empty symbol table.
///
/// This must be called before [`Self::reserve_symtab`].
pub fn reserve_null_symbol_index(&mut self) -> SymbolIndex {
debug_assert_eq!(self.symtab_offset, 0);
debug_assert_eq!(self.symtab_num, 0);
self.symtab_num = 1;
// The symtab must link to a strtab.
self.need_strtab = true;
SymbolIndex(0)
}

/// Reserve a symbol table entry.
///
/// This will be stored in the `.symtab` section.
Expand Down

0 comments on commit fc6289c

Please sign in to comment.