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