17d62b00eSchristos /* Frame unwinder for frames with DWARF Call Frame Information. 27d62b00eSchristos 3*6881a400Schristos Copyright (C) 2003-2023 Free Software Foundation, Inc. 47d62b00eSchristos 57d62b00eSchristos Contributed by Mark Kettenis. 67d62b00eSchristos 77d62b00eSchristos This file is part of GDB. 87d62b00eSchristos 97d62b00eSchristos This program is free software; you can redistribute it and/or modify 107d62b00eSchristos it under the terms of the GNU General Public License as published by 117d62b00eSchristos the Free Software Foundation; either version 3 of the License, or 127d62b00eSchristos (at your option) any later version. 137d62b00eSchristos 147d62b00eSchristos This program is distributed in the hope that it will be useful, 157d62b00eSchristos but WITHOUT ANY WARRANTY; without even the implied warranty of 167d62b00eSchristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 177d62b00eSchristos GNU General Public License for more details. 187d62b00eSchristos 197d62b00eSchristos You should have received a copy of the GNU General Public License 207d62b00eSchristos along with this program. If not, see <http://www.gnu.org/licenses/>. */ 217d62b00eSchristos 227d62b00eSchristos #include "defs.h" 237d62b00eSchristos #include "dwarf2/expr.h" 247d62b00eSchristos #include "dwarf2.h" 257d62b00eSchristos #include "dwarf2/leb.h" 267d62b00eSchristos #include "frame.h" 277d62b00eSchristos #include "frame-base.h" 287d62b00eSchristos #include "frame-unwind.h" 297d62b00eSchristos #include "gdbcore.h" 307d62b00eSchristos #include "gdbtypes.h" 317d62b00eSchristos #include "symtab.h" 327d62b00eSchristos #include "objfiles.h" 337d62b00eSchristos #include "regcache.h" 347d62b00eSchristos #include "value.h" 357d62b00eSchristos #include "record.h" 367d62b00eSchristos 377d62b00eSchristos #include "complaints.h" 387d62b00eSchristos #include "dwarf2/frame.h" 397d62b00eSchristos #include "dwarf2/read.h" 40*6881a400Schristos #include "dwarf2/public.h" 417d62b00eSchristos #include "ax.h" 427d62b00eSchristos #include "dwarf2/loc.h" 437d62b00eSchristos #include "dwarf2/frame-tailcall.h" 447d62b00eSchristos #include "gdbsupport/gdb_binary_search.h" 457d62b00eSchristos #if GDB_SELF_TEST 467d62b00eSchristos #include "gdbsupport/selftest.h" 477d62b00eSchristos #include "selftest-arch.h" 487d62b00eSchristos #endif 497d62b00eSchristos #include <unordered_map> 507d62b00eSchristos 517d62b00eSchristos #include <algorithm> 527d62b00eSchristos 537d62b00eSchristos struct comp_unit; 547d62b00eSchristos 557d62b00eSchristos /* Call Frame Information (CFI). */ 567d62b00eSchristos 577d62b00eSchristos /* Common Information Entry (CIE). */ 587d62b00eSchristos 597d62b00eSchristos struct dwarf2_cie 607d62b00eSchristos { 617d62b00eSchristos /* Computation Unit for this CIE. */ 627d62b00eSchristos struct comp_unit *unit; 637d62b00eSchristos 647d62b00eSchristos /* Offset into the .debug_frame section where this CIE was found. 657d62b00eSchristos Used to identify this CIE. */ 667d62b00eSchristos ULONGEST cie_pointer; 677d62b00eSchristos 687d62b00eSchristos /* Constant that is factored out of all advance location 697d62b00eSchristos instructions. */ 707d62b00eSchristos ULONGEST code_alignment_factor; 717d62b00eSchristos 727d62b00eSchristos /* Constants that is factored out of all offset instructions. */ 737d62b00eSchristos LONGEST data_alignment_factor; 747d62b00eSchristos 757d62b00eSchristos /* Return address column. */ 767d62b00eSchristos ULONGEST return_address_register; 777d62b00eSchristos 787d62b00eSchristos /* Instruction sequence to initialize a register set. */ 797d62b00eSchristos const gdb_byte *initial_instructions; 807d62b00eSchristos const gdb_byte *end; 817d62b00eSchristos 827d62b00eSchristos /* Saved augmentation, in case it's needed later. */ 837d62b00eSchristos char *augmentation; 847d62b00eSchristos 857d62b00eSchristos /* Encoding of addresses. */ 867d62b00eSchristos gdb_byte encoding; 877d62b00eSchristos 887d62b00eSchristos /* Target address size in bytes. */ 897d62b00eSchristos int addr_size; 907d62b00eSchristos 917d62b00eSchristos /* Target pointer size in bytes. */ 927d62b00eSchristos int ptr_size; 937d62b00eSchristos 947d62b00eSchristos /* True if a 'z' augmentation existed. */ 957d62b00eSchristos unsigned char saw_z_augmentation; 967d62b00eSchristos 977d62b00eSchristos /* True if an 'S' augmentation existed. */ 987d62b00eSchristos unsigned char signal_frame; 997d62b00eSchristos 1007d62b00eSchristos /* The version recorded in the CIE. */ 1017d62b00eSchristos unsigned char version; 1027d62b00eSchristos 1037d62b00eSchristos /* The segment size. */ 1047d62b00eSchristos unsigned char segment_size; 1057d62b00eSchristos }; 1067d62b00eSchristos 1077d62b00eSchristos /* The CIE table is used to find CIEs during parsing, but then 1087d62b00eSchristos discarded. It maps from the CIE's offset to the CIE. */ 1097d62b00eSchristos typedef std::unordered_map<ULONGEST, dwarf2_cie *> dwarf2_cie_table; 1107d62b00eSchristos 1117d62b00eSchristos /* Frame Description Entry (FDE). */ 1127d62b00eSchristos 1137d62b00eSchristos struct dwarf2_fde 1147d62b00eSchristos { 1157d62b00eSchristos /* CIE for this FDE. */ 1167d62b00eSchristos struct dwarf2_cie *cie; 1177d62b00eSchristos 1187d62b00eSchristos /* First location associated with this FDE. */ 1197d62b00eSchristos CORE_ADDR initial_location; 1207d62b00eSchristos 1217d62b00eSchristos /* Number of bytes of program instructions described by this FDE. */ 1227d62b00eSchristos CORE_ADDR address_range; 1237d62b00eSchristos 1247d62b00eSchristos /* Instruction sequence. */ 1257d62b00eSchristos const gdb_byte *instructions; 1267d62b00eSchristos const gdb_byte *end; 1277d62b00eSchristos 1287d62b00eSchristos /* True if this FDE is read from a .eh_frame instead of a .debug_frame 1297d62b00eSchristos section. */ 1307d62b00eSchristos unsigned char eh_frame_p; 1317d62b00eSchristos }; 1327d62b00eSchristos 1337d62b00eSchristos typedef std::vector<dwarf2_fde *> dwarf2_fde_table; 1347d62b00eSchristos 1357d62b00eSchristos /* A minimal decoding of DWARF2 compilation units. We only decode 1367d62b00eSchristos what's needed to get to the call frame information. */ 1377d62b00eSchristos 1387d62b00eSchristos struct comp_unit 1397d62b00eSchristos { 1407d62b00eSchristos comp_unit (struct objfile *objf) 141*6881a400Schristos : abfd (objf->obfd.get ()) 1427d62b00eSchristos { 1437d62b00eSchristos } 1447d62b00eSchristos 1457d62b00eSchristos /* Keep the bfd convenient. */ 1467d62b00eSchristos bfd *abfd; 1477d62b00eSchristos 1487d62b00eSchristos /* Pointer to the .debug_frame section loaded into memory. */ 1497d62b00eSchristos const gdb_byte *dwarf_frame_buffer = nullptr; 1507d62b00eSchristos 1517d62b00eSchristos /* Length of the loaded .debug_frame section. */ 1527d62b00eSchristos bfd_size_type dwarf_frame_size = 0; 1537d62b00eSchristos 1547d62b00eSchristos /* Pointer to the .debug_frame section. */ 1557d62b00eSchristos asection *dwarf_frame_section = nullptr; 1567d62b00eSchristos 1577d62b00eSchristos /* Base for DW_EH_PE_datarel encodings. */ 1587d62b00eSchristos bfd_vma dbase = 0; 1597d62b00eSchristos 1607d62b00eSchristos /* Base for DW_EH_PE_textrel encodings. */ 1617d62b00eSchristos bfd_vma tbase = 0; 1627d62b00eSchristos 1637d62b00eSchristos /* The FDE table. */ 1647d62b00eSchristos dwarf2_fde_table fde_table; 1657d62b00eSchristos 1667d62b00eSchristos /* Hold data used by this module. */ 1677d62b00eSchristos auto_obstack obstack; 1687d62b00eSchristos }; 1697d62b00eSchristos 1707d62b00eSchristos static struct dwarf2_fde *dwarf2_frame_find_fde 1717d62b00eSchristos (CORE_ADDR *pc, dwarf2_per_objfile **out_per_objfile); 1727d62b00eSchristos 1737d62b00eSchristos static int dwarf2_frame_adjust_regnum (struct gdbarch *gdbarch, int regnum, 1747d62b00eSchristos int eh_frame_p); 1757d62b00eSchristos 1767d62b00eSchristos static CORE_ADDR read_encoded_value (struct comp_unit *unit, gdb_byte encoding, 1777d62b00eSchristos int ptr_len, const gdb_byte *buf, 1787d62b00eSchristos unsigned int *bytes_read_ptr, 1797d62b00eSchristos CORE_ADDR func_base); 1807d62b00eSchristos 1817d62b00eSchristos 182*6881a400Schristos /* See dwarf2/frame.h. */ 1837d62b00eSchristos bool dwarf2_frame_unwinders_enabled_p = true; 1847d62b00eSchristos 1857d62b00eSchristos /* Store the length the expression for the CFA in the `cfa_reg' field, 1867d62b00eSchristos which is unused in that case. */ 1877d62b00eSchristos #define cfa_exp_len cfa_reg 1887d62b00eSchristos 1897d62b00eSchristos dwarf2_frame_state::dwarf2_frame_state (CORE_ADDR pc_, struct dwarf2_cie *cie) 1907d62b00eSchristos : pc (pc_), data_align (cie->data_alignment_factor), 1917d62b00eSchristos code_align (cie->code_alignment_factor), 1927d62b00eSchristos retaddr_column (cie->return_address_register) 1937d62b00eSchristos { 1947d62b00eSchristos } 1957d62b00eSchristos 1967d62b00eSchristos /* Execute the required actions for both the DW_CFA_restore and 1977d62b00eSchristos DW_CFA_restore_extended instructions. */ 1987d62b00eSchristos static void 1997d62b00eSchristos dwarf2_restore_rule (struct gdbarch *gdbarch, ULONGEST reg_num, 2007d62b00eSchristos struct dwarf2_frame_state *fs, int eh_frame_p) 2017d62b00eSchristos { 2027d62b00eSchristos ULONGEST reg; 2037d62b00eSchristos 2047d62b00eSchristos reg = dwarf2_frame_adjust_regnum (gdbarch, reg_num, eh_frame_p); 2057d62b00eSchristos fs->regs.alloc_regs (reg + 1); 2067d62b00eSchristos 2077d62b00eSchristos /* Check if this register was explicitly initialized in the 2087d62b00eSchristos CIE initial instructions. If not, default the rule to 2097d62b00eSchristos UNSPECIFIED. */ 2107d62b00eSchristos if (reg < fs->initial.reg.size ()) 2117d62b00eSchristos fs->regs.reg[reg] = fs->initial.reg[reg]; 2127d62b00eSchristos else 2137d62b00eSchristos fs->regs.reg[reg].how = DWARF2_FRAME_REG_UNSPECIFIED; 2147d62b00eSchristos 2157d62b00eSchristos if (fs->regs.reg[reg].how == DWARF2_FRAME_REG_UNSPECIFIED) 2167d62b00eSchristos { 2177d62b00eSchristos int regnum = dwarf_reg_to_regnum (gdbarch, reg); 2187d62b00eSchristos 2197d62b00eSchristos complaint (_("\ 2207d62b00eSchristos incomplete CFI data; DW_CFA_restore unspecified\n\ 2217d62b00eSchristos register %s (#%d) at %s"), 2227d62b00eSchristos gdbarch_register_name (gdbarch, regnum), regnum, 2237d62b00eSchristos paddress (gdbarch, fs->pc)); 2247d62b00eSchristos } 2257d62b00eSchristos } 2267d62b00eSchristos 2277d62b00eSchristos static CORE_ADDR 2287d62b00eSchristos execute_stack_op (const gdb_byte *exp, ULONGEST len, int addr_size, 229*6881a400Schristos frame_info_ptr this_frame, CORE_ADDR initial, 2307d62b00eSchristos int initial_in_stack_memory, dwarf2_per_objfile *per_objfile) 2317d62b00eSchristos { 232*6881a400Schristos dwarf_expr_context ctx (per_objfile, addr_size); 2337d62b00eSchristos scoped_value_mark free_values; 2347d62b00eSchristos 2357d62b00eSchristos ctx.push_address (initial, initial_in_stack_memory); 236*6881a400Schristos value *result_val = ctx.evaluate (exp, len, true, nullptr, this_frame); 2377d62b00eSchristos 238*6881a400Schristos if (VALUE_LVAL (result_val) == lval_memory) 239*6881a400Schristos return value_address (result_val); 2407d62b00eSchristos else 241*6881a400Schristos return value_as_address (result_val); 2427d62b00eSchristos } 2437d62b00eSchristos 2447d62b00eSchristos 2457d62b00eSchristos /* Execute FDE program from INSN_PTR possibly up to INSN_END or up to inferior 2467d62b00eSchristos PC. Modify FS state accordingly. Return current INSN_PTR where the 2477d62b00eSchristos execution has stopped, one can resume it on the next call. */ 2487d62b00eSchristos 2497d62b00eSchristos static const gdb_byte * 2507d62b00eSchristos execute_cfa_program (struct dwarf2_fde *fde, const gdb_byte *insn_ptr, 2517d62b00eSchristos const gdb_byte *insn_end, struct gdbarch *gdbarch, 2527d62b00eSchristos CORE_ADDR pc, struct dwarf2_frame_state *fs, 2537d62b00eSchristos CORE_ADDR text_offset) 2547d62b00eSchristos { 2557d62b00eSchristos int eh_frame_p = fde->eh_frame_p; 2567d62b00eSchristos unsigned int bytes_read; 2577d62b00eSchristos enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); 2587d62b00eSchristos 2597d62b00eSchristos while (insn_ptr < insn_end && fs->pc <= pc) 2607d62b00eSchristos { 2617d62b00eSchristos gdb_byte insn = *insn_ptr++; 2627d62b00eSchristos uint64_t utmp, reg; 2637d62b00eSchristos int64_t offset; 2647d62b00eSchristos 2657d62b00eSchristos if ((insn & 0xc0) == DW_CFA_advance_loc) 2667d62b00eSchristos fs->pc += (insn & 0x3f) * fs->code_align; 2677d62b00eSchristos else if ((insn & 0xc0) == DW_CFA_offset) 2687d62b00eSchristos { 2697d62b00eSchristos reg = insn & 0x3f; 2707d62b00eSchristos reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p); 2717d62b00eSchristos insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp); 2727d62b00eSchristos offset = utmp * fs->data_align; 2737d62b00eSchristos fs->regs.alloc_regs (reg + 1); 2747d62b00eSchristos fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET; 2757d62b00eSchristos fs->regs.reg[reg].loc.offset = offset; 2767d62b00eSchristos } 2777d62b00eSchristos else if ((insn & 0xc0) == DW_CFA_restore) 2787d62b00eSchristos { 2797d62b00eSchristos reg = insn & 0x3f; 2807d62b00eSchristos dwarf2_restore_rule (gdbarch, reg, fs, eh_frame_p); 2817d62b00eSchristos } 2827d62b00eSchristos else 2837d62b00eSchristos { 2847d62b00eSchristos switch (insn) 2857d62b00eSchristos { 2867d62b00eSchristos case DW_CFA_set_loc: 2877d62b00eSchristos fs->pc = read_encoded_value (fde->cie->unit, fde->cie->encoding, 2887d62b00eSchristos fde->cie->ptr_size, insn_ptr, 2897d62b00eSchristos &bytes_read, fde->initial_location); 2907d62b00eSchristos /* Apply the text offset for relocatable objects. */ 2917d62b00eSchristos fs->pc += text_offset; 2927d62b00eSchristos insn_ptr += bytes_read; 2937d62b00eSchristos break; 2947d62b00eSchristos 2957d62b00eSchristos case DW_CFA_advance_loc1: 2967d62b00eSchristos utmp = extract_unsigned_integer (insn_ptr, 1, byte_order); 2977d62b00eSchristos fs->pc += utmp * fs->code_align; 2987d62b00eSchristos insn_ptr++; 2997d62b00eSchristos break; 3007d62b00eSchristos case DW_CFA_advance_loc2: 3017d62b00eSchristos utmp = extract_unsigned_integer (insn_ptr, 2, byte_order); 3027d62b00eSchristos fs->pc += utmp * fs->code_align; 3037d62b00eSchristos insn_ptr += 2; 3047d62b00eSchristos break; 3057d62b00eSchristos case DW_CFA_advance_loc4: 3067d62b00eSchristos utmp = extract_unsigned_integer (insn_ptr, 4, byte_order); 3077d62b00eSchristos fs->pc += utmp * fs->code_align; 3087d62b00eSchristos insn_ptr += 4; 3097d62b00eSchristos break; 3107d62b00eSchristos 3117d62b00eSchristos case DW_CFA_offset_extended: 3127d62b00eSchristos insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®); 3137d62b00eSchristos reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p); 3147d62b00eSchristos insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp); 3157d62b00eSchristos offset = utmp * fs->data_align; 3167d62b00eSchristos fs->regs.alloc_regs (reg + 1); 3177d62b00eSchristos fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET; 3187d62b00eSchristos fs->regs.reg[reg].loc.offset = offset; 3197d62b00eSchristos break; 3207d62b00eSchristos 3217d62b00eSchristos case DW_CFA_restore_extended: 3227d62b00eSchristos insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®); 3237d62b00eSchristos dwarf2_restore_rule (gdbarch, reg, fs, eh_frame_p); 3247d62b00eSchristos break; 3257d62b00eSchristos 3267d62b00eSchristos case DW_CFA_undefined: 3277d62b00eSchristos insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®); 3287d62b00eSchristos reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p); 3297d62b00eSchristos fs->regs.alloc_regs (reg + 1); 3307d62b00eSchristos fs->regs.reg[reg].how = DWARF2_FRAME_REG_UNDEFINED; 3317d62b00eSchristos break; 3327d62b00eSchristos 3337d62b00eSchristos case DW_CFA_same_value: 3347d62b00eSchristos insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®); 3357d62b00eSchristos reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p); 3367d62b00eSchristos fs->regs.alloc_regs (reg + 1); 3377d62b00eSchristos fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAME_VALUE; 3387d62b00eSchristos break; 3397d62b00eSchristos 3407d62b00eSchristos case DW_CFA_register: 3417d62b00eSchristos insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®); 3427d62b00eSchristos reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p); 3437d62b00eSchristos insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp); 3447d62b00eSchristos utmp = dwarf2_frame_adjust_regnum (gdbarch, utmp, eh_frame_p); 3457d62b00eSchristos fs->regs.alloc_regs (reg + 1); 3467d62b00eSchristos fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_REG; 3477d62b00eSchristos fs->regs.reg[reg].loc.reg = utmp; 3487d62b00eSchristos break; 3497d62b00eSchristos 3507d62b00eSchristos case DW_CFA_remember_state: 3517d62b00eSchristos { 3527d62b00eSchristos struct dwarf2_frame_state_reg_info *new_rs; 3537d62b00eSchristos 3547d62b00eSchristos new_rs = new dwarf2_frame_state_reg_info (fs->regs); 3557d62b00eSchristos fs->regs.prev = new_rs; 3567d62b00eSchristos } 3577d62b00eSchristos break; 3587d62b00eSchristos 3597d62b00eSchristos case DW_CFA_restore_state: 3607d62b00eSchristos { 3617d62b00eSchristos struct dwarf2_frame_state_reg_info *old_rs = fs->regs.prev; 3627d62b00eSchristos 3637d62b00eSchristos if (old_rs == NULL) 3647d62b00eSchristos { 3657d62b00eSchristos complaint (_("\ 3667d62b00eSchristos bad CFI data; mismatched DW_CFA_restore_state at %s"), 3677d62b00eSchristos paddress (gdbarch, fs->pc)); 3687d62b00eSchristos } 3697d62b00eSchristos else 3707d62b00eSchristos fs->regs = std::move (*old_rs); 3717d62b00eSchristos } 3727d62b00eSchristos break; 3737d62b00eSchristos 3747d62b00eSchristos case DW_CFA_def_cfa: 3757d62b00eSchristos insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®); 3767d62b00eSchristos fs->regs.cfa_reg = reg; 3777d62b00eSchristos insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp); 3787d62b00eSchristos 3797d62b00eSchristos if (fs->armcc_cfa_offsets_sf) 3807d62b00eSchristos utmp *= fs->data_align; 3817d62b00eSchristos 3827d62b00eSchristos fs->regs.cfa_offset = utmp; 3837d62b00eSchristos fs->regs.cfa_how = CFA_REG_OFFSET; 3847d62b00eSchristos break; 3857d62b00eSchristos 3867d62b00eSchristos case DW_CFA_def_cfa_register: 3877d62b00eSchristos insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®); 3887d62b00eSchristos fs->regs.cfa_reg = dwarf2_frame_adjust_regnum (gdbarch, reg, 3897d62b00eSchristos eh_frame_p); 3907d62b00eSchristos fs->regs.cfa_how = CFA_REG_OFFSET; 3917d62b00eSchristos break; 3927d62b00eSchristos 3937d62b00eSchristos case DW_CFA_def_cfa_offset: 3947d62b00eSchristos insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp); 3957d62b00eSchristos 3967d62b00eSchristos if (fs->armcc_cfa_offsets_sf) 3977d62b00eSchristos utmp *= fs->data_align; 3987d62b00eSchristos 3997d62b00eSchristos fs->regs.cfa_offset = utmp; 4007d62b00eSchristos /* cfa_how deliberately not set. */ 4017d62b00eSchristos break; 4027d62b00eSchristos 4037d62b00eSchristos case DW_CFA_nop: 4047d62b00eSchristos break; 4057d62b00eSchristos 4067d62b00eSchristos case DW_CFA_def_cfa_expression: 4077d62b00eSchristos insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp); 4087d62b00eSchristos fs->regs.cfa_exp_len = utmp; 4097d62b00eSchristos fs->regs.cfa_exp = insn_ptr; 4107d62b00eSchristos fs->regs.cfa_how = CFA_EXP; 4117d62b00eSchristos insn_ptr += fs->regs.cfa_exp_len; 4127d62b00eSchristos break; 4137d62b00eSchristos 4147d62b00eSchristos case DW_CFA_expression: 4157d62b00eSchristos insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®); 4167d62b00eSchristos reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p); 4177d62b00eSchristos fs->regs.alloc_regs (reg + 1); 4187d62b00eSchristos insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp); 4197d62b00eSchristos fs->regs.reg[reg].loc.exp.start = insn_ptr; 4207d62b00eSchristos fs->regs.reg[reg].loc.exp.len = utmp; 4217d62b00eSchristos fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_EXP; 4227d62b00eSchristos insn_ptr += utmp; 4237d62b00eSchristos break; 4247d62b00eSchristos 4257d62b00eSchristos case DW_CFA_offset_extended_sf: 4267d62b00eSchristos insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®); 4277d62b00eSchristos reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p); 4287d62b00eSchristos insn_ptr = safe_read_sleb128 (insn_ptr, insn_end, &offset); 4297d62b00eSchristos offset *= fs->data_align; 4307d62b00eSchristos fs->regs.alloc_regs (reg + 1); 4317d62b00eSchristos fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET; 4327d62b00eSchristos fs->regs.reg[reg].loc.offset = offset; 4337d62b00eSchristos break; 4347d62b00eSchristos 4357d62b00eSchristos case DW_CFA_val_offset: 4367d62b00eSchristos insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®); 4377d62b00eSchristos fs->regs.alloc_regs (reg + 1); 4387d62b00eSchristos insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp); 4397d62b00eSchristos offset = utmp * fs->data_align; 4407d62b00eSchristos fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_VAL_OFFSET; 4417d62b00eSchristos fs->regs.reg[reg].loc.offset = offset; 4427d62b00eSchristos break; 4437d62b00eSchristos 4447d62b00eSchristos case DW_CFA_val_offset_sf: 4457d62b00eSchristos insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®); 4467d62b00eSchristos fs->regs.alloc_regs (reg + 1); 4477d62b00eSchristos insn_ptr = safe_read_sleb128 (insn_ptr, insn_end, &offset); 4487d62b00eSchristos offset *= fs->data_align; 4497d62b00eSchristos fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_VAL_OFFSET; 4507d62b00eSchristos fs->regs.reg[reg].loc.offset = offset; 4517d62b00eSchristos break; 4527d62b00eSchristos 4537d62b00eSchristos case DW_CFA_val_expression: 4547d62b00eSchristos insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®); 4557d62b00eSchristos fs->regs.alloc_regs (reg + 1); 4567d62b00eSchristos insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp); 4577d62b00eSchristos fs->regs.reg[reg].loc.exp.start = insn_ptr; 4587d62b00eSchristos fs->regs.reg[reg].loc.exp.len = utmp; 4597d62b00eSchristos fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_VAL_EXP; 4607d62b00eSchristos insn_ptr += utmp; 4617d62b00eSchristos break; 4627d62b00eSchristos 4637d62b00eSchristos case DW_CFA_def_cfa_sf: 4647d62b00eSchristos insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®); 4657d62b00eSchristos fs->regs.cfa_reg = dwarf2_frame_adjust_regnum (gdbarch, reg, 4667d62b00eSchristos eh_frame_p); 4677d62b00eSchristos insn_ptr = safe_read_sleb128 (insn_ptr, insn_end, &offset); 4687d62b00eSchristos fs->regs.cfa_offset = offset * fs->data_align; 4697d62b00eSchristos fs->regs.cfa_how = CFA_REG_OFFSET; 4707d62b00eSchristos break; 4717d62b00eSchristos 4727d62b00eSchristos case DW_CFA_def_cfa_offset_sf: 4737d62b00eSchristos insn_ptr = safe_read_sleb128 (insn_ptr, insn_end, &offset); 4747d62b00eSchristos fs->regs.cfa_offset = offset * fs->data_align; 4757d62b00eSchristos /* cfa_how deliberately not set. */ 4767d62b00eSchristos break; 4777d62b00eSchristos 4787d62b00eSchristos case DW_CFA_GNU_args_size: 4797d62b00eSchristos /* Ignored. */ 4807d62b00eSchristos insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp); 4817d62b00eSchristos break; 4827d62b00eSchristos 4837d62b00eSchristos case DW_CFA_GNU_negative_offset_extended: 4847d62b00eSchristos insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®); 4857d62b00eSchristos reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p); 4867d62b00eSchristos insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp); 4877d62b00eSchristos offset = utmp * fs->data_align; 4887d62b00eSchristos fs->regs.alloc_regs (reg + 1); 4897d62b00eSchristos fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET; 4907d62b00eSchristos fs->regs.reg[reg].loc.offset = -offset; 4917d62b00eSchristos break; 4927d62b00eSchristos 4937d62b00eSchristos default: 4947d62b00eSchristos if (insn >= DW_CFA_lo_user && insn <= DW_CFA_hi_user) 4957d62b00eSchristos { 4967d62b00eSchristos /* Handle vendor-specific CFI for different architectures. */ 4977d62b00eSchristos if (!gdbarch_execute_dwarf_cfa_vendor_op (gdbarch, insn, fs)) 4987d62b00eSchristos error (_("Call Frame Instruction op %d in vendor extension " 4997d62b00eSchristos "space is not handled on this architecture."), 5007d62b00eSchristos insn); 5017d62b00eSchristos } 5027d62b00eSchristos else 503*6881a400Schristos internal_error (_("Unknown CFI encountered.")); 5047d62b00eSchristos } 5057d62b00eSchristos } 5067d62b00eSchristos } 5077d62b00eSchristos 5087d62b00eSchristos if (fs->initial.reg.empty ()) 5097d62b00eSchristos { 5107d62b00eSchristos /* Don't allow remember/restore between CIE and FDE programs. */ 5117d62b00eSchristos delete fs->regs.prev; 5127d62b00eSchristos fs->regs.prev = NULL; 5137d62b00eSchristos } 5147d62b00eSchristos 5157d62b00eSchristos return insn_ptr; 5167d62b00eSchristos } 5177d62b00eSchristos 5187d62b00eSchristos #if GDB_SELF_TEST 5197d62b00eSchristos 5207d62b00eSchristos namespace selftests { 5217d62b00eSchristos 5227d62b00eSchristos /* Unit test to function execute_cfa_program. */ 5237d62b00eSchristos 5247d62b00eSchristos static void 5257d62b00eSchristos execute_cfa_program_test (struct gdbarch *gdbarch) 5267d62b00eSchristos { 5277d62b00eSchristos struct dwarf2_fde fde; 5287d62b00eSchristos struct dwarf2_cie cie; 5297d62b00eSchristos 5307d62b00eSchristos memset (&fde, 0, sizeof fde); 5317d62b00eSchristos memset (&cie, 0, sizeof cie); 5327d62b00eSchristos 5337d62b00eSchristos cie.data_alignment_factor = -4; 5347d62b00eSchristos cie.code_alignment_factor = 2; 5357d62b00eSchristos fde.cie = &cie; 5367d62b00eSchristos 5377d62b00eSchristos dwarf2_frame_state fs (0, fde.cie); 5387d62b00eSchristos 5397d62b00eSchristos gdb_byte insns[] = 5407d62b00eSchristos { 5417d62b00eSchristos DW_CFA_def_cfa, 1, 4, /* DW_CFA_def_cfa: r1 ofs 4 */ 5427d62b00eSchristos DW_CFA_offset | 0x2, 1, /* DW_CFA_offset: r2 at cfa-4 */ 5437d62b00eSchristos DW_CFA_remember_state, 5447d62b00eSchristos DW_CFA_restore_state, 5457d62b00eSchristos }; 5467d62b00eSchristos 5477d62b00eSchristos const gdb_byte *insn_end = insns + sizeof (insns); 5487d62b00eSchristos const gdb_byte *out = execute_cfa_program (&fde, insns, insn_end, gdbarch, 5497d62b00eSchristos 0, &fs, 0); 5507d62b00eSchristos 5517d62b00eSchristos SELF_CHECK (out == insn_end); 5527d62b00eSchristos SELF_CHECK (fs.pc == 0); 5537d62b00eSchristos 5547d62b00eSchristos /* The instructions above only use r1 and r2, but the register numbers 5557d62b00eSchristos used are adjusted by dwarf2_frame_adjust_regnum. */ 5567d62b00eSchristos auto r1 = dwarf2_frame_adjust_regnum (gdbarch, 1, fde.eh_frame_p); 5577d62b00eSchristos auto r2 = dwarf2_frame_adjust_regnum (gdbarch, 2, fde.eh_frame_p); 5587d62b00eSchristos 5597d62b00eSchristos SELF_CHECK (fs.regs.reg.size () == (std::max (r1, r2) + 1)); 5607d62b00eSchristos 5617d62b00eSchristos SELF_CHECK (fs.regs.reg[r2].how == DWARF2_FRAME_REG_SAVED_OFFSET); 5627d62b00eSchristos SELF_CHECK (fs.regs.reg[r2].loc.offset == -4); 5637d62b00eSchristos 5647d62b00eSchristos for (auto i = 0; i < fs.regs.reg.size (); i++) 5657d62b00eSchristos if (i != r2) 5667d62b00eSchristos SELF_CHECK (fs.regs.reg[i].how == DWARF2_FRAME_REG_UNSPECIFIED); 5677d62b00eSchristos 5687d62b00eSchristos SELF_CHECK (fs.regs.cfa_reg == 1); 5697d62b00eSchristos SELF_CHECK (fs.regs.cfa_offset == 4); 5707d62b00eSchristos SELF_CHECK (fs.regs.cfa_how == CFA_REG_OFFSET); 5717d62b00eSchristos SELF_CHECK (fs.regs.cfa_exp == NULL); 5727d62b00eSchristos SELF_CHECK (fs.regs.prev == NULL); 5737d62b00eSchristos } 5747d62b00eSchristos 5757d62b00eSchristos } // namespace selftests 5767d62b00eSchristos #endif /* GDB_SELF_TEST */ 5777d62b00eSchristos 5787d62b00eSchristos 5797d62b00eSchristos 5807d62b00eSchristos /* Architecture-specific operations. */ 5817d62b00eSchristos 582*6881a400Schristos static void dwarf2_frame_default_init_reg (struct gdbarch *gdbarch, 583*6881a400Schristos int regnum, 584*6881a400Schristos struct dwarf2_frame_state_reg *reg, 585*6881a400Schristos frame_info_ptr this_frame); 5867d62b00eSchristos 5877d62b00eSchristos struct dwarf2_frame_ops 5887d62b00eSchristos { 5897d62b00eSchristos /* Pre-initialize the register state REG for register REGNUM. */ 5907d62b00eSchristos void (*init_reg) (struct gdbarch *, int, struct dwarf2_frame_state_reg *, 591*6881a400Schristos frame_info_ptr) 592*6881a400Schristos = dwarf2_frame_default_init_reg; 5937d62b00eSchristos 5947d62b00eSchristos /* Check whether the THIS_FRAME is a signal trampoline. */ 595*6881a400Schristos int (*signal_frame_p) (struct gdbarch *, frame_info_ptr) = nullptr; 5967d62b00eSchristos 5977d62b00eSchristos /* Convert .eh_frame register number to DWARF register number, or 5987d62b00eSchristos adjust .debug_frame register number. */ 599*6881a400Schristos int (*adjust_regnum) (struct gdbarch *, int, int) = nullptr; 6007d62b00eSchristos }; 6017d62b00eSchristos 602*6881a400Schristos /* Per-architecture data key. */ 603*6881a400Schristos static const registry<gdbarch>::key<dwarf2_frame_ops> dwarf2_frame_data; 604*6881a400Schristos 605*6881a400Schristos /* Get or initialize the frame ops. */ 606*6881a400Schristos static dwarf2_frame_ops * 607*6881a400Schristos get_frame_ops (struct gdbarch *gdbarch) 608*6881a400Schristos { 609*6881a400Schristos dwarf2_frame_ops *result = dwarf2_frame_data.get (gdbarch); 610*6881a400Schristos if (result == nullptr) 611*6881a400Schristos result = dwarf2_frame_data.emplace (gdbarch); 612*6881a400Schristos return result; 613*6881a400Schristos } 614*6881a400Schristos 6157d62b00eSchristos /* Default architecture-specific register state initialization 6167d62b00eSchristos function. */ 6177d62b00eSchristos 6187d62b00eSchristos static void 6197d62b00eSchristos dwarf2_frame_default_init_reg (struct gdbarch *gdbarch, int regnum, 6207d62b00eSchristos struct dwarf2_frame_state_reg *reg, 621*6881a400Schristos frame_info_ptr this_frame) 6227d62b00eSchristos { 6237d62b00eSchristos /* If we have a register that acts as a program counter, mark it as 6247d62b00eSchristos a destination for the return address. If we have a register that 6257d62b00eSchristos serves as the stack pointer, arrange for it to be filled with the 6267d62b00eSchristos call frame address (CFA). The other registers are marked as 6277d62b00eSchristos unspecified. 6287d62b00eSchristos 6297d62b00eSchristos We copy the return address to the program counter, since many 6307d62b00eSchristos parts in GDB assume that it is possible to get the return address 6317d62b00eSchristos by unwinding the program counter register. However, on ISA's 6327d62b00eSchristos with a dedicated return address register, the CFI usually only 6337d62b00eSchristos contains information to unwind that return address register. 6347d62b00eSchristos 6357d62b00eSchristos The reason we're treating the stack pointer special here is 6367d62b00eSchristos because in many cases GCC doesn't emit CFI for the stack pointer 6377d62b00eSchristos and implicitly assumes that it is equal to the CFA. This makes 6387d62b00eSchristos some sense since the DWARF specification (version 3, draft 8, 6397d62b00eSchristos p. 102) says that: 6407d62b00eSchristos 6417d62b00eSchristos "Typically, the CFA is defined to be the value of the stack 6427d62b00eSchristos pointer at the call site in the previous frame (which may be 6437d62b00eSchristos different from its value on entry to the current frame)." 6447d62b00eSchristos 6457d62b00eSchristos However, this isn't true for all platforms supported by GCC 6467d62b00eSchristos (e.g. IBM S/390 and zSeries). Those architectures should provide 6477d62b00eSchristos their own architecture-specific initialization function. */ 6487d62b00eSchristos 6497d62b00eSchristos if (regnum == gdbarch_pc_regnum (gdbarch)) 6507d62b00eSchristos reg->how = DWARF2_FRAME_REG_RA; 6517d62b00eSchristos else if (regnum == gdbarch_sp_regnum (gdbarch)) 6527d62b00eSchristos reg->how = DWARF2_FRAME_REG_CFA; 6537d62b00eSchristos } 6547d62b00eSchristos 6557d62b00eSchristos /* Set the architecture-specific register state initialization 6567d62b00eSchristos function for GDBARCH to INIT_REG. */ 6577d62b00eSchristos 6587d62b00eSchristos void 6597d62b00eSchristos dwarf2_frame_set_init_reg (struct gdbarch *gdbarch, 6607d62b00eSchristos void (*init_reg) (struct gdbarch *, int, 6617d62b00eSchristos struct dwarf2_frame_state_reg *, 662*6881a400Schristos frame_info_ptr)) 6637d62b00eSchristos { 664*6881a400Schristos struct dwarf2_frame_ops *ops = get_frame_ops (gdbarch); 6657d62b00eSchristos 6667d62b00eSchristos ops->init_reg = init_reg; 6677d62b00eSchristos } 6687d62b00eSchristos 6697d62b00eSchristos /* Pre-initialize the register state REG for register REGNUM. */ 6707d62b00eSchristos 6717d62b00eSchristos static void 6727d62b00eSchristos dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum, 6737d62b00eSchristos struct dwarf2_frame_state_reg *reg, 674*6881a400Schristos frame_info_ptr this_frame) 6757d62b00eSchristos { 676*6881a400Schristos struct dwarf2_frame_ops *ops = get_frame_ops (gdbarch); 6777d62b00eSchristos 6787d62b00eSchristos ops->init_reg (gdbarch, regnum, reg, this_frame); 6797d62b00eSchristos } 6807d62b00eSchristos 6817d62b00eSchristos /* Set the architecture-specific signal trampoline recognition 6827d62b00eSchristos function for GDBARCH to SIGNAL_FRAME_P. */ 6837d62b00eSchristos 6847d62b00eSchristos void 6857d62b00eSchristos dwarf2_frame_set_signal_frame_p (struct gdbarch *gdbarch, 6867d62b00eSchristos int (*signal_frame_p) (struct gdbarch *, 687*6881a400Schristos frame_info_ptr)) 6887d62b00eSchristos { 689*6881a400Schristos struct dwarf2_frame_ops *ops = get_frame_ops (gdbarch); 6907d62b00eSchristos 6917d62b00eSchristos ops->signal_frame_p = signal_frame_p; 6927d62b00eSchristos } 6937d62b00eSchristos 6947d62b00eSchristos /* Query the architecture-specific signal frame recognizer for 6957d62b00eSchristos THIS_FRAME. */ 6967d62b00eSchristos 6977d62b00eSchristos static int 6987d62b00eSchristos dwarf2_frame_signal_frame_p (struct gdbarch *gdbarch, 699*6881a400Schristos frame_info_ptr this_frame) 7007d62b00eSchristos { 701*6881a400Schristos struct dwarf2_frame_ops *ops = get_frame_ops (gdbarch); 7027d62b00eSchristos 7037d62b00eSchristos if (ops->signal_frame_p == NULL) 7047d62b00eSchristos return 0; 7057d62b00eSchristos return ops->signal_frame_p (gdbarch, this_frame); 7067d62b00eSchristos } 7077d62b00eSchristos 7087d62b00eSchristos /* Set the architecture-specific adjustment of .eh_frame and .debug_frame 7097d62b00eSchristos register numbers. */ 7107d62b00eSchristos 7117d62b00eSchristos void 7127d62b00eSchristos dwarf2_frame_set_adjust_regnum (struct gdbarch *gdbarch, 7137d62b00eSchristos int (*adjust_regnum) (struct gdbarch *, 7147d62b00eSchristos int, int)) 7157d62b00eSchristos { 716*6881a400Schristos struct dwarf2_frame_ops *ops = get_frame_ops (gdbarch); 7177d62b00eSchristos 7187d62b00eSchristos ops->adjust_regnum = adjust_regnum; 7197d62b00eSchristos } 7207d62b00eSchristos 7217d62b00eSchristos /* Translate a .eh_frame register to DWARF register, or adjust a .debug_frame 7227d62b00eSchristos register. */ 7237d62b00eSchristos 7247d62b00eSchristos static int 7257d62b00eSchristos dwarf2_frame_adjust_regnum (struct gdbarch *gdbarch, 7267d62b00eSchristos int regnum, int eh_frame_p) 7277d62b00eSchristos { 728*6881a400Schristos struct dwarf2_frame_ops *ops = get_frame_ops (gdbarch); 7297d62b00eSchristos 7307d62b00eSchristos if (ops->adjust_regnum == NULL) 7317d62b00eSchristos return regnum; 7327d62b00eSchristos return ops->adjust_regnum (gdbarch, regnum, eh_frame_p); 7337d62b00eSchristos } 7347d62b00eSchristos 7357d62b00eSchristos static void 7367d62b00eSchristos dwarf2_frame_find_quirks (struct dwarf2_frame_state *fs, 7377d62b00eSchristos struct dwarf2_fde *fde) 7387d62b00eSchristos { 7397d62b00eSchristos struct compunit_symtab *cust; 7407d62b00eSchristos 7417d62b00eSchristos cust = find_pc_compunit_symtab (fs->pc); 7427d62b00eSchristos if (cust == NULL) 7437d62b00eSchristos return; 7447d62b00eSchristos 745*6881a400Schristos if (producer_is_realview (cust->producer ())) 7467d62b00eSchristos { 7477d62b00eSchristos if (fde->cie->version == 1) 7487d62b00eSchristos fs->armcc_cfa_offsets_sf = 1; 7497d62b00eSchristos 7507d62b00eSchristos if (fde->cie->version == 1) 7517d62b00eSchristos fs->armcc_cfa_offsets_reversed = 1; 7527d62b00eSchristos 7537d62b00eSchristos /* The reversed offset problem is present in some compilers 7547d62b00eSchristos using DWARF3, but it was eventually fixed. Check the ARM 7557d62b00eSchristos defined augmentations, which are in the format "armcc" followed 7567d62b00eSchristos by a list of one-character options. The "+" option means 7577d62b00eSchristos this problem is fixed (no quirk needed). If the armcc 7587d62b00eSchristos augmentation is missing, the quirk is needed. */ 7597d62b00eSchristos if (fde->cie->version == 3 7607d62b00eSchristos && (!startswith (fde->cie->augmentation, "armcc") 7617d62b00eSchristos || strchr (fde->cie->augmentation + 5, '+') == NULL)) 7627d62b00eSchristos fs->armcc_cfa_offsets_reversed = 1; 7637d62b00eSchristos 7647d62b00eSchristos return; 7657d62b00eSchristos } 7667d62b00eSchristos } 7677d62b00eSchristos 7687d62b00eSchristos 769*6881a400Schristos /* See dwarf2/frame.h. */ 7707d62b00eSchristos 7717d62b00eSchristos int 7727d62b00eSchristos dwarf2_fetch_cfa_info (struct gdbarch *gdbarch, CORE_ADDR pc, 7737d62b00eSchristos struct dwarf2_per_cu_data *data, 7747d62b00eSchristos int *regnum_out, LONGEST *offset_out, 7757d62b00eSchristos CORE_ADDR *text_offset_out, 7767d62b00eSchristos const gdb_byte **cfa_start_out, 7777d62b00eSchristos const gdb_byte **cfa_end_out) 7787d62b00eSchristos { 7797d62b00eSchristos struct dwarf2_fde *fde; 7807d62b00eSchristos dwarf2_per_objfile *per_objfile; 7817d62b00eSchristos CORE_ADDR pc1 = pc; 7827d62b00eSchristos 7837d62b00eSchristos /* Find the correct FDE. */ 7847d62b00eSchristos fde = dwarf2_frame_find_fde (&pc1, &per_objfile); 7857d62b00eSchristos if (fde == NULL) 7867d62b00eSchristos error (_("Could not compute CFA; needed to translate this expression")); 7877d62b00eSchristos 7887d62b00eSchristos gdb_assert (per_objfile != nullptr); 7897d62b00eSchristos 7907d62b00eSchristos dwarf2_frame_state fs (pc1, fde->cie); 7917d62b00eSchristos 7927d62b00eSchristos /* Check for "quirks" - known bugs in producers. */ 7937d62b00eSchristos dwarf2_frame_find_quirks (&fs, fde); 7947d62b00eSchristos 7957d62b00eSchristos /* First decode all the insns in the CIE. */ 7967d62b00eSchristos execute_cfa_program (fde, fde->cie->initial_instructions, 7977d62b00eSchristos fde->cie->end, gdbarch, pc, &fs, 7987d62b00eSchristos per_objfile->objfile->text_section_offset ()); 7997d62b00eSchristos 8007d62b00eSchristos /* Save the initialized register set. */ 8017d62b00eSchristos fs.initial = fs.regs; 8027d62b00eSchristos 8037d62b00eSchristos /* Then decode the insns in the FDE up to our target PC. */ 8047d62b00eSchristos execute_cfa_program (fde, fde->instructions, fde->end, gdbarch, pc, &fs, 8057d62b00eSchristos per_objfile->objfile->text_section_offset ()); 8067d62b00eSchristos 8077d62b00eSchristos /* Calculate the CFA. */ 8087d62b00eSchristos switch (fs.regs.cfa_how) 8097d62b00eSchristos { 8107d62b00eSchristos case CFA_REG_OFFSET: 8117d62b00eSchristos { 8127d62b00eSchristos int regnum = dwarf_reg_to_regnum_or_error (gdbarch, fs.regs.cfa_reg); 8137d62b00eSchristos 8147d62b00eSchristos *regnum_out = regnum; 8157d62b00eSchristos if (fs.armcc_cfa_offsets_reversed) 8167d62b00eSchristos *offset_out = -fs.regs.cfa_offset; 8177d62b00eSchristos else 8187d62b00eSchristos *offset_out = fs.regs.cfa_offset; 8197d62b00eSchristos return 1; 8207d62b00eSchristos } 8217d62b00eSchristos 8227d62b00eSchristos case CFA_EXP: 8237d62b00eSchristos *text_offset_out = per_objfile->objfile->text_section_offset (); 8247d62b00eSchristos *cfa_start_out = fs.regs.cfa_exp; 8257d62b00eSchristos *cfa_end_out = fs.regs.cfa_exp + fs.regs.cfa_exp_len; 8267d62b00eSchristos return 0; 8277d62b00eSchristos 8287d62b00eSchristos default: 829*6881a400Schristos internal_error (_("Unknown CFA rule.")); 8307d62b00eSchristos } 8317d62b00eSchristos } 8327d62b00eSchristos 8337d62b00eSchristos 834*6881a400Schristos /* Custom function data object for architecture specific prev_register 835*6881a400Schristos implementation. Main purpose of this object is to allow caching of 836*6881a400Schristos expensive data lookups in the prev_register handling. */ 837*6881a400Schristos 838*6881a400Schristos struct dwarf2_frame_fn_data 839*6881a400Schristos { 840*6881a400Schristos /* The cookie to identify the custom function data by. */ 841*6881a400Schristos fn_prev_register cookie; 842*6881a400Schristos 843*6881a400Schristos /* The custom function data. */ 844*6881a400Schristos void *data; 845*6881a400Schristos 846*6881a400Schristos /* Pointer to the next custom function data object for this frame. */ 847*6881a400Schristos struct dwarf2_frame_fn_data *next; 848*6881a400Schristos }; 849*6881a400Schristos 8507d62b00eSchristos struct dwarf2_frame_cache 8517d62b00eSchristos { 8527d62b00eSchristos /* DWARF Call Frame Address. */ 8537d62b00eSchristos CORE_ADDR cfa; 8547d62b00eSchristos 8557d62b00eSchristos /* Set if the return address column was marked as unavailable 8567d62b00eSchristos (required non-collected memory or registers to compute). */ 8577d62b00eSchristos int unavailable_retaddr; 8587d62b00eSchristos 8597d62b00eSchristos /* Set if the return address column was marked as undefined. */ 8607d62b00eSchristos int undefined_retaddr; 8617d62b00eSchristos 8627d62b00eSchristos /* Saved registers, indexed by GDB register number, not by DWARF 8637d62b00eSchristos register number. */ 8647d62b00eSchristos struct dwarf2_frame_state_reg *reg; 8657d62b00eSchristos 8667d62b00eSchristos /* Return address register. */ 8677d62b00eSchristos struct dwarf2_frame_state_reg retaddr_reg; 8687d62b00eSchristos 8697d62b00eSchristos /* Target address size in bytes. */ 8707d62b00eSchristos int addr_size; 8717d62b00eSchristos 8727d62b00eSchristos /* The dwarf2_per_objfile from which this frame description came. */ 8737d62b00eSchristos dwarf2_per_objfile *per_objfile; 8747d62b00eSchristos 8757d62b00eSchristos /* If not NULL then this frame is the bottom frame of a TAILCALL_FRAME 8767d62b00eSchristos sequence. If NULL then it is a normal case with no TAILCALL_FRAME 8777d62b00eSchristos involved. Non-bottom frames of a virtual tail call frames chain use 8787d62b00eSchristos dwarf2_tailcall_frame_unwind unwinder so this field does not apply for 8797d62b00eSchristos them. */ 8807d62b00eSchristos void *tailcall_cache; 881*6881a400Schristos 882*6881a400Schristos struct dwarf2_frame_fn_data *fn_data; 8837d62b00eSchristos }; 8847d62b00eSchristos 8857d62b00eSchristos static struct dwarf2_frame_cache * 886*6881a400Schristos dwarf2_frame_cache (frame_info_ptr this_frame, void **this_cache) 8877d62b00eSchristos { 8887d62b00eSchristos struct gdbarch *gdbarch = get_frame_arch (this_frame); 8897d62b00eSchristos const int num_regs = gdbarch_num_cooked_regs (gdbarch); 8907d62b00eSchristos struct dwarf2_frame_cache *cache; 8917d62b00eSchristos struct dwarf2_fde *fde; 8927d62b00eSchristos CORE_ADDR entry_pc; 8937d62b00eSchristos const gdb_byte *instr; 8947d62b00eSchristos 8957d62b00eSchristos if (*this_cache) 8967d62b00eSchristos return (struct dwarf2_frame_cache *) *this_cache; 8977d62b00eSchristos 8987d62b00eSchristos /* Allocate a new cache. */ 8997d62b00eSchristos cache = FRAME_OBSTACK_ZALLOC (struct dwarf2_frame_cache); 9007d62b00eSchristos cache->reg = FRAME_OBSTACK_CALLOC (num_regs, struct dwarf2_frame_state_reg); 9017d62b00eSchristos *this_cache = cache; 9027d62b00eSchristos 9037d62b00eSchristos /* Unwind the PC. 9047d62b00eSchristos 9057d62b00eSchristos Note that if the next frame is never supposed to return (i.e. a call 9067d62b00eSchristos to abort), the compiler might optimize away the instruction at 9077d62b00eSchristos its return address. As a result the return address will 9087d62b00eSchristos point at some random instruction, and the CFI for that 9097d62b00eSchristos instruction is probably worthless to us. GCC's unwinder solves 9107d62b00eSchristos this problem by substracting 1 from the return address to get an 9117d62b00eSchristos address in the middle of a presumed call instruction (or the 9127d62b00eSchristos instruction in the associated delay slot). This should only be 9137d62b00eSchristos done for "normal" frames and not for resume-type frames (signal 9147d62b00eSchristos handlers, sentinel frames, dummy frames). The function 9157d62b00eSchristos get_frame_address_in_block does just this. It's not clear how 9167d62b00eSchristos reliable the method is though; there is the potential for the 9177d62b00eSchristos register state pre-call being different to that on return. */ 9187d62b00eSchristos CORE_ADDR pc1 = get_frame_address_in_block (this_frame); 9197d62b00eSchristos 9207d62b00eSchristos /* Find the correct FDE. */ 9217d62b00eSchristos fde = dwarf2_frame_find_fde (&pc1, &cache->per_objfile); 9227d62b00eSchristos gdb_assert (fde != NULL); 9237d62b00eSchristos gdb_assert (cache->per_objfile != nullptr); 9247d62b00eSchristos 9257d62b00eSchristos /* Allocate and initialize the frame state. */ 9267d62b00eSchristos struct dwarf2_frame_state fs (pc1, fde->cie); 9277d62b00eSchristos 9287d62b00eSchristos cache->addr_size = fde->cie->addr_size; 9297d62b00eSchristos 9307d62b00eSchristos /* Check for "quirks" - known bugs in producers. */ 9317d62b00eSchristos dwarf2_frame_find_quirks (&fs, fde); 9327d62b00eSchristos 9337d62b00eSchristos /* First decode all the insns in the CIE. */ 9347d62b00eSchristos execute_cfa_program (fde, fde->cie->initial_instructions, 9357d62b00eSchristos fde->cie->end, gdbarch, 9367d62b00eSchristos get_frame_address_in_block (this_frame), &fs, 9377d62b00eSchristos cache->per_objfile->objfile->text_section_offset ()); 9387d62b00eSchristos 9397d62b00eSchristos /* Save the initialized register set. */ 9407d62b00eSchristos fs.initial = fs.regs; 9417d62b00eSchristos 9427d62b00eSchristos /* Fetching the entry pc for THIS_FRAME won't necessarily result 9437d62b00eSchristos in an address that's within the range of FDE locations. This 9447d62b00eSchristos is due to the possibility of the function occupying non-contiguous 9457d62b00eSchristos ranges. */ 9467d62b00eSchristos LONGEST entry_cfa_sp_offset; 9477d62b00eSchristos int entry_cfa_sp_offset_p = 0; 9487d62b00eSchristos if (get_frame_func_if_available (this_frame, &entry_pc) 9497d62b00eSchristos && fde->initial_location <= entry_pc 9507d62b00eSchristos && entry_pc < fde->initial_location + fde->address_range) 9517d62b00eSchristos { 9527d62b00eSchristos /* Decode the insns in the FDE up to the entry PC. */ 9537d62b00eSchristos instr = execute_cfa_program 9547d62b00eSchristos (fde, fde->instructions, fde->end, gdbarch, entry_pc, &fs, 9557d62b00eSchristos cache->per_objfile->objfile->text_section_offset ()); 9567d62b00eSchristos 9577d62b00eSchristos if (fs.regs.cfa_how == CFA_REG_OFFSET 9587d62b00eSchristos && (dwarf_reg_to_regnum (gdbarch, fs.regs.cfa_reg) 9597d62b00eSchristos == gdbarch_sp_regnum (gdbarch))) 9607d62b00eSchristos { 9617d62b00eSchristos entry_cfa_sp_offset = fs.regs.cfa_offset; 9627d62b00eSchristos entry_cfa_sp_offset_p = 1; 9637d62b00eSchristos } 9647d62b00eSchristos } 9657d62b00eSchristos else 9667d62b00eSchristos instr = fde->instructions; 9677d62b00eSchristos 9687d62b00eSchristos /* Then decode the insns in the FDE up to our target PC. */ 9697d62b00eSchristos execute_cfa_program (fde, instr, fde->end, gdbarch, 9707d62b00eSchristos get_frame_address_in_block (this_frame), &fs, 9717d62b00eSchristos cache->per_objfile->objfile->text_section_offset ()); 9727d62b00eSchristos 9737d62b00eSchristos try 9747d62b00eSchristos { 9757d62b00eSchristos /* Calculate the CFA. */ 9767d62b00eSchristos switch (fs.regs.cfa_how) 9777d62b00eSchristos { 9787d62b00eSchristos case CFA_REG_OFFSET: 9797d62b00eSchristos cache->cfa = read_addr_from_reg (this_frame, fs.regs.cfa_reg); 9807d62b00eSchristos if (fs.armcc_cfa_offsets_reversed) 9817d62b00eSchristos cache->cfa -= fs.regs.cfa_offset; 9827d62b00eSchristos else 9837d62b00eSchristos cache->cfa += fs.regs.cfa_offset; 9847d62b00eSchristos break; 9857d62b00eSchristos 9867d62b00eSchristos case CFA_EXP: 9877d62b00eSchristos cache->cfa = 9887d62b00eSchristos execute_stack_op (fs.regs.cfa_exp, fs.regs.cfa_exp_len, 9897d62b00eSchristos cache->addr_size, this_frame, 0, 0, 9907d62b00eSchristos cache->per_objfile); 9917d62b00eSchristos break; 9927d62b00eSchristos 9937d62b00eSchristos default: 994*6881a400Schristos internal_error (_("Unknown CFA rule.")); 9957d62b00eSchristos } 9967d62b00eSchristos } 9977d62b00eSchristos catch (const gdb_exception_error &ex) 9987d62b00eSchristos { 9997d62b00eSchristos if (ex.error == NOT_AVAILABLE_ERROR) 10007d62b00eSchristos { 10017d62b00eSchristos cache->unavailable_retaddr = 1; 10027d62b00eSchristos return cache; 10037d62b00eSchristos } 10047d62b00eSchristos 10057d62b00eSchristos throw; 10067d62b00eSchristos } 10077d62b00eSchristos 10087d62b00eSchristos /* Initialize the register state. */ 10097d62b00eSchristos { 10107d62b00eSchristos int regnum; 10117d62b00eSchristos 10127d62b00eSchristos for (regnum = 0; regnum < num_regs; regnum++) 10137d62b00eSchristos dwarf2_frame_init_reg (gdbarch, regnum, &cache->reg[regnum], this_frame); 10147d62b00eSchristos } 10157d62b00eSchristos 10167d62b00eSchristos /* Go through the DWARF2 CFI generated table and save its register 10177d62b00eSchristos location information in the cache. Note that we don't skip the 10187d62b00eSchristos return address column; it's perfectly all right for it to 10197d62b00eSchristos correspond to a real register. */ 10207d62b00eSchristos { 10217d62b00eSchristos int column; /* CFI speak for "register number". */ 10227d62b00eSchristos 10237d62b00eSchristos for (column = 0; column < fs.regs.reg.size (); column++) 10247d62b00eSchristos { 10257d62b00eSchristos /* Use the GDB register number as the destination index. */ 10267d62b00eSchristos int regnum = dwarf_reg_to_regnum (gdbarch, column); 10277d62b00eSchristos 10287d62b00eSchristos /* Protect against a target returning a bad register. */ 10297d62b00eSchristos if (regnum < 0 || regnum >= num_regs) 10307d62b00eSchristos continue; 10317d62b00eSchristos 10327d62b00eSchristos /* NOTE: cagney/2003-09-05: CFI should specify the disposition 10337d62b00eSchristos of all debug info registers. If it doesn't, complain (but 10347d62b00eSchristos not too loudly). It turns out that GCC assumes that an 10357d62b00eSchristos unspecified register implies "same value" when CFI (draft 10367d62b00eSchristos 7) specifies nothing at all. Such a register could equally 10377d62b00eSchristos be interpreted as "undefined". Also note that this check 10387d62b00eSchristos isn't sufficient; it only checks that all registers in the 10397d62b00eSchristos range [0 .. max column] are specified, and won't detect 10407d62b00eSchristos problems when a debug info register falls outside of the 10417d62b00eSchristos table. We need a way of iterating through all the valid 10427d62b00eSchristos DWARF2 register numbers. */ 10437d62b00eSchristos if (fs.regs.reg[column].how == DWARF2_FRAME_REG_UNSPECIFIED) 10447d62b00eSchristos { 10457d62b00eSchristos if (cache->reg[regnum].how == DWARF2_FRAME_REG_UNSPECIFIED) 10467d62b00eSchristos complaint (_("\ 10477d62b00eSchristos incomplete CFI data; unspecified registers (e.g., %s) at %s"), 10487d62b00eSchristos gdbarch_register_name (gdbarch, regnum), 10497d62b00eSchristos paddress (gdbarch, fs.pc)); 10507d62b00eSchristos } 10517d62b00eSchristos else 10527d62b00eSchristos cache->reg[regnum] = fs.regs.reg[column]; 10537d62b00eSchristos } 10547d62b00eSchristos } 10557d62b00eSchristos 10567d62b00eSchristos /* Eliminate any DWARF2_FRAME_REG_RA rules, and save the information 10577d62b00eSchristos we need for evaluating DWARF2_FRAME_REG_RA_OFFSET rules. */ 10587d62b00eSchristos { 10597d62b00eSchristos int regnum; 10607d62b00eSchristos 10617d62b00eSchristos for (regnum = 0; regnum < num_regs; regnum++) 10627d62b00eSchristos { 10637d62b00eSchristos if (cache->reg[regnum].how == DWARF2_FRAME_REG_RA 10647d62b00eSchristos || cache->reg[regnum].how == DWARF2_FRAME_REG_RA_OFFSET) 10657d62b00eSchristos { 10667d62b00eSchristos const std::vector<struct dwarf2_frame_state_reg> ®s 10677d62b00eSchristos = fs.regs.reg; 10687d62b00eSchristos ULONGEST retaddr_column = fs.retaddr_column; 10697d62b00eSchristos 10707d62b00eSchristos /* It seems rather bizarre to specify an "empty" column as 10717d62b00eSchristos the return adress column. However, this is exactly 10727d62b00eSchristos what GCC does on some targets. It turns out that GCC 10737d62b00eSchristos assumes that the return address can be found in the 10747d62b00eSchristos register corresponding to the return address column. 10757d62b00eSchristos Incidentally, that's how we should treat a return 10767d62b00eSchristos address column specifying "same value" too. */ 10777d62b00eSchristos if (fs.retaddr_column < fs.regs.reg.size () 10787d62b00eSchristos && regs[retaddr_column].how != DWARF2_FRAME_REG_UNSPECIFIED 10797d62b00eSchristos && regs[retaddr_column].how != DWARF2_FRAME_REG_SAME_VALUE) 10807d62b00eSchristos { 10817d62b00eSchristos if (cache->reg[regnum].how == DWARF2_FRAME_REG_RA) 10827d62b00eSchristos cache->reg[regnum] = regs[retaddr_column]; 10837d62b00eSchristos else 10847d62b00eSchristos cache->retaddr_reg = regs[retaddr_column]; 10857d62b00eSchristos } 10867d62b00eSchristos else 10877d62b00eSchristos { 10887d62b00eSchristos if (cache->reg[regnum].how == DWARF2_FRAME_REG_RA) 10897d62b00eSchristos { 10907d62b00eSchristos cache->reg[regnum].loc.reg = fs.retaddr_column; 10917d62b00eSchristos cache->reg[regnum].how = DWARF2_FRAME_REG_SAVED_REG; 10927d62b00eSchristos } 10937d62b00eSchristos else 10947d62b00eSchristos { 10957d62b00eSchristos cache->retaddr_reg.loc.reg = fs.retaddr_column; 10967d62b00eSchristos cache->retaddr_reg.how = DWARF2_FRAME_REG_SAVED_REG; 10977d62b00eSchristos } 10987d62b00eSchristos } 10997d62b00eSchristos } 11007d62b00eSchristos } 11017d62b00eSchristos } 11027d62b00eSchristos 11037d62b00eSchristos if (fs.retaddr_column < fs.regs.reg.size () 11047d62b00eSchristos && fs.regs.reg[fs.retaddr_column].how == DWARF2_FRAME_REG_UNDEFINED) 11057d62b00eSchristos cache->undefined_retaddr = 1; 11067d62b00eSchristos 11077d62b00eSchristos dwarf2_tailcall_sniffer_first (this_frame, &cache->tailcall_cache, 11087d62b00eSchristos (entry_cfa_sp_offset_p 11097d62b00eSchristos ? &entry_cfa_sp_offset : NULL)); 11107d62b00eSchristos 11117d62b00eSchristos return cache; 11127d62b00eSchristos } 11137d62b00eSchristos 11147d62b00eSchristos static enum unwind_stop_reason 1115*6881a400Schristos dwarf2_frame_unwind_stop_reason (frame_info_ptr this_frame, 11167d62b00eSchristos void **this_cache) 11177d62b00eSchristos { 11187d62b00eSchristos struct dwarf2_frame_cache *cache 11197d62b00eSchristos = dwarf2_frame_cache (this_frame, this_cache); 11207d62b00eSchristos 11217d62b00eSchristos if (cache->unavailable_retaddr) 11227d62b00eSchristos return UNWIND_UNAVAILABLE; 11237d62b00eSchristos 11247d62b00eSchristos if (cache->undefined_retaddr) 11257d62b00eSchristos return UNWIND_OUTERMOST; 11267d62b00eSchristos 11277d62b00eSchristos return UNWIND_NO_REASON; 11287d62b00eSchristos } 11297d62b00eSchristos 11307d62b00eSchristos static void 1131*6881a400Schristos dwarf2_frame_this_id (frame_info_ptr this_frame, void **this_cache, 11327d62b00eSchristos struct frame_id *this_id) 11337d62b00eSchristos { 11347d62b00eSchristos struct dwarf2_frame_cache *cache = 11357d62b00eSchristos dwarf2_frame_cache (this_frame, this_cache); 11367d62b00eSchristos 11377d62b00eSchristos if (cache->unavailable_retaddr) 11387d62b00eSchristos (*this_id) = frame_id_build_unavailable_stack (get_frame_func (this_frame)); 11397d62b00eSchristos else if (cache->undefined_retaddr) 11407d62b00eSchristos return; 11417d62b00eSchristos else 11427d62b00eSchristos (*this_id) = frame_id_build (cache->cfa, get_frame_func (this_frame)); 11437d62b00eSchristos } 11447d62b00eSchristos 11457d62b00eSchristos static struct value * 1146*6881a400Schristos dwarf2_frame_prev_register (frame_info_ptr this_frame, void **this_cache, 11477d62b00eSchristos int regnum) 11487d62b00eSchristos { 11497d62b00eSchristos struct gdbarch *gdbarch = get_frame_arch (this_frame); 11507d62b00eSchristos struct dwarf2_frame_cache *cache = 11517d62b00eSchristos dwarf2_frame_cache (this_frame, this_cache); 11527d62b00eSchristos CORE_ADDR addr; 11537d62b00eSchristos int realnum; 11547d62b00eSchristos 11557d62b00eSchristos /* Non-bottom frames of a virtual tail call frames chain use 11567d62b00eSchristos dwarf2_tailcall_frame_unwind unwinder so this code does not apply for 11577d62b00eSchristos them. If dwarf2_tailcall_prev_register_first does not have specific value 11587d62b00eSchristos unwind the register, tail call frames are assumed to have the register set 11597d62b00eSchristos of the top caller. */ 11607d62b00eSchristos if (cache->tailcall_cache) 11617d62b00eSchristos { 11627d62b00eSchristos struct value *val; 11637d62b00eSchristos 11647d62b00eSchristos val = dwarf2_tailcall_prev_register_first (this_frame, 11657d62b00eSchristos &cache->tailcall_cache, 11667d62b00eSchristos regnum); 11677d62b00eSchristos if (val) 11687d62b00eSchristos return val; 11697d62b00eSchristos } 11707d62b00eSchristos 11717d62b00eSchristos switch (cache->reg[regnum].how) 11727d62b00eSchristos { 11737d62b00eSchristos case DWARF2_FRAME_REG_UNDEFINED: 11747d62b00eSchristos /* If CFI explicitly specified that the value isn't defined, 11757d62b00eSchristos mark it as optimized away; the value isn't available. */ 11767d62b00eSchristos return frame_unwind_got_optimized (this_frame, regnum); 11777d62b00eSchristos 11787d62b00eSchristos case DWARF2_FRAME_REG_SAVED_OFFSET: 11797d62b00eSchristos addr = cache->cfa + cache->reg[regnum].loc.offset; 11807d62b00eSchristos return frame_unwind_got_memory (this_frame, regnum, addr); 11817d62b00eSchristos 11827d62b00eSchristos case DWARF2_FRAME_REG_SAVED_REG: 11837d62b00eSchristos realnum = dwarf_reg_to_regnum_or_error 11847d62b00eSchristos (gdbarch, cache->reg[regnum].loc.reg); 11857d62b00eSchristos return frame_unwind_got_register (this_frame, regnum, realnum); 11867d62b00eSchristos 11877d62b00eSchristos case DWARF2_FRAME_REG_SAVED_EXP: 11887d62b00eSchristos addr = execute_stack_op (cache->reg[regnum].loc.exp.start, 11897d62b00eSchristos cache->reg[regnum].loc.exp.len, 11907d62b00eSchristos cache->addr_size, 11917d62b00eSchristos this_frame, cache->cfa, 1, 11927d62b00eSchristos cache->per_objfile); 11937d62b00eSchristos return frame_unwind_got_memory (this_frame, regnum, addr); 11947d62b00eSchristos 11957d62b00eSchristos case DWARF2_FRAME_REG_SAVED_VAL_OFFSET: 11967d62b00eSchristos addr = cache->cfa + cache->reg[regnum].loc.offset; 11977d62b00eSchristos return frame_unwind_got_constant (this_frame, regnum, addr); 11987d62b00eSchristos 11997d62b00eSchristos case DWARF2_FRAME_REG_SAVED_VAL_EXP: 12007d62b00eSchristos addr = execute_stack_op (cache->reg[regnum].loc.exp.start, 12017d62b00eSchristos cache->reg[regnum].loc.exp.len, 12027d62b00eSchristos cache->addr_size, 12037d62b00eSchristos this_frame, cache->cfa, 1, 12047d62b00eSchristos cache->per_objfile); 12057d62b00eSchristos return frame_unwind_got_constant (this_frame, regnum, addr); 12067d62b00eSchristos 12077d62b00eSchristos case DWARF2_FRAME_REG_UNSPECIFIED: 12087d62b00eSchristos /* GCC, in its infinite wisdom decided to not provide unwind 12097d62b00eSchristos information for registers that are "same value". Since 12107d62b00eSchristos DWARF2 (3 draft 7) doesn't define such behavior, said 12117d62b00eSchristos registers are actually undefined (which is different to CFI 12127d62b00eSchristos "undefined"). Code above issues a complaint about this. 12137d62b00eSchristos Here just fudge the books, assume GCC, and that the value is 12147d62b00eSchristos more inner on the stack. */ 12157d62b00eSchristos return frame_unwind_got_register (this_frame, regnum, regnum); 12167d62b00eSchristos 12177d62b00eSchristos case DWARF2_FRAME_REG_SAME_VALUE: 12187d62b00eSchristos return frame_unwind_got_register (this_frame, regnum, regnum); 12197d62b00eSchristos 12207d62b00eSchristos case DWARF2_FRAME_REG_CFA: 12217d62b00eSchristos return frame_unwind_got_address (this_frame, regnum, cache->cfa); 12227d62b00eSchristos 12237d62b00eSchristos case DWARF2_FRAME_REG_CFA_OFFSET: 12247d62b00eSchristos addr = cache->cfa + cache->reg[regnum].loc.offset; 12257d62b00eSchristos return frame_unwind_got_address (this_frame, regnum, addr); 12267d62b00eSchristos 12277d62b00eSchristos case DWARF2_FRAME_REG_RA_OFFSET: 12287d62b00eSchristos addr = cache->reg[regnum].loc.offset; 12297d62b00eSchristos regnum = dwarf_reg_to_regnum_or_error 12307d62b00eSchristos (gdbarch, cache->retaddr_reg.loc.reg); 12317d62b00eSchristos addr += get_frame_register_unsigned (this_frame, regnum); 12327d62b00eSchristos return frame_unwind_got_address (this_frame, regnum, addr); 12337d62b00eSchristos 12347d62b00eSchristos case DWARF2_FRAME_REG_FN: 12357d62b00eSchristos return cache->reg[regnum].loc.fn (this_frame, this_cache, regnum); 12367d62b00eSchristos 12377d62b00eSchristos default: 1238*6881a400Schristos internal_error (_("Unknown register rule.")); 12397d62b00eSchristos } 12407d62b00eSchristos } 12417d62b00eSchristos 1242*6881a400Schristos /* See frame.h. */ 1243*6881a400Schristos 1244*6881a400Schristos void * 1245*6881a400Schristos dwarf2_frame_get_fn_data (frame_info_ptr this_frame, void **this_cache, 1246*6881a400Schristos fn_prev_register cookie) 1247*6881a400Schristos { 1248*6881a400Schristos struct dwarf2_frame_fn_data *fn_data = nullptr; 1249*6881a400Schristos struct dwarf2_frame_cache *cache 1250*6881a400Schristos = dwarf2_frame_cache (this_frame, this_cache); 1251*6881a400Schristos 1252*6881a400Schristos /* Find the object for the function. */ 1253*6881a400Schristos for (fn_data = cache->fn_data; fn_data; fn_data = fn_data->next) 1254*6881a400Schristos if (fn_data->cookie == cookie) 1255*6881a400Schristos return fn_data->data; 1256*6881a400Schristos 1257*6881a400Schristos return nullptr; 1258*6881a400Schristos } 1259*6881a400Schristos 1260*6881a400Schristos /* See frame.h. */ 1261*6881a400Schristos 1262*6881a400Schristos void * 1263*6881a400Schristos dwarf2_frame_allocate_fn_data (frame_info_ptr this_frame, void **this_cache, 1264*6881a400Schristos fn_prev_register cookie, unsigned long size) 1265*6881a400Schristos { 1266*6881a400Schristos struct dwarf2_frame_fn_data *fn_data = nullptr; 1267*6881a400Schristos struct dwarf2_frame_cache *cache 1268*6881a400Schristos = dwarf2_frame_cache (this_frame, this_cache); 1269*6881a400Schristos 1270*6881a400Schristos /* First try to find an existing object. */ 1271*6881a400Schristos void *data = dwarf2_frame_get_fn_data (this_frame, this_cache, cookie); 1272*6881a400Schristos gdb_assert (data == nullptr); 1273*6881a400Schristos 1274*6881a400Schristos /* No object found, lets create a new instance. */ 1275*6881a400Schristos fn_data = FRAME_OBSTACK_ZALLOC (struct dwarf2_frame_fn_data); 1276*6881a400Schristos fn_data->cookie = cookie; 1277*6881a400Schristos fn_data->data = frame_obstack_zalloc (size); 1278*6881a400Schristos fn_data->next = cache->fn_data; 1279*6881a400Schristos cache->fn_data = fn_data; 1280*6881a400Schristos 1281*6881a400Schristos return fn_data->data; 1282*6881a400Schristos } 1283*6881a400Schristos 12847d62b00eSchristos /* Proxy for tailcall_frame_dealloc_cache for bottom frame of a virtual tail 12857d62b00eSchristos call frames chain. */ 12867d62b00eSchristos 12877d62b00eSchristos static void 1288*6881a400Schristos dwarf2_frame_dealloc_cache (frame_info *self, void *this_cache) 12897d62b00eSchristos { 1290*6881a400Schristos struct dwarf2_frame_cache *cache 1291*6881a400Schristos = dwarf2_frame_cache (frame_info_ptr (self), &this_cache); 12927d62b00eSchristos 12937d62b00eSchristos if (cache->tailcall_cache) 12947d62b00eSchristos dwarf2_tailcall_frame_unwind.dealloc_cache (self, cache->tailcall_cache); 12957d62b00eSchristos } 12967d62b00eSchristos 12977d62b00eSchristos static int 12987d62b00eSchristos dwarf2_frame_sniffer (const struct frame_unwind *self, 1299*6881a400Schristos frame_info_ptr this_frame, void **this_cache) 13007d62b00eSchristos { 13017d62b00eSchristos if (!dwarf2_frame_unwinders_enabled_p) 13027d62b00eSchristos return 0; 13037d62b00eSchristos 13047d62b00eSchristos /* Grab an address that is guaranteed to reside somewhere within the 13057d62b00eSchristos function. get_frame_pc(), with a no-return next function, can 13067d62b00eSchristos end up returning something past the end of this function's body. 13077d62b00eSchristos If the frame we're sniffing for is a signal frame whose start 13087d62b00eSchristos address is placed on the stack by the OS, its FDE must 13097d62b00eSchristos extend one byte before its start address or we could potentially 13107d62b00eSchristos select the FDE of the previous function. */ 13117d62b00eSchristos CORE_ADDR block_addr = get_frame_address_in_block (this_frame); 13127d62b00eSchristos struct dwarf2_fde *fde = dwarf2_frame_find_fde (&block_addr, NULL); 13137d62b00eSchristos 13147d62b00eSchristos if (!fde) 13157d62b00eSchristos return 0; 13167d62b00eSchristos 13177d62b00eSchristos /* On some targets, signal trampolines may have unwind information. 13187d62b00eSchristos We need to recognize them so that we set the frame type 13197d62b00eSchristos correctly. */ 13207d62b00eSchristos 13217d62b00eSchristos if (fde->cie->signal_frame 13227d62b00eSchristos || dwarf2_frame_signal_frame_p (get_frame_arch (this_frame), 13237d62b00eSchristos this_frame)) 13247d62b00eSchristos return self->type == SIGTRAMP_FRAME; 13257d62b00eSchristos 13267d62b00eSchristos if (self->type != NORMAL_FRAME) 13277d62b00eSchristos return 0; 13287d62b00eSchristos 13297d62b00eSchristos return 1; 13307d62b00eSchristos } 13317d62b00eSchristos 13327d62b00eSchristos static const struct frame_unwind dwarf2_frame_unwind = 13337d62b00eSchristos { 1334*6881a400Schristos "dwarf2", 13357d62b00eSchristos NORMAL_FRAME, 13367d62b00eSchristos dwarf2_frame_unwind_stop_reason, 13377d62b00eSchristos dwarf2_frame_this_id, 13387d62b00eSchristos dwarf2_frame_prev_register, 13397d62b00eSchristos NULL, 13407d62b00eSchristos dwarf2_frame_sniffer, 13417d62b00eSchristos dwarf2_frame_dealloc_cache 13427d62b00eSchristos }; 13437d62b00eSchristos 13447d62b00eSchristos static const struct frame_unwind dwarf2_signal_frame_unwind = 13457d62b00eSchristos { 1346*6881a400Schristos "dwarf2 signal", 13477d62b00eSchristos SIGTRAMP_FRAME, 13487d62b00eSchristos dwarf2_frame_unwind_stop_reason, 13497d62b00eSchristos dwarf2_frame_this_id, 13507d62b00eSchristos dwarf2_frame_prev_register, 13517d62b00eSchristos NULL, 13527d62b00eSchristos dwarf2_frame_sniffer, 13537d62b00eSchristos 13547d62b00eSchristos /* TAILCALL_CACHE can never be in such frame to need dealloc_cache. */ 13557d62b00eSchristos NULL 13567d62b00eSchristos }; 13577d62b00eSchristos 13587d62b00eSchristos /* Append the DWARF-2 frame unwinders to GDBARCH's list. */ 13597d62b00eSchristos 13607d62b00eSchristos void 13617d62b00eSchristos dwarf2_append_unwinders (struct gdbarch *gdbarch) 13627d62b00eSchristos { 13637d62b00eSchristos frame_unwind_append_unwinder (gdbarch, &dwarf2_frame_unwind); 13647d62b00eSchristos frame_unwind_append_unwinder (gdbarch, &dwarf2_signal_frame_unwind); 13657d62b00eSchristos } 13667d62b00eSchristos 13677d62b00eSchristos 13687d62b00eSchristos /* There is no explicitly defined relationship between the CFA and the 13697d62b00eSchristos location of frame's local variables and arguments/parameters. 13707d62b00eSchristos Therefore, frame base methods on this page should probably only be 13717d62b00eSchristos used as a last resort, just to avoid printing total garbage as a 13727d62b00eSchristos response to the "info frame" command. */ 13737d62b00eSchristos 13747d62b00eSchristos static CORE_ADDR 1375*6881a400Schristos dwarf2_frame_base_address (frame_info_ptr this_frame, void **this_cache) 13767d62b00eSchristos { 13777d62b00eSchristos struct dwarf2_frame_cache *cache = 13787d62b00eSchristos dwarf2_frame_cache (this_frame, this_cache); 13797d62b00eSchristos 13807d62b00eSchristos return cache->cfa; 13817d62b00eSchristos } 13827d62b00eSchristos 13837d62b00eSchristos static const struct frame_base dwarf2_frame_base = 13847d62b00eSchristos { 13857d62b00eSchristos &dwarf2_frame_unwind, 13867d62b00eSchristos dwarf2_frame_base_address, 13877d62b00eSchristos dwarf2_frame_base_address, 13887d62b00eSchristos dwarf2_frame_base_address 13897d62b00eSchristos }; 13907d62b00eSchristos 13917d62b00eSchristos const struct frame_base * 1392*6881a400Schristos dwarf2_frame_base_sniffer (frame_info_ptr this_frame) 13937d62b00eSchristos { 13947d62b00eSchristos CORE_ADDR block_addr = get_frame_address_in_block (this_frame); 13957d62b00eSchristos 13967d62b00eSchristos if (dwarf2_frame_find_fde (&block_addr, NULL)) 13977d62b00eSchristos return &dwarf2_frame_base; 13987d62b00eSchristos 13997d62b00eSchristos return NULL; 14007d62b00eSchristos } 14017d62b00eSchristos 14027d62b00eSchristos /* Compute the CFA for THIS_FRAME, but only if THIS_FRAME came from 14037d62b00eSchristos the DWARF unwinder. This is used to implement 14047d62b00eSchristos DW_OP_call_frame_cfa. */ 14057d62b00eSchristos 14067d62b00eSchristos CORE_ADDR 1407*6881a400Schristos dwarf2_frame_cfa (frame_info_ptr this_frame) 14087d62b00eSchristos { 14097d62b00eSchristos if (frame_unwinder_is (this_frame, &record_btrace_tailcall_frame_unwind) 14107d62b00eSchristos || frame_unwinder_is (this_frame, &record_btrace_frame_unwind)) 14117d62b00eSchristos throw_error (NOT_AVAILABLE_ERROR, 14127d62b00eSchristos _("cfa not available for record btrace target")); 14137d62b00eSchristos 14147d62b00eSchristos while (get_frame_type (this_frame) == INLINE_FRAME) 14157d62b00eSchristos this_frame = get_prev_frame (this_frame); 14167d62b00eSchristos if (get_frame_unwind_stop_reason (this_frame) == UNWIND_UNAVAILABLE) 14177d62b00eSchristos throw_error (NOT_AVAILABLE_ERROR, 14187d62b00eSchristos _("can't compute CFA for this frame: " 14197d62b00eSchristos "required registers or memory are unavailable")); 14207d62b00eSchristos 14217d62b00eSchristos if (get_frame_id (this_frame).stack_status != FID_STACK_VALID) 14227d62b00eSchristos throw_error (NOT_AVAILABLE_ERROR, 14237d62b00eSchristos _("can't compute CFA for this frame: " 14247d62b00eSchristos "frame base not available")); 14257d62b00eSchristos 14267d62b00eSchristos return get_frame_base (this_frame); 14277d62b00eSchristos } 14287d62b00eSchristos 14297d62b00eSchristos /* We store the frame data on the BFD. This is only done if it is 14307d62b00eSchristos independent of the address space and so can be shared. */ 1431*6881a400Schristos static const registry<bfd>::key<comp_unit> dwarf2_frame_bfd_data; 14327d62b00eSchristos 14337d62b00eSchristos /* If any BFD sections require relocations (note; really should be if 14347d62b00eSchristos any debug info requires relocations), then we store the frame data 14357d62b00eSchristos on the objfile instead, and do not share it. */ 1436*6881a400Schristos static const registry<objfile>::key<comp_unit> dwarf2_frame_objfile_data; 14377d62b00eSchristos 14387d62b00eSchristos 14397d62b00eSchristos /* Pointer encoding helper functions. */ 14407d62b00eSchristos 14417d62b00eSchristos /* GCC supports exception handling based on DWARF2 CFI. However, for 14427d62b00eSchristos technical reasons, it encodes addresses in its FDE's in a different 14437d62b00eSchristos way. Several "pointer encodings" are supported. The encoding 14447d62b00eSchristos that's used for a particular FDE is determined by the 'R' 14457d62b00eSchristos augmentation in the associated CIE. The argument of this 14467d62b00eSchristos augmentation is a single byte. 14477d62b00eSchristos 14487d62b00eSchristos The address can be encoded as 2 bytes, 4 bytes, 8 bytes, or as a 14497d62b00eSchristos LEB128. This is encoded in bits 0, 1 and 2. Bit 3 encodes whether 14507d62b00eSchristos the address is signed or unsigned. Bits 4, 5 and 6 encode how the 14517d62b00eSchristos address should be interpreted (absolute, relative to the current 14527d62b00eSchristos position in the FDE, ...). Bit 7, indicates that the address 14537d62b00eSchristos should be dereferenced. */ 14547d62b00eSchristos 14557d62b00eSchristos static gdb_byte 14567d62b00eSchristos encoding_for_size (unsigned int size) 14577d62b00eSchristos { 14587d62b00eSchristos switch (size) 14597d62b00eSchristos { 14607d62b00eSchristos case 2: 14617d62b00eSchristos return DW_EH_PE_udata2; 14627d62b00eSchristos case 4: 14637d62b00eSchristos return DW_EH_PE_udata4; 14647d62b00eSchristos case 8: 14657d62b00eSchristos return DW_EH_PE_udata8; 14667d62b00eSchristos default: 1467*6881a400Schristos internal_error (_("Unsupported address size")); 14687d62b00eSchristos } 14697d62b00eSchristos } 14707d62b00eSchristos 14717d62b00eSchristos static CORE_ADDR 14727d62b00eSchristos read_encoded_value (struct comp_unit *unit, gdb_byte encoding, 14737d62b00eSchristos int ptr_len, const gdb_byte *buf, 14747d62b00eSchristos unsigned int *bytes_read_ptr, 14757d62b00eSchristos CORE_ADDR func_base) 14767d62b00eSchristos { 14777d62b00eSchristos ptrdiff_t offset; 14787d62b00eSchristos CORE_ADDR base; 14797d62b00eSchristos 14807d62b00eSchristos /* GCC currently doesn't generate DW_EH_PE_indirect encodings for 14817d62b00eSchristos FDE's. */ 14827d62b00eSchristos if (encoding & DW_EH_PE_indirect) 1483*6881a400Schristos internal_error (_("Unsupported encoding: DW_EH_PE_indirect")); 14847d62b00eSchristos 14857d62b00eSchristos *bytes_read_ptr = 0; 14867d62b00eSchristos 14877d62b00eSchristos switch (encoding & 0x70) 14887d62b00eSchristos { 14897d62b00eSchristos case DW_EH_PE_absptr: 14907d62b00eSchristos base = 0; 14917d62b00eSchristos break; 14927d62b00eSchristos case DW_EH_PE_pcrel: 14937d62b00eSchristos base = bfd_section_vma (unit->dwarf_frame_section); 14947d62b00eSchristos base += (buf - unit->dwarf_frame_buffer); 14957d62b00eSchristos break; 14967d62b00eSchristos case DW_EH_PE_datarel: 14977d62b00eSchristos base = unit->dbase; 14987d62b00eSchristos break; 14997d62b00eSchristos case DW_EH_PE_textrel: 15007d62b00eSchristos base = unit->tbase; 15017d62b00eSchristos break; 15027d62b00eSchristos case DW_EH_PE_funcrel: 15037d62b00eSchristos base = func_base; 15047d62b00eSchristos break; 15057d62b00eSchristos case DW_EH_PE_aligned: 15067d62b00eSchristos base = 0; 15077d62b00eSchristos offset = buf - unit->dwarf_frame_buffer; 15087d62b00eSchristos if ((offset % ptr_len) != 0) 15097d62b00eSchristos { 15107d62b00eSchristos *bytes_read_ptr = ptr_len - (offset % ptr_len); 15117d62b00eSchristos buf += *bytes_read_ptr; 15127d62b00eSchristos } 15137d62b00eSchristos break; 15147d62b00eSchristos default: 1515*6881a400Schristos internal_error (_("Invalid or unsupported encoding")); 15167d62b00eSchristos } 15177d62b00eSchristos 15187d62b00eSchristos if ((encoding & 0x07) == 0x00) 15197d62b00eSchristos { 15207d62b00eSchristos encoding |= encoding_for_size (ptr_len); 15217d62b00eSchristos if (bfd_get_sign_extend_vma (unit->abfd)) 15227d62b00eSchristos encoding |= DW_EH_PE_signed; 15237d62b00eSchristos } 15247d62b00eSchristos 15257d62b00eSchristos switch (encoding & 0x0f) 15267d62b00eSchristos { 15277d62b00eSchristos case DW_EH_PE_uleb128: 15287d62b00eSchristos { 15297d62b00eSchristos uint64_t value; 15307d62b00eSchristos const gdb_byte *end_buf = buf + (sizeof (value) + 1) * 8 / 7; 15317d62b00eSchristos 15327d62b00eSchristos *bytes_read_ptr += safe_read_uleb128 (buf, end_buf, &value) - buf; 15337d62b00eSchristos return base + value; 15347d62b00eSchristos } 15357d62b00eSchristos case DW_EH_PE_udata2: 15367d62b00eSchristos *bytes_read_ptr += 2; 15377d62b00eSchristos return (base + bfd_get_16 (unit->abfd, (bfd_byte *) buf)); 15387d62b00eSchristos case DW_EH_PE_udata4: 15397d62b00eSchristos *bytes_read_ptr += 4; 15407d62b00eSchristos return (base + bfd_get_32 (unit->abfd, (bfd_byte *) buf)); 15417d62b00eSchristos case DW_EH_PE_udata8: 15427d62b00eSchristos *bytes_read_ptr += 8; 15437d62b00eSchristos return (base + bfd_get_64 (unit->abfd, (bfd_byte *) buf)); 15447d62b00eSchristos case DW_EH_PE_sleb128: 15457d62b00eSchristos { 15467d62b00eSchristos int64_t value; 15477d62b00eSchristos const gdb_byte *end_buf = buf + (sizeof (value) + 1) * 8 / 7; 15487d62b00eSchristos 15497d62b00eSchristos *bytes_read_ptr += safe_read_sleb128 (buf, end_buf, &value) - buf; 15507d62b00eSchristos return base + value; 15517d62b00eSchristos } 15527d62b00eSchristos case DW_EH_PE_sdata2: 15537d62b00eSchristos *bytes_read_ptr += 2; 15547d62b00eSchristos return (base + bfd_get_signed_16 (unit->abfd, (bfd_byte *) buf)); 15557d62b00eSchristos case DW_EH_PE_sdata4: 15567d62b00eSchristos *bytes_read_ptr += 4; 15577d62b00eSchristos return (base + bfd_get_signed_32 (unit->abfd, (bfd_byte *) buf)); 15587d62b00eSchristos case DW_EH_PE_sdata8: 15597d62b00eSchristos *bytes_read_ptr += 8; 15607d62b00eSchristos return (base + bfd_get_signed_64 (unit->abfd, (bfd_byte *) buf)); 15617d62b00eSchristos default: 1562*6881a400Schristos internal_error (_("Invalid or unsupported encoding")); 15637d62b00eSchristos } 15647d62b00eSchristos } 15657d62b00eSchristos 15667d62b00eSchristos 15677d62b00eSchristos /* Find CIE with the given CIE_POINTER in CIE_TABLE. */ 15687d62b00eSchristos static struct dwarf2_cie * 15697d62b00eSchristos find_cie (const dwarf2_cie_table &cie_table, ULONGEST cie_pointer) 15707d62b00eSchristos { 15717d62b00eSchristos auto iter = cie_table.find (cie_pointer); 15727d62b00eSchristos if (iter != cie_table.end ()) 15737d62b00eSchristos return iter->second; 15747d62b00eSchristos return NULL; 15757d62b00eSchristos } 15767d62b00eSchristos 15777d62b00eSchristos static inline int 15787d62b00eSchristos bsearch_fde_cmp (const dwarf2_fde *fde, CORE_ADDR seek_pc) 15797d62b00eSchristos { 15807d62b00eSchristos if (fde->initial_location + fde->address_range <= seek_pc) 15817d62b00eSchristos return -1; 15827d62b00eSchristos if (fde->initial_location <= seek_pc) 15837d62b00eSchristos return 0; 15847d62b00eSchristos return 1; 15857d62b00eSchristos } 15867d62b00eSchristos 15877d62b00eSchristos /* Find an existing comp_unit for an objfile, if any. */ 15887d62b00eSchristos 15897d62b00eSchristos static comp_unit * 15907d62b00eSchristos find_comp_unit (struct objfile *objfile) 15917d62b00eSchristos { 1592*6881a400Schristos bfd *abfd = objfile->obfd.get (); 15937d62b00eSchristos if (gdb_bfd_requires_relocations (abfd)) 15947d62b00eSchristos return dwarf2_frame_objfile_data.get (objfile); 1595*6881a400Schristos 1596*6881a400Schristos return dwarf2_frame_bfd_data.get (abfd); 15977d62b00eSchristos } 15987d62b00eSchristos 15997d62b00eSchristos /* Store the comp_unit on OBJFILE, or the corresponding BFD, as 16007d62b00eSchristos appropriate. */ 16017d62b00eSchristos 16027d62b00eSchristos static void 16037d62b00eSchristos set_comp_unit (struct objfile *objfile, struct comp_unit *unit) 16047d62b00eSchristos { 1605*6881a400Schristos bfd *abfd = objfile->obfd.get (); 16067d62b00eSchristos if (gdb_bfd_requires_relocations (abfd)) 16077d62b00eSchristos return dwarf2_frame_objfile_data.set (objfile, unit); 1608*6881a400Schristos 1609*6881a400Schristos return dwarf2_frame_bfd_data.set (abfd, unit); 16107d62b00eSchristos } 16117d62b00eSchristos 16127d62b00eSchristos /* Find the FDE for *PC. Return a pointer to the FDE, and store the 16137d62b00eSchristos initial location associated with it into *PC. */ 16147d62b00eSchristos 16157d62b00eSchristos static struct dwarf2_fde * 16167d62b00eSchristos dwarf2_frame_find_fde (CORE_ADDR *pc, dwarf2_per_objfile **out_per_objfile) 16177d62b00eSchristos { 16187d62b00eSchristos for (objfile *objfile : current_program_space->objfiles ()) 16197d62b00eSchristos { 16207d62b00eSchristos CORE_ADDR offset; 16217d62b00eSchristos CORE_ADDR seek_pc; 16227d62b00eSchristos 1623*6881a400Schristos if (objfile->obfd == nullptr) 1624*6881a400Schristos continue; 1625*6881a400Schristos 16267d62b00eSchristos comp_unit *unit = find_comp_unit (objfile); 16277d62b00eSchristos if (unit == NULL) 16287d62b00eSchristos { 16297d62b00eSchristos dwarf2_build_frame_info (objfile); 16307d62b00eSchristos unit = find_comp_unit (objfile); 16317d62b00eSchristos } 16327d62b00eSchristos gdb_assert (unit != NULL); 16337d62b00eSchristos 16347d62b00eSchristos dwarf2_fde_table *fde_table = &unit->fde_table; 16357d62b00eSchristos if (fde_table->empty ()) 16367d62b00eSchristos continue; 16377d62b00eSchristos 16387d62b00eSchristos gdb_assert (!objfile->section_offsets.empty ()); 16397d62b00eSchristos offset = objfile->text_section_offset (); 16407d62b00eSchristos 16417d62b00eSchristos gdb_assert (!fde_table->empty ()); 16427d62b00eSchristos if (*pc < offset + (*fde_table)[0]->initial_location) 16437d62b00eSchristos continue; 16447d62b00eSchristos 16457d62b00eSchristos seek_pc = *pc - offset; 16467d62b00eSchristos auto it = gdb::binary_search (fde_table->begin (), fde_table->end (), 16477d62b00eSchristos seek_pc, bsearch_fde_cmp); 16487d62b00eSchristos if (it != fde_table->end ()) 16497d62b00eSchristos { 16507d62b00eSchristos *pc = (*it)->initial_location + offset; 16517d62b00eSchristos if (out_per_objfile != nullptr) 16527d62b00eSchristos *out_per_objfile = get_dwarf2_per_objfile (objfile); 16537d62b00eSchristos 16547d62b00eSchristos return *it; 16557d62b00eSchristos } 16567d62b00eSchristos } 16577d62b00eSchristos return NULL; 16587d62b00eSchristos } 16597d62b00eSchristos 16607d62b00eSchristos /* Add FDE to FDE_TABLE. */ 16617d62b00eSchristos static void 16627d62b00eSchristos add_fde (dwarf2_fde_table *fde_table, struct dwarf2_fde *fde) 16637d62b00eSchristos { 16647d62b00eSchristos if (fde->address_range == 0) 16657d62b00eSchristos /* Discard useless FDEs. */ 16667d62b00eSchristos return; 16677d62b00eSchristos 16687d62b00eSchristos fde_table->push_back (fde); 16697d62b00eSchristos } 16707d62b00eSchristos 16717d62b00eSchristos #define DW64_CIE_ID 0xffffffffffffffffULL 16727d62b00eSchristos 16737d62b00eSchristos /* Defines the type of eh_frames that are expected to be decoded: CIE, FDE 16747d62b00eSchristos or any of them. */ 16757d62b00eSchristos 16767d62b00eSchristos enum eh_frame_type 16777d62b00eSchristos { 16787d62b00eSchristos EH_CIE_TYPE_ID = 1 << 0, 16797d62b00eSchristos EH_FDE_TYPE_ID = 1 << 1, 16807d62b00eSchristos EH_CIE_OR_FDE_TYPE_ID = EH_CIE_TYPE_ID | EH_FDE_TYPE_ID 16817d62b00eSchristos }; 16827d62b00eSchristos 16837d62b00eSchristos static const gdb_byte *decode_frame_entry (struct gdbarch *gdbarch, 16847d62b00eSchristos struct comp_unit *unit, 16857d62b00eSchristos const gdb_byte *start, 16867d62b00eSchristos int eh_frame_p, 16877d62b00eSchristos dwarf2_cie_table &cie_table, 16887d62b00eSchristos dwarf2_fde_table *fde_table, 16897d62b00eSchristos enum eh_frame_type entry_type); 16907d62b00eSchristos 16917d62b00eSchristos /* Decode the next CIE or FDE, entry_type specifies the expected type. 16927d62b00eSchristos Return NULL if invalid input, otherwise the next byte to be processed. */ 16937d62b00eSchristos 16947d62b00eSchristos static const gdb_byte * 16957d62b00eSchristos decode_frame_entry_1 (struct gdbarch *gdbarch, 16967d62b00eSchristos struct comp_unit *unit, const gdb_byte *start, 16977d62b00eSchristos int eh_frame_p, 16987d62b00eSchristos dwarf2_cie_table &cie_table, 16997d62b00eSchristos dwarf2_fde_table *fde_table, 17007d62b00eSchristos enum eh_frame_type entry_type) 17017d62b00eSchristos { 17027d62b00eSchristos const gdb_byte *buf, *end; 17037d62b00eSchristos ULONGEST length; 17047d62b00eSchristos unsigned int bytes_read; 17057d62b00eSchristos int dwarf64_p; 17067d62b00eSchristos ULONGEST cie_id; 17077d62b00eSchristos ULONGEST cie_pointer; 17087d62b00eSchristos int64_t sleb128; 17097d62b00eSchristos uint64_t uleb128; 17107d62b00eSchristos 17117d62b00eSchristos buf = start; 17127d62b00eSchristos length = read_initial_length (unit->abfd, buf, &bytes_read, false); 17137d62b00eSchristos buf += bytes_read; 17147d62b00eSchristos end = buf + (size_t) length; 17157d62b00eSchristos 17167d62b00eSchristos if (length == 0) 17177d62b00eSchristos return end; 17187d62b00eSchristos 17197d62b00eSchristos /* Are we still within the section? */ 17207d62b00eSchristos if (end <= buf || end > unit->dwarf_frame_buffer + unit->dwarf_frame_size) 17217d62b00eSchristos return NULL; 17227d62b00eSchristos 17237d62b00eSchristos /* Distinguish between 32 and 64-bit encoded frame info. */ 17247d62b00eSchristos dwarf64_p = (bytes_read == 12); 17257d62b00eSchristos 17267d62b00eSchristos /* In a .eh_frame section, zero is used to distinguish CIEs from FDEs. */ 17277d62b00eSchristos if (eh_frame_p) 17287d62b00eSchristos cie_id = 0; 17297d62b00eSchristos else if (dwarf64_p) 17307d62b00eSchristos cie_id = DW64_CIE_ID; 17317d62b00eSchristos else 17327d62b00eSchristos cie_id = DW_CIE_ID; 17337d62b00eSchristos 17347d62b00eSchristos if (dwarf64_p) 17357d62b00eSchristos { 17367d62b00eSchristos cie_pointer = read_8_bytes (unit->abfd, buf); 17377d62b00eSchristos buf += 8; 17387d62b00eSchristos } 17397d62b00eSchristos else 17407d62b00eSchristos { 17417d62b00eSchristos cie_pointer = read_4_bytes (unit->abfd, buf); 17427d62b00eSchristos buf += 4; 17437d62b00eSchristos } 17447d62b00eSchristos 17457d62b00eSchristos if (cie_pointer == cie_id) 17467d62b00eSchristos { 17477d62b00eSchristos /* This is a CIE. */ 17487d62b00eSchristos struct dwarf2_cie *cie; 17497d62b00eSchristos char *augmentation; 17507d62b00eSchristos unsigned int cie_version; 17517d62b00eSchristos 17527d62b00eSchristos /* Check that a CIE was expected. */ 17537d62b00eSchristos if ((entry_type & EH_CIE_TYPE_ID) == 0) 17547d62b00eSchristos error (_("Found a CIE when not expecting it.")); 17557d62b00eSchristos 17567d62b00eSchristos /* Record the offset into the .debug_frame section of this CIE. */ 17577d62b00eSchristos cie_pointer = start - unit->dwarf_frame_buffer; 17587d62b00eSchristos 17597d62b00eSchristos /* Check whether we've already read it. */ 17607d62b00eSchristos if (find_cie (cie_table, cie_pointer)) 17617d62b00eSchristos return end; 17627d62b00eSchristos 17637d62b00eSchristos cie = XOBNEW (&unit->obstack, struct dwarf2_cie); 17647d62b00eSchristos cie->initial_instructions = NULL; 17657d62b00eSchristos cie->cie_pointer = cie_pointer; 17667d62b00eSchristos 17677d62b00eSchristos /* The encoding for FDE's in a normal .debug_frame section 17687d62b00eSchristos depends on the target address size. */ 17697d62b00eSchristos cie->encoding = DW_EH_PE_absptr; 17707d62b00eSchristos 17717d62b00eSchristos /* We'll determine the final value later, but we need to 17727d62b00eSchristos initialize it conservatively. */ 17737d62b00eSchristos cie->signal_frame = 0; 17747d62b00eSchristos 17757d62b00eSchristos /* Check version number. */ 17767d62b00eSchristos cie_version = read_1_byte (unit->abfd, buf); 17777d62b00eSchristos if (cie_version != 1 && cie_version != 3 && cie_version != 4) 17787d62b00eSchristos return NULL; 17797d62b00eSchristos cie->version = cie_version; 17807d62b00eSchristos buf += 1; 17817d62b00eSchristos 17827d62b00eSchristos /* Interpret the interesting bits of the augmentation. */ 17837d62b00eSchristos cie->augmentation = augmentation = (char *) buf; 17847d62b00eSchristos buf += (strlen (augmentation) + 1); 17857d62b00eSchristos 17867d62b00eSchristos /* Ignore armcc augmentations. We only use them for quirks, 17877d62b00eSchristos and that doesn't happen until later. */ 17887d62b00eSchristos if (startswith (augmentation, "armcc")) 17897d62b00eSchristos augmentation += strlen (augmentation); 17907d62b00eSchristos 17917d62b00eSchristos /* The GCC 2.x "eh" augmentation has a pointer immediately 17927d62b00eSchristos following the augmentation string, so it must be handled 17937d62b00eSchristos first. */ 17947d62b00eSchristos if (augmentation[0] == 'e' && augmentation[1] == 'h') 17957d62b00eSchristos { 17967d62b00eSchristos /* Skip. */ 17977d62b00eSchristos buf += gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT; 17987d62b00eSchristos augmentation += 2; 17997d62b00eSchristos } 18007d62b00eSchristos 18017d62b00eSchristos if (cie->version >= 4) 18027d62b00eSchristos { 18037d62b00eSchristos /* FIXME: check that this is the same as from the CU header. */ 18047d62b00eSchristos cie->addr_size = read_1_byte (unit->abfd, buf); 18057d62b00eSchristos ++buf; 18067d62b00eSchristos cie->segment_size = read_1_byte (unit->abfd, buf); 18077d62b00eSchristos ++buf; 18087d62b00eSchristos } 18097d62b00eSchristos else 18107d62b00eSchristos { 18117d62b00eSchristos cie->addr_size = gdbarch_dwarf2_addr_size (gdbarch); 18127d62b00eSchristos cie->segment_size = 0; 18137d62b00eSchristos } 18147d62b00eSchristos /* Address values in .eh_frame sections are defined to have the 18157d62b00eSchristos target's pointer size. Watchout: This breaks frame info for 18167d62b00eSchristos targets with pointer size < address size, unless a .debug_frame 18177d62b00eSchristos section exists as well. */ 18187d62b00eSchristos if (eh_frame_p) 18197d62b00eSchristos cie->ptr_size = gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT; 18207d62b00eSchristos else 18217d62b00eSchristos cie->ptr_size = cie->addr_size; 18227d62b00eSchristos 18237d62b00eSchristos buf = gdb_read_uleb128 (buf, end, &uleb128); 18247d62b00eSchristos if (buf == NULL) 18257d62b00eSchristos return NULL; 18267d62b00eSchristos cie->code_alignment_factor = uleb128; 18277d62b00eSchristos 18287d62b00eSchristos buf = gdb_read_sleb128 (buf, end, &sleb128); 18297d62b00eSchristos if (buf == NULL) 18307d62b00eSchristos return NULL; 18317d62b00eSchristos cie->data_alignment_factor = sleb128; 18327d62b00eSchristos 18337d62b00eSchristos if (cie_version == 1) 18347d62b00eSchristos { 18357d62b00eSchristos cie->return_address_register = read_1_byte (unit->abfd, buf); 18367d62b00eSchristos ++buf; 18377d62b00eSchristos } 18387d62b00eSchristos else 18397d62b00eSchristos { 18407d62b00eSchristos buf = gdb_read_uleb128 (buf, end, &uleb128); 18417d62b00eSchristos if (buf == NULL) 18427d62b00eSchristos return NULL; 18437d62b00eSchristos cie->return_address_register = uleb128; 18447d62b00eSchristos } 18457d62b00eSchristos 18467d62b00eSchristos cie->return_address_register 18477d62b00eSchristos = dwarf2_frame_adjust_regnum (gdbarch, 18487d62b00eSchristos cie->return_address_register, 18497d62b00eSchristos eh_frame_p); 18507d62b00eSchristos 18517d62b00eSchristos cie->saw_z_augmentation = (*augmentation == 'z'); 18527d62b00eSchristos if (cie->saw_z_augmentation) 18537d62b00eSchristos { 18547d62b00eSchristos uint64_t uleb_length; 18557d62b00eSchristos 18567d62b00eSchristos buf = gdb_read_uleb128 (buf, end, &uleb_length); 18577d62b00eSchristos if (buf == NULL) 18587d62b00eSchristos return NULL; 18597d62b00eSchristos cie->initial_instructions = buf + uleb_length; 18607d62b00eSchristos augmentation++; 18617d62b00eSchristos } 18627d62b00eSchristos 18637d62b00eSchristos while (*augmentation) 18647d62b00eSchristos { 18657d62b00eSchristos /* "L" indicates a byte showing how the LSDA pointer is encoded. */ 18667d62b00eSchristos if (*augmentation == 'L') 18677d62b00eSchristos { 18687d62b00eSchristos /* Skip. */ 18697d62b00eSchristos buf++; 18707d62b00eSchristos augmentation++; 18717d62b00eSchristos } 18727d62b00eSchristos 18737d62b00eSchristos /* "R" indicates a byte indicating how FDE addresses are encoded. */ 18747d62b00eSchristos else if (*augmentation == 'R') 18757d62b00eSchristos { 18767d62b00eSchristos cie->encoding = *buf++; 18777d62b00eSchristos augmentation++; 18787d62b00eSchristos } 18797d62b00eSchristos 18807d62b00eSchristos /* "P" indicates a personality routine in the CIE augmentation. */ 18817d62b00eSchristos else if (*augmentation == 'P') 18827d62b00eSchristos { 18837d62b00eSchristos /* Skip. Avoid indirection since we throw away the result. */ 18847d62b00eSchristos gdb_byte encoding = (*buf++) & ~DW_EH_PE_indirect; 18857d62b00eSchristos read_encoded_value (unit, encoding, cie->ptr_size, 18867d62b00eSchristos buf, &bytes_read, 0); 18877d62b00eSchristos buf += bytes_read; 18887d62b00eSchristos augmentation++; 18897d62b00eSchristos } 18907d62b00eSchristos 18917d62b00eSchristos /* "S" indicates a signal frame, such that the return 18927d62b00eSchristos address must not be decremented to locate the call frame 18937d62b00eSchristos info for the previous frame; it might even be the first 18947d62b00eSchristos instruction of a function, so decrementing it would take 18957d62b00eSchristos us to a different function. */ 18967d62b00eSchristos else if (*augmentation == 'S') 18977d62b00eSchristos { 18987d62b00eSchristos cie->signal_frame = 1; 18997d62b00eSchristos augmentation++; 19007d62b00eSchristos } 19017d62b00eSchristos 19027d62b00eSchristos /* Otherwise we have an unknown augmentation. Assume that either 19037d62b00eSchristos there is no augmentation data, or we saw a 'z' prefix. */ 19047d62b00eSchristos else 19057d62b00eSchristos { 19067d62b00eSchristos if (cie->initial_instructions) 19077d62b00eSchristos buf = cie->initial_instructions; 19087d62b00eSchristos break; 19097d62b00eSchristos } 19107d62b00eSchristos } 19117d62b00eSchristos 19127d62b00eSchristos cie->initial_instructions = buf; 19137d62b00eSchristos cie->end = end; 19147d62b00eSchristos cie->unit = unit; 19157d62b00eSchristos 19167d62b00eSchristos cie_table[cie->cie_pointer] = cie; 19177d62b00eSchristos } 19187d62b00eSchristos else 19197d62b00eSchristos { 19207d62b00eSchristos /* This is a FDE. */ 19217d62b00eSchristos struct dwarf2_fde *fde; 19227d62b00eSchristos CORE_ADDR addr; 19237d62b00eSchristos 19247d62b00eSchristos /* Check that an FDE was expected. */ 19257d62b00eSchristos if ((entry_type & EH_FDE_TYPE_ID) == 0) 19267d62b00eSchristos error (_("Found an FDE when not expecting it.")); 19277d62b00eSchristos 19287d62b00eSchristos /* In an .eh_frame section, the CIE pointer is the delta between the 19297d62b00eSchristos address within the FDE where the CIE pointer is stored and the 19307d62b00eSchristos address of the CIE. Convert it to an offset into the .eh_frame 19317d62b00eSchristos section. */ 19327d62b00eSchristos if (eh_frame_p) 19337d62b00eSchristos { 19347d62b00eSchristos cie_pointer = buf - unit->dwarf_frame_buffer - cie_pointer; 19357d62b00eSchristos cie_pointer -= (dwarf64_p ? 8 : 4); 19367d62b00eSchristos } 19377d62b00eSchristos 19387d62b00eSchristos /* In either case, validate the result is still within the section. */ 19397d62b00eSchristos if (cie_pointer >= unit->dwarf_frame_size) 19407d62b00eSchristos return NULL; 19417d62b00eSchristos 19427d62b00eSchristos fde = XOBNEW (&unit->obstack, struct dwarf2_fde); 19437d62b00eSchristos fde->cie = find_cie (cie_table, cie_pointer); 19447d62b00eSchristos if (fde->cie == NULL) 19457d62b00eSchristos { 19467d62b00eSchristos decode_frame_entry (gdbarch, unit, 19477d62b00eSchristos unit->dwarf_frame_buffer + cie_pointer, 19487d62b00eSchristos eh_frame_p, cie_table, fde_table, 19497d62b00eSchristos EH_CIE_TYPE_ID); 19507d62b00eSchristos fde->cie = find_cie (cie_table, cie_pointer); 19517d62b00eSchristos } 19527d62b00eSchristos 19537d62b00eSchristos gdb_assert (fde->cie != NULL); 19547d62b00eSchristos 19557d62b00eSchristos addr = read_encoded_value (unit, fde->cie->encoding, fde->cie->ptr_size, 19567d62b00eSchristos buf, &bytes_read, 0); 19577d62b00eSchristos fde->initial_location = gdbarch_adjust_dwarf2_addr (gdbarch, addr); 19587d62b00eSchristos buf += bytes_read; 19597d62b00eSchristos 19607d62b00eSchristos fde->address_range = 19617d62b00eSchristos read_encoded_value (unit, fde->cie->encoding & 0x0f, 19627d62b00eSchristos fde->cie->ptr_size, buf, &bytes_read, 0); 19637d62b00eSchristos addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + fde->address_range); 19647d62b00eSchristos fde->address_range = addr - fde->initial_location; 19657d62b00eSchristos buf += bytes_read; 19667d62b00eSchristos 19677d62b00eSchristos /* A 'z' augmentation in the CIE implies the presence of an 19687d62b00eSchristos augmentation field in the FDE as well. The only thing known 19697d62b00eSchristos to be in here at present is the LSDA entry for EH. So we 19707d62b00eSchristos can skip the whole thing. */ 19717d62b00eSchristos if (fde->cie->saw_z_augmentation) 19727d62b00eSchristos { 19737d62b00eSchristos uint64_t uleb_length; 19747d62b00eSchristos 19757d62b00eSchristos buf = gdb_read_uleb128 (buf, end, &uleb_length); 19767d62b00eSchristos if (buf == NULL) 19777d62b00eSchristos return NULL; 19787d62b00eSchristos buf += uleb_length; 19797d62b00eSchristos if (buf > end) 19807d62b00eSchristos return NULL; 19817d62b00eSchristos } 19827d62b00eSchristos 19837d62b00eSchristos fde->instructions = buf; 19847d62b00eSchristos fde->end = end; 19857d62b00eSchristos 19867d62b00eSchristos fde->eh_frame_p = eh_frame_p; 19877d62b00eSchristos 19887d62b00eSchristos add_fde (fde_table, fde); 19897d62b00eSchristos } 19907d62b00eSchristos 19917d62b00eSchristos return end; 19927d62b00eSchristos } 19937d62b00eSchristos 19947d62b00eSchristos /* Read a CIE or FDE in BUF and decode it. Entry_type specifies whether we 19957d62b00eSchristos expect an FDE or a CIE. */ 19967d62b00eSchristos 19977d62b00eSchristos static const gdb_byte * 19987d62b00eSchristos decode_frame_entry (struct gdbarch *gdbarch, 19997d62b00eSchristos struct comp_unit *unit, const gdb_byte *start, 20007d62b00eSchristos int eh_frame_p, 20017d62b00eSchristos dwarf2_cie_table &cie_table, 20027d62b00eSchristos dwarf2_fde_table *fde_table, 20037d62b00eSchristos enum eh_frame_type entry_type) 20047d62b00eSchristos { 20057d62b00eSchristos enum { NONE, ALIGN4, ALIGN8, FAIL } workaround = NONE; 20067d62b00eSchristos const gdb_byte *ret; 20077d62b00eSchristos ptrdiff_t start_offset; 20087d62b00eSchristos 20097d62b00eSchristos while (1) 20107d62b00eSchristos { 20117d62b00eSchristos ret = decode_frame_entry_1 (gdbarch, unit, start, eh_frame_p, 20127d62b00eSchristos cie_table, fde_table, entry_type); 20137d62b00eSchristos if (ret != NULL) 20147d62b00eSchristos break; 20157d62b00eSchristos 20167d62b00eSchristos /* We have corrupt input data of some form. */ 20177d62b00eSchristos 20187d62b00eSchristos /* ??? Try, weakly, to work around compiler/assembler/linker bugs 20197d62b00eSchristos and mismatches wrt padding and alignment of debug sections. */ 20207d62b00eSchristos /* Note that there is no requirement in the standard for any 20217d62b00eSchristos alignment at all in the frame unwind sections. Testing for 20227d62b00eSchristos alignment before trying to interpret data would be incorrect. 20237d62b00eSchristos 20247d62b00eSchristos However, GCC traditionally arranged for frame sections to be 20257d62b00eSchristos sized such that the FDE length and CIE fields happen to be 20267d62b00eSchristos aligned (in theory, for performance). This, unfortunately, 20277d62b00eSchristos was done with .align directives, which had the side effect of 20287d62b00eSchristos forcing the section to be aligned by the linker. 20297d62b00eSchristos 20307d62b00eSchristos This becomes a problem when you have some other producer that 20317d62b00eSchristos creates frame sections that are not as strictly aligned. That 20327d62b00eSchristos produces a hole in the frame info that gets filled by the 20337d62b00eSchristos linker with zeros. 20347d62b00eSchristos 20357d62b00eSchristos The GCC behaviour is arguably a bug, but it's effectively now 20367d62b00eSchristos part of the ABI, so we're now stuck with it, at least at the 20377d62b00eSchristos object file level. A smart linker may decide, in the process 20387d62b00eSchristos of compressing duplicate CIE information, that it can rewrite 20397d62b00eSchristos the entire output section without this extra padding. */ 20407d62b00eSchristos 20417d62b00eSchristos start_offset = start - unit->dwarf_frame_buffer; 20427d62b00eSchristos if (workaround < ALIGN4 && (start_offset & 3) != 0) 20437d62b00eSchristos { 20447d62b00eSchristos start += 4 - (start_offset & 3); 20457d62b00eSchristos workaround = ALIGN4; 20467d62b00eSchristos continue; 20477d62b00eSchristos } 20487d62b00eSchristos if (workaround < ALIGN8 && (start_offset & 7) != 0) 20497d62b00eSchristos { 20507d62b00eSchristos start += 8 - (start_offset & 7); 20517d62b00eSchristos workaround = ALIGN8; 20527d62b00eSchristos continue; 20537d62b00eSchristos } 20547d62b00eSchristos 20557d62b00eSchristos /* Nothing left to try. Arrange to return as if we've consumed 20567d62b00eSchristos the entire input section. Hopefully we'll get valid info from 20577d62b00eSchristos the other of .debug_frame/.eh_frame. */ 20587d62b00eSchristos workaround = FAIL; 20597d62b00eSchristos ret = unit->dwarf_frame_buffer + unit->dwarf_frame_size; 20607d62b00eSchristos break; 20617d62b00eSchristos } 20627d62b00eSchristos 20637d62b00eSchristos switch (workaround) 20647d62b00eSchristos { 20657d62b00eSchristos case NONE: 20667d62b00eSchristos break; 20677d62b00eSchristos 20687d62b00eSchristos case ALIGN4: 20697d62b00eSchristos complaint (_("\ 20707d62b00eSchristos Corrupt data in %s:%s; align 4 workaround apparently succeeded"), 20717d62b00eSchristos bfd_get_filename (unit->dwarf_frame_section->owner), 20727d62b00eSchristos bfd_section_name (unit->dwarf_frame_section)); 20737d62b00eSchristos break; 20747d62b00eSchristos 20757d62b00eSchristos case ALIGN8: 20767d62b00eSchristos complaint (_("\ 20777d62b00eSchristos Corrupt data in %s:%s; align 8 workaround apparently succeeded"), 20787d62b00eSchristos bfd_get_filename (unit->dwarf_frame_section->owner), 20797d62b00eSchristos bfd_section_name (unit->dwarf_frame_section)); 20807d62b00eSchristos break; 20817d62b00eSchristos 20827d62b00eSchristos default: 20837d62b00eSchristos complaint (_("Corrupt data in %s:%s"), 20847d62b00eSchristos bfd_get_filename (unit->dwarf_frame_section->owner), 20857d62b00eSchristos bfd_section_name (unit->dwarf_frame_section)); 20867d62b00eSchristos break; 20877d62b00eSchristos } 20887d62b00eSchristos 20897d62b00eSchristos return ret; 20907d62b00eSchristos } 20917d62b00eSchristos 20927d62b00eSchristos static bool 20937d62b00eSchristos fde_is_less_than (const dwarf2_fde *aa, const dwarf2_fde *bb) 20947d62b00eSchristos { 20957d62b00eSchristos if (aa->initial_location == bb->initial_location) 20967d62b00eSchristos { 20977d62b00eSchristos if (aa->address_range != bb->address_range 20987d62b00eSchristos && aa->eh_frame_p == 0 && bb->eh_frame_p == 0) 20997d62b00eSchristos /* Linker bug, e.g. gold/10400. 21007d62b00eSchristos Work around it by keeping stable sort order. */ 21017d62b00eSchristos return aa < bb; 21027d62b00eSchristos else 21037d62b00eSchristos /* Put eh_frame entries after debug_frame ones. */ 21047d62b00eSchristos return aa->eh_frame_p < bb->eh_frame_p; 21057d62b00eSchristos } 21067d62b00eSchristos 21077d62b00eSchristos return aa->initial_location < bb->initial_location; 21087d62b00eSchristos } 21097d62b00eSchristos 21107d62b00eSchristos void 21117d62b00eSchristos dwarf2_build_frame_info (struct objfile *objfile) 21127d62b00eSchristos { 21137d62b00eSchristos const gdb_byte *frame_ptr; 21147d62b00eSchristos dwarf2_cie_table cie_table; 21157d62b00eSchristos dwarf2_fde_table fde_table; 21167d62b00eSchristos 21177d62b00eSchristos struct gdbarch *gdbarch = objfile->arch (); 21187d62b00eSchristos 21197d62b00eSchristos /* Build a minimal decoding of the DWARF2 compilation unit. */ 21207d62b00eSchristos std::unique_ptr<comp_unit> unit (new comp_unit (objfile)); 21217d62b00eSchristos 21227d62b00eSchristos if (objfile->separate_debug_objfile_backlink == NULL) 21237d62b00eSchristos { 21247d62b00eSchristos /* Do not read .eh_frame from separate file as they must be also 21257d62b00eSchristos present in the main file. */ 21267d62b00eSchristos dwarf2_get_section_info (objfile, DWARF2_EH_FRAME, 21277d62b00eSchristos &unit->dwarf_frame_section, 21287d62b00eSchristos &unit->dwarf_frame_buffer, 21297d62b00eSchristos &unit->dwarf_frame_size); 21307d62b00eSchristos if (unit->dwarf_frame_size) 21317d62b00eSchristos { 21327d62b00eSchristos asection *got, *txt; 21337d62b00eSchristos 21347d62b00eSchristos /* FIXME: kettenis/20030602: This is the DW_EH_PE_datarel base 21357d62b00eSchristos that is used for the i386/amd64 target, which currently is 21367d62b00eSchristos the only target in GCC that supports/uses the 21377d62b00eSchristos DW_EH_PE_datarel encoding. */ 21387d62b00eSchristos got = bfd_get_section_by_name (unit->abfd, ".got"); 21397d62b00eSchristos if (got) 21407d62b00eSchristos unit->dbase = got->vma; 21417d62b00eSchristos 21427d62b00eSchristos /* GCC emits the DW_EH_PE_textrel encoding type on sh and ia64 21437d62b00eSchristos so far. */ 21447d62b00eSchristos txt = bfd_get_section_by_name (unit->abfd, ".text"); 21457d62b00eSchristos if (txt) 21467d62b00eSchristos unit->tbase = txt->vma; 21477d62b00eSchristos 21487d62b00eSchristos try 21497d62b00eSchristos { 21507d62b00eSchristos frame_ptr = unit->dwarf_frame_buffer; 21517d62b00eSchristos while (frame_ptr < unit->dwarf_frame_buffer + unit->dwarf_frame_size) 21527d62b00eSchristos frame_ptr = decode_frame_entry (gdbarch, unit.get (), 21537d62b00eSchristos frame_ptr, 1, 21547d62b00eSchristos cie_table, &fde_table, 21557d62b00eSchristos EH_CIE_OR_FDE_TYPE_ID); 21567d62b00eSchristos } 21577d62b00eSchristos 21587d62b00eSchristos catch (const gdb_exception_error &e) 21597d62b00eSchristos { 21607d62b00eSchristos warning (_("skipping .eh_frame info of %s: %s"), 21617d62b00eSchristos objfile_name (objfile), e.what ()); 21627d62b00eSchristos 21637d62b00eSchristos fde_table.clear (); 21647d62b00eSchristos /* The cie_table is discarded below. */ 21657d62b00eSchristos } 21667d62b00eSchristos 21677d62b00eSchristos cie_table.clear (); 21687d62b00eSchristos } 21697d62b00eSchristos } 21707d62b00eSchristos 21717d62b00eSchristos dwarf2_get_section_info (objfile, DWARF2_DEBUG_FRAME, 21727d62b00eSchristos &unit->dwarf_frame_section, 21737d62b00eSchristos &unit->dwarf_frame_buffer, 21747d62b00eSchristos &unit->dwarf_frame_size); 21757d62b00eSchristos if (unit->dwarf_frame_size) 21767d62b00eSchristos { 21777d62b00eSchristos size_t num_old_fde_entries = fde_table.size (); 21787d62b00eSchristos 21797d62b00eSchristos try 21807d62b00eSchristos { 21817d62b00eSchristos frame_ptr = unit->dwarf_frame_buffer; 21827d62b00eSchristos while (frame_ptr < unit->dwarf_frame_buffer + unit->dwarf_frame_size) 21837d62b00eSchristos frame_ptr = decode_frame_entry (gdbarch, unit.get (), frame_ptr, 0, 21847d62b00eSchristos cie_table, &fde_table, 21857d62b00eSchristos EH_CIE_OR_FDE_TYPE_ID); 21867d62b00eSchristos } 21877d62b00eSchristos catch (const gdb_exception_error &e) 21887d62b00eSchristos { 21897d62b00eSchristos warning (_("skipping .debug_frame info of %s: %s"), 21907d62b00eSchristos objfile_name (objfile), e.what ()); 21917d62b00eSchristos 21927d62b00eSchristos fde_table.resize (num_old_fde_entries); 21937d62b00eSchristos } 21947d62b00eSchristos } 21957d62b00eSchristos 21967d62b00eSchristos struct dwarf2_fde *fde_prev = NULL; 21977d62b00eSchristos struct dwarf2_fde *first_non_zero_fde = NULL; 21987d62b00eSchristos 21997d62b00eSchristos /* Prepare FDE table for lookups. */ 22007d62b00eSchristos std::sort (fde_table.begin (), fde_table.end (), fde_is_less_than); 22017d62b00eSchristos 22027d62b00eSchristos /* Check for leftovers from --gc-sections. The GNU linker sets 22037d62b00eSchristos the relevant symbols to zero, but doesn't zero the FDE *end* 22047d62b00eSchristos ranges because there's no relocation there. It's (offset, 22057d62b00eSchristos length), not (start, end). On targets where address zero is 22067d62b00eSchristos just another valid address this can be a problem, since the 22077d62b00eSchristos FDEs appear to be non-empty in the output --- we could pick 22087d62b00eSchristos out the wrong FDE. To work around this, when overlaps are 22097d62b00eSchristos detected, we prefer FDEs that do not start at zero. 22107d62b00eSchristos 22117d62b00eSchristos Start by finding the first FDE with non-zero start. Below 22127d62b00eSchristos we'll discard all FDEs that start at zero and overlap this 22137d62b00eSchristos one. */ 22147d62b00eSchristos for (struct dwarf2_fde *fde : fde_table) 22157d62b00eSchristos { 22167d62b00eSchristos if (fde->initial_location != 0) 22177d62b00eSchristos { 22187d62b00eSchristos first_non_zero_fde = fde; 22197d62b00eSchristos break; 22207d62b00eSchristos } 22217d62b00eSchristos } 22227d62b00eSchristos 22237d62b00eSchristos /* Since we'll be doing bsearch, squeeze out identical (except 22247d62b00eSchristos for eh_frame_p) fde entries so bsearch result is predictable. 22257d62b00eSchristos Also discard leftovers from --gc-sections. */ 22267d62b00eSchristos for (struct dwarf2_fde *fde : fde_table) 22277d62b00eSchristos { 22287d62b00eSchristos if (fde->initial_location == 0 22297d62b00eSchristos && first_non_zero_fde != NULL 22307d62b00eSchristos && (first_non_zero_fde->initial_location 22317d62b00eSchristos < fde->initial_location + fde->address_range)) 22327d62b00eSchristos continue; 22337d62b00eSchristos 22347d62b00eSchristos if (fde_prev != NULL 22357d62b00eSchristos && fde_prev->initial_location == fde->initial_location) 22367d62b00eSchristos continue; 22377d62b00eSchristos 22387d62b00eSchristos unit->fde_table.push_back (fde); 22397d62b00eSchristos fde_prev = fde; 22407d62b00eSchristos } 22417d62b00eSchristos unit->fde_table.shrink_to_fit (); 22427d62b00eSchristos 22437d62b00eSchristos set_comp_unit (objfile, unit.release ()); 22447d62b00eSchristos } 22457d62b00eSchristos 22467d62b00eSchristos /* Handle 'maintenance show dwarf unwinders'. */ 22477d62b00eSchristos 22487d62b00eSchristos static void 22497d62b00eSchristos show_dwarf_unwinders_enabled_p (struct ui_file *file, int from_tty, 22507d62b00eSchristos struct cmd_list_element *c, 22517d62b00eSchristos const char *value) 22527d62b00eSchristos { 2253*6881a400Schristos gdb_printf (file, 22547d62b00eSchristos _("The DWARF stack unwinders are currently %s.\n"), 22557d62b00eSchristos value); 22567d62b00eSchristos } 22577d62b00eSchristos 22587d62b00eSchristos void _initialize_dwarf2_frame (); 22597d62b00eSchristos void 22607d62b00eSchristos _initialize_dwarf2_frame () 22617d62b00eSchristos { 22627d62b00eSchristos add_setshow_boolean_cmd ("unwinders", class_obscure, 22637d62b00eSchristos &dwarf2_frame_unwinders_enabled_p , _("\ 22647d62b00eSchristos Set whether the DWARF stack frame unwinders are used."), _("\ 22657d62b00eSchristos Show whether the DWARF stack frame unwinders are used."), _("\ 22667d62b00eSchristos When enabled the DWARF stack frame unwinders can be used for architectures\n\ 22677d62b00eSchristos that support the DWARF unwinders. Enabling the DWARF unwinders for an\n\ 22687d62b00eSchristos architecture that doesn't support them will have no effect."), 22697d62b00eSchristos NULL, 22707d62b00eSchristos show_dwarf_unwinders_enabled_p, 22717d62b00eSchristos &set_dwarf_cmdlist, 22727d62b00eSchristos &show_dwarf_cmdlist); 22737d62b00eSchristos 22747d62b00eSchristos #if GDB_SELF_TEST 22757d62b00eSchristos selftests::register_test_foreach_arch ("execute_cfa_program", 22767d62b00eSchristos selftests::execute_cfa_program_test); 22777d62b00eSchristos #endif 22787d62b00eSchristos } 2279