Skip to content

Commit 4fca90e

Browse files
committed
Fix to read correctly even when linkedit does not exist in the same cache
1 parent 77546a2 commit 4fca90e

File tree

3 files changed

+39
-22
lines changed

3 files changed

+39
-22
lines changed

Sources/MachOKit/MachOFile+ExportTrie.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,10 @@ extension MachOFile.ExportTrie {
113113
exportSize: Int,
114114
ldVersion: Version?
115115
) {
116-
let offset = machO.headerStartOffset + exportOffset
117-
let data = try! machO.fileHandle.readData(
118-
offset: offset,
116+
let data = machO._readLinkEditData(
117+
offset: exportOffset,
119118
length: exportSize
120-
)
119+
)!
121120

122121
self.init(
123122
exportOffset: exportOffset,

Sources/MachOKit/MachOFile+FunctionStarts.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,10 @@ extension MachOFile.FunctionStarts {
3131
functionStartsSize: Int,
3232
functionStartBase: UInt
3333
) {
34-
let offset = machO.headerStartOffset + functionStartsOffset
35-
let data = try! machO.fileHandle.readData(
36-
offset: offset,
34+
let data = machO._readLinkEditData(
35+
offset: functionStartsOffset,
3736
length: functionStartsSize
38-
)
37+
)!
3938

4039
self.init(
4140
data: data,

Sources/MachOKit/MachOFile.swift

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -421,8 +421,13 @@ extension MachOFile {
421421
return nil
422422
}
423423

424-
let entries: DataSequence<DataInCodeEntry> = fileHandle.readDataSequence(
425-
offset: numericCast(headerStartOffset) + numericCast(dataInCode.dataoff),
424+
guard let data = _readLinkEditData(
425+
offset: numericCast(dataInCode.dataoff),
426+
length: numericCast(dataInCode.datasize)
427+
) else { return nil }
428+
429+
let entries: DataSequence<DataInCodeEntry> = .init(
430+
data: data,
426431
numberOfElements: numericCast(dataInCode.datasize) / DataInCodeEntry.layoutSize
427432
)
428433

@@ -449,12 +454,13 @@ extension MachOFile {
449454
guard let info = loadCommands.dyldChainedFixups else {
450455
return nil
451456
}
457+
guard let fileSlice = _fileSliceForLinkEditData(
458+
offset: numericCast(info.dataoff),
459+
length: numericCast(info.datasize)
460+
) else { return nil }
452461

453462
return .init(
454-
fileSice: try! fileHandle.fileSlice(
455-
offset: headerStartOffset + numericCast(info.dataoff),
456-
length: numericCast(info.datasize)
457-
),
463+
fileSice: fileSlice,
458464
isSwapped: isSwapped
459465
)
460466
}
@@ -508,11 +514,13 @@ extension MachOFile {
508514
guard let info = loadCommands.codeSignature else {
509515
return nil
510516
}
517+
guard let fileSlice = _fileSliceForLinkEditData(
518+
offset: numericCast(info.dataoff),
519+
length: numericCast(info.datasize)
520+
) else { return nil }
521+
511522
return .init(
512-
fileSice: try! fileHandle.fileSlice(
513-
offset: headerStartOffset + numericCast(info.dataoff),
514-
length: numericCast(info.datasize)
515-
)
523+
fileSice: fileSlice
516524
)
517525
}
518526
}
@@ -600,10 +608,10 @@ extension MachOFile {
600608
}
601609

602610
extension MachOFile {
603-
internal func _readLinkEditData(
611+
internal func _fileSliceForLinkEditData(
604612
offset: Int, // linkedit_data_command->dataoff (linkedit.fileoff + x)
605613
length: Int
606-
) -> Data? {
614+
) -> File.FileSlice? {
607615
let linkedit: (any SegmentCommandProtocol)? = loadCommands.linkedit64 ?? loadCommands.linkedit
608616
guard let linkedit else { return nil }
609617
guard linkedit.fileOffset + linkedit.fileSize >= offset + length else { return nil }
@@ -621,17 +629,28 @@ extension MachOFile {
621629
) else {
622630
return nil
623631
}
624-
return try? segment._file.readData(
632+
return try? segment._file.fileSlice(
625633
offset: numericCast(fileOffset) - segment.offset,
626634
length: length
627635
)
628636
} else {
629-
return try? fileHandle.readData(
637+
return try? fileHandle.fileSlice(
630638
offset: headerStartOffset + offset,
631639
length: length
632640
)
633641
}
634642
}
643+
644+
internal func _readLinkEditData(
645+
offset: Int, // linkedit_data_command->dataoff (linkedit.fileoff + x)
646+
length: Int
647+
) -> Data? {
648+
guard let fileSlice = _fileSliceForLinkEditData(
649+
offset: offset,
650+
length: length
651+
) else { return nil }
652+
return try? fileSlice.readAllData()
653+
}
635654
}
636655

637656
extension MachOFile {

0 commit comments

Comments
 (0)