Added support for dynamically allocated arrays.
[repair.git] / Repair / RepairCompiler / structextract / typedata.h
1 /*
2    This file is part of Kvasir, a Valgrind skin that implements the
3    C language front-end for the Daikon Invariant Detection System
4
5    Copyright (C) 2004 Philip Guo, MIT CSAIL Program Analysis Group
6
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.
11 */
12
13 /* typedata.h:
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
18    in tracking.
19 */
20
21 #ifndef TYPEDATA_H
22 #define TYPEDATA_H
23
24 // Type information data structures
25
26 // Contains one entry that holds data for one of many possible types
27 // depending on tag_name
28 typedef struct
29 {
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
33   unsigned int level;
34 } dwarf_entry;
35
36 // Entries for individual types
37
38 typedef struct
39 {
40   unsigned long byte_size; // DW_AT_byte_size
41   unsigned long encoding;
42
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
48
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
52 // arrays of size 1 -
53 // I will add full support for arrays later!!! //PG
54 // modifier_type = {const_type, pointer_type, array_type, volatile_type}
55 typedef struct
56 {
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)
59   int num_array;
60   dwarf_entry** array_ptr;
61 } modifier_type;
62
63 typedef struct
64
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)
68 } array_bound;
69
70 typedef struct
71
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)
74 } tdef;
75
76 typedef struct
77
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)
80 } consttype;
81
82 typedef struct
83
84   unsigned long target_ID; // ID of the entry that contains the type that this modifies
85   dwarf_entry* target_ptr; // Type that this entry modifies (DW_AT_type)
86   long data_member_location; // Addr offset relative to struct head
87 } inherit;
88
89 // collection_type = {structure_type, union_type, enumeration_type}
90 typedef struct
91 {
92   char* name;
93   unsigned long byte_size;
94   unsigned long num_members;
95   dwarf_entry** members; // Array of size num_members, type = {member, enumerator}
96 } collection_type;
97
98 // struct or union member
99 typedef struct
100 {
101   char* name;
102   unsigned long type_ID;
103   dwarf_entry* type_ptr;
104   long data_member_location; // Addr offset relative to struct head
105                              // This will be 0 for a union
106                              // This is stored as:
107                              // (DW_OP_plus_uconst: x)
108                              // where x is the location relative to struct head
109   //  char is_bit_field; // 1 = bit field
110   // Only relevant for bit fields
111   unsigned long byte_size;
112   unsigned long bit_offset;
113   unsigned long bit_size;
114 } member;
115
116 // enumeration member
117 typedef struct
118 {
119   char* name;
120   long const_value; // Enumeration value (SIGNED!)
121 } enumerator;
122
123 // FUNCTION!!!
124 typedef struct
125 {
126   char* name;
127   char* filename; // The file name relative to the compilation directory
128   unsigned long return_type_ID;
129   dwarf_entry* return_type;
130   unsigned long num_formal_params;
131   dwarf_entry* params; // Array of size num_formal_params, type = {formal_parameter}
132   int is_external; /* Is it extern? If so, probably want to skip it */
133   unsigned long start_pc; /* Location of the function in memory */
134 } function;
135
136 /* This is for abstract function types, as might be used in declaring
137    a parameter as taking a function pointer. At least for the moment, we
138    won't bother about the parameters. */
139 typedef struct {
140   unsigned long return_type_ID;
141   dwarf_entry* return_type;
142 } function_type;
143
144 // function formal parameter
145 typedef struct
146 {
147   char* name;
148   unsigned long type_ID;
149   dwarf_entry* type_ptr;
150   long location; // Offset from function base (this is SIGNED!)
151                  // This is stored as: (DW_OP_fbreg: x),
152                  // where x is location offset
153 } formal_parameter;
154
155 // compile_unit - only used to figure out filename and compilation directory
156 // We assume that every function belongs to the file specified
157 // by the nearest compile_unit entry (to its left) in dwarf_entry_array
158 typedef struct
159 {
160   char* filename;
161   char* comp_dir;
162 } compile_unit;
163
164 // Globals
165
166 extern dwarf_entry* dwarf_entry_array;
167 extern unsigned long dwarf_entry_array_size;
168
169 // Function declarations
170
171 // From readelf.c
172 char *get_TAG_name(unsigned long tag);
173 int process_elf_binary_data(char* filename);
174
175 // From typedata.c
176 char tag_is_relevant_entry(unsigned long tag);
177 char tag_is_modifier_type(unsigned long tag);
178 char tag_is_collection_type(unsigned long tag);
179 char tag_is_base_type(unsigned long tag);
180 char tag_is_member(unsigned long tag);
181 char tag_is_enumerator(unsigned long tag);
182 char tag_is_function(unsigned long tag);
183 char tag_is_formal_parameter(unsigned long tag);
184 char tag_is_compile_unit(unsigned long tag);
185 char tag_is_function_type(unsigned long tag);
186 char entry_is_listening_for_attribute(dwarf_entry* e, unsigned long attr);
187
188 char harvest_type_value(dwarf_entry* e, unsigned long value);
189 char harvest_byte_size_value(dwarf_entry* e, unsigned long value);
190 char harvest_encoding_value(dwarf_entry* e, unsigned long value);
191 char harvest_bit_size_value(dwarf_entry* e, unsigned long value);
192 char harvest_bit_offset_value(dwarf_entry* e, unsigned long value);
193 char harvest_const_value(dwarf_entry* e, unsigned long value);
194 char harvest_name(dwarf_entry* e, const char* str);
195 char harvest_comp_dir(dwarf_entry* e, const char* str);
196 char harvest_location(dwarf_entry* e, long value);
197 char harvest_data_member_location(dwarf_entry* e, long value);
198 char harvest_string(dwarf_entry* e, unsigned long attr, const char* str);
199 char harvest_external_flag_value(dwarf_entry *e, unsigned long value);
200 char harvest_address_value(dwarf_entry* e, unsigned long attr, unsigned long value);
201 char harvest_ordinary_unsigned_value(dwarf_entry* e, unsigned long attr, unsigned long value);
202
203 char binary_search_dwarf_entry_array(unsigned long target_ID, unsigned long* index_ptr);
204
205 void link_entries_to_type_entries();
206 void link_collection_to_members(dwarf_entry* e, unsigned long dist_to_end);
207 void link_function_to_params(dwarf_entry* e, unsigned long dist_to_end);
208 void initialize_function_filenames();
209 void link_array_entries_to_members();
210 void print_dwarf_entry(dwarf_entry* e);
211
212 void initialize_dwarf_entry_array(unsigned long num_entries);
213 void destroy_dwarf_entry_array(void);
214 void print_dwarf_entry_array();
215 void initialize_dwarf_entry_ptr(dwarf_entry* e);
216 void finish_dwarf_entry_array_init(void);
217
218 char tag_is_modifier_type(unsigned long tag);
219 char tag_is_collection_type(unsigned long tag);
220 char tag_is_base_type(unsigned long tag);
221 char tag_is_member(unsigned long tag);
222 char tag_is_enumerator(unsigned long tag);
223 char tag_is_function(unsigned long tag);
224 char tag_is_formal_parameter(unsigned long tag);
225
226
227 #endif