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.
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 /* Since `struct context' contains a pointer to an array with
308 register values, make sure we end up with a copy of that array,
309 and not with a copy of the pointer to that array. */
313 memcpy (dst->reg, src->reg, regs_size);
317 read_1u (bfd *abfd, char **p)
321 ret = bfd_get_8 (abfd, (bfd_byte *) * p);
327 read_1s (bfd *abfd, char **p)
331 ret = bfd_get_signed_8 (abfd, (bfd_byte *) * p);
337 read_2u (bfd *abfd, char **p)
341 ret = bfd_get_16 (abfd, (bfd_byte *) * p);
347 read_2s (bfd *abfd, char **p)
351 ret = bfd_get_signed_16 (abfd, (bfd_byte *) * p);
357 read_4u (bfd *abfd, char **p)
361 ret = bfd_get_32 (abfd, (bfd_byte *) * p);
367 read_4s (bfd *abfd, char **p)
371 ret = bfd_get_signed_32 (abfd, (bfd_byte *) * p);
377 read_8u (bfd *abfd, char **p)
381 ret = bfd_get_64 (abfd, (bfd_byte *) * p);
387 read_8s (bfd *abfd, char **p)
391 ret = bfd_get_signed_64 (abfd, (bfd_byte *) * p);
397 read_uleb128 (bfd *abfd, char **p)
408 byte = bfd_get_8 (abfd, (bfd_byte *) * p);
410 ret |= ((unsigned long) (byte & 127) << shift);
411 if ((byte & 128) == 0)
421 read_sleb128 (bfd *abfd, char **p)
424 int i, shift, size, num_read;
434 byte = bfd_get_8 (abfd, (bfd_byte *) * p);
436 ret |= ((long) (byte & 127) << shift);
438 if ((byte & 128) == 0)
443 if ((shift < size) && (byte & 0x40))
445 ret |= -(1 << shift);
451 read_pointer (bfd *abfd, char **p)
453 switch (TARGET_ADDR_BIT / TARGET_CHAR_BIT)
456 return read_4u (abfd, p);
458 return read_8u (abfd, p);
460 error ("dwarf cfi error: unsupported target address length.");
464 /* Read the appropriate amount of data from *P and return the
465 resulting value based on ENCODING, which the calling function must
468 read_encoded_pointer (bfd *abfd, char **p, unsigned char encoding)
472 switch (encoding & 0x0f)
474 case DW_EH_PE_absptr:
475 ret = read_pointer (abfd, p);
478 case DW_EH_PE_uleb128:
479 ret = read_uleb128 (abfd, p);
481 case DW_EH_PE_sleb128:
482 ret = read_sleb128 (abfd, p);
485 case DW_EH_PE_udata2:
486 ret = read_2u (abfd, p);
488 case DW_EH_PE_udata4:
489 ret = read_4u (abfd, p);
491 case DW_EH_PE_udata8:
492 ret = read_8u (abfd, p);
495 case DW_EH_PE_sdata2:
496 ret = read_2s (abfd, p);
498 case DW_EH_PE_sdata4:
499 ret = read_4s (abfd, p);
501 case DW_EH_PE_sdata8:
502 ret = read_8s (abfd, p);
506 internal_error (__FILE__, __LINE__,
507 "read_encoded_pointer: unknown pointer encoding");
513 /* The variable 'encoding' carries three different flags:
514 - encoding & 0x0f : size of the address (handled in read_encoded_pointer())
515 - encoding & 0x70 : type (absolute, relative, ...)
516 - encoding & 0x80 : indirect flag (DW_EH_PE_indirect == 0x80). */
518 pointer_encoding (unsigned char encoding)
522 if (encoding & DW_EH_PE_indirect)
523 warning ("CFI: Unsupported pointer encoding: DW_EH_PE_indirect");
525 switch (encoding & 0x70)
527 case DW_EH_PE_absptr:
529 case DW_EH_PE_textrel:
530 case DW_EH_PE_datarel:
531 case DW_EH_PE_funcrel:
532 ret = encoding & 0x70;
535 internal_error (__FILE__, __LINE__, "CFI: unknown pointer encoding");
541 read_initial_length (bfd *abfd, char *buf, int *bytes_read)
545 ret = bfd_get_32 (abfd, (bfd_byte *) buf);
547 if (ret == 0xffffffff)
549 ret = bfd_get_64 (abfd, (bfd_byte *) buf + 4);
561 read_length (bfd *abfd, char *buf, int *bytes_read, int dwarf64)
566 return read_8u (abfd, &buf);
571 return read_4u (abfd, &buf);
576 execute_cfa_program (struct objfile *objfile, char *insn_ptr, char *insn_end,
577 struct context *context, struct frame_state *fs)
579 struct frame_state_regs *unused_rs = NULL;
581 /* Don't allow remember/restore between CIE and FDE programs. */
582 fs->regs.prev = NULL;
584 while (insn_ptr < insn_end && fs->pc < context->ra)
586 unsigned char insn = *insn_ptr++;
587 ULONGEST reg, uoffset;
590 if (insn & DW_CFA_advance_loc)
591 fs->pc += (insn & 0x3f) * fs->code_align;
592 else if (insn & DW_CFA_offset)
595 uoffset = read_uleb128 (objfile->obfd, &insn_ptr);
596 offset = (long) uoffset *fs->data_align;
597 fs->regs.reg[reg].how = REG_SAVED_OFFSET;
598 fs->regs.reg[reg].loc.offset = offset;
600 else if (insn & DW_CFA_restore)
603 fs->regs.reg[reg].how = REG_UNSAVED;
609 fs->pc = read_encoded_pointer (objfile->obfd, &insn_ptr,
612 if (pointer_encoding (fs->addr_encoding) != PE_absptr)
613 warning ("CFI: DW_CFA_set_loc uses relative addressing");
617 case DW_CFA_advance_loc1:
618 fs->pc += read_1u (objfile->obfd, &insn_ptr);
620 case DW_CFA_advance_loc2:
621 fs->pc += read_2u (objfile->obfd, &insn_ptr);
623 case DW_CFA_advance_loc4:
624 fs->pc += read_4u (objfile->obfd, &insn_ptr);
627 case DW_CFA_offset_extended:
628 reg = read_uleb128 (objfile->obfd, &insn_ptr);
629 uoffset = read_uleb128 (objfile->obfd, &insn_ptr);
630 offset = (long) uoffset *fs->data_align;
631 fs->regs.reg[reg].how = REG_SAVED_OFFSET;
632 fs->regs.reg[reg].loc.offset = offset;
635 case DW_CFA_restore_extended:
636 reg = read_uleb128 (objfile->obfd, &insn_ptr);
637 fs->regs.reg[reg].how = REG_UNSAVED;
640 case DW_CFA_undefined:
641 case DW_CFA_same_value:
645 case DW_CFA_register:
648 reg = read_uleb128 (objfile->obfd, &insn_ptr);
649 reg2 = read_uleb128 (objfile->obfd, &insn_ptr);
650 fs->regs.reg[reg].how = REG_SAVED_REG;
651 fs->regs.reg[reg].loc.reg = reg2;
655 case DW_CFA_remember_state:
657 struct frame_state_regs *new_rs;
661 unused_rs = unused_rs->prev;
664 new_rs = xmalloc (sizeof (struct frame_state_regs));
667 fs->regs.prev = new_rs;
671 case DW_CFA_restore_state:
673 struct frame_state_regs *old_rs = fs->regs.prev;
675 old_rs->prev = unused_rs;
681 reg = read_uleb128 (objfile->obfd, &insn_ptr);
682 uoffset = read_uleb128 (objfile->obfd, &insn_ptr);
684 fs->cfa_offset = uoffset;
685 fs->cfa_how = CFA_REG_OFFSET;
688 case DW_CFA_def_cfa_register:
689 reg = read_uleb128 (objfile->obfd, &insn_ptr);
691 fs->cfa_how = CFA_REG_OFFSET;
694 case DW_CFA_def_cfa_offset:
695 uoffset = read_uleb128 (objfile->obfd, &insn_ptr);
696 fs->cfa_offset = uoffset;
699 case DW_CFA_def_cfa_expression:
700 uoffset = read_uleb128 (objfile->obfd, &insn_ptr);
701 fs->cfa_exp = insn_ptr;
702 fs->cfa_how = CFA_EXP;
706 case DW_CFA_expression:
707 reg = read_uleb128 (objfile->obfd, &insn_ptr);
708 uoffset = read_uleb128 (objfile->obfd, &insn_ptr);
709 fs->regs.reg[reg].how = REG_SAVED_EXP;
710 fs->regs.reg[reg].loc.exp = insn_ptr;
714 /* From the 2.1 draft. */
715 case DW_CFA_offset_extended_sf:
716 reg = read_uleb128 (objfile->obfd, &insn_ptr);
717 offset = read_sleb128 (objfile->obfd, &insn_ptr);
718 offset *= fs->data_align;
719 fs->regs.reg[reg].how = REG_SAVED_OFFSET;
720 fs->regs.reg[reg].loc.offset = offset;
723 case DW_CFA_def_cfa_sf:
724 reg = read_uleb128 (objfile->obfd, &insn_ptr);
725 offset = read_sleb128 (objfile->obfd, &insn_ptr);
726 fs->cfa_offset = offset;
728 fs->cfa_how = CFA_REG_OFFSET;
731 case DW_CFA_def_cfa_offset_sf:
732 uoffset = read_uleb128 (objfile->obfd, &insn_ptr);
733 fs->cfa_offset = uoffset;
734 /* cfa_how deliberately not set. */
737 case DW_CFA_GNU_window_save:
738 /* ??? Hardcoded for SPARC register window configuration. */
739 for (reg = 16; reg < 32; ++reg)
741 fs->regs.reg[reg].how = REG_SAVED_OFFSET;
742 fs->regs.reg[reg].loc.offset = (reg - 16) * sizeof (void *);
746 case DW_CFA_GNU_args_size:
747 uoffset = read_uleb128 (objfile->obfd, &insn_ptr);
748 context->args_size = uoffset;
751 case DW_CFA_GNU_negative_offset_extended:
752 /* Obsoleted by DW_CFA_offset_extended_sf, but used by
753 older PowerPC code. */
754 reg = read_uleb128 (objfile->obfd, &insn_ptr);
755 uoffset = read_uleb128 (objfile->obfd, &insn_ptr);
756 offset = (long) uoffset *fs->data_align;
757 fs->regs.reg[reg].how = REG_SAVED_OFFSET;
758 fs->regs.reg[reg].loc.offset = -offset;
762 error ("dwarf cfi error: unknown cfa instruction %d.", insn);
767 static struct fde_unit *
768 get_fde_for_addr (CORE_ADDR pc)
771 struct fde_unit *fde = NULL;
773 hi = fde_chunks.elems;
777 size_t i = (lo + hi) / 2;
778 fde = fde_chunks.array[i];
779 if (pc < fde->initial_location)
781 else if (pc >= fde->initial_location + fde->address_range)
790 frame_state_for (struct context *context, struct frame_state *fs)
792 struct fde_unit *fde;
793 struct cie_unit *cie;
795 context->args_size = 0;
798 fde = get_fde_for_addr (context->ra - 1);
803 fs->pc = fde->initial_location;
805 gdb_assert (fde->cie_ptr != NULL);
809 fs->code_align = cie->code_align;
810 fs->data_align = cie->data_align;
811 fs->retaddr_column = cie->ra;
812 fs->addr_encoding = cie->addr_encoding;
813 fs->objfile = cie->objfile;
815 execute_cfa_program (cie->objfile, cie->data,
816 cie->data + cie->data_length, context, fs);
817 execute_cfa_program (cie->objfile, fde->data,
818 fde->data + fde->data_length, context, fs);
822 get_reg (char *reg, struct context *context, int regnum)
824 switch (context->reg[regnum].how)
826 case REG_CTX_UNSAVED:
827 deprecated_read_register_gen (regnum, reg);
829 case REG_CTX_SAVED_OFFSET:
830 target_read_memory (context->cfa + context->reg[regnum].loc.offset,
831 reg, REGISTER_RAW_SIZE (regnum));
833 case REG_CTX_SAVED_REG:
834 deprecated_read_register_gen (context->reg[regnum].loc.reg, reg);
836 case REG_CTX_SAVED_ADDR:
837 target_read_memory (context->reg[regnum].loc.addr,
838 reg, REGISTER_RAW_SIZE (regnum));
841 memcpy (reg, &context->reg[regnum].loc.addr,
842 REGISTER_RAW_SIZE (regnum));
845 internal_error (__FILE__, __LINE__, "get_reg: unknown register rule");
849 /* Decode a DW_OP stack program. Return the top of stack. Push INITIAL
850 onto the stack to start. */
852 execute_stack_op (struct objfile *objfile,
853 char *op_ptr, char *op_end, struct context *context,
856 CORE_ADDR stack[64]; /* ??? Assume this is enough. */
862 while (op_ptr < op_end)
864 enum dwarf_location_atom op = *op_ptr++;
903 result = op - DW_OP_lit0;
907 result = read_pointer (objfile->obfd, &op_ptr);
911 result = read_1u (objfile->obfd, &op_ptr);
914 result = read_1s (objfile->obfd, &op_ptr);
917 result = read_2u (objfile->obfd, &op_ptr);
920 result = read_2s (objfile->obfd, &op_ptr);
923 result = read_4u (objfile->obfd, &op_ptr);
926 result = read_4s (objfile->obfd, &op_ptr);
929 result = read_8u (objfile->obfd, &op_ptr);
932 result = read_8s (objfile->obfd, &op_ptr);
935 result = read_uleb128 (objfile->obfd, &op_ptr);
938 result = read_sleb128 (objfile->obfd, &op_ptr);
973 get_reg ((char *) &result, context, op - DW_OP_reg0);
976 reg = read_uleb128 (objfile->obfd, &op_ptr);
977 get_reg ((char *) &result, context, reg);
1012 offset = read_sleb128 (objfile->obfd, &op_ptr);
1013 get_reg ((char *) &result, context, op - DW_OP_breg0);
1017 reg = read_uleb128 (objfile->obfd, &op_ptr);
1018 offset = read_sleb128 (objfile->obfd, &op_ptr);
1019 get_reg ((char *) &result, context, reg);
1025 internal_error (__FILE__, __LINE__, "execute_stack_op error");
1026 result = stack[stack_elt - 1];
1030 if (--stack_elt < 0)
1031 internal_error (__FILE__, __LINE__, "execute_stack_op error");
1036 if (offset >= stack_elt - 1)
1037 internal_error (__FILE__, __LINE__, "execute_stack_op error");
1038 result = stack[stack_elt - 1 - offset];
1043 internal_error (__FILE__, __LINE__, "execute_stack_op error");
1044 result = stack[stack_elt - 2];
1049 CORE_ADDR t1, t2, t3;
1052 internal_error (__FILE__, __LINE__, "execute_stack_op error");
1053 t1 = stack[stack_elt - 1];
1054 t2 = stack[stack_elt - 2];
1055 t3 = stack[stack_elt - 3];
1056 stack[stack_elt - 1] = t2;
1057 stack[stack_elt - 2] = t3;
1058 stack[stack_elt - 3] = t1;
1063 case DW_OP_deref_size:
1067 case DW_OP_plus_uconst:
1068 /* Unary operations. */
1069 if (--stack_elt < 0)
1070 internal_error (__FILE__, __LINE__, "execute_stack_op error");
1071 result = stack[stack_elt];
1077 int len = TARGET_ADDR_BIT / TARGET_CHAR_BIT;
1078 if (len != 4 && len != 8)
1079 internal_error (__FILE__, __LINE__,
1080 "execute_stack_op error");
1081 result = read_memory_unsigned_integer (result, len);
1085 case DW_OP_deref_size:
1087 int len = *op_ptr++;
1088 if (len != 1 && len != 2 && len != 4 && len != 8)
1089 internal_error (__FILE__, __LINE__,
1090 "execute_stack_op error");
1091 result = read_memory_unsigned_integer (result, len);
1105 case DW_OP_plus_uconst:
1106 result += read_uleb128 (objfile->obfd, &op_ptr);
1127 /* Binary operations. */
1128 CORE_ADDR first, second;
1129 if ((stack_elt -= 2) < 0)
1130 internal_error (__FILE__, __LINE__, "execute_stack_op error");
1131 second = stack[stack_elt];
1132 first = stack[stack_elt + 1];
1137 result = second & first;
1140 result = (LONGEST) second / (LONGEST) first;
1143 result = second - first;
1146 result = (LONGEST) second % (LONGEST) first;
1149 result = second * first;
1152 result = second | first;
1155 result = second + first;
1158 result = second << first;
1161 result = second >> first;
1164 result = (LONGEST) second >> first;
1167 result = second ^ first;
1170 result = (LONGEST) first <= (LONGEST) second;
1173 result = (LONGEST) first >= (LONGEST) second;
1176 result = (LONGEST) first == (LONGEST) second;
1179 result = (LONGEST) first < (LONGEST) second;
1182 result = (LONGEST) first > (LONGEST) second;
1185 result = (LONGEST) first != (LONGEST) second;
1188 error ("execute_stack_op: Unknown DW_OP_ value");
1195 offset = read_2s (objfile->obfd, &op_ptr);
1200 if (--stack_elt < 0)
1201 internal_error (__FILE__, __LINE__, "execute_stack_op error");
1202 offset = read_2s (objfile->obfd, &op_ptr);
1203 if (stack[stack_elt] != 0)
1211 internal_error (__FILE__, __LINE__, "execute_stack_op error");
1214 /* Most things push a result value. */
1215 if ((size_t) stack_elt >= sizeof (stack) / sizeof (*stack))
1216 internal_error (__FILE__, __LINE__, "execute_stack_op error");
1217 stack[++stack_elt] = result;
1221 /* We were executing this program to get a value. It should be
1223 if (--stack_elt < 0)
1224 internal_error (__FILE__, __LINE__, "execute_stack_op error");
1225 return stack[stack_elt];
1229 update_context (struct context *context, struct frame_state *fs, int chain)
1231 struct context *orig_context;
1235 unwind_tmp_obstack_init ();
1237 orig_context = context_alloc ();
1238 context_cpy (orig_context, context);
1240 /* Compute this frame's CFA. */
1241 switch (fs->cfa_how)
1243 case CFA_REG_OFFSET:
1244 get_reg ((char *) &cfa, context, fs->cfa_reg);
1245 cfa += fs->cfa_offset;
1249 /* ??? No way of knowing what register number is the stack pointer
1250 to do the same sort of handling as above. Assume that if the
1251 CFA calculation is so complicated as to require a stack program
1252 that this will not be a problem. */
1254 char *exp = fs->cfa_exp;
1257 len = read_uleb128 (fs->objfile->obfd, &exp);
1258 cfa = (CORE_ADDR) execute_stack_op (fs->objfile, exp,
1259 exp + len, context, 0);
1268 orig_context->cfa = cfa;
1270 /* Compute the addresses of all registers saved in this frame. */
1271 for (i = 0; i < NUM_REGS; ++i)
1272 switch (fs->regs.reg[i].how)
1277 context->reg[i].how = REG_CTX_VALUE;
1278 context->reg[i].loc.addr = cfa;
1281 context->reg[i].how = REG_CTX_UNSAVED;
1283 case REG_SAVED_OFFSET:
1284 context->reg[i].how = REG_CTX_SAVED_OFFSET;
1285 context->reg[i].loc.offset = fs->regs.reg[i].loc.offset;
1288 switch (orig_context->reg[fs->regs.reg[i].loc.reg].how)
1290 case REG_CTX_UNSAVED:
1291 context->reg[i].how = REG_CTX_UNSAVED;
1293 case REG_CTX_SAVED_OFFSET:
1294 context->reg[i].how = REG_CTX_SAVED_OFFSET;
1295 context->reg[i].loc.offset = orig_context->cfa - context->cfa +
1296 orig_context->reg[fs->regs.reg[i].loc.reg].loc.offset;
1298 case REG_CTX_SAVED_REG:
1299 context->reg[i].how = REG_CTX_SAVED_REG;
1300 context->reg[i].loc.reg =
1301 orig_context->reg[fs->regs.reg[i].loc.reg].loc.reg;
1303 case REG_CTX_SAVED_ADDR:
1304 context->reg[i].how = REG_CTX_SAVED_ADDR;
1305 context->reg[i].loc.addr =
1306 orig_context->reg[fs->regs.reg[i].loc.reg].loc.addr;
1309 internal_error (__FILE__, __LINE__, "bad switch");
1314 char *exp = fs->regs.reg[i].loc.exp;
1318 len = read_uleb128 (fs->objfile->obfd, &exp);
1319 val = execute_stack_op (fs->objfile, exp, exp + len,
1321 context->reg[i].how = REG_CTX_SAVED_ADDR;
1322 context->reg[i].loc.addr = val;
1326 internal_error (__FILE__, __LINE__, "bad switch");
1328 get_reg ((char *) &context->ra, context, fs->retaddr_column);
1329 unwind_tmp_obstack_free ();
1333 is_cie (ULONGEST cie_id, int dwarf64)
1335 return dwarf64 ? (cie_id == 0xffffffffffffffff) : (cie_id == 0xffffffff);
1339 compare_fde_unit (const void *a, const void *b)
1341 struct fde_unit **first, **second;
1342 first = (struct fde_unit **) a;
1343 second = (struct fde_unit **) b;
1344 if ((*first)->initial_location > (*second)->initial_location)
1346 else if ((*first)->initial_location < (*second)->initial_location)
1352 /* Build the cie_chunks and fde_chunks tables from informations
1353 found in .debug_frame and .eh_frame sections. */
1354 /* We can handle both of these sections almost in the same way, however there
1355 are some exceptions:
1356 - CIE ID is -1 in debug_frame, but 0 in eh_frame
1357 - eh_frame may contain some more information that are used only by gcc
1358 (eg. personality pointer, LSDA pointer, ...). Most of them we can ignore.
1359 - In debug_frame FDE's item cie_id contains offset of it's parent CIE.
1360 In eh_frame FDE's item cie_id is a relative pointer to the parent CIE.
1361 Anyway we don't need to bother with this, because we are smart enough
1362 to keep the pointer to the parent CIE of oncomming FDEs in 'last_cie'.
1363 - Although debug_frame items can contain Augmentation as well as
1364 eh_frame ones, I have never seen them non-empty. Thus only in eh_frame
1365 we can encounter for example non-absolute pointers (Aug. 'R').
1368 parse_frame_info (struct objfile *objfile, file_ptr frame_offset,
1369 unsigned int frame_size, int eh_frame)
1371 bfd *abfd = objfile->obfd;
1372 asection *curr_section_ptr;
1375 char *frame_buffer = NULL;
1376 char *curr_section_name, *aug_data;
1377 struct cie_unit *last_cie = NULL;
1378 int last_dup_fde = 0;
1380 CORE_ADDR curr_section_vma = 0;
1382 unwind_tmp_obstack_init ();
1384 frame_buffer = dwarf2_read_section (objfile, frame_offset, frame_size);
1386 start = frame_buffer;
1387 end = frame_buffer + frame_size;
1389 curr_section_name = eh_frame ? ".eh_frame" : ".debug_frame";
1390 curr_section_ptr = bfd_get_section_by_name (abfd, curr_section_name);
1391 if (curr_section_ptr)
1392 curr_section_vma = curr_section_ptr->vma;
1398 unsigned long length;
1400 ULONGEST unit_offset = start - frame_buffer;
1401 int bytes_read, dwarf64;
1404 length = read_initial_length (abfd, start, &bytes_read);
1405 start += bytes_read;
1406 dwarf64 = (bytes_read == 12);
1407 block_end = start + length;
1415 cie_id = read_length (abfd, start, &bytes_read, dwarf64);
1416 start += bytes_read;
1418 if ((eh_frame && cie_id == 0) || is_cie (cie_id, dwarf64))
1420 struct cie_unit *cie = cie_unit_alloc ();
1423 cie->objfile = objfile;
1424 cie->next = cie_chunks;
1427 cie->objfile = objfile;
1429 cie->offset = unit_offset;
1431 start++; /* version */
1433 cie->augmentation = aug = start;
1434 while (*start++); /* Skips last NULL as well */
1436 cie->code_align = read_uleb128 (abfd, &start);
1437 cie->data_align = read_sleb128 (abfd, &start);
1438 cie->ra = read_1u (abfd, &start);
1441 z Indicates that a uleb128 is present to size the
1442 augmentation section.
1443 L Indicates the encoding (and thus presence) of
1444 an LSDA pointer in the FDE augmentation.
1445 R Indicates a non-default pointer encoding for
1447 P Indicates the presence of an encoding + language
1448 personality routine in the CIE augmentation.
1450 [This info comes from GCC's dwarf2out.c]
1454 aug_len = read_uleb128 (abfd, &start);
1461 cie->data_length = block_end - cie->data;
1463 while (*aug != '\0')
1465 if (aug[0] == 'e' && aug[1] == 'h')
1467 aug_data += sizeof (void *);
1470 else if (aug[0] == 'R')
1471 cie->addr_encoding = *aug_data++;
1472 else if (aug[0] == 'P')
1474 CORE_ADDR pers_addr;
1477 pers_addr_enc = *aug_data++;
1478 /* We don't need pers_addr value and so we
1479 don't care about it's encoding. */
1480 pers_addr = read_encoded_pointer (abfd, &aug_data,
1483 else if (aug[0] == 'L' && eh_frame)
1487 /* Perhaps we should save this to CIE for later use?
1488 Do we need it for something in GDB? */
1489 lsda_addr_enc = *aug_data++;
1492 warning ("CFI warning: unknown augmentation \"%c\""
1494 "\t%s", aug[0], curr_section_name,
1503 struct fde_unit *fde;
1504 struct cie_unit *cie;
1508 /* We assume that debug_frame is in order
1509 CIE,FDE,CIE,FDE,FDE,... and thus the CIE for this FDE
1510 should be stored in last_cie pointer. If not, we'll
1511 try to find it by the older way. */
1516 warning ("CFI: last_cie == NULL. "
1517 "Perhaps a malformed %s section in '%s'...?\n",
1518 curr_section_name, objfile->name);
1523 if (cie->objfile == objfile)
1527 (unit_offset + bytes_read - cie_id)))
1529 if (!eh_frame && (cie->offset == cie_id))
1536 error ("CFI: can't find CIE pointer");
1539 init_loc = read_encoded_pointer (abfd, &start,
1540 cie->addr_encoding);
1542 switch (pointer_encoding (cie->addr_encoding))
1547 /* start-frame_buffer gives offset from
1548 the beginning of actual section. */
1549 init_loc += curr_section_vma + start - frame_buffer;
1552 warning ("CFI: Unsupported pointer encoding\n");
1555 /* For relocatable objects we must add an offset telling
1556 where the section is actually mapped in the memory. */
1557 init_loc += ANOFFSET (objfile->section_offsets,
1558 SECT_OFF_TEXT (objfile));
1560 /* If we have both .debug_frame and .eh_frame present in
1561 a file, we must eliminate duplicate FDEs. For now we'll
1562 run through all entries in fde_chunks and check it one
1563 by one. Perhaps in the future we can implement a faster
1564 searching algorithm. */
1565 /* eh_frame==2 indicates, that this file has an already
1566 parsed .debug_frame too. When eh_frame==1 it means, that no
1567 .debug_frame is present and thus we don't need to check for
1568 duplicities. eh_frame==0 means, that we parse .debug_frame
1569 and don't need to care about duplicate FDEs, because
1570 .debug_frame is parsed first. */
1572 for (i = 0; eh_frame == 2 && i < fde_chunks.elems; i++)
1574 /* We assume that FDEs in .debug_frame and .eh_frame
1575 have the same order (if they are present, of course).
1576 If we find a duplicate entry for one FDE and save
1577 it's index to last_dup_fde it's very likely, that
1578 we'll find an entry for the following FDE right after
1579 the previous one. Thus in many cases we'll run this
1581 last_dup_fde = (last_dup_fde + i) % fde_chunks.elems;
1582 if (fde_chunks.array[last_dup_fde]->initial_location
1590 /* Allocate a new entry only if this FDE isn't a duplicate of
1591 something we have already seen. */
1594 fde_chunks_need_space ();
1595 fde = fde_unit_alloc ();
1597 fde_chunks.array[fde_chunks.elems++] = fde;
1599 fde->initial_location = init_loc;
1600 fde->address_range = read_encoded_pointer (abfd, &start,
1606 /* Here we intentionally ignore augmentation data
1607 from FDE, because we don't need them. */
1608 if (cie->augmentation[0] == 'z')
1609 start += read_uleb128 (abfd, &start);
1612 fde->data_length = block_end - start;
1617 qsort (fde_chunks.array, fde_chunks.elems,
1618 sizeof (struct fde_unit *), compare_fde_unit);
1622 /* We must parse both .debug_frame section and .eh_frame because
1623 * not all frames must be present in both of these sections. */
1625 dwarf2_build_frame_info (struct objfile *objfile)
1627 int after_debug_frame = 0;
1629 /* If we have .debug_frame then the parser is called with
1630 eh_frame==0 for .debug_frame and eh_frame==2 for .eh_frame,
1631 otherwise it's only called once for .eh_frame with argument
1634 if (dwarf_frame_offset)
1636 parse_frame_info (objfile, dwarf_frame_offset,
1637 dwarf_frame_size, 0 /* = debug_frame */ );
1638 after_debug_frame = 1;
1641 if (dwarf_eh_frame_offset)
1642 parse_frame_info (objfile, dwarf_eh_frame_offset, dwarf_eh_frame_size,
1643 1 /* = eh_frame */ + after_debug_frame);
1646 /* Return the frame address. */
1650 struct context *context;
1651 struct frame_state *fs;
1654 unwind_tmp_obstack_init ();
1656 context = context_alloc ();
1657 fs = frame_state_alloc ();
1659 context->ra = read_pc () + 1;
1661 frame_state_for (context, fs);
1662 update_context (context, fs, 0);
1666 unwind_tmp_obstack_free ();
1671 /* Store the frame address. This function is not used. */
1674 cfi_write_fp (CORE_ADDR val)
1676 struct context *context;
1677 struct frame_state *fs;
1679 unwind_tmp_obstack_init ();
1681 context = context_alloc ();
1682 fs = frame_state_alloc ();
1684 context->ra = read_pc () + 1;
1686 frame_state_for (context, fs);
1688 if (fs->cfa_how == CFA_REG_OFFSET)
1690 val -= fs->cfa_offset;
1691 deprecated_write_register_gen (fs->cfa_reg, (char *) &val);
1694 warning ("Can't write fp.");
1696 unwind_tmp_obstack_free ();
1699 /* Restore the machine to the state it had before the current frame
1702 cfi_pop_frame (struct frame_info *fi)
1704 char *regbuf = alloca (MAX_REGISTER_RAW_SIZE);
1707 for (regnum = 0; regnum < NUM_REGS; regnum++)
1709 get_reg (regbuf, UNWIND_CONTEXT (fi), regnum);
1710 deprecated_write_register_bytes (REGISTER_BYTE (regnum), regbuf,
1711 REGISTER_RAW_SIZE (regnum));
1713 write_register (PC_REGNUM, UNWIND_CONTEXT (fi)->ra);
1715 flush_cached_frames ();
1718 /* Determine the address of the calling function's frame. */
1720 cfi_frame_chain (struct frame_info *fi)
1722 struct context *context;
1723 struct frame_state *fs;
1726 unwind_tmp_obstack_init ();
1728 context = context_alloc ();
1729 fs = frame_state_alloc ();
1730 context_cpy (context, UNWIND_CONTEXT (fi));
1732 /* outermost frame */
1733 if (context->ra == 0)
1735 unwind_tmp_obstack_free ();
1739 frame_state_for (context, fs);
1740 update_context (context, fs, 1);
1743 unwind_tmp_obstack_free ();
1748 /* Sets the pc of the frame. */
1750 cfi_init_frame_pc (int fromleaf, struct frame_info *fi)
1752 if (get_next_frame (fi))
1755 /* FIXME: cagney/2002-12-04: This is straight wrong. It's
1756 assuming that the PC is CORE_ADDR (a host quantity) in size. */
1757 get_reg ((void *)&pc, UNWIND_CONTEXT (get_next_frame (fi)), PC_REGNUM);
1764 /* Initialize unwind context informations of the frame. */
1766 cfi_init_extra_frame_info (int fromleaf, struct frame_info *fi)
1768 struct frame_state *fs;
1770 unwind_tmp_obstack_init ();
1772 fs = frame_state_alloc ();
1773 fi->context = frame_obstack_alloc (sizeof (struct context));
1774 UNWIND_CONTEXT (fi)->reg =
1775 frame_obstack_alloc (sizeof (struct context_reg) * NUM_REGS);
1776 memset (UNWIND_CONTEXT (fi)->reg, 0,
1777 sizeof (struct context_reg) * NUM_REGS);
1781 context_cpy (UNWIND_CONTEXT (fi), UNWIND_CONTEXT (fi->next));
1782 frame_state_for (UNWIND_CONTEXT (fi), fs);
1783 update_context (UNWIND_CONTEXT (fi), fs, 1);
1787 UNWIND_CONTEXT (fi)->ra = get_frame_pc (fi) + 1;
1788 frame_state_for (UNWIND_CONTEXT (fi), fs);
1789 update_context (UNWIND_CONTEXT (fi), fs, 0);
1792 unwind_tmp_obstack_free ();
1795 /* Obtain return address of the frame. */
1797 cfi_get_ra (struct frame_info *fi)
1799 return UNWIND_CONTEXT (fi)->ra;
1802 /* Find register number REGNUM relative to FRAME and put its
1803 (raw) contents in *RAW_BUFFER. Set *OPTIMIZED if the variable
1804 was optimized out (and thus can't be fetched). If the variable
1805 was fetched from memory, set *ADDRP to where it was fetched from,
1806 otherwise it was fetched from a register.
1808 The argument RAW_BUFFER must point to aligned memory. */
1810 cfi_get_saved_register (char *raw_buffer,
1813 struct frame_info *frame,
1814 int regnum, enum lval_type *lval)
1816 if (!target_has_registers)
1817 error ("No registers.");
1819 /* Normal systems don't optimize out things with register numbers. */
1820 if (optimized != NULL)
1823 if (addrp) /* default assumption: not found in memory */
1828 deprecated_read_register_gen (regnum, raw_buffer);
1830 *lval = lval_register;
1832 *addrp = REGISTER_BYTE (regnum);
1836 frame = frame->next;
1837 switch (UNWIND_CONTEXT (frame)->reg[regnum].how)
1839 case REG_CTX_UNSAVED:
1840 deprecated_read_register_gen (regnum, raw_buffer);
1843 if (optimized != NULL)
1846 case REG_CTX_SAVED_OFFSET:
1847 target_read_memory (UNWIND_CONTEXT (frame)->cfa +
1848 UNWIND_CONTEXT (frame)->reg[regnum].loc.offset,
1849 raw_buffer, REGISTER_RAW_SIZE (regnum));
1851 *lval = lval_memory;
1854 UNWIND_CONTEXT (frame)->cfa +
1855 UNWIND_CONTEXT (frame)->reg[regnum].loc.offset;
1857 case REG_CTX_SAVED_REG:
1858 deprecated_read_register_gen (UNWIND_CONTEXT (frame)->reg[regnum].loc.reg,
1861 *lval = lval_register;
1864 REGISTER_BYTE (UNWIND_CONTEXT (frame)->reg[regnum].loc.reg);
1866 case REG_CTX_SAVED_ADDR:
1867 target_read_memory (UNWIND_CONTEXT (frame)->reg[regnum].loc.addr,
1868 raw_buffer, REGISTER_RAW_SIZE (regnum));
1870 *lval = lval_memory;
1872 *addrp = UNWIND_CONTEXT (frame)->reg[regnum].loc.addr;
1875 memcpy (raw_buffer, &UNWIND_CONTEXT (frame)->reg[regnum].loc.addr,
1876 REGISTER_RAW_SIZE (regnum));
1879 if (optimized != NULL)
1883 internal_error (__FILE__, __LINE__,
1884 "cfi_get_saved_register: unknown register rule");
1889 /* Return the register that the function uses for a frame pointer,
1890 plus any necessary offset to be applied to the register before
1891 any frame pointer offsets. */
1893 cfi_virtual_frame_pointer (CORE_ADDR pc, int *frame_reg,
1894 LONGEST * frame_offset)
1896 struct context *context;
1897 struct frame_state *fs;
1899 unwind_tmp_obstack_init ();
1901 context = context_alloc ();
1902 fs = frame_state_alloc ();
1904 context->ra = read_pc () + 1;
1906 frame_state_for (context, fs);
1908 if (fs->cfa_how == CFA_REG_OFFSET)
1910 *frame_reg = fs->cfa_reg;
1911 *frame_offset = fs->cfa_offset;
1914 error ("dwarf cfi error: CFA is not defined as CFA_REG_OFFSET");
1916 unwind_tmp_obstack_free ();