public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] <Overlapping Dwarf Compile Units with non-overlapping subranges gives incorrect line information>
@ 2020-05-15  2:43 Mitch Souders
  2020-05-28 16:51 ` [PING] " Mitch Souders
  2020-06-02 11:26 ` Tom de Vries
  0 siblings, 2 replies; 6+ messages in thread
From: Mitch Souders @ 2020-05-15  2:43 UTC (permalink / raw)
  To: gdb-patches

[-- Attachment #1: Type: text/plain, Size: 2756 bytes --]

RE: GDB Bugz 25980

CHANGELOG:

2020-05-14  Mitch Souders  <mitch@runsafesecurity>
	* symtab.c : Find correct compilation unit for regions with overlapping
	regions that contain non-overlapping subregions.
	
In cases where two compilation units have overlapping ranges (with
non-overlapping subranges), gdb gets a bit confused and reports line
positions from the wrong compilation unit.

The bugz has an attached out.dwarf as an example testcase.

main.c has a top-level range of 0x1129-0x1253.
i.c has a top-level range of 0x11e5-0x1273.
Though these two ranges overlap, none of their child subprograms
overlap. This should mean that the line numbers for specific addresses
are unambiguous.
> gdb out.dwarf --batch --ex="set trace-commands on" --command=gdb.cmd
> +symbol-file
> +add-symbol-file -readnow ~/proj/LFR/dwarf/test/out.dwarf
> add symbol table from file "/home/crzysdrs/proj/LFR/dwarf/test/out.dwarf"
> + info line *main
> Line 11 of "i.c" starts at address 0x1210 <f+20> and ends at 0x1265 <d>.
> +info line *a
> Line 3 of "main.c" starts at address 0x1129 <a> and ends at 0x1131 <a+8>.
> +info line *b
> Line 6 of "main.c" starts at address 0x1157 <b> and ends at 0x115f <b+8>.
> +info line *c
> Line 9 of "main.c" starts at address 0x1140 <c> and ends at 0x1148 <c+8>.
> +info line *d
> Line 3 of "i.c" starts at address 0x1265 <d> and ends at 0x126d <d+8>.
> +info line *e
> Line 6 of "i.c" starts at address 0x11e5 <e> and ends at 0x11ed <e+8>.
> +info line *f
> Line 9 of "i.c" starts at address 0x11fc <f> and ends at 0x1204 <f+8>.

As shown above, the line information is mistakenly found in i.c, when
it should in fact be in main.c.

(Corrected)> +info line *main
> Line 12 of "main.c" starts at address 0x1213 <main> and ends at 0x121b <main+8>.

This appears to be related to mistaken assumption in
symtab.c:find_pc_sect_compunit_symtab where in the absence of a
partial symbol table it tries to find the smallest compilation unit
where the address resides without considering the valid subranges of
the compilation unit. If the compilation unit that matched is the
smallest, all future compilation units containing that address will be
ignored.

It should also be noted that removing -readnow for adding the symbol
file does correct the output. Additionally lldb and addr2line report
the correct position.> $ addr2line -e out.dwarf 0x1213
> /home/crzysdrs/proj/LFR/dwarf/test/main.c:12

The attached patch searches through valid subranges of a compilation
in order to correctly identify the target subrange, rather than
incorrectly identify the smallest compilation unit with an enclosing
range that may or may not include the targeted subrange. In the event
there are no subranges, the behavior is the same as always.

-- 
-Mitch

[-- Attachment #2: patch --]
[-- Type: application/octet-stream, Size: 1970 bytes --]

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 4f948d57a0..43110dfdfb 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,7 @@
+2020-05-14  Mitch Souders  <mitch@runsafesecurity>
+	* symtab.c : Find correct compilation unit for regions with overlapping regions
+	that contain non-overlapping subregions.
+	
 2020-05-13  Tom Tromey  <tromey@adacore.com>
 
 	* ada-lang.c (align_value): Remove.
diff --git a/gdb/symtab.c b/gdb/symtab.c
index b765a583c4..4bbfcd66e2 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -2933,15 +2933,39 @@ find_pc_sect_compunit_symtab (CORE_ADDR pc, struct obj_section *section)
 	{
 	  const struct block *b;
 	  const struct blockvector *bv;
-
+	  const struct block *best_block = NULL;
 	  bv = COMPUNIT_BLOCKVECTOR (cust);
 	  b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
-
+	  
 	  if (BLOCK_START (b) <= pc
-	      && BLOCK_END (b) > pc
-	      && (distance == 0
-		  || BLOCK_END (b) - BLOCK_START (b) < distance))
+	      && BLOCK_END (b) > pc)
+	    {
+	      int block_start = GLOBAL_BLOCK;
+	      /* skip global region when sub-regions exist, when block_start is zero,
+		 it's effectively the old behavior.
+	      */
+	      if (BLOCKVECTOR_NBLOCKS(bv) > 1)
+		{
+		  block_start++;
+		}
+	      /* in the event that non-contiguous regions cause overlaps with other regions,
+		 we need to check within the subregions which are valid */
+	      for (int block_id = block_start; block_id < BLOCKVECTOR_NBLOCKS(bv); block_id++)
+	        {
+		  b = BLOCKVECTOR_BLOCK (bv, block_id);
+		  if (BLOCK_START (b) <= pc
+		      && BLOCK_END (b) > pc
+		      && (distance == 0
+			  || BLOCK_END (b) - BLOCK_START (b) < distance))
+		    {
+		      best_block = b;
+		      break;
+		    }
+		}
+	    }
+	  if (best_block)
 	    {
+	      b = best_block;
 	      /* For an objfile that has its functions reordered,
 		 find_pc_psymtab will find the proper partial symbol table
 		 and we simply return its corresponding symtab.  */

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2020-10-28 20:21 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-15  2:43 [PATCH] <Overlapping Dwarf Compile Units with non-overlapping subranges gives incorrect line information> Mitch Souders
2020-05-28 16:51 ` [PING] " Mitch Souders
2020-06-02 11:26 ` Tom de Vries
2020-06-03  4:18   ` Mitch Souders
2020-06-08 16:04     ` Tom de Vries
2020-10-28 20:21       ` Tom de Vries

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).