[ELF] Move TLS dynamic relocations to postScanRelocations

This temporarily increases sizeof(SymbolUnion), but allows us to mov GOT/PLT/etc
index members outside Symbol in the future.

Then, we can make TLSDESC and TLSGD use different indexes and support mixed
TLSDESC and TLSGD (tested by x86-64-tlsdesc-gd-mixed.s).

Note: needsTlsGd and needsTlsGdToIe may optionally be combined.

Test updates are due to reordered GOT entries.
This commit is contained in:
Fangrui Song
2021-12-24 22:36:49 -08:00
parent cde37a7e5a
commit 70912420bb
6 changed files with 122 additions and 100 deletions

View File

@@ -1158,13 +1158,10 @@ handleTlsRelocation(RelType type, Symbol &sym, InputSectionBase &c,
if (oneof<R_AARCH64_TLSDESC_PAGE, R_TLSDESC, R_TLSDESC_CALL, R_TLSDESC_PC,
R_TLSDESC_GOTPLT>(expr) &&
config->shared) {
if (in.got->addDynTlsEntry(sym)) {
uint64_t off = in.got->getGlobalDynOffset(sym);
mainPart->relaDyn->addAddendOnlyRelocIfNonPreemptible(
target->tlsDescRel, *in.got, off, sym, target->tlsDescRel);
}
if (expr != R_TLSDESC_CALL)
if (expr != R_TLSDESC_CALL) {
sym.needsTlsDesc = true;
c.relocations.push_back({expr, type, offset, addend, &sym});
}
return 1;
}
@@ -1200,14 +1197,7 @@ handleTlsRelocation(RelType type, Symbol &sym, InputSectionBase &c,
}
if (expr == R_TLSLD_HINT)
return 1;
if (in.got->addTlsIndex()) {
if (isLocalInExecutable)
in.got->relocations.push_back(
{R_ADDEND, target->symbolicRel, in.got->getTlsIndexOff(), 1, &sym});
else
mainPart->relaDyn->addReloc(
{target->tlsModuleIndexRel, in.got, in.got->getTlsIndexOff()});
}
sym.needsTlsLd = true;
c.relocations.push_back({expr, type, offset, addend, &sym});
return 1;
}
@@ -1223,12 +1213,7 @@ handleTlsRelocation(RelType type, Symbol &sym, InputSectionBase &c,
// Local-Dynamic sequence where offset of tls variable relative to dynamic
// thread pointer is stored in the got. This cannot be relaxed to Local-Exec.
if (expr == R_TLSLD_GOT_OFF) {
if (!sym.isInGot()) {
in.got->addEntry(sym);
uint64_t off = sym.getGotOffset();
in.got->relocations.push_back(
{R_ABS, target->tlsOffsetRel, off, 0, &sym});
}
sym.needsGotDtprel = true;
c.relocations.push_back({expr, type, offset, addend, &sym});
return 1;
}
@@ -1236,27 +1221,7 @@ handleTlsRelocation(RelType type, Symbol &sym, InputSectionBase &c,
if (oneof<R_AARCH64_TLSDESC_PAGE, R_TLSDESC, R_TLSDESC_CALL, R_TLSDESC_PC,
R_TLSDESC_GOTPLT, R_TLSGD_GOT, R_TLSGD_GOTPLT, R_TLSGD_PC>(expr)) {
if (!toExecRelax) {
if (in.got->addDynTlsEntry(sym)) {
uint64_t off = in.got->getGlobalDynOffset(sym);
if (isLocalInExecutable)
// Write one to the GOT slot.
in.got->relocations.push_back(
{R_ADDEND, target->symbolicRel, off, 1, &sym});
else
mainPart->relaDyn->addSymbolReloc(target->tlsModuleIndexRel, *in.got,
off, sym);
// If the symbol is preemptible we need the dynamic linker to write
// the offset too.
uint64_t offsetOff = off + config->wordsize;
if (sym.isPreemptible)
mainPart->relaDyn->addSymbolReloc(target->tlsOffsetRel, *in.got,
offsetOff, sym);
else
in.got->relocations.push_back(
{R_ABS, target->tlsOffsetRel, offsetOff, 0, &sym});
}
sym.needsTlsGd = true;
c.relocations.push_back({expr, type, offset, addend, &sym});
return 1;
}
@@ -1264,14 +1229,10 @@ handleTlsRelocation(RelType type, Symbol &sym, InputSectionBase &c,
// Global-Dynamic relocs can be relaxed to Initial-Exec or Local-Exec
// depending on the symbol being locally defined or not.
if (sym.isPreemptible) {
sym.needsTlsGdToIe = true;
c.relocations.push_back(
{target->adjustTlsExpr(type, R_RELAX_TLS_GD_TO_IE), type, offset,
addend, &sym});
if (!sym.isInGot()) {
in.got->addEntry(sym);
mainPart->relaDyn->addSymbolReloc(target->tlsGotRel, *in.got,
sym.getGotOffset(), sym);
}
} else {
c.relocations.push_back(
{target->adjustTlsExpr(type, R_RELAX_TLS_GD_TO_LE), type, offset,
@@ -1288,8 +1249,7 @@ handleTlsRelocation(RelType type, Symbol &sym, InputSectionBase &c,
c.relocations.push_back(
{R_RELAX_TLS_IE_TO_LE, type, offset, addend, &sym});
} else if (expr != R_TLSIE_HINT) {
if (!sym.isInGot())
addTpOffsetGotEntry(sym);
sym.needsTlsIe = true;
// R_GOT needs a relative relocation for PIC on i386 and Hexagon.
if (expr == R_GOT && config->isPic && !target->usesOnlyLowPageBits(type))
addRelativeReloc(c, offset, sym, addend, expr, type);
@@ -1638,6 +1598,61 @@ void elf::postScanRelocations() {
}
}
}
if (!sym.isTls())
return;
bool isLocalInExecutable = !sym.isPreemptible && !config->shared;
if (sym.needsTlsDesc) {
in.got->addDynTlsEntry(sym);
mainPart->relaDyn->addAddendOnlyRelocIfNonPreemptible(
target->tlsDescRel, *in.got, in.got->getGlobalDynOffset(sym), sym,
target->tlsDescRel);
}
if (sym.needsTlsGd && !sym.needsTlsDesc) {
// TODO Support mixed TLSDESC and TLS GD.
in.got->addDynTlsEntry(sym);
uint64_t off = in.got->getGlobalDynOffset(sym);
if (isLocalInExecutable)
// Write one to the GOT slot.
in.got->relocations.push_back(
{R_ADDEND, target->symbolicRel, off, 1, &sym});
else
mainPart->relaDyn->addSymbolReloc(target->tlsModuleIndexRel, *in.got,
off, sym);
// If the symbol is preemptible we need the dynamic linker to write
// the offset too.
uint64_t offsetOff = off + config->wordsize;
if (sym.isPreemptible)
mainPart->relaDyn->addSymbolReloc(target->tlsOffsetRel, *in.got,
offsetOff, sym);
else
in.got->relocations.push_back(
{R_ABS, target->tlsOffsetRel, offsetOff, 0, &sym});
}
if (sym.needsTlsGdToIe) {
in.got->addEntry(sym);
mainPart->relaDyn->addSymbolReloc(target->tlsGotRel, *in.got,
sym.getGotOffset(), sym);
}
if (sym.needsTlsLd && in.got->addTlsIndex()) {
if (isLocalInExecutable)
in.got->relocations.push_back(
{R_ADDEND, target->symbolicRel, in.got->getTlsIndexOff(), 1, &sym});
else
mainPart->relaDyn->addReloc(
{target->tlsModuleIndexRel, in.got, in.got->getTlsIndexOff()});
}
if (sym.needsGotDtprel) {
in.got->addEntry(sym);
in.got->relocations.push_back(
{R_ABS, target->tlsOffsetRel, sym.getGotOffset(), 0, &sym});
}
if (sym.needsTlsIe && !sym.needsTlsGdToIe)
addTpOffsetGotEntry(sym);
};
for (Symbol *sym : symtab->symbols())
fn(*sym);

View File

@@ -249,8 +249,9 @@ protected:
canInline(false), referenced(false), traced(false), isInIplt(false),
gotInIgot(false), isPreemptible(false), used(!config->gcSections),
folded(false), needsTocRestore(false), scriptDefined(false),
needsCopy(false), needsGot(false), needsPlt(false),
hasDirectReloc(false) {}
needsCopy(false), needsGot(false), needsPlt(false), needsTlsDesc(false),
needsTlsGd(false), needsTlsGdToIe(false), needsTlsLd(false),
needsGotDtprel(false), needsTlsIe(false), hasDirectReloc(false) {}
public:
// True if this symbol is in the Iplt sub-section of the Plt and the Igot
@@ -288,6 +289,12 @@ public:
// entries during postScanRelocations();
uint8_t needsGot : 1;
uint8_t needsPlt : 1;
uint8_t needsTlsDesc : 1;
uint8_t needsTlsGd : 1;
uint8_t needsTlsGdToIe : 1;
uint8_t needsTlsLd : 1;
uint8_t needsGotDtprel : 1;
uint8_t needsTlsIe : 1;
uint8_t hasDirectReloc : 1;
// The partition whose dynamic symbol table contains this symbol's definition.
@@ -493,9 +500,9 @@ union SymbolUnion {
};
// It is important to keep the size of SymbolUnion small for performance and
// memory usage reasons. 72 bytes is a soft limit based on the size of Defined
// memory usage reasons. 80 bytes is a soft limit based on the size of Defined
// on a 64-bit system.
static_assert(sizeof(SymbolUnion) <= 72, "SymbolUnion too large");
static_assert(sizeof(SymbolUnion) <= 80, "SymbolUnion too large");
template <typename T> struct AssertSymbol {
static_assert(std::is_trivially_destructible<T>(),

View File

@@ -66,35 +66,35 @@ addl tls1@gotntpoff(%ebx),%eax
# CHECK: Relocations [
# CHECK: Section ({{.+}}) .rel.dyn {
# CHECK-NEXT: 0x2368 R_386_TLS_DTPMOD32 -
# CHECK-NEXT: 0x2370 R_386_TLS_DTPMOD32 -
# CHECK-NEXT: 0x2358 R_386_TLS_DTPMOD32 tls0
# CHECK-NEXT: 0x235C R_386_TLS_DTPOFF32 tls0
# CHECK-NEXT: 0x2370 R_386_TLS_TPOFF tls0
# CHECK-NEXT: 0x2360 R_386_TLS_DTPMOD32 tls1
# CHECK-NEXT: 0x2364 R_386_TLS_DTPOFF32 tls1
# CHECK-NEXT: 0x2374 R_386_TLS_TPOFF tls1
# CHECK-NEXT: 0x2360 R_386_TLS_TPOFF tls0
# CHECK-NEXT: 0x2364 R_386_TLS_DTPMOD32 tls1
# CHECK-NEXT: 0x2368 R_386_TLS_DTPOFF32 tls1
# CHECK-NEXT: 0x236C R_386_TLS_TPOFF tls1
# CHECK-NEXT: }
# DIS: Disassembly of section .text:
# DIS-EMPTY:
# DIS-NEXT: <_start>:
## General dynamic model:
## -4128 and -4120 are first and second GOT entries offsets.
## -4128 and -4116 are first and second GOT entries offsets.
## Each one is a pair of records.
# DIS-NEXT: 1260: leal -4128(,%ebx), %eax
# DIS-NEXT: 1267: calll 0x12d0
# DIS-NEXT: 126c: leal -4120(,%ebx), %eax
# DIS-NEXT: 126c: leal -4116(,%ebx), %eax
# DIS-NEXT: 1273: calll 0x12d0
## Local dynamic model:
## -16 is a local module tls index offset.
# DIS-NEXT: 1278: leal -4112(%ebx), %eax
# DIS-NEXT: 1278: leal -4104(%ebx), %eax
# DIS-NEXT: 127e: calll 0x12d0
# DIS-NEXT: 1283: leal 8(%eax), %edx
# DIS-NEXT: 1289: leal -4112(%ebx), %eax
# DIS-NEXT: 1289: leal -4104(%ebx), %eax
# DIS-NEXT: 128f: calll 0x12d0
# DIS-NEXT: 1294: leal 12(%eax), %edx
## Initial exec model:
# DIS-NEXT: 129a: movl %gs:0, %eax
# DIS-NEXT: 12a0: addl -4104(%ebx), %eax
# DIS-NEXT: 12a0: addl -4120(%ebx), %eax
# DIS-NEXT: 12a6: movl %gs:0, %eax
# DIS-NEXT: 12ac: addl -4100(%ebx), %eax
# DIS-NEXT: 12ac: addl -4108(%ebx), %eax

View File

@@ -19,18 +19,18 @@
# RUN: llvm-objdump -h -d --no-show-raw-insn %t | FileCheck --check-prefix=IE %s
# GD-REL: .rel.dyn {
# GD-REL-NEXT: 0x2250 R_386_TLS_DESC -
# GD-REL-NEXT: 0x2258 R_386_TLS_DESC -
# GD-REL-NEXT: 0x2248 R_386_TLS_DESC a
# GD-REL-NEXT: 0x2258 R_386_TLS_DESC c
# GD-REL-NEXT: 0x2250 R_386_TLS_DESC c
# GD-REL-NEXT: }
# GD-REL: Hex dump of section '.got':
# GD-REL-NEXT: 0x00002248 00000000 00000000 00000000 0b000000
# GD-REL-NEXT: 0x00002258 00000000 00000000
# GD-REL-NEXT: 0x00002248 00000000 00000000 00000000 00000000
# GD-REL-NEXT: 0x00002258 00000000 0b000000
# GD-RELA: .rela.dyn {
# GD-RELA-NEXT: 0x225C R_386_TLS_DESC - 0xB
# GD-RELA-NEXT: 0x2264 R_386_TLS_DESC - 0xB
# GD-RELA-NEXT: 0x2254 R_386_TLS_DESC a 0x0
# GD-RELA-NEXT: 0x2264 R_386_TLS_DESC c 0x0
# GD-RELA-NEXT: 0x225C R_386_TLS_DESC c 0x0
# GD-RELA-NEXT: }
# GD-RELA: Hex dump of section '.got':
# GD-RELA-NEXT: 0x00002254 00000000 00000000 00000000 00000000
@@ -44,14 +44,14 @@
# GD-NEXT: calll *(%eax)
# GD-NEXT: movl %gs:(%eax), %eax
# &.rel.dyn[b]-.got.plt = 0x2250-0x2260 = -16
# GD-NEXT: leal -16(%ebx), %eax
# &.rel.dyn[b]-.got.plt = 0x2258-0x2260 = -8
# GD-NEXT: leal -8(%ebx), %eax
# GD-NEXT: movl %edx, %ebx
# GD-NEXT: calll *(%eax)
# GD-NEXT: movl %gs:(%eax), %eax
# &.rel.dyn[c]-.got.plt = 0x2258-0x2260 = -8
# GD-NEXT: leal -8(%ebx), %eax
# &.rel.dyn[c]-.got.plt = 0x2250-0x2260 = -16
# GD-NEXT: leal -16(%ebx), %eax
# GD-NEXT: calll *(%eax)
# GD-NEXT: movl %gs:(%eax), %eax

View File

@@ -35,26 +35,26 @@
## a@dtprel = st_value(a)-0x800 = 0xfffff808 is a link-time constant.
# LD32-REL: .rela.dyn {
# LD32-REL-NEXT: 0x22B4
# LD32-REL-NEXT: 0x22AC R_RISCV_TLS_DTPMOD32 - 0x0
# LD32-REL-NEXT: 0x22AC
# LD32-REL-NEXT: 0x22B0 R_RISCV_TLS_DTPMOD32 - 0x0
# LD32-REL-NEXT: }
# LD32-GOT: section '.got':
# LD32-GOT-NEXT: 0x000022a8 30220000 00000000 00f8ffff 00000000
# LD32-GOT-NEXT: 0x000022a8 30220000 00000000 00000000 00f8ffff
# LD64-REL: .rela.dyn {
# LD64-REL-NEXT: 0x2458
# LD64-REL-NEXT: 0x2448 R_RISCV_TLS_DTPMOD64 - 0x0
# LD64-REL-NEXT: 0x2448
# LD64-REL-NEXT: 0x2450 R_RISCV_TLS_DTPMOD64 - 0x0
# LD64-REL-NEXT: }
# LD64-GOT: section '.got':
# LD64-GOT-NEXT: 0x00002440 50230000 00000000 00000000 00000000
# LD64-GOT-NEXT: 0x00002450 00f8ffff ffffffff 00000000 00000000
# LD64-GOT-NEXT: 0x00002450 00000000 00000000 00f8ffff ffffffff
## rv32: &DTPMOD(a) - . = 0x22ac - 0x11d8 = 4096*1+212
## rv64: &DTPMOD(a) - . = 0x2448 - 0x12f8 = 4096*1+336
## rv32: &DTPMOD(a) - . = 0x22b0 - 0x11d8 = 4096*1+216
## rv64: &DTPMOD(a) - . = 0x2450 - 0x12f8 = 4096*1+344
# LD32: 11d8: auipc a0, 1
# LD32-NEXT: addi a0, a0, 212
# LD32-NEXT: addi a0, a0, 216
# LD64: 12f8: auipc a0, 1
# LD64-NEXT: addi a0, a0, 336
# LD64-NEXT: addi a0, a0, 344
# LD-NEXT: auipc ra, 0
# LD-NEXT: jalr 64(ra)
@@ -63,18 +63,18 @@
## a is local - its DTPMOD/DTPREL slots are link-time constants.
## a@dtpmod = 1 (main module)
# LE32-GOT: section '.got':
# LE32-GOT-NEXT: 0x00012134 00000000 01000000 00f8ffff 34210100
# LE32-GOT-NEXT: 0x00012134 00000000 34210100 01000000 00f8ffff
# LE64-GOT: section '.got':
# LE64-GOT-NEXT: 0x000121e8 00000000 00000000 01000000 00000000
# LE64-GOT-NEXT: 0x000121f8 00f8ffff ffffffff e8210100 00000000
# LE64-GOT-NEXT: 0x000121e8 00000000 00000000 e8210100 00000000
# LE64-GOT-NEXT: 0x000121f8 01000000 00000000 00f8ffff ffffffff
## rv32: DTPMOD(.LANCHOR0) - . = 0x12138 - 0x11114 = 4096*1+36
## rv64: DTPMOD(.LANCHOR0) - . = 0x121f0 - 0x111c8 = 4096*1+40
## rv32: DTPMOD(.LANCHOR0) - . = 0x1213c - 0x11114 = 4096*1+40
## rv64: DTPMOD(.LANCHOR0) - . = 0x121f8 - 0x111c8 = 4096*1+48
# LE32: 11114: auipc a0, 1
# LE32-NEXT: addi a0, a0, 36
# LE32-NEXT: addi a0, a0, 40
# LE64: 111c8: auipc a0, 1
# LE64-NEXT: addi a0, a0, 40
# LE64-NEXT: addi a0, a0, 48
# LE-NEXT: auipc ra, 0
# LE-NEXT: jalr 24(ra)

View File

@@ -19,9 +19,9 @@
# RUN: llvm-objdump -d --no-show-raw-insn %t | FileCheck --check-prefix=IE %s
# GD-RELA: .rela.dyn {
# GD-RELA-NEXT: 0x23C0 R_X86_64_TLSDESC - 0xB
# GD-RELA-NEXT: 0x23D0 R_X86_64_TLSDESC - 0xB
# GD-RELA-NEXT: 0x23B0 R_X86_64_TLSDESC a 0x0
# GD-RELA-NEXT: 0x23D0 R_X86_64_TLSDESC c 0x0
# GD-RELA-NEXT: 0x23C0 R_X86_64_TLSDESC c 0x0
# GD-RELA-NEXT: }
# GD-RELA: Hex dump of section '.got':
# GD-RELA-NEXT: 0x000023b0 00000000 00000000 00000000 00000000
@@ -29,28 +29,28 @@
# GD-RELA-NEXT: 0x000023d0 00000000 00000000 00000000 00000000
# GD-REL: .rel.dyn {
# GD-REL-NEXT: 0x23A8 R_X86_64_TLSDESC -
# GD-REL-NEXT: 0x23B8 R_X86_64_TLSDESC -
# GD-REL-NEXT: 0x2398 R_X86_64_TLSDESC a
# GD-REL-NEXT: 0x23B8 R_X86_64_TLSDESC c
# GD-REL-NEXT: 0x23A8 R_X86_64_TLSDESC c
# GD-REL-NEXT: }
# GD-REL: Hex dump of section '.got':
# GD-REL-NEXT: 0x00002398 00000000 00000000 00000000 00000000
# GD-REL-NEXT: 0x000023a8 00000000 00000000 0b000000 00000000
# GD-REL-NEXT: 0x000023b8 00000000 00000000 00000000 00000000
# GD-REL-NEXT: 0x000023a8 00000000 00000000 00000000 00000000
# GD-REL-NEXT: 0x000023b8 00000000 00000000 0b000000 00000000
## &.rela.dyn[a]-pc = 0x23B0-0x12e7 = 4297
# GD: leaq 4297(%rip), %rax
# GD-NEXT: 12e7: callq *(%rax)
# GD-NEXT: movl %fs:(%rax), %eax
## &.rela.dyn[b]-pc = 0x23C0-0x12f3 = 4301
# GD-NEXT: leaq 4301(%rip), %rcx
## &.rela.dyn[b]-pc = 0x23D0-0x12f3 = 4317
# GD-NEXT: leaq 4317(%rip), %rcx
# GD-NEXT: 12f3: movq %rcx, %rax
# GD-NEXT: callq *(%rax)
# GD-NEXT: movl %fs:(%rax), %eax
## &.rela.dyn[c]-pc = 0x23D0-0x1302 = 4302
# GD-NEXT: leaq 4302(%rip), %r15
## &.rela.dyn[c]-pc = 0x23C0-0x1302 = 4286
# GD-NEXT: leaq 4286(%rip), %r15
# GD-NEXT: 1302: movq %r15, %rax
# GD-NEXT: callq *(%rax)
# GD-NEXT: movl %fs:(%rax), %eax