gdb
[platform/upstream/binutils.git] / gdb / dwarf2-frame.c
1 /* Frame unwinder for frames with DWARF Call Frame Information.
2
3    Copyright (C) 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011
4    Free Software Foundation, Inc.
5
6    Contributed by Mark Kettenis.
7
8    This file is part of GDB.
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
22
23 #include "defs.h"
24 #include "dwarf2expr.h"
25 #include "dwarf2.h"
26 #include "frame.h"
27 #include "frame-base.h"
28 #include "frame-unwind.h"
29 #include "gdbcore.h"
30 #include "gdbtypes.h"
31 #include "symtab.h"
32 #include "objfiles.h"
33 #include "regcache.h"
34 #include "value.h"
35
36 #include "gdb_assert.h"
37 #include "gdb_string.h"
38
39 #include "complaints.h"
40 #include "dwarf2-frame.h"
41 #include "ax.h"
42 #include "dwarf2loc.h"
43 #include "exceptions.h"
44
45 struct comp_unit;
46
47 /* Call Frame Information (CFI).  */
48
49 /* Common Information Entry (CIE).  */
50
51 struct dwarf2_cie
52 {
53   /* Computation Unit for this CIE.  */
54   struct comp_unit *unit;
55
56   /* Offset into the .debug_frame section where this CIE was found.
57      Used to identify this CIE.  */
58   ULONGEST cie_pointer;
59
60   /* Constant that is factored out of all advance location
61      instructions.  */
62   ULONGEST code_alignment_factor;
63
64   /* Constants that is factored out of all offset instructions.  */
65   LONGEST data_alignment_factor;
66
67   /* Return address column.  */
68   ULONGEST return_address_register;
69
70   /* Instruction sequence to initialize a register set.  */
71   gdb_byte *initial_instructions;
72   gdb_byte *end;
73
74   /* Saved augmentation, in case it's needed later.  */
75   char *augmentation;
76
77   /* Encoding of addresses.  */
78   gdb_byte encoding;
79
80   /* Target address size in bytes.  */
81   int addr_size;
82
83   /* Target pointer size in bytes.  */
84   int ptr_size;
85
86   /* True if a 'z' augmentation existed.  */
87   unsigned char saw_z_augmentation;
88
89   /* True if an 'S' augmentation existed.  */
90   unsigned char signal_frame;
91
92   /* The version recorded in the CIE.  */
93   unsigned char version;
94
95   /* The segment size.  */
96   unsigned char segment_size;
97 };
98
99 struct dwarf2_cie_table
100 {
101   int num_entries;
102   struct dwarf2_cie **entries;
103 };
104
105 /* Frame Description Entry (FDE).  */
106
107 struct dwarf2_fde
108 {
109   /* CIE for this FDE.  */
110   struct dwarf2_cie *cie;
111
112   /* First location associated with this FDE.  */
113   CORE_ADDR initial_location;
114
115   /* Number of bytes of program instructions described by this FDE.  */
116   CORE_ADDR address_range;
117
118   /* Instruction sequence.  */
119   gdb_byte *instructions;
120   gdb_byte *end;
121
122   /* True if this FDE is read from a .eh_frame instead of a .debug_frame
123      section.  */
124   unsigned char eh_frame_p;
125 };
126
127 struct dwarf2_fde_table
128 {
129   int num_entries;
130   struct dwarf2_fde **entries;
131 };
132
133 /* A minimal decoding of DWARF2 compilation units.  We only decode
134    what's needed to get to the call frame information.  */
135
136 struct comp_unit
137 {
138   /* Keep the bfd convenient.  */
139   bfd *abfd;
140
141   struct objfile *objfile;
142
143   /* Pointer to the .debug_frame section loaded into memory.  */
144   gdb_byte *dwarf_frame_buffer;
145
146   /* Length of the loaded .debug_frame section.  */
147   bfd_size_type dwarf_frame_size;
148
149   /* Pointer to the .debug_frame section.  */
150   asection *dwarf_frame_section;
151
152   /* Base for DW_EH_PE_datarel encodings.  */
153   bfd_vma dbase;
154
155   /* Base for DW_EH_PE_textrel encodings.  */
156   bfd_vma tbase;
157 };
158
159 static struct dwarf2_fde *dwarf2_frame_find_fde (CORE_ADDR *pc,
160                                                  CORE_ADDR *out_offset);
161
162 static int dwarf2_frame_adjust_regnum (struct gdbarch *gdbarch, int regnum,
163                                        int eh_frame_p);
164
165 static CORE_ADDR read_encoded_value (struct comp_unit *unit, gdb_byte encoding,
166                                      int ptr_len, const gdb_byte *buf,
167                                      unsigned int *bytes_read_ptr,
168                                      CORE_ADDR func_base);
169 \f
170
171 /* Structure describing a frame state.  */
172
173 struct dwarf2_frame_state
174 {
175   /* Each register save state can be described in terms of a CFA slot,
176      another register, or a location expression.  */
177   struct dwarf2_frame_state_reg_info
178   {
179     struct dwarf2_frame_state_reg *reg;
180     int num_regs;
181
182     LONGEST cfa_offset;
183     ULONGEST cfa_reg;
184     enum {
185       CFA_UNSET,
186       CFA_REG_OFFSET,
187       CFA_EXP
188     } cfa_how;
189     const gdb_byte *cfa_exp;
190
191     /* Used to implement DW_CFA_remember_state.  */
192     struct dwarf2_frame_state_reg_info *prev;
193   } regs;
194
195   /* The PC described by the current frame state.  */
196   CORE_ADDR pc;
197
198   /* Initial register set from the CIE.
199      Used to implement DW_CFA_restore.  */
200   struct dwarf2_frame_state_reg_info initial;
201
202   /* The information we care about from the CIE.  */
203   LONGEST data_align;
204   ULONGEST code_align;
205   ULONGEST retaddr_column;
206
207   /* Flags for known producer quirks.  */
208
209   /* The ARM compilers, in DWARF2 mode, assume that DW_CFA_def_cfa
210      and DW_CFA_def_cfa_offset takes a factored offset.  */
211   int armcc_cfa_offsets_sf;
212
213   /* The ARM compilers, in DWARF2 or DWARF3 mode, may assume that
214      the CFA is defined as REG - OFFSET rather than REG + OFFSET.  */
215   int armcc_cfa_offsets_reversed;
216 };
217
218 /* Store the length the expression for the CFA in the `cfa_reg' field,
219    which is unused in that case.  */
220 #define cfa_exp_len cfa_reg
221
222 /* Assert that the register set RS is large enough to store gdbarch_num_regs
223    columns.  If necessary, enlarge the register set.  */
224
225 static void
226 dwarf2_frame_state_alloc_regs (struct dwarf2_frame_state_reg_info *rs,
227                                int num_regs)
228 {
229   size_t size = sizeof (struct dwarf2_frame_state_reg);
230
231   if (num_regs <= rs->num_regs)
232     return;
233
234   rs->reg = (struct dwarf2_frame_state_reg *)
235     xrealloc (rs->reg, num_regs * size);
236
237   /* Initialize newly allocated registers.  */
238   memset (rs->reg + rs->num_regs, 0, (num_regs - rs->num_regs) * size);
239   rs->num_regs = num_regs;
240 }
241
242 /* Copy the register columns in register set RS into newly allocated
243    memory and return a pointer to this newly created copy.  */
244
245 static struct dwarf2_frame_state_reg *
246 dwarf2_frame_state_copy_regs (struct dwarf2_frame_state_reg_info *rs)
247 {
248   size_t size = rs->num_regs * sizeof (struct dwarf2_frame_state_reg);
249   struct dwarf2_frame_state_reg *reg;
250
251   reg = (struct dwarf2_frame_state_reg *) xmalloc (size);
252   memcpy (reg, rs->reg, size);
253
254   return reg;
255 }
256
257 /* Release the memory allocated to register set RS.  */
258
259 static void
260 dwarf2_frame_state_free_regs (struct dwarf2_frame_state_reg_info *rs)
261 {
262   if (rs)
263     {
264       dwarf2_frame_state_free_regs (rs->prev);
265
266       xfree (rs->reg);
267       xfree (rs);
268     }
269 }
270
271 /* Release the memory allocated to the frame state FS.  */
272
273 static void
274 dwarf2_frame_state_free (void *p)
275 {
276   struct dwarf2_frame_state *fs = p;
277
278   dwarf2_frame_state_free_regs (fs->initial.prev);
279   dwarf2_frame_state_free_regs (fs->regs.prev);
280   xfree (fs->initial.reg);
281   xfree (fs->regs.reg);
282   xfree (fs);
283 }
284 \f
285
286 /* Helper functions for execute_stack_op.  */
287
288 static CORE_ADDR
289 read_reg (void *baton, int reg)
290 {
291   struct frame_info *this_frame = (struct frame_info *) baton;
292   struct gdbarch *gdbarch = get_frame_arch (this_frame);
293   int regnum;
294   gdb_byte *buf;
295
296   regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, reg);
297
298   buf = alloca (register_size (gdbarch, regnum));
299   get_frame_register (this_frame, regnum, buf);
300
301   /* Convert the register to an integer.  This returns a LONGEST
302      rather than a CORE_ADDR, but unpack_pointer does the same thing
303      under the covers, and this makes more sense for non-pointer
304      registers.  Maybe read_reg and the associated interfaces should
305      deal with "struct value" instead of CORE_ADDR.  */
306   return unpack_long (register_type (gdbarch, regnum), buf);
307 }
308
309 static void
310 read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
311 {
312   read_memory (addr, buf, len);
313 }
314
315 static void
316 no_get_frame_base (void *baton, const gdb_byte **start, size_t *length)
317 {
318   internal_error (__FILE__, __LINE__,
319                   _("Support for DW_OP_fbreg is unimplemented"));
320 }
321
322 /* Helper function for execute_stack_op.  */
323
324 static CORE_ADDR
325 no_get_frame_cfa (void *baton)
326 {
327   internal_error (__FILE__, __LINE__,
328                   _("Support for DW_OP_call_frame_cfa is unimplemented"));
329 }
330
331 /* Helper function for execute_stack_op.  */
332
333 static CORE_ADDR
334 no_get_frame_pc (void *baton)
335 {
336   internal_error (__FILE__, __LINE__, _("\
337 Support for DW_OP_GNU_implicit_pointer is unimplemented"));
338 }
339
340 static CORE_ADDR
341 no_get_tls_address (void *baton, CORE_ADDR offset)
342 {
343   internal_error (__FILE__, __LINE__, _("\
344 Support for DW_OP_GNU_push_tls_address is unimplemented"));
345 }
346
347 /* Helper function for execute_stack_op.  */
348
349 static void
350 no_dwarf_call (struct dwarf_expr_context *ctx, size_t die_offset)
351 {
352   internal_error (__FILE__, __LINE__,
353                   _("Support for DW_OP_call* is invalid in CFI"));
354 }
355
356 /* Helper function for execute_stack_op.  */
357
358 static struct type *
359 no_base_type (struct dwarf_expr_context *ctx, size_t die)
360 {
361   error (_("Support for typed DWARF is not supported in CFI"));
362 }
363
364 /* Execute the required actions for both the DW_CFA_restore and
365 DW_CFA_restore_extended instructions.  */
366 static void
367 dwarf2_restore_rule (struct gdbarch *gdbarch, ULONGEST reg_num,
368                      struct dwarf2_frame_state *fs, int eh_frame_p)
369 {
370   ULONGEST reg;
371
372   gdb_assert (fs->initial.reg);
373   reg = dwarf2_frame_adjust_regnum (gdbarch, reg_num, eh_frame_p);
374   dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
375
376   /* Check if this register was explicitly initialized in the
377   CIE initial instructions.  If not, default the rule to
378   UNSPECIFIED.  */
379   if (reg < fs->initial.num_regs)
380     fs->regs.reg[reg] = fs->initial.reg[reg];
381   else
382     fs->regs.reg[reg].how = DWARF2_FRAME_REG_UNSPECIFIED;
383
384   if (fs->regs.reg[reg].how == DWARF2_FRAME_REG_UNSPECIFIED)
385     complaint (&symfile_complaints, _("\
386 incomplete CFI data; DW_CFA_restore unspecified\n\
387 register %s (#%d) at %s"),
388                        gdbarch_register_name
389                        (gdbarch, gdbarch_dwarf2_reg_to_regnum (gdbarch, reg)),
390                        gdbarch_dwarf2_reg_to_regnum (gdbarch, reg),
391                        paddress (gdbarch, fs->pc));
392 }
393
394 static CORE_ADDR
395 execute_stack_op (const gdb_byte *exp, ULONGEST len, int addr_size,
396                   CORE_ADDR offset, struct frame_info *this_frame,
397                   CORE_ADDR initial, int initial_in_stack_memory)
398 {
399   struct dwarf_expr_context *ctx;
400   CORE_ADDR result;
401   struct cleanup *old_chain;
402
403   ctx = new_dwarf_expr_context ();
404   old_chain = make_cleanup_free_dwarf_expr_context (ctx);
405
406   ctx->gdbarch = get_frame_arch (this_frame);
407   ctx->addr_size = addr_size;
408   ctx->offset = offset;
409   ctx->baton = this_frame;
410   ctx->read_reg = read_reg;
411   ctx->read_mem = read_mem;
412   ctx->get_frame_base = no_get_frame_base;
413   ctx->get_frame_cfa = no_get_frame_cfa;
414   ctx->get_frame_pc = no_get_frame_pc;
415   ctx->get_tls_address = no_get_tls_address;
416   ctx->dwarf_call = no_dwarf_call;
417   ctx->get_base_type = no_base_type;
418
419   dwarf_expr_push_address (ctx, initial, initial_in_stack_memory);
420   dwarf_expr_eval (ctx, exp, len);
421
422   if (ctx->location == DWARF_VALUE_MEMORY)
423     result = dwarf_expr_fetch_address (ctx, 0);
424   else if (ctx->location == DWARF_VALUE_REGISTER)
425     result = read_reg (this_frame, value_as_long (dwarf_expr_fetch (ctx, 0)));
426   else
427     {
428       /* This is actually invalid DWARF, but if we ever do run across
429          it somehow, we might as well support it.  So, instead, report
430          it as unimplemented.  */
431       error (_("\
432 Not implemented: computing unwound register using explicit value operator"));
433     }
434
435   do_cleanups (old_chain);
436
437   return result;
438 }
439 \f
440
441 static void
442 execute_cfa_program (struct dwarf2_fde *fde, const gdb_byte *insn_ptr,
443                      const gdb_byte *insn_end, struct gdbarch *gdbarch,
444                      CORE_ADDR pc, struct dwarf2_frame_state *fs)
445 {
446   int eh_frame_p = fde->eh_frame_p;
447   int bytes_read;
448   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
449
450   while (insn_ptr < insn_end && fs->pc <= pc)
451     {
452       gdb_byte insn = *insn_ptr++;
453       ULONGEST utmp, reg;
454       LONGEST offset;
455
456       if ((insn & 0xc0) == DW_CFA_advance_loc)
457         fs->pc += (insn & 0x3f) * fs->code_align;
458       else if ((insn & 0xc0) == DW_CFA_offset)
459         {
460           reg = insn & 0x3f;
461           reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
462           insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp);
463           offset = utmp * fs->data_align;
464           dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
465           fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
466           fs->regs.reg[reg].loc.offset = offset;
467         }
468       else if ((insn & 0xc0) == DW_CFA_restore)
469         {
470           reg = insn & 0x3f;
471           dwarf2_restore_rule (gdbarch, reg, fs, eh_frame_p);
472         }
473       else
474         {
475           switch (insn)
476             {
477             case DW_CFA_set_loc:
478               fs->pc = read_encoded_value (fde->cie->unit, fde->cie->encoding,
479                                            fde->cie->ptr_size, insn_ptr,
480                                            &bytes_read, fde->initial_location);
481               /* Apply the objfile offset for relocatable objects.  */
482               fs->pc += ANOFFSET (fde->cie->unit->objfile->section_offsets,
483                                   SECT_OFF_TEXT (fde->cie->unit->objfile));
484               insn_ptr += bytes_read;
485               break;
486
487             case DW_CFA_advance_loc1:
488               utmp = extract_unsigned_integer (insn_ptr, 1, byte_order);
489               fs->pc += utmp * fs->code_align;
490               insn_ptr++;
491               break;
492             case DW_CFA_advance_loc2:
493               utmp = extract_unsigned_integer (insn_ptr, 2, byte_order);
494               fs->pc += utmp * fs->code_align;
495               insn_ptr += 2;
496               break;
497             case DW_CFA_advance_loc4:
498               utmp = extract_unsigned_integer (insn_ptr, 4, byte_order);
499               fs->pc += utmp * fs->code_align;
500               insn_ptr += 4;
501               break;
502
503             case DW_CFA_offset_extended:
504               insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
505               reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
506               insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp);
507               offset = utmp * fs->data_align;
508               dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
509               fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
510               fs->regs.reg[reg].loc.offset = offset;
511               break;
512
513             case DW_CFA_restore_extended:
514               insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
515               dwarf2_restore_rule (gdbarch, reg, fs, eh_frame_p);
516               break;
517
518             case DW_CFA_undefined:
519               insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
520               reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
521               dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
522               fs->regs.reg[reg].how = DWARF2_FRAME_REG_UNDEFINED;
523               break;
524
525             case DW_CFA_same_value:
526               insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
527               reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
528               dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
529               fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAME_VALUE;
530               break;
531
532             case DW_CFA_register:
533               insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
534               reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
535               insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp);
536               utmp = dwarf2_frame_adjust_regnum (gdbarch, utmp, eh_frame_p);
537               dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
538               fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_REG;
539               fs->regs.reg[reg].loc.reg = utmp;
540               break;
541
542             case DW_CFA_remember_state:
543               {
544                 struct dwarf2_frame_state_reg_info *new_rs;
545
546                 new_rs = XMALLOC (struct dwarf2_frame_state_reg_info);
547                 *new_rs = fs->regs;
548                 fs->regs.reg = dwarf2_frame_state_copy_regs (&fs->regs);
549                 fs->regs.prev = new_rs;
550               }
551               break;
552
553             case DW_CFA_restore_state:
554               {
555                 struct dwarf2_frame_state_reg_info *old_rs = fs->regs.prev;
556
557                 if (old_rs == NULL)
558                   {
559                     complaint (&symfile_complaints, _("\
560 bad CFI data; mismatched DW_CFA_restore_state at %s"),
561                                paddress (gdbarch, fs->pc));
562                   }
563                 else
564                   {
565                     xfree (fs->regs.reg);
566                     fs->regs = *old_rs;
567                     xfree (old_rs);
568                   }
569               }
570               break;
571
572             case DW_CFA_def_cfa:
573               insn_ptr = read_uleb128 (insn_ptr, insn_end, &fs->regs.cfa_reg);
574               insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp);
575
576               if (fs->armcc_cfa_offsets_sf)
577                 utmp *= fs->data_align;
578
579               fs->regs.cfa_offset = utmp;
580               fs->regs.cfa_how = CFA_REG_OFFSET;
581               break;
582
583             case DW_CFA_def_cfa_register:
584               insn_ptr = read_uleb128 (insn_ptr, insn_end, &fs->regs.cfa_reg);
585               fs->regs.cfa_reg = dwarf2_frame_adjust_regnum (gdbarch,
586                                                              fs->regs.cfa_reg,
587                                                              eh_frame_p);
588               fs->regs.cfa_how = CFA_REG_OFFSET;
589               break;
590
591             case DW_CFA_def_cfa_offset:
592               insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp);
593
594               if (fs->armcc_cfa_offsets_sf)
595                 utmp *= fs->data_align;
596
597               fs->regs.cfa_offset = utmp;
598               /* cfa_how deliberately not set.  */
599               break;
600
601             case DW_CFA_nop:
602               break;
603
604             case DW_CFA_def_cfa_expression:
605               insn_ptr = read_uleb128 (insn_ptr, insn_end,
606                                        &fs->regs.cfa_exp_len);
607               fs->regs.cfa_exp = insn_ptr;
608               fs->regs.cfa_how = CFA_EXP;
609               insn_ptr += fs->regs.cfa_exp_len;
610               break;
611
612             case DW_CFA_expression:
613               insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
614               reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
615               dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
616               insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp);
617               fs->regs.reg[reg].loc.exp = insn_ptr;
618               fs->regs.reg[reg].exp_len = utmp;
619               fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_EXP;
620               insn_ptr += utmp;
621               break;
622
623             case DW_CFA_offset_extended_sf:
624               insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
625               reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
626               insn_ptr = read_sleb128 (insn_ptr, insn_end, &offset);
627               offset *= fs->data_align;
628               dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
629               fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
630               fs->regs.reg[reg].loc.offset = offset;
631               break;
632
633             case DW_CFA_val_offset:
634               insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
635               dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
636               insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp);
637               offset = utmp * fs->data_align;
638               fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_VAL_OFFSET;
639               fs->regs.reg[reg].loc.offset = offset;
640               break;
641
642             case DW_CFA_val_offset_sf:
643               insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
644               dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
645               insn_ptr = read_sleb128 (insn_ptr, insn_end, &offset);
646               offset *= fs->data_align;
647               fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_VAL_OFFSET;
648               fs->regs.reg[reg].loc.offset = offset;
649               break;
650
651             case DW_CFA_val_expression:
652               insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
653               dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
654               insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp);
655               fs->regs.reg[reg].loc.exp = insn_ptr;
656               fs->regs.reg[reg].exp_len = utmp;
657               fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_VAL_EXP;
658               insn_ptr += utmp;
659               break;
660
661             case DW_CFA_def_cfa_sf:
662               insn_ptr = read_uleb128 (insn_ptr, insn_end, &fs->regs.cfa_reg);
663               fs->regs.cfa_reg = dwarf2_frame_adjust_regnum (gdbarch,
664                                                              fs->regs.cfa_reg,
665                                                              eh_frame_p);
666               insn_ptr = read_sleb128 (insn_ptr, insn_end, &offset);
667               fs->regs.cfa_offset = offset * fs->data_align;
668               fs->regs.cfa_how = CFA_REG_OFFSET;
669               break;
670
671             case DW_CFA_def_cfa_offset_sf:
672               insn_ptr = read_sleb128 (insn_ptr, insn_end, &offset);
673               fs->regs.cfa_offset = offset * fs->data_align;
674               /* cfa_how deliberately not set.  */
675               break;
676
677             case DW_CFA_GNU_window_save:
678               /* This is SPARC-specific code, and contains hard-coded
679                  constants for the register numbering scheme used by
680                  GCC.  Rather than having a architecture-specific
681                  operation that's only ever used by a single
682                  architecture, we provide the implementation here.
683                  Incidentally that's what GCC does too in its
684                  unwinder.  */
685               {
686                 int size = register_size (gdbarch, 0);
687
688                 dwarf2_frame_state_alloc_regs (&fs->regs, 32);
689                 for (reg = 8; reg < 16; reg++)
690                   {
691                     fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_REG;
692                     fs->regs.reg[reg].loc.reg = reg + 16;
693                   }
694                 for (reg = 16; reg < 32; reg++)
695                   {
696                     fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
697                     fs->regs.reg[reg].loc.offset = (reg - 16) * size;
698                   }
699               }
700               break;
701
702             case DW_CFA_GNU_args_size:
703               /* Ignored.  */
704               insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp);
705               break;
706
707             case DW_CFA_GNU_negative_offset_extended:
708               insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
709               reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
710               insn_ptr = read_uleb128 (insn_ptr, insn_end, &offset);
711               offset *= fs->data_align;
712               dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
713               fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
714               fs->regs.reg[reg].loc.offset = -offset;
715               break;
716
717             default:
718               internal_error (__FILE__, __LINE__,
719                               _("Unknown CFI encountered."));
720             }
721         }
722     }
723
724   /* Don't allow remember/restore between CIE and FDE programs.  */
725   dwarf2_frame_state_free_regs (fs->regs.prev);
726   fs->regs.prev = NULL;
727 }
728 \f
729
730 /* Architecture-specific operations.  */
731
732 /* Per-architecture data key.  */
733 static struct gdbarch_data *dwarf2_frame_data;
734
735 struct dwarf2_frame_ops
736 {
737   /* Pre-initialize the register state REG for register REGNUM.  */
738   void (*init_reg) (struct gdbarch *, int, struct dwarf2_frame_state_reg *,
739                     struct frame_info *);
740
741   /* Check whether the THIS_FRAME is a signal trampoline.  */
742   int (*signal_frame_p) (struct gdbarch *, struct frame_info *);
743
744   /* Convert .eh_frame register number to DWARF register number, or
745      adjust .debug_frame register number.  */
746   int (*adjust_regnum) (struct gdbarch *, int, int);
747 };
748
749 /* Default architecture-specific register state initialization
750    function.  */
751
752 static void
753 dwarf2_frame_default_init_reg (struct gdbarch *gdbarch, int regnum,
754                                struct dwarf2_frame_state_reg *reg,
755                                struct frame_info *this_frame)
756 {
757   /* If we have a register that acts as a program counter, mark it as
758      a destination for the return address.  If we have a register that
759      serves as the stack pointer, arrange for it to be filled with the
760      call frame address (CFA).  The other registers are marked as
761      unspecified.
762
763      We copy the return address to the program counter, since many
764      parts in GDB assume that it is possible to get the return address
765      by unwinding the program counter register.  However, on ISA's
766      with a dedicated return address register, the CFI usually only
767      contains information to unwind that return address register.
768
769      The reason we're treating the stack pointer special here is
770      because in many cases GCC doesn't emit CFI for the stack pointer
771      and implicitly assumes that it is equal to the CFA.  This makes
772      some sense since the DWARF specification (version 3, draft 8,
773      p. 102) says that:
774
775      "Typically, the CFA is defined to be the value of the stack
776      pointer at the call site in the previous frame (which may be
777      different from its value on entry to the current frame)."
778
779      However, this isn't true for all platforms supported by GCC
780      (e.g. IBM S/390 and zSeries).  Those architectures should provide
781      their own architecture-specific initialization function.  */
782
783   if (regnum == gdbarch_pc_regnum (gdbarch))
784     reg->how = DWARF2_FRAME_REG_RA;
785   else if (regnum == gdbarch_sp_regnum (gdbarch))
786     reg->how = DWARF2_FRAME_REG_CFA;
787 }
788
789 /* Return a default for the architecture-specific operations.  */
790
791 static void *
792 dwarf2_frame_init (struct obstack *obstack)
793 {
794   struct dwarf2_frame_ops *ops;
795   
796   ops = OBSTACK_ZALLOC (obstack, struct dwarf2_frame_ops);
797   ops->init_reg = dwarf2_frame_default_init_reg;
798   return ops;
799 }
800
801 /* Set the architecture-specific register state initialization
802    function for GDBARCH to INIT_REG.  */
803
804 void
805 dwarf2_frame_set_init_reg (struct gdbarch *gdbarch,
806                            void (*init_reg) (struct gdbarch *, int,
807                                              struct dwarf2_frame_state_reg *,
808                                              struct frame_info *))
809 {
810   struct dwarf2_frame_ops *ops = gdbarch_data (gdbarch, dwarf2_frame_data);
811
812   ops->init_reg = init_reg;
813 }
814
815 /* Pre-initialize the register state REG for register REGNUM.  */
816
817 static void
818 dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
819                        struct dwarf2_frame_state_reg *reg,
820                        struct frame_info *this_frame)
821 {
822   struct dwarf2_frame_ops *ops = gdbarch_data (gdbarch, dwarf2_frame_data);
823
824   ops->init_reg (gdbarch, regnum, reg, this_frame);
825 }
826
827 /* Set the architecture-specific signal trampoline recognition
828    function for GDBARCH to SIGNAL_FRAME_P.  */
829
830 void
831 dwarf2_frame_set_signal_frame_p (struct gdbarch *gdbarch,
832                                  int (*signal_frame_p) (struct gdbarch *,
833                                                         struct frame_info *))
834 {
835   struct dwarf2_frame_ops *ops = gdbarch_data (gdbarch, dwarf2_frame_data);
836
837   ops->signal_frame_p = signal_frame_p;
838 }
839
840 /* Query the architecture-specific signal frame recognizer for
841    THIS_FRAME.  */
842
843 static int
844 dwarf2_frame_signal_frame_p (struct gdbarch *gdbarch,
845                              struct frame_info *this_frame)
846 {
847   struct dwarf2_frame_ops *ops = gdbarch_data (gdbarch, dwarf2_frame_data);
848
849   if (ops->signal_frame_p == NULL)
850     return 0;
851   return ops->signal_frame_p (gdbarch, this_frame);
852 }
853
854 /* Set the architecture-specific adjustment of .eh_frame and .debug_frame
855    register numbers.  */
856
857 void
858 dwarf2_frame_set_adjust_regnum (struct gdbarch *gdbarch,
859                                 int (*adjust_regnum) (struct gdbarch *,
860                                                       int, int))
861 {
862   struct dwarf2_frame_ops *ops = gdbarch_data (gdbarch, dwarf2_frame_data);
863
864   ops->adjust_regnum = adjust_regnum;
865 }
866
867 /* Translate a .eh_frame register to DWARF register, or adjust a .debug_frame
868    register.  */
869
870 static int
871 dwarf2_frame_adjust_regnum (struct gdbarch *gdbarch,
872                             int regnum, int eh_frame_p)
873 {
874   struct dwarf2_frame_ops *ops = gdbarch_data (gdbarch, dwarf2_frame_data);
875
876   if (ops->adjust_regnum == NULL)
877     return regnum;
878   return ops->adjust_regnum (gdbarch, regnum, eh_frame_p);
879 }
880
881 static void
882 dwarf2_frame_find_quirks (struct dwarf2_frame_state *fs,
883                           struct dwarf2_fde *fde)
884 {
885   struct symtab *s;
886
887   s = find_pc_symtab (fs->pc);
888   if (s == NULL)
889     return;
890
891   if (producer_is_realview (s->producer))
892     {
893       if (fde->cie->version == 1)
894         fs->armcc_cfa_offsets_sf = 1;
895
896       if (fde->cie->version == 1)
897         fs->armcc_cfa_offsets_reversed = 1;
898
899       /* The reversed offset problem is present in some compilers
900          using DWARF3, but it was eventually fixed.  Check the ARM
901          defined augmentations, which are in the format "armcc" followed
902          by a list of one-character options.  The "+" option means
903          this problem is fixed (no quirk needed).  If the armcc
904          augmentation is missing, the quirk is needed.  */
905       if (fde->cie->version == 3
906           && (strncmp (fde->cie->augmentation, "armcc", 5) != 0
907               || strchr (fde->cie->augmentation + 5, '+') == NULL))
908         fs->armcc_cfa_offsets_reversed = 1;
909
910       return;
911     }
912 }
913 \f
914
915 void
916 dwarf2_compile_cfa_to_ax (struct agent_expr *expr, struct axs_value *loc,
917                           struct gdbarch *gdbarch,
918                           CORE_ADDR pc,
919                           struct dwarf2_per_cu_data *data)
920 {
921   const int num_regs = gdbarch_num_regs (gdbarch)
922                        + gdbarch_num_pseudo_regs (gdbarch);
923   struct dwarf2_fde *fde;
924   CORE_ADDR text_offset, cfa;
925   struct dwarf2_frame_state fs;
926   int addr_size;
927
928   memset (&fs, 0, sizeof (struct dwarf2_frame_state));
929
930   fs.pc = pc;
931
932   /* Find the correct FDE.  */
933   fde = dwarf2_frame_find_fde (&fs.pc, &text_offset);
934   if (fde == NULL)
935     error (_("Could not compute CFA; needed to translate this expression"));
936
937   /* Extract any interesting information from the CIE.  */
938   fs.data_align = fde->cie->data_alignment_factor;
939   fs.code_align = fde->cie->code_alignment_factor;
940   fs.retaddr_column = fde->cie->return_address_register;
941   addr_size = fde->cie->addr_size;
942
943   /* Check for "quirks" - known bugs in producers.  */
944   dwarf2_frame_find_quirks (&fs, fde);
945
946   /* First decode all the insns in the CIE.  */
947   execute_cfa_program (fde, fde->cie->initial_instructions,
948                        fde->cie->end, gdbarch, pc, &fs);
949
950   /* Save the initialized register set.  */
951   fs.initial = fs.regs;
952   fs.initial.reg = dwarf2_frame_state_copy_regs (&fs.regs);
953
954   /* Then decode the insns in the FDE up to our target PC.  */
955   execute_cfa_program (fde, fde->instructions, fde->end, gdbarch, pc, &fs);
956
957   /* Calculate the CFA.  */
958   switch (fs.regs.cfa_how)
959     {
960     case CFA_REG_OFFSET:
961       {
962         int regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, fs.regs.cfa_reg);
963
964         if (regnum == -1)
965           error (_("Unable to access DWARF register number %d"),
966                  (int) fs.regs.cfa_reg); /* FIXME */
967         ax_reg (expr, regnum);
968
969         if (fs.regs.cfa_offset != 0)
970           {
971             if (fs.armcc_cfa_offsets_reversed)
972               ax_const_l (expr, -fs.regs.cfa_offset);
973             else
974               ax_const_l (expr, fs.regs.cfa_offset);
975             ax_simple (expr, aop_add);
976           }
977       }
978       break;
979
980     case CFA_EXP:
981       ax_const_l (expr, text_offset);
982       dwarf2_compile_expr_to_ax (expr, loc, gdbarch, addr_size,
983                                  fs.regs.cfa_exp,
984                                  fs.regs.cfa_exp + fs.regs.cfa_exp_len,
985                                  data);
986       break;
987
988     default:
989       internal_error (__FILE__, __LINE__, _("Unknown CFA rule."));
990     }
991 }
992
993 \f
994 struct dwarf2_frame_cache
995 {
996   /* DWARF Call Frame Address.  */
997   CORE_ADDR cfa;
998
999   /* Set if the return address column was marked as unavailable
1000      (required non-collected memory or registers to compute).  */
1001   int unavailable_retaddr;
1002
1003   /* Set if the return address column was marked as undefined.  */
1004   int undefined_retaddr;
1005
1006   /* Saved registers, indexed by GDB register number, not by DWARF
1007      register number.  */
1008   struct dwarf2_frame_state_reg *reg;
1009
1010   /* Return address register.  */
1011   struct dwarf2_frame_state_reg retaddr_reg;
1012
1013   /* Target address size in bytes.  */
1014   int addr_size;
1015
1016   /* The .text offset.  */
1017   CORE_ADDR text_offset;
1018 };
1019
1020 static struct dwarf2_frame_cache *
1021 dwarf2_frame_cache (struct frame_info *this_frame, void **this_cache)
1022 {
1023   struct cleanup *old_chain;
1024   struct gdbarch *gdbarch = get_frame_arch (this_frame);
1025   const int num_regs = gdbarch_num_regs (gdbarch)
1026                        + gdbarch_num_pseudo_regs (gdbarch);
1027   struct dwarf2_frame_cache *cache;
1028   struct dwarf2_frame_state *fs;
1029   struct dwarf2_fde *fde;
1030   volatile struct gdb_exception ex;
1031
1032   if (*this_cache)
1033     return *this_cache;
1034
1035   /* Allocate a new cache.  */
1036   cache = FRAME_OBSTACK_ZALLOC (struct dwarf2_frame_cache);
1037   cache->reg = FRAME_OBSTACK_CALLOC (num_regs, struct dwarf2_frame_state_reg);
1038   *this_cache = cache;
1039
1040   /* Allocate and initialize the frame state.  */
1041   fs = XZALLOC (struct dwarf2_frame_state);
1042   old_chain = make_cleanup (dwarf2_frame_state_free, fs);
1043
1044   /* Unwind the PC.
1045
1046      Note that if the next frame is never supposed to return (i.e. a call
1047      to abort), the compiler might optimize away the instruction at
1048      its return address.  As a result the return address will
1049      point at some random instruction, and the CFI for that
1050      instruction is probably worthless to us.  GCC's unwinder solves
1051      this problem by substracting 1 from the return address to get an
1052      address in the middle of a presumed call instruction (or the
1053      instruction in the associated delay slot).  This should only be
1054      done for "normal" frames and not for resume-type frames (signal
1055      handlers, sentinel frames, dummy frames).  The function
1056      get_frame_address_in_block does just this.  It's not clear how
1057      reliable the method is though; there is the potential for the
1058      register state pre-call being different to that on return.  */
1059   fs->pc = get_frame_address_in_block (this_frame);
1060
1061   /* Find the correct FDE.  */
1062   fde = dwarf2_frame_find_fde (&fs->pc, &cache->text_offset);
1063   gdb_assert (fde != NULL);
1064
1065   /* Extract any interesting information from the CIE.  */
1066   fs->data_align = fde->cie->data_alignment_factor;
1067   fs->code_align = fde->cie->code_alignment_factor;
1068   fs->retaddr_column = fde->cie->return_address_register;
1069   cache->addr_size = fde->cie->addr_size;
1070
1071   /* Check for "quirks" - known bugs in producers.  */
1072   dwarf2_frame_find_quirks (fs, fde);
1073
1074   /* First decode all the insns in the CIE.  */
1075   execute_cfa_program (fde, fde->cie->initial_instructions,
1076                        fde->cie->end, gdbarch, get_frame_pc (this_frame), fs);
1077
1078   /* Save the initialized register set.  */
1079   fs->initial = fs->regs;
1080   fs->initial.reg = dwarf2_frame_state_copy_regs (&fs->regs);
1081
1082   /* Then decode the insns in the FDE up to our target PC.  */
1083   execute_cfa_program (fde, fde->instructions, fde->end, gdbarch,
1084                        get_frame_pc (this_frame), fs);
1085
1086   TRY_CATCH (ex, RETURN_MASK_ERROR)
1087     {
1088       /* Calculate the CFA.  */
1089       switch (fs->regs.cfa_how)
1090         {
1091         case CFA_REG_OFFSET:
1092           cache->cfa = read_reg (this_frame, fs->regs.cfa_reg);
1093           if (fs->armcc_cfa_offsets_reversed)
1094             cache->cfa -= fs->regs.cfa_offset;
1095           else
1096             cache->cfa += fs->regs.cfa_offset;
1097           break;
1098
1099         case CFA_EXP:
1100           cache->cfa =
1101             execute_stack_op (fs->regs.cfa_exp, fs->regs.cfa_exp_len,
1102                               cache->addr_size, cache->text_offset,
1103                               this_frame, 0, 0);
1104           break;
1105
1106         default:
1107           internal_error (__FILE__, __LINE__, _("Unknown CFA rule."));
1108         }
1109     }
1110   if (ex.reason < 0)
1111     {
1112       if (ex.error == NOT_AVAILABLE_ERROR)
1113         {
1114           cache->unavailable_retaddr = 1;
1115           return cache;
1116         }
1117
1118       throw_exception (ex);
1119     }
1120
1121   /* Initialize the register state.  */
1122   {
1123     int regnum;
1124
1125     for (regnum = 0; regnum < num_regs; regnum++)
1126       dwarf2_frame_init_reg (gdbarch, regnum, &cache->reg[regnum], this_frame);
1127   }
1128
1129   /* Go through the DWARF2 CFI generated table and save its register
1130      location information in the cache.  Note that we don't skip the
1131      return address column; it's perfectly all right for it to
1132      correspond to a real register.  If it doesn't correspond to a
1133      real register, or if we shouldn't treat it as such,
1134      gdbarch_dwarf2_reg_to_regnum should be defined to return a number outside
1135      the range [0, gdbarch_num_regs).  */
1136   {
1137     int column;         /* CFI speak for "register number".  */
1138
1139     for (column = 0; column < fs->regs.num_regs; column++)
1140       {
1141         /* Use the GDB register number as the destination index.  */
1142         int regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, column);
1143
1144         /* If there's no corresponding GDB register, ignore it.  */
1145         if (regnum < 0 || regnum >= num_regs)
1146           continue;
1147
1148         /* NOTE: cagney/2003-09-05: CFI should specify the disposition
1149            of all debug info registers.  If it doesn't, complain (but
1150            not too loudly).  It turns out that GCC assumes that an
1151            unspecified register implies "same value" when CFI (draft
1152            7) specifies nothing at all.  Such a register could equally
1153            be interpreted as "undefined".  Also note that this check
1154            isn't sufficient; it only checks that all registers in the
1155            range [0 .. max column] are specified, and won't detect
1156            problems when a debug info register falls outside of the
1157            table.  We need a way of iterating through all the valid
1158            DWARF2 register numbers.  */
1159         if (fs->regs.reg[column].how == DWARF2_FRAME_REG_UNSPECIFIED)
1160           {
1161             if (cache->reg[regnum].how == DWARF2_FRAME_REG_UNSPECIFIED)
1162               complaint (&symfile_complaints, _("\
1163 incomplete CFI data; unspecified registers (e.g., %s) at %s"),
1164                          gdbarch_register_name (gdbarch, regnum),
1165                          paddress (gdbarch, fs->pc));
1166           }
1167         else
1168           cache->reg[regnum] = fs->regs.reg[column];
1169       }
1170   }
1171
1172   /* Eliminate any DWARF2_FRAME_REG_RA rules, and save the information
1173      we need for evaluating DWARF2_FRAME_REG_RA_OFFSET rules.  */
1174   {
1175     int regnum;
1176
1177     for (regnum = 0; regnum < num_regs; regnum++)
1178       {
1179         if (cache->reg[regnum].how == DWARF2_FRAME_REG_RA
1180             || cache->reg[regnum].how == DWARF2_FRAME_REG_RA_OFFSET)
1181           {
1182             struct dwarf2_frame_state_reg *retaddr_reg =
1183               &fs->regs.reg[fs->retaddr_column];
1184
1185             /* It seems rather bizarre to specify an "empty" column as
1186                the return adress column.  However, this is exactly
1187                what GCC does on some targets.  It turns out that GCC
1188                assumes that the return address can be found in the
1189                register corresponding to the return address column.
1190                Incidentally, that's how we should treat a return
1191                address column specifying "same value" too.  */
1192             if (fs->retaddr_column < fs->regs.num_regs
1193                 && retaddr_reg->how != DWARF2_FRAME_REG_UNSPECIFIED
1194                 && retaddr_reg->how != DWARF2_FRAME_REG_SAME_VALUE)
1195               {
1196                 if (cache->reg[regnum].how == DWARF2_FRAME_REG_RA)
1197                   cache->reg[regnum] = *retaddr_reg;
1198                 else
1199                   cache->retaddr_reg = *retaddr_reg;
1200               }
1201             else
1202               {
1203                 if (cache->reg[regnum].how == DWARF2_FRAME_REG_RA)
1204                   {
1205                     cache->reg[regnum].loc.reg = fs->retaddr_column;
1206                     cache->reg[regnum].how = DWARF2_FRAME_REG_SAVED_REG;
1207                   }
1208                 else
1209                   {
1210                     cache->retaddr_reg.loc.reg = fs->retaddr_column;
1211                     cache->retaddr_reg.how = DWARF2_FRAME_REG_SAVED_REG;
1212                   }
1213               }
1214           }
1215       }
1216   }
1217
1218   if (fs->retaddr_column < fs->regs.num_regs
1219       && fs->regs.reg[fs->retaddr_column].how == DWARF2_FRAME_REG_UNDEFINED)
1220     cache->undefined_retaddr = 1;
1221
1222   do_cleanups (old_chain);
1223
1224   return cache;
1225 }
1226
1227 static enum unwind_stop_reason
1228 dwarf2_frame_unwind_stop_reason (struct frame_info *this_frame,
1229                                  void **this_cache)
1230 {
1231   struct dwarf2_frame_cache *cache
1232     = dwarf2_frame_cache (this_frame, this_cache);
1233
1234   if (cache->unavailable_retaddr)
1235     return UNWIND_UNAVAILABLE;
1236
1237   if (cache->undefined_retaddr)
1238     return UNWIND_OUTERMOST;
1239
1240   return UNWIND_NO_REASON;
1241 }
1242
1243 static void
1244 dwarf2_frame_this_id (struct frame_info *this_frame, void **this_cache,
1245                       struct frame_id *this_id)
1246 {
1247   struct dwarf2_frame_cache *cache =
1248     dwarf2_frame_cache (this_frame, this_cache);
1249
1250   if (cache->unavailable_retaddr)
1251     return;
1252
1253   if (cache->undefined_retaddr)
1254     return;
1255
1256   (*this_id) = frame_id_build (cache->cfa, get_frame_func (this_frame));
1257 }
1258
1259 static struct value *
1260 dwarf2_frame_prev_register (struct frame_info *this_frame, void **this_cache,
1261                             int regnum)
1262 {
1263   struct gdbarch *gdbarch = get_frame_arch (this_frame);
1264   struct dwarf2_frame_cache *cache =
1265     dwarf2_frame_cache (this_frame, this_cache);
1266   CORE_ADDR addr;
1267   int realnum;
1268
1269   switch (cache->reg[regnum].how)
1270     {
1271     case DWARF2_FRAME_REG_UNDEFINED:
1272       /* If CFI explicitly specified that the value isn't defined,
1273          mark it as optimized away; the value isn't available.  */
1274       return frame_unwind_got_optimized (this_frame, regnum);
1275
1276     case DWARF2_FRAME_REG_SAVED_OFFSET:
1277       addr = cache->cfa + cache->reg[regnum].loc.offset;
1278       return frame_unwind_got_memory (this_frame, regnum, addr);
1279
1280     case DWARF2_FRAME_REG_SAVED_REG:
1281       realnum
1282         = gdbarch_dwarf2_reg_to_regnum (gdbarch, cache->reg[regnum].loc.reg);
1283       return frame_unwind_got_register (this_frame, regnum, realnum);
1284
1285     case DWARF2_FRAME_REG_SAVED_EXP:
1286       addr = execute_stack_op (cache->reg[regnum].loc.exp,
1287                                cache->reg[regnum].exp_len,
1288                                cache->addr_size, cache->text_offset,
1289                                this_frame, cache->cfa, 1);
1290       return frame_unwind_got_memory (this_frame, regnum, addr);
1291
1292     case DWARF2_FRAME_REG_SAVED_VAL_OFFSET:
1293       addr = cache->cfa + cache->reg[regnum].loc.offset;
1294       return frame_unwind_got_constant (this_frame, regnum, addr);
1295
1296     case DWARF2_FRAME_REG_SAVED_VAL_EXP:
1297       addr = execute_stack_op (cache->reg[regnum].loc.exp,
1298                                cache->reg[regnum].exp_len,
1299                                cache->addr_size, cache->text_offset,
1300                                this_frame, cache->cfa, 1);
1301       return frame_unwind_got_constant (this_frame, regnum, addr);
1302
1303     case DWARF2_FRAME_REG_UNSPECIFIED:
1304       /* GCC, in its infinite wisdom decided to not provide unwind
1305          information for registers that are "same value".  Since
1306          DWARF2 (3 draft 7) doesn't define such behavior, said
1307          registers are actually undefined (which is different to CFI
1308          "undefined").  Code above issues a complaint about this.
1309          Here just fudge the books, assume GCC, and that the value is
1310          more inner on the stack.  */
1311       return frame_unwind_got_register (this_frame, regnum, regnum);
1312
1313     case DWARF2_FRAME_REG_SAME_VALUE:
1314       return frame_unwind_got_register (this_frame, regnum, regnum);
1315
1316     case DWARF2_FRAME_REG_CFA:
1317       return frame_unwind_got_address (this_frame, regnum, cache->cfa);
1318
1319     case DWARF2_FRAME_REG_CFA_OFFSET:
1320       addr = cache->cfa + cache->reg[regnum].loc.offset;
1321       return frame_unwind_got_address (this_frame, regnum, addr);
1322
1323     case DWARF2_FRAME_REG_RA_OFFSET:
1324       addr = cache->reg[regnum].loc.offset;
1325       regnum = gdbarch_dwarf2_reg_to_regnum
1326         (gdbarch, cache->retaddr_reg.loc.reg);
1327       addr += get_frame_register_unsigned (this_frame, regnum);
1328       return frame_unwind_got_address (this_frame, regnum, addr);
1329
1330     case DWARF2_FRAME_REG_FN:
1331       return cache->reg[regnum].loc.fn (this_frame, this_cache, regnum);
1332
1333     default:
1334       internal_error (__FILE__, __LINE__, _("Unknown register rule."));
1335     }
1336 }
1337
1338 static int
1339 dwarf2_frame_sniffer (const struct frame_unwind *self,
1340                       struct frame_info *this_frame, void **this_cache)
1341 {
1342   /* Grab an address that is guarenteed to reside somewhere within the
1343      function.  get_frame_pc(), with a no-return next function, can
1344      end up returning something past the end of this function's body.
1345      If the frame we're sniffing for is a signal frame whose start
1346      address is placed on the stack by the OS, its FDE must
1347      extend one byte before its start address or we could potentially
1348      select the FDE of the previous function.  */
1349   CORE_ADDR block_addr = get_frame_address_in_block (this_frame);
1350   struct dwarf2_fde *fde = dwarf2_frame_find_fde (&block_addr, NULL);
1351
1352   if (!fde)
1353     return 0;
1354
1355   /* On some targets, signal trampolines may have unwind information.
1356      We need to recognize them so that we set the frame type
1357      correctly.  */
1358
1359   if (fde->cie->signal_frame
1360       || dwarf2_frame_signal_frame_p (get_frame_arch (this_frame),
1361                                       this_frame))
1362     return self->type == SIGTRAMP_FRAME;
1363
1364   return self->type != SIGTRAMP_FRAME;
1365 }
1366
1367 static const struct frame_unwind dwarf2_frame_unwind =
1368 {
1369   NORMAL_FRAME,
1370   dwarf2_frame_unwind_stop_reason,
1371   dwarf2_frame_this_id,
1372   dwarf2_frame_prev_register,
1373   NULL,
1374   dwarf2_frame_sniffer
1375 };
1376
1377 static const struct frame_unwind dwarf2_signal_frame_unwind =
1378 {
1379   SIGTRAMP_FRAME,
1380   dwarf2_frame_unwind_stop_reason,
1381   dwarf2_frame_this_id,
1382   dwarf2_frame_prev_register,
1383   NULL,
1384   dwarf2_frame_sniffer
1385 };
1386
1387 /* Append the DWARF-2 frame unwinders to GDBARCH's list.  */
1388
1389 void
1390 dwarf2_append_unwinders (struct gdbarch *gdbarch)
1391 {
1392   frame_unwind_append_unwinder (gdbarch, &dwarf2_frame_unwind);
1393   frame_unwind_append_unwinder (gdbarch, &dwarf2_signal_frame_unwind);
1394 }
1395 \f
1396
1397 /* There is no explicitly defined relationship between the CFA and the
1398    location of frame's local variables and arguments/parameters.
1399    Therefore, frame base methods on this page should probably only be
1400    used as a last resort, just to avoid printing total garbage as a
1401    response to the "info frame" command.  */
1402
1403 static CORE_ADDR
1404 dwarf2_frame_base_address (struct frame_info *this_frame, void **this_cache)
1405 {
1406   struct dwarf2_frame_cache *cache =
1407     dwarf2_frame_cache (this_frame, this_cache);
1408
1409   return cache->cfa;
1410 }
1411
1412 static const struct frame_base dwarf2_frame_base =
1413 {
1414   &dwarf2_frame_unwind,
1415   dwarf2_frame_base_address,
1416   dwarf2_frame_base_address,
1417   dwarf2_frame_base_address
1418 };
1419
1420 const struct frame_base *
1421 dwarf2_frame_base_sniffer (struct frame_info *this_frame)
1422 {
1423   CORE_ADDR block_addr = get_frame_address_in_block (this_frame);
1424
1425   if (dwarf2_frame_find_fde (&block_addr, NULL))
1426     return &dwarf2_frame_base;
1427
1428   return NULL;
1429 }
1430
1431 /* Compute the CFA for THIS_FRAME, but only if THIS_FRAME came from
1432    the DWARF unwinder.  This is used to implement
1433    DW_OP_call_frame_cfa.  */
1434
1435 CORE_ADDR
1436 dwarf2_frame_cfa (struct frame_info *this_frame)
1437 {
1438   while (get_frame_type (this_frame) == INLINE_FRAME)
1439     this_frame = get_prev_frame (this_frame);
1440   /* This restriction could be lifted if other unwinders are known to
1441      compute the frame base in a way compatible with the DWARF
1442      unwinder.  */
1443   if (! frame_unwinder_is (this_frame, &dwarf2_frame_unwind))
1444     error (_("can't compute CFA for this frame"));
1445   return get_frame_base (this_frame);
1446 }
1447 \f
1448 const struct objfile_data *dwarf2_frame_objfile_data;
1449
1450 static unsigned int
1451 read_1_byte (bfd *abfd, gdb_byte *buf)
1452 {
1453   return bfd_get_8 (abfd, buf);
1454 }
1455
1456 static unsigned int
1457 read_4_bytes (bfd *abfd, gdb_byte *buf)
1458 {
1459   return bfd_get_32 (abfd, buf);
1460 }
1461
1462 static ULONGEST
1463 read_8_bytes (bfd *abfd, gdb_byte *buf)
1464 {
1465   return bfd_get_64 (abfd, buf);
1466 }
1467
1468 static ULONGEST
1469 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
1470 {
1471   ULONGEST result;
1472   unsigned int num_read;
1473   int shift;
1474   gdb_byte byte;
1475
1476   result = 0;
1477   shift = 0;
1478   num_read = 0;
1479
1480   do
1481     {
1482       byte = bfd_get_8 (abfd, (bfd_byte *) buf);
1483       buf++;
1484       num_read++;
1485       result |= ((byte & 0x7f) << shift);
1486       shift += 7;
1487     }
1488   while (byte & 0x80);
1489
1490   *bytes_read_ptr = num_read;
1491
1492   return result;
1493 }
1494
1495 static LONGEST
1496 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
1497 {
1498   LONGEST result;
1499   int shift;
1500   unsigned int num_read;
1501   gdb_byte byte;
1502
1503   result = 0;
1504   shift = 0;
1505   num_read = 0;
1506
1507   do
1508     {
1509       byte = bfd_get_8 (abfd, (bfd_byte *) buf);
1510       buf++;
1511       num_read++;
1512       result |= ((byte & 0x7f) << shift);
1513       shift += 7;
1514     }
1515   while (byte & 0x80);
1516
1517   if (shift < 8 * sizeof (result) && (byte & 0x40))
1518     result |= -(((LONGEST)1) << shift);
1519
1520   *bytes_read_ptr = num_read;
1521
1522   return result;
1523 }
1524
1525 static ULONGEST
1526 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
1527 {
1528   LONGEST result;
1529
1530   result = bfd_get_32 (abfd, buf);
1531   if (result == 0xffffffff)
1532     {
1533       result = bfd_get_64 (abfd, buf + 4);
1534       *bytes_read_ptr = 12;
1535     }
1536   else
1537     *bytes_read_ptr = 4;
1538
1539   return result;
1540 }
1541 \f
1542
1543 /* Pointer encoding helper functions.  */
1544
1545 /* GCC supports exception handling based on DWARF2 CFI.  However, for
1546    technical reasons, it encodes addresses in its FDE's in a different
1547    way.  Several "pointer encodings" are supported.  The encoding
1548    that's used for a particular FDE is determined by the 'R'
1549    augmentation in the associated CIE.  The argument of this
1550    augmentation is a single byte.  
1551
1552    The address can be encoded as 2 bytes, 4 bytes, 8 bytes, or as a
1553    LEB128.  This is encoded in bits 0, 1 and 2.  Bit 3 encodes whether
1554    the address is signed or unsigned.  Bits 4, 5 and 6 encode how the
1555    address should be interpreted (absolute, relative to the current
1556    position in the FDE, ...).  Bit 7, indicates that the address
1557    should be dereferenced.  */
1558
1559 static gdb_byte
1560 encoding_for_size (unsigned int size)
1561 {
1562   switch (size)
1563     {
1564     case 2:
1565       return DW_EH_PE_udata2;
1566     case 4:
1567       return DW_EH_PE_udata4;
1568     case 8:
1569       return DW_EH_PE_udata8;
1570     default:
1571       internal_error (__FILE__, __LINE__, _("Unsupported address size"));
1572     }
1573 }
1574
1575 static CORE_ADDR
1576 read_encoded_value (struct comp_unit *unit, gdb_byte encoding,
1577                     int ptr_len, const gdb_byte *buf,
1578                     unsigned int *bytes_read_ptr,
1579                     CORE_ADDR func_base)
1580 {
1581   ptrdiff_t offset;
1582   CORE_ADDR base;
1583
1584   /* GCC currently doesn't generate DW_EH_PE_indirect encodings for
1585      FDE's.  */
1586   if (encoding & DW_EH_PE_indirect)
1587     internal_error (__FILE__, __LINE__, 
1588                     _("Unsupported encoding: DW_EH_PE_indirect"));
1589
1590   *bytes_read_ptr = 0;
1591
1592   switch (encoding & 0x70)
1593     {
1594     case DW_EH_PE_absptr:
1595       base = 0;
1596       break;
1597     case DW_EH_PE_pcrel:
1598       base = bfd_get_section_vma (unit->abfd, unit->dwarf_frame_section);
1599       base += (buf - unit->dwarf_frame_buffer);
1600       break;
1601     case DW_EH_PE_datarel:
1602       base = unit->dbase;
1603       break;
1604     case DW_EH_PE_textrel:
1605       base = unit->tbase;
1606       break;
1607     case DW_EH_PE_funcrel:
1608       base = func_base;
1609       break;
1610     case DW_EH_PE_aligned:
1611       base = 0;
1612       offset = buf - unit->dwarf_frame_buffer;
1613       if ((offset % ptr_len) != 0)
1614         {
1615           *bytes_read_ptr = ptr_len - (offset % ptr_len);
1616           buf += *bytes_read_ptr;
1617         }
1618       break;
1619     default:
1620       internal_error (__FILE__, __LINE__,
1621                       _("Invalid or unsupported encoding"));
1622     }
1623
1624   if ((encoding & 0x07) == 0x00)
1625     {
1626       encoding |= encoding_for_size (ptr_len);
1627       if (bfd_get_sign_extend_vma (unit->abfd))
1628         encoding |= DW_EH_PE_signed;
1629     }
1630
1631   switch (encoding & 0x0f)
1632     {
1633     case DW_EH_PE_uleb128:
1634       {
1635         ULONGEST value;
1636         const gdb_byte *end_buf = buf + (sizeof (value) + 1) * 8 / 7;
1637
1638         *bytes_read_ptr += read_uleb128 (buf, end_buf, &value) - buf;
1639         return base + value;
1640       }
1641     case DW_EH_PE_udata2:
1642       *bytes_read_ptr += 2;
1643       return (base + bfd_get_16 (unit->abfd, (bfd_byte *) buf));
1644     case DW_EH_PE_udata4:
1645       *bytes_read_ptr += 4;
1646       return (base + bfd_get_32 (unit->abfd, (bfd_byte *) buf));
1647     case DW_EH_PE_udata8:
1648       *bytes_read_ptr += 8;
1649       return (base + bfd_get_64 (unit->abfd, (bfd_byte *) buf));
1650     case DW_EH_PE_sleb128:
1651       {
1652         LONGEST value;
1653         const gdb_byte *end_buf = buf + (sizeof (value) + 1) * 8 / 7;
1654
1655         *bytes_read_ptr += read_sleb128 (buf, end_buf, &value) - buf;
1656         return base + value;
1657       }
1658     case DW_EH_PE_sdata2:
1659       *bytes_read_ptr += 2;
1660       return (base + bfd_get_signed_16 (unit->abfd, (bfd_byte *) buf));
1661     case DW_EH_PE_sdata4:
1662       *bytes_read_ptr += 4;
1663       return (base + bfd_get_signed_32 (unit->abfd, (bfd_byte *) buf));
1664     case DW_EH_PE_sdata8:
1665       *bytes_read_ptr += 8;
1666       return (base + bfd_get_signed_64 (unit->abfd, (bfd_byte *) buf));
1667     default:
1668       internal_error (__FILE__, __LINE__,
1669                       _("Invalid or unsupported encoding"));
1670     }
1671 }
1672 \f
1673
1674 static int
1675 bsearch_cie_cmp (const void *key, const void *element)
1676 {
1677   ULONGEST cie_pointer = *(ULONGEST *) key;
1678   struct dwarf2_cie *cie = *(struct dwarf2_cie **) element;
1679
1680   if (cie_pointer == cie->cie_pointer)
1681     return 0;
1682
1683   return (cie_pointer < cie->cie_pointer) ? -1 : 1;
1684 }
1685
1686 /* Find CIE with the given CIE_POINTER in CIE_TABLE.  */
1687 static struct dwarf2_cie *
1688 find_cie (struct dwarf2_cie_table *cie_table, ULONGEST cie_pointer)
1689 {
1690   struct dwarf2_cie **p_cie;
1691
1692   /* The C standard (ISO/IEC 9899:TC2) requires the BASE argument to
1693      bsearch be non-NULL.  */
1694   if (cie_table->entries == NULL)
1695     {
1696       gdb_assert (cie_table->num_entries == 0);
1697       return NULL;
1698     }
1699
1700   p_cie = bsearch (&cie_pointer, cie_table->entries, cie_table->num_entries,
1701                    sizeof (cie_table->entries[0]), bsearch_cie_cmp);
1702   if (p_cie != NULL)
1703     return *p_cie;
1704   return NULL;
1705 }
1706
1707 /* Add a pointer to new CIE to the CIE_TABLE, allocating space for it.  */
1708 static void
1709 add_cie (struct dwarf2_cie_table *cie_table, struct dwarf2_cie *cie)
1710 {
1711   const int n = cie_table->num_entries;
1712
1713   gdb_assert (n < 1
1714               || cie_table->entries[n - 1]->cie_pointer < cie->cie_pointer);
1715
1716   cie_table->entries =
1717       xrealloc (cie_table->entries, (n + 1) * sizeof (cie_table->entries[0]));
1718   cie_table->entries[n] = cie;
1719   cie_table->num_entries = n + 1;
1720 }
1721
1722 static int
1723 bsearch_fde_cmp (const void *key, const void *element)
1724 {
1725   CORE_ADDR seek_pc = *(CORE_ADDR *) key;
1726   struct dwarf2_fde *fde = *(struct dwarf2_fde **) element;
1727
1728   if (seek_pc < fde->initial_location)
1729     return -1;
1730   if (seek_pc < fde->initial_location + fde->address_range)
1731     return 0;
1732   return 1;
1733 }
1734
1735 /* Find the FDE for *PC.  Return a pointer to the FDE, and store the
1736    inital location associated with it into *PC.  */
1737
1738 static struct dwarf2_fde *
1739 dwarf2_frame_find_fde (CORE_ADDR *pc, CORE_ADDR *out_offset)
1740 {
1741   struct objfile *objfile;
1742
1743   ALL_OBJFILES (objfile)
1744     {
1745       struct dwarf2_fde_table *fde_table;
1746       struct dwarf2_fde **p_fde;
1747       CORE_ADDR offset;
1748       CORE_ADDR seek_pc;
1749
1750       fde_table = objfile_data (objfile, dwarf2_frame_objfile_data);
1751       if (fde_table == NULL)
1752         {
1753           dwarf2_build_frame_info (objfile);
1754           fde_table = objfile_data (objfile, dwarf2_frame_objfile_data);
1755         }
1756       gdb_assert (fde_table != NULL);
1757
1758       if (fde_table->num_entries == 0)
1759         continue;
1760
1761       gdb_assert (objfile->section_offsets);
1762       offset = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1763
1764       gdb_assert (fde_table->num_entries > 0);
1765       if (*pc < offset + fde_table->entries[0]->initial_location)
1766         continue;
1767
1768       seek_pc = *pc - offset;
1769       p_fde = bsearch (&seek_pc, fde_table->entries, fde_table->num_entries,
1770                        sizeof (fde_table->entries[0]), bsearch_fde_cmp);
1771       if (p_fde != NULL)
1772         {
1773           *pc = (*p_fde)->initial_location + offset;
1774           if (out_offset)
1775             *out_offset = offset;
1776           return *p_fde;
1777         }
1778     }
1779   return NULL;
1780 }
1781
1782 /* Add a pointer to new FDE to the FDE_TABLE, allocating space for it.  */
1783 static void
1784 add_fde (struct dwarf2_fde_table *fde_table, struct dwarf2_fde *fde)
1785 {
1786   if (fde->address_range == 0)
1787     /* Discard useless FDEs.  */
1788     return;
1789
1790   fde_table->num_entries += 1;
1791   fde_table->entries =
1792       xrealloc (fde_table->entries,
1793                 fde_table->num_entries * sizeof (fde_table->entries[0]));
1794   fde_table->entries[fde_table->num_entries - 1] = fde;
1795 }
1796
1797 #ifdef CC_HAS_LONG_LONG
1798 #define DW64_CIE_ID 0xffffffffffffffffULL
1799 #else
1800 #define DW64_CIE_ID ~0
1801 #endif
1802
1803 static gdb_byte *decode_frame_entry (struct comp_unit *unit, gdb_byte *start,
1804                                      int eh_frame_p,
1805                                      struct dwarf2_cie_table *cie_table,
1806                                      struct dwarf2_fde_table *fde_table);
1807
1808 /* Decode the next CIE or FDE.  Return NULL if invalid input, otherwise
1809    the next byte to be processed.  */
1810 static gdb_byte *
1811 decode_frame_entry_1 (struct comp_unit *unit, gdb_byte *start, int eh_frame_p,
1812                       struct dwarf2_cie_table *cie_table,
1813                       struct dwarf2_fde_table *fde_table)
1814 {
1815   struct gdbarch *gdbarch = get_objfile_arch (unit->objfile);
1816   gdb_byte *buf, *end;
1817   LONGEST length;
1818   unsigned int bytes_read;
1819   int dwarf64_p;
1820   ULONGEST cie_id;
1821   ULONGEST cie_pointer;
1822
1823   buf = start;
1824   length = read_initial_length (unit->abfd, buf, &bytes_read);
1825   buf += bytes_read;
1826   end = buf + length;
1827
1828   /* Are we still within the section?  */
1829   if (end > unit->dwarf_frame_buffer + unit->dwarf_frame_size)
1830     return NULL;
1831
1832   if (length == 0)
1833     return end;
1834
1835   /* Distinguish between 32 and 64-bit encoded frame info.  */
1836   dwarf64_p = (bytes_read == 12);
1837
1838   /* In a .eh_frame section, zero is used to distinguish CIEs from FDEs.  */
1839   if (eh_frame_p)
1840     cie_id = 0;
1841   else if (dwarf64_p)
1842     cie_id = DW64_CIE_ID;
1843   else
1844     cie_id = DW_CIE_ID;
1845
1846   if (dwarf64_p)
1847     {
1848       cie_pointer = read_8_bytes (unit->abfd, buf);
1849       buf += 8;
1850     }
1851   else
1852     {
1853       cie_pointer = read_4_bytes (unit->abfd, buf);
1854       buf += 4;
1855     }
1856
1857   if (cie_pointer == cie_id)
1858     {
1859       /* This is a CIE.  */
1860       struct dwarf2_cie *cie;
1861       char *augmentation;
1862       unsigned int cie_version;
1863
1864       /* Record the offset into the .debug_frame section of this CIE.  */
1865       cie_pointer = start - unit->dwarf_frame_buffer;
1866
1867       /* Check whether we've already read it.  */
1868       if (find_cie (cie_table, cie_pointer))
1869         return end;
1870
1871       cie = (struct dwarf2_cie *)
1872         obstack_alloc (&unit->objfile->objfile_obstack,
1873                        sizeof (struct dwarf2_cie));
1874       cie->initial_instructions = NULL;
1875       cie->cie_pointer = cie_pointer;
1876
1877       /* The encoding for FDE's in a normal .debug_frame section
1878          depends on the target address size.  */
1879       cie->encoding = DW_EH_PE_absptr;
1880
1881       /* We'll determine the final value later, but we need to
1882          initialize it conservatively.  */
1883       cie->signal_frame = 0;
1884
1885       /* Check version number.  */
1886       cie_version = read_1_byte (unit->abfd, buf);
1887       if (cie_version != 1 && cie_version != 3 && cie_version != 4)
1888         return NULL;
1889       cie->version = cie_version;
1890       buf += 1;
1891
1892       /* Interpret the interesting bits of the augmentation.  */
1893       cie->augmentation = augmentation = (char *) buf;
1894       buf += (strlen (augmentation) + 1);
1895
1896       /* Ignore armcc augmentations.  We only use them for quirks,
1897          and that doesn't happen until later.  */
1898       if (strncmp (augmentation, "armcc", 5) == 0)
1899         augmentation += strlen (augmentation);
1900
1901       /* The GCC 2.x "eh" augmentation has a pointer immediately
1902          following the augmentation string, so it must be handled
1903          first.  */
1904       if (augmentation[0] == 'e' && augmentation[1] == 'h')
1905         {
1906           /* Skip.  */
1907           buf += gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT;
1908           augmentation += 2;
1909         }
1910
1911       if (cie->version >= 4)
1912         {
1913           /* FIXME: check that this is the same as from the CU header.  */
1914           cie->addr_size = read_1_byte (unit->abfd, buf);
1915           ++buf;
1916           cie->segment_size = read_1_byte (unit->abfd, buf);
1917           ++buf;
1918         }
1919       else
1920         {
1921           cie->addr_size = gdbarch_dwarf2_addr_size (gdbarch);
1922           cie->segment_size = 0;
1923         }
1924       /* Address values in .eh_frame sections are defined to have the
1925          target's pointer size.  Watchout: This breaks frame info for
1926          targets with pointer size < address size, unless a .debug_frame
1927          section exists as well.  */
1928       if (eh_frame_p)
1929         cie->ptr_size = gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT;
1930       else
1931         cie->ptr_size = cie->addr_size;
1932
1933       cie->code_alignment_factor =
1934         read_unsigned_leb128 (unit->abfd, buf, &bytes_read);
1935       buf += bytes_read;
1936
1937       cie->data_alignment_factor =
1938         read_signed_leb128 (unit->abfd, buf, &bytes_read);
1939       buf += bytes_read;
1940
1941       if (cie_version == 1)
1942         {
1943           cie->return_address_register = read_1_byte (unit->abfd, buf);
1944           bytes_read = 1;
1945         }
1946       else
1947         cie->return_address_register = read_unsigned_leb128 (unit->abfd, buf,
1948                                                              &bytes_read);
1949       cie->return_address_register
1950         = dwarf2_frame_adjust_regnum (gdbarch,
1951                                       cie->return_address_register,
1952                                       eh_frame_p);
1953
1954       buf += bytes_read;
1955
1956       cie->saw_z_augmentation = (*augmentation == 'z');
1957       if (cie->saw_z_augmentation)
1958         {
1959           ULONGEST length;
1960
1961           length = read_unsigned_leb128 (unit->abfd, buf, &bytes_read);
1962           buf += bytes_read;
1963           if (buf > end)
1964             return NULL;
1965           cie->initial_instructions = buf + length;
1966           augmentation++;
1967         }
1968
1969       while (*augmentation)
1970         {
1971           /* "L" indicates a byte showing how the LSDA pointer is encoded.  */
1972           if (*augmentation == 'L')
1973             {
1974               /* Skip.  */
1975               buf++;
1976               augmentation++;
1977             }
1978
1979           /* "R" indicates a byte indicating how FDE addresses are encoded.  */
1980           else if (*augmentation == 'R')
1981             {
1982               cie->encoding = *buf++;
1983               augmentation++;
1984             }
1985
1986           /* "P" indicates a personality routine in the CIE augmentation.  */
1987           else if (*augmentation == 'P')
1988             {
1989               /* Skip.  Avoid indirection since we throw away the result.  */
1990               gdb_byte encoding = (*buf++) & ~DW_EH_PE_indirect;
1991               read_encoded_value (unit, encoding, cie->ptr_size,
1992                                   buf, &bytes_read, 0);
1993               buf += bytes_read;
1994               augmentation++;
1995             }
1996
1997           /* "S" indicates a signal frame, such that the return
1998              address must not be decremented to locate the call frame
1999              info for the previous frame; it might even be the first
2000              instruction of a function, so decrementing it would take
2001              us to a different function.  */
2002           else if (*augmentation == 'S')
2003             {
2004               cie->signal_frame = 1;
2005               augmentation++;
2006             }
2007
2008           /* Otherwise we have an unknown augmentation.  Assume that either
2009              there is no augmentation data, or we saw a 'z' prefix.  */
2010           else
2011             {
2012               if (cie->initial_instructions)
2013                 buf = cie->initial_instructions;
2014               break;
2015             }
2016         }
2017
2018       cie->initial_instructions = buf;
2019       cie->end = end;
2020       cie->unit = unit;
2021
2022       add_cie (cie_table, cie);
2023     }
2024   else
2025     {
2026       /* This is a FDE.  */
2027       struct dwarf2_fde *fde;
2028
2029       /* In an .eh_frame section, the CIE pointer is the delta between the
2030          address within the FDE where the CIE pointer is stored and the
2031          address of the CIE.  Convert it to an offset into the .eh_frame
2032          section.  */
2033       if (eh_frame_p)
2034         {
2035           cie_pointer = buf - unit->dwarf_frame_buffer - cie_pointer;
2036           cie_pointer -= (dwarf64_p ? 8 : 4);
2037         }
2038
2039       /* In either case, validate the result is still within the section.  */
2040       if (cie_pointer >= unit->dwarf_frame_size)
2041         return NULL;
2042
2043       fde = (struct dwarf2_fde *)
2044         obstack_alloc (&unit->objfile->objfile_obstack,
2045                        sizeof (struct dwarf2_fde));
2046       fde->cie = find_cie (cie_table, cie_pointer);
2047       if (fde->cie == NULL)
2048         {
2049           decode_frame_entry (unit, unit->dwarf_frame_buffer + cie_pointer,
2050                               eh_frame_p, cie_table, fde_table);
2051           fde->cie = find_cie (cie_table, cie_pointer);
2052         }
2053
2054       gdb_assert (fde->cie != NULL);
2055
2056       fde->initial_location =
2057         read_encoded_value (unit, fde->cie->encoding, fde->cie->ptr_size,
2058                             buf, &bytes_read, 0);
2059       buf += bytes_read;
2060
2061       fde->address_range =
2062         read_encoded_value (unit, fde->cie->encoding & 0x0f,
2063                             fde->cie->ptr_size, buf, &bytes_read, 0);
2064       buf += bytes_read;
2065
2066       /* A 'z' augmentation in the CIE implies the presence of an
2067          augmentation field in the FDE as well.  The only thing known
2068          to be in here at present is the LSDA entry for EH.  So we
2069          can skip the whole thing.  */
2070       if (fde->cie->saw_z_augmentation)
2071         {
2072           ULONGEST length;
2073
2074           length = read_unsigned_leb128 (unit->abfd, buf, &bytes_read);
2075           buf += bytes_read + length;
2076           if (buf > end)
2077             return NULL;
2078         }
2079
2080       fde->instructions = buf;
2081       fde->end = end;
2082
2083       fde->eh_frame_p = eh_frame_p;
2084
2085       add_fde (fde_table, fde);
2086     }
2087
2088   return end;
2089 }
2090
2091 /* Read a CIE or FDE in BUF and decode it.  */
2092 static gdb_byte *
2093 decode_frame_entry (struct comp_unit *unit, gdb_byte *start, int eh_frame_p,
2094                     struct dwarf2_cie_table *cie_table,
2095                     struct dwarf2_fde_table *fde_table)
2096 {
2097   enum { NONE, ALIGN4, ALIGN8, FAIL } workaround = NONE;
2098   gdb_byte *ret;
2099   ptrdiff_t start_offset;
2100
2101   while (1)
2102     {
2103       ret = decode_frame_entry_1 (unit, start, eh_frame_p,
2104                                   cie_table, fde_table);
2105       if (ret != NULL)
2106         break;
2107
2108       /* We have corrupt input data of some form.  */
2109
2110       /* ??? Try, weakly, to work around compiler/assembler/linker bugs
2111          and mismatches wrt padding and alignment of debug sections.  */
2112       /* Note that there is no requirement in the standard for any
2113          alignment at all in the frame unwind sections.  Testing for
2114          alignment before trying to interpret data would be incorrect.
2115
2116          However, GCC traditionally arranged for frame sections to be
2117          sized such that the FDE length and CIE fields happen to be
2118          aligned (in theory, for performance).  This, unfortunately,
2119          was done with .align directives, which had the side effect of
2120          forcing the section to be aligned by the linker.
2121
2122          This becomes a problem when you have some other producer that
2123          creates frame sections that are not as strictly aligned.  That
2124          produces a hole in the frame info that gets filled by the 
2125          linker with zeros.
2126
2127          The GCC behaviour is arguably a bug, but it's effectively now
2128          part of the ABI, so we're now stuck with it, at least at the
2129          object file level.  A smart linker may decide, in the process
2130          of compressing duplicate CIE information, that it can rewrite
2131          the entire output section without this extra padding.  */
2132
2133       start_offset = start - unit->dwarf_frame_buffer;
2134       if (workaround < ALIGN4 && (start_offset & 3) != 0)
2135         {
2136           start += 4 - (start_offset & 3);
2137           workaround = ALIGN4;
2138           continue;
2139         }
2140       if (workaround < ALIGN8 && (start_offset & 7) != 0)
2141         {
2142           start += 8 - (start_offset & 7);
2143           workaround = ALIGN8;
2144           continue;
2145         }
2146
2147       /* Nothing left to try.  Arrange to return as if we've consumed
2148          the entire input section.  Hopefully we'll get valid info from
2149          the other of .debug_frame/.eh_frame.  */
2150       workaround = FAIL;
2151       ret = unit->dwarf_frame_buffer + unit->dwarf_frame_size;
2152       break;
2153     }
2154
2155   switch (workaround)
2156     {
2157     case NONE:
2158       break;
2159
2160     case ALIGN4:
2161       complaint (&symfile_complaints, _("\
2162 Corrupt data in %s:%s; align 4 workaround apparently succeeded"),
2163                  unit->dwarf_frame_section->owner->filename,
2164                  unit->dwarf_frame_section->name);
2165       break;
2166
2167     case ALIGN8:
2168       complaint (&symfile_complaints, _("\
2169 Corrupt data in %s:%s; align 8 workaround apparently succeeded"),
2170                  unit->dwarf_frame_section->owner->filename,
2171                  unit->dwarf_frame_section->name);
2172       break;
2173
2174     default:
2175       complaint (&symfile_complaints,
2176                  _("Corrupt data in %s:%s"),
2177                  unit->dwarf_frame_section->owner->filename,
2178                  unit->dwarf_frame_section->name);
2179       break;
2180     }
2181
2182   return ret;
2183 }
2184 \f
2185
2186 /* Imported from dwarf2read.c.  */
2187 extern void dwarf2_get_section_info (struct objfile *, const char *,
2188                                      asection **, gdb_byte **,
2189                                      bfd_size_type *);
2190
2191 static int
2192 qsort_fde_cmp (const void *a, const void *b)
2193 {
2194   struct dwarf2_fde *aa = *(struct dwarf2_fde **)a;
2195   struct dwarf2_fde *bb = *(struct dwarf2_fde **)b;
2196
2197   if (aa->initial_location == bb->initial_location)
2198     {
2199       if (aa->address_range != bb->address_range
2200           && aa->eh_frame_p == 0 && bb->eh_frame_p == 0)
2201         /* Linker bug, e.g. gold/10400.
2202            Work around it by keeping stable sort order.  */
2203         return (a < b) ? -1 : 1;
2204       else
2205         /* Put eh_frame entries after debug_frame ones.  */
2206         return aa->eh_frame_p - bb->eh_frame_p;
2207     }
2208
2209   return (aa->initial_location < bb->initial_location) ? -1 : 1;
2210 }
2211
2212 void
2213 dwarf2_build_frame_info (struct objfile *objfile)
2214 {
2215   struct comp_unit *unit;
2216   gdb_byte *frame_ptr;
2217   struct dwarf2_cie_table cie_table;
2218   struct dwarf2_fde_table fde_table;
2219   struct dwarf2_fde_table *fde_table2;
2220
2221   cie_table.num_entries = 0;
2222   cie_table.entries = NULL;
2223
2224   fde_table.num_entries = 0;
2225   fde_table.entries = NULL;
2226
2227   /* Build a minimal decoding of the DWARF2 compilation unit.  */
2228   unit = (struct comp_unit *) obstack_alloc (&objfile->objfile_obstack,
2229                                              sizeof (struct comp_unit));
2230   unit->abfd = objfile->obfd;
2231   unit->objfile = objfile;
2232   unit->dbase = 0;
2233   unit->tbase = 0;
2234
2235   dwarf2_get_section_info (objfile, ".eh_frame",
2236                            &unit->dwarf_frame_section,
2237                            &unit->dwarf_frame_buffer,
2238                            &unit->dwarf_frame_size);
2239   if (unit->dwarf_frame_size)
2240     {
2241       asection *got, *txt;
2242
2243       /* FIXME: kettenis/20030602: This is the DW_EH_PE_datarel base
2244          that is used for the i386/amd64 target, which currently is
2245          the only target in GCC that supports/uses the
2246          DW_EH_PE_datarel encoding.  */
2247       got = bfd_get_section_by_name (unit->abfd, ".got");
2248       if (got)
2249         unit->dbase = got->vma;
2250
2251       /* GCC emits the DW_EH_PE_textrel encoding type on sh and ia64
2252          so far.  */
2253       txt = bfd_get_section_by_name (unit->abfd, ".text");
2254       if (txt)
2255         unit->tbase = txt->vma;
2256
2257       frame_ptr = unit->dwarf_frame_buffer;
2258       while (frame_ptr < unit->dwarf_frame_buffer + unit->dwarf_frame_size)
2259         frame_ptr = decode_frame_entry (unit, frame_ptr, 1,
2260                                         &cie_table, &fde_table);
2261
2262       if (cie_table.num_entries != 0)
2263         {
2264           /* Reinit cie_table: debug_frame has different CIEs.  */
2265           xfree (cie_table.entries);
2266           cie_table.num_entries = 0;
2267           cie_table.entries = NULL;
2268         }
2269     }
2270
2271   dwarf2_get_section_info (objfile, ".debug_frame",
2272                            &unit->dwarf_frame_section,
2273                            &unit->dwarf_frame_buffer,
2274                            &unit->dwarf_frame_size);
2275   if (unit->dwarf_frame_size)
2276     {
2277       frame_ptr = unit->dwarf_frame_buffer;
2278       while (frame_ptr < unit->dwarf_frame_buffer + unit->dwarf_frame_size)
2279         frame_ptr = decode_frame_entry (unit, frame_ptr, 0,
2280                                         &cie_table, &fde_table);
2281     }
2282
2283   /* Discard the cie_table, it is no longer needed.  */
2284   if (cie_table.num_entries != 0)
2285     {
2286       xfree (cie_table.entries);
2287       cie_table.entries = NULL;   /* Paranoia.  */
2288       cie_table.num_entries = 0;  /* Paranoia.  */
2289     }
2290
2291   /* Copy fde_table to obstack: it is needed at runtime.  */
2292   fde_table2 = (struct dwarf2_fde_table *)
2293     obstack_alloc (&objfile->objfile_obstack, sizeof (*fde_table2));
2294
2295   if (fde_table.num_entries == 0)
2296     {
2297       fde_table2->entries = NULL;
2298       fde_table2->num_entries = 0;
2299     }
2300   else
2301     {
2302       struct dwarf2_fde *fde_prev = NULL;
2303       struct dwarf2_fde *first_non_zero_fde = NULL;
2304       int i;
2305
2306       /* Prepare FDE table for lookups.  */
2307       qsort (fde_table.entries, fde_table.num_entries,
2308              sizeof (fde_table.entries[0]), qsort_fde_cmp);
2309
2310       /* Check for leftovers from --gc-sections.  The GNU linker sets
2311          the relevant symbols to zero, but doesn't zero the FDE *end*
2312          ranges because there's no relocation there.  It's (offset,
2313          length), not (start, end).  On targets where address zero is
2314          just another valid address this can be a problem, since the
2315          FDEs appear to be non-empty in the output --- we could pick
2316          out the wrong FDE.  To work around this, when overlaps are
2317          detected, we prefer FDEs that do not start at zero.
2318
2319          Start by finding the first FDE with non-zero start.  Below
2320          we'll discard all FDEs that start at zero and overlap this
2321          one.  */
2322       for (i = 0; i < fde_table.num_entries; i++)
2323         {
2324           struct dwarf2_fde *fde = fde_table.entries[i];
2325
2326           if (fde->initial_location != 0)
2327             {
2328               first_non_zero_fde = fde;
2329               break;
2330             }
2331         }
2332
2333       /* Since we'll be doing bsearch, squeeze out identical (except
2334          for eh_frame_p) fde entries so bsearch result is predictable.
2335          Also discard leftovers from --gc-sections.  */
2336       fde_table2->num_entries = 0;
2337       for (i = 0; i < fde_table.num_entries; i++)
2338         {
2339           struct dwarf2_fde *fde = fde_table.entries[i];
2340
2341           if (fde->initial_location == 0
2342               && first_non_zero_fde != NULL
2343               && (first_non_zero_fde->initial_location
2344                   < fde->initial_location + fde->address_range))
2345             continue;
2346
2347           if (fde_prev != NULL
2348               && fde_prev->initial_location == fde->initial_location)
2349             continue;
2350
2351           obstack_grow (&objfile->objfile_obstack, &fde_table.entries[i],
2352                         sizeof (fde_table.entries[0]));
2353           ++fde_table2->num_entries;
2354           fde_prev = fde;
2355         }
2356       fde_table2->entries = obstack_finish (&objfile->objfile_obstack);
2357
2358       /* Discard the original fde_table.  */
2359       xfree (fde_table.entries);
2360     }
2361
2362   set_objfile_data (objfile, dwarf2_frame_objfile_data, fde_table2);
2363 }
2364
2365 /* Provide a prototype to silence -Wmissing-prototypes.  */
2366 void _initialize_dwarf2_frame (void);
2367
2368 void
2369 _initialize_dwarf2_frame (void)
2370 {
2371   dwarf2_frame_data = gdbarch_data_register_pre_init (dwarf2_frame_init);
2372   dwarf2_frame_objfile_data = register_objfile_data ();
2373 }