Skip to content

Commit

Permalink
Fix a crash bug in --emit-relocs
Browse files Browse the repository at this point in the history
Fixes #950
  • Loading branch information
rui314 committed Jan 9, 2023
1 parent d4e40a3 commit e17d7da
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
15 changes: 12 additions & 3 deletions elf/output-chunks.cc
Expand Up @@ -3026,9 +3026,18 @@ void RelocSection<E>::copy_buf(Context<E> &ctx) {
addend = frag->offset + sym.value + get_addend(isec, rel);
} else {
InputSection<E> *target = sym.get_input_section();
OutputSection<E> *osec = target->output_section;
out.r_sym = osec->shndx;
addend = get_addend(isec, rel) + target->offset;

if (OutputSection<E> *osec = target->output_section) {
out.r_sym = osec->shndx;
addend = get_addend(isec, rel) + target->offset;
} else if (isec.name() == ".eh_frame") {
out.r_sym = ctx.eh_frame->shndx;
addend = get_addend(isec, rel);
} else {
// This is usually a dead debug section referring a
// COMDAT-eliminated section.
addend = 0;
}
}

if constexpr (E::is_rela) {
Expand Down
26 changes: 26 additions & 0 deletions test/elf/emit-relocs-dead-sections.sh
@@ -0,0 +1,26 @@
#!/bin/bash
. $(dirname $0)/common.inc

[ $MACHINE = sh4 ] && skip

cat <<EOF | $CXX -o $t/a.o -c -fPIC -xc++ -g -ffunction-sections -
#include <iostream>
struct Foo {
Foo() { std::cout << "Hello world\n"; }
};
Foo x;
EOF

cat <<EOF | $CXX -o $t/b.o -c -fPIC -xc++ -g -ffunction-sections -
#include <iostream>
struct Foo {
Foo() { std::cout << "Hello world\n"; }
};
Foo y;
int main() {}
EOF

$CXX -B. -o $t/exe $t/a.o $t/b.o -Wl,-emit-relocs
$QEMU $t/exe | grep -q 'Hello world'

0 comments on commit e17d7da

Please sign in to comment.