* dw2gencfi.c (DWARF2_FDE_RELOC_SIZE): New.
[external/binutils.git] / gas / dw2gencfi.c
1 /* dw2gencfi.c - Support for generating Dwarf2 CFI information.
2    Copyright 2003, 2004, 2005, 2006, 2007 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 3, 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 #include "subsegs.h"
25
26 #ifdef TARGET_USE_CFIPOP
27
28 /* We re-use DWARF2_LINE_MIN_INSN_LENGTH for the code alignment field
29    of the CIE.  Default to 1 if not otherwise specified.  */
30 #ifndef  DWARF2_LINE_MIN_INSN_LENGTH
31 # define DWARF2_LINE_MIN_INSN_LENGTH 1
32 #endif
33
34 /* By default, use 32-bit relocations from .eh_frame into .text.  */
35 #ifndef DWARF2_FDE_RELOC_SIZE
36 # define DWARF2_FDE_RELOC_SIZE 4
37 #endif
38
39 /* By default, use a read-only .eh_frame section.  */
40 #ifndef DWARF2_EH_FRAME_READ_ONLY
41 # define DWARF2_EH_FRAME_READ_ONLY SEC_READONLY
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 char per_encoding;
91   unsigned char lsda_encoding;
92   expressionS personality;
93   expressionS lsda;
94   unsigned int return_column;
95   unsigned int signal_frame;
96 };
97
98 struct cie_entry
99 {
100   struct cie_entry *next;
101   symbolS *start_address;
102   unsigned int return_column;
103   unsigned int signal_frame;
104   unsigned char per_encoding;
105   unsigned char lsda_encoding;
106   expressionS personality;
107   struct cfi_insn_data *first, *last;
108 };
109
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 /* Current open FDE entry.  */
126 struct frch_cfi_data
127 {
128   struct fde_entry *cur_fde_data;
129   symbolS *last_address;
130   offsetT cur_cfa_offset;
131   struct cfa_save_data *cfa_save_stack;
132 };
133 \f
134 /* Construct a new FDE structure and add it to the end of the fde list.  */
135
136 static struct fde_entry *
137 alloc_fde_entry (void)
138 {
139   struct fde_entry *fde = xcalloc (1, sizeof (struct fde_entry));
140
141   frchain_now->frch_cfi_data = xcalloc (1, sizeof (struct frch_cfi_data));
142   frchain_now->frch_cfi_data->cur_fde_data = fde;
143   *last_fde_data = fde;
144   last_fde_data = &fde->next;
145
146   fde->last = &fde->data;
147   fde->return_column = DWARF2_DEFAULT_RETURN_COLUMN;
148   fde->per_encoding = DW_EH_PE_omit;
149   fde->lsda_encoding = DW_EH_PE_omit;
150
151   return fde;
152 }
153
154 /* The following functions are available for a backend to construct its
155    own unwind information, usually from legacy unwind directives.  */
156
157 /* Construct a new INSN structure and add it to the end of the insn list
158    for the currently active FDE.  */
159
160 static struct cfi_insn_data *
161 alloc_cfi_insn_data (void)
162 {
163   struct cfi_insn_data *insn = xcalloc (1, sizeof (struct cfi_insn_data));
164   struct fde_entry *cur_fde_data = frchain_now->frch_cfi_data->cur_fde_data;
165
166   *cur_fde_data->last = insn;
167   cur_fde_data->last = &insn->next;
168
169   return insn;
170 }
171
172 /* Construct a new FDE structure that begins at LABEL.  */
173
174 void 
175 cfi_new_fde (symbolS *label)
176 {
177   struct fde_entry *fde = alloc_fde_entry ();
178   fde->start_address = label;
179   frchain_now->frch_cfi_data->last_address = label;
180 }
181
182 /* End the currently open FDE.  */
183
184 void 
185 cfi_end_fde (symbolS *label)
186 {
187   frchain_now->frch_cfi_data->cur_fde_data->end_address = label;
188   free (frchain_now->frch_cfi_data);
189   frchain_now->frch_cfi_data = NULL;
190 }
191
192 /* Set the return column for the current FDE.  */
193
194 void
195 cfi_set_return_column (unsigned regno)
196 {
197   frchain_now->frch_cfi_data->cur_fde_data->return_column = regno;
198 }
199
200 /* Universal functions to store new instructions.  */
201
202 static void
203 cfi_add_CFA_insn(int insn)
204 {
205   struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
206
207   insn_ptr->insn = insn;
208 }
209
210 static void
211 cfi_add_CFA_insn_reg (int insn, unsigned regno)
212 {
213   struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
214
215   insn_ptr->insn = insn;
216   insn_ptr->u.r = regno;
217 }
218
219 static void
220 cfi_add_CFA_insn_offset (int insn, offsetT offset)
221 {
222   struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
223
224   insn_ptr->insn = insn;
225   insn_ptr->u.i = offset;
226 }
227
228 static void
229 cfi_add_CFA_insn_reg_reg (int insn, unsigned reg1, unsigned reg2)
230 {
231   struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
232
233   insn_ptr->insn = insn;
234   insn_ptr->u.rr.reg1 = reg1;
235   insn_ptr->u.rr.reg2 = reg2;
236 }
237
238 static void
239 cfi_add_CFA_insn_reg_offset (int insn, unsigned regno, offsetT offset)
240 {
241   struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
242
243   insn_ptr->insn = insn;
244   insn_ptr->u.ri.reg = regno;
245   insn_ptr->u.ri.offset = offset;
246 }
247
248 /* Add a CFI insn to advance the PC from the last address to LABEL.  */
249
250 void
251 cfi_add_advance_loc (symbolS *label)
252 {
253   struct cfi_insn_data *insn = alloc_cfi_insn_data ();
254
255   insn->insn = DW_CFA_advance_loc;
256   insn->u.ll.lab1 = frchain_now->frch_cfi_data->last_address;
257   insn->u.ll.lab2 = label;
258
259   frchain_now->frch_cfi_data->last_address = label;
260 }
261
262 /* Add a DW_CFA_offset record to the CFI data.  */
263
264 void
265 cfi_add_CFA_offset (unsigned regno, offsetT offset)
266 {
267   unsigned int abs_data_align;
268
269   assert (DWARF2_CIE_DATA_ALIGNMENT != 0);
270   cfi_add_CFA_insn_reg_offset (DW_CFA_offset, regno, offset);
271
272   abs_data_align = (DWARF2_CIE_DATA_ALIGNMENT < 0
273                     ? -DWARF2_CIE_DATA_ALIGNMENT : DWARF2_CIE_DATA_ALIGNMENT);
274   if (offset % abs_data_align)
275     as_bad (_("register save offset not a multiple of %u"), abs_data_align);
276 }
277
278 /* Add a DW_CFA_def_cfa record to the CFI data.  */
279
280 void
281 cfi_add_CFA_def_cfa (unsigned regno, offsetT offset)
282 {
283   cfi_add_CFA_insn_reg_offset (DW_CFA_def_cfa, regno, offset);
284   frchain_now->frch_cfi_data->cur_cfa_offset = offset;
285 }
286
287 /* Add a DW_CFA_register record to the CFI data.  */
288
289 void
290 cfi_add_CFA_register (unsigned reg1, unsigned reg2)
291 {
292   cfi_add_CFA_insn_reg_reg (DW_CFA_register, reg1, reg2);
293 }
294
295 /* Add a DW_CFA_def_cfa_register record to the CFI data.  */
296
297 void
298 cfi_add_CFA_def_cfa_register (unsigned regno)
299 {
300   cfi_add_CFA_insn_reg (DW_CFA_def_cfa_register, regno);
301 }
302
303 /* Add a DW_CFA_def_cfa_offset record to the CFI data.  */
304
305 void
306 cfi_add_CFA_def_cfa_offset (offsetT offset)
307 {
308   cfi_add_CFA_insn_offset (DW_CFA_def_cfa_offset, offset);
309   frchain_now->frch_cfi_data->cur_cfa_offset = offset;
310 }
311
312 void
313 cfi_add_CFA_restore (unsigned regno)
314 {
315   cfi_add_CFA_insn_reg (DW_CFA_restore, regno);
316 }
317
318 void
319 cfi_add_CFA_undefined (unsigned regno)
320 {
321   cfi_add_CFA_insn_reg (DW_CFA_undefined, regno);
322 }
323
324 void
325 cfi_add_CFA_same_value (unsigned regno)
326 {
327   cfi_add_CFA_insn_reg (DW_CFA_same_value, regno);
328 }
329
330 void
331 cfi_add_CFA_remember_state (void)
332 {
333   struct cfa_save_data *p;
334
335   cfi_add_CFA_insn (DW_CFA_remember_state);
336
337   p = xmalloc (sizeof (*p));
338   p->cfa_offset = frchain_now->frch_cfi_data->cur_cfa_offset;
339   p->next = frchain_now->frch_cfi_data->cfa_save_stack;
340   frchain_now->frch_cfi_data->cfa_save_stack = p;
341 }
342
343 void
344 cfi_add_CFA_restore_state (void)
345 {
346   struct cfa_save_data *p;
347
348   cfi_add_CFA_insn (DW_CFA_restore_state);
349
350   p = frchain_now->frch_cfi_data->cfa_save_stack;
351   if (p)
352     {
353       frchain_now->frch_cfi_data->cur_cfa_offset = p->cfa_offset;
354       frchain_now->frch_cfi_data->cfa_save_stack = p->next;
355       free (p);
356     }
357   else
358     as_bad (_("CFI state restore without previous remember"));
359 }
360
361 \f
362 /* Parse CFI assembler directives.  */
363
364 static void dot_cfi (int);
365 static void dot_cfi_escape (int);
366 static void dot_cfi_startproc (int);
367 static void dot_cfi_endproc (int);
368 static void dot_cfi_personality (int);
369 static void dot_cfi_lsda (int);
370
371 /* Fake CFI type; outside the byte range of any real CFI insn.  */
372 #define CFI_adjust_cfa_offset   0x100
373 #define CFI_return_column       0x101
374 #define CFI_rel_offset          0x102
375 #define CFI_escape              0x103
376 #define CFI_signal_frame        0x104
377
378 const pseudo_typeS cfi_pseudo_table[] =
379   {
380     { "cfi_startproc", dot_cfi_startproc, 0 },
381     { "cfi_endproc", dot_cfi_endproc, 0 },
382     { "cfi_def_cfa", dot_cfi, DW_CFA_def_cfa },
383     { "cfi_def_cfa_register", dot_cfi, DW_CFA_def_cfa_register },
384     { "cfi_def_cfa_offset", dot_cfi, DW_CFA_def_cfa_offset },
385     { "cfi_adjust_cfa_offset", dot_cfi, CFI_adjust_cfa_offset },
386     { "cfi_offset", dot_cfi, DW_CFA_offset },
387     { "cfi_rel_offset", dot_cfi, CFI_rel_offset },
388     { "cfi_register", dot_cfi, DW_CFA_register },
389     { "cfi_return_column", dot_cfi, CFI_return_column },
390     { "cfi_restore", dot_cfi, DW_CFA_restore },
391     { "cfi_undefined", dot_cfi, DW_CFA_undefined },
392     { "cfi_same_value", dot_cfi, DW_CFA_same_value },
393     { "cfi_remember_state", dot_cfi, DW_CFA_remember_state },
394     { "cfi_restore_state", dot_cfi, DW_CFA_restore_state },
395     { "cfi_window_save", dot_cfi, DW_CFA_GNU_window_save },
396     { "cfi_escape", dot_cfi_escape, 0 },
397     { "cfi_signal_frame", dot_cfi, CFI_signal_frame },
398     { "cfi_personality", dot_cfi_personality, 0 },
399     { "cfi_lsda", dot_cfi_lsda, 0 },
400     { NULL, NULL, 0 }
401   };
402
403 static void
404 cfi_parse_separator (void)
405 {
406   SKIP_WHITESPACE ();
407   if (*input_line_pointer == ',')
408     input_line_pointer++;
409   else
410     as_bad (_("missing separator"));
411 }
412
413 #ifndef tc_parse_to_dw2regnum
414 static void
415 tc_parse_to_dw2regnum(expressionS *exp)
416 {
417 # ifdef tc_regname_to_dw2regnum
418   SKIP_WHITESPACE ();
419   if (is_name_beginner (*input_line_pointer)
420       || (*input_line_pointer == '%'
421           && is_name_beginner (*++input_line_pointer)))
422     {
423       char *name, c;
424
425       name = input_line_pointer;
426       c = get_symbol_end ();
427
428       exp->X_op = O_constant;
429       exp->X_add_number = tc_regname_to_dw2regnum (name);
430
431       *input_line_pointer = c;
432     }
433   else
434 # endif
435     expression_and_evaluate (exp);
436 }
437 #endif
438
439 static unsigned
440 cfi_parse_reg (void)
441 {
442   int regno;
443   expressionS exp;
444
445   tc_parse_to_dw2regnum (&exp);
446   switch (exp.X_op)
447     {
448     case O_register:
449     case O_constant:
450       regno = exp.X_add_number;
451       break;
452
453     default:
454       regno = -1;
455       break;
456     }
457
458   if (regno < 0)
459     {
460       as_bad (_("bad register expression"));
461       regno = 0;
462     }
463
464   return regno;
465 }
466
467 static offsetT
468 cfi_parse_const (void)
469 {
470   return get_absolute_expression ();
471 }
472
473 static void
474 dot_cfi (int arg)
475 {
476   offsetT offset;
477   unsigned reg1, reg2;
478
479   if (frchain_now->frch_cfi_data == NULL)
480     {
481       as_bad (_("CFI instruction used without previous .cfi_startproc"));
482       ignore_rest_of_line ();
483       return;
484     }
485
486   /* If the last address was not at the current PC, advance to current.  */
487   if (symbol_get_frag (frchain_now->frch_cfi_data->last_address) != frag_now
488       || S_GET_VALUE (frchain_now->frch_cfi_data->last_address)
489          != frag_now_fix ())
490     cfi_add_advance_loc (symbol_temp_new_now ());
491
492   switch (arg)
493     {
494     case DW_CFA_offset:
495       reg1 = cfi_parse_reg ();
496       cfi_parse_separator ();
497       offset = cfi_parse_const ();
498       cfi_add_CFA_offset (reg1, offset);
499       break;
500
501     case CFI_rel_offset:
502       reg1 = cfi_parse_reg ();
503       cfi_parse_separator ();
504       offset = cfi_parse_const ();
505       cfi_add_CFA_offset (reg1,
506                           offset - frchain_now->frch_cfi_data->cur_cfa_offset);
507       break;
508
509     case DW_CFA_def_cfa:
510       reg1 = cfi_parse_reg ();
511       cfi_parse_separator ();
512       offset = cfi_parse_const ();
513       cfi_add_CFA_def_cfa (reg1, offset);
514       break;
515
516     case DW_CFA_register:
517       reg1 = cfi_parse_reg ();
518       cfi_parse_separator ();
519       reg2 = cfi_parse_reg ();
520       cfi_add_CFA_register (reg1, reg2);
521       break;
522
523     case DW_CFA_def_cfa_register:
524       reg1 = cfi_parse_reg ();
525       cfi_add_CFA_def_cfa_register (reg1);
526       break;
527
528     case DW_CFA_def_cfa_offset:
529       offset = cfi_parse_const ();
530       cfi_add_CFA_def_cfa_offset (offset);
531       break;
532
533     case CFI_adjust_cfa_offset:
534       offset = cfi_parse_const ();
535       cfi_add_CFA_def_cfa_offset (frchain_now->frch_cfi_data->cur_cfa_offset
536                                   + offset);
537       break;
538
539     case DW_CFA_restore:
540       for (;;)
541         {
542           reg1 = cfi_parse_reg ();
543           cfi_add_CFA_restore (reg1);
544           SKIP_WHITESPACE ();
545           if (*input_line_pointer != ',')
546             break;
547           ++input_line_pointer;
548         }
549       break;
550
551     case DW_CFA_undefined:
552       for (;;)
553         {
554           reg1 = cfi_parse_reg ();
555           cfi_add_CFA_undefined (reg1);
556           SKIP_WHITESPACE ();
557           if (*input_line_pointer != ',')
558             break;
559           ++input_line_pointer;
560         }
561       break;
562
563     case DW_CFA_same_value:
564       reg1 = cfi_parse_reg ();
565       cfi_add_CFA_same_value (reg1);
566       break;
567
568     case CFI_return_column:
569       reg1 = cfi_parse_reg ();
570       cfi_set_return_column (reg1);
571       break;
572
573     case DW_CFA_remember_state:
574       cfi_add_CFA_remember_state ();
575       break;
576
577     case DW_CFA_restore_state:
578       cfi_add_CFA_restore_state ();
579       break;
580
581     case DW_CFA_GNU_window_save:
582       cfi_add_CFA_insn (DW_CFA_GNU_window_save);
583       break;
584
585     case CFI_signal_frame:
586       frchain_now->frch_cfi_data->cur_fde_data->signal_frame = 1;
587       break;
588
589     default:
590       abort ();
591     }
592
593   demand_empty_rest_of_line ();
594 }
595
596 static void
597 dot_cfi_escape (int ignored ATTRIBUTE_UNUSED)
598 {
599   struct cfi_escape_data *head, **tail, *e;
600   struct cfi_insn_data *insn;
601
602   if (frchain_now->frch_cfi_data == NULL)
603     {
604       as_bad (_("CFI instruction used without previous .cfi_startproc"));
605       ignore_rest_of_line ();
606       return;
607     }
608
609   /* If the last address was not at the current PC, advance to current.  */
610   if (symbol_get_frag (frchain_now->frch_cfi_data->last_address) != frag_now
611       || S_GET_VALUE (frchain_now->frch_cfi_data->last_address)
612          != frag_now_fix ())
613     cfi_add_advance_loc (symbol_temp_new_now ());
614
615   tail = &head;
616   do
617     {
618       e = xmalloc (sizeof (*e));
619       do_parse_cons_expression (&e->exp, 1);
620       *tail = e;
621       tail = &e->next;
622     }
623   while (*input_line_pointer++ == ',');
624   *tail = NULL;
625
626   insn = alloc_cfi_insn_data ();
627   insn->insn = CFI_escape;
628   insn->u.esc = head;
629
630   --input_line_pointer;
631   demand_empty_rest_of_line ();
632 }
633
634 static void
635 dot_cfi_personality (int ignored ATTRIBUTE_UNUSED)
636 {
637   struct fde_entry *fde;
638   offsetT encoding;
639
640   if (frchain_now->frch_cfi_data == NULL)
641     {
642       as_bad (_("CFI instruction used without previous .cfi_startproc"));
643       ignore_rest_of_line ();
644       return;
645     }
646
647   fde = frchain_now->frch_cfi_data->cur_fde_data;
648   encoding = get_absolute_expression ();
649   if (encoding == DW_EH_PE_omit)
650     {
651       demand_empty_rest_of_line ();
652       fde->per_encoding = encoding;
653       return;
654     }
655
656   if ((encoding & 0xff) != encoding
657       || ((encoding & 0x70) != 0
658 #if defined DIFF_EXPR_OK || defined tc_cfi_emit_pcrel_expr
659           && (encoding & 0x70) != DW_EH_PE_pcrel
660 #endif
661           )
662          /* leb128 can be handled, but does something actually need it?  */
663       || (encoding & 7) == DW_EH_PE_uleb128
664       || (encoding & 7) > DW_EH_PE_udata8)
665     {
666       as_bad (_("invalid or unsupported encoding in .cfi_personality"));
667       ignore_rest_of_line ();
668       return;
669     }
670
671   if (*input_line_pointer++ != ',')
672     {
673       as_bad (_(".cfi_personality requires encoding and symbol arguments"));
674       ignore_rest_of_line ();
675       return;
676     }
677
678   expression_and_evaluate (&fde->personality);
679   switch (fde->personality.X_op)
680     {
681     case O_symbol:
682       break;
683     case O_constant:
684       if ((encoding & 0x70) == DW_EH_PE_pcrel)
685         encoding = DW_EH_PE_omit;
686       break;
687     default:
688       encoding = DW_EH_PE_omit;
689       break;
690     }
691
692   fde->per_encoding = encoding;
693
694   if (encoding == DW_EH_PE_omit)
695     {
696       as_bad (_("wrong second argument to .cfi_personality"));
697       ignore_rest_of_line ();
698       return;
699     }
700
701   demand_empty_rest_of_line ();
702 }
703
704 static void
705 dot_cfi_lsda (int ignored ATTRIBUTE_UNUSED)
706 {
707   struct fde_entry *fde;
708   offsetT encoding;
709
710   if (frchain_now->frch_cfi_data == NULL)
711     {
712       as_bad (_("CFI instruction used without previous .cfi_startproc"));
713       ignore_rest_of_line ();
714       return;
715     }
716
717   fde = frchain_now->frch_cfi_data->cur_fde_data;
718   encoding = get_absolute_expression ();
719   if (encoding == DW_EH_PE_omit)
720     {
721       demand_empty_rest_of_line ();
722       fde->lsda_encoding = encoding;
723       return;
724     }
725
726   if ((encoding & 0xff) != encoding
727       || ((encoding & 0x70) != 0
728 #if defined DIFF_EXPR_OK || defined tc_cfi_emit_pcrel_expr
729           && (encoding & 0x70) != DW_EH_PE_pcrel
730 #endif
731           )
732          /* leb128 can be handled, but does something actually need it?  */
733       || (encoding & 7) == DW_EH_PE_uleb128
734       || (encoding & 7) > DW_EH_PE_udata8)
735     {
736       as_bad (_("invalid or unsupported encoding in .cfi_lsda"));
737       ignore_rest_of_line ();
738       return;
739     }
740
741   if (*input_line_pointer++ != ',')
742     {
743       as_bad (_(".cfi_lsda requires encoding and symbol arguments"));
744       ignore_rest_of_line ();
745       return;
746     }
747
748   fde->lsda_encoding = encoding;
749
750   expression_and_evaluate (&fde->lsda);
751   switch (fde->lsda.X_op)
752     {
753     case O_symbol:
754       break;
755     case O_constant:
756       if ((encoding & 0x70) == DW_EH_PE_pcrel)
757         encoding = DW_EH_PE_omit;
758       break;
759     default:
760       encoding = DW_EH_PE_omit;
761       break;
762     }
763
764   fde->lsda_encoding = encoding;
765
766   if (encoding == DW_EH_PE_omit)
767     {
768       as_bad (_("wrong second argument to .cfi_lsda"));
769       ignore_rest_of_line ();
770       return;
771     }
772
773   demand_empty_rest_of_line ();
774 }
775
776 static void
777 dot_cfi_startproc (int ignored ATTRIBUTE_UNUSED)
778 {
779   int simple = 0;
780
781   if (frchain_now->frch_cfi_data != NULL)
782     {
783       as_bad (_("previous CFI entry not closed (missing .cfi_endproc)"));
784       ignore_rest_of_line ();
785       return;
786     }
787
788   cfi_new_fde (symbol_temp_new_now ());
789
790   SKIP_WHITESPACE ();
791   if (is_name_beginner (*input_line_pointer))
792     {
793       char *name, c;
794
795       name = input_line_pointer;
796       c = get_symbol_end ();
797
798       if (strcmp (name, "simple") == 0)
799         {
800           simple = 1;
801           *input_line_pointer = c;
802         }
803       else
804         input_line_pointer = name;
805     }
806   demand_empty_rest_of_line ();
807
808   frchain_now->frch_cfi_data->cur_cfa_offset = 0;
809   if (!simple)
810     tc_cfi_frame_initial_instructions ();
811 }
812
813 static void
814 dot_cfi_endproc (int ignored ATTRIBUTE_UNUSED)
815 {
816   if (frchain_now->frch_cfi_data == NULL)
817     {
818       as_bad (_(".cfi_endproc without corresponding .cfi_startproc"));
819       ignore_rest_of_line ();
820       return;
821     }
822
823   cfi_end_fde (symbol_temp_new_now ());
824
825   demand_empty_rest_of_line ();
826 }
827
828 \f
829 /* Emit a single byte into the current segment.  */
830
831 static inline void
832 out_one (int byte)
833 {
834   FRAG_APPEND_1_CHAR (byte);
835 }
836
837 /* Emit a two-byte word into the current segment.  */
838
839 static inline void
840 out_two (int data)
841 {
842   md_number_to_chars (frag_more (2), data, 2);
843 }
844
845 /* Emit a four byte word into the current segment.  */
846
847 static inline void
848 out_four (int data)
849 {
850   md_number_to_chars (frag_more (4), data, 4);
851 }
852
853 /* Emit an unsigned "little-endian base 128" number.  */
854
855 static void
856 out_uleb128 (addressT value)
857 {
858   output_leb128 (frag_more (sizeof_leb128 (value, 0)), value, 0);
859 }
860
861 /* Emit an unsigned "little-endian base 128" number.  */
862
863 static void
864 out_sleb128 (offsetT value)
865 {
866   output_leb128 (frag_more (sizeof_leb128 (value, 1)), value, 1);
867 }
868
869 static void
870 output_cfi_insn (struct cfi_insn_data *insn)
871 {
872   offsetT offset;
873   unsigned int regno;
874
875   switch (insn->insn)
876     {
877     case DW_CFA_advance_loc:
878       {
879         symbolS *from = insn->u.ll.lab1;
880         symbolS *to = insn->u.ll.lab2;
881
882         if (symbol_get_frag (to) == symbol_get_frag (from))
883           {
884             addressT delta = S_GET_VALUE (to) - S_GET_VALUE (from);
885             addressT scaled = delta / DWARF2_LINE_MIN_INSN_LENGTH;
886
887             if (scaled <= 0x3F)
888               out_one (DW_CFA_advance_loc + scaled);
889             else if (delta <= 0xFF)
890               {
891                 out_one (DW_CFA_advance_loc1);
892                 out_one (delta);
893               }
894             else if (delta <= 0xFFFF)
895               {
896                 out_one (DW_CFA_advance_loc2);
897                 out_two (delta);
898               }
899             else
900               {
901                 out_one (DW_CFA_advance_loc4);
902                 out_four (delta);
903               }
904           }
905         else
906           {
907             expressionS exp;
908
909             exp.X_op = O_subtract;
910             exp.X_add_symbol = to;
911             exp.X_op_symbol = from;
912             exp.X_add_number = 0;
913
914             /* The code in ehopt.c expects that one byte of the encoding
915                is already allocated to the frag.  This comes from the way
916                that it scans the .eh_frame section looking first for the
917                .byte DW_CFA_advance_loc4.  */
918             frag_more (1);
919
920             frag_var (rs_cfa, 4, 0, DWARF2_LINE_MIN_INSN_LENGTH << 3,
921                       make_expr_symbol (&exp), frag_now_fix () - 1,
922                       (char *) frag_now);
923           }
924       }
925       break;
926
927     case DW_CFA_def_cfa:
928       offset = insn->u.ri.offset;
929       if (offset < 0)
930         {
931           out_one (DW_CFA_def_cfa_sf);
932           out_uleb128 (insn->u.ri.reg);
933           out_sleb128 (offset / DWARF2_CIE_DATA_ALIGNMENT);
934         }
935       else
936         {
937           out_one (DW_CFA_def_cfa);
938           out_uleb128 (insn->u.ri.reg);
939           out_uleb128 (offset);
940         }
941       break;
942
943     case DW_CFA_def_cfa_register:
944     case DW_CFA_undefined:
945     case DW_CFA_same_value:
946       out_one (insn->insn);
947       out_uleb128 (insn->u.r);
948       break;
949
950     case DW_CFA_def_cfa_offset:
951       offset = insn->u.i;
952       if (offset < 0)
953         {
954           out_one (DW_CFA_def_cfa_offset_sf);
955           out_sleb128 (offset / DWARF2_CIE_DATA_ALIGNMENT);
956         }
957       else
958         {
959           out_one (DW_CFA_def_cfa_offset);
960           out_uleb128 (offset);
961         }
962       break;
963
964     case DW_CFA_restore:
965       regno = insn->u.r;
966       if (regno <= 0x3F)
967         {
968           out_one (DW_CFA_restore + regno);
969         }
970       else
971         {
972           out_one (DW_CFA_restore_extended);
973           out_uleb128 (regno);
974         }
975       break;
976
977     case DW_CFA_offset:
978       regno = insn->u.ri.reg;
979       offset = insn->u.ri.offset / DWARF2_CIE_DATA_ALIGNMENT;
980       if (offset < 0)
981         {
982           out_one (DW_CFA_offset_extended_sf);
983           out_uleb128 (regno);
984           out_sleb128 (offset);
985         }
986       else if (regno <= 0x3F)
987         {
988           out_one (DW_CFA_offset + regno);
989           out_uleb128 (offset);
990         }
991       else
992         {
993           out_one (DW_CFA_offset_extended);
994           out_uleb128 (regno);
995           out_uleb128 (offset);
996         }
997       break;
998
999     case DW_CFA_register:
1000       out_one (DW_CFA_register);
1001       out_uleb128 (insn->u.rr.reg1);
1002       out_uleb128 (insn->u.rr.reg2);
1003       break;
1004
1005     case DW_CFA_remember_state:
1006     case DW_CFA_restore_state:
1007       out_one (insn->insn);
1008       break;
1009
1010     case DW_CFA_GNU_window_save:
1011       out_one (DW_CFA_GNU_window_save);
1012       break;
1013
1014     case CFI_escape:
1015       {
1016         struct cfi_escape_data *e;
1017         for (e = insn->u.esc; e ; e = e->next)
1018           emit_expr (&e->exp, 1);
1019         break;
1020       }
1021
1022     default:
1023       abort ();
1024     }
1025 }
1026
1027 static offsetT
1028 encoding_size (unsigned char encoding)
1029 {
1030   if (encoding == DW_EH_PE_omit)
1031     return 0;
1032   switch (encoding & 0x7)
1033     {
1034     case 0:
1035       return bfd_get_arch_size (stdoutput) == 64 ? 8 : 4;
1036     case DW_EH_PE_udata2:
1037       return 2;
1038     case DW_EH_PE_udata4:
1039       return 4;
1040     case DW_EH_PE_udata8:
1041       return 8;
1042     default:
1043       abort ();
1044     }
1045 }
1046
1047 static void
1048 output_cie (struct cie_entry *cie)
1049 {
1050   symbolS *after_size_address, *end_address;
1051   expressionS exp;
1052   struct cfi_insn_data *i;
1053   offsetT augmentation_size;
1054   int enc;
1055
1056   cie->start_address = symbol_temp_new_now ();
1057   after_size_address = symbol_temp_make ();
1058   end_address = symbol_temp_make ();
1059
1060   exp.X_op = O_subtract;
1061   exp.X_add_symbol = end_address;
1062   exp.X_op_symbol = after_size_address;
1063   exp.X_add_number = 0;
1064
1065   emit_expr (&exp, 4);                          /* Length.  */
1066   symbol_set_value_now (after_size_address);
1067   out_four (0);                                 /* CIE id.  */
1068   out_one (DW_CIE_VERSION);                     /* Version.  */
1069   out_one ('z');                                /* Augmentation.  */
1070   if (cie->per_encoding != DW_EH_PE_omit)
1071     out_one ('P');
1072   if (cie->lsda_encoding != DW_EH_PE_omit)
1073     out_one ('L');
1074   out_one ('R');
1075   if (cie->signal_frame)
1076     out_one ('S');
1077   out_one (0);
1078   out_uleb128 (DWARF2_LINE_MIN_INSN_LENGTH);    /* Code alignment.  */
1079   out_sleb128 (DWARF2_CIE_DATA_ALIGNMENT);      /* Data alignment.  */
1080   if (DW_CIE_VERSION == 1)                      /* Return column.  */
1081     out_one (cie->return_column);
1082   else
1083     out_uleb128 (cie->return_column);
1084   augmentation_size = 1 + (cie->lsda_encoding != DW_EH_PE_omit);
1085   if (cie->per_encoding != DW_EH_PE_omit)
1086     augmentation_size += 1 + encoding_size (cie->per_encoding);
1087   out_uleb128 (augmentation_size);              /* Augmentation size.  */
1088   if (cie->per_encoding != DW_EH_PE_omit)
1089     {
1090       offsetT size = encoding_size (cie->per_encoding);
1091       out_one (cie->per_encoding);
1092       exp = cie->personality;
1093       if ((cie->per_encoding & 0x70) == DW_EH_PE_pcrel)
1094         {
1095 #ifdef DIFF_EXPR_OK
1096           exp.X_op = O_subtract;
1097           exp.X_op_symbol = symbol_temp_new_now ();
1098           emit_expr (&exp, size);
1099 #elif defined (tc_cfi_emit_pcrel_expr)
1100           tc_cfi_emit_pcrel_expr (&exp, size);
1101 #else
1102           abort ();
1103 #endif
1104         }
1105       else
1106         emit_expr (&exp, size);
1107     }
1108   if (cie->lsda_encoding != DW_EH_PE_omit)
1109     out_one (cie->lsda_encoding);
1110
1111   switch (DWARF2_FDE_RELOC_SIZE)
1112     {
1113     case 2:
1114       enc = DW_EH_PE_sdata2;
1115       break;
1116     case 4:
1117       enc = DW_EH_PE_sdata4;
1118       break;
1119     case 8:
1120       enc = DW_EH_PE_sdata8;
1121       break;
1122     default:
1123       abort ();
1124     }
1125 #if defined DIFF_EXPR_OK || defined tc_cfi_emit_pcrel_expr
1126   enc |= DW_EH_PE_pcrel;
1127 #endif
1128   out_one (enc);
1129
1130   if (cie->first)
1131     for (i = cie->first; i != cie->last; i = i->next)
1132       output_cfi_insn (i);
1133
1134   frag_align (2, DW_CFA_nop, 0);
1135   symbol_set_value_now (end_address);
1136 }
1137
1138 static void
1139 output_fde (struct fde_entry *fde, struct cie_entry *cie,
1140             struct cfi_insn_data *first, int align)
1141 {
1142   symbolS *after_size_address, *end_address;
1143   expressionS exp;
1144   offsetT augmentation_size;
1145
1146   after_size_address = symbol_temp_make ();
1147   end_address = symbol_temp_make ();
1148
1149   exp.X_op = O_subtract;
1150   exp.X_add_symbol = end_address;
1151   exp.X_op_symbol = after_size_address;
1152   exp.X_add_number = 0;
1153   emit_expr (&exp, 4);                          /* Length.  */
1154   symbol_set_value_now (after_size_address);
1155
1156   exp.X_add_symbol = after_size_address;
1157   exp.X_op_symbol = cie->start_address;
1158   emit_expr (&exp, 4);                          /* CIE offset.  */
1159
1160 #ifdef DIFF_EXPR_OK
1161   exp.X_add_symbol = fde->start_address;
1162   exp.X_op_symbol = symbol_temp_new_now ();
1163   emit_expr (&exp, DWARF2_FDE_RELOC_SIZE);      /* Code offset.  */
1164 #else
1165   exp.X_op = O_symbol;
1166   exp.X_add_symbol = fde->start_address;
1167   exp.X_op_symbol = NULL;
1168 #ifdef tc_cfi_emit_pcrel_expr
1169   tc_cfi_emit_pcrel_expr (&exp, DWARF2_FDE_RELOC_SIZE);  /* Code offset.  */
1170 #else
1171   emit_expr (&exp, DWARF2_FDE_RELOC_SIZE);      /* Code offset.  */
1172 #endif
1173   exp.X_op = O_subtract;
1174 #endif
1175
1176   exp.X_add_symbol = fde->end_address;
1177   exp.X_op_symbol = fde->start_address;         /* Code length.  */
1178   emit_expr (&exp, DWARF2_FDE_RELOC_SIZE);
1179
1180   augmentation_size = encoding_size (fde->lsda_encoding);
1181   out_uleb128 (augmentation_size);              /* Augmentation size.  */
1182
1183   if (fde->lsda_encoding != DW_EH_PE_omit)
1184     {
1185       exp = fde->lsda;
1186       if ((fde->lsda_encoding & 0x70) == DW_EH_PE_pcrel)
1187         {
1188 #ifdef DIFF_EXPR_OK
1189           exp.X_op = O_subtract;
1190           exp.X_op_symbol = symbol_temp_new_now ();
1191           emit_expr (&exp, augmentation_size);
1192 #elif defined (tc_cfi_emit_pcrel_expr)
1193           tc_cfi_emit_pcrel_expr (&exp, augmentation_size);
1194 #else
1195           abort ();
1196 #endif
1197         }
1198       else
1199         emit_expr (&exp, augmentation_size);
1200     }
1201
1202   for (; first; first = first->next)
1203     output_cfi_insn (first);
1204
1205   frag_align (align, DW_CFA_nop, 0);
1206   symbol_set_value_now (end_address);
1207 }
1208
1209 static struct cie_entry *
1210 select_cie_for_fde (struct fde_entry *fde, struct cfi_insn_data **pfirst)
1211 {
1212   struct cfi_insn_data *i, *j;
1213   struct cie_entry *cie;
1214
1215   for (cie = cie_root; cie; cie = cie->next)
1216     {
1217       if (cie->return_column != fde->return_column
1218           || cie->signal_frame != fde->signal_frame
1219           || cie->per_encoding != fde->per_encoding
1220           || cie->lsda_encoding != fde->lsda_encoding)
1221         continue;
1222       if (cie->per_encoding != DW_EH_PE_omit)
1223         {
1224           if (cie->personality.X_op != fde->personality.X_op
1225               || cie->personality.X_add_number
1226                  != fde->personality.X_add_number)
1227             continue;
1228           switch (cie->personality.X_op)
1229             {
1230             case O_constant:
1231               if (cie->personality.X_unsigned != fde->personality.X_unsigned)
1232                 continue;
1233               break;
1234             case O_symbol:
1235               if (cie->personality.X_add_symbol
1236                   != fde->personality.X_add_symbol)
1237                 continue;
1238               break;
1239             default:
1240               abort ();
1241             }
1242         }
1243       for (i = cie->first, j = fde->data;
1244            i != cie->last && j != NULL;
1245            i = i->next, j = j->next)
1246         {
1247           if (i->insn != j->insn)
1248             goto fail;
1249           switch (i->insn)
1250             {
1251             case DW_CFA_advance_loc:
1252             case DW_CFA_remember_state:
1253               /* We reached the first advance/remember in the FDE,
1254                  but did not reach the end of the CIE list.  */
1255               goto fail;
1256
1257             case DW_CFA_offset:
1258             case DW_CFA_def_cfa:
1259               if (i->u.ri.reg != j->u.ri.reg)
1260                 goto fail;
1261               if (i->u.ri.offset != j->u.ri.offset)
1262                 goto fail;
1263               break;
1264
1265             case DW_CFA_register:
1266               if (i->u.rr.reg1 != j->u.rr.reg1)
1267                 goto fail;
1268               if (i->u.rr.reg2 != j->u.rr.reg2)
1269                 goto fail;
1270               break;
1271
1272             case DW_CFA_def_cfa_register:
1273             case DW_CFA_restore:
1274             case DW_CFA_undefined:
1275             case DW_CFA_same_value:
1276               if (i->u.r != j->u.r)
1277                 goto fail;
1278               break;
1279
1280             case DW_CFA_def_cfa_offset:
1281               if (i->u.i != j->u.i)
1282                 goto fail;
1283               break;
1284
1285             case CFI_escape:
1286               /* Don't bother matching these for now.  */
1287               goto fail;
1288
1289             default:
1290               abort ();
1291             }
1292         }
1293
1294       /* Success if we reached the end of the CIE list, and we've either
1295          run out of FDE entries or we've encountered an advance,
1296          remember, or escape.  */
1297       if (i == cie->last
1298           && (!j
1299               || j->insn == DW_CFA_advance_loc
1300               || j->insn == DW_CFA_remember_state
1301               || j->insn == CFI_escape))
1302         {
1303           *pfirst = j;
1304           return cie;
1305         }
1306
1307     fail:;
1308     }
1309
1310   cie = xmalloc (sizeof (struct cie_entry));
1311   cie->next = cie_root;
1312   cie_root = cie;
1313   cie->return_column = fde->return_column;
1314   cie->signal_frame = fde->signal_frame;
1315   cie->per_encoding = fde->per_encoding;
1316   cie->lsda_encoding = fde->lsda_encoding;
1317   cie->personality = fde->personality;
1318   cie->first = fde->data;
1319
1320   for (i = cie->first; i ; i = i->next)
1321     if (i->insn == DW_CFA_advance_loc
1322         || i->insn == DW_CFA_remember_state
1323         || i->insn == CFI_escape)
1324       break;
1325
1326   cie->last = i;
1327   *pfirst = i;
1328    
1329   output_cie (cie);
1330
1331   return cie;
1332 }
1333
1334 void
1335 cfi_finish (void)
1336 {
1337   segT cfi_seg;
1338   struct fde_entry *fde;
1339   int save_flag_traditional_format;
1340
1341   if (all_fde_data == 0)
1342     return;
1343
1344   /* Open .eh_frame section.  */
1345   cfi_seg = subseg_new (".eh_frame", 0);
1346   bfd_set_section_flags (stdoutput, cfi_seg,
1347                          SEC_ALLOC | SEC_LOAD | SEC_DATA
1348                          | DWARF2_EH_FRAME_READ_ONLY);
1349   subseg_set (cfi_seg, 0);
1350   record_alignment (cfi_seg, EH_FRAME_ALIGNMENT);
1351
1352   /* Make sure check_eh_frame doesn't do anything with our output.  */
1353   save_flag_traditional_format = flag_traditional_format;
1354   flag_traditional_format = 1;
1355
1356   for (fde = all_fde_data; fde ; fde = fde->next)
1357     {
1358       struct cfi_insn_data *first;
1359       struct cie_entry *cie;
1360
1361       if (fde->end_address == NULL)
1362         {
1363           as_bad (_("open CFI at the end of file; missing .cfi_endproc directive"));
1364           fde->end_address = fde->start_address;
1365         }
1366
1367       cie = select_cie_for_fde (fde, &first);
1368       output_fde (fde, cie, first, fde->next == NULL ? EH_FRAME_ALIGNMENT : 2);
1369     }
1370
1371   flag_traditional_format = save_flag_traditional_format;
1372 }
1373
1374 #else /* TARGET_USE_CFIPOP */
1375 void
1376 cfi_finish (void)
1377 {
1378 }
1379 #endif /* TARGET_USE_CFIPOP */