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

[lld] why does lld export hidden symbols without warning when dynamic libraries link to object files?

Open Zhenhang1213 opened this issue 2 years ago • 6 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=ld  -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
clang-15: error: linker command failed with exit code 1 (use -v to see invocation)

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

wangzhenhang@bisheng:~/mls$ clang -v
clang version 15.0.4
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /opt/buildtools/llvm-15.0.4/bin
Found candidate GCC installation: /usr/lib/gcc-cross/i686-linux-gnu/7
Found candidate GCC installation: /usr/lib/gcc-cross/i686-linux-gnu/7.5.0
Found candidate GCC installation: /usr/lib/gcc-cross/x86_64-linux-gnux32/7
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7.5.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/8
Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7.5.0
Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Candidate multilib: x32;@mx32
Selected multilib: .;@m64

Zhenhang1213 avatar Mar 27 '24 06:03 Zhenhang1213

Duplicate of #86369?

EugeneZelenko avatar Mar 27 '24 13:03 EugeneZelenko

Duplicate of #86369?

yes,I rearrange it.

Zhenhang1213 avatar Mar 28 '24 01:03 Zhenhang1213

@Zhenhang1213: You could edit comments in issues, so there is no need to open new ticket for this purpose.

EugeneZelenko avatar Mar 28 '24 01:03 EugeneZelenko

@Zhenhang1213: You could edit comments in issues, so there is no need to open new ticket for this purpose.

OK,and I find it doesn‘t precross undefined symbols in a SharedFile from function Symbol::resolveUndefined in Symbols.cpp

Zhenhang1213 avatar Mar 28 '24 01:03 Zhenhang1213

CC @MaskRay

hstk30-hw avatar Mar 30 '24 01:03 hstk30-hw

@llvm/issue-subscribers-lld-elf

Author: Austin (Zhenhang1213)

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&lt;stdio.h&gt;
#include"a.h"

int main1(void) {
    printf("main1\n");
    return 0;
}

a.h:

int main1(void); b.c:

#include&lt;stdio.h&gt;
#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=ld  -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
clang-15: error: linker command failed with exit code 1 (use -v to see invocation)

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

wangzhenhang@<!-- -->bisheng:~/mls$ clang -v
clang version 15.0.4
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /opt/buildtools/llvm-15.0.4/bin
Found candidate GCC installation: /usr/lib/gcc-cross/i686-linux-gnu/7
Found candidate GCC installation: /usr/lib/gcc-cross/i686-linux-gnu/7.5.0
Found candidate GCC installation: /usr/lib/gcc-cross/x86_64-linux-gnux32/7
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7.5.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/8
Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/7.5.0
Candidate multilib: .;@<!-- -->m64
Candidate multilib: 32;@<!-- -->m32
Candidate multilib: x32;@<!-- -->mx32
Selected multilib: .;@<!-- -->m64

llvmbot avatar Mar 30 '24 14:03 llvmbot