Skip to content
Snippets Groups Projects
Commit f1ffe8ab authored by Rui Ueyama's avatar Rui Ueyama
Browse files

[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.

llvm-svn: 188154
parent d5735503
No related branches found
No related tags found
No related merge requests found
...@@ -266,6 +266,14 @@ private: ...@@ -266,6 +266,14 @@ private:
const LinkingContext &_context; const LinkingContext &_context;
mutable llvm::BumpPtrAllocator _alloc; 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 // Convert the given symbol name to the import symbol name exported by the
// DLL. // DLL.
StringRef symbolNameToImportName(StringRef symbolName, int nameType) const { StringRef symbolNameToImportName(StringRef symbolName, int nameType) const {
...@@ -280,11 +288,11 @@ private: ...@@ -280,11 +288,11 @@ private:
return symbolName; return symbolName;
case llvm::COFF::IMPORT_NAME_NOPREFIX: case llvm::COFF::IMPORT_NAME_NOPREFIX:
// The import name is the symbol name without leading ?, @ or _. // The import name is the symbol name without leading ?, @ or _.
ret = symbolName.ltrim("?@_"); ret = ltrim1(symbolName, "?@_");
break; break;
case llvm::COFF::IMPORT_NAME_UNDECORATE: case llvm::COFF::IMPORT_NAME_UNDECORATE:
// Similar to NOPREFIX, but we also need to truncate at the first @. // Similar to NOPREFIX, but we also need to truncate at the first @.
ret = symbolName.ltrim("?@_"); ret = ltrim1(symbolName, "?@_");
ret = ret.substr(0, ret.find('@')); ret = ret.substr(0, ret.find('@'));
break; break;
} }
......
__declspec(dllimport) int var; __declspec(dllimport) int var;
__declspec(dllimport) int fn(void); __declspec(dllimport) int fn(void);
__declspec(dllimport) int _name_with_underscore(void);
int main() { int main() {
return var + fn(); return var + fn() + _name_with_underscore();
} }
...@@ -3,65 +3,44 @@ header: ...@@ -3,65 +3,44 @@ header:
Machine: IMAGE_FILE_MACHINE_I386 Machine: IMAGE_FILE_MACHINE_I386
Characteristics: [ ] Characteristics: [ ]
sections: 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 - Name: .text
Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ] Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
Alignment: 16 Alignment: 16
SectionData: 558BECFF15000000008B0D0000000003015DC3 SectionData: 558BEC56FF15000000008B0D000000008B3103F0FF150000000003C65E5DC3
Relocations: Relocations:
- VirtualAddress: 5 - VirtualAddress: 6
SymbolName: __imp__fn SymbolName: __imp__fn
Type: IMAGE_REL_I386_DIR32 Type: IMAGE_REL_I386_DIR32
- VirtualAddress: 11 - VirtualAddress: 12
SymbolName: __imp__var SymbolName: __imp__var
Type: IMAGE_REL_I386_DIR32 Type: IMAGE_REL_I386_DIR32
- VirtualAddress: 22
SymbolName: __imp___name_with_underscore
Type: IMAGE_REL_I386_DIR32
symbols: symbols:
- Name: "@comp.id" - Name: .text
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
Value: 0 Value: 0
SectionNumber: 1 SectionNumber: 1
SimpleType: IMAGE_SYM_TYPE_NULL SimpleType: IMAGE_SYM_TYPE_NULL
ComplexType: IMAGE_SYM_DTYPE_NULL ComplexType: IMAGE_SYM_DTYPE_NULL
StorageClass: IMAGE_SYM_CLASS_STATIC StorageClass: IMAGE_SYM_CLASS_STATIC
NumberOfAuxSymbols: 1 NumberOfAuxSymbols: 1
AuxiliaryData: 2F0000000000000000000000000000000000 AuxiliaryData: 1F000000030000008C7450D6000000000000
- Name: ".debug$S" - Name: __imp__fn
Value: 0 Value: 0
SectionNumber: 2 SectionNumber: 0
SimpleType: IMAGE_SYM_TYPE_NULL SimpleType: IMAGE_SYM_TYPE_NULL
ComplexType: IMAGE_SYM_DTYPE_NULL ComplexType: IMAGE_SYM_DTYPE_NULL
StorageClass: IMAGE_SYM_CLASS_STATIC StorageClass: IMAGE_SYM_CLASS_EXTERNAL
NumberOfAuxSymbols: 1 - Name: __imp___name_with_underscore
AuxiliaryData: 640000000000000000000000000000000000
- Name: .text
Value: 0 Value: 0
SectionNumber: 3 SectionNumber: 0
SimpleType: IMAGE_SYM_TYPE_NULL SimpleType: IMAGE_SYM_TYPE_NULL
ComplexType: IMAGE_SYM_DTYPE_NULL ComplexType: IMAGE_SYM_DTYPE_NULL
StorageClass: IMAGE_SYM_CLASS_STATIC StorageClass: IMAGE_SYM_CLASS_EXTERNAL
NumberOfAuxSymbols: 1
AuxiliaryData: 1300000002000000B8433CE4000000000000
- Name: _main - Name: _main
Value: 0 Value: 0
SectionNumber: 3 SectionNumber: 1
SimpleType: IMAGE_SYM_TYPE_NULL SimpleType: IMAGE_SYM_TYPE_NULL
ComplexType: IMAGE_SYM_DTYPE_FUNCTION ComplexType: IMAGE_SYM_DTYPE_FUNCTION
StorageClass: IMAGE_SYM_CLASS_EXTERNAL StorageClass: IMAGE_SYM_CLASS_EXTERNAL
...@@ -71,10 +50,4 @@ symbols: ...@@ -71,10 +50,4 @@ symbols:
SimpleType: IMAGE_SYM_TYPE_NULL SimpleType: IMAGE_SYM_TYPE_NULL
ComplexType: IMAGE_SYM_DTYPE_NULL ComplexType: IMAGE_SYM_DTYPE_NULL
StorageClass: IMAGE_SYM_CLASS_EXTERNAL 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
... ...
// cl.exe /c vars.c // cl.exe /c vars.c
// link /debug /dll /nodefaultlib /entry:dllmain /export:var,@1,NONAME,DATA /export:fn vars.obj // link /dll /nodefaultlib /entry:dllmain /export:var,@1,NONAME,DATA \
// /export:fn /export:_name_with_underscore vars.obj
// will be exported by ordinal // will be exported by ordinal
int var = 3; int var = 3;
...@@ -9,6 +10,11 @@ int fn(void) { ...@@ -9,6 +10,11 @@ int fn(void) {
return 4; return 4;
} }
// will be exported by name
int _name_with_underscore(void) {
return 5;
}
int dllmain() { int dllmain() {
return 1; return 1;
} }
...@@ -6,11 +6,11 @@ sections: ...@@ -6,11 +6,11 @@ sections:
- Name: .text - Name: .text
Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ] Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
Alignment: 2147483648 Alignment: 2147483648
SectionData: 558BECB8090000005DC3CCCCCCCCCCCC558BECB8010000005DC3000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 SectionData: 558BECB8040000005DC3CCCCCCCCCCCC558BECB8050000005DC3CCCCCCCCCCCC558BECB8010000005DC30000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
- Name: .rdata - Name: .rdata
Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ] Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ]
Alignment: 2147483648 Alignment: 2147483648
SectionData: 0000000041ADCC51000000003C2000000100000002000000020000002820000030200000382000000010000000300000452000004820000000000100766172732E646C6C00666E007661720000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 SectionData: 0000000050570852000000004020000001000000030000000200000028200000342000003C200000003000001010000000100000492000005F20000001000200766172732E646C6C005F6E616D655F776974685F756E64657273636F726500666E00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
- Name: .data - Name: .data
Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ, IMAGE_SCN_MEM_WRITE ] Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ, IMAGE_SCN_MEM_WRITE ]
Alignment: 2147483648 Alignment: 2147483648
......
No preview for this file type
...@@ -19,11 +19,16 @@ ...@@ -19,11 +19,16 @@
# RUN: %p/Inputs/vars.lib && llvm-objdump -d %t1 | FileCheck %s # RUN: %p/Inputs/vars.lib && llvm-objdump -d %t1 | FileCheck %s
CHECK: Disassembly of section .text: CHECK: Disassembly of section .text:
CHECK: .text: CHECK-NEXT: .text:
CHECK: 1000: 55 pushl %ebp CHECK-NEXT: 1000: 55 pushl %ebp
CHECK: 1001: 8b ec movl %esp, %ebp CHECK-NEXT: 1001: 8b ec movl %esp, %ebp
CHECK: 1003: ff 15 40 20 40 00 calll *4202560 CHECK-NEXT: 1003: 56 pushl %esi
CHECK: 1009: 8b 0d 44 20 40 00 movl 4202564, %ecx CHECK-NEXT: 1004: ff 15 78 20 40 00 calll *4202616
CHECK: 100f: 03 01 addl (%ecx), %eax CHECK-NEXT: 100a: 8b 0d 7c 20 40 00 movl 4202620, %ecx
CHECK: 1011: 5d popl %ebp CHECK-NEXT: 1010: 8b 31 movl (%ecx), %esi
CHECK: 1012: c3 ret 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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment