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