[PATCH] Fix macro info lookup for binaries containing DWARFv5 line table

Sourabh Singh Tomar via Gdb-patches
2021-05-12T17:17:55Z

Consider following C test case:
```
cat test.c
int main() {
int a = OUT;
}
```
Compiled as:
`clang -gdwarf-5 -fdebug-macro test.c`

Loading this bin into GDB and trying to access macro info,
GDB is unable to extract and display macro info correctly.
```
$gdb a.out
(gdb) start
(gdb) info macro OUT
Temporary breakpoint 1 at 0x2016a8: file test.c, line 3.
Starting program: /home/a.out
(gdb) info macro OUT
The symbol `OUT' has no definition as a C/C++ preprocessor macro
at /home/test.c:-1
```

The problems starts when processing file and dir indexes from DWARFv5
line table. DWARFv5 specifies:
- The current directory is explicitly present in the
directories field.
- The current compilation filename is explicitly present and
has index 0.
dir entry 0 contains the absoute path(Not including source
filename).
file entry 0 contains the source file name and refers dir 0.

As one may notice that dir index `0` will always contain
absoulte path(Not including the file name as), this causes
GDB to append the path + filename to construt macro info
`main_file->filename`. This leads to overriding of the macro filename
which will cause macro info lookup failure due to mismatch in
`main_file->filename` and `sal.symtab->filename`.

This problem does not pops up with GCC compiled binaries, since
GCC does not emits DWARFv5 compliant line tables.

Testing:
- NO regressing observed in `gdb.base` and associated macro based
tests.

gdb/ChangeLog
2021-05-12 Sourabh Singh Tomar <SourabhSingh.Tomar@amd.com>

* dwarf2/line-header.c: Updated the comment and extended
check in function `line_header::file_file_name` for
DWARFv5 line tables.

gdb/testsuite/ChangeLog
2021-05-12 Sourabh Singh Tomar <SourabhSingh.Tomar@amd.com>

* gdb.base/dwarf5-macro.exp: Add new tests.
* gdb.base/dwarf5-macro.c: Likewise.

Change-Id: I09f39c8680fca382523f4ac72bcbd1018851b480
---
gdb/ChangeLog | 5 +++
gdb/dwarf2/line-header.c | 11 +++++--
gdb/testsuite/ChangeLog | 5 +++
gdb/testsuite/gdb.base/dwarf5-macro.c | 3 ++
gdb/testsuite/gdb.base/dwarf5-macro.exp | 43 +++++++++++++++++++++++++
5 files changed, 64 insertions(+), 3 deletions(-)
create mode 100644 gdb/testsuite/gdb.base/dwarf5-macro.c
create mode 100644 gdb/testsuite/gdb.base/dwarf5-macro.exp

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index d564621f4c6..98650ff8a60 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,8 @@
+2021-05-12 Sourabh Singh Tomar <SourabhSingh.Tomar@amd.com>
+
+ * dwarf2/line-header.c: Update the comment and extended
+ check in function `line_header::file_file_name` for
+ DWARFv5 line tables.
2021-05-12 George Barrett <bob@bob131.so>

* NEWS (Guile API): Note the addition of the new procedure.
diff --git a/gdb/dwarf2/line-header.c b/gdb/dwarf2/line-header.c
index 7575297f966..d79051d326e 100644
--- a/gdb/dwarf2/line-header.c
+++ b/gdb/dwarf2/line-header.c
@@ -64,7 +64,8 @@ gdb::unique_xmalloc_ptr<char>
line_header::file_file_name (int file) const
{
/* Is the file number a valid index into the line header's file name
- table? Remember that file numbers start with one, not zero. */
+ table? Remember that file numbers start with zero for DWARFv5 and one
+ for DWARFv4 . */
if (is_valid_file_index (file))
{
const file_entry *fe = file_name_at (file);
@@ -72,7 +73,10 @@ line_header::file_file_name (int file) const
if (!IS_ABSOLUTE_PATH (fe->name))
{
const char *dir = fe->include_dir (this);
- if (dir != NULL)
+ /* The file at index 0 (valid starting with DWARF 5) is already
+ relative to the compilation directory. */
+
+ if (file != 0)
return gdb::unique_xmalloc_ptr<char> (concat (dir, SLASH_STRING,
fe->name,
(char *) NULL));
@@ -100,7 +104,8 @@ gdb::unique_xmalloc_ptr<char>
line_header::file_full_name (int file, const char *comp_dir) const
{
/* Is the file number a valid index into the line header's file name
- table? Remember that file numbers start with one, not zero. */
+ table? Remember that file numbers start with zero for DWARFv5 and one
+ for DWARFv4 . */
if (is_valid_file_index (file))
{
gdb::unique_xmalloc_ptr<char> relative = file_file_name (file);
diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog
index e0f64965d94..66b395b1e4c 100644
--- a/gdb/testsuite/ChangeLog
+++ b/gdb/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2021-05-12 Sourabh Singh Tomar <SourabhSingh.Tomar@amd.com>
+
+ * gdb.base/dwarf5-macro.exp: Add new tests.
+ * gdb.base/dwarf5-macro.c: Likewise.
+
2021-05-12 George Barrett <bob@bob131.so>

* gdb.guile/scm-value.exp (test_value_in_inferior): Add test for
diff --git a/gdb/testsuite/gdb.base/dwarf5-macro.c b/gdb/testsuite/gdb.base/dwarf5-macro.c
new file mode 100644
index 00000000000..0a7dc98c620
--- /dev/null
+++ b/gdb/testsuite/gdb.base/dwarf5-macro.c
@@ -0,0 +1,3 @@
+#define HELLO_GDB 1
+int main() {
+}
diff --git a/gdb/testsuite/gdb.base/dwarf5-macro.exp b/gdb/testsuite/gdb.base/dwarf5-macro.exp
new file mode 100644
index 00000000000..cf622cebe32
--- /dev/null
+++ b/gdb/testsuite/gdb.base/dwarf5-macro.exp
@@ -0,0 +1,43 @@
+# Copyright 2020-2021 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+standard_testfile
+set additional_flags {debug}
+file copy -force $srcdir/$subdir/$srcfile [standard_output_file $srcfile]
+gdb_compile outputs/gdb.base/dwarf5-macro/dwarf5-macro.c $binfile executable {debug}
+
+get_compiler_info
+if { [test_compiler_info "gcc-*"] } {
+ set options "additional_flags=-g3"
+} elseif { [test_compiler_info "clang-*"] } {
+ set options "additional_flags=-gdwarf-5 -fdebug-macro"
+}
+
+if { [prepare_for_testing "failed to prepare" \
+ ${testfile} ${srcfile}] } {
+ return
+}
+
+if { ![runto_main] } {
+ untested "could not run to main"
+ return
+}
+
+set filepat {dwarf5-macro.[ch]}
+set definition {}
+set location {}
+set nonzero {[1-9][0-9]*}
+gdb_test "info macro HELLO_GDB" \
+ ".*define HELLO_GDB 1"
--
2.17.1