From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lf1-x129.google.com (mail-lf1-x129.google.com [IPv6:2a00:1450:4864:20::129]) by sourceware.org (Postfix) with ESMTPS id 71F7F389EC46 for ; Mon, 9 May 2022 14:25:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 71F7F389EC46 Received: by mail-lf1-x129.google.com with SMTP id t25so24084393lfg.7 for ; Mon, 09 May 2022 07:25:08 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=raVBnlQOFh74irdAR8dar51joylC9EEhsFi7NICWd4E=; b=lgfyxsXlnp1eZEZqQkPFvct4bXaVpDfB/+jZWCQQ9HKJGQsTZG07qUu/xLGc1vBN37 yq9wtfQmDoIe2upHUAp59ymHU3J5aFX7Hr+pL4KH/FhDi/db7b4CNgYLvij8FgHr+fqp oe/8fdBkOhmiUBajIrzKGiUVRIY1baQ9mHZCIxpVKfpxP38KTCbLAo7QsA5gZN00azA1 XDM6gs8qXcvN64iXlAjMeOF7l7vYucFxpfAQj4aKoG4SyAqga9sIHxK8BAuzTvzEsP3H zP5CEUmq/PKFNf0F1i6c4Yn29b5XNRuzfrPoN19NXsJ2Al9SDs6yxBUCFJhDYLJ+b9Ug 8bFA== X-Gm-Message-State: AOAM531OrE9wmrgUmHI9r4+IDQPs9R+S6eX3fIfdouBsBJCo1QSpuRUN hlLXZ/FRmgQTop6h+2NBoBVoambMCKPwt8lkmDtvFHlBj3brUA== X-Google-Smtp-Source: ABdhPJzc/rubK9dQXUbEPrGv3AXxmL/v1uC58WxIbfmR3UkkcPEL6qM9BMtVHyvauWj1rSzwPAEpD6bJy+g3OoFdIQ8= X-Received: by 2002:a05:6512:2244:b0:473:aa9d:8ae1 with SMTP id i4-20020a056512224400b00473aa9d8ae1mr13018657lfu.317.1652106306757; Mon, 09 May 2022 07:25:06 -0700 (PDT) MIME-Version: 1.0 References: <257aa215-a141-6d9b-672d-ea8ce209b107@arm.com> <63f63d70-8a55-618c-06e8-22f923c39e02@arm.com> In-Reply-To: <63f63d70-8a55-618c-06e8-22f923c39e02@arm.com> From: Yichao Yu Date: Mon, 9 May 2022 10:24:54 -0400 Message-ID: Subject: Re: Restoring pc to a different value than lr on aarch64 To: Luis Machado Cc: gdb@sourceware.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-0.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM, KAM_NUMSUBJECT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gdb@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 09 May 2022 14:25:10 -0000 On Mon, May 9, 2022 at 6:44 AM Luis Machado wrote: > > On 5/6/22 17:30, Yichao Yu wrote: > > On Fri, May 6, 2022 at 12:11 PM Yichao Yu wrote: > >> > >>>> Actually I misspoke for that. It seems that sp is probably fine and > >>>> the only thing missing causing pc to not work is that > >>>> aarch64_dwarf_reg_to_regnum doesn't understand the PC dwarf reg > >>>> number. It seems that the only thing needed is to add a > >>>> > >>>> + if (reg == AARCH64_DWARF_PC) > >>>> + return AARCH64_PC_REGNUM; > >>>> > >>>> to that function. > >>>> > >>> > >>> Yes, GDB always assumes the PC from the previous frame is the LR from > >>> the current frame. That is what GCC generates. > >>> > >>> If a different setup is needed, GDB needs to be taught about it. > >> > >> I agree the current code makes sense for what gcc generates. However, > >> I think given the document from arm, explicitly setting the PC value > >> in the unwind info should also work. > >> Would a patch similar to the one above be acceptable to fix this issue? > >> > >> A related issue is that gdb also seems to be ignoring the return > >> address register in CIE. There is at least one use of it in glibc[2] > >> where the return address register is set to x15 instead. > >> I've verified that gdb is currently unable to unwind after the call to > >> `strlen` from `rawmemchr` even though the return address is still in > >> x15. > >> I thought this can be fixed by chaiming that PC is RA just like the > >> fallback case but that is apparently not working... > > > > Actually this did work but the address is wrong before the value was > > written to x15... So it was just due to incorrect unwind info (the > > glibc implementation should specify how to find x15 on the entry of > > rawmemchr). > > Is the current implementation due to some edge cases? (like old > > compiler version doesn't put a valid value in the CIE for the return > > address register). It seems that many other architecture simply use > > _RA so I don't see why this would have broader problems... > > It is probably historical that this has been handled like this. If there > is a use case for having a PC column containing distinct data, then we > could support that. I couldn't find any existed code using this, but it seems that this is the intent from ARM and I really can't think of any other way to restore everything including both pc and lr so I'd like to support that at least.... > It wouldn't be as simple as that change you mentioned though, as other > parts of the code assume PC comes from the LR. There are indeed other unwinders (the prologue unwinder) that assumes this, which I think should be fine. Specifying the PC explicitly should only apply to the dwarf unwinder. The only other place where I can find that relies on this is aarch64_gen_return_address. It doesn't seem that this function is used in most of the other logics and if the return address column is somehow accessible from here it should also not be too hard to fix. Is there any other cases that I'm missing? I've also tested with my simple change and it seems that unwinding from normal functions still works as intended. > PC is probably specified > as DWARF2_FRAME_REG_SAME_VALUE (as far as I remember), so it will cause > some issues during unwinding. I don't believe this is the case. AFAICT, the only way gcc emits debug info to specify that pc is in lr without relying on architecture detail is that it specifies the return address column is 30 in the CIE. Currently, on aarch64, the return address column is basically never used but if the how for PC is changed to RA then it seems that gdb does pick it up correctly. > There is a comment in gdb/dwarf2/expr.c about some odd cases. For example: > > > /* GCC, in its infinite wisdom decided to not provide unwind > information for registers that are "same value". Since > DWARF2 (3 draft 7) doesn't define such behavior, said > registers are actually undefined (which is different to CFI > "undefined"). Code above issues a complaint about this. > Here just fudge the books, assume GCC, and that the value is > more inner on the stack. */ > > > > > > > >> > >> [2] https://github.com/bminor/glibc/blob/b92a49359f33a461db080a33940d73f47c756126/sysdeps/aarch64/rawmemchr.S#L34 > >> > >>>>> > >>>>> According to aadwarf64[1], > >>>>> > >>>>>> having both LR and PC columns is useful for describing asynchronously created stack frames. A DWARF expression may use this register to restore the context in case of a signal context. > >>>>> > >>>>> so assume the intention is that if I explicitly unwind the pc in > >>>>> addition to lr, it should work. I tried to do that, and also to set > >>>>> return address column to 32, as well as trying to mark the frame as > >>>>> signal frame but none of them seems to work. Is there any way for gdb > >>>>> to honer the explicit unwinding of pc? > >>>>> > >>>>> Also it seems that the sp is also card coded to be cfa. My code also > >>>>> contains explicit saving and restoring of that as well so if that's > >>>>> the case (haven't tested yet) it would be a problem too... > >>>>> > >>>>> Would it be possible to not use this hard-coded logic if the frame > >>>>> contains explicit override of the pc value? > >>>>> > >>>>> Yichao > >>>>> > >>>>> A bit more about the actual code. This is done as part of runtime > >>>>> patching code. The actual restoration of lr is done by returning to a > >>>>> runtime allocated stub that restores lr and directly branch back to > >>>>> the return location. After returning, all registers values are > >>>>> restored back to their previous one. The stack pointer is also > >>>>> switched out since we cannot rely on how much stack space the call > >>>>> site has available. > >>> > >>> This seems to work in a similar way as signal handler. GDB needs to be > >>> taught where to find the registers so it can properly unwind things. > >>> > >>>>> > >>>>> [1] https://github.com/ARM-software/abi-aa/blob/8a7b266879c60ca1c76e94ebb279b2dac60ed6a5/aadwarf64/aadwarf64.rst#note-9 > >>> >