Skip to content

Commit

Permalink
Fix symbol size computation
Browse files Browse the repository at this point in the history
  • Loading branch information
rui314 committed Jan 6, 2025
1 parent aa4d065 commit 6d8c5ca
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/output-chunks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1761,7 +1761,8 @@ static u64 get_symbol_size(Symbol<E> &sym) {
if (esym.st_size > 0)
if (InputSection<E> *isec = sym.get_input_section())
if (!isec->extra.r_deltas.empty())
return esym.st_size - get_r_delta(*isec, esym.st_value + esym.st_size);
return esym.st_size + esym.st_value - sym.value -
get_r_delta(*isec, esym.st_value + esym.st_size);
return esym.st_size;
}

Expand Down
18 changes: 15 additions & 3 deletions test/arch-riscv64-symbol-size.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,39 @@
. $(dirname $0)/common.inc

cat <<EOF | $CC -o $t/a.o -c -xassembler -
.globl get_foo
.globl get_foo, get_bar
.type get_foo @function
get_foo:
lui a0, %hi(foo)
add a0, a0, %lo(foo)
ret
.size get_foo, .-get_foo
.type get_bar @function
get_bar:
lui a0, %hi(bar)
add a0, a0, %lo(bar)
ret
.size get_bar, .-get_bar
EOF

cat <<EOF | $CC -o $t/b.o -c -xassembler -
.globl foo, bar, baz
.globl foo, bar
foo = 0xf00
bar = 0xf00
EOF

cat <<EOF | $CC -o $t/c.o -c -xc -
#include <stdio.h>
int get_foo();
int main() { printf("%x\n", get_foo()); }
int get_bar();
int main() { printf("%x %x\n", get_foo(), get_bar()); }
EOF

$CC -B. -o $t/exe $t/a.o $t/b.o $t/c.o

readelf --syms $t/a.o | grep -Eq ' 10 FUNC .* get_foo$'
readelf --syms $t/a.o | grep -Eq ' 10 FUNC .* get_bar$'

readelf --syms $t/exe | grep -Eq ' 8 FUNC .* get_foo$'
readelf --syms $t/exe | grep -Eq ' 8 FUNC .* get_bar$'

0 comments on commit 6d8c5ca

Please sign in to comment.