Issue 98294
Summary [LLD][ELF] undefined reference to foo when using --wrap=foo when __real_foo is declared weak
Labels
Assignees
Reporter xdqi
    foo.c
```c
extern __attribute__((weak)) int __real_foo();
int __wrap_foo() {
  if (__real_foo) return __real_foo();
  return 42;
}
int foo();

void _exit(int);
int _start() {
  _exit(foo());
}

```

It's okay to compile and run it with GNU ld or mold:

```sh
❯ clang -o foo foo.c -Wl,--wrap=foo -fuse-ld=ld -nostartfiles
❯ ./foo
❯ echo $?
42
❯ ld -v
GNU ld (GNU Binutils for Ubuntu) 2.38
```

But when I switch to lld, I got an error instead
```sh
❯ clang -o foo foo.c -Wl,--wrap=foo -fuse-ld=lld -nostartfiles
ld.lld: error: undefined symbol: foo
>>> referenced by foo.c
>>>               /tmp/foo-e30076.o:(__wrap_foo)
>>> referenced by foo.c
>>>               /tmp/foo-e30076.o:(__wrap_foo)
>>> did you mean: __wrap_foo
>>> defined in: /tmp/foo-e30076.o
clang: error: linker command failed with exit code 1 (use -v to see invocation)
❯ ld.lld -v
LLD 19.0.0 (https://github.com/llvm/llvm-project.git 4d052a76185875bf8d8343d723a444795b87c8c5) (compatible with GNU linkers)
```

I'm sure that I'm using the latest LLVM. I will try to submit a patch soon
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to