OSDN Git Service

* dwarf2out.c (output_call_frame_info): The CIE pointer is now a 32
[pf3gnuchains/gcc-fork.git] / gcc / frame.c
1 /* Subroutines needed for unwinding stack frames for exception handling.  */
2 /* Compile this one with gcc.  */
3 /* Copyright (C) 1997 Free Software Foundation, Inc.
4    Contributed by Jason Merrill <jason@cygnus.com>.
5
6 This file is part of GNU CC.
7
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING.  If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.  */
22
23 /* As a special exception, if you link this library with other files,
24    some of which are compiled with GCC, to produce an executable,
25    this library does not by itself cause the resulting executable
26    to be covered by the GNU General Public License.
27    This exception does not however invalidate any other reasons why
28    the executable file might be covered by the GNU General Public License.  */
29
30 /* It is incorrect to include config.h here, because this file is being
31    compiled for the target, and hence definitions concerning only the host
32    do not apply.  */
33
34 #include "tconfig.h"
35 #include "defaults.h"
36
37 #ifdef DWARF2_UNWIND_INFO
38 #include "dwarf2.h"
39 #include "frame.h"
40 #include <stddef.h>
41
42 /* Don't use `fancy_abort' here even if config.h says to use it.  */
43 #ifdef abort
44 #undef abort
45 #endif
46
47 /* Some types used by the DWARF 2 spec.  */
48
49 typedef          int  sword __attribute__ ((mode (SI)));
50 typedef unsigned int  uword __attribute__ ((mode (SI)));
51 typedef unsigned int  uaddr __attribute__ ((mode (pointer)));
52 typedef          int  saddr __attribute__ ((mode (pointer)));
53 typedef unsigned char ubyte;
54
55 /* The first few fields of a CIE.  The CIE_id field is 0xffffffff for a CIE,
56    to distinguish it from a valid FDE.  FDEs are aligned to an addressing
57    unit boundary, but the fields within are unaligned.  */
58
59 struct dwarf_cie {
60   uword length;
61   sword CIE_id;
62   ubyte version;
63   char augmentation[0];
64 } __attribute__ ((packed, aligned (__alignof__ (void *))));
65
66 /* The first few fields of an FDE.  */
67
68 struct dwarf_fde {
69   uword length;
70   sword CIE_delta;
71   void* pc_begin;
72   uaddr pc_range;
73 } __attribute__ ((packed, aligned (__alignof__ (void *))));
74
75 typedef struct dwarf_fde fde;
76
77 /* The representation for an "object" to be searched for frame unwind info.
78    For targets with named sections, one object is an executable or shared
79    library; for other targets, one object is one translation unit.  */
80
81 struct object {
82   void *pc_begin;
83   void *pc_end;
84   fde *fde_begin;
85   fde ** fde_array;
86   size_t count;
87   struct object *next;
88 };
89
90 static struct object *objects;
91
92 /* The information we care about from a CIE.  */
93
94 struct cie_info {
95   char *augmentation;
96   void *eh_ptr;
97   int code_align;
98   int data_align;
99   unsigned ra_regno;
100 };
101
102 /* The current unwind state, plus a saved copy for DW_CFA_remember_state.  */
103
104 struct frame_state_internal
105 {
106   struct frame_state s;
107   struct frame_state_internal *saved_state;
108 };
109 \f  
110 /* Decode the unsigned LEB128 constant at BUF into the variable pointed to
111    by R, and return the new value of BUF.  */
112
113 static void *
114 decode_uleb128 (unsigned char *buf, unsigned *r)
115 {
116   unsigned shift = 0;
117   unsigned result = 0;
118
119   while (1)
120     {
121       unsigned byte = *buf++;
122       result |= (byte & 0x7f) << shift;
123       if ((byte & 0x80) == 0)
124         break;
125       shift += 7;
126     }
127   *r = result;
128   return buf;
129 }
130
131 /* Decode the signed LEB128 constant at BUF into the variable pointed to
132    by R, and return the new value of BUF.  */
133
134 static void *
135 decode_sleb128 (unsigned char *buf, int *r)
136 {
137   unsigned shift = 0;
138   unsigned result = 0;
139   unsigned byte;
140
141   while (1)
142     {
143       byte = *buf++;
144       result |= (byte & 0x7f) << shift;
145       shift += 7;
146       if ((byte & 0x80) == 0)
147         break;
148     }
149   if (shift < (sizeof (*r) * 8) && (byte & 0x40) != 0)
150     result |= - (1 << shift);
151
152   *r = result;
153   return buf;
154 }
155
156 /* Read unaligned data from the instruction buffer.  */
157
158 union unaligned {
159   void *p;
160   unsigned b2 __attribute__ ((mode (HI)));
161   unsigned b4 __attribute__ ((mode (SI)));
162   unsigned b8 __attribute__ ((mode (DI)));
163 } __attribute__ ((packed));
164 static inline void *
165 read_pointer (void *p)
166 { union unaligned *up = p; return up->p; }
167 static inline unsigned
168 read_1byte (void *p)
169 { return *(unsigned char *)p; }
170 static inline unsigned
171 read_2byte (void *p)
172 { union unaligned *up = p; return up->b2; }
173 static inline unsigned
174 read_4byte (void *p)
175 { union unaligned *up = p; return up->b4; }
176 static inline unsigned long
177 read_8byte (void *p)
178 { union unaligned *up = p; return up->b8; }
179 \f
180 /* Ordering function for FDEs.  Functions can't overlap, so we just compare
181    their starting addresses.  */
182
183 static inline saddr
184 fde_compare (fde *x, fde *y)
185 {
186   return (saddr)x->pc_begin - (saddr)y->pc_begin;
187 }
188
189 /* Return the address of the FDE after P.  */
190
191 static inline fde *
192 next_fde (fde *p)
193 {
194   return (fde *)(((char *)p) + p->length + sizeof (p->length));
195 }
196
197 /* One iteration of an insertion sort, for adding new FDEs to the array.
198    Usually the new FDE will go in at the end, so we can expect close to
199    O(n) performance.  If this turns out to be overly optimistic, we can have
200    the linker sort the FDEs so we don't have to do it at run time.  */
201
202 static void
203 fde_insert (fde **array, size_t i, fde *this_fde)
204 {
205   array[i] = this_fde;
206
207   for (; i > 0 && fde_compare (array[i], array[i-1]) < 0; --i)
208     {
209       this_fde = array[i];
210       array[i] = array[i-1];
211       array[i-1] = this_fde;
212     }
213 }
214
215 static size_t
216 count_fdes (fde *this_fde)
217 {
218   size_t count;
219
220   for (count = 0; this_fde->length != 0; this_fde = next_fde (this_fde))
221     {
222       /* Skip CIEs and linked once FDE entries.  */
223       if (this_fde->CIE_delta == 0 || this_fde->pc_range == 0)
224         continue;
225
226       ++count;
227     }
228
229   return count;
230 }
231
232 static void
233 add_fdes (fde *this_fde, fde **array, size_t *i_ptr,
234           void **beg_ptr, void **end_ptr)
235 {
236   size_t i = *i_ptr;
237   void *pc_begin = *beg_ptr;
238   void *pc_end = *end_ptr;
239
240   for (; this_fde->length != 0; this_fde = next_fde (this_fde))
241     {
242       /* Skip CIEs and linked once FDE entries.  */
243       if (this_fde->CIE_delta == 0 || this_fde->pc_range == 0)
244         continue;
245
246       fde_insert (array, i++, this_fde);
247
248       if (this_fde->pc_begin < pc_begin)
249         pc_begin = this_fde->pc_begin;
250       if (this_fde->pc_begin + this_fde->pc_range > pc_end)
251         pc_end = this_fde->pc_begin + this_fde->pc_range;
252     }
253
254   *i_ptr = i;
255   *beg_ptr = pc_begin;
256   *end_ptr = pc_end;
257 }
258
259 /* Set up a sorted array of pointers to FDEs for a loaded object.  We
260    count up the entries before allocating the array because it's likely to
261    be faster.  */
262
263 static void
264 frame_init (struct object* ob)
265 {
266   fde *this_fde;
267   size_t count;
268   fde **array;
269   void *pc_begin, *pc_end;
270
271   if (ob->fde_array)
272     {
273       fde **p = ob->fde_array;
274       for (count = 0; *p; ++p)
275         count += count_fdes (*p);
276     }
277   else
278     count = count_fdes (ob->fde_begin);
279
280   ob->count = count;
281   array = (fde **) malloc (sizeof (fde *) * count);
282
283   pc_begin = (void*)(uaddr)-1;
284   pc_end = 0;
285   count = 0;
286
287   if (ob->fde_array)
288     {
289       fde **p = ob->fde_array;
290       for (; *p; ++p)
291         add_fdes (*p, array, &count, &pc_begin, &pc_end);
292     }
293   else
294     add_fdes (ob->fde_begin, array, &count, &pc_begin, &pc_end);
295
296   ob->fde_array = array;
297   ob->pc_begin = pc_begin;
298   ob->pc_end = pc_end;
299 }
300
301 /* Return a pointer to the FDE for the function containing PC.  */
302
303 static fde *
304 find_fde (void *pc)
305 {
306   struct object *ob;
307   size_t lo, hi;
308
309   for (ob = objects; ob; ob = ob->next)
310     {
311       if (ob->pc_begin == 0)
312         frame_init (ob);
313       if (pc >= ob->pc_begin && pc < ob->pc_end)
314         break;
315     }
316
317   if (ob == 0)
318     return 0;
319
320   /* Standard binary search algorithm.  */
321   for (lo = 0, hi = ob->count; lo < hi; )
322     {
323       size_t i = (lo + hi) / 2;
324       fde *f = ob->fde_array[i];
325
326       if (pc < f->pc_begin)
327         hi = i;
328       else if (pc > f->pc_begin + f->pc_range)
329         lo = i + 1;
330       else
331         return f;
332     }
333
334   return 0;
335 }
336 \f
337 static inline struct dwarf_cie *
338 get_cie (fde *f)
339 {
340   return ((void *)&f->CIE_delta) - f->CIE_delta;
341 }
342
343 /* Extract any interesting information from the CIE for the translation
344    unit F belongs to.  */
345
346 static void *
347 extract_cie_info (fde *f, struct cie_info *c)
348 {
349   void *p;
350   int i;
351
352   c->augmentation = get_cie (f)->augmentation;
353
354   if (strcmp (c->augmentation, "") != 0
355       && strcmp (c->augmentation, "eh") != 0
356       && c->augmentation[0] != 'z')
357     return 0;
358
359   p = c->augmentation + strlen (c->augmentation) + 1;
360
361   if (strcmp (c->augmentation, "eh") == 0)
362     {
363       c->eh_ptr = read_pointer (p);
364       p += sizeof (void *);
365     }
366   else
367     c->eh_ptr = 0;
368
369   p = decode_uleb128 (p, &c->code_align);
370   p = decode_sleb128 (p, &c->data_align);
371   c->ra_regno = *(unsigned char *)p++;
372
373   /* If the augmentation starts with 'z', we now see the length of the
374      augmentation fields.  */
375   if (c->augmentation[0] == 'z')
376     {
377       p = decode_uleb128 (p, &i);
378       p += i;
379     }
380
381   return p;
382 }
383
384 /* Decode one instruction's worth of of DWARF 2 call frame information.
385    Used by __frame_state_for.  Takes pointers P to the instruction to
386    decode, STATE to the current register unwind information, INFO to the
387    current CIE information, and PC to the current PC value.  Returns a
388    pointer to the next instruction.  */
389
390 static void *
391 execute_cfa_insn (void *p, struct frame_state_internal *state,
392                   struct cie_info *info, void **pc)
393 {
394   unsigned insn = *(unsigned char *)p++;
395   unsigned reg;
396   int offset;
397
398   if (insn & DW_CFA_advance_loc)
399     *pc += ((insn & 0x3f) * info->code_align);
400   else if (insn & DW_CFA_offset)
401     {
402       reg = (insn & 0x3f);
403       p = decode_uleb128 (p, &offset);
404       offset *= info->data_align;
405       state->s.saved[reg] = REG_SAVED_OFFSET;
406       state->s.reg_or_offset[reg] = offset;
407     }
408   else if (insn & DW_CFA_restore)
409     {
410       reg = (insn & 0x3f);
411       state->s.saved[reg] = REG_UNSAVED;
412     }
413   else switch (insn)
414     {
415     case DW_CFA_set_loc:
416       *pc = read_pointer (p);
417       p += sizeof (void *);
418       break;
419     case DW_CFA_advance_loc1:
420       *pc += read_1byte (p);
421       p += 1;
422       break;
423     case DW_CFA_advance_loc2:
424       *pc += read_2byte (p);
425       p += 2;
426       break;
427     case DW_CFA_advance_loc4:
428       *pc += read_4byte (p);
429       p += 4;
430       break;
431
432     case DW_CFA_offset_extended:
433       p = decode_uleb128 (p, &reg);
434       p = decode_uleb128 (p, &offset);
435       offset *= info->data_align;
436       state->s.saved[reg] = REG_SAVED_OFFSET;
437       state->s.reg_or_offset[reg] = offset;
438       break;
439     case DW_CFA_restore_extended:
440       p = decode_uleb128 (p, &reg);
441       state->s.saved[reg] = REG_UNSAVED;
442       break;
443
444     case DW_CFA_undefined:
445     case DW_CFA_same_value:
446     case DW_CFA_nop:
447       break;
448
449     case DW_CFA_register:
450       {
451         unsigned reg2;
452         p = decode_uleb128 (p, &reg);
453         p = decode_uleb128 (p, &reg2);
454         state->s.saved[reg] = REG_SAVED_REG;
455         state->s.reg_or_offset[reg] = reg2;
456       }
457       break;
458
459     case DW_CFA_def_cfa:
460       p = decode_uleb128 (p, &reg);
461       p = decode_uleb128 (p, &offset);
462       state->s.cfa_reg = reg;
463       state->s.cfa_offset = offset;
464       break;
465     case DW_CFA_def_cfa_register:
466       p = decode_uleb128 (p, &reg);
467       state->s.cfa_reg = reg;
468       break;
469     case DW_CFA_def_cfa_offset:
470       p = decode_uleb128 (p, &offset);
471       state->s.cfa_offset = offset;
472       break;
473       
474     case DW_CFA_remember_state:
475       {
476         struct frame_state_internal *save =
477           (struct frame_state_internal *)
478           malloc (sizeof (struct frame_state_internal));
479         memcpy (save, state, sizeof (struct frame_state_internal));
480         state->saved_state = save;
481       }
482       break;
483     case DW_CFA_restore_state:
484       {
485         struct frame_state_internal *save = state->saved_state;
486         memcpy (state, save, sizeof (struct frame_state_internal));
487         free (save);
488       }
489       break;
490
491       /* FIXME: Hardcoded for SPARC register window configuration.  */
492     case DW_CFA_GNU_window_save:
493       for (reg = 16; reg < 32; ++reg)
494         {
495           state->s.saved[reg] = REG_SAVED_OFFSET;
496           state->s.reg_or_offset[reg] = (reg - 16) * sizeof (void *);
497         }
498       break;
499
500     case DW_CFA_GNU_args_size:
501       p = decode_uleb128 (p, &offset);
502       state->s.args_size = offset;
503       break;
504
505     default:
506       abort ();
507     }
508   return p;
509 }
510 \f
511 /* Called from crtbegin.o to register the unwind info for an object.  */
512
513 void
514 __register_frame (void *begin)
515 {
516   struct object *ob = (struct object *) malloc (sizeof (struct object));
517
518   ob->fde_begin = begin;
519
520   ob->pc_begin = ob->pc_end = 0;
521   ob->fde_array = 0;
522   ob->count = 0;
523
524   ob->next = objects;
525   objects = ob;
526 }
527
528 /* Similar, but BEGIN is actually a pointer to a table of unwind entries
529    for different translation units.  Called from the file generated by
530    collect2.  */
531
532 void
533 __register_frame_table (void *begin)
534 {
535   struct object *ob = (struct object *) malloc (sizeof (struct object));
536
537   ob->fde_begin = begin;
538   ob->fde_array = begin;
539
540   ob->pc_begin = ob->pc_end = 0;
541   ob->count = 0;
542
543   ob->next = objects;
544   objects = ob;
545 }
546
547 /* Called from crtend.o to deregister the unwind info for an object.  */
548
549 void
550 __deregister_frame (void *begin)
551 {
552   struct object **p = &objects;
553
554   while (*p)
555     {
556       if ((*p)->fde_begin == begin)
557         {
558           struct object *ob = *p;
559           *p = (*p)->next;
560
561           /* If we've run init_frame for this object, free the FDE array.  */
562           if (ob->pc_begin)
563             free (ob->fde_array);
564           free (ob);
565
566           return;
567         }
568       p = &((*p)->next);
569     }
570   abort ();
571 }
572
573 /* Called from __throw to find the registers to restore for a given
574    PC_TARGET.  The caller should allocate a local variable of `struct
575    frame_state' (declared in frame.h) and pass its address to STATE_IN.  */
576
577 struct frame_state *
578 __frame_state_for (void *pc_target, struct frame_state *state_in)
579 {
580   fde *f;
581   void *insn, *end, *pc;
582   struct cie_info info;
583   struct frame_state_internal state;
584
585   f = find_fde (pc_target);
586   if (f == 0)
587     return 0;
588
589   insn = extract_cie_info (f, &info);
590   if (insn == 0)
591     return 0;
592
593   memset (&state, 0, sizeof (state));
594   state.s.retaddr_column = info.ra_regno;
595   state.s.eh_ptr = info.eh_ptr;
596
597   /* First decode all the insns in the CIE.  */
598   end = next_fde ((fde*) get_cie (f));
599   while (insn < end)
600     insn = execute_cfa_insn (insn, &state, &info, 0);
601
602   insn = ((fde *)f) + 1;
603
604   if (info.augmentation[0] == 'z')
605     {
606       int i;
607       insn = decode_uleb128 (insn, &i);
608       insn += i;
609     }
610
611   /* Then the insns in the FDE up to our target PC.  */
612   end = next_fde (f);
613   pc = f->pc_begin;
614   while (insn < end && pc <= pc_target)
615     insn = execute_cfa_insn (insn, &state, &info, &pc);
616
617   memcpy (state_in, &state.s, sizeof (state.s));
618   return state_in;
619 }
620 #endif /* DWARF2_UNWIND_INFO */