From 6ca8fde226e907db13bc538e721af8724f0e92d0 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Wed, 24 Nov 2021 23:17:13 -0800 Subject: [PATCH] [ELF] Emit DF_STATIC_TLS only for -shared This matches GNU ld and saves 2 words for executables. --- lld/ELF/SyntheticSections.cpp | 2 +- lld/test/ELF/i386-tls-opt-iele-nopic.s | 12 ++++++------ lld/test/ELF/x86-64-static-tls-model.s | 10 +++++++--- lld/test/ELF/x86-64-tls-ie.s | 16 ++++++++-------- 4 files changed, 22 insertions(+), 18 deletions(-) diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp index a47b8d7..78d41e5 100644 --- a/lld/ELF/SyntheticSections.cpp +++ b/lld/ELF/SyntheticSections.cpp @@ -1382,7 +1382,7 @@ template void DynamicSection::finalizeContents() { } if (!config->zText) dtFlags |= DF_TEXTREL; - if (config->hasTlsIe) + if (config->hasTlsIe && config->shared) dtFlags |= DF_STATIC_TLS; if (dtFlags) diff --git a/lld/test/ELF/i386-tls-opt-iele-nopic.s b/lld/test/ELF/i386-tls-opt-iele-nopic.s index 641552d..8dd0cf6 100644 --- a/lld/test/ELF/i386-tls-opt-iele-nopic.s +++ b/lld/test/ELF/i386-tls-opt-iele-nopic.s @@ -14,8 +14,8 @@ // GOTREL-NEXT: SHF_ALLOC // GOTREL-NEXT: SHF_WRITE // GOTREL-NEXT: ] -// GOTREL-NEXT: Address: 0x402258 -// GOTREL-NEXT: Offset: 0x258 +// GOTREL-NEXT: Address: 0x402250 +// GOTREL-NEXT: Offset: 0x250 // GOTREL-NEXT: Size: 8 // GOTREL-NEXT: Link: 0 // GOTREL-NEXT: Info: 0 @@ -24,8 +24,8 @@ // GOTREL-NEXT: } // GOTREL: Relocations [ // GOTREL-NEXT: Section ({{.*}}) .rel.dyn { -// GOTREL-NEXT: 0x402258 R_386_TLS_TPOFF tlsshared0 -// GOTREL-NEXT: 0x40225C R_386_TLS_TPOFF tlsshared1 +// GOTREL-NEXT: 0x402250 R_386_TLS_TPOFF tlsshared0 +// GOTREL-NEXT: 0x402254 R_386_TLS_TPOFF tlsshared1 // GOTREL-NEXT: } // GOTREL-NEXT: ] @@ -45,10 +45,10 @@ // DISASM-NEXT: addl $0xfffffffc, %ecx // DISASM-NEXT: movl %gs:(%ecx), %eax /// &.got[0] -// DISASM-NEXT: movl 0x402258, %ecx +// DISASM-NEXT: movl 0x402250, %ecx // DISASM-NEXT: movl %gs:(%ecx), %eax /// &.got[1] -// DISASM-NEXT: addl 0x40225c, %ecx +// DISASM-NEXT: addl 0x402254, %ecx // DISASM-NEXT: movl %gs:(%ecx), %eax .type tlslocal0,@object diff --git a/lld/test/ELF/x86-64-static-tls-model.s b/lld/test/ELF/x86-64-static-tls-model.s index 997def3..0908462 100644 --- a/lld/test/ELF/x86-64-static-tls-model.s +++ b/lld/test/ELF/x86-64-static-tls-model.s @@ -1,15 +1,19 @@ # REQUIRES: x86 ## In this test R_X86_64_GOTTPOFF is a IE relocation (static TLS model), -## test check we add STATIC_TLS flag. +## test check we add STATIC_TLS flag for -shared. # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o -# RUN: ld.lld %t.o -o %t1 -shared -# RUN: llvm-readobj --dynamic-table %t1 | FileCheck %s +# RUN: ld.lld %t.o -o %t.so -shared +# RUN: llvm-readobj --dynamic-table %t.so | FileCheck %s +# RUN: ld.lld %t.o -o %t -pie +# RUN: llvm-readobj --dynamic-table %t | FileCheck %s --check-prefix=EXE # CHECK: DynamicSection [ # CHECK: FLAGS STATIC_TLS +# EXE-NOT: FLAGS STATIC_TLS + .section ".tdata", "awT", @progbits .globl var var: diff --git a/lld/test/ELF/x86-64-tls-ie.s b/lld/test/ELF/x86-64-tls-ie.s index 9dd7010..e5510f8 100644 --- a/lld/test/ELF/x86-64-tls-ie.s +++ b/lld/test/ELF/x86-64-tls-ie.s @@ -15,7 +15,7 @@ // CHECK-NEXT: SHF_WRITE // CHECK-NEXT: ] // CHECK-NEXT: Address: [[ADDR:.*]] -// CHECK-NEXT: Offset: 0x3C0 +// CHECK-NEXT: Offset: 0x3B0 // CHECK-NEXT: Size: 16 // CHECK-NEXT: Link: 0 // CHECK-NEXT: Info: 0 @@ -26,21 +26,21 @@ // CHECK: Relocations [ // CHECK-NEXT: Section (5) .rela.dyn { // CHECK-NEXT: [[ADDR]] R_X86_64_TPOFF64 tls1 0x0 -// CHECK-NEXT: 0x2023C8 R_X86_64_TPOFF64 tls0 0x0 +// CHECK-NEXT: 0x2023B8 R_X86_64_TPOFF64 tls0 0x0 // CHECK-NEXT: } // CHECK-NEXT: ] -// 0x2012d0 + 4329 + 7 = 0x2023C0 -// 0x2012dA + 4327 + 7 = 0x2023C8 -// 0x2012e4 + 4317 + 7 = 0x2023C8 +// 0x2012d0 + 4313 + 7 = 0x2023B0 +// 0x2012dA + 4311 + 7 = 0x2023B8 +// 0x2012e4 + 4301 + 7 = 0x2023B8 // DISASM: Disassembly of section .text: // DISASM-EMPTY: // DISASM-NEXT:
: -// DISASM-NEXT: 2012d0: {{.*}} movq 4329(%rip), %rax +// DISASM-NEXT: 2012d0: {{.*}} movq 4313(%rip), %rax // DISASM-NEXT: 2012d7: {{.*}} movl %fs:(%rax), %eax -// DISASM-NEXT: 2012da: {{.*}} movq 4327(%rip), %rax +// DISASM-NEXT: 2012da: {{.*}} movq 4311(%rip), %rax // DISASM-NEXT: 2012e1: {{.*}} movl %fs:(%rax), %eax -// DISASM-NEXT: 2012e4: {{.*}} movq 4317(%rip), %rax +// DISASM-NEXT: 2012e4: {{.*}} movq 4301(%rip), %rax // DISASM-NEXT: 2012eb: {{.*}} movl %fs:(%rax), %eax // DISASM-NEXT: 2012ee: {{.*}} retq -- 2.7.4