]> git.ipfire.org Git - thirdparty/elfutils.git/commitdiff
readelf: Add support for printing DW_AT_language_name DW_LNAMEs
authorMark Wielaard <mark@klomp.org>
Mon, 10 Mar 2025 18:09:27 +0000 (19:09 +0100)
committerMark Wielaard <mark@klomp.org>
Tue, 11 Mar 2025 09:54:22 +0000 (10:54 +0100)
Add a testfile using GCC 15 (experimental).

 * libdw/dwarf.h: Add DW_LNAME_lo_user and DW_LNAME_hi_user.
 * src/readelf.c (dwarf_lname_string): New function.
 (dwarf_lname_name): Likewise.
 (attr_callback): Handle DW_AT_language_name by calling
 dwarf_lname_name.

 * run-readelf-lnames.sh: New test.
 * testfile-lnames.bz2: New testfile.
 * tests/Makefile.am (TESTS): Add run-readelf-lnames.sh.
 (EXTRA_DIST): Add run-readelf-lnames.sh and
 testfile-lnames.bz2.

Signed-off-by: Mark Wielaard <mark@klomp.org>
libdw/dwarf.h
src/readelf.c
tests/Makefile.am
tests/run-readelf-lnames.sh [new file with mode: 0755]
tests/testfile-lnames.bz2 [new file with mode: 0755]

index 66017c1518ddc35c5521a197b5ece52d39f23993..5c8563128644fa9362311e8f9b44991d90b6c5d2 100644 (file)
@@ -834,7 +834,10 @@ enum
     DW_LNAME_P4 = 0x002b,
     DW_LNAME_Metal = 0x002c,
     DW_LNAME_V = 0x002d,
-    DW_LNAME_Algol68 = 0x002e
+    DW_LNAME_Algol68 = 0x002e,
+
+    DW_LNAME_lo_user = 0x8000,
+    DW_LNAME_hi_user = 0xffff
   };
 
 /* DWARF identifier case encodings.  */
index 12d85472cef1df0b081fe9905a9b42661f38b631..e5bc16a5bc4382d5cbe66cd28e809af4659ae886 100644 (file)
@@ -4371,6 +4371,20 @@ dwarf_lang_string (unsigned int lang)
 }
 
 
+static const char *
+dwarf_lname_string (unsigned int lname)
+{
+  switch (lname)
+    {
+#define DWARF_ONE_KNOWN_DW_LNAME(NAME, CODE) case CODE: return #NAME;
+      DWARF_ALL_KNOWN_DW_LNAME
+#undef DWARF_ONE_KNOWN_DW_LNAME
+    default:
+      return NULL;
+    }
+}
+
+
 static const char *
 dwarf_inline_string (unsigned int code)
 {
@@ -4678,6 +4692,15 @@ dwarf_lang_name (unsigned int lang)
 }
 
 
