[lld] r243195 - COFF: Split ImportThunkChunk into x86 and x64. NFC.

Rui Ueyama ruiu at google.com
Fri Jul 24 18:16:07 PDT 2015


Author: ruiu
Date: Fri Jul 24 20:16:06 2015
New Revision: 243195

URL: http://llvm.org/viewvc/llvm-project?rev=243195&view=rev
Log:
COFF: Split ImportThunkChunk into x86 and x64. NFC.

This change should make it easy to port this code to ARM.

Modified:
    lld/trunk/COFF/Chunks.cpp
    lld/trunk/COFF/Chunks.h
    lld/trunk/COFF/InputFiles.cpp
    lld/trunk/COFF/Symbols.cpp
    lld/trunk/COFF/Symbols.h

Modified: lld/trunk/COFF/Chunks.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Chunks.cpp?rev=243195&r1=243194&r2=243195&view=diff
==============================================================================
--- lld/trunk/COFF/Chunks.cpp (original)
+++ lld/trunk/COFF/Chunks.cpp Fri Jul 24 20:16:06 2015
@@ -245,31 +245,27 @@ void StringChunk::writeTo(uint8_t *Buf)
   memcpy(Buf + FileOff, Str.data(), Str.size());
 }
 
-ImportThunkChunk::ImportThunkChunk(Defined *S) : ImpSymbol(S) {
+ImportThunkChunkX64::ImportThunkChunkX64(Defined *S) : ImpSymbol(S) {
   // Intel Optimization Manual says that all branch targets
   // should be 16-byte aligned. MSVC linker does this too.
   if (Config->MachineType == AMD64)
       Align = 16;
 }
 
-void ImportThunkChunk::getBaserels(std::vector<uint32_t> *Res) {
-  if (Config->MachineType == I386)
-    Res->push_back(getRVA() + 2);
+void ImportThunkChunkX64::writeTo(uint8_t *Buf) {
+  memcpy(Buf + FileOff, ImportThunkX86, sizeof(ImportThunkX86));
+  // The first two bytes is a JMP instruction. Fill its operand.
+  write32le(Buf + FileOff + 2, ImpSymbol->getRVA() - RVA - getSize());
+}
+
+void ImportThunkChunkX86::getBaserels(std::vector<uint32_t> *Res) {
+  Res->push_back(getRVA() + 2);
 }
 
-void ImportThunkChunk::writeTo(uint8_t *Buf) {
-  memcpy(Buf + FileOff, ImportThunkData, sizeof(ImportThunkData));
+void ImportThunkChunkX86::writeTo(uint8_t *Buf) {
+  memcpy(Buf + FileOff, ImportThunkX86, sizeof(ImportThunkX86));
   // The first two bytes is a JMP instruction. Fill its operand.
-  switch (Config->MachineType) {
-  case AMD64:
-    write32le(Buf + FileOff + 2, ImpSymbol->getRVA() - RVA - getSize());
-    break;
-  case I386:
-    write32le(Buf + FileOff + 2, ImpSymbol->getRVA() + Config->ImageBase);
-    break;
-  default:
-    llvm_unreachable("unsupported machine type");
-  }
+  write32le(Buf + FileOff + 2, ImpSymbol->getRVA() + Config->ImageBase);
 }
 
 void LocalImportChunk::getBaserels(std::vector<uint32_t> *Res) {

Modified: lld/trunk/COFF/Chunks.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Chunks.h?rev=243195&r1=243194&r2=243195&view=diff
==============================================================================
--- lld/trunk/COFF/Chunks.h (original)
+++ lld/trunk/COFF/Chunks.h Fri Jul 24 20:16:06 2015
@@ -223,17 +223,27 @@ private:
   StringRef Str;
 };
 
-static const uint8_t ImportThunkData[] = {
+static const uint8_t ImportThunkX86[] = {
     0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // JMP *0x0
 };
 
 // Windows-specific.
 // A chunk for DLL import jump table entry. In a final output, it's
 // contents will be a JMP instruction to some __imp_ symbol.
-class ImportThunkChunk : public Chunk {
+class ImportThunkChunkX64 : public Chunk {
 public:
-  explicit ImportThunkChunk(Defined *ImpSymbol);
-  size_t getSize() const override { return sizeof(ImportThunkData); }
+  explicit ImportThunkChunkX64(Defined *S);
+  size_t getSize() const override { return sizeof(ImportThunkX86); }
+  void writeTo(uint8_t *Buf) override;
+
+private:
+  Defined *ImpSymbol;
+};
+
+class ImportThunkChunkX86 : public Chunk {
+public:
+  explicit ImportThunkChunkX86(Defined *S) : ImpSymbol(S) {}
+  size_t getSize() const override { return sizeof(ImportThunkX86); }
   void getBaserels(std::vector<uint32_t> *Res) override;
   void writeTo(uint8_t *Buf) override;
 

Modified: lld/trunk/COFF/InputFiles.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/InputFiles.cpp?rev=243195&r1=243194&r2=243195&view=diff
==============================================================================
--- lld/trunk/COFF/InputFiles.cpp (original)
+++ lld/trunk/COFF/InputFiles.cpp Fri Jul 24 20:16:06 2015
@@ -320,8 +320,10 @@ std::error_code ImportFile::parse() {
   // If type is function, we need to create a thunk which jump to an
   // address pointed by the __imp_ symbol. (This allows you to call
   // DLL functions just like regular non-DLL functions.)
-  if (Hdr->getType() == llvm::COFF::IMPORT_CODE)
-    SymbolBodies.push_back(new (Alloc) DefinedImportThunk(Name, ImpSym));
+  if (Hdr->getType() == llvm::COFF::IMPORT_CODE) {
+    auto *B = new (Alloc) DefinedImportThunk(Name, ImpSym, Hdr->Machine);
+    SymbolBodies.push_back(B);
+  }
   return std::error_code();
 }
 

Modified: lld/trunk/COFF/Symbols.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Symbols.cpp?rev=243195&r1=243194&r2=243195&view=diff
==============================================================================
--- lld/trunk/COFF/Symbols.cpp (original)
+++ lld/trunk/COFF/Symbols.cpp Fri Jul 24 20:16:06 2015
@@ -196,6 +196,21 @@ COFFSymbolRef DefinedCOFF::getCOFFSymbol
   return COFFSymbolRef(reinterpret_cast<const coff_symbol32 *>(Sym));
 }
 
+DefinedImportThunk::DefinedImportThunk(StringRef Name, DefinedImportData *S,
+                                       uint16_t MachineType)
+    : Defined(DefinedImportThunkKind, Name) {
+  switch (MachineType) {
+  case AMD64:
+    Data.reset(new ImportThunkChunkX64(S));
+    return;
+  case I386:
+    Data.reset(new ImportThunkChunkX86(S));
+    return;
+  default:
+    llvm_unreachable("unknown machine type");
+  }
+}
+
 ErrorOr<std::unique_ptr<InputFile>> Lazy::getMember() {
   auto MBRefOrErr = File->getMember(&Sym);
   if (auto EC = MBRefOrErr.getError())

Modified: lld/trunk/COFF/Symbols.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Symbols.h?rev=243195&r1=243194&r2=243195&view=diff
==============================================================================
--- lld/trunk/COFF/Symbols.h (original)
+++ lld/trunk/COFF/Symbols.h Fri Jul 24 20:16:06 2015
@@ -327,20 +327,19 @@ private:
 // a regular name. A function pointer is given as a DefinedImportData.
 class DefinedImportThunk : public Defined {
 public:
-  DefinedImportThunk(StringRef N, DefinedImportData *S)
-      : Defined(DefinedImportThunkKind, N), Data(S) {}
+  DefinedImportThunk(StringRef Name, DefinedImportData *S,
+                     uint16_t MachineType);
 
   static bool classof(const SymbolBody *S) {
     return S->kind() == DefinedImportThunkKind;
   }
 
-  uint64_t getRVA() { return Data.getRVA(); }
-  uint64_t getFileOff() { return Data.getFileOff(); }
-
-  Chunk *getChunk() { return &Data; }
+  uint64_t getRVA() { return Data->getRVA(); }
+  uint64_t getFileOff() { return Data->getFileOff(); }
+  Chunk *getChunk() { return Data.get(); }
 
 private:
-  ImportThunkChunk Data;
+  std::unique_ptr<Chunk> Data;
 };
 
 // If you have a symbol "__imp_foo" in your object file, a symbol name





More information about the llvm-commits mailing list