Skip to content

Commit d17db60

Browse files
authored
[LLD] [COFF] Don't create pseudo relocations for discardable sections (#89043)
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.
1 parent 750de32 commit d17db60

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

lld/COFF/Writer.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2060,6 +2060,10 @@ void Writer::createRuntimePseudoRelocs() {
20602060
auto *sc = dyn_cast<SectionChunk>(c);
20612061
if (!sc || !sc->live)
20622062
continue;
2063+
// Don't create pseudo relocations for sections that won't be
2064+
// mapped at runtime.
2065+
if (sc->header->Characteristics & IMAGE_SCN_MEM_DISCARDABLE)
2066+
continue;
20632067
sc->getRuntimePseudoRelocs(rels);
20642068
}
20652069

lld/test/COFF/autoimport-debug.s

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# REQUIRES: x86
2+
# RUN: split-file %s %t.dir
3+
4+
## We've got references to variable both in a .refptr and in .debug_info.
5+
## The .debug_info section should be discardable, so no pseudo relocations
6+
## need to be created in it. The .refptr section should be elimiated
7+
## and redirected to __imp_variable instead, so we shouldn't need to
8+
## create any runtime pseudo relocations. Thus, test that we can link
9+
## successfully with -runtime-pseudo-reloc:no, while keeping the
10+
## debug info.
11+
12+
# RUN: llvm-mc -triple=x86_64-windows-gnu %t.dir/lib.s -filetype=obj -o %t.dir/lib.obj
13+
# RUN: lld-link -out:%t.dir/lib.dll -dll -entry:DllMainCRTStartup %t.dir/lib.obj -lldmingw -implib:%t.dir/lib.lib
14+
15+
# RUN: llvm-mc -triple=x86_64-windows-gnu %t.dir/main.s -filetype=obj -o %t.dir/main.obj
16+
# 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
17+
18+
#--- main.s
19+
.global main
20+
.text
21+
main:
22+
movq .refptr.variable(%rip), %rax
23+
ret
24+
25+
.section .rdata$.refptr.variable,"dr",discard,.refptr.variable
26+
.global .refptr.variable
27+
.refptr.variable:
28+
.quad variable
29+
30+
.section .debug_info
31+
.long 1
32+
.quad variable
33+
.long 2
34+
35+
#--- lib.s
36+
.global variable
37+
.global DllMainCRTStartup
38+
.text
39+
DllMainCRTStartup:
40+
ret
41+
.data
42+
variable:
43+
.long 42

0 commit comments

Comments
 (0)