[PATCH 10/11] Display type, size and address of all existing global variables

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


- Implemented function display_var_type() to print variable information held in
  the structure var_t.
---
 binutils/dwarf.c   | 132 +++++++++++++++++++++++++++++++++++++++++++++
 binutils/dwarf.h   |   1 +
 binutils/objdump.c |   2 +
 3 files changed, 135 insertions(+)

diff --git a/binutils/dwarf.c b/binutils/dwarf.c
index 5df2d02962b..0586d4a5eb4 100644
--- a/binutils/dwarf.c
+++ b/binutils/dwarf.c
@@ -13952,6 +13952,138 @@ struct dwarf_section_display debug_displays[] =
 /* A static assertion.  */
 extern int debug_displays_assert[ARRAY_SIZE (debug_displays) == max ? 1 : -1];
 
+static field*
+reverse_field (field *head)
+{
+  field *prev = NULL;
+  field *current = head;
+  field *next = NULL;
+
+  while (current != NULL)
+    {
+      next = current->next;
+      current->next = prev;
+      prev = current;
+      current = next;
+    }
+  return prev;
+}
+
+void
+display_var_type (void)
+{
+  if (m_var_map == NULL)
+    return;
+
+  variable_pair *i = m_var_map;
+  while (i != NULL)
+    {
+      variable *var = i->var;
+      printf ("0x%lx 0x%lx %s (%s)\n",
+	      var->location, var->location + var->size,
+	      var->name, var->type_name);
+
+      if (var->array_type != NULL)
+	{
+	  for (uint64_t j = 0; j < var->array_type->counts; j++)
+	    {
+	      printf ("\t0x%lx %s[%ld]\n",
+	       var->location + (var->size / var->array_type->counts * j),
+	       var->name, j);
+
+	  if (var->struct_type != NULL)
+	    {
+	      printf ("\t%s {\n", var->struct_type->name);
+	      var->struct_type->fields = reverse_field (
+		var->struct_type->fields);
+	      field *i_field = var->struct_type->fields;
+	      while (i_field != NULL)
+		{
+		  printf ("\t\t0x%lx %s.%s\n",
+		    var->location + (var->size / var->array_type->counts * j)
+		    + i_field->member_location,
+		    var->struct_type->name, i_field->name);
+		  i_field = i_field->next;
+		}
+	      var->struct_type->fields = reverse_field (
+		var->struct_type->fields);
+	      printf ("\t}\n");
+	    }
+	  if (var->union_type != NULL)
+	    {
+	      printf ("\tunion type {\n");
+	      var->union_type->fields = reverse_field (var->union_type->fields);
+	      field *i_field = var->union_type->fields;
+	      while (i_field != NULL)
+		{
+		  printf ("\t\t0x%lx %s\n",
+		    var->location + (var->size / var->array_type->counts * j)
+		    + i_field->member_location, i_field->name);
+		  i_field = i_field->next;
+		}
+	      var->union_type->fields = reverse_field (var->union_type->fields);
+	      printf ("\t}\n");
+	    }
+	  if (var->enum_type != NULL)
+	    {
+	      printf ("\tenumeration type {\n");
+	      enum_constant *i_enum = var->enum_type;
+	      while (i_enum != NULL)
+		{
+		  printf ("\t\t0x%lx %s %ld\n",
+		    var->location + (var->size / var->array_type->counts * j),
+		    i_enum->name, i_enum->value);
+		  i_enum = i_enum->next;
+		}
+	      printf ("\t}\n");
+	    }
+	    }
+	}
+      else if (var->struct_type != NULL)
+	{
+	  printf ("\t%s {\n", var->struct_type->name);
+	  var->struct_type->fields = reverse_field (var->struct_type->fields);
+	  field *i_field = var->struct_type->fields;
+	  while (i_field != NULL)
+	    {
+	      printf ("\t\t0x%lx %s.%s\n",
+		var->location + i_field->member_location,
+		var->struct_type->name, i_field->name);
+	      i_field = i_field->next;
+	    }
+	  var->struct_type->fields = reverse_field (var->struct_type->fields);
+	  printf ("\t}\n");
+	}
+      else if (var->union_type != NULL)
+	{
+	  printf ("\tunion type {\n");
+	  var->union_type->fields = reverse_field (var->union_type->fields);
+	  field *i_field = var->union_type->fields;
+	  while (i_field != NULL)
+	    {
+	      printf ("\t\t0x%lx %s\n",
+		var->location + i_field->member_location, i_field->name);
+	      i_field = i_field->next;
+	    }
+	  var->union_type->fields = reverse_field (var->union_type->fields);
+	  printf ("\t}\n");
+	}
+      else if (var->enum_type != NULL)
+	{
+	  printf ("\tenumeration type {\n");
+	  enum_constant *i_enum = var->enum_type;
+	  while (i_enum != NULL)
+	    {
+	      printf ("\t\t0x%lx %s %ld\n",
+		var->location, i_enum->name, i_enum->value);
+	      i_enum = i_enum->next;
+	    }
+	  printf ("\t}\n");
+	}
+      i = i->next;
+   }
+}
+
 bool
 collect_memory_mapping_info_base (struct dwarf_section *section, void *file)
 {
diff --git a/binutils/dwarf.h b/binutils/dwarf.h
index 90ad806a649..a517204d76d 100644
--- a/binutils/dwarf.h
+++ b/binutils/dwarf.h
@@ -369,6 +369,7 @@ extern bool load_separate_debug_files (void *, const char *);
 extern bool collect_memory_mapping_info_base (struct dwarf_section *, void *);
 extern bool resolve_and_display_mapping_info (struct dwarf_section *, void *);
 extern void free_mapping_info_struct (void);
+extern void display_var_type (void);
 extern void close_debug_file (void *);
 extern void *open_debug_file (const char *);
 
diff --git a/binutils/objdump.c b/binutils/objdump.c
index c2c2f65cc2b..14be8b402db 100644
--- a/binutils/objdump.c
+++ b/binutils/objdump.c
@@ -4616,6 +4616,8 @@ dump_map_file_info (bfd *file)
 		printf (_ ("Failed to retrieve mapping info\n"));
 	      if ( !resolve_and_display_mapping_info (sec, file))
 		printf (_ ("Failed to resolve and display mapping info\n"));
+	      else
+		display_var_type ();
 	      free_mapping_info_struct ();
               free_debug_section (info);
 	    }
-- 
2.25.1



More information about the Binutils mailing list