diff --git a/llvm/lib/Target/PowerPC/PPCTargetMachine.cpp b/llvm/lib/Target/PowerPC/PPCTargetMachine.cpp --- a/llvm/lib/Target/PowerPC/PPCTargetMachine.cpp +++ b/llvm/lib/Target/PowerPC/PPCTargetMachine.cpp @@ -223,6 +223,10 @@ static Reloc::Model getEffectiveRelocModel(const Triple &TT, Optional RM) { + // AIX only supports PIC, RM option is ignored. + if (TT.isOSAIX()) + return Reloc::PIC_; + if (RM.hasValue()) return *RM; diff --git a/llvm/unittests/CodeGen/AIXRelocModelTest.cpp b/llvm/unittests/CodeGen/AIXRelocModelTest.cpp new file mode 100644 --- /dev/null +++ b/llvm/unittests/CodeGen/AIXRelocModelTest.cpp @@ -0,0 +1,25 @@ +#include "llvm/ADT/Triple.h" +#include "llvm/Support/TargetRegistry.h" +#include "llvm/Target/TargetMachine.h" +#include "gtest/gtest.h" + +using namespace llvm; + +TEST(AIXRelocModelTest, ForceDefalutToPIC) { + Triple TheTriple(/*ArchStr*/ "powerpc", /*VendorStr*/ "", /*OSStr*/ "aix"); + std::string Error; + const Target *TheTarget = TargetRegistry::lookupTarget("", TheTriple, Error); + ASSERT_TRUE(TheTarget) << Error; + + TargetOptions Options; + // Create a TargetMachine for powerpc-aix target, and deliberately try to set + // its relocation model to static. + std::unique_ptr Target(TheTarget->createTargetMachine( + /*TT*/ TheTriple.getTriple(), /*CPU*/ "", /*Features*/ "", + /*Options*/ Options, /*RM*/ Reloc::Static, /*CM*/ None, + /*OL*/ CodeGenOpt::Default)); + ASSERT_TRUE(Target) << "Could not allocate target machine!"; + + // The relocation model on AIX should be force to PIC regardeless. + EXPECT_TRUE(Target->getRelocationModel() == Reloc::PIC_); +} diff --git a/llvm/unittests/CodeGen/CMakeLists.txt b/llvm/unittests/CodeGen/CMakeLists.txt --- a/llvm/unittests/CodeGen/CMakeLists.txt +++ b/llvm/unittests/CodeGen/CMakeLists.txt @@ -14,6 +14,7 @@ add_llvm_unittest(CodeGenTests AArch64SelectionDAGTest.cpp + AIXRelocModelTest.cpp DIEHashTest.cpp LowLevelTypeTest.cpp MachineInstrBundleIteratorTest.cpp