From a833790a626d9620319d0ca6aee23daa584d445c Mon Sep 17 00:00:00 2001 From: Tom de Vries Date: Wed, 1 Nov 2023 00:33:12 +0100 Subject: [gdb/symtab] Work around gas PR28629 When running test-case gdb.tui/tui-layout-asm-short-prog.exp on AlmaLinux 9.2 ppc64le, I run into: ... FAIL: gdb.tui/tui-layout-asm-short-prog.exp: check asm box contents ... The problem is that we get: ... 7 [ No Assembly Available ] ... because tui_get_begin_asm_address doesn't succeed. In more detail, tui_get_begin_asm_address calls: ... find_line_pc (sal.symtab, sal.line, &addr); ... with: ... (gdb) p *sal.symtab $5 = {next = 0x130393c0, m_compunit = 0x130392f0, m_linetable = 0x0, filename = "tui-layout-asm-short-prog.S", filename_for_id = "$gdb/build/gdb/testsuite/tui-layout-asm-short-prog.S", m_language = language_asm, fullname = 0x0} (gdb) p sal.line $6 = 1 ... The problem is the filename_for_id which is the source file prefixed with the compilation dir rather than the source dir. This is due to faulty debug info generated by gas, PR28629: ... <1a> DW_AT_name : tui-layout-asm-short-prog.S <1e> DW_AT_comp_dir : $gdb/build/gdb/testsuite <22> DW_AT_producer : GNU AS 2.35.2 ... The DW_AT_name is relative, and it's relative to the DW_AT_comp_dir entry, making the effective name $gdb/build/gdb/testsuite/tui-layout-asm-short-prog.S. The bug is fixed starting version 2.38, where we get instead: ... <1a> DW_AT_name : $gdb/src/gdb/testsuite/gdb.tui/tui-layout-asm-short-prog.S <1e> DW_AT_comp_dir : $gdb/build/gdb/testsuite <22> DW_AT_producer : GNU AS 2.38 ... Work around the faulty debug info by constructing the filename_for_id using the second directory from the directory table in the .debug_line header: ... The Directory Table (offset 0x22, lines 2, columns 1): Entry Name 0 $gdb/build/gdb/testsuite 1 $gdb/src/gdb/testsuite/gdb.tui ... Note that the used gas contains a backport of commit 3417bfca676 ("GAS: DWARF-5: Ensure that the 0'th entry in the directory table contains the current working directory."), because directory 0 is correct. With the unpatched 2.35.2 release the directory 0 entry is incorrect: it's a copy of entry 1. Add a dwarf assembly test-case that reflects the debug info as generated by unpatched gas 2.35.2. Tested on x86_64-linux. Approved-By: Tom Tromey --- gdb/dwarf2/cu.c | 1 + gdb/dwarf2/cu.h | 1 + gdb/dwarf2/read.c | 37 ++++++++++++++++++++++++++++++++++++- 3 files changed, 38 insertions(+), 1 deletion(-) (limited to 'gdb/dwarf2') diff --git a/gdb/dwarf2/cu.c b/gdb/dwarf2/cu.c index a908ec9..f9b05df 100644 --- a/gdb/dwarf2/cu.c +++ b/gdb/dwarf2/cu.c @@ -40,6 +40,7 @@ dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu, producer_is_icc_lt_14 (false), producer_is_codewarrior (false), producer_is_clang (false), + producer_is_gas_lt_2_38 (false), producer_is_gas_2_39 (false), processing_has_namespace_info (false), load_all_dies (false) diff --git a/gdb/dwarf2/cu.h b/gdb/dwarf2/cu.h index 6c61171..ef8db48 100644 --- a/gdb/dwarf2/cu.h +++ b/gdb/dwarf2/cu.h @@ -265,6 +265,7 @@ public: bool producer_is_icc_lt_14 : 1; bool producer_is_codewarrior : 1; bool producer_is_clang : 1; + bool producer_is_gas_lt_2_38 : 1; bool producer_is_gas_2_39 : 1; /* When true, the file that we're processing is known to have diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c index bd43b5b..c494816 100644 --- a/gdb/dwarf2/read.c +++ b/gdb/dwarf2/read.c @@ -148,6 +148,8 @@ static int dwarf2_locexpr_block_index; static int dwarf2_loclist_block_index; static int ada_block_index; +static bool producer_is_gas_lt_2_38 (struct dwarf2_cu *cu); + /* Size of .debug_loclists section header for 32-bit DWARF format. */ #define LOCLIST_HEADER_SIZE32 12 @@ -7489,6 +7491,27 @@ read_file_scope (struct die_info *die, struct dwarf2_cu *cu) file_and_directory &fnd = find_file_and_directory (die, cu); + /* GAS supports generating dwarf-5 info starting version 2.35. Versions + 2.35-2.37 generate an incorrect CU name attribute: it's relative, + implicitly prefixing it with the compilation dir. Work around this by + prefixing it with the source dir instead. */ + if (cu->header.version == 5 && !IS_ABSOLUTE_PATH (fnd.get_name ()) + && producer_is_gas_lt_2_38 (cu)) + { + attr = dwarf2_attr (die, DW_AT_stmt_list, cu); + if (attr != nullptr && attr->form_is_unsigned ()) + { + sect_offset line_offset = (sect_offset) attr->as_unsigned (); + line_header_up lh = dwarf_decode_line_header (line_offset, cu, + fnd.get_comp_dir ()); + if (lh->version == 5 && lh->is_valid_file_index (1)) + { + std::string dir = lh->include_dir_at (1); + fnd.set_comp_dir (std::move (dir)); + } + } + } + cu->start_compunit_symtab (fnd.get_name (), fnd.intern_comp_dir (objfile), lowpc); @@ -11251,7 +11274,10 @@ check_producer (struct dwarf2_cu *cu) else if (producer_is_clang (cu->producer, &major, &minor)) cu->producer_is_clang = true; else if (producer_is_gas (cu->producer, &major, &minor)) - cu->producer_is_gas_2_39 = major == 2 && minor == 39; + { + cu->producer_is_gas_lt_2_38 = major < 2 || (major == 2 && minor < 38); + cu->producer_is_gas_2_39 = major == 2 && minor == 39; + } else { /* For other non-GCC compilers, expect their behavior is DWARF version @@ -11288,6 +11314,15 @@ producer_is_codewarrior (struct dwarf2_cu *cu) } static bool +producer_is_gas_lt_2_38 (struct dwarf2_cu *cu) +{ + if (!cu->checked_producer) + check_producer (cu); + + return cu->producer_is_gas_lt_2_38; +} + +static bool producer_is_gas_2_39 (struct dwarf2_cu *cu) { if (!cu->checked_producer) -- cgit v1.1