[lld] r241719 - COFF: Implement dllimported symbol name mangling.

Rui Ueyama ruiu at google.com
Wed Jul 8 13:22:51 PDT 2015


Author: ruiu
Date: Wed Jul  8 15:22:50 2015
New Revision: 241719

URL: http://llvm.org/viewvc/llvm-project?rev=241719&view=rev
Log:
COFF: Implement dllimported symbol name mangling.

Symbols exported by DLLs are listed in import library files.
Exported names may be mangled by "Import Name Type" field as
described in PE/COFF spec 7.3. This patch implements that
mangling scheme.

Added:
    lld/trunk/test/COFF/Inputs/imports-mangle.lib
    lld/trunk/test/COFF/imports-mangle.test
Modified:
    lld/trunk/COFF/InputFiles.cpp

Modified: lld/trunk/COFF/InputFiles.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/InputFiles.cpp?rev=241719&r1=241718&r2=241719&view=diff
==============================================================================
--- lld/trunk/COFF/InputFiles.cpp (original)
+++ lld/trunk/COFF/InputFiles.cpp Wed Jul  8 15:22:50 2015
@@ -20,12 +20,9 @@
 #include "llvm/Support/raw_ostream.h"
 #include <mutex>
 
+using namespace llvm::COFF;
 using namespace llvm::object;
 using namespace llvm::support::endian;
-using llvm::COFF::ImportHeader;
-using llvm::COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE;
-using llvm::COFF::IMAGE_FILE_MACHINE_AMD64;
-using llvm::COFF::IMAGE_FILE_MACHINE_UNKNOWN;
 using llvm::RoundUpToAlignment;
 using llvm::sys::fs::identify_magic;
 using llvm::sys::fs::file_magic;
@@ -260,11 +257,23 @@ std::error_code ImportFile::parse() {
   StringRef Name = StringAlloc.save(StringRef(Buf + sizeof(*Hdr)));
   StringRef ImpName = StringAlloc.save(Twine("__imp_") + Name);
   StringRef DLLName(Buf + sizeof(coff_import_header) + Name.size() + 1);
-  StringRef ExternalName = Name;
-  if (Hdr->getNameType() == llvm::COFF::IMPORT_ORDINAL)
-    ExternalName = "";
-  auto *ImpSym = new (Alloc) DefinedImportData(DLLName, ImpName, ExternalName,
-                                               Hdr);
+  StringRef ExtName;
+  switch (Hdr->getNameType()) {
+  case IMPORT_ORDINAL:
+    ExtName = "";
+    break;
+  case IMPORT_NAME:
+    ExtName = Name;
+    break;
+  case IMPORT_NAME_NOPREFIX:
+    ExtName = Name.ltrim("?@_");
+    break;
+  case IMPORT_NAME_UNDECORATE:
+    ExtName = Name.ltrim("?@_");
+    ExtName = ExtName.substr(0, ExtName.find('@'));
+    break;
+  }
+  auto *ImpSym = new (Alloc) DefinedImportData(DLLName, ImpName, ExtName, Hdr);
   SymbolBodies.push_back(ImpSym);
 
   // If type is function, we need to create a thunk which jump to an

Added: lld/trunk/test/COFF/Inputs/imports-mangle.lib
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/COFF/Inputs/imports-mangle.lib?rev=241719&view=auto
==============================================================================
Binary files lld/trunk/test/COFF/Inputs/imports-mangle.lib (added) and lld/trunk/test/COFF/Inputs/imports-mangle.lib Wed Jul  8 15:22:50 2015 differ

Added: lld/trunk/test/COFF/imports-mangle.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/COFF/imports-mangle.test?rev=241719&view=auto
==============================================================================
--- lld/trunk/test/COFF/imports-mangle.test (added)
+++ lld/trunk/test/COFF/imports-mangle.test Wed Jul  8 15:22:50 2015
@@ -0,0 +1,66 @@
+# RUN: yaml2obj < %s > %t.obj
+# RUN: lld -flavor link2 /out:%t.exe /opt:noref /entry:main \
+# RUN:   %t.obj %p/Inputs/imports-mangle.lib
+# RUN: llvm-readobj -coff-imports %t.exe | FileCheck %s
+
+# CHECK: Import {
+# CHECK:   Symbol: sym4 (0)
+# CHECK:   Symbol: sym3 (1)
+# CHECK:   Symbol: sym1 (2)
+# CHECK:   Symbol:  (2)
+# CHECK: }
+
+---
+header:
+  Machine:         IMAGE_FILE_MACHINE_AMD64
+  Characteristics: []
+sections:
+  - Name:            .text
+    Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     000000000000
+symbols:
+  - Name:            .text
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          6
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      CheckSum:        0
+      Number:          0
+      Selection:       IMAGE_COMDAT_SELECT_ANY
+  - Name:            main
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_FUNCTION
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+  - Name:            sym1
+    Value:           0
+    SectionNumber:   0
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_FUNCTION
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+  - Name:            sym2
+    Value:           0
+    SectionNumber:   0
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_FUNCTION
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+  - Name:            _sym3
+    Value:           0
+    SectionNumber:   0
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_FUNCTION
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+  - Name:            '?sym4@@YAHH at Z'
+    Value:           0
+    SectionNumber:   0
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_FUNCTION
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+...





More information about the llvm-commits mailing list