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
;
173 extern asection
*dwarf_frame_section
;
174 extern asection
*dwarf_eh_frame_section
;
178 extern char *dwarf2_read_section (struct objfile
*objfile
, file_ptr offset
,
179 unsigned int size
, asection
* sectp
);
181 static struct fde_unit
*fde_unit_alloc (void);
182 static struct cie_unit
*cie_unit_alloc (void);
183 static void fde_chunks_need_space ();
185 static void unwind_tmp_obstack_init ();
186 static void unwind_tmp_obstack_free ();
188 static unsigned int read_1u (bfd
*abfd
, char **p
);
189 static int read_1s (bfd
*abfd
, char **p
);
190 static unsigned int read_2u (bfd
*abfd
, char **p
);
191 static int read_2s (bfd
*abfd
, char **p
);
192 static unsigned int read_4u (bfd
*abfd
, char **p
);
193 static int read_4s (bfd
*abfd
, char **p
);
194 static ULONGEST
read_8u (bfd
*abfd
, char **p
);
195 static LONGEST
read_8s (bfd
*abfd
, char **p
);
197 static ULONGEST
read_uleb128 (bfd
*abfd
, char **p
);
198 static LONGEST
read_sleb128 (bfd
*abfd
, char **p
);
199 static CORE_ADDR
read_pointer (bfd
*abfd
, char **p
);
200 static CORE_ADDR
read_encoded_pointer (bfd
*abfd
, char **p
,
201 unsigned char encoding
);
202 static enum ptr_encoding
pointer_encoding (unsigned char encoding
);
204 static LONGEST
read_initial_length (bfd
*abfd
, char *buf
, int *bytes_read
);
205 static ULONGEST
read_length (bfd
*abfd
, char *buf
, int *bytes_read
,
208 static int is_cie (ULONGEST cie_id
, int dwarf64
);
209 static int compare_fde_unit (const void *a
, const void *b
);
210 void dwarf2_build_frame_info (struct objfile
*objfile
);
212 static void execute_cfa_program (struct objfile
*objfile
, char *insn_ptr
,
213 char *insn_end
, struct context
*context
,
214 struct frame_state
*fs
);
215 static struct fde_unit
*get_fde_for_addr (CORE_ADDR pc
);
216 static void frame_state_for (struct context
*context
, struct frame_state
*fs
);
217 static void get_reg (char *reg
, struct context
*context
, int regnum
);
218 static CORE_ADDR
execute_stack_op (struct objfile
*objfile
,
219 char *op_ptr
, char *op_end
,
220 struct context
*context
,
222 static void update_context (struct context
*context
, struct frame_state
*fs
,
226 /* Memory allocation functions. */
227 static struct fde_unit
*
228 fde_unit_alloc (void)
230 struct fde_unit
*fde
;
232 fde
= (struct fde_unit
*) xmalloc (sizeof (struct fde_unit
));
233 memset (fde
, 0, sizeof (struct fde_unit
));
237 static struct cie_unit
*
238 cie_unit_alloc (void)
240 struct cie_unit
*cie
;
242 cie
= (struct cie_unit
*) xmalloc (sizeof (struct cie_unit
));
243 memset (cie
, 0, sizeof (struct cie_unit
));
248 fde_chunks_need_space (void)
250 if (fde_chunks
.elems
< fde_chunks
.array_size
)
252 fde_chunks
.array_size
=
253 fde_chunks
.array_size
? 2 * fde_chunks
.array_size
: 1024;
255 xrealloc (fde_chunks
.array
,
256 sizeof (struct fde_unit
) * fde_chunks
.array_size
);
259 /* Alocate a new `struct context' on temporary obstack. */
263 struct context
*context
;
265 int regs_size
= sizeof (struct context_reg
) * NUM_REGS
;
267 context
= (struct context
*) obstack_alloc (&unwind_tmp_obstack
,
268 sizeof (struct context
));
269 memset (context
, 0, sizeof (struct context
));
270 context
->reg
= (struct context_reg
*) obstack_alloc (&unwind_tmp_obstack
,
272 memset (context
->reg
, 0, regs_size
);
276 /* Alocate a new `struct frame_state' on temporary obstack. */
278 frame_state_alloc (void)
280 struct frame_state
*fs
;
282 int regs_size
= sizeof (struct frame_state_reg
) * NUM_REGS
;
284 fs
= (struct frame_state
*) obstack_alloc (&unwind_tmp_obstack
,
285 sizeof (struct frame_state
));
286 memset (fs
, 0, sizeof (struct frame_state
));
288 (struct frame_state_reg
*) obstack_alloc (&unwind_tmp_obstack
, regs_size
);
289 memset (fs
->regs
.reg
, 0, regs_size
);
294 unwind_tmp_obstack_init (void)
296 obstack_init (&unwind_tmp_obstack
);
300 unwind_tmp_obstack_free (void)
302 obstack_free (&unwind_tmp_obstack
, NULL
);
303 unwind_tmp_obstack_init ();
307 context_cpy (struct context
*dst
, struct context
*src
)
309 int regs_size
= sizeof (struct context_reg
) * NUM_REGS
;
310 struct context_reg
*dreg
;
312 /* Since `struct context' contains a pointer to an array with
313 register values, make sure we end up with a copy of that array,
314 and not with a copy of the pointer to that array. */
318 memcpy (dst
->reg
, src
->reg
, regs_size
);
322 read_1u (bfd
*abfd
, char **p
)
326 ret
= bfd_get_8 (abfd
, (bfd_byte
*) * p
);
332 read_1s (bfd
*abfd
, char **p
)
336 ret
= bfd_get_signed_8 (abfd
, (bfd_byte
*) * p
);
342 read_2u (bfd
*abfd
, char **p
)
346 ret
= bfd_get_16 (abfd
, (bfd_byte
*) * p
);
352 read_2s (bfd
*abfd
, char **p
)
356 ret
= bfd_get_signed_16 (abfd
, (bfd_byte
*) * p
);
362 read_4u (bfd
*abfd
, char **p
)
366 ret
= bfd_get_32 (abfd
, (bfd_byte
*) * p
);
372 read_4s (bfd
*abfd
, char **p
)
376 ret
= bfd_get_signed_32 (abfd
, (bfd_byte
*) * p
);
382 read_8u (bfd
*abfd
, char **p
)
386 ret
= bfd_get_64 (abfd
, (bfd_byte
*) * p
);
392 read_8s (bfd
*abfd
, char **p
)
396 ret
= bfd_get_signed_64 (abfd
, (bfd_byte
*) * p
);
402 read_uleb128 (bfd
*abfd
, char **p
)
413 byte
= bfd_get_8 (abfd
, (bfd_byte
*) * p
);
415 ret
|= ((unsigned long) (byte
& 127) << shift
);
416 if ((byte
& 128) == 0)
426 read_sleb128 (bfd
*abfd
, char **p
)
429 int i
, shift
, size
, num_read
;
439 byte
= bfd_get_8 (abfd
, (bfd_byte
*) * p
);
441 ret
|= ((long) (byte
& 127) << shift
);
443 if ((byte
& 128) == 0)
448 if ((shift
< size
) && (byte
& 0x40))
450 ret
|= -(1 << shift
);
456 read_pointer (bfd
*abfd
, char **p
)
458 switch (TARGET_ADDR_BIT
/ TARGET_CHAR_BIT
)
461 return read_4u (abfd
, p
);
463 return read_8u (abfd
, p
);
465 error ("dwarf cfi error: unsupported target address length.");
469 /* Read the appropriate amount of data from *P and return the
470 resulting value based on ENCODING, which the calling function must
473 read_encoded_pointer (bfd
*abfd
, char **p
, unsigned char encoding
)
477 switch (encoding
& 0x0f)
479 case DW_EH_PE_absptr
:
480 ret
= read_pointer (abfd
, p
);
483 case DW_EH_PE_uleb128
:
484 ret
= read_uleb128 (abfd
, p
);
486 case DW_EH_PE_sleb128
:
487 ret
= read_sleb128 (abfd
, p
);
490 case DW_EH_PE_udata2
:
491 ret
= read_2u (abfd
, p
);
493 case DW_EH_PE_udata4
:
494 ret
= read_4u (abfd
, p
);
496 case DW_EH_PE_udata8
:
497 ret
= read_8u (abfd
, p
);
500 case DW_EH_PE_sdata2
:
501 ret
= read_2s (abfd
, p
);
503 case DW_EH_PE_sdata4
:
504 ret
= read_4s (abfd
, p
);
506 case DW_EH_PE_sdata8
:
507 ret
= read_8s (abfd
, p
);
511 internal_error (__FILE__
, __LINE__
,
512 "read_encoded_pointer: unknown pointer encoding");
518 /* The variable 'encoding' carries three different flags:
519 - encoding & 0x0f : size of the address (handled in read_encoded_pointer())
520 - encoding & 0x70 : type (absolute, relative, ...)
521 - encoding & 0x80 : indirect flag (DW_EH_PE_indirect == 0x80). */
523 pointer_encoding (unsigned char encoding
)
527 if (encoding
& DW_EH_PE_indirect
)
528 warning ("CFI: Unsupported pointer encoding: DW_EH_PE_indirect");
530 switch (encoding
& 0x70)
532 case DW_EH_PE_absptr
:
534 case DW_EH_PE_textrel
:
535 case DW_EH_PE_datarel
:
536 case DW_EH_PE_funcrel
:
537 ret
= encoding
& 0x70;
540 internal_error (__FILE__
, __LINE__
, "CFI: unknown pointer encoding");
546 read_initial_length (bfd
*abfd
, char *buf
, int *bytes_read
)
550 ret
= bfd_get_32 (abfd
, (bfd_byte
*) buf
);
552 if (ret
== 0xffffffff)
554 ret
= bfd_get_64 (abfd
, (bfd_byte
*) buf
+ 4);
566 read_length (bfd
*abfd
, char *buf
, int *bytes_read
, int dwarf64
)
571 return read_8u (abfd
, &buf
);
576 return read_4u (abfd
, &buf
);
581 execute_cfa_program (struct objfile
*objfile
, char *insn_ptr
, char *insn_end
,
582 struct context
*context
, struct frame_state
*fs
)
584 struct frame_state_regs
*unused_rs
= NULL
;
586 /* Don't allow remember/restore between CIE and FDE programs. */
587 fs
->regs
.prev
= NULL
;
589 while (insn_ptr
< insn_end
&& fs
->pc
< context
->ra
)
591 unsigned char insn
= *insn_ptr
++;
592 ULONGEST reg
, uoffset
;
595 if (insn
& DW_CFA_advance_loc
)
596 fs
->pc
+= (insn
& 0x3f) * fs
->code_align
;
597 else if (insn
& DW_CFA_offset
)
600 uoffset
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
601 offset
= (long) uoffset
*fs
->data_align
;
602 fs
->regs
.reg
[reg
].how
= REG_SAVED_OFFSET
;
603 fs
->regs
.reg
[reg
].loc
.offset
= offset
;
605 else if (insn
& DW_CFA_restore
)
608 fs
->regs
.reg
[reg
].how
= REG_UNSAVED
;
614 fs
->pc
= read_encoded_pointer (objfile
->obfd
, &insn_ptr
,
617 if (pointer_encoding (fs
->addr_encoding
) != PE_absptr
)
618 warning ("CFI: DW_CFA_set_loc uses relative addressing");
622 case DW_CFA_advance_loc1
:
623 fs
->pc
+= read_1u (objfile
->obfd
, &insn_ptr
);
625 case DW_CFA_advance_loc2
:
626 fs
->pc
+= read_2u (objfile
->obfd
, &insn_ptr
);
628 case DW_CFA_advance_loc4
:
629 fs
->pc
+= read_4u (objfile
->obfd
, &insn_ptr
);
632 case DW_CFA_offset_extended
:
633 reg
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
634 uoffset
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
635 offset
= (long) uoffset
*fs
->data_align
;
636 fs
->regs
.reg
[reg
].how
= REG_SAVED_OFFSET
;
637 fs
->regs
.reg
[reg
].loc
.offset
= offset
;
640 case DW_CFA_restore_extended
:
641 reg
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
642 fs
->regs
.reg
[reg
].how
= REG_UNSAVED
;
645 case DW_CFA_undefined
:
646 case DW_CFA_same_value
:
650 case DW_CFA_register
:
653 reg
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
654 reg2
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
655 fs
->regs
.reg
[reg
].how
= REG_SAVED_REG
;
656 fs
->regs
.reg
[reg
].loc
.reg
= reg2
;
660 case DW_CFA_remember_state
:
662 struct frame_state_regs
*new_rs
;
666 unused_rs
= unused_rs
->prev
;
669 new_rs
= xmalloc (sizeof (struct frame_state_regs
));
672 fs
->regs
.prev
= new_rs
;
676 case DW_CFA_restore_state
:
678 struct frame_state_regs
*old_rs
= fs
->regs
.prev
;
680 old_rs
->prev
= unused_rs
;
686 reg
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
687 uoffset
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
689 fs
->cfa_offset
= uoffset
;
690 fs
->cfa_how
= CFA_REG_OFFSET
;
693 case DW_CFA_def_cfa_register
:
694 reg
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
696 fs
->cfa_how
= CFA_REG_OFFSET
;
699 case DW_CFA_def_cfa_offset
:
700 uoffset
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
701 fs
->cfa_offset
= uoffset
;
704 case DW_CFA_def_cfa_expression
:
705 uoffset
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
706 fs
->cfa_exp
= insn_ptr
;
707 fs
->cfa_how
= CFA_EXP
;
711 case DW_CFA_expression
:
712 reg
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
713 uoffset
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
714 fs
->regs
.reg
[reg
].how
= REG_SAVED_EXP
;
715 fs
->regs
.reg
[reg
].loc
.exp
= insn_ptr
;
719 /* From the 2.1 draft. */
720 case DW_CFA_offset_extended_sf
:
721 reg
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
722 offset
= read_sleb128 (objfile
->obfd
, &insn_ptr
);
723 offset
*= fs
->data_align
;
724 fs
->regs
.reg
[reg
].how
= REG_SAVED_OFFSET
;
725 fs
->regs
.reg
[reg
].loc
.offset
= offset
;
728 case DW_CFA_def_cfa_sf
:
729 reg
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
730 offset
= read_sleb128 (objfile
->obfd
, &insn_ptr
);
731 fs
->cfa_offset
= offset
;
733 fs
->cfa_how
= CFA_REG_OFFSET
;
736 case DW_CFA_def_cfa_offset_sf
:
737 uoffset
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
738 fs
->cfa_offset
= uoffset
;
739 /* cfa_how deliberately not set. */
742 case DW_CFA_GNU_window_save
:
743 /* ??? Hardcoded for SPARC register window configuration. */
744 for (reg
= 16; reg
< 32; ++reg
)
746 fs
->regs
.reg
[reg
].how
= REG_SAVED_OFFSET
;
747 fs
->regs
.reg
[reg
].loc
.offset
= (reg
- 16) * sizeof (void *);
751 case DW_CFA_GNU_args_size
:
752 uoffset
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
753 context
->args_size
= uoffset
;
756 case DW_CFA_GNU_negative_offset_extended
:
757 /* Obsoleted by DW_CFA_offset_extended_sf, but used by
758 older PowerPC code. */
759 reg
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
760 uoffset
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
761 offset
= (long) uoffset
*fs
->data_align
;
762 fs
->regs
.reg
[reg
].how
= REG_SAVED_OFFSET
;
763 fs
->regs
.reg
[reg
].loc
.offset
= -offset
;
767 error ("dwarf cfi error: unknown cfa instruction %d.", insn
);
772 static struct fde_unit
*
773 get_fde_for_addr (CORE_ADDR pc
)
776 struct fde_unit
*fde
= NULL
;
778 hi
= fde_chunks
.elems
;
782 size_t i
= (lo
+ hi
) / 2;
783 fde
= fde_chunks
.array
[i
];
784 if (pc
< fde
->initial_location
)
786 else if (pc
>= fde
->initial_location
+ fde
->address_range
)
795 frame_state_for (struct context
*context
, struct frame_state
*fs
)
797 struct fde_unit
*fde
;
798 struct cie_unit
*cie
;
800 context
->args_size
= 0;
803 fde
= get_fde_for_addr (context
->ra
- 1);
808 fs
->pc
= fde
->initial_location
;
810 gdb_assert (fde
->cie_ptr
!= NULL
);
814 fs
->code_align
= cie
->code_align
;
815 fs
->data_align
= cie
->data_align
;
816 fs
->retaddr_column
= cie
->ra
;
817 fs
->addr_encoding
= cie
->addr_encoding
;
818 fs
->objfile
= cie
->objfile
;
820 execute_cfa_program (cie
->objfile
, cie
->data
,
821 cie
->data
+ cie
->data_length
, context
, fs
);
822 execute_cfa_program (cie
->objfile
, fde
->data
,
823 fde
->data
+ fde
->data_length
, context
, fs
);
827 get_reg (char *reg
, struct context
*context
, int regnum
)
829 switch (context
->reg
[regnum
].how
)
831 case REG_CTX_UNSAVED
:
832 deprecated_read_register_gen (regnum
, reg
);
834 case REG_CTX_SAVED_OFFSET
:
835 target_read_memory (context
->cfa
+ context
->reg
[regnum
].loc
.offset
,
836 reg
, REGISTER_RAW_SIZE (regnum
));
838 case REG_CTX_SAVED_REG
:
839 deprecated_read_register_gen (context
->reg
[regnum
].loc
.reg
, reg
);
841 case REG_CTX_SAVED_ADDR
:
842 target_read_memory (context
->reg
[regnum
].loc
.addr
,
843 reg
, REGISTER_RAW_SIZE (regnum
));
846 memcpy (reg
, &context
->reg
[regnum
].loc
.addr
,
847 REGISTER_RAW_SIZE (regnum
));
850 internal_error (__FILE__
, __LINE__
, "get_reg: unknown register rule");
854 /* Decode a DW_OP stack program. Return the top of stack. Push INITIAL
855 onto the stack to start. */
857 execute_stack_op (struct objfile
*objfile
,
858 char *op_ptr
, char *op_end
, struct context
*context
,
861 CORE_ADDR stack
[64]; /* ??? Assume this is enough. */
867 while (op_ptr
< op_end
)
869 enum dwarf_location_atom op
= *op_ptr
++;
908 result
= op
- DW_OP_lit0
;
912 result
= read_pointer (objfile
->obfd
, &op_ptr
);
916 result
= read_1u (objfile
->obfd
, &op_ptr
);
919 result
= read_1s (objfile
->obfd
, &op_ptr
);
922 result
= read_2u (objfile
->obfd
, &op_ptr
);
925 result
= read_2s (objfile
->obfd
, &op_ptr
);
928 result
= read_4u (objfile
->obfd
, &op_ptr
);
931 result
= read_4s (objfile
->obfd
, &op_ptr
);
934 result
= read_8u (objfile
->obfd
, &op_ptr
);
937 result
= read_8s (objfile
->obfd
, &op_ptr
);
940 result
= read_uleb128 (objfile
->obfd
, &op_ptr
);
943 result
= read_sleb128 (objfile
->obfd
, &op_ptr
);
978 get_reg ((char *) &result
, context
, op
- DW_OP_reg0
);
981 reg
= read_uleb128 (objfile
->obfd
, &op_ptr
);
982 get_reg ((char *) &result
, context
, reg
);
1017 offset
= read_sleb128 (objfile
->obfd
, &op_ptr
);
1018 get_reg ((char *) &result
, context
, op
- DW_OP_breg0
);
1022 reg
= read_uleb128 (objfile
->obfd
, &op_ptr
);
1023 offset
= read_sleb128 (objfile
->obfd
, &op_ptr
);
1024 get_reg ((char *) &result
, context
, reg
);
1030 internal_error (__FILE__
, __LINE__
, "execute_stack_op error");
1031 result
= stack
[stack_elt
- 1];
1035 if (--stack_elt
< 0)
1036 internal_error (__FILE__
, __LINE__
, "execute_stack_op error");
1041 if (offset
>= stack_elt
- 1)
1042 internal_error (__FILE__
, __LINE__
, "execute_stack_op error");
1043 result
= stack
[stack_elt
- 1 - offset
];
1048 internal_error (__FILE__
, __LINE__
, "execute_stack_op error");
1049 result
= stack
[stack_elt
- 2];
1054 CORE_ADDR t1
, t2
, t3
;
1057 internal_error (__FILE__
, __LINE__
, "execute_stack_op error");
1058 t1
= stack
[stack_elt
- 1];
1059 t2
= stack
[stack_elt
- 2];
1060 t3
= stack
[stack_elt
- 3];
1061 stack
[stack_elt
- 1] = t2
;
1062 stack
[stack_elt
- 2] = t3
;
1063 stack
[stack_elt
- 3] = t1
;
1068 case DW_OP_deref_size
:
1072 case DW_OP_plus_uconst
:
1073 /* Unary operations. */
1074 if (--stack_elt
< 0)
1075 internal_error (__FILE__
, __LINE__
, "execute_stack_op error");
1076 result
= stack
[stack_elt
];
1082 int len
= TARGET_ADDR_BIT
/ TARGET_CHAR_BIT
;
1083 if (len
!= 4 && len
!= 8)
1084 internal_error (__FILE__
, __LINE__
,
1085 "execute_stack_op error");
1086 result
= read_memory_unsigned_integer (result
, len
);
1090 case DW_OP_deref_size
:
1092 int len
= *op_ptr
++;
1093 if (len
!= 1 && len
!= 2 && len
!= 4 && len
!= 8)
1094 internal_error (__FILE__
, __LINE__
,
1095 "execute_stack_op error");
1096 result
= read_memory_unsigned_integer (result
, len
);
1110 case DW_OP_plus_uconst
:
1111 result
+= read_uleb128 (objfile
->obfd
, &op_ptr
);
1132 /* Binary operations. */
1133 CORE_ADDR first
, second
;
1134 if ((stack_elt
-= 2) < 0)
1135 internal_error (__FILE__
, __LINE__
, "execute_stack_op error");
1136 second
= stack
[stack_elt
];
1137 first
= stack
[stack_elt
+ 1];
1142 result
= second
& first
;
1145 result
= (LONGEST
) second
/ (LONGEST
) first
;
1148 result
= second
- first
;
1151 result
= (LONGEST
) second
% (LONGEST
) first
;
1154 result
= second
* first
;
1157 result
= second
| first
;
1160 result
= second
+ first
;
1163 result
= second
<< first
;
1166 result
= second
>> first
;
1169 result
= (LONGEST
) second
>> first
;
1172 result
= second
^ first
;
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 result
= (LONGEST
) first
!= (LONGEST
) second
;
1193 error ("execute_stack_op: Unknown DW_OP_ value");
1200 offset
= read_2s (objfile
->obfd
, &op_ptr
);
1205 if (--stack_elt
< 0)
1206 internal_error (__FILE__
, __LINE__
, "execute_stack_op error");
1207 offset
= read_2s (objfile
->obfd
, &op_ptr
);
1208 if (stack
[stack_elt
] != 0)
1216 internal_error (__FILE__
, __LINE__
, "execute_stack_op error");
1219 /* Most things push a result value. */
1220 if ((size_t) stack_elt
>= sizeof (stack
) / sizeof (*stack
))
1221 internal_error (__FILE__
, __LINE__
, "execute_stack_op error");
1222 stack
[++stack_elt
] = result
;
1226 /* We were executing this program to get a value. It should be
1228 if (--stack_elt
< 0)
1229 internal_error (__FILE__
, __LINE__
, "execute_stack_op error");
1230 return stack
[stack_elt
];
1234 update_context (struct context
*context
, struct frame_state
*fs
, int chain
)
1236 struct context
*orig_context
;
1240 unwind_tmp_obstack_init ();
1242 orig_context
= context_alloc ();
1243 context_cpy (orig_context
, context
);
1245 /* Compute this frame's CFA. */
1246 switch (fs
->cfa_how
)
1248 case CFA_REG_OFFSET
:
1249 get_reg ((char *) &cfa
, context
, fs
->cfa_reg
);
1250 cfa
+= fs
->cfa_offset
;
1254 /* ??? No way of knowing what register number is the stack pointer
1255 to do the same sort of handling as above. Assume that if the
1256 CFA calculation is so complicated as to require a stack program
1257 that this will not be a problem. */
1259 char *exp
= fs
->cfa_exp
;
1262 len
= read_uleb128 (fs
->objfile
->obfd
, &exp
);
1263 cfa
= (CORE_ADDR
) execute_stack_op (fs
->objfile
, exp
,
1264 exp
+ len
, context
, 0);
1273 orig_context
->cfa
= cfa
;
1275 /* Compute the addresses of all registers saved in this frame. */
1276 for (i
= 0; i
< NUM_REGS
; ++i
)
1277 switch (fs
->regs
.reg
[i
].how
)
1282 context
->reg
[i
].how
= REG_CTX_VALUE
;
1283 context
->reg
[i
].loc
.addr
= cfa
;
1286 context
->reg
[i
].how
= REG_CTX_UNSAVED
;
1288 case REG_SAVED_OFFSET
:
1289 context
->reg
[i
].how
= REG_CTX_SAVED_OFFSET
;
1290 context
->reg
[i
].loc
.offset
= fs
->regs
.reg
[i
].loc
.offset
;
1293 switch (orig_context
->reg
[fs
->regs
.reg
[i
].loc
.reg
].how
)
1295 case REG_CTX_UNSAVED
:
1296 context
->reg
[i
].how
= REG_CTX_UNSAVED
;
1298 case REG_CTX_SAVED_OFFSET
:
1299 context
->reg
[i
].how
= REG_CTX_SAVED_OFFSET
;
1300 context
->reg
[i
].loc
.offset
= orig_context
->cfa
- context
->cfa
+
1301 orig_context
->reg
[fs
->regs
.reg
[i
].loc
.reg
].loc
.offset
;
1303 case REG_CTX_SAVED_REG
:
1304 context
->reg
[i
].how
= REG_CTX_SAVED_REG
;
1305 context
->reg
[i
].loc
.reg
=
1306 orig_context
->reg
[fs
->regs
.reg
[i
].loc
.reg
].loc
.reg
;
1308 case REG_CTX_SAVED_ADDR
:
1309 context
->reg
[i
].how
= REG_CTX_SAVED_ADDR
;
1310 context
->reg
[i
].loc
.addr
=
1311 orig_context
->reg
[fs
->regs
.reg
[i
].loc
.reg
].loc
.addr
;
1314 internal_error (__FILE__
, __LINE__
, "bad switch");
1319 char *exp
= fs
->regs
.reg
[i
].loc
.exp
;
1323 len
= read_uleb128 (fs
->objfile
->obfd
, &exp
);
1324 val
= execute_stack_op (fs
->objfile
, exp
, exp
+ len
,
1326 context
->reg
[i
].how
= REG_CTX_SAVED_ADDR
;
1327 context
->reg
[i
].loc
.addr
= val
;
1331 internal_error (__FILE__
, __LINE__
, "bad switch");
1333 get_reg ((char *) &context
->ra
, context
, fs
->retaddr_column
);
1334 unwind_tmp_obstack_free ();
1338 is_cie (ULONGEST cie_id
, int dwarf64
)
1340 return dwarf64
? (cie_id
== 0xffffffffffffffff) : (cie_id
== 0xffffffff);
1344 compare_fde_unit (const void *a
, const void *b
)
1346 struct fde_unit
**first
, **second
;
1347 first
= (struct fde_unit
**) a
;
1348 second
= (struct fde_unit
**) b
;
1349 if ((*first
)->initial_location
> (*second
)->initial_location
)
1351 else if ((*first
)->initial_location
< (*second
)->initial_location
)
1357 /* Build the cie_chunks and fde_chunks tables from informations
1358 found in .debug_frame and .eh_frame sections. */
1359 /* We can handle both of these sections almost in the same way, however there
1360 are some exceptions:
1361 - CIE ID is -1 in debug_frame, but 0 in eh_frame
1362 - eh_frame may contain some more information that are used only by gcc
1363 (eg. personality pointer, LSDA pointer, ...). Most of them we can ignore.
1364 - In debug_frame FDE's item cie_id contains offset of it's parent CIE.
1365 In eh_frame FDE's item cie_id is a relative pointer to the parent CIE.
1366 Anyway we don't need to bother with this, because we are smart enough
1367 to keep the pointer to the parent CIE of oncomming FDEs in 'last_cie'.
1368 - Although debug_frame items can contain Augmentation as well as
1369 eh_frame ones, I have never seen them non-empty. Thus only in eh_frame
1370 we can encounter for example non-absolute pointers (Aug. 'R').
1373 parse_frame_info (struct objfile
*objfile
, file_ptr frame_offset
,
1374 unsigned int frame_size
, asection
*frame_section
,
1377 bfd
*abfd
= objfile
->obfd
;
1378 asection
*curr_section_ptr
;
1381 char *frame_buffer
= NULL
;
1382 char *curr_section_name
, *aug_data
;
1383 struct cie_unit
*last_cie
= NULL
;
1384 int last_dup_fde
= 0;
1386 CORE_ADDR curr_section_vma
= 0;
1388 unwind_tmp_obstack_init ();
1390 frame_buffer
= dwarf2_read_section (objfile
, frame_offset
, frame_size
,
1393 start
= frame_buffer
;
1394 end
= frame_buffer
+ frame_size
;
1396 curr_section_name
= eh_frame
? ".eh_frame" : ".debug_frame";
1397 curr_section_ptr
= bfd_get_section_by_name (abfd
, curr_section_name
);
1398 if (curr_section_ptr
)
1399 curr_section_vma
= curr_section_ptr
->vma
;
1405 unsigned long length
;
1407 ULONGEST unit_offset
= start
- frame_buffer
;
1408 int bytes_read
, dwarf64
;
1411 length
= read_initial_length (abfd
, start
, &bytes_read
);
1412 start
+= bytes_read
;
1413 dwarf64
= (bytes_read
== 12);
1414 block_end
= start
+ length
;
1422 cie_id
= read_length (abfd
, start
, &bytes_read
, dwarf64
);
1423 start
+= bytes_read
;
1425 if ((eh_frame
&& cie_id
== 0) || is_cie (cie_id
, dwarf64
))
1427 struct cie_unit
*cie
= cie_unit_alloc ();
1430 cie
->objfile
= objfile
;
1431 cie
->next
= cie_chunks
;
1434 cie
->objfile
= objfile
;
1436 cie
->offset
= unit_offset
;
1438 start
++; /* version */
1440 cie
->augmentation
= aug
= start
;
1441 while (*start
++); /* Skips last NULL as well */
1443 cie
->code_align
= read_uleb128 (abfd
, &start
);
1444 cie
->data_align
= read_sleb128 (abfd
, &start
);
1445 cie
->ra
= read_1u (abfd
, &start
);
1448 z Indicates that a uleb128 is present to size the
1449 augmentation section.
1450 L Indicates the encoding (and thus presence) of
1451 an LSDA pointer in the FDE augmentation.
1452 R Indicates a non-default pointer encoding for
1454 P Indicates the presence of an encoding + language
1455 personality routine in the CIE augmentation.
1457 [This info comes from GCC's dwarf2out.c]
1461 aug_len
= read_uleb128 (abfd
, &start
);
1468 cie
->data_length
= block_end
- cie
->data
;
1470 while (*aug
!= '\0')
1472 if (aug
[0] == 'e' && aug
[1] == 'h')
1474 aug_data
+= sizeof (void *);
1477 else if (aug
[0] == 'R')
1478 cie
->addr_encoding
= *aug_data
++;
1479 else if (aug
[0] == 'P')
1481 CORE_ADDR pers_addr
;
1484 pers_addr_enc
= *aug_data
++;
1485 /* We don't need pers_addr value and so we
1486 don't care about it's encoding. */
1487 pers_addr
= read_encoded_pointer (abfd
, &aug_data
,
1490 else if (aug
[0] == 'L' && eh_frame
)
1494 /* Perhaps we should save this to CIE for later use?
1495 Do we need it for something in GDB? */
1496 lsda_addr_enc
= *aug_data
++;
1499 warning ("CFI warning: unknown augmentation \"%c\""
1501 "\t%s", aug
[0], curr_section_name
,
1510 struct fde_unit
*fde
;
1511 struct cie_unit
*cie
;
1515 /* We assume that debug_frame is in order
1516 CIE,FDE,CIE,FDE,FDE,... and thus the CIE for this FDE
1517 should be stored in last_cie pointer. If not, we'll
1518 try to find it by the older way. */
1523 warning ("CFI: last_cie == NULL. "
1524 "Perhaps a malformed %s section in '%s'...?\n",
1525 curr_section_name
, objfile
->name
);
1530 if (cie
->objfile
== objfile
)
1534 (unit_offset
+ bytes_read
- cie_id
)))
1536 if (!eh_frame
&& (cie
->offset
== cie_id
))
1543 error ("CFI: can't find CIE pointer");
1546 init_loc
= read_encoded_pointer (abfd
, &start
,
1547 cie
->addr_encoding
);
1549 switch (pointer_encoding (cie
->addr_encoding
))
1554 /* start-frame_buffer gives offset from
1555 the beginning of actual section. */
1556 init_loc
+= curr_section_vma
+ start
- frame_buffer
;
1559 warning ("CFI: Unsupported pointer encoding\n");
1562 /* For relocatable objects we must add an offset telling
1563 where the section is actually mapped in the memory. */
1564 init_loc
+= ANOFFSET (objfile
->section_offsets
,
1565 SECT_OFF_TEXT (objfile
));
1567 /* If we have both .debug_frame and .eh_frame present in
1568 a file, we must eliminate duplicate FDEs. For now we'll
1569 run through all entries in fde_chunks and check it one
1570 by one. Perhaps in the future we can implement a faster
1571 searching algorithm. */
1572 /* eh_frame==2 indicates, that this file has an already
1573 parsed .debug_frame too. When eh_frame==1 it means, that no
1574 .debug_frame is present and thus we don't need to check for
1575 duplicities. eh_frame==0 means, that we parse .debug_frame
1576 and don't need to care about duplicate FDEs, because
1577 .debug_frame is parsed first. */
1579 for (i
= 0; eh_frame
== 2 && i
< fde_chunks
.elems
; i
++)
1581 /* We assume that FDEs in .debug_frame and .eh_frame
1582 have the same order (if they are present, of course).
1583 If we find a duplicate entry for one FDE and save
1584 it's index to last_dup_fde it's very likely, that
1585 we'll find an entry for the following FDE right after
1586 the previous one. Thus in many cases we'll run this
1588 last_dup_fde
= (last_dup_fde
+ i
) % fde_chunks
.elems
;
1589 if (fde_chunks
.array
[last_dup_fde
]->initial_location
1597 /* Allocate a new entry only if this FDE isn't a duplicate of
1598 something we have already seen. */
1601 fde_chunks_need_space ();
1602 fde
= fde_unit_alloc ();
1604 fde_chunks
.array
[fde_chunks
.elems
++] = fde
;
1606 fde
->initial_location
= init_loc
;
1607 fde
->address_range
= read_encoded_pointer (abfd
, &start
,
1613 /* Here we intentionally ignore augmentation data
1614 from FDE, because we don't need them. */
1615 if (cie
->augmentation
[0] == 'z')
1616 start
+= read_uleb128 (abfd
, &start
);
1619 fde
->data_length
= block_end
- start
;
1624 qsort (fde_chunks
.array
, fde_chunks
.elems
,
1625 sizeof (struct fde_unit
*), compare_fde_unit
);
1629 /* We must parse both .debug_frame section and .eh_frame because
1630 * not all frames must be present in both of these sections. */
1632 dwarf2_build_frame_info (struct objfile
*objfile
)
1634 int after_debug_frame
= 0;
1636 /* If we have .debug_frame then the parser is called with
1637 eh_frame==0 for .debug_frame and eh_frame==2 for .eh_frame,
1638 otherwise it's only called once for .eh_frame with argument
1641 if (dwarf_frame_offset
)
1643 parse_frame_info (objfile
, dwarf_frame_offset
,
1644 dwarf_frame_size
, dwarf_frame_section
,
1645 0 /* = debug_frame */ );
1646 after_debug_frame
= 1;
1649 if (dwarf_eh_frame_offset
)
1650 parse_frame_info (objfile
, dwarf_eh_frame_offset
, dwarf_eh_frame_size
,
1651 dwarf_eh_frame_section
,
1652 1 /* = eh_frame */ + after_debug_frame
);
1655 /* Return the frame address. */
1659 struct context
*context
;
1660 struct frame_state
*fs
;
1663 unwind_tmp_obstack_init ();
1665 context
= context_alloc ();
1666 fs
= frame_state_alloc ();
1668 context
->ra
= read_pc () + 1;
1670 frame_state_for (context
, fs
);
1671 update_context (context
, fs
, 0);
1675 unwind_tmp_obstack_free ();
1680 /* Store the frame address. This function is not used. */
1683 cfi_write_fp (CORE_ADDR val
)
1685 struct context
*context
;
1686 struct frame_state
*fs
;
1688 unwind_tmp_obstack_init ();
1690 context
= context_alloc ();
1691 fs
= frame_state_alloc ();
1693 context
->ra
= read_pc () + 1;
1695 frame_state_for (context
, fs
);
1697 if (fs
->cfa_how
== CFA_REG_OFFSET
)
1699 val
-= fs
->cfa_offset
;
1700 deprecated_write_register_gen (fs
->cfa_reg
, (char *) &val
);
1703 warning ("Can't write fp.");
1705 unwind_tmp_obstack_free ();
1708 /* Restore the machine to the state it had before the current frame
1711 cfi_pop_frame (struct frame_info
*fi
)
1713 char *regbuf
= alloca (MAX_REGISTER_RAW_SIZE
);
1716 for (regnum
= 0; regnum
< NUM_REGS
; regnum
++)
1718 get_reg (regbuf
, UNWIND_CONTEXT (fi
), regnum
);
1719 deprecated_write_register_bytes (REGISTER_BYTE (regnum
), regbuf
,
1720 REGISTER_RAW_SIZE (regnum
));
1722 write_register (PC_REGNUM
, UNWIND_CONTEXT (fi
)->ra
);
1724 flush_cached_frames ();
1727 /* Determine the address of the calling function's frame. */
1729 cfi_frame_chain (struct frame_info
*fi
)
1731 struct context
*context
;
1732 struct frame_state
*fs
;
1735 unwind_tmp_obstack_init ();
1737 context
= context_alloc ();
1738 fs
= frame_state_alloc ();
1739 context_cpy (context
, UNWIND_CONTEXT (fi
));
1741 /* outermost frame */
1742 if (context
->ra
== 0)
1744 unwind_tmp_obstack_free ();
1748 frame_state_for (context
, fs
);
1749 update_context (context
, fs
, 1);
1752 unwind_tmp_obstack_free ();
1757 /* Sets the pc of the frame. */
1759 cfi_init_frame_pc (int fromleaf
, struct frame_info
*fi
)
1761 if (get_next_frame (fi
))
1764 /* FIXME: cagney/2002-12-04: This is straight wrong. It's
1765 assuming that the PC is CORE_ADDR (a host quantity) in size. */
1766 get_reg ((void *)&pc
, UNWIND_CONTEXT (get_next_frame (fi
)), PC_REGNUM
);
1773 /* Initialize unwind context informations of the frame. */
1775 cfi_init_extra_frame_info (int fromleaf
, struct frame_info
*fi
)
1777 struct frame_state
*fs
;
1779 unwind_tmp_obstack_init ();
1781 fs
= frame_state_alloc ();
1782 deprecated_set_frame_context (fi
, frame_obstack_zalloc (sizeof (struct context
)));
1783 UNWIND_CONTEXT (fi
)->reg
=
1784 frame_obstack_zalloc (sizeof (struct context_reg
) * NUM_REGS
);
1785 memset (UNWIND_CONTEXT (fi
)->reg
, 0,
1786 sizeof (struct context_reg
) * NUM_REGS
);
1788 if (get_next_frame (fi
))
1790 context_cpy (UNWIND_CONTEXT (fi
), UNWIND_CONTEXT (get_next_frame (fi
)));
1791 frame_state_for (UNWIND_CONTEXT (fi
), fs
);
1792 update_context (UNWIND_CONTEXT (fi
), fs
, 1);
1796 UNWIND_CONTEXT (fi
)->ra
= get_frame_pc (fi
) + 1;
1797 frame_state_for (UNWIND_CONTEXT (fi
), fs
);
1798 update_context (UNWIND_CONTEXT (fi
), fs
, 0);
1801 unwind_tmp_obstack_free ();
1804 /* Obtain return address of the frame. */
1806 cfi_get_ra (struct frame_info
*fi
)
1808 return UNWIND_CONTEXT (fi
)->ra
;
1811 /* Find register number REGNUM relative to FRAME and put its
1812 (raw) contents in *RAW_BUFFER. Set *OPTIMIZED if the variable
1813 was optimized out (and thus can't be fetched). If the variable
1814 was fetched from memory, set *ADDRP to where it was fetched from,
1815 otherwise it was fetched from a register.
1817 The argument RAW_BUFFER must point to aligned memory. */
1819 cfi_get_saved_register (char *raw_buffer
,
1822 struct frame_info
*frame
,
1823 int regnum
, enum lval_type
*lval
)
1825 if (!target_has_registers
)
1826 error ("No registers.");
1828 /* Normal systems don't optimize out things with register numbers. */
1829 if (optimized
!= NULL
)
1832 if (addrp
) /* default assumption: not found in memory */
1835 if (!get_next_frame (frame
))
1837 deprecated_read_register_gen (regnum
, raw_buffer
);
1839 *lval
= lval_register
;
1841 *addrp
= REGISTER_BYTE (regnum
);
1845 frame
= get_next_frame (frame
);
1846 switch (UNWIND_CONTEXT (frame
)->reg
[regnum
].how
)
1848 case REG_CTX_UNSAVED
:
1849 deprecated_read_register_gen (regnum
, raw_buffer
);
1852 if (optimized
!= NULL
)
1855 case REG_CTX_SAVED_OFFSET
:
1856 target_read_memory (UNWIND_CONTEXT (frame
)->cfa
+
1857 UNWIND_CONTEXT (frame
)->reg
[regnum
].loc
.offset
,
1858 raw_buffer
, REGISTER_RAW_SIZE (regnum
));
1860 *lval
= lval_memory
;
1863 UNWIND_CONTEXT (frame
)->cfa
+
1864 UNWIND_CONTEXT (frame
)->reg
[regnum
].loc
.offset
;
1866 case REG_CTX_SAVED_REG
:
1867 deprecated_read_register_gen (UNWIND_CONTEXT (frame
)->reg
[regnum
].loc
.reg
,
1870 *lval
= lval_register
;
1873 REGISTER_BYTE (UNWIND_CONTEXT (frame
)->reg
[regnum
].loc
.reg
);
1875 case REG_CTX_SAVED_ADDR
:
1876 target_read_memory (UNWIND_CONTEXT (frame
)->reg
[regnum
].loc
.addr
,
1877 raw_buffer
, REGISTER_RAW_SIZE (regnum
));
1879 *lval
= lval_memory
;
1881 *addrp
= UNWIND_CONTEXT (frame
)->reg
[regnum
].loc
.addr
;
1884 memcpy (raw_buffer
, &UNWIND_CONTEXT (frame
)->reg
[regnum
].loc
.addr
,
1885 REGISTER_RAW_SIZE (regnum
));
1888 if (optimized
!= NULL
)
1892 internal_error (__FILE__
, __LINE__
,
1893 "cfi_get_saved_register: unknown register rule");
1898 /* Return the register that the function uses for a frame pointer,
1899 plus any necessary offset to be applied to the register before
1900 any frame pointer offsets. */
1902 cfi_virtual_frame_pointer (CORE_ADDR pc
, int *frame_reg
,
1903 LONGEST
* frame_offset
)
1905 struct context
*context
;
1906 struct frame_state
*fs
;
1908 unwind_tmp_obstack_init ();
1910 context
= context_alloc ();
1911 fs
= frame_state_alloc ();
1913 context
->ra
= read_pc () + 1;
1915 frame_state_for (context
, fs
);
1917 if (fs
->cfa_how
== CFA_REG_OFFSET
)
1919 *frame_reg
= fs
->cfa_reg
;
1920 *frame_offset
= fs
->cfa_offset
;
1923 error ("dwarf cfi error: CFA is not defined as CFA_REG_OFFSET");
1925 unwind_tmp_obstack_free ();