[PATCH] D61699: [lld-link] initialize targets and asmparsers before invoking lib

Bob Haarman via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed May 8 13:55:46 PDT 2019


inglorion created this revision.
inglorion added reviewers: ruiu, rnk, hans.
Herald added a project: LLVM.

When using lld-link to build static libraries containing object files
with module assembly, the program would crash with "Assertion `T &&
T->hasMCAsmParser()' failed". This change causes the code in lld-link
that initialized Targets, TargetInfos, and AsmParsers (which already
existed) to be run before entering the lib building path (which needs
it). This avoids the error (and is what llvm-lib and llvm-ar do, too).

Fixes PR41803.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D61699

Files:
  lld/COFF/Driver.cpp
  lld/test/COFF/lib-module-asm.ll


Index: lld/test/COFF/lib-module-asm.ll
===================================================================
--- /dev/null
+++ lld/test/COFF/lib-module-asm.ll
@@ -0,0 +1,9 @@
+; PR41803: llvm-link /lib on object with module asm crashes
+; RUN: rm -f %t.lib
+; RUN: llvm-as -o %t.obj %s
+; RUN: lld-link /lib /out:%t.lib %t.obj
+
+target datalayout = "e-m:x-p:32:32-i64:64-f80:32-n8:16:32-a:0:32-S32"
+target triple = "i386-pc-windows-msvc19.11.0"
+
+module asm ".section .text"
Index: lld/COFF/Driver.cpp
===================================================================
--- lld/COFF/Driver.cpp
+++ lld/COFF/Driver.cpp
@@ -975,6 +975,13 @@
 }
 
 void LinkerDriver::link(ArrayRef<const char *> ArgsArr) {
+  // Needed for LTO.
+  InitializeAllTargetInfos();
+  InitializeAllTargets();
+  InitializeAllTargetMCs();
+  InitializeAllAsmParsers();
+  InitializeAllAsmPrinters();
+
   // If the first command line argument is "/lib", link.exe acts like lib.exe.
   // We call our own implementation of lib.exe that understands bitcode files.
   if (ArgsArr.size() > 1 && StringRef(ArgsArr[1]).equals_lower("/lib")) {
@@ -983,13 +990,6 @@
     return;
   }
 
-  // Needed for LTO.
-  InitializeAllTargetInfos();
-  InitializeAllTargets();
-  InitializeAllTargetMCs();
-  InitializeAllAsmParsers();
-  InitializeAllAsmPrinters();
-
   // Parse command line options.
   ArgParser Parser;
   opt::InputArgList Args = Parser.parseLINK(ArgsArr);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D61699.198722.patch
Type: text/x-patch
Size: 1434 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190508/bc98d1d3/attachment-0001.bin>


More information about the llvm-commits mailing list