From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-qk1-x72a.google.com (mail-qk1-x72a.google.com [IPv6:2607:f8b0:4864:20::72a]) by sourceware.org (Postfix) with ESMTPS id 857B1382D82E for ; Mon, 1 Feb 2021 13:48:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 857B1382D82E Received: by mail-qk1-x72a.google.com with SMTP id k193so16177600qke.6 for ; Mon, 01 Feb 2021 05:48:19 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=UJIr6d9+56bSZac+XsdN/0UcC1uq8mIFHDRS+tWCbLs=; b=U2nijiQ6+0cpQY6G5N+NaF7fkKOJMfm8rdKtx1MUzNwTDhmZRFj5ixp9arUBAIXMez UFERkFuGA5yTxHWQal5KhkJyqColb0MrWVumKuLT5yFm/o6tUyofjcmBc73f5QkAy1lh /WZOaoLAkACVVQ8hn+VB7MQah5OC4nnGxlRWRsg/xr/b2gwxnoqhF5VbpK1gS0MBn28S QXS/kJ5F5zWMbciXtZZ25593DK2Nx1nBeu5kKwLj1P0arm71D/64deUFUIo8xf34Pu9T Pg0FoMwBKNsmD3BVWyGFayKJUUjKOLYPW6bkYN3awIKe7tGoCzIiz5GywjH3aoXmGVQH HQ8w== X-Gm-Message-State: AOAM530k7TvOcbPue5BiRAdup9Uh6pInZ9xyj6k1zPYhP1ATag3HlTnn 02YzqicG98Aoc3mFov/l8Sm8mNEGmDGEuw== X-Google-Smtp-Source: ABdhPJz5/Ah1zL7EkjNVH9L4XCspxhuPhTe1ZDvPUI6r0oq0BMrdQIYNTYcE62AyOYl0552AT/l9Qw== X-Received: by 2002:a37:68f:: with SMTP id 137mr15116696qkg.420.1612187299104; Mon, 01 Feb 2021 05:48:19 -0800 (PST) Received: from ?IPv6:2804:7f0:8284:848d:c0e:b160:155c:b079? ([2804:7f0:8284:848d:c0e:b160:155c:b079]) by smtp.gmail.com with ESMTPSA id h185sm14097161qkd.122.2021.02.01.05.48.17 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 01 Feb 2021 05:48:18 -0800 (PST) Subject: Re: [PATCHv2 4/9] bfd/riscv: prepare to handle bare metal core dump creation To: Andrew Burgess , gdb-patches@sourceware.org, binutils@sourceware.org Cc: Fredrik Hederstierna References: <9379a3e1af978103cfa47e869594f0df91d807ea.1611172468.git.andrew.burgess@embecosm.com> From: Luis Machado Message-ID: <39d197ae-ecdb-976c-c7ae-40628fa61e12@linaro.org> Date: Mon, 1 Feb 2021 10:48:15 -0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: <9379a3e1af978103cfa47e869594f0df91d807ea.1611172468.git.andrew.burgess@embecosm.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, NICE_REPLY_A, 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: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 01 Feb 2021 13:48:21 -0000 On 1/20/21 5:23 PM, Andrew Burgess wrote: > 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. ptstatus -> prstatus? > > 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. > --- > bfd/ChangeLog | 6 ++++ > bfd/elfnn-riscv.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++ > 2 files changed, 78 insertions(+) > > diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c > index b2ec6a29fbf..70a683b45a3 100644 > --- a/bfd/elfnn-riscv.c > +++ b/bfd/elfnn-riscv.c > @@ -4886,6 +4886,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 > @@ -5000,6 +5071,7 @@ 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 > +#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 >