1 /* Frame unwinder for frames with DWARF Call Frame Information.
3 Copyright 2003 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., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
25 #include "dwarf2expr.h"
26 #include "elf/dwarf2.h"
28 #include "frame-base.h"
29 #include "frame-unwind.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). */
48 /* Offset into the .debug_frame section where this CIE was found.
49 Used to identify this CIE. */
52 /* Constant that is factored out of all advance location
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 unsigned char *initial_instructions
;
66 /* Encoding of addresses. */
67 unsigned char encoding
;
69 /* True if a 'z' augmentation existed. */
70 unsigned char saw_z_augmentation
;
72 struct dwarf2_cie
*next
;
75 /* Frame Description Entry (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 unsigned char *instructions
;
92 struct dwarf2_fde
*next
;
95 static struct dwarf2_fde
*dwarf2_frame_find_fde (CORE_ADDR
*pc
);
98 /* Structure describing a frame state. */
102 /* Make certain that 0 maps onto the correct enum value; the
103 corresponding structure is being initialized using memset zero.
104 This indicates that CFI didn't provide any information at all
105 about a register, leaving how to obtain its value totally
108 /* The term "undefined" comes from the DWARF2 CFI spec which this
109 code is moddeling; it indicates that the register's value is
110 "undefined". GCC uses the less formal term "unsaved". Its
111 definition is a combination of REG_UNDEFINED and REG_UNSPECIFIED.
112 The failure to differentiate the two helps explain a few problems
113 with the CFI generated by GCC. */
121 struct dwarf2_frame_state
123 /* Each register save state can be described in terms of a CFA slot,
124 another register, or a location expression. */
125 struct dwarf2_frame_state_reg_info
127 struct dwarf2_frame_state_reg
135 enum dwarf2_reg_rule how
;
139 /* Used to implement DW_CFA_remember_state. */
140 struct dwarf2_frame_state_reg_info
*prev
;
145 unsigned char *cfa_exp
;
152 /* The PC described by the current frame state. */
155 /* Initial register set from the CIE.
156 Used to implement DW_CFA_restore. */
157 struct dwarf2_frame_state_reg_info initial
;
159 /* The information we care about from the CIE. */
162 ULONGEST retaddr_column
;
165 /* Store the length the expression for the CFA in the `cfa_reg' field,
166 which is unused in that case. */
167 #define cfa_exp_len cfa_reg
169 /* Assert that the register set RS is large enough to store NUM_REGS
170 columns. If necessary, enlarge the register set. */
173 dwarf2_frame_state_alloc_regs (struct dwarf2_frame_state_reg_info
*rs
,
176 size_t size
= sizeof (struct dwarf2_frame_state_reg
);
178 if (num_regs
<= rs
->num_regs
)
181 rs
->reg
= (struct dwarf2_frame_state_reg
*)
182 xrealloc (rs
->reg
, num_regs
* size
);
184 /* Initialize newly allocated registers. */
185 memset (rs
->reg
+ rs
->num_regs
, 0, (num_regs
- rs
->num_regs
) * size
);
186 rs
->num_regs
= num_regs
;
189 /* Copy the register columns in register set RS into newly allocated
190 memory and return a pointer to this newly created copy. */
192 static struct dwarf2_frame_state_reg
*
193 dwarf2_frame_state_copy_regs (struct dwarf2_frame_state_reg_info
*rs
)
195 size_t size
= rs
->num_regs
* sizeof (struct dwarf2_frame_state_reg_info
);
196 struct dwarf2_frame_state_reg
*reg
;
198 reg
= (struct dwarf2_frame_state_reg
*) xmalloc (size
);
199 memcpy (reg
, rs
->reg
, size
);
204 /* Release the memory allocated to register set RS. */
207 dwarf2_frame_state_free_regs (struct dwarf2_frame_state_reg_info
*rs
)
211 dwarf2_frame_state_free_regs (rs
->prev
);
218 /* Release the memory allocated to the frame state FS. */
221 dwarf2_frame_state_free (void *p
)
223 struct dwarf2_frame_state
*fs
= p
;
225 dwarf2_frame_state_free_regs (fs
->initial
.prev
);
226 dwarf2_frame_state_free_regs (fs
->regs
.prev
);
227 xfree (fs
->initial
.reg
);
228 xfree (fs
->regs
.reg
);
233 /* Helper functions for execute_stack_op. */
236 read_reg (void *baton
, int reg
)
238 struct frame_info
*next_frame
= (struct frame_info
*) baton
;
242 regnum
= DWARF2_REG_TO_REGNUM (reg
);
244 buf
= (char *) alloca (register_size (current_gdbarch
, regnum
));
245 frame_unwind_register (next_frame
, regnum
, buf
);
246 return extract_typed_address (buf
, builtin_type_void_data_ptr
);
250 read_mem (void *baton
, char *buf
, CORE_ADDR addr
, size_t len
)
252 read_memory (addr
, buf
, len
);
256 no_get_frame_base (void *baton
, unsigned char **start
, size_t *length
)
258 internal_error (__FILE__
, __LINE__
,
259 "Support for DW_OP_fbreg is unimplemented");
263 no_get_tls_address (void *baton
, CORE_ADDR offset
)
265 internal_error (__FILE__
, __LINE__
,
266 "Support for DW_OP_GNU_push_tls_address is unimplemented");
270 execute_stack_op (unsigned char *exp
, ULONGEST len
,
271 struct frame_info
*next_frame
, CORE_ADDR initial
)
273 struct dwarf_expr_context
*ctx
;
276 ctx
= new_dwarf_expr_context ();
277 ctx
->baton
= next_frame
;
278 ctx
->read_reg
= read_reg
;
279 ctx
->read_mem
= read_mem
;
280 ctx
->get_frame_base
= no_get_frame_base
;
281 ctx
->get_tls_address
= no_get_tls_address
;
283 dwarf_expr_push (ctx
, initial
);
284 dwarf_expr_eval (ctx
, exp
, len
);
285 result
= dwarf_expr_fetch (ctx
, 0);
288 result
= read_reg (next_frame
, result
);
290 free_dwarf_expr_context (ctx
);
297 execute_cfa_program (unsigned char *insn_ptr
, unsigned char *insn_end
,
298 struct frame_info
*next_frame
,
299 struct dwarf2_frame_state
*fs
)
301 CORE_ADDR pc
= frame_pc_unwind (next_frame
);
304 while (insn_ptr
< insn_end
&& fs
->pc
<= pc
)
306 unsigned char insn
= *insn_ptr
++;
310 if ((insn
& 0xc0) == DW_CFA_advance_loc
)
311 fs
->pc
+= (insn
& 0x3f) * fs
->code_align
;
312 else if ((insn
& 0xc0) == DW_CFA_offset
)
315 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &utmp
);
316 offset
= utmp
* fs
->data_align
;
317 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
318 fs
->regs
.reg
[reg
].how
= REG_SAVED_OFFSET
;
319 fs
->regs
.reg
[reg
].loc
.offset
= offset
;
321 else if ((insn
& 0xc0) == DW_CFA_restore
)
323 gdb_assert (fs
->initial
.reg
);
325 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
326 fs
->regs
.reg
[reg
] = fs
->initial
.reg
[reg
];
333 fs
->pc
= dwarf2_read_address (insn_ptr
, insn_end
, &bytes_read
);
334 insn_ptr
+= bytes_read
;
337 case DW_CFA_advance_loc1
:
338 utmp
= extract_unsigned_integer (insn_ptr
, 1);
339 fs
->pc
+= utmp
* fs
->code_align
;
342 case DW_CFA_advance_loc2
:
343 utmp
= extract_unsigned_integer (insn_ptr
, 2);
344 fs
->pc
+= utmp
* fs
->code_align
;
347 case DW_CFA_advance_loc4
:
348 utmp
= extract_unsigned_integer (insn_ptr
, 4);
349 fs
->pc
+= utmp
* fs
->code_align
;
353 case DW_CFA_offset_extended
:
354 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, ®
);
355 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &utmp
);
356 offset
= utmp
* fs
->data_align
;
357 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
358 fs
->regs
.reg
[reg
].how
= REG_SAVED_OFFSET
;
359 fs
->regs
.reg
[reg
].loc
.offset
= offset
;
362 case DW_CFA_restore_extended
:
363 gdb_assert (fs
->initial
.reg
);
364 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, ®
);
365 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
366 fs
->regs
.reg
[reg
] = fs
->initial
.reg
[reg
];
369 case DW_CFA_undefined
:
370 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, ®
);
371 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
372 fs
->regs
.reg
[reg
].how
= REG_UNDEFINED
;
375 case DW_CFA_same_value
:
376 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, ®
);
377 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
378 fs
->regs
.reg
[reg
].how
= REG_SAME_VALUE
;
381 case DW_CFA_register
:
382 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, ®
);
383 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &utmp
);
384 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
385 fs
->regs
.reg
[reg
].how
= REG_SAVED_REG
;
386 fs
->regs
.reg
[reg
].loc
.reg
= utmp
;
389 case DW_CFA_remember_state
:
391 struct dwarf2_frame_state_reg_info
*new_rs
;
393 new_rs
= XMALLOC (struct dwarf2_frame_state_reg_info
);
395 fs
->regs
.reg
= dwarf2_frame_state_copy_regs (&fs
->regs
);
396 fs
->regs
.prev
= new_rs
;
400 case DW_CFA_restore_state
:
402 struct dwarf2_frame_state_reg_info
*old_rs
= fs
->regs
.prev
;
406 xfree (fs
->regs
.reg
);
413 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &fs
->cfa_reg
);
414 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &utmp
);
415 fs
->cfa_offset
= utmp
;
416 fs
->cfa_how
= CFA_REG_OFFSET
;
419 case DW_CFA_def_cfa_register
:
420 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &fs
->cfa_reg
);
421 fs
->cfa_how
= CFA_REG_OFFSET
;
424 case DW_CFA_def_cfa_offset
:
425 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &fs
->cfa_offset
);
426 /* cfa_how deliberately not set. */
429 case DW_CFA_def_cfa_expression
:
430 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &fs
->cfa_exp_len
);
431 fs
->cfa_exp
= insn_ptr
;
432 fs
->cfa_how
= CFA_EXP
;
433 insn_ptr
+= fs
->cfa_exp_len
;
436 case DW_CFA_expression
:
437 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, ®
);
438 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
439 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &utmp
);
440 fs
->regs
.reg
[reg
].loc
.exp
= insn_ptr
;
441 fs
->regs
.reg
[reg
].exp_len
= utmp
;
442 fs
->regs
.reg
[reg
].how
= REG_SAVED_EXP
;
449 case DW_CFA_GNU_args_size
:
451 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &utmp
);
455 internal_error (__FILE__
, __LINE__
, "Unknown CFI encountered.");
460 /* Don't allow remember/restore between CIE and FDE programs. */
461 dwarf2_frame_state_free_regs (fs
->regs
.prev
);
462 fs
->regs
.prev
= NULL
;
465 struct dwarf2_frame_cache
467 /* DWARF Call Frame Address. */
470 /* Saved registers, indexed by GDB register number, not by DWARF
472 struct dwarf2_frame_state_reg
*reg
;
475 static struct dwarf2_frame_cache
*
476 dwarf2_frame_cache (struct frame_info
*next_frame
, void **this_cache
)
478 struct cleanup
*old_chain
;
479 const int num_regs
= NUM_REGS
+ NUM_PSEUDO_REGS
;
480 struct dwarf2_frame_cache
*cache
;
481 struct dwarf2_frame_state
*fs
;
482 struct dwarf2_fde
*fde
;
487 /* Allocate a new cache. */
488 cache
= FRAME_OBSTACK_ZALLOC (struct dwarf2_frame_cache
);
489 cache
->reg
= FRAME_OBSTACK_CALLOC (num_regs
, struct dwarf2_frame_state_reg
);
491 /* Allocate and initialize the frame state. */
492 fs
= XMALLOC (struct dwarf2_frame_state
);
493 memset (fs
, 0, sizeof (struct dwarf2_frame_state
));
494 old_chain
= make_cleanup (dwarf2_frame_state_free
, fs
);
498 Note that if NEXT_FRAME is never supposed to return (i.e. a call
499 to abort), the compiler might optimize away the instruction at
500 NEXT_FRAME's return address. As a result the return address will
501 point at some random instruction, and the CFI for that
502 instruction is probably worthless to us. GCC's unwinder solves
503 this problem by substracting 1 from the return address to get an
504 address in the middle of a presumed call instruction (or the
505 instruction in the associated delay slot). This should only be
506 done for "normal" frames and not for resume-type frames (signal
507 handlers, sentinel frames, dummy frames). The function
508 frame_unwind_address_in_block does just this. It's not clear how
509 reliable the method is though; there is the potential for the
510 register state pre-call being different to that on return. */
511 fs
->pc
= frame_unwind_address_in_block (next_frame
);
513 /* Find the correct FDE. */
514 fde
= dwarf2_frame_find_fde (&fs
->pc
);
515 gdb_assert (fde
!= NULL
);
517 /* Extract any interesting information from the CIE. */
518 fs
->data_align
= fde
->cie
->data_alignment_factor
;
519 fs
->code_align
= fde
->cie
->code_alignment_factor
;
520 fs
->retaddr_column
= fde
->cie
->return_address_register
;
522 /* First decode all the insns in the CIE. */
523 execute_cfa_program (fde
->cie
->initial_instructions
,
524 fde
->cie
->end
, next_frame
, fs
);
526 /* Save the initialized register set. */
527 fs
->initial
= fs
->regs
;
528 fs
->initial
.reg
= dwarf2_frame_state_copy_regs (&fs
->regs
);
530 /* Then decode the insns in the FDE up to our target PC. */
531 execute_cfa_program (fde
->instructions
, fde
->end
, next_frame
, fs
);
533 /* Caclulate the CFA. */
537 cache
->cfa
= read_reg (next_frame
, fs
->cfa_reg
);
538 cache
->cfa
+= fs
->cfa_offset
;
543 execute_stack_op (fs
->cfa_exp
, fs
->cfa_exp_len
, next_frame
, 0);
547 internal_error (__FILE__
, __LINE__
, "Unknown CFA rule.");
550 /* Initialize things so that all registers are marked as
555 for (regnum
= 0; regnum
< num_regs
; regnum
++)
556 cache
->reg
[regnum
].how
= REG_UNSPECIFIED
;
559 /* Go through the DWARF2 CFI generated table and save its register
560 location information in the cache. Note that we don't skip the
561 return address column; it's perfectly all right for it to
562 correspond to a real register. If it doesn't correspond to a
563 real register, or if we shouldn't treat it as such,
564 DWARF2_REG_TO_REGNUM should be defined to return a number outside
565 the range [0, NUM_REGS). */
567 int column
; /* CFI speak for "register number". */
569 for (column
= 0; column
< fs
->regs
.num_regs
; column
++)
571 /* Use the GDB register number as the destination index. */
572 int regnum
= DWARF2_REG_TO_REGNUM (column
);
574 /* If there's no corresponding GDB register, ignore it. */
575 if (regnum
< 0 || regnum
>= num_regs
)
578 /* NOTE: cagney/2003-09-05: CFI should specify the disposition
579 of all debug info registers. If it doesn't, complain (but
580 not too loudly). It turns out that GCC assumes that an
581 unspecified register implies "same value" when CFI (draft
582 7) specifies nothing at all. Such a register could equally
583 be interpreted as "undefined". Also note that this check
584 isn't sufficient; it only checks that all registers in the
585 range [0 .. max column] are specified, and won't detect
586 problems when a debug info register falls outside of the
587 table. We need a way of iterating through all the valid
588 DWARF2 register numbers. */
589 if (fs
->regs
.reg
[column
].how
== REG_UNSPECIFIED
)
590 complaint (&symfile_complaints
,
591 "Incomplete CFI data; unspecified registers at 0x%s",
594 cache
->reg
[regnum
] = fs
->regs
.reg
[column
];
598 /* Store the location of the return addess. If the return address
599 column (adjusted) is not the same as GDB's PC_REGNUM, then this
600 implies a copy from the return address column register. */
601 if (fs
->retaddr_column
< fs
->regs
.num_regs
602 && fs
->regs
.reg
[fs
->retaddr_column
].how
!= REG_UNDEFINED
)
604 /* See comment above about a possibly negative PC_REGNUM. If
605 this assertion fails, it's a problem with this code and not
607 gdb_assert (PC_REGNUM
>= 0);
608 cache
->reg
[PC_REGNUM
] = fs
->regs
.reg
[fs
->retaddr_column
];
612 if (DWARF2_REG_TO_REGNUM (fs
->retaddr_column
) != PC_REGNUM
)
614 /* See comment above about PC_REGNUM being negative. If
615 this assertion fails, it's a problem with this code and
616 not the architecture. */
617 gdb_assert (PC_REGNUM
>= 0);
618 cache
->reg
[PC_REGNUM
].loc
.reg
= fs
->retaddr_column
;
619 cache
->reg
[PC_REGNUM
].how
= REG_SAVED_REG
;
623 do_cleanups (old_chain
);
630 dwarf2_frame_this_id (struct frame_info
*next_frame
, void **this_cache
,
631 struct frame_id
*this_id
)
633 struct dwarf2_frame_cache
*cache
=
634 dwarf2_frame_cache (next_frame
, this_cache
);
636 (*this_id
) = frame_id_build (cache
->cfa
, frame_func_unwind (next_frame
));
640 dwarf2_frame_prev_register (struct frame_info
*next_frame
, void **this_cache
,
641 int regnum
, int *optimizedp
,
642 enum lval_type
*lvalp
, CORE_ADDR
*addrp
,
643 int *realnump
, void *valuep
)
645 struct dwarf2_frame_cache
*cache
=
646 dwarf2_frame_cache (next_frame
, this_cache
);
648 switch (cache
->reg
[regnum
].how
)
651 /* If CFI explicitly specified that the value isn't defined,
652 mark it as optimized away; the value isn't available. */
657 if (regnum
== SP_REGNUM
)
659 /* GCC defines the CFA as the value of the stack pointer
660 just before the call instruction is executed. Do other
661 compilers use the same definition? */
662 /* DWARF V3 Draft 7 p102: Typically, the CFA is defined to
663 be the value of the stack pointer at the call site in the
664 previous frame (which may be different from its value on
665 entry to the current frame). */
666 /* DWARF V3 Draft 7 p103: The first column of the rules
667 defines the rule which computes the CFA value; it may be
668 either a register and a signed offset that are added
669 together or a DWARF expression that is evaluated. */
670 /* FIXME: cagney/2003-07-07: I don't understand this. The
671 CFI info should have provided unwind information for the
672 SP register and then pointed ->cfa_reg at it, not the
673 reverse. Assuming that SP_REGNUM isn't negative, there
674 is a very real posibility that CFA is an offset from some
675 other register, having nothing to do with the unwound SP
677 /* FIXME: cagney/2003-09-05: I think I understand. GDB was
678 lumping the two states "unspecified" and "undefined"
679 together. Here SP_REGNUM was "unspecified", GCC assuming
680 that in such a case CFA would be used. This branch of
681 the if statement should be deleted - the problem of
682 SP_REGNUM is now handed by the case REG_UNSPECIFIED
687 /* Store the value. */
688 store_typed_address (valuep
, builtin_type_void_data_ptr
,
694 /* In some cases, for example %eflags on the i386, we have
695 to provide a sane value, even though this register wasn't
696 saved. Assume we can get it from NEXT_FRAME. */
697 frame_unwind_register (next_frame
, regnum
, valuep
);
701 case REG_SAVED_OFFSET
:
703 *lvalp
= lval_memory
;
704 *addrp
= cache
->cfa
+ cache
->reg
[regnum
].loc
.offset
;
708 /* Read the value in from memory. */
709 read_memory (*addrp
, valuep
,
710 register_size (current_gdbarch
, regnum
));
715 regnum
= DWARF2_REG_TO_REGNUM (cache
->reg
[regnum
].loc
.reg
);
716 frame_register_unwind (next_frame
, regnum
,
717 optimizedp
, lvalp
, addrp
, realnump
, valuep
);
722 *lvalp
= lval_memory
;
723 *addrp
= execute_stack_op (cache
->reg
[regnum
].loc
.exp
,
724 cache
->reg
[regnum
].exp_len
,
725 next_frame
, cache
->cfa
);
729 /* Read the value in from memory. */
730 read_memory (*addrp
, valuep
,
731 register_size (current_gdbarch
, regnum
));
735 case REG_UNSPECIFIED
:
736 /* GCC, in its infinite wisdom decided to not provide unwind
737 information for registers that are "same value". Since
738 DWARF2 (3 draft 7) doesn't define such behavior, said
739 registers are actually undefined (which is different to CFI
740 "undefined"). Code above issues a complaint about this.
741 Here just fudge the books, assume GCC, and that the value is
742 more inner on the stack. */
743 if (SP_REGNUM
>= 0 && regnum
== SP_REGNUM
)
745 /* Can things get worse? Yep! One of the registers GCC
746 forgot to provide unwind information for was the stack
747 pointer. Outch! GCC appears to assumes that the CFA
748 address can be used - after all it points to the inner
749 most address of the previous frame before the function
750 call and that's always the same as the stack pointer on
751 return, right? Wrong. See GCC's i386 STDCALL option for
752 an ABI that has a different entry and return stack
754 /* DWARF V3 Draft 7 p102: Typically, the CFA is defined to
755 be the value of the stack pointer at the call site in the
756 previous frame (which may be different from its value on
757 entry to the current frame). */
758 /* DWARF V3 Draft 7 p103: The first column of the rules
759 defines the rule which computes the CFA value; it may be
760 either a register and a signed offset that are added
761 together or a DWARF expression that is evaluated. */
762 /* NOTE: cagney/2003-09-05: Should issue a complaint.
763 Unfortunately it turns out that DWARF2 CFI has a problem.
764 Since CFI specifies the location at which a register was
765 saved (not its value) it isn't possible to specify
766 something like "unwound(REG) == REG + constant" using CFI
767 as will almost always occure with the stack pointer. I
768 guess CFI should be point SP at CFA. Ref: danielj,
769 "Describing unsaved stack pointers", posted to dwarf2
776 /* Store the value. */
777 store_typed_address (valuep
, builtin_type_void_data_ptr
,
781 /* Assume that the register can be found in the next inner
783 frame_register_unwind (next_frame
, regnum
,
784 optimizedp
, lvalp
, addrp
, realnump
, valuep
);
788 frame_register_unwind (next_frame
, regnum
,
789 optimizedp
, lvalp
, addrp
, realnump
, valuep
);
793 internal_error (__FILE__
, __LINE__
, "Unknown register rule.");
797 static const struct frame_unwind dwarf2_frame_unwind
=
800 dwarf2_frame_this_id
,
801 dwarf2_frame_prev_register
804 const struct frame_unwind
*
805 dwarf2_frame_sniffer (struct frame_info
*next_frame
)
807 /* Grab an address that is guarenteed to reside somewhere within the
808 function. frame_pc_unwind(), for a no-return next function, can
809 end up returning something past the end of this function's body. */
810 CORE_ADDR block_addr
= frame_unwind_address_in_block (next_frame
);
811 if (dwarf2_frame_find_fde (&block_addr
))
812 return &dwarf2_frame_unwind
;
818 /* There is no explicitly defined relationship between the CFA and the
819 location of frame's local variables and arguments/parameters.
820 Therefore, frame base methods on this page should probably only be
821 used as a last resort, just to avoid printing total garbage as a
822 response to the "info frame" command. */
825 dwarf2_frame_base_address (struct frame_info
*next_frame
, void **this_cache
)
827 struct dwarf2_frame_cache
*cache
=
828 dwarf2_frame_cache (next_frame
, this_cache
);
833 static const struct frame_base dwarf2_frame_base
=
835 &dwarf2_frame_unwind
,
836 dwarf2_frame_base_address
,
837 dwarf2_frame_base_address
,
838 dwarf2_frame_base_address
841 const struct frame_base
*
842 dwarf2_frame_base_sniffer (struct frame_info
*next_frame
)
844 CORE_ADDR pc
= frame_pc_unwind (next_frame
);
845 if (dwarf2_frame_find_fde (&pc
))
846 return &dwarf2_frame_base
;
851 /* A minimal decoding of DWARF2 compilation units. We only decode
852 what's needed to get to the call frame information. */
856 /* Keep the bfd convenient. */
859 struct objfile
*objfile
;
861 /* Linked list of CIEs for this object. */
862 struct dwarf2_cie
*cie
;
864 /* Address size for this unit - from unit header. */
865 unsigned char addr_size
;
867 /* Pointer to the .debug_frame section loaded into memory. */
868 char *dwarf_frame_buffer
;
870 /* Length of the loaded .debug_frame section. */
871 unsigned long dwarf_frame_size
;
873 /* Pointer to the .debug_frame section. */
874 asection
*dwarf_frame_section
;
876 /* Base for DW_EH_PE_datarel encodings. */
879 /* Base for DW_EH_PE_textrel encodings. */
883 const struct objfile_data
*dwarf2_frame_data
;
886 read_1_byte (bfd
*bfd
, char *buf
)
888 return bfd_get_8 (abfd
, (bfd_byte
*) buf
);
892 read_4_bytes (bfd
*abfd
, char *buf
)
894 return bfd_get_32 (abfd
, (bfd_byte
*) buf
);
898 read_8_bytes (bfd
*abfd
, char *buf
)
900 return bfd_get_64 (abfd
, (bfd_byte
*) buf
);
904 read_unsigned_leb128 (bfd
*abfd
, char *buf
, unsigned int *bytes_read_ptr
)
907 unsigned int num_read
;
917 byte
= bfd_get_8 (abfd
, (bfd_byte
*) buf
);
920 result
|= ((byte
& 0x7f) << shift
);
925 *bytes_read_ptr
= num_read
;
931 read_signed_leb128 (bfd
*abfd
, char *buf
, unsigned int *bytes_read_ptr
)
935 unsigned int num_read
;
944 byte
= bfd_get_8 (abfd
, (bfd_byte
*) buf
);
947 result
|= ((byte
& 0x7f) << shift
);
952 if ((shift
< 32) && (byte
& 0x40))
953 result
|= -(1 << shift
);
955 *bytes_read_ptr
= num_read
;
961 read_initial_length (bfd
*abfd
, char *buf
, unsigned int *bytes_read_ptr
)
965 result
= bfd_get_32 (abfd
, (bfd_byte
*) buf
);
966 if (result
== 0xffffffff)
968 result
= bfd_get_64 (abfd
, (bfd_byte
*) buf
+ 4);
969 *bytes_read_ptr
= 12;
978 /* Pointer encoding helper functions. */
980 /* GCC supports exception handling based on DWARF2 CFI. However, for
981 technical reasons, it encodes addresses in its FDE's in a different
982 way. Several "pointer encodings" are supported. The encoding
983 that's used for a particular FDE is determined by the 'R'
984 augmentation in the associated CIE. The argument of this
985 augmentation is a single byte.
987 The address can be encoded as 2 bytes, 4 bytes, 8 bytes, or as a
988 LEB128. This is encoded in bits 0, 1 and 2. Bit 3 encodes whether
989 the address is signed or unsigned. Bits 4, 5 and 6 encode how the
990 address should be interpreted (absolute, relative to the current
991 position in the FDE, ...). Bit 7, indicates that the address
992 should be dereferenced. */
995 encoding_for_size (unsigned int size
)
1000 return DW_EH_PE_udata2
;
1002 return DW_EH_PE_udata4
;
1004 return DW_EH_PE_udata8
;
1006 internal_error (__FILE__
, __LINE__
, "Unsupported address size");
1011 size_of_encoded_value (unsigned char encoding
)
1013 if (encoding
== DW_EH_PE_omit
)
1016 switch (encoding
& 0x07)
1018 case DW_EH_PE_absptr
:
1019 return TYPE_LENGTH (builtin_type_void_data_ptr
);
1020 case DW_EH_PE_udata2
:
1022 case DW_EH_PE_udata4
:
1024 case DW_EH_PE_udata8
:
1027 internal_error (__FILE__
, __LINE__
, "Invalid or unsupported encoding");
1032 read_encoded_value (struct comp_unit
*unit
, unsigned char encoding
,
1033 char *buf
, unsigned int *bytes_read_ptr
)
1035 int ptr_len
= size_of_encoded_value (DW_EH_PE_absptr
);
1039 /* GCC currently doesn't generate DW_EH_PE_indirect encodings for
1041 if (encoding
& DW_EH_PE_indirect
)
1042 internal_error (__FILE__
, __LINE__
,
1043 "Unsupported encoding: DW_EH_PE_indirect");
1045 *bytes_read_ptr
= 0;
1047 switch (encoding
& 0x70)
1049 case DW_EH_PE_absptr
:
1052 case DW_EH_PE_pcrel
:
1053 base
= bfd_get_section_vma (unit
->bfd
, unit
->dwarf_frame_section
);
1054 base
+= (buf
- unit
->dwarf_frame_buffer
);
1056 case DW_EH_PE_datarel
:
1059 case DW_EH_PE_textrel
:
1062 case DW_EH_PE_aligned
:
1064 offset
= buf
- unit
->dwarf_frame_buffer
;
1065 if ((offset
% ptr_len
) != 0)
1067 *bytes_read_ptr
= ptr_len
- (offset
% ptr_len
);
1068 buf
+= *bytes_read_ptr
;
1072 internal_error (__FILE__
, __LINE__
, "Invalid or unsupported encoding");
1075 if ((encoding
& 0x0f) == 0x00)
1076 encoding
|= encoding_for_size (ptr_len
);
1078 switch (encoding
& 0x0f)
1080 case DW_EH_PE_udata2
:
1081 *bytes_read_ptr
+= 2;
1082 return (base
+ bfd_get_16 (unit
->abfd
, (bfd_byte
*) buf
));
1083 case DW_EH_PE_udata4
:
1084 *bytes_read_ptr
+= 4;
1085 return (base
+ bfd_get_32 (unit
->abfd
, (bfd_byte
*) buf
));
1086 case DW_EH_PE_udata8
:
1087 *bytes_read_ptr
+= 8;
1088 return (base
+ bfd_get_64 (unit
->abfd
, (bfd_byte
*) buf
));
1089 case DW_EH_PE_sdata2
:
1090 *bytes_read_ptr
+= 2;
1091 return (base
+ bfd_get_signed_16 (unit
->abfd
, (bfd_byte
*) buf
));
1092 case DW_EH_PE_sdata4
:
1093 *bytes_read_ptr
+= 4;
1094 return (base
+ bfd_get_signed_32 (unit
->abfd
, (bfd_byte
*) buf
));
1095 case DW_EH_PE_sdata8
:
1096 *bytes_read_ptr
+= 8;
1097 return (base
+ bfd_get_signed_64 (unit
->abfd
, (bfd_byte
*) buf
));
1099 internal_error (__FILE__
, __LINE__
, "Invalid or unsupported encoding");
1104 /* GCC uses a single CIE for all FDEs in a .debug_frame section.
1105 That's why we use a simple linked list here. */
1107 static struct dwarf2_cie
*
1108 find_cie (struct comp_unit
*unit
, ULONGEST cie_pointer
)
1110 struct dwarf2_cie
*cie
= unit
->cie
;
1114 if (cie
->cie_pointer
== cie_pointer
)
1124 add_cie (struct comp_unit
*unit
, struct dwarf2_cie
*cie
)
1126 cie
->next
= unit
->cie
;
1130 /* Find the FDE for *PC. Return a pointer to the FDE, and store the
1131 inital location associated with it into *PC. */
1133 static struct dwarf2_fde
*
1134 dwarf2_frame_find_fde (CORE_ADDR
*pc
)
1136 struct objfile
*objfile
;
1138 ALL_OBJFILES (objfile
)
1140 struct dwarf2_fde
*fde
;
1143 fde
= objfile_data (objfile
, dwarf2_frame_data
);
1147 gdb_assert (objfile
->section_offsets
);
1148 offset
= ANOFFSET (objfile
->section_offsets
, SECT_OFF_TEXT (objfile
));
1152 if (*pc
>= fde
->initial_location
+ offset
1153 && *pc
< fde
->initial_location
+ offset
+ fde
->address_range
)
1155 *pc
= fde
->initial_location
+ offset
;
1167 add_fde (struct comp_unit
*unit
, struct dwarf2_fde
*fde
)
1169 fde
->next
= objfile_data (unit
->objfile
, dwarf2_frame_data
);
1170 set_objfile_data (unit
->objfile
, dwarf2_frame_data
, fde
);
1173 #ifdef CC_HAS_LONG_LONG
1174 #define DW64_CIE_ID 0xffffffffffffffffULL
1176 #define DW64_CIE_ID ~0
1179 static char *decode_frame_entry (struct comp_unit
*unit
, char *start
,
1182 /* Decode the next CIE or FDE. Return NULL if invalid input, otherwise
1183 the next byte to be processed. */
1185 decode_frame_entry_1 (struct comp_unit
*unit
, char *start
, int eh_frame_p
)
1189 unsigned int bytes_read
;
1192 ULONGEST cie_pointer
;
1196 length
= read_initial_length (unit
->abfd
, buf
, &bytes_read
);
1200 /* Are we still within the section? */
1201 if (end
> unit
->dwarf_frame_buffer
+ unit
->dwarf_frame_size
)
1207 /* Distinguish between 32 and 64-bit encoded frame info. */
1208 dwarf64_p
= (bytes_read
== 12);
1210 /* In a .eh_frame section, zero is used to distinguish CIEs from FDEs. */
1214 cie_id
= DW64_CIE_ID
;
1220 cie_pointer
= read_8_bytes (unit
->abfd
, buf
);
1225 cie_pointer
= read_4_bytes (unit
->abfd
, buf
);
1229 if (cie_pointer
== cie_id
)
1231 /* This is a CIE. */
1232 struct dwarf2_cie
*cie
;
1235 /* Record the offset into the .debug_frame section of this CIE. */
1236 cie_pointer
= start
- unit
->dwarf_frame_buffer
;
1238 /* Check whether we've already read it. */
1239 if (find_cie (unit
, cie_pointer
))
1242 cie
= (struct dwarf2_cie
*)
1243 obstack_alloc (&unit
->objfile
->psymbol_obstack
,
1244 sizeof (struct dwarf2_cie
));
1245 cie
->initial_instructions
= NULL
;
1246 cie
->cie_pointer
= cie_pointer
;
1248 /* The encoding for FDE's in a normal .debug_frame section
1249 depends on the target address size as specified in the
1250 Compilation Unit Header. */
1251 cie
->encoding
= encoding_for_size (unit
->addr_size
);
1253 /* Check version number. */
1254 if (read_1_byte (unit
->abfd
, buf
) != DW_CIE_VERSION
)
1258 /* Interpret the interesting bits of the augmentation. */
1260 buf
= augmentation
+ strlen (augmentation
) + 1;
1262 /* The GCC 2.x "eh" augmentation has a pointer immediately
1263 following the augmentation string, so it must be handled
1265 if (augmentation
[0] == 'e' && augmentation
[1] == 'h')
1268 buf
+= TYPE_LENGTH (builtin_type_void_data_ptr
);
1272 cie
->code_alignment_factor
=
1273 read_unsigned_leb128 (unit
->abfd
, buf
, &bytes_read
);
1276 cie
->data_alignment_factor
=
1277 read_signed_leb128 (unit
->abfd
, buf
, &bytes_read
);
1280 cie
->return_address_register
= read_1_byte (unit
->abfd
, buf
);
1283 cie
->saw_z_augmentation
= (*augmentation
== 'z');
1284 if (cie
->saw_z_augmentation
)
1288 length
= read_unsigned_leb128 (unit
->abfd
, buf
, &bytes_read
);
1292 cie
->initial_instructions
= buf
+ length
;
1296 while (*augmentation
)
1298 /* "L" indicates a byte showing how the LSDA pointer is encoded. */
1299 if (*augmentation
== 'L')
1306 /* "R" indicates a byte indicating how FDE addresses are encoded. */
1307 else if (*augmentation
== 'R')
1309 cie
->encoding
= *buf
++;
1313 /* "P" indicates a personality routine in the CIE augmentation. */
1314 else if (*augmentation
== 'P')
1317 buf
+= size_of_encoded_value (*buf
++);
1321 /* Otherwise we have an unknown augmentation.
1322 Bail out unless we saw a 'z' prefix. */
1325 if (cie
->initial_instructions
== NULL
)
1328 /* Skip unknown augmentations. */
1329 buf
= cie
->initial_instructions
;
1334 cie
->initial_instructions
= buf
;
1337 add_cie (unit
, cie
);
1341 /* This is a FDE. */
1342 struct dwarf2_fde
*fde
;
1344 /* In an .eh_frame section, the CIE pointer is the delta between the
1345 address within the FDE where the CIE pointer is stored and the
1346 address of the CIE. Convert it to an offset into the .eh_frame
1350 cie_pointer
= buf
- unit
->dwarf_frame_buffer
- cie_pointer
;
1351 cie_pointer
-= (dwarf64_p
? 8 : 4);
1354 /* In either case, validate the result is still within the section. */
1355 if (cie_pointer
>= unit
->dwarf_frame_size
)
1358 fde
= (struct dwarf2_fde
*)
1359 obstack_alloc (&unit
->objfile
->psymbol_obstack
,
1360 sizeof (struct dwarf2_fde
));
1361 fde
->cie
= find_cie (unit
, cie_pointer
);
1362 if (fde
->cie
== NULL
)
1364 decode_frame_entry (unit
, unit
->dwarf_frame_buffer
+ cie_pointer
,
1366 fde
->cie
= find_cie (unit
, cie_pointer
);
1369 gdb_assert (fde
->cie
!= NULL
);
1371 fde
->initial_location
=
1372 read_encoded_value (unit
, fde
->cie
->encoding
, buf
, &bytes_read
);
1375 fde
->address_range
=
1376 read_encoded_value (unit
, fde
->cie
->encoding
& 0x0f, buf
, &bytes_read
);
1379 /* A 'z' augmentation in the CIE implies the presence of an
1380 augmentation field in the FDE as well. The only thing known
1381 to be in here at present is the LSDA entry for EH. So we
1382 can skip the whole thing. */
1383 if (fde
->cie
->saw_z_augmentation
)
1387 length
= read_unsigned_leb128 (unit
->abfd
, buf
, &bytes_read
);
1388 buf
+= bytes_read
+ length
;
1393 fde
->instructions
= buf
;
1396 add_fde (unit
, fde
);
1402 /* Read a CIE or FDE in BUF and decode it. */
1404 decode_frame_entry (struct comp_unit
*unit
, char *start
, int eh_frame_p
)
1406 enum { NONE
, ALIGN4
, ALIGN8
, FAIL
} workaround
= NONE
;
1409 ptrdiff_t start_offset
;
1413 ret
= decode_frame_entry_1 (unit
, start
, eh_frame_p
);
1417 /* We have corrupt input data of some form. */
1419 /* ??? Try, weakly, to work around compiler/assembler/linker bugs
1420 and mismatches wrt padding and alignment of debug sections. */
1421 /* Note that there is no requirement in the standard for any
1422 alignment at all in the frame unwind sections. Testing for
1423 alignment before trying to interpret data would be incorrect.
1425 However, GCC traditionally arranged for frame sections to be
1426 sized such that the FDE length and CIE fields happen to be
1427 aligned (in theory, for performance). This, unfortunately,
1428 was done with .align directives, which had the side effect of
1429 forcing the section to be aligned by the linker.
1431 This becomes a problem when you have some other producer that
1432 creates frame sections that are not as strictly aligned. That
1433 produces a hole in the frame info that gets filled by the
1436 The GCC behaviour is arguably a bug, but it's effectively now
1437 part of the ABI, so we're now stuck with it, at least at the
1438 object file level. A smart linker may decide, in the process
1439 of compressing duplicate CIE information, that it can rewrite
1440 the entire output section without this extra padding. */
1442 start_offset
= start
- unit
->dwarf_frame_buffer
;
1443 if (workaround
< ALIGN4
&& (start_offset
& 3) != 0)
1445 start
+= 4 - (start_offset
& 3);
1446 workaround
= ALIGN4
;
1449 if (workaround
< ALIGN8
&& (start_offset
& 7) != 0)
1451 start
+= 8 - (start_offset
& 7);
1452 workaround
= ALIGN8
;
1456 /* Nothing left to try. Arrange to return as if we've consumed
1457 the entire input section. Hopefully we'll get valid info from
1458 the other of .debug_frame/.eh_frame. */
1460 ret
= unit
->dwarf_frame_buffer
+ unit
->dwarf_frame_size
;
1470 complaint (&symfile_complaints
,
1471 "Corrupt data in %s:%s; align 4 workaround apparently succeeded",
1472 unit
->dwarf_frame_section
->owner
->filename
,
1473 unit
->dwarf_frame_section
->name
);
1477 complaint (&symfile_complaints
,
1478 "Corrupt data in %s:%s; align 8 workaround apparently succeeded",
1479 unit
->dwarf_frame_section
->owner
->filename
,
1480 unit
->dwarf_frame_section
->name
);
1484 complaint (&symfile_complaints
,
1485 "Corrupt data in %s:%s",
1486 unit
->dwarf_frame_section
->owner
->filename
,
1487 unit
->dwarf_frame_section
->name
);
1496 /* FIXME: kettenis/20030504: This still needs to be integrated with
1497 dwarf2read.c in a better way. */
1499 /* Imported from dwarf2read.c. */
1500 extern asection
*dwarf_frame_section
;
1501 extern asection
*dwarf_eh_frame_section
;
1503 /* Imported from dwarf2read.c. */
1504 extern char *dwarf2_read_section (struct objfile
*objfile
, asection
*sectp
);
1507 dwarf2_build_frame_info (struct objfile
*objfile
)
1509 struct comp_unit unit
;
1512 /* Build a minimal decoding of the DWARF2 compilation unit. */
1513 unit
.abfd
= objfile
->obfd
;
1514 unit
.objfile
= objfile
;
1515 unit
.addr_size
= objfile
->obfd
->arch_info
->bits_per_address
/ 8;
1519 /* First add the information from the .eh_frame section. That way,
1520 the FDEs from that section are searched last. */
1521 if (dwarf_eh_frame_section
)
1523 asection
*got
, *txt
;
1526 unit
.dwarf_frame_buffer
= dwarf2_read_section (objfile
,
1527 dwarf_eh_frame_section
);
1529 unit
.dwarf_frame_size
1530 = bfd_get_section_size_before_reloc (dwarf_eh_frame_section
);
1531 unit
.dwarf_frame_section
= dwarf_eh_frame_section
;
1533 /* FIXME: kettenis/20030602: This is the DW_EH_PE_datarel base
1534 that is used for the i386/amd64 target, which currently is
1535 the only target in GCC that supports/uses the
1536 DW_EH_PE_datarel encoding. */
1537 got
= bfd_get_section_by_name (unit
.abfd
, ".got");
1539 unit
.dbase
= got
->vma
;
1541 /* GCC emits the DW_EH_PE_textrel encoding type on sh and ia64
1543 txt
= bfd_get_section_by_name (unit
.abfd
, ".text");
1545 unit
.tbase
= txt
->vma
;
1547 frame_ptr
= unit
.dwarf_frame_buffer
;
1548 while (frame_ptr
< unit
.dwarf_frame_buffer
+ unit
.dwarf_frame_size
)
1549 frame_ptr
= decode_frame_entry (&unit
, frame_ptr
, 1);
1552 if (dwarf_frame_section
)
1555 unit
.dwarf_frame_buffer
= dwarf2_read_section (objfile
,
1556 dwarf_frame_section
);
1557 unit
.dwarf_frame_size
1558 = bfd_get_section_size_before_reloc (dwarf_frame_section
);
1559 unit
.dwarf_frame_section
= dwarf_frame_section
;
1561 frame_ptr
= unit
.dwarf_frame_buffer
;
1562 while (frame_ptr
< unit
.dwarf_frame_buffer
+ unit
.dwarf_frame_size
)
1563 frame_ptr
= decode_frame_entry (&unit
, frame_ptr
, 0);
1567 /* Provide a prototype to silence -Wmissing-prototypes. */
1568 void _initialize_dwarf2_frame (void);
1571 _initialize_dwarf2_frame (void)
1573 dwarf2_frame_data
= register_objfile_data ();