From: Fangrui Song Date: Sat, 1 May 2021 00:19:45 +0000 (-0700) Subject: [ELF] Simplify the condition adding .got header X-Git-Tag: llvmorg-14-init~7897 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=818b508953c7684a6c104b89c6f6ce84e961d82e;p=platform%2Fupstream%2Fllvm.git [ELF] Simplify the condition adding .got header Adopt my suggestion in https://reviews.llvm.org/D91426#2653926 , generalizing the ppc64 specific code. GNU ld and glibc ld.so has a contract about the first few entries of .got . There are somewhat complex conditions when the header is needed. This patch switches to a simpler approach: add a header unconditionally if _GLOBAL_OFFSET_TABLE_ is used or the number of entries is more than just the header. --- diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp index 76f6d37..9c80edf 100644 --- a/lld/ELF/SyntheticSections.cpp +++ b/lld/ELF/SyntheticSections.cpp @@ -646,13 +646,7 @@ void EhFrameSection::writeTo(uint8_t *buf) { GotSection::GotSection() : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, config->wordsize, ".got") { - // If ElfSym::globalOffsetTable is relative to .got and is referenced, - // increase numEntries by the number of entries used to emit - // ElfSym::globalOffsetTable. - // On PP64 we always add the header at the start. - if ((ElfSym::globalOffsetTable && !target->gotBaseSymInGotPlt) || - config->emachine == EM_PPC64) - numEntries += target->gotHeaderEntriesNum; + numEntries = target->gotHeaderEntriesNum; } void GotSection::addEntry(Symbol &sym) { @@ -696,16 +690,9 @@ void GotSection::finalizeContents() { } bool GotSection::isNeeded() const { - // We need to emit a GOT even if it's empty if there's a relocation that is - // relative to GOT(such as GOTOFFREL). - - // On PPC64 we need to check that the number of entries is more than just the - // size of the header since the header is always added. A GOT with just the - // header may not actually be needed. - if (config->emachine == EM_PPC64) - return numEntries > target->gotHeaderEntriesNum || hasGotOffRel; - - return numEntries || hasGotOffRel; + // Needed if the GOT symbol is used or the number of entries is more than just + // the header. A GOT with just the header may not be needed. + return hasGotOffRel || numEntries > target->gotHeaderEntriesNum; } void GotSection::writeTo(uint8_t *buf) { diff --git a/lld/test/ELF/ppc32-tls-gd.s b/lld/test/ELF/ppc32-tls-gd.s index c3a1ffd..dfff837 100644 --- a/lld/test/ELF/ppc32-tls-gd.s +++ b/lld/test/ELF/ppc32-tls-gd.s @@ -21,26 +21,26 @@ # GD-DYN: PPC_GOT 0x20358 # GD-REL: .rela.dyn { -# GD-REL-NEXT: 0x20358 R_PPC_DTPMOD32 a 0x0 -# GD-REL-NEXT: 0x2035C R_PPC_DTPREL32 a 0x0 -# GD-REL-NEXT: 0x20360 R_PPC_DTPMOD32 b 0x0 -# GD-REL-NEXT: 0x20364 R_PPC_DTPREL32 b 0x0 -# GD-REL-NEXT: 0x20368 R_PPC_DTPMOD32 c 0x0 -# GD-REL-NEXT: 0x2036C R_PPC_DTPREL32 c 0x0 +# GD-REL-NEXT: 0x20364 R_PPC_DTPMOD32 a 0x0 +# GD-REL-NEXT: 0x20368 R_PPC_DTPREL32 a 0x0 +# GD-REL-NEXT: 0x2036C R_PPC_DTPMOD32 b 0x0 +# GD-REL-NEXT: 0x20370 R_PPC_DTPREL32 b 0x0 +# GD-REL-NEXT: 0x20374 R_PPC_DTPMOD32 c 0x0 +# GD-REL-NEXT: 0x20378 R_PPC_DTPREL32 c 0x0 # GD-REL-NEXT: } -## &DTPMOD(a) - _GLOBAL_OFFSET_TABLE_ = 0x20078 - 0x20078 = 0 -# GD: addi 3, 31, 0 +## &DTPMOD(a) - _GLOBAL_OFFSET_TABLE_ = 12 +# GD: addi 3, 31, 12 # GD-NEXT: bl 0x1028c # GD-NEXT: lwz 3, 0(3) -## &DTPMOD(b) - _GLOBAL_OFFSET_TABLE_ = 0x20080 - 0x20078 = 8 -# GD-NEXT: addi 3, 31, 8 +## &DTPMOD(b) - _GLOBAL_OFFSET_TABLE_ = 20 +# GD-NEXT: addi 3, 31, 20 # GD-NEXT: bl 0x1028c # GD-NEXT: lwz 3, 0(3) -## &DTPMOD(c) - _GLOBAL_OFFSET_TABLE_ = 0x20088 - 0x20078 = 16 -# GD-NEXT: addi 3, 9, 16 +## &DTPMOD(c) - _GLOBAL_OFFSET_TABLE_ = 28 +# GD-NEXT: addi 3, 9, 28 # GD-NEXT: bl 0x1028c # GD-NEXT: lwz 3, 0(3) @@ -60,8 +60,8 @@ # LE-NEXT: lwz 3, 0(3) # IE-REL: .rela.dyn { -# IE-REL-NEXT: 0x10020274 R_PPC_TPREL32 b 0x0 -# IE-REL-NEXT: 0x10020278 R_PPC_TPREL32 c 0x0 +# IE-REL-NEXT: 0x10020280 R_PPC_TPREL32 b 0x0 +# IE-REL-NEXT: 0x10020284 R_PPC_TPREL32 c 0x0 # IE-REL-NEXT: } ## a is relaxed to use LE. @@ -69,12 +69,12 @@ # IE: addis 3, 2, 0 # IE-NEXT: addi 3, 3, -28664 # IE-NEXT: lwz 3, 0(3) -## &.got[0] - _GLOBAL_OFFSET_TABLE_ = 0 -# IE-NEXT: lwz 3, 0(31) +## &.got[3] - _GLOBAL_OFFSET_TABLE_ = 12 +# IE-NEXT: lwz 3, 12(31) # IE-NEXT: add 3, 3, 2 # IE-NEXT: lwz 3, 0(3) -## &.got[1] - _GLOBAL_OFFSET_TABLE_ = 4 -# IE-NEXT: lwz 3, 4(9) +## &.got[4] - _GLOBAL_OFFSET_TABLE_ = 16 +# IE-NEXT: lwz 3, 16(9) # IE-NEXT: add 3, 3, 2 # IE-NEXT: lwz 3, 0(3) diff --git a/lld/test/ELF/ppc32-tls-ie.s b/lld/test/ELF/ppc32-tls-ie.s index 9bc8ffc..9157129 100644 --- a/lld/test/ELF/ppc32-tls-ie.s +++ b/lld/test/ELF/ppc32-tls-ie.s @@ -11,15 +11,15 @@ ## A non-preemptable symbol (b) has 0 st_shndx. # IE-REL: .rela.dyn { -# IE-REL-NEXT: 0x20224 R_PPC_TPREL32 - 0xC -# IE-REL-NEXT: 0x20220 R_PPC_TPREL32 a 0x0 +# IE-REL-NEXT: 0x20230 R_PPC_TPREL32 - 0xC +# IE-REL-NEXT: 0x2022C R_PPC_TPREL32 a 0x0 # IE-REL-NEXT: } -## &.got[0] - _GLOBAL_OFFSET_TABLE_ = 0 -# IE: lwz 10, 0(9) +## &.got[3] - _GLOBAL_OFFSET_TABLE_ = 12 +# IE: lwz 10, 12(9) # IE-NEXT: add 10, 10, 2 -## &.got[1] - _GLOBAL_OFFSET_TABLE_ = 4 -# IE-NEXT: lwz 8, 4(7) +## &.got[4] - _GLOBAL_OFFSET_TABLE_ = 16 +# IE-NEXT: lwz 8, 16(7) # IE-NEXT: lbzx 10, 8, 2 # NOREL: no relocations diff --git a/lld/test/ELF/ppc32-tls-ld.s b/lld/test/ELF/ppc32-tls-ld.s index 0eda8e9..d70a52a 100644 --- a/lld/test/ELF/ppc32-tls-ld.s +++ b/lld/test/ELF/ppc32-tls-ld.s @@ -11,11 +11,11 @@ # RUN: llvm-objdump -d --no-show-raw-insn %t | FileCheck --check-prefix=LE %s # LD-REL: .rela.dyn { -# LD-REL-NEXT: 0x202C4 R_PPC_DTPMOD32 - 0x0 +# LD-REL-NEXT: 0x202D0 R_PPC_DTPMOD32 - 0x0 # LD-REL-NEXT: } -## .got - _GLOBAL_OFFSET_TABLE_ = 0 -# LD: addi 3, 30, 0 +## &.got[3] - _GLOBAL_OFFSET_TABLE_ = 12 +# LD: addi 3, 30, 12 # LD-NEXT: bl 0x101f8 ## a@dtprel = st_value(a)-0x8000 = 65540-0x8000 = 65536*1-32764 ## b@dtprel = st_value(a)-0x8000 = 131080-0x8000 = 65536*2-32760 @@ -28,8 +28,8 @@ ## Check that b@got@tlsld does not allocate another GOT entry. ## It shares In.Got->TlsIndexOff allocated when processing a@got@tlsld. -## .got - _GLOBAL_OFFSET_TABLE_ = 0 -# LD-NEXT: addi 3, 9, 0 +## &.got[3] - _GLOBAL_OFFSET_TABLE_ = 12 +# LD-NEXT: addi 3, 9, 12 # LD-NEXT: bl 0x101f8 ## b@dtprel = st_value(a)-0x8000 = 131080-0x8000 = 65536*2-32760 # LD-NEXT: addis 29, 3, 2 diff --git a/lld/test/ELF/riscv-tls-gd.s b/lld/test/ELF/riscv-tls-gd.s index 0781994..21a9d42 100644 --- a/lld/test/ELF/riscv-tls-gd.s +++ b/lld/test/ELF/riscv-tls-gd.s @@ -46,40 +46,40 @@ # RUN: llvm-readelf -x .got %t.64 | FileCheck --check-prefix=IE64-GOT %s # GD32-REL: .rela.dyn { -# GD32-REL-NEXT: 0x2310 R_RISCV_TLS_DTPMOD32 a 0x0 -# GD32-REL-NEXT: 0x2314 R_RISCV_TLS_DTPREL32 a 0x0 -# GD32-REL-NEXT: 0x2318 R_RISCV_TLS_DTPMOD32 b 0x0 -# GD32-REL-NEXT: 0x231C R_RISCV_TLS_DTPREL32 b 0x0 +# GD32-REL-NEXT: 0x2314 R_RISCV_TLS_DTPMOD32 a 0x0 +# GD32-REL-NEXT: 0x2318 R_RISCV_TLS_DTPREL32 a 0x0 +# GD32-REL-NEXT: 0x231C R_RISCV_TLS_DTPMOD32 b 0x0 +# GD32-REL-NEXT: 0x2320 R_RISCV_TLS_DTPREL32 b 0x0 # GD32-REL-NEXT: } -## &DTPMOD(a) - . = 0x2310 - 0x1250 = 4096*1+192 +## &DTPMOD(a) - . = 0x2314 - 0x1250 = 4096*1+196 # GD32: 1250: auipc a0, 1 -# GD32-NEXT: addi a0, a0, 192 +# GD32-NEXT: addi a0, a0, 196 # GD32-NEXT: auipc ra, 0 # GD32-NEXT: jalr 56(ra) -## &DTPMOD(b) - . = 0x2318 - 0x1260 = 4096*1+184 +## &DTPMOD(b) - . = 0x231C - 0x1260 = 4096*1+188 # GD32: 1260: auipc a0, 1 -# GD32-NEXT: addi a0, a0, 184 +# GD32-NEXT: addi a0, a0, 188 # GD32-NEXT: auipc ra, 0 # GD32-NEXT: jalr 40(ra) # GD64-REL: .rela.dyn { -# GD64-REL-NEXT: 0x24D0 R_RISCV_TLS_DTPMOD64 a 0x0 -# GD64-REL-NEXT: 0x24D8 R_RISCV_TLS_DTPREL64 a 0x0 -# GD64-REL-NEXT: 0x24E0 R_RISCV_TLS_DTPMOD64 b 0x0 -# GD64-REL-NEXT: 0x24E8 R_RISCV_TLS_DTPREL64 b 0x0 +# GD64-REL-NEXT: 0x24D8 R_RISCV_TLS_DTPMOD64 a 0x0 +# GD64-REL-NEXT: 0x24E0 R_RISCV_TLS_DTPREL64 a 0x0 +# GD64-REL-NEXT: 0x24E8 R_RISCV_TLS_DTPMOD64 b 0x0 +# GD64-REL-NEXT: 0x24F0 R_RISCV_TLS_DTPREL64 b 0x0 # GD64-REL-NEXT: } -## &DTPMOD(a) - . = 0x24d0 - 0x1398 = 4096*1+312 +## &DTPMOD(a) - . = 0x24d8 - 0x1398 = 4096*1+320 # GD64: 1398: auipc a0, 1 -# GD64-NEXT: addi a0, a0, 312 +# GD64-NEXT: addi a0, a0, 320 # GD64-NEXT: auipc ra, 0 # GD64-NEXT: jalr 64(ra) -## &DTPMOD(b) - . = 0x24e0 - 0x13a8 = 4096*1+312 +## &DTPMOD(b) - . = 0x24e8 - 0x13a8 = 4096*1+320 # GD64: 13a8: auipc a0, 1 -# GD64-NEXT: addi a0, a0, 312 +# GD64-NEXT: addi a0, a0, 320 # GD64-NEXT: auipc ra, 0 # GD64-NEXT: jalr 48(ra) @@ -89,27 +89,31 @@ ## a@dtprel = st_value(a)-0x800 = 0xfffff808 ## b@dtprel = st_value(b)-0x800 = 0xfffff80c # LE32-GOT: section '.got': -# LE32-GOT-NEXT: 0x{{[0-9a-f]+}} 01000000 08f8ffff 01000000 0cf8ffff +# LE32-GOT-NEXT: 0x[[#%x,A:]] [[#%x,GOT:]] 01000000 08f8ffff 01000000 +# LE32-GOT-NEXT: 0x[[#%x,A:]] 0cf8ffff # LE64-GOT: section '.got': -# LE64-GOT-NEXT: 0x{{[0-9a-f]+}} 01000000 00000000 08f8ffff ffffffff -# LE64-GOT-NEXT: 0x{{[0-9a-f]+}} 01000000 00000000 0cf8ffff ffffffff +# LE64-GOT-NEXT: 0x[[#%x,A:]] [[#%x,GOT:]] [[#%x,GOT:]] 01000000 00000000 +# LE64-GOT-NEXT: 0x[[#%x,A:]] 08f8ffff ffffffff 01000000 00000000 +# LE64-GOT-NEXT: 0x[[#%x,A:]] 0cf8ffff ffffffff ## a is local - relaxed to LE - its DTPMOD/DTPREL slots are link-time constants. ## b is external - DTPMOD/DTPREL dynamic relocations are required. # IE32-REL: .rela.dyn { -# IE32-REL-NEXT: 0x12230 R_RISCV_TLS_DTPMOD32 b 0x0 -# IE32-REL-NEXT: 0x12234 R_RISCV_TLS_DTPREL32 b 0x0 +# IE32-REL-NEXT: 0x12234 R_RISCV_TLS_DTPMOD32 b 0x0 +# IE32-REL-NEXT: 0x12238 R_RISCV_TLS_DTPREL32 b 0x0 # IE32-REL-NEXT: } # IE32-GOT: section '.got': -# IE32-GOT-NEXT: 0x00012228 01000000 08f8ffff 00000000 00000000 +# IE32-GOT-NEXT: 0x00012228 c8210100 01000000 08f8ffff 00000000 +# IE32-GOT-NEXT: 0x00012238 00000000 # IE64-REL: .rela.dyn { -# IE64-REL-NEXT: 0x12390 R_RISCV_TLS_DTPMOD64 b 0x0 -# IE64-REL-NEXT: 0x12398 R_RISCV_TLS_DTPREL64 b 0x0 +# IE64-REL-NEXT: 0x12398 R_RISCV_TLS_DTPMOD64 b 0x0 +# IE64-REL-NEXT: 0x123A0 R_RISCV_TLS_DTPREL64 b 0x0 # IE64-REL-NEXT: } # IE64-GOT: section '.got': -# IE64-GOT-NEXT: 0x00012380 01000000 00000000 08f8ffff ffffffff -# IE64-GOT-NEXT: 0x00012390 00000000 00000000 00000000 00000000 +# IE64-GOT-NEXT: 0x00012380 c0220100 00000000 01000000 00000000 +# IE64-GOT-NEXT: 0x00012390 08f8ffff ffffffff 00000000 00000000 +# IE64-GOT-NEXT: 0x000123a0 00000000 00000000 la.tls.gd a0,a call __tls_get_addr@plt diff --git a/lld/test/ELF/riscv-tls-ie.s b/lld/test/ELF/riscv-tls-ie.s index 424edb9..6d58c99 100644 --- a/lld/test/ELF/riscv-tls-ie.s +++ b/lld/test/ELF/riscv-tls-ie.s @@ -24,27 +24,27 @@ # IE32-REL: FLAGS STATIC_TLS # IE32-REL: .rela.dyn { -# IE32-REL-NEXT: 0x2218 R_RISCV_TLS_TPREL32 - 0xC -# IE32-REL-NEXT: 0x2214 R_RISCV_TLS_TPREL32 a 0x0 +# IE32-REL-NEXT: 0x221C R_RISCV_TLS_TPREL32 - 0xC +# IE32-REL-NEXT: 0x2218 R_RISCV_TLS_TPREL32 a 0x0 # IE32-REL-NEXT: } # IE64-REL: FLAGS STATIC_TLS # IE64-REL: .rela.dyn { -# IE64-REL-NEXT: 0x2370 R_RISCV_TLS_TPREL64 - 0xC -# IE64-REL-NEXT: 0x2368 R_RISCV_TLS_TPREL64 a 0x0 +# IE64-REL-NEXT: 0x2378 R_RISCV_TLS_TPREL64 - 0xC +# IE64-REL-NEXT: 0x2370 R_RISCV_TLS_TPREL64 a 0x0 # IE64-REL-NEXT: } -## rv32: &.got[0] - . = 0x2214 - . = 4096*1+112 -## rv64: &.got[0] - . = 0x2368 - . = 4096*1+200 +## rv32: &.got[0] - . = 0x2218 - . = 4096*1+116 +## rv64: &.got[0] - . = 0x2378 - . = 4096*1+208 # IE: auipc a4, 1 -# IE32-NEXT: lw a4, 112(a4) -# IE64-NEXT: ld a4, 200(a4) +# IE32-NEXT: lw a4, 116(a4) +# IE64-NEXT: ld a4, 208(a4) # IE-NEXT: add a4, a4, tp -## rv32: &.got[1] - . = 0x2218 - . = 4096*1+104 -## rv64: &.got[1] - . = 0x2370 - . = 4096*1+196 +## rv32: &.got[1] - . = 0x221c - . = 4096*1+108 +## rv64: &.got[1] - . = 0x2378 - . = 4096*1+204 # IE: auipc a5, 1 -# IE32-NEXT: lw a5, 104(a5) -# IE64-NEXT: ld a5, 196(a5) +# IE32-NEXT: lw a5, 108(a5) +# IE64-NEXT: ld a5, 204(a5) # IE-NEXT: add a5, a5, tp # NOREL: no relocations @@ -52,23 +52,24 @@ # a@tprel = st_value(a) = 0x8 # b@tprel = st_value(a) = 0xc # LE32-GOT: section '.got': -# LE32-GOT-NEXT: 0x0001212c 08000000 0c000000 +# LE32-GOT-NEXT: 0x0001212c 00000000 08000000 0c000000 # LE64-GOT: section '.got': -# LE64-GOT-NEXT: 0x000121e0 08000000 00000000 0c000000 00000000 +# LE64-GOT-NEXT: 0x000121e0 00000000 00000000 08000000 00000000 +# LE64-GOT-NEXT: 0x000121f0 0c000000 00000000 -## rv32: &.got[0] - . = 0x1212c - 0x11114 = 4096*1+24 -## rv64: &.got[0] - . = 0x121e0 - 0x111c8 = 4096*1+24 +## rv32: &.got[0] - . = 0x12130 - 0x11114 = 4096*1+28 +## rv64: &.got[0] - . = 0x121e8 - 0x111c8 = 4096*1+32 # LE32: 11114: auipc a4, 1 -# LE32-NEXT: lw a4, 24(a4) +# LE32-NEXT: lw a4, 28(a4) # LE64: 111c8: auipc a4, 1 -# LE64-NEXT: ld a4, 24(a4) +# LE64-NEXT: ld a4, 32(a4) # LE-NEXT: add a4, a4, tp -## rv32: &.got[1] - . = 0x12130 - 0x11120 = 4096*1+16 -## rv64: &.got[1] - . = 0x121e8 - 0x111d4 = 4096*1+20 +## rv32: &.got[1] - . = 0x12134 - 0x11120 = 4096*1+20 +## rv64: &.got[1] - . = 0x121f0 - 0x111d4 = 4096*1+28 # LE32: 11120: auipc a5, 1 -# LE32-NEXT: lw a5, 16(a5) +# LE32-NEXT: lw a5, 20(a5) # LE64: 111d4: auipc a5, 1 -# LE64-NEXT: ld a5, 20(a5) +# LE64-NEXT: ld a5, 28(a5) # LE-NEXT: add a5, a5, tp la.tls.ie a4,a