From f365e7a74546efb8601592ff5acc96a5cf040098 Mon Sep 17 00:00:00 2001 From: Dominique Martinet Date: Tue, 3 Oct 2017 15:59:47 +0900 Subject: print union --- dwarf.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/dwarf.c b/dwarf.c index 4e7539f..d18c74b 100644 --- a/dwarf.c +++ b/dwarf.c @@ -242,10 +242,9 @@ static void print_field(Dwarf_Debug dbg, Dwarf_Die die, const char *field_name) Dwarf_Attribute attr; Dwarf_Error err; Dwarf_Unsigned offset = 0; + char type_buf[1024]; int rc; - printf("Found %s\n", field_name); - rc = dwarf_attr(die, DW_AT_data_member_location, &attr, &err); if (rc == DW_DLV_NO_ENTRY) { printf("Found %s but no offset, assuming 0\n", field_name); @@ -288,7 +287,6 @@ static void print_field(Dwarf_Debug dbg, Dwarf_Die die, const char *field_name) } dwarf_dealloc(dbg, attr, DW_DLA_ATTR); } - printf("offset %u\n", (unsigned int)offset); rc = dwarf_attr(die, DW_AT_type, &attr, &err); if (rc == DW_DLV_NO_ENTRY) { @@ -302,7 +300,6 @@ static void print_field(Dwarf_Debug dbg, Dwarf_Die die, const char *field_name) Dwarf_Off type_off; Dwarf_Half type_tag; char *type_name; - char type_buf[1024]; int pointer = 0; rc = dwarf_global_formref(attr, &type_off, &err); @@ -374,10 +371,10 @@ static void print_field(Dwarf_Debug dbg, Dwarf_Die die, const char *field_name) } if (type_tag == DW_TAG_structure_type) { - sprintf(type_buf, "struct %s%s", pointer ? "*" : "", + snprintf(type_buf, 1024, "struct %s%s", pointer ? "*" : "", type_name); } else if (type_tag == DW_TAG_base_type || type_tag == DW_TAG_typedef) { - sprintf(type_buf, "%s%s", pointer ? "*" : "", + snprintf(type_buf, 1024, "%s%s", pointer ? "*" : "", type_name); } else { const char *tag_name; @@ -391,10 +388,13 @@ static void print_field(Dwarf_Debug dbg, Dwarf_Die die, const char *field_name) tag_name, type_tag); exit(7); } - printf("type name %s\n", type_buf); dwarf_dealloc(dbg, type_name, DW_DLA_STRING); dwarf_dealloc(dbg, attr, DW_DLA_ATTR); dwarf_dealloc(dbg, type_die, DW_DLA_DIE); } + + printf("union {\n\tchar padding[%u]\n\t%s %s\n};\n", + (unsigned int) offset, + type_buf, field_name); } -- cgit v1.2.1-2-g3f67