Skip to content

Commit 1007e24

Browse files
committed
Combine data/text sections: Pad sections to more than 4-byte alignment if they have alignment specified
1 parent 6ca5007 commit 1007e24

File tree

4 files changed

+32
-25
lines changed

4 files changed

+32
-25
lines changed

objdiff-core/src/obj/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ impl Section {
109109
}
110110
}
111111

112+
// The alignment to use when "Combine data/text sections" is enabled.
113+
pub fn combined_alignment(&self) -> u64 {
114+
const MIN_ALIGNMENT: u64 = 4;
115+
self.align.map(|align| align.get().max(MIN_ALIGNMENT)).unwrap_or(MIN_ALIGNMENT)
116+
}
117+
112118
pub fn relocation_at<'obj>(
113119
&'obj self,
114120
obj: &'obj Object,

objdiff-core/src/obj/read.rs

Lines changed: 5 additions & 4 deletions
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::{align_data_slice_to_4, align_size_to_4, align_u64_to_4, read_u16, read_u32},
20+
util::{align_data_slice_to, align_u64_to, read_u16, read_u32},
2121
};
2222

2323
fn map_section_kind(section: &object::Section) -> SectionKind {
@@ -740,9 +740,10 @@ fn do_combine_sections(
740740
}
741741
offsets.push(current_offset);
742742
current_offset += section.size;
743-
current_offset = align_u64_to_4(current_offset);
743+
let align = section.combined_alignment();
744+
current_offset = align_u64_to(current_offset, align);
744745
data_size += section.data.len();
745-
data_size = align_size_to_4(data_size);
746+
data_size = align_u64_to(data_size as u64, align) as usize;
746747
num_relocations += section.relocations.len();
747748
}
748749
if data_size > 0 {
@@ -757,7 +758,7 @@ fn do_combine_sections(
757758
let section = &mut sections[i];
758759
section.size = 0;
759760
data.append(&mut section.data.0);
760-
align_data_slice_to_4(&mut data);
761+
align_data_slice_to(&mut data, section.combined_alignment());
761762
section.relocations.iter_mut().for_each(|r| r.address += offset);
762763
relocations.append(&mut section.relocations);
763764
line_info.append(&mut section.line_info.iter().map(|(&a, &l)| (a + offset, l)).collect());

objdiff-core/src/util.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ pub fn read_u16(obj_file: &object::File, reader: &mut &[u8]) -> Result<u16> {
4242

4343
pub fn align_size_to_4(size: usize) -> usize { (size + 3) & !3 }
4444

45-
pub fn align_u64_to_4(size: u64) -> u64 { (size + 3) & !3 }
46-
4745
#[cfg(feature = "std")]
4846
pub fn align_data_to_4<W: std::io::Write + ?Sized>(
4947
writer: &mut W,
@@ -56,7 +54,9 @@ pub fn align_data_to_4<W: std::io::Write + ?Sized>(
5654
Ok(())
5755
}
5856

59-
pub fn align_data_slice_to_4(data: &mut Vec<u8>) {
57+
pub fn align_u64_to(len: u64, align: u64) -> u64 { len + ((align - (len % align)) % align) }
58+
59+
pub fn align_data_slice_to(data: &mut Vec<u8>, align: u64) {
6060
const ALIGN_BYTE: u8 = 0;
61-
data.resize(align_size_to_4(data.len()), ALIGN_BYTE);
61+
data.resize(align_u64_to(data.len() as u64, align) as usize, ALIGN_BYTE);
6262
}

objdiff-core/tests/snapshots/arch_x86__read_x86_combine_sections.snap

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -747,10 +747,10 @@ expression: obj.sections
747747
id: ".text-combined",
748748
name: ".text",
749749
address: 0,
750-
size: 280,
750+
size: 320,
751751
kind: Code,
752752
data: SectionData(
753-
280,
753+
320,
754754
),
755755
flags: FlagSet(Combined),
756756
align: Some(
@@ -777,119 +777,119 @@ expression: obj.sections
777777
flags: Coff(
778778
20,
779779
),
780-
address: 31,
780+
address: 43,
781781
target_symbol: 60,
782782
addend: 0,
783783
},
784784
Relocation {
785785
flags: Coff(
786786
20,
787787
),
788-
address: 50,
788+
address: 62,
789789
target_symbol: 52,
790790
addend: 0,
791791
},
792792
Relocation {
793793
flags: Coff(
794794
20,
795795
),
796-
address: 72,
796+
address: 84,
797797
target_symbol: 11,
798798
addend: 0,
799799
},
800800
Relocation {
801801
flags: Coff(
802802
6,
803803
),
804-
address: 88,
804+
address: 100,
805805
target_symbol: 64,
806806
addend: 0,
807807
},
808808
Relocation {
809809
flags: Coff(
810810
6,
811811
),
812-
address: 108,
812+
address: 124,
813813
target_symbol: 66,
814814
addend: 0,
815815
},
816816
Relocation {
817817
flags: Coff(
818818
6,
819819
),
820-
address: 128,
820+
address: 156,
821821
target_symbol: 6,
822822
addend: 0,
823823
},
824824
Relocation {
825825
flags: Coff(
826826
6,
827827
),
828-
address: 138,
828+
address: 166,
829829
target_symbol: 8,
830830
addend: 0,
831831
},
832832
Relocation {
833833
flags: Coff(
834834
20,
835835
),
836-
address: 149,
836+
address: 177,
837837
target_symbol: 57,
838838
addend: 0,
839839
},
840840
Relocation {
841841
flags: Coff(
842842
20,
843843
),
844-
address: 157,
844+
address: 185,
845845
target_symbol: 54,
846846
addend: 0,
847847
},
848848
Relocation {
849849
flags: Coff(
850850
20,
851851
),
852-
address: 179,
852+
address: 219,
853853
target_symbol: 54,
854854
addend: 0,
855855
},
856856
Relocation {
857857
flags: Coff(
858858
20,
859859
),
860-
address: 198,
860+
address: 238,
861861
target_symbol: 52,
862862
addend: 0,
863863
},
864864
Relocation {
865865
flags: Coff(
866866
20,
867867
),
868-
address: 227,
868+
address: 267,
869869
target_symbol: 57,
870870
addend: 0,
871871
},
872872
Relocation {
873873
flags: Coff(
874874
20,
875875
),
876-
address: 246,
876+
address: 286,
877877
target_symbol: 52,
878878
addend: 0,
879879
},
880880
Relocation {
881881
flags: Coff(
882882
6,
883883
),
884-
address: 268,
884+
address: 308,
885885
target_symbol: 68,
886886
addend: 0,
887887
},
888888
Relocation {
889889
flags: Coff(
890890
20,
891891
),
892-
address: 273,
892+
address: 313,
893893
target_symbol: 60,
894894
addend: 0,
895895
},

0 commit comments

Comments
 (0)