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