]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
ada: Fix incorrect tracebacks on Windows
authorSebastian Poeplau <poeplau@adacore.com>
Wed, 7 Aug 2024 09:21:25 +0000 (11:21 +0200)
committerMarc Poulhiès <dkm@gcc.gnu.org>
Fri, 23 Aug 2024 08:51:05 +0000 (10:51 +0200)
PECOFF symbols don't have a size attached to them. The symbol size that
System.Object_Reader.Read_Symbol guesses to make up for the lack of
information can be wrong when the symbol table doesn't match the
algorithm's expectations; in particular that's the case when function
symbols aren't sorted by address.

To avoid incorrect tracebacks caused by wrong symbol size guesses, don't
use the symbol size for PECOFF files when producing a traceback and
instead pick the symbol with the highest address lower than the target
address.

gcc/ada/

* libgnat/s-dwalin.adb (Symbolic_Address): Ignore symbol size in
address-to-symbol translation for PECOFF files.

gcc/ada/libgnat/s-dwalin.adb

index 46a7d61e78db27907334b96ac01e196152d1715e..028a55d1f20b3d665138ac5f0926fe1952ac3b81 100644 (file)
@@ -1753,6 +1753,7 @@ package body System.Dwarf_Lines is
       Success      : Boolean;
       Done         : Boolean;
       S            : Object_Symbol;
+      Closest_S    : Object_Symbol := Null_Symbol;
 
    begin
       --  Initialize result
@@ -1801,7 +1802,22 @@ package body System.Dwarf_Lines is
       else
          S := First_Symbol (C.Obj.all);
          while S /= Null_Symbol loop
-            if Spans (S, Addr_Int) then
+            if Format (C.Obj.all) = PECOFF
+              or else Format (C.Obj.all) = PECOFF_PLUS
+            then
+               --  Don't use the size of symbols from PECOFF files; it's
+               --  just a guess and can be unreliable. Instead, iterate
+               --  over the entire symbol table and use the symbol with the
+               --  highest address lower than Addr_Int.
+
+               if Closest_S = Null_Symbol
+                 or else (Closest_S.Value < S.Value
+                   and then S.Value <= Addr_Int)
+               then
+                  Closest_S := S;
+               end if;
+
+            elsif Spans (S, Addr_Int) then
                Subprg_Name := Object_Reader.Name (C.Obj.all, S);
                exit;
             end if;
@@ -1809,6 +1825,14 @@ package body System.Dwarf_Lines is
             S := Next_Symbol (C.Obj.all, S);
          end loop;
 
+         if (Format (C.Obj.all) = PECOFF
+             or else Format (C.Obj.all) = PECOFF_PLUS)
+           and then Closest_S /= Null_Symbol
+         then
+            S := Closest_S;     --  for consistency with non-PECOFF
+            Subprg_Name := Object_Reader.Name (C.Obj.all, S);
+         end if;
+
          --  Search address in aranges table
 
          Aranges_Lookup (C, Addr, Info_Offset, Success);