From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: <3wV2YYgcKCPgrfxpwf3lttlqj.htrgnszynqxxtzwhj1fwj.twl@flex--maskray.bounces.google.com> Received: from mail-pj1-x104a.google.com (mail-pj1-x104a.google.com [IPv6:2607:f8b0:4864:20::104a]) by sourceware.org (Postfix) with ESMTPS id 638F038F8603 for ; Thu, 2 Jun 2022 06:50:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 638F038F8603 Received: by mail-pj1-x104a.google.com with SMTP id l2-20020a17090a72c200b001e325e14e3eso2335249pjk.7 for ; Wed, 01 Jun 2022 23:50:43 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=mmQO3hbMNJYRLrp3PdikdEn9pONts/OJp4/Q/ay6HyQ=; b=v0kWJ/S+PLlSj/S3xvLqr5lzDYf2CLDWY+kAbJ/nNK+0zev55C3hv0C2HpHBmnr3Mj B0FOTEFJ3OPB0gzJrn6MfDfqnX2udUDg1VS1cdxtTLWa1HdeCRqn/wnHL0BtWV+0rbRL gzcTl47Plx53VO2X4q+/Y5Q4aBZFIOCbRjlojlEhyO8j9+FbAMWLlaRVXiCq9CK5h7vb nywnEgut9q9hxHRncmlLyo33RAABgDDB4rDmaDOf3mQQTbex4tlPNsSu+4aNbu1adqwg VHVvovKOkdjZeKzX/WjAEQFbKa8Q7Fv1tusdtejFvWh77mloZVKzGKutOoTGs3q+Nqz4 8/hw== X-Gm-Message-State: AOAM530XfN3xesFpywlP5smZgCi7mcLYxrzt/TLELefCgCtun7/dvkwL cNkXrc6929vMPXSnaRKHSWYtPUg0RCSPUl7splVikmbbCPU1TmCMaQwxLxOHYZwEP/z9+BSPcXU lcp0txPPV77x9fagXHeSTVrAwzRywb74O8zEY0MdIueYExikUGADXUoAOu/Beu8tedQ== X-Google-Smtp-Source: ABdhPJzdwx4iEPAFLWgyBbX1H+V18bbZNamuZ5Ixc9jm2iB7HFdPKD+JFV1/h/qSmZbuvqmico3bYJT2l4p0 X-Received: from maskray1.svl.corp.google.com ([2620:15c:2ce:200:54f5:789a:e4e8:6740]) (user=maskray job=sendgmr) by 2002:a62:a501:0:b0:51b:3ee1:e499 with SMTP id v1-20020a62a501000000b0051b3ee1e499mr3566642pfm.35.1654152641819; Wed, 01 Jun 2022 23:50:41 -0700 (PDT) Date: Wed, 1 Jun 2022 23:50:38 -0700 Message-Id: <20220602065038.3198997-1-maskray@google.com> Mime-Version: 1.0 Subject: [PATCH] aarch64: Allow PC-relative relocations against protected STT_FUNC From: Fangrui Song To: binutils@sourceware.org, Szabolcs Nagy Cc: Fangrui Song Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-18.9 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 X-BeenThere: binutils@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 02 Jun 2022 06:50:45 -0000 __attribute__((visibility("protected"))) void *foo() { return (void *)foo; } gcc -fpic -shared incorrectly fails with: relocation R_AARCH64_ADR_PREL_PG_HI21 against symbol `foo' which may bind externally can not be used when making a shared object; recompile with -fPIC Call _bfd_elf_symbol_refs_local_p with local_protected==true to suppress the error. The new behavior matches gold and ld.lld. Note: if some code tries to use direct access relocations to take the address of foo (likely due to -fno-pic), the pointer equality will break, but the error should be reported on that link. --- bfd/elfnn-aarch64.c | 2 +- ld/testsuite/ld-aarch64/aarch64-elf.exp | 1 + ld/testsuite/ld-aarch64/pcrel-protected.s | 13 +++++++++++++ ld/testsuite/ld-aarch64/pcrel_pic_protected.d | 7 +++++++ 4 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 ld/testsuite/ld-aarch64/pcrel-protected.s create mode 100644 ld/testsuite/ld-aarch64/pcrel_pic_protected.d diff --git a/bfd/elfnn-aarch64.c b/bfd/elfnn-aarch64.c index cb316a928ef..2c8e3439d43 100644 --- a/bfd/elfnn-aarch64.c +++ b/bfd/elfnn-aarch64.c @@ -5878,7 +5878,7 @@ elfNN_aarch64_final_link_relocate (reloc_howto_type *howto, if (bfd_link_pic (info) && (input_section->flags & SEC_ALLOC) != 0 && (input_section->flags & SEC_READONLY) != 0 - && !SYMBOL_REFERENCES_LOCAL (info, h)) + && !_bfd_elf_symbol_refs_local_p (h, info, 1)) { int howto_index = bfd_r_type - BFD_RELOC_AARCH64_RELOC_START; diff --git a/ld/testsuite/ld-aarch64/aarch64-elf.exp b/ld/testsuite/ld-aarch64/aarch64-elf.exp index 64476f111e0..565a67aa9aa 100644 --- a/ld/testsuite/ld-aarch64/aarch64-elf.exp +++ b/ld/testsuite/ld-aarch64/aarch64-elf.exp @@ -248,6 +248,7 @@ run_dump_test_lp64 "local-addend-r" # test error handling on pcrel relocation for shared libraries. run_dump_test_lp64 "pcrel_pic_undefined" run_dump_test_lp64 "pcrel_pic_defined" +run_dump_test_lp64 "pcrel_pic_protected" run_dump_test "limit-b" run_dump_test "limit-bl" diff --git a/ld/testsuite/ld-aarch64/pcrel-protected.s b/ld/testsuite/ld-aarch64/pcrel-protected.s new file mode 100644 index 00000000000..ce2a92851ad --- /dev/null +++ b/ld/testsuite/ld-aarch64/pcrel-protected.s @@ -0,0 +1,13 @@ +.protected protected_a +.protected protected_b +.type protected_b, %object +.protected protected_c +.type protected_c, %function + +.text + adrp x0, protected_a + add x0, x0, :lo12:protected_a + adrp x0, protected_b + add x0, x0, :lo12:protected_b + adrp x0, protected_c + add x0, x0, :lo12:protected_c diff --git a/ld/testsuite/ld-aarch64/pcrel_pic_protected.d b/ld/testsuite/ld-aarch64/pcrel_pic_protected.d new file mode 100644 index 00000000000..1b6f24cf2fb --- /dev/null +++ b/ld/testsuite/ld-aarch64/pcrel_pic_protected.d @@ -0,0 +1,7 @@ +#name: PC-Rel relocation against protected +#source: pcrel-protected.s +#target: [check_shared_lib_support] +#ld: -shared -e0 --defsym protected_a=0x1000 --defsym protected_b=0x1010 --defsym protected_c=0x1020 +#readelf: -r -W +#... +There are no relocations in this file. -- 2.36.1.255.ge46751e96f-goog