Index: lld/Common/TargetOptionsCommandFlags.cpp =================================================================== --- lld/Common/TargetOptionsCommandFlags.cpp +++ lld/Common/TargetOptionsCommandFlags.cpp @@ -26,6 +26,10 @@ return ::InitTargetOptionsFromCodeGenFlags(); } +llvm::Optional lld::getRelocModelFromCMModel() { + return getRelocModel(); +} + llvm::Optional lld::getCodeModelFromCMModel() { return getCodeModel(); } Index: lld/ELF/LTO.cpp =================================================================== --- lld/ELF/LTO.cpp +++ lld/ELF/LTO.cpp @@ -76,7 +76,9 @@ c.Options.FunctionSections = true; c.Options.DataSections = true; - if (config->relocatable) + if (auto relocModel = getRelocModelFromCMModel()) + c.RelocModel = *relocModel; + else if (config->relocatable) c.RelocModel = None; else if (config->isPic) c.RelocModel = Reloc::PIC_; Index: lld/include/lld/Common/TargetOptionsCommandFlags.h =================================================================== --- lld/include/lld/Common/TargetOptionsCommandFlags.h +++ lld/include/lld/Common/TargetOptionsCommandFlags.h @@ -16,6 +16,7 @@ namespace lld { llvm::TargetOptions initTargetOptionsFromCodeGenFlags(); +llvm::Optional getRelocModelFromCMModel(); llvm::Optional getCodeModelFromCMModel(); std::string getCPUStr(); std::vector getMAttrs(); Index: lld/test/ELF/lto/relocation-model.ll =================================================================== --- lld/test/ELF/lto/relocation-model.ll +++ lld/test/ELF/lto/relocation-model.ll @@ -33,6 +33,15 @@ ; RUN: llvm-readobj -r %t-out.lto.o | FileCheck %s --check-prefix=PIC +;; Explicit flag. + +; RUN: ld.lld %t.o -o %t-out -save-temps -r -mllvm -relocation-model=pic +; RUN: llvm-readobj -r %t-out.lto.o | FileCheck %s --check-prefix=PIC + +; RUN: ld.lld %t.o -o %t-out -save-temps -r -mllvm -relocation-model=static +; RUN: llvm-readobj -r %t-out.lto.o | FileCheck %s --check-prefix=STATIC + + ; PIC: R_X86_64_REX_GOTPCRELX foo ; STATIC: R_X86_64_PC32 foo