-/* Most of this is Copyright Dima Kogan <dima@secretsauce.net>
- *
- * Pieces of this were taken from dwarf_prototypes.c in the dwarves project.
- * Those are Copyright (C) 2008 Arnaldo Carvalho de Melo <acme@redhat.com>.
+/* Copyright Dima Kogan <dima@secretsauce.net>
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of version 2 of the GNU General Public License as published by the
//#define DUMP_PROTOTYPES
#if 1
-#define complain( die, format, ... ) \
+#define complain(die, format, ...) \
fprintf(stderr, "%s() die '%s' @ 0x%lx: " format "\n", \
__func__, dwarf_diename(die), dwarf_dieoffset(die), \
- ##__VA_ARGS__ )
+ ##__VA_ARGS__)
#else
-#define complain( die, format, ... )
+#define complain(die, format, ...)
#endif
+#define NEXT_SIBLING(die) \
+ int res = dwarf_siblingof(die, die); \
+ if (res == 0) continue; /* sibling exists */ \
+ if (res < 0) return false; /* error */ \
+ break /* no sibling exists */
+
// A map from DIE addresses (Dwarf_Off) to type structures (struct
// arg_type_info*). This is created and filled in at the start of each import,
// and deleted when the import is complete
static struct dict type_hash;
-static bool getType( struct arg_type_info** info, Dwarf_Die* type_die);
+static bool get_type(struct arg_type_info** info, Dwarf_Die* type_die, struct protolib* plib);
#if 0
static bool _dump_dwarf_tree(Dwarf_Die* die, int indent)
{
- while(1)
- {
+ while (1) {
fprintf(stderr, "%*sprocessing unit: 0x%02x/'%s'\n", indent*4, "",
- dwarf_tag(die), dwarf_diename(die) );
+ dwarf_tag(die), dwarf_diename(die));
Dwarf_Die child;
- if (dwarf_child(die, &child) == 0)
- {
- if( !_dump_dwarf_tree(&child, indent+1) )
+ if (dwarf_child(die, &child) == 0) {
+ if (!_dump_dwarf_tree(&child, indent+1))
return false;
}
- int res = dwarf_siblingof(die, die);
- if( res == 0 ) continue; // sibling exists
- if( res < 0 ) return false; // error
- break; // no sibling exists
+ SIBLING(die);
}
return true;
static bool dump_dwarf_tree(Dwarf_Die* die)
{
- return _dump_dwarf_tree( die, 0 );
+ return _dump_dwarf_tree(die, 0);
}
#endif
#ifdef DUMP_PROTOTYPES
-static bool _dump_ltrace_tree( const struct arg_type_info* info, int indent )
+static bool _dump_ltrace_tree(const struct arg_type_info* info, int indent)
{
- if( indent > 7 )
- {
+ if (indent > 7) {
fprintf(stderr, "%*s%p ...\n", indent*4, "", (void*)info);
return true;
}
- if( info == NULL )
- {
+ if (info == NULL) {
fprintf(stderr, "%*s%p NULL\n", indent*4, "", (void*)info);
return true;
}
- switch(info->type)
- {
+ switch (info->type) {
case ARGTYPE_VOID:
fprintf(stderr, "%*s%p void\n", indent*4, "", (void*)info);
break;
break;
case ARGTYPE_ARRAY:
- fprintf(stderr, "%*s%p array. elements not printed\n", indent*4, "", (void*)info);
+ fprintf(stderr, "%*s%p array. elements not printed\n", indent*4, "",
+ (void*)info);
break;
case ARGTYPE_POINTER:
fprintf(stderr, "%*s%p pointer to...\n", indent*4, "", (void*)info);
- _dump_ltrace_tree( info->u.ptr_info.info, indent+1 );
+ _dump_ltrace_tree(info->u.ptr_info.info, indent+1);
break;
case ARGTYPE_STRUCT:
}* elements = (struct struct_field*)info->u.entries.data;
unsigned int i;
for(i=0; i<info->u.entries.size; i++)
- _dump_ltrace_tree( elements[i].info, indent+1 );
+ _dump_ltrace_tree(elements[i].info, indent+1);
break;
default:
return true;
}
-static bool dump_ltrace_tree( const struct arg_type_info* info )
+static bool dump_ltrace_tree(const struct arg_type_info* info)
{
- return _dump_ltrace_tree( info, 0 );
+ return _dump_ltrace_tree(info, 0);
}
#endif
-
-static uint64_t attr_numeric(Dwarf_Die *die, uint32_t name)
+// pulls a numerical value out of a particular attribute in a die. Returns true
+// if successful. The result is returned in *result. Note that this is cast to
+// (uint64_t), regardless of the actual type of the input
+static bool get_die_numeric(uint64_t* result,
+ Dwarf_Die *die, unsigned int attr_name)
{
- Dwarf_Attribute attr;
- uint32_t form;
+ Dwarf_Attribute attr ;
+
+ union {
+ Dwarf_Word udata;
+ Dwarf_Sword sdata;
+ Dwarf_Addr addr;
+ bool flag;
+ } u;
+
+ if (dwarf_attr(die, attr_name, &attr) == NULL)
+ return false;
+
+ unsigned int form = dwarf_whatform(&attr);
- if (dwarf_attr(die, name, &attr) == NULL)
- return 0;
+#define PROCESS_NUMERIC(type) \
+ if (dwarf_form ## type(&attr, &u.type) != 0) \
+ return false; \
+ *result = (uint64_t)u.type; \
+ return true
- form = dwarf_whatform(&attr);
switch (form) {
- case DW_FORM_addr: {
- Dwarf_Addr addr;
- if (dwarf_formaddr(&attr, &addr) == 0)
- return addr;
- }
- break;
+ case DW_FORM_addr:
+ PROCESS_NUMERIC(addr);
+
case DW_FORM_data1:
case DW_FORM_data2:
case DW_FORM_data4:
case DW_FORM_data8:
+ case DW_FORM_udata:
+ PROCESS_NUMERIC(udata);
+
case DW_FORM_sdata:
- case DW_FORM_udata: {
- Dwarf_Word value;
- if (dwarf_formudata(&attr, &value) == 0)
- return value;
- }
- break;
+ PROCESS_NUMERIC(sdata);
+
case DW_FORM_flag:
- case DW_FORM_flag_present: {
- bool value;
- if (dwarf_formflag(&attr, &value) == 0)
- return value;
- }
- break;
+ PROCESS_NUMERIC(flag);
+
default:
- complain(die, "DW_AT_<0x%x>=0x%x", name, form);
- break;
+ complain(die, "Unknown numeric form %d for attr_name: %d", form, attr_name);
+ return false;
}
+#undef PROCESS_NUMERIC
+}
+
+static bool get_integer_base_type(enum arg_type* type, int byte_size, bool is_signed)
+{
+ switch (byte_size) {
+ case sizeof(char):
+ *type = ARGTYPE_CHAR;
+ return true;
- return 0;
+ case sizeof(short):
+ *type = is_signed ? ARGTYPE_SHORT : ARGTYPE_USHORT;
+ return true;
+
+ case sizeof(int):
+ *type = is_signed ? ARGTYPE_INT : ARGTYPE_UINT;
+ return true;
+
+ case sizeof(long):
+ *type = is_signed ? ARGTYPE_LONG : ARGTYPE_ULONG;
+ return true;
+
+ default:
+ return false;
+ }
}
-static enum arg_type getBaseType( Dwarf_Die* die )
+static enum arg_type get_base_type(Dwarf_Die* die)
{
- int encoding = attr_numeric(die, DW_AT_encoding);
+ int64_t encoding;
+ if(!get_die_numeric((uint64_t*)&encoding, die, DW_AT_encoding))
+ return ARGTYPE_VOID;
- if( encoding == DW_ATE_void )
+ if (encoding == DW_ATE_void)
return ARGTYPE_VOID;
- if( encoding == DW_ATE_signed_char || encoding == DW_ATE_unsigned_char )
+ if (encoding == DW_ATE_signed_char || encoding == DW_ATE_unsigned_char)
return ARGTYPE_CHAR;
- if( encoding == DW_ATE_signed ||
- encoding == DW_ATE_unsigned )
- {
+ uint64_t byte_size;
+ if (!get_die_numeric(&byte_size, die, DW_AT_byte_size))
+ return ARGTYPE_VOID;
+
+ if (encoding == DW_ATE_signed ||
+ encoding == DW_ATE_unsigned ||
+ encoding == DW_ATE_boolean) {
+
bool is_signed = (encoding == DW_ATE_signed);
- switch( attr_numeric(die, DW_AT_byte_size) )
- {
- case sizeof(char):
- return ARGTYPE_CHAR;
- case sizeof(short):
- return is_signed ? ARGTYPE_SHORT : ARGTYPE_USHORT;
+ enum arg_type type;
+ if(!get_integer_base_type(&type, (int)byte_size, is_signed)) {
+ complain(die, "Unknown integer base type. Using 'void'");
+ return ARGTYPE_VOID;
+ }
+ return type;
+ }
- case sizeof(int):
- return is_signed ? ARGTYPE_INT : ARGTYPE_UINT;
+ if (encoding == DW_ATE_float) {
+ switch (byte_size) {
+ case sizeof(float):
+ return ARGTYPE_FLOAT;
- case sizeof(long):
- return is_signed ? ARGTYPE_LONG : ARGTYPE_ULONG;
+ case sizeof(double):
+ return ARGTYPE_DOUBLE;
default:
- complain(die, "");
- exit(1);
+ // things like long doubles. ltrace has no support yet, so I just
+ // say "void"
+ return ARGTYPE_VOID;
}
}
- if( encoding == DW_ATE_float )
- {
- switch( attr_numeric(die, DW_AT_byte_size) )
- {
- case sizeof(float):
+#if 0
+ if (encoding == DW_ATE_complex_float) {
+ switch (byte_size) {
+ case 2*sizeof(float):
return ARGTYPE_FLOAT;
- case sizeof(double):
+ case 2*sizeof(double):
return ARGTYPE_DOUBLE;
default:
- complain(die, "");
- exit(1);
+ // things like long doubles. ltrace has no support yet, so I just
+ // say "void"
+ return ARGTYPE_VOID;
}
}
+#endif
- complain(die, "");
- exit(1);
+ // Unknown encoding. I just say void
+ complain(die, "Unknown base type. Returning 'void'");
return ARGTYPE_VOID;
}
-static bool getTypeDie( Dwarf_Die* type_die, Dwarf_Die* die )
+static bool get_type_die(Dwarf_Die* type_die, Dwarf_Die* die)
{
Dwarf_Attribute attr;
return
return *(const Dwarf_Off*)a == *(const Dwarf_Off*)b;
}
-static bool getEnum(struct arg_type_info* enum_info, Dwarf_Die* parent)
+static bool get_enum(struct arg_type_info* enum_info, Dwarf_Die* parent)
{
- enum_info->type = ARGTYPE_INT;
+ uint64_t byte_size;
+ if (!get_die_numeric(&byte_size, parent, DW_AT_byte_size)) {
+ // No byte size given, assume 'int'
+ enum_info->type = ARGTYPE_INT;
+ } else {
+ if(!get_integer_base_type(&enum_info->type, (int)byte_size, true)) {
+ complain(parent, "Unknown integer base type. Using 'int'");
+ enum_info->type = ARGTYPE_INT;
+ }
+ }
struct enum_lens *lens = calloc(1, sizeof(struct enum_lens));
- if (lens == NULL)
- {
+ if (lens == NULL) {
complain(parent, "alloc error");
return false;
}
enum_info->lens = &lens->super;
Dwarf_Die die;
- if( dwarf_child(parent, &die) != 0 )
- {
+ if (dwarf_child(parent, &die) != 0) {
// empty enum. we're done
return true;
}
while(1) {
- complain(&die, "enum element: 0x%02x/'%s'", dwarf_tag(&die), dwarf_diename(&die) );
+ complain(&die, "enum element: 0x%02x/'%s'", dwarf_tag(&die),
+ dwarf_diename(&die));
- if( dwarf_tag(&die) != DW_TAG_enumerator )
- {
+ if (dwarf_tag(&die) != DW_TAG_enumerator) {
complain(&die, "Enums can have ONLY DW_TAG_enumerator elements");
return false;
}
- if( !dwarf_hasattr(&die, DW_AT_const_value) )
- {
+ if (!dwarf_hasattr(&die, DW_AT_const_value)) {
complain(&die, "Enums MUST have DW_AT_const_value values");
return false;
}
const char* key = dwarf_diename(&die);
- if( key == NULL )
- {
+ if (key == NULL) {
complain(&die, "Enums must have a DW_AT_name key");
return false;
}
const char* dupkey = strdup(key);
- if( dupkey == NULL )
- {
+ if (dupkey == NULL) {
complain(&die, "Couldn't duplicate enum key");
return false;
}
- struct value* value = calloc( 1, sizeof(struct value) );
- if( value == NULL )
- {
+ struct value* value = calloc(1, sizeof(struct value));
+ if (value == NULL) {
complain(&die, "Couldn't alloc enum value");
return false;
}
- value_init_detached(value, NULL, type_get_simple( ARGTYPE_INT ), 0);
- value_set_word(value, attr_numeric(&die, DW_AT_const_value) );
+ value_init_detached(value, NULL, type_get_simple(enum_info->type), 0);
+ uint64_t enum_value;
+ if (!get_die_numeric(&enum_value, &die, DW_AT_const_value)) {
+ complain(&die, "Couldn't get enum value");
+ return false;
+ }
+
+ value_set_word(value, (long)enum_value);
- if( lens_enum_add( lens, dupkey, 0, value, 0 ) )
- {
+ if (lens_enum_add(lens, dupkey, 0, value, 0)) {
complain(&die, "Couldn't add enum element");
return false;
}
- int res = dwarf_siblingof(&die, &die);
- if( res == 0 ) continue; /* sibling exists */
- if( res < 0 ) return false; /* error */
- break; /* no sibling exists */
+ NEXT_SIBLING(&die);
}
return true;
}
-static bool getArray(struct arg_type_info* array_info, Dwarf_Die* parent)
+static bool get_array(struct arg_type_info* array_info, Dwarf_Die* parent, struct protolib* plib)
{
Dwarf_Die type_die;
- if( !getTypeDie( &type_die, parent ) )
- {
- complain( parent, "Array has unknown type" );
+ if (!get_type_die(&type_die, parent)) {
+ complain(parent, "Array has unknown type");
return false;
}
struct arg_type_info* info;
- if( !getType( &info, &type_die ) )
- {
- complain( parent, "Couldn't figure out array's type" );
+ if (!get_type(&info, &type_die, plib)) {
+ complain(parent, "Couldn't figure out array's type");
return false;
}
Dwarf_Die subrange;
- if( dwarf_child(parent, &subrange) != 0 )
- {
- complain( parent, "Array must have a DW_TAG_subrange_type child, but has none" );
+ if (dwarf_child(parent, &subrange) != 0) {
+ complain(parent,
+ "Array must have a DW_TAG_subrange_type child, but has none");
return false;
}
Dwarf_Die next_subrange;
- if( dwarf_siblingof(&subrange, &next_subrange) <= 0 )
- {
- complain( parent, "Array must have exactly one DW_TAG_subrange_type child" );
+ if (dwarf_siblingof(&subrange, &next_subrange) <= 0) {
+ complain(parent,
+ "Array must have exactly one DW_TAG_subrange_type child");
return false;
}
- if( !dwarf_hasattr(&subrange, DW_AT_upper_bound) )
- {
- complain( parent, "Array subrange must have a DW_AT_upper_bound");
- return false;
+ if (dwarf_hasattr(&subrange, DW_AT_lower_bound)) {
+ uint64_t lower_bound;
+ if (!get_die_numeric(&lower_bound, &subrange, DW_AT_lower_bound)) {
+ complain(parent, "Couldn't read lower bound");
+ return false;
+ }
+
+ if (lower_bound != 0) {
+ complain(parent,
+ "Array subrange has a nonzero lower bound. Don't know what to do");
+ return false;
+ }
}
- if( dwarf_hasattr(&subrange, DW_AT_lower_bound) )
+ uint64_t N;
+ if (!dwarf_hasattr(&subrange, DW_AT_upper_bound)) {
+ // no upper bound is defined. This is probably a variable-width array,
+ // and I don't know how long it is. Let's say 0 to be safe
+ N = 0;
+ }
+ else
{
- if( attr_numeric(&subrange, DW_AT_lower_bound) != 0 )
- {
- complain( parent, "Array subrange has a nonzero lower bound. Don't know what to do");
+ if (!get_die_numeric(&N, &subrange, DW_AT_upper_bound)) {
+ complain(parent, "Couldn't read upper bound");
return false;
}
+ N++;
}
// I'm not checking the subrange type. It should be some sort of integer,
// and I don't know what it would mean for it to be something else
- struct value* value = calloc( 1, sizeof(struct value) );
- if( value == NULL )
- {
+ struct value* value = calloc(1, sizeof(struct value));
+ if (value == NULL) {
complain(&subrange, "Couldn't alloc length value");
return false;
}
- value_init_detached(value, NULL, type_get_simple( ARGTYPE_INT ), 0);
- value_set_word(value, attr_numeric(&subrange, DW_AT_upper_bound)+1 );
+ value_init_detached(value, NULL, type_get_simple(ARGTYPE_INT), 0);
+ value_set_word(value, N);
- struct expr_node* length = calloc( 1, sizeof(struct expr_node) );
- if( length == NULL )
- {
+ struct expr_node* length = calloc(1, sizeof(struct expr_node));
+ if (length == NULL) {
complain(&subrange, "Couldn't alloc length expr");
return false;
}
expr_init_const(length, value);
- type_init_array(array_info, info, 0, length, 0 );
+ type_init_array(array_info, info, 0, length, 0);
return true;
}
-static bool getStructure(struct arg_type_info* struct_info, Dwarf_Die* parent)
+static bool get_structure(struct arg_type_info* struct_info, Dwarf_Die* parent, struct protolib* plib)
{
type_init_struct(struct_info);
Dwarf_Die die;
- if( dwarf_child(parent, &die) != 0 )
- {
+ if (dwarf_child(parent, &die) != 0) {
// no elements; we're done
return true;
}
while(1) {
- fprintf(stderr, "member: 0x%02x/'%s'\n", dwarf_tag(&die), dwarf_diename(&die) );
+ complain(&die, "member: 0x%02x", dwarf_tag(&die));
- if( dwarf_tag(&die) != DW_TAG_member )
- {
+ if (dwarf_tag(&die) != DW_TAG_member) {
complain(&die, "Structure can have ONLY DW_TAG_member");
return false;
}
Dwarf_Die type_die;
- if( !getTypeDie( &type_die, &die ) )
- {
- complain( &die, "Couldn't get type of element");
+ if (!get_type_die(&type_die, &die)) {
+ complain(&die, "Couldn't get type of element");
return false;
}
struct arg_type_info* member_info = NULL;
- if( !getType( &member_info, &type_die ) )
- {
+ if (!get_type(&member_info, &type_die, plib)) {
complain(&die, "Couldn't parse type from DWARF data");
return false;
}
- type_struct_add( struct_info, member_info, 0 );
+ type_struct_add(struct_info, member_info, 0);
- int res = dwarf_siblingof(&die, &die);
- if( res == 0 ) continue; /* sibling exists */
- if( res < 0 ) return false; /* error */
- break; /* no sibling exists */
+ NEXT_SIBLING(&die);
}
return true;
// Reads the type in the die into the given structure
// Returns true on sucess
-static bool getType( struct arg_type_info** info, Dwarf_Die* type_die)
+static bool get_type(struct arg_type_info** info, Dwarf_Die* type_die, struct protolib* plib)
{
Dwarf_Off die_offset = dwarf_dieoffset(type_die);
- struct arg_type_info** found_type = dict_find(&type_hash, &die_offset );
- if(found_type != NULL)
- {
+ struct arg_type_info** found_type = dict_find(&type_hash, &die_offset);
+ if (found_type != NULL) {
*info = *found_type;
complain(type_die, "Read pre-computed type: %p", *info);
return true;
}
+ const char* type_name = dwarf_diename(type_die);
+ if (type_name != NULL) {
+
+ struct named_type* already_defined_type =
+ protolib_lookup_type(plib, type_name, false);
+
+ if (already_defined_type != NULL) {
+ complain(type_die,
+ "Type '%s' defined in a .conf file. Using that instead of DWARF",
+ type_name);
+ *info = already_defined_type->info;
+ return true;
+ }
+ }
+
Dwarf_Die next_die;
- switch( dwarf_tag(type_die) )
- {
+ switch (dwarf_tag(type_die)) {
case DW_TAG_base_type:
- *info = type_get_simple( getBaseType( type_die ) );
+ *info = type_get_simple(get_base_type(type_die));
complain(type_die, "Storing base type: %p", *info);
- dict_insert( &type_hash, &die_offset, info );
+ dict_insert(&type_hash, &die_offset, info);
return true;
case DW_TAG_subroutine_type:
case DW_TAG_inlined_subroutine:
// function pointers are stored as void*. If ltrace tries to dereference
// these, it'll get a segfault
- *info = type_get_simple( ARGTYPE_VOID );
+ *info = type_get_simple(ARGTYPE_VOID);
complain(type_die, "Storing subroutine type: %p", *info);
- dict_insert( &type_hash, &die_offset, info );
+ dict_insert(&type_hash, &die_offset, info);
return true;
case DW_TAG_pointer_type:
- if( !getTypeDie(&next_die, type_die ) )
- {
+ if (!get_type_die(&next_die, type_die)) {
// the pointed-to type isn't defined, so I report a void*
- *info = type_get_simple( ARGTYPE_VOID );
+ *info = type_get_simple(ARGTYPE_VOID);
complain(type_die, "Storing void-pointer type: %p", *info);
- dict_insert( &type_hash, &die_offset, info );
+ dict_insert(&type_hash, &die_offset, info);
return true;
}
- *info = calloc( 1, sizeof(struct arg_type_info) );
- if( *info == NULL )
- {
+ *info = calloc(1, sizeof(struct arg_type_info));
+ if (*info == NULL) {
complain(type_die, "alloc error");
return false;
}
type_init_pointer(*info, NULL, 0);
complain(type_die, "Storing pointer type: %p", *info);
- dict_insert( &type_hash, &die_offset, info );
- return getType( &(*info)->u.ptr_info.info, &next_die );
+ dict_insert(&type_hash, &die_offset, info);
+ return get_type(&(*info)->u.ptr_info.info, &next_die, plib);
case DW_TAG_structure_type:
- *info = calloc( 1, sizeof(struct arg_type_info) );
- if( *info == NULL )
- {
+ *info = calloc(1, sizeof(struct arg_type_info));
+ if (*info == NULL) {
complain(type_die, "alloc error");
return false;
}
complain(type_die, "Storing struct type: %p", *info);
- dict_insert( &type_hash, &die_offset, info );
- return getStructure( *info, type_die );
+ dict_insert(&type_hash, &die_offset, info);
+ return get_structure(*info, type_die, plib);
- case DW_TAG_typedef: ;
- case DW_TAG_const_type: ;
- case DW_TAG_volatile_type: ;
+ case DW_TAG_typedef:
+ case DW_TAG_const_type:
+ case DW_TAG_volatile_type: {
// Various tags are simply pass-through, so I just keep going
bool res = true;
- if( getTypeDie(&next_die, type_die ) )
- {
+ if (get_type_die(&next_die, type_die)) {
complain(type_die, "Storing const/typedef type: %p", *info);
- res = getType( info, &next_die );
- }
- else
- {
+ res = get_type(info, &next_die, plib);
+ } else {
// no type. Use 'void'. Normally I'd think this is bogus, but stdio
// typedefs something to void
- *info = type_get_simple( ARGTYPE_VOID );
+ *info = type_get_simple(ARGTYPE_VOID);
complain(type_die, "Storing void type: %p", *info);
}
- if( res )
- dict_insert( &type_hash, &die_offset, info );
+ if (res)
+ dict_insert(&type_hash, &die_offset, info);
return res;
+ }
case DW_TAG_enumeration_type:
// We have an enumeration. This has type "int", but has a particular
// lens to handle the enum
- *info = calloc( 1, sizeof(struct arg_type_info) );
- if( *info == NULL )
- {
+ *info = calloc(1, sizeof(struct arg_type_info));
+ if (*info == NULL) {
complain(type_die, "alloc error");
return false;
}
complain(type_die, "Storing enum int: %p", *info);
- dict_insert( &type_hash, &die_offset, info );
- return getEnum( *info, type_die );
+ dict_insert(&type_hash, &die_offset, info);
+ return get_enum(*info, type_die);
case DW_TAG_array_type:
- *info = calloc( 1, sizeof(struct arg_type_info) );
- if( *info == NULL )
- {
+ *info = calloc(1, sizeof(struct arg_type_info));
+ if (*info == NULL) {
complain(type_die, "alloc error");
return false;
}
complain(type_die, "Storing array: %p", *info);
- dict_insert( &type_hash, &die_offset, info );
- return getArray( *info, type_die );
+ dict_insert(&type_hash, &die_offset, info);
+ return get_array(*info, type_die, plib);
+
+ case DW_TAG_union_type:
+ *info = type_get_simple(ARGTYPE_VOID);
+ complain(type_die, "Storing union-as-void type: %p", *info);
+ return true;
default:
complain(type_die, "Unknown type tag 0x%x", dwarf_tag(type_die));
return false;
}
-static bool getPrototype(struct prototype* proto, Dwarf_Die* subroutine)
+static bool get_prototype(struct prototype* proto, Dwarf_Die* subroutine, struct protolib* plib)
{
// First, look at the return type. This is stored in a DW_AT_type tag in the
// subroutine DIE. If there is no such tag, this function returns void
Dwarf_Die return_type_die;
- if( !getTypeDie(&return_type_die, subroutine ) )
- {
- proto->return_info = type_get_simple( ARGTYPE_VOID );
+ if (!get_type_die(&return_type_die, subroutine)) {
+ proto->return_info = type_get_simple(ARGTYPE_VOID);
proto->own_return_info = 0;
- }
- else
- {
- proto->return_info = calloc( 1, sizeof( struct arg_type_info ) );
- if( proto->return_info == NULL )
- {
+ } else {
+ proto->return_info = calloc(1, sizeof(struct arg_type_info));
+ if (proto->return_info == NULL) {
complain(subroutine, "Couldn't alloc return type");
return false;
}
proto->own_return_info = 0;
- if( !getType( &proto->return_info, &return_type_die ) )
- {
+ if (!get_type(&proto->return_info, &return_type_die, plib)) {
complain(subroutine, "Couldn't get return type");
return false;
}
// Now look at the arguments
Dwarf_Die arg_die;
- if( dwarf_child(subroutine, &arg_die) != 0 )
- {
+ if (dwarf_child(subroutine, &arg_die) != 0) {
// no args. We're done
return true;
}
while(1) {
- if( dwarf_tag(&arg_die) != DW_TAG_formal_parameter )
- goto next_prototype_argument;
+ if (dwarf_tag(&arg_die) == DW_TAG_formal_parameter) {
- complain(&arg_die, "arg: 0x%02x", dwarf_tag(&arg_die));
+ complain(&arg_die, "arg: 0x%02x", dwarf_tag(&arg_die));
- Dwarf_Die type_die;
- if( !getTypeDie(&type_die, &arg_die ) )
- {
- complain(&arg_die, "Couldn't get the argument type die");
- return false;
- }
+ Dwarf_Die type_die;
+ if (!get_type_die(&type_die, &arg_die)) {
+ complain(&arg_die, "Couldn't get the argument type die");
+ return false;
+ }
- struct arg_type_info* arg_type_info = NULL;
- if( !getType( &arg_type_info, &type_die ) )
- {
- complain(&arg_die, "Couldn't parse arg type from DWARF data");
- return false;
- }
+ struct arg_type_info* arg_type_info = NULL;
+ if (!get_type(&arg_type_info, &type_die, plib)) {
+ complain(&arg_die, "Couldn't parse arg type from DWARF data");
+ return false;
+ }
- struct param param;
- param_init_type(¶m, arg_type_info, 0);
- if( prototype_push_param(proto, ¶m) <0 )
- {
- complain(&arg_die, "couldn't add argument to the prototype");
- return false;
- }
+ struct param param;
+ param_init_type(¶m, arg_type_info, 0);
+ if (prototype_push_param(proto, ¶m) <0) {
+ complain(&arg_die, "couldn't add argument to the prototype");
+ return false;
+ }
#ifdef DUMP_PROTOTYPES
- fprintf(stderr, "Adding argument:\n");
- dump_ltrace_tree(arg_type_info);
+ fprintf(stderr, "Adding argument:\n");
+ dump_ltrace_tree(arg_type_info);
#endif
+ }
- next_prototype_argument: ;
- int res = dwarf_siblingof(&arg_die, &arg_die);
- if( res == 0 ) continue; /* sibling exists */
- if( res < 0 ) return false; /* error */
- break; /* no sibling exists */
+ NEXT_SIBLING(&arg_die);
}
return true;
}
-static bool process_die_compileunit(struct protolib* plib, struct library* lib, Dwarf_Die* parent)
+static bool import_subprogram(struct protolib* plib, struct library* lib,
+ Dwarf_Die* die)
{
- Dwarf_Die die;
- if( dwarf_child(parent, &die) != 0 )
- return false;
+ // I use the linkage function name if there is one, otherwise the
+ // plain name
+ const char* function_name = NULL;
+ Dwarf_Attribute attr;
+ if (dwarf_attr(die, DW_AT_linkage_name, &attr) != NULL)
+ function_name = dwarf_formstring(&attr);
+ if (function_name == NULL)
+ function_name = dwarf_diename(die);
+ if (function_name == NULL) {
+ complain(die, "Function has no name. Not importing");
+ return true;
+ }
- while(1)
- {
- if( dwarf_tag(&die) == DW_TAG_subprogram )
- {
- const char* function_name = dwarf_diename(&die);
+ if (!filter_matches_symbol(options.plt_filter, function_name, lib) &&
+ !filter_matches_symbol(options.static_filter, function_name, lib) &&
+ !filter_matches_symbol(options.export_filter, function_name, lib)) {
+ complain(die, "Prototype not requested by any filter");
+ return true;
+ }
- complain(&die, "subroutine_type: 0x%02x; function '%s'", dwarf_tag(&die), function_name);
+ complain(die, "subroutine_type: 0x%02x; function '%s'",
+ dwarf_tag(die), function_name);
- struct prototype* proto =
- protolib_lookup_prototype(plib, function_name, true );
+ struct prototype* proto =
+ protolib_lookup_prototype(plib, function_name, false);
- if( proto != NULL )
- {
- complain(&die, "Prototype already exists. Skipping");
- goto next_prototype;
- }
+ if (proto != NULL) {
+ complain(die, "Prototype already exists. Skipping");
+ return true;
+ }
- if( !filter_matches_symbol(options.plt_filter, function_name, lib) &&
- !filter_matches_symbol(options.static_filter, function_name, lib) &&
- !filter_matches_symbol(options.export_filter, function_name, lib) )
- {
- complain(&die, "Prototype not requested by any filter");
- goto next_prototype;
- }
+ proto = malloc(sizeof(struct prototype));
+ if (proto == NULL) {
+ complain(die, "couldn't alloc prototype");
+ return false;
+ }
+ prototype_init(proto);
- proto = malloc(sizeof(struct prototype));
- if( proto == NULL )
- {
- complain(&die, "couldn't alloc prototype");
- return false;
- }
- prototype_init( proto );
+ if (!get_prototype(proto, die, plib)) {
+ complain(die, "couldn't get prototype");
+ return false;
+ }
- if( !getPrototype(proto, &die ) )
- {
- complain(&die, "couldn't get prototype");
- return false;
- }
+ protolib_add_prototype(plib, function_name, 0, proto);
+ return true;
+}
- protolib_add_prototype(plib, function_name, 0, proto);
- }
+static bool process_die_compileunit(struct protolib* plib, struct library* lib,
+ Dwarf_Die* parent)
+{
+ Dwarf_Die die;
+ if (dwarf_child(parent, &die) != 0) {
+ // no child nodes, so nothing to do
+ return true;
+ }
+
+ while (1) {
+ if (dwarf_tag(&die) == DW_TAG_subprogram)
+ if(!import_subprogram(plib, lib, &die))
+ return false;
- next_prototype:;
- int res = dwarf_siblingof(&die, &die);
- if( res == 0 ) continue; /* sibling exists */
- if( res < 0 ) return false; /* error */
- break; /* no sibling exists */
+ NEXT_SIBLING(&die);
}
return true;
}
-static bool import( struct protolib* plib, struct library* lib, Dwfl* dwfl )
+static bool import(struct protolib* plib, struct library* lib, Dwfl* dwfl)
{
dict_init(&type_hash, sizeof(Dwarf_Off), sizeof(struct arg_type_info*),
- dwarf_die_hash, dwarf_die_eq, NULL );
+ dwarf_die_hash, dwarf_die_eq, NULL);
Dwarf_Addr bias;
Dwarf_Die* die = NULL;
- while( (die = dwfl_nextcu(dwfl, die, &bias)) != NULL )
- {
- if( dwarf_tag(die) == DW_TAG_compile_unit )
- {
- if( !process_die_compileunit(plib, lib, die) )
- {
+ while ((die = dwfl_nextcu(dwfl, die, &bias)) != NULL) {
+ if (dwarf_tag(die) == DW_TAG_compile_unit) {
+ if (!process_die_compileunit(plib, lib, die)) {
complain(die, "Error reading compile unit");
exit(1);
return false;
}
- }
- else
- {
+ } else {
complain(die, "DW_TAG_compile_unit expected");
exit(1);
return false;
}
}
- dict_destroy( &type_hash, NULL, NULL, NULL );
+ dict_destroy(&type_hash, NULL, NULL, NULL);
return true;
}
-bool import_DWARF_prototypes( struct protolib* plib, struct library* lib,
- Dwfl *dwfl )
+bool import_DWARF_prototypes(struct library* lib)
{
- if( plib == NULL )
- {
+ struct protolib* plib = lib->protolib;
+ Dwfl* dwfl = lib->dwfl;
+
+ if (plib == NULL) {
plib = protolib_cache_default(&g_protocache, lib->soname, 0);
- if (plib == NULL)
- {
+ if (plib == NULL) {
fprintf(stderr, "Error loading protolib %s: %s.\n",
lib->soname, strerror(errno));
}
}
- return import(plib, lib, dwfl);
+ if (import(plib, lib, dwfl)) {
+ lib->protolib = plib;
+ return true;
+ }
+ return false;
}
/*
- all my *allocs leak
-- protolib_lookup_prototype should look for imports?
*/