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