Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-5552: Universal Binary Mach-O Loader

Ryan Kurtz87fdda4981a8
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/ubi/FatHeader.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/ubi/FatHeader.java
index e9662e63e6..b0d331b240 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/ubi/FatHeader.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/ubi/FatHeader.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.
@@ -30,7 +30,8 @@ import ghidra.util.task.TaskMonitor;
/**
* Represents a fat_header structure.
*
- * @see <a href="https://opensource.apple.com/source/xnu/xnu-4570.71.2/EXTERNAL_HEADERS/mach-o/fat.h.auto.html">mach-o/fat.h</a>
+ * @see <a href="https://github.com/apple-oss-distributions/xnu/blob/main/EXTERNAL_HEADERS/mach-o/fat.h">mach-o/fat.h</a>
+ * @see <a href="https://github.com/apple-oss-distributions/xnu/blob/main/osfmk/mach/machine.h">mach/machine.h</a>
*/
public class FatHeader {
public final static int FAT_MAGIC = 0xcafebabe;
@@ -38,8 +39,10 @@ public class FatHeader {
private int magic;
private int nfat_arch;
- private List<FatArch> architectures = new ArrayList<FatArch>();
- private List<MachHeader> machHeaders = new ArrayList<MachHeader>();
+ private List<FatArch> architectures = new ArrayList<>();
+ private List<MachHeader> machHeaders = new ArrayList<>();
+ private List<Long> machStarts = new ArrayList<>();
+ private List<Long> machSizes = new ArrayList<>();
public FatHeader(ByteProvider provider)
throws IOException, UbiException, MachException {
@@ -78,6 +81,8 @@ public class FatHeader {
fatarch.getOffset() + camh.getPayloadOffset(), camh.getSize());
try {
machHeaders.add(new MachHeader(wrapper));
+ machStarts.add(fatarch.getOffset() + camh.getPayloadOffset());
+ machSizes.add(camh.getSize());
}
catch (MachException e) {
// Could be __.SYMDEF archive member instead of a Mach-O
@@ -86,6 +91,8 @@ public class FatHeader {
}
else {
machHeaders.add(new MachHeader(wrapper));
+ machStarts.add(Integer.toUnsignedLong(fatarch.getOffset()));
+ machSizes.add(Integer.toUnsignedLong(fatarch.getSize()));
}
}
}
@@ -105,4 +112,12 @@ public class FatHeader {
public List<MachHeader> getMachHeaders() {
return machHeaders;
}
+
+ public List<Long> getMachStarts() {
+ return machStarts;
+ }
+
+ public List<Long> getMachSizes() {
+ return machSizes;
+ }
}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/LoadSpec.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/LoadSpec.java
index a654c317e5..7a32bc75ca 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/LoadSpec.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/LoadSpec.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,6 +15,9 @@
*/
package ghidra.app.util.opinion;
+import org.apache.commons.lang3.builder.ToStringBuilder;
+import org.apache.commons.lang3.builder.ToStringStyle;
+
import ghidra.program.model.lang.LanguageCompilerSpecPair;
/**
@@ -136,4 +139,9 @@ public class LoadSpec {
public boolean isComplete() {
return !requiresLanguageCompilerSpec || lcs != null;
}
+
+ @Override
+ public String toString() {
+ return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
+ }
}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/UniversalMachoLoader.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/UniversalMachoLoader.java
new file mode 100644
index 0000000000..93b591c017
--- /dev/null
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/UniversalMachoLoader.java
@@ -0,0 +1,113 @@
+/* ###
+ * 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.app.util.opinion;
+
+import java.io.IOException;
+import java.util.*;
+
+import ghidra.app.util.Option;
+import ghidra.app.util.bin.*;
+import ghidra.app.util.bin.format.macho.MachException;
+import ghidra.app.util.bin.format.ubi.FatHeader;
+import ghidra.app.util.bin.format.ubi.UbiException;
+import ghidra.app.util.importer.MessageLog;
+import ghidra.program.model.listing.Program;
+import ghidra.util.task.TaskMonitor;
+
+/**
+ * A {@link Loader} for Mach-O files contained in a Universal Binary
+ */
+public class UniversalMachoLoader extends MachoLoader {
+
+ public final static String UNIVERSAL_MACH_O_NAME = "Universal Mach-O";
+ private static final long MIN_BYTE_LENGTH = 4;
+
+ @Override
+ public Collection<LoadSpec> findSupportedLoadSpecs(ByteProvider provider) throws IOException {
+ List<LoadSpec> loadSpecs = new ArrayList<>();
+
+ // Efficient check to fail fast
+ if (provider.length() < MIN_BYTE_LENGTH) {
+ return loadSpecs;
+ }
+
+ // Efficient check to fail fast
+ BinaryReader reader = new BinaryReader(provider, false);
+ int magic = reader.readInt(0);
+ if (magic != FatHeader.FAT_MAGIC && magic != FatHeader.FAT_CIGAM) {
+ return loadSpecs;
+ }
+
+ // Only add the preferred load specs for each Mach-O so only 1 entry for each architecture
+ // shows up. Keep them all preferred though so the user is forced to pick one (headless
+ // will just use the "first preferred" by default)
+ for (ByteProvider wrapper : getWrappers(provider)) {
+ super.findSupportedLoadSpecs(wrapper).stream()
+ .filter(LoadSpec::isPreferred)
+ .forEach(loadSpecs::add);
+ }
+
+ return loadSpecs;
+ }
+
+ @Override
+ public void load(ByteProvider provider, LoadSpec loadSpec, List<Option> options,
+ Program program, TaskMonitor monitor, MessageLog log) throws IOException {
+ for (ByteProvider wrapper : getWrappers(provider)) {
+ for (LoadSpec ls : super.findSupportedLoadSpecs(wrapper)) {
+ if (monitor.isCancelled()) {
+ break;
+ }
+ if (loadSpec.getLanguageCompilerSpec().equals(ls.getLanguageCompilerSpec())) {
+ super.load(wrapper, loadSpec, options, program, monitor, log);
+ return;
+ }
+ }
+ }
+ }
+
+ @Override
+ public String getName() {
+ return UNIVERSAL_MACH_O_NAME;
+ }
+
+ /**
+ * Gets a {@link List} of {@link ByteProviderWrapper}s, one for each entry in the Universal
+ * Binary
+ *
+ * @param provider The Universal Binary's provider
+ * @return A {@link List} of {@link ByteProviderWrapper}s, one for each entry in the Universal
+ * Binary
+ * @throws IOException if an IO-related error occurred
+ */
+ private List<ByteProviderWrapper> getWrappers(ByteProvider provider) throws IOException {
+ List<ByteProviderWrapper> wrappers = new ArrayList<>();
+ try {
+ FatHeader fatHeader = new FatHeader(provider);
+ List<Long> machStarts = fatHeader.getMachStarts();
+ List<Long> machSizes = fatHeader.getMachSizes();
+ for (int i = 0; i < machStarts.size(); i++) {
+ wrappers.add(new ByteProviderWrapper(provider, machStarts.get(i), machSizes.get(i),
+ provider.getFSRL()));
+ }
+ }
+ catch (MachException | UbiException e) {
+ // not a problem, just don't add it
+ }
+ return wrappers;
+ }
+
+}