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