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. */
28 #include "elf/dwarf2.h"
31 #include "dwarf2cfi.h"
33 /* Common Information Entry - holds information that is shared among many
37 /* Offset of this unit in .debug_frame or .eh_frame. */
40 /* A null-terminated string that identifies the augmentation to this CIE or
41 to the FDEs that use it. */
44 /* A constant that is factored out of all advance location instructions. */
45 unsigned int code_align;
47 /* A constant that is factored out of all offset instructions. */
50 /* A constant that indicates which regiter represents the return address
54 /* Indicates how addresses are encoded. */
55 unsigned char addr_encoding;
57 /* Pointer and length of the cie program. */
59 unsigned int data_length;
61 struct objfile *objfile;
64 struct cie_unit *next;
67 /* Frame Description Entry. */
70 /* Address of the first location associated with this entry. */
71 CORE_ADDR initial_location;
73 /* Length of program section described by this entry. */
74 CORE_ADDR address_range;
76 /* Pointer to asociated CIE. */
77 struct cie_unit *cie_ptr;
79 /* Pointer and length of the cie program. */
81 unsigned int data_length;
86 struct fde_unit **array;
103 REG_CTX_SAVED_OFFSET,
111 /* This is the register and unwind state for a particular frame. */
114 struct context_reg *reg;
122 struct frame_state_reg
143 /* Each register save state can be described in terms of a CFA slot,
144 another register, or a location expression. */
145 struct frame_state_regs
147 struct frame_state_reg *reg;
149 /* Used to implement DW_CFA_remember_state. */
150 struct frame_state_regs *prev;
154 /* The CFA can be described in terms of a reg+offset or a
155 location expression. */
158 unsigned char *cfa_exp;
167 /* The PC described by the current frame state. */
170 /* The information we care about from the CIE/FDE. */
172 unsigned int code_align;
173 unsigned char retaddr_column;
174 unsigned char addr_encoding;
176 struct objfile *objfile;
181 PE_absptr = DW_EH_PE_absptr,
182 PE_pcrel = DW_EH_PE_pcrel,
183 PE_textrel = DW_EH_PE_textrel,
184 PE_datarel = DW_EH_PE_datarel,
185 PE_funcrel = DW_EH_PE_funcrel
188 #define UNWIND_CONTEXT(fi) ((struct context *) (fi->context))
191 static struct cie_unit *cie_chunks;
192 static struct fde_array fde_chunks;
193 /* Obstack for allocating temporary storage used during unwind operations. */
194 static struct obstack unwind_tmp_obstack;
196 extern file_ptr dwarf_frame_offset;
197 extern unsigned int dwarf_frame_size;
198 extern file_ptr dwarf_eh_frame_offset;
199 extern unsigned int dwarf_eh_frame_size;
202 extern char *dwarf2_read_section (struct objfile *objfile, file_ptr offset,
205 static struct fde_unit *fde_unit_alloc (void);
206 static struct cie_unit *cie_unit_alloc (void);
207 static void fde_chunks_need_space ();
209 static struct context *context_alloc ();
210 static struct frame_state *frame_state_alloc ();
211 static void unwind_tmp_obstack_init ();
212 static void unwind_tmp_obstack_free ();
213 static void context_cpy (struct context *dst, struct context *src);
215 static unsigned int read_1u (bfd * abfd, char **p);
216 static int read_1s (bfd * abfd, char **p);
217 static unsigned int read_2u (bfd * abfd, char **p);
218 static int read_2s (bfd * abfd, char **p);
219 static unsigned int read_4u (bfd * abfd, char **p);
220 static int read_4s (bfd * abfd, char **p);
221 static ULONGEST read_8u (bfd * abfd, char **p);
222 static LONGEST read_8s (bfd * abfd, char **p);
224 static ULONGEST read_uleb128 (bfd * abfd, char **p);
225 static LONGEST read_sleb128 (bfd * abfd, char **p);
226 static CORE_ADDR read_pointer (bfd * abfd, char **p);
227 static CORE_ADDR read_encoded_pointer (bfd * abfd, char **p,
228 unsigned char encoding);
229 static enum ptr_encoding pointer_encoding (unsigned char encoding);
231 static LONGEST read_initial_length (bfd * abfd, char *buf, int *bytes_read);
232 static ULONGEST read_length (bfd * abfd, char *buf, int *bytes_read,
235 static int is_cie (ULONGEST cie_id, int dwarf64);
236 static int compare_fde_unit (const void *a, const void *b);
237 void dwarf2_build_frame_info (struct objfile *objfile);
239 static void execute_cfa_program (struct objfile *objfile, char *insn_ptr,
240 char *insn_end, struct context *context,
241 struct frame_state *fs);
242 static struct fde_unit *get_fde_for_addr (CORE_ADDR pc);
243 static void frame_state_for (struct context *context, struct frame_state *fs);
244 static void get_reg (char *reg, struct context *context, int regnum);
245 static CORE_ADDR execute_stack_op (struct objfile *objfile,
246 char *op_ptr, char *op_end,
247 struct context *context,
249 static void update_context (struct context *context, struct frame_state *fs,
253 /* Memory allocation functions. */
254 static struct fde_unit *
255 fde_unit_alloc (void)
257 struct fde_unit *fde;
259 fde = (struct fde_unit *) xmalloc (sizeof (struct fde_unit));
260 memset (fde, 0, sizeof (struct fde_unit));
264 static struct cie_unit *
265 cie_unit_alloc (void)
267 struct cie_unit *cie;
269 cie = (struct cie_unit *) xmalloc (sizeof (struct cie_unit));
270 memset (cie, 0, sizeof (struct cie_unit));
275 fde_chunks_need_space ()
277 if (fde_chunks.elems < fde_chunks.array_size)
279 fde_chunks.array_size =
280 fde_chunks.array_size ? 2 * fde_chunks.array_size : 1024;
282 xrealloc (fde_chunks.array,
283 sizeof (struct fde_unit) * fde_chunks.array_size);
286 /* Alocate a new `struct context' on temporary obstack. */
287 static struct context *
290 struct context *context;
292 int regs_size = sizeof (struct context_reg) * NUM_REGS;
294 context = (struct context *) obstack_alloc (&unwind_tmp_obstack,
295 sizeof (struct context));
296 memset (context, 0, sizeof (struct context));
297 context->reg = (struct context_reg *) obstack_alloc (&unwind_tmp_obstack,
299 memset (context->reg, 0, regs_size);
303 /* Alocate a new `struct frame_state' on temporary obstack. */
304 static struct frame_state *
307 struct frame_state *fs;
309 int regs_size = sizeof (struct frame_state_reg) * NUM_REGS;
311 fs = (struct frame_state *) obstack_alloc (&unwind_tmp_obstack,
312 sizeof (struct frame_state));
313 memset (fs, 0, sizeof (struct frame_state));
315 (struct frame_state_reg *) obstack_alloc (&unwind_tmp_obstack, regs_size);
316 memset (fs->regs.reg, 0, regs_size);
321 unwind_tmp_obstack_init ()
323 obstack_init (&unwind_tmp_obstack);
327 unwind_tmp_obstack_free ()
329 obstack_free (&unwind_tmp_obstack, NULL);
330 unwind_tmp_obstack_init ();
334 context_cpy (struct context *dst, struct context *src)
336 int regs_size = sizeof (struct context_reg) * NUM_REGS;
337 struct context_reg *dreg;
339 /* Structure dst contains a pointer to an array of
340 * registers of a given frame as well as src does. This
341 * array was already allocated before dst was passed to
342 * context_cpy but the pointer to it was overriden by
343 * '*dst = *src' and the array was lost. This led to the
344 * situation, that we've had a copy of src placed in dst,
345 * but both of them pointed to the same regs array and
346 * thus we've sometimes blindly rewritten it. Now we save
347 * the pointer before copying src to dst, return it back
348 * after that and copy the registers into their new place
349 * finally. --- mludvig@suse.cz */
354 memcpy (dst->reg, src->reg, regs_size);
358 read_1u (bfd * abfd, char **p)
362 ret = bfd_get_8 (abfd, (bfd_byte *) * p);
368 read_1s (bfd * abfd, char **p)
372 ret = bfd_get_signed_8 (abfd, (bfd_byte *) * p);
378 read_2u (bfd * abfd, char **p)
382 ret = bfd_get_16 (abfd, (bfd_byte *) * p);
388 read_2s (bfd * abfd, char **p)
392 ret = bfd_get_signed_16 (abfd, (bfd_byte *) * p);
398 read_4u (bfd * abfd, char **p)
402 ret = bfd_get_32 (abfd, (bfd_byte *) * p);
408 read_4s (bfd * abfd, char **p)
412 ret = bfd_get_signed_32 (abfd, (bfd_byte *) * p);
418 read_8u (bfd * abfd, char **p)
422 ret = bfd_get_64 (abfd, (bfd_byte *) * p);
428 read_8s (bfd * abfd, char **p)
432 ret = bfd_get_signed_64 (abfd, (bfd_byte *) * p);
438 read_uleb128 (bfd * abfd, char **p)
449 byte = bfd_get_8 (abfd, (bfd_byte *) * p);
451 ret |= ((unsigned long) (byte & 127) << shift);
452 if ((byte & 128) == 0)
462 read_sleb128 (bfd * abfd, char **p)
465 int i, shift, size, num_read;
475 byte = bfd_get_8 (abfd, (bfd_byte *) * p);
477 ret |= ((long) (byte & 127) << shift);
479 if ((byte & 128) == 0)
484 if ((shift < size) && (byte & 0x40))
486 ret |= -(1 << shift);
492 read_pointer (bfd * abfd, char **p)
494 switch (TARGET_ADDR_BIT / TARGET_CHAR_BIT)
497 return read_4u (abfd, p);
499 return read_8u (abfd, p);
501 error ("dwarf cfi error: unsupported target address length.");
505 /* This functions only reads appropriate amount of data from *p
506 * and returns the resulting value. Calling function must handle
507 * different encoding possibilities itself! */
509 read_encoded_pointer (bfd * abfd, char **p, unsigned char encoding)
513 switch (encoding & 0x0f)
515 case DW_EH_PE_absptr:
516 ret = read_pointer (abfd, p);
519 case DW_EH_PE_uleb128:
520 ret = read_uleb128 (abfd, p);
522 case DW_EH_PE_sleb128:
523 ret = read_sleb128 (abfd, p);
526 case DW_EH_PE_udata2:
527 ret = read_2u (abfd, p);
529 case DW_EH_PE_udata4:
530 ret = read_4u (abfd, p);
532 case DW_EH_PE_udata8:
533 ret = read_8u (abfd, p);
536 case DW_EH_PE_sdata2:
537 ret = read_2s (abfd, p);
539 case DW_EH_PE_sdata4:
540 ret = read_4s (abfd, p);
542 case DW_EH_PE_sdata8:
543 ret = read_8s (abfd, p);
547 internal_error (__FILE__, __LINE__,
548 "read_encoded_pointer: unknown pointer encoding");
554 /* Variable 'encoding' carries 3 different flags:
555 * - encoding & 0x0f : size of the address (handled in read_encoded_pointer())
556 * - encoding & 0x70 : type (absolute, relative, ...)
557 * - encoding & 0x80 : indirect flag (DW_EH_PE_indirect == 0x80). */
559 pointer_encoding (unsigned char encoding)
563 if (encoding & DW_EH_PE_indirect)
564 warning ("CFI: Unsupported pointer encoding: DW_EH_PE_indirect");
566 switch (encoding & 0x70)
568 case DW_EH_PE_absptr:
570 case DW_EH_PE_textrel:
571 case DW_EH_PE_datarel:
572 case DW_EH_PE_funcrel:
573 ret = encoding & 0x70;
576 internal_error (__FILE__, __LINE__, "CFI: unknown pointer encoding");
582 read_initial_length (bfd * abfd, char *buf, int *bytes_read)
586 ret = bfd_get_32 (abfd, (bfd_byte *) buf);
588 if (ret == 0xffffffff)
590 ret = bfd_get_64 (abfd, (bfd_byte *) buf + 4);
602 read_length (bfd * abfd, char *buf, int *bytes_read, int dwarf64)
607 return read_8u (abfd, &buf);
612 return read_4u (abfd, &buf);
617 execute_cfa_program (struct objfile *objfile, char *insn_ptr, char *insn_end,
618 struct context *context, struct frame_state *fs)
620 struct frame_state_regs *unused_rs = NULL;
622 /* Don't allow remember/restore between CIE and FDE programs. */
623 fs->regs.prev = NULL;
625 while (insn_ptr < insn_end && fs->pc < context->ra)
627 unsigned char insn = *insn_ptr++;
628 ULONGEST reg, uoffset;
631 if (insn & DW_CFA_advance_loc)
632 fs->pc += (insn & 0x3f) * fs->code_align;
633 else if (insn & DW_CFA_offset)
636 uoffset = read_uleb128 (objfile->obfd, &insn_ptr);
637 offset = (long) uoffset *fs->data_align;
638 fs->regs.reg[reg].how = REG_SAVED_OFFSET;
639 fs->regs.reg[reg].loc.offset = offset;
641 else if (insn & DW_CFA_restore)
644 fs->regs.reg[reg].how = REG_UNSAVED;
650 fs->pc = read_encoded_pointer (objfile->obfd, &insn_ptr,
653 if (pointer_encoding (fs->addr_encoding) != PE_absptr)
654 warning ("CFI: DW_CFA_set_loc uses relative addressing");
658 case DW_CFA_advance_loc1:
659 fs->pc += read_1u (objfile->obfd, &insn_ptr);
661 case DW_CFA_advance_loc2:
662 fs->pc += read_2u (objfile->obfd, &insn_ptr);
664 case DW_CFA_advance_loc4:
665 fs->pc += read_4u (objfile->obfd, &insn_ptr);
668 case DW_CFA_offset_extended:
669 reg = read_uleb128 (objfile->obfd, &insn_ptr);
670 uoffset = read_uleb128 (objfile->obfd, &insn_ptr);
671 offset = (long) uoffset *fs->data_align;
672 fs->regs.reg[reg].how = REG_SAVED_OFFSET;
673 fs->regs.reg[reg].loc.offset = offset;
676 case DW_CFA_restore_extended:
677 reg = read_uleb128 (objfile->obfd, &insn_ptr);
678 fs->regs.reg[reg].how = REG_UNSAVED;
681 case DW_CFA_undefined:
682 case DW_CFA_same_value:
686 case DW_CFA_register:
689 reg = read_uleb128 (objfile->obfd, &insn_ptr);
690 reg2 = read_uleb128 (objfile->obfd, &insn_ptr);
691 fs->regs.reg[reg].how = REG_SAVED_REG;
692 fs->regs.reg[reg].loc.reg = reg2;
696 case DW_CFA_remember_state:
698 struct frame_state_regs *new_rs;
702 unused_rs = unused_rs->prev;
705 new_rs = xmalloc (sizeof (struct frame_state_regs));
708 fs->regs.prev = new_rs;
712 case DW_CFA_restore_state:
714 struct frame_state_regs *old_rs = fs->regs.prev;
716 old_rs->prev = unused_rs;
722 reg = read_uleb128 (objfile->obfd, &insn_ptr);
723 uoffset = read_uleb128 (objfile->obfd, &insn_ptr);
725 fs->cfa_offset = uoffset;
726 fs->cfa_how = CFA_REG_OFFSET;
729 case DW_CFA_def_cfa_register:
730 reg = read_uleb128 (objfile->obfd, &insn_ptr);
732 fs->cfa_how = CFA_REG_OFFSET;
735 case DW_CFA_def_cfa_offset:
736 uoffset = read_uleb128 (objfile->obfd, &insn_ptr);
737 fs->cfa_offset = uoffset;
740 case DW_CFA_def_cfa_expression:
741 uoffset = read_uleb128 (objfile->obfd, &insn_ptr);
742 fs->cfa_exp = insn_ptr;
743 fs->cfa_how = CFA_EXP;
747 case DW_CFA_expression:
748 reg = read_uleb128 (objfile->obfd, &insn_ptr);
749 uoffset = read_uleb128 (objfile->obfd, &insn_ptr);
750 fs->regs.reg[reg].how = REG_SAVED_EXP;
751 fs->regs.reg[reg].loc.exp = insn_ptr;
755 /* From the 2.1 draft. */
756 case DW_CFA_offset_extended_sf:
757 reg = read_uleb128 (objfile->obfd, &insn_ptr);
758 offset = read_sleb128 (objfile->obfd, &insn_ptr);
759 offset *= fs->data_align;
760 fs->regs.reg[reg].how = REG_SAVED_OFFSET;
761 fs->regs.reg[reg].loc.offset = offset;
764 case DW_CFA_def_cfa_sf:
765 reg = read_uleb128 (objfile->obfd, &insn_ptr);
766 offset = read_sleb128 (objfile->obfd, &insn_ptr);
767 fs->cfa_offset = offset;
769 fs->cfa_how = CFA_REG_OFFSET;
772 case DW_CFA_def_cfa_offset_sf:
773 uoffset = read_uleb128 (objfile->obfd, &insn_ptr);
774 fs->cfa_offset = uoffset;
775 /* cfa_how deliberately not set. */
778 case DW_CFA_GNU_window_save:
779 /* ??? Hardcoded for SPARC register window configuration. */
780 for (reg = 16; reg < 32; ++reg)
782 fs->regs.reg[reg].how = REG_SAVED_OFFSET;
783 fs->regs.reg[reg].loc.offset = (reg - 16) * sizeof (void *);
787 case DW_CFA_GNU_args_size:
788 uoffset = read_uleb128 (objfile->obfd, &insn_ptr);
789 context->args_size = uoffset;
792 case DW_CFA_GNU_negative_offset_extended:
793 /* Obsoleted by DW_CFA_offset_extended_sf, but used by
794 older PowerPC code. */
795 reg = read_uleb128 (objfile->obfd, &insn_ptr);
796 uoffset = read_uleb128 (objfile->obfd, &insn_ptr);
797 offset = (long) uoffset *fs->data_align;
798 fs->regs.reg[reg].how = REG_SAVED_OFFSET;
799 fs->regs.reg[reg].loc.offset = -offset;
803 error ("dwarf cfi error: unknown cfa instruction %d.", insn);
808 static struct fde_unit *
809 get_fde_for_addr (CORE_ADDR pc)
812 struct fde_unit *fde = NULL;
814 hi = fde_chunks.elems;
818 size_t i = (lo + hi) / 2;
819 fde = fde_chunks.array[i];
820 if (pc < fde->initial_location)
822 else if (pc >= fde->initial_location + fde->address_range)
831 frame_state_for (struct context *context, struct frame_state *fs)
833 struct fde_unit *fde;
834 struct cie_unit *cie;
836 context->args_size = 0;
839 fde = get_fde_for_addr (context->ra - 1);
844 fs->pc = fde->initial_location;
850 fs->code_align = cie->code_align;
851 fs->data_align = cie->data_align;
852 fs->retaddr_column = cie->ra;
853 fs->addr_encoding = cie->addr_encoding;
854 fs->objfile = cie->objfile;
856 execute_cfa_program (cie->objfile, cie->data,
857 cie->data + cie->data_length, context, fs);
858 execute_cfa_program (cie->objfile, fde->data,
859 fde->data + fde->data_length, context, fs);
862 internal_error (__FILE__, __LINE__,
863 "%s(): Internal error: fde->cie_ptr==NULL !", __func__);
867 get_reg (char *reg, struct context *context, int regnum)
869 switch (context->reg[regnum].how)
871 case REG_CTX_UNSAVED:
872 read_register_gen (regnum, reg);
874 case REG_CTX_SAVED_OFFSET:
875 target_read_memory (context->cfa + context->reg[regnum].loc.offset,
876 reg, REGISTER_RAW_SIZE (regnum));
878 case REG_CTX_SAVED_REG:
879 read_register_gen (context->reg[regnum].loc.reg, reg);
881 case REG_CTX_SAVED_ADDR:
882 target_read_memory (context->reg[regnum].loc.addr,
883 reg, REGISTER_RAW_SIZE (regnum));
886 memcpy (reg, &context->reg[regnum].loc.addr,
887 REGISTER_RAW_SIZE (regnum));
890 internal_error (__FILE__, __LINE__, "get_reg: unknown register rule");
894 /* Decode a DW_OP stack program. Return the top of stack. Push INITIAL
895 onto the stack to start. */
897 execute_stack_op (struct objfile *objfile,
898 char *op_ptr, char *op_end, struct context *context,
901 CORE_ADDR stack[64]; /* ??? Assume this is enough. */
907 while (op_ptr < op_end)
909 enum dwarf_location_atom op = *op_ptr++;
948 result = op - DW_OP_lit0;
952 result = read_pointer (objfile->obfd, &op_ptr);
956 result = read_1u (objfile->obfd, &op_ptr);
959 result = read_1s (objfile->obfd, &op_ptr);
962 result = read_2u (objfile->obfd, &op_ptr);
965 result = read_2s (objfile->obfd, &op_ptr);
968 result = read_4u (objfile->obfd, &op_ptr);
971 result = read_4s (objfile->obfd, &op_ptr);
974 result = read_8u (objfile->obfd, &op_ptr);
977 result = read_8s (objfile->obfd, &op_ptr);
980 result = read_uleb128 (objfile->obfd, &op_ptr);
983 result = read_sleb128 (objfile->obfd, &op_ptr);
1018 get_reg ((char *) &result, context, op - DW_OP_reg0);
1021 reg = read_uleb128 (objfile->obfd, &op_ptr);
1022 get_reg ((char *) &result, context, reg);
1057 offset = read_sleb128 (objfile->obfd, &op_ptr);
1058 get_reg ((char *) &result, context, op - DW_OP_breg0);
1062 reg = read_uleb128 (objfile->obfd, &op_ptr);
1063 offset = read_sleb128 (objfile->obfd, &op_ptr);
1064 get_reg ((char *) &result, context, reg);
1070 internal_error (__FILE__, __LINE__, "execute_stack_op error");
1071 result = stack[stack_elt - 1];
1075 if (--stack_elt < 0)
1076 internal_error (__FILE__, __LINE__, "execute_stack_op error");
1081 if (offset >= stack_elt - 1)
1082 internal_error (__FILE__, __LINE__, "execute_stack_op error");
1083 result = stack[stack_elt - 1 - offset];
1088 internal_error (__FILE__, __LINE__, "execute_stack_op error");
1089 result = stack[stack_elt - 2];
1094 CORE_ADDR t1, t2, t3;
1097 internal_error (__FILE__, __LINE__, "execute_stack_op error");
1098 t1 = stack[stack_elt - 1];
1099 t2 = stack[stack_elt - 2];
1100 t3 = stack[stack_elt - 3];
1101 stack[stack_elt - 1] = t2;
1102 stack[stack_elt - 2] = t3;
1103 stack[stack_elt - 3] = t1;
1108 case DW_OP_deref_size:
1112 case DW_OP_plus_uconst:
1113 /* Unary operations. */
1114 if (--stack_elt < 0)
1115 internal_error (__FILE__, __LINE__, "execute_stack_op error");
1116 result = stack[stack_elt];
1122 char *ptr = (char *) result;
1123 result = read_pointer (objfile->obfd, &ptr);
1127 case DW_OP_deref_size:
1129 char *ptr = (char *) result;
1133 result = read_1u (objfile->obfd, &ptr);
1136 result = read_2u (objfile->obfd, &ptr);
1139 result = read_4u (objfile->obfd, &ptr);
1142 result = read_8u (objfile->obfd, &ptr);
1145 internal_error (__FILE__, __LINE__,
1146 "execute_stack_op error");
1161 case DW_OP_plus_uconst:
1162 result += read_uleb128 (objfile->obfd, &op_ptr);
1183 /* Binary operations. */
1184 CORE_ADDR first, second;
1185 if ((stack_elt -= 2) < 0)
1186 internal_error (__FILE__, __LINE__, "execute_stack_op error");
1187 second = stack[stack_elt];
1188 first = stack[stack_elt + 1];
1193 result = second & first;
1196 result = (LONGEST) second / (LONGEST) first;
1199 result = second - first;
1202 result = (LONGEST) second % (LONGEST) first;
1205 result = second * first;
1208 result = second | first;
1211 result = second + first;
1214 result = second << first;
1217 result = second >> first;
1220 result = (LONGEST) second >> first;
1223 result = second ^ first;
1226 result = (LONGEST) first <= (LONGEST) second;
1229 result = (LONGEST) first >= (LONGEST) second;
1232 result = (LONGEST) first == (LONGEST) second;
1235 result = (LONGEST) first < (LONGEST) second;
1238 result = (LONGEST) first > (LONGEST) second;
1241 result = (LONGEST) first != (LONGEST) second;
1243 default: /* This label is here just to avoid warning. */
1250 offset = read_2s (objfile->obfd, &op_ptr);
1255 if (--stack_elt < 0)
1256 internal_error (__FILE__, __LINE__, "execute_stack_op error");
1257 offset = read_2s (objfile->obfd, &op_ptr);
1258 if (stack[stack_elt] != 0)
1266 internal_error (__FILE__, __LINE__, "execute_stack_op error");
1269 /* Most things push a result value. */
1270 if ((size_t) stack_elt >= sizeof (stack) / sizeof (*stack))
1271 internal_error (__FILE__, __LINE__, "execute_stack_op error");
1272 stack[++stack_elt] = result;
1276 /* We were executing this program to get a value. It should be
1278 if (--stack_elt < 0)
1279 internal_error (__FILE__, __LINE__, "execute_stack_op error");
1280 return stack[stack_elt];
1284 update_context (struct context *context, struct frame_state *fs, int chain)
1286 struct context *orig_context;
1290 unwind_tmp_obstack_init ();
1292 orig_context = context_alloc ();
1293 context_cpy (orig_context, context);
1295 /* Compute this frame's CFA. */
1296 switch (fs->cfa_how)
1298 case CFA_REG_OFFSET:
1299 get_reg ((char *) &cfa, context, fs->cfa_reg);
1300 cfa += fs->cfa_offset;
1304 /* ??? No way of knowing what register number is the stack pointer
1305 to do the same sort of handling as above. Assume that if the
1306 CFA calculation is so complicated as to require a stack program
1307 that this will not be a problem. */
1309 char *exp = fs->cfa_exp;
1312 len = read_uleb128 (fs->objfile->obfd, &exp);
1313 cfa = (CORE_ADDR) execute_stack_op (fs->objfile, exp,
1314 exp + len, context, 0);
1323 orig_context->cfa = cfa;
1325 /* Compute the addresses of all registers saved in this frame. */
1326 for (i = 0; i < NUM_REGS; ++i)
1327 switch (fs->regs.reg[i].how)
1332 context->reg[i].how = REG_CTX_VALUE;
1333 context->reg[i].loc.addr = cfa;
1336 context->reg[i].how = REG_CTX_UNSAVED;
1338 case REG_SAVED_OFFSET:
1339 context->reg[i].how = REG_CTX_SAVED_OFFSET;
1340 context->reg[i].loc.offset = fs->regs.reg[i].loc.offset;
1343 switch (orig_context->reg[fs->regs.reg[i].loc.reg].how)
1345 case REG_CTX_UNSAVED:
1346 context->reg[i].how = REG_CTX_UNSAVED;
1348 case REG_CTX_SAVED_OFFSET:
1349 context->reg[i].how = REG_CTX_SAVED_OFFSET;
1350 context->reg[i].loc.offset = orig_context->cfa - context->cfa +
1351 orig_context->reg[fs->regs.reg[i].loc.reg].loc.offset;
1353 case REG_CTX_SAVED_REG:
1354 context->reg[i].how = REG_CTX_SAVED_REG;
1355 context->reg[i].loc.reg =
1356 orig_context->reg[fs->regs.reg[i].loc.reg].loc.reg;
1358 case REG_CTX_SAVED_ADDR:
1359 context->reg[i].how = REG_CTX_SAVED_ADDR;
1360 context->reg[i].loc.addr =
1361 orig_context->reg[fs->regs.reg[i].loc.reg].loc.addr;
1363 internal_error (__FILE__, __LINE__,
1364 "%s: unknown register rule", __func__);
1369 char *exp = fs->regs.reg[i].loc.exp;
1373 len = read_uleb128 (fs->objfile->obfd, &exp);
1374 val = execute_stack_op (fs->objfile, exp, exp + len,
1376 context->reg[i].how = REG_CTX_SAVED_ADDR;
1377 context->reg[i].loc.addr = val;
1381 internal_error (__FILE__, __LINE__,
1382 "%s: unknown register rule", __func__);
1384 get_reg ((char *) &context->ra, context, fs->retaddr_column);
1385 unwind_tmp_obstack_free ();
1389 is_cie (ULONGEST cie_id, int dwarf64)
1391 return dwarf64 ? (cie_id == 0xffffffffffffffff) : (cie_id == 0xffffffff);
1395 compare_fde_unit (const void *a, const void *b)
1397 struct fde_unit **first, **second;
1398 first = (struct fde_unit **) a;
1399 second = (struct fde_unit **) b;
1400 if ((*first)->initial_location > (*second)->initial_location)
1402 else if ((*first)->initial_location < (*second)->initial_location)
1408 /* Build the cie_chunks and fde_chunks tables from informations
1409 found in .debug_frame and .eh_frame sections. */
1410 /* We can handle both of these sections almost in the same way, however there
1411 are some exceptions:
1412 - CIE ID is -1 in debug_frame, but 0 in eh_frame
1413 - eh_frame may contain some more information that are used only by gcc
1414 (eg. personality pointer, LSDA pointer, ...). Most of them we can ignore.
1415 - In debug_frame FDE's item cie_id contains offset of it's parent CIE.
1416 In eh_frame FDE's item cie_id is a relative pointer to the parent CIE.
1417 Anyway we don't need to bother with this, because we are smart enough
1418 to keep the pointer to the parent CIE of oncomming FDEs in 'last_cie'.
1419 - Although debug_frame items can contain Augmentation as well as
1420 eh_frame ones, I have never seen them non-empty. Thus only in eh_frame
1421 we can encounter for example non-absolute pointers (Aug. 'R').
1424 parse_frame_info (struct objfile *objfile, file_ptr frame_offset,
1425 unsigned int frame_size, int eh_frame)
1427 bfd *abfd = objfile->obfd;
1428 asection *curr_section_ptr;
1431 char *frame_buffer = NULL;
1432 char *curr_section_name, *aug_data;
1433 struct cie_unit *last_cie = NULL;
1434 int last_dup_fde = 0;
1436 CORE_ADDR curr_section_vma = 0;
1438 unwind_tmp_obstack_init ();
1440 frame_buffer = dwarf2_read_section (objfile, frame_offset, frame_size);
1442 start = frame_buffer;
1443 end = frame_buffer + frame_size;
1445 curr_section_name = eh_frame ? ".eh_frame" : ".debug_frame";
1446 curr_section_ptr = bfd_get_section_by_name (abfd, curr_section_name);
1447 if (curr_section_ptr)
1448 curr_section_vma = curr_section_ptr->vma;
1454 unsigned long length;
1456 ULONGEST unit_offset = start - frame_buffer;
1457 int bytes_read, dwarf64;
1460 length = read_initial_length (abfd, start, &bytes_read);
1461 start += bytes_read;
1462 dwarf64 = (bytes_read == 12);
1463 block_end = start + length;
1471 cie_id = read_length (abfd, start, &bytes_read, dwarf64);
1472 start += bytes_read;
1474 if ((eh_frame && cie_id == 0) || is_cie (cie_id, dwarf64))
1476 struct cie_unit *cie = cie_unit_alloc ();
1479 cie->objfile = objfile;
1480 cie->next = cie_chunks;
1483 cie->objfile = objfile;
1485 cie->offset = unit_offset;
1487 start++; /* version */
1489 cie->augmentation = aug = start;
1490 while (*start++); /* Skips last NULL as well */
1492 cie->code_align = read_uleb128 (abfd, &start);
1493 cie->data_align = read_sleb128 (abfd, &start);
1494 cie->ra = read_1u (abfd, &start);
1497 z Indicates that a uleb128 is present to size the
1498 augmentation section.
1499 L Indicates the encoding (and thus presence) of
1500 an LSDA pointer in the FDE augmentation.
1501 R Indicates a non-default pointer encoding for
1503 P Indicates the presence of an encoding + language
1504 personality routine in the CIE augmentation.
1506 [This info comes from GCC's dwarf2out.c]
1510 aug_len = read_uleb128 (abfd, &start);
1517 cie->data_length = block_end - cie->data;
1519 while (*aug != '\0')
1521 if (aug[0] == 'e' && aug[1] == 'h')
1523 aug_data += sizeof (void *);
1526 else if (aug[0] == 'R')
1527 cie->addr_encoding = *aug_data++;
1528 else if (aug[0] == 'P')
1530 CORE_ADDR pers_addr;
1533 pers_addr_enc = *aug_data++;
1534 /* We don't need pers_addr value and so we
1535 don't care about it's encoding. */
1536 pers_addr = read_encoded_pointer (abfd, &aug_data,
1539 else if (aug[0] == 'L' && eh_frame)
1543 /* Perhaps we should save this to CIE for later use?
1544 Do we need it for something in GDB? */
1545 lsda_addr_enc = *aug_data++;
1548 warning ("CFI warning: unknown augmentation \"%c\""
1550 "\t%s", aug[0], curr_section_name,
1559 struct fde_unit *fde;
1560 struct cie_unit *cie;
1564 /* We assume that debug_frame is in order
1565 CIE,FDE,CIE,FDE,FDE,... and thus the CIE for this FDE
1566 should be stored in last_cie pointer. If not, we'll
1567 try to find it by the older way. */
1572 warning ("CFI: last_cie == NULL. "
1573 "Perhaps a malformed %s section in '%s'...?\n",
1574 curr_section_name, objfile->name);
1579 if (cie->objfile == objfile)
1583 (unit_offset + bytes_read - cie_id)))
1585 if (!eh_frame && (cie->offset == cie_id))
1592 error ("CFI: can't find CIE pointer");
1595 init_loc = read_encoded_pointer (abfd, &start,
1596 cie->addr_encoding);
1598 switch (pointer_encoding (cie->addr_encoding))
1603 /* start-frame_buffer gives offset from
1604 the beginning of actual section. */
1605 init_loc += curr_section_vma + start - frame_buffer;
1608 warning ("CFI: Unsupported pointer encoding\n");
1611 /* For relocatable objects we must add an offset telling
1612 where the section is actually mapped in the memory. */
1613 init_loc += ANOFFSET (objfile->section_offsets,
1614 SECT_OFF_TEXT (objfile));
1616 /* If we have both .debug_frame and .eh_frame present in
1617 a file, we must eliminate duplicate FDEs. For now we'll
1618 run through all entries in fde_chunks and check it one
1619 by one. Perhaps in the future we can implement a faster
1620 searching algorithm. */
1621 /* eh_frame==2 indicates, that this file has an already
1622 parsed .debug_frame too. When eh_frame==1 it means, that no
1623 .debug_frame is present and thus we don't need to check for
1624 duplicities. eh_frame==0 means, that we parse .debug_frame
1625 and don't need to care about duplicate FDEs, because
1626 .debug_frame is parsed first. */
1628 for (i = 0; eh_frame == 2 && i < fde_chunks.elems; i++)
1630 /* We assume that FDEs in .debug_frame and .eh_frame
1631 have the same order (if they are present, of course).
1632 If we find a duplicate entry for one FDE and save
1633 it's index to last_dup_fde it's very likely, that
1634 we'll find an entry for the following FDE right after
1635 the previous one. Thus in many cases we'll run this
1637 last_dup_fde = (last_dup_fde + i) % fde_chunks.elems;
1638 if (fde_chunks.array[last_dup_fde]->initial_location
1646 /* Allocate a new entry only if this FDE isn't a duplicate of
1647 something we have already seen. */
1650 fde_chunks_need_space ();
1651 fde = fde_unit_alloc ();
1653 fde_chunks.array[fde_chunks.elems++] = fde;
1655 fde->initial_location = init_loc;
1656 fde->address_range = read_encoded_pointer (abfd, &start,
1662 /* Here we intentionally ignore augmentation data
1663 from FDE, because we don't need them. */
1664 if (cie->augmentation[0] == 'z')
1665 start += read_uleb128 (abfd, &start);
1668 fde->data_length = block_end - start;
1673 qsort (fde_chunks.array, fde_chunks.elems,
1674 sizeof (struct fde_unit *), compare_fde_unit);
1678 /* We must parse both .debug_frame section and .eh_frame because
1679 * not all frames must be present in both of these sections. */
1681 dwarf2_build_frame_info (struct objfile *objfile)
1683 int after_debug_frame = 0;
1685 /* If we have .debug_frame then the parser is called with
1686 eh_frame==0 for .debug_frame and eh_frame==2 for .eh_frame,
1687 otherwise it's only called once for .eh_frame with argument
1690 if (dwarf_frame_offset)
1692 parse_frame_info (objfile, dwarf_frame_offset,
1693 dwarf_frame_size, 0 /* = debug_frame */ );
1694 after_debug_frame = 1;
1697 if (dwarf_eh_frame_offset)
1698 parse_frame_info (objfile, dwarf_eh_frame_offset, dwarf_eh_frame_size,
1699 1 /* = eh_frame */ + after_debug_frame);
1702 /* Return the frame address. */
1706 struct context *context;
1707 struct frame_state *fs;
1710 unwind_tmp_obstack_init ();
1712 context = context_alloc ();
1713 fs = frame_state_alloc ();
1715 context->ra = read_pc () + 1;
1717 frame_state_for (context, fs);
1718 update_context (context, fs, 0);
1722 unwind_tmp_obstack_free ();
1727 /* Store the frame address. This function is not used. */
1730 cfi_write_fp (CORE_ADDR val)
1732 struct context *context;
1733 struct frame_state *fs;
1735 unwind_tmp_obstack_init ();
1737 context = context_alloc ();
1738 fs = frame_state_alloc ();
1740 context->ra = read_pc () + 1;
1742 frame_state_for (context, fs);
1744 if (fs->cfa_how == CFA_REG_OFFSET)
1746 val -= fs->cfa_offset;
1747 write_register_gen (fs->cfa_reg, (char *) &val);
1750 warning ("Can't write fp.");
1752 unwind_tmp_obstack_free ();
1755 /* Restore the machine to the state it had before the current frame
1758 cfi_pop_frame (struct frame_info *fi)
1760 char *regbuf = alloca (MAX_REGISTER_RAW_SIZE);
1763 for (regnum = 0; regnum < NUM_REGS; regnum++)
1765 get_reg (regbuf, UNWIND_CONTEXT (fi), regnum);
1766 write_register_bytes (REGISTER_BYTE (regnum), regbuf,
1767 REGISTER_RAW_SIZE (regnum));
1769 write_register (PC_REGNUM, UNWIND_CONTEXT (fi)->ra);
1771 flush_cached_frames ();
1774 /* Determine the address of the calling function's frame. */
1776 cfi_frame_chain (struct frame_info *fi)
1778 struct context *context;
1779 struct frame_state *fs;
1782 unwind_tmp_obstack_init ();
1784 context = context_alloc ();
1785 fs = frame_state_alloc ();
1786 context_cpy (context, UNWIND_CONTEXT (fi));
1788 /* outermost frame */
1789 if (context->ra == 0)
1791 unwind_tmp_obstack_free ();
1795 frame_state_for (context, fs);
1796 update_context (context, fs, 1);
1799 unwind_tmp_obstack_free ();
1804 /* Sets the pc of the frame. */
1806 cfi_init_frame_pc (int fromleaf, struct frame_info *fi)
1809 get_reg ((char *) &(fi->pc), UNWIND_CONTEXT (fi->next), PC_REGNUM);
1811 fi->pc = read_pc ();
1814 /* Initialize unwind context informations of the frame. */
1816 cfi_init_extra_frame_info (int fromleaf, struct frame_info *fi)
1818 struct frame_state *fs;
1820 unwind_tmp_obstack_init ();
1822 fs = frame_state_alloc ();
1823 fi->context = frame_obstack_alloc (sizeof (struct context));
1824 UNWIND_CONTEXT (fi)->reg =
1825 frame_obstack_alloc (sizeof (struct context_reg) * NUM_REGS);
1826 memset (UNWIND_CONTEXT (fi)->reg, 0,
1827 sizeof (struct context_reg) * NUM_REGS);
1831 context_cpy (UNWIND_CONTEXT (fi), UNWIND_CONTEXT (fi->next));
1832 frame_state_for (UNWIND_CONTEXT (fi), fs);
1833 update_context (UNWIND_CONTEXT (fi), fs, 1);
1837 UNWIND_CONTEXT (fi)->ra = fi->pc + 1;
1838 frame_state_for (UNWIND_CONTEXT (fi), fs);
1839 update_context (UNWIND_CONTEXT (fi), fs, 0);
1842 unwind_tmp_obstack_free ();
1845 /* Obtain return address of the frame. */
1847 cfi_get_ra (struct frame_info *fi)
1849 return UNWIND_CONTEXT (fi)->ra;
1852 /* Find register number REGNUM relative to FRAME and put its
1853 (raw) contents in *RAW_BUFFER. Set *OPTIMIZED if the variable
1854 was optimized out (and thus can't be fetched). If the variable
1855 was fetched from memory, set *ADDRP to where it was fetched from,
1856 otherwise it was fetched from a register.
1858 The argument RAW_BUFFER must point to aligned memory. */
1860 cfi_get_saved_register (char *raw_buffer,
1863 struct frame_info *frame,
1864 int regnum, enum lval_type *lval)
1866 if (!target_has_registers)
1867 error ("No registers.");
1869 /* Normal systems don't optimize out things with register numbers. */
1870 if (optimized != NULL)
1873 if (addrp) /* default assumption: not found in memory */
1878 read_register_gen (regnum, raw_buffer);
1880 *lval = lval_register;
1882 *addrp = REGISTER_BYTE (regnum);
1886 frame = frame->next;
1887 switch (UNWIND_CONTEXT (frame)->reg[regnum].how)
1889 case REG_CTX_UNSAVED:
1890 read_register_gen (regnum, raw_buffer);
1893 if (optimized != NULL)
1896 case REG_CTX_SAVED_OFFSET:
1897 target_read_memory (UNWIND_CONTEXT (frame)->cfa +
1898 UNWIND_CONTEXT (frame)->reg[regnum].loc.offset,
1899 raw_buffer, REGISTER_RAW_SIZE (regnum));
1901 *lval = lval_memory;
1904 UNWIND_CONTEXT (frame)->cfa +
1905 UNWIND_CONTEXT (frame)->reg[regnum].loc.offset;
1907 case REG_CTX_SAVED_REG:
1908 read_register_gen (UNWIND_CONTEXT (frame)->reg[regnum].loc.reg,
1911 *lval = lval_register;
1914 REGISTER_BYTE (UNWIND_CONTEXT (frame)->reg[regnum].loc.reg);
1916 case REG_CTX_SAVED_ADDR:
1917 target_read_memory (UNWIND_CONTEXT (frame)->reg[regnum].loc.addr,
1918 raw_buffer, REGISTER_RAW_SIZE (regnum));
1920 *lval = lval_memory;
1922 *addrp = UNWIND_CONTEXT (frame)->reg[regnum].loc.addr;
1925 memcpy (raw_buffer, &UNWIND_CONTEXT (frame)->reg[regnum].loc.addr,
1926 REGISTER_RAW_SIZE (regnum));
1929 if (optimized != NULL)
1933 internal_error (__FILE__, __LINE__,
1934 "cfi_get_saved_register: unknown register rule");
1939 /* Return the register that the function uses for a frame pointer,
1940 plus any necessary offset to be applied to the register before
1941 any frame pointer offsets. */
1943 cfi_virtual_frame_pointer (CORE_ADDR pc, int *frame_reg,
1944 LONGEST * frame_offset)
1946 struct context *context;
1947 struct frame_state *fs;
1949 unwind_tmp_obstack_init ();
1951 context = context_alloc ();
1952 fs = frame_state_alloc ();
1954 context->ra = read_pc () + 1;
1956 frame_state_for (context, fs);
1958 if (fs->cfa_how == CFA_REG_OFFSET)
1960 *frame_reg = fs->cfa_reg;
1961 *frame_offset = fs->cfa_offset;
1964 error ("dwarf cfi error: CFA is not defined as CFA_REG_OFFSET");
1966 unwind_tmp_obstack_free ();