[PATCH] D102138: [LLD] [COFF] Fix including the personality function for DWARF EH when linking with --gc-sections

Martin Storsjö via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun May 9 13:50:20 PDT 2021


mstorsjo created this revision.
mstorsjo added a reviewer: rnk.
mstorsjo requested review of this revision.
Herald added a project: LLVM.

Since c579a5b1d92a9bc2046d00ee2d427832e0f5ddec <https://reviews.llvm.org/rGc579a5b1d92a9bc2046d00ee2d427832e0f5ddec> we don't traverse
.eh_frame when doing GC. But the exception handling personality
function needs to be included, and is only referenced from within
.eh_frame.

Longterm, we probably would want to split .eh_frame into associative
COMDATs just like we do for .xdata/.pdata (and then include them in
the GC traversal), but for now, just try to pull in the name of the
known personality function in mingw DWARF EH.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D102138

Files:
  lld/COFF/Driver.cpp
  lld/test/COFF/gc-dwarf-eh.s


Index: lld/test/COFF/gc-dwarf-eh.s
===================================================================
--- /dev/null
+++ lld/test/COFF/gc-dwarf-eh.s
@@ -0,0 +1,39 @@
+# REQUIRES: x86
+
+# RUN: llvm-mc -triple=i686-windows-gnu %s -filetype=obj -o %t.obj
+# RUN: lld-link -lldmingw -lldmap:%t.map -out:%t.exe -opt:ref -entry:main %t.obj -verbose 2>&1 | FileCheck %s
+# RUN: FileCheck %s --check-prefix=MAP --input-file=%t.map
+
+# CHECK: Discarded _unused
+
+# MAP: In Symbol
+# MAP: gc-dwarf-eh.s.tmp.obj:(.text)
+# MAP: {{ ___gxx_personality_v0$}}
+
+	.def	_main; .scl	2; .type	32; .endef
+	.section	.text,"xr",one_only,_main
+	.globl	_main
+_main:
+	xorl	%eax, %eax
+	ret
+
+	.def	___gxx_personality_v0; .scl	2; .type	32; .endef
+	.section	.text,"xr",one_only,___gxx_personality_v0
+	.globl	___gxx_personality_v0
+___gxx_personality_v0:
+	ret
+
+	.def	_unused; .scl	2; .type	32; .endef
+	.section	.text,"xr",one_only,_unused
+	.globl	_unused
+_unused:
+	ret
+
+# This isn't valid DWARF, but LLD doesn't care. Make up some data that
+# references the functions above.
+# We need the personality function included, but not all functions referenced
+# here.
+.section .eh_frame,"r"
+.long _main at IMGREL
+.long ___gxx_personality_v0 at IMGREL
+.long _unused at IMGREL
Index: lld/COFF/Driver.cpp
===================================================================
--- lld/COFF/Driver.cpp
+++ lld/COFF/Driver.cpp
@@ -2229,8 +2229,23 @@
     config->printSymbolOrder = arg->getValue();
 
   // Identify unreferenced COMDAT sections.
-  if (config->doGC)
+  if (config->doGC) {
+    if (config->mingw) {
+      // markLive doesn't traverse .eh_frame, but the personality function is
+      // only reached that way. The proper solution would be to make .eh_frame
+      // into individual COMDAT sections (just like .xdata/.pdata) and have
+      // markLive traverse them, but until then, just try to include this one
+      // symbol, if available.
+      Defined *d = dyn_cast_or_null<Defined>(
+          symtab->findUnderscore("__gxx_personality_v0"));
+      if (d && !d->isGCRoot) {
+        d->isGCRoot = true;
+        config->gcroot.push_back(d);
+      }
+    }
+
     markLive(symtab->getChunks());
+  }
 
   // Needs to happen after the last call to addFile().
   convertResources();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D102138.343933.patch
Type: text/x-patch
Size: 2283 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210509/da5984dc/attachment.bin>


More information about the llvm-commits mailing list