1

Could it be that clang and gcc somehow implement -Bsymbolic differently?

\\ exe.c
void f();

typedef void (*fptr)();
void compareFuncPtrs(fptr);

int main()
{
    compareFuncPtrs(f);
    return 0;
}
\\ shlib.c
#include <stdio.h>

void f() {}

typedef void (*fptr)();
void compareFuncPtrs(fptr pExe)
{
    fptr pShlib = f;
    printf(pExe == pShlib ? "Equal\n" : "Different\n");
}
# buildAndRun.sh
echo
echo 'clang + Bsymbolic'
echo '-----------------'

clang -shared -Wl,-Bsymbolic -fPIC -o libshlib.so shlib.c
clang -L. -Wl,-rpath,. -o exe exe.c -lshlib 
./exe


echo
echo 'gcc + Bsymbolic'
echo '---------------'

gcc -shared -Wl,-Bsymbolic -fPIC -o libshlib.so shlib.c
gcc -L. -Wl,-rpath,. -o exe exe.c -lshlib
./exe

On my system $ buildAndRun.sh gives:

clang + Bsymbolic
-----------------
Equal

gcc + Bsymbolic
---------------
Different

The test system has gcc 10.3.1 and clang 16.0.2. Can anyone reproduce this diff? What is the reason for it?


Edit: by @yugr's suggestion I diffed command lines and discovered that for some reason my clang adds -pie to the build of the executable. Indeed, when adding it manually to the gcc build I get the behavior I expected:

# buildAndRun.sh
...

gcc -fpie -L. -Wl,-rpath,. -o exe exe.c -lshlib
./exe

gives -

clang + Bsymbolic
-----------------
Equal

gcc + Bsymbolic
---------------
Equal

Any insight on how -fpie intervenes in Bsymbolics impact?

3
  • 1
    -Wl flags are passed straight to the linker so I suggest to see if linker invocations are similar (you can emit them with -v flag). The issue does not repro on Ubuntu 22.04 (clang 14, gcc 11.4).
    – yugr
    Commented Mar 26 at 17:49
  • I can't explain the difference, but the C language spec says that two pointers to the same function compare equal (C17 6.5.9/6), so I'm inclined to think that GCC is buggy here. I speculate GCC itself, not the linker, for I put the primary responsibility on GCC to provide conformance with the spec, regardless of foreseeable combinations of link options that do not inherently conflict with conformance. Commented Mar 26 at 20:54
  • The answer probably lies somewhere here, but is still beyond me: maskray.me/blog/2021-05-16-elf-interposition-and-bsymbolic Commented Mar 27 at 9:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.