[lld] r243993 - Create ObjectFile with the correct endian and word size.

Rafael Espindola rafael.espindola at gmail.com
Tue Aug 4 08:45:54 PDT 2015


Author: rafael
Date: Tue Aug  4 10:45:54 2015
New Revision: 243993

URL: http://llvm.org/viewvc/llvm-project?rev=243993&view=rev
Log:
Create ObjectFile with the correct endian and word size.

The writer is still hard coded to 64 bits le, but with this we can test for
invalid ELF files.

Added:
    lld/trunk/test/elf2/Inputs/
    lld/trunk/test/elf2/Inputs/invalid-data-encoding.elf
    lld/trunk/test/elf2/Inputs/invalid-file-class.elf
    lld/trunk/test/elf2/invalid-elf.test
Modified:
    lld/trunk/ELF/Driver.cpp
    lld/trunk/ELF/Driver.h

Modified: lld/trunk/ELF/Driver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Driver.cpp?rev=243993&r1=243992&r2=243993&view=diff
==============================================================================
--- lld/trunk/ELF/Driver.cpp (original)
+++ lld/trunk/ELF/Driver.cpp Tue Aug  4 10:45:54 2015
@@ -61,7 +61,22 @@ MemoryBufferRef LinkerDriver::openFile(S
 }
 
 static std::unique_ptr<InputFile> createFile(MemoryBufferRef MB) {
-  return make_unique<ObjectFile<object::ELF64LE>>(MB);
+  std::pair<unsigned char, unsigned char> Type =
+      object::getElfArchType(MB.getBuffer());
+  if (Type.second != ELF::ELFDATA2LSB && Type.second != ELF::ELFDATA2MSB)
+    error("Invalid data encoding");
+
+  if (Type.first == ELF::ELFCLASS32) {
+    if (Type.second == ELF::ELFDATA2LSB)
+      return make_unique<ObjectFile<object::ELF32LE>>(MB);
+    return make_unique<ObjectFile<object::ELF32BE>>(MB);
+  }
+  if (Type.first == ELF::ELFCLASS64) {
+    if (Type.second == ELF::ELFDATA2LSB)
+      return make_unique<ObjectFile<object::ELF64LE>>(MB);
+    return make_unique<ObjectFile<object::ELF64BE>>(MB);
+  }
+  error("Invalid file class");
 }
 
 void LinkerDriver::link(ArrayRef<const char *> ArgsArr) {

Modified: lld/trunk/ELF/Driver.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Driver.h?rev=243993&r1=243992&r2=243993&view=diff
==============================================================================
--- lld/trunk/ELF/Driver.h (original)
+++ lld/trunk/ELF/Driver.h Tue Aug  4 10:45:54 2015
@@ -24,7 +24,7 @@ class InputFile;
 // Entry point of the ELF linker.
 void link(ArrayRef<const char *> Args);
 
-void error(Twine Msg);
+LLVM_ATTRIBUTE_NORETURN void error(Twine Msg);
 void error(std::error_code EC, Twine Prefix);
 void error(std::error_code EC);
 template <typename T> void error(const ErrorOr<T> &V, Twine Prefix) {

Added: lld/trunk/test/elf2/Inputs/invalid-data-encoding.elf
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf2/Inputs/invalid-data-encoding.elf?rev=243993&view=auto
==============================================================================
Binary files lld/trunk/test/elf2/Inputs/invalid-data-encoding.elf (added) and lld/trunk/test/elf2/Inputs/invalid-data-encoding.elf Tue Aug  4 10:45:54 2015 differ

Added: lld/trunk/test/elf2/Inputs/invalid-file-class.elf
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf2/Inputs/invalid-file-class.elf?rev=243993&view=auto
==============================================================================
Binary files lld/trunk/test/elf2/Inputs/invalid-file-class.elf (added) and lld/trunk/test/elf2/Inputs/invalid-file-class.elf Tue Aug  4 10:45:54 2015 differ

Added: lld/trunk/test/elf2/invalid-elf.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf2/invalid-elf.test?rev=243993&view=auto
==============================================================================
--- lld/trunk/test/elf2/invalid-elf.test (added)
+++ lld/trunk/test/elf2/invalid-elf.test Tue Aug  4 10:45:54 2015
@@ -0,0 +1,7 @@
+RUN: not lld -flavor gnu2 %p/Inputs/invalid-data-encoding.elf -o %t2 2>&1 | \
+RUN:   FileCheck --check-prefix=INVALID-DATA-ENC %s
+INVALID-DATA-ENC: Invalid data encoding
+
+RUN: not lld -flavor gnu2 %p/Inputs/invalid-file-class.elf -o %t2 2>&1 | \
+RUN:   FileCheck --check-prefix=INVALID-FILE-CLASS %s
+INVALID-FILE-CLASS: Invalid file class





More information about the llvm-commits mailing list