[lld] r188154 - [PECOFF] Trim at most one character from imported symbols.

Rui Ueyama ruiu at google.com
Sun Aug 11 21:10:00 PDT 2013


Author: ruiu
Date: Sun Aug 11 23:10:00 2013
New Revision: 188154

URL: http://llvm.org/viewvc/llvm-project?rev=188154&view=rev
Log:
[PECOFF] Trim at most one character from imported symbols.

The import name is not always the same as the symbol name. If the name/type
field in the import header is NOPREFIX or UNDECORATE, we need to strip some
characters from symbol to get its import name.

The Microsoft PE/COFF spec is vague if symbol contains more than two
consecutive characters to be stripped. We used to strip all characters,
but it doesn't seem right as we couldn't link against the system library
because of this name mangling. Looks like we shouldn't strip more than one
character.

Modified:
    lld/trunk/lib/ReaderWriter/PECOFF/ReaderImportHeader.cpp
    lld/trunk/test/pecoff/Inputs/vars-main.c
    lld/trunk/test/pecoff/Inputs/vars-main.obj.yaml
    lld/trunk/test/pecoff/Inputs/vars.c
    lld/trunk/test/pecoff/Inputs/vars.dll.yaml
    lld/trunk/test/pecoff/Inputs/vars.lib
    lld/trunk/test/pecoff/importlib.test

Modified: lld/trunk/lib/ReaderWriter/PECOFF/ReaderImportHeader.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/PECOFF/ReaderImportHeader.cpp?rev=188154&r1=188153&r2=188154&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/PECOFF/ReaderImportHeader.cpp (original)
+++ lld/trunk/lib/ReaderWriter/PECOFF/ReaderImportHeader.cpp Sun Aug 11 23:10:00 2013
@@ -266,6 +266,14 @@ private:
   const LinkingContext &_context;
   mutable llvm::BumpPtrAllocator _alloc;
 
+  // Does the same thing as StringRef::ltrim() but removes at most one
+  // character.
+  StringRef ltrim1(StringRef str, const char *chars) const {
+    if (!str.empty() && strchr(chars, str[0]))
+      return str.substr(1);
+    return str;
+  }
+
   // Convert the given symbol name to the import symbol name exported by the
   // DLL.
   StringRef symbolNameToImportName(StringRef symbolName, int nameType) const {
@@ -280,11 +288,11 @@ private:
       return symbolName;
     case llvm::COFF::IMPORT_NAME_NOPREFIX:
       // The import name is the symbol name without leading ?, @ or _.
-      ret = symbolName.ltrim("?@_");
+      ret = ltrim1(symbolName, "?@_");
       break;
     case llvm::COFF::IMPORT_NAME_UNDECORATE:
       // Similar to NOPREFIX, but we also need to truncate at the first @.
-      ret = symbolName.ltrim("?@_");
+      ret = ltrim1(symbolName, "?@_");
       ret = ret.substr(0, ret.find('@'));
       break;
     }

Modified: lld/trunk/test/pecoff/Inputs/vars-main.c
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/pecoff/Inputs/vars-main.c?rev=188154&r1=188153&r2=188154&view=diff
==============================================================================
--- lld/trunk/test/pecoff/Inputs/vars-main.c (original)
+++ lld/trunk/test/pecoff/Inputs/vars-main.c Sun Aug 11 23:10:00 2013
@@ -1,6 +1,7 @@
 __declspec(dllimport) int var;
 __declspec(dllimport) int fn(void);
+__declspec(dllimport) int _name_with_underscore(void);
 
 int main() {
-  return var + fn();
+  return var + fn() + _name_with_underscore();
 }

Modified: lld/trunk/test/pecoff/Inputs/vars-main.obj.yaml
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/pecoff/Inputs/vars-main.obj.yaml?rev=188154&r1=188153&r2=188154&view=diff
==============================================================================
--- lld/trunk/test/pecoff/Inputs/vars-main.obj.yaml (original)
+++ lld/trunk/test/pecoff/Inputs/vars-main.obj.yaml Sun Aug 11 23:10:00 2013
@@ -3,65 +3,44 @@ header:
   Machine:         IMAGE_FILE_MACHINE_I386
   Characteristics: [  ]
 sections:
