File: pro_expr.c

package info (click to toggle)
dwarfutils 20180809-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 8,228 kB
  • sloc: ansic: 63,382; sh: 5,222; cpp: 4,041; makefile: 548; python: 445; awk: 11
file content (574 lines) | stat: -rw-r--r-- 16,276 bytes parent folder | download
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/*
  Copyright (C) 2000,2004,2006 Silicon Graphics, Inc.  All Rights Reserved.
  Portions Copyright 2007-2010 Sun Microsystems, Inc. All rights reserved.
  Portions Copyright 2011 David Anderson. All rights reserved.

  This program is free software; you can redistribute it and/or modify it
  under the terms of version 2.1 of the GNU Lesser General Public License
  as published by the Free Software Foundation.

  This program is distributed in the hope that it would be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

  Further, this software is distributed without any warranty that it is
  free of the rightful claim of any third person regarding infringement
  or the like.  Any license provided herein, whether implied or
  otherwise, applies only to this software file.  Patent licenses, if
  any, provided herein do not apply to combinations of this program with
  other software, or any other product whatsoever.

  You should have received a copy of the GNU Lesser General Public
  License along with this program; if not, write the Free Software
  Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston MA 02110-1301,
  USA.

*/

#include "config.h"
#include "libdwarfdefs.h"
#include <stdio.h>
#include <string.h>
#include "pro_incl.h"
#include "pro_expr.h"

/*
    This function creates a new expression
    struct that can be used to build up a
    location expression.
*/
Dwarf_P_Expr
dwarf_new_expr(Dwarf_P_Debug dbg, Dwarf_Error * error)
{
    Dwarf_P_Expr ret_expr;

    if (dbg == NULL) {
        _dwarf_p_error(NULL, error, DW_DLE_DBG_NULL);
        return (NULL);
    }

    ret_expr = (Dwarf_P_Expr)
        _dwarf_p_get_alloc(dbg, sizeof(struct Dwarf_P_Expr_s));
    if (ret_expr == NULL) {
        _dwarf_p_error(dbg, error, DW_DLE_ALLOC_FAIL);
        return (NULL);
    }

    ret_expr->ex_dbg = dbg;

    return (ret_expr);
}


