Skip to content

[LLD] [COFF] Don't create pseudo relocations for discardable sections #89043

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 18, 2024

Conversation

mstorsjo
Copy link
Member

This extends on the case from 9c970d5; if a section is marked discardable, it won't be mapped into memory at runtime, so there's no point in creating runtime pseudo relocations for such sections.

This extends on the case from 9c970d5;
if a section is marked discardable, it won't be mapped into memory
at runtime, so there's no point in creating runtime pseudo
relocations for such sections.
@llvmbot
Copy link
Member

llvmbot commented Apr 17, 2024

@llvm/pr-subscribers-lld
@llvm/pr-subscribers-platform-windows

@llvm/pr-subscribers-lld-coff

Author: Martin Storsjö (mstorsjo)

Changes

This extends on the case from 9c970d5; if a section is marked discardable, it won't be mapped into memory at runtime, so there's no point in creating runtime pseudo relocations for such sections.


Full diff: https://github.com/llvm/llvm-project/pull/89043.diff

2 Files Affected:

  • (modified) lld/COFF/Writer.cpp (+4)
  • (added) lld/test/COFF/autoimport-debug.s (+43)
diff --git a/lld/COFF/Writer.cpp b/lld/COFF/Writer.cpp
index 7269d156752de8..68993232f09c0b 100644
--- a/lld/COFF/Writer.cpp
+++ b/lld/COFF/Writer.cpp
@@ -2060,6 +2060,10 @@ void Writer::createRuntimePseudoRelocs() {
     auto *sc = dyn_cast<SectionChunk>(c);
     if (!sc || !sc->live)
       continue;
+    // Don't create pseudo relocations for sections that won't be
+    // mapped at runtime.
+    if (sc->header->Characteristics & IMAGE_SCN_MEM_DISCARDABLE)
+      continue;
     sc->getRuntimePseudoRelocs(rels);
   }
 
diff --git a/lld/test/COFF/autoimport-debug.s b/lld/test/COFF/autoimport-debug.s
new file mode 100644
index 00000000000000..1f31a2b9e554ff
--- /dev/null
+++ b/lld/test/COFF/autoimport-debug.s
@@ -0,0 +1,43 @@
+# REQUIRES: x86
+# RUN: split-file %s %t.dir
+
+## We've got references to variable both in a .refptr and in .debug_info.
+## The .debug_info section should be discardable, so no pseudo relocations
+## need to be created in it. The .refptr section should be elimiated
+## and redirected to __imp_variable instead, so we shouldn't need to
+## create any runtime pseudo relocations. Thus, test that we can link
+## successfully with -runtime-pseudo-reloc:no, while keeping the
+## debug info.
+
+# RUN: llvm-mc -triple=x86_64-windows-gnu %t.dir/lib.s -filetype=obj -o %t.dir/lib.obj
+# RUN: lld-link -out:%t.dir/lib.dll -dll -entry:DllMainCRTStartup %t.dir/lib.obj -lldmingw -implib:%t.dir/lib.lib
+
+# RUN: llvm-mc -triple=x86_64-windows-gnu %t.dir/main.s -filetype=obj -o %t.dir/main.obj
+# RUN: lld-link -lldmingw -out:%t.dir/main.exe -entry:main %t.dir/main.obj %t.dir/lib.lib -opt:noref -debug:dwarf -runtime-pseudo-reloc:no
+
+#--- main.s
+    .global main
+    .text
+main:
+    movq .refptr.variable(%rip), %rax
+    ret
+
+    .section .rdata$.refptr.variable,"dr",discard,.refptr.variable
+    .global .refptr.variable
+.refptr.variable:
+    .quad   variable
+
+    .section .debug_info
+    .long 1
+    .quad variable
+    .long 2
+
+#--- lib.s
+    .global variable
+    .global DllMainCRTStartup
+    .text
+DllMainCRTStartup:
+    ret
+    .data
+variable:
+    .long 42

Copy link
Contributor

@cjacek cjacek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@mstorsjo mstorsjo merged commit d17db60 into llvm:main Apr 18, 2024
@mstorsjo mstorsjo deleted the lld-pseudo-relocs-discardable branch April 18, 2024 10:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants