[PATCH] D21779: [LTO] Infer EKind/EMachine from Bitcode files

Davide Italiano via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 27 19:03:20 PDT 2016


davide created this revision.
davide added a reviewer: rafael.
davide added a subscriber: llvm-commits.
Herald added a subscriber: mehdi_amini.

This is a proposed fix for https://llvm.org/bugs/show_bug.cgi?id=28268
I hope there's an easier way to get these informations because the current code is a little bit cumbersome :(

http://reviews.llvm.org/D21779

Files:
  ELF/InputFiles.cpp
  test/ELF/lto/pic.ll

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 [
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<InputFile> elf::createObjectFile(MemoryBufferRef MB,
                                                  StringRef ArchiveName) {
   std::unique_ptr<InputFile> F;
-  if (isBitcode(MB))
+  if (isBitcode(MB)) {
+    if (Config->EKind == ELFNoneKind)
+      setBitcodeMachineAttributes(MB);
     F.reset(new BitcodeFile(MB));
+  }
   else
     F = createELFFile<ObjectFile>(MB);
   F->ArchiveName = ArchiveName;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D21779.62049.patch
Type: text/x-patch
Size: 2384 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160628/00d46f8b/attachment.bin>


More information about the llvm-commits mailing list