From patchwork Tue Feb 20 17:55:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tatsuyuki Ishi X-Patchwork-Id: 86058 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 17B363857B82 for ; Tue, 20 Feb 2024 17:59:26 +0000 (GMT) X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-pl1-x62b.google.com (mail-pl1-x62b.google.com [IPv6:2607:f8b0:4864:20::62b]) by sourceware.org (Postfix) with ESMTPS id C0F153858D33 for ; Tue, 20 Feb 2024 17:56:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C0F153858D33 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 C0F153858D33 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::62b ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708451804; cv=none; b=PXV4LOv7p6yunWkv2BEpUS21+/OlfiUvr5LlU4FDLX5vVopZHU8096Aunr44w+TTtkiuJCgD///OyuOQbDO6/+OwA5B/trd41HqJBul5d7vhMVL52hRQzAFg8op3Ym4BnEAz0xL+Kc036kF8tCujEkY8fYVfuZIQUycCyzvSciA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708451804; c=relaxed/simple; bh=GMNky/qO4h4VcvnGoDdEsj6/ofnAA/Df7Y3QB4bv9ks=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Hh/ISyx18Wd932LpcYYp9xnyn/Zse8Y8jJQbZMdfshtfrZhJf2vjVqjOsrmh1pKAh8zHzL+SUscOHGAw8owQi2RcEsLIGik3jwWK4AX0zIhD9HpNSZzRbpFBYCI7BN7LJeuW75HATMM/0aMUYMZpI7f0WFRzZlv4zQUqEVHlTxs= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x62b.google.com with SMTP id d9443c01a7336-1dbdd58ca83so602775ad.1 for ; Tue, 20 Feb 2024 09:56:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708451799; x=1709056599; 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=h/yOCjAubBXr4UQpG8YHynJ3eSjL8rvvoG3ahJeSLJI=; b=SyyZP5d6RyaVeXzGFLoTjOpNZXs9yVrlNrc4LKgvpGumJB54eMt1xFTjhqD6rricpC e7kg7Nd/yraQuBI6E/g1MFcVJXMYwxHEDx7D0jFx0fElpXICOWHdiUCB8N4qVTqPpMAX eljzqu/QiRSok/cs4VWs92f3XfzlNW04673DUgZqPN0Cvyk0ceaqz2ZDUdFQ1yLwsNQ8 tdyJVXgQytHPm/yZvBOLmhKUVtAyokw3z/3OyK1OSy3YNDCtnQ6lD0i8xeNGznMjU0BH GvsnBersYqnMEWuMjS2wNk95lHeqqShFwSVj1BUBbHjy/l21OxPLieGFJGTdyP425g+O Kskw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708451799; x=1709056599; 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=h/yOCjAubBXr4UQpG8YHynJ3eSjL8rvvoG3ahJeSLJI=; b=VT7iOfg/RW+b9UVZihoGXoWEspmNz6lyK755Gt8R0Iw8q05Dvwdn92ZetTfzajxc9J ExnGu9J9hlmEWb39M1UfUKQbzPIeM2ERD7cJYugOJ4Zl0JMSNY1v4BOumShSiCpFWHDv XFncAcsI0g91xnEUXZQiWytHkHL9rhKkBaQTh6NeQtgObS+BNInI8pnBsIa4CnxJEn1u R/hX651xCzCWbKBN3c0Q+khBOS8/hp0GaV1sui4DK1hDFy0r3H4cuqwzoQvAJf+fR+nX /2ghlxWjUVTwoVKsg+G89zlXveg2vtS2HaCkxm/yQh7f18XmppPraJYQ0Uv8MpDY3loT wmXw== X-Gm-Message-State: AOJu0YytCfZdLjp+9P2p8FQIW18noBumgsDC80db6zDJP31nISqwIsBB OGbqYQobK3GORVHpNl7lQ9MURAAxFFxkODR/H8D+39sDyMApdLulcXxnTNidNGurGA== X-Google-Smtp-Source: AGHT+IGu8CuUMvh7lPCXxQBw4wsUTApHmfKNVC1dI7NY8Xlb4g93NsZfYE5/F1HvphxqfsZc3ZwxDw== X-Received: by 2002:a17:902:b082:b0:1dc:3c:fb67 with SMTP id p2-20020a170902b08200b001dc003cfb67mr4843555plr.5.1708451798792; Tue, 20 Feb 2024 09:56:38 -0800 (PST) Received: from localhost (zz20184013906F627101.userreverse.dion.ne.jp. [111.98.113.1]) by smtp.gmail.com with ESMTPSA id v6-20020a170903238600b001d97fe26d47sm6487737plh.34.2024.02.20.09.56.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Feb 2024 09:56:38 -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 7/9] RISC-V: Unify TLS handling in check_relocs. Date: Wed, 21 Feb 2024 02:55:54 +0900 Message-ID: <20240220175556.304692-8-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 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 X-BeenThere: binutils@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: binutils-bounces+patchwork=sourceware.org@sourceware.org With the introduction of transition, multiple relocation types may map to GOT_TLS_IE or GOT_TLS_LE and it makes more sense to unify the code path to perform checks common to them. bfd/ * elfnn-riscv.c (riscv_elf_check_relocs): Merge switch cases for TLS relocs. --- bfd/elfnn-riscv.c | 64 +++++++++++++++++++++++++++-------------------- 1 file changed, 37 insertions(+), 27 deletions(-) diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c index 1594856ccef..1fbb0698ce1 100644 --- a/bfd/elfnn-riscv.c +++ b/bfd/elfnn-riscv.c @@ -662,6 +662,24 @@ riscv_elf_copy_indirect_symbol (struct bfd_link_info *info, _bfd_elf_link_hash_copy_indirect (info, dir, ind); } +static char +riscv_elf_tls_type_from_hi_reloc (unsigned int r_type) +{ + switch (r_type) + { + case R_RISCV_TLS_GD_HI20: + return GOT_TLS_GD; + case R_RISCV_TLS_GOT_HI20: + return GOT_TLS_IE; + case R_RISCV_TLSDESC_HI20: + return GOT_TLSDESC; + case R_RISCV_TPREL_HI20: + return GOT_TLS_LE; + default: + abort (); + } +} + static bool riscv_elf_record_tls_type (bfd *abfd, struct bfd_link_info *info, struct elf_link_hash_entry *h, @@ -843,19 +861,26 @@ riscv_elf_check_relocs (bfd *abfd, struct bfd_link_info *info, switch (r_type) { - case R_RISCV_TLS_GD_HI20: - if (!riscv_elf_record_got_reference (abfd, info, h, r_symndx) - || !riscv_elf_record_tls_type (abfd, info, h, r_symndx, - GOT_TLS_GD)) - return false; - break; + case R_RISCV_TLS_GD_HI20: + case R_RISCV_TLS_GOT_HI20: + case R_RISCV_TLSDESC_HI20: + case R_RISCV_TPREL_HI20: + { + char tls_type = riscv_elf_tls_type_from_hi_reloc (r_type); - case R_RISCV_TLS_GOT_HI20: - if (!riscv_elf_record_got_reference (abfd, info, h, r_symndx) - || !riscv_elf_record_tls_type (abfd, info, h, r_symndx, - GOT_TLS_IE)) - return false; - break; + /* Local exec is only allowed for executables. */ + if (tls_type == GOT_TLS_LE && !bfd_link_executable (info)) + return bad_static_reloc (abfd, r_type, h); + + if (tls_type != GOT_TLS_LE + && !riscv_elf_record_got_reference (abfd, info, h, r_symndx)) + return false; + if ((tls_type != GOT_TLS_LE || h != NULL) + && !riscv_elf_record_tls_type (abfd, info, h, r_symndx, + tls_type)) + return false; + break; + } case R_RISCV_GOT_HI20: if (!riscv_elf_record_got_reference (abfd, info, h, r_symndx) @@ -864,12 +889,6 @@ 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, info, h, r_symndx, - GOT_TLSDESC)) - return false; - break; case R_RISCV_CALL: case R_RISCV_CALL_PLT: @@ -953,15 +972,6 @@ riscv_elf_check_relocs (bfd *abfd, struct bfd_link_info *info, break; goto static_reloc; - case R_RISCV_TPREL_HI20: - /* This is not allowed in the pic, but okay in pie. */ - if (!bfd_link_executable (info)) - return bad_static_reloc (abfd, r_type, h); - if (h != NULL - && !riscv_elf_record_tls_type (abfd, info, h, r_symndx, GOT_TLS_LE)) - return false; - break; - case R_RISCV_HI20: if (bfd_link_pic (info)) return bad_static_reloc (abfd, r_type, h);