From eba14fb8f51f3f036ed1b827ec3b138358875dfe Mon Sep 17 00:00:00 2001 From: Maschell Date: Sun, 10 Mar 2019 17:28:48 +0100 Subject: [PATCH] Get the sections header names before converting the rpl/rpx to elf --- src/main/java/de/mas/ghidra/utils/Utils.java | 14 +++++++++++ .../java/de/mas/ghidra/wiiu/RPXUtils.java | 23 +++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/main/java/de/mas/ghidra/utils/Utils.java b/src/main/java/de/mas/ghidra/utils/Utils.java index e3d57d7..48b488f 100644 --- a/src/main/java/de/mas/ghidra/utils/Utils.java +++ b/src/main/java/de/mas/ghidra/utils/Utils.java @@ -24,4 +24,18 @@ public class Utils { } return buffer; } + + public static String stringFromStringTable(byte[] stringTable, int index) { + ByteBuffer buf = ByteBuffer.wrap(stringTable); + + int pos = index; + + StringBuilder result = new StringBuilder(); + + for (byte b; (b = buf.get(pos)) != 0; pos++) { + result.append((char) b); + } + + return result.toString(); + } } diff --git a/src/main/java/de/mas/ghidra/wiiu/RPXUtils.java b/src/main/java/de/mas/ghidra/wiiu/RPXUtils.java index 9726d7c..151f53d 100644 --- a/src/main/java/de/mas/ghidra/wiiu/RPXUtils.java +++ b/src/main/java/de/mas/ghidra/wiiu/RPXUtils.java @@ -38,6 +38,28 @@ public class RPXUtils { long shdr_elf_offset = elfFile.e_ehsize() & 0xFFFFFFFF; long shdr_data_elf_offset = shdr_elf_offset + elfFile.e_shnum() * elfFile.e_shentsize(); + // Let's get / decompress the section header string table at first. + ElfSectionHeader sh_str_sh = elfFile.getSections()[elfFile.e_shstrndx()]; + byte[] sh_str_sh_data = new byte[0]; + if (sh_str_sh.getOffset() != 0) { + if ((sh_str_sh.getFlags() & SHT_NOBITS) != SHT_NOBITS) { + sh_str_sh_data = sh_str_sh.getData(); + if ((sh_str_sh.getFlags() & SHF_RPL_ZLIB) == SHF_RPL_ZLIB) { + long section_size_inflated = ByteBuffer.wrap(Arrays.copyOf(sh_str_sh_data, 4)).getInt() + & 0xFFFFFFFF; + Inflater inflater = new Inflater(); + inflater.setInput(sh_str_sh_data, 4, (int) sh_str_sh.getSize() - 4); // the first byte is the size + + byte[] decompressed = new byte[(int) section_size_inflated]; + + inflater.inflate(decompressed); + inflater.end(); + + sh_str_sh_data = decompressed; + } + } + } + long curSymbolAddress = 0x01000000; for (ElfSectionHeader h : elfFile.getSections()) { @@ -45,6 +67,7 @@ public class RPXUtils { long curSize = h.getSize(); long flags = h.getFlags(); long offset = h.getOffset(); + String sectionName = Utils.stringFromStringTable(sh_str_sh_data, h.getName()); if (offset != 0) { if ((flags & SHT_NOBITS) != SHT_NOBITS) {