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