Skip to content

Commit

Permalink
#54 add elf info method (#90)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
unknown321 authored Feb 28, 2024
1 parent 5db2030 commit 79c2376
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ build/*
*.gdb_history
decomp2dbg/decompilers
decomp2dbg/d2d.py
decompilers/*/.gradle/
decompilers/*/build/
decompilers/*/dist/
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -323,4 +326,33 @@ public Map<String, Object> enums() {
this.server.plugin.enumCache = resp;
return resp;
}

public Map<String, Object> elf_info() {
if(!this.server.plugin.elfInfoCache.isEmpty())
return this.server.plugin.elfInfoCache;

Map<String, Object> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public class D2DPlugin extends ProgramPlugin implements DomainObjectListener {
public Map<String, Object> typeAliasCache;
public Map<String, Object> unionCache;
public Map<String, Object> enumCache;
public Map<String, Object> elfInfoCache;

public D2DPlugin(PluginTool tool) {
super(tool);
Expand All @@ -74,6 +75,7 @@ public D2DPlugin(PluginTool tool) {
typeAliasCache = new HashMap<>();
unionCache = new HashMap<>();
enumCache = new HashMap<>();
elfInfoCache = new HashMap<>();
}

@Override
Expand Down

0 comments on commit 79c2376

Please sign in to comment.