Debian Codegraph @

src/lib/libdwarf/dwarf_frame.c

raw
1 /*
2 Copyright (C) 2000-2006 Silicon Graphics, Inc. All Rights Reserved.
3 Portions Copyright (C) 2007-2022 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 <stdlib.h> /* calloc() free() */
34 #include <string.h> /* memset() */
35 #include <stdio.h> /* memset() */
36 #include <limits.h> /* MAX/MIN() */
37
38 #if defined(_WIN32) && defined(HAVE_STDAFX_H)
39 #include "stdafx.h"
40 #endif /* HAVE_STDAFX_H */
41
42 #ifdef HAVE_STDINT_H
43 #include <stdint.h> /* uintptr_t */
44 #endif /* HAVE_STDINT_H */
45
46 #include "dwarf.h"
47 #include "libdwarf.h"
48 #include "libdwarf_private.h"
49 #include "dwarf_base_types.h"
50 #include "dwarf_opaque.h"
51 #include "dwarf_alloc.h"
52 #include "dwarf_error.h"
53 #include "dwarf_util.h"
54 #include "dwarf_frame.h"
55 #include "dwarf_arange.h" /* Using Arange as a way to build a list */
56 #include "dwarf_string.h"
57 #include "dwarf_safe_arithmetic.h"
58
59 /* Dwarf_Unsigned is always 64 bits */
60 #define INVALIDUNSIGNED(x) ((x) & (((Dwarf_Unsigned)1) << 63))
61
62 /* Simply assumes error is a Dwarf_Error * in its context */
63 #define FDE_NULL_CHECKS_AND_SET_DBG(fde,dbg ) \
64 do { \
65 if ((fde) == NULL) { \
66 _dwarf_error(NULL, error, DW_DLE_FDE_NULL);\
67 return DW_DLV_ERROR; \
68 } \
69 (dbg)= (fde)->fd_dbg; \
70 if (IS_INVALID_DBG((dbg))) { \
71 _dwarf_error_string(NULL, error, DW_DLE_FDE_DBG_NULL,\
72 "DW_DLE_FDE_DBG_NULL: An fde contains a stale "\
73 "Dwarf_Debug "); \
74 return DW_DLV_ERROR; \
75 } \
76 } while (0)
77
78 #define MIN(a,b) (((a) < (b))? (a):(b))
79
80 #if 0 /* dump_bytes FOR DEBUGGING */
81 static void
82 dump_bytes(const char *msg,Dwarf_Small * start, long len)
83 {
84 Dwarf_Small *end = start + len;
85 Dwarf_Small *cur = start;
86 printf("%s (0x%lx) ",msg,(unsigned long)start);
87 for (; cur < end; cur++) {
88 printf("%02x", *cur);
89 }
90 printf("\n");
91 }
92 /* Only used for debugging libdwarf. */
93 static void dump_frame_rule(char *msg,
94 struct Dwarf_Reg_Rule_s *reg_rule);
95 #endif /*0*/
96
97 static int _dwarf_initialize_fde_table(Dwarf_Debug dbg,
98 struct Dwarf_Frame_s *fde_table,
99 Dwarf_Unsigned table_real_data_size,
100 Dwarf_Error * error);
101 static void _dwarf_free_fde_table(struct Dwarf_Frame_s *fde_table);
102 static void _dwarf_init_reg_rules_ru(
103 struct Dwarf_Reg_Rule_s *base,
104 Dwarf_Unsigned first, Dwarf_Unsigned last,
105 Dwarf_Unsigned initial_value);
106 static void _dwarf_init_reg_rules_dw3(
107 Dwarf_Regtable_Entry3_i *base,
108 Dwarf_Unsigned, Dwarf_Unsigned last,
109 Dwarf_Unsigned initial_value);
110
111 /* The rules for register settings are described
112 in libdwarf.pdf and the html version.
113 (see Special Frame Registers).
114 */
115 static int
116 regerror(Dwarf_Debug dbg,Dwarf_Error *error,
117 int enumber,
118 const char *msg)
119 {
120 _dwarf_error_string(dbg,error,enumber,(char *)msg);
121 return DW_DLV_ERROR;
122 }
123
124 int
125 _dwarf_validate_register_numbers(
126 Dwarf_Debug dbg,
127 Dwarf_Error *error)
128 {
129 if (dbg->de_frame_same_value_number ==
130 dbg->de_frame_undefined_value_number) {
131 return regerror(dbg,error,DW_DLE_DEBUGFRAME_ERROR,
132 "DW_DLE_DEBUGFRAME_ERROR "
133 "same_value == undefined_value");
134 }
135 if (dbg->de_frame_cfa_col_number ==
136 dbg->de_frame_same_value_number) {
137 return regerror(dbg,error,DW_DLE_DEBUGFRAME_ERROR,
138 "DW_DLE_DEBUGFRAME_ERROR "
139 "same_value == cfa_column_number ");
140 }
141 if (dbg->de_frame_cfa_col_number ==
142 dbg->de_frame_undefined_value_number) {
143 return regerror(dbg,error,DW_DLE_DEBUGFRAME_ERROR,
144 "DW_DLE_DEBUGFRAME_ERROR "
145 "undefined_value == cfa_column_number ");
146 }
147 if ((dbg->de_frame_rule_initial_value !=
148 dbg->de_frame_same_value_number) &&
149 (dbg->de_frame_rule_initial_value !=
150 dbg->de_frame_undefined_value_number)) {
151 return regerror(dbg,error,DW_DLE_DEBUGFRAME_ERROR,
152 "DW_DLE_DEBUGFRAME_ERROR "
153 "initial_value not set to "
154 " same_value or undefined_value");
155 }
156 if (dbg->de_frame_undefined_value_number <=
157 dbg->de_frame_reg_rules_entry_count) {
158 return regerror(dbg,error,DW_DLE_DEBUGFRAME_ERROR,
159 "DW_DLE_DEBUGFRAME_ERROR "
160 "undefined_value less than number of registers");
161 }
162 if (dbg->de_frame_same_value_number <=
163 dbg->de_frame_reg_rules_entry_count) {
164 return regerror(dbg,error,DW_DLE_DEBUGFRAME_ERROR,
165 "DW_DLE_DEBUGFRAME_ERROR "
166 "same_value <= number of registers");
167 }
168 if (dbg->de_frame_cfa_col_number <=
169 dbg->de_frame_reg_rules_entry_count) {
170 return regerror(dbg,error,DW_DLE_DEBUGFRAME_ERROR,
171 "DW_DLE_DEBUGFRAME_ERROR "
172 "cfa_column <= number of registers");
173 }
174 return DW_DLV_OK;
175 }
176
177 int
178 dwarf_get_frame_section_name(Dwarf_Debug dbg,
179 const char **sec_name,
180 Dwarf_Error *error)
181 {
182 struct Dwarf_Section_s *sec = 0;
183
184 CHECK_DBG(dbg,error,"dwarf_get_frame_section_name()");
185 if (error != NULL) {
186 *error = NULL;
187 }
188 sec = &dbg->de_debug_frame;
189 if (sec->dss_size == 0) {
190 /* We don't have such a section at all. */
191 return DW_DLV_NO_ENTRY;
192 }
193 *sec_name = sec->dss_name;
194 return DW_DLV_OK;
195 }
196
197 int
198 dwarf_get_frame_section_name_eh_gnu(Dwarf_Debug dbg,
199 const char **sec_name,
200 Dwarf_Error *error)
201 {
202 struct Dwarf_Section_s *sec = 0;
203
204 CHECK_DBG(dbg,error,"dwarf_get_frame_section_name_eh_gnu()");
205 if (error != NULL) {
206 *error = NULL;
207 }
208 sec = &dbg->de_debug_frame_eh_gnu;
209 if (sec->dss_size == 0) {
210 /* We don't have such a section at all. */
211 return DW_DLV_NO_ENTRY;
212 }
213 *sec_name = sec->dss_name;
214 return DW_DLV_OK;
215 }
216
217 /*
218 This function is the heart of the debug_frame stuff. Don't even
219 think of reading this without reading both the Libdwarf and
220 consumer API carefully first. This function executes
221 frame instructions contained in a Cie or an Fde, but does in a
222 number of different ways depending on the information sought.
223 Start_instr_ptr points to the first byte of the frame instruction
224 stream, and final_instr_ptr one past the last byte.
225
226 The offsets returned in the frame instructions are factored. That
227 is they need to be multiplied by either the code_alignment_factor
228 or the data_alignment_factor, as appropriate to obtain the actual
229 offset. This makes it possible to expand an instruction stream
230 without the corresponding Cie. However, when an Fde frame instr
231 sequence is being expanded there must be a valid Cie
232 with a pointer to an initial table row.
233
234 If successful, returns DW_DLV_OK
235 And sets returned_count thru the pointer
236 if make_instr is TRUE.
237 If make_instr is FALSE returned_count
238 should NOT be used by the caller (returned_count
239 is set to 0 thru the pointer by this routine...)
240 If unsuccessful, returns DW_DLV_ERROR
241 and sets returned_error to the error code
242
243 It does not do a whole lot of input validation being a private
244 function. Please make sure inputs are valid.
245
246 (1) If make_instr is TRUE, it makes a list of pointers to
247 Dwarf_Frame_Op structures containing the frame instructions
248 executed. A pointer to this list is returned in ret_frame_instr.
249 Make_instr is TRUE only when a list of frame instructions is to be
250 returned. In this case since we are not interested
251 in the contents
252 of the table, the input Cie can be NULL. This is the only case
253 where the input Cie can be NULL.
254
255 (2) If search_pc is TRUE, frame instructions are executed till
256 either a location is reached that is greater than the
257 search_pc_val
258 provided, or all instructions are executed. At this point the
259 last row of the table generated is returned in a structure.
260 A pointer to this structure is supplied in table.
261
262 (3) This function is also used to create the initial table row
263 defined by a Cie. In this case, the Dwarf_Cie pointer cie, is
264 NULL. For an FDE, however, cie points to the associated Cie.
265
266 (4) If search_pc is TRUE and (has_more_rows and subsequent_pc
267 are non-null) then:
268 has_more_rows is set TRUE if there are instruction
269 bytes following the detection of search_over.
270 If all the instruction bytes have been seen
271 then *has_more_rows is set FALSE.
272
273 If *has_more_rows is TRUE then *subsequent_pc
274 is set to the pc value that is the following
275 row in the table.
276
277 make_instr - make list of frame instr? 0/1
278 ret_frame_instr - Ptr to list of ptrs to frame instrs
279 search_pc - Search for a pc value? 0/1
280 search_pc_val - Search for this pc value
281 initial_loc - Initial code location value.
282 start_instr_ptr - Ptr to start of frame instrs.
283 final_instr_ptr - Ptr just past frame instrs.
284 table - Ptr to struct with last row.
285 cie - Ptr to Cie used by the Fde.
286
287 Different cies may have distinct address-sizes, so the cie
288 is used, not de_pointer_size.
289
290 */
291
292 /* Cleans up the in-process linked list of these
293 in case of early exit in
294 _dwarf_exec_frame_instr. */
295 static void
296 _dwarf_free_dfi_list(Dwarf_Frame_Instr fr)
297 {
298 Dwarf_Frame_Instr cur = fr;
299 Dwarf_Frame_Instr next = 0;
300 for ( ; cur ; cur = next) {
301 next = cur->fi_next;
302 free(cur);
303 }
304 }
305 #if 0 /* printlist() for debugging */
306 static void
307 printlist(Dwarf_Frame_Instr x)
308 {
309 int i = 0;
310 Dwarf_Frame_Instr nxt = 0;
311
312 printf("=========== print cur list of ptrs\n");
313 for ( ; x ; x = nxt,++i) {
314 printf("%d inst 0x%lx nxt 0x%lx\n",
315 i,(unsigned long)x,
316 (unsigned long)x->fi_next);
317 nxt = x->fi_next;
318 }
319 printf("=========== done cur list of ptrs\n");
320 }
321 #endif /*0*/
322
323 int
324 _dwarf_exec_frame_instr(Dwarf_Bool make_instr,
325 Dwarf_Bool search_pc,
326 Dwarf_Addr search_pc_val,
327 Dwarf_Addr initial_loc,
328 Dwarf_Small * start_instr_ptr,
329 Dwarf_Small * final_instr_ptr,
330 Dwarf_Frame table,
331 Dwarf_Cie cie,
332 Dwarf_Debug dbg,
333 Dwarf_Unsigned reg_num_of_cfa,
334 Dwarf_Bool * has_more_rows,
335 Dwarf_Addr * subsequent_pc,
336 Dwarf_Frame_Instr_Head *ret_frame_instr_head,
337 Dwarf_Unsigned * returned_frame_instr_count,
338 Dwarf_Error *error)
339 {
340 /* The following macro depends on macreg and
341 machigh_reg both being unsigned to avoid
342 unintended behavior and to avoid compiler warnings when
343 high warning levels are turned on. To avoid
344 truncation turning a bogus large value into a smaller
345 sensible-seeming value we use Dwarf_Unsigned for register
346 numbers. */
347 #define ERROR_IF_REG_NUM_TOO_HIGH(macreg,machigh_reg) \
348 do { \
349 if ((macreg) >= (machigh_reg)) { \
350 SER(DW_DLE_DF_REG_NUM_TOO_HIGH); \
351 } \
352 } /*CONSTCOND */ while (0)
353 #define FREELOCALMALLOC \
354 _dwarf_free_dfi_list(ilisthead); \
355 ilisthead =0; \
356 free(dfi); dfi = 0; \
357 free(localregtab); localregtab = 0;
358 /* SER === SIMPLE_ERROR_RETURN */
359 #define SER(code) \
360 FREELOCALMALLOC; \
361 _dwarf_error(dbg,error,(code)); \
362 return DW_DLV_ERROR
363 #define SERSTRING(code,m) \
364 FREELOCALMALLOC; \
365 _dwarf_error_string(dbg,error,(code),m); \
366 return DW_DLV_ERROR
367 /* m must be a quoted string */
368 #define SERINST(m) \
369 FREELOCALMALLOC; \
370 _dwarf_error_string(dbg,error,DW_DLE_ALLOC_FAIL, \
371 "DW_DLE_ALLOC_FAIL: " m); \
372 return DW_DLV_ERROR
373
374 /* Sweeps the frame instructions. */
375 Dwarf_Small *instr_ptr = 0;
376 Dwarf_Frame_Instr dfi = 0;
377
378 /* Register numbers not limited to just 255,
379 thus not using Dwarf_Small. */
380 typedef Dwarf_Unsigned reg_num_type;
381
382 Dwarf_Unsigned factored_N_value = 0;
383 Dwarf_Signed signed_factored_N_value = 0;
384 Dwarf_Addr current_loc = initial_loc; /* code location/
385 pc-value corresponding to the frame instructions.
386 Starts at zero when the caller has no value to pass in. */
387
388 /* Must be min de_pointer_size bytes and must be at least 4 */
389 Dwarf_Unsigned adv_loc = 0;
390
391 Dwarf_Unsigned reg_count = dbg->de_frame_reg_rules_entry_count;
392 struct Dwarf_Reg_Rule_s *localregtab = calloc(reg_count,
393 sizeof(struct Dwarf_Reg_Rule_s));
394
395 struct Dwarf_Reg_Rule_s cfa_reg;
396
397 /* This is used to end executing frame instructions. */
398 /* Becomes TRUE when search_pc is TRUE and current_loc */
399 /* is greater than search_pc_val. */
400 Dwarf_Bool search_over = FALSE;
401
402 Dwarf_Addr possible_subsequent_pc = 0;
403
404 Dwarf_Half address_size = (cie)? cie->ci_address_size:
405 dbg->de_pointer_size;
406
407 /* Stack_table points to the row (Dwarf_Frame ie) being
408 pushed or popped by a remember or restore instruction.
409 Top_stack points to
410 the top of the stack of rows. */
411 Dwarf_Frame stack_table = NULL;
412 Dwarf_Frame top_stack = NULL;
413
414 /* These are used only when make_instr is TRUE. Curr_instr is a
415 pointer to the current frame instruction executed.
416 Curr_instr_ptr, head_instr_list, and curr_instr_list are
417 used to form a chain of Dwarf_Frame_Op structs.
418 Dealloc_instr_ptr is
419 used to deallocate the structs used to form the chain.
420 Head_instr_block points to a contiguous list of
421 pointers to the
422 Dwarf_Frame_Op structs executed. */
423 /* Build single linked list of instrs, and
424 at end turn into array. */
425 Dwarf_Frame_Instr ilisthead = 0;
426 Dwarf_Frame_Instr *ilistlastptr = &ilisthead;
427 /* Counts the number of frame instructions
428 in the returned instrs if instruction
429 details are asked for. Else 0. */
430 Dwarf_Unsigned instr_count = 0;
431
432 /* These are the alignment_factors taken from the Cie provided.
433 When no input Cie is provided they are set to 1, because only
434 factored offsets are required. */
435 Dwarf_Signed code_alignment_factor = 1;
436 Dwarf_Signed data_alignment_factor = 1;
437
438 /* This flag indicates when an actual alignment factor
439 is needed.
440 So if a frame instruction that computes an offset
441 using an alignment factor is encountered when this
442 flag is set, an error is returned because the Cie
443 did not have a valid augmentation. */
444 Dwarf_Bool need_augmentation = FALSE;
445 Dwarf_Unsigned instr_area_length = 0;
446
447 Dwarf_Unsigned i = 0;
448
449 /* Initialize first row from associated Cie.
450 Using temp regs explicitly */
451
452 if (!localregtab) {
453 SER(DW_DLE_ALLOC_FAIL);
454 }
455 {
456 struct Dwarf_Reg_Rule_s *t1reg = localregtab;
457 if (cie != NULL && cie->ci_initial_table != NULL) {
458 unsigned minregcount = 0;
459 unsigned curreg = 0;
460 struct Dwarf_Reg_Rule_s *t2reg =
461 cie->ci_initial_table->fr_reg;
462
463 if (reg_count != cie->ci_initial_table->fr_reg_count) {
464 /* Should never happen,
465 it makes no sense to have the
466 table sizes change. There
467 is no real allowance for
468 the set of registers
469 to change dynamically
470 in a single Dwarf_Debug
471 (except the size can be set
472 near initial Dwarf_Debug
473 creation time). */
474 SER(DW_DLE_FRAME_REGISTER_COUNT_MISMATCH);
475 }
476 minregcount =
477 (unsigned)MIN(reg_count,
478 cie->ci_initial_table->fr_reg_count);
479 for ( ; curreg < minregcount ;
480 curreg++, t1reg++, t2reg++) {
481 *t1reg = *t2reg;
482 } cfa_reg =
483 cie->ci_initial_table->fr_cfa_rule;
484 } else {
485 _dwarf_init_reg_rules_ru(localregtab,0,reg_count,
486 dbg->de_frame_rule_initial_value);
487 _dwarf_init_reg_rules_ru(&cfa_reg,0, 1,
488 dbg->de_frame_rule_initial_value);
489 }
490 }
491 /* The idea here is that the code_alignment_factor and
492 data_alignment_factor which are needed for certain
493 instructions are valid only when the Cie has a proper
494 augmentation string. So if the augmentation is not
495 right, only Frame instruction can be read. */
496 if (cie != NULL && cie->ci_augmentation != NULL) {
497 code_alignment_factor = cie->ci_code_alignment_factor;
498 data_alignment_factor = cie->ci_data_alignment_factor;
499 } else {
500 need_augmentation = !make_instr;
501 }
502 instr_ptr = start_instr_ptr;
503 instr_area_length = (uintptr_t)
504 (final_instr_ptr - start_instr_ptr);
505 while ((instr_ptr < final_instr_ptr) && (!search_over)) {
506 Dwarf_Small instr = 0;
507 Dwarf_Small opcode = 0;
508 reg_num_type reg_no = 0;
509 Dwarf_Unsigned adv_pc = 0;
510 Dwarf_Off fp_instr_offset = 0;
511 Dwarf_Small * base_instr_ptr = 0;
512
513 if (instr_ptr < start_instr_ptr) {
514 SERINST("DW_DLE_DF_NEW_LOC_LESS_OLD_LOC: "
515 "Following instruction bytes we find impossible "
516 "decrease in a pointer");
517 }
518 fp_instr_offset = instr_ptr - start_instr_ptr;
519 if (instr_ptr >= final_instr_ptr) {
520 _dwarf_error(NULL, error, DW_DLE_DF_FRAME_DECODING_ERROR);
521 return DW_DLV_ERROR;
522 }
523 instr = *(Dwarf_Small *) instr_ptr;
524 instr_ptr += sizeof(Dwarf_Small);
525 base_instr_ptr = instr_ptr;
526 if ((instr & 0xc0) == 0x00) {
527 opcode = instr; /* is really extended op */
528 } else {
529 opcode = instr & 0xc0; /* is base op */
530 }
531 if (make_instr) {
532 dfi = calloc(1,sizeof(*dfi));
533 if (!dfi) {
534 SERINST("DW_CFA_advance_loc out of memory");
535 }
536 dfi->fi_op = opcode;
537 dfi->fi_instr_offset = fp_instr_offset;
538 dfi->fi_fields = "";
539 }
540 switch (opcode) {
541 case DW_CFA_lo_user: {
542 if (make_instr) {
543 dfi->fi_fields = "";
544 }
545 }
546 break;
547 case DW_CFA_advance_loc: {
548 Dwarf_Unsigned adv_pc_val = 0;
549 int alres = 0;
550
551 /* base op */
552 adv_pc_val = instr &DW_FRAME_INSTR_OFFSET_MASK;
553 if (need_augmentation) {
554 SER(DW_DLE_DF_NO_CIE_AUGMENTATION);
555 }
556
557 /* CHECK OVERFLOW */
558 alres = _dwarf_uint64_mult(adv_pc_val,
559 code_alignment_factor,&adv_pc,dbg,error);
560 if (alres == DW_DLV_ERROR) {
561 FREELOCALMALLOC;
562 return DW_DLV_ERROR;
563 }
564 if (INVALIDUNSIGNED(adv_pc)) {
565 SERSTRING(DW_DLE_ARITHMETIC_OVERFLOW,
566 "DW_DLE_ARITHMETIC_OVERFLOW "
567 "negative new location");
568 }
569
570 possible_subsequent_pc = current_loc +
571 (Dwarf_Unsigned)adv_pc;
572 if (possible_subsequent_pc < current_loc &&
573 possible_subsequent_pc < adv_pc) {
574 SERSTRING(DW_DLE_ARITHMETIC_OVERFLOW,
575 "DW_DLE_ARITHMETIC_OVERFLOW "
576 "add overflowed");
577 }
578
579 search_over = search_pc &&
580 (possible_subsequent_pc > search_pc_val);
581 /* If gone past pc needed, retain old pc. */
582 if (!search_over) {
583 current_loc = possible_subsequent_pc;
584 }
585 if (make_instr) {
586 dfi->fi_fields = "uc";
587 dfi->fi_u0 = adv_pc_val;
588 dfi->fi_code_align_factor = code_alignment_factor;
589 }
590 }
591 break;
592 case DW_CFA_offset: { /* base op */
593 int adres = 0;
594 Dwarf_Signed result = 0;
595 reg_no = (reg_num_type) (instr &
596 DW_FRAME_INSTR_OFFSET_MASK);
597 ERROR_IF_REG_NUM_TOO_HIGH(reg_no, reg_count);
598 adres = _dwarf_leb128_uword_wrapper(dbg,
599 &instr_ptr,final_instr_ptr,
600 &factored_N_value,error);
601 if (adres != DW_DLV_OK) {
602 FREELOCALMALLOC;
603 return adres;
604 }
605 if (need_augmentation) {
606 SER( DW_DLE_DF_NO_CIE_AUGMENTATION);
607 }
608 if (INVALIDUNSIGNED(factored_N_value)) {
609 SERSTRING(DW_DLE_ARITHMETIC_OVERFLOW,
610 "DW_DLE_ARITHMETIC_OVERFLOW "
611 "negative factored_N_value location");
612 }
613 /* CHECK OVERFLOW */
614 adres = _dwarf_int64_mult(
615 (Dwarf_Signed)factored_N_value,
616 data_alignment_factor,
617 &result,dbg, error);
618 if (adres == DW_DLV_ERROR) {
619 FREELOCALMALLOC;
620 return DW_DLV_ERROR;
621 }
622 localregtab[reg_no].ru_offset = result;
623 localregtab[reg_no].ru_is_offset = 1;
624 localregtab[reg_no].ru_register = reg_num_of_cfa;
625 localregtab[reg_no].ru_value_type = DW_EXPR_OFFSET;
626 if (make_instr) {
627 dfi->fi_fields = "rud";
628 dfi->fi_u0 = reg_no;
629 dfi->fi_u1 = factored_N_value;
630 dfi->fi_data_align_factor =
631 data_alignment_factor;
632 }
633 }
634 break;
635 case DW_CFA_restore: { /* base op */
636 reg_no = (instr & DW_FRAME_INSTR_OFFSET_MASK);
637 ERROR_IF_REG_NUM_TOO_HIGH(reg_no, reg_count);
638
639 if (cie != NULL && cie->ci_initial_table != NULL) {
640 localregtab[reg_no] =
641 cie->ci_initial_table->fr_reg[reg_no];
642 } else if (!make_instr) {
643 SER(DW_DLE_DF_MAKE_INSTR_NO_INIT);
644 }
645 if (make_instr) {
646 dfi->fi_fields = "r";
647 dfi->fi_u0 = reg_no;
648 }
649 }
650 break;
651 case DW_CFA_set_loc: {
652 Dwarf_Addr new_loc = 0;
653 int adres = 0;
654 adres=_dwarf_read_unaligned_ck_wrapper(dbg,
655 &new_loc,
656 instr_ptr, address_size,
657 final_instr_ptr,error);
658 if (adres != DW_DLV_OK) {
659 FREELOCALMALLOC;
660 return adres;
661 }
662 instr_ptr += address_size;
663 if (new_loc != 0 && current_loc != 0) {
664 /* Pre-relocation or before current_loc
665 is set the test comparing new_loc
666 and current_loc makes no
667 sense. Testing for non-zero (above) is a way
668 (fallible) to check that current_loc, new_loc
669 are already relocated. */
670 if (new_loc <= current_loc) {
671 /* Within a frame, address must increase.
672 Seemingly it has not.
673 Seems to be an error. */
674 SER(DW_DLE_DF_NEW_LOC_LESS_OLD_LOC);
675 }
676 }
677 search_over = search_pc && (new_loc > search_pc_val);
678 /* If gone past pc needed, retain old pc. */
679 possible_subsequent_pc = new_loc;
680 if (!search_over) {
681 current_loc = possible_subsequent_pc;
682 }
683 if (make_instr) {
684 dfi->fi_fields = "u";
685 dfi->fi_u0 = new_loc;
686 }
687 }
688 break;
689 case DW_CFA_advance_loc1:
690 {
691 int adres = 0;
692 Dwarf_Unsigned advloc_val = 0;
693 adres=_dwarf_read_unaligned_ck_wrapper(dbg,
694 &advloc_val,
695 instr_ptr, sizeof(Dwarf_Small),
696 final_instr_ptr,error);
697 if (adres != DW_DLV_OK) {
698 FREELOCALMALLOC;
699 return adres;
700 }
701 instr_ptr += sizeof(Dwarf_Small);
702 if (need_augmentation) {
703 SER(DW_DLE_DF_NO_CIE_AUGMENTATION);
704 }
705 /* CHECK OVERFLOW */
706 adres = _dwarf_uint64_mult(
707 advloc_val,
708 code_alignment_factor,
709 &adv_loc,dbg,error);
710 if (adres == DW_DLV_ERROR) {
711 FREELOCALMALLOC;
712 return adres;
713 }
714
715 /* CHECK OVERFLOW add */
716 possible_subsequent_pc = current_loc + adv_loc;
717 if (possible_subsequent_pc < current_loc &&
718 possible_subsequent_pc < adv_loc) {
719 SERSTRING(DW_DLE_ARITHMETIC_OVERFLOW,
720 "DW_DLE_ARITHMETIC_OVERFLOW "
721 "add overflowed calcating subsequent pc");
722 }
723 search_over = search_pc &&
724 (possible_subsequent_pc > search_pc_val);
725
726 /* If gone past pc needed, retain old pc. */
727 if (!search_over) {
728 current_loc = possible_subsequent_pc;
729 }
730 if (make_instr) {
731 dfi->fi_fields = "uc";
732 dfi->fi_u0 = advloc_val;
733 dfi->fi_code_align_factor =
734 code_alignment_factor;
735 }
736 break;
737 }
738
739 case DW_CFA_advance_loc2:
740 {
741 int adres = 0;
742 Dwarf_Unsigned advloc_val = 0;
743 adres=_dwarf_read_unaligned_ck_wrapper(dbg, &advloc_val,
744 instr_ptr, DWARF_HALF_SIZE,
745 final_instr_ptr,error);
746 if (adres != DW_DLV_OK) {
747 FREELOCALMALLOC;
748 return adres;
749 }
750 instr_ptr += DWARF_HALF_SIZE;
751 if (need_augmentation) {
752 SER(DW_DLE_DF_NO_CIE_AUGMENTATION);
753 }
754 /* CHECK OVERFLOW */
755 adres = _dwarf_uint64_mult(
756 advloc_val,
757 code_alignment_factor,
758 &adv_loc,dbg,error);
759 if (adres == DW_DLV_ERROR) {
760 FREELOCALMALLOC;
761 return adres;
762 }
763 /* CHECK OVERFLOW add */
764 if (INVALIDUNSIGNED(adv_loc)) {
765 SERSTRING( DW_DLE_ARITHMETIC_OVERFLOW,
766 "DW_DLE_ARITHMETIC_OVERFLOW "
767 "negative new location");
768 }
769
770 /* CHECK OVERFLOW add */
771 possible_subsequent_pc = current_loc + adv_loc;
772 if (possible_subsequent_pc < current_loc &&
773 possible_subsequent_pc < adv_loc) {
774 SERSTRING(DW_DLE_ARITHMETIC_OVERFLOW,
775 "DW_DLE_ARITHMETIC_OVERFLOW "
776 "add overflowed");
777 }
778 search_over = search_pc &&
779 (possible_subsequent_pc > search_pc_val);
780 /* If gone past pc needed, retain old pc. */
781 if (!search_over) {
782 current_loc = possible_subsequent_pc;
783 }
784 if (make_instr) {
785 dfi->fi_fields = "uc";
786 dfi->fi_u0 = advloc_val;
787 dfi->fi_code_align_factor =
788 code_alignment_factor;
789 }
790 break;
791 }
792
793 case DW_CFA_advance_loc4:
794 {
795 int adres = 0;
796 Dwarf_Unsigned advloc_val = 0;
797
798 adres=_dwarf_read_unaligned_ck_wrapper(dbg, &advloc_val,
799 instr_ptr, DWARF_32BIT_SIZE,
800 final_instr_ptr,error);
801 if (adres != DW_DLV_OK) {
802 FREELOCALMALLOC;
803 return adres;
804 }
805 instr_ptr += DWARF_32BIT_SIZE;
806 if (need_augmentation) {
807 SER(DW_DLE_DF_NO_CIE_AUGMENTATION);
808 }
809 /* CHECK OVERFLOW */
810 adres = _dwarf_uint64_mult(
811 advloc_val,
812 code_alignment_factor,
813 &adv_loc,dbg,error);
814 if (adres == DW_DLV_ERROR) {
815 FREELOCALMALLOC;
816 return adres;
817 }
818 /* CHECK OVERFLOW add */
819 possible_subsequent_pc = current_loc + adv_loc;
820 if (possible_subsequent_pc < current_loc &&
821 possible_subsequent_pc < adv_loc) {
822 SERSTRING(DW_DLE_ARITHMETIC_OVERFLOW,
823 "DW_DLE_ARITHMETIC_OVERFLOW "
824 "unsigned add overflowed");
825 }
826
827 search_over = search_pc &&
828 (possible_subsequent_pc > search_pc_val);
829 /* If gone past pc needed, retain old pc. */
830 if (!search_over) {
831 current_loc = possible_subsequent_pc;
832 }
833 if (make_instr) {
834 dfi->fi_fields = "uc";
835 dfi->fi_u0 = advloc_val;
836 dfi->fi_code_align_factor =
837 code_alignment_factor;
838 }
839 break;
840 }
841 case DW_CFA_MIPS_advance_loc8:
842 {
843 int adres = 0;
844 Dwarf_Unsigned advloc_val = 0;
845 adres=_dwarf_read_unaligned_ck_wrapper(dbg, &advloc_val,
846 instr_ptr, DWARF_64BIT_SIZE,
847 final_instr_ptr,error);
848 if (adres != DW_DLV_OK) {
849 FREELOCALMALLOC;
850 return adres;
851 }
852 instr_ptr += DWARF_64BIT_SIZE;
853 if (need_augmentation) {
854 SER(DW_DLE_DF_NO_CIE_AUGMENTATION);
855 }
856 /* CHECK OVERFLOW */
857 adres = _dwarf_uint64_mult(advloc_val,
858 code_alignment_factor,&adv_loc,
859 dbg,error);
860 if (adres == DW_DLV_ERROR) {
861 FREELOCALMALLOC;
862 return adres;
863 }
864 /* CHECK OVERFLOW add */
865 possible_subsequent_pc = current_loc + adv_loc;
866 if (possible_subsequent_pc < current_loc &&
867 possible_subsequent_pc < adv_loc) {
868 SERSTRING(DW_DLE_ARITHMETIC_OVERFLOW,
869 "DW_DLE_ARITHMETIC_OVERFLOW "
870 "unsigned add overflowed");
871 }
872 search_over = search_pc &&
873 (possible_subsequent_pc > search_pc_val);
874 /* If gone past pc needed, retain old pc. */
875 if (!search_over) {
876 current_loc = possible_subsequent_pc;
877 }
878 if (make_instr) {
879 dfi->fi_fields = "u";
880 dfi->fi_u0 = advloc_val;
881 dfi->fi_code_align_factor =
882 code_alignment_factor;
883 }
884 break;
885 }
886
887 case DW_CFA_offset_extended:
888 {
889 Dwarf_Unsigned lreg = 0;
890 Dwarf_Signed result = 0;
891 int adres = 0;
892 adres = _dwarf_leb128_uword_wrapper(dbg,
893 &instr_ptr,final_instr_ptr,
894 &lreg,error);
895 if (adres != DW_DLV_OK) {
896 FREELOCALMALLOC;
897 return adres;
898 }
899 reg_no = (reg_num_type) lreg;
900 ERROR_IF_REG_NUM_TOO_HIGH(reg_no, reg_count);
901 adres = _dwarf_leb128_uword_wrapper(dbg,
902 &instr_ptr,final_instr_ptr,
903 &factored_N_value,error);
904 if (adres != DW_DLV_OK) {
905 FREELOCALMALLOC;
906 return adres;
907 }
908 if (need_augmentation) {
909 SER(DW_DLE_DF_NO_CIE_AUGMENTATION);
910 }
911 if (INVALIDUNSIGNED(factored_N_value)) {
912 SERSTRING(DW_DLE_ARITHMETIC_OVERFLOW,
913 "DW_DLE_ARITHMETIC_OVERFLOW "
914 "negative new location");
915 }
916 /* CHECK OVERFLOW */
917 adres = _dwarf_int64_mult((Dwarf_Signed)factored_N_value,
918 data_alignment_factor, &result,
919 dbg,error);
920 if (adres == DW_DLV_ERROR) {
921 FREELOCALMALLOC;
922 return adres;
923 }
924 localregtab[reg_no].ru_is_offset = 1;
925 localregtab[reg_no].ru_value_type = DW_EXPR_OFFSET;
926 localregtab[reg_no].ru_register = reg_num_of_cfa;
927 localregtab[reg_no].ru_offset = result;
928 if (make_instr) {
929 dfi->fi_fields = "rud";
930 dfi->fi_u0 = lreg;
931 dfi->fi_u1 = factored_N_value;
932 dfi->fi_data_align_factor =
933 data_alignment_factor;
934 }
935 break;
936 }
937
938 case DW_CFA_restore_extended:
939 {
940 Dwarf_Unsigned lreg = 0;
941 int adres = 0;
942
943 adres = _dwarf_leb128_uword_wrapper(dbg,
944 &instr_ptr,final_instr_ptr,
945 &lreg,error);
946 if (adres != DW_DLV_OK) {
947 FREELOCALMALLOC;
948 return adres;
949 }
950 reg_no = (reg_num_type) lreg;
951 ERROR_IF_REG_NUM_TOO_HIGH(reg_no, reg_count);
952 if (cie != NULL && cie->ci_initial_table != NULL) {
953 localregtab[reg_no] =
954 cie->ci_initial_table->fr_reg[reg_no];
955 } else {
956 if (!make_instr) {
957 SER(DW_DLE_DF_MAKE_INSTR_NO_INIT);
958 }
959 }
960 if (make_instr) {
961 dfi->fi_fields = "r";
962 dfi->fi_u0 = lreg;
963 }
964 break;
965 }
966
967 case DW_CFA_undefined:
968 {
969 Dwarf_Unsigned lreg = 0;
970 int adres = 0;
971
972 adres = _dwarf_leb128_uword_wrapper(dbg,
973 &instr_ptr,final_instr_ptr,
974 &lreg,error);
975 if (adres != DW_DLV_OK) {
976 FREELOCALMALLOC;
977 return adres;
978 }
979 reg_no = (reg_num_type) lreg;
980 ERROR_IF_REG_NUM_TOO_HIGH(reg_no, reg_count);
981 localregtab[reg_no].ru_is_offset = 0;
982 localregtab[reg_no].ru_value_type = DW_EXPR_OFFSET;
983 localregtab[reg_no].ru_register =
984 dbg->de_frame_undefined_value_number;
985 localregtab[reg_no].ru_offset = 0;
986 if (make_instr) {
987 dfi->fi_fields = "r";
988 dfi->fi_u0 = lreg;
989 }
990 break;
991 }
992
993 case DW_CFA_same_value:
994 {
995 Dwarf_Unsigned lreg = 0;
996 int adres = 0;
997
998 adres = _dwarf_leb128_uword_wrapper(dbg,
999 &instr_ptr,final_instr_ptr,
1000 &lreg,error);
1001 if (adres != DW_DLV_OK) {
1002 FREELOCALMALLOC;
1003 return adres;
1004 }
1005 reg_no = (reg_num_type) lreg;
1006 ERROR_IF_REG_NUM_TOO_HIGH(reg_no, reg_count);
1007 localregtab[reg_no].ru_is_offset = 0;
1008 localregtab[reg_no].ru_value_type = DW_EXPR_OFFSET;
1009 localregtab[reg_no].ru_register =
1010 dbg->de_frame_same_value_number;
1011 localregtab[reg_no].ru_offset = 0;
1012 if (make_instr) {
1013 dfi->fi_fields = "r";
1014 dfi->fi_u0 = lreg;
1015 }
1016 break;
1017 }
1018
1019 case DW_CFA_register:
1020 {
1021 Dwarf_Unsigned lreg;
1022 reg_num_type reg_noA = 0;
1023 reg_num_type reg_noB = 0;
1024 int adres = 0;
1025
1026 adres = _dwarf_leb128_uword_wrapper(dbg,
1027 &instr_ptr,final_instr_ptr,
1028 &lreg,error);
1029 if (adres != DW_DLV_OK) {
1030 FREELOCALMALLOC;
1031 return adres;
1032 }
1033 reg_noA = (reg_num_type) lreg;
1034 ERROR_IF_REG_NUM_TOO_HIGH(reg_noA, reg_count);
1035 adres = _dwarf_leb128_uword_wrapper(dbg,
1036 &instr_ptr,final_instr_ptr,
1037 &lreg,error);
1038 if (adres != DW_DLV_OK) {
1039 FREELOCALMALLOC;
1040 return adres;
1041 }
1042 reg_noB = (reg_num_type) lreg;
1043 if (reg_noB > reg_count) {
1044 SER(DW_DLE_DF_REG_NUM_TOO_HIGH);
1045 }
1046 localregtab[reg_noA].ru_is_offset = 0;
1047 localregtab[reg_noA].ru_value_type = DW_EXPR_OFFSET;
1048 localregtab[reg_noA].ru_register = reg_noB;
1049 localregtab[reg_noA].ru_offset = 0;
1050 if (make_instr) {
1051 dfi->fi_fields = "rr";
1052 dfi->fi_u0 = reg_noA;
1053 dfi->fi_u1 = reg_noB;
1054 }
1055 break;
1056 }
1057
1058 case DW_CFA_remember_state:
1059 {
1060 stack_table = (Dwarf_Frame)
1061 _dwarf_get_alloc(dbg, DW_DLA_FRAME, 1);
1062 if (stack_table == NULL) {
1063 SER(DW_DLE_DF_ALLOC_FAIL);
1064 }
1065 for (i = 0; i < reg_count; i++) {
1066 stack_table->fr_reg[i] = localregtab[i];
1067 }
1068 stack_table->fr_cfa_rule = cfa_reg;
1069 if (top_stack != NULL) {
1070 stack_table->fr_next = top_stack;
1071 }
1072 top_stack = stack_table;
1073 if (make_instr) {
1074 dfi->fi_fields = "";
1075 }
1076 }
1077 break;
1078 case DW_CFA_restore_state:
1079 {
1080 if (top_stack == NULL) {
1081 SER(DW_DLE_DF_POP_EMPTY_STACK);
1082 }
1083 stack_table = top_stack;
1084 top_stack = stack_table->fr_next;
1085 for (i = 0; i < reg_count; i++) {
1086 localregtab[i] = stack_table->fr_reg[i];
1087 }
1088 cfa_reg = stack_table->fr_cfa_rule;
1089 dwarf_dealloc(dbg, stack_table, DW_DLA_FRAME);
1090 if (make_instr) {
1091 dfi->fi_fields = "";
1092 }
1093 break;
1094 }
1095
1096 case DW_CFA_def_cfa:
1097 {
1098 Dwarf_Unsigned lreg = 0;
1099 int adres = 0;
1100 Dwarf_Off nonfactoredoffset = 0;
1101
1102 adres = _dwarf_leb128_uword_wrapper(dbg,
1103 &instr_ptr,final_instr_ptr,
1104 &lreg,error);
1105 if (adres != DW_DLV_OK) {
1106 FREELOCALMALLOC;
1107 return adres;
1108 }
1109 reg_no = lreg;
1110 ERROR_IF_REG_NUM_TOO_HIGH(reg_no, reg_count);
1111 adres = _dwarf_leb128_uword_wrapper(dbg,
1112 &instr_ptr,final_instr_ptr,
1113 &nonfactoredoffset,error);
1114 if (adres != DW_DLV_OK) {
1115 FREELOCALMALLOC;
1116 return adres;
1117 }
1118 if (need_augmentation) {
1119 SER(DW_DLE_DF_NO_CIE_AUGMENTATION);
1120 }
1121 cfa_reg.ru_is_offset = 1;
1122 cfa_reg.ru_value_type = DW_EXPR_OFFSET;
1123 cfa_reg.ru_register = reg_no;
1124 if (INVALIDUNSIGNED(nonfactoredoffset)) {
1125 SERSTRING(DW_DLE_ARITHMETIC_OVERFLOW,
1126 "DW_DLE_ARITHMETIC_OVERFLOW "
1127 "DW_CFA_def_cfa offset unrepresantable "
1128 "as signed");
1129 }
1130 cfa_reg.ru_offset = (Dwarf_Signed)nonfactoredoffset;
1131 if (make_instr) {
1132 dfi->fi_fields = "ru";
1133 dfi->fi_u0 = lreg;
1134 dfi->fi_u1 = nonfactoredoffset;
1135 }
1136 break;
1137 }
1138
1139 case DW_CFA_def_cfa_register:
1140 {
1141 Dwarf_Unsigned lreg = 0;
1142 int adres = 0;
1143
1144 adres = _dwarf_leb128_uword_wrapper(dbg,
1145 &instr_ptr,final_instr_ptr,
1146 &lreg,error);
1147 if (adres != DW_DLV_OK) {
1148 FREELOCALMALLOC;
1149 return adres;
1150 }
1151 reg_no = (reg_num_type) lreg;
1152 ERROR_IF_REG_NUM_TOO_HIGH(reg_no, reg_count);
1153 cfa_reg.ru_register = (Dwarf_Half)reg_no;
1154 /* Do NOT set ru_offset_or_block_len or
1155 ru_is_off here.
1156 See dwarf2/3 spec. */
1157 if (make_instr) {
1158 dfi->fi_fields = "r";
1159 dfi->fi_u0 = lreg;
1160 }
1161 break;
1162 }
1163
1164 case DW_CFA_def_cfa_offset:
1165 {
1166 int adres = 0;
1167 adres = _dwarf_leb128_uword_wrapper(dbg,
1168 &instr_ptr,final_instr_ptr,
1169 &factored_N_value,error);
1170 if (adres != DW_DLV_OK) {
1171 FREELOCALMALLOC;
1172 return adres;
1173 }
1174 if (need_augmentation) {
1175 SER(DW_DLE_DF_NO_CIE_AUGMENTATION);
1176 }
1177 /* Do set ru_is_off here, as here factored_N_value
1178 counts. */
1179 cfa_reg.ru_is_offset = 1;
1180 cfa_reg.ru_value_type = DW_EXPR_OFFSET;
1181 if (INVALIDUNSIGNED(factored_N_value)) {
1182 SERSTRING(DW_DLE_ARITHMETIC_OVERFLOW,
1183 "DW_DLE_ARITHMETIC_OVERFLOW "
1184 "DW_CFA_def_cfa_offset unrepresantable "
1185 "as signed");
1186 }
1187 cfa_reg.ru_offset = (Dwarf_Signed)factored_N_value;
1188 if (make_instr) {
1189 dfi->fi_fields = "u";
1190 dfi->fi_u0 = factored_N_value;
1191 }
1192 break;
1193 }
1194 /* This is for Metaware with augmentation string HC
1195 We do not really know what to do with it. */
1196 case DW_CFA_METAWARE_info:
1197 {
1198 int adres = 0;
1199 adres = _dwarf_leb128_uword_wrapper(dbg,
1200 &instr_ptr,final_instr_ptr,
1201 &factored_N_value,error);
1202 if (adres != DW_DLV_OK) {
1203 FREELOCALMALLOC;
1204 return adres;
1205 }
1206 /* Not really known what the value means or is. */
1207 cfa_reg.ru_is_offset = 1;
1208 cfa_reg.ru_value_type = DW_EXPR_OFFSET;
1209 if (INVALIDUNSIGNED(factored_N_value)) {
1210 SERSTRING(DW_DLE_ARITHMETIC_OVERFLOW,
1211 "DW_DLE_ARITHMETIC_OVERFLOW "
1212 "DW_CFA_METAWARE_info unrepresantable as signed");
1213 }
1214 cfa_reg.ru_offset = (Dwarf_Signed)factored_N_value;
1215 if (make_instr) {
1216 dfi->fi_fields = "u";
1217 dfi->fi_u0 = factored_N_value;
1218 }
1219 break;
1220 }
1221 case DW_CFA_nop:
1222 {
1223 if (make_instr) {
1224 dfi->fi_fields = "";
1225 }
1226 break;
1227 }
1228 /* DWARF3 ops begin here. */
1229 case DW_CFA_def_cfa_expression: {
1230 /* A single DW_FORM_block representing a dwarf
1231 expression. The form block establishes the way to
1232 compute the CFA. */
1233 Dwarf_Unsigned block_len = 0;
1234 int adres = 0;
1235
1236 adres = _dwarf_leb128_uword_wrapper(dbg,
1237 &instr_ptr,final_instr_ptr,
1238 &block_len,error);
1239 if (adres != DW_DLV_OK) {
1240 FREELOCALMALLOC;
1241 return adres;
1242 }
1243 cfa_reg.ru_is_offset = 0; /* arbitrary */
1244 cfa_reg.ru_value_type = DW_EXPR_EXPRESSION;
1245 cfa_reg.ru_block.bl_len = block_len;
1246 cfa_reg.ru_block.bl_data = instr_ptr;
1247 if (make_instr) {
1248 dfi->fi_fields = "b";
1249 dfi->fi_expr.bl_len = block_len;
1250 dfi->fi_expr.bl_data = instr_ptr;
1251 }
1252 if (block_len >= instr_area_length) {
1253 SERSTRING(DW_DLE_DF_FRAME_DECODING_ERROR,
1254 "DW_DLE_DF_FRAME_DECODING_ERROR: "
1255 "DW_CFA_def_cfa_expression "
1256 "block len overflows instructions "
1257 "available range.");
1258 }
1259 instr_ptr += block_len;
1260 if (instr_area_length < block_len ||
1261 instr_ptr < base_instr_ptr) {
1262 SERSTRING(DW_DLE_DF_FRAME_DECODING_ERROR,
1263 "DW_DLE_DF_FRAME_DECODING_ERROR: "
1264 "DW_CFA_def_cfa_expression "
1265 "block len overflows instructions "
1266 "available range.");
1267 }
1268 }
1269 break;
1270 case DW_CFA_expression: {
1271 /* An unsigned leb128 value is the first operand (a
1272 register number). The second operand is single
1273 DW_FORM_block representing a dwarf expression. The
1274 evaluator pushes the CFA on the evaluation stack
1275 then evaluates the expression to compute the value
1276 of the register contents. */
1277 Dwarf_Unsigned lreg = 0;
1278 Dwarf_Unsigned block_len = 0;
1279 int adres = 0;
1280
1281 adres = _dwarf_leb128_uword_wrapper(dbg,
1282 &instr_ptr,final_instr_ptr,
1283 &lreg,error);
1284 if (adres != DW_DLV_OK) {
1285 FREELOCALMALLOC;
1286 return adres;
1287 }
1288 reg_no = (reg_num_type) lreg;
1289 ERROR_IF_REG_NUM_TOO_HIGH(reg_no, reg_count);
1290
1291 adres = _dwarf_leb128_uword_wrapper(dbg,
1292 &instr_ptr,final_instr_ptr,
1293 &block_len,error);
1294 if (adres != DW_DLV_OK) {
1295 FREELOCALMALLOC;
1296 return adres;
1297 }
1298 localregtab[lreg].ru_is_offset = 0; /* arbitrary */
1299 localregtab[lreg].ru_value_type = DW_EXPR_EXPRESSION;
1300 localregtab[lreg].ru_block.bl_data = instr_ptr;
1301 localregtab[lreg].ru_block.bl_len = block_len;
1302 if (make_instr) {
1303 dfi->fi_fields = "rb";
1304 dfi->fi_u0 = lreg;
1305 dfi->fi_expr.bl_len = block_len;
1306 dfi->fi_expr.bl_data = instr_ptr;
1307 }
1308 instr_ptr += block_len;
1309 if (instr_area_length < block_len ||
1310 instr_ptr < base_instr_ptr) {
1311 SERSTRING(DW_DLE_DF_FRAME_DECODING_ERROR,
1312 "DW_DLE_DF_FRAME_DECODING_ERROR: "
1313 "DW_CFA_expression "
1314 "block len overflows instructions "
1315 "available range.");
1316 }
1317 }
1318 break;
1319 case DW_CFA_offset_extended_sf: {
1320 /* The first operand is an unsigned leb128 register
1321 number. The second is a signed factored offset.
1322 Identical to DW_CFA_offset_extended except the
1323 second operand is signed */
1324 Dwarf_Unsigned lreg = 0;
1325 int adres = 0;
1326 Dwarf_Signed result = 0;
1327
1328 adres = _dwarf_leb128_uword_wrapper(dbg,
1329 &instr_ptr,final_instr_ptr,
1330 &lreg,error);
1331 if (adres != DW_DLV_OK) {
1332 FREELOCALMALLOC;
1333 return adres;
1334 }
1335 reg_no = (reg_num_type) lreg;
1336 ERROR_IF_REG_NUM_TOO_HIGH(reg_no, reg_count);
1337 adres = _dwarf_leb128_sword_wrapper(dbg,
1338 &instr_ptr,final_instr_ptr,
1339 &signed_factored_N_value,error);
1340 if (adres != DW_DLV_OK) {
1341 FREELOCALMALLOC;
1342 return adres;
1343 }
1344 if (need_augmentation) {
1345 SER(DW_DLE_DF_NO_CIE_AUGMENTATION);
1346 }
1347 /* CHECK OVERFLOW */
1348 adres = _dwarf_int64_mult(signed_factored_N_value,
1349 data_alignment_factor,
1350 &result,dbg,error);
1351 if (adres == DW_DLV_ERROR) {
1352 FREELOCALMALLOC;
1353 return adres;
1354 }
1355 localregtab[reg_no].ru_is_offset = 1;
1356 localregtab[reg_no].ru_value_type = DW_EXPR_OFFSET;
1357 localregtab[reg_no].ru_register = reg_num_of_cfa;
1358 localregtab[reg_no].ru_offset = result;
1359 if (make_instr) {
1360 dfi->fi_fields = "rsd";
1361 dfi->fi_u0 = lreg;
1362 dfi->fi_s1 = signed_factored_N_value;
1363 dfi->fi_data_align_factor =
1364 data_alignment_factor;
1365 }
1366 }
1367 break;
1368 case DW_CFA_def_cfa_sf: {
1369 /* The first operand is an unsigned leb128 register
1370 number. The second is a signed leb128 factored
1371 offset. Identical to DW_CFA_def_cfa except
1372 that the second operand is signed
1373 and factored. */
1374 Dwarf_Unsigned lreg = 0;
1375 int adres = 0;
1376 Dwarf_Signed result =0;
1377
1378 adres = _dwarf_leb128_uword_wrapper(dbg,
1379 &instr_ptr,final_instr_ptr,
1380 &lreg,error);
1381 if (adres != DW_DLV_OK) {
1382 FREELOCALMALLOC;
1383 return adres;
1384 }
1385 reg_no = lreg;
1386 ERROR_IF_REG_NUM_TOO_HIGH(reg_no, reg_count);
1387 adres = _dwarf_leb128_sword_wrapper(dbg,
1388 &instr_ptr,final_instr_ptr,
1389 &signed_factored_N_value,error);
1390 if (adres != DW_DLV_OK) {
1391 FREELOCALMALLOC;
1392 return adres;
1393 }
1394 if (need_augmentation) {
1395 SER(DW_DLE_DF_NO_CIE_AUGMENTATION);
1396 }
1397 /* CHECK OVERFLOW */
1398 adres = _dwarf_int64_mult(signed_factored_N_value,
1399 data_alignment_factor,
1400 &result,dbg,error);
1401 if (adres == DW_DLV_ERROR) {
1402 FREELOCALMALLOC;
1403 return adres;
1404 }
1405 cfa_reg.ru_is_offset = 1;
1406 cfa_reg.ru_value_type = DW_EXPR_OFFSET;
1407 cfa_reg.ru_register = reg_no;
1408 cfa_reg.ru_offset = result;
1409 if (make_instr) {
1410 dfi->fi_fields = "rsd";
1411 dfi->fi_u0 = lreg;
1412 dfi->fi_s1 = signed_factored_N_value;
1413 dfi->fi_data_align_factor =
1414 data_alignment_factor;
1415 }
1416 }
1417 break;
1418 case DW_CFA_def_cfa_offset_sf: {
1419 /* The operand is a signed leb128 operand
1420 representing a factored offset. Identical to
1421 DW_CFA_def_cfa_offset except the operand is
1422 signed and factored. */
1423 int adres = 0;
1424 Dwarf_Signed result = 0;
1425
1426 adres = _dwarf_leb128_sword_wrapper(dbg,
1427 &instr_ptr,final_instr_ptr,
1428 &signed_factored_N_value,error);
1429 if (adres != DW_DLV_OK) {
1430 FREELOCALMALLOC;
1431 return adres;
1432 }
1433 if (need_augmentation) {
1434 SER(DW_DLE_DF_NO_CIE_AUGMENTATION);
1435 }
1436 /* CHECK OVERFLOW */
1437 adres = _dwarf_int64_mult(signed_factored_N_value,
1438 data_alignment_factor,
1439 &result,dbg,error);
1440 if (adres == DW_DLV_ERROR) {
1441 FREELOCALMALLOC;
1442 return adres;
1443 }
1444 /* Do set ru_is_off here, as here factored_N_value
1445 counts. */
1446 cfa_reg.ru_is_offset = 1;
1447 cfa_reg.ru_value_type = DW_EXPR_OFFSET;
1448 cfa_reg.ru_offset = result;
1449 if (make_instr) {
1450 dfi->fi_fields = "sd";
1451 dfi->fi_s0 = signed_factored_N_value;
1452 dfi->fi_data_align_factor =
1453 data_alignment_factor;
1454 }
1455 }
1456 break;
1457 case DW_CFA_val_offset: {
1458 /* The first operand is an unsigned leb128 register
1459 number. The second is a factored unsigned offset.
1460 Makes the register be a val_offset(N)
1461 rule with N =
1462 factored_offset*data_alignment_factor. */
1463 Dwarf_Unsigned lreg = 0;
1464 int adres = 0;
1465 Dwarf_Signed result = 0;
1466
1467 adres = _dwarf_leb128_uword_wrapper(dbg,
1468 &instr_ptr,final_instr_ptr,
1469 &lreg,error);
1470 if (adres != DW_DLV_OK) {
1471 FREELOCALMALLOC;
1472 return adres;
1473 }
1474 reg_no = (reg_num_type) lreg;
1475 ERROR_IF_REG_NUM_TOO_HIGH(reg_no, reg_count);
1476 adres = _dwarf_leb128_uword_wrapper(dbg,
1477 &instr_ptr,final_instr_ptr,
1478 &factored_N_value,error);
1479 if (adres != DW_DLV_OK) {
1480 FREELOCALMALLOC;
1481 return adres;
1482 }
1483 if (INVALIDUNSIGNED(factored_N_value) ) {
1484 SERSTRING(DW_DLE_ARITHMETIC_OVERFLOW,
1485 "DW_DLE_ARITHMETIC_OVERFLOW "
1486 "in DW_CFA_val_offset factored value");
1487 }
1488 if (need_augmentation) {
1489 SER(DW_DLE_DF_NO_CIE_AUGMENTATION);
1490 }
1491 /* CHECK OVERFLOW */
1492 adres = _dwarf_int64_mult(
1493 (Dwarf_Signed)factored_N_value,
1494 data_alignment_factor,
1495 &result,dbg,error);
1496 if (adres == DW_DLV_ERROR) {
1497 FREELOCALMALLOC;
1498 return adres;
1499 }
1500
1501 /* Do set ru_is_off here, as here factored_N_value
1502 counts. */
1503 localregtab[reg_no].ru_is_offset = 1;
1504 localregtab[reg_no].ru_register = reg_num_of_cfa;
1505 localregtab[reg_no].ru_value_type =
1506 DW_EXPR_VAL_OFFSET;
1507 /* CHECK OVERFLOW */
1508 localregtab[reg_no].ru_offset = result;
1509 if (make_instr) {
1510 dfi->fi_fields = "rud";
1511 dfi->fi_u0 = lreg;
1512 dfi->fi_u1 = factored_N_value;
1513 dfi->fi_data_align_factor =
1514 data_alignment_factor;
1515 }
1516 break;
1517 }
1518 case DW_CFA_val_offset_sf: {
1519 /* The first operand is an unsigned leb128 register
1520 number. The second is a factored signed offset.
1521 Makes the register be a val_offset(N) rule
1522 with
1523 N = factored_offset*data_alignment_factor. */
1524 Dwarf_Unsigned lreg = 0;
1525 Dwarf_Signed result = 0;
1526 int adres = 0;
1527
1528 adres = _dwarf_leb128_uword_wrapper(dbg,
1529 &instr_ptr,final_instr_ptr,
1530 &lreg,error);
1531 if (adres != DW_DLV_OK) {
1532 FREELOCALMALLOC;
1533 return adres;
1534 }
1535 ERROR_IF_REG_NUM_TOO_HIGH(reg_no, reg_count);
1536 adres = _dwarf_leb128_sword_wrapper(dbg,
1537 &instr_ptr,final_instr_ptr,
1538 &signed_factored_N_value,error);
1539 if (adres != DW_DLV_OK) {
1540 FREELOCALMALLOC;
1541 return adres;
1542 }
1543 if (need_augmentation) {
1544 SER(DW_DLE_DF_NO_CIE_AUGMENTATION);
1545 }
1546 adres = _dwarf_int64_mult(signed_factored_N_value,
1547 data_alignment_factor,&result,
1548 dbg,error);
1549 if (adres == DW_DLV_ERROR) {
1550 FREELOCALMALLOC;
1551 return adres;
1552 }
1553 /* Do set ru_is_off here, as here factored_N_value
1554 counts. */
1555 localregtab[reg_no].ru_is_offset = 1;
1556 localregtab[reg_no].ru_value_type =
1557 DW_EXPR_VAL_OFFSET;
1558 /* CHECK OVERFLOW */
1559 localregtab[reg_no].ru_offset = result;
1560 if (make_instr) {
1561 dfi->fi_fields = "rsd";
1562 dfi->fi_u0 = lreg;
1563 dfi->fi_s1 = signed_factored_N_value;
1564 dfi->fi_data_align_factor =
1565 data_alignment_factor;
1566 }
1567 }
1568 break;
1569 case DW_CFA_val_expression: {
1570 /* The first operand is an unsigned leb128 register
1571 number. The second is a DW_FORM_block
1572 representing a
1573 DWARF expression. The rule for the register
1574 number becomes a val_expression(E) rule. */
1575 Dwarf_Unsigned lreg = 0;
1576 Dwarf_Unsigned block_len = 0;
1577 int adres = 0;
1578
1579 adres = _dwarf_leb128_uword_wrapper(dbg,
1580 &instr_ptr,final_instr_ptr,
1581 &lreg,error);
1582 if (adres != DW_DLV_OK) {
1583 FREELOCALMALLOC;
1584 return adres;
1585 }
1586 reg_no = (reg_num_type) lreg;
1587 ERROR_IF_REG_NUM_TOO_HIGH(reg_no, reg_count);
1588 adres = _dwarf_leb128_uword_wrapper(dbg,
1589 &instr_ptr,final_instr_ptr,
1590 &block_len,error);
1591 if (adres != DW_DLV_OK) {
1592 FREELOCALMALLOC;
1593 return adres;
1594 }
1595 localregtab[lreg].ru_is_offset = 0; /* arbitrary */
1596 localregtab[lreg].ru_value_type =
1597 DW_EXPR_VAL_EXPRESSION;
1598 localregtab[lreg].ru_offset = 0;
1599 localregtab[lreg].ru_block.bl_data = instr_ptr;
1600 localregtab[lreg].ru_block.bl_len = block_len;
1601 if (make_instr) {
1602 dfi->fi_fields = "rb";
1603 dfi->fi_u0 = lreg;
1604 dfi->fi_expr.bl_len = block_len;
1605 dfi->fi_expr.bl_data = instr_ptr;
1606 }
1607 instr_ptr += block_len;
1608 if (instr_area_length < block_len ||
1609 instr_ptr < base_instr_ptr) {
1610 SERSTRING(DW_DLE_DF_FRAME_DECODING_ERROR,
1611 "DW_DLE_DF_FRAME_DECODING_ERROR: "
1612 "DW_CFA_val_expression "
1613 "block len overflows instructions "
1614 "available range.");
1615 }
1616 }
1617 break;
1618 /* END DWARF3 new ops. */
1619
1620 #ifdef DW_CFA_GNU_window_save
1621 case DW_CFA_GNU_window_save: {
1622 /* No information: this just tells
1623 unwinder to restore
1624 the window registers from the previous frame's
1625 window save area */
1626 if (make_instr) {
1627 dfi->fi_fields = "";
1628 }
1629 }
1630 break;
1631 #endif
1632 #ifdef DW_CFA_GNU_args_size
1633 /* Single uleb128 is the current arg area
1634 size in bytes. No
1635 register exists yet to save this in.
1636 the value of must be added to
1637 an x86 register to get the correct
1638 stack pointer.
1639 https://lists.nongnu.org/archive/html/
1640 libunwind-devel/2016-12/msg00004.html
1641 https://refspecs.linuxfoundation.org/
1642 LSB_3.0.0/LSB-PDA/LSB-PDA.junk/dwarfext.html
1643 */
1644 case DW_CFA_GNU_args_size: {
1645 Dwarf_Unsigned asize = 0;
1646 int adres = 0;
1647
1648 adres = _dwarf_leb128_uword_wrapper(dbg,
1649 &instr_ptr,final_instr_ptr,
1650 &asize,error);
1651 if (adres != DW_DLV_OK) {
1652 FREELOCALMALLOC;
1653 return adres;
1654 }
1655 /* Currently not put into ru_* reg rules, not
1656 sure what to do with it. */
1657 /* This is the total size of arguments
1658 pushed on the stack. */
1659 if (make_instr) {
1660 dfi->fi_fields = "u";
1661 dfi->fi_u0 = asize;
1662 }
1663 }
1664 break;
1665 #endif
1666 case DW_CFA_LLVM_def_aspace_cfa: {
1667 Dwarf_Unsigned lreg = 0;
1668 Dwarf_Unsigned offset = 0;
1669 Dwarf_Unsigned addrspace = 0;
1670 int adres = 0;
1671
1672 adres = _dwarf_leb128_uword_wrapper(dbg,
1673 &instr_ptr,final_instr_ptr,
1674 &lreg,error);
1675 if (adres != DW_DLV_OK) {
1676 FREELOCALMALLOC;
1677 return adres;
1678 }
1679 ERROR_IF_REG_NUM_TOO_HIGH(lreg, reg_count);
1680 adres = _dwarf_leb128_uword_wrapper(dbg,
1681 &instr_ptr,final_instr_ptr,
1682 &offset,error);
1683 if (adres != DW_DLV_OK) {
1684 FREELOCALMALLOC;
1685 return adres;
1686 }
1687 adres = _dwarf_leb128_uword_wrapper(dbg,
1688 &instr_ptr,final_instr_ptr,
1689 &addrspace,error);
1690 if (adres != DW_DLV_OK) {
1691 FREELOCALMALLOC;
1692 return adres;
1693 }
1694 if (make_instr) {
1695 dfi->fi_fields = "rua";
1696 dfi->fi_u0 = lreg;
1697 dfi->fi_u1 = offset;
1698 dfi->fi_u2 = addrspace;
1699 }
1700 }
1701 break;
1702 case DW_CFA_LLVM_def_aspace_cfa_sf: {
1703 Dwarf_Unsigned lreg = 0;
1704 Dwarf_Signed offset = 0;
1705 Dwarf_Signed result = 0;
1706 Dwarf_Unsigned addrspace = 0;
1707 int adres = 0;
1708
1709 adres = _dwarf_leb128_uword_wrapper(dbg,
1710 &instr_ptr,final_instr_ptr,
1711 &lreg,error);
1712 if (adres != DW_DLV_OK) {
1713 FREELOCALMALLOC;
1714 return adres;
1715 }
1716 ERROR_IF_REG_NUM_TOO_HIGH(lreg, reg_count);
1717 adres = _dwarf_leb128_sword_wrapper(dbg,
1718 &instr_ptr,final_instr_ptr,
1719 &offset,error);
1720 if (adres != DW_DLV_OK) {
1721 FREELOCALMALLOC;
1722 return adres;
1723 }
1724 adres = _dwarf_leb128_uword_wrapper(dbg,
1725 &instr_ptr,final_instr_ptr,
1726 &addrspace,error);
1727 if (adres != DW_DLV_OK) {
1728 FREELOCALMALLOC;
1729 return adres;
1730 }
1731 /* CHECK OVERFLOW */
1732 adres = _dwarf_int64_mult(
1733 (Dwarf_Signed)offset,
1734 data_alignment_factor,
1735 &result,dbg, error);
1736 if (adres == DW_DLV_ERROR) {
1737 FREELOCALMALLOC;
1738 return DW_DLV_ERROR;
1739 }
1740 localregtab[reg_no].ru_is_offset = 1;
1741 localregtab[reg_no].ru_value_type = DW_EXPR_OFFSET;
1742 localregtab[reg_no].ru_register = reg_num_of_cfa;
1743 localregtab[reg_no].ru_offset = result;
1744 if (make_instr) {
1745 dfi->fi_fields = "rsda";
1746 dfi->fi_u0 = lreg;
1747 dfi->fi_s1 = offset;
1748 dfi->fi_u2 = addrspace;
1749 dfi->fi_data_align_factor =
1750 data_alignment_factor;
1751 }
1752 }
1753 break;
1754 default: {
1755 /* ERROR, we have an opcode we know nothing
1756 about. Memory leak here, but an error
1757 like this is not supposed to
1758 happen so we ignore the leak.
1759 These used to be ignored,
1760 now we notice and report. */
1761 dwarfstring ms;
1762
1763 dwarfstring_constructor(&ms);
1764 dwarfstring_append_printf_u(&ms,
1765 "DW_DLE_DF_FRAME_DECODING_ERROR: "
1766 "instr opcode 0x%x unknown",opcode);
1767 _dwarf_error_string(dbg,error,
1768 DW_DLE_DF_FRAME_DECODING_ERROR,
1769 dwarfstring_string(&ms));
1770 dwarfstring_destructor(&ms);
1771 FREELOCALMALLOC;
1772 return DW_DLV_ERROR;
1773 }
1774 }
1775 if (make_instr) {
1776 /* add dfi to end of singly-linked list */
1777 instr_count++;
1778 (*ilistlastptr) = dfi;
1779 ilistlastptr = &dfi->fi_next;
1780 /* dfi itself is stale, the pointer is on the list */
1781 dfi = 0;
1782 }
1783 } /* end for-loop on ops */
1784
1785 /* If frame instruction decoding was right we would
1786 stop exactly at
1787 final_instr_ptr. */
1788 if (instr_ptr > final_instr_ptr) {
1789 SER(DW_DLE_DF_FRAME_DECODING_ERROR);
1790 }
1791 /* If search_over is set the last instr was an advance_loc
1792 so we are not done with rows. */
1793 if ((instr_ptr == final_instr_ptr) && !search_over) {
1794 if (has_more_rows) {
1795 *has_more_rows = FALSE;
1796 }
1797 if (subsequent_pc) {
1798 *subsequent_pc = 0;
1799 }
1800 } else {
1801 if (has_more_rows) {
1802 *has_more_rows = TRUE;
1803 }
1804 if (subsequent_pc) {
1805 *subsequent_pc = possible_subsequent_pc;
1806 }
1807 }
1808
1809 /* Fill in the actual output table, the space the
1810 caller passed in. */
1811 if (table) {
1812
1813 struct Dwarf_Reg_Rule_s *t2reg = table->fr_reg;
1814 struct Dwarf_Reg_Rule_s *t3reg = localregtab;
1815 unsigned minregcount = (unsigned)MIN(table->fr_reg_count,
1816 reg_count);
1817 unsigned curreg = 0;
1818
1819 table->fr_loc = current_loc;
1820 for (; curreg < minregcount ; curreg++, t3reg++, t2reg++) {
1821 *t2reg = *t3reg;
1822 }
1823
1824 /* CONSTCOND */
1825 /* Do not update the main table with the cfa_reg.
1826 Just leave cfa_reg as cfa_reg. */
1827 table->fr_cfa_rule = cfa_reg;
1828 }
1829 /* Dealloc anything remaining on stack. */
1830 for (; top_stack != NULL;) {
1831 stack_table = top_stack;
1832 top_stack = top_stack->fr_next;
1833 dwarf_dealloc(dbg, stack_table, DW_DLA_FRAME);
1834 }
1835 if (make_instr) {
1836 Dwarf_Frame_Instr_Head head = 0;
1837 Dwarf_Frame_Instr *instrptrs = 0;
1838 Dwarf_Frame_Instr *curinstrptr = 0;
1839 Dwarf_Frame_Instr cur = 0;
1840 Dwarf_Frame_Instr next = 0;
1841 Dwarf_Unsigned ic = 0;
1842
1843 head= (Dwarf_Frame_Instr_Head)
1844 _dwarf_get_alloc(dbg, DW_DLA_FRAME_INSTR_HEAD,1);
1845 if (!head) {
1846 SER(DW_DLE_DF_ALLOC_FAIL);
1847 }
1848 instrptrs= (Dwarf_Frame_Instr *)
1849 _dwarf_get_alloc(dbg, DW_DLA_LIST,instr_count);
1850 if (!instrptrs) {
1851 dwarf_dealloc(dbg,head,DW_DLA_FRAME_INSTR_HEAD);
1852 SER(DW_DLE_DF_ALLOC_FAIL);
1853 }
1854 head->fh_array = instrptrs;
1855 head->fh_array_count = instr_count;
1856 head->fh_dbg = dbg;
1857 head->fh_cie = cie;
1858 cur = ilisthead;
1859 curinstrptr = instrptrs;
1860 for ( ; cur ; ic++,cur = next,++curinstrptr) {
1861 *curinstrptr = cur;
1862 next = cur->fi_next;
1863 cur->fi_next = 0;
1864 }
1865 ilisthead = 0;
1866 if (ic != instr_count) {
1867 dwarfstring m;
1868
1869 FREELOCALMALLOC;
1870 dwarf_dealloc(dbg,head,DW_DLA_FRAME_INSTR_HEAD);
1871 dwarfstring_constructor(&m);
1872 dwarfstring_append_printf_u(&m,
1873 "DW_DLE_DF_FRAME_DECODING_ERROR: "
1874 "Instruction array build, instr count %u",
1875 instr_count);
1876 dwarfstring_append_printf_u(&m,
1877 " index i %u. Impossible error.",ic);
1878 _dwarf_error_string(dbg,error,
1879 DW_DLE_DF_FRAME_DECODING_ERROR,
1880 dwarfstring_string(&m));
1881 dwarfstring_destructor(&m);
1882 return DW_DLV_ERROR;
1883 }
1884 *ret_frame_instr_head = head;
1885 *returned_frame_instr_count = instr_count;
1886 } else {
1887 if (ret_frame_instr_head) {
1888 *ret_frame_instr_head = 0;
1889 }
1890 if (returned_frame_instr_count) {
1891 *returned_frame_instr_count = 0;
1892 }
1893 }
1894 FREELOCALMALLOC;
1895 return DW_DLV_OK;
1896 #undef ERROR_IF_REG_NUM_TOO_HIGH
1897 #undef FREELOCALMALLOC
1898 #undef SER
1899 }
1900
1901 /* Depending on version, either read the return address register
1902 as a ubyte or as an leb number.
1903 The form of this value changed for DWARF3.
1904 */
1905 int
1906 _dwarf_get_return_address_reg(Dwarf_Small *frame_ptr,
1907 int version,
1908 Dwarf_Debug dbg,
1909 Dwarf_Byte_Ptr section_end,
1910 unsigned long *size,
1911 Dwarf_Unsigned *return_address_register,
1912 Dwarf_Error *error)
1913 {
1914 Dwarf_Unsigned uvalue = 0;
1915 Dwarf_Unsigned leb128_length = 0;
1916
1917 if (version == 1) {
1918 if (frame_ptr >= section_end) {
1919 _dwarf_error(NULL, error, DW_DLE_DF_FRAME_DECODING_ERROR);
1920 return DW_DLV_ERROR;
1921 }
1922 *size = 1;
1923 uvalue = *(unsigned char *) frame_ptr;
1924 *return_address_register = uvalue;
1925 return DW_DLV_OK;
1926 }
1927 DECODE_LEB128_UWORD_LEN_CK(frame_ptr,uvalue,leb128_length,
1928 dbg,error,section_end);
1929 *size = (unsigned long)leb128_length;
1930 *return_address_register = uvalue;
1931 return DW_DLV_OK;
1932 }
1933
1934 /* Trivial consumer function.
1935 */
1936 int
1937 dwarf_get_cie_of_fde(Dwarf_Fde fde,
1938 Dwarf_Cie * cie_returned, Dwarf_Error * error)
1939 {
1940 if (!fde) {
1941 _dwarf_error(NULL, error, DW_DLE_FDE_NULL);
1942 return DW_DLV_ERROR;
1943 }
1944
1945 *cie_returned = fde->fd_cie;
1946 return DW_DLV_OK;
1947
1948 }
1949
1950 int
1951 dwarf_get_cie_index(
1952 Dwarf_Cie cie,
1953 Dwarf_Signed* indx,
1954 Dwarf_Error* error )
1955 {
1956 if (cie == NULL)
1957 {
1958 _dwarf_error(NULL, error, DW_DLE_CIE_NULL);
1959 return DW_DLV_ERROR;
1960 }
1961
1962 *indx = cie->ci_index;
1963 return DW_DLV_OK;
1964 }
1965
1966 /* For g++ .eh_frame fde and cie.
1967 the cie id is different as the
1968 definition of the cie_id in an fde
1969 is the distance back from the address of the
1970 value to the cie.
1971 Or 0 if this is a TRUE cie.
1972 Non standard dwarf, designed this way to be
1973 convenient at run time for an allocated
1974 (mapped into memory as part of the running image) section.
1975 */
1976 int
1977 dwarf_get_fde_list_eh(Dwarf_Debug dbg,
1978 Dwarf_Cie ** cie_data,
1979 Dwarf_Signed * cie_element_count,
1980 Dwarf_Fde ** fde_data,
1981 Dwarf_Signed * fde_element_count,
1982 Dwarf_Error * error)
1983 {
1984 int res = 0;
1985
1986 CHECK_DBG(dbg,error,"dwarf_get_fde_list_eh()");
1987 res = _dwarf_load_section(dbg,
1988 &dbg->de_debug_frame_eh_gnu,error);
1989 if (res != DW_DLV_OK) {
1990 return res;
1991 }
1992 res = _dwarf_get_fde_list_internal(dbg,
1993 cie_data,
1994 cie_element_count,
1995 fde_data,
1996 fde_element_count,
1997 dbg->de_debug_frame_eh_gnu.dss_data,
1998 dbg->de_debug_frame_eh_gnu.dss_index,
1999 dbg->de_debug_frame_eh_gnu.dss_size,
2000 /* cie_id_value */ 0,
2001 /* use_gnu_cie_calc= */ 1,
2002 error);
2003 return res;
2004 }
2005
2006 /* For standard dwarf .debug_frame
2007 cie_id is -1 in a cie, and
2008 is the section offset in the .debug_frame section
2009 of the cie otherwise. Standard dwarf
2010 */
2011 int
2012 dwarf_get_fde_list(Dwarf_Debug dbg,
2013 Dwarf_Cie ** cie_data,
2014 Dwarf_Signed * cie_element_count,
2015 Dwarf_Fde ** fde_data,
2016 Dwarf_Signed * fde_element_count,
2017 Dwarf_Error * error)
2018 {
2019 int res = 0;
2020
2021 CHECK_DBG(dbg,error,"dwarf_get_fde_list()");
2022 res = _dwarf_load_section(dbg, &dbg->de_debug_frame,error);
2023 if (res != DW_DLV_OK) {
2024 return res;
2025 }
2026 res = _dwarf_get_fde_list_internal(dbg, cie_data,
2027 cie_element_count,
2028 fde_data,
2029 fde_element_count,
2030 dbg->de_debug_frame.dss_data,
2031 dbg->de_debug_frame.dss_index,
2032 dbg->de_debug_frame.dss_size,
2033 (Dwarf_Unsigned)DW_CIE_ID,
2034 /* use_gnu_cie_calc= */ 0,
2035 error);
2036
2037 return res;
2038 }
2039
2040 /* Only works on dwarf sections, not eh_frame
2041 because based on DW_AT_MIPS_fde.
2042 Given a Dwarf_Die, see if it has a
2043 DW_AT_MIPS_fde attribute and if so use that
2044 to get an fde offset.
2045 Then create a Dwarf_Fde to return thru the ret_fde pointer.
2046 Also creates a cie (pointed at from the Dwarf_Fde). */
2047 int
2048 dwarf_get_fde_for_die(Dwarf_Debug dbg,
2049 Dwarf_Die die,
2050 Dwarf_Fde * ret_fde, Dwarf_Error * error)
2051 {
2052 Dwarf_Attribute attr;
2053 Dwarf_Unsigned fde_offset = 0;
2054 Dwarf_Signed signdval = 0;
2055 Dwarf_Fde new_fde = 0;
2056 unsigned char *fde_ptr = 0;
2057 unsigned char *fde_start_ptr = 0;
2058 unsigned char *fde_end_ptr = 0;
2059 unsigned char *cie_ptr = 0;
2060 Dwarf_Unsigned cie_id = 0;
2061 Dwarf_Half address_size = 0;
2062
2063 /* Fields for the current Cie being read. */
2064 int res = 0;
2065 int resattr = 0;
2066 int sdatares = 0;
2067
2068 struct cie_fde_prefix_s prefix;
2069 struct cie_fde_prefix_s prefix_c;
2070
2071 CHECK_DBG(dbg,error,"dwarf_get_fde_for_die()");
2072 if (!die ) {
2073 _dwarf_error_string(NULL, error, DW_DLE_DIE_NULL,
2074 "DW_DLE_DIE_NUL: in dwarf_get_fde_for_die(): "
2075 "Called with Dwarf_Die argument null");
2076 return DW_DLV_ERROR;
2077 }
2078 resattr = dwarf_attr(die, DW_AT_MIPS_fde, &attr, error);
2079 if (resattr != DW_DLV_OK) {
2080 return resattr;
2081 }
2082 /* why is this formsdata? FIX */
2083 sdatares = dwarf_formsdata(attr, &signdval, error);
2084 if (sdatares != DW_DLV_OK) {
2085 dwarf_dealloc_attribute(attr);
2086 return sdatares;
2087 }
2088 res = dwarf_get_die_address_size(die,&address_size,error);
2089 if (res != DW_DLV_OK) {
2090 dwarf_dealloc_attribute(attr);
2091 return res;
2092 }
2093 dwarf_dealloc_attribute(attr);
2094 res = _dwarf_load_section(dbg, &dbg->de_debug_frame,error);
2095 if (res != DW_DLV_OK) {
2096 return res;
2097 }
2098 fde_offset = signdval;
2099 fde_start_ptr = dbg->de_debug_frame.dss_data;
2100 fde_ptr = fde_start_ptr + fde_offset;
2101 fde_end_ptr = fde_start_ptr + dbg->de_debug_frame.dss_size;
2102 res = _dwarf_validate_register_numbers(dbg,error);
2103 if (res == DW_DLV_ERROR) {
2104 return res;
2105 }
2106
2107 /* First read in the 'common prefix' to figure out
2108 what we are to do with this entry. */
2109 memset(&prefix_c, 0, sizeof(prefix_c));
2110 memset(&prefix, 0, sizeof(prefix));
2111 res = _dwarf_read_cie_fde_prefix(dbg, fde_ptr,
2112 dbg->de_debug_frame.dss_data,
2113 dbg->de_debug_frame.dss_index,
2114 dbg->de_debug_frame.dss_size,
2115 &prefix,
2116 error);
2117 if (res == DW_DLV_ERROR) {
2118 return res;
2119 }
2120 if (res == DW_DLV_NO_ENTRY) {
2121 return res;
2122 }
2123 fde_ptr = prefix.cf_addr_after_prefix;
2124 cie_id = prefix.cf_cie_id;
2125 if (cie_id >= dbg->de_debug_frame.dss_size ) {
2126 _dwarf_error_string(dbg, error, DW_DLE_NO_CIE_FOR_FDE,
2127 "DW_DLE_NO_CIE_FOR_FDE: "
2128 "dwarf_get_fde_for_die fails as the CIE id "
2129 "offset is impossibly large");
2130 return DW_DLV_ERROR;
2131 }
2132 /* Pass NULL, not section pointer, for 3rd argument.
2133 de_debug_frame.dss_data has no eh_frame relevance. */
2134 res = _dwarf_create_fde_from_after_start(dbg, &prefix,
2135 fde_start_ptr,
2136 dbg->de_debug_frame.dss_size,
2137 fde_ptr,
2138 fde_end_ptr,
2139 /* use_gnu_cie_calc= */ 0,
2140 /* Dwarf_Cie = */ 0,
2141 address_size,
2142 &new_fde, error);
2143 if (res == DW_DLV_ERROR) {
2144 return res;
2145 }
2146 if (res == DW_DLV_NO_ENTRY) {
2147 return res;
2148 }
2149 /* DW_DLV_OK */
2150
2151 /* This is the only situation this is set.
2152 and is really dangerous. as fde and cie
2153 are set for dealloc by dwarf_finish(). */
2154 new_fde->fd_fde_owns_cie = TRUE;
2155 /* Now read the cie corresponding to the fde,
2156 _dwarf_read_cie_fde_prefix checks
2157 cie_ptr for being within the section. */
2158 if (cie_id >= dbg->de_debug_frame.dss_size ) {
2159 _dwarf_error_string(dbg, error, DW_DLE_NO_CIE_FOR_FDE,
2160 "DW_DLE_NO_CIE_FOR_FDE: "
2161 "dwarf_get_fde_for_die fails as the CIE id "
2162 "offset is impossibly large");
2163 return DW_DLV_ERROR;
2164 }
2165 cie_ptr = new_fde->fd_section_ptr + cie_id;
2166 if ((Dwarf_Unsigned)(uintptr_t)cie_ptr <
2167 (Dwarf_Unsigned)(uintptr_t)new_fde->fd_section_ptr ||
2168 (Dwarf_Unsigned)(uintptr_t)cie_ptr < cie_id) {
2169 dwarf_dealloc(dbg,new_fde,DW_DLA_FDE);
2170 new_fde = 0;
2171 _dwarf_error_string(dbg, error, DW_DLE_NO_CIE_FOR_FDE,
2172 "DW_DLE_NO_CIE_FOR_FDE: "
2173 "dwarf_get_fde_for_die fails as the CIE id "
2174 "offset is impossibly large");
2175 return DW_DLV_ERROR;
2176 }
2177 res = _dwarf_read_cie_fde_prefix(dbg, cie_ptr,
2178 dbg->de_debug_frame.dss_data,
2179 dbg->de_debug_frame.dss_index,
2180 dbg->de_debug_frame.dss_size,
2181 &prefix_c, error);
2182 if (res == DW_DLV_ERROR) {
2183 dwarf_dealloc(dbg,new_fde,DW_DLA_FDE);
2184 new_fde = 0;
2185 return res;
2186 }
2187 if (res == DW_DLV_NO_ENTRY) {
2188 dwarf_dealloc(dbg,new_fde,DW_DLA_FDE);
2189 new_fde = 0;
2190 return res;
2191 }
2192
2193 cie_ptr = prefix_c.cf_addr_after_prefix;
2194 cie_id = prefix_c.cf_cie_id;
2195
2196 if (cie_id == (Dwarf_Unsigned)DW_CIE_ID) {
2197 int res2 = 0;
2198 Dwarf_Cie new_cie = 0;
2199
2200 /* Pass NULL, not section pointer, for 3rd argument.
2201 de_debug_frame.dss_data has no eh_frame relevance. */
2202 res2 = _dwarf_create_cie_from_after_start(dbg,
2203 &prefix_c,
2204 fde_start_ptr,
2205 cie_ptr,
2206 fde_end_ptr,
2207 /* cie_count= */ 0,
2208 /* use_gnu_cie_calc= */
2209 0, &new_cie, error);
2210 if (res2 != DW_DLV_OK) {
2211 dwarf_dealloc(dbg, new_fde, DW_DLA_FDE);
2212 return res;
2213 }
2214 new_fde->fd_cie = new_cie;
2215 } else {
2216 dwarf_dealloc(dbg,new_fde,DW_DLA_FDE);
2217 new_fde = 0;
2218 _dwarf_error_string(dbg, error, DW_DLE_NO_CIE_FOR_FDE,
2219 "DW_DLE_NO_CIE_FOR_FDE: "
2220 "The CIE id is not a true cid id. Corrupt DWARF.");
2221 return DW_DLV_ERROR;
2222 }
2223 *ret_fde = new_fde;
2224 return DW_DLV_OK;
2225 }
2226
2227 int
2228 dwarf_get_fde_range(Dwarf_Fde fde,
2229 Dwarf_Addr * low_pc,
2230 Dwarf_Unsigned * func_length,
2231 Dwarf_Byte_Ptr * fde_bytes,
2232 Dwarf_Unsigned * fde_byte_length,
2233 Dwarf_Off * cie_offset,
2234 Dwarf_Signed * cie_index,
2235 Dwarf_Off * fde_offset, Dwarf_Error * error)
2236 {
2237 Dwarf_Debug dbg;
2238
2239 if (fde == NULL) {
2240 _dwarf_error(NULL, error, DW_DLE_FDE_NULL);
2241 return DW_DLV_ERROR;
2242 }
2243
2244 dbg = fde->fd_dbg;
2245 if (IS_INVALID_DBG(dbg)) {
2246 _dwarf_error_string(NULL, error, DW_DLE_FDE_DBG_NULL,
2247 "DW_DLE_FDE_DBG_NULL: Either null or it contains"
2248 "a stale Dwarf_Debug pointer");
2249 return DW_DLV_ERROR;
2250 }
2251 /* We have always already done the section load here,
2252 so no need to load the section. We did the section
2253 load in order to create the
2254 Dwarf_Fde pointer passed in here. */
2255 if (low_pc != NULL)
2256 *low_pc = fde->fd_initial_location;
2257 if (func_length != NULL)
2258 *func_length = fde->fd_address_range;
2259 if (fde_bytes != NULL)
2260 *fde_bytes = fde->fd_fde_start;
2261 if (fde_byte_length != NULL)
2262 *fde_byte_length = fde->fd_length;
2263 if (cie_offset != NULL)
2264 *cie_offset = fde->fd_cie_offset;
2265 if (cie_index != NULL)
2266 *cie_index = fde->fd_cie_index;
2267 if (fde_offset != NULL)
2268 *fde_offset = fde->fd_fde_start - fde->fd_section_ptr;
2269
2270 return DW_DLV_OK;
2271 }
2272
2273 /* IRIX specific function. The exception tables
2274 have C++ destructor information and are
2275 at present undocumented. */
2276 int
2277 dwarf_get_fde_exception_info(Dwarf_Fde fde,
2278 Dwarf_Signed *
2279 offset_into_exception_tables,
2280 Dwarf_Error * error)
2281 {
2282 Dwarf_Debug dbg;
2283
2284 dbg = fde->fd_dbg;
2285 if (IS_INVALID_DBG(dbg)) {
2286 _dwarf_error_string(NULL, error, DW_DLE_FDE_DBG_NULL,
2287 "DW_DLE_FDE_DBG_NULL: Either null or it contains"
2288 "a stale Dwarf_Debug pointer");
2289 return DW_DLV_ERROR;
2290 }
2291
2292 *offset_into_exception_tables =
2293 fde->fd_offset_into_exception_tables;
2294 return DW_DLV_OK;
2295 }
2296
2297 /* A consumer code function.
2298 Given a CIE pointer, return the normal CIE data thru
2299 pointers.
2300 Special augmentation data is not returned here.
2301 */
2302 int
2303 dwarf_get_cie_info_b(Dwarf_Cie cie,
2304 Dwarf_Unsigned *bytes_in_cie,
2305 Dwarf_Small *ptr_to_version,
2306 char **augmenter,
2307 Dwarf_Unsigned *code_alignment_factor,
2308 Dwarf_Signed *data_alignment_factor,
2309 Dwarf_Half *return_address_register,
2310 Dwarf_Byte_Ptr *initial_instructions,
2311 Dwarf_Unsigned *initial_instructions_length,
2312 Dwarf_Half *offset_size,
2313 Dwarf_Error *error)
2314 {
2315 Dwarf_Debug dbg = 0;
2316
2317 if (!cie) {
2318 _dwarf_error(NULL, error, DW_DLE_CIE_NULL);
2319 return DW_DLV_ERROR;
2320 }
2321 dbg = cie->ci_dbg;
2322 if (IS_INVALID_DBG(dbg)) {
2323 _dwarf_error_string(NULL, error, DW_DLE_CIE_DBG_NULL,
2324 "DW_DLE_CIE_DBG_NULL: Either null or it contains"
2325 "a stale Dwarf_Debug pointer");
2326 return DW_DLV_ERROR;
2327 }
2328 if (ptr_to_version != NULL)
2329 *ptr_to_version =
2330 (Dwarf_Small)cie->ci_cie_version_number;
2331 if (augmenter != NULL)
2332 *augmenter = cie->ci_augmentation;
2333 if (code_alignment_factor != NULL)
2334 *code_alignment_factor = cie->ci_code_alignment_factor;
2335 if (data_alignment_factor != NULL)
2336 *data_alignment_factor = cie->ci_data_alignment_factor;
2337 if (return_address_register != NULL)
2338 *return_address_register =
2339 (Dwarf_Half)cie->ci_return_address_register;
2340 if (initial_instructions != NULL)
2341 *initial_instructions = cie->ci_cie_instr_start;
2342 if (initial_instructions_length != NULL) {
2343 *initial_instructions_length = cie->ci_length +
2344 cie->ci_length_size +
2345 cie->ci_extension_size -
2346 (cie->ci_cie_instr_start - cie->ci_cie_start);
2347 }
2348 if (offset_size) {
2349 *offset_size = cie->ci_length_size;
2350 }
2351 *bytes_in_cie = (cie->ci_length);
2352 return DW_DLV_OK;
2353 }
2354
2355 /* Return the register rules for all registers at a given pc.
2356 */
2357 static int
2358 _dwarf_get_fde_info_for_a_pc_row(Dwarf_Fde fde,
2359 Dwarf_Addr pc_requested,
2360 Dwarf_Frame table,
2361 Dwarf_Unsigned cfa_reg_col_num,
2362 Dwarf_Bool * has_more_rows,
2363 Dwarf_Addr * subsequent_pc,
2364 Dwarf_Error * error)
2365 {
2366 Dwarf_Debug dbg = 0;
2367 Dwarf_Cie cie = 0;
2368 int res = 0;
2369
2370 if (fde == NULL) {
2371 _dwarf_error(NULL, error, DW_DLE_FDE_NULL);
2372 return DW_DLV_ERROR;
2373 }
2374
2375 dbg = fde->fd_dbg;
2376 if (IS_INVALID_DBG(dbg)) {
2377 _dwarf_error(NULL, error, DW_DLE_FDE_DBG_NULL);
2378 return DW_DLV_ERROR;
2379 }
2380
2381 if (pc_requested < fde->fd_initial_location ||
2382 pc_requested >=
2383 fde->fd_initial_location + fde->fd_address_range) {
2384 _dwarf_error(dbg, error, DW_DLE_PC_NOT_IN_FDE_RANGE);
2385 return DW_DLV_ERROR;
2386 }
2387
2388 cie = fde->fd_cie;
2389 if (cie->ci_initial_table == NULL) {
2390 Dwarf_Small *instrstart = cie->ci_cie_instr_start;
2391 Dwarf_Small *instrend = instrstart +cie->ci_length +
2392 cie->ci_length_size +
2393 cie->ci_extension_size -
2394 (cie->ci_cie_instr_start -
2395 cie->ci_cie_start);
2396 if (instrend > cie->ci_cie_end) {
2397 _dwarf_error(dbg, error,DW_DLE_CIE_INSTR_PTR_ERROR);
2398 return DW_DLV_ERROR;
2399 }
2400 cie->ci_initial_table = (Dwarf_Frame)_dwarf_get_alloc(dbg,
2401 DW_DLA_FRAME, 1);
2402
2403 if (cie->ci_initial_table == NULL) {
2404 _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
2405 return DW_DLV_ERROR;
2406 }
2407 _dwarf_init_reg_rules_ru(cie->ci_initial_table->fr_reg,
2408 0, cie->ci_initial_table->fr_reg_count,
2409 dbg->de_frame_rule_initial_value);
2410 _dwarf_init_reg_rules_ru(&cie->ci_initial_table->fr_cfa_rule,
2411 0,1,dbg->de_frame_rule_initial_value);
2412 res = _dwarf_exec_frame_instr( /* make_instr= */ FALSE,
2413 /* search_pc */ FALSE,
2414 /* search_pc_val */ 0,
2415 /* location */ 0,
2416 instrstart,
2417 instrend,
2418 cie->ci_initial_table,
2419 cie, dbg,
2420 cfa_reg_col_num,
2421 has_more_rows,
2422 subsequent_pc,
2423 NULL,NULL,
2424 error);
2425 if (res != DW_DLV_OK) {
2426 return res;
2427 }
2428 }
2429
2430 {
2431 Dwarf_Small *instr_end = fde->fd_length +
2432 fde->fd_length_size +
2433 fde->fd_extension_size + fde->fd_fde_start;
2434 if (instr_end > fde->fd_fde_end) {
2435 _dwarf_error(dbg, error,DW_DLE_FDE_INSTR_PTR_ERROR);
2436 return DW_DLV_ERROR;
2437 }
2438 res = _dwarf_exec_frame_instr( /* make_instr= */ FALSE,
2439 /* search_pc */ TRUE,
2440 /* search_pc_val */ pc_requested,
2441 fde->fd_initial_location,
2442 fde->fd_fde_instr_start,
2443 instr_end,
2444 table,
2445 cie,dbg,
2446 cfa_reg_col_num,
2447 has_more_rows,
2448 subsequent_pc,
2449 NULL,NULL,
2450 error);
2451 }
2452 if (res != DW_DLV_OK) {
2453 return res;
2454 }
2455
2456 return DW_DLV_OK;
2457 }
2458
2459 int
2460 dwarf_get_fde_info_for_all_regs3_b(Dwarf_Fde fde,
2461 Dwarf_Addr pc_requested,
2462 Dwarf_Regtable3 * reg_table,
2463 Dwarf_Addr * row_pc,
2464 Dwarf_Bool * has_more_rows,
2465 Dwarf_Addr * subsequent_pc,
2466 Dwarf_Error * error)
2467 {
2468
2469 struct Dwarf_Frame_s fde_table;
2470 Dwarf_Unsigned i = 0;
2471 int res = 0;
2472 struct Dwarf_Reg_Rule_s *rule = NULL;
2473
2474 /* Internal-only struct. */
2475 Dwarf_Regtable_Entry3_i *rule_i = NULL;
2476
2477 Dwarf_Debug dbg = 0;
2478 Dwarf_Unsigned output_table_real_data_size = 0;
2479 Dwarf_Regtable3_i reg_table_i;
2480
2481 memset(&reg_table_i,0,sizeof(reg_table_i));
2482 memset(&fde_table,0,sizeof(fde_table));
2483 FDE_NULL_CHECKS_AND_SET_DBG(fde, dbg);
2484 output_table_real_data_size = reg_table->rt3_reg_table_size;
2485 reg_table_i.rt3_reg_table_size = output_table_real_data_size;
2486 output_table_real_data_size =
2487 MIN(output_table_real_data_size,
2488 dbg->de_frame_reg_rules_entry_count);
2489 res = _dwarf_initialize_fde_table(dbg, &fde_table,
2490 output_table_real_data_size,
2491 error);
2492 if (res != DW_DLV_OK) {
2493 return res;
2494 }
2495 /* Allocate array of internal structs to match,
2496 in count, what was passed in. */
2497 reg_table_i.rt3_rules = calloc(reg_table->rt3_reg_table_size,
2498 sizeof(Dwarf_Regtable_Entry3_i));
2499 if (!reg_table_i.rt3_rules) {
2500 _dwarf_free_fde_table(&fde_table);
2501 _dwarf_error_string(dbg,error,
2502 DW_DLE_ALLOC_FAIL,
2503 "Failure allocating Dwarf_Regtable_Entry3_i "
2504 "in dwarf_get_fde_info_for_all_regs3()");
2505 return DW_DLV_ERROR;
2506 }
2507 /* _dwarf_get_fde_info_for_a_pc_row will perform
2508 more sanity checks */
2509 res = _dwarf_get_fde_info_for_a_pc_row(fde, pc_requested,
2510 &fde_table,
2511 dbg->de_frame_cfa_col_number,
2512 has_more_rows,subsequent_pc,
2513 error);
2514 if (res != DW_DLV_OK) {
2515 free(reg_table_i.rt3_rules);
2516 reg_table_i.rt3_rules = 0;
2517 _dwarf_free_fde_table(&fde_table);
2518 return res;
2519 }
2520
2521 rule_i = &reg_table_i.rt3_rules[0];
2522 rule = &fde_table.fr_reg[0];
2523 /* Initialize known rules */
2524 for (i = 0; i < output_table_real_data_size;
2525 i++, ++rule_i, ++rule) {
2526 rule_i->dw_offset_relevant = rule->ru_is_offset;
2527 rule_i->dw_args_size = rule->ru_args_size;
2528 rule_i->dw_value_type = rule->ru_value_type;
2529 rule_i->dw_regnum = rule->ru_register;
2530 rule_i->dw_offset = (Dwarf_Unsigned)rule->ru_offset;
2531 rule_i->dw_block = rule->ru_block;
2532 }
2533 /* If i < reg_table_i.rt3_reg_table_size finish
2534 initializing register rules */
2535 _dwarf_init_reg_rules_dw3(&reg_table_i.rt3_rules[0],
2536 i, reg_table_i.rt3_reg_table_size,
2537 dbg->de_frame_undefined_value_number);
2538 {
2539 /* Now get this into the real output.
2540 Truncating rule numbers, and offset set
2541 unsigned. */
2542 Dwarf_Unsigned j = 0;
2543 Dwarf_Regtable_Entry3 *targ = &reg_table->rt3_rules[0];
2544 Dwarf_Regtable_Entry3_i *src = &reg_table_i.rt3_rules[0];
2545 for ( ; j < reg_table->rt3_reg_table_size;
2546 ++j, targ++,src++) {
2547 targ->dw_offset_relevant = src->dw_offset_relevant;
2548 targ->dw_args_size = src->dw_args_size;
2549 targ->dw_value_type = src->dw_value_type;
2550 targ->dw_regnum = (Dwarf_Half)src->dw_regnum;
2551 targ->dw_offset= (Dwarf_Unsigned)src->dw_offset;
2552 targ->dw_block = src->dw_block;
2553 }
2554 }
2555 reg_table->rt3_cfa_rule.dw_offset_relevant =
2556 fde_table.fr_cfa_rule.ru_is_offset;
2557 reg_table->rt3_cfa_rule.dw_value_type =
2558 fde_table.fr_cfa_rule.ru_value_type;
2559 reg_table->rt3_cfa_rule.dw_regnum =
2560 (Dwarf_Half)fde_table.fr_cfa_rule.ru_register;
2561 reg_table->rt3_cfa_rule.dw_offset =
2562 (Dwarf_Unsigned)fde_table.fr_cfa_rule.ru_offset;
2563 reg_table->rt3_cfa_rule.dw_block =
2564 fde_table.fr_cfa_rule.ru_block;
2565 reg_table->rt3_cfa_rule.dw_args_size =
2566 fde_table.fr_cfa_rule.ru_args_size;
2567 if (row_pc != NULL) {
2568 *row_pc = fde_table.fr_loc;
2569 }
2570 free(reg_table_i.rt3_rules);
2571 reg_table_i.rt3_rules = 0;
2572 reg_table_i.rt3_reg_table_size = 0;
2573 _dwarf_free_fde_table(&fde_table);
2574 return DW_DLV_OK;
2575 }
2576
2577 int
2578 dwarf_get_fde_info_for_all_regs3(Dwarf_Fde fde,
2579 Dwarf_Addr pc_requested,
2580 Dwarf_Regtable3 * reg_table,
2581 Dwarf_Addr * row_pc,
2582 Dwarf_Error * error)
2583 {
2584 int res = dwarf_get_fde_info_for_all_regs3_b(fde,pc_requested,
2585 reg_table,row_pc,NULL,NULL,error);
2586
2587 return res;
2588 }
2589
2590 /* Table_column DW_FRAME_CFA_COL is not meaningful.
2591 Use dwarf_get_fde_info_for_cfa_reg3_b() to get the CFA.
2592 Call dwarf_set_frame_cfa_value() to set the correct column
2593 after calling dwarf_init()
2594 (DW_FRAME_CFA_COL3 is a sensible column to use).
2595 */
2596 /* New May 2018.
2597 If one is tracking the value of a single table
2598 column through a function, this lets us
2599 skip to the next pc value easily.
2600
2601 if pc_requested is a change from the last
2602 pc_requested on this pc, this function
2603 returns *has_more_rows and *subsequent_pc
2604 (null pointers passed are acceptable, the
2605 assignment through the pointer is skipped
2606 if the pointer is null).
2607 Otherwise *has_more_rows and *subsequent_pc
2608 are not set.
2609
2610 The offset returned is Unsigned, which was
2611 always wrong. Cast to Dwarf_Signed to use it.
2612 */
2613 int
2614 dwarf_get_fde_info_for_reg3_b(Dwarf_Fde fde,
2615 Dwarf_Half table_column,
2616 Dwarf_Addr requested,
2617 Dwarf_Small *value_type,
2618 Dwarf_Unsigned *offset_relevant,
2619 Dwarf_Unsigned *register_num,
2620 Dwarf_Unsigned *offset,
2621 Dwarf_Block *block,
2622 Dwarf_Addr *row_pc_out,
2623 Dwarf_Bool *has_more_rows,
2624 Dwarf_Addr *subsequent_pc,
2625 Dwarf_Error *error)
2626 {
2627 Dwarf_Signed soff = 0;
2628 int res = 0;
2629
2630 res = dwarf_get_fde_info_for_reg3_c(
2631 fde,table_column,requested,
2632 value_type,offset_relevant,
2633 register_num,&soff,
2634 block,row_pc_out,has_more_rows,
2635 subsequent_pc,error);
2636 if (offset) {
2637 *offset = (Dwarf_Unsigned)soff;
2638 }
2639 return res;
2640 }
2641 /* New September 2023.
2642 The same as dwarf_get_fde_info_for_reg3_b() but here
2643 */
2644 int
2645 dwarf_get_fde_info_for_reg3_c(Dwarf_Fde fde,
2646 Dwarf_Half table_column,
2647 Dwarf_Addr pc_requested,
2648 Dwarf_Small *value_type,
2649 Dwarf_Unsigned *offset_relevant,
2650 Dwarf_Unsigned *register_num,
2651 Dwarf_Signed *offset,
2652 Dwarf_Block *block,
2653 Dwarf_Addr *row_pc_out,
2654 Dwarf_Bool *has_more_rows,
2655 Dwarf_Addr *subsequent_pc,
2656 Dwarf_Error *error)
2657 {
2658 struct Dwarf_Frame_s * fde_table = &(fde->fd_fde_table);
2659 int res = DW_DLV_ERROR;
2660
2661 Dwarf_Debug dbg = 0;
2662 Dwarf_Unsigned table_real_data_size = 0;
2663
2664 FDE_NULL_CHECKS_AND_SET_DBG(fde, dbg);
2665
2666 if (!fde->fd_have_fde_tab ||
2667 /* The test is just in case it's not inside the table.
2668 For non-MIPS
2669 it could be outside the table and that is just fine, it was
2670 really a mistake to put it in the table in 1993. */
2671 fde->fd_fde_pc_requested != pc_requested) {
2672 if (fde->fd_have_fde_tab) {
2673 _dwarf_free_fde_table(fde_table);
2674 fde->fd_have_fde_tab = FALSE;
2675 }
2676 table_real_data_size = dbg->de_frame_reg_rules_entry_count;
2677 res = _dwarf_initialize_fde_table(dbg, fde_table,
2678 table_real_data_size, error);
2679 if (res != DW_DLV_OK) {
2680 return res;
2681 }
2682 if (table_column >= table_real_data_size) {
2683 _dwarf_free_fde_table(fde_table);
2684 fde->fd_have_fde_tab = FALSE;
2685 _dwarf_error(dbg, error, DW_DLE_FRAME_TABLE_COL_BAD);
2686 return DW_DLV_ERROR;
2687 }
2688
2689 /* _dwarf_get_fde_info_for_a_pc_row will perform
2690 more sanity checks */
2691 res = _dwarf_get_fde_info_for_a_pc_row(fde,
2692 pc_requested, fde_table,
2693 dbg->de_frame_cfa_col_number,
2694 has_more_rows,subsequent_pc,
2695 error);
2696 if (res != DW_DLV_OK) {
2697 _dwarf_free_fde_table(fde_table);
2698 fde->fd_have_fde_tab = FALSE;
2699 return res;
2700 }
2701 }
2702
2703 if (register_num) {
2704 *register_num = fde_table->fr_reg[table_column].ru_register;
2705 }
2706 if (offset) {
2707 *offset = fde_table->fr_reg[table_column].ru_offset;
2708 }
2709 if (row_pc_out != NULL) {
2710 *row_pc_out = fde_table->fr_loc;
2711 }
2712 if (block) {
2713 *block = fde_table->fr_reg[table_column].ru_block;
2714 }
2715
2716 /* Without value_type the data cannot be understood,
2717 so we insist on it being present, we don't test it. */
2718 *value_type = fde_table->fr_reg[table_column].ru_value_type;
2719 *offset_relevant = (fde_table->fr_reg[table_column].ru_is_offset);
2720 fde->fd_have_fde_tab = TRUE;
2721 fde->fd_fde_pc_requested = pc_requested;
2722 return DW_DLV_OK;
2723
2724 }
2725
2726 /*
2727 This deals with the CFA by not
2728 making the CFA a column number, which means
2729 DW_FRAME_CFA_COL3 is, like DW_CFA_SAME_VALUE,
2730 a special value, not something one uses as an index.
2731
2732 Call dwarf_set_frame_cfa_value() to set the correct column
2733 after calling dwarf_init().
2734 DW_FRAME_CFA_COL3 is a sensible column to use.
2735 */
2736 int
2737 dwarf_get_fde_info_for_cfa_reg3_b(Dwarf_Fde fde,
2738 Dwarf_Addr pc_requested,
2739 Dwarf_Small *value_type,
2740 Dwarf_Unsigned *offset_relevant,
2741 Dwarf_Unsigned *register_num,
2742 Dwarf_Unsigned *offset,
2743 Dwarf_Block *block,
2744 Dwarf_Addr *row_pc_out,
2745 Dwarf_Bool *has_more_rows,
2746 Dwarf_Addr *subsequent_pc,
2747 Dwarf_Error *error)
2748 {
2749 Dwarf_Signed soff = 0;
2750 int res = 0;
2751
2752 res = dwarf_get_fde_info_for_cfa_reg3_c(fde,
2753 pc_requested, value_type,offset_relevant,
2754 register_num,&soff,block, row_pc_out,
2755 has_more_rows,subsequent_pc,error);
2756 if (offset) {
2757 *offset = (Dwarf_Unsigned)soff;
2758 }
2759 return res;
2760 }
2761 /*
2762 New September 2023. With the offset argument
2763 a signed value. This is more correct, so
2764 convert from dwarf_get_fde_info_for_cfa_reg3_b
2765 when convenient.
2766 */
2767 int
2768 dwarf_get_fde_info_for_cfa_reg3_c(Dwarf_Fde fde,
2769 Dwarf_Addr pc_requested,
2770 Dwarf_Small *value_type,
2771 Dwarf_Unsigned *offset_relevant,
2772 Dwarf_Unsigned *register_num,
2773 Dwarf_Signed *offset,
2774 Dwarf_Block *block,
2775 Dwarf_Addr *row_pc_out,
2776 Dwarf_Bool *has_more_rows,
2777 Dwarf_Addr *subsequent_pc,
2778 Dwarf_Error *error)
2779 {
2780 struct Dwarf_Frame_s fde_table;
2781 int res = DW_DLV_ERROR;
2782 Dwarf_Debug dbg = 0;
2783
2784 Dwarf_Unsigned table_real_data_size = 0;
2785
2786 FDE_NULL_CHECKS_AND_SET_DBG(fde, dbg);
2787
2788 table_real_data_size = dbg->de_frame_reg_rules_entry_count;
2789 res = _dwarf_initialize_fde_table(dbg, &fde_table,
2790 table_real_data_size, error);
2791 if (res != DW_DLV_OK)
2792 return res;
2793 res = _dwarf_get_fde_info_for_a_pc_row(fde, pc_requested,
2794 &fde_table,
2795 dbg->de_frame_cfa_col_number,has_more_rows,
2796 subsequent_pc,error);
2797 if (res != DW_DLV_OK) {
2798 _dwarf_free_fde_table(&fde_table);
2799 return res;
2800 }
2801 if (register_num) {
2802 *register_num = fde_table.fr_cfa_rule.ru_register;
2803 }
2804 if (offset) {
2805 *offset = fde_table.fr_cfa_rule.ru_offset;
2806 }
2807 if (row_pc_out != NULL) {
2808 *row_pc_out = fde_table.fr_loc;
2809 }
2810 if (block) {
2811 *block = fde_table.fr_cfa_rule.ru_block;
2812 }
2813 /* Without value_type the data cannot be
2814 understood, so we insist
2815 on it being present, we don't test it. */
2816 *value_type = fde_table.fr_cfa_rule.ru_value_type;
2817 *offset_relevant = fde_table.fr_cfa_rule.ru_is_offset;
2818 _dwarf_free_fde_table(&fde_table);
2819 return DW_DLV_OK;
2820 }
2821
2822 /* Return pointer to the instructions in the dwarf fde. */
2823 int
2824 dwarf_get_fde_instr_bytes(Dwarf_Fde inFde,
2825 Dwarf_Small ** outinstrs,
2826 Dwarf_Unsigned * outinstrslen,
2827 Dwarf_Error * error)
2828 {
2829 Dwarf_Unsigned len = 0;
2830 Dwarf_Small *instrs = 0;
2831 Dwarf_Debug dbg = 0;
2832
2833 if (!inFde) {
2834 _dwarf_error(dbg, error, DW_DLE_FDE_NULL);
2835 return DW_DLV_ERROR;
2836 }
2837 dbg = inFde->fd_dbg;
2838 if (IS_INVALID_DBG(dbg)) {
2839 _dwarf_error_string(NULL, error, DW_DLE_FDE_DBG_NULL,
2840 "DW_DLE_FDE_DBG_NULL: Either null or it contains"
2841 "a stale Dwarf_Debug pointer");
2842 return DW_DLV_ERROR;
2843 }
2844 instrs = inFde->fd_fde_instr_start;
2845 len = inFde->fd_fde_end - inFde->fd_fde_instr_start;
2846 *outinstrs = instrs;
2847 *outinstrslen = len;
2848 return DW_DLV_OK;
2849 }
2850
2851 /* Allows getting an fde from its table via an index.
2852 With more error checking than simply indexing oneself. */
2853 int
2854 dwarf_get_fde_n(Dwarf_Fde * fde_data,
2855 Dwarf_Unsigned fde_index,
2856 Dwarf_Fde * returned_fde, Dwarf_Error * error)
2857 {
2858 Dwarf_Debug dbg = 0;
2859 Dwarf_Unsigned fdecount = 0;
2860
2861 if (fde_data == NULL) {
2862 _dwarf_error(dbg, error, DW_DLE_FDE_PTR_NULL);
2863 return DW_DLV_ERROR;
2864 }
2865
2866 FDE_NULL_CHECKS_AND_SET_DBG(*fde_data, dbg);
2867 /* Assumes fde_data table has at least one entry. */
2868 fdecount = fde_data[0]->fd_is_eh?
2869 dbg->de_fde_count_eh:dbg->de_fde_count;
2870 if (fde_index >= fdecount) {
2871 return DW_DLV_NO_ENTRY;
2872 }
2873 *returned_fde = (*(fde_data + fde_index));
2874 return DW_DLV_OK;
2875 }
2876
2877 /* Lopc and hipc are extensions to the interface to
2878 return the range of addresses that are described
2879 by the returned fde. */
2880 int
2881 dwarf_get_fde_at_pc(Dwarf_Fde * fde_data,
2882 Dwarf_Addr pc_of_interest,
2883 Dwarf_Fde * returned_fde,
2884 Dwarf_Addr * lopc,
2885 Dwarf_Addr * hipc, Dwarf_Error * error)
2886 {
2887 Dwarf_Debug dbg = NULL;
2888 Dwarf_Fde fde = NULL;
2889 Dwarf_Fde entryfde = NULL;
2890 Dwarf_Signed fdecount = 0;
2891
2892 if (fde_data == NULL) {
2893 _dwarf_error(NULL, error, DW_DLE_FDE_PTR_NULL);
2894 return DW_DLV_ERROR;
2895 }
2896
2897 /* Assumes fde_data table has at least one entry. */
2898 entryfde = *fde_data;
2899 FDE_NULL_CHECKS_AND_SET_DBG(entryfde, dbg);
2900 fdecount = entryfde->fd_is_eh?
2901 dbg->de_fde_count_eh:dbg->de_fde_count;
2902 {
2903 /* The fdes are sorted by their addresses. Binary search to
2904 find correct fde. */
2905 Dwarf_Signed low = 0;
2906 Dwarf_Signed high = fdecount - 1L;
2907 Dwarf_Signed middle = 0;
2908 Dwarf_Fde cur_fde;
2909
2910 while (low <= high) {
2911 middle = (low + high) / 2;
2912 cur_fde = fde_data[middle];
2913 if (pc_of_interest < cur_fde->fd_initial_location) {
2914 high = middle - 1;
2915 } else if (pc_of_interest >=
2916 (cur_fde->fd_initial_location +
2917 cur_fde->fd_address_range)) {
2918 low = middle + 1;
2919 } else {
2920 fde = fde_data[middle];
2921 break;
2922 }
2923 }
2924 }
2925
2926 if (fde) {
2927 if (lopc != NULL)
2928 *lopc = fde->fd_initial_location;
2929 if (hipc != NULL)
2930 *hipc =
2931 fde->fd_initial_location + fde->fd_address_range - 1;
2932 *returned_fde = fde;
2933 return DW_DLV_OK;
2934 }
2935
2936 return DW_DLV_NO_ENTRY;
2937 }
2938
2939 /* Expands a single frame instruction block
2940 from a specific cie or fde into a
2941 Dwarf_Frame_Instr_Head.
2942
2943 Call dwarf_set_frame_cfa_value() to set the correct column
2944 after calling dwarf_init().
2945 DW_FRAME_CFA_COL3 is a sensible column to use.
2946 */
2947 int
2948 dwarf_expand_frame_instructions(Dwarf_Cie cie,
2949 Dwarf_Small *instruction,
2950 Dwarf_Unsigned i_length,
2951 Dwarf_Frame_Instr_Head * returned_instr_head,
2952 Dwarf_Unsigned * returned_instr_count,
2953 Dwarf_Error * error)
2954 {
2955 int res = DW_DLV_ERROR;
2956 Dwarf_Debug dbg = 0;
2957 Dwarf_Small * instr_start = instruction;
2958 Dwarf_Small * instr_end = (Dwarf_Small *)instruction + i_length;;
2959
2960 if (cie == 0) {
2961 _dwarf_error(NULL, error, DW_DLE_DBG_NULL);
2962 return DW_DLV_ERROR;
2963 }
2964 dbg = cie->ci_dbg;
2965
2966 if (!returned_instr_head || !returned_instr_count) {
2967 _dwarf_error_string(dbg, error, DW_DLE_RET_OP_LIST_NULL,
2968 "DW_DLE_RET_OP_LIST_NULL: "
2969 "Calling dwarf_expand_frame_instructions without "
2970 "a non-NULL Dwarf_Frame_Instr_Head pointer and "
2971 "count pointer seems wrong.");
2972 return DW_DLV_ERROR;
2973 }
2974 if ( instr_end < instr_start) {
2975 /* Impossible unless there was wraparond somewhere and
2976 we missed it. */
2977 _dwarf_error(dbg, error,DW_DLE_FDE_INSTR_PTR_ERROR);
2978 return DW_DLV_ERROR;
2979 }
2980 res = _dwarf_exec_frame_instr( /* make_instr= */ TRUE,
2981 /* search_pc */ FALSE,
2982 /* search_pc_val */ 0,
2983 /* location */ 0,
2984 instr_start,
2985 instr_end,
2986 /* Dwarf_Frame */ NULL,
2987 cie,
2988 dbg,
2989 dbg->de_frame_cfa_col_number,
2990 /* has more rows */0,
2991 /* subsequent_pc */0,
2992 returned_instr_head,
2993 returned_instr_count,
2994 error);
2995 if (res != DW_DLV_OK) {
2996 return res;
2997 }
2998 return DW_DLV_OK;
2999 }
3000
3001 /* Call to access a single CFA frame instruction.
3002 The 2021 DW_CFA_LLVM addition for hetrogenous
3003 debugging has a third field, an address space
3004 value. */
3005 int
3006 dwarf_get_frame_instruction(Dwarf_Frame_Instr_Head head,
3007 Dwarf_Unsigned instr_index,
3008 Dwarf_Unsigned * instr_offset_in_instrs,
3009 Dwarf_Small * cfa_operation,
3010 const char ** fields_description,
3011 Dwarf_Unsigned * u0,
3012 Dwarf_Unsigned * u1,
3013 Dwarf_Signed * s0,
3014 Dwarf_Signed * s1,
3015 Dwarf_Unsigned * code_alignment_factor,
3016 Dwarf_Signed * data_alignment_factor,
3017 Dwarf_Block * expression_block,
3018 Dwarf_Error * error)
3019 {
3020 Dwarf_Unsigned aspace = 0;
3021 return dwarf_get_frame_instruction_a(head,
3022 instr_index,
3023 instr_offset_in_instrs,
3024 cfa_operation,
3025 fields_description,
3026 u0,
3027 u1,
3028 & aspace,
3029 s0,
3030 s1,
3031 code_alignment_factor,
3032 data_alignment_factor,
3033 expression_block,
3034 error);
3035 }
3036 int
3037 dwarf_get_frame_instruction_a(Dwarf_Frame_Instr_Head head,
3038 Dwarf_Unsigned instr_index,
3039 Dwarf_Unsigned * instr_offset_in_instrs,
3040 Dwarf_Small * cfa_operation,
3041 const char ** fields_description,
3042 Dwarf_Unsigned * u0,
3043 Dwarf_Unsigned * u1,
3044 Dwarf_Unsigned * u2,
3045 Dwarf_Signed * s0,
3046 Dwarf_Signed * s1,
3047 Dwarf_Unsigned * code_alignment_factor,
3048 Dwarf_Signed * data_alignment_factor,
3049 Dwarf_Block * expression_block,
3050 Dwarf_Error * error)
3051 {
3052 Dwarf_Frame_Instr ip = 0;
3053 Dwarf_Debug dbg = 0;
3054 if (!head) {
3055 _dwarf_error_string(dbg, error,DW_DLE_CFA_INSTRUCTION_ERROR,
3056 "DW_DLE_CFA_INSTRUCTION_ERROR: Head argument NULL "
3057 " calling dwarf_get_frame_instruction");
3058 return DW_DLV_ERROR;
3059 }
3060 if (!head->fh_dbg) {
3061 _dwarf_error_string(dbg, error,DW_DLE_CFA_INSTRUCTION_ERROR,
3062 "DW_DLE_CFA_INSTRUCTION_ERROR: Head missing "
3063 "Dwarf_Debug field "
3064 " calling dwarf_get_frame_instruction");
3065 return DW_DLV_ERROR;
3066 }
3067 dbg = head->fh_dbg;
3068 if (instr_index >= head->fh_array_count) {
3069 return DW_DLV_NO_ENTRY;
3070 }
3071 ip = head->fh_array[instr_index];
3072 if (!ip) {
3073 _dwarf_error_string(dbg, error,DW_DLE_CFA_INSTRUCTION_ERROR,
3074 "DW_DLE_CFA_INSTRUCTION_ERROR: instr array missing "
3075 "calling dwarf_get_frame_instruction");
3076 return DW_DLV_ERROR;
3077 }
3078 *instr_offset_in_instrs = ip->fi_instr_offset;
3079 *cfa_operation = ip->fi_op;
3080 *fields_description = ip->fi_fields;
3081 *u0 = ip->fi_u0;
3082 *u1 = ip->fi_u1;
3083 *u2 = ip->fi_u2;
3084 *s0 = ip->fi_s0;
3085 *s1 = ip->fi_s1;
3086 /* These next two might be known to caller already,
3087 so let caller not pass useless pointers. */
3088 if (code_alignment_factor) {
3089 *code_alignment_factor = ip->fi_code_align_factor;
3090 }
3091 if (data_alignment_factor) {
3092 *data_alignment_factor = ip->fi_data_align_factor;
3093 }
3094 *expression_block = ip->fi_expr;
3095 return DW_DLV_OK;
3096 }
3097
3098 /* Used by dwarfdump -v to print offsets, for debugging
3099 dwarf info.
3100 The dwarf_ version is preferred over the obsolete _dwarf version.
3101 _dwarf version kept for compatibility.
3102 */
3103 int
3104 _dwarf_fde_section_offset(Dwarf_Debug dbg, Dwarf_Fde in_fde,
3105 Dwarf_Off * fde_off, Dwarf_Off * cie_off,
3106 Dwarf_Error * error)
3107 {
3108 return dwarf_fde_section_offset(dbg,in_fde,fde_off,
3109 cie_off,error);
3110 }
3111 int
3112 dwarf_fde_section_offset(Dwarf_Debug dbg, Dwarf_Fde in_fde,
3113 Dwarf_Off * fde_off, Dwarf_Off * cie_off,
3114 Dwarf_Error * error)
3115 {
3116 char *start = 0;
3117 char *loc = 0;
3118
3119 CHECK_DBG(dbg,error,"dwarf_fde_section_offset()");
3120 if (!in_fde) {
3121 _dwarf_error(dbg, error, DW_DLE_FDE_NULL);
3122 return DW_DLV_ERROR;
3123 }
3124 start = (char *) in_fde->fd_section_ptr;
3125 loc = (char *) in_fde->fd_fde_start;
3126
3127 *fde_off = (loc - start);
3128 *cie_off = in_fde->fd_cie_offset;
3129 return DW_DLV_OK;
3130 }
3131
3132 /* Used by dwarfdump -v to print offsets, for debugging
3133 dwarf info.
3134 The dwarf_ version is preferred over the obsolete _dwarf version.
3135 _dwarf version kept for compatibility.
3136 */
3137 int
3138 _dwarf_cie_section_offset(Dwarf_Debug dbg, Dwarf_Cie in_cie,
3139 Dwarf_Off * cie_off, Dwarf_Error * error)
3140 {
3141 return dwarf_cie_section_offset(dbg,in_cie,cie_off,error);
3142 }
3143
3144 int
3145 dwarf_cie_section_offset(Dwarf_Debug dbg, Dwarf_Cie in_cie,
3146 Dwarf_Off * cie_off, Dwarf_Error * error)
3147 {
3148 char *start = 0;
3149 char *loc = 0;
3150
3151 CHECK_DBG(dbg,error,"dwarf_cie_section_offset()");
3152 if (!in_cie) {
3153 _dwarf_error(dbg, error, DW_DLE_CIE_NULL);
3154 return DW_DLV_ERROR;
3155 }
3156 start = (char *) in_cie->ci_section_ptr;
3157 loc = (char *) in_cie->ci_cie_start;
3158
3159 *cie_off = (loc - start);
3160 return DW_DLV_OK;
3161 }
3162
3163 /* Returns a pointer to target-specific augmentation data
3164 thru augdata
3165 and returns the length of the data thru augdata_len.
3166
3167 It's up to the consumer code to know how to interpret the bytes
3168 of target-specific data (endian issues apply too, these
3169 are just raw bytes pointed to).
3170 See Linux Standard Base Core Specification version 3.0 for
3171 the details on .eh_frame info.
3172
3173 Returns DW_DLV_ERROR if fde is NULL or some other serious
3174 error.
3175 Returns DW_DLV_NO_ENTRY if there is no target-specific
3176 augmentation data.
3177
3178 The bytes pointed to are in the Dwarf_Cie, and as long as that
3179 is valid the bytes are there. No 'dealloc' call is needed
3180 for the bytes. */
3181 int
3182 dwarf_get_cie_augmentation_data(Dwarf_Cie cie,
3183 Dwarf_Small ** augdata,
3184 Dwarf_Unsigned * augdata_len,
3185 Dwarf_Error * error)
3186 {
3187 if (cie == NULL) {
3188 _dwarf_error(NULL, error, DW_DLE_CIE_NULL);
3189 return DW_DLV_ERROR;
3190 }
3191 if (cie->ci_gnu_eh_augmentation_len == 0) {
3192 return DW_DLV_NO_ENTRY;
3193 }
3194 *augdata = (Dwarf_Small *) (cie->ci_gnu_eh_augmentation_bytes);
3195 *augdata_len = cie->ci_gnu_eh_augmentation_len;
3196 return DW_DLV_OK;
3197 }
3198
3199 /* Returns a pointer to target-specific augmentation data
3200 thru augdata
3201 and returns the length of the data thru augdata_len.
3202
3203 It's up to the consumer code to know how to interpret the bytes
3204 of target-specific data (endian issues apply too, these
3205 are just raw bytes pointed to).
3206 See Linux Standard Base Core Specification version 3.0 for
3207 the details on .eh_frame info.
3208
3209 Returns DW_DLV_ERROR if fde is NULL or some other serious
3210 error.
3211 Returns DW_DLV_NO_ENTRY if there is no target-specific
3212 augmentation data.
3213
3214 The bytes pointed to are in the Dwarf_Fde, and as long as that
3215 is valid the bytes are there. No 'dealloc' call is needed
3216 for the bytes. */
3217 int
3218 dwarf_get_fde_augmentation_data(Dwarf_Fde fde,
3219 Dwarf_Small * *augdata,
3220 Dwarf_Unsigned * augdata_len,
3221 Dwarf_Error * error)
3222 {
3223 Dwarf_Cie cie = 0;
3224
3225 if (fde == NULL) {
3226 _dwarf_error(NULL, error, DW_DLE_FDE_NULL);
3227 return DW_DLV_ERROR;
3228 }
3229 if (!fde->fd_gnu_eh_aug_present) {
3230 return DW_DLV_NO_ENTRY;
3231 }
3232 cie = fde->fd_cie;
3233 if (cie == NULL) {
3234 _dwarf_error(NULL, error, DW_DLE_CIE_NULL);
3235 return DW_DLV_ERROR;
3236 }
3237 *augdata = (Dwarf_Small *) fde->fd_gnu_eh_augmentation_bytes;
3238 *augdata_len = fde->fd_gnu_eh_augmentation_len;
3239 return DW_DLV_OK;
3240 }
3241
3242 #if 0 /* dump_frame_rule() FOR DEBUGGING */
3243 /* Used solely for debugging libdwarf. */
3244 static void
3245 dump_frame_rule(char *msg, struct Dwarf_Reg_Rule_s *reg_rule)
3246 {
3247 printf
3248 ("%s type %s (0x%" DW_PR_XZEROS DW_PR_DUx
3249 "), is_off %" DW_PR_DUu
3250 " reg %" DW_PR_DUu " offset 0x%" DW_PR_XZEROS DW_PR_DUx
3251 " blockp 0x%" DW_PR_XZEROS DW_PR_DUx "\n",
3252 msg,
3253 (reg_rule->ru_value_type == DW_EXPR_OFFSET) ?
3254 "DW_EXPR_OFFSET" :
3255 (reg_rule->ru_value_type == DW_EXPR_VAL_OFFSET) ?
3256 "DW_EXPR_VAL_OFFSET" :
3257 (reg_rule->ru_value_type == DW_EXPR_VAL_EXPRESSION) ?
3258 "DW_EXPR_VAL_EXPRESSION" :
3259 (reg_rule->ru_value_type == DW_EXPR_EXPRESSION) ?
3260 "DW_EXPR_EXPRESSION" : "Unknown",
3261 (Dwarf_Unsigned) reg_rule->ru_value_type,
3262 (Dwarf_Unsigned) reg_rule->ru_is_off,
3263 (Dwarf_Unsigned) reg_rule->ru_register,
3264 (Dwarf_Unsigned) reg_rule->ru_offset_or_block_len,
3265 (Dwarf_Unsigned) reg_rule->ru_block);
3266 return;
3267 }
3268 #endif /*0*/
3269
3270 /* This allows consumers to set the 'initial value' so that
3271 an ISA/ABI specific default can be used, dynamically,
3272 at run time. Useful for dwarfdump and non-MIPS architectures..
3273 The value defaults to one of
3274 DW_FRAME_SAME_VALUE or DW_FRAME_UNKNOWN_VALUE
3275 but dwarfdump can dump multiple ISA/ABI objects so
3276 we may want to get this set to what the ABI says is correct.
3277
3278 Returns the value that was present before we changed it here. */
3279 Dwarf_Half
3280 dwarf_set_frame_rule_initial_value(Dwarf_Debug dbg,
3281 Dwarf_Half value)
3282 {
3283 Dwarf_Half orig =
3284 (Dwarf_Half)dbg->de_frame_rule_initial_value;
3285 dbg->de_frame_rule_initial_value = value;
3286 return orig;
3287 }
3288
3289 /* This allows consumers to set the array size of the reg rules
3290 table so that
3291 an ISA/ABI specific value can be used, dynamically,
3292 at run time. Useful for non-MIPS architectures.
3293 The value defaults to DW_FRAME_LAST_REG_NUM.
3294 but dwarfdump can dump multiple ISA/ABI objects so
3295 consumers want to get this set to what the ABI says is correct.
3296
3297 Returns the value that was present before we changed it here.
3298 */
3299
3300 Dwarf_Half
3301 dwarf_set_frame_rule_table_size(Dwarf_Debug dbg, Dwarf_Half value)
3302 {
3303 Dwarf_Half orig =
3304 (Dwarf_Half)dbg->de_frame_reg_rules_entry_count;
3305 dbg->de_frame_reg_rules_entry_count = value;
3306
3307 /* Take the caller-specified value, but do not
3308 let the value be too small. Keep it at least to
3309 DW_FRAME_LAST_REG_NUM.
3310 This helps prevent libdwarf (mistakenly) indexing outside
3311 of of a register array when the ABI reg count
3312 is really small. */
3313 if (value < DW_FRAME_LAST_REG_NUM) {
3314 dbg->de_frame_reg_rules_entry_count = DW_FRAME_LAST_REG_NUM;
3315 }
3316 return orig;
3317 }
3318 /* This allows consumers to set the CFA register value
3319 so that an ISA/ABI specific value can be used, dynamically,
3320 at run time. Useful for non-MIPS architectures.
3321 The value defaults to DW_FRAME_CFA_COL3 and should be
3322 higher than any real register in the ABI.
3323 Dwarfdump can dump multiple ISA/ABI objects so
3324 consumers want to get this set to what the ABI says is correct.
3325
3326 Returns the value that was present before we changed it here. */
3327
3328 Dwarf_Half
3329 dwarf_set_frame_cfa_value(Dwarf_Debug dbg, Dwarf_Half value)
3330 {
3331 Dwarf_Half orig = (Dwarf_Half)dbg->de_frame_cfa_col_number;
3332 dbg->de_frame_cfa_col_number = value;
3333 return orig;
3334 }
3335 /* Similar to above, but for the other crucial fields for frames. */
3336 Dwarf_Half
3337 dwarf_set_frame_same_value(Dwarf_Debug dbg, Dwarf_Half value)
3338 {
3339 Dwarf_Half orig =
3340 (Dwarf_Half)dbg->de_frame_same_value_number;
3341 dbg->de_frame_same_value_number = value;
3342 return orig;
3343 }
3344 Dwarf_Half
3345 dwarf_set_frame_undefined_value(Dwarf_Debug dbg, Dwarf_Half value)
3346 {
3347 Dwarf_Half orig =
3348 (Dwarf_Half)dbg->de_frame_same_value_number;
3349 dbg->de_frame_undefined_value_number = value;
3350 return orig;
3351 }
3352
3353 /* Does something only if value passed in is greater than 0 and
3354 a size than we can handle (in number of bytes). */
3355 Dwarf_Small
3356 dwarf_set_default_address_size(Dwarf_Debug dbg,
3357 Dwarf_Small value )
3358 {
3359 Dwarf_Small orig = dbg->de_pointer_size;
3360 if (value > 0 && value <= sizeof(Dwarf_Addr)) {
3361 dbg->de_pointer_size = value;
3362 }
3363 return orig;
3364 }
3365
3366 static int
3367 init_reg_rules_alloc(Dwarf_Debug dbg,struct Dwarf_Frame_s *f,
3368 Dwarf_Unsigned count, Dwarf_Error * error)
3369 {
3370 f->fr_reg_count = count;
3371 f->fr_reg = (struct Dwarf_Reg_Rule_s *)
3372 calloc((size_t)count, sizeof(struct Dwarf_Reg_Rule_s));
3373 if (f->fr_reg == 0) {
3374 if (error) {
3375 _dwarf_error(dbg, error, DW_DLE_DF_ALLOC_FAIL);
3376 }
3377 return DW_DLV_ERROR;
3378 }
3379 _dwarf_init_reg_rules_ru(f->fr_reg,0, count,
3380 dbg->de_frame_rule_initial_value);
3381 return DW_DLV_OK;
3382 }
3383 static int
3384 _dwarf_initialize_fde_table(Dwarf_Debug dbg,
3385 struct Dwarf_Frame_s *fde_table,
3386 Dwarf_Unsigned table_real_data_size,
3387 Dwarf_Error * error)
3388 {
3389 unsigned entry_size = sizeof(struct Dwarf_Frame_s);
3390 memset(fde_table,0,entry_size);
3391 fde_table->fr_loc = 0;
3392 fde_table->fr_next = 0;
3393
3394 return init_reg_rules_alloc(dbg,fde_table,
3395 table_real_data_size,error);
3396 }
3397 static void
3398 _dwarf_free_fde_table(struct Dwarf_Frame_s *fde_table)
3399 {
3400 free(fde_table->fr_reg);
3401 fde_table->fr_reg_count = 0;
3402 fde_table->fr_reg = 0;
3403 }
3404
3405 /* Return DW_DLV_OK if we succeed. else return DW_DLV_ERROR.
3406 */
3407 int
3408 _dwarf_frame_constructor(Dwarf_Debug dbg, void *frame)
3409 {
3410 struct Dwarf_Frame_s *fp = frame;
3411
3412 if (IS_INVALID_DBG(dbg)) {
3413 return DW_DLV_ERROR;
3414 }
3415 return init_reg_rules_alloc(dbg,fp,
3416 dbg->de_frame_reg_rules_entry_count, 0);
3417 }
3418
3419 void
3420 _dwarf_frame_destructor(void *frame)
3421 {
3422 struct Dwarf_Frame_s *fp = frame;
3423 _dwarf_free_fde_table(fp);
3424 }
3425
3426 void
3427 _dwarf_fde_destructor(void *f)
3428 {
3429 struct Dwarf_Fde_s *fde = f;
3430
3431 if (fde->fd_fde_owns_cie) {
3432 Dwarf_Debug dbg = fde->fd_dbg;
3433
3434 if (!dbg->de_in_tdestroy) {
3435 /* This is just for dwarf_get_fde_for_die() and
3436 must not be applied in alloc tree destruction. */
3437 dwarf_dealloc(fde->fd_dbg,fde->fd_cie,DW_DLA_CIE);
3438 fde->fd_cie = 0;
3439 }
3440 }
3441 if (fde->fd_have_fde_tab) {
3442 _dwarf_free_fde_table(&fde->fd_fde_table);
3443 fde->fd_have_fde_tab = FALSE;
3444 }
3445 }
3446 void
3447 _dwarf_frame_instr_destructor(void *f)
3448 {
3449 Dwarf_Frame_Instr_Head head = f;
3450 Dwarf_Debug dbg = head->fh_dbg;
3451 Dwarf_Unsigned count = head->fh_array_count;
3452 Dwarf_Unsigned i = 0;
3453
3454 for ( ; i < count ; ++i) {
3455 free(head->fh_array[i]);
3456 head->fh_array[i] = 0;
3457 }
3458 dwarf_dealloc(dbg,head->fh_array,DW_DLA_LIST);
3459 head->fh_array = 0;
3460 head->fh_array_count = 0;
3461 }
3462 void
3463 dwarf_dealloc_frame_instr_head(Dwarf_Frame_Instr_Head h)
3464 {
3465 if (!h) {
3466 return;
3467 }
3468 dwarf_dealloc(h->fh_dbg,h,DW_DLA_FRAME_INSTR_HEAD);
3469 }
3470
3471 static void
3472 _dwarf_init_reg_rules_ru(struct Dwarf_Reg_Rule_s *base,
3473 Dwarf_Unsigned first, Dwarf_Unsigned last,
3474 Dwarf_Unsigned initial_value)
3475 {
3476 struct Dwarf_Reg_Rule_s *r = base+first;
3477 Dwarf_Unsigned i = first;
3478 for (; i < last; ++i,++r) {
3479 r->ru_is_offset = 0;
3480 r->ru_value_type = DW_EXPR_OFFSET;
3481 r->ru_register = (Dwarf_Unsigned)initial_value;
3482 r->ru_offset = 0;
3483 r->ru_args_size = 0;
3484 r->ru_block.bl_data = 0;
3485 r->ru_block.bl_len = 0;
3486 }
3487 }
3488
3489 /* For any remaining columns after what fde has. */
3490 static void
3491 _dwarf_init_reg_rules_dw3(
3492 Dwarf_Regtable_Entry3_i *base,
3493 Dwarf_Unsigned first, Dwarf_Unsigned last,
3494 Dwarf_Unsigned initial_value)
3495 {
3496 Dwarf_Regtable_Entry3_i *r = base+first;
3497 Dwarf_Unsigned i = first;
3498 for (; i < last; ++i,++r) {
3499 r->dw_offset_relevant = 0;
3500 r->dw_value_type = DW_EXPR_OFFSET;
3501 r->dw_regnum = initial_value;
3502 r->dw_offset = 0;
3503 r->dw_args_size = 0;
3504 r->dw_block.bl_data = 0;
3505 r->dw_block.bl_len = 0;
3506 }
3507 }