dwarf_loc.c
11.1 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
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
#include "dwarf_incl.h"
#include "dwarf_loc.h"
/*
Given a Dwarf_Block that represents a location expression,
this function returns a pointer to a Dwarf_Locdesc struct
that has its ld_cents field set to the number of location
operators in the block, and its ld_s field pointing to a
contiguous block of Dwarf_Loc structs. However, the
ld_lopc and ld_hipc values are uninitialized. Returns
NULL on error. This function assumes that the length of
the block is greater than 0. Zero length location expressions
to represent variables that have been optimized away are
handled in the calling function.
*/
static Dwarf_Locdesc *
_dwarf_get_locdesc (
Dwarf_Debug dbg,
Dwarf_Block *loc_block,
Dwarf_Error *error
)
{
/*
Size of the block containing
the location expression.
*/
Dwarf_Unsigned loc_len;
/*
Sweeps the block containing
the location expression.
*/
Dwarf_Small *loc_ptr;
/* Current location operator. */
Dwarf_Small atom;
/* Offset of current operator from start of block. */
Dwarf_Unsigned offset;
/* Operands of current location operator. */
Dwarf_Unsigned operand1, operand2;
/* Used to chain the Dwarf_Loc_Chain_s structs. */
Dwarf_Loc_Chain curr_loc, prev_loc, head_loc = NULL;
/* Count of the number of location operators. */
Dwarf_Unsigned op_count;
/* Contiguous block of Dwarf_Loc's for Dwarf_Locdesc. */
Dwarf_Loc *block_loc;
/* Dwarf_Locdesc pointer to be returned. */
Dwarf_Locdesc *locdesc;
Dwarf_Word leb128_length;
Dwarf_Unsigned i;
/* ***** BEGIN CODE ***** */
loc_len = loc_block->bl_len;
loc_ptr = loc_block->bl_data;
offset = 0; op_count = 0;
while (offset < loc_len) {
operand1 = 0;
operand2 = 0;
op_count++;
atom = *(Dwarf_Small *)loc_ptr;
loc_ptr ++;
offset++;
curr_loc = (Dwarf_Loc_Chain)_dwarf_get_alloc(dbg, DW_DLA_LOC_CHAIN, 1);
if (curr_loc == NULL)
{_dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL); return(NULL);}
curr_loc->lc_offset = offset;
curr_loc->lc_atom = atom;
switch (atom) {
case DW_OP_reg0 :
case DW_OP_reg1 :
case DW_OP_reg2 :
case DW_OP_reg3 :
case DW_OP_reg4 :
case DW_OP_reg5 :
case DW_OP_reg6 :
case DW_OP_reg7 :
case DW_OP_reg8 :
case DW_OP_reg9 :
case DW_OP_reg10 :
case DW_OP_reg11 :
case DW_OP_reg12 :
case DW_OP_reg13 :
case DW_OP_reg14 :
case DW_OP_reg15 :
case DW_OP_reg16 :
case DW_OP_reg17 :
case DW_OP_reg18 :
case DW_OP_reg19 :
case DW_OP_reg20 :
case DW_OP_reg21 :
case DW_OP_reg22 :
case DW_OP_reg23 :
case DW_OP_reg24 :
case DW_OP_reg25 :
case DW_OP_reg26 :
case DW_OP_reg27 :
case DW_OP_reg28 :
case DW_OP_reg29 :
case DW_OP_reg30 :
case DW_OP_reg31 :
break;
case DW_OP_regx :
operand1 = _dwarf_decode_u_leb128(loc_ptr,
&leb128_length);
loc_ptr = loc_ptr + leb128_length;
offset = offset + leb128_length;
break;
case DW_OP_lit0 :
case DW_OP_lit1 :
case DW_OP_lit2 :
case DW_OP_lit3 :
case DW_OP_lit4 :
case DW_OP_lit5 :
case DW_OP_lit6 :
case DW_OP_lit7 :
case DW_OP_lit8 :
case DW_OP_lit9 :
case DW_OP_lit10 :
case DW_OP_lit11 :
case DW_OP_lit12 :
case DW_OP_lit13 :
case DW_OP_lit14 :
case DW_OP_lit15 :
case DW_OP_lit16 :
case DW_OP_lit17 :
case DW_OP_lit18 :
case DW_OP_lit19 :
case DW_OP_lit20 :
case DW_OP_lit21 :
case DW_OP_lit22 :
case DW_OP_lit23 :
case DW_OP_lit24 :
case DW_OP_lit25 :
case DW_OP_lit26 :
case DW_OP_lit27 :
case DW_OP_lit28 :
case DW_OP_lit29 :
case DW_OP_lit30 :
case DW_OP_lit31 :
operand1 = atom - DW_OP_lit0;
break;
case DW_OP_addr :
READ_UNALIGNED(operand1, loc_ptr,
dbg->de_length_size);
loc_ptr += dbg->de_length_size;
offset += dbg->de_length_size;
break;
case DW_OP_const1u :
operand1 = *(Dwarf_Small *)loc_ptr;
loc_ptr = loc_ptr + 1;
offset = offset + 1;
break;
case DW_OP_const1s :
operand1 = *(Dwarf_Sbyte *)loc_ptr;
loc_ptr = loc_ptr + 1;
offset = offset + 1;
break;
case DW_OP_const2u :
READ_UNALIGNED(operand1, loc_ptr, 2);
loc_ptr = loc_ptr + 2;
offset = offset + 2;
break;
case DW_OP_const2s :
READ_UNALIGNED(operand1, loc_ptr, 2);
loc_ptr = loc_ptr + 2;
offset = offset + 2;
break;
case DW_OP_const4u :
READ_UNALIGNED(operand1, loc_ptr, 4);
loc_ptr = loc_ptr + 4;
offset = offset + 4;
break;
case DW_OP_const4s :
READ_UNALIGNED(operand1, loc_ptr, 4);
loc_ptr = loc_ptr + 4;
offset = offset + 4;
break;
case DW_OP_const8u :
READ_UNALIGNED(operand1, loc_ptr, 8);
loc_ptr = loc_ptr + 8;
offset = offset + 8;
break;
case DW_OP_const8s :
READ_UNALIGNED(operand1, loc_ptr ,8);
loc_ptr = loc_ptr + 8;
offset = offset + 8;
break;
case DW_OP_constu :
operand1 = _dwarf_decode_u_leb128(loc_ptr,
&leb128_length);
loc_ptr = loc_ptr + leb128_length;
offset = offset + leb128_length;
break;
case DW_OP_consts :
operand1 = _dwarf_decode_s_leb128(loc_ptr,
&leb128_length);
loc_ptr = loc_ptr + leb128_length;
offset = offset + leb128_length;
break;
case DW_OP_fbreg :
operand1 = _dwarf_decode_s_leb128(loc_ptr,
&leb128_length);
loc_ptr = loc_ptr + leb128_length;
offset = offset + leb128_length;
break;
case DW_OP_breg0 :
case DW_OP_breg1 :
case DW_OP_breg2 :
case DW_OP_breg3 :
case DW_OP_breg4 :
case DW_OP_breg5 :
case DW_OP_breg6 :
case DW_OP_breg7 :
case DW_OP_breg8 :
case DW_OP_breg9 :
case DW_OP_breg10 :
case DW_OP_breg11 :
case DW_OP_breg12 :
case DW_OP_breg13 :
case DW_OP_breg14 :
case DW_OP_breg15 :
case DW_OP_breg16 :
case DW_OP_breg17 :
case DW_OP_breg18 :
case DW_OP_breg19 :
case DW_OP_breg20 :
case DW_OP_breg21 :
case DW_OP_breg22 :
case DW_OP_breg23 :
case DW_OP_breg24 :
case DW_OP_breg25 :
case DW_OP_breg26 :
case DW_OP_breg27 :
case DW_OP_breg28 :
case DW_OP_breg29 :
case DW_OP_breg30 :
case DW_OP_breg31 :
operand1 = _dwarf_decode_s_leb128(loc_ptr,
&leb128_length);
loc_ptr = loc_ptr + leb128_length;
offset = offset + leb128_length;
break;
case DW_OP_bregx :
operand1 = _dwarf_decode_s_leb128(loc_ptr,
&leb128_length);
loc_ptr = loc_ptr + leb128_length;
offset = offset + leb128_length;
operand2 = _dwarf_decode_u_leb128(loc_ptr,
&leb128_length);
loc_ptr = loc_ptr + leb128_length;
offset = offset + leb128_length;
break;
case DW_OP_dup :
case DW_OP_drop :
break;
case DW_OP_pick :
operand1 = *(Dwarf_Small *)loc_ptr;
loc_ptr = loc_ptr + 1;
offset = offset + 1;
break;
case DW_OP_over :
case DW_OP_swap :
case DW_OP_rot :
case DW_OP_deref :
break;
case DW_OP_deref_size :
operand1 = *(Dwarf_Small *)loc_ptr;
loc_ptr = loc_ptr + 1;
offset = offset + 1;
break;
case DW_OP_xderef :
break;
case DW_OP_xderef_size :
operand1 = *(Dwarf_Small *)loc_ptr;
loc_ptr = loc_ptr + 1;
offset = offset + 1;
break;
case DW_OP_abs :
case DW_OP_and :
case DW_OP_div :
case DW_OP_minus :
case DW_OP_mod :
case DW_OP_mul :
case DW_OP_neg :
case DW_OP_not :
case DW_OP_or :
case DW_OP_plus :
break;
case DW_OP_plus_uconst :
operand1 = _dwarf_decode_u_leb128(loc_ptr,
&leb128_length);
loc_ptr = loc_ptr + leb128_length;
offset = offset + leb128_length;
break;
case DW_OP_shl :
case DW_OP_shr :
case DW_OP_shra :
case DW_OP_xor :
break;
case DW_OP_le :
case DW_OP_ge :
case DW_OP_eq :
case DW_OP_lt :
case DW_OP_gt :
case DW_OP_ne :
break;
case DW_OP_skip :
case DW_OP_bra :
READ_UNALIGNED(operand1, loc_ptr, 2);
loc_ptr = loc_ptr + 2;
offset = offset + 2;
break;
case DW_OP_piece :
operand1 = _dwarf_decode_u_leb128(loc_ptr,
&leb128_length);
loc_ptr = loc_ptr + leb128_length;
offset = offset + leb128_length;
break;
case DW_OP_nop :
break;
default :
_dwarf_error(dbg, error, DW_DLE_LOC_EXPR_BAD);
return(NULL);
break;
}
curr_loc->lc_number = operand1;
curr_loc->lc_number2 = operand2;
if (head_loc == NULL)
head_loc = prev_loc = curr_loc;
else {
prev_loc->lc_next = curr_loc;
prev_loc = curr_loc;
}
}
block_loc =
(Dwarf_Loc *)_dwarf_get_alloc(dbg, DW_DLA_LOC_BLOCK, op_count);
if (block_loc == NULL)
{_dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL); return(NULL);}
curr_loc = head_loc;
for (i = 0; i < op_count; i++) {
(block_loc + i)->lr_atom = curr_loc->lc_atom;
(block_loc + i)->lr_number = curr_loc->lc_number;
(block_loc + i)->lr_number2 = curr_loc->lc_number2;
(block_loc + i)->lr_offset = curr_loc->lc_offset;
prev_loc = curr_loc;
curr_loc = curr_loc->lc_next;
dwarf_dealloc(dbg, prev_loc, DW_DLA_LOC_CHAIN);
}
locdesc = (Dwarf_Locdesc *)_dwarf_get_alloc(dbg, DW_DLA_LOCDESC, 1);
if (locdesc == NULL)
{_dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL); return(NULL);}
locdesc->ld_cents = op_count;
locdesc->ld_s = block_loc;
return(locdesc);
}
/*
Handles only location expressions at the moment.
*/
int
dwarf_loclist (
Dwarf_Attribute attr,
Dwarf_Locdesc **llbuf,
Dwarf_Signed *listlen,
Dwarf_Error *error
)
{
/* Dwarf_Debug of input die. */
Dwarf_Debug dbg;
/*
Dwarf_Attribute that describes the
DW_AT_location in die, if present.
*/
Dwarf_Attribute loc_attr = attr;
/*
Pointer to Dwarf_Block that describes
the location expression.
*/
Dwarf_Block *loc_block;
/* A pointer to the current Dwarf_Locdesc read. */
Dwarf_Locdesc *locdesc;
int blkres;
/* ***** BEGIN CODE ***** */
if (loc_attr == NULL) {
_dwarf_error(NULL,error,DW_DLE_ATTR_NULL);
return(DW_DLV_ERROR);
}
if (loc_attr->ar_cu_context == NULL) {
_dwarf_error(NULL,error,DW_DLE_ATTR_NO_CU_CONTEXT);
return(DW_DLV_ERROR);
}
dbg = attr->ar_cu_context->cc_dbg;
if (dbg == NULL) {
_dwarf_error(NULL,error,DW_DLE_ATTR_DBG_NULL);
return(DW_DLV_ERROR);
}
blkres = dwarf_formblock(loc_attr,&loc_block, error);
if (blkres != DW_DLV_OK ) {
return(blkres);
}
/* Don't know what to do for 0 length location expressions. */
if (loc_block->bl_len != 0) {
locdesc = _dwarf_get_locdesc(dbg, loc_block, error);
if (locdesc == NULL) {
/* low level error already set: let it be passed back */
return(DW_DLV_ERROR);
}
*llbuf = locdesc;
dwarf_dealloc(dbg, loc_block, DW_DLA_BLOCK);
*listlen = 1;
return(DW_DLV_OK);
}
/* strange situation: internal error? */
_dwarf_error(dbg, error, DW_DLE_LOC_EXPR_BAD);
return DW_DLV_ERROR;
}