Sync usage with man page.
[netbsd-mini2440.git] / gnu / dist / gdb6 / gdb / dwarf2-frame.c
blob3f7fbfe047cdb5336832303a07ef53f3e96c7bda
1 /* Frame unwinder for frames with DWARF Call Frame Information.
3 Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
5 Contributed by Mark Kettenis.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA. */
24 #include "defs.h"
25 #include "dwarf2expr.h"
26 #include "elf/dwarf2.h"
27 #include "frame.h"
28 #include "frame-base.h"
29 #include "frame-unwind.h"
30 #include "gdbcore.h"
31 #include "gdbtypes.h"
32 #include "symtab.h"
33 #include "objfiles.h"
34 #include "regcache.h"
36 #include "gdb_assert.h"
37 #include "gdb_string.h"
39 #include "complaints.h"
40 #include "dwarf2-frame.h"
42 /* Call Frame Information (CFI). */
44 /* Common Information Entry (CIE). */
46 struct dwarf2_cie
48 /* Offset into the .debug_frame section where this CIE was found.
49 Used to identify this CIE. */
50 ULONGEST cie_pointer;
52 /* Constant that is factored out of all advance location
53 instructions. */
54 ULONGEST code_alignment_factor;
56 /* Constants that is factored out of all offset instructions. */
57 LONGEST data_alignment_factor;
59 /* Return address column. */
60 ULONGEST return_address_register;
62 /* Instruction sequence to initialize a register set. */
63 gdb_byte *initial_instructions;
64 gdb_byte *end;
66 /* Encoding of addresses. */
67 gdb_byte encoding;
69 /* True if a 'z' augmentation existed. */
70 unsigned char saw_z_augmentation;
72 struct dwarf2_cie *next;
75 /* Frame Description Entry (FDE). */
77 struct dwarf2_fde
79 /* CIE for this FDE. */
80 struct dwarf2_cie *cie;
82 /* First location associated with this FDE. */
83 CORE_ADDR initial_location;
85 /* Number of bytes of program instructions described by this FDE. */
86 CORE_ADDR address_range;
88 /* Instruction sequence. */
89 gdb_byte *instructions;
90 gdb_byte *end;
92 /* True if this FDE is read from a .eh_frame instead of a .debug_frame
93 section. */
94 unsigned char eh_frame_p;
96 struct dwarf2_fde *next;
99 static struct dwarf2_fde *dwarf2_frame_find_fde (CORE_ADDR *pc);
102 /* Structure describing a frame state. */
104 struct dwarf2_frame_state
106 /* Each register save state can be described in terms of a CFA slot,
107 another register, or a location expression. */
108 struct dwarf2_frame_state_reg_info
110 struct dwarf2_frame_state_reg *reg;
111 int num_regs;
113 /* Used to implement DW_CFA_remember_state. */
114 struct dwarf2_frame_state_reg_info *prev;
115 } regs;
117 LONGEST cfa_offset;
118 ULONGEST cfa_reg;
119 gdb_byte *cfa_exp;
120 enum {
121 CFA_UNSET,
122 CFA_REG_OFFSET,
123 CFA_EXP
124 } cfa_how;
126 /* The PC described by the current frame state. */
127 CORE_ADDR pc;
129 /* Initial register set from the CIE.
130 Used to implement DW_CFA_restore. */
131 struct dwarf2_frame_state_reg_info initial;
133 /* The information we care about from the CIE. */
134 LONGEST data_align;
135 ULONGEST code_align;
136 ULONGEST retaddr_column;
139 /* Store the length the expression for the CFA in the `cfa_reg' field,
140 which is unused in that case. */
141 #define cfa_exp_len cfa_reg
143 /* Assert that the register set RS is large enough to store NUM_REGS
144 columns. If necessary, enlarge the register set. */
146 static void
147 dwarf2_frame_state_alloc_regs (struct dwarf2_frame_state_reg_info *rs,
148 int num_regs)
150 size_t size = sizeof (struct dwarf2_frame_state_reg);
152 if (num_regs <= rs->num_regs)
153 return;
155 rs->reg = (struct dwarf2_frame_state_reg *)
156 xrealloc (rs->reg, num_regs * size);
158 /* Initialize newly allocated registers. */
159 memset (rs->reg + rs->num_regs, 0, (num_regs - rs->num_regs) * size);
160 rs->num_regs = num_regs;
163 /* Copy the register columns in register set RS into newly allocated
164 memory and return a pointer to this newly created copy. */
166 static struct dwarf2_frame_state_reg *
167 dwarf2_frame_state_copy_regs (struct dwarf2_frame_state_reg_info *rs)
169 size_t size = rs->num_regs * sizeof (struct dwarf2_frame_state_reg);
170 struct dwarf2_frame_state_reg *reg;
172 reg = (struct dwarf2_frame_state_reg *) xmalloc (size);
173 memcpy (reg, rs->reg, size);
175 return reg;
178 /* Release the memory allocated to register set RS. */
180 static void
181 dwarf2_frame_state_free_regs (struct dwarf2_frame_state_reg_info *rs)
183 if (rs)
185 dwarf2_frame_state_free_regs (rs->prev);
187 xfree (rs->reg);
188 xfree (rs);
192 /* Release the memory allocated to the frame state FS. */
194 static void
195 dwarf2_frame_state_free (void *p)
197 struct dwarf2_frame_state *fs = p;
199 dwarf2_frame_state_free_regs (fs->initial.prev);
200 dwarf2_frame_state_free_regs (fs->regs.prev);
201 xfree (fs->initial.reg);
202 xfree (fs->regs.reg);
203 xfree (fs);
207 /* Helper functions for execute_stack_op. */
209 static CORE_ADDR
210 read_reg (void *baton, int reg)
212 struct frame_info *next_frame = (struct frame_info *) baton;
213 struct gdbarch *gdbarch = get_frame_arch (next_frame);
214 int regnum;
215 gdb_byte *buf;
217 regnum = DWARF2_REG_TO_REGNUM (reg);
219 buf = alloca (register_size (gdbarch, regnum));
220 frame_unwind_register (next_frame, regnum, buf);
221 return extract_typed_address (buf, builtin_type_void_data_ptr);
224 static void
225 read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
227 read_memory (addr, buf, len);
230 static void
231 no_get_frame_base (void *baton, gdb_byte **start, size_t *length)
233 internal_error (__FILE__, __LINE__,
234 _("Support for DW_OP_fbreg is unimplemented"));
237 static CORE_ADDR
238 no_get_tls_address (void *baton, CORE_ADDR offset)
240 internal_error (__FILE__, __LINE__,
241 _("Support for DW_OP_GNU_push_tls_address is unimplemented"));
244 static CORE_ADDR
245 execute_stack_op (gdb_byte *exp, ULONGEST len,
246 struct frame_info *next_frame, CORE_ADDR initial)
248 struct dwarf_expr_context *ctx;
249 CORE_ADDR result;
251 ctx = new_dwarf_expr_context ();
252 ctx->baton = next_frame;
253 ctx->read_reg = read_reg;
254 ctx->read_mem = read_mem;
255 ctx->get_frame_base = no_get_frame_base;
256 ctx->get_tls_address = no_get_tls_address;
258 dwarf_expr_push (ctx, initial);
259 dwarf_expr_eval (ctx, exp, len);
260 result = dwarf_expr_fetch (ctx, 0);
262 if (ctx->in_reg)
263 result = read_reg (next_frame, result);
265 free_dwarf_expr_context (ctx);
267 return result;
271 static void
272 execute_cfa_program (gdb_byte *insn_ptr, gdb_byte *insn_end,
273 struct frame_info *next_frame,
274 struct dwarf2_frame_state *fs, int eh_frame_p)
276 CORE_ADDR pc = frame_pc_unwind (next_frame);
277 int bytes_read;
278 struct gdbarch *gdbarch = get_frame_arch (next_frame);
280 while (insn_ptr < insn_end && fs->pc <= pc)
282 gdb_byte insn = *insn_ptr++;
283 ULONGEST utmp, reg;
284 LONGEST offset;
286 if ((insn & 0xc0) == DW_CFA_advance_loc)
287 fs->pc += (insn & 0x3f) * fs->code_align;
288 else if ((insn & 0xc0) == DW_CFA_offset)
290 reg = insn & 0x3f;
291 if (eh_frame_p)
292 reg = dwarf2_frame_eh_frame_regnum (gdbarch, reg);
293 insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp);
294 offset = utmp * fs->data_align;
295 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
296 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
297 fs->regs.reg[reg].loc.offset = offset;
299 else if ((insn & 0xc0) == DW_CFA_restore)
301 gdb_assert (fs->initial.reg);
302 reg = insn & 0x3f;
303 if (eh_frame_p)
304 reg = dwarf2_frame_eh_frame_regnum (gdbarch, reg);
305 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
306 if (reg < fs->initial.num_regs)
307 fs->regs.reg[reg] = fs->initial.reg[reg];
308 else
309 fs->regs.reg[reg].how = DWARF2_FRAME_REG_UNSPECIFIED;
311 if (fs->regs.reg[reg].how == DWARF2_FRAME_REG_UNSPECIFIED)
312 complaint (&symfile_complaints, _("\
313 incomplete CFI data; DW_CFA_restore unspecified\n\
314 register %s (#%d) at 0x%s"),
315 REGISTER_NAME(DWARF2_REG_TO_REGNUM(reg)),
316 DWARF2_REG_TO_REGNUM(reg), paddr (fs->pc));
318 else
320 switch (insn)
322 case DW_CFA_set_loc:
323 fs->pc = dwarf2_read_address (insn_ptr, insn_end, &bytes_read);
324 insn_ptr += bytes_read;
325 break;
327 case DW_CFA_advance_loc1:
328 utmp = extract_unsigned_integer (insn_ptr, 1);
329 fs->pc += utmp * fs->code_align;
330 insn_ptr++;
331 break;
332 case DW_CFA_advance_loc2:
333 utmp = extract_unsigned_integer (insn_ptr, 2);
334 fs->pc += utmp * fs->code_align;
335 insn_ptr += 2;
336 break;
337 case DW_CFA_advance_loc4:
338 utmp = extract_unsigned_integer (insn_ptr, 4);
339 fs->pc += utmp * fs->code_align;
340 insn_ptr += 4;
341 break;
343 case DW_CFA_offset_extended:
344 insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
345 if (eh_frame_p)
346 reg = dwarf2_frame_eh_frame_regnum (gdbarch, reg);
347 insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp);
348 offset = utmp * fs->data_align;
349 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
350 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
351 fs->regs.reg[reg].loc.offset = offset;
352 break;
354 case DW_CFA_restore_extended:
355 gdb_assert (fs->initial.reg);
356 insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
357 if (eh_frame_p)
358 reg = dwarf2_frame_eh_frame_regnum (gdbarch, reg);
359 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
360 fs->regs.reg[reg] = fs->initial.reg[reg];
361 break;
363 case DW_CFA_undefined:
364 insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
365 if (eh_frame_p)
366 reg = dwarf2_frame_eh_frame_regnum (gdbarch, reg);
367 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
368 fs->regs.reg[reg].how = DWARF2_FRAME_REG_UNDEFINED;
369 break;
371 case DW_CFA_same_value:
372 insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
373 if (eh_frame_p)
374 reg = dwarf2_frame_eh_frame_regnum (gdbarch, reg);
375 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
376 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAME_VALUE;
377 break;
379 case DW_CFA_register:
380 insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
381 if (eh_frame_p)
382 reg = dwarf2_frame_eh_frame_regnum (gdbarch, reg);
383 insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp);
384 if (eh_frame_p)
385 utmp = dwarf2_frame_eh_frame_regnum (gdbarch, utmp);
386 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
387 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_REG;
388 fs->regs.reg[reg].loc.reg = utmp;
389 break;
391 case DW_CFA_remember_state:
393 struct dwarf2_frame_state_reg_info *new_rs;
395 new_rs = XMALLOC (struct dwarf2_frame_state_reg_info);
396 *new_rs = fs->regs;
397 fs->regs.reg = dwarf2_frame_state_copy_regs (&fs->regs);
398 fs->regs.prev = new_rs;
400 break;
402 case DW_CFA_restore_state:
404 struct dwarf2_frame_state_reg_info *old_rs = fs->regs.prev;
406 if (old_rs == NULL)
408 complaint (&symfile_complaints, _("\
409 bad CFI data; mismatched DW_CFA_restore_state at 0x%s"), paddr (fs->pc));
411 else
413 xfree (fs->regs.reg);
414 fs->regs = *old_rs;
415 xfree (old_rs);
418 break;
420 case DW_CFA_def_cfa:
421 insn_ptr = read_uleb128 (insn_ptr, insn_end, &fs->cfa_reg);
422 insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp);
423 fs->cfa_offset = utmp;
424 fs->cfa_how = CFA_REG_OFFSET;
425 break;
427 case DW_CFA_def_cfa_register:
428 insn_ptr = read_uleb128 (insn_ptr, insn_end, &fs->cfa_reg);
429 if (eh_frame_p)
430 fs->cfa_reg = dwarf2_frame_eh_frame_regnum (gdbarch,
431 fs->cfa_reg);
432 fs->cfa_how = CFA_REG_OFFSET;
433 break;
435 case DW_CFA_def_cfa_offset:
436 insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp);
437 fs->cfa_offset = utmp;
438 /* cfa_how deliberately not set. */
439 break;
441 case DW_CFA_nop:
442 break;
444 case DW_CFA_def_cfa_expression:
445 insn_ptr = read_uleb128 (insn_ptr, insn_end, &fs->cfa_exp_len);
446 fs->cfa_exp = insn_ptr;
447 fs->cfa_how = CFA_EXP;
448 insn_ptr += fs->cfa_exp_len;
449 break;
451 case DW_CFA_expression:
452 insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
453 if (eh_frame_p)
454 reg = dwarf2_frame_eh_frame_regnum (gdbarch, reg);
455 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
456 insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp);
457 fs->regs.reg[reg].loc.exp = insn_ptr;
458 fs->regs.reg[reg].exp_len = utmp;
459 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_EXP;
460 insn_ptr += utmp;
461 break;
463 case DW_CFA_offset_extended_sf:
464 insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
465 if (eh_frame_p)
466 reg = dwarf2_frame_eh_frame_regnum (gdbarch, reg);
467 insn_ptr = read_sleb128 (insn_ptr, insn_end, &offset);
468 offset *= fs->data_align;
469 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
470 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
471 fs->regs.reg[reg].loc.offset = offset;
472 break;
474 case DW_CFA_def_cfa_sf:
475 insn_ptr = read_uleb128 (insn_ptr, insn_end, &fs->cfa_reg);
476 if (eh_frame_p)
477 fs->cfa_reg = dwarf2_frame_eh_frame_regnum (gdbarch,
478 fs->cfa_reg);
479 insn_ptr = read_sleb128 (insn_ptr, insn_end, &offset);
480 fs->cfa_offset = offset * fs->data_align;
481 fs->cfa_how = CFA_REG_OFFSET;
482 break;
484 case DW_CFA_def_cfa_offset_sf:
485 insn_ptr = read_sleb128 (insn_ptr, insn_end, &offset);
486 fs->cfa_offset = offset * fs->data_align;
487 /* cfa_how deliberately not set. */
488 break;
490 case DW_CFA_GNU_window_save:
491 /* This is SPARC-specific code, and contains hard-coded
492 constants for the register numbering scheme used by
493 GCC. Rather than having a architecture-specific
494 operation that's only ever used by a single
495 architecture, we provide the implementation here.
496 Incidentally that's what GCC does too in its
497 unwinder. */
499 struct gdbarch *gdbarch = get_frame_arch (next_frame);
500 int size = register_size(gdbarch, 0);
501 dwarf2_frame_state_alloc_regs (&fs->regs, 32);
502 for (reg = 8; reg < 16; reg++)
504 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_REG;
505 fs->regs.reg[reg].loc.reg = reg + 16;
507 for (reg = 16; reg < 32; reg++)
509 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
510 fs->regs.reg[reg].loc.offset = (reg - 16) * size;
513 break;
515 case DW_CFA_GNU_args_size:
516 /* Ignored. */
517 insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp);
518 break;
520 default:
521 internal_error (__FILE__, __LINE__, _("Unknown CFI encountered."));
526 /* Don't allow remember/restore between CIE and FDE programs. */
527 dwarf2_frame_state_free_regs (fs->regs.prev);
528 fs->regs.prev = NULL;
532 /* Architecture-specific operations. */
534 /* Per-architecture data key. */
535 static struct gdbarch_data *dwarf2_frame_data;
537 struct dwarf2_frame_ops
539 /* Pre-initialize the register state REG for register REGNUM. */
540 void (*init_reg) (struct gdbarch *, int, struct dwarf2_frame_state_reg *,
541 struct frame_info *);
543 /* Check whether the frame preceding NEXT_FRAME will be a signal
544 trampoline. */
545 int (*signal_frame_p) (struct gdbarch *, struct frame_info *);
547 /* Convert .eh_frame register number to DWARF register number. */
548 int (*eh_frame_regnum) (struct gdbarch *, int);
551 /* Default architecture-specific register state initialization
552 function. */
554 static void
555 dwarf2_frame_default_init_reg (struct gdbarch *gdbarch, int regnum,
556 struct dwarf2_frame_state_reg *reg,
557 struct frame_info *next_frame)
559 /* If we have a register that acts as a program counter, mark it as
560 a destination for the return address. If we have a register that
561 serves as the stack pointer, arrange for it to be filled with the
562 call frame address (CFA). The other registers are marked as
563 unspecified.
565 We copy the return address to the program counter, since many
566 parts in GDB assume that it is possible to get the return address
567 by unwinding the program counter register. However, on ISA's
568 with a dedicated return address register, the CFI usually only
569 contains information to unwind that return address register.
571 The reason we're treating the stack pointer special here is
572 because in many cases GCC doesn't emit CFI for the stack pointer
573 and implicitly assumes that it is equal to the CFA. This makes
574 some sense since the DWARF specification (version 3, draft 8,
575 p. 102) says that:
577 "Typically, the CFA is defined to be the value of the stack
578 pointer at the call site in the previous frame (which may be
579 different from its value on entry to the current frame)."
581 However, this isn't true for all platforms supported by GCC
582 (e.g. IBM S/390 and zSeries). Those architectures should provide
583 their own architecture-specific initialization function. */
585 if (regnum == PC_REGNUM)
586 reg->how = DWARF2_FRAME_REG_RA;
587 else if (regnum == SP_REGNUM)
588 reg->how = DWARF2_FRAME_REG_CFA;
591 /* Return a default for the architecture-specific operations. */
593 static void *
594 dwarf2_frame_init (struct obstack *obstack)
596 struct dwarf2_frame_ops *ops;
598 ops = OBSTACK_ZALLOC (obstack, struct dwarf2_frame_ops);
599 ops->init_reg = dwarf2_frame_default_init_reg;
600 return ops;
603 /* Set the architecture-specific register state initialization
604 function for GDBARCH to INIT_REG. */
606 void
607 dwarf2_frame_set_init_reg (struct gdbarch *gdbarch,
608 void (*init_reg) (struct gdbarch *, int,
609 struct dwarf2_frame_state_reg *,
610 struct frame_info *))
612 struct dwarf2_frame_ops *ops = gdbarch_data (gdbarch, dwarf2_frame_data);
614 ops->init_reg = init_reg;
617 /* Pre-initialize the register state REG for register REGNUM. */
619 static void
620 dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
621 struct dwarf2_frame_state_reg *reg,
622 struct frame_info *next_frame)
624 struct dwarf2_frame_ops *ops = gdbarch_data (gdbarch, dwarf2_frame_data);
626 ops->init_reg (gdbarch, regnum, reg, next_frame);
629 /* Set the architecture-specific signal trampoline recognition
630 function for GDBARCH to SIGNAL_FRAME_P. */
632 void
633 dwarf2_frame_set_signal_frame_p (struct gdbarch *gdbarch,
634 int (*signal_frame_p) (struct gdbarch *,
635 struct frame_info *))
637 struct dwarf2_frame_ops *ops = gdbarch_data (gdbarch, dwarf2_frame_data);
639 ops->signal_frame_p = signal_frame_p;
642 /* Query the architecture-specific signal frame recognizer for
643 NEXT_FRAME. */
645 static int
646 dwarf2_frame_signal_frame_p (struct gdbarch *gdbarch,
647 struct frame_info *next_frame)
649 struct dwarf2_frame_ops *ops = gdbarch_data (gdbarch, dwarf2_frame_data);
651 if (ops->signal_frame_p == NULL)
652 return 0;
653 return ops->signal_frame_p (gdbarch, next_frame);
656 /* Set the architecture-specific mapping of .eh_frame register numbers to
657 DWARF register numbers. */
659 void
660 dwarf2_frame_set_eh_frame_regnum (struct gdbarch *gdbarch,
661 int (*eh_frame_regnum) (struct gdbarch *,
662 int))
664 struct dwarf2_frame_ops *ops = gdbarch_data (gdbarch, dwarf2_frame_data);
666 ops->eh_frame_regnum = eh_frame_regnum;
669 /* Translate a .eh_frame register to DWARF register. */
672 dwarf2_frame_eh_frame_regnum (struct gdbarch *gdbarch, int regnum)
674 struct dwarf2_frame_ops *ops = gdbarch_data (gdbarch, dwarf2_frame_data);
676 if (ops->eh_frame_regnum == NULL)
677 return regnum;
678 return ops->eh_frame_regnum (gdbarch, regnum);
682 struct dwarf2_frame_cache
684 /* DWARF Call Frame Address. */
685 CORE_ADDR cfa;
687 /* Set if the return address column was marked as undefined. */
688 int undefined_retaddr;
690 /* Saved registers, indexed by GDB register number, not by DWARF
691 register number. */
692 struct dwarf2_frame_state_reg *reg;
694 /* Return address register. */
695 struct dwarf2_frame_state_reg retaddr_reg;
698 static struct dwarf2_frame_cache *
699 dwarf2_frame_cache (struct frame_info *next_frame, void **this_cache)
701 struct cleanup *old_chain;
702 struct gdbarch *gdbarch = get_frame_arch (next_frame);
703 const int num_regs = NUM_REGS + NUM_PSEUDO_REGS;
704 struct dwarf2_frame_cache *cache;
705 struct dwarf2_frame_state *fs;
706 struct dwarf2_fde *fde;
708 if (*this_cache)
709 return *this_cache;
711 /* Allocate a new cache. */
712 cache = FRAME_OBSTACK_ZALLOC (struct dwarf2_frame_cache);
713 cache->reg = FRAME_OBSTACK_CALLOC (num_regs, struct dwarf2_frame_state_reg);
715 /* Allocate and initialize the frame state. */
716 fs = XMALLOC (struct dwarf2_frame_state);
717 memset (fs, 0, sizeof (struct dwarf2_frame_state));
718 old_chain = make_cleanup (dwarf2_frame_state_free, fs);
720 /* Unwind the PC.
722 Note that if NEXT_FRAME is never supposed to return (i.e. a call
723 to abort), the compiler might optimize away the instruction at
724 NEXT_FRAME's return address. As a result the return address will
725 point at some random instruction, and the CFI for that
726 instruction is probably worthless to us. GCC's unwinder solves
727 this problem by substracting 1 from the return address to get an
728 address in the middle of a presumed call instruction (or the
729 instruction in the associated delay slot). This should only be
730 done for "normal" frames and not for resume-type frames (signal
731 handlers, sentinel frames, dummy frames). The function
732 frame_unwind_address_in_block does just this. It's not clear how
733 reliable the method is though; there is the potential for the
734 register state pre-call being different to that on return. */
735 fs->pc = frame_unwind_address_in_block (next_frame);
737 /* Find the correct FDE. */
738 fde = dwarf2_frame_find_fde (&fs->pc);
739 gdb_assert (fde != NULL);
741 /* Extract any interesting information from the CIE. */
742 fs->data_align = fde->cie->data_alignment_factor;
743 fs->code_align = fde->cie->code_alignment_factor;
744 fs->retaddr_column = fde->cie->return_address_register;
746 /* First decode all the insns in the CIE. */
747 execute_cfa_program (fde->cie->initial_instructions,
748 fde->cie->end, next_frame, fs, fde->eh_frame_p);
750 /* Save the initialized register set. */
751 fs->initial = fs->regs;
752 fs->initial.reg = dwarf2_frame_state_copy_regs (&fs->regs);
754 /* Then decode the insns in the FDE up to our target PC. */
755 execute_cfa_program (fde->instructions, fde->end, next_frame, fs,
756 fde->eh_frame_p);
758 /* Caclulate the CFA. */
759 switch (fs->cfa_how)
761 case CFA_REG_OFFSET:
762 cache->cfa = read_reg (next_frame, fs->cfa_reg);
763 cache->cfa += fs->cfa_offset;
764 break;
766 case CFA_EXP:
767 cache->cfa =
768 execute_stack_op (fs->cfa_exp, fs->cfa_exp_len, next_frame, 0);
769 break;
771 default:
772 internal_error (__FILE__, __LINE__, _("Unknown CFA rule."));
775 /* Initialize the register state. */
777 int regnum;
779 for (regnum = 0; regnum < num_regs; regnum++)
780 dwarf2_frame_init_reg (gdbarch, regnum, &cache->reg[regnum], next_frame);
783 /* Go through the DWARF2 CFI generated table and save its register
784 location information in the cache. Note that we don't skip the
785 return address column; it's perfectly all right for it to
786 correspond to a real register. If it doesn't correspond to a
787 real register, or if we shouldn't treat it as such,
788 DWARF2_REG_TO_REGNUM should be defined to return a number outside
789 the range [0, NUM_REGS). */
791 int column; /* CFI speak for "register number". */
793 for (column = 0; column < fs->regs.num_regs; column++)
795 /* Use the GDB register number as the destination index. */
796 int regnum = DWARF2_REG_TO_REGNUM (column);
798 /* If there's no corresponding GDB register, ignore it. */
799 if (regnum < 0 || regnum >= num_regs)
800 continue;
802 /* NOTE: cagney/2003-09-05: CFI should specify the disposition
803 of all debug info registers. If it doesn't, complain (but
804 not too loudly). It turns out that GCC assumes that an
805 unspecified register implies "same value" when CFI (draft
806 7) specifies nothing at all. Such a register could equally
807 be interpreted as "undefined". Also note that this check
808 isn't sufficient; it only checks that all registers in the
809 range [0 .. max column] are specified, and won't detect
810 problems when a debug info register falls outside of the
811 table. We need a way of iterating through all the valid
812 DWARF2 register numbers. */
813 if (fs->regs.reg[column].how == DWARF2_FRAME_REG_UNSPECIFIED)
815 if (cache->reg[regnum].how == DWARF2_FRAME_REG_UNSPECIFIED)
816 complaint (&symfile_complaints, _("\
817 incomplete CFI data; unspecified registers (e.g., %s) at 0x%s"),
818 gdbarch_register_name (gdbarch, regnum),
819 paddr_nz (fs->pc));
821 else
822 cache->reg[regnum] = fs->regs.reg[column];
826 /* Eliminate any DWARF2_FRAME_REG_RA rules, and save the information
827 we need for evaluating DWARF2_FRAME_REG_RA_OFFSET rules. */
829 int regnum;
831 for (regnum = 0; regnum < num_regs; regnum++)
833 if (cache->reg[regnum].how == DWARF2_FRAME_REG_RA
834 || cache->reg[regnum].how == DWARF2_FRAME_REG_RA_OFFSET)
836 struct dwarf2_frame_state_reg *retaddr_reg =
837 &fs->regs.reg[fs->retaddr_column];
839 /* It seems rather bizarre to specify an "empty" column as
840 the return adress column. However, this is exactly
841 what GCC does on some targets. It turns out that GCC
842 assumes that the return address can be found in the
843 register corresponding to the return address column.
844 Incidentally, that's how we should treat a return
845 address column specifying "same value" too. */
846 if (fs->retaddr_column < fs->regs.num_regs
847 && retaddr_reg->how != DWARF2_FRAME_REG_UNSPECIFIED
848 && retaddr_reg->how != DWARF2_FRAME_REG_SAME_VALUE)
850 if (cache->reg[regnum].how == DWARF2_FRAME_REG_RA)
851 cache->reg[regnum] = *retaddr_reg;
852 else
853 cache->retaddr_reg = *retaddr_reg;
855 else
857 if (cache->reg[regnum].how == DWARF2_FRAME_REG_RA)
859 cache->reg[regnum].loc.reg = fs->retaddr_column;
860 cache->reg[regnum].how = DWARF2_FRAME_REG_SAVED_REG;
862 else
864 cache->retaddr_reg.loc.reg = fs->retaddr_column;
865 cache->retaddr_reg.how = DWARF2_FRAME_REG_SAVED_REG;
872 if (fs->retaddr_column < fs->regs.num_regs
873 && fs->regs.reg[fs->retaddr_column].how == DWARF2_FRAME_REG_UNDEFINED)
874 cache->undefined_retaddr = 1;
876 do_cleanups (old_chain);
878 *this_cache = cache;
879 return cache;
882 static void
883 dwarf2_frame_this_id (struct frame_info *next_frame, void **this_cache,
884 struct frame_id *this_id)
886 struct dwarf2_frame_cache *cache =
887 dwarf2_frame_cache (next_frame, this_cache);
889 if (cache->undefined_retaddr)
890 return;
892 (*this_id) = frame_id_build (cache->cfa, frame_func_unwind (next_frame));
895 static void
896 dwarf2_frame_prev_register (struct frame_info *next_frame, void **this_cache,
897 int regnum, int *optimizedp,
898 enum lval_type *lvalp, CORE_ADDR *addrp,
899 int *realnump, gdb_byte *valuep)
901 struct gdbarch *gdbarch = get_frame_arch (next_frame);
902 struct dwarf2_frame_cache *cache =
903 dwarf2_frame_cache (next_frame, this_cache);
905 switch (cache->reg[regnum].how)
907 case DWARF2_FRAME_REG_UNDEFINED:
908 /* If CFI explicitly specified that the value isn't defined,
909 mark it as optimized away; the value isn't available. */
910 *optimizedp = 1;
911 *lvalp = not_lval;
912 *addrp = 0;
913 *realnump = -1;
914 if (valuep)
916 /* In some cases, for example %eflags on the i386, we have
917 to provide a sane value, even though this register wasn't
918 saved. Assume we can get it from NEXT_FRAME. */
919 frame_unwind_register (next_frame, regnum, valuep);
921 break;
923 case DWARF2_FRAME_REG_SAVED_OFFSET:
924 *optimizedp = 0;
925 *lvalp = lval_memory;
926 *addrp = cache->cfa + cache->reg[regnum].loc.offset;
927 *realnump = -1;
928 if (valuep)
930 /* Read the value in from memory. */
931 read_memory (*addrp, valuep, register_size (gdbarch, regnum));
933 break;
935 case DWARF2_FRAME_REG_SAVED_REG:
936 *optimizedp = 0;
937 *lvalp = lval_register;
938 *addrp = 0;
939 *realnump = DWARF2_REG_TO_REGNUM (cache->reg[regnum].loc.reg);
940 if (valuep)
941 frame_unwind_register (next_frame, (*realnump), valuep);
942 break;
944 case DWARF2_FRAME_REG_SAVED_EXP:
945 *optimizedp = 0;
946 *lvalp = lval_memory;
947 *addrp = execute_stack_op (cache->reg[regnum].loc.exp,
948 cache->reg[regnum].exp_len,
949 next_frame, cache->cfa);
950 *realnump = -1;
951 if (valuep)
953 /* Read the value in from memory. */
954 read_memory (*addrp, valuep, register_size (gdbarch, regnum));
956 break;
958 case DWARF2_FRAME_REG_UNSPECIFIED:
959 /* GCC, in its infinite wisdom decided to not provide unwind
960 information for registers that are "same value". Since
961 DWARF2 (3 draft 7) doesn't define such behavior, said
962 registers are actually undefined (which is different to CFI
963 "undefined"). Code above issues a complaint about this.
964 Here just fudge the books, assume GCC, and that the value is
965 more inner on the stack. */
966 *optimizedp = 0;
967 *lvalp = lval_register;
968 *addrp = 0;
969 *realnump = regnum;
970 if (valuep)
971 frame_unwind_register (next_frame, (*realnump), valuep);
972 break;
974 case DWARF2_FRAME_REG_SAME_VALUE:
975 *optimizedp = 0;
976 *lvalp = lval_register;
977 *addrp = 0;
978 *realnump = regnum;
979 if (valuep)
980 frame_unwind_register (next_frame, (*realnump), valuep);
981 break;
983 case DWARF2_FRAME_REG_CFA:
984 *optimizedp = 0;
985 *lvalp = not_lval;
986 *addrp = 0;
987 *realnump = -1;
988 if (valuep)
990 /* Store the value. */
991 store_typed_address (valuep, builtin_type_void_data_ptr, cache->cfa);
993 break;
995 case DWARF2_FRAME_REG_CFA_OFFSET:
996 *optimizedp = 0;
997 *lvalp = not_lval;
998 *addrp = 0;
999 *realnump = -1;
1000 if (valuep)
1002 /* Store the value. */
1003 store_typed_address (valuep, builtin_type_void_data_ptr,
1004 cache->cfa + cache->reg[regnum].loc.offset);
1006 break;
1008 case DWARF2_FRAME_REG_RA_OFFSET:
1009 *optimizedp = 0;
1010 *lvalp = not_lval;
1011 *addrp = 0;
1012 *realnump = -1;
1013 if (valuep)
1015 CORE_ADDR pc = cache->reg[regnum].loc.offset;
1017 regnum = DWARF2_REG_TO_REGNUM (cache->retaddr_reg.loc.reg);
1018 pc += frame_unwind_register_unsigned (next_frame, regnum);
1019 store_typed_address (valuep, builtin_type_void_func_ptr, pc);
1021 break;
1023 default:
1024 internal_error (__FILE__, __LINE__, _("Unknown register rule."));
1028 static const struct frame_unwind dwarf2_frame_unwind =
1030 NORMAL_FRAME,
1031 dwarf2_frame_this_id,
1032 dwarf2_frame_prev_register
1035 static const struct frame_unwind dwarf2_signal_frame_unwind =
1037 SIGTRAMP_FRAME,
1038 dwarf2_frame_this_id,
1039 dwarf2_frame_prev_register
1042 const struct frame_unwind *
1043 dwarf2_frame_sniffer (struct frame_info *next_frame)
1045 /* Grab an address that is guarenteed to reside somewhere within the
1046 function. frame_pc_unwind(), for a no-return next function, can
1047 end up returning something past the end of this function's body. */
1048 CORE_ADDR block_addr = frame_unwind_address_in_block (next_frame);
1049 if (!dwarf2_frame_find_fde (&block_addr))
1050 return NULL;
1052 /* On some targets, signal trampolines may have unwind information.
1053 We need to recognize them so that we set the frame type
1054 correctly. */
1056 if (dwarf2_frame_signal_frame_p (get_frame_arch (next_frame),
1057 next_frame))
1058 return &dwarf2_signal_frame_unwind;
1060 return &dwarf2_frame_unwind;
1064 /* There is no explicitly defined relationship between the CFA and the
1065 location of frame's local variables and arguments/parameters.
1066 Therefore, frame base methods on this page should probably only be
1067 used as a last resort, just to avoid printing total garbage as a
1068 response to the "info frame" command. */
1070 static CORE_ADDR
1071 dwarf2_frame_base_address (struct frame_info *next_frame, void **this_cache)
1073 struct dwarf2_frame_cache *cache =
1074 dwarf2_frame_cache (next_frame, this_cache);
1076 return cache->cfa;
1079 static const struct frame_base dwarf2_frame_base =
1081 &dwarf2_frame_unwind,
1082 dwarf2_frame_base_address,
1083 dwarf2_frame_base_address,
1084 dwarf2_frame_base_address
1087 const struct frame_base *
1088 dwarf2_frame_base_sniffer (struct frame_info *next_frame)
1090 CORE_ADDR pc = frame_pc_unwind (next_frame);
1091 if (dwarf2_frame_find_fde (&pc))
1092 return &dwarf2_frame_base;
1094 return NULL;
1097 /* A minimal decoding of DWARF2 compilation units. We only decode
1098 what's needed to get to the call frame information. */
1100 struct comp_unit
1102 /* Keep the bfd convenient. */
1103 bfd *abfd;
1105 struct objfile *objfile;
1107 /* Linked list of CIEs for this object. */
1108 struct dwarf2_cie *cie;
1110 /* Pointer to the .debug_frame section loaded into memory. */
1111 gdb_byte *dwarf_frame_buffer;
1113 /* Length of the loaded .debug_frame section. */
1114 unsigned long dwarf_frame_size;
1116 /* Pointer to the .debug_frame section. */
1117 asection *dwarf_frame_section;
1119 /* Base for DW_EH_PE_datarel encodings. */
1120 bfd_vma dbase;
1122 /* Base for DW_EH_PE_textrel encodings. */
1123 bfd_vma tbase;
1126 const struct objfile_data *dwarf2_frame_objfile_data;
1128 static unsigned int
1129 read_1_byte (bfd *abfd, gdb_byte *buf)
1131 return bfd_get_8 (abfd, buf);
1134 static unsigned int
1135 read_4_bytes (bfd *abfd, gdb_byte *buf)
1137 return bfd_get_32 (abfd, buf);
1140 static ULONGEST
1141 read_8_bytes (bfd *abfd, gdb_byte *buf)
1143 return bfd_get_64 (abfd, buf);
1146 static ULONGEST
1147 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
1149 ULONGEST result;
1150 unsigned int num_read;
1151 int shift;
1152 gdb_byte byte;
1154 result = 0;
1155 shift = 0;
1156 num_read = 0;
1160 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
1161 buf++;
1162 num_read++;
1163 result |= ((byte & 0x7f) << shift);
1164 shift += 7;
1166 while (byte & 0x80);
1168 *bytes_read_ptr = num_read;
1170 return result;
1173 static LONGEST
1174 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
1176 LONGEST result;
1177 int shift;
1178 unsigned int num_read;
1179 gdb_byte byte;
1181 result = 0;
1182 shift = 0;
1183 num_read = 0;
1187 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
1188 buf++;
1189 num_read++;
1190 result |= ((byte & 0x7f) << shift);
1191 shift += 7;
1193 while (byte & 0x80);
1195 if (shift < 8 * sizeof (result) && (byte & 0x40))
1196 result |= -(((LONGEST)1) << shift);
1198 *bytes_read_ptr = num_read;
1200 return result;
1203 static ULONGEST
1204 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
1206 LONGEST result;
1208 result = bfd_get_32 (abfd, buf);
1209 if (result == 0xffffffff)
1211 result = bfd_get_64 (abfd, buf + 4);
1212 *bytes_read_ptr = 12;
1214 else
1215 *bytes_read_ptr = 4;
1217 return result;
1221 /* Pointer encoding helper functions. */
1223 /* GCC supports exception handling based on DWARF2 CFI. However, for
1224 technical reasons, it encodes addresses in its FDE's in a different
1225 way. Several "pointer encodings" are supported. The encoding
1226 that's used for a particular FDE is determined by the 'R'
1227 augmentation in the associated CIE. The argument of this
1228 augmentation is a single byte.
1230 The address can be encoded as 2 bytes, 4 bytes, 8 bytes, or as a
1231 LEB128. This is encoded in bits 0, 1 and 2. Bit 3 encodes whether
1232 the address is signed or unsigned. Bits 4, 5 and 6 encode how the
1233 address should be interpreted (absolute, relative to the current
1234 position in the FDE, ...). Bit 7, indicates that the address
1235 should be dereferenced. */
1237 static gdb_byte
1238 encoding_for_size (unsigned int size)
1240 switch (size)
1242 case 2:
1243 return DW_EH_PE_udata2;
1244 case 4:
1245 return DW_EH_PE_udata4;
1246 case 8:
1247 return DW_EH_PE_udata8;
1248 default:
1249 internal_error (__FILE__, __LINE__, _("Unsupported address size"));
1253 static unsigned int
1254 size_of_encoded_value (gdb_byte encoding)
1256 if (encoding == DW_EH_PE_omit)
1257 return 0;
1259 switch (encoding & 0x07)
1261 case DW_EH_PE_absptr:
1262 return TYPE_LENGTH (builtin_type_void_data_ptr);
1263 case DW_EH_PE_udata2:
1264 return 2;
1265 case DW_EH_PE_udata4:
1266 return 4;
1267 case DW_EH_PE_udata8:
1268 return 8;
1269 default:
1270 internal_error (__FILE__, __LINE__, _("Invalid or unsupported encoding"));
1274 static CORE_ADDR
1275 read_encoded_value (struct comp_unit *unit, gdb_byte encoding,
1276 gdb_byte *buf, unsigned int *bytes_read_ptr)
1278 int ptr_len = size_of_encoded_value (DW_EH_PE_absptr);
1279 ptrdiff_t offset;
1280 CORE_ADDR base;
1282 /* GCC currently doesn't generate DW_EH_PE_indirect encodings for
1283 FDE's. */
1284 if (encoding & DW_EH_PE_indirect)
1285 internal_error (__FILE__, __LINE__,
1286 _("Unsupported encoding: DW_EH_PE_indirect"));
1288 *bytes_read_ptr = 0;
1290 switch (encoding & 0x70)
1292 case DW_EH_PE_absptr:
1293 base = 0;
1294 break;
1295 case DW_EH_PE_pcrel:
1296 base = bfd_get_section_vma (unit->bfd, unit->dwarf_frame_section);
1297 base += (buf - unit->dwarf_frame_buffer);
1298 break;
1299 case DW_EH_PE_datarel:
1300 base = unit->dbase;
1301 break;
1302 case DW_EH_PE_textrel:
1303 base = unit->tbase;
1304 break;
1305 case DW_EH_PE_funcrel:
1306 /* FIXME: kettenis/20040501: For now just pretend
1307 DW_EH_PE_funcrel is equivalent to DW_EH_PE_absptr. For
1308 reading the initial location of an FDE it should be treated
1309 as such, and currently that's the only place where this code
1310 is used. */
1311 base = 0;
1312 break;
1313 case DW_EH_PE_aligned:
1314 base = 0;
1315 offset = buf - unit->dwarf_frame_buffer;
1316 if ((offset % ptr_len) != 0)
1318 *bytes_read_ptr = ptr_len - (offset % ptr_len);
1319 buf += *bytes_read_ptr;
1321 break;
1322 default:
1323 internal_error (__FILE__, __LINE__, _("Invalid or unsupported encoding"));
1326 if ((encoding & 0x07) == 0x00)
1327 encoding |= encoding_for_size (ptr_len);
1329 switch (encoding & 0x0f)
1331 case DW_EH_PE_uleb128:
1333 ULONGEST value;
1334 gdb_byte *end_buf = buf + (sizeof (value) + 1) * 8 / 7;
1335 *bytes_read_ptr += read_uleb128 (buf, end_buf, &value) - buf;
1336 return base + value;
1338 case DW_EH_PE_udata2:
1339 *bytes_read_ptr += 2;
1340 return (base + bfd_get_16 (unit->abfd, (bfd_byte *) buf));
1341 case DW_EH_PE_udata4:
1342 *bytes_read_ptr += 4;
1343 return (base + bfd_get_32 (unit->abfd, (bfd_byte *) buf));
1344 case DW_EH_PE_udata8:
1345 *bytes_read_ptr += 8;
1346 return (base + bfd_get_64 (unit->abfd, (bfd_byte *) buf));
1347 case DW_EH_PE_sleb128:
1349 LONGEST value;
1350 gdb_byte *end_buf = buf + (sizeof (value) + 1) * 8 / 7;
1351 *bytes_read_ptr += read_sleb128 (buf, end_buf, &value) - buf;
1352 return base + value;
1354 case DW_EH_PE_sdata2:
1355 *bytes_read_ptr += 2;
1356 return (base + bfd_get_signed_16 (unit->abfd, (bfd_byte *) buf));
1357 case DW_EH_PE_sdata4:
1358 *bytes_read_ptr += 4;
1359 return (base + bfd_get_signed_32 (unit->abfd, (bfd_byte *) buf));
1360 case DW_EH_PE_sdata8:
1361 *bytes_read_ptr += 8;
1362 return (base + bfd_get_signed_64 (unit->abfd, (bfd_byte *) buf));
1363 default:
1364 internal_error (__FILE__, __LINE__, _("Invalid or unsupported encoding"));
1369 /* GCC uses a single CIE for all FDEs in a .debug_frame section.
1370 That's why we use a simple linked list here. */
1372 static struct dwarf2_cie *
1373 find_cie (struct comp_unit *unit, ULONGEST cie_pointer)
1375 struct dwarf2_cie *cie = unit->cie;
1377 while (cie)
1379 if (cie->cie_pointer == cie_pointer)
1380 return cie;
1382 cie = cie->next;
1385 return NULL;
1388 static void
1389 add_cie (struct comp_unit *unit, struct dwarf2_cie *cie)
1391 cie->next = unit->cie;
1392 unit->cie = cie;
1395 /* Find the FDE for *PC. Return a pointer to the FDE, and store the
1396 inital location associated with it into *PC. */
1398 static struct dwarf2_fde *
1399 dwarf2_frame_find_fde (CORE_ADDR *pc)
1401 struct objfile *objfile;
1403 ALL_OBJFILES (objfile)
1405 struct dwarf2_fde *fde;
1406 CORE_ADDR offset;
1408 fde = objfile_data (objfile, dwarf2_frame_objfile_data);
1409 if (fde == NULL)
1410 continue;
1412 gdb_assert (objfile->section_offsets);
1413 offset = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1415 while (fde)
1417 if (*pc >= fde->initial_location + offset
1418 && *pc < fde->initial_location + offset + fde->address_range)
1420 *pc = fde->initial_location + offset;
1421 return fde;
1424 fde = fde->next;
1428 return NULL;
1431 static void
1432 add_fde (struct comp_unit *unit, struct dwarf2_fde *fde)
1434 fde->next = objfile_data (unit->objfile, dwarf2_frame_objfile_data);
1435 set_objfile_data (unit->objfile, dwarf2_frame_objfile_data, fde);
1438 #ifdef CC_HAS_LONG_LONG
1439 #define DW64_CIE_ID 0xffffffffffffffffULL
1440 #else
1441 #define DW64_CIE_ID ~0
1442 #endif
1444 static gdb_byte *decode_frame_entry (struct comp_unit *unit, gdb_byte *start,
1445 int eh_frame_p);
1447 /* Decode the next CIE or FDE. Return NULL if invalid input, otherwise
1448 the next byte to be processed. */
1449 static gdb_byte *
1450 decode_frame_entry_1 (struct comp_unit *unit, gdb_byte *start, int eh_frame_p)
1452 gdb_byte *buf, *end;
1453 LONGEST length;
1454 unsigned int bytes_read;
1455 int dwarf64_p;
1456 ULONGEST cie_id;
1457 ULONGEST cie_pointer;
1459 buf = start;
1460 length = read_initial_length (unit->abfd, buf, &bytes_read);
1461 buf += bytes_read;
1462 end = buf + length;
1464 /* Are we still within the section? */
1465 if (end > unit->dwarf_frame_buffer + unit->dwarf_frame_size)
1466 return NULL;
1468 if (length == 0)
1469 return end;
1471 /* Distinguish between 32 and 64-bit encoded frame info. */
1472 dwarf64_p = (bytes_read == 12);
1474 /* In a .eh_frame section, zero is used to distinguish CIEs from FDEs. */
1475 if (eh_frame_p)
1476 cie_id = 0;
1477 else if (dwarf64_p)
1478 cie_id = DW64_CIE_ID;
1479 else
1480 cie_id = DW_CIE_ID;
1482 if (dwarf64_p)
1484 cie_pointer = read_8_bytes (unit->abfd, buf);
1485 buf += 8;
1487 else
1489 cie_pointer = read_4_bytes (unit->abfd, buf);
1490 buf += 4;
1493 if (cie_pointer == cie_id)
1495 /* This is a CIE. */
1496 struct dwarf2_cie *cie;
1497 char *augmentation;
1498 unsigned int cie_version;
1500 /* Record the offset into the .debug_frame section of this CIE. */
1501 cie_pointer = start - unit->dwarf_frame_buffer;
1503 /* Check whether we've already read it. */
1504 if (find_cie (unit, cie_pointer))
1505 return end;
1507 cie = (struct dwarf2_cie *)
1508 obstack_alloc (&unit->objfile->objfile_obstack,
1509 sizeof (struct dwarf2_cie));
1510 cie->initial_instructions = NULL;
1511 cie->cie_pointer = cie_pointer;
1513 /* The encoding for FDE's in a normal .debug_frame section
1514 depends on the target address size. */
1515 cie->encoding = DW_EH_PE_absptr;
1517 /* Check version number. */
1518 cie_version = read_1_byte (unit->abfd, buf);
1519 if (cie_version != 1 && cie_version != 3)
1520 return NULL;
1521 buf += 1;
1523 /* Interpret the interesting bits of the augmentation. */
1524 augmentation = (char *) buf;
1525 buf += (strlen (augmentation) + 1);
1527 /* The GCC 2.x "eh" augmentation has a pointer immediately
1528 following the augmentation string, so it must be handled
1529 first. */
1530 if (augmentation[0] == 'e' && augmentation[1] == 'h')
1532 /* Skip. */
1533 buf += TYPE_LENGTH (builtin_type_void_data_ptr);
1534 augmentation += 2;
1537 cie->code_alignment_factor =
1538 read_unsigned_leb128 (unit->abfd, buf, &bytes_read);
1539 buf += bytes_read;
1541 cie->data_alignment_factor =
1542 read_signed_leb128 (unit->abfd, buf, &bytes_read);
1543 buf += bytes_read;
1545 if (cie_version == 1)
1547 cie->return_address_register = read_1_byte (unit->abfd, buf);
1548 bytes_read = 1;
1550 else
1551 cie->return_address_register = read_unsigned_leb128 (unit->abfd, buf,
1552 &bytes_read);
1553 if (eh_frame_p)
1554 cie->return_address_register
1555 = dwarf2_frame_eh_frame_regnum (current_gdbarch,
1556 cie->return_address_register);
1558 buf += bytes_read;
1560 cie->saw_z_augmentation = (*augmentation == 'z');
1561 if (cie->saw_z_augmentation)
1563 ULONGEST length;
1565 length = read_unsigned_leb128 (unit->abfd, buf, &bytes_read);
1566 buf += bytes_read;
1567 if (buf > end)
1568 return NULL;
1569 cie->initial_instructions = buf + length;
1570 augmentation++;
1573 while (*augmentation)
1575 /* "L" indicates a byte showing how the LSDA pointer is encoded. */
1576 if (*augmentation == 'L')
1578 /* Skip. */
1579 buf++;
1580 augmentation++;
1583 /* "R" indicates a byte indicating how FDE addresses are encoded. */
1584 else if (*augmentation == 'R')
1586 cie->encoding = *buf++;
1587 augmentation++;
1590 /* "P" indicates a personality routine in the CIE augmentation. */
1591 else if (*augmentation == 'P')
1593 /* Skip. Avoid indirection since we throw away the result. */
1594 gdb_byte encoding = (*buf++) & ~DW_EH_PE_indirect;
1595 read_encoded_value (unit, encoding, buf, &bytes_read);
1596 buf += bytes_read;
1597 augmentation++;
1600 /* Otherwise we have an unknown augmentation.
1601 Bail out unless we saw a 'z' prefix. */
1602 else
1604 if (cie->initial_instructions == NULL)
1605 return end;
1607 /* Skip unknown augmentations. */
1608 buf = cie->initial_instructions;
1609 break;
1613 cie->initial_instructions = buf;
1614 cie->end = end;
1616 add_cie (unit, cie);
1618 else
1620 /* This is a FDE. */
1621 struct dwarf2_fde *fde;
1623 /* In an .eh_frame section, the CIE pointer is the delta between the
1624 address within the FDE where the CIE pointer is stored and the
1625 address of the CIE. Convert it to an offset into the .eh_frame
1626 section. */
1627 if (eh_frame_p)
1629 cie_pointer = buf - unit->dwarf_frame_buffer - cie_pointer;
1630 cie_pointer -= (dwarf64_p ? 8 : 4);
1633 /* In either case, validate the result is still within the section. */
1634 if (cie_pointer >= unit->dwarf_frame_size)
1635 return NULL;
1637 fde = (struct dwarf2_fde *)
1638 obstack_alloc (&unit->objfile->objfile_obstack,
1639 sizeof (struct dwarf2_fde));
1640 fde->cie = find_cie (unit, cie_pointer);
1641 if (fde->cie == NULL)
1643 decode_frame_entry (unit, unit->dwarf_frame_buffer + cie_pointer,
1644 eh_frame_p);
1645 fde->cie = find_cie (unit, cie_pointer);
1648 gdb_assert (fde->cie != NULL);
1650 fde->initial_location =
1651 read_encoded_value (unit, fde->cie->encoding, buf, &bytes_read);
1652 buf += bytes_read;
1654 fde->address_range =
1655 read_encoded_value (unit, fde->cie->encoding & 0x0f, buf, &bytes_read);
1656 buf += bytes_read;
1658 /* A 'z' augmentation in the CIE implies the presence of an
1659 augmentation field in the FDE as well. The only thing known
1660 to be in here at present is the LSDA entry for EH. So we
1661 can skip the whole thing. */
1662 if (fde->cie->saw_z_augmentation)
1664 ULONGEST length;
1666 length = read_unsigned_leb128 (unit->abfd, buf, &bytes_read);
1667 buf += bytes_read + length;
1668 if (buf > end)
1669 return NULL;
1672 fde->instructions = buf;
1673 fde->end = end;
1675 fde->eh_frame_p = eh_frame_p;
1677 add_fde (unit, fde);
1680 return end;
1683 /* Read a CIE or FDE in BUF and decode it. */
1684 static gdb_byte *
1685 decode_frame_entry (struct comp_unit *unit, gdb_byte *start, int eh_frame_p)
1687 enum { NONE, ALIGN4, ALIGN8, FAIL } workaround = NONE;
1688 gdb_byte *ret;
1689 const char *msg;
1690 ptrdiff_t start_offset;
1692 while (1)
1694 ret = decode_frame_entry_1 (unit, start, eh_frame_p);
1695 if (ret != NULL)
1696 break;
1698 /* We have corrupt input data of some form. */
1700 /* ??? Try, weakly, to work around compiler/assembler/linker bugs
1701 and mismatches wrt padding and alignment of debug sections. */
1702 /* Note that there is no requirement in the standard for any
1703 alignment at all in the frame unwind sections. Testing for
1704 alignment before trying to interpret data would be incorrect.
1706 However, GCC traditionally arranged for frame sections to be
1707 sized such that the FDE length and CIE fields happen to be
1708 aligned (in theory, for performance). This, unfortunately,
1709 was done with .align directives, which had the side effect of
1710 forcing the section to be aligned by the linker.
1712 This becomes a problem when you have some other producer that
1713 creates frame sections that are not as strictly aligned. That
1714 produces a hole in the frame info that gets filled by the
1715 linker with zeros.
1717 The GCC behaviour is arguably a bug, but it's effectively now
1718 part of the ABI, so we're now stuck with it, at least at the
1719 object file level. A smart linker may decide, in the process
1720 of compressing duplicate CIE information, that it can rewrite
1721 the entire output section without this extra padding. */
1723 start_offset = start - unit->dwarf_frame_buffer;
1724 if (workaround < ALIGN4 && (start_offset & 3) != 0)
1726 start += 4 - (start_offset & 3);
1727 workaround = ALIGN4;
1728 continue;
1730 if (workaround < ALIGN8 && (start_offset & 7) != 0)
1732 start += 8 - (start_offset & 7);
1733 workaround = ALIGN8;
1734 continue;
1737 /* Nothing left to try. Arrange to return as if we've consumed
1738 the entire input section. Hopefully we'll get valid info from
1739 the other of .debug_frame/.eh_frame. */
1740 workaround = FAIL;
1741 ret = unit->dwarf_frame_buffer + unit->dwarf_frame_size;
1742 break;
1745 switch (workaround)
1747 case NONE:
1748 break;
1750 case ALIGN4:
1751 complaint (&symfile_complaints,
1752 _("Corrupt data in %s:%s; align 4 workaround apparently succeeded"),
1753 unit->dwarf_frame_section->owner->filename,
1754 unit->dwarf_frame_section->name);
1755 break;
1757 case ALIGN8:
1758 complaint (&symfile_complaints,
1759 _("Corrupt data in %s:%s; align 8 workaround apparently succeeded"),
1760 unit->dwarf_frame_section->owner->filename,
1761 unit->dwarf_frame_section->name);
1762 break;
1764 default:
1765 complaint (&symfile_complaints,
1766 _("Corrupt data in %s:%s"),
1767 unit->dwarf_frame_section->owner->filename,
1768 unit->dwarf_frame_section->name);
1769 break;
1772 return ret;
1776 /* FIXME: kettenis/20030504: This still needs to be integrated with
1777 dwarf2read.c in a better way. */
1779 /* Imported from dwarf2read.c. */
1780 extern asection *dwarf_frame_section;
1781 extern asection *dwarf_eh_frame_section;
1783 /* Imported from dwarf2read.c. */
1784 extern gdb_byte *dwarf2_read_section (struct objfile *objfile, asection *sectp);
1786 void
1787 dwarf2_build_frame_info (struct objfile *objfile)
1789 struct comp_unit unit;
1790 gdb_byte *frame_ptr;
1792 /* Build a minimal decoding of the DWARF2 compilation unit. */
1793 unit.abfd = objfile->obfd;
1794 unit.objfile = objfile;
1795 unit.dbase = 0;
1796 unit.tbase = 0;
1798 /* First add the information from the .eh_frame section. That way,
1799 the FDEs from that section are searched last. */
1800 if (dwarf_eh_frame_section)
1802 asection *got, *txt;
1804 unit.cie = NULL;
1805 unit.dwarf_frame_buffer = dwarf2_read_section (objfile,
1806 dwarf_eh_frame_section);
1808 unit.dwarf_frame_size = bfd_get_section_size (dwarf_eh_frame_section);
1809 unit.dwarf_frame_section = dwarf_eh_frame_section;
1811 /* FIXME: kettenis/20030602: This is the DW_EH_PE_datarel base
1812 that is used for the i386/amd64 target, which currently is
1813 the only target in GCC that supports/uses the
1814 DW_EH_PE_datarel encoding. */
1815 got = bfd_get_section_by_name (unit.abfd, ".got");
1816 if (got)
1817 unit.dbase = got->vma;
1819 /* GCC emits the DW_EH_PE_textrel encoding type on sh and ia64
1820 so far. */
1821 txt = bfd_get_section_by_name (unit.abfd, ".text");
1822 if (txt)
1823 unit.tbase = txt->vma;
1825 frame_ptr = unit.dwarf_frame_buffer;
1826 while (frame_ptr < unit.dwarf_frame_buffer + unit.dwarf_frame_size)
1827 frame_ptr = decode_frame_entry (&unit, frame_ptr, 1);
1830 if (dwarf_frame_section)
1832 unit.cie = NULL;
1833 unit.dwarf_frame_buffer = dwarf2_read_section (objfile,
1834 dwarf_frame_section);
1835 unit.dwarf_frame_size = bfd_get_section_size (dwarf_frame_section);
1836 unit.dwarf_frame_section = dwarf_frame_section;
1838 frame_ptr = unit.dwarf_frame_buffer;
1839 while (frame_ptr < unit.dwarf_frame_buffer + unit.dwarf_frame_size)
1840 frame_ptr = decode_frame_entry (&unit, frame_ptr, 0);
1844 /* Provide a prototype to silence -Wmissing-prototypes. */
1845 void _initialize_dwarf2_frame (void);
1847 void
1848 _initialize_dwarf2_frame (void)
1850 dwarf2_frame_data = gdbarch_data_register_pre_init (dwarf2_frame_init);
1851 dwarf2_frame_objfile_data = register_objfile_data ();