libdw: Search for abstract origin in the correct CU

Message ID 20230123091520.688326-1-mark@klomp.org
State Committed
Headers
Series libdw: Search for abstract origin in the correct CU |

Commit Message

Mark Wielaard Jan. 23, 2023, 9:15 a.m. UTC
  With gcc -flto the abstract origin of an inlined subroutine
could be in a different CU. dwarf_getscopes might return an
empty scope if it cannot find the abstract origin scope. So
make sure to search in the

We also tried to add the origin match in pc_record directly
in the current inlined scope. This always failed, causing
to do a needless traversal, followed by the full CU scan in
dwarf_getscopes. Just always stop the pc_record search and
then do the CU origin_match in dwarf_getscopes.

Signed-off-by: Mark Wielaard <mark@klomp.org>
---
 libdw/ChangeLog                |   6 +++++
 libdw/dwarf_getscopes.c        |  18 ++++++++------
 tests/ChangeLog                |   6 +++++
 tests/Makefile.am              |   1 +
 tests/run-addr2line-i-test.sh  |  43 +++++++++++++++++++++++++++++++++
 tests/testfile-inlines-lto.bz2 | Bin 0 -> 2838 bytes
 6 files changed, 66 insertions(+), 8 deletions(-)
 create mode 100755 tests/testfile-inlines-lto.bz2
  

Comments

Mark Wielaard Jan. 30, 2023, 9:12 p.m. UTC | #1
Hi,

On Mon, Jan 23, 2023 at 10:15:20AM +0100, Mark Wielaard wrote:
> With gcc -flto the abstract origin of an inlined subroutine
> could be in a different CU. dwarf_getscopes might return an
> empty scope if it cannot find the abstract origin scope. So
> make sure to search in the
> 
> We also tried to add the origin match in pc_record directly
> in the current inlined scope. This always failed, causing
> to do a needless traversal, followed by the full CU scan in
> dwarf_getscopes. Just always stop the pc_record search and
> then do the CU origin_match in dwarf_getscopes.

Pushed.

Cheers,

Mark
  

Patch

diff --git a/libdw/ChangeLog b/libdw/ChangeLog
index 4c7af94e..71e96c88 100644
--- a/libdw/ChangeLog
+++ b/libdw/ChangeLog
@@ -1,3 +1,9 @@ 
+2023-01-22  Mark Wielaard  <mark@klomp.org>
+
+	* dwarf_getscopes.c (pc_record): Return nscopes when done.
+	(dwarf_getscopes): Call __libdw_visit_scopes with
+	inlined_origin CU.
+
 2022-12-20  Mark Wielaard  <mark@klomp.org>
 
 	* Makefile.am (AM_CPPFLAGS): Add -I$(srcdir)/../libebl.
diff --git a/libdw/dwarf_getscopes.c b/libdw/dwarf_getscopes.c
index 5662eecf..833f1ac5 100644
--- a/libdw/dwarf_getscopes.c
+++ b/libdw/dwarf_getscopes.c
@@ -1,5 +1,6 @@ 
 /* Return scope DIEs containing PC address.
    Copyright (C) 2005, 2007, 2015 Red Hat, Inc.
+   Copyright (C) 2023 Mark J. Wielaard <mark@klomp.org>
    This file is part of elfutils.
 
    This file is free software; you can redistribute it and/or modify
@@ -173,12 +174,8 @@  pc_record (unsigned int depth, struct Dwarf_Die_Chain *die, void *arg)
     /* Not there yet.  */
     return 0;
 
-  /* Now we are in a scope that contains the concrete inlined instance.
-     Search it for the inline function's abstract definition.
-     If we don't find it, return to search the containing scope.
-     If we do find it, the nonzero return value will bail us out
-     of the postorder traversal.  */
-  return __libdw_visit_scopes (depth, die, NULL, &origin_match, NULL, a);
+  /* This is the innermost inline scope, we are done here.  */
+  return a->nscopes;
 }
 
 
@@ -193,8 +190,13 @@  dwarf_getscopes (Dwarf_Die *cudie, Dwarf_Addr pc, Dwarf_Die **scopes)
 
   int result = __libdw_visit_scopes (0, &cu, NULL, &pc_match, &pc_record, &a);
 
-  if (result == 0 && a.scopes != NULL)
-    result = __libdw_visit_scopes (0, &cu, NULL, &origin_match, NULL, &a);
+  if (result >= 0 && a.scopes != NULL && a.inlined > 0)
+    {
+      /* We like the find the inline function's abstract definition
+         scope, but that might be in a different CU.  */
+      cu.die = CUDIE (a.inlined_origin.cu);
+      result = __libdw_visit_scopes (0, &cu, NULL, &origin_match, NULL, &a);
+    }
 
   if (result > 0)
     *scopes = a.scopes;
