COFF: Fix entry point inference bug.
authorRui Ueyama <ruiu@google.com>
Thu, 18 Jun 2015 00:40:33 +0000 (00:40 +0000)
committerRui Ueyama <ruiu@google.com>
Thu, 18 Jun 2015 00:40:33 +0000 (00:40 +0000)
Previously, LLD couldn't find a default entry point if it's
defined by a library.

llvm-svn: 239982

lld/COFF/SymbolTable.cpp
lld/COFF/SymbolTable.h
lld/test/COFF/entrylib.ll [new file with mode: 0644]

index cf1be7d..10c13be 100644 (file)
@@ -187,6 +187,14 @@ Defined *SymbolTable::find(StringRef Name) {
   return nullptr;
 }
 
+std::error_code SymbolTable::resolveIfPossible(StringRef Name) {
+  auto It = Symtab.find(Name);
+  if (It != Symtab.end())
+    if (auto *B = dyn_cast<Lazy>(It->second->Body))
+      return addMemberFile(B);
+  return std::error_code();
+}
+
 // Windows specific -- Link default entry point name.
 ErrorOr<StringRef> SymbolTable::findDefaultEntry() {
   // If it's DLL, the rule is easy.
@@ -205,6 +213,7 @@ ErrorOr<StringRef> SymbolTable::findDefaultEntry() {
       {"wWinMain", "wWinMainCRTStartup"},
   };
   for (auto E : Entries) {
+    resolveIfPossible(E[1]);
     if (find(E[1]))
       return StringRef(E[1]);
     if (!find(E[0]))
index df52982..0817fb3 100644 (file)
@@ -88,6 +88,7 @@ private:
   std::error_code addBitcode(BitcodeFile *File);
 
   std::error_code resolve(SymbolBody *Body);
+  std::error_code resolveIfPossible(StringRef Name);
   std::error_code addMemberFile(Lazy *Body);
   ErrorOr<ObjectFile *> createLTOObject(llvm::LTOCodeGenerator *CG);
 
diff --git a/lld/test/COFF/entrylib.ll b/lld/test/COFF/entrylib.ll
new file mode 100644 (file)
index 0000000..bc189a1
--- /dev/null
@@ -0,0 +1,11 @@
+; RUN: llvm-as -o %t.obj %s
+; RUN: rm -f %t.lib
+; RUN: llvm-ar cru %t.lib %t.obj
+; RUN: lld -flavor link2 /out:%t.exe %t.lib
+
+target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-pc-windows-msvc"
+
+define i32 @mainCRTStartup() {
+  ret i32 0
+}