1 /* Stack unwinding code based on dwarf2 frame info for GDB, the GNU debugger.
2 Copyright 2001, 2002 Free Software Foundation, Inc.
3 Contributed by Jiri Smid, SuSE Labs.
4 Based on code written by Daniel Berlin (dan@dberlin.org).
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
29 #include "elf/dwarf2.h"
32 #include "dwarf2cfi.h"
33 #include "gdb_assert.h"
35 /* Common Information Entry - holds information that is shared among many
39 /* Offset of this unit in .debug_frame or .eh_frame. */
42 /* A null-terminated string that identifies the augmentation to this CIE or
43 to the FDEs that use it. */
46 /* A constant that is factored out of all advance location instructions. */
47 unsigned int code_align
;
49 /* A constant that is factored out of all offset instructions. */
52 /* A constant that indicates which regiter represents the return address
56 /* Indicates how addresses are encoded. */
57 unsigned char addr_encoding
;
59 /* Pointer and length of the cie program. */
61 unsigned int data_length
;
63 struct objfile
*objfile
;
66 struct cie_unit
*next
;
69 /* Frame Description Entry. */
72 /* Address of the first location associated with this entry. */
73 CORE_ADDR initial_location
;
75 /* Length of program section described by this entry. */
76 CORE_ADDR address_range
;
78 /* Pointer to asociated CIE. */
79 struct cie_unit
*cie_ptr
;
81 /* Pointer and length of the cie program. */
83 unsigned int data_length
;
88 struct fde_unit
**array
;
93 struct frame_state_reg
114 /* Each register save state can be described in terms of a CFA slot,
115 another register, or a location expression. */
116 struct frame_state_regs
118 struct frame_state_reg
*reg
;
120 /* Used to implement DW_CFA_remember_state. */
121 struct frame_state_regs
*prev
;
125 /* The CFA can be described in terms of a reg+offset or a
126 location expression. */
129 unsigned char *cfa_exp
;
138 /* The PC described by the current frame state. */
141 /* The information we care about from the CIE/FDE. */
143 unsigned int code_align
;
144 unsigned char retaddr_column
;
145 unsigned char addr_encoding
;
147 struct objfile
*objfile
;
152 PE_absptr
= DW_EH_PE_absptr
,
153 PE_pcrel
= DW_EH_PE_pcrel
,
154 PE_textrel
= DW_EH_PE_textrel
,
155 PE_datarel
= DW_EH_PE_datarel
,
156 PE_funcrel
= DW_EH_PE_funcrel
159 #define UNWIND_CONTEXT(fi) ((struct context *) (fi->context))
162 static struct cie_unit
*cie_chunks
;
163 static struct fde_array fde_chunks
;
164 /* Obstack for allocating temporary storage used during unwind operations. */
165 static struct obstack unwind_tmp_obstack
;
167 extern file_ptr dwarf_frame_offset
;
168 extern unsigned int dwarf_frame_size
;
169 extern file_ptr dwarf_eh_frame_offset
;
170 extern unsigned int dwarf_eh_frame_size
;
173 extern char *dwarf2_read_section (struct objfile
*objfile
, file_ptr offset
,
176 static struct fde_unit
*fde_unit_alloc (void);
177 static struct cie_unit
*cie_unit_alloc (void);
178 static void fde_chunks_need_space ();
180 static void unwind_tmp_obstack_init ();
181 static void unwind_tmp_obstack_free ();
183 static unsigned int read_1u (bfd
* abfd
, char **p
);
184 static int read_1s (bfd
* abfd
, char **p
);
185 static unsigned int read_2u (bfd
* abfd
, char **p
);
186 static int read_2s (bfd
* abfd
, char **p
);
187 static unsigned int read_4u (bfd
* abfd
, char **p
);
188 static int read_4s (bfd
* abfd
, char **p
);
189 static ULONGEST
read_8u (bfd
* abfd
, char **p
);
190 static LONGEST
read_8s (bfd
* abfd
, char **p
);
192 static ULONGEST
read_uleb128 (bfd
* abfd
, char **p
);
193 static LONGEST
read_sleb128 (bfd
* abfd
, char **p
);
194 static CORE_ADDR
read_pointer (bfd
* abfd
, char **p
);
195 static CORE_ADDR
read_encoded_pointer (bfd
* abfd
, char **p
,
196 unsigned char encoding
);
197 static enum ptr_encoding
pointer_encoding (unsigned char encoding
);
199 static LONGEST
read_initial_length (bfd
* abfd
, char *buf
, int *bytes_read
);
200 static ULONGEST
read_length (bfd
* abfd
, char *buf
, int *bytes_read
,
203 static int is_cie (ULONGEST cie_id
, int dwarf64
);
204 static int compare_fde_unit (const void *a
, const void *b
);
205 void dwarf2_build_frame_info (struct objfile
*objfile
);
207 static void execute_cfa_program (struct objfile
*objfile
, char *insn_ptr
,
208 char *insn_end
, struct context
*context
,
209 struct frame_state
*fs
);
210 static struct fde_unit
*get_fde_for_addr (CORE_ADDR pc
);
211 static void frame_state_for (struct context
*context
, struct frame_state
*fs
);
212 static void get_reg (char *reg
, struct context
*context
, int regnum
);
213 static CORE_ADDR
execute_stack_op (struct objfile
*objfile
,
214 char *op_ptr
, char *op_end
,
215 struct context
*context
,
217 static void update_context (struct context
*context
, struct frame_state
*fs
,
221 /* Memory allocation functions. */
222 static struct fde_unit
*
223 fde_unit_alloc (void)
225 struct fde_unit
*fde
;
227 fde
= (struct fde_unit
*) xmalloc (sizeof (struct fde_unit
));
228 memset (fde
, 0, sizeof (struct fde_unit
));
232 static struct cie_unit
*
233 cie_unit_alloc (void)
235 struct cie_unit
*cie
;
237 cie
= (struct cie_unit
*) xmalloc (sizeof (struct cie_unit
));
238 memset (cie
, 0, sizeof (struct cie_unit
));
243 fde_chunks_need_space (void)
245 if (fde_chunks
.elems
< fde_chunks
.array_size
)
247 fde_chunks
.array_size
=
248 fde_chunks
.array_size
? 2 * fde_chunks
.array_size
: 1024;
250 xrealloc (fde_chunks
.array
,
251 sizeof (struct fde_unit
) * fde_chunks
.array_size
);
254 /* Alocate a new `struct context' on temporary obstack. */
258 struct context
*context
;
260 int regs_size
= sizeof (struct context_reg
) * NUM_REGS
;
262 context
= (struct context
*) obstack_alloc (&unwind_tmp_obstack
,
263 sizeof (struct context
));
264 memset (context
, 0, sizeof (struct context
));
265 context
->reg
= (struct context_reg
*) obstack_alloc (&unwind_tmp_obstack
,
267 memset (context
->reg
, 0, regs_size
);
271 /* Alocate a new `struct frame_state' on temporary obstack. */
273 frame_state_alloc (void)
275 struct frame_state
*fs
;
277 int regs_size
= sizeof (struct frame_state_reg
) * NUM_REGS
;
279 fs
= (struct frame_state
*) obstack_alloc (&unwind_tmp_obstack
,
280 sizeof (struct frame_state
));
281 memset (fs
, 0, sizeof (struct frame_state
));
283 (struct frame_state_reg
*) obstack_alloc (&unwind_tmp_obstack
, regs_size
);
284 memset (fs
->regs
.reg
, 0, regs_size
);
289 unwind_tmp_obstack_init (void)
291 obstack_init (&unwind_tmp_obstack
);
295 unwind_tmp_obstack_free (void)
297 obstack_free (&unwind_tmp_obstack
, NULL
);
298 unwind_tmp_obstack_init ();
302 context_cpy (struct context
*dst
, struct context
*src
)
304 int regs_size
= sizeof (struct context_reg
) * NUM_REGS
;
305 struct context_reg
*dreg
;
307 /* Structure dst contains a pointer to an array of
308 * registers of a given frame as well as src does. This
309 * array was already allocated before dst was passed to
310 * context_cpy but the pointer to it was overriden by
311 * '*dst = *src' and the array was lost. This led to the
312 * situation, that we've had a copy of src placed in dst,
313 * but both of them pointed to the same regs array and
314 * thus we've sometimes blindly rewritten it. Now we save
315 * the pointer before copying src to dst, return it back
316 * after that and copy the registers into their new place
317 * finally. --- mludvig@suse.cz */
322 memcpy (dst
->reg
, src
->reg
, regs_size
);
326 read_1u (bfd
* abfd
, char **p
)
330 ret
= bfd_get_8 (abfd
, (bfd_byte
*) * p
);
336 read_1s (bfd
* abfd
, char **p
)
340 ret
= bfd_get_signed_8 (abfd
, (bfd_byte
*) * p
);
346 read_2u (bfd
* abfd
, char **p
)
350 ret
= bfd_get_16 (abfd
, (bfd_byte
*) * p
);
356 read_2s (bfd
* abfd
, char **p
)
360 ret
= bfd_get_signed_16 (abfd
, (bfd_byte
*) * p
);
366 read_4u (bfd
* abfd
, char **p
)
370 ret
= bfd_get_32 (abfd
, (bfd_byte
*) * p
);
376 read_4s (bfd
* abfd
, char **p
)
380 ret
= bfd_get_signed_32 (abfd
, (bfd_byte
*) * p
);
386 read_8u (bfd
* abfd
, char **p
)
390 ret
= bfd_get_64 (abfd
, (bfd_byte
*) * p
);
396 read_8s (bfd
* abfd
, char **p
)
400 ret
= bfd_get_signed_64 (abfd
, (bfd_byte
*) * p
);
406 read_uleb128 (bfd
* abfd
, char **p
)
417 byte
= bfd_get_8 (abfd
, (bfd_byte
*) * p
);
419 ret
|= ((unsigned long) (byte
& 127) << shift
);
420 if ((byte
& 128) == 0)
430 read_sleb128 (bfd
* abfd
, char **p
)
433 int i
, shift
, size
, num_read
;
443 byte
= bfd_get_8 (abfd
, (bfd_byte
*) * p
);
445 ret
|= ((long) (byte
& 127) << shift
);
447 if ((byte
& 128) == 0)
452 if ((shift
< size
) && (byte
& 0x40))
454 ret
|= -(1 << shift
);
460 read_pointer (bfd
* abfd
, char **p
)
462 switch (TARGET_ADDR_BIT
/ TARGET_CHAR_BIT
)
465 return read_4u (abfd
, p
);
467 return read_8u (abfd
, p
);
469 error ("dwarf cfi error: unsupported target address length.");
473 /* This functions only reads appropriate amount of data from *p
474 * and returns the resulting value. Calling function must handle
475 * different encoding possibilities itself! */
477 read_encoded_pointer (bfd
* abfd
, char **p
, unsigned char encoding
)
481 switch (encoding
& 0x0f)
483 case DW_EH_PE_absptr
:
484 ret
= read_pointer (abfd
, p
);
487 case DW_EH_PE_uleb128
:
488 ret
= read_uleb128 (abfd
, p
);
490 case DW_EH_PE_sleb128
:
491 ret
= read_sleb128 (abfd
, p
);
494 case DW_EH_PE_udata2
:
495 ret
= read_2u (abfd
, p
);
497 case DW_EH_PE_udata4
:
498 ret
= read_4u (abfd
, p
);
500 case DW_EH_PE_udata8
:
501 ret
= read_8u (abfd
, p
);
504 case DW_EH_PE_sdata2
:
505 ret
= read_2s (abfd
, p
);
507 case DW_EH_PE_sdata4
:
508 ret
= read_4s (abfd
, p
);
510 case DW_EH_PE_sdata8
:
511 ret
= read_8s (abfd
, p
);
515 internal_error (__FILE__
, __LINE__
,
516 "read_encoded_pointer: unknown pointer encoding");
522 /* Variable 'encoding' carries 3 different flags:
523 * - encoding & 0x0f : size of the address (handled in read_encoded_pointer())
524 * - encoding & 0x70 : type (absolute, relative, ...)
525 * - encoding & 0x80 : indirect flag (DW_EH_PE_indirect == 0x80). */
527 pointer_encoding (unsigned char encoding
)
531 if (encoding
& DW_EH_PE_indirect
)
532 warning ("CFI: Unsupported pointer encoding: DW_EH_PE_indirect");
534 switch (encoding
& 0x70)
536 case DW_EH_PE_absptr
:
538 case DW_EH_PE_textrel
:
539 case DW_EH_PE_datarel
:
540 case DW_EH_PE_funcrel
:
541 ret
= encoding
& 0x70;
544 internal_error (__FILE__
, __LINE__
, "CFI: unknown pointer encoding");
550 read_initial_length (bfd
* abfd
, char *buf
, int *bytes_read
)
554 ret
= bfd_get_32 (abfd
, (bfd_byte
*) buf
);
556 if (ret
== 0xffffffff)
558 ret
= bfd_get_64 (abfd
, (bfd_byte
*) buf
+ 4);
570 read_length (bfd
* abfd
, char *buf
, int *bytes_read
, int dwarf64
)
575 return read_8u (abfd
, &buf
);
580 return read_4u (abfd
, &buf
);
585 execute_cfa_program (struct objfile
*objfile
, char *insn_ptr
, char *insn_end
,
586 struct context
*context
, struct frame_state
*fs
)
588 struct frame_state_regs
*unused_rs
= NULL
;
590 /* Don't allow remember/restore between CIE and FDE programs. */
591 fs
->regs
.prev
= NULL
;
593 while (insn_ptr
< insn_end
&& fs
->pc
< context
->ra
)
595 unsigned char insn
= *insn_ptr
++;
596 ULONGEST reg
, uoffset
;
599 if (insn
& DW_CFA_advance_loc
)
600 fs
->pc
+= (insn
& 0x3f) * fs
->code_align
;
601 else if (insn
& DW_CFA_offset
)
604 uoffset
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
605 offset
= (long) uoffset
*fs
->data_align
;
606 fs
->regs
.reg
[reg
].how
= REG_SAVED_OFFSET
;
607 fs
->regs
.reg
[reg
].loc
.offset
= offset
;
609 else if (insn
& DW_CFA_restore
)
612 fs
->regs
.reg
[reg
].how
= REG_UNSAVED
;
618 fs
->pc
= read_encoded_pointer (objfile
->obfd
, &insn_ptr
,
621 if (pointer_encoding (fs
->addr_encoding
) != PE_absptr
)
622 warning ("CFI: DW_CFA_set_loc uses relative addressing");
626 case DW_CFA_advance_loc1
:
627 fs
->pc
+= read_1u (objfile
->obfd
, &insn_ptr
);
629 case DW_CFA_advance_loc2
:
630 fs
->pc
+= read_2u (objfile
->obfd
, &insn_ptr
);
632 case DW_CFA_advance_loc4
:
633 fs
->pc
+= read_4u (objfile
->obfd
, &insn_ptr
);
636 case DW_CFA_offset_extended
:
637 reg
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
638 uoffset
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
639 offset
= (long) uoffset
*fs
->data_align
;
640 fs
->regs
.reg
[reg
].how
= REG_SAVED_OFFSET
;
641 fs
->regs
.reg
[reg
].loc
.offset
= offset
;
644 case DW_CFA_restore_extended
:
645 reg
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
646 fs
->regs
.reg
[reg
].how
= REG_UNSAVED
;
649 case DW_CFA_undefined
:
650 case DW_CFA_same_value
:
654 case DW_CFA_register
:
657 reg
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
658 reg2
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
659 fs
->regs
.reg
[reg
].how
= REG_SAVED_REG
;
660 fs
->regs
.reg
[reg
].loc
.reg
= reg2
;
664 case DW_CFA_remember_state
:
666 struct frame_state_regs
*new_rs
;
670 unused_rs
= unused_rs
->prev
;
673 new_rs
= xmalloc (sizeof (struct frame_state_regs
));
676 fs
->regs
.prev
= new_rs
;
680 case DW_CFA_restore_state
:
682 struct frame_state_regs
*old_rs
= fs
->regs
.prev
;
684 old_rs
->prev
= unused_rs
;
690 reg
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
691 uoffset
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
693 fs
->cfa_offset
= uoffset
;
694 fs
->cfa_how
= CFA_REG_OFFSET
;
697 case DW_CFA_def_cfa_register
:
698 reg
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
700 fs
->cfa_how
= CFA_REG_OFFSET
;
703 case DW_CFA_def_cfa_offset
:
704 uoffset
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
705 fs
->cfa_offset
= uoffset
;
708 case DW_CFA_def_cfa_expression
:
709 uoffset
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
710 fs
->cfa_exp
= insn_ptr
;
711 fs
->cfa_how
= CFA_EXP
;
715 case DW_CFA_expression
:
716 reg
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
717 uoffset
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
718 fs
->regs
.reg
[reg
].how
= REG_SAVED_EXP
;
719 fs
->regs
.reg
[reg
].loc
.exp
= insn_ptr
;
723 /* From the 2.1 draft. */
724 case DW_CFA_offset_extended_sf
:
725 reg
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
726 offset
= read_sleb128 (objfile
->obfd
, &insn_ptr
);
727 offset
*= fs
->data_align
;
728 fs
->regs
.reg
[reg
].how
= REG_SAVED_OFFSET
;
729 fs
->regs
.reg
[reg
].loc
.offset
= offset
;
732 case DW_CFA_def_cfa_sf
:
733 reg
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
734 offset
= read_sleb128 (objfile
->obfd
, &insn_ptr
);
735 fs
->cfa_offset
= offset
;
737 fs
->cfa_how
= CFA_REG_OFFSET
;
740 case DW_CFA_def_cfa_offset_sf
:
741 uoffset
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
742 fs
->cfa_offset
= uoffset
;
743 /* cfa_how deliberately not set. */
746 case DW_CFA_GNU_window_save
:
747 /* ??? Hardcoded for SPARC register window configuration. */
748 for (reg
= 16; reg
< 32; ++reg
)
750 fs
->regs
.reg
[reg
].how
= REG_SAVED_OFFSET
;
751 fs
->regs
.reg
[reg
].loc
.offset
= (reg
- 16) * sizeof (void *);
755 case DW_CFA_GNU_args_size
:
756 uoffset
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
757 context
->args_size
= uoffset
;
760 case DW_CFA_GNU_negative_offset_extended
:
761 /* Obsoleted by DW_CFA_offset_extended_sf, but used by
762 older PowerPC code. */
763 reg
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
764 uoffset
= read_uleb128 (objfile
->obfd
, &insn_ptr
);
765 offset
= (long) uoffset
*fs
->data_align
;
766 fs
->regs
.reg
[reg
].how
= REG_SAVED_OFFSET
;
767 fs
->regs
.reg
[reg
].loc
.offset
= -offset
;
771 error ("dwarf cfi error: unknown cfa instruction %d.", insn
);
776 static struct fde_unit
*
777 get_fde_for_addr (CORE_ADDR pc
)
780 struct fde_unit
*fde
= NULL
;
782 hi
= fde_chunks
.elems
;
786 size_t i
= (lo
+ hi
) / 2;
787 fde
= fde_chunks
.array
[i
];
788 if (pc
< fde
->initial_location
)
790 else if (pc
>= fde
->initial_location
+ fde
->address_range
)
799 frame_state_for (struct context
*context
, struct frame_state
*fs
)
801 struct fde_unit
*fde
;
802 struct cie_unit
*cie
;
804 context
->args_size
= 0;
807 fde
= get_fde_for_addr (context
->ra
- 1);
812 fs
->pc
= fde
->initial_location
;
814 gdb_assert (fde
->cie_ptr
!= NULL
);
818 fs
->code_align
= cie
->code_align
;
819 fs
->data_align
= cie
->data_align
;
820 fs
->retaddr_column
= cie
->ra
;
821 fs
->addr_encoding
= cie
->addr_encoding
;
822 fs
->objfile
= cie
->objfile
;
824 execute_cfa_program (cie
->objfile
, cie
->data
,
825 cie
->data
+ cie
->data_length
, context
, fs
);
826 execute_cfa_program (cie
->objfile
, fde
->data
,
827 fde
->data
+ fde
->data_length
, context
, fs
);
831 get_reg (char *reg
, struct context
*context
, int regnum
)
833 switch (context
->reg
[regnum
].how
)
835 case REG_CTX_UNSAVED
:
836 read_register_gen (regnum
, reg
);
838 case REG_CTX_SAVED_OFFSET
:
839 target_read_memory (context
->cfa
+ context
->reg
[regnum
].loc
.offset
,
840 reg
, REGISTER_RAW_SIZE (regnum
));
842 case REG_CTX_SAVED_REG
:
843 read_register_gen (context
->reg
[regnum
].loc
.reg
, reg
);
845 case REG_CTX_SAVED_ADDR
:
846 target_read_memory (context
->reg
[regnum
].loc
.addr
,
847 reg
, REGISTER_RAW_SIZE (regnum
));
850 memcpy (reg
, &context
->reg
[regnum
].loc
.addr
,
851 REGISTER_RAW_SIZE (regnum
));
854 internal_error (__FILE__
, __LINE__
, "get_reg: unknown register rule");
858 /* Decode a DW_OP stack program. Return the top of stack. Push INITIAL
859 onto the stack to start. */
861 execute_stack_op (struct objfile
*objfile
,
862 char *op_ptr
, char *op_end
, struct context
*context
,
865 CORE_ADDR stack
[64]; /* ??? Assume this is enough. */
871 while (op_ptr
< op_end
)
873 enum dwarf_location_atom op
= *op_ptr
++;
912 result
= op
- DW_OP_lit0
;
916 result
= read_pointer (objfile
->obfd
, &op_ptr
);
920 result
= read_1u (objfile
->obfd
, &op_ptr
);
923 result
= read_1s (objfile
->obfd
, &op_ptr
);
926 result
= read_2u (objfile
->obfd
, &op_ptr
);
929 result
= read_2s (objfile
->obfd
, &op_ptr
);
932 result
= read_4u (objfile
->obfd
, &op_ptr
);
935 result
= read_4s (objfile
->obfd
, &op_ptr
);
938 result
= read_8u (objfile
->obfd
, &op_ptr
);
941 result
= read_8s (objfile
->obfd
, &op_ptr
);
944 result
= read_uleb128 (objfile
->obfd
, &op_ptr
);
947 result
= read_sleb128 (objfile
->obfd
, &op_ptr
);
982 get_reg ((char *) &result
, context
, op
- DW_OP_reg0
);
985 reg
= read_uleb128 (objfile
->obfd
, &op_ptr
);
986 get_reg ((char *) &result
, context
, reg
);
1021 offset
= read_sleb128 (objfile
->obfd
, &op_ptr
);
1022 get_reg ((char *) &result
, context
, op
- DW_OP_breg0
);
1026 reg
= read_uleb128 (objfile
->obfd
, &op_ptr
);
1027 offset
= read_sleb128 (objfile
->obfd
, &op_ptr
);
1028 get_reg ((char *) &result
, context
, reg
);
1034 internal_error (__FILE__
, __LINE__
, "execute_stack_op error");
1035 result
= stack
[stack_elt
- 1];
1039 if (--stack_elt
< 0)
1040 internal_error (__FILE__
, __LINE__
, "execute_stack_op error");
1045 if (offset
>= stack_elt
- 1)
1046 internal_error (__FILE__
, __LINE__
, "execute_stack_op error");
1047 result
= stack
[stack_elt
- 1 - offset
];
1052 internal_error (__FILE__
, __LINE__
, "execute_stack_op error");
1053 result
= stack
[stack_elt
- 2];
1058 CORE_ADDR t1
, t2
, t3
;
1061 internal_error (__FILE__
, __LINE__
, "execute_stack_op error");
1062 t1
= stack
[stack_elt
- 1];
1063 t2
= stack
[stack_elt
- 2];
1064 t3
= stack
[stack_elt
- 3];
1065 stack
[stack_elt
- 1] = t2
;
1066 stack
[stack_elt
- 2] = t3
;
1067 stack
[stack_elt
- 3] = t1
;
1072 case DW_OP_deref_size
:
1076 case DW_OP_plus_uconst
:
1077 /* Unary operations. */
1078 if (--stack_elt
< 0)
1079 internal_error (__FILE__
, __LINE__
, "execute_stack_op error");
1080 result
= stack
[stack_elt
];
1086 int len
= TARGET_ADDR_BIT
/ TARGET_CHAR_BIT
;
1087 if (len
!= 4 && len
!= 8)
1088 internal_error (__FILE__
, __LINE__
,
1089 "execute_stack_op error");
1090 result
= read_memory_unsigned_integer (result
, len
);
1094 case DW_OP_deref_size
:
1096 int len
= *op_ptr
++;
1097 if (len
!= 1 && len
!= 2 && len
!= 4 && len
!=8)
1098 internal_error (__FILE__
, __LINE__
,
1099 "execute_stack_op error");
1100 result
= read_memory_unsigned_integer (result
, len
);
1114 case DW_OP_plus_uconst
:
1115 result
+= read_uleb128 (objfile
->obfd
, &op_ptr
);
1136 /* Binary operations. */
1137 CORE_ADDR first
, second
;
1138 if ((stack_elt
-= 2) < 0)
1139 internal_error (__FILE__
, __LINE__
, "execute_stack_op error");
1140 second
= stack
[stack_elt
];
1141 first
= stack
[stack_elt
+ 1];
1146 result
= second
& first
;
1149 result
= (LONGEST
) second
/ (LONGEST
) first
;
1152 result
= second
- first
;
1155 result
= (LONGEST
) second
% (LONGEST
) first
;
1158 result
= second
* first
;
1161 result
= second
| first
;
1164 result
= second
+ first
;
1167 result
= second
<< first
;
1170 result
= second
>> first
;
1173 result
= (LONGEST
) second
>> first
;
1176 result
= second
^ first
;
1179 result
= (LONGEST
) first
<= (LONGEST
) second
;
1182 result
= (LONGEST
) first
>= (LONGEST
) second
;
1185 result
= (LONGEST
) first
== (LONGEST
) second
;
1188 result
= (LONGEST
) first
< (LONGEST
) second
;
1191 result
= (LONGEST
) first
> (LONGEST
) second
;
1194 result
= (LONGEST
) first
!= (LONGEST
) second
;
1197 error ("execute_stack_op: Unknown DW_OP_ value");
1204 offset
= read_2s (objfile
->obfd
, &op_ptr
);
1209 if (--stack_elt
< 0)
1210 internal_error (__FILE__
, __LINE__
, "execute_stack_op error");
1211 offset
= read_2s (objfile
->obfd
, &op_ptr
);
1212 if (stack
[stack_elt
] != 0)
1220 internal_error (__FILE__
, __LINE__
, "execute_stack_op error");
1223 /* Most things push a result value. */
1224 if ((size_t) stack_elt
>= sizeof (stack
) / sizeof (*stack
))
1225 internal_error (__FILE__
, __LINE__
, "execute_stack_op error");
1226 stack
[++stack_elt
] = result
;
1230 /* We were executing this program to get a value. It should be
1232 if (--stack_elt
< 0)
1233 internal_error (__FILE__
, __LINE__
, "execute_stack_op error");
1234 return stack
[stack_elt
];
1238 update_context (struct context
*context
, struct frame_state
*fs
, int chain
)
1240 struct context
*orig_context
;
1244 unwind_tmp_obstack_init ();
1246 orig_context
= context_alloc ();
1247 context_cpy (orig_context
, context
);
1249 /* Compute this frame's CFA. */
1250 switch (fs
->cfa_how
)
1252 case CFA_REG_OFFSET
:
1253 get_reg ((char *) &cfa
, context
, fs
->cfa_reg
);
1254 cfa
+= fs
->cfa_offset
;
1258 /* ??? No way of knowing what register number is the stack pointer
1259 to do the same sort of handling as above. Assume that if the
1260 CFA calculation is so complicated as to require a stack program
1261 that this will not be a problem. */
1263 char *exp
= fs
->cfa_exp
;
1266 len
= read_uleb128 (fs
->objfile
->obfd
, &exp
);
1267 cfa
= (CORE_ADDR
) execute_stack_op (fs
->objfile
, exp
,
1268 exp
+ len
, context
, 0);
1277 orig_context
->cfa
= cfa
;
1279 /* Compute the addresses of all registers saved in this frame. */
1280 for (i
= 0; i
< NUM_REGS
; ++i
)
1281 switch (fs
->regs
.reg
[i
].how
)
1286 context
->reg
[i
].how
= REG_CTX_VALUE
;
1287 context
->reg
[i
].loc
.addr
= cfa
;
1290 context
->reg
[i
].how
= REG_CTX_UNSAVED
;
1292 case REG_SAVED_OFFSET
:
1293 context
->reg
[i
].how
= REG_CTX_SAVED_OFFSET
;
1294 context
->reg
[i
].loc
.offset
= fs
->regs
.reg
[i
].loc
.offset
;
1297 switch (orig_context
->reg
[fs
->regs
.reg
[i
].loc
.reg
].how
)
1299 case REG_CTX_UNSAVED
:
1300 context
->reg
[i
].how
= REG_CTX_UNSAVED
;
1302 case REG_CTX_SAVED_OFFSET
:
1303 context
->reg
[i
].how
= REG_CTX_SAVED_OFFSET
;
1304 context
->reg
[i
].loc
.offset
= orig_context
->cfa
- context
->cfa
+
1305 orig_context
->reg
[fs
->regs
.reg
[i
].loc
.reg
].loc
.offset
;
1307 case REG_CTX_SAVED_REG
:
1308 context
->reg
[i
].how
= REG_CTX_SAVED_REG
;
1309 context
->reg
[i
].loc
.reg
=
1310 orig_context
->reg
[fs
->regs
.reg
[i
].loc
.reg
].loc
.reg
;
1312 case REG_CTX_SAVED_ADDR
:
1313 context
->reg
[i
].how
= REG_CTX_SAVED_ADDR
;
1314 context
->reg
[i
].loc
.addr
=
1315 orig_context
->reg
[fs
->regs
.reg
[i
].loc
.reg
].loc
.addr
;
1318 internal_error (__FILE__
, __LINE__
, "bad switch");
1323 char *exp
= fs
->regs
.reg
[i
].loc
.exp
;
1327 len
= read_uleb128 (fs
->objfile
->obfd
, &exp
);
1328 val
= execute_stack_op (fs
->objfile
, exp
, exp
+ len
,
1330 context
->reg
[i
].how
= REG_CTX_SAVED_ADDR
;
1331 context
->reg
[i
].loc
.addr
= val
;
1335 internal_error (__FILE__
, __LINE__
, "bad switch");
1337 get_reg ((char *) &context
->ra
, context
, fs
->retaddr_column
);
1338 unwind_tmp_obstack_free ();
1342 is_cie (ULONGEST cie_id
, int dwarf64
)
1344 return dwarf64
? (cie_id
== 0xffffffffffffffff) : (cie_id
== 0xffffffff);
1348 compare_fde_unit (const void *a
, const void *b
)
1350 struct fde_unit
**first
, **second
;
1351 first
= (struct fde_unit
**) a
;
1352 second
= (struct fde_unit
**) b
;
1353 if ((*first
)->initial_location
> (*second
)->initial_location
)
1355 else if ((*first
)->initial_location
< (*second
)->initial_location
)
1361 /* Build the cie_chunks and fde_chunks tables from informations
1362 found in .debug_frame and .eh_frame sections. */
1363 /* We can handle both of these sections almost in the same way, however there
1364 are some exceptions:
1365 - CIE ID is -1 in debug_frame, but 0 in eh_frame
1366 - eh_frame may contain some more information that are used only by gcc
1367 (eg. personality pointer, LSDA pointer, ...). Most of them we can ignore.
1368 - In debug_frame FDE's item cie_id contains offset of it's parent CIE.
1369 In eh_frame FDE's item cie_id is a relative pointer to the parent CIE.
1370 Anyway we don't need to bother with this, because we are smart enough
1371 to keep the pointer to the parent CIE of oncomming FDEs in 'last_cie'.
1372 - Although debug_frame items can contain Augmentation as well as
1373 eh_frame ones, I have never seen them non-empty. Thus only in eh_frame
1374 we can encounter for example non-absolute pointers (Aug. 'R').
1377 parse_frame_info (struct objfile
*objfile
, file_ptr frame_offset
,
1378 unsigned int frame_size
, int eh_frame
)
1380 bfd
*abfd
= objfile
->obfd
;
1381 asection
*curr_section_ptr
;
1384 char *frame_buffer
= NULL
;
1385 char *curr_section_name
, *aug_data
;
1386 struct cie_unit
*last_cie
= NULL
;
1387 int last_dup_fde
= 0;
1389 CORE_ADDR curr_section_vma
= 0;
1391 unwind_tmp_obstack_init ();
1393 frame_buffer
= dwarf2_read_section (objfile
, frame_offset
, frame_size
);
1395 start
= frame_buffer
;
1396 end
= frame_buffer
+ frame_size
;
1398 curr_section_name
= eh_frame
? ".eh_frame" : ".debug_frame";
1399 curr_section_ptr
= bfd_get_section_by_name (abfd
, curr_section_name
);
1400 if (curr_section_ptr
)
1401 curr_section_vma
= curr_section_ptr
->vma
;
1407 unsigned long length
;
1409 ULONGEST unit_offset
= start
- frame_buffer
;
1410 int bytes_read
, dwarf64
;
1413 length
= read_initial_length (abfd
, start
, &bytes_read
);
1414 start
+= bytes_read
;
1415 dwarf64
= (bytes_read
== 12);
1416 block_end
= start
+ length
;
1424 cie_id
= read_length (abfd
, start
, &bytes_read
, dwarf64
);
1425 start
+= bytes_read
;
1427 if ((eh_frame
&& cie_id
== 0) || is_cie (cie_id
, dwarf64
))
1429 struct cie_unit
*cie
= cie_unit_alloc ();
1432 cie
->objfile
= objfile
;
1433 cie
->next
= cie_chunks
;
1436 cie
->objfile
= objfile
;
1438 cie
->offset
= unit_offset
;
1440 start
++; /* version */
1442 cie
->augmentation
= aug
= start
;
1443 while (*start
++); /* Skips last NULL as well */
1445 cie
->code_align
= read_uleb128 (abfd
, &start
);
1446 cie
->data_align
= read_sleb128 (abfd
, &start
);
1447 cie
->ra
= read_1u (abfd
, &start
);
1450 z Indicates that a uleb128 is present to size the
1451 augmentation section.
1452 L Indicates the encoding (and thus presence) of
1453 an LSDA pointer in the FDE augmentation.
1454 R Indicates a non-default pointer encoding for
1456 P Indicates the presence of an encoding + language
1457 personality routine in the CIE augmentation.
1459 [This info comes from GCC's dwarf2out.c]
1463 aug_len
= read_uleb128 (abfd
, &start
);
1470 cie
->data_length
= block_end
- cie
->data
;
1472 while (*aug
!= '\0')
1474 if (aug
[0] == 'e' && aug
[1] == 'h')
1476 aug_data
+= sizeof (void *);
1479 else if (aug
[0] == 'R')
1480 cie
->addr_encoding
= *aug_data
++;
1481 else if (aug
[0] == 'P')
1483 CORE_ADDR pers_addr
;
1486 pers_addr_enc
= *aug_data
++;
1487 /* We don't need pers_addr value and so we
1488 don't care about it's encoding. */
1489 pers_addr
= read_encoded_pointer (abfd
, &aug_data
,
1492 else if (aug
[0] == 'L' && eh_frame
)
1496 /* Perhaps we should save this to CIE for later use?
1497 Do we need it for something in GDB? */
1498 lsda_addr_enc
= *aug_data
++;
1501 warning ("CFI warning: unknown augmentation \"%c\""
1503 "\t%s", aug
[0], curr_section_name
,
1512 struct fde_unit
*fde
;
1513 struct cie_unit
*cie
;
1517 /* We assume that debug_frame is in order
1518 CIE,FDE,CIE,FDE,FDE,... and thus the CIE for this FDE
1519 should be stored in last_cie pointer. If not, we'll
1520 try to find it by the older way. */
1525 warning ("CFI: last_cie == NULL. "
1526 "Perhaps a malformed %s section in '%s'...?\n",
1527 curr_section_name
, objfile
->name
);
1532 if (cie
->objfile
== objfile
)
1536 (unit_offset
+ bytes_read
- cie_id
)))
1538 if (!eh_frame
&& (cie
->offset
== cie_id
))
1545 error ("CFI: can't find CIE pointer");
1548 init_loc
= read_encoded_pointer (abfd
, &start
,
1549 cie
->addr_encoding
);
1551 switch (pointer_encoding (cie
->addr_encoding
))
1556 /* start-frame_buffer gives offset from
1557 the beginning of actual section. */
1558 init_loc
+= curr_section_vma
+ start
- frame_buffer
;
1561 warning ("CFI: Unsupported pointer encoding\n");
1564 /* For relocatable objects we must add an offset telling
1565 where the section is actually mapped in the memory. */
1566 init_loc
+= ANOFFSET (objfile
->section_offsets
,
1567 SECT_OFF_TEXT (objfile
));
1569 /* If we have both .debug_frame and .eh_frame present in
1570 a file, we must eliminate duplicate FDEs. For now we'll
1571 run through all entries in fde_chunks and check it one
1572 by one. Perhaps in the future we can implement a faster
1573 searching algorithm. */
1574 /* eh_frame==2 indicates, that this file has an already
1575 parsed .debug_frame too. When eh_frame==1 it means, that no
1576 .debug_frame is present and thus we don't need to check for
1577 duplicities. eh_frame==0 means, that we parse .debug_frame
1578 and don't need to care about duplicate FDEs, because
1579 .debug_frame is parsed first. */
1581 for (i
= 0; eh_frame
== 2 && i
< fde_chunks
.elems
; i
++)
1583 /* We assume that FDEs in .debug_frame and .eh_frame
1584 have the same order (if they are present, of course).
1585 If we find a duplicate entry for one FDE and save
1586 it's index to last_dup_fde it's very likely, that
1587 we'll find an entry for the following FDE right after
1588 the previous one. Thus in many cases we'll run this
1590 last_dup_fde
= (last_dup_fde
+ i
) % fde_chunks
.elems
;
1591 if (fde_chunks
.array
[last_dup_fde
]->initial_location
1599 /* Allocate a new entry only if this FDE isn't a duplicate of
1600 something we have already seen. */
1603 fde_chunks_need_space ();
1604 fde
= fde_unit_alloc ();
1606 fde_chunks
.array
[fde_chunks
.elems
++] = fde
;
1608 fde
->initial_location
= init_loc
;
1609 fde
->address_range
= read_encoded_pointer (abfd
, &start
,
1615 /* Here we intentionally ignore augmentation data
1616 from FDE, because we don't need them. */
1617 if (cie
->augmentation
[0] == 'z')
1618 start
+= read_uleb128 (abfd
, &start
);
1621 fde
->data_length
= block_end
- start
;
1626 qsort (fde_chunks
.array
, fde_chunks
.elems
,
1627 sizeof (struct fde_unit
*), compare_fde_unit
);
1631 /* We must parse both .debug_frame section and .eh_frame because
1632 * not all frames must be present in both of these sections. */
1634 dwarf2_build_frame_info (struct objfile
*objfile
)
1636 int after_debug_frame
= 0;
1638 /* If we have .debug_frame then the parser is called with
1639 eh_frame==0 for .debug_frame and eh_frame==2 for .eh_frame,
1640 otherwise it's only called once for .eh_frame with argument
1643 if (dwarf_frame_offset
)
1645 parse_frame_info (objfile
, dwarf_frame_offset
,
1646 dwarf_frame_size
, 0 /* = debug_frame */ );
1647 after_debug_frame
= 1;
1650 if (dwarf_eh_frame_offset
)
1651 parse_frame_info (objfile
, dwarf_eh_frame_offset
, dwarf_eh_frame_size
,
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 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 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
)
1762 get_reg ((char *) &(fi
->pc
), UNWIND_CONTEXT (fi
->next
), PC_REGNUM
);
1764 fi
->pc
= read_pc ();
1767 /* Initialize unwind context informations of the frame. */
1769 cfi_init_extra_frame_info (int fromleaf
, struct frame_info
*fi
)
1771 struct frame_state
*fs
;
1773 unwind_tmp_obstack_init ();
1775 fs
= frame_state_alloc ();
1776 fi
->context
= frame_obstack_alloc (sizeof (struct context
));
1777 UNWIND_CONTEXT (fi
)->reg
=
1778 frame_obstack_alloc (sizeof (struct context_reg
) * NUM_REGS
);
1779 memset (UNWIND_CONTEXT (fi
)->reg
, 0,
1780 sizeof (struct context_reg
) * NUM_REGS
);
1784 context_cpy (UNWIND_CONTEXT (fi
), UNWIND_CONTEXT (fi
->next
));
1785 frame_state_for (UNWIND_CONTEXT (fi
), fs
);
1786 update_context (UNWIND_CONTEXT (fi
), fs
, 1);
1790 UNWIND_CONTEXT (fi
)->ra
= fi
->pc
+ 1;
1791 frame_state_for (UNWIND_CONTEXT (fi
), fs
);
1792 update_context (UNWIND_CONTEXT (fi
), fs
, 0);
1795 unwind_tmp_obstack_free ();
1798 /* Obtain return address of the frame. */
1800 cfi_get_ra (struct frame_info
*fi
)
1802 return UNWIND_CONTEXT (fi
)->ra
;
1805 /* Find register number REGNUM relative to FRAME and put its
1806 (raw) contents in *RAW_BUFFER. Set *OPTIMIZED if the variable
1807 was optimized out (and thus can't be fetched). If the variable
1808 was fetched from memory, set *ADDRP to where it was fetched from,
1809 otherwise it was fetched from a register.
1811 The argument RAW_BUFFER must point to aligned memory. */
1813 cfi_get_saved_register (char *raw_buffer
,
1816 struct frame_info
*frame
,
1817 int regnum
, enum lval_type
*lval
)
1819 if (!target_has_registers
)
1820 error ("No registers.");
1822 /* Normal systems don't optimize out things with register numbers. */
1823 if (optimized
!= NULL
)
1826 if (addrp
) /* default assumption: not found in memory */
1831 read_register_gen (regnum
, raw_buffer
);
1833 *lval
= lval_register
;
1835 *addrp
= REGISTER_BYTE (regnum
);
1839 frame
= frame
->next
;
1840 switch (UNWIND_CONTEXT (frame
)->reg
[regnum
].how
)
1842 case REG_CTX_UNSAVED
:
1843 read_register_gen (regnum
, raw_buffer
);
1846 if (optimized
!= NULL
)
1849 case REG_CTX_SAVED_OFFSET
:
1850 target_read_memory (UNWIND_CONTEXT (frame
)->cfa
+
1851 UNWIND_CONTEXT (frame
)->reg
[regnum
].loc
.offset
,
1852 raw_buffer
, REGISTER_RAW_SIZE (regnum
));
1854 *lval
= lval_memory
;
1857 UNWIND_CONTEXT (frame
)->cfa
+
1858 UNWIND_CONTEXT (frame
)->reg
[regnum
].loc
.offset
;
1860 case REG_CTX_SAVED_REG
:
1861 read_register_gen (UNWIND_CONTEXT (frame
)->reg
[regnum
].loc
.reg
,
1864 *lval
= lval_register
;
1867 REGISTER_BYTE (UNWIND_CONTEXT (frame
)->reg
[regnum
].loc
.reg
);
1869 case REG_CTX_SAVED_ADDR
:
1870 target_read_memory (UNWIND_CONTEXT (frame
)->reg
[regnum
].loc
.addr
,
1871 raw_buffer
, REGISTER_RAW_SIZE (regnum
));
1873 *lval
= lval_memory
;
1875 *addrp
= UNWIND_CONTEXT (frame
)->reg
[regnum
].loc
.addr
;
1878 memcpy (raw_buffer
, &UNWIND_CONTEXT (frame
)->reg
[regnum
].loc
.addr
,
1879 REGISTER_RAW_SIZE (regnum
));
1882 if (optimized
!= NULL
)
1886 internal_error (__FILE__
, __LINE__
,
1887 "cfi_get_saved_register: unknown register rule");
1892 /* Return the register that the function uses for a frame pointer,
1893 plus any necessary offset to be applied to the register before
1894 any frame pointer offsets. */
1896 cfi_virtual_frame_pointer (CORE_ADDR pc
, int *frame_reg
,
1897 LONGEST
* frame_offset
)
1899 struct context
*context
;
1900 struct frame_state
*fs
;
1902 unwind_tmp_obstack_init ();
1904 context
= context_alloc ();
1905 fs
= frame_state_alloc ();
1907 context
->ra
= read_pc () + 1;
1909 frame_state_for (context
, fs
);
1911 if (fs
->cfa_how
== CFA_REG_OFFSET
)
1913 *frame_reg
= fs
->cfa_reg
;
1914 *frame_offset
= fs
->cfa_offset
;
1917 error ("dwarf cfi error: CFA is not defined as CFA_REG_OFFSET");
1919 unwind_tmp_obstack_free ();