Dwarf_Unsigned
dwarf_add_expr_gen(Dwarf_P_Expr expr,
    Dwarf_Small opcode,
    Dwarf_Unsigned val1,
    Dwarf_Unsigned val2, Dwarf_Error * error)
{
    /* 2* since used to concatenate 2 leb's below */
    char encode_buffer[2 * ENCODE_SPACE_NEEDED];

    char encode_buffer2[ENCODE_SPACE_NEEDED];
    int res = 0;
    Dwarf_P_Debug dbg = 0;

    /*  Give the buffer where the operands are first going to be
        assembled the largest alignment. */
    Dwarf_Unsigned operand_buffer[10];

    /* Size of the byte stream buffer that needs to be memcpy-ed. */
    int operand_size = 0;

    /*  Points to the byte stream for the first operand, and finally to
        the buffer that is memcp-ed into the Dwarf_P_Expr_s struct. */
    Dwarf_Small *operand = 0;

    /*  Size of the byte stream for second operand. */
    int operand2_size = 0;

    /*  Points to next byte to be written in Dwarf_P_Expr_s struct. */
    Dwarf_Small *next_byte_ptr = 0;

    /*  Offset past the last byte written into Dwarf_P_Expr_s. */
    int next_byte_offset = 0;

    /* ***** BEGIN CODE ***** */

    if (expr == NULL) {
        _dwarf_p_error(NULL, error, DW_DLE_EXPR_NULL);
        return (DW_DLV_NOCOUNT);
    }
    dbg = expr->ex_dbg;

    if (expr->ex_dbg == NULL) {
        _dwarf_p_error(NULL, error, DW_DLE_DBG_NULL);
        return (DW_DLV_NOCOUNT);
    }

    operand = NULL;
    operand_size = 0;

    switch (opcode) {
    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_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:
        res = _dwarf_pro_encode_signed_leb128_nm(val1,
            &operand_size, encode_buffer, sizeof(encode_buffer));
        if (res != DW_DLV_OK) {
            _dwarf_p_error(expr->ex_dbg, error, DW_DLE_EXPR_LENGTH_BAD);
            return (DW_DLV_NOCOUNT);
        }
        operand = (Dwarf_Small *) encode_buffer;
        break;

    case DW_OP_regx:
        res = _dwarf_pro_encode_leb128_nm(val1, &operand_size,
            encode_buffer, sizeof(encode_buffer));
        if (res != DW_DLV_OK) {
            _dwarf_p_error(expr->ex_dbg, error, DW_DLE_EXPR_LENGTH_BAD);
            return (DW_DLV_NOCOUNT);
        }
        operand = (Dwarf_Small *) encode_buffer;
        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:
        break;

    case DW_OP_addr:
        _dwarf_p_error(expr->ex_dbg, error, DW_DLE_BAD_EXPR_OPCODE);
        return (DW_DLV_NOCOUNT);

    case DW_OP_const1u:
    case DW_OP_const1s:
        operand = (Dwarf_Small *) & operand_buffer[0];
        WRITE_UNALIGNED(dbg, operand, &val1, sizeof(val1), 1);
        operand_size = 1;
        break;

    case DW_OP_const2u:
    case DW_OP_const2s:
        operand = (Dwarf_Small *) & operand_buffer[0];
        WRITE_UNALIGNED(dbg, operand, &val1, sizeof(val1), 2);
        operand_size = 2;
        break;

    case DW_OP_const4u:
    case DW_OP_const4s:
        operand = (Dwarf_Small *) & operand_buffer[0];
        WRITE_UNALIGNED(dbg, operand, &val1, sizeof(val1), 4);
        operand_size = 4;
        break;

    case DW_OP_const8u:
    case DW_OP_const8s:
        operand = (Dwarf_Small *) & operand_buffer[0];
        WRITE_UNALIGNED(dbg, operand, &val1, sizeof(val1), 8);
        operand_size = 8;
        break;

    case DW_OP_constu:
        res = _dwarf_pro_encode_leb128_nm(val1,
            &operand_size, encode_buffer, sizeof(encode_buffer));
        if (res != DW_DLV_OK) {
            _dwarf_p_error(expr->ex_dbg, error, DW_DLE_EXPR_LENGTH_BAD);
            return (DW_DLV_NOCOUNT);
        }
        operand = (Dwarf_Small *) encode_buffer;
        break;

    case DW_OP_consts:
        res = _dwarf_pro_encode_signed_leb128_nm(val1,
            &operand_size,
            encode_buffer,
            sizeof(encode_buffer));
        if (res != DW_DLV_OK) {
            _dwarf_p_error(expr->ex_dbg, error, DW_DLE_EXPR_LENGTH_BAD);
            return (DW_DLV_NOCOUNT);
        }
        operand = (Dwarf_Small *) encode_buffer;
        break;

    case DW_OP_fbreg:
        res = _dwarf_pro_encode_signed_leb128_nm(val1,
            &operand_size,
            encode_buffer,
            sizeof(encode_buffer));
        if (res != DW_DLV_OK) {
            _dwarf_p_error(expr->ex_dbg, error, DW_DLE_EXPR_LENGTH_BAD);
            return (DW_DLV_NOCOUNT);
        }
        operand = (Dwarf_Small *) encode_buffer;
        break;

    case DW_OP_bregx:
        res = _dwarf_pro_encode_leb128_nm(val1, &operand_size,
            encode_buffer,
            sizeof(encode_buffer));
        if (res != DW_DLV_OK) {
            _dwarf_p_error(expr->ex_dbg, error, DW_DLE_EXPR_LENGTH_BAD);
            return (DW_DLV_NOCOUNT);
        }
        operand = (Dwarf_Small *) encode_buffer;
        /* put this one directly into 'operand' at tail of prev value */
        res = _dwarf_pro_encode_signed_leb128_nm(val2, &operand2_size,
            ((char *) operand) +
            operand_size,
            sizeof(encode_buffer2));
        if (res != DW_DLV_OK) {
            _dwarf_p_error(expr->ex_dbg, error, DW_DLE_EXPR_LENGTH_BAD);
            return (DW_DLV_NOCOUNT);
        }
        operand_size += operand2_size;

    case DW_OP_dup:
    case DW_OP_drop:
        break;

    case DW_OP_pick:
        operand = (Dwarf_Small *) & operand_buffer[0];
        WRITE_UNALIGNED(dbg, operand, (const void *) &val1,
            sizeof(val1), 1);
        operand_size = 1;
        break;

    case DW_OP_over:
    case DW_OP_swap:
    case DW_OP_rot:
    case DW_OP_deref:
    case DW_OP_xderef:
        break;

    case DW_OP_deref_size:
    case DW_OP_xderef_size:
        operand = (Dwarf_Small *) & operand_buffer[0];
        WRITE_UNALIGNED(dbg, operand, (const void *) &val1,
            sizeof(val1), 1);
        operand_size = 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:
        res = _dwarf_pro_encode_leb128_nm(val1, &operand_size,
            encode_buffer,
            sizeof(encode_buffer));
        if (res != DW_DLV_OK) {
            _dwarf_p_error(expr->ex_dbg, error, DW_DLE_EXPR_LENGTH_BAD);
            return (DW_DLV_NOCOUNT);
        }
        operand = (Dwarf_Small *) encode_buffer;
        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:
        /* FIX: unhandled! OP_bra, OP_skip! */
        _dwarf_p_error(expr->ex_dbg, error, DW_DLE_BAD_EXPR_OPCODE);
        return (DW_DLV_NOCOUNT);

    case DW_OP_piece:
        res = _dwarf_pro_encode_leb128_nm(val1, &operand_size,
            encode_buffer,
            sizeof(encode_buffer));
        if (res != DW_DLV_OK) {
            _dwarf_p_error(expr->ex_dbg, error, DW_DLE_EXPR_LENGTH_BAD);
            return (DW_DLV_NOCOUNT);
        }
        operand = (Dwarf_Small *) encode_buffer;
        break;

    case DW_OP_nop:
        break;
    case DW_OP_push_object_address:     /* DWARF3 */
        break;
    case DW_OP_call2:           /* DWARF3 */
        operand = (Dwarf_Small *) & operand_buffer[0];
        WRITE_UNALIGNED(dbg, operand, &val1, sizeof(val1), 2);
        operand_size = 2;
        break;

    case DW_OP_call4:           /* DWARF3 */
        operand = (Dwarf_Small *) & operand_buffer[0];
        WRITE_UNALIGNED(dbg, operand, &val1, sizeof(val1), 4);
        operand_size = 4;
        break;

    case DW_OP_call_ref:        /* DWARF3 */
        operand = (Dwarf_Small *) & operand_buffer[0];
        WRITE_UNALIGNED(dbg, operand, &val1, sizeof(val1),
            dbg->de_offset_size);
        operand_size = dbg->de_offset_size;
        break;
    case DW_OP_form_tls_address:        /* DWARF3f */
        break;
    case DW_OP_call_frame_cfa:  /* DWARF3f */
        break;
    case DW_OP_bit_piece:       /* DWARF3f */
        res = _dwarf_pro_encode_leb128_nm(val1, &operand_size,
            encode_buffer,
            sizeof(encode_buffer));
        if (res != DW_DLV_OK) {
            _dwarf_p_error(expr->ex_dbg, error, DW_DLE_EXPR_LENGTH_BAD);
            return (DW_DLV_NOCOUNT);
        }
        operand = (Dwarf_Small *) encode_buffer;
        /* put this one directly into 'operand' at tail of prev value */
        res = _dwarf_pro_encode_leb128_nm(val2, &operand2_size,
            ((char *) operand) +
            operand_size,
            sizeof(encode_buffer2));
        if (res != DW_DLV_OK) {
            _dwarf_p_error(expr->ex_dbg, error, DW_DLE_EXPR_LENGTH_BAD);
            return (DW_DLV_NOCOUNT);
        }
        operand_size += operand2_size;
        break;
    default:
        _dwarf_p_error(expr->ex_dbg, error, DW_DLE_BAD_EXPR_OPCODE);
        return (DW_DLV_NOCOUNT);
    }

    next_byte_offset = expr->ex_next_byte_offset + operand_size + 1;

    if (next_byte_offset > MAXIMUM_LOC_EXPR_LENGTH) {
        _dwarf_p_error(expr->ex_dbg, error, DW_DLE_EXPR_LENGTH_BAD);
        return (DW_DLV_NOCOUNT);
    }

    next_byte_ptr =
        &(expr->ex_byte_stream[0]) + expr->ex_next_byte_offset;

    *next_byte_ptr = opcode;
    next_byte_ptr++;
    memcpy(next_byte_ptr, operand, operand_size);

    expr->ex_next_byte_offset = next_byte_offset;
    return (next_byte_offset);
}

