From 79c23764dd557bf9b0a6fd11ca9c84c60d2a6691 Mon Sep 17 00:00:00 2001 From: u Date: Wed, 28 Feb 2024 05:29:35 +0300 Subject: [PATCH] #54 add elf info method (#90) This method provides info required to build ELF file accepted by gdb: - machine id - endianness - bit size It also provides: - image base offset to rebase symbols to their original offsets - ELF flags (not mandatory) - program name --- .gitignore | 3 ++ .../java/decomp2dbg/D2DGhidraServerAPI.java | 32 +++++++++++++++++++ .../src/main/java/decomp2dbg/D2DPlugin.java | 2 ++ 3 files changed, 37 insertions(+) diff --git a/.gitignore b/.gitignore index 99225cf..abdad38 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,6 @@ build/* *.gdb_history decomp2dbg/decompilers decomp2dbg/d2d.py +decompilers/*/.gradle/ +decompilers/*/build/ +decompilers/*/dist/ diff --git a/decompilers/d2d_ghidra/src/main/java/decomp2dbg/D2DGhidraServerAPI.java b/decompilers/d2d_ghidra/src/main/java/decomp2dbg/D2DGhidraServerAPI.java index 0fd4b95..a497f47 100644 --- a/decompilers/d2d_ghidra/src/main/java/decomp2dbg/D2DGhidraServerAPI.java +++ b/decompilers/d2d_ghidra/src/main/java/decomp2dbg/D2DGhidraServerAPI.java @@ -6,6 +6,9 @@ import ghidra.app.decompiler.ClangLine; import ghidra.app.decompiler.PrettyPrinter; +import ghidra.app.util.bin.MemoryByteProvider; +import ghidra.app.util.bin.format.elf.ElfException; +import ghidra.app.util.bin.format.elf.ElfHeader; import ghidra.program.model.listing.Function; import ghidra.program.model.symbol.Symbol; import ghidra.program.model.symbol.SymbolType; @@ -323,4 +326,33 @@ public Map enums() { this.server.plugin.enumCache = resp; return resp; } + + public Map elf_info() { + if(!this.server.plugin.elfInfoCache.isEmpty()) + return this.server.plugin.elfInfoCache; + + Map elf_info = new HashMap<>(); + + var program = this.server.plugin.getCurrentProgram(); + var provider = new MemoryByteProvider(program.getMemory(), program.getMinAddress()); + ElfHeader header = null; + try { + header = new ElfHeader(provider, null); + } + catch (ElfException e) { + elf_info.put("error", e.toString()); + return elf_info; + } + + elf_info.put("flags", "0x" + Integer.toHexString(header.e_flags())); + elf_info.put("machine", (int) header.e_machine()); + elf_info.put("is_big_endian", header.isBigEndian()); + elf_info.put("is_32_bit", header.is32Bit()); + elf_info.put("image_base", "0x" + Long.toHexString(program.getMinAddress().getOffset())); + elf_info.put("name", program.getName()); + elf_info.put("error", ""); + + this.server.plugin.elfInfoCache = elf_info; + return elf_info; + } } diff --git a/decompilers/d2d_ghidra/src/main/java/decomp2dbg/D2DPlugin.java b/decompilers/d2d_ghidra/src/main/java/decomp2dbg/D2DPlugin.java index 6569d73..77083ef 100644 --- a/decompilers/d2d_ghidra/src/main/java/decomp2dbg/D2DPlugin.java +++ b/decompilers/d2d_ghidra/src/main/java/decomp2dbg/D2DPlugin.java @@ -57,6 +57,7 @@ public class D2DPlugin extends ProgramPlugin implements DomainObjectListener { public Map typeAliasCache; public Map unionCache; public Map enumCache; + public Map elfInfoCache; public D2DPlugin(PluginTool tool) { super(tool); @@ -74,6 +75,7 @@ public D2DPlugin(PluginTool tool) { typeAliasCache = new HashMap<>(); unionCache = new HashMap<>(); enumCache = new HashMap<>(); + elfInfoCache = new HashMap<>(); } @Override