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 it's 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
111 /* NOTE: cagney/2003-09-08: GCC uses the less formal term "unsaved"
112 - it's definition is a combination of REG_UNDEFINED and
113 REG_UNSPECIFIED - the failure to differentiate the two helps
114 explain a few problems with the CFI GCC outputs. */
122 struct dwarf2_frame_state
124 /* Each register save state can be described in terms of a CFA slot,
125 another register, or a location expression. */
126 struct dwarf2_frame_state_reg_info
128 struct dwarf2_frame_state_reg
136 enum dwarf2_reg_rule how
;
140 /* Used to implement DW_CFA_remember_state. */
141 struct dwarf2_frame_state_reg_info
*prev
;
146 unsigned char *cfa_exp
;
153 /* The PC described by the current frame state. */
156 /* Initial register set from the CIE.
157 Used to implement DW_CFA_restore. */
158 struct dwarf2_frame_state_reg_info initial
;
160 /* The information we care about from the CIE. */
163 ULONGEST retaddr_column
;
166 /* Store the length the expression for the CFA in the `cfa_reg' field,
167 which is unused in that case. */
168 #define cfa_exp_len cfa_reg
170 /* Assert that the register set RS is large enough to store NUM_REGS
171 columns. If necessary, enlarge the register set. */
174 dwarf2_frame_state_alloc_regs (struct dwarf2_frame_state_reg_info
*rs
,
177 size_t size
= sizeof (struct dwarf2_frame_state_reg
);
179 if (num_regs
<= rs
->num_regs
)
182 rs
->reg
= (struct dwarf2_frame_state_reg
*)
183 xrealloc (rs
->reg
, num_regs
* size
);
185 /* Initialize newly allocated registers. */
186 memset (rs
->reg
+ rs
->num_regs
, 0, (num_regs
- rs
->num_regs
) * size
);
187 rs
->num_regs
= num_regs
;
190 /* Copy the register columns in register set RS into newly allocated
191 memory and return a pointer to this newly created copy. */
193 static struct dwarf2_frame_state_reg
*
194 dwarf2_frame_state_copy_regs (struct dwarf2_frame_state_reg_info
*rs
)
196 size_t size
= rs
->num_regs
* sizeof (struct dwarf2_frame_state_reg_info
);
197 struct dwarf2_frame_state_reg
*reg
;
199 reg
= (struct dwarf2_frame_state_reg
*) xmalloc (size
);
200 memcpy (reg
, rs
->reg
, size
);
205 /* Release the memory allocated to register set RS. */
208 dwarf2_frame_state_free_regs (struct dwarf2_frame_state_reg_info
*rs
)
212 dwarf2_frame_state_free_regs (rs
->prev
);
219 /* Release the memory allocated to the frame state FS. */
222 dwarf2_frame_state_free (void *p
)
224 struct dwarf2_frame_state
*fs
= p
;
226 dwarf2_frame_state_free_regs (fs
->initial
.prev
);
227 dwarf2_frame_state_free_regs (fs
->regs
.prev
);
228 xfree (fs
->initial
.reg
);
229 xfree (fs
->regs
.reg
);
234 /* Helper functions for execute_stack_op. */
237 read_reg (void *baton
, int reg
)
239 struct frame_info
*next_frame
= (struct frame_info
*) baton
;
243 regnum
= DWARF2_REG_TO_REGNUM (reg
);
245 buf
= (char *) alloca (register_size (current_gdbarch
, regnum
));
246 frame_unwind_register (next_frame
, regnum
, buf
);
247 return extract_typed_address (buf
, builtin_type_void_data_ptr
);
251 read_mem (void *baton
, char *buf
, CORE_ADDR addr
, size_t len
)
253 read_memory (addr
, buf
, len
);
257 no_get_frame_base (void *baton
, unsigned char **start
, size_t *length
)
259 internal_error (__FILE__
, __LINE__
,
260 "Support for DW_OP_fbreg is unimplemented");
264 no_get_tls_address (void *baton
, CORE_ADDR offset
)
266 internal_error (__FILE__
, __LINE__
,
267 "Support for DW_OP_GNU_push_tls_address is unimplemented");
271 execute_stack_op (unsigned char *exp
, ULONGEST len
,
272 struct frame_info
*next_frame
, CORE_ADDR initial
)
274 struct dwarf_expr_context
*ctx
;
277 ctx
= new_dwarf_expr_context ();
278 ctx
->baton
= next_frame
;
279 ctx
->read_reg
= read_reg
;
280 ctx
->read_mem
= read_mem
;
281 ctx
->get_frame_base
= no_get_frame_base
;
282 ctx
->get_tls_address
= no_get_tls_address
;
284 dwarf_expr_push (ctx
, initial
);
285 dwarf_expr_eval (ctx
, exp
, len
);
286 result
= dwarf_expr_fetch (ctx
, 0);
289 result
= read_reg (next_frame
, result
);
291 free_dwarf_expr_context (ctx
);
298 execute_cfa_program (unsigned char *insn_ptr
, unsigned char *insn_end
,
299 struct frame_info
*next_frame
,
300 struct dwarf2_frame_state
*fs
)
302 CORE_ADDR pc
= frame_pc_unwind (next_frame
);
305 while (insn_ptr
< insn_end
&& fs
->pc
<= pc
)
307 unsigned char insn
= *insn_ptr
++;
311 if ((insn
& 0xc0) == DW_CFA_advance_loc
)
312 fs
->pc
+= (insn
& 0x3f) * fs
->code_align
;
313 else if ((insn
& 0xc0) == DW_CFA_offset
)
316 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &utmp
);
317 offset
= utmp
* fs
->data_align
;
318 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
319 fs
->regs
.reg
[reg
].how
= REG_SAVED_OFFSET
;
320 fs
->regs
.reg
[reg
].loc
.offset
= offset
;
322 else if ((insn
& 0xc0) == DW_CFA_restore
)
324 gdb_assert (fs
->initial
.reg
);
326 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
327 fs
->regs
.reg
[reg
] = fs
->initial
.reg
[reg
];
334 fs
->pc
= dwarf2_read_address (insn_ptr
, insn_end
, &bytes_read
);
335 insn_ptr
+= bytes_read
;
338 case DW_CFA_advance_loc1
:
339 utmp
= extract_unsigned_integer (insn_ptr
, 1);
340 fs
->pc
+= utmp
* fs
->code_align
;
343 case DW_CFA_advance_loc2
:
344 utmp
= extract_unsigned_integer (insn_ptr
, 2);
345 fs
->pc
+= utmp
* fs
->code_align
;
348 case DW_CFA_advance_loc4
:
349 utmp
= extract_unsigned_integer (insn_ptr
, 4);
350 fs
->pc
+= utmp
* fs
->code_align
;
354 case DW_CFA_offset_extended
:
355 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, ®
);
356 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &utmp
);
357 offset
= utmp
* fs
->data_align
;
358 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
359 fs
->regs
.reg
[reg
].how
= REG_SAVED_OFFSET
;
360 fs
->regs
.reg
[reg
].loc
.offset
= offset
;
363 case DW_CFA_restore_extended
:
364 gdb_assert (fs
->initial
.reg
);
365 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, ®
);
366 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
367 fs
->regs
.reg
[reg
] = fs
->initial
.reg
[reg
];
370 case DW_CFA_undefined
:
371 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, ®
);
372 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
373 fs
->regs
.reg
[reg
].how
= REG_UNDEFINED
;
376 case DW_CFA_same_value
:
377 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, ®
);
378 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
379 fs
->regs
.reg
[reg
].how
= REG_SAME_VALUE
;
382 case DW_CFA_register
:
383 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, ®
);
384 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &utmp
);
385 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
386 fs
->regs
.reg
[reg
].how
= REG_SAVED_REG
;
387 fs
->regs
.reg
[reg
].loc
.reg
= utmp
;
390 case DW_CFA_remember_state
:
392 struct dwarf2_frame_state_reg_info
*new_rs
;
394 new_rs
= XMALLOC (struct dwarf2_frame_state_reg_info
);
396 fs
->regs
.reg
= dwarf2_frame_state_copy_regs (&fs
->regs
);
397 fs
->regs
.prev
= new_rs
;
401 case DW_CFA_restore_state
:
403 struct dwarf2_frame_state_reg_info
*old_rs
= fs
->regs
.prev
;
407 xfree (fs
->regs
.reg
);
414 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &fs
->cfa_reg
);
415 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &utmp
);
416 fs
->cfa_offset
= utmp
;
417 fs
->cfa_how
= CFA_REG_OFFSET
;
420 case DW_CFA_def_cfa_register
:
421 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &fs
->cfa_reg
);
422 fs
->cfa_how
= CFA_REG_OFFSET
;
425 case DW_CFA_def_cfa_offset
:
426 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &fs
->cfa_offset
);
427 /* cfa_how deliberately not set. */
430 case DW_CFA_def_cfa_expression
:
431 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &fs
->cfa_exp_len
);
432 fs
->cfa_exp
= insn_ptr
;
433 fs
->cfa_how
= CFA_EXP
;
434 insn_ptr
+= fs
->cfa_exp_len
;
437 case DW_CFA_expression
:
438 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, ®
);
439 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
440 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &utmp
);
441 fs
->regs
.reg
[reg
].loc
.exp
= insn_ptr
;
442 fs
->regs
.reg
[reg
].exp_len
= utmp
;
443 fs
->regs
.reg
[reg
].how
= REG_SAVED_EXP
;
450 case DW_CFA_GNU_args_size
:
452 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &utmp
);
456 internal_error (__FILE__
, __LINE__
, "Unknown CFI encountered.");
461 /* Don't allow remember/restore between CIE and FDE programs. */
462 dwarf2_frame_state_free_regs (fs
->regs
.prev
);
463 fs
->regs
.prev
= NULL
;
466 struct dwarf2_frame_cache
468 /* DWARF Call Frame Address. */
471 /* Saved registers, indexed by GDB register number, not by DWARF
473 struct dwarf2_frame_state_reg
*reg
;
476 static struct dwarf2_frame_cache
*
477 dwarf2_frame_cache (struct frame_info
*next_frame
, void **this_cache
)
479 struct cleanup
*old_chain
;
480 const int num_regs
= NUM_REGS
+ NUM_PSEUDO_REGS
;
481 struct dwarf2_frame_cache
*cache
;
482 struct dwarf2_frame_state
*fs
;
483 struct dwarf2_fde
*fde
;
488 /* Allocate a new cache. */
489 cache
= FRAME_OBSTACK_ZALLOC (struct dwarf2_frame_cache
);
490 cache
->reg
= FRAME_OBSTACK_CALLOC (num_regs
, struct dwarf2_frame_state_reg
);
492 /* Allocate and initialize the frame state. */
493 fs
= XMALLOC (struct dwarf2_frame_state
);
494 memset (fs
, 0, sizeof (struct dwarf2_frame_state
));
495 old_chain
= make_cleanup (dwarf2_frame_state_free
, fs
);
499 Note that if NEXT_FRAME is never supposed to return (i.e. a call
500 to abort), the compiler might optimize away the instruction at
501 NEXT_FRAME's return address. As a result the return address will
502 point at some random instruction, and the CFI for that
503 instruction is probably wortless to us. GCC's unwinder solves
504 this problem by substracting 1 from the return address to get an
505 address in the middle of a presumed call instruction (or the
506 instruction in the associated delay slot). This should only be
507 done for "normal" frames and not for resume-type frames (signal
508 handlers, sentinel frames, dummy frames).
510 frame_unwind_address_in_block does just this.
512 It's not clear how reliable the method is though - there is the
513 potential for the register state pre-call being different to that
515 fs
->pc
= frame_unwind_address_in_block (next_frame
);
517 /* Find the correct FDE. */
518 fde
= dwarf2_frame_find_fde (&fs
->pc
);
519 gdb_assert (fde
!= NULL
);
521 /* Extract any interesting information from the CIE. */
522 fs
->data_align
= fde
->cie
->data_alignment_factor
;
523 fs
->code_align
= fde
->cie
->code_alignment_factor
;
524 fs
->retaddr_column
= fde
->cie
->return_address_register
;
526 /* First decode all the insns in the CIE. */
527 execute_cfa_program (fde
->cie
->initial_instructions
,
528 fde
->cie
->end
, next_frame
, fs
);
530 /* Save the initialized register set. */
531 fs
->initial
= fs
->regs
;
532 fs
->initial
.reg
= dwarf2_frame_state_copy_regs (&fs
->regs
);
534 /* Then decode the insns in the FDE up to our target PC. */
535 execute_cfa_program (fde
->instructions
, fde
->end
, next_frame
, fs
);
537 /* Caclulate the CFA. */
541 cache
->cfa
= read_reg (next_frame
, fs
->cfa_reg
);
542 cache
->cfa
+= fs
->cfa_offset
;
547 execute_stack_op (fs
->cfa_exp
, fs
->cfa_exp_len
, next_frame
, 0);
551 internal_error (__FILE__
, __LINE__
, "Unknown CFA rule.");
554 /* Initialize things so that all registers are marked as
558 for (regnum
= 0; regnum
< num_regs
; regnum
++)
559 cache
->reg
[regnum
].how
= REG_UNSPECIFIED
;
562 /* Go through the DWARF2 CFI generated table and save its register
563 location information in the cache. */
565 int column
; /* CFI speak for "register number". */
566 for (column
= 0; column
< fs
->regs
.num_regs
; column
++)
570 /* Skip the return address column. */
571 if (column
== fs
->retaddr_column
)
572 /* NOTE: cagney/2003-06-07: Is this right? What if
573 RETADDR_COLUMN corresponds to a real register (and,
574 worse, that isn't the PC_REGNUM)? I'm guessing that the
575 PC_REGNUM further down is trying to handle this. That
576 can't be right though - PC_REGNUM may not be valid (it
577 can be -ve). I think, instead when RETADDR_COLUM isn't a
578 real register, it should map itself onto frame_pc_unwind. */
581 /* Use the GDB register number as the destination index. */
582 regnum
= DWARF2_REG_TO_REGNUM (column
);
584 /* If there's no corresponding GDB register, ignore it. */
585 if (regnum
< 0 || regnum
>= num_regs
)
588 /* NOTE: cagney/2003-09-05: CFI should specify the disposition
589 of all debug info registers. If it doesn't complain (but
590 not too loudly). It turns out that GCC, assumes that an
591 unspecified register implies "same value" when CFI (draft
592 7) specifies nothing at all. Such a register could equally
593 be interpreted as "undefined". Also note that this check
594 isn't sufficient - it only checks that all registers in the
595 range [0 .. max column] are specified - and won't detect
596 problems when a debug info register falls outside of the
597 table. Need a way of iterating through all the valid
598 DWARF2 register numbers. */
599 if (fs
->regs
.reg
[column
].how
== REG_UNSPECIFIED
)
600 complaint (&symfile_complaints
,
601 "Incomplete CFI data; unspecified registers at 0x%s",
604 cache
->reg
[regnum
] = fs
->regs
.reg
[column
];
608 /* Store the location of the return addess. If the return address
609 column (adjusted) is not the same as gdb's PC_REGNUM, then this
610 implies a copy from the ra column register. */
611 if (fs
->retaddr_column
< fs
->regs
.num_regs
612 && fs
->regs
.reg
[fs
->retaddr_column
].how
!= REG_UNDEFINED
)
614 /* See comment above about a possibly -ve PC_REGNUM. If this
615 assertion fails, it's a problem with this code and not the
617 gdb_assert (PC_REGNUM
>= 0);
618 cache
->reg
[PC_REGNUM
] = fs
->regs
.reg
[fs
->retaddr_column
];
622 int reg
= DWARF2_REG_TO_REGNUM (fs
->retaddr_column
);
623 if (reg
!= PC_REGNUM
)
625 /* See comment above about PC_REGNUM being -ve. If this
626 assertion fails, it's a problem with this code and not
628 gdb_assert (PC_REGNUM
>= 0);
629 cache
->reg
[PC_REGNUM
].loc
.reg
= reg
;
630 cache
->reg
[PC_REGNUM
].how
= REG_SAVED_REG
;
634 do_cleanups (old_chain
);
641 dwarf2_frame_this_id (struct frame_info
*next_frame
, void **this_cache
,
642 struct frame_id
*this_id
)
644 struct dwarf2_frame_cache
*cache
=
645 dwarf2_frame_cache (next_frame
, this_cache
);
647 (*this_id
) = frame_id_build (cache
->cfa
, frame_func_unwind (next_frame
));
651 dwarf2_frame_prev_register (struct frame_info
*next_frame
, void **this_cache
,
652 int regnum
, int *optimizedp
,
653 enum lval_type
*lvalp
, CORE_ADDR
*addrp
,
654 int *realnump
, void *valuep
)
656 struct dwarf2_frame_cache
*cache
=
657 dwarf2_frame_cache (next_frame
, this_cache
);
659 switch (cache
->reg
[regnum
].how
)
662 /* If CFI explicitly specified that the value isn't defined,
663 mark it as optimized away - the value isn't available. */
668 if (regnum
== SP_REGNUM
)
670 /* GCC defines the CFA as the value of the stack pointer
671 just before the call instruction is executed. Do other
672 compilers use the same definition? */
673 /* DWARF V3 Draft 7 p102: Typically, the CFA is defined to
674 be the value of the stack pointer at the call site in the
675 previous frame (which may be different from its value on
676 entry to the current frame). */
677 /* DWARF V3 Draft 7 p103: The first column of the rules
678 defines the rule which computes the CFA value; it may be
679 either a register and a signed offset that are added
680 together or a DWARF expression that is evaluated. */
681 /* FIXME: cagney/2003-07-07: I don't understand this. The
682 CFI info should have provided unwind information for the
683 SP register and then pointed ->cfa_reg at it, not the
684 reverse. Assuming that SP_REGNUM is !-ve, there is a
685 very real posibility that CFA is an offset from some
686 other register, having nothing to do with the unwound SP
688 /* FIXME: cagney/2003-09-05: I think I understand. GDB was
689 lumping the two states "unspecified" and "undefined"
690 together. Here SP_REGNUM was "unspecified", GCC assuming
691 that in such a case CFA would be used. This branch of
692 the if statement should be deleted - the problem of
693 SP_REGNUM is now handed by the case REG_UNSPECIFIED
698 /* Store the value. */
699 store_typed_address (valuep
, builtin_type_void_data_ptr
,
705 /* In some cases, for example %eflags on the i386, we have
706 to provide a sane value, even though this register wasn't
707 saved. Assume we can get it from NEXT_FRAME. */
708 frame_unwind_register (next_frame
, regnum
, valuep
);
712 case REG_SAVED_OFFSET
:
714 *lvalp
= lval_memory
;
715 *addrp
= cache
->cfa
+ cache
->reg
[regnum
].loc
.offset
;
719 /* Read the value in from memory. */
720 read_memory (*addrp
, valuep
,
721 register_size (current_gdbarch
, regnum
));
726 regnum
= DWARF2_REG_TO_REGNUM (cache
->reg
[regnum
].loc
.reg
);
727 frame_register_unwind (next_frame
, regnum
,
728 optimizedp
, lvalp
, addrp
, realnump
, valuep
);
733 *lvalp
= lval_memory
;
734 *addrp
= execute_stack_op (cache
->reg
[regnum
].loc
.exp
,
735 cache
->reg
[regnum
].exp_len
,
736 next_frame
, cache
->cfa
);
740 /* Read the value in from memory. */
741 read_memory (*addrp
, valuep
,
742 register_size (current_gdbarch
, regnum
));
746 case REG_UNSPECIFIED
:
747 /* GCC, in its infinite wisdom decided to not provide unwind
748 information for registers that are "same value". Since
749 DWARF2 (3 draft 7) doesn't define such behavior, said
750 registers are actually undefined (which is different to CFI
751 "undefined"). Code above issues a complaint about this.
752 Here just fudge the books, assume GCC, and that the value is
753 more inner on the stack. */
754 if (SP_REGNUM
>= 0 && regnum
== SP_REGNUM
)
756 /* Can things get worse? Yep! One of the registers GCC
757 forgot to provide unwind information for was the stack
758 pointer. Outch! GCC appears to assumes that the CFA
759 address can be used - after all it points to the inner
760 most address of the previous frame before the function
761 call and that's always the same as the stack pointer on
762 return, right? Wrong. See GCC's i386 STDCALL option for
763 an ABI that has a different entry and return stack
765 /* DWARF V3 Draft 7 p102: Typically, the CFA is defined to
766 be the value of the stack pointer at the call site in the
767 previous frame (which may be different from its value on
768 entry to the current frame). */
769 /* DWARF V3 Draft 7 p103: The first column of the rules
770 defines the rule which computes the CFA value; it may be
771 either a register and a signed offset that are added
772 together or a DWARF expression that is evaluated. */
773 /* NOTE: cagney/2003-09-05: Should issue a complain.
774 Unfortunately it turns out that DWARF2 CFI has a problem.
775 Since CFI specifies the location at which a register was
776 saved (not its value) it isn't possible to specify
777 something like "unwound(REG) == REG + constant" using CFI
778 as will almost always occure with the stack pointer. I
779 guess CFI should be point SP at CFA. Ref: danielj,
780 "Describing unsaved stack pointers", posted to dwarf2
787 /* Store the value. */
788 store_typed_address (valuep
, builtin_type_void_data_ptr
,
792 /* Assume that the register can be found in the next inner
794 frame_register_unwind (next_frame
, regnum
,
795 optimizedp
, lvalp
, addrp
, realnump
, valuep
);
799 frame_register_unwind (next_frame
, regnum
,
800 optimizedp
, lvalp
, addrp
, realnump
, valuep
);
804 internal_error (__FILE__
, __LINE__
, "Unknown register rule.");
808 static const struct frame_unwind dwarf2_frame_unwind
=
811 dwarf2_frame_this_id
,
812 dwarf2_frame_prev_register
815 const struct frame_unwind
*
816 dwarf2_frame_sniffer (struct frame_info
*next_frame
)
818 /* Grab an address that is guarenteed to reside somewhere within the
819 function. frame_pc_unwind(), for a no-return next function, can
820 end up returning something past the end of this function's body. */
821 CORE_ADDR block_addr
= frame_unwind_address_in_block (next_frame
);
822 if (dwarf2_frame_find_fde (&block_addr
))
823 return &dwarf2_frame_unwind
;
829 /* There is no explicitly defined relationship between the CFA and the
830 location of frame's local variables and arguments/parameters.
831 Therefore, frame base methods on this page should probably only be
832 used as a last resort, just to avoid printing total garbage as a
833 response to the "info frame" command. */
836 dwarf2_frame_base_address (struct frame_info
*next_frame
, void **this_cache
)
838 struct dwarf2_frame_cache
*cache
=
839 dwarf2_frame_cache (next_frame
, this_cache
);
844 static const struct frame_base dwarf2_frame_base
=
846 &dwarf2_frame_unwind
,
847 dwarf2_frame_base_address
,
848 dwarf2_frame_base_address
,
849 dwarf2_frame_base_address
852 const struct frame_base
*
853 dwarf2_frame_base_sniffer (struct frame_info
*next_frame
)
855 CORE_ADDR pc
= frame_pc_unwind (next_frame
);
856 if (dwarf2_frame_find_fde (&pc
))
857 return &dwarf2_frame_base
;
862 /* A minimal decoding of DWARF2 compilation units. We only decode
863 what's needed to get to the call frame information. */
867 /* Keep the bfd convenient. */
870 struct objfile
*objfile
;
872 /* Linked list of CIEs for this object. */
873 struct dwarf2_cie
*cie
;
875 /* Address size for this unit - from unit header. */
876 unsigned char addr_size
;
878 /* Pointer to the .debug_frame section loaded into memory. */
879 char *dwarf_frame_buffer
;
881 /* Length of the loaded .debug_frame section. */
882 unsigned long dwarf_frame_size
;
884 /* Pointer to the .debug_frame section. */
885 asection
*dwarf_frame_section
;
887 /* Base for DW_EH_PE_datarel encodings. */
890 /* Base for DW_EH_PE_textrel encodings. */
894 const struct objfile_data
*dwarf2_frame_data
;
897 read_1_byte (bfd
*bfd
, char *buf
)
899 return bfd_get_8 (abfd
, (bfd_byte
*) buf
);
903 read_4_bytes (bfd
*abfd
, char *buf
)
905 return bfd_get_32 (abfd
, (bfd_byte
*) buf
);
909 read_8_bytes (bfd
*abfd
, char *buf
)
911 return bfd_get_64 (abfd
, (bfd_byte
*) buf
);
915 read_unsigned_leb128 (bfd
*abfd
, char *buf
, unsigned int *bytes_read_ptr
)
918 unsigned int num_read
;
928 byte
= bfd_get_8 (abfd
, (bfd_byte
*) buf
);
931 result
|= ((byte
& 0x7f) << shift
);
936 *bytes_read_ptr
= num_read
;
942 read_signed_leb128 (bfd
*abfd
, char *buf
, unsigned int *bytes_read_ptr
)
946 unsigned int num_read
;
955 byte
= bfd_get_8 (abfd
, (bfd_byte
*) buf
);
958 result
|= ((byte
& 0x7f) << shift
);
963 if ((shift
< 32) && (byte
& 0x40))
964 result
|= -(1 << shift
);
966 *bytes_read_ptr
= num_read
;
972 read_initial_length (bfd
*abfd
, char *buf
, unsigned int *bytes_read_ptr
)
976 result
= bfd_get_32 (abfd
, (bfd_byte
*) buf
);
977 if (result
== 0xffffffff)
979 result
= bfd_get_64 (abfd
, (bfd_byte
*) buf
+ 4);
980 *bytes_read_ptr
= 12;
989 /* Pointer encoding helper functions. */
991 /* GCC supports exception handling based on DWARF2 CFI. However, for
992 technical reasons, it encodes addresses in its FDE's in a different
993 way. Several "pointer encodings" are supported. The encoding
994 that's used for a particular FDE is determined by the 'R'
995 augmentation in the associated CIE. The argument of this
996 augmentation is a single byte.
998 The address can be encoded as 2 bytes, 4 bytes, 8 bytes, or as a
999 LEB128. This is encoded in bits 0, 1 and 2. Bit 3 encodes whether
1000 the address is signed or unsigned. Bits 4, 5 and 6 encode how the
1001 address should be interpreted (absolute, relative to the current
1002 position in the FDE, ...). Bit 7, indicates that the address
1003 should be dereferenced. */
1005 static unsigned char
1006 encoding_for_size (unsigned int size
)
1011 return DW_EH_PE_udata2
;
1013 return DW_EH_PE_udata4
;
1015 return DW_EH_PE_udata8
;
1017 internal_error (__FILE__
, __LINE__
, "Unsupported address size");
1022 size_of_encoded_value (unsigned char encoding
)
1024 if (encoding
== DW_EH_PE_omit
)
1027 switch (encoding
& 0x07)
1029 case DW_EH_PE_absptr
:
1030 return TYPE_LENGTH (builtin_type_void_data_ptr
);
1031 case DW_EH_PE_udata2
:
1033 case DW_EH_PE_udata4
:
1035 case DW_EH_PE_udata8
:
1038 internal_error (__FILE__
, __LINE__
, "Invalid or unsupported encoding");
1043 read_encoded_value (struct comp_unit
*unit
, unsigned char encoding
,
1044 char *buf
, unsigned int *bytes_read_ptr
)
1046 int ptr_len
= size_of_encoded_value (DW_EH_PE_absptr
);
1050 /* GCC currently doesn't generate DW_EH_PE_indirect encodings for
1052 if (encoding
& DW_EH_PE_indirect
)
1053 internal_error (__FILE__
, __LINE__
,
1054 "Unsupported encoding: DW_EH_PE_indirect");
1056 *bytes_read_ptr
= 0;
1058 switch (encoding
& 0x70)
1060 case DW_EH_PE_absptr
:
1063 case DW_EH_PE_pcrel
:
1064 base
= bfd_get_section_vma (unit
->bfd
, unit
->dwarf_frame_section
);
1065 base
+= (buf
- unit
->dwarf_frame_buffer
);
1067 case DW_EH_PE_datarel
:
1070 case DW_EH_PE_textrel
:
1073 case DW_EH_PE_aligned
:
1075 offset
= buf
- unit
->dwarf_frame_buffer
;
1076 if ((offset
% ptr_len
) != 0)
1078 *bytes_read_ptr
= ptr_len
- (offset
% ptr_len
);
1079 buf
+= *bytes_read_ptr
;
1083 internal_error (__FILE__
, __LINE__
, "Invalid or unsupported encoding");
1086 if ((encoding
& 0x0f) == 0x00)
1087 encoding
|= encoding_for_size (ptr_len
);
1089 switch (encoding
& 0x0f)
1091 case DW_EH_PE_udata2
:
1092 *bytes_read_ptr
+= 2;
1093 return (base
+ bfd_get_16 (unit
->abfd
, (bfd_byte
*) buf
));
1094 case DW_EH_PE_udata4
:
1095 *bytes_read_ptr
+= 4;
1096 return (base
+ bfd_get_32 (unit
->abfd
, (bfd_byte
*) buf
));
1097 case DW_EH_PE_udata8
:
1098 *bytes_read_ptr
+= 8;
1099 return (base
+ bfd_get_64 (unit
->abfd
, (bfd_byte
*) buf
));
1100 case DW_EH_PE_sdata2
:
1101 *bytes_read_ptr
+= 2;
1102 return (base
+ bfd_get_signed_16 (unit
->abfd
, (bfd_byte
*) buf
));
1103 case DW_EH_PE_sdata4
:
1104 *bytes_read_ptr
+= 4;
1105 return (base
+ bfd_get_signed_32 (unit
->abfd
, (bfd_byte
*) buf
));
1106 case DW_EH_PE_sdata8
:
1107 *bytes_read_ptr
+= 8;
1108 return (base
+ bfd_get_signed_64 (unit
->abfd
, (bfd_byte
*) buf
));
1110 internal_error (__FILE__
, __LINE__
, "Invalid or unsupported encoding");
1115 /* GCC uses a single CIE for all FDEs in a .debug_frame section.
1116 That's why we use a simple linked list here. */
1118 static struct dwarf2_cie
*
1119 find_cie (struct comp_unit
*unit
, ULONGEST cie_pointer
)
1121 struct dwarf2_cie
*cie
= unit
->cie
;
1125 if (cie
->cie_pointer
== cie_pointer
)
1135 add_cie (struct comp_unit
*unit
, struct dwarf2_cie
*cie
)
1137 cie
->next
= unit
->cie
;
1141 /* Find the FDE for *PC. Return a pointer to the FDE, and store the
1142 inital location associated with it into *PC. */
1144 static struct dwarf2_fde
*
1145 dwarf2_frame_find_fde (CORE_ADDR
*pc
)
1147 struct objfile
*objfile
;
1149 ALL_OBJFILES (objfile
)
1151 struct dwarf2_fde
*fde
;
1154 fde
= objfile_data (objfile
, dwarf2_frame_data
);
1158 gdb_assert (objfile
->section_offsets
);
1159 offset
= ANOFFSET (objfile
->section_offsets
, SECT_OFF_TEXT (objfile
));
1163 if (*pc
>= fde
->initial_location
+ offset
1164 && *pc
< fde
->initial_location
+ offset
+ fde
->address_range
)
1166 *pc
= fde
->initial_location
+ offset
;
1178 add_fde (struct comp_unit
*unit
, struct dwarf2_fde
*fde
)
1180 fde
->next
= objfile_data (unit
->objfile
, dwarf2_frame_data
);
1181 set_objfile_data (unit
->objfile
, dwarf2_frame_data
, fde
);
1184 #ifdef CC_HAS_LONG_LONG
1185 #define DW64_CIE_ID 0xffffffffffffffffULL
1187 #define DW64_CIE_ID ~0
1190 static char *decode_frame_entry (struct comp_unit
*unit
, char *start
,
1193 /* Decode the next CIE or FDE. Return NULL if invalid input, otherwise
1194 the next byte to be processed. */
1196 decode_frame_entry_1 (struct comp_unit
*unit
, char *start
, int eh_frame_p
)
1200 unsigned int bytes_read
;
1203 ULONGEST cie_pointer
;
1207 length
= read_initial_length (unit
->abfd
, buf
, &bytes_read
);
1211 /* Are we still within the section? */
1212 if (end
> unit
->dwarf_frame_buffer
+ unit
->dwarf_frame_size
)
1218 /* Distinguish between 32 and 64-bit encoded frame info. */
1219 dwarf64_p
= (bytes_read
== 12);
1221 /* In a .eh_frame section, zero is used to distinguish CIEs from FDEs. */
1225 cie_id
= DW64_CIE_ID
;
1231 cie_pointer
= read_8_bytes (unit
->abfd
, buf
);
1236 cie_pointer
= read_4_bytes (unit
->abfd
, buf
);
1240 if (cie_pointer
== cie_id
)
1242 /* This is a CIE. */
1243 struct dwarf2_cie
*cie
;
1246 /* Record the offset into the .debug_frame section of this CIE. */
1247 cie_pointer
= start
- unit
->dwarf_frame_buffer
;
1249 /* Check whether we've already read it. */
1250 if (find_cie (unit
, cie_pointer
))
1253 cie
= (struct dwarf2_cie
*)
1254 obstack_alloc (&unit
->objfile
->psymbol_obstack
,
1255 sizeof (struct dwarf2_cie
));
1256 cie
->initial_instructions
= NULL
;
1257 cie
->cie_pointer
= cie_pointer
;
1259 /* The encoding for FDE's in a normal .debug_frame section
1260 depends on the target address size as specified in the
1261 Compilation Unit Header. */
1262 cie
->encoding
= encoding_for_size (unit
->addr_size
);
1264 /* Check version number. */
1265 if (read_1_byte (unit
->abfd
, buf
) != DW_CIE_VERSION
)
1269 /* Interpret the interesting bits of the augmentation. */
1271 buf
= augmentation
+ strlen (augmentation
) + 1;
1273 /* The GCC 2.x "eh" augmentation has a pointer immediately
1274 following the augmentation string, so it must be handled
1276 if (augmentation
[0] == 'e' && augmentation
[1] == 'h')
1279 buf
+= TYPE_LENGTH (builtin_type_void_data_ptr
);
1283 cie
->code_alignment_factor
=
1284 read_unsigned_leb128 (unit
->abfd
, buf
, &bytes_read
);
1287 cie
->data_alignment_factor
=
1288 read_signed_leb128 (unit
->abfd
, buf
, &bytes_read
);
1291 cie
->return_address_register
= read_1_byte (unit
->abfd
, buf
);
1294 cie
->saw_z_augmentation
= (*augmentation
== 'z');
1295 if (cie
->saw_z_augmentation
)
1299 length
= read_unsigned_leb128 (unit
->abfd
, buf
, &bytes_read
);
1303 cie
->initial_instructions
= buf
+ length
;
1307 while (*augmentation
)
1309 /* "L" indicates a byte showing how the LSDA pointer is encoded. */
1310 if (*augmentation
== 'L')
1317 /* "R" indicates a byte indicating how FDE addresses are encoded. */
1318 else if (*augmentation
== 'R')
1320 cie
->encoding
= *buf
++;
1324 /* "P" indicates a personality routine in the CIE augmentation. */
1325 else if (*augmentation
== 'P')
1328 buf
+= size_of_encoded_value (*buf
++);
1332 /* Otherwise we have an unknown augmentation.
1333 Bail out unless we saw a 'z' prefix. */
1336 if (cie
->initial_instructions
== NULL
)
1339 /* Skip unknown augmentations. */
1340 buf
= cie
->initial_instructions
;
1345 cie
->initial_instructions
= buf
;
1348 add_cie (unit
, cie
);
1352 /* This is a FDE. */
1353 struct dwarf2_fde
*fde
;
1355 /* In an .eh_frame section, the CIE pointer is the delta between the
1356 address within the FDE where the CIE pointer is stored and the
1357 address of the CIE. Convert it to an offset into the .eh_frame
1361 cie_pointer
= buf
- unit
->dwarf_frame_buffer
- cie_pointer
;
1362 cie_pointer
-= (dwarf64_p
? 8 : 4);
1365 /* In either case, validate the result is still within the section. */
1366 if (cie_pointer
>= unit
->dwarf_frame_size
)
1369 fde
= (struct dwarf2_fde
*)
1370 obstack_alloc (&unit
->objfile
->psymbol_obstack
,
1371 sizeof (struct dwarf2_fde
));
1372 fde
->cie
= find_cie (unit
, cie_pointer
);
1373 if (fde
->cie
== NULL
)
1375 decode_frame_entry (unit
, unit
->dwarf_frame_buffer
+ cie_pointer
,
1377 fde
->cie
= find_cie (unit
, cie_pointer
);
1380 gdb_assert (fde
->cie
!= NULL
);
1382 fde
->initial_location
=
1383 read_encoded_value (unit
, fde
->cie
->encoding
, buf
, &bytes_read
);
1386 fde
->address_range
=
1387 read_encoded_value (unit
, fde
->cie
->encoding
& 0x0f, buf
, &bytes_read
);
1390 /* A 'z' augmentation in the CIE implies the presence of an
1391 augmentation field in the FDE as well. The only thing known
1392 to be in here at present is the LSDA entry for EH. So we
1393 can skip the whole thing. */
1394 if (fde
->cie
->saw_z_augmentation
)
1398 length
= read_unsigned_leb128 (unit
->abfd
, buf
, &bytes_read
);
1399 buf
+= bytes_read
+ length
;
1404 fde
->instructions
= buf
;
1407 add_fde (unit
, fde
);
1413 /* Read a CIE or FDE in BUF and decode it. */
1415 decode_frame_entry (struct comp_unit
*unit
, char *start
, int eh_frame_p
)
1417 enum { NONE
, ALIGN4
, ALIGN8
, FAIL
} workaround
= NONE
;
1420 ptrdiff_t start_offset
;
1424 ret
= decode_frame_entry_1 (unit
, start
, eh_frame_p
);
1428 /* We have corrupt input data of some form. */
1430 /* ??? Try, weakly, to work around compiler/assembler/linker bugs
1431 and mismatches wrt padding and alignment of debug sections. */
1432 /* Note that there is no requirement in the standard for any
1433 alignment at all in the frame unwind sections. Testing for
1434 alignment before trying to interpret data would be incorrect.
1436 However, GCC traditionally arranged for frame sections to be
1437 sized such that the FDE length and CIE fields happen to be
1438 aligned (in theory, for performance). This, unfortunately,
1439 was done with .align directives, which had the side effect of
1440 forcing the section to be aligned by the linker.
1442 This becomes a problem when you have some other producer that
1443 creates frame sections that are not as strictly aligned. That
1444 produces a hole in the frame info that gets filled by the
1447 The GCC behaviour is arguably a bug, but it's effectively now
1448 part of the ABI, so we're now stuck with it, at least at the
1449 object file level. A smart linker may decide, in the process
1450 of compressing duplicate CIE information, that it can rewrite
1451 the entire output section without this extra padding. */
1453 start_offset
= start
- unit
->dwarf_frame_buffer
;
1454 if (workaround
< ALIGN4
&& (start_offset
& 3) != 0)
1456 start
+= 4 - (start_offset
& 3);
1457 workaround
= ALIGN4
;
1460 if (workaround
< ALIGN8
&& (start_offset
& 7) != 0)
1462 start
+= 8 - (start_offset
& 7);
1463 workaround
= ALIGN8
;
1467 /* Nothing left to try. Arrange to return as if we've consumed
1468 the entire input section. Hopefully we'll get valid info from
1469 the other of .debug_frame/.eh_frame. */
1471 ret
= unit
->dwarf_frame_buffer
+ unit
->dwarf_frame_size
;
1481 complaint (&symfile_complaints
,
1482 "Corrupt data in %s:%s; align 4 workaround apparently succeeded",
1483 unit
->dwarf_frame_section
->owner
->filename
,
1484 unit
->dwarf_frame_section
->name
);
1488 complaint (&symfile_complaints
,
1489 "Corrupt data in %s:%s; align 8 workaround apparently succeeded",
1490 unit
->dwarf_frame_section
->owner
->filename
,
1491 unit
->dwarf_frame_section
->name
);
1495 complaint (&symfile_complaints
,
1496 "Corrupt data in %s:%s",
1497 unit
->dwarf_frame_section
->owner
->filename
,
1498 unit
->dwarf_frame_section
->name
);
1507 /* FIXME: kettenis/20030504: This still needs to be integrated with
1508 dwarf2read.c in a better way. */
1510 /* Imported from dwarf2read.c. */
1511 extern file_ptr dwarf_frame_offset
;
1512 extern unsigned int dwarf_frame_size
;
1513 extern asection
*dwarf_frame_section
;
1514 extern file_ptr dwarf_eh_frame_offset
;
1515 extern unsigned int dwarf_eh_frame_size
;
1516 extern asection
*dwarf_eh_frame_section
;
1518 /* Imported from dwarf2read.c. */
1519 extern char *dwarf2_read_section (struct objfile
*objfile
, file_ptr offset
,
1520 unsigned int size
, asection
*sectp
);
1523 dwarf2_build_frame_info (struct objfile
*objfile
)
1525 struct comp_unit unit
;
1528 /* Build a minimal decoding of the DWARF2 compilation unit. */
1529 unit
.abfd
= objfile
->obfd
;
1530 unit
.objfile
= objfile
;
1531 unit
.addr_size
= objfile
->obfd
->arch_info
->bits_per_address
/ 8;
1535 /* First add the information from the .eh_frame section. That way,
1536 the FDEs from that section are searched last. */
1537 if (dwarf_eh_frame_offset
)
1539 asection
*got
, *txt
;
1542 unit
.dwarf_frame_buffer
= dwarf2_read_section (objfile
,
1543 dwarf_eh_frame_offset
,
1544 dwarf_eh_frame_size
,
1545 dwarf_eh_frame_section
);
1547 unit
.dwarf_frame_size
= dwarf_eh_frame_size
;
1548 unit
.dwarf_frame_section
= dwarf_eh_frame_section
;
1550 /* FIXME: kettenis/20030602: This is the DW_EH_PE_datarel base
1551 that is used for the i386/amd64 target, which currently is
1552 the only target in GCC that supports/uses the
1553 DW_EH_PE_datarel encoding. */
1554 got
= bfd_get_section_by_name (unit
.abfd
, ".got");
1556 unit
.dbase
= got
->vma
;
1558 /* GCC emits the DW_EH_PE_textrel encoding type on sh and ia64
1560 txt
= bfd_get_section_by_name (unit
.abfd
, ".text");
1562 unit
.tbase
= txt
->vma
;
1564 frame_ptr
= unit
.dwarf_frame_buffer
;
1565 while (frame_ptr
< unit
.dwarf_frame_buffer
+ unit
.dwarf_frame_size
)
1566 frame_ptr
= decode_frame_entry (&unit
, frame_ptr
, 1);
1569 if (dwarf_frame_offset
)
1572 unit
.dwarf_frame_buffer
= dwarf2_read_section (objfile
,
1575 dwarf_frame_section
);
1576 unit
.dwarf_frame_size
= dwarf_frame_size
;
1577 unit
.dwarf_frame_section
= dwarf_frame_section
;
1579 frame_ptr
= unit
.dwarf_frame_buffer
;
1580 while (frame_ptr
< unit
.dwarf_frame_buffer
+ unit
.dwarf_frame_size
)
1581 frame_ptr
= decode_frame_entry (&unit
, frame_ptr
, 0);
1585 /* Provide a prototype to silence -Wmissing-prototypes. */
1586 void _initialize_dwarf2_frame (void);
1589 _initialize_dwarf2_frame (void)
1591 dwarf2_frame_data
= register_objfile_data ();