bfd: Improve lookup of file / line information for errors
authorAndrew Burgess <andrew.burgess@embecosm.com>
Mon, 19 Dec 2016 15:27:59 +0000 (15:27 +0000)
committerAndrew Burgess <andrew.burgess@embecosm.com>
Thu, 16 Feb 2017 13:06:14 +0000 (13:06 +0000)
When looking up file and line information (used from the linker to
report error messages) if no symbol is passed in, then use the symbol
list to look for a matching symbol.

If a matching symbol is found then use this to look up the file / line
information.

This should improve errors when looking up file / line information for
data sections.  Hopefully we should find a matching data symbol, which
should, in turn (we hope) match a DW_TAG_variable in the DWARF, this
should allow us to give accurate file / line errors for data symbols.

As the hope is to find a matching DW_TAG_variable in the DWARF then we
ignore section symbols, and prefer global symbols to locals.

bfd/ChangeLog:

* dwarf2.c (_bfd_dwarf2_find_nearest_line): Perform symbol lookup
before trying to fine matching file and line information.

ld/ChangeLog:

* testsuite/ld-elf/shared.exp: Update expected results.
* testsuite/ld-elf/dwarf2.err: Likewise

bfd/ChangeLog
bfd/dwarf2.c
ld/ChangeLog
ld/testsuite/ld-elf/dwarf2.err
ld/testsuite/ld-elf/shared.exp

index 01314bda146263b08391c0143a6bbad7b3aca8c5..40edca71ff455469669ad1977a0bd99d0b0b5907 100644 (file)
@@ -1,3 +1,8 @@
+2017-02-16  Andrew Burgess  <andrew.burgess@embecosm.com>
+
+       * dwarf2.c (_bfd_dwarf2_find_nearest_line): Perform symbol lookup
+       before trying to fine matching file and line information.
+
 2017-02-16  Andrew Burgess  <andrew.burgess@embecosm.com>
 
        * dwarf2.c (struct dwarf2_debug): Add orig_bfd member.
index 03447a908a5337b543c64849ffb086183ba0ae6d..9bb81264872554d766a47bc1fdf38cb97358d847 100644 (file)
@@ -4155,6 +4155,38 @@ _bfd_dwarf2_find_nearest_line (bfd *abfd,
     {
       BFD_ASSERT (section != NULL && functionname_ptr != NULL);
       addr = offset;
+
+      /* If we have no SYMBOL but the section we're looking at is not a
+         code section, then take a look through the list of symbols to see
+         if we have a symbol at the address we're looking for.  If we do
+         then use this to look up line information.  This will allow us to
+         give file and line results for data symbols.  We exclude code
+         symbols here, if we look up a function symbol and then look up the
+         line information we'll actually return the line number for the
+         opening '{' rather than the function definition line.  This is
+         because looking up by symbol uses the line table, in which the
+         first line for a function is usually the opening '{', while
+         looking up the function by section + offset uses the
+         DW_AT_decl_line from the function DW_TAG_subprogram for the line,
+         which will be the line of the function name.  */
+      if ((section->flags & SEC_CODE) == 0)
+       {
+         asymbol **tmp;
+
+         for (tmp = symbols; (*tmp) != NULL; ++tmp)
+           if ((*tmp)->the_bfd == abfd
+               && (*tmp)->section == section
+               && (*tmp)->value == offset
+               && ((*tmp)->flags & BSF_SECTION_SYM) == 0)
+             {
+               symbol = *tmp;
+               do_line = TRUE;
+                /* For local symbols, keep going in the hope we find a
+                   global.  */
+                if ((symbol->flags & BSF_GLOBAL) != 0)
+                  break;
+             }
+       }
     }
 
   if (section->output_section)
index 696e08afffb5e1bcfa847e91e19fce01c4c63837..7d4739756a399effdd6efd18295adf15e8236220 100644 (file)
@@ -1,3 +1,8 @@
+2017-02-16  Andrew Burgess  <andrew.burgess@embecosm.com>
+
+       * testsuite/ld-elf/shared.exp: Update expected results.
+       * testsuite/ld-elf/dwarf2.err: Likewise
+
 2017-02-16  Andrew Burgess  <andrew.burgess@embecosm.com>
 
        * testsuite/ld-elf/dwarf.exp (build_tests): Add new tests.
index 872d282363543c796ef6aa0a14324673d61c34d4..b4ea67fc1959b6847884c22d14e0b6e5599fd808 100644 (file)
@@ -1,4 +1,5 @@
-tmpdir/dwarf2b\.o:\(\.data\+0x0\): multiple definition of `global_var'
-tmpdir/dwarf2a\.o:\(\.data\+0x0\): first defined here
-tmpdir/dwarf2b\.o:\(\.data\+0x4\): multiple definition of `other_var'
-tmpdir/dwarf2a\.o:\(\.data\+0x4\): first defined here
+tmpdir/dwarf2b\.o:.*dwarf2b\.c:1: multiple definition of `global_var'
+tmpdir/dwarf2a\.o:.*dwarf2a\.c:1: first defined here
+tmpdir/dwarf2b\.o:.*dwarf2b\.c:2: multiple definition of `other_var'
+tmpdir/dwarf2a\.o:.*dwarf2a\.c:2: first defined here
+#...
\ No newline at end of file
index 9d5a9d9fe904f28b9468d66217615ea7908d9767..3b8ab181d1f98144f3493440a34c06ff20aa04e1 100644 (file)
@@ -105,7 +105,7 @@ set build_tests {
    "-shared" "-fPIC"
   {beginwarn.c end.c}
   {{readelf {-S --wide} libbarw.rd}
-   {warning "^.*\\): warning: function foo is deprecated$"}}
+   {warning "^.*beginwarn.c:7: warning: function foo is deprecated$"}}
   "libbarw.so" "c"}
   {"Build hidden libbar.so"
    "-shared" "-fPIC"
@@ -350,7 +350,7 @@ set run_tests [list \
     [list "Run warn with versioned libfoo.so" \
      "-Wl,--no-as-needed tmpdir/beginwarn.o tmpdir/libfoov.so" "" \
      {main.c} "warn" "warn.out" \
-     "" "c" {^.*\): warning: function foo is deprecated$} ] \
+     "" "c" {^.*beginwarn.c:7: warning: function foo is deprecated$} ] \
     [list "Run protected with versioned libfoo.so" \
      "-Wl,--no-as-needed tmpdir/begin.o tmpdir/libfoov.so tmpdir/endprotected.o" "" \
      {main.c} "protected" "normal.out" ] \