[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)
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

index abcafb1..180236c 100644 (file)
@@ -15,6 +15,7 @@
 #include "llvm/Analysis/TargetLibraryInfo.h"
 #include "llvm/Analysis/TargetTransformInfo.h"
 #include "llvm/Bitcode/ReaderWriter.h"
+#include "llvm/CodeGen/CommandFlags.h"
 #include "llvm/IR/LegacyPassManager.h"
 #include "llvm/Linker/IRMover.h"
 #include "llvm/Support/StringSaver.h"
@@ -167,7 +168,7 @@ TargetMachine *BitcodeCompiler::getTargetMachine() {
   const Target *T = TargetRegistry::lookupTarget(TripleStr, Msg);
   if (!T)
     fatal("target not found: " + Msg);
-  TargetOptions Options;
+  TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
   Reloc::Model R = Config->Pic ? Reloc::PIC_ : Reloc::Static;
   return T->createTargetMachine(TripleStr, "", "", Options, R);
 }
index aaf9a13..7fce645 100644 (file)
@@ -13,4 +13,6 @@ define void @ctor() {
 }
 
 ; The llvm.global_ctors should end up producing constructors.
-; CHECK: Name: .ctors
+; On x86-64 (linux) we should always emit .init_array and never .ctors.
+; CHECK: Name: .init_array
+; CHECK-NOT: Name: .ctors