OSDN Git Service

* c-parse.in (structsp): Pedwarn when "enum foo" refers to an
[pf3gnuchains/gcc-fork.git] / gcc / frame-dwarf2.c
1 /* Subroutines needed for unwinding DWARF 2 format stack frame info
2    for exception handling.  */
3 /* Compile this one with gcc.  */
4 /* Copyright (C) 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
5    Contributed by Jason Merrill <jason@cygnus.com>.
6
7 This file is part of GNU CC.
8
9 GNU CC 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, or (at your option)
12 any later version.
13
14 In addition to the permissions in the GNU General Public License, the
15 Free Software Foundation gives you unlimited permission to link the
16 compiled version of this file into combinations with other programs,
17 and to distribute those combinations without any restriction coming
18 from the use of this file.  (The General Public License restrictions
19 do apply in other respects; for example, they cover modification of
20 the file, and distribution when not linked into a combine
21 executable.)
22
23 GNU CC is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26 GNU General Public License for more details.
27
28 You should have received a copy of the GNU General Public License
29 along with GNU CC; see the file COPYING.  If not, write to
30 the Free Software Foundation, 59 Temple Place - Suite 330,
31 Boston, MA 02111-1307, USA.  */
32
33 /* It is incorrect to include config.h here, because this file is being
34    compiled for the target, and hence definitions concerning only the host
35    do not apply.  */
36
37 #include "tconfig.h"
38 #include "tsystem.h"
39
40 #ifdef DWARF2_UNWIND_INFO
41 #include "dwarf2.h"
42 #include "frame.h"
43 #include "gthr.h"
44
45 #ifdef __GTHREAD_MUTEX_INIT
46 static __gthread_mutex_t object_mutex = __GTHREAD_MUTEX_INIT;
47 #else
48 static __gthread_mutex_t object_mutex;
49 #endif
50
51 /* Don't use `fancy_abort' here even if config.h says to use it.  */
52 #ifdef abort
53 #undef abort
54 #endif
55
56 /* Some types used by the DWARF 2 spec.  */
57
58 typedef          int  sword __attribute__ ((mode (SI)));
59 typedef unsigned int  uword __attribute__ ((mode (SI)));
60 typedef unsigned int  uaddr __attribute__ ((mode (pointer)));
61 typedef          int  saddr __attribute__ ((mode (pointer)));
62 typedef unsigned char ubyte;
63
64 /* Terminology:
65    CIE - Common Information Element
66    FDE - Frame Descriptor Element
67
68    There is one per function, and it describes where the function code
69    is located, and what the register lifetimes and stack layout are
70    within the function.
71
72    The data structures are defined in the DWARF specfication, although
73    not in a very readable way (see LITERATURE).
74
75    Every time an exception is thrown, the code needs to locate the FDE
76    for the current function, and starts to look for exception regions
77    from that FDE. This works in a two-level search:
78    a) in a linear search, find the shared image (i.e. DLL) containing
79       the PC
80    b) using the FDE table for that shared object, locate the FDE using
81       binary search (which requires the sorting).  */   
82
83 /* The first few fields of a CIE.  The CIE_id field is 0 for a CIE,
84    to distinguish it from a valid FDE.  FDEs are aligned to an addressing
85    unit boundary, but the fields within are unaligned.  */
86
87 struct dwarf_cie {
88   uword length;
89   sword CIE_id;
90   ubyte version;
91   char augmentation[0];
92 } __attribute__ ((packed, aligned (__alignof__ (void *))));
93
94 /* The first few fields of an FDE.  */
95
96 struct dwarf_fde {
97   uword length;
98   sword CIE_delta;
99   void* pc_begin;
100   uaddr pc_range;
101 } __attribute__ ((packed, aligned (__alignof__ (void *))));
102
103 typedef struct dwarf_fde fde;
104
105 /* Objects to be searched for frame unwind info.  */
106
107 static struct object *objects;
108
109 /* The information we care about from a CIE.  */
110
111 struct cie_info {
112   char *augmentation;
113   void *eh_ptr;
114   int code_align;
115   int data_align;
116   unsigned ra_regno;
117 };
118
119 /* The current unwind state, plus a saved copy for DW_CFA_remember_state.  */
120
121 struct frame_state_internal
122 {
123   struct frame_state s;
124   struct frame_state_internal *saved_state;
125 };
126 \f
127 /* This is undefined below if we need it to be an actual function.  */
128 #define init_object_mutex_once()
129
130 #if __GTHREADS
131 #ifdef __GTHREAD_MUTEX_INIT_FUNCTION
132
133 /* Helper for init_object_mutex_once.  */
134
135 static void
136 init_object_mutex (void)
137 {
138   __GTHREAD_MUTEX_INIT_FUNCTION (&object_mutex);
139 }
140
141 /* Call this to arrange to initialize the object mutex.  */
142
143 #undef init_object_mutex_once
144 static void
145 init_object_mutex_once (void)
146 {
147   static __gthread_once_t once = __GTHREAD_ONCE_INIT;
148   __gthread_once (&once, init_object_mutex);
149 }
150
151 #endif /* __GTHREAD_MUTEX_INIT_FUNCTION */
152 #endif /* __GTHREADS */
153 \f  
154 /* Decode the unsigned LEB128 constant at BUF into the variable pointed to
155    by R, and return the new value of BUF.  */
156
157 static void *
158 decode_uleb128 (unsigned char *buf, unsigned *r)
159 {
160   unsigned shift = 0;
161   unsigned result = 0;
162
163   while (1)
164     {
165       unsigned byte = *buf++;
166       result |= (byte & 0x7f) << shift;
167       if ((byte & 0x80) == 0)
168         break;
169       shift += 7;
170     }
171   *r = result;
172   return buf;
173 }
174
175 /* Decode the signed LEB128 constant at BUF into the variable pointed to
176    by R, and return the new value of BUF.  */
177
178 static void *
179 decode_sleb128 (unsigned char *buf, int *r)
180 {
181   unsigned shift = 0;
182   unsigned result = 0;
183   unsigned byte;
184
185   while (1)
186     {
187       byte = *buf++;
188       result |= (byte & 0x7f) << shift;
189       shift += 7;
190       if ((byte & 0x80) == 0)
191         break;
192     }
193   if (shift < (sizeof (*r) * 8) && (byte & 0x40) != 0)
194     result |= - (1 << shift);
195
196   *r = result;
197   return buf;
198 }
199
200 /* Read unaligned data from the instruction buffer.  */
201
202 union unaligned {
203   void *p;
204   unsigned b2 __attribute__ ((mode (HI)));
205   unsigned b4 __attribute__ ((mode (SI)));
206   unsigned b8 __attribute__ ((mode (DI)));
207 } __attribute__ ((packed));
208 static inline void *
209 read_pointer (void *p)
210 { union unaligned *up = p; return up->p; }
211 static inline unsigned
212 read_1byte (void *p)
213 { return *(unsigned char *)p; }
214 static inline unsigned
215 read_2byte (void *p)
216 { union unaligned *up = p; return up->b2; }
217 static inline unsigned
218 read_4byte (void *p)
219 { union unaligned *up = p; return up->b4; }
220 static inline unsigned long
221 read_8byte (void *p)
222 { union unaligned *up = p; return up->b8; }
223 \f
224 /* Ordering function for FDEs.  Functions can't overlap, so we just compare
225    their starting addresses.  */
226
227 static inline saddr
228 fde_compare (fde *x, fde *y)
229 {
230   return (saddr)x->pc_begin - (saddr)y->pc_begin;
231 }
232
233 /* Return the address of the FDE after P.  */
234
235 static inline fde *
236 next_fde (fde *p)
237 {
238   return (fde *)(((char *)p) + p->length + sizeof (p->length));
239 }
240
241 #include "frame.c"
242
243 static size_t
244 count_fdes (fde *this_fde)
245 {
246   size_t count;
247
248   for (count = 0; this_fde->length != 0; this_fde = next_fde (this_fde))
249     {
250       /* Skip CIEs and linked once FDE entries.  */
251       if (this_fde->CIE_delta == 0 || this_fde->pc_begin == 0)
252         continue;
253
254       ++count;
255     }
256
257   return count;
258 }
259
260 static void
261 add_fdes (fde *this_fde, fde_accumulator *accu, void **beg_ptr, void **end_ptr)
262 {
263   void *pc_begin = *beg_ptr;
264   void *pc_end = *end_ptr;
265
266   for (; this_fde->length != 0; this_fde = next_fde (this_fde))
267     {
268       /* Skip CIEs and linked once FDE entries.  */
269       if (this_fde->CIE_delta == 0 || this_fde->pc_begin == 0)
270         continue;
271
272       fde_insert (accu, this_fde);
273
274       if (this_fde->pc_begin < pc_begin)
275         pc_begin = this_fde->pc_begin;
276       if (this_fde->pc_begin + this_fde->pc_range > pc_end)
277         pc_end = this_fde->pc_begin + this_fde->pc_range;
278     }
279
280   *beg_ptr = pc_begin;
281   *end_ptr = pc_end;
282 }
283
284 /* search this fde table for the one containing the pc */
285 static fde *
286 search_fdes (fde *this_fde, void *pc)
287 {
288   for (; this_fde->length != 0; this_fde = next_fde (this_fde))
289     {
290       /* Skip CIEs and linked once FDE entries.  */
291       if (this_fde->CIE_delta == 0 || this_fde->pc_begin == 0)
292         continue;
293
294       if ((uaddr)((char *)pc - (char *)this_fde->pc_begin) < this_fde->pc_range)
295         return this_fde;
296     }
297   return NULL;
298 }
299
300 /* Set up a sorted array of pointers to FDEs for a loaded object.  We
301    count up the entries before allocating the array because it's likely to
302    be faster.  We can be called multiple times, should we have failed to
303    allocate a sorted fde array on a previous occasion.  */
304
305 static void
306 frame_init (struct object* ob)
307 {
308   size_t count;
309   fde_accumulator accu;
310   void *pc_begin, *pc_end;
311   fde **array;
312
313   if (ob->pc_begin)
314     count = ob->count;
315   else if (ob->fde_array)
316     {
317       fde **p = ob->fde_array;
318       for (count = 0; *p; ++p)
319         count += count_fdes (*p);
320     }
321   else
322     count = count_fdes (ob->fde_begin);
323   ob->count = count;
324
325   if (!start_fde_sort (&accu, count) && ob->pc_begin)
326     return;
327
328   pc_begin = (void*)(uaddr)-1;
329   pc_end = 0;
330
331   if (ob->fde_array)
332     {
333       fde **p = ob->fde_array;
334       for (; *p; ++p)
335         add_fdes (*p, &accu, &pc_begin, &pc_end);
336     }
337   else
338     add_fdes (ob->fde_begin, &accu, &pc_begin, &pc_end);
339
340   array = end_fde_sort (&accu, count);
341   if (array)
342     ob->fde_array = array;
343   ob->pc_begin = pc_begin;
344   ob->pc_end = pc_end;
345 }
346
347 /* Return a pointer to the FDE for the function containing PC.  */
348
349 static fde *
350 find_fde (void *pc)
351 {
352   struct object *ob;
353   size_t lo, hi;
354
355   init_object_mutex_once ();
356   __gthread_mutex_lock (&object_mutex);
357
358   /* Linear search through the objects, to find the one containing the pc. */
359   for (ob = objects; ob; ob = ob->next)
360     {
361       if (ob->pc_begin == 0)
362         frame_init (ob);
363       if (pc >= ob->pc_begin && pc < ob->pc_end)
364         break;
365     }
366
367   if (ob == 0)
368     {
369       __gthread_mutex_unlock (&object_mutex);
370       return 0;
371     }
372
373   if (!ob->fde_array || (void *)ob->fde_array == (void *)ob->fde_begin)
374     frame_init (ob);
375
376   if (ob->fde_array && (void *)ob->fde_array != (void *)ob->fde_begin)
377     {
378       __gthread_mutex_unlock (&object_mutex);
379       
380       /* Standard binary search algorithm.  */
381       for (lo = 0, hi = ob->count; lo < hi; )
382         {
383           size_t i = (lo + hi) / 2;
384           fde *f = ob->fde_array[i];
385
386           if (pc < f->pc_begin)
387             hi = i;
388           else if (pc >= f->pc_begin + f->pc_range)
389             lo = i + 1;
390           else
391             return f;
392         }
393     }
394   else
395     {
396       /* Long slow labourious linear search, cos we've no memory. */
397       fde *f;
398       
399       if (ob->fde_array)
400         {
401           fde **p = ob->fde_array;
402           
403           do
404             {
405               f = search_fdes (*p, pc);
406               if (f)
407                 break;
408               p++;
409             }
410           while (*p);
411         }
412       else
413         f = search_fdes (ob->fde_begin, pc);
414       __gthread_mutex_unlock (&object_mutex);
415       return f;
416     }
417   return 0;
418 }
419 \f
420 static inline struct dwarf_cie *
421 get_cie (fde *f)
422 {
423   return ((void *)&f->CIE_delta) - f->CIE_delta;
424 }
425
426 /* Extract any interesting information from the CIE for the translation
427    unit F belongs to.  */
428
429 static void *
430 extract_cie_info (fde *f, struct cie_info *c)
431 {
432   void *p;
433   int i;
434
435   c->augmentation = get_cie (f)->augmentation;
436
437   if (strcmp (c->augmentation, "") != 0
438       && strcmp (c->augmentation, "eh") != 0
439       && c->augmentation[0] != 'z')
440     return 0;
441
442   p = c->augmentation + strlen (c->augmentation) + 1;
443
444   if (strcmp (c->augmentation, "eh") == 0)
445     {
446       c->eh_ptr = read_pointer (p);
447       p += sizeof (void *);
448     }
449   else
450     c->eh_ptr = 0;
451
452   p = decode_uleb128 (p, &c->code_align);
453   p = decode_sleb128 (p, &c->data_align);
454   c->ra_regno = *(unsigned char *)p++;
455
456   /* If the augmentation starts with 'z', we now see the length of the
457      augmentation fields.  */
458   if (c->augmentation[0] == 'z')
459     {
460       p = decode_uleb128 (p, &i);
461       p += i;
462     }
463
464   return p;
465 }
466
467 /* Decode a DW_OP stack operation.  */
468
469 static void *
470 decode_stack_op (unsigned char *buf, struct frame_state *state)
471 {
472   enum dwarf_location_atom op;
473   int offset;
474
475   op = *buf++;
476   switch (op)
477     {
478     case DW_OP_reg0:
479     case DW_OP_reg1:
480     case DW_OP_reg2:
481     case DW_OP_reg3:
482     case DW_OP_reg4:
483     case DW_OP_reg5:
484     case DW_OP_reg6:
485     case DW_OP_reg7:
486     case DW_OP_reg8:
487     case DW_OP_reg9:
488     case DW_OP_reg10:
489     case DW_OP_reg11:
490     case DW_OP_reg12:
491     case DW_OP_reg13:
492     case DW_OP_reg14:
493     case DW_OP_reg15:
494     case DW_OP_reg16:
495     case DW_OP_reg17:
496     case DW_OP_reg18:
497     case DW_OP_reg19:
498     case DW_OP_reg20:
499     case DW_OP_reg21:
500     case DW_OP_reg22:
501     case DW_OP_reg23:
502     case DW_OP_reg24:
503     case DW_OP_reg25:
504     case DW_OP_reg26:
505     case DW_OP_reg27:
506     case DW_OP_reg28:
507     case DW_OP_reg29:
508     case DW_OP_reg30:
509     case DW_OP_reg31:
510       state->cfa_reg = op - DW_OP_reg0;
511       break;
512     case DW_OP_regx:
513       buf = decode_sleb128 (buf, &offset);
514       state->cfa_reg = offset;
515       break;
516     case DW_OP_breg0:
517     case DW_OP_breg1:
518     case DW_OP_breg2:
519     case DW_OP_breg3:
520     case DW_OP_breg4:
521     case DW_OP_breg5:
522     case DW_OP_breg6:
523     case DW_OP_breg7:
524     case DW_OP_breg8:
525     case DW_OP_breg9:
526     case DW_OP_breg10:
527     case DW_OP_breg11:
528     case DW_OP_breg12:
529     case DW_OP_breg13:
530     case DW_OP_breg14:
531     case DW_OP_breg15:
532     case DW_OP_breg16:
533     case DW_OP_breg17:
534     case DW_OP_breg18:
535     case DW_OP_breg19:
536     case DW_OP_breg20:
537     case DW_OP_breg21:
538     case DW_OP_breg22:
539     case DW_OP_breg23:
540     case DW_OP_breg24:
541     case DW_OP_breg25:
542     case DW_OP_breg26:
543     case DW_OP_breg27:
544     case DW_OP_breg28:
545     case DW_OP_breg29:
546     case DW_OP_breg30:
547     case DW_OP_breg31:
548       state->cfa_reg = op - DW_OP_breg0;
549       buf = decode_sleb128 (buf, &offset);
550       state->base_offset = offset;
551       break;
552     case DW_OP_bregx:
553       buf = decode_sleb128 (buf, &offset);
554       state->cfa_reg = offset;
555       buf = decode_sleb128 (buf, &offset);
556       state->base_offset = offset;
557       break;
558     case DW_OP_deref:
559       state->indirect = 1;
560       break;
561     case DW_OP_plus_uconst:
562       buf = decode_uleb128 (buf, &offset);
563       state->cfa_offset = offset;
564       break;
565     default:
566       abort ();
567     }
568   return buf;
569 }
570 /* Decode one instruction's worth of DWARF 2 call frame information.
571    Used by __frame_state_for.  Takes pointers P to the instruction to
572    decode, STATE to the current register unwind information, INFO to the
573    current CIE information, and PC to the current PC value.  Returns a
574    pointer to the next instruction.  */
575
576 static void *
577 execute_cfa_insn (void *p, struct frame_state_internal *state,
578                   struct cie_info *info, void **pc)
579 {
580   unsigned insn = *(unsigned char *)p++;
581   unsigned reg;
582   int offset;
583
584   if (insn & DW_CFA_advance_loc)
585     *pc += ((insn & 0x3f) * info->code_align);
586   else if (insn & DW_CFA_offset)
587     {
588       reg = (insn & 0x3f);
589       p = decode_uleb128 (p, &offset);
590       if (reg == state->s.cfa_reg)
591         /* Don't record anything about this register; it's only used to
592            reload SP in the epilogue.  We don't want to copy in SP
593            values for outer frames; we handle restoring SP specially.  */;
594       else
595         {
596           offset *= info->data_align;
597           state->s.saved[reg] = REG_SAVED_OFFSET;
598           state->s.reg_or_offset[reg] = offset;
599         }
600     }
601   else if (insn & DW_CFA_restore)
602     {
603       reg = (insn & 0x3f);
604       state->s.saved[reg] = REG_UNSAVED;
605     }
606   else switch (insn)
607     {
608     case DW_CFA_set_loc:
609       *pc = read_pointer (p);
610       p += sizeof (void *);
611       break;
612     case DW_CFA_advance_loc1:
613       *pc += read_1byte (p);
614       p += 1;
615       break;
616     case DW_CFA_advance_loc2:
617       *pc += read_2byte (p);
618       p += 2;
619       break;
620     case DW_CFA_advance_loc4:
621       *pc += read_4byte (p);
622       p += 4;
623       break;
624
625     case DW_CFA_offset_extended:
626       p = decode_uleb128 (p, &reg);
627       p = decode_uleb128 (p, &offset);
628       if (reg == state->s.cfa_reg)
629         /* Don't record anything; see above.  */;
630       else
631         {
632           offset *= info->data_align;
633           state->s.saved[reg] = REG_SAVED_OFFSET;
634           state->s.reg_or_offset[reg] = offset;
635         }
636       break;
637     case DW_CFA_restore_extended:
638       p = decode_uleb128 (p, &reg);
639       state->s.saved[reg] = REG_UNSAVED;
640       break;
641
642     case DW_CFA_undefined:
643     case DW_CFA_same_value:
644     case DW_CFA_nop:
645       break;
646
647     case DW_CFA_register:
648       {
649         unsigned reg2;
650         p = decode_uleb128 (p, &reg);
651         p = decode_uleb128 (p, &reg2);
652         state->s.saved[reg] = REG_SAVED_REG;
653         state->s.reg_or_offset[reg] = reg2;
654       }
655       break;
656
657     case DW_CFA_def_cfa:
658       p = decode_uleb128 (p, &reg);
659       p = decode_uleb128 (p, &offset);
660       state->s.cfa_reg = reg;
661       state->s.cfa_offset = offset;
662       break;
663     case DW_CFA_def_cfa_register:
664       p = decode_uleb128 (p, &reg);
665       state->s.cfa_reg = reg;
666       break;
667     case DW_CFA_def_cfa_offset:
668       p = decode_uleb128 (p, &offset);
669       state->s.cfa_offset = offset;
670       break;
671     case DW_CFA_def_cfa_expression:
672       {
673         void *end;
674         state->s.cfa_reg = 0;
675         state->s.cfa_offset = 0;
676         state->s.base_offset = 0;
677         state->s.indirect = 0;
678
679         p = decode_uleb128 (p, &offset);
680         end = p + offset;
681         while (p < end)
682           p = decode_stack_op (p, &(state->s));
683         break;
684       }
685       
686     case DW_CFA_remember_state:
687       {
688         struct frame_state_internal *save =
689           (struct frame_state_internal *)
690           malloc (sizeof (struct frame_state_internal));
691         memcpy (save, state, sizeof (struct frame_state_internal));
692         state->saved_state = save;
693       }
694       break;
695     case DW_CFA_restore_state:
696       {
697         struct frame_state_internal *save = state->saved_state;
698         memcpy (state, save, sizeof (struct frame_state_internal));
699         free (save);
700       }
701       break;
702
703       /* FIXME: Hardcoded for SPARC register window configuration.  */
704     case DW_CFA_GNU_window_save:
705       for (reg = 16; reg < 32; ++reg)
706         {
707           state->s.saved[reg] = REG_SAVED_OFFSET;
708           state->s.reg_or_offset[reg] = (reg - 16) * sizeof (void *);
709         }
710       break;
711
712     case DW_CFA_GNU_args_size:
713       p = decode_uleb128 (p, &offset);
714       state->s.args_size = offset;
715       break;
716
717     case DW_CFA_GNU_negative_offset_extended:
718       p = decode_uleb128 (p, &reg);
719       p = decode_uleb128 (p, &offset);
720       offset *= info->data_align;
721       state->s.saved[reg] = REG_SAVED_OFFSET;
722       state->s.reg_or_offset[reg] = -offset;
723       break;
724
725     default:
726       abort ();
727     }
728   return p;
729 }
730 \f
731 /* Called from __throw to find the registers to restore for a given
732    PC_TARGET.  The caller should allocate a local variable of `struct
733    frame_state' (declared in frame.h) and pass its address to STATE_IN.  */
734
735 struct frame_state *
736 __frame_state_for (void *pc_target, struct frame_state *state_in)
737 {
738   fde *f;
739   void *insn, *end, *pc;
740   struct cie_info info;
741   struct frame_state_internal state;
742
743   f = find_fde (pc_target);
744   if (f == 0)
745     return 0;
746
747   insn = extract_cie_info (f, &info);
748   if (insn == 0)
749     return 0;
750
751   memset (&state, 0, sizeof (state));
752   state.s.retaddr_column = info.ra_regno;
753   state.s.eh_ptr = info.eh_ptr;
754
755   /* First decode all the insns in the CIE.  */
756   end = next_fde ((fde*) get_cie (f));
757   while (insn < end)
758     insn = execute_cfa_insn (insn, &state, &info, 0);
759
760   insn = ((fde *)f) + 1;
761
762   if (info.augmentation[0] == 'z')
763     {
764       int i;
765       insn = decode_uleb128 (insn, &i);
766       insn += i;
767     }
768
769   /* Then the insns in the FDE up to our target PC.  */
770   end = next_fde (f);
771   pc = f->pc_begin;
772   while (insn < end && pc <= pc_target)
773     insn = execute_cfa_insn (insn, &state, &info, &pc);
774
775   memcpy (state_in, &state.s, sizeof (state.s));
776   return state_in;
777 }
778 #endif /* DWARF2_UNWIND_INFO */