From: Davide Italiano Date: Fri, 1 Apr 2016 00:35:29 +0000 (+0000) Subject: [LTO] Inherit options from Codegen before initializing TargetMachine. X-Git-Tag: llvmorg-3.9.0-rc1~10313 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=8eca282dc97698b98fd02636eaa260cea267ca31;p=platform%2Fupstream%2Fllvm.git [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 --- diff --git a/lld/ELF/LTO.cpp b/lld/ELF/LTO.cpp index abcafb1..180236c 100644 --- a/lld/ELF/LTO.cpp +++ b/lld/ELF/LTO.cpp @@ -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); } diff --git a/lld/test/ELF/lto/ctors.ll b/lld/test/ELF/lto/ctors.ll index aaf9a13..7fce645 100644 --- a/lld/test/ELF/lto/ctors.ll +++ b/lld/test/ELF/lto/ctors.ll @@ -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