Debian Codegraph @

src/lib/libdwarfp/dwarf_pro_expr.c

raw
1 /*
2 Copyright (C) 2000,2004,2006 Silicon Graphics, Inc. All Rights Reserved.
3 Portions Copyright 2007-2010 Sun Microsystems, Inc. All rights reserved.
4 Portions Copyright 2011-2019 David Anderson. All rights reserved.
5
6 This program is free software; you can redistribute it
7 and/or modify it under the terms of version 2.1 of the
8 GNU Lesser General Public License as published by the Free
9 Software Foundation.
10
11 This program is distributed in the hope that it would be
12 useful, but WITHOUT ANY WARRANTY; without even the implied
13 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE.
15
16 Further, this software is distributed without any warranty
17 that it is free of the rightful claim of any third person
18 regarding infringement or the like. Any license provided
19 herein, whether implied or otherwise, applies only to this
20 software file. Patent licenses, if any, provided herein
21 do not apply to combinations of this program with other
22 software, or any other product whatsoever.
23
24 You should have received a copy of the GNU Lesser General
25 Public License along with this program; if not, write the
26 Free Software Foundation, Inc., 51 Franklin Street - Fifth
27 Floor, Boston MA 02110-1301, USA.
28
29 */
30
31 #include <config.h>
32
33 #include <stddef.h> /* NULL */
34 #include <string.h> /* memcpy() */
35
36 #include "dwarf.h"
37 #include "libdwarf.h"
38 #include "dwarf_base_types.h"
39 #include "libdwarfp.h"
40 #include "dwarf_pro_incl.h"
41 #include "dwarf_pro_opaque.h"
42 #include "dwarf_pro_error.h"
43 #include "dwarf_pro_alloc.h"
44 #include "dwarf_pro_expr.h"
45
46 #define SIZEOFT16 2
47 #define SIZEOFT32 4
48 #define SIZEOFT64 8
49
50 /*
51 Creates a new expression
52 struct that can be used to build up a
53 location expression.
54 Neither ever returns DW_DLV_NO_ENTRY
55 */
56 int dwarf_new_expr_a(Dwarf_P_Debug dbg,
57 Dwarf_P_Expr *newexpr,
58 Dwarf_Error *error)
59 {
60 Dwarf_P_Expr ret_expr;
61
62 if (dbg == NULL) {
63 _dwarf_p_error(NULL, error, DW_DLE_DBG_NULL);
64 return DW_DLV_ERROR;
65 }
66 ret_expr = (Dwarf_P_Expr)
67 _dwarf_p_get_alloc(dbg, sizeof(struct Dwarf_P_Expr_s));
68 if (ret_expr == NULL) {
69 _dwarf_p_error(dbg, error, DW_DLE_ALLOC_FAIL);
70 return DW_DLV_ERROR;
71 }
72 ret_expr->ex_dbg = dbg;
73 *newexpr = ret_expr;
74 return DW_DLV_OK;
75 }
76
77 int
78 dwarf_add_expr_gen_a(Dwarf_P_Expr expr,
79 Dwarf_Small opcode,
80 Dwarf_Unsigned val1,
81 Dwarf_Unsigned val2,
82 Dwarf_Unsigned *stream_length_out,
83 Dwarf_Error * error)
84 {
85 /* 2* since used to concatenate 2 leb's below */
86 char encode_buffer[2 * ENCODE_SPACE_NEEDED];
87
88 char encode_buffer2[ENCODE_SPACE_NEEDED];
89 int res = 0;
90 Dwarf_P_Debug dbg = 0;
91
92 /* Give the buffer where the operands are first going to be
93 assembled the largest alignment. */
94 Dwarf_Unsigned operand_buffer[10];
95
96 /* Size of the byte stream buffer that needs to be memcpy-ed. */
97 int operand_size = 0;
98
99 /* Points to the byte stream for the first operand,
100 and finally to
101 the buffer that is memcp-ed into the Dwarf_P_Expr_s struct. */
102 Dwarf_Small *operand = 0;
103
104 /* Size of the byte stream for second operand. */
105 int operand2_size = 0;
106 int address_size = 0;
107 /* int offset_size = 0; */
108
109 /* Points to next byte to be written in Dwarf_P_Expr_s struct. */
110 Dwarf_Small *next_byte_ptr = 0;
111
112 /* Offset past the last byte written into Dwarf_P_Expr_s. */
113 int next_byte_offset = 0;
114
115 /* ***** BEGIN CODE ***** */
116
117 if (expr == NULL) {
118 _dwarf_p_error(NULL, error, DW_DLE_EXPR_NULL);
119 return DW_DLV_ERROR;
120 }
121 dbg = expr->ex_dbg;
122
123 if (!dbg) {
124 _dwarf_p_error(NULL, error, DW_DLE_DBG_NULL);
125 return DW_DLV_ERROR;
126 }
127 address_size =dbg->de_pointer_size;
128 /* offset_size =dbg->de_dwarf_offset_size; */
129
130 operand = NULL;
131 operand_size = 0;
132
133 switch (opcode) {
134 case DW_OP_reg0:
135 case DW_OP_reg1:
136 case DW_OP_reg2:
137 case DW_OP_reg3:
138 case DW_OP_reg4:
139 case DW_OP_reg5:
140 case DW_OP_reg6:
141 case DW_OP_reg7:
142 case DW_OP_reg8:
143 case DW_OP_reg9:
144 case DW_OP_reg10:
145 case DW_OP_reg11:
146 case DW_OP_reg12:
147 case DW_OP_reg13:
148 case DW_OP_reg14:
149 case DW_OP_reg15:
150 case DW_OP_reg16:
151 case DW_OP_reg17:
152 case DW_OP_reg18:
153 case DW_OP_reg19:
154 case DW_OP_reg20:
155 case DW_OP_reg21:
156 case DW_OP_reg22:
157 case DW_OP_reg23:
158 case DW_OP_reg24:
159 case DW_OP_reg25:
160 case DW_OP_reg26:
161 case DW_OP_reg27:
162 case DW_OP_reg28:
163 case DW_OP_reg29:
164 case DW_OP_reg30:
165 case DW_OP_reg31:
166 break;
167
168 case DW_OP_breg0:
169 case DW_OP_breg1:
170 case DW_OP_breg2:
171 case DW_OP_breg3:
172 case DW_OP_breg4:
173 case DW_OP_breg5:
174 case DW_OP_breg6:
175 case DW_OP_breg7:
176 case DW_OP_breg8:
177 case DW_OP_breg9:
178 case DW_OP_breg10:
179 case DW_OP_breg11:
180 case DW_OP_breg12:
181 case DW_OP_breg13:
182 case DW_OP_breg14:
183 case DW_OP_breg15:
184 case DW_OP_breg16:
185 case DW_OP_breg17:
186 case DW_OP_breg18:
187 case DW_OP_breg19:
188 case DW_OP_breg20:
189 case DW_OP_breg21:
190 case DW_OP_breg22:
191 case DW_OP_breg23:
192 case DW_OP_breg24:
193 case DW_OP_breg25:
194 case DW_OP_breg26:
195 case DW_OP_breg27:
196 case DW_OP_breg28:
197 case DW_OP_breg29:
198 case DW_OP_breg30:
199 case DW_OP_breg31:
200 res = dwarf_encode_signed_leb128(val1,
201 &operand_size, encode_buffer, sizeof(encode_buffer));
202 if (res != DW_DLV_OK) {
203 _dwarf_p_error(expr->ex_dbg, error,
204 DW_DLE_EXPR_LENGTH_BAD);
205 return DW_DLV_ERROR;
206 }
207 operand = (Dwarf_Small *) encode_buffer;
208 break;
209
210 case DW_OP_regx:
211 res = dwarf_encode_leb128(val1, &operand_size,
212 encode_buffer, sizeof(encode_buffer));
213 if (res != DW_DLV_OK) {
214 _dwarf_p_error(expr->ex_dbg, error,
215 DW_DLE_EXPR_LENGTH_BAD);
216 return DW_DLV_ERROR;
217 }
218 operand = (Dwarf_Small *) encode_buffer;
219 break;
220
221 case DW_OP_lit0:
222 case DW_OP_lit1:
223 case DW_OP_lit2:
224 case DW_OP_lit3:
225 case DW_OP_lit4:
226 case DW_OP_lit5:
227 case DW_OP_lit6:
228 case DW_OP_lit7:
229 case DW_OP_lit8:
230 case DW_OP_lit9:
231 case DW_OP_lit10:
232 case DW_OP_lit11:
233 case DW_OP_lit12:
234 case DW_OP_lit13:
235 case DW_OP_lit14:
236 case DW_OP_lit15:
237 case DW_OP_lit16:
238 case DW_OP_lit17:
239 case DW_OP_lit18:
240 case DW_OP_lit19:
241 case DW_OP_lit20:
242 case DW_OP_lit21:
243 case DW_OP_lit22:
244 case DW_OP_lit23:
245 case DW_OP_lit24:
246 case DW_OP_lit25:
247 case DW_OP_lit26:
248 case DW_OP_lit27:
249 case DW_OP_lit28:
250 case DW_OP_lit29:
251 case DW_OP_lit30:
252 case DW_OP_lit31:
253 break;
254
255 case DW_OP_addr:
256 operand = (Dwarf_Small *) & operand_buffer[0];
257 WRITE_UNALIGNED(dbg, operand, &val1,sizeof(val1),
258 address_size);
259 operand_size = address_size;
260 break;
261
262 case DW_OP_const1u:
263 case DW_OP_const1s:
264 operand = (Dwarf_Small *) & operand_buffer[0];
265 WRITE_UNALIGNED(dbg, operand, &val1, sizeof(val1), 1);
266 operand_size = 1;
267 break;
268
269 case DW_OP_const2u:
270 case DW_OP_const2s:
271 operand = (Dwarf_Small *) & operand_buffer[0];
272 WRITE_UNALIGNED(dbg, operand, &val1, sizeof(val1), 2);
273 operand_size = 2;
274 break;
275
276 case DW_OP_const4u:
277 case DW_OP_const4s:
278 operand = (Dwarf_Small *) & operand_buffer[0];
279 WRITE_UNALIGNED(dbg, operand, &val1, sizeof(val1),
280 SIZEOFT32);
281 operand_size = SIZEOFT32;
282 break;
283
284 case DW_OP_const8u:
285 case DW_OP_const8s:
286 operand = (Dwarf_Small *) & operand_buffer[0];
287 WRITE_UNALIGNED(dbg, operand, &val1, sizeof(val1), 8);
288 operand_size = 8;
289 break;
290
291 case DW_OP_constu:
292 res = dwarf_encode_leb128(val1,
293 &operand_size, encode_buffer, sizeof(encode_buffer));
294 if (res != DW_DLV_OK) {
295 _dwarf_p_error(expr->ex_dbg, error,
296 DW_DLE_EXPR_LENGTH_BAD);
297 return DW_DLV_ERROR;
298 }
299 operand = (Dwarf_Small *) encode_buffer;
300 break;
301
302 case DW_OP_consts:
303 res = dwarf_encode_signed_leb128(val1,
304 &operand_size,
305 encode_buffer,
306 sizeof(encode_buffer));
307 if (res != DW_DLV_OK) {
308 _dwarf_p_error(expr->ex_dbg, error,
309 DW_DLE_EXPR_LENGTH_BAD);
310 return DW_DLV_ERROR;
311 }
312 operand = (Dwarf_Small *) encode_buffer;
313 break;
314
315 case DW_OP_fbreg:
316 res = dwarf_encode_signed_leb128(val1,
317 &operand_size,
318 encode_buffer,
319 sizeof(encode_buffer));
320 if (res != DW_DLV_OK) {
321 _dwarf_p_error(expr->ex_dbg, error,
322 DW_DLE_EXPR_LENGTH_BAD);
323 return DW_DLV_ERROR;
324 }
325 operand = (Dwarf_Small *) encode_buffer;
326 break;
327
328 case DW_OP_bregx:
329 res = dwarf_encode_leb128(val1, &operand_size,
330 encode_buffer,
331 sizeof(encode_buffer));
332 if (res != DW_DLV_OK) {
333 _dwarf_p_error(expr->ex_dbg, error,
334 DW_DLE_EXPR_LENGTH_BAD);
335 return DW_DLV_ERROR;
336 }
337 operand = (Dwarf_Small *) encode_buffer;
338 /* Put this one directly into 'operand' at
339 tail of prev value */
340 res = dwarf_encode_signed_leb128(val2, &operand2_size,
341 ((char *) operand) +
342 operand_size,
343 sizeof(encode_buffer2));
344 if (res != DW_DLV_OK) {
345 _dwarf_p_error(expr->ex_dbg, error,
346 DW_DLE_EXPR_LENGTH_BAD);
347 return DW_DLV_ERROR;
348 }
349 operand_size += operand2_size;
350
351 case DW_OP_dup:
352 case DW_OP_drop:
353 break;
354
355 case DW_OP_pick:
356 operand = (Dwarf_Small *) & operand_buffer[0];
357 WRITE_UNALIGNED(dbg, operand, (const void *) &val1,
358 sizeof(val1), 1);
359 operand_size = 1;
360 break;
361
362 case DW_OP_over:
363 case DW_OP_swap:
364 case DW_OP_rot:
365 case DW_OP_deref:
366 case DW_OP_xderef:
367 break;
368
369 case DW_OP_deref_size:
370 case DW_OP_xderef_size:
371 operand = (Dwarf_Small *) & operand_buffer[0];
372 WRITE_UNALIGNED(dbg, operand, (const void *) &val1,
373 sizeof(val1), 1);
374 operand_size = 1;
375 break;
376
377 case DW_OP_abs:
378 case DW_OP_and:
379 case DW_OP_div:
380 case DW_OP_minus:
381 case DW_OP_mod:
382 case DW_OP_mul:
383 case DW_OP_neg:
384 case DW_OP_not:
385 case DW_OP_or:
386 case DW_OP_plus:
387 break;
388
389 case DW_OP_plus_uconst:
390 res = dwarf_encode_leb128(val1, &operand_size,
391 encode_buffer,
392 sizeof(encode_buffer));
393 if (res != DW_DLV_OK) {
394 _dwarf_p_error(expr->ex_dbg, error,
395 DW_DLE_EXPR_LENGTH_BAD);
396 return DW_DLV_ERROR;
397 }
398 operand = (Dwarf_Small *) encode_buffer;
399 break;
400
401 case DW_OP_shl:
402 case DW_OP_shr:
403 case DW_OP_shra:
404 case DW_OP_xor:
405 break;
406
407 case DW_OP_le:
408 case DW_OP_ge:
409 case DW_OP_eq:
410 case DW_OP_lt:
411 case DW_OP_gt:
412 case DW_OP_ne:
413 break;
414
415 case DW_OP_skip:
416 case DW_OP_bra:
417 /* The operand is 16bit, signed. */
418 WRITE_UNALIGNED(dbg, encode_buffer, &val1,\
419 sizeof(val1), SIZEOFT16);
420 operand_size = SIZEOFT16;
421 operand = (Dwarf_Small *) encode_buffer;
422 break;
423
424 case DW_OP_piece:
425 res = dwarf_encode_leb128(val1, &operand_size,
426 encode_buffer,
427 sizeof(encode_buffer));
428 if (res != DW_DLV_OK) {
429 _dwarf_p_error(expr->ex_dbg, error,
430 DW_DLE_EXPR_LENGTH_BAD);
431 return DW_DLV_ERROR;
432 }
433 operand = (Dwarf_Small *) encode_buffer;
434 break;
435
436 case DW_OP_nop:
437 break;
438 case DW_OP_push_object_address: /* DWARF3 */
439 break;
440 case DW_OP_call2: /* DWARF3 */
441 operand = (Dwarf_Small *) & operand_buffer[0];
442 WRITE_UNALIGNED(dbg, operand, &val1, sizeof(val1), SIZEOFT16);
443 operand_size = SIZEOFT16;
444 break;
445
446 case DW_OP_call4: /* DWARF3 */
447 operand = (Dwarf_Small *) & operand_buffer[0];
448 WRITE_UNALIGNED(dbg, operand, &val1, sizeof(val1), SIZEOFT32);
449 operand_size = SIZEOFT32;
450 break;
451
452 case DW_OP_call_ref: /* DWARF3 */
453 operand = (Dwarf_Small *) & operand_buffer[0];
454 WRITE_UNALIGNED(dbg, operand, &val1, sizeof(val1),
455 dbg->de_dwarf_offset_size);
456 operand_size = dbg->de_dwarf_offset_size;
457 break;
458 case DW_OP_form_tls_address: /* DWARF3f */
459 break;
460 case DW_OP_call_frame_cfa: /* DWARF3f */
461 break;
462 case DW_OP_bit_piece: /* DWARF3f */
463 res = dwarf_encode_leb128(val1, &operand_size,
464 encode_buffer,
465 sizeof(encode_buffer));
466 if (res != DW_DLV_OK) {
467 _dwarf_p_error(expr->ex_dbg, error,
468 DW_DLE_EXPR_LENGTH_BAD);
469 return DW_DLV_ERROR;
470 }
471 operand = (Dwarf_Small *) encode_buffer;
472 /* Put this one directly into 'operand'
473 at tail of prev value */
474 res = dwarf_encode_leb128(val2, &operand2_size,
475 ((char *) operand) +
476 operand_size,
477 sizeof(encode_buffer2));
478 if (res != DW_DLV_OK) {
479 _dwarf_p_error(expr->ex_dbg, error,
480 DW_DLE_EXPR_LENGTH_BAD);
481 return DW_DLV_ERROR;
482 }
483 operand_size += operand2_size;
484 break;
485 default:
486 _dwarf_p_error(expr->ex_dbg, error, DW_DLE_BAD_EXPR_OPCODE);
487 return DW_DLV_ERROR;
488 }
489
490 next_byte_offset = expr->ex_next_byte_offset + operand_size + 1;
491
492 if (next_byte_offset > MAXIMUM_LOC_EXPR_LENGTH) {
493 _dwarf_p_error(expr->ex_dbg, error, DW_DLE_EXPR_LENGTH_BAD);
494 return DW_DLV_ERROR;
495 }
496
497 next_byte_ptr =
498 &(expr->ex_byte_stream[0]) + expr->ex_next_byte_offset;
499
500 *next_byte_ptr = opcode;
501 next_byte_ptr++;
502 if (operand) {
503 memcpy(next_byte_ptr, operand, operand_size);
504 }
505
506 expr->ex_next_byte_offset = next_byte_offset;
507 *stream_length_out = next_byte_offset;
508 return DW_DLV_OK;
509 }
510
511 int
512 dwarf_add_expr_addr_c(Dwarf_P_Expr expr,
513 Dwarf_Unsigned addr,
514 Dwarf_Unsigned sym_index,
515 Dwarf_Unsigned *stream_length_out,
516 Dwarf_Error * error)
517 {
518 Dwarf_P_Debug dbg;
519 Dwarf_Small *next_byte_ptr;
520 Dwarf_Unsigned next_byte_offset;
521 int upointer_size;
522
523 if (expr == NULL) {
524 _dwarf_p_error(NULL, error, DW_DLE_EXPR_NULL);
525 return DW_DLV_ERROR;
526 }
527
528 dbg = expr->ex_dbg;
529 if (dbg == NULL) {
530 _dwarf_p_error(NULL, error, DW_DLE_DBG_NULL);
531 return DW_DLV_ERROR;
532 }
533
534 upointer_size = dbg->de_pointer_size;
535 next_byte_offset = expr->ex_next_byte_offset + upointer_size + 1;
536 if (next_byte_offset > MAXIMUM_LOC_EXPR_LENGTH) {
537 _dwarf_p_error(dbg, error, DW_DLE_EXPR_LENGTH_BAD);
538 return DW_DLV_ERROR;
539 }
540
541 next_byte_ptr =
542 &(expr->ex_byte_stream[0]) + expr->ex_next_byte_offset;
543
544 *next_byte_ptr = DW_OP_addr;
545 next_byte_ptr++;
546 WRITE_UNALIGNED(dbg, next_byte_ptr, (const void *) &addr,
547 sizeof(addr), upointer_size);
548
549 if (expr->ex_reloc_offset != 0) {
550 _dwarf_p_error(dbg, error, DW_DLE_MULTIPLE_RELOC_IN_EXPR);
551 return DW_DLV_ERROR;
552 }
553
554 expr->ex_reloc_sym_index = sym_index;
555 expr->ex_reloc_offset = expr->ex_next_byte_offset + 1;
556
557 expr->ex_next_byte_offset = next_byte_offset;
558 *stream_length_out = next_byte_offset;
559 return DW_DLV_OK;
560 }
561
562 int
563 dwarf_expr_current_offset_a(Dwarf_P_Expr expr,
564 Dwarf_Unsigned * stream_length_out,
565 Dwarf_Error * error)
566 {
567 if (expr == NULL) {
568 _dwarf_p_error(NULL, error, DW_DLE_EXPR_NULL);
569 return DW_DLV_ERROR;
570 }
571
572 if (expr->ex_dbg == NULL) {
573 _dwarf_p_error(NULL, error, DW_DLE_DBG_NULL);
574 return DW_DLV_ERROR;
575 }
576 *stream_length_out = expr->ex_next_byte_offset;
577 return DW_DLV_OK;
578 }
579
580 void
581 dwarf_expr_reset(Dwarf_P_Expr expr, Dwarf_Error * error)
582 {
583 if (expr == NULL) {
584 _dwarf_p_error(NULL, error, DW_DLE_EXPR_NULL);
585 return;
586 }
587 expr->ex_next_byte_offset=0;
588 }
589
590 int
591 dwarf_expr_into_block_a(Dwarf_P_Expr expr,
592 Dwarf_Unsigned * length,
593 Dwarf_Small ** address,
594 Dwarf_Error * error)
595 {
596 if (expr == NULL) {
597 _dwarf_p_error(NULL, error, DW_DLE_EXPR_NULL);
598 return DW_DLV_ERROR;
599 }
600
601 if (expr->ex_dbg == NULL) {
602 _dwarf_p_error(NULL, error, DW_DLE_DBG_NULL);
603 return DW_DLV_ERROR;
604 }
605
606 if (length != NULL)
607 *length = expr->ex_next_byte_offset;
608 *address = &(expr->ex_byte_stream[0]);
609 return DW_DLV_OK;
610 }