From f1ffe8abbf5a90cb7e9f4280d24d4fd8f0dca44a Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Mon, 12 Aug 2013 04:10:00 +0000 Subject: [PATCH] [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 --- .../PECOFF/ReaderImportHeader.cpp | 12 +++- lld/test/pecoff/Inputs/vars-main.c | 3 +- lld/test/pecoff/Inputs/vars-main.obj.yaml | 57 +++++------------- lld/test/pecoff/Inputs/vars.c | 8 ++- lld/test/pecoff/Inputs/vars.dll.yaml | 4 +- lld/test/pecoff/Inputs/vars.lib | Bin 1762 -> 1994 bytes lld/test/pecoff/importlib.test | 21 ++++--- 7 files changed, 49 insertions(+), 56 deletions(-) diff --git a/lld/lib/ReaderWriter/PECOFF/ReaderImportHeader.cpp b/lld/lib/ReaderWriter/PECOFF/ReaderImportHeader.cpp index 1b619e0a43b0..29d2c133a3de 100644 --- a/lld/lib/ReaderWriter/PECOFF/ReaderImportHeader.cpp +++ b/lld/lib/ReaderWriter/PECOFF/ReaderImportHeader.cpp @@ -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; } diff --git a/lld/test/pecoff/Inputs/vars-main.c b/lld/test/pecoff/Inputs/vars-main.c index ac72617ec4ad..d588ca54b88e 100644 --- a/lld/test/pecoff/Inputs/vars-main.c +++ b/lld/test/pecoff/Inputs/vars-main.c @@ -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(); } diff --git a/lld/test/pecoff/Inputs/vars-main.obj.yaml b/lld/test/pecoff/Inputs/vars-main.obj.yaml index 058708359486..191d295e65f8 100644 --- a/lld/test/pecoff/Inputs/vars-main.obj.yaml +++ b/lld/test/pecoff/Inputs/vars-main.obj.yaml @@ -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 @@ -71,10 +50,4 @@ symbols: 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 ... diff --git a/lld/test/pecoff/Inputs/vars.c b/lld/test/pecoff/Inputs/vars.c index 7d2ab9bfdea8..ae2ec46d1f47 100644 --- a/lld/test/pecoff/Inputs/vars.c +++ b/lld/test/pecoff/Inputs/vars.c @@ -1,5 +1,6 @@ // 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 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; } diff --git a/lld/test/pecoff/Inputs/vars.dll.yaml b/lld/test/pecoff/Inputs/vars.dll.yaml index cbeab6e49028..06f65ced6933 100644 --- a/lld/test/pecoff/Inputs/vars.dll.yaml +++ b/lld/test/pecoff/Inputs/vars.dll.yaml @@ -6,11 +6,11 @@ sections: - Name: .text Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ] Alignment: 2147483648 - SectionData: 558BECB8090000005DC3CCCCCCCCCCCC558BECB8010000005DC3000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 + SectionData: 558BECB8040000005DC3CCCCCCCCCCCC558BECB8050000005DC3CCCCCCCCCCCC558BECB8010000005DC30000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 - Name: .rdata Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ] Alignment: 2147483648 - SectionData: 0000000041ADCC51000000003C2000000100000002000000020000002820000030200000382000000010000000300000452000004820000000000100766172732E646C6C00666E007661720000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 + SectionData: 0000000050570852000000004020000001000000030000000200000028200000342000003C200000003000001010000000100000492000005F20000001000200766172732E646C6C005F6E616D655F776974685F756E64657273636F726500666E00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 - Name: .data Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ, IMAGE_SCN_MEM_WRITE ] Alignment: 2147483648 diff --git a/lld/test/pecoff/Inputs/vars.lib b/lld/test/pecoff/Inputs/vars.lib index 329362bc5204692760d9f90c420c1a9c83ceef8e..2d3aa2af678ef35c76bf0062de905b9968912e53 100644 GIT binary patch delta 516 zcmaFFdy0R8G`pFRxrMQ*(c}ZnVg`m5rV0>{z{SA8z`?-41f*Ex7#LWC7#P@dfS3=0 z+2%1Yu+5v8kZ2qqpP5?_A0MBWn4213o>`I+Uz(SaT2!2zUzEy#U3lUFQDke)4K2;# z*0KRD1lcOb!oU#3%D|Ar&cMJ2#Pfg>K=&{(0wviPSQ%It*d{A7+PUJg7wo9CJfI7} ztg^%+9FAd#Ps^J)QG9YcqX@F2ChuSrUc^Bw0>;c69bat^`N=^=(p$|32 w(AXRnaSm)+oFJokfd(;7E@o4KJIR4feDYQ{VJVO-Fy4S+$H)*53JJKe054C1uK)l5 delta 346 zcmX@b|A=>jw5zG1vALfbtBDOrfj6KClsaOtBMqo4pX9Ipk&Fq;#&Sb;WlFfuSKV`gBu#lpbQ0<=yIh=GWKk%5VU zm4Rh)Dx)25S{_4ud}eMze0*7A(c~wL`s`3ECQC7?af-hG7RUxPg=6w;R