1130803Smarcel/* Frame unwinder for frames with DWARF Call Frame Information. 2130803Smarcel 3130803Smarcel Copyright 2003, 2004 Free Software Foundation, Inc. 4130803Smarcel 5130803Smarcel Contributed by Mark Kettenis. 6130803Smarcel 7130803Smarcel This file is part of GDB. 8130803Smarcel 9130803Smarcel This program is free software; you can redistribute it and/or modify 10130803Smarcel it under the terms of the GNU General Public License as published by 11130803Smarcel the Free Software Foundation; either version 2 of the License, or 12130803Smarcel (at your option) any later version. 13130803Smarcel 14130803Smarcel This program is distributed in the hope that it will be useful, 15130803Smarcel but WITHOUT ANY WARRANTY; without even the implied warranty of 16130803Smarcel MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17130803Smarcel GNU General Public License for more details. 18130803Smarcel 19130803Smarcel You should have received a copy of the GNU General Public License 20130803Smarcel along with this program; if not, write to the Free Software 21130803Smarcel Foundation, Inc., 59 Temple Place - Suite 330, 22130803Smarcel Boston, MA 02111-1307, USA. */ 23130803Smarcel 24130803Smarcel#include "defs.h" 25130803Smarcel#include "dwarf2expr.h" 26130803Smarcel#include "elf/dwarf2.h" 27130803Smarcel#include "frame.h" 28130803Smarcel#include "frame-base.h" 29130803Smarcel#include "frame-unwind.h" 30130803Smarcel#include "gdbcore.h" 31130803Smarcel#include "gdbtypes.h" 32130803Smarcel#include "symtab.h" 33130803Smarcel#include "objfiles.h" 34130803Smarcel#include "regcache.h" 35130803Smarcel 36130803Smarcel#include "gdb_assert.h" 37130803Smarcel#include "gdb_string.h" 38130803Smarcel 39130803Smarcel#include "complaints.h" 40130803Smarcel#include "dwarf2-frame.h" 41130803Smarcel 42130803Smarcel/* Call Frame Information (CFI). */ 43130803Smarcel 44130803Smarcel/* Common Information Entry (CIE). */ 45130803Smarcel 46130803Smarcelstruct dwarf2_cie 47130803Smarcel{ 48130803Smarcel /* Offset into the .debug_frame section where this CIE was found. 49130803Smarcel Used to identify this CIE. */ 50130803Smarcel ULONGEST cie_pointer; 51130803Smarcel 52130803Smarcel /* Constant that is factored out of all advance location 53130803Smarcel instructions. */ 54130803Smarcel ULONGEST code_alignment_factor; 55130803Smarcel 56130803Smarcel /* Constants that is factored out of all offset instructions. */ 57130803Smarcel LONGEST data_alignment_factor; 58130803Smarcel 59130803Smarcel /* Return address column. */ 60130803Smarcel ULONGEST return_address_register; 61130803Smarcel 62130803Smarcel /* Instruction sequence to initialize a register set. */ 63130803Smarcel unsigned char *initial_instructions; 64130803Smarcel unsigned char *end; 65130803Smarcel 66130803Smarcel /* Encoding of addresses. */ 67130803Smarcel unsigned char encoding; 68130803Smarcel 69130803Smarcel /* True if a 'z' augmentation existed. */ 70130803Smarcel unsigned char saw_z_augmentation; 71130803Smarcel 72130803Smarcel struct dwarf2_cie *next; 73130803Smarcel}; 74130803Smarcel 75130803Smarcel/* Frame Description Entry (FDE). */ 76130803Smarcel 77130803Smarcelstruct dwarf2_fde 78130803Smarcel{ 79130803Smarcel /* CIE for this FDE. */ 80130803Smarcel struct dwarf2_cie *cie; 81130803Smarcel 82130803Smarcel /* First location associated with this FDE. */ 83130803Smarcel CORE_ADDR initial_location; 84130803Smarcel 85130803Smarcel /* Number of bytes of program instructions described by this FDE. */ 86130803Smarcel CORE_ADDR address_range; 87130803Smarcel 88130803Smarcel /* Instruction sequence. */ 89130803Smarcel unsigned char *instructions; 90130803Smarcel unsigned char *end; 91130803Smarcel 92130803Smarcel struct dwarf2_fde *next; 93130803Smarcel}; 94130803Smarcel 95130803Smarcelstatic struct dwarf2_fde *dwarf2_frame_find_fde (CORE_ADDR *pc); 96130803Smarcel 97130803Smarcel 98130803Smarcel/* Structure describing a frame state. */ 99130803Smarcel 100130803Smarcelstruct dwarf2_frame_state 101130803Smarcel{ 102130803Smarcel /* Each register save state can be described in terms of a CFA slot, 103130803Smarcel another register, or a location expression. */ 104130803Smarcel struct dwarf2_frame_state_reg_info 105130803Smarcel { 106130803Smarcel struct dwarf2_frame_state_reg *reg; 107130803Smarcel int num_regs; 108130803Smarcel 109130803Smarcel /* Used to implement DW_CFA_remember_state. */ 110130803Smarcel struct dwarf2_frame_state_reg_info *prev; 111130803Smarcel } regs; 112130803Smarcel 113130803Smarcel LONGEST cfa_offset; 114130803Smarcel ULONGEST cfa_reg; 115130803Smarcel unsigned char *cfa_exp; 116130803Smarcel enum { 117130803Smarcel CFA_UNSET, 118130803Smarcel CFA_REG_OFFSET, 119130803Smarcel CFA_EXP 120130803Smarcel } cfa_how; 121130803Smarcel 122130803Smarcel /* The PC described by the current frame state. */ 123130803Smarcel CORE_ADDR pc; 124130803Smarcel 125130803Smarcel /* Initial register set from the CIE. 126130803Smarcel Used to implement DW_CFA_restore. */ 127130803Smarcel struct dwarf2_frame_state_reg_info initial; 128130803Smarcel 129130803Smarcel /* The information we care about from the CIE. */ 130130803Smarcel LONGEST data_align; 131130803Smarcel ULONGEST code_align; 132130803Smarcel ULONGEST retaddr_column; 133130803Smarcel}; 134130803Smarcel 135130803Smarcel/* Store the length the expression for the CFA in the `cfa_reg' field, 136130803Smarcel which is unused in that case. */ 137130803Smarcel#define cfa_exp_len cfa_reg 138130803Smarcel 139130803Smarcel/* Assert that the register set RS is large enough to store NUM_REGS 140130803Smarcel columns. If necessary, enlarge the register set. */ 141130803Smarcel 142130803Smarcelstatic void 143130803Smarceldwarf2_frame_state_alloc_regs (struct dwarf2_frame_state_reg_info *rs, 144130803Smarcel int num_regs) 145130803Smarcel{ 146130803Smarcel size_t size = sizeof (struct dwarf2_frame_state_reg); 147130803Smarcel 148130803Smarcel if (num_regs <= rs->num_regs) 149130803Smarcel return; 150130803Smarcel 151130803Smarcel rs->reg = (struct dwarf2_frame_state_reg *) 152130803Smarcel xrealloc (rs->reg, num_regs * size); 153130803Smarcel 154130803Smarcel /* Initialize newly allocated registers. */ 155130803Smarcel memset (rs->reg + rs->num_regs, 0, (num_regs - rs->num_regs) * size); 156130803Smarcel rs->num_regs = num_regs; 157130803Smarcel} 158130803Smarcel 159130803Smarcel/* Copy the register columns in register set RS into newly allocated 160130803Smarcel memory and return a pointer to this newly created copy. */ 161130803Smarcel 162130803Smarcelstatic struct dwarf2_frame_state_reg * 163130803Smarceldwarf2_frame_state_copy_regs (struct dwarf2_frame_state_reg_info *rs) 164130803Smarcel{ 165130803Smarcel size_t size = rs->num_regs * sizeof (struct dwarf2_frame_state_reg_info); 166130803Smarcel struct dwarf2_frame_state_reg *reg; 167130803Smarcel 168130803Smarcel reg = (struct dwarf2_frame_state_reg *) xmalloc (size); 169130803Smarcel memcpy (reg, rs->reg, size); 170130803Smarcel 171130803Smarcel return reg; 172130803Smarcel} 173130803Smarcel 174130803Smarcel/* Release the memory allocated to register set RS. */ 175130803Smarcel 176130803Smarcelstatic void 177130803Smarceldwarf2_frame_state_free_regs (struct dwarf2_frame_state_reg_info *rs) 178130803Smarcel{ 179130803Smarcel if (rs) 180130803Smarcel { 181130803Smarcel dwarf2_frame_state_free_regs (rs->prev); 182130803Smarcel 183130803Smarcel xfree (rs->reg); 184130803Smarcel xfree (rs); 185130803Smarcel } 186130803Smarcel} 187130803Smarcel 188130803Smarcel/* Release the memory allocated to the frame state FS. */ 189130803Smarcel 190130803Smarcelstatic void 191130803Smarceldwarf2_frame_state_free (void *p) 192130803Smarcel{ 193130803Smarcel struct dwarf2_frame_state *fs = p; 194130803Smarcel 195130803Smarcel dwarf2_frame_state_free_regs (fs->initial.prev); 196130803Smarcel dwarf2_frame_state_free_regs (fs->regs.prev); 197130803Smarcel xfree (fs->initial.reg); 198130803Smarcel xfree (fs->regs.reg); 199130803Smarcel xfree (fs); 200130803Smarcel} 201130803Smarcel 202130803Smarcel 203130803Smarcel/* Helper functions for execute_stack_op. */ 204130803Smarcel 205130803Smarcelstatic CORE_ADDR 206130803Smarcelread_reg (void *baton, int reg) 207130803Smarcel{ 208130803Smarcel struct frame_info *next_frame = (struct frame_info *) baton; 209130803Smarcel struct gdbarch *gdbarch = get_frame_arch (next_frame); 210130803Smarcel int regnum; 211130803Smarcel char *buf; 212130803Smarcel 213130803Smarcel regnum = DWARF2_REG_TO_REGNUM (reg); 214130803Smarcel 215130803Smarcel buf = (char *) alloca (register_size (gdbarch, regnum)); 216130803Smarcel frame_unwind_register (next_frame, regnum, buf); 217130803Smarcel return extract_typed_address (buf, builtin_type_void_data_ptr); 218130803Smarcel} 219130803Smarcel 220130803Smarcelstatic void 221130803Smarcelread_mem (void *baton, char *buf, CORE_ADDR addr, size_t len) 222130803Smarcel{ 223130803Smarcel read_memory (addr, buf, len); 224130803Smarcel} 225130803Smarcel 226130803Smarcelstatic void 227130803Smarcelno_get_frame_base (void *baton, unsigned char **start, size_t *length) 228130803Smarcel{ 229130803Smarcel internal_error (__FILE__, __LINE__, 230130803Smarcel "Support for DW_OP_fbreg is unimplemented"); 231130803Smarcel} 232130803Smarcel 233130803Smarcelstatic CORE_ADDR 234130803Smarcelno_get_tls_address (void *baton, CORE_ADDR offset) 235130803Smarcel{ 236130803Smarcel internal_error (__FILE__, __LINE__, 237130803Smarcel "Support for DW_OP_GNU_push_tls_address is unimplemented"); 238130803Smarcel} 239130803Smarcel 240130803Smarcelstatic CORE_ADDR 241130803Smarcelexecute_stack_op (unsigned char *exp, ULONGEST len, 242130803Smarcel struct frame_info *next_frame, CORE_ADDR initial) 243130803Smarcel{ 244130803Smarcel struct dwarf_expr_context *ctx; 245130803Smarcel CORE_ADDR result; 246130803Smarcel 247130803Smarcel ctx = new_dwarf_expr_context (); 248130803Smarcel ctx->baton = next_frame; 249130803Smarcel ctx->read_reg = read_reg; 250130803Smarcel ctx->read_mem = read_mem; 251130803Smarcel ctx->get_frame_base = no_get_frame_base; 252130803Smarcel ctx->get_tls_address = no_get_tls_address; 253130803Smarcel 254130803Smarcel dwarf_expr_push (ctx, initial); 255130803Smarcel dwarf_expr_eval (ctx, exp, len); 256130803Smarcel result = dwarf_expr_fetch (ctx, 0); 257130803Smarcel 258130803Smarcel if (ctx->in_reg) 259130803Smarcel result = read_reg (next_frame, result); 260130803Smarcel 261130803Smarcel free_dwarf_expr_context (ctx); 262130803Smarcel 263130803Smarcel return result; 264130803Smarcel} 265130803Smarcel 266130803Smarcel 267130803Smarcelstatic void 268130803Smarcelexecute_cfa_program (unsigned char *insn_ptr, unsigned char *insn_end, 269130803Smarcel struct frame_info *next_frame, 270130803Smarcel struct dwarf2_frame_state *fs) 271130803Smarcel{ 272130803Smarcel CORE_ADDR pc = frame_pc_unwind (next_frame); 273130803Smarcel int bytes_read; 274130803Smarcel 275130803Smarcel while (insn_ptr < insn_end && fs->pc <= pc) 276130803Smarcel { 277130803Smarcel unsigned char insn = *insn_ptr++; 278130803Smarcel ULONGEST utmp, reg; 279130803Smarcel LONGEST offset; 280130803Smarcel 281130803Smarcel if ((insn & 0xc0) == DW_CFA_advance_loc) 282130803Smarcel fs->pc += (insn & 0x3f) * fs->code_align; 283130803Smarcel else if ((insn & 0xc0) == DW_CFA_offset) 284130803Smarcel { 285130803Smarcel reg = insn & 0x3f; 286130803Smarcel insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp); 287130803Smarcel offset = utmp * fs->data_align; 288130803Smarcel dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1); 289130803Smarcel fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET; 290130803Smarcel fs->regs.reg[reg].loc.offset = offset; 291130803Smarcel } 292130803Smarcel else if ((insn & 0xc0) == DW_CFA_restore) 293130803Smarcel { 294130803Smarcel gdb_assert (fs->initial.reg); 295130803Smarcel reg = insn & 0x3f; 296130803Smarcel dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1); 297130803Smarcel fs->regs.reg[reg] = fs->initial.reg[reg]; 298130803Smarcel } 299130803Smarcel else 300130803Smarcel { 301130803Smarcel switch (insn) 302130803Smarcel { 303130803Smarcel case DW_CFA_set_loc: 304130803Smarcel fs->pc = dwarf2_read_address (insn_ptr, insn_end, &bytes_read); 305130803Smarcel insn_ptr += bytes_read; 306130803Smarcel break; 307130803Smarcel 308130803Smarcel case DW_CFA_advance_loc1: 309130803Smarcel utmp = extract_unsigned_integer (insn_ptr, 1); 310130803Smarcel fs->pc += utmp * fs->code_align; 311130803Smarcel insn_ptr++; 312130803Smarcel break; 313130803Smarcel case DW_CFA_advance_loc2: 314130803Smarcel utmp = extract_unsigned_integer (insn_ptr, 2); 315130803Smarcel fs->pc += utmp * fs->code_align; 316130803Smarcel insn_ptr += 2; 317130803Smarcel break; 318130803Smarcel case DW_CFA_advance_loc4: 319130803Smarcel utmp = extract_unsigned_integer (insn_ptr, 4); 320130803Smarcel fs->pc += utmp * fs->code_align; 321130803Smarcel insn_ptr += 4; 322130803Smarcel break; 323130803Smarcel 324130803Smarcel case DW_CFA_offset_extended: 325130803Smarcel insn_ptr = read_uleb128 (insn_ptr, insn_end, ®); 326130803Smarcel insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp); 327130803Smarcel offset = utmp * fs->data_align; 328130803Smarcel dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1); 329130803Smarcel fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET; 330130803Smarcel fs->regs.reg[reg].loc.offset = offset; 331130803Smarcel break; 332130803Smarcel 333130803Smarcel case DW_CFA_restore_extended: 334130803Smarcel gdb_assert (fs->initial.reg); 335130803Smarcel insn_ptr = read_uleb128 (insn_ptr, insn_end, ®); 336130803Smarcel dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1); 337130803Smarcel fs->regs.reg[reg] = fs->initial.reg[reg]; 338130803Smarcel break; 339130803Smarcel 340130803Smarcel case DW_CFA_undefined: 341130803Smarcel insn_ptr = read_uleb128 (insn_ptr, insn_end, ®); 342130803Smarcel dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1); 343130803Smarcel fs->regs.reg[reg].how = DWARF2_FRAME_REG_UNDEFINED; 344130803Smarcel break; 345130803Smarcel 346130803Smarcel case DW_CFA_same_value: 347130803Smarcel insn_ptr = read_uleb128 (insn_ptr, insn_end, ®); 348130803Smarcel dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1); 349130803Smarcel fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAME_VALUE; 350130803Smarcel break; 351130803Smarcel 352130803Smarcel case DW_CFA_register: 353130803Smarcel insn_ptr = read_uleb128 (insn_ptr, insn_end, ®); 354130803Smarcel insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp); 355130803Smarcel dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1); 356130803Smarcel fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_REG; 357130803Smarcel fs->regs.reg[reg].loc.reg = utmp; 358130803Smarcel break; 359130803Smarcel 360130803Smarcel case DW_CFA_remember_state: 361130803Smarcel { 362130803Smarcel struct dwarf2_frame_state_reg_info *new_rs; 363130803Smarcel 364130803Smarcel new_rs = XMALLOC (struct dwarf2_frame_state_reg_info); 365130803Smarcel *new_rs = fs->regs; 366130803Smarcel fs->regs.reg = dwarf2_frame_state_copy_regs (&fs->regs); 367130803Smarcel fs->regs.prev = new_rs; 368130803Smarcel } 369130803Smarcel break; 370130803Smarcel 371130803Smarcel case DW_CFA_restore_state: 372130803Smarcel { 373130803Smarcel struct dwarf2_frame_state_reg_info *old_rs = fs->regs.prev; 374130803Smarcel 375130803Smarcel gdb_assert (old_rs); 376130803Smarcel 377130803Smarcel xfree (fs->regs.reg); 378130803Smarcel fs->regs = *old_rs; 379130803Smarcel xfree (old_rs); 380130803Smarcel } 381130803Smarcel break; 382130803Smarcel 383130803Smarcel case DW_CFA_def_cfa: 384130803Smarcel insn_ptr = read_uleb128 (insn_ptr, insn_end, &fs->cfa_reg); 385130803Smarcel insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp); 386130803Smarcel fs->cfa_offset = utmp; 387130803Smarcel fs->cfa_how = CFA_REG_OFFSET; 388130803Smarcel break; 389130803Smarcel 390130803Smarcel case DW_CFA_def_cfa_register: 391130803Smarcel insn_ptr = read_uleb128 (insn_ptr, insn_end, &fs->cfa_reg); 392130803Smarcel fs->cfa_how = CFA_REG_OFFSET; 393130803Smarcel break; 394130803Smarcel 395130803Smarcel case DW_CFA_def_cfa_offset: 396130803Smarcel insn_ptr = read_uleb128 (insn_ptr, insn_end, &fs->cfa_offset); 397130803Smarcel /* cfa_how deliberately not set. */ 398130803Smarcel break; 399130803Smarcel 400130803Smarcel case DW_CFA_nop: 401130803Smarcel break; 402130803Smarcel 403130803Smarcel case DW_CFA_def_cfa_expression: 404130803Smarcel insn_ptr = read_uleb128 (insn_ptr, insn_end, &fs->cfa_exp_len); 405130803Smarcel fs->cfa_exp = insn_ptr; 406130803Smarcel fs->cfa_how = CFA_EXP; 407130803Smarcel insn_ptr += fs->cfa_exp_len; 408130803Smarcel break; 409130803Smarcel 410130803Smarcel case DW_CFA_expression: 411130803Smarcel insn_ptr = read_uleb128 (insn_ptr, insn_end, ®); 412130803Smarcel dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1); 413130803Smarcel insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp); 414130803Smarcel fs->regs.reg[reg].loc.exp = insn_ptr; 415130803Smarcel fs->regs.reg[reg].exp_len = utmp; 416130803Smarcel fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_EXP; 417130803Smarcel insn_ptr += utmp; 418130803Smarcel break; 419130803Smarcel 420130803Smarcel case DW_CFA_offset_extended_sf: 421130803Smarcel insn_ptr = read_uleb128 (insn_ptr, insn_end, ®); 422130803Smarcel insn_ptr = read_sleb128 (insn_ptr, insn_end, &offset); 423130803Smarcel offset += fs->data_align; 424130803Smarcel dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1); 425130803Smarcel fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET; 426130803Smarcel fs->regs.reg[reg].loc.offset = offset; 427130803Smarcel break; 428130803Smarcel 429130803Smarcel case DW_CFA_def_cfa_sf: 430130803Smarcel insn_ptr = read_uleb128 (insn_ptr, insn_end, &fs->cfa_reg); 431130803Smarcel insn_ptr = read_sleb128 (insn_ptr, insn_end, &offset); 432130803Smarcel fs->cfa_offset = offset * fs->data_align; 433130803Smarcel fs->cfa_how = CFA_REG_OFFSET; 434130803Smarcel break; 435130803Smarcel 436130803Smarcel case DW_CFA_def_cfa_offset_sf: 437130803Smarcel insn_ptr = read_sleb128 (insn_ptr, insn_end, &offset); 438130803Smarcel fs->cfa_offset = offset * fs->data_align; 439130803Smarcel /* cfa_how deliberately not set. */ 440130803Smarcel break; 441130803Smarcel 442130803Smarcel case DW_CFA_GNU_args_size: 443130803Smarcel /* Ignored. */ 444130803Smarcel insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp); 445130803Smarcel break; 446130803Smarcel 447130803Smarcel default: 448130803Smarcel internal_error (__FILE__, __LINE__, "Unknown CFI encountered."); 449130803Smarcel } 450130803Smarcel } 451130803Smarcel } 452130803Smarcel 453130803Smarcel /* Don't allow remember/restore between CIE and FDE programs. */ 454130803Smarcel dwarf2_frame_state_free_regs (fs->regs.prev); 455130803Smarcel fs->regs.prev = NULL; 456130803Smarcel} 457130803Smarcel 458130803Smarcel 459130803Smarcel/* Architecture-specific operations. */ 460130803Smarcel 461130803Smarcel/* Per-architecture data key. */ 462130803Smarcelstatic struct gdbarch_data *dwarf2_frame_data; 463130803Smarcel 464130803Smarcelstruct dwarf2_frame_ops 465130803Smarcel{ 466130803Smarcel /* Pre-initialize the register state REG for register REGNUM. */ 467130803Smarcel void (*init_reg) (struct gdbarch *, int, struct dwarf2_frame_state_reg *); 468130803Smarcel}; 469130803Smarcel 470130803Smarcel/* Default architecture-specific register state initialization 471130803Smarcel function. */ 472130803Smarcel 473130803Smarcelstatic void 474130803Smarceldwarf2_frame_default_init_reg (struct gdbarch *gdbarch, int regnum, 475130803Smarcel struct dwarf2_frame_state_reg *reg) 476130803Smarcel{ 477130803Smarcel /* If we have a register that acts as a program counter, mark it as 478130803Smarcel a destination for the return address. If we have a register that 479130803Smarcel serves as the stack pointer, arrange for it to be filled with the 480130803Smarcel call frame address (CFA). The other registers are marked as 481130803Smarcel unspecified. 482130803Smarcel 483130803Smarcel We copy the return address to the program counter, since many 484130803Smarcel parts in GDB assume that it is possible to get the return address 485130803Smarcel by unwinding the program counter register. However, on ISA's 486130803Smarcel with a dedicated return address register, the CFI usually only 487130803Smarcel contains information to unwind that return address register. 488130803Smarcel 489130803Smarcel The reason we're treating the stack pointer special here is 490130803Smarcel because in many cases GCC doesn't emit CFI for the stack pointer 491130803Smarcel and implicitly assumes that it is equal to the CFA. This makes 492130803Smarcel some sense since the DWARF specification (version 3, draft 8, 493130803Smarcel p. 102) says that: 494130803Smarcel 495130803Smarcel "Typically, the CFA is defined to be the value of the stack 496130803Smarcel pointer at the call site in the previous frame (which may be 497130803Smarcel different from its value on entry to the current frame)." 498130803Smarcel 499130803Smarcel However, this isn't true for all platforms supported by GCC 500130803Smarcel (e.g. IBM S/390 and zSeries). Those architectures should provide 501130803Smarcel their own architecture-specific initialization function. */ 502130803Smarcel 503130803Smarcel if (regnum == PC_REGNUM) 504130803Smarcel reg->how = DWARF2_FRAME_REG_RA; 505130803Smarcel else if (regnum == SP_REGNUM) 506130803Smarcel reg->how = DWARF2_FRAME_REG_CFA; 507130803Smarcel} 508130803Smarcel 509130803Smarcel/* Return a default for the architecture-specific operations. */ 510130803Smarcel 511130803Smarcelstatic void * 512130803Smarceldwarf2_frame_init (struct gdbarch *gdbarch) 513130803Smarcel{ 514130803Smarcel struct dwarf2_frame_ops *ops; 515130803Smarcel 516130803Smarcel ops = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct dwarf2_frame_ops); 517130803Smarcel ops->init_reg = dwarf2_frame_default_init_reg; 518130803Smarcel return ops; 519130803Smarcel} 520130803Smarcel 521130803Smarcelstatic struct dwarf2_frame_ops * 522130803Smarceldwarf2_frame_ops (struct gdbarch *gdbarch) 523130803Smarcel{ 524130803Smarcel struct dwarf2_frame_ops *ops = gdbarch_data (gdbarch, dwarf2_frame_data); 525130803Smarcel if (ops == NULL) 526130803Smarcel { 527130803Smarcel /* ULGH, called during architecture initialization. Patch 528130803Smarcel things up. */ 529130803Smarcel ops = dwarf2_frame_init (gdbarch); 530130803Smarcel set_gdbarch_data (gdbarch, dwarf2_frame_data, ops); 531130803Smarcel } 532130803Smarcel return ops; 533130803Smarcel} 534130803Smarcel 535130803Smarcel/* Set the architecture-specific register state initialization 536130803Smarcel function for GDBARCH to INIT_REG. */ 537130803Smarcel 538130803Smarcelvoid 539130803Smarceldwarf2_frame_set_init_reg (struct gdbarch *gdbarch, 540130803Smarcel void (*init_reg) (struct gdbarch *, int, 541130803Smarcel struct dwarf2_frame_state_reg *)) 542130803Smarcel{ 543130803Smarcel struct dwarf2_frame_ops *ops; 544130803Smarcel 545130803Smarcel ops = dwarf2_frame_ops (gdbarch); 546130803Smarcel ops->init_reg = init_reg; 547130803Smarcel} 548130803Smarcel 549130803Smarcel/* Pre-initialize the register state REG for register REGNUM. */ 550130803Smarcel 551130803Smarcelstatic void 552130803Smarceldwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum, 553130803Smarcel struct dwarf2_frame_state_reg *reg) 554130803Smarcel{ 555130803Smarcel struct dwarf2_frame_ops *ops; 556130803Smarcel 557130803Smarcel ops = dwarf2_frame_ops (gdbarch); 558130803Smarcel ops->init_reg (gdbarch, regnum, reg); 559130803Smarcel} 560130803Smarcel 561130803Smarcel 562130803Smarcelstruct dwarf2_frame_cache 563130803Smarcel{ 564130803Smarcel /* DWARF Call Frame Address. */ 565130803Smarcel CORE_ADDR cfa; 566130803Smarcel 567130803Smarcel /* Saved registers, indexed by GDB register number, not by DWARF 568130803Smarcel register number. */ 569130803Smarcel struct dwarf2_frame_state_reg *reg; 570130803Smarcel}; 571130803Smarcel 572130803Smarcelstatic struct dwarf2_frame_cache * 573130803Smarceldwarf2_frame_cache (struct frame_info *next_frame, void **this_cache) 574130803Smarcel{ 575130803Smarcel struct cleanup *old_chain; 576130803Smarcel struct gdbarch *gdbarch = get_frame_arch (next_frame); 577130803Smarcel const int num_regs = NUM_REGS + NUM_PSEUDO_REGS; 578130803Smarcel struct dwarf2_frame_cache *cache; 579130803Smarcel struct dwarf2_frame_state *fs; 580130803Smarcel struct dwarf2_fde *fde; 581130803Smarcel 582130803Smarcel if (*this_cache) 583130803Smarcel return *this_cache; 584130803Smarcel 585130803Smarcel /* Allocate a new cache. */ 586130803Smarcel cache = FRAME_OBSTACK_ZALLOC (struct dwarf2_frame_cache); 587130803Smarcel cache->reg = FRAME_OBSTACK_CALLOC (num_regs, struct dwarf2_frame_state_reg); 588130803Smarcel 589130803Smarcel /* Allocate and initialize the frame state. */ 590130803Smarcel fs = XMALLOC (struct dwarf2_frame_state); 591130803Smarcel memset (fs, 0, sizeof (struct dwarf2_frame_state)); 592130803Smarcel old_chain = make_cleanup (dwarf2_frame_state_free, fs); 593130803Smarcel 594130803Smarcel /* Unwind the PC. 595130803Smarcel 596130803Smarcel Note that if NEXT_FRAME is never supposed to return (i.e. a call 597130803Smarcel to abort), the compiler might optimize away the instruction at 598130803Smarcel NEXT_FRAME's return address. As a result the return address will 599130803Smarcel point at some random instruction, and the CFI for that 600130803Smarcel instruction is probably worthless to us. GCC's unwinder solves 601130803Smarcel this problem by substracting 1 from the return address to get an 602130803Smarcel address in the middle of a presumed call instruction (or the 603130803Smarcel instruction in the associated delay slot). This should only be 604130803Smarcel done for "normal" frames and not for resume-type frames (signal 605130803Smarcel handlers, sentinel frames, dummy frames). The function 606130803Smarcel frame_unwind_address_in_block does just this. It's not clear how 607130803Smarcel reliable the method is though; there is the potential for the 608130803Smarcel register state pre-call being different to that on return. */ 609130803Smarcel fs->pc = frame_unwind_address_in_block (next_frame); 610130803Smarcel 611130803Smarcel /* Find the correct FDE. */ 612130803Smarcel fde = dwarf2_frame_find_fde (&fs->pc); 613130803Smarcel gdb_assert (fde != NULL); 614130803Smarcel 615130803Smarcel /* Extract any interesting information from the CIE. */ 616130803Smarcel fs->data_align = fde->cie->data_alignment_factor; 617130803Smarcel fs->code_align = fde->cie->code_alignment_factor; 618130803Smarcel fs->retaddr_column = fde->cie->return_address_register; 619130803Smarcel 620130803Smarcel /* First decode all the insns in the CIE. */ 621130803Smarcel execute_cfa_program (fde->cie->initial_instructions, 622130803Smarcel fde->cie->end, next_frame, fs); 623130803Smarcel 624130803Smarcel /* Save the initialized register set. */ 625130803Smarcel fs->initial = fs->regs; 626130803Smarcel fs->initial.reg = dwarf2_frame_state_copy_regs (&fs->regs); 627130803Smarcel 628130803Smarcel /* Then decode the insns in the FDE up to our target PC. */ 629130803Smarcel execute_cfa_program (fde->instructions, fde->end, next_frame, fs); 630130803Smarcel 631130803Smarcel /* Caclulate the CFA. */ 632130803Smarcel switch (fs->cfa_how) 633130803Smarcel { 634130803Smarcel case CFA_REG_OFFSET: 635130803Smarcel cache->cfa = read_reg (next_frame, fs->cfa_reg); 636130803Smarcel cache->cfa += fs->cfa_offset; 637130803Smarcel break; 638130803Smarcel 639130803Smarcel case CFA_EXP: 640130803Smarcel cache->cfa = 641130803Smarcel execute_stack_op (fs->cfa_exp, fs->cfa_exp_len, next_frame, 0); 642130803Smarcel break; 643130803Smarcel 644130803Smarcel default: 645130803Smarcel internal_error (__FILE__, __LINE__, "Unknown CFA rule."); 646130803Smarcel } 647130803Smarcel 648130803Smarcel /* Initialize the register state. */ 649130803Smarcel { 650130803Smarcel int regnum; 651130803Smarcel 652130803Smarcel for (regnum = 0; regnum < num_regs; regnum++) 653130803Smarcel dwarf2_frame_init_reg (gdbarch, regnum, &cache->reg[regnum]); 654130803Smarcel } 655130803Smarcel 656130803Smarcel /* Go through the DWARF2 CFI generated table and save its register 657130803Smarcel location information in the cache. Note that we don't skip the 658130803Smarcel return address column; it's perfectly all right for it to 659130803Smarcel correspond to a real register. If it doesn't correspond to a 660130803Smarcel real register, or if we shouldn't treat it as such, 661130803Smarcel DWARF2_REG_TO_REGNUM should be defined to return a number outside 662130803Smarcel the range [0, NUM_REGS). */ 663130803Smarcel { 664130803Smarcel int column; /* CFI speak for "register number". */ 665130803Smarcel 666130803Smarcel for (column = 0; column < fs->regs.num_regs; column++) 667130803Smarcel { 668130803Smarcel /* Use the GDB register number as the destination index. */ 669130803Smarcel int regnum = DWARF2_REG_TO_REGNUM (column); 670130803Smarcel 671130803Smarcel /* If there's no corresponding GDB register, ignore it. */ 672130803Smarcel if (regnum < 0 || regnum >= num_regs) 673130803Smarcel continue; 674130803Smarcel 675130803Smarcel /* NOTE: cagney/2003-09-05: CFI should specify the disposition 676130803Smarcel of all debug info registers. If it doesn't, complain (but 677130803Smarcel not too loudly). It turns out that GCC assumes that an 678130803Smarcel unspecified register implies "same value" when CFI (draft 679130803Smarcel 7) specifies nothing at all. Such a register could equally 680130803Smarcel be interpreted as "undefined". Also note that this check 681130803Smarcel isn't sufficient; it only checks that all registers in the 682130803Smarcel range [0 .. max column] are specified, and won't detect 683130803Smarcel problems when a debug info register falls outside of the 684130803Smarcel table. We need a way of iterating through all the valid 685130803Smarcel DWARF2 register numbers. */ 686130803Smarcel if (fs->regs.reg[column].how == DWARF2_FRAME_REG_UNSPECIFIED) 687130803Smarcel complaint (&symfile_complaints, 688130803Smarcel "Incomplete CFI data; unspecified registers at 0x%s", 689130803Smarcel paddr (fs->pc)); 690130803Smarcel else 691130803Smarcel cache->reg[regnum] = fs->regs.reg[column]; 692130803Smarcel } 693130803Smarcel } 694130803Smarcel 695130803Smarcel /* Eliminate any DWARF2_FRAME_REG_RA rules. */ 696130803Smarcel { 697130803Smarcel int regnum; 698130803Smarcel 699130803Smarcel for (regnum = 0; regnum < num_regs; regnum++) 700130803Smarcel { 701130803Smarcel if (cache->reg[regnum].how == DWARF2_FRAME_REG_RA) 702130803Smarcel { 703130803Smarcel struct dwarf2_frame_state_reg *retaddr_reg = 704130803Smarcel &fs->regs.reg[fs->retaddr_column]; 705130803Smarcel 706130803Smarcel /* It seems rather bizarre to specify an "empty" column as 707130803Smarcel the return adress column. However, this is exactly 708130803Smarcel what GCC does on some targets. It turns out that GCC 709130803Smarcel assumes that the return address can be found in the 710130803Smarcel register corresponding to the return address column. 711130803Smarcel Incidentally, that's how should treat a return address 712130803Smarcel column specifying "same value" too. */ 713130803Smarcel if (fs->retaddr_column < fs->regs.num_regs 714130803Smarcel && retaddr_reg->how != DWARF2_FRAME_REG_UNSPECIFIED 715130803Smarcel && retaddr_reg->how != DWARF2_FRAME_REG_SAME_VALUE) 716130803Smarcel cache->reg[regnum] = *retaddr_reg; 717130803Smarcel else 718130803Smarcel { 719130803Smarcel cache->reg[regnum].loc.reg = fs->retaddr_column; 720130803Smarcel cache->reg[regnum].how = DWARF2_FRAME_REG_SAVED_REG; 721130803Smarcel } 722130803Smarcel } 723130803Smarcel } 724130803Smarcel } 725130803Smarcel 726130803Smarcel do_cleanups (old_chain); 727130803Smarcel 728130803Smarcel *this_cache = cache; 729130803Smarcel return cache; 730130803Smarcel} 731130803Smarcel 732130803Smarcelstatic void 733130803Smarceldwarf2_frame_this_id (struct frame_info *next_frame, void **this_cache, 734130803Smarcel struct frame_id *this_id) 735130803Smarcel{ 736130803Smarcel struct dwarf2_frame_cache *cache = 737130803Smarcel dwarf2_frame_cache (next_frame, this_cache); 738130803Smarcel 739130803Smarcel (*this_id) = frame_id_build (cache->cfa, frame_func_unwind (next_frame)); 740130803Smarcel} 741130803Smarcel 742130803Smarcelstatic void 743130803Smarceldwarf2_frame_prev_register (struct frame_info *next_frame, void **this_cache, 744130803Smarcel int regnum, int *optimizedp, 745130803Smarcel enum lval_type *lvalp, CORE_ADDR *addrp, 746130803Smarcel int *realnump, void *valuep) 747130803Smarcel{ 748130803Smarcel struct gdbarch *gdbarch = get_frame_arch (next_frame); 749130803Smarcel struct dwarf2_frame_cache *cache = 750130803Smarcel dwarf2_frame_cache (next_frame, this_cache); 751130803Smarcel 752130803Smarcel switch (cache->reg[regnum].how) 753130803Smarcel { 754130803Smarcel case DWARF2_FRAME_REG_UNDEFINED: 755130803Smarcel /* If CFI explicitly specified that the value isn't defined, 756130803Smarcel mark it as optimized away; the value isn't available. */ 757130803Smarcel *optimizedp = 1; 758130803Smarcel *lvalp = not_lval; 759130803Smarcel *addrp = 0; 760130803Smarcel *realnump = -1; 761130803Smarcel if (valuep) 762130803Smarcel { 763130803Smarcel /* In some cases, for example %eflags on the i386, we have 764130803Smarcel to provide a sane value, even though this register wasn't 765130803Smarcel saved. Assume we can get it from NEXT_FRAME. */ 766130803Smarcel frame_unwind_register (next_frame, regnum, valuep); 767130803Smarcel } 768130803Smarcel break; 769130803Smarcel 770130803Smarcel case DWARF2_FRAME_REG_SAVED_OFFSET: 771130803Smarcel *optimizedp = 0; 772130803Smarcel *lvalp = lval_memory; 773130803Smarcel *addrp = cache->cfa + cache->reg[regnum].loc.offset; 774130803Smarcel *realnump = -1; 775130803Smarcel if (valuep) 776130803Smarcel { 777130803Smarcel /* Read the value in from memory. */ 778130803Smarcel read_memory (*addrp, valuep, register_size (gdbarch, regnum)); 779130803Smarcel } 780130803Smarcel break; 781130803Smarcel 782130803Smarcel case DWARF2_FRAME_REG_SAVED_REG: 783130803Smarcel regnum = DWARF2_REG_TO_REGNUM (cache->reg[regnum].loc.reg); 784130803Smarcel frame_register_unwind (next_frame, regnum, 785130803Smarcel optimizedp, lvalp, addrp, realnump, valuep); 786130803Smarcel break; 787130803Smarcel 788130803Smarcel case DWARF2_FRAME_REG_SAVED_EXP: 789130803Smarcel *optimizedp = 0; 790130803Smarcel *lvalp = lval_memory; 791130803Smarcel *addrp = execute_stack_op (cache->reg[regnum].loc.exp, 792130803Smarcel cache->reg[regnum].exp_len, 793130803Smarcel next_frame, cache->cfa); 794130803Smarcel *realnump = -1; 795130803Smarcel if (valuep) 796130803Smarcel { 797130803Smarcel /* Read the value in from memory. */ 798130803Smarcel read_memory (*addrp, valuep, register_size (gdbarch, regnum)); 799130803Smarcel } 800130803Smarcel break; 801130803Smarcel 802130803Smarcel case DWARF2_FRAME_REG_UNSPECIFIED: 803130803Smarcel /* GCC, in its infinite wisdom decided to not provide unwind 804130803Smarcel information for registers that are "same value". Since 805130803Smarcel DWARF2 (3 draft 7) doesn't define such behavior, said 806130803Smarcel registers are actually undefined (which is different to CFI 807130803Smarcel "undefined"). Code above issues a complaint about this. 808130803Smarcel Here just fudge the books, assume GCC, and that the value is 809130803Smarcel more inner on the stack. */ 810130803Smarcel frame_register_unwind (next_frame, regnum, 811130803Smarcel optimizedp, lvalp, addrp, realnump, valuep); 812130803Smarcel break; 813130803Smarcel 814130803Smarcel case DWARF2_FRAME_REG_SAME_VALUE: 815130803Smarcel frame_register_unwind (next_frame, regnum, 816130803Smarcel optimizedp, lvalp, addrp, realnump, valuep); 817130803Smarcel break; 818130803Smarcel 819130803Smarcel case DWARF2_FRAME_REG_CFA: 820130803Smarcel *optimizedp = 0; 821130803Smarcel *lvalp = not_lval; 822130803Smarcel *addrp = 0; 823130803Smarcel *realnump = -1; 824130803Smarcel if (valuep) 825130803Smarcel { 826130803Smarcel /* Store the value. */ 827130803Smarcel store_typed_address (valuep, builtin_type_void_data_ptr, cache->cfa); 828130803Smarcel } 829130803Smarcel break; 830130803Smarcel 831130803Smarcel default: 832130803Smarcel internal_error (__FILE__, __LINE__, "Unknown register rule."); 833130803Smarcel } 834130803Smarcel} 835130803Smarcel 836130803Smarcelstatic const struct frame_unwind dwarf2_frame_unwind = 837130803Smarcel{ 838130803Smarcel NORMAL_FRAME, 839130803Smarcel dwarf2_frame_this_id, 840130803Smarcel dwarf2_frame_prev_register 841130803Smarcel}; 842130803Smarcel 843130803Smarcelconst struct frame_unwind * 844130803Smarceldwarf2_frame_sniffer (struct frame_info *next_frame) 845130803Smarcel{ 846130803Smarcel /* Grab an address that is guarenteed to reside somewhere within the 847130803Smarcel function. frame_pc_unwind(), for a no-return next function, can 848130803Smarcel end up returning something past the end of this function's body. */ 849130803Smarcel CORE_ADDR block_addr = frame_unwind_address_in_block (next_frame); 850130803Smarcel if (dwarf2_frame_find_fde (&block_addr)) 851130803Smarcel return &dwarf2_frame_unwind; 852130803Smarcel 853130803Smarcel return NULL; 854130803Smarcel} 855130803Smarcel 856130803Smarcel 857130803Smarcel/* There is no explicitly defined relationship between the CFA and the 858130803Smarcel location of frame's local variables and arguments/parameters. 859130803Smarcel Therefore, frame base methods on this page should probably only be 860130803Smarcel used as a last resort, just to avoid printing total garbage as a 861130803Smarcel response to the "info frame" command. */ 862130803Smarcel 863130803Smarcelstatic CORE_ADDR 864130803Smarceldwarf2_frame_base_address (struct frame_info *next_frame, void **this_cache) 865130803Smarcel{ 866130803Smarcel struct dwarf2_frame_cache *cache = 867130803Smarcel dwarf2_frame_cache (next_frame, this_cache); 868130803Smarcel 869130803Smarcel return cache->cfa; 870130803Smarcel} 871130803Smarcel 872130803Smarcelstatic const struct frame_base dwarf2_frame_base = 873130803Smarcel{ 874130803Smarcel &dwarf2_frame_unwind, 875130803Smarcel dwarf2_frame_base_address, 876130803Smarcel dwarf2_frame_base_address, 877130803Smarcel dwarf2_frame_base_address 878130803Smarcel}; 879130803Smarcel 880130803Smarcelconst struct frame_base * 881130803Smarceldwarf2_frame_base_sniffer (struct frame_info *next_frame) 882130803Smarcel{ 883130803Smarcel CORE_ADDR pc = frame_pc_unwind (next_frame); 884130803Smarcel if (dwarf2_frame_find_fde (&pc)) 885130803Smarcel return &dwarf2_frame_base; 886130803Smarcel 887130803Smarcel return NULL; 888130803Smarcel} 889130803Smarcel 890130803Smarcel/* A minimal decoding of DWARF2 compilation units. We only decode 891130803Smarcel what's needed to get to the call frame information. */ 892130803Smarcel 893130803Smarcelstruct comp_unit 894130803Smarcel{ 895130803Smarcel /* Keep the bfd convenient. */ 896130803Smarcel bfd *abfd; 897130803Smarcel 898130803Smarcel struct objfile *objfile; 899130803Smarcel 900130803Smarcel /* Linked list of CIEs for this object. */ 901130803Smarcel struct dwarf2_cie *cie; 902130803Smarcel 903130803Smarcel /* Address size for this unit - from unit header. */ 904130803Smarcel unsigned char addr_size; 905130803Smarcel 906130803Smarcel /* Pointer to the .debug_frame section loaded into memory. */ 907130803Smarcel char *dwarf_frame_buffer; 908130803Smarcel 909130803Smarcel /* Length of the loaded .debug_frame section. */ 910130803Smarcel unsigned long dwarf_frame_size; 911130803Smarcel 912130803Smarcel /* Pointer to the .debug_frame section. */ 913130803Smarcel asection *dwarf_frame_section; 914130803Smarcel 915130803Smarcel /* Base for DW_EH_PE_datarel encodings. */ 916130803Smarcel bfd_vma dbase; 917130803Smarcel 918130803Smarcel /* Base for DW_EH_PE_textrel encodings. */ 919130803Smarcel bfd_vma tbase; 920130803Smarcel}; 921130803Smarcel 922130803Smarcelconst struct objfile_data *dwarf2_frame_objfile_data; 923130803Smarcel 924130803Smarcelstatic unsigned int 925130803Smarcelread_1_byte (bfd *bfd, char *buf) 926130803Smarcel{ 927130803Smarcel return bfd_get_8 (abfd, (bfd_byte *) buf); 928130803Smarcel} 929130803Smarcel 930130803Smarcelstatic unsigned int 931130803Smarcelread_4_bytes (bfd *abfd, char *buf) 932130803Smarcel{ 933130803Smarcel return bfd_get_32 (abfd, (bfd_byte *) buf); 934130803Smarcel} 935130803Smarcel 936130803Smarcelstatic ULONGEST 937130803Smarcelread_8_bytes (bfd *abfd, char *buf) 938130803Smarcel{ 939130803Smarcel return bfd_get_64 (abfd, (bfd_byte *) buf); 940130803Smarcel} 941130803Smarcel 942130803Smarcelstatic ULONGEST 943130803Smarcelread_unsigned_leb128 (bfd *abfd, char *buf, unsigned int *bytes_read_ptr) 944130803Smarcel{ 945130803Smarcel ULONGEST result; 946130803Smarcel unsigned int num_read; 947130803Smarcel int shift; 948130803Smarcel unsigned char byte; 949130803Smarcel 950130803Smarcel result = 0; 951130803Smarcel shift = 0; 952130803Smarcel num_read = 0; 953130803Smarcel 954130803Smarcel do 955130803Smarcel { 956130803Smarcel byte = bfd_get_8 (abfd, (bfd_byte *) buf); 957130803Smarcel buf++; 958130803Smarcel num_read++; 959130803Smarcel result |= ((byte & 0x7f) << shift); 960130803Smarcel shift += 7; 961130803Smarcel } 962130803Smarcel while (byte & 0x80); 963130803Smarcel 964130803Smarcel *bytes_read_ptr = num_read; 965130803Smarcel 966130803Smarcel return result; 967130803Smarcel} 968130803Smarcel 969130803Smarcelstatic LONGEST 970130803Smarcelread_signed_leb128 (bfd *abfd, char *buf, unsigned int *bytes_read_ptr) 971130803Smarcel{ 972130803Smarcel LONGEST result; 973130803Smarcel int shift; 974130803Smarcel unsigned int num_read; 975130803Smarcel unsigned char byte; 976130803Smarcel 977130803Smarcel result = 0; 978130803Smarcel shift = 0; 979130803Smarcel num_read = 0; 980130803Smarcel 981130803Smarcel do 982130803Smarcel { 983130803Smarcel byte = bfd_get_8 (abfd, (bfd_byte *) buf); 984130803Smarcel buf++; 985130803Smarcel num_read++; 986130803Smarcel result |= ((byte & 0x7f) << shift); 987130803Smarcel shift += 7; 988130803Smarcel } 989130803Smarcel while (byte & 0x80); 990130803Smarcel 991130803Smarcel if ((shift < 32) && (byte & 0x40)) 992130803Smarcel result |= -(1 << shift); 993130803Smarcel 994130803Smarcel *bytes_read_ptr = num_read; 995130803Smarcel 996130803Smarcel return result; 997130803Smarcel} 998130803Smarcel 999130803Smarcelstatic ULONGEST 1000130803Smarcelread_initial_length (bfd *abfd, char *buf, unsigned int *bytes_read_ptr) 1001130803Smarcel{ 1002130803Smarcel LONGEST result; 1003130803Smarcel 1004130803Smarcel result = bfd_get_32 (abfd, (bfd_byte *) buf); 1005130803Smarcel if (result == 0xffffffff) 1006130803Smarcel { 1007130803Smarcel result = bfd_get_64 (abfd, (bfd_byte *) buf + 4); 1008130803Smarcel *bytes_read_ptr = 12; 1009130803Smarcel } 1010130803Smarcel else 1011130803Smarcel *bytes_read_ptr = 4; 1012130803Smarcel 1013130803Smarcel return result; 1014130803Smarcel} 1015130803Smarcel 1016130803Smarcel 1017130803Smarcel/* Pointer encoding helper functions. */ 1018130803Smarcel 1019130803Smarcel/* GCC supports exception handling based on DWARF2 CFI. However, for 1020130803Smarcel technical reasons, it encodes addresses in its FDE's in a different 1021130803Smarcel way. Several "pointer encodings" are supported. The encoding 1022130803Smarcel that's used for a particular FDE is determined by the 'R' 1023130803Smarcel augmentation in the associated CIE. The argument of this 1024130803Smarcel augmentation is a single byte. 1025130803Smarcel 1026130803Smarcel The address can be encoded as 2 bytes, 4 bytes, 8 bytes, or as a 1027130803Smarcel LEB128. This is encoded in bits 0, 1 and 2. Bit 3 encodes whether 1028130803Smarcel the address is signed or unsigned. Bits 4, 5 and 6 encode how the 1029130803Smarcel address should be interpreted (absolute, relative to the current 1030130803Smarcel position in the FDE, ...). Bit 7, indicates that the address 1031130803Smarcel should be dereferenced. */ 1032130803Smarcel 1033130803Smarcelstatic unsigned char 1034130803Smarcelencoding_for_size (unsigned int size) 1035130803Smarcel{ 1036130803Smarcel switch (size) 1037130803Smarcel { 1038130803Smarcel case 2: 1039130803Smarcel return DW_EH_PE_udata2; 1040130803Smarcel case 4: 1041130803Smarcel return DW_EH_PE_udata4; 1042130803Smarcel case 8: 1043130803Smarcel return DW_EH_PE_udata8; 1044130803Smarcel default: 1045130803Smarcel internal_error (__FILE__, __LINE__, "Unsupported address size"); 1046130803Smarcel } 1047130803Smarcel} 1048130803Smarcel 1049130803Smarcelstatic unsigned int 1050130803Smarcelsize_of_encoded_value (unsigned char encoding) 1051130803Smarcel{ 1052130803Smarcel if (encoding == DW_EH_PE_omit) 1053130803Smarcel return 0; 1054130803Smarcel 1055130803Smarcel switch (encoding & 0x07) 1056130803Smarcel { 1057130803Smarcel case DW_EH_PE_absptr: 1058130803Smarcel return TYPE_LENGTH (builtin_type_void_data_ptr); 1059130803Smarcel case DW_EH_PE_udata2: 1060130803Smarcel return 2; 1061130803Smarcel case DW_EH_PE_udata4: 1062130803Smarcel return 4; 1063130803Smarcel case DW_EH_PE_udata8: 1064130803Smarcel return 8; 1065130803Smarcel default: 1066130803Smarcel internal_error (__FILE__, __LINE__, "Invalid or unsupported encoding"); 1067130803Smarcel } 1068130803Smarcel} 1069130803Smarcel 1070130803Smarcelstatic CORE_ADDR 1071130803Smarcelread_encoded_value (struct comp_unit *unit, unsigned char encoding, 1072130803Smarcel char *buf, unsigned int *bytes_read_ptr) 1073130803Smarcel{ 1074130803Smarcel int ptr_len = size_of_encoded_value (DW_EH_PE_absptr); 1075130803Smarcel ptrdiff_t offset; 1076130803Smarcel CORE_ADDR base; 1077130803Smarcel 1078130803Smarcel /* GCC currently doesn't generate DW_EH_PE_indirect encodings for 1079130803Smarcel FDE's. */ 1080130803Smarcel if (encoding & DW_EH_PE_indirect) 1081130803Smarcel internal_error (__FILE__, __LINE__, 1082130803Smarcel "Unsupported encoding: DW_EH_PE_indirect"); 1083130803Smarcel 1084130803Smarcel *bytes_read_ptr = 0; 1085130803Smarcel 1086130803Smarcel switch (encoding & 0x70) 1087130803Smarcel { 1088130803Smarcel case DW_EH_PE_absptr: 1089130803Smarcel base = 0; 1090130803Smarcel break; 1091130803Smarcel case DW_EH_PE_pcrel: 1092130803Smarcel base = bfd_get_section_vma (unit->bfd, unit->dwarf_frame_section); 1093130803Smarcel base += (buf - unit->dwarf_frame_buffer); 1094130803Smarcel break; 1095130803Smarcel case DW_EH_PE_datarel: 1096130803Smarcel base = unit->dbase; 1097130803Smarcel break; 1098130803Smarcel case DW_EH_PE_textrel: 1099130803Smarcel base = unit->tbase; 1100130803Smarcel break; 1101130803Smarcel case DW_EH_PE_funcrel: 1102130803Smarcel /* FIXME: kettenis/20040501: For now just pretend 1103130803Smarcel DW_EH_PE_funcrel is equivalent to DW_EH_PE_absptr. For 1104130803Smarcel reading the initial location of an FDE it should be treated 1105130803Smarcel as such, and currently that's the only place where this code 1106130803Smarcel is used. */ 1107130803Smarcel base = 0; 1108130803Smarcel break; 1109130803Smarcel case DW_EH_PE_aligned: 1110130803Smarcel base = 0; 1111130803Smarcel offset = buf - unit->dwarf_frame_buffer; 1112130803Smarcel if ((offset % ptr_len) != 0) 1113130803Smarcel { 1114130803Smarcel *bytes_read_ptr = ptr_len - (offset % ptr_len); 1115130803Smarcel buf += *bytes_read_ptr; 1116130803Smarcel } 1117130803Smarcel break; 1118130803Smarcel default: 1119130803Smarcel internal_error (__FILE__, __LINE__, "Invalid or unsupported encoding"); 1120130803Smarcel } 1121130803Smarcel 1122130803Smarcel if ((encoding & 0x0f) == 0x00) 1123130803Smarcel encoding |= encoding_for_size (ptr_len); 1124130803Smarcel 1125130803Smarcel switch (encoding & 0x0f) 1126130803Smarcel { 1127130803Smarcel case DW_EH_PE_udata2: 1128130803Smarcel *bytes_read_ptr += 2; 1129130803Smarcel return (base + bfd_get_16 (unit->abfd, (bfd_byte *) buf)); 1130130803Smarcel case DW_EH_PE_udata4: 1131130803Smarcel *bytes_read_ptr += 4; 1132130803Smarcel return (base + bfd_get_32 (unit->abfd, (bfd_byte *) buf)); 1133130803Smarcel case DW_EH_PE_udata8: 1134130803Smarcel *bytes_read_ptr += 8; 1135130803Smarcel return (base + bfd_get_64 (unit->abfd, (bfd_byte *) buf)); 1136130803Smarcel case DW_EH_PE_sdata2: 1137130803Smarcel *bytes_read_ptr += 2; 1138130803Smarcel return (base + bfd_get_signed_16 (unit->abfd, (bfd_byte *) buf)); 1139130803Smarcel case DW_EH_PE_sdata4: 1140130803Smarcel *bytes_read_ptr += 4; 1141130803Smarcel return (base + bfd_get_signed_32 (unit->abfd, (bfd_byte *) buf)); 1142130803Smarcel case DW_EH_PE_sdata8: 1143130803Smarcel *bytes_read_ptr += 8; 1144130803Smarcel return (base + bfd_get_signed_64 (unit->abfd, (bfd_byte *) buf)); 1145130803Smarcel default: 1146130803Smarcel internal_error (__FILE__, __LINE__, "Invalid or unsupported encoding"); 1147130803Smarcel } 1148130803Smarcel} 1149130803Smarcel 1150130803Smarcel 1151130803Smarcel/* GCC uses a single CIE for all FDEs in a .debug_frame section. 1152130803Smarcel That's why we use a simple linked list here. */ 1153130803Smarcel 1154130803Smarcelstatic struct dwarf2_cie * 1155130803Smarcelfind_cie (struct comp_unit *unit, ULONGEST cie_pointer) 1156130803Smarcel{ 1157130803Smarcel struct dwarf2_cie *cie = unit->cie; 1158130803Smarcel 1159130803Smarcel while (cie) 1160130803Smarcel { 1161130803Smarcel if (cie->cie_pointer == cie_pointer) 1162130803Smarcel return cie; 1163130803Smarcel 1164130803Smarcel cie = cie->next; 1165130803Smarcel } 1166130803Smarcel 1167130803Smarcel return NULL; 1168130803Smarcel} 1169130803Smarcel 1170130803Smarcelstatic void 1171130803Smarceladd_cie (struct comp_unit *unit, struct dwarf2_cie *cie) 1172130803Smarcel{ 1173130803Smarcel cie->next = unit->cie; 1174130803Smarcel unit->cie = cie; 1175130803Smarcel} 1176130803Smarcel 1177130803Smarcel/* Find the FDE for *PC. Return a pointer to the FDE, and store the 1178130803Smarcel inital location associated with it into *PC. */ 1179130803Smarcel 1180130803Smarcelstatic struct dwarf2_fde * 1181130803Smarceldwarf2_frame_find_fde (CORE_ADDR *pc) 1182130803Smarcel{ 1183130803Smarcel struct objfile *objfile; 1184130803Smarcel 1185130803Smarcel ALL_OBJFILES (objfile) 1186130803Smarcel { 1187130803Smarcel struct dwarf2_fde *fde; 1188130803Smarcel CORE_ADDR offset; 1189130803Smarcel 1190130803Smarcel fde = objfile_data (objfile, dwarf2_frame_objfile_data); 1191130803Smarcel if (fde == NULL) 1192130803Smarcel continue; 1193130803Smarcel 1194130803Smarcel gdb_assert (objfile->section_offsets); 1195130803Smarcel offset = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile)); 1196130803Smarcel 1197130803Smarcel while (fde) 1198130803Smarcel { 1199130803Smarcel if (*pc >= fde->initial_location + offset 1200130803Smarcel && *pc < fde->initial_location + offset + fde->address_range) 1201130803Smarcel { 1202130803Smarcel *pc = fde->initial_location + offset; 1203130803Smarcel return fde; 1204130803Smarcel } 1205130803Smarcel 1206130803Smarcel fde = fde->next; 1207130803Smarcel } 1208130803Smarcel } 1209130803Smarcel 1210130803Smarcel return NULL; 1211130803Smarcel} 1212130803Smarcel 1213130803Smarcelstatic void 1214130803Smarceladd_fde (struct comp_unit *unit, struct dwarf2_fde *fde) 1215130803Smarcel{ 1216130803Smarcel fde->next = objfile_data (unit->objfile, dwarf2_frame_objfile_data); 1217130803Smarcel set_objfile_data (unit->objfile, dwarf2_frame_objfile_data, fde); 1218130803Smarcel} 1219130803Smarcel 1220130803Smarcel#ifdef CC_HAS_LONG_LONG 1221130803Smarcel#define DW64_CIE_ID 0xffffffffffffffffULL 1222130803Smarcel#else 1223130803Smarcel#define DW64_CIE_ID ~0 1224130803Smarcel#endif 1225130803Smarcel 1226130803Smarcelstatic char *decode_frame_entry (struct comp_unit *unit, char *start, 1227130803Smarcel int eh_frame_p); 1228130803Smarcel 1229130803Smarcel/* Decode the next CIE or FDE. Return NULL if invalid input, otherwise 1230130803Smarcel the next byte to be processed. */ 1231130803Smarcelstatic char * 1232130803Smarceldecode_frame_entry_1 (struct comp_unit *unit, char *start, int eh_frame_p) 1233130803Smarcel{ 1234130803Smarcel char *buf; 1235130803Smarcel LONGEST length; 1236130803Smarcel unsigned int bytes_read; 1237130803Smarcel int dwarf64_p; 1238130803Smarcel ULONGEST cie_id; 1239130803Smarcel ULONGEST cie_pointer; 1240130803Smarcel char *end; 1241130803Smarcel 1242130803Smarcel buf = start; 1243130803Smarcel length = read_initial_length (unit->abfd, buf, &bytes_read); 1244130803Smarcel buf += bytes_read; 1245130803Smarcel end = buf + length; 1246130803Smarcel 1247130803Smarcel /* Are we still within the section? */ 1248130803Smarcel if (end > unit->dwarf_frame_buffer + unit->dwarf_frame_size) 1249130803Smarcel return NULL; 1250130803Smarcel 1251130803Smarcel if (length == 0) 1252130803Smarcel return end; 1253130803Smarcel 1254130803Smarcel /* Distinguish between 32 and 64-bit encoded frame info. */ 1255130803Smarcel dwarf64_p = (bytes_read == 12); 1256130803Smarcel 1257130803Smarcel /* In a .eh_frame section, zero is used to distinguish CIEs from FDEs. */ 1258130803Smarcel if (eh_frame_p) 1259130803Smarcel cie_id = 0; 1260130803Smarcel else if (dwarf64_p) 1261130803Smarcel cie_id = DW64_CIE_ID; 1262130803Smarcel else 1263130803Smarcel cie_id = DW_CIE_ID; 1264130803Smarcel 1265130803Smarcel if (dwarf64_p) 1266130803Smarcel { 1267130803Smarcel cie_pointer = read_8_bytes (unit->abfd, buf); 1268130803Smarcel buf += 8; 1269130803Smarcel } 1270130803Smarcel else 1271130803Smarcel { 1272130803Smarcel cie_pointer = read_4_bytes (unit->abfd, buf); 1273130803Smarcel buf += 4; 1274130803Smarcel } 1275130803Smarcel 1276130803Smarcel if (cie_pointer == cie_id) 1277130803Smarcel { 1278130803Smarcel /* This is a CIE. */ 1279130803Smarcel struct dwarf2_cie *cie; 1280130803Smarcel char *augmentation; 1281130803Smarcel 1282130803Smarcel /* Record the offset into the .debug_frame section of this CIE. */ 1283130803Smarcel cie_pointer = start - unit->dwarf_frame_buffer; 1284130803Smarcel 1285130803Smarcel /* Check whether we've already read it. */ 1286130803Smarcel if (find_cie (unit, cie_pointer)) 1287130803Smarcel return end; 1288130803Smarcel 1289130803Smarcel cie = (struct dwarf2_cie *) 1290130803Smarcel obstack_alloc (&unit->objfile->objfile_obstack, 1291130803Smarcel sizeof (struct dwarf2_cie)); 1292130803Smarcel cie->initial_instructions = NULL; 1293130803Smarcel cie->cie_pointer = cie_pointer; 1294130803Smarcel 1295130803Smarcel /* The encoding for FDE's in a normal .debug_frame section 1296130803Smarcel depends on the target address size as specified in the 1297130803Smarcel Compilation Unit Header. */ 1298130803Smarcel cie->encoding = encoding_for_size (unit->addr_size); 1299130803Smarcel 1300130803Smarcel /* Check version number. */ 1301130803Smarcel if (read_1_byte (unit->abfd, buf) != DW_CIE_VERSION) 1302130803Smarcel return NULL; 1303130803Smarcel buf += 1; 1304130803Smarcel 1305130803Smarcel /* Interpret the interesting bits of the augmentation. */ 1306130803Smarcel augmentation = buf; 1307130803Smarcel buf = augmentation + strlen (augmentation) + 1; 1308130803Smarcel 1309130803Smarcel /* The GCC 2.x "eh" augmentation has a pointer immediately 1310130803Smarcel following the augmentation string, so it must be handled 1311130803Smarcel first. */ 1312130803Smarcel if (augmentation[0] == 'e' && augmentation[1] == 'h') 1313130803Smarcel { 1314130803Smarcel /* Skip. */ 1315130803Smarcel buf += TYPE_LENGTH (builtin_type_void_data_ptr); 1316130803Smarcel augmentation += 2; 1317130803Smarcel } 1318130803Smarcel 1319130803Smarcel cie->code_alignment_factor = 1320130803Smarcel read_unsigned_leb128 (unit->abfd, buf, &bytes_read); 1321130803Smarcel buf += bytes_read; 1322130803Smarcel 1323130803Smarcel cie->data_alignment_factor = 1324130803Smarcel read_signed_leb128 (unit->abfd, buf, &bytes_read); 1325130803Smarcel buf += bytes_read; 1326130803Smarcel 1327130803Smarcel cie->return_address_register = read_1_byte (unit->abfd, buf); 1328130803Smarcel buf += 1; 1329130803Smarcel 1330130803Smarcel cie->saw_z_augmentation = (*augmentation == 'z'); 1331130803Smarcel if (cie->saw_z_augmentation) 1332130803Smarcel { 1333130803Smarcel ULONGEST length; 1334130803Smarcel 1335130803Smarcel length = read_unsigned_leb128 (unit->abfd, buf, &bytes_read); 1336130803Smarcel buf += bytes_read; 1337130803Smarcel if (buf > end) 1338130803Smarcel return NULL; 1339130803Smarcel cie->initial_instructions = buf + length; 1340130803Smarcel augmentation++; 1341130803Smarcel } 1342130803Smarcel 1343130803Smarcel while (*augmentation) 1344130803Smarcel { 1345130803Smarcel /* "L" indicates a byte showing how the LSDA pointer is encoded. */ 1346130803Smarcel if (*augmentation == 'L') 1347130803Smarcel { 1348130803Smarcel /* Skip. */ 1349130803Smarcel buf++; 1350130803Smarcel augmentation++; 1351130803Smarcel } 1352130803Smarcel 1353130803Smarcel /* "R" indicates a byte indicating how FDE addresses are encoded. */ 1354130803Smarcel else if (*augmentation == 'R') 1355130803Smarcel { 1356130803Smarcel cie->encoding = *buf++; 1357130803Smarcel augmentation++; 1358130803Smarcel } 1359130803Smarcel 1360130803Smarcel /* "P" indicates a personality routine in the CIE augmentation. */ 1361130803Smarcel else if (*augmentation == 'P') 1362130803Smarcel { 1363130803Smarcel /* Skip. */ 1364130803Smarcel buf += size_of_encoded_value (*buf++); 1365130803Smarcel augmentation++; 1366130803Smarcel } 1367130803Smarcel 1368130803Smarcel /* Otherwise we have an unknown augmentation. 1369130803Smarcel Bail out unless we saw a 'z' prefix. */ 1370130803Smarcel else 1371130803Smarcel { 1372130803Smarcel if (cie->initial_instructions == NULL) 1373130803Smarcel return end; 1374130803Smarcel 1375130803Smarcel /* Skip unknown augmentations. */ 1376130803Smarcel buf = cie->initial_instructions; 1377130803Smarcel break; 1378130803Smarcel } 1379130803Smarcel } 1380130803Smarcel 1381130803Smarcel cie->initial_instructions = buf; 1382130803Smarcel cie->end = end; 1383130803Smarcel 1384130803Smarcel add_cie (unit, cie); 1385130803Smarcel } 1386130803Smarcel else 1387130803Smarcel { 1388130803Smarcel /* This is a FDE. */ 1389130803Smarcel struct dwarf2_fde *fde; 1390130803Smarcel 1391130803Smarcel /* In an .eh_frame section, the CIE pointer is the delta between the 1392130803Smarcel address within the FDE where the CIE pointer is stored and the 1393130803Smarcel address of the CIE. Convert it to an offset into the .eh_frame 1394130803Smarcel section. */ 1395130803Smarcel if (eh_frame_p) 1396130803Smarcel { 1397130803Smarcel cie_pointer = buf - unit->dwarf_frame_buffer - cie_pointer; 1398130803Smarcel cie_pointer -= (dwarf64_p ? 8 : 4); 1399130803Smarcel } 1400130803Smarcel 1401130803Smarcel /* In either case, validate the result is still within the section. */ 1402130803Smarcel if (cie_pointer >= unit->dwarf_frame_size) 1403130803Smarcel return NULL; 1404130803Smarcel 1405130803Smarcel fde = (struct dwarf2_fde *) 1406130803Smarcel obstack_alloc (&unit->objfile->objfile_obstack, 1407130803Smarcel sizeof (struct dwarf2_fde)); 1408130803Smarcel fde->cie = find_cie (unit, cie_pointer); 1409130803Smarcel if (fde->cie == NULL) 1410130803Smarcel { 1411130803Smarcel decode_frame_entry (unit, unit->dwarf_frame_buffer + cie_pointer, 1412130803Smarcel eh_frame_p); 1413130803Smarcel fde->cie = find_cie (unit, cie_pointer); 1414130803Smarcel } 1415130803Smarcel 1416130803Smarcel gdb_assert (fde->cie != NULL); 1417130803Smarcel 1418130803Smarcel fde->initial_location = 1419130803Smarcel read_encoded_value (unit, fde->cie->encoding, buf, &bytes_read); 1420130803Smarcel buf += bytes_read; 1421130803Smarcel 1422130803Smarcel fde->address_range = 1423130803Smarcel read_encoded_value (unit, fde->cie->encoding & 0x0f, buf, &bytes_read); 1424130803Smarcel buf += bytes_read; 1425130803Smarcel 1426130803Smarcel /* A 'z' augmentation in the CIE implies the presence of an 1427130803Smarcel augmentation field in the FDE as well. The only thing known 1428130803Smarcel to be in here at present is the LSDA entry for EH. So we 1429130803Smarcel can skip the whole thing. */ 1430130803Smarcel if (fde->cie->saw_z_augmentation) 1431130803Smarcel { 1432130803Smarcel ULONGEST length; 1433130803Smarcel 1434130803Smarcel length = read_unsigned_leb128 (unit->abfd, buf, &bytes_read); 1435130803Smarcel buf += bytes_read + length; 1436130803Smarcel if (buf > end) 1437130803Smarcel return NULL; 1438130803Smarcel } 1439130803Smarcel 1440130803Smarcel fde->instructions = buf; 1441130803Smarcel fde->end = end; 1442130803Smarcel 1443130803Smarcel add_fde (unit, fde); 1444130803Smarcel } 1445130803Smarcel 1446130803Smarcel return end; 1447130803Smarcel} 1448130803Smarcel 1449130803Smarcel/* Read a CIE or FDE in BUF and decode it. */ 1450130803Smarcelstatic char * 1451130803Smarceldecode_frame_entry (struct comp_unit *unit, char *start, int eh_frame_p) 1452130803Smarcel{ 1453130803Smarcel enum { NONE, ALIGN4, ALIGN8, FAIL } workaround = NONE; 1454130803Smarcel char *ret; 1455130803Smarcel const char *msg; 1456130803Smarcel ptrdiff_t start_offset; 1457130803Smarcel 1458130803Smarcel while (1) 1459130803Smarcel { 1460130803Smarcel ret = decode_frame_entry_1 (unit, start, eh_frame_p); 1461130803Smarcel if (ret != NULL) 1462130803Smarcel break; 1463130803Smarcel 1464130803Smarcel /* We have corrupt input data of some form. */ 1465130803Smarcel 1466130803Smarcel /* ??? Try, weakly, to work around compiler/assembler/linker bugs 1467130803Smarcel and mismatches wrt padding and alignment of debug sections. */ 1468130803Smarcel /* Note that there is no requirement in the standard for any 1469130803Smarcel alignment at all in the frame unwind sections. Testing for 1470130803Smarcel alignment before trying to interpret data would be incorrect. 1471130803Smarcel 1472130803Smarcel However, GCC traditionally arranged for frame sections to be 1473130803Smarcel sized such that the FDE length and CIE fields happen to be 1474130803Smarcel aligned (in theory, for performance). This, unfortunately, 1475130803Smarcel was done with .align directives, which had the side effect of 1476130803Smarcel forcing the section to be aligned by the linker. 1477130803Smarcel 1478130803Smarcel This becomes a problem when you have some other producer that 1479130803Smarcel creates frame sections that are not as strictly aligned. That 1480130803Smarcel produces a hole in the frame info that gets filled by the 1481130803Smarcel linker with zeros. 1482130803Smarcel 1483130803Smarcel The GCC behaviour is arguably a bug, but it's effectively now 1484130803Smarcel part of the ABI, so we're now stuck with it, at least at the 1485130803Smarcel object file level. A smart linker may decide, in the process 1486130803Smarcel of compressing duplicate CIE information, that it can rewrite 1487130803Smarcel the entire output section without this extra padding. */ 1488130803Smarcel 1489130803Smarcel start_offset = start - unit->dwarf_frame_buffer; 1490130803Smarcel if (workaround < ALIGN4 && (start_offset & 3) != 0) 1491130803Smarcel { 1492130803Smarcel start += 4 - (start_offset & 3); 1493130803Smarcel workaround = ALIGN4; 1494130803Smarcel continue; 1495130803Smarcel } 1496130803Smarcel if (workaround < ALIGN8 && (start_offset & 7) != 0) 1497130803Smarcel { 1498130803Smarcel start += 8 - (start_offset & 7); 1499130803Smarcel workaround = ALIGN8; 1500130803Smarcel continue; 1501130803Smarcel } 1502130803Smarcel 1503130803Smarcel /* Nothing left to try. Arrange to return as if we've consumed 1504130803Smarcel the entire input section. Hopefully we'll get valid info from 1505130803Smarcel the other of .debug_frame/.eh_frame. */ 1506130803Smarcel workaround = FAIL; 1507130803Smarcel ret = unit->dwarf_frame_buffer + unit->dwarf_frame_size; 1508130803Smarcel break; 1509130803Smarcel } 1510130803Smarcel 1511130803Smarcel switch (workaround) 1512130803Smarcel { 1513130803Smarcel case NONE: 1514130803Smarcel break; 1515130803Smarcel 1516130803Smarcel case ALIGN4: 1517130803Smarcel complaint (&symfile_complaints, 1518130803Smarcel "Corrupt data in %s:%s; align 4 workaround apparently succeeded", 1519130803Smarcel unit->dwarf_frame_section->owner->filename, 1520130803Smarcel unit->dwarf_frame_section->name); 1521130803Smarcel break; 1522130803Smarcel 1523130803Smarcel case ALIGN8: 1524130803Smarcel complaint (&symfile_complaints, 1525130803Smarcel "Corrupt data in %s:%s; align 8 workaround apparently succeeded", 1526130803Smarcel unit->dwarf_frame_section->owner->filename, 1527130803Smarcel unit->dwarf_frame_section->name); 1528130803Smarcel break; 1529130803Smarcel 1530130803Smarcel default: 1531130803Smarcel complaint (&symfile_complaints, 1532130803Smarcel "Corrupt data in %s:%s", 1533130803Smarcel unit->dwarf_frame_section->owner->filename, 1534130803Smarcel unit->dwarf_frame_section->name); 1535130803Smarcel break; 1536130803Smarcel } 1537130803Smarcel 1538130803Smarcel return ret; 1539130803Smarcel} 1540130803Smarcel 1541130803Smarcel 1542130803Smarcel/* FIXME: kettenis/20030504: This still needs to be integrated with 1543130803Smarcel dwarf2read.c in a better way. */ 1544130803Smarcel 1545130803Smarcel/* Imported from dwarf2read.c. */ 1546130803Smarcelextern asection *dwarf_frame_section; 1547130803Smarcelextern asection *dwarf_eh_frame_section; 1548130803Smarcel 1549130803Smarcel/* Imported from dwarf2read.c. */ 1550130803Smarcelextern char *dwarf2_read_section (struct objfile *objfile, asection *sectp); 1551130803Smarcel 1552130803Smarcelvoid 1553130803Smarceldwarf2_build_frame_info (struct objfile *objfile) 1554130803Smarcel{ 1555130803Smarcel struct comp_unit unit; 1556130803Smarcel char *frame_ptr; 1557130803Smarcel 1558130803Smarcel /* Build a minimal decoding of the DWARF2 compilation unit. */ 1559130803Smarcel unit.abfd = objfile->obfd; 1560130803Smarcel unit.objfile = objfile; 1561130803Smarcel unit.addr_size = objfile->obfd->arch_info->bits_per_address / 8; 1562130803Smarcel unit.dbase = 0; 1563130803Smarcel unit.tbase = 0; 1564130803Smarcel 1565130803Smarcel /* First add the information from the .eh_frame section. That way, 1566130803Smarcel the FDEs from that section are searched last. */ 1567130803Smarcel if (dwarf_eh_frame_section) 1568130803Smarcel { 1569130803Smarcel asection *got, *txt; 1570130803Smarcel 1571130803Smarcel unit.cie = NULL; 1572130803Smarcel unit.dwarf_frame_buffer = dwarf2_read_section (objfile, 1573130803Smarcel dwarf_eh_frame_section); 1574130803Smarcel 1575130803Smarcel unit.dwarf_frame_size 1576218822Sdim = bfd_get_section_size (dwarf_eh_frame_section); 1577130803Smarcel unit.dwarf_frame_section = dwarf_eh_frame_section; 1578130803Smarcel 1579130803Smarcel /* FIXME: kettenis/20030602: This is the DW_EH_PE_datarel base 1580130803Smarcel that is used for the i386/amd64 target, which currently is 1581130803Smarcel the only target in GCC that supports/uses the 1582130803Smarcel DW_EH_PE_datarel encoding. */ 1583130803Smarcel got = bfd_get_section_by_name (unit.abfd, ".got"); 1584130803Smarcel if (got) 1585130803Smarcel unit.dbase = got->vma; 1586130803Smarcel 1587130803Smarcel /* GCC emits the DW_EH_PE_textrel encoding type on sh and ia64 1588130803Smarcel so far. */ 1589130803Smarcel txt = bfd_get_section_by_name (unit.abfd, ".text"); 1590130803Smarcel if (txt) 1591130803Smarcel unit.tbase = txt->vma; 1592130803Smarcel 1593130803Smarcel frame_ptr = unit.dwarf_frame_buffer; 1594130803Smarcel while (frame_ptr < unit.dwarf_frame_buffer + unit.dwarf_frame_size) 1595130803Smarcel frame_ptr = decode_frame_entry (&unit, frame_ptr, 1); 1596130803Smarcel } 1597130803Smarcel 1598130803Smarcel if (dwarf_frame_section) 1599130803Smarcel { 1600130803Smarcel unit.cie = NULL; 1601130803Smarcel unit.dwarf_frame_buffer = dwarf2_read_section (objfile, 1602130803Smarcel dwarf_frame_section); 1603130803Smarcel unit.dwarf_frame_size 1604218822Sdim = bfd_get_section_size (dwarf_frame_section); 1605130803Smarcel unit.dwarf_frame_section = dwarf_frame_section; 1606130803Smarcel 1607130803Smarcel frame_ptr = unit.dwarf_frame_buffer; 1608130803Smarcel while (frame_ptr < unit.dwarf_frame_buffer + unit.dwarf_frame_size) 1609130803Smarcel frame_ptr = decode_frame_entry (&unit, frame_ptr, 0); 1610130803Smarcel } 1611130803Smarcel} 1612130803Smarcel 1613130803Smarcel/* Provide a prototype to silence -Wmissing-prototypes. */ 1614130803Smarcelvoid _initialize_dwarf2_frame (void); 1615130803Smarcel 1616130803Smarcelvoid 1617130803Smarcel_initialize_dwarf2_frame (void) 1618130803Smarcel{ 1619130803Smarcel dwarf2_frame_data = register_gdbarch_data (dwarf2_frame_init); 1620130803Smarcel dwarf2_frame_objfile_data = register_objfile_data (); 1621130803Smarcel} 1622