[PATCH] D13055: [ELF2] Handle -m option

Rafael EspĂ­ndola via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 5 05:56:36 PDT 2015


See the attached patch for example.

On 5 October 2015 at 08:51, Rafael EspĂ­ndola <rafael.espindola at gmail.com> wrote:
>> Returned back FirstObjName config variable since we need a flag indicating whether ELF parameters were grabbed from the first object file or from the -m option.
>
> Why?
>
> You report an error as soon as an incompatibility is found. If
> getFirstELF returns non null, you know either
>
> * There was a -m and it is compatible with it
> * There was no -m and you don't have to worry about it
>
> In any case you only have to check if that file is compatible with the
> current one.
>
> Cheers,
> Rafael
-------------- next part --------------
diff --git a/ELF/Config.h b/ELF/Config.h
index d9c3a4b..c536afa 100644
--- a/ELF/Config.h
+++ b/ELF/Config.h
@@ -11,12 +11,21 @@
 #define LLD_ELF_CONFIG_H
 
 #include "llvm/ADT/StringRef.h"
+#include "llvm/Support/ELF.h"
 
 #include <vector>
 
 namespace lld {
 namespace elf2 {
 
+enum ELFKind {
+  ELFNoneKind,
+  ELF32LEKind,
+  ELF32BEKind,
+  ELF64LEKind,
+  ELF64BEKind
+};
+
 struct Configuration {
   llvm::StringRef DynamicLinker;
   llvm::StringRef Entry;
@@ -37,6 +46,8 @@ struct Configuration {
   bool Shared;
   bool Static = false;
   bool WholeArchive = false;
+  ELFKind ElfKind = ELFNoneKind;
+  uint16_t EMachine = llvm::ELF::EM_NONE;
 };
 
 extern Configuration *Config;
diff --git a/ELF/Driver.cpp b/ELF/Driver.cpp
index 7921764..6c085cf 100644
--- a/ELF/Driver.cpp
+++ b/ELF/Driver.cpp
@@ -19,6 +19,7 @@
 #include "llvm/Support/Path.h"
 
 using namespace llvm;
+using namespace llvm::ELF;
 
 using namespace lld;
 using namespace lld::elf2;
@@ -34,6 +35,30 @@ void lld::elf2::link(ArrayRef<const char *> Args) {
   Driver->link(Args.slice(1));
 }
 
+static void setELFType(StringRef Emul) {
+  if (Emul == "elf_i386") {
+    Config->ElfKind = ELF32LEKind;
+    Config->EMachine = EM_386;
+    return;
+  }
+  if (Emul == "elf_x86_64") {
+    Config->ElfKind = ELF64LEKind;
+    Config->EMachine = EM_X86_64;
+    return;
+  }
+  if (Emul == "elf32ppc") {
+    Config->ElfKind = ELF32BEKind;
+    Config->EMachine = EM_PPC;
+    return;
+  }
+  if (Emul == "elf64ppc") {
+    Config->ElfKind = ELF64BEKind;
+    Config->EMachine = EM_PPC64;
+    return;
+  }
+  error(Twine("Unknown emulation: ") + Emul);
+}
+
 // Makes a path by concatenating Dir and File.
 // If Dir starts with '=' the result will be preceded by Sysroot,
 // which can be set with --sysroot command line switch.
@@ -67,6 +92,26 @@ static std::string searchLibrary(StringRef Path) {
   error(Twine("Unable to find library -l") + Path);
 }
 
+template <template <class> class T>
+std::unique_ptr<ELFFileBase> LinkerDriver::createELFFile(MemoryBufferRef MB) {
+  std::unique_ptr<ELFFileBase> File = ::createELFFile<T>(MB);
+  const ELFKind ElfKind = File->getELFKind();
+  const uint16_t EMachine = File->getEMachine();
+
+  // Grab target from the first input file if wasn't set by -m option.
+  if (Config->ElfKind == ELFNoneKind) {
+    Config->ElfKind = ElfKind;
+    Config->EMachine = EMachine;
+    return File;
+  }
+  if (ElfKind != Config->ElfKind || EMachine != Config->EMachine) {
+    const ELFFileBase *First = Symtab.getFirstELF();
+    error(Twine(MB.getBufferIdentifier() + " is incompatible with " +
+                (First ? First->getName() : "target architecture")));
+  }
+  return File;
+}
+
 // Opens and parses a file. Path has to be resolved already.
 // Newly created memory buffers are owned by this driver.
 void LinkerDriver::addFile(StringRef Path) {
@@ -126,6 +171,9 @@ void LinkerDriver::link(ArrayRef<const char *> ArgsArr) {
   if (auto *Arg = Args.getLastArg(OPT_soname))
     Config->SoName = Arg->getValue();
 
+  if (auto *Arg = Args.getLastArg(OPT_m))
+    setELFType(Arg->getValue());
+
   Config->AllowMultipleDefinition = Args.hasArg(OPT_allow_multiple_definition);
   Config->DiscardAll = Args.hasArg(OPT_discard_all);
   Config->DiscardLocals = Args.hasArg(OPT_discard_locals);
@@ -181,5 +229,7 @@ void LinkerDriver::link(ArrayRef<const char *> ArgsArr) {
   case ELF64BEKind:
     writeResult<object::ELF64BE>(&Symtab);
     return;
+  default:
+    llvm_unreachable("Invalid kind");
   }
 }
diff --git a/ELF/Driver.h b/ELF/Driver.h
index 94d2ff4..5de9811 100644
--- a/ELF/Driver.h
+++ b/ELF/Driver.h
@@ -38,6 +38,9 @@ public:
   void addFile(StringRef Path);
 
 private:
+  template <template <class> class T>
+  std::unique_ptr<ELFFileBase> createELFFile(MemoryBufferRef MB);
+
   SymbolTable Symtab;
   ArgParser Parser;
   std::vector<std::unique_ptr<MemoryBuffer>> OwningMBs;
diff --git a/ELF/InputFiles.cpp b/ELF/InputFiles.cpp
index 3ca4481..3dfe6b0 100644
--- a/ELF/InputFiles.cpp
+++ b/ELF/InputFiles.cpp
@@ -38,13 +38,9 @@ uint16_t ELFFileBase::getEMachine() const {
     return ::getEMachine<ELF64BE>(*this);
   case ELF64LEKind:
     return ::getEMachine<ELF64LE>(*this);
+  default:
+    llvm_unreachable("Invalid kind");
   }
-  llvm_unreachable("Invalid kind");
-}
-
-bool ELFFileBase::isCompatibleWith(const ELFFileBase &Other) const {
-  return getELFKind() == Other.getELFKind() &&
-         getEMachine() == Other.getEMachine();
 }
 
 namespace {
diff --git a/ELF/InputFiles.h b/ELF/InputFiles.h
index 80dff3f..f26be48 100644
--- a/ELF/InputFiles.h
+++ b/ELF/InputFiles.h
@@ -10,6 +10,7 @@
 #ifndef LLD_ELF_INPUT_FILES_H
 #define LLD_ELF_INPUT_FILES_H
 
+#include "Config.h"
 #include "InputSection.h"
 #include "Error.h"
 #include "Symbols.h"
@@ -49,8 +50,6 @@ private:
   const Kind FileKind;
 };
 
-enum ELFKind { ELF32LEKind, ELF32BEKind, ELF64LEKind, ELF64BEKind };
-
 class ELFFileBase : public InputFile {
 public:
   ELFFileBase(Kind K, ELFKind EKind, MemoryBufferRef M)
@@ -60,7 +59,6 @@ public:
     return K == ObjectKind || K == SharedKind;
   }
 
-  bool isCompatibleWith(const ELFFileBase &Other) const;
   ELFKind getELFKind() const { return EKind; }
 
   uint16_t getEMachine() const;
diff --git a/ELF/Options.td b/ELF/Options.td
index abdd439..ec73af9 100644
--- a/ELF/Options.td
+++ b/ELF/Options.td
@@ -41,6 +41,9 @@ def init : Separate<["-"], "init">, MetaVarName<"<symdol>">,
 def l : Joined<["-"], "l">, MetaVarName<"<libName>">,
   HelpText<"Root name of library to use">;
 
+def m : Separate<["-"], "m">,
+  HelpText<"Set target emulation">;
+
 def no_allow_shlib_undefined : Flag<["--"], "no-allow-shlib-undefined">;
 
 def no_whole_archive : Flag<["--"], "no-whole-archive">,
@@ -100,7 +103,6 @@ def enable_new_dtags : Flag<["--"], "enable-new-dtags">;
 def end_group : Flag<["--"], "end-group">;
 def gc_sections : Flag<["--"], "gc-sections">;
 def hash_style : Joined<["--"], "hash-style=">;
-def m : Separate<["-"], "m">;
 def no_add_needed : Flag<["--"], "no-add-needed">;
 def no_as_needed : Flag<["--"], "no-as-needed">;
 def no_fatal_warnings : Flag<["--"], "no-fatal-warnings">;
diff --git a/ELF/SymbolTable.cpp b/ELF/SymbolTable.cpp
index 14fcb35..961810f 100644
--- a/ELF/SymbolTable.cpp
+++ b/ELF/SymbolTable.cpp
@@ -84,6 +84,8 @@ void SymbolTable::addUndefinedSym(StringRef Name) {
   case ELF64BEKind:
     addUndefinedSym<ELF64BE>(Name);
     break;
+  default:
+    llvm_unreachable("Invalid kind");
   }
 }
 
@@ -136,12 +138,8 @@ template <class ELFT> void SymbolTable::init(uint16_t EMachine) {
 }
 
 template <class ELFT> void SymbolTable::addELFFile(ELFFileBase *File) {
-  if (const ELFFileBase *Old = getFirstELF()) {
-    if (!Old->isCompatibleWith(*File))
-      error(Twine(Old->getName() + " is incompatible with " + File->getName()));
-  } else {
+  if (!getFirstELF())
     init<ELFT>(File->getEMachine());
-  }
 
   if (auto *O = dyn_cast<ObjectFileBase>(File)) {
     ObjectFiles.emplace_back(O);
@@ -170,6 +168,8 @@ void SymbolTable::addELFFile(ELFFileBase *File) {
   case ELF64BEKind:
     addELFFile<ELF64BE>(File);
     break;
+  default:
+    llvm_unreachable("Invalid kind");
   }
 }
 
diff --git a/test/elf2/basic.s b/test/elf2/basic.s
index 325eb3f..c523dba 100644
--- a/test/elf2/basic.s
+++ b/test/elf2/basic.s
@@ -205,3 +205,6 @@ _start:
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
 # RUN: not lld -flavor gnu2 %t %t -o %t2 2>&1 | FileCheck --check-prefix=DUP %s
 # DUP: duplicate symbol: _start in {{.*}} and {{.*}}
+
+# RUN: not lld -flavor gnu2 %t -o %t -m wrong_emul 2>&1 | FileCheck --check-prefix=UNKNOWN_EMUL %s
+# UNKNOWN_EMUL: Unknown emulation: wrong_emul
diff --git a/test/elf2/incompatible.s b/test/elf2/incompatible.s
index 46a8df1..5596a60 100644
--- a/test/elf2/incompatible.s
+++ b/test/elf2/incompatible.s
@@ -5,22 +5,42 @@
 
 // RUN: not lld -flavor gnu2 %ta.o %tb.o -o %t 2>&1 | \
 // RUN:   FileCheck --check-prefix=A-AND-B %s
-// A-AND-B: a.o is incompatible with {{.*}}b.o
+// A-AND-B: b.o is incompatible with {{.*}}a.o
 
 // RUN: not lld -flavor gnu2 %tb.o %tc.o -o %t 2>&1 | \
 // RUN:   FileCheck --check-prefix=B-AND-C %s
-// B-AND-C: b.o is incompatible with {{.*}}c.o
+// B-AND-C: c.o is incompatible with {{.*}}b.o
 
 // RUN: not lld -flavor gnu2 %ta.o %ti686.so -o %t 2>&1 | \
 // RUN:   FileCheck --check-prefix=A-AND-SO %s
-// A-AND-SO: a.o is incompatible with {{.*}}i686.so
+// A-AND-SO: i686.so is incompatible with {{.*}}a.o
 
 // RUN: not lld -flavor gnu2 %tc.o %ti686.so -o %t 2>&1 | \
 // RUN:   FileCheck --check-prefix=C-AND-SO %s
-// C-AND-SO: c.o is incompatible with {{.*}}i686.so
+// C-AND-SO: i686.so is incompatible with {{.*}}c.o
 
 // RUN: not lld -flavor gnu2 %ti686.so %tc.o -o %t 2>&1 | \
 // RUN:   FileCheck --check-prefix=SO-AND-C %s
-// SO-AND-C: i686.so is incompatible with {{.*}}c.o
+// SO-AND-C: c.o is incompatible with {{.*}}i686.so
+
+// RUN: not lld -flavor gnu2 -m elf64ppc %ta.o -o %t 2>&1 | \
+// RUN:   FileCheck --check-prefix=A-ONLY %s
+// A-ONLY: a.o is incompatible with target architecture
+
+// RUN: not lld -flavor gnu2 -m elf64ppc %tb.o -o %t 2>&1 | \
+// RUN:   FileCheck --check-prefix=B-ONLY %s
+// B-ONLY: b.o is incompatible with target architecture
+
+// RUN: not lld -flavor gnu2 -m elf64ppc %tc.o -o %t 2>&1 | \
+// RUN:   FileCheck --check-prefix=C-ONLY %s
+// C-ONLY: c.o is incompatible with target architecture
+
+// RUN: not lld -flavor gnu2 -m elf_i386 %tc.o %ti686.so -o %t 2>&1 | \
+// RUN:   FileCheck --check-prefix=C-AND-SO-I386 %s
+// C-AND-SO-I386: c.o is incompatible with target architecture
+
+// RUN: not lld -flavor gnu2 -m elf_i386 %ti686.so %tc.o -o %t 2>&1 | \
+// RUN:   FileCheck --check-prefix=SO-AND-C-I386 %s
+// SO-AND-C-I386: c.o is incompatible with {{.*}}i686.so
 
 // REQUIRES: x86,arm


More information about the llvm-commits mailing list