Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MachO support #81

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/integration_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
uses: er28-0652/[email protected]
if: steps.cache.outputs.cache-hit != 'true'
with:
version: '10.4'
version: '11.1.2'
- name: Download Z3
uses: pavpanchekha/[email protected]
if: steps.cache.outputs.cache-hit != 'true'
Expand All @@ -39,7 +39,7 @@ jobs:
if: steps.cache.outputs.cache-hit == 'true'
run: |
echo "CPATH=/opt/hostedtoolcache/z3/4.8.15/x64/z3-4.8.15-x64-glibc-2.31/include" >> $GITHUB_ENV
echo "GHIDRA_INSTALL_DIR=/opt/hostedtoolcache/ghidra/10.4/x64" >> $GITHUB_ENV
echo "GHIDRA_INSTALL_DIR=/opt/hostedtoolcache/ghidra/11.1.2/x64" >> $GITHUB_ENV
- name: Setup Z3
run: |
cp $CPATH/../bin/com.microsoft.z3.jar $GITHUB_WORKSPACE/lib/com.microsoft.z3.jar
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/unittest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
uses: er28-0652/[email protected]
if: steps.cache.outputs.cache-hit != 'true'
with:
version: '10.4'
version: '11.1.2'
- name: Download Z3
uses: pavpanchekha/[email protected]
if: steps.cache.outputs.cache-hit != 'true'
Expand All @@ -39,7 +39,7 @@ jobs:
if: steps.cache.outputs.cache-hit == 'true'
run: |
echo "CPATH=/opt/hostedtoolcache/z3/4.8.15/x64/z3-4.8.15-x64-glibc-2.31/include" >> $GITHUB_ENV
echo "GHIDRA_INSTALL_DIR=/opt/hostedtoolcache/ghidra/10.4/x64" >> $GITHUB_ENV
echo "GHIDRA_INSTALL_DIR=/opt/hostedtoolcache/ghidra/11.1.2/x64" >> $GITHUB_ENV
- name: Setup Z3
run: |
cp $CPATH/../bin/com.microsoft.z3.jar $GITHUB_WORKSPACE/lib/com.microsoft.z3.jar
Expand Down
36 changes: 35 additions & 1 deletion src/main/java/com/bai/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@
import ghidra.app.util.bin.MemoryByteProvider;
import ghidra.app.util.bin.format.elf.ElfException;
import ghidra.app.util.bin.format.elf.ElfHeader;
import ghidra.app.util.bin.format.macho.commands.EntryPointCommand;
import ghidra.app.util.bin.format.macho.commands.LoadCommand;
import ghidra.app.util.bin.format.macho.commands.LoadCommandTypes;
import ghidra.app.util.bin.format.pe.PortableExecutable;
import ghidra.app.util.bin.format.pe.OptionalHeader;
import ghidra.app.util.bin.format.macho.MachHeader;
import ghidra.app.util.opinion.ElfLoader;
import ghidra.app.util.opinion.PeLoader;
import ghidra.app.util.opinion.MachoLoader;
import ghidra.program.model.address.Address;
import ghidra.program.model.data.FunctionDefinitionDataType;
import ghidra.program.model.data.IntegerDataType;
Expand Down Expand Up @@ -330,6 +335,15 @@ public static List<Reference> getReferences(List<String> symbolNames) {
Logging.error("Empty symbol table");
return new ArrayList<>();
}

// OS X ABI for Mach-O format mangles C symbols by prepending an underscore (_) to symbols
if (GlobalState.currentProgram.getExecutableFormat().equals(MachoLoader.MACH_O_NAME)) {
List<String> machOMangledNames = symbolNames.stream()
.map(name -> "_" + name)
.toList();
symbolNames.addAll(machOMangledNames);
}

return Stream.generate(() -> null)
.takeWhile(x -> symbolIterator.hasNext())
.map(n -> symbolIterator.next())
Expand All @@ -343,7 +357,7 @@ public static List<Reference> getReferences(List<String> symbolNames) {
}

/**
* Get the entry function of the ELF/PE executable.
* Get the entry function of the ELF/PE/MACH-O executable.
* @return
*/
public static Function getEntryFunction() {
Expand Down Expand Up @@ -371,7 +385,27 @@ public static Function getEntryFunction() {
entryAddress = entryAddress.add(GlobalState.currentProgram.getImageBase().getOffset());
}
break;
case MachoLoader.MACH_O_NAME: {
MachHeader header = new MachHeader(provider);
header.parse();

if (!header.parseAndCheck(LoadCommandTypes.LC_MAIN)) {
throw new RuntimeException("Could not locate the entrypoint of the Mach-O binary executable.");
}

Optional<LoadCommand> loadCommandOptional = header.getLoadCommands().stream()
.filter(loadCommand -> loadCommand.getCommandType() == LoadCommandTypes.LC_MAIN)
.findFirst();

if (loadCommandOptional.isEmpty()) {
throw new RuntimeException("Could not find the LC_MAIN load command in the Mach-O header.");
}

entryAddress = (GlobalState.currentProgram.getImageBase())
.add(((EntryPointCommand) loadCommandOptional.get()).getEntryOffset());

}
break;
default:
throw new Exception("Unsupported file format.");
}
Expand Down
Loading