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