Skip to content

Commit 0feeab8

Browse files
committed
read/macho: fix data for segments in dyld caches
This is for operations on the segment itself, and for section relocations. Individual section data was already using the correct source.
1 parent 9650d64 commit 0feeab8

File tree

3 files changed

+14
-31
lines changed

3 files changed

+14
-31
lines changed

Diff for: src/read/macho/file.rs

+4-15
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ where
4444
pub(super) header_offset: u64,
4545
pub(super) header: &'data Mach,
4646
pub(super) segments: Vec<MachOSegmentInternal<'data, Mach, R>>,
47-
pub(super) sections: Vec<MachOSectionInternal<'data, Mach>>,
47+
pub(super) sections: Vec<MachOSectionInternal<'data, Mach, R>>,
4848
pub(super) symbols: SymbolTable<'data, Mach, R>,
4949
}
5050

@@ -65,11 +65,10 @@ where
6565
if let Ok(mut commands) = header.load_commands(endian, data, 0) {
6666
while let Ok(Some(command)) = commands.next() {
6767
if let Some((segment, section_data)) = Mach::Segment::from_command(command)? {
68-
let segment_index = segments.len();
6968
segments.push(MachOSegmentInternal { segment, data });
7069
for section in segment.sections(endian, section_data)? {
7170
let index = SectionIndex(sections.len() + 1);
72-
sections.push(MachOSectionInternal::parse(index, segment_index, section));
71+
sections.push(MachOSectionInternal::parse(index, section, data));
7372
}
7473
} else if let Some(symtab) = command.symtab()? {
7574
symbols = symtab.symbols(endian, data)?;
@@ -118,12 +117,11 @@ where
118117
if segment.name() == macho::SEG_LINKEDIT.as_bytes() {
119118
linkedit_data = Some(data);
120119
}
121-
let segment_index = segments.len();
122120
segments.push(MachOSegmentInternal { segment, data });
123121

124122
for section in segment.sections(endian, section_data)? {
125123
let index = SectionIndex(sections.len() + 1);
126-
sections.push(MachOSectionInternal::parse(index, segment_index, section));
124+
sections.push(MachOSectionInternal::parse(index, section, data));
127125
}
128126
} else if let Some(st) = command.symtab()? {
129127
symtab = Some(st);
@@ -154,23 +152,14 @@ where
154152
pub(super) fn section_internal(
155153
&self,
156154
index: SectionIndex,
157-
) -> Result<&MachOSectionInternal<'data, Mach>> {
155+
) -> Result<&MachOSectionInternal<'data, Mach, R>> {
158156
index
159157
.0
160158
.checked_sub(1)
161159
.and_then(|index| self.sections.get(index))
162160
.read_error("Invalid Mach-O section index")
163161
}
164162

165-
pub(super) fn segment_internal(
166-
&self,
167-
index: usize,
168-
) -> Result<&MachOSegmentInternal<'data, Mach, R>> {
169-
self.segments
170-
.get(index)
171-
.read_error("Invalid Mach-O segment index")
172-
}
173-
174163
/// Returns the endianness.
175164
pub fn endian(&self) -> Mach::Endian {
176165
self.endian

Diff for: src/read/macho/section.rs

+9-15
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ where
2525
R: ReadRef<'data>,
2626
{
2727
pub(super) file: &'file MachOFile<'data, Mach, R>,
28-
pub(super) iter: slice::Iter<'file, MachOSectionInternal<'data, Mach>>,
28+
pub(super) iter: slice::Iter<'file, MachOSectionInternal<'data, Mach, R>>,
2929
}
3030

3131
impl<'data, 'file, Mach, R> fmt::Debug for MachOSectionIterator<'data, 'file, Mach, R>
@@ -71,7 +71,7 @@ where
7171
R: ReadRef<'data>,
7272
{
7373
pub(super) file: &'file MachOFile<'data, Mach, R>,
74-
pub(super) internal: MachOSectionInternal<'data, Mach>,
74+
pub(super) internal: MachOSectionInternal<'data, Mach, R>,
7575
}
7676

7777
impl<'data, 'file, Mach, R> MachOSection<'data, 'file, Mach, R>
@@ -80,11 +80,9 @@ where
8080
R: ReadRef<'data>,
8181
{
8282
fn bytes(&self) -> Result<&'data [u8]> {
83-
let segment_index = self.internal.segment_index;
84-
let segment = self.file.segment_internal(segment_index)?;
8583
self.internal
8684
.section
87-
.data(self.file.endian, segment.data)
85+
.data(self.file.endian, self.internal.data)
8886
.read_error("Invalid Mach-O section size or offset")
8987
}
9088
}
@@ -193,7 +191,7 @@ where
193191
relocations: self
194192
.internal
195193
.section
196-
.relocations(self.file.endian, self.file.data)
194+
.relocations(self.file.endian, self.internal.data)
197195
.unwrap_or(&[])
198196
.iter(),
199197
}
@@ -211,19 +209,15 @@ where
211209
}
212210

213211
#[derive(Debug, Clone, Copy)]
214-
pub(super) struct MachOSectionInternal<'data, Mach: MachHeader> {
212+
pub(super) struct MachOSectionInternal<'data, Mach: MachHeader, R: ReadRef<'data>> {
215213
pub index: SectionIndex,
216-
pub segment_index: usize,
217214
pub kind: SectionKind,
218215
pub section: &'data Mach::Section,
216+
pub data: R,
219217
}
220218

221-
impl<'data, Mach: MachHeader> MachOSectionInternal<'data, Mach> {
222-
pub(super) fn parse(
223-
index: SectionIndex,
224-
segment_index: usize,
225-
section: &'data Mach::Section,
226-
) -> Self {
219+
impl<'data, Mach: MachHeader, R: ReadRef<'data>> MachOSectionInternal<'data, Mach, R> {
220+
pub(super) fn parse(index: SectionIndex, section: &'data Mach::Section, data: R) -> Self {
227221
// TODO: we don't validate flags, should we?
228222
let kind = match (section.segment_name(), section.name()) {
229223
(b"__TEXT", b"__text") => SectionKind::Text,
@@ -246,9 +240,9 @@ impl<'data, Mach: MachHeader> MachOSectionInternal<'data, Mach> {
246240
};
247241
MachOSectionInternal {
248242
index,
249-
segment_index,
250243
kind,
251244
section,
245+
data,
252246
}
253247
}
254248
}

Diff for: src/read/macho/segment.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ where
6969
fn bytes(&self) -> Result<&'data [u8]> {
7070
self.internal
7171
.segment
72-
.data(self.file.endian, self.file.data)
72+
.data(self.file.endian, self.internal.data)
7373
.read_error("Invalid Mach-O segment size or offset")
7474
}
7575
}

0 commit comments

Comments
 (0)