Dwarf_Unsigned
dwarf_add_expr_addr_b(Dwarf_P_Expr expr,
    Dwarf_Unsigned addr,
    Dwarf_Unsigned sym_index, Dwarf_Error * error)
{
    Dwarf_P_Debug dbg;
    Dwarf_Small *next_byte_ptr;
    Dwarf_Unsigned next_byte_offset;
    int upointer_size;

    if (expr == NULL) {
        _dwarf_p_error(NULL, error, DW_DLE_EXPR_NULL);
        return (DW_DLV_NOCOUNT);
    }

    dbg = expr->ex_dbg;
    if (dbg == NULL) {
        _dwarf_p_error(NULL, error, DW_DLE_DBG_NULL);
        return (DW_DLV_NOCOUNT);
    }

    upointer_size = dbg->de_pointer_size;
    next_byte_offset = expr->ex_next_byte_offset + upointer_size + 1;
    if (next_byte_offset > MAXIMUM_LOC_EXPR_LENGTH) {
        _dwarf_p_error(dbg, error, DW_DLE_EXPR_LENGTH_BAD);
        return (DW_DLV_NOCOUNT);
    }

    next_byte_ptr =
        &(expr->ex_byte_stream[0]) + expr->ex_next_byte_offset;

    *next_byte_ptr = DW_OP_addr;
    next_byte_ptr++;
    WRITE_UNALIGNED(dbg, next_byte_ptr, (const void *) &addr,
        sizeof(addr), upointer_size);

    if (expr->ex_reloc_offset != 0) {
        _dwarf_p_error(dbg, error, DW_DLE_MULTIPLE_RELOC_IN_EXPR);
        return (DW_DLV_NOCOUNT);
    }

    expr->ex_reloc_sym_index = sym_index;
    expr->ex_reloc_offset = expr->ex_next_byte_offset + 1;

    expr->ex_next_byte_offset = next_byte_offset;
    return (next_byte_offset);
}

