Skip to content

Commit

Permalink
Name each thunk symbol unique name
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rui314 committed Jan 3, 2025
1 parent 2352ec6 commit 1e8cee8
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/mold.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ class Thunk<E> {
OutputSection<E> &output_section;
i64 offset;
std::vector<Symbol<E> *> symbols;
std::string name;
};

template <needs_thunk E>
Expand Down
5 changes: 3 additions & 2 deletions src/output-chunks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1185,7 +1185,7 @@ void OutputSection<E>::compute_symtab_size(Context<E> &ctx) {
this->num_local_symtab += thunk->symbols.size();

for (Symbol<E> *sym : thunk->symbols)
this->strtab_size += sym->name().size() + sizeof("$thunk");
this->strtab_size += sym->name().size() + thunk->name.size() + 2;
}
}
}
Expand Down Expand Up @@ -1219,7 +1219,8 @@ void OutputSection<E>::populate_symtab(Context<E> &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<E>) {
Expand Down
8 changes: 8 additions & 0 deletions src/passes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1754,6 +1754,14 @@ template <typename E>
void create_output_symtab(Context<E> &ctx) {
Timer t(ctx, "compute_symtab_size");

if constexpr (needs_thunk<E>) {
i64 n = 0;
for (Chunk<E> *chunk : ctx.chunks)
if (OutputSection<E> *osec = chunk->to_osec())
for (std::unique_ptr<Thunk<E>> &thunk : osec->thunks)
thunk->name = "thunk" + std::to_string(n++);
}

tbb::parallel_for_each(ctx.chunks, [&](Chunk<E> *chunk) {
chunk->compute_symtab_size(ctx);
});
Expand Down
2 changes: 1 addition & 1 deletion test/arch-aarch64-range-extension-thunk-disassembly.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<fn1$thunk>:'
$OBJDUMP -dr $t/exe | grep -Eq '<fn1\$thunk[0-9]+>:'
2 changes: 1 addition & 1 deletion test/arch-arm-range-extension-thunk-disassembly.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<fn1$thunk>:' > $t/log
$OBJDUMP -dr $t/exe | grep -E -A7 '<fn1\$thunk[0-9]+>:' > $t/log

grep -Eq 'bx\s+pc' $t/log
grep -Eq 'add\s+pc, ip, pc' $t/log

0 comments on commit 1e8cee8

Please sign in to comment.