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

Fix writing of aligned empty sections #540

Merged
merged 1 commit into from
May 9, 2023
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
9 changes: 3 additions & 6 deletions src/write/elf/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,12 +323,9 @@ impl<'a> Object<'a> {
}
}
for (index, section) in self.sections.iter().enumerate() {
let len = section.data.len();
if len != 0 {
writer.write_align(section.align as usize);
debug_assert_eq!(section_offsets[index].offset, writer.len());
writer.write(&section.data);
}
writer.write_align(section.align as usize);
debug_assert_eq!(section_offsets[index].offset, writer.len());
writer.write(&section.data);
}

// Write symbols.
Expand Down
23 changes: 23 additions & 0 deletions tests/aligned_sections.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#![cfg(feature = "write")]

use object::{
write::{Object, StreamingBuffer},
Architecture, BinaryFormat, Endianness, SectionKind,
};

#[test]
fn aligned_sections() {
let mut object = Object::new(BinaryFormat::Elf, Architecture::X86_64, Endianness::Little);

let text_section_id = object.add_section(vec![], b".text".to_vec(), SectionKind::Text);
let text_section = object.section_mut(text_section_id);
text_section.set_data(&[][..], 4096);

let data_section_id = object.add_section(vec![], b".data".to_vec(), SectionKind::Data);
let data_section = object.section_mut(data_section_id);
data_section.set_data(&b"1234"[..], 16);

let mut buffer = StreamingBuffer::new(vec![]);
object.emit(&mut buffer).unwrap();
buffer.result().unwrap();
}