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