From 72fbd346c62f3c2bf30ffa99dd4ee55430ac995a Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Fri, 21 Apr 2017 18:05:46 +0000 Subject: [PATCH] COFF: add support for CONSTANT exports The CONSTANT export type is marked as obsolete, but link still supports this. Furthermore, WinObjC uses this for certain exports. Add support for this export type. llvm-svn: 301013 --- lld/COFF/Config.h | 1 + lld/COFF/DriverUtils.cpp | 4 ++ lld/COFF/Librarian.cpp | 20 +++--- lld/COFF/ModuleDef.cpp | 7 +++ lld/test/COFF/Inputs/constant-export.ll | 10 +++ lld/test/COFF/constant-export.test | 9 +++ lld/test/COFF/constant-export.yaml | 83 +++++++++++++++++++++++++ 7 files changed, 127 insertions(+), 7 deletions(-) create mode 100644 lld/test/COFF/Inputs/constant-export.ll create mode 100644 lld/test/COFF/constant-export.test create mode 100644 lld/test/COFF/constant-export.yaml diff --git a/lld/COFF/Config.h b/lld/COFF/Config.h index 31534aeb3971..fafd3bcde2e3 100644 --- a/lld/COFF/Config.h +++ b/lld/COFF/Config.h @@ -43,6 +43,7 @@ struct Export { bool Noname = false; bool Data = false; bool Private = false; + bool Constant = false; // If an export is a form of /export:foo=dllname.bar, that means // that foo should be exported as an alias to bar in the DLL. diff --git a/lld/COFF/DriverUtils.cpp b/lld/COFF/DriverUtils.cpp index 2c9ba797f73b..736e8463dd8e 100644 --- a/lld/COFF/DriverUtils.cpp +++ b/lld/COFF/DriverUtils.cpp @@ -479,6 +479,10 @@ Export parseExport(StringRef Arg) { E.Data = true; continue; } + if (Tok.equals_lower("constant")) { + E.Constant = true; + continue; + } if (Tok.equals_lower("private")) { E.Private = true; continue; diff --git a/lld/COFF/Librarian.cpp b/lld/COFF/Librarian.cpp index 3ce72822180b..91316ee6b0c9 100644 --- a/lld/COFF/Librarian.cpp +++ b/lld/COFF/Librarian.cpp @@ -162,7 +162,7 @@ public: // Create a short import file which is described in PE/COFF spec 7. Import // Library Format. NewArchiveMember createShortImport(StringRef Sym, uint16_t Ordinal, - ImportNameType NameType, bool isData); + ImportType Type, ImportNameType NameType); }; } @@ -440,8 +440,8 @@ NewArchiveMember ObjectFactory::createNullThunk(std::vector &Buffer) { NewArchiveMember ObjectFactory::createShortImport(StringRef Sym, uint16_t Ordinal, - ImportNameType NameType, - bool isData) { + ImportType ImportType, + ImportNameType NameType) { size_t ImpSize = DLLName.size() + Sym.size() + 2; // +2 for NULs size_t Size = sizeof(coff_import_header) + ImpSize; char *Buf = Alloc.Allocate(Size); @@ -456,8 +456,7 @@ NewArchiveMember ObjectFactory::createShortImport(StringRef Sym, Imp->SizeOfData = ImpSize; if (Ordinal > 0) Imp->OrdinalHint = Ordinal; - Imp->TypeInfo = (isData ? IMPORT_DATA : IMPORT_CODE); - Imp->TypeInfo |= NameType << 2; + Imp->TypeInfo = (NameType << 2) | ImportType; // Write symbol name and DLL name. memcpy(P, Sym.data(), Sym.size()); @@ -490,11 +489,18 @@ void lld::coff::writeImportLibrary() { if (E.Private) continue; - ImportNameType Type = getNameType(E.SymbolName, E.Name); + ImportType ImportType = IMPORT_CODE; + if (E.Data) + ImportType = IMPORT_DATA; + if (E.Constant) + ImportType = IMPORT_CONST; + + ImportNameType NameType = getNameType(E.SymbolName, E.Name); std::string Name = E.ExtName.empty() ? std::string(E.SymbolName) : replace(E.SymbolName, E.Name, E.ExtName); - Members.push_back(OF.createShortImport(Name, E.Ordinal, Type, E.Data)); + Members.push_back(OF.createShortImport(Name, E.Ordinal, ImportType, + NameType)); } std::pair Result = diff --git a/lld/COFF/ModuleDef.cpp b/lld/COFF/ModuleDef.cpp index c9a40ac5ab8c..740ce867a7c4 100644 --- a/lld/COFF/ModuleDef.cpp +++ b/lld/COFF/ModuleDef.cpp @@ -38,6 +38,7 @@ enum Kind { Comma, Equal, KwBase, + KwConstant, KwData, KwExports, KwHeapsize, @@ -92,6 +93,7 @@ public: StringRef Word = Buf.substr(0, End); Kind K = llvm::StringSwitch(Word) .Case("BASE", KwBase) + .Case("CONSTANT", KwConstant) .Case("DATA", KwData) .Case("EXPORTS", KwExports) .Case("HEAPSIZE", KwHeapsize) @@ -227,6 +229,11 @@ private: E.Data = true; continue; } + if (Tok.K == KwConstant) { + warn("CONSTANT keyword is obsolete; use DATA"); + E.Constant = true; + continue; + } if (Tok.K == KwPrivate) { E.Private = true; continue; diff --git a/lld/test/COFF/Inputs/constant-export.ll b/lld/test/COFF/Inputs/constant-export.ll new file mode 100644 index 000000000000..2b57e8d083fa --- /dev/null +++ b/lld/test/COFF/Inputs/constant-export.ll @@ -0,0 +1,10 @@ +target datalayout = "e-m:x-p:32:32-i64:64-f80:32-n8:16:32-a:0:32-S32" +target triple = "i686-unknown-windows-msvc18.0.0" + +@__CFConstantStringClassReference = common global [32 x i32] zeroinitializer, align 4 + +!llvm.module.flags = !{!0} + +!0 = !{i32 6, !"Linker Options", !1} +!1 = !{!2} +!2 = !{!" -export:___CFConstantStringClassReference,CONSTANT"} diff --git a/lld/test/COFF/constant-export.test b/lld/test/COFF/constant-export.test new file mode 100644 index 000000000000..18b1f5e30d29 --- /dev/null +++ b/lld/test/COFF/constant-export.test @@ -0,0 +1,9 @@ +# RUN: mkdir -p %t +# RUN: yaml2obj -o %t/constant-export.obj %S/constant-export.yaml +# RUN: lld-link /machine:x86 /dll /entry:__CFConstantStringClassReference -out:%t/constant-export.dll %t/constant-export.obj +# RUN: llvm-readobj -coff-exports %t/constant-export.lib | FileCheck %s + +# CHECK: Type: const +# CHECK: Name type: noprefix +# CHECK: Symbol: __imp____CFConstantStringClassReference + diff --git a/lld/test/COFF/constant-export.yaml b/lld/test/COFF/constant-export.yaml new file mode 100644 index 000000000000..7e44bb70c9d7 --- /dev/null +++ b/lld/test/COFF/constant-export.yaml @@ -0,0 +1,83 @@ +--- !COFF +header: + Machine: IMAGE_FILE_MACHINE_I386 + Characteristics: [ ] +sections: + - Name: .text + Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ] + Alignment: 4 + SectionData: '' + - Name: .data + Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ, IMAGE_SCN_MEM_WRITE ] + Alignment: 4 + SectionData: '' + - Name: .bss + Characteristics: [ IMAGE_SCN_CNT_UNINITIALIZED_DATA, IMAGE_SCN_MEM_READ, IMAGE_SCN_MEM_WRITE ] + Alignment: 4 + SectionData: '' + - Name: .drectve + Characteristics: [ IMAGE_SCN_LNK_INFO, IMAGE_SCN_LNK_REMOVE ] + Alignment: 1 + SectionData: 20202D6578706F72743A5F5F5F4346436F6E7374616E74537472696E67436C6173735265666572656E63652C434F4E5354414E54 +symbols: + - Name: .text + Value: 0 + SectionNumber: 1 + SimpleType: IMAGE_SYM_TYPE_NULL + ComplexType: IMAGE_SYM_DTYPE_NULL + StorageClass: IMAGE_SYM_CLASS_STATIC + SectionDefinition: + Length: 0 + NumberOfRelocations: 0 + NumberOfLinenumbers: 0 + CheckSum: 0 + Number: 1 + - Name: .data + Value: 0 + SectionNumber: 2 + SimpleType: IMAGE_SYM_TYPE_NULL + ComplexType: IMAGE_SYM_DTYPE_NULL + StorageClass: IMAGE_SYM_CLASS_STATIC + SectionDefinition: + Length: 0 + NumberOfRelocations: 0 + NumberOfLinenumbers: 0 + CheckSum: 0 + Number: 2 + - Name: .bss + Value: 0 + SectionNumber: 3 + SimpleType: IMAGE_SYM_TYPE_NULL + ComplexType: IMAGE_SYM_DTYPE_NULL + StorageClass: IMAGE_SYM_CLASS_STATIC + SectionDefinition: + Length: 0 + NumberOfRelocations: 0 + NumberOfLinenumbers: 0 + CheckSum: 0 + Number: 3 + - Name: .drectve + Value: 0 + SectionNumber: 4 + SimpleType: IMAGE_SYM_TYPE_NULL + ComplexType: IMAGE_SYM_DTYPE_NULL + StorageClass: IMAGE_SYM_CLASS_STATIC + SectionDefinition: + Length: 52 + NumberOfRelocations: 0 + NumberOfLinenumbers: 0 + CheckSum: 1983959296 + Number: 4 + - Name: '@feat.00' + Value: 1 + SectionNumber: -1 + SimpleType: IMAGE_SYM_TYPE_NULL + ComplexType: IMAGE_SYM_DTYPE_NULL + StorageClass: IMAGE_SYM_CLASS_STATIC + - Name: ___CFConstantStringClassReference + Value: 128 + SectionNumber: 0 + SimpleType: IMAGE_SYM_TYPE_NULL + ComplexType: IMAGE_SYM_DTYPE_NULL + StorageClass: IMAGE_SYM_CLASS_EXTERNAL +...