[LLVM] With LTO, ld -ObjC links too much and interacts badly with -dead_strip
| Originator: | pcc | ||
| Number: | rdar://20356002 | Date Originated: | 3/30/2015 |
| Status: | Open | Resolved: | |
| Product: | Developer Tools | Product Version: | |
| Classification: | Reproducible: |
Summary:
It appears that -ObjC causes the linker to link all LTO objects in all archives. If -dead_strip is also passed the linker can fail to link the program, producing "loaded too late" and "undefined symbol" errors in certain cases.
See also: https://llvm.org/bugs/show_bug.cgi?id=23007
Steps to Reproduce:
With Xcode installed:
$ cat main.cc
int main() {}
$ cat globalinit.cc
class A {
public:
A();
};
A a;
int b;
$ cat ctor.cc
#include <stdio.h>
class A {
public:
A();
};
A::A() {
puts("hello world");
}
$ clang++ -c -flto main.cc globalinit.cc
$ clang++ -c ctor.cc
$ rm -f libglobalinit.a
$ ar cru libglobalinit.a globalinit.o
$ rm -f libctor.a
$ ar cru libctor.a ctor.o
$ clang++ -Wl,-ObjC -o test1 main.o libglobalinit.a libctor.a
$ clang++ -Wl,-ObjC -Wl,-dead_strip -o test2 main.o libglobalinit.a libctor.a
ld: warning: '_b' is implemented in bitcode, but it was loaded too late
Undefined symbols for architecture x86_64:
"_a", referenced from:
__GLOBAL__I_a in lto.o
import-atom in libglobalinit.a(globalinit.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
$ ./test1
hello world
Expected Results:
test1 should print no output and test2 should link successfully.
Actual Results:
The global initializers in test1 are run causing the output "hello world" to be printed. test2 produces linker errors.
Version:
OS X 10.9.5
Xcode 6.1.1
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!