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