[llvm-bugs] [Bug 51082] New: link error when overriding common symbols with weak symbols in library
via llvm-bugs
llvm-bugs at lists.llvm.org
Tue Jul 13 11:20:36 PDT 2021
https://bugs.llvm.org/show_bug.cgi?id=51082
Bug ID: 51082
Summary: link error when overriding common symbols with weak
symbols in library
Product: lld
Version: unspecified
Hardware: PC
OS: Linux
Status: NEW
Severity: normal
Priority: P
Component: ELF
Assignee: unassignedbugs at nondot.org
Reporter: yabinc at google.com
CC: llvm-bugs at lists.llvm.org, smithp352 at googlemail.com
If I define a common symbol in main object file, and define a weak symbol with
the same name in a library. lld may not be able to override the common symbol
correctly with the weak symbol, and report link error.
Below is an example:
main.c:
#include <stdio.h>
extern int A;
const char* B;
int main() {
printf("A = %d, B = %s\n", A, B);
}
lib.c:
int A;
const char* B __attribute__((weak));
build.sh:
CLANG_DIR=/ssd/opensource/llvm/llvm-build/install/bin
$CLANG_DIR/clang -c -o main.o main.c -fPIE -fcommon
$CLANG_DIR/clang -c -o lib.o lib.c -fPIC
$CLANG_DIR/llvm-ar crsPD -format=gnu lib.a lib.o
$CLANG_DIR/clang main.o lib.a -pie -fuse-ld=lld -v
link error:
ld.lld: error: relocation R_X86_64_PC32 cannot be used against symbol B;
recompile with -fPIC
>>> defined in lib.a
>>> referenced by main.c
>>> main.o:(main)
The cause of the error seems to be below:
1. lld parses main.o, and adds undefined symbol A and common symbol B.
2. lld parses lib.a, reads LazyArchive symbol A, and decides to read lib.o
to fetch symbol A. By the way, it reads weak symbol B in lib.o, but
decide not to override common symbol B.
3. lld parses lib.a, reads lazyArchive symbol B, and decides to override
common symbol B. So it replace common symbol B with lazyArchive symbol B.
But not further read weak symbol B, because lib.o has been read before.
4. As a result, we have a LazyArchive symbol B, which can't do relocation.
--
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20210713/e1c79a44/attachment-0001.html>
More information about the llvm-bugs
mailing list