Skip to content

Commit e26dae8

Browse files
committed
Combine data/text sections: Pad all sections to 4-byte minimum alignment
1 parent e1c51ac commit e26dae8

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-13
lines changed

objdiff-core/src/obj/read.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::{
1717
Symbol, SymbolFlag, SymbolKind,
1818
split_meta::{SPLITMETA_SECTION, SplitMeta},
1919
},
20-
util::{read_u16, read_u32},
20+
util::{align_data_slice_to_4, align_size_to_4, align_u64_to_4, read_u16, read_u32},
2121
};
2222

2323
fn map_section_kind(section: &object::Section) -> SectionKind {
@@ -739,7 +739,9 @@ fn do_combine_sections(
739739
}
740740
offsets.push(current_offset);
741741
current_offset += section.size;
742+
current_offset = align_u64_to_4(current_offset);
742743
data_size += section.data.len();
744+
data_size = align_size_to_4(data_size);
743745
num_relocations += section.relocations.len();
744746
}
745747
if data_size > 0 {
@@ -754,6 +756,7 @@ fn do_combine_sections(
754756
let section = &mut sections[i];
755757
section.size = 0;
756758
data.append(&mut section.data.0);
759+
align_data_slice_to_4(&mut data);
757760
section.relocations.iter_mut().for_each(|r| r.address += offset);
758761
relocations.append(&mut section.relocations);
759762
line_info.append(&mut section.line_info.iter().map(|(&a, &l)| (a + offset, l)).collect());

objdiff-core/src/obj/split_meta.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ use alloc::{string::String, vec, vec::Vec};
33
use anyhow::{Result, anyhow};
44
use object::{Endian, ObjectSection, elf::SHT_NOTE};
55

6+
#[cfg(feature = "std")]
7+
use crate::util::align_data_to_4;
8+
use crate::util::align_size_to_4;
9+
610
pub const SPLITMETA_SECTION: &str = ".note.split";
711
pub const SHT_SPLITMETA: u32 = SHT_NOTE;
812
pub const ELF_NOTE_SPLIT: &[u8] = b"Split";
@@ -190,17 +194,6 @@ where E: Endian
190194
}
191195
}
192196

193-
fn align_size_to_4(size: usize) -> usize { (size + 3) & !3 }
194-
195-
#[cfg(feature = "std")]
196-
fn align_data_to_4<W: std::io::Write + ?Sized>(writer: &mut W, len: usize) -> std::io::Result<()> {
197-
const ALIGN_BYTES: &[u8] = &[0; 4];
198-
if len % 4 != 0 {
199-
writer.write_all(&ALIGN_BYTES[..4 - len % 4])?;
200-
}
201-
Ok(())
202-
}
203-
204197
// ELF note format:
205198
// Name Size | 4 bytes (integer)
206199
// Desc Size | 4 bytes (integer)

objdiff-core/src/util.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use alloc::format;
1+
use alloc::{format, vec::Vec};
22
use core::fmt;
33

44
use anyhow::{Result, ensure};
@@ -39,3 +39,24 @@ pub fn read_u16(obj_file: &object::File, reader: &mut &[u8]) -> Result<u16> {
3939
*reader = &reader[2..];
4040
Ok(obj_file.endianness().read_u16(value))
4141
}
42+
43+
pub fn align_size_to_4(size: usize) -> usize { (size + 3) & !3 }
44+
45+
pub fn align_u64_to_4(size: u64) -> u64 { (size + 3) & !3 }
46+
47+
#[cfg(feature = "std")]
48+
pub fn align_data_to_4<W: std::io::Write + ?Sized>(
49+
writer: &mut W,
50+
len: usize,
51+
) -> std::io::Result<()> {
52+
const ALIGN_BYTES: &[u8] = &[0; 4];
53+
if len % 4 != 0 {
54+
writer.write_all(&ALIGN_BYTES[..4 - len % 4])?;
55+
}
56+
Ok(())
57+
}
58+
59+
pub fn align_data_slice_to_4(data: &mut Vec<u8>) {
60+
const ALIGN_BYTE: u8 = 0;
61+
data.resize(align_size_to_4(data.len()), ALIGN_BYTE);
62+
}

0 commit comments

Comments
 (0)