Bug 32071 - undefined weak hidden function symbols resolves to garbage with PIE
Summary: undefined weak hidden function symbols resolves to garbage with PIE
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: gold (show other bugs)
Version: 2.43
: P2 normal
Target Milestone: 2.44
Assignee: Cary Coutant
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2024-08-10 15:17 UTC by Tatsuyuki Ishi
Modified: 2024-08-31 13:30 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed: 2024-08-17 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Tatsuyuki Ishi 2024-08-10 15:17:45 UTC
When there is no definition for a weak undef STT_FUNC symbol, the function's address should resolve to absolute zero, not some random address.

In Bug 15435 gold was changed to accept weak undefs with STV_HIDDEN, however when no definition is present it seems to resolve to some garbage address [1], instead of zero. This is easily reproducible with the sample from the same bug.

---
extern void undefined () __attribute__((visibility("hidden")))
  __attribute__((weak));

int main ()
{
  if (&undefined != 0) return 1;
  return 0;
}
---

$ gcc main.c -fuse-ld=bfd && ./a.out; echo $?
0

$ gcc main.c -fuse-ld=gold && ./a.out; echo $?
1

$ gcc main.c -fuse-ld=lld && ./a.out; echo $?
0

$ gcc main.c -fuse-ld=mold && ./a.out; echo $?
0

[1]: https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/29986#note_2516917
Comment 1 H.J. Lu 2024-08-17 13:54:14 UTC
A patch is posted at

https://sourceware.org/pipermail/binutils/2024-August/136394.html
Comment 2 H.J. Lu 2024-08-17 14:15:23 UTC
Here is the v2 patch:

https://sourceware.org/pipermail/binutils/2024-August/136395.html
Comment 3 Sourceware Commits 2024-08-31 11:46:50 UTC
The master branch has been updated by H.J. Lu <hjl@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=229ecf80f35c64145678e537daf54358d16107e3

commit 229ecf80f35c64145678e537daf54358d16107e3
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Wed Aug 21 08:06:41 2024 -0700

    gold: Always resolve non-default weak undefined to 0
    
    Non-default weak undefined symbols in executable and shared library are
    always resolved to 0 at runtime and don't need dynamic relocation.
    
    Tested on i686, x86-64, powerpc64le and aarch64.
    
            PR gold/32071
            * symtab.cc (Symbol::final_value_is_known): Always resolve
            non-default weak undefined symbol in executable and shared library
            to 0 at runtime.
            * symtab.h (Symbol::needs_dynamic_reloc): Return false for
            non-default weak undefined symbol in executable and shared library.
            * testsuite/Makefile.am: Add weak_undef_test_3 and
            weak_undef_test_4 tests.
            * testsuite/Makefile.in: Regenerated.
            * testsuite/weak_undef_lib_4.c: New file.
            * testsuite/weak_undef_test_3.c: Likewise.
            * testsuite/weak_undef_test_4.c: Likewise.
    
    Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
Comment 4 H.J. Lu 2024-08-31 13:22:11 UTC
Fixed for 2.44.
Comment 5 Tatsuyuki Ishi 2024-08-31 13:30:29 UTC
Thanks for fixing.