1 /* Frame unwinder for frames with DWARF Call Frame Information. 2 3 Copyright (C) 2003-2023 Free Software Foundation, Inc. 4 5 Contributed by Mark Kettenis. 6 7 This file is part of GDB. 8 9 This program is free software; you can redistribute it and/or modify 10 it under the terms of the GNU General Public License as published by 11 the Free Software Foundation; either version 3 of the License, or 12 (at your option) any later version. 13 14 This program is distributed in the hope that it will be useful, 15 but WITHOUT ANY WARRANTY; without even the implied warranty of 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 GNU General Public License for more details. 18 19 You should have received a copy of the GNU General Public License 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 21 22 #include "defs.h" 23 #include "dwarf2/expr.h" 24 #include "dwarf2.h" 25 #include "dwarf2/leb.h" 26 #include "frame.h" 27 #include "frame-base.h" 28 #include "frame-unwind.h" 29 #include "gdbcore.h" 30 #include "gdbtypes.h" 31 #include "symtab.h" 32 #include "objfiles.h" 33 #include "regcache.h" 34 #include "value.h" 35 #include "record.h" 36 37 #include "complaints.h" 38 #include "dwarf2/frame.h" 39 #include "dwarf2/read.h" 40 #include "dwarf2/public.h" 41 #include "ax.h" 42 #include "dwarf2/loc.h" 43 #include "dwarf2/frame-tailcall.h" 44 #include "gdbsupport/gdb_binary_search.h" 45 #if GDB_SELF_TEST 46 #include "gdbsupport/selftest.h" 47 #include "selftest-arch.h" 48 #endif 49 #include <unordered_map> 50 51 #include <algorithm> 52 53 struct comp_unit; 54 55 /* Call Frame Information (CFI). */ 56 57 /* Common Information Entry (CIE). */ 58 59 struct dwarf2_cie 60 { 61 /* Computation Unit for this CIE. */ 62 struct comp_unit *unit; 63 64 /* Offset into the .debug_frame section where this CIE was found. 65 Used to identify this CIE. */ 66 ULONGEST cie_pointer; 67 68 /* Constant that is factored out of all advance location 69 instructions. */ 70 ULONGEST code_alignment_factor; 71 72 /* Constants that is factored out of all offset instructions. */ 73 LONGEST data_alignment_factor; 74 75 /* Return address column. */ 76 ULONGEST return_address_register; 77 78 /* Instruction sequence to initialize a register set. */ 79 const gdb_byte *initial_instructions; 80 const gdb_byte *end; 81 82 /* Saved augmentation, in case it's needed later. */ 83 char *augmentation; 84 85 /* Encoding of addresses. */ 86 gdb_byte encoding; 87 88 /* Target address size in bytes. */ 89 int addr_size; 90 91 /* Target pointer size in bytes. */ 92 int ptr_size; 93 94 /* True if a 'z' augmentation existed. */ 95 unsigned char saw_z_augmentation; 96 97 /* True if an 'S' augmentation existed. */ 98 unsigned char signal_frame; 99 100 /* The version recorded in the CIE. */ 101 unsigned char version; 102 103 /* The segment size. */ 104 unsigned char segment_size; 105 }; 106 107 /* The CIE table is used to find CIEs during parsing, but then 108 discarded. It maps from the CIE's offset to the CIE. */ 109 typedef std::unordered_map<ULONGEST, dwarf2_cie *> dwarf2_cie_table; 110 111 /* Frame Description Entry (FDE). */ 112 113 struct dwarf2_fde 114 { 115 /* CIE for this FDE. */ 116 struct dwarf2_cie *cie; 117 118 /* First location associated with this FDE. */ 119 CORE_ADDR initial_location; 120 121 /* Number of bytes of program instructions described by this FDE. */ 122 CORE_ADDR address_range; 123 124 /* Instruction sequence. */ 125 const gdb_byte *instructions; 126 const gdb_byte *end; 127 128 /* True if this FDE is read from a .eh_frame instead of a .debug_frame 129 section. */ 130 unsigned char eh_frame_p; 131 }; 132 133 typedef std::vector<dwarf2_fde *> dwarf2_fde_table; 134 135 /* A minimal decoding of DWARF2 compilation units. We only decode 136 what's needed to get to the call frame information. */ 137 138 struct comp_unit 139 { 140 comp_unit (struct objfile *objf) 141 : abfd (objf->obfd.get ()) 142 { 143 } 144 145 /* Keep the bfd convenient. */ 146 bfd *abfd; 147 148 /* Pointer to the .debug_frame section loaded into memory. */ 149 const gdb_byte *dwarf_frame_buffer = nullptr; 150 151 /* Length of the loaded .debug_frame section. */ 152 bfd_size_type dwarf_frame_size = 0; 153 154 /* Pointer to the .debug_frame section. */ 155 asection *dwarf_frame_section = nullptr; 156 157 /* Base for DW_EH_PE_datarel encodings. */ 158 bfd_vma dbase = 0; 159 160 /* Base for DW_EH_PE_textrel encodings. */ 161 bfd_vma tbase = 0; 162 163 /* The FDE table. */ 164 dwarf2_fde_table fde_table; 165 166 /* Hold data used by this module. */ 167 auto_obstack obstack; 168 }; 169 170 static struct dwarf2_fde *dwarf2_frame_find_fde 171 (CORE_ADDR *pc, dwarf2_per_objfile **out_per_objfile); 172 173 static int dwarf2_frame_adjust_regnum (struct gdbarch *gdbarch, int regnum, 174 int eh_frame_p); 175 176 static CORE_ADDR read_encoded_value (struct comp_unit *unit, gdb_byte encoding, 177 int ptr_len, const gdb_byte *buf, 178 unsigned int *bytes_read_ptr, 179 CORE_ADDR func_base); 180 181 182 /* See dwarf2/frame.h. */ 183 bool dwarf2_frame_unwinders_enabled_p = true; 184 185 /* Store the length the expression for the CFA in the `cfa_reg' field, 186 which is unused in that case. */ 187 #define cfa_exp_len cfa_reg 188 189 dwarf2_frame_state::dwarf2_frame_state (CORE_ADDR pc_, struct dwarf2_cie *cie) 190 : pc (pc_), data_align (cie->data_alignment_factor), 191 code_align (cie->code_alignment_factor), 192 retaddr_column (cie->return_address_register) 193 { 194 } 195 196 /* Execute the required actions for both the DW_CFA_restore and 197 DW_CFA_restore_extended instructions. */ 198 static void 199 dwarf2_restore_rule (struct gdbarch *gdbarch, ULONGEST reg_num, 200 struct dwarf2_frame_state *fs, int eh_frame_p) 201 { 202 ULONGEST reg; 203 204 reg = dwarf2_frame_adjust_regnum (gdbarch, reg_num, eh_frame_p); 205 fs->regs.alloc_regs (reg + 1); 206 207 /* Check if this register was explicitly initialized in the 208 CIE initial instructions. If not, default the rule to 209 UNSPECIFIED. */ 210 if (reg < fs->initial.reg.size ()) 211 fs->regs.reg[reg] = fs->initial.reg[reg]; 212 else 213 fs->regs.reg[reg].how = DWARF2_FRAME_REG_UNSPECIFIED; 214 215 if (fs->regs.reg[reg].how == DWARF2_FRAME_REG_UNSPECIFIED) 216 { 217 int regnum = dwarf_reg_to_regnum (gdbarch, reg); 218 219 complaint (_("\ 220 incomplete CFI data; DW_CFA_restore unspecified\n\ 221 register %s (#%d) at %s"), 222 gdbarch_register_name (gdbarch, regnum), regnum, 223 paddress (gdbarch, fs->pc)); 224 } 225 } 226 227 static CORE_ADDR 228 execute_stack_op (const gdb_byte *exp, ULONGEST len, int addr_size, 229 frame_info_ptr this_frame, CORE_ADDR initial, 230 int initial_in_stack_memory, dwarf2_per_objfile *per_objfile) 231 { 232 dwarf_expr_context ctx (per_objfile, addr_size); 233 scoped_value_mark free_values; 234 235 ctx.push_address (initial, initial_in_stack_memory); 236 value *result_val = ctx.evaluate (exp, len, true, nullptr, this_frame); 237 238 if (VALUE_LVAL (result_val) == lval_memory) 239 return value_address (result_val); 240 else 241 return value_as_address (result_val); 242 } 243 244 245 /* Execute FDE program from INSN_PTR possibly up to INSN_END or up to inferior 246 PC. Modify FS state accordingly. Return current INSN_PTR where the 247 execution has stopped, one can resume it on the next call. */ 248 249 static const gdb_byte * 250 execute_cfa_program (struct dwarf2_fde *fde, const gdb_byte *insn_ptr, 251 const gdb_byte *insn_end, struct gdbarch *gdbarch, 252 CORE_ADDR pc, struct dwarf2_frame_state *fs, 253 CORE_ADDR text_offset) 254 { 255 int eh_frame_p = fde->eh_frame_p; 256 unsigned int bytes_read; 257 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); 258 259 while (insn_ptr < insn_end && fs->pc <= pc) 260 { 261 gdb_byte insn = *insn_ptr++; 262 uint64_t utmp, reg; 263 int64_t offset; 264 265 if ((insn & 0xc0) == DW_CFA_advance_loc) 266 fs->pc += (insn & 0x3f) * fs->code_align; 267 else if ((insn & 0xc0) == DW_CFA_offset) 268 { 269 reg = insn & 0x3f; 270 reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p); 271 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp); 272 offset = utmp * fs->data_align; 273 fs->regs.alloc_regs (reg + 1); 274 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET; 275 fs->regs.reg[reg].loc.offset = offset; 276 } 277 else if ((insn & 0xc0) == DW_CFA_restore) 278 { 279 reg = insn & 0x3f; 280 dwarf2_restore_rule (gdbarch, reg, fs, eh_frame_p); 281 } 282 else 283 { 284 switch (insn) 285 { 286 case DW_CFA_set_loc: 287 fs->pc = read_encoded_value (fde->cie->unit, fde->cie->encoding, 288 fde->cie->ptr_size, insn_ptr, 289 &bytes_read, fde->initial_location); 290 /* Apply the text offset for relocatable objects. */ 291 fs->pc += text_offset; 292 insn_ptr += bytes_read; 293 break; 294 295 case DW_CFA_advance_loc1: 296 utmp = extract_unsigned_integer (insn_ptr, 1, byte_order); 297 fs->pc += utmp * fs->code_align; 298 insn_ptr++; 299 break; 300 case DW_CFA_advance_loc2: 301 utmp = extract_unsigned_integer (insn_ptr, 2, byte_order); 302 fs->pc += utmp * fs->code_align; 303 insn_ptr += 2; 304 break; 305 case DW_CFA_advance_loc4: 306 utmp = extract_unsigned_integer (insn_ptr, 4, byte_order); 307 fs->pc += utmp * fs->code_align; 308 insn_ptr += 4; 309 break; 310 311 case DW_CFA_offset_extended: 312 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®); 313 reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p); 314 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp); 315 offset = utmp * fs->data_align; 316 fs->regs.alloc_regs (reg + 1); 317 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET; 318 fs->regs.reg[reg].loc.offset = offset; 319 break; 320 321 case DW_CFA_restore_extended: 322 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®); 323 dwarf2_restore_rule (gdbarch, reg, fs, eh_frame_p); 324 break; 325 326 case DW_CFA_undefined: 327 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®); 328 reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p); 329 fs->regs.alloc_regs (reg + 1); 330 fs->regs.reg[reg].how = DWARF2_FRAME_REG_UNDEFINED; 331 break; 332 333 case DW_CFA_same_value: 334 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®); 335 reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p); 336 fs->regs.alloc_regs (reg + 1); 337 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAME_VALUE; 338 break; 339 340 case DW_CFA_register: 341 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®); 342 reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p); 343 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp); 344 utmp = dwarf2_frame_adjust_regnum (gdbarch, utmp, eh_frame_p); 345 fs->regs.alloc_regs (reg + 1); 346 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_REG; 347 fs->regs.reg[reg].loc.reg = utmp; 348 break; 349 350 case DW_CFA_remember_state: 351 { 352 struct dwarf2_frame_state_reg_info *new_rs; 353 354 new_rs = new dwarf2_frame_state_reg_info (fs->regs); 355 fs->regs.prev = new_rs; 356 } 357 break; 358 359 case DW_CFA_restore_state: 360 { 361 struct dwarf2_frame_state_reg_info *old_rs = fs->regs.prev; 362 363 if (old_rs == NULL) 364 { 365 complaint (_("\ 366 bad CFI data; mismatched DW_CFA_restore_state at %s"), 367 paddress (gdbarch, fs->pc)); 368 } 369 else 370 fs->regs = std::move (*old_rs); 371 } 372 break; 373 374 case DW_CFA_def_cfa: 375 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®); 376 fs->regs.cfa_reg = reg; 377 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp); 378 379 if (fs->armcc_cfa_offsets_sf) 380 utmp *= fs->data_align; 381 382 fs->regs.cfa_offset = utmp; 383 fs->regs.cfa_how = CFA_REG_OFFSET; 384 break; 385 386 case DW_CFA_def_cfa_register: 387 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®); 388 fs->regs.cfa_reg = dwarf2_frame_adjust_regnum (gdbarch, reg, 389 eh_frame_p); 390 fs->regs.cfa_how = CFA_REG_OFFSET; 391 break; 392 393 case DW_CFA_def_cfa_offset: 394 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp); 395 396 if (fs->armcc_cfa_offsets_sf) 397 utmp *= fs->data_align; 398 399 fs->regs.cfa_offset = utmp; 400 /* cfa_how deliberately not set. */ 401 break; 402 403 case DW_CFA_nop: 404 break; 405 406 case DW_CFA_def_cfa_expression: 407 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp); 408 fs->regs.cfa_exp_len = utmp; 409 fs->regs.cfa_exp = insn_ptr; 410 fs->regs.cfa_how = CFA_EXP; 411 insn_ptr += fs->regs.cfa_exp_len; 412 break; 413 414 case DW_CFA_expression: 415 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®); 416 reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p); 417 fs->regs.alloc_regs (reg + 1); 418 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp); 419 fs->regs.reg[reg].loc.exp.start = insn_ptr; 420 fs->regs.reg[reg].loc.exp.len = utmp; 421 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_EXP; 422 insn_ptr += utmp; 423 break; 424 425 case DW_CFA_offset_extended_sf: 426 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®); 427 reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p); 428 insn_ptr = safe_read_sleb128 (insn_ptr, insn_end, &offset); 429 offset *= fs->data_align; 430 fs->regs.alloc_regs (reg + 1); 431 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET; 432 fs->regs.reg[reg].loc.offset = offset; 433 break; 434 435 case DW_CFA_val_offset: 436 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®); 437 fs->regs.alloc_regs (reg + 1); 438 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp); 439 offset = utmp * fs->data_align; 440 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_VAL_OFFSET; 441 fs->regs.reg[reg].loc.offset = offset; 442 break; 443 444 case DW_CFA_val_offset_sf: 445 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®); 446 fs->regs.alloc_regs (reg + 1); 447 insn_ptr = safe_read_sleb128 (insn_ptr, insn_end, &offset); 448 offset *= fs->data_align; 449 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_VAL_OFFSET; 450 fs->regs.reg[reg].loc.offset = offset; 451 break; 452 453 case DW_CFA_val_expression: 454 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®); 455 fs->regs.alloc_regs (reg + 1); 456 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp); 457 fs->regs.reg[reg].loc.exp.start = insn_ptr; 458 fs->regs.reg[reg].loc.exp.len = utmp; 459 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_VAL_EXP; 460 insn_ptr += utmp; 461 break; 462 463 case DW_CFA_def_cfa_sf: 464 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®); 465 fs->regs.cfa_reg = dwarf2_frame_adjust_regnum (gdbarch, reg, 466 eh_frame_p); 467 insn_ptr = safe_read_sleb128 (insn_ptr, insn_end, &offset); 468 fs->regs.cfa_offset = offset * fs->data_align; 469 fs->regs.cfa_how = CFA_REG_OFFSET; 470 break; 471 472 case DW_CFA_def_cfa_offset_sf: 473 insn_ptr = safe_read_sleb128 (insn_ptr, insn_end, &offset); 474 fs->regs.cfa_offset = offset * fs->data_align; 475 /* cfa_how deliberately not set. */ 476 break; 477 478 case DW_CFA_GNU_args_size: 479 /* Ignored. */ 480 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp); 481 break; 482 483 case DW_CFA_GNU_negative_offset_extended: 484 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®); 485 reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p); 486 insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp); 487 offset = utmp * fs->data_align; 488 fs->regs.alloc_regs (reg + 1); 489 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET; 490 fs->regs.reg[reg].loc.offset = -offset; 491 break; 492 493 default: 494 if (insn >= DW_CFA_lo_user && insn <= DW_CFA_hi_user) 495 { 496 /* Handle vendor-specific CFI for different architectures. */ 497 if (!gdbarch_execute_dwarf_cfa_vendor_op (gdbarch, insn, fs)) 498 error (_("Call Frame Instruction op %d in vendor extension " 499 "space is not handled on this architecture."), 500 insn); 501 } 502 else 503 internal_error (_("Unknown CFI encountered.")); 504 } 505 } 506 } 507 508 if (fs->initial.reg.empty ()) 509 { 510 /* Don't allow remember/restore between CIE and FDE programs. */ 511 delete fs->regs.prev; 512 fs->regs.prev = NULL; 513 } 514 515 return insn_ptr; 516 } 517 518 #if GDB_SELF_TEST 519 520 namespace selftests { 521 522 /* Unit test to function execute_cfa_program. */ 523 524 static void 525 execute_cfa_program_test (struct gdbarch *gdbarch) 526 { 527 struct dwarf2_fde fde; 528 struct dwarf2_cie cie; 529 530 memset (&fde, 0, sizeof fde); 531 memset (&cie, 0, sizeof cie); 532 533 cie.data_alignment_factor = -4; 534 cie.code_alignment_factor = 2; 535 fde.cie = &cie; 536 537 dwarf2_frame_state fs (0, fde.cie); 538 539 gdb_byte insns[] = 540 { 541 DW_CFA_def_cfa, 1, 4, /* DW_CFA_def_cfa: r1 ofs 4 */ 542 DW_CFA_offset | 0x2, 1, /* DW_CFA_offset: r2 at cfa-4 */ 543 DW_CFA_remember_state, 544 DW_CFA_restore_state, 545 }; 546 547 const gdb_byte *insn_end = insns + sizeof (insns); 548 const gdb_byte *out = execute_cfa_program (&fde, insns, insn_end, gdbarch, 549 0, &fs, 0); 550 551 SELF_CHECK (out == insn_end); 552 SELF_CHECK (fs.pc == 0); 553 554 /* The instructions above only use r1 and r2, but the register numbers 555 used are adjusted by dwarf2_frame_adjust_regnum. */ 556 auto r1 = dwarf2_frame_adjust_regnum (gdbarch, 1, fde.eh_frame_p); 557 auto r2 = dwarf2_frame_adjust_regnum (gdbarch, 2, fde.eh_frame_p); 558 559 SELF_CHECK (fs.regs.reg.size () == (std::max (r1, r2) + 1)); 560 561 SELF_CHECK (fs.regs.reg[r2].how == DWARF2_FRAME_REG_SAVED_OFFSET); 562 SELF_CHECK (fs.regs.reg[r2].loc.offset == -4); 563 564 for (auto i = 0; i < fs.regs.reg.size (); i++) 565 if (i != r2) 566 SELF_CHECK (fs.regs.reg[i].how == DWARF2_FRAME_REG_UNSPECIFIED); 567 568 SELF_CHECK (fs.regs.cfa_reg == 1); 569 SELF_CHECK (fs.regs.cfa_offset == 4); 570 SELF_CHECK (fs.regs.cfa_how == CFA_REG_OFFSET); 571 SELF_CHECK (fs.regs.cfa_exp == NULL); 572 SELF_CHECK (fs.regs.prev == NULL); 573 } 574 575 } // namespace selftests 576 #endif /* GDB_SELF_TEST */ 577 578 579 580 /* Architecture-specific operations. */ 581 582 static void dwarf2_frame_default_init_reg (struct gdbarch *gdbarch, 583 int regnum, 584 struct dwarf2_frame_state_reg *reg, 585 frame_info_ptr this_frame); 586 587 struct dwarf2_frame_ops 588 { 589 /* Pre-initialize the register state REG for register REGNUM. */ 590 void (*init_reg) (struct gdbarch *, int, struct dwarf2_frame_state_reg *, 591 frame_info_ptr) 592 = dwarf2_frame_default_init_reg; 593 594 /* Check whether the THIS_FRAME is a signal trampoline. */ 595 int (*signal_frame_p) (struct gdbarch *, frame_info_ptr) = nullptr; 596 597 /* Convert .eh_frame register number to DWARF register number, or 598 adjust .debug_frame register number. */ 599 int (*adjust_regnum) (struct gdbarch *, int, int) = nullptr; 600 }; 601 602 /* Per-architecture data key. */ 603 static const registry<gdbarch>::key<dwarf2_frame_ops> dwarf2_frame_data; 604 605 /* Get or initialize the frame ops. */ 606 static dwarf2_frame_ops * 607 get_frame_ops (struct gdbarch *gdbarch) 608 { 609 dwarf2_frame_ops *result = dwarf2_frame_data.get (gdbarch); 610 if (result == nullptr) 611 result = dwarf2_frame_data.emplace (gdbarch); 612 return result; 613 } 614 615 /* Default architecture-specific register state initialization 616 function. */ 617 618 static void 619 dwarf2_frame_default_init_reg (struct gdbarch *gdbarch, int regnum, 620 struct dwarf2_frame_state_reg *reg, 621 frame_info_ptr this_frame) 622 { 623 /* If we have a register that acts as a program counter, mark it as 624 a destination for the return address. If we have a register that 625 serves as the stack pointer, arrange for it to be filled with the 626 call frame address (CFA). The other registers are marked as 627 unspecified. 628 629 We copy the return address to the program counter, since many 630 parts in GDB assume that it is possible to get the return address 631 by unwinding the program counter register. However, on ISA's 632 with a dedicated return address register, the CFI usually only 633 contains information to unwind that return address register. 634 635 The reason we're treating the stack pointer special here is 636 because in many cases GCC doesn't emit CFI for the stack pointer 637 and implicitly assumes that it is equal to the CFA. This makes 638 some sense since the DWARF specification (version 3, draft 8, 639 p. 102) says that: 640 641 "Typically, the CFA is defined to be the value of the stack 642 pointer at the call site in the previous frame (which may be 643 different from its value on entry to the current frame)." 644 645 However, this isn't true for all platforms supported by GCC 646 (e.g. IBM S/390 and zSeries). Those architectures should provide 647 their own architecture-specific initialization function. */ 648 649 if (regnum == gdbarch_pc_regnum (gdbarch)) 650 reg->how = DWARF2_FRAME_REG_RA; 651 else if (regnum == gdbarch_sp_regnum (gdbarch)) 652 reg->how = DWARF2_FRAME_REG_CFA; 653 } 654 655 /* Set the architecture-specific register state initialization 656 function for GDBARCH to INIT_REG. */ 657 658 void 659 dwarf2_frame_set_init_reg (struct gdbarch *gdbarch, 660 void (*init_reg) (struct gdbarch *, int, 661 struct dwarf2_frame_state_reg *, 662 frame_info_ptr)) 663 { 664 struct dwarf2_frame_ops *ops = get_frame_ops (gdbarch); 665 666 ops->init_reg = init_reg; 667 } 668 669 /* Pre-initialize the register state REG for register REGNUM. */ 670 671 static void 672 dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum, 673 struct dwarf2_frame_state_reg *reg, 674 frame_info_ptr this_frame) 675 { 676 struct dwarf2_frame_ops *ops = get_frame_ops (gdbarch); 677 678 ops->init_reg (gdbarch, regnum, reg, this_frame); 679 } 680 681 /* Set the architecture-specific signal trampoline recognition 682 function for GDBARCH to SIGNAL_FRAME_P. */ 683 684 void 685 dwarf2_frame_set_signal_frame_p (struct gdbarch *gdbarch, 686 int (*signal_frame_p) (struct gdbarch *, 687 frame_info_ptr)) 688 { 689 struct dwarf2_frame_ops *ops = get_frame_ops (gdbarch); 690 691 ops->signal_frame_p = signal_frame_p; 692 } 693 694 /* Query the architecture-specific signal frame recognizer for 695 THIS_FRAME. */ 696 697 static int 698 dwarf2_frame_signal_frame_p (struct gdbarch *gdbarch, 699 frame_info_ptr this_frame) 700 { 701 struct dwarf2_frame_ops *ops = get_frame_ops (gdbarch); 702 703 if (ops->signal_frame_p == NULL) 704 return 0; 705 return ops->signal_frame_p (gdbarch, this_frame); 706 } 707 708 /* Set the architecture-specific adjustment of .eh_frame and .debug_frame 709 register numbers. */ 710 711 void 712 dwarf2_frame_set_adjust_regnum (struct gdbarch *gdbarch, 713 int (*adjust_regnum) (struct gdbarch *, 714 int, int)) 715 { 716 struct dwarf2_frame_ops *ops = get_frame_ops (gdbarch); 717 718 ops->adjust_regnum = adjust_regnum; 719 } 720 721 /* Translate a .eh_frame register to DWARF register, or adjust a .debug_frame 722 register. */ 723 724 static int 725 dwarf2_frame_adjust_regnum (struct gdbarch *gdbarch, 726 int regnum, int eh_frame_p) 727 { 728 struct dwarf2_frame_ops *ops = get_frame_ops (gdbarch); 729 730 if (ops->adjust_regnum == NULL) 731 return regnum; 732 return ops->adjust_regnum (gdbarch, regnum, eh_frame_p); 733 } 734 735 static void 736 dwarf2_frame_find_quirks (struct dwarf2_frame_state *fs, 737 struct dwarf2_fde *fde) 738 { 739 struct compunit_symtab *cust; 740 741 cust = find_pc_compunit_symtab (fs->pc); 742 if (cust == NULL) 743 return; 744 745 if (producer_is_realview (cust->producer ())) 746 { 747 if (fde->cie->version == 1) 748 fs->armcc_cfa_offsets_sf = 1; 749 750 if (fde->cie->version == 1) 751 fs->armcc_cfa_offsets_reversed = 1; 752 753 /* The reversed offset problem is present in some compilers 754 using DWARF3, but it was eventually fixed. Check the ARM 755 defined augmentations, which are in the format "armcc" followed 756 by a list of one-character options. The "+" option means 757 this problem is fixed (no quirk needed). If the armcc 758 augmentation is missing, the quirk is needed. */ 759 if (fde->cie->version == 3 760 && (!startswith (fde->cie->augmentation, "armcc") 761 || strchr (fde->cie->augmentation + 5, '+') == NULL)) 762 fs->armcc_cfa_offsets_reversed = 1; 763 764 return; 765 } 766 } 767 768 769 /* See dwarf2/frame.h. */ 770 771 int 772 dwarf2_fetch_cfa_info (struct gdbarch *gdbarch, CORE_ADDR pc, 773 struct dwarf2_per_cu_data *data, 774 int *regnum_out, LONGEST *offset_out, 775 CORE_ADDR *text_offset_out, 776 const gdb_byte **cfa_start_out, 777 const gdb_byte **cfa_end_out) 778 { 779 struct dwarf2_fde *fde; 780 dwarf2_per_objfile *per_objfile; 781 CORE_ADDR pc1 = pc; 782 783 /* Find the correct FDE. */ 784 fde = dwarf2_frame_find_fde (&pc1, &per_objfile); 785 if (fde == NULL) 786 error (_("Could not compute CFA; needed to translate this expression")); 787 788 gdb_assert (per_objfile != nullptr); 789 790 dwarf2_frame_state fs (pc1, fde->cie); 791 792 /* Check for "quirks" - known bugs in producers. */ 793 dwarf2_frame_find_quirks (&fs, fde); 794 795 /* First decode all the insns in the CIE. */ 796 execute_cfa_program (fde, fde->cie->initial_instructions, 797 fde->cie->end, gdbarch, pc, &fs, 798 per_objfile->objfile->text_section_offset ()); 799 800 /* Save the initialized register set. */ 801 fs.initial = fs.regs; 802 803 /* Then decode the insns in the FDE up to our target PC. */ 804 execute_cfa_program (fde, fde->instructions, fde->end, gdbarch, pc, &fs, 805 per_objfile->objfile->text_section_offset ()); 806 807 /* Calculate the CFA. */ 808 switch (fs.regs.cfa_how) 809 { 810 case CFA_REG_OFFSET: 811 { 812 int regnum = dwarf_reg_to_regnum_or_error (gdbarch, fs.regs.cfa_reg); 813 814 *regnum_out = regnum; 815 if (fs.armcc_cfa_offsets_reversed) 816 *offset_out = -fs.regs.cfa_offset; 817 else 818 *offset_out = fs.regs.cfa_offset; 819 return 1; 820 } 821 822 case CFA_EXP: 823 *text_offset_out = per_objfile->objfile->text_section_offset (); 824 *cfa_start_out = fs.regs.cfa_exp; 825 *cfa_end_out = fs.regs.cfa_exp + fs.regs.cfa_exp_len; 826 return 0; 827 828 default: 829 internal_error (_("Unknown CFA rule.")); 830 } 831 } 832 833 834 /* Custom function data object for architecture specific prev_register 835 implementation. Main purpose of this object is to allow caching of 836 expensive data lookups in the prev_register handling. */ 837 838 struct dwarf2_frame_fn_data 839 { 840 /* The cookie to identify the custom function data by. */ 841 fn_prev_register cookie; 842 843 /* The custom function data. */ 844 void *data; 845 846 /* Pointer to the next custom function data object for this frame. */ 847 struct dwarf2_frame_fn_data *next; 848 }; 849 850 struct dwarf2_frame_cache 851 { 852 /* DWARF Call Frame Address. */ 853 CORE_ADDR cfa; 854 855 /* Set if the return address column was marked as unavailable 856 (required non-collected memory or registers to compute). */ 857 int unavailable_retaddr; 858 859 /* Set if the return address column was marked as undefined. */ 860 int undefined_retaddr; 861 862 /* Saved registers, indexed by GDB register number, not by DWARF 863 register number. */ 864 struct dwarf2_frame_state_reg *reg; 865 866 /* Return address register. */ 867 struct dwarf2_frame_state_reg retaddr_reg; 868 869 /* Target address size in bytes. */ 870 int addr_size; 871 872 /* The dwarf2_per_objfile from which this frame description came. */ 873 dwarf2_per_objfile *per_objfile; 874 875 /* If not NULL then this frame is the bottom frame of a TAILCALL_FRAME 876 sequence. If NULL then it is a normal case with no TAILCALL_FRAME 877 involved. Non-bottom frames of a virtual tail call frames chain use 878 dwarf2_tailcall_frame_unwind unwinder so this field does not apply for 879 them. */ 880 void *tailcall_cache; 881 882 struct dwarf2_frame_fn_data *fn_data; 883 }; 884 885 static struct dwarf2_frame_cache * 886 dwarf2_frame_cache (frame_info_ptr this_frame, void **this_cache) 887 { 888 struct gdbarch *gdbarch = get_frame_arch (this_frame); 889 const int num_regs = gdbarch_num_cooked_regs (gdbarch); 890 struct dwarf2_frame_cache *cache; 891 struct dwarf2_fde *fde; 892 CORE_ADDR entry_pc; 893 const gdb_byte *instr; 894 895 if (*this_cache) 896 return (struct dwarf2_frame_cache *) *this_cache; 897 898 /* Allocate a new cache. */ 899 cache = FRAME_OBSTACK_ZALLOC (struct dwarf2_frame_cache); 900 cache->reg = FRAME_OBSTACK_CALLOC (num_regs, struct dwarf2_frame_state_reg); 901 *this_cache = cache; 902 903 /* Unwind the PC. 904 905 Note that if the next frame is never supposed to return (i.e. a call 906 to abort), the compiler might optimize away the instruction at 907 its return address. As a result the return address will 908 point at some random instruction, and the CFI for that 909 instruction is probably worthless to us. GCC's unwinder solves 910 this problem by substracting 1 from the return address to get an 911 address in the middle of a presumed call instruction (or the 912 instruction in the associated delay slot). This should only be 913 done for "normal" frames and not for resume-type frames (signal 914 handlers, sentinel frames, dummy frames). The function 915 get_frame_address_in_block does just this. It's not clear how 916 reliable the method is though; there is the potential for the 917 register state pre-call being different to that on return. */ 918 CORE_ADDR pc1 = get_frame_address_in_block (this_frame); 919 920 /* Find the correct FDE. */ 921 fde = dwarf2_frame_find_fde (&pc1, &cache->per_objfile); 922 gdb_assert (fde != NULL); 923 gdb_assert (cache->per_objfile != nullptr); 924 925 /* Allocate and initialize the frame state. */ 926 struct dwarf2_frame_state fs (pc1, fde->cie); 927 928 cache->addr_size = fde->cie->addr_size; 929 930 /* Check for "quirks" - known bugs in producers. */ 931 dwarf2_frame_find_quirks (&fs, fde); 932 933 /* First decode all the insns in the CIE. */ 934 execute_cfa_program (fde, fde->cie->initial_instructions, 935 fde->cie->end, gdbarch, 936 get_frame_address_in_block (this_frame), &fs, 937 cache->per_objfile->objfile->text_section_offset ()); 938 939 /* Save the initialized register set. */ 940 fs.initial = fs.regs; 941 942 /* Fetching the entry pc for THIS_FRAME won't necessarily result 943 in an address that's within the range of FDE locations. This 944 is due to the possibility of the function occupying non-contiguous 945 ranges. */ 946 LONGEST entry_cfa_sp_offset; 947 int entry_cfa_sp_offset_p = 0; 948 if (get_frame_func_if_available (this_frame, &entry_pc) 949 && fde->initial_location <= entry_pc 950 && entry_pc < fde->initial_location + fde->address_range) 951 { 952 /* Decode the insns in the FDE up to the entry PC. */ 953 instr = execute_cfa_program 954 (fde, fde->instructions, fde->end, gdbarch, entry_pc, &fs, 955 cache->per_objfile->objfile->text_section_offset ()); 956 957 if (fs.regs.cfa_how == CFA_REG_OFFSET 958 && (dwarf_reg_to_regnum (gdbarch, fs.regs.cfa_reg) 959 == gdbarch_sp_regnum (gdbarch))) 960 { 961 entry_cfa_sp_offset = fs.regs.cfa_offset; 962 entry_cfa_sp_offset_p = 1; 963 } 964 } 965 else 966 instr = fde->instructions; 967 968 /* Then decode the insns in the FDE up to our target PC. */ 969 execute_cfa_program (fde, instr, fde->end, gdbarch, 970 get_frame_address_in_block (this_frame), &fs, 971 cache->per_objfile->objfile->text_section_offset ()); 972 973 try 974 { 975 /* Calculate the CFA. */ 976 switch (fs.regs.cfa_how) 977 { 978 case CFA_REG_OFFSET: 979 cache->cfa = read_addr_from_reg (this_frame, fs.regs.cfa_reg); 980 if (fs.armcc_cfa_offsets_reversed) 981 cache->cfa -= fs.regs.cfa_offset; 982 else 983 cache->cfa += fs.regs.cfa_offset; 984 break; 985 986 case CFA_EXP: 987 cache->cfa = 988 execute_stack_op (fs.regs.cfa_exp, fs.regs.cfa_exp_len, 989 cache->addr_size, this_frame, 0, 0, 990 cache->per_objfile); 991 break; 992 993 default: 994 internal_error (_("Unknown CFA rule.")); 995 } 996 } 997 catch (const gdb_exception_error &ex) 998 { 999 if (ex.error == NOT_AVAILABLE_ERROR) 1000 { 1001 cache->unavailable_retaddr = 1; 1002 return cache; 1003 } 1004 1005 throw; 1006 } 1007 1008 /* Initialize the register state. */ 1009 { 1010 int regnum; 1011 1012 for (regnum = 0; regnum < num_regs; regnum++) 1013 dwarf2_frame_init_reg (gdbarch, regnum, &cache->reg[regnum], this_frame); 1014 } 1015 1016 /* Go through the DWARF2 CFI generated table and save its register 1017 location information in the cache. Note that we don't skip the 1018 return address column; it's perfectly all right for it to 1019 correspond to a real register. */ 1020 { 1021 int column; /* CFI speak for "register number". */ 1022 1023 for (column = 0; column < fs.regs.reg.size (); column++) 1024 { 1025 /* Use the GDB register number as the destination index. */ 1026 int regnum = dwarf_reg_to_regnum (gdbarch, column); 1027 1028 /* Protect against a target returning a bad register. */ 1029 if (regnum < 0 || regnum >= num_regs) 1030 continue; 1031 1032 /* NOTE: cagney/2003-09-05: CFI should specify the disposition 1033 of all debug info registers. If it doesn't, complain (but 1034 not too loudly). It turns out that GCC assumes that an 1035 unspecified register implies "same value" when CFI (draft 1036 7) specifies nothing at all. Such a register could equally 1037 be interpreted as "undefined". Also note that this check 1038 isn't sufficient; it only checks that all registers in the 1039 range [0 .. max column] are specified, and won't detect 1040 problems when a debug info register falls outside of the 1041 table. We need a way of iterating through all the valid 1042 DWARF2 register numbers. */ 1043 if (fs.regs.reg[column].how == DWARF2_FRAME_REG_UNSPECIFIED) 1044 { 1045 if (cache->reg[regnum].how == DWARF2_FRAME_REG_UNSPECIFIED) 1046 complaint (_("\ 1047 incomplete CFI data; unspecified registers (e.g., %s) at %s"), 1048 gdbarch_register_name (gdbarch, regnum), 1049 paddress (gdbarch, fs.pc)); 1050 } 1051 else 1052 cache->reg[regnum] = fs.regs.reg[column]; 1053 } 1054 } 1055 1056 /* Eliminate any DWARF2_FRAME_REG_RA rules, and save the information 1057 we need for evaluating DWARF2_FRAME_REG_RA_OFFSET rules. */ 1058 { 1059 int regnum; 1060 1061 for (regnum = 0; regnum < num_regs; regnum++) 1062 { 1063 if (cache->reg[regnum].how == DWARF2_FRAME_REG_RA 1064 || cache->reg[regnum].how == DWARF2_FRAME_REG_RA_OFFSET) 1065 { 1066 const std::vector<struct dwarf2_frame_state_reg> ®s 1067 = fs.regs.reg; 1068 ULONGEST retaddr_column = fs.retaddr_column; 1069 1070 /* It seems rather bizarre to specify an "empty" column as 1071 the return adress column. However, this is exactly 1072 what GCC does on some targets. It turns out that GCC 1073 assumes that the return address can be found in the 1074 register corresponding to the return address column. 1075 Incidentally, that's how we should treat a return 1076 address column specifying "same value" too. */ 1077 if (fs.retaddr_column < fs.regs.reg.size () 1078 && regs[retaddr_column].how != DWARF2_FRAME_REG_UNSPECIFIED 1079 && regs[retaddr_column].how != DWARF2_FRAME_REG_SAME_VALUE) 1080 { 1081 if (cache->reg[regnum].how == DWARF2_FRAME_REG_RA) 1082 cache->reg[regnum] = regs[retaddr_column]; 1083 else 1084 cache->retaddr_reg = regs[retaddr_column]; 1085 } 1086 else 1087 { 1088 if (cache->reg[regnum].how == DWARF2_FRAME_REG_RA) 1089 { 1090 cache->reg[regnum].loc.reg = fs.retaddr_column; 1091 cache->reg[regnum].how = DWARF2_FRAME_REG_SAVED_REG; 1092 } 1093 else 1094 { 1095 cache->retaddr_reg.loc.reg = fs.retaddr_column; 1096 cache->retaddr_reg.how = DWARF2_FRAME_REG_SAVED_REG; 1097 } 1098 } 1099 } 1100 } 1101 } 1102 1103 if (fs.retaddr_column < fs.regs.reg.size () 1104 && fs.regs.reg[fs.retaddr_column].how == DWARF2_FRAME_REG_UNDEFINED) 1105 cache->undefined_retaddr = 1; 1106 1107 dwarf2_tailcall_sniffer_first (this_frame, &cache->tailcall_cache, 1108 (entry_cfa_sp_offset_p 1109 ? &entry_cfa_sp_offset : NULL)); 1110 1111 return cache; 1112 } 1113 1114 static enum unwind_stop_reason 1115 dwarf2_frame_unwind_stop_reason (frame_info_ptr this_frame, 1116 void **this_cache) 1117 { 1118 struct dwarf2_frame_cache *cache 1119 = dwarf2_frame_cache (this_frame, this_cache); 1120 1121 if (cache->unavailable_retaddr) 1122 return UNWIND_UNAVAILABLE; 1123 1124 if (cache->undefined_retaddr) 1125 return UNWIND_OUTERMOST; 1126 1127 return UNWIND_NO_REASON; 1128 } 1129 1130 static void 1131 dwarf2_frame_this_id (frame_info_ptr this_frame, void **this_cache, 1132 struct frame_id *this_id) 1133 { 1134 struct dwarf2_frame_cache *cache = 1135 dwarf2_frame_cache (this_frame, this_cache); 1136 1137 if (cache->unavailable_retaddr) 1138 (*this_id) = frame_id_build_unavailable_stack (get_frame_func (this_frame)); 1139 else if (cache->undefined_retaddr) 1140 return; 1141 else 1142 (*this_id) = frame_id_build (cache->cfa, get_frame_func (this_frame)); 1143 } 1144 1145 static struct value * 1146 dwarf2_frame_prev_register (frame_info_ptr this_frame, void **this_cache, 1147 int regnum) 1148 { 1149 struct gdbarch *gdbarch = get_frame_arch (this_frame); 1150 struct dwarf2_frame_cache *cache = 1151 dwarf2_frame_cache (this_frame, this_cache); 1152 CORE_ADDR addr; 1153 int realnum; 1154 1155 /* Non-bottom frames of a virtual tail call frames chain use 1156 dwarf2_tailcall_frame_unwind unwinder so this code does not apply for 1157 them. If dwarf2_tailcall_prev_register_first does not have specific value 1158 unwind the register, tail call frames are assumed to have the register set 1159 of the top caller. */ 1160 if (cache->tailcall_cache) 1161 { 1162 struct value *val; 1163 1164 val = dwarf2_tailcall_prev_register_first (this_frame, 1165 &cache->tailcall_cache, 1166 regnum); 1167 if (val) 1168 return val; 1169 } 1170 1171 switch (cache->reg[regnum].how) 1172 { 1173 case DWARF2_FRAME_REG_UNDEFINED: 1174 /* If CFI explicitly specified that the value isn't defined, 1175 mark it as optimized away; the value isn't available. */ 1176 return frame_unwind_got_optimized (this_frame, regnum); 1177 1178 case DWARF2_FRAME_REG_SAVED_OFFSET: 1179 addr = cache->cfa + cache->reg[regnum].loc.offset; 1180 return frame_unwind_got_memory (this_frame, regnum, addr); 1181 1182 case DWARF2_FRAME_REG_SAVED_REG: 1183 realnum = dwarf_reg_to_regnum_or_error 1184 (gdbarch, cache->reg[regnum].loc.reg); 1185 return frame_unwind_got_register (this_frame, regnum, realnum); 1186 1187 case DWARF2_FRAME_REG_SAVED_EXP: 1188 addr = execute_stack_op (cache->reg[regnum].loc.exp.start, 1189 cache->reg[regnum].loc.exp.len, 1190 cache->addr_size, 1191 this_frame, cache->cfa, 1, 1192 cache->per_objfile); 1193 return frame_unwind_got_memory (this_frame, regnum, addr); 1194 1195 case DWARF2_FRAME_REG_SAVED_VAL_OFFSET: 1196 addr = cache->cfa + cache->reg[regnum].loc.offset; 1197 return frame_unwind_got_constant (this_frame, regnum, addr); 1198 1199 case DWARF2_FRAME_REG_SAVED_VAL_EXP: 1200 addr = execute_stack_op (cache->reg[regnum].loc.exp.start, 1201 cache->reg[regnum].loc.exp.len, 1202 cache->addr_size, 1203 this_frame, cache->cfa, 1, 1204 cache->per_objfile); 1205 return frame_unwind_got_constant (this_frame, regnum, addr); 1206 1207 case DWARF2_FRAME_REG_UNSPECIFIED: 1208 /* GCC, in its infinite wisdom decided to not provide unwind 1209 information for registers that are "same value". Since 1210 DWARF2 (3 draft 7) doesn't define such behavior, said 1211 registers are actually undefined (which is different to CFI 1212 "undefined"). Code above issues a complaint about this. 1213 Here just fudge the books, assume GCC, and that the value is 1214 more inner on the stack. */ 1215 return frame_unwind_got_register (this_frame, regnum, regnum); 1216 1217 case DWARF2_FRAME_REG_SAME_VALUE: 1218 return frame_unwind_got_register (this_frame, regnum, regnum); 1219 1220 case DWARF2_FRAME_REG_CFA: 1221 return frame_unwind_got_address (this_frame, regnum, cache->cfa); 1222 1223 case DWARF2_FRAME_REG_CFA_OFFSET: 1224 addr = cache->cfa + cache->reg[regnum].loc.offset; 1225 return frame_unwind_got_address (this_frame, regnum, addr); 1226 1227 case DWARF2_FRAME_REG_RA_OFFSET: 1228 addr = cache->reg[regnum].loc.offset; 1229 regnum = dwarf_reg_to_regnum_or_error 1230 (gdbarch, cache->retaddr_reg.loc.reg); 1231 addr += get_frame_register_unsigned (this_frame, regnum); 1232 return frame_unwind_got_address (this_frame, regnum, addr); 1233 1234 case DWARF2_FRAME_REG_FN: 1235 return cache->reg[regnum].loc.fn (this_frame, this_cache, regnum); 1236 1237 default: 1238 internal_error (_("Unknown register rule.")); 1239 } 1240 } 1241 1242 /* See frame.h. */ 1243 1244 void * 1245 dwarf2_frame_get_fn_data (frame_info_ptr this_frame, void **this_cache, 1246 fn_prev_register cookie) 1247 { 1248 struct dwarf2_frame_fn_data *fn_data = nullptr; 1249 struct dwarf2_frame_cache *cache 1250 = dwarf2_frame_cache (this_frame, this_cache); 1251 1252 /* Find the object for the function. */ 1253 for (fn_data = cache->fn_data; fn_data; fn_data = fn_data->next) 1254 if (fn_data->cookie == cookie) 1255 return fn_data->data; 1256 1257 return nullptr; 1258 } 1259 1260 /* See frame.h. */ 1261 1262 void * 1263 dwarf2_frame_allocate_fn_data (frame_info_ptr this_frame, void **this_cache, 1264 fn_prev_register cookie, unsigned long size) 1265 { 1266 struct dwarf2_frame_fn_data *fn_data = nullptr; 1267 struct dwarf2_frame_cache *cache 1268 = dwarf2_frame_cache (this_frame, this_cache); 1269 1270 /* First try to find an existing object. */ 1271 void *data = dwarf2_frame_get_fn_data (this_frame, this_cache, cookie); 1272 gdb_assert (data == nullptr); 1273 1274 /* No object found, lets create a new instance. */ 1275 fn_data = FRAME_OBSTACK_ZALLOC (struct dwarf2_frame_fn_data); 1276 fn_data->cookie = cookie; 1277 fn_data->data = frame_obstack_zalloc (size); 1278 fn_data->next = cache->fn_data; 1279 cache->fn_data = fn_data; 1280 1281 return fn_data->data; 1282 } 1283 1284 /* Proxy for tailcall_frame_dealloc_cache for bottom frame of a virtual tail 1285 call frames chain. */ 1286 1287 static void 1288 dwarf2_frame_dealloc_cache (frame_info *self, void *this_cache) 1289 { 1290 struct dwarf2_frame_cache *cache 1291 = dwarf2_frame_cache (frame_info_ptr (self), &this_cache); 1292 1293 if (cache->tailcall_cache) 1294 dwarf2_tailcall_frame_unwind.dealloc_cache (self, cache->tailcall_cache); 1295 } 1296 1297 static int 1298 dwarf2_frame_sniffer (const struct frame_unwind *self, 1299 frame_info_ptr this_frame, void **this_cache) 1300 { 1301 if (!dwarf2_frame_unwinders_enabled_p) 1302 return 0; 1303 1304 /* Grab an address that is guaranteed to reside somewhere within the 1305 function. get_frame_pc(), with a no-return next function, can 1306 end up returning something past the end of this function's body. 1307 If the frame we're sniffing for is a signal frame whose start 1308 address is placed on the stack by the OS, its FDE must 1309 extend one byte before its start address or we could potentially 1310 select the FDE of the previous function. */ 1311 CORE_ADDR block_addr = get_frame_address_in_block (this_frame); 1312 struct dwarf2_fde *fde = dwarf2_frame_find_fde (&block_addr, NULL); 1313 1314 if (!fde) 1315 return 0; 1316 1317 /* On some targets, signal trampolines may have unwind information. 1318 We need to recognize them so that we set the frame type 1319 correctly. */ 1320 1321 if (fde->cie->signal_frame 1322 || dwarf2_frame_signal_frame_p (get_frame_arch (this_frame), 1323 this_frame)) 1324 return self->type == SIGTRAMP_FRAME; 1325 1326 if (self->type != NORMAL_FRAME) 1327 return 0; 1328 1329 return 1; 1330 } 1331 1332 static const struct frame_unwind dwarf2_frame_unwind = 1333 { 1334 "dwarf2", 1335 NORMAL_FRAME, 1336 dwarf2_frame_unwind_stop_reason, 1337 dwarf2_frame_this_id, 1338 dwarf2_frame_prev_register, 1339 NULL, 1340 dwarf2_frame_sniffer, 1341 dwarf2_frame_dealloc_cache 1342 }; 1343 1344 static const struct frame_unwind dwarf2_signal_frame_unwind = 1345 { 1346 "dwarf2 signal", 1347 SIGTRAMP_FRAME, 1348 dwarf2_frame_unwind_stop_reason, 1349 dwarf2_frame_this_id, 1350 dwarf2_frame_prev_register, 1351 NULL, 1352 dwarf2_frame_sniffer, 1353 1354 /* TAILCALL_CACHE can never be in such frame to need dealloc_cache. */ 1355 NULL 1356 }; 1357 1358 /* Append the DWARF-2 frame unwinders to GDBARCH's list. */ 1359 1360 void 1361 dwarf2_append_unwinders (struct gdbarch *gdbarch) 1362 { 1363 frame_unwind_append_unwinder (gdbarch, &dwarf2_frame_unwind); 1364 frame_unwind_append_unwinder (gdbarch, &dwarf2_signal_frame_unwind); 1365 } 1366 1367 1368 /* There is no explicitly defined relationship between the CFA and the 1369 location of frame's local variables and arguments/parameters. 1370 Therefore, frame base methods on this page should probably only be 1371 used as a last resort, just to avoid printing total garbage as a 1372 response to the "info frame" command. */ 1373 1374 static CORE_ADDR 1375 dwarf2_frame_base_address (frame_info_ptr this_frame, void **this_cache) 1376 { 1377 struct dwarf2_frame_cache *cache = 1378 dwarf2_frame_cache (this_frame, this_cache); 1379 1380 return cache->cfa; 1381 } 1382 1383 static const struct frame_base dwarf2_frame_base = 1384 { 1385 &dwarf2_frame_unwind, 1386 dwarf2_frame_base_address, 1387 dwarf2_frame_base_address, 1388 dwarf2_frame_base_address 1389 }; 1390 1391 const struct frame_base * 1392 dwarf2_frame_base_sniffer (frame_info_ptr this_frame) 1393 { 1394 CORE_ADDR block_addr = get_frame_address_in_block (this_frame); 1395 1396 if (dwarf2_frame_find_fde (&block_addr, NULL)) 1397 return &dwarf2_frame_base; 1398 1399 return NULL; 1400 } 1401 1402 /* Compute the CFA for THIS_FRAME, but only if THIS_FRAME came from 1403 the DWARF unwinder. This is used to implement 1404 DW_OP_call_frame_cfa. */ 1405 1406 CORE_ADDR 1407 dwarf2_frame_cfa (frame_info_ptr this_frame) 1408 { 1409 if (frame_unwinder_is (this_frame, &record_btrace_tailcall_frame_unwind) 1410 || frame_unwinder_is (this_frame, &record_btrace_frame_unwind)) 1411 throw_error (NOT_AVAILABLE_ERROR, 1412 _("cfa not available for record btrace target")); 1413 1414 while (get_frame_type (this_frame) == INLINE_FRAME) 1415 this_frame = get_prev_frame (this_frame); 1416 if (get_frame_unwind_stop_reason (this_frame) == UNWIND_UNAVAILABLE) 1417 throw_error (NOT_AVAILABLE_ERROR, 1418 _("can't compute CFA for this frame: " 1419 "required registers or memory are unavailable")); 1420 1421 if (get_frame_id (this_frame).stack_status != FID_STACK_VALID) 1422 throw_error (NOT_AVAILABLE_ERROR, 1423 _("can't compute CFA for this frame: " 1424 "frame base not available")); 1425 1426 return get_frame_base (this_frame); 1427 } 1428 1429 /* We store the frame data on the BFD. This is only done if it is 1430 independent of the address space and so can be shared. */ 1431 static const registry<bfd>::key<comp_unit> dwarf2_frame_bfd_data; 1432 1433 /* If any BFD sections require relocations (note; really should be if 1434 any debug info requires relocations), then we store the frame data 1435 on the objfile instead, and do not share it. */ 1436 static const registry<objfile>::key<comp_unit> dwarf2_frame_objfile_data; 1437 1438 1439 /* Pointer encoding helper functions. */ 1440 1441 /* GCC supports exception handling based on DWARF2 CFI. However, for 1442 technical reasons, it encodes addresses in its FDE's in a different 1443 way. Several "pointer encodings" are supported. The encoding 1444 that's used for a particular FDE is determined by the 'R' 1445 augmentation in the associated CIE. The argument of this 1446 augmentation is a single byte. 1447 1448 The address can be encoded as 2 bytes, 4 bytes, 8 bytes, or as a 1449 LEB128. This is encoded in bits 0, 1 and 2. Bit 3 encodes whether 1450 the address is signed or unsigned. Bits 4, 5 and 6 encode how the 1451 address should be interpreted (absolute, relative to the current 1452 position in the FDE, ...). Bit 7, indicates that the address 1453 should be dereferenced. */ 1454 1455 static gdb_byte 1456 encoding_for_size (unsigned int size) 1457 { 1458 switch (size) 1459 { 1460 case 2: 1461 return DW_EH_PE_udata2; 1462 case 4: 1463 return DW_EH_PE_udata4; 1464 case 8: 1465 return DW_EH_PE_udata8; 1466 default: 1467 internal_error (_("Unsupported address size")); 1468 } 1469 } 1470 1471 static CORE_ADDR 1472 read_encoded_value (struct comp_unit *unit, gdb_byte encoding, 1473 int ptr_len, const gdb_byte *buf, 1474 unsigned int *bytes_read_ptr, 1475 CORE_ADDR func_base) 1476 { 1477 ptrdiff_t offset; 1478 CORE_ADDR base; 1479 1480 /* GCC currently doesn't generate DW_EH_PE_indirect encodings for 1481 FDE's. */ 1482 if (encoding & DW_EH_PE_indirect) 1483 internal_error (_("Unsupported encoding: DW_EH_PE_indirect")); 1484 1485 *bytes_read_ptr = 0; 1486 1487 switch (encoding & 0x70) 1488 { 1489 case DW_EH_PE_absptr: 1490 base = 0; 1491 break; 1492 case DW_EH_PE_pcrel: 1493 base = bfd_section_vma (unit->dwarf_frame_section); 1494 base += (buf - unit->dwarf_frame_buffer); 1495 break; 1496 case DW_EH_PE_datarel: 1497 base = unit->dbase; 1498 break; 1499 case DW_EH_PE_textrel: 1500 base = unit->tbase; 1501 break; 1502 case DW_EH_PE_funcrel: 1503 base = func_base; 1504 break; 1505 case DW_EH_PE_aligned: 1506 base = 0; 1507 offset = buf - unit->dwarf_frame_buffer; 1508 if ((offset % ptr_len) != 0) 1509 { 1510 *bytes_read_ptr = ptr_len - (offset % ptr_len); 1511 buf += *bytes_read_ptr; 1512 } 1513 break; 1514 default: 1515 internal_error (_("Invalid or unsupported encoding")); 1516 } 1517 1518 if ((encoding & 0x07) == 0x00) 1519 { 1520 encoding |= encoding_for_size (ptr_len); 1521 if (bfd_get_sign_extend_vma (unit->abfd)) 1522 encoding |= DW_EH_PE_signed; 1523 } 1524 1525 switch (encoding & 0x0f) 1526 { 1527 case DW_EH_PE_uleb128: 1528 { 1529 uint64_t value; 1530 const gdb_byte *end_buf = buf + (sizeof (value) + 1) * 8 / 7; 1531 1532 *bytes_read_ptr += safe_read_uleb128 (buf, end_buf, &value) - buf; 1533 return base + value; 1534 } 1535 case DW_EH_PE_udata2: 1536 *bytes_read_ptr += 2; 1537 return (base + bfd_get_16 (unit->abfd, (bfd_byte *) buf)); 1538 case DW_EH_PE_udata4: 1539 *bytes_read_ptr += 4; 1540 return (base + bfd_get_32 (unit->abfd, (bfd_byte *) buf)); 1541 case DW_EH_PE_udata8: 1542 *bytes_read_ptr += 8; 1543 return (base + bfd_get_64 (unit->abfd, (bfd_byte *) buf)); 1544 case DW_EH_PE_sleb128: 1545 { 1546 int64_t value; 1547 const gdb_byte *end_buf = buf + (sizeof (value) + 1) * 8 / 7; 1548 1549 *bytes_read_ptr += safe_read_sleb128 (buf, end_buf, &value) - buf; 1550 return base + value; 1551 } 1552 case DW_EH_PE_sdata2: 1553 *bytes_read_ptr += 2; 1554 return (base + bfd_get_signed_16 (unit->abfd, (bfd_byte *) buf)); 1555 case DW_EH_PE_sdata4: 1556 *bytes_read_ptr += 4; 1557 return (base + bfd_get_signed_32 (unit->abfd, (bfd_byte *) buf)); 1558 case DW_EH_PE_sdata8: 1559 *bytes_read_ptr += 8; 1560 return (base + bfd_get_signed_64 (unit->abfd, (bfd_byte *) buf)); 1561 default: 1562 internal_error (_("Invalid or unsupported encoding")); 1563 } 1564 } 1565 1566 1567 /* Find CIE with the given CIE_POINTER in CIE_TABLE. */ 1568 static struct dwarf2_cie * 1569 find_cie (const dwarf2_cie_table &cie_table, ULONGEST cie_pointer) 1570 { 1571 auto iter = cie_table.find (cie_pointer); 1572 if (iter != cie_table.end ()) 1573 return iter->second; 1574 return NULL; 1575 } 1576 1577 static inline int 1578 bsearch_fde_cmp (const dwarf2_fde *fde, CORE_ADDR seek_pc) 1579 { 1580 if (fde->initial_location + fde->address_range <= seek_pc) 1581 return -1; 1582 if (fde->initial_location <= seek_pc) 1583 return 0; 1584 return 1; 1585 } 1586 1587 /* Find an existing comp_unit for an objfile, if any. */ 1588 1589 static comp_unit * 1590 find_comp_unit (struct objfile *objfile) 1591 { 1592 bfd *abfd = objfile->obfd.get (); 1593 if (gdb_bfd_requires_relocations (abfd)) 1594 return dwarf2_frame_objfile_data.get (objfile); 1595 1596 return dwarf2_frame_bfd_data.get (abfd); 1597 } 1598 1599 /* Store the comp_unit on OBJFILE, or the corresponding BFD, as 1600 appropriate. */ 1601 1602 static void 1603 set_comp_unit (struct objfile *objfile, struct comp_unit *unit) 1604 { 1605 bfd *abfd = objfile->obfd.get (); 1606 if (gdb_bfd_requires_relocations (abfd)) 1607 return dwarf2_frame_objfile_data.set (objfile, unit); 1608 1609 return dwarf2_frame_bfd_data.set (abfd, unit); 1610 } 1611 1612 /* Find the FDE for *PC. Return a pointer to the FDE, and store the 1613 initial location associated with it into *PC. */ 1614 1615 static struct dwarf2_fde * 1616 dwarf2_frame_find_fde (CORE_ADDR *pc, dwarf2_per_objfile **out_per_objfile) 1617 { 1618 for (objfile *objfile : current_program_space->objfiles ()) 1619 { 1620 CORE_ADDR offset; 1621 CORE_ADDR seek_pc; 1622 1623 if (objfile->obfd == nullptr) 1624 continue; 1625 1626 comp_unit *unit = find_comp_unit (objfile); 1627 if (unit == NULL) 1628 { 1629 dwarf2_build_frame_info (objfile); 1630 unit = find_comp_unit (objfile); 1631 } 1632 gdb_assert (unit != NULL); 1633 1634 dwarf2_fde_table *fde_table = &unit->fde_table; 1635 if (fde_table->empty ()) 1636 continue; 1637 1638 gdb_assert (!objfile->section_offsets.empty ()); 1639 offset = objfile->text_section_offset (); 1640 1641 gdb_assert (!fde_table->empty ()); 1642 if (*pc < offset + (*fde_table)[0]->initial_location) 1643 continue; 1644 1645 seek_pc = *pc - offset; 1646 auto it = gdb::binary_search (fde_table->begin (), fde_table->end (), 1647 seek_pc, bsearch_fde_cmp); 1648 if (it != fde_table->end ()) 1649 { 1650 *pc = (*it)->initial_location + offset; 1651 if (out_per_objfile != nullptr) 1652 *out_per_objfile = get_dwarf2_per_objfile (objfile); 1653 1654 return *it; 1655 } 1656 } 1657 return NULL; 1658 } 1659 1660 /* Add FDE to FDE_TABLE. */ 1661 static void 1662 add_fde (dwarf2_fde_table *fde_table, struct dwarf2_fde *fde) 1663 { 1664 if (fde->address_range == 0) 1665 /* Discard useless FDEs. */ 1666 return; 1667 1668 fde_table->push_back (fde); 1669 } 1670 1671 #define DW64_CIE_ID 0xffffffffffffffffULL 1672 1673 /* Defines the type of eh_frames that are expected to be decoded: CIE, FDE 1674 or any of them. */ 1675 1676 enum eh_frame_type 1677 { 1678 EH_CIE_TYPE_ID = 1 << 0, 1679 EH_FDE_TYPE_ID = 1 << 1, 1680 EH_CIE_OR_FDE_TYPE_ID = EH_CIE_TYPE_ID | EH_FDE_TYPE_ID 1681 }; 1682 1683 static const gdb_byte *decode_frame_entry (struct gdbarch *gdbarch, 1684 struct comp_unit *unit, 1685 const gdb_byte *start, 1686 int eh_frame_p, 1687 dwarf2_cie_table &cie_table, 1688 dwarf2_fde_table *fde_table, 1689 enum eh_frame_type entry_type); 1690 1691 /* Decode the next CIE or FDE, entry_type specifies the expected type. 1692 Return NULL if invalid input, otherwise the next byte to be processed. */ 1693 1694 static const gdb_byte * 1695 decode_frame_entry_1 (struct gdbarch *gdbarch, 1696 struct comp_unit *unit, const gdb_byte *start, 1697 int eh_frame_p, 1698 dwarf2_cie_table &cie_table, 1699 dwarf2_fde_table *fde_table, 1700 enum eh_frame_type entry_type) 1701 { 1702 const gdb_byte *buf, *end; 1703 ULONGEST length; 1704 unsigned int bytes_read; 1705 int dwarf64_p; 1706 ULONGEST cie_id; 1707 ULONGEST cie_pointer; 1708 int64_t sleb128; 1709 uint64_t uleb128; 1710 1711 buf = start; 1712 length = read_initial_length (unit->abfd, buf, &bytes_read, false); 1713 buf += bytes_read; 1714 end = buf + (size_t) length; 1715 1716 if (length == 0) 1717 return end; 1718 1719 /* Are we still within the section? */ 1720 if (end <= buf || end > unit->dwarf_frame_buffer + unit->dwarf_frame_size) 1721 return NULL; 1722 1723 /* Distinguish between 32 and 64-bit encoded frame info. */ 1724 dwarf64_p = (bytes_read == 12); 1725 1726 /* In a .eh_frame section, zero is used to distinguish CIEs from FDEs. */ 1727 if (eh_frame_p) 1728 cie_id = 0; 1729 else if (dwarf64_p) 1730 cie_id = DW64_CIE_ID; 1731 else 1732 cie_id = DW_CIE_ID; 1733 1734 if (dwarf64_p) 1735 { 1736 cie_pointer = read_8_bytes (unit->abfd, buf); 1737 buf += 8; 1738 } 1739 else 1740 { 1741 cie_pointer = read_4_bytes (unit->abfd, buf); 1742 buf += 4; 1743 } 1744 1745 if (cie_pointer == cie_id) 1746 { 1747 /* This is a CIE. */ 1748 struct dwarf2_cie *cie; 1749 char *augmentation; 1750 unsigned int cie_version; 1751 1752 /* Check that a CIE was expected. */ 1753 if ((entry_type & EH_CIE_TYPE_ID) == 0) 1754 error (_("Found a CIE when not expecting it.")); 1755 1756 /* Record the offset into the .debug_frame section of this CIE. */ 1757 cie_pointer = start - unit->dwarf_frame_buffer; 1758 1759 /* Check whether we've already read it. */ 1760 if (find_cie (cie_table, cie_pointer)) 1761 return end; 1762 1763 cie = XOBNEW (&unit->obstack, struct dwarf2_cie); 1764 cie->initial_instructions = NULL; 1765 cie->cie_pointer = cie_pointer; 1766 1767 /* The encoding for FDE's in a normal .debug_frame section 1768 depends on the target address size. */ 1769 cie->encoding = DW_EH_PE_absptr; 1770 1771 /* We'll determine the final value later, but we need to 1772 initialize it conservatively. */ 1773 cie->signal_frame = 0; 1774 1775 /* Check version number. */ 1776 cie_version = read_1_byte (unit->abfd, buf); 1777 if (cie_version != 1 && cie_version != 3 && cie_version != 4) 1778 return NULL; 1779 cie->version = cie_version; 1780 buf += 1; 1781 1782 /* Interpret the interesting bits of the augmentation. */ 1783 cie->augmentation = augmentation = (char *) buf; 1784 buf += (strlen (augmentation) + 1); 1785 1786 /* Ignore armcc augmentations. We only use them for quirks, 1787 and that doesn't happen until later. */ 1788 if (startswith (augmentation, "armcc")) 1789 augmentation += strlen (augmentation); 1790 1791 /* The GCC 2.x "eh" augmentation has a pointer immediately 1792 following the augmentation string, so it must be handled 1793 first. */ 1794 if (augmentation[0] == 'e' && augmentation[1] == 'h') 1795 { 1796 /* Skip. */ 1797 buf += gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT; 1798 augmentation += 2; 1799 } 1800 1801 if (cie->version >= 4) 1802 { 1803 /* FIXME: check that this is the same as from the CU header. */ 1804 cie->addr_size = read_1_byte (unit->abfd, buf); 1805 ++buf; 1806 cie->segment_size = read_1_byte (unit->abfd, buf); 1807 ++buf; 1808 } 1809 else 1810 { 1811 cie->addr_size = gdbarch_dwarf2_addr_size (gdbarch); 1812 cie->segment_size = 0; 1813 } 1814 /* Address values in .eh_frame sections are defined to have the 1815 target's pointer size. Watchout: This breaks frame info for 1816 targets with pointer size < address size, unless a .debug_frame 1817 section exists as well. */ 1818 if (eh_frame_p) 1819 cie->ptr_size = gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT; 1820 else 1821 cie->ptr_size = cie->addr_size; 1822 1823 buf = gdb_read_uleb128 (buf, end, &uleb128); 1824 if (buf == NULL) 1825 return NULL; 1826 cie->code_alignment_factor = uleb128; 1827 1828 buf = gdb_read_sleb128 (buf, end, &sleb128); 1829 if (buf == NULL) 1830 return NULL; 1831 cie->data_alignment_factor = sleb128; 1832 1833 if (cie_version == 1) 1834 { 1835 cie->return_address_register = read_1_byte (unit->abfd, buf); 1836 ++buf; 1837 } 1838 else 1839 { 1840 buf = gdb_read_uleb128 (buf, end, &uleb128); 1841 if (buf == NULL) 1842 return NULL; 1843 cie->return_address_register = uleb128; 1844 } 1845 1846 cie->return_address_register 1847 = dwarf2_frame_adjust_regnum (gdbarch, 1848 cie->return_address_register, 1849 eh_frame_p); 1850 1851 cie->saw_z_augmentation = (*augmentation == 'z'); 1852 if (cie->saw_z_augmentation) 1853 { 1854 uint64_t uleb_length; 1855 1856 buf = gdb_read_uleb128 (buf, end, &uleb_length); 1857 if (buf == NULL) 1858 return NULL; 1859 cie->initial_instructions = buf + uleb_length; 1860 augmentation++; 1861 } 1862 1863 while (*augmentation) 1864 { 1865 /* "L" indicates a byte showing how the LSDA pointer is encoded. */ 1866 if (*augmentation == 'L') 1867 { 1868 /* Skip. */ 1869 buf++; 1870 augmentation++; 1871 } 1872 1873 /* "R" indicates a byte indicating how FDE addresses are encoded. */ 1874 else if (*augmentation == 'R') 1875 { 1876 cie->encoding = *buf++; 1877 augmentation++; 1878 } 1879 1880 /* "P" indicates a personality routine in the CIE augmentation. */ 1881 else if (*augmentation == 'P') 1882 { 1883 /* Skip. Avoid indirection since we throw away the result. */ 1884 gdb_byte encoding = (*buf++) & ~DW_EH_PE_indirect; 1885 read_encoded_value (unit, encoding, cie->ptr_size, 1886 buf, &bytes_read, 0); 1887 buf += bytes_read; 1888 augmentation++; 1889 } 1890 1891 /* "S" indicates a signal frame, such that the return 1892 address must not be decremented to locate the call frame 1893 info for the previous frame; it might even be the first 1894 instruction of a function, so decrementing it would take 1895 us to a different function. */ 1896 else if (*augmentation == 'S') 1897 { 1898 cie->signal_frame = 1; 1899 augmentation++; 1900 } 1901 1902 /* Otherwise we have an unknown augmentation. Assume that either 1903 there is no augmentation data, or we saw a 'z' prefix. */ 1904 else 1905 { 1906 if (cie->initial_instructions) 1907 buf = cie->initial_instructions; 1908 break; 1909 } 1910 } 1911 1912 cie->initial_instructions = buf; 1913 cie->end = end; 1914 cie->unit = unit; 1915 1916 cie_table[cie->cie_pointer] = cie; 1917 } 1918 else 1919 { 1920 /* This is a FDE. */ 1921 struct dwarf2_fde *fde; 1922 CORE_ADDR addr; 1923 1924 /* Check that an FDE was expected. */ 1925 if ((entry_type & EH_FDE_TYPE_ID) == 0) 1926 error (_("Found an FDE when not expecting it.")); 1927 1928 /* In an .eh_frame section, the CIE pointer is the delta between the 1929 address within the FDE where the CIE pointer is stored and the 1930 address of the CIE. Convert it to an offset into the .eh_frame 1931 section. */ 1932 if (eh_frame_p) 1933 { 1934 cie_pointer = buf - unit->dwarf_frame_buffer - cie_pointer; 1935 cie_pointer -= (dwarf64_p ? 8 : 4); 1936 } 1937 1938 /* In either case, validate the result is still within the section. */ 1939 if (cie_pointer >= unit->dwarf_frame_size) 1940 return NULL; 1941 1942 fde = XOBNEW (&unit->obstack, struct dwarf2_fde); 1943 fde->cie = find_cie (cie_table, cie_pointer); 1944 if (fde->cie == NULL) 1945 { 1946 decode_frame_entry (gdbarch, unit, 1947 unit->dwarf_frame_buffer + cie_pointer, 1948 eh_frame_p, cie_table, fde_table, 1949 EH_CIE_TYPE_ID); 1950 fde->cie = find_cie (cie_table, cie_pointer); 1951 } 1952 1953 gdb_assert (fde->cie != NULL); 1954 1955 addr = read_encoded_value (unit, fde->cie->encoding, fde->cie->ptr_size, 1956 buf, &bytes_read, 0); 1957 fde->initial_location = gdbarch_adjust_dwarf2_addr (gdbarch, addr); 1958 buf += bytes_read; 1959 1960 fde->address_range = 1961 read_encoded_value (unit, fde->cie->encoding & 0x0f, 1962 fde->cie->ptr_size, buf, &bytes_read, 0); 1963 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + fde->address_range); 1964 fde->address_range = addr - fde->initial_location; 1965 buf += bytes_read; 1966 1967 /* A 'z' augmentation in the CIE implies the presence of an 1968 augmentation field in the FDE as well. The only thing known 1969 to be in here at present is the LSDA entry for EH. So we 1970 can skip the whole thing. */ 1971 if (fde->cie->saw_z_augmentation) 1972 { 1973 uint64_t uleb_length; 1974 1975 buf = gdb_read_uleb128 (buf, end, &uleb_length); 1976 if (buf == NULL) 1977 return NULL; 1978 buf += uleb_length; 1979 if (buf > end) 1980 return NULL; 1981 } 1982 1983 fde->instructions = buf; 1984 fde->end = end; 1985 1986 fde->eh_frame_p = eh_frame_p; 1987 1988 add_fde (fde_table, fde); 1989 } 1990 1991 return end; 1992 } 1993 1994 /* Read a CIE or FDE in BUF and decode it. Entry_type specifies whether we 1995 expect an FDE or a CIE. */ 1996 1997 static const gdb_byte * 1998 decode_frame_entry (struct gdbarch *gdbarch, 1999 struct comp_unit *unit, const gdb_byte *start, 2000 int eh_frame_p, 2001 dwarf2_cie_table &cie_table, 2002 dwarf2_fde_table *fde_table, 2003 enum eh_frame_type entry_type) 2004 { 2005 enum { NONE, ALIGN4, ALIGN8, FAIL } workaround = NONE; 2006 const gdb_byte *ret; 2007 ptrdiff_t start_offset; 2008 2009 while (1) 2010 { 2011 ret = decode_frame_entry_1 (gdbarch, unit, start, eh_frame_p, 2012 cie_table, fde_table, entry_type); 2013 if (ret != NULL) 2014 break; 2015 2016 /* We have corrupt input data of some form. */ 2017 2018 /* ??? Try, weakly, to work around compiler/assembler/linker bugs 2019 and mismatches wrt padding and alignment of debug sections. */ 2020 /* Note that there is no requirement in the standard for any 2021 alignment at all in the frame unwind sections. Testing for 2022 alignment before trying to interpret data would be incorrect. 2023 2024 However, GCC traditionally arranged for frame sections to be 2025 sized such that the FDE length and CIE fields happen to be 2026 aligned (in theory, for performance). This, unfortunately, 2027 was done with .align directives, which had the side effect of 2028 forcing the section to be aligned by the linker. 2029 2030 This becomes a problem when you have some other producer that 2031 creates frame sections that are not as strictly aligned. That 2032 produces a hole in the frame info that gets filled by the 2033 linker with zeros. 2034 2035 The GCC behaviour is arguably a bug, but it's effectively now 2036 part of the ABI, so we're now stuck with it, at least at the 2037 object file level. A smart linker may decide, in the process 2038 of compressing duplicate CIE information, that it can rewrite 2039 the entire output section without this extra padding. */ 2040 2041 start_offset = start - unit->dwarf_frame_buffer; 2042 if (workaround < ALIGN4 && (start_offset & 3) != 0) 2043 { 2044 start += 4 - (start_offset & 3); 2045 workaround = ALIGN4; 2046 continue; 2047 } 2048 if (workaround < ALIGN8 && (start_offset & 7) != 0) 2049 { 2050 start += 8 - (start_offset & 7); 2051 workaround = ALIGN8; 2052 continue; 2053 } 2054 2055 /* Nothing left to try. Arrange to return as if we've consumed 2056 the entire input section. Hopefully we'll get valid info from 2057 the other of .debug_frame/.eh_frame. */ 2058 workaround = FAIL; 2059 ret = unit->dwarf_frame_buffer + unit->dwarf_frame_size; 2060 break; 2061 } 2062 2063 switch (workaround) 2064 { 2065 case NONE: 2066 break; 2067 2068 case ALIGN4: 2069 complaint (_("\ 2070 Corrupt data in %s:%s; align 4 workaround apparently succeeded"), 2071 bfd_get_filename (unit->dwarf_frame_section->owner), 2072 bfd_section_name (unit->dwarf_frame_section)); 2073 break; 2074 2075 case ALIGN8: 2076 complaint (_("\ 2077 Corrupt data in %s:%s; align 8 workaround apparently succeeded"), 2078 bfd_get_filename (unit->dwarf_frame_section->owner), 2079 bfd_section_name (unit->dwarf_frame_section)); 2080 break; 2081 2082 default: 2083 complaint (_("Corrupt data in %s:%s"), 2084 bfd_get_filename (unit->dwarf_frame_section->owner), 2085 bfd_section_name (unit->dwarf_frame_section)); 2086 break; 2087 } 2088 2089 return ret; 2090 } 2091 2092 static bool 2093 fde_is_less_than (const dwarf2_fde *aa, const dwarf2_fde *bb) 2094 { 2095 if (aa->initial_location == bb->initial_location) 2096 { 2097 if (aa->address_range != bb->address_range 2098 && aa->eh_frame_p == 0 && bb->eh_frame_p == 0) 2099 /* Linker bug, e.g. gold/10400. 2100 Work around it by keeping stable sort order. */ 2101 return aa < bb; 2102 else 2103 /* Put eh_frame entries after debug_frame ones. */ 2104 return aa->eh_frame_p < bb->eh_frame_p; 2105 } 2106 2107 return aa->initial_location < bb->initial_location; 2108 } 2109 2110 void 2111 dwarf2_build_frame_info (struct objfile *objfile) 2112 { 2113 const gdb_byte *frame_ptr; 2114 dwarf2_cie_table cie_table; 2115 dwarf2_fde_table fde_table; 2116 2117 struct gdbarch *gdbarch = objfile->arch (); 2118 2119 /* Build a minimal decoding of the DWARF2 compilation unit. */ 2120 std::unique_ptr<comp_unit> unit (new comp_unit (objfile)); 2121 2122 if (objfile->separate_debug_objfile_backlink == NULL) 2123 { 2124 /* Do not read .eh_frame from separate file as they must be also 2125 present in the main file. */ 2126 dwarf2_get_section_info (objfile, DWARF2_EH_FRAME, 2127 &unit->dwarf_frame_section, 2128 &unit->dwarf_frame_buffer, 2129 &unit->dwarf_frame_size); 2130 if (unit->dwarf_frame_size) 2131 { 2132 asection *got, *txt; 2133 2134 /* FIXME: kettenis/20030602: This is the DW_EH_PE_datarel base 2135 that is used for the i386/amd64 target, which currently is 2136 the only target in GCC that supports/uses the 2137 DW_EH_PE_datarel encoding. */ 2138 got = bfd_get_section_by_name (unit->abfd, ".got"); 2139 if (got) 2140 unit->dbase = got->vma; 2141 2142 /* GCC emits the DW_EH_PE_textrel encoding type on sh and ia64 2143 so far. */ 2144 txt = bfd_get_section_by_name (unit->abfd, ".text"); 2145 if (txt) 2146 unit->tbase = txt->vma; 2147 2148 try 2149 { 2150 frame_ptr = unit->dwarf_frame_buffer; 2151 while (frame_ptr < unit->dwarf_frame_buffer + unit->dwarf_frame_size) 2152 frame_ptr = decode_frame_entry (gdbarch, unit.get (), 2153 frame_ptr, 1, 2154 cie_table, &fde_table, 2155 EH_CIE_OR_FDE_TYPE_ID); 2156 } 2157 2158 catch (const gdb_exception_error &e) 2159 { 2160 warning (_("skipping .eh_frame info of %s: %s"), 2161 objfile_name (objfile), e.what ()); 2162 2163 fde_table.clear (); 2164 /* The cie_table is discarded below. */ 2165 } 2166 2167 cie_table.clear (); 2168 } 2169 } 2170 2171 dwarf2_get_section_info (objfile, DWARF2_DEBUG_FRAME, 2172 &unit->dwarf_frame_section, 2173 &unit->dwarf_frame_buffer, 2174 &unit->dwarf_frame_size); 2175 if (unit->dwarf_frame_size) 2176 { 2177 size_t num_old_fde_entries = fde_table.size (); 2178 2179 try 2180 { 2181 frame_ptr = unit->dwarf_frame_buffer; 2182 while (frame_ptr < unit->dwarf_frame_buffer + unit->dwarf_frame_size) 2183 frame_ptr = decode_frame_entry (gdbarch, unit.get (), frame_ptr, 0, 2184 cie_table, &fde_table, 2185 EH_CIE_OR_FDE_TYPE_ID); 2186 } 2187 catch (const gdb_exception_error &e) 2188 { 2189 warning (_("skipping .debug_frame info of %s: %s"), 2190 objfile_name (objfile), e.what ()); 2191 2192 fde_table.resize (num_old_fde_entries); 2193 } 2194 } 2195 2196 struct dwarf2_fde *fde_prev = NULL; 2197 struct dwarf2_fde *first_non_zero_fde = NULL; 2198 2199 /* Prepare FDE table for lookups. */ 2200 std::sort (fde_table.begin (), fde_table.end (), fde_is_less_than); 2201 2202 /* Check for leftovers from --gc-sections. The GNU linker sets 2203 the relevant symbols to zero, but doesn't zero the FDE *end* 2204 ranges because there's no relocation there. It's (offset, 2205 length), not (start, end). On targets where address zero is 2206 just another valid address this can be a problem, since the 2207 FDEs appear to be non-empty in the output --- we could pick 2208 out the wrong FDE. To work around this, when overlaps are 2209 detected, we prefer FDEs that do not start at zero. 2210 2211 Start by finding the first FDE with non-zero start. Below 2212 we'll discard all FDEs that start at zero and overlap this 2213 one. */ 2214 for (struct dwarf2_fde *fde : fde_table) 2215 { 2216 if (fde->initial_location != 0) 2217 { 2218 first_non_zero_fde = fde; 2219 break; 2220 } 2221 } 2222 2223 /* Since we'll be doing bsearch, squeeze out identical (except 2224 for eh_frame_p) fde entries so bsearch result is predictable. 2225 Also discard leftovers from --gc-sections. */ 2226 for (struct dwarf2_fde *fde : fde_table) 2227 { 2228 if (fde->initial_location == 0 2229 && first_non_zero_fde != NULL 2230 && (first_non_zero_fde->initial_location 2231 < fde->initial_location + fde->address_range)) 2232 continue; 2233 2234 if (fde_prev != NULL 2235 && fde_prev->initial_location == fde->initial_location) 2236 continue; 2237 2238 unit->fde_table.push_back (fde); 2239 fde_prev = fde; 2240 } 2241 unit->fde_table.shrink_to_fit (); 2242 2243 set_comp_unit (objfile, unit.release ()); 2244 } 2245 2246 /* Handle 'maintenance show dwarf unwinders'. */ 2247 2248 static void 2249 show_dwarf_unwinders_enabled_p (struct ui_file *file, int from_tty, 2250 struct cmd_list_element *c, 2251 const char *value) 2252 { 2253 gdb_printf (file, 2254 _("The DWARF stack unwinders are currently %s.\n"), 2255 value); 2256 } 2257 2258 void _initialize_dwarf2_frame (); 2259 void 2260 _initialize_dwarf2_frame () 2261 { 2262 add_setshow_boolean_cmd ("unwinders", class_obscure, 2263 &dwarf2_frame_unwinders_enabled_p , _("\ 2264 Set whether the DWARF stack frame unwinders are used."), _("\ 2265 Show whether the DWARF stack frame unwinders are used."), _("\ 2266 When enabled the DWARF stack frame unwinders can be used for architectures\n\ 2267 that support the DWARF unwinders. Enabling the DWARF unwinders for an\n\ 2268 architecture that doesn't support them will have no effect."), 2269 NULL, 2270 show_dwarf_unwinders_enabled_p, 2271 &set_dwarf_cmdlist, 2272 &show_dwarf_cmdlist); 2273 2274 #if GDB_SELF_TEST 2275 selftests::register_test_foreach_arch ("execute_cfa_program", 2276 selftests::execute_cfa_program_test); 2277 #endif 2278 } 2279