Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-1171 handle MIPSPro 64bit DWARF info

dev74736809849f3c59fb
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/DWARFCompilationUnit.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/DWARFCompilationUnit.java
index 1a81b35b42..dc5da87630 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/DWARFCompilationUnit.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/DWARFCompilationUnit.java
@@ -19,6 +19,7 @@ import java.io.IOException;
import java.util.*;
import ghidra.app.util.bin.BinaryReader;
+import ghidra.app.util.bin.format.dwarf4.DWARFUtil.LengthResult;
import ghidra.app.util.bin.format.dwarf4.next.DWARFProgram;
import ghidra.util.Msg;
import ghidra.util.exception.CancelledException;
@@ -126,32 +127,12 @@ public class DWARFCompilationUnit {
throws DWARFException, IOException, CancelledException {
long startOffset = debugInfoBR.getPointerIndex();
- long length = debugInfoBR.readNextUnsignedInt();
- int format;
-
- if (length == 0xffffffffL) {
- // Length of 0xffffffff implies 64-bit DWARF format
- // Mostly untested as there is no easy way to force the compiler
- // to generate this
- length = debugInfoBR.readNextLong();
- format = DWARF_64;
- }
- else if (length >= 0xfffffff0L) {
- // Length of 0xfffffff0 or greater is reserved for DWARF
- throw new DWARFException("Reserved DWARF length value: " + Long.toHexString(length) +
- ". Unknown extension.");
- }
- else if (length == 0) {
- throw new DWARFException("Invalid length 0 for DWARF Compilation Unit at 0x" +
- Long.toHexString(startOffset));
- }
- else {
- format = DWARF_32;
- }
+ LengthResult lengthInfo =
+ DWARFUtil.readLength(debugInfoBR, dwarfProgram.getGhidraProgram());
- long endOffset = (debugInfoBR.getPointerIndex() + length);
+ long endOffset = debugInfoBR.getPointerIndex() + lengthInfo.length;
short version = debugInfoBR.readNextShort();
- long abbreviationOffset = DWARFUtil.readOffsetByDWARFformat(debugInfoBR, format);
+ long abbreviationOffset = DWARFUtil.readOffsetByDWARFformat(debugInfoBR, lengthInfo.format);
byte pointerSize = debugInfoBR.readNextByte();
long firstDIEOffset = debugInfoBR.getPointerIndex();
@@ -172,9 +153,9 @@ public class DWARFCompilationUnit {
Map<Integer, DWARFAbbreviation> abbrMap =
DWARFAbbreviation.readAbbreviations(debugAbbrBR, dwarfProgram, monitor);
- DWARFCompilationUnit cu =
- new DWARFCompilationUnit(dwarfProgram, startOffset, endOffset, length, format, version,
- abbreviationOffset, pointerSize, cuNumber, firstDIEOffset, abbrMap);
+ DWARFCompilationUnit cu = new DWARFCompilationUnit(dwarfProgram, startOffset, endOffset,
+ lengthInfo.length, lengthInfo.format, version, abbreviationOffset, pointerSize,
+ cuNumber, firstDIEOffset, abbrMap);
try {
DebugInfoEntry compileUnitDIE =
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/DWARFCompileUnit.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/DWARFCompileUnit.java
index 072507ba72..718380e3f2 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/DWARFCompileUnit.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/DWARFCompileUnit.java
@@ -81,7 +81,7 @@ public class DWARFCompileUnit {
DWARFLine line = null;
if (stmt_list != null && lineReader != null) {
lineReader.setPointerIndex(stmt_list.longValue());
- line = new DWARFLine(lineReader);
+ line = new DWARFLine(lineReader, diea.getProgram().getGhidraProgram());
}
return new DWARFCompileUnit(name, producer, comp_dir, low_pc, high_pc, language, stmt_list,
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/DWARFLine.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/DWARFLine.java
index 1bed4feea9..3d23b67f29 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/DWARFLine.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/DWARFLine.java
@@ -23,6 +23,8 @@ import java.util.List;
import org.apache.commons.io.FilenameUtils;
import ghidra.app.util.bin.BinaryReader;
+import ghidra.app.util.bin.format.dwarf4.DWARFUtil.LengthResult;
+import ghidra.program.model.listing.Program;
public class DWARFLine {
private long unit_length;
@@ -40,21 +42,10 @@ public class DWARFLine {
private List<DWARFFile> file_names;
// TODO: convert this to a static factory method and a simple setter ctor
- public DWARFLine(BinaryReader reader) throws IOException, DWARFException {
- this.unit_length = reader.readNextUnsignedInt();
- // Length of 0xffffffff implies 64-bit DWARF format
- if (this.unit_length == 0xffffffffL) {
- this.unit_length = reader.readNextLong();
- this.format = DWARFCompilationUnit.DWARF_64;
- }
- // Length of 0xfffffff0 or greater is reserved for DWARF
- else if (this.unit_length >= 0xfffffff0L) {
- throw new DWARFException("Reserved DWARF length value: " +
- Long.toHexString(this.unit_length) + ". Unknown extension.");
- }
- else {
- this.format = DWARFCompilationUnit.DWARF_32;
- }
+ public DWARFLine(BinaryReader reader, Program program) throws IOException, DWARFException {
+ LengthResult lengthInfo = DWARFUtil.readLength(reader, program);
+ this.unit_length = lengthInfo.length;
+ this.format = lengthInfo.format;
// A version number for this line number information section
this.version = reader.readNextUnsignedShort();
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/DWARFUtil.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/DWARFUtil.java
index e6d877ed51..16c23fc08f 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/DWARFUtil.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/DWARFUtil.java
@@ -30,6 +30,7 @@ import ghidra.app.util.bin.format.dwarf4.encoding.DWARFTag;
import ghidra.app.util.bin.format.dwarf4.next.DWARFProgram;
import ghidra.program.model.data.DataType;
import ghidra.program.model.data.DataTypeComponent;
+import ghidra.program.model.listing.Program;
import ghidra.program.model.symbol.SymbolType;
import ghidra.util.Conv;
@@ -567,4 +568,64 @@ public class DWARFUtil {
return (referers.size() == 1) ? referers.get(0) : null;
}
+ public static class LengthResult {
+ public final long length;
+ public final int format; // either DWARF_32 or DWARF_64
+
+ private LengthResult(long length, int format) {
+ this.length = length;
+ this.format = format;
+ }
+ }
+
+ /**
+ * Read a variable-length length value from the stream.
+ * <p>
+ *
+ * @param reader {@link BinaryReader} stream to read from
+ * @param program Ghidra {@link Program}
+ * @return new {@link LengthResult}, never null
+ * @throws IOException if io error
+ * @throws DWARFException if invalid values
+ */
+ public static LengthResult readLength(BinaryReader reader, Program program)
+ throws IOException, DWARFException {
+ long startOffset = reader.getPointerIndex();
+ long length = reader.readNextUnsignedInt();
+ int format;
+
+ if (length == 0xffffffffL) {
+ // Length of 0xffffffff implies 64-bit DWARF format
+ // Mostly untested as there is no easy way to force the compiler
+ // to generate this
+ length = reader.readNextLong();
+ format = DWARFCompilationUnit.DWARF_64;
+ }
+ else if (length >= 0xfffffff0L) {
+ // Length of 0xfffffff0 or greater is reserved for DWARF
+ throw new DWARFException("Reserved DWARF length value: " + Long.toHexString(length) +
+ ". Unknown extension.");
+ }
+ else if (length == 0) {
+ // Test for special case of weird BE MIPS 64bit length value.
+ // Instead of following DWARF std (a few lines above with length == MAX_INT),
+ // it writes a raw 64bit long (BE). The upper 32 bits (already read as length) will
+ // always be 0 since super-large binaries from that system weren't really possible.
+ // The next 32 bits will be the remainder of the value.
+ if ( reader.isBigEndian() && program.getDefaultPointerSize() == 8) {
+ length = reader.readNextUnsignedInt();
+ format = DWARFCompilationUnit.DWARF_64;
+ }
+ else {
+ throw new DWARFException(
+ "Invalid DWARF length 0 at 0x" + Long.toHexString(startOffset));
+ }
+ }
+ else {
+ format = DWARFCompilationUnit.DWARF_32;
+ }
+
+ return new LengthResult(length, format);
+ }
+
}