Skip to content

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

@Zhenhang1213

Description

@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<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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions