Debian Codegraph @

src/lib/libdwarfp/dwarf_pro_frame.c

raw
1 /*
2 Copyright (C) 2000,2004 Silicon Graphics, Inc. All Rights Reserved.
3 Portions Copyright 2011-2017 David Anderson. All Rights Reserved.
4 Portions Copyright 2012 SN Systems Ltd. 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() strcpy() strlen() */
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_frame.h"
45
46 static void _dwarf_pro_add_to_fde(Dwarf_P_Fde fde,
47 Dwarf_P_Frame_Pgm inst);
48
49 /* This function adds a cie struct to the debug pointer. Its in the
50 form of a linked list.
51 augmenter: string reps augmentation (implementation defined)
52 code_align: alignment of code
53 data_align: alignment of data
54 init_bytes: byts having initial instructions
55 init_n_bytes: number of bytes of initial instructions */
56
57 int
58 dwarf_add_frame_cie_a(Dwarf_P_Debug dbg,
59 char *augmenter,
60 Dwarf_Small code_align,
61 Dwarf_Small data_align,
62 Dwarf_Small return_reg,
63 Dwarf_Ptr init_bytes,
64 Dwarf_Unsigned init_n_bytes,
65 Dwarf_Unsigned * cie_index_out,
66 Dwarf_Error * error)
67 {
68 Dwarf_P_Cie curcie;
69 char *tmpaug = 0;
70
71 if (dbg->de_frame_cies == NULL) {
72 dbg->de_frame_cies = (Dwarf_P_Cie)
73 _dwarf_p_get_alloc(dbg, sizeof(struct Dwarf_P_Cie_s));
74 if (dbg->de_frame_cies == NULL) {
75 DWARF_P_DBG_ERROR(dbg, DW_DLE_CIE_ALLOC, DW_DLV_ERROR);
76 }
77 curcie = dbg->de_frame_cies;
78 dbg->de_n_cie = 1;
79 dbg->de_last_cie = curcie;
80 } else {
81 curcie = dbg->de_last_cie;
82 curcie->cie_next = (Dwarf_P_Cie)
83 _dwarf_p_get_alloc(dbg, sizeof(struct Dwarf_P_Cie_s));
84 if (curcie->cie_next == NULL) {
85 DWARF_P_DBG_ERROR(dbg, DW_DLE_CIE_ALLOC, DW_DLV_ERROR);
86 }
87 curcie = curcie->cie_next;
88 dbg->de_n_cie++;
89 dbg->de_last_cie = curcie;
90 }
91 curcie->cie_version = 1;
92 if (dbg->de_output_version > 2) {
93 curcie->cie_version = dbg->de_output_version;
94 } else {
95 /* V2 dwarf has debug_frame as version 1, there
96 is no 2 used in this section. */
97 curcie->cie_version = 1;
98 }
99 tmpaug = (char *)_dwarf_p_get_alloc(dbg,strlen(augmenter)+1);
100 if (!tmpaug) {
101 DWARF_P_DBG_ERROR(dbg, DW_DLE_CIE_ALLOC, DW_DLV_ERROR);
102 }
103 strcpy(tmpaug,augmenter);
104 curcie->cie_aug = tmpaug;
105 curcie->cie_code_align = code_align;
106 curcie->cie_data_align = data_align;
107 curcie->cie_ret_reg = return_reg;
108 curcie->cie_inst = (char *) init_bytes;
109 curcie->cie_inst_bytes = (long) init_n_bytes;
110 curcie->cie_next = NULL;
111 *cie_index_out = dbg->de_n_cie;
112 return DW_DLV_OK;
113 }
114
115 /* This functions adds a fde struct to the debug pointer. Its in the
116 form of a linked list.
117 die: subprogram/function die corresponding to this fde
118 cie: cie referred to by this fde, obtained from call to
119 add_frame_cie() routine.
120 virt_addr: beginning address
121 code_len: length of code reps by the fde */
122
123 /* New December 2018 */
124 int
125 dwarf_add_frame_fde_c(Dwarf_P_Debug dbg,
126 Dwarf_P_Fde fde,
127 Dwarf_P_Die die,
128 Dwarf_Unsigned cie,
129 Dwarf_Unsigned virt_addr,
130 Dwarf_Unsigned code_len,
131 Dwarf_Unsigned symidx,
132 Dwarf_Unsigned symidx_of_end,
133 Dwarf_Addr offset_from_end_sym,
134 Dwarf_Unsigned *index_to_fde,
135 Dwarf_Error * error)
136 {
137 Dwarf_P_Fde curfde;
138
139 (void)error;
140 fde->fde_die = die;
141 fde->fde_cie = (long) cie;
142 fde->fde_initloc = virt_addr;
143 fde->fde_r_symidx = symidx;
144 fde->fde_addr_range = code_len;
145 fde->fde_offset_into_exception_tables = DW_DLX_NO_EH_OFFSET;
146 fde->fde_exception_table_symbol = 0;
147 fde->fde_end_symbol_offset = offset_from_end_sym;
148 fde->fde_end_symbol = symidx_of_end;
149 fde->fde_dbg = dbg;
150
151 curfde = dbg->de_last_fde;
152 if (curfde == NULL) {
153 dbg->de_frame_fdes = fde;
154 dbg->de_last_fde = fde;
155 dbg->de_n_fde = 1;
156 } else {
157 curfde->fde_next = fde;
158 dbg->de_last_fde = fde;
159 dbg->de_n_fde++;
160 }
161 *index_to_fde = dbg->de_n_fde;
162 return DW_DLV_OK;
163 }
164
165 /* This function adds information to an fde. The fde is
166 linked into the linked list of fde's
167 maintained in the Dwarf_P_Debug
168 structure.
169 dbg: The debug descriptor.
170 fde: The fde to be added.
171 die: subprogram/function die corresponding to this fde
172 cie: cie referred to by this fde, obtained from call to
173 add_frame_cie() routine.
174 virt_addr: beginning address
175 code_len: length of code reps by the fde
176 symidx: The symbol id of the symbol wrt to which relocation needs
177 to be performed for 'virt_addr'.
178 offset_into_exception_tables: The start of exception tables for
179 this function (indicated as an offset into the exception
180 tables). A value of -1 indicates that there is no exception
181 table entries associated with this function.
182 exception_table_symbol: The symbol id of the section for exception
183 tables wrt to which the offset_into_exception_tables will
184 be relocated. */
185 int
186 dwarf_add_frame_info_c(Dwarf_P_Debug dbg,
187 Dwarf_P_Fde fde,
188 Dwarf_P_Die die,
189 Dwarf_Unsigned cie,
190 Dwarf_Unsigned virt_addr,
191 Dwarf_Unsigned code_len,
192 Dwarf_Unsigned symidx,
193 Dwarf_Unsigned end_symidx,
194 Dwarf_Unsigned offset_from_end_symbol,
195 Dwarf_Signed offset_into_exception_tables,
196 Dwarf_Unsigned exception_table_symbol,
197 Dwarf_Unsigned *fde_index_out,
198 Dwarf_Error * error)
199 {
200 Dwarf_P_Fde curfde = 0;
201
202 (void)error;
203 fde->fde_die = die;
204 fde->fde_cie = (long) cie;
205 fde->fde_initloc = virt_addr;
206 fde->fde_r_symidx = symidx;
207 fde->fde_addr_range = code_len;
208 fde->fde_offset_into_exception_tables =
209 offset_into_exception_tables;
210 fde->fde_exception_table_symbol = exception_table_symbol;
211 fde->fde_end_symbol_offset = offset_from_end_symbol;
212 fde->fde_end_symbol = end_symidx;
213 fde->fde_dbg = dbg;
214
215 curfde = dbg->de_last_fde;
216 if (curfde == NULL) {
217 dbg->de_frame_fdes = fde;
218 dbg->de_last_fde = fde;
219 dbg->de_n_fde = 1;
220 } else {
221 curfde->fde_next = fde;
222 dbg->de_last_fde = fde;
223 dbg->de_n_fde++;
224 }
225 *fde_index_out = dbg->de_n_fde;
226 return DW_DLV_OK;
227 }
228
229 /* This is an alternate to inserting frame instructions
230 one instruction at a time. But use either this
231 or instruction level, not both in one fde. */
232 int
233 dwarf_insert_fde_inst_bytes(Dwarf_P_Debug dbg,
234 Dwarf_P_Fde fde,Dwarf_Unsigned len, Dwarf_Ptr ibytes,
235 Dwarf_Error *error)
236 {
237 if (len == 0) {
238 return DW_DLV_OK;
239 }
240 if (fde->fde_block || fde->fde_inst) {
241 DWARF_P_DBG_ERROR(dbg, DW_DLE_DUPLICATE_INST_BLOCK,
242 DW_DLV_ERROR);
243 }
244 fde->fde_block = (Dwarf_Ptr)_dwarf_p_get_alloc(dbg, len);
245 memcpy(fde->fde_block,ibytes,len);
246 fde->fde_inst_block_size = len;
247 fde->fde_n_bytes += len;
248 return DW_DLV_OK;
249 }
250
251 /* Create a new fde. */
252 int
253 dwarf_new_fde_a(Dwarf_P_Debug dbg,
254 Dwarf_P_Fde *fde_out,
255 Dwarf_Error * error)
256 {
257 Dwarf_P_Fde fde;
258
259 fde = (Dwarf_P_Fde)
260 _dwarf_p_get_alloc(dbg, sizeof(struct Dwarf_P_Fde_s));
261 if (fde == NULL) {
262 DWARF_P_DBG_ERROR(dbg, DW_DLE_FDE_ALLOC, DW_DLV_ERROR);
263 }
264 fde->fde_dbg = dbg;
265 fde->fde_uwordb_size = dbg->de_dwarf_offset_size;
266 *fde_out = fde;
267 return DW_DLV_OK;
268 }
269
270 /* Add a cfe_offset instruction to the fde passed in. */
271
272 int
273 dwarf_fde_cfa_offset_a(Dwarf_P_Fde fde,
274 Dwarf_Unsigned reg,
275 Dwarf_Signed offset,
276 Dwarf_Error * error)
277 {
278 Dwarf_Ubyte opc, regno;
279 char *ptr = 0;
280 Dwarf_P_Frame_Pgm curinst;
281 int nbytes = 0;
282 int res = 0;
283 char buff1[ENCODE_SPACE_NEEDED];
284 Dwarf_P_Debug dbg = fde->fde_dbg;
285
286 curinst = (Dwarf_P_Frame_Pgm)
287 _dwarf_p_get_alloc(dbg, sizeof(struct Dwarf_P_Frame_Pgm_s));
288 if (curinst == NULL) {
289 DWARF_P_DBG_ERROR(dbg, DW_DLE_FPGM_ALLOC, DW_DLV_ERROR);
290 }
291 opc = DW_CFA_offset;
292 regno = reg;
293 if (regno & 0xc0) {
294 DWARF_P_DBG_ERROR(dbg, DW_DLE_REGNO_OVFL,DW_DLV_ERROR);
295 }
296 opc = opc | regno; /* lower 6 bits are register number */
297 curinst->dfp_opcode = opc;
298 res = dwarf_encode_leb128(offset, &nbytes,
299 buff1, sizeof(buff1));
300 if (res != DW_DLV_OK) {
301 _dwarf_p_error(dbg, error, DW_DLE_STRING_ALLOC);
302 return DW_DLV_ERROR;
303 }
304 ptr = (char *) _dwarf_p_get_alloc(dbg, nbytes);
305 if (ptr == NULL) {
306 _dwarf_p_error(dbg, error, DW_DLE_STRING_ALLOC);
307 return DW_DLV_ERROR;
308 }
309 memcpy(ptr, buff1, nbytes);
310
311 curinst->dfp_args = ptr;
312 curinst->dfp_nbytes = nbytes;
313 curinst->dfp_next = NULL;
314
315 _dwarf_pro_add_to_fde(fde, curinst);
316 return DW_DLV_OK;
317 }
318
319 /* Generic routine to add opcode to fde instructions. val1 and
320 val2 are parameters whose interpretation depends on the 'op'.
321
322 This does not work properly for DW_DLC_SYMBOLIC_RELOCATIONS
323 for DW_CFA_set_loc or DW_DVA_advance_loc* 'op', as
324 these ops normally are addresses or (DW_CFA_set_loc)
325 or code lengths (DW_DVA_advance_loc*) and such must be
326 represented with relocations and symbol indices for
327 DW_DLC_SYMBOLIC_RELOCATIONS.
328
329 This does not treat all DW_CFA instructions yet.
330
331 For certain operations a val? value must be
332 signed (though passed in as unsigned here).
333
334 Does not check that the frame
335 version is 3(for dwarf3) or 4 (for dwarf4) or 5
336 when applying operations that are only valid for
337 particular versions. */
338
339 int
340 dwarf_add_fde_inst_a(Dwarf_P_Fde fde,
341 Dwarf_Small op,
342 Dwarf_Unsigned val1,
343 Dwarf_Unsigned val2,
344 Dwarf_Error * error)
345 {
346 Dwarf_P_Frame_Pgm curinst;
347 int nbytes = 0;
348 int nbytes1 = 0;
349 int nbytes2 = 0;
350 Dwarf_Ubyte db = 0;
351 Dwarf_Half dh = 0;
352 Dwarf_Unsigned du = 0;
353 char *ptr = 0;
354 int res = 0;
355 char buff1[ENCODE_SPACE_NEEDED];
356 char buff2[ENCODE_SPACE_NEEDED];
357 Dwarf_P_Debug dbg = fde->fde_dbg;
358 /* This is a hack telling the code when to transform
359 a value to a signed leb number. */
360 int signed_second = 0;
361 int signed_first = 0;
362
363 buff1[0] = 0;
364 buff2[0] = 0;
365 curinst = (Dwarf_P_Frame_Pgm)
366 _dwarf_p_get_alloc(dbg, sizeof(struct Dwarf_P_Frame_Pgm_s));
367 if (curinst == NULL) {
368 _dwarf_p_error(dbg, error, DW_DLE_FPGM_ALLOC);
369 return DW_DLV_ERROR;
370 }
371 switch (op) {
372
373 case DW_CFA_advance_loc: {
374 if (val1 <= 0x3f) {
375 db = val1;
376 op |= db;
377 } else if (!(val1& ~0xff)) {
378 op = DW_CFA_advance_loc1;
379 db = val1;
380 ptr = (char *) _dwarf_p_get_alloc(dbg, 1);
381 if (ptr == NULL) {
382 _dwarf_p_error(dbg, error, DW_DLE_STRING_ALLOC);
383 return DW_DLV_ERROR;
384 }
385 memcpy((void *) ptr, (const void *) &db, 1);
386 nbytes = 1;
387 } else if (!(val1& (~(Dwarf_Unsigned)0xffff))) {
388 if (sizeof(dh) < SIZEOFT16) {
389 _dwarf_p_error(dbg, error,
390 DW_DLE_DEBUG_FRAME_LENGTH_BAD);
391 return DW_DLV_ERROR;
392 }
393 op = DW_CFA_advance_loc2;
394 dh = val1;
395 ptr = (char *) _dwarf_p_get_alloc(dbg, SIZEOFT16);
396 if (ptr == NULL) {
397 _dwarf_p_error(dbg, error, DW_DLE_STRING_ALLOC);
398 return DW_DLV_ERROR;
399 }
400 /* No byte swapping, assuming running at
401 target endianness. */
402 ASNOUT((void *) ptr, dh, SIZEOFT16);
403 nbytes = SIZEOFT16;
404 } else if (!(val1& ~(Dwarf_Unsigned)0xffffffff)) {
405 if (sizeof(du) < SIZEOFT32) {
406 _dwarf_p_error(dbg, error,
407 DW_DLE_DEBUG_FRAME_LENGTH_BAD);
408 return DW_DLV_ERROR;
409 }
410 op = DW_CFA_advance_loc4;
411 du = val1;
412 ptr = (char *) _dwarf_p_get_alloc(dbg, SIZEOFT32);
413 if (ptr == NULL) {
414 _dwarf_p_error(dbg, error, DW_DLE_STRING_ALLOC);
415 return DW_DLV_ERROR;
416 }
417 ASNOUT((void *) ptr, du, SIZEOFT32);
418 nbytes = SIZEOFT32;
419 } else {
420 if (sizeof(du) < SIZEOFT64) {
421 _dwarf_p_error(dbg, error,
422 DW_DLE_DEBUG_FRAME_LENGTH_BAD);
423 return DW_DLV_ERROR;
424 }
425 op = DW_CFA_MIPS_advance_loc8;
426 du = val1;
427 ptr = (char *) _dwarf_p_get_alloc(dbg,
428 SIZEOFT64);
429 if (ptr == NULL) {
430 _dwarf_p_error(dbg, error, DW_DLE_STRING_ALLOC);
431 return DW_DLV_ERROR;
432 }
433 /* No byte swapping, assuming running at
434 target endianness. */
435 ASNOUT((void *) ptr, du, SIZEOFT64);
436 nbytes = SIZEOFT64;
437 }
438 break;
439 }
440 case DW_CFA_offset:
441 if (val1 <= MAX_6_BIT_VALUE) {
442 db = val1;
443 op |= db;
444 res = dwarf_encode_leb128(val2, &nbytes,
445 buff1, sizeof(buff1));
446 if (res != DW_DLV_OK) {
447 _dwarf_p_error(dbg, error, DW_DLE_STRING_ALLOC);
448 return DW_DLV_ERROR;
449 }
450 ptr = (char *) _dwarf_p_get_alloc(dbg, nbytes);
451 if (ptr == NULL) {
452 _dwarf_p_error(dbg, error, DW_DLE_STRING_ALLOC);
453 return DW_DLV_ERROR;
454 }
455 memcpy(ptr, buff1, nbytes);
456
457 } else {
458 op = DW_CFA_offset_extended;
459 goto two_leb;
460 }
461 break;
462 case DW_CFA_offset_extended_sf: /* DWARF3 */
463 signed_second = 1;
464 goto two_leb;
465 case DW_CFA_offset_extended:
466 goto two_leb;
467
468 case DW_CFA_undefined:
469 case DW_CFA_same_value:
470 goto one_leb;
471
472 case DW_CFA_val_offset:
473 goto two_leb;
474 case DW_CFA_val_offset_sf:
475 signed_second = 1;
476 goto two_leb;
477 case DW_CFA_def_cfa_sf:
478 signed_second = 1;
479 goto two_leb;
480 case DW_CFA_register:
481 case DW_CFA_def_cfa:
482 two_leb:
483 res = dwarf_encode_leb128(val1, &nbytes1,
484 buff1, sizeof(buff1));
485 if (res != DW_DLV_OK) {
486 _dwarf_p_error(dbg, error, DW_DLE_STRING_ALLOC);
487 return DW_DLV_ERROR;
488 }
489 if (!signed_second) {
490 res = dwarf_encode_leb128(val2, &nbytes2,
491 buff2, sizeof(buff2));
492 } else {
493 Dwarf_Signed val2s = val2;
494 res = dwarf_encode_signed_leb128(val2s, &nbytes2,
495 buff2, sizeof(buff2));
496 }
497 if (res != DW_DLV_OK) {
498 _dwarf_p_error(dbg, error, DW_DLE_STRING_ALLOC);
499 return DW_DLV_ERROR;
500 }
501
502 res = dwarf_encode_leb128(val2, &nbytes2,
503 buff2, sizeof(buff2));
504 if (res != DW_DLV_OK) {
505 _dwarf_p_error(dbg, error, DW_DLE_STRING_ALLOC);
506 return DW_DLV_ERROR;
507 }
508
509 ptr = (char *) _dwarf_p_get_alloc(dbg, nbytes1 + nbytes2);
510 if (ptr == NULL) {
511 _dwarf_p_error(dbg, error, DW_DLE_STRING_ALLOC);
512 return DW_DLV_ERROR;
513 }
514 memcpy(ptr, buff1, nbytes1);
515 memcpy(ptr + nbytes1, buff2, nbytes2);
516 nbytes = nbytes1 + nbytes2;
517 break;
518
519 case DW_CFA_def_cfa_offset_sf: /* DWARF3 */
520 signed_first = 1;
521 goto one_leb;
522 case DW_CFA_def_cfa_register:
523 case DW_CFA_def_cfa_offset:
524 one_leb:
525 if (!signed_first) {
526 res = dwarf_encode_leb128(val1, &nbytes,
527 buff1, sizeof(buff1));
528 } else {
529 Dwarf_Signed val1s = val1;
530 res = dwarf_encode_signed_leb128(val1s, &nbytes,
531 buff1, sizeof(buff1));
532 }
533 if (res != DW_DLV_OK) {
534 _dwarf_p_error(dbg, error, DW_DLE_STRING_ALLOC);
535 return DW_DLV_ERROR;
536 }
537 ptr = (char *) _dwarf_p_get_alloc(dbg, nbytes);
538 if (ptr == NULL) {
539 _dwarf_p_error(dbg, error, DW_DLE_STRING_ALLOC);
540 return DW_DLV_ERROR;
541 }
542 memcpy(ptr, buff1, nbytes);
543 break;
544 case DW_CFA_def_cfa_expression: /* DWARF3 */
545 /* FIXME: argument is dwarf expr, not handled yet. */
546 case DW_CFA_expression: /* DWARF3 */
547 /* First arg: ULEB reg num. 2nd arg dwarf expr in form block.
548 FIXME: not handled yet. */
549 case DW_CFA_val_expression: /* DWARF3f */
550 /* First arg: ULEB reg num. 2nd arg dwarf expr in form block.
551 FIXME: not handled yet. */
552 default:
553 _dwarf_p_error(dbg, error, DW_DLE_DEBUGFRAME_ERROR);
554 return DW_DLV_ERROR;
555 }
556
557 curinst->dfp_opcode = op;
558 curinst->dfp_args = ptr;
559 curinst->dfp_nbytes = nbytes;
560 curinst->dfp_next = NULL;
561 _dwarf_pro_add_to_fde(fde, curinst);
562 return DW_DLV_OK;
563 }
564
565 /* Instructions are added to an fde in the form of a linked
566 list. This function manages the linked list. */
567 void
568 _dwarf_pro_add_to_fde(Dwarf_P_Fde fde, Dwarf_P_Frame_Pgm curinst)
569 {
570 if (fde->fde_last_inst) {
571 fde->fde_last_inst->dfp_next = curinst;
572 fde->fde_last_inst = curinst;
573 fde->fde_n_inst++;
574 fde->fde_n_bytes +=
575 (long) (curinst->dfp_nbytes + sizeof(Dwarf_Ubyte));
576 } else {
577 fde->fde_last_inst = curinst;
578 fde->fde_inst = curinst;
579 fde->fde_n_inst = 1;
580 fde->fde_n_bytes =
581 (long) (curinst->dfp_nbytes + sizeof(Dwarf_Ubyte));
582 }
583 }