[PATCH] D101569: [LLD] [COFF] Fix automatic export of symbols from LTO objects

Martin Storsjö via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 29 13:47:08 PDT 2021


mstorsjo created this revision.
mstorsjo added a reviewer: rnk.
Herald added subscribers: steven_wu, hiraditya, inglorion.
mstorsjo requested review of this revision.
Herald added a project: LLVM.

This isn't quite pretty, but it seems to work at least. Suggestions
for how to do things better are very welcome. I'll make a few inline
comments about a few surprising bits.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D101569

Files:
  lld/COFF/Driver.cpp
  lld/COFF/MinGW.cpp
  lld/test/COFF/export-all-lto.ll


Index: lld/test/COFF/export-all-lto.ll
===================================================================
--- /dev/null
+++ lld/test/COFF/export-all-lto.ll
@@ -0,0 +1,23 @@
+; REQUIRES: x86
+
+; RUN: llvm-as %s -o %t.bc
+
+; RUN: lld-link -lldmingw -dll -out:%t.dll %t.bc -noentry -output-def:%t.def
+; RUN: llvm-readobj --coff-exports %t.dll | grep Name: | FileCheck %s
+; RUN: cat %t.def | FileCheck --check-prefix=IMPLIB %s
+
+; CHECK: Name: MyExtData
+; CHECK: Name: MyLibFunc
+
+; IMPLIB: MyExtData @1 DATA
+; IMPLIB: MyLibFunc @2{{$}}
+
+target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-w64-windows-gnu"
+
+ at MyExtData = dso_local global i32 42, align 4
+
+define dso_local void @MyLibFunc() {
+entry:
+  ret void
+}
Index: lld/COFF/MinGW.cpp
===================================================================
--- lld/COFF/MinGW.cpp
+++ lld/COFF/MinGW.cpp
@@ -123,7 +123,7 @@
 }
 
 bool AutoExporter::shouldExport(Defined *sym) const {
-  if (!sym || !sym->getChunk())
+  if (!sym)
     return false;
 
   // Only allow the symbol kinds that make sense to export; in particular,
Index: lld/COFF/Driver.cpp
===================================================================
--- lld/COFF/Driver.cpp
+++ lld/COFF/Driver.cpp
@@ -1208,9 +1208,24 @@
     Export e;
     e.name = def->getName();
     e.sym = def;
-    if (Chunk *c = def->getChunk())
-      if (!(c->getOutputCharacteristics() & IMAGE_SCN_MEM_EXECUTE))
-        e.data = true;
+    auto *defReg = dyn_cast<DefinedRegular>(s);
+    if (!defReg || defReg->data) {
+      if (Chunk *c = def->getChunk())
+        if (!(c->getOutputCharacteristics() & IMAGE_SCN_MEM_EXECUTE))
+          e.data = true;
+    } else if (defReg) {
+      InputFile *file = defReg->getFile();
+      if (BitcodeFile *bc = dyn_cast<BitcodeFile>(file)) {
+        for (const lto::InputFile::Symbol &objSym : bc->obj->symbols()) {
+          if (objSym.getName() == e.name) {
+            if (!objSym.isExecutable())
+              e.data = true;
+            break;
+          }
+        }
+      }
+    }
+    s->isUsedInRegularObj = true;
     config->exports.push_back(e);
   });
 }
@@ -2108,6 +2123,13 @@
   if (errorCount())
     return;
 
+  config->hadExplicitExports = !config->exports.empty();
+  if (config->mingw) {
+    // In MinGW, all symbols are automatically exported if no symbols
+    // are chosen to be exported.
+    maybeExportMinGWSymbols(args);
+  }
+
   // Do LTO by compiling bitcode input files to a set of native COFF files then
   // link those files (unless -thinlto-index-only was given, in which case we
   // resolve symbols and write indices, but don't generate native code or link).
@@ -2132,12 +2154,7 @@
   if (errorCount())
     return;
 
-  config->hadExplicitExports = !config->exports.empty();
   if (config->mingw) {
-    // In MinGW, all symbols are automatically exported if no symbols
-    // are chosen to be exported.
-    maybeExportMinGWSymbols(args);
-
     // Make sure the crtend.o object is the last object file. This object
     // file can contain terminating section chunks that need to be placed
     // last. GNU ld processes files and static libraries explicitly in the


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D101569.341632.patch
Type: text/x-patch
Size: 3239 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210429/f4988737/attachment.bin>


More information about the llvm-commits mailing list