+static const char *
+dwarf_lname_name (unsigned int lname)
+{
+  const char *ret = dwarf_lname_string (lname);
+  return string_or_unknown (ret, lname, DW_LNAME_lo_user, DW_LNAME_hi_user,
+                           false);
+}
+
+
 static const char *
 dwarf_inline_name (unsigned int code)
 {
@@ -7938,6 +7961,9 @@ attr_callback (Dwarf_Attribute *attrp, void *arg)
        case DW_AT_language:
          valuestr = dwarf_lang_name (num);
          break;
+       case DW_AT_language_name:
+         valuestr = dwarf_lname_name (num);
+         break;
        case DW_AT_encoding:
          valuestr = dwarf_encoding_name (num);
          break;
index bf5a9b228126f60e4eb4f31461276f6140d706da..f53fd92662e3be2ed51fea43b994b8f6dd4284ba 100644 (file)
@@ -165,6 +165,7 @@ TESTS = run-arextract.sh run-arsymtest.sh run-ar.sh newfile test-nlist \
        run-test-flag-nobits.sh run-prelink-addr-test.sh \
        run-dwarf-getstring.sh run-rerequest_tag.sh run-typeiter.sh \
        run-readelf-d.sh run-readelf-gdb_index.sh run-unstrip-n.sh \
+       run-readelf-lnames.sh \
        run-low_high_pc.sh run-macro-test.sh run-elf_cntl_gelf_getshdr.sh \
        run-test-archive64.sh run-readelf-vmcoreinfo.sh \
        run-readelf-mixed-corenote.sh run-dwfllines.sh \
@@ -451,6 +452,7 @@ EXTRA_DIST = run-arextract.sh run-arsymtest.sh run-ar.sh \
             run-typeiter.sh testfile59.bz2 \
             run-readelf-d.sh testlib_dynseg.so.bz2 \
             run-readelf-Dd.sh \
+            run-readelf-lnames.sh testfile-lnames.bz2 \
             testfile-s390x-hash-both.bz2 \
             run-readelf-gdb_index.sh testfilegdbindex5.bz2 \
             testfilegdbindex7.bz2 testfilegdbindex9.bz2 \
diff --git a/tests/run-readelf-lnames.sh b/tests/run-readelf-lnames.sh
new file mode 100755 (executable)
index 0000000..0f5d874
--- /dev/null
@@ -0,0 +1,92 @@
+#! /bin/sh
+# Copyright (C) 2025 Mark J. Wielaard <mark@klomp.org>
+# This file is part of elfutils.
+#
+# This file 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.
+#
+# elfutils 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/>.
+
+. $srcdir/test-subr.sh
+
+# Using GCC 15.0.1 20250310 (experimental)
+#
+# echo "void foo () { }" | g++ -g -c -x c++ -std=c++20 - -o cpp.o
+# echo "int main () { }" | gcc -g -c -x c -std=c23 - -o c.o
+# gcc -g  -o testfile-lnames cpp.o c.o
+#
+# Note this version outputs an older DW_AT_language you need to
+# see the DW_AT_language_version to see the real std used.
+
+testfiles testfile-lnames
+
+testrun_compare ${abs_top_builddir}/src/readelf --debug-dump=info testfile-lnames <<\EOF
+
+DWARF section [28] '.debug_info' at offset 0x3314:
+ [Offset]
+ Compilation unit at offset 0:
+ Version: 5, Abbreviation section offset: 0, Address size: 8, Offset size: 4
+ Unit type: compile (1)
+ [     c]  compile_unit         abbrev: 1
+           producer             (strp) "GNU C++20 15.0.1 20250310 (experimental) -mtune=generic -march=x86-64 -g -std=c++20"
+           language             (data1) C_plus_plus_14 (33)
+           language_name        (data1) C_plus_plus (4)
+           language_version     (data4) 202002
+           name                 (line_strp) "<stdin>"
+           comp_dir             (line_strp) "/tmp"
+           low_pc               (addr) 0x0000000000401106 <_Z3foov>
+           high_pc              (data8) 7 (0x000000000040110d <main>)
+           stmt_list            (sec_offset) 0
+ [    33]    subprogram           abbrev: 2
+             external             (flag_present) yes
+             name                 (string) "foo"
+             decl_file            (data1) <stdin> (1)
+             decl_line            (data1) 1
+             decl_column          (data1) 6
+             linkage_name         (strp) "_Z3foov"
+             low_pc               (addr) 0x0000000000401106 <_Z3foov>
+             high_pc              (data8) 7 (0x000000000040110d <main>)
+             frame_base           (exprloc) 
+              [ 0] call_frame_cfa
+             call_all_calls       (flag_present) yes
+ Compilation unit at offset 82:
+ Version: 5, Abbreviation section offset: 51, Address size: 8, Offset size: 4
+ Unit type: compile (1)
+ [    5e]  compile_unit         abbrev: 1
+           producer             (strp) "GNU C23 15.0.1 20250310 (experimental) -mtune=generic -march=x86-64 -g -std=c23"
+           language             (data1) C11 (29)
+           language_name        (data1) C (3)
+           language_version     (data4) 202311
+           name                 (line_strp) "<stdin>"
+           comp_dir             (line_strp) "/tmp"
+           low_pc               (addr) 0x000000000040110d <main>
+           high_pc              (data8) 11 (0x0000000000401118 <_fini>)
+           stmt_list            (sec_offset) 76
+ [    85]    subprogram           abbrev: 2
+             external             (flag_present) yes
+             name                 (strp) "main"
+             decl_file            (data1) <stdin> (1)
+             decl_line            (data1) 1
+             decl_column          (data1) 5
+             prototyped           (flag_present) yes
+             type                 (ref4) [    a3]
+             low_pc               (addr) 0x000000000040110d <main>
+             high_pc              (data8) 11 (0x0000000000401118 <_fini>)
+             frame_base           (exprloc) 
+              [ 0] call_frame_cfa
+             call_all_calls       (flag_present) yes
+ [    a3]    base_type            abbrev: 3
+             byte_size            (data1) 4
+             encoding             (data1) signed (5)
+             name                 (string) "int"
+EOF
+
+exit 0
diff --git a/tests/testfile-lnames.bz2 b/tests/testfile-lnames.bz2
new file mode 100755 (executable)
index 0000000..4ce4d17
Binary files /dev/null and b/tests/testfile-lnames.bz2 differ