From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pf1-x42a.google.com (mail-pf1-x42a.google.com [IPv6:2607:f8b0:4864:20::42a]) by sourceware.org (Postfix) with ESMTPS id 539853858C30 for ; Tue, 20 Feb 2024 17:56:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 539853858C30 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 539853858C30 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::42a ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708451798; cv=none; b=IXm4fOTEmnylmIIWtDRCBNWm/P5gF1CbrvILFzekUJFi10Eq6yRI735fnRfDvODZlYXv4+kzpTLA4oUMIrw7jod7D4tz1TmIVjIdOdOJjUdXx6fp2Z2gyvoz/pcaE/87xVmKiAVwolWJ7Q7Kd9O6o62HX42ie/4TNMqiomw3DaI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708451798; c=relaxed/simple; bh=jQ0ADRuCZSC8Cu2tE24tsrYuF52AKdXLLHSsxd4sE/Q=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=U522WmyxVdaOe959pIaO0sDo4VWPhfrE/ED0koXVdzN6D3WQ8MfPlLXDIED2LMHeYt2I+LtSJgT4oBDTNE1drG/usDw3V00ec4GHts/quRWe6a9FelefTPdHAyfRFyAaief9gy80iC1sDyIHqqrbSnFVaEf2//zujXnFizjnosA= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pf1-x42a.google.com with SMTP id d2e1a72fcca58-6e28029f2b4so88395b3a.1 for ; Tue, 20 Feb 2024 09:56:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708451794; x=1709056594; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=D/cXJaGSQmvE2V6Lu9MWVIwKfk0NquMNE7zI4nMY2Cc=; b=mVjS1RhvdFl0iqucW2J5XvP6LGWv9oImR7acrQsJlFg3fWzDABCk9H70DyNUnfRyXB gjrRGL/zxAb5FtDoTpqEEegdDt+3yTT09z0b0NcZ3bMKIO+e/77ERioHeeFImlT06kIL clGxfxCX7S2wfDs16G+4mLTpDENfwqjtaKiQL4URO13AZEb5jVmEATFIgIjIhrMomW3N KsxmowQU9ceV6TvYexF3dYgLdQQcMrLgGEBLDwsqc8fEFvwp0xcwb6njb9VQjfOX6Mxr NmSt2VOQr4SonHbXRGh85MghfVpH+yo+emc/4cTREW5LPC8EuCB4hw/kDDmOoHLL2B7e iT2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708451794; x=1709056594; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=D/cXJaGSQmvE2V6Lu9MWVIwKfk0NquMNE7zI4nMY2Cc=; b=Mo8TBcjqnabIez3m2TiQFdAeKmQBmhb0ujBIcXYxrFIPF9cHI9EE6yYGauL5BPZe0D eAw+ocHQEqkrapTiI4+5P66d6jnPn2tU2qX8Ftd7wtCkTcdinNj5m0ToCNIFFEofe7/U yzKc1nv21n1p00OqC41cL3nIWq4FJT76Mu1WNrx0GfUW4qpJol3prdJTumhkOAEsX25j kCCR1/ZsIr0FqGv4HYuLDJPwqHqZlo8xFCYfp94QhFc2Jw8h/S0Gy2GsJJxnndGb5Yc4 +l+zaiqGBFs0giPn6BoOaZZlbsP8sjPiXoZhcqdpmKBsnhL3prANb7lmmYaBVzYMSgaB OzgA== X-Gm-Message-State: AOJu0YxNoXoqHSPPwPXtP7ZWR8wNK4hyWZaaMmi3ix1VCqr0+oXshoA6 dVepaddRDjpZC77E8il0bqCZ4yXlMm87FTbithPAx7LYo8Y9agod8B5IFlLRDHw3ow== X-Google-Smtp-Source: AGHT+IFPuXH6WS96ufZ1HZkQZBWD6hA8XHd+MtpflLg+nWALiLu+QUNqcSZlf6SouasNFsPYemu7cQ== X-Received: by 2002:a05:6a20:3942:b0:1a0:b51c:bf82 with SMTP id r2-20020a056a20394200b001a0b51cbf82mr2373832pzg.4.1708451793777; Tue, 20 Feb 2024 09:56:33 -0800 (PST) Received: from localhost (zz20184013906F627101.userreverse.dion.ne.jp. [111.98.113.1]) by smtp.gmail.com with ESMTPSA id c11-20020a62e80b000000b006e39d08cb3asm6075071pfi.167.2024.02.20.09.56.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Feb 2024 09:56:33 -0800 (PST) From: Tatsuyuki Ishi To: binutils@sourceware.org Cc: i@maskray.me, nelson@rivosinc.com, rui314@gmail.com, ruiu@bluewhale.systems, Tatsuyuki Ishi Subject: [PATCH v4 5/9] RISC-V: Initial ld.bfd support for TLSDESC. Date: Wed, 21 Feb 2024 02:55:52 +0900 Message-ID: <20240220175556.304692-6-ishitatsuyuki@gmail.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240220175556.304692-1-ishitatsuyuki@gmail.com> References: <20230817180852.121628-2-ishitatsuyuki@gmail.com> <20240220175556.304692-1-ishitatsuyuki@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-11.3 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Only relocation handling for now; relaxation is not implemented yet. bfd/ * elfnn-riscv.c (riscv_elf_check_relocs): Record GOT reference and paired relocation for TLSDESC_HI20. (riscv_elf_adjust_dynamic_symbol): Allocate GOT and reloc slots for TLSDESC symbols. (riscv_elf_size_dynamic_sections): Likewise but for local symbols. (tlsdescoff): New helper to determine static addend for R_TLSDESC. (riscv_elf_relocate_section): Ignore TLSDESC_CALL reloc for now (it is relaxation only). Handle TLSDESC_{LOAD,ADD}_LO12 as paired pcrel relocs. For TLS GOT slot generation, generalize the logic to handle any combination of (GD, IE, TLSDESC). Add TLSDESC Rela generation. * ld/testsuite/ld-riscv-elf/tls*: Add TLSDESC instruction sequences next to the existing GD and IE sequences. Update expectations. --- v4: Reject non-zero addend for LO12. Calculate offset correctly when DESC and GD is present but not IE. bfd/elfnn-riscv.c | 104 +++++++++++++++++++++++++---- ld/testsuite/ld-riscv-elf/tls.d | 4 +- ld/testsuite/ld-riscv-elf/tls.s | 14 ++++ ld/testsuite/ld-riscv-elf/tlsbin.d | 5 ++ 4 files changed, 114 insertions(+), 13 deletions(-) diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c index 17d44979d7e..16394110c6a 100644 --- a/bfd/elfnn-riscv.c +++ b/bfd/elfnn-riscv.c @@ -163,6 +163,7 @@ struct riscv_elf_link_hash_entry #define GOT_TLS_GD 2 #define GOT_TLS_IE 4 #define GOT_TLS_LE 8 +#define GOT_TLSDESC 16 char tls_type; }; @@ -300,6 +301,7 @@ riscv_is_insn_reloc (const reloc_howto_type *howto) #define GOT_ENTRY_SIZE RISCV_ELF_WORD_BYTES #define TLS_GD_GOT_ENTRY_SIZE (RISCV_ELF_WORD_BYTES * 2) #define TLS_IE_GOT_ENTRY_SIZE RISCV_ELF_WORD_BYTES +#define TLSDESC_GOT_ENTRY_SIZE (RISCV_ELF_WORD_BYTES * 2) /* Reserve two entries of GOTPLT for ld.so, one is used for PLT resolver, the other is used for link map. Other targets also reserve one more entry used for runtime profile? */ @@ -855,6 +857,12 @@ riscv_elf_check_relocs (bfd *abfd, struct bfd_link_info *info, return false; break; + case R_RISCV_TLSDESC_HI20: + if (!riscv_elf_record_got_reference (abfd, info, h, r_symndx) + || !riscv_elf_record_tls_type (abfd, h, r_symndx, GOT_TLSDESC)) + return false; + break; + case R_RISCV_CALL: case R_RISCV_CALL_PLT: /* These symbol requires a procedure linkage table entry. @@ -1313,7 +1321,7 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf) s = htab->elf.sgot; h->got.offset = s->size; dyn = htab->elf.dynamic_sections_created; - if (tls_type & (GOT_TLS_GD | GOT_TLS_IE)) + if (tls_type & (GOT_TLS_GD | GOT_TLS_IE | GOT_TLSDESC)) { int indx = 0; bool need_reloc = false; @@ -1334,6 +1342,14 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf) if (need_reloc) htab->elf.srelgot->size += sizeof (ElfNN_External_Rela); } + + /* TLSDESC needs one dynamic reloc and two GOT slots. */ + if (tls_type & GOT_TLSDESC) + { + s->size += TLSDESC_GOT_ENTRY_SIZE; + /* TLSDESC always use dynamic relocs. */ + htab->elf.srelgot->size += sizeof (ElfNN_External_Rela); + } } else { @@ -1561,7 +1577,7 @@ riscv_elf_size_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info) if (*local_got > 0) { *local_got = s->size; - if (*local_tls_type & (GOT_TLS_GD | GOT_TLS_IE)) + if (*local_tls_type & (GOT_TLS_GD | GOT_TLS_IE | GOT_TLSDESC)) { if (*local_tls_type & GOT_TLS_GD) { @@ -1575,6 +1591,11 @@ riscv_elf_size_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info) if (bfd_link_dll (info)) srel->size += sizeof (ElfNN_External_Rela); } + if (*local_tls_type & GOT_TLSDESC) + { + s->size += TLSDESC_GOT_ENTRY_SIZE; + srel->size += sizeof (ElfNN_External_Rela); + } } else { @@ -1725,6 +1746,17 @@ tpoff (struct bfd_link_info *info, bfd_vma address) return address - elf_hash_table (info)->tls_sec->vma - TP_OFFSET; } +/* Return the relocation value for a static TLSDESC relocation. */ + +static bfd_vma +tlsdescoff (struct bfd_link_info *info, bfd_vma address) +{ + /* If tls_sec is NULL, we should have signalled an error already. */ + if (elf_hash_table (info)->tls_sec == NULL) + return 0; + return address - elf_hash_table (info)->tls_sec->vma; +} + /* Return the global pointer's value, or 0 if it is not in use. */ static bfd_vma @@ -1764,6 +1796,7 @@ perform_relocation (const reloc_howto_type *howto, case R_RISCV_GOT_HI20: case R_RISCV_TLS_GOT_HI20: case R_RISCV_TLS_GD_HI20: + case R_RISCV_TLSDESC_HI20: if (ARCH_SIZE > 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value))) return bfd_reloc_overflow; value = ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value)); @@ -1774,6 +1807,8 @@ perform_relocation (const reloc_howto_type *howto, case R_RISCV_TPREL_LO12_I: case R_RISCV_TPREL_I: case R_RISCV_PCREL_LO12_I: + case R_RISCV_TLSDESC_LOAD_LO12: + case R_RISCV_TLSDESC_ADD_LO12: value = ENCODE_ITYPE_IMM (value); break; @@ -2192,8 +2227,8 @@ riscv_elf_relocate_section (bfd *output_bfd, bfd_vma relocation; bfd_reloc_status_type r = bfd_reloc_ok; const char *name = NULL; - bfd_vma off, ie_off; - bool unresolved_reloc, is_ie = false; + bfd_vma off, ie_off, desc_off; + bool unresolved_reloc, is_ie = false, is_desc = false; bfd_vma pc = sec_addr (input_section) + rel->r_offset; int r_type = ELFNN_R_TYPE (rel->r_info), tls_type; reloc_howto_type *howto = riscv_elf_rtype_to_howto (input_bfd, r_type); @@ -2496,6 +2531,7 @@ riscv_elf_relocate_section (bfd *output_bfd, case R_RISCV_NONE: case R_RISCV_RELAX: case R_RISCV_TPREL_ADD: + case R_RISCV_TLSDESC_CALL: case R_RISCV_COPY: case R_RISCV_JUMP_SLOT: case R_RISCV_RELATIVE: @@ -2843,6 +2879,22 @@ riscv_elf_relocate_section (bfd *output_bfd, relocation = dtpoff (info, relocation); break; + case R_RISCV_TLSDESC_LOAD_LO12: + case R_RISCV_TLSDESC_ADD_LO12: + if (rel->r_addend) + { + msg = _("%tlsdesc_lo with addend"); + r = bfd_reloc_dangerous; + break; + } + + if (riscv_record_pcrel_lo_reloc (&pcrel_relocs, relocation, rel, + input_section, info, howto, + contents)) + continue; + r = bfd_reloc_overflow; + break; + case R_RISCV_32: /* Non ABS symbol should be blocked in check_relocs. */ if (ARCH_SIZE > 32) @@ -2908,11 +2960,16 @@ riscv_elf_relocate_section (bfd *output_bfd, } break; + case R_RISCV_TLSDESC_HI20: + is_desc = true; + goto tls; + case R_RISCV_TLS_GOT_HI20: is_ie = true; - /* Fall through. */ + goto tls; case R_RISCV_TLS_GD_HI20: + tls: if (h != NULL) { off = h->got.offset; @@ -2925,12 +2982,16 @@ riscv_elf_relocate_section (bfd *output_bfd, } tls_type = _bfd_riscv_elf_tls_type (input_bfd, h, r_symndx); - BFD_ASSERT (tls_type & (GOT_TLS_IE | GOT_TLS_GD)); - /* If this symbol is referenced by both GD and IE TLS, the IE - reference's GOT slot follows the GD reference's slots. */ + BFD_ASSERT (tls_type & (GOT_TLS_IE | GOT_TLS_GD | GOT_TLSDESC)); + /* When more than one TLS type is used, the GD slot comes first, + then IE, then finally TLSDESC. */ ie_off = 0; - if ((tls_type & GOT_TLS_GD) && (tls_type & GOT_TLS_IE)) - ie_off = TLS_GD_GOT_ENTRY_SIZE; + if (tls_type & GOT_TLS_GD) + ie_off += TLS_GD_GOT_ENTRY_SIZE; + + desc_off = ie_off; + if (tls_type & GOT_TLS_IE) + desc_off += TLS_IE_GOT_ENTRY_SIZE; if ((off & 1) != 0) off &= ~1; @@ -3012,10 +3073,29 @@ riscv_elf_relocate_section (bfd *output_bfd, htab->elf.sgot->contents + off + ie_off); } } + + if (tls_type & GOT_TLSDESC) + { + /* TLSDESC is always handled by the dynamic linker and always need + * a relocation. */ + bfd_put_NN (output_bfd, 0, + htab->elf.sgot->contents + off + desc_off); + outrel.r_offset = sec_addr (htab->elf.sgot) + + off + desc_off; + outrel.r_addend = 0; + if (indx == 0) + outrel.r_addend = tlsdescoff (info, relocation); + outrel.r_info = ELFNN_R_INFO (indx, R_RISCV_TLSDESC); + riscv_elf_append_rela (output_bfd, htab->elf.srelgot, &outrel); + } } BFD_ASSERT (off < (bfd_vma) -2); - relocation = sec_addr (htab->elf.sgot) + off + (is_ie ? ie_off : 0); + relocation = sec_addr (htab->elf.sgot) + off; + if (is_ie) + relocation += ie_off; + else if (is_desc) + relocation += desc_off; if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs, pc, relocation, r_type, false)) @@ -3235,7 +3315,7 @@ riscv_elf_finish_dynamic_symbol (bfd *output_bfd, } if (h->got.offset != (bfd_vma) -1 - && !(riscv_elf_hash_entry (h)->tls_type & (GOT_TLS_GD | GOT_TLS_IE)) + && !(riscv_elf_hash_entry (h)->tls_type & (GOT_TLS_GD | GOT_TLS_IE | GOT_TLSDESC)) && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h)) { asection *sgot; diff --git a/ld/testsuite/ld-riscv-elf/tls.d b/ld/testsuite/ld-riscv-elf/tls.d index e7f20309bf1..6cf008ac7a0 100644 --- a/ld/testsuite/ld-riscv-elf/tls.d +++ b/ld/testsuite/ld-riscv-elf/tls.d @@ -2,13 +2,15 @@ #ld: --shared tmpdir/tlslib.so #readelf: -Wr -Relocation section '.rela.dyn' at offset 0x[0-9a-f]+ contains 5 entries: +Relocation section '.rela.dyn' at offset 0x[0-9a-f]+ contains 7 entries: +Offset +Info +Type +Symbol's Value +Symbol's Name \+ Addend [0-9a-f]+ +[0-9a-f]+ R_RISCV_TLS_DTPMOD64 +0 [0-9a-f]+ +[0-9a-f]+ R_RISCV_TLS_TPREL64 +4 +[0-9a-f]+ +[0-9a-f]+ R_RISCV_TLSDESC +4 [0-9a-f]+ +[0-9a-f]+ R_RISCV_TLS_DTPMOD64 +0+ sg1 \+ 0 [0-9a-f]+ +[0-9a-f]+ R_RISCV_TLS_DTPREL64 +0+ sg1 \+ 0 [0-9a-f]+ +[0-9a-f]+ R_RISCV_TLS_TPREL64 +0+ sg1 \+ 0 +[0-9a-f]+ +[0-9a-f]+ R_RISCV_TLSDESC +0+ sg1 \+ 0 Relocation section '.rela.plt' at offset 0x[0-9a-f]+ contains 1 entry: +Offset +Info +Type +Symbol's Value +Symbol's Name \+ Addend diff --git a/ld/testsuite/ld-riscv-elf/tls.s b/ld/testsuite/ld-riscv-elf/tls.s index 79e9bc20374..7cbf5ea6c72 100644 --- a/ld/testsuite/ld-riscv-elf/tls.s +++ b/ld/testsuite/ld-riscv-elf/tls.s @@ -17,6 +17,13 @@ _start: la.tls.ie a0,sg1 add a0,a0,tp + /* GD (TLSDESC), global var */ +.desc1: + auipc a0, %tlsdesc_hi(sg1) + lw t0, %tlsdesc_load_lo(.desc1)(a0) + addi a0, a0, %tlsdesc_add_lo(.desc1) + jalr t0, t0, %tlsdesc_call(.desc1) + /* GD, local var */ la.tls.gd a0,sl1 call __tls_get_addr @@ -25,4 +32,11 @@ _start: la.tls.ie a0,sl1 add a0,a0,tp + /* GD (TLSDESC), local var */ +.desc2: + auipc a0, %tlsdesc_hi(sl1) + lw t0, %tlsdesc_load_lo(.desc2)(a0) + addi a0, a0, %tlsdesc_add_lo(.desc2) + jalr t0, t0, %tlsdesc_call(.desc2) + ret diff --git a/ld/testsuite/ld-riscv-elf/tlsbin.d b/ld/testsuite/ld-riscv-elf/tlsbin.d index cdcd51a9199..79b7ade405e 100644 --- a/ld/testsuite/ld-riscv-elf/tlsbin.d +++ b/ld/testsuite/ld-riscv-elf/tlsbin.d @@ -2,6 +2,11 @@ #ld: -no-pie tmpdir/tlslib.so #readelf: -Wr +Relocation section '.rela.dyn' at offset 0x[0-9a-f]+ contains 2 entries: + +Offset +Info +Type +Symbol's Value +Symbol's Name \+ Addend +[0-9a-f]+ +[0-9a-f]+ R_RISCV_TLSDESC +4 +[0-9a-f]+ +[0-9a-f]+ R_RISCV_TLSDESC +0 + Relocation section '.rela.plt' at offset 0x[0-9a-f]+ contains 1 entry: +Offset +Info +Type +Symbol's Value +Symbol's Name \+ Addend [0-9a-f]+ +[0-9a-f]+ R_RISCV_JUMP_SLOT +[0-9a-f]+ __tls_get_addr \+ 0 -- 2.43.2