From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id YLREC4LRx19ncQAAWB0awg (envelope-from ) for ; Wed, 02 Dec 2020 12:40:18 -0500 Received: by simark.ca (Postfix, from userid 112) id CB3221F0AC; Wed, 2 Dec 2020 12:40:16 -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.4 required=5.0 tests=DKIM_SIGNED,MAILING_LIST_MULTI, RDNS_NONE,T_DKIM_INVALID,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.2 Received: from sourceware.org (unknown [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 DCFD21E58E for ; Wed, 2 Dec 2020 12:40:15 -0500 (EST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0BE9F396D812; Wed, 2 Dec 2020 17:39:45 +0000 (GMT) Received: from mail-wr1-x444.google.com (mail-wr1-x444.google.com [IPv6:2a00:1450:4864:20::444]) by sourceware.org (Postfix) with ESMTPS id AD29E395CCC5 for ; Wed, 2 Dec 2020 17:39:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org AD29E395CCC5 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-wr1-x444.google.com with SMTP id e7so4948902wrv.6 for ; Wed, 02 Dec 2020 09:39:41 -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=sCdXztIMMHcToBRAS7v9pff7BVBb5BoX9sX7ZPcL14c=; b=UFvYNNqzTdxssfWCJxHtB3j0SngtpHPEiwhZrh3Zk0ye+g3oIRYKhqomkqq5hqB6Cl Afv3KzeRoREIMHlcdy0cZb2zAnK+JyViESwmpsqD7g1QplmIrwoXhhqb65Q60CdznET/ 9VCNBK7fvcUWNkWfKgLUT61yXtd5RaV73EzZLz3t9lVrojEJ442MhSWVXv1S9eJrJKCk ACV99BpPBWzgJIdFWTFd1xUr4TJFjKrkqx7Th8qQ8WO3ZZ2bvnCrRG8I+EjD7hQvjkYp WVu9URaBT57w44iy2/d1UP72TYm5T/0TEjsBvuisRhHq5rA65IWqAe2EFdGF750j/cYq abWg== 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=sCdXztIMMHcToBRAS7v9pff7BVBb5BoX9sX7ZPcL14c=; b=sxxaTQKSdJHkKYGOfUlCaJQpqjge3zD3b6Qb9Zdq9kHInC8Dy/ypMBvCl9H1Y70IcF 05XbA1TN4PDKCeGiVJTN7/Wq+6YIjwbvC5RK5wK7dAHhXcoOv8IFHqSxMkkDP3H4uksu +GaAeNAVLX7cdNkpepAWwlCyaNqYRvb7lw0ggM1dzFAuU1p4HJwT3buYPmwii0s4+uHH dzBREQ11rY380m8uwBynQzKEI41Zv79ao99vh2yNn8ZqK0qTeQEDPSphU0203PFlYF8k ZkYkFTMg+bpcTqZk4mXaWr/QsWFj+3MI3YtNOfHAzO7yCshRoBQfLLh4/IzFIZKcnNa3 JXqg== X-Gm-Message-State: AOAM530mdBl50vz1YYBwyXiGVxhR9tuWWsNNtUBPLBhNmOYelNouGhpO ExxRcUK5t2jsmrR1CVHlACMMRQ== X-Google-Smtp-Source: ABdhPJyVIkyFxakoY/slXwLWWfqyfRwOKnKTH+crOlfntSeNsTqHSqNWetl1wBwdVFQMJVn5axGSrw== X-Received: by 2002:adf:f944:: with SMTP id q4mr4777036wrr.120.1606930780789; Wed, 02 Dec 2020 09:39:40 -0800 (PST) Received: from localhost (host109-154-20-215.range109-154.btcentralplus.com. [109.154.20.215]) by smtp.gmail.com with ESMTPSA id j13sm2928355wrp.70.2020.12.02.09.39.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Dec 2020 09:39:40 -0800 (PST) From: Andrew Burgess To: binutils@sourceware.org, gdb-patches@sourceware.org Subject: [PATCH 4/8] bfd/riscv: prepare to handle bare metal core dump creation Date: Wed, 2 Dec 2020 17:39:28 +0000 Message-Id: <1738a5518a1dc57b34e4a8b13fa9b5e46f2a8d88.1606930261.git.andrew.burgess@embecosm.com> 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. 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. --- bfd/ChangeLog | 6 ++++ bfd/elfnn-riscv.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c index 20944c8109f..cffdf0b21f2 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 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. */ + +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 -- 2.25.4