Ghidra is a software reverse engineering (SRE) framework
GP-2698 fix DWARF handling of MachO .o files
dev7473681cf9ba78c28a
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/next/DWARFFunctionImporter.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/next/DWARFFunctionImporter.javaindex 049f3876de..0ffe5f73a6 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/next/DWARFFunctionImporter.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/next/DWARFFunctionImporter.java@@ -18,10 +18,11 @@ package ghidra.app.util.bin.format.dwarf4.next;import static ghidra.app.util.bin.format.dwarf4.encoding.DWARFAttribute.*;import static ghidra.app.util.bin.format.dwarf4.encoding.DWARFTag.*;-import java.io.IOException;import java.util.*;import java.util.stream.Collectors;+import java.io.IOException;+import ghidra.app.cmd.comments.AppendCommentCmd;import ghidra.app.cmd.label.SetLabelPrimaryCmd;import ghidra.app.util.bin.format.dwarf4.*;@@ -181,11 +182,12 @@ public class DWARFFunctionImporter {return true;}- // fetch the low_pc attribute directly instead of calling diea.getLowPc() to avoid- // any fixups applied by lower level code+ long lowPC = diea.getLowPC(0); // adjusted by program base addr fixupDWARFNumericAttribute attr =diea.getAttribute(DWARFAttribute.DW_AT_low_pc, DWARFNumericAttribute.class);- if (attr != null && attr.getUnsignedValue() == 0) {+ if (attr != null && attr.getUnsignedValue() == 0 && lowPC != 0) {+ // don't process this func if its raw lowpc is 0, with the exception of a binary (a .o)+ // that starts at 0 and has a function at 0return true;}diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/next/DWARFProgram.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/next/DWARFProgram.javaindex 7dc220501f..6061c28a20 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/next/DWARFProgram.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/next/DWARFProgram.java@@ -76,7 +76,8 @@ public class DWARFProgram implements Closeable {return hasExpectedDWARFSections(program) ||ExternalDebugInfo.fromProgram(program) != null;case MachoLoader.MACH_O_NAME:- return DSymSectionProvider.getDSYMForProgram(program) != null;+ return hasExpectedDWARFSections(program) ||+ DSymSectionProvider.getDSYMForProgram(program) != null;}return false;}diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/next/sectionprovider/BaseSectionProvider.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/next/sectionprovider/BaseSectionProvider.javaindex 8cf5384d68..6fafd698c5 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/next/sectionprovider/BaseSectionProvider.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/next/sectionprovider/BaseSectionProvider.java@@ -15,6 +15,8 @@*/package ghidra.app.util.bin.format.dwarf4.next.sectionprovider;+import java.util.List;+import java.io.IOException;import ghidra.app.util.bin.ByteProvider;@@ -28,6 +30,7 @@ import ghidra.util.task.TaskMonitor;*/public class BaseSectionProvider implements DWARFSectionProvider {protected Program program;+ protected List<String> sectionPrefixes = getSectionPrefixSearchList();public static BaseSectionProvider createSectionProviderFor(Program program,TaskMonitor monitor) {@@ -38,14 +41,15 @@ public class BaseSectionProvider implements DWARFSectionProvider {this.program = program;}+ protected List<String> getSectionPrefixSearchList() {+ return List.of(".", "_", "__");+ }+@Overridepublic ByteProvider getSectionAsByteProvider(String sectionName, TaskMonitor monitor)throws IOException {- MemoryBlock block = program.getMemory().getBlock(sectionName);- if (block == null) {- block = program.getMemory().getBlock("." + sectionName);- }+ MemoryBlock block = getSection(sectionName);if (block != null && block.isInitialized()) {// NOTE: MemoryByteProvider instances don't need to be closed(), so we don't// track them here@@ -58,14 +62,26 @@ public class BaseSectionProvider implements DWARFSectionProvider {@Overridepublic boolean hasSection(String... sectionNames) {for (String sectionName : sectionNames) {- if (program.getMemory().getBlock(sectionName) == null &&- program.getMemory().getBlock("." + sectionName) == null) {+ if (getSection(sectionName) == null) {return false;}}return true;}+ protected MemoryBlock getSection(String sectionName) {+ MemoryBlock block = program.getMemory().getBlock(sectionName);+ if (block == null) {+ for (String prefix : sectionPrefixes) {+ block = program.getMemory().getBlock(prefix + sectionName);+ if (block != null) {+ break;+ }+ }+ }+ return block;+ }+@Overridepublic void close() {// nothing