From 46d3e1294631f278843b5b8ac736e67a5d9df3b5 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Tue, 5 Oct 2021 19:25:44 +0900 Subject: [PATCH] [Mach-O] wip --- macho/input-sections.cc | 10 +++++----- macho/mold.h | 2 +- macho/object-file.cc | 6 +++++- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/macho/input-sections.cc b/macho/input-sections.cc index fae4af7e..c3be4e19 100644 --- a/macho/input-sections.cc +++ b/macho/input-sections.cc @@ -26,11 +26,11 @@ Subsection *InputSection::find_subsection(Context &ctx, u32 addr) { return &*(it - 1); } -static u64 read_addend(u8 *buf, u32 offset, u32 p2size) { +static i64 read_addend(u8 *buf, u32 offset, u32 p2size) { switch (p2size) { - case 0: return buf[offset]; - case 1: return *(u16 *)(buf + offset); - case 2: return *(u32 *)(buf + offset); + case 0: return *(i8 *)(buf + offset); + case 1: return *(i16 *)(buf + offset); + case 2: return *(i32 *)(buf + offset); case 3: return *(i64 *)(buf + offset); } unreachable(); @@ -42,7 +42,7 @@ void InputSection::parse_relocations(Context &ctx) { MachRel *rel = (MachRel *)(file.mf->data + hdr.reloff); for (i64 i = 0; i < hdr.nreloc; i++) { MachRel &r = rel[i]; - u64 addend = read_addend((u8 *)contents.data(), r.offset, r.p2size); + i64 addend = read_addend((u8 *)contents.data(), r.offset, r.p2size); if (r.is_extern) { rels.push_back({r.offset, addend, file.syms[r.idx], nullptr}); diff --git a/macho/mold.h b/macho/mold.h index 1c92fbb1..de2f1ccc 100644 --- a/macho/mold.h +++ b/macho/mold.h @@ -42,7 +42,7 @@ std::ostream &operator<<(std::ostream &out, const ObjectFile &file); struct Relocation { u32 offset = 0; - u64 addend = 0; + i64 addend = 0; Symbol *sym = nullptr; Subsection *subsec = nullptr; }; diff --git a/macho/object-file.cc b/macho/object-file.cc index f612b17a..2c892057 100644 --- a/macho/object-file.cc +++ b/macho/object-file.cc @@ -54,8 +54,12 @@ void ObjectFile::parse(Context &ctx) { p += lc.cmdsize; } - for (std::unique_ptr &sec : sections) + for (std::unique_ptr &sec : sections) { sec->parse_relocations(ctx); + for (Relocation &rel : sec->rels) + SyncOut(ctx) << *sec << ": " << rel.offset << " " << rel.addend + << " " << rel.sym << " " << rel.subsec; + } } } // namespace mold::macho