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