Add translations for various sub-directories
[binutils-gdb.git] / gdb / dwarf2 / frame.c
blob03e973eb902e046cae085470108092dbcdefaf62
1 /* Frame unwinder for frames with DWARF Call Frame Information.
3 Copyright (C) 2003-2024 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/>. */
22 #include "dwarf2/expr.h"
23 #include "dwarf2.h"
24 #include "dwarf2/leb.h"
25 #include "frame.h"
26 #include "frame-base.h"
27 #include "frame-unwind.h"
28 #include "gdbtypes.h"
29 #include "symtab.h"
30 #include "objfiles.h"
31 #include "regcache.h"
32 #include "value.h"
33 #include "record.h"
34 #include "extract-store-integer.h"
36 #include "complaints.h"
37 #include "dwarf2/frame.h"
38 #include "dwarf2/read.h"
39 #include "dwarf2/public.h"
40 #include "dwarf2/loc.h"
41 #include "dwarf2/frame-tailcall.h"
42 #include "gdbsupport/gdb_binary_search.h"
43 #if GDB_SELF_TEST
44 #include "gdbsupport/selftest.h"
45 #include "selftest-arch.h"
46 #endif
47 #include <unordered_map>
49 #include <algorithm>
51 struct comp_unit;
53 /* Call Frame Information (CFI). */
55 /* Common Information Entry (CIE). */
57 struct dwarf2_cie
59 /* Computation Unit for this CIE. */
60 struct comp_unit *unit;
62 /* Offset into the .debug_frame section where this CIE was found.
63 Used to identify this CIE. */
64 ULONGEST cie_pointer;
66 /* Constant that is factored out of all advance location
67 instructions. */
68 ULONGEST code_alignment_factor;
70 /* Constants that is factored out of all offset instructions. */
71 LONGEST data_alignment_factor;
73 /* Return address column. */
74 ULONGEST return_address_register;
76 /* Instruction sequence to initialize a register set. */
77 const gdb_byte *initial_instructions;
78 const gdb_byte *end;
80 /* Saved augmentation, in case it's needed later. */
81 const char *augmentation;
83 /* Encoding of addresses. */
84 gdb_byte encoding;
86 /* Target address size in bytes. */
87 int addr_size;
89 /* Target pointer size in bytes. */
90 int ptr_size;
92 /* True if a 'z' augmentation existed. */
93 unsigned char saw_z_augmentation;
95 /* True if an 'S' augmentation existed. */
96 unsigned char signal_frame;
98 /* The version recorded in the CIE. */
99 unsigned char version;
101 /* The segment size. */
102 unsigned char segment_size;
105 /* The CIE table is used to find CIEs during parsing, but then
106 discarded. It maps from the CIE's offset to the CIE. */
107 typedef std::unordered_map<ULONGEST, dwarf2_cie *> dwarf2_cie_table;
109 /* Frame Description Entry (FDE). */
111 struct dwarf2_fde
113 /* Return the final location in this FDE. */
114 unrelocated_addr end_addr () const
116 return (unrelocated_addr) ((ULONGEST) initial_location
117 + address_range);
120 /* CIE for this FDE. */
121 struct dwarf2_cie *cie;
123 /* First location associated with this FDE. */
124 unrelocated_addr initial_location;
126 /* Number of bytes of program instructions described by this FDE. */
127 ULONGEST address_range;
129 /* Instruction sequence. */
130 const gdb_byte *instructions;
131 const gdb_byte *end;
133 /* True if this FDE is read from a .eh_frame instead of a .debug_frame
134 section. */
135 unsigned char eh_frame_p;
138 typedef std::vector<dwarf2_fde *> dwarf2_fde_table;
140 /* A minimal decoding of DWARF2 compilation units. We only decode
141 what's needed to get to the call frame information. */
143 struct comp_unit
145 comp_unit (struct objfile *objf)
146 : abfd (objf->obfd.get ())
150 /* Keep the bfd convenient. */
151 bfd *abfd;
153 /* Pointer to the .debug_frame section loaded into memory. */
154 const gdb_byte *dwarf_frame_buffer = nullptr;
156 /* Length of the loaded .debug_frame section. */
157 bfd_size_type dwarf_frame_size = 0;
159 /* Pointer to the .debug_frame section. */
160 asection *dwarf_frame_section = nullptr;
162 /* Base for DW_EH_PE_datarel encodings. */
163 bfd_vma dbase = 0;
165 /* Base for DW_EH_PE_textrel encodings. */
166 bfd_vma tbase = 0;
168 /* The FDE table. */
169 dwarf2_fde_table fde_table;
171 /* Hold data used by this module. */
172 auto_obstack obstack;
175 static struct dwarf2_fde *dwarf2_frame_find_fde
176 (CORE_ADDR *pc, dwarf2_per_objfile **out_per_objfile);
178 static int dwarf2_frame_adjust_regnum (struct gdbarch *gdbarch, int regnum,
179 int eh_frame_p);
181 static ULONGEST read_encoded_value (struct comp_unit *unit, gdb_byte encoding,
182 int ptr_len, const gdb_byte *buf,
183 unsigned int *bytes_read_ptr,
184 unrelocated_addr func_base);
187 /* Store the length the expression for the CFA in the `cfa_reg' field,
188 which is unused in that case. */
189 #define cfa_exp_len cfa_reg
191 dwarf2_frame_state::dwarf2_frame_state (CORE_ADDR pc_, struct dwarf2_cie *cie)
192 : pc (pc_), data_align (cie->data_alignment_factor),
193 code_align (cie->code_alignment_factor),
194 retaddr_column (cie->return_address_register)
198 /* Execute the required actions for both the DW_CFA_restore and
199 DW_CFA_restore_extended instructions. */
200 static void
201 dwarf2_restore_rule (struct gdbarch *gdbarch, ULONGEST reg_num,
202 struct dwarf2_frame_state *fs, int eh_frame_p)
204 ULONGEST reg;
206 reg = dwarf2_frame_adjust_regnum (gdbarch, reg_num, eh_frame_p);
207 fs->regs.alloc_regs (reg + 1);
209 /* Check if this register was explicitly initialized in the
210 CIE initial instructions. If not, default the rule to
211 UNSPECIFIED. */
212 if (reg < fs->initial.reg.size ())
213 fs->regs.reg[reg] = fs->initial.reg[reg];
214 else
215 fs->regs.reg[reg].how = DWARF2_FRAME_REG_UNSPECIFIED;
217 if (fs->regs.reg[reg].how == DWARF2_FRAME_REG_UNSPECIFIED)
219 int regnum = dwarf_reg_to_regnum (gdbarch, reg);
221 complaint (_("\
222 incomplete CFI data; DW_CFA_restore unspecified\n\
223 register %s (#%d) at %s"),
224 gdbarch_register_name (gdbarch, regnum), regnum,
225 paddress (gdbarch, fs->pc));
229 static CORE_ADDR
230 execute_stack_op (const gdb_byte *exp, ULONGEST len, int addr_size,
231 const frame_info_ptr &this_frame, CORE_ADDR initial,
232 int initial_in_stack_memory, dwarf2_per_objfile *per_objfile)
234 dwarf_expr_context ctx (per_objfile, addr_size);
235 scoped_value_mark free_values;
237 ctx.push_address (initial, initial_in_stack_memory);
238 value *result_val = ctx.evaluate (exp, len, true, nullptr, this_frame);
240 if (result_val->lval () == lval_memory)
241 return result_val->address ();
242 else
243 return value_as_address (result_val);
247 /* Execute FDE program from INSN_PTR possibly up to INSN_END or up to inferior
248 PC. Modify FS state accordingly. Return current INSN_PTR where the
249 execution has stopped, one can resume it on the next call. */
251 static const gdb_byte *
252 execute_cfa_program (struct dwarf2_fde *fde, const gdb_byte *insn_ptr,
253 const gdb_byte *insn_end, struct gdbarch *gdbarch,
254 CORE_ADDR pc, struct dwarf2_frame_state *fs,
255 CORE_ADDR text_offset)
257 int eh_frame_p = fde->eh_frame_p;
258 unsigned int bytes_read;
259 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
261 while (insn_ptr < insn_end && fs->pc <= pc)
263 gdb_byte insn = *insn_ptr++;
264 uint64_t utmp, reg;
265 int64_t offset;
267 if ((insn & 0xc0) == DW_CFA_advance_loc)
268 fs->pc += (insn & 0x3f) * fs->code_align;
269 else if ((insn & 0xc0) == DW_CFA_offset)
271 reg = insn & 0x3f;
272 reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
273 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
274 offset = utmp * fs->data_align;
275 fs->regs.alloc_regs (reg + 1);
276 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
277 fs->regs.reg[reg].loc.offset = offset;
279 else if ((insn & 0xc0) == DW_CFA_restore)
281 reg = insn & 0x3f;
282 dwarf2_restore_rule (gdbarch, reg, fs, eh_frame_p);
284 else
286 switch (insn)
288 case DW_CFA_set_loc:
289 fs->pc = read_encoded_value (fde->cie->unit, fde->cie->encoding,
290 fde->cie->ptr_size, insn_ptr,
291 &bytes_read, fde->initial_location);
292 /* Apply the text offset for relocatable objects. */
293 fs->pc += text_offset;
294 insn_ptr += bytes_read;
295 break;
297 case DW_CFA_advance_loc1:
298 utmp = extract_unsigned_integer (insn_ptr, 1, byte_order);
299 fs->pc += utmp * fs->code_align;
300 insn_ptr++;
301 break;
302 case DW_CFA_advance_loc2:
303 utmp = extract_unsigned_integer (insn_ptr, 2, byte_order);
304 fs->pc += utmp * fs->code_align;
305 insn_ptr += 2;
306 break;
307 case DW_CFA_advance_loc4:
308 utmp = extract_unsigned_integer (insn_ptr, 4, byte_order);
309 fs->pc += utmp * fs->code_align;
310 insn_ptr += 4;
311 break;
313 case DW_CFA_offset_extended:
314 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
315 reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
316 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
317 offset = utmp * fs->data_align;
318 fs->regs.alloc_regs (reg + 1);
319 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
320 fs->regs.reg[reg].loc.offset = offset;
321 break;
323 case DW_CFA_restore_extended:
324 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
325 dwarf2_restore_rule (gdbarch, reg, fs, eh_frame_p);
326 break;
328 case DW_CFA_undefined:
329 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
330 reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
331 fs->regs.alloc_regs (reg + 1);
332 fs->regs.reg[reg].how = DWARF2_FRAME_REG_UNDEFINED;
333 break;
335 case DW_CFA_same_value:
336 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
337 reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
338 fs->regs.alloc_regs (reg + 1);
339 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAME_VALUE;
340 break;
342 case DW_CFA_register:
343 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
344 reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
345 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
346 utmp = dwarf2_frame_adjust_regnum (gdbarch, utmp, eh_frame_p);
347 fs->regs.alloc_regs (reg + 1);
348 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_REG;
349 fs->regs.reg[reg].loc.reg = utmp;
350 break;
352 case DW_CFA_remember_state:
354 struct dwarf2_frame_state_reg_info *new_rs;
356 new_rs = new dwarf2_frame_state_reg_info (fs->regs);
357 fs->regs.prev = new_rs;
359 break;
361 case DW_CFA_restore_state:
363 struct dwarf2_frame_state_reg_info *old_rs = fs->regs.prev;
365 if (old_rs == NULL)
367 complaint (_("\
368 bad CFI data; mismatched DW_CFA_restore_state at %s"),
369 paddress (gdbarch, fs->pc));
371 else
372 fs->regs = std::move (*old_rs);
374 break;
376 case DW_CFA_def_cfa:
377 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
378 fs->regs.cfa_reg = reg;
379 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
381 if (fs->armcc_cfa_offsets_sf)
382 utmp *= fs->data_align;
384 fs->regs.cfa_offset = utmp;
385 fs->regs.cfa_how = CFA_REG_OFFSET;
386 break;
388 case DW_CFA_def_cfa_register:
389 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
390 fs->regs.cfa_reg = dwarf2_frame_adjust_regnum (gdbarch, reg,
391 eh_frame_p);
392 fs->regs.cfa_how = CFA_REG_OFFSET;
393 break;
395 case DW_CFA_def_cfa_offset:
396 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
398 if (fs->armcc_cfa_offsets_sf)
399 utmp *= fs->data_align;
401 fs->regs.cfa_offset = utmp;
402 /* cfa_how deliberately not set. */
403 break;
405 case DW_CFA_nop:
406 break;
408 case DW_CFA_def_cfa_expression:
409 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
410 fs->regs.cfa_exp_len = utmp;
411 fs->regs.cfa_exp = insn_ptr;
412 fs->regs.cfa_how = CFA_EXP;
413 insn_ptr += fs->regs.cfa_exp_len;
414 break;
416 case DW_CFA_expression:
417 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
418 reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
419 fs->regs.alloc_regs (reg + 1);
420 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
421 fs->regs.reg[reg].loc.exp.start = insn_ptr;
422 fs->regs.reg[reg].loc.exp.len = utmp;
423 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_EXP;
424 insn_ptr += utmp;
425 break;
427 case DW_CFA_offset_extended_sf:
428 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
429 reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
430 insn_ptr = safe_read_sleb128 (insn_ptr, insn_end, &offset);
431 offset *= fs->data_align;
432 fs->regs.alloc_regs (reg + 1);
433 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
434 fs->regs.reg[reg].loc.offset = offset;
435 break;
437 case DW_CFA_val_offset:
438 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
439 fs->regs.alloc_regs (reg + 1);
440 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
441 offset = utmp * fs->data_align;
442 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_VAL_OFFSET;
443 fs->regs.reg[reg].loc.offset = offset;
444 break;
446 case DW_CFA_val_offset_sf:
447 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
448 fs->regs.alloc_regs (reg + 1);
449 insn_ptr = safe_read_sleb128 (insn_ptr, insn_end, &offset);
450 offset *= fs->data_align;
451 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_VAL_OFFSET;
452 fs->regs.reg[reg].loc.offset = offset;
453 break;
455 case DW_CFA_val_expression:
456 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
457 fs->regs.alloc_regs (reg + 1);
458 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
459 fs->regs.reg[reg].loc.exp.start = insn_ptr;
460 fs->regs.reg[reg].loc.exp.len = utmp;
461 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_VAL_EXP;
462 insn_ptr += utmp;
463 break;
465 case DW_CFA_def_cfa_sf:
466 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
467 fs->regs.cfa_reg = dwarf2_frame_adjust_regnum (gdbarch, reg,
468 eh_frame_p);
469 insn_ptr = safe_read_sleb128 (insn_ptr, insn_end, &offset);
470 fs->regs.cfa_offset = offset * fs->data_align;
471 fs->regs.cfa_how = CFA_REG_OFFSET;
472 break;
474 case DW_CFA_def_cfa_offset_sf:
475 insn_ptr = safe_read_sleb128 (insn_ptr, insn_end, &offset);
476 fs->regs.cfa_offset = offset * fs->data_align;
477 /* cfa_how deliberately not set. */
478 break;
480 case DW_CFA_GNU_args_size:
481 /* Ignored. */
482 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
483 break;
485 case DW_CFA_GNU_negative_offset_extended:
486 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
487 reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
488 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
489 offset = utmp * fs->data_align;
490 fs->regs.alloc_regs (reg + 1);
491 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
492 fs->regs.reg[reg].loc.offset = -offset;
493 break;
495 default:
496 if (insn >= DW_CFA_lo_user && insn <= DW_CFA_hi_user)
498 /* Handle vendor-specific CFI for different architectures. */
499 if (!gdbarch_execute_dwarf_cfa_vendor_op (gdbarch, insn, fs))
500 error (_("Call Frame Instruction op %d in vendor extension "
501 "space is not handled on this architecture."),
502 insn);
504 else
505 internal_error (_("Unknown CFI encountered."));
510 if (fs->initial.reg.empty ())
512 /* Don't allow remember/restore between CIE and FDE programs. */
513 delete fs->regs.prev;
514 fs->regs.prev = NULL;
517 return insn_ptr;
520 #if GDB_SELF_TEST
522 namespace selftests {
524 /* Unit test to function execute_cfa_program. */
526 static void
527 execute_cfa_program_test (struct gdbarch *gdbarch)
529 struct dwarf2_fde fde;
530 struct dwarf2_cie cie;
532 memset (&fde, 0, sizeof fde);
533 memset (&cie, 0, sizeof cie);
535 cie.data_alignment_factor = -4;
536 cie.code_alignment_factor = 2;
537 fde.cie = &cie;
539 dwarf2_frame_state fs (0, fde.cie);
541 gdb_byte insns[] =
543 DW_CFA_def_cfa, 1, 4, /* DW_CFA_def_cfa: r1 ofs 4 */
544 DW_CFA_offset | 0x2, 1, /* DW_CFA_offset: r2 at cfa-4 */
545 DW_CFA_remember_state,
546 DW_CFA_restore_state,
549 const gdb_byte *insn_end = insns + sizeof (insns);
550 const gdb_byte *out = execute_cfa_program (&fde, insns, insn_end, gdbarch,
551 0, &fs, 0);
553 SELF_CHECK (out == insn_end);
554 SELF_CHECK (fs.pc == 0);
556 /* The instructions above only use r1 and r2, but the register numbers
557 used are adjusted by dwarf2_frame_adjust_regnum. */
558 auto r1 = dwarf2_frame_adjust_regnum (gdbarch, 1, fde.eh_frame_p);
559 auto r2 = dwarf2_frame_adjust_regnum (gdbarch, 2, fde.eh_frame_p);
561 SELF_CHECK (fs.regs.reg.size () == (std::max (r1, r2) + 1));
563 SELF_CHECK (fs.regs.reg[r2].how == DWARF2_FRAME_REG_SAVED_OFFSET);
564 SELF_CHECK (fs.regs.reg[r2].loc.offset == -4);
566 for (auto i = 0; i < fs.regs.reg.size (); i++)
567 if (i != r2)
568 SELF_CHECK (fs.regs.reg[i].how == DWARF2_FRAME_REG_UNSPECIFIED);
570 SELF_CHECK (fs.regs.cfa_reg == 1);
571 SELF_CHECK (fs.regs.cfa_offset == 4);
572 SELF_CHECK (fs.regs.cfa_how == CFA_REG_OFFSET);
573 SELF_CHECK (fs.regs.cfa_exp == NULL);
574 SELF_CHECK (fs.regs.prev == NULL);
577 } // namespace selftests
578 #endif /* GDB_SELF_TEST */
582 /* Architecture-specific operations. */
584 static void dwarf2_frame_default_init_reg (struct gdbarch *gdbarch,
585 int regnum,
586 struct dwarf2_frame_state_reg *reg,
587 const frame_info_ptr &this_frame);
589 struct dwarf2_frame_ops
591 /* Pre-initialize the register state REG for register REGNUM. */
592 void (*init_reg) (struct gdbarch *, int, struct dwarf2_frame_state_reg *,
593 const frame_info_ptr &)
594 = dwarf2_frame_default_init_reg;
596 /* Check whether the THIS_FRAME is a signal trampoline. */
597 int (*signal_frame_p) (struct gdbarch *, const frame_info_ptr &) = nullptr;
599 /* Convert .eh_frame register number to DWARF register number, or
600 adjust .debug_frame register number. */
601 int (*adjust_regnum) (struct gdbarch *, int, int) = nullptr;
604 /* Per-architecture data key. */
605 static const registry<gdbarch>::key<dwarf2_frame_ops> dwarf2_frame_data;
607 /* Get or initialize the frame ops. */
608 static dwarf2_frame_ops *
609 get_frame_ops (struct gdbarch *gdbarch)
611 dwarf2_frame_ops *result = dwarf2_frame_data.get (gdbarch);
612 if (result == nullptr)
613 result = dwarf2_frame_data.emplace (gdbarch);
614 return result;
617 /* Default architecture-specific register state initialization
618 function. */
620 static void
621 dwarf2_frame_default_init_reg (struct gdbarch *gdbarch, int regnum,
622 struct dwarf2_frame_state_reg *reg,
623 const frame_info_ptr &this_frame)
625 /* If we have a register that acts as a program counter, mark it as
626 a destination for the return address. If we have a register that
627 serves as the stack pointer, arrange for it to be filled with the
628 call frame address (CFA). The other registers are marked as
629 unspecified.
631 We copy the return address to the program counter, since many
632 parts in GDB assume that it is possible to get the return address
633 by unwinding the program counter register. However, on ISA's
634 with a dedicated return address register, the CFI usually only
635 contains information to unwind that return address register.
637 The reason we're treating the stack pointer special here is
638 because in many cases GCC doesn't emit CFI for the stack pointer
639 and implicitly assumes that it is equal to the CFA. This makes
640 some sense since the DWARF specification (version 3, draft 8,
641 p. 102) says that:
643 "Typically, the CFA is defined to be the value of the stack
644 pointer at the call site in the previous frame (which may be
645 different from its value on entry to the current frame)."
647 However, this isn't true for all platforms supported by GCC
648 (e.g. IBM S/390 and zSeries). Those architectures should provide
649 their own architecture-specific initialization function. */
651 if (regnum == gdbarch_pc_regnum (gdbarch))
652 reg->how = DWARF2_FRAME_REG_RA;
653 else if (regnum == gdbarch_sp_regnum (gdbarch))
654 reg->how = DWARF2_FRAME_REG_CFA;
657 /* Set the architecture-specific register state initialization
658 function for GDBARCH to INIT_REG. */
660 void
661 dwarf2_frame_set_init_reg (struct gdbarch *gdbarch,
662 void (*init_reg) (struct gdbarch *, int,
663 struct dwarf2_frame_state_reg *,
664 const frame_info_ptr &))
666 struct dwarf2_frame_ops *ops = get_frame_ops (gdbarch);
668 ops->init_reg = init_reg;
671 /* Pre-initialize the register state REG for register REGNUM. */
673 static void
674 dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
675 struct dwarf2_frame_state_reg *reg,
676 const frame_info_ptr &this_frame)
678 struct dwarf2_frame_ops *ops = get_frame_ops (gdbarch);
680 ops->init_reg (gdbarch, regnum, reg, this_frame);
683 /* Set the architecture-specific signal trampoline recognition
684 function for GDBARCH to SIGNAL_FRAME_P. */
686 void
687 dwarf2_frame_set_signal_frame_p (struct gdbarch *gdbarch,
688 int (*signal_frame_p) (struct gdbarch *,
689 const frame_info_ptr &))
691 struct dwarf2_frame_ops *ops = get_frame_ops (gdbarch);
693 ops->signal_frame_p = signal_frame_p;
696 /* Query the architecture-specific signal frame recognizer for
697 THIS_FRAME. */
699 static int
700 dwarf2_frame_signal_frame_p (struct gdbarch *gdbarch,
701 const frame_info_ptr &this_frame)
703 struct dwarf2_frame_ops *ops = get_frame_ops (gdbarch);
705 if (ops->signal_frame_p == NULL)
706 return 0;
707 return ops->signal_frame_p (gdbarch, this_frame);
710 /* Set the architecture-specific adjustment of .eh_frame and .debug_frame
711 register numbers. */
713 void
714 dwarf2_frame_set_adjust_regnum (struct gdbarch *gdbarch,
715 int (*adjust_regnum) (struct gdbarch *,
716 int, int))
718 struct dwarf2_frame_ops *ops = get_frame_ops (gdbarch);
720 ops->adjust_regnum = adjust_regnum;
723 /* Translate a .eh_frame register to DWARF register, or adjust a .debug_frame
724 register. */
726 static int
727 dwarf2_frame_adjust_regnum (struct gdbarch *gdbarch,
728 int regnum, int eh_frame_p)
730 struct dwarf2_frame_ops *ops = get_frame_ops (gdbarch);
732 if (ops->adjust_regnum == NULL)
733 return regnum;
734 return ops->adjust_regnum (gdbarch, regnum, eh_frame_p);
737 static void
738 dwarf2_frame_find_quirks (struct dwarf2_frame_state *fs,
739 struct dwarf2_fde *fde)
741 struct compunit_symtab *cust;
743 cust = find_pc_compunit_symtab (fs->pc);
744 if (cust == NULL)
745 return;
747 if (producer_is_realview (cust->producer ()))
749 if (fde->cie->version == 1)
750 fs->armcc_cfa_offsets_sf = 1;
752 if (fde->cie->version == 1)
753 fs->armcc_cfa_offsets_reversed = 1;
755 /* The reversed offset problem is present in some compilers
756 using DWARF3, but it was eventually fixed. Check the ARM
757 defined augmentations, which are in the format "armcc" followed
758 by a list of one-character options. The "+" option means
759 this problem is fixed (no quirk needed). If the armcc
760 augmentation is missing, the quirk is needed. */
761 if (fde->cie->version == 3
762 && (!startswith (fde->cie->augmentation, "armcc")
763 || strchr (fde->cie->augmentation + 5, '+') == NULL))
764 fs->armcc_cfa_offsets_reversed = 1;
766 return;
771 /* See dwarf2/frame.h. */
774 dwarf2_fetch_cfa_info (struct gdbarch *gdbarch, CORE_ADDR pc,
775 struct dwarf2_per_cu_data *data,
776 int *regnum_out, LONGEST *offset_out,
777 CORE_ADDR *text_offset_out,
778 const gdb_byte **cfa_start_out,
779 const gdb_byte **cfa_end_out)
781 struct dwarf2_fde *fde;
782 dwarf2_per_objfile *per_objfile;
783 CORE_ADDR pc1 = pc;
785 /* Find the correct FDE. */
786 fde = dwarf2_frame_find_fde (&pc1, &per_objfile);
787 if (fde == NULL)
788 error (_("Could not compute CFA; needed to translate this expression"));
790 gdb_assert (per_objfile != nullptr);
792 dwarf2_frame_state fs (pc1, fde->cie);
794 /* Check for "quirks" - known bugs in producers. */
795 dwarf2_frame_find_quirks (&fs, fde);
797 /* First decode all the insns in the CIE. */
798 execute_cfa_program (fde, fde->cie->initial_instructions,
799 fde->cie->end, gdbarch, pc, &fs,
800 per_objfile->objfile->text_section_offset ());
802 /* Save the initialized register set. */
803 fs.initial = fs.regs;
805 /* Then decode the insns in the FDE up to our target PC. */
806 execute_cfa_program (fde, fde->instructions, fde->end, gdbarch, pc, &fs,
807 per_objfile->objfile->text_section_offset ());
809 /* Calculate the CFA. */
810 switch (fs.regs.cfa_how)
812 case CFA_REG_OFFSET:
814 int regnum = dwarf_reg_to_regnum_or_error (gdbarch, fs.regs.cfa_reg);
816 *regnum_out = regnum;
817 if (fs.armcc_cfa_offsets_reversed)
818 *offset_out = -fs.regs.cfa_offset;
819 else
820 *offset_out = fs.regs.cfa_offset;
821 return 1;
824 case CFA_EXP:
825 *text_offset_out = per_objfile->objfile->text_section_offset ();
826 *cfa_start_out = fs.regs.cfa_exp;
827 *cfa_end_out = fs.regs.cfa_exp + fs.regs.cfa_exp_len;
828 return 0;
830 default:
831 internal_error (_("Unknown CFA rule."));
836 /* Custom function data object for architecture specific prev_register
837 implementation. Main purpose of this object is to allow caching of
838 expensive data lookups in the prev_register handling. */
840 struct dwarf2_frame_fn_data
842 /* The cookie to identify the custom function data by. */
843 fn_prev_register cookie;
845 /* The custom function data. */
846 void *data;
848 /* Pointer to the next custom function data object for this frame. */
849 struct dwarf2_frame_fn_data *next;
852 struct dwarf2_frame_cache
854 /* DWARF Call Frame Address. */
855 CORE_ADDR cfa;
857 /* Set if the return address column was marked as unavailable
858 (required non-collected memory or registers to compute). */
859 int unavailable_retaddr;
861 /* Set if the return address column was marked as undefined. */
862 int undefined_retaddr;
864 /* Saved registers, indexed by GDB register number, not by DWARF
865 register number. */
866 struct dwarf2_frame_state_reg *reg;
868 /* Return address register. */
869 struct dwarf2_frame_state_reg retaddr_reg;
871 /* Target address size in bytes. */
872 int addr_size;
874 /* The dwarf2_per_objfile from which this frame description came. */
875 dwarf2_per_objfile *per_objfile;
877 /* If not NULL then this frame is the bottom frame of a TAILCALL_FRAME
878 sequence. If NULL then it is a normal case with no TAILCALL_FRAME
879 involved. Non-bottom frames of a virtual tail call frames chain use
880 dwarf2_tailcall_frame_unwind unwinder so this field does not apply for
881 them. */
882 void *tailcall_cache;
884 struct dwarf2_frame_fn_data *fn_data;
887 static struct dwarf2_frame_cache *
888 dwarf2_frame_cache (const frame_info_ptr &this_frame, void **this_cache)
890 struct gdbarch *gdbarch = get_frame_arch (this_frame);
891 const int num_regs = gdbarch_num_cooked_regs (gdbarch);
892 struct dwarf2_frame_cache *cache;
893 struct dwarf2_fde *fde;
894 CORE_ADDR entry_pc;
895 const gdb_byte *instr;
897 if (*this_cache)
898 return (struct dwarf2_frame_cache *) *this_cache;
900 /* Allocate a new cache. */
901 cache = FRAME_OBSTACK_ZALLOC (struct dwarf2_frame_cache);
902 cache->reg = FRAME_OBSTACK_CALLOC (num_regs, struct dwarf2_frame_state_reg);
903 *this_cache = cache;
905 /* Unwind the PC.
907 Note that if the next frame is never supposed to return (i.e. a call
908 to abort), the compiler might optimize away the instruction at
909 its return address. As a result the return address will
910 point at some random instruction, and the CFI for that
911 instruction is probably worthless to us. GCC's unwinder solves
912 this problem by subtracting 1 from the return address to get an
913 address in the middle of a presumed call instruction (or the
914 instruction in the associated delay slot). This should only be
915 done for "normal" frames and not for resume-type frames (signal
916 handlers, sentinel frames, dummy frames). The function
917 get_frame_address_in_block does just this. It's not clear how
918 reliable the method is though; there is the potential for the
919 register state pre-call being different to that on return. */
920 CORE_ADDR pc1 = get_frame_address_in_block (this_frame);
922 /* Find the correct FDE. */
923 fde = dwarf2_frame_find_fde (&pc1, &cache->per_objfile);
924 gdb_assert (fde != NULL);
925 gdb_assert (cache->per_objfile != nullptr);
927 CORE_ADDR text_offset = cache->per_objfile->objfile->text_section_offset ();
929 /* Allocate and initialize the frame state. */
930 struct dwarf2_frame_state fs (pc1, fde->cie);
932 cache->addr_size = fde->cie->addr_size;
934 /* Check for "quirks" - known bugs in producers. */
935 dwarf2_frame_find_quirks (&fs, fde);
937 /* First decode all the insns in the CIE. */
938 execute_cfa_program (fde, fde->cie->initial_instructions,
939 fde->cie->end, gdbarch,
940 get_frame_address_in_block (this_frame), &fs,
941 text_offset);
943 /* Save the initialized register set. */
944 fs.initial = fs.regs;
946 /* Fetching the entry pc for THIS_FRAME won't necessarily result
947 in an address that's within the range of FDE locations. This
948 is due to the possibility of the function occupying non-contiguous
949 ranges. */
950 LONGEST entry_cfa_sp_offset;
951 int entry_cfa_sp_offset_p = 0;
952 if (get_frame_func_if_available (this_frame, &entry_pc)
953 && fde->initial_location <= (unrelocated_addr) (entry_pc - text_offset)
954 && (unrelocated_addr) (entry_pc - text_offset) < fde->end_addr ())
956 /* Decode the insns in the FDE up to the entry PC. */
957 instr = execute_cfa_program (fde, fde->instructions, fde->end, gdbarch,
958 entry_pc, &fs, text_offset);
960 if (fs.regs.cfa_how == CFA_REG_OFFSET
961 && (dwarf_reg_to_regnum (gdbarch, fs.regs.cfa_reg)
962 == gdbarch_sp_regnum (gdbarch)))
964 entry_cfa_sp_offset = fs.regs.cfa_offset;
965 entry_cfa_sp_offset_p = 1;
968 else
969 instr = fde->instructions;
971 /* Then decode the insns in the FDE up to our target PC. */
972 execute_cfa_program (fde, instr, fde->end, gdbarch,
973 get_frame_address_in_block (this_frame), &fs,
974 text_offset);
978 /* Calculate the CFA. */
979 switch (fs.regs.cfa_how)
981 case CFA_REG_OFFSET:
982 cache->cfa = read_addr_from_reg (this_frame, fs.regs.cfa_reg);
983 if (fs.armcc_cfa_offsets_reversed)
984 cache->cfa -= fs.regs.cfa_offset;
985 else
986 cache->cfa += fs.regs.cfa_offset;
987 break;
989 case CFA_EXP:
990 cache->cfa =
991 execute_stack_op (fs.regs.cfa_exp, fs.regs.cfa_exp_len,
992 cache->addr_size, this_frame, 0, 0,
993 cache->per_objfile);
994 break;
996 default:
997 internal_error (_("Unknown CFA rule."));
1000 catch (const gdb_exception_error &ex)
1002 if (ex.error == NOT_AVAILABLE_ERROR)
1004 cache->unavailable_retaddr = 1;
1005 return cache;
1008 throw;
1011 /* Initialize the register state. */
1013 int regnum;
1015 for (regnum = 0; regnum < num_regs; regnum++)
1016 dwarf2_frame_init_reg (gdbarch, regnum, &cache->reg[regnum], this_frame);
1019 /* Go through the DWARF2 CFI generated table and save its register
1020 location information in the cache. Note that we don't skip the
1021 return address column; it's perfectly all right for it to
1022 correspond to a real register. */
1024 int column; /* CFI speak for "register number". */
1026 for (column = 0; column < fs.regs.reg.size (); column++)
1028 /* Use the GDB register number as the destination index. */
1029 int regnum = dwarf_reg_to_regnum (gdbarch, column);
1031 /* Protect against a target returning a bad register. */
1032 if (regnum < 0 || regnum >= num_regs)
1033 continue;
1035 /* NOTE: cagney/2003-09-05: CFI should specify the disposition
1036 of all debug info registers. If it doesn't, complain (but
1037 not too loudly). It turns out that GCC assumes that an
1038 unspecified register implies "same value" when CFI (draft
1039 7) specifies nothing at all. Such a register could equally
1040 be interpreted as "undefined". Also note that this check
1041 isn't sufficient; it only checks that all registers in the
1042 range [0 .. max column] are specified, and won't detect
1043 problems when a debug info register falls outside of the
1044 table. We need a way of iterating through all the valid
1045 DWARF2 register numbers. */
1046 if (fs.regs.reg[column].how == DWARF2_FRAME_REG_UNSPECIFIED)
1048 if (cache->reg[regnum].how == DWARF2_FRAME_REG_UNSPECIFIED)
1049 complaint (_("\
1050 incomplete CFI data; unspecified registers (e.g., %s) at %s"),
1051 gdbarch_register_name (gdbarch, regnum),
1052 paddress (gdbarch, fs.pc));
1054 else
1055 cache->reg[regnum] = fs.regs.reg[column];
1059 /* Eliminate any DWARF2_FRAME_REG_RA rules, and save the information
1060 we need for evaluating DWARF2_FRAME_REG_RA_OFFSET rules. */
1062 int regnum;
1064 for (regnum = 0; regnum < num_regs; regnum++)
1066 if (cache->reg[regnum].how == DWARF2_FRAME_REG_RA
1067 || cache->reg[regnum].how == DWARF2_FRAME_REG_RA_OFFSET)
1069 const std::vector<struct dwarf2_frame_state_reg> &regs
1070 = fs.regs.reg;
1071 ULONGEST retaddr_column = fs.retaddr_column;
1073 /* It seems rather bizarre to specify an "empty" column as
1074 the return address column. However, this is exactly
1075 what GCC does on some targets. It turns out that GCC
1076 assumes that the return address can be found in the
1077 register corresponding to the return address column.
1078 Incidentally, that's how we should treat a return
1079 address column specifying "same value" too. */
1080 if (fs.retaddr_column < fs.regs.reg.size ()
1081 && regs[retaddr_column].how != DWARF2_FRAME_REG_UNSPECIFIED
1082 && regs[retaddr_column].how != DWARF2_FRAME_REG_SAME_VALUE)
1084 if (cache->reg[regnum].how == DWARF2_FRAME_REG_RA)
1085 cache->reg[regnum] = regs[retaddr_column];
1086 else
1087 cache->retaddr_reg = regs[retaddr_column];
1089 else
1091 if (cache->reg[regnum].how == DWARF2_FRAME_REG_RA)
1093 cache->reg[regnum].loc.reg = fs.retaddr_column;
1094 cache->reg[regnum].how = DWARF2_FRAME_REG_SAVED_REG;
1096 else
1098 cache->retaddr_reg.loc.reg = fs.retaddr_column;
1099 cache->retaddr_reg.how = DWARF2_FRAME_REG_SAVED_REG;
1106 if (fs.retaddr_column < fs.regs.reg.size ()
1107 && fs.regs.reg[fs.retaddr_column].how == DWARF2_FRAME_REG_UNDEFINED)
1108 cache->undefined_retaddr = 1;
1110 dwarf2_tailcall_sniffer_first (this_frame, &cache->tailcall_cache,
1111 (entry_cfa_sp_offset_p
1112 ? &entry_cfa_sp_offset : NULL));
1114 return cache;
1117 static enum unwind_stop_reason
1118 dwarf2_frame_unwind_stop_reason (const frame_info_ptr &this_frame,
1119 void **this_cache)
1121 struct dwarf2_frame_cache *cache
1122 = dwarf2_frame_cache (this_frame, this_cache);
1124 if (cache->unavailable_retaddr)
1125 return UNWIND_UNAVAILABLE;
1127 if (cache->undefined_retaddr)
1128 return UNWIND_OUTERMOST;
1130 return UNWIND_NO_REASON;
1133 static void
1134 dwarf2_frame_this_id (const frame_info_ptr &this_frame, void **this_cache,
1135 struct frame_id *this_id)
1137 struct dwarf2_frame_cache *cache =
1138 dwarf2_frame_cache (this_frame, this_cache);
1140 if (cache->unavailable_retaddr)
1141 (*this_id) = frame_id_build_unavailable_stack (get_frame_func (this_frame));
1142 else if (cache->undefined_retaddr)
1143 return;
1144 else
1145 (*this_id) = frame_id_build (cache->cfa, get_frame_func (this_frame));
1148 static struct value *
1149 dwarf2_frame_prev_register (const frame_info_ptr &this_frame, void **this_cache,
1150 int regnum)
1152 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1153 struct dwarf2_frame_cache *cache =
1154 dwarf2_frame_cache (this_frame, this_cache);
1155 CORE_ADDR addr;
1156 int realnum;
1158 /* Non-bottom frames of a virtual tail call frames chain use
1159 dwarf2_tailcall_frame_unwind unwinder so this code does not apply for
1160 them. If dwarf2_tailcall_prev_register_first does not have specific value
1161 unwind the register, tail call frames are assumed to have the register set
1162 of the top caller. */
1163 if (cache->tailcall_cache)
1165 struct value *val;
1167 val = dwarf2_tailcall_prev_register_first (this_frame,
1168 &cache->tailcall_cache,
1169 regnum);
1170 if (val)
1171 return val;
1174 switch (cache->reg[regnum].how)
1176 case DWARF2_FRAME_REG_UNDEFINED:
1177 /* If CFI explicitly specified that the value isn't defined,
1178 mark it as optimized away; the value isn't available. */
1179 return frame_unwind_got_optimized (this_frame, regnum);
1181 case DWARF2_FRAME_REG_SAVED_OFFSET:
1182 addr = cache->cfa + cache->reg[regnum].loc.offset;
1183 return frame_unwind_got_memory (this_frame, regnum, addr);
1185 case DWARF2_FRAME_REG_SAVED_REG:
1186 realnum = dwarf_reg_to_regnum_or_error
1187 (gdbarch, cache->reg[regnum].loc.reg);
1188 return frame_unwind_got_register (this_frame, regnum, realnum);
1190 case DWARF2_FRAME_REG_SAVED_EXP:
1191 addr = execute_stack_op (cache->reg[regnum].loc.exp.start,
1192 cache->reg[regnum].loc.exp.len,
1193 cache->addr_size,
1194 this_frame, cache->cfa, 1,
1195 cache->per_objfile);
1196 return frame_unwind_got_memory (this_frame, regnum, addr);
1198 case DWARF2_FRAME_REG_SAVED_VAL_OFFSET:
1199 addr = cache->cfa + cache->reg[regnum].loc.offset;
1200 return frame_unwind_got_constant (this_frame, regnum, addr);
1202 case DWARF2_FRAME_REG_SAVED_VAL_EXP:
1203 addr = execute_stack_op (cache->reg[regnum].loc.exp.start,
1204 cache->reg[regnum].loc.exp.len,
1205 cache->addr_size,
1206 this_frame, cache->cfa, 1,
1207 cache->per_objfile);
1208 return frame_unwind_got_constant (this_frame, regnum, addr);
1210 case DWARF2_FRAME_REG_UNSPECIFIED:
1211 /* GCC, in its infinite wisdom decided to not provide unwind
1212 information for registers that are "same value". Since
1213 DWARF2 (3 draft 7) doesn't define such behavior, said
1214 registers are actually undefined (which is different to CFI
1215 "undefined"). Code above issues a complaint about this.
1216 Here just fudge the books, assume GCC, and that the value is
1217 more inner on the stack. */
1218 if (regnum < gdbarch_num_regs (gdbarch))
1219 return frame_unwind_got_register (this_frame, regnum, regnum);
1220 else
1221 return nullptr;
1223 case DWARF2_FRAME_REG_SAME_VALUE:
1224 return frame_unwind_got_register (this_frame, regnum, regnum);
1226 case DWARF2_FRAME_REG_CFA:
1227 return frame_unwind_got_address (this_frame, regnum, cache->cfa);
1229 case DWARF2_FRAME_REG_CFA_OFFSET:
1230 addr = cache->cfa + cache->reg[regnum].loc.offset;
1231 return frame_unwind_got_address (this_frame, regnum, addr);
1233 case DWARF2_FRAME_REG_RA_OFFSET:
1234 addr = cache->reg[regnum].loc.offset;
1235 regnum = dwarf_reg_to_regnum_or_error
1236 (gdbarch, cache->retaddr_reg.loc.reg);
1237 addr += get_frame_register_unsigned (this_frame, regnum);
1238 return frame_unwind_got_address (this_frame, regnum, addr);
1240 case DWARF2_FRAME_REG_FN:
1241 return cache->reg[regnum].loc.fn (this_frame, this_cache, regnum);
1243 default:
1244 internal_error (_("Unknown register rule."));
1248 /* See frame.h. */
1250 void *
1251 dwarf2_frame_get_fn_data (const frame_info_ptr &this_frame, void **this_cache,
1252 fn_prev_register cookie)
1254 struct dwarf2_frame_fn_data *fn_data = nullptr;
1255 struct dwarf2_frame_cache *cache
1256 = dwarf2_frame_cache (this_frame, this_cache);
1258 /* Find the object for the function. */
1259 for (fn_data = cache->fn_data; fn_data; fn_data = fn_data->next)
1260 if (fn_data->cookie == cookie)
1261 return fn_data->data;
1263 return nullptr;
1266 /* See frame.h. */
1268 void *
1269 dwarf2_frame_allocate_fn_data (const frame_info_ptr &this_frame, void **this_cache,
1270 fn_prev_register cookie, unsigned long size)
1272 struct dwarf2_frame_fn_data *fn_data = nullptr;
1273 struct dwarf2_frame_cache *cache
1274 = dwarf2_frame_cache (this_frame, this_cache);
1276 /* First try to find an existing object. */
1277 void *data = dwarf2_frame_get_fn_data (this_frame, this_cache, cookie);
1278 gdb_assert (data == nullptr);
1280 /* No object found, lets create a new instance. */
1281 fn_data = FRAME_OBSTACK_ZALLOC (struct dwarf2_frame_fn_data);
1282 fn_data->cookie = cookie;
1283 fn_data->data = frame_obstack_zalloc (size);
1284 fn_data->next = cache->fn_data;
1285 cache->fn_data = fn_data;
1287 return fn_data->data;
1290 /* Proxy for tailcall_frame_dealloc_cache for bottom frame of a virtual tail
1291 call frames chain. */
1293 static void
1294 dwarf2_frame_dealloc_cache (frame_info *self, void *this_cache)
1296 struct dwarf2_frame_cache *cache
1297 = dwarf2_frame_cache (frame_info_ptr (self), &this_cache);
1299 if (cache->tailcall_cache)
1300 dwarf2_tailcall_frame_unwind.dealloc_cache (self, cache->tailcall_cache);
1303 static int
1304 dwarf2_frame_sniffer (const struct frame_unwind *self,
1305 const frame_info_ptr &this_frame, void **this_cache)
1307 /* Grab an address that is guaranteed to reside somewhere within the
1308 function. get_frame_pc(), with a no-return next function, can
1309 end up returning something past the end of this function's body.
1310 If the frame we're sniffing for is a signal frame whose start
1311 address is placed on the stack by the OS, its FDE must
1312 extend one byte before its start address or we could potentially
1313 select the FDE of the previous function. */
1314 CORE_ADDR block_addr = get_frame_address_in_block (this_frame);
1315 struct dwarf2_fde *fde = dwarf2_frame_find_fde (&block_addr, NULL);
1317 if (!fde)
1318 return 0;
1320 /* On some targets, signal trampolines may have unwind information.
1321 We need to recognize them so that we set the frame type
1322 correctly. */
1324 if (fde->cie->signal_frame
1325 || dwarf2_frame_signal_frame_p (get_frame_arch (this_frame),
1326 this_frame))
1327 return self->type () == SIGTRAMP_FRAME;
1329 if (self->type () != NORMAL_FRAME)
1330 return 0;
1332 return 1;
1335 static const struct frame_unwind_legacy dwarf2_frame_unwind (
1336 "dwarf2",
1337 NORMAL_FRAME,
1338 FRAME_UNWIND_DEBUGINFO,
1339 dwarf2_frame_unwind_stop_reason,
1340 dwarf2_frame_this_id,
1341 dwarf2_frame_prev_register,
1342 NULL,
1343 dwarf2_frame_sniffer,
1344 dwarf2_frame_dealloc_cache
1347 static const struct frame_unwind_legacy dwarf2_signal_frame_unwind (
1348 "dwarf2 signal",
1349 SIGTRAMP_FRAME,
1350 FRAME_UNWIND_DEBUGINFO,
1351 dwarf2_frame_unwind_stop_reason,
1352 dwarf2_frame_this_id,
1353 dwarf2_frame_prev_register,
1354 NULL,
1355 dwarf2_frame_sniffer,
1357 /* TAILCALL_CACHE can never be in such frame to need dealloc_cache. */
1358 NULL
1361 /* Append the DWARF-2 frame unwinders to GDBARCH's list. */
1363 void
1364 dwarf2_append_unwinders (struct gdbarch *gdbarch)
1366 frame_unwind_append_unwinder (gdbarch, &dwarf2_frame_unwind);
1367 frame_unwind_append_unwinder (gdbarch, &dwarf2_signal_frame_unwind);
1371 /* There is no explicitly defined relationship between the CFA and the
1372 location of frame's local variables and arguments/parameters.
1373 Therefore, frame base methods on this page should probably only be
1374 used as a last resort, just to avoid printing total garbage as a
1375 response to the "info frame" command. */
1377 static CORE_ADDR
1378 dwarf2_frame_base_address (const frame_info_ptr &this_frame, void **this_cache)
1380 struct dwarf2_frame_cache *cache =
1381 dwarf2_frame_cache (this_frame, this_cache);
1383 return cache->cfa;
1386 static const struct frame_base dwarf2_frame_base =
1388 &dwarf2_frame_unwind,
1389 dwarf2_frame_base_address,
1390 dwarf2_frame_base_address,
1391 dwarf2_frame_base_address
1394 const struct frame_base *
1395 dwarf2_frame_base_sniffer (const frame_info_ptr &this_frame)
1397 CORE_ADDR block_addr = get_frame_address_in_block (this_frame);
1399 if (dwarf2_frame_find_fde (&block_addr, NULL))
1400 return &dwarf2_frame_base;
1402 return NULL;
1405 /* Compute the CFA for THIS_FRAME, but only if THIS_FRAME came from
1406 the DWARF unwinder. This is used to implement
1407 DW_OP_call_frame_cfa. */
1409 CORE_ADDR
1410 dwarf2_frame_cfa (const frame_info_ptr &initial_this_frame)
1412 frame_info_ptr this_frame = initial_this_frame;
1414 if (frame_unwinder_is (this_frame, &record_btrace_tailcall_frame_unwind)
1415 || frame_unwinder_is (this_frame, &record_btrace_frame_unwind))
1416 throw_error (NOT_AVAILABLE_ERROR,
1417 _("cfa not available for record btrace target"));
1419 while (get_frame_type (this_frame) == INLINE_FRAME)
1420 this_frame = get_prev_frame_always (this_frame);
1421 if (get_frame_unwind_stop_reason (this_frame) == UNWIND_UNAVAILABLE)
1422 throw_error (NOT_AVAILABLE_ERROR,
1423 _("can't compute CFA for this frame: "
1424 "required registers or memory are unavailable"));
1426 if (get_frame_id (this_frame).stack_status != FID_STACK_VALID)
1427 throw_error (NOT_AVAILABLE_ERROR,
1428 _("can't compute CFA for this frame: "
1429 "frame base not available"));
1431 return get_frame_base (this_frame);
1434 /* We store the frame data on the BFD. This is only done if it is
1435 independent of the address space and so can be shared. */
1436 static const registry<bfd>::key<comp_unit> dwarf2_frame_bfd_data;
1438 /* If any BFD sections require relocations (note; really should be if
1439 any debug info requires relocations), then we store the frame data
1440 on the objfile instead, and do not share it. */
1441 static const registry<objfile>::key<comp_unit> dwarf2_frame_objfile_data;
1444 /* Pointer encoding helper functions. */
1446 /* GCC supports exception handling based on DWARF2 CFI. However, for
1447 technical reasons, it encodes addresses in its FDE's in a different
1448 way. Several "pointer encodings" are supported. The encoding
1449 that's used for a particular FDE is determined by the 'R'
1450 augmentation in the associated CIE. The argument of this
1451 augmentation is a single byte.
1453 The address can be encoded as 2 bytes, 4 bytes, 8 bytes, or as a
1454 LEB128. This is encoded in bits 0, 1 and 2. Bit 3 encodes whether
1455 the address is signed or unsigned. Bits 4, 5 and 6 encode how the
1456 address should be interpreted (absolute, relative to the current
1457 position in the FDE, ...). Bit 7, indicates that the address
1458 should be dereferenced. */
1460 static gdb_byte
1461 encoding_for_size (unsigned int size)
1463 switch (size)
1465 case 2:
1466 return DW_EH_PE_udata2;
1467 case 4:
1468 return DW_EH_PE_udata4;
1469 case 8:
1470 return DW_EH_PE_udata8;
1471 default:
1472 internal_error (_("Unsupported address size"));
1476 static ULONGEST
1477 read_encoded_value (struct comp_unit *unit, gdb_byte encoding,
1478 int ptr_len, const gdb_byte *buf,
1479 unsigned int *bytes_read_ptr,
1480 unrelocated_addr func_base)
1482 ptrdiff_t offset;
1483 ULONGEST base;
1485 /* GCC currently doesn't generate DW_EH_PE_indirect encodings for
1486 FDE's. */
1487 if (encoding & DW_EH_PE_indirect)
1488 internal_error (_("Unsupported encoding: DW_EH_PE_indirect"));
1490 *bytes_read_ptr = 0;
1492 switch (encoding & 0x70)
1494 case DW_EH_PE_absptr:
1495 base = 0;
1496 break;
1497 case DW_EH_PE_pcrel:
1498 base = bfd_section_vma (unit->dwarf_frame_section);
1499 base += (buf - unit->dwarf_frame_buffer);
1500 break;
1501 case DW_EH_PE_datarel:
1502 base = unit->dbase;
1503 break;
1504 case DW_EH_PE_textrel:
1505 base = unit->tbase;
1506 break;
1507 case DW_EH_PE_funcrel:
1508 base = (ULONGEST) func_base;
1509 break;
1510 case DW_EH_PE_aligned:
1511 base = 0;
1512 offset = buf - unit->dwarf_frame_buffer;
1513 if ((offset % ptr_len) != 0)
1515 *bytes_read_ptr = ptr_len - (offset % ptr_len);
1516 buf += *bytes_read_ptr;
1518 break;
1519 default:
1520 internal_error (_("Invalid or unsupported encoding"));
1523 if ((encoding & 0x07) == 0x00)
1525 encoding |= encoding_for_size (ptr_len);
1526 if (bfd_get_sign_extend_vma (unit->abfd))
1527 encoding |= DW_EH_PE_signed;
1530 switch (encoding & 0x0f)
1532 case DW_EH_PE_uleb128:
1534 uint64_t value;
1535 const gdb_byte *end_buf = buf + (sizeof (value) + 1) * 8 / 7;
1537 *bytes_read_ptr += safe_read_uleb128 (buf, end_buf, &value) - buf;
1538 return base + value;
1540 case DW_EH_PE_udata2:
1541 *bytes_read_ptr += 2;
1542 return (base + bfd_get_16 (unit->abfd, (bfd_byte *) buf));
1543 case DW_EH_PE_udata4:
1544 *bytes_read_ptr += 4;
1545 return (base + bfd_get_32 (unit->abfd, (bfd_byte *) buf));
1546 case DW_EH_PE_udata8:
1547 *bytes_read_ptr += 8;
1548 return (base + bfd_get_64 (unit->abfd, (bfd_byte *) buf));
1549 case DW_EH_PE_sleb128:
1551 int64_t value;
1552 const gdb_byte *end_buf = buf + (sizeof (value) + 1) * 8 / 7;
1554 *bytes_read_ptr += safe_read_sleb128 (buf, end_buf, &value) - buf;
1555 return base + value;
1557 case DW_EH_PE_sdata2:
1558 *bytes_read_ptr += 2;
1559 return (base + bfd_get_signed_16 (unit->abfd, (bfd_byte *) buf));
1560 case DW_EH_PE_sdata4:
1561 *bytes_read_ptr += 4;
1562 return (base + bfd_get_signed_32 (unit->abfd, (bfd_byte *) buf));
1563 case DW_EH_PE_sdata8:
1564 *bytes_read_ptr += 8;
1565 return (base + bfd_get_signed_64 (unit->abfd, (bfd_byte *) buf));
1566 default:
1567 internal_error (_("Invalid or unsupported encoding"));
1572 /* Find CIE with the given CIE_POINTER in CIE_TABLE. */
1573 static struct dwarf2_cie *
1574 find_cie (const dwarf2_cie_table &cie_table, ULONGEST cie_pointer)
1576 auto iter = cie_table.find (cie_pointer);
1577 if (iter != cie_table.end ())
1578 return iter->second;
1579 return NULL;
1582 static inline int
1583 bsearch_fde_cmp (const dwarf2_fde *fde, unrelocated_addr seek_pc)
1585 if (fde->end_addr () <= seek_pc)
1586 return -1;
1587 if (fde->initial_location <= seek_pc)
1588 return 0;
1589 return 1;
1592 /* Find an existing comp_unit for an objfile, if any. */
1594 static comp_unit *
1595 find_comp_unit (struct objfile *objfile)
1597 bfd *abfd = objfile->obfd.get ();
1598 if (gdb_bfd_requires_relocations (abfd))
1599 return dwarf2_frame_objfile_data.get (objfile);
1601 return dwarf2_frame_bfd_data.get (abfd);
1604 /* Store the comp_unit on OBJFILE, or the corresponding BFD, as
1605 appropriate. */
1607 static void
1608 set_comp_unit (struct objfile *objfile, struct comp_unit *unit)
1610 bfd *abfd = objfile->obfd.get ();
1611 if (gdb_bfd_requires_relocations (abfd))
1612 return dwarf2_frame_objfile_data.set (objfile, unit);
1614 return dwarf2_frame_bfd_data.set (abfd, unit);
1617 /* Find the FDE for *PC. Return a pointer to the FDE, and store the
1618 initial location associated with it into *PC. */
1620 static struct dwarf2_fde *
1621 dwarf2_frame_find_fde (CORE_ADDR *pc, dwarf2_per_objfile **out_per_objfile)
1623 for (objfile *objfile : current_program_space->objfiles ())
1625 CORE_ADDR offset;
1627 if (objfile->obfd == nullptr)
1628 continue;
1630 comp_unit *unit = find_comp_unit (objfile);
1631 if (unit == NULL)
1633 dwarf2_build_frame_info (objfile);
1634 unit = find_comp_unit (objfile);
1636 gdb_assert (unit != NULL);
1638 dwarf2_fde_table *fde_table = &unit->fde_table;
1639 if (fde_table->empty ())
1640 continue;
1642 gdb_assert (!objfile->section_offsets.empty ());
1643 offset = objfile->text_section_offset ();
1645 gdb_assert (!fde_table->empty ());
1646 unrelocated_addr seek_pc = (unrelocated_addr) (*pc - offset);
1647 if (seek_pc < (*fde_table)[0]->initial_location)
1648 continue;
1650 auto it = gdb::binary_search (fde_table->begin (), fde_table->end (),
1651 seek_pc, bsearch_fde_cmp);
1652 if (it != fde_table->end ())
1654 *pc = (CORE_ADDR) (*it)->initial_location + offset;
1655 if (out_per_objfile != nullptr)
1656 *out_per_objfile = get_dwarf2_per_objfile (objfile);
1658 return *it;
1661 return NULL;
1664 /* Add FDE to FDE_TABLE. */
1665 static void
1666 add_fde (dwarf2_fde_table *fde_table, struct dwarf2_fde *fde)
1668 if (fde->address_range == 0)
1669 /* Discard useless FDEs. */
1670 return;
1672 fde_table->push_back (fde);
1675 #define DW64_CIE_ID 0xffffffffffffffffULL
1677 /* Defines the type of eh_frames that are expected to be decoded: CIE, FDE
1678 or any of them. */
1680 enum eh_frame_type
1682 EH_CIE_TYPE_ID = 1 << 0,
1683 EH_FDE_TYPE_ID = 1 << 1,
1684 EH_CIE_OR_FDE_TYPE_ID = EH_CIE_TYPE_ID | EH_FDE_TYPE_ID
1687 static const gdb_byte *decode_frame_entry (struct gdbarch *gdbarch,
1688 struct comp_unit *unit,
1689 const gdb_byte *start,
1690 int eh_frame_p,
1691 dwarf2_cie_table &cie_table,
1692 dwarf2_fde_table *fde_table,
1693 enum eh_frame_type entry_type);
1695 /* Decode the next CIE or FDE, entry_type specifies the expected type.
1696 Return NULL if invalid input, otherwise the next byte to be processed. */
1698 static const gdb_byte *
1699 decode_frame_entry_1 (struct gdbarch *gdbarch,
1700 struct comp_unit *unit, const gdb_byte *start,
1701 int eh_frame_p,
1702 dwarf2_cie_table &cie_table,
1703 dwarf2_fde_table *fde_table,
1704 enum eh_frame_type entry_type)
1706 const gdb_byte *buf, *end;
1707 ULONGEST length;
1708 unsigned int bytes_read;
1709 int dwarf64_p;
1710 ULONGEST cie_id;
1711 ULONGEST cie_pointer;
1712 int64_t sleb128;
1713 uint64_t uleb128;
1715 buf = start;
1716 length = read_initial_length (unit->abfd, buf, &bytes_read, false);
1717 buf += bytes_read;
1718 end = buf + (size_t) length;
1720 if (length == 0)
1721 return end;
1723 /* Are we still within the section? */
1724 if (end <= buf || end > unit->dwarf_frame_buffer + unit->dwarf_frame_size)
1725 return NULL;
1727 /* Distinguish between 32 and 64-bit encoded frame info. */
1728 dwarf64_p = (bytes_read == 12);
1730 /* In a .eh_frame section, zero is used to distinguish CIEs from FDEs. */
1731 if (eh_frame_p)
1732 cie_id = 0;
1733 else if (dwarf64_p)
1734 cie_id = DW64_CIE_ID;
1735 else
1736 cie_id = DW_CIE_ID;
1738 if (dwarf64_p)
1740 cie_pointer = read_8_bytes (unit->abfd, buf);
1741 buf += 8;
1743 else
1745 cie_pointer = read_4_bytes (unit->abfd, buf);
1746 buf += 4;
1749 if (cie_pointer == cie_id)
1751 /* This is a CIE. */
1752 struct dwarf2_cie *cie;
1753 const char *augmentation;
1754 unsigned int cie_version;
1756 /* Check that a CIE was expected. */
1757 if ((entry_type & EH_CIE_TYPE_ID) == 0)
1758 error (_("Found a CIE when not expecting it."));
1760 /* Record the offset into the .debug_frame section of this CIE. */
1761 cie_pointer = start - unit->dwarf_frame_buffer;
1763 /* Check whether we've already read it. */
1764 if (find_cie (cie_table, cie_pointer))
1765 return end;
1767 cie = XOBNEW (&unit->obstack, struct dwarf2_cie);
1768 cie->initial_instructions = NULL;
1769 cie->cie_pointer = cie_pointer;
1771 /* The encoding for FDE's in a normal .debug_frame section
1772 depends on the target address size. */
1773 cie->encoding = DW_EH_PE_absptr;
1775 /* We'll determine the final value later, but we need to
1776 initialize it conservatively. */
1777 cie->signal_frame = 0;
1779 /* Check version number. */
1780 cie_version = read_1_byte (unit->abfd, buf);
1781 if (cie_version != 1 && cie_version != 3 && cie_version != 4)
1782 return NULL;
1783 cie->version = cie_version;
1784 buf += 1;
1786 /* Interpret the interesting bits of the augmentation. */
1787 cie->augmentation = augmentation = (const char *) buf;
1788 buf += (strlen (augmentation) + 1);
1790 /* Ignore armcc augmentations. We only use them for quirks,
1791 and that doesn't happen until later. */
1792 if (startswith (augmentation, "armcc"))
1793 augmentation += strlen (augmentation);
1795 /* The GCC 2.x "eh" augmentation has a pointer immediately
1796 following the augmentation string, so it must be handled
1797 first. */
1798 if (augmentation[0] == 'e' && augmentation[1] == 'h')
1800 /* Skip. */
1801 buf += gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT;
1802 augmentation += 2;
1805 if (cie->version >= 4)
1807 /* FIXME: check that this is the same as from the CU header. */
1808 cie->addr_size = read_1_byte (unit->abfd, buf);
1809 ++buf;
1810 cie->segment_size = read_1_byte (unit->abfd, buf);
1811 ++buf;
1813 else
1815 cie->addr_size = gdbarch_dwarf2_addr_size (gdbarch);
1816 cie->segment_size = 0;
1818 /* Address values in .eh_frame sections are defined to have the
1819 target's pointer size. Watchout: This breaks frame info for
1820 targets with pointer size < address size, unless a .debug_frame
1821 section exists as well. */
1822 if (eh_frame_p)
1823 cie->ptr_size = gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT;
1824 else
1825 cie->ptr_size = cie->addr_size;
1827 buf = gdb_read_uleb128 (buf, end, &uleb128);
1828 if (buf == NULL)
1829 return NULL;
1830 cie->code_alignment_factor = uleb128;
1832 buf = gdb_read_sleb128 (buf, end, &sleb128);
1833 if (buf == NULL)
1834 return NULL;
1835 cie->data_alignment_factor = sleb128;
1837 if (cie_version == 1)
1839 cie->return_address_register = read_1_byte (unit->abfd, buf);
1840 ++buf;
1842 else
1844 buf = gdb_read_uleb128 (buf, end, &uleb128);
1845 if (buf == NULL)
1846 return NULL;
1847 cie->return_address_register = uleb128;
1850 cie->return_address_register
1851 = dwarf2_frame_adjust_regnum (gdbarch,
1852 cie->return_address_register,
1853 eh_frame_p);
1855 cie->saw_z_augmentation = (*augmentation == 'z');
1856 if (cie->saw_z_augmentation)
1858 uint64_t uleb_length;
1860 buf = gdb_read_uleb128 (buf, end, &uleb_length);
1861 if (buf == NULL)
1862 return NULL;
1863 cie->initial_instructions = buf + uleb_length;
1864 augmentation++;
1867 while (*augmentation)
1869 /* "L" indicates a byte showing how the LSDA pointer is encoded. */
1870 if (*augmentation == 'L')
1872 /* Skip. */
1873 buf++;
1874 augmentation++;
1877 /* "R" indicates a byte indicating how FDE addresses are encoded. */
1878 else if (*augmentation == 'R')
1880 cie->encoding = *buf++;
1881 augmentation++;
1884 /* "P" indicates a personality routine in the CIE augmentation. */
1885 else if (*augmentation == 'P')
1887 /* Skip. Avoid indirection since we throw away the result. */
1888 gdb_byte encoding = (*buf++) & ~DW_EH_PE_indirect;
1889 read_encoded_value (unit, encoding, cie->ptr_size,
1890 buf, &bytes_read, (unrelocated_addr) 0);
1891 buf += bytes_read;
1892 augmentation++;
1895 /* "S" indicates a signal frame, such that the return
1896 address must not be decremented to locate the call frame
1897 info for the previous frame; it might even be the first
1898 instruction of a function, so decrementing it would take
1899 us to a different function. */
1900 else if (*augmentation == 'S')
1902 cie->signal_frame = 1;
1903 augmentation++;
1906 /* Otherwise we have an unknown augmentation. Assume that either
1907 there is no augmentation data, or we saw a 'z' prefix. */
1908 else
1910 if (cie->initial_instructions)
1911 buf = cie->initial_instructions;
1912 break;
1916 cie->initial_instructions = buf;
1917 cie->end = end;
1918 cie->unit = unit;
1920 cie_table[cie->cie_pointer] = cie;
1922 else
1924 /* This is a FDE. */
1925 struct dwarf2_fde *fde;
1927 /* Check that an FDE was expected. */
1928 if ((entry_type & EH_FDE_TYPE_ID) == 0)
1929 error (_("Found an FDE when not expecting it."));
1931 /* In an .eh_frame section, the CIE pointer is the delta between the
1932 address within the FDE where the CIE pointer is stored and the
1933 address of the CIE. Convert it to an offset into the .eh_frame
1934 section. */
1935 if (eh_frame_p)
1937 cie_pointer = buf - unit->dwarf_frame_buffer - cie_pointer;
1938 cie_pointer -= (dwarf64_p ? 8 : 4);
1941 /* In either case, validate the result is still within the section. */
1942 if (cie_pointer >= unit->dwarf_frame_size)
1943 return NULL;
1945 fde = XOBNEW (&unit->obstack, struct dwarf2_fde);
1946 fde->cie = find_cie (cie_table, cie_pointer);
1947 if (fde->cie == NULL)
1949 decode_frame_entry (gdbarch, unit,
1950 unit->dwarf_frame_buffer + cie_pointer,
1951 eh_frame_p, cie_table, fde_table,
1952 EH_CIE_TYPE_ID);
1953 fde->cie = find_cie (cie_table, cie_pointer);
1956 gdb_assert (fde->cie != NULL);
1958 ULONGEST init_addr
1959 = read_encoded_value (unit, fde->cie->encoding, fde->cie->ptr_size,
1960 buf, &bytes_read, (unrelocated_addr) 0);
1961 fde->initial_location
1962 = (unrelocated_addr) gdbarch_adjust_dwarf2_addr (gdbarch, init_addr);
1963 buf += bytes_read;
1965 ULONGEST range
1966 = read_encoded_value (unit, fde->cie->encoding & 0x0f,
1967 fde->cie->ptr_size, buf, &bytes_read,
1968 (unrelocated_addr) 0);
1969 ULONGEST addr = gdbarch_adjust_dwarf2_addr (gdbarch, init_addr + range);
1970 fde->address_range = addr - (ULONGEST) fde->initial_location;
1971 buf += bytes_read;
1973 /* A 'z' augmentation in the CIE implies the presence of an
1974 augmentation field in the FDE as well. The only thing known
1975 to be in here at present is the LSDA entry for EH. So we
1976 can skip the whole thing. */
1977 if (fde->cie->saw_z_augmentation)
1979 uint64_t uleb_length;
1981 buf = gdb_read_uleb128 (buf, end, &uleb_length);
1982 if (buf == NULL)
1983 return NULL;
1984 buf += uleb_length;
1985 if (buf > end)
1986 return NULL;
1989 fde->instructions = buf;
1990 fde->end = end;
1992 fde->eh_frame_p = eh_frame_p;
1994 add_fde (fde_table, fde);
1997 return end;
2000 /* Read a CIE or FDE in BUF and decode it. Entry_type specifies whether we
2001 expect an FDE or a CIE. */
2003 static const gdb_byte *
2004 decode_frame_entry (struct gdbarch *gdbarch,
2005 struct comp_unit *unit, const gdb_byte *start,
2006 int eh_frame_p,
2007 dwarf2_cie_table &cie_table,
2008 dwarf2_fde_table *fde_table,
2009 enum eh_frame_type entry_type)
2011 enum { NONE, ALIGN4, ALIGN8, FAIL } workaround = NONE;
2012 const gdb_byte *ret;
2013 ptrdiff_t start_offset;
2015 while (1)
2017 ret = decode_frame_entry_1 (gdbarch, unit, start, eh_frame_p,
2018 cie_table, fde_table, entry_type);
2019 if (ret != NULL)
2020 break;
2022 /* We have corrupt input data of some form. */
2024 /* ??? Try, weakly, to work around compiler/assembler/linker bugs
2025 and mismatches wrt padding and alignment of debug sections. */
2026 /* Note that there is no requirement in the standard for any
2027 alignment at all in the frame unwind sections. Testing for
2028 alignment before trying to interpret data would be incorrect.
2030 However, GCC traditionally arranged for frame sections to be
2031 sized such that the FDE length and CIE fields happen to be
2032 aligned (in theory, for performance). This, unfortunately,
2033 was done with .align directives, which had the side effect of
2034 forcing the section to be aligned by the linker.
2036 This becomes a problem when you have some other producer that
2037 creates frame sections that are not as strictly aligned. That
2038 produces a hole in the frame info that gets filled by the
2039 linker with zeros.
2041 The GCC behavior is arguably a bug, but it's effectively now
2042 part of the ABI, so we're now stuck with it, at least at the
2043 object file level. A smart linker may decide, in the process
2044 of compressing duplicate CIE information, that it can rewrite
2045 the entire output section without this extra padding. */
2047 start_offset = start - unit->dwarf_frame_buffer;
2048 if (workaround < ALIGN4 && (start_offset & 3) != 0)
2050 start += 4 - (start_offset & 3);
2051 workaround = ALIGN4;
2052 continue;
2054 if (workaround < ALIGN8 && (start_offset & 7) != 0)
2056 start += 8 - (start_offset & 7);
2057 workaround = ALIGN8;
2058 continue;
2061 /* Nothing left to try. Arrange to return as if we've consumed
2062 the entire input section. Hopefully we'll get valid info from
2063 the other of .debug_frame/.eh_frame. */
2064 workaround = FAIL;
2065 ret = unit->dwarf_frame_buffer + unit->dwarf_frame_size;
2066 break;
2069 switch (workaround)
2071 case NONE:
2072 break;
2074 case ALIGN4:
2075 complaint (_("\
2076 Corrupt data in %s:%s; align 4 workaround apparently succeeded"),
2077 bfd_get_filename (unit->dwarf_frame_section->owner),
2078 bfd_section_name (unit->dwarf_frame_section));
2079 break;
2081 case ALIGN8:
2082 complaint (_("\
2083 Corrupt data in %s:%s; align 8 workaround apparently succeeded"),
2084 bfd_get_filename (unit->dwarf_frame_section->owner),
2085 bfd_section_name (unit->dwarf_frame_section));
2086 break;
2088 default:
2089 complaint (_("Corrupt data in %s:%s"),
2090 bfd_get_filename (unit->dwarf_frame_section->owner),
2091 bfd_section_name (unit->dwarf_frame_section));
2092 break;
2095 return ret;
2098 static bool
2099 fde_is_less_than (const dwarf2_fde *aa, const dwarf2_fde *bb)
2101 if (aa->initial_location == bb->initial_location)
2103 if (aa->address_range != bb->address_range
2104 && aa->eh_frame_p == 0 && bb->eh_frame_p == 0)
2105 /* Linker bug, e.g. gold/10400.
2106 Work around it by keeping stable sort order. */
2107 return aa < bb;
2108 else
2109 /* Put eh_frame entries after debug_frame ones. */
2110 return aa->eh_frame_p < bb->eh_frame_p;
2113 return aa->initial_location < bb->initial_location;
2116 void
2117 dwarf2_build_frame_info (struct objfile *objfile)
2119 const gdb_byte *frame_ptr;
2120 dwarf2_cie_table cie_table;
2121 dwarf2_fde_table fde_table;
2123 struct gdbarch *gdbarch = objfile->arch ();
2125 /* Build a minimal decoding of the DWARF2 compilation unit. */
2126 auto unit = std::make_unique<comp_unit> (objfile);
2128 if (objfile->separate_debug_objfile_backlink == NULL)
2130 /* Do not read .eh_frame from separate file as they must be also
2131 present in the main file. */
2132 dwarf2_get_section_info (objfile, DWARF2_EH_FRAME,
2133 &unit->dwarf_frame_section,
2134 &unit->dwarf_frame_buffer,
2135 &unit->dwarf_frame_size);
2136 if (unit->dwarf_frame_size)
2138 asection *got, *txt;
2140 /* FIXME: kettenis/20030602: This is the DW_EH_PE_datarel base
2141 that is used for the i386/amd64 target, which currently is
2142 the only target in GCC that supports/uses the
2143 DW_EH_PE_datarel encoding. */
2144 got = bfd_get_section_by_name (unit->abfd, ".got");
2145 if (got)
2146 unit->dbase = got->vma;
2148 /* GCC emits the DW_EH_PE_textrel encoding type on sh and ia64
2149 so far. */
2150 txt = bfd_get_section_by_name (unit->abfd, ".text");
2151 if (txt)
2152 unit->tbase = txt->vma;
2156 frame_ptr = unit->dwarf_frame_buffer;
2157 while (frame_ptr < unit->dwarf_frame_buffer + unit->dwarf_frame_size)
2158 frame_ptr = decode_frame_entry (gdbarch, unit.get (),
2159 frame_ptr, 1,
2160 cie_table, &fde_table,
2161 EH_CIE_OR_FDE_TYPE_ID);
2164 catch (const gdb_exception_error &e)
2166 warning (_("skipping .eh_frame info of %s: %s"),
2167 objfile_name (objfile), e.what ());
2169 fde_table.clear ();
2170 /* The cie_table is discarded below. */
2173 cie_table.clear ();
2177 dwarf2_get_section_info (objfile, DWARF2_DEBUG_FRAME,
2178 &unit->dwarf_frame_section,
2179 &unit->dwarf_frame_buffer,
2180 &unit->dwarf_frame_size);
2181 if (unit->dwarf_frame_size)
2183 size_t num_old_fde_entries = fde_table.size ();
2187 frame_ptr = unit->dwarf_frame_buffer;
2188 while (frame_ptr < unit->dwarf_frame_buffer + unit->dwarf_frame_size)
2189 frame_ptr = decode_frame_entry (gdbarch, unit.get (), frame_ptr, 0,
2190 cie_table, &fde_table,
2191 EH_CIE_OR_FDE_TYPE_ID);
2193 catch (const gdb_exception_error &e)
2195 warning (_("skipping .debug_frame info of %s: %s"),
2196 objfile_name (objfile), e.what ());
2198 fde_table.resize (num_old_fde_entries);
2202 struct dwarf2_fde *fde_prev = NULL;
2203 struct dwarf2_fde *first_non_zero_fde = NULL;
2205 /* Prepare FDE table for lookups. */
2206 std::sort (fde_table.begin (), fde_table.end (), fde_is_less_than);
2208 /* Check for leftovers from --gc-sections. The GNU linker sets
2209 the relevant symbols to zero, but doesn't zero the FDE *end*
2210 ranges because there's no relocation there. It's (offset,
2211 length), not (start, end). On targets where address zero is
2212 just another valid address this can be a problem, since the
2213 FDEs appear to be non-empty in the output --- we could pick
2214 out the wrong FDE. To work around this, when overlaps are
2215 detected, we prefer FDEs that do not start at zero.
2217 Start by finding the first FDE with non-zero start. Below
2218 we'll discard all FDEs that start at zero and overlap this
2219 one. */
2220 for (struct dwarf2_fde *fde : fde_table)
2222 if (fde->initial_location != (unrelocated_addr) 0)
2224 first_non_zero_fde = fde;
2225 break;
2229 /* Since we'll be doing bsearch, squeeze out identical (except
2230 for eh_frame_p) fde entries so bsearch result is predictable.
2231 Also discard leftovers from --gc-sections. */
2232 for (struct dwarf2_fde *fde : fde_table)
2234 if (fde->initial_location == (unrelocated_addr) 0
2235 && first_non_zero_fde != NULL
2236 && first_non_zero_fde->initial_location < fde->end_addr ())
2237 continue;
2239 if (fde_prev != NULL
2240 && fde_prev->initial_location == fde->initial_location)
2241 continue;
2243 unit->fde_table.push_back (fde);
2244 fde_prev = fde;
2246 unit->fde_table.shrink_to_fit ();
2248 set_comp_unit (objfile, unit.release ());
2251 void _initialize_dwarf2_frame ();
2252 void
2253 _initialize_dwarf2_frame ()
2255 #if GDB_SELF_TEST
2256 selftests::register_test_foreach_arch ("execute_cfa_program",
2257 selftests::execute_cfa_program_test);
2258 #endif