From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 87638 invoked by alias); 16 Apr 2015 09:42:36 -0000 Mailing-List: contact binutils-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: binutils-owner@sourceware.org Received: (qmail 87610 invoked by uid 89); 16 Apr 2015 09:42:35 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.7 required=5.0 tests=AWL,BAYES_00,SPF_PASS autolearn=ham version=3.3.2 X-HELO: eu-smtp-delivery-143.mimecast.com Received: from eu-smtp-delivery-143.mimecast.com (HELO eu-smtp-delivery-143.mimecast.com) (146.101.78.143) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 16 Apr 2015 09:42:33 +0000 Received: from cam-owa1.Emea.Arm.com (fw-tnat.cambridge.arm.com [217.140.96.140]) by uk-mta-21.uk.mimecast.lan; Thu, 16 Apr 2015 10:42:30 +0100 Received: from e104437-lin ([10.1.2.79]) by cam-owa1.Emea.Arm.com with Microsoft SMTPSVC(6.0.3790.3959); Thu, 16 Apr 2015 10:42:30 +0100 From: Jiong Wang To: binutils@sourceware.org Subject: [AArch64] Improve PC-relative relocation check for shared library Date: Thu, 16 Apr 2015 09:42:00 -0000 Message-ID: MIME-Version: 1.0 X-MC-Unique: yLVcEWuoQQmw4osVaMt5jw-1 Content-Type: multipart/mixed; boundary="=-=-=" X-IsSubscribed: yes X-SW-Source: 2015-04/txt/msg00247.txt.bz2 --=-=-= Content-Type: text/plain; charset=WINDOWS-1252 Content-Transfer-Encoding: quoted-printable Content-length: 990 For PC relative relocation against external symbol, both defined & undefined, we should reject them when building shared library, as we don't propagate them to runtime, and AArch64 dynamic linker doesn't support any of them. This is also what x86 backend is doing. Currently, when generating .so, for PC-relative relocation against external undefined symbol, we are silently generating wrong binary. while when it's external symbol defined in other .so, we will trigger unresolved_reloc_p check, while it's better to remind user to rebuild the code with -fPIC explicitly OK for trunk? thanks. 2015-04-16 Jiong. Wang bfd/ * elfnn-aarch64.c (elfNN_aarch64_final_link_relocate): Reject PC-relative relocation for external symbol. =20=20 ld/testsuite/ * ld-aarch64/pcrel.s: New testcase. * ld-aarch64/pcrel_pic_defiend_local.d: New expect file. * ld-aarch64/pcrel_pic_undefined.d: Ditto. * ld-aarch64/aarch64-elf.exp: Run them. --=20 Regards, Jiong --=-=-= Content-Type: text/x-diff; charset=WINDOWS-1252 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline; filename=pcrel.patch Content-length: 4496 diff --git a/bfd/elfnn-aarch64.c b/bfd/elfnn-aarch64.c index 37de7fa..e58c07e 100644 --- a/bfd/elfnn-aarch64.c +++ b/bfd/elfnn-aarch64.c @@ -4434,16 +4434,36 @@ elfNN_aarch64_final_link_relocate (reloc_howto_type= *howto, signed_addend, weak_undef_p); break; =20 + case BFD_RELOC_AARCH64_ADR_LO21_PCREL: + case BFD_RELOC_AARCH64_ADR_HI21_PCREL: + case BFD_RELOC_AARCH64_ADR_HI21_NC_PCREL: + case BFD_RELOC_AARCH64_LD_LO19_PCREL: + case BFD_RELOC_AARCH64_16_PCREL: + case BFD_RELOC_AARCH64_32_PCREL: + case BFD_RELOC_AARCH64_64_PCREL: + if (info->shared + && (input_section->flags & SEC_ALLOC) !=3D 0 + && (input_section->flags & SEC_READONLY) !=3D 0 + && h !=3D NULL + && !h->def_regular) + { + int howto_index =3D bfd_r_type - BFD_RELOC_AARCH64_RELOC_START; + + (*_bfd_error_handler) + (_("%B: relocation %s against external symbol `%s' can not be used" + " when making a shared object; recompile with -fPIC"), + input_bfd, elfNN_aarch64_howto_table[howto_index].name, + h->root.root.string); + bfd_set_error (bfd_error_bad_value); + return FALSE; + } + case BFD_RELOC_AARCH64_16: #if ARCH_SIZE =3D=3D 64 case BFD_RELOC_AARCH64_32: #endif case BFD_RELOC_AARCH64_ADD_LO12: - case BFD_RELOC_AARCH64_ADR_LO21_PCREL: - case BFD_RELOC_AARCH64_ADR_HI21_PCREL: - case BFD_RELOC_AARCH64_ADR_HI21_NC_PCREL: case BFD_RELOC_AARCH64_BRANCH19: - case BFD_RELOC_AARCH64_LD_LO19_PCREL: case BFD_RELOC_AARCH64_LDST8_LO12: case BFD_RELOC_AARCH64_LDST16_LO12: case BFD_RELOC_AARCH64_LDST32_LO12: @@ -4459,9 +4479,6 @@ elfNN_aarch64_final_link_relocate (reloc_howto_type *= howto, case BFD_RELOC_AARCH64_MOVW_G2: case BFD_RELOC_AARCH64_MOVW_G2_NC: case BFD_RELOC_AARCH64_MOVW_G3: - case BFD_RELOC_AARCH64_16_PCREL: - case BFD_RELOC_AARCH64_32_PCREL: - case BFD_RELOC_AARCH64_64_PCREL: case BFD_RELOC_AARCH64_TSTBR14: value =3D _bfd_aarch64_elf_resolve_relocation (bfd_r_type, place, va= lue, signed_addend, weak_undef_p); diff --git a/ld/testsuite/ld-aarch64/aarch64-elf.exp b/ld/testsuite/ld-aarc= h64/aarch64-elf.exp index 4810d60..5e354a5 100644 --- a/ld/testsuite/ld-aarch64/aarch64-elf.exp +++ b/ld/testsuite/ld-aarch64/aarch64-elf.exp @@ -104,6 +104,10 @@ run_dump_test "emit-relocs-local-addend" # test addend correctness when -r specified. run_dump_test "local-addend-r" =20 +# test error handling on pcrel relocation for shared libraries. +run_dump_test "pcrel_pic_undefined" +run_dump_test "pcrel_pic_defined_local" + run_dump_test "limit-b" run_dump_test "limit-bl" run_dump_test "farcall-section" diff --git a/ld/testsuite/ld-aarch64/pcrel.s b/ld/testsuite/ld-aarch64/pcre= l.s new file mode 100644 index 0000000..df542ef --- /dev/null +++ b/ld/testsuite/ld-aarch64/pcrel.s @@ -0,0 +1,20 @@ + .text + .align 2 +main: + # R_AARCH64_ADR_PREL_PG_HI21 + # R_AARCH64_ADR_PREL_PG_HI21_NC + # R_AARCH64_ADR_LO_21 + adrp x0, :pg_hi21:global_a + adrp x1, :pg_hi21_nc:global_a + adr x2, global_a + + #R_AARCH64_LD_PREL_LO19 + ldr x3, global_a + + # R_AARCH64_PREL16 + # R_AARCH64_PREL32 + # R_AARCH64_PREL64 + .hword global_a - . + .word global_a - . + .xword global_a - . + diff --git a/ld/testsuite/ld-aarch64/pcrel_pic_defined_local.d b/ld/testsui= te/ld-aarch64/pcrel_pic_defined_local.d new file mode 100644 index 0000000..832652f --- /dev/null +++ b/ld/testsuite/ld-aarch64/pcrel_pic_defined_local.d @@ -0,0 +1,5 @@ +#name: PC-Rel relocation against defined +#source: pcrel.s +#objdump: -r +#ld: -shared -e0 -defsym global_a=3D0x1000 +#... diff --git a/ld/testsuite/ld-aarch64/pcrel_pic_undefined.d b/ld/testsuite/l= d-aarch64/pcrel_pic_undefined.d new file mode 100644 index 0000000..de7f020 --- /dev/null +++ b/ld/testsuite/ld-aarch64/pcrel_pic_undefined.d @@ -0,0 +1,10 @@ +#name: PC-Rel relocation against undefined +#source: pcrel.s +#ld: -shared -e0 +#warning: .*: relocation R_AARCH64_ADR_PREL_PG_HI21 against external symbo= l.*fPIC.* +#warning: .*: relocation R_AARCH64_ADR_PREL_PG_HI21_NC against external sy= mbol.*fPIC.* +#warning: .*: relocation R_AARCH64_ADR_PREL_LO21 against external symbol.*= fPIC.* +#warning: .*: relocation R_AARCH64_LD_PREL_LO19 against external symbol.*f= PIC.* +#warning: .*: relocation R_AARCH64_PREL16 against external symbol.*fPIC.* +#warning: .*: relocation R_AARCH64_PREL32 against external symbol.*fPIC.* +#warning: .*: relocation R_AARCH64_PREL64 against external symbol.*fPIC.* --=-=-=--