From d23183ed318c560e46f932cde33a8e90d74524b9 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Fri, 3 Dec 2021 15:54:18 +0900 Subject: [PATCH] wip --- macho/mold.h | 19 ++++--------------- macho/output-chunks.cc | 29 +++++++++++++---------------- 2 files changed, 17 insertions(+), 31 deletions(-) diff --git a/macho/mold.h b/macho/mold.h index e9c27ead..8589dec8 100644 --- a/macho/mold.h +++ b/macho/mold.h @@ -553,14 +553,12 @@ public: this->hdr.p2align = __builtin_ctz(2); this->hdr.type = S_SYMBOL_STUBS; this->hdr.attr = S_ATTR_SOME_INSTRUCTIONS | S_ATTR_PURE_INSTRUCTIONS; - this->hdr.reserved2 = ENTRY_SIZE; + this->hdr.reserved2 = E::stub_size; } void add(Context &ctx, Symbol *sym); void copy_buf(Context &ctx) override; - static constexpr i64 ENTRY_SIZE = 6; - std::vector *> syms; std::vector bind_offsets; }; @@ -575,9 +573,6 @@ public: } void copy_buf(Context &ctx) override; - - static constexpr i64 HEADER_SIZE = 16; - static constexpr i64 ENTRY_SIZE = 10; }; template @@ -620,8 +615,6 @@ public: void copy_buf(Context &ctx) override; std::vector *> syms; - - static constexpr i64 ENTRY_SIZE = 8; }; template @@ -634,8 +627,6 @@ public: } void copy_buf(Context &ctx) override; - - static constexpr i64 ENTRY_SIZE = 8; }; template @@ -651,8 +642,6 @@ public: void copy_buf(Context &ctx) override; std::vector *> syms; - - static constexpr i64 ENTRY_SIZE = 8; }; // @@ -879,20 +868,20 @@ u64 Symbol::get_addr(Context &ctx) const { if (subsec) return subsec->get_addr(ctx) + value; if (stub_idx != -1) - return ctx.stubs.hdr.addr + stub_idx * StubsSection::ENTRY_SIZE; + return ctx.stubs.hdr.addr + stub_idx * E::stub_size; return value; } template u64 Symbol::get_got_addr(Context &ctx) const { assert(got_idx != -1); - return ctx.got.hdr.addr + got_idx * GotSection::ENTRY_SIZE; + return ctx.got.hdr.addr + got_idx * E::wordsize; } template u64 Symbol::get_tlv_addr(Context &ctx) const { assert(tlv_idx != -1); - return ctx.thread_ptrs.hdr.addr + tlv_idx * ThreadPtrsSection::ENTRY_SIZE; + return ctx.thread_ptrs.hdr.addr + tlv_idx * E::wordsize; } template diff --git a/macho/output-chunks.cc b/macho/output-chunks.cc index 22b4c22e..68768137 100644 --- a/macho/output-chunks.cc +++ b/macho/output-chunks.cc @@ -563,7 +563,7 @@ void OutputRebaseSection::compute_size(Context &ctx) { for (i64 i = 0; i < ctx.stubs.syms.size(); i++) enc.add(ctx.data_seg->seg_idx, - ctx.lazy_symbol_ptr.hdr.addr + i * LazySymbolPtrSection::ENTRY_SIZE - + ctx.lazy_symbol_ptr.hdr.addr + i * E::wordsize - ctx.data_seg->cmd.vmaddr); for (Symbol *sym : ctx.got.syms) @@ -686,8 +686,7 @@ void OutputLazyBindSection::add(Context &ctx, Symbol &sym, i64 flags) { i64 seg_idx = ctx.data_seg->seg_idx; emit(BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB | seg_idx); - i64 offset = ctx.lazy_symbol_ptr.hdr.addr + - sym.stub_idx * LazySymbolPtrSection::ENTRY_SIZE - + i64 offset = ctx.lazy_symbol_ptr.hdr.addr + sym.stub_idx * E::wordsize - ctx.data_seg->cmd.vmaddr; encode_uleb(contents, offset); @@ -1065,11 +1064,11 @@ void StubsSection::add(Context &ctx, Symbol *sym) { syms.push_back(sym); i64 nsyms = syms.size(); - this->hdr.size = nsyms * ENTRY_SIZE; + this->hdr.size = nsyms * E::stub_size; ctx.stub_helper.hdr.size = - StubHelperSection::HEADER_SIZE + nsyms * StubHelperSection::ENTRY_SIZE; - ctx.lazy_symbol_ptr.hdr.size = nsyms * LazySymbolPtrSection::ENTRY_SIZE; + E::stub_helper_hdr_size + nsyms * E::stub_helper_size; + ctx.lazy_symbol_ptr.hdr.size = nsyms * E::wordsize; } template <> @@ -1080,12 +1079,11 @@ void StubsSection::copy_buf(Context &ctx) { // `ff 25 xx xx xx xx` is a RIP-relative indirect jump instruction, // i.e., `jmp *IMM(%rip)`. It loads an address from la_symbol_ptr // and jump there. - assert(ENTRY_SIZE == 6); + assert(ARM64::stub_size == 6); buf[i * 6] = 0xff; buf[i * 6 + 1] = 0x25; *(u32 *)(buf + i * 6 + 2) = - ctx.lazy_symbol_ptr.hdr.addr + - i * LazySymbolPtrSection::ENTRY_SIZE - + ctx.lazy_symbol_ptr.hdr.addr + i * ARM64::wordsize - (this->hdr.addr + i * 6 + 6); } } @@ -1098,12 +1096,11 @@ void StubsSection::copy_buf(Context &ctx) { // `ff 25 xx xx xx xx` is a RIP-relative indirect jump instruction, // i.e., `jmp *IMM(%rip)`. It loads an address from la_symbol_ptr // and jump there. - assert(ENTRY_SIZE == 6); + assert(X86_64::stub_size == 6); buf[i * 6] = 0xff; buf[i * 6 + 1] = 0x25; *(u32 *)(buf + i * 6 + 2) = - ctx.lazy_symbol_ptr.hdr.addr + - i * LazySymbolPtrSection::ENTRY_SIZE - + ctx.lazy_symbol_ptr.hdr.addr + i * X86_64::wordsize - (this->hdr.addr + i * 6 + 6); } } @@ -1337,7 +1334,7 @@ void GotSection::add(Context &ctx, Symbol *sym) { assert(sym->got_idx == -1); sym->got_idx = syms.size(); syms.push_back(sym); - this->hdr.size = syms.size() * ENTRY_SIZE; + this->hdr.size = syms.size() * E::wordsize; } template @@ -1353,8 +1350,8 @@ void LazySymbolPtrSection::copy_buf(Context &ctx) { u64 *buf = (u64 *)(ctx.buf + this->hdr.offset); for (i64 i = 0; i < ctx.stubs.syms.size(); i++) - buf[i] = ctx.stub_helper.hdr.addr + StubHelperSection::HEADER_SIZE + - i * StubHelperSection::ENTRY_SIZE; + buf[i] = ctx.stub_helper.hdr.addr + E::stub_helper_hdr_size + + i * E::stub_helper_size; } template @@ -1362,7 +1359,7 @@ void ThreadPtrsSection::add(Context &ctx, Symbol *sym) { assert(sym->tlv_idx == -1); sym->tlv_idx = syms.size(); syms.push_back(sym); - this->hdr.size = syms.size() * ENTRY_SIZE; + this->hdr.size = syms.size() * E::wordsize; } template