Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-0: Improving Mach-O loader progress messages (#7863)

Ryan Kurtz825b6754f126
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/commands/chained/DyldChainedFixups.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/commands/chained/DyldChainedFixups.java
index 96c356069a..7c6aae4da3 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/commands/chained/DyldChainedFixups.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/commands/chained/DyldChainedFixups.java
@@ -139,8 +139,9 @@ public class DyldChainedFixups {
return fixedAddrs;
}
Memory memory = program.getMemory();
+ monitor.initialize(fixups.size(), "Fixing up chained pointers...");
for (DyldFixup fixup : fixups) {
- monitor.checkCancelled();
+ monitor.increment();
Status status = Status.FAILURE;
Address addr = imagebase.add(fixup.offset());
try {
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/MachoProgramBuilder.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/MachoProgramBuilder.java
index cd9a9defea..60f8af9404 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/MachoProgramBuilder.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/MachoProgramBuilder.java
@@ -211,7 +211,6 @@ public class MachoProgramBuilder {
*/
protected void processMemoryBlocks(MachHeader header, String source, boolean processSections,
boolean allowZeroAddr) throws Exception {
- monitor.setMessage("Processing memory blocks for " + source + "...");
if (header.getFileType() == MachHeaderFileTypes.MH_DYLIB_STUB) {
return;
@@ -220,10 +219,10 @@ public class MachoProgramBuilder {
Set<Section> overlaySections = new HashSet<>();
// Create memory blocks for segments.
- for (SegmentCommand segment : header.getAllSegments()) {
- if (monitor.isCancelled()) {
- break;
- }
+ List<SegmentCommand> segments = header.getAllSegments();
+ monitor.initialize(segments.size(), "Processing segments for " + source + "...");
+ for (SegmentCommand segment : segments) {
+ monitor.increment();
if (segment.getFileSize() > 0 && segment.getVMsize() > 0 &&
(allowZeroAddr || segment.getVMaddress() != 0)) {
@@ -266,10 +265,10 @@ public class MachoProgramBuilder {
// Create memory blocks for sections. They will be in the segments we just created, so the
// segment blocks will be split and possibly replaced.
if (processSections) {
- for (Section section : header.getAllSections()) {
- if (monitor.isCancelled()) {
- break;
- }
+ List<Section> sections = header.getAllSections();
+ monitor.initialize(sections.size(), "Processing sections for " + source + "...");
+ for (Section section : sections) {
+ monitor.increment();
AddressSpace sectionSpace = overlaySections.contains(section)
? segmentOverlayMap.get(section.getSegmentName())
: space;
@@ -519,8 +518,6 @@ public class MachoProgramBuilder {
}
protected boolean processExports(MachHeader header) throws Exception {
- monitor.setMessage("Processing exports...");
-
List<ExportEntry> exports = new ArrayList<>();
// Old way - export tree in DyldInfoCommand
@@ -548,7 +545,9 @@ public class MachoProgramBuilder {
}
Address baseAddr = space.getAddress(textSegment.getVMaddress());
+ monitor.initialize(exports.size(), "Processing exports...");
for (ExportEntry export : exports) {
+ monitor.increment();
String name = SymbolUtilities.replaceInvalidChars(export.name(), true);
try {
processNewExport(baseAddr, export, name);
@@ -572,15 +571,13 @@ public class MachoProgramBuilder {
}
protected void processSymbolTables(MachHeader header, boolean processExports) throws Exception {
- monitor.setMessage("Processing symbol tables...");
SymbolTable symbolTable = program.getSymbolTable();
List<SymbolTableCommand> commands = header.getLoadCommands(SymbolTableCommand.class);
for (SymbolTableCommand symbolTableCommand : commands) {
List<NList> symbols = symbolTableCommand.getSymbols();
+ monitor.initialize(symbols.size(), "Processing symbol tables...");
for (NList symbol : symbols) {
- if (monitor.isCancelled()) {
- return;
- }
+ monitor.increment();
if (symbol.isTypePreboundUndefined()) {
continue;
@@ -644,8 +641,6 @@ public class MachoProgramBuilder {
}
protected void processStubs() throws Exception {
- monitor.setMessage("Processing stubs...");
-
SymbolTableCommand symbolTableCommand =
machoHeader.getFirstLoadCommand(SymbolTableCommand.class);
@@ -661,9 +656,7 @@ public class MachoProgramBuilder {
}
for (Section section : machoHeader.getAllSections()) {
- if (monitor.isCancelled()) {
- return;
- }
+ monitor.checkCancelled();
if (section.getSize() == 0 || section.getType() != SectionTypes.S_SYMBOL_STUBS) {
continue;
}
@@ -678,10 +671,9 @@ public class MachoProgramBuilder {
int nSymbols = (int) section.getSize() / symbolSize;
Address startAddr = space.getAddress(section.getAddress());
+ monitor.initialize(nSymbols, "Processing stubs...");
for (int i = indirectSymbolTableIndex; i < indirectSymbolTableIndex + nSymbols; ++i) {
- if (monitor.isCancelled()) {
- break;
- }
+ monitor.increment();
int symbolIndex = indirectSymbols.get(i);
NList symbol = symbolTableCommand.getSymbolAt(symbolIndex);
if (symbol == null) {
@@ -703,8 +695,6 @@ public class MachoProgramBuilder {
}
protected void processUndefinedSymbols() throws Exception {
-
- monitor.setMessage("Processing undefined symbols...");
List<NList> undefinedSymbols = new ArrayList<>();
List<LoadCommand> commands = machoHeader.getLoadCommands();
for (LoadCommand command : commands) {
@@ -716,10 +706,9 @@ public class MachoProgramBuilder {
}
SymbolTableCommand symbolTableCommand = (SymbolTableCommand) command;
List<NList> symbols = symbolTableCommand.getSymbols();
+ monitor.initialize(symbols.size(), "Collectiing undefined symbols...");
for (NList symbol : symbols) {
- if (monitor.isCancelled()) {
- return;
- }
+ monitor.increment();
if (symbol.isSymbolicDebugging()) {
continue;
}
@@ -738,10 +727,9 @@ public class MachoProgramBuilder {
try {
Address addr = MachoProgramUtils.addExternalBlock(program,
undefinedSymbols.size() * machoHeader.getAddressSize(), log);
+ monitor.initialize(undefinedSymbols.size(), "Processing undefined symbols...");
for (NList symbol : undefinedSymbols) {
- if (monitor.isCancelled()) {
- return;
- }
+ monitor.increment();
try {
String name = SymbolUtilities.replaceInvalidChars(symbol.getString(), true);
if (name != null && name.length() > 0) {
@@ -762,22 +750,18 @@ public class MachoProgramBuilder {
}
protected void processAbsoluteSymbols() throws Exception {
- monitor.setMessage("Processing absolute symbols...");
List<NList> absoluteSymbols = new ArrayList<>();
List<LoadCommand> commands = machoHeader.getLoadCommands();
for (LoadCommand command : commands) {
- if (monitor.isCancelled()) {
- return;
- }
+ monitor.checkCancelled();
if (!(command instanceof SymbolTableCommand)) {
continue;
}
SymbolTableCommand symbolTableCommand = (SymbolTableCommand) command;
List<NList> symbols = symbolTableCommand.getSymbols();
+ monitor.initialize(symbols.size(), "Collecting absolute symbols...");
for (NList symbol : symbols) {
- if (monitor.isCancelled()) {
- return;
- }
+ monitor.increment();
if (symbol.isSymbolicDebugging()) {
continue;
}
@@ -793,7 +777,9 @@ public class MachoProgramBuilder {
try {
memory.createUninitializedBlock("ABSOLUTE", start,
absoluteSymbols.size() * machoHeader.getAddressSize(), false);
+ monitor.initialize(absoluteSymbols.size(), "Processing absolute symbols...");
for (NList symbol : absoluteSymbols) {
+ monitor.increment();
try {
String name = SymbolUtilities.replaceInvalidChars(symbol.getString(), true);
if (name != null && name.length() > 0) {
@@ -825,6 +811,7 @@ public class MachoProgramBuilder {
BinaryReader memReader = new BinaryReader(new MemoryByteProvider(memory, imagebase),
!memory.isBigEndian());
for (DyldChainedFixupsCommand loadCommand : loadCommands) {
+ monitor.checkCancelled();
fixups.addAll(loadCommand.getChainedFixups(memReader, imagebase.getOffset(),
symbolTable, log, monitor));
}
@@ -842,6 +829,7 @@ public class MachoProgramBuilder {
DyldChainedStartsOffsets chainedStartsOffsets =
new DyldChainedStartsOffsets(reader);
for (int offset : chainedStartsOffsets.getChainStartOffsets()) {
+ monitor.checkCancelled();
fixups.addAll(DyldChainedFixups.getChainedFixups(reader, null,
chainedStartsOffsets.getPointerFormat(), offset, 0, 0,
imagebase.getOffset(), symbolTable, log, monitor));
@@ -918,7 +906,9 @@ public class MachoProgramBuilder {
if (threadedBindings != null) {
DyldChainedImports chainedImports = new DyldChainedImports(bindings);
+ monitor.initialize(threadedBindings.size(), "Processing threaded bindings...");
for (Binding threadedBinding : threadedBindings) {
+ monitor.increment();
List<DyldFixup> fixups = DyldChainedFixups.getChainedFixups(reader,
chainedImports, DyldChainType.DYLD_CHAINED_PTR_ARM64E,
segments.get(threadedBinding.getSegmentIndex()).getFileOffset(),
@@ -929,7 +919,9 @@ public class MachoProgramBuilder {
}
}
else {
+ monitor.initialize(bindings.size(), "Processing bindings...");
for (Binding binding : bindings) {
+ monitor.increment();
if (binding.getUnknownOpcode() != null) {
log.appendMsg(
"Unknown bind opcode: 0x%x".formatted(binding.getUnknownOpcode()));
@@ -976,8 +968,6 @@ public class MachoProgramBuilder {
}
protected void markupHeaders(MachHeader header, Address headerAddr) throws Exception {
- monitor.setMessage("Processing header markup...");
-
if (headerAddr == null) {
return;
}
@@ -986,21 +976,20 @@ public class MachoProgramBuilder {
DataUtilities.createData(program, headerAddr, header.toDataType(), -1,
DataUtilities.ClearDataMode.CHECK_FOR_SPACE);
+ monitor.initialize(header.getLoadCommands().size(), "Marking up header...");
for (LoadCommand loadCommand : header.getLoadCommands()) {
- if (monitor.isCancelled()) {
- break;
- }
+ monitor.increment();
Address loadCommandAddr =
headerAddr.add(loadCommand.getStartIndex() - header.getStartIndexInProvider());
DataType loadCommandDataType = loadCommand.toDataType();
DataUtilities.createData(program, loadCommandAddr, loadCommandDataType, -1,
DataUtilities.ClearDataMode.CHECK_FOR_SPACE);
- listing.setComment(loadCommandAddr, CodeUnit.PRE_COMMENT,
+ listing.setComment(loadCommandAddr, CommentType.PRE,
LoadCommandTypes.getLoadCommandName(loadCommand.getCommandType()));
if (loadCommand instanceof SegmentCommand) {
SegmentCommand segmentCommand = (SegmentCommand) loadCommand;
- listing.setComment(loadCommandAddr, CodeUnit.EOL_COMMENT,
+ listing.setComment(loadCommandAddr, CommentType.EOL,
segmentCommand.getSegmentName());
int sectionOffset = loadCommandDataType.getLength();
@@ -1009,7 +998,7 @@ public class MachoProgramBuilder {
Address sectionAddr = loadCommandAddr.add(sectionOffset);
DataUtilities.createData(program, sectionAddr, sectionDataType, -1,
DataUtilities.ClearDataMode.CHECK_FOR_SPACE);
- listing.setComment(sectionAddr, CodeUnit.EOL_COMMENT,
+ listing.setComment(sectionAddr, CommentType.EOL,
section.getSegmentName() + "." + section.getSectionName());
sectionOffset += sectionDataType.getLength();
}
@@ -1134,18 +1123,12 @@ public class MachoProgramBuilder {
}
protected void markupSections() throws Exception {
-
- monitor.setMessage("Processing section markup...");
-
if (machoHeader.getFileType() == MachHeaderFileTypes.MH_DYLIB_STUB) {
return;
}
for (SegmentCommand segment : machoHeader.getAllSegments()) {
-
- if (monitor.isCancelled()) {
- break;
- }
+ monitor.checkCancelled();
// do not markup sections that are encrypted
if (segment.isAppleProtected()) {
@@ -1157,12 +1140,9 @@ public class MachoProgramBuilder {
}
List<Section> sections = segment.getSections();
-
+ monitor.initialize(sections.size(), "Marking up sections...");
for (Section section : sections) {
-
- if (monitor.isCancelled()) {
- break;
- }
+ monitor.increment();
if (section.getSize() == 0) {
continue;
@@ -1217,8 +1197,6 @@ public class MachoProgramBuilder {
* @throws CancelledException if the operation was cancelled.
*/
protected void processSectionRelocations() throws CancelledException {
- monitor.setMessage("Processing section relocations...");
-
LinkedHashMap<RelocationInfo, Address> relocationMap = new LinkedHashMap<>();
for (Section section : machoHeader.getAllSections()) {
monitor.checkCancelled();
@@ -1231,9 +1209,10 @@ public class MachoProgramBuilder {
}
continue;
}
-
- for (RelocationInfo relocationInfo : section.getRelocations()) {
- monitor.checkCancelled();
+ List<RelocationInfo> relocations = section.getRelocations();
+ monitor.initialize(relocations.size(), "Processing section relocations...");
+ for (RelocationInfo relocationInfo : relocations) {
+ monitor.increment();
Address address = sectionMemoryBlock.getStart().add(relocationInfo.getAddress());
relocationMap.put(relocationInfo, address);
}
@@ -1247,15 +1226,14 @@ public class MachoProgramBuilder {
* @throws CancelledException if the operation was cancelled.
*/
protected void processExternalRelocations() throws CancelledException {
-
- monitor.setMessage("Processing external relocations...");
-
LinkedHashMap<RelocationInfo, Address> relocationMap = new LinkedHashMap<>();
for (DynamicSymbolTableCommand cmd : machoHeader
.getLoadCommands(DynamicSymbolTableCommand.class)) {
monitor.checkCancelled();
- for (RelocationInfo relocationInfo : cmd.getExternalRelocations()) {
- monitor.checkCancelled();
+ List<RelocationInfo> relocations = cmd.getExternalRelocations();
+ monitor.initialize(relocations.size(), "Processing external relocations...");
+ for (RelocationInfo relocationInfo : relocations) {
+ monitor.increment();
relocationMap.put(relocationInfo, space.getAddress(relocationInfo.getAddress()));
}
}
@@ -1275,8 +1253,10 @@ public class MachoProgramBuilder {
for (DynamicSymbolTableCommand cmd : machoHeader
.getLoadCommands(DynamicSymbolTableCommand.class)) {
monitor.checkCancelled();
- for (RelocationInfo relocationInfo : cmd.getLocalRelocations()) {
- monitor.checkCancelled();
+ List<RelocationInfo> relocations = cmd.getLocalRelocations();
+ monitor.initialize(relocations.size(), "Processing local relocations...");
+ for (RelocationInfo relocationInfo : relocations) {
+ monitor.increment();
relocationMap.put(relocationInfo, space.getAddress(relocationInfo.getAddress()));
}
}
@@ -1284,16 +1264,13 @@ public class MachoProgramBuilder {
}
protected List<String> processLibraries() throws Exception {
- monitor.setMessage("Processing libraries...");
-
Options props = program.getOptions(Program.PROGRAM_INFO);
int libraryIndex = 0;
List<String> libraryPaths = new ArrayList<>();
-
- for (LoadCommand command : machoHeader.getLoadCommands()) {
- if (monitor.isCancelled()) {
- return libraryPaths;
- }
+ List<LoadCommand> loadCommands = machoHeader.getLoadCommands();
+ monitor.initialize(loadCommands.size(), "Processing libraries...");
+ for (LoadCommand command : loadCommands) {
+ monitor.increment();
String libraryPath = null;
@@ -1334,9 +1311,11 @@ public class MachoProgramBuilder {
* @throws Exception if there was a problem detecting the encrypted block ranges
*/
protected void processEncryption() throws Exception {
- monitor.setMessage("Processing encryption...");
- for (EncryptedInformationCommand cmd : machoHeader
- .getLoadCommands(EncryptedInformationCommand.class)) {
+ List<EncryptedInformationCommand> cmds =
+ machoHeader.getLoadCommands(EncryptedInformationCommand.class);
+ monitor.initialize(cmds.size(), "Processing encryption...");
+ for (EncryptedInformationCommand cmd : cmds) {
+ monitor.increment();
if (cmd.getCryptID() != 0) {
log.appendMsg(String.format("ENCRYPTION DETECTED: (file offset 0x%x, size 0x%x)",
cmd.getCryptOffset(), cmd.getCryptSize()));
@@ -1350,10 +1329,11 @@ public class MachoProgramBuilder {
* @throws CancelledException if the operation was cancelled.
*/
protected void processUnsupportedLoadCommands() throws CancelledException {
- monitor.setMessage("Processing unsupported load commands...");
-
- for (LoadCommand cmd : machoHeader.getLoadCommands(UnsupportedLoadCommand.class)) {
- monitor.checkCancelled();
+ List<UnsupportedLoadCommand> cmds =
+ machoHeader.getLoadCommands(UnsupportedLoadCommand.class);
+ monitor.initialize(cmds.size(), "Processing unsupported load commands...");
+ for (LoadCommand cmd : cmds) {
+ monitor.increment();
log.appendMsg("Skipping unsupported load command: " +
LoadCommandTypes.getLoadCommandName(cmd.getCommandType()));
}
@@ -1365,10 +1345,10 @@ public class MachoProgramBuilder {
* @throws CancelledException if the operation was cancelled.
*/
protected void processCorruptLoadCommands() throws CancelledException {
- monitor.setMessage("Processing corrupt load commands...");
-
- for (CorruptLoadCommand cmd : machoHeader.getLoadCommands(CorruptLoadCommand.class)) {
- monitor.checkCancelled();
+ List<CorruptLoadCommand> cmds = machoHeader.getLoadCommands(CorruptLoadCommand.class);
+ monitor.initialize(cmds.size(), "Processing corrupt load commands...");
+ for (CorruptLoadCommand cmd : cmds) {
+ monitor.increment();
log.appendMsg("Skipping corrupt load command: %s (%s: %s)".formatted(
LoadCommandTypes.getLoadCommandName(cmd.getCommandType()),
cmd.getProblem().getClass().getSimpleName(),
@@ -1454,7 +1434,9 @@ public class MachoProgramBuilder {
* @throws Exception If there was a problem performing the markup
*/
protected void markupLoadCommandData(MachHeader header, String source) throws Exception {
+ monitor.initialize(header.getLoadCommands().size(), "Marking up load command data...");
for (LoadCommand cmd : header.getLoadCommands()) {
+ monitor.increment();
cmd.markup(program, header, source, monitor, log);
}
}
@@ -1684,8 +1666,9 @@ public class MachoProgramBuilder {
*/
protected void markupChainedFixups(MachHeader header, List<Address> chainedFixups)
throws CancelledException {
+ monitor.initialize(chainedFixups.size(), "Marking up chained fixups...");
for (Address addr : chainedFixups) {
- monitor.checkCancelled();
+ monitor.increment();
try {
listing.createData(addr, PointerDataType.dataType);
}
@@ -1695,10 +1678,7 @@ public class MachoProgramBuilder {
}
}
- /**
- * See crt.c from opensource.apple.com
- */
- protected void markupProgramVars() {
+ protected void markupProgramVars() throws Exception {
if (program.getLanguage().getProcessor() == Processor
.findOrPossiblyCreateProcessor("PowerPC")) {
return;
@@ -1730,7 +1710,9 @@ public class MachoProgramBuilder {
Namespace namespace = createNamespace(SectionNames.PROGRAM_VARS);
List<Section> sections = machoHeader.getAllSections();
+ monitor.initialize(sections.size(), "Marking up program variables...");
for (Section section : sections) {
+ monitor.increment();
if (section.getSectionName().equals(SectionNames.PROGRAM_VARS)) {
MemoryBlock memoryBlock = getMemoryBlock(section);
try {