Index: ELF/InputFiles.cpp =================================================================== --- ELF/InputFiles.cpp +++ ELF/InputFiles.cpp @@ -18,6 +18,7 @@ #include "llvm/IR/LLVMContext.h" #include "llvm/IR/Module.h" #include "llvm/Support/raw_ostream.h" +#include "llvm/Support/TargetRegistry.h" using namespace llvm; using namespace llvm::ELF; @@ -687,11 +688,47 @@ return identify_magic(MB.getBuffer()) == file_magic::bitcode; } +static void setBitcodeMachineAttributes(MemoryBufferRef MB) { + auto Obj = check(IRObjectFile::create(MB, Driver->Context)); + const Module &M = Obj->getModule(); + std::string TripleStr = M.getTargetTriple(); + Triple TheTriple = Triple(TripleStr); + std::string Msg; + const Target *T = TargetRegistry::lookupTarget(TripleStr, Msg); + if (!T) + fatal("target not found: " + Msg); + const DataLayout &DL = M.getDataLayout(); + bool IsLE = DL.isLittleEndian(); + bool Is64Bits = TheTriple.isArch64Bit(); + if (IsLE) + Config->EKind = (Is64Bits) ? ELF64LEKind : ELF32LEKind; + else + Config->EKind = (Is64Bits) ? ELF64BEKind : ELF32BEKind; + std::string ArchName = TheTriple.getArchName(); + if (ArchName == "aarch64") + Config->EMachine = EM_AARCH64; + if (ArchName == "arm") + Config->EMachine = EM_ARM; + if (ArchName == "i386") + Config->EMachine = EM_386; + if (ArchName == "mips") + Config->EMachine = EM_MIPS; + if (ArchName == "ppc") + Config->EMachine = EM_PPC; + if (ArchName == "ppc64") + Config->EMachine = EM_PPC64; + if (ArchName == "x86_64") + Config->EMachine = EM_X86_64; +} + std::unique_ptr elf::createObjectFile(MemoryBufferRef MB, StringRef ArchiveName) { std::unique_ptr F; - if (isBitcode(MB)) + if (isBitcode(MB)) { + if (Config->EKind == ELFNoneKind) + setBitcodeMachineAttributes(MB); F.reset(new BitcodeFile(MB)); + } else F = createELFFile(MB); F->ArchiveName = ArchiveName; Index: test/ELF/lto/pic.ll =================================================================== --- test/ELF/lto/pic.ll +++ test/ELF/lto/pic.ll @@ -1,7 +1,7 @@ ; REQUIRES: x86 ; RUN: llvm-as %s -o %t.o -; RUN: ld.lld %t.o -m elf_x86_64 -o %t.so -shared +; RUN: ld.lld %t.o -o %t.so -shared ; RUN: llvm-readobj -r %t.so | FileCheck %s ; CHECK: Relocations [