libdw: Add dwarf_next_lines to read .debug_line tables without CUs.
authorMark Wielaard <mark@klomp.org>
Fri, 22 Jun 2018 20:44:51 +0000 (22:44 +0200)
committerMark Wielaard <mark@klomp.org>
Fri, 29 Jun 2018 08:22:51 +0000 (10:22 +0200)
commit2ee84b2ca8b2da2bc4ad5663babfa25d97aa85b4
tree4d0858c6896050b169d77d7211b8f4d94109a209
parente3b2060bb6a687587500c8d6c74145ef1d320c5c
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>
16 files changed:
libdw/ChangeLog
libdw/Makefile.am
libdw/dwarf_begin_elf.c
libdw/dwarf_getsrclines.c
libdw/dwarf_next_lines.c [new file with mode: 0644]
libdw/libdw.h
libdw/libdw.map
src/ChangeLog
src/readelf.c
tests/ChangeLog
tests/Makefile.am
tests/next-files.c [new file with mode: 0644]
tests/next-lines.c [new file with mode: 0644]
tests/run-next-files.sh [new file with mode: 0755]
tests/run-next-lines.sh [new file with mode: 0755]
tests/testfile-only-debug-line.bz2 [new file with mode: 0644]