15867d
Upstream commits since 0.172:
15867d
15867d
  b4dced3 readelf: While printing .debug_loc make sure that next_off doesn't overflow.
15867d
  bb11e36 libdw: Make __libdw_dieabbrev more robust on failure.
15867d
  3647b5b readelf: Make sure print_form_data always consumes DW_FORM_strx[1234] data.
15867d
  eaaf908 readelf: Check there are at least 4 bytes available for DWARF_FORM_block4.
15867d
  0d93f49 libdw, readelf: Don't handle DW_FORM_data16 as expression block/location.
15867d
  9495c26 libdw: aggregate_size check NULL result from get_type.
15867d
  e636112 libdw: dwarf_peel_type break long chains/cycles.
15867d
  5956b8a libdw: Break dwarf_aggregate_size recursion because of type cycles.
15867d
  ca0d831 libelf: Don't return unaligned data returned from elf_getdata[_rawchunk].
15867d
15867d
diff --git a/libdw/dwarf_aggregate_size.c b/libdw/dwarf_aggregate_size.c
15867d
index 6e50185..75105e4 100644
15867d
--- a/libdw/dwarf_aggregate_size.c
15867d
+++ b/libdw/dwarf_aggregate_size.c
15867d
@@ -46,13 +46,17 @@ get_type (Dwarf_Die *die, Dwarf_Attribute *attr_mem, Dwarf_Die *type_mem)
15867d
   return type;
15867d
 }
15867d
 
15867d
+static int aggregate_size (Dwarf_Die *die, Dwarf_Word *size,
15867d
+			   Dwarf_Die *type_mem, int depth);
15867d
+
15867d
 static int
