[lld] r185516 - [PECOFF] Handle hint field in Hint/Name table in the import library.

Rui Ueyama ruiu at google.com
Tue Jul 2 23:09:33 PDT 2013


Author: ruiu
Date: Wed Jul  3 01:09:33 2013
New Revision: 185516

URL: http://llvm.org/viewvc/llvm-project?rev=185516&view=rev
Log:
[PECOFF] Handle hint field in Hint/Name table in the import library.

A hint is an index of the export pointer table in a DLL, at which
PE/COFF loader starts looking for a symbol name. The import library
comes with hints and symbol pairs, and as long as hints are in sync
with the actual symbol table in DLL, the symbols will be resolved
quickly. So, we shouldn't ignore hints but propagate them to an output.

Modified:
    lld/trunk/lib/ReaderWriter/PECOFF/Atoms.h
    lld/trunk/lib/ReaderWriter/PECOFF/ReaderImportHeader.cpp

Modified: lld/trunk/lib/ReaderWriter/PECOFF/Atoms.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/PECOFF/Atoms.h?rev=185516&r1=185515&r2=185516&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/PECOFF/Atoms.h (original)
+++ lld/trunk/lib/ReaderWriter/PECOFF/Atoms.h Wed Jul  3 01:09:33 2013
@@ -219,7 +219,7 @@ private:
 /// A COFFLinkerInternalAtom represents a defined atom created by the linker,
 /// not read from file.
 class COFFLinkerInternalAtom : public COFFBaseDefinedAtom {
-  public:
+public:
   virtual uint64_t ordinal() const { return 0; }
   virtual Scope scope() const { return scopeGlobal; }
   virtual Alignment alignment() const { return Alignment(1); }
@@ -235,24 +235,37 @@ private:
   std::vector<uint8_t> *_data;
 };
 
+// A COFFSharedLibraryAtom represents a symbol for data in an import library.  A
+// reference to a COFFSharedLibraryAtom will be transformed to a real reference
+// to an import address table entry in a pass.
 class COFFSharedLibraryAtom : public SharedLibraryAtom {
 public:
-  COFFSharedLibraryAtom(const File &file, StringRef symbolName,
-                        StringRef originalName, StringRef loadName)
-      : _file(file), _symbolName(symbolName), _loadName(loadName),
-        _originalName(originalName) {}
+  COFFSharedLibraryAtom(const File &file, uint16_t hint,
+                        StringRef symbolName, StringRef loadName)
+      : _file(file), _hint(hint), _unmangledName(symbolName),
+        _loadName(loadName), _mangledName(addImpPrefix(symbolName)) {}
 
   virtual const File &file() const { return _file; }
-  virtual StringRef name() const { return _symbolName; }
+  uint16_t hint() const { return _hint; }
+  virtual StringRef name() const { return _mangledName; }
+  virtual StringRef unmangledName() const { return _unmangledName; }
   virtual StringRef loadName() const { return _loadName; }
   virtual bool canBeNullAtRuntime() const { return false; }
-  virtual StringRef originalName() const { return _originalName; }
 
 private:
+  /// Mangle the symbol name by adding "__imp_" prefix. See the file comment of
+  /// ReaderImportHeader.cpp for details about the prefix.
+  std::string addImpPrefix(StringRef symbolName) {
+    std::string ret("__imp_");
+    ret.append(symbolName);
+    return std::move(ret);
+  }
+
   const File &_file;
-  StringRef _symbolName;
+  uint16_t _hint;
+  StringRef _unmangledName;
   StringRef _loadName;
-  StringRef _originalName;
+  std::string _mangledName;
 };
 
 //===----------------------------------------------------------------------===//

Modified: lld/trunk/lib/ReaderWriter/PECOFF/ReaderImportHeader.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/PECOFF/ReaderImportHeader.cpp?rev=185516&r1=185515&r2=185516&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/PECOFF/ReaderImportHeader.cpp (original)
+++ lld/trunk/lib/ReaderWriter/PECOFF/ReaderImportHeader.cpp Wed Jul  3 01:09:33 2013
@@ -178,11 +178,12 @@ public:
       return;
     }
 
+    uint16_t hint = *reinterpret_cast<const support::ulittle16_t *>(buf + 16);
     StringRef symbolName(buf + 20);
     StringRef dllName(buf + 20 + symbolName.size() + 1);
 
-    const COFFSharedLibraryAtom *dataAtom = addSharedLibraryAtom(symbolName,
-                                                                 dllName);
+    const COFFSharedLibraryAtom *dataAtom = addSharedLibraryAtom(
+        hint, symbolName, dllName);
     int type = *reinterpret_cast<const support::ulittle16_t *>(buf + 18) >> 16;
     if (type == llvm::COFF::IMPORT_CODE)
       addDefinedAtom(symbolName, dllName, dataAtom);
@@ -209,19 +210,17 @@ public:
   virtual const TargetInfo &getTargetInfo() const { return _targetInfo; }
 
 private:
-  const COFFSharedLibraryAtom *addSharedLibraryAtom(StringRef symbolName,
-                                                    StringRef dllName) {
-    auto *name = new (allocator.Allocate<std::string>()) std::string("__imp_");
-    name->append(symbolName);
-    auto *atom = new (allocator.Allocate<COFFSharedLibraryAtom>())
-        COFFSharedLibraryAtom(*this, *name, symbolName, dllName);
+  const COFFSharedLibraryAtom *addSharedLibraryAtom(
+      uint16_t hint, StringRef symbolName, StringRef dllName) {
+    auto *atom = new (_allocator.Allocate<COFFSharedLibraryAtom>())
+        COFFSharedLibraryAtom(*this, hint, symbolName, dllName);
     _sharedLibraryAtoms._atoms.push_back(atom);
     return atom;
   }
 
   void addDefinedAtom(StringRef symbolName, StringRef dllName,
                       const COFFSharedLibraryAtom *dataAtom) {
-    auto *atom = new (allocator.Allocate<FuncAtom>())
+    auto *atom = new (_allocator.Allocate<FuncAtom>())
         FuncAtom(*this, symbolName);
 
     // The first two byte of the atom is JMP instruction.
@@ -233,7 +232,7 @@ private:
   atom_collection_vector<DefinedAtom> _definedAtoms;
   atom_collection_vector<SharedLibraryAtom> _sharedLibraryAtoms;
   const TargetInfo &_targetInfo;
-  mutable llvm::BumpPtrAllocator allocator;
+  mutable llvm::BumpPtrAllocator _allocator;
 };
 
 } // end anonymous namespace





More information about the llvm-commits mailing list