* dw2gencfi.c (struct cfa_save_data, cfa_save_stack): New.
[binutils.git] / gas / dw2gencfi.c
blobccfb1103a16bdf4705953c8c1a6cc5a5059e0c5a
1 /* dw2gencfi.c - Support for generating Dwarf2 CFI information.
2 Copyright 2003 Free Software Foundation, Inc.
3 Contributed by Michal Ludvig <mludvig@suse.cz>
5 This file is part of GAS, the GNU Assembler.
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.
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.
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, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 #include "as.h"
23 #include "dw2gencfi.h"
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
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
44 #ifndef tc_cfi_frame_initial_instructions
45 # define tc_cfi_frame_initial_instructions() ((void)0)
46 #endif
49 struct cfi_insn_data
51 struct cfi_insn_data *next;
52 int insn;
53 union {
54 struct {
55 unsigned reg;
56 offsetT offset;
57 } ri;
59 struct {
60 unsigned reg1;
61 unsigned reg2;
62 } rr;
64 unsigned r;
65 offsetT i;
67 struct {
68 symbolS *lab1;
69 symbolS *lab2;
70 } ll;
71 } u;
74 struct fde_entry
76 struct fde_entry *next;
77 symbolS *start_address;
78 symbolS *end_address;
79 struct cfi_insn_data *data;
80 struct cfi_insn_data **last;
81 unsigned int return_column;
84 struct cie_entry
86 struct cie_entry *next;
87 symbolS *start_address;
88 unsigned int return_column;
89 struct cfi_insn_data *first, *last;
93 /* Current open FDE entry. */
94 static struct fde_entry *cur_fde_data;
95 static symbolS *last_address;
96 static offsetT cur_cfa_offset;
98 /* List of FDE entries. */
99 static struct fde_entry *all_fde_data;
100 static struct fde_entry **last_fde_data = &all_fde_data;
102 /* List of CIEs so that they could be reused. */
103 static struct cie_entry *cie_root;
105 /* Stack of old CFI data, for save/restore. */
106 struct cfa_save_data
108 struct cfa_save_data *next;
109 offsetT cfa_offset;
112 static struct cfa_save_data *cfa_save_stack;
114 /* Construct a new FDE structure and add it to the end of the fde list. */
116 static struct fde_entry *
117 alloc_fde_entry (void)
119 struct fde_entry *fde = xcalloc (1, sizeof (struct fde_entry));
121 cur_fde_data = fde;
122 *last_fde_data = fde;
123 last_fde_data = &fde->next;
125 fde->last = &fde->data;
126 fde->return_column = DWARF2_DEFAULT_RETURN_COLUMN;
128 return fde;
131 /* The following functions are available for a backend to construct its
132 own unwind information, usually from legacy unwind directives. */
134 /* Construct a new INSN structure and add it to the end of the insn list
135 for the currently active FDE. */
137 static struct cfi_insn_data *
138 alloc_cfi_insn_data (void)
140 struct cfi_insn_data *insn = xcalloc (1, sizeof (struct cfi_insn_data));
142 *cur_fde_data->last = insn;
143 cur_fde_data->last = &insn->next;
145 return insn;
148 /* Construct a new FDE structure that begins at LABEL. */
150 void
151 cfi_new_fde (symbolS *label)
153 struct fde_entry *fde = alloc_fde_entry ();
154 fde->start_address = label;
155 last_address = label;
158 /* End the currently open FDE. */
160 void
161 cfi_end_fde (symbolS *label)
163 cur_fde_data->end_address = label;
164 cur_fde_data = NULL;
167 /* Set the return column for the current FDE. */
169 void
170 cfi_set_return_column (unsigned regno)
172 cur_fde_data->return_column = regno;
175 /* Universal functions to store new instructions. */
177 static void
178 cfi_add_CFA_insn(int insn)
180 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
182 insn_ptr->insn = insn;
185 static void
186 cfi_add_CFA_insn_reg (int insn, unsigned regno)
188 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
190 insn_ptr->insn = insn;
191 insn_ptr->u.r = regno;
194 static void
195 cfi_add_CFA_insn_offset (int insn, offsetT offset)
197 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
199 insn_ptr->insn = insn;
200 insn_ptr->u.i = offset;
203 static void
204 cfi_add_CFA_insn_reg_reg (int insn, unsigned reg1, unsigned reg2)
206 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
208 insn_ptr->insn = insn;
209 insn_ptr->u.rr.reg1 = reg1;
210 insn_ptr->u.rr.reg2 = reg2;
213 static void
214 cfi_add_CFA_insn_reg_offset (int insn, unsigned regno, offsetT offset)
216 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
218 insn_ptr->insn = insn;
219 insn_ptr->u.ri.reg = regno;
220 insn_ptr->u.ri.offset = offset;
223 /* Add a CFI insn to advance the PC from the last address to LABEL. */
225 void
226 cfi_add_advance_loc (symbolS *label)
228 struct cfi_insn_data *insn = alloc_cfi_insn_data ();
230 insn->insn = DW_CFA_advance_loc;
231 insn->u.ll.lab1 = last_address;
232 insn->u.ll.lab2 = label;
234 last_address = label;
237 /* Add a DW_CFA_offset record to the CFI data. */
239 void
240 cfi_add_CFA_offset (unsigned regno, offsetT offset)
242 unsigned int abs_data_align;
244 cfi_add_CFA_insn_reg_offset (DW_CFA_offset, regno, offset);
246 abs_data_align = (DWARF2_CIE_DATA_ALIGNMENT < 0
247 ? -DWARF2_CIE_DATA_ALIGNMENT : DWARF2_CIE_DATA_ALIGNMENT);
248 if (offset % abs_data_align)
249 as_bad (_("register save offset not a multiple of %u"), abs_data_align);
252 /* Add a DW_CFA_def_cfa record to the CFI data. */
254 void
255 cfi_add_CFA_def_cfa (unsigned regno, offsetT offset)
257 cfi_add_CFA_insn_reg_offset (DW_CFA_def_cfa, regno, offset);
258 cur_cfa_offset = offset;
261 /* Add a DW_CFA_register record to the CFI data. */
263 void
264 cfi_add_CFA_register (unsigned reg1, unsigned reg2)
266 cfi_add_CFA_insn_reg_reg (DW_CFA_register, reg1, reg2);
269 /* Add a DW_CFA_def_cfa_register record to the CFI data. */
271 void
272 cfi_add_CFA_def_cfa_register (unsigned regno)
274 cfi_add_CFA_insn_reg (DW_CFA_def_cfa_register, regno);
277 /* Add a DW_CFA_def_cfa_offset record to the CFI data. */
279 void
280 cfi_add_CFA_def_cfa_offset (offsetT offset)
282 cfi_add_CFA_insn_offset (DW_CFA_def_cfa_offset, offset);
283 cur_cfa_offset = offset;
286 void
287 cfi_add_CFA_restore (unsigned regno)
289 cfi_add_CFA_insn_reg (DW_CFA_restore, regno);
292 void
293 cfi_add_CFA_undefined (unsigned regno)
295 cfi_add_CFA_insn_reg (DW_CFA_undefined, regno);
298 void
299 cfi_add_CFA_same_value (unsigned regno)
301 cfi_add_CFA_insn_reg (DW_CFA_same_value, regno);
304 void
305 cfi_add_CFA_remember_state (void)
307 struct cfa_save_data *p;
309 cfi_add_CFA_insn (DW_CFA_remember_state);
311 p = xmalloc (sizeof (*p));
312 p->cfa_offset = cur_cfa_offset;
313 p->next = cfa_save_stack;
314 cfa_save_stack = p;
317 void
318 cfi_add_CFA_restore_state (void)
320 struct cfa_save_data *p;
322 cfi_add_CFA_insn (DW_CFA_restore_state);
324 p = cfa_save_stack;
325 if (p)
327 cur_cfa_offset = p->cfa_offset;
328 cfa_save_stack = p->next;
329 free (p);
333 void
334 cfi_add_CFA_nop (void)
336 cfi_add_CFA_insn (DW_CFA_nop);
340 /* Parse CFI assembler directives. */
342 static void dot_cfi (int);
343 static void dot_cfi_startproc (int);
344 static void dot_cfi_endproc (int);
346 /* Fake CFI type; outside the byte range of any real CFI insn. */
347 #define CFI_adjust_cfa_offset 0x100
348 #define CFI_return_column 0x101
349 #define CFI_rel_offset 0x102
351 const pseudo_typeS cfi_pseudo_table[] =
353 { "cfi_startproc", dot_cfi_startproc, 0 },
354 { "cfi_endproc", dot_cfi_endproc, 0 },
355 { "cfi_def_cfa", dot_cfi, DW_CFA_def_cfa },
356 { "cfi_def_cfa_register", dot_cfi, DW_CFA_def_cfa_register },
357 { "cfi_def_cfa_offset", dot_cfi, DW_CFA_def_cfa_offset },
358 { "cfi_adjust_cfa_offset", dot_cfi, CFI_adjust_cfa_offset },
359 { "cfi_offset", dot_cfi, DW_CFA_offset },
360 { "cfi_rel_offset", dot_cfi, CFI_rel_offset },
361 { "cfi_register", dot_cfi, DW_CFA_register },
362 { "cfi_return_column", dot_cfi, CFI_return_column },
363 { "cfi_restore", dot_cfi, DW_CFA_restore },
364 { "cfi_undefined", dot_cfi, DW_CFA_undefined },
365 { "cfi_same_value", dot_cfi, DW_CFA_same_value },
366 { "cfi_remember_state", dot_cfi, DW_CFA_remember_state },
367 { "cfi_restore_state", dot_cfi, DW_CFA_restore_state },
368 { "cfi_nop", dot_cfi, DW_CFA_nop },
369 { NULL, NULL, 0 }
372 static void
373 cfi_parse_separator (void)
375 SKIP_WHITESPACE ();
376 if (*input_line_pointer == ',')
377 input_line_pointer++;
378 else
379 as_bad (_("missing separator"));
382 static unsigned
383 cfi_parse_reg (void)
385 int regno;
386 expressionS exp;
388 #ifdef tc_regname_to_dw2regnum
389 SKIP_WHITESPACE ();
390 if (is_name_beginner (*input_line_pointer)
391 || (*input_line_pointer == '%'
392 && is_name_beginner (*++input_line_pointer)))
394 char *name, c;
396 name = input_line_pointer;
397 c = get_symbol_end ();
399 if ((regno = tc_regname_to_dw2regnum (name)) < 0)
401 as_bad (_("bad register expression"));
402 regno = 0;
405 *input_line_pointer = c;
406 return regno;
408 #endif
410 expression (&exp);
411 switch (exp.X_op)
413 case O_register:
414 case O_constant:
415 regno = exp.X_add_number;
416 break;
418 default:
419 as_bad (_("bad register expression"));
420 regno = 0;
421 break;
424 return regno;
427 static offsetT
428 cfi_parse_const (void)
430 return get_absolute_expression ();
433 static void
434 dot_cfi (int arg)
436 offsetT offset;
437 unsigned reg1, reg2;
439 if (!cur_fde_data)
441 as_bad (_("CFI instruction used without previous .cfi_startproc"));
442 return;
445 /* If the last address was not at the current PC, advance to current. */
446 if (symbol_get_frag (last_address) != frag_now
447 || S_GET_VALUE (last_address) != frag_now_fix ())
448 cfi_add_advance_loc (symbol_temp_new_now ());
450 switch (arg)
452 case DW_CFA_offset:
453 reg1 = cfi_parse_reg ();
454 cfi_parse_separator ();
455 offset = cfi_parse_const ();
456 cfi_add_CFA_offset (reg1, offset);
457 break;
459 case CFI_rel_offset:
460 reg1 = cfi_parse_reg ();
461 cfi_parse_separator ();
462 offset = cfi_parse_const ();
463 cfi_add_CFA_offset (reg1, offset - cur_cfa_offset);
464 break;
466 case DW_CFA_def_cfa:
467 reg1 = cfi_parse_reg ();
468 cfi_parse_separator ();
469 offset = cfi_parse_const ();
470 cfi_add_CFA_def_cfa (reg1, offset);
471 break;
473 case DW_CFA_register:
474 reg1 = cfi_parse_reg ();
475 cfi_parse_separator ();
476 reg2 = cfi_parse_reg ();
477 cfi_add_CFA_register (reg1, reg2);
478 break;
480 case DW_CFA_def_cfa_register:
481 reg1 = cfi_parse_reg ();
482 cfi_add_CFA_def_cfa_register (reg1);
483 break;
485 case DW_CFA_def_cfa_offset:
486 offset = cfi_parse_const ();
487 cfi_add_CFA_def_cfa_offset (offset);
488 break;
490 case CFI_adjust_cfa_offset:
491 offset = cfi_parse_const ();
492 cfi_add_CFA_def_cfa_offset (cur_cfa_offset + offset);
493 break;
495 case DW_CFA_restore:
496 reg1 = cfi_parse_reg ();
497 cfi_add_CFA_restore (reg1);
498 break;
500 case DW_CFA_undefined:
501 reg1 = cfi_parse_reg ();
502 cfi_add_CFA_undefined (reg1);
503 break;
505 case DW_CFA_same_value:
506 reg1 = cfi_parse_reg ();
507 cfi_add_CFA_same_value (reg1);
508 break;
510 case CFI_return_column:
511 reg1 = cfi_parse_reg ();
512 cfi_set_return_column (reg1);
513 break;
515 case DW_CFA_remember_state:
516 cfi_add_CFA_remember_state ();
517 break;
519 case DW_CFA_restore_state:
520 cfi_add_CFA_restore_state ();
521 break;
523 case DW_CFA_nop:
524 cfi_add_CFA_nop ();
525 break;
527 default:
528 abort ();
531 demand_empty_rest_of_line ();
534 static void
535 dot_cfi_startproc (int ignored ATTRIBUTE_UNUSED)
537 int simple = 0;
539 if (cur_fde_data)
541 as_bad (_("previous CFI entry not closed (missing .cfi_endproc)"));
542 return;
545 cfi_new_fde (symbol_temp_new_now ());
547 SKIP_WHITESPACE ();
548 if (is_name_beginner (*input_line_pointer))
550 char *name, c;
552 name = input_line_pointer;
553 c = get_symbol_end ();
555 if (strcmp (name, "simple") == 0)
557 simple = 1;
558 *input_line_pointer = c;
560 else
561 input_line_pointer = name;
563 demand_empty_rest_of_line ();
565 if (!simple)
566 tc_cfi_frame_initial_instructions ();
569 static void
570 dot_cfi_endproc (int ignored ATTRIBUTE_UNUSED)
572 if (! cur_fde_data)
574 as_bad (_(".cfi_endproc without corresponding .cfi_startproc"));
575 return;
578 cfi_end_fde (symbol_temp_new_now ());
582 /* Emit a single byte into the current segment. */
584 static inline void
585 out_one (int byte)
587 FRAG_APPEND_1_CHAR (byte);
590 /* Emit a two-byte word into the current segment. */
592 static inline void
593 out_two (int data)
595 md_number_to_chars (frag_more (2), data, 2);
598 /* Emit a four byte word into the current segment. */
600 static inline void
601 out_four (int data)
603 md_number_to_chars (frag_more (4), data, 4);
606 /* Emit an unsigned "little-endian base 128" number. */
608 static void
609 out_uleb128 (addressT value)
611 output_leb128 (frag_more (sizeof_leb128 (value, 0)), value, 0);
614 /* Emit an unsigned "little-endian base 128" number. */
616 static void
617 out_sleb128 (offsetT value)
619 output_leb128 (frag_more (sizeof_leb128 (value, 1)), value, 1);
622 static void
623 output_cfi_insn (struct cfi_insn_data *insn)
625 offsetT offset;
626 unsigned int regno;
628 switch (insn->insn)
630 case DW_CFA_advance_loc:
632 symbolS *from = insn->u.ll.lab1;
633 symbolS *to = insn->u.ll.lab2;
635 if (symbol_get_frag (to) == symbol_get_frag (from))
637 addressT delta = S_GET_VALUE (to) - S_GET_VALUE (from);
638 addressT scaled = delta / DWARF2_LINE_MIN_INSN_LENGTH;
640 if (scaled <= 0x3F)
641 out_one (DW_CFA_advance_loc + scaled);
642 else if (delta <= 0xFF)
644 out_one (DW_CFA_advance_loc1);
645 out_one (delta);
647 else if (delta <= 0xFFFF)
649 out_one (DW_CFA_advance_loc2);
650 out_two (delta);
652 else
654 out_one (DW_CFA_advance_loc4);
655 out_four (delta);
658 else
660 expressionS exp;
662 exp.X_op = O_subtract;
663 exp.X_add_symbol = to;
664 exp.X_op_symbol = from;
665 exp.X_add_number = 0;
667 /* The code in ehopt.c expects that one byte of the encoding
668 is already allocated to the frag. This comes from the way
669 that it scans the .eh_frame section looking first for the
670 .byte DW_CFA_advance_loc4. */
671 frag_more (1);
673 frag_var (rs_cfa, 4, 0, DWARF2_LINE_MIN_INSN_LENGTH << 3,
674 make_expr_symbol (&exp), frag_now_fix () - 1,
675 (char *) frag_now);
678 break;
680 case DW_CFA_def_cfa:
681 offset = insn->u.ri.offset;
682 if (offset < 0)
684 out_one (DW_CFA_def_cfa_sf);
685 out_uleb128 (insn->u.ri.reg);
686 out_uleb128 (offset);
688 else
690 out_one (DW_CFA_def_cfa);
691 out_uleb128 (insn->u.ri.reg);
692 out_uleb128 (offset);
694 break;
696 case DW_CFA_def_cfa_register:
697 case DW_CFA_undefined:
698 case DW_CFA_same_value:
699 out_one (insn->insn);
700 out_uleb128 (insn->u.r);
701 break;
703 case DW_CFA_def_cfa_offset:
704 offset = insn->u.i;
705 if (offset < 0)
707 out_one (DW_CFA_def_cfa_offset_sf);
708 out_sleb128 (offset);
710 else
712 out_one (DW_CFA_def_cfa_offset);
713 out_uleb128 (offset);
715 break;
717 case DW_CFA_restore:
718 regno = insn->u.r;
719 if (regno <= 0x3F)
721 out_one (DW_CFA_restore + regno);
723 else
725 out_one (DW_CFA_restore_extended);
726 out_uleb128 (regno);
728 break;
730 case DW_CFA_offset:
731 regno = insn->u.ri.reg;
732 offset = insn->u.ri.offset / DWARF2_CIE_DATA_ALIGNMENT;
733 if (offset < 0)
735 out_one (DW_CFA_offset_extended_sf);
736 out_uleb128 (regno);
737 out_sleb128 (offset);
739 else if (regno <= 0x3F)
741 out_one (DW_CFA_offset + regno);
742 out_uleb128 (offset);
744 else
746 out_one (DW_CFA_offset_extended);
747 out_uleb128 (regno);
748 out_uleb128 (offset);
750 break;
752 case DW_CFA_register:
753 out_one (DW_CFA_register);
754 out_uleb128 (insn->u.rr.reg1);
755 out_uleb128 (insn->u.rr.reg2);
756 break;
758 case DW_CFA_remember_state:
759 case DW_CFA_restore_state:
760 case DW_CFA_nop:
761 out_one (insn->insn);
762 break;
764 default:
765 abort ();
769 static void
770 output_cie (struct cie_entry *cie)
772 symbolS *after_size_address, *end_address;
773 expressionS exp;
774 struct cfi_insn_data *i;
776 cie->start_address = symbol_temp_new_now ();
777 after_size_address = symbol_temp_make ();
778 end_address = symbol_temp_make ();
780 exp.X_op = O_subtract;
781 exp.X_add_symbol = end_address;
782 exp.X_op_symbol = after_size_address;
783 exp.X_add_number = 0;
785 emit_expr (&exp, 4); /* Length */
786 symbol_set_value_now (after_size_address);
787 out_four (0); /* CIE id */
788 out_one (DW_CIE_VERSION); /* Version */
789 out_one ('z'); /* Augmentation */
790 out_one ('R');
791 out_one (0);
792 out_uleb128 (DWARF2_LINE_MIN_INSN_LENGTH); /* Code alignment */
793 out_sleb128 (DWARF2_CIE_DATA_ALIGNMENT); /* Data alignment */
794 out_one (cie->return_column); /* Return column */
795 out_uleb128 (1); /* Augmentation size */
796 out_one (DW_EH_PE_pcrel | DW_EH_PE_sdata4);
798 if (cie->first)
799 for (i = cie->first; i != cie->last; i = i->next)
800 output_cfi_insn (i);
802 frag_align (2, 0, 0);
803 symbol_set_value_now (end_address);
806 static void
807 output_fde (struct fde_entry *fde, struct cie_entry *cie,
808 struct cfi_insn_data *first)
810 symbolS *after_size_address, *end_address;
811 expressionS exp;
813 after_size_address = symbol_temp_make ();
814 end_address = symbol_temp_make ();
816 exp.X_op = O_subtract;
817 exp.X_add_symbol = end_address;
818 exp.X_op_symbol = after_size_address;
819 exp.X_add_number = 0;
820 emit_expr (&exp, 4); /* Length */
821 symbol_set_value_now (after_size_address);
823 exp.X_add_symbol = after_size_address;
824 exp.X_op_symbol = cie->start_address;
825 emit_expr (&exp, 4); /* CIE offset */
827 exp.X_add_symbol = fde->start_address;
828 exp.X_op_symbol = symbol_temp_new_now ();
829 emit_expr (&exp, 4); /* Code offset */
831 exp.X_add_symbol = fde->end_address;
832 exp.X_op_symbol = fde->start_address; /* Code length */
833 emit_expr (&exp, 4);
835 out_uleb128 (0); /* Augmentation size */
837 for (; first; first = first->next)
838 output_cfi_insn (first);
840 frag_align (2, 0, 0);
841 symbol_set_value_now (end_address);
844 static struct cie_entry *
845 select_cie_for_fde (struct fde_entry *fde, struct cfi_insn_data **pfirst)
847 struct cfi_insn_data *i, *j;
848 struct cie_entry *cie;
850 for (cie = cie_root; cie; cie = cie->next)
852 if (cie->return_column != fde->return_column)
853 continue;
854 for (i = cie->first, j = fde->data;
855 i != cie->last && j != NULL;
856 i = i->next, j = j->next)
858 if (i->insn != j->insn)
859 goto fail;
860 switch (i->insn)
862 case DW_CFA_advance_loc:
863 /* We reached the first advance in the FDE, but did not
864 reach the end of the CIE list. */
865 goto fail;
867 case DW_CFA_offset:
868 case DW_CFA_def_cfa:
869 if (i->u.ri.reg != j->u.ri.reg)
870 goto fail;
871 if (i->u.ri.offset != j->u.ri.offset)
872 goto fail;
873 break;
875 case DW_CFA_register:
876 if (i->u.rr.reg1 != j->u.rr.reg1)
877 goto fail;
878 if (i->u.rr.reg2 != j->u.rr.reg2)
879 goto fail;
880 break;
882 case DW_CFA_def_cfa_register:
883 case DW_CFA_restore:
884 case DW_CFA_undefined:
885 case DW_CFA_same_value:
886 if (i->u.r != j->u.r)
887 goto fail;
888 break;
890 case DW_CFA_def_cfa_offset:
891 if (i->u.i != j->u.i)
892 goto fail;
893 break;
895 default:
896 abort ();
900 /* Success if we reached the end of the CIE list, and we've either
901 run out of FDE entries or we've encountered an advance. */
902 if (i == cie->last && (!j || j->insn == DW_CFA_advance_loc))
904 *pfirst = j;
905 return cie;
908 fail:;
911 cie = xmalloc (sizeof (struct cie_entry));
912 cie->next = cie_root;
913 cie_root = cie;
914 cie->return_column = fde->return_column;
915 cie->first = fde->data;
917 for (i = cie->first; i ; i = i->next)
918 if (i->insn == DW_CFA_advance_loc)
919 break;
921 cie->last = i;
922 *pfirst = i;
924 output_cie (cie);
926 return cie;
929 void
930 cfi_finish (void)
932 segT cfi_seg;
933 struct fde_entry *fde;
934 int save_flag_traditional_format;
936 if (cur_fde_data)
938 as_bad (_("open CFI at the end of file; missing .cfi_endproc directive"));
939 cur_fde_data->end_address = cur_fde_data->start_address;
942 if (all_fde_data == 0)
943 return;
945 /* Open .eh_frame section. */
946 cfi_seg = subseg_new (".eh_frame", 0);
947 #ifdef BFD_ASSEMBLER
948 bfd_set_section_flags (stdoutput, cfi_seg,
949 SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_READONLY);
950 #endif
951 subseg_set (cfi_seg, 0);
952 record_alignment (cfi_seg, 2);
954 /* Make sure check_eh_frame doesn't do anything with our output. */
955 save_flag_traditional_format = flag_traditional_format;
956 flag_traditional_format = 1;
958 for (fde = all_fde_data; fde ; fde = fde->next)
960 struct cfi_insn_data *first;
961 struct cie_entry *cie;
963 cie = select_cie_for_fde (fde, &first);
964 output_fde (fde, cie, first);
967 flag_traditional_format = save_flag_traditional_format;