From f9f61cdf2aa0faa41f84c3965d5bb548141fa879 Mon Sep 17 00:00:00 2001 From: James Benton Date: Wed, 2 Oct 2019 19:50:05 +0100 Subject: [PATCH] Create symbols for data exports. --- .../java/cafeloader/Cafe_ElfExtension.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/main/java/cafeloader/Cafe_ElfExtension.java b/src/main/java/cafeloader/Cafe_ElfExtension.java index 5a7ad4d..619a7a3 100644 --- a/src/main/java/cafeloader/Cafe_ElfExtension.java +++ b/src/main/java/cafeloader/Cafe_ElfExtension.java @@ -1,5 +1,8 @@ package cafeloader; +import java.io.IOException; + +import ghidra.app.util.bin.BinaryReader; import ghidra.app.util.bin.format.elf.*; import ghidra.app.util.bin.format.elf.extend.*; import ghidra.program.model.address.*; @@ -109,10 +112,43 @@ public class Cafe_ElfExtension extends ElfExtension { processRplCrcs(elfLoadHelper, sectionHeader); } else if (headertype == SHT_RPL_FILEINFO.value) { processRplFileInfo(elfLoadHelper, sectionHeader); + } else if (headertype == SHT_RPL_EXPORTS.value) { + processRplExports(elfLoadHelper, sectionHeader); } } } + private void processRplExports(ElfLoadHelper elfLoadHelper, ElfSectionHeader sectionHeader) { + String sectionName = sectionHeader.getNameAsString(); + Address loadAddress = elfLoadHelper.findLoadAddress(sectionHeader, 0); + Memory memory = elfLoadHelper.getProgram().getMemory(); + boolean isDataExports = sectionName.contentEquals(".dexports"); + if (!isDataExports) { + // Function exports are already in symbol table + return; + } + + // Create symbols for data exports + BinaryReader reader = elfLoadHelper.getElfHeader().getReader(); + reader.setPointerIndex(sectionHeader.getOffset()); + + try { + int count = reader.readNextInt(); + int signature = reader.readNextInt(); + for (int i = 0; i < count; ++i) { + int value = reader.readNextInt(); + int nameOffset = reader.readNextInt(); + boolean isTlsExport = (nameOffset & 0x80000000) != 0; + String name = reader.readAsciiString(sectionHeader.getOffset() + (nameOffset & 0x7FFFFFFF)); + elfLoadHelper.createSymbol(elfLoadHelper.getDefaultAddress(value), name, true, false, null); + } + } catch (IOException e) { + e.printStackTrace(); + } catch (InvalidInputException e) { + e.printStackTrace(); + } + } + private void processRplCrcs(ElfLoadHelper elfLoadHelper, ElfSectionHeader sectionHeader) { Address address = elfLoadHelper.findLoadAddress(sectionHeader, 0); if (address == null) {