/
DwarfFunctions.cpp
530 lines (506 loc) · 13.4 KB
/
DwarfFunctions.cpp
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/*******************************************************************************
* Copyright (c) 2015, 2019 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at http://eclipse.org/legal/epl-2.0
* or the Apache License, Version 2.0 which accompanies this distribution
* and is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License, v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception [1] and GNU General Public
* License, version 2 with the OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] http://openjdk.java.net/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
*******************************************************************************/
#include "ddr/scanner/dwarf/DwarfFunctions.hpp"
/* Statics to define */
static const char * const _errmsgs[] = {
"No error (0)",
"DW_DLE_ATTR_FORM_BAD (1)",
"DW_DLE_BADOFF (2) Invalid offset",
"DW_DLE_DIE_NULL (3)",
"DW_DLE_FNO (4) file not open",
"DW_DLE_IA (5) invalid argument",
"DW_DLE_IOF (6) I/O failure",
"DW_DLE_MAF (7) memory allocation failure",
"DW_DLE_NOB (8) not an object file or dSYM bundle not found",
"DW_DLE_VMM (9) dwarf format/library version mismatch",
};
vector<string> Dwarf_CU_Context::_fileList;
unordered_map<Dwarf_Off, Dwarf_Die> Dwarf_Die_s::refMap;
int
dwarf_srcfiles(Dwarf_Die die, char ***srcfiles, Dwarf_Signed *filecount, Dwarf_Error *error)
{
int ret = DW_DLV_OK;
if (NULL == die) {
ret = DW_DLV_ERROR;
setError(error, DW_DLE_DIE_NULL);
} else if ((NULL == filecount) || (NULL == srcfiles)) {
ret = DW_DLV_ERROR;
setError(error, DW_DLE_IA);
} else {
/* Copy the list of file names found during dwarf_init.
* This implementation maintains the entire list through
* all CU's, rather than just the current CU, for simplicity.
*/
*filecount = Dwarf_CU_Context::_fileList.size();
*srcfiles = new char *[*filecount];
if (NULL == *srcfiles) {
ret = DW_DLV_ERROR;
setError(error, DW_DLE_MAF);
} else {
size_t index = 0;
for (str_vect::iterator it = Dwarf_CU_Context::_fileList.begin(); it != Dwarf_CU_Context::_fileList.end(); ++it) {
(*srcfiles)[index] = strdup(it->c_str());
if (NULL == (*srcfiles)[index]) {
ret = DW_DLV_ERROR;
setError(error, DW_DLE_MAF);
break;
}
index += 1;
}
}
}
return ret;
}
int
dwarf_attr(
Dwarf_Die die,
Dwarf_Half attr,
Dwarf_Attribute *return_attr,
Dwarf_Error *error)
{
int ret = DW_DLV_NO_ENTRY;
if (NULL == die) {
setError(error, DW_DLE_DIE_NULL);
ret = DW_DLV_ERROR;
} else if (NULL == return_attr) {
setError(error, DW_DLE_DIE_NULL);
ret = DW_DLE_IA;
} else {
/* Iterate the Die's attributes to look for one with
* a type matching 'attr' and return it.
*/
*return_attr = NULL;
Dwarf_Attribute attrCurrent = die->_attribute;
while (NULL != attrCurrent) {
if (attr == attrCurrent->_type) {
*return_attr = attrCurrent;
ret = DW_DLV_OK;
break;
}
attrCurrent = attrCurrent->_nextAttr;
}
}
return ret;
}
char *
dwarf_errmsg(Dwarf_Error error)
{
return (char *)_errmsgs[error->_errno];
}
int
dwarf_formstring(Dwarf_Attribute attr, char **returned_string, Dwarf_Error *error)
{
int ret = DW_DLV_OK;
if ((NULL == attr) || (NULL == returned_string)) {
ret = DW_DLV_ERROR;
setError(error, DW_DLE_IA);
} else if (DW_FORM_string != attr->_form) {
ret = DW_DLV_ERROR;
setError(error, DW_DLE_ATTR_FORM_BAD);
} else {
/* Return attribute data. */
*returned_string = attr->_stringdata;
}
return ret;
}
void
dwarf_dealloc(Dwarf_Debug dbg, void *space, Dwarf_Unsigned type)
{
/* Since only the dwarf_init() function allocates anything,
* all memory is freed in dwarf_finish(). Freeing any data
* in part could lead to not being able to free other data
* late and would prevent using the Die's that rely on the
* freed data. Just as in the dwarf library, dwarf_dealloc()
* is a no-op for Dies and Attributes.
*/
if (DW_DLA_STRING == type) {
free((char *)space);
} else if (DW_DLA_LIST == type) {
delete[] (void **)space;
} else if (DW_DLA_ERROR == type) {
delete (Dwarf_Error)space;
}
}
int
dwarf_hasattr(Dwarf_Die die, Dwarf_Half attr, Dwarf_Bool *returned_bool, Dwarf_Error *error)
{
int ret = DW_DLV_OK;
if (NULL == die) {
ret = DW_DLV_ERROR;
setError(error, DW_DLE_DIE_NULL);
} else if (NULL == returned_bool) {
ret = DW_DLV_ERROR;
setError(error, DW_DLE_IA);
} else {
/* Iterate the Die's attributes to check if it has an
* attribute of a given type.
*/
*returned_bool = false;
Dwarf_Attribute attrCurrent = die->_attribute;
while (NULL != attrCurrent) {
if (attr == attrCurrent->_type) {
*returned_bool = true;
break;
}
attrCurrent = attrCurrent->_nextAttr;
}
}
return ret;
}
int
dwarf_formblock(Dwarf_Attribute attr, Dwarf_Block **returned_block, Dwarf_Error *error)
{
/* No attributes are generated with block forms on OSX. */
if ((NULL == attr) || (NULL == returned_block)) {
setError(error, DW_DLE_IA);
} else {
setError(error, DW_DLE_ATTR_FORM_BAD);
}
return DW_DLV_ERROR;
}
int
dwarf_formflag(Dwarf_Attribute attr, Dwarf_Bool *returned_flag, Dwarf_Error *error)
{
int ret = DW_DLV_OK;
if ((NULL == attr) || (NULL == returned_flag)) {
ret = DW_DLV_ERROR;
setError(error, DW_DLE_IA);
} else if (DW_FORM_flag != attr->_form) {
ret = DW_DLV_ERROR;
setError(error, DW_DLE_ATTR_FORM_BAD);
} else {
/* Return attribute flag. */
*returned_flag = attr->_flag;
}
return ret;
}
int
dwarf_formudata(Dwarf_Attribute attr, Dwarf_Unsigned *returned_val, Dwarf_Error *error)
{
int ret = DW_DLV_OK;
if ((NULL == attr) || (NULL == returned_val)) {
ret = DW_DLV_ERROR;
setError(error, DW_DLE_IA);
} else if (DW_FORM_udata != attr->_form) {
ret = DW_DLV_ERROR;
setError(error, DW_DLE_ATTR_FORM_BAD);
} else {
/* Return attribute data. */
*returned_val = attr->_udata;
}
return ret;
}
int
dwarf_diename(Dwarf_Die die, char **diename, Dwarf_Error *error)
{
int ret = DW_DLV_OK;
if (NULL == die) {
ret = DW_DLV_ERROR;
setError(error, DW_DLE_DIE_NULL);
} else if (NULL == diename) {
ret = DW_DLV_ERROR;
setError(error, DW_DLE_IA);
} else {
/* Get the name attribute. */
Dwarf_Attribute attr = NULL;
ret = dwarf_attr(die, DW_AT_name, &attr, error);
char *name = NULL;
if (DW_DLV_OK == ret) {
ret = dwarf_formstring(attr, &name, error);
}
/* If the Die has a name attribute, copy the name string. */
if (DW_DLV_OK == ret) {
*diename = strdup(name);
if (NULL == *diename) {
ret = DW_DLV_ERROR;
setError(error, DW_DLE_MAF);
}
} else if (DW_DLV_NO_ENTRY == ret) {
*diename = strdup("");
if (NULL == *diename) {
ret = DW_DLV_ERROR;
setError(error, DW_DLE_MAF);
}
}
}
return ret;
}
int
dwarf_global_formref(Dwarf_Attribute attr, Dwarf_Off *return_offset, Dwarf_Error *error)
{
int ret = DW_DLV_OK;
if ((NULL == attr) || (NULL == return_offset)) {
ret = DW_DLV_ERROR;
setError(error, DW_DLE_IA);
} else if ((DW_FORM_ref1 > attr->_form) || (DW_FORM_ref8 < attr->_form)) {
ret = DW_DLV_ERROR;
setError(error, DW_DLE_ATTR_FORM_BAD);
} else {
/* Return attribute data. */
*return_offset = attr->_refdata;
}
return ret;
}
int
dwarf_offdie_b(Dwarf_Debug dbg,
Dwarf_Off offset,
Dwarf_Bool is_info,
Dwarf_Die *return_die,
Dwarf_Error *error)
{
int ret = DW_DLV_OK;
if (NULL == return_die) {
ret = DW_DLV_ERROR;
setError(error, DW_DLE_IA);
} else {
unordered_map<Dwarf_Off, Dwarf_Die>::const_iterator iter = Dwarf_Die_s::refMap.find(offset);
if ((Dwarf_Die_s::refMap.end() != iter) && (NULL != iter->second)) {
/* Return any Die from its global offset. */
*return_die = iter->second;
} else {
ret = DW_DLV_NO_ENTRY;
setError(error, DW_DLE_BADOFF);
}
}
return ret;
}
int
dwarf_child(Dwarf_Die die, Dwarf_Die *return_childdie, Dwarf_Error *error)
{
int ret = DW_DLV_OK;
if (NULL == die) {
ret = DW_DLV_ERROR;
setError(error, DW_DLE_DIE_NULL);
} else if (NULL == return_childdie) {
ret = DW_DLE_IA;
setError(error, DW_DLE_DIE_NULL);
} else if (NULL == die->_child) {
ret = DW_DLV_NO_ENTRY;
*return_childdie = NULL;
} else {
/* Return the Die's first child. */
*return_childdie = die->_child;
}
return ret;
}
int
dwarf_whatform(Dwarf_Attribute attr, Dwarf_Half *returned_final_form, Dwarf_Error *error)
{
int ret = DW_DLV_OK;
if ((NULL == attr) || (NULL == returned_final_form)) {
ret = DW_DLV_ERROR;
setError(error, DW_DLE_IA);
} else {
/* Get the data form of an attribute. */
*returned_final_form = attr->_form;
}
return ret;
}
int
dwarf_tag(Dwarf_Die die, Dwarf_Half *return_tag, Dwarf_Error *error)
{
int ret = DW_DLV_OK;
if (NULL == die) {
ret = DW_DLV_ERROR;
setError(error, DW_DLE_DIE_NULL);
} else if (NULL == return_tag) {
ret = DW_DLV_ERROR;
setError(error, DW_DLE_IA);
} else {
/* Get a Die's tag, indicating what type of Die it is. */
*return_tag = die->_tag;
}
return ret;
}
int
dwarf_formsdata(Dwarf_Attribute attr, Dwarf_Signed *returned_val, Dwarf_Error *error)
{
int ret = DW_DLV_OK;
if ((NULL == attr) || (NULL == returned_val)) {
ret = DW_DLV_ERROR;
setError(error, DW_DLE_IA);
} else if (DW_FORM_sdata != attr->_form) {
ret = DW_DLV_ERROR;
setError(error, DW_DLE_ATTR_FORM_BAD);
} else {
/* Return attribute data. */
*returned_val = attr->_sdata;
}
return ret;
}
int
dwarf_next_cu_header(Dwarf_Debug dbg,
Dwarf_Unsigned *cu_header_length,
Dwarf_Half *version_stamp,
Dwarf_Off *abbrev_offset,
Dwarf_Half *address_size,
Dwarf_Unsigned *next_cu_header_offset,
Dwarf_Error *error)
{
/* Advance the current CU and return its properties. */
int ret = DW_DLV_OK;
if (NULL == Dwarf_CU_Context::_currentCU) {
Dwarf_CU_Context::_currentCU = Dwarf_CU_Context::_firstCU;
} else if (NULL == Dwarf_CU_Context::_currentCU->_nextCU) {
ret = DW_DLV_NO_ENTRY;
} else {
Dwarf_CU_Context::_currentCU = Dwarf_CU_Context::_currentCU->_nextCU;
}
if (DW_DLV_OK == ret) {
*cu_header_length = Dwarf_CU_Context::_currentCU->_CUheaderLength;
*version_stamp = Dwarf_CU_Context::_currentCU->_versionStamp;
*abbrev_offset = Dwarf_CU_Context::_currentCU->_abbrevOffset;
*address_size = Dwarf_CU_Context::_currentCU->_addressSize;
*next_cu_header_offset = Dwarf_CU_Context::_currentCU->_nextCUheaderOffset;
}
return ret;
}
int
dwarf_siblingof(Dwarf_Debug dbg, Dwarf_Die die, Dwarf_Die *dieOut, Dwarf_Error *error)
{
int ret = DW_DLV_OK;
if (NULL == dieOut) {
ret = DW_DLV_ERROR;
setError(error, DW_DLE_IA);
} else if (NULL == die) {
*dieOut = Dwarf_CU_Context::_currentCU->_die;
} else if (NULL == die->_sibling) {
*dieOut = NULL;
ret = DW_DLV_NO_ENTRY;
} else {
/* Get the next sibling of a Die. */
*dieOut = die->_sibling;
}
return ret;
}
int
dwarf_dieoffset(Dwarf_Die die, Dwarf_Off *dieOffset, Dwarf_Error *error)
{
int ret = DW_DLV_OK;
if (NULL == dieOffset) {
ret = DW_DLV_ERROR;
setError(error, DW_DLE_IA);
} else if (NULL == die) {
ret = DW_DLV_ERROR;
setError(error, DW_DLE_DIE_NULL);
} else {
/* Get the address of a Die. */
/* Note that the returned offset is not reversible using dwarf_offdie_b,
* but just needs to be unique per Die.
*/
*dieOffset = (Dwarf_Off)die;
}
return ret;
}
int
dwarf_get_TAG_name(Dwarf_Half tag, const char **name)
{
int ret = DW_DLV_OK;
if (NULL == name) {
ret = DW_DLV_ERROR;
} else {
switch (tag) {
case DW_TAG_unknown:
*name = "DW_TAG_unknown";
break;
case DW_TAG_array_type:
*name = "DW_TAG_array_type";
break;
case DW_TAG_base_type:
*name = "DW_TAG_base_type";
break;
case DW_TAG_class_type:
*name = "DW_TAG_class_type";
break;
case DW_TAG_compile_unit:
*name = "DW_TAG_compile_unit";
break;
case DW_TAG_const_type:
*name = "DW_TAG_const_type";
break;
case DW_TAG_enumeration_type:
*name = "DW_TAG_enumeration_type";
break;
case DW_TAG_enumerator:
*name = "DW_TAG_enumerator";
break;
case DW_TAG_inheritance:
*name = "DW_TAG_inheritance";
break;
case DW_TAG_member:
*name = "DW_TAG_member";
break;
case DW_TAG_namespace:
*name = "DW_TAG_namespace";
break;
case DW_TAG_pointer_type:
*name = "DW_TAG_pointer_type";
break;
case DW_TAG_ptr_to_member_type:
*name = "DW_TAG_ptr_to_member_type";
break;
case DW_TAG_restrict_type:
*name = "DW_TAG_restrict_type";
break;
case DW_TAG_reference_type:
*name = "DW_TAG_reference_type";
break;
case DW_TAG_shared_type:
*name = "DW_TAG_shared_type";
break;
case DW_TAG_structure_type:
*name = "DW_TAG_structure_type";
break;
case DW_TAG_subprogram:
*name = "DW_TAG_subprogram";
break;
case DW_TAG_subrange_type:
*name = "DW_TAG_subrange_type";
break;
case DW_TAG_subroutine_type:
*name = "DW_TAG_subroutine_type";
break;
case DW_TAG_typedef:
*name = "DW_TAG_typedef";
break;
case DW_TAG_union_type:
*name = "DW_TAG_union_type";
break;
case DW_TAG_volatile_type:
*name = "DW_TAG_volatile_type";
break;
default:
*name = "unknown";
break;
}
}
return ret;
}
void
setError(Dwarf_Error *error, Dwarf_Half num)
{
if (NULL != error) {
*error = new Dwarf_Error_s;
if (NULL != *error) {
(*error)->_errno = num;
}
}
}