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