From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id GFuBFX+vKmCIMwAAWB0awg (envelope-from ) for ; Mon, 15 Feb 2021 12:29:35 -0500 Received: by simark.ca (Postfix, from userid 112) id 514991EF78; Mon, 15 Feb 2021 12:29:35 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED, MAILING_LIST_MULTI,T_DKIM_INVALID,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id E94E61E54D for ; Mon, 15 Feb 2021 12:29:32 -0500 (EST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7BABC396E841; Mon, 15 Feb 2021 17:29:27 +0000 (GMT) Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [IPv6:2a00:1450:4864:20::334]) by sourceware.org (Postfix) with ESMTPS id 532AF395B058 for ; Mon, 15 Feb 2021 17:29:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 532AF395B058 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-x334.google.com with SMTP id a132so426745wmc.0 for ; Mon, 15 Feb 2021 09:29:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=KAk42PAFyOAxv8Gj/AIelMWd40r7yTRbrmUlH4Wicxg=; b=BMk1mvkGgpS6GNP2HMsRbPO7k/YIppTnoThc11JznLdPGZol24LkIJPKuBQ0rArFEj 8CIpND/m5MCJfBWE9F/8p1fTBXbc6gJ9JR5iNPDCKBtQjdA5W6uTWtQodQ+jvoBLUSO/ Gykjn3IUQiLqWZqMLjNu4MG2vL9uWXHzOxTpTInlDPo5WYyGmXSxK2YaXWW4xlnkZGIH viMT+CxHPk2UItmI/yCuAVq0iMYGkE4XDm8ov2aIn8JSz8sDPFODPF5hV5E8tFGXLHnN m0r8b+2/51l/XKt5se6WFBLTCYfRRsLJyllPbIFR0JlyMxSM7X2rce4Asp75+PQ7zfb8 RXog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=KAk42PAFyOAxv8Gj/AIelMWd40r7yTRbrmUlH4Wicxg=; b=TO64uJ9YnPrLgOB1qbJdXby63Vk88mJbiBQd2o5ePb1j0kuuevOubKdKGoYVBwaP70 blKnmgMwPuAVmDHjv0ygNMRPaccXi6RryZ+BoMcLjguEB+q4gwTuPCODly6fe6M89EFn GFuB92OenbwFxQWmaiqDM121EbSJinG53amlcCAv5TVuTtVj+Lz4slJFE6TBrcYktMLf 44CCGUqmuUd7DfZI/CmjfRVoJGxw3PVf2eBOCihbAyYrSR2vg3E1e1rcdUMuyL5CItBS f6IsrZXdl/6ON3AMurHwNp0vemHAyDn0tU2X+ogPyrpZVF7oQpukkpCex1jQyms4MDqC hzrw== X-Gm-Message-State: AOAM533vc0xgxIFxzzM8i4nqLB6IkZPLBQA4BpNKvLV6RPPCwv2jYoXx 6AvOTPcrnenVW6rFszFPemFntyQfG8l3rA== X-Google-Smtp-Source: ABdhPJy9S1ZBVTcytwB9W+myOtl8AFg2MqBQp1gv1qqcg8TyG1Wak/Zt6+90dk0JTre4Y8/EVZJ+Yw== X-Received: by 2002:a1c:d7:: with SMTP id 206mr15163767wma.68.1613410163168; Mon, 15 Feb 2021 09:29:23 -0800 (PST) Received: from localhost (host86-186-80-154.range86-186.btcentralplus.com. [86.186.80.154]) by smtp.gmail.com with ESMTPSA id w25sm25514640wmc.42.2021.02.15.09.29.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Feb 2021 09:29:22 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org, binutils@sourceware.org Subject: [PATCHv3 4/9] bfd/riscv: prepare to handle bare metal core dump creation Date: Mon, 15 Feb 2021 17:29:07 +0000 Message-Id: X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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: , Errors-To: gdb-patches-bounces@sourceware.org Sender: "Gdb-patches" 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 (into a prstatus structure) and the executable name and arguments (into a prpsinfo_t structure). However, for a bare metal RISC-V tool chain the prstatus_t and prpsinfo_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 required 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 or pspsinfo_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 or prpsinfo_t is not available, the new backend function will write out the 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 (PRPSINFO_PR_FNAME_LENGTH): Define. (PRPSINFO_PR_PSARGS_LENGTH): Define. (riscv_write_core_note): New function. (riscv_elf_grok_psinfo): Make use of two new length defines. (elf_backend_write_core_note): Define. --- bfd/ChangeLog | 9 +++++ bfd/elfnn-riscv.c | 84 +++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 91 insertions(+), 2 deletions(-) diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c index b2ec6a29fbf..4444dcdf263 100644 --- a/bfd/elfnn-riscv.c +++ b/bfd/elfnn-riscv.c @@ -4874,6 +4874,8 @@ _bfd_riscv_relax_section (bfd *abfd, asection *sec, # define PRPSINFO_OFFSET_PR_PID 16 # define PRPSINFO_OFFSET_PR_FNAME 32 # define PRPSINFO_OFFSET_PR_PSARGS 48 +# define PRPSINFO_PR_FNAME_LENGTH 16 +# define PRPSINFO_PR_PSARGS_LENGTH 80 #else # define PRSTATUS_SIZE 376 # define PRSTATUS_OFFSET_PR_CURSIG 12 @@ -4884,8 +4886,83 @@ _bfd_riscv_relax_section (bfd *abfd, asection *sec, # define PRPSINFO_OFFSET_PR_PID 24 # define PRPSINFO_OFFSET_PR_FNAME 40 # define PRPSINFO_OFFSET_PR_PSARGS 56 +# define PRPSINFO_PR_FNAME_LENGTH 16 +# define PRPSINFO_PR_PSARGS_LENGTH 80 #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 *), + PRPSINFO_PR_FNAME_LENGTH); +#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 *), + PRPSINFO_PR_PSARGS_LENGTH); +#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 @@ -4927,11 +5004,13 @@ riscv_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note) /* pr_fname */ elf_tdata (abfd)->core->program = _bfd_elfcore_strndup - (abfd, note->descdata + PRPSINFO_OFFSET_PR_FNAME, 16); + (abfd, note->descdata + PRPSINFO_OFFSET_PR_FNAME, + PRPSINFO_PR_FNAME_LENGTH); /* pr_psargs */ elf_tdata (abfd)->core->command = _bfd_elfcore_strndup - (abfd, note->descdata + PRPSINFO_OFFSET_PR_PSARGS, 80); + (abfd, note->descdata + PRPSINFO_OFFSET_PR_PSARGS, + PRPSINFO_PR_PSARGS_LENGTH); break; } @@ -5000,6 +5079,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 -- 2.25.4