OSDN Git Service

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