From: Mark Wielaard Date: Mon, 10 Mar 2025 18:09:27 +0000 (+0100) Subject: readelf: Add support for printing DW_AT_language_name DW_LNAMEs X-Git-Tag: elfutils-0.193~31 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=66ae4b7ed9fd8dfd95c1276e752b279fc1aad996;p=thirdparty%2Felfutils.git readelf: Add support for printing DW_AT_language_name DW_LNAMEs 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 --- diff --git a/libdw/dwarf.h b/libdw/dwarf.h index 66017c15..5c856312 100644 --- a/libdw/dwarf.h +++ b/libdw/dwarf.h @@ -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. */ diff --git a/src/readelf.c b/src/readelf.c index 12d85472..e5bc16a5 100644 --- a/src/readelf.c +++ b/src/readelf.c @@ -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; diff --git a/tests/Makefile.am b/tests/Makefile.am index bf5a9b22..f53fd926 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -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 index 00000000..0f5d874d --- /dev/null +++ b/tests/run-readelf-lnames.sh @@ -0,0 +1,92 @@ +#! /bin/sh +# Copyright (C) 2025 Mark J. Wielaard +# 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 . + +. $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) "" + comp_dir (line_strp) "/tmp" + low_pc (addr) 0x0000000000401106 <_Z3foov> + high_pc (data8) 7 (0x000000000040110d
) + stmt_list (sec_offset) 0 + [ 33] subprogram abbrev: 2 + external (flag_present) yes + name (string) "foo" + decl_file (data1) (1) + decl_line (data1) 1 + decl_column (data1) 6 + linkage_name (strp) "_Z3foov" + low_pc (addr) 0x0000000000401106 <_Z3foov> + high_pc (data8) 7 (0x000000000040110d
) + 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) "" + comp_dir (line_strp) "/tmp" + low_pc (addr) 0x000000000040110d
+ 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) (1) + decl_line (data1) 1 + decl_column (data1) 5 + prototyped (flag_present) yes + type (ref4) [ a3] + low_pc (addr) 0x000000000040110d
+ 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 index 00000000..4ce4d179 Binary files /dev/null and b/tests/testfile-lnames.bz2 differ