summaryrefslogtreecommitdiffstats
path: root/libdw
diff options
context:
space:
mode:
authorMark Wielaard <mark@klomp.org>2018-06-22 22:44:51 +0200
committerMark Wielaard <mark@klomp.org>2018-06-29 10:22:51 +0200
commit2ee84b2ca8b2da2bc4ad5663babfa25d97aa85b4 (patch)
tree4d0858c6896050b169d77d7211b8f4d94109a209 /libdw
parente3b2060bb6a687587500c8d6c74145ef1d320c5c (diff)
libdw: Add dwarf_next_lines to read .debug_line tables without CUs.
It is sometimes useful to read .debug_line tables on their own without having an associated CU DIE. DWARF5 line tables are self-contained. Adjust dwarf_begin_elf to accept ELF files with just a .debug_line. Add a new function dwarf_next_lines that returns the Dwarf_Files and Dwarf_Lines while iterating over just the .debug_lines section. Since we parse and cache the information it also will try to match the CU a table is associated with. This is only necessary for DWARF4 line tables (we will need at least the compilation dir from the CU) and won't be done for DWARF5 line tables. It also isn't an error if there is no associated CU (but will mean for DWARF4 line tables the dir list and the file paths might not be complete). A typical way to call this new function is: Dwarf_Off off, next_off = 0; Dwarf_CU *cu = NULL; Dwarf_Files *files; size_t nfiles; Dwarf_Lines *lines; size_t nlines; int res; while ((res = dwarf_next_lines (dbg, off = next_off, &next_off, &cu, &files, &nfiles, &lines, &nlines)) == 0) { /* ... handle files and lines ... */ } if (res < 0) printf ("BAD dwarf_next_lines: %s\n", dwarf_errmsg (-1)); See libdw.h for the full documentation. For more examples on how to use the function see the new testcases next-files and next-lines. Also adjust the file paths for line tables missing a comp_dir. They are no longer made "absolute" by prepending a slash '/' in front of them. This really was not useful and didn't happen in any of the testcases. They are now just kept relative. Make eu-readelf --debug-dump=decodedline use dwarf_next_lines instead of iterating over the CUs to show the (decoded) line tables. This allows it to show decoded line tables even if there is no .debug_info section. New tests have been added that mimic the get-files and get-lines tests but use dwarf_next_lines instead of iterating over all CUs. They produce identical output (modulo the CU information). Also add a new test file that contains only a .debug_line section. Signed-off-by: Mark Wielaard <mark@klomp.org>
Diffstat (limited to 'libdw')
-rw-r--r--libdw/ChangeLog12
-rw-r--r--libdw/Makefile.am3
-rw-r--r--libdw/dwarf_begin_elf.c21
-rw-r--r--libdw/dwarf_getsrclines.c16
-rw-r--r--libdw/dwarf_next_lines.c197
-rw-r--r--libdw/libdw.h18
-rw-r--r--libdw/libdw.map5
7 files changed, 248 insertions, 24 deletions
diff --git a/libdw/ChangeLog b/libdw/ChangeLog
index 11de763f..4e4fd265 100644
--- a/libdw/ChangeLog
+++ b/libdw/ChangeLog
@@ -1,3 +1,15 @@
+2018-06-25 Mark Wielaard <mark@klomp.org>
+
+ * Makefile.am (libdw_a_SOURCES): Add dwarf_next_lines.c.
+ * libdw.h (dwarf_next_lines): New function declaration.
+ * libdw.map (ELFUTILS_0.173): New section.
+ * dwarf_next_lines.c: New files.
+ * dwarf_begin_elf.c (check_section): Don't error out when elf
+ decompression fails.
+ (valid_p): Allow just a single .debug_line section.
+ * dwarf_getsrclines.c (read_srclines): Keep files relative if comp_dir
+ is missing.
+
2018-06-22 Mark Wielaard <mark@klomp.org>
* dwarf_nextcu.c (__libdw_next_unit): Set next_off to -1 when it would
diff --git a/libdw/Makefile.am b/libdw/Makefile.am
index 41df4f3b..7a3d5322 100644
--- a/libdw/Makefile.am
+++ b/libdw/Makefile.am
@@ -91,7 +91,8 @@ libdw_a_SOURCES = dwarf_begin.c dwarf_begin_elf.c dwarf_end.c dwarf_getelf.c \
dwarf_getalt.c dwarf_setalt.c dwarf_cu_getdwarf.c \
dwarf_cu_die.c dwarf_peel_type.c dwarf_default_lower_bound.c \
dwarf_die_addr_die.c dwarf_get_units.c \
- libdw_find_split_unit.c dwarf_cu_info.c
+ libdw_find_split_unit.c dwarf_cu_info.c \
+ dwarf_next_lines.c
if MAINTAINER_MODE
BUILT_SOURCES = $(srcdir)/known-dwarf.h
diff --git a/libdw/dwarf_begin_elf.c b/libdw/dwarf_begin_elf.c
index 513af2b1..e1542c75 100644
--- a/libdw/dwarf_begin_elf.c
+++ b/libdw/dwarf_begin_elf.c
@@ -156,17 +156,9 @@ check_section (Dwarf *result, GElf_Ehdr *ehdr, Elf_Scn *scn, bool inscngrp)
{
if (elf_compress (scn, 0, 0) < 0)
{
- /* If we failed to decompress the section and it's the
- debug_info section, then fail with specific error rather
- than the generic NO_DWARF. Without debug_info we can't do
- anything (see also valid_p()). */
- if (cnt == IDX_debug_info)
- {
- Dwarf_Sig8_Hash_free (&result->sig8_hash);
- __libdw_seterrno (DWARF_E_COMPRESSED_ERROR);
- free (result);
- return NULL;
- }
+ /* It would be nice if we could fail with a specific error.
+ But we don't know if this was an essential section or not.
+ So just continue for now. See also valid_p(). */
return result;
}
}
@@ -214,11 +206,10 @@ valid_p (Dwarf *result)
/* We looked at all the sections. Now determine whether all the
sections with debugging information we need are there.
- XXX Which sections are absolutely necessary? Add tests if
- necessary. For now we require only .debug_info. Hopefully this
- is correct. */
+ Require at least one section that can be read "standalone". */
if (likely (result != NULL)
- && unlikely (result->sectiondata[IDX_debug_info] == NULL))
+ && unlikely (result->sectiondata[IDX_debug_info] == NULL
+ && result->sectiondata[IDX_debug_line] == NULL))
{
Dwarf_Sig8_Hash_free (&result->sig8_hash);
__libdw_seterrno (DWARF_E_NO_DWARF);
diff --git a/libdw/dwarf_getsrclines.c b/libdw/dwarf_getsrclines.c
index 85105387..1432b1db 100644
--- a/libdw/dwarf_getsrclines.c
+++ b/libdw/dwarf_getsrclines.c
@@ -508,11 +508,10 @@ read_srclines (Dwarf *dbg,
{
/* This value could be NULL in case the DW_AT_comp_dir
was not present. We cannot do much in this case.
- The easiest thing is to convert the path in an
- absolute path. */
+ Just keep the file relative. */
cp = stpcpy (cp, dirarray[diridx].dir);
+ *cp++ = '/';
}
- *cp++ = '/';
strcpy (cp, fname);
assert (strlen (new_file->info.name)
< dirarray[diridx].len + 1 + fnamelen + 1);
@@ -803,11 +802,12 @@ read_srclines (Dwarf *dbg,
if (dirarray[diridx].dir != NULL)
/* This value could be NULL in case the
DW_AT_comp_dir was not present. We
- cannot do much in this case. The easiest
- thing is to convert the path in an
- absolute path. */
- cp = stpcpy (cp, dirarray[diridx].dir);
- *cp++ = '/';
+ cannot do much in this case. Just
+ keep the file relative. */
+ {
+ cp = stpcpy (cp, dirarray[diridx].dir);
+ *cp++ = '/';
+ }
strcpy (cp, fname);
}
diff --git a/libdw/dwarf_next_lines.c b/libdw/dwarf_next_lines.c
new file mode 100644
index 00000000..9b76b47e
--- /dev/null
+++ b/libdw/dwarf_next_lines.c
@@ -0,0 +1,197 @@
+/* Iterate through the debug line table.
+ Copyright (C) 2018 Red Hat, Inc.
+ This file is part of elfutils.
+
+ This file is free software; you can redistribute it and/or modify
+ it under the terms of either
+
+ * the GNU Lesser General Public License as published by the Free
+ Software Foundation; either version 3 of the License, or (at
+ your option) any later version
+
+ or
+
+ * the GNU General Public License as published by the Free
+ Software Foundation; either version 2 of the License, or (at
+ your option) any later version
+
+ or both in parallel, as here.
+
+ 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 copies of the GNU General Public License and
+ the GNU Lesser General Public License along with this program. If
+ not, see <http://www.gnu.org/licenses/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <libdwP.h>
+
+
+int
+dwarf_next_lines (Dwarf *dbg, Dwarf_Off off,
+ Dwarf_Off *next_off, Dwarf_CU **cu,
+ Dwarf_Files **srcfiles, size_t *nfiles,
+ Dwarf_Lines **srclines, size_t *nlines)
+{
+ /* Ignore existing errors. */
+ if (dbg == NULL)
+ return -1;
+
+ Elf_Data *lines = dbg->sectiondata[IDX_debug_line];
+ if (lines == NULL)
+ {
+ __libdw_seterrno (DWARF_E_NO_DEBUG_LINE);
+ return -1;
+ }
+
+ if (off == (Dwarf_Off) -1
+ || lines->d_size < 4
+ || off >= lines->d_size)
+ {
+ *next_off = (Dwarf_Off) -1;
+ return 1;
+ }
+
+ /* Read enough of the header to know where the next table is and
+ whether we need to lookup the CU (version < 5). */
+ const unsigned char *linep = lines->d_buf + off;
+ const unsigned char *lineendp = lines->d_buf + lines->d_size;
+
+ if ((size_t) (lineendp - linep) < 4)
+ {
+ invalid_data:
+ __libdw_seterrno (DWARF_E_INVALID_DEBUG_LINE);
+ return -1;
+ }
+
+ *next_off = off + 4;
+ Dwarf_Word unit_length = read_4ubyte_unaligned_inc (dbg, linep);
+ if (unit_length == DWARF3_LENGTH_64_BIT)
+ {
+ if ((size_t) (lineendp - linep) < 8)
+ goto invalid_data;
+ unit_length = read_8ubyte_unaligned_inc (dbg, linep);
+ *next_off += 8;
+ }
+
+ if (unit_length > (size_t) (lineendp - linep))
+ goto invalid_data;
+
+ *next_off += unit_length;
+ lineendp = linep + unit_length;
+
+ if ((size_t) (lineendp - linep) < 2)
+ goto invalid_data;
+ uint_fast16_t version = read_2ubyte_unaligned_inc (dbg, linep);
+
+ Dwarf_Die cudie;
+ if (version < 5)
+ {
+ /* We need to find the matching CU to get the comp_dir. Use the
+ given CU as hint where to start searching. Normally it will
+ be the next CU that has a statement list. */
+ Dwarf_CU *given_cu = *cu;
+ Dwarf_CU *next_cu = given_cu;
+ bool found = false;
+ while (dwarf_get_units (dbg, next_cu, &next_cu, NULL, NULL,
+ &cudie, NULL) == 0)
+ {
+ if (dwarf_hasattr (&cudie, DW_AT_stmt_list))
+ {
+ Dwarf_Attribute attr;
+ Dwarf_Word stmt_off;
+ if (dwarf_formudata (dwarf_attr (&cudie, DW_AT_stmt_list, &attr),
+ &stmt_off) == 0
+ && stmt_off == off)
+ {
+ found = true;
+ break;
+ }
+ }
+ else if (off == 0
+ && (next_cu->unit_type == DW_UT_split_compile
+ || next_cu->unit_type == DW_UT_split_type))
+ {
+ /* For split units (in .dwo files) there is only one table
+ at offset zero (containing just the files, no lines). */
+ found = true;
+ break;
+ }
+ }
+
+ if (!found && given_cu != NULL)
+ {
+ /* The CUs might be in a different order from the line
+ tables. Need to do a linear search (but stop at the given
+ CU, since we already searched those. */
+ next_cu = NULL;
+ while (dwarf_get_units (dbg, next_cu, &next_cu, NULL, NULL,
+ &cudie, NULL) == 0
+ && next_cu != given_cu)
+ {
+ Dwarf_Attribute attr;
+ Dwarf_Word stmt_off;
+ if (dwarf_formudata (dwarf_attr (&cudie, DW_AT_stmt_list, &attr),
+ &stmt_off) == 0
+ && stmt_off == off)
+ {
+ found = true;
+ break;
+ }
+ }
+ }
+
+ if (found)
+ *cu = next_cu;
+ else
+ *cu = NULL;
+ }
+ else
+ *cu = NULL;
+
+ const char *comp_dir;
+ unsigned address_size;
+ if (*cu != NULL)
+ {
+ comp_dir = __libdw_getcompdir (&cudie);
+ address_size = (*cu)->address_size;
+ }
+ else
+ {
+ comp_dir = NULL;
+
+ size_t esize;
+ char *ident = elf_getident (dbg->elf, &esize);
+ if (ident == NULL || esize < EI_NIDENT)
+ goto invalid_data;
+ address_size = ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
+ }
+
+ if (__libdw_getsrclines (dbg, off, comp_dir, address_size,
+ srclines, srcfiles) != 0)
+ return -1;
+
+ if (nlines != NULL)
+ {
+ if (srclines != NULL && *srclines != NULL)
+ *nlines = (*srclines)->nlines;
+ else
+ *nlines = 0;
+ }
+
+ if (nfiles != NULL)
+ {
+ if (srcfiles != NULL && *srcfiles != NULL)
+ *nfiles = (*srcfiles)->nfiles;
+ else
+ *nfiles = 0;
+ }
+
+ return 0;
+}
diff --git a/libdw/libdw.h b/libdw/libdw.h
index a871eb29..b500aa86 100644
--- a/libdw/libdw.h
+++ b/libdw/libdw.h
@@ -716,6 +716,24 @@ extern int dwarf_getsrcdirs (Dwarf_Files *files,
const char *const **result, size_t *ndirs)
__nonnull_attribute__ (2, 3);
+/* Iterates through the debug line units. Returns 0 on success, -1 on
+ error or 1 if there are no more units. To start iterating use zero
+ for OFF and set *CU to NULL. On success NEXT_OFF will be set to
+ the next offset to use. The *CU will be set if this line table
+ needed a specific CU and needs to be given when calling
+ dwarf_next_lines again (to help dwarf_next_lines quickly find the
+ next CU). *CU might be set to NULL when it couldn't be found (the
+ compilation directory entry will be the empty string in that case)
+ or for DWARF 5 or later tables, which are self contained. SRCFILES
+ and SRCLINES may be NULL if the caller is not interested in the
+ actual line or file table. On success and when not NULL, NFILES
+ and NLINES will be set to the number of files in the file table and
+ number of lines in the line table. */
+extern int dwarf_next_lines (Dwarf *dwarf, Dwarf_Off off,
+ Dwarf_Off *next_off, Dwarf_CU **cu,
+ Dwarf_Files **srcfiles, size_t *nfiles,
+ Dwarf_Lines **srclines, size_t *nlines)
+ __nonnull_attribute__ (3,4);
/* Return location expression, decoded as a list of operations. */
extern int dwarf_getlocation (Dwarf_Attribute *attr, Dwarf_Op **expr,
diff --git a/libdw/libdw.map b/libdw/libdw.map
index 17c99f61..b3e84d5d 100644
--- a/libdw/libdw.map
+++ b/libdw/libdw.map
@@ -352,3 +352,8 @@ ELFUTILS_0.171 {
dwarf_getabbrevattr_data;
dwarf_cu_info;
} ELFUTILS_0.170;
+
+ELFUTILS_0.173 {
+ global:
+ dwarf_next_lines;
+} ELFUTILS_0.171;