1 /* Frame unwinder for frames with DWARF Call Frame Information.
3 Copyright (C) 2003-2021 Free Software Foundation, Inc.
5 Contributed by Mark Kettenis.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "dwarf2/expr.h"
25 #include "dwarf2/leb.h"
27 #include "frame-base.h"
28 #include "frame-unwind.h"
37 #include "complaints.h"
38 #include "dwarf2/frame.h"
39 #include "dwarf2/read.h"
40 #include "dwarf2/public.h"
42 #include "dwarf2/loc.h"
43 #include "dwarf2/frame-tailcall.h"
44 #include "gdbsupport/gdb_binary_search.h"
46 #include "gdbsupport/selftest.h"
47 #include "selftest-arch.h"
49 #include <unordered_map>
55 /* Call Frame Information (CFI). */
57 /* Common Information Entry (CIE). */
61 /* Computation Unit for this CIE. */
62 struct comp_unit
*unit
;
64 /* Offset into the .debug_frame section where this CIE was found.
65 Used to identify this CIE. */
68 /* Constant that is factored out of all advance location
70 ULONGEST code_alignment_factor
;
72 /* Constants that is factored out of all offset instructions. */
73 LONGEST data_alignment_factor
;
75 /* Return address column. */
76 ULONGEST return_address_register
;
78 /* Instruction sequence to initialize a register set. */
79 const gdb_byte
*initial_instructions
;
82 /* Saved augmentation, in case it's needed later. */
85 /* Encoding of addresses. */
88 /* Target address size in bytes. */
91 /* Target pointer size in bytes. */
94 /* True if a 'z' augmentation existed. */
95 unsigned char saw_z_augmentation
;
97 /* True if an 'S' augmentation existed. */
98 unsigned char signal_frame
;
100 /* The version recorded in the CIE. */
101 unsigned char version
;
103 /* The segment size. */
104 unsigned char segment_size
;
107 /* The CIE table is used to find CIEs during parsing, but then
108 discarded. It maps from the CIE's offset to the CIE. */
109 typedef std::unordered_map
<ULONGEST
, dwarf2_cie
*> dwarf2_cie_table
;
111 /* Frame Description Entry (FDE). */
115 /* CIE for this FDE. */
116 struct dwarf2_cie
*cie
;
118 /* First location associated with this FDE. */
119 CORE_ADDR initial_location
;
121 /* Number of bytes of program instructions described by this FDE. */
122 CORE_ADDR address_range
;
124 /* Instruction sequence. */
125 const gdb_byte
*instructions
;
128 /* True if this FDE is read from a .eh_frame instead of a .debug_frame
130 unsigned char eh_frame_p
;
133 typedef std::vector
<dwarf2_fde
*> dwarf2_fde_table
;
135 /* A minimal decoding of DWARF2 compilation units. We only decode
136 what's needed to get to the call frame information. */
140 comp_unit (struct objfile
*objf
)
145 /* Keep the bfd convenient. */
148 /* Pointer to the .debug_frame section loaded into memory. */
149 const gdb_byte
*dwarf_frame_buffer
= nullptr;
151 /* Length of the loaded .debug_frame section. */
152 bfd_size_type dwarf_frame_size
= 0;
154 /* Pointer to the .debug_frame section. */
155 asection
*dwarf_frame_section
= nullptr;
157 /* Base for DW_EH_PE_datarel encodings. */
160 /* Base for DW_EH_PE_textrel encodings. */
164 dwarf2_fde_table fde_table
;
166 /* Hold data used by this module. */
167 auto_obstack obstack
;
170 static struct dwarf2_fde
*dwarf2_frame_find_fde
171 (CORE_ADDR
*pc
, dwarf2_per_objfile
**out_per_objfile
);
173 static int dwarf2_frame_adjust_regnum (struct gdbarch
*gdbarch
, int regnum
,
176 static CORE_ADDR
read_encoded_value (struct comp_unit
*unit
, gdb_byte encoding
,
177 int ptr_len
, const gdb_byte
*buf
,
178 unsigned int *bytes_read_ptr
,
179 CORE_ADDR func_base
);
182 /* See dwarf2-frame.h. */
183 bool dwarf2_frame_unwinders_enabled_p
= true;
185 /* Store the length the expression for the CFA in the `cfa_reg' field,
186 which is unused in that case. */
187 #define cfa_exp_len cfa_reg
189 dwarf2_frame_state::dwarf2_frame_state (CORE_ADDR pc_
, struct dwarf2_cie
*cie
)
190 : pc (pc_
), data_align (cie
->data_alignment_factor
),
191 code_align (cie
->code_alignment_factor
),
192 retaddr_column (cie
->return_address_register
)
196 /* Execute the required actions for both the DW_CFA_restore and
197 DW_CFA_restore_extended instructions. */
199 dwarf2_restore_rule (struct gdbarch
*gdbarch
, ULONGEST reg_num
,
200 struct dwarf2_frame_state
*fs
, int eh_frame_p
)
204 reg
= dwarf2_frame_adjust_regnum (gdbarch
, reg_num
, eh_frame_p
);
205 fs
->regs
.alloc_regs (reg
+ 1);
207 /* Check if this register was explicitly initialized in the
208 CIE initial instructions. If not, default the rule to
210 if (reg
< fs
->initial
.reg
.size ())
211 fs
->regs
.reg
[reg
] = fs
->initial
.reg
[reg
];
213 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_UNSPECIFIED
;
215 if (fs
->regs
.reg
[reg
].how
== DWARF2_FRAME_REG_UNSPECIFIED
)
217 int regnum
= dwarf_reg_to_regnum (gdbarch
, reg
);
220 incomplete CFI data; DW_CFA_restore unspecified\n\
221 register %s (#%d) at %s"),
222 gdbarch_register_name (gdbarch
, regnum
), regnum
,
223 paddress (gdbarch
, fs
->pc
));
227 class dwarf_expr_executor
: public dwarf_expr_context
231 dwarf_expr_executor (dwarf2_per_objfile
*per_objfile
)
232 : dwarf_expr_context (per_objfile
)
237 void invalid (const char *op
) ATTRIBUTE_NORETURN
239 error (_("%s is invalid in this context"), op
);
244 execute_stack_op (const gdb_byte
*exp
, ULONGEST len
, int addr_size
,
245 struct frame_info
*this_frame
, CORE_ADDR initial
,
246 int initial_in_stack_memory
, dwarf2_per_objfile
*per_objfile
)
250 dwarf_expr_executor
ctx (per_objfile
);
251 scoped_value_mark free_values
;
253 ctx
.frame
= this_frame
;
254 ctx
.gdbarch
= get_frame_arch (this_frame
);
255 ctx
.addr_size
= addr_size
;
257 ctx
.push_address (initial
, initial_in_stack_memory
);
260 if (ctx
.location
== DWARF_VALUE_MEMORY
)
261 result
= ctx
.fetch_address (0);
262 else if (ctx
.location
== DWARF_VALUE_REGISTER
)
263 result
= read_addr_from_reg (this_frame
, value_as_long (ctx
.fetch (0)));
266 /* This is actually invalid DWARF, but if we ever do run across
267 it somehow, we might as well support it. So, instead, report
268 it as unimplemented. */
270 Not implemented: computing unwound register using explicit value operator"));
277 /* Execute FDE program from INSN_PTR possibly up to INSN_END or up to inferior
278 PC. Modify FS state accordingly. Return current INSN_PTR where the
279 execution has stopped, one can resume it on the next call. */
281 static const gdb_byte
*
282 execute_cfa_program (struct dwarf2_fde
*fde
, const gdb_byte
*insn_ptr
,
283 const gdb_byte
*insn_end
, struct gdbarch
*gdbarch
,
284 CORE_ADDR pc
, struct dwarf2_frame_state
*fs
,
285 CORE_ADDR text_offset
)
287 int eh_frame_p
= fde
->eh_frame_p
;
288 unsigned int bytes_read
;
289 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
291 while (insn_ptr
< insn_end
&& fs
->pc
<= pc
)
293 gdb_byte insn
= *insn_ptr
++;
297 if ((insn
& 0xc0) == DW_CFA_advance_loc
)
298 fs
->pc
+= (insn
& 0x3f) * fs
->code_align
;
299 else if ((insn
& 0xc0) == DW_CFA_offset
)
302 reg
= dwarf2_frame_adjust_regnum (gdbarch
, reg
, eh_frame_p
);
303 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, &utmp
);
304 offset
= utmp
* fs
->data_align
;
305 fs
->regs
.alloc_regs (reg
+ 1);
306 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_SAVED_OFFSET
;
307 fs
->regs
.reg
[reg
].loc
.offset
= offset
;
309 else if ((insn
& 0xc0) == DW_CFA_restore
)
312 dwarf2_restore_rule (gdbarch
, reg
, fs
, eh_frame_p
);
319 fs
->pc
= read_encoded_value (fde
->cie
->unit
, fde
->cie
->encoding
,
320 fde
->cie
->ptr_size
, insn_ptr
,
321 &bytes_read
, fde
->initial_location
);
322 /* Apply the text offset for relocatable objects. */
323 fs
->pc
+= text_offset
;
324 insn_ptr
+= bytes_read
;
327 case DW_CFA_advance_loc1
:
328 utmp
= extract_unsigned_integer (insn_ptr
, 1, byte_order
);
329 fs
->pc
+= utmp
* fs
->code_align
;
332 case DW_CFA_advance_loc2
:
333 utmp
= extract_unsigned_integer (insn_ptr
, 2, byte_order
);
334 fs
->pc
+= utmp
* fs
->code_align
;
337 case DW_CFA_advance_loc4
:
338 utmp
= extract_unsigned_integer (insn_ptr
, 4, byte_order
);
339 fs
->pc
+= utmp
* fs
->code_align
;
343 case DW_CFA_offset_extended
:
344 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, ®
);
345 reg
= dwarf2_frame_adjust_regnum (gdbarch
, reg
, eh_frame_p
);
346 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, &utmp
);
347 offset
= utmp
* fs
->data_align
;
348 fs
->regs
.alloc_regs (reg
+ 1);
349 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_SAVED_OFFSET
;
350 fs
->regs
.reg
[reg
].loc
.offset
= offset
;
353 case DW_CFA_restore_extended
:
354 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, ®
);
355 dwarf2_restore_rule (gdbarch
, reg
, fs
, eh_frame_p
);
358 case DW_CFA_undefined
:
359 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, ®
);
360 reg
= dwarf2_frame_adjust_regnum (gdbarch
, reg
, eh_frame_p
);
361 fs
->regs
.alloc_regs (reg
+ 1);
362 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_UNDEFINED
;
365 case DW_CFA_same_value
:
366 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, ®
);
367 reg
= dwarf2_frame_adjust_regnum (gdbarch
, reg
, eh_frame_p
);
368 fs
->regs
.alloc_regs (reg
+ 1);
369 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_SAME_VALUE
;
372 case DW_CFA_register
:
373 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, ®
);
374 reg
= dwarf2_frame_adjust_regnum (gdbarch
, reg
, eh_frame_p
);
375 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, &utmp
);
376 utmp
= dwarf2_frame_adjust_regnum (gdbarch
, utmp
, eh_frame_p
);
377 fs
->regs
.alloc_regs (reg
+ 1);
378 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_SAVED_REG
;
379 fs
->regs
.reg
[reg
].loc
.reg
= utmp
;
382 case DW_CFA_remember_state
:
384 struct dwarf2_frame_state_reg_info
*new_rs
;
386 new_rs
= new dwarf2_frame_state_reg_info (fs
->regs
);
387 fs
->regs
.prev
= new_rs
;
391 case DW_CFA_restore_state
:
393 struct dwarf2_frame_state_reg_info
*old_rs
= fs
->regs
.prev
;
398 bad CFI data; mismatched DW_CFA_restore_state at %s"),
399 paddress (gdbarch
, fs
->pc
));
402 fs
->regs
= std::move (*old_rs
);
407 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, ®
);
408 fs
->regs
.cfa_reg
= reg
;
409 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, &utmp
);
411 if (fs
->armcc_cfa_offsets_sf
)
412 utmp
*= fs
->data_align
;
414 fs
->regs
.cfa_offset
= utmp
;
415 fs
->regs
.cfa_how
= CFA_REG_OFFSET
;
418 case DW_CFA_def_cfa_register
:
419 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, ®
);
420 fs
->regs
.cfa_reg
= dwarf2_frame_adjust_regnum (gdbarch
, reg
,
422 fs
->regs
.cfa_how
= CFA_REG_OFFSET
;
425 case DW_CFA_def_cfa_offset
:
426 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, &utmp
);
428 if (fs
->armcc_cfa_offsets_sf
)
429 utmp
*= fs
->data_align
;
431 fs
->regs
.cfa_offset
= utmp
;
432 /* cfa_how deliberately not set. */
438 case DW_CFA_def_cfa_expression
:
439 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, &utmp
);
440 fs
->regs
.cfa_exp_len
= utmp
;
441 fs
->regs
.cfa_exp
= insn_ptr
;
442 fs
->regs
.cfa_how
= CFA_EXP
;
443 insn_ptr
+= fs
->regs
.cfa_exp_len
;
446 case DW_CFA_expression
:
447 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, ®
);
448 reg
= dwarf2_frame_adjust_regnum (gdbarch
, reg
, eh_frame_p
);
449 fs
->regs
.alloc_regs (reg
+ 1);
450 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, &utmp
);
451 fs
->regs
.reg
[reg
].loc
.exp
.start
= insn_ptr
;
452 fs
->regs
.reg
[reg
].loc
.exp
.len
= utmp
;
453 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_SAVED_EXP
;
457 case DW_CFA_offset_extended_sf
:
458 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, ®
);
459 reg
= dwarf2_frame_adjust_regnum (gdbarch
, reg
, eh_frame_p
);
460 insn_ptr
= safe_read_sleb128 (insn_ptr
, insn_end
, &offset
);
461 offset
*= fs
->data_align
;
462 fs
->regs
.alloc_regs (reg
+ 1);
463 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_SAVED_OFFSET
;
464 fs
->regs
.reg
[reg
].loc
.offset
= offset
;
467 case DW_CFA_val_offset
:
468 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, ®
);
469 fs
->regs
.alloc_regs (reg
+ 1);
470 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, &utmp
);
471 offset
= utmp
* fs
->data_align
;
472 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_SAVED_VAL_OFFSET
;
473 fs
->regs
.reg
[reg
].loc
.offset
= offset
;
476 case DW_CFA_val_offset_sf
:
477 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, ®
);
478 fs
->regs
.alloc_regs (reg
+ 1);
479 insn_ptr
= safe_read_sleb128 (insn_ptr
, insn_end
, &offset
);
480 offset
*= fs
->data_align
;
481 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_SAVED_VAL_OFFSET
;
482 fs
->regs
.reg
[reg
].loc
.offset
= offset
;
485 case DW_CFA_val_expression
:
486 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, ®
);
487 fs
->regs
.alloc_regs (reg
+ 1);
488 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, &utmp
);
489 fs
->regs
.reg
[reg
].loc
.exp
.start
= insn_ptr
;
490 fs
->regs
.reg
[reg
].loc
.exp
.len
= utmp
;
491 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_SAVED_VAL_EXP
;
495 case DW_CFA_def_cfa_sf
:
496 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, ®
);
497 fs
->regs
.cfa_reg
= dwarf2_frame_adjust_regnum (gdbarch
, reg
,
499 insn_ptr
= safe_read_sleb128 (insn_ptr
, insn_end
, &offset
);
500 fs
->regs
.cfa_offset
= offset
* fs
->data_align
;
501 fs
->regs
.cfa_how
= CFA_REG_OFFSET
;
504 case DW_CFA_def_cfa_offset_sf
:
505 insn_ptr
= safe_read_sleb128 (insn_ptr
, insn_end
, &offset
);
506 fs
->regs
.cfa_offset
= offset
* fs
->data_align
;
507 /* cfa_how deliberately not set. */
510 case DW_CFA_GNU_args_size
:
512 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, &utmp
);
515 case DW_CFA_GNU_negative_offset_extended
:
516 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, ®
);
517 reg
= dwarf2_frame_adjust_regnum (gdbarch
, reg
, eh_frame_p
);
518 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, &utmp
);
519 offset
= utmp
* fs
->data_align
;
520 fs
->regs
.alloc_regs (reg
+ 1);
521 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_SAVED_OFFSET
;
522 fs
->regs
.reg
[reg
].loc
.offset
= -offset
;
526 if (insn
>= DW_CFA_lo_user
&& insn
<= DW_CFA_hi_user
)
528 /* Handle vendor-specific CFI for different architectures. */
529 if (!gdbarch_execute_dwarf_cfa_vendor_op (gdbarch
, insn
, fs
))
530 error (_("Call Frame Instruction op %d in vendor extension "
531 "space is not handled on this architecture."),
535 internal_error (__FILE__
, __LINE__
,
536 _("Unknown CFI encountered."));
541 if (fs
->initial
.reg
.empty ())
543 /* Don't allow remember/restore between CIE and FDE programs. */
544 delete fs
->regs
.prev
;
545 fs
->regs
.prev
= NULL
;
553 namespace selftests
{
555 /* Unit test to function execute_cfa_program. */
558 execute_cfa_program_test (struct gdbarch
*gdbarch
)
560 struct dwarf2_fde fde
;
561 struct dwarf2_cie cie
;
563 memset (&fde
, 0, sizeof fde
);
564 memset (&cie
, 0, sizeof cie
);
566 cie
.data_alignment_factor
= -4;
567 cie
.code_alignment_factor
= 2;
570 dwarf2_frame_state
fs (0, fde
.cie
);
574 DW_CFA_def_cfa
, 1, 4, /* DW_CFA_def_cfa: r1 ofs 4 */
575 DW_CFA_offset
| 0x2, 1, /* DW_CFA_offset: r2 at cfa-4 */
576 DW_CFA_remember_state
,
577 DW_CFA_restore_state
,
580 const gdb_byte
*insn_end
= insns
+ sizeof (insns
);
581 const gdb_byte
*out
= execute_cfa_program (&fde
, insns
, insn_end
, gdbarch
,
584 SELF_CHECK (out
== insn_end
);
585 SELF_CHECK (fs
.pc
== 0);
587 /* The instructions above only use r1 and r2, but the register numbers
588 used are adjusted by dwarf2_frame_adjust_regnum. */
589 auto r1
= dwarf2_frame_adjust_regnum (gdbarch
, 1, fde
.eh_frame_p
);
590 auto r2
= dwarf2_frame_adjust_regnum (gdbarch
, 2, fde
.eh_frame_p
);
592 SELF_CHECK (fs
.regs
.reg
.size () == (std::max (r1
, r2
) + 1));
594 SELF_CHECK (fs
.regs
.reg
[r2
].how
== DWARF2_FRAME_REG_SAVED_OFFSET
);
595 SELF_CHECK (fs
.regs
.reg
[r2
].loc
.offset
== -4);
597 for (auto i
= 0; i
< fs
.regs
.reg
.size (); i
++)
599 SELF_CHECK (fs
.regs
.reg
[i
].how
== DWARF2_FRAME_REG_UNSPECIFIED
);
601 SELF_CHECK (fs
.regs
.cfa_reg
== 1);
602 SELF_CHECK (fs
.regs
.cfa_offset
== 4);
603 SELF_CHECK (fs
.regs
.cfa_how
== CFA_REG_OFFSET
);
604 SELF_CHECK (fs
.regs
.cfa_exp
== NULL
);
605 SELF_CHECK (fs
.regs
.prev
== NULL
);
608 } // namespace selftests
609 #endif /* GDB_SELF_TEST */
613 /* Architecture-specific operations. */
615 /* Per-architecture data key. */
616 static struct gdbarch_data
*dwarf2_frame_data
;
618 struct dwarf2_frame_ops
620 /* Pre-initialize the register state REG for register REGNUM. */
621 void (*init_reg
) (struct gdbarch
*, int, struct dwarf2_frame_state_reg
*,
622 struct frame_info
*);
624 /* Check whether the THIS_FRAME is a signal trampoline. */
625 int (*signal_frame_p
) (struct gdbarch
*, struct frame_info
*);
627 /* Convert .eh_frame register number to DWARF register number, or
628 adjust .debug_frame register number. */
629 int (*adjust_regnum
) (struct gdbarch
*, int, int);
632 /* Default architecture-specific register state initialization
636 dwarf2_frame_default_init_reg (struct gdbarch
*gdbarch
, int regnum
,
637 struct dwarf2_frame_state_reg
*reg
,
638 struct frame_info
*this_frame
)
640 /* If we have a register that acts as a program counter, mark it as
641 a destination for the return address. If we have a register that
642 serves as the stack pointer, arrange for it to be filled with the
643 call frame address (CFA). The other registers are marked as
646 We copy the return address to the program counter, since many
647 parts in GDB assume that it is possible to get the return address
648 by unwinding the program counter register. However, on ISA's
649 with a dedicated return address register, the CFI usually only
650 contains information to unwind that return address register.
652 The reason we're treating the stack pointer special here is
653 because in many cases GCC doesn't emit CFI for the stack pointer
654 and implicitly assumes that it is equal to the CFA. This makes
655 some sense since the DWARF specification (version 3, draft 8,
658 "Typically, the CFA is defined to be the value of the stack
659 pointer at the call site in the previous frame (which may be
660 different from its value on entry to the current frame)."
662 However, this isn't true for all platforms supported by GCC
663 (e.g. IBM S/390 and zSeries). Those architectures should provide
664 their own architecture-specific initialization function. */
666 if (regnum
== gdbarch_pc_regnum (gdbarch
))
667 reg
->how
= DWARF2_FRAME_REG_RA
;
668 else if (regnum
== gdbarch_sp_regnum (gdbarch
))
669 reg
->how
= DWARF2_FRAME_REG_CFA
;
672 /* Return a default for the architecture-specific operations. */
675 dwarf2_frame_init (struct obstack
*obstack
)
677 struct dwarf2_frame_ops
*ops
;
679 ops
= OBSTACK_ZALLOC (obstack
, struct dwarf2_frame_ops
);
680 ops
->init_reg
= dwarf2_frame_default_init_reg
;
684 /* Set the architecture-specific register state initialization
685 function for GDBARCH to INIT_REG. */
688 dwarf2_frame_set_init_reg (struct gdbarch
*gdbarch
,
689 void (*init_reg
) (struct gdbarch
*, int,
690 struct dwarf2_frame_state_reg
*,
691 struct frame_info
*))
693 struct dwarf2_frame_ops
*ops
694 = (struct dwarf2_frame_ops
*) gdbarch_data (gdbarch
, dwarf2_frame_data
);
696 ops
->init_reg
= init_reg
;
699 /* Pre-initialize the register state REG for register REGNUM. */
702 dwarf2_frame_init_reg (struct gdbarch
*gdbarch
, int regnum
,
703 struct dwarf2_frame_state_reg
*reg
,
704 struct frame_info
*this_frame
)
706 struct dwarf2_frame_ops
*ops
707 = (struct dwarf2_frame_ops
*) gdbarch_data (gdbarch
, dwarf2_frame_data
);
709 ops
->init_reg (gdbarch
, regnum
, reg
, this_frame
);
712 /* Set the architecture-specific signal trampoline recognition
713 function for GDBARCH to SIGNAL_FRAME_P. */
716 dwarf2_frame_set_signal_frame_p (struct gdbarch
*gdbarch
,
717 int (*signal_frame_p
) (struct gdbarch
*,
718 struct frame_info
*))
720 struct dwarf2_frame_ops
*ops
721 = (struct dwarf2_frame_ops
*) gdbarch_data (gdbarch
, dwarf2_frame_data
);
723 ops
->signal_frame_p
= signal_frame_p
;
726 /* Query the architecture-specific signal frame recognizer for
730 dwarf2_frame_signal_frame_p (struct gdbarch
*gdbarch
,
731 struct frame_info
*this_frame
)
733 struct dwarf2_frame_ops
*ops
734 = (struct dwarf2_frame_ops
*) gdbarch_data (gdbarch
, dwarf2_frame_data
);
736 if (ops
->signal_frame_p
== NULL
)
738 return ops
->signal_frame_p (gdbarch
, this_frame
);
741 /* Set the architecture-specific adjustment of .eh_frame and .debug_frame
745 dwarf2_frame_set_adjust_regnum (struct gdbarch
*gdbarch
,
746 int (*adjust_regnum
) (struct gdbarch
*,
749 struct dwarf2_frame_ops
*ops
750 = (struct dwarf2_frame_ops
*) gdbarch_data (gdbarch
, dwarf2_frame_data
);
752 ops
->adjust_regnum
= adjust_regnum
;
755 /* Translate a .eh_frame register to DWARF register, or adjust a .debug_frame
759 dwarf2_frame_adjust_regnum (struct gdbarch
*gdbarch
,
760 int regnum
, int eh_frame_p
)
762 struct dwarf2_frame_ops
*ops
763 = (struct dwarf2_frame_ops
*) gdbarch_data (gdbarch
, dwarf2_frame_data
);
765 if (ops
->adjust_regnum
== NULL
)
767 return ops
->adjust_regnum (gdbarch
, regnum
, eh_frame_p
);
771 dwarf2_frame_find_quirks (struct dwarf2_frame_state
*fs
,
772 struct dwarf2_fde
*fde
)
774 struct compunit_symtab
*cust
;
776 cust
= find_pc_compunit_symtab (fs
->pc
);
780 if (producer_is_realview (COMPUNIT_PRODUCER (cust
)))
782 if (fde
->cie
->version
== 1)
783 fs
->armcc_cfa_offsets_sf
= 1;
785 if (fde
->cie
->version
== 1)
786 fs
->armcc_cfa_offsets_reversed
= 1;
788 /* The reversed offset problem is present in some compilers
789 using DWARF3, but it was eventually fixed. Check the ARM
790 defined augmentations, which are in the format "armcc" followed
791 by a list of one-character options. The "+" option means
792 this problem is fixed (no quirk needed). If the armcc
793 augmentation is missing, the quirk is needed. */
794 if (fde
->cie
->version
== 3
795 && (!startswith (fde
->cie
->augmentation
, "armcc")
796 || strchr (fde
->cie
->augmentation
+ 5, '+') == NULL
))
797 fs
->armcc_cfa_offsets_reversed
= 1;
804 /* See dwarf2-frame.h. */
807 dwarf2_fetch_cfa_info (struct gdbarch
*gdbarch
, CORE_ADDR pc
,
808 struct dwarf2_per_cu_data
*data
,
809 int *regnum_out
, LONGEST
*offset_out
,
810 CORE_ADDR
*text_offset_out
,
811 const gdb_byte
**cfa_start_out
,
812 const gdb_byte
**cfa_end_out
)
814 struct dwarf2_fde
*fde
;
815 dwarf2_per_objfile
*per_objfile
;
818 /* Find the correct FDE. */
819 fde
= dwarf2_frame_find_fde (&pc1
, &per_objfile
);
821 error (_("Could not compute CFA; needed to translate this expression"));
823 gdb_assert (per_objfile
!= nullptr);
825 dwarf2_frame_state
fs (pc1
, fde
->cie
);
827 /* Check for "quirks" - known bugs in producers. */
828 dwarf2_frame_find_quirks (&fs
, fde
);
830 /* First decode all the insns in the CIE. */
831 execute_cfa_program (fde
, fde
->cie
->initial_instructions
,
832 fde
->cie
->end
, gdbarch
, pc
, &fs
,
833 per_objfile
->objfile
->text_section_offset ());
835 /* Save the initialized register set. */
836 fs
.initial
= fs
.regs
;
838 /* Then decode the insns in the FDE up to our target PC. */
839 execute_cfa_program (fde
, fde
->instructions
, fde
->end
, gdbarch
, pc
, &fs
,
840 per_objfile
->objfile
->text_section_offset ());
842 /* Calculate the CFA. */
843 switch (fs
.regs
.cfa_how
)
847 int regnum
= dwarf_reg_to_regnum_or_error (gdbarch
, fs
.regs
.cfa_reg
);
849 *regnum_out
= regnum
;
850 if (fs
.armcc_cfa_offsets_reversed
)
851 *offset_out
= -fs
.regs
.cfa_offset
;
853 *offset_out
= fs
.regs
.cfa_offset
;
858 *text_offset_out
= per_objfile
->objfile
->text_section_offset ();
859 *cfa_start_out
= fs
.regs
.cfa_exp
;
860 *cfa_end_out
= fs
.regs
.cfa_exp
+ fs
.regs
.cfa_exp_len
;
864 internal_error (__FILE__
, __LINE__
, _("Unknown CFA rule."));
869 struct dwarf2_frame_cache
871 /* DWARF Call Frame Address. */
874 /* Set if the return address column was marked as unavailable
875 (required non-collected memory or registers to compute). */
876 int unavailable_retaddr
;
878 /* Set if the return address column was marked as undefined. */
879 int undefined_retaddr
;
881 /* Saved registers, indexed by GDB register number, not by DWARF
883 struct dwarf2_frame_state_reg
*reg
;
885 /* Return address register. */
886 struct dwarf2_frame_state_reg retaddr_reg
;
888 /* Target address size in bytes. */
891 /* The dwarf2_per_objfile from which this frame description came. */
892 dwarf2_per_objfile
*per_objfile
;
894 /* If not NULL then this frame is the bottom frame of a TAILCALL_FRAME
895 sequence. If NULL then it is a normal case with no TAILCALL_FRAME
896 involved. Non-bottom frames of a virtual tail call frames chain use
897 dwarf2_tailcall_frame_unwind unwinder so this field does not apply for
899 void *tailcall_cache
;
902 static struct dwarf2_frame_cache
*
903 dwarf2_frame_cache (struct frame_info
*this_frame
, void **this_cache
)
905 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
906 const int num_regs
= gdbarch_num_cooked_regs (gdbarch
);
907 struct dwarf2_frame_cache
*cache
;
908 struct dwarf2_fde
*fde
;
910 const gdb_byte
*instr
;
913 return (struct dwarf2_frame_cache
*) *this_cache
;
915 /* Allocate a new cache. */
916 cache
= FRAME_OBSTACK_ZALLOC (struct dwarf2_frame_cache
);
917 cache
->reg
= FRAME_OBSTACK_CALLOC (num_regs
, struct dwarf2_frame_state_reg
);
922 Note that if the next frame is never supposed to return (i.e. a call
923 to abort), the compiler might optimize away the instruction at
924 its return address. As a result the return address will
925 point at some random instruction, and the CFI for that
926 instruction is probably worthless to us. GCC's unwinder solves
927 this problem by substracting 1 from the return address to get an
928 address in the middle of a presumed call instruction (or the
929 instruction in the associated delay slot). This should only be
930 done for "normal" frames and not for resume-type frames (signal
931 handlers, sentinel frames, dummy frames). The function
932 get_frame_address_in_block does just this. It's not clear how
933 reliable the method is though; there is the potential for the
934 register state pre-call being different to that on return. */
935 CORE_ADDR pc1
= get_frame_address_in_block (this_frame
);
937 /* Find the correct FDE. */
938 fde
= dwarf2_frame_find_fde (&pc1
, &cache
->per_objfile
);
939 gdb_assert (fde
!= NULL
);
940 gdb_assert (cache
->per_objfile
!= nullptr);
942 /* Allocate and initialize the frame state. */
943 struct dwarf2_frame_state
fs (pc1
, fde
->cie
);
945 cache
->addr_size
= fde
->cie
->addr_size
;
947 /* Check for "quirks" - known bugs in producers. */
948 dwarf2_frame_find_quirks (&fs
, fde
);
950 /* First decode all the insns in the CIE. */
951 execute_cfa_program (fde
, fde
->cie
->initial_instructions
,
952 fde
->cie
->end
, gdbarch
,
953 get_frame_address_in_block (this_frame
), &fs
,
954 cache
->per_objfile
->objfile
->text_section_offset ());
956 /* Save the initialized register set. */
957 fs
.initial
= fs
.regs
;
959 /* Fetching the entry pc for THIS_FRAME won't necessarily result
960 in an address that's within the range of FDE locations. This
961 is due to the possibility of the function occupying non-contiguous
963 LONGEST entry_cfa_sp_offset
;
964 int entry_cfa_sp_offset_p
= 0;
965 if (get_frame_func_if_available (this_frame
, &entry_pc
)
966 && fde
->initial_location
<= entry_pc
967 && entry_pc
< fde
->initial_location
+ fde
->address_range
)
969 /* Decode the insns in the FDE up to the entry PC. */
970 instr
= execute_cfa_program
971 (fde
, fde
->instructions
, fde
->end
, gdbarch
, entry_pc
, &fs
,
972 cache
->per_objfile
->objfile
->text_section_offset ());
974 if (fs
.regs
.cfa_how
== CFA_REG_OFFSET
975 && (dwarf_reg_to_regnum (gdbarch
, fs
.regs
.cfa_reg
)
976 == gdbarch_sp_regnum (gdbarch
)))
978 entry_cfa_sp_offset
= fs
.regs
.cfa_offset
;
979 entry_cfa_sp_offset_p
= 1;
983 instr
= fde
->instructions
;
985 /* Then decode the insns in the FDE up to our target PC. */
986 execute_cfa_program (fde
, instr
, fde
->end
, gdbarch
,
987 get_frame_address_in_block (this_frame
), &fs
,
988 cache
->per_objfile
->objfile
->text_section_offset ());
992 /* Calculate the CFA. */
993 switch (fs
.regs
.cfa_how
)
996 cache
->cfa
= read_addr_from_reg (this_frame
, fs
.regs
.cfa_reg
);
997 if (fs
.armcc_cfa_offsets_reversed
)
998 cache
->cfa
-= fs
.regs
.cfa_offset
;
1000 cache
->cfa
+= fs
.regs
.cfa_offset
;
1005 execute_stack_op (fs
.regs
.cfa_exp
, fs
.regs
.cfa_exp_len
,
1006 cache
->addr_size
, this_frame
, 0, 0,
1007 cache
->per_objfile
);
1011 internal_error (__FILE__
, __LINE__
, _("Unknown CFA rule."));
1014 catch (const gdb_exception_error
&ex
)
1016 if (ex
.error
== NOT_AVAILABLE_ERROR
)
1018 cache
->unavailable_retaddr
= 1;
1025 /* Initialize the register state. */
1029 for (regnum
= 0; regnum
< num_regs
; regnum
++)
1030 dwarf2_frame_init_reg (gdbarch
, regnum
, &cache
->reg
[regnum
], this_frame
);
1033 /* Go through the DWARF2 CFI generated table and save its register
1034 location information in the cache. Note that we don't skip the
1035 return address column; it's perfectly all right for it to
1036 correspond to a real register. */
1038 int column
; /* CFI speak for "register number". */
1040 for (column
= 0; column
< fs
.regs
.reg
.size (); column
++)
1042 /* Use the GDB register number as the destination index. */
1043 int regnum
= dwarf_reg_to_regnum (gdbarch
, column
);
1045 /* Protect against a target returning a bad register. */
1046 if (regnum
< 0 || regnum
>= num_regs
)
1049 /* NOTE: cagney/2003-09-05: CFI should specify the disposition
1050 of all debug info registers. If it doesn't, complain (but
1051 not too loudly). It turns out that GCC assumes that an
1052 unspecified register implies "same value" when CFI (draft
1053 7) specifies nothing at all. Such a register could equally
1054 be interpreted as "undefined". Also note that this check
1055 isn't sufficient; it only checks that all registers in the
1056 range [0 .. max column] are specified, and won't detect
1057 problems when a debug info register falls outside of the
1058 table. We need a way of iterating through all the valid
1059 DWARF2 register numbers. */
1060 if (fs
.regs
.reg
[column
].how
== DWARF2_FRAME_REG_UNSPECIFIED
)
1062 if (cache
->reg
[regnum
].how
== DWARF2_FRAME_REG_UNSPECIFIED
)
1064 incomplete CFI data; unspecified registers (e.g., %s) at %s"),
1065 gdbarch_register_name (gdbarch
, regnum
),
1066 paddress (gdbarch
, fs
.pc
));
1069 cache
->reg
[regnum
] = fs
.regs
.reg
[column
];
1073 /* Eliminate any DWARF2_FRAME_REG_RA rules, and save the information
1074 we need for evaluating DWARF2_FRAME_REG_RA_OFFSET rules. */
1078 for (regnum
= 0; regnum
< num_regs
; regnum
++)
1080 if (cache
->reg
[regnum
].how
== DWARF2_FRAME_REG_RA
1081 || cache
->reg
[regnum
].how
== DWARF2_FRAME_REG_RA_OFFSET
)
1083 const std::vector
<struct dwarf2_frame_state_reg
> ®s
1085 ULONGEST retaddr_column
= fs
.retaddr_column
;
1087 /* It seems rather bizarre to specify an "empty" column as
1088 the return adress column. However, this is exactly
1089 what GCC does on some targets. It turns out that GCC
1090 assumes that the return address can be found in the
1091 register corresponding to the return address column.
1092 Incidentally, that's how we should treat a return
1093 address column specifying "same value" too. */
1094 if (fs
.retaddr_column
< fs
.regs
.reg
.size ()
1095 && regs
[retaddr_column
].how
!= DWARF2_FRAME_REG_UNSPECIFIED
1096 && regs
[retaddr_column
].how
!= DWARF2_FRAME_REG_SAME_VALUE
)
1098 if (cache
->reg
[regnum
].how
== DWARF2_FRAME_REG_RA
)
1099 cache
->reg
[regnum
] = regs
[retaddr_column
];
1101 cache
->retaddr_reg
= regs
[retaddr_column
];
1105 if (cache
->reg
[regnum
].how
== DWARF2_FRAME_REG_RA
)
1107 cache
->reg
[regnum
].loc
.reg
= fs
.retaddr_column
;
1108 cache
->reg
[regnum
].how
= DWARF2_FRAME_REG_SAVED_REG
;
1112 cache
->retaddr_reg
.loc
.reg
= fs
.retaddr_column
;
1113 cache
->retaddr_reg
.how
= DWARF2_FRAME_REG_SAVED_REG
;
1120 if (fs
.retaddr_column
< fs
.regs
.reg
.size ()
1121 && fs
.regs
.reg
[fs
.retaddr_column
].how
== DWARF2_FRAME_REG_UNDEFINED
)
1122 cache
->undefined_retaddr
= 1;
1124 dwarf2_tailcall_sniffer_first (this_frame
, &cache
->tailcall_cache
,
1125 (entry_cfa_sp_offset_p
1126 ? &entry_cfa_sp_offset
: NULL
));
1131 static enum unwind_stop_reason
1132 dwarf2_frame_unwind_stop_reason (struct frame_info
*this_frame
,
1135 struct dwarf2_frame_cache
*cache
1136 = dwarf2_frame_cache (this_frame
, this_cache
);
1138 if (cache
->unavailable_retaddr
)
1139 return UNWIND_UNAVAILABLE
;
1141 if (cache
->undefined_retaddr
)
1142 return UNWIND_OUTERMOST
;
1144 return UNWIND_NO_REASON
;
1148 dwarf2_frame_this_id (struct frame_info
*this_frame
, void **this_cache
,
1149 struct frame_id
*this_id
)
1151 struct dwarf2_frame_cache
*cache
=
1152 dwarf2_frame_cache (this_frame
, this_cache
);
1154 if (cache
->unavailable_retaddr
)
1155 (*this_id
) = frame_id_build_unavailable_stack (get_frame_func (this_frame
));
1156 else if (cache
->undefined_retaddr
)
1159 (*this_id
) = frame_id_build (cache
->cfa
, get_frame_func (this_frame
));
1162 static struct value
*
1163 dwarf2_frame_prev_register (struct frame_info
*this_frame
, void **this_cache
,
1166 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
1167 struct dwarf2_frame_cache
*cache
=
1168 dwarf2_frame_cache (this_frame
, this_cache
);
1172 /* Non-bottom frames of a virtual tail call frames chain use
1173 dwarf2_tailcall_frame_unwind unwinder so this code does not apply for
1174 them. If dwarf2_tailcall_prev_register_first does not have specific value
1175 unwind the register, tail call frames are assumed to have the register set
1176 of the top caller. */
1177 if (cache
->tailcall_cache
)
1181 val
= dwarf2_tailcall_prev_register_first (this_frame
,
1182 &cache
->tailcall_cache
,
1188 switch (cache
->reg
[regnum
].how
)
1190 case DWARF2_FRAME_REG_UNDEFINED
:
1191 /* If CFI explicitly specified that the value isn't defined,
1192 mark it as optimized away; the value isn't available. */
1193 return frame_unwind_got_optimized (this_frame
, regnum
);
1195 case DWARF2_FRAME_REG_SAVED_OFFSET
:
1196 addr
= cache
->cfa
+ cache
->reg
[regnum
].loc
.offset
;
1197 return frame_unwind_got_memory (this_frame
, regnum
, addr
);
1199 case DWARF2_FRAME_REG_SAVED_REG
:
1200 realnum
= dwarf_reg_to_regnum_or_error
1201 (gdbarch
, cache
->reg
[regnum
].loc
.reg
);
1202 return frame_unwind_got_register (this_frame
, regnum
, realnum
);
1204 case DWARF2_FRAME_REG_SAVED_EXP
:
1205 addr
= execute_stack_op (cache
->reg
[regnum
].loc
.exp
.start
,
1206 cache
->reg
[regnum
].loc
.exp
.len
,
1208 this_frame
, cache
->cfa
, 1,
1209 cache
->per_objfile
);
1210 return frame_unwind_got_memory (this_frame
, regnum
, addr
);
1212 case DWARF2_FRAME_REG_SAVED_VAL_OFFSET
:
1213 addr
= cache
->cfa
+ cache
->reg
[regnum
].loc
.offset
;
1214 return frame_unwind_got_constant (this_frame
, regnum
, addr
);
1216 case DWARF2_FRAME_REG_SAVED_VAL_EXP
:
1217 addr
= execute_stack_op (cache
->reg
[regnum
].loc
.exp
.start
,
1218 cache
->reg
[regnum
].loc
.exp
.len
,
1220 this_frame
, cache
->cfa
, 1,
1221 cache
->per_objfile
);
1222 return frame_unwind_got_constant (this_frame
, regnum
, addr
);
1224 case DWARF2_FRAME_REG_UNSPECIFIED
:
1225 /* GCC, in its infinite wisdom decided to not provide unwind
1226 information for registers that are "same value". Since
1227 DWARF2 (3 draft 7) doesn't define such behavior, said
1228 registers are actually undefined (which is different to CFI
1229 "undefined"). Code above issues a complaint about this.
1230 Here just fudge the books, assume GCC, and that the value is
1231 more inner on the stack. */
1232 return frame_unwind_got_register (this_frame
, regnum
, regnum
);
1234 case DWARF2_FRAME_REG_SAME_VALUE
:
1235 return frame_unwind_got_register (this_frame
, regnum
, regnum
);
1237 case DWARF2_FRAME_REG_CFA
:
1238 return frame_unwind_got_address (this_frame
, regnum
, cache
->cfa
);
1240 case DWARF2_FRAME_REG_CFA_OFFSET
:
1241 addr
= cache
->cfa
+ cache
->reg
[regnum
].loc
.offset
;
1242 return frame_unwind_got_address (this_frame
, regnum
, addr
);
1244 case DWARF2_FRAME_REG_RA_OFFSET
:
1245 addr
= cache
->reg
[regnum
].loc
.offset
;
1246 regnum
= dwarf_reg_to_regnum_or_error
1247 (gdbarch
, cache
->retaddr_reg
.loc
.reg
);
1248 addr
+= get_frame_register_unsigned (this_frame
, regnum
);
1249 return frame_unwind_got_address (this_frame
, regnum
, addr
);
1251 case DWARF2_FRAME_REG_FN
:
1252 return cache
->reg
[regnum
].loc
.fn (this_frame
, this_cache
, regnum
);
1255 internal_error (__FILE__
, __LINE__
, _("Unknown register rule."));
1259 /* Proxy for tailcall_frame_dealloc_cache for bottom frame of a virtual tail
1260 call frames chain. */
1263 dwarf2_frame_dealloc_cache (struct frame_info
*self
, void *this_cache
)
1265 struct dwarf2_frame_cache
*cache
= dwarf2_frame_cache (self
, &this_cache
);
1267 if (cache
->tailcall_cache
)
1268 dwarf2_tailcall_frame_unwind
.dealloc_cache (self
, cache
->tailcall_cache
);
1272 dwarf2_frame_sniffer (const struct frame_unwind
*self
,
1273 struct frame_info
*this_frame
, void **this_cache
)
1275 if (!dwarf2_frame_unwinders_enabled_p
)
1278 /* Grab an address that is guaranteed to reside somewhere within the
1279 function. get_frame_pc(), with a no-return next function, can
1280 end up returning something past the end of this function's body.
1281 If the frame we're sniffing for is a signal frame whose start
1282 address is placed on the stack by the OS, its FDE must
1283 extend one byte before its start address or we could potentially
1284 select the FDE of the previous function. */
1285 CORE_ADDR block_addr
= get_frame_address_in_block (this_frame
);
1286 struct dwarf2_fde
*fde
= dwarf2_frame_find_fde (&block_addr
, NULL
);
1291 /* On some targets, signal trampolines may have unwind information.
1292 We need to recognize them so that we set the frame type
1295 if (fde
->cie
->signal_frame
1296 || dwarf2_frame_signal_frame_p (get_frame_arch (this_frame
),
1298 return self
->type
== SIGTRAMP_FRAME
;
1300 if (self
->type
!= NORMAL_FRAME
)
1306 static const struct frame_unwind dwarf2_frame_unwind
=
1310 dwarf2_frame_unwind_stop_reason
,
1311 dwarf2_frame_this_id
,
1312 dwarf2_frame_prev_register
,
1314 dwarf2_frame_sniffer
,
1315 dwarf2_frame_dealloc_cache
1318 static const struct frame_unwind dwarf2_signal_frame_unwind
=
1322 dwarf2_frame_unwind_stop_reason
,
1323 dwarf2_frame_this_id
,
1324 dwarf2_frame_prev_register
,
1326 dwarf2_frame_sniffer
,
1328 /* TAILCALL_CACHE can never be in such frame to need dealloc_cache. */
1332 /* Append the DWARF-2 frame unwinders to GDBARCH's list. */
1335 dwarf2_append_unwinders (struct gdbarch
*gdbarch
)
1337 frame_unwind_append_unwinder (gdbarch
, &dwarf2_frame_unwind
);
1338 frame_unwind_append_unwinder (gdbarch
, &dwarf2_signal_frame_unwind
);
1342 /* There is no explicitly defined relationship between the CFA and the
1343 location of frame's local variables and arguments/parameters.
1344 Therefore, frame base methods on this page should probably only be
1345 used as a last resort, just to avoid printing total garbage as a
1346 response to the "info frame" command. */
1349 dwarf2_frame_base_address (struct frame_info
*this_frame
, void **this_cache
)
1351 struct dwarf2_frame_cache
*cache
=
1352 dwarf2_frame_cache (this_frame
, this_cache
);
1357 static const struct frame_base dwarf2_frame_base
=
1359 &dwarf2_frame_unwind
,
1360 dwarf2_frame_base_address
,
1361 dwarf2_frame_base_address
,
1362 dwarf2_frame_base_address
1365 const struct frame_base
*
1366 dwarf2_frame_base_sniffer (struct frame_info
*this_frame
)
1368 CORE_ADDR block_addr
= get_frame_address_in_block (this_frame
);
1370 if (dwarf2_frame_find_fde (&block_addr
, NULL
))
1371 return &dwarf2_frame_base
;
1376 /* Compute the CFA for THIS_FRAME, but only if THIS_FRAME came from
1377 the DWARF unwinder. This is used to implement
1378 DW_OP_call_frame_cfa. */
1381 dwarf2_frame_cfa (struct frame_info
*this_frame
)
1383 if (frame_unwinder_is (this_frame
, &record_btrace_tailcall_frame_unwind
)
1384 || frame_unwinder_is (this_frame
, &record_btrace_frame_unwind
))
1385 throw_error (NOT_AVAILABLE_ERROR
,
1386 _("cfa not available for record btrace target"));
1388 while (get_frame_type (this_frame
) == INLINE_FRAME
)
1389 this_frame
= get_prev_frame (this_frame
);
1390 if (get_frame_unwind_stop_reason (this_frame
) == UNWIND_UNAVAILABLE
)
1391 throw_error (NOT_AVAILABLE_ERROR
,
1392 _("can't compute CFA for this frame: "
1393 "required registers or memory are unavailable"));
1395 if (get_frame_id (this_frame
).stack_status
!= FID_STACK_VALID
)
1396 throw_error (NOT_AVAILABLE_ERROR
,
1397 _("can't compute CFA for this frame: "
1398 "frame base not available"));
1400 return get_frame_base (this_frame
);
1403 /* We store the frame data on the BFD. This is only done if it is
1404 independent of the address space and so can be shared. */
1405 static const struct bfd_key
<comp_unit
> dwarf2_frame_bfd_data
;
1407 /* If any BFD sections require relocations (note; really should be if
1408 any debug info requires relocations), then we store the frame data
1409 on the objfile instead, and do not share it. */
1410 const struct objfile_key
<comp_unit
> dwarf2_frame_objfile_data
;
1413 /* Pointer encoding helper functions. */
1415 /* GCC supports exception handling based on DWARF2 CFI. However, for
1416 technical reasons, it encodes addresses in its FDE's in a different
1417 way. Several "pointer encodings" are supported. The encoding
1418 that's used for a particular FDE is determined by the 'R'
1419 augmentation in the associated CIE. The argument of this
1420 augmentation is a single byte.
1422 The address can be encoded as 2 bytes, 4 bytes, 8 bytes, or as a
1423 LEB128. This is encoded in bits 0, 1 and 2. Bit 3 encodes whether
1424 the address is signed or unsigned. Bits 4, 5 and 6 encode how the
1425 address should be interpreted (absolute, relative to the current
1426 position in the FDE, ...). Bit 7, indicates that the address
1427 should be dereferenced. */
1430 encoding_for_size (unsigned int size
)
1435 return DW_EH_PE_udata2
;
1437 return DW_EH_PE_udata4
;
1439 return DW_EH_PE_udata8
;
1441 internal_error (__FILE__
, __LINE__
, _("Unsupported address size"));
1446 read_encoded_value (struct comp_unit
*unit
, gdb_byte encoding
,
1447 int ptr_len
, const gdb_byte
*buf
,
1448 unsigned int *bytes_read_ptr
,
1449 CORE_ADDR func_base
)
1454 /* GCC currently doesn't generate DW_EH_PE_indirect encodings for
1456 if (encoding
& DW_EH_PE_indirect
)
1457 internal_error (__FILE__
, __LINE__
,
1458 _("Unsupported encoding: DW_EH_PE_indirect"));
1460 *bytes_read_ptr
= 0;
1462 switch (encoding
& 0x70)
1464 case DW_EH_PE_absptr
:
1467 case DW_EH_PE_pcrel
:
1468 base
= bfd_section_vma (unit
->dwarf_frame_section
);
1469 base
+= (buf
- unit
->dwarf_frame_buffer
);
1471 case DW_EH_PE_datarel
:
1474 case DW_EH_PE_textrel
:
1477 case DW_EH_PE_funcrel
:
1480 case DW_EH_PE_aligned
:
1482 offset
= buf
- unit
->dwarf_frame_buffer
;
1483 if ((offset
% ptr_len
) != 0)
1485 *bytes_read_ptr
= ptr_len
- (offset
% ptr_len
);
1486 buf
+= *bytes_read_ptr
;
1490 internal_error (__FILE__
, __LINE__
,
1491 _("Invalid or unsupported encoding"));
1494 if ((encoding
& 0x07) == 0x00)
1496 encoding
|= encoding_for_size (ptr_len
);
1497 if (bfd_get_sign_extend_vma (unit
->abfd
))
1498 encoding
|= DW_EH_PE_signed
;
1501 switch (encoding
& 0x0f)
1503 case DW_EH_PE_uleb128
:
1506 const gdb_byte
*end_buf
= buf
+ (sizeof (value
) + 1) * 8 / 7;
1508 *bytes_read_ptr
+= safe_read_uleb128 (buf
, end_buf
, &value
) - buf
;
1509 return base
+ value
;
1511 case DW_EH_PE_udata2
:
1512 *bytes_read_ptr
+= 2;
1513 return (base
+ bfd_get_16 (unit
->abfd
, (bfd_byte
*) buf
));
1514 case DW_EH_PE_udata4
:
1515 *bytes_read_ptr
+= 4;
1516 return (base
+ bfd_get_32 (unit
->abfd
, (bfd_byte
*) buf
));
1517 case DW_EH_PE_udata8
:
1518 *bytes_read_ptr
+= 8;
1519 return (base
+ bfd_get_64 (unit
->abfd
, (bfd_byte
*) buf
));
1520 case DW_EH_PE_sleb128
:
1523 const gdb_byte
*end_buf
= buf
+ (sizeof (value
) + 1) * 8 / 7;
1525 *bytes_read_ptr
+= safe_read_sleb128 (buf
, end_buf
, &value
) - buf
;
1526 return base
+ value
;
1528 case DW_EH_PE_sdata2
:
1529 *bytes_read_ptr
+= 2;
1530 return (base
+ bfd_get_signed_16 (unit
->abfd
, (bfd_byte
*) buf
));
1531 case DW_EH_PE_sdata4
:
1532 *bytes_read_ptr
+= 4;
1533 return (base
+ bfd_get_signed_32 (unit
->abfd
, (bfd_byte
*) buf
));
1534 case DW_EH_PE_sdata8
:
1535 *bytes_read_ptr
+= 8;
1536 return (base
+ bfd_get_signed_64 (unit
->abfd
, (bfd_byte
*) buf
));
1538 internal_error (__FILE__
, __LINE__
,
1539 _("Invalid or unsupported encoding"));
1544 /* Find CIE with the given CIE_POINTER in CIE_TABLE. */
1545 static struct dwarf2_cie
*
1546 find_cie (const dwarf2_cie_table
&cie_table
, ULONGEST cie_pointer
)
1548 auto iter
= cie_table
.find (cie_pointer
);
1549 if (iter
!= cie_table
.end ())
1550 return iter
->second
;
1555 bsearch_fde_cmp (const dwarf2_fde
*fde
, CORE_ADDR seek_pc
)
1557 if (fde
->initial_location
+ fde
->address_range
<= seek_pc
)
1559 if (fde
->initial_location
<= seek_pc
)
1564 /* Find an existing comp_unit for an objfile, if any. */
1567 find_comp_unit (struct objfile
*objfile
)
1569 bfd
*abfd
= objfile
->obfd
;
1570 if (gdb_bfd_requires_relocations (abfd
))
1571 return dwarf2_frame_objfile_data
.get (objfile
);
1573 return dwarf2_frame_bfd_data
.get (abfd
);
1576 /* Store the comp_unit on OBJFILE, or the corresponding BFD, as
1580 set_comp_unit (struct objfile
*objfile
, struct comp_unit
*unit
)
1582 bfd
*abfd
= objfile
->obfd
;
1583 if (gdb_bfd_requires_relocations (abfd
))
1584 return dwarf2_frame_objfile_data
.set (objfile
, unit
);
1586 return dwarf2_frame_bfd_data
.set (abfd
, unit
);
1589 /* Find the FDE for *PC. Return a pointer to the FDE, and store the
1590 initial location associated with it into *PC. */
1592 static struct dwarf2_fde
*
1593 dwarf2_frame_find_fde (CORE_ADDR
*pc
, dwarf2_per_objfile
**out_per_objfile
)
1595 for (objfile
*objfile
: current_program_space
->objfiles ())
1600 comp_unit
*unit
= find_comp_unit (objfile
);
1603 dwarf2_build_frame_info (objfile
);
1604 unit
= find_comp_unit (objfile
);
1606 gdb_assert (unit
!= NULL
);
1608 dwarf2_fde_table
*fde_table
= &unit
->fde_table
;
1609 if (fde_table
->empty ())
1612 gdb_assert (!objfile
->section_offsets
.empty ());
1613 offset
= objfile
->text_section_offset ();
1615 gdb_assert (!fde_table
->empty ());
1616 if (*pc
< offset
+ (*fde_table
)[0]->initial_location
)
1619 seek_pc
= *pc
- offset
;
1620 auto it
= gdb::binary_search (fde_table
->begin (), fde_table
->end (),
1621 seek_pc
, bsearch_fde_cmp
);
1622 if (it
!= fde_table
->end ())
1624 *pc
= (*it
)->initial_location
+ offset
;
1625 if (out_per_objfile
!= nullptr)
1626 *out_per_objfile
= get_dwarf2_per_objfile (objfile
);
1634 /* Add FDE to FDE_TABLE. */
1636 add_fde (dwarf2_fde_table
*fde_table
, struct dwarf2_fde
*fde
)
1638 if (fde
->address_range
== 0)
1639 /* Discard useless FDEs. */
1642 fde_table
->push_back (fde
);
1645 #define DW64_CIE_ID 0xffffffffffffffffULL
1647 /* Defines the type of eh_frames that are expected to be decoded: CIE, FDE
1652 EH_CIE_TYPE_ID
= 1 << 0,
1653 EH_FDE_TYPE_ID
= 1 << 1,
1654 EH_CIE_OR_FDE_TYPE_ID
= EH_CIE_TYPE_ID
| EH_FDE_TYPE_ID
1657 static const gdb_byte
*decode_frame_entry (struct gdbarch
*gdbarch
,
1658 struct comp_unit
*unit
,
1659 const gdb_byte
*start
,
1661 dwarf2_cie_table
&cie_table
,
1662 dwarf2_fde_table
*fde_table
,
1663 enum eh_frame_type entry_type
);
1665 /* Decode the next CIE or FDE, entry_type specifies the expected type.
1666 Return NULL if invalid input, otherwise the next byte to be processed. */
1668 static const gdb_byte
*
1669 decode_frame_entry_1 (struct gdbarch
*gdbarch
,
1670 struct comp_unit
*unit
, const gdb_byte
*start
,
1672 dwarf2_cie_table
&cie_table
,
1673 dwarf2_fde_table
*fde_table
,
1674 enum eh_frame_type entry_type
)
1676 const gdb_byte
*buf
, *end
;
1678 unsigned int bytes_read
;
1681 ULONGEST cie_pointer
;
1686 length
= read_initial_length (unit
->abfd
, buf
, &bytes_read
, false);
1688 end
= buf
+ (size_t) length
;
1693 /* Are we still within the section? */
1694 if (end
<= buf
|| end
> unit
->dwarf_frame_buffer
+ unit
->dwarf_frame_size
)
1697 /* Distinguish between 32 and 64-bit encoded frame info. */
1698 dwarf64_p
= (bytes_read
== 12);
1700 /* In a .eh_frame section, zero is used to distinguish CIEs from FDEs. */
1704 cie_id
= DW64_CIE_ID
;
1710 cie_pointer
= read_8_bytes (unit
->abfd
, buf
);
1715 cie_pointer
= read_4_bytes (unit
->abfd
, buf
);
1719 if (cie_pointer
== cie_id
)
1721 /* This is a CIE. */
1722 struct dwarf2_cie
*cie
;
1724 unsigned int cie_version
;
1726 /* Check that a CIE was expected. */
1727 if ((entry_type
& EH_CIE_TYPE_ID
) == 0)
1728 error (_("Found a CIE when not expecting it."));
1730 /* Record the offset into the .debug_frame section of this CIE. */
1731 cie_pointer
= start
- unit
->dwarf_frame_buffer
;
1733 /* Check whether we've already read it. */
1734 if (find_cie (cie_table
, cie_pointer
))
1737 cie
= XOBNEW (&unit
->obstack
, struct dwarf2_cie
);
1738 cie
->initial_instructions
= NULL
;
1739 cie
->cie_pointer
= cie_pointer
;
1741 /* The encoding for FDE's in a normal .debug_frame section
1742 depends on the target address size. */
1743 cie
->encoding
= DW_EH_PE_absptr
;
1745 /* We'll determine the final value later, but we need to
1746 initialize it conservatively. */
1747 cie
->signal_frame
= 0;
1749 /* Check version number. */
1750 cie_version
= read_1_byte (unit
->abfd
, buf
);
1751 if (cie_version
!= 1 && cie_version
!= 3 && cie_version
!= 4)
1753 cie
->version
= cie_version
;
1756 /* Interpret the interesting bits of the augmentation. */
1757 cie
->augmentation
= augmentation
= (char *) buf
;
1758 buf
+= (strlen (augmentation
) + 1);
1760 /* Ignore armcc augmentations. We only use them for quirks,
1761 and that doesn't happen until later. */
1762 if (startswith (augmentation
, "armcc"))
1763 augmentation
+= strlen (augmentation
);
1765 /* The GCC 2.x "eh" augmentation has a pointer immediately
1766 following the augmentation string, so it must be handled
1768 if (augmentation
[0] == 'e' && augmentation
[1] == 'h')
1771 buf
+= gdbarch_ptr_bit (gdbarch
) / TARGET_CHAR_BIT
;
1775 if (cie
->version
>= 4)
1777 /* FIXME: check that this is the same as from the CU header. */
1778 cie
->addr_size
= read_1_byte (unit
->abfd
, buf
);
1780 cie
->segment_size
= read_1_byte (unit
->abfd
, buf
);
1785 cie
->addr_size
= gdbarch_dwarf2_addr_size (gdbarch
);
1786 cie
->segment_size
= 0;
1788 /* Address values in .eh_frame sections are defined to have the
1789 target's pointer size. Watchout: This breaks frame info for
1790 targets with pointer size < address size, unless a .debug_frame
1791 section exists as well. */
1793 cie
->ptr_size
= gdbarch_ptr_bit (gdbarch
) / TARGET_CHAR_BIT
;
1795 cie
->ptr_size
= cie
->addr_size
;
1797 buf
= gdb_read_uleb128 (buf
, end
, &uleb128
);
1800 cie
->code_alignment_factor
= uleb128
;
1802 buf
= gdb_read_sleb128 (buf
, end
, &sleb128
);
1805 cie
->data_alignment_factor
= sleb128
;
1807 if (cie_version
== 1)
1809 cie
->return_address_register
= read_1_byte (unit
->abfd
, buf
);
1814 buf
= gdb_read_uleb128 (buf
, end
, &uleb128
);
1817 cie
->return_address_register
= uleb128
;
1820 cie
->return_address_register
1821 = dwarf2_frame_adjust_regnum (gdbarch
,
1822 cie
->return_address_register
,
1825 cie
->saw_z_augmentation
= (*augmentation
== 'z');
1826 if (cie
->saw_z_augmentation
)
1828 uint64_t uleb_length
;
1830 buf
= gdb_read_uleb128 (buf
, end
, &uleb_length
);
1833 cie
->initial_instructions
= buf
+ uleb_length
;
1837 while (*augmentation
)
1839 /* "L" indicates a byte showing how the LSDA pointer is encoded. */
1840 if (*augmentation
== 'L')
1847 /* "R" indicates a byte indicating how FDE addresses are encoded. */
1848 else if (*augmentation
== 'R')
1850 cie
->encoding
= *buf
++;
1854 /* "P" indicates a personality routine in the CIE augmentation. */
1855 else if (*augmentation
== 'P')
1857 /* Skip. Avoid indirection since we throw away the result. */
1858 gdb_byte encoding
= (*buf
++) & ~DW_EH_PE_indirect
;
1859 read_encoded_value (unit
, encoding
, cie
->ptr_size
,
1860 buf
, &bytes_read
, 0);
1865 /* "S" indicates a signal frame, such that the return
1866 address must not be decremented to locate the call frame
1867 info for the previous frame; it might even be the first
1868 instruction of a function, so decrementing it would take
1869 us to a different function. */
1870 else if (*augmentation
== 'S')
1872 cie
->signal_frame
= 1;
1876 /* Otherwise we have an unknown augmentation. Assume that either
1877 there is no augmentation data, or we saw a 'z' prefix. */
1880 if (cie
->initial_instructions
)
1881 buf
= cie
->initial_instructions
;
1886 cie
->initial_instructions
= buf
;
1890 cie_table
[cie
->cie_pointer
] = cie
;
1894 /* This is a FDE. */
1895 struct dwarf2_fde
*fde
;
1898 /* Check that an FDE was expected. */
1899 if ((entry_type
& EH_FDE_TYPE_ID
) == 0)
1900 error (_("Found an FDE when not expecting it."));
1902 /* In an .eh_frame section, the CIE pointer is the delta between the
1903 address within the FDE where the CIE pointer is stored and the
1904 address of the CIE. Convert it to an offset into the .eh_frame
1908 cie_pointer
= buf
- unit
->dwarf_frame_buffer
- cie_pointer
;
1909 cie_pointer
-= (dwarf64_p
? 8 : 4);
1912 /* In either case, validate the result is still within the section. */
1913 if (cie_pointer
>= unit
->dwarf_frame_size
)
1916 fde
= XOBNEW (&unit
->obstack
, struct dwarf2_fde
);
1917 fde
->cie
= find_cie (cie_table
, cie_pointer
);
1918 if (fde
->cie
== NULL
)
1920 decode_frame_entry (gdbarch
, unit
,
1921 unit
->dwarf_frame_buffer
+ cie_pointer
,
1922 eh_frame_p
, cie_table
, fde_table
,
1924 fde
->cie
= find_cie (cie_table
, cie_pointer
);
1927 gdb_assert (fde
->cie
!= NULL
);
1929 addr
= read_encoded_value (unit
, fde
->cie
->encoding
, fde
->cie
->ptr_size
,
1930 buf
, &bytes_read
, 0);
1931 fde
->initial_location
= gdbarch_adjust_dwarf2_addr (gdbarch
, addr
);
1934 fde
->address_range
=
1935 read_encoded_value (unit
, fde
->cie
->encoding
& 0x0f,
1936 fde
->cie
->ptr_size
, buf
, &bytes_read
, 0);
1937 addr
= gdbarch_adjust_dwarf2_addr (gdbarch
, addr
+ fde
->address_range
);
1938 fde
->address_range
= addr
- fde
->initial_location
;
1941 /* A 'z' augmentation in the CIE implies the presence of an
1942 augmentation field in the FDE as well. The only thing known
1943 to be in here at present is the LSDA entry for EH. So we
1944 can skip the whole thing. */
1945 if (fde
->cie
->saw_z_augmentation
)
1947 uint64_t uleb_length
;
1949 buf
= gdb_read_uleb128 (buf
, end
, &uleb_length
);
1957 fde
->instructions
= buf
;
1960 fde
->eh_frame_p
= eh_frame_p
;
1962 add_fde (fde_table
, fde
);
1968 /* Read a CIE or FDE in BUF and decode it. Entry_type specifies whether we
1969 expect an FDE or a CIE. */
1971 static const gdb_byte
*
1972 decode_frame_entry (struct gdbarch
*gdbarch
,
1973 struct comp_unit
*unit
, const gdb_byte
*start
,
1975 dwarf2_cie_table
&cie_table
,
1976 dwarf2_fde_table
*fde_table
,
1977 enum eh_frame_type entry_type
)
1979 enum { NONE
, ALIGN4
, ALIGN8
, FAIL
} workaround
= NONE
;
1980 const gdb_byte
*ret
;
1981 ptrdiff_t start_offset
;
1985 ret
= decode_frame_entry_1 (gdbarch
, unit
, start
, eh_frame_p
,
1986 cie_table
, fde_table
, entry_type
);
1990 /* We have corrupt input data of some form. */
1992 /* ??? Try, weakly, to work around compiler/assembler/linker bugs
1993 and mismatches wrt padding and alignment of debug sections. */
1994 /* Note that there is no requirement in the standard for any
1995 alignment at all in the frame unwind sections. Testing for
1996 alignment before trying to interpret data would be incorrect.
1998 However, GCC traditionally arranged for frame sections to be
1999 sized such that the FDE length and CIE fields happen to be
2000 aligned (in theory, for performance). This, unfortunately,
2001 was done with .align directives, which had the side effect of
2002 forcing the section to be aligned by the linker.
2004 This becomes a problem when you have some other producer that
2005 creates frame sections that are not as strictly aligned. That
2006 produces a hole in the frame info that gets filled by the
2009 The GCC behaviour is arguably a bug, but it's effectively now
2010 part of the ABI, so we're now stuck with it, at least at the
2011 object file level. A smart linker may decide, in the process
2012 of compressing duplicate CIE information, that it can rewrite
2013 the entire output section without this extra padding. */
2015 start_offset
= start
- unit
->dwarf_frame_buffer
;
2016 if (workaround
< ALIGN4
&& (start_offset
& 3) != 0)
2018 start
+= 4 - (start_offset
& 3);
2019 workaround
= ALIGN4
;
2022 if (workaround
< ALIGN8
&& (start_offset
& 7) != 0)
2024 start
+= 8 - (start_offset
& 7);
2025 workaround
= ALIGN8
;
2029 /* Nothing left to try. Arrange to return as if we've consumed
2030 the entire input section. Hopefully we'll get valid info from
2031 the other of .debug_frame/.eh_frame. */
2033 ret
= unit
->dwarf_frame_buffer
+ unit
->dwarf_frame_size
;
2044 Corrupt data in %s:%s; align 4 workaround apparently succeeded"),
2045 bfd_get_filename (unit
->dwarf_frame_section
->owner
),
2046 bfd_section_name (unit
->dwarf_frame_section
));
2051 Corrupt data in %s:%s; align 8 workaround apparently succeeded"),
2052 bfd_get_filename (unit
->dwarf_frame_section
->owner
),
2053 bfd_section_name (unit
->dwarf_frame_section
));
2057 complaint (_("Corrupt data in %s:%s"),
2058 bfd_get_filename (unit
->dwarf_frame_section
->owner
),
2059 bfd_section_name (unit
->dwarf_frame_section
));
2067 fde_is_less_than (const dwarf2_fde
*aa
, const dwarf2_fde
*bb
)
2069 if (aa
->initial_location
== bb
->initial_location
)
2071 if (aa
->address_range
!= bb
->address_range
2072 && aa
->eh_frame_p
== 0 && bb
->eh_frame_p
== 0)
2073 /* Linker bug, e.g. gold/10400.
2074 Work around it by keeping stable sort order. */
2077 /* Put eh_frame entries after debug_frame ones. */
2078 return aa
->eh_frame_p
< bb
->eh_frame_p
;
2081 return aa
->initial_location
< bb
->initial_location
;
2085 dwarf2_build_frame_info (struct objfile
*objfile
)
2087 const gdb_byte
*frame_ptr
;
2088 dwarf2_cie_table cie_table
;
2089 dwarf2_fde_table fde_table
;
2091 struct gdbarch
*gdbarch
= objfile
->arch ();
2093 /* Build a minimal decoding of the DWARF2 compilation unit. */
2094 std::unique_ptr
<comp_unit
> unit (new comp_unit (objfile
));
2096 if (objfile
->separate_debug_objfile_backlink
== NULL
)
2098 /* Do not read .eh_frame from separate file as they must be also
2099 present in the main file. */
2100 dwarf2_get_section_info (objfile
, DWARF2_EH_FRAME
,
2101 &unit
->dwarf_frame_section
,
2102 &unit
->dwarf_frame_buffer
,
2103 &unit
->dwarf_frame_size
);
2104 if (unit
->dwarf_frame_size
)
2106 asection
*got
, *txt
;
2108 /* FIXME: kettenis/20030602: This is the DW_EH_PE_datarel base
2109 that is used for the i386/amd64 target, which currently is
2110 the only target in GCC that supports/uses the
2111 DW_EH_PE_datarel encoding. */
2112 got
= bfd_get_section_by_name (unit
->abfd
, ".got");
2114 unit
->dbase
= got
->vma
;
2116 /* GCC emits the DW_EH_PE_textrel encoding type on sh and ia64
2118 txt
= bfd_get_section_by_name (unit
->abfd
, ".text");
2120 unit
->tbase
= txt
->vma
;
2124 frame_ptr
= unit
->dwarf_frame_buffer
;
2125 while (frame_ptr
< unit
->dwarf_frame_buffer
+ unit
->dwarf_frame_size
)
2126 frame_ptr
= decode_frame_entry (gdbarch
, unit
.get (),
2128 cie_table
, &fde_table
,
2129 EH_CIE_OR_FDE_TYPE_ID
);
2132 catch (const gdb_exception_error
&e
)
2134 warning (_("skipping .eh_frame info of %s: %s"),
2135 objfile_name (objfile
), e
.what ());
2138 /* The cie_table is discarded below. */
2145 dwarf2_get_section_info (objfile
, DWARF2_DEBUG_FRAME
,
2146 &unit
->dwarf_frame_section
,
2147 &unit
->dwarf_frame_buffer
,
2148 &unit
->dwarf_frame_size
);
2149 if (unit
->dwarf_frame_size
)
2151 size_t num_old_fde_entries
= fde_table
.size ();
2155 frame_ptr
= unit
->dwarf_frame_buffer
;
2156 while (frame_ptr
< unit
->dwarf_frame_buffer
+ unit
->dwarf_frame_size
)
2157 frame_ptr
= decode_frame_entry (gdbarch
, unit
.get (), frame_ptr
, 0,
2158 cie_table
, &fde_table
,
2159 EH_CIE_OR_FDE_TYPE_ID
);
2161 catch (const gdb_exception_error
&e
)
2163 warning (_("skipping .debug_frame info of %s: %s"),
2164 objfile_name (objfile
), e
.what ());
2166 fde_table
.resize (num_old_fde_entries
);
2170 struct dwarf2_fde
*fde_prev
= NULL
;
2171 struct dwarf2_fde
*first_non_zero_fde
= NULL
;
2173 /* Prepare FDE table for lookups. */
2174 std::sort (fde_table
.begin (), fde_table
.end (), fde_is_less_than
);
2176 /* Check for leftovers from --gc-sections. The GNU linker sets
2177 the relevant symbols to zero, but doesn't zero the FDE *end*
2178 ranges because there's no relocation there. It's (offset,
2179 length), not (start, end). On targets where address zero is
2180 just another valid address this can be a problem, since the
2181 FDEs appear to be non-empty in the output --- we could pick
2182 out the wrong FDE. To work around this, when overlaps are
2183 detected, we prefer FDEs that do not start at zero.
2185 Start by finding the first FDE with non-zero start. Below
2186 we'll discard all FDEs that start at zero and overlap this
2188 for (struct dwarf2_fde
*fde
: fde_table
)
2190 if (fde
->initial_location
!= 0)
2192 first_non_zero_fde
= fde
;
2197 /* Since we'll be doing bsearch, squeeze out identical (except
2198 for eh_frame_p) fde entries so bsearch result is predictable.
2199 Also discard leftovers from --gc-sections. */
2200 for (struct dwarf2_fde
*fde
: fde_table
)
2202 if (fde
->initial_location
== 0
2203 && first_non_zero_fde
!= NULL
2204 && (first_non_zero_fde
->initial_location
2205 < fde
->initial_location
+ fde
->address_range
))
2208 if (fde_prev
!= NULL
2209 && fde_prev
->initial_location
== fde
->initial_location
)
2212 unit
->fde_table
.push_back (fde
);
2215 unit
->fde_table
.shrink_to_fit ();
2217 set_comp_unit (objfile
, unit
.release ());
2220 /* Handle 'maintenance show dwarf unwinders'. */
2223 show_dwarf_unwinders_enabled_p (struct ui_file
*file
, int from_tty
,
2224 struct cmd_list_element
*c
,
2227 fprintf_filtered (file
,
2228 _("The DWARF stack unwinders are currently %s.\n"),
2232 void _initialize_dwarf2_frame ();
2234 _initialize_dwarf2_frame ()
2236 dwarf2_frame_data
= gdbarch_data_register_pre_init (dwarf2_frame_init
);
2238 add_setshow_boolean_cmd ("unwinders", class_obscure
,
2239 &dwarf2_frame_unwinders_enabled_p
, _("\
2240 Set whether the DWARF stack frame unwinders are used."), _("\
2241 Show whether the DWARF stack frame unwinders are used."), _("\
2242 When enabled the DWARF stack frame unwinders can be used for architectures\n\
2243 that support the DWARF unwinders. Enabling the DWARF unwinders for an\n\
2244 architecture that doesn't support them will have no effect."),
2246 show_dwarf_unwinders_enabled_p
,
2248 &show_dwarf_cmdlist
);
2251 selftests::register_test_foreach_arch ("execute_cfa_program",
2252 selftests::execute_cfa_program_test
);