From 485067b2f9b5f3e061fef89aad62a264a3bf3280 Mon Sep 17 00:00:00 2001
From: nitachra <Nitika.Achra@amd.com>
Date: Mon, 20 Jan 2020 14:07:29 +0530
Subject: [PATCH] Support for DW_AT_loclists_base and DW_FORM_loclistx.

This patch handles DW_AT_loclists_base and DW_FORM_loclistx.
DW_AT_loclists_base is a new attribute added in DWARFv5 which
points to the beginning of the offset table of .debug_loclist
section. Reference to the location list (DW_FORM_loclistx) is
interpreted relative to this base. DW_FORM_loclistx is a new
form added in DWARFv5 which is used to access location list.

Tested by running the testsuite before and after the patch and there
is no increase in the number of test cases that fails. Tested with both
-gdwarf-4 and -gdwarf-5 flags. Also tested -gslit-dwarf along with
-gdwarf-4 as well as -gdwarf5 flags.

gdb/ChangeLog:

   *dwarf2read.c (cu_debug_loc_section): Added the declaration for the function.
   (read_loclist_index): New function declaration.
   (lookup_loclist_base): New function declaration.
   (read_loclist_header): New function declaration
   (dwarf2_cu): Added loclist_base and loclist_header field.
   (dwarf2_locate_dwo_sections): Handle .debug_loclist.dwo section.
   (read_full_die_1): Read the value of DW_AT_loclists_base.
   (read_attribute_reprocess): Handle DW_FORM_loclistx.
   (read_attribute_value): Handle DW_FORM_loclistx.
   (skip_one_die): Handle DW_FORM_loclistx.
   (attr_form_is_section_offset): Handle DW_FORM_loclistx.
   (read_loclist_index): Function definition.
   (lookup_loclist_base): Function definition.
   (read_loclist_header): Function definition.
   (loclist_header): New structure declaration.
---
 gdb/dwarf2read.c | 141 ++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 140 insertions(+), 1 deletion(-)

diff --git a/gdb/dwarf2read.c b/gdb/dwarf2read.c
index dfa2f91d45..9bd249ba72 100644
--- a/gdb/dwarf2read.c
+++ b/gdb/dwarf2read.c
@@ -104,6 +104,12 @@ static int dwarf2_loclist_index;
 static int dwarf2_locexpr_block_index;
 static int dwarf2_loclist_block_index;
 
+/* Size of .debug_loclist section header for 32-bit DWARF format. */
+#define LOCLIST_HEADER_SIZE32 12;
+
+/* Size of .debug_loclist section header for 64-bit DWARF format. */
+#define LOCLIST_HEADER_SIZE64 20;
+
 /* An index into a (C++) symbol name component in a symbol name as
    recorded in the mapped_index's symbol table.  For each C++ symbol
    in the symbol table, we record one entry for the start of each
@@ -373,6 +379,30 @@ struct comp_unit_head
   cu_offset type_cu_offset_in_tu;
 };
 
+/* The location list section (.debug_loclist) begins with a header,
+   which contains the following information. */
+struct loclist_header
+{
+  /* A 4-byte or 12-byte length containing the length of the
+  set of entries for this compilation unit, not including the
+  length field itself. */
+  unsigned int length;
+
+  /* A 2-byte version identifier. */
+  short version;
+
+  /* A 1-byte unsigned integer containing the size in bytes of an address on
+     the target system. */
+  unsigned char addr_size;
+
+  /* A 1-byte unsigned integer containing the size in bytes of a segment selector
+     on the target system. */
+  unsigned char segment_collector_size;
+
+  /* A 4-byte count of the number of offsets that follow the header. */
+  unsigned int offset_entry_count;
+};
+
 /* Type used for delaying computation of method physnames.
    See comments for compute_delayed_physnames.  */
 struct delayed_method_info
@@ -473,6 +503,9 @@ public:
      die_info->offset.sect_off as hash.  */
   htab_t die_hash = nullptr;
 
+  /* Header data from the location list section. */
+  struct loclist_header* loclist_header;
+
   /* Full DIEs if read in.  */
   struct die_info *dies = nullptr;
 
@@ -523,6 +556,9 @@ public:
      whether the DW_AT_ranges attribute came from the skeleton or DWO.  */
   ULONGEST ranges_base = 0;
 
+  /* The DW_AT_loclists_base attribute if present. */
+  gdb::optional<ULONGEST> loclist_base;
+
   /* When reading debug info generated by older versions of rustc, we
      have to rewrite some union types to be struct types with a
      variant part.  This rewriting must be done after the CU is fully
@@ -1705,6 +1741,14 @@ static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
 			       struct dwarf2_cu *, struct partial_symtab *);
 
+static struct dwarf2_section_info* cu_debug_loc_section (struct dwarf2_cu* cu);
+
+static CORE_ADDR read_loclist_index (struct dwarf2_cu* cu, ULONGEST loclist_index);
+
+static ULONGEST lookup_loclist_base (struct dwarf2_cu* cu);
+
+static void read_loclist_header (struct dwarf2_cu* cu, struct dwarf2_section_info* section);
+
 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
    values.  Keep the items ordered with increasing constraints compliance.  */
 enum pc_bounds_kind
@@ -9445,6 +9489,7 @@ skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
 	case DW_FORM_GNU_addr_index:
 	case DW_FORM_GNU_str_index:
 	case DW_FORM_rnglistx:
+	case DW_FORM_loclistx:
 	  info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
 	  break;
 	case DW_FORM_indirect:
@@ -12979,6 +13024,11 @@ dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
       dwo_sections->loc.s.section = sectp;
       dwo_sections->loc.size = bfd_section_size (sectp);
     }
