From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by sourceware.org (Postfix) with ESMTPS id 38C983858C66 for ; Tue, 5 Mar 2024 00:43:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 38C983858C66 Authentication-Results: sourceware.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=flex--maskray.bounces.google.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 38C983858C66 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::b4a ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709599432; cv=none; b=NRBmSoqSQsY7zKTfpjV6e+iGGkmTPQ/QZDNH52w7RhB9qohzTGOxmxmBhaUwAhKdmgNq/DAk+d+y1MjpEY0z0lBy8LBzxS6Dqk+MY7/KyH6T8aHCwazsPmVUL/fejEtq0Ncq8wRfX8Zur1CE4fqPvLQzn/QFDv0tK126SqEJokc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709599432; c=relaxed/simple; bh=Brp+gNbY1cDE6fVb/wyQ3EBYkd42O/w8bHGyJ54KA0A=; h=DKIM-Signature:Date:Mime-Version:Message-ID:Subject:From:To; b=h+D/VxMwgxUekctrck0dHyT+wD01PkiFDiooNp4CRIGCarHCx2sU0aFrPbwgN1lAdB0zdx9hL+PZ6JlxFFhcIqOSMQo2EMRLQVPvWsdQRaQB4lWcU5JsmWoswGDFp2Tdu22c71FIsYO15CigkOIdn2Vsb5vlJc4QzcBQpNFOM08= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-dcd1779adbeso234555276.3 for ; Mon, 04 Mar 2024 16:43:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709599429; x=1710204229; darn=sourceware.org; h=content-transfer-encoding:cc:to:from:subject:message-id :mime-version:date:from:to:cc:subject:date:message-id:reply-to; bh=zX65kgIg5KjZHEd+4m5RGWKLvTLHWt6nZgD1Jf7pm/s=; b=3BLwJ1Egynm8goYpfsL+rEYEaSdDXbhuT2Zzu2mRDJwsswyNQwbc/YmDSsJ1Mtwupi gPwfVlCBVH5Sd0VjkCVJAx4Zlsdg0DOhxTULldPKGrxKLU/0VaVx6LogatLGSI/slhKR h85+Rbun49mmRT4hsRVZSEgwgYA+8hicxlJtYbd7yZGvTLq/7bDheFUAIQKOyACde5lb L4PszahUKOH4EL7xXEfmAVGpY8sXHSx1GuIXVA8fR30smlghx3jNxJ3rJAN//Ia29nVG h9prN8lOtDIxVNvs68D1qdrvfKh58iG0D3IDlTYJmos3k4gmJm5FK0F+MwKrU4UwiFQa ZKng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709599429; x=1710204229; h=content-transfer-encoding:cc:to:from:subject:message-id :mime-version:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=zX65kgIg5KjZHEd+4m5RGWKLvTLHWt6nZgD1Jf7pm/s=; b=G5W3fs52gvVi0Z0tvrL3tAbZwR247wKK9E5zYLgnJ+3wgphvMJtfQP+EPtsTAlCFfp 3ZlbyeSEPtC/6iF0q6L7ZfXS3iWis0u6TqaquTDtb0GHI5FYPHyVQ4U+6QEILst0w6fc AZMCa+fnlXVA3ntJUW8WkGipunyCaxT8kAKj6ctoP8xpWSOgr1aJk9i7TpJ//iG6WQjp HSvOHfgRnoIQZhF/a49jGvugCuupN2wYm5i40UUEFceRYMhWdFnmsEVvo/MQ7CdTydh4 7SbX0tt0Bn2wjUIXrUFwoWuxshqs8G1qvfwzYmI5xofETx0Wlp/uvAJMJeImD9UYvXKs BWNQ== X-Forwarded-Encrypted: i=1; AJvYcCWzx+j/dPBdsABbP+uB9i1b9jsuLulk/axWcR26HQ+/0FP1gXCPdqOl3qobAccLuxHdYs3Ry95ud8oCnwa5deqmXQwcmintXA== X-Gm-Message-State: AOJu0YwfniWuN0PxDdYOKpKIJ1bjP1+tUJaNha05MCrNG3m/HkOuM0jY eTE2Mdk10M2hfp8jBQtKH1RTx7wKK9DTiSRGw/WEaEzIG41H9sBS2szqwRBPv3eNYloDacb39VK v4Wq8jg== X-Google-Smtp-Source: AGHT+IFiN91XH/HuRj2Jexznq2tNuePEZzlnMs8MUKDXr7RvC7V+2WRRujL3BH/N97um9JdWzlu4d6hfNQIc X-Received: from maskray.svl.corp.google.com ([2620:15c:2d3:205:ea55:ca02:adbf:ac1b]) (user=maskray job=sendgmr) by 2002:a25:b10e:0:b0:dc6:d233:ffdd with SMTP id g14-20020a25b10e000000b00dc6d233ffddmr2940828ybj.0.1709599429583; Mon, 04 Mar 2024 16:43:49 -0800 (PST) Date: Mon, 4 Mar 2024 16:43:39 -0800 Mime-Version: 1.0 Message-ID: <20240305004339.1353851-1-maskray@google.com> Subject: [PATCH v2] sh: Make protected symbols local for FDPIC -shared From: Fangrui Song To: Alan Modra , Joseph Myers , Alexandre Oliva , binutils@sourceware.org Cc: Fangrui Song Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-19.0 required=5.0 tests=BAYES_00,DKIMWL_WL_MED,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_STOCKGEN,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL 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: From: Fangrui Song __attribute__((visibility("protected"))) void protected_fun() {} void *get_protected_fun() { return protected_fun; } links fine in the non-FDPIC mode but not in the FDPIC mode: R_SH_GOTOFFFUNCDESC relocation against external symbol "protected_fun" The issue is similar to the x86 issue fixed by commit d19a265487eda186b6977d9d15648cda9fad3298 ("x86: Make protected symbols local for -shared"). SYMBOL_FUNCDESC_LOCAL in the R_SH_GOTOFFFUNCDESC check unnecessarily considers protected symbols non-local. elf32-frv.c:FRVFDPIC_SYM_LOCAL avoids the issue by setting local_protected. Set local_protected for sh as well. Technically, the condition `! elf_hash_table (INFO)->dynamic_sections_created` does not belong to the meaning implied by SYMBOL_FUNCDESC_LOCAL, but a few `!bfd_link_pic (info) && SYMBOL_FUNCDESC_LOCAL (info, h)` uses expect that SYMBOL_FUNCDESC_LOCAL returns true when the dynamic section is not created. In addition, remove the `SYMBOL_CALLS_LOCAL (info, h) && ! SYMBOL_FUNCDESC_LOCAL (info, h)` code path, which is now dead. Nowadays, we should try setting `local_protected` to 1 everywhere. --- changes from v1: * keep dynamic_sections_created since it is needed. it could be removed, which would require extensive cleanup, so I decide not to do it. * use SYMBOL_CALLS_LOCAL and remove a dead code block as Alan suggested. --- bfd/elf32-sh.c | 22 ++++--------------- .../ld-sh/fdpic-gotofffuncdesc-protected.d | 6 +++++ .../ld-sh/fdpic-gotofffuncdesc-protected.s | 13 +++++++++++ 3 files changed, 23 insertions(+), 18 deletions(-) create mode 100644 ld/testsuite/ld-sh/fdpic-gotofffuncdesc-protected.d create mode 100644 ld/testsuite/ld-sh/fdpic-gotofffuncdesc-protected.s diff --git a/bfd/elf32-sh.c b/bfd/elf32-sh.c index bf4cb2a8242..d25b2b5808a 100644 --- a/bfd/elf32-sh.c +++ b/bfd/elf32-sh.c @@ -63,10 +63,10 @@ static bfd_vma tpoff /* Decide whether a reference to a symbol can be resolved locally or not. If the symbol is protected, we want the local address, but its function descriptor must be assigned by the dynamic linker. */ -#define SYMBOL_FUNCDESC_LOCAL(INFO, H) \ - (SYMBOL_REFERENCES_LOCAL (INFO, H) \ - || ! elf_hash_table (INFO)->dynamic_sections_created) -=0C +#define SYMBOL_FUNCDESC_LOCAL(INFO, H) = \ + (SYMBOL_CALLS_LOCAL (INFO, H) = \ + || !elf_hash_table (INFO)->dynamic_sections_created) + #define SH_PARTIAL32 true #define SH_SRC_MASK32 0xffffffff #define SH_ELF_RELOC sh_elf_reloc @@ -4279,20 +4279,6 @@ sh_elf_relocate_section (bfd *output_bfd, struct bfd= _link_info *info, /* Undefined weak symbol which will not be dynamically resolved later; leave it at zero. */ goto funcdesc_leave_zero; - else if (SYMBOL_CALLS_LOCAL (info, h) - && ! SYMBOL_FUNCDESC_LOCAL (info, h)) - { - /* If the symbol needs a non-local function descriptor - but binds locally (i.e., its visibility is - protected), emit a dynamic relocation decayed to - section+offset. This is an optimization; the dynamic - linker would resolve our function descriptor request - to our copy of the function anyway. */ - dynindx =3D elf_section_data (h->root.u.def.section - ->output_section)->dynindx; - relocation +=3D h->root.u.def.section->output_offset - + h->root.u.def.value; - } else if (! SYMBOL_FUNCDESC_LOCAL (info, h)) { /* If the symbol is dynamic and there will be dynamic diff --git a/ld/testsuite/ld-sh/fdpic-gotofffuncdesc-protected.d b/ld/tests= uite/ld-sh/fdpic-gotofffuncdesc-protected.d new file mode 100644 index 00000000000..a8d0803cccb --- /dev/null +++ b/ld/testsuite/ld-sh/fdpic-gotofffuncdesc-protected.d @@ -0,0 +1,6 @@ +#as: --isa=3Dsh4 --fdpic +#ld: -EL -mshlelf_fd -shared +#readelf: -sW +#... + +[0-9]+: +[0-9a-f]+ +[0-9a-f]+ +FUNC +GLOBAL +PROTECTED +[0-9]+ +foo +#pass diff --git a/ld/testsuite/ld-sh/fdpic-gotofffuncdesc-protected.s b/ld/tests= uite/ld-sh/fdpic-gotofffuncdesc-protected.s new file mode 100644 index 00000000000..14463149b07 --- /dev/null +++ b/ld/testsuite/ld-sh/fdpic-gotofffuncdesc-protected.s @@ -0,0 +1,13 @@ + .text + .globl f + .type f,@function +f: + mov.l .L1, r1 + add r12, r1 + .align 2 +.L1: + .long foo@GOTOFFFUNCDESC + .globl foo + .protected foo + .type foo,@function +foo: --=20 2.44.0.rc1.240.g4c46232300-goog