-  - Name:            .drectve
-    Characteristics: [ IMAGE_SCN_LNK_INFO, IMAGE_SCN_LNK_REMOVE ]
-    Alignment:       1
-    SectionData:     00000000
-  - Name:            ".debug$S"
-    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_DISCARDABLE, IMAGE_SCN_MEM_READ ]
-    Alignment:       1
-    SectionData:     04000000F10000005500000017000111000000005A3A5C766172732D6D61696E2E6F626A003A003C11002200000700100000001B9D0100100000001B9D01004D6963726F736F667420285229204F7074696D697A696E6720436F6D70696C657200000000
   - Name:            .text
     Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
     Alignment:       16
-    SectionData:     558BECFF15000000008B0D0000000003015DC3
+    SectionData:     558BEC56FF15000000008B0D000000008B3103F0FF150000000003C65E5DC3
     Relocations:
-      - VirtualAddress:  5
+      - VirtualAddress:  6
         SymbolName:      __imp__fn
         Type:            IMAGE_REL_I386_DIR32
-      - VirtualAddress:  11
+      - VirtualAddress:  12
         SymbolName:      __imp__var
         Type:            IMAGE_REL_I386_DIR32
+      - VirtualAddress:  22
+        SymbolName:      __imp___name_with_underscore
+        Type:            IMAGE_REL_I386_DIR32
 symbols:
-  - Name:            "@comp.id"
-    Value:           11181339
-    SectionNumber:   65535
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_STATIC
-  - Name:            "@feat.00"
-    Value:           1
-    SectionNumber:   65535
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_STATIC
-  - Name:            .drectve
+  - Name:            .text
     Value:           0
     SectionNumber:   1
     SimpleType:      IMAGE_SYM_TYPE_NULL
     ComplexType:     IMAGE_SYM_DTYPE_NULL
     StorageClass:    IMAGE_SYM_CLASS_STATIC
     NumberOfAuxSymbols: 1
-    AuxiliaryData:   2F0000000000000000000000000000000000
-  - Name:            ".debug$S"
+    AuxiliaryData:   1F000000030000008C7450D6000000000000
+  - Name:            __imp__fn
     Value:           0
-    SectionNumber:   2
+    SectionNumber:   0
     SimpleType:      IMAGE_SYM_TYPE_NULL
     ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_STATIC
-    NumberOfAuxSymbols: 1
-    AuxiliaryData:   640000000000000000000000000000000000
-  - Name:            .text
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+  - Name:            __imp___name_with_underscore
     Value:           0
-    SectionNumber:   3
+    SectionNumber:   0
     SimpleType:      IMAGE_SYM_TYPE_NULL
     ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_STATIC
-    NumberOfAuxSymbols: 1
-    AuxiliaryData:   1300000002000000B8433CE4000000000000
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
   - Name:            _main
     Value:           0
-    SectionNumber:   3
+    SectionNumber:   1
     SimpleType:      IMAGE_SYM_TYPE_NULL
     ComplexType:     IMAGE_SYM_DTYPE_FUNCTION
     StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
@@ -69,12 +48,6 @@ symbols:
     Value:           0
     SectionNumber:   0
     SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
-  - Name:            __imp__fn
-    Value:           0
-    SectionNumber:   0
-    SimpleType:      IMAGE_SYM_TYPE_NULL
     ComplexType:     IMAGE_SYM_DTYPE_NULL
     StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
 ...

Modified: lld/trunk/test/pecoff/Inputs/vars.c
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/pecoff/Inputs/vars.c?rev=188154&r1=188153&r2=188154&view=diff
==============================================================================
--- lld/trunk/test/pecoff/Inputs/vars.c (original)
+++ lld/trunk/test/pecoff/Inputs/vars.c Sun Aug 11 23:10:00 2013
@@ -1,5 +1,6 @@
 // cl.exe /c vars.c
-// link /debug /dll /nodefaultlib /entry:dllmain /export:var, at 1,NONAME,DATA /export:fn vars.obj
+// link /dll /nodefaultlib /entry:dllmain /export:var, at 1,NONAME,DATA \
+//   /export:fn /export:_name_with_underscore vars.obj
 
 // will be exported by ordinal
 int var = 3;
@@ -9,6 +10,11 @@ int fn(void) {
   return 4;
 }
 
