Skip to content

Commit

Permalink
elf: allow for absence of dynstr
Browse files Browse the repository at this point in the history
.dynstr doesn't need to be present if there are no dynamic strings.

- handle section index 0 in read::elf::SectionTable::strings
- don't require .dynstr when writing if there are no strings
- don't print name of symbol index 0 in readobj example
  • Loading branch information
philipc committed Mar 18, 2024
1 parent 609f677 commit 5e225a2
Show file tree
Hide file tree
Showing 15 changed files with 68 additions and 48 deletions.
18 changes: 13 additions & 5 deletions crates/examples/src/readobj/elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,11 +398,15 @@ fn print_section_symbols<Elf: FileHeader>(
for (index, symbol) in symbols.iter().enumerate() {
p.group("Symbol", |p| {
p.field("Index", index);
p.field_string(
"Name",
symbol.st_name(endian),
symbol.name(endian, symbols.strings()),
);
if index == 0 {
p.field_hex("Name", symbol.st_name(endian));
} else {
p.field_string(
"Name",
symbol.st_name(endian),
symbol.name(endian, symbols.strings()),
);
}
if let Some(versions) = versions.as_ref() {
let version_index = versions.version_index(endian, index);
print_version(p, Some(versions), version_index);
Expand Down Expand Up @@ -511,6 +515,10 @@ fn print_rel_symbol<Elf: FileHeader>(
symbols: Option<SymbolTable<'_, Elf>>,
sym: u32,
) {
if sym == 0 {
p.field_hex("Symbol", sym);
return;
}
let name = symbols.and_then(|symbols| {
symbols
.symbol(sym as usize)
Expand Down
2 changes: 1 addition & 1 deletion crates/examples/testfiles/elf/base-aarch64.o.readobj
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ SectionHeader {
EntrySize: 0x18
Symbol {
Index: 0
Name: "" (0x0)
Name: 0x0
Value: 0x0
Size: 0x0
Type: STT_NOTYPE (0x0)
Expand Down
16 changes: 8 additions & 8 deletions crates/examples/testfiles/elf/base-aarch64.readobj
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ SectionHeader {
EntrySize: 0x18
Symbol {
Index: 0
Name: "" (0x0)
Name: 0x0
Version: VER_NDX_LOCAL (0x0)
Value: 0x0
Size: 0x0
Expand Down Expand Up @@ -559,37 +559,37 @@ SectionHeader {
Relocation {
Offset: 0x10D80
Type: R_AARCH64_RELATIVE (0x403)
Symbol: "" (0x0)
Symbol: 0x0
Addend: 0x720
}
Relocation {
Offset: 0x10D88
Type: R_AARCH64_RELATIVE (0x403)
Symbol: "" (0x0)
Symbol: 0x0
Addend: 0x6D8
}
Relocation {
Offset: 0x10FC8
Type: R_AARCH64_RELATIVE (0x403)
Symbol: "" (0x0)
Symbol: 0x0
Addend: 0x7C8
}
Relocation {
Offset: 0x10FE8
Type: R_AARCH64_RELATIVE (0x403)
Symbol: "" (0x0)
Symbol: 0x0
Addend: 0x748
}
Relocation {
Offset: 0x10FF0
Type: R_AARCH64_RELATIVE (0x403)
Symbol: "" (0x0)
Symbol: 0x0
Addend: 0x724
}
Relocation {
Offset: 0x11008
Type: R_AARCH64_RELATIVE (0x403)
Symbol: "" (0x0)
Symbol: 0x0
Addend: 0x11008
}
Relocation {
Expand Down Expand Up @@ -971,7 +971,7 @@ SectionHeader {
EntrySize: 0x18
Symbol {
Index: 0
Name: "" (0x0)
Name: 0x0
Value: 0x0
Size: 0x0
Type: STT_NOTYPE (0x0)
Expand Down
2 changes: 1 addition & 1 deletion crates/examples/testfiles/elf/base-mips64el.o.readobj
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ SectionHeader {
EntrySize: 0x18
Symbol {
Index: 0
Name: "" (0x0)
Name: 0x0
Value: 0x0
Size: 0x0
Type: STT_NOTYPE (0x0)
Expand Down
4 changes: 2 additions & 2 deletions crates/examples/testfiles/elf/base-mips64el.readobj
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ SectionHeader {
EntrySize: 0x18
Symbol {
Index: 0
Name: "" (0x0)
Name: 0x0
Version: VER_NDX_LOCAL (0x0)
Value: 0x0
Size: 0x0
Expand Down Expand Up @@ -984,7 +984,7 @@ SectionHeader {
EntrySize: 0x18
Symbol {
Index: 0
Name: "" (0x0)
Name: 0x0
Value: 0x0
Size: 0x0
Type: STT_NOTYPE (0x0)
Expand Down
2 changes: 1 addition & 1 deletion crates/examples/testfiles/elf/base.o.readobj
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ SectionHeader {
EntrySize: 0x18
Symbol {
Index: 0
Name: "" (0x0)
Name: 0x0
Value: 0x0
Size: 0x0
Type: STT_NOTYPE (0x0)
Expand Down
10 changes: 5 additions & 5 deletions crates/examples/testfiles/elf/base.readobj
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ SectionHeader {
EntrySize: 0x18
Symbol {
Index: 0
Name: "" (0x0)
Name: 0x0
Version: VER_NDX_LOCAL (0x0)
Value: 0x0
Size: 0x0
Expand Down Expand Up @@ -547,19 +547,19 @@ SectionHeader {
Relocation {
Offset: 0x200DA8
Type: R_X86_64_RELATIVE (0x8)
Symbol: "" (0x0)
Symbol: 0x0
Addend: 0x670
}
Relocation {
Offset: 0x200DB0
Type: R_X86_64_RELATIVE (0x8)
Symbol: "" (0x0)
Symbol: 0x0
Addend: 0x630
}
Relocation {
Offset: 0x201008
Type: R_X86_64_RELATIVE (0x8)
Symbol: "" (0x0)
Symbol: 0x0
Addend: 0x201008
}
Relocation {
Expand Down Expand Up @@ -959,7 +959,7 @@ SectionHeader {
EntrySize: 0x18
Symbol {
Index: 0
Name: "" (0x0)
Name: 0x0
Value: 0x0
Size: 0x0
Type: STT_NOTYPE (0x0)
Expand Down
2 changes: 1 addition & 1 deletion crates/examples/testfiles/elf/nostd-mipsel.o.readobj
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ SectionHeader {
EntrySize: 0x10
Symbol {
Index: 0
Name: "" (0x0)
Name: 0x0
Value: 0x0
Size: 0x0
Type: STT_NOTYPE (0x0)
Expand Down
2 changes: 1 addition & 1 deletion crates/examples/testfiles/elf/nostd-mipsel.readobj
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ SectionHeader {
EntrySize: 0x10
Symbol {
Index: 0
Name: "" (0x0)
Name: 0x0
Value: 0x0
Size: 0x0
Type: STT_NOTYPE (0x0)
Expand Down
4 changes: 2 additions & 2 deletions crates/rewrite/testfiles/elf/base.delete-symbol
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ SectionHeader {
EntrySize: 0x18
Symbol {
Index: 0
Name: ""
Name: 0x0
Version: VER_NDX_LOCAL (0x0)
Value: 0x0
Size: 0x0
Expand Down Expand Up @@ -583,7 +583,7 @@ SectionHeader {
EntrySize: 0x18
Symbol {
Index: 0
Name: ""
Name: 0x0
Value: 0x0
Size: 0x0
Type: STT_NOTYPE (0x0)
Expand Down
10 changes: 5 additions & 5 deletions crates/rewrite/testfiles/elf/base.noop
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ SectionHeader {
EntrySize: 0x18
Symbol {
Index: 0
Name: ""
Name: 0x0
Version: VER_NDX_LOCAL (0x0)
Value: 0x0
Size: 0x0
Expand Down Expand Up @@ -547,19 +547,19 @@ SectionHeader {
Relocation {
Offset: 0x200DA8
Type: R_X86_64_RELATIVE (0x8)
Symbol: ""
Symbol: 0x0
Addend: 0x670
}
Relocation {
Offset: 0x200DB0
Type: R_X86_64_RELATIVE (0x8)
Symbol: ""
Symbol: 0x0
Addend: 0x630
}
Relocation {
Offset: 0x201008
Type: R_X86_64_RELATIVE (0x8)
Symbol: ""
Symbol: 0x0
Addend: 0x201008
}
Relocation {
Expand Down Expand Up @@ -959,7 +959,7 @@ SectionHeader {
EntrySize: 0x18
Symbol {
Index: 0
Name: ""
Name: 0x0
Value: 0x0
Size: 0x0
Type: STT_NOTYPE (0x0)
Expand Down
4 changes: 2 additions & 2 deletions crates/rewrite/testfiles/elf/base.rename-symbol
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ SectionHeader {
EntrySize: 0x18
Symbol {
Index: 0
Name: ""
Name: 0x0
Version: VER_NDX_LOCAL (0x0)
Value: 0x0
Size: 0x0
Expand Down Expand Up @@ -605,7 +605,7 @@ SectionHeader {
EntrySize: 0x18
Symbol {
Index: 0
Name: ""
Name: 0x0
Value: 0x0
Size: 0x0
Type: STT_NOTYPE (0x0)
Expand Down
6 changes: 6 additions & 0 deletions src/build/elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1034,15 +1034,21 @@ impl<'data> Builder<'data> {
return Err(Error::new(
".symtab.shndx section is needed but not present",
));
} else if symtab_shndx_id.is_some() {
writer.require_symtab_shndx();
}
if strtab_id.is_none() && writer.strtab_needed() {
return Err(Error::new(".strtab section is needed but not present"));
} else if strtab_id.is_some() {
writer.require_strtab();
}
if dynsym_id.is_none() && !out_dynsyms.is_empty() {
return Err(Error::new(".dynsym section is needed but not present"));
}
if dynstr_id.is_none() && writer.dynstr_needed() {
return Err(Error::new(".dynstr section is needed but not present"));
} else if dynstr_id.is_some() {
writer.require_dynstr();
}
if gnu_verdef_id.is_none() && verdef_count > 0 {
return Err(Error::new(
Expand Down
4 changes: 4 additions & 0 deletions src/read/elf/section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ impl<'data, Elf: FileHeader, R: ReadRef<'data>> SectionTable<'data, Elf, R> {

/// Return the string table at the given section index.
///
/// Returns an empty string table if the index is 0.
/// Returns an error if the section is not a string table.
#[inline]
pub fn strings(
Expand All @@ -94,6 +95,9 @@ impl<'data, Elf: FileHeader, R: ReadRef<'data>> SectionTable<'data, Elf, R> {
data: R,
index: SectionIndex,
) -> read::Result<StringTable<'data, R>> {
if index == SectionIndex(0) {
return Ok(StringTable::default());
}
self.section(index)?
.strings(endian, data)?
.read_error("Invalid ELF string section type")
Expand Down
Loading

0 comments on commit 5e225a2

Please sign in to comment.