15867d
 array_size (Dwarf_Die *die, Dwarf_Word *size,
15867d
-	    Dwarf_Attribute *attr_mem, Dwarf_Die *type_mem)
15867d
+	    Dwarf_Attribute *attr_mem, int depth)
15867d
 {
15867d
   Dwarf_Word eltsize;
15867d
-  if (INTUSE(dwarf_aggregate_size) (get_type (die, attr_mem, type_mem),
15867d
-				    &eltsize) != 0)
15867d
+  Dwarf_Die type_mem, aggregate_type_mem;
15867d
+  if (aggregate_size (get_type (die, attr_mem, &type_mem), &eltsize,
15867d
+		      &aggregate_type_mem, depth) != 0)
15867d
       return -1;
15867d
 
15867d
   /* An array can have DW_TAG_subrange_type or DW_TAG_enumeration_type
15867d
@@ -167,21 +171,30 @@ array_size (Dwarf_Die *die, Dwarf_Word *size,
15867d
 }
15867d
 
15867d
 static int
15867d
-aggregate_size (Dwarf_Die *die, Dwarf_Word *size, Dwarf_Die *type_mem)
15867d
+aggregate_size (Dwarf_Die *die, Dwarf_Word *size,
15867d
+		Dwarf_Die *type_mem, int depth)
15867d
 {
15867d
   Dwarf_Attribute attr_mem;
15867d
 
15867d
+/* Arrays of arrays of subrange types of arrays... Don't recurse too deep.  */
15867d
+#define MAX_DEPTH 256
15867d
+  if (die == NULL || depth++ >= MAX_DEPTH)
15867d
+    return -1;
15867d
+
15867d
   if (INTUSE(dwarf_attr_integrate) (die, DW_AT_byte_size, &attr_mem) != NULL)
15867d
     return INTUSE(dwarf_formudata) (&attr_mem, size);
15867d
 
15867d
   switch (INTUSE(dwarf_tag) (die))
15867d
     {
15867d
     case DW_TAG_subrange_type:
15867d
-      return aggregate_size (get_type (die, &attr_mem, type_mem),
15867d
-			     size, type_mem); /* Tail call.  */
15867d
+      {
15867d
+	Dwarf_Die aggregate_type_mem;
15867d
+	return aggregate_size (get_type (die, &attr_mem, type_mem),
15867d
+			       size, &aggregate_type_mem, depth);
15867d
+      }
15867d
 
15867d
     case DW_TAG_array_type:
15867d
-      return array_size (die, size, &attr_mem, type_mem);
15867d
+      return array_size (die, size, &attr_mem, depth);
15867d
 
15867d
     /* Assume references and pointers have pointer size if not given an
15867d
        explicit DW_AT_byte_size.  */
15867d
@@ -204,7 +217,7 @@ dwarf_aggregate_size (Dwarf_Die *die, Dwarf_Word *size)
15867d
   if (INTUSE (dwarf_peel_type) (die, &die_mem) != 0)
15867d
     return -1;
15867d
 
15867d
-  return aggregate_size (&die_mem, size, &type_mem);
15867d
+  return aggregate_size (&die_mem, size, &type_mem, 0);
15867d
 }
15867d
 INTDEF (dwarf_aggregate_size)
15867d
 OLD_VERSION (dwarf_aggregate_size, ELFUTILS_0.144)
15867d
diff --git a/libdw/dwarf_getlocation.c b/libdw/dwarf_getlocation.c
15867d
index 7f294fe..fc59a2a 100644
15867d
--- a/libdw/dwarf_getlocation.c
15867d
+++ b/libdw/dwarf_getlocation.c
15867d
@@ -174,6 +174,8 @@ check_constant_offset (Dwarf_Attribute *attr,
15867d
     default:
15867d
       return 1;
15867d
 
15867d
+      /* Note, we don't regard DW_FORM_data16 as a constant form,
15867d
+	 even though technically it is according to the standard.  */
15867d
     case DW_FORM_data1:
15867d
     case DW_FORM_data2:
15867d
     case DW_FORM_data4:
15867d
@@ -665,7 +667,13 @@ dwarf_getlocation (Dwarf_Attribute *attr, Dwarf_Op **llbuf, size_t *listlen)
15867d
   if (result != 1)
15867d
     return result;
15867d
 
15867d
-  /* If it has a block form, it's a single location expression.  */
15867d
+  /* If it has a block form, it's a single location expression.
15867d
+     Except for DW_FORM_data16, which is a 128bit constant.  */
15867d
+  if (attr->form == DW_FORM_data16)
15867d
+    {
15867d
+      __libdw_seterrno (DWARF_E_NO_BLOCK);
15867d
+      return -1;
15867d
+    }
15867d
   Dwarf_Block block;
15867d
   if (INTUSE(dwarf_formblock) (attr, &block) != 0)
15867d
     return -1;
15867d
@@ -863,9 +871,11 @@ dwarf_getlocation_addr (Dwarf_Attribute *attr, Dwarf_Addr address,
15867d
   if (llbufs == NULL)
15867d
     maxlocs = SIZE_MAX;
15867d
 
15867d
-  /* If it has a block form, it's a single location expression.  */
15867d
+  /* If it has a block form, it's a single location expression.
15867d
+     Except for DW_FORM_data16, which is a 128bit constant.  */
15867d
   Dwarf_Block block;
15867d
-  if (INTUSE(dwarf_formblock) (attr, &block) == 0)
15867d
+  if (attr->form != DW_FORM_data16
15867d
+      && INTUSE(dwarf_formblock) (attr, &block) == 0)
15867d
     {
15867d
       if (maxlocs == 0)
15867d
 	return 0;
15867d
@@ -876,11 +886,14 @@ dwarf_getlocation_addr (Dwarf_Attribute *attr, Dwarf_Addr address,
15867d
       return listlens[0] == 0 ? 0 : 1;
15867d
     }
15867d
 
15867d
-  int error = INTUSE(dwarf_errno) ();
15867d
-  if (unlikely (error != DWARF_E_NO_BLOCK))
15867d
+  if (attr->form != DW_FORM_data16)
15867d
     {
15867d
-      __libdw_seterrno (error);
15867d
-      return -1;
15867d
+      int error = INTUSE(dwarf_errno) ();
15867d
+      if (unlikely (error != DWARF_E_NO_BLOCK))
15867d
+	{
15867d
+	  __libdw_seterrno (error);
15867d
+	  return -1;
15867d
+	}
15867d
     }
15867d
 
15867d
   int result = check_constant_offset (attr, &llbufs[0], &listlens[0]);
15867d
@@ -938,9 +951,11 @@ dwarf_getlocations (Dwarf_Attribute *attr, ptrdiff_t offset, Dwarf_Addr *basep,
15867d
 
15867d
   if (offset == 0)
15867d
     {
15867d
-      /* If it has a block form, it's a single location expression.  */
15867d
+      /* If it has a block form, it's a single location expression.
15867d
+	 Except for DW_FORM_data16, which is a 128bit constant.  */
15867d
       Dwarf_Block block;
15867d
-      if (INTUSE(dwarf_formblock) (attr, &block) == 0)
15867d
+      if (attr->form != DW_FORM_data16
15867d
+	  && INTUSE(dwarf_formblock) (attr, &block) == 0)
15867d
 	{
15867d
 	  if (getlocation (attr->cu, &block, expr, exprlen,
15867d
 			   cu_sec_idx (attr->cu)) != 0)
15867d
@@ -952,11 +967,14 @@ dwarf_getlocations (Dwarf_Attribute *attr, ptrdiff_t offset, Dwarf_Addr *basep,
15867d
 	  return 1;
15867d
 	}
15867d
 
15867d
-      int error = INTUSE(dwarf_errno) ();
15867d
-      if (unlikely (error != DWARF_E_NO_BLOCK))
15867d
+      if (attr->form != DW_FORM_data16)
15867d
 	{
15867d
-	  __libdw_seterrno (error);
15867d
-	  return -1;
15867d
+	  int error = INTUSE(dwarf_errno) ();
15867d
+	  if (unlikely (error != DWARF_E_NO_BLOCK))
15867d
+	    {
15867d
+	      __libdw_seterrno (error);
15867d
+	      return -1;
15867d
+	    }
15867d
 	}
15867d
 
15867d
       int result = check_constant_offset (attr, expr, exprlen);
15867d
diff --git a/libdw/dwarf_peel_type.c b/libdw/dwarf_peel_type.c
15867d
index 6bbfd42..59fc6f1 100644
15867d
--- a/libdw/dwarf_peel_type.c
15867d
+++ b/libdw/dwarf_peel_type.c
15867d
@@ -46,14 +46,19 @@ dwarf_peel_type (Dwarf_Die *die, Dwarf_Die *result)
15867d
 
15867d
   *result = *die;
15867d
   tag = INTUSE (dwarf_tag) (result);
15867d
-  while (tag == DW_TAG_typedef
15867d
-	 || tag == DW_TAG_const_type
15867d
-	 || tag == DW_TAG_volatile_type
15867d
-	 || tag == DW_TAG_restrict_type
15867d
-	 || tag == DW_TAG_atomic_type
15867d
-	 || tag == DW_TAG_immutable_type
15867d
-	 || tag == DW_TAG_packed_type
15867d
-	 || tag == DW_TAG_shared_type)
15867d
+
15867d
+/* Stack 8 of all these modifiers, after that it gets silly.  */
15867d
+#define MAX_DEPTH (8 * 8)
15867d
+  int max_depth = MAX_DEPTH;
15867d
+  while ((tag == DW_TAG_typedef
15867d
+	  || tag == DW_TAG_const_type
15867d
+	  || tag == DW_TAG_volatile_type
15867d
+	  || tag == DW_TAG_restrict_type
15867d
+	  || tag == DW_TAG_atomic_type
15867d
+	  || tag == DW_TAG_immutable_type
15867d
+	  || tag == DW_TAG_packed_type
15867d
+	  || tag == DW_TAG_shared_type)
15867d
+	&& max_depth-- > 0)
15867d
     {
15867d
       Dwarf_Attribute attr_mem;
15867d
       Dwarf_Attribute *attr = INTUSE (dwarf_attr_integrate) (result, DW_AT_type,
15867d
@@ -67,7 +72,7 @@ dwarf_peel_type (Dwarf_Die *die, Dwarf_Die *result)
15867d
       tag = INTUSE (dwarf_tag) (result);
15867d
     }
15867d
 
15867d
-  if (tag == DW_TAG_invalid)
15867d
+  if (tag == DW_TAG_invalid || max_depth <= 0)
15867d
     return -1;
15867d
 
15867d
   return 0;
15867d
diff --git a/libdw/libdwP.h b/libdw/libdwP.h
15867d
index 3d8e145..eebb7d1 100644
15867d
--- a/libdw/libdwP.h
15867d
+++ b/libdw/libdwP.h
15867d
@@ -653,8 +653,9 @@ __libdw_dieabbrev (Dwarf_Die *die, const unsigned char **readp)
15867d
       /* Get the abbreviation code.  */
15867d
       unsigned int code;
15867d
       const unsigned char *addr = die->addr;
15867d
-      if (die->cu == NULL || addr >= (const unsigned char *) die->cu->endp)
15867d
-	return DWARF_END_ABBREV;
15867d
+      if (unlikely (die->cu == NULL
15867d
+		    || addr >= (const unsigned char *) die->cu->endp))
15867d
+	return die->abbrev = DWARF_END_ABBREV;
15867d
       get_uleb128 (code, addr, die->cu->endp);
15867d
       if (readp != NULL)
15867d
 	*readp = addr;
15867d
diff --git a/libdw/memory-access.h b/libdw/memory-access.h
15867d
index 22918cb..a39ad6d 100644
15867d
--- a/libdw/memory-access.h
15867d
+++ b/libdw/memory-access.h
15867d
@@ -362,6 +362,11 @@ read_3ubyte_unaligned (Dwarf *dbg, const unsigned char *p)
15867d
 }
15867d
 
15867d
 
15867d
+#define read_3ubyte_unaligned_inc(Dbg, Addr) \
15867d
+  ({ uint32_t t_ = read_2ubyte_unaligned (Dbg, Addr);			      \
15867d
+     Addr = (__typeof (Addr)) (((uintptr_t) (Addr)) + 3);		      \
15867d
+     t_; })
15867d
+
15867d
 #define read_addr_unaligned_inc(Nbytes, Dbg, Addr)			\
15867d
   (assert ((Nbytes) == 4 || (Nbytes) == 8),				\
15867d
     ((Nbytes) == 4 ? read_4ubyte_unaligned_inc (Dbg, Addr)		\
15867d
diff --git a/libelf/elf_getdata.c b/libelf/elf_getdata.c
15867d
index 97c503b..278dfa8 100644
15867d
--- a/libelf/elf_getdata.c
15867d
+++ b/libelf/elf_getdata.c
15867d
@@ -76,7 +76,6 @@ static const Elf_Type shtype_map[EV_NUM - 1][TYPEIDX (SHT_HISUNW) + 1] =
15867d
     }
15867d
   };
15867d
 
15867d
-#if !ALLOW_UNALIGNED
15867d
 /* Associate libelf types with their internal alignment requirements.  */
15867d
 const uint_fast8_t __libelf_type_aligns[EV_NUM - 1][ELFCLASSNUM - 1][ELF_T_NUM] =
15867d
   {
15867d
@@ -115,7 +114,6 @@ const uint_fast8_t __libelf_type_aligns[EV_NUM - 1][ELFCLASSNUM - 1][ELF_T_NUM]
15867d
     }
15867d
 # undef TYPE_ALIGNS
15867d
   };
15867d
-#endif
15867d
 
15867d
 
15867d
 Elf_Type
15867d
@@ -173,8 +171,7 @@ convert_data (Elf_Scn *scn, int version __attribute__ ((unused)), int eclass,
15867d
       /* Make sure the source is correctly aligned for the conversion
15867d
 	 function to directly access the data elements.  */
15867d
       char *rawdata_source;
15867d
-      if (ALLOW_UNALIGNED ||
15867d
-	  ((((size_t) (char *) scn->rawdata_base)) & (align - 1)) == 0)
15867d
+      if (((((size_t) (char *) scn->rawdata_base)) & (align - 1)) == 0)
15867d
 	rawdata_source = scn->rawdata_base;
15867d
       else
15867d
 	{
15867d
diff --git a/libelf/elf_getdata_rawchunk.c b/libelf/elf_getdata_rawchunk.c
15867d
index 31b2fe7..d0c0b75 100644
15867d
--- a/libelf/elf_getdata_rawchunk.c
15867d
+++ b/libelf/elf_getdata_rawchunk.c
15867d
@@ -80,8 +80,7 @@ elf_getdata_rawchunk (Elf *elf, off_t offset, size_t size, Elf_Type type)
15867d
     {
15867d
     /* If the file is mmap'ed we can use it directly, if aligned for type.  */
15867d
       char *rawdata = elf->map_address + elf->start_offset + offset;
15867d
-      if (ALLOW_UNALIGNED ||
15867d
-	  ((uintptr_t) rawdata & (align - 1)) == 0)
15867d
+      if (((uintptr_t) rawdata & (align - 1)) == 0)
15867d
 	rawchunk = rawdata;
15867d
       else
15867d
 	{
15867d
diff --git a/libelf/libelfP.h b/libelf/libelfP.h
15867d
index ca805ac..ed216c8 100644
15867d
--- a/libelf/libelfP.h
15867d
+++ b/libelf/libelfP.h
15867d
@@ -443,15 +443,11 @@ extern int __libelf_version_initialized attribute_hidden;
15867d
 # define LIBELF_EV_IDX	(__libelf_version - 1)
15867d
 #endif
15867d
 
15867d
-#if !ALLOW_UNALIGNED
15867d
 /* Array with alignment requirements of the internal types indexed by ELF
15867d
    version, binary class, and type. */
15867d
 extern const uint_fast8_t __libelf_type_aligns[EV_NUM - 1][ELFCLASSNUM - 1][ELF_T_NUM] attribute_hidden;
15867d
 # define __libelf_type_align(class, type)	\
15867d
     (__libelf_type_aligns[LIBELF_EV_IDX][class - 1][type] ?: 1)
15867d
-#else
15867d
-# define __libelf_type_align(class, type)	1
15867d
-#endif
15867d
 
15867d
 /* Given an Elf handle and a section type returns the Elf_Data d_type.
15867d
    Should not be called when SHF_COMPRESSED is set, the d_type should
15867d
diff --git a/src/readelf.c b/src/readelf.c
15867d
index f185897..313f940 100644
15867d
--- a/src/readelf.c
15867d
+++ b/src/readelf.c
15867d
@@ -7403,11 +7403,16 @@ attr_callback (Dwarf_Attribute *attrp, void *arg)
15867d
 	case DW_AT_GNU_call_site_data_value:
15867d
 	case DW_AT_GNU_call_site_target:
15867d
 	case DW_AT_GNU_call_site_target_clobbered:
15867d
-	  putchar ('\n');
15867d
-	  print_ops (cbargs->dwflmod, cbargs->dbg,
15867d
-		     12 + level * 2, 12 + level * 2,
15867d
-		     cbargs->version, cbargs->addrsize, cbargs->offset_size,
15867d
-		     attrp->cu, block.length, block.data);
15867d
+	  if (form != DW_FORM_data16)
15867d
+	    {
15867d
+	      putchar ('\n');
15867d
+	      print_ops (cbargs->dwflmod, cbargs->dbg,
15867d
+			 12 + level * 2, 12 + level * 2,
15867d
+			 cbargs->version, cbargs->addrsize, cbargs->offset_size,
15867d
+			 attrp->cu, block.length, block.data);
15867d
+	    }
15867d
+	  else
15867d
+	    print_block (block.length, block.data);
15867d
 	  break;
15867d
 	}
15867d
       break;
15867d
@@ -7907,7 +7912,7 @@ print_form_data (Dwarf *dbg, int form, const unsigned char *readp,
15867d
       break;
15867d
 
15867d
     case DW_FORM_block4:
15867d
-      if (readendp - readp < 2)
15867d
+      if (readendp - readp < 4)
15867d
 	goto invalid_data;
15867d
       val = read_4ubyte_unaligned_inc (dbg, readp);
15867d
       if ((size_t) (readendp - readp) < val)
15867d
@@ -7994,9 +7999,9 @@ print_form_data (Dwarf *dbg, int form, const unsigned char *readp,
15867d
 	    {
15867d
 	      Dwarf_Off idx;
15867d
 	      if (offset_len == 8)
15867d
-		idx = read_8ubyte_unaligned_inc (dbg, strreadp);
15867d
+		idx = read_8ubyte_unaligned (dbg, strreadp);
15867d
 	      else
15867d
-		idx = read_4ubyte_unaligned_inc (dbg, strreadp);
15867d
+		idx = read_4ubyte_unaligned (dbg, strreadp);
15867d
 
15867d
 	      data = dbg->sectiondata[IDX_debug_str];
15867d
 	      if (data == NULL || idx >= data->d_size
15867d
@@ -8013,25 +8018,25 @@ print_form_data (Dwarf *dbg, int form, const unsigned char *readp,
15867d
     case DW_FORM_strx1:
15867d
       if (readendp - readp < 1)
15867d
 	goto invalid_data;
15867d
-      val = *readp;
15867d
+      val = *readp++;
15867d
       goto strx_val;
15867d
 
15867d
     case DW_FORM_strx2:
15867d
       if (readendp - readp < 2)
15867d
 	goto invalid_data;
15867d
-      val = read_2ubyte_unaligned (dbg, readp);
15867d
+      val = read_2ubyte_unaligned_inc (dbg, readp);
15867d
       goto strx_val;
15867d
 
15867d
     case DW_FORM_strx3:
15867d
       if (readendp - readp < 3)
15867d
 	goto invalid_data;
15867d
-      val = read_3ubyte_unaligned (dbg, readp);
15867d
+      val = read_3ubyte_unaligned_inc (dbg, readp);
15867d
       goto strx_val;
15867d
 
15867d
     case DW_FORM_strx4:
15867d
       if (readendp - readp < 4)
15867d
 	goto invalid_data;
15867d
-      val = read_4ubyte_unaligned (dbg, readp);
15867d
+      val = read_4ubyte_unaligned_inc (dbg, readp);
15867d
       goto strx_val;
15867d
 
15867d
     default:
15867d
@@ -9230,7 +9235,9 @@ print_debug_loc_section (Dwfl_Module *dwflmod,
15867d
 						    listptr_idx);
15867d
 	  const unsigned char *locp = readp;
15867d
 	  const unsigned char *locendp;
15867d
-	  if (next_off == 0)
15867d
+	  if (next_off == 0
15867d
+	      || next_off > (size_t) (endp
15867d
+				      - (const unsigned char *) data->d_buf))
15867d
 	    locendp = endp;
15867d
 	  else
15867d
 	    locendp = (const unsigned char *) data->d_buf + next_off;