dwarf_weaks.c
6.73 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
265
266
267
#include <stdio.h>
#include "dwarf_incl.h"
#include "dwarf_weaks.h"
int
dwarf_get_weaks (
Dwarf_Debug dbg,
Dwarf_Weak **weaks,
Dwarf_Signed *ret_weak_count,
Dwarf_Error *error
)
{
/* Sweeps the complete .debug_weaknames section. */
Dwarf_Small *weaknames_ptr;
Dwarf_Word length;
/*
Points to the context for the current set of weak names,
and contains information to identify the compilation-unit
that the set refers to.
*/
Dwarf_Weak_Context weaknames_context;
/* Version number for the current set of weak names. */
Dwarf_Half version;
/*
Offset from the start of compilation-unit
for the current weak name.
*/
Dwarf_Off cu_offset;
/* Counts the number of weak names read. */
Dwarf_Unsigned weak_count = 0;
/* Points to the current weak name read. */
Dwarf_Weak weak;
/*
Used to chain the Dwarf_Weak_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_Weak's to be returned. */
Dwarf_Weak *ret_weaks;
/* 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_weaknames == NULL) {
return(DW_DLV_NO_ENTRY);
}
weaknames_ptr = dbg->de_debug_weaknames;
do {
weaknames_context = (Dwarf_Weak_Context)
_dwarf_get_alloc(dbg, DW_DLA_WEAK_CONTEXT, 1);
if (weaknames_context == NULL) {
_dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
return(DW_DLV_ERROR);
}
READ_UNALIGNED(length, weaknames_ptr, dbg->de_length_size);
weaknames_ptr += dbg->de_length_size;
weaknames_context->wk_length = length;
READ_UNALIGNED(version, weaknames_ptr, sizeof(Dwarf_Half));
weaknames_ptr += sizeof(Dwarf_Half);
if (version != CURRENT_VERSION_STAMP) {
_dwarf_error(dbg, error, DW_DLE_DEBUG_WEAKNAMES_VERSION_ERROR);
return(DW_DLV_ERROR);
}
READ_UNALIGNED(weaknames_context->wk_info_offset, weaknames_ptr,
dbg->de_length_size);
weaknames_ptr += dbg->de_length_size;
/*
Add the length of the cu_header to point to the
DW_TAG_compile_unit die.
*/
weaknames_context->wk_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(weaknames_context->wk_info_length, weaknames_ptr,
dbg->de_length_size);
weaknames_ptr += dbg->de_length_size;
if (weaknames_ptr > dbg->de_debug_weaknames +
dbg->de_debug_weaknames_size) {
_dwarf_error(dbg, error, DW_DLE_DEBUG_WEAKNAMES_LENGTH_BAD);
return(DW_DLV_ERROR);
}
READ_UNALIGNED(cu_offset, weaknames_ptr, dbg->de_length_size);
weaknames_ptr += dbg->de_length_size;
while (cu_offset != 0) {
weak = (Dwarf_Weak)_dwarf_get_alloc(dbg, DW_DLA_WEAK, 1);
if (weak == NULL) {
_dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
return(DW_DLV_ERROR);
}
weak_count++;
weak->we_context = weaknames_context;
/* Subtract length of CU header to adjust offsets. */
weak->we_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. */
weak->we_name = weaknames_ptr;
weaknames_ptr = weaknames_ptr + strlen((char *)weaknames_ptr) + 1;
READ_UNALIGNED(cu_offset, weaknames_ptr, dbg->de_length_size);
weaknames_ptr += dbg->de_length_size;
if (weaknames_ptr > dbg->de_debug_weaknames +
dbg->de_debug_weaknames_size) {
_dwarf_error(dbg, error, DW_DLE_DEBUG_WEAKNAMES_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 weak name on singly_linked list. */
curr_chain->ch_item = (Dwarf_Weak)weak;
if (head_chain == NULL)
head_chain = prev_chain = curr_chain;
else {
prev_chain->ch_next = curr_chain;
prev_chain = curr_chain;
}
}
} while (weaknames_ptr <
dbg->de_debug_weaknames + dbg->de_debug_weaknames_size);
/* Points to contiguous block of Dwarf_Weak's. */
ret_weaks = (Dwarf_Weak *)
_dwarf_get_alloc(dbg, DW_DLA_LIST, weak_count);
if (ret_weaks == NULL) {
_dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
return(DW_DLV_ERROR);
}
/*
Store pointers to Dwarf_Weak_s structs in
contiguous block, and deallocate the chain.
*/
curr_chain = head_chain;
for (i = 0; i < weak_count; i++) {
*(ret_weaks + i) = curr_chain->ch_item;
prev_chain = curr_chain;
curr_chain = curr_chain->ch_next;
dwarf_dealloc(dbg, prev_chain, DW_DLA_CHAIN);
}
*weaks = ret_weaks;
*ret_weak_count = (weak_count);
return DW_DLV_OK;
}
int
dwarf_weakname (
Dwarf_Weak weak,
char ** ret_name,
Dwarf_Error *error
)
{
if (weak == NULL) {
_dwarf_error(NULL, error, DW_DLE_WEAK_NULL);
return(DW_DLV_ERROR);
}
*ret_name = (char *)(weak->we_name);
return DW_DLV_OK;
}
int
dwarf_weak_die_offset (
Dwarf_Weak weak,
Dwarf_Off *weak_off,
Dwarf_Error *error
)
{
if (weak == NULL)
{_dwarf_error(NULL, error, DW_DLE_WEAK_NULL);
return(DW_DLV_ERROR);}
if (weak->we_context == NULL) {
_dwarf_error(NULL, error, DW_DLE_WEAK_CONTEXT_NULL);
return(DW_DLV_ERROR);
}
*weak_off = (weak->we_cu_offset + weak->we_context->wk_info_offset);
return DW_DLV_OK;
}
int
dwarf_weak_cu_offset (
Dwarf_Weak weak,
Dwarf_Off *weak_off,
Dwarf_Error *error
)
{
if (weak == NULL)
{_dwarf_error(NULL, error, DW_DLE_WEAK_NULL); return(DW_DLV_ERROR);}
if (weak->we_context == NULL) {
_dwarf_error(NULL, error, DW_DLE_WEAK_CONTEXT_NULL);
return(DW_DLV_ERROR);
}
*weak_off = (weak->we_context->wk_info_offset);
return DW_DLV_OK;
}
int
dwarf_weak_name_offsets (
Dwarf_Weak weak,
char ** weak_name,
Dwarf_Off *die_offset,
Dwarf_Off *cu_offset,
Dwarf_Error *error
)
{
if (weak == NULL)
{_dwarf_error(NULL, error, DW_DLE_WEAK_NULL); return(DW_DLV_ERROR);}
if (weak->we_context == NULL)
{_dwarf_error(NULL, error, DW_DLE_WEAK_CONTEXT_NULL);
return(DW_DLV_ERROR);}
if (die_offset != NULL)
*die_offset = weak->we_cu_offset +
weak->we_context->wk_info_offset;
if (cu_offset != NULL)
*cu_offset = weak->we_context->wk_info_offset;
*weak_name = (char *)(weak->we_name);
return DW_DLV_OK;
}