Skip to content

Commit

Permalink
Export __global_pointer$ from executable
Browse files Browse the repository at this point in the history
RISC-V psABI requires that symbol to be exported from an executable
if there's a GP-relative reference. For simplicity, we always export
it from executable as long as it has a .dynamic section.

#1222
  • Loading branch information
rui314 committed Mar 25, 2024
1 parent 0edbe29 commit 3df7c8e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
5 changes: 4 additions & 1 deletion elf/passes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -801,8 +801,11 @@ void add_synthetic_symbols(Context<E> &ctx) {
if constexpr (supports_tlsdesc<E>)
ctx._TLS_MODULE_BASE_ = add("_TLS_MODULE_BASE_", STT_TLS);

if constexpr (is_riscv<E>)
if constexpr (is_riscv<E>) {
ctx.__global_pointer = add("__global_pointer$");
if (ctx.dynamic && !ctx.arg.shared)
ctx.__global_pointer->is_exported = true;
}

if constexpr (is_arm32<E>) {
ctx.__exidx_start = add("__exidx_start");
Expand Down
27 changes: 27 additions & 0 deletions test/elf/riscv64_global-pointer.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash
. $(dirname $0)/common.inc

cat <<EOF | $CC -o $t/a.o -c -xc - -fPIC
#include <stdio.h>
int main() {
printf("Hello world\n");
}
EOF

$CC -B. -o $t/exe1 $t/a.o -fno-PIE
readelf -W --dyn-syms $t/exe1 | grep -Fq '__global_pointer$'

$CC -B. -o $t/exe2 $t/a.o -fPIE
readelf -W --dyn-syms $t/exe2 | grep -Fq '__global_pointer$'


cat <<EOF | $CC -o $t/b.o -c -xc - -fPIC
#include <stdio.h>
int hello() {
printf("Hello world\n");
}
EOF

$CC -B. -o $t/c.so $t/b.o -shared
readelf -W --dyn-syms $t/c.so > $t/log1
! grep -Fq '__global_pointer$' $t/log1 || false

0 comments on commit 3df7c8e

Please sign in to comment.