diff --git a/tests/ChangeLog b/tests/ChangeLog
index 66807856..ba7c1c93 100644
--- a/tests/ChangeLog
+++ b/tests/ChangeLog
@@ -1,3 +1,9 @@ 
+2023-01-22  Mark Wielaard  <mark@klomp.org>
+
+	* testfile-inlines-lto.bz2: New testfile.
+	* run-addr2line-i-test.sh: Add new lto inlines test.
+	* Makefile.am (EXTRA_DIST): Add testfile-inlines-lto.bz2.
+
 2023-01-19  Mark Wielaard  <mark@klomp.org>
 
 	* run-debuginfod-query-retry.sh: Use libdebuginfod.so.1 instead
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 71b19601..393ce7b1 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -426,6 +426,7 @@  EXTRA_DIST = run-arextract.sh run-arsymtest.sh run-ar.sh \
 	     testfilenolines.bz2 test-core-lib.so.bz2 test-core.core.bz2 \
 	     test-core.exec.bz2 run-addr2line-test.sh \
 	     run-addr2line-i-test.sh testfile-inlines.bz2 \
+	     testfile-inlines-lto.bz2 \
 	     run-addr2line-i-lex-test.sh testfile-lex-inlines.bz2 \
 	     run-addr2line-i-demangle-test.sh run-addr2line-alt-debugpath.sh \
 	     testfileppc32.bz2 testfileppc64.bz2 \
diff --git a/tests/run-addr2line-i-test.sh b/tests/run-addr2line-i-test.sh
index d08f3cba..4f63e487 100755
--- a/tests/run-addr2line-i-test.sh
+++ b/tests/run-addr2line-i-test.sh
@@ -220,4 +220,47 @@  testrun_compare ${abs_top_builddir}/src/addr2line --pretty-print -a -f -i -e tes
  (inlined by) _Z2fuv at /tmp/x.cpp:33
 EOF
 
+# == x.cpp ==
+# g++ x.cpp -g -fPIC -olibx.so -shared -O0 -flto
+#
+# __attribute__((always_inline)) inline
+# int foobar(int i)
+# {
+#   return i + 1;
+# }
+#
+# __attribute__((always_inline)) inline
+# int fubar(int i)
+# {
+#   return i + 1;
+# }
+#
+# __attribute__((always_inline)) inline
+# int bar(int i)
+# {
+#   return fubar(i++);
+# }
+#
+# __attribute__((always_inline)) inline
+# int foo(int i)
+# {
+#   return foobar(i++);
+# }
+#
+# int fu(int i)
+# {
+#   return foo(i++) + bar(i++);
+# }
+
+testfiles testfile-inlines-lto
+
+testrun_compare ${abs_top_builddir}/src/addr2line --pretty -fiC -e testfile-inlines-lto 0x1118 0x1137 <<\EOF
+foobar(int) at /tmp/x.cpp:4:14
+ (inlined by) foo(int) at /tmp/x.cpp:22:16
+ (inlined by) fu(int) at /tmp/x.cpp:27:13
+fubar(int) at /tmp/x.cpp:10:14
+ (inlined by) bar(int) at /tmp/x.cpp:16:15
+ (inlined by) fu(int) at /tmp/x.cpp:27:24
+EOF
+
 exit 0
diff --git a/tests/testfile-inlines-lto.bz2 b/tests/testfile-inlines-lto.bz2
new file mode 100755
index 0000000000000000000000000000000000000000..7e2e2bfd481a1d56f8a396a39726c3d21c984620
GIT binary patch
literal 2838
zcmV+x3+ePiT4*^jL0KkKS*V|nQveFr|NsC0|Nr;@|NsC0{{H{}-}>)sb*9*BL-~C}
zbZ~xc|9{{KpL5=Jc+s%-JF{#}+-~*^uDU(!b?t8+xJe=i1PVNvO|(YUG}@a})5xHD
zhqRh%Y2_PJ)Owl$>TFZg^)fWk<sPF!k)RC&N1;7K)YBu>00SnPXk_w@8Udu#8Y+Gx
zRNj-++EZ$NspUOG)CP?hLqIYB007WvGyn!b$N``L000000j7Y^28aPtd8hyhpfLag
zL7)Rb00E$Bp^yy#00000(?da^00001rhzG<iJ_8dlR(u5!B0&msrsgB2c&w9s69Xf
zL7+6#(r66;&>A!Wrho&~9-wG601W^EpbZdY(<XxuXahh1XlM+EA*O%>CMKEy(V?IQ
zgA+{+0B8UJ01OjAXwcBfkP@0gO$mU3rjuxs#WdPAGB%-;AZ;d$LFxbi116aROo5;c
z4FCp!02%-Rpa24HuD~A02{EXtGvA{gtjaHwSgo#?Ye?9-MS_Tcyp^5hYW4+E;bn<I
zC@4if4wMO2E>Y!4dniDRpu{AN^C@zH%!X1yaUlzhAhL2KA%J@Y_}2i!HyH#fp)x9u
z5y{0dgi>Qjh@p-ui5?p1tqS&&h>Gyf6w1V=zpd>c->za)tu1`4pFcA#62i%_j^r@J
zrBRR?R$Gnnw^16LfCjG989E9Tl7hS<2@{WyWilX9N?d#K!^_5^IIvnDolzlBiWDsR
ztE;;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