llvm.org GIT mirror llvm / 49f70cc
Infer relocation model from module flags in relocatable LTO link. Fix for PR33096. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@303578 91177308-0d34-0410-b5e6-96231b3b80d8 Evgeniy Stepanov 3 years ago
5 changed file(s) with 85 addition(s) and 12 deletion(s). Raw diff Collapse all Expand all
3838 std::string CPU;
3939 TargetOptions Options;
4040 std::vector MAttrs;
41 Reloc::Model RelocModel = Reloc::PIC_;
41 Optional RelocModel = Reloc::PIC_;
4242 CodeModel::Model CodeModel = CodeModel::Default;
4343 CodeGenOpt::Level CGOptLevel = CodeGenOpt::Default;
4444 TargetMachine::CodeGenFileType CGFileType = TargetMachine::CGFT_ObjectFile;
113113 AddUnsigned((unsigned)Conf.Options.DebuggerTuning);
114114 for (auto &A : Conf.MAttrs)
115115 AddString(A);
116 AddUnsigned(Conf.RelocModel);
116 if (Conf.RelocModel)
117 AddUnsigned(*Conf.RelocModel);
118 else
119 AddUnsigned(-1);
117120 AddUnsigned(Conf.CodeModel);
118121 AddUnsigned(Conf.CGOptLevel);
119122 AddUnsigned(Conf.CGFileType);
116116 namespace {
117117
118118 std::unique_ptr
119 createTargetMachine(Config &Conf, StringRef TheTriple,
120 const Target *TheTarget) {
119 createTargetMachine(Config &Conf, const Target *TheTarget, Module &M) {
120 StringRef TheTriple = M.getTargetTriple();
121121 SubtargetFeatures Features;
122122 Features.getDefaultSubtargetFeatures(Triple(TheTriple));
123123 for (const std::string &A : Conf.MAttrs)
124124 Features.AddFeature(A);
125125
126 Reloc::Model RelocModel;
127 if (Conf.RelocModel)
128 RelocModel = *Conf.RelocModel;
129 else
130 RelocModel =
131 M.getPICLevel() == PICLevel::NotPIC ? Reloc::Static : Reloc::PIC_;
132
126133 return std::unique_ptr(TheTarget->createTargetMachine(
127 TheTriple, Conf.CPU, Features.getString(), Conf.Options, Conf.RelocModel,
134 TheTriple, Conf.CPU, Features.getString(), Conf.Options, RelocModel,
128135 Conf.CodeModel, Conf.CGOptLevel));
129136 }
130137
310317 std::unique_ptr MPartInCtx = std::move(MOrErr.get());
311318
312319 std::unique_ptr TM =
313 createTargetMachine(C, MPartInCtx->getTargetTriple(), T);
320 createTargetMachine(C, T, *MPartInCtx);
314321
315322 codegen(C, TM.get(), AddStream, ThreadId, *MPartInCtx);
316323 },
359366 if (!TOrErr)
360367 return TOrErr.takeError();
361368
362 std::unique_ptr TM =
363 createTargetMachine(C, Mod->getTargetTriple(), *TOrErr);
369 std::unique_ptr TM = createTargetMachine(C, *TOrErr, *Mod);
364370
365371 // Setup optimization remarks.
366372 auto DiagFileOrErr = lto::setupOptimizationRemarks(
396402 if (!TOrErr)
397403 return TOrErr.takeError();
398404
399 std::unique_ptr TM =
400 createTargetMachine(Conf, Mod.getTargetTriple(), *TOrErr);
405 std::unique_ptr TM = createTargetMachine(Conf, *TOrErr, Mod);
401406
402407 if (Conf.CodeGenOnly) {
403408 codegen(Conf, TM.get(), AddStream, Task, Mod);
0 ; RUN: cat %s >%t.pic.ll
1 ; RUN: echo '!llvm.module.flags = !{!0}' >>%t.pic.ll
2 ; RUN: echo '!0 = !{i32 1, !"PIC Level", i32 2}' >>%t.pic.ll
3
4 ; RUN: llvm-as %s -o %t.o
5 ; RUN: llvm-as %t.pic.ll -o %t.pic.o
6
7 ;; Non-PIC source.
8
9 ; RUN: %gold -m elf_x86_64 -plugin %llvmshlibdir/LLVMgold.so \
10 ; RUN: --shared \
11 ; RUN: --plugin-opt=save-temps %t.o -o %t-out
12 ; RUN: llvm-readobj -r %t-out.o | FileCheck %s --check-prefix=PIC
13
14 ; RUN: %gold -m elf_x86_64 -plugin %llvmshlibdir/LLVMgold.so \
15 ; RUN: --export-dynamic --noinhibit-exec -pie \
16 ; RUN: --plugin-opt=save-temps %t.o -o %t-out
17 ; RUN: llvm-readobj -r %t-out.o | FileCheck %s --check-prefix=PIC
18
19 ; RUN: %gold -m elf_x86_64 -plugin %llvmshlibdir/LLVMgold.so \
20 ; RUN: --export-dynamic --noinhibit-exec \
21 ; RUN: --plugin-opt=save-temps %t.o -o %t-out
22 ; RUN: llvm-readobj -r %t-out.o | FileCheck %s --check-prefix=STATIC
23
24 ; RUN: %gold -m elf_x86_64 -plugin %llvmshlibdir/LLVMgold.so \
25 ; RUN: -r \
26 ; RUN: --plugin-opt=save-temps %t.o -o %t-out
27 ; RUN: llvm-readobj -r %t-out.o | FileCheck %s --check-prefix=STATIC
28
29 ;; PIC source.
30
31 ; RUN: %gold -m elf_x86_64 -plugin %llvmshlibdir/LLVMgold.so \
32 ; RUN: --shared \
33 ; RUN: --plugin-opt=save-temps %t.pic.o -o %t-out
34 ; RUN: llvm-readobj -r %t-out.o | FileCheck %s --check-prefix=PIC
35
36 ; RUN: %gold -m elf_x86_64 -plugin %llvmshlibdir/LLVMgold.so \
37 ; RUN: --export-dynamic --noinhibit-exec -pie \
38 ; RUN: --plugin-opt=save-temps %t.pic.o -o %t-out
39 ; RUN: llvm-readobj -r %t-out.o | FileCheck %s --check-prefix=PIC
40
41 ; RUN: %gold -m elf_x86_64 -plugin %llvmshlibdir/LLVMgold.so \
42 ; RUN: --export-dynamic --noinhibit-exec \
43 ; RUN: --plugin-opt=save-temps %t.pic.o -o %t-out
44 ; RUN: llvm-readobj -r %t-out.o | FileCheck %s --check-prefix=STATIC
45
46 ; RUN: %gold -m elf_x86_64 -plugin %llvmshlibdir/LLVMgold.so \
47 ; RUN: -r \
48 ; RUN: --plugin-opt=save-temps %t.pic.o -o %t-out
49 ; RUN: llvm-readobj -r %t-out.o | FileCheck %s --check-prefix=PIC
50
51
52 ; PIC: R_X86_64_GOTPCREL foo
53 ; STATIC: R_X86_64_PC32 foo
54
55 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
56 target triple = "x86_64-unknown-linux-gnu"
57
58 @foo = external global i32
59 define i32 @main() {
60 %t = load i32, i32* @foo
61 ret i32 %t
62 }
101101 static ld_plugin_set_extra_library_path set_extra_library_path = nullptr;
102102 static ld_plugin_get_view get_view = nullptr;
103103 static bool IsExecutable = false;
104 static Optional RelocationModel;
104 static Optional RelocationModel = None;
105105 static std::string output_name = "";
106106 static std::list Modules;
107107 static DenseMap FDToLeaderHandle;
281281 case LDPT_LINKER_OUTPUT:
282282 switch (tv->tv_u.tv_val) {
283283 case LDPO_REL: // .o
284 IsExecutable = false;
285 break;
284286 case LDPO_DYN: // .so
285287 IsExecutable = false;
286288 RelocationModel = Reloc::PIC_;
725727 Conf.Options.RelaxELFRelocations = false;
726728
727729 Conf.MAttrs = MAttrs;
728 Conf.RelocModel = *RelocationModel;
730 Conf.RelocModel = RelocationModel;
729731 Conf.CGOptLevel = getCGOptLevel();
730732 Conf.DisableVerify = options::DisableVerify;
731733 Conf.OptLevel = options::OptLevel;