dwarf_funcs.c
6.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include <stdio.h>
#include "dwarf_incl.h"
#include "dwarf_funcs.h"
int
dwarf_get_funcs (
Dwarf_Debug dbg,
Dwarf_Func **funcs,
Dwarf_Signed * ret_func_count,
Dwarf_Error *error
)
{
/* Sweeps the complete .debug_funcnames section. */
Dwarf_Small *funcnames_ptr;
Dwarf_Word length;
/*
Points to the context for the current set of function names,
and contains information to identify the compilation-unit
that the set refers to.
*/
Dwarf_Func_Context funcnames_context;
/* Version number for the current set of funcnames. */
Dwarf_Half version;
/*
Offset from the start of compilation-unit
for the current function.
*/
Dwarf_Off cu_offset;
/* Counts the number of functions read. */
Dwarf_Unsigned func_count = 0;
/* Points to the current function read. */
Dwarf_Func func;
/*
Used to chain the Dwarf_Func_s structs for creating
contiguous list of pointers to the structs.
*/
Dwarf_Chain curr_chain, prev_chain, head_chain = NULL;
/* Points to contiguous block of Dwarf_Func's to be returned. */
Dwarf_Func *ret_funcs;
/* Temporary counter. */
Dwarf_Unsigned i;
/* ***** BEGIN CODE ***** */
if (dbg == NULL) {
_dwarf_error(NULL, error, DW_DLE_DBG_NULL);
return(DW_DLV_ERROR);
}
if (dbg->de_debug_funcnames == NULL) {
return(DW_DLV_NO_ENTRY);
}
funcnames_ptr = dbg->de_debug_funcnames;
do {
funcnames_context = (Dwarf_Func_Context)
_dwarf_get_alloc(dbg, DW_DLA_FUNC_CONTEXT, 1);
if (funcnames_context == NULL) {
_dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
return(DW_DLV_ERROR);
}
READ_UNALIGNED(length, funcnames_ptr, dbg->de_length_size);
funcnames_ptr += dbg->de_length_size;
funcnames_context->fu_length = length;
READ_UNALIGNED(version, funcnames_ptr, sizeof(Dwarf_Half));
funcnames_ptr += sizeof(Dwarf_Half);
if (version != CURRENT_VERSION_STAMP) {
_dwarf_error(dbg, error, DW_DLE_DEBUG_FUNCNAMES_VERSION_ERROR);
return(DW_DLV_ERROR);
}
READ_UNALIGNED(funcnames_context->fu_info_offset, funcnames_ptr,
dbg->de_length_size);
funcnames_ptr += dbg->de_length_size;
/*
Add the length of the cu_header to point to the
DW_TAG_compile_unit die.
*/
funcnames_context->fu_info_offset +=
dbg->de_length_size + /* Size of cu length field. */
sizeof(Dwarf_Half) + /* Size of version stamp field. */
dbg->de_length_size + /* Size of abbrev offset field. */
sizeof(Dwarf_Small); /* Size of address size field. */
READ_UNALIGNED(funcnames_context->fu_info_length, funcnames_ptr,
dbg->de_length_size);
funcnames_ptr += dbg->de_length_size;
if (funcnames_ptr > dbg->de_debug_funcnames +
dbg->de_debug_funcnames_size) {
_dwarf_error(dbg, error, DW_DLE_DEBUG_FUNCNAMES_LENGTH_BAD);
return(DW_DLV_ERROR);
}
READ_UNALIGNED(cu_offset, funcnames_ptr, dbg->de_length_size);
funcnames_ptr += dbg->de_length_size;
while (cu_offset != 0) {
func = (Dwarf_Func)_dwarf_get_alloc(dbg, DW_DLA_FUNC, 1);
if (func == NULL) {
_dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
return(DW_DLV_ERROR);
}
func_count++;
func->fl_context = funcnames_context;
/* Subtract length of CU header to adjust offsets. */
func->fl_cu_offset = cu_offset -
dbg->de_length_size - /* Size of CU length field. */
sizeof(Dwarf_Half) - /* Size of version stamp field. */
dbg->de_length_size - /* Size of abbrev offset field. */
sizeof(Dwarf_Small); /* Size of address size field. */
func->fl_name = funcnames_ptr;
funcnames_ptr = funcnames_ptr + strlen((char *)funcnames_ptr) + 1;
READ_UNALIGNED(cu_offset, funcnames_ptr, dbg->de_length_size);
funcnames_ptr += dbg->de_length_size;
if (funcnames_ptr > dbg->de_debug_funcnames +
dbg->de_debug_funcnames_size) {
_dwarf_error(dbg, error, DW_DLE_DEBUG_FUNCNAMES_LENGTH_BAD);
return(DW_DLV_ERROR);
}
curr_chain = (Dwarf_Chain)_dwarf_get_alloc(dbg, DW_DLA_CHAIN, 1);
if (curr_chain == NULL) {
_dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
return(DW_DLV_ERROR);
}
/* Put current function on singly_linked list. */
curr_chain->ch_item = (Dwarf_Func)func;
if (head_chain == NULL)
head_chain = prev_chain = curr_chain;
else {
prev_chain->ch_next = curr_chain;
prev_chain = curr_chain;
}
}
} while (funcnames_ptr <
dbg->de_debug_funcnames + dbg->de_debug_funcnames_size);
/* Points to contiguous block of Dwarf_Func's. */
ret_funcs = (Dwarf_Func *)
_dwarf_get_alloc(dbg, DW_DLA_LIST, func_count);
if (ret_funcs == NULL)
{_dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL); return(DW_DLV_ERROR);}
/*
Store pointers to Dwarf_Func_s structs in
contiguous block, and deallocate the chain.
*/
curr_chain = head_chain;
for (i = 0; i < func_count; i++) {
*(ret_funcs + i) = curr_chain->ch_item;
prev_chain = curr_chain;
curr_chain = curr_chain->ch_next;
dwarf_dealloc(dbg, prev_chain, DW_DLA_CHAIN);
}
*funcs = ret_funcs;
*ret_func_count = (func_count);
return DW_DLV_OK;
}
int
dwarf_funcname (
Dwarf_Func func,
char ** ret_name,
Dwarf_Error *error
)
{
if (func == NULL)
{_dwarf_error(NULL, error, DW_DLE_FUNC_NULL); return(DW_DLV_ERROR);}
*ret_name = (char *)(func->fl_name);
return DW_DLV_OK;
}
int
dwarf_func_die_offset (
Dwarf_Func func,
Dwarf_Off * return_offset,
Dwarf_Error *error
)
{
if (func == NULL)
{_dwarf_error(NULL, error, DW_DLE_FUNC_NULL); return(DW_DLV_ERROR);}
if (func->fl_context == NULL) {
_dwarf_error(NULL, error, DW_DLE_FUNC_CONTEXT_NULL);
return(DW_DLV_ERROR);
}
*return_offset = (func->fl_cu_offset + func->fl_context->fu_info_offset);
return DW_DLV_OK;
}
int
dwarf_func_cu_offset (
Dwarf_Func func,
Dwarf_Off * return_offset,
Dwarf_Error *error
)
{
if (func == NULL)
{_dwarf_error(NULL, error, DW_DLE_FUNC_NULL); return(DW_DLV_ERROR);}
if (func->fl_context == NULL) {
_dwarf_error(NULL, error, DW_DLE_FUNC_CONTEXT_NULL);
return(DW_DLV_ERROR);
}
*return_offset = (func->fl_context->fu_info_offset);
return DW_DLV_OK;
}
int
dwarf_func_name_offsets (
Dwarf_Func func,
char ** ret_func_name,
Dwarf_Off *die_offset,
Dwarf_Off *cu_offset,
Dwarf_Error *error
)
{
if (func == NULL)
{_dwarf_error(NULL, error, DW_DLE_FUNC_NULL); return(DW_DLV_ERROR);}
if (func->fl_context == NULL) {
_dwarf_error(NULL, error, DW_DLE_FUNC_CONTEXT_NULL);
return(DW_DLV_ERROR);
}
if (die_offset != NULL)
*die_offset = func->fl_cu_offset +
func->fl_context->fu_info_offset;
if (cu_offset != NULL)
*cu_offset = func->fl_context->fu_info_offset;
*ret_func_name = (char *)(func->fl_name);
return DW_DLV_OK;
}