xref: /dragonfly/contrib/gdb-7/gdb/dwarf2-frame.c (revision ef5ccd6c)
15796c8dcSSimon Schubert /* Frame unwinder for frames with DWARF Call Frame Information.
25796c8dcSSimon Schubert 
3*ef5ccd6cSJohn Marino    Copyright (C) 2003-2013 Free Software Foundation, Inc.
45796c8dcSSimon Schubert 
55796c8dcSSimon Schubert    Contributed by Mark Kettenis.
65796c8dcSSimon Schubert 
75796c8dcSSimon Schubert    This file is part of GDB.
85796c8dcSSimon Schubert 
95796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
105796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
115796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
125796c8dcSSimon Schubert    (at your option) any later version.
135796c8dcSSimon Schubert 
145796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
155796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
165796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
175796c8dcSSimon Schubert    GNU General Public License for more details.
185796c8dcSSimon Schubert 
195796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
205796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
215796c8dcSSimon Schubert 
225796c8dcSSimon Schubert #include "defs.h"
235796c8dcSSimon Schubert #include "dwarf2expr.h"
245796c8dcSSimon Schubert #include "dwarf2.h"
255796c8dcSSimon Schubert #include "frame.h"
265796c8dcSSimon Schubert #include "frame-base.h"
275796c8dcSSimon Schubert #include "frame-unwind.h"
285796c8dcSSimon Schubert #include "gdbcore.h"
295796c8dcSSimon Schubert #include "gdbtypes.h"
305796c8dcSSimon Schubert #include "symtab.h"
315796c8dcSSimon Schubert #include "objfiles.h"
325796c8dcSSimon Schubert #include "regcache.h"
335796c8dcSSimon Schubert #include "value.h"
345796c8dcSSimon Schubert 
355796c8dcSSimon Schubert #include "gdb_assert.h"
365796c8dcSSimon Schubert #include "gdb_string.h"
375796c8dcSSimon Schubert 
385796c8dcSSimon Schubert #include "complaints.h"
395796c8dcSSimon Schubert #include "dwarf2-frame.h"
40c50c785cSJohn Marino #include "ax.h"
41c50c785cSJohn Marino #include "dwarf2loc.h"
42c50c785cSJohn Marino #include "exceptions.h"
43a45ae5f8SJohn Marino #include "dwarf2-frame-tailcall.h"
445796c8dcSSimon Schubert 
455796c8dcSSimon Schubert struct comp_unit;
465796c8dcSSimon Schubert 
475796c8dcSSimon Schubert /* Call Frame Information (CFI).  */
485796c8dcSSimon Schubert 
495796c8dcSSimon Schubert /* Common Information Entry (CIE).  */
505796c8dcSSimon Schubert 
515796c8dcSSimon Schubert struct dwarf2_cie
525796c8dcSSimon Schubert {
535796c8dcSSimon Schubert   /* Computation Unit for this CIE.  */
545796c8dcSSimon Schubert   struct comp_unit *unit;
555796c8dcSSimon Schubert 
565796c8dcSSimon Schubert   /* Offset into the .debug_frame section where this CIE was found.
575796c8dcSSimon Schubert      Used to identify this CIE.  */
585796c8dcSSimon Schubert   ULONGEST cie_pointer;
595796c8dcSSimon Schubert 
605796c8dcSSimon Schubert   /* Constant that is factored out of all advance location
615796c8dcSSimon Schubert      instructions.  */
625796c8dcSSimon Schubert   ULONGEST code_alignment_factor;
635796c8dcSSimon Schubert 
645796c8dcSSimon Schubert   /* Constants that is factored out of all offset instructions.  */
655796c8dcSSimon Schubert   LONGEST data_alignment_factor;
665796c8dcSSimon Schubert 
675796c8dcSSimon Schubert   /* Return address column.  */
685796c8dcSSimon Schubert   ULONGEST return_address_register;
695796c8dcSSimon Schubert 
705796c8dcSSimon Schubert   /* Instruction sequence to initialize a register set.  */
71*ef5ccd6cSJohn Marino   const gdb_byte *initial_instructions;
72*ef5ccd6cSJohn Marino   const gdb_byte *end;
735796c8dcSSimon Schubert 
745796c8dcSSimon Schubert   /* Saved augmentation, in case it's needed later.  */
755796c8dcSSimon Schubert   char *augmentation;
765796c8dcSSimon Schubert 
775796c8dcSSimon Schubert   /* Encoding of addresses.  */
785796c8dcSSimon Schubert   gdb_byte encoding;
795796c8dcSSimon Schubert 
805796c8dcSSimon Schubert   /* Target address size in bytes.  */
815796c8dcSSimon Schubert   int addr_size;
825796c8dcSSimon Schubert 
83c50c785cSJohn Marino   /* Target pointer size in bytes.  */
84c50c785cSJohn Marino   int ptr_size;
85c50c785cSJohn Marino 
865796c8dcSSimon Schubert   /* True if a 'z' augmentation existed.  */
875796c8dcSSimon Schubert   unsigned char saw_z_augmentation;
885796c8dcSSimon Schubert 
895796c8dcSSimon Schubert   /* True if an 'S' augmentation existed.  */
905796c8dcSSimon Schubert   unsigned char signal_frame;
915796c8dcSSimon Schubert 
925796c8dcSSimon Schubert   /* The version recorded in the CIE.  */
935796c8dcSSimon Schubert   unsigned char version;
94cf7f2e2dSJohn Marino 
95cf7f2e2dSJohn Marino   /* The segment size.  */
96cf7f2e2dSJohn Marino   unsigned char segment_size;
975796c8dcSSimon Schubert };
985796c8dcSSimon Schubert 
995796c8dcSSimon Schubert struct dwarf2_cie_table
1005796c8dcSSimon Schubert {
1015796c8dcSSimon Schubert   int num_entries;
1025796c8dcSSimon Schubert   struct dwarf2_cie **entries;
1035796c8dcSSimon Schubert };
1045796c8dcSSimon Schubert 
1055796c8dcSSimon Schubert /* Frame Description Entry (FDE).  */
1065796c8dcSSimon Schubert 
1075796c8dcSSimon Schubert struct dwarf2_fde
1085796c8dcSSimon Schubert {
1095796c8dcSSimon Schubert   /* CIE for this FDE.  */
1105796c8dcSSimon Schubert   struct dwarf2_cie *cie;
1115796c8dcSSimon Schubert 
1125796c8dcSSimon Schubert   /* First location associated with this FDE.  */
1135796c8dcSSimon Schubert   CORE_ADDR initial_location;
1145796c8dcSSimon Schubert 
1155796c8dcSSimon Schubert   /* Number of bytes of program instructions described by this FDE.  */
1165796c8dcSSimon Schubert   CORE_ADDR address_range;
1175796c8dcSSimon Schubert 
1185796c8dcSSimon Schubert   /* Instruction sequence.  */
119*ef5ccd6cSJohn Marino   const gdb_byte *instructions;
120*ef5ccd6cSJohn Marino   const gdb_byte *end;
1215796c8dcSSimon Schubert 
1225796c8dcSSimon Schubert   /* True if this FDE is read from a .eh_frame instead of a .debug_frame
1235796c8dcSSimon Schubert      section.  */
1245796c8dcSSimon Schubert   unsigned char eh_frame_p;
1255796c8dcSSimon Schubert };
1265796c8dcSSimon Schubert 
1275796c8dcSSimon Schubert struct dwarf2_fde_table
1285796c8dcSSimon Schubert {
1295796c8dcSSimon Schubert   int num_entries;
1305796c8dcSSimon Schubert   struct dwarf2_fde **entries;
1315796c8dcSSimon Schubert };
1325796c8dcSSimon Schubert 
1335796c8dcSSimon Schubert /* A minimal decoding of DWARF2 compilation units.  We only decode
1345796c8dcSSimon Schubert    what's needed to get to the call frame information.  */
1355796c8dcSSimon Schubert 
1365796c8dcSSimon Schubert struct comp_unit
1375796c8dcSSimon Schubert {
1385796c8dcSSimon Schubert   /* Keep the bfd convenient.  */
1395796c8dcSSimon Schubert   bfd *abfd;
1405796c8dcSSimon Schubert 
1415796c8dcSSimon Schubert   struct objfile *objfile;
1425796c8dcSSimon Schubert 
1435796c8dcSSimon Schubert   /* Pointer to the .debug_frame section loaded into memory.  */
1445796c8dcSSimon Schubert   gdb_byte *dwarf_frame_buffer;
1455796c8dcSSimon Schubert 
1465796c8dcSSimon Schubert   /* Length of the loaded .debug_frame section.  */
1475796c8dcSSimon Schubert   bfd_size_type dwarf_frame_size;
1485796c8dcSSimon Schubert 
1495796c8dcSSimon Schubert   /* Pointer to the .debug_frame section.  */
1505796c8dcSSimon Schubert   asection *dwarf_frame_section;
1515796c8dcSSimon Schubert 
1525796c8dcSSimon Schubert   /* Base for DW_EH_PE_datarel encodings.  */
1535796c8dcSSimon Schubert   bfd_vma dbase;
1545796c8dcSSimon Schubert 
1555796c8dcSSimon Schubert   /* Base for DW_EH_PE_textrel encodings.  */
1565796c8dcSSimon Schubert   bfd_vma tbase;
1575796c8dcSSimon Schubert };
1585796c8dcSSimon Schubert 
159cf7f2e2dSJohn Marino static struct dwarf2_fde *dwarf2_frame_find_fde (CORE_ADDR *pc,
160cf7f2e2dSJohn Marino 						 CORE_ADDR *out_offset);
1615796c8dcSSimon Schubert 
1625796c8dcSSimon Schubert static int dwarf2_frame_adjust_regnum (struct gdbarch *gdbarch, int regnum,
1635796c8dcSSimon Schubert 				       int eh_frame_p);
1645796c8dcSSimon Schubert 
1655796c8dcSSimon Schubert static CORE_ADDR read_encoded_value (struct comp_unit *unit, gdb_byte encoding,
166cf7f2e2dSJohn Marino 				     int ptr_len, const gdb_byte *buf,
1675796c8dcSSimon Schubert 				     unsigned int *bytes_read_ptr,
1685796c8dcSSimon Schubert 				     CORE_ADDR func_base);
1695796c8dcSSimon Schubert 
1705796c8dcSSimon Schubert 
1715796c8dcSSimon Schubert /* Structure describing a frame state.  */
1725796c8dcSSimon Schubert 
1735796c8dcSSimon Schubert struct dwarf2_frame_state
1745796c8dcSSimon Schubert {
1755796c8dcSSimon Schubert   /* Each register save state can be described in terms of a CFA slot,
1765796c8dcSSimon Schubert      another register, or a location expression.  */
1775796c8dcSSimon Schubert   struct dwarf2_frame_state_reg_info
1785796c8dcSSimon Schubert   {
1795796c8dcSSimon Schubert     struct dwarf2_frame_state_reg *reg;
1805796c8dcSSimon Schubert     int num_regs;
1815796c8dcSSimon Schubert 
1825796c8dcSSimon Schubert     LONGEST cfa_offset;
1835796c8dcSSimon Schubert     ULONGEST cfa_reg;
1845796c8dcSSimon Schubert     enum {
1855796c8dcSSimon Schubert       CFA_UNSET,
1865796c8dcSSimon Schubert       CFA_REG_OFFSET,
1875796c8dcSSimon Schubert       CFA_EXP
1885796c8dcSSimon Schubert     } cfa_how;
189cf7f2e2dSJohn Marino     const gdb_byte *cfa_exp;
1905796c8dcSSimon Schubert 
1915796c8dcSSimon Schubert     /* Used to implement DW_CFA_remember_state.  */
1925796c8dcSSimon Schubert     struct dwarf2_frame_state_reg_info *prev;
1935796c8dcSSimon Schubert   } regs;
1945796c8dcSSimon Schubert 
1955796c8dcSSimon Schubert   /* The PC described by the current frame state.  */
1965796c8dcSSimon Schubert   CORE_ADDR pc;
1975796c8dcSSimon Schubert 
1985796c8dcSSimon Schubert   /* Initial register set from the CIE.
1995796c8dcSSimon Schubert      Used to implement DW_CFA_restore.  */
2005796c8dcSSimon Schubert   struct dwarf2_frame_state_reg_info initial;
2015796c8dcSSimon Schubert 
2025796c8dcSSimon Schubert   /* The information we care about from the CIE.  */
2035796c8dcSSimon Schubert   LONGEST data_align;
2045796c8dcSSimon Schubert   ULONGEST code_align;
2055796c8dcSSimon Schubert   ULONGEST retaddr_column;
2065796c8dcSSimon Schubert 
2075796c8dcSSimon Schubert   /* Flags for known producer quirks.  */
2085796c8dcSSimon Schubert 
2095796c8dcSSimon Schubert   /* The ARM compilers, in DWARF2 mode, assume that DW_CFA_def_cfa
2105796c8dcSSimon Schubert      and DW_CFA_def_cfa_offset takes a factored offset.  */
2115796c8dcSSimon Schubert   int armcc_cfa_offsets_sf;
2125796c8dcSSimon Schubert 
2135796c8dcSSimon Schubert   /* The ARM compilers, in DWARF2 or DWARF3 mode, may assume that
2145796c8dcSSimon Schubert      the CFA is defined as REG - OFFSET rather than REG + OFFSET.  */
2155796c8dcSSimon Schubert   int armcc_cfa_offsets_reversed;
2165796c8dcSSimon Schubert };
2175796c8dcSSimon Schubert 
2185796c8dcSSimon Schubert /* Store the length the expression for the CFA in the `cfa_reg' field,
2195796c8dcSSimon Schubert    which is unused in that case.  */
2205796c8dcSSimon Schubert #define cfa_exp_len cfa_reg
2215796c8dcSSimon Schubert 
2225796c8dcSSimon Schubert /* Assert that the register set RS is large enough to store gdbarch_num_regs
2235796c8dcSSimon Schubert    columns.  If necessary, enlarge the register set.  */
2245796c8dcSSimon Schubert 
2255796c8dcSSimon Schubert static void
dwarf2_frame_state_alloc_regs(struct dwarf2_frame_state_reg_info * rs,int num_regs)2265796c8dcSSimon Schubert dwarf2_frame_state_alloc_regs (struct dwarf2_frame_state_reg_info *rs,
2275796c8dcSSimon Schubert 			       int num_regs)
2285796c8dcSSimon Schubert {
2295796c8dcSSimon Schubert   size_t size = sizeof (struct dwarf2_frame_state_reg);
2305796c8dcSSimon Schubert 
2315796c8dcSSimon Schubert   if (num_regs <= rs->num_regs)
2325796c8dcSSimon Schubert     return;
2335796c8dcSSimon Schubert 
2345796c8dcSSimon Schubert   rs->reg = (struct dwarf2_frame_state_reg *)
2355796c8dcSSimon Schubert     xrealloc (rs->reg, num_regs * size);
2365796c8dcSSimon Schubert 
2375796c8dcSSimon Schubert   /* Initialize newly allocated registers.  */
2385796c8dcSSimon Schubert   memset (rs->reg + rs->num_regs, 0, (num_regs - rs->num_regs) * size);
2395796c8dcSSimon Schubert   rs->num_regs = num_regs;
2405796c8dcSSimon Schubert }
2415796c8dcSSimon Schubert 
2425796c8dcSSimon Schubert /* Copy the register columns in register set RS into newly allocated
2435796c8dcSSimon Schubert    memory and return a pointer to this newly created copy.  */
2445796c8dcSSimon Schubert 
2455796c8dcSSimon Schubert static struct dwarf2_frame_state_reg *
dwarf2_frame_state_copy_regs(struct dwarf2_frame_state_reg_info * rs)2465796c8dcSSimon Schubert dwarf2_frame_state_copy_regs (struct dwarf2_frame_state_reg_info *rs)
2475796c8dcSSimon Schubert {
2485796c8dcSSimon Schubert   size_t size = rs->num_regs * sizeof (struct dwarf2_frame_state_reg);
2495796c8dcSSimon Schubert   struct dwarf2_frame_state_reg *reg;
2505796c8dcSSimon Schubert 
2515796c8dcSSimon Schubert   reg = (struct dwarf2_frame_state_reg *) xmalloc (size);
2525796c8dcSSimon Schubert   memcpy (reg, rs->reg, size);
2535796c8dcSSimon Schubert 
2545796c8dcSSimon Schubert   return reg;
2555796c8dcSSimon Schubert }
2565796c8dcSSimon Schubert 
2575796c8dcSSimon Schubert /* Release the memory allocated to register set RS.  */
2585796c8dcSSimon Schubert 
2595796c8dcSSimon Schubert static void
dwarf2_frame_state_free_regs(struct dwarf2_frame_state_reg_info * rs)2605796c8dcSSimon Schubert dwarf2_frame_state_free_regs (struct dwarf2_frame_state_reg_info *rs)
2615796c8dcSSimon Schubert {
2625796c8dcSSimon Schubert   if (rs)
2635796c8dcSSimon Schubert     {
2645796c8dcSSimon Schubert       dwarf2_frame_state_free_regs (rs->prev);
2655796c8dcSSimon Schubert 
2665796c8dcSSimon Schubert       xfree (rs->reg);
2675796c8dcSSimon Schubert       xfree (rs);
2685796c8dcSSimon Schubert     }
2695796c8dcSSimon Schubert }
2705796c8dcSSimon Schubert 
2715796c8dcSSimon Schubert /* Release the memory allocated to the frame state FS.  */
2725796c8dcSSimon Schubert 
2735796c8dcSSimon Schubert static void
dwarf2_frame_state_free(void * p)2745796c8dcSSimon Schubert dwarf2_frame_state_free (void *p)
2755796c8dcSSimon Schubert {
2765796c8dcSSimon Schubert   struct dwarf2_frame_state *fs = p;
2775796c8dcSSimon Schubert 
2785796c8dcSSimon Schubert   dwarf2_frame_state_free_regs (fs->initial.prev);
2795796c8dcSSimon Schubert   dwarf2_frame_state_free_regs (fs->regs.prev);
2805796c8dcSSimon Schubert   xfree (fs->initial.reg);
2815796c8dcSSimon Schubert   xfree (fs->regs.reg);
2825796c8dcSSimon Schubert   xfree (fs);
2835796c8dcSSimon Schubert }
2845796c8dcSSimon Schubert 
2855796c8dcSSimon Schubert 
2865796c8dcSSimon Schubert /* Helper functions for execute_stack_op.  */
2875796c8dcSSimon Schubert 
2885796c8dcSSimon Schubert static CORE_ADDR
read_reg(void * baton,int reg)2895796c8dcSSimon Schubert read_reg (void *baton, int reg)
2905796c8dcSSimon Schubert {
2915796c8dcSSimon Schubert   struct frame_info *this_frame = (struct frame_info *) baton;
2925796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_frame_arch (this_frame);
2935796c8dcSSimon Schubert   int regnum;
2945796c8dcSSimon Schubert   gdb_byte *buf;
2955796c8dcSSimon Schubert 
2965796c8dcSSimon Schubert   regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, reg);
2975796c8dcSSimon Schubert 
2985796c8dcSSimon Schubert   buf = alloca (register_size (gdbarch, regnum));
2995796c8dcSSimon Schubert   get_frame_register (this_frame, regnum, buf);
3005796c8dcSSimon Schubert 
3015796c8dcSSimon Schubert   /* Convert the register to an integer.  This returns a LONGEST
3025796c8dcSSimon Schubert      rather than a CORE_ADDR, but unpack_pointer does the same thing
3035796c8dcSSimon Schubert      under the covers, and this makes more sense for non-pointer
3045796c8dcSSimon Schubert      registers.  Maybe read_reg and the associated interfaces should
3055796c8dcSSimon Schubert      deal with "struct value" instead of CORE_ADDR.  */
3065796c8dcSSimon Schubert   return unpack_long (register_type (gdbarch, regnum), buf);
3075796c8dcSSimon Schubert }
3085796c8dcSSimon Schubert 
3095796c8dcSSimon Schubert static void
read_mem(void * baton,gdb_byte * buf,CORE_ADDR addr,size_t len)3105796c8dcSSimon Schubert read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
3115796c8dcSSimon Schubert {
3125796c8dcSSimon Schubert   read_memory (addr, buf, len);
3135796c8dcSSimon Schubert }
3145796c8dcSSimon Schubert 
3155796c8dcSSimon Schubert /* Execute the required actions for both the DW_CFA_restore and
3165796c8dcSSimon Schubert DW_CFA_restore_extended instructions.  */
3175796c8dcSSimon Schubert static void
dwarf2_restore_rule(struct gdbarch * gdbarch,ULONGEST reg_num,struct dwarf2_frame_state * fs,int eh_frame_p)3185796c8dcSSimon Schubert dwarf2_restore_rule (struct gdbarch *gdbarch, ULONGEST reg_num,
3195796c8dcSSimon Schubert 		     struct dwarf2_frame_state *fs, int eh_frame_p)
3205796c8dcSSimon Schubert {
3215796c8dcSSimon Schubert   ULONGEST reg;
3225796c8dcSSimon Schubert 
3235796c8dcSSimon Schubert   gdb_assert (fs->initial.reg);
3245796c8dcSSimon Schubert   reg = dwarf2_frame_adjust_regnum (gdbarch, reg_num, eh_frame_p);
3255796c8dcSSimon Schubert   dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
3265796c8dcSSimon Schubert 
3275796c8dcSSimon Schubert   /* Check if this register was explicitly initialized in the
3285796c8dcSSimon Schubert   CIE initial instructions.  If not, default the rule to
3295796c8dcSSimon Schubert   UNSPECIFIED.  */
3305796c8dcSSimon Schubert   if (reg < fs->initial.num_regs)
3315796c8dcSSimon Schubert     fs->regs.reg[reg] = fs->initial.reg[reg];
3325796c8dcSSimon Schubert   else
3335796c8dcSSimon Schubert     fs->regs.reg[reg].how = DWARF2_FRAME_REG_UNSPECIFIED;
3345796c8dcSSimon Schubert 
3355796c8dcSSimon Schubert   if (fs->regs.reg[reg].how == DWARF2_FRAME_REG_UNSPECIFIED)
3365796c8dcSSimon Schubert     complaint (&symfile_complaints, _("\
3375796c8dcSSimon Schubert incomplete CFI data; DW_CFA_restore unspecified\n\
3385796c8dcSSimon Schubert register %s (#%d) at %s"),
3395796c8dcSSimon Schubert 		       gdbarch_register_name
3405796c8dcSSimon Schubert 		       (gdbarch, gdbarch_dwarf2_reg_to_regnum (gdbarch, reg)),
3415796c8dcSSimon Schubert 		       gdbarch_dwarf2_reg_to_regnum (gdbarch, reg),
3425796c8dcSSimon Schubert 		       paddress (gdbarch, fs->pc));
3435796c8dcSSimon Schubert }
3445796c8dcSSimon Schubert 
345a45ae5f8SJohn Marino /* Virtual method table for execute_stack_op below.  */
346a45ae5f8SJohn Marino 
347a45ae5f8SJohn Marino static const struct dwarf_expr_context_funcs dwarf2_frame_ctx_funcs =
348a45ae5f8SJohn Marino {
349a45ae5f8SJohn Marino   read_reg,
350a45ae5f8SJohn Marino   read_mem,
351a45ae5f8SJohn Marino   ctx_no_get_frame_base,
352a45ae5f8SJohn Marino   ctx_no_get_frame_cfa,
353a45ae5f8SJohn Marino   ctx_no_get_frame_pc,
354a45ae5f8SJohn Marino   ctx_no_get_tls_address,
355a45ae5f8SJohn Marino   ctx_no_dwarf_call,
356a45ae5f8SJohn Marino   ctx_no_get_base_type,
357*ef5ccd6cSJohn Marino   ctx_no_push_dwarf_reg_entry_value,
358*ef5ccd6cSJohn Marino   ctx_no_get_addr_index
359a45ae5f8SJohn Marino };
360a45ae5f8SJohn Marino 
3615796c8dcSSimon Schubert static CORE_ADDR
execute_stack_op(const gdb_byte * exp,ULONGEST len,int addr_size,CORE_ADDR offset,struct frame_info * this_frame,CORE_ADDR initial,int initial_in_stack_memory)362cf7f2e2dSJohn Marino execute_stack_op (const gdb_byte *exp, ULONGEST len, int addr_size,
363cf7f2e2dSJohn Marino 		  CORE_ADDR offset, struct frame_info *this_frame,
364cf7f2e2dSJohn Marino 		  CORE_ADDR initial, int initial_in_stack_memory)
3655796c8dcSSimon Schubert {
3665796c8dcSSimon Schubert   struct dwarf_expr_context *ctx;
3675796c8dcSSimon Schubert   CORE_ADDR result;
3685796c8dcSSimon Schubert   struct cleanup *old_chain;
3695796c8dcSSimon Schubert 
3705796c8dcSSimon Schubert   ctx = new_dwarf_expr_context ();
3715796c8dcSSimon Schubert   old_chain = make_cleanup_free_dwarf_expr_context (ctx);
372a45ae5f8SJohn Marino   make_cleanup_value_free_to_mark (value_mark ());
3735796c8dcSSimon Schubert 
3745796c8dcSSimon Schubert   ctx->gdbarch = get_frame_arch (this_frame);
3755796c8dcSSimon Schubert   ctx->addr_size = addr_size;
376a45ae5f8SJohn Marino   ctx->ref_addr_size = -1;
377cf7f2e2dSJohn Marino   ctx->offset = offset;
3785796c8dcSSimon Schubert   ctx->baton = this_frame;
379a45ae5f8SJohn Marino   ctx->funcs = &dwarf2_frame_ctx_funcs;
3805796c8dcSSimon Schubert 
381a45ae5f8SJohn Marino   dwarf_expr_push_address (ctx, initial, initial_in_stack_memory);
3825796c8dcSSimon Schubert   dwarf_expr_eval (ctx, exp, len);
3835796c8dcSSimon Schubert 
384cf7f2e2dSJohn Marino   if (ctx->location == DWARF_VALUE_MEMORY)
385cf7f2e2dSJohn Marino     result = dwarf_expr_fetch_address (ctx, 0);
386cf7f2e2dSJohn Marino   else if (ctx->location == DWARF_VALUE_REGISTER)
387a45ae5f8SJohn Marino     result = read_reg (this_frame, value_as_long (dwarf_expr_fetch (ctx, 0)));
388cf7f2e2dSJohn Marino   else
3895796c8dcSSimon Schubert     {
3905796c8dcSSimon Schubert       /* This is actually invalid DWARF, but if we ever do run across
3915796c8dcSSimon Schubert 	 it somehow, we might as well support it.  So, instead, report
3925796c8dcSSimon Schubert 	 it as unimplemented.  */
393c50c785cSJohn Marino       error (_("\
394c50c785cSJohn Marino Not implemented: computing unwound register using explicit value operator"));
3955796c8dcSSimon Schubert     }
3965796c8dcSSimon Schubert 
3975796c8dcSSimon Schubert   do_cleanups (old_chain);
3985796c8dcSSimon Schubert 
3995796c8dcSSimon Schubert   return result;
4005796c8dcSSimon Schubert }
4015796c8dcSSimon Schubert 
4025796c8dcSSimon Schubert 
403a45ae5f8SJohn Marino /* Execute FDE program from INSN_PTR possibly up to INSN_END or up to inferior
404a45ae5f8SJohn Marino    PC.  Modify FS state accordingly.  Return current INSN_PTR where the
405a45ae5f8SJohn Marino    execution has stopped, one can resume it on the next call.  */
406a45ae5f8SJohn Marino 
407a45ae5f8SJohn Marino static const gdb_byte *
execute_cfa_program(struct dwarf2_fde * fde,const gdb_byte * insn_ptr,const gdb_byte * insn_end,struct gdbarch * gdbarch,CORE_ADDR pc,struct dwarf2_frame_state * fs)408cf7f2e2dSJohn Marino execute_cfa_program (struct dwarf2_fde *fde, const gdb_byte *insn_ptr,
409c50c785cSJohn Marino 		     const gdb_byte *insn_end, struct gdbarch *gdbarch,
410c50c785cSJohn Marino 		     CORE_ADDR pc, struct dwarf2_frame_state *fs)
4115796c8dcSSimon Schubert {
4125796c8dcSSimon Schubert   int eh_frame_p = fde->eh_frame_p;
4135796c8dcSSimon Schubert   int bytes_read;
4145796c8dcSSimon Schubert   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4155796c8dcSSimon Schubert 
4165796c8dcSSimon Schubert   while (insn_ptr < insn_end && fs->pc <= pc)
4175796c8dcSSimon Schubert     {
4185796c8dcSSimon Schubert       gdb_byte insn = *insn_ptr++;
419*ef5ccd6cSJohn Marino       uint64_t utmp, reg;
420*ef5ccd6cSJohn Marino       int64_t offset;
4215796c8dcSSimon Schubert 
4225796c8dcSSimon Schubert       if ((insn & 0xc0) == DW_CFA_advance_loc)
4235796c8dcSSimon Schubert 	fs->pc += (insn & 0x3f) * fs->code_align;
4245796c8dcSSimon Schubert       else if ((insn & 0xc0) == DW_CFA_offset)
4255796c8dcSSimon Schubert 	{
4265796c8dcSSimon Schubert 	  reg = insn & 0x3f;
4275796c8dcSSimon Schubert 	  reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
428*ef5ccd6cSJohn Marino 	  insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
4295796c8dcSSimon Schubert 	  offset = utmp * fs->data_align;
4305796c8dcSSimon Schubert 	  dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
4315796c8dcSSimon Schubert 	  fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
4325796c8dcSSimon Schubert 	  fs->regs.reg[reg].loc.offset = offset;
4335796c8dcSSimon Schubert 	}
4345796c8dcSSimon Schubert       else if ((insn & 0xc0) == DW_CFA_restore)
4355796c8dcSSimon Schubert 	{
4365796c8dcSSimon Schubert 	  reg = insn & 0x3f;
4375796c8dcSSimon Schubert 	  dwarf2_restore_rule (gdbarch, reg, fs, eh_frame_p);
4385796c8dcSSimon Schubert 	}
4395796c8dcSSimon Schubert       else
4405796c8dcSSimon Schubert 	{
4415796c8dcSSimon Schubert 	  switch (insn)
4425796c8dcSSimon Schubert 	    {
4435796c8dcSSimon Schubert 	    case DW_CFA_set_loc:
4445796c8dcSSimon Schubert 	      fs->pc = read_encoded_value (fde->cie->unit, fde->cie->encoding,
445c50c785cSJohn Marino 					   fde->cie->ptr_size, insn_ptr,
4465796c8dcSSimon Schubert 					   &bytes_read, fde->initial_location);
4475796c8dcSSimon Schubert 	      /* Apply the objfile offset for relocatable objects.  */
4485796c8dcSSimon Schubert 	      fs->pc += ANOFFSET (fde->cie->unit->objfile->section_offsets,
4495796c8dcSSimon Schubert 				  SECT_OFF_TEXT (fde->cie->unit->objfile));
4505796c8dcSSimon Schubert 	      insn_ptr += bytes_read;
4515796c8dcSSimon Schubert 	      break;
4525796c8dcSSimon Schubert 
4535796c8dcSSimon Schubert 	    case DW_CFA_advance_loc1:
4545796c8dcSSimon Schubert 	      utmp = extract_unsigned_integer (insn_ptr, 1, byte_order);
4555796c8dcSSimon Schubert 	      fs->pc += utmp * fs->code_align;
4565796c8dcSSimon Schubert 	      insn_ptr++;
4575796c8dcSSimon Schubert 	      break;
4585796c8dcSSimon Schubert 	    case DW_CFA_advance_loc2:
4595796c8dcSSimon Schubert 	      utmp = extract_unsigned_integer (insn_ptr, 2, byte_order);
4605796c8dcSSimon Schubert 	      fs->pc += utmp * fs->code_align;
4615796c8dcSSimon Schubert 	      insn_ptr += 2;
4625796c8dcSSimon Schubert 	      break;
4635796c8dcSSimon Schubert 	    case DW_CFA_advance_loc4:
4645796c8dcSSimon Schubert 	      utmp = extract_unsigned_integer (insn_ptr, 4, byte_order);
4655796c8dcSSimon Schubert 	      fs->pc += utmp * fs->code_align;
4665796c8dcSSimon Schubert 	      insn_ptr += 4;
4675796c8dcSSimon Schubert 	      break;
4685796c8dcSSimon Schubert 
4695796c8dcSSimon Schubert 	    case DW_CFA_offset_extended:
470*ef5ccd6cSJohn Marino 	      insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
4715796c8dcSSimon Schubert 	      reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
472*ef5ccd6cSJohn Marino 	      insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
4735796c8dcSSimon Schubert 	      offset = utmp * fs->data_align;
4745796c8dcSSimon Schubert 	      dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
4755796c8dcSSimon Schubert 	      fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
4765796c8dcSSimon Schubert 	      fs->regs.reg[reg].loc.offset = offset;
4775796c8dcSSimon Schubert 	      break;
4785796c8dcSSimon Schubert 
4795796c8dcSSimon Schubert 	    case DW_CFA_restore_extended:
480*ef5ccd6cSJohn Marino 	      insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
4815796c8dcSSimon Schubert 	      dwarf2_restore_rule (gdbarch, reg, fs, eh_frame_p);
4825796c8dcSSimon Schubert 	      break;
4835796c8dcSSimon Schubert 
4845796c8dcSSimon Schubert 	    case DW_CFA_undefined:
485*ef5ccd6cSJohn Marino 	      insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
4865796c8dcSSimon Schubert 	      reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
4875796c8dcSSimon Schubert 	      dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
4885796c8dcSSimon Schubert 	      fs->regs.reg[reg].how = DWARF2_FRAME_REG_UNDEFINED;
4895796c8dcSSimon Schubert 	      break;
4905796c8dcSSimon Schubert 
4915796c8dcSSimon Schubert 	    case DW_CFA_same_value:
492*ef5ccd6cSJohn Marino 	      insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
4935796c8dcSSimon Schubert 	      reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
4945796c8dcSSimon Schubert 	      dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
4955796c8dcSSimon Schubert 	      fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAME_VALUE;
4965796c8dcSSimon Schubert 	      break;
4975796c8dcSSimon Schubert 
4985796c8dcSSimon Schubert 	    case DW_CFA_register:
499*ef5ccd6cSJohn Marino 	      insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
5005796c8dcSSimon Schubert 	      reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
501*ef5ccd6cSJohn Marino 	      insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
5025796c8dcSSimon Schubert 	      utmp = dwarf2_frame_adjust_regnum (gdbarch, utmp, eh_frame_p);
5035796c8dcSSimon Schubert 	      dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
5045796c8dcSSimon Schubert 	      fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_REG;
5055796c8dcSSimon Schubert 	      fs->regs.reg[reg].loc.reg = utmp;
5065796c8dcSSimon Schubert 	      break;
5075796c8dcSSimon Schubert 
5085796c8dcSSimon Schubert 	    case DW_CFA_remember_state:
5095796c8dcSSimon Schubert 	      {
5105796c8dcSSimon Schubert 		struct dwarf2_frame_state_reg_info *new_rs;
5115796c8dcSSimon Schubert 
5125796c8dcSSimon Schubert 		new_rs = XMALLOC (struct dwarf2_frame_state_reg_info);
5135796c8dcSSimon Schubert 		*new_rs = fs->regs;
5145796c8dcSSimon Schubert 		fs->regs.reg = dwarf2_frame_state_copy_regs (&fs->regs);
5155796c8dcSSimon Schubert 		fs->regs.prev = new_rs;
5165796c8dcSSimon Schubert 	      }
5175796c8dcSSimon Schubert 	      break;
5185796c8dcSSimon Schubert 
5195796c8dcSSimon Schubert 	    case DW_CFA_restore_state:
5205796c8dcSSimon Schubert 	      {
5215796c8dcSSimon Schubert 		struct dwarf2_frame_state_reg_info *old_rs = fs->regs.prev;
5225796c8dcSSimon Schubert 
5235796c8dcSSimon Schubert 		if (old_rs == NULL)
5245796c8dcSSimon Schubert 		  {
5255796c8dcSSimon Schubert 		    complaint (&symfile_complaints, _("\
5265796c8dcSSimon Schubert bad CFI data; mismatched DW_CFA_restore_state at %s"),
5275796c8dcSSimon Schubert 			       paddress (gdbarch, fs->pc));
5285796c8dcSSimon Schubert 		  }
5295796c8dcSSimon Schubert 		else
5305796c8dcSSimon Schubert 		  {
5315796c8dcSSimon Schubert 		    xfree (fs->regs.reg);
5325796c8dcSSimon Schubert 		    fs->regs = *old_rs;
5335796c8dcSSimon Schubert 		    xfree (old_rs);
5345796c8dcSSimon Schubert 		  }
5355796c8dcSSimon Schubert 	      }
5365796c8dcSSimon Schubert 	      break;
5375796c8dcSSimon Schubert 
5385796c8dcSSimon Schubert 	    case DW_CFA_def_cfa:
539*ef5ccd6cSJohn Marino 	      insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
540*ef5ccd6cSJohn Marino 	      fs->regs.cfa_reg = reg;
541*ef5ccd6cSJohn Marino 	      insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
5425796c8dcSSimon Schubert 
5435796c8dcSSimon Schubert 	      if (fs->armcc_cfa_offsets_sf)
5445796c8dcSSimon Schubert 		utmp *= fs->data_align;
5455796c8dcSSimon Schubert 
5465796c8dcSSimon Schubert 	      fs->regs.cfa_offset = utmp;
5475796c8dcSSimon Schubert 	      fs->regs.cfa_how = CFA_REG_OFFSET;
5485796c8dcSSimon Schubert 	      break;
5495796c8dcSSimon Schubert 
5505796c8dcSSimon Schubert 	    case DW_CFA_def_cfa_register:
551*ef5ccd6cSJohn Marino 	      insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
552*ef5ccd6cSJohn Marino 	      fs->regs.cfa_reg = dwarf2_frame_adjust_regnum (gdbarch, reg,
5535796c8dcSSimon Schubert                                                              eh_frame_p);
5545796c8dcSSimon Schubert 	      fs->regs.cfa_how = CFA_REG_OFFSET;
5555796c8dcSSimon Schubert 	      break;
5565796c8dcSSimon Schubert 
5575796c8dcSSimon Schubert 	    case DW_CFA_def_cfa_offset:
558*ef5ccd6cSJohn Marino 	      insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
5595796c8dcSSimon Schubert 
5605796c8dcSSimon Schubert 	      if (fs->armcc_cfa_offsets_sf)
5615796c8dcSSimon Schubert 		utmp *= fs->data_align;
5625796c8dcSSimon Schubert 
5635796c8dcSSimon Schubert 	      fs->regs.cfa_offset = utmp;
5645796c8dcSSimon Schubert 	      /* cfa_how deliberately not set.  */
5655796c8dcSSimon Schubert 	      break;
5665796c8dcSSimon Schubert 
5675796c8dcSSimon Schubert 	    case DW_CFA_nop:
5685796c8dcSSimon Schubert 	      break;
5695796c8dcSSimon Schubert 
5705796c8dcSSimon Schubert 	    case DW_CFA_def_cfa_expression:
571*ef5ccd6cSJohn Marino 	      insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
572*ef5ccd6cSJohn Marino 	      fs->regs.cfa_exp_len = utmp;
5735796c8dcSSimon Schubert 	      fs->regs.cfa_exp = insn_ptr;
5745796c8dcSSimon Schubert 	      fs->regs.cfa_how = CFA_EXP;
5755796c8dcSSimon Schubert 	      insn_ptr += fs->regs.cfa_exp_len;
5765796c8dcSSimon Schubert 	      break;
5775796c8dcSSimon Schubert 
5785796c8dcSSimon Schubert 	    case DW_CFA_expression:
579*ef5ccd6cSJohn Marino 	      insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
5805796c8dcSSimon Schubert 	      reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
5815796c8dcSSimon Schubert 	      dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
582*ef5ccd6cSJohn Marino 	      insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
5835796c8dcSSimon Schubert 	      fs->regs.reg[reg].loc.exp = insn_ptr;
5845796c8dcSSimon Schubert 	      fs->regs.reg[reg].exp_len = utmp;
5855796c8dcSSimon Schubert 	      fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_EXP;
5865796c8dcSSimon Schubert 	      insn_ptr += utmp;
5875796c8dcSSimon Schubert 	      break;
5885796c8dcSSimon Schubert 
5895796c8dcSSimon Schubert 	    case DW_CFA_offset_extended_sf:
590*ef5ccd6cSJohn Marino 	      insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
5915796c8dcSSimon Schubert 	      reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
592*ef5ccd6cSJohn Marino 	      insn_ptr = safe_read_sleb128 (insn_ptr, insn_end, &offset);
5935796c8dcSSimon Schubert 	      offset *= fs->data_align;
5945796c8dcSSimon Schubert 	      dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
5955796c8dcSSimon Schubert 	      fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
5965796c8dcSSimon Schubert 	      fs->regs.reg[reg].loc.offset = offset;
5975796c8dcSSimon Schubert 	      break;
5985796c8dcSSimon Schubert 
5995796c8dcSSimon Schubert 	    case DW_CFA_val_offset:
600*ef5ccd6cSJohn Marino 	      insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
6015796c8dcSSimon Schubert 	      dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
602*ef5ccd6cSJohn Marino 	      insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
6035796c8dcSSimon Schubert 	      offset = utmp * fs->data_align;
6045796c8dcSSimon Schubert 	      fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_VAL_OFFSET;
6055796c8dcSSimon Schubert 	      fs->regs.reg[reg].loc.offset = offset;
6065796c8dcSSimon Schubert 	      break;
6075796c8dcSSimon Schubert 
6085796c8dcSSimon Schubert 	    case DW_CFA_val_offset_sf:
609*ef5ccd6cSJohn Marino 	      insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
6105796c8dcSSimon Schubert 	      dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
611*ef5ccd6cSJohn Marino 	      insn_ptr = safe_read_sleb128 (insn_ptr, insn_end, &offset);
6125796c8dcSSimon Schubert 	      offset *= fs->data_align;
6135796c8dcSSimon Schubert 	      fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_VAL_OFFSET;
6145796c8dcSSimon Schubert 	      fs->regs.reg[reg].loc.offset = offset;
6155796c8dcSSimon Schubert 	      break;
6165796c8dcSSimon Schubert 
6175796c8dcSSimon Schubert 	    case DW_CFA_val_expression:
618*ef5ccd6cSJohn Marino 	      insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
6195796c8dcSSimon Schubert 	      dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
620*ef5ccd6cSJohn Marino 	      insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
6215796c8dcSSimon Schubert 	      fs->regs.reg[reg].loc.exp = insn_ptr;
6225796c8dcSSimon Schubert 	      fs->regs.reg[reg].exp_len = utmp;
6235796c8dcSSimon Schubert 	      fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_VAL_EXP;
6245796c8dcSSimon Schubert 	      insn_ptr += utmp;
6255796c8dcSSimon Schubert 	      break;
6265796c8dcSSimon Schubert 
6275796c8dcSSimon Schubert 	    case DW_CFA_def_cfa_sf:
628*ef5ccd6cSJohn Marino 	      insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
629*ef5ccd6cSJohn Marino 	      fs->regs.cfa_reg = dwarf2_frame_adjust_regnum (gdbarch, reg,
6305796c8dcSSimon Schubert                                                              eh_frame_p);
631*ef5ccd6cSJohn Marino 	      insn_ptr = safe_read_sleb128 (insn_ptr, insn_end, &offset);
6325796c8dcSSimon Schubert 	      fs->regs.cfa_offset = offset * fs->data_align;
6335796c8dcSSimon Schubert 	      fs->regs.cfa_how = CFA_REG_OFFSET;
6345796c8dcSSimon Schubert 	      break;
6355796c8dcSSimon Schubert 
6365796c8dcSSimon Schubert 	    case DW_CFA_def_cfa_offset_sf:
637*ef5ccd6cSJohn Marino 	      insn_ptr = safe_read_sleb128 (insn_ptr, insn_end, &offset);
6385796c8dcSSimon Schubert 	      fs->regs.cfa_offset = offset * fs->data_align;
6395796c8dcSSimon Schubert 	      /* cfa_how deliberately not set.  */
6405796c8dcSSimon Schubert 	      break;
6415796c8dcSSimon Schubert 
6425796c8dcSSimon Schubert 	    case DW_CFA_GNU_window_save:
6435796c8dcSSimon Schubert 	      /* This is SPARC-specific code, and contains hard-coded
6445796c8dcSSimon Schubert 		 constants for the register numbering scheme used by
6455796c8dcSSimon Schubert 		 GCC.  Rather than having a architecture-specific
6465796c8dcSSimon Schubert 		 operation that's only ever used by a single
6475796c8dcSSimon Schubert 		 architecture, we provide the implementation here.
6485796c8dcSSimon Schubert 		 Incidentally that's what GCC does too in its
6495796c8dcSSimon Schubert 		 unwinder.  */
6505796c8dcSSimon Schubert 	      {
6515796c8dcSSimon Schubert 		int size = register_size (gdbarch, 0);
652cf7f2e2dSJohn Marino 
6535796c8dcSSimon Schubert 		dwarf2_frame_state_alloc_regs (&fs->regs, 32);
6545796c8dcSSimon Schubert 		for (reg = 8; reg < 16; reg++)
6555796c8dcSSimon Schubert 		  {
6565796c8dcSSimon Schubert 		    fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_REG;
6575796c8dcSSimon Schubert 		    fs->regs.reg[reg].loc.reg = reg + 16;
6585796c8dcSSimon Schubert 		  }
6595796c8dcSSimon Schubert 		for (reg = 16; reg < 32; reg++)
6605796c8dcSSimon Schubert 		  {
6615796c8dcSSimon Schubert 		    fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
6625796c8dcSSimon Schubert 		    fs->regs.reg[reg].loc.offset = (reg - 16) * size;
6635796c8dcSSimon Schubert 		  }
6645796c8dcSSimon Schubert 	      }
6655796c8dcSSimon Schubert 	      break;
6665796c8dcSSimon Schubert 
6675796c8dcSSimon Schubert 	    case DW_CFA_GNU_args_size:
6685796c8dcSSimon Schubert 	      /* Ignored.  */
669*ef5ccd6cSJohn Marino 	      insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
6705796c8dcSSimon Schubert 	      break;
6715796c8dcSSimon Schubert 
6725796c8dcSSimon Schubert 	    case DW_CFA_GNU_negative_offset_extended:
673*ef5ccd6cSJohn Marino 	      insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
6745796c8dcSSimon Schubert 	      reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
675*ef5ccd6cSJohn Marino 	      insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &offset);
6765796c8dcSSimon Schubert 	      offset *= fs->data_align;
6775796c8dcSSimon Schubert 	      dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
6785796c8dcSSimon Schubert 	      fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
6795796c8dcSSimon Schubert 	      fs->regs.reg[reg].loc.offset = -offset;
6805796c8dcSSimon Schubert 	      break;
6815796c8dcSSimon Schubert 
6825796c8dcSSimon Schubert 	    default:
683c50c785cSJohn Marino 	      internal_error (__FILE__, __LINE__,
684c50c785cSJohn Marino 			      _("Unknown CFI encountered."));
6855796c8dcSSimon Schubert 	    }
6865796c8dcSSimon Schubert 	}
6875796c8dcSSimon Schubert     }
6885796c8dcSSimon Schubert 
689a45ae5f8SJohn Marino   if (fs->initial.reg == NULL)
690a45ae5f8SJohn Marino     {
6915796c8dcSSimon Schubert       /* Don't allow remember/restore between CIE and FDE programs.  */
6925796c8dcSSimon Schubert       dwarf2_frame_state_free_regs (fs->regs.prev);
6935796c8dcSSimon Schubert       fs->regs.prev = NULL;
6945796c8dcSSimon Schubert     }
695a45ae5f8SJohn Marino 
696a45ae5f8SJohn Marino   return insn_ptr;
697a45ae5f8SJohn Marino }
6985796c8dcSSimon Schubert 
6995796c8dcSSimon Schubert 
7005796c8dcSSimon Schubert /* Architecture-specific operations.  */
7015796c8dcSSimon Schubert 
7025796c8dcSSimon Schubert /* Per-architecture data key.  */
7035796c8dcSSimon Schubert static struct gdbarch_data *dwarf2_frame_data;
7045796c8dcSSimon Schubert 
7055796c8dcSSimon Schubert struct dwarf2_frame_ops
7065796c8dcSSimon Schubert {
7075796c8dcSSimon Schubert   /* Pre-initialize the register state REG for register REGNUM.  */
7085796c8dcSSimon Schubert   void (*init_reg) (struct gdbarch *, int, struct dwarf2_frame_state_reg *,
7095796c8dcSSimon Schubert 		    struct frame_info *);
7105796c8dcSSimon Schubert 
7115796c8dcSSimon Schubert   /* Check whether the THIS_FRAME is a signal trampoline.  */
7125796c8dcSSimon Schubert   int (*signal_frame_p) (struct gdbarch *, struct frame_info *);
7135796c8dcSSimon Schubert 
7145796c8dcSSimon Schubert   /* Convert .eh_frame register number to DWARF register number, or
7155796c8dcSSimon Schubert      adjust .debug_frame register number.  */
7165796c8dcSSimon Schubert   int (*adjust_regnum) (struct gdbarch *, int, int);
7175796c8dcSSimon Schubert };
7185796c8dcSSimon Schubert 
7195796c8dcSSimon Schubert /* Default architecture-specific register state initialization
7205796c8dcSSimon Schubert    function.  */
7215796c8dcSSimon Schubert 
7225796c8dcSSimon Schubert static void
dwarf2_frame_default_init_reg(struct gdbarch * gdbarch,int regnum,struct dwarf2_frame_state_reg * reg,struct frame_info * this_frame)7235796c8dcSSimon Schubert dwarf2_frame_default_init_reg (struct gdbarch *gdbarch, int regnum,
7245796c8dcSSimon Schubert 			       struct dwarf2_frame_state_reg *reg,
7255796c8dcSSimon Schubert 			       struct frame_info *this_frame)
7265796c8dcSSimon Schubert {
7275796c8dcSSimon Schubert   /* If we have a register that acts as a program counter, mark it as
7285796c8dcSSimon Schubert      a destination for the return address.  If we have a register that
7295796c8dcSSimon Schubert      serves as the stack pointer, arrange for it to be filled with the
7305796c8dcSSimon Schubert      call frame address (CFA).  The other registers are marked as
7315796c8dcSSimon Schubert      unspecified.
7325796c8dcSSimon Schubert 
7335796c8dcSSimon Schubert      We copy the return address to the program counter, since many
7345796c8dcSSimon Schubert      parts in GDB assume that it is possible to get the return address
7355796c8dcSSimon Schubert      by unwinding the program counter register.  However, on ISA's
7365796c8dcSSimon Schubert      with a dedicated return address register, the CFI usually only
7375796c8dcSSimon Schubert      contains information to unwind that return address register.
7385796c8dcSSimon Schubert 
7395796c8dcSSimon Schubert      The reason we're treating the stack pointer special here is
7405796c8dcSSimon Schubert      because in many cases GCC doesn't emit CFI for the stack pointer
7415796c8dcSSimon Schubert      and implicitly assumes that it is equal to the CFA.  This makes
7425796c8dcSSimon Schubert      some sense since the DWARF specification (version 3, draft 8,
7435796c8dcSSimon Schubert      p. 102) says that:
7445796c8dcSSimon Schubert 
7455796c8dcSSimon Schubert      "Typically, the CFA is defined to be the value of the stack
7465796c8dcSSimon Schubert      pointer at the call site in the previous frame (which may be
7475796c8dcSSimon Schubert      different from its value on entry to the current frame)."
7485796c8dcSSimon Schubert 
7495796c8dcSSimon Schubert      However, this isn't true for all platforms supported by GCC
7505796c8dcSSimon Schubert      (e.g. IBM S/390 and zSeries).  Those architectures should provide
7515796c8dcSSimon Schubert      their own architecture-specific initialization function.  */
7525796c8dcSSimon Schubert 
7535796c8dcSSimon Schubert   if (regnum == gdbarch_pc_regnum (gdbarch))
7545796c8dcSSimon Schubert     reg->how = DWARF2_FRAME_REG_RA;
7555796c8dcSSimon Schubert   else if (regnum == gdbarch_sp_regnum (gdbarch))
7565796c8dcSSimon Schubert     reg->how = DWARF2_FRAME_REG_CFA;
7575796c8dcSSimon Schubert }
7585796c8dcSSimon Schubert 
7595796c8dcSSimon Schubert /* Return a default for the architecture-specific operations.  */
7605796c8dcSSimon Schubert 
7615796c8dcSSimon Schubert static void *
dwarf2_frame_init(struct obstack * obstack)7625796c8dcSSimon Schubert dwarf2_frame_init (struct obstack *obstack)
7635796c8dcSSimon Schubert {
7645796c8dcSSimon Schubert   struct dwarf2_frame_ops *ops;
7655796c8dcSSimon Schubert 
7665796c8dcSSimon Schubert   ops = OBSTACK_ZALLOC (obstack, struct dwarf2_frame_ops);
7675796c8dcSSimon Schubert   ops->init_reg = dwarf2_frame_default_init_reg;
7685796c8dcSSimon Schubert   return ops;
7695796c8dcSSimon Schubert }
7705796c8dcSSimon Schubert 
7715796c8dcSSimon Schubert /* Set the architecture-specific register state initialization
7725796c8dcSSimon Schubert    function for GDBARCH to INIT_REG.  */
7735796c8dcSSimon Schubert 
7745796c8dcSSimon Schubert void
dwarf2_frame_set_init_reg(struct gdbarch * gdbarch,void (* init_reg)(struct gdbarch *,int,struct dwarf2_frame_state_reg *,struct frame_info *))7755796c8dcSSimon Schubert dwarf2_frame_set_init_reg (struct gdbarch *gdbarch,
7765796c8dcSSimon Schubert 			   void (*init_reg) (struct gdbarch *, int,
7775796c8dcSSimon Schubert 					     struct dwarf2_frame_state_reg *,
7785796c8dcSSimon Schubert 					     struct frame_info *))
7795796c8dcSSimon Schubert {
7805796c8dcSSimon Schubert   struct dwarf2_frame_ops *ops = gdbarch_data (gdbarch, dwarf2_frame_data);
7815796c8dcSSimon Schubert 
7825796c8dcSSimon Schubert   ops->init_reg = init_reg;
7835796c8dcSSimon Schubert }
7845796c8dcSSimon Schubert 
7855796c8dcSSimon Schubert /* Pre-initialize the register state REG for register REGNUM.  */
7865796c8dcSSimon Schubert 
7875796c8dcSSimon Schubert static void
dwarf2_frame_init_reg(struct gdbarch * gdbarch,int regnum,struct dwarf2_frame_state_reg * reg,struct frame_info * this_frame)7885796c8dcSSimon Schubert dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
7895796c8dcSSimon Schubert 		       struct dwarf2_frame_state_reg *reg,
7905796c8dcSSimon Schubert 		       struct frame_info *this_frame)
7915796c8dcSSimon Schubert {
7925796c8dcSSimon Schubert   struct dwarf2_frame_ops *ops = gdbarch_data (gdbarch, dwarf2_frame_data);
7935796c8dcSSimon Schubert 
7945796c8dcSSimon Schubert   ops->init_reg (gdbarch, regnum, reg, this_frame);
7955796c8dcSSimon Schubert }
7965796c8dcSSimon Schubert 
7975796c8dcSSimon Schubert /* Set the architecture-specific signal trampoline recognition
7985796c8dcSSimon Schubert    function for GDBARCH to SIGNAL_FRAME_P.  */
7995796c8dcSSimon Schubert 
8005796c8dcSSimon Schubert void
dwarf2_frame_set_signal_frame_p(struct gdbarch * gdbarch,int (* signal_frame_p)(struct gdbarch *,struct frame_info *))8015796c8dcSSimon Schubert dwarf2_frame_set_signal_frame_p (struct gdbarch *gdbarch,
8025796c8dcSSimon Schubert 				 int (*signal_frame_p) (struct gdbarch *,
8035796c8dcSSimon Schubert 							struct frame_info *))
8045796c8dcSSimon Schubert {
8055796c8dcSSimon Schubert   struct dwarf2_frame_ops *ops = gdbarch_data (gdbarch, dwarf2_frame_data);
8065796c8dcSSimon Schubert 
8075796c8dcSSimon Schubert   ops->signal_frame_p = signal_frame_p;
8085796c8dcSSimon Schubert }
8095796c8dcSSimon Schubert 
8105796c8dcSSimon Schubert /* Query the architecture-specific signal frame recognizer for
8115796c8dcSSimon Schubert    THIS_FRAME.  */
8125796c8dcSSimon Schubert 
8135796c8dcSSimon Schubert static int
dwarf2_frame_signal_frame_p(struct gdbarch * gdbarch,struct frame_info * this_frame)8145796c8dcSSimon Schubert dwarf2_frame_signal_frame_p (struct gdbarch *gdbarch,
8155796c8dcSSimon Schubert 			     struct frame_info *this_frame)
8165796c8dcSSimon Schubert {
8175796c8dcSSimon Schubert   struct dwarf2_frame_ops *ops = gdbarch_data (gdbarch, dwarf2_frame_data);
8185796c8dcSSimon Schubert 
8195796c8dcSSimon Schubert   if (ops->signal_frame_p == NULL)
8205796c8dcSSimon Schubert     return 0;
8215796c8dcSSimon Schubert   return ops->signal_frame_p (gdbarch, this_frame);
8225796c8dcSSimon Schubert }
8235796c8dcSSimon Schubert 
8245796c8dcSSimon Schubert /* Set the architecture-specific adjustment of .eh_frame and .debug_frame
8255796c8dcSSimon Schubert    register numbers.  */
8265796c8dcSSimon Schubert 
8275796c8dcSSimon Schubert void
dwarf2_frame_set_adjust_regnum(struct gdbarch * gdbarch,int (* adjust_regnum)(struct gdbarch *,int,int))8285796c8dcSSimon Schubert dwarf2_frame_set_adjust_regnum (struct gdbarch *gdbarch,
8295796c8dcSSimon Schubert 				int (*adjust_regnum) (struct gdbarch *,
8305796c8dcSSimon Schubert 						      int, int))
8315796c8dcSSimon Schubert {
8325796c8dcSSimon Schubert   struct dwarf2_frame_ops *ops = gdbarch_data (gdbarch, dwarf2_frame_data);
8335796c8dcSSimon Schubert 
8345796c8dcSSimon Schubert   ops->adjust_regnum = adjust_regnum;
8355796c8dcSSimon Schubert }
8365796c8dcSSimon Schubert 
8375796c8dcSSimon Schubert /* Translate a .eh_frame register to DWARF register, or adjust a .debug_frame
8385796c8dcSSimon Schubert    register.  */
8395796c8dcSSimon Schubert 
8405796c8dcSSimon Schubert static int
dwarf2_frame_adjust_regnum(struct gdbarch * gdbarch,int regnum,int eh_frame_p)841c50c785cSJohn Marino dwarf2_frame_adjust_regnum (struct gdbarch *gdbarch,
842c50c785cSJohn Marino 			    int regnum, int eh_frame_p)
8435796c8dcSSimon Schubert {
8445796c8dcSSimon Schubert   struct dwarf2_frame_ops *ops = gdbarch_data (gdbarch, dwarf2_frame_data);
8455796c8dcSSimon Schubert 
8465796c8dcSSimon Schubert   if (ops->adjust_regnum == NULL)
8475796c8dcSSimon Schubert     return regnum;
8485796c8dcSSimon Schubert   return ops->adjust_regnum (gdbarch, regnum, eh_frame_p);
8495796c8dcSSimon Schubert }
8505796c8dcSSimon Schubert 
8515796c8dcSSimon Schubert static void
dwarf2_frame_find_quirks(struct dwarf2_frame_state * fs,struct dwarf2_fde * fde)8525796c8dcSSimon Schubert dwarf2_frame_find_quirks (struct dwarf2_frame_state *fs,
8535796c8dcSSimon Schubert 			  struct dwarf2_fde *fde)
8545796c8dcSSimon Schubert {
8555796c8dcSSimon Schubert   struct symtab *s;
8565796c8dcSSimon Schubert 
8575796c8dcSSimon Schubert   s = find_pc_symtab (fs->pc);
858cf7f2e2dSJohn Marino   if (s == NULL)
8595796c8dcSSimon Schubert     return;
8605796c8dcSSimon Schubert 
861cf7f2e2dSJohn Marino   if (producer_is_realview (s->producer))
8625796c8dcSSimon Schubert     {
8635796c8dcSSimon Schubert       if (fde->cie->version == 1)
8645796c8dcSSimon Schubert 	fs->armcc_cfa_offsets_sf = 1;
8655796c8dcSSimon Schubert 
8665796c8dcSSimon Schubert       if (fde->cie->version == 1)
8675796c8dcSSimon Schubert 	fs->armcc_cfa_offsets_reversed = 1;
8685796c8dcSSimon Schubert 
8695796c8dcSSimon Schubert       /* The reversed offset problem is present in some compilers
8705796c8dcSSimon Schubert 	 using DWARF3, but it was eventually fixed.  Check the ARM
8715796c8dcSSimon Schubert 	 defined augmentations, which are in the format "armcc" followed
8725796c8dcSSimon Schubert 	 by a list of one-character options.  The "+" option means
8735796c8dcSSimon Schubert 	 this problem is fixed (no quirk needed).  If the armcc
8745796c8dcSSimon Schubert 	 augmentation is missing, the quirk is needed.  */
8755796c8dcSSimon Schubert       if (fde->cie->version == 3
8765796c8dcSSimon Schubert 	  && (strncmp (fde->cie->augmentation, "armcc", 5) != 0
8775796c8dcSSimon Schubert 	      || strchr (fde->cie->augmentation + 5, '+') == NULL))
8785796c8dcSSimon Schubert 	fs->armcc_cfa_offsets_reversed = 1;
8795796c8dcSSimon Schubert 
8805796c8dcSSimon Schubert       return;
8815796c8dcSSimon Schubert     }
8825796c8dcSSimon Schubert }
8835796c8dcSSimon Schubert 
8845796c8dcSSimon Schubert 
885c50c785cSJohn Marino void
dwarf2_compile_cfa_to_ax(struct agent_expr * expr,struct axs_value * loc,struct gdbarch * gdbarch,CORE_ADDR pc,struct dwarf2_per_cu_data * data)886c50c785cSJohn Marino dwarf2_compile_cfa_to_ax (struct agent_expr *expr, struct axs_value *loc,
887c50c785cSJohn Marino 			  struct gdbarch *gdbarch,
888c50c785cSJohn Marino 			  CORE_ADDR pc,
889c50c785cSJohn Marino 			  struct dwarf2_per_cu_data *data)
890c50c785cSJohn Marino {
891c50c785cSJohn Marino   struct dwarf2_fde *fde;
892*ef5ccd6cSJohn Marino   CORE_ADDR text_offset;
893c50c785cSJohn Marino   struct dwarf2_frame_state fs;
894c50c785cSJohn Marino   int addr_size;
895c50c785cSJohn Marino 
896c50c785cSJohn Marino   memset (&fs, 0, sizeof (struct dwarf2_frame_state));
897c50c785cSJohn Marino 
898c50c785cSJohn Marino   fs.pc = pc;
899c50c785cSJohn Marino 
900c50c785cSJohn Marino   /* Find the correct FDE.  */
901c50c785cSJohn Marino   fde = dwarf2_frame_find_fde (&fs.pc, &text_offset);
902c50c785cSJohn Marino   if (fde == NULL)
903c50c785cSJohn Marino     error (_("Could not compute CFA; needed to translate this expression"));
904c50c785cSJohn Marino 
905c50c785cSJohn Marino   /* Extract any interesting information from the CIE.  */
906c50c785cSJohn Marino   fs.data_align = fde->cie->data_alignment_factor;
907c50c785cSJohn Marino   fs.code_align = fde->cie->code_alignment_factor;
908c50c785cSJohn Marino   fs.retaddr_column = fde->cie->return_address_register;
909c50c785cSJohn Marino   addr_size = fde->cie->addr_size;
910c50c785cSJohn Marino 
911c50c785cSJohn Marino   /* Check for "quirks" - known bugs in producers.  */
912c50c785cSJohn Marino   dwarf2_frame_find_quirks (&fs, fde);
913c50c785cSJohn Marino 
914c50c785cSJohn Marino   /* First decode all the insns in the CIE.  */
915c50c785cSJohn Marino   execute_cfa_program (fde, fde->cie->initial_instructions,
916c50c785cSJohn Marino 		       fde->cie->end, gdbarch, pc, &fs);
917c50c785cSJohn Marino 
918c50c785cSJohn Marino   /* Save the initialized register set.  */
919c50c785cSJohn Marino   fs.initial = fs.regs;
920c50c785cSJohn Marino   fs.initial.reg = dwarf2_frame_state_copy_regs (&fs.regs);
921c50c785cSJohn Marino 
922c50c785cSJohn Marino   /* Then decode the insns in the FDE up to our target PC.  */
923c50c785cSJohn Marino   execute_cfa_program (fde, fde->instructions, fde->end, gdbarch, pc, &fs);
924c50c785cSJohn Marino 
925c50c785cSJohn Marino   /* Calculate the CFA.  */
926c50c785cSJohn Marino   switch (fs.regs.cfa_how)
927c50c785cSJohn Marino     {
928c50c785cSJohn Marino     case CFA_REG_OFFSET:
929c50c785cSJohn Marino       {
930c50c785cSJohn Marino 	int regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, fs.regs.cfa_reg);
931c50c785cSJohn Marino 
932c50c785cSJohn Marino 	if (regnum == -1)
933c50c785cSJohn Marino 	  error (_("Unable to access DWARF register number %d"),
934c50c785cSJohn Marino 		 (int) fs.regs.cfa_reg); /* FIXME */
935c50c785cSJohn Marino 	ax_reg (expr, regnum);
936c50c785cSJohn Marino 
937c50c785cSJohn Marino 	if (fs.regs.cfa_offset != 0)
938c50c785cSJohn Marino 	  {
939c50c785cSJohn Marino 	    if (fs.armcc_cfa_offsets_reversed)
940c50c785cSJohn Marino 	      ax_const_l (expr, -fs.regs.cfa_offset);
941c50c785cSJohn Marino 	    else
942c50c785cSJohn Marino 	      ax_const_l (expr, fs.regs.cfa_offset);
943c50c785cSJohn Marino 	    ax_simple (expr, aop_add);
944c50c785cSJohn Marino 	  }
945c50c785cSJohn Marino       }
946c50c785cSJohn Marino       break;
947c50c785cSJohn Marino 
948c50c785cSJohn Marino     case CFA_EXP:
949c50c785cSJohn Marino       ax_const_l (expr, text_offset);
950c50c785cSJohn Marino       dwarf2_compile_expr_to_ax (expr, loc, gdbarch, addr_size,
951c50c785cSJohn Marino 				 fs.regs.cfa_exp,
952c50c785cSJohn Marino 				 fs.regs.cfa_exp + fs.regs.cfa_exp_len,
953c50c785cSJohn Marino 				 data);
954c50c785cSJohn Marino       break;
955c50c785cSJohn Marino 
956c50c785cSJohn Marino     default:
957c50c785cSJohn Marino       internal_error (__FILE__, __LINE__, _("Unknown CFA rule."));
958c50c785cSJohn Marino     }
959c50c785cSJohn Marino }
960c50c785cSJohn Marino 
961c50c785cSJohn Marino 
9625796c8dcSSimon Schubert struct dwarf2_frame_cache
9635796c8dcSSimon Schubert {
9645796c8dcSSimon Schubert   /* DWARF Call Frame Address.  */
9655796c8dcSSimon Schubert   CORE_ADDR cfa;
9665796c8dcSSimon Schubert 
967c50c785cSJohn Marino   /* Set if the return address column was marked as unavailable
968c50c785cSJohn Marino      (required non-collected memory or registers to compute).  */
969c50c785cSJohn Marino   int unavailable_retaddr;
970c50c785cSJohn Marino 
9715796c8dcSSimon Schubert   /* Set if the return address column was marked as undefined.  */
9725796c8dcSSimon Schubert   int undefined_retaddr;
9735796c8dcSSimon Schubert 
9745796c8dcSSimon Schubert   /* Saved registers, indexed by GDB register number, not by DWARF
9755796c8dcSSimon Schubert      register number.  */
9765796c8dcSSimon Schubert   struct dwarf2_frame_state_reg *reg;
9775796c8dcSSimon Schubert 
9785796c8dcSSimon Schubert   /* Return address register.  */
9795796c8dcSSimon Schubert   struct dwarf2_frame_state_reg retaddr_reg;
9805796c8dcSSimon Schubert 
9815796c8dcSSimon Schubert   /* Target address size in bytes.  */
9825796c8dcSSimon Schubert   int addr_size;
983cf7f2e2dSJohn Marino 
984cf7f2e2dSJohn Marino   /* The .text offset.  */
985cf7f2e2dSJohn Marino   CORE_ADDR text_offset;
986a45ae5f8SJohn Marino 
987a45ae5f8SJohn Marino   /* If not NULL then this frame is the bottom frame of a TAILCALL_FRAME
988a45ae5f8SJohn Marino      sequence.  If NULL then it is a normal case with no TAILCALL_FRAME
989a45ae5f8SJohn Marino      involved.  Non-bottom frames of a virtual tail call frames chain use
990a45ae5f8SJohn Marino      dwarf2_tailcall_frame_unwind unwinder so this field does not apply for
991a45ae5f8SJohn Marino      them.  */
992a45ae5f8SJohn Marino   void *tailcall_cache;
9935796c8dcSSimon Schubert };
9945796c8dcSSimon Schubert 
995*ef5ccd6cSJohn Marino /* A cleanup that sets a pointer to NULL.  */
996*ef5ccd6cSJohn Marino 
997*ef5ccd6cSJohn Marino static void
clear_pointer_cleanup(void * arg)998*ef5ccd6cSJohn Marino clear_pointer_cleanup (void *arg)
999*ef5ccd6cSJohn Marino {
1000*ef5ccd6cSJohn Marino   void **ptr = arg;
1001*ef5ccd6cSJohn Marino 
1002*ef5ccd6cSJohn Marino   *ptr = NULL;
1003*ef5ccd6cSJohn Marino }
1004*ef5ccd6cSJohn Marino 
10055796c8dcSSimon Schubert static struct dwarf2_frame_cache *
dwarf2_frame_cache(struct frame_info * this_frame,void ** this_cache)10065796c8dcSSimon Schubert dwarf2_frame_cache (struct frame_info *this_frame, void **this_cache)
10075796c8dcSSimon Schubert {
1008*ef5ccd6cSJohn Marino   struct cleanup *reset_cache_cleanup, *old_chain;
10095796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_frame_arch (this_frame);
10105796c8dcSSimon Schubert   const int num_regs = gdbarch_num_regs (gdbarch)
10115796c8dcSSimon Schubert 		       + gdbarch_num_pseudo_regs (gdbarch);
10125796c8dcSSimon Schubert   struct dwarf2_frame_cache *cache;
10135796c8dcSSimon Schubert   struct dwarf2_frame_state *fs;
10145796c8dcSSimon Schubert   struct dwarf2_fde *fde;
1015c50c785cSJohn Marino   volatile struct gdb_exception ex;
1016a45ae5f8SJohn Marino   CORE_ADDR entry_pc;
1017a45ae5f8SJohn Marino   LONGEST entry_cfa_sp_offset;
1018a45ae5f8SJohn Marino   int entry_cfa_sp_offset_p = 0;
1019a45ae5f8SJohn Marino   const gdb_byte *instr;
10205796c8dcSSimon Schubert 
10215796c8dcSSimon Schubert   if (*this_cache)
10225796c8dcSSimon Schubert     return *this_cache;
10235796c8dcSSimon Schubert 
10245796c8dcSSimon Schubert   /* Allocate a new cache.  */
10255796c8dcSSimon Schubert   cache = FRAME_OBSTACK_ZALLOC (struct dwarf2_frame_cache);
10265796c8dcSSimon Schubert   cache->reg = FRAME_OBSTACK_CALLOC (num_regs, struct dwarf2_frame_state_reg);
1027c50c785cSJohn Marino   *this_cache = cache;
1028*ef5ccd6cSJohn Marino   reset_cache_cleanup = make_cleanup (clear_pointer_cleanup, this_cache);
10295796c8dcSSimon Schubert 
10305796c8dcSSimon Schubert   /* Allocate and initialize the frame state.  */
1031c50c785cSJohn Marino   fs = XZALLOC (struct dwarf2_frame_state);
10325796c8dcSSimon Schubert   old_chain = make_cleanup (dwarf2_frame_state_free, fs);
10335796c8dcSSimon Schubert 
10345796c8dcSSimon Schubert   /* Unwind the PC.
10355796c8dcSSimon Schubert 
10365796c8dcSSimon Schubert      Note that if the next frame is never supposed to return (i.e. a call
10375796c8dcSSimon Schubert      to abort), the compiler might optimize away the instruction at
10385796c8dcSSimon Schubert      its return address.  As a result the return address will
10395796c8dcSSimon Schubert      point at some random instruction, and the CFI for that
10405796c8dcSSimon Schubert      instruction is probably worthless to us.  GCC's unwinder solves
10415796c8dcSSimon Schubert      this problem by substracting 1 from the return address to get an
10425796c8dcSSimon Schubert      address in the middle of a presumed call instruction (or the
10435796c8dcSSimon Schubert      instruction in the associated delay slot).  This should only be
10445796c8dcSSimon Schubert      done for "normal" frames and not for resume-type frames (signal
10455796c8dcSSimon Schubert      handlers, sentinel frames, dummy frames).  The function
10465796c8dcSSimon Schubert      get_frame_address_in_block does just this.  It's not clear how
10475796c8dcSSimon Schubert      reliable the method is though; there is the potential for the
10485796c8dcSSimon Schubert      register state pre-call being different to that on return.  */
10495796c8dcSSimon Schubert   fs->pc = get_frame_address_in_block (this_frame);
10505796c8dcSSimon Schubert 
10515796c8dcSSimon Schubert   /* Find the correct FDE.  */
1052cf7f2e2dSJohn Marino   fde = dwarf2_frame_find_fde (&fs->pc, &cache->text_offset);
10535796c8dcSSimon Schubert   gdb_assert (fde != NULL);
10545796c8dcSSimon Schubert 
10555796c8dcSSimon Schubert   /* Extract any interesting information from the CIE.  */
10565796c8dcSSimon Schubert   fs->data_align = fde->cie->data_alignment_factor;
10575796c8dcSSimon Schubert   fs->code_align = fde->cie->code_alignment_factor;
10585796c8dcSSimon Schubert   fs->retaddr_column = fde->cie->return_address_register;
10595796c8dcSSimon Schubert   cache->addr_size = fde->cie->addr_size;
10605796c8dcSSimon Schubert 
10615796c8dcSSimon Schubert   /* Check for "quirks" - known bugs in producers.  */
10625796c8dcSSimon Schubert   dwarf2_frame_find_quirks (fs, fde);
10635796c8dcSSimon Schubert 
10645796c8dcSSimon Schubert   /* First decode all the insns in the CIE.  */
10655796c8dcSSimon Schubert   execute_cfa_program (fde, fde->cie->initial_instructions,
1066*ef5ccd6cSJohn Marino 		       fde->cie->end, gdbarch,
1067*ef5ccd6cSJohn Marino 		       get_frame_address_in_block (this_frame), fs);
10685796c8dcSSimon Schubert 
10695796c8dcSSimon Schubert   /* Save the initialized register set.  */
10705796c8dcSSimon Schubert   fs->initial = fs->regs;
10715796c8dcSSimon Schubert   fs->initial.reg = dwarf2_frame_state_copy_regs (&fs->regs);
10725796c8dcSSimon Schubert 
1073a45ae5f8SJohn Marino   if (get_frame_func_if_available (this_frame, &entry_pc))
1074a45ae5f8SJohn Marino     {
1075a45ae5f8SJohn Marino       /* Decode the insns in the FDE up to the entry PC.  */
1076a45ae5f8SJohn Marino       instr = execute_cfa_program (fde, fde->instructions, fde->end, gdbarch,
1077a45ae5f8SJohn Marino 				   entry_pc, fs);
1078a45ae5f8SJohn Marino 
1079a45ae5f8SJohn Marino       if (fs->regs.cfa_how == CFA_REG_OFFSET
1080a45ae5f8SJohn Marino 	  && (gdbarch_dwarf2_reg_to_regnum (gdbarch, fs->regs.cfa_reg)
1081a45ae5f8SJohn Marino 	      == gdbarch_sp_regnum (gdbarch)))
1082a45ae5f8SJohn Marino 	{
1083a45ae5f8SJohn Marino 	  entry_cfa_sp_offset = fs->regs.cfa_offset;
1084a45ae5f8SJohn Marino 	  entry_cfa_sp_offset_p = 1;
1085a45ae5f8SJohn Marino 	}
1086a45ae5f8SJohn Marino     }
1087a45ae5f8SJohn Marino   else
1088a45ae5f8SJohn Marino     instr = fde->instructions;
1089a45ae5f8SJohn Marino 
10905796c8dcSSimon Schubert   /* Then decode the insns in the FDE up to our target PC.  */
1091a45ae5f8SJohn Marino   execute_cfa_program (fde, instr, fde->end, gdbarch,
1092*ef5ccd6cSJohn Marino 		       get_frame_address_in_block (this_frame), fs);
10935796c8dcSSimon Schubert 
1094c50c785cSJohn Marino   TRY_CATCH (ex, RETURN_MASK_ERROR)
1095c50c785cSJohn Marino     {
10965796c8dcSSimon Schubert       /* Calculate the CFA.  */
10975796c8dcSSimon Schubert       switch (fs->regs.cfa_how)
10985796c8dcSSimon Schubert 	{
10995796c8dcSSimon Schubert 	case CFA_REG_OFFSET:
11005796c8dcSSimon Schubert 	  cache->cfa = read_reg (this_frame, fs->regs.cfa_reg);
11015796c8dcSSimon Schubert 	  if (fs->armcc_cfa_offsets_reversed)
11025796c8dcSSimon Schubert 	    cache->cfa -= fs->regs.cfa_offset;
11035796c8dcSSimon Schubert 	  else
11045796c8dcSSimon Schubert 	    cache->cfa += fs->regs.cfa_offset;
11055796c8dcSSimon Schubert 	  break;
11065796c8dcSSimon Schubert 
11075796c8dcSSimon Schubert 	case CFA_EXP:
11085796c8dcSSimon Schubert 	  cache->cfa =
11095796c8dcSSimon Schubert 	    execute_stack_op (fs->regs.cfa_exp, fs->regs.cfa_exp_len,
1110cf7f2e2dSJohn Marino 			      cache->addr_size, cache->text_offset,
1111cf7f2e2dSJohn Marino 			      this_frame, 0, 0);
11125796c8dcSSimon Schubert 	  break;
11135796c8dcSSimon Schubert 
11145796c8dcSSimon Schubert 	default:
11155796c8dcSSimon Schubert 	  internal_error (__FILE__, __LINE__, _("Unknown CFA rule."));
11165796c8dcSSimon Schubert 	}
1117c50c785cSJohn Marino     }
1118c50c785cSJohn Marino   if (ex.reason < 0)
1119c50c785cSJohn Marino     {
1120c50c785cSJohn Marino       if (ex.error == NOT_AVAILABLE_ERROR)
1121c50c785cSJohn Marino 	{
1122c50c785cSJohn Marino 	  cache->unavailable_retaddr = 1;
1123*ef5ccd6cSJohn Marino 	  do_cleanups (old_chain);
1124*ef5ccd6cSJohn Marino 	  discard_cleanups (reset_cache_cleanup);
1125c50c785cSJohn Marino 	  return cache;
1126c50c785cSJohn Marino 	}
1127c50c785cSJohn Marino 
1128c50c785cSJohn Marino       throw_exception (ex);
1129c50c785cSJohn Marino     }
11305796c8dcSSimon Schubert 
11315796c8dcSSimon Schubert   /* Initialize the register state.  */
11325796c8dcSSimon Schubert   {
11335796c8dcSSimon Schubert     int regnum;
11345796c8dcSSimon Schubert 
11355796c8dcSSimon Schubert     for (regnum = 0; regnum < num_regs; regnum++)
11365796c8dcSSimon Schubert       dwarf2_frame_init_reg (gdbarch, regnum, &cache->reg[regnum], this_frame);
11375796c8dcSSimon Schubert   }
11385796c8dcSSimon Schubert 
11395796c8dcSSimon Schubert   /* Go through the DWARF2 CFI generated table and save its register
11405796c8dcSSimon Schubert      location information in the cache.  Note that we don't skip the
11415796c8dcSSimon Schubert      return address column; it's perfectly all right for it to
11425796c8dcSSimon Schubert      correspond to a real register.  If it doesn't correspond to a
11435796c8dcSSimon Schubert      real register, or if we shouldn't treat it as such,
11445796c8dcSSimon Schubert      gdbarch_dwarf2_reg_to_regnum should be defined to return a number outside
11455796c8dcSSimon Schubert      the range [0, gdbarch_num_regs).  */
11465796c8dcSSimon Schubert   {
11475796c8dcSSimon Schubert     int column;		/* CFI speak for "register number".  */
11485796c8dcSSimon Schubert 
11495796c8dcSSimon Schubert     for (column = 0; column < fs->regs.num_regs; column++)
11505796c8dcSSimon Schubert       {
11515796c8dcSSimon Schubert 	/* Use the GDB register number as the destination index.  */
11525796c8dcSSimon Schubert 	int regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, column);
11535796c8dcSSimon Schubert 
11545796c8dcSSimon Schubert 	/* If there's no corresponding GDB register, ignore it.  */
11555796c8dcSSimon Schubert 	if (regnum < 0 || regnum >= num_regs)
11565796c8dcSSimon Schubert 	  continue;
11575796c8dcSSimon Schubert 
11585796c8dcSSimon Schubert 	/* NOTE: cagney/2003-09-05: CFI should specify the disposition
11595796c8dcSSimon Schubert 	   of all debug info registers.  If it doesn't, complain (but
11605796c8dcSSimon Schubert 	   not too loudly).  It turns out that GCC assumes that an
11615796c8dcSSimon Schubert 	   unspecified register implies "same value" when CFI (draft
11625796c8dcSSimon Schubert 	   7) specifies nothing at all.  Such a register could equally
11635796c8dcSSimon Schubert 	   be interpreted as "undefined".  Also note that this check
11645796c8dcSSimon Schubert 	   isn't sufficient; it only checks that all registers in the
11655796c8dcSSimon Schubert 	   range [0 .. max column] are specified, and won't detect
11665796c8dcSSimon Schubert 	   problems when a debug info register falls outside of the
11675796c8dcSSimon Schubert 	   table.  We need a way of iterating through all the valid
11685796c8dcSSimon Schubert 	   DWARF2 register numbers.  */
11695796c8dcSSimon Schubert 	if (fs->regs.reg[column].how == DWARF2_FRAME_REG_UNSPECIFIED)
11705796c8dcSSimon Schubert 	  {
11715796c8dcSSimon Schubert 	    if (cache->reg[regnum].how == DWARF2_FRAME_REG_UNSPECIFIED)
11725796c8dcSSimon Schubert 	      complaint (&symfile_complaints, _("\
11735796c8dcSSimon Schubert incomplete CFI data; unspecified registers (e.g., %s) at %s"),
11745796c8dcSSimon Schubert 			 gdbarch_register_name (gdbarch, regnum),
11755796c8dcSSimon Schubert 			 paddress (gdbarch, fs->pc));
11765796c8dcSSimon Schubert 	  }
11775796c8dcSSimon Schubert 	else
11785796c8dcSSimon Schubert 	  cache->reg[regnum] = fs->regs.reg[column];
11795796c8dcSSimon Schubert       }
11805796c8dcSSimon Schubert   }
11815796c8dcSSimon Schubert 
11825796c8dcSSimon Schubert   /* Eliminate any DWARF2_FRAME_REG_RA rules, and save the information
11835796c8dcSSimon Schubert      we need for evaluating DWARF2_FRAME_REG_RA_OFFSET rules.  */
11845796c8dcSSimon Schubert   {
11855796c8dcSSimon Schubert     int regnum;
11865796c8dcSSimon Schubert 
11875796c8dcSSimon Schubert     for (regnum = 0; regnum < num_regs; regnum++)
11885796c8dcSSimon Schubert       {
11895796c8dcSSimon Schubert 	if (cache->reg[regnum].how == DWARF2_FRAME_REG_RA
11905796c8dcSSimon Schubert 	    || cache->reg[regnum].how == DWARF2_FRAME_REG_RA_OFFSET)
11915796c8dcSSimon Schubert 	  {
11925796c8dcSSimon Schubert 	    struct dwarf2_frame_state_reg *retaddr_reg =
11935796c8dcSSimon Schubert 	      &fs->regs.reg[fs->retaddr_column];
11945796c8dcSSimon Schubert 
11955796c8dcSSimon Schubert 	    /* It seems rather bizarre to specify an "empty" column as
11965796c8dcSSimon Schubert                the return adress column.  However, this is exactly
11975796c8dcSSimon Schubert                what GCC does on some targets.  It turns out that GCC
11985796c8dcSSimon Schubert                assumes that the return address can be found in the
11995796c8dcSSimon Schubert                register corresponding to the return address column.
12005796c8dcSSimon Schubert                Incidentally, that's how we should treat a return
12015796c8dcSSimon Schubert                address column specifying "same value" too.  */
12025796c8dcSSimon Schubert 	    if (fs->retaddr_column < fs->regs.num_regs
12035796c8dcSSimon Schubert 		&& retaddr_reg->how != DWARF2_FRAME_REG_UNSPECIFIED
12045796c8dcSSimon Schubert 		&& retaddr_reg->how != DWARF2_FRAME_REG_SAME_VALUE)
12055796c8dcSSimon Schubert 	      {
12065796c8dcSSimon Schubert 		if (cache->reg[regnum].how == DWARF2_FRAME_REG_RA)
12075796c8dcSSimon Schubert 		  cache->reg[regnum] = *retaddr_reg;
12085796c8dcSSimon Schubert 		else
12095796c8dcSSimon Schubert 		  cache->retaddr_reg = *retaddr_reg;
12105796c8dcSSimon Schubert 	      }
12115796c8dcSSimon Schubert 	    else
12125796c8dcSSimon Schubert 	      {
12135796c8dcSSimon Schubert 		if (cache->reg[regnum].how == DWARF2_FRAME_REG_RA)
12145796c8dcSSimon Schubert 		  {
12155796c8dcSSimon Schubert 		    cache->reg[regnum].loc.reg = fs->retaddr_column;
12165796c8dcSSimon Schubert 		    cache->reg[regnum].how = DWARF2_FRAME_REG_SAVED_REG;
12175796c8dcSSimon Schubert 		  }
12185796c8dcSSimon Schubert 		else
12195796c8dcSSimon Schubert 		  {
12205796c8dcSSimon Schubert 		    cache->retaddr_reg.loc.reg = fs->retaddr_column;
12215796c8dcSSimon Schubert 		    cache->retaddr_reg.how = DWARF2_FRAME_REG_SAVED_REG;
12225796c8dcSSimon Schubert 		  }
12235796c8dcSSimon Schubert 	      }
12245796c8dcSSimon Schubert 	  }
12255796c8dcSSimon Schubert       }
12265796c8dcSSimon Schubert   }
12275796c8dcSSimon Schubert 
12285796c8dcSSimon Schubert   if (fs->retaddr_column < fs->regs.num_regs
12295796c8dcSSimon Schubert       && fs->regs.reg[fs->retaddr_column].how == DWARF2_FRAME_REG_UNDEFINED)
12305796c8dcSSimon Schubert     cache->undefined_retaddr = 1;
12315796c8dcSSimon Schubert 
12325796c8dcSSimon Schubert   do_cleanups (old_chain);
12335796c8dcSSimon Schubert 
1234a45ae5f8SJohn Marino   /* Try to find a virtual tail call frames chain with bottom (callee) frame
1235a45ae5f8SJohn Marino      starting at THIS_FRAME.  */
1236a45ae5f8SJohn Marino   dwarf2_tailcall_sniffer_first (this_frame, &cache->tailcall_cache,
1237a45ae5f8SJohn Marino 				 (entry_cfa_sp_offset_p
1238a45ae5f8SJohn Marino 				  ? &entry_cfa_sp_offset : NULL));
1239a45ae5f8SJohn Marino 
1240*ef5ccd6cSJohn Marino   discard_cleanups (reset_cache_cleanup);
12415796c8dcSSimon Schubert   return cache;
12425796c8dcSSimon Schubert }
12435796c8dcSSimon Schubert 
1244c50c785cSJohn Marino static enum unwind_stop_reason
dwarf2_frame_unwind_stop_reason(struct frame_info * this_frame,void ** this_cache)1245c50c785cSJohn Marino dwarf2_frame_unwind_stop_reason (struct frame_info *this_frame,
1246c50c785cSJohn Marino 				 void **this_cache)
1247c50c785cSJohn Marino {
1248c50c785cSJohn Marino   struct dwarf2_frame_cache *cache
1249c50c785cSJohn Marino     = dwarf2_frame_cache (this_frame, this_cache);
1250c50c785cSJohn Marino 
1251c50c785cSJohn Marino   if (cache->unavailable_retaddr)
1252c50c785cSJohn Marino     return UNWIND_UNAVAILABLE;
1253c50c785cSJohn Marino 
1254c50c785cSJohn Marino   if (cache->undefined_retaddr)
1255c50c785cSJohn Marino     return UNWIND_OUTERMOST;
1256c50c785cSJohn Marino 
1257c50c785cSJohn Marino   return UNWIND_NO_REASON;
1258c50c785cSJohn Marino }
1259c50c785cSJohn Marino 
12605796c8dcSSimon Schubert static void
dwarf2_frame_this_id(struct frame_info * this_frame,void ** this_cache,struct frame_id * this_id)12615796c8dcSSimon Schubert dwarf2_frame_this_id (struct frame_info *this_frame, void **this_cache,
12625796c8dcSSimon Schubert 		      struct frame_id *this_id)
12635796c8dcSSimon Schubert {
12645796c8dcSSimon Schubert   struct dwarf2_frame_cache *cache =
12655796c8dcSSimon Schubert     dwarf2_frame_cache (this_frame, this_cache);
12665796c8dcSSimon Schubert 
1267c50c785cSJohn Marino   if (cache->unavailable_retaddr)
1268c50c785cSJohn Marino     return;
1269c50c785cSJohn Marino 
12705796c8dcSSimon Schubert   if (cache->undefined_retaddr)
12715796c8dcSSimon Schubert     return;
12725796c8dcSSimon Schubert 
12735796c8dcSSimon Schubert   (*this_id) = frame_id_build (cache->cfa, get_frame_func (this_frame));
12745796c8dcSSimon Schubert }
12755796c8dcSSimon Schubert 
12765796c8dcSSimon Schubert static struct value *
dwarf2_frame_prev_register(struct frame_info * this_frame,void ** this_cache,int regnum)12775796c8dcSSimon Schubert dwarf2_frame_prev_register (struct frame_info *this_frame, void **this_cache,
12785796c8dcSSimon Schubert 			    int regnum)
12795796c8dcSSimon Schubert {
12805796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_frame_arch (this_frame);
12815796c8dcSSimon Schubert   struct dwarf2_frame_cache *cache =
12825796c8dcSSimon Schubert     dwarf2_frame_cache (this_frame, this_cache);
12835796c8dcSSimon Schubert   CORE_ADDR addr;
12845796c8dcSSimon Schubert   int realnum;
12855796c8dcSSimon Schubert 
1286a45ae5f8SJohn Marino   /* Non-bottom frames of a virtual tail call frames chain use
1287a45ae5f8SJohn Marino      dwarf2_tailcall_frame_unwind unwinder so this code does not apply for
1288a45ae5f8SJohn Marino      them.  If dwarf2_tailcall_prev_register_first does not have specific value
1289a45ae5f8SJohn Marino      unwind the register, tail call frames are assumed to have the register set
1290a45ae5f8SJohn Marino      of the top caller.  */
1291a45ae5f8SJohn Marino   if (cache->tailcall_cache)
1292a45ae5f8SJohn Marino     {
1293a45ae5f8SJohn Marino       struct value *val;
1294a45ae5f8SJohn Marino 
1295a45ae5f8SJohn Marino       val = dwarf2_tailcall_prev_register_first (this_frame,
1296a45ae5f8SJohn Marino 						 &cache->tailcall_cache,
1297a45ae5f8SJohn Marino 						 regnum);
1298a45ae5f8SJohn Marino       if (val)
1299a45ae5f8SJohn Marino 	return val;
1300a45ae5f8SJohn Marino     }
1301a45ae5f8SJohn Marino 
13025796c8dcSSimon Schubert   switch (cache->reg[regnum].how)
13035796c8dcSSimon Schubert     {
13045796c8dcSSimon Schubert     case DWARF2_FRAME_REG_UNDEFINED:
13055796c8dcSSimon Schubert       /* If CFI explicitly specified that the value isn't defined,
13065796c8dcSSimon Schubert 	 mark it as optimized away; the value isn't available.  */
13075796c8dcSSimon Schubert       return frame_unwind_got_optimized (this_frame, regnum);
13085796c8dcSSimon Schubert 
13095796c8dcSSimon Schubert     case DWARF2_FRAME_REG_SAVED_OFFSET:
13105796c8dcSSimon Schubert       addr = cache->cfa + cache->reg[regnum].loc.offset;
13115796c8dcSSimon Schubert       return frame_unwind_got_memory (this_frame, regnum, addr);
13125796c8dcSSimon Schubert 
13135796c8dcSSimon Schubert     case DWARF2_FRAME_REG_SAVED_REG:
13145796c8dcSSimon Schubert       realnum
13155796c8dcSSimon Schubert 	= gdbarch_dwarf2_reg_to_regnum (gdbarch, cache->reg[regnum].loc.reg);
13165796c8dcSSimon Schubert       return frame_unwind_got_register (this_frame, regnum, realnum);
13175796c8dcSSimon Schubert 
13185796c8dcSSimon Schubert     case DWARF2_FRAME_REG_SAVED_EXP:
13195796c8dcSSimon Schubert       addr = execute_stack_op (cache->reg[regnum].loc.exp,
13205796c8dcSSimon Schubert 			       cache->reg[regnum].exp_len,
1321cf7f2e2dSJohn Marino 			       cache->addr_size, cache->text_offset,
1322cf7f2e2dSJohn Marino 			       this_frame, cache->cfa, 1);
13235796c8dcSSimon Schubert       return frame_unwind_got_memory (this_frame, regnum, addr);
13245796c8dcSSimon Schubert 
13255796c8dcSSimon Schubert     case DWARF2_FRAME_REG_SAVED_VAL_OFFSET:
13265796c8dcSSimon Schubert       addr = cache->cfa + cache->reg[regnum].loc.offset;
13275796c8dcSSimon Schubert       return frame_unwind_got_constant (this_frame, regnum, addr);
13285796c8dcSSimon Schubert 
13295796c8dcSSimon Schubert     case DWARF2_FRAME_REG_SAVED_VAL_EXP:
13305796c8dcSSimon Schubert       addr = execute_stack_op (cache->reg[regnum].loc.exp,
13315796c8dcSSimon Schubert 			       cache->reg[regnum].exp_len,
1332cf7f2e2dSJohn Marino 			       cache->addr_size, cache->text_offset,
1333cf7f2e2dSJohn Marino 			       this_frame, cache->cfa, 1);
13345796c8dcSSimon Schubert       return frame_unwind_got_constant (this_frame, regnum, addr);
13355796c8dcSSimon Schubert 
13365796c8dcSSimon Schubert     case DWARF2_FRAME_REG_UNSPECIFIED:
13375796c8dcSSimon Schubert       /* GCC, in its infinite wisdom decided to not provide unwind
13385796c8dcSSimon Schubert 	 information for registers that are "same value".  Since
13395796c8dcSSimon Schubert 	 DWARF2 (3 draft 7) doesn't define such behavior, said
13405796c8dcSSimon Schubert 	 registers are actually undefined (which is different to CFI
13415796c8dcSSimon Schubert 	 "undefined").  Code above issues a complaint about this.
13425796c8dcSSimon Schubert 	 Here just fudge the books, assume GCC, and that the value is
13435796c8dcSSimon Schubert 	 more inner on the stack.  */
13445796c8dcSSimon Schubert       return frame_unwind_got_register (this_frame, regnum, regnum);
13455796c8dcSSimon Schubert 
13465796c8dcSSimon Schubert     case DWARF2_FRAME_REG_SAME_VALUE:
13475796c8dcSSimon Schubert       return frame_unwind_got_register (this_frame, regnum, regnum);
13485796c8dcSSimon Schubert 
13495796c8dcSSimon Schubert     case DWARF2_FRAME_REG_CFA:
13505796c8dcSSimon Schubert       return frame_unwind_got_address (this_frame, regnum, cache->cfa);
13515796c8dcSSimon Schubert 
13525796c8dcSSimon Schubert     case DWARF2_FRAME_REG_CFA_OFFSET:
13535796c8dcSSimon Schubert       addr = cache->cfa + cache->reg[regnum].loc.offset;
13545796c8dcSSimon Schubert       return frame_unwind_got_address (this_frame, regnum, addr);
13555796c8dcSSimon Schubert 
13565796c8dcSSimon Schubert     case DWARF2_FRAME_REG_RA_OFFSET:
13575796c8dcSSimon Schubert       addr = cache->reg[regnum].loc.offset;
13585796c8dcSSimon Schubert       regnum = gdbarch_dwarf2_reg_to_regnum
13595796c8dcSSimon Schubert 	(gdbarch, cache->retaddr_reg.loc.reg);
13605796c8dcSSimon Schubert       addr += get_frame_register_unsigned (this_frame, regnum);
13615796c8dcSSimon Schubert       return frame_unwind_got_address (this_frame, regnum, addr);
13625796c8dcSSimon Schubert 
13635796c8dcSSimon Schubert     case DWARF2_FRAME_REG_FN:
13645796c8dcSSimon Schubert       return cache->reg[regnum].loc.fn (this_frame, this_cache, regnum);
13655796c8dcSSimon Schubert 
13665796c8dcSSimon Schubert     default:
13675796c8dcSSimon Schubert       internal_error (__FILE__, __LINE__, _("Unknown register rule."));
13685796c8dcSSimon Schubert     }
13695796c8dcSSimon Schubert }
13705796c8dcSSimon Schubert 
1371a45ae5f8SJohn Marino /* Proxy for tailcall_frame_dealloc_cache for bottom frame of a virtual tail
1372a45ae5f8SJohn Marino    call frames chain.  */
1373a45ae5f8SJohn Marino 
1374a45ae5f8SJohn Marino static void
dwarf2_frame_dealloc_cache(struct frame_info * self,void * this_cache)1375a45ae5f8SJohn Marino dwarf2_frame_dealloc_cache (struct frame_info *self, void *this_cache)
1376a45ae5f8SJohn Marino {
1377a45ae5f8SJohn Marino   struct dwarf2_frame_cache *cache = dwarf2_frame_cache (self, &this_cache);
1378a45ae5f8SJohn Marino 
1379a45ae5f8SJohn Marino   if (cache->tailcall_cache)
1380a45ae5f8SJohn Marino     dwarf2_tailcall_frame_unwind.dealloc_cache (self, cache->tailcall_cache);
1381a45ae5f8SJohn Marino }
1382a45ae5f8SJohn Marino 
13835796c8dcSSimon Schubert static int
dwarf2_frame_sniffer(const struct frame_unwind * self,struct frame_info * this_frame,void ** this_cache)13845796c8dcSSimon Schubert dwarf2_frame_sniffer (const struct frame_unwind *self,
13855796c8dcSSimon Schubert 		      struct frame_info *this_frame, void **this_cache)
13865796c8dcSSimon Schubert {
13875796c8dcSSimon Schubert   /* Grab an address that is guarenteed to reside somewhere within the
13885796c8dcSSimon Schubert      function.  get_frame_pc(), with a no-return next function, can
13895796c8dcSSimon Schubert      end up returning something past the end of this function's body.
13905796c8dcSSimon Schubert      If the frame we're sniffing for is a signal frame whose start
13915796c8dcSSimon Schubert      address is placed on the stack by the OS, its FDE must
13925796c8dcSSimon Schubert      extend one byte before its start address or we could potentially
13935796c8dcSSimon Schubert      select the FDE of the previous function.  */
13945796c8dcSSimon Schubert   CORE_ADDR block_addr = get_frame_address_in_block (this_frame);
1395cf7f2e2dSJohn Marino   struct dwarf2_fde *fde = dwarf2_frame_find_fde (&block_addr, NULL);
1396cf7f2e2dSJohn Marino 
13975796c8dcSSimon Schubert   if (!fde)
13985796c8dcSSimon Schubert     return 0;
13995796c8dcSSimon Schubert 
14005796c8dcSSimon Schubert   /* On some targets, signal trampolines may have unwind information.
14015796c8dcSSimon Schubert      We need to recognize them so that we set the frame type
14025796c8dcSSimon Schubert      correctly.  */
14035796c8dcSSimon Schubert 
14045796c8dcSSimon Schubert   if (fde->cie->signal_frame
14055796c8dcSSimon Schubert       || dwarf2_frame_signal_frame_p (get_frame_arch (this_frame),
14065796c8dcSSimon Schubert 				      this_frame))
14075796c8dcSSimon Schubert     return self->type == SIGTRAMP_FRAME;
14085796c8dcSSimon Schubert 
1409a45ae5f8SJohn Marino   if (self->type != NORMAL_FRAME)
1410a45ae5f8SJohn Marino     return 0;
1411a45ae5f8SJohn Marino 
1412a45ae5f8SJohn Marino   /* Preinitializa the cache so that TAILCALL_FRAME can find the record by
1413a45ae5f8SJohn Marino      dwarf2_tailcall_sniffer_first.  */
1414a45ae5f8SJohn Marino   dwarf2_frame_cache (this_frame, this_cache);
1415a45ae5f8SJohn Marino 
1416a45ae5f8SJohn Marino   return 1;
14175796c8dcSSimon Schubert }
14185796c8dcSSimon Schubert 
14195796c8dcSSimon Schubert static const struct frame_unwind dwarf2_frame_unwind =
14205796c8dcSSimon Schubert {
14215796c8dcSSimon Schubert   NORMAL_FRAME,
1422c50c785cSJohn Marino   dwarf2_frame_unwind_stop_reason,
14235796c8dcSSimon Schubert   dwarf2_frame_this_id,
14245796c8dcSSimon Schubert   dwarf2_frame_prev_register,
14255796c8dcSSimon Schubert   NULL,
1426a45ae5f8SJohn Marino   dwarf2_frame_sniffer,
1427a45ae5f8SJohn Marino   dwarf2_frame_dealloc_cache
14285796c8dcSSimon Schubert };
14295796c8dcSSimon Schubert 
14305796c8dcSSimon Schubert static const struct frame_unwind dwarf2_signal_frame_unwind =
14315796c8dcSSimon Schubert {
14325796c8dcSSimon Schubert   SIGTRAMP_FRAME,
1433c50c785cSJohn Marino   dwarf2_frame_unwind_stop_reason,
14345796c8dcSSimon Schubert   dwarf2_frame_this_id,
14355796c8dcSSimon Schubert   dwarf2_frame_prev_register,
14365796c8dcSSimon Schubert   NULL,
1437a45ae5f8SJohn Marino   dwarf2_frame_sniffer,
1438a45ae5f8SJohn Marino 
1439a45ae5f8SJohn Marino   /* TAILCALL_CACHE can never be in such frame to need dealloc_cache.  */
1440a45ae5f8SJohn Marino   NULL
14415796c8dcSSimon Schubert };
14425796c8dcSSimon Schubert 
14435796c8dcSSimon Schubert /* Append the DWARF-2 frame unwinders to GDBARCH's list.  */
14445796c8dcSSimon Schubert 
14455796c8dcSSimon Schubert void
dwarf2_append_unwinders(struct gdbarch * gdbarch)14465796c8dcSSimon Schubert dwarf2_append_unwinders (struct gdbarch *gdbarch)
14475796c8dcSSimon Schubert {
1448a45ae5f8SJohn Marino   /* TAILCALL_FRAME must be first to find the record by
1449a45ae5f8SJohn Marino      dwarf2_tailcall_sniffer_first.  */
1450a45ae5f8SJohn Marino   frame_unwind_append_unwinder (gdbarch, &dwarf2_tailcall_frame_unwind);
1451a45ae5f8SJohn Marino 
14525796c8dcSSimon Schubert   frame_unwind_append_unwinder (gdbarch, &dwarf2_frame_unwind);
14535796c8dcSSimon Schubert   frame_unwind_append_unwinder (gdbarch, &dwarf2_signal_frame_unwind);
14545796c8dcSSimon Schubert }
14555796c8dcSSimon Schubert 
14565796c8dcSSimon Schubert 
14575796c8dcSSimon Schubert /* There is no explicitly defined relationship between the CFA and the
14585796c8dcSSimon Schubert    location of frame's local variables and arguments/parameters.
14595796c8dcSSimon Schubert    Therefore, frame base methods on this page should probably only be
14605796c8dcSSimon Schubert    used as a last resort, just to avoid printing total garbage as a
14615796c8dcSSimon Schubert    response to the "info frame" command.  */
14625796c8dcSSimon Schubert 
14635796c8dcSSimon Schubert static CORE_ADDR
dwarf2_frame_base_address(struct frame_info * this_frame,void ** this_cache)14645796c8dcSSimon Schubert dwarf2_frame_base_address (struct frame_info *this_frame, void **this_cache)
14655796c8dcSSimon Schubert {
14665796c8dcSSimon Schubert   struct dwarf2_frame_cache *cache =
14675796c8dcSSimon Schubert     dwarf2_frame_cache (this_frame, this_cache);
14685796c8dcSSimon Schubert 
14695796c8dcSSimon Schubert   return cache->cfa;
14705796c8dcSSimon Schubert }
14715796c8dcSSimon Schubert 
14725796c8dcSSimon Schubert static const struct frame_base dwarf2_frame_base =
14735796c8dcSSimon Schubert {
14745796c8dcSSimon Schubert   &dwarf2_frame_unwind,
14755796c8dcSSimon Schubert   dwarf2_frame_base_address,
14765796c8dcSSimon Schubert   dwarf2_frame_base_address,
14775796c8dcSSimon Schubert   dwarf2_frame_base_address
14785796c8dcSSimon Schubert };
14795796c8dcSSimon Schubert 
14805796c8dcSSimon Schubert const struct frame_base *
dwarf2_frame_base_sniffer(struct frame_info * this_frame)14815796c8dcSSimon Schubert dwarf2_frame_base_sniffer (struct frame_info *this_frame)
14825796c8dcSSimon Schubert {
14835796c8dcSSimon Schubert   CORE_ADDR block_addr = get_frame_address_in_block (this_frame);
1484cf7f2e2dSJohn Marino 
1485cf7f2e2dSJohn Marino   if (dwarf2_frame_find_fde (&block_addr, NULL))
14865796c8dcSSimon Schubert     return &dwarf2_frame_base;
14875796c8dcSSimon Schubert 
14885796c8dcSSimon Schubert   return NULL;
14895796c8dcSSimon Schubert }
14905796c8dcSSimon Schubert 
14915796c8dcSSimon Schubert /* Compute the CFA for THIS_FRAME, but only if THIS_FRAME came from
14925796c8dcSSimon Schubert    the DWARF unwinder.  This is used to implement
14935796c8dcSSimon Schubert    DW_OP_call_frame_cfa.  */
14945796c8dcSSimon Schubert 
14955796c8dcSSimon Schubert CORE_ADDR
dwarf2_frame_cfa(struct frame_info * this_frame)14965796c8dcSSimon Schubert dwarf2_frame_cfa (struct frame_info *this_frame)
14975796c8dcSSimon Schubert {
14985796c8dcSSimon Schubert   while (get_frame_type (this_frame) == INLINE_FRAME)
14995796c8dcSSimon Schubert     this_frame = get_prev_frame (this_frame);
15005796c8dcSSimon Schubert   /* This restriction could be lifted if other unwinders are known to
15015796c8dcSSimon Schubert      compute the frame base in a way compatible with the DWARF
15025796c8dcSSimon Schubert      unwinder.  */
1503a45ae5f8SJohn Marino   if (!frame_unwinder_is (this_frame, &dwarf2_frame_unwind)
1504a45ae5f8SJohn Marino       && !frame_unwinder_is (this_frame, &dwarf2_tailcall_frame_unwind))
15055796c8dcSSimon Schubert     error (_("can't compute CFA for this frame"));
1506*ef5ccd6cSJohn Marino   if (get_frame_unwind_stop_reason (this_frame) == UNWIND_UNAVAILABLE)
1507*ef5ccd6cSJohn Marino     throw_error (NOT_AVAILABLE_ERROR,
1508*ef5ccd6cSJohn Marino 		 _("can't compute CFA for this frame: "
1509*ef5ccd6cSJohn Marino 		   "required registers or memory are unavailable"));
15105796c8dcSSimon Schubert   return get_frame_base (this_frame);
15115796c8dcSSimon Schubert }
15125796c8dcSSimon Schubert 
15135796c8dcSSimon Schubert const struct objfile_data *dwarf2_frame_objfile_data;
15145796c8dcSSimon Schubert 
15155796c8dcSSimon Schubert static unsigned int
read_1_byte(bfd * abfd,const gdb_byte * buf)1516*ef5ccd6cSJohn Marino read_1_byte (bfd *abfd, const gdb_byte *buf)
15175796c8dcSSimon Schubert {
15185796c8dcSSimon Schubert   return bfd_get_8 (abfd, buf);
15195796c8dcSSimon Schubert }
15205796c8dcSSimon Schubert 
15215796c8dcSSimon Schubert static unsigned int
read_4_bytes(bfd * abfd,const gdb_byte * buf)1522*ef5ccd6cSJohn Marino read_4_bytes (bfd *abfd, const gdb_byte *buf)
15235796c8dcSSimon Schubert {
15245796c8dcSSimon Schubert   return bfd_get_32 (abfd, buf);
15255796c8dcSSimon Schubert }
15265796c8dcSSimon Schubert 
15275796c8dcSSimon Schubert static ULONGEST
read_8_bytes(bfd * abfd,const gdb_byte * buf)1528*ef5ccd6cSJohn Marino read_8_bytes (bfd *abfd, const gdb_byte *buf)
15295796c8dcSSimon Schubert {
15305796c8dcSSimon Schubert   return bfd_get_64 (abfd, buf);
15315796c8dcSSimon Schubert }
15325796c8dcSSimon Schubert 
15335796c8dcSSimon Schubert static ULONGEST
read_initial_length(bfd * abfd,const gdb_byte * buf,unsigned int * bytes_read_ptr)1534*ef5ccd6cSJohn Marino read_initial_length (bfd *abfd, const gdb_byte *buf,
1535*ef5ccd6cSJohn Marino 		     unsigned int *bytes_read_ptr)
15365796c8dcSSimon Schubert {
15375796c8dcSSimon Schubert   LONGEST result;
15385796c8dcSSimon Schubert 
15395796c8dcSSimon Schubert   result = bfd_get_32 (abfd, buf);
15405796c8dcSSimon Schubert   if (result == 0xffffffff)
15415796c8dcSSimon Schubert     {
15425796c8dcSSimon Schubert       result = bfd_get_64 (abfd, buf + 4);
15435796c8dcSSimon Schubert       *bytes_read_ptr = 12;
15445796c8dcSSimon Schubert     }
15455796c8dcSSimon Schubert   else
15465796c8dcSSimon Schubert     *bytes_read_ptr = 4;
15475796c8dcSSimon Schubert 
15485796c8dcSSimon Schubert   return result;
15495796c8dcSSimon Schubert }
15505796c8dcSSimon Schubert 
15515796c8dcSSimon Schubert 
15525796c8dcSSimon Schubert /* Pointer encoding helper functions.  */
15535796c8dcSSimon Schubert 
15545796c8dcSSimon Schubert /* GCC supports exception handling based on DWARF2 CFI.  However, for
15555796c8dcSSimon Schubert    technical reasons, it encodes addresses in its FDE's in a different
15565796c8dcSSimon Schubert    way.  Several "pointer encodings" are supported.  The encoding
15575796c8dcSSimon Schubert    that's used for a particular FDE is determined by the 'R'
15585796c8dcSSimon Schubert    augmentation in the associated CIE.  The argument of this
15595796c8dcSSimon Schubert    augmentation is a single byte.
15605796c8dcSSimon Schubert 
15615796c8dcSSimon Schubert    The address can be encoded as 2 bytes, 4 bytes, 8 bytes, or as a
15625796c8dcSSimon Schubert    LEB128.  This is encoded in bits 0, 1 and 2.  Bit 3 encodes whether
15635796c8dcSSimon Schubert    the address is signed or unsigned.  Bits 4, 5 and 6 encode how the
15645796c8dcSSimon Schubert    address should be interpreted (absolute, relative to the current
15655796c8dcSSimon Schubert    position in the FDE, ...).  Bit 7, indicates that the address
15665796c8dcSSimon Schubert    should be dereferenced.  */
15675796c8dcSSimon Schubert 
15685796c8dcSSimon Schubert static gdb_byte
encoding_for_size(unsigned int size)15695796c8dcSSimon Schubert encoding_for_size (unsigned int size)
15705796c8dcSSimon Schubert {
15715796c8dcSSimon Schubert   switch (size)
15725796c8dcSSimon Schubert     {
15735796c8dcSSimon Schubert     case 2:
15745796c8dcSSimon Schubert       return DW_EH_PE_udata2;
15755796c8dcSSimon Schubert     case 4:
15765796c8dcSSimon Schubert       return DW_EH_PE_udata4;
15775796c8dcSSimon Schubert     case 8:
15785796c8dcSSimon Schubert       return DW_EH_PE_udata8;
15795796c8dcSSimon Schubert     default:
15805796c8dcSSimon Schubert       internal_error (__FILE__, __LINE__, _("Unsupported address size"));
15815796c8dcSSimon Schubert     }
15825796c8dcSSimon Schubert }
15835796c8dcSSimon Schubert 
15845796c8dcSSimon Schubert static CORE_ADDR
read_encoded_value(struct comp_unit * unit,gdb_byte encoding,int ptr_len,const gdb_byte * buf,unsigned int * bytes_read_ptr,CORE_ADDR func_base)15855796c8dcSSimon Schubert read_encoded_value (struct comp_unit *unit, gdb_byte encoding,
1586cf7f2e2dSJohn Marino 		    int ptr_len, const gdb_byte *buf,
1587cf7f2e2dSJohn Marino 		    unsigned int *bytes_read_ptr,
15885796c8dcSSimon Schubert 		    CORE_ADDR func_base)
15895796c8dcSSimon Schubert {
15905796c8dcSSimon Schubert   ptrdiff_t offset;
15915796c8dcSSimon Schubert   CORE_ADDR base;
15925796c8dcSSimon Schubert 
15935796c8dcSSimon Schubert   /* GCC currently doesn't generate DW_EH_PE_indirect encodings for
15945796c8dcSSimon Schubert      FDE's.  */
15955796c8dcSSimon Schubert   if (encoding & DW_EH_PE_indirect)
15965796c8dcSSimon Schubert     internal_error (__FILE__, __LINE__,
15975796c8dcSSimon Schubert 		    _("Unsupported encoding: DW_EH_PE_indirect"));
15985796c8dcSSimon Schubert 
15995796c8dcSSimon Schubert   *bytes_read_ptr = 0;
16005796c8dcSSimon Schubert 
16015796c8dcSSimon Schubert   switch (encoding & 0x70)
16025796c8dcSSimon Schubert     {
16035796c8dcSSimon Schubert     case DW_EH_PE_absptr:
16045796c8dcSSimon Schubert       base = 0;
16055796c8dcSSimon Schubert       break;
16065796c8dcSSimon Schubert     case DW_EH_PE_pcrel:
16075796c8dcSSimon Schubert       base = bfd_get_section_vma (unit->abfd, unit->dwarf_frame_section);
16085796c8dcSSimon Schubert       base += (buf - unit->dwarf_frame_buffer);
16095796c8dcSSimon Schubert       break;
16105796c8dcSSimon Schubert     case DW_EH_PE_datarel:
16115796c8dcSSimon Schubert       base = unit->dbase;
16125796c8dcSSimon Schubert       break;
16135796c8dcSSimon Schubert     case DW_EH_PE_textrel:
16145796c8dcSSimon Schubert       base = unit->tbase;
16155796c8dcSSimon Schubert       break;
16165796c8dcSSimon Schubert     case DW_EH_PE_funcrel:
16175796c8dcSSimon Schubert       base = func_base;
16185796c8dcSSimon Schubert       break;
16195796c8dcSSimon Schubert     case DW_EH_PE_aligned:
16205796c8dcSSimon Schubert       base = 0;
16215796c8dcSSimon Schubert       offset = buf - unit->dwarf_frame_buffer;
16225796c8dcSSimon Schubert       if ((offset % ptr_len) != 0)
16235796c8dcSSimon Schubert 	{
16245796c8dcSSimon Schubert 	  *bytes_read_ptr = ptr_len - (offset % ptr_len);
16255796c8dcSSimon Schubert 	  buf += *bytes_read_ptr;
16265796c8dcSSimon Schubert 	}
16275796c8dcSSimon Schubert       break;
16285796c8dcSSimon Schubert     default:
1629c50c785cSJohn Marino       internal_error (__FILE__, __LINE__,
1630c50c785cSJohn Marino 		      _("Invalid or unsupported encoding"));
16315796c8dcSSimon Schubert     }
16325796c8dcSSimon Schubert 
16335796c8dcSSimon Schubert   if ((encoding & 0x07) == 0x00)
16345796c8dcSSimon Schubert     {
16355796c8dcSSimon Schubert       encoding |= encoding_for_size (ptr_len);
16365796c8dcSSimon Schubert       if (bfd_get_sign_extend_vma (unit->abfd))
16375796c8dcSSimon Schubert 	encoding |= DW_EH_PE_signed;
16385796c8dcSSimon Schubert     }
16395796c8dcSSimon Schubert 
16405796c8dcSSimon Schubert   switch (encoding & 0x0f)
16415796c8dcSSimon Schubert     {
16425796c8dcSSimon Schubert     case DW_EH_PE_uleb128:
16435796c8dcSSimon Schubert       {
1644*ef5ccd6cSJohn Marino 	uint64_t value;
1645cf7f2e2dSJohn Marino 	const gdb_byte *end_buf = buf + (sizeof (value) + 1) * 8 / 7;
1646cf7f2e2dSJohn Marino 
1647*ef5ccd6cSJohn Marino 	*bytes_read_ptr += safe_read_uleb128 (buf, end_buf, &value) - buf;
16485796c8dcSSimon Schubert 	return base + value;
16495796c8dcSSimon Schubert       }
16505796c8dcSSimon Schubert     case DW_EH_PE_udata2:
16515796c8dcSSimon Schubert       *bytes_read_ptr += 2;
16525796c8dcSSimon Schubert       return (base + bfd_get_16 (unit->abfd, (bfd_byte *) buf));
16535796c8dcSSimon Schubert     case DW_EH_PE_udata4:
16545796c8dcSSimon Schubert       *bytes_read_ptr += 4;
16555796c8dcSSimon Schubert       return (base + bfd_get_32 (unit->abfd, (bfd_byte *) buf));
16565796c8dcSSimon Schubert     case DW_EH_PE_udata8:
16575796c8dcSSimon Schubert       *bytes_read_ptr += 8;
16585796c8dcSSimon Schubert       return (base + bfd_get_64 (unit->abfd, (bfd_byte *) buf));
16595796c8dcSSimon Schubert     case DW_EH_PE_sleb128:
16605796c8dcSSimon Schubert       {
1661*ef5ccd6cSJohn Marino 	int64_t value;
1662cf7f2e2dSJohn Marino 	const gdb_byte *end_buf = buf + (sizeof (value) + 1) * 8 / 7;
1663cf7f2e2dSJohn Marino 
1664*ef5ccd6cSJohn Marino 	*bytes_read_ptr += safe_read_sleb128 (buf, end_buf, &value) - buf;
16655796c8dcSSimon Schubert 	return base + value;
16665796c8dcSSimon Schubert       }
16675796c8dcSSimon Schubert     case DW_EH_PE_sdata2:
16685796c8dcSSimon Schubert       *bytes_read_ptr += 2;
16695796c8dcSSimon Schubert       return (base + bfd_get_signed_16 (unit->abfd, (bfd_byte *) buf));
16705796c8dcSSimon Schubert     case DW_EH_PE_sdata4:
16715796c8dcSSimon Schubert       *bytes_read_ptr += 4;
16725796c8dcSSimon Schubert       return (base + bfd_get_signed_32 (unit->abfd, (bfd_byte *) buf));
16735796c8dcSSimon Schubert     case DW_EH_PE_sdata8:
16745796c8dcSSimon Schubert       *bytes_read_ptr += 8;
16755796c8dcSSimon Schubert       return (base + bfd_get_signed_64 (unit->abfd, (bfd_byte *) buf));
16765796c8dcSSimon Schubert     default:
1677c50c785cSJohn Marino       internal_error (__FILE__, __LINE__,
1678c50c785cSJohn Marino 		      _("Invalid or unsupported encoding"));
16795796c8dcSSimon Schubert     }
16805796c8dcSSimon Schubert }
16815796c8dcSSimon Schubert 
16825796c8dcSSimon Schubert 
16835796c8dcSSimon Schubert static int
bsearch_cie_cmp(const void * key,const void * element)16845796c8dcSSimon Schubert bsearch_cie_cmp (const void *key, const void *element)
16855796c8dcSSimon Schubert {
16865796c8dcSSimon Schubert   ULONGEST cie_pointer = *(ULONGEST *) key;
16875796c8dcSSimon Schubert   struct dwarf2_cie *cie = *(struct dwarf2_cie **) element;
16885796c8dcSSimon Schubert 
16895796c8dcSSimon Schubert   if (cie_pointer == cie->cie_pointer)
16905796c8dcSSimon Schubert     return 0;
16915796c8dcSSimon Schubert 
16925796c8dcSSimon Schubert   return (cie_pointer < cie->cie_pointer) ? -1 : 1;
16935796c8dcSSimon Schubert }
16945796c8dcSSimon Schubert 
16955796c8dcSSimon Schubert /* Find CIE with the given CIE_POINTER in CIE_TABLE.  */
16965796c8dcSSimon Schubert static struct dwarf2_cie *
find_cie(struct dwarf2_cie_table * cie_table,ULONGEST cie_pointer)16975796c8dcSSimon Schubert find_cie (struct dwarf2_cie_table *cie_table, ULONGEST cie_pointer)
16985796c8dcSSimon Schubert {
16995796c8dcSSimon Schubert   struct dwarf2_cie **p_cie;
17005796c8dcSSimon Schubert 
1701cf7f2e2dSJohn Marino   /* The C standard (ISO/IEC 9899:TC2) requires the BASE argument to
1702cf7f2e2dSJohn Marino      bsearch be non-NULL.  */
1703cf7f2e2dSJohn Marino   if (cie_table->entries == NULL)
1704cf7f2e2dSJohn Marino     {
1705cf7f2e2dSJohn Marino       gdb_assert (cie_table->num_entries == 0);
1706cf7f2e2dSJohn Marino       return NULL;
1707cf7f2e2dSJohn Marino     }
1708cf7f2e2dSJohn Marino 
17095796c8dcSSimon Schubert   p_cie = bsearch (&cie_pointer, cie_table->entries, cie_table->num_entries,
17105796c8dcSSimon Schubert                    sizeof (cie_table->entries[0]), bsearch_cie_cmp);
17115796c8dcSSimon Schubert   if (p_cie != NULL)
17125796c8dcSSimon Schubert     return *p_cie;
17135796c8dcSSimon Schubert   return NULL;
17145796c8dcSSimon Schubert }
17155796c8dcSSimon Schubert 
17165796c8dcSSimon Schubert /* Add a pointer to new CIE to the CIE_TABLE, allocating space for it.  */
17175796c8dcSSimon Schubert static void
add_cie(struct dwarf2_cie_table * cie_table,struct dwarf2_cie * cie)17185796c8dcSSimon Schubert add_cie (struct dwarf2_cie_table *cie_table, struct dwarf2_cie *cie)
17195796c8dcSSimon Schubert {
17205796c8dcSSimon Schubert   const int n = cie_table->num_entries;
17215796c8dcSSimon Schubert 
17225796c8dcSSimon Schubert   gdb_assert (n < 1
17235796c8dcSSimon Schubert               || cie_table->entries[n - 1]->cie_pointer < cie->cie_pointer);
17245796c8dcSSimon Schubert 
17255796c8dcSSimon Schubert   cie_table->entries =
17265796c8dcSSimon Schubert       xrealloc (cie_table->entries, (n + 1) * sizeof (cie_table->entries[0]));
17275796c8dcSSimon Schubert   cie_table->entries[n] = cie;
17285796c8dcSSimon Schubert   cie_table->num_entries = n + 1;
17295796c8dcSSimon Schubert }
17305796c8dcSSimon Schubert 
17315796c8dcSSimon Schubert static int
bsearch_fde_cmp(const void * key,const void * element)17325796c8dcSSimon Schubert bsearch_fde_cmp (const void *key, const void *element)
17335796c8dcSSimon Schubert {
17345796c8dcSSimon Schubert   CORE_ADDR seek_pc = *(CORE_ADDR *) key;
17355796c8dcSSimon Schubert   struct dwarf2_fde *fde = *(struct dwarf2_fde **) element;
1736cf7f2e2dSJohn Marino 
17375796c8dcSSimon Schubert   if (seek_pc < fde->initial_location)
17385796c8dcSSimon Schubert     return -1;
17395796c8dcSSimon Schubert   if (seek_pc < fde->initial_location + fde->address_range)
17405796c8dcSSimon Schubert     return 0;
17415796c8dcSSimon Schubert   return 1;
17425796c8dcSSimon Schubert }
17435796c8dcSSimon Schubert 
17445796c8dcSSimon Schubert /* Find the FDE for *PC.  Return a pointer to the FDE, and store the
17455796c8dcSSimon Schubert    inital location associated with it into *PC.  */
17465796c8dcSSimon Schubert 
17475796c8dcSSimon Schubert static struct dwarf2_fde *
dwarf2_frame_find_fde(CORE_ADDR * pc,CORE_ADDR * out_offset)1748cf7f2e2dSJohn Marino dwarf2_frame_find_fde (CORE_ADDR *pc, CORE_ADDR *out_offset)
17495796c8dcSSimon Schubert {
17505796c8dcSSimon Schubert   struct objfile *objfile;
17515796c8dcSSimon Schubert 
17525796c8dcSSimon Schubert   ALL_OBJFILES (objfile)
17535796c8dcSSimon Schubert     {
17545796c8dcSSimon Schubert       struct dwarf2_fde_table *fde_table;
17555796c8dcSSimon Schubert       struct dwarf2_fde **p_fde;
17565796c8dcSSimon Schubert       CORE_ADDR offset;
17575796c8dcSSimon Schubert       CORE_ADDR seek_pc;
17585796c8dcSSimon Schubert 
17595796c8dcSSimon Schubert       fde_table = objfile_data (objfile, dwarf2_frame_objfile_data);
17605796c8dcSSimon Schubert       if (fde_table == NULL)
1761cf7f2e2dSJohn Marino 	{
1762cf7f2e2dSJohn Marino 	  dwarf2_build_frame_info (objfile);
1763cf7f2e2dSJohn Marino 	  fde_table = objfile_data (objfile, dwarf2_frame_objfile_data);
1764cf7f2e2dSJohn Marino 	}
1765cf7f2e2dSJohn Marino       gdb_assert (fde_table != NULL);
1766cf7f2e2dSJohn Marino 
1767cf7f2e2dSJohn Marino       if (fde_table->num_entries == 0)
17685796c8dcSSimon Schubert 	continue;
17695796c8dcSSimon Schubert 
17705796c8dcSSimon Schubert       gdb_assert (objfile->section_offsets);
17715796c8dcSSimon Schubert       offset = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
17725796c8dcSSimon Schubert 
17735796c8dcSSimon Schubert       gdb_assert (fde_table->num_entries > 0);
17745796c8dcSSimon Schubert       if (*pc < offset + fde_table->entries[0]->initial_location)
17755796c8dcSSimon Schubert         continue;
17765796c8dcSSimon Schubert 
17775796c8dcSSimon Schubert       seek_pc = *pc - offset;
17785796c8dcSSimon Schubert       p_fde = bsearch (&seek_pc, fde_table->entries, fde_table->num_entries,
17795796c8dcSSimon Schubert                        sizeof (fde_table->entries[0]), bsearch_fde_cmp);
17805796c8dcSSimon Schubert       if (p_fde != NULL)
17815796c8dcSSimon Schubert         {
17825796c8dcSSimon Schubert           *pc = (*p_fde)->initial_location + offset;
1783cf7f2e2dSJohn Marino 	  if (out_offset)
1784cf7f2e2dSJohn Marino 	    *out_offset = offset;
17855796c8dcSSimon Schubert           return *p_fde;
17865796c8dcSSimon Schubert         }
17875796c8dcSSimon Schubert     }
17885796c8dcSSimon Schubert   return NULL;
17895796c8dcSSimon Schubert }
17905796c8dcSSimon Schubert 
17915796c8dcSSimon Schubert /* Add a pointer to new FDE to the FDE_TABLE, allocating space for it.  */
17925796c8dcSSimon Schubert static void
add_fde(struct dwarf2_fde_table * fde_table,struct dwarf2_fde * fde)17935796c8dcSSimon Schubert add_fde (struct dwarf2_fde_table *fde_table, struct dwarf2_fde *fde)
17945796c8dcSSimon Schubert {
17955796c8dcSSimon Schubert   if (fde->address_range == 0)
17965796c8dcSSimon Schubert     /* Discard useless FDEs.  */
17975796c8dcSSimon Schubert     return;
17985796c8dcSSimon Schubert 
17995796c8dcSSimon Schubert   fde_table->num_entries += 1;
18005796c8dcSSimon Schubert   fde_table->entries =
18015796c8dcSSimon Schubert       xrealloc (fde_table->entries,
18025796c8dcSSimon Schubert                 fde_table->num_entries * sizeof (fde_table->entries[0]));
18035796c8dcSSimon Schubert   fde_table->entries[fde_table->num_entries - 1] = fde;
18045796c8dcSSimon Schubert }
18055796c8dcSSimon Schubert 
18065796c8dcSSimon Schubert #define DW64_CIE_ID 0xffffffffffffffffULL
18075796c8dcSSimon Schubert 
1808a45ae5f8SJohn Marino /* Defines the type of eh_frames that are expected to be decoded: CIE, FDE
1809a45ae5f8SJohn Marino    or any of them.  */
1810a45ae5f8SJohn Marino 
1811a45ae5f8SJohn Marino enum eh_frame_type
1812a45ae5f8SJohn Marino {
1813a45ae5f8SJohn Marino   EH_CIE_TYPE_ID = 1 << 0,
1814a45ae5f8SJohn Marino   EH_FDE_TYPE_ID = 1 << 1,
1815a45ae5f8SJohn Marino   EH_CIE_OR_FDE_TYPE_ID = EH_CIE_TYPE_ID | EH_FDE_TYPE_ID
1816a45ae5f8SJohn Marino };
1817a45ae5f8SJohn Marino 
1818*ef5ccd6cSJohn Marino static const gdb_byte *decode_frame_entry (struct comp_unit *unit,
1819*ef5ccd6cSJohn Marino 					   const gdb_byte *start,
18205796c8dcSSimon Schubert 					   int eh_frame_p,
18215796c8dcSSimon Schubert 					   struct dwarf2_cie_table *cie_table,
1822a45ae5f8SJohn Marino 					   struct dwarf2_fde_table *fde_table,
1823a45ae5f8SJohn Marino 					   enum eh_frame_type entry_type);
18245796c8dcSSimon Schubert 
1825a45ae5f8SJohn Marino /* Decode the next CIE or FDE, entry_type specifies the expected type.
1826a45ae5f8SJohn Marino    Return NULL if invalid input, otherwise the next byte to be processed.  */
1827a45ae5f8SJohn Marino 
1828*ef5ccd6cSJohn Marino static const gdb_byte *
decode_frame_entry_1(struct comp_unit * unit,const gdb_byte * start,int eh_frame_p,struct dwarf2_cie_table * cie_table,struct dwarf2_fde_table * fde_table,enum eh_frame_type entry_type)1829*ef5ccd6cSJohn Marino decode_frame_entry_1 (struct comp_unit *unit, const gdb_byte *start,
1830*ef5ccd6cSJohn Marino 		      int eh_frame_p,
18315796c8dcSSimon Schubert                       struct dwarf2_cie_table *cie_table,
1832a45ae5f8SJohn Marino                       struct dwarf2_fde_table *fde_table,
1833a45ae5f8SJohn Marino                       enum eh_frame_type entry_type)
18345796c8dcSSimon Schubert {
18355796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_objfile_arch (unit->objfile);
1836*ef5ccd6cSJohn Marino   const gdb_byte *buf, *end;
18375796c8dcSSimon Schubert   LONGEST length;
18385796c8dcSSimon Schubert   unsigned int bytes_read;
18395796c8dcSSimon Schubert   int dwarf64_p;
18405796c8dcSSimon Schubert   ULONGEST cie_id;
18415796c8dcSSimon Schubert   ULONGEST cie_pointer;
1842*ef5ccd6cSJohn Marino   int64_t sleb128;
1843*ef5ccd6cSJohn Marino   uint64_t uleb128;
18445796c8dcSSimon Schubert 
18455796c8dcSSimon Schubert   buf = start;
18465796c8dcSSimon Schubert   length = read_initial_length (unit->abfd, buf, &bytes_read);
18475796c8dcSSimon Schubert   buf += bytes_read;
18485796c8dcSSimon Schubert   end = buf + length;
18495796c8dcSSimon Schubert 
18505796c8dcSSimon Schubert   /* Are we still within the section?  */
18515796c8dcSSimon Schubert   if (end > unit->dwarf_frame_buffer + unit->dwarf_frame_size)
18525796c8dcSSimon Schubert     return NULL;
18535796c8dcSSimon Schubert 
18545796c8dcSSimon Schubert   if (length == 0)
18555796c8dcSSimon Schubert     return end;
18565796c8dcSSimon Schubert 
18575796c8dcSSimon Schubert   /* Distinguish between 32 and 64-bit encoded frame info.  */
18585796c8dcSSimon Schubert   dwarf64_p = (bytes_read == 12);
18595796c8dcSSimon Schubert 
18605796c8dcSSimon Schubert   /* In a .eh_frame section, zero is used to distinguish CIEs from FDEs.  */
18615796c8dcSSimon Schubert   if (eh_frame_p)
18625796c8dcSSimon Schubert     cie_id = 0;
18635796c8dcSSimon Schubert   else if (dwarf64_p)
18645796c8dcSSimon Schubert     cie_id = DW64_CIE_ID;
18655796c8dcSSimon Schubert   else
18665796c8dcSSimon Schubert     cie_id = DW_CIE_ID;
18675796c8dcSSimon Schubert 
18685796c8dcSSimon Schubert   if (dwarf64_p)
18695796c8dcSSimon Schubert     {
18705796c8dcSSimon Schubert       cie_pointer = read_8_bytes (unit->abfd, buf);
18715796c8dcSSimon Schubert       buf += 8;
18725796c8dcSSimon Schubert     }
18735796c8dcSSimon Schubert   else
18745796c8dcSSimon Schubert     {
18755796c8dcSSimon Schubert       cie_pointer = read_4_bytes (unit->abfd, buf);
18765796c8dcSSimon Schubert       buf += 4;
18775796c8dcSSimon Schubert     }
18785796c8dcSSimon Schubert 
18795796c8dcSSimon Schubert   if (cie_pointer == cie_id)
18805796c8dcSSimon Schubert     {
18815796c8dcSSimon Schubert       /* This is a CIE.  */
18825796c8dcSSimon Schubert       struct dwarf2_cie *cie;
18835796c8dcSSimon Schubert       char *augmentation;
18845796c8dcSSimon Schubert       unsigned int cie_version;
18855796c8dcSSimon Schubert 
1886a45ae5f8SJohn Marino       /* Check that a CIE was expected.  */
1887a45ae5f8SJohn Marino       if ((entry_type & EH_CIE_TYPE_ID) == 0)
1888a45ae5f8SJohn Marino 	error (_("Found a CIE when not expecting it."));
1889a45ae5f8SJohn Marino 
18905796c8dcSSimon Schubert       /* Record the offset into the .debug_frame section of this CIE.  */
18915796c8dcSSimon Schubert       cie_pointer = start - unit->dwarf_frame_buffer;
18925796c8dcSSimon Schubert 
18935796c8dcSSimon Schubert       /* Check whether we've already read it.  */
18945796c8dcSSimon Schubert       if (find_cie (cie_table, cie_pointer))
18955796c8dcSSimon Schubert 	return end;
18965796c8dcSSimon Schubert 
18975796c8dcSSimon Schubert       cie = (struct dwarf2_cie *)
18985796c8dcSSimon Schubert 	obstack_alloc (&unit->objfile->objfile_obstack,
18995796c8dcSSimon Schubert 		       sizeof (struct dwarf2_cie));
19005796c8dcSSimon Schubert       cie->initial_instructions = NULL;
19015796c8dcSSimon Schubert       cie->cie_pointer = cie_pointer;
19025796c8dcSSimon Schubert 
19035796c8dcSSimon Schubert       /* The encoding for FDE's in a normal .debug_frame section
19045796c8dcSSimon Schubert          depends on the target address size.  */
19055796c8dcSSimon Schubert       cie->encoding = DW_EH_PE_absptr;
19065796c8dcSSimon Schubert 
19075796c8dcSSimon Schubert       /* We'll determine the final value later, but we need to
19085796c8dcSSimon Schubert 	 initialize it conservatively.  */
19095796c8dcSSimon Schubert       cie->signal_frame = 0;
19105796c8dcSSimon Schubert 
19115796c8dcSSimon Schubert       /* Check version number.  */
19125796c8dcSSimon Schubert       cie_version = read_1_byte (unit->abfd, buf);
1913cf7f2e2dSJohn Marino       if (cie_version != 1 && cie_version != 3 && cie_version != 4)
19145796c8dcSSimon Schubert 	return NULL;
19155796c8dcSSimon Schubert       cie->version = cie_version;
19165796c8dcSSimon Schubert       buf += 1;
19175796c8dcSSimon Schubert 
19185796c8dcSSimon Schubert       /* Interpret the interesting bits of the augmentation.  */
19195796c8dcSSimon Schubert       cie->augmentation = augmentation = (char *) buf;
19205796c8dcSSimon Schubert       buf += (strlen (augmentation) + 1);
19215796c8dcSSimon Schubert 
19225796c8dcSSimon Schubert       /* Ignore armcc augmentations.  We only use them for quirks,
19235796c8dcSSimon Schubert 	 and that doesn't happen until later.  */
19245796c8dcSSimon Schubert       if (strncmp (augmentation, "armcc", 5) == 0)
19255796c8dcSSimon Schubert 	augmentation += strlen (augmentation);
19265796c8dcSSimon Schubert 
19275796c8dcSSimon Schubert       /* The GCC 2.x "eh" augmentation has a pointer immediately
19285796c8dcSSimon Schubert          following the augmentation string, so it must be handled
19295796c8dcSSimon Schubert          first.  */
19305796c8dcSSimon Schubert       if (augmentation[0] == 'e' && augmentation[1] == 'h')
19315796c8dcSSimon Schubert 	{
19325796c8dcSSimon Schubert 	  /* Skip.  */
19335796c8dcSSimon Schubert 	  buf += gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT;
19345796c8dcSSimon Schubert 	  augmentation += 2;
19355796c8dcSSimon Schubert 	}
19365796c8dcSSimon Schubert 
1937cf7f2e2dSJohn Marino       if (cie->version >= 4)
1938cf7f2e2dSJohn Marino 	{
1939cf7f2e2dSJohn Marino 	  /* FIXME: check that this is the same as from the CU header.  */
1940cf7f2e2dSJohn Marino 	  cie->addr_size = read_1_byte (unit->abfd, buf);
1941cf7f2e2dSJohn Marino 	  ++buf;
1942cf7f2e2dSJohn Marino 	  cie->segment_size = read_1_byte (unit->abfd, buf);
1943cf7f2e2dSJohn Marino 	  ++buf;
1944cf7f2e2dSJohn Marino 	}
1945cf7f2e2dSJohn Marino       else
1946cf7f2e2dSJohn Marino 	{
1947c50c785cSJohn Marino 	  cie->addr_size = gdbarch_dwarf2_addr_size (gdbarch);
1948cf7f2e2dSJohn Marino 	  cie->segment_size = 0;
1949cf7f2e2dSJohn Marino 	}
1950c50c785cSJohn Marino       /* Address values in .eh_frame sections are defined to have the
1951c50c785cSJohn Marino 	 target's pointer size.  Watchout: This breaks frame info for
1952c50c785cSJohn Marino 	 targets with pointer size < address size, unless a .debug_frame
1953c50c785cSJohn Marino 	 section exists as well.  */
1954c50c785cSJohn Marino       if (eh_frame_p)
1955c50c785cSJohn Marino 	cie->ptr_size = gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT;
1956c50c785cSJohn Marino       else
1957c50c785cSJohn Marino 	cie->ptr_size = cie->addr_size;
1958cf7f2e2dSJohn Marino 
1959*ef5ccd6cSJohn Marino       buf = gdb_read_uleb128 (buf, end, &uleb128);
1960*ef5ccd6cSJohn Marino       if (buf == NULL)
1961*ef5ccd6cSJohn Marino 	return NULL;
1962*ef5ccd6cSJohn Marino       cie->code_alignment_factor = uleb128;
19635796c8dcSSimon Schubert 
1964*ef5ccd6cSJohn Marino       buf = gdb_read_sleb128 (buf, end, &sleb128);
1965*ef5ccd6cSJohn Marino       if (buf == NULL)
1966*ef5ccd6cSJohn Marino 	return NULL;
1967*ef5ccd6cSJohn Marino       cie->data_alignment_factor = sleb128;
19685796c8dcSSimon Schubert 
19695796c8dcSSimon Schubert       if (cie_version == 1)
19705796c8dcSSimon Schubert 	{
19715796c8dcSSimon Schubert 	  cie->return_address_register = read_1_byte (unit->abfd, buf);
1972*ef5ccd6cSJohn Marino 	  ++buf;
19735796c8dcSSimon Schubert 	}
19745796c8dcSSimon Schubert       else
1975*ef5ccd6cSJohn Marino 	{
1976*ef5ccd6cSJohn Marino 	  buf = gdb_read_uleb128 (buf, end, &uleb128);
1977*ef5ccd6cSJohn Marino 	  if (buf == NULL)
1978*ef5ccd6cSJohn Marino 	    return NULL;
1979*ef5ccd6cSJohn Marino 	  cie->return_address_register = uleb128;
1980*ef5ccd6cSJohn Marino 	}
1981*ef5ccd6cSJohn Marino 
19825796c8dcSSimon Schubert       cie->return_address_register
19835796c8dcSSimon Schubert 	= dwarf2_frame_adjust_regnum (gdbarch,
19845796c8dcSSimon Schubert 				      cie->return_address_register,
19855796c8dcSSimon Schubert 				      eh_frame_p);
19865796c8dcSSimon Schubert 
19875796c8dcSSimon Schubert       cie->saw_z_augmentation = (*augmentation == 'z');
19885796c8dcSSimon Schubert       if (cie->saw_z_augmentation)
19895796c8dcSSimon Schubert 	{
1990*ef5ccd6cSJohn Marino 	  uint64_t length;
19915796c8dcSSimon Schubert 
1992*ef5ccd6cSJohn Marino 	  buf = gdb_read_uleb128 (buf, end, &length);
1993*ef5ccd6cSJohn Marino 	  if (buf == NULL)
19945796c8dcSSimon Schubert 	    return NULL;
19955796c8dcSSimon Schubert 	  cie->initial_instructions = buf + length;
19965796c8dcSSimon Schubert 	  augmentation++;
19975796c8dcSSimon Schubert 	}
19985796c8dcSSimon Schubert 
19995796c8dcSSimon Schubert       while (*augmentation)
20005796c8dcSSimon Schubert 	{
20015796c8dcSSimon Schubert 	  /* "L" indicates a byte showing how the LSDA pointer is encoded.  */
20025796c8dcSSimon Schubert 	  if (*augmentation == 'L')
20035796c8dcSSimon Schubert 	    {
20045796c8dcSSimon Schubert 	      /* Skip.  */
20055796c8dcSSimon Schubert 	      buf++;
20065796c8dcSSimon Schubert 	      augmentation++;
20075796c8dcSSimon Schubert 	    }
20085796c8dcSSimon Schubert 
20095796c8dcSSimon Schubert 	  /* "R" indicates a byte indicating how FDE addresses are encoded.  */
20105796c8dcSSimon Schubert 	  else if (*augmentation == 'R')
20115796c8dcSSimon Schubert 	    {
20125796c8dcSSimon Schubert 	      cie->encoding = *buf++;
20135796c8dcSSimon Schubert 	      augmentation++;
20145796c8dcSSimon Schubert 	    }
20155796c8dcSSimon Schubert 
20165796c8dcSSimon Schubert 	  /* "P" indicates a personality routine in the CIE augmentation.  */
20175796c8dcSSimon Schubert 	  else if (*augmentation == 'P')
20185796c8dcSSimon Schubert 	    {
20195796c8dcSSimon Schubert 	      /* Skip.  Avoid indirection since we throw away the result.  */
20205796c8dcSSimon Schubert 	      gdb_byte encoding = (*buf++) & ~DW_EH_PE_indirect;
2021c50c785cSJohn Marino 	      read_encoded_value (unit, encoding, cie->ptr_size,
20225796c8dcSSimon Schubert 				  buf, &bytes_read, 0);
20235796c8dcSSimon Schubert 	      buf += bytes_read;
20245796c8dcSSimon Schubert 	      augmentation++;
20255796c8dcSSimon Schubert 	    }
20265796c8dcSSimon Schubert 
20275796c8dcSSimon Schubert 	  /* "S" indicates a signal frame, such that the return
20285796c8dcSSimon Schubert 	     address must not be decremented to locate the call frame
20295796c8dcSSimon Schubert 	     info for the previous frame; it might even be the first
20305796c8dcSSimon Schubert 	     instruction of a function, so decrementing it would take
20315796c8dcSSimon Schubert 	     us to a different function.  */
20325796c8dcSSimon Schubert 	  else if (*augmentation == 'S')
20335796c8dcSSimon Schubert 	    {
20345796c8dcSSimon Schubert 	      cie->signal_frame = 1;
20355796c8dcSSimon Schubert 	      augmentation++;
20365796c8dcSSimon Schubert 	    }
20375796c8dcSSimon Schubert 
20385796c8dcSSimon Schubert 	  /* Otherwise we have an unknown augmentation.  Assume that either
20395796c8dcSSimon Schubert 	     there is no augmentation data, or we saw a 'z' prefix.  */
20405796c8dcSSimon Schubert 	  else
20415796c8dcSSimon Schubert 	    {
20425796c8dcSSimon Schubert 	      if (cie->initial_instructions)
20435796c8dcSSimon Schubert 		buf = cie->initial_instructions;
20445796c8dcSSimon Schubert 	      break;
20455796c8dcSSimon Schubert 	    }
20465796c8dcSSimon Schubert 	}
20475796c8dcSSimon Schubert 
20485796c8dcSSimon Schubert       cie->initial_instructions = buf;
20495796c8dcSSimon Schubert       cie->end = end;
20505796c8dcSSimon Schubert       cie->unit = unit;
20515796c8dcSSimon Schubert 
20525796c8dcSSimon Schubert       add_cie (cie_table, cie);
20535796c8dcSSimon Schubert     }
20545796c8dcSSimon Schubert   else
20555796c8dcSSimon Schubert     {
20565796c8dcSSimon Schubert       /* This is a FDE.  */
20575796c8dcSSimon Schubert       struct dwarf2_fde *fde;
20585796c8dcSSimon Schubert 
2059a45ae5f8SJohn Marino       /* Check that an FDE was expected.  */
2060a45ae5f8SJohn Marino       if ((entry_type & EH_FDE_TYPE_ID) == 0)
2061a45ae5f8SJohn Marino 	error (_("Found an FDE when not expecting it."));
2062a45ae5f8SJohn Marino 
20635796c8dcSSimon Schubert       /* In an .eh_frame section, the CIE pointer is the delta between the
20645796c8dcSSimon Schubert 	 address within the FDE where the CIE pointer is stored and the
20655796c8dcSSimon Schubert 	 address of the CIE.  Convert it to an offset into the .eh_frame
20665796c8dcSSimon Schubert 	 section.  */
20675796c8dcSSimon Schubert       if (eh_frame_p)
20685796c8dcSSimon Schubert 	{
20695796c8dcSSimon Schubert 	  cie_pointer = buf - unit->dwarf_frame_buffer - cie_pointer;
20705796c8dcSSimon Schubert 	  cie_pointer -= (dwarf64_p ? 8 : 4);
20715796c8dcSSimon Schubert 	}
20725796c8dcSSimon Schubert 
20735796c8dcSSimon Schubert       /* In either case, validate the result is still within the section.  */
20745796c8dcSSimon Schubert       if (cie_pointer >= unit->dwarf_frame_size)
20755796c8dcSSimon Schubert 	return NULL;
20765796c8dcSSimon Schubert 
20775796c8dcSSimon Schubert       fde = (struct dwarf2_fde *)
20785796c8dcSSimon Schubert 	obstack_alloc (&unit->objfile->objfile_obstack,
20795796c8dcSSimon Schubert 		       sizeof (struct dwarf2_fde));
20805796c8dcSSimon Schubert       fde->cie = find_cie (cie_table, cie_pointer);
20815796c8dcSSimon Schubert       if (fde->cie == NULL)
20825796c8dcSSimon Schubert 	{
20835796c8dcSSimon Schubert 	  decode_frame_entry (unit, unit->dwarf_frame_buffer + cie_pointer,
2084a45ae5f8SJohn Marino 			      eh_frame_p, cie_table, fde_table,
2085a45ae5f8SJohn Marino 			      EH_CIE_TYPE_ID);
20865796c8dcSSimon Schubert 	  fde->cie = find_cie (cie_table, cie_pointer);
20875796c8dcSSimon Schubert 	}
20885796c8dcSSimon Schubert 
20895796c8dcSSimon Schubert       gdb_assert (fde->cie != NULL);
20905796c8dcSSimon Schubert 
20915796c8dcSSimon Schubert       fde->initial_location =
2092c50c785cSJohn Marino 	read_encoded_value (unit, fde->cie->encoding, fde->cie->ptr_size,
20935796c8dcSSimon Schubert 			    buf, &bytes_read, 0);
20945796c8dcSSimon Schubert       buf += bytes_read;
20955796c8dcSSimon Schubert 
20965796c8dcSSimon Schubert       fde->address_range =
20975796c8dcSSimon Schubert 	read_encoded_value (unit, fde->cie->encoding & 0x0f,
2098c50c785cSJohn Marino 			    fde->cie->ptr_size, buf, &bytes_read, 0);
20995796c8dcSSimon Schubert       buf += bytes_read;
21005796c8dcSSimon Schubert 
21015796c8dcSSimon Schubert       /* A 'z' augmentation in the CIE implies the presence of an
21025796c8dcSSimon Schubert 	 augmentation field in the FDE as well.  The only thing known
21035796c8dcSSimon Schubert 	 to be in here at present is the LSDA entry for EH.  So we
21045796c8dcSSimon Schubert 	 can skip the whole thing.  */
21055796c8dcSSimon Schubert       if (fde->cie->saw_z_augmentation)
21065796c8dcSSimon Schubert 	{
2107*ef5ccd6cSJohn Marino 	  uint64_t length;
21085796c8dcSSimon Schubert 
2109*ef5ccd6cSJohn Marino 	  buf = gdb_read_uleb128 (buf, end, &length);
2110*ef5ccd6cSJohn Marino 	  if (buf == NULL)
2111*ef5ccd6cSJohn Marino 	    return NULL;
2112*ef5ccd6cSJohn Marino 	  buf += length;
21135796c8dcSSimon Schubert 	  if (buf > end)
21145796c8dcSSimon Schubert 	    return NULL;
21155796c8dcSSimon Schubert 	}
21165796c8dcSSimon Schubert 
21175796c8dcSSimon Schubert       fde->instructions = buf;
21185796c8dcSSimon Schubert       fde->end = end;
21195796c8dcSSimon Schubert 
21205796c8dcSSimon Schubert       fde->eh_frame_p = eh_frame_p;
21215796c8dcSSimon Schubert 
21225796c8dcSSimon Schubert       add_fde (fde_table, fde);
21235796c8dcSSimon Schubert     }
21245796c8dcSSimon Schubert 
21255796c8dcSSimon Schubert   return end;
21265796c8dcSSimon Schubert }
21275796c8dcSSimon Schubert 
2128a45ae5f8SJohn Marino /* Read a CIE or FDE in BUF and decode it. Entry_type specifies whether we
2129a45ae5f8SJohn Marino    expect an FDE or a CIE.  */
2130a45ae5f8SJohn Marino 
2131*ef5ccd6cSJohn Marino static const gdb_byte *
decode_frame_entry(struct comp_unit * unit,const gdb_byte * start,int eh_frame_p,struct dwarf2_cie_table * cie_table,struct dwarf2_fde_table * fde_table,enum eh_frame_type entry_type)2132*ef5ccd6cSJohn Marino decode_frame_entry (struct comp_unit *unit, const gdb_byte *start,
2133*ef5ccd6cSJohn Marino 		    int eh_frame_p,
21345796c8dcSSimon Schubert                     struct dwarf2_cie_table *cie_table,
2135a45ae5f8SJohn Marino                     struct dwarf2_fde_table *fde_table,
2136a45ae5f8SJohn Marino                     enum eh_frame_type entry_type)
21375796c8dcSSimon Schubert {
21385796c8dcSSimon Schubert   enum { NONE, ALIGN4, ALIGN8, FAIL } workaround = NONE;
2139*ef5ccd6cSJohn Marino   const gdb_byte *ret;
21405796c8dcSSimon Schubert   ptrdiff_t start_offset;
21415796c8dcSSimon Schubert 
21425796c8dcSSimon Schubert   while (1)
21435796c8dcSSimon Schubert     {
21445796c8dcSSimon Schubert       ret = decode_frame_entry_1 (unit, start, eh_frame_p,
2145a45ae5f8SJohn Marino 				  cie_table, fde_table, entry_type);
21465796c8dcSSimon Schubert       if (ret != NULL)
21475796c8dcSSimon Schubert 	break;
21485796c8dcSSimon Schubert 
21495796c8dcSSimon Schubert       /* We have corrupt input data of some form.  */
21505796c8dcSSimon Schubert 
21515796c8dcSSimon Schubert       /* ??? Try, weakly, to work around compiler/assembler/linker bugs
21525796c8dcSSimon Schubert 	 and mismatches wrt padding and alignment of debug sections.  */
21535796c8dcSSimon Schubert       /* Note that there is no requirement in the standard for any
21545796c8dcSSimon Schubert 	 alignment at all in the frame unwind sections.  Testing for
21555796c8dcSSimon Schubert 	 alignment before trying to interpret data would be incorrect.
21565796c8dcSSimon Schubert 
21575796c8dcSSimon Schubert 	 However, GCC traditionally arranged for frame sections to be
21585796c8dcSSimon Schubert 	 sized such that the FDE length and CIE fields happen to be
21595796c8dcSSimon Schubert 	 aligned (in theory, for performance).  This, unfortunately,
21605796c8dcSSimon Schubert 	 was done with .align directives, which had the side effect of
21615796c8dcSSimon Schubert 	 forcing the section to be aligned by the linker.
21625796c8dcSSimon Schubert 
21635796c8dcSSimon Schubert 	 This becomes a problem when you have some other producer that
21645796c8dcSSimon Schubert 	 creates frame sections that are not as strictly aligned.  That
21655796c8dcSSimon Schubert 	 produces a hole in the frame info that gets filled by the
21665796c8dcSSimon Schubert 	 linker with zeros.
21675796c8dcSSimon Schubert 
21685796c8dcSSimon Schubert 	 The GCC behaviour is arguably a bug, but it's effectively now
21695796c8dcSSimon Schubert 	 part of the ABI, so we're now stuck with it, at least at the
21705796c8dcSSimon Schubert 	 object file level.  A smart linker may decide, in the process
21715796c8dcSSimon Schubert 	 of compressing duplicate CIE information, that it can rewrite
21725796c8dcSSimon Schubert 	 the entire output section without this extra padding.  */
21735796c8dcSSimon Schubert 
21745796c8dcSSimon Schubert       start_offset = start - unit->dwarf_frame_buffer;
21755796c8dcSSimon Schubert       if (workaround < ALIGN4 && (start_offset & 3) != 0)
21765796c8dcSSimon Schubert 	{
21775796c8dcSSimon Schubert 	  start += 4 - (start_offset & 3);
21785796c8dcSSimon Schubert 	  workaround = ALIGN4;
21795796c8dcSSimon Schubert 	  continue;
21805796c8dcSSimon Schubert 	}
21815796c8dcSSimon Schubert       if (workaround < ALIGN8 && (start_offset & 7) != 0)
21825796c8dcSSimon Schubert 	{
21835796c8dcSSimon Schubert 	  start += 8 - (start_offset & 7);
21845796c8dcSSimon Schubert 	  workaround = ALIGN8;
21855796c8dcSSimon Schubert 	  continue;
21865796c8dcSSimon Schubert 	}
21875796c8dcSSimon Schubert 
21885796c8dcSSimon Schubert       /* Nothing left to try.  Arrange to return as if we've consumed
21895796c8dcSSimon Schubert 	 the entire input section.  Hopefully we'll get valid info from
21905796c8dcSSimon Schubert 	 the other of .debug_frame/.eh_frame.  */
21915796c8dcSSimon Schubert       workaround = FAIL;
21925796c8dcSSimon Schubert       ret = unit->dwarf_frame_buffer + unit->dwarf_frame_size;
21935796c8dcSSimon Schubert       break;
21945796c8dcSSimon Schubert     }
21955796c8dcSSimon Schubert 
21965796c8dcSSimon Schubert   switch (workaround)
21975796c8dcSSimon Schubert     {
21985796c8dcSSimon Schubert     case NONE:
21995796c8dcSSimon Schubert       break;
22005796c8dcSSimon Schubert 
22015796c8dcSSimon Schubert     case ALIGN4:
2202c50c785cSJohn Marino       complaint (&symfile_complaints, _("\
2203c50c785cSJohn Marino Corrupt data in %s:%s; align 4 workaround apparently succeeded"),
22045796c8dcSSimon Schubert 		 unit->dwarf_frame_section->owner->filename,
22055796c8dcSSimon Schubert 		 unit->dwarf_frame_section->name);
22065796c8dcSSimon Schubert       break;
22075796c8dcSSimon Schubert 
22085796c8dcSSimon Schubert     case ALIGN8:
2209c50c785cSJohn Marino       complaint (&symfile_complaints, _("\
2210c50c785cSJohn Marino Corrupt data in %s:%s; align 8 workaround apparently succeeded"),
22115796c8dcSSimon Schubert 		 unit->dwarf_frame_section->owner->filename,
22125796c8dcSSimon Schubert 		 unit->dwarf_frame_section->name);
22135796c8dcSSimon Schubert       break;
22145796c8dcSSimon Schubert 
22155796c8dcSSimon Schubert     default:
22165796c8dcSSimon Schubert       complaint (&symfile_complaints,
22175796c8dcSSimon Schubert 		 _("Corrupt data in %s:%s"),
22185796c8dcSSimon Schubert 		 unit->dwarf_frame_section->owner->filename,
22195796c8dcSSimon Schubert 		 unit->dwarf_frame_section->name);
22205796c8dcSSimon Schubert       break;
22215796c8dcSSimon Schubert     }
22225796c8dcSSimon Schubert 
22235796c8dcSSimon Schubert   return ret;
22245796c8dcSSimon Schubert }
22255796c8dcSSimon Schubert 
22265796c8dcSSimon Schubert static int
qsort_fde_cmp(const void * a,const void * b)22275796c8dcSSimon Schubert qsort_fde_cmp (const void *a, const void *b)
22285796c8dcSSimon Schubert {
22295796c8dcSSimon Schubert   struct dwarf2_fde *aa = *(struct dwarf2_fde **)a;
22305796c8dcSSimon Schubert   struct dwarf2_fde *bb = *(struct dwarf2_fde **)b;
22315796c8dcSSimon Schubert 
22325796c8dcSSimon Schubert   if (aa->initial_location == bb->initial_location)
22335796c8dcSSimon Schubert     {
22345796c8dcSSimon Schubert       if (aa->address_range != bb->address_range
22355796c8dcSSimon Schubert           && aa->eh_frame_p == 0 && bb->eh_frame_p == 0)
22365796c8dcSSimon Schubert         /* Linker bug, e.g. gold/10400.
22375796c8dcSSimon Schubert            Work around it by keeping stable sort order.  */
22385796c8dcSSimon Schubert         return (a < b) ? -1 : 1;
22395796c8dcSSimon Schubert       else
22405796c8dcSSimon Schubert         /* Put eh_frame entries after debug_frame ones.  */
22415796c8dcSSimon Schubert         return aa->eh_frame_p - bb->eh_frame_p;
22425796c8dcSSimon Schubert     }
22435796c8dcSSimon Schubert 
22445796c8dcSSimon Schubert   return (aa->initial_location < bb->initial_location) ? -1 : 1;
22455796c8dcSSimon Schubert }
22465796c8dcSSimon Schubert 
22475796c8dcSSimon Schubert void
dwarf2_build_frame_info(struct objfile * objfile)22485796c8dcSSimon Schubert dwarf2_build_frame_info (struct objfile *objfile)
22495796c8dcSSimon Schubert {
22505796c8dcSSimon Schubert   struct comp_unit *unit;
2251*ef5ccd6cSJohn Marino   const gdb_byte *frame_ptr;
22525796c8dcSSimon Schubert   struct dwarf2_cie_table cie_table;
22535796c8dcSSimon Schubert   struct dwarf2_fde_table fde_table;
2254cf7f2e2dSJohn Marino   struct dwarf2_fde_table *fde_table2;
2255a45ae5f8SJohn Marino   volatile struct gdb_exception e;
22565796c8dcSSimon Schubert 
22575796c8dcSSimon Schubert   cie_table.num_entries = 0;
22585796c8dcSSimon Schubert   cie_table.entries = NULL;
22595796c8dcSSimon Schubert 
22605796c8dcSSimon Schubert   fde_table.num_entries = 0;
22615796c8dcSSimon Schubert   fde_table.entries = NULL;
22625796c8dcSSimon Schubert 
22635796c8dcSSimon Schubert   /* Build a minimal decoding of the DWARF2 compilation unit.  */
22645796c8dcSSimon Schubert   unit = (struct comp_unit *) obstack_alloc (&objfile->objfile_obstack,
22655796c8dcSSimon Schubert 					     sizeof (struct comp_unit));
22665796c8dcSSimon Schubert   unit->abfd = objfile->obfd;
22675796c8dcSSimon Schubert   unit->objfile = objfile;
22685796c8dcSSimon Schubert   unit->dbase = 0;
22695796c8dcSSimon Schubert   unit->tbase = 0;
22705796c8dcSSimon Schubert 
2271a45ae5f8SJohn Marino   if (objfile->separate_debug_objfile_backlink == NULL)
2272a45ae5f8SJohn Marino     {
2273a45ae5f8SJohn Marino       /* Do not read .eh_frame from separate file as they must be also
2274a45ae5f8SJohn Marino          present in the main file.  */
2275a45ae5f8SJohn Marino       dwarf2_get_section_info (objfile, DWARF2_EH_FRAME,
22765796c8dcSSimon Schubert                                &unit->dwarf_frame_section,
22775796c8dcSSimon Schubert                                &unit->dwarf_frame_buffer,
22785796c8dcSSimon Schubert                                &unit->dwarf_frame_size);
22795796c8dcSSimon Schubert       if (unit->dwarf_frame_size)
22805796c8dcSSimon Schubert         {
22815796c8dcSSimon Schubert           asection *got, *txt;
22825796c8dcSSimon Schubert 
22835796c8dcSSimon Schubert           /* FIXME: kettenis/20030602: This is the DW_EH_PE_datarel base
22845796c8dcSSimon Schubert              that is used for the i386/amd64 target, which currently is
22855796c8dcSSimon Schubert              the only target in GCC that supports/uses the
22865796c8dcSSimon Schubert              DW_EH_PE_datarel encoding.  */
22875796c8dcSSimon Schubert           got = bfd_get_section_by_name (unit->abfd, ".got");
22885796c8dcSSimon Schubert           if (got)
22895796c8dcSSimon Schubert             unit->dbase = got->vma;
22905796c8dcSSimon Schubert 
22915796c8dcSSimon Schubert           /* GCC emits the DW_EH_PE_textrel encoding type on sh and ia64
22925796c8dcSSimon Schubert              so far.  */
22935796c8dcSSimon Schubert           txt = bfd_get_section_by_name (unit->abfd, ".text");
22945796c8dcSSimon Schubert           if (txt)
22955796c8dcSSimon Schubert             unit->tbase = txt->vma;
22965796c8dcSSimon Schubert 
2297a45ae5f8SJohn Marino 	  TRY_CATCH (e, RETURN_MASK_ERROR)
2298a45ae5f8SJohn Marino 	    {
22995796c8dcSSimon Schubert 	      frame_ptr = unit->dwarf_frame_buffer;
23005796c8dcSSimon Schubert 	      while (frame_ptr < unit->dwarf_frame_buffer + unit->dwarf_frame_size)
23015796c8dcSSimon Schubert 		frame_ptr = decode_frame_entry (unit, frame_ptr, 1,
2302a45ae5f8SJohn Marino 						&cie_table, &fde_table,
2303a45ae5f8SJohn Marino 						EH_CIE_OR_FDE_TYPE_ID);
2304a45ae5f8SJohn Marino 	    }
2305a45ae5f8SJohn Marino 
2306a45ae5f8SJohn Marino 	  if (e.reason < 0)
2307a45ae5f8SJohn Marino 	    {
2308a45ae5f8SJohn Marino 	      warning (_("skipping .eh_frame info of %s: %s"),
2309a45ae5f8SJohn Marino 		       objfile->name, e.message);
2310a45ae5f8SJohn Marino 
2311a45ae5f8SJohn Marino 	      if (fde_table.num_entries != 0)
2312a45ae5f8SJohn Marino 		{
2313a45ae5f8SJohn Marino                   xfree (fde_table.entries);
2314a45ae5f8SJohn Marino 		  fde_table.entries = NULL;
2315a45ae5f8SJohn Marino 		  fde_table.num_entries = 0;
2316a45ae5f8SJohn Marino 		}
2317a45ae5f8SJohn Marino 	      /* The cie_table is discarded by the next if.  */
2318a45ae5f8SJohn Marino 	    }
23195796c8dcSSimon Schubert 
23205796c8dcSSimon Schubert           if (cie_table.num_entries != 0)
23215796c8dcSSimon Schubert             {
23225796c8dcSSimon Schubert               /* Reinit cie_table: debug_frame has different CIEs.  */
23235796c8dcSSimon Schubert               xfree (cie_table.entries);
23245796c8dcSSimon Schubert               cie_table.num_entries = 0;
23255796c8dcSSimon Schubert               cie_table.entries = NULL;
23265796c8dcSSimon Schubert             }
23275796c8dcSSimon Schubert         }
2328a45ae5f8SJohn Marino     }
23295796c8dcSSimon Schubert 
2330a45ae5f8SJohn Marino   dwarf2_get_section_info (objfile, DWARF2_DEBUG_FRAME,
23315796c8dcSSimon Schubert                            &unit->dwarf_frame_section,
23325796c8dcSSimon Schubert                            &unit->dwarf_frame_buffer,
23335796c8dcSSimon Schubert                            &unit->dwarf_frame_size);
23345796c8dcSSimon Schubert   if (unit->dwarf_frame_size)
23355796c8dcSSimon Schubert     {
2336a45ae5f8SJohn Marino       int num_old_fde_entries = fde_table.num_entries;
2337a45ae5f8SJohn Marino 
2338a45ae5f8SJohn Marino       TRY_CATCH (e, RETURN_MASK_ERROR)
2339a45ae5f8SJohn Marino 	{
23405796c8dcSSimon Schubert 	  frame_ptr = unit->dwarf_frame_buffer;
23415796c8dcSSimon Schubert 	  while (frame_ptr < unit->dwarf_frame_buffer + unit->dwarf_frame_size)
23425796c8dcSSimon Schubert 	    frame_ptr = decode_frame_entry (unit, frame_ptr, 0,
2343a45ae5f8SJohn Marino 					    &cie_table, &fde_table,
2344a45ae5f8SJohn Marino 					    EH_CIE_OR_FDE_TYPE_ID);
2345a45ae5f8SJohn Marino 	}
2346a45ae5f8SJohn Marino       if (e.reason < 0)
2347a45ae5f8SJohn Marino 	{
2348a45ae5f8SJohn Marino 	  warning (_("skipping .debug_frame info of %s: %s"),
2349a45ae5f8SJohn Marino 		   objfile->name, e.message);
2350a45ae5f8SJohn Marino 
2351a45ae5f8SJohn Marino 	  if (fde_table.num_entries != 0)
2352a45ae5f8SJohn Marino 	    {
2353a45ae5f8SJohn Marino 	      fde_table.num_entries = num_old_fde_entries;
2354a45ae5f8SJohn Marino 	      if (num_old_fde_entries == 0)
2355a45ae5f8SJohn Marino 		{
2356a45ae5f8SJohn Marino 		  xfree (fde_table.entries);
2357a45ae5f8SJohn Marino 		  fde_table.entries = NULL;
2358a45ae5f8SJohn Marino 		}
2359a45ae5f8SJohn Marino 	      else
2360a45ae5f8SJohn Marino 		{
2361a45ae5f8SJohn Marino 		  fde_table.entries = xrealloc (fde_table.entries,
2362a45ae5f8SJohn Marino 						fde_table.num_entries *
2363a45ae5f8SJohn Marino 						sizeof (fde_table.entries[0]));
2364a45ae5f8SJohn Marino 		}
2365a45ae5f8SJohn Marino 	    }
2366a45ae5f8SJohn Marino 	  fde_table.num_entries = num_old_fde_entries;
2367a45ae5f8SJohn Marino 	  /* The cie_table is discarded by the next if.  */
2368a45ae5f8SJohn Marino 	}
23695796c8dcSSimon Schubert     }
23705796c8dcSSimon Schubert 
23715796c8dcSSimon Schubert   /* Discard the cie_table, it is no longer needed.  */
23725796c8dcSSimon Schubert   if (cie_table.num_entries != 0)
23735796c8dcSSimon Schubert     {
23745796c8dcSSimon Schubert       xfree (cie_table.entries);
23755796c8dcSSimon Schubert       cie_table.entries = NULL;   /* Paranoia.  */
23765796c8dcSSimon Schubert       cie_table.num_entries = 0;  /* Paranoia.  */
23775796c8dcSSimon Schubert     }
23785796c8dcSSimon Schubert 
2379cf7f2e2dSJohn Marino   /* Copy fde_table to obstack: it is needed at runtime.  */
2380cf7f2e2dSJohn Marino   fde_table2 = (struct dwarf2_fde_table *)
2381cf7f2e2dSJohn Marino     obstack_alloc (&objfile->objfile_obstack, sizeof (*fde_table2));
2382cf7f2e2dSJohn Marino 
2383cf7f2e2dSJohn Marino   if (fde_table.num_entries == 0)
23845796c8dcSSimon Schubert     {
2385cf7f2e2dSJohn Marino       fde_table2->entries = NULL;
2386cf7f2e2dSJohn Marino       fde_table2->num_entries = 0;
2387cf7f2e2dSJohn Marino     }
2388cf7f2e2dSJohn Marino   else
2389cf7f2e2dSJohn Marino     {
2390cf7f2e2dSJohn Marino       struct dwarf2_fde *fde_prev = NULL;
2391cf7f2e2dSJohn Marino       struct dwarf2_fde *first_non_zero_fde = NULL;
2392cf7f2e2dSJohn Marino       int i;
23935796c8dcSSimon Schubert 
23945796c8dcSSimon Schubert       /* Prepare FDE table for lookups.  */
23955796c8dcSSimon Schubert       qsort (fde_table.entries, fde_table.num_entries,
23965796c8dcSSimon Schubert              sizeof (fde_table.entries[0]), qsort_fde_cmp);
23975796c8dcSSimon Schubert 
2398cf7f2e2dSJohn Marino       /* Check for leftovers from --gc-sections.  The GNU linker sets
2399cf7f2e2dSJohn Marino 	 the relevant symbols to zero, but doesn't zero the FDE *end*
2400cf7f2e2dSJohn Marino 	 ranges because there's no relocation there.  It's (offset,
2401cf7f2e2dSJohn Marino 	 length), not (start, end).  On targets where address zero is
2402cf7f2e2dSJohn Marino 	 just another valid address this can be a problem, since the
2403cf7f2e2dSJohn Marino 	 FDEs appear to be non-empty in the output --- we could pick
2404cf7f2e2dSJohn Marino 	 out the wrong FDE.  To work around this, when overlaps are
2405cf7f2e2dSJohn Marino 	 detected, we prefer FDEs that do not start at zero.
24065796c8dcSSimon Schubert 
2407cf7f2e2dSJohn Marino 	 Start by finding the first FDE with non-zero start.  Below
2408cf7f2e2dSJohn Marino 	 we'll discard all FDEs that start at zero and overlap this
2409cf7f2e2dSJohn Marino 	 one.  */
2410cf7f2e2dSJohn Marino       for (i = 0; i < fde_table.num_entries; i++)
24115796c8dcSSimon Schubert 	{
2412cf7f2e2dSJohn Marino 	  struct dwarf2_fde *fde = fde_table.entries[i];
24135796c8dcSSimon Schubert 
2414cf7f2e2dSJohn Marino 	  if (fde->initial_location != 0)
2415cf7f2e2dSJohn Marino 	    {
2416cf7f2e2dSJohn Marino 	      first_non_zero_fde = fde;
2417cf7f2e2dSJohn Marino 	      break;
2418cf7f2e2dSJohn Marino 	    }
2419cf7f2e2dSJohn Marino 	}
2420cf7f2e2dSJohn Marino 
2421cf7f2e2dSJohn Marino       /* Since we'll be doing bsearch, squeeze out identical (except
2422cf7f2e2dSJohn Marino 	 for eh_frame_p) fde entries so bsearch result is predictable.
2423cf7f2e2dSJohn Marino 	 Also discard leftovers from --gc-sections.  */
2424cf7f2e2dSJohn Marino       fde_table2->num_entries = 0;
2425cf7f2e2dSJohn Marino       for (i = 0; i < fde_table.num_entries; i++)
2426cf7f2e2dSJohn Marino 	{
2427cf7f2e2dSJohn Marino 	  struct dwarf2_fde *fde = fde_table.entries[i];
2428cf7f2e2dSJohn Marino 
2429cf7f2e2dSJohn Marino 	  if (fde->initial_location == 0
2430cf7f2e2dSJohn Marino 	      && first_non_zero_fde != NULL
2431cf7f2e2dSJohn Marino 	      && (first_non_zero_fde->initial_location
2432cf7f2e2dSJohn Marino 		  < fde->initial_location + fde->address_range))
2433cf7f2e2dSJohn Marino 	    continue;
2434cf7f2e2dSJohn Marino 
2435cf7f2e2dSJohn Marino 	  if (fde_prev != NULL
2436cf7f2e2dSJohn Marino 	      && fde_prev->initial_location == fde->initial_location)
2437cf7f2e2dSJohn Marino 	    continue;
2438cf7f2e2dSJohn Marino 
2439cf7f2e2dSJohn Marino 	  obstack_grow (&objfile->objfile_obstack, &fde_table.entries[i],
24405796c8dcSSimon Schubert 			sizeof (fde_table.entries[0]));
2441cf7f2e2dSJohn Marino 	  ++fde_table2->num_entries;
2442cf7f2e2dSJohn Marino 	  fde_prev = fde;
24435796c8dcSSimon Schubert 	}
24445796c8dcSSimon Schubert       fde_table2->entries = obstack_finish (&objfile->objfile_obstack);
24455796c8dcSSimon Schubert 
24465796c8dcSSimon Schubert       /* Discard the original fde_table.  */
24475796c8dcSSimon Schubert       xfree (fde_table.entries);
24485796c8dcSSimon Schubert     }
2449cf7f2e2dSJohn Marino 
2450cf7f2e2dSJohn Marino   set_objfile_data (objfile, dwarf2_frame_objfile_data, fde_table2);
24515796c8dcSSimon Schubert }
24525796c8dcSSimon Schubert 
24535796c8dcSSimon Schubert /* Provide a prototype to silence -Wmissing-prototypes.  */
24545796c8dcSSimon Schubert void _initialize_dwarf2_frame (void);
24555796c8dcSSimon Schubert 
24565796c8dcSSimon Schubert void
_initialize_dwarf2_frame(void)24575796c8dcSSimon Schubert _initialize_dwarf2_frame (void)
24585796c8dcSSimon Schubert {
24595796c8dcSSimon Schubert   dwarf2_frame_data = gdbarch_data_register_pre_init (dwarf2_frame_init);
24605796c8dcSSimon Schubert   dwarf2_frame_objfile_data = register_objfile_data ();
24615796c8dcSSimon Schubert }
2462