[LTO] Inherit options from Codegen before initializing TargetMachine.
authorDavide Italiano <davide@freebsd.org>
Fri, 1 Apr 2016 00:35:29 +0000 (00:35 +0000)
committerDavide Italiano <davide@freebsd.org>
Fri, 1 Apr 2016 00:35:29 +0000 (00:35 +0000)
commit8eca282dc97698b98fd02636eaa260cea267ca31
treea5bad730f531101fd59d5ac5feea45e0c118595a
parente9148dd62f42531703d8d7995549ec4cf4f9e363
[LTO] Inherit options from Codegen before initializing TargetMachine.

This fixes bootstrap of llvm-tblgen (with LTO) and PR27150.
Slightly longer explanation follows.

Emission of .init_array instead of .ctors is supported only on a
subset of the Target LLVM supports. Codegen needs to be conservative
and always emit .ctors unless instructed otherwise (based on target).
If the dynamic linker sees .init_array it completely ignores
what's inside .ctors and therefore some constructors are not called
(and this causes llvm-tblgen to crash on startup).

Teach LLD/LTO about the Codegen options so we end up always emitting
.init_array and avoid this issue.
In future, we might end up supporting mix of .ctors and .init_array
in different input files if this shows up as a real-world use case.
The way gold handles this case is mapping .ctors from input into
.init_array in output. There's also another caveat because
as far as I understand .ctors run in reverse order so when we do
the copy/mapping we need to reverse copy in the output if there's
more than one ctor. That's why I'd rather avoid this complicate logic
unless there's a real need.

An analogous reasoning holds for .dtors/.fini_array.

llvm-svn: 265085
lld/ELF/LTO.cpp
lld/test/ELF/lto/ctors.ll