1
1
mirror of https://github.com/rui314/mold.git synced 2024-11-10 00:59:38 +03:00

Do not crash if defsym'ed symbol didn't exist

Fixes https://github.com/rui314/mold/issues/1108
This commit is contained in:
Rui Ueyama 2023-11-05 16:15:28 +09:00
parent 0804fbd095
commit 5c7a75b3e4
2 changed files with 19 additions and 9 deletions

View File

@ -645,6 +645,7 @@ void create_internal_file(Context<E> &ctx) {
auto add = [&](Symbol<E> *sym, bool is_undef) { auto add = [&](Symbol<E> *sym, bool is_undef) {
obj->symbols.push_back(sym); obj->symbols.push_back(sym);
sym->sym_idx = ctx.internal_esyms.size();
// An actual value will be set to a linker-synthesized symbol by // An actual value will be set to a linker-synthesized symbol by
// fix_synthetic_symbols(). Until then, `value` doesn't have a valid // fix_synthetic_symbols(). Until then, `value` doesn't have a valid
@ -812,20 +813,20 @@ void add_synthetic_symbols(Context<E> &ctx) {
Symbol<E> *sym = ctx.arg.defsyms[i].first; Symbol<E> *sym = ctx.arg.defsyms[i].first;
std::variant<Symbol<E> *, u64> val = ctx.arg.defsyms[i].second; std::variant<Symbol<E> *, u64> val = ctx.arg.defsyms[i].second;
Symbol<E> *target = nullptr; Symbol<E> *sym2 = nullptr;
if (Symbol<E> **ref = std::get_if<Symbol<E> *>(&val)) if (Symbol<E> **ref = std::get_if<Symbol<E> *>(&val))
target = *ref; sym2 = *ref;
// If the alias refers another symobl, copy ELF symbol attributes. // If the alias refers another symobl, copy ELF symbol attributes.
if (target) { if (sym2 && sym2->file) {
ElfSym<E> &esym = obj.elf_syms[i + 1]; ElfSym<E> &esym = (ElfSym<E> &)sym->esym();
esym.st_type = target->esym().st_type; esym.st_type = sym2->esym().st_type;
if constexpr (is_ppc64v2<E>) if constexpr (is_ppc64v2<E>)
esym.ppc_local_entry = target->esym().ppc_local_entry; esym.ppc_local_entry = sym2->esym().ppc_local_entry;
} }
// Make the target absolute if necessary. // Make the defsym'ed symbol absolute if necessary.
if (!target || target->is_absolute()) if (!sym2 || sym2->is_absolute())
sym->origin = 0; sym->origin = 0;
} }
@ -2584,7 +2585,7 @@ void fix_synthetic_symbols(Context<E> &ctx) {
} }
Symbol<E> *sym2 = std::get<Symbol<E> *>(val); Symbol<E> *sym2 = std::get<Symbol<E> *>(val);
if (!sym2->file) { if (sym2->file == ctx.internal_obj) {
Error(ctx) << "--defsym: undefined symbol: " << *sym2; Error(ctx) << "--defsym: undefined symbol: " << *sym2;
continue; continue;
} }

View File

@ -0,0 +1,9 @@
#!/bin/bash
. $(dirname $0)/common.inc
cat <<EOF | $CC -o $t/a.o -c -xc -
int main() { return 0; }
EOF
! $CC -B. -o $t/exe $t/a.o -Wl,--defsym=foo=bar >& $t/log
grep -Fq 'defsym: undefined symbol: bar' $t/log