Skip to content

Fix size of .note.split section #61

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

Merged
merged 2 commits into from
May 17, 2024
Merged
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
14 changes: 10 additions & 4 deletions objdiff-core/src/obj/split_meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ impl SplitMeta {
if let Some(generator) = &self.generator {
write_note_header(writer, e, NT_SPLIT_GENERATOR, generator.len())?;
writer.write_all(generator.as_bytes())?;
align_to_4(writer, generator.len())?;
align_data_to_4(writer, generator.len())?;
}
if let Some(module_name) = &self.module_name {
write_note_header(writer, e, NT_SPLIT_MODULE_NAME, module_name.len())?;
writer.write_all(module_name.as_bytes())?;
align_to_4(writer, module_name.len())?;
align_data_to_4(writer, module_name.len())?;
}
if let Some(module_id) = self.module_id {
write_note_header(writer, e, NT_SPLIT_MODULE_ID, 4)?;
Expand All @@ -119,15 +119,19 @@ impl SplitMeta {
let mut size = 0;
if let Some(generator) = self.generator.as_deref() {
size += NOTE_HEADER_SIZE + generator.len();
size = align_size_to_4(size);
}
if let Some(module_name) = self.module_name.as_deref() {
size += NOTE_HEADER_SIZE + module_name.len();
size = align_size_to_4(size);
}
if self.module_id.is_some() {
size += NOTE_HEADER_SIZE + 4;
size = align_size_to_4(size);
}
if let Some(virtual_addresses) = self.virtual_addresses.as_deref() {
size += NOTE_HEADER_SIZE + if is_64 { 8 } else { 4 } * virtual_addresses.len();
size = align_size_to_4(size);
}
size
}
Expand Down Expand Up @@ -186,7 +190,9 @@ where E: Endian
}
}

fn align_to_4<W: Write + ?Sized>(writer: &mut W, len: usize) -> io::Result<()> {
fn align_size_to_4(size: usize) -> usize { (size + 3) & !3 }

fn align_data_to_4<W: Write + ?Sized>(writer: &mut W, len: usize) -> io::Result<()> {
const ALIGN_BYTES: &[u8] = &[0; 4];
if len % 4 != 0 {
writer.write_all(&ALIGN_BYTES[..4 - len % 4])?;
Expand All @@ -212,6 +218,6 @@ where
writer.write_all(&e.write_u32_bytes(kind))?; // Type
writer.write_all(ELF_NOTE_SPLIT)?; // Name
writer.write_all(&[0; 1])?; // Null terminator
align_to_4(writer, ELF_NOTE_SPLIT.len() + 1)?;
align_data_to_4(writer, ELF_NOTE_SPLIT.len() + 1)?;
Ok(())
}