Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

write/elf: fix writing of strtab when symtab is empty #710

Merged
merged 1 commit into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/write/elf/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ impl<'a> Writer<'a> {
///
/// This range is used for a section named `.strtab`.
///
/// This function does nothing if no strings were defined.
/// This function does nothing if a string table is not required.
/// This must be called after [`Self::add_string`].
pub fn reserve_strtab(&mut self) {
debug_assert_eq!(self.strtab_offset, 0);
Expand All @@ -684,13 +684,19 @@ impl<'a> Writer<'a> {

/// Reserve the section index for the string table.
///
/// You should check [`Self::strtab_needed`] before calling this
/// unless you have other means of knowing if this section is needed.
///
/// This must be called before [`Self::reserve_section_headers`].
pub fn reserve_strtab_section_index(&mut self) -> SectionIndex {
self.reserve_strtab_section_index_with_name(&b".strtab"[..])
}

/// Reserve the section index for the string table.
///
/// You should check [`Self::strtab_needed`] before calling this
/// unless you have other means of knowing if this section is needed.
///
/// This must be called before [`Self::reserve_section_headers`].
pub fn reserve_strtab_section_index_with_name(&mut self, name: &'a [u8]) -> SectionIndex {
debug_assert_eq!(self.strtab_index, SectionIndex(0));
Expand Down Expand Up @@ -732,6 +738,8 @@ impl<'a> Writer<'a> {
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)
}

Expand All @@ -752,6 +760,8 @@ impl<'a> Writer<'a> {
debug_assert_eq!(self.symtab_shndx_offset, 0);
if self.symtab_num == 0 {
self.symtab_num = 1;
// The symtab must link to a strtab.
self.need_strtab = true;
}
let index = self.symtab_num;
self.symtab_num += 1;
Expand Down Expand Up @@ -1052,13 +1062,19 @@ impl<'a> Writer<'a> {

/// Reserve the section index for the dynamic string table.
///
/// You should check [`Self::dynstr_needed`] before calling this
/// unless you have other means of knowing if this section is needed.
///
/// This must be called before [`Self::reserve_section_headers`].
pub fn reserve_dynstr_section_index(&mut self) -> SectionIndex {
self.reserve_dynstr_section_index_with_name(&b".dynstr"[..])
}

/// Reserve the section index for the dynamic string table.
///
/// You should check [`Self::dynstr_needed`] before calling this
/// unless you have other means of knowing if this section is needed.
///
/// This must be called before [`Self::reserve_section_headers`].
pub fn reserve_dynstr_section_index_with_name(&mut self, name: &'a [u8]) -> SectionIndex {
debug_assert_eq!(self.dynstr_index, SectionIndex(0));
Expand Down
14 changes: 14 additions & 0 deletions tests/round_trip/elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@ fn symtab_shndx() {
}
}

#[test]
fn empty_symtab() {
let object = write::Object::new(BinaryFormat::Elf, Architecture::X86_64, Endianness::Little);
let bytes = object.write().unwrap();

let object = read::File::parse(&*bytes).unwrap();
assert_eq!(object.format(), BinaryFormat::Elf);
assert_eq!(object.architecture(), Architecture::X86_64);
let symtab = object.section_by_name(".symtab").unwrap();
assert_eq!(symtab.size(), 24);
let strtab = object.section_by_name(".strtab").unwrap();
assert_eq!(strtab.size(), 1);
}

#[test]
fn aligned_sections() {
let mut object =
Expand Down
Loading