Skip to content

Commit ae4c30a

Browse files
committed
[ELF] Support explicitly overriding relocation model in LTO
lld currently selects the relocation model automatically depending on the link flags specified, but in some cases it'd be useful to allow explicitly overriding the relocation model using a flag. llvm-svn: 366644
1 parent a2dd672 commit ae4c30a

File tree

4 files changed

+17
-1
lines changed

4 files changed

+17
-1
lines changed

lld/Common/TargetOptionsCommandFlags.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ llvm::TargetOptions lld::initTargetOptionsFromCodeGenFlags() {
2626
return ::InitTargetOptionsFromCodeGenFlags();
2727
}
2828

29+
llvm::Optional<llvm::Reloc::Model> lld::getRelocModelFromCMModel() {
30+
return getRelocModel();
31+
}
32+
2933
llvm::Optional<llvm::CodeModel::Model> lld::getCodeModelFromCMModel() {
3034
return getCodeModel();
3135
}

lld/ELF/LTO.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ static lto::Config createConfig() {
7676
c.Options.FunctionSections = true;
7777
c.Options.DataSections = true;
7878

79-
if (config->relocatable)
79+
if (auto relocModel = getRelocModelFromCMModel())
80+
c.RelocModel = *relocModel;
81+
else if (config->relocatable)
8082
c.RelocModel = None;
8183
else if (config->isPic)
8284
c.RelocModel = Reloc::PIC_;

lld/include/lld/Common/TargetOptionsCommandFlags.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
namespace lld {
1818
llvm::TargetOptions initTargetOptionsFromCodeGenFlags();
19+
llvm::Optional<llvm::Reloc::Model> getRelocModelFromCMModel();
1920
llvm::Optional<llvm::CodeModel::Model> getCodeModelFromCMModel();
2021
std::string getCPUStr();
2122
std::vector<std::string> getMAttrs();

lld/test/ELF/lto/relocation-model.ll

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@
3333
; RUN: llvm-readobj -r %t-out.lto.o | FileCheck %s --check-prefix=PIC
3434

3535

36+
;; Explicit flag.
37+
38+
; RUN: ld.lld %t.o -o %t-out -save-temps -r -mllvm -relocation-model=pic
39+
; RUN: llvm-readobj -r %t-out.lto.o | FileCheck %s --check-prefix=PIC
40+
41+
; RUN: ld.lld %t.o -o %t-out -save-temps -r -mllvm -relocation-model=static
42+
; RUN: llvm-readobj -r %t-out.lto.o | FileCheck %s --check-prefix=STATIC
43+
44+
3645
; PIC: R_X86_64_REX_GOTPCRELX foo
3746
; STATIC: R_X86_64_PC32 foo
3847

0 commit comments

Comments
 (0)