Skip to content

Commit

Permalink
Report a "symbol duplication" error for LTO
Browse files Browse the repository at this point in the history
Fixes #1414
  • Loading branch information
rui314 committed Feb 17, 2025
1 parent b82028d commit 9306f69
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/passes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -327,9 +327,7 @@ void resolve_symbols(Context<E> &ctx) {

if (!flag)
return;

clear_symbols(ctx);
resolve_symbols(ctx);
}
}

Expand All @@ -346,6 +344,12 @@ void do_lto(Context<E> &ctx) {
parse_symbol_version(ctx);
compute_import_export(ctx);

// If multiple IR object files define the same symbol, the LTO backend
// would choose one of them randomly instead of reporting an error.
// So we need to check for symbol duplication error before doing an LTO.
if (!ctx.arg.allow_multiple_definition)
check_duplicate_symbols(ctx);

// Invoke the LTO plugin. This step compiles IR object files into a few
// big ELF files.
std::vector<ObjectFile<E> *> lto_objs = run_lto_plugin(ctx);
Expand Down Expand Up @@ -1037,7 +1041,7 @@ void check_duplicate_symbols(Context<E> &ctx) {
Symbol<E> &sym = *file->symbols[i];

// Skip if our symbol is undef or weak
if (sym.file == file || sym.file == ctx.internal_obj ||
if (!sym.file || sym.file == file || sym.file == ctx.internal_obj ||
esym.is_undef() || esym.is_common() || (esym.st_bind == STB_WEAK))
continue;

Expand Down
16 changes: 16 additions & 0 deletions test/duplicate-error-lto.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash
. $(dirname $0)/common.inc

nm mold | grep '__tsan_init' && skip
test_cflags -flto || skip

cat <<EOF | $CC -o $t/a.o -c -xc - -flto
void foo() {}
EOF

cat <<EOF | $CC -o $t/b.o -c -xc - -flto
int main() {}
EOF

not $CC -B. -o $t/exe1 $t/a.o $t/a.o $t/b.o -flto |&
grep 'duplicate symbol.*: foo$'

0 comments on commit 9306f69

Please sign in to comment.