From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm1-x342.google.com (mail-wm1-x342.google.com [IPv6:2a00:1450:4864:20::342]) by sourceware.org (Postfix) with ESMTPS id AFCCB394CC10 for ; Mon, 7 Dec 2020 14:39:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org AFCCB394CC10 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=embecosm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=andrew.burgess@embecosm.com Received: by mail-wm1-x342.google.com with SMTP id a3so13863131wmb.5 for ; Mon, 07 Dec 2020 06:39:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=vz3Slspn0r7eNCeES670A/Jn1wVJmip5I78JgBohkPg=; b=c1Nhre5G9tDFtI8Djw49P/EyANPb67LDuT/O9I+7kb+WDuK8bns/kbTmRgfgxdZ85p sC4HYwWNAN5JTjWeJ8q8PLZFXPvELLZflezte4fGdO7wDv4ke/4iS4ZEPiAT07GjIYlH iCzdJGzB3bMfo6LE2UOdoEOndwkStZgiysSPRRwJmvmUTRyNuSVPH7cL7E8Xm3h7U7sG j7b/bU5ZVxp0ifWIRz9sxqm8FCkky9iPv77IfJWR7HF+BIA8hbSJ5796DEj899iayJ36 b4TKK0tRvaNBF0cMELMwrQA1tpFw91hvRUV1wuiJNnD94qi+RsfGvdJCICTOEglXX4Ly a7Jg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=vz3Slspn0r7eNCeES670A/Jn1wVJmip5I78JgBohkPg=; b=uTef+veG7O+Ce0J76KSjV8rS9Dib7Qdzp5UH8Vd4RrAzbH59wvf9U+XSgm5SJCZwy/ Bpl9tyMkrlLvPznV9q8PiRThAD9sDJOUiWOdNk/EeXl6d1WKqcN3A2myBPVmjUKnpDTC uZzir63ESqkFjEH9k78KOaVkLkwQxUa9Ie89bKbHI3yVtggKtfYJybAKQqR+qeQBPB41 szvYU3ItZIeUH3SYnyFyfLizjgnRVcjKFrIi6KgGPV6ngZLLteYgA8hfc0qV4wfGIAXo BuLVlpU+qaXDxgbU6p+msCSF0Ry9O78DxyDGACZNR4MY57CR0pIUjfu9+i3GPaIiT/Wv e7GA== X-Gm-Message-State: AOAM531QGJM63pyc8WlQ3EsVk3s2HXMdoGllJ8E5JSLStnt2/reaxvc/ 6c7+B6s4sRiySfMahnLKp44E8A== X-Google-Smtp-Source: ABdhPJw0cSv28dWaHapqyDkrXOTvw8fyBg/6jRlYMnU2Vw2htY1LbuxsbAaviqd2RjURhyFV1Zs3nw== X-Received: by 2002:a7b:c773:: with SMTP id x19mr18318638wmk.127.1607351986747; Mon, 07 Dec 2020 06:39:46 -0800 (PST) Received: from localhost (host109-154-20-215.range109-154.btcentralplus.com. [109.154.20.215]) by smtp.gmail.com with ESMTPSA id w13sm4981152wrt.52.2020.12.07.06.39.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Dec 2020 06:39:46 -0800 (PST) Date: Mon, 7 Dec 2020 14:39:45 +0000 From: Andrew Burgess To: Jim Wilson Cc: Binutils , gdb-patches@sourceware.org, Nelson Chu , John Baldwin Subject: Re: [PATCH 4/8] bfd/riscv: prepare to handle bare metal core dump creation Message-ID: <20201207143945.GJ2729@embecosm.com> References: <1738a5518a1dc57b34e4a8b13fa9b5e46f2a8d88.1606930261.git.andrew.burgess@embecosm.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Operating-System: Linux/5.8.13-100.fc31.x86_64 (x86_64) X-Uptime: 14:38:58 up 43 days, 5:42, X-Editor: GNU Emacs [ http://www.gnu.org/software/emacs ] X-Spam-Status: No, score=-13.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: Mon, 07 Dec 2020 14:39:49 -0000 * Jim Wilson [2020-12-02 15:24:41 -0800]: > On Wed, Dec 2, 2020 at 9:39 AM Andrew Burgess > wrote: > > > +/* Write PRSTATUS note into core file. This will be called before the > > + generic code in elf.c. By checking the compiler defines we only > > perform > > + any action here if the generic code would otherwise not be able to help > > + us. The intention is that bare metal core dumps (where the > > prstatus32_t > > + might not be available) will use this code, while non bare metal tools > > + will use the generic elf code. */ > > > > The reference to prstatus32_t is a little confusing, as that appears to be > an x64_64 specific type. I think that should be prstatus_t. > > The function handles both PRSTATUS and PRPSINFO but the comment only > mentions PRSTATUS. Maybe it should mention both? > > Otherwise this looks OK to me, with a minor comment clarification. I updated the comment and posted the revised patch below. Thanks, Andrew --- commit 00f409a72ba183ef0f80b5874f7d0c6464d85073 Author: Andrew Burgess Date: Mon Nov 30 12:14:38 2020 +0000 bfd/riscv: prepare to handle bare metal core dump creation When creating a core file GDB will call the function elfcore_write_prstatus to write out the general purpose registers along with the pid/tid for the thread. However, for a bare metal RISC-V tool chain the prstatus*_t types are not defined so the elfcore_write_prstatus function will return NULL, preventing core file creation. This commit provides the `elf_backend_write_core_note' hook and uses the provided function to write out the ptstatus information. In order to keep changes in the non bare metal tools to a minimum, the provided backend function will itself return NULL when the prstatus*_t types are available, the consequence of this is that the generic code in elfcore_write_prstatus will be used just as before. But, when prstatus*_t is not available, the new backend function will write out the prstatus information using predefined offsets. This new functionality will be used by a later GDB commit that will add bare metal core dumps for RISC-V. bfd/ChangeLog: * elfnn-riscv.c (riscv_write_core_note): New function. (elf_backend_write_core_note): Define. diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c index 20944c8109f..29cccf06e67 100644 --- a/bfd/elfnn-riscv.c +++ b/bfd/elfnn-riscv.c @@ -4874,6 +4874,77 @@ _bfd_riscv_relax_section (bfd *abfd, asection *sec, # define PRPSINFO_OFFSET_PR_PSARGS 56 #endif +/* Write PRSTATUS and PRPSINFO note into core file. This will be called + before the generic code in elf.c. By checking the compiler defines we + only perform any action here if the generic code would otherwise not be + able to help us. The intention is that bare metal core dumps (where the + prstatus_t and/or prpsinfo_t might not be available) will use this code, + while non bare metal tools will use the generic elf code. */ + +static char * +riscv_write_core_note (bfd *abfd ATTRIBUTE_UNUSED, + char *buf ATTRIBUTE_UNUSED, + int *bufsiz ATTRIBUTE_UNUSED, + int note_type ATTRIBUTE_UNUSED, ...) +{ + switch (note_type) + { + default: + return NULL; + +#if !defined (HAVE_PRPSINFO_T) + case NT_PRPSINFO: + { + char data[PRPSINFO_SIZE] ATTRIBUTE_NONSTRING; + va_list ap; + + va_start (ap, note_type); + memset (data, 0, sizeof (data)); + strncpy (data + PRPSINFO_OFFSET_PR_FNAME, va_arg (ap, const char *), 16); +#if GCC_VERSION == 8000 || GCC_VERSION == 8001 + DIAGNOSTIC_PUSH; + /* GCC 8.0 and 8.1 warn about 80 equals destination size with + -Wstringop-truncation: + https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643 + */ + DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION; +#endif + strncpy (data + PRPSINFO_OFFSET_PR_PSARGS, va_arg (ap, const char *), 80); +#if GCC_VERSION == 8000 || GCC_VERSION == 8001 + DIAGNOSTIC_POP; +#endif + va_end (ap); + return elfcore_write_note (abfd, buf, bufsiz, + "CORE", note_type, data, sizeof (data)); + } +#endif /* !HAVE_PRPSINFO_T */ + +#if !defined (HAVE_PRSTATUS_T) + case NT_PRSTATUS: + { + char data[PRSTATUS_SIZE]; + va_list ap; + long pid; + int cursig; + const void *greg; + + va_start (ap, note_type); + memset (data, 0, sizeof(data)); + pid = va_arg (ap, long); + bfd_put_32 (abfd, pid, data + PRSTATUS_OFFSET_PR_PID); + cursig = va_arg (ap, int); + bfd_put_16 (abfd, cursig, data + PRSTATUS_OFFSET_PR_CURSIG); + greg = va_arg (ap, const void *); + memcpy (data + PRSTATUS_OFFSET_PR_REG, greg, + PRSTATUS_SIZE - PRSTATUS_OFFSET_PR_REG - ARCH_SIZE / 8); + va_end (ap); + return elfcore_write_note (abfd, buf, bufsiz, + "CORE", note_type, data, sizeof (data)); + } +#endif /* !HAVE_PRSTATUS_T */ + } +} + /* Support for core dump NOTE sections. */ static bfd_boolean @@ -4984,6 +5055,8 @@ riscv_elf_obj_attrs_arg_type (int tag) #define elf_backend_grok_prstatus riscv_elf_grok_prstatus #define elf_backend_grok_psinfo riscv_elf_grok_psinfo #define elf_backend_object_p riscv_elf_object_p +#undef elf_backend_write_core_note +#define elf_backend_write_core_note riscv_write_core_note #define elf_info_to_howto_rel NULL #define elf_info_to_howto riscv_info_to_howto_rela #define bfd_elfNN_bfd_relax_section _bfd_riscv_relax_section