llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[lld] why does lld export hidden symbols without warning?

Open Zhenhang1213 opened this issue 2 years ago • 3 comments

When I compile .o with two c files -fvisibility=hidden,and a.c depends on a function of b.c. then generates bit dynamic libraries of a.c, Finally define a main function in the new file to generate executable files together, there will be an error using ld, but not lld.

code: a.c:

`#include<stdio.h> #include"a.h"

int main1(void) { printf("main1\n"); return 0; } ` a.h:

int main1(void); b.c:

`#include<stdio.h> #include"a.h"

extern int main1();

int shared1(void) { printf("shared1\n"); main1(); return 0; } ` main.c:

int main(void) { return 0; } and the compiler Commands is:

clang -fvisibility=hidden -o a.o  -c a.c
clang -fvisibility=hidden -o main.o  -c main.c
clang -fvisibility=hidden -o b.o  -c b.c
clang -shared -Wl,-soname,libb.so -o libb.so b.o
clang -fuse-ld=lld  -o main.out  main.o a.o libb.so

error: /usr/bin/ld: main.out: hidden symbol main1' in a.o is referenced by DSO /usr/bin/ld: final link failed: bad value`

without any error or warning: clang -fuse-ld=lld -o main.out main.o a.o libb.so

Zhenhang1213 avatar Mar 23 '24 02:03 Zhenhang1213

You samples don't compile and link at all, did you actually test them? In any case, if I correct all the errors, it gives the expected error during linking:

$ clang -fuse-ld=lld  -o main.out  main.o b.o a.so
ld.lld: error: undefined symbol: main1
>>> referenced by b.c
>>>               b.o:(shared1)
>>> did you mean: main
>>> defined in: main.o
clang: error: linker command failed with exit code 1 (use -v to see invocation)

DimitryAndric avatar Mar 23 '24 10:03 DimitryAndric

You samples don't compile and link at all, did you actually test them? In any case, if I correct all the errors, it gives the expected error during linking:

$ clang -fuse-ld=lld  -o main.out  main.o b.o a.so
ld.lld: error: undefined symbol: main1
>>> referenced by b.c
>>>               b.o:(shared1)
>>> did you mean: main
>>> defined in: main.o
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I am sorry, it's my fault. And I modify my sample, you can test it again.

Zhenhang1213 avatar Mar 25 '24 01:03 Zhenhang1213

clang -fuse-ld=lld -o main.out main.o b.o a.so

if you change you command to use clang -fuse-ld=lld -o main.out main.o a.o b.so, it doesn't give any errors.

Zhenhang1213 avatar Mar 26 '24 01:03 Zhenhang1213