Skip to content

Commit

Permalink
Fix a crash bug in --defsym
Browse files Browse the repository at this point in the history
Fixes #1108
  • Loading branch information
rui314 committed Nov 13, 2023
1 parent 25c9cf3 commit ff3d54d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 24 deletions.
43 changes: 19 additions & 24 deletions elf/passes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -817,26 +817,27 @@ void add_synthetic_symbols(Context<E> &ctx) {

// Handle --defsym symbols.
for (i64 i = 0; i < ctx.arg.defsyms.size(); i++) {
Symbol<E> *sym = ctx.arg.defsyms[i].first;
Symbol<E> *sym1 = ctx.arg.defsyms[i].first;
std::variant<Symbol<E> *, u64> val = ctx.arg.defsyms[i].second;

Symbol<E> *target = nullptr;
if (Symbol<E> **ref = std::get_if<Symbol<E> *>(&val))
target = *ref;
if (Symbol<E> **ref = std::get_if<Symbol<E> *>(&val)) {
Symbol<E> *sym2 = *ref;
if (!sym2->file) {
Error(ctx) << "--defsym: undefined symbol: " << *sym2;
continue;
}

// If the alias refers another symobl, copy ELF symbol attributes.
if (target) {
ElfSym<E> &esym = obj.elf_syms[i + 1];
esym.st_type = target->esym().st_type;
esym.st_type = sym2->esym().st_type;
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.
if (!target || target->is_absolute())
sym->origin = 0;
if (sym2->is_absolute())
sym1->origin = 0;
} else {
sym1->origin = 0;
}
}

}

template <typename E>
Expand Down Expand Up @@ -2715,18 +2716,12 @@ void fix_synthetic_symbols(Context<E> &ctx) {
if (u64 *addr = std::get_if<u64>(&val)) {
sym->origin = 0;
sym->value = *addr;
continue;
}

Symbol<E> *sym2 = std::get<Symbol<E> *>(val);
if (!sym2->file) {
Error(ctx) << "--defsym: undefined symbol: " << *sym2;
continue;
} else {
Symbol<E> *sym2 = std::get<Symbol<E> *>(val);
sym->value = sym2->value;
sym->origin = sym2->origin;
sym->visibility = sym2->visibility.load();
}

sym->value = sym2->value;
sym->origin = sym2->origin;
sym->visibility = sym2->visibility.load();
}


Expand Down
9 changes: 9 additions & 0 deletions test/elf/defsym-missing-symbol.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash
. $(dirname $0)/common.inc

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

! $CC -B. -o $t/exe $t/a.o -Wl,-defsym=foo=bar 2> $t/log
grep -q 'undefined symbol: bar' $t/log

0 comments on commit ff3d54d

Please sign in to comment.