[PATCH 03/11] Introduce structure to hold DW_TAG_typedef information from DWARF info section to determine all possible defined structure

Guillaume VACHERIAS guillaume.vacherias@foss.st.com
Thu May 22 11:03:27 GMT 2025


- Define structure to hold typedef information (DW_TAG_typedef)
---
 binutils/dwarf.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++
 binutils/dwarf.h |  9 ++++++
 2 files changed, 84 insertions(+)

diff --git a/binutils/dwarf.c b/binutils/dwarf.c
index ee57b12fab4..2477a857110 100644
--- a/binutils/dwarf.c
+++ b/binutils/dwarf.c
@@ -62,6 +62,7 @@ static unsigned int alloc_num_debug_info_entries = 0;
 static debug_info *debug_information = NULL;
 
 base_type_pair *m_base_type_map = NULL;
+type_def_pair *m_type_def_map = NULL;
 /* Special value for num_debug_info_entries to indicate
    that the .debug_info section could not be loaded/parsed.  */
 #define DEBUG_INFO_UNAVAILABLE  (unsigned int) -1
@@ -2607,6 +2608,26 @@ get_or_create_base_type_elt (base_type_pair *head)
   return head->base_type;
 }
 
+static type_def_pair *
+get_or_create_typedef_pair (uint64_t offset)
+{
+  /* We use offset as key which represents the address entry of a DIE.
+     Each DIE retains several attributes.  We use offset to track current
+     DIE until we parse all of its attributes and eventually reach another
+     DIE hence a different offset.  */
+  if (m_type_def_map == NULL || m_type_def_map->offset != offset)
+    {
+      type_def_pair *ret = (type_def_pair *) xmalloc (
+	sizeof (type_def_pair));
+      ret->name = NULL;
+      ret->offset = offset;
+      ret->address = 0;
+      ret->next = m_type_def_map;
+      m_type_def_map = ret;
+    }
+  return m_type_def_map;
+}
+
 static void
 insert_element_in_map (enum dwarf_tag dw_tag,
 		       enum dwarf_attribute dw_attr,
@@ -2658,6 +2679,45 @@ insert_element_in_map (enum dwarf_tag dw_tag,
 	    }
 	}
 	break;
+      case DW_TAG_typedef:
+	{
+	  type_def_pair *head =	get_or_create_typedef_pair (offset);
+	  if (dw_attr == DW_AT_type)
+	    {
+	      if (uvalue == NULL && svalue == NULL)
+		{
+		  fprintf (stderr,
+		    _ ("insert_element_in_map: DW_TAG_typedef-DW_AT_type uvalue"
+		    " and svalue are NULL!\n"));
+		  free_mapping_info_struct ();
+		  xexit (1);
+		}
+		head->address = (svalue != NULL)
+		  ? (uint64_t) *svalue : *uvalue;
+	    }
+	  else if (dw_attr == DW_AT_name)
+	    {
+	      if (uvalue == NULL
+		  && svalue == NULL
+		  && data == NULL)
+		{
+		  fprintf (stderr,
+		    _ ("insert_element_in_map: DW_TAG_typedef-DW_AT_type "
+		    "uvalue, svalue and data are NULL!\n"));
+		  free_mapping_info_struct ();
+		  xexit (1);
+		}
+	      if (data != NULL)
+		head->name = data;
+	      else if (uvalue != NULL)
+		head->name = fetch_indirect_string (*uvalue);
+	      else
+		fprintf (stderr,
+		  _ ("insert_element_in_map: "
+		  "DW_TAG_typedef-DW_AT_type no name!\n"));
+	    }
+	}
+	break;
       default:
       break;
     }
@@ -13583,8 +13643,23 @@ free_base_type (void)
   m_base_type_map = NULL;
 }
 
+static void
+free_type_def (void)
+{
+  type_def_pair *current = m_type_def_map;
+  type_def_pair *next = NULL;
+  while (current != NULL)
+    {
+      next = current->next;
+      free (current);
+      current = next;
+    }
+  m_type_def_map = NULL;
+}
+
 void
 free_mapping_info_struct ()
 {
   free_base_type ();
+  free_type_def ();
 }
diff --git a/binutils/dwarf.h b/binutils/dwarf.h
index 6d4008083e6..55c1d79375f 100644
--- a/binutils/dwarf.h
+++ b/binutils/dwarf.h
@@ -223,6 +223,15 @@ typedef struct base_type_pair
 }
 base_type_pair;
 
+typedef struct type_def_pair
+{
+  const unsigned char *name;
+  uint64_t offset;
+  uint64_t address;
+  struct type_def_pair *next;
+}
+type_def_pair;
+
 extern unsigned int eh_addr_size;
 
 extern int do_debug_info;
-- 
2.25.1



More information about the Binutils mailing list