Skip to content

Commit 303370e

Browse files
authored
[LLD] [COFF] Mark the symbol _tls_used as a GC root (#71336)
This is necessary if the object file containing _tls_used is built with -fdata-sections; if this symbol is present, lld is going to include references to it in the PE header, in the TLS_TABLE data directory. Therefore, treat the symbol as a GC root, i.e. retain it (and whatever other symbols the section chunk references) when doing doing GC (/opt:ref), if the symbol is present.
1 parent 0d3eeac commit 303370e

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

lld/COFF/Driver.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -2472,6 +2472,10 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
24722472
// resolve symbols and write indices, but don't generate native code or link).
24732473
ctx.symtab.compileBitcodeFiles();
24742474

2475+
if (Defined *d =
2476+
dyn_cast_or_null<Defined>(ctx.symtab.findUnderscore("_tls_used")))
2477+
config->gcroot.push_back(d);
2478+
24752479
// If -thinlto-index-only is given, we should create only "index
24762480
// files" and not object files. Index file creation is already done
24772481
// in addCombinedLTOObject, so we are done if that's the case.

lld/test/COFF/tls-used-gc.s

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# REQUIRES: x86
2+
# RUN: llvm-mc -triple=x86_64-windows-gnu -filetype=obj -o %t.obj %s
3+
# RUN: lld-link %t.obj /out:%t.exe /entry:main /subsystem:console /opt:ref
4+
# RUN: llvm-readobj --file-headers %t.exe | FileCheck %s
5+
6+
# CHECK: TLSTableRVA: 0x1000
7+
# CHECK: TLSTableSize: 0x28
8+
9+
.section .text@main,"xr",one_only,main
10+
.globl main
11+
main:
12+
ret
13+
14+
.section .tls$aaa
15+
tlsvar:
16+
.long 1
17+
18+
.section .rdata$_tls_used,"dr",one_only,_tls_used
19+
.globl _tls_used
20+
_tls_used:
21+
.zero 40

0 commit comments

Comments
 (0)