1 /* Frame unwinder for frames with DWARF Call Frame Information.
3 Copyright (C) 2003-2023 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. */
83 const char *augmentation
;
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 /* Return the final location in this FDE. */
116 unrelocated_addr
end_addr () const
118 return (unrelocated_addr
) ((ULONGEST
) initial_location
122 /* CIE for this FDE. */
123 struct dwarf2_cie
*cie
;
125 /* First location associated with this FDE. */
126 unrelocated_addr initial_location
;
128 /* Number of bytes of program instructions described by this FDE. */
129 ULONGEST address_range
;
131 /* Instruction sequence. */
132 const gdb_byte
*instructions
;
135 /* True if this FDE is read from a .eh_frame instead of a .debug_frame
137 unsigned char eh_frame_p
;
140 typedef std::vector
<dwarf2_fde
*> dwarf2_fde_table
;
142 /* A minimal decoding of DWARF2 compilation units. We only decode
143 what's needed to get to the call frame information. */
147 comp_unit (struct objfile
*objf
)
148 : abfd (objf
->obfd
.get ())
152 /* Keep the bfd convenient. */
155 /* Pointer to the .debug_frame section loaded into memory. */
156 const gdb_byte
*dwarf_frame_buffer
= nullptr;
158 /* Length of the loaded .debug_frame section. */
159 bfd_size_type dwarf_frame_size
= 0;
161 /* Pointer to the .debug_frame section. */
162 asection
*dwarf_frame_section
= nullptr;
164 /* Base for DW_EH_PE_datarel encodings. */
167 /* Base for DW_EH_PE_textrel encodings. */
171 dwarf2_fde_table fde_table
;
173 /* Hold data used by this module. */
174 auto_obstack obstack
;
177 static struct dwarf2_fde
*dwarf2_frame_find_fde
178 (CORE_ADDR
*pc
, dwarf2_per_objfile
**out_per_objfile
);
180 static int dwarf2_frame_adjust_regnum (struct gdbarch
*gdbarch
, int regnum
,
183 static ULONGEST
read_encoded_value (struct comp_unit
*unit
, gdb_byte encoding
,
184 int ptr_len
, const gdb_byte
*buf
,
185 unsigned int *bytes_read_ptr
,
186 unrelocated_addr func_base
);
189 /* See dwarf2/frame.h. */
190 bool dwarf2_frame_unwinders_enabled_p
= true;
192 /* Store the length the expression for the CFA in the `cfa_reg' field,
193 which is unused in that case. */
194 #define cfa_exp_len cfa_reg
196 dwarf2_frame_state::dwarf2_frame_state (CORE_ADDR pc_
, struct dwarf2_cie
*cie
)
197 : pc (pc_
), data_align (cie
->data_alignment_factor
),
198 code_align (cie
->code_alignment_factor
),
199 retaddr_column (cie
->return_address_register
)
203 /* Execute the required actions for both the DW_CFA_restore and
204 DW_CFA_restore_extended instructions. */
206 dwarf2_restore_rule (struct gdbarch
*gdbarch
, ULONGEST reg_num
,
207 struct dwarf2_frame_state
*fs
, int eh_frame_p
)
211 reg
= dwarf2_frame_adjust_regnum (gdbarch
, reg_num
, eh_frame_p
);
212 fs
->regs
.alloc_regs (reg
+ 1);
214 /* Check if this register was explicitly initialized in the
215 CIE initial instructions. If not, default the rule to
217 if (reg
< fs
->initial
.reg
.size ())
218 fs
->regs
.reg
[reg
] = fs
->initial
.reg
[reg
];
220 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_UNSPECIFIED
;
222 if (fs
->regs
.reg
[reg
].how
== DWARF2_FRAME_REG_UNSPECIFIED
)
224 int regnum
= dwarf_reg_to_regnum (gdbarch
, reg
);
227 incomplete CFI data; DW_CFA_restore unspecified\n\
228 register %s (#%d) at %s"),
229 gdbarch_register_name (gdbarch
, regnum
), regnum
,
230 paddress (gdbarch
, fs
->pc
));
235 execute_stack_op (const gdb_byte
*exp
, ULONGEST len
, int addr_size
,
236 frame_info_ptr this_frame
, CORE_ADDR initial
,
237 int initial_in_stack_memory
, dwarf2_per_objfile
*per_objfile
)
239 dwarf_expr_context
ctx (per_objfile
, addr_size
);
240 scoped_value_mark free_values
;
242 ctx
.push_address (initial
, initial_in_stack_memory
);
243 value
*result_val
= ctx
.evaluate (exp
, len
, true, nullptr, this_frame
);
245 if (result_val
->lval () == lval_memory
)
246 return result_val
->address ();
248 return value_as_address (result_val
);
252 /* Execute FDE program from INSN_PTR possibly up to INSN_END or up to inferior
253 PC. Modify FS state accordingly. Return current INSN_PTR where the
254 execution has stopped, one can resume it on the next call. */
256 static const gdb_byte
*
257 execute_cfa_program (struct dwarf2_fde
*fde
, const gdb_byte
*insn_ptr
,
258 const gdb_byte
*insn_end
, struct gdbarch
*gdbarch
,
259 CORE_ADDR pc
, struct dwarf2_frame_state
*fs
,
260 CORE_ADDR text_offset
)
262 int eh_frame_p
= fde
->eh_frame_p
;
263 unsigned int bytes_read
;
264 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
266 while (insn_ptr
< insn_end
&& fs
->pc
<= pc
)
268 gdb_byte insn
= *insn_ptr
++;
272 if ((insn
& 0xc0) == DW_CFA_advance_loc
)
273 fs
->pc
+= (insn
& 0x3f) * fs
->code_align
;
274 else if ((insn
& 0xc0) == DW_CFA_offset
)
277 reg
= dwarf2_frame_adjust_regnum (gdbarch
, reg
, eh_frame_p
);
278 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, &utmp
);
279 offset
= utmp
* fs
->data_align
;
280 fs
->regs
.alloc_regs (reg
+ 1);
281 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_SAVED_OFFSET
;
282 fs
->regs
.reg
[reg
].loc
.offset
= offset
;
284 else if ((insn
& 0xc0) == DW_CFA_restore
)
287 dwarf2_restore_rule (gdbarch
, reg
, fs
, eh_frame_p
);
294 fs
->pc
= read_encoded_value (fde
->cie
->unit
, fde
->cie
->encoding
,
295 fde
->cie
->ptr_size
, insn_ptr
,
296 &bytes_read
, fde
->initial_location
);
297 /* Apply the text offset for relocatable objects. */
298 fs
->pc
+= text_offset
;
299 insn_ptr
+= bytes_read
;
302 case DW_CFA_advance_loc1
:
303 utmp
= extract_unsigned_integer (insn_ptr
, 1, byte_order
);
304 fs
->pc
+= utmp
* fs
->code_align
;
307 case DW_CFA_advance_loc2
:
308 utmp
= extract_unsigned_integer (insn_ptr
, 2, byte_order
);
309 fs
->pc
+= utmp
* fs
->code_align
;
312 case DW_CFA_advance_loc4
:
313 utmp
= extract_unsigned_integer (insn_ptr
, 4, byte_order
);
314 fs
->pc
+= utmp
* fs
->code_align
;
318 case DW_CFA_offset_extended
:
319 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, ®
);
320 reg
= dwarf2_frame_adjust_regnum (gdbarch
, reg
, eh_frame_p
);
321 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, &utmp
);
322 offset
= utmp
* fs
->data_align
;
323 fs
->regs
.alloc_regs (reg
+ 1);
324 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_SAVED_OFFSET
;
325 fs
->regs
.reg
[reg
].loc
.offset
= offset
;
328 case DW_CFA_restore_extended
:
329 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, ®
);
330 dwarf2_restore_rule (gdbarch
, reg
, fs
, eh_frame_p
);
333 case DW_CFA_undefined
:
334 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, ®
);
335 reg
= dwarf2_frame_adjust_regnum (gdbarch
, reg
, eh_frame_p
);
336 fs
->regs
.alloc_regs (reg
+ 1);
337 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_UNDEFINED
;
340 case DW_CFA_same_value
:
341 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, ®
);
342 reg
= dwarf2_frame_adjust_regnum (gdbarch
, reg
, eh_frame_p
);
343 fs
->regs
.alloc_regs (reg
+ 1);
344 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_SAME_VALUE
;
347 case DW_CFA_register
:
348 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, ®
);
349 reg
= dwarf2_frame_adjust_regnum (gdbarch
, reg
, eh_frame_p
);
350 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, &utmp
);
351 utmp
= dwarf2_frame_adjust_regnum (gdbarch
, utmp
, eh_frame_p
);
352 fs
->regs
.alloc_regs (reg
+ 1);
353 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_SAVED_REG
;
354 fs
->regs
.reg
[reg
].loc
.reg
= utmp
;
357 case DW_CFA_remember_state
:
359 struct dwarf2_frame_state_reg_info
*new_rs
;
361 new_rs
= new dwarf2_frame_state_reg_info (fs
->regs
);
362 fs
->regs
.prev
= new_rs
;
366 case DW_CFA_restore_state
:
368 struct dwarf2_frame_state_reg_info
*old_rs
= fs
->regs
.prev
;
373 bad CFI data; mismatched DW_CFA_restore_state at %s"),
374 paddress (gdbarch
, fs
->pc
));
377 fs
->regs
= std::move (*old_rs
);
382 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, ®
);
383 fs
->regs
.cfa_reg
= reg
;
384 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, &utmp
);
386 if (fs
->armcc_cfa_offsets_sf
)
387 utmp
*= fs
->data_align
;
389 fs
->regs
.cfa_offset
= utmp
;
390 fs
->regs
.cfa_how
= CFA_REG_OFFSET
;
393 case DW_CFA_def_cfa_register
:
394 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, ®
);
395 fs
->regs
.cfa_reg
= dwarf2_frame_adjust_regnum (gdbarch
, reg
,
397 fs
->regs
.cfa_how
= CFA_REG_OFFSET
;
400 case DW_CFA_def_cfa_offset
:
401 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, &utmp
);
403 if (fs
->armcc_cfa_offsets_sf
)
404 utmp
*= fs
->data_align
;
406 fs
->regs
.cfa_offset
= utmp
;
407 /* cfa_how deliberately not set. */
413 case DW_CFA_def_cfa_expression
:
414 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, &utmp
);
415 fs
->regs
.cfa_exp_len
= utmp
;
416 fs
->regs
.cfa_exp
= insn_ptr
;
417 fs
->regs
.cfa_how
= CFA_EXP
;
418 insn_ptr
+= fs
->regs
.cfa_exp_len
;
421 case DW_CFA_expression
:
422 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, ®
);
423 reg
= dwarf2_frame_adjust_regnum (gdbarch
, reg
, eh_frame_p
);
424 fs
->regs
.alloc_regs (reg
+ 1);
425 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, &utmp
);
426 fs
->regs
.reg
[reg
].loc
.exp
.start
= insn_ptr
;
427 fs
->regs
.reg
[reg
].loc
.exp
.len
= utmp
;
428 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_SAVED_EXP
;
432 case DW_CFA_offset_extended_sf
:
433 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, ®
);
434 reg
= dwarf2_frame_adjust_regnum (gdbarch
, reg
, eh_frame_p
);
435 insn_ptr
= safe_read_sleb128 (insn_ptr
, insn_end
, &offset
);
436 offset
*= fs
->data_align
;
437 fs
->regs
.alloc_regs (reg
+ 1);
438 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_SAVED_OFFSET
;
439 fs
->regs
.reg
[reg
].loc
.offset
= offset
;
442 case DW_CFA_val_offset
:
443 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, ®
);
444 fs
->regs
.alloc_regs (reg
+ 1);
445 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, &utmp
);
446 offset
= utmp
* fs
->data_align
;
447 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_SAVED_VAL_OFFSET
;
448 fs
->regs
.reg
[reg
].loc
.offset
= offset
;
451 case DW_CFA_val_offset_sf
:
452 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, ®
);
453 fs
->regs
.alloc_regs (reg
+ 1);
454 insn_ptr
= safe_read_sleb128 (insn_ptr
, insn_end
, &offset
);
455 offset
*= fs
->data_align
;
456 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_SAVED_VAL_OFFSET
;
457 fs
->regs
.reg
[reg
].loc
.offset
= offset
;
460 case DW_CFA_val_expression
:
461 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, ®
);
462 fs
->regs
.alloc_regs (reg
+ 1);
463 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, &utmp
);
464 fs
->regs
.reg
[reg
].loc
.exp
.start
= insn_ptr
;
465 fs
->regs
.reg
[reg
].loc
.exp
.len
= utmp
;
466 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_SAVED_VAL_EXP
;
470 case DW_CFA_def_cfa_sf
:
471 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, ®
);
472 fs
->regs
.cfa_reg
= dwarf2_frame_adjust_regnum (gdbarch
, reg
,
474 insn_ptr
= safe_read_sleb128 (insn_ptr
, insn_end
, &offset
);
475 fs
->regs
.cfa_offset
= offset
* fs
->data_align
;
476 fs
->regs
.cfa_how
= CFA_REG_OFFSET
;
479 case DW_CFA_def_cfa_offset_sf
:
480 insn_ptr
= safe_read_sleb128 (insn_ptr
, insn_end
, &offset
);
481 fs
->regs
.cfa_offset
= offset
* fs
->data_align
;
482 /* cfa_how deliberately not set. */
485 case DW_CFA_GNU_args_size
:
487 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, &utmp
);
490 case DW_CFA_GNU_negative_offset_extended
:
491 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, ®
);
492 reg
= dwarf2_frame_adjust_regnum (gdbarch
, reg
, eh_frame_p
);
493 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, &utmp
);
494 offset
= utmp
* fs
->data_align
;
495 fs
->regs
.alloc_regs (reg
+ 1);
496 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_SAVED_OFFSET
;
497 fs
->regs
.reg
[reg
].loc
.offset
= -offset
;
501 if (insn
>= DW_CFA_lo_user
&& insn
<= DW_CFA_hi_user
)
503 /* Handle vendor-specific CFI for different architectures. */
504 if (!gdbarch_execute_dwarf_cfa_vendor_op (gdbarch
, insn
, fs
))
505 error (_("Call Frame Instruction op %d in vendor extension "
506 "space is not handled on this architecture."),
510 internal_error (_("Unknown CFI encountered."));
515 if (fs
->initial
.reg
.empty ())
517 /* Don't allow remember/restore between CIE and FDE programs. */
518 delete fs
->regs
.prev
;
519 fs
->regs
.prev
= NULL
;
527 namespace selftests
{
529 /* Unit test to function execute_cfa_program. */
532 execute_cfa_program_test (struct gdbarch
*gdbarch
)
534 struct dwarf2_fde fde
;
535 struct dwarf2_cie cie
;
537 memset (&fde
, 0, sizeof fde
);
538 memset (&cie
, 0, sizeof cie
);
540 cie
.data_alignment_factor
= -4;
541 cie
.code_alignment_factor
= 2;
544 dwarf2_frame_state
fs (0, fde
.cie
);
548 DW_CFA_def_cfa
, 1, 4, /* DW_CFA_def_cfa: r1 ofs 4 */
549 DW_CFA_offset
| 0x2, 1, /* DW_CFA_offset: r2 at cfa-4 */
550 DW_CFA_remember_state
,
551 DW_CFA_restore_state
,
554 const gdb_byte
*insn_end
= insns
+ sizeof (insns
);
555 const gdb_byte
*out
= execute_cfa_program (&fde
, insns
, insn_end
, gdbarch
,
558 SELF_CHECK (out
== insn_end
);
559 SELF_CHECK (fs
.pc
== 0);
561 /* The instructions above only use r1 and r2, but the register numbers
562 used are adjusted by dwarf2_frame_adjust_regnum. */
563 auto r1
= dwarf2_frame_adjust_regnum (gdbarch
, 1, fde
.eh_frame_p
);
564 auto r2
= dwarf2_frame_adjust_regnum (gdbarch
, 2, fde
.eh_frame_p
);
566 SELF_CHECK (fs
.regs
.reg
.size () == (std::max (r1
, r2
) + 1));
568 SELF_CHECK (fs
.regs
.reg
[r2
].how
== DWARF2_FRAME_REG_SAVED_OFFSET
);
569 SELF_CHECK (fs
.regs
.reg
[r2
].loc
.offset
== -4);
571 for (auto i
= 0; i
< fs
.regs
.reg
.size (); i
++)
573 SELF_CHECK (fs
.regs
.reg
[i
].how
== DWARF2_FRAME_REG_UNSPECIFIED
);
575 SELF_CHECK (fs
.regs
.cfa_reg
== 1);
576 SELF_CHECK (fs
.regs
.cfa_offset
== 4);
577 SELF_CHECK (fs
.regs
.cfa_how
== CFA_REG_OFFSET
);
578 SELF_CHECK (fs
.regs
.cfa_exp
== NULL
);
579 SELF_CHECK (fs
.regs
.prev
== NULL
);
582 } // namespace selftests
583 #endif /* GDB_SELF_TEST */
587 /* Architecture-specific operations. */
589 static void dwarf2_frame_default_init_reg (struct gdbarch
*gdbarch
,
591 struct dwarf2_frame_state_reg
*reg
,
592 frame_info_ptr this_frame
);
594 struct dwarf2_frame_ops
596 /* Pre-initialize the register state REG for register REGNUM. */
597 void (*init_reg
) (struct gdbarch
*, int, struct dwarf2_frame_state_reg
*,
599 = dwarf2_frame_default_init_reg
;
601 /* Check whether the THIS_FRAME is a signal trampoline. */
602 int (*signal_frame_p
) (struct gdbarch
*, frame_info_ptr
) = nullptr;
604 /* Convert .eh_frame register number to DWARF register number, or
605 adjust .debug_frame register number. */
606 int (*adjust_regnum
) (struct gdbarch
*, int, int) = nullptr;
609 /* Per-architecture data key. */
610 static const registry
<gdbarch
>::key
<dwarf2_frame_ops
> dwarf2_frame_data
;
612 /* Get or initialize the frame ops. */
613 static dwarf2_frame_ops
*
614 get_frame_ops (struct gdbarch
*gdbarch
)
616 dwarf2_frame_ops
*result
= dwarf2_frame_data
.get (gdbarch
);
617 if (result
== nullptr)
618 result
= dwarf2_frame_data
.emplace (gdbarch
);
622 /* Default architecture-specific register state initialization
626 dwarf2_frame_default_init_reg (struct gdbarch
*gdbarch
, int regnum
,
627 struct dwarf2_frame_state_reg
*reg
,
628 frame_info_ptr this_frame
)
630 /* If we have a register that acts as a program counter, mark it as
631 a destination for the return address. If we have a register that
632 serves as the stack pointer, arrange for it to be filled with the
633 call frame address (CFA). The other registers are marked as
636 We copy the return address to the program counter, since many
637 parts in GDB assume that it is possible to get the return address
638 by unwinding the program counter register. However, on ISA's
639 with a dedicated return address register, the CFI usually only
640 contains information to unwind that return address register.
642 The reason we're treating the stack pointer special here is
643 because in many cases GCC doesn't emit CFI for the stack pointer
644 and implicitly assumes that it is equal to the CFA. This makes
645 some sense since the DWARF specification (version 3, draft 8,
648 "Typically, the CFA is defined to be the value of the stack
649 pointer at the call site in the previous frame (which may be
650 different from its value on entry to the current frame)."
652 However, this isn't true for all platforms supported by GCC
653 (e.g. IBM S/390 and zSeries). Those architectures should provide
654 their own architecture-specific initialization function. */
656 if (regnum
== gdbarch_pc_regnum (gdbarch
))
657 reg
->how
= DWARF2_FRAME_REG_RA
;
658 else if (regnum
== gdbarch_sp_regnum (gdbarch
))
659 reg
->how
= DWARF2_FRAME_REG_CFA
;
662 /* Set the architecture-specific register state initialization
663 function for GDBARCH to INIT_REG. */
666 dwarf2_frame_set_init_reg (struct gdbarch
*gdbarch
,
667 void (*init_reg
) (struct gdbarch
*, int,
668 struct dwarf2_frame_state_reg
*,
671 struct dwarf2_frame_ops
*ops
= get_frame_ops (gdbarch
);
673 ops
->init_reg
= init_reg
;
676 /* Pre-initialize the register state REG for register REGNUM. */
679 dwarf2_frame_init_reg (struct gdbarch
*gdbarch
, int regnum
,
680 struct dwarf2_frame_state_reg
*reg
,
681 frame_info_ptr this_frame
)
683 struct dwarf2_frame_ops
*ops
= get_frame_ops (gdbarch
);
685 ops
->init_reg (gdbarch
, regnum
, reg
, this_frame
);
688 /* Set the architecture-specific signal trampoline recognition
689 function for GDBARCH to SIGNAL_FRAME_P. */
692 dwarf2_frame_set_signal_frame_p (struct gdbarch
*gdbarch
,
693 int (*signal_frame_p
) (struct gdbarch
*,
696 struct dwarf2_frame_ops
*ops
= get_frame_ops (gdbarch
);
698 ops
->signal_frame_p
= signal_frame_p
;
701 /* Query the architecture-specific signal frame recognizer for
705 dwarf2_frame_signal_frame_p (struct gdbarch
*gdbarch
,
706 frame_info_ptr this_frame
)
708 struct dwarf2_frame_ops
*ops
= get_frame_ops (gdbarch
);
710 if (ops
->signal_frame_p
== NULL
)
712 return ops
->signal_frame_p (gdbarch
, this_frame
);
715 /* Set the architecture-specific adjustment of .eh_frame and .debug_frame
719 dwarf2_frame_set_adjust_regnum (struct gdbarch
*gdbarch
,
720 int (*adjust_regnum
) (struct gdbarch
*,
723 struct dwarf2_frame_ops
*ops
= get_frame_ops (gdbarch
);
725 ops
->adjust_regnum
= adjust_regnum
;
728 /* Translate a .eh_frame register to DWARF register, or adjust a .debug_frame
732 dwarf2_frame_adjust_regnum (struct gdbarch
*gdbarch
,
733 int regnum
, int eh_frame_p
)
735 struct dwarf2_frame_ops
*ops
= get_frame_ops (gdbarch
);
737 if (ops
->adjust_regnum
== NULL
)
739 return ops
->adjust_regnum (gdbarch
, regnum
, eh_frame_p
);
743 dwarf2_frame_find_quirks (struct dwarf2_frame_state
*fs
,
744 struct dwarf2_fde
*fde
)
746 struct compunit_symtab
*cust
;
748 cust
= find_pc_compunit_symtab (fs
->pc
);
752 if (producer_is_realview (cust
->producer ()))
754 if (fde
->cie
->version
== 1)
755 fs
->armcc_cfa_offsets_sf
= 1;
757 if (fde
->cie
->version
== 1)
758 fs
->armcc_cfa_offsets_reversed
= 1;
760 /* The reversed offset problem is present in some compilers
761 using DWARF3, but it was eventually fixed. Check the ARM
762 defined augmentations, which are in the format "armcc" followed
763 by a list of one-character options. The "+" option means
764 this problem is fixed (no quirk needed). If the armcc
765 augmentation is missing, the quirk is needed. */
766 if (fde
->cie
->version
== 3
767 && (!startswith (fde
->cie
->augmentation
, "armcc")
768 || strchr (fde
->cie
->augmentation
+ 5, '+') == NULL
))
769 fs
->armcc_cfa_offsets_reversed
= 1;
776 /* See dwarf2/frame.h. */
779 dwarf2_fetch_cfa_info (struct gdbarch
*gdbarch
, CORE_ADDR pc
,
780 struct dwarf2_per_cu_data
*data
,
781 int *regnum_out
, LONGEST
*offset_out
,
782 CORE_ADDR
*text_offset_out
,
783 const gdb_byte
**cfa_start_out
,
784 const gdb_byte
**cfa_end_out
)
786 struct dwarf2_fde
*fde
;
787 dwarf2_per_objfile
*per_objfile
;
790 /* Find the correct FDE. */
791 fde
= dwarf2_frame_find_fde (&pc1
, &per_objfile
);
793 error (_("Could not compute CFA; needed to translate this expression"));
795 gdb_assert (per_objfile
!= nullptr);
797 dwarf2_frame_state
fs (pc1
, fde
->cie
);
799 /* Check for "quirks" - known bugs in producers. */
800 dwarf2_frame_find_quirks (&fs
, fde
);
802 /* First decode all the insns in the CIE. */
803 execute_cfa_program (fde
, fde
->cie
->initial_instructions
,
804 fde
->cie
->end
, gdbarch
, pc
, &fs
,
805 per_objfile
->objfile
->text_section_offset ());
807 /* Save the initialized register set. */
808 fs
.initial
= fs
.regs
;
810 /* Then decode the insns in the FDE up to our target PC. */
811 execute_cfa_program (fde
, fde
->instructions
, fde
->end
, gdbarch
, pc
, &fs
,
812 per_objfile
->objfile
->text_section_offset ());
814 /* Calculate the CFA. */
815 switch (fs
.regs
.cfa_how
)
819 int regnum
= dwarf_reg_to_regnum_or_error (gdbarch
, fs
.regs
.cfa_reg
);
821 *regnum_out
= regnum
;
822 if (fs
.armcc_cfa_offsets_reversed
)
823 *offset_out
= -fs
.regs
.cfa_offset
;
825 *offset_out
= fs
.regs
.cfa_offset
;
830 *text_offset_out
= per_objfile
->objfile
->text_section_offset ();
831 *cfa_start_out
= fs
.regs
.cfa_exp
;
832 *cfa_end_out
= fs
.regs
.cfa_exp
+ fs
.regs
.cfa_exp_len
;
836 internal_error (_("Unknown CFA rule."));
841 /* Custom function data object for architecture specific prev_register
842 implementation. Main purpose of this object is to allow caching of
843 expensive data lookups in the prev_register handling. */
845 struct dwarf2_frame_fn_data
847 /* The cookie to identify the custom function data by. */
848 fn_prev_register cookie
;
850 /* The custom function data. */
853 /* Pointer to the next custom function data object for this frame. */
854 struct dwarf2_frame_fn_data
*next
;
857 struct dwarf2_frame_cache
859 /* DWARF Call Frame Address. */
862 /* Set if the return address column was marked as unavailable
863 (required non-collected memory or registers to compute). */
864 int unavailable_retaddr
;
866 /* Set if the return address column was marked as undefined. */
867 int undefined_retaddr
;
869 /* Saved registers, indexed by GDB register number, not by DWARF
871 struct dwarf2_frame_state_reg
*reg
;
873 /* Return address register. */
874 struct dwarf2_frame_state_reg retaddr_reg
;
876 /* Target address size in bytes. */
879 /* The dwarf2_per_objfile from which this frame description came. */
880 dwarf2_per_objfile
*per_objfile
;
882 /* If not NULL then this frame is the bottom frame of a TAILCALL_FRAME
883 sequence. If NULL then it is a normal case with no TAILCALL_FRAME
884 involved. Non-bottom frames of a virtual tail call frames chain use
885 dwarf2_tailcall_frame_unwind unwinder so this field does not apply for
887 void *tailcall_cache
;
889 struct dwarf2_frame_fn_data
*fn_data
;
892 static struct dwarf2_frame_cache
*
893 dwarf2_frame_cache (frame_info_ptr this_frame
, void **this_cache
)
895 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
896 const int num_regs
= gdbarch_num_cooked_regs (gdbarch
);
897 struct dwarf2_frame_cache
*cache
;
898 struct dwarf2_fde
*fde
;
900 const gdb_byte
*instr
;
903 return (struct dwarf2_frame_cache
*) *this_cache
;
905 /* Allocate a new cache. */
906 cache
= FRAME_OBSTACK_ZALLOC (struct dwarf2_frame_cache
);
907 cache
->reg
= FRAME_OBSTACK_CALLOC (num_regs
, struct dwarf2_frame_state_reg
);
912 Note that if the next frame is never supposed to return (i.e. a call
913 to abort), the compiler might optimize away the instruction at
914 its return address. As a result the return address will
915 point at some random instruction, and the CFI for that
916 instruction is probably worthless to us. GCC's unwinder solves
917 this problem by substracting 1 from the return address to get an
918 address in the middle of a presumed call instruction (or the
919 instruction in the associated delay slot). This should only be
920 done for "normal" frames and not for resume-type frames (signal
921 handlers, sentinel frames, dummy frames). The function
922 get_frame_address_in_block does just this. It's not clear how
923 reliable the method is though; there is the potential for the
924 register state pre-call being different to that on return. */
925 CORE_ADDR pc1
= get_frame_address_in_block (this_frame
);
927 /* Find the correct FDE. */
928 fde
= dwarf2_frame_find_fde (&pc1
, &cache
->per_objfile
);
929 gdb_assert (fde
!= NULL
);
930 gdb_assert (cache
->per_objfile
!= nullptr);
932 CORE_ADDR text_offset
= cache
->per_objfile
->objfile
->text_section_offset ();
934 /* Allocate and initialize the frame state. */
935 struct dwarf2_frame_state
fs (pc1
, fde
->cie
);
937 cache
->addr_size
= fde
->cie
->addr_size
;
939 /* Check for "quirks" - known bugs in producers. */
940 dwarf2_frame_find_quirks (&fs
, fde
);
942 /* First decode all the insns in the CIE. */
943 execute_cfa_program (fde
, fde
->cie
->initial_instructions
,
944 fde
->cie
->end
, gdbarch
,
945 get_frame_address_in_block (this_frame
), &fs
,
948 /* Save the initialized register set. */
949 fs
.initial
= fs
.regs
;
951 /* Fetching the entry pc for THIS_FRAME won't necessarily result
952 in an address that's within the range of FDE locations. This
953 is due to the possibility of the function occupying non-contiguous
955 LONGEST entry_cfa_sp_offset
;
956 int entry_cfa_sp_offset_p
= 0;
957 if (get_frame_func_if_available (this_frame
, &entry_pc
)
958 && fde
->initial_location
<= (unrelocated_addr
) (entry_pc
- text_offset
)
959 && (unrelocated_addr
) (entry_pc
- text_offset
) < fde
->end_addr ())
961 /* Decode the insns in the FDE up to the entry PC. */
962 instr
= execute_cfa_program (fde
, fde
->instructions
, fde
->end
, gdbarch
,
963 entry_pc
, &fs
, text_offset
);
965 if (fs
.regs
.cfa_how
== CFA_REG_OFFSET
966 && (dwarf_reg_to_regnum (gdbarch
, fs
.regs
.cfa_reg
)
967 == gdbarch_sp_regnum (gdbarch
)))
969 entry_cfa_sp_offset
= fs
.regs
.cfa_offset
;
970 entry_cfa_sp_offset_p
= 1;
974 instr
= fde
->instructions
;
976 /* Then decode the insns in the FDE up to our target PC. */
977 execute_cfa_program (fde
, instr
, fde
->end
, gdbarch
,
978 get_frame_address_in_block (this_frame
), &fs
,
983 /* Calculate the CFA. */
984 switch (fs
.regs
.cfa_how
)
987 cache
->cfa
= read_addr_from_reg (this_frame
, fs
.regs
.cfa_reg
);
988 if (fs
.armcc_cfa_offsets_reversed
)
989 cache
->cfa
-= fs
.regs
.cfa_offset
;
991 cache
->cfa
+= fs
.regs
.cfa_offset
;
996 execute_stack_op (fs
.regs
.cfa_exp
, fs
.regs
.cfa_exp_len
,
997 cache
->addr_size
, this_frame
, 0, 0,
1002 internal_error (_("Unknown CFA rule."));
1005 catch (const gdb_exception_error
&ex
)
1007 if (ex
.error
== NOT_AVAILABLE_ERROR
)
1009 cache
->unavailable_retaddr
= 1;
1016 /* Initialize the register state. */
1020 for (regnum
= 0; regnum
< num_regs
; regnum
++)
1021 dwarf2_frame_init_reg (gdbarch
, regnum
, &cache
->reg
[regnum
], this_frame
);
1024 /* Go through the DWARF2 CFI generated table and save its register
1025 location information in the cache. Note that we don't skip the
1026 return address column; it's perfectly all right for it to
1027 correspond to a real register. */
1029 int column
; /* CFI speak for "register number". */
1031 for (column
= 0; column
< fs
.regs
.reg
.size (); column
++)
1033 /* Use the GDB register number as the destination index. */
1034 int regnum
= dwarf_reg_to_regnum (gdbarch
, column
);
1036 /* Protect against a target returning a bad register. */
1037 if (regnum
< 0 || regnum
>= num_regs
)
1040 /* NOTE: cagney/2003-09-05: CFI should specify the disposition
1041 of all debug info registers. If it doesn't, complain (but
1042 not too loudly). It turns out that GCC assumes that an
1043 unspecified register implies "same value" when CFI (draft
1044 7) specifies nothing at all. Such a register could equally
1045 be interpreted as "undefined". Also note that this check
1046 isn't sufficient; it only checks that all registers in the
1047 range [0 .. max column] are specified, and won't detect
1048 problems when a debug info register falls outside of the
1049 table. We need a way of iterating through all the valid
1050 DWARF2 register numbers. */
1051 if (fs
.regs
.reg
[column
].how
== DWARF2_FRAME_REG_UNSPECIFIED
)
1053 if (cache
->reg
[regnum
].how
== DWARF2_FRAME_REG_UNSPECIFIED
)
1055 incomplete CFI data; unspecified registers (e.g., %s) at %s"),
1056 gdbarch_register_name (gdbarch
, regnum
),
1057 paddress (gdbarch
, fs
.pc
));
1060 cache
->reg
[regnum
] = fs
.regs
.reg
[column
];
1064 /* Eliminate any DWARF2_FRAME_REG_RA rules, and save the information
1065 we need for evaluating DWARF2_FRAME_REG_RA_OFFSET rules. */
1069 for (regnum
= 0; regnum
< num_regs
; regnum
++)
1071 if (cache
->reg
[regnum
].how
== DWARF2_FRAME_REG_RA
1072 || cache
->reg
[regnum
].how
== DWARF2_FRAME_REG_RA_OFFSET
)
1074 const std::vector
<struct dwarf2_frame_state_reg
> ®s
1076 ULONGEST retaddr_column
= fs
.retaddr_column
;
1078 /* It seems rather bizarre to specify an "empty" column as
1079 the return adress column. However, this is exactly
1080 what GCC does on some targets. It turns out that GCC
1081 assumes that the return address can be found in the
1082 register corresponding to the return address column.
1083 Incidentally, that's how we should treat a return
1084 address column specifying "same value" too. */
1085 if (fs
.retaddr_column
< fs
.regs
.reg
.size ()
1086 && regs
[retaddr_column
].how
!= DWARF2_FRAME_REG_UNSPECIFIED
1087 && regs
[retaddr_column
].how
!= DWARF2_FRAME_REG_SAME_VALUE
)
1089 if (cache
->reg
[regnum
].how
== DWARF2_FRAME_REG_RA
)
1090 cache
->reg
[regnum
] = regs
[retaddr_column
];
1092 cache
->retaddr_reg
= regs
[retaddr_column
];
1096 if (cache
->reg
[regnum
].how
== DWARF2_FRAME_REG_RA
)
1098 cache
->reg
[regnum
].loc
.reg
= fs
.retaddr_column
;
1099 cache
->reg
[regnum
].how
= DWARF2_FRAME_REG_SAVED_REG
;
1103 cache
->retaddr_reg
.loc
.reg
= fs
.retaddr_column
;
1104 cache
->retaddr_reg
.how
= DWARF2_FRAME_REG_SAVED_REG
;
1111 if (fs
.retaddr_column
< fs
.regs
.reg
.size ()
1112 && fs
.regs
.reg
[fs
.retaddr_column
].how
== DWARF2_FRAME_REG_UNDEFINED
)
1113 cache
->undefined_retaddr
= 1;
1115 dwarf2_tailcall_sniffer_first (this_frame
, &cache
->tailcall_cache
,
1116 (entry_cfa_sp_offset_p
1117 ? &entry_cfa_sp_offset
: NULL
));
1122 static enum unwind_stop_reason
1123 dwarf2_frame_unwind_stop_reason (frame_info_ptr this_frame
,
1126 struct dwarf2_frame_cache
*cache
1127 = dwarf2_frame_cache (this_frame
, this_cache
);
1129 if (cache
->unavailable_retaddr
)
1130 return UNWIND_UNAVAILABLE
;
1132 if (cache
->undefined_retaddr
)
1133 return UNWIND_OUTERMOST
;
1135 return UNWIND_NO_REASON
;
1139 dwarf2_frame_this_id (frame_info_ptr this_frame
, void **this_cache
,
1140 struct frame_id
*this_id
)
1142 struct dwarf2_frame_cache
*cache
=
1143 dwarf2_frame_cache (this_frame
, this_cache
);
1145 if (cache
->unavailable_retaddr
)
1146 (*this_id
) = frame_id_build_unavailable_stack (get_frame_func (this_frame
));
1147 else if (cache
->undefined_retaddr
)
1150 (*this_id
) = frame_id_build (cache
->cfa
, get_frame_func (this_frame
));
1153 static struct value
*
1154 dwarf2_frame_prev_register (frame_info_ptr this_frame
, void **this_cache
,
1157 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
1158 struct dwarf2_frame_cache
*cache
=
1159 dwarf2_frame_cache (this_frame
, this_cache
);
1163 /* Non-bottom frames of a virtual tail call frames chain use
1164 dwarf2_tailcall_frame_unwind unwinder so this code does not apply for
1165 them. If dwarf2_tailcall_prev_register_first does not have specific value
1166 unwind the register, tail call frames are assumed to have the register set
1167 of the top caller. */
1168 if (cache
->tailcall_cache
)
1172 val
= dwarf2_tailcall_prev_register_first (this_frame
,
1173 &cache
->tailcall_cache
,
1179 switch (cache
->reg
[regnum
].how
)
1181 case DWARF2_FRAME_REG_UNDEFINED
:
1182 /* If CFI explicitly specified that the value isn't defined,
1183 mark it as optimized away; the value isn't available. */
1184 return frame_unwind_got_optimized (this_frame
, regnum
);
1186 case DWARF2_FRAME_REG_SAVED_OFFSET
:
1187 addr
= cache
->cfa
+ cache
->reg
[regnum
].loc
.offset
;
1188 return frame_unwind_got_memory (this_frame
, regnum
, addr
);
1190 case DWARF2_FRAME_REG_SAVED_REG
:
1191 realnum
= dwarf_reg_to_regnum_or_error
1192 (gdbarch
, cache
->reg
[regnum
].loc
.reg
);
1193 return frame_unwind_got_register (this_frame
, regnum
, realnum
);
1195 case DWARF2_FRAME_REG_SAVED_EXP
:
1196 addr
= execute_stack_op (cache
->reg
[regnum
].loc
.exp
.start
,
1197 cache
->reg
[regnum
].loc
.exp
.len
,
1199 this_frame
, cache
->cfa
, 1,
1200 cache
->per_objfile
);
1201 return frame_unwind_got_memory (this_frame
, regnum
, addr
);
1203 case DWARF2_FRAME_REG_SAVED_VAL_OFFSET
:
1204 addr
= cache
->cfa
+ cache
->reg
[regnum
].loc
.offset
;
1205 return frame_unwind_got_constant (this_frame
, regnum
, addr
);
1207 case DWARF2_FRAME_REG_SAVED_VAL_EXP
:
1208 addr
= execute_stack_op (cache
->reg
[regnum
].loc
.exp
.start
,
1209 cache
->reg
[regnum
].loc
.exp
.len
,
1211 this_frame
, cache
->cfa
, 1,
1212 cache
->per_objfile
);
1213 return frame_unwind_got_constant (this_frame
, regnum
, addr
);
1215 case DWARF2_FRAME_REG_UNSPECIFIED
:
1216 /* GCC, in its infinite wisdom decided to not provide unwind
1217 information for registers that are "same value". Since
1218 DWARF2 (3 draft 7) doesn't define such behavior, said
1219 registers are actually undefined (which is different to CFI
1220 "undefined"). Code above issues a complaint about this.
1221 Here just fudge the books, assume GCC, and that the value is
1222 more inner on the stack. */
1223 return frame_unwind_got_register (this_frame
, regnum
, regnum
);
1225 case DWARF2_FRAME_REG_SAME_VALUE
:
1226 return frame_unwind_got_register (this_frame
, regnum
, regnum
);
1228 case DWARF2_FRAME_REG_CFA
:
1229 return frame_unwind_got_address (this_frame
, regnum
, cache
->cfa
);
1231 case DWARF2_FRAME_REG_CFA_OFFSET
:
1232 addr
= cache
->cfa
+ cache
->reg
[regnum
].loc
.offset
;
1233 return frame_unwind_got_address (this_frame
, regnum
, addr
);
1235 case DWARF2_FRAME_REG_RA_OFFSET
:
1236 addr
= cache
->reg
[regnum
].loc
.offset
;
1237 regnum
= dwarf_reg_to_regnum_or_error
1238 (gdbarch
, cache
->retaddr_reg
.loc
.reg
);
1239 addr
+= get_frame_register_unsigned (this_frame
, regnum
);
1240 return frame_unwind_got_address (this_frame
, regnum
, addr
);
1242 case DWARF2_FRAME_REG_FN
:
1243 return cache
->reg
[regnum
].loc
.fn (this_frame
, this_cache
, regnum
);
1246 internal_error (_("Unknown register rule."));
1253 dwarf2_frame_get_fn_data (frame_info_ptr this_frame
, void **this_cache
,
1254 fn_prev_register cookie
)
1256 struct dwarf2_frame_fn_data
*fn_data
= nullptr;
1257 struct dwarf2_frame_cache
*cache
1258 = dwarf2_frame_cache (this_frame
, this_cache
);
1260 /* Find the object for the function. */
1261 for (fn_data
= cache
->fn_data
; fn_data
; fn_data
= fn_data
->next
)
1262 if (fn_data
->cookie
== cookie
)
1263 return fn_data
->data
;
1271 dwarf2_frame_allocate_fn_data (frame_info_ptr this_frame
, void **this_cache
,
1272 fn_prev_register cookie
, unsigned long size
)
1274 struct dwarf2_frame_fn_data
*fn_data
= nullptr;
1275 struct dwarf2_frame_cache
*cache
1276 = dwarf2_frame_cache (this_frame
, this_cache
);
1278 /* First try to find an existing object. */
1279 void *data
= dwarf2_frame_get_fn_data (this_frame
, this_cache
, cookie
);
1280 gdb_assert (data
== nullptr);
1282 /* No object found, lets create a new instance. */
1283 fn_data
= FRAME_OBSTACK_ZALLOC (struct dwarf2_frame_fn_data
);
1284 fn_data
->cookie
= cookie
;
1285 fn_data
->data
= frame_obstack_zalloc (size
);
1286 fn_data
->next
= cache
->fn_data
;
1287 cache
->fn_data
= fn_data
;
1289 return fn_data
->data
;
1292 /* Proxy for tailcall_frame_dealloc_cache for bottom frame of a virtual tail
1293 call frames chain. */
1296 dwarf2_frame_dealloc_cache (frame_info
*self
, void *this_cache
)
1298 struct dwarf2_frame_cache
*cache
1299 = dwarf2_frame_cache (frame_info_ptr (self
), &this_cache
);
1301 if (cache
->tailcall_cache
)
1302 dwarf2_tailcall_frame_unwind
.dealloc_cache (self
, cache
->tailcall_cache
);
1306 dwarf2_frame_sniffer (const struct frame_unwind
*self
,
1307 frame_info_ptr this_frame
, void **this_cache
)
1309 if (!dwarf2_frame_unwinders_enabled_p
)
1312 /* Grab an address that is guaranteed to reside somewhere within the
1313 function. get_frame_pc(), with a no-return next function, can
1314 end up returning something past the end of this function's body.
1315 If the frame we're sniffing for is a signal frame whose start
1316 address is placed on the stack by the OS, its FDE must
1317 extend one byte before its start address or we could potentially
1318 select the FDE of the previous function. */
1319 CORE_ADDR block_addr
= get_frame_address_in_block (this_frame
);
1320 struct dwarf2_fde
*fde
= dwarf2_frame_find_fde (&block_addr
, NULL
);
1325 /* On some targets, signal trampolines may have unwind information.
1326 We need to recognize them so that we set the frame type
1329 if (fde
->cie
->signal_frame
1330 || dwarf2_frame_signal_frame_p (get_frame_arch (this_frame
),
1332 return self
->type
== SIGTRAMP_FRAME
;
1334 if (self
->type
!= NORMAL_FRAME
)
1340 static const struct frame_unwind dwarf2_frame_unwind
=
1344 dwarf2_frame_unwind_stop_reason
,
1345 dwarf2_frame_this_id
,
1346 dwarf2_frame_prev_register
,
1348 dwarf2_frame_sniffer
,
1349 dwarf2_frame_dealloc_cache
1352 static const struct frame_unwind dwarf2_signal_frame_unwind
=
1356 dwarf2_frame_unwind_stop_reason
,
1357 dwarf2_frame_this_id
,
1358 dwarf2_frame_prev_register
,
1360 dwarf2_frame_sniffer
,
1362 /* TAILCALL_CACHE can never be in such frame to need dealloc_cache. */
1366 /* Append the DWARF-2 frame unwinders to GDBARCH's list. */
1369 dwarf2_append_unwinders (struct gdbarch
*gdbarch
)
1371 frame_unwind_append_unwinder (gdbarch
, &dwarf2_frame_unwind
);
1372 frame_unwind_append_unwinder (gdbarch
, &dwarf2_signal_frame_unwind
);
1376 /* There is no explicitly defined relationship between the CFA and the
1377 location of frame's local variables and arguments/parameters.
1378 Therefore, frame base methods on this page should probably only be
1379 used as a last resort, just to avoid printing total garbage as a
1380 response to the "info frame" command. */
1383 dwarf2_frame_base_address (frame_info_ptr this_frame
, void **this_cache
)
1385 struct dwarf2_frame_cache
*cache
=
1386 dwarf2_frame_cache (this_frame
, this_cache
);
1391 static const struct frame_base dwarf2_frame_base
=
1393 &dwarf2_frame_unwind
,
1394 dwarf2_frame_base_address
,
1395 dwarf2_frame_base_address
,
1396 dwarf2_frame_base_address
1399 const struct frame_base
*
1400 dwarf2_frame_base_sniffer (frame_info_ptr this_frame
)
1402 CORE_ADDR block_addr
= get_frame_address_in_block (this_frame
);
1404 if (dwarf2_frame_find_fde (&block_addr
, NULL
))
1405 return &dwarf2_frame_base
;
1410 /* Compute the CFA for THIS_FRAME, but only if THIS_FRAME came from
1411 the DWARF unwinder. This is used to implement
1412 DW_OP_call_frame_cfa. */
1415 dwarf2_frame_cfa (frame_info_ptr this_frame
)
1417 if (frame_unwinder_is (this_frame
, &record_btrace_tailcall_frame_unwind
)
1418 || frame_unwinder_is (this_frame
, &record_btrace_frame_unwind
))
1419 throw_error (NOT_AVAILABLE_ERROR
,
1420 _("cfa not available for record btrace target"));
1422 while (get_frame_type (this_frame
) == INLINE_FRAME
)
1423 this_frame
= get_prev_frame (this_frame
);
1424 if (get_frame_unwind_stop_reason (this_frame
) == UNWIND_UNAVAILABLE
)
1425 throw_error (NOT_AVAILABLE_ERROR
,
1426 _("can't compute CFA for this frame: "
1427 "required registers or memory are unavailable"));
1429 if (get_frame_id (this_frame
).stack_status
!= FID_STACK_VALID
)
1430 throw_error (NOT_AVAILABLE_ERROR
,
1431 _("can't compute CFA for this frame: "
1432 "frame base not available"));
1434 return get_frame_base (this_frame
);
1437 /* We store the frame data on the BFD. This is only done if it is
1438 independent of the address space and so can be shared. */
1439 static const registry
<bfd
>::key
<comp_unit
> dwarf2_frame_bfd_data
;
1441 /* If any BFD sections require relocations (note; really should be if
1442 any debug info requires relocations), then we store the frame data
1443 on the objfile instead, and do not share it. */
1444 static const registry
<objfile
>::key
<comp_unit
> dwarf2_frame_objfile_data
;
1447 /* Pointer encoding helper functions. */
1449 /* GCC supports exception handling based on DWARF2 CFI. However, for
1450 technical reasons, it encodes addresses in its FDE's in a different
1451 way. Several "pointer encodings" are supported. The encoding
1452 that's used for a particular FDE is determined by the 'R'
1453 augmentation in the associated CIE. The argument of this
1454 augmentation is a single byte.
1456 The address can be encoded as 2 bytes, 4 bytes, 8 bytes, or as a
1457 LEB128. This is encoded in bits 0, 1 and 2. Bit 3 encodes whether
1458 the address is signed or unsigned. Bits 4, 5 and 6 encode how the
1459 address should be interpreted (absolute, relative to the current
1460 position in the FDE, ...). Bit 7, indicates that the address
1461 should be dereferenced. */
1464 encoding_for_size (unsigned int size
)
1469 return DW_EH_PE_udata2
;
1471 return DW_EH_PE_udata4
;
1473 return DW_EH_PE_udata8
;
1475 internal_error (_("Unsupported address size"));
1480 read_encoded_value (struct comp_unit
*unit
, gdb_byte encoding
,
1481 int ptr_len
, const gdb_byte
*buf
,
1482 unsigned int *bytes_read_ptr
,
1483 unrelocated_addr func_base
)
1488 /* GCC currently doesn't generate DW_EH_PE_indirect encodings for
1490 if (encoding
& DW_EH_PE_indirect
)
1491 internal_error (_("Unsupported encoding: DW_EH_PE_indirect"));
1493 *bytes_read_ptr
= 0;
1495 switch (encoding
& 0x70)
1497 case DW_EH_PE_absptr
:
1500 case DW_EH_PE_pcrel
:
1501 base
= bfd_section_vma (unit
->dwarf_frame_section
);
1502 base
+= (buf
- unit
->dwarf_frame_buffer
);
1504 case DW_EH_PE_datarel
:
1507 case DW_EH_PE_textrel
:
1510 case DW_EH_PE_funcrel
:
1511 base
= (ULONGEST
) func_base
;
1513 case DW_EH_PE_aligned
:
1515 offset
= buf
- unit
->dwarf_frame_buffer
;
1516 if ((offset
% ptr_len
) != 0)
1518 *bytes_read_ptr
= ptr_len
- (offset
% ptr_len
);
1519 buf
+= *bytes_read_ptr
;
1523 internal_error (_("Invalid or unsupported encoding"));
1526 if ((encoding
& 0x07) == 0x00)
1528 encoding
|= encoding_for_size (ptr_len
);
1529 if (bfd_get_sign_extend_vma (unit
->abfd
))
1530 encoding
|= DW_EH_PE_signed
;
1533 switch (encoding
& 0x0f)
1535 case DW_EH_PE_uleb128
:
1538 const gdb_byte
*end_buf
= buf
+ (sizeof (value
) + 1) * 8 / 7;
1540 *bytes_read_ptr
+= safe_read_uleb128 (buf
, end_buf
, &value
) - buf
;
1541 return base
+ value
;
1543 case DW_EH_PE_udata2
:
1544 *bytes_read_ptr
+= 2;
1545 return (base
+ bfd_get_16 (unit
->abfd
, (bfd_byte
*) buf
));
1546 case DW_EH_PE_udata4
:
1547 *bytes_read_ptr
+= 4;
1548 return (base
+ bfd_get_32 (unit
->abfd
, (bfd_byte
*) buf
));
1549 case DW_EH_PE_udata8
:
1550 *bytes_read_ptr
+= 8;
1551 return (base
+ bfd_get_64 (unit
->abfd
, (bfd_byte
*) buf
));
1552 case DW_EH_PE_sleb128
:
1555 const gdb_byte
*end_buf
= buf
+ (sizeof (value
) + 1) * 8 / 7;
1557 *bytes_read_ptr
+= safe_read_sleb128 (buf
, end_buf
, &value
) - buf
;
1558 return base
+ value
;
1560 case DW_EH_PE_sdata2
:
1561 *bytes_read_ptr
+= 2;
1562 return (base
+ bfd_get_signed_16 (unit
->abfd
, (bfd_byte
*) buf
));
1563 case DW_EH_PE_sdata4
:
1564 *bytes_read_ptr
+= 4;
1565 return (base
+ bfd_get_signed_32 (unit
->abfd
, (bfd_byte
*) buf
));
1566 case DW_EH_PE_sdata8
:
1567 *bytes_read_ptr
+= 8;
1568 return (base
+ bfd_get_signed_64 (unit
->abfd
, (bfd_byte
*) buf
));
1570 internal_error (_("Invalid or unsupported encoding"));
1575 /* Find CIE with the given CIE_POINTER in CIE_TABLE. */
1576 static struct dwarf2_cie
*
1577 find_cie (const dwarf2_cie_table
&cie_table
, ULONGEST cie_pointer
)
1579 auto iter
= cie_table
.find (cie_pointer
);
1580 if (iter
!= cie_table
.end ())
1581 return iter
->second
;
1586 bsearch_fde_cmp (const dwarf2_fde
*fde
, unrelocated_addr seek_pc
)
1588 if (fde
->end_addr () <= seek_pc
)
1590 if (fde
->initial_location
<= seek_pc
)
1595 /* Find an existing comp_unit for an objfile, if any. */
1598 find_comp_unit (struct objfile
*objfile
)
1600 bfd
*abfd
= objfile
->obfd
.get ();
1601 if (gdb_bfd_requires_relocations (abfd
))
1602 return dwarf2_frame_objfile_data
.get (objfile
);
1604 return dwarf2_frame_bfd_data
.get (abfd
);
1607 /* Store the comp_unit on OBJFILE, or the corresponding BFD, as
1611 set_comp_unit (struct objfile
*objfile
, struct comp_unit
*unit
)
1613 bfd
*abfd
= objfile
->obfd
.get ();
1614 if (gdb_bfd_requires_relocations (abfd
))
1615 return dwarf2_frame_objfile_data
.set (objfile
, unit
);
1617 return dwarf2_frame_bfd_data
.set (abfd
, unit
);
1620 /* Find the FDE for *PC. Return a pointer to the FDE, and store the
1621 initial location associated with it into *PC. */
1623 static struct dwarf2_fde
*
1624 dwarf2_frame_find_fde (CORE_ADDR
*pc
, dwarf2_per_objfile
**out_per_objfile
)
1626 for (objfile
*objfile
: current_program_space
->objfiles ())
1630 if (objfile
->obfd
== nullptr)
1633 comp_unit
*unit
= find_comp_unit (objfile
);
1636 dwarf2_build_frame_info (objfile
);
1637 unit
= find_comp_unit (objfile
);
1639 gdb_assert (unit
!= NULL
);
1641 dwarf2_fde_table
*fde_table
= &unit
->fde_table
;
1642 if (fde_table
->empty ())
1645 gdb_assert (!objfile
->section_offsets
.empty ());
1646 offset
= objfile
->text_section_offset ();
1648 gdb_assert (!fde_table
->empty ());
1649 unrelocated_addr seek_pc
= (unrelocated_addr
) (*pc
- offset
);
1650 if (seek_pc
< (*fde_table
)[0]->initial_location
)
1653 auto it
= gdb::binary_search (fde_table
->begin (), fde_table
->end (),
1654 seek_pc
, bsearch_fde_cmp
);
1655 if (it
!= fde_table
->end ())
1657 *pc
= (CORE_ADDR
) (*it
)->initial_location
+ offset
;
1658 if (out_per_objfile
!= nullptr)
1659 *out_per_objfile
= get_dwarf2_per_objfile (objfile
);
1667 /* Add FDE to FDE_TABLE. */
1669 add_fde (dwarf2_fde_table
*fde_table
, struct dwarf2_fde
*fde
)
1671 if (fde
->address_range
== 0)
1672 /* Discard useless FDEs. */
1675 fde_table
->push_back (fde
);
1678 #define DW64_CIE_ID 0xffffffffffffffffULL
1680 /* Defines the type of eh_frames that are expected to be decoded: CIE, FDE
1685 EH_CIE_TYPE_ID
= 1 << 0,
1686 EH_FDE_TYPE_ID
= 1 << 1,
1687 EH_CIE_OR_FDE_TYPE_ID
= EH_CIE_TYPE_ID
| EH_FDE_TYPE_ID
1690 static const gdb_byte
*decode_frame_entry (struct gdbarch
*gdbarch
,
1691 struct comp_unit
*unit
,
1692 const gdb_byte
*start
,
1694 dwarf2_cie_table
&cie_table
,
1695 dwarf2_fde_table
*fde_table
,
1696 enum eh_frame_type entry_type
);
1698 /* Decode the next CIE or FDE, entry_type specifies the expected type.
1699 Return NULL if invalid input, otherwise the next byte to be processed. */
1701 static const gdb_byte
*
1702 decode_frame_entry_1 (struct gdbarch
*gdbarch
,
1703 struct comp_unit
*unit
, const gdb_byte
*start
,
1705 dwarf2_cie_table
&cie_table
,
1706 dwarf2_fde_table
*fde_table
,
1707 enum eh_frame_type entry_type
)
1709 const gdb_byte
*buf
, *end
;
1711 unsigned int bytes_read
;
1714 ULONGEST cie_pointer
;
1719 length
= read_initial_length (unit
->abfd
, buf
, &bytes_read
, false);
1721 end
= buf
+ (size_t) length
;
1726 /* Are we still within the section? */
1727 if (end
<= buf
|| end
> unit
->dwarf_frame_buffer
+ unit
->dwarf_frame_size
)
1730 /* Distinguish between 32 and 64-bit encoded frame info. */
1731 dwarf64_p
= (bytes_read
== 12);
1733 /* In a .eh_frame section, zero is used to distinguish CIEs from FDEs. */
1737 cie_id
= DW64_CIE_ID
;
1743 cie_pointer
= read_8_bytes (unit
->abfd
, buf
);
1748 cie_pointer
= read_4_bytes (unit
->abfd
, buf
);
1752 if (cie_pointer
== cie_id
)
1754 /* This is a CIE. */
1755 struct dwarf2_cie
*cie
;
1756 const char *augmentation
;
1757 unsigned int cie_version
;
1759 /* Check that a CIE was expected. */
1760 if ((entry_type
& EH_CIE_TYPE_ID
) == 0)
1761 error (_("Found a CIE when not expecting it."));
1763 /* Record the offset into the .debug_frame section of this CIE. */
1764 cie_pointer
= start
- unit
->dwarf_frame_buffer
;
1766 /* Check whether we've already read it. */
1767 if (find_cie (cie_table
, cie_pointer
))
1770 cie
= XOBNEW (&unit
->obstack
, struct dwarf2_cie
);
1771 cie
->initial_instructions
= NULL
;
1772 cie
->cie_pointer
= cie_pointer
;
1774 /* The encoding for FDE's in a normal .debug_frame section
1775 depends on the target address size. */
1776 cie
->encoding
= DW_EH_PE_absptr
;
1778 /* We'll determine the final value later, but we need to
1779 initialize it conservatively. */
1780 cie
->signal_frame
= 0;
1782 /* Check version number. */
1783 cie_version
= read_1_byte (unit
->abfd
, buf
);
1784 if (cie_version
!= 1 && cie_version
!= 3 && cie_version
!= 4)
1786 cie
->version
= cie_version
;
1789 /* Interpret the interesting bits of the augmentation. */
1790 cie
->augmentation
= augmentation
= (const char *) buf
;
1791 buf
+= (strlen (augmentation
) + 1);
1793 /* Ignore armcc augmentations. We only use them for quirks,
1794 and that doesn't happen until later. */
1795 if (startswith (augmentation
, "armcc"))
1796 augmentation
+= strlen (augmentation
);
1798 /* The GCC 2.x "eh" augmentation has a pointer immediately
1799 following the augmentation string, so it must be handled
1801 if (augmentation
[0] == 'e' && augmentation
[1] == 'h')
1804 buf
+= gdbarch_ptr_bit (gdbarch
) / TARGET_CHAR_BIT
;
1808 if (cie
->version
>= 4)
1810 /* FIXME: check that this is the same as from the CU header. */
1811 cie
->addr_size
= read_1_byte (unit
->abfd
, buf
);
1813 cie
->segment_size
= read_1_byte (unit
->abfd
, buf
);
1818 cie
->addr_size
= gdbarch_dwarf2_addr_size (gdbarch
);
1819 cie
->segment_size
= 0;
1821 /* Address values in .eh_frame sections are defined to have the
1822 target's pointer size. Watchout: This breaks frame info for
1823 targets with pointer size < address size, unless a .debug_frame
1824 section exists as well. */
1826 cie
->ptr_size
= gdbarch_ptr_bit (gdbarch
) / TARGET_CHAR_BIT
;
1828 cie
->ptr_size
= cie
->addr_size
;
1830 buf
= gdb_read_uleb128 (buf
, end
, &uleb128
);
1833 cie
->code_alignment_factor
= uleb128
;
1835 buf
= gdb_read_sleb128 (buf
, end
, &sleb128
);
1838 cie
->data_alignment_factor
= sleb128
;
1840 if (cie_version
== 1)
1842 cie
->return_address_register
= read_1_byte (unit
->abfd
, buf
);
1847 buf
= gdb_read_uleb128 (buf
, end
, &uleb128
);
1850 cie
->return_address_register
= uleb128
;
1853 cie
->return_address_register
1854 = dwarf2_frame_adjust_regnum (gdbarch
,
1855 cie
->return_address_register
,
1858 cie
->saw_z_augmentation
= (*augmentation
== 'z');
1859 if (cie
->saw_z_augmentation
)
1861 uint64_t uleb_length
;
1863 buf
= gdb_read_uleb128 (buf
, end
, &uleb_length
);
1866 cie
->initial_instructions
= buf
+ uleb_length
;
1870 while (*augmentation
)
1872 /* "L" indicates a byte showing how the LSDA pointer is encoded. */
1873 if (*augmentation
== 'L')
1880 /* "R" indicates a byte indicating how FDE addresses are encoded. */
1881 else if (*augmentation
== 'R')
1883 cie
->encoding
= *buf
++;
1887 /* "P" indicates a personality routine in the CIE augmentation. */
1888 else if (*augmentation
== 'P')
1890 /* Skip. Avoid indirection since we throw away the result. */
1891 gdb_byte encoding
= (*buf
++) & ~DW_EH_PE_indirect
;
1892 read_encoded_value (unit
, encoding
, cie
->ptr_size
,
1893 buf
, &bytes_read
, (unrelocated_addr
) 0);
1898 /* "S" indicates a signal frame, such that the return
1899 address must not be decremented to locate the call frame
1900 info for the previous frame; it might even be the first
1901 instruction of a function, so decrementing it would take
1902 us to a different function. */
1903 else if (*augmentation
== 'S')
1905 cie
->signal_frame
= 1;
1909 /* Otherwise we have an unknown augmentation. Assume that either
1910 there is no augmentation data, or we saw a 'z' prefix. */
1913 if (cie
->initial_instructions
)
1914 buf
= cie
->initial_instructions
;
1919 cie
->initial_instructions
= buf
;
1923 cie_table
[cie
->cie_pointer
] = cie
;
1927 /* This is a FDE. */
1928 struct dwarf2_fde
*fde
;
1930 /* Check that an FDE was expected. */
1931 if ((entry_type
& EH_FDE_TYPE_ID
) == 0)
1932 error (_("Found an FDE when not expecting it."));
1934 /* In an .eh_frame section, the CIE pointer is the delta between the
1935 address within the FDE where the CIE pointer is stored and the
1936 address of the CIE. Convert it to an offset into the .eh_frame
1940 cie_pointer
= buf
- unit
->dwarf_frame_buffer
- cie_pointer
;
1941 cie_pointer
-= (dwarf64_p
? 8 : 4);
1944 /* In either case, validate the result is still within the section. */
1945 if (cie_pointer
>= unit
->dwarf_frame_size
)
1948 fde
= XOBNEW (&unit
->obstack
, struct dwarf2_fde
);
1949 fde
->cie
= find_cie (cie_table
, cie_pointer
);
1950 if (fde
->cie
== NULL
)
1952 decode_frame_entry (gdbarch
, unit
,
1953 unit
->dwarf_frame_buffer
+ cie_pointer
,
1954 eh_frame_p
, cie_table
, fde_table
,
1956 fde
->cie
= find_cie (cie_table
, cie_pointer
);
1959 gdb_assert (fde
->cie
!= NULL
);
1962 = read_encoded_value (unit
, fde
->cie
->encoding
, fde
->cie
->ptr_size
,
1963 buf
, &bytes_read
, (unrelocated_addr
) 0);
1964 fde
->initial_location
1965 = (unrelocated_addr
) gdbarch_adjust_dwarf2_addr (gdbarch
, init_addr
);
1969 = read_encoded_value (unit
, fde
->cie
->encoding
& 0x0f,
1970 fde
->cie
->ptr_size
, buf
, &bytes_read
,
1971 (unrelocated_addr
) 0);
1972 ULONGEST addr
= gdbarch_adjust_dwarf2_addr (gdbarch
, init_addr
+ range
);
1973 fde
->address_range
= addr
- (ULONGEST
) fde
->initial_location
;
1976 /* A 'z' augmentation in the CIE implies the presence of an
1977 augmentation field in the FDE as well. The only thing known
1978 to be in here at present is the LSDA entry for EH. So we
1979 can skip the whole thing. */
1980 if (fde
->cie
->saw_z_augmentation
)
1982 uint64_t uleb_length
;
1984 buf
= gdb_read_uleb128 (buf
, end
, &uleb_length
);
1992 fde
->instructions
= buf
;
1995 fde
->eh_frame_p
= eh_frame_p
;
1997 add_fde (fde_table
, fde
);
2003 /* Read a CIE or FDE in BUF and decode it. Entry_type specifies whether we
2004 expect an FDE or a CIE. */
2006 static const gdb_byte
*
2007 decode_frame_entry (struct gdbarch
*gdbarch
,
2008 struct comp_unit
*unit
, const gdb_byte
*start
,
2010 dwarf2_cie_table
&cie_table
,
2011 dwarf2_fde_table
*fde_table
,
2012 enum eh_frame_type entry_type
)
2014 enum { NONE
, ALIGN4
, ALIGN8
, FAIL
} workaround
= NONE
;
2015 const gdb_byte
*ret
;
2016 ptrdiff_t start_offset
;
2020 ret
= decode_frame_entry_1 (gdbarch
, unit
, start
, eh_frame_p
,
2021 cie_table
, fde_table
, entry_type
);
2025 /* We have corrupt input data of some form. */
2027 /* ??? Try, weakly, to work around compiler/assembler/linker bugs
2028 and mismatches wrt padding and alignment of debug sections. */
2029 /* Note that there is no requirement in the standard for any
2030 alignment at all in the frame unwind sections. Testing for
2031 alignment before trying to interpret data would be incorrect.
2033 However, GCC traditionally arranged for frame sections to be
2034 sized such that the FDE length and CIE fields happen to be
2035 aligned (in theory, for performance). This, unfortunately,
2036 was done with .align directives, which had the side effect of
2037 forcing the section to be aligned by the linker.
2039 This becomes a problem when you have some other producer that
2040 creates frame sections that are not as strictly aligned. That
2041 produces a hole in the frame info that gets filled by the
2044 The GCC behaviour is arguably a bug, but it's effectively now
2045 part of the ABI, so we're now stuck with it, at least at the
2046 object file level. A smart linker may decide, in the process
2047 of compressing duplicate CIE information, that it can rewrite
2048 the entire output section without this extra padding. */
2050 start_offset
= start
- unit
->dwarf_frame_buffer
;
2051 if (workaround
< ALIGN4
&& (start_offset
& 3) != 0)
2053 start
+= 4 - (start_offset
& 3);
2054 workaround
= ALIGN4
;
2057 if (workaround
< ALIGN8
&& (start_offset
& 7) != 0)
2059 start
+= 8 - (start_offset
& 7);
2060 workaround
= ALIGN8
;
2064 /* Nothing left to try. Arrange to return as if we've consumed
2065 the entire input section. Hopefully we'll get valid info from
2066 the other of .debug_frame/.eh_frame. */
2068 ret
= unit
->dwarf_frame_buffer
+ unit
->dwarf_frame_size
;
2079 Corrupt data in %s:%s; align 4 workaround apparently succeeded"),
2080 bfd_get_filename (unit
->dwarf_frame_section
->owner
),
2081 bfd_section_name (unit
->dwarf_frame_section
));
2086 Corrupt data in %s:%s; align 8 workaround apparently succeeded"),
2087 bfd_get_filename (unit
->dwarf_frame_section
->owner
),
2088 bfd_section_name (unit
->dwarf_frame_section
));
2092 complaint (_("Corrupt data in %s:%s"),
2093 bfd_get_filename (unit
->dwarf_frame_section
->owner
),
2094 bfd_section_name (unit
->dwarf_frame_section
));
2102 fde_is_less_than (const dwarf2_fde
*aa
, const dwarf2_fde
*bb
)
2104 if (aa
->initial_location
== bb
->initial_location
)
2106 if (aa
->address_range
!= bb
->address_range
2107 && aa
->eh_frame_p
== 0 && bb
->eh_frame_p
== 0)
2108 /* Linker bug, e.g. gold/10400.
2109 Work around it by keeping stable sort order. */
2112 /* Put eh_frame entries after debug_frame ones. */
2113 return aa
->eh_frame_p
< bb
->eh_frame_p
;
2116 return aa
->initial_location
< bb
->initial_location
;
2120 dwarf2_build_frame_info (struct objfile
*objfile
)
2122 const gdb_byte
*frame_ptr
;
2123 dwarf2_cie_table cie_table
;
2124 dwarf2_fde_table fde_table
;
2126 struct gdbarch
*gdbarch
= objfile
->arch ();
2128 /* Build a minimal decoding of the DWARF2 compilation unit. */
2129 std::unique_ptr
<comp_unit
> unit (new comp_unit (objfile
));
2131 if (objfile
->separate_debug_objfile_backlink
== NULL
)
2133 /* Do not read .eh_frame from separate file as they must be also
2134 present in the main file. */
2135 dwarf2_get_section_info (objfile
, DWARF2_EH_FRAME
,
2136 &unit
->dwarf_frame_section
,
2137 &unit
->dwarf_frame_buffer
,
2138 &unit
->dwarf_frame_size
);
2139 if (unit
->dwarf_frame_size
)
2141 asection
*got
, *txt
;
2143 /* FIXME: kettenis/20030602: This is the DW_EH_PE_datarel base
2144 that is used for the i386/amd64 target, which currently is
2145 the only target in GCC that supports/uses the
2146 DW_EH_PE_datarel encoding. */
2147 got
= bfd_get_section_by_name (unit
->abfd
, ".got");
2149 unit
->dbase
= got
->vma
;
2151 /* GCC emits the DW_EH_PE_textrel encoding type on sh and ia64
2153 txt
= bfd_get_section_by_name (unit
->abfd
, ".text");
2155 unit
->tbase
= txt
->vma
;
2159 frame_ptr
= unit
->dwarf_frame_buffer
;
2160 while (frame_ptr
< unit
->dwarf_frame_buffer
+ unit
->dwarf_frame_size
)
2161 frame_ptr
= decode_frame_entry (gdbarch
, unit
.get (),
2163 cie_table
, &fde_table
,
2164 EH_CIE_OR_FDE_TYPE_ID
);
2167 catch (const gdb_exception_error
&e
)
2169 warning (_("skipping .eh_frame info of %s: %s"),
2170 objfile_name (objfile
), e
.what ());
2173 /* The cie_table is discarded below. */
2180 dwarf2_get_section_info (objfile
, DWARF2_DEBUG_FRAME
,
2181 &unit
->dwarf_frame_section
,
2182 &unit
->dwarf_frame_buffer
,
2183 &unit
->dwarf_frame_size
);
2184 if (unit
->dwarf_frame_size
)
2186 size_t num_old_fde_entries
= fde_table
.size ();
2190 frame_ptr
= unit
->dwarf_frame_buffer
;
2191 while (frame_ptr
< unit
->dwarf_frame_buffer
+ unit
->dwarf_frame_size
)
2192 frame_ptr
= decode_frame_entry (gdbarch
, unit
.get (), frame_ptr
, 0,
2193 cie_table
, &fde_table
,
2194 EH_CIE_OR_FDE_TYPE_ID
);
2196 catch (const gdb_exception_error
&e
)
2198 warning (_("skipping .debug_frame info of %s: %s"),
2199 objfile_name (objfile
), e
.what ());
2201 fde_table
.resize (num_old_fde_entries
);
2205 struct dwarf2_fde
*fde_prev
= NULL
;
2206 struct dwarf2_fde
*first_non_zero_fde
= NULL
;
2208 /* Prepare FDE table for lookups. */
2209 std::sort (fde_table
.begin (), fde_table
.end (), fde_is_less_than
);
2211 /* Check for leftovers from --gc-sections. The GNU linker sets
2212 the relevant symbols to zero, but doesn't zero the FDE *end*
2213 ranges because there's no relocation there. It's (offset,
2214 length), not (start, end). On targets where address zero is
2215 just another valid address this can be a problem, since the
2216 FDEs appear to be non-empty in the output --- we could pick
2217 out the wrong FDE. To work around this, when overlaps are
2218 detected, we prefer FDEs that do not start at zero.
2220 Start by finding the first FDE with non-zero start. Below
2221 we'll discard all FDEs that start at zero and overlap this
2223 for (struct dwarf2_fde
*fde
: fde_table
)
2225 if (fde
->initial_location
!= (unrelocated_addr
) 0)
2227 first_non_zero_fde
= fde
;
2232 /* Since we'll be doing bsearch, squeeze out identical (except
2233 for eh_frame_p) fde entries so bsearch result is predictable.
2234 Also discard leftovers from --gc-sections. */
2235 for (struct dwarf2_fde
*fde
: fde_table
)
2237 if (fde
->initial_location
== (unrelocated_addr
) 0
2238 && first_non_zero_fde
!= NULL
2239 && first_non_zero_fde
->initial_location
< fde
->end_addr ())
2242 if (fde_prev
!= NULL
2243 && fde_prev
->initial_location
== fde
->initial_location
)
2246 unit
->fde_table
.push_back (fde
);
2249 unit
->fde_table
.shrink_to_fit ();
2251 set_comp_unit (objfile
, unit
.release ());
2254 /* Handle 'maintenance show dwarf unwinders'. */
2257 show_dwarf_unwinders_enabled_p (struct ui_file
*file
, int from_tty
,
2258 struct cmd_list_element
*c
,
2262 _("The DWARF stack unwinders are currently %s.\n"),
2266 void _initialize_dwarf2_frame ();
2268 _initialize_dwarf2_frame ()
2270 add_setshow_boolean_cmd ("unwinders", class_obscure
,
2271 &dwarf2_frame_unwinders_enabled_p
, _("\
2272 Set whether the DWARF stack frame unwinders are used."), _("\
2273 Show whether the DWARF stack frame unwinders are used."), _("\
2274 When enabled the DWARF stack frame unwinders can be used for architectures\n\
2275 that support the DWARF unwinders. Enabling the DWARF unwinders for an\n\
2276 architecture that doesn't support them will have no effect."),
2278 show_dwarf_unwinders_enabled_p
,
2280 &show_dwarf_cmdlist
);
2283 selftests::register_test_foreach_arch ("execute_cfa_program",
2284 selftests::execute_cfa_program_test
);