* dwarf2-frame.c (dwarf2_frame_eh_frame_regnum): Rename to...
[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 2 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, write to the Free Software
21    Foundation, Inc., 51 Franklin Street, Fifth Floor,
22    Boston, MA 02110-1301, USA.  */
23
24 #include "defs.h"
25 #include "dwarf2expr.h"
26 #include "elf/dwarf2.h"
27 #include "frame.h"
28 #include "frame-base.h"
29 #include "frame-unwind.h"
30 #include "gdbcore.h"
31 #include "gdbtypes.h"
32 #include "symtab.h"
33 #include "objfiles.h"
34 #include "regcache.h"
35 #include "value.h"
36
37 #include "gdb_assert.h"
38 #include "gdb_string.h"
39
40 #include "complaints.h"
41 #include "dwarf2-frame.h"
42
43 /* Call Frame Information (CFI).  */
44
45 /* Common Information Entry (CIE).  */
46
47 struct dwarf2_cie
48 {
49   /* Offset into the .debug_frame section where this CIE was found.
50      Used to identify this CIE.  */
51   ULONGEST cie_pointer;
52
53   /* Constant that is factored out of all advance location
54      instructions.  */
55   ULONGEST code_alignment_factor;
56
57   /* Constants that is factored out of all offset instructions.  */
58   LONGEST data_alignment_factor;
59
60   /* Return address column.  */
61   ULONGEST return_address_register;
62
63   /* Instruction sequence to initialize a register set.  */
64   gdb_byte *initial_instructions;
65   gdb_byte *end;
66
67   /* Saved augmentation, in case it's needed later.  */
68   char *augmentation;
69
70   /* Encoding of addresses.  */
71   gdb_byte encoding;
72
73   /* True if a 'z' augmentation existed.  */
74   unsigned char saw_z_augmentation;
75
76   /* True if an 'S' augmentation existed.  */
77   unsigned char signal_frame;
78
79   /* The version recorded in the CIE.  */
80   unsigned char version;
81
82   struct dwarf2_cie *next;
83 };
84
85 /* Frame Description Entry (FDE).  */
86
87 struct dwarf2_fde
88 {
89   /* CIE for this FDE.  */
90   struct dwarf2_cie *cie;
91
92   /* First location associated with this FDE.  */
93   CORE_ADDR initial_location;
94
95   /* Number of bytes of program instructions described by this FDE.  */
96   CORE_ADDR address_range;
97
98   /* Instruction sequence.  */
99   gdb_byte *instructions;
100   gdb_byte *end;
101
102   /* True if this FDE is read from a .eh_frame instead of a .debug_frame
103      section.  */
104   unsigned char eh_frame_p;
105
106   struct dwarf2_fde *next;
107 };
108
109 static struct dwarf2_fde *dwarf2_frame_find_fde (CORE_ADDR *pc);
110
111 static int dwarf2_frame_adjust_regnum (struct gdbarch *gdbarch, int regnum,
112                                        int eh_frame_p);
113 \f
114
115 /* Structure describing a frame state.  */
116
117 struct dwarf2_frame_state
118 {
119   /* Each register save state can be described in terms of a CFA slot,
120      another register, or a location expression.  */
121   struct dwarf2_frame_state_reg_info
122   {
123     struct dwarf2_frame_state_reg *reg;
124     int num_regs;
125
126     /* Used to implement DW_CFA_remember_state.  */
127     struct dwarf2_frame_state_reg_info *prev;
128   } regs;
129
130   LONGEST cfa_offset;
131   ULONGEST cfa_reg;
132   gdb_byte *cfa_exp;
133   enum {
134     CFA_UNSET,
135     CFA_REG_OFFSET,
136     CFA_EXP
137   } cfa_how;
138
139   /* The PC described by the current frame state.  */
140   CORE_ADDR pc;
141
142   /* Initial register set from the CIE.
143      Used to implement DW_CFA_restore.  */
144   struct dwarf2_frame_state_reg_info initial;
145
146   /* The information we care about from the CIE.  */
147   LONGEST data_align;
148   ULONGEST code_align;
149   ULONGEST retaddr_column;
150
151   /* Flags for known producer quirks.  */
152
153   /* The ARM compilers, in DWARF2 mode, assume that DW_CFA_def_cfa
154      and DW_CFA_def_cfa_offset takes a factored offset.  */
155   int armcc_cfa_offsets_sf;
156
157   /* The ARM compilers, in DWARF2 or DWARF3 mode, may assume that
158      the CFA is defined as REG - OFFSET rather than REG + OFFSET.  */
159   int armcc_cfa_offsets_reversed;
160 };
161
162 /* Store the length the expression for the CFA in the `cfa_reg' field,
163    which is unused in that case.  */
164 #define cfa_exp_len cfa_reg
165
166 /* Assert that the register set RS is large enough to store NUM_REGS
167    columns.  If necessary, enlarge the register set.  */
168
169 static void
170 dwarf2_frame_state_alloc_regs (struct dwarf2_frame_state_reg_info *rs,
171                                int num_regs)
172 {
173   size_t size = sizeof (struct dwarf2_frame_state_reg);
174
175   if (num_regs <= rs->num_regs)
176     return;
177
178   rs->reg = (struct dwarf2_frame_state_reg *)
179     xrealloc (rs->reg, num_regs * size);
180
181   /* Initialize newly allocated registers.  */
182   memset (rs->reg + rs->num_regs, 0, (num_regs - rs->num_regs) * size);
183   rs->num_regs = num_regs;
184 }
185
186 /* Copy the register columns in register set RS into newly allocated
187    memory and return a pointer to this newly created copy.  */
188
189 static struct dwarf2_frame_state_reg *
190 dwarf2_frame_state_copy_regs (struct dwarf2_frame_state_reg_info *rs)
191 {
192   size_t size = rs->num_regs * sizeof (struct dwarf2_frame_state_reg);
193   struct dwarf2_frame_state_reg *reg;
194
195   reg = (struct dwarf2_frame_state_reg *) xmalloc (size);
196   memcpy (reg, rs->reg, size);
197
198   return reg;
199 }
200
201 /* Release the memory allocated to register set RS.  */
202
203 static void
204 dwarf2_frame_state_free_regs (struct dwarf2_frame_state_reg_info *rs)
205 {
206   if (rs)
207     {
208       dwarf2_frame_state_free_regs (rs->prev);
209
210       xfree (rs->reg);
211       xfree (rs);
212     }
213 }
214
215 /* Release the memory allocated to the frame state FS.  */
216
217 static void
218 dwarf2_frame_state_free (void *p)
219 {
220   struct dwarf2_frame_state *fs = p;
221
222   dwarf2_frame_state_free_regs (fs->initial.prev);
223   dwarf2_frame_state_free_regs (fs->regs.prev);
224   xfree (fs->initial.reg);
225   xfree (fs->regs.reg);
226   xfree (fs);
227 }
228 \f
229
230 /* Helper functions for execute_stack_op.  */
231
232 static CORE_ADDR
233 read_reg (void *baton, int reg)
234 {
235   struct frame_info *next_frame = (struct frame_info *) baton;
236   struct gdbarch *gdbarch = get_frame_arch (next_frame);
237   int regnum;
238   gdb_byte *buf;
239
240   regnum = DWARF2_REG_TO_REGNUM (reg);
241
242   buf = alloca (register_size (gdbarch, regnum));
243   frame_unwind_register (next_frame, regnum, buf);
244
245   /* Convert the register to an integer.  This returns a LONGEST
246      rather than a CORE_ADDR, but unpack_pointer does the same thing
247      under the covers, and this makes more sense for non-pointer
248      registers.  Maybe read_reg and the associated interfaces should
249      deal with "struct value" instead of CORE_ADDR.  */
250   return unpack_long (register_type (gdbarch, regnum), buf);
251 }
252
253 static void
254 read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
255 {
256   read_memory (addr, buf, len);
257 }
258
259 static void
260 no_get_frame_base (void *baton, gdb_byte **start, size_t *length)
261 {
262   internal_error (__FILE__, __LINE__,
263                   _("Support for DW_OP_fbreg is unimplemented"));
264 }
265
266 static CORE_ADDR
267 no_get_tls_address (void *baton, CORE_ADDR offset)
268 {
269   internal_error (__FILE__, __LINE__,
270                   _("Support for DW_OP_GNU_push_tls_address is unimplemented"));
271 }
272
273 static CORE_ADDR
274 execute_stack_op (gdb_byte *exp, ULONGEST len,
275                   struct frame_info *next_frame, CORE_ADDR initial)
276 {
277   struct dwarf_expr_context *ctx;
278   CORE_ADDR result;
279
280   ctx = new_dwarf_expr_context ();
281   ctx->baton = next_frame;
282   ctx->read_reg = read_reg;
283   ctx->read_mem = read_mem;
284   ctx->get_frame_base = no_get_frame_base;
285   ctx->get_tls_address = no_get_tls_address;
286
287   dwarf_expr_push (ctx, initial);
288   dwarf_expr_eval (ctx, exp, len);
289   result = dwarf_expr_fetch (ctx, 0);
290
291   if (ctx->in_reg)
292     result = read_reg (next_frame, result);
293
294   free_dwarf_expr_context (ctx);
295
296   return result;
297 }
298 \f
299
300 static void
301 execute_cfa_program (gdb_byte *insn_ptr, gdb_byte *insn_end,
302                      struct frame_info *next_frame,
303                      struct dwarf2_frame_state *fs, int eh_frame_p)
304 {
305   CORE_ADDR pc = frame_pc_unwind (next_frame);
306   int bytes_read;
307   struct gdbarch *gdbarch = get_frame_arch (next_frame);
308
309   while (insn_ptr < insn_end && fs->pc <= pc)
310     {
311       gdb_byte insn = *insn_ptr++;
312       ULONGEST utmp, reg;
313       LONGEST offset;
314
315       if ((insn & 0xc0) == DW_CFA_advance_loc)
316         fs->pc += (insn & 0x3f) * fs->code_align;
317       else if ((insn & 0xc0) == DW_CFA_offset)
318         {
319           reg = insn & 0x3f;
320           reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
321           insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp);
322           offset = utmp * fs->data_align;
323           dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
324           fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
325           fs->regs.reg[reg].loc.offset = offset;
326         }
327       else if ((insn & 0xc0) == DW_CFA_restore)
328         {
329           gdb_assert (fs->initial.reg);
330           reg = insn & 0x3f;
331           reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
332           dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
333           if (reg < fs->initial.num_regs)
334             fs->regs.reg[reg] = fs->initial.reg[reg];
335           else 
336             fs->regs.reg[reg].how = DWARF2_FRAME_REG_UNSPECIFIED;
337
338           if (fs->regs.reg[reg].how == DWARF2_FRAME_REG_UNSPECIFIED)
339             complaint (&symfile_complaints, _("\
340 incomplete CFI data; DW_CFA_restore unspecified\n\
341 register %s (#%d) at 0x%s"),
342                        REGISTER_NAME(DWARF2_REG_TO_REGNUM(reg)),
343                        DWARF2_REG_TO_REGNUM(reg), 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 == PC_REGNUM)
650     reg->how = DWARF2_FRAME_REG_RA;
651   else if (regnum == SP_REGNUM)
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 = NUM_REGS + NUM_PSEUDO_REGS;
812   struct dwarf2_frame_cache *cache;
813   struct dwarf2_frame_state *fs;
814   struct dwarf2_fde *fde;
815
816   if (*this_cache)
817     return *this_cache;
818
819   /* Allocate a new cache.  */
820   cache = FRAME_OBSTACK_ZALLOC (struct dwarf2_frame_cache);
821   cache->reg = FRAME_OBSTACK_CALLOC (num_regs, struct dwarf2_frame_state_reg);
822
823   /* Allocate and initialize the frame state.  */
824   fs = XMALLOC (struct dwarf2_frame_state);
825   memset (fs, 0, sizeof (struct dwarf2_frame_state));
826   old_chain = make_cleanup (dwarf2_frame_state_free, fs);
827
828   /* Unwind the PC.
829
830      Note that if NEXT_FRAME is never supposed to return (i.e. a call
831      to abort), the compiler might optimize away the instruction at
832      NEXT_FRAME's return address.  As a result the return address will
833      point at some random instruction, and the CFI for that
834      instruction is probably worthless to us.  GCC's unwinder solves
835      this problem by substracting 1 from the return address to get an
836      address in the middle of a presumed call instruction (or the
837      instruction in the associated delay slot).  This should only be
838      done for "normal" frames and not for resume-type frames (signal
839      handlers, sentinel frames, dummy frames).  The function
840      frame_unwind_address_in_block does just this.  It's not clear how
841      reliable the method is though; there is the potential for the
842      register state pre-call being different to that on return.  */
843   fs->pc = frame_unwind_address_in_block (next_frame, NORMAL_FRAME);
844
845   /* Find the correct FDE.  */
846   fde = dwarf2_frame_find_fde (&fs->pc);
847   gdb_assert (fde != NULL);
848
849   /* Extract any interesting information from the CIE.  */
850   fs->data_align = fde->cie->data_alignment_factor;
851   fs->code_align = fde->cie->code_alignment_factor;
852   fs->retaddr_column = fde->cie->return_address_register;
853
854   /* Check for "quirks" - known bugs in producers.  */
855   dwarf2_frame_find_quirks (fs, fde);
856
857   /* First decode all the insns in the CIE.  */
858   execute_cfa_program (fde->cie->initial_instructions,
859                        fde->cie->end, next_frame, fs, fde->eh_frame_p);
860
861   /* Save the initialized register set.  */
862   fs->initial = fs->regs;
863   fs->initial.reg = dwarf2_frame_state_copy_regs (&fs->regs);
864
865   /* Then decode the insns in the FDE up to our target PC.  */
866   execute_cfa_program (fde->instructions, fde->end, next_frame, fs,
867                        fde->eh_frame_p);
868
869   /* Caclulate the CFA.  */
870   switch (fs->cfa_how)
871     {
872     case CFA_REG_OFFSET:
873       cache->cfa = read_reg (next_frame, fs->cfa_reg);
874       if (fs->armcc_cfa_offsets_reversed)
875         cache->cfa -= fs->cfa_offset;
876       else
877         cache->cfa += fs->cfa_offset;
878       break;
879
880     case CFA_EXP:
881       cache->cfa =
882         execute_stack_op (fs->cfa_exp, fs->cfa_exp_len, next_frame, 0);
883       break;
884
885     default:
886       internal_error (__FILE__, __LINE__, _("Unknown CFA rule."));
887     }
888
889   /* Initialize the register state.  */
890   {
891     int regnum;
892
893     for (regnum = 0; regnum < num_regs; regnum++)
894       dwarf2_frame_init_reg (gdbarch, regnum, &cache->reg[regnum], next_frame);
895   }
896
897   /* Go through the DWARF2 CFI generated table and save its register
898      location information in the cache.  Note that we don't skip the
899      return address column; it's perfectly all right for it to
900      correspond to a real register.  If it doesn't correspond to a
901      real register, or if we shouldn't treat it as such,
902      DWARF2_REG_TO_REGNUM should be defined to return a number outside
903      the range [0, NUM_REGS).  */
904   {
905     int column;         /* CFI speak for "register number".  */
906
907     for (column = 0; column < fs->regs.num_regs; column++)
908       {
909         /* Use the GDB register number as the destination index.  */
910         int regnum = DWARF2_REG_TO_REGNUM (column);
911
912         /* If there's no corresponding GDB register, ignore it.  */
913         if (regnum < 0 || regnum >= num_regs)
914           continue;
915
916         /* NOTE: cagney/2003-09-05: CFI should specify the disposition
917            of all debug info registers.  If it doesn't, complain (but
918            not too loudly).  It turns out that GCC assumes that an
919            unspecified register implies "same value" when CFI (draft
920            7) specifies nothing at all.  Such a register could equally
921            be interpreted as "undefined".  Also note that this check
922            isn't sufficient; it only checks that all registers in the
923            range [0 .. max column] are specified, and won't detect
924            problems when a debug info register falls outside of the
925            table.  We need a way of iterating through all the valid
926            DWARF2 register numbers.  */
927         if (fs->regs.reg[column].how == DWARF2_FRAME_REG_UNSPECIFIED)
928           {
929             if (cache->reg[regnum].how == DWARF2_FRAME_REG_UNSPECIFIED)
930               complaint (&symfile_complaints, _("\
931 incomplete CFI data; unspecified registers (e.g., %s) at 0x%s"),
932                          gdbarch_register_name (gdbarch, regnum),
933                          paddr_nz (fs->pc));
934           }
935         else
936           cache->reg[regnum] = fs->regs.reg[column];
937       }
938   }
939
940   /* Eliminate any DWARF2_FRAME_REG_RA rules, and save the information
941      we need for evaluating DWARF2_FRAME_REG_RA_OFFSET rules.  */
942   {
943     int regnum;
944
945     for (regnum = 0; regnum < num_regs; regnum++)
946       {
947         if (cache->reg[regnum].how == DWARF2_FRAME_REG_RA
948             || cache->reg[regnum].how == DWARF2_FRAME_REG_RA_OFFSET)
949           {
950             struct dwarf2_frame_state_reg *retaddr_reg =
951               &fs->regs.reg[fs->retaddr_column];
952
953             /* It seems rather bizarre to specify an "empty" column as
954                the return adress column.  However, this is exactly
955                what GCC does on some targets.  It turns out that GCC
956                assumes that the return address can be found in the
957                register corresponding to the return address column.
958                Incidentally, that's how we should treat a return
959                address column specifying "same value" too.  */
960             if (fs->retaddr_column < fs->regs.num_regs
961                 && retaddr_reg->how != DWARF2_FRAME_REG_UNSPECIFIED
962                 && retaddr_reg->how != DWARF2_FRAME_REG_SAME_VALUE)
963               {
964                 if (cache->reg[regnum].how == DWARF2_FRAME_REG_RA)
965                   cache->reg[regnum] = *retaddr_reg;
966                 else
967                   cache->retaddr_reg = *retaddr_reg;
968               }
969             else
970               {
971                 if (cache->reg[regnum].how == DWARF2_FRAME_REG_RA)
972                   {
973                     cache->reg[regnum].loc.reg = fs->retaddr_column;
974                     cache->reg[regnum].how = DWARF2_FRAME_REG_SAVED_REG;
975                   }
976                 else
977                   {
978                     cache->retaddr_reg.loc.reg = fs->retaddr_column;
979                     cache->retaddr_reg.how = DWARF2_FRAME_REG_SAVED_REG;
980                   }
981               }
982           }
983       }
984   }
985
986   if (fs->retaddr_column < fs->regs.num_regs
987       && fs->regs.reg[fs->retaddr_column].how == DWARF2_FRAME_REG_UNDEFINED)
988     cache->undefined_retaddr = 1;
989
990   do_cleanups (old_chain);
991
992   *this_cache = cache;
993   return cache;
994 }
995
996 static void
997 dwarf2_frame_this_id (struct frame_info *next_frame, void **this_cache,
998                       struct frame_id *this_id)
999 {
1000   struct dwarf2_frame_cache *cache =
1001     dwarf2_frame_cache (next_frame, this_cache);
1002
1003   if (cache->undefined_retaddr)
1004     return;
1005
1006   (*this_id) = frame_id_build (cache->cfa,
1007                                frame_func_unwind (next_frame, NORMAL_FRAME));
1008 }
1009
1010 static void
1011 dwarf2_signal_frame_this_id (struct frame_info *next_frame, void **this_cache,
1012                              struct frame_id *this_id)
1013 {
1014   struct dwarf2_frame_cache *cache =
1015     dwarf2_frame_cache (next_frame, this_cache);
1016
1017   if (cache->undefined_retaddr)
1018     return;
1019
1020   (*this_id) = frame_id_build (cache->cfa,
1021                                frame_func_unwind (next_frame, SIGTRAMP_FRAME));
1022 }
1023
1024 static void
1025 dwarf2_frame_prev_register (struct frame_info *next_frame, void **this_cache,
1026                             int regnum, int *optimizedp,
1027                             enum lval_type *lvalp, CORE_ADDR *addrp,
1028                             int *realnump, gdb_byte *valuep)
1029 {
1030   struct gdbarch *gdbarch = get_frame_arch (next_frame);
1031   struct dwarf2_frame_cache *cache =
1032     dwarf2_frame_cache (next_frame, this_cache);
1033
1034   switch (cache->reg[regnum].how)
1035     {
1036     case DWARF2_FRAME_REG_UNDEFINED:
1037       /* If CFI explicitly specified that the value isn't defined,
1038          mark it as optimized away; the value isn't available.  */
1039       *optimizedp = 1;
1040       *lvalp = not_lval;
1041       *addrp = 0;
1042       *realnump = -1;
1043       if (valuep)
1044         {
1045           /* In some cases, for example %eflags on the i386, we have
1046              to provide a sane value, even though this register wasn't
1047              saved.  Assume we can get it from NEXT_FRAME.  */
1048           frame_unwind_register (next_frame, regnum, valuep);
1049         }
1050       break;
1051
1052     case DWARF2_FRAME_REG_SAVED_OFFSET:
1053       *optimizedp = 0;
1054       *lvalp = lval_memory;
1055       *addrp = cache->cfa + cache->reg[regnum].loc.offset;
1056       *realnump = -1;
1057       if (valuep)
1058         {
1059           /* Read the value in from memory.  */
1060           read_memory (*addrp, valuep, register_size (gdbarch, regnum));
1061         }
1062       break;
1063
1064     case DWARF2_FRAME_REG_SAVED_REG:
1065       *optimizedp = 0;
1066       *lvalp = lval_register;
1067       *addrp = 0;
1068       *realnump = DWARF2_REG_TO_REGNUM (cache->reg[regnum].loc.reg);
1069       if (valuep)
1070         frame_unwind_register (next_frame, (*realnump), valuep);
1071       break;
1072
1073     case DWARF2_FRAME_REG_SAVED_EXP:
1074       *optimizedp = 0;
1075       *lvalp = lval_memory;
1076       *addrp = execute_stack_op (cache->reg[regnum].loc.exp,
1077                                  cache->reg[regnum].exp_len,
1078                                  next_frame, cache->cfa);
1079       *realnump = -1;
1080       if (valuep)
1081         {
1082           /* Read the value in from memory.  */
1083           read_memory (*addrp, valuep, register_size (gdbarch, regnum));
1084         }
1085       break;
1086
1087     case DWARF2_FRAME_REG_SAVED_VAL_OFFSET:
1088       *optimizedp = 0;
1089       *lvalp = not_lval;
1090       *addrp = 0;
1091       *realnump = -1;
1092       if (valuep)
1093         store_unsigned_integer (valuep, register_size (gdbarch, regnum),
1094                                 cache->cfa + cache->reg[regnum].loc.offset);
1095       break;
1096
1097     case DWARF2_FRAME_REG_SAVED_VAL_EXP:
1098       *optimizedp = 0;
1099       *lvalp = not_lval;
1100       *addrp = 0;
1101       *realnump = -1;
1102       if (valuep)
1103         store_unsigned_integer (valuep, register_size (gdbarch, regnum),
1104                                 execute_stack_op (cache->reg[regnum].loc.exp,
1105                                                   cache->reg[regnum].exp_len,
1106                                                   next_frame, cache->cfa));
1107       break;
1108
1109     case DWARF2_FRAME_REG_UNSPECIFIED:
1110       /* GCC, in its infinite wisdom decided to not provide unwind
1111          information for registers that are "same value".  Since
1112          DWARF2 (3 draft 7) doesn't define such behavior, said
1113          registers are actually undefined (which is different to CFI
1114          "undefined").  Code above issues a complaint about this.
1115          Here just fudge the books, assume GCC, and that the value is
1116          more inner on the stack.  */
1117       *optimizedp = 0;
1118       *lvalp = lval_register;
1119       *addrp = 0;
1120       *realnump = regnum;
1121       if (valuep)
1122         frame_unwind_register (next_frame, (*realnump), valuep);
1123       break;
1124
1125     case DWARF2_FRAME_REG_SAME_VALUE:
1126       *optimizedp = 0;
1127       *lvalp = lval_register;
1128       *addrp = 0;
1129       *realnump = regnum;
1130       if (valuep)
1131         frame_unwind_register (next_frame, (*realnump), valuep);
1132       break;
1133
1134     case DWARF2_FRAME_REG_CFA:
1135       *optimizedp = 0;
1136       *lvalp = not_lval;
1137       *addrp = 0;
1138       *realnump = -1;
1139       if (valuep)
1140         {
1141           /* Store the value.  */
1142           store_typed_address (valuep, builtin_type_void_data_ptr, cache->cfa);
1143         }
1144       break;
1145
1146     case DWARF2_FRAME_REG_CFA_OFFSET:
1147       *optimizedp = 0;
1148       *lvalp = not_lval;
1149       *addrp = 0;
1150       *realnump = -1;
1151       if (valuep)
1152         {
1153           /* Store the value.  */
1154           store_typed_address (valuep, builtin_type_void_data_ptr,
1155                                cache->cfa + cache->reg[regnum].loc.offset);
1156         }
1157       break;
1158
1159     case DWARF2_FRAME_REG_RA_OFFSET:
1160       *optimizedp = 0;
1161       *lvalp = not_lval;
1162       *addrp = 0;
1163       *realnump = -1;
1164       if (valuep)
1165         {
1166           CORE_ADDR pc = cache->reg[regnum].loc.offset;
1167
1168           regnum = DWARF2_REG_TO_REGNUM (cache->retaddr_reg.loc.reg);
1169           pc += frame_unwind_register_unsigned (next_frame, regnum);
1170           store_typed_address (valuep, builtin_type_void_func_ptr, pc);
1171         }
1172       break;
1173
1174     default:
1175       internal_error (__FILE__, __LINE__, _("Unknown register rule."));
1176     }
1177 }
1178
1179 static const struct frame_unwind dwarf2_frame_unwind =
1180 {
1181   NORMAL_FRAME,
1182   dwarf2_frame_this_id,
1183   dwarf2_frame_prev_register
1184 };
1185
1186 static const struct frame_unwind dwarf2_signal_frame_unwind =
1187 {
1188   SIGTRAMP_FRAME,
1189   dwarf2_signal_frame_this_id,
1190   dwarf2_frame_prev_register
1191 };
1192
1193 const struct frame_unwind *
1194 dwarf2_frame_sniffer (struct frame_info *next_frame)
1195 {
1196   /* Grab an address that is guarenteed to reside somewhere within the
1197      function.  frame_pc_unwind(), for a no-return next function, can
1198      end up returning something past the end of this function's body.
1199      If the frame we're sniffing for is a signal frame whose start
1200      address is placed on the stack by the OS, its FDE must
1201      extend one byte before its start address or we will miss it.  */
1202   CORE_ADDR block_addr = frame_unwind_address_in_block (next_frame,
1203                                                         NORMAL_FRAME);
1204   struct dwarf2_fde *fde = dwarf2_frame_find_fde (&block_addr);
1205   if (!fde)
1206     return NULL;
1207
1208   /* On some targets, signal trampolines may have unwind information.
1209      We need to recognize them so that we set the frame type
1210      correctly.  */
1211
1212   if (fde->cie->signal_frame
1213       || dwarf2_frame_signal_frame_p (get_frame_arch (next_frame),
1214                                       next_frame))
1215     return &dwarf2_signal_frame_unwind;
1216
1217   return &dwarf2_frame_unwind;
1218 }
1219 \f
1220
1221 /* There is no explicitly defined relationship between the CFA and the
1222    location of frame's local variables and arguments/parameters.
1223    Therefore, frame base methods on this page should probably only be
1224    used as a last resort, just to avoid printing total garbage as a
1225    response to the "info frame" command.  */
1226
1227 static CORE_ADDR
1228 dwarf2_frame_base_address (struct frame_info *next_frame, void **this_cache)
1229 {
1230   struct dwarf2_frame_cache *cache =
1231     dwarf2_frame_cache (next_frame, this_cache);
1232
1233   return cache->cfa;
1234 }
1235
1236 static const struct frame_base dwarf2_frame_base =
1237 {
1238   &dwarf2_frame_unwind,
1239   dwarf2_frame_base_address,
1240   dwarf2_frame_base_address,
1241   dwarf2_frame_base_address
1242 };
1243
1244 const struct frame_base *
1245 dwarf2_frame_base_sniffer (struct frame_info *next_frame)
1246 {
1247   CORE_ADDR block_addr = frame_unwind_address_in_block (next_frame,
1248                                                         NORMAL_FRAME);
1249   if (dwarf2_frame_find_fde (&block_addr))
1250     return &dwarf2_frame_base;
1251
1252   return NULL;
1253 }
1254 \f
1255 /* A minimal decoding of DWARF2 compilation units.  We only decode
1256    what's needed to get to the call frame information.  */
1257
1258 struct comp_unit
1259 {
1260   /* Keep the bfd convenient.  */
1261   bfd *abfd;
1262
1263   struct objfile *objfile;
1264
1265   /* Linked list of CIEs for this object.  */
1266   struct dwarf2_cie *cie;
1267
1268   /* Pointer to the .debug_frame section loaded into memory.  */
1269   gdb_byte *dwarf_frame_buffer;
1270
1271   /* Length of the loaded .debug_frame section.  */
1272   unsigned long dwarf_frame_size;
1273
1274   /* Pointer to the .debug_frame section.  */
1275   asection *dwarf_frame_section;
1276
1277   /* Base for DW_EH_PE_datarel encodings.  */
1278   bfd_vma dbase;
1279
1280   /* Base for DW_EH_PE_textrel encodings.  */
1281   bfd_vma tbase;
1282 };
1283
1284 const struct objfile_data *dwarf2_frame_objfile_data;
1285
1286 static unsigned int
1287 read_1_byte (bfd *abfd, gdb_byte *buf)
1288 {
1289   return bfd_get_8 (abfd, buf);
1290 }
1291
1292 static unsigned int
1293 read_4_bytes (bfd *abfd, gdb_byte *buf)
1294 {
1295   return bfd_get_32 (abfd, buf);
1296 }
1297
1298 static ULONGEST
1299 read_8_bytes (bfd *abfd, gdb_byte *buf)
1300 {
1301   return bfd_get_64 (abfd, buf);
1302 }
1303
1304 static ULONGEST
1305 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
1306 {
1307   ULONGEST result;
1308   unsigned int num_read;
1309   int shift;
1310   gdb_byte byte;
1311
1312   result = 0;
1313   shift = 0;
1314   num_read = 0;
1315
1316   do
1317     {
1318       byte = bfd_get_8 (abfd, (bfd_byte *) buf);
1319       buf++;
1320       num_read++;
1321       result |= ((byte & 0x7f) << shift);
1322       shift += 7;
1323     }
1324   while (byte & 0x80);
1325
1326   *bytes_read_ptr = num_read;
1327
1328   return result;
1329 }
1330
1331 static LONGEST
1332 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
1333 {
1334   LONGEST result;
1335   int shift;
1336   unsigned int num_read;
1337   gdb_byte byte;
1338
1339   result = 0;
1340   shift = 0;
1341   num_read = 0;
1342
1343   do
1344     {
1345       byte = bfd_get_8 (abfd, (bfd_byte *) buf);
1346       buf++;
1347       num_read++;
1348       result |= ((byte & 0x7f) << shift);
1349       shift += 7;
1350     }
1351   while (byte & 0x80);
1352
1353   if (shift < 8 * sizeof (result) && (byte & 0x40))
1354     result |= -(((LONGEST)1) << shift);
1355
1356   *bytes_read_ptr = num_read;
1357
1358   return result;
1359 }
1360
1361 static ULONGEST
1362 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
1363 {
1364   LONGEST result;
1365
1366   result = bfd_get_32 (abfd, buf);
1367   if (result == 0xffffffff)
1368     {
1369       result = bfd_get_64 (abfd, buf + 4);
1370       *bytes_read_ptr = 12;
1371     }
1372   else
1373     *bytes_read_ptr = 4;
1374
1375   return result;
1376 }
1377 \f
1378
1379 /* Pointer encoding helper functions.  */
1380
1381 /* GCC supports exception handling based on DWARF2 CFI.  However, for
1382    technical reasons, it encodes addresses in its FDE's in a different
1383    way.  Several "pointer encodings" are supported.  The encoding
1384    that's used for a particular FDE is determined by the 'R'
1385    augmentation in the associated CIE.  The argument of this
1386    augmentation is a single byte.  
1387
1388    The address can be encoded as 2 bytes, 4 bytes, 8 bytes, or as a
1389    LEB128.  This is encoded in bits 0, 1 and 2.  Bit 3 encodes whether
1390    the address is signed or unsigned.  Bits 4, 5 and 6 encode how the
1391    address should be interpreted (absolute, relative to the current
1392    position in the FDE, ...).  Bit 7, indicates that the address
1393    should be dereferenced.  */
1394
1395 static gdb_byte
1396 encoding_for_size (unsigned int size)
1397 {
1398   switch (size)
1399     {
1400     case 2:
1401       return DW_EH_PE_udata2;
1402     case 4:
1403       return DW_EH_PE_udata4;
1404     case 8:
1405       return DW_EH_PE_udata8;
1406     default:
1407       internal_error (__FILE__, __LINE__, _("Unsupported address size"));
1408     }
1409 }
1410
1411 static unsigned int
1412 size_of_encoded_value (gdb_byte encoding)
1413 {
1414   if (encoding == DW_EH_PE_omit)
1415     return 0;
1416
1417   switch (encoding & 0x07)
1418     {
1419     case DW_EH_PE_absptr:
1420       return TYPE_LENGTH (builtin_type_void_data_ptr);
1421     case DW_EH_PE_udata2:
1422       return 2;
1423     case DW_EH_PE_udata4:
1424       return 4;
1425     case DW_EH_PE_udata8:
1426       return 8;
1427     default:
1428       internal_error (__FILE__, __LINE__, _("Invalid or unsupported encoding"));
1429     }
1430 }
1431
1432 static CORE_ADDR
1433 read_encoded_value (struct comp_unit *unit, gdb_byte encoding,
1434                     gdb_byte *buf, unsigned int *bytes_read_ptr)
1435 {
1436   int ptr_len = size_of_encoded_value (DW_EH_PE_absptr);
1437   ptrdiff_t offset;
1438   CORE_ADDR base;
1439
1440   /* GCC currently doesn't generate DW_EH_PE_indirect encodings for
1441      FDE's.  */
1442   if (encoding & DW_EH_PE_indirect)
1443     internal_error (__FILE__, __LINE__, 
1444                     _("Unsupported encoding: DW_EH_PE_indirect"));
1445
1446   *bytes_read_ptr = 0;
1447
1448   switch (encoding & 0x70)
1449     {
1450     case DW_EH_PE_absptr:
1451       base = 0;
1452       break;
1453     case DW_EH_PE_pcrel:
1454       base = bfd_get_section_vma (unit->bfd, unit->dwarf_frame_section);
1455       base += (buf - unit->dwarf_frame_buffer);
1456       break;
1457     case DW_EH_PE_datarel:
1458       base = unit->dbase;
1459       break;
1460     case DW_EH_PE_textrel:
1461       base = unit->tbase;
1462       break;
1463     case DW_EH_PE_funcrel:
1464       /* FIXME: kettenis/20040501: For now just pretend
1465          DW_EH_PE_funcrel is equivalent to DW_EH_PE_absptr.  For
1466          reading the initial location of an FDE it should be treated
1467          as such, and currently that's the only place where this code
1468          is used.  */
1469       base = 0;
1470       break;
1471     case DW_EH_PE_aligned:
1472       base = 0;
1473       offset = buf - unit->dwarf_frame_buffer;
1474       if ((offset % ptr_len) != 0)
1475         {
1476           *bytes_read_ptr = ptr_len - (offset % ptr_len);
1477           buf += *bytes_read_ptr;
1478         }
1479       break;
1480     default:
1481       internal_error (__FILE__, __LINE__, _("Invalid or unsupported encoding"));
1482     }
1483
1484   if ((encoding & 0x07) == 0x00)
1485     encoding |= encoding_for_size (ptr_len);
1486
1487   switch (encoding & 0x0f)
1488     {
1489     case DW_EH_PE_uleb128:
1490       {
1491         ULONGEST value;
1492         gdb_byte *end_buf = buf + (sizeof (value) + 1) * 8 / 7;
1493         *bytes_read_ptr += read_uleb128 (buf, end_buf, &value) - buf;
1494         return base + value;
1495       }
1496     case DW_EH_PE_udata2:
1497       *bytes_read_ptr += 2;
1498       return (base + bfd_get_16 (unit->abfd, (bfd_byte *) buf));
1499     case DW_EH_PE_udata4:
1500       *bytes_read_ptr += 4;
1501       return (base + bfd_get_32 (unit->abfd, (bfd_byte *) buf));
1502     case DW_EH_PE_udata8:
1503       *bytes_read_ptr += 8;
1504       return (base + bfd_get_64 (unit->abfd, (bfd_byte *) buf));
1505     case DW_EH_PE_sleb128:
1506       {
1507         LONGEST value;
1508         gdb_byte *end_buf = buf + (sizeof (value) + 1) * 8 / 7;
1509         *bytes_read_ptr += read_sleb128 (buf, end_buf, &value) - buf;
1510         return base + value;
1511       }
1512     case DW_EH_PE_sdata2:
1513       *bytes_read_ptr += 2;
1514       return (base + bfd_get_signed_16 (unit->abfd, (bfd_byte *) buf));
1515     case DW_EH_PE_sdata4:
1516       *bytes_read_ptr += 4;
1517       return (base + bfd_get_signed_32 (unit->abfd, (bfd_byte *) buf));
1518     case DW_EH_PE_sdata8:
1519       *bytes_read_ptr += 8;
1520       return (base + bfd_get_signed_64 (unit->abfd, (bfd_byte *) buf));
1521     default:
1522       internal_error (__FILE__, __LINE__, _("Invalid or unsupported encoding"));
1523     }
1524 }
1525 \f
1526
1527 /* GCC uses a single CIE for all FDEs in a .debug_frame section.
1528    That's why we use a simple linked list here.  */
1529
1530 static struct dwarf2_cie *
1531 find_cie (struct comp_unit *unit, ULONGEST cie_pointer)
1532 {
1533   struct dwarf2_cie *cie = unit->cie;
1534
1535   while (cie)
1536     {
1537       if (cie->cie_pointer == cie_pointer)
1538         return cie;
1539
1540       cie = cie->next;
1541     }
1542
1543   return NULL;
1544 }
1545
1546 static void
1547 add_cie (struct comp_unit *unit, struct dwarf2_cie *cie)
1548 {
1549   cie->next = unit->cie;
1550   unit->cie = cie;
1551 }
1552
1553 /* Find the FDE for *PC.  Return a pointer to the FDE, and store the
1554    inital location associated with it into *PC.  */
1555
1556 static struct dwarf2_fde *
1557 dwarf2_frame_find_fde (CORE_ADDR *pc)
1558 {
1559   struct objfile *objfile;
1560
1561   ALL_OBJFILES (objfile)
1562     {
1563       struct dwarf2_fde *fde;
1564       CORE_ADDR offset;
1565
1566       fde = objfile_data (objfile, dwarf2_frame_objfile_data);
1567       if (fde == NULL)
1568         continue;
1569
1570       gdb_assert (objfile->section_offsets);
1571       offset = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1572
1573       while (fde)
1574         {
1575           if (*pc >= fde->initial_location + offset
1576               && *pc < fde->initial_location + offset + fde->address_range)
1577             {
1578               *pc = fde->initial_location + offset;
1579               return fde;
1580             }
1581
1582           fde = fde->next;
1583         }
1584     }
1585
1586   return NULL;
1587 }
1588
1589 static void
1590 add_fde (struct comp_unit *unit, struct dwarf2_fde *fde)
1591 {
1592   fde->next = objfile_data (unit->objfile, dwarf2_frame_objfile_data);
1593   set_objfile_data (unit->objfile, dwarf2_frame_objfile_data, fde);
1594 }
1595
1596 #ifdef CC_HAS_LONG_LONG
1597 #define DW64_CIE_ID 0xffffffffffffffffULL
1598 #else
1599 #define DW64_CIE_ID ~0
1600 #endif
1601
1602 static gdb_byte *decode_frame_entry (struct comp_unit *unit, gdb_byte *start,
1603                                      int eh_frame_p);
1604
1605 /* Decode the next CIE or FDE.  Return NULL if invalid input, otherwise
1606    the next byte to be processed.  */
1607 static gdb_byte *
1608 decode_frame_entry_1 (struct comp_unit *unit, gdb_byte *start, int eh_frame_p)
1609 {
1610   gdb_byte *buf, *end;
1611   LONGEST length;
1612   unsigned int bytes_read;
1613   int dwarf64_p;
1614   ULONGEST cie_id;
1615   ULONGEST cie_pointer;
1616
1617   buf = start;
1618   length = read_initial_length (unit->abfd, buf, &bytes_read);
1619   buf += bytes_read;
1620   end = buf + length;
1621
1622   /* Are we still within the section? */
1623   if (end > unit->dwarf_frame_buffer + unit->dwarf_frame_size)
1624     return NULL;
1625
1626   if (length == 0)
1627     return end;
1628
1629   /* Distinguish between 32 and 64-bit encoded frame info.  */
1630   dwarf64_p = (bytes_read == 12);
1631
1632   /* In a .eh_frame section, zero is used to distinguish CIEs from FDEs.  */
1633   if (eh_frame_p)
1634     cie_id = 0;
1635   else if (dwarf64_p)
1636     cie_id = DW64_CIE_ID;
1637   else
1638     cie_id = DW_CIE_ID;
1639
1640   if (dwarf64_p)
1641     {
1642       cie_pointer = read_8_bytes (unit->abfd, buf);
1643       buf += 8;
1644     }
1645   else
1646     {
1647       cie_pointer = read_4_bytes (unit->abfd, buf);
1648       buf += 4;
1649     }
1650
1651   if (cie_pointer == cie_id)
1652     {
1653       /* This is a CIE.  */
1654       struct dwarf2_cie *cie;
1655       char *augmentation;
1656       unsigned int cie_version;
1657
1658       /* Record the offset into the .debug_frame section of this CIE.  */
1659       cie_pointer = start - unit->dwarf_frame_buffer;
1660
1661       /* Check whether we've already read it.  */
1662       if (find_cie (unit, cie_pointer))
1663         return end;
1664
1665       cie = (struct dwarf2_cie *)
1666         obstack_alloc (&unit->objfile->objfile_obstack,
1667                        sizeof (struct dwarf2_cie));
1668       cie->initial_instructions = NULL;
1669       cie->cie_pointer = cie_pointer;
1670
1671       /* The encoding for FDE's in a normal .debug_frame section
1672          depends on the target address size.  */
1673       cie->encoding = DW_EH_PE_absptr;
1674
1675       /* We'll determine the final value later, but we need to
1676          initialize it conservatively.  */
1677       cie->signal_frame = 0;
1678
1679       /* Check version number.  */
1680       cie_version = read_1_byte (unit->abfd, buf);
1681       if (cie_version != 1 && cie_version != 3)
1682         return NULL;
1683       cie->version = cie_version;
1684       buf += 1;
1685
1686       /* Interpret the interesting bits of the augmentation.  */
1687       cie->augmentation = augmentation = (char *) buf;
1688       buf += (strlen (augmentation) + 1);
1689
1690       /* Ignore armcc augmentations.  We only use them for quirks,
1691          and that doesn't happen until later.  */
1692       if (strncmp (augmentation, "armcc", 5) == 0)
1693         augmentation += strlen (augmentation);
1694
1695       /* The GCC 2.x "eh" augmentation has a pointer immediately
1696          following the augmentation string, so it must be handled
1697          first.  */
1698       if (augmentation[0] == 'e' && augmentation[1] == 'h')
1699         {
1700           /* Skip.  */
1701           buf += TYPE_LENGTH (builtin_type_void_data_ptr);
1702           augmentation += 2;
1703         }
1704
1705       cie->code_alignment_factor =
1706         read_unsigned_leb128 (unit->abfd, buf, &bytes_read);
1707       buf += bytes_read;
1708
1709       cie->data_alignment_factor =
1710         read_signed_leb128 (unit->abfd, buf, &bytes_read);
1711       buf += bytes_read;
1712
1713       if (cie_version == 1)
1714         {
1715           cie->return_address_register = read_1_byte (unit->abfd, buf);
1716           bytes_read = 1;
1717         }
1718       else
1719         cie->return_address_register = read_unsigned_leb128 (unit->abfd, buf,
1720                                                              &bytes_read);
1721       cie->return_address_register
1722         = dwarf2_frame_adjust_regnum (current_gdbarch,
1723                                       cie->return_address_register,
1724                                       eh_frame_p);
1725
1726       buf += bytes_read;
1727
1728       cie->saw_z_augmentation = (*augmentation == 'z');
1729       if (cie->saw_z_augmentation)
1730         {
1731           ULONGEST length;
1732
1733           length = read_unsigned_leb128 (unit->abfd, buf, &bytes_read);
1734           buf += bytes_read;
1735           if (buf > end)
1736             return NULL;
1737           cie->initial_instructions = buf + length;
1738           augmentation++;
1739         }
1740
1741       while (*augmentation)
1742         {
1743           /* "L" indicates a byte showing how the LSDA pointer is encoded.  */
1744           if (*augmentation == 'L')
1745             {
1746               /* Skip.  */
1747               buf++;
1748               augmentation++;
1749             }
1750
1751           /* "R" indicates a byte indicating how FDE addresses are encoded.  */
1752           else if (*augmentation == 'R')
1753             {
1754               cie->encoding = *buf++;
1755               augmentation++;
1756             }
1757
1758           /* "P" indicates a personality routine in the CIE augmentation.  */
1759           else if (*augmentation == 'P')
1760             {
1761               /* Skip.  Avoid indirection since we throw away the result.  */
1762               gdb_byte encoding = (*buf++) & ~DW_EH_PE_indirect;
1763               read_encoded_value (unit, encoding, buf, &bytes_read);
1764               buf += bytes_read;
1765               augmentation++;
1766             }
1767
1768           /* "S" indicates a signal frame, such that the return
1769              address must not be decremented to locate the call frame
1770              info for the previous frame; it might even be the first
1771              instruction of a function, so decrementing it would take
1772              us to a different function.  */
1773           else if (*augmentation == 'S')
1774             {
1775               cie->signal_frame = 1;
1776               augmentation++;
1777             }
1778
1779           /* Otherwise we have an unknown augmentation.  Assume that either
1780              there is no augmentation data, or we saw a 'z' prefix.  */
1781           else
1782             {
1783               if (cie->initial_instructions)
1784                 buf = cie->initial_instructions;
1785               break;
1786             }
1787         }
1788
1789       cie->initial_instructions = buf;
1790       cie->end = end;
1791
1792       add_cie (unit, cie);
1793     }
1794   else
1795     {
1796       /* This is a FDE.  */
1797       struct dwarf2_fde *fde;
1798
1799       /* In an .eh_frame section, the CIE pointer is the delta between the
1800          address within the FDE where the CIE pointer is stored and the
1801          address of the CIE.  Convert it to an offset into the .eh_frame
1802          section.  */
1803       if (eh_frame_p)
1804         {
1805           cie_pointer = buf - unit->dwarf_frame_buffer - cie_pointer;
1806           cie_pointer -= (dwarf64_p ? 8 : 4);
1807         }
1808
1809       /* In either case, validate the result is still within the section.  */
1810       if (cie_pointer >= unit->dwarf_frame_size)
1811         return NULL;
1812
1813       fde = (struct dwarf2_fde *)
1814         obstack_alloc (&unit->objfile->objfile_obstack,
1815                        sizeof (struct dwarf2_fde));
1816       fde->cie = find_cie (unit, cie_pointer);
1817       if (fde->cie == NULL)
1818         {
1819           decode_frame_entry (unit, unit->dwarf_frame_buffer + cie_pointer,
1820                               eh_frame_p);
1821           fde->cie = find_cie (unit, cie_pointer);
1822         }
1823
1824       gdb_assert (fde->cie != NULL);
1825
1826       fde->initial_location =
1827         read_encoded_value (unit, fde->cie->encoding, buf, &bytes_read);
1828       buf += bytes_read;
1829
1830       fde->address_range =
1831         read_encoded_value (unit, fde->cie->encoding & 0x0f, buf, &bytes_read);
1832       buf += bytes_read;
1833
1834       /* A 'z' augmentation in the CIE implies the presence of an
1835          augmentation field in the FDE as well.  The only thing known
1836          to be in here at present is the LSDA entry for EH.  So we
1837          can skip the whole thing.  */
1838       if (fde->cie->saw_z_augmentation)
1839         {
1840           ULONGEST length;
1841
1842           length = read_unsigned_leb128 (unit->abfd, buf, &bytes_read);
1843           buf += bytes_read + length;
1844           if (buf > end)
1845             return NULL;
1846         }
1847
1848       fde->instructions = buf;
1849       fde->end = end;
1850
1851       fde->eh_frame_p = eh_frame_p;
1852
1853       add_fde (unit, fde);
1854     }
1855
1856   return end;
1857 }
1858
1859 /* Read a CIE or FDE in BUF and decode it.  */
1860 static gdb_byte *
1861 decode_frame_entry (struct comp_unit *unit, gdb_byte *start, int eh_frame_p)
1862 {
1863   enum { NONE, ALIGN4, ALIGN8, FAIL } workaround = NONE;
1864   gdb_byte *ret;
1865   const char *msg;
1866   ptrdiff_t start_offset;
1867
1868   while (1)
1869     {
1870       ret = decode_frame_entry_1 (unit, start, eh_frame_p);
1871       if (ret != NULL)
1872         break;
1873
1874       /* We have corrupt input data of some form.  */
1875
1876       /* ??? Try, weakly, to work around compiler/assembler/linker bugs
1877          and mismatches wrt padding and alignment of debug sections.  */
1878       /* Note that there is no requirement in the standard for any
1879          alignment at all in the frame unwind sections.  Testing for
1880          alignment before trying to interpret data would be incorrect.
1881
1882          However, GCC traditionally arranged for frame sections to be
1883          sized such that the FDE length and CIE fields happen to be
1884          aligned (in theory, for performance).  This, unfortunately,
1885          was done with .align directives, which had the side effect of
1886          forcing the section to be aligned by the linker.
1887
1888          This becomes a problem when you have some other producer that
1889          creates frame sections that are not as strictly aligned.  That
1890          produces a hole in the frame info that gets filled by the 
1891          linker with zeros.
1892
1893          The GCC behaviour is arguably a bug, but it's effectively now
1894          part of the ABI, so we're now stuck with it, at least at the
1895          object file level.  A smart linker may decide, in the process
1896          of compressing duplicate CIE information, that it can rewrite
1897          the entire output section without this extra padding.  */
1898
1899       start_offset = start - unit->dwarf_frame_buffer;
1900       if (workaround < ALIGN4 && (start_offset & 3) != 0)
1901         {
1902           start += 4 - (start_offset & 3);
1903           workaround = ALIGN4;
1904           continue;
1905         }
1906       if (workaround < ALIGN8 && (start_offset & 7) != 0)
1907         {
1908           start += 8 - (start_offset & 7);
1909           workaround = ALIGN8;
1910           continue;
1911         }
1912
1913       /* Nothing left to try.  Arrange to return as if we've consumed
1914          the entire input section.  Hopefully we'll get valid info from
1915          the other of .debug_frame/.eh_frame.  */
1916       workaround = FAIL;
1917       ret = unit->dwarf_frame_buffer + unit->dwarf_frame_size;
1918       break;
1919     }
1920
1921   switch (workaround)
1922     {
1923     case NONE:
1924       break;
1925
1926     case ALIGN4:
1927       complaint (&symfile_complaints,
1928                  _("Corrupt data in %s:%s; align 4 workaround apparently succeeded"),
1929                  unit->dwarf_frame_section->owner->filename,
1930                  unit->dwarf_frame_section->name);
1931       break;
1932
1933     case ALIGN8:
1934       complaint (&symfile_complaints,
1935                  _("Corrupt data in %s:%s; align 8 workaround apparently succeeded"),
1936                  unit->dwarf_frame_section->owner->filename,
1937                  unit->dwarf_frame_section->name);
1938       break;
1939
1940     default:
1941       complaint (&symfile_complaints,
1942                  _("Corrupt data in %s:%s"),
1943                  unit->dwarf_frame_section->owner->filename,
1944                  unit->dwarf_frame_section->name);
1945       break;
1946     }
1947
1948   return ret;
1949 }
1950 \f
1951
1952 /* FIXME: kettenis/20030504: This still needs to be integrated with
1953    dwarf2read.c in a better way.  */
1954
1955 /* Imported from dwarf2read.c.  */
1956 extern asection *dwarf_frame_section;
1957 extern asection *dwarf_eh_frame_section;
1958
1959 /* Imported from dwarf2read.c.  */
1960 extern gdb_byte *dwarf2_read_section (struct objfile *objfile, asection *sectp);
1961
1962 void
1963 dwarf2_build_frame_info (struct objfile *objfile)
1964 {
1965   struct comp_unit unit;
1966   gdb_byte *frame_ptr;
1967
1968   /* Build a minimal decoding of the DWARF2 compilation unit.  */
1969   unit.abfd = objfile->obfd;
1970   unit.objfile = objfile;
1971   unit.dbase = 0;
1972   unit.tbase = 0;
1973
1974   /* First add the information from the .eh_frame section.  That way,
1975      the FDEs from that section are searched last.  */
1976   if (dwarf_eh_frame_section)
1977     {
1978       asection *got, *txt;
1979
1980       unit.cie = NULL;
1981       unit.dwarf_frame_buffer = dwarf2_read_section (objfile,
1982                                                      dwarf_eh_frame_section);
1983
1984       unit.dwarf_frame_size = bfd_get_section_size (dwarf_eh_frame_section);
1985       unit.dwarf_frame_section = dwarf_eh_frame_section;
1986
1987       /* FIXME: kettenis/20030602: This is the DW_EH_PE_datarel base
1988          that is used for the i386/amd64 target, which currently is
1989          the only target in GCC that supports/uses the
1990          DW_EH_PE_datarel encoding.  */
1991       got = bfd_get_section_by_name (unit.abfd, ".got");
1992       if (got)
1993         unit.dbase = got->vma;
1994
1995       /* GCC emits the DW_EH_PE_textrel encoding type on sh and ia64
1996          so far.  */
1997       txt = bfd_get_section_by_name (unit.abfd, ".text");
1998       if (txt)
1999         unit.tbase = txt->vma;
2000
2001       frame_ptr = unit.dwarf_frame_buffer;
2002       while (frame_ptr < unit.dwarf_frame_buffer + unit.dwarf_frame_size)
2003         frame_ptr = decode_frame_entry (&unit, frame_ptr, 1);
2004     }
2005
2006   if (dwarf_frame_section)
2007     {
2008       unit.cie = NULL;
2009       unit.dwarf_frame_buffer = dwarf2_read_section (objfile,
2010                                                      dwarf_frame_section);
2011       unit.dwarf_frame_size = bfd_get_section_size (dwarf_frame_section);
2012       unit.dwarf_frame_section = dwarf_frame_section;
2013
2014       frame_ptr = unit.dwarf_frame_buffer;
2015       while (frame_ptr < unit.dwarf_frame_buffer + unit.dwarf_frame_size)
2016         frame_ptr = decode_frame_entry (&unit, frame_ptr, 0);
2017     }
2018 }
2019
2020 /* Provide a prototype to silence -Wmissing-prototypes.  */
2021 void _initialize_dwarf2_frame (void);
2022
2023 void
2024 _initialize_dwarf2_frame (void)
2025 {
2026   dwarf2_frame_data = gdbarch_data_register_pre_init (dwarf2_frame_init);
2027   dwarf2_frame_objfile_data = register_objfile_data ();
2028 }