+// will be exported by name
+int _name_with_underscore(void) {
+  return 5;
+}
+
 int dllmain() {
   return 1;
 }

Modified: lld/trunk/test/pecoff/Inputs/vars.dll.yaml
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/pecoff/Inputs/vars.dll.yaml?rev=188154&r1=188153&r2=188154&view=diff
==============================================================================
--- lld/trunk/test/pecoff/Inputs/vars.dll.yaml (original)
+++ lld/trunk/test/pecoff/Inputs/vars.dll.yaml Sun Aug 11 23:10:00 2013
@@ -6,11 +6,11 @@ sections:
   - Name:            .text
     Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
     Alignment:       2147483648
-    SectionData:     558BECB8090000005DC3CCCCCCCCCCCC558BECB8010000005DC3000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000!
 000000000
 000000000000000000000000000000000000000000000000
+    SectionData:     558BECB8040000005DC3CCCCCCCCCCCC558BECB8050000005DC3CCCCCCCCCCCC558BECB8010000005DC30000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000!
 000000000
 000000000000000000000000000000000000000000000000
   - Name:            .rdata
     Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ]
     Alignment:       2147483648
-    SectionData:     0000000041ADCC51000000003C2000000100000002000000020000002820000030200000382000000010000000300000452000004820000000000100766172732E646C6C00666E007661720000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000!
 000000000
 000000000000000000000000000000000000000000000000
+    SectionData:     0000000050570852000000004020000001000000030000000200000028200000342000003C200000003000001010000000100000492000005F20000001000200766172732E646C6C005F6E616D655F776974685F756E64657273636F726500666E00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000!
 000000000
 000000000000000000000000000000000000000000000000
   - Name:            .data
     Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ, IMAGE_SCN_MEM_WRITE ]
     Alignment:       2147483648

Modified: lld/trunk/test/pecoff/Inputs/vars.lib
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/pecoff/Inputs/vars.lib?rev=188154&r1=188153&r2=188154&view=diff
==============================================================================
Binary files lld/trunk/test/pecoff/Inputs/vars.lib (original) and lld/trunk/test/pecoff/Inputs/vars.lib Sun Aug 11 23:10:00 2013 differ

Modified: lld/trunk/test/pecoff/importlib.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/pecoff/importlib.test?rev=188154&r1=188153&r2=188154&view=diff
==============================================================================
--- lld/trunk/test/pecoff/importlib.test (original)
+++ lld/trunk/test/pecoff/importlib.test Sun Aug 11 23:10:00 2013
@@ -19,11 +19,16 @@
 # RUN:    %p/Inputs/vars.lib && llvm-objdump -d %t1 | FileCheck %s
 
 CHECK: Disassembly of section .text:
-CHECK: .text:
-CHECK:     1000:       55                    pushl   %ebp
-CHECK:     1001:       8b ec                 movl    %esp, %ebp
-CHECK:     1003:       ff 15 40 20 40 00     calll   *4202560
-CHECK:     1009:       8b 0d 44 20 40 00     movl    4202564, %ecx
-CHECK:     100f:       03 01                 addl    (%ecx), %eax
-CHECK:     1011:       5d                    popl    %ebp
-CHECK:     1012:       c3                    ret
+CHECK-NEXT: .text:
+CHECK-NEXT:     1000:       55                   pushl   %ebp
+CHECK-NEXT:     1001:       8b ec                movl    %esp, %ebp
+CHECK-NEXT:     1003:       56                   pushl   %esi
+CHECK-NEXT:     1004:       ff 15 78 20 40 00    calll   *4202616
+CHECK-NEXT:     100a:       8b 0d 7c 20 40 00    movl    4202620, %ecx
+CHECK-NEXT:     1010:       8b 31                movl    (%ecx), %esi
+CHECK-NEXT:     1012:       03 f0                addl    %eax, %esi
+CHECK-NEXT:     1014:       ff 15 74 20 40 00    calll   *4202612
+CHECK-NEXT:     101a:       03 c6                addl    %esi, %eax
+CHECK-NEXT:     101c:       5e                   popl    %esi
+CHECK-NEXT:     101d:       5d                   popl    %ebp
+CHECK-NEXT:     101e:       c3                   ret





More information about the llvm-commits mailing list