Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-5755 Fix Apple KDK DWARF section name lookup, atomic type and source file npe

dev747368e2fa4aaa7b6f
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/DWARFDataTypeImporter.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/DWARFDataTypeImporter.java
index b160583c32..3f533dfa78 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/DWARFDataTypeImporter.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/DWARFDataTypeImporter.java
@@ -173,6 +173,7 @@ public class DWARFDataTypeImporter {
case DW_TAG_restrict_type:
case DW_TAG_shared_type:
case DW_TAG_APPLE_ptrauth_type:
+ case DW_TAG_atomic_type:
result = makeDataTypeForConst(diea);
break;
case DW_TAG_enumeration_type:
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/DWARFImportSummary.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/DWARFImportSummary.java
index f36816b2ae..bc23c7b138 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/DWARFImportSummary.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/DWARFImportSummary.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.
@@ -41,6 +41,8 @@ public class DWARFImportSummary {
int exprReadError;
Set<String> typeRemappings = new HashSet<>();
int paramZeroLenDataType;
+ public int badSourceFileCount;
+
Set<Integer> dwarfVers = new HashSet<>();
int compUnitCount;
int dieCount;
@@ -136,6 +138,9 @@ public class DWARFImportSummary {
if (exprReadError > 0) {
Msg.error(this, "DWARF expression failed to read: " + exprReadError);
}
+ if (badSourceFileCount > 0) {
+ Msg.error(this, "DWARF source file info errors: %d".formatted(badSourceFileCount));
+ }
}
private <T extends Comparable<T>> List<T> getSortedSet(Set<T> set) {
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/line/DWARFLine.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/line/DWARFLine.java
index af2da49c7e..92273c158f 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/line/DWARFLine.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/line/DWARFLine.java
@@ -282,8 +282,13 @@ public class DWARFLine {
if (cu.getDWARFVersion() >= 5 && (row.file < cu.getLine().getNumFiles())) {
md5 = cu.getLine().getFile(row.file).getMD5();
}
- results.add(new SourceFileAddr(row.address, getFilePath(row.file, true), md5,
- row.line, row.isEndSequence));
+ String filePath = getFilePath(row.file, true);
+ if (filePath != null) {
+ results.add(new SourceFileAddr(row.address, filePath, md5, row.line,
+ row.isEndSequence));
+ } else {
+ cu.getProgram().getImportSummary().badSourceFileCount++;
+ }
}
return results;
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/sectionprovider/DSymSectionProvider.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/sectionprovider/DSymSectionProvider.java
index 647b6e3776..2dff58772b 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/sectionprovider/DSymSectionProvider.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/sectionprovider/DSymSectionProvider.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,11 +15,11 @@
*/
package ghidra.app.util.bin.format.dwarf.sectionprovider;
-import java.util.HashMap;
-import java.util.Map;
-
import java.io.File;
import java.io.IOException;
+import java.nio.file.AccessMode;
+import java.util.HashMap;
+import java.util.Map;
import ghidra.app.util.bin.*;
import ghidra.app.util.bin.format.macho.*;
@@ -35,7 +35,7 @@ import ghidra.util.task.TaskMonitor;
public class DSymSectionProvider implements DWARFSectionProvider {
private MachHeader machHeader;
private Map<String, Section> machSectionsByName = new HashMap<>();
- private RandomAccessByteProvider provider;
+ private FileByteProvider provider;
public static File getDSYMForProgram(Program program) {
@@ -63,7 +63,7 @@ public class DSymSectionProvider implements DWARFSectionProvider {
}
public DSymSectionProvider(File dsymFile) throws IOException, MachException {
- this.provider = new RandomAccessByteProvider(dsymFile);
+ this.provider = new FileByteProvider(dsymFile, null, AccessMode.READ);
machHeader = new MachHeader(provider);
machHeader.parse();
@@ -78,11 +78,20 @@ public class DSymSectionProvider implements DWARFSectionProvider {
public ByteProvider getSectionAsByteProvider(String sectionName, TaskMonitor monitor)
throws IOException {
- Section s = machSectionsByName.get(sectionName);
+ Section s = findSectionByName(sectionName);
return (s != null) ? new ByteProviderWrapper(provider,
machHeader.getStartIndex() + s.getOffset(), s.getSize()) : null;
}
+ private Section findSectionByName(String name) {
+ Section section = machSectionsByName.get(name);
+ if (section == null &&
+ name.length() > 14 /* max macho section name length - 2 for leading "__" */) {
+ section = machSectionsByName.get(name.substring(0, 14));
+ }
+ return section;
+ }
+
@Override
public void close() {
FSUtilities.uncheckedClose(provider, null);
@@ -91,7 +100,7 @@ public class DSymSectionProvider implements DWARFSectionProvider {
@Override
public boolean hasSection(String... sectionNames) {
for (String sectionName : sectionNames) {
- if (machSectionsByName.get(sectionName) == null) {
+ if (findSectionByName(sectionName) == null) {
return false;
}
}