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
].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 wortless 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).
509 frame_unwind_address_in_block does just this.
511 It's not clear how reliable the method is though - there is the
512 potential for the register state pre-call being different to that
514 fs
->pc
= frame_unwind_address_in_block (next_frame
);
516 /* Find the correct FDE. */
517 fde
= dwarf2_frame_find_fde (&fs
->pc
);
518 gdb_assert (fde
!= NULL
);
520 /* Extract any interesting information from the CIE. */
521 fs
->data_align
= fde
->cie
->data_alignment_factor
;
522 fs
->code_align
= fde
->cie
->code_alignment_factor
;
523 fs
->retaddr_column
= fde
->cie
->return_address_register
;
525 /* First decode all the insns in the CIE. */
526 execute_cfa_program (fde
->cie
->initial_instructions
,
527 fde
->cie
->end
, next_frame
, fs
);
529 /* Save the initialized register set. */
530 fs
->initial
= fs
->regs
;
531 fs
->initial
.reg
= dwarf2_frame_state_copy_regs (&fs
->regs
);
533 /* Then decode the insns in the FDE up to our target PC. */
534 execute_cfa_program (fde
->instructions
, fde
->end
, next_frame
, fs
);
536 /* Caclulate the CFA. */
540 cache
->cfa
= read_reg (next_frame
, fs
->cfa_reg
);
541 cache
->cfa
+= fs
->cfa_offset
;
546 execute_stack_op (fs
->cfa_exp
, fs
->cfa_exp_len
, next_frame
, 0);
550 internal_error (__FILE__
, __LINE__
, "Unknown CFA rule.");
553 /* Initialize things so that all registers are marked as
557 for (regnum
= 0; regnum
< num_regs
; regnum
++)
558 cache
->reg
[regnum
].how
= REG_UNSPECIFIED
;
561 /* Go through the DWARF2 CFI generated table and save its register
562 location information in the cache. */
564 int column
; /* CFI speak for "register number". */
565 for (column
= 0; column
< fs
->regs
.num_regs
; column
++)
569 /* Skip the return address column. */
570 if (column
== fs
->retaddr_column
)
571 /* NOTE: cagney/2003-06-07: Is this right? What if
572 RETADDR_COLUMN corresponds to a real register (and,
573 worse, that isn't the PC_REGNUM)? I'm guessing that the
574 PC_REGNUM further down is trying to handle this. That
575 can't be right though - PC_REGNUM may not be valid (it
576 can be -ve). I think, instead when RETADDR_COLUM isn't a
577 real register, it should map itself onto frame_pc_unwind. */
580 /* Use the GDB register number as the destination index. */
581 regnum
= DWARF2_REG_TO_REGNUM (column
);
583 /* If there's no corresponding GDB register, ignore it. */
584 if (regnum
< 0 || regnum
>= num_regs
)
587 /* NOTE: cagney/2003-09-05: CFI should specify the disposition
588 of all debug info registers. If it doesn't complain (but
589 not too loudly). It turns out that GCC, assumes that an
590 unspecified register implies "same value" when CFI (draft
591 7) specifies nothing at all. Such a register could equally
592 be interpreted as "undefined". Also note that this check
593 isn't sufficient - it only checks that all registers in the
594 range [0 .. max column] are specified - and won't detect
595 problems when a debug info register falls outside of the
596 table. Need a way of iterating through all the valid
597 DWARF2 register numbers. */
598 if (fs
->regs
.reg
[column
].how
== REG_UNSPECIFIED
)
599 complaint (&symfile_complaints
,
600 "Incomplete CFI data; unspecified registers at 0x%s",
603 cache
->reg
[regnum
] = fs
->regs
.reg
[column
];
607 /* Store the location of the return addess. If the return address
608 column (adjusted) is not the same as gdb's PC_REGNUM, then this
609 implies a copy from the ra column register. */
610 if (fs
->retaddr_column
< fs
->regs
.num_regs
611 && fs
->regs
.reg
[fs
->retaddr_column
].how
!= REG_UNDEFINED
)
613 /* See comment above about a possibly -ve PC_REGNUM. If this
614 assertion fails, it's a problem with this code and not the
616 gdb_assert (PC_REGNUM
>= 0);
617 cache
->reg
[PC_REGNUM
] = fs
->regs
.reg
[fs
->retaddr_column
];
621 int reg
= DWARF2_REG_TO_REGNUM (fs
->retaddr_column
);
622 if (reg
!= PC_REGNUM
)
624 /* See comment above about PC_REGNUM being -ve. If this
625 assertion fails, it's a problem with this code and not
627 gdb_assert (PC_REGNUM
>= 0);
628 cache
->reg
[PC_REGNUM
].loc
.reg
= reg
;
629 cache
->reg
[PC_REGNUM
].how
= REG_SAVED_REG
;
633 do_cleanups (old_chain
);
640 dwarf2_frame_this_id (struct frame_info
*next_frame
, void **this_cache
,
641 struct frame_id
*this_id
)
643 struct dwarf2_frame_cache
*cache
=
644 dwarf2_frame_cache (next_frame
, this_cache
);
646 (*this_id
) = frame_id_build (cache
->cfa
, frame_func_unwind (next_frame
));
650 dwarf2_frame_prev_register (struct frame_info
*next_frame
, void **this_cache
,
651 int regnum
, int *optimizedp
,
652 enum lval_type
*lvalp
, CORE_ADDR
*addrp
,
653 int *realnump
, void *valuep
)
655 struct dwarf2_frame_cache
*cache
=
656 dwarf2_frame_cache (next_frame
, this_cache
);
658 switch (cache
->reg
[regnum
].how
)
661 /* If CFI explicitly specified that the value isn't defined,
662 mark it as optimized away - the value isn't available. */
667 if (regnum
== SP_REGNUM
)
669 /* GCC defines the CFA as the value of the stack pointer
670 just before the call instruction is executed. Do other
671 compilers use the same definition? */
672 /* DWARF V3 Draft 7 p102: Typically, the CFA is defined to
673 be the value of the stack pointer at the call site in the
674 previous frame (which may be different from its value on
675 entry to the current frame). */
676 /* DWARF V3 Draft 7 p103: The first column of the rules
677 defines the rule which computes the CFA value; it may be
678 either a register and a signed offset that are added
679 together or a DWARF expression that is evaluated. */
680 /* FIXME: cagney/2003-07-07: I don't understand this. The
681 CFI info should have provided unwind information for the
682 SP register and then pointed ->cfa_reg at it, not the
683 reverse. Assuming that SP_REGNUM is !-ve, there is a
684 very real posibility that CFA is an offset from some
685 other register, having nothing to do with the unwound SP
687 /* FIXME: cagney/2003-09-05: I think I understand. GDB was
688 lumping the two states "unspecified" and "undefined"
689 together. Here SP_REGNUM was "unspecified", GCC assuming
690 that in such a case CFA would be used. This branch of
691 the if statement should be deleted - the problem of
692 SP_REGNUM is now handed by the case REG_UNSPECIFIED
697 /* Store the value. */
698 store_typed_address (valuep
, builtin_type_void_data_ptr
,
704 /* In some cases, for example %eflags on the i386, we have
705 to provide a sane value, even though this register wasn't
706 saved. Assume we can get it from NEXT_FRAME. */
707 frame_unwind_register (next_frame
, regnum
, valuep
);
711 case REG_SAVED_OFFSET
:
713 *lvalp
= lval_memory
;
714 *addrp
= cache
->cfa
+ cache
->reg
[regnum
].loc
.offset
;
718 /* Read the value in from memory. */
719 read_memory (*addrp
, valuep
,
720 register_size (current_gdbarch
, regnum
));
725 regnum
= DWARF2_REG_TO_REGNUM (cache
->reg
[regnum
].loc
.reg
);
726 frame_register_unwind (next_frame
, regnum
,
727 optimizedp
, lvalp
, addrp
, realnump
, valuep
);
732 *lvalp
= lval_memory
;
733 *addrp
= execute_stack_op (cache
->reg
[regnum
].loc
.exp
,
734 cache
->reg
[regnum
].exp_len
,
735 next_frame
, cache
->cfa
);
739 /* Read the value in from memory. */
740 read_memory (*addrp
, valuep
,
741 register_size (current_gdbarch
, regnum
));
745 case REG_UNSPECIFIED
:
746 /* GCC, in its infinite wisdom decided to not provide unwind
747 information for registers that are "same value". Since
748 DWARF2 (3 draft 7) doesn't define such behavior, said
749 registers are actually undefined (which is different to CFI
750 "undefined"). Code above issues a complaint about this.
751 Here just fudge the books, assume GCC, and that the value is
752 more inner on the stack. */
753 if (SP_REGNUM
>= 0 && regnum
== SP_REGNUM
)
755 /* Can things get worse? Yep! One of the registers GCC
756 forgot to provide unwind information for was the stack
757 pointer. Outch! GCC appears to assumes that the CFA
758 address can be used - after all it points to the inner
759 most address of the previous frame before the function
760 call and that's always the same as the stack pointer on
761 return, right? Wrong. See GCC's i386 STDCALL option for
762 an ABI that has a different entry and return stack
764 /* DWARF V3 Draft 7 p102: Typically, the CFA is defined to
765 be the value of the stack pointer at the call site in the
766 previous frame (which may be different from its value on
767 entry to the current frame). */
768 /* DWARF V3 Draft 7 p103: The first column of the rules
769 defines the rule which computes the CFA value; it may be
770 either a register and a signed offset that are added
771 together or a DWARF expression that is evaluated. */
772 /* NOTE: cagney/2003-09-05: Should issue a complain.
773 Unfortunatly it turns out that DWARF2 CFI has a problem.
774 Since CFI specifies the location at which a register was
775 saved (not its value) it isn't possible to specify
776 something like "unwound(REG) == REG + constant" using CFI
777 as will almost always occure with the stack pointer. I
778 guess CFI should be point SP at CFA. Ref: danielj,
779 "Describing unsaved stack pointers", posted to dwarf2
786 /* Store the value. */
787 store_typed_address (valuep
, builtin_type_void_data_ptr
,
791 /* Assume that the register can be found in the next inner
793 frame_register_unwind (next_frame
, regnum
,
794 optimizedp
, lvalp
, addrp
, realnump
, valuep
);
798 frame_register_unwind (next_frame
, regnum
,
799 optimizedp
, lvalp
, addrp
, realnump
, valuep
);
803 internal_error (__FILE__
, __LINE__
, "Unknown register rule.");
807 static const struct frame_unwind dwarf2_frame_unwind
=
810 dwarf2_frame_this_id
,
811 dwarf2_frame_prev_register
814 const struct frame_unwind
*
815 dwarf2_frame_sniffer (struct frame_info
*next_frame
)
817 /* Grab an address that is guarenteed to reside somewhere within the
818 function. frame_pc_unwind(), for a no-return next function, can
819 end up returning something past the end of this function's body. */
820 CORE_ADDR block_addr
= frame_unwind_address_in_block (next_frame
);
821 if (dwarf2_frame_find_fde (&block_addr
))
822 return &dwarf2_frame_unwind
;
828 /* There is no explicitly defined relationship between the CFA and the
829 location of frame's local variables and arguments/parameters.
830 Therefore, frame base methods on this page should probably only be
831 used as a last resort, just to avoid printing total garbage as a
832 response to the "info frame" command. */
835 dwarf2_frame_base_address (struct frame_info
*next_frame
, void **this_cache
)
837 struct dwarf2_frame_cache
*cache
=
838 dwarf2_frame_cache (next_frame
, this_cache
);
843 static const struct frame_base dwarf2_frame_base
=
845 &dwarf2_frame_unwind
,
846 dwarf2_frame_base_address
,
847 dwarf2_frame_base_address
,
848 dwarf2_frame_base_address
851 const struct frame_base
*
852 dwarf2_frame_base_sniffer (struct frame_info
*next_frame
)
854 CORE_ADDR pc
= frame_pc_unwind (next_frame
);
855 if (dwarf2_frame_find_fde (&pc
))
856 return &dwarf2_frame_base
;
861 /* A minimal decoding of DWARF2 compilation units. We only decode
862 what's needed to get to the call frame information. */
866 /* Keep the bfd convenient. */
869 struct objfile
*objfile
;
871 /* Linked list of CIEs for this object. */
872 struct dwarf2_cie
*cie
;
874 /* Address size for this unit - from unit header. */
875 unsigned char addr_size
;
877 /* Pointer to the .debug_frame section loaded into memory. */
878 char *dwarf_frame_buffer
;
880 /* Length of the loaded .debug_frame section. */
881 unsigned long dwarf_frame_size
;
883 /* Pointer to the .debug_frame section. */
884 asection
*dwarf_frame_section
;
886 /* Base for DW_EH_PE_datarel encodings. */
890 const struct objfile_data
*dwarf2_frame_data
;
893 read_1_byte (bfd
*bfd
, char *buf
)
895 return bfd_get_8 (abfd
, (bfd_byte
*) buf
);
899 read_4_bytes (bfd
*abfd
, char *buf
)
901 return bfd_get_32 (abfd
, (bfd_byte
*) buf
);
905 read_8_bytes (bfd
*abfd
, char *buf
)
907 return bfd_get_64 (abfd
, (bfd_byte
*) buf
);
911 read_unsigned_leb128 (bfd
*abfd
, char *buf
, unsigned int *bytes_read_ptr
)
914 unsigned int num_read
;
924 byte
= bfd_get_8 (abfd
, (bfd_byte
*) buf
);
927 result
|= ((byte
& 0x7f) << shift
);
932 *bytes_read_ptr
= num_read
;
938 read_signed_leb128 (bfd
*abfd
, char *buf
, unsigned int *bytes_read_ptr
)
942 unsigned int num_read
;
951 byte
= bfd_get_8 (abfd
, (bfd_byte
*) buf
);
954 result
|= ((byte
& 0x7f) << shift
);
959 if ((shift
< 32) && (byte
& 0x40))
960 result
|= -(1 << shift
);
962 *bytes_read_ptr
= num_read
;
968 read_initial_length (bfd
*abfd
, char *buf
, unsigned int *bytes_read_ptr
)
972 result
= bfd_get_32 (abfd
, (bfd_byte
*) buf
);
973 if (result
== 0xffffffff)
975 result
= bfd_get_64 (abfd
, (bfd_byte
*) buf
+ 4);
976 *bytes_read_ptr
= 12;
985 /* Pointer encoding helper functions. */
987 /* GCC supports exception handling based on DWARF2 CFI. However, for
988 technical reasons, it encodes addresses in its FDE's in a different
989 way. Several "pointer encodings" are supported. The encoding
990 that's used for a particular FDE is determined by the 'R'
991 augmentation in the associated CIE. The argument of this
992 augmentation is a single byte.
994 The address can be encoded as 2 bytes, 4 bytes, 8 bytes, or as a
995 LEB128. This is encoded in bits 0, 1 and 2. Bit 3 encodes whether
996 the address is signed or unsigned. Bits 4, 5 and 6 encode how the
997 address should be interpreted (absolute, relative to the current
998 position in the FDE, ...). Bit 7, indicates that the address
999 should be dereferenced. */
1001 static unsigned char
1002 encoding_for_size (unsigned int size
)
1007 return DW_EH_PE_udata2
;
1009 return DW_EH_PE_udata4
;
1011 return DW_EH_PE_udata8
;
1013 internal_error (__FILE__
, __LINE__
, "Unsupported address size");
1018 size_of_encoded_value (unsigned char encoding
)
1020 if (encoding
== DW_EH_PE_omit
)
1023 switch (encoding
& 0x07)
1025 case DW_EH_PE_absptr
:
1026 return TYPE_LENGTH (builtin_type_void_data_ptr
);
1027 case DW_EH_PE_udata2
:
1029 case DW_EH_PE_udata4
:
1031 case DW_EH_PE_udata8
:
1034 internal_error (__FILE__
, __LINE__
, "Invalid or unsupported encoding");
1039 read_encoded_value (struct comp_unit
*unit
, unsigned char encoding
,
1040 char *buf
, unsigned int *bytes_read_ptr
)
1042 int ptr_len
= size_of_encoded_value (DW_EH_PE_absptr
);
1046 /* GCC currently doesn't generate DW_EH_PE_indirect encodings for
1048 if (encoding
& DW_EH_PE_indirect
)
1049 internal_error (__FILE__
, __LINE__
,
1050 "Unsupported encoding: DW_EH_PE_indirect");
1052 *bytes_read_ptr
= 0;
1054 switch (encoding
& 0x70)
1056 case DW_EH_PE_absptr
:
1059 case DW_EH_PE_pcrel
:
1060 base
= bfd_get_section_vma (unit
->bfd
, unit
->dwarf_frame_section
);
1061 base
+= (buf
- unit
->dwarf_frame_buffer
);
1063 case DW_EH_PE_datarel
:
1066 case DW_EH_PE_aligned
:
1068 offset
= buf
- unit
->dwarf_frame_buffer
;
1069 if ((offset
% ptr_len
) != 0)
1071 *bytes_read_ptr
= ptr_len
- (offset
% ptr_len
);
1072 buf
+= *bytes_read_ptr
;
1076 internal_error (__FILE__
, __LINE__
, "Invalid or unsupported encoding");
1079 if ((encoding
& 0x0f) == 0x00)
1080 encoding
|= encoding_for_size (ptr_len
);
1082 switch (encoding
& 0x0f)
1084 case DW_EH_PE_udata2
:
1085 *bytes_read_ptr
+= 2;
1086 return (base
+ bfd_get_16 (unit
->abfd
, (bfd_byte
*) buf
));
1087 case DW_EH_PE_udata4
:
1088 *bytes_read_ptr
+= 4;
1089 return (base
+ bfd_get_32 (unit
->abfd
, (bfd_byte
*) buf
));
1090 case DW_EH_PE_udata8
:
1091 *bytes_read_ptr
+= 8;
1092 return (base
+ bfd_get_64 (unit
->abfd
, (bfd_byte
*) buf
));
1093 case DW_EH_PE_sdata2
:
1094 *bytes_read_ptr
+= 2;
1095 return (base
+ bfd_get_signed_16 (unit
->abfd
, (bfd_byte
*) buf
));
1096 case DW_EH_PE_sdata4
:
1097 *bytes_read_ptr
+= 4;
1098 return (base
+ bfd_get_signed_32 (unit
->abfd
, (bfd_byte
*) buf
));
1099 case DW_EH_PE_sdata8
:
1100 *bytes_read_ptr
+= 8;
1101 return (base
+ bfd_get_signed_64 (unit
->abfd
, (bfd_byte
*) buf
));
1103 internal_error (__FILE__
, __LINE__
, "Invalid or unsupported encoding");
1108 /* GCC uses a single CIE for all FDEs in a .debug_frame section.
1109 That's why we use a simple linked list here. */
1111 static struct dwarf2_cie
*
1112 find_cie (struct comp_unit
*unit
, ULONGEST cie_pointer
)
1114 struct dwarf2_cie
*cie
= unit
->cie
;
1118 if (cie
->cie_pointer
== cie_pointer
)
1128 add_cie (struct comp_unit
*unit
, struct dwarf2_cie
*cie
)
1130 cie
->next
= unit
->cie
;
1134 /* Find the FDE for *PC. Return a pointer to the FDE, and store the
1135 inital location associated with it into *PC. */
1137 static struct dwarf2_fde
*
1138 dwarf2_frame_find_fde (CORE_ADDR
*pc
)
1140 struct objfile
*objfile
;
1142 ALL_OBJFILES (objfile
)
1144 struct dwarf2_fde
*fde
;
1147 fde
= objfile_data (objfile
, dwarf2_frame_data
);
1151 gdb_assert (objfile
->section_offsets
);
1152 offset
= ANOFFSET (objfile
->section_offsets
, SECT_OFF_TEXT (objfile
));
1156 if (*pc
>= fde
->initial_location
+ offset
1157 && *pc
< fde
->initial_location
+ offset
+ fde
->address_range
)
1159 *pc
= fde
->initial_location
+ offset
;
1171 add_fde (struct comp_unit
*unit
, struct dwarf2_fde
*fde
)
1173 fde
->next
= objfile_data (unit
->objfile
, dwarf2_frame_data
);
1174 set_objfile_data (unit
->objfile
, dwarf2_frame_data
, fde
);
1177 #ifdef CC_HAS_LONG_LONG
1178 #define DW64_CIE_ID 0xffffffffffffffffULL
1180 #define DW64_CIE_ID ~0
1183 static char *decode_frame_entry (struct comp_unit
*unit
, char *start
,
1186 /* Decode the next CIE or FDE. Return NULL if invalid input, otherwise
1187 the next byte to be processed. */
1189 decode_frame_entry_1 (struct comp_unit
*unit
, char *start
, int eh_frame_p
)
1193 unsigned int bytes_read
;
1196 ULONGEST cie_pointer
;
1200 length
= read_initial_length (unit
->abfd
, buf
, &bytes_read
);
1204 /* Are we still within the section? */
1205 if (end
> unit
->dwarf_frame_buffer
+ unit
->dwarf_frame_size
)
1211 /* Distinguish between 32 and 64-bit encoded frame info. */
1212 dwarf64_p
= (bytes_read
== 12);
1214 /* In a .eh_frame section, zero is used to distinguish CIEs from FDEs. */
1218 cie_id
= DW64_CIE_ID
;
1224 cie_pointer
= read_8_bytes (unit
->abfd
, buf
);
1229 cie_pointer
= read_4_bytes (unit
->abfd
, buf
);
1233 if (cie_pointer
== cie_id
)
1235 /* This is a CIE. */
1236 struct dwarf2_cie
*cie
;
1239 /* Record the offset into the .debug_frame section of this CIE. */
1240 cie_pointer
= start
- unit
->dwarf_frame_buffer
;
1242 /* Check whether we've already read it. */
1243 if (find_cie (unit
, cie_pointer
))
1246 cie
= (struct dwarf2_cie
*)
1247 obstack_alloc (&unit
->objfile
->psymbol_obstack
,
1248 sizeof (struct dwarf2_cie
));
1249 cie
->initial_instructions
= NULL
;
1250 cie
->cie_pointer
= cie_pointer
;
1252 /* The encoding for FDE's in a normal .debug_frame section
1253 depends on the target address size as specified in the
1254 Compilation Unit Header. */
1255 cie
->encoding
= encoding_for_size (unit
->addr_size
);
1257 /* Check version number. */
1258 if (read_1_byte (unit
->abfd
, buf
) != DW_CIE_VERSION
)
1262 /* Interpret the interesting bits of the augmentation. */
1264 buf
= augmentation
+ strlen (augmentation
) + 1;
1266 /* The GCC 2.x "eh" augmentation has a pointer immediately
1267 following the augmentation string, so it must be handled
1269 if (augmentation
[0] == 'e' && augmentation
[1] == 'h')
1272 buf
+= TYPE_LENGTH (builtin_type_void_data_ptr
);
1276 cie
->code_alignment_factor
=
1277 read_unsigned_leb128 (unit
->abfd
, buf
, &bytes_read
);
1280 cie
->data_alignment_factor
=
1281 read_signed_leb128 (unit
->abfd
, buf
, &bytes_read
);
1284 cie
->return_address_register
= read_1_byte (unit
->abfd
, buf
);
1287 cie
->saw_z_augmentation
= (*augmentation
== 'z');
1288 if (cie
->saw_z_augmentation
)
1292 length
= read_unsigned_leb128 (unit
->abfd
, buf
, &bytes_read
);
1296 cie
->initial_instructions
= buf
+ length
;
1300 while (*augmentation
)
1302 /* "L" indicates a byte showing how the LSDA pointer is encoded. */
1303 if (*augmentation
== 'L')
1310 /* "R" indicates a byte indicating how FDE addresses are encoded. */
1311 else if (*augmentation
== 'R')
1313 cie
->encoding
= *buf
++;
1317 /* "P" indicates a personality routine in the CIE augmentation. */
1318 else if (*augmentation
== 'P')
1321 buf
+= size_of_encoded_value (*buf
++);
1325 /* Otherwise we have an unknown augmentation.
1326 Bail out unless we saw a 'z' prefix. */
1329 if (cie
->initial_instructions
== NULL
)
1332 /* Skip unknown augmentations. */
1333 buf
= cie
->initial_instructions
;
1338 cie
->initial_instructions
= buf
;
1341 add_cie (unit
, cie
);
1345 /* This is a FDE. */
1346 struct dwarf2_fde
*fde
;
1348 /* In an .eh_frame section, the CIE pointer is the delta between the
1349 address within the FDE where the CIE pointer is stored and the
1350 address of the CIE. Convert it to an offset into the .eh_frame
1354 cie_pointer
= buf
- unit
->dwarf_frame_buffer
- cie_pointer
;
1355 cie_pointer
-= (dwarf64_p
? 8 : 4);
1358 /* In either case, validate the result is still within the section. */
1359 if (cie_pointer
>= unit
->dwarf_frame_size
)
1362 fde
= (struct dwarf2_fde
*)
1363 obstack_alloc (&unit
->objfile
->psymbol_obstack
,
1364 sizeof (struct dwarf2_fde
));
1365 fde
->cie
= find_cie (unit
, cie_pointer
);
1366 if (fde
->cie
== NULL
)
1368 decode_frame_entry (unit
, unit
->dwarf_frame_buffer
+ cie_pointer
,
1370 fde
->cie
= find_cie (unit
, cie_pointer
);
1373 gdb_assert (fde
->cie
!= NULL
);
1375 fde
->initial_location
=
1376 read_encoded_value (unit
, fde
->cie
->encoding
, buf
, &bytes_read
);
1379 fde
->address_range
=
1380 read_encoded_value (unit
, fde
->cie
->encoding
& 0x0f, buf
, &bytes_read
);
1383 /* A 'z' augmentation in the CIE implies the presence of an
1384 augmentation field in the FDE as well. The only thing known
1385 to be in here at present is the LSDA entry for EH. So we
1386 can skip the whole thing. */
1387 if (fde
->cie
->saw_z_augmentation
)
1391 length
= read_unsigned_leb128 (unit
->abfd
, buf
, &bytes_read
);
1392 buf
+= bytes_read
+ length
;
1397 fde
->instructions
= buf
;
1400 add_fde (unit
, fde
);
1406 /* Read a CIE or FDE in BUF and decode it. */
1408 decode_frame_entry (struct comp_unit
*unit
, char *start
, int eh_frame_p
)
1410 enum { NONE
, ALIGN4
, ALIGN8
, FAIL
} workaround
= NONE
;
1413 ptrdiff_t start_offset
;
1417 ret
= decode_frame_entry_1 (unit
, start
, eh_frame_p
);
1421 /* We have corrupt input data of some form. */
1423 /* ??? Try, weakly, to work around compiler/assembler/linker bugs
1424 and mismatches wrt padding and alignment of debug sections. */
1425 /* Note that there is no requirement in the standard for any
1426 alignment at all in the frame unwind sections. Testing for
1427 alignment before trying to interpret data would be incorrect.
1429 However, GCC traditionally arranged for frame sections to be
1430 sized such that the FDE length and CIE fields happen to be
1431 aligned (in theory, for performance). This, unfortunately,
1432 was done with .align directives, which had the side effect of
1433 forcing the section to be aligned by the linker.
1435 This becomes a problem when you have some other producer that
1436 creates frame sections that are not as strictly aligned. That
1437 produces a hole in the frame info that gets filled by the
1440 The GCC behaviour is arguably a bug, but it's effectively now
1441 part of the ABI, so we're now stuck with it, at least at the
1442 object file level. A smart linker may decide, in the process
1443 of compressing duplicate CIE information, that it can rewrite
1444 the entire output section without this extra padding. */
1446 start_offset
= start
- unit
->dwarf_frame_buffer
;
1447 if (workaround
< ALIGN4
&& (start_offset
& 3) != 0)
1449 start
+= 4 - (start_offset
& 3);
1450 workaround
= ALIGN4
;
1453 if (workaround
< ALIGN8
&& (start_offset
& 7) != 0)
1455 start
+= 8 - (start_offset
& 7);
1456 workaround
= ALIGN8
;
1460 /* Nothing left to try. Arrange to return as if we've consumed
1461 the entire input section. Hopefully we'll get valid info from
1462 the other of .debug_frame/.eh_frame. */
1464 ret
= unit
->dwarf_frame_buffer
+ unit
->dwarf_frame_size
;
1474 complaint (&symfile_complaints
,
1475 "Corrupt data in %s:%s; align 4 workaround apparently succeeded",
1476 unit
->dwarf_frame_section
->owner
->filename
,
1477 unit
->dwarf_frame_section
->name
);
1481 complaint (&symfile_complaints
,
1482 "Corrupt data in %s:%s; align 8 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",
1490 unit
->dwarf_frame_section
->owner
->filename
,
1491 unit
->dwarf_frame_section
->name
);
1500 /* FIXME: kettenis/20030504: This still needs to be integrated with
1501 dwarf2read.c in a better way. */
1503 /* Imported from dwarf2read.c. */
1504 extern file_ptr dwarf_frame_offset
;
1505 extern unsigned int dwarf_frame_size
;
1506 extern asection
*dwarf_frame_section
;
1507 extern file_ptr dwarf_eh_frame_offset
;
1508 extern unsigned int dwarf_eh_frame_size
;
1509 extern asection
*dwarf_eh_frame_section
;
1511 /* Imported from dwarf2read.c. */
1512 extern char *dwarf2_read_section (struct objfile
*objfile
, file_ptr offset
,
1513 unsigned int size
, asection
*sectp
);
1516 dwarf2_build_frame_info (struct objfile
*objfile
)
1518 struct comp_unit unit
;
1521 /* Build a minimal decoding of the DWARF2 compilation unit. */
1522 unit
.abfd
= objfile
->obfd
;
1523 unit
.objfile
= objfile
;
1524 unit
.addr_size
= objfile
->obfd
->arch_info
->bits_per_address
/ 8;
1527 /* First add the information from the .eh_frame section. That way,
1528 the FDEs from that section are searched last. */
1529 if (dwarf_eh_frame_offset
)
1534 unit
.dwarf_frame_buffer
= dwarf2_read_section (objfile
,
1535 dwarf_eh_frame_offset
,
1536 dwarf_eh_frame_size
,
1537 dwarf_eh_frame_section
);
1539 unit
.dwarf_frame_size
= dwarf_eh_frame_size
;
1540 unit
.dwarf_frame_section
= dwarf_eh_frame_section
;
1542 /* FIXME: kettenis/20030602: This is the DW_EH_PE_datarel base
1543 that for the i386/amd64 target, which currently is the only
1544 target in GCC that supports/uses the DW_EH_PE_datarel
1546 got
= bfd_get_section_by_name (unit
.abfd
, ".got");
1548 unit
.dbase
= got
->vma
;
1550 frame_ptr
= unit
.dwarf_frame_buffer
;
1551 while (frame_ptr
< unit
.dwarf_frame_buffer
+ unit
.dwarf_frame_size
)
1552 frame_ptr
= decode_frame_entry (&unit
, frame_ptr
, 1);
1555 if (dwarf_frame_offset
)
1558 unit
.dwarf_frame_buffer
= dwarf2_read_section (objfile
,
1561 dwarf_frame_section
);
1562 unit
.dwarf_frame_size
= dwarf_frame_size
;
1563 unit
.dwarf_frame_section
= dwarf_frame_section
;
1565 frame_ptr
= unit
.dwarf_frame_buffer
;
1566 while (frame_ptr
< unit
.dwarf_frame_buffer
+ unit
.dwarf_frame_size
)
1567 frame_ptr
= decode_frame_entry (&unit
, frame_ptr
, 0);
1571 /* Provide a prototype to silence -Wmissing-prototypes. */
1572 void _initialize_dwarf2_frame (void);
1575 _initialize_dwarf2_frame (void)
1577 dwarf2_frame_data
= register_objfile_data ();