1 /* Stack unwinding code based on dwarf2 frame info for GDB, the GNU debugger.
3 Copyright 2001, 2002, 2003 Free Software Foundation, Inc.
5 Contributed by Jiri Smid, SuSE Labs.
6 Based on code written by Daniel Berlin (dan@dberlin.org).
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA. */
31 #include "elf/dwarf2.h"
34 #include "dwarf2cfi.h"
35 #include "gdb_assert.h"
37 /* Common Information Entry - holds information that is shared among many
41 /* Offset of this unit in .debug_frame or .eh_frame. */
44 /* A null-terminated string that identifies the augmentation to this CIE or
45 to the FDEs that use it. */
48 /* A constant that is factored out of all advance location instructions. */
49 unsigned int code_align
;
51 /* A constant that is factored out of all offset instructions. */
54 /* A constant that indicates which regiter represents the return address
58 /* Indicates how addresses are encoded. */
59 unsigned char addr_encoding
;
61 /* Pointer and length of the cie program. */
63 unsigned int data_length
;
65 struct objfile
*objfile
;
68 struct cie_unit
*next
;
71 /* Frame Description Entry. */
74 /* Address of the first location associated with this entry. */
75 CORE_ADDR initial_location
;
77 /* Length of program section described by this entry. */
78 CORE_ADDR address_range
;
80 /* Pointer to asociated CIE. */
81 struct cie_unit
*cie_ptr
;
83 /* Pointer and length of the cie program. */
85 unsigned int data_length
;
90 struct fde_unit
**array
;
95 struct frame_state_reg
116 /* Each register save state can be described in terms of a CFA slot,
117 another register, or a location expression. */
118 struct frame_state_regs
120 struct frame_state_reg
*reg
;
122 /* Used to implement DW_CFA_remember_state. */
123 struct frame_state_regs
*prev
;
127 /* The CFA can be described in terms of a reg+offset or a
128 location expression. */
131 unsigned char *cfa_exp
;
140 /* The PC described by the current frame state. */
143 /* The information we care about from the CIE/FDE. */
145 unsigned int code_align
;
146 unsigned char retaddr_column
;
147 unsigned char addr_encoding
;
149 struct objfile
*objfile
;
154 PE_absptr
= DW_EH_PE_absptr
,
155 PE_pcrel
= DW_EH_PE_pcrel
,
156 PE_textrel
= DW_EH_PE_textrel
,
157 PE_datarel
= DW_EH_PE_datarel
,
158 PE_funcrel
= DW_EH_PE_funcrel
161 #define UNWIND_CONTEXT(fi) ((struct context *) (deprecated_get_frame_context (fi)))
164 static struct cie_unit
*cie_chunks
;
165 static struct fde_array fde_chunks
;
166 /* Obstack for allocating temporary storage used during unwind operations. */
167 static struct obstack unwind_tmp_obstack
;
169 extern file_ptr dwarf_frame_offset
;
170 extern unsigned int dwarf_frame_size
;
171 extern file_ptr dwarf_eh_frame_offset
;
172 extern unsigned int dwarf_eh_frame_size
;
175 extern char *dwarf2_read_section (struct objfile
*objfile
, file_ptr offset
,
178 static struct fde_unit
*fde_unit_alloc (void);
179 static struct cie_unit
*cie_unit_alloc (void);
180 static void fde_chunks_need_space ();
182 static void unwind_tmp_obstack_init ();
183 static void unwind_tmp_obstack_free ();
185 static unsigned int read_1u (bfd
*abfd
, char **p
);
186 static int read_1s (bfd
*abfd
, char **p
);
187 static unsigned int read_2u (bfd
*abfd
, char **p
);
188 static int read_2s (bfd
*abfd
, char **p
);
189 static unsigned int read_4u (bfd
*abfd
, char **p
);
190 static int read_4s (bfd
*abfd
, char **p
);
191 static ULONGEST
read_8u (bfd
*abfd
, char **p
);
192 static LONGEST
read_8s (bfd
*abfd
, char **p
);
194 static ULONGEST
read_uleb128 (bfd
*abfd
, char **p
);
195 static LONGEST
read_sleb128 (bfd
*abfd
, char **p
);
196 static CORE_ADDR
read_pointer (bfd
*abfd
, char **p
);
197 static CORE_ADDR
read_encoded_pointer (bfd
*abfd
, char **p
,
198 unsigned char encoding
);
199 static enum ptr_encoding
pointer_encoding (unsigned char encoding
);
201 static LONGEST
read_initial_length (bfd
*abfd
, char *buf
, int *bytes_read
);
202 static ULONGEST
read_length (bfd
*abfd
, char *buf
, int *bytes_read
,
205 static int is_cie (ULONGEST cie_id
, int dwarf64
);
206 static int compare_fde_unit (const void *a
, const void *b
);
207 void dwarf2_build_frame_info (struct objfile
*objfile
);
209 static void execute_cfa_program (struct objfile
*objfile
, char *insn_ptr
,
210 char *insn_end
, struct context
*context
,
211 struct frame_state
*fs
);
212 static struct fde_unit
*get_fde_for_addr (CORE_ADDR pc
);
213 static void frame_state_for (struct context
*context
, struct frame_state
*fs
);
214 static void get_reg (char *reg
, struct context
*context
, int regnum
);
215 static CORE_ADDR
execute_stack_op (struct objfile
*objfile
,
216 char *op_ptr
, char *op_end
,
217 struct context
*context
,
219 static void update_context (struct context
*context
, struct frame_state
*fs
,
223 /* Memory allocation functions. */
224 static struct fde_unit
*
225 fde_unit_alloc (void)
227 struct fde_unit
*fde
;
229 fde
= (struct fde_unit
*) xmalloc (sizeof (struct fde_unit
));
230 memset (fde
, 0, sizeof (struct fde_unit
));
234 static struct cie_unit
*
235 cie_unit_alloc (void)
237 struct cie_unit
*cie
;
239 cie
= (struct cie_unit
*) xmalloc (sizeof (struct cie_unit
));
240 memset (cie
, 0, sizeof (struct cie_unit
));
245 fde_chunks_need_space (void)
247 if (fde_chunks
.elems
< fde_chunks
.array_size
)
249 fde_chunks
.array_size
=
250 fde_chunks
.array_size
? 2 * fde_chunks
.array_size
: 1024;
252 xrealloc (fde_chunks
.array
,
253 sizeof (struct fde_unit
) * fde_chunks
.array_size
);
256 /* Alocate a new `struct context' on temporary obstack. */
260 struct context
*context
;
262 int regs_size
= sizeof (struct context_reg
) * NUM_REGS
;
264 context
= (struct context
*) obstack_alloc (&unwind_tmp_obstack
,
265 sizeof (struct context
));
266 memset (context
, 0, sizeof (struct context
));
267 context
->reg
= (struct context_reg
*) obstack_alloc (&unwind_tmp_obstack
,
269 memset (context
->reg
, 0, regs_size
);
273 /* Alocate a new `struct frame_state' on temporary obstack. */
275 frame_state_alloc (void)
277 struct frame_state
*fs
;
279 int regs_size
= sizeof (struct frame_state_reg
) * NUM_REGS
;
281 fs
= (struct frame_state
*) obstack_alloc (&unwind_tmp_obstack
,
282 sizeof (struct frame_state
));
283 memset (fs
, 0, sizeof (struct frame_state
));
285 (struct frame_state_reg
*) obstack_alloc (&unwind_tmp_obstack
, regs_size
);
286 memset (fs
->regs
.reg
, 0, regs_size
);
291 unwind_tmp_obstack_init (void)
293 obstack_init (&unwind_tmp_obstack
);
297 unwind_tmp_obstack_free (void)
299 obstack_free (&unwind_tmp_obstack
, NULL
);
300 unwind_tmp_obstack_init ();
304 context_cpy (struct context
*dst
, struct context
*src
)
306 int regs_size
= sizeof (struct context_reg
) * NUM_REGS
;
307 struct context_reg
*dreg
;
309 /* Since `struct context' contains a pointer to an array with
310 register values, make sure we end up with a copy of that array,
311 and not with a copy of the pointer to that array. */
315 memcpy (dst
->reg
, src
->reg
, regs_size
);
319 read_1u (bfd
*abfd
, char **p
)
323 ret
= bfd_get_8 (abfd
, (bfd_byte
*) * p
);
329 read_1s (bfd
*abfd
, char **p
)
333 ret
= bfd_get_signed_8 (abfd
, (bfd_byte
*) * p
);
339 read_2u (bfd
*abfd
, char **p
)
343 ret
= bfd_get_16 (abfd
, (bfd_byte
*) * p
);
349 read_2s (bfd
*abfd
, char **p
)
353 ret
= bfd_get_signed_16 (abfd
, (bfd_byte
*) * p
);
359 read_4u (bfd
*abfd
, char **p
)
363 ret
= bfd_get_32 (abfd
, (bfd_byte
*) * p
);
369 read_4s (bfd
*abfd
, char **p
)
373 ret
= bfd_get_signed_32 (abfd
, (bfd_byte
*) * p
);
379 read_8u (bfd
*abfd
, char **p
)
383 ret
= bfd_get_64 (abfd
, (bfd_byte
*) * p
);
389 read_8s (bfd
*abfd
, char **p
)
393 ret
= bfd_get_signed_64 (abfd
, (bfd_byte
*) * p
);
399 read_uleb128 (bfd
*abfd
, char **p
)
410 byte
= bfd_get_8 (abfd
, (bfd_byte
*) * p
);
412 ret
|= ((unsigned long) (byte
& 127) << shift
);
413 if ((byte
& 128) == 0)
423 read_sleb128 (bfd
*abfd
, char **p
)
426 int i
, shift
, size
, num_read
;
436 byte
= bfd_get_8 (abfd
, (bfd_byte
*) * p
);
438 ret
|= ((long) (byte
& 127) << shift
);
440 if ((byte
& 128) == 0)
445 if ((shift
< size
) && (byte
& 0x40))
447 ret
|= -(1 << shift
);
453 read_pointer (bfd
*abfd
, char **p
)
455 switch (TARGET_ADDR_BIT
/ TARGET_CHAR_BIT
)
458 return read_4u (abfd
, p
);
460 return read_8u (abfd
, p
);
462 error ("dwarf cfi error: unsupported target address length.");
466 /* Read the appropriate amount of data from *P and return the
467 resulting value based on ENCODING, which the calling function must
470 read_encoded_pointer (bfd
*abfd
, char **p
, unsigned char encoding
)
474 switch (encoding
& 0x0f)
476 case DW_EH_PE_absptr
:
477 ret
= read_pointer (abfd
, p
);
480 case DW_EH_PE_uleb128
:
481 ret
= read_uleb128 (abfd
, p
);
483 case DW_EH_PE_sleb128
:
484 ret
= read_sleb128 (abfd
, p
);
487 case DW_EH_PE_udata2
:
488 ret
= read_2u (abfd
, p
);
490 case DW_EH_PE_udata4
:
491 ret
= read_4u (abfd
, p
);
493 case DW_EH_PE_udata8
:
494 ret
= read_8u (abfd
, p
);
497 case DW_EH_PE_sdata2
:
498 ret
= read_2s (abfd
, p
);
500 case DW_EH_PE_sdata4
:
501 ret
= read_4s (abfd
, p
);
503 case DW_EH_PE_sdata8
:
504 ret
= read_8s (abfd
, p
);
508 internal_error (__FILE__
, __LINE__
,
509 "read_encoded_pointer: unknown pointer encoding");
515 /* The variable 'encoding' carries three different flags:
516 - encoding & 0x0f : size of the address (handled in read_encoded_pointer())
517 - encoding & 0x70 : type (absolute, relative, ...)
518 - encoding & 0x80 : indirect flag (DW_EH_PE_indirect == 0x80). */
520 pointer_encoding (unsigned char encoding
)
524 if (encoding
& DW_EH_PE_indirect
)
525 warning ("CFI: Unsupported pointer encoding: DW_EH_PE_indirect");
527 switch (encoding
& 0x70)
529 case DW_EH_PE_absptr
:
531 case DW_EH_PE_textrel
:
532 case DW_EH_PE_datarel
:
533 case DW_EH_PE_funcrel
:
534 ret
= encoding
& 0x70;
537 internal_error (__FILE__
, __LINE__
, "CFI: unknown pointer encoding");
543 read_initial_length (bfd
*abfd
, char *buf
, int *bytes_read
)
547 ret
= bfd_get_32 (abfd
, (bfd_byte
*) buf
);
549 if (ret
== 0xffffffff)
551 ret
= bfd_get_64 (abfd
, (bfd_byte
*) buf
+ 4);
563 read_length (bfd
*abfd
, char *buf
, int *bytes_read
, int dwarf64
)
568 return read_8u (abfd
, &buf
);
573 return read_4u (abfd
, &buf
);
578 execute_cfa_program (struct objfile
*objfile
, char *insn_ptr
, char *insn_end
,
579 struct context
*context
, struct frame_state
*fs
)
581 struct frame_state_regs
*unused_rs
= NULL
;
583 /* Don't allow remember/restore between CIE and FDE programs. */
584 fs
->regs
.prev
= NULL
;
586 while (insn_ptr
< insn_end
&& fs
->pc
< context
->ra
)
588 unsigned char insn
= *insn_ptr
++;
589 ULONGEST reg
, uoffset
;
592 if (insn
& DW_CFA_advance_loc
)
593 fs
->pc
+= (insn
& 0x3f) * fs
->code_align
;
594 else if (insn
& DW_CFA_offset
)
597 uoffset
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
598 offset
= (long) uoffset
*fs
->data_align
;
599 fs
->regs
.reg
[reg
].how
= REG_SAVED_OFFSET
;
600 fs
->regs
.reg
[reg
].loc
.offset
= offset
;
602 else if (insn
& DW_CFA_restore
)
605 fs
->regs
.reg
[reg
].how
= REG_UNSAVED
;
611 fs
->pc
= read_encoded_pointer (objfile
->obfd
, &insn_ptr
,
614 if (pointer_encoding (fs
->addr_encoding
) != PE_absptr
)
615 warning ("CFI: DW_CFA_set_loc uses relative addressing");
619 case DW_CFA_advance_loc1
:
620 fs
->pc
+= read_1u (objfile
->obfd
, &insn_ptr
);
622 case DW_CFA_advance_loc2
:
623 fs
->pc
+= read_2u (objfile
->obfd
, &insn_ptr
);
625 case DW_CFA_advance_loc4
:
626 fs
->pc
+= read_4u (objfile
->obfd
, &insn_ptr
);
629 case DW_CFA_offset_extended
:
630 reg
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
631 uoffset
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
632 offset
= (long) uoffset
*fs
->data_align
;
633 fs
->regs
.reg
[reg
].how
= REG_SAVED_OFFSET
;
634 fs
->regs
.reg
[reg
].loc
.offset
= offset
;
637 case DW_CFA_restore_extended
:
638 reg
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
639 fs
->regs
.reg
[reg
].how
= REG_UNSAVED
;
642 case DW_CFA_undefined
:
643 case DW_CFA_same_value
:
647 case DW_CFA_register
:
650 reg
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
651 reg2
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
652 fs
->regs
.reg
[reg
].how
= REG_SAVED_REG
;
653 fs
->regs
.reg
[reg
].loc
.reg
= reg2
;
657 case DW_CFA_remember_state
:
659 struct frame_state_regs
*new_rs
;
663 unused_rs
= unused_rs
->prev
;
666 new_rs
= xmalloc (sizeof (struct frame_state_regs
));
669 fs
->regs
.prev
= new_rs
;
673 case DW_CFA_restore_state
:
675 struct frame_state_regs
*old_rs
= fs
->regs
.prev
;
677 old_rs
->prev
= unused_rs
;
683 reg
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
684 uoffset
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
686 fs
->cfa_offset
= uoffset
;
687 fs
->cfa_how
= CFA_REG_OFFSET
;
690 case DW_CFA_def_cfa_register
:
691 reg
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
693 fs
->cfa_how
= CFA_REG_OFFSET
;
696 case DW_CFA_def_cfa_offset
:
697 uoffset
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
698 fs
->cfa_offset
= uoffset
;
701 case DW_CFA_def_cfa_expression
:
702 uoffset
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
703 fs
->cfa_exp
= insn_ptr
;
704 fs
->cfa_how
= CFA_EXP
;
708 case DW_CFA_expression
:
709 reg
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
710 uoffset
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
711 fs
->regs
.reg
[reg
].how
= REG_SAVED_EXP
;
712 fs
->regs
.reg
[reg
].loc
.exp
= insn_ptr
;
716 /* From the 2.1 draft. */
717 case DW_CFA_offset_extended_sf
:
718 reg
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
719 offset
= read_sleb128 (objfile
->obfd
, &insn_ptr
);
720 offset
*= fs
->data_align
;
721 fs
->regs
.reg
[reg
].how
= REG_SAVED_OFFSET
;
722 fs
->regs
.reg
[reg
].loc
.offset
= offset
;
725 case DW_CFA_def_cfa_sf
:
726 reg
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
727 offset
= read_sleb128 (objfile
->obfd
, &insn_ptr
);
728 fs
->cfa_offset
= offset
;
730 fs
->cfa_how
= CFA_REG_OFFSET
;
733 case DW_CFA_def_cfa_offset_sf
:
734 uoffset
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
735 fs
->cfa_offset
= uoffset
;
736 /* cfa_how deliberately not set. */
739 case DW_CFA_GNU_window_save
:
740 /* ??? Hardcoded for SPARC register window configuration. */
741 for (reg
= 16; reg
< 32; ++reg
)
743 fs
->regs
.reg
[reg
].how
= REG_SAVED_OFFSET
;
744 fs
->regs
.reg
[reg
].loc
.offset
= (reg
- 16) * sizeof (void *);
748 case DW_CFA_GNU_args_size
:
749 uoffset
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
750 context
->args_size
= uoffset
;
753 case DW_CFA_GNU_negative_offset_extended
:
754 /* Obsoleted by DW_CFA_offset_extended_sf, but used by
755 older PowerPC code. */
756 reg
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
757 uoffset
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
758 offset
= (long) uoffset
*fs
->data_align
;
759 fs
->regs
.reg
[reg
].how
= REG_SAVED_OFFSET
;
760 fs
->regs
.reg
[reg
].loc
.offset
= -offset
;
764 error ("dwarf cfi error: unknown cfa instruction %d.", insn
);
769 static struct fde_unit
*
770 get_fde_for_addr (CORE_ADDR pc
)
773 struct fde_unit
*fde
= NULL
;
775 hi
= fde_chunks
.elems
;
779 size_t i
= (lo
+ hi
) / 2;
780 fde
= fde_chunks
.array
[i
];
781 if (pc
< fde
->initial_location
)
783 else if (pc
>= fde
->initial_location
+ fde
->address_range
)
792 frame_state_for (struct context
*context
, struct frame_state
*fs
)
794 struct fde_unit
*fde
;
795 struct cie_unit
*cie
;
797 context
->args_size
= 0;
800 fde
= get_fde_for_addr (context
->ra
- 1);
805 fs
->pc
= fde
->initial_location
;
807 gdb_assert (fde
->cie_ptr
!= NULL
);
811 fs
->code_align
= cie
->code_align
;
812 fs
->data_align
= cie
->data_align
;
813 fs
->retaddr_column
= cie
->ra
;
814 fs
->addr_encoding
= cie
->addr_encoding
;
815 fs
->objfile
= cie
->objfile
;
817 execute_cfa_program (cie
->objfile
, cie
->data
,
818 cie
->data
+ cie
->data_length
, context
, fs
);
819 execute_cfa_program (cie
->objfile
, fde
->data
,
820 fde
->data
+ fde
->data_length
, context
, fs
);
824 get_reg (char *reg
, struct context
*context
, int regnum
)
826 switch (context
->reg
[regnum
].how
)
828 case REG_CTX_UNSAVED
:
829 deprecated_read_register_gen (regnum
, reg
);
831 case REG_CTX_SAVED_OFFSET
:
832 target_read_memory (context
->cfa
+ context
->reg
[regnum
].loc
.offset
,
833 reg
, REGISTER_RAW_SIZE (regnum
));
835 case REG_CTX_SAVED_REG
:
836 deprecated_read_register_gen (context
->reg
[regnum
].loc
.reg
, reg
);
838 case REG_CTX_SAVED_ADDR
:
839 target_read_memory (context
->reg
[regnum
].loc
.addr
,
840 reg
, REGISTER_RAW_SIZE (regnum
));
843 memcpy (reg
, &context
->reg
[regnum
].loc
.addr
,
844 REGISTER_RAW_SIZE (regnum
));
847 internal_error (__FILE__
, __LINE__
, "get_reg: unknown register rule");
851 /* Decode a DW_OP stack program. Return the top of stack. Push INITIAL
852 onto the stack to start. */
854 execute_stack_op (struct objfile
*objfile
,
855 char *op_ptr
, char *op_end
, struct context
*context
,
858 CORE_ADDR stack
[64]; /* ??? Assume this is enough. */
864 while (op_ptr
< op_end
)
866 enum dwarf_location_atom op
= *op_ptr
++;
905 result
= op
- DW_OP_lit0
;
909 result
= read_pointer (objfile
->obfd
, &op_ptr
);
913 result
= read_1u (objfile
->obfd
, &op_ptr
);
916 result
= read_1s (objfile
->obfd
, &op_ptr
);
919 result
= read_2u (objfile
->obfd
, &op_ptr
);
922 result
= read_2s (objfile
->obfd
, &op_ptr
);
925 result
= read_4u (objfile
->obfd
, &op_ptr
);
928 result
= read_4s (objfile
->obfd
, &op_ptr
);
931 result
= read_8u (objfile
->obfd
, &op_ptr
);
934 result
= read_8s (objfile
->obfd
, &op_ptr
);
937 result
= read_uleb128 (objfile
->obfd
, &op_ptr
);
940 result
= read_sleb128 (objfile
->obfd
, &op_ptr
);
975 get_reg ((char *) &result
, context
, op
- DW_OP_reg0
);
978 reg
= read_uleb128 (objfile
->obfd
, &op_ptr
);
979 get_reg ((char *) &result
, context
, reg
);
1014 offset
= read_sleb128 (objfile
->obfd
, &op_ptr
);
1015 get_reg ((char *) &result
, context
, op
- DW_OP_breg0
);
1019 reg
= read_uleb128 (objfile
->obfd
, &op_ptr
);
1020 offset
= read_sleb128 (objfile
->obfd
, &op_ptr
);
1021 get_reg ((char *) &result
, context
, reg
);
1027 internal_error (__FILE__
, __LINE__
, "execute_stack_op error");
1028 result
= stack
[stack_elt
- 1];
1032 if (--stack_elt
< 0)
1033 internal_error (__FILE__
, __LINE__
, "execute_stack_op error");
1038 if (offset
>= stack_elt
- 1)
1039 internal_error (__FILE__
, __LINE__
, "execute_stack_op error");
1040 result
= stack
[stack_elt
- 1 - offset
];
1045 internal_error (__FILE__
, __LINE__
, "execute_stack_op error");
1046 result
= stack
[stack_elt
- 2];
1051 CORE_ADDR t1
, t2
, t3
;
1054 internal_error (__FILE__
, __LINE__
, "execute_stack_op error");
1055 t1
= stack
[stack_elt
- 1];
1056 t2
= stack
[stack_elt
- 2];
1057 t3
= stack
[stack_elt
- 3];
1058 stack
[stack_elt
- 1] = t2
;
1059 stack
[stack_elt
- 2] = t3
;
1060 stack
[stack_elt
- 3] = t1
;
1065 case DW_OP_deref_size
:
1069 case DW_OP_plus_uconst
:
1070 /* Unary operations. */
1071 if (--stack_elt
< 0)
1072 internal_error (__FILE__
, __LINE__
, "execute_stack_op error");
1073 result
= stack
[stack_elt
];
1079 int len
= TARGET_ADDR_BIT
/ TARGET_CHAR_BIT
;
1080 if (len
!= 4 && len
!= 8)
1081 internal_error (__FILE__
, __LINE__
,
1082 "execute_stack_op error");
1083 result
= read_memory_unsigned_integer (result
, len
);
1087 case DW_OP_deref_size
:
1089 int len
= *op_ptr
++;
1090 if (len
!= 1 && len
!= 2 && len
!= 4 && len
!= 8)
1091 internal_error (__FILE__
, __LINE__
,
1092 "execute_stack_op error");
1093 result
= read_memory_unsigned_integer (result
, len
);
1107 case DW_OP_plus_uconst
:
1108 result
+= read_uleb128 (objfile
->obfd
, &op_ptr
);
1129 /* Binary operations. */
1130 CORE_ADDR first
, second
;
1131 if ((stack_elt
-= 2) < 0)
1132 internal_error (__FILE__
, __LINE__
, "execute_stack_op error");
1133 second
= stack
[stack_elt
];
1134 first
= stack
[stack_elt
+ 1];
1139 result
= second
& first
;
1142 result
= (LONGEST
) second
/ (LONGEST
) first
;
1145 result
= second
- first
;
1148 result
= (LONGEST
) second
% (LONGEST
) first
;
1151 result
= second
* first
;
1154 result
= second
| first
;
1157 result
= second
+ first
;
1160 result
= second
<< first
;
1163 result
= second
>> first
;
1166 result
= (LONGEST
) second
>> first
;
1169 result
= second
^ first
;
1172 result
= (LONGEST
) first
<= (LONGEST
) second
;
1175 result
= (LONGEST
) first
>= (LONGEST
) second
;
1178 result
= (LONGEST
) first
== (LONGEST
) second
;
1181 result
= (LONGEST
) first
< (LONGEST
) second
;
1184 result
= (LONGEST
) first
> (LONGEST
) second
;
1187 result
= (LONGEST
) first
!= (LONGEST
) second
;
1190 error ("execute_stack_op: Unknown DW_OP_ value");
1197 offset
= read_2s (objfile
->obfd
, &op_ptr
);
1202 if (--stack_elt
< 0)
1203 internal_error (__FILE__
, __LINE__
, "execute_stack_op error");
1204 offset
= read_2s (objfile
->obfd
, &op_ptr
);
1205 if (stack
[stack_elt
] != 0)
1213 internal_error (__FILE__
, __LINE__
, "execute_stack_op error");
1216 /* Most things push a result value. */
1217 if ((size_t) stack_elt
>= sizeof (stack
) / sizeof (*stack
))
1218 internal_error (__FILE__
, __LINE__
, "execute_stack_op error");
1219 stack
[++stack_elt
] = result
;
1223 /* We were executing this program to get a value. It should be
1225 if (--stack_elt
< 0)
1226 internal_error (__FILE__
, __LINE__
, "execute_stack_op error");
1227 return stack
[stack_elt
];
1231 update_context (struct context
*context
, struct frame_state
*fs
, int chain
)
1233 struct context
*orig_context
;
1237 unwind_tmp_obstack_init ();
1239 orig_context
= context_alloc ();
1240 context_cpy (orig_context
, context
);
1242 /* Compute this frame's CFA. */
1243 switch (fs
->cfa_how
)
1245 case CFA_REG_OFFSET
:
1246 get_reg ((char *) &cfa
, context
, fs
->cfa_reg
);
1247 cfa
+= fs
->cfa_offset
;
1251 /* ??? No way of knowing what register number is the stack pointer
1252 to do the same sort of handling as above. Assume that if the
1253 CFA calculation is so complicated as to require a stack program
1254 that this will not be a problem. */
1256 char *exp
= fs
->cfa_exp
;
1259 len
= read_uleb128 (fs
->objfile
->obfd
, &exp
);
1260 cfa
= (CORE_ADDR
) execute_stack_op (fs
->objfile
, exp
,
1261 exp
+ len
, context
, 0);
1270 orig_context
->cfa
= cfa
;
1272 /* Compute the addresses of all registers saved in this frame. */
1273 for (i
= 0; i
< NUM_REGS
; ++i
)
1274 switch (fs
->regs
.reg
[i
].how
)
1279 context
->reg
[i
].how
= REG_CTX_VALUE
;
1280 context
->reg
[i
].loc
.addr
= cfa
;
1283 context
->reg
[i
].how
= REG_CTX_UNSAVED
;
1285 case REG_SAVED_OFFSET
:
1286 context
->reg
[i
].how
= REG_CTX_SAVED_OFFSET
;
1287 context
->reg
[i
].loc
.offset
= fs
->regs
.reg
[i
].loc
.offset
;
1290 switch (orig_context
->reg
[fs
->regs
.reg
[i
].loc
.reg
].how
)
1292 case REG_CTX_UNSAVED
:
1293 context
->reg
[i
].how
= REG_CTX_UNSAVED
;
1295 case REG_CTX_SAVED_OFFSET
:
1296 context
->reg
[i
].how
= REG_CTX_SAVED_OFFSET
;
1297 context
->reg
[i
].loc
.offset
= orig_context
->cfa
- context
->cfa
+
1298 orig_context
->reg
[fs
->regs
.reg
[i
].loc
.reg
].loc
.offset
;
1300 case REG_CTX_SAVED_REG
:
1301 context
->reg
[i
].how
= REG_CTX_SAVED_REG
;
1302 context
->reg
[i
].loc
.reg
=
1303 orig_context
->reg
[fs
->regs
.reg
[i
].loc
.reg
].loc
.reg
;
1305 case REG_CTX_SAVED_ADDR
:
1306 context
->reg
[i
].how
= REG_CTX_SAVED_ADDR
;
1307 context
->reg
[i
].loc
.addr
=
1308 orig_context
->reg
[fs
->regs
.reg
[i
].loc
.reg
].loc
.addr
;
1311 internal_error (__FILE__
, __LINE__
, "bad switch");
1316 char *exp
= fs
->regs
.reg
[i
].loc
.exp
;
1320 len
= read_uleb128 (fs
->objfile
->obfd
, &exp
);
1321 val
= execute_stack_op (fs
->objfile
, exp
, exp
+ len
,
1323 context
->reg
[i
].how
= REG_CTX_SAVED_ADDR
;
1324 context
->reg
[i
].loc
.addr
= val
;
1328 internal_error (__FILE__
, __LINE__
, "bad switch");
1330 get_reg ((char *) &context
->ra
, context
, fs
->retaddr_column
);
1331 unwind_tmp_obstack_free ();
1335 is_cie (ULONGEST cie_id
, int dwarf64
)
1337 return dwarf64
? (cie_id
== 0xffffffffffffffff) : (cie_id
== 0xffffffff);
1341 compare_fde_unit (const void *a
, const void *b
)
1343 struct fde_unit
**first
, **second
;
1344 first
= (struct fde_unit
**) a
;
1345 second
= (struct fde_unit
**) b
;
1346 if ((*first
)->initial_location
> (*second
)->initial_location
)
1348 else if ((*first
)->initial_location
< (*second
)->initial_location
)
1354 /* Build the cie_chunks and fde_chunks tables from informations
1355 found in .debug_frame and .eh_frame sections. */
1356 /* We can handle both of these sections almost in the same way, however there
1357 are some exceptions:
1358 - CIE ID is -1 in debug_frame, but 0 in eh_frame
1359 - eh_frame may contain some more information that are used only by gcc
1360 (eg. personality pointer, LSDA pointer, ...). Most of them we can ignore.
1361 - In debug_frame FDE's item cie_id contains offset of it's parent CIE.
1362 In eh_frame FDE's item cie_id is a relative pointer to the parent CIE.
1363 Anyway we don't need to bother with this, because we are smart enough
1364 to keep the pointer to the parent CIE of oncomming FDEs in 'last_cie'.
1365 - Although debug_frame items can contain Augmentation as well as
1366 eh_frame ones, I have never seen them non-empty. Thus only in eh_frame
1367 we can encounter for example non-absolute pointers (Aug. 'R').
1370 parse_frame_info (struct objfile
*objfile
, file_ptr frame_offset
,
1371 unsigned int frame_size
, int eh_frame
)
1373 bfd
*abfd
= objfile
->obfd
;
1374 asection
*curr_section_ptr
;
1377 char *frame_buffer
= NULL
;
1378 char *curr_section_name
, *aug_data
;
1379 struct cie_unit
*last_cie
= NULL
;
1380 int last_dup_fde
= 0;
1382 CORE_ADDR curr_section_vma
= 0;
1384 unwind_tmp_obstack_init ();
1386 frame_buffer
= dwarf2_read_section (objfile
, frame_offset
, frame_size
);
1388 start
= frame_buffer
;
1389 end
= frame_buffer
+ frame_size
;
1391 curr_section_name
= eh_frame
? ".eh_frame" : ".debug_frame";
1392 curr_section_ptr
= bfd_get_section_by_name (abfd
, curr_section_name
);
1393 if (curr_section_ptr
)
1394 curr_section_vma
= curr_section_ptr
->vma
;
1400 unsigned long length
;
1402 ULONGEST unit_offset
= start
- frame_buffer
;
1403 int bytes_read
, dwarf64
;
1406 length
= read_initial_length (abfd
, start
, &bytes_read
);
1407 start
+= bytes_read
;
1408 dwarf64
= (bytes_read
== 12);
1409 block_end
= start
+ length
;
1417 cie_id
= read_length (abfd
, start
, &bytes_read
, dwarf64
);
1418 start
+= bytes_read
;
1420 if ((eh_frame
&& cie_id
== 0) || is_cie (cie_id
, dwarf64
))
1422 struct cie_unit
*cie
= cie_unit_alloc ();
1425 cie
->objfile
= objfile
;
1426 cie
->next
= cie_chunks
;
1429 cie
->objfile
= objfile
;
1431 cie
->offset
= unit_offset
;
1433 start
++; /* version */
1435 cie
->augmentation
= aug
= start
;
1436 while (*start
++); /* Skips last NULL as well */
1438 cie
->code_align
= read_uleb128 (abfd
, &start
);
1439 cie
->data_align
= read_sleb128 (abfd
, &start
);
1440 cie
->ra
= read_1u (abfd
, &start
);
1443 z Indicates that a uleb128 is present to size the
1444 augmentation section.
1445 L Indicates the encoding (and thus presence) of
1446 an LSDA pointer in the FDE augmentation.
1447 R Indicates a non-default pointer encoding for
1449 P Indicates the presence of an encoding + language
1450 personality routine in the CIE augmentation.
1452 [This info comes from GCC's dwarf2out.c]
1456 aug_len
= read_uleb128 (abfd
, &start
);
1463 cie
->data_length
= block_end
- cie
->data
;
1465 while (*aug
!= '\0')
1467 if (aug
[0] == 'e' && aug
[1] == 'h')
1469 aug_data
+= sizeof (void *);
1472 else if (aug
[0] == 'R')
1473 cie
->addr_encoding
= *aug_data
++;
1474 else if (aug
[0] == 'P')
1476 CORE_ADDR pers_addr
;
1479 pers_addr_enc
= *aug_data
++;
1480 /* We don't need pers_addr value and so we
1481 don't care about it's encoding. */
1482 pers_addr
= read_encoded_pointer (abfd
, &aug_data
,
1485 else if (aug
[0] == 'L' && eh_frame
)
1489 /* Perhaps we should save this to CIE for later use?
1490 Do we need it for something in GDB? */
1491 lsda_addr_enc
= *aug_data
++;
1494 warning ("CFI warning: unknown augmentation \"%c\""
1496 "\t%s", aug
[0], curr_section_name
,
1505 struct fde_unit
*fde
;
1506 struct cie_unit
*cie
;
1510 /* We assume that debug_frame is in order
1511 CIE,FDE,CIE,FDE,FDE,... and thus the CIE for this FDE
1512 should be stored in last_cie pointer. If not, we'll
1513 try to find it by the older way. */
1518 warning ("CFI: last_cie == NULL. "
1519 "Perhaps a malformed %s section in '%s'...?\n",
1520 curr_section_name
, objfile
->name
);
1525 if (cie
->objfile
== objfile
)
1529 (unit_offset
+ bytes_read
- cie_id
)))
1531 if (!eh_frame
&& (cie
->offset
== cie_id
))
1538 error ("CFI: can't find CIE pointer");
1541 init_loc
= read_encoded_pointer (abfd
, &start
,
1542 cie
->addr_encoding
);
1544 switch (pointer_encoding (cie
->addr_encoding
))
1549 /* start-frame_buffer gives offset from
1550 the beginning of actual section. */
1551 init_loc
+= curr_section_vma
+ start
- frame_buffer
;
1554 warning ("CFI: Unsupported pointer encoding\n");
1557 /* For relocatable objects we must add an offset telling
1558 where the section is actually mapped in the memory. */
1559 init_loc
+= ANOFFSET (objfile
->section_offsets
,
1560 SECT_OFF_TEXT (objfile
));
1562 /* If we have both .debug_frame and .eh_frame present in
1563 a file, we must eliminate duplicate FDEs. For now we'll
1564 run through all entries in fde_chunks and check it one
1565 by one. Perhaps in the future we can implement a faster
1566 searching algorithm. */
1567 /* eh_frame==2 indicates, that this file has an already
1568 parsed .debug_frame too. When eh_frame==1 it means, that no
1569 .debug_frame is present and thus we don't need to check for
1570 duplicities. eh_frame==0 means, that we parse .debug_frame
1571 and don't need to care about duplicate FDEs, because
1572 .debug_frame is parsed first. */
1574 for (i
= 0; eh_frame
== 2 && i
< fde_chunks
.elems
; i
++)
1576 /* We assume that FDEs in .debug_frame and .eh_frame
1577 have the same order (if they are present, of course).
1578 If we find a duplicate entry for one FDE and save
1579 it's index to last_dup_fde it's very likely, that
1580 we'll find an entry for the following FDE right after
1581 the previous one. Thus in many cases we'll run this
1583 last_dup_fde
= (last_dup_fde
+ i
) % fde_chunks
.elems
;
1584 if (fde_chunks
.array
[last_dup_fde
]->initial_location
1592 /* Allocate a new entry only if this FDE isn't a duplicate of
1593 something we have already seen. */
1596 fde_chunks_need_space ();
1597 fde
= fde_unit_alloc ();
1599 fde_chunks
.array
[fde_chunks
.elems
++] = fde
;
1601 fde
->initial_location
= init_loc
;
1602 fde
->address_range
= read_encoded_pointer (abfd
, &start
,
1608 /* Here we intentionally ignore augmentation data
1609 from FDE, because we don't need them. */
1610 if (cie
->augmentation
[0] == 'z')
1611 start
+= read_uleb128 (abfd
, &start
);
1614 fde
->data_length
= block_end
- start
;
1619 qsort (fde_chunks
.array
, fde_chunks
.elems
,
1620 sizeof (struct fde_unit
*), compare_fde_unit
);
1624 /* We must parse both .debug_frame section and .eh_frame because
1625 * not all frames must be present in both of these sections. */
1627 dwarf2_build_frame_info (struct objfile
*objfile
)
1629 int after_debug_frame
= 0;
1631 /* If we have .debug_frame then the parser is called with
1632 eh_frame==0 for .debug_frame and eh_frame==2 for .eh_frame,
1633 otherwise it's only called once for .eh_frame with argument
1636 if (dwarf_frame_offset
)
1638 parse_frame_info (objfile
, dwarf_frame_offset
,
1639 dwarf_frame_size
, 0 /* = debug_frame */ );
1640 after_debug_frame
= 1;
1643 if (dwarf_eh_frame_offset
)
1644 parse_frame_info (objfile
, dwarf_eh_frame_offset
, dwarf_eh_frame_size
,
1645 1 /* = eh_frame */ + after_debug_frame
);
1648 /* Return the frame address. */
1652 struct context
*context
;
1653 struct frame_state
*fs
;
1656 unwind_tmp_obstack_init ();
1658 context
= context_alloc ();
1659 fs
= frame_state_alloc ();
1661 context
->ra
= read_pc () + 1;
1663 frame_state_for (context
, fs
);
1664 update_context (context
, fs
, 0);
1668 unwind_tmp_obstack_free ();
1673 /* Store the frame address. This function is not used. */
1676 cfi_write_fp (CORE_ADDR val
)
1678 struct context
*context
;
1679 struct frame_state
*fs
;
1681 unwind_tmp_obstack_init ();
1683 context
= context_alloc ();
1684 fs
= frame_state_alloc ();
1686 context
->ra
= read_pc () + 1;
1688 frame_state_for (context
, fs
);
1690 if (fs
->cfa_how
== CFA_REG_OFFSET
)
1692 val
-= fs
->cfa_offset
;
1693 deprecated_write_register_gen (fs
->cfa_reg
, (char *) &val
);
1696 warning ("Can't write fp.");
1698 unwind_tmp_obstack_free ();
1701 /* Restore the machine to the state it had before the current frame
1704 cfi_pop_frame (struct frame_info
*fi
)
1706 char *regbuf
= alloca (MAX_REGISTER_RAW_SIZE
);
1709 for (regnum
= 0; regnum
< NUM_REGS
; regnum
++)
1711 get_reg (regbuf
, UNWIND_CONTEXT (fi
), regnum
);
1712 deprecated_write_register_bytes (REGISTER_BYTE (regnum
), regbuf
,
1713 REGISTER_RAW_SIZE (regnum
));
1715 write_register (PC_REGNUM
, UNWIND_CONTEXT (fi
)->ra
);
1717 flush_cached_frames ();
1720 /* Determine the address of the calling function's frame. */
1722 cfi_frame_chain (struct frame_info
*fi
)
1724 struct context
*context
;
1725 struct frame_state
*fs
;
1728 unwind_tmp_obstack_init ();
1730 context
= context_alloc ();
1731 fs
= frame_state_alloc ();
1732 context_cpy (context
, UNWIND_CONTEXT (fi
));
1734 /* outermost frame */
1735 if (context
->ra
== 0)
1737 unwind_tmp_obstack_free ();
1741 frame_state_for (context
, fs
);
1742 update_context (context
, fs
, 1);
1745 unwind_tmp_obstack_free ();
1750 /* Sets the pc of the frame. */
1752 cfi_init_frame_pc (int fromleaf
, struct frame_info
*fi
)
1754 if (get_next_frame (fi
))
1757 /* FIXME: cagney/2002-12-04: This is straight wrong. It's
1758 assuming that the PC is CORE_ADDR (a host quantity) in size. */
1759 get_reg ((void *)&pc
, UNWIND_CONTEXT (get_next_frame (fi
)), PC_REGNUM
);
1766 /* Initialize unwind context informations of the frame. */
1768 cfi_init_extra_frame_info (int fromleaf
, struct frame_info
*fi
)
1770 struct frame_state
*fs
;
1772 unwind_tmp_obstack_init ();
1774 fs
= frame_state_alloc ();
1775 deprecated_set_frame_context (fi
, frame_obstack_zalloc (sizeof (struct context
)));
1776 UNWIND_CONTEXT (fi
)->reg
=
1777 frame_obstack_zalloc (sizeof (struct context_reg
) * NUM_REGS
);
1778 memset (UNWIND_CONTEXT (fi
)->reg
, 0,
1779 sizeof (struct context_reg
) * NUM_REGS
);
1781 if (get_next_frame (fi
))
1783 context_cpy (UNWIND_CONTEXT (fi
), UNWIND_CONTEXT (get_next_frame (fi
)));
1784 frame_state_for (UNWIND_CONTEXT (fi
), fs
);
1785 update_context (UNWIND_CONTEXT (fi
), fs
, 1);
1789 UNWIND_CONTEXT (fi
)->ra
= get_frame_pc (fi
) + 1;
1790 frame_state_for (UNWIND_CONTEXT (fi
), fs
);
1791 update_context (UNWIND_CONTEXT (fi
), fs
, 0);
1794 unwind_tmp_obstack_free ();
1797 /* Obtain return address of the frame. */
1799 cfi_get_ra (struct frame_info
*fi
)
1801 return UNWIND_CONTEXT (fi
)->ra
;
1804 /* Find register number REGNUM relative to FRAME and put its
1805 (raw) contents in *RAW_BUFFER. Set *OPTIMIZED if the variable
1806 was optimized out (and thus can't be fetched). If the variable
1807 was fetched from memory, set *ADDRP to where it was fetched from,
1808 otherwise it was fetched from a register.
1810 The argument RAW_BUFFER must point to aligned memory. */
1812 cfi_get_saved_register (char *raw_buffer
,
1815 struct frame_info
*frame
,
1816 int regnum
, enum lval_type
*lval
)
1818 if (!target_has_registers
)
1819 error ("No registers.");
1821 /* Normal systems don't optimize out things with register numbers. */
1822 if (optimized
!= NULL
)
1825 if (addrp
) /* default assumption: not found in memory */
1828 if (!get_next_frame (frame
))
1830 deprecated_read_register_gen (regnum
, raw_buffer
);
1832 *lval
= lval_register
;
1834 *addrp
= REGISTER_BYTE (regnum
);
1838 frame
= get_next_frame (frame
);
1839 switch (UNWIND_CONTEXT (frame
)->reg
[regnum
].how
)
1841 case REG_CTX_UNSAVED
:
1842 deprecated_read_register_gen (regnum
, raw_buffer
);
1845 if (optimized
!= NULL
)
1848 case REG_CTX_SAVED_OFFSET
:
1849 target_read_memory (UNWIND_CONTEXT (frame
)->cfa
+
1850 UNWIND_CONTEXT (frame
)->reg
[regnum
].loc
.offset
,
1851 raw_buffer
, REGISTER_RAW_SIZE (regnum
));
1853 *lval
= lval_memory
;
1856 UNWIND_CONTEXT (frame
)->cfa
+
1857 UNWIND_CONTEXT (frame
)->reg
[regnum
].loc
.offset
;
1859 case REG_CTX_SAVED_REG
:
1860 deprecated_read_register_gen (UNWIND_CONTEXT (frame
)->reg
[regnum
].loc
.reg
,
1863 *lval
= lval_register
;
1866 REGISTER_BYTE (UNWIND_CONTEXT (frame
)->reg
[regnum
].loc
.reg
);
1868 case REG_CTX_SAVED_ADDR
:
1869 target_read_memory (UNWIND_CONTEXT (frame
)->reg
[regnum
].loc
.addr
,
1870 raw_buffer
, REGISTER_RAW_SIZE (regnum
));
1872 *lval
= lval_memory
;
1874 *addrp
= UNWIND_CONTEXT (frame
)->reg
[regnum
].loc
.addr
;
1877 memcpy (raw_buffer
, &UNWIND_CONTEXT (frame
)->reg
[regnum
].loc
.addr
,
1878 REGISTER_RAW_SIZE (regnum
));
1881 if (optimized
!= NULL
)
1885 internal_error (__FILE__
, __LINE__
,
1886 "cfi_get_saved_register: unknown register rule");
1891 /* Return the register that the function uses for a frame pointer,
1892 plus any necessary offset to be applied to the register before
1893 any frame pointer offsets. */
1895 cfi_virtual_frame_pointer (CORE_ADDR pc
, int *frame_reg
,
1896 LONGEST
* frame_offset
)
1898 struct context
*context
;
1899 struct frame_state
*fs
;
1901 unwind_tmp_obstack_init ();
1903 context
= context_alloc ();
1904 fs
= frame_state_alloc ();
1906 context
->ra
= read_pc () + 1;
1908 frame_state_for (context
, fs
);
1910 if (fs
->cfa_how
== CFA_REG_OFFSET
)
1912 *frame_reg
= fs
->cfa_reg
;
1913 *frame_offset
= fs
->cfa_offset
;
1916 error ("dwarf cfi error: CFA is not defined as CFA_REG_OFFSET");
1918 unwind_tmp_obstack_free ();