From patchwork Mon May 20 07:32:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom de Vries X-Patchwork-Id: 90439 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 37EB73858CDB for ; Mon, 20 May 2024 07:31:41 +0000 (GMT) X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2a07:de40:b251:101:10:150:64:2]) by sourceware.org (Postfix) with ESMTPS id 6D8E63858D1E for ; Mon, 20 May 2024 07:31:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6D8E63858D1E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 6D8E63858D1E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a07:de40:b251:101:10:150:64:2 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716190273; cv=none; b=lLOmVq9SU0e+jrIpx8TdNJBTfISdxjzUblrw3pueEyOK1oayOMkLPUIKR7bQ4MAvV9rT7o9UZ/xU5AsxitmV0XTFnBZqgaxqFyxbLJ9NIXmWon91siE0xHmPqEQnR/YAVjbtsAykq2hqcEVRNOo/FLgC/VoZBtIJ/9neyHB4KW8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716190273; c=relaxed/simple; bh=rFCJle5MHh9K8aki2fAS0d9dgZvFe8qtqIZajuLpO40=; h=DKIM-Signature:DKIM-Signature:DKIM-Signature:DKIM-Signature:From: To:Subject:Date:Message-Id:MIME-Version; b=totgr0VAvlLiRpbKInjkj0NuyIRHVelQrL+/f5qA/KvGbg5w6QZEPvrHOb6SthehKdrsCspE2dVBcF0H5wS6cn6HgRBARtLZmptoMGmgvFVkAIyAHA0nWZHbbYpsyG1a3vnanPq2SLKipkP5Em1edvsSzzt883+UT/gTKmh/7SA= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id B79B7202AB for ; Mon, 20 May 2024 07:31:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1716190269; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=PdxtzL1XRsnE9YgQjEvFMrvsxleudbwyw11NOSjf2a0=; b=iNG+VIC5jCNRkCFeypIlnUtF9ucK9hYdcGTcbA3pWUtF6dIAzUmn5tPNJju2QKIy9fMdqO ZO38Drc5GfpEwfG32u9/uDNweuzs+3E5CjXbMZtLeCCIa8hFOtuPnTMrr2VY0/C16usGQ5 1ZRVWm7jumb/HgxwdrJ0ciwT9/vSuUA= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1716190269; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=PdxtzL1XRsnE9YgQjEvFMrvsxleudbwyw11NOSjf2a0=; b=NdpXQqxgFI7N2ASThLyMT3tZt/fsDNkHKlo4QmcRwIXUyn8q9Aa5zV/Hs+dcmMCkOR5Xxq +Va5qFaLe7EFwwBg== Authentication-Results: smtp-out2.suse.de; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=iNG+VIC5; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=NdpXQqxg DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1716190269; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=PdxtzL1XRsnE9YgQjEvFMrvsxleudbwyw11NOSjf2a0=; b=iNG+VIC5jCNRkCFeypIlnUtF9ucK9hYdcGTcbA3pWUtF6dIAzUmn5tPNJju2QKIy9fMdqO ZO38Drc5GfpEwfG32u9/uDNweuzs+3E5CjXbMZtLeCCIa8hFOtuPnTMrr2VY0/C16usGQ5 1ZRVWm7jumb/HgxwdrJ0ciwT9/vSuUA= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1716190269; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=PdxtzL1XRsnE9YgQjEvFMrvsxleudbwyw11NOSjf2a0=; b=NdpXQqxgFI7N2ASThLyMT3tZt/fsDNkHKlo4QmcRwIXUyn8q9Aa5zV/Hs+dcmMCkOR5Xxq +Va5qFaLe7EFwwBg== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 9948813A6B for ; Mon, 20 May 2024 07:31:09 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id Dzy+Ij38SmaVUQAAD6G6ig (envelope-from ) for ; Mon, 20 May 2024 07:31:09 +0000 From: Tom de Vries To: binutils@sourceware.org Subject: [PATCH] [binutils/readelf] Fix printing of dwarf5 .debug_info.dwo Date: Mon, 20 May 2024 09:32:18 +0200 Message-Id: <20240520073218.22297-1-tdevries@suse.de> X-Mailer: git-send-email 2.35.3 MIME-Version: 1.0 X-Spam-Level: X-Spamd-Result: default: False [-5.01 / 50.00]; BAYES_HAM(-3.00)[100.00%]; DWL_DNSWL_MED(-2.00)[suse.de:dkim]; NEURAL_HAM_LONG(-1.00)[-1.000]; MID_CONTAINS_FROM(1.00)[]; R_MISSING_CHARSET(0.50)[]; R_DKIM_ALLOW(-0.20)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; MX_GOOD(-0.01)[]; MIME_TRACE(0.00)[0:+]; SPAMHAUS_XBL(0.00)[2a07:de40:b281:104:10:150:64:97:from]; FUZZY_BLOCKED(0.00)[rspamd.com]; RBL_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[2a07:de40:b281:104:10:150:64:97:from]; ARC_NA(0.00)[]; RCPT_COUNT_ONE(0.00)[1]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; TO_MATCH_ENVRCPT_ALL(0.00)[]; RCVD_TLS_ALL(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; RCVD_COUNT_TWO(0.00)[2]; FROM_EQ_ENVFROM(0.00)[]; FROM_HAS_DN(0.00)[]; DKIM_TRACE(0.00)[suse.de:+]; TO_DN_NONE(0.00)[]; PREVIOUSLY_DELIVERED(0.00)[binutils@sourceware.org]; DNSWL_BLOCKED(0.00)[2a07:de40:b281:104:10:150:64:97:from]; RECEIVED_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[2a07:de40:b281:106:10:150:64:167:received]; DBL_BLOCKED_OPENRESOLVER(0.00)[imap1.dmz-prg2.suse.org:helo, imap1.dmz-prg2.suse.org:rdns, suse.de:dkim] X-Rspamd-Action: no action X-Rspamd-Queue-Id: B79B7202AB X-Rspamd-Server: rspamd1.dmz-prg2.suse.org X-Spam-Score: -5.01 X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: binutils@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: binutils-bounces+patchwork=sourceware.org@sourceware.org When compiling a hello world like this: ... $ gcc ~/data/hello.c -gsplit-dwarf -gdwarf-5 ... we get with readelf -w a-hello.dwo for the split full compilation unit entry incorrect strings, for instance an incorrect producer string: ... <15> DW_AT_producer : (indexed string: 0xb): /home/vries/data/hello.c ... The problem is that a str_offsets_base of 0 is used, so the header of the .debug_str_offsets section of size 8 is treated as two offsets, and the index 0xb is mapped to entry 9 instead of 11: ... 9 0000006c /home/vries/data/hello.c ... 11 00000097 GNU C17 15.0.0 -gsplit-dwarf -gdwarf-5 ... The DW_AT_str_offsets_base attribute is inherited from the corresponding skeleton compilation unit in a.out, but: - that's outside the scope of the .dwo file, and - it's also not there (though it is there when using clang instead). Fix this by assuming a sane default for str_offsets_base for dwarf5: the size of the header of the .debug_str_offsets section, which is 8 for 32-bit dwarf and 16 for 64-bit dwarf. Implement this by moving the reading of DW_AT_str_offsets_base to read_bases, which also fixes the printing of DW_FORM_strx strings in the skeleton compilation unit entries for attributes before the DW_AT_str_offsets_base attribute. Conforming with the other code in read_bases, we only handle DW_FORM_sec_offset, which means the code dealing with negative str_offsets_base can be dropped. Tested on x86_64-linux. --- binutils/dwarf.c | 38 ++++++++++++------- .../binutils-all/readelf-str-offsets-base.d | 17 +++++++++ .../binutils-all/readelf-str-offsets-base.s | 30 +++++++++++++++ binutils/testsuite/binutils-all/readelf.exp | 1 + 4 files changed, 72 insertions(+), 14 deletions(-) create mode 100644 binutils/testsuite/binutils-all/readelf-str-offsets-base.d create mode 100644 binutils/testsuite/binutils-all/readelf-str-offsets-base.s base-commit: 84f7f8940760d0eaeb51a38b345db369b4207e5b diff --git a/binutils/dwarf.c b/binutils/dwarf.c index d375148dc26..3fde898602e 100644 --- a/binutils/dwarf.c +++ b/binutils/dwarf.c @@ -2874,20 +2874,7 @@ read_and_display_attr_value (unsigned long attribute, break; case DW_AT_str_offsets_base: - if (debug_info_p->str_offsets_base) - warn (_("CU @ %#" PRIx64 " has multiple str_offsets_base values " - "%#" PRIx64 " and %#" PRIx64 ")\n"), - debug_info_p->cu_offset, - debug_info_p->str_offsets_base, uvalue); - svalue = uvalue; - if (svalue < 0) - { - warn (_("CU @ %#" PRIx64 " has has a negative stroffsets_base " - "value of %#" PRIx64 " - treating as zero\n"), - debug_info_p->cu_offset, svalue); - uvalue = 0; - } - debug_info_p->str_offsets_base = uvalue; + /* Assignment to debug_info_p->str_offsets_base is now elsewhere. */ break; case DW_AT_frame_base: @@ -3685,6 +3672,8 @@ read_bases (abbrev_entry * entry, { abbrev_attr *attr; + uint64_t initial_str_offsets_base = debug_info_p->str_offsets_base; + for (attr = entry->first_attr; attr && attr->attribute; attr = attr->next) @@ -3711,10 +3700,31 @@ read_bases (abbrev_entry * entry, else warn (_("Unexpected form of DW_AT_addr_base in the top DIE\n")); } + if (attr->attribute == DW_AT_str_offsets_base) + { + if (attr->form == DW_FORM_sec_offset) + { + SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end); + if (initial_str_offsets_base == 0 && debug_info_p->str_offsets_base != 0) + warn (_("CU @ %#" PRIx64 " has multiple str_offsets_base values " + "%#" PRIx64 " and %#" PRIx64 ")\n"), + debug_info_p->cu_offset, + debug_info_p->str_offsets_base, uvalue); + debug_info_p->str_offsets_base = uvalue; + } + else + warn (_("Unexpected form of DW_AT_str_offsets_base in the top DIE\n")); + } else data = skip_attribute (attr->form, data, end, pointer_size, offset_size, dwarf_version); } + + /* Provide a sane default for str_offsets_base: the size of the header of + the .debug_str_offsets section. */ + if (dwarf_version >= 5 && initial_str_offsets_base == 0 + && debug_info_p->str_offsets_base == 0) + debug_info_p->str_offsets_base = offset_size == 4 ? 8 : 16; } /* Process the contents of a .debug_info section. diff --git a/binutils/testsuite/binutils-all/readelf-str-offsets-base.d b/binutils/testsuite/binutils-all/readelf-str-offsets-base.d new file mode 100644 index 00000000000..063c7ef8b44 --- /dev/null +++ b/binutils/testsuite/binutils-all/readelf-str-offsets-base.d @@ -0,0 +1,17 @@ +#name: readelf -wi readelf-str-offsets-base +#source: readelf-str-offsets-base.s +#readelf: -wi + +Contents of the .debug_info.dwo section: + + Compilation Unit @ offset 0: + Length: 0x13 \(32-bit\) + Version: 5 + Unit Type: DW_UT_split_compile \(5\) + Abbrev Offset: 0 + Pointer Size: 8 + DWO ID: 0xba7e77f573c097b6 + <0><14>: Abbrev Number: 1 \(DW_TAG_compile_unit\) + <15> DW_AT_producer : \(indexed string: 0\): FIRST + <1><16>: Abbrev Number: 0 + diff --git a/binutils/testsuite/binutils-all/readelf-str-offsets-base.s b/binutils/testsuite/binutils-all/readelf-str-offsets-base.s new file mode 100644 index 00000000000..6e21e6dc0b1 --- /dev/null +++ b/binutils/testsuite/binutils-all/readelf-str-offsets-base.s @@ -0,0 +1,30 @@ + .section .debug_str.dwo,"MS",%progbits,1 + .asciz "FIRST" + .asciz "SECOND" + .section .debug_str_offsets.dwo,"MS",%progbits,1 + .long 16 + .short 5 + .short 0 + .4byte 0 + .4byte 6 + .section .debug_info.dwo,"e",%progbits + .long .Ldebug_info_dwo_end0-.Ldebug_info_dwo_start0 /* Length of Unit */ +.Ldebug_info_dwo_start0: + .2byte 0x5 /* DWARF macro version number. */ + .byte 5 /* DWARF Unit Type */ + .byte 8 /* Address Size (in bytes) */ + .long 0 /* Offset Into Abbrev. Section */ + .quad -5008433839496718410 /* DWO ID */ + .byte 1 /* Abbrev [1] 0x14:0x1a DW_TAG_compile_unit */ + .byte 0 /* DW_AT_producer */ + .byte 0 /* End Of Children Mark */ +.Ldebug_info_dwo_end0: + .section .debug_abbrev.dwo,"e",%progbits + .byte 1 /* Abbreviation Code */ + .byte 17 /* DW_TAG_compile_unit */ + .byte 1 /* DW_CHILDREN_yes */ + .byte 37 /* DW_AT_producer */ + .byte 37 /* DW_FORM_strx1 */ + .byte 0 /* EOM(1) */ + .byte 0 /* EOM(2) */ + .byte 0 /* EOM(3) */ diff --git a/binutils/testsuite/binutils-all/readelf.exp b/binutils/testsuite/binutils-all/readelf.exp index 9f20cb42812..b381562dbc6 100644 --- a/binutils/testsuite/binutils-all/readelf.exp +++ b/binutils/testsuite/binutils-all/readelf.exp @@ -403,6 +403,7 @@ if {[which $AS] != 0} then { run_dump_test "readelf-maskos-1a" run_dump_test "readelf-maskos-1b" run_dump_test "readelf-debug-str-offsets-dw4" + run_dump_test "readelf-str-offsets-base" if {![istarget *-*-hpux*]} then { run_dump_test pr26548 if {![binutils_assemble_flags $srcdir/$subdir/pr26548.s tmpdir/pr26548e.o {--defsym ERROR=1}]} then {