From 1e8cee85bb6a93b8a91255e506f5ea47febe5be9 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Fri, 3 Jan 2025 16:27:04 +0900 Subject: [PATCH] Name each thunk symbol unique name Previously, if symbol `foo` appears more than once in multiple thunks, they got the same name `foo$thunk`. Now, they are named `foo$thunk0`, etc. --- src/mold.h | 1 + src/output-chunks.cc | 5 +++-- src/passes.cc | 8 ++++++++ test/arch-aarch64-range-extension-thunk-disassembly.sh | 2 +- test/arch-arm-range-extension-thunk-disassembly.sh | 2 +- 5 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/mold.h b/src/mold.h index c1d38f10ad..ea916adcee 100644 --- a/src/mold.h +++ b/src/mold.h @@ -117,6 +117,7 @@ class Thunk { OutputSection &output_section; i64 offset; std::vector *> symbols; + std::string name; }; template diff --git a/src/output-chunks.cc b/src/output-chunks.cc index a1878bc3f6..f8458dadd6 100644 --- a/src/output-chunks.cc +++ b/src/output-chunks.cc @@ -1185,7 +1185,7 @@ void OutputSection::compute_symtab_size(Context &ctx) { this->num_local_symtab += thunk->symbols.size(); for (Symbol *sym : thunk->symbols) - this->strtab_size += sym->name().size() + sizeof("$thunk"); + this->strtab_size += sym->name().size() + thunk->name.size() + 2; } } } @@ -1219,7 +1219,8 @@ void OutputSection::populate_symtab(Context &ctx) { write_esym(addr, strtab - strtab_base); strtab += write_string(strtab, sym.name()) - 1; - strtab += write_string(strtab, "$thunk"); + *strtab++ = '$'; + strtab += write_string(strtab, thunk->name); // Emit "$t", "$a" and "$d" if ARM32. if constexpr (is_arm32) { diff --git a/src/passes.cc b/src/passes.cc index 0a8fdcbfd3..b6bf7b8fd5 100644 --- a/src/passes.cc +++ b/src/passes.cc @@ -1754,6 +1754,14 @@ template void create_output_symtab(Context &ctx) { Timer t(ctx, "compute_symtab_size"); + if constexpr (needs_thunk) { + i64 n = 0; + for (Chunk *chunk : ctx.chunks) + if (OutputSection *osec = chunk->to_osec()) + for (std::unique_ptr> &thunk : osec->thunks) + thunk->name = "thunk" + std::to_string(n++); + } + tbb::parallel_for_each(ctx.chunks, [&](Chunk *chunk) { chunk->compute_symtab_size(ctx); }); diff --git a/test/arch-aarch64-range-extension-thunk-disassembly.sh b/test/arch-aarch64-range-extension-thunk-disassembly.sh index 4c7c5fce12..b31fc2a3ba 100755 --- a/test/arch-aarch64-range-extension-thunk-disassembly.sh +++ b/test/arch-aarch64-range-extension-thunk-disassembly.sh @@ -18,4 +18,4 @@ EOF $CC -B. -o $t/exe $t/a.o \ -Wl,--section-start=.low=0x10000000,--section-start=.high=0x20000000 -$OBJDUMP -dr $t/exe | grep -Fq ':' +$OBJDUMP -dr $t/exe | grep -Eq ':' diff --git a/test/arch-arm-range-extension-thunk-disassembly.sh b/test/arch-arm-range-extension-thunk-disassembly.sh index b1f28d793e..28d614a51b 100755 --- a/test/arch-arm-range-extension-thunk-disassembly.sh +++ b/test/arch-arm-range-extension-thunk-disassembly.sh @@ -18,7 +18,7 @@ EOF $CC -B. -o $t/exe $t/a.o \ -Wl,--section-start=.low=0x10000000,--section-start=.high=0x20000000 -$OBJDUMP -dr $t/exe | grep -F -A7 ':' > $t/log +$OBJDUMP -dr $t/exe | grep -E -A7 ':' > $t/log grep -Eq 'bx\s+pc' $t/log grep -Eq 'add\s+pc, ip, pc' $t/log