-Wimplicit-fallthrough noreturn fixes
[external/binutils.git] / gas / dw2gencfi.c
1 /* dw2gencfi.c - Support for generating Dwarf2 CFI information.
2    Copyright (C) 2003-2016 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 #include "dwarf2dbg.h"
26
27 #ifdef TARGET_USE_CFIPOP
28
29 /* By default, use difference expressions if DIFF_EXPR_OK is defined.  */
30 #ifndef CFI_DIFF_EXPR_OK
31 # ifdef DIFF_EXPR_OK
32 #  define CFI_DIFF_EXPR_OK 1
33 # else
34 #  define CFI_DIFF_EXPR_OK 0
35 # endif
36 #endif
37
38 #ifndef CFI_DIFF_LSDA_OK
39 #define CFI_DIFF_LSDA_OK CFI_DIFF_EXPR_OK
40 #endif
41
42 #if CFI_DIFF_EXPR_OK == 1 && CFI_DIFF_LSDA_OK == 0
43 # error "CFI_DIFF_EXPR_OK should imply CFI_DIFF_LSDA_OK"
44 #endif
45
46 /* We re-use DWARF2_LINE_MIN_INSN_LENGTH for the code alignment field
47    of the CIE.  Default to 1 if not otherwise specified.  */
48 #ifndef DWARF2_LINE_MIN_INSN_LENGTH
49 #define DWARF2_LINE_MIN_INSN_LENGTH 1
50 #endif
51
52 /* By default, use 32-bit relocations from .eh_frame into .text.  */
53 #ifndef DWARF2_FDE_RELOC_SIZE
54 #define DWARF2_FDE_RELOC_SIZE 4
55 #endif
56
57 /* By default, use a read-only .eh_frame section.  */
58 #ifndef DWARF2_EH_FRAME_READ_ONLY
59 #define DWARF2_EH_FRAME_READ_ONLY SEC_READONLY
60 #endif
61
62 #ifndef EH_FRAME_ALIGNMENT
63 #define EH_FRAME_ALIGNMENT (bfd_get_arch_size (stdoutput) == 64 ? 3 : 2)
64 #endif
65
66 #ifndef tc_cfi_frame_initial_instructions
67 #define tc_cfi_frame_initial_instructions() ((void)0)
68 #endif
69
70 #ifndef tc_cfi_startproc
71 # define tc_cfi_startproc() ((void)0)
72 #endif
73
74 #ifndef tc_cfi_endproc
75 # define tc_cfi_endproc(fde) ((void) (fde))
76 #endif
77
78 #define EH_FRAME_LINKONCE (SUPPORT_FRAME_LINKONCE || compact_eh)
79
80 #ifndef DWARF2_FORMAT
81 #define DWARF2_FORMAT(SEC) dwarf2_format_32bit
82 #endif
83
84 #ifndef DWARF2_ADDR_SIZE
85 #define DWARF2_ADDR_SIZE(bfd) (bfd_arch_bits_per_address (bfd) / 8)
86 #endif
87
88 #if MULTIPLE_FRAME_SECTIONS
89 #define CUR_SEG(structp) structp->cur_seg
90 #define SET_CUR_SEG(structp, seg) structp->cur_seg = seg
91 #define HANDLED(structp) structp->handled
92 #define SET_HANDLED(structp, val) structp->handled = val
93 #else
94 #define CUR_SEG(structp) NULL
95 #define SET_CUR_SEG(structp, seg) (void) (0 && seg)
96 #define HANDLED(structp) 0
97 #define SET_HANDLED(structp, val) (void) (0 && val)
98 #endif
99
100 #ifndef tc_cfi_reloc_for_encoding
101 #define tc_cfi_reloc_for_encoding(e) BFD_RELOC_NONE
102 #endif
103
104 /* Private segment collection list.  */
105 struct dwcfi_seg_list
106 {
107   segT   seg;
108   int    subseg;
109   char * seg_name;
110 };
111
112 #ifdef SUPPORT_COMPACT_EH
113 static bfd_boolean compact_eh;
114 #else
115 #define compact_eh 0
116 #endif
117
118 static struct hash_control *dwcfi_hash;
119 \f
120 /* Emit a single byte into the current segment.  */
121
122 static inline void
123 out_one (int byte)
124 {
125   FRAG_APPEND_1_CHAR (byte);
126 }
127
128 /* Emit a two-byte word into the current segment.  */
129
130 static inline void
131 out_two (int data)
132 {
133   md_number_to_chars (frag_more (2), data, 2);
134 }
135
136 /* Emit a four byte word into the current segment.  */
137
138 static inline void
139 out_four (int data)
140 {
141   md_number_to_chars (frag_more (4), data, 4);
142 }
143
144 /* Emit an unsigned "little-endian base 128" number.  */
145
146 static void
147 out_uleb128 (addressT value)
148 {
149   output_leb128 (frag_more (sizeof_leb128 (value, 0)), value, 0);
150 }
151
152 /* Emit an unsigned "little-endian base 128" number.  */
153
154 static void
155 out_sleb128 (offsetT value)
156 {
157   output_leb128 (frag_more (sizeof_leb128 (value, 1)), value, 1);
158 }
159
160 static offsetT
161 encoding_size (unsigned char encoding)
162 {
163   if (encoding == DW_EH_PE_omit)
164     return 0;
165   switch (encoding & 0x7)
166     {
167     case 0:
168       return bfd_get_arch_size (stdoutput) == 64 ? 8 : 4;
169     case DW_EH_PE_udata2:
170       return 2;
171     case DW_EH_PE_udata4:
172       return 4;
173     case DW_EH_PE_udata8:
174       return 8;
175     default:
176       abort ();
177     }
178 }
179
180 /* Emit expression EXP in ENCODING.  If EMIT_ENCODING is true, first
181    emit a byte containing ENCODING.  */
182
183 static void
184 emit_expr_encoded (expressionS *exp, int encoding, bfd_boolean emit_encoding)
185 {
186   offsetT size = encoding_size (encoding);
187   bfd_reloc_code_real_type code;
188
189   if (encoding == DW_EH_PE_omit)
190     return;
191
192   if (emit_encoding)
193     out_one (encoding);
194
195   code = tc_cfi_reloc_for_encoding (encoding);
196   if (code != BFD_RELOC_NONE)
197     {
198       reloc_howto_type *howto = bfd_reloc_type_lookup (stdoutput, code);
199       char *p = frag_more (size);
200       md_number_to_chars (p, 0, size);
201       fix_new (frag_now, p - frag_now->fr_literal, size, exp->X_add_symbol,
202                exp->X_add_number, howto->pc_relative, code);
203     }
204   else if ((encoding & 0x70) == DW_EH_PE_pcrel)
205     {
206 #if CFI_DIFF_EXPR_OK
207       expressionS tmp = *exp;
208       tmp.X_op = O_subtract;
209       tmp.X_op_symbol = symbol_temp_new_now ();
210       emit_expr (&tmp, size);
211 #elif defined (tc_cfi_emit_pcrel_expr)
212       tc_cfi_emit_pcrel_expr (exp, size);
213 #else
214       abort ();
215 #endif
216     }
217   else
218     emit_expr (exp, size);
219 }
220 \f
221 /* Build based on segment the derived .debug_...
222    segment name containing origin segment's postfix name part.  */
223
224 static char *
225 get_debugseg_name (segT seg, const char *base_name)
226 {
227   const char *name;
228
229   if (!seg)
230     name = "";
231   else
232     {
233       const char * dollar;
234       const char * dot;
235
236       name = bfd_get_section_name (stdoutput, seg);
237
238       dollar = strchr (name, '$');
239       dot = strchr (name + 1, '.');
240
241       if (!dollar && !dot)
242         {
243           if (!strcmp (base_name, ".eh_frame_entry")
244               && strcmp (name, ".text") != 0)
245             return concat (base_name, ".", name, NULL);
246
247           name = "";
248         }
249       else if (!dollar)
250         name = dot;
251       else if (!dot)
252         name = dollar;
253       else if (dot < dollar)
254         name = dot;
255       else
256         name = dollar;
257     }
258
259   return concat (base_name, name, NULL);
260 }
261
262 /* Allocate a dwcfi_seg_list structure.  */
263
264 static struct dwcfi_seg_list *
265 alloc_debugseg_item (segT seg, int subseg, char *name)
266 {
267   struct dwcfi_seg_list *r;
268
269   r = (struct dwcfi_seg_list *)
270     xmalloc (sizeof (struct dwcfi_seg_list) + strlen (name));
271   r->seg = seg;
272   r->subseg = subseg;
273   r->seg_name = name;
274   return r;
275 }
276
277 static segT
278 is_now_linkonce_segment (void)
279 {
280   if (compact_eh)
281     return now_seg;
282
283   if ((bfd_get_section_flags (stdoutput, now_seg)
284        & (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD
285           | SEC_LINK_DUPLICATES_ONE_ONLY | SEC_LINK_DUPLICATES_SAME_SIZE
286           | SEC_LINK_DUPLICATES_SAME_CONTENTS)) != 0)
287     return now_seg;
288   return NULL;
289 }
290
291 /* Generate debug... segment with same linkonce properties
292    of based segment.  */
293
294 static segT
295 make_debug_seg (segT cseg, char *name, int sflags)
296 {
297   segT save_seg = now_seg;
298   int save_subseg = now_subseg;
299   segT r;
300   flagword flags;
301
302   r = subseg_new (name, 0);
303
304   /* Check if code segment is marked as linked once.  */
305   if (!cseg)
306     flags = 0;
307   else
308     flags = bfd_get_section_flags (stdoutput, cseg)
309       & (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD
310          | SEC_LINK_DUPLICATES_ONE_ONLY | SEC_LINK_DUPLICATES_SAME_SIZE
311          | SEC_LINK_DUPLICATES_SAME_CONTENTS);
312
313   /* Add standard section flags.  */
314   flags |= sflags;
315
316   /* Apply possibly linked once flags to new generated segment, too.  */
317   if (!bfd_set_section_flags (stdoutput, r, flags))
318     as_bad (_("bfd_set_section_flags: %s"),
319             bfd_errmsg (bfd_get_error ()));
320
321   /* Restore to previous segment.  */
322   if (save_seg != NULL)
323     subseg_set (save_seg, save_subseg);
324   return r;
325 }
326
327 static void
328 dwcfi_hash_insert (const char *name, struct dwcfi_seg_list *item)
329 {
330   const char *error_string;
331
332   if ((error_string = hash_jam (dwcfi_hash, name, (char *) item)))
333     as_fatal (_("Inserting \"%s\" into structure table failed: %s"),
334               name, error_string);
335 }
336
337 static struct dwcfi_seg_list *
338 dwcfi_hash_find (char *name)
339 {
340   return (struct dwcfi_seg_list *) hash_find (dwcfi_hash, name);
341 }
342
343 static struct dwcfi_seg_list *
344 dwcfi_hash_find_or_make (segT cseg, const char *base_name, int flags)
345 {
346   struct dwcfi_seg_list *item;
347   char *name;
348
349   /* Initialize dwcfi_hash once.  */
350   if (!dwcfi_hash)
351     dwcfi_hash = hash_new ();
352
353   name = get_debugseg_name (cseg, base_name);
354
355   item = dwcfi_hash_find (name);
356   if (!item)
357     {
358       item = alloc_debugseg_item (make_debug_seg (cseg, name, flags), 0, name);
359
360       dwcfi_hash_insert (item->seg_name, item);
361     }
362   else
363     free (name);
364
365   return item;
366 }
367
368 /* ??? Share this with dwarf2cfg.c.  */
369 #ifndef TC_DWARF2_EMIT_OFFSET
370 #define TC_DWARF2_EMIT_OFFSET  generic_dwarf2_emit_offset
371
372 /* Create an offset to .dwarf2_*.  */
373
374 static void
375 generic_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
376 {
377   expressionS exp;
378
379   exp.X_op = O_symbol;
380   exp.X_add_symbol = symbol;
381   exp.X_add_number = 0;
382   emit_expr (&exp, size);
383 }
384 #endif
385
386 struct cfi_escape_data
387 {
388   struct cfi_escape_data *next;
389   expressionS exp;
390 };
391
392 struct cie_entry
393 {
394   struct cie_entry *next;
395 #if MULTIPLE_FRAME_SECTIONS
396   segT cur_seg;
397 #endif
398   symbolS *start_address;
399   unsigned int return_column;
400   unsigned int signal_frame;
401   unsigned char fde_encoding;
402   unsigned char per_encoding;
403   unsigned char lsda_encoding;
404   expressionS personality;
405   struct cfi_insn_data *first, *last;
406 };
407
408 /* List of FDE entries.  */
409
410 struct fde_entry *all_fde_data;
411 static struct fde_entry **last_fde_data = &all_fde_data;
412
413 /* List of CIEs so that they could be reused.  */
414 static struct cie_entry *cie_root;
415
416 /* Stack of old CFI data, for save/restore.  */
417 struct cfa_save_data
418 {
419   struct cfa_save_data *next;
420   offsetT cfa_offset;
421 };
422
423 /* Current open FDE entry.  */
424 struct frch_cfi_data
425 {
426   struct fde_entry *cur_fde_data;
427   symbolS *last_address;
428   offsetT cur_cfa_offset;
429   struct cfa_save_data *cfa_save_stack;
430 };
431 \f
432 /* Construct a new FDE structure and add it to the end of the fde list.  */
433
434 static struct fde_entry *
435 alloc_fde_entry (void)
436 {
437   struct fde_entry *fde = XCNEW (struct fde_entry);
438
439   frchain_now->frch_cfi_data = XCNEW (struct frch_cfi_data);
440   frchain_now->frch_cfi_data->cur_fde_data = fde;
441   *last_fde_data = fde;
442   last_fde_data = &fde->next;
443   SET_CUR_SEG (fde, is_now_linkonce_segment ());
444   SET_HANDLED (fde, 0);
445   fde->last = &fde->data;
446   fde->return_column = DWARF2_DEFAULT_RETURN_COLUMN;
447   fde->per_encoding = DW_EH_PE_omit;
448   fde->lsda_encoding = DW_EH_PE_omit;
449   fde->eh_header_type = EH_COMPACT_UNKNOWN;
450
451   return fde;
452 }
453
454 /* The following functions are available for a backend to construct its
455    own unwind information, usually from legacy unwind directives.  */
456
457 /* Construct a new INSN structure and add it to the end of the insn list
458    for the currently active FDE.  */
459
460 static bfd_boolean cfi_sections_set = FALSE;
461 static int cfi_sections = CFI_EMIT_eh_frame;
462 int all_cfi_sections = 0;
463 static struct fde_entry *last_fde;
464
465 static struct cfi_insn_data *
466 alloc_cfi_insn_data (void)
467 {
468   struct cfi_insn_data *insn = XCNEW (struct cfi_insn_data);
469   struct fde_entry *cur_fde_data = frchain_now->frch_cfi_data->cur_fde_data;
470
471   *cur_fde_data->last = insn;
472   cur_fde_data->last = &insn->next;
473   SET_CUR_SEG (insn, is_now_linkonce_segment ());
474   return insn;
475 }
476
477 /* Construct a new FDE structure that begins at LABEL.  */
478
479 void
480 cfi_new_fde (symbolS *label)
481 {
482   struct fde_entry *fde = alloc_fde_entry ();
483   fde->start_address = label;
484   frchain_now->frch_cfi_data->last_address = label;
485 }
486
487 /* End the currently open FDE.  */
488
489 void
490 cfi_end_fde (symbolS *label)
491 {
492   frchain_now->frch_cfi_data->cur_fde_data->end_address = label;
493   free (frchain_now->frch_cfi_data);
494   frchain_now->frch_cfi_data = NULL;
495 }
496
497 /* Set the return column for the current FDE.  */
498
499 void
500 cfi_set_return_column (unsigned regno)
501 {
502   frchain_now->frch_cfi_data->cur_fde_data->return_column = regno;
503 }
504
505 void
506 cfi_set_sections (void)
507 {
508   frchain_now->frch_cfi_data->cur_fde_data->sections = all_cfi_sections;
509   cfi_sections_set = TRUE;
510 }
511
512 /* Universal functions to store new instructions.  */
513
514 static void
515 cfi_add_CFA_insn (int insn)
516 {
517   struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
518
519   insn_ptr->insn = insn;
520 }
521
522 static void
523 cfi_add_CFA_insn_reg (int insn, unsigned regno)
524 {
525   struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
526
527   insn_ptr->insn = insn;
528   insn_ptr->u.r = regno;
529 }
530
531 static void
532 cfi_add_CFA_insn_offset (int insn, offsetT offset)
533 {
534   struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
535
536   insn_ptr->insn = insn;
537   insn_ptr->u.i = offset;
538 }
539
540 static void
541 cfi_add_CFA_insn_reg_reg (int insn, unsigned reg1, unsigned reg2)
542 {
543   struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
544
545   insn_ptr->insn = insn;
546   insn_ptr->u.rr.reg1 = reg1;
547   insn_ptr->u.rr.reg2 = reg2;
548 }
549
550 static void
551 cfi_add_CFA_insn_reg_offset (int insn, unsigned regno, offsetT offset)
552 {
553   struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
554
555   insn_ptr->insn = insn;
556   insn_ptr->u.ri.reg = regno;
557   insn_ptr->u.ri.offset = offset;
558 }
559
560 /* Add a CFI insn to advance the PC from the last address to LABEL.  */
561
562 void
563 cfi_add_advance_loc (symbolS *label)
564 {
565   struct cfi_insn_data *insn = alloc_cfi_insn_data ();
566
567   insn->insn = DW_CFA_advance_loc;
568   insn->u.ll.lab1 = frchain_now->frch_cfi_data->last_address;
569   insn->u.ll.lab2 = label;
570
571   frchain_now->frch_cfi_data->last_address = label;
572 }
573
574 /* Add a CFI insn to label the current position in the CFI segment.  */
575
576 void
577 cfi_add_label (const char *name)
578 {
579   unsigned int len = strlen (name) + 1;
580   struct cfi_insn_data *insn = alloc_cfi_insn_data ();
581
582   insn->insn = CFI_label;
583   obstack_grow (&notes, name, len);
584   insn->u.sym_name = (char *) obstack_finish (&notes);
585 }
586
587 /* Add a DW_CFA_offset record to the CFI data.  */
588
589 void
590 cfi_add_CFA_offset (unsigned regno, offsetT offset)
591 {
592   unsigned int abs_data_align;
593
594   gas_assert (DWARF2_CIE_DATA_ALIGNMENT != 0);
595   cfi_add_CFA_insn_reg_offset (DW_CFA_offset, regno, offset);
596
597   abs_data_align = (DWARF2_CIE_DATA_ALIGNMENT < 0
598                     ? -DWARF2_CIE_DATA_ALIGNMENT : DWARF2_CIE_DATA_ALIGNMENT);
599   if (offset % abs_data_align)
600     as_bad (_("register save offset not a multiple of %u"), abs_data_align);
601 }
602
603 /* Add a DW_CFA_val_offset record to the CFI data.  */
604
605 void
606 cfi_add_CFA_val_offset (unsigned regno, offsetT offset)
607 {
608   unsigned int abs_data_align;
609
610   gas_assert (DWARF2_CIE_DATA_ALIGNMENT != 0);
611   cfi_add_CFA_insn_reg_offset (DW_CFA_val_offset, regno, offset);
612
613   abs_data_align = (DWARF2_CIE_DATA_ALIGNMENT < 0
614                     ? -DWARF2_CIE_DATA_ALIGNMENT : DWARF2_CIE_DATA_ALIGNMENT);
615   if (offset % abs_data_align)
616     as_bad (_("register save offset not a multiple of %u"), abs_data_align);
617 }
618
619 /* Add a DW_CFA_def_cfa record to the CFI data.  */
620
621 void
622 cfi_add_CFA_def_cfa (unsigned regno, offsetT offset)
623 {
624   cfi_add_CFA_insn_reg_offset (DW_CFA_def_cfa, regno, offset);
625   frchain_now->frch_cfi_data->cur_cfa_offset = offset;
626 }
627
628 /* Add a DW_CFA_register record to the CFI data.  */
629
630 void
631 cfi_add_CFA_register (unsigned reg1, unsigned reg2)
632 {
633   cfi_add_CFA_insn_reg_reg (DW_CFA_register, reg1, reg2);
634 }
635
636 /* Add a DW_CFA_def_cfa_register record to the CFI data.  */
637
638 void
639 cfi_add_CFA_def_cfa_register (unsigned regno)
640 {
641   cfi_add_CFA_insn_reg (DW_CFA_def_cfa_register, regno);
642 }
643
644 /* Add a DW_CFA_def_cfa_offset record to the CFI data.  */
645
646 void
647 cfi_add_CFA_def_cfa_offset (offsetT offset)
648 {
649   cfi_add_CFA_insn_offset (DW_CFA_def_cfa_offset, offset);
650   frchain_now->frch_cfi_data->cur_cfa_offset = offset;
651 }
652
653 void
654 cfi_add_CFA_restore (unsigned regno)
655 {
656   cfi_add_CFA_insn_reg (DW_CFA_restore, regno);
657 }
658
659 void
660 cfi_add_CFA_undefined (unsigned regno)
661 {
662   cfi_add_CFA_insn_reg (DW_CFA_undefined, regno);
663 }
664
665 void
666 cfi_add_CFA_same_value (unsigned regno)
667 {
668   cfi_add_CFA_insn_reg (DW_CFA_same_value, regno);
669 }
670
671 void
672 cfi_add_CFA_remember_state (void)
673 {
674   struct cfa_save_data *p;
675
676   cfi_add_CFA_insn (DW_CFA_remember_state);
677
678   p = XNEW (struct cfa_save_data);
679   p->cfa_offset = frchain_now->frch_cfi_data->cur_cfa_offset;
680   p->next = frchain_now->frch_cfi_data->cfa_save_stack;
681   frchain_now->frch_cfi_data->cfa_save_stack = p;
682 }
683
684 void
685 cfi_add_CFA_restore_state (void)
686 {
687   struct cfa_save_data *p;
688
689   cfi_add_CFA_insn (DW_CFA_restore_state);
690
691   p = frchain_now->frch_cfi_data->cfa_save_stack;
692   if (p)
693     {
694       frchain_now->frch_cfi_data->cur_cfa_offset = p->cfa_offset;
695       frchain_now->frch_cfi_data->cfa_save_stack = p->next;
696       free (p);
697     }
698   else
699     as_bad (_("CFI state restore without previous remember"));
700 }
701
702 \f
703 /* Parse CFI assembler directives.  */
704
705 static void dot_cfi (int);
706 static void dot_cfi_escape (int);
707 static void dot_cfi_sections (int);
708 static void dot_cfi_startproc (int);
709 static void dot_cfi_endproc (int);
710 static void dot_cfi_fde_data (int);
711 static void dot_cfi_personality (int);
712 static void dot_cfi_personality_id (int);
713 static void dot_cfi_lsda (int);
714 static void dot_cfi_val_encoded_addr (int);
715 static void dot_cfi_inline_lsda (int);
716 static void dot_cfi_label (int);
717
718 const pseudo_typeS cfi_pseudo_table[] =
719   {
720     { "cfi_sections", dot_cfi_sections, 0 },
721     { "cfi_startproc", dot_cfi_startproc, 0 },
722     { "cfi_endproc", dot_cfi_endproc, 0 },
723     { "cfi_fde_data", dot_cfi_fde_data, 0 },
724     { "cfi_def_cfa", dot_cfi, DW_CFA_def_cfa },
725     { "cfi_def_cfa_register", dot_cfi, DW_CFA_def_cfa_register },
726     { "cfi_def_cfa_offset", dot_cfi, DW_CFA_def_cfa_offset },
727     { "cfi_adjust_cfa_offset", dot_cfi, CFI_adjust_cfa_offset },
728     { "cfi_offset", dot_cfi, DW_CFA_offset },
729     { "cfi_rel_offset", dot_cfi, CFI_rel_offset },
730     { "cfi_register", dot_cfi, DW_CFA_register },
731     { "cfi_return_column", dot_cfi, CFI_return_column },
732     { "cfi_restore", dot_cfi, DW_CFA_restore },
733     { "cfi_undefined", dot_cfi, DW_CFA_undefined },
734     { "cfi_same_value", dot_cfi, DW_CFA_same_value },
735     { "cfi_remember_state", dot_cfi, DW_CFA_remember_state },
736     { "cfi_restore_state", dot_cfi, DW_CFA_restore_state },
737     { "cfi_window_save", dot_cfi, DW_CFA_GNU_window_save },
738     { "cfi_escape", dot_cfi_escape, 0 },
739     { "cfi_signal_frame", dot_cfi, CFI_signal_frame },
740     { "cfi_personality", dot_cfi_personality, 0 },
741     { "cfi_personality_id", dot_cfi_personality_id, 0 },
742     { "cfi_lsda", dot_cfi_lsda, 0 },
743     { "cfi_val_encoded_addr", dot_cfi_val_encoded_addr, 0 },
744     { "cfi_inline_lsda", dot_cfi_inline_lsda, 0 },
745     { "cfi_label", dot_cfi_label, 0 },
746     { "cfi_val_offset", dot_cfi, DW_CFA_val_offset },
747     { NULL, NULL, 0 }
748   };
749
750 static void
751 cfi_parse_separator (void)
752 {
753   SKIP_WHITESPACE ();
754   if (*input_line_pointer == ',')
755     input_line_pointer++;
756   else
757     as_bad (_("missing separator"));
758 }
759
760 #ifndef tc_parse_to_dw2regnum
761 static void
762 tc_parse_to_dw2regnum (expressionS *exp)
763 {
764 # ifdef tc_regname_to_dw2regnum
765   SKIP_WHITESPACE ();
766   if (is_name_beginner (*input_line_pointer)
767       || (*input_line_pointer == '%'
768           && is_name_beginner (*++input_line_pointer)))
769     {
770       char *name, c;
771
772       c = get_symbol_name (& name);
773
774       exp->X_op = O_constant;
775       exp->X_add_number = tc_regname_to_dw2regnum (name);
776
777       restore_line_pointer (c);
778     }
779   else
780 # endif
781     expression_and_evaluate (exp);
782 }
783 #endif
784
785 static unsigned
786 cfi_parse_reg (void)
787 {
788   int regno;
789   expressionS exp;
790
791   tc_parse_to_dw2regnum (&exp);
792   switch (exp.X_op)
793     {
794     case O_register:
795     case O_constant:
796       regno = exp.X_add_number;
797       break;
798
799     default:
800       regno = -1;
801       break;
802     }
803
804   if (regno < 0)
805     {
806       as_bad (_("bad register expression"));
807       regno = 0;
808     }
809
810   return regno;
811 }
812
813 static offsetT
814 cfi_parse_const (void)
815 {
816   return get_absolute_expression ();
817 }
818
819 static void
820 dot_cfi (int arg)
821 {
822   offsetT offset;
823   unsigned reg1, reg2;
824
825   if (frchain_now->frch_cfi_data == NULL)
826     {
827       as_bad (_("CFI instruction used without previous .cfi_startproc"));
828       ignore_rest_of_line ();
829       return;
830     }
831
832   /* If the last address was not at the current PC, advance to current.  */
833   if (symbol_get_frag (frchain_now->frch_cfi_data->last_address) != frag_now
834       || S_GET_VALUE (frchain_now->frch_cfi_data->last_address)
835          != frag_now_fix ())
836     cfi_add_advance_loc (symbol_temp_new_now ());
837
838   switch (arg)
839     {
840     case DW_CFA_offset:
841       reg1 = cfi_parse_reg ();
842       cfi_parse_separator ();
843       offset = cfi_parse_const ();
844       cfi_add_CFA_offset (reg1, offset);
845       break;
846
847     case DW_CFA_val_offset:
848       reg1 = cfi_parse_reg ();
849       cfi_parse_separator ();
850       offset = cfi_parse_const ();
851       cfi_add_CFA_val_offset (reg1, offset);
852       break;
853
854     case CFI_rel_offset:
855       reg1 = cfi_parse_reg ();
856       cfi_parse_separator ();
857       offset = cfi_parse_const ();
858       cfi_add_CFA_offset (reg1,
859                           offset - frchain_now->frch_cfi_data->cur_cfa_offset);
860       break;
861
862     case DW_CFA_def_cfa:
863       reg1 = cfi_parse_reg ();
864       cfi_parse_separator ();
865       offset = cfi_parse_const ();
866       cfi_add_CFA_def_cfa (reg1, offset);
867       break;
868
869     case DW_CFA_register:
870       reg1 = cfi_parse_reg ();
871       cfi_parse_separator ();
872       reg2 = cfi_parse_reg ();
873       cfi_add_CFA_register (reg1, reg2);
874       break;
875
876     case DW_CFA_def_cfa_register:
877       reg1 = cfi_parse_reg ();
878       cfi_add_CFA_def_cfa_register (reg1);
879       break;
880
881     case DW_CFA_def_cfa_offset:
882       offset = cfi_parse_const ();
883       cfi_add_CFA_def_cfa_offset (offset);
884       break;
885
886     case CFI_adjust_cfa_offset:
887       offset = cfi_parse_const ();
888       cfi_add_CFA_def_cfa_offset (frchain_now->frch_cfi_data->cur_cfa_offset
889                                   + offset);
890       break;
891
892     case DW_CFA_restore:
893       for (;;)
894         {
895           reg1 = cfi_parse_reg ();
896           cfi_add_CFA_restore (reg1);
897           SKIP_WHITESPACE ();
898           if (*input_line_pointer != ',')
899             break;
900           ++input_line_pointer;
901         }
902       break;
903
904     case DW_CFA_undefined:
905       for (;;)
906         {
907           reg1 = cfi_parse_reg ();
908           cfi_add_CFA_undefined (reg1);
909           SKIP_WHITESPACE ();
910           if (*input_line_pointer != ',')
911             break;
912           ++input_line_pointer;
913         }
914       break;
915
916     case DW_CFA_same_value:
917       reg1 = cfi_parse_reg ();
918       cfi_add_CFA_same_value (reg1);
919       break;
920
921     case CFI_return_column:
922       reg1 = cfi_parse_reg ();
923       cfi_set_return_column (reg1);
924       break;
925
926     case DW_CFA_remember_state:
927       cfi_add_CFA_remember_state ();
928       break;
929
930     case DW_CFA_restore_state:
931       cfi_add_CFA_restore_state ();
932       break;
933
934     case DW_CFA_GNU_window_save:
935       cfi_add_CFA_insn (DW_CFA_GNU_window_save);
936       break;
937
938     case CFI_signal_frame:
939       frchain_now->frch_cfi_data->cur_fde_data->signal_frame = 1;
940       break;
941
942     default:
943       abort ();
944     }
945
946   demand_empty_rest_of_line ();
947 }
948
949 static void
950 dot_cfi_escape (int ignored ATTRIBUTE_UNUSED)
951 {
952   struct cfi_escape_data *head, **tail, *e;
953   struct cfi_insn_data *insn;
954
955   if (frchain_now->frch_cfi_data == NULL)
956     {
957       as_bad (_("CFI instruction used without previous .cfi_startproc"));
958       ignore_rest_of_line ();
959       return;
960     }
961
962   /* If the last address was not at the current PC, advance to current.  */
963   if (symbol_get_frag (frchain_now->frch_cfi_data->last_address) != frag_now
964       || S_GET_VALUE (frchain_now->frch_cfi_data->last_address)
965          != frag_now_fix ())
966     cfi_add_advance_loc (symbol_temp_new_now ());
967
968   tail = &head;
969   do
970     {
971       e = XNEW (struct cfi_escape_data);
972       do_parse_cons_expression (&e->exp, 1);
973       *tail = e;
974       tail = &e->next;
975     }
976   while (*input_line_pointer++ == ',');
977   *tail = NULL;
978
979   insn = alloc_cfi_insn_data ();
980   insn->insn = CFI_escape;
981   insn->u.esc = head;
982
983   --input_line_pointer;
984   demand_empty_rest_of_line ();
985 }
986
987 static void
988 dot_cfi_personality (int ignored ATTRIBUTE_UNUSED)
989 {
990   struct fde_entry *fde;
991   offsetT encoding;
992
993   if (frchain_now->frch_cfi_data == NULL)
994     {
995       as_bad (_("CFI instruction used without previous .cfi_startproc"));
996       ignore_rest_of_line ();
997       return;
998     }
999
1000   fde = frchain_now->frch_cfi_data->cur_fde_data;
1001   encoding = cfi_parse_const ();
1002   if (encoding == DW_EH_PE_omit)
1003     {
1004       demand_empty_rest_of_line ();
1005       fde->per_encoding = encoding;
1006       return;
1007     }
1008
1009   if ((encoding & 0xff) != encoding
1010       || ((((encoding & 0x70) != 0
1011 #if CFI_DIFF_EXPR_OK || defined tc_cfi_emit_pcrel_expr
1012            && (encoding & 0x70) != DW_EH_PE_pcrel
1013 #endif
1014           )
1015          /* leb128 can be handled, but does something actually need it?  */
1016            || (encoding & 7) == DW_EH_PE_uleb128
1017            || (encoding & 7) > DW_EH_PE_udata8)
1018         && tc_cfi_reloc_for_encoding (encoding) == BFD_RELOC_NONE))
1019     {
1020       as_bad (_("invalid or unsupported encoding in .cfi_personality"));
1021       ignore_rest_of_line ();
1022       return;
1023     }
1024
1025   if (*input_line_pointer++ != ',')
1026     {
1027       as_bad (_(".cfi_personality requires encoding and symbol arguments"));
1028       ignore_rest_of_line ();
1029       return;
1030     }
1031
1032   expression_and_evaluate (&fde->personality);
1033   switch (fde->personality.X_op)
1034     {
1035     case O_symbol:
1036       break;
1037     case O_constant:
1038       if ((encoding & 0x70) == DW_EH_PE_pcrel)
1039         encoding = DW_EH_PE_omit;
1040       break;
1041     default:
1042       encoding = DW_EH_PE_omit;
1043       break;
1044     }
1045
1046   fde->per_encoding = encoding;
1047
1048   if (encoding == DW_EH_PE_omit)
1049     {
1050       as_bad (_("wrong second argument to .cfi_personality"));
1051       ignore_rest_of_line ();
1052       return;
1053     }
1054
1055   demand_empty_rest_of_line ();
1056 }
1057
1058 static void
1059 dot_cfi_lsda (int ignored ATTRIBUTE_UNUSED)
1060 {
1061   struct fde_entry *fde;
1062   offsetT encoding;
1063
1064   if (frchain_now->frch_cfi_data == NULL)
1065     {
1066       as_bad (_("CFI instruction used without previous .cfi_startproc"));
1067       ignore_rest_of_line ();
1068       return;
1069     }
1070
1071   fde = frchain_now->frch_cfi_data->cur_fde_data;
1072   encoding = cfi_parse_const ();
1073   if (encoding == DW_EH_PE_omit)
1074     {
1075       demand_empty_rest_of_line ();
1076       fde->lsda_encoding = encoding;
1077       return;
1078     }
1079
1080   if ((encoding & 0xff) != encoding
1081       || ((((encoding & 0x70) != 0
1082 #if CFI_DIFF_LSDA_OK || defined tc_cfi_emit_pcrel_expr
1083             && (encoding & 0x70) != DW_EH_PE_pcrel
1084 #endif
1085            )
1086          /* leb128 can be handled, but does something actually need it?  */
1087            || (encoding & 7) == DW_EH_PE_uleb128
1088            || (encoding & 7) > DW_EH_PE_udata8)
1089           && tc_cfi_reloc_for_encoding (encoding) == BFD_RELOC_NONE))
1090     {
1091       as_bad (_("invalid or unsupported encoding in .cfi_lsda"));
1092       ignore_rest_of_line ();
1093       return;
1094     }
1095
1096   if (*input_line_pointer++ != ',')
1097     {
1098       as_bad (_(".cfi_lsda requires encoding and symbol arguments"));
1099       ignore_rest_of_line ();
1100       return;
1101     }
1102
1103   fde->lsda_encoding = encoding;
1104
1105   expression_and_evaluate (&fde->lsda);
1106   switch (fde->lsda.X_op)
1107     {
1108     case O_symbol:
1109       break;
1110     case O_constant:
1111       if ((encoding & 0x70) == DW_EH_PE_pcrel)
1112         encoding = DW_EH_PE_omit;
1113       break;
1114     default:
1115       encoding = DW_EH_PE_omit;
1116       break;
1117     }
1118
1119   fde->lsda_encoding = encoding;
1120
1121   if (encoding == DW_EH_PE_omit)
1122     {
1123       as_bad (_("wrong second argument to .cfi_lsda"));
1124       ignore_rest_of_line ();
1125       return;
1126     }
1127
1128   demand_empty_rest_of_line ();
1129 }
1130
1131 static void
1132 dot_cfi_val_encoded_addr (int ignored ATTRIBUTE_UNUSED)
1133 {
1134   struct cfi_insn_data *insn_ptr;
1135   offsetT encoding;
1136
1137   if (frchain_now->frch_cfi_data == NULL)
1138     {
1139       as_bad (_("CFI instruction used without previous .cfi_startproc"));
1140       ignore_rest_of_line ();
1141       return;
1142     }
1143
1144   /* If the last address was not at the current PC, advance to current.  */
1145   if (symbol_get_frag (frchain_now->frch_cfi_data->last_address) != frag_now
1146       || S_GET_VALUE (frchain_now->frch_cfi_data->last_address)
1147          != frag_now_fix ())
1148     cfi_add_advance_loc (symbol_temp_new_now ());
1149
1150   insn_ptr = alloc_cfi_insn_data ();
1151   insn_ptr->insn = CFI_val_encoded_addr;
1152
1153   insn_ptr->u.ea.reg = cfi_parse_reg ();
1154
1155   cfi_parse_separator ();
1156   encoding = cfi_parse_const ();
1157   if ((encoding & 0xff) != encoding
1158       || ((encoding & 0x70) != 0
1159 #if CFI_DIFF_EXPR_OK || defined tc_cfi_emit_pcrel_expr
1160           && (encoding & 0x70) != DW_EH_PE_pcrel
1161 #endif
1162           )
1163          /* leb128 can be handled, but does something actually need it?  */
1164       || (encoding & 7) == DW_EH_PE_uleb128
1165       || (encoding & 7) > DW_EH_PE_udata8)
1166     {
1167       as_bad (_("invalid or unsupported encoding in .cfi_lsda"));
1168       encoding = DW_EH_PE_omit;
1169     }
1170
1171   cfi_parse_separator ();
1172   expression_and_evaluate (&insn_ptr->u.ea.exp);
1173   switch (insn_ptr->u.ea.exp.X_op)
1174     {
1175     case O_symbol:
1176       break;
1177     case O_constant:
1178       if ((encoding & 0x70) != DW_EH_PE_pcrel)
1179         break;
1180     default:
1181       encoding = DW_EH_PE_omit;
1182       break;
1183     }
1184
1185   insn_ptr->u.ea.encoding = encoding;
1186   if (encoding == DW_EH_PE_omit)
1187     {
1188       as_bad (_("wrong third argument to .cfi_val_encoded_addr"));
1189       ignore_rest_of_line ();
1190       return;
1191     }
1192
1193   demand_empty_rest_of_line ();
1194 }
1195
1196 static void
1197 dot_cfi_label (int ignored ATTRIBUTE_UNUSED)
1198 {
1199   char *name = read_symbol_name ();
1200
1201   if (name == NULL)
1202     return;
1203
1204   /* If the last address was not at the current PC, advance to current.  */
1205   if (symbol_get_frag (frchain_now->frch_cfi_data->last_address) != frag_now
1206       || S_GET_VALUE (frchain_now->frch_cfi_data->last_address)
1207          != frag_now_fix ())
1208     cfi_add_advance_loc (symbol_temp_new_now ());
1209
1210   cfi_add_label (name);
1211   free (name);
1212
1213   demand_empty_rest_of_line ();
1214 }
1215
1216 static void
1217 dot_cfi_sections (int ignored ATTRIBUTE_UNUSED)
1218 {
1219   int sections = 0;
1220
1221   SKIP_WHITESPACE ();
1222   if (is_name_beginner (*input_line_pointer) || *input_line_pointer == '"')
1223     while (1)
1224       {
1225         char * saved_ilp;
1226         char *name, c;
1227
1228         saved_ilp = input_line_pointer;
1229         c = get_symbol_name (& name);
1230
1231         if (strncmp (name, ".eh_frame", sizeof ".eh_frame") == 0
1232             && name[9] != '_')
1233           sections |= CFI_EMIT_eh_frame;
1234         else if (strncmp (name, ".debug_frame", sizeof ".debug_frame") == 0)
1235           sections |= CFI_EMIT_debug_frame;
1236 #if SUPPORT_COMPACT_EH
1237         else if (strncmp (name, ".eh_frame_entry", sizeof ".eh_frame_entry") == 0)
1238           {
1239             compact_eh = TRUE;
1240             sections |= CFI_EMIT_eh_frame_compact;
1241           }
1242 #endif
1243 #ifdef tc_cfi_section_name
1244         else if (strcmp (name, tc_cfi_section_name) == 0)
1245           sections |= CFI_EMIT_target;
1246 #endif
1247         else
1248           {
1249             *input_line_pointer = c;
1250             input_line_pointer = saved_ilp;
1251             break;
1252           }
1253
1254         *input_line_pointer = c;
1255         SKIP_WHITESPACE_AFTER_NAME ();
1256         if (*input_line_pointer == ',')
1257           {
1258             name = input_line_pointer++;
1259             SKIP_WHITESPACE ();
1260             if (!is_name_beginner (*input_line_pointer) && *input_line_pointer != '"')
1261               {
1262                 input_line_pointer = name;
1263                 break;
1264               }
1265           }
1266         else if (is_name_beginner (*input_line_pointer) || *input_line_pointer == '"')
1267           break;
1268       }
1269
1270   demand_empty_rest_of_line ();
1271   if (cfi_sections_set && cfi_sections != sections)
1272     as_bad (_("inconsistent uses of .cfi_sections"));
1273   cfi_sections = sections;
1274 }
1275
1276 static void
1277 dot_cfi_startproc (int ignored ATTRIBUTE_UNUSED)
1278 {
1279   int simple = 0;
1280
1281   if (frchain_now->frch_cfi_data != NULL)
1282     {
1283       as_bad (_("previous CFI entry not closed (missing .cfi_endproc)"));
1284       ignore_rest_of_line ();
1285       return;
1286     }
1287
1288   cfi_new_fde (symbol_temp_new_now ());
1289
1290   SKIP_WHITESPACE ();
1291   if (is_name_beginner (*input_line_pointer) || *input_line_pointer == '"')
1292     {
1293       char * saved_ilp = input_line_pointer;
1294       char *name, c;
1295
1296       c = get_symbol_name (& name);
1297
1298       if (strcmp (name, "simple") == 0)
1299         {
1300           simple = 1;
1301           restore_line_pointer (c);
1302         }
1303       else
1304         input_line_pointer = saved_ilp;
1305     }
1306   demand_empty_rest_of_line ();
1307
1308   cfi_sections_set = TRUE;
1309   all_cfi_sections |= cfi_sections;
1310   cfi_set_sections ();
1311   frchain_now->frch_cfi_data->cur_cfa_offset = 0;
1312   if (!simple)
1313     tc_cfi_frame_initial_instructions ();
1314
1315   if ((cfi_sections & CFI_EMIT_target) != 0)
1316     tc_cfi_startproc ();
1317 }
1318
1319 static void
1320 dot_cfi_endproc (int ignored ATTRIBUTE_UNUSED)
1321 {
1322   if (frchain_now->frch_cfi_data == NULL)
1323     {
1324       as_bad (_(".cfi_endproc without corresponding .cfi_startproc"));
1325       ignore_rest_of_line ();
1326       return;
1327     }
1328
1329   last_fde = frchain_now->frch_cfi_data->cur_fde_data;
1330
1331   cfi_end_fde (symbol_temp_new_now ());
1332
1333   demand_empty_rest_of_line ();
1334
1335   cfi_sections_set = TRUE;
1336   if ((cfi_sections & CFI_EMIT_target) != 0)
1337     tc_cfi_endproc (last_fde);
1338 }
1339
1340 static segT
1341 get_cfi_seg (segT cseg, const char *base, flagword flags, int align)
1342 {
1343   /* Exclude .debug_frame sections for Compact EH.  */
1344   if (SUPPORT_FRAME_LINKONCE || ((flags & SEC_DEBUGGING) == 0 && compact_eh))
1345     {
1346       struct dwcfi_seg_list *l;
1347
1348       l = dwcfi_hash_find_or_make (cseg, base, flags);
1349
1350       cseg = l->seg;
1351       subseg_set (cseg, l->subseg);
1352     }
1353   else
1354     {
1355       cseg = subseg_new (base, 0);
1356       bfd_set_section_flags (stdoutput, cseg, flags);
1357     }
1358   record_alignment (cseg, align);
1359   return cseg;
1360 }
1361
1362 #if SUPPORT_COMPACT_EH
1363 static void
1364 dot_cfi_personality_id (int ignored ATTRIBUTE_UNUSED)
1365 {
1366   struct fde_entry *fde;
1367
1368   if (frchain_now->frch_cfi_data == NULL)
1369     {
1370       as_bad (_("CFI instruction used without previous .cfi_startproc"));
1371       ignore_rest_of_line ();
1372       return;
1373     }
1374
1375   fde = frchain_now->frch_cfi_data->cur_fde_data;
1376   fde->personality_id = cfi_parse_const ();
1377   demand_empty_rest_of_line ();
1378
1379   if (fde->personality_id == 0 || fde->personality_id > 3)
1380     {
1381       as_bad (_("wrong argument to .cfi_personality_id"));
1382       return;
1383     }
1384 }
1385
1386 static void
1387 dot_cfi_fde_data (int ignored ATTRIBUTE_UNUSED)
1388 {
1389   if (frchain_now->frch_cfi_data == NULL)
1390     {
1391       as_bad (_(".cfi_fde_data without corresponding .cfi_startproc"));
1392       ignore_rest_of_line ();
1393       return;
1394     }
1395
1396   last_fde = frchain_now->frch_cfi_data->cur_fde_data;
1397
1398   cfi_sections_set = TRUE;
1399   if ((cfi_sections & CFI_EMIT_target) != 0
1400       || (cfi_sections & CFI_EMIT_eh_frame_compact) != 0)
1401     {
1402       struct cfi_escape_data *head, **tail, *e;
1403       int num_ops = 0;
1404
1405       tail = &head;
1406       if (!is_it_end_of_statement ())
1407         {
1408           num_ops = 0;
1409           do
1410             {
1411               e = XNEW (struct cfi_escape_data);
1412               do_parse_cons_expression (&e->exp, 1);
1413               *tail = e;
1414               tail = &e->next;
1415               num_ops++;
1416             }
1417           while (*input_line_pointer++ == ',');
1418           --input_line_pointer;
1419         }
1420       *tail = NULL;
1421
1422       if (last_fde->lsda_encoding != DW_EH_PE_omit)
1423         last_fde->eh_header_type = EH_COMPACT_HAS_LSDA;
1424       else if (num_ops <= 3 && last_fde->per_encoding == DW_EH_PE_omit)
1425         last_fde->eh_header_type = EH_COMPACT_INLINE;
1426       else
1427         last_fde->eh_header_type = EH_COMPACT_OUTLINE;
1428
1429       if (last_fde->eh_header_type == EH_COMPACT_INLINE)
1430         num_ops = 3;
1431
1432       last_fde->eh_data_size = num_ops;
1433       last_fde->eh_data =  XNEWVEC (bfd_byte, num_ops);
1434       num_ops = 0;
1435       while (head)
1436         {
1437           e = head;
1438           head = e->next;
1439           last_fde->eh_data[num_ops++] = e->exp.X_add_number;
1440           free (e);
1441         }
1442       if (last_fde->eh_header_type == EH_COMPACT_INLINE)
1443         while (num_ops < 3)
1444           last_fde->eh_data[num_ops++] = tc_compact_eh_opcode_stop;
1445     }
1446
1447   demand_empty_rest_of_line ();
1448 }
1449
1450 /* Function to emit the compact unwinding opcodes stored in the
1451    fde's eh_data field.  The end of the opcode data will be
1452    padded to the value in align.  */
1453
1454 static void
1455 output_compact_unwind_data (struct fde_entry *fde, int align)
1456 {
1457   int data_size = fde->eh_data_size + 2;
1458   int align_padding;
1459   int amask;
1460   char *p;
1461
1462   fde->eh_loc = symbol_temp_new_now ();
1463
1464   p = frag_more (1);
1465   if (fde->personality_id != 0)
1466     *p = fde->personality_id;
1467   else if (fde->per_encoding != DW_EH_PE_omit)
1468     {
1469       *p = 0;
1470       emit_expr_encoded (&fde->personality, fde->per_encoding, FALSE);
1471       data_size += encoding_size (fde->per_encoding);
1472     }
1473   else
1474     *p = 1;
1475
1476   amask = (1 << align) - 1;
1477   align_padding = ((data_size + amask) & ~amask) - data_size;
1478
1479   p = frag_more (fde->eh_data_size + 1 + align_padding);
1480   memcpy (p, fde->eh_data, fde->eh_data_size);
1481   p += fde->eh_data_size;
1482
1483   while (align_padding-- > 0)
1484     *(p++) = tc_compact_eh_opcode_pad;
1485
1486   *(p++) = tc_compact_eh_opcode_stop;
1487   fde->eh_header_type = EH_COMPACT_OUTLINE_DONE;
1488 }
1489
1490 /* Handle the .cfi_inline_lsda directive.  */
1491 static void
1492 dot_cfi_inline_lsda (int ignored ATTRIBUTE_UNUSED)
1493 {
1494   segT ccseg;
1495   int align;
1496   long max_alignment = 28;
1497
1498   if (!last_fde)
1499     {
1500       as_bad (_("unexpected .cfi_inline_lsda"));
1501       ignore_rest_of_line ();
1502       return;
1503     }
1504
1505   if ((last_fde->sections & CFI_EMIT_eh_frame_compact) == 0)
1506     {
1507       as_bad (_(".cfi_inline_lsda not valid for this frame"));
1508       ignore_rest_of_line ();
1509       return;
1510     }
1511
1512   if (last_fde->eh_header_type != EH_COMPACT_UNKNOWN
1513       && last_fde->eh_header_type != EH_COMPACT_HAS_LSDA)
1514     {
1515       as_bad (_(".cfi_inline_lsda seen for frame without .cfi_lsda"));
1516       ignore_rest_of_line ();
1517       return;
1518     }
1519
1520 #ifdef md_flush_pending_output
1521   md_flush_pending_output ();
1522 #endif
1523
1524   align = get_absolute_expression ();
1525   if (align > max_alignment)
1526     {
1527       align = max_alignment;
1528       as_bad (_("Alignment too large: %d. assumed."), align);
1529     }
1530   else if (align < 0)
1531     {
1532       as_warn (_("Alignment negative: 0 assumed."));
1533       align = 0;
1534     }
1535
1536   demand_empty_rest_of_line ();
1537   ccseg = CUR_SEG (last_fde);
1538
1539   /* Open .gnu_extab section.  */
1540   get_cfi_seg (ccseg, ".gnu_extab",
1541                (SEC_ALLOC | SEC_LOAD | SEC_DATA
1542                 | DWARF2_EH_FRAME_READ_ONLY),
1543                1);
1544
1545   frag_align (align, 0, 0);
1546   record_alignment (now_seg, align);
1547   if (last_fde->eh_header_type == EH_COMPACT_HAS_LSDA)
1548     output_compact_unwind_data (last_fde, align);
1549
1550   last_fde = NULL;
1551
1552   return;
1553 }
1554 #else /* !SUPPORT_COMPACT_EH */
1555 static void
1556 dot_cfi_inline_lsda (int ignored ATTRIBUTE_UNUSED)
1557 {
1558   as_bad (_(".cfi_inline_lsda is not supported for this target"));
1559   ignore_rest_of_line ();
1560 }
1561
1562 static void
1563 dot_cfi_fde_data (int ignored ATTRIBUTE_UNUSED)
1564 {
1565   as_bad (_(".cfi_fde_data is not supported for this target"));
1566   ignore_rest_of_line ();
1567 }
1568
1569 static void
1570 dot_cfi_personality_id (int ignored ATTRIBUTE_UNUSED)
1571 {
1572   as_bad (_(".cfi_personality_id is not supported for this target"));
1573   ignore_rest_of_line ();
1574 }
1575 #endif
1576 \f
1577 static void
1578 output_cfi_insn (struct cfi_insn_data *insn)
1579 {
1580   offsetT offset;
1581   unsigned int regno;
1582
1583   switch (insn->insn)
1584     {
1585     case DW_CFA_advance_loc:
1586       {
1587         symbolS *from = insn->u.ll.lab1;
1588         symbolS *to = insn->u.ll.lab2;
1589
1590         if (symbol_get_frag (to) == symbol_get_frag (from))
1591           {
1592             addressT delta = S_GET_VALUE (to) - S_GET_VALUE (from);
1593             addressT scaled = delta / DWARF2_LINE_MIN_INSN_LENGTH;
1594
1595             if (scaled <= 0x3F)
1596               out_one (DW_CFA_advance_loc + scaled);
1597             else if (scaled <= 0xFF)
1598               {
1599                 out_one (DW_CFA_advance_loc1);
1600                 out_one (scaled);
1601               }
1602             else if (scaled <= 0xFFFF)
1603               {
1604                 out_one (DW_CFA_advance_loc2);
1605                 out_two (scaled);
1606               }
1607             else
1608               {
1609                 out_one (DW_CFA_advance_loc4);
1610                 out_four (scaled);
1611               }
1612           }
1613         else
1614           {
1615             expressionS exp;
1616
1617             exp.X_op = O_subtract;
1618             exp.X_add_symbol = to;
1619             exp.X_op_symbol = from;
1620             exp.X_add_number = 0;
1621
1622             /* The code in ehopt.c expects that one byte of the encoding
1623                is already allocated to the frag.  This comes from the way
1624                that it scans the .eh_frame section looking first for the
1625                .byte DW_CFA_advance_loc4.  */
1626             *frag_more (1) = DW_CFA_advance_loc4;
1627
1628             frag_var (rs_cfa, 4, 0, DWARF2_LINE_MIN_INSN_LENGTH << 3,
1629                       make_expr_symbol (&exp), frag_now_fix () - 1,
1630                       (char *) frag_now);
1631           }
1632       }
1633       break;
1634
1635     case DW_CFA_def_cfa:
1636       offset = insn->u.ri.offset;
1637       if (offset < 0)
1638         {
1639           out_one (DW_CFA_def_cfa_sf);
1640           out_uleb128 (insn->u.ri.reg);
1641           out_sleb128 (offset / DWARF2_CIE_DATA_ALIGNMENT);
1642         }
1643       else
1644         {
1645           out_one (DW_CFA_def_cfa);
1646           out_uleb128 (insn->u.ri.reg);
1647           out_uleb128 (offset);
1648         }
1649       break;
1650
1651     case DW_CFA_def_cfa_register:
1652     case DW_CFA_undefined:
1653     case DW_CFA_same_value:
1654       out_one (insn->insn);
1655       out_uleb128 (insn->u.r);
1656       break;
1657
1658     case DW_CFA_def_cfa_offset:
1659       offset = insn->u.i;
1660       if (offset < 0)
1661         {
1662           out_one (DW_CFA_def_cfa_offset_sf);
1663           out_sleb128 (offset / DWARF2_CIE_DATA_ALIGNMENT);
1664         }
1665       else
1666         {
1667           out_one (DW_CFA_def_cfa_offset);
1668           out_uleb128 (offset);
1669         }
1670       break;
1671
1672     case DW_CFA_restore:
1673       regno = insn->u.r;
1674       if (regno <= 0x3F)
1675         {
1676           out_one (DW_CFA_restore + regno);
1677         }
1678       else
1679         {
1680           out_one (DW_CFA_restore_extended);
1681           out_uleb128 (regno);
1682         }
1683       break;
1684
1685     case DW_CFA_offset:
1686       regno = insn->u.ri.reg;
1687       offset = insn->u.ri.offset / DWARF2_CIE_DATA_ALIGNMENT;
1688       if (offset < 0)
1689         {
1690           out_one (DW_CFA_offset_extended_sf);
1691           out_uleb128 (regno);
1692           out_sleb128 (offset);
1693         }
1694       else if (regno <= 0x3F)
1695         {
1696           out_one (DW_CFA_offset + regno);
1697           out_uleb128 (offset);
1698         }
1699       else
1700         {
1701           out_one (DW_CFA_offset_extended);
1702           out_uleb128 (regno);
1703           out_uleb128 (offset);
1704         }
1705       break;
1706
1707     case DW_CFA_val_offset:
1708       regno = insn->u.ri.reg;
1709       offset = insn->u.ri.offset / DWARF2_CIE_DATA_ALIGNMENT;
1710       if (offset < 0)
1711         {
1712           out_one (DW_CFA_val_offset_sf);
1713           out_uleb128 (regno);
1714           out_sleb128 (offset);
1715         }
1716       else
1717         {
1718           out_one (DW_CFA_val_offset);
1719           out_uleb128 (regno);
1720           out_uleb128 (offset);
1721         }
1722       break;
1723
1724     case DW_CFA_register:
1725       out_one (DW_CFA_register);
1726       out_uleb128 (insn->u.rr.reg1);
1727       out_uleb128 (insn->u.rr.reg2);
1728       break;
1729
1730     case DW_CFA_remember_state:
1731     case DW_CFA_restore_state:
1732       out_one (insn->insn);
1733       break;
1734
1735     case DW_CFA_GNU_window_save:
1736       out_one (DW_CFA_GNU_window_save);
1737       break;
1738
1739     case CFI_escape:
1740       {
1741         struct cfi_escape_data *e;
1742         for (e = insn->u.esc; e ; e = e->next)
1743           emit_expr (&e->exp, 1);
1744         break;
1745       }
1746
1747     case CFI_val_encoded_addr:
1748       {
1749         unsigned encoding = insn->u.ea.encoding;
1750         offsetT enc_size;
1751
1752         if (encoding == DW_EH_PE_omit)
1753           break;
1754         out_one (DW_CFA_val_expression);
1755         out_uleb128 (insn->u.ea.reg);
1756
1757         switch (encoding & 0x7)
1758           {
1759           case DW_EH_PE_absptr:
1760             enc_size = DWARF2_ADDR_SIZE (stdoutput);
1761             break;
1762           case DW_EH_PE_udata2:
1763             enc_size = 2;
1764             break;
1765           case DW_EH_PE_udata4:
1766             enc_size = 4;
1767             break;
1768           case DW_EH_PE_udata8:
1769             enc_size = 8;
1770             break;
1771           default:
1772             abort ();
1773           }
1774
1775         /* If the user has requested absolute encoding,
1776            then use the smaller DW_OP_addr encoding.  */
1777         if (insn->u.ea.encoding == DW_EH_PE_absptr)
1778           {
1779             out_uleb128 (1 + enc_size);
1780             out_one (DW_OP_addr);
1781           }
1782         else
1783           {
1784             out_uleb128 (1 + 1 + enc_size);
1785             out_one (DW_OP_GNU_encoded_addr);
1786             out_one (encoding);
1787
1788             if ((encoding & 0x70) == DW_EH_PE_pcrel)
1789               {
1790 #if CFI_DIFF_EXPR_OK
1791                 insn->u.ea.exp.X_op = O_subtract;
1792                 insn->u.ea.exp.X_op_symbol = symbol_temp_new_now ();
1793 #elif defined (tc_cfi_emit_pcrel_expr)
1794                 tc_cfi_emit_pcrel_expr (&insn->u.ea.exp, enc_size);
1795                 break;
1796 #else
1797                 abort ();
1798 #endif
1799               }
1800           }
1801         emit_expr (&insn->u.ea.exp, enc_size);
1802       }
1803       break;
1804
1805     case CFI_label:
1806       colon (insn->u.sym_name);
1807       break;
1808
1809     default:
1810       abort ();
1811     }
1812 }
1813
1814 static void
1815 output_cie (struct cie_entry *cie, bfd_boolean eh_frame, int align)
1816 {
1817   symbolS *after_size_address, *end_address;
1818   expressionS exp;
1819   struct cfi_insn_data *i;
1820   offsetT augmentation_size;
1821   int enc;
1822   enum dwarf2_format fmt = DWARF2_FORMAT (now_seg);
1823
1824   cie->start_address = symbol_temp_new_now ();
1825   after_size_address = symbol_temp_make ();
1826   end_address = symbol_temp_make ();
1827
1828   exp.X_op = O_subtract;
1829   exp.X_add_symbol = end_address;
1830   exp.X_op_symbol = after_size_address;
1831   exp.X_add_number = 0;
1832
1833   if (eh_frame || fmt == dwarf2_format_32bit)
1834     emit_expr (&exp, 4);                        /* Length.  */
1835   else
1836     {
1837       if (fmt == dwarf2_format_64bit)
1838         out_four (-1);
1839       emit_expr (&exp, 8);                      /* Length.  */
1840     }
1841   symbol_set_value_now (after_size_address);
1842   if (eh_frame)
1843     out_four (0);                               /* CIE id.  */
1844   else
1845     {
1846       out_four (-1);                            /* CIE id.  */
1847       if (fmt != dwarf2_format_32bit)
1848         out_four (-1);
1849     }
1850   out_one (DW_CIE_VERSION);                     /* Version.  */
1851   if (eh_frame)
1852     {
1853       out_one ('z');                            /* Augmentation.  */
1854       if (cie->per_encoding != DW_EH_PE_omit)
1855         out_one ('P');
1856       if (cie->lsda_encoding != DW_EH_PE_omit)
1857         out_one ('L');
1858       out_one ('R');
1859     }
1860   if (cie->signal_frame)
1861     out_one ('S');
1862   out_one (0);
1863   out_uleb128 (DWARF2_LINE_MIN_INSN_LENGTH);    /* Code alignment.  */
1864   out_sleb128 (DWARF2_CIE_DATA_ALIGNMENT);      /* Data alignment.  */
1865   if (DW_CIE_VERSION == 1)                      /* Return column.  */
1866     out_one (cie->return_column);
1867   else
1868     out_uleb128 (cie->return_column);
1869   if (eh_frame)
1870     {
1871       augmentation_size = 1 + (cie->lsda_encoding != DW_EH_PE_omit);
1872       if (cie->per_encoding != DW_EH_PE_omit)
1873         augmentation_size += 1 + encoding_size (cie->per_encoding);
1874       out_uleb128 (augmentation_size);          /* Augmentation size.  */
1875
1876       emit_expr_encoded (&cie->personality, cie->per_encoding, TRUE);
1877
1878       if (cie->lsda_encoding != DW_EH_PE_omit)
1879         out_one (cie->lsda_encoding);
1880     }
1881
1882   switch (DWARF2_FDE_RELOC_SIZE)
1883     {
1884     case 2:
1885       enc = DW_EH_PE_sdata2;
1886       break;
1887     case 4:
1888       enc = DW_EH_PE_sdata4;
1889       break;
1890     case 8:
1891       enc = DW_EH_PE_sdata8;
1892       break;
1893     default:
1894       abort ();
1895     }
1896 #if CFI_DIFF_EXPR_OK || defined tc_cfi_emit_pcrel_expr
1897   enc |= DW_EH_PE_pcrel;
1898 #endif
1899 #ifdef DWARF2_FDE_RELOC_ENCODING
1900   /* Allow target to override encoding.  */
1901   enc = DWARF2_FDE_RELOC_ENCODING (enc);
1902 #endif
1903   cie->fde_encoding = enc;
1904   if (eh_frame)
1905     out_one (enc);
1906
1907   if (cie->first)
1908     {
1909       for (i = cie->first; i != cie->last; i = i->next)
1910         {
1911           if (CUR_SEG (i) != CUR_SEG (cie))
1912             continue;
1913           output_cfi_insn (i);
1914         }
1915     }
1916
1917   frag_align (align, DW_CFA_nop, 0);
1918   symbol_set_value_now (end_address);
1919 }
1920
1921 static void
1922 output_fde (struct fde_entry *fde, struct cie_entry *cie,
1923             bfd_boolean eh_frame, struct cfi_insn_data *first,
1924             int align)
1925 {
1926   symbolS *after_size_address, *end_address;
1927   expressionS exp;
1928   offsetT augmentation_size;
1929   enum dwarf2_format fmt = DWARF2_FORMAT (now_seg);
1930   int offset_size;
1931   int addr_size;
1932
1933   after_size_address = symbol_temp_make ();
1934   end_address = symbol_temp_make ();
1935
1936   exp.X_op = O_subtract;
1937   exp.X_add_symbol = end_address;
1938   exp.X_op_symbol = after_size_address;
1939   exp.X_add_number = 0;
1940   if (eh_frame || fmt == dwarf2_format_32bit)
1941     offset_size = 4;
1942   else
1943     {
1944       if (fmt == dwarf2_format_64bit)
1945         out_four (-1);
1946       offset_size = 8;
1947     }
1948   emit_expr (&exp, offset_size);                /* Length.  */
1949   symbol_set_value_now (after_size_address);
1950
1951   if (eh_frame)
1952     {
1953       exp.X_op = O_subtract;
1954       exp.X_add_symbol = after_size_address;
1955       exp.X_op_symbol = cie->start_address;
1956       exp.X_add_number = 0;
1957       emit_expr (&exp, offset_size);            /* CIE offset.  */
1958     }
1959   else
1960     {
1961       TC_DWARF2_EMIT_OFFSET (cie->start_address, offset_size);
1962     }
1963
1964   exp.X_op = O_symbol;
1965   if (eh_frame)
1966     {
1967       bfd_reloc_code_real_type code
1968         = tc_cfi_reloc_for_encoding (cie->fde_encoding);
1969       if (code != BFD_RELOC_NONE)
1970         {
1971           reloc_howto_type *howto = bfd_reloc_type_lookup (stdoutput, code);
1972           char *p = frag_more (4);
1973           md_number_to_chars (p, 0, 4);
1974           fix_new (frag_now, p - frag_now->fr_literal, 4, fde->start_address,
1975                    0, howto->pc_relative, code);
1976         }
1977       else
1978         {
1979           exp.X_op = O_subtract;
1980           exp.X_add_number = 0;
1981 #if CFI_DIFF_EXPR_OK
1982           exp.X_add_symbol = fde->start_address;
1983           exp.X_op_symbol = symbol_temp_new_now ();
1984           emit_expr (&exp, DWARF2_FDE_RELOC_SIZE);      /* Code offset.  */
1985 #else
1986           exp.X_op = O_symbol;
1987           exp.X_add_symbol = fde->start_address;
1988
1989 #if defined(tc_cfi_emit_pcrel_expr)
1990           tc_cfi_emit_pcrel_expr (&exp, DWARF2_FDE_RELOC_SIZE);  /* Code offset.  */
1991 #else
1992           emit_expr (&exp, DWARF2_FDE_RELOC_SIZE);      /* Code offset.  */
1993 #endif
1994 #endif
1995         }
1996       addr_size = DWARF2_FDE_RELOC_SIZE;
1997     }
1998   else
1999     {
2000       exp.X_add_number = 0;
2001       exp.X_add_symbol = fde->start_address;
2002       addr_size = DWARF2_ADDR_SIZE (stdoutput);
2003       emit_expr (&exp, addr_size);
2004     }
2005
2006   exp.X_op = O_subtract;
2007   exp.X_add_symbol = fde->end_address;
2008   exp.X_op_symbol = fde->start_address;         /* Code length.  */
2009   exp.X_add_number = 0;
2010   emit_expr (&exp, addr_size);
2011
2012   augmentation_size = encoding_size (fde->lsda_encoding);
2013   if (eh_frame)
2014     out_uleb128 (augmentation_size);            /* Augmentation size.  */
2015
2016   emit_expr_encoded (&fde->lsda, cie->lsda_encoding, FALSE);
2017
2018   for (; first; first = first->next)
2019     if (CUR_SEG (first) == CUR_SEG (fde))
2020       output_cfi_insn (first);
2021
2022   frag_align (align, DW_CFA_nop, 0);
2023   symbol_set_value_now (end_address);
2024 }
2025
2026 static struct cie_entry *
2027 select_cie_for_fde (struct fde_entry *fde, bfd_boolean eh_frame,
2028                     struct cfi_insn_data **pfirst, int align)
2029 {
2030   struct cfi_insn_data *i, *j;
2031   struct cie_entry *cie;
2032
2033   for (cie = cie_root; cie; cie = cie->next)
2034     {
2035       if (CUR_SEG (cie) != CUR_SEG (fde))
2036         continue;
2037       if (cie->return_column != fde->return_column
2038           || cie->signal_frame != fde->signal_frame
2039           || cie->per_encoding != fde->per_encoding
2040           || cie->lsda_encoding != fde->lsda_encoding)
2041         continue;
2042       if (cie->per_encoding != DW_EH_PE_omit)
2043         {
2044           if (cie->personality.X_op != fde->personality.X_op
2045               || cie->personality.X_add_number
2046                  != fde->personality.X_add_number)
2047             continue;
2048           switch (cie->personality.X_op)
2049             {
2050             case O_constant:
2051               if (cie->personality.X_unsigned != fde->personality.X_unsigned)
2052                 continue;
2053               break;
2054             case O_symbol:
2055               if (cie->personality.X_add_symbol
2056                   != fde->personality.X_add_symbol)
2057                 continue;
2058               break;
2059             default:
2060               abort ();
2061             }
2062         }
2063       for (i = cie->first, j = fde->data;
2064            i != cie->last && j != NULL;
2065            i = i->next, j = j->next)
2066         {
2067           if (i->insn != j->insn)
2068             goto fail;
2069           switch (i->insn)
2070             {
2071             case DW_CFA_advance_loc:
2072             case DW_CFA_remember_state:
2073               /* We reached the first advance/remember in the FDE,
2074                  but did not reach the end of the CIE list.  */
2075               goto fail;
2076
2077             case DW_CFA_offset:
2078             case DW_CFA_def_cfa:
2079               if (i->u.ri.reg != j->u.ri.reg)
2080                 goto fail;
2081               if (i->u.ri.offset != j->u.ri.offset)
2082                 goto fail;
2083               break;
2084
2085             case DW_CFA_register:
2086               if (i->u.rr.reg1 != j->u.rr.reg1)
2087                 goto fail;
2088               if (i->u.rr.reg2 != j->u.rr.reg2)
2089                 goto fail;
2090               break;
2091
2092             case DW_CFA_def_cfa_register:
2093             case DW_CFA_restore:
2094             case DW_CFA_undefined:
2095             case DW_CFA_same_value:
2096               if (i->u.r != j->u.r)
2097                 goto fail;
2098               break;
2099
2100             case DW_CFA_def_cfa_offset:
2101               if (i->u.i != j->u.i)
2102                 goto fail;
2103               break;
2104
2105             case CFI_escape:
2106             case CFI_val_encoded_addr:
2107             case CFI_label:
2108               /* Don't bother matching these for now.  */
2109               goto fail;
2110
2111             default:
2112               abort ();
2113             }
2114         }
2115
2116       /* Success if we reached the end of the CIE list, and we've either
2117          run out of FDE entries or we've encountered an advance,
2118          remember, or escape.  */
2119       if (i == cie->last
2120           && (!j
2121               || j->insn == DW_CFA_advance_loc
2122               || j->insn == DW_CFA_remember_state
2123               || j->insn == CFI_escape
2124               || j->insn == CFI_val_encoded_addr
2125               || j->insn == CFI_label))
2126         {
2127           *pfirst = j;
2128           return cie;
2129         }
2130
2131     fail:;
2132     }
2133
2134   cie = XNEW (struct cie_entry);
2135   cie->next = cie_root;
2136   cie_root = cie;
2137   SET_CUR_SEG (cie, CUR_SEG (fde));
2138   cie->return_column = fde->return_column;
2139   cie->signal_frame = fde->signal_frame;
2140   cie->per_encoding = fde->per_encoding;
2141   cie->lsda_encoding = fde->lsda_encoding;
2142   cie->personality = fde->personality;
2143   cie->first = fde->data;
2144
2145   for (i = cie->first; i ; i = i->next)
2146     if (i->insn == DW_CFA_advance_loc
2147         || i->insn == DW_CFA_remember_state
2148         || i->insn == CFI_escape
2149         || i->insn == CFI_val_encoded_addr
2150         || i->insn == CFI_label)
2151       break;
2152
2153   cie->last = i;
2154   *pfirst = i;
2155
2156   output_cie (cie, eh_frame, align);
2157
2158   return cie;
2159 }
2160
2161 #ifdef md_reg_eh_frame_to_debug_frame
2162 static void
2163 cfi_change_reg_numbers (struct cfi_insn_data *insn, segT ccseg)
2164 {
2165   for (; insn; insn = insn->next)
2166     {
2167       if (CUR_SEG (insn) != ccseg)
2168         continue;
2169       switch (insn->insn)
2170         {
2171         case DW_CFA_advance_loc:
2172         case DW_CFA_def_cfa_offset:
2173         case DW_CFA_remember_state:
2174         case DW_CFA_restore_state:
2175         case DW_CFA_GNU_window_save:
2176         case CFI_escape:
2177         case CFI_label:
2178           break;
2179
2180         case DW_CFA_def_cfa:
2181         case DW_CFA_offset:
2182           insn->u.ri.reg = md_reg_eh_frame_to_debug_frame (insn->u.ri.reg);
2183           break;
2184
2185         case DW_CFA_def_cfa_register:
2186         case DW_CFA_undefined:
2187         case DW_CFA_same_value:
2188         case DW_CFA_restore:
2189           insn->u.r = md_reg_eh_frame_to_debug_frame (insn->u.r);
2190           break;
2191
2192         case DW_CFA_register:
2193           insn->u.rr.reg1 = md_reg_eh_frame_to_debug_frame (insn->u.rr.reg1);
2194           insn->u.rr.reg2 = md_reg_eh_frame_to_debug_frame (insn->u.rr.reg2);
2195           break;
2196
2197         case CFI_val_encoded_addr:
2198           insn->u.ea.reg = md_reg_eh_frame_to_debug_frame (insn->u.ea.reg);
2199           break;
2200
2201         default:
2202           abort ();
2203         }
2204     }
2205 }
2206 #else
2207 #define cfi_change_reg_numbers(insn, cseg) do { } while (0)
2208 #endif
2209
2210 #if SUPPORT_COMPACT_EH
2211 static void
2212 cfi_emit_eh_header (symbolS *sym, bfd_vma addend)
2213 {
2214   expressionS exp;
2215
2216   exp.X_add_number = addend;
2217   exp.X_add_symbol = sym;
2218   emit_expr_encoded (&exp, DW_EH_PE_sdata4 | DW_EH_PE_pcrel, FALSE);
2219 }
2220
2221 static void
2222 output_eh_header (struct fde_entry *fde)
2223 {
2224   char *p;
2225   bfd_vma addend;
2226
2227   if (fde->eh_header_type == EH_COMPACT_INLINE)
2228     addend = 0;
2229   else
2230     addend = 1;
2231
2232   cfi_emit_eh_header (fde->start_address, addend);
2233
2234   if (fde->eh_header_type == EH_COMPACT_INLINE)
2235     {
2236       p = frag_more (4);
2237       /* Inline entries always use PR1.  */
2238       *(p++) = 1;
2239       memcpy(p, fde->eh_data, 3);
2240     }
2241   else
2242     {
2243       if (fde->eh_header_type == EH_COMPACT_LEGACY)
2244         addend = 1;
2245       else if (fde->eh_header_type == EH_COMPACT_OUTLINE
2246                || fde->eh_header_type == EH_COMPACT_OUTLINE_DONE)
2247         addend = 0;
2248       else
2249         abort ();
2250       cfi_emit_eh_header (fde->eh_loc, addend);
2251     }
2252 }
2253 #endif
2254
2255 void
2256 cfi_finish (void)
2257 {
2258   struct cie_entry *cie, *cie_next;
2259   segT cfi_seg, ccseg;
2260   struct fde_entry *fde;
2261   struct cfi_insn_data *first;
2262   int save_flag_traditional_format, seek_next_seg;
2263
2264   if (all_fde_data == 0)
2265     return;
2266
2267   cfi_sections_set = TRUE;
2268   if ((all_cfi_sections & CFI_EMIT_eh_frame) != 0
2269       || (all_cfi_sections & CFI_EMIT_eh_frame_compact) != 0)
2270     {
2271       /* Make sure check_eh_frame doesn't do anything with our output.  */
2272       save_flag_traditional_format = flag_traditional_format;
2273       flag_traditional_format = 1;
2274
2275       if (!EH_FRAME_LINKONCE)
2276         {
2277           /* Open .eh_frame section.  */
2278           cfi_seg = get_cfi_seg (NULL, ".eh_frame",
2279                                  (SEC_ALLOC | SEC_LOAD | SEC_DATA
2280                                   | DWARF2_EH_FRAME_READ_ONLY),
2281                                  EH_FRAME_ALIGNMENT);
2282 #ifdef md_fix_up_eh_frame
2283           md_fix_up_eh_frame (cfi_seg);
2284 #else
2285           (void) cfi_seg;
2286 #endif
2287         }
2288
2289       do
2290         {
2291           ccseg = NULL;
2292           seek_next_seg = 0;
2293
2294           for (cie = cie_root; cie; cie = cie_next)
2295             {
2296               cie_next = cie->next;
2297               free ((void *) cie);
2298             }
2299           cie_root = NULL;
2300
2301           for (fde = all_fde_data; fde ; fde = fde->next)
2302             {
2303               if ((fde->sections & CFI_EMIT_eh_frame) == 0
2304                   && (fde->sections & CFI_EMIT_eh_frame_compact) == 0)
2305                 continue;
2306
2307 #if SUPPORT_COMPACT_EH
2308               /* Emit a LEGACY format header if we have processed all
2309                  of the .cfi directives without encountering either inline or
2310                  out-of-line compact unwinding opcodes.  */
2311               if (fde->eh_header_type == EH_COMPACT_HAS_LSDA
2312                   || fde->eh_header_type == EH_COMPACT_UNKNOWN)
2313                 fde->eh_header_type = EH_COMPACT_LEGACY;
2314
2315               if (fde->eh_header_type != EH_COMPACT_LEGACY)
2316                 continue;
2317 #endif
2318               if (EH_FRAME_LINKONCE)
2319                 {
2320                   if (HANDLED (fde))
2321                     continue;
2322                   if (seek_next_seg && CUR_SEG (fde) != ccseg)
2323                     {
2324                       seek_next_seg = 2;
2325                       continue;
2326                     }
2327                   if (!seek_next_seg)
2328                     {
2329                       ccseg = CUR_SEG (fde);
2330                       /* Open .eh_frame section.  */
2331                       cfi_seg = get_cfi_seg (ccseg, ".eh_frame",
2332                                              (SEC_ALLOC | SEC_LOAD | SEC_DATA
2333                                               | DWARF2_EH_FRAME_READ_ONLY),
2334                                              EH_FRAME_ALIGNMENT);
2335 #ifdef md_fix_up_eh_frame
2336                       md_fix_up_eh_frame (cfi_seg);
2337 #else
2338                       (void) cfi_seg;
2339 #endif
2340                       seek_next_seg = 1;
2341                     }
2342                   SET_HANDLED (fde, 1);
2343                 }
2344
2345               if (fde->end_address == NULL)
2346                 {
2347                   as_bad (_("open CFI at the end of file; missing .cfi_endproc directive"));
2348                   fde->end_address = fde->start_address;
2349                 }
2350
2351               cie = select_cie_for_fde (fde, TRUE, &first, 2);
2352               fde->eh_loc = symbol_temp_new_now ();
2353               output_fde (fde, cie, TRUE, first,
2354                           fde->next == NULL ? EH_FRAME_ALIGNMENT : 2);
2355             }
2356         }
2357       while (EH_FRAME_LINKONCE && seek_next_seg == 2);
2358
2359       if (EH_FRAME_LINKONCE)
2360         for (fde = all_fde_data; fde ; fde = fde->next)
2361           SET_HANDLED (fde, 0);
2362
2363 #if SUPPORT_COMPACT_EH
2364       if (compact_eh)
2365         {
2366           /* Create remaining out of line table entries.  */
2367           do
2368             {
2369               ccseg = NULL;
2370               seek_next_seg = 0;
2371
2372               for (fde = all_fde_data; fde ; fde = fde->next)
2373                 {
2374                   if ((fde->sections & CFI_EMIT_eh_frame) == 0
2375                       && (fde->sections & CFI_EMIT_eh_frame_compact) == 0)
2376                     continue;
2377
2378                   if (fde->eh_header_type != EH_COMPACT_OUTLINE)
2379                     continue;
2380                   if (HANDLED (fde))
2381                     continue;
2382                   if (seek_next_seg && CUR_SEG (fde) != ccseg)
2383                     {
2384                       seek_next_seg = 2;
2385                       continue;
2386                     }
2387                   if (!seek_next_seg)
2388                     {
2389                       ccseg = CUR_SEG (fde);
2390                       /* Open .gnu_extab section.  */
2391                       get_cfi_seg (ccseg, ".gnu_extab",
2392                                    (SEC_ALLOC | SEC_LOAD | SEC_DATA
2393                                     | DWARF2_EH_FRAME_READ_ONLY),
2394                                    1);
2395                       seek_next_seg = 1;
2396                     }
2397                   SET_HANDLED (fde, 1);
2398
2399                   frag_align (1, 0, 0);
2400                   record_alignment (now_seg, 1);
2401                   output_compact_unwind_data (fde, 1);
2402                 }
2403             }
2404           while (EH_FRAME_LINKONCE && seek_next_seg == 2);
2405
2406           for (fde = all_fde_data; fde ; fde = fde->next)
2407             SET_HANDLED (fde, 0);
2408
2409           /* Create index table fragments.  */
2410           do
2411             {
2412               ccseg = NULL;
2413               seek_next_seg = 0;
2414
2415               for (fde = all_fde_data; fde ; fde = fde->next)
2416                 {
2417                   if ((fde->sections & CFI_EMIT_eh_frame) == 0
2418                       && (fde->sections & CFI_EMIT_eh_frame_compact) == 0)
2419                     continue;
2420
2421                   if (HANDLED (fde))
2422                     continue;
2423                   if (seek_next_seg && CUR_SEG (fde) != ccseg)
2424                     {
2425                       seek_next_seg = 2;
2426                       continue;
2427                     }
2428                   if (!seek_next_seg)
2429                     {
2430                       ccseg = CUR_SEG (fde);
2431                       /* Open .eh_frame_entry section.  */
2432                       cfi_seg = get_cfi_seg (ccseg, ".eh_frame_entry",
2433                                              (SEC_ALLOC | SEC_LOAD | SEC_DATA
2434                                               | DWARF2_EH_FRAME_READ_ONLY),
2435                                              2);
2436                       seek_next_seg = 1;
2437                     }
2438                   SET_HANDLED (fde, 1);
2439
2440                   output_eh_header (fde);
2441                 }
2442             }
2443           while (seek_next_seg == 2);
2444
2445           for (fde = all_fde_data; fde ; fde = fde->next)
2446             SET_HANDLED (fde, 0);
2447         }
2448 #endif /* SUPPORT_COMPACT_EH */
2449
2450       flag_traditional_format = save_flag_traditional_format;
2451     }
2452
2453   cfi_sections_set = TRUE;
2454   if ((all_cfi_sections & CFI_EMIT_debug_frame) != 0)
2455     {
2456       int alignment = ffs (DWARF2_ADDR_SIZE (stdoutput)) - 1;
2457
2458       if (!SUPPORT_FRAME_LINKONCE)
2459         get_cfi_seg (NULL, ".debug_frame",
2460                      SEC_READONLY | SEC_DEBUGGING,
2461                      alignment);
2462
2463       do
2464         {
2465           ccseg = NULL;
2466           seek_next_seg = 0;
2467
2468           for (cie = cie_root; cie; cie = cie_next)
2469             {
2470               cie_next = cie->next;
2471               free ((void *) cie);
2472             }
2473           cie_root = NULL;
2474
2475           for (fde = all_fde_data; fde ; fde = fde->next)
2476             {
2477               if ((fde->sections & CFI_EMIT_debug_frame) == 0)
2478                 continue;
2479
2480               if (SUPPORT_FRAME_LINKONCE)
2481                 {
2482                   if (HANDLED (fde))
2483                     continue;
2484                   if (seek_next_seg && CUR_SEG (fde) != ccseg)
2485                     {
2486                       seek_next_seg = 2;
2487                       continue;
2488                     }
2489                   if (!seek_next_seg)
2490                     {
2491                       ccseg = CUR_SEG (fde);
2492                       /* Open .debug_frame section.  */
2493                       get_cfi_seg (ccseg, ".debug_frame",
2494                                    SEC_READONLY | SEC_DEBUGGING,
2495                                    alignment);
2496                       seek_next_seg = 1;
2497                     }
2498                   SET_HANDLED (fde, 1);
2499                 }
2500               if (fde->end_address == NULL)
2501                 {
2502                   as_bad (_("open CFI at the end of file; missing .cfi_endproc directive"));
2503                   fde->end_address = fde->start_address;
2504                 }
2505
2506               fde->per_encoding = DW_EH_PE_omit;
2507               fde->lsda_encoding = DW_EH_PE_omit;
2508               cfi_change_reg_numbers (fde->data, ccseg);
2509               cie = select_cie_for_fde (fde, FALSE, &first, alignment);
2510               output_fde (fde, cie, FALSE, first, alignment);
2511             }
2512         }
2513       while (SUPPORT_FRAME_LINKONCE && seek_next_seg == 2);
2514
2515       if (SUPPORT_FRAME_LINKONCE)
2516         for (fde = all_fde_data; fde ; fde = fde->next)
2517           SET_HANDLED (fde, 0);
2518     }
2519 }
2520
2521 #else /* TARGET_USE_CFIPOP */
2522
2523 /* Emit an intelligible error message for missing support.  */
2524
2525 static void
2526 dot_cfi_dummy (int ignored ATTRIBUTE_UNUSED)
2527 {
2528   as_bad (_("CFI is not supported for this target"));
2529   ignore_rest_of_line ();
2530 }
2531
2532 const pseudo_typeS cfi_pseudo_table[] =
2533   {
2534     { "cfi_sections", dot_cfi_dummy, 0 },
2535     { "cfi_startproc", dot_cfi_dummy, 0 },
2536     { "cfi_endproc", dot_cfi_dummy, 0 },
2537     { "cfi_fde_data", dot_cfi_dummy, 0 },
2538     { "cfi_def_cfa", dot_cfi_dummy, 0 },
2539     { "cfi_def_cfa_register", dot_cfi_dummy, 0 },
2540     { "cfi_def_cfa_offset", dot_cfi_dummy, 0 },
2541     { "cfi_adjust_cfa_offset", dot_cfi_dummy, 0 },
2542     { "cfi_offset", dot_cfi_dummy, 0 },
2543     { "cfi_rel_offset", dot_cfi_dummy, 0 },
2544     { "cfi_register", dot_cfi_dummy, 0 },
2545     { "cfi_return_column", dot_cfi_dummy, 0 },
2546     { "cfi_restore", dot_cfi_dummy, 0 },
2547     { "cfi_undefined", dot_cfi_dummy, 0 },
2548     { "cfi_same_value", dot_cfi_dummy, 0 },
2549     { "cfi_remember_state", dot_cfi_dummy, 0 },
2550     { "cfi_restore_state", dot_cfi_dummy, 0 },
2551     { "cfi_window_save", dot_cfi_dummy, 0 },
2552     { "cfi_escape", dot_cfi_dummy, 0 },
2553     { "cfi_signal_frame", dot_cfi_dummy, 0 },
2554     { "cfi_personality", dot_cfi_dummy, 0 },
2555     { "cfi_personality_id", dot_cfi_dummy, 0 },
2556     { "cfi_lsda", dot_cfi_dummy, 0 },
2557     { "cfi_val_encoded_addr", dot_cfi_dummy, 0 },
2558     { "cfi_label", dot_cfi_dummy, 0 },
2559     { "cfi_inline_lsda", dot_cfi_dummy, 0 },
2560     { "cfi_val_offset", dot_cfi_dummy, 0 },
2561     { NULL, NULL, 0 }
2562   };
2563
2564 void
2565 cfi_finish (void)
2566 {
2567 }
2568 #endif /* TARGET_USE_CFIPOP */