@@ -631,19 +631,30 @@ extension MachOFile {
631
631
offset: Int , // linkedit_data_command->dataoff (linkedit.fileoff + x)
632
632
length: Int
633
633
) -> File . FileSlice ? {
634
+ let text : ( any SegmentCommandProtocol ) ? = loadCommands. text64 ?? loadCommands. text
634
635
let linkedit : ( any SegmentCommandProtocol ) ? = loadCommands. linkedit64 ?? loadCommands. linkedit
635
- guard let linkedit else { return nil }
636
+ guard let text , let linkedit else { return nil }
636
637
guard linkedit. fileOffset + linkedit. fileSize >= offset + length else { return nil }
637
638
639
+ let maxFileOffsetToCheck = text. fileOffset + linkedit. virtualMemoryAddress - text. virtualMemoryAddress
640
+ let isWithinFileRange : Bool = fileHandle. size >= maxFileOffsetToCheck
641
+
642
+ // 1) text.vmaddr < linkedit.vmaddr
643
+ // 2) fileoff_diff <= vmaddr_diff
644
+ // 3) If both exist in the same file
645
+ // text.fileoff < linkedit.fileoff <= text.fileoff + vmaddr_diff
646
+ // 4) if fileHandle.size < text.fileoff + vmaddr_diff
647
+ // both exist in the same file
648
+
638
649
// The linkeditdata in iOS is stored together in a separate, independent cache.
639
650
// (.0x.linkeditdata)
640
- if isLoadedFromDyldCache {
651
+ if isLoadedFromDyldCache && !isWithinFileRange {
641
652
let offset = offset - numericCast( linkedit. fileOffset)
642
653
guard let fullCache = self . fullCache,
643
654
let fileOffset = fullCache. fileOffset (
644
655
of: numericCast ( linkedit. virtualMemoryAddress + offset)
645
656
) ,
646
- let ( _ , segment) = fullCache. urlAndFileSegment (
657
+ let segment = fullCache. fileSegment (
647
658
forOffset: fileOffset
648
659
) else {
649
660
return nil
0 commit comments