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.
14 Everything here attempts to extract the information directly
15 from the DWARF2 debugging information embedded within an ELF
16 executable, piggy-backing off of readelf.c code. These data
17 structures mimic the types of DWARF2 entries that we are interested
24 // Type information data structures
26 // Contains one entry that holds data for one of many possible types
27 // depending on tag_name
30 unsigned long ID; // Unique ID for each entry
31 unsigned long tag_name; // DW_TAG_____ for the type of this entry
32 void* entry_ptr; // Cast this pointer depending on value of tag_name
36 // Entries for individual types
40 unsigned long byte_size; // DW_AT_byte_size
41 unsigned long encoding;
43 // char is_bit_field; // 1 = bit field
44 // Only relevant for bit fields
45 unsigned long bit_size;
46 unsigned long bit_offset;
47 } base_type; // DW_TAG_base_type
49 // COP-OUT!!! Treat array_type JUST LIKE pointer_type for now
50 // so we don't keep track of the array size. We only care about the
51 // FIRST ELEMENT of the array since we just treat all pointers as
53 // I will add full support for arrays later!!! //PG
54 // modifier_type = {const_type, pointer_type, array_type, volatile_type}
57 unsigned long target_ID; // ID of the entry that contains the type that this modifies
58 dwarf_entry* target_ptr; // Type that this entry modifies (DW_AT_type)
60 dwarf_entry** array_ptr;
65 unsigned long target_ID; // ID of the entry that contains the type that this modifies
66 unsigned long upperbound;
67 dwarf_entry* target_ptr; // Type that this entry modifies (DW_AT_type)
72 unsigned long target_ID; // ID of the entry that contains the type that this modifies
73 dwarf_entry* target_ptr; // Type that this entry modifies (DW_AT_type)
78 unsigned long target_ID; // ID of the entry that contains the type that this modifies
79 dwarf_entry* target_ptr; // Type that this entry modifies (DW_AT_type)
82 // collection_type = {structure_type, union_type, enumeration_type}
86 unsigned long byte_size;
87 unsigned long num_members;
88 dwarf_entry** members; // Array of size num_members, type = {member, enumerator}
91 // struct or union member
95 unsigned long type_ID;
96 dwarf_entry* type_ptr;
97 long data_member_location; // Addr offset relative to struct head
98 // This will be 0 for a union
100 // (DW_OP_plus_uconst: x)
101 // where x is the location relative to struct head
102 // char is_bit_field; // 1 = bit field
103 // Only relevant for bit fields
104 unsigned long byte_size;
105 unsigned long bit_offset;
106 unsigned long bit_size;
109 // enumeration member
113 long const_value; // Enumeration value (SIGNED!)
120 char* filename; // The file name relative to the compilation directory
121 unsigned long return_type_ID;
122 dwarf_entry* return_type;
123 unsigned long num_formal_params;
124 dwarf_entry* params; // Array of size num_formal_params, type = {formal_parameter}
125 int is_external; /* Is it extern? If so, probably want to skip it */
126 unsigned long start_pc; /* Location of the function in memory */
129 /* This is for abstract function types, as might be used in declaring
130 a parameter as taking a function pointer. At least for the moment, we
131 won't bother about the parameters. */
133 unsigned long return_type_ID;
134 dwarf_entry* return_type;
137 // function formal parameter
141 unsigned long type_ID;
142 dwarf_entry* type_ptr;
143 long location; // Offset from function base (this is SIGNED!)
144 // This is stored as: (DW_OP_fbreg: x),
145 // where x is location offset
148 // compile_unit - only used to figure out filename and compilation directory
149 // We assume that every function belongs to the file specified
150 // by the nearest compile_unit entry (to its left) in dwarf_entry_array
159 extern dwarf_entry* dwarf_entry_array;
160 extern unsigned long dwarf_entry_array_size;
162 // Function declarations
165 char *get_TAG_name(unsigned long tag);
166 int process_elf_binary_data(char* filename);
169 char tag_is_relevant_entry(unsigned long tag);
170 char tag_is_modifier_type(unsigned long tag);
171 char tag_is_collection_type(unsigned long tag);
172 char tag_is_base_type(unsigned long tag);
173 char tag_is_member(unsigned long tag);
174 char tag_is_enumerator(unsigned long tag);
175 char tag_is_function(unsigned long tag);
176 char tag_is_formal_parameter(unsigned long tag);
177 char tag_is_compile_unit(unsigned long tag);
178 char tag_is_function_type(unsigned long tag);
179 char entry_is_listening_for_attribute(dwarf_entry* e, unsigned long attr);
181 char harvest_type_value(dwarf_entry* e, unsigned long value);
182 char harvest_byte_size_value(dwarf_entry* e, unsigned long value);
183 char harvest_encoding_value(dwarf_entry* e, unsigned long value);
184 char harvest_bit_size_value(dwarf_entry* e, unsigned long value);
185 char harvest_bit_offset_value(dwarf_entry* e, unsigned long value);
186 char harvest_const_value(dwarf_entry* e, unsigned long value);
187 char harvest_name(dwarf_entry* e, const char* str);
188 char harvest_comp_dir(dwarf_entry* e, const char* str);
189 char harvest_location(dwarf_entry* e, long value);
190 char harvest_data_member_location(dwarf_entry* e, long value);
191 char harvest_string(dwarf_entry* e, unsigned long attr, const char* str);
192 char harvest_external_flag_value(dwarf_entry *e, unsigned long value);
193 char harvest_address_value(dwarf_entry* e, unsigned long attr, unsigned long value);
194 char harvest_ordinary_unsigned_value(dwarf_entry* e, unsigned long attr, unsigned long value);
196 char binary_search_dwarf_entry_array(unsigned long target_ID, unsigned long* index_ptr);
198 void link_entries_to_type_entries();
199 void link_collection_to_members(dwarf_entry* e, unsigned long dist_to_end);
200 void link_function_to_params(dwarf_entry* e, unsigned long dist_to_end);
201 void initialize_function_filenames();
202 void link_array_entries_to_members();
203 void print_dwarf_entry(dwarf_entry* e);
205 void initialize_dwarf_entry_array(unsigned long num_entries);
206 void destroy_dwarf_entry_array(void);
207 void print_dwarf_entry_array();
208 void initialize_dwarf_entry_ptr(dwarf_entry* e);
209 void finish_dwarf_entry_array_init(void);
211 char tag_is_modifier_type(unsigned long tag);
212 char tag_is_collection_type(unsigned long tag);
213 char tag_is_base_type(unsigned long tag);
214 char tag_is_member(unsigned long tag);
215 char tag_is_enumerator(unsigned long tag);
216 char tag_is_function(unsigned long tag);
217 char tag_is_formal_parameter(unsigned long tag);