* README-vms: Delete.
[deliverable/binutils-gdb.git] / gas / dw2gencfi.c
1 /* dw2gencfi.c - Support for generating Dwarf2 CFI information.
2 Copyright 2003, 2004, 2005 Free Software Foundation, Inc.
3 Contributed by Michal Ludvig <mludvig@suse.cz>
4
5 This file is part of GAS, the GNU Assembler.
6
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
20 02110-1301, USA. */
21
22 #include "as.h"
23 #include "dw2gencfi.h"
24
25
26 /* We re-use DWARF2_LINE_MIN_INSN_LENGTH for the code alignment field
27 of the CIE. Default to 1 if not otherwise specified. */
28 #ifndef DWARF2_LINE_MIN_INSN_LENGTH
29 # define DWARF2_LINE_MIN_INSN_LENGTH 1
30 #endif
31
32 /* If TARGET_USE_CFIPOP is defined, it is required that the target
33 provide the following definitions. Otherwise provide them to
34 allow compilation to continue. */
35 #ifndef TARGET_USE_CFIPOP
36 # ifndef DWARF2_DEFAULT_RETURN_COLUMN
37 # define DWARF2_DEFAULT_RETURN_COLUMN 0
38 # endif
39 # ifndef DWARF2_CIE_DATA_ALIGNMENT
40 # define DWARF2_CIE_DATA_ALIGNMENT 1
41 # endif
42 #endif
43
44 #ifndef EH_FRAME_ALIGNMENT
45 # define EH_FRAME_ALIGNMENT (bfd_get_arch_size (stdoutput) == 64 ? 3 : 2)
46 #endif
47
48 #ifndef tc_cfi_frame_initial_instructions
49 # define tc_cfi_frame_initial_instructions() ((void)0)
50 #endif
51
52
53 struct cfi_insn_data
54 {
55 struct cfi_insn_data *next;
56 int insn;
57 union {
58 struct {
59 unsigned reg;
60 offsetT offset;
61 } ri;
62
63 struct {
64 unsigned reg1;
65 unsigned reg2;
66 } rr;
67
68 unsigned r;
69 offsetT i;
70
71 struct {
72 symbolS *lab1;
73 symbolS *lab2;
74 } ll;
75
76 struct cfi_escape_data {
77 struct cfi_escape_data *next;
78 expressionS exp;
79 } *esc;
80 } u;
81 };
82
83 struct fde_entry
84 {
85 struct fde_entry *next;
86 symbolS *start_address;
87 symbolS *end_address;
88 struct cfi_insn_data *data;
89 struct cfi_insn_data **last;
90 unsigned int return_column;
91 };
92
93 struct cie_entry
94 {
95 struct cie_entry *next;
96 symbolS *start_address;
97 unsigned int return_column;
98 struct cfi_insn_data *first, *last;
99 };
100
101
102 /* Current open FDE entry. */
103 static struct fde_entry *cur_fde_data;
104 static symbolS *last_address;
105 static offsetT cur_cfa_offset;
106
107 /* List of FDE entries. */
108 static struct fde_entry *all_fde_data;
109 static struct fde_entry **last_fde_data = &all_fde_data;
110
111 /* List of CIEs so that they could be reused. */
112 static struct cie_entry *cie_root;
113
114 /* Stack of old CFI data, for save/restore. */
115 struct cfa_save_data
116 {
117 struct cfa_save_data *next;
118 offsetT cfa_offset;
119 };
120
121 static struct cfa_save_data *cfa_save_stack;
122 \f
123 /* Construct a new FDE structure and add it to the end of the fde list. */
124
125 static struct fde_entry *
126 alloc_fde_entry (void)
127 {
128 struct fde_entry *fde = xcalloc (1, sizeof (struct fde_entry));
129
130 cur_fde_data = fde;
131 *last_fde_data = fde;
132 last_fde_data = &fde->next;
133
134 fde->last = &fde->data;
135 fde->return_column = DWARF2_DEFAULT_RETURN_COLUMN;
136
137 return fde;
138 }
139
140 /* The following functions are available for a backend to construct its
141 own unwind information, usually from legacy unwind directives. */
142
143 /* Construct a new INSN structure and add it to the end of the insn list
144 for the currently active FDE. */
145
146 static struct cfi_insn_data *
147 alloc_cfi_insn_data (void)
148 {
149 struct cfi_insn_data *insn = xcalloc (1, sizeof (struct cfi_insn_data));
150
151 *cur_fde_data->last = insn;
152 cur_fde_data->last = &insn->next;
153
154 return insn;
155 }
156
157 /* Construct a new FDE structure that begins at LABEL. */
158
159 void
160 cfi_new_fde (symbolS *label)
161 {
162 struct fde_entry *fde = alloc_fde_entry ();
163 fde->start_address = label;
164 last_address = label;
165 }
166
167 /* End the currently open FDE. */
168
169 void
170 cfi_end_fde (symbolS *label)
171 {
172 cur_fde_data->end_address = label;
173 cur_fde_data = NULL;
174 }
175
176 /* Set the return column for the current FDE. */
177
178 void
179 cfi_set_return_column (unsigned regno)
180 {
181 cur_fde_data->return_column = regno;
182 }
183
184 /* Universal functions to store new instructions. */
185
186 static void
187 cfi_add_CFA_insn(int insn)
188 {
189 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
190
191 insn_ptr->insn = insn;
192 }
193
194 static void
195 cfi_add_CFA_insn_reg (int insn, unsigned regno)
196 {
197 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
198
199 insn_ptr->insn = insn;
200 insn_ptr->u.r = regno;
201 }
202
203 static void
204 cfi_add_CFA_insn_offset (int insn, offsetT offset)
205 {
206 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
207
208 insn_ptr->insn = insn;
209 insn_ptr->u.i = offset;
210 }
211
212 static void
213 cfi_add_CFA_insn_reg_reg (int insn, unsigned reg1, unsigned reg2)
214 {
215 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
216
217 insn_ptr->insn = insn;
218 insn_ptr->u.rr.reg1 = reg1;
219 insn_ptr->u.rr.reg2 = reg2;
220 }
221
222 static void
223 cfi_add_CFA_insn_reg_offset (int insn, unsigned regno, offsetT offset)
224 {
225 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
226
227 insn_ptr->insn = insn;
228 insn_ptr->u.ri.reg = regno;
229 insn_ptr->u.ri.offset = offset;
230 }
231
232 /* Add a CFI insn to advance the PC from the last address to LABEL. */
233
234 void
235 cfi_add_advance_loc (symbolS *label)
236 {
237 struct cfi_insn_data *insn = alloc_cfi_insn_data ();
238
239 insn->insn = DW_CFA_advance_loc;
240 insn->u.ll.lab1 = last_address;
241 insn->u.ll.lab2 = label;
242
243 last_address = label;
244 }
245
246 /* Add a DW_CFA_offset record to the CFI data. */
247
248 void
249 cfi_add_CFA_offset (unsigned regno, offsetT offset)
250 {
251 unsigned int abs_data_align;
252
253 cfi_add_CFA_insn_reg_offset (DW_CFA_offset, regno, offset);
254
255 abs_data_align = (DWARF2_CIE_DATA_ALIGNMENT < 0
256 ? -DWARF2_CIE_DATA_ALIGNMENT : DWARF2_CIE_DATA_ALIGNMENT);
257 if (offset % abs_data_align)
258 as_bad (_("register save offset not a multiple of %u"), abs_data_align);
259 }
260
261 /* Add a DW_CFA_def_cfa record to the CFI data. */
262
263 void
264 cfi_add_CFA_def_cfa (unsigned regno, offsetT offset)
265 {
266 cfi_add_CFA_insn_reg_offset (DW_CFA_def_cfa, regno, offset);
267 cur_cfa_offset = offset;
268 }
269
270 /* Add a DW_CFA_register record to the CFI data. */
271
272 void
273 cfi_add_CFA_register (unsigned reg1, unsigned reg2)
274 {
275 cfi_add_CFA_insn_reg_reg (DW_CFA_register, reg1, reg2);
276 }
277
278 /* Add a DW_CFA_def_cfa_register record to the CFI data. */
279
280 void
281 cfi_add_CFA_def_cfa_register (unsigned regno)
282 {
283 cfi_add_CFA_insn_reg (DW_CFA_def_cfa_register, regno);
284 }
285
286 /* Add a DW_CFA_def_cfa_offset record to the CFI data. */
287
288 void
289 cfi_add_CFA_def_cfa_offset (offsetT offset)
290 {
291 cfi_add_CFA_insn_offset (DW_CFA_def_cfa_offset, offset);
292 cur_cfa_offset = offset;
293 }
294
295 void
296 cfi_add_CFA_restore (unsigned regno)
297 {
298 cfi_add_CFA_insn_reg (DW_CFA_restore, regno);
299 }
300
301 void
302 cfi_add_CFA_undefined (unsigned regno)
303 {
304 cfi_add_CFA_insn_reg (DW_CFA_undefined, regno);
305 }
306
307 void
308 cfi_add_CFA_same_value (unsigned regno)
309 {
310 cfi_add_CFA_insn_reg (DW_CFA_same_value, regno);
311 }
312
313 void
314 cfi_add_CFA_remember_state (void)
315 {
316 struct cfa_save_data *p;
317
318 cfi_add_CFA_insn (DW_CFA_remember_state);
319
320 p = xmalloc (sizeof (*p));
321 p->cfa_offset = cur_cfa_offset;
322 p->next = cfa_save_stack;
323 cfa_save_stack = p;
324 }
325
326 void
327 cfi_add_CFA_restore_state (void)
328 {
329 struct cfa_save_data *p;
330
331 cfi_add_CFA_insn (DW_CFA_restore_state);
332
333 p = cfa_save_stack;
334 if (p)
335 {
336 cur_cfa_offset = p->cfa_offset;
337 cfa_save_stack = p->next;
338 free (p);
339 }
340 else
341 as_bad (_("CFI state restore without previous remember"));
342 }
343
344 \f
345 /* Parse CFI assembler directives. */
346
347 static void dot_cfi (int);
348 static void dot_cfi_escape (int);
349 static void dot_cfi_startproc (int);
350 static void dot_cfi_endproc (int);
351
352 /* Fake CFI type; outside the byte range of any real CFI insn. */
353 #define CFI_adjust_cfa_offset 0x100
354 #define CFI_return_column 0x101
355 #define CFI_rel_offset 0x102
356 #define CFI_escape 0x103
357
358 const pseudo_typeS cfi_pseudo_table[] =
359 {
360 { "cfi_startproc", dot_cfi_startproc, 0 },
361 { "cfi_endproc", dot_cfi_endproc, 0 },
362 { "cfi_def_cfa", dot_cfi, DW_CFA_def_cfa },
363 { "cfi_def_cfa_register", dot_cfi, DW_CFA_def_cfa_register },
364 { "cfi_def_cfa_offset", dot_cfi, DW_CFA_def_cfa_offset },
365 { "cfi_adjust_cfa_offset", dot_cfi, CFI_adjust_cfa_offset },
366 { "cfi_offset", dot_cfi, DW_CFA_offset },
367 { "cfi_rel_offset", dot_cfi, CFI_rel_offset },
368 { "cfi_register", dot_cfi, DW_CFA_register },
369 { "cfi_return_column", dot_cfi, CFI_return_column },
370 { "cfi_restore", dot_cfi, DW_CFA_restore },
371 { "cfi_undefined", dot_cfi, DW_CFA_undefined },
372 { "cfi_same_value", dot_cfi, DW_CFA_same_value },
373 { "cfi_remember_state", dot_cfi, DW_CFA_remember_state },
374 { "cfi_restore_state", dot_cfi, DW_CFA_restore_state },
375 { "cfi_window_save", dot_cfi, DW_CFA_GNU_window_save },
376 { "cfi_escape", dot_cfi_escape, 0 },
377 { NULL, NULL, 0 }
378 };
379
380 static void
381 cfi_parse_separator (void)
382 {
383 SKIP_WHITESPACE ();
384 if (*input_line_pointer == ',')
385 input_line_pointer++;
386 else
387 as_bad (_("missing separator"));
388 }
389
390 static unsigned
391 cfi_parse_reg (void)
392 {
393 int regno;
394 expressionS exp;
395
396 #ifdef tc_regname_to_dw2regnum
397 SKIP_WHITESPACE ();
398 if (is_name_beginner (*input_line_pointer)
399 || (*input_line_pointer == '%'
400 && is_name_beginner (*++input_line_pointer)))
401 {
402 char *name, c;
403
404 name = input_line_pointer;
405 c = get_symbol_end ();
406
407 if ((regno = tc_regname_to_dw2regnum (name)) < 0)
408 {
409 as_bad (_("bad register expression"));
410 regno = 0;
411 }
412
413 *input_line_pointer = c;
414 return regno;
415 }
416 #endif
417
418 expression (&exp);
419 switch (exp.X_op)
420 {
421 case O_register:
422 case O_constant:
423 regno = exp.X_add_number;
424 break;
425
426 default:
427 as_bad (_("bad register expression"));
428 regno = 0;
429 break;
430 }
431
432 return regno;
433 }
434
435 static offsetT
436 cfi_parse_const (void)
437 {
438 return get_absolute_expression ();
439 }
440
441 static void
442 dot_cfi (int arg)
443 {
444 offsetT offset;
445 unsigned reg1, reg2;
446
447 if (!cur_fde_data)
448 {
449 as_bad (_("CFI instruction used without previous .cfi_startproc"));
450 return;
451 }
452
453 /* If the last address was not at the current PC, advance to current. */
454 if (symbol_get_frag (last_address) != frag_now
455 || S_GET_VALUE (last_address) != frag_now_fix ())
456 cfi_add_advance_loc (symbol_temp_new_now ());
457
458 switch (arg)
459 {
460 case DW_CFA_offset:
461 reg1 = cfi_parse_reg ();
462 cfi_parse_separator ();
463 offset = cfi_parse_const ();
464 cfi_add_CFA_offset (reg1, offset);
465 break;
466
467 case CFI_rel_offset:
468 reg1 = cfi_parse_reg ();
469 cfi_parse_separator ();
470 offset = cfi_parse_const ();
471 cfi_add_CFA_offset (reg1, offset - cur_cfa_offset);
472 break;
473
474 case DW_CFA_def_cfa:
475 reg1 = cfi_parse_reg ();
476 cfi_parse_separator ();
477 offset = cfi_parse_const ();
478 cfi_add_CFA_def_cfa (reg1, offset);
479 break;
480
481 case DW_CFA_register:
482 reg1 = cfi_parse_reg ();
483 cfi_parse_separator ();
484 reg2 = cfi_parse_reg ();
485 cfi_add_CFA_register (reg1, reg2);
486 break;
487
488 case DW_CFA_def_cfa_register:
489 reg1 = cfi_parse_reg ();
490 cfi_add_CFA_def_cfa_register (reg1);
491 break;
492
493 case DW_CFA_def_cfa_offset:
494 offset = cfi_parse_const ();
495 cfi_add_CFA_def_cfa_offset (offset);
496 break;
497
498 case CFI_adjust_cfa_offset:
499 offset = cfi_parse_const ();
500 cfi_add_CFA_def_cfa_offset (cur_cfa_offset + offset);
501 break;
502
503 case DW_CFA_restore:
504 reg1 = cfi_parse_reg ();
505 cfi_add_CFA_restore (reg1);
506 break;
507
508 case DW_CFA_undefined:
509 reg1 = cfi_parse_reg ();
510 cfi_add_CFA_undefined (reg1);
511 break;
512
513 case DW_CFA_same_value:
514 reg1 = cfi_parse_reg ();
515 cfi_add_CFA_same_value (reg1);
516 break;
517
518 case CFI_return_column:
519 reg1 = cfi_parse_reg ();
520 cfi_set_return_column (reg1);
521 break;
522
523 case DW_CFA_remember_state:
524 cfi_add_CFA_remember_state ();
525 break;
526
527 case DW_CFA_restore_state:
528 cfi_add_CFA_restore_state ();
529 break;
530
531 case DW_CFA_GNU_window_save:
532 cfi_add_CFA_insn (DW_CFA_GNU_window_save);
533 break;
534
535 default:
536 abort ();
537 }
538
539 demand_empty_rest_of_line ();
540 }
541
542 static void
543 dot_cfi_escape (int ignored ATTRIBUTE_UNUSED)
544 {
545 struct cfi_escape_data *head, **tail, *e;
546 struct cfi_insn_data *insn;
547
548 if (!cur_fde_data)
549 {
550 as_bad (_("CFI instruction used without previous .cfi_startproc"));
551 return;
552 }
553
554 /* If the last address was not at the current PC, advance to current. */
555 if (symbol_get_frag (last_address) != frag_now
556 || S_GET_VALUE (last_address) != frag_now_fix ())
557 cfi_add_advance_loc (symbol_temp_new_now ());
558
559 tail = &head;
560 do
561 {
562 e = xmalloc (sizeof (*e));
563 do_parse_cons_expression (&e->exp, 1);
564 *tail = e;
565 tail = &e->next;
566 }
567 while (*input_line_pointer++ == ',');
568 *tail = NULL;
569
570 insn = alloc_cfi_insn_data ();
571 insn->insn = CFI_escape;
572 insn->u.esc = head;
573 }
574
575 static void
576 dot_cfi_startproc (int ignored ATTRIBUTE_UNUSED)
577 {
578 int simple = 0;
579
580 if (cur_fde_data)
581 {
582 as_bad (_("previous CFI entry not closed (missing .cfi_endproc)"));
583 return;
584 }
585
586 cfi_new_fde (symbol_temp_new_now ());
587
588 SKIP_WHITESPACE ();
589 if (is_name_beginner (*input_line_pointer))
590 {
591 char *name, c;
592
593 name = input_line_pointer;
594 c = get_symbol_end ();
595
596 if (strcmp (name, "simple") == 0)
597 {
598 simple = 1;
599 *input_line_pointer = c;
600 }
601 else
602 input_line_pointer = name;
603 }
604 demand_empty_rest_of_line ();
605
606 cur_cfa_offset = 0;
607 if (!simple)
608 tc_cfi_frame_initial_instructions ();
609 }
610
611 static void
612 dot_cfi_endproc (int ignored ATTRIBUTE_UNUSED)
613 {
614 if (! cur_fde_data)
615 {
616 as_bad (_(".cfi_endproc without corresponding .cfi_startproc"));
617 return;
618 }
619
620 cfi_end_fde (symbol_temp_new_now ());
621 }
622
623 \f
624 /* Emit a single byte into the current segment. */
625
626 static inline void
627 out_one (int byte)
628 {
629 FRAG_APPEND_1_CHAR (byte);
630 }
631
632 /* Emit a two-byte word into the current segment. */
633
634 static inline void
635 out_two (int data)
636 {
637 md_number_to_chars (frag_more (2), data, 2);
638 }
639
640 /* Emit a four byte word into the current segment. */
641
642 static inline void
643 out_four (int data)
644 {
645 md_number_to_chars (frag_more (4), data, 4);
646 }
647
648 /* Emit an unsigned "little-endian base 128" number. */
649
650 static void
651 out_uleb128 (addressT value)
652 {
653 output_leb128 (frag_more (sizeof_leb128 (value, 0)), value, 0);
654 }
655
656 /* Emit an unsigned "little-endian base 128" number. */
657
658 static void
659 out_sleb128 (offsetT value)
660 {
661 output_leb128 (frag_more (sizeof_leb128 (value, 1)), value, 1);
662 }
663
664 static void
665 output_cfi_insn (struct cfi_insn_data *insn)
666 {
667 offsetT offset;
668 unsigned int regno;
669
670 switch (insn->insn)
671 {
672 case DW_CFA_advance_loc:
673 {
674 symbolS *from = insn->u.ll.lab1;
675 symbolS *to = insn->u.ll.lab2;
676
677 if (symbol_get_frag (to) == symbol_get_frag (from))
678 {
679 addressT delta = S_GET_VALUE (to) - S_GET_VALUE (from);
680 addressT scaled = delta / DWARF2_LINE_MIN_INSN_LENGTH;
681
682 if (scaled <= 0x3F)
683 out_one (DW_CFA_advance_loc + scaled);
684 else if (delta <= 0xFF)
685 {
686 out_one (DW_CFA_advance_loc1);
687 out_one (delta);
688 }
689 else if (delta <= 0xFFFF)
690 {
691 out_one (DW_CFA_advance_loc2);
692 out_two (delta);
693 }
694 else
695 {
696 out_one (DW_CFA_advance_loc4);
697 out_four (delta);
698 }
699 }
700 else
701 {
702 expressionS exp;
703
704 exp.X_op = O_subtract;
705 exp.X_add_symbol = to;
706 exp.X_op_symbol = from;
707 exp.X_add_number = 0;
708
709 /* The code in ehopt.c expects that one byte of the encoding
710 is already allocated to the frag. This comes from the way
711 that it scans the .eh_frame section looking first for the
712 .byte DW_CFA_advance_loc4. */
713 frag_more (1);
714
715 frag_var (rs_cfa, 4, 0, DWARF2_LINE_MIN_INSN_LENGTH << 3,
716 make_expr_symbol (&exp), frag_now_fix () - 1,
717 (char *) frag_now);
718 }
719 }
720 break;
721
722 case DW_CFA_def_cfa:
723 offset = insn->u.ri.offset;
724 if (offset < 0)
725 {
726 out_one (DW_CFA_def_cfa_sf);
727 out_uleb128 (insn->u.ri.reg);
728 out_sleb128 (offset / DWARF2_CIE_DATA_ALIGNMENT);
729 }
730 else
731 {
732 out_one (DW_CFA_def_cfa);
733 out_uleb128 (insn->u.ri.reg);
734 out_uleb128 (offset);
735 }
736 break;
737
738 case DW_CFA_def_cfa_register:
739 case DW_CFA_undefined:
740 case DW_CFA_same_value:
741 out_one (insn->insn);
742 out_uleb128 (insn->u.r);
743 break;
744
745 case DW_CFA_def_cfa_offset:
746 offset = insn->u.i;
747 if (offset < 0)
748 {
749 out_one (DW_CFA_def_cfa_offset_sf);
750 out_sleb128 (offset / DWARF2_CIE_DATA_ALIGNMENT);
751 }
752 else
753 {
754 out_one (DW_CFA_def_cfa_offset);
755 out_uleb128 (offset);
756 }
757 break;
758
759 case DW_CFA_restore:
760 regno = insn->u.r;
761 if (regno <= 0x3F)
762 {
763 out_one (DW_CFA_restore + regno);
764 }
765 else
766 {
767 out_one (DW_CFA_restore_extended);
768 out_uleb128 (regno);
769 }
770 break;
771
772 case DW_CFA_offset:
773 regno = insn->u.ri.reg;
774 offset = insn->u.ri.offset / DWARF2_CIE_DATA_ALIGNMENT;
775 if (offset < 0)
776 {
777 out_one (DW_CFA_offset_extended_sf);
778 out_uleb128 (regno);
779 out_sleb128 (offset);
780 }
781 else if (regno <= 0x3F)
782 {
783 out_one (DW_CFA_offset + regno);
784 out_uleb128 (offset);
785 }
786 else
787 {
788 out_one (DW_CFA_offset_extended);
789 out_uleb128 (regno);
790 out_uleb128 (offset);
791 }
792 break;
793
794 case DW_CFA_register:
795 out_one (DW_CFA_register);
796 out_uleb128 (insn->u.rr.reg1);
797 out_uleb128 (insn->u.rr.reg2);
798 break;
799
800 case DW_CFA_remember_state:
801 case DW_CFA_restore_state:
802 out_one (insn->insn);
803 break;
804
805 case DW_CFA_GNU_window_save:
806 out_one (DW_CFA_GNU_window_save);
807 break;
808
809 case CFI_escape:
810 {
811 struct cfi_escape_data *e;
812 for (e = insn->u.esc; e ; e = e->next)
813 emit_expr (&e->exp, 1);
814 break;
815 }
816
817 default:
818 abort ();
819 }
820 }
821
822 static void
823 output_cie (struct cie_entry *cie)
824 {
825 symbolS *after_size_address, *end_address;
826 expressionS exp;
827 struct cfi_insn_data *i;
828
829 cie->start_address = symbol_temp_new_now ();
830 after_size_address = symbol_temp_make ();
831 end_address = symbol_temp_make ();
832
833 exp.X_op = O_subtract;
834 exp.X_add_symbol = end_address;
835 exp.X_op_symbol = after_size_address;
836 exp.X_add_number = 0;
837
838 emit_expr (&exp, 4); /* Length. */
839 symbol_set_value_now (after_size_address);
840 out_four (0); /* CIE id. */
841 out_one (DW_CIE_VERSION); /* Version. */
842 out_one ('z'); /* Augmentation. */
843 out_one ('R');
844 out_one (0);
845 out_uleb128 (DWARF2_LINE_MIN_INSN_LENGTH); /* Code alignment. */
846 out_sleb128 (DWARF2_CIE_DATA_ALIGNMENT); /* Data alignment. */
847 if (DW_CIE_VERSION == 1) /* Return column. */
848 out_one (cie->return_column);
849 else
850 out_uleb128 (cie->return_column);
851 out_uleb128 (1); /* Augmentation size. */
852 #if defined DIFF_EXPR_OK || defined tc_cfi_emit_pcrel_expr
853 out_one (DW_EH_PE_pcrel | DW_EH_PE_sdata4);
854 #else
855 out_one (DW_EH_PE_sdata4);
856 #endif
857
858 if (cie->first)
859 for (i = cie->first; i != cie->last; i = i->next)
860 output_cfi_insn (i);
861
862 frag_align (2, DW_CFA_nop, 0);
863 symbol_set_value_now (end_address);
864 }
865
866 static void
867 output_fde (struct fde_entry *fde, struct cie_entry *cie,
868 struct cfi_insn_data *first, int align)
869 {
870 symbolS *after_size_address, *end_address;
871 expressionS exp;
872
873 after_size_address = symbol_temp_make ();
874 end_address = symbol_temp_make ();
875
876 exp.X_op = O_subtract;
877 exp.X_add_symbol = end_address;
878 exp.X_op_symbol = after_size_address;
879 exp.X_add_number = 0;
880 emit_expr (&exp, 4); /* Length. */
881 symbol_set_value_now (after_size_address);
882
883 exp.X_add_symbol = after_size_address;
884 exp.X_op_symbol = cie->start_address;
885 emit_expr (&exp, 4); /* CIE offset. */
886
887 #ifdef DIFF_EXPR_OK
888 exp.X_add_symbol = fde->start_address;
889 exp.X_op_symbol = symbol_temp_new_now ();
890 emit_expr (&exp, 4); /* Code offset. */
891 #else
892 exp.X_op = O_symbol;
893 exp.X_add_symbol = fde->start_address;
894 exp.X_op_symbol = NULL;
895 #ifdef tc_cfi_emit_pcrel_expr
896 tc_cfi_emit_pcrel_expr (&exp, 4); /* Code offset. */
897 #else
898 emit_expr (&exp, 4); /* Code offset. */
899 #endif
900 exp.X_op = O_subtract;
901 #endif
902
903 exp.X_add_symbol = fde->end_address;
904 exp.X_op_symbol = fde->start_address; /* Code length. */
905 emit_expr (&exp, 4);
906
907 out_uleb128 (0); /* Augmentation size. */
908
909 for (; first; first = first->next)
910 output_cfi_insn (first);
911
912 frag_align (align, DW_CFA_nop, 0);
913 symbol_set_value_now (end_address);
914 }
915
916 static struct cie_entry *
917 select_cie_for_fde (struct fde_entry *fde, struct cfi_insn_data **pfirst)
918 {
919 struct cfi_insn_data *i, *j;
920 struct cie_entry *cie;
921
922 for (cie = cie_root; cie; cie = cie->next)
923 {
924 if (cie->return_column != fde->return_column)
925 continue;
926 for (i = cie->first, j = fde->data;
927 i != cie->last && j != NULL;
928 i = i->next, j = j->next)
929 {
930 if (i->insn != j->insn)
931 goto fail;
932 switch (i->insn)
933 {
934 case DW_CFA_advance_loc:
935 case DW_CFA_remember_state:
936 /* We reached the first advance/remember in the FDE,
937 but did not reach the end of the CIE list. */
938 goto fail;
939
940 case DW_CFA_offset:
941 case DW_CFA_def_cfa:
942 if (i->u.ri.reg != j->u.ri.reg)
943 goto fail;
944 if (i->u.ri.offset != j->u.ri.offset)
945 goto fail;
946 break;
947
948 case DW_CFA_register:
949 if (i->u.rr.reg1 != j->u.rr.reg1)
950 goto fail;
951 if (i->u.rr.reg2 != j->u.rr.reg2)
952 goto fail;
953 break;
954
955 case DW_CFA_def_cfa_register:
956 case DW_CFA_restore:
957 case DW_CFA_undefined:
958 case DW_CFA_same_value:
959 if (i->u.r != j->u.r)
960 goto fail;
961 break;
962
963 case DW_CFA_def_cfa_offset:
964 if (i->u.i != j->u.i)
965 goto fail;
966 break;
967
968 case CFI_escape:
969 /* Don't bother matching these for now. */
970 goto fail;
971
972 default:
973 abort ();
974 }
975 }
976
977 /* Success if we reached the end of the CIE list, and we've either
978 run out of FDE entries or we've encountered an advance,
979 remember, or escape. */
980 if (i == cie->last
981 && (!j
982 || j->insn == DW_CFA_advance_loc
983 || j->insn == DW_CFA_remember_state
984 || j->insn == CFI_escape))
985 {
986 *pfirst = j;
987 return cie;
988 }
989
990 fail:;
991 }
992
993 cie = xmalloc (sizeof (struct cie_entry));
994 cie->next = cie_root;
995 cie_root = cie;
996 cie->return_column = fde->return_column;
997 cie->first = fde->data;
998
999 for (i = cie->first; i ; i = i->next)
1000 if (i->insn == DW_CFA_advance_loc
1001 || i->insn == DW_CFA_remember_state
1002 || i->insn == CFI_escape)
1003 break;
1004
1005 cie->last = i;
1006 *pfirst = i;
1007
1008 output_cie (cie);
1009
1010 return cie;
1011 }
1012
1013 void
1014 cfi_finish (void)
1015 {
1016 segT cfi_seg;
1017 struct fde_entry *fde;
1018 int save_flag_traditional_format;
1019
1020 if (cur_fde_data)
1021 {
1022 as_bad (_("open CFI at the end of file; missing .cfi_endproc directive"));
1023 cur_fde_data->end_address = cur_fde_data->start_address;
1024 }
1025
1026 if (all_fde_data == 0)
1027 return;
1028
1029 /* Open .eh_frame section. */
1030 cfi_seg = subseg_new (".eh_frame", 0);
1031 bfd_set_section_flags (stdoutput, cfi_seg,
1032 SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_READONLY);
1033 subseg_set (cfi_seg, 0);
1034 record_alignment (cfi_seg, EH_FRAME_ALIGNMENT);
1035
1036 /* Make sure check_eh_frame doesn't do anything with our output. */
1037 save_flag_traditional_format = flag_traditional_format;
1038 flag_traditional_format = 1;
1039
1040 for (fde = all_fde_data; fde ; fde = fde->next)
1041 {
1042 struct cfi_insn_data *first;
1043 struct cie_entry *cie;
1044
1045 cie = select_cie_for_fde (fde, &first);
1046 output_fde (fde, cie, first, fde->next == NULL ? EH_FRAME_ALIGNMENT : 2);
1047 }
1048
1049 flag_traditional_format = save_flag_traditional_format;
1050 }
This page took 0.051918 seconds and 5 git commands to generate.