Index: COFF/Driver.h =================================================================== --- COFF/Driver.h +++ COFF/Driver.h @@ -103,7 +103,6 @@ std::set VisitedLibs; Symbol *addUndefined(StringRef Sym); - StringRef mangle(StringRef Sym); // Windows specific -- "main" is not the only main function in Windows. // You can choose one from these four -- {w,}{WinMain,main}. Index: COFF/Driver.cpp =================================================================== --- COFF/Driver.cpp +++ COFF/Driver.cpp @@ -116,6 +116,19 @@ }); } +// Symbol names are mangled by prepending "_" on x86. +static StringRef mangle(StringRef Sym) { + assert(Config->Machine != IMAGE_FILE_MACHINE_UNKNOWN); + if (Config->Machine == I386) + return Saver.save("_" + Sym); + return Sym; +} + +static bool findUnderscoreMangle(StringRef Sym) { + StringRef Entry = Symtab->findMangle(mangle(Sym)); + return !Entry.empty() && !isa(Symtab->find(Entry)); +} + MemoryBufferRef LinkerDriver::takeBuffer(std::unique_ptr MB) { MemoryBufferRef MBRef = *MB; make>(std::move(MB)); // take ownership @@ -407,14 +420,6 @@ return B; } -// Symbol names are mangled by appending "_" prefix on x86. -StringRef LinkerDriver::mangle(StringRef Sym) { - assert(Config->Machine != IMAGE_FILE_MACHINE_UNKNOWN); - if (Config->Machine == I386) - return Saver.save("_" + Sym); - return Sym; -} - // Windows specific -- find default entry point name. // // There are four different entry point functions for Windows executables, @@ -427,8 +432,7 @@ if (Config->NoDefaultLibAll) { for (StringRef S : {"mainCRTStartup", "wmainCRTStartup", "WinMainCRTStartup", "wWinMainCRTStartup"}) { - StringRef Entry = Symtab->findMangle(S); - if (!Entry.empty() && !isa(Symtab->find(Entry))) + if (findUnderscoreMangle(S)) return mangle(S); } return ""; @@ -442,8 +446,7 @@ {"wWinMain", "wWinMainCRTStartup"}, }; for (auto E : Entries) { - StringRef Entry = Symtab->findMangle(mangle(E[0])); - if (!Entry.empty() && !isa(Symtab->find(Entry))) + if (findUnderscoreMangle(E[0])) return mangle(E[1]); } return ""; @@ -452,9 +455,9 @@ WindowsSubsystem LinkerDriver::inferSubsystem() { if (Config->DLL) return IMAGE_SUBSYSTEM_WINDOWS_GUI; - if (Symtab->findUnderscore("main") || Symtab->findUnderscore("wmain")) + if (findUnderscoreMangle("main") || findUnderscoreMangle("wmain")) return IMAGE_SUBSYSTEM_WINDOWS_CUI; - if (Symtab->findUnderscore("WinMain") || Symtab->findUnderscore("wWinMain")) + if (findUnderscoreMangle("WinMain") || findUnderscoreMangle("wWinMain")) return IMAGE_SUBSYSTEM_WINDOWS_GUI; return IMAGE_SUBSYSTEM_UNKNOWN; } Index: test/COFF/entry-inference3.test =================================================================== --- test/COFF/entry-inference3.test +++ test/COFF/entry-inference3.test @@ -1,4 +1,8 @@ -# RUN: yaml2obj < %s > %t.obj +# RUN: sed -e s/ENTRYNAME/mainCRTStartup/ %s | yaml2obj > %t.obj +# RUN: not lld-link /out:%t.exe %t.obj /verbose /nodefaultlib > %t.log 2>&1 +# RUN: FileCheck %s < %t.log + +# RUN: sed -e s/ENTRYNAME/?mainCRTStartup@@YAHXZ/ %s | yaml2obj > %t.obj # RUN: not lld-link /out:%t.exe %t.obj /verbose /nodefaultlib > %t.log 2>&1 # RUN: FileCheck %s < %t.log @@ -26,7 +30,7 @@ NumberOfLinenumbers: 0 CheckSum: 0 Number: 0 - - Name: mainCRTStartup + - Name: "ENTRYNAME" Value: 0 SectionNumber: 1 SimpleType: IMAGE_SYM_TYPE_NULL Index: test/COFF/entry-inference332.test =================================================================== --- test/COFF/entry-inference332.test +++ test/COFF/entry-inference332.test @@ -0,0 +1,39 @@ +# RUN: sed -e s/ENTRYNAME/_mainCRTStartup/ %s | yaml2obj > %t.obj +# RUN: not lld-link /out:%t.exe %t.obj /verbose /nodefaultlib > %t.log 2>&1 +# RUN: FileCheck %s < %t.log + +# RUN: sed -e s/ENTRYNAME/?mainCRTStartup@@YAHXZ/ %s | yaml2obj > %t.obj +# RUN: not lld-link /out:%t.exe %t.obj /verbose /nodefaultlib > %t.log 2>&1 +# RUN: FileCheck %s < %t.log + +# CHECK: Entry name inferred: _mainCRTStartup + +--- !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: B82A000000C3 +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 + - Name: "ENTRYNAME" + Value: 0 + SectionNumber: 1 + SimpleType: IMAGE_SYM_TYPE_NULL + ComplexType: IMAGE_SYM_DTYPE_NULL + StorageClass: IMAGE_SYM_CLASS_EXTERNAL +... Index: test/COFF/subsystem-inference32.test =================================================================== --- test/COFF/subsystem-inference32.test +++ test/COFF/subsystem-inference32.test @@ -0,0 +1,74 @@ +# RUN: sed -e s/ENTRYNAME/_main/ %s | yaml2obj > %t.obj +# RUN: lld-link /out:%t.exe %t.obj +# RUN: llvm-readobj -file-headers %t.exe | FileCheck -check-prefix=MAIN %s + +# RUN: sed s/ENTRYNAME/_wmain/ %s | yaml2obj > %t.obj +# RUN: lld-link /out:%t.exe %t.obj +# RUN: llvm-readobj -file-headers %t.exe | FileCheck -check-prefix=WMAIN %s + +# RUN: sed s/ENTRYNAME/_WinMain@16/ %s | yaml2obj > %t.obj +# RUN: lld-link /out:%t.exe %t.obj +# RUN: llvm-readobj -file-headers %t.exe | FileCheck -check-prefix=WINMAIN %s + +# RUN: sed s/ENTRYNAME/_wWinMain@16/ %s | yaml2obj > %t.obj +# RUN: lld-link /out:%t.exe %t.obj +# RUN: llvm-readobj -file-headers %t.exe | FileCheck -check-prefix=WWINMAIN %s + +# MAIN: Subsystem: IMAGE_SUBSYSTEM_WINDOWS_CUI +# WMAIN: Subsystem: IMAGE_SUBSYSTEM_WINDOWS_CUI +# WINMAIN: Subsystem: IMAGE_SUBSYSTEM_WINDOWS_GUI +# WWINMAIN: Subsystem: IMAGE_SUBSYSTEM_WINDOWS_GUI + +--- !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: B82A000000C3 +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 + - Name: ENTRYNAME + Value: 0 + SectionNumber: 1 + SimpleType: IMAGE_SYM_TYPE_NULL + ComplexType: IMAGE_SYM_DTYPE_NULL + StorageClass: IMAGE_SYM_CLASS_EXTERNAL + - Name: _mainCRTStartup + Value: 0 + SectionNumber: 1 + SimpleType: IMAGE_SYM_TYPE_NULL + ComplexType: IMAGE_SYM_DTYPE_NULL + StorageClass: IMAGE_SYM_CLASS_EXTERNAL + - Name: _wmainCRTStartup + Value: 0 + SectionNumber: 1 + SimpleType: IMAGE_SYM_TYPE_NULL + ComplexType: IMAGE_SYM_DTYPE_NULL + StorageClass: IMAGE_SYM_CLASS_EXTERNAL + - Name: _WinMainCRTStartup + Value: 0 + SectionNumber: 1 + SimpleType: IMAGE_SYM_TYPE_NULL + ComplexType: IMAGE_SYM_DTYPE_NULL + StorageClass: IMAGE_SYM_CLASS_EXTERNAL + - Name: _wWinMainCRTStartup + Value: 0 + SectionNumber: 1 + SimpleType: IMAGE_SYM_TYPE_NULL + ComplexType: IMAGE_SYM_DTYPE_NULL + StorageClass: IMAGE_SYM_CLASS_EXTERNAL +...