+  else if (section_is_p (sectp->name, &names->loclists_dwo))
+    {
+      dwo_sections->loclists.s.section = sectp;
+      dwo_sections->loclists.size = bfd_section_size (sectp);
+    }
   else if (section_is_p (sectp->name, &names->macinfo_dwo))
     {
       dwo_sections->macinfo.s.section = sectp;
@@ -18453,6 +18503,9 @@ read_full_die_1 (const struct die_reader_specs *reader,
   struct attribute *attr = dwarf2_attr_no_follow (die, DW_AT_str_offsets_base);
   if (attr != nullptr)
     cu->str_offsets_base = DW_UNSND (attr);
+  attr = dwarf2_attr_no_follow (die, DW_AT_loclists_base);
+  if (attr)
+    cu->loclist_base = DW_UNSND (attr);
 
   auto maybe_addr_base = lookup_addr_base(die);
   if (maybe_addr_base.has_value ())
@@ -19409,6 +19462,81 @@ partial_die_info::fixup (struct dwarf2_cu *cu)
   fixup_called = 1;
 }
 
+void
+read_loclist_header (struct dwarf2_cu* cu, struct dwarf2_section_info* section)
+{
+  unsigned int bytes_read;
+  bfd* abfd = get_section_bfd_owner (section);
+  const gdb_byte* info_ptr = section->buffer;
+  cu->loclist_header = new loclist_header();
+  cu->loclist_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
+  info_ptr += bytes_read;
+  cu->loclist_header->version = read_2_bytes (abfd, info_ptr);
+  info_ptr += 2;
+  cu->loclist_header->addr_size = read_1_byte (abfd, info_ptr);
+  info_ptr += 1;
+  cu->loclist_header->segment_collector_size = read_1_byte (abfd, info_ptr);
+  info_ptr += 1;
+  cu->loclist_header->offset_entry_count = read_4_bytes (abfd, info_ptr);
+}
+
+
+ULONGEST
+lookup_loclist_base (struct dwarf2_cu* cu)
+{
+  /* For the .dwo unit, the loclist_base points to the first offset following
+     the header. The header consists of the following entities-
+     1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64 bit format)
+     2. version (2 bytes)
+     3. address size (1 byte)
+     4. segment selector size (1 byte)
+     5. offset entry count (4 bytes)
+     These sizes are derived as per the DWARFv5 standard. */
+  if (cu->dwo_unit)
+  {
+    if (cu->header.initial_length_size == 4)
+      return LOCLIST_HEADER_SIZE32;
+    return LOCLIST_HEADER_SIZE64;
+  }
+  return *cu->loclist_base;
+}
+
+/* Given a DW_FORM_loclistx value loclist_index, fetch the offset from the array
+   of offsets in the .debug_loclists section. */
+CORE_ADDR
+read_loclist_index (struct dwarf2_cu* cu, ULONGEST loclist_index)
+{
+  struct dwarf2_per_objfile* dwarf2_per_objfile
+	= cu->per_cu->dwarf2_per_objfile;
+  struct objfile* objfile = dwarf2_per_objfile->objfile;
+  bfd* abfd = objfile->obfd;
+  ULONGEST loclist_base = lookup_loclist_base (cu);
+  const gdb_byte* info_ptr;
+  struct dwarf2_section_info* section = cu_debug_loc_section (cu);
+  dwarf2_read_section (objfile, section);
+  if (section->buffer == NULL)
+    error(_("DW_FORM_loclistx used without .debug_loclist section [in module %s]"),
+	objfile_name (objfile));
+  read_loclist_header (cu, section);
+  if (loclist_index >= cu->loclist_header->offset_entry_count)
+    error(_("DW_FORM_loclistx pointing outside of "
+	".debug_loclist offset array [in module %s]"),
+	objfile_name (objfile));
+  if (loclist_base + loclist_index * cu->header.offset_size
+	>= section->size)
+    error(_("DW_FORM_loclistx pointing outside of "
+	".debug_loclist section [in module %s]"),
+	objfile_name (objfile));
+  info_ptr = (section->buffer + loclist_base +
+	loclist_index * cu->header.offset_size);
+  delete cu->loclist_header;
+  cu->loclist_header = NULL;
+  if (cu->header.offset_size == 4)
+    return bfd_get_32 (abfd, info_ptr) + loclist_base;
+  else
+    return bfd_get_64 (abfd, info_ptr) + loclist_base;
+}
+
 /* Process the attributes that had to be skipped in the first round. These
    attributes are the ones that need str_offsets_base or addr_base attributes.
    They could not have been processed in the first round, because at the time
@@ -19423,6 +19551,9 @@ void read_attribute_reprocess (const struct die_reader_specs *reader,
       case DW_FORM_GNU_addr_index:
         DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
         break;
+      case DW_FORM_loclistx:
+	 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
+	 break;
       case DW_FORM_strx:
       case DW_FORM_strx1:
       case DW_FORM_strx2:
@@ -19526,6 +19657,13 @@ read_attribute_value (const struct die_reader_specs *reader,
       DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
       info_ptr += bytes_read;
       break;
+    case DW_FORM_loclistx:
+    {
+      *need_reprocess = true;
+      DW_UNSND(attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
+      info_ptr += bytes_read;
+    }
+      break;
     case DW_FORM_string:
       DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
       DW_STRING_IS_CANONICAL (attr) = 0;
@@ -25407,7 +25545,8 @@ attr_form_is_section_offset (const struct attribute *attr)
 {
   return (attr->form == DW_FORM_data4
           || attr->form == DW_FORM_data8
-	  || attr->form == DW_FORM_sec_offset);
+	  || attr->form == DW_FORM_sec_offset
+	  || attr->form == DW_FORM_loclistx);
 }
 
 /* Return non-zero if ATTR's value falls in the 'constant' class, or
-- 
2.17.1