Dwarf_Unsigned
dwarf_add_expr_addr(Dwarf_P_Expr expr,
    Dwarf_Unsigned addr,
    Dwarf_Signed sym_index, Dwarf_Error * error)
{
    return
        dwarf_add_expr_addr_b(expr, addr, (Dwarf_Unsigned) sym_index,
            error);
}


Dwarf_Unsigned
dwarf_expr_current_offset(Dwarf_P_Expr expr, Dwarf_Error * error)
{
    if (expr == NULL) {
        _dwarf_p_error(NULL, error, DW_DLE_EXPR_NULL);
        return (DW_DLV_NOCOUNT);
    }

    if (expr->ex_dbg == NULL) {
        _dwarf_p_error(NULL, error, DW_DLE_DBG_NULL);
        return (DW_DLV_NOCOUNT);
    }

    return (expr->ex_next_byte_offset);
}

void
dwarf_expr_reset(Dwarf_P_Expr expr, Dwarf_Error * error)
{
    if (expr == NULL) {
        _dwarf_p_error(NULL, error, DW_DLE_EXPR_NULL);
        return;
    }
    expr->ex_next_byte_offset=0;
}


Dwarf_Addr
dwarf_expr_into_block(Dwarf_P_Expr expr,
    Dwarf_Unsigned * length, Dwarf_Error * error)
{
    if (expr == NULL) {
        _dwarf_p_error(NULL, error, DW_DLE_EXPR_NULL);
        return (DW_DLV_BADADDR);
    }

    if (expr->ex_dbg == NULL) {
        _dwarf_p_error(NULL, error, DW_DLE_DBG_NULL);
        return (DW_DLV_BADADDR);
    }

    if (length != NULL)
        *length = expr->ex_next_byte_offset;
    /*  The following cast from pointer to integer is ok as long as
        Dwarf_Addr is at least as large as a pointer. Which is a
        requirement of libdwarf so must be satisfied (some compilers
        emit a warning about the following line). */
    return ((Dwarf_Addr) & (expr->ex_byte_stream[0]));
}