2 This file is part of Kvasir, a Valgrind skin that implements the
3 C language front-end for the Daikon Invariant Detection System
5 Copyright (C) 2004 Philip Guo, MIT CSAIL Program Analysis Group
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
17 #include <sys/types.h>
21 #include "dumpstructures.h"
23 #include "elf/dwarf2.h"
30 int process_elf_binary_data(char* filename);
34 int main(int argc, char **argv) {
39 process_elf_binary_data(argv[1]);
40 daikon_preprocess_entry_array();
43 // Pre-processes global dwarf_entry_array in order to place
44 // the data in a form that can easily be turned into .decls
46 void daikon_preprocess_entry_array()
48 initializeTypeArray();
53 int entry_is_type(dwarf_entry *entry) {
54 if (entry->tag_name==DW_TAG_structure_type||
55 entry->tag_name==DW_TAG_union_type) {
56 collection_type* collection_ptr = (collection_type*)(entry->entry_ptr);
57 /* if (collection_ptr->name==0&&assigntype) {
58 collection_ptr->name=(char*)malloc(100);
59 sprintf(collection_ptr->name,"TYPE%ld",typecount++);
66 int entry_is_valid_function(dwarf_entry *entry) {
67 if (tag_is_function(entry->tag_name)) {
68 function* funcPtr = (function*)(entry->entry_ptr);
69 if (funcPtr->start_pc != 0 && funcPtr->name != 0) {
73 printf("Skipping invalid-looking function %s\n", funcPtr->name);
85 void initializeTypeArray()
88 dwarf_entry * cur_entry;
89 struct genhashtable * ght=genallocatehashtable((unsigned int (*)(void *)) & hashstring,(int (*)(void *,void *)) &equivalentstrings);
90 struct genhashtable * sht=NULL;
95 int fd=open(rootfile,O_RDONLY);
97 sht=genallocatehashtable((unsigned int (*)(void *)) & hashstring,(int (*)(void *,void *)) &equivalentstrings);
99 if (read(fd,&a,1)>0) {
104 if (offset>0&&(a==13||a==10)) {
107 char *str=copystr(buf);
108 genputtable(sht,str,str);
115 for (i = 0; i < dwarf_entry_array_size; i++)
117 cur_entry = &dwarf_entry_array[i];
118 if (entry_is_type(cur_entry))
120 collection_type* collection_ptr = (collection_type*)(cur_entry->entry_ptr);
124 for(j=0;j<collection_ptr->num_members;j++) {
125 dwarf_entry *entry=collection_ptr->members[j];
126 if (entry->tag_name==DW_TAG_inheritance) {
129 member * member_ptr=(member *)entry->entry_ptr;
130 char *name=member_ptr->name;
131 dwarf_entry *type=member_ptr->type_ptr;
132 char *typestr=printname(type,GETTYPE);
133 char *poststr=printname(type,POSTNAME);
139 if (collection_ptr->name!=NULL) {
140 struct valuepair *vp=NULL;
141 if (gencontains(ght,collection_ptr->name))
142 vp=(struct valuepair *)gengettable(ght,collection_ptr->name);
143 if (vp==NULL||vp->value<value) {
145 vp=(struct valuepair*)calloc(1,sizeof(struct valuepair));
146 genputtable(ght,collection_ptr->name,vp);
160 for (i = 0; i < dwarf_entry_array_size; i++) {
161 cur_entry = &dwarf_entry_array[i];
162 if (entry_is_type(cur_entry)) {
163 collection_type* collection_ptr = (collection_type*)(cur_entry->entry_ptr);
168 if (!gencontains(sht,collection_ptr->name))
170 if (gencontains(ght,collection_ptr->name)) {
171 struct valuepair *vp=(struct valuepair*)gengettable(ght,collection_ptr->name);
176 for(j=0;j<collection_ptr->num_members;j++) {
177 dwarf_entry *entry=collection_ptr->members[j];
178 if (entry->tag_name==DW_TAG_inheritance) {
179 inherit *in_ptr=(inherit*)collection_ptr->members[j]->entry_ptr;
180 dwarf_entry *typeptr=in_ptr->target_ptr;
181 collection_type* sub_ptr = (collection_type*)(typeptr->entry_ptr);
182 if (!gencontains(sht,sub_ptr->name)) {
184 genputtable(sht,sub_ptr->name,sub_ptr->name);
187 member * member_ptr=(member *)entry->entry_ptr;
188 char *name=member_ptr->name;
189 dwarf_entry *type=member_ptr->type_ptr;
190 char *typestr=printname(type,GETJUSTTYPE);
191 if (typestr!=NULL&&!gencontains(sht,typestr)) {
193 genputtable(sht,typestr,typestr);
203 for (i = 0; i < dwarf_entry_array_size; i++)
205 cur_entry = &dwarf_entry_array[i];
206 if (entry_is_type(cur_entry))
208 collection_type* collection_ptr = (collection_type*)(cur_entry->entry_ptr);
211 if (collection_ptr->name==NULL)
213 if (sht!=NULL&&!gencontains(sht,collection_ptr->name))
215 if (gencontains(ght,collection_ptr->name)) {
216 struct valuepair *vp=(struct valuepair*)gengettable(ght,collection_ptr->name);
221 printf("structure %s ",collection_ptr->name);
223 while(j<collection_ptr->num_members&&
224 collection_ptr->members[j]->tag_name==DW_TAG_inheritance) {
225 inherit *in_ptr=(inherit*)collection_ptr->members[j]->entry_ptr;
226 dwarf_entry *typeptr=in_ptr->target_ptr;
227 collection_type* sub_ptr = (collection_type*)(typeptr->entry_ptr);
229 printf("subclasses ");
232 printf("%s ",sub_ptr->name);
237 for(j=0;j<collection_ptr->num_members;j++) {
238 dwarf_entry *entry=collection_ptr->members[j];
239 if (entry->tag_name==DW_TAG_inheritance) {
240 inherit * inherit_ptr=(inherit *)entry->entry_ptr;
241 if (inherit_ptr->data_member_location>offset) {
242 printf(" reserved byte[%ld];\n",inherit_ptr->data_member_location-offset);
243 offset=inherit_ptr->data_member_location;
246 dwarf_entry *type=inherit_ptr->target_ptr;
247 collection_type *c_ptr=(collection_type*)type->entry_ptr;
248 offset+=printtype(c_ptr,ght);
251 member * member_ptr=(member *)entry->entry_ptr;
252 char *name=member_ptr->name;
253 dwarf_entry *type=member_ptr->type_ptr;
254 char *typestr=printname(type,GETTYPE);
255 char *poststr=printname(type,POSTNAME);
256 if (member_ptr->data_member_location>offset) {
257 printf(" reserved byte[%ld];\n",member_ptr->data_member_location-offset);
258 offset=member_ptr->data_member_location;
260 offset+=getsize(type);
262 printf(" %s %s%s;\n",typestr,name,poststr);
265 if (offset<collection_ptr->byte_size)
266 printf(" reserved byte[%ld];\n",collection_ptr->byte_size-offset);
272 int printtype(collection_type *collection_ptr,struct genhashtable *ght)
278 struct valuepair *vp=NULL;
279 if (gencontains(ght,collection_ptr->name))
280 vp=(struct valuepair *)gengettable(ght,collection_ptr->name);
282 collection_ptr=(collection_type*) dwarf_entry_array[vp->index].entry_ptr;
284 for(j=0;j<collection_ptr->num_members;j++) {
285 dwarf_entry *entry=collection_ptr->members[j];
286 if (entry->tag_name==DW_TAG_inheritance) {
287 inherit * inherit_ptr=(inherit *)entry->entry_ptr;
288 if (inherit_ptr->data_member_location>offset) {
289 printf(" reserved byte[%ld];\n",inherit_ptr->data_member_location-offset);
290 offset=inherit_ptr->data_member_location;
294 dwarf_entry *type=inherit_ptr->target_ptr;
295 collection_type *c_ptr=(collection_type*)type->entry_ptr;
296 offset+=printtype(c_ptr,ght);
299 member * member_ptr=(member *)entry->entry_ptr;
300 char *name=member_ptr->name;
301 dwarf_entry *type=member_ptr->type_ptr;
302 char *typestr=printname(type,GETTYPE);
303 char *poststr=printname(type,POSTNAME);
304 if (member_ptr->data_member_location>offset) {
305 printf(" reserved byte[%ld];\n",member_ptr->data_member_location-offset);
306 offset=member_ptr->data_member_location;
308 offset+=getsize(type);
310 printf(" %s %s%s;\n",typestr,name,poststr);
318 int getsize(dwarf_entry *type) {
321 switch(type->tag_name) {
322 case DW_TAG_enumeration_type:
324 case DW_TAG_array_type: {
325 modifier_type * modifier_ptr=(modifier_type*)type->entry_ptr;
328 for(i=0;i<modifier_ptr->num_array;i++) {
329 size*=((array_bound*)modifier_ptr->array_ptr[i]->entry_ptr)->upperbound+1;
331 return size*getsize(modifier_ptr->target_ptr);
333 case DW_TAG_const_type:
335 consttype * ctype_ptr=(consttype*)type->entry_ptr;
336 return getsize(ctype_ptr->target_ptr);
339 case DW_TAG_base_type: {
340 base_type *base=(base_type*)type->entry_ptr;
341 return base->byte_size;
343 case DW_TAG_pointer_type: {
346 case DW_TAG_union_type:
347 case DW_TAG_structure_type: {
348 collection_type *ctype=(collection_type*)type->entry_ptr;
349 return ctype->byte_size;
351 case DW_TAG_subroutine_type: {
356 tdef * tdef_ptr=(tdef*)type->entry_ptr;
357 return getsize(tdef_ptr->target_ptr);
366 char * printname(dwarf_entry * type,int op) {
368 if (op==GETTYPE||op==GETJUSTTYPE)
372 switch(type->tag_name) {
373 case DW_TAG_enumeration_type:
377 case DW_TAG_array_type: {
378 modifier_type * modifier_ptr=(modifier_type*)type->entry_ptr;
379 if (op==GETTYPE||op==GETJUSTTYPE) {
380 char *typename=printname(modifier_ptr->target_ptr,op);
382 } else if (op==POSTNAME) {
385 char *typename=printname(modifier_ptr->target_ptr,op);
386 char *newptr=(char *)malloc(200);
387 for(i=0;i<modifier_ptr->num_array;i++) {
388 size*=((array_bound*)modifier_ptr->array_ptr[i]->entry_ptr)->upperbound+1;
390 sprintf(newptr,"%s[%ld]",typename,size);
395 case DW_TAG_const_type:
397 consttype * ctype_ptr=(consttype*)type->entry_ptr;
398 if (op==GETTYPE||op==GETJUSTTYPE) {
399 char *typename=printname(ctype_ptr->target_ptr,op);
404 case DW_TAG_subroutine_type: {
409 tdef * tdef_ptr=(tdef*)type->entry_ptr;
410 if (op==GETTYPE||op==GETJUSTTYPE) {
411 char *typename=printname(tdef_ptr->target_ptr,op);
416 case DW_TAG_base_type: {
417 base_type *base=(base_type*)type->entry_ptr;
419 switch(base->byte_size) {
427 char *m=(char*)malloc(100);
428 sprintf(m,"error%ld",base->byte_size);
434 case DW_TAG_pointer_type: {
435 modifier_type * modifier_ptr=(modifier_type*)type->entry_ptr;
437 if (modifier_ptr->target_ptr==NULL)
438 return "void *"; /* seems like a good guess */
440 char *typename=printname(modifier_ptr->target_ptr,op);
442 char *newptr=(char *)malloc(200);
443 sprintf(newptr,"%s *",typename);
446 } else if (op==GETJUSTTYPE) {
447 if (modifier_ptr->target_ptr==NULL)
450 char *typename=printname(modifier_ptr->target_ptr,op);
456 case DW_TAG_union_type:
457 case DW_TAG_structure_type: {
458 collection_type *ctype=(collection_type*)type->entry_ptr;
459 if (op==GETTYPE&&ctype->name==NULL&&assigntype) {
460 ctype->name=(char*)malloc(100);
461 sprintf(ctype->name,"TYPE%ld",typecount++);
465 if (op==GETJUSTTYPE&&ctype->name==NULL&&assigntype) {
466 ctype->name=(char*)malloc(100);
467 sprintf(ctype->name,"TYPE%ld",typecount++);
479 char * p=(char *)malloc(100);
480 sprintf(p,"0x%x",type->tag_name);