Skip to content

Commit

Permalink
Emit an IRELATIVE relocation instead of GLOB_DAT for an ifunc in DSO
Browse files Browse the repository at this point in the history
Fixes #1385
  • Loading branch information
rui314 committed Jan 3, 2025
1 parent 0a112d0 commit a297859
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/output-chunks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1323,12 +1323,6 @@ static std::vector<GotEntry<E>> get_got_entries(Context<E> &ctx) {
for (Symbol<E> *sym : ctx.got->got_syms) {
i64 idx = sym->get_got_idx(ctx);

// If a symbol is imported, let the dynamic linker to resolve it.
if (sym->is_imported) {
add({idx, 0, E::R_GLOB_DAT, sym});
continue;
}

// IFUNC always needs to be fixed up by the dynamic linker.
if constexpr (supports_ifunc<E>) {
if (sym->is_ifunc()) {
Expand All @@ -1342,6 +1336,12 @@ static std::vector<GotEntry<E>> get_got_entries(Context<E> &ctx) {
}
}

// If a symbol is imported, let the dynamic linker to resolve it.
if (sym->is_imported) {
add({idx, 0, E::R_GLOB_DAT, sym});
continue;
}

// If we know an address at link-time, fill that GOT entry now.
// It may need a base relocation, though.
if (ctx.arg.pic && sym->is_relative())
Expand Down
26 changes: 26 additions & 0 deletions test/arch-x86_64-function-multiversion.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/bash
. $(dirname $0)/common.inc

cat <<EOF | $GCC -o $t/a.o -c -xc++ - -fPIC
#include <iostream>
class Hello {
public:
__attribute__((target("default"))) void say() { std::cout << "Hello\n"; }
__attribute__((target("popcnt"))) void say() { std::cout << "Howdy\n"; }
};
void hello() {
Hello().say();
}
EOF

$CXX -B. -shared -o $t/b.so $t/a.o

cat <<EOF | $CC -o $t/c.o -c -xc++ - -fPIC
void hello();
int main() { hello(); }
EOF

$CXX -B. -o $t/exe $t/b.so $t/c.o
$QEMU $t/exe | grep -q '^H'

0 comments on commit a297859

Please sign in to comment.