OSDN Git Service

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