Combine data/text sections: Pad sections to alignment (#197)

* Combine data/text sections: Pad all sections to 4-byte minimum alignment

* Update x86 test snapshot

* Read and store object section alignment

* Combine data/text sections: Pad sections to more than 4-byte alignment if they have alignment specified
This commit is contained in:
LagoLunatic
2025-05-06 23:47:08 -04:00
committed by GitHub
parent d0e6c5c057
commit f263e490e3
14 changed files with 431 additions and 65 deletions
+12 -1
View File
@@ -9,7 +9,10 @@ use alloc::{
vec,
vec::Vec,
};
use core::{fmt, num::NonZeroU32};
use core::{
fmt,
num::{NonZeroU32, NonZeroU64},
};
use flagset::{FlagSet, flags};
@@ -70,6 +73,7 @@ pub struct Section {
pub kind: SectionKind,
pub data: SectionData,
pub flags: SectionFlagSet,
pub align: Option<NonZeroU64>,
pub relocations: Vec<Relocation>,
/// Line number info (.line or .debug_line section)
pub line_info: BTreeMap<u64, u32>,
@@ -105,6 +109,12 @@ impl Section {
}
}
// The alignment to use when "Combine data/text sections" is enabled.
pub fn combined_alignment(&self) -> u64 {
const MIN_ALIGNMENT: u64 = 4;
self.align.map(|align| align.get().max(MIN_ALIGNMENT)).unwrap_or(MIN_ALIGNMENT)
}
pub fn relocation_at<'obj>(
&'obj self,
obj: &'obj Object,
@@ -363,6 +373,7 @@ static DUMMY_SECTION: Section = Section {
kind: SectionKind::Unknown,
data: SectionData(Vec::new()),
flags: SectionFlagSet::empty(),
align: None,
relocations: Vec::new(),
line_info: BTreeMap::new(),
virtual_address: None,
+7 -2
View File
@@ -4,7 +4,7 @@ use alloc::{
string::{String, ToString},
vec::Vec,
};
use core::cmp::Ordering;
use core::{cmp::Ordering, num::NonZeroU64};
use anyhow::{Context, Result, anyhow, bail, ensure};
use object::{Object as _, ObjectSection as _, ObjectSymbol as _};
@@ -17,7 +17,7 @@ use crate::{
Symbol, SymbolFlag, SymbolKind,
split_meta::{SPLITMETA_SECTION, SplitMeta},
},
util::{read_u16, read_u32},
util::{align_data_slice_to, align_u64_to, read_u16, read_u32},
};
fn map_section_kind(section: &object::Section) -> SectionKind {
@@ -257,6 +257,7 @@ fn map_sections(
kind,
data: SectionData(data),
flags: Default::default(),
align: NonZeroU64::new(section.align()),
relocations: Default::default(),
virtual_address,
line_info: Default::default(),
@@ -739,7 +740,10 @@ fn do_combine_sections(
}
offsets.push(current_offset);
current_offset += section.size;
let align = section.combined_alignment();
current_offset = align_u64_to(current_offset, align);
data_size += section.data.len();
data_size = align_u64_to(data_size as u64, align) as usize;
num_relocations += section.relocations.len();
}
if data_size > 0 {
@@ -754,6 +758,7 @@ fn do_combine_sections(
let section = &mut sections[i];
section.size = 0;
data.append(&mut section.data.0);
align_data_slice_to(&mut data, section.combined_alignment());
section.relocations.iter_mut().for_each(|r| r.address += offset);
relocations.append(&mut section.relocations);
line_info.append(&mut section.line_info.iter().map(|(&a, &l)| (a + offset, l)).collect());
@@ -14,6 +14,7 @@ expression: "(sections, symbols)"
8,
),
flags: FlagSet(),
align: None,
relocations: [
Relocation {
flags: Elf(
@@ -53,6 +54,7 @@ expression: "(sections, symbols)"
12,
),
flags: FlagSet(Combined),
align: None,
relocations: [
Relocation {
flags: Elf(
@@ -87,6 +89,7 @@ expression: "(sections, symbols)"
0,
),
flags: FlagSet(Hidden),
align: None,
relocations: [],
line_info: {},
virtual_address: None,
@@ -101,6 +104,7 @@ expression: "(sections, symbols)"
0,
),
flags: FlagSet(Hidden),
align: None,
relocations: [],
line_info: {},
virtual_address: None,
+4 -11
View File
@@ -3,6 +3,10 @@ use alloc::{string::String, vec, vec::Vec};
use anyhow::{Result, anyhow};
use object::{Endian, ObjectSection, elf::SHT_NOTE};
#[cfg(feature = "std")]
use crate::util::align_data_to_4;
use crate::util::align_size_to_4;
pub const SPLITMETA_SECTION: &str = ".note.split";
pub const SHT_SPLITMETA: u32 = SHT_NOTE;
pub const ELF_NOTE_SPLIT: &[u8] = b"Split";
@@ -190,17 +194,6 @@ where E: Endian
}
}
fn align_size_to_4(size: usize) -> usize { (size + 3) & !3 }
#[cfg(feature = "std")]
fn align_data_to_4<W: std::io::Write + ?Sized>(writer: &mut W, len: usize) -> std::io::Result<()> {
const ALIGN_BYTES: &[u8] = &[0; 4];
if len % 4 != 0 {
writer.write_all(&ALIGN_BYTES[..4 - len % 4])?;
}
Ok(())
}
// ELF note format:
// Name Size | 4 bytes (integer)
// Desc Size | 4 bytes (integer)
+22 -1
View File
@@ -1,4 +1,4 @@
use alloc::format;
use alloc::{format, vec::Vec};
use core::fmt;
use anyhow::{Result, ensure};
@@ -39,3 +39,24 @@ pub fn read_u16(obj_file: &object::File, reader: &mut &[u8]) -> Result<u16> {
*reader = &reader[2..];
Ok(obj_file.endianness().read_u16(value))
}
pub fn align_size_to_4(size: usize) -> usize { (size + 3) & !3 }
#[cfg(feature = "std")]
pub fn align_data_to_4<W: std::io::Write + ?Sized>(
writer: &mut W,
len: usize,
) -> std::io::Result<()> {
const ALIGN_BYTES: &[u8] = &[0; 4];
if len % 4 != 0 {
writer.write_all(&ALIGN_BYTES[..4 - len % 4])?;
}
Ok(())
}
pub fn align_u64_to(len: u64, align: u64) -> u64 { len + ((align - (len % align)) % align) }
pub fn align_data_slice_to(data: &mut Vec<u8>, align: u64) {
const ALIGN_BYTE: u8 = 0;
data.resize(align_u64_to(data.len() as u64, align) as usize, ALIGN_BYTE);
}
@@ -1519,6 +1519,9 @@ Object {
556,
),
flags: FlagSet(),
align: Some(
1,
),
relocations: [
Relocation {
flags: Elf(
@@ -1718,6 +1721,9 @@ Object {
0,
),
flags: FlagSet(),
align: Some(
4,
),
relocations: [],
line_info: {},
virtual_address: None,
@@ -1732,6 +1738,9 @@ Object {
76,
),
flags: FlagSet(),
align: Some(
1,
),
relocations: [
Relocation {
flags: Elf(
@@ -1883,6 +1892,9 @@ Object {
0,
),
flags: FlagSet(),
align: Some(
4,
),
relocations: [],
line_info: {},
virtual_address: None,
@@ -1897,6 +1909,9 @@ Object {
0,
),
flags: FlagSet(),
align: Some(
4,
),
relocations: [],
line_info: {},
virtual_address: None,
@@ -1911,6 +1926,9 @@ Object {
0,
),
flags: FlagSet(),
align: Some(
1,
),
relocations: [],
line_info: {},
virtual_address: None,
@@ -1925,6 +1943,9 @@ Object {
0,
),
flags: FlagSet(),
align: Some(
1,
),
relocations: [],
line_info: {},
virtual_address: None,
@@ -3449,6 +3449,7 @@ Object {
0,
),
flags: FlagSet(),
align: None,
relocations: [],
line_info: {},
virtual_address: None,
@@ -3463,6 +3464,7 @@ Object {
0,
),
flags: FlagSet(),
align: None,
relocations: [],
line_info: {},
virtual_address: None,
@@ -3477,6 +3479,7 @@ Object {
0,
),
flags: FlagSet(),
align: None,
relocations: [],
line_info: {},
virtual_address: None,
@@ -3491,6 +3494,7 @@ Object {
0,
),
flags: FlagSet(),
align: None,
relocations: [],
line_info: {},
virtual_address: None,
@@ -3505,6 +3509,7 @@ Object {
0,
),
flags: FlagSet(),
align: None,
relocations: [],
line_info: {},
virtual_address: None,
@@ -3519,6 +3524,7 @@ Object {
0,
),
flags: FlagSet(),
align: None,
relocations: [],
line_info: {},
virtual_address: None,
@@ -3533,6 +3539,7 @@ Object {
0,
),
flags: FlagSet(),
align: None,
relocations: [],
line_info: {},
virtual_address: None,
@@ -3547,6 +3554,7 @@ Object {
0,
),
flags: FlagSet(),
align: None,
relocations: [],
line_info: {},
virtual_address: None,
@@ -3561,6 +3569,7 @@ Object {
0,
),
flags: FlagSet(),
align: None,
relocations: [],
line_info: {},
virtual_address: None,
@@ -3575,6 +3584,7 @@ Object {
0,
),
flags: FlagSet(),
align: None,
relocations: [],
line_info: {},
virtual_address: None,
@@ -3589,6 +3599,7 @@ Object {
0,
),
flags: FlagSet(),
align: None,
relocations: [],
line_info: {},
virtual_address: None,
@@ -3603,6 +3614,7 @@ Object {
0,
),
flags: FlagSet(),
align: None,
relocations: [],
line_info: {},
virtual_address: None,
@@ -3617,6 +3629,7 @@ Object {
0,
),
flags: FlagSet(),
align: None,
relocations: [],
line_info: {},
virtual_address: None,
@@ -3631,6 +3644,7 @@ Object {
0,
),
flags: FlagSet(),
align: None,
relocations: [],
line_info: {},
virtual_address: None,
@@ -3645,6 +3659,7 @@ Object {
0,
),
flags: FlagSet(),
align: None,
relocations: [],
line_info: {},
virtual_address: None,
@@ -3659,6 +3674,7 @@ Object {
0,
),
flags: FlagSet(),
align: None,
relocations: [],
line_info: {},
virtual_address: None,
@@ -3673,6 +3689,7 @@ Object {
0,
),
flags: FlagSet(),
align: None,
relocations: [],
line_info: {},
virtual_address: None,
@@ -3687,6 +3704,9 @@ Object {
244,
),
flags: FlagSet(),
align: Some(
4,
),
relocations: [
Relocation {
flags: Elf(
@@ -3797,6 +3817,7 @@ Object {
0,
),
flags: FlagSet(),
align: None,
relocations: [],
line_info: {},
virtual_address: None,
@@ -683,6 +683,9 @@ Object {
632,
),
flags: FlagSet(),
align: Some(
8,
),
relocations: [
Relocation {
flags: Elf(
@@ -1306,6 +1309,9 @@ Object {
0,
),
flags: FlagSet(),
align: Some(
4,
),
relocations: [],
line_info: {},
virtual_address: None,
@@ -1320,6 +1326,9 @@ Object {
0,
),
flags: FlagSet(),
align: Some(
1,
),
relocations: [],
line_info: {},
virtual_address: None,
@@ -1334,6 +1343,9 @@ Object {
0,
),
flags: FlagSet(),
align: Some(
1,
),
relocations: [],
line_info: {},
virtual_address: None,
@@ -1348,6 +1360,9 @@ Object {
0,
),
flags: FlagSet(),
align: Some(
4,
),
relocations: [],
line_info: {},
virtual_address: None,
@@ -1362,6 +1377,9 @@ Object {
0,
),
flags: FlagSet(),
align: Some(
4,
),
relocations: [],
line_info: {},
virtual_address: None,
@@ -1376,6 +1394,9 @@ Object {
0,
),
flags: FlagSet(),
align: Some(
1,
),
relocations: [],
line_info: {},
virtual_address: None,
@@ -1390,6 +1411,9 @@ Object {
43,
),
flags: FlagSet(),
align: Some(
8,
),
relocations: [],
line_info: {},
virtual_address: None,
@@ -1404,6 +1428,9 @@ Object {
76,
),
flags: FlagSet(),
align: Some(
8,
),
relocations: [],
line_info: {},
virtual_address: None,
@@ -1418,6 +1445,9 @@ Object {
0,
),
flags: FlagSet(),
align: Some(
4,
),
relocations: [],
line_info: {},
virtual_address: None,
@@ -1432,6 +1462,9 @@ Object {
0,
),
flags: FlagSet(),
align: Some(
1,
),
relocations: [],
line_info: {},
virtual_address: None,
@@ -1446,6 +1479,9 @@ Object {
0,
),
flags: FlagSet(),
align: Some(
1,
),
relocations: [],
line_info: {},
virtual_address: None,
@@ -317,6 +317,9 @@ Object {
552,
),
flags: FlagSet(),
align: Some(
4,
),
relocations: [
Relocation {
flags: Elf(
@@ -340,6 +343,9 @@ Object {
40,
),
flags: FlagSet(),
align: Some(
4,
),
relocations: [
Relocation {
flags: Elf(
@@ -363,6 +369,9 @@ Object {
36,
),
flags: FlagSet(),
align: Some(
4,
),
relocations: [
Relocation {
flags: Elf(
@@ -426,6 +435,9 @@ Object {
0,
),
flags: FlagSet(),
align: Some(
4,
),
relocations: [],
line_info: {},
virtual_address: None,
@@ -440,6 +452,9 @@ Object {
0,
),
flags: FlagSet(),
align: Some(
4,
),
relocations: [],
line_info: {},
virtual_address: None,
@@ -454,6 +469,9 @@ Object {
0,
),
flags: FlagSet(),
align: Some(
4,
),
relocations: [],
line_info: {},
virtual_address: None,
@@ -468,6 +486,9 @@ Object {
0,
),
flags: FlagSet(),
align: Some(
4,
),
relocations: [],
line_info: {},
virtual_address: None,
@@ -482,6 +503,9 @@ Object {
0,
),
flags: FlagSet(),
align: Some(
1,
),
relocations: [],
line_info: {},
virtual_address: None,
@@ -496,6 +520,9 @@ Object {
0,
),
flags: FlagSet(),
align: Some(
1,
),
relocations: [],
line_info: {},
virtual_address: None,
@@ -510,6 +537,9 @@ Object {
0,
),
flags: FlagSet(),
align: Some(
1,
),
relocations: [],
line_info: {},
virtual_address: None,
@@ -1,6 +1,5 @@
---
source: objdiff-core/tests/arch_ppc.rs
assertion_line: 14
expression: obj
---
Object {
@@ -167,6 +166,9 @@ Object {
284,
),
flags: FlagSet(),
align: Some(
4,
),
relocations: [
Relocation {
flags: Elf(
@@ -392,6 +394,9 @@ Object {
4,
),
flags: FlagSet(),
align: Some(
4,
),
relocations: [
Relocation {
flags: Elf(
@@ -417,6 +422,9 @@ Object {
0,
),
flags: FlagSet(),
align: Some(
8,
),
relocations: [],
line_info: {},
virtual_address: Some(
@@ -433,6 +441,9 @@ Object {
0,
),
flags: FlagSet(),
align: Some(
4,
),
relocations: [],
line_info: {},
virtual_address: None,
@@ -447,6 +458,9 @@ Object {
0,
),
flags: FlagSet(),
align: Some(
4,
),
relocations: [],
line_info: {},
virtual_address: None,
@@ -461,6 +475,9 @@ Object {
0,
),
flags: FlagSet(),
align: Some(
4,
),
relocations: [],
line_info: {},
virtual_address: None,
@@ -475,6 +492,9 @@ Object {
0,
),
flags: FlagSet(),
align: Some(
1,
),
relocations: [],
line_info: {},
virtual_address: None,
@@ -489,6 +509,9 @@ Object {
0,
),
flags: FlagSet(),
align: Some(
1,
),
relocations: [],
line_info: {},
virtual_address: None,
@@ -503,6 +526,9 @@ Object {
0,
),
flags: FlagSet(),
align: Some(
1,
),
relocations: [],
line_info: {},
virtual_address: None,
@@ -517,6 +543,9 @@ Object {
0,
),
flags: FlagSet(),
align: Some(
4,
),
relocations: [],
line_info: {},
virtual_address: None,
@@ -136,6 +136,9 @@ Object {
0,
),
flags: FlagSet(),
align: Some(
1,
),
relocations: [],
line_info: {},
virtual_address: None,
@@ -150,6 +153,9 @@ Object {
10,
),
flags: FlagSet(),
align: Some(
4,
),
relocations: [
Relocation {
flags: Coff(
@@ -173,6 +179,9 @@ Object {
18,
),
flags: FlagSet(),
align: Some(
16,
),
relocations: [
Relocation {
flags: Coff(
@@ -866,6 +866,9 @@ Object {
0,
),
flags: FlagSet(),
align: Some(
1,
),
relocations: [],
line_info: {},
virtual_address: None,
@@ -880,6 +883,9 @@ Object {
0,
),
flags: FlagSet(),
align: Some(
1,
),
relocations: [],
line_info: {},
virtual_address: None,
@@ -894,6 +900,9 @@ Object {
429,
),
flags: FlagSet(),
align: Some(
16,
),
relocations: [
Relocation {
flags: Coff(
@@ -1029,6 +1038,9 @@ Object {
141,
),
flags: FlagSet(),
align: Some(
16,
),
relocations: [],
line_info: {},
virtual_address: None,
@@ -1043,6 +1055,9 @@ Object {
87,
),
flags: FlagSet(),
align: Some(
16,
),
relocations: [],
line_info: {},
virtual_address: None,
@@ -1057,6 +1072,9 @@ Object {
105,
),
flags: FlagSet(),
align: Some(
16,
),
relocations: [],
line_info: {},
virtual_address: None,
@@ -1071,6 +1089,9 @@ Object {
82,
),
flags: FlagSet(),
align: Some(
16,
),
relocations: [],
line_info: {},
virtual_address: None,
@@ -1085,6 +1106,9 @@ Object {
8,
),
flags: FlagSet(),
align: Some(
4,
),
relocations: [],
line_info: {},
virtual_address: None,
@@ -1099,6 +1123,9 @@ Object {
12,
),
flags: FlagSet(),
align: Some(
4,
),
relocations: [
Relocation {
flags: Coff(
@@ -1138,6 +1165,9 @@ Object {
8,
),
flags: FlagSet(),
align: Some(
4,
),
relocations: [],
line_info: {},
virtual_address: None,
@@ -1152,6 +1182,9 @@ Object {
12,
),
flags: FlagSet(),
align: Some(
4,
),
relocations: [
Relocation {
flags: Coff(
@@ -1191,6 +1224,9 @@ Object {
8,
),
flags: FlagSet(),
align: Some(
4,
),
relocations: [],
line_info: {},
virtual_address: None,
@@ -1205,6 +1241,9 @@ Object {
12,
),
flags: FlagSet(),
align: Some(
4,
),
relocations: [
Relocation {
flags: Coff(
@@ -1244,6 +1283,9 @@ Object {
8,
),
flags: FlagSet(),
align: Some(
4,
),
relocations: [],
line_info: {},
virtual_address: None,
@@ -1258,6 +1300,9 @@ Object {
12,
),
flags: FlagSet(),
align: Some(
4,
),
relocations: [
Relocation {
flags: Coff(
@@ -1297,6 +1342,9 @@ Object {
256,
),
flags: FlagSet(),
align: Some(
16,
),
relocations: [
Relocation {
flags: Coff(
@@ -1344,6 +1392,9 @@ Object {
20,
),
flags: FlagSet(),
align: Some(
4,
),
relocations: [
Relocation {
flags: Coff(
@@ -1367,6 +1418,9 @@ Object {
12,
),
flags: FlagSet(),
align: Some(
4,
),
relocations: [
Relocation {
flags: Coff(
@@ -1406,6 +1460,9 @@ Object {
0,
),
flags: FlagSet(),
align: Some(
4,
),
relocations: [],
line_info: {},
virtual_address: None,
@@ -1420,6 +1477,9 @@ Object {
8,
),
flags: FlagSet(),
align: Some(
8,
),
relocations: [
Relocation {
flags: Coff(
@@ -1443,6 +1503,9 @@ Object {
8,
),
flags: FlagSet(),
align: Some(
8,
),
relocations: [
Relocation {
flags: Coff(
@@ -1466,6 +1529,9 @@ Object {
4,
),
flags: FlagSet(),
align: Some(
4,
),
relocations: [],
line_info: {},
virtual_address: None,
@@ -1480,6 +1546,9 @@ Object {
4,
),
flags: FlagSet(),
align: Some(
4,
),
relocations: [],
line_info: {},
virtual_address: None,
@@ -1494,6 +1563,9 @@ Object {
0,
),
flags: FlagSet(),
align: Some(
16,
),
relocations: [],
line_info: {},
virtual_address: None,
File diff suppressed because it is too large Load Diff
@@ -201,6 +201,9 @@ Object {
0,
),
flags: FlagSet(),
align: Some(
1,
),
relocations: [],
line_info: {},
virtual_address: None,
@@ -215,6 +218,9 @@ Object {
96,
),
flags: FlagSet(),
align: Some(
16,
),
relocations: [
Relocation {
flags: Coff(
@@ -294,6 +300,9 @@ Object {
0,
),
flags: FlagSet(),
align: Some(
1,
),
relocations: [],
line_info: {},
virtual_address: None,