Skip to content

Ghidra is a software reverse engineering (SRE) framework

Merge remote-tracking branch 'origin/GP-1592_dev747368_gnu_debugdata--SQUASHED'

ghidra17ed911569a33
diff --cc Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/ElfProgramBuilder.java
index f5d2043723,f331bf6991..1df8f31230
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/ElfProgramBuilder.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/ElfProgramBuilder.java
@@@ -15,14 -15,17 +15,16 @@@
*/
package ghidra.app.util.opinion;
- import java.io.IOException;
- import java.io.InputStream;
-import java.util.*;
-
+ import java.io.*;
import java.math.BigInteger;
+ import java.nio.file.AccessMode;
import java.text.NumberFormat;
+import java.util.*;
+ import org.apache.commons.compress.compressors.xz.XZCompressorInputStream;
import org.apache.commons.lang3.StringUtils;
+ import generic.continues.GenericFactory;
import ghidra.app.cmd.label.SetLabelPrimaryCmd;
import ghidra.app.util.MemoryBlockUtils;
import ghidra.app.util.Option;
@@@ -1390,6 -1365,60 +1397,61 @@@ class ElfProgramBuilder extends MemoryS
allocateUndefinedSymbolData(dataAllocationMap);
}
+ /**
+ * Returns any symbol tables that are embedded in the ".gnu_debugdata" section.
+ * <p>
+ * The ".gnu_debugdata" section contains a xz compressed minimal ELF file that has symbols that
+ * have been stripped from this binary.
+ *
+ * @param monitor checked for cancelation when copying data
+ * @return list of ElfSymbolTables, empty if not present
+ */
+ private List<ElfSymbolTable> getGnuDebugDataSymbolTables(TaskMonitor monitor) {
+ ElfSectionHeader debugDataSection = elf.getSection(".gnu_debugdata");
+ Address debugDataAddr = findLoadAddress(debugDataSection, 0);
+ if (debugDataAddr != null) {
+ try {
+ File tmpFile = File.createTempFile("ghidra_gnu_debugdata", null);
+ try (ByteProviderWrapper compressedDebugDataBP = new ByteProviderWrapper(
+ new MemoryByteProvider(memory, debugDataAddr), 0, debugDataSection.getSize());
+ XZCompressorInputStream xzIS =
+ new XZCompressorInputStream(compressedDebugDataBP.getInputStream(0));
+ ObfuscatedOutputStream oos =
+ new ObfuscatedOutputStream(new FileOutputStream(tmpFile));) {
+
+ FileUtilities.copyStreamToStream(xzIS, oos, monitor);
+ oos.close();
+
+ try (ByteProvider debugDataBP =
+ new ObfuscatedFileByteProvider(tmpFile, null, AccessMode.READ)) {
+
+ GenericFactory factory = MessageLogContinuesFactory.create(log);
- ElfHeader minidebugElf = ElfHeader.createElfHeader(factory, debugDataBP);
++ ElfHeader minidebugElf =
++ ElfHeader.createElfHeader(factory, debugDataBP, null);
+ minidebugElf.parse();
+
+ ElfSymbolTable[] minidebugSymbolTables = minidebugElf.getSymbolTables();
+ int debugSymbolsCount = 0;
+ for (ElfSymbolTable symTable : minidebugSymbolTables) {
+ debugSymbolsCount += symTable.getSymbols().length;
+ }
+ log(String.format("Found %d symbols in .gnu_debugdata", debugSymbolsCount));
+
+ return List.of(minidebugSymbolTables);
+ }
+ }
+ finally {
+ tmpFile.delete();
+ }
+ }
+ catch (IOException | ElfException e) {
+ log("Error extracting .gnu_debugdata section embedded symbols.");
+ Msg.error(this, "Error extracting .gnu_debugdata section embedded symbols.", e);
+ }
+ }
+ return List.of();
+ }
+
private void processSymbols(ElfSymbol[] symbols, HashMap<Address, Integer> dataAllocationMap,
TaskMonitor monitor) throws CancelledException {
for (ElfSymbol elfSymbol : symbols) {