From 19b1b0252c0022567f24fc652eda8697c725a793 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Sat, 2 Oct 2021 17:47:25 +0900 Subject: [PATCH] [Mach-O] Dump export table entries --- macho/dumper.cc | 31 +++++++++++++++++++++++++++++++ mold.h | 10 ++++++++++ 2 files changed, 41 insertions(+) diff --git a/macho/dumper.cc b/macho/dumper.cc index 8c119aeb..4a150714 100644 --- a/macho/dumper.cc +++ b/macho/dumper.cc @@ -43,6 +43,32 @@ static void print_bytes(u8 *buf, i64 size) { std::cout << "]\n"; } +struct ExportEntry { + std::string name; + u32 flags; + u64 addr; +}; + +static void read_trie(std::vector &vec, u8 *start, i64 offset = 0, + const std::string &prefix = "") { + u8 *buf = start + offset; + + if (read_uleb(buf)) { + ExportEntry ent; + ent.name = prefix; + ent.flags = read_uleb(buf); + ent.addr = read_uleb(buf); + vec.push_back(std::move(ent)); + } + + for (i64 i = 0, n = read_uleb(buf); i < n; i++) { + std::string suffix((char *)buf); + buf += suffix.size() + 1; + i64 off = read_uleb(buf); + read_trie(vec, start, off, prefix + suffix); + } +} + void dump_file(std::string path) { u8 *buf = open_file(path); @@ -255,6 +281,11 @@ void dump_file(std::string path) { std::cout << " export_off: 0x" << std::hex << cmd.export_off << "\n export_size: 0x" << cmd.export_size << "\n"; + + std::vector vec; + read_trie(vec, buf + cmd.export_off); + for (ExportEntry &ent : vec) + std::cout << " export_sym: " << ent.name << " 0x" << ent.addr << "\n"; } break; } diff --git a/mold.h b/mold.h index 8fdd11ef..963c8d22 100644 --- a/mold.h +++ b/mold.h @@ -229,6 +229,16 @@ inline i64 write_uleb(u8 *buf, u64 val) { return i; } +inline u64 read_uleb(u8 *&buf) { + u64 val = 0; + u8 byte; + do { + byte = *buf++; + val = (val << 7) | (byte & 0x7f); + } while (byte & 0x80); + return val; +} + inline i64 uleb_size(u64 val) { i64 i = 0; do {