From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pf1-x436.google.com (mail-pf1-x436.google.com [IPv6:2607:f8b0:4864:20::436]) by sourceware.org (Postfix) with ESMTPS id 4279B3858D26 for ; Thu, 23 May 2024 03:54:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4279B3858D26 Authentication-Results: sourceware.org; dmarc=pass (p=reject dis=none) header.from=sifive.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=sifive.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 4279B3858D26 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::436 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716436445; cv=none; b=pbUl72BYtSPMXgk13VPN/HJBpQGf/KsjE2uMJM+SEq2TXBlmgvyZcR9kWKtL0rxt+gkKx1Tzd+hX0kuk7AnADz5aTHUoADtw9sifIQHGW7IsAqNsRRAiEzSiB89JyOmupLAlKtOJgr0R+lvbVdCf+pBf/6XH5RJwsYBBQLRUXCg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716436445; c=relaxed/simple; bh=a42q1+RvmoJ3t/OfNJJtVeDCcIYa0g4AfAZpJDMRoe0=; h=DKIM-Signature:From:Message-Id:Mime-Version:Subject:Date:To; b=brOPpfwtMlhWR8Wne9gxQx0CXTF5Q1suoAVaiNKubjv1/y3bd4Wnd+VlxeZUIY8io5n2SBfz0dm5/gzT9JBpi6EpCedPE24HdZcyTvcf+XRf5e1FZ7NeV8NOVKe3lvNcH+1Bo1BLEWQ91iIZhSXr6cLQNO6J8gtF484tcU62X8Q= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pf1-x436.google.com with SMTP id d2e1a72fcca58-6f850ff30c0so112312b3a.0 for ; Wed, 22 May 2024 20:54:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1716436441; x=1717041241; darn=sourceware.org; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:from:to:cc:subject:date:message-id:reply-to; bh=UGBx2yBEz2sHd5FXGbvKVxTtPjr/9gwSBBpsI2sPwrQ=; b=mgLAzVSTTJHY2GRVmZ4cgxigKHtc/Bh1igYnJBKvcPC0NeA5zELgBbihbwJAaeF0Io Lby7AodMlCsN1iJaqfzU7suF+obcndlIa+4KDEgVfdTmElVYGYoswliUTSI9RCJZNCsZ Uyt+4ayFKYx7oTJKHHPkgw9fheyPhAO025jcuveJxJWiRZX558EopXc7apGCEHiAVuAf P8wV8S6T/MHYRlCXwSOU5tsnP1+z9kWBWNNgov0NqOiRsJWTn0QG7Yxfu8i1zy92D+ij p8FWS6nVs1es73iHK7xDdFj63h8dkjaWNdEghKc2N0V4kNfEyujMMEuMqghzvOuQOQIg AF9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716436441; x=1717041241; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=UGBx2yBEz2sHd5FXGbvKVxTtPjr/9gwSBBpsI2sPwrQ=; b=UcIsO+zBvpe7OZY8BkGQ06Gr3PPdcrfaIodM7lquvB0AY2dPRLaWrrS//+1gq+Xi8K 68D/vRImG60eb0vccOMXv86flDawc8uDc/OqjxJo8QfFd9eDvWaCIypY737WjpiTYFIA TQFX1pwhVi/6/zYEzd4Beg6drywaxU13LBPNNmRbMspg7m0STN73wSrZ+H0HGvDuaoxJ iKe3hA7kZSUckei9yvsHXhv+OaDa+hEppu5w3ZKw5ZMnaKqzi2kQVSAKAlK97vys4CUy XWe9j7ACYrzKTWQeRDeB4fjvCNe5+o/z3fZEEVVpMeyKNyrDWMghxPntZNGl9IcNyaSE iZcw== X-Gm-Message-State: AOJu0Yx+CNr8YGRbwT3G+hZ1ReWBz91CAK9QqkGlV2jzGAq/P1/0ImXY AnXFAjpjHZW7UeV5hNKhptIpHd25pPhAI5VZp4ghxoO20WOwU6GvxgIGPIeYcNo= X-Google-Smtp-Source: AGHT+IECPKpETYoB+IdIpr9vgTwMpYe/4EZEme/pwEtc5//V2VVOvoQqwOZte1GHhrumweGV7MOBUA== X-Received: by 2002:a05:6a00:189d:b0:6f6:7c6a:2c14 with SMTP id d2e1a72fcca58-6f6d60071d6mr4711399b3a.4.1716436440938; Wed, 22 May 2024 20:54:00 -0700 (PDT) Received: from smtpclient.apple ([136.226.240.187]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-6f4d2b2f7dcsm23164342b3a.206.2024.05.22.20.53.59 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 22 May 2024 20:54:00 -0700 (PDT) From: Hau Hsu Message-Id: <53B9A941-BE25-4DB5-8F65-607890181EDF@sifive.com> Content-Type: multipart/alternative; boundary="Apple-Mail=_9DC85A97-19D8-47B3-82D0-4F376267A66E" Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3774.400.31\)) Subject: Re: [PATCH 2/2] riscv: Fix R_RISCV_IRELATIVE overwrite and order issues Date: Thu, 23 May 2024 11:53:47 +0800 In-Reply-To: Cc: Binutils , Kito Cheng To: Nelson Chu References: <20240506044520.2780464-1-hau.hsu@sifive.com> <20240506044520.2780464-2-hau.hsu@sifive.com> <89C7C17E-5E9D-4E00-A3AE-FF1F014C0939@sifive.com> <6B8C7B37-CAB2-40E0-9C3F-CA8F8B37D367@sifive.com> X-Mailer: Apple Mail (2.3774.400.31) X-Spam-Status: No, score=-12.5 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,HTML_MESSAGE,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP 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: --Apple-Mail=_9DC85A97-19D8-47B3-82D0-4F376267A66E Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 I used riscv-gnu-toolchain to run gcc test suite (linux) and got no regress= ions: =3D=3D=3D=3D=3D=3D=3D=3D=3D Summary of gcc testsuite =3D=3D= =3D=3D=3D=3D=3D=3D=3D | # of unexpected case / # of unique unexpected= case | gcc | g++ | gfortran | rv64imafdc/ lp64d/ medlow | 0 / 0 | 0 / 0 | 0 / 0 | >> I think the quite easy solution is - don't use riscv_elf_append_rela to = emit dynamic IRELATIVE for R_RISCV_32/64 into iplt section. Seems like com= mit 51a8a7c2e3cc only handles the finish_dynamic_symbol, but forgot to appl= y a similar fix in the relocate_section. It seems my change works. But I can try to modify code as suggested. Hau Hsu > On May 17, 2024, at 10:32=E2=80=AFAM, Hau Hsu wrote: >=20 > Let me run more toolchain integrated tests. > Thanks! >=20 > Hau >=20 >=20 >=20 >> On May 16, 2024, at 4:28=E2=80=AFPM, Nelson Chu wr= ote: >>=20 >> I think the quite easy solution is - don't use riscv_elf_append_rela to = emit dynamic IRELATIVE for R_RISCV_32/64 into iplt section. Seems like com= mit 51a8a7c2e3cc only handles the finish_dynamic_symbol, but forgot to appl= y a similar fix in the relocate_section. >>=20 >> https://github.com/bminor/binutils-gdb/blob/master/bfd/elfnn-riscv.c#L24= 08 >> diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c >> index 604f6de4511..dca446c0495 100644 >> --- a/bfd/elfnn-riscv.c >> +++ b/bfd/elfnn-riscv.c >> @@ -2399,13 +2399,16 @@ riscv_elf_relocate_section (bfd *output_bfd, >> 2. .rela.got section in dynamic executable. >> 3. .rela.iplt section in static executable. */ >> if (bfd_link_pic (info)) >> - sreloc =3D htab->elf.irelifunc; >> + riscv_elf_append_rela (output_bfd, htab->elf.ireli= func, &outrel); >> else if (htab->elf.splt !=3D NULL) >> - sreloc =3D htab->elf.srelgot; >> + riscv_elf_append_rela (output_bfd, htab->elf.srelg= ot, &outrel); >> else >> - sreloc =3D htab->elf.irelplt; >> - >> - riscv_elf_append_rela (output_bfd, sreloc, &outrel); >> + { >> + const struct elf_backend_data *bed =3D get_elf_b= ackend_data (output_bfd); >> + bfd_vma iplt_idx =3D htab->last_iplt_index--; >> + bfd_byte *loc =3D htab->elf.irelplt->contents + = iplt_idx * sizeof (ElfNN_External_Rela); >> + bed->s->swap_reloca_out (output_bfd, &outrel, lo= c); >> + } >>=20 >> /* If this reloc is against an external symbol, we >> do not want to fiddle with the addend. Otherwise, >>=20 >> The above changes seem to fix the testcase you provided, but without tes= ting fully riscv-gnu-toolchain regressions. >> Or we should find a way to handle reloc_index for iplt, and all use risc= v_elf_append_rela to emit the dynamic relocation. >>=20 >> Nelson >>=20 >> On Thu, May 16, 2024 at 3:30=E2=80=AFPM Hau Hsu > wrote: >>>=20 >>>> On May 16, 2024, at 3:07=E2=80=AFPM, Nelson Chu > wrote: >>>>=20 >>>> On Thu, May 16, 2024 at 2:16=E2=80=AFPM Hau Hsu > wrote: >>>>> Hi Nelson, >>>>>=20 >>>>> Sorry for the late reply. >>>>>=20 >>>>>> On May 8, 2024, at 9:00=E2=80=AFAM, Nelson Chu > wrote: >>>>>>=20 >>>>>> Can you provide the testcase to show the case which I mentioned in t= he pr13302? Which is that an ifunc with a dynamic jump slot calls another = ifunc, and are not in the rel.dyn.=20 >>>>> I am not quite sure what's the issue you mentioned in PR13302. >>>>> You mean the order issue of cause by one ifunc (generates JUMP_SLOT[3= 2|64]) calls another JUMP_SLOT[32|64] ifunc? >>>>>=20 >>>>>> Since according to the testcase below, it seems no requirement to ap= ply this fix though. >>>>> The new test case uses two methods to call ifuncs: >>>>> 1. Through a normal function call: PLT + GOT >>>>> 2. Through a global function pointer: GOT only >>>>>=20 >>>>> Without the fix, the relocation of the first method overwrites the se= cond's. >>>>> The relocation section of my test case would be: >>>>>=20 >>>>> Relocation section '.rela.plt' at offset 0x94 contains 2 entries: >>>>> Offset Info Type Sym. Value Symbol's Name + A= ddend >>>>> 000110dc 0000003a R_RISCV_IRELATIVE 100c0 >>>>> 00000000 00000000 R_RISCV_NONE 0 >>>>>=20 >>>>>=20 >>>>> With the fix, it becomes >>>>>=20 >>>>> Relocation section '.rela.plt' at offset 0x94 contains 2 entries: >>>>> Offset Info Type Sym. Value Symbol's Name + A= ddend >>>>> 000110e0 0000003a R_RISCV_IRELATIVE 100c0 >>>>> 000110dc 0000003a R_RISCV_IRELATIVE 100c0 >>>>>=20 >>>>=20 >>>> So it seems like the overwrite problem, not the order problem we were = discussing in the pr13302... >>>=20 >>> Yes. Sorry that I didn't explain the whole story well. >>>=20 >>> This PR is originally to fix the overwrite problem, as my commit messag= e says:=20 >>> > This commit resolved two issues:=20 >>> > 1. When an ifunc is referenced by a pointer, the relocation of >>> > the pointer in .rela.plt would be overwritten by normal ifunc call. >>> We found the issue when building glibc testbench statically. >>>=20 >>> To fix this issue, I sent a PR (https://sourceware.org/pipermail/binuti= ls/2023-July/128485.html) about a year ago.=20 >>> The PR use the method smilier to your previous commit, i.e. >>> https://sourceware.org/git/?p=3Dbinutils-gdb.git;a=3Dcommit;h=3D51a8a7c= 2e3cc0730831963651a55d23d1fae624d >>> Then you suggested to check whether the relocation order is correct. >>> After that I checked the X86 implementation, I sent this PR. >>>=20 >>>>=20=20 >>>>>>> --- /dev/null >>>>>>> +++ b/ld/testsuite/ld-riscv-elf/ifunc-macro.s >>>>>>> @@ -0,0 +1,19 @@ >>>>>>> +/* Define macros to handle similar behaviors for rv32/rv64. >>>>>>> + Assumes macro "__64_bit__" defined for rv64. >>>>>>> + The macro is specifically defined for ifunc tests in ld-riscv-e= lf.exp. */ >>>>>>> + >>>>>>> +.macro PTR_DATA name >>>>>>> +.ifdef __64_bit__ >>>>>>> + .quad \name >>>>>>> +.else >>>>>>> + .long \name >>>>>>> +.endif >>>>>>> +.endm >>>>>>> + >>>>>>> +.macro LOAD rd, rs, offset >>>>>>> +.ifdef __64_bit__ >>>>>>> + ld \rd, \offset (\rs) >>>>>>> +.else >>>>>>> + lw \rd, \offset (\rs) >>>>>>> +.endif >>>>>>> +.endm >>>>=20 >>>> Btw, can we not use these macroes? >>>=20 >>> No problem. I just want to avoid similar codes in the ifunc tests. >>>=20 >>>>=20 >>>> Nelson >>>=20 >=20 --Apple-Mail=_9DC85A97-19D8-47B3-82D0-4F376267A66E--