Skip to content
This repository was archived by the owner on Dec 28, 2025. It is now read-only.

Commit 04258f1

Browse files
committed
Refactor FileContext
1 parent dbf2cfc commit 04258f1

File tree

14 files changed

+183
-182
lines changed

14 files changed

+183
-182
lines changed

bin/dyldex

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,13 @@ except AttributeError:
1515
exit(1)
1616

1717
from DyldExtractor.extraction_context import ExtractionContext
18-
from DyldExtractor.file_context import FileContext
19-
18+
from DyldExtractor.macho.macho_context import MachOContext
2019
from DyldExtractor.dyld.dyld_context import DyldContext
20+
2121
from DyldExtractor.dyld.dyld_structs import (
2222
dyld_cache_image_info
2323
)
2424

25-
from DyldExtractor.macho.macho_context import MachOContext
26-
2725
from DyldExtractor.converter import (
2826
slide_info,
2927
macho_offset,
@@ -82,8 +80,8 @@ def _getArguments():
8280
def _openSubCaches(
8381
mainCachePath: str,
8482
numSubCaches: int
85-
) -> Tuple[List[FileContext], List[BinaryIO]]:
86-
"""Create FileContext objects for each sub cache.
83+
) -> Tuple[List[DyldContext], List[BinaryIO]]:
84+
"""Create DyldContext objects for each sub cache.
8785
8886
Assumes that each sub cache has the same base name as the
8987
main cache, and that the suffixes are preserved.
@@ -102,7 +100,7 @@ def _openSubCaches(
102100
for cacheSuffix in subCacheSuffixes:
103101
subCachePath = f"{mainCachePath}.{cacheSuffix}"
104102
cacheFileObject = open(subCachePath, mode="rb")
105-
cacheFileCtx = FileContext(cacheFileObject)
103+
cacheFileCtx = DyldContext(cacheFileObject)
106104

107105
subCaches.append(cacheFileCtx)
108106
subCachesFiles.append(cacheFileObject)
@@ -144,7 +142,7 @@ def _extractImage(
144142

145143
# get a a writable copy of the MachOContext
146144
machoOffset, context = dyldCtx.convertAddr(image.address)
147-
machoCtx = MachOContext(context.fileCtx.makeCopy(copyMode=True), machoOffset)
145+
machoCtx = MachOContext(context.fileObject, machoOffset, True)
148146

149147
# Add sub caches if necessary
150148
if dyldCtx.hasSubCaches():
@@ -154,7 +152,7 @@ def _extractImage(
154152
)
155153
machoCtx.addSubfiles(
156154
mainFileMap,
157-
((m, ctx.fileCtx.makeCopy(copyMode=True)) for m, ctx in mappings)
155+
((m, ctx.makeCopy(copyMode=True)) for m, ctx in mappings)
158156
)
159157
pass
160158

@@ -224,13 +222,12 @@ def main():
224222
)
225223

226224
with open(args.dyld_path, "rb") as f:
227-
dyldFileCtx = FileContext(f)
228-
dyldCtx = DyldContext(dyldFileCtx)
225+
dyldCtx = DyldContext(f)
229226

230227
# enumerate images, create a map of paths and images
231228
imageMap = {}
232229
for imageData in dyldCtx.images:
233-
path = dyldCtx.fileCtx.readString(imageData.pathFileOffset)
230+
path = dyldCtx.readString(imageData.pathFileOffset)
234231
path = path[0:-1] # remove null terminator
235232
path = path.decode("utf-8")
236233

bin/dyldex_all

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ from DyldExtractor.converter import (
2323
slide_info,
2424
stub_fixer
2525
)
26+
2627
from DyldExtractor.dyld.dyld_context import DyldContext
2728
from DyldExtractor.extraction_context import ExtractionContext
2829
from DyldExtractor.macho.macho_context import MachOContext
29-
from DyldExtractor.file_context import FileContext
3030

3131
# check dependencies
3232
try:
@@ -91,8 +91,8 @@ def _workerInitializer():
9191
def _openSubCaches(
9292
mainCachePath: str,
9393
numSubCaches: int
94-
) -> Tuple[List[FileContext], List[BinaryIO]]:
95-
"""Create FileContext objects for each sub cache.
94+
) -> Tuple[List[DyldContext], List[BinaryIO]]:
95+
"""Create DyldContext objects for each sub cache.
9696
9797
Assumes that each sub cache has the same base name as the
9898
main cache, and that the suffixes are preserved.
@@ -111,7 +111,7 @@ def _openSubCaches(
111111
for cacheSuffix in subCacheSuffixes:
112112
subCachePath = f"{mainCachePath}.{cacheSuffix}"
113113
cacheFileObject = open(subCachePath, mode="rb")
114-
cacheFileCtx = FileContext(cacheFileObject)
114+
cacheFileCtx = DyldContext(cacheFileObject)
115115

116116
subCaches.append(cacheFileCtx)
117117
subCachesFiles.append(cacheFileObject)
@@ -153,8 +153,7 @@ def _extractImage(
153153
with open(dyldPath, "rb") as f:
154154
subCacheFiles: List[BinaryIO] = []
155155
try:
156-
dyldFileCtx = FileContext(f)
157-
dyldCtx = DyldContext(dyldFileCtx)
156+
dyldCtx = DyldContext(f)
158157

159158
# add sub caches if there are any
160159
if dyldCtx.hasSubCaches():
@@ -168,10 +167,7 @@ def _extractImage(
168167
machoOffset, context = dyldCtx.convertAddr(
169168
dyldCtx.images[imageIndex].address
170169
)
171-
machoCtx = MachOContext(
172-
context.fileCtx.makeCopy(copyMode=True),
173-
machoOffset
174-
)
170+
machoCtx = MachOContext(context.fileObject, machoOffset, True)
175171

176172
# Add sub caches if necessary
177173
if dyldCtx.hasSubCaches():
@@ -181,7 +177,7 @@ def _extractImage(
181177
)
182178
machoCtx.addSubfiles(
183179
mainFileMap,
184-
((m, ctx.fileCtx.makeCopy(copyMode=True)) for m, ctx in mappings)
180+
((m, ctx.makeCopy(copyMode=True)) for m, ctx in mappings)
185181
)
186182
pass
187183

@@ -256,11 +252,10 @@ def _main() -> None:
256252
# create a list of image paths
257253
imagePaths: List[str] = []
258254
with open(args.dyld_path, "rb") as f:
259-
dyldFileCtx = FileContext(f)
260-
dyldCtx = DyldContext(dyldFileCtx)
255+
dyldCtx = DyldContext(f)
261256

262257
for image in dyldCtx.images:
263-
imagePath = dyldCtx.fileCtx.readString(
258+
imagePath = dyldCtx.readString(
264259
image.pathFileOffset
265260
)[0:-1].decode("utf-8")
266261
imagePaths.append(imagePath)

src/DyldExtractor/converter/linkedit_optimizer.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import struct
22
from typing import Union, Type, Dict
33

4+
from DyldExtractor.dyld.dyld_context import DyldContext
45
from DyldExtractor.extraction_context import ExtractionContext
56

67
from DyldExtractor.dyld.dyld_structs import (
78
dyld_cache_local_symbols_info,
89
dyld_cache_local_symbols_entry,
910
dyld_cache_local_symbols_entry2
1011
)
11-
from DyldExtractor.file_context import FileContext
1212

1313
from DyldExtractor.macho.macho_constants import *
1414
from DyldExtractor.macho.macho_structs import (
@@ -79,7 +79,7 @@ def __init__(self, extractionCtx: ExtractionContext) -> None:
7979
self.statusBar = extractionCtx.statusBar
8080
self.logger = extractionCtx.logger
8181

82-
self.linkeditFile = self.machoCtx.fileForAddr(
82+
self.linkeditFile = self.machoCtx.ctxForAddr(
8383
self.machoCtx.segments[b"__LINKEDIT"].seg.vmaddr
8484
)
8585

@@ -216,7 +216,7 @@ def startSymbolContext(self, newLinkedit: bytearray) -> None:
216216

217217
def getLocalSymsEntryStruct(
218218
self,
219-
symbolsCache: FileContext,
219+
symbolsCache: DyldContext,
220220
symbolsInfo: dyld_cache_local_symbols_info
221221
) -> Union[
222222
Type[dyld_cache_local_symbols_entry],
@@ -252,12 +252,12 @@ def copyLocalSymbols(self, newLinkedit: bytearray) -> None:
252252

253253
symbolsCache = self.dyldCtx.getSymbolsCache()
254254
localSymbolsInfo = dyld_cache_local_symbols_info(
255-
symbolsCache.fileCtx.file,
255+
symbolsCache.file,
256256
symbolsCache.header.localSymbolsOffset
257257
)
258258

259259
entryStruct = self.getLocalSymsEntryStruct(
260-
symbolsCache.fileCtx,
260+
symbolsCache,
261261
localSymbolsInfo
262262
)
263263
if not entryStruct:
@@ -274,7 +274,7 @@ def copyLocalSymbols(self, newLinkedit: bytearray) -> None:
274274
entryOff = (i * entryStruct.SIZE)
275275
entryOff += localSymbolsInfo._fileOff_ + localSymbolsInfo.entriesOffset
276276

277-
entry = entryStruct(symbolsCache.fileCtx.file, entryOff)
277+
entry = entryStruct(symbolsCache.file, entryOff)
278278
if entry.dylibOffset == dylibOffset:
279279
localSymbolsEntriesInfo = entry
280280
break
@@ -300,8 +300,8 @@ def copyLocalSymbols(self, newLinkedit: bytearray) -> None:
300300
symbolStrOff = localSymbolsInfo._fileOff_ + localSymbolsInfo.stringsOffset
301301

302302
for offset in range(entriesStart, entriesEnd, nlist_64.SIZE):
303-
symbolEnt = nlist_64(symbolsCache.fileCtx.file, offset)
304-
name = symbolsCache.fileCtx.readString(symbolStrOff + symbolEnt.n_strx)
303+
symbolEnt = nlist_64(symbolsCache.file, offset)
304+
name = symbolsCache.readString(symbolStrOff + symbolEnt.n_strx)
305305

306306
# copy data
307307
self.newLocalSymbolCount += 1
@@ -618,7 +618,7 @@ def optimizeLinkedit(extractionCtx: ExtractionContext) -> None:
618618

619619
# Set the new linkedit in the same location
620620
newLinkeditOff = extractionCtx.machoCtx.segments[b"__LINKEDIT"].seg.fileoff
621-
linkeditFile = extractionCtx.machoCtx.fileForAddr(
621+
linkeditFile = extractionCtx.machoCtx.ctxForAddr(
622622
extractionCtx.machoCtx.segments[b"__LINKEDIT"].seg.vmaddr
623623
)
624624
linkeditFile.writeBytes(newLinkeditOff, newLinkedit)

src/DyldExtractor/converter/macho_offset.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
from typing import List
33

44
from DyldExtractor.extraction_context import ExtractionContext
5+
from DyldExtractor.macho.macho_context import MachOContext
56
from DyldExtractor.file_context import FileContext
67

7-
from DyldExtractor.macho.macho_context import MachOContext
88
from DyldExtractor.macho.macho_structs import (
99
linkedit_data_command,
1010
dyld_info_command,
@@ -116,7 +116,7 @@ def optimizeOffsets(extractionCtx: ExtractionContext) -> List[WriteProcedure]:
116116
segment.seg.fileoff + shiftDelta,
117117
dyldCtx.convertAddr(segment.seg.vmaddr)[0],
118118
segment.seg.filesize,
119-
machoCtx.fileForAddr(segment.seg.vmaddr)
119+
machoCtx.ctxForAddr(segment.seg.vmaddr)
120120
)
121121
pass
122122
writeProcedures.append(procedure)

0 commit comments

Comments
 (0)