Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-5349: Improvements to the DyldCacheFileSystem

Ryan Kurtz3cda1198895e
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/FileByteProvider.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/FileByteProvider.java
index aaa145c0a8..e50ac871c2 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/FileByteProvider.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/FileByteProvider.java
@@ -120,7 +120,8 @@ public class FileByteProvider implements MutableByteProvider {
public byte[] readBytes(long index, long length) throws IOException {
ensureBounds(index, length);
if (length > Integer.MAX_VALUE) {
- throw new IllegalArgumentException();
+ throw new IllegalArgumentException("Read length 0x%x exceeds Integer.MAX_VALUE (0x%x)"
+ .formatted(length, Integer.MAX_VALUE));
}
int len = (int) length;
byte[] result = new byte[len];
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/dyld/DyldCacheMappingAndSlideInfo.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/dyld/DyldCacheMappingAndSlideInfo.java
index 6dbf7ef267..a6af0f5e69 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/dyld/DyldCacheMappingAndSlideInfo.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/dyld/DyldCacheMappingAndSlideInfo.java
@@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -37,6 +37,8 @@ public class DyldCacheMappingAndSlideInfo implements StructConverter {
public static long DYLD_CACHE_MAPPING_CONST_DATA = 0x4;
public static long DYLD_CACHE_MAPPING_TEXT_STUBS = 0x8;
public static long DYLD_CACHE_DYNAMIC_CONFIG_DATA = 0x10;
+ public static long DYLD_CACHE_READ_ONLY_DATA = 0x20;
+ public static long DYLD_CACHE_MAPPING_CONST_TPRO_DATA = 0x40;
private long address;
private long size;
@@ -146,6 +148,14 @@ public class DyldCacheMappingAndSlideInfo implements StructConverter {
return (flags & DYLD_CACHE_DYNAMIC_CONFIG_DATA) != 0;
}
+ public boolean isReadOnlyData() {
+ return (flags & DYLD_CACHE_READ_ONLY_DATA) != 0;
+ }
+
+ public boolean isConstTproData() {
+ return (flags & DYLD_CACHE_MAPPING_CONST_TPRO_DATA) != 0;
+ }
+
/**
* Returns true if the initial protections include READ.
*
diff --git a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/ios/dyldcache/DyldCacheEntry.java b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/ios/dyldcache/DyldCacheEntry.java
new file mode 100644
index 0000000000..99d94cbcc5
--- /dev/null
+++ b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/ios/dyldcache/DyldCacheEntry.java
@@ -0,0 +1,35 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package ghidra.file.formats.ios.dyldcache;
+
+import com.google.common.collect.RangeSet;
+
+import ghidra.app.util.bin.format.macho.dyld.DyldCacheMappingAndSlideInfo;
+import ghidra.app.util.opinion.DyldCacheUtils.SplitDyldCache;
+
+/**
+ * An entry in the {@link DyldCacheFileSystem}
+ *
+ * @param path The path of the entry
+ * @param splitCacheIndex The entry's {@link SplitDyldCache} index
+ * @param rangeSet The entry's address ranges
+ * @param mappingInfo The entry's {@link DyldCacheMappingAndSlideInfo}; could be null if this entry
+ * represents a DYLIB
+ * @param mappingIndex The entry's {@link DyldCacheMappingAndSlideInfo} index; ignored if the
+ * {@code mappingInfo} is null.
+ */
+public record DyldCacheEntry(String path, int splitCacheIndex, RangeSet<Long> rangeSet,
+ DyldCacheMappingAndSlideInfo mappingInfo, int mappingIndex) {}
diff --git a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/ios/dyldcache/DyldCacheExtractor.java b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/ios/dyldcache/DyldCacheExtractor.java
index 531d1f372d..289fa135ed 100644
--- a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/ios/dyldcache/DyldCacheExtractor.java
+++ b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/ios/dyldcache/DyldCacheExtractor.java
@@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -20,7 +20,6 @@ import java.nio.charset.StandardCharsets;
import java.util.*;
import com.google.common.collect.Range;
-import com.google.common.collect.RangeSet;
import ghidra.app.util.bin.*;
import ghidra.app.util.bin.format.macho.*;
@@ -47,20 +46,11 @@ public class DyldCacheExtractor {
public static final byte[] FOOTER_V1 =
"Ghidra DYLD extraction v1".getBytes(StandardCharsets.US_ASCII);
- /**
- * A {@link DyldCacheMappingAndSlideInfo} with a possibly reduced set of available addresses
- * within the mapping
- *
- * @param mappingInfo A {@link DyldCacheMappingAndSlideInfo}
- * @param rangeSet A a possibly reduced set of available addresses within the mapping
- */
- public record MappingRange(DyldCacheMappingAndSlideInfo mappingInfo, RangeSet<Long> rangeSet) {}
-
/**
* Gets a {@link ByteProvider} that contains a DYLIB from a {@link DyldCacheFileSystem}. The
* DYLIB's header will be altered to account for its segment bytes being packed down.
*
- * @param dylibOffset The offset of the DYLIB in the given provider
+ * @param entry The mapping's {@link DyldCacheEntry}
* @param splitDyldCache The {@link SplitDyldCache}
* @param index The DYLIB's {@link SplitDyldCache} index
* @param slideFixupMap A {@link Map} of {@link DyldFixup}s to perform
@@ -71,10 +61,11 @@ public class DyldCacheExtractor {
* @throws IOException If there was an IO-related issue with extracting the DYLIB
* @throws CancelledException If the user cancelled the operation
*/
- public static ByteProvider extractDylib(long dylibOffset, SplitDyldCache splitDyldCache,
+ public static ByteProvider extractDylib(DyldCacheEntry entry, SplitDyldCache splitDyldCache,
int index, Map<DyldCacheSlideInfoCommon, List<DyldFixup>> slideFixupMap,
FSRL fsrl, TaskMonitor monitor) throws IOException, MachException, CancelledException {
-
+ long dylibOffset = entry.rangeSet().asRanges().iterator().next().lowerEndpoint() -
+ splitDyldCache.getDyldCacheHeader(entry.splitCacheIndex()).getBaseAddress();
ExtractedMacho extractedMacho = new DyldPackedSegments(dylibOffset, splitDyldCache, index,
FOOTER_V1, slideFixupMap, monitor);
extractedMacho.pack();
@@ -84,7 +75,7 @@ public class DyldCacheExtractor {
/**
* Gets a {@link ByteProvider} that contains a byte mapping from a {@link DyldCacheFileSystem}
*
- * @param mappingRange The {@link MappingRange}
+ * @param entry The mapping's {@link DyldCacheEntry}
* @param segmentName The name of the segment in the resulting Mach-O
* @param splitDyldCache The {@link SplitDyldCache}
* @param index The mapping's {@link SplitDyldCache} index
@@ -96,14 +87,14 @@ public class DyldCacheExtractor {
* @throws IOException If there was an IO-related issue with extracting the mapping
* @throws CancelledException If the user cancelled the operation
*/
- public static ByteProvider extractMapping(MappingRange mappingRange, String segmentName,
+ public static ByteProvider extractMapping(DyldCacheEntry entry, String segmentName,
SplitDyldCache splitDyldCache, int index,
Map<DyldCacheSlideInfoCommon, List<DyldFixup>> slideFixupMap, FSRL fsrl,
TaskMonitor monitor) throws IOException, MachException, CancelledException {
int magic = MachConstants.MH_MAGIC_64;
- List<Range<Long>> ranges = new ArrayList<>(mappingRange.rangeSet().asRanges());
- DyldCacheMappingAndSlideInfo mappingInfo = mappingRange.mappingInfo();
+ List<Range<Long>> ranges = new ArrayList<>(entry.rangeSet().asRanges());
+ DyldCacheMappingAndSlideInfo mappingInfo = entry.mappingInfo();
int allSegmentsSize = SegmentCommand.size(magic) * ranges.size();
// Fix slide pointers
diff --git a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/ios/dyldcache/DyldCacheFileSystem.java b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/ios/dyldcache/DyldCacheFileSystem.java
index e05928b31c..620c132ddb 100644
--- a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/ios/dyldcache/DyldCacheFileSystem.java
+++ b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/ios/dyldcache/DyldCacheFileSystem.java
@@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -15,81 +15,127 @@
*/
package ghidra.file.formats.ios.dyldcache;
-import java.io.IOException;
-import java.util.*;
+import static ghidra.formats.gfilesystem.fileinfo.FileAttributeType.*;
-import org.apache.commons.io.FilenameUtils;
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
import com.google.common.collect.*;
-import ghidra.app.util.bin.BinaryReader;
import ghidra.app.util.bin.ByteProvider;
import ghidra.app.util.bin.format.macho.MachException;
import ghidra.app.util.bin.format.macho.MachHeader;
import ghidra.app.util.bin.format.macho.commands.SegmentCommand;
import ghidra.app.util.bin.format.macho.dyld.*;
import ghidra.app.util.importer.MessageLog;
-import ghidra.app.util.opinion.DyldCacheUtils;
import ghidra.app.util.opinion.DyldCacheUtils.SplitDyldCache;
-import ghidra.file.formats.ios.dyldcache.DyldCacheExtractor.MappingRange;
import ghidra.formats.gfilesystem.*;
import ghidra.formats.gfilesystem.annotations.FileSystemInfo;
-import ghidra.formats.gfilesystem.factory.GFileSystemBaseFactory;
+import ghidra.formats.gfilesystem.fileinfo.FileAttributes;
import ghidra.util.exception.CancelledException;
-import ghidra.util.exception.CryptoException;
import ghidra.util.task.TaskMonitor;
+import util.CollectionUtils;
/**
* A {@link GFileSystem} implementation for the components of a DYLD Cache
*/
-@FileSystemInfo(type = DyldCacheFileSystem.DYLD_CACHE_FSTYPE, description = "iOS DYLD Cache Version 1", factory = GFileSystemBaseFactory.class)
-public class DyldCacheFileSystem extends GFileSystemBase {
+@FileSystemInfo(
+ type = DyldCacheFileSystem.DYLD_CACHE_FSTYPE,
+ description = "iOS DYLD Cache Version 1",
+ factory = DyldCacheFileSystemFactory.class
+)
+public class DyldCacheFileSystem extends AbstractFileSystem<DyldCacheEntry> {
public static final String DYLD_CACHE_FSTYPE = "dyldcachev1";
+ private ByteProvider provider;
private SplitDyldCache splitDyldCache;
private boolean parsedLocalSymbols = false;
private Map<DyldCacheSlideInfoCommon, List<DyldFixup>> slideFixupMap;
- private Map<GFile, Long> addrMap = new HashMap<>();
- private Map<GFile, Integer> indexMap = new HashMap<>();
- private Map<Long, MappingRange> stubMap = new HashMap<>();
- private Map<Long, MappingRange> dyldDataMap = new HashMap<>();
+ private RangeMap<Long, DyldCacheEntry> rangeMap = TreeRangeMap.create();
/**
* Creates a new {@link DyldCacheFileSystem}
*
- * @param fileSystemName The name of the file system
+ * @param fsFSRL {@link FSRLRoot} of this file system
* @param provider The {@link ByteProvider} that contains the file system
*/
- public DyldCacheFileSystem(String fileSystemName, ByteProvider provider) {
- super(fileSystemName, provider);
+ public DyldCacheFileSystem(FSRLRoot fsFSRL, ByteProvider provider) {
+ super(fsFSRL, FileSystemService.getInstance());
+ this.provider = provider;
}
- @Override
- public void close() throws IOException {
- slideFixupMap = null;
- parsedLocalSymbols = false;
- addrMap.clear();
- indexMap.clear();
- stubMap.clear();
- dyldDataMap.clear();
- if (splitDyldCache != null) {
- splitDyldCache.close();
- splitDyldCache = null;
+ /**
+ * Mounts this file system
+ *
+ * @param monitor {@link TaskMonitor}
+ * @throws IOException If there was an issue mounting the file system
+ * @throws MachException if there was an error parsing a DYLIB header
+ * @throws CancelledException If the user cancelled the operation
+ */
+ public void mount(TaskMonitor monitor) throws IOException, MachException, CancelledException {
+ splitDyldCache = new SplitDyldCache(provider, false, new MessageLog(), monitor);
+ RangeSet<Long> allDylibRanges = TreeRangeSet.create();
+
+ // Find the DYLIB's and add them as files
+ monitor.initialize(splitDyldCache.size(), "Find DYLD DYLIBs...");
+ for (int i = 0; i < splitDyldCache.size(); i++) {
+ monitor.increment();
+ DyldCacheHeader header = splitDyldCache.getDyldCacheHeader(i);
+ ByteProvider p = splitDyldCache.getProvider(i);
+ for (DyldCacheImage mappedImage : header.getMappedImages()) {
+ MachHeader machHeader =
+ new MachHeader(p, mappedImage.getAddress() - header.getBaseAddress());
+ RangeSet<Long> rangeSet = TreeRangeSet.create();
+ for (SegmentCommand segment : machHeader.parseSegments()) {
+ Range<Long> range = Range.openClosed(segment.getVMaddress(),
+ segment.getVMaddress() + segment.getVMsize());
+ rangeSet.add(range);
+ }
+ DyldCacheEntry entry =
+ new DyldCacheEntry(mappedImage.getPath(), i, rangeSet, null, -1);
+ rangeSet.asRanges().forEach(r -> rangeMap.put(r, entry));
+ allDylibRanges.addAll(rangeSet);
+ fsIndex.storeFile(mappedImage.getPath(), fsIndex.getFileCount(), false, -1,
+ entry);
+ }
+ }
+
+ // Find and store all the mappings for all of the subcaches. We need to remove the DYLIB's
+ // that we just found so we don't account for any bytes more than once. This will result
+ // in the mappings being broken up into a lot of small chunks, each being its own file.
+ monitor.initialize(splitDyldCache.size(), "Find DYLD mapping ranges...");
+ for (int i = 0; i < splitDyldCache.size(); i++) {
+ monitor.increment();
+ DyldCacheHeader header = splitDyldCache.getDyldCacheHeader(i);
+ String name = splitDyldCache.getName(i);
+ List<DyldCacheMappingAndSlideInfo> mappingInfos = header.getCacheMappingAndSlideInfos();
+ for (int j = 0; j < mappingInfos.size(); j++) {
+ DyldCacheMappingAndSlideInfo mappingInfo = mappingInfos.get(j);
+ Range<Long> mappingRange = Range.openClosed(mappingInfo.getAddress(),
+ mappingInfo.getAddress() + mappingInfo.getSize());
+ RangeSet<Long> reducedRangeSet = TreeRangeSet.create();
+ reducedRangeSet.add(mappingRange);
+ reducedRangeSet.removeAll(allDylibRanges);
+ for (Range<Long> range : reducedRangeSet.asRanges()) {
+ String path = getComponentPath(name, mappingInfo, j, range);
+ DyldCacheEntry entry = new DyldCacheEntry(path, i,
+ TreeRangeSet.create(CollectionUtils.asIterable(range)), mappingInfo, j);
+ rangeMap.put(range, entry);
+ fsIndex.storeFile(path, fsIndex.getFileCount(), false, -1, entry);
+ }
+ }
}
- super.close();
}
@Override
public ByteProvider getByteProvider(GFile file, TaskMonitor monitor)
throws CancelledException, IOException {
- Long addr = addrMap.get(file);
- if (addr == null) {
+ DyldCacheEntry entry = fsIndex.getMetadata(file);
+ if (entry == null) {
return null;
}
- int index = indexMap.get(file);
- long machHeaderStartIndexInProvider =
- addr - splitDyldCache.getDyldCacheHeader(index).getBaseAddress();
if (slideFixupMap == null) {
slideFixupMap = DyldCacheExtractor.getSlideFixups(splitDyldCache, monitor);
@@ -104,232 +150,109 @@ public class DyldCacheFileSystem extends GFileSystemBase {
}
try {
- if (stubMap.containsKey(addr)) {
- MappingRange mappingRange = stubMap.get(addr);
- return DyldCacheExtractor.extractMapping(mappingRange, "_STUBS", splitDyldCache,
- index, slideFixupMap, file.getFSRL(), monitor);
+ if (entry.mappingInfo() != null) {
+ return DyldCacheExtractor.extractMapping(entry,
+ getComponentName(entry.mappingInfo()), splitDyldCache,
+ entry.splitCacheIndex(), slideFixupMap, file.getFSRL(), monitor);
}
- if (dyldDataMap.containsKey(addr)) {
- MappingRange mappingRange = dyldDataMap.get(addr);
- return DyldCacheExtractor.extractMapping(mappingRange, "__DATA", splitDyldCache,
- index, slideFixupMap, file.getFSRL(), monitor);
- }
- return DyldCacheExtractor.extractDylib(machHeaderStartIndexInProvider,
- splitDyldCache, index, slideFixupMap, file.getFSRL(), monitor);
+ return DyldCacheExtractor.extractDylib(entry, splitDyldCache, entry.splitCacheIndex(),
+ slideFixupMap, file.getFSRL(), monitor);
}
catch (MachException e) {
- throw new IOException("Invalid Mach-O header detected at 0x" +
- Long.toHexString(machHeaderStartIndexInProvider));
+ throw new IOException("Invalid Mach-O header detected at: " + entry);
}
}
- @Override
- public List<GFile> getListing(GFile directory) throws IOException {
- if (directory == null || directory.equals(root)) {
- List<GFile> roots = new ArrayList<>();
- for (GFile file : addrMap.keySet()) {
- if (file.getParentFile() == root || file.getParentFile().equals(root)) {
- roots.add(file);
- }
- }
- return roots;
- }
- List<GFile> tmp = new ArrayList<>();
- for (GFile file : addrMap.keySet()) {
- if (file.getParentFile() == null) {
- continue;
- }
- if (file.getParentFile().equals(directory)) {
- tmp.add(file);
- }
- }
- return tmp;
+ /**
+ * Attempts to find the given address in the DYLD Cache
+ *
+ * @param addr The address to find
+ * @return The path of the file within the {@link DyldCacheFileSystem} that contains the given
+ * address, or null if the address was not found
+ * @throws IOException if an IO-related error occurred
+ */
+ public String findAddress(long addr) throws IOException {
+ DyldCacheEntry entry = rangeMap.get(addr);
+ return entry != null ? entry.path() : null;
}
@Override
- public boolean isValid(TaskMonitor monitor) throws IOException {
- if (!DyldCacheUtils.isDyldCache(provider)) {
- return false;
- }
- try {
- DyldCacheHeader header = new DyldCacheHeader(new BinaryReader(provider, true));
- return !header.isSubcache();
- }
- catch (IOException e) {
- return false;
+ public FileAttributes getFileAttributes(GFile file, TaskMonitor monitor) {
+ FileAttributes result = new FileAttributes();
+ DyldCacheEntry entry = fsIndex.getMetadata(file);
+ if (entry != null) {
+ result.add(NAME_ATTR, entry.path());
+ result.add(PATH_ATTR, entry.path());
+ result.add("Cache Index", entry.splitCacheIndex());
+ result.add("Address Range", entry.rangeSet().toString()); // TODO: display as hex
}
+ return result;
}
@Override
- public void open(TaskMonitor monitor) throws IOException, CryptoException, CancelledException {
- splitDyldCache = new SplitDyldCache(provider, false, new MessageLog(), monitor);
- Map<Integer, List<MappingRange>> dyldDataMappingRanges = new HashMap<>();
-
- // Find and store all the mappings for the DYLD data subcaches.
- // As we find other components that overlap, these mapping ranges will be reduced so there
- // is no overlap
- monitor.initialize(splitDyldCache.size(), "Find DYLD data...");
- for (int i = 0; i < splitDyldCache.size(); i++) {
- monitor.increment();
- String name = splitDyldCache.getName(i);
- DyldCacheHeader header = splitDyldCache.getDyldCacheHeader(i);
- List<DyldCacheMappingAndSlideInfo> mappingInfos = header.getCacheMappingAndSlideInfos();
- List<MappingRange> mappingRangeList = new ArrayList<>();
- if (name.endsWith(".dylddata")) {
- dyldDataMappingRanges.put(i, mappingRangeList);
- for (int j = 0; j < mappingInfos.size(); j++) {
- DyldCacheMappingAndSlideInfo mappingInfo = mappingInfos.get(j);
- mappingRangeList.add(new MappingRange(mappingInfo, getRangeSet(mappingInfo)));
- }
- }
- }
-
- // Find DYLIB and STUBS components. Remove DYLIB segment ranges from the DYLD data mappings
- // so we can later add the DYLD data with no overlap.
- // NOTE: The STUBS will never overlap with DYLD data so there is no need to remove STUB
- // segment ranges.
- monitor.initialize(splitDyldCache.size(), "Find DYLD components...");
- for (int i = 0; i < splitDyldCache.size(); i++) {
- monitor.increment();
- String name = splitDyldCache.getName(i);
- DyldCacheHeader header = splitDyldCache.getDyldCacheHeader(i);
- List<DyldCacheMappingAndSlideInfo> mappingInfos = header.getCacheMappingAndSlideInfos();
+ public boolean isClosed() {
+ return provider == null;
+ }
- // DYLIBs
- List<DyldCacheImage> mappedImages = header.getMappedImages();
- for (DyldCacheImage mappedImage : mappedImages) {
- GFileImpl file =
- GFileImpl.fromPathString(this, root, mappedImage.getPath(), null, false, -1);
- storeFile(file, mappedImage.getAddress(), i);
- reduceOverlappingSegments(i, mappedImage, dyldDataMappingRanges.values());
- }
+ @Override
+ public void close() throws IOException {
+ refManager.onClose();
+ fsIndex.clear();
- // STUBS
- for (DyldCacheMappingAndSlideInfo mappingInfo : mappingInfos) {
- if (mappingInfo.isTextStubs()) {
- GFileImpl file =
- GFileImpl.fromPathString(this, root, getStubPath(name), null, false, -1);
- storeFile(file, mappingInfo.getAddress(), i);
- stubMap.put(mappingInfo.getAddress(),
- new MappingRange(mappingInfo, getRangeSet(mappingInfo)));
- break; // assuming just 1 stub block
- }
- }
+ if (splitDyldCache != null) {
+ splitDyldCache.close();
+ splitDyldCache = null;
}
-
- // Add DYLD data components with reduced mapping ranges
- for (Integer i : dyldDataMappingRanges.keySet()) {
- String name = splitDyldCache.getName(i);
- List<MappingRange> mappingRangeList = dyldDataMappingRanges.get(i);
- for (int j = 0; j < mappingRangeList.size(); j++) {
- monitor.checkCancelled();
- MappingRange mappingRange = mappingRangeList.get(j);
- DyldCacheMappingAndSlideInfo mappingInfo = mappingRange.mappingInfo();
- GFileImpl file =
- GFileImpl.fromPathString(this, root, getDyldDataPath(name, j), null, false, -1);
- storeFile(file, mappingInfo.getAddress(), i);
- dyldDataMap.put(mappingInfo.getAddress(), mappingRange);
- }
+ if (provider != null) {
+ provider.close();
+ provider = null;
}
- }
-
- /**
- * Gets the open {@link SplitDyldCache}
- *
- * @return The opened {@link SplitDyldCache}, or null if it has is not open
- */
- public SplitDyldCache getSplitDyldCache() {
- return splitDyldCache;
- }
-
- /**
- * Gets the text stub path for the given DYLD Cache name
- *
- * @param dyldCacheName The name of the DYLD Cache
- * @return The text stub path for the given DYLD Cache name
- */
- public static String getStubPath(String dyldCacheName) {
- return "/STUBS/STUBS." + FilenameUtils.getExtension(dyldCacheName);
- }
- /**
- * Gets the DYLD data path for the given DYLD Cache name
- *
- * @param dyldCacheName The name of the DYLD Cache
- * @param mappingIndex The mapping index
- * @return The DYLD data path for the given DYLD Cache name
- */
- public static String getDyldDataPath(String dyldCacheName, int mappingIndex) {
- return "/DYLD_DATA/DYLD_DATA.%s.%d".formatted(
- FilenameUtils.getExtension(
- dyldCacheName.substring(0, dyldCacheName.length() - ".dylddata".length())),
- mappingIndex);
+ slideFixupMap = null;
+ parsedLocalSymbols = false;
+ rangeMap.clear();
}
- /**
- * "Stores" the given {@link GFile file} and it's parent hierarchy in lookup maps for future
- * access
- *
- * @param file The {@link GFile file} to store
- * @param address The address that corresponds to the {@link GFile file}
- * @param splitDyldCacheIndex The {@link SplitDyldCache} index that corresponds to the given
- * {@link GFile file}
- */
- private void storeFile(GFile file, Long address, Integer splitDyldCacheIndex) {
- if (file == null) {
- return;
+ private String getComponentName(DyldCacheMappingAndSlideInfo mappingInfo) {
+ String name;
+ if (mappingInfo.isDirtyData()) {
+ name = "DATA_DIRTY";
}
- if (file.equals(root)) {
- return;
+ else if (mappingInfo.isConstData()) {
+ name = mappingInfo.isAuthData() ? "AUTH_CONST" : "DATA_CONST";
}
- if (!addrMap.containsKey(file) || addrMap.get(file) == null) {
- addrMap.put(file, address);
- indexMap.put(file, splitDyldCacheIndex);
+ else if (mappingInfo.isTextStubs()) {
+ name = "TEXT_STUBS";
}
- GFile parentFile = file.getParentFile();
- storeFile(parentFile, null, null);
- }
-
- /**
- * Gets the default range of the given {@link DyldCacheMappingAndSlideInfo}
- *
- * @param mappingInfo The {@link DyldCacheMappingAndSlideInfo} to the the range of
- * @return The default range of the given {@link DyldCacheMappingAndSlideInfo}
- */
- private RangeSet<Long> getRangeSet(DyldCacheMappingAndSlideInfo mappingInfo) {
- RangeSet<Long> rangeSet = TreeRangeSet.create();
- rangeSet.add(Range.openClosed(mappingInfo.getAddress(),
- mappingInfo.getAddress() + mappingInfo.getSize()));
- return rangeSet;
+ else if (mappingInfo.isConfigData()) {
+ name = "DATA_CONFIG";
+ }
+ else if (mappingInfo.isAuthData()) {
+ name = "AUTH";
+ }
+ else if (mappingInfo.isReadOnlyData()) {
+ name = "DATA_RO";
+ }
+ else if (mappingInfo.isConstTproData()) {
+ name = "DATA_CONST_TPRO";
+ }
+ else {
+ name = "DYLD";
+ }
+ return name;
}
/**
- * Reduces the given ranges so they do not overlap with the segments founded in the given image
+ * Gets the DYLD component path of the given DYLD component
*
- * @param splitDyldCacheIndex The {@link SplitDyldCache}
- * @param mappedImage The {@link DyldCacheImage} that may overlap the given ranges
- * @param ranges The {@link MappingRange ranges} to reduce
- * @throws IOException if an exception occurred while parsing the image's Mach-O header
+ * @param dyldCacheName The name of the DYLD Cache
+ * @param mappingInfo the mapping info
+ * @param mappingIndex The mapping index
+ * @return The DYLD component path of the given DYLD component
*/
- private void reduceOverlappingSegments(int splitDyldCacheIndex, DyldCacheImage mappedImage,
- Collection<List<MappingRange>> ranges) throws IOException {
- DyldCacheHeader dyldCacheHeader = splitDyldCache.getDyldCacheHeader(splitDyldCacheIndex);
- ByteProvider p = splitDyldCache.getProvider(splitDyldCacheIndex);
- try {
- MachHeader machoHeader =
- new MachHeader(p, mappedImage.getAddress() - dyldCacheHeader.getBaseAddress());
- for (SegmentCommand segment : machoHeader.parseSegments()) {
- for (List<MappingRange> mappingRanges : ranges) {
- for (MappingRange mappingRange : mappingRanges) {
- Range<Long> range = Range.closedOpen(segment.getVMaddress(),
- segment.getVMaddress() + segment.getVMsize());
- mappingRange.rangeSet().remove(range);
- }
- }
- }
- }
- catch (MachException e) {
- throw new IOException(e);
- }
+ private String getComponentPath(String dyldCacheName, DyldCacheMappingAndSlideInfo mappingInfo,
+ int mappingIndex, Range<Long> range) {
+ return "/DYLD/%s/%s.%d.0x%x-0x%x".formatted(dyldCacheName, getComponentName(mappingInfo),
+ mappingIndex, range.lowerEndpoint(), range.upperEndpoint());
}
}
diff --git a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/ios/dyldcache/DyldCacheFileSystemFactory.java b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/ios/dyldcache/DyldCacheFileSystemFactory.java
new file mode 100644
index 0000000000..ef9d48696b
--- /dev/null
+++ b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/ios/dyldcache/DyldCacheFileSystemFactory.java
@@ -0,0 +1,66 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package ghidra.file.formats.ios.dyldcache;
+
+import java.io.IOException;
+
+import ghidra.app.util.bin.BinaryReader;
+import ghidra.app.util.bin.ByteProvider;
+import ghidra.app.util.bin.format.macho.MachException;
+import ghidra.app.util.bin.format.macho.dyld.DyldCacheHeader;
+import ghidra.app.util.opinion.DyldCacheUtils;
+import ghidra.formats.gfilesystem.FSRLRoot;
+import ghidra.formats.gfilesystem.FileSystemService;
+import ghidra.formats.gfilesystem.factory.GFileSystemFactoryByteProvider;
+import ghidra.formats.gfilesystem.factory.GFileSystemProbeByteProvider;
+import ghidra.util.exception.CancelledException;
+import ghidra.util.task.TaskMonitor;
+
+/**
+ * Factory to identify and create instances of a {@link DyldCacheFileSystem}
+ */
+public class DyldCacheFileSystemFactory implements
+ GFileSystemFactoryByteProvider<DyldCacheFileSystem>, GFileSystemProbeByteProvider {
+
+ @Override
+ public DyldCacheFileSystem create(FSRLRoot targetFSRL, ByteProvider byteProvider,
+ FileSystemService fsService, TaskMonitor monitor)
+ throws IOException, CancelledException {
+ DyldCacheFileSystem fs = new DyldCacheFileSystem(targetFSRL, byteProvider);
+ try {
+ fs.mount(monitor);
+ return fs;
+ }
+ catch (MachException e) {
+ throw new IOException(e);
+ }
+ }
+
+ @Override
+ public boolean probe(ByteProvider byteProvider, FileSystemService fsService,
+ TaskMonitor monitor) throws IOException, CancelledException {
+ if (!DyldCacheUtils.isDyldCache(byteProvider)) {
+ return false;
+ }
+ try {
+ DyldCacheHeader header = new DyldCacheHeader(new BinaryReader(byteProvider, true));
+ return !header.isSubcache();
+ }
+ catch (IOException e) {
+ return false;
+ }
+ }
+}
diff --git a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/ios/fileset/MachoFileSetFileSystem.java b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/ios/fileset/MachoFileSetFileSystem.java
index b33ea69895..cfca6975c2 100644
--- a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/ios/fileset/MachoFileSetFileSystem.java
+++ b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/ios/fileset/MachoFileSetFileSystem.java
@@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -36,7 +36,11 @@ import ghidra.util.task.TaskMonitor;
/**
* A {@link GFileSystem} implementation for Mach-O file set entries
*/
-@FileSystemInfo(type = MachoFileSetFileSystem.MACHO_FILESET_FSTYPE, description = "Mach-O file set", factory = MachoFileSetFileSystemFactory.class)
+@FileSystemInfo(
+ type = MachoFileSetFileSystem.MACHO_FILESET_FSTYPE,
+ description = "Mach-O file set",
+ factory = MachoFileSetFileSystemFactory.class
+)
public class MachoFileSetFileSystem extends AbstractFileSystem<MachoFileSetEntry> {
public static final String MACHO_FILESET_FSTYPE = "machofileset";
diff --git a/Ghidra/Features/FileFormats/src/main/java/ghidra/macosx/plugins/DyldCacheBuilderPlugin.java b/Ghidra/Features/FileFormats/src/main/java/ghidra/macosx/plugins/DyldCacheBuilderPlugin.java
index 3fd47d0859..e3d57fe541 100644
--- a/Ghidra/Features/FileFormats/src/main/java/ghidra/macosx/plugins/DyldCacheBuilderPlugin.java
+++ b/Ghidra/Features/FileFormats/src/main/java/ghidra/macosx/plugins/DyldCacheBuilderPlugin.java
@@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -16,19 +16,12 @@
package ghidra.macosx.plugins;
import java.io.IOException;
-import java.util.List;
import docking.action.builder.ActionBuilder;
import ghidra.app.CorePluginPackage;
import ghidra.app.context.ProgramLocationActionContext;
import ghidra.app.plugin.PluginCategoryNames;
-import ghidra.app.util.bin.ByteProvider;
-import ghidra.app.util.bin.format.macho.MachException;
-import ghidra.app.util.bin.format.macho.MachHeader;
-import ghidra.app.util.bin.format.macho.commands.SegmentCommand;
-import ghidra.app.util.bin.format.macho.dyld.*;
import ghidra.app.util.opinion.DyldCacheExtractLoader;
-import ghidra.app.util.opinion.DyldCacheUtils.SplitDyldCache;
import ghidra.file.formats.ios.dyldcache.DyldCacheFileSystem;
import ghidra.formats.gfilesystem.*;
import ghidra.framework.plugintool.*;
@@ -109,15 +102,8 @@ public class DyldCacheBuilderPlugin extends Plugin {
try (FileSystemRef fsRef = openDyldCache(program, monitor)) {
DyldCacheFileSystem fs = (DyldCacheFileSystem) fsRef.getFilesystem();
- SplitDyldCache splitDyldCache = fs.getSplitDyldCache();
long refAddr = refAddress.getOffset();
- String fsPath = findInDylibSegment(refAddr, splitDyldCache);
- if (fsPath == null) {
- fsPath = findInStubs(refAddr, splitDyldCache);
- }
- if (fsPath == null) {
- fsPath = findInDyldData(refAddr, splitDyldCache);
- }
+ String fsPath = fs.findAddress(refAddr);
if (fsPath != null) {
ImporterUtilities.showAddToProgramDialog(fs.getFSRL().appendPath(fsPath), program,
tool, monitor);
@@ -130,7 +116,7 @@ public class DyldCacheBuilderPlugin extends Plugin {
catch (CancelledException e) {
// Do nothing
}
- catch (MachException | IOException e) {
+ catch (IOException e) {
Msg.showError(this, null, name, e.getMessage(), e);
}
}
@@ -158,80 +144,4 @@ public class DyldCacheBuilderPlugin extends Plugin {
FSRLRoot fsrlRoot = fsrl.getFS();
return FileSystemService.getInstance().getFilesystem(fsrlRoot, monitor);
}
-
- /**
- * Attempts to find the given address in the DYLD Cache's DYLIB segments
- *
- * @param addr The address to find
- * @param splitDyldCache The {@link SplitDyldCache}
- * @return The path of the DYLIB within the {@link DyldCacheFileSystem} that contains the given
- * address, or null if the address was not found
- * @throws MachException if there was an error parsing a DYLIB header
- * @throws IOException if an IO-related error occurred
- */
- private String findInDylibSegment(long addr, SplitDyldCache splitDyldCache)
- throws MachException, IOException {
- for (int i = 0; i < splitDyldCache.size(); i++) {
- DyldCacheHeader dyldCacheHeader = splitDyldCache.getDyldCacheHeader(i);
- ByteProvider provider = splitDyldCache.getProvider(i);
- for (DyldCacheImage mappedImage : dyldCacheHeader.getMappedImages()) {
- MachHeader machHeader = new MachHeader(provider,
- mappedImage.getAddress() - dyldCacheHeader.getBaseAddress());
- for (SegmentCommand segment : machHeader.parseSegments()) {
- if (segment.contains(addr)) {
- return mappedImage.getPath();
- }
- }
- }
- }
- return null;
- }
-
- /**
- * Attempts to find the given address in the DYLD Cache's text stubs
- *
- * @param addr The address to find
- * @param splitDyldCache The {@link SplitDyldCache}
- * @return The path of the text stub within the {@link DyldCacheFileSystem} that contains the
- * given address, or null if the address was not found
- */
- private String findInStubs(long addr, SplitDyldCache splitDyldCache) {
- for (int i = 0; i < splitDyldCache.size(); i++) {
- String dyldCacheName = splitDyldCache.getName(i);
- DyldCacheHeader dyldCacheHeader = splitDyldCache.getDyldCacheHeader(i);
- for (DyldCacheMappingAndSlideInfo mappingInfo : dyldCacheHeader
- .getCacheMappingAndSlideInfos()) {
- if (mappingInfo.contains(addr) && mappingInfo.isTextStubs()) {
- return DyldCacheFileSystem.getStubPath(dyldCacheName);
- }
- }
- }
- return null;
- }
-
- /**
- * Attempts to find the given address in the DYLD data
- *
- * @param addr The address to find
- * @param splitDyldCache The {@link SplitDyldCache}
- * @return The path of the Dyld data within the {@link DyldCacheFileSystem} that contains the
- * given address, or null if the address was not found
- */
- private String findInDyldData(long addr, SplitDyldCache splitDyldCache) {
- for (int i = 0; i < splitDyldCache.size(); i++) {
- String dyldCacheName = splitDyldCache.getName(i);
- if (dyldCacheName.endsWith(".dylddata")) {
- DyldCacheHeader dyldCacheHeader = splitDyldCache.getDyldCacheHeader(i);
- List<DyldCacheMappingAndSlideInfo> mappingInfos =
- dyldCacheHeader.getCacheMappingAndSlideInfos();
- for (int j = 0; j < mappingInfos.size(); j++) {
- DyldCacheMappingAndSlideInfo mappingInfo = mappingInfos.get(j);
- if (mappingInfo.contains(addr)) {
- return DyldCacheFileSystem.getDyldDataPath(dyldCacheName, j);
- }
- }
- }
- }
- return null;
- }
}