gdb/dwarf2-frame.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Frame unwinder for frames with DWARF Call Frame Information.

  2.    Copyright (C) 2003-2015 Free Software Foundation, Inc.

  3.    Contributed by Mark Kettenis.

  4.    This file is part of GDB.

  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 3 of the License, or
  8.    (at your option) any later version.

  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.

  13.    You should have received a copy of the GNU General Public License
  14.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  15. #include "defs.h"
  16. #include "dwarf2expr.h"
  17. #include "dwarf2.h"
  18. #include "frame.h"
  19. #include "frame-base.h"
  20. #include "frame-unwind.h"
  21. #include "gdbcore.h"
  22. #include "gdbtypes.h"
  23. #include "symtab.h"
  24. #include "objfiles.h"
  25. #include "regcache.h"
  26. #include "value.h"
  27. #include "record.h"

  28. #include "complaints.h"
  29. #include "dwarf2-frame.h"
  30. #include "ax.h"
  31. #include "dwarf2loc.h"
  32. #include "dwarf2-frame-tailcall.h"

  33. struct comp_unit;

  34. /* Call Frame Information (CFI).  */

  35. /* Common Information Entry (CIE).  */

  36. struct dwarf2_cie
  37. {
  38.   /* Computation Unit for this CIE.  */
  39.   struct comp_unit *unit;

  40.   /* Offset into the .debug_frame section where this CIE was found.
  41.      Used to identify this CIE.  */
  42.   ULONGEST cie_pointer;

  43.   /* Constant that is factored out of all advance location
  44.      instructions.  */
  45.   ULONGEST code_alignment_factor;

  46.   /* Constants that is factored out of all offset instructions.  */
  47.   LONGEST data_alignment_factor;

  48.   /* Return address column.  */
  49.   ULONGEST return_address_register;

  50.   /* Instruction sequence to initialize a register set.  */
  51.   const gdb_byte *initial_instructions;
  52.   const gdb_byte *end;

  53.   /* Saved augmentation, in case it's needed later.  */
  54.   char *augmentation;

  55.   /* Encoding of addresses.  */
  56.   gdb_byte encoding;

  57.   /* Target address size in bytes.  */
  58.   int addr_size;

  59.   /* Target pointer size in bytes.  */
  60.   int ptr_size;

  61.   /* True if a 'z' augmentation existed.  */
  62.   unsigned char saw_z_augmentation;

  63.   /* True if an 'S' augmentation existed.  */
  64.   unsigned char signal_frame;

  65.   /* The version recorded in the CIE.  */
  66.   unsigned char version;

  67.   /* The segment size.  */
  68.   unsigned char segment_size;
  69. };

  70. struct dwarf2_cie_table
  71. {
  72.   int num_entries;
  73.   struct dwarf2_cie **entries;
  74. };

  75. /* Frame Description Entry (FDE).  */

  76. struct dwarf2_fde
  77. {
  78.   /* CIE for this FDE.  */
  79.   struct dwarf2_cie *cie;

  80.   /* First location associated with this FDE.  */
  81.   CORE_ADDR initial_location;

  82.   /* Number of bytes of program instructions described by this FDE.  */
  83.   CORE_ADDR address_range;

  84.   /* Instruction sequence.  */
  85.   const gdb_byte *instructions;
  86.   const gdb_byte *end;

  87.   /* True if this FDE is read from a .eh_frame instead of a .debug_frame
  88.      section.  */
  89.   unsigned char eh_frame_p;
  90. };

  91. struct dwarf2_fde_table
  92. {
  93.   int num_entries;
  94.   struct dwarf2_fde **entries;
  95. };

  96. /* A minimal decoding of DWARF2 compilation units.  We only decode
  97.    what's needed to get to the call frame information.  */

  98. struct comp_unit
  99. {
  100.   /* Keep the bfd convenient.  */
  101.   bfd *abfd;

  102.   struct objfile *objfile;

  103.   /* Pointer to the .debug_frame section loaded into memory.  */
  104.   const gdb_byte *dwarf_frame_buffer;

  105.   /* Length of the loaded .debug_frame section.  */
  106.   bfd_size_type dwarf_frame_size;

  107.   /* Pointer to the .debug_frame section.  */
  108.   asection *dwarf_frame_section;

  109.   /* Base for DW_EH_PE_datarel encodings.  */
  110.   bfd_vma dbase;

  111.   /* Base for DW_EH_PE_textrel encodings.  */
  112.   bfd_vma tbase;
  113. };

  114. static struct dwarf2_fde *dwarf2_frame_find_fde (CORE_ADDR *pc,
  115.                                                  CORE_ADDR *out_offset);

  116. static int dwarf2_frame_adjust_regnum (struct gdbarch *gdbarch, int regnum,
  117.                                        int eh_frame_p);

  118. static CORE_ADDR read_encoded_value (struct comp_unit *unit, gdb_byte encoding,
  119.                                      int ptr_len, const gdb_byte *buf,
  120.                                      unsigned int *bytes_read_ptr,
  121.                                      CORE_ADDR func_base);


  122. /* Structure describing a frame state.  */

  123. struct dwarf2_frame_state
  124. {
  125.   /* Each register save state can be described in terms of a CFA slot,
  126.      another register, or a location expression.  */
  127.   struct dwarf2_frame_state_reg_info
  128.   {
  129.     struct dwarf2_frame_state_reg *reg;
  130.     int num_regs;

  131.     LONGEST cfa_offset;
  132.     ULONGEST cfa_reg;
  133.     enum {
  134.       CFA_UNSET,
  135.       CFA_REG_OFFSET,
  136.       CFA_EXP
  137.     } cfa_how;
  138.     const gdb_byte *cfa_exp;

  139.     /* Used to implement DW_CFA_remember_state.  */
  140.     struct dwarf2_frame_state_reg_info *prev;
  141.   } regs;

  142.   /* The PC described by the current frame state.  */
  143.   CORE_ADDR pc;

  144.   /* Initial register set from the CIE.
  145.      Used to implement DW_CFA_restore.  */
  146.   struct dwarf2_frame_state_reg_info initial;

  147.   /* The information we care about from the CIE.  */
  148.   LONGEST data_align;
  149.   ULONGEST code_align;
  150.   ULONGEST retaddr_column;

  151.   /* Flags for known producer quirks.  */

  152.   /* The ARM compilers, in DWARF2 mode, assume that DW_CFA_def_cfa
  153.      and DW_CFA_def_cfa_offset takes a factored offset.  */
  154.   int armcc_cfa_offsets_sf;

  155.   /* The ARM compilers, in DWARF2 or DWARF3 mode, may assume that
  156.      the CFA is defined as REG - OFFSET rather than REG + OFFSET.  */
  157.   int armcc_cfa_offsets_reversed;
  158. };

  159. /* Store the length the expression for the CFA in the `cfa_reg' field,
  160.    which is unused in that case.  */
  161. #define cfa_exp_len cfa_reg

  162. /* Assert that the register set RS is large enough to store gdbarch_num_regs
  163.    columns.  If necessary, enlarge the register set.  */

  164. static void
  165. dwarf2_frame_state_alloc_regs (struct dwarf2_frame_state_reg_info *rs,
  166.                                int num_regs)
  167. {
  168.   size_t size = sizeof (struct dwarf2_frame_state_reg);

  169.   if (num_regs <= rs->num_regs)
  170.     return;

  171.   rs->reg = (struct dwarf2_frame_state_reg *)
  172.     xrealloc (rs->reg, num_regs * size);

  173.   /* Initialize newly allocated registers.  */
  174.   memset (rs->reg + rs->num_regs, 0, (num_regs - rs->num_regs) * size);
  175.   rs->num_regs = num_regs;
  176. }

  177. /* Copy the register columns in register set RS into newly allocated
  178.    memory and return a pointer to this newly created copy.  */

  179. static struct dwarf2_frame_state_reg *
  180. dwarf2_frame_state_copy_regs (struct dwarf2_frame_state_reg_info *rs)
  181. {
  182.   size_t size = rs->num_regs * sizeof (struct dwarf2_frame_state_reg);
  183.   struct dwarf2_frame_state_reg *reg;

  184.   reg = (struct dwarf2_frame_state_reg *) xmalloc (size);
  185.   memcpy (reg, rs->reg, size);

  186.   return reg;
  187. }

  188. /* Release the memory allocated to register set RS.  */

  189. static void
  190. dwarf2_frame_state_free_regs (struct dwarf2_frame_state_reg_info *rs)
  191. {
  192.   if (rs)
  193.     {
  194.       dwarf2_frame_state_free_regs (rs->prev);

  195.       xfree (rs->reg);
  196.       xfree (rs);
  197.     }
  198. }

  199. /* Release the memory allocated to the frame state FS.  */

  200. static void
  201. dwarf2_frame_state_free (void *p)
  202. {
  203.   struct dwarf2_frame_state *fs = p;

  204.   dwarf2_frame_state_free_regs (fs->initial.prev);
  205.   dwarf2_frame_state_free_regs (fs->regs.prev);
  206.   xfree (fs->initial.reg);
  207.   xfree (fs->regs.reg);
  208.   xfree (fs);
  209. }


  210. /* Helper functions for execute_stack_op.  */

  211. static CORE_ADDR
  212. read_addr_from_reg (void *baton, int reg)
  213. {
  214.   struct frame_info *this_frame = (struct frame_info *) baton;
  215.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  216.   int regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, reg);

  217.   return address_from_register (regnum, this_frame);
  218. }

  219. /* Implement struct dwarf_expr_context_funcs' "get_reg_value" callback.  */

  220. static struct value *
  221. get_reg_value (void *baton, struct type *type, int reg)
  222. {
  223.   struct frame_info *this_frame = (struct frame_info *) baton;
  224.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  225.   int regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, reg);

  226.   return value_from_register (type, regnum, this_frame);
  227. }

  228. static void
  229. read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
  230. {
  231.   read_memory (addr, buf, len);
  232. }

  233. /* Execute the required actions for both the DW_CFA_restore and
  234. DW_CFA_restore_extended instructions.  */
  235. static void
  236. dwarf2_restore_rule (struct gdbarch *gdbarch, ULONGEST reg_num,
  237.                      struct dwarf2_frame_state *fs, int eh_frame_p)
  238. {
  239.   ULONGEST reg;

  240.   gdb_assert (fs->initial.reg);
  241.   reg = dwarf2_frame_adjust_regnum (gdbarch, reg_num, eh_frame_p);
  242.   dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);

  243.   /* Check if this register was explicitly initialized in the
  244.   CIE initial instructions.  If not, default the rule to
  245.   UNSPECIFIED.  */
  246.   if (reg < fs->initial.num_regs)
  247.     fs->regs.reg[reg] = fs->initial.reg[reg];
  248.   else
  249.     fs->regs.reg[reg].how = DWARF2_FRAME_REG_UNSPECIFIED;

  250.   if (fs->regs.reg[reg].how == DWARF2_FRAME_REG_UNSPECIFIED)
  251.     complaint (&symfile_complaints, _("\
  252. incomplete CFI data; DW_CFA_restore unspecified\n\
  253. register %s (#%d) at %s"),
  254.                        gdbarch_register_name
  255.                        (gdbarch, gdbarch_dwarf2_reg_to_regnum (gdbarch, reg)),
  256.                        gdbarch_dwarf2_reg_to_regnum (gdbarch, reg),
  257.                        paddress (gdbarch, fs->pc));
  258. }

  259. /* Virtual method table for execute_stack_op below.  */

  260. static const struct dwarf_expr_context_funcs dwarf2_frame_ctx_funcs =
  261. {
  262.   read_addr_from_reg,
  263.   get_reg_value,
  264.   read_mem,
  265.   ctx_no_get_frame_base,
  266.   ctx_no_get_frame_cfa,
  267.   ctx_no_get_frame_pc,
  268.   ctx_no_get_tls_address,
  269.   ctx_no_dwarf_call,
  270.   ctx_no_get_base_type,
  271.   ctx_no_push_dwarf_reg_entry_value,
  272.   ctx_no_get_addr_index
  273. };

  274. static CORE_ADDR
  275. execute_stack_op (const gdb_byte *exp, ULONGEST len, int addr_size,
  276.                   CORE_ADDR offset, struct frame_info *this_frame,
  277.                   CORE_ADDR initial, int initial_in_stack_memory)
  278. {
  279.   struct dwarf_expr_context *ctx;
  280.   CORE_ADDR result;
  281.   struct cleanup *old_chain;

  282.   ctx = new_dwarf_expr_context ();
  283.   old_chain = make_cleanup_free_dwarf_expr_context (ctx);
  284.   make_cleanup_value_free_to_mark (value_mark ());

  285.   ctx->gdbarch = get_frame_arch (this_frame);
  286.   ctx->addr_size = addr_size;
  287.   ctx->ref_addr_size = -1;
  288.   ctx->offset = offset;
  289.   ctx->baton = this_frame;
  290.   ctx->funcs = &dwarf2_frame_ctx_funcs;

  291.   dwarf_expr_push_address (ctx, initial, initial_in_stack_memory);
  292.   dwarf_expr_eval (ctx, exp, len);

  293.   if (ctx->location == DWARF_VALUE_MEMORY)
  294.     result = dwarf_expr_fetch_address (ctx, 0);
  295.   else if (ctx->location == DWARF_VALUE_REGISTER)
  296.     result = read_addr_from_reg (this_frame,
  297.                                  value_as_long (dwarf_expr_fetch (ctx, 0)));
  298.   else
  299.     {
  300.       /* This is actually invalid DWARF, but if we ever do run across
  301.          it somehow, we might as well support it.  So, instead, report
  302.          it as unimplemented.  */
  303.       error (_("\
  304. Not implemented: computing unwound register using explicit value operator"));
  305.     }

  306.   do_cleanups (old_chain);

  307.   return result;
  308. }


  309. /* Execute FDE program from INSN_PTR possibly up to INSN_END or up to inferior
  310.    PC.  Modify FS state accordingly.  Return current INSN_PTR where the
  311.    execution has stopped, one can resume it on the next call.  */

  312. static const gdb_byte *
  313. execute_cfa_program (struct dwarf2_fde *fde, const gdb_byte *insn_ptr,
  314.                      const gdb_byte *insn_end, struct gdbarch *gdbarch,
  315.                      CORE_ADDR pc, struct dwarf2_frame_state *fs)
  316. {
  317.   int eh_frame_p = fde->eh_frame_p;
  318.   unsigned int bytes_read;
  319.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);

  320.   while (insn_ptr < insn_end && fs->pc <= pc)
  321.     {
  322.       gdb_byte insn = *insn_ptr++;
  323.       uint64_t utmp, reg;
  324.       int64_t offset;

  325.       if ((insn & 0xc0) == DW_CFA_advance_loc)
  326.         fs->pc += (insn & 0x3f) * fs->code_align;
  327.       else if ((insn & 0xc0) == DW_CFA_offset)
  328.         {
  329.           reg = insn & 0x3f;
  330.           reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
  331.           insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
  332.           offset = utmp * fs->data_align;
  333.           dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
  334.           fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
  335.           fs->regs.reg[reg].loc.offset = offset;
  336.         }
  337.       else if ((insn & 0xc0) == DW_CFA_restore)
  338.         {
  339.           reg = insn & 0x3f;
  340.           dwarf2_restore_rule (gdbarch, reg, fs, eh_frame_p);
  341.         }
  342.       else
  343.         {
  344.           switch (insn)
  345.             {
  346.             case DW_CFA_set_loc:
  347.               fs->pc = read_encoded_value (fde->cie->unit, fde->cie->encoding,
  348.                                            fde->cie->ptr_size, insn_ptr,
  349.                                            &bytes_read, fde->initial_location);
  350.               /* Apply the objfile offset for relocatable objects.  */
  351.               fs->pc += ANOFFSET (fde->cie->unit->objfile->section_offsets,
  352.                                   SECT_OFF_TEXT (fde->cie->unit->objfile));
  353.               insn_ptr += bytes_read;
  354.               break;

  355.             case DW_CFA_advance_loc1:
  356.               utmp = extract_unsigned_integer (insn_ptr, 1, byte_order);
  357.               fs->pc += utmp * fs->code_align;
  358.               insn_ptr++;
  359.               break;
  360.             case DW_CFA_advance_loc2:
  361.               utmp = extract_unsigned_integer (insn_ptr, 2, byte_order);
  362.               fs->pc += utmp * fs->code_align;
  363.               insn_ptr += 2;
  364.               break;
  365.             case DW_CFA_advance_loc4:
  366.               utmp = extract_unsigned_integer (insn_ptr, 4, byte_order);
  367.               fs->pc += utmp * fs->code_align;
  368.               insn_ptr += 4;
  369.               break;

  370.             case DW_CFA_offset_extended:
  371.               insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
  372.               reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
  373.               insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
  374.               offset = utmp * fs->data_align;
  375.               dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
  376.               fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
  377.               fs->regs.reg[reg].loc.offset = offset;
  378.               break;

  379.             case DW_CFA_restore_extended:
  380.               insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
  381.               dwarf2_restore_rule (gdbarch, reg, fs, eh_frame_p);
  382.               break;

  383.             case DW_CFA_undefined:
  384.               insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
  385.               reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
  386.               dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
  387.               fs->regs.reg[reg].how = DWARF2_FRAME_REG_UNDEFINED;
  388.               break;

  389.             case DW_CFA_same_value:
  390.               insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
  391.               reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
  392.               dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
  393.               fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAME_VALUE;
  394.               break;

  395.             case DW_CFA_register:
  396.               insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
  397.               reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
  398.               insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
  399.               utmp = dwarf2_frame_adjust_regnum (gdbarch, utmp, eh_frame_p);
  400.               dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
  401.               fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_REG;
  402.               fs->regs.reg[reg].loc.reg = utmp;
  403.               break;

  404.             case DW_CFA_remember_state:
  405.               {
  406.                 struct dwarf2_frame_state_reg_info *new_rs;

  407.                 new_rs = XNEW (struct dwarf2_frame_state_reg_info);
  408.                 *new_rs = fs->regs;
  409.                 fs->regs.reg = dwarf2_frame_state_copy_regs (&fs->regs);
  410.                 fs->regs.prev = new_rs;
  411.               }
  412.               break;

  413.             case DW_CFA_restore_state:
  414.               {
  415.                 struct dwarf2_frame_state_reg_info *old_rs = fs->regs.prev;

  416.                 if (old_rs == NULL)
  417.                   {
  418.                     complaint (&symfile_complaints, _("\
  419. bad CFI data; mismatched DW_CFA_restore_state at %s"),
  420.                                paddress (gdbarch, fs->pc));
  421.                   }
  422.                 else
  423.                   {
  424.                     xfree (fs->regs.reg);
  425.                     fs->regs = *old_rs;
  426.                     xfree (old_rs);
  427.                   }
  428.               }
  429.               break;

  430.             case DW_CFA_def_cfa:
  431.               insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
  432.               fs->regs.cfa_reg = reg;
  433.               insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);

  434.               if (fs->armcc_cfa_offsets_sf)
  435.                 utmp *= fs->data_align;

  436.               fs->regs.cfa_offset = utmp;
  437.               fs->regs.cfa_how = CFA_REG_OFFSET;
  438.               break;

  439.             case DW_CFA_def_cfa_register:
  440.               insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
  441.               fs->regs.cfa_reg = dwarf2_frame_adjust_regnum (gdbarch, reg,
  442.                                                              eh_frame_p);
  443.               fs->regs.cfa_how = CFA_REG_OFFSET;
  444.               break;

  445.             case DW_CFA_def_cfa_offset:
  446.               insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);

  447.               if (fs->armcc_cfa_offsets_sf)
  448.                 utmp *= fs->data_align;

  449.               fs->regs.cfa_offset = utmp;
  450.               /* cfa_how deliberately not set.  */
  451.               break;

  452.             case DW_CFA_nop:
  453.               break;

  454.             case DW_CFA_def_cfa_expression:
  455.               insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
  456.               fs->regs.cfa_exp_len = utmp;
  457.               fs->regs.cfa_exp = insn_ptr;
  458.               fs->regs.cfa_how = CFA_EXP;
  459.               insn_ptr += fs->regs.cfa_exp_len;
  460.               break;

  461.             case DW_CFA_expression:
  462.               insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
  463.               reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
  464.               dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
  465.               insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
  466.               fs->regs.reg[reg].loc.exp = insn_ptr;
  467.               fs->regs.reg[reg].exp_len = utmp;
  468.               fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_EXP;
  469.               insn_ptr += utmp;
  470.               break;

  471.             case DW_CFA_offset_extended_sf:
  472.               insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
  473.               reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
  474.               insn_ptr = safe_read_sleb128 (insn_ptr, insn_end, &offset);
  475.               offset *= fs->data_align;
  476.               dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
  477.               fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
  478.               fs->regs.reg[reg].loc.offset = offset;
  479.               break;

  480.             case DW_CFA_val_offset:
  481.               insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
  482.               dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
  483.               insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
  484.               offset = utmp * fs->data_align;
  485.               fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_VAL_OFFSET;
  486.               fs->regs.reg[reg].loc.offset = offset;
  487.               break;

  488.             case DW_CFA_val_offset_sf:
  489.               insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
  490.               dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
  491.               insn_ptr = safe_read_sleb128 (insn_ptr, insn_end, &offset);
  492.               offset *= fs->data_align;
  493.               fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_VAL_OFFSET;
  494.               fs->regs.reg[reg].loc.offset = offset;
  495.               break;

  496.             case DW_CFA_val_expression:
  497.               insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
  498.               dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
  499.               insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
  500.               fs->regs.reg[reg].loc.exp = insn_ptr;
  501.               fs->regs.reg[reg].exp_len = utmp;
  502.               fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_VAL_EXP;
  503.               insn_ptr += utmp;
  504.               break;

  505.             case DW_CFA_def_cfa_sf:
  506.               insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
  507.               fs->regs.cfa_reg = dwarf2_frame_adjust_regnum (gdbarch, reg,
  508.                                                              eh_frame_p);
  509.               insn_ptr = safe_read_sleb128 (insn_ptr, insn_end, &offset);
  510.               fs->regs.cfa_offset = offset * fs->data_align;
  511.               fs->regs.cfa_how = CFA_REG_OFFSET;
  512.               break;

  513.             case DW_CFA_def_cfa_offset_sf:
  514.               insn_ptr = safe_read_sleb128 (insn_ptr, insn_end, &offset);
  515.               fs->regs.cfa_offset = offset * fs->data_align;
  516.               /* cfa_how deliberately not set.  */
  517.               break;

  518.             case DW_CFA_GNU_window_save:
  519.               /* This is SPARC-specific code, and contains hard-coded
  520.                  constants for the register numbering scheme used by
  521.                  GCC.  Rather than having a architecture-specific
  522.                  operation that's only ever used by a single
  523.                  architecture, we provide the implementation here.
  524.                  Incidentally that's what GCC does too in its
  525.                  unwinder.  */
  526.               {
  527.                 int size = register_size (gdbarch, 0);

  528.                 dwarf2_frame_state_alloc_regs (&fs->regs, 32);
  529.                 for (reg = 8; reg < 16; reg++)
  530.                   {
  531.                     fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_REG;
  532.                     fs->regs.reg[reg].loc.reg = reg + 16;
  533.                   }
  534.                 for (reg = 16; reg < 32; reg++)
  535.                   {
  536.                     fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
  537.                     fs->regs.reg[reg].loc.offset = (reg - 16) * size;
  538.                   }
  539.               }
  540.               break;

  541.             case DW_CFA_GNU_args_size:
  542.               /* Ignored.  */
  543.               insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
  544.               break;

  545.             case DW_CFA_GNU_negative_offset_extended:
  546.               insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
  547.               reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
  548.               insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
  549.               offset = utmp * fs->data_align;
  550.               dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
  551.               fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
  552.               fs->regs.reg[reg].loc.offset = -offset;
  553.               break;

  554.             default:
  555.               internal_error (__FILE__, __LINE__,
  556.                               _("Unknown CFI encountered."));
  557.             }
  558.         }
  559.     }

  560.   if (fs->initial.reg == NULL)
  561.     {
  562.       /* Don't allow remember/restore between CIE and FDE programs.  */
  563.       dwarf2_frame_state_free_regs (fs->regs.prev);
  564.       fs->regs.prev = NULL;
  565.     }

  566.   return insn_ptr;
  567. }


  568. /* Architecture-specific operations.  */

  569. /* Per-architecture data key.  */
  570. static struct gdbarch_data *dwarf2_frame_data;

  571. struct dwarf2_frame_ops
  572. {
  573.   /* Pre-initialize the register state REG for register REGNUM.  */
  574.   void (*init_reg) (struct gdbarch *, int, struct dwarf2_frame_state_reg *,
  575.                     struct frame_info *);

  576.   /* Check whether the THIS_FRAME is a signal trampoline.  */
  577.   int (*signal_frame_p) (struct gdbarch *, struct frame_info *);

  578.   /* Convert .eh_frame register number to DWARF register number, or
  579.      adjust .debug_frame register number.  */
  580.   int (*adjust_regnum) (struct gdbarch *, int, int);
  581. };

  582. /* Default architecture-specific register state initialization
  583.    function.  */

  584. static void
  585. dwarf2_frame_default_init_reg (struct gdbarch *gdbarch, int regnum,
  586.                                struct dwarf2_frame_state_reg *reg,
  587.                                struct frame_info *this_frame)
  588. {
  589.   /* If we have a register that acts as a program counter, mark it as
  590.      a destination for the return address.  If we have a register that
  591.      serves as the stack pointer, arrange for it to be filled with the
  592.      call frame address (CFA).  The other registers are marked as
  593.      unspecified.

  594.      We copy the return address to the program counter, since many
  595.      parts in GDB assume that it is possible to get the return address
  596.      by unwinding the program counter register.  However, on ISA's
  597.      with a dedicated return address register, the CFI usually only
  598.      contains information to unwind that return address register.

  599.      The reason we're treating the stack pointer special here is
  600.      because in many cases GCC doesn't emit CFI for the stack pointer
  601.      and implicitly assumes that it is equal to the CFA.  This makes
  602.      some sense since the DWARF specification (version 3, draft 8,
  603.      p. 102) says that:

  604.      "Typically, the CFA is defined to be the value of the stack
  605.      pointer at the call site in the previous frame (which may be
  606.      different from its value on entry to the current frame)."

  607.      However, this isn't true for all platforms supported by GCC
  608.      (e.g. IBM S/390 and zSeries).  Those architectures should provide
  609.      their own architecture-specific initialization function.  */

  610.   if (regnum == gdbarch_pc_regnum (gdbarch))
  611.     reg->how = DWARF2_FRAME_REG_RA;
  612.   else if (regnum == gdbarch_sp_regnum (gdbarch))
  613.     reg->how = DWARF2_FRAME_REG_CFA;
  614. }

  615. /* Return a default for the architecture-specific operations.  */

  616. static void *
  617. dwarf2_frame_init (struct obstack *obstack)
  618. {
  619.   struct dwarf2_frame_ops *ops;

  620.   ops = OBSTACK_ZALLOC (obstack, struct dwarf2_frame_ops);
  621.   ops->init_reg = dwarf2_frame_default_init_reg;
  622.   return ops;
  623. }

  624. /* Set the architecture-specific register state initialization
  625.    function for GDBARCH to INIT_REG.  */

  626. void
  627. dwarf2_frame_set_init_reg (struct gdbarch *gdbarch,
  628.                            void (*init_reg) (struct gdbarch *, int,
  629.                                              struct dwarf2_frame_state_reg *,
  630.                                              struct frame_info *))
  631. {
  632.   struct dwarf2_frame_ops *ops = gdbarch_data (gdbarch, dwarf2_frame_data);

  633.   ops->init_reg = init_reg;
  634. }

  635. /* Pre-initialize the register state REG for register REGNUM.  */

  636. static void
  637. dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
  638.                        struct dwarf2_frame_state_reg *reg,
  639.                        struct frame_info *this_frame)
  640. {
  641.   struct dwarf2_frame_ops *ops = gdbarch_data (gdbarch, dwarf2_frame_data);

  642.   ops->init_reg (gdbarch, regnum, reg, this_frame);
  643. }

  644. /* Set the architecture-specific signal trampoline recognition
  645.    function for GDBARCH to SIGNAL_FRAME_P.  */

  646. void
  647. dwarf2_frame_set_signal_frame_p (struct gdbarch *gdbarch,
  648.                                  int (*signal_frame_p) (struct gdbarch *,
  649.                                                         struct frame_info *))
  650. {
  651.   struct dwarf2_frame_ops *ops = gdbarch_data (gdbarch, dwarf2_frame_data);

  652.   ops->signal_frame_p = signal_frame_p;
  653. }

  654. /* Query the architecture-specific signal frame recognizer for
  655.    THIS_FRAME.  */

  656. static int
  657. dwarf2_frame_signal_frame_p (struct gdbarch *gdbarch,
  658.                              struct frame_info *this_frame)
  659. {
  660.   struct dwarf2_frame_ops *ops = gdbarch_data (gdbarch, dwarf2_frame_data);

  661.   if (ops->signal_frame_p == NULL)
  662.     return 0;
  663.   return ops->signal_frame_p (gdbarch, this_frame);
  664. }

  665. /* Set the architecture-specific adjustment of .eh_frame and .debug_frame
  666.    register numbers.  */

  667. void
  668. dwarf2_frame_set_adjust_regnum (struct gdbarch *gdbarch,
  669.                                 int (*adjust_regnum) (struct gdbarch *,
  670.                                                       int, int))
  671. {
  672.   struct dwarf2_frame_ops *ops = gdbarch_data (gdbarch, dwarf2_frame_data);

  673.   ops->adjust_regnum = adjust_regnum;
  674. }

  675. /* Translate a .eh_frame register to DWARF register, or adjust a .debug_frame
  676.    register.  */

  677. static int
  678. dwarf2_frame_adjust_regnum (struct gdbarch *gdbarch,
  679.                             int regnum, int eh_frame_p)
  680. {
  681.   struct dwarf2_frame_ops *ops = gdbarch_data (gdbarch, dwarf2_frame_data);

  682.   if (ops->adjust_regnum == NULL)
  683.     return regnum;
  684.   return ops->adjust_regnum (gdbarch, regnum, eh_frame_p);
  685. }

  686. static void
  687. dwarf2_frame_find_quirks (struct dwarf2_frame_state *fs,
  688.                           struct dwarf2_fde *fde)
  689. {
  690.   struct compunit_symtab *cust;

  691.   cust = find_pc_compunit_symtab (fs->pc);
  692.   if (cust == NULL)
  693.     return;

  694.   if (producer_is_realview (COMPUNIT_PRODUCER (cust)))
  695.     {
  696.       if (fde->cie->version == 1)
  697.         fs->armcc_cfa_offsets_sf = 1;

  698.       if (fde->cie->version == 1)
  699.         fs->armcc_cfa_offsets_reversed = 1;

  700.       /* The reversed offset problem is present in some compilers
  701.          using DWARF3, but it was eventually fixed.  Check the ARM
  702.          defined augmentations, which are in the format "armcc" followed
  703.          by a list of one-character options.  The "+" option means
  704.          this problem is fixed (no quirk needed).  If the armcc
  705.          augmentation is missing, the quirk is needed.  */
  706.       if (fde->cie->version == 3
  707.           && (strncmp (fde->cie->augmentation, "armcc", 5) != 0
  708.               || strchr (fde->cie->augmentation + 5, '+') == NULL))
  709.         fs->armcc_cfa_offsets_reversed = 1;

  710.       return;
  711.     }
  712. }


  713. /* See dwarf2-frame.h.  */

  714. int
  715. dwarf2_fetch_cfa_info (struct gdbarch *gdbarch, CORE_ADDR pc,
  716.                        struct dwarf2_per_cu_data *data,
  717.                        int *regnum_out, LONGEST *offset_out,
  718.                        CORE_ADDR *text_offset_out,
  719.                        const gdb_byte **cfa_start_out,
  720.                        const gdb_byte **cfa_end_out)
  721. {
  722.   struct dwarf2_fde *fde;
  723.   CORE_ADDR text_offset;
  724.   struct dwarf2_frame_state fs;
  725.   int addr_size;

  726.   memset (&fs, 0, sizeof (struct dwarf2_frame_state));

  727.   fs.pc = pc;

  728.   /* Find the correct FDE.  */
  729.   fde = dwarf2_frame_find_fde (&fs.pc, &text_offset);
  730.   if (fde == NULL)
  731.     error (_("Could not compute CFA; needed to translate this expression"));

  732.   /* Extract any interesting information from the CIE.  */
  733.   fs.data_align = fde->cie->data_alignment_factor;
  734.   fs.code_align = fde->cie->code_alignment_factor;
  735.   fs.retaddr_column = fde->cie->return_address_register;
  736.   addr_size = fde->cie->addr_size;

  737.   /* Check for "quirks" - known bugs in producers.  */
  738.   dwarf2_frame_find_quirks (&fs, fde);

  739.   /* First decode all the insns in the CIE.  */
  740.   execute_cfa_program (fde, fde->cie->initial_instructions,
  741.                        fde->cie->end, gdbarch, pc, &fs);

  742.   /* Save the initialized register set.  */
  743.   fs.initial = fs.regs;
  744.   fs.initial.reg = dwarf2_frame_state_copy_regs (&fs.regs);

  745.   /* Then decode the insns in the FDE up to our target PC.  */
  746.   execute_cfa_program (fde, fde->instructions, fde->end, gdbarch, pc, &fs);

  747.   /* Calculate the CFA.  */
  748.   switch (fs.regs.cfa_how)
  749.     {
  750.     case CFA_REG_OFFSET:
  751.       {
  752.         int regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, fs.regs.cfa_reg);

  753.         if (regnum == -1)
  754.           error (_("Unable to access DWARF register number %d"),
  755.                  (int) fs.regs.cfa_reg); /* FIXME */

  756.         *regnum_out = regnum;
  757.         if (fs.armcc_cfa_offsets_reversed)
  758.           *offset_out = -fs.regs.cfa_offset;
  759.         else
  760.           *offset_out = fs.regs.cfa_offset;
  761.         return 1;
  762.       }

  763.     case CFA_EXP:
  764.       *text_offset_out = text_offset;
  765.       *cfa_start_out = fs.regs.cfa_exp;
  766.       *cfa_end_out = fs.regs.cfa_exp + fs.regs.cfa_exp_len;
  767.       return 0;

  768.     default:
  769.       internal_error (__FILE__, __LINE__, _("Unknown CFA rule."));
  770.     }
  771. }


  772. struct dwarf2_frame_cache
  773. {
  774.   /* DWARF Call Frame Address.  */
  775.   CORE_ADDR cfa;

  776.   /* Set if the return address column was marked as unavailable
  777.      (required non-collected memory or registers to compute).  */
  778.   int unavailable_retaddr;

  779.   /* Set if the return address column was marked as undefined.  */
  780.   int undefined_retaddr;

  781.   /* Saved registers, indexed by GDB register number, not by DWARF
  782.      register number.  */
  783.   struct dwarf2_frame_state_reg *reg;

  784.   /* Return address register.  */
  785.   struct dwarf2_frame_state_reg retaddr_reg;

  786.   /* Target address size in bytes.  */
  787.   int addr_size;

  788.   /* The .text offset.  */
  789.   CORE_ADDR text_offset;

  790.   /* True if we already checked whether this frame is the bottom frame
  791.      of a virtual tail call frame chain.  */
  792.   int checked_tailcall_bottom;

  793.   /* If not NULL then this frame is the bottom frame of a TAILCALL_FRAME
  794.      sequence.  If NULL then it is a normal case with no TAILCALL_FRAME
  795.      involved.  Non-bottom frames of a virtual tail call frames chain use
  796.      dwarf2_tailcall_frame_unwind unwinder so this field does not apply for
  797.      them.  */
  798.   void *tailcall_cache;

  799.   /* The number of bytes to subtract from TAILCALL_FRAME frames frame
  800.      base to get the SP, to simulate the return address pushed on the
  801.      stack.  */
  802.   LONGEST entry_cfa_sp_offset;
  803.   int entry_cfa_sp_offset_p;
  804. };

  805. /* A cleanup that sets a pointer to NULL.  */

  806. static void
  807. clear_pointer_cleanup (void *arg)
  808. {
  809.   void **ptr = arg;

  810.   *ptr = NULL;
  811. }

  812. static struct dwarf2_frame_cache *
  813. dwarf2_frame_cache (struct frame_info *this_frame, void **this_cache)
  814. {
  815.   struct cleanup *reset_cache_cleanup, *old_chain;
  816.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  817.   const int num_regs = gdbarch_num_regs (gdbarch)
  818.                        + gdbarch_num_pseudo_regs (gdbarch);
  819.   struct dwarf2_frame_cache *cache;
  820.   struct dwarf2_frame_state *fs;
  821.   struct dwarf2_fde *fde;
  822.   volatile struct gdb_exception ex;
  823.   CORE_ADDR entry_pc;
  824.   const gdb_byte *instr;

  825.   if (*this_cache)
  826.     return *this_cache;

  827.   /* Allocate a new cache.  */
  828.   cache = FRAME_OBSTACK_ZALLOC (struct dwarf2_frame_cache);
  829.   cache->reg = FRAME_OBSTACK_CALLOC (num_regs, struct dwarf2_frame_state_reg);
  830.   *this_cache = cache;
  831.   reset_cache_cleanup = make_cleanup (clear_pointer_cleanup, this_cache);

  832.   /* Allocate and initialize the frame state.  */
  833.   fs = XCNEW (struct dwarf2_frame_state);
  834.   old_chain = make_cleanup (dwarf2_frame_state_free, fs);

  835.   /* Unwind the PC.

  836.      Note that if the next frame is never supposed to return (i.e. a call
  837.      to abort), the compiler might optimize away the instruction at
  838.      its return address.  As a result the return address will
  839.      point at some random instruction, and the CFI for that
  840.      instruction is probably worthless to us.  GCC's unwinder solves
  841.      this problem by substracting 1 from the return address to get an
  842.      address in the middle of a presumed call instruction (or the
  843.      instruction in the associated delay slot).  This should only be
  844.      done for "normal" frames and not for resume-type frames (signal
  845.      handlers, sentinel frames, dummy frames).  The function
  846.      get_frame_address_in_block does just this.  It's not clear how
  847.      reliable the method is though; there is the potential for the
  848.      register state pre-call being different to that on return.  */
  849.   fs->pc = get_frame_address_in_block (this_frame);

  850.   /* Find the correct FDE.  */
  851.   fde = dwarf2_frame_find_fde (&fs->pc, &cache->text_offset);
  852.   gdb_assert (fde != NULL);

  853.   /* Extract any interesting information from the CIE.  */
  854.   fs->data_align = fde->cie->data_alignment_factor;
  855.   fs->code_align = fde->cie->code_alignment_factor;
  856.   fs->retaddr_column = fde->cie->return_address_register;
  857.   cache->addr_size = fde->cie->addr_size;

  858.   /* Check for "quirks" - known bugs in producers.  */
  859.   dwarf2_frame_find_quirks (fs, fde);

  860.   /* First decode all the insns in the CIE.  */
  861.   execute_cfa_program (fde, fde->cie->initial_instructions,
  862.                        fde->cie->end, gdbarch,
  863.                        get_frame_address_in_block (this_frame), fs);

  864.   /* Save the initialized register set.  */
  865.   fs->initial = fs->regs;
  866.   fs->initial.reg = dwarf2_frame_state_copy_regs (&fs->regs);

  867.   if (get_frame_func_if_available (this_frame, &entry_pc))
  868.     {
  869.       /* Decode the insns in the FDE up to the entry PC.  */
  870.       instr = execute_cfa_program (fde, fde->instructions, fde->end, gdbarch,
  871.                                    entry_pc, fs);

  872.       if (fs->regs.cfa_how == CFA_REG_OFFSET
  873.           && (gdbarch_dwarf2_reg_to_regnum (gdbarch, fs->regs.cfa_reg)
  874.               == gdbarch_sp_regnum (gdbarch)))
  875.         {
  876.           cache->entry_cfa_sp_offset = fs->regs.cfa_offset;
  877.           cache->entry_cfa_sp_offset_p = 1;
  878.         }
  879.     }
  880.   else
  881.     instr = fde->instructions;

  882.   /* Then decode the insns in the FDE up to our target PC.  */
  883.   execute_cfa_program (fde, instr, fde->end, gdbarch,
  884.                        get_frame_address_in_block (this_frame), fs);

  885.   TRY_CATCH (ex, RETURN_MASK_ERROR)
  886.     {
  887.       /* Calculate the CFA.  */
  888.       switch (fs->regs.cfa_how)
  889.         {
  890.         case CFA_REG_OFFSET:
  891.           cache->cfa = read_addr_from_reg (this_frame, fs->regs.cfa_reg);
  892.           if (fs->armcc_cfa_offsets_reversed)
  893.             cache->cfa -= fs->regs.cfa_offset;
  894.           else
  895.             cache->cfa += fs->regs.cfa_offset;
  896.           break;

  897.         case CFA_EXP:
  898.           cache->cfa =
  899.             execute_stack_op (fs->regs.cfa_exp, fs->regs.cfa_exp_len,
  900.                               cache->addr_size, cache->text_offset,
  901.                               this_frame, 0, 0);
  902.           break;

  903.         default:
  904.           internal_error (__FILE__, __LINE__, _("Unknown CFA rule."));
  905.         }
  906.     }
  907.   if (ex.reason < 0)
  908.     {
  909.       if (ex.error == NOT_AVAILABLE_ERROR)
  910.         {
  911.           cache->unavailable_retaddr = 1;
  912.           do_cleanups (old_chain);
  913.           discard_cleanups (reset_cache_cleanup);
  914.           return cache;
  915.         }

  916.       throw_exception (ex);
  917.     }

  918.   /* Initialize the register state.  */
  919.   {
  920.     int regnum;

  921.     for (regnum = 0; regnum < num_regs; regnum++)
  922.       dwarf2_frame_init_reg (gdbarch, regnum, &cache->reg[regnum], this_frame);
  923.   }

  924.   /* Go through the DWARF2 CFI generated table and save its register
  925.      location information in the cache.  Note that we don't skip the
  926.      return address column; it's perfectly all right for it to
  927.      correspond to a real register.  If it doesn't correspond to a
  928.      real register, or if we shouldn't treat it as such,
  929.      gdbarch_dwarf2_reg_to_regnum should be defined to return a number outside
  930.      the range [0, gdbarch_num_regs).  */
  931.   {
  932.     int column;                /* CFI speak for "register number".  */

  933.     for (column = 0; column < fs->regs.num_regs; column++)
  934.       {
  935.         /* Use the GDB register number as the destination index.  */
  936.         int regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, column);

  937.         /* If there's no corresponding GDB register, ignore it.  */
  938.         if (regnum < 0 || regnum >= num_regs)
  939.           continue;

  940.         /* NOTE: cagney/2003-09-05: CFI should specify the disposition
  941.            of all debug info registers.  If it doesn't, complain (but
  942.            not too loudly).  It turns out that GCC assumes that an
  943.            unspecified register implies "same value" when CFI (draft
  944.            7) specifies nothing at all.  Such a register could equally
  945.            be interpreted as "undefined".  Also note that this check
  946.            isn't sufficient; it only checks that all registers in the
  947.            range [0 .. max column] are specified, and won't detect
  948.            problems when a debug info register falls outside of the
  949.            table.  We need a way of iterating through all the valid
  950.            DWARF2 register numbers.  */
  951.         if (fs->regs.reg[column].how == DWARF2_FRAME_REG_UNSPECIFIED)
  952.           {
  953.             if (cache->reg[regnum].how == DWARF2_FRAME_REG_UNSPECIFIED)
  954.               complaint (&symfile_complaints, _("\
  955. incomplete CFI data; unspecified registers (e.g., %s) at %s"),
  956.                          gdbarch_register_name (gdbarch, regnum),
  957.                          paddress (gdbarch, fs->pc));
  958.           }
  959.         else
  960.           cache->reg[regnum] = fs->regs.reg[column];
  961.       }
  962.   }

  963.   /* Eliminate any DWARF2_FRAME_REG_RA rules, and save the information
  964.      we need for evaluating DWARF2_FRAME_REG_RA_OFFSET rules.  */
  965.   {
  966.     int regnum;

  967.     for (regnum = 0; regnum < num_regs; regnum++)
  968.       {
  969.         if (cache->reg[regnum].how == DWARF2_FRAME_REG_RA
  970.             || cache->reg[regnum].how == DWARF2_FRAME_REG_RA_OFFSET)
  971.           {
  972.             struct dwarf2_frame_state_reg *retaddr_reg =
  973.               &fs->regs.reg[fs->retaddr_column];

  974.             /* It seems rather bizarre to specify an "empty" column as
  975.                the return adress column.  However, this is exactly
  976.                what GCC does on some targets.  It turns out that GCC
  977.                assumes that the return address can be found in the
  978.                register corresponding to the return address column.
  979.                Incidentally, that's how we should treat a return
  980.                address column specifying "same value" too.  */
  981.             if (fs->retaddr_column < fs->regs.num_regs
  982.                 && retaddr_reg->how != DWARF2_FRAME_REG_UNSPECIFIED
  983.                 && retaddr_reg->how != DWARF2_FRAME_REG_SAME_VALUE)
  984.               {
  985.                 if (cache->reg[regnum].how == DWARF2_FRAME_REG_RA)
  986.                   cache->reg[regnum] = *retaddr_reg;
  987.                 else
  988.                   cache->retaddr_reg = *retaddr_reg;
  989.               }
  990.             else
  991.               {
  992.                 if (cache->reg[regnum].how == DWARF2_FRAME_REG_RA)
  993.                   {
  994.                     cache->reg[regnum].loc.reg = fs->retaddr_column;
  995.                     cache->reg[regnum].how = DWARF2_FRAME_REG_SAVED_REG;
  996.                   }
  997.                 else
  998.                   {
  999.                     cache->retaddr_reg.loc.reg = fs->retaddr_column;
  1000.                     cache->retaddr_reg.how = DWARF2_FRAME_REG_SAVED_REG;
  1001.                   }
  1002.               }
  1003.           }
  1004.       }
  1005.   }

  1006.   if (fs->retaddr_column < fs->regs.num_regs
  1007.       && fs->regs.reg[fs->retaddr_column].how == DWARF2_FRAME_REG_UNDEFINED)
  1008.     cache->undefined_retaddr = 1;

  1009.   do_cleanups (old_chain);
  1010.   discard_cleanups (reset_cache_cleanup);
  1011.   return cache;
  1012. }

  1013. static enum unwind_stop_reason
  1014. dwarf2_frame_unwind_stop_reason (struct frame_info *this_frame,
  1015.                                  void **this_cache)
  1016. {
  1017.   struct dwarf2_frame_cache *cache
  1018.     = dwarf2_frame_cache (this_frame, this_cache);

  1019.   if (cache->unavailable_retaddr)
  1020.     return UNWIND_UNAVAILABLE;

  1021.   if (cache->undefined_retaddr)
  1022.     return UNWIND_OUTERMOST;

  1023.   return UNWIND_NO_REASON;
  1024. }

  1025. static void
  1026. dwarf2_frame_this_id (struct frame_info *this_frame, void **this_cache,
  1027.                       struct frame_id *this_id)
  1028. {
  1029.   struct dwarf2_frame_cache *cache =
  1030.     dwarf2_frame_cache (this_frame, this_cache);

  1031.   if (cache->unavailable_retaddr)
  1032.     (*this_id) = frame_id_build_unavailable_stack (get_frame_func (this_frame));
  1033.   else if (cache->undefined_retaddr)
  1034.     return;
  1035.   else
  1036.     (*this_id) = frame_id_build (cache->cfa, get_frame_func (this_frame));
  1037. }

  1038. static struct value *
  1039. dwarf2_frame_prev_register (struct frame_info *this_frame, void **this_cache,
  1040.                             int regnum)
  1041. {
  1042.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  1043.   struct dwarf2_frame_cache *cache =
  1044.     dwarf2_frame_cache (this_frame, this_cache);
  1045.   CORE_ADDR addr;
  1046.   int realnum;

  1047.   /* Check whether THIS_FRAME is the bottom frame of a virtual tail
  1048.      call frame chain.  */
  1049.   if (!cache->checked_tailcall_bottom)
  1050.     {
  1051.       cache->checked_tailcall_bottom = 1;
  1052.       dwarf2_tailcall_sniffer_first (this_frame, &cache->tailcall_cache,
  1053.                                      (cache->entry_cfa_sp_offset_p
  1054.                                       ? &cache->entry_cfa_sp_offset : NULL));
  1055.     }

  1056.   /* Non-bottom frames of a virtual tail call frames chain use
  1057.      dwarf2_tailcall_frame_unwind unwinder so this code does not apply for
  1058.      them.  If dwarf2_tailcall_prev_register_first does not have specific value
  1059.      unwind the register, tail call frames are assumed to have the register set
  1060.      of the top caller.  */
  1061.   if (cache->tailcall_cache)
  1062.     {
  1063.       struct value *val;

  1064.       val = dwarf2_tailcall_prev_register_first (this_frame,
  1065.                                                  &cache->tailcall_cache,
  1066.                                                  regnum);
  1067.       if (val)
  1068.         return val;
  1069.     }

  1070.   switch (cache->reg[regnum].how)
  1071.     {
  1072.     case DWARF2_FRAME_REG_UNDEFINED:
  1073.       /* If CFI explicitly specified that the value isn't defined,
  1074.          mark it as optimized away; the value isn't available.  */
  1075.       return frame_unwind_got_optimized (this_frame, regnum);

  1076.     case DWARF2_FRAME_REG_SAVED_OFFSET:
  1077.       addr = cache->cfa + cache->reg[regnum].loc.offset;
  1078.       return frame_unwind_got_memory (this_frame, regnum, addr);

  1079.     case DWARF2_FRAME_REG_SAVED_REG:
  1080.       realnum
  1081.         = gdbarch_dwarf2_reg_to_regnum (gdbarch, cache->reg[regnum].loc.reg);
  1082.       return frame_unwind_got_register (this_frame, regnum, realnum);

  1083.     case DWARF2_FRAME_REG_SAVED_EXP:
  1084.       addr = execute_stack_op (cache->reg[regnum].loc.exp,
  1085.                                cache->reg[regnum].exp_len,
  1086.                                cache->addr_size, cache->text_offset,
  1087.                                this_frame, cache->cfa, 1);
  1088.       return frame_unwind_got_memory (this_frame, regnum, addr);

  1089.     case DWARF2_FRAME_REG_SAVED_VAL_OFFSET:
  1090.       addr = cache->cfa + cache->reg[regnum].loc.offset;
  1091.       return frame_unwind_got_constant (this_frame, regnum, addr);

  1092.     case DWARF2_FRAME_REG_SAVED_VAL_EXP:
  1093.       addr = execute_stack_op (cache->reg[regnum].loc.exp,
  1094.                                cache->reg[regnum].exp_len,
  1095.                                cache->addr_size, cache->text_offset,
  1096.                                this_frame, cache->cfa, 1);
  1097.       return frame_unwind_got_constant (this_frame, regnum, addr);

  1098.     case DWARF2_FRAME_REG_UNSPECIFIED:
  1099.       /* GCC, in its infinite wisdom decided to not provide unwind
  1100.          information for registers that are "same value".  Since
  1101.          DWARF2 (3 draft 7) doesn't define such behavior, said
  1102.          registers are actually undefined (which is different to CFI
  1103.          "undefined").  Code above issues a complaint about this.
  1104.          Here just fudge the books, assume GCC, and that the value is
  1105.          more inner on the stack.  */
  1106.       return frame_unwind_got_register (this_frame, regnum, regnum);

  1107.     case DWARF2_FRAME_REG_SAME_VALUE:
  1108.       return frame_unwind_got_register (this_frame, regnum, regnum);

  1109.     case DWARF2_FRAME_REG_CFA:
  1110.       return frame_unwind_got_address (this_frame, regnum, cache->cfa);

  1111.     case DWARF2_FRAME_REG_CFA_OFFSET:
  1112.       addr = cache->cfa + cache->reg[regnum].loc.offset;
  1113.       return frame_unwind_got_address (this_frame, regnum, addr);

  1114.     case DWARF2_FRAME_REG_RA_OFFSET:
  1115.       addr = cache->reg[regnum].loc.offset;
  1116.       regnum = gdbarch_dwarf2_reg_to_regnum
  1117.         (gdbarch, cache->retaddr_reg.loc.reg);
  1118.       addr += get_frame_register_unsigned (this_frame, regnum);
  1119.       return frame_unwind_got_address (this_frame, regnum, addr);

  1120.     case DWARF2_FRAME_REG_FN:
  1121.       return cache->reg[regnum].loc.fn (this_frame, this_cache, regnum);

  1122.     default:
  1123.       internal_error (__FILE__, __LINE__, _("Unknown register rule."));
  1124.     }
  1125. }

  1126. /* Proxy for tailcall_frame_dealloc_cache for bottom frame of a virtual tail
  1127.    call frames chain.  */

  1128. static void
  1129. dwarf2_frame_dealloc_cache (struct frame_info *self, void *this_cache)
  1130. {
  1131.   struct dwarf2_frame_cache *cache = dwarf2_frame_cache (self, &this_cache);

  1132.   if (cache->tailcall_cache)
  1133.     dwarf2_tailcall_frame_unwind.dealloc_cache (self, cache->tailcall_cache);
  1134. }

  1135. static int
  1136. dwarf2_frame_sniffer (const struct frame_unwind *self,
  1137.                       struct frame_info *this_frame, void **this_cache)
  1138. {
  1139.   /* Grab an address that is guarenteed to reside somewhere within the
  1140.      functionget_frame_pc(), with a no-return next function, can
  1141.      end up returning something past the end of this function's body.
  1142.      If the frame we're sniffing for is a signal frame whose start
  1143.      address is placed on the stack by the OS, its FDE must
  1144.      extend one byte before its start address or we could potentially
  1145.      select the FDE of the previous function.  */
  1146.   CORE_ADDR block_addr = get_frame_address_in_block (this_frame);
  1147.   struct dwarf2_fde *fde = dwarf2_frame_find_fde (&block_addr, NULL);

  1148.   if (!fde)
  1149.     return 0;

  1150.   /* On some targets, signal trampolines may have unwind information.
  1151.      We need to recognize them so that we set the frame type
  1152.      correctly.  */

  1153.   if (fde->cie->signal_frame
  1154.       || dwarf2_frame_signal_frame_p (get_frame_arch (this_frame),
  1155.                                       this_frame))
  1156.     return self->type == SIGTRAMP_FRAME;

  1157.   if (self->type != NORMAL_FRAME)
  1158.     return 0;

  1159.   return 1;
  1160. }

  1161. static const struct frame_unwind dwarf2_frame_unwind =
  1162. {
  1163.   NORMAL_FRAME,
  1164.   dwarf2_frame_unwind_stop_reason,
  1165.   dwarf2_frame_this_id,
  1166.   dwarf2_frame_prev_register,
  1167.   NULL,
  1168.   dwarf2_frame_sniffer,
  1169.   dwarf2_frame_dealloc_cache
  1170. };

  1171. static const struct frame_unwind dwarf2_signal_frame_unwind =
  1172. {
  1173.   SIGTRAMP_FRAME,
  1174.   dwarf2_frame_unwind_stop_reason,
  1175.   dwarf2_frame_this_id,
  1176.   dwarf2_frame_prev_register,
  1177.   NULL,
  1178.   dwarf2_frame_sniffer,

  1179.   /* TAILCALL_CACHE can never be in such frame to need dealloc_cache.  */
  1180.   NULL
  1181. };

  1182. /* Append the DWARF-2 frame unwinders to GDBARCH's list.  */

  1183. void
  1184. dwarf2_append_unwinders (struct gdbarch *gdbarch)
  1185. {
  1186.   /* TAILCALL_FRAME must be first to find the record by
  1187.      dwarf2_tailcall_sniffer_first.  */
  1188.   frame_unwind_append_unwinder (gdbarch, &dwarf2_tailcall_frame_unwind);

  1189.   frame_unwind_append_unwinder (gdbarch, &dwarf2_frame_unwind);
  1190.   frame_unwind_append_unwinder (gdbarch, &dwarf2_signal_frame_unwind);
  1191. }


  1192. /* There is no explicitly defined relationship between the CFA and the
  1193.    location of frame's local variables and arguments/parameters.
  1194.    Therefore, frame base methods on this page should probably only be
  1195.    used as a last resort, just to avoid printing total garbage as a
  1196.    response to the "info frame" command.  */

  1197. static CORE_ADDR
  1198. dwarf2_frame_base_address (struct frame_info *this_frame, void **this_cache)
  1199. {
  1200.   struct dwarf2_frame_cache *cache =
  1201.     dwarf2_frame_cache (this_frame, this_cache);

  1202.   return cache->cfa;
  1203. }

  1204. static const struct frame_base dwarf2_frame_base =
  1205. {
  1206.   &dwarf2_frame_unwind,
  1207.   dwarf2_frame_base_address,
  1208.   dwarf2_frame_base_address,
  1209.   dwarf2_frame_base_address
  1210. };

  1211. const struct frame_base *
  1212. dwarf2_frame_base_sniffer (struct frame_info *this_frame)
  1213. {
  1214.   CORE_ADDR block_addr = get_frame_address_in_block (this_frame);

  1215.   if (dwarf2_frame_find_fde (&block_addr, NULL))
  1216.     return &dwarf2_frame_base;

  1217.   return NULL;
  1218. }

  1219. /* Compute the CFA for THIS_FRAME, but only if THIS_FRAME came from
  1220.    the DWARF unwinder.  This is used to implement
  1221.    DW_OP_call_frame_cfa.  */

  1222. CORE_ADDR
  1223. dwarf2_frame_cfa (struct frame_info *this_frame)
  1224. {
  1225.   if (frame_unwinder_is (this_frame, &record_btrace_tailcall_frame_unwind)
  1226.       || frame_unwinder_is (this_frame, &record_btrace_frame_unwind))
  1227.     throw_error (NOT_AVAILABLE_ERROR,
  1228.                  _("cfa not available for record btrace target"));

  1229.   while (get_frame_type (this_frame) == INLINE_FRAME)
  1230.     this_frame = get_prev_frame (this_frame);
  1231.   if (get_frame_unwind_stop_reason (this_frame) == UNWIND_UNAVAILABLE)
  1232.     throw_error (NOT_AVAILABLE_ERROR,
  1233.                 _("can't compute CFA for this frame: "
  1234.                   "required registers or memory are unavailable"));

  1235.   if (get_frame_id (this_frame).stack_status != FID_STACK_VALID)
  1236.     throw_error (NOT_AVAILABLE_ERROR,
  1237.                 _("can't compute CFA for this frame: "
  1238.                   "frame base not available"));

  1239.   return get_frame_base (this_frame);
  1240. }

  1241. const struct objfile_data *dwarf2_frame_objfile_data;

  1242. static unsigned int
  1243. read_1_byte (bfd *abfd, const gdb_byte *buf)
  1244. {
  1245.   return bfd_get_8 (abfd, buf);
  1246. }

  1247. static unsigned int
  1248. read_4_bytes (bfd *abfd, const gdb_byte *buf)
  1249. {
  1250.   return bfd_get_32 (abfd, buf);
  1251. }

  1252. static ULONGEST
  1253. read_8_bytes (bfd *abfd, const gdb_byte *buf)
  1254. {
  1255.   return bfd_get_64 (abfd, buf);
  1256. }

  1257. static ULONGEST
  1258. read_initial_length (bfd *abfd, const gdb_byte *buf,
  1259.                      unsigned int *bytes_read_ptr)
  1260. {
  1261.   LONGEST result;

  1262.   result = bfd_get_32 (abfd, buf);
  1263.   if (result == 0xffffffff)
  1264.     {
  1265.       result = bfd_get_64 (abfd, buf + 4);
  1266.       *bytes_read_ptr = 12;
  1267.     }
  1268.   else
  1269.     *bytes_read_ptr = 4;

  1270.   return result;
  1271. }


  1272. /* Pointer encoding helper functions.  */

  1273. /* GCC supports exception handling based on DWARF2 CFI.  However, for
  1274.    technical reasons, it encodes addresses in its FDE's in a different
  1275.    way.  Several "pointer encodings" are supported.  The encoding
  1276.    that's used for a particular FDE is determined by the 'R'
  1277.    augmentation in the associated CIE.  The argument of this
  1278.    augmentation is a single byte.

  1279.    The address can be encoded as 2 bytes, 4 bytes, 8 bytes, or as a
  1280.    LEB128.  This is encoded in bits 0, 1 and 2.  Bit 3 encodes whether
  1281.    the address is signed or unsigned.  Bits 4, 5 and 6 encode how the
  1282.    address should be interpreted (absolute, relative to the current
  1283.    position in the FDE, ...).  Bit 7, indicates that the address
  1284.    should be dereferenced.  */

  1285. static gdb_byte
  1286. encoding_for_size (unsigned int size)
  1287. {
  1288.   switch (size)
  1289.     {
  1290.     case 2:
  1291.       return DW_EH_PE_udata2;
  1292.     case 4:
  1293.       return DW_EH_PE_udata4;
  1294.     case 8:
  1295.       return DW_EH_PE_udata8;
  1296.     default:
  1297.       internal_error (__FILE__, __LINE__, _("Unsupported address size"));
  1298.     }
  1299. }

  1300. static CORE_ADDR
  1301. read_encoded_value (struct comp_unit *unit, gdb_byte encoding,
  1302.                     int ptr_len, const gdb_byte *buf,
  1303.                     unsigned int *bytes_read_ptr,
  1304.                     CORE_ADDR func_base)
  1305. {
  1306.   ptrdiff_t offset;
  1307.   CORE_ADDR base;

  1308.   /* GCC currently doesn't generate DW_EH_PE_indirect encodings for
  1309.      FDE's.  */
  1310.   if (encoding & DW_EH_PE_indirect)
  1311.     internal_error (__FILE__, __LINE__,
  1312.                     _("Unsupported encoding: DW_EH_PE_indirect"));

  1313.   *bytes_read_ptr = 0;

  1314.   switch (encoding & 0x70)
  1315.     {
  1316.     case DW_EH_PE_absptr:
  1317.       base = 0;
  1318.       break;
  1319.     case DW_EH_PE_pcrel:
  1320.       base = bfd_get_section_vma (unit->abfd, unit->dwarf_frame_section);
  1321.       base += (buf - unit->dwarf_frame_buffer);
  1322.       break;
  1323.     case DW_EH_PE_datarel:
  1324.       base = unit->dbase;
  1325.       break;
  1326.     case DW_EH_PE_textrel:
  1327.       base = unit->tbase;
  1328.       break;
  1329.     case DW_EH_PE_funcrel:
  1330.       base = func_base;
  1331.       break;
  1332.     case DW_EH_PE_aligned:
  1333.       base = 0;
  1334.       offset = buf - unit->dwarf_frame_buffer;
  1335.       if ((offset % ptr_len) != 0)
  1336.         {
  1337.           *bytes_read_ptr = ptr_len - (offset % ptr_len);
  1338.           buf += *bytes_read_ptr;
  1339.         }
  1340.       break;
  1341.     default:
  1342.       internal_error (__FILE__, __LINE__,
  1343.                       _("Invalid or unsupported encoding"));
  1344.     }

  1345.   if ((encoding & 0x07) == 0x00)
  1346.     {
  1347.       encoding |= encoding_for_size (ptr_len);
  1348.       if (bfd_get_sign_extend_vma (unit->abfd))
  1349.         encoding |= DW_EH_PE_signed;
  1350.     }

  1351.   switch (encoding & 0x0f)
  1352.     {
  1353.     case DW_EH_PE_uleb128:
  1354.       {
  1355.         uint64_t value;
  1356.         const gdb_byte *end_buf = buf + (sizeof (value) + 1) * 8 / 7;

  1357.         *bytes_read_ptr += safe_read_uleb128 (buf, end_buf, &value) - buf;
  1358.         return base + value;
  1359.       }
  1360.     case DW_EH_PE_udata2:
  1361.       *bytes_read_ptr += 2;
  1362.       return (base + bfd_get_16 (unit->abfd, (bfd_byte *) buf));
  1363.     case DW_EH_PE_udata4:
  1364.       *bytes_read_ptr += 4;
  1365.       return (base + bfd_get_32 (unit->abfd, (bfd_byte *) buf));
  1366.     case DW_EH_PE_udata8:
  1367.       *bytes_read_ptr += 8;
  1368.       return (base + bfd_get_64 (unit->abfd, (bfd_byte *) buf));
  1369.     case DW_EH_PE_sleb128:
  1370.       {
  1371.         int64_t value;
  1372.         const gdb_byte *end_buf = buf + (sizeof (value) + 1) * 8 / 7;

  1373.         *bytes_read_ptr += safe_read_sleb128 (buf, end_buf, &value) - buf;
  1374.         return base + value;
  1375.       }
  1376.     case DW_EH_PE_sdata2:
  1377.       *bytes_read_ptr += 2;
  1378.       return (base + bfd_get_signed_16 (unit->abfd, (bfd_byte *) buf));
  1379.     case DW_EH_PE_sdata4:
  1380.       *bytes_read_ptr += 4;
  1381.       return (base + bfd_get_signed_32 (unit->abfd, (bfd_byte *) buf));
  1382.     case DW_EH_PE_sdata8:
  1383.       *bytes_read_ptr += 8;
  1384.       return (base + bfd_get_signed_64 (unit->abfd, (bfd_byte *) buf));
  1385.     default:
  1386.       internal_error (__FILE__, __LINE__,
  1387.                       _("Invalid or unsupported encoding"));
  1388.     }
  1389. }


  1390. static int
  1391. bsearch_cie_cmp (const void *key, const void *element)
  1392. {
  1393.   ULONGEST cie_pointer = *(ULONGEST *) key;
  1394.   struct dwarf2_cie *cie = *(struct dwarf2_cie **) element;

  1395.   if (cie_pointer == cie->cie_pointer)
  1396.     return 0;

  1397.   return (cie_pointer < cie->cie_pointer) ? -1 : 1;
  1398. }

  1399. /* Find CIE with the given CIE_POINTER in CIE_TABLE.  */
  1400. static struct dwarf2_cie *
  1401. find_cie (struct dwarf2_cie_table *cie_table, ULONGEST cie_pointer)
  1402. {
  1403.   struct dwarf2_cie **p_cie;

  1404.   /* The C standard (ISO/IEC 9899:TC2) requires the BASE argument to
  1405.      bsearch be non-NULL.  */
  1406.   if (cie_table->entries == NULL)
  1407.     {
  1408.       gdb_assert (cie_table->num_entries == 0);
  1409.       return NULL;
  1410.     }

  1411.   p_cie = bsearch (&cie_pointer, cie_table->entries, cie_table->num_entries,
  1412.                    sizeof (cie_table->entries[0]), bsearch_cie_cmp);
  1413.   if (p_cie != NULL)
  1414.     return *p_cie;
  1415.   return NULL;
  1416. }

  1417. /* Add a pointer to new CIE to the CIE_TABLE, allocating space for it.  */
  1418. static void
  1419. add_cie (struct dwarf2_cie_table *cie_table, struct dwarf2_cie *cie)
  1420. {
  1421.   const int n = cie_table->num_entries;

  1422.   gdb_assert (n < 1
  1423.               || cie_table->entries[n - 1]->cie_pointer < cie->cie_pointer);

  1424.   cie_table->entries =
  1425.       xrealloc (cie_table->entries, (n + 1) * sizeof (cie_table->entries[0]));
  1426.   cie_table->entries[n] = cie;
  1427.   cie_table->num_entries = n + 1;
  1428. }

  1429. static int
  1430. bsearch_fde_cmp (const void *key, const void *element)
  1431. {
  1432.   CORE_ADDR seek_pc = *(CORE_ADDR *) key;
  1433.   struct dwarf2_fde *fde = *(struct dwarf2_fde **) element;

  1434.   if (seek_pc < fde->initial_location)
  1435.     return -1;
  1436.   if (seek_pc < fde->initial_location + fde->address_range)
  1437.     return 0;
  1438.   return 1;
  1439. }

  1440. /* Find the FDE for *PC.  Return a pointer to the FDE, and store the
  1441.    inital location associated with it into *PC.  */

  1442. static struct dwarf2_fde *
  1443. dwarf2_frame_find_fde (CORE_ADDR *pc, CORE_ADDR *out_offset)
  1444. {
  1445.   struct objfile *objfile;

  1446.   ALL_OBJFILES (objfile)
  1447.     {
  1448.       struct dwarf2_fde_table *fde_table;
  1449.       struct dwarf2_fde **p_fde;
  1450.       CORE_ADDR offset;
  1451.       CORE_ADDR seek_pc;

  1452.       fde_table = objfile_data (objfile, dwarf2_frame_objfile_data);
  1453.       if (fde_table == NULL)
  1454.         {
  1455.           dwarf2_build_frame_info (objfile);
  1456.           fde_table = objfile_data (objfile, dwarf2_frame_objfile_data);
  1457.         }
  1458.       gdb_assert (fde_table != NULL);

  1459.       if (fde_table->num_entries == 0)
  1460.         continue;

  1461.       gdb_assert (objfile->section_offsets);
  1462.       offset = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));

  1463.       gdb_assert (fde_table->num_entries > 0);
  1464.       if (*pc < offset + fde_table->entries[0]->initial_location)
  1465.         continue;

  1466.       seek_pc = *pc - offset;
  1467.       p_fde = bsearch (&seek_pc, fde_table->entries, fde_table->num_entries,
  1468.                        sizeof (fde_table->entries[0]), bsearch_fde_cmp);
  1469.       if (p_fde != NULL)
  1470.         {
  1471.           *pc = (*p_fde)->initial_location + offset;
  1472.           if (out_offset)
  1473.             *out_offset = offset;
  1474.           return *p_fde;
  1475.         }
  1476.     }
  1477.   return NULL;
  1478. }

  1479. /* Add a pointer to new FDE to the FDE_TABLE, allocating space for it.  */
  1480. static void
  1481. add_fde (struct dwarf2_fde_table *fde_table, struct dwarf2_fde *fde)
  1482. {
  1483.   if (fde->address_range == 0)
  1484.     /* Discard useless FDEs.  */
  1485.     return;

  1486.   fde_table->num_entries += 1;
  1487.   fde_table->entries =
  1488.       xrealloc (fde_table->entries,
  1489.                 fde_table->num_entries * sizeof (fde_table->entries[0]));
  1490.   fde_table->entries[fde_table->num_entries - 1] = fde;
  1491. }

  1492. #define DW64_CIE_ID 0xffffffffffffffffULL

  1493. /* Defines the type of eh_frames that are expected to be decoded: CIE, FDE
  1494.    or any of them.  */

  1495. enum eh_frame_type
  1496. {
  1497.   EH_CIE_TYPE_ID = 1 << 0,
  1498.   EH_FDE_TYPE_ID = 1 << 1,
  1499.   EH_CIE_OR_FDE_TYPE_ID = EH_CIE_TYPE_ID | EH_FDE_TYPE_ID
  1500. };

  1501. static const gdb_byte *decode_frame_entry (struct comp_unit *unit,
  1502.                                            const gdb_byte *start,
  1503.                                            int eh_frame_p,
  1504.                                            struct dwarf2_cie_table *cie_table,
  1505.                                            struct dwarf2_fde_table *fde_table,
  1506.                                            enum eh_frame_type entry_type);

  1507. /* Decode the next CIE or FDE, entry_type specifies the expected type.
  1508.    Return NULL if invalid input, otherwise the next byte to be processed.  */

  1509. static const gdb_byte *
  1510. decode_frame_entry_1 (struct comp_unit *unit, const gdb_byte *start,
  1511.                       int eh_frame_p,
  1512.                       struct dwarf2_cie_table *cie_table,
  1513.                       struct dwarf2_fde_table *fde_table,
  1514.                       enum eh_frame_type entry_type)
  1515. {
  1516.   struct gdbarch *gdbarch = get_objfile_arch (unit->objfile);
  1517.   const gdb_byte *buf, *end;
  1518.   LONGEST length;
  1519.   unsigned int bytes_read;
  1520.   int dwarf64_p;
  1521.   ULONGEST cie_id;
  1522.   ULONGEST cie_pointer;
  1523.   int64_t sleb128;
  1524.   uint64_t uleb128;

  1525.   buf = start;
  1526.   length = read_initial_length (unit->abfd, buf, &bytes_read);
  1527.   buf += bytes_read;
  1528.   end = buf + length;

  1529.   /* Are we still within the section?  */
  1530.   if (end > unit->dwarf_frame_buffer + unit->dwarf_frame_size)
  1531.     return NULL;

  1532.   if (length == 0)
  1533.     return end;

  1534.   /* Distinguish between 32 and 64-bit encoded frame info.  */
  1535.   dwarf64_p = (bytes_read == 12);

  1536.   /* In a .eh_frame section, zero is used to distinguish CIEs from FDEs.  */
  1537.   if (eh_frame_p)
  1538.     cie_id = 0;
  1539.   else if (dwarf64_p)
  1540.     cie_id = DW64_CIE_ID;
  1541.   else
  1542.     cie_id = DW_CIE_ID;

  1543.   if (dwarf64_p)
  1544.     {
  1545.       cie_pointer = read_8_bytes (unit->abfd, buf);
  1546.       buf += 8;
  1547.     }
  1548.   else
  1549.     {
  1550.       cie_pointer = read_4_bytes (unit->abfd, buf);
  1551.       buf += 4;
  1552.     }

  1553.   if (cie_pointer == cie_id)
  1554.     {
  1555.       /* This is a CIE.  */
  1556.       struct dwarf2_cie *cie;
  1557.       char *augmentation;
  1558.       unsigned int cie_version;

  1559.       /* Check that a CIE was expected.  */
  1560.       if ((entry_type & EH_CIE_TYPE_ID) == 0)
  1561.         error (_("Found a CIE when not expecting it."));

  1562.       /* Record the offset into the .debug_frame section of this CIE.  */
  1563.       cie_pointer = start - unit->dwarf_frame_buffer;

  1564.       /* Check whether we've already read it.  */
  1565.       if (find_cie (cie_table, cie_pointer))
  1566.         return end;

  1567.       cie = (struct dwarf2_cie *)
  1568.         obstack_alloc (&unit->objfile->objfile_obstack,
  1569.                        sizeof (struct dwarf2_cie));
  1570.       cie->initial_instructions = NULL;
  1571.       cie->cie_pointer = cie_pointer;

  1572.       /* The encoding for FDE's in a normal .debug_frame section
  1573.          depends on the target address size.  */
  1574.       cie->encoding = DW_EH_PE_absptr;

  1575.       /* We'll determine the final value later, but we need to
  1576.          initialize it conservatively.  */
  1577.       cie->signal_frame = 0;

  1578.       /* Check version number.  */
  1579.       cie_version = read_1_byte (unit->abfd, buf);
  1580.       if (cie_version != 1 && cie_version != 3 && cie_version != 4)
  1581.         return NULL;
  1582.       cie->version = cie_version;
  1583.       buf += 1;

  1584.       /* Interpret the interesting bits of the augmentation.  */
  1585.       cie->augmentation = augmentation = (char *) buf;
  1586.       buf += (strlen (augmentation) + 1);

  1587.       /* Ignore armcc augmentations.  We only use them for quirks,
  1588.          and that doesn't happen until later.  */
  1589.       if (strncmp (augmentation, "armcc", 5) == 0)
  1590.         augmentation += strlen (augmentation);

  1591.       /* The GCC 2.x "eh" augmentation has a pointer immediately
  1592.          following the augmentation string, so it must be handled
  1593.          first.  */
  1594.       if (augmentation[0] == 'e' && augmentation[1] == 'h')
  1595.         {
  1596.           /* Skip.  */
  1597.           buf += gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT;
  1598.           augmentation += 2;
  1599.         }

  1600.       if (cie->version >= 4)
  1601.         {
  1602.           /* FIXME: check that this is the same as from the CU header.  */
  1603.           cie->addr_size = read_1_byte (unit->abfd, buf);
  1604.           ++buf;
  1605.           cie->segment_size = read_1_byte (unit->abfd, buf);
  1606.           ++buf;
  1607.         }
  1608.       else
  1609.         {
  1610.           cie->addr_size = gdbarch_dwarf2_addr_size (gdbarch);
  1611.           cie->segment_size = 0;
  1612.         }
  1613.       /* Address values in .eh_frame sections are defined to have the
  1614.          target's pointer size.  Watchout: This breaks frame info for
  1615.          targets with pointer size < address size, unless a .debug_frame
  1616.          section exists as well.  */
  1617.       if (eh_frame_p)
  1618.         cie->ptr_size = gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT;
  1619.       else
  1620.         cie->ptr_size = cie->addr_size;

  1621.       buf = gdb_read_uleb128 (buf, end, &uleb128);
  1622.       if (buf == NULL)
  1623.         return NULL;
  1624.       cie->code_alignment_factor = uleb128;

  1625.       buf = gdb_read_sleb128 (buf, end, &sleb128);
  1626.       if (buf == NULL)
  1627.         return NULL;
  1628.       cie->data_alignment_factor = sleb128;

  1629.       if (cie_version == 1)
  1630.         {
  1631.           cie->return_address_register = read_1_byte (unit->abfd, buf);
  1632.           ++buf;
  1633.         }
  1634.       else
  1635.         {
  1636.           buf = gdb_read_uleb128 (buf, end, &uleb128);
  1637.           if (buf == NULL)
  1638.             return NULL;
  1639.           cie->return_address_register = uleb128;
  1640.         }

  1641.       cie->return_address_register
  1642.         = dwarf2_frame_adjust_regnum (gdbarch,
  1643.                                       cie->return_address_register,
  1644.                                       eh_frame_p);

  1645.       cie->saw_z_augmentation = (*augmentation == 'z');
  1646.       if (cie->saw_z_augmentation)
  1647.         {
  1648.           uint64_t length;

  1649.           buf = gdb_read_uleb128 (buf, end, &length);
  1650.           if (buf == NULL)
  1651.             return NULL;
  1652.           cie->initial_instructions = buf + length;
  1653.           augmentation++;
  1654.         }

  1655.       while (*augmentation)
  1656.         {
  1657.           /* "L" indicates a byte showing how the LSDA pointer is encoded.  */
  1658.           if (*augmentation == 'L')
  1659.             {
  1660.               /* Skip.  */
  1661.               buf++;
  1662.               augmentation++;
  1663.             }

  1664.           /* "R" indicates a byte indicating how FDE addresses are encoded.  */
  1665.           else if (*augmentation == 'R')
  1666.             {
  1667.               cie->encoding = *buf++;
  1668.               augmentation++;
  1669.             }

  1670.           /* "P" indicates a personality routine in the CIE augmentation.  */
  1671.           else if (*augmentation == 'P')
  1672.             {
  1673.               /* Skip.  Avoid indirection since we throw away the result.  */
  1674.               gdb_byte encoding = (*buf++) & ~DW_EH_PE_indirect;
  1675.               read_encoded_value (unit, encoding, cie->ptr_size,
  1676.                                   buf, &bytes_read, 0);
  1677.               buf += bytes_read;
  1678.               augmentation++;
  1679.             }

  1680.           /* "S" indicates a signal frame, such that the return
  1681.              address must not be decremented to locate the call frame
  1682.              info for the previous frame; it might even be the first
  1683.              instruction of a function, so decrementing it would take
  1684.              us to a different function.  */
  1685.           else if (*augmentation == 'S')
  1686.             {
  1687.               cie->signal_frame = 1;
  1688.               augmentation++;
  1689.             }

  1690.           /* Otherwise we have an unknown augmentation.  Assume that either
  1691.              there is no augmentation data, or we saw a 'z' prefix.  */
  1692.           else
  1693.             {
  1694.               if (cie->initial_instructions)
  1695.                 buf = cie->initial_instructions;
  1696.               break;
  1697.             }
  1698.         }

  1699.       cie->initial_instructions = buf;
  1700.       cie->end = end;
  1701.       cie->unit = unit;

  1702.       add_cie (cie_table, cie);
  1703.     }
  1704.   else
  1705.     {
  1706.       /* This is a FDE.  */
  1707.       struct dwarf2_fde *fde;
  1708.       CORE_ADDR addr;

  1709.       /* Check that an FDE was expected.  */
  1710.       if ((entry_type & EH_FDE_TYPE_ID) == 0)
  1711.         error (_("Found an FDE when not expecting it."));

  1712.       /* In an .eh_frame section, the CIE pointer is the delta between the
  1713.          address within the FDE where the CIE pointer is stored and the
  1714.          address of the CIE.  Convert it to an offset into the .eh_frame
  1715.          section.  */
  1716.       if (eh_frame_p)
  1717.         {
  1718.           cie_pointer = buf - unit->dwarf_frame_buffer - cie_pointer;
  1719.           cie_pointer -= (dwarf64_p ? 8 : 4);
  1720.         }

  1721.       /* In either case, validate the result is still within the section.  */
  1722.       if (cie_pointer >= unit->dwarf_frame_size)
  1723.         return NULL;

  1724.       fde = (struct dwarf2_fde *)
  1725.         obstack_alloc (&unit->objfile->objfile_obstack,
  1726.                        sizeof (struct dwarf2_fde));
  1727.       fde->cie = find_cie (cie_table, cie_pointer);
  1728.       if (fde->cie == NULL)
  1729.         {
  1730.           decode_frame_entry (unit, unit->dwarf_frame_buffer + cie_pointer,
  1731.                               eh_frame_p, cie_table, fde_table,
  1732.                               EH_CIE_TYPE_ID);
  1733.           fde->cie = find_cie (cie_table, cie_pointer);
  1734.         }

  1735.       gdb_assert (fde->cie != NULL);

  1736.       addr = read_encoded_value (unit, fde->cie->encoding, fde->cie->ptr_size,
  1737.                                  buf, &bytes_read, 0);
  1738.       fde->initial_location = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
  1739.       buf += bytes_read;

  1740.       fde->address_range =
  1741.         read_encoded_value (unit, fde->cie->encoding & 0x0f,
  1742.                             fde->cie->ptr_size, buf, &bytes_read, 0);
  1743.       addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + fde->address_range);
  1744.       fde->address_range = addr - fde->initial_location;
  1745.       buf += bytes_read;

  1746.       /* A 'z' augmentation in the CIE implies the presence of an
  1747.          augmentation field in the FDE as well.  The only thing known
  1748.          to be in here at present is the LSDA entry for EH.  So we
  1749.          can skip the whole thing.  */
  1750.       if (fde->cie->saw_z_augmentation)
  1751.         {
  1752.           uint64_t length;

  1753.           buf = gdb_read_uleb128 (buf, end, &length);
  1754.           if (buf == NULL)
  1755.             return NULL;
  1756.           buf += length;
  1757.           if (buf > end)
  1758.             return NULL;
  1759.         }

  1760.       fde->instructions = buf;
  1761.       fde->end = end;

  1762.       fde->eh_frame_p = eh_frame_p;

  1763.       add_fde (fde_table, fde);
  1764.     }

  1765.   return end;
  1766. }

  1767. /* Read a CIE or FDE in BUF and decode it. Entry_type specifies whether we
  1768.    expect an FDE or a CIE.  */

  1769. static const gdb_byte *
  1770. decode_frame_entry (struct comp_unit *unit, const gdb_byte *start,
  1771.                     int eh_frame_p,
  1772.                     struct dwarf2_cie_table *cie_table,
  1773.                     struct dwarf2_fde_table *fde_table,
  1774.                     enum eh_frame_type entry_type)
  1775. {
  1776.   enum { NONE, ALIGN4, ALIGN8, FAIL } workaround = NONE;
  1777.   const gdb_byte *ret;
  1778.   ptrdiff_t start_offset;

  1779.   while (1)
  1780.     {
  1781.       ret = decode_frame_entry_1 (unit, start, eh_frame_p,
  1782.                                   cie_table, fde_table, entry_type);
  1783.       if (ret != NULL)
  1784.         break;

  1785.       /* We have corrupt input data of some form.  */

  1786.       /* ??? Try, weakly, to work around compiler/assembler/linker bugs
  1787.          and mismatches wrt padding and alignment of debug sections.  */
  1788.       /* Note that there is no requirement in the standard for any
  1789.          alignment at all in the frame unwind sections.  Testing for
  1790.          alignment before trying to interpret data would be incorrect.

  1791.          However, GCC traditionally arranged for frame sections to be
  1792.          sized such that the FDE length and CIE fields happen to be
  1793.          aligned (in theory, for performance).  This, unfortunately,
  1794.          was done with .align directives, which had the side effect of
  1795.          forcing the section to be aligned by the linker.

  1796.          This becomes a problem when you have some other producer that
  1797.          creates frame sections that are not as strictly aligned.  That
  1798.          produces a hole in the frame info that gets filled by the
  1799.          linker with zeros.

  1800.          The GCC behaviour is arguably a bug, but it's effectively now
  1801.          part of the ABI, so we're now stuck with it, at least at the
  1802.          object file levelA smart linker may decide, in the process
  1803.          of compressing duplicate CIE information, that it can rewrite
  1804.          the entire output section without this extra padding.  */

  1805.       start_offset = start - unit->dwarf_frame_buffer;
  1806.       if (workaround < ALIGN4 && (start_offset & 3) != 0)
  1807.         {
  1808.           start += 4 - (start_offset & 3);
  1809.           workaround = ALIGN4;
  1810.           continue;
  1811.         }
  1812.       if (workaround < ALIGN8 && (start_offset & 7) != 0)
  1813.         {
  1814.           start += 8 - (start_offset & 7);
  1815.           workaround = ALIGN8;
  1816.           continue;
  1817.         }

  1818.       /* Nothing left to try.  Arrange to return as if we've consumed
  1819.          the entire input section.  Hopefully we'll get valid info from
  1820.          the other of .debug_frame/.eh_frame.  */
  1821.       workaround = FAIL;
  1822.       ret = unit->dwarf_frame_buffer + unit->dwarf_frame_size;
  1823.       break;
  1824.     }

  1825.   switch (workaround)
  1826.     {
  1827.     case NONE:
  1828.       break;

  1829.     case ALIGN4:
  1830.       complaint (&symfile_complaints, _("\
  1831. Corrupt data in %s:%s; align 4 workaround apparently succeeded"),
  1832.                  unit->dwarf_frame_section->owner->filename,
  1833.                  unit->dwarf_frame_section->name);
  1834.       break;

  1835.     case ALIGN8:
  1836.       complaint (&symfile_complaints, _("\
  1837. Corrupt data in %s:%s; align 8 workaround apparently succeeded"),
  1838.                  unit->dwarf_frame_section->owner->filename,
  1839.                  unit->dwarf_frame_section->name);
  1840.       break;

  1841.     default:
  1842.       complaint (&symfile_complaints,
  1843.                  _("Corrupt data in %s:%s"),
  1844.                  unit->dwarf_frame_section->owner->filename,
  1845.                  unit->dwarf_frame_section->name);
  1846.       break;
  1847.     }

  1848.   return ret;
  1849. }

  1850. static int
  1851. qsort_fde_cmp (const void *a, const void *b)
  1852. {
  1853.   struct dwarf2_fde *aa = *(struct dwarf2_fde **)a;
  1854.   struct dwarf2_fde *bb = *(struct dwarf2_fde **)b;

  1855.   if (aa->initial_location == bb->initial_location)
  1856.     {
  1857.       if (aa->address_range != bb->address_range
  1858.           && aa->eh_frame_p == 0 && bb->eh_frame_p == 0)
  1859.         /* Linker bug, e.g. gold/10400.
  1860.            Work around it by keeping stable sort order.  */
  1861.         return (a < b) ? -1 : 1;
  1862.       else
  1863.         /* Put eh_frame entries after debug_frame ones.  */
  1864.         return aa->eh_frame_p - bb->eh_frame_p;
  1865.     }

  1866.   return (aa->initial_location < bb->initial_location) ? -1 : 1;
  1867. }

  1868. void
  1869. dwarf2_build_frame_info (struct objfile *objfile)
  1870. {
  1871.   struct comp_unit *unit;
  1872.   const gdb_byte *frame_ptr;
  1873.   struct dwarf2_cie_table cie_table;
  1874.   struct dwarf2_fde_table fde_table;
  1875.   struct dwarf2_fde_table *fde_table2;
  1876.   volatile struct gdb_exception e;

  1877.   cie_table.num_entries = 0;
  1878.   cie_table.entries = NULL;

  1879.   fde_table.num_entries = 0;
  1880.   fde_table.entries = NULL;

  1881.   /* Build a minimal decoding of the DWARF2 compilation unit.  */
  1882.   unit = (struct comp_unit *) obstack_alloc (&objfile->objfile_obstack,
  1883.                                              sizeof (struct comp_unit));
  1884.   unit->abfd = objfile->obfd;
  1885.   unit->objfile = objfile;
  1886.   unit->dbase = 0;
  1887.   unit->tbase = 0;

  1888.   if (objfile->separate_debug_objfile_backlink == NULL)
  1889.     {
  1890.       /* Do not read .eh_frame from separate file as they must be also
  1891.          present in the main file.  */
  1892.       dwarf2_get_section_info (objfile, DWARF2_EH_FRAME,
  1893.                                &unit->dwarf_frame_section,
  1894.                                &unit->dwarf_frame_buffer,
  1895.                                &unit->dwarf_frame_size);
  1896.       if (unit->dwarf_frame_size)
  1897.         {
  1898.           asection *got, *txt;

  1899.           /* FIXME: kettenis/20030602: This is the DW_EH_PE_datarel base
  1900.              that is used for the i386/amd64 target, which currently is
  1901.              the only target in GCC that supports/uses the
  1902.              DW_EH_PE_datarel encoding.  */
  1903.           got = bfd_get_section_by_name (unit->abfd, ".got");
  1904.           if (got)
  1905.             unit->dbase = got->vma;

  1906.           /* GCC emits the DW_EH_PE_textrel encoding type on sh and ia64
  1907.              so far.  */
  1908.           txt = bfd_get_section_by_name (unit->abfd, ".text");
  1909.           if (txt)
  1910.             unit->tbase = txt->vma;

  1911.           TRY_CATCH (e, RETURN_MASK_ERROR)
  1912.             {
  1913.               frame_ptr = unit->dwarf_frame_buffer;
  1914.               while (frame_ptr < unit->dwarf_frame_buffer + unit->dwarf_frame_size)
  1915.                 frame_ptr = decode_frame_entry (unit, frame_ptr, 1,
  1916.                                                 &cie_table, &fde_table,
  1917.                                                 EH_CIE_OR_FDE_TYPE_ID);
  1918.             }

  1919.           if (e.reason < 0)
  1920.             {
  1921.               warning (_("skipping .eh_frame info of %s: %s"),
  1922.                        objfile_name (objfile), e.message);

  1923.               if (fde_table.num_entries != 0)
  1924.                 {
  1925.                   xfree (fde_table.entries);
  1926.                   fde_table.entries = NULL;
  1927.                   fde_table.num_entries = 0;
  1928.                 }
  1929.               /* The cie_table is discarded by the next if.  */
  1930.             }

  1931.           if (cie_table.num_entries != 0)
  1932.             {
  1933.               /* Reinit cie_table: debug_frame has different CIEs.  */
  1934.               xfree (cie_table.entries);
  1935.               cie_table.num_entries = 0;
  1936.               cie_table.entries = NULL;
  1937.             }
  1938.         }
  1939.     }

  1940.   dwarf2_get_section_info (objfile, DWARF2_DEBUG_FRAME,
  1941.                            &unit->dwarf_frame_section,
  1942.                            &unit->dwarf_frame_buffer,
  1943.                            &unit->dwarf_frame_size);
  1944.   if (unit->dwarf_frame_size)
  1945.     {
  1946.       int num_old_fde_entries = fde_table.num_entries;

  1947.       TRY_CATCH (e, RETURN_MASK_ERROR)
  1948.         {
  1949.           frame_ptr = unit->dwarf_frame_buffer;
  1950.           while (frame_ptr < unit->dwarf_frame_buffer + unit->dwarf_frame_size)
  1951.             frame_ptr = decode_frame_entry (unit, frame_ptr, 0,
  1952.                                             &cie_table, &fde_table,
  1953.                                             EH_CIE_OR_FDE_TYPE_ID);
  1954.         }
  1955.       if (e.reason < 0)
  1956.         {
  1957.           warning (_("skipping .debug_frame info of %s: %s"),
  1958.                    objfile_name (objfile), e.message);

  1959.           if (fde_table.num_entries != 0)
  1960.             {
  1961.               fde_table.num_entries = num_old_fde_entries;
  1962.               if (num_old_fde_entries == 0)
  1963.                 {
  1964.                   xfree (fde_table.entries);
  1965.                   fde_table.entries = NULL;
  1966.                 }
  1967.               else
  1968.                 {
  1969.                   fde_table.entries = xrealloc (fde_table.entries,
  1970.                                                 fde_table.num_entries *
  1971.                                                 sizeof (fde_table.entries[0]));
  1972.                 }
  1973.             }
  1974.           fde_table.num_entries = num_old_fde_entries;
  1975.           /* The cie_table is discarded by the next if.  */
  1976.         }
  1977.     }

  1978.   /* Discard the cie_table, it is no longer needed.  */
  1979.   if (cie_table.num_entries != 0)
  1980.     {
  1981.       xfree (cie_table.entries);
  1982.       cie_table.entries = NULL;   /* Paranoia.  */
  1983.       cie_table.num_entries = 0/* Paranoia.  */
  1984.     }

  1985.   /* Copy fde_table to obstack: it is needed at runtime.  */
  1986.   fde_table2 = (struct dwarf2_fde_table *)
  1987.     obstack_alloc (&objfile->objfile_obstack, sizeof (*fde_table2));

  1988.   if (fde_table.num_entries == 0)
  1989.     {
  1990.       fde_table2->entries = NULL;
  1991.       fde_table2->num_entries = 0;
  1992.     }
  1993.   else
  1994.     {
  1995.       struct dwarf2_fde *fde_prev = NULL;
  1996.       struct dwarf2_fde *first_non_zero_fde = NULL;
  1997.       int i;

  1998.       /* Prepare FDE table for lookups.  */
  1999.       qsort (fde_table.entries, fde_table.num_entries,
  2000.              sizeof (fde_table.entries[0]), qsort_fde_cmp);

  2001.       /* Check for leftovers from --gc-sections.  The GNU linker sets
  2002.          the relevant symbols to zero, but doesn't zero the FDE *end*
  2003.          ranges because there's no relocation there.  It's (offset,
  2004.          length), not (start, end).  On targets where address zero is
  2005.          just another valid address this can be a problem, since the
  2006.          FDEs appear to be non-empty in the output --- we could pick
  2007.          out the wrong FDE.  To work around this, when overlaps are
  2008.          detected, we prefer FDEs that do not start at zero.

  2009.          Start by finding the first FDE with non-zero start.  Below
  2010.          we'll discard all FDEs that start at zero and overlap this
  2011.          one.  */
  2012.       for (i = 0; i < fde_table.num_entries; i++)
  2013.         {
  2014.           struct dwarf2_fde *fde = fde_table.entries[i];

  2015.           if (fde->initial_location != 0)
  2016.             {
  2017.               first_non_zero_fde = fde;
  2018.               break;
  2019.             }
  2020.         }

  2021.       /* Since we'll be doing bsearch, squeeze out identical (except
  2022.          for eh_frame_p) fde entries so bsearch result is predictable.
  2023.          Also discard leftovers from --gc-sections.  */
  2024.       fde_table2->num_entries = 0;
  2025.       for (i = 0; i < fde_table.num_entries; i++)
  2026.         {
  2027.           struct dwarf2_fde *fde = fde_table.entries[i];

  2028.           if (fde->initial_location == 0
  2029.               && first_non_zero_fde != NULL
  2030.               && (first_non_zero_fde->initial_location
  2031.                   < fde->initial_location + fde->address_range))
  2032.             continue;

  2033.           if (fde_prev != NULL
  2034.               && fde_prev->initial_location == fde->initial_location)
  2035.             continue;

  2036.           obstack_grow (&objfile->objfile_obstack, &fde_table.entries[i],
  2037.                         sizeof (fde_table.entries[0]));
  2038.           ++fde_table2->num_entries;
  2039.           fde_prev = fde;
  2040.         }
  2041.       fde_table2->entries = obstack_finish (&objfile->objfile_obstack);

  2042.       /* Discard the original fde_table.  */
  2043.       xfree (fde_table.entries);
  2044.     }

  2045.   set_objfile_data (objfile, dwarf2_frame_objfile_data, fde_table2);
  2046. }

  2047. /* Provide a prototype to silence -Wmissing-prototypes.  */
  2048. void _initialize_dwarf2_frame (void);

  2049. void
  2050. _initialize_dwarf2_frame (void)
  2051. {
  2052.   dwarf2_frame_data = gdbarch_data_register_pre_init (dwarf2_frame_init);
  2053.   dwarf2_frame_objfile_data = register_objfile_data ();
  2054. }