[Bug,libdw/28220] dwarf_location_attr returns high-bit junk from .debug_addr when fetching 32-bit addresses

Message ID bug-28220-10460-sVQi61Cr0u@http.sourceware.org/bugzilla/
State Committed
Headers
Series [Bug,libdw/28220] dwarf_location_attr returns high-bit junk from .debug_addr when fetching 32-bit addresses |

Commit Message

dodji at seketeli dot org Aug. 26, 2021, 5:12 p.m. UTC
  https://sourceware.org/bugzilla/show_bug.cgi?id=28220

Mark Wielaard <mark at klomp dot org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |ASSIGNED
                 CC|                            |mark at klomp dot org
   Last reconfirmed|                            |2021-08-26
     Ever confirmed|0                           |1

--- Comment #2 from Mark Wielaard <mark at klomp dot org> ---
That was an interesting bug. The issue is the use of DW_OP_addrx which creates
a fake attribute of DW_FORM_addr pointing to the actual address. This fake
attribute also has a fake CU. We didn't set the correct address size on this
fake CU.

The following patch fixes it:

commit d18228697f750e651bf0bdf19abdaab0a4217008
Author: Mark Wielaard <mark@klomp.org>
Date:   Thu Aug 26 19:05:45 2021 +0200

    PR28220


Also on
https://code.wildebeest.org/git/user/mjw/elfutils/commit/?h=fake_cu_elf_addr_size

Thanks for the testcases I'll try to incorporate them into the testsuite.
  

Patch

diff --git a/libdw/dwarf_begin_elf.c b/libdw/dwarf_begin_elf.c
index 9e944b86..a368feb8 100644
--- a/libdw/dwarf_begin_elf.c
+++ b/libdw/dwarf_begin_elf.c
@@ -224,6 +224,23 @@  valid_p (Dwarf *result)
       result = NULL;
     }

+  /* We are setting up some "fake" CUs, which need an address size.
+     Check the ELF class to come up with something reasonable.  */
+  int elf_addr_size = 8;
+  if (result != NULL)
+    {
+      GElf_Ehdr ehdr;
+      if (gelf_getehdr (result->elf, &ehdr) == NULL)
+       {
+         Dwarf_Sig8_Hash_free (&result->sig8_hash);
+         __libdw_seterrno (DWARF_E_INVALID_ELF);
+         free (result);
+         result = NULL;
+       }
+      else if (ehdr.e_ident[EI_CLASS] == ELFCLASS32)
+       elf_addr_size = 4;
+    }
+
   /* For dwarf_location_attr () we need a "fake" CU to indicate
      where the "fake" attribute data comes from.  This is a block
      inside the .debug_loc or .debug_loclists section.  */
@@ -247,8 +264,9 @@  valid_p (Dwarf *result)
            = (result->sectiondata[IDX_debug_loc]->d_buf
               + result->sectiondata[IDX_debug_loc]->d_size);
          result->fake_loc_cu->locs = NULL;
-         result->fake_loc_cu->address_size = 0;
-         result->fake_loc_cu->version = 0;
+         result->fake_loc_cu->address_size = elf_addr_size;
+         result->fake_loc_cu->offset_size = 4;
+         result->fake_loc_cu->version = 4;
          result->fake_loc_cu->split = NULL;
        }
     }
@@ -274,8 +292,9 @@  valid_p (Dwarf *result)
            = (result->sectiondata[IDX_debug_loclists]->d_buf
               + result->sectiondata[IDX_debug_loclists]->d_size);
          result->fake_loclists_cu->locs = NULL;
-         result->fake_loclists_cu->address_size = 0;
-         result->fake_loclists_cu->version = 0;
+         result->fake_loclists_cu->address_size = elf_addr_size;
+         result->fake_loclists_cu->offset_size = 4;
+         result->fake_loclists_cu->version = 5;
          result->fake_loclists_cu->split = NULL;
        }
     }
@@ -306,8 +325,9 @@  valid_p (Dwarf *result)
            = (result->sectiondata[IDX_debug_addr]->d_buf
               + result->sectiondata[IDX_debug_addr]->d_size);
          result->fake_addr_cu->locs = NULL;
-         result->fake_addr_cu->address_size = 0;
-         result->fake_addr_cu->version = 0;
+         result->fake_addr_cu->address_size = elf_addr_size;
+         result->fake_addr_cu->offset_size = 4;
+         result->fake_addr_cu->version = 5;
          result->fake_addr_cu->split = NULL;
        }
     }