Bug 27871 - ld: Add -Bsymbolic-non-weak-functions which only applies to STB_GLOBAL STT_FUNC
Summary: ld: Add -Bsymbolic-non-weak-functions which only applies to STB_GLOBAL STT_FUNC
Status: UNCONFIRMED
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: unspecified
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-05-15 23:09 UTC by Fangrui Song
Modified: 2021-05-23 06:14 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Fangrui Song 2021-05-15 23:09:56 UTC
As a poor man's direct binding feature, -Bsymbolic-functions is incompatible with two things:

(1) canonical PLT entries with -fno-pic code. This should be fixed on GCC's side https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100593
(2) some vague linkage function definitions. See below


cat > ./a.cc <<eof
inline void f() {}
void *g() { return (void *)&f; }
eof
cat > ./a.sh <<eof
gcc -fpic -fuse-ld=bfd -shared -Wl,-Bsymbolic-functions a.cc -o a.so
gcc -fpie -fuse-ld=bfd b.cc ./a.so
./a.out
eof
cat > ./b.cc <<eof
#include <stdio.h>

inline void f() {}
void *g();
int main() {
  printf("exe: %p\n", (void *)&f);
  printf("DSO: %p\n", g());
}
eof

On Mach-O, such symbols are placed into __LINKEDIT,__weak_binding so that dyld can coalesce the definitions across dylibs.

For ELF, we can introduce -Bsymbolic-global-functions to exclude STB_WEAK function definitions, avoiding the pointer equality issue.

For more context, see https://maskray.me/blog/2021-05-16-elf-interposition-and-bsymbolic#the-last-alliance-of-elf-and-men
Comment 1 Fangrui Song 2021-05-23 06:14:15 UTC
ld patch: https://sourceware.org/pipermail/binutils/2021-May/116703.html
gold patch: https://sourceware.org/pipermail/binutils/2021-May/116683.html

(I was considering -Bsymbolic-global-functions but Peter Smith suggested that "global" can confuse users. non-weak or non-vague is better. Detecting vague properly requires detection of COMDAT, which would cause some complexity. I feel that non-weak is better: this way we provide an escape hatch for users who want definition interposition (very rare and good to have a way))