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>
5 This file is part of GAS, the GNU Assembler.
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)
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.
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
23 #include "dw2gencfi.h"
25 #include "dwarf2dbg.h"
27 #ifdef TARGET_USE_CFIPOP
29 /* By default, use difference expressions if DIFF_EXPR_OK is defined. */
30 #ifndef CFI_DIFF_EXPR_OK
32 # define CFI_DIFF_EXPR_OK 1
34 # define CFI_DIFF_EXPR_OK 0
38 #ifndef CFI_DIFF_LSDA_OK
39 #define CFI_DIFF_LSDA_OK CFI_DIFF_EXPR_OK
42 #if CFI_DIFF_EXPR_OK == 1 && CFI_DIFF_LSDA_OK == 0
43 # error "CFI_DIFF_EXPR_OK should imply CFI_DIFF_LSDA_OK"
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
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
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
62 #ifndef EH_FRAME_ALIGNMENT
63 #define EH_FRAME_ALIGNMENT (bfd_get_arch_size (stdoutput) == 64 ? 3 : 2)
66 #ifndef tc_cfi_frame_initial_instructions
67 #define tc_cfi_frame_initial_instructions() ((void)0)
70 #ifndef tc_cfi_startproc
71 # define tc_cfi_startproc() ((void)0)
74 #ifndef tc_cfi_endproc
75 # define tc_cfi_endproc(fde) ((void) (fde))
78 #define EH_FRAME_LINKONCE (SUPPORT_FRAME_LINKONCE || compact_eh)
81 #define DWARF2_FORMAT(SEC) dwarf2_format_32bit
84 #ifndef DWARF2_ADDR_SIZE
85 #define DWARF2_ADDR_SIZE(bfd) (bfd_arch_bits_per_address (bfd) / 8)
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
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)
100 #ifndef tc_cfi_reloc_for_encoding
101 #define tc_cfi_reloc_for_encoding(e) BFD_RELOC_NONE
104 /* Private segment collection list. */
105 struct dwcfi_seg_list
112 #ifdef SUPPORT_COMPACT_EH
113 static bfd_boolean compact_eh;
118 static struct hash_control *dwcfi_hash;
120 /* Emit a single byte into the current segment. */
125 FRAG_APPEND_1_CHAR (byte);
128 /* Emit a two-byte word into the current segment. */
133 md_number_to_chars (frag_more (2), data, 2);
136 /* Emit a four byte word into the current segment. */
141 md_number_to_chars (frag_more (4), data, 4);
144 /* Emit an unsigned "little-endian base 128" number. */
147 out_uleb128 (addressT value)
149 output_leb128 (frag_more (sizeof_leb128 (value, 0)), value, 0);
152 /* Emit an unsigned "little-endian base 128" number. */
155 out_sleb128 (offsetT value)
157 output_leb128 (frag_more (sizeof_leb128 (value, 1)), value, 1);
161 encoding_size (unsigned char encoding)
163 if (encoding == DW_EH_PE_omit)
165 switch (encoding & 0x7)
168 return bfd_get_arch_size (stdoutput) == 64 ? 8 : 4;
169 case DW_EH_PE_udata2:
171 case DW_EH_PE_udata4:
173 case DW_EH_PE_udata8:
180 /* Emit expression EXP in ENCODING. If EMIT_ENCODING is true, first
181 emit a byte containing ENCODING. */
184 emit_expr_encoded (expressionS *exp, int encoding, bfd_boolean emit_encoding)
186 offsetT size = encoding_size (encoding);
187 bfd_reloc_code_real_type code;
189 if (encoding == DW_EH_PE_omit)
195 code = tc_cfi_reloc_for_encoding (encoding);
196 if (code != BFD_RELOC_NONE)
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);
204 else if ((encoding & 0x70) == DW_EH_PE_pcrel)
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);
218 emit_expr (exp, size);
221 /* Build based on segment the derived .debug_...
222 segment name containing origin segment's postfix name part. */
225 get_debugseg_name (segT seg, const char *base_name)
236 name = bfd_get_section_name (stdoutput, seg);
238 dollar = strchr (name, '$');
239 dot = strchr (name + 1, '.');
243 if (!strcmp (base_name, ".eh_frame_entry")
244 && strcmp (name, ".text") != 0)
245 return concat (base_name, ".", name, NULL);
253 else if (dot < dollar)
259 return concat (base_name, name, NULL);
262 /* Allocate a dwcfi_seg_list structure. */
264 static struct dwcfi_seg_list *
265 alloc_debugseg_item (segT seg, int subseg, char *name)
267 struct dwcfi_seg_list *r;
269 r = (struct dwcfi_seg_list *)
270 xmalloc (sizeof (struct dwcfi_seg_list) + strlen (name));
278 is_now_linkonce_segment (void)
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)
291 /* Generate debug... segment with same linkonce properties
295 make_debug_seg (segT cseg, char *name, int sflags)
297 segT save_seg = now_seg;
298 int save_subseg = now_subseg;
302 r = subseg_new (name, 0);
304 /* Check if code segment is marked as linked once. */
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);
313 /* Add standard section flags. */
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 ()));
321 /* Restore to previous segment. */
322 if (save_seg != NULL)
323 subseg_set (save_seg, save_subseg);
328 dwcfi_hash_insert (const char *name, struct dwcfi_seg_list *item)
330 const char *error_string;
332 if ((error_string = hash_jam (dwcfi_hash, name, (char *) item)))
333 as_fatal (_("Inserting \"%s\" into structure table failed: %s"),
337 static struct dwcfi_seg_list *
338 dwcfi_hash_find (char *name)
340 return (struct dwcfi_seg_list *) hash_find (dwcfi_hash, name);
343 static struct dwcfi_seg_list *
344 dwcfi_hash_find_or_make (segT cseg, const char *base_name, int flags)
346 struct dwcfi_seg_list *item;
349 /* Initialize dwcfi_hash once. */
351 dwcfi_hash = hash_new ();
353 name = get_debugseg_name (cseg, base_name);
355 item = dwcfi_hash_find (name);
358 item = alloc_debugseg_item (make_debug_seg (cseg, name, flags), 0, name);
360 dwcfi_hash_insert (item->seg_name, item);
368 /* ??? Share this with dwarf2cfg.c. */
369 #ifndef TC_DWARF2_EMIT_OFFSET
370 #define TC_DWARF2_EMIT_OFFSET generic_dwarf2_emit_offset
372 /* Create an offset to .dwarf2_*. */
375 generic_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
380 exp.X_add_symbol = symbol;
381 exp.X_add_number = 0;
382 emit_expr (&exp, size);
386 struct cfi_escape_data
388 struct cfi_escape_data *next;
394 struct cie_entry *next;
395 #if MULTIPLE_FRAME_SECTIONS
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;
408 /* List of FDE entries. */
410 struct fde_entry *all_fde_data;
411 static struct fde_entry **last_fde_data = &all_fde_data;
413 /* List of CIEs so that they could be reused. */
414 static struct cie_entry *cie_root;
416 /* Stack of old CFI data, for save/restore. */
419 struct cfa_save_data *next;
423 /* Current open FDE entry. */
426 struct fde_entry *cur_fde_data;
427 symbolS *last_address;
428 offsetT cur_cfa_offset;
429 struct cfa_save_data *cfa_save_stack;
432 /* Construct a new FDE structure and add it to the end of the fde list. */
434 static struct fde_entry *
435 alloc_fde_entry (void)
437 struct fde_entry *fde = XCNEW (struct fde_entry);
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;
454 /* The following functions are available for a backend to construct its
455 own unwind information, usually from legacy unwind directives. */
457 /* Construct a new INSN structure and add it to the end of the insn list
458 for the currently active FDE. */
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;
465 static struct cfi_insn_data *
466 alloc_cfi_insn_data (void)
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;
471 *cur_fde_data->last = insn;
472 cur_fde_data->last = &insn->next;
473 SET_CUR_SEG (insn, is_now_linkonce_segment ());
477 /* Construct a new FDE structure that begins at LABEL. */
480 cfi_new_fde (symbolS *label)
482 struct fde_entry *fde = alloc_fde_entry ();
483 fde->start_address = label;
484 frchain_now->frch_cfi_data->last_address = label;
487 /* End the currently open FDE. */
490 cfi_end_fde (symbolS *label)
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;
497 /* Set the return column for the current FDE. */
500 cfi_set_return_column (unsigned regno)
502 frchain_now->frch_cfi_data->cur_fde_data->return_column = regno;
506 cfi_set_sections (void)
508 frchain_now->frch_cfi_data->cur_fde_data->sections = all_cfi_sections;
509 cfi_sections_set = TRUE;
512 /* Universal functions to store new instructions. */
515 cfi_add_CFA_insn (int insn)
517 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
519 insn_ptr->insn = insn;
523 cfi_add_CFA_insn_reg (int insn, unsigned regno)
525 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
527 insn_ptr->insn = insn;
528 insn_ptr->u.r = regno;
532 cfi_add_CFA_insn_offset (int insn, offsetT offset)
534 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
536 insn_ptr->insn = insn;
537 insn_ptr->u.i = offset;
541 cfi_add_CFA_insn_reg_reg (int insn, unsigned reg1, unsigned reg2)
543 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
545 insn_ptr->insn = insn;
546 insn_ptr->u.rr.reg1 = reg1;
547 insn_ptr->u.rr.reg2 = reg2;
551 cfi_add_CFA_insn_reg_offset (int insn, unsigned regno, offsetT offset)
553 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
555 insn_ptr->insn = insn;
556 insn_ptr->u.ri.reg = regno;
557 insn_ptr->u.ri.offset = offset;
560 /* Add a CFI insn to advance the PC from the last address to LABEL. */
563 cfi_add_advance_loc (symbolS *label)
565 struct cfi_insn_data *insn = alloc_cfi_insn_data ();
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;
571 frchain_now->frch_cfi_data->last_address = label;
574 /* Add a CFI insn to label the current position in the CFI segment. */
577 cfi_add_label (const char *name)
579 unsigned int len = strlen (name) + 1;
580 struct cfi_insn_data *insn = alloc_cfi_insn_data ();
582 insn->insn = CFI_label;
583 obstack_grow (¬es, name, len);
584 insn->u.sym_name = (char *) obstack_finish (¬es);
587 /* Add a DW_CFA_offset record to the CFI data. */
590 cfi_add_CFA_offset (unsigned regno, offsetT offset)
592 unsigned int abs_data_align;
594 gas_assert (DWARF2_CIE_DATA_ALIGNMENT != 0);
595 cfi_add_CFA_insn_reg_offset (DW_CFA_offset, regno, offset);
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);
603 /* Add a DW_CFA_val_offset record to the CFI data. */
606 cfi_add_CFA_val_offset (unsigned regno, offsetT offset)
608 unsigned int abs_data_align;
610 gas_assert (DWARF2_CIE_DATA_ALIGNMENT != 0);
611 cfi_add_CFA_insn_reg_offset (DW_CFA_val_offset, regno, offset);
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);
619 /* Add a DW_CFA_def_cfa record to the CFI data. */
622 cfi_add_CFA_def_cfa (unsigned regno, offsetT offset)
624 cfi_add_CFA_insn_reg_offset (DW_CFA_def_cfa, regno, offset);
625 frchain_now->frch_cfi_data->cur_cfa_offset = offset;
628 /* Add a DW_CFA_register record to the CFI data. */
631 cfi_add_CFA_register (unsigned reg1, unsigned reg2)
633 cfi_add_CFA_insn_reg_reg (DW_CFA_register, reg1, reg2);
636 /* Add a DW_CFA_def_cfa_register record to the CFI data. */
639 cfi_add_CFA_def_cfa_register (unsigned regno)
641 cfi_add_CFA_insn_reg (DW_CFA_def_cfa_register, regno);
644 /* Add a DW_CFA_def_cfa_offset record to the CFI data. */
647 cfi_add_CFA_def_cfa_offset (offsetT offset)
649 cfi_add_CFA_insn_offset (DW_CFA_def_cfa_offset, offset);
650 frchain_now->frch_cfi_data->cur_cfa_offset = offset;
654 cfi_add_CFA_restore (unsigned regno)
656 cfi_add_CFA_insn_reg (DW_CFA_restore, regno);
660 cfi_add_CFA_undefined (unsigned regno)
662 cfi_add_CFA_insn_reg (DW_CFA_undefined, regno);
666 cfi_add_CFA_same_value (unsigned regno)
668 cfi_add_CFA_insn_reg (DW_CFA_same_value, regno);
672 cfi_add_CFA_remember_state (void)
674 struct cfa_save_data *p;
676 cfi_add_CFA_insn (DW_CFA_remember_state);
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;
685 cfi_add_CFA_restore_state (void)
687 struct cfa_save_data *p;
689 cfi_add_CFA_insn (DW_CFA_restore_state);
691 p = frchain_now->frch_cfi_data->cfa_save_stack;
694 frchain_now->frch_cfi_data->cur_cfa_offset = p->cfa_offset;
695 frchain_now->frch_cfi_data->cfa_save_stack = p->next;
699 as_bad (_("CFI state restore without previous remember"));
703 /* Parse CFI assembler directives. */
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);
718 const pseudo_typeS cfi_pseudo_table[] =
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 },
751 cfi_parse_separator (void)
754 if (*input_line_pointer == ',')
755 input_line_pointer++;
757 as_bad (_("missing separator"));
760 #ifndef tc_parse_to_dw2regnum
762 tc_parse_to_dw2regnum (expressionS *exp)
764 # ifdef tc_regname_to_dw2regnum
766 if (is_name_beginner (*input_line_pointer)
767 || (*input_line_pointer == '%'
768 && is_name_beginner (*++input_line_pointer)))
772 c = get_symbol_name (& name);
774 exp->X_op = O_constant;
775 exp->X_add_number = tc_regname_to_dw2regnum (name);
777 restore_line_pointer (c);
781 expression_and_evaluate (exp);
791 tc_parse_to_dw2regnum (&exp);
796 regno = exp.X_add_number;
806 as_bad (_("bad register expression"));
814 cfi_parse_const (void)
816 return get_absolute_expression ();
825 if (frchain_now->frch_cfi_data == NULL)
827 as_bad (_("CFI instruction used without previous .cfi_startproc"));
828 ignore_rest_of_line ();
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)
836 cfi_add_advance_loc (symbol_temp_new_now ());
841 reg1 = cfi_parse_reg ();
842 cfi_parse_separator ();
843 offset = cfi_parse_const ();
844 cfi_add_CFA_offset (reg1, offset);
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);
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);
863 reg1 = cfi_parse_reg ();
864 cfi_parse_separator ();
865 offset = cfi_parse_const ();
866 cfi_add_CFA_def_cfa (reg1, offset);
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);
876 case DW_CFA_def_cfa_register:
877 reg1 = cfi_parse_reg ();
878 cfi_add_CFA_def_cfa_register (reg1);
881 case DW_CFA_def_cfa_offset:
882 offset = cfi_parse_const ();
883 cfi_add_CFA_def_cfa_offset (offset);
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
895 reg1 = cfi_parse_reg ();
896 cfi_add_CFA_restore (reg1);
898 if (*input_line_pointer != ',')
900 ++input_line_pointer;
904 case DW_CFA_undefined:
907 reg1 = cfi_parse_reg ();
908 cfi_add_CFA_undefined (reg1);
910 if (*input_line_pointer != ',')
912 ++input_line_pointer;
916 case DW_CFA_same_value:
917 reg1 = cfi_parse_reg ();
918 cfi_add_CFA_same_value (reg1);
921 case CFI_return_column:
922 reg1 = cfi_parse_reg ();
923 cfi_set_return_column (reg1);
926 case DW_CFA_remember_state:
927 cfi_add_CFA_remember_state ();
930 case DW_CFA_restore_state:
931 cfi_add_CFA_restore_state ();
934 case DW_CFA_GNU_window_save:
935 cfi_add_CFA_insn (DW_CFA_GNU_window_save);
938 case CFI_signal_frame:
939 frchain_now->frch_cfi_data->cur_fde_data->signal_frame = 1;
946 demand_empty_rest_of_line ();
950 dot_cfi_escape (int ignored ATTRIBUTE_UNUSED)
952 struct cfi_escape_data *head, **tail, *e;
953 struct cfi_insn_data *insn;
955 if (frchain_now->frch_cfi_data == NULL)
957 as_bad (_("CFI instruction used without previous .cfi_startproc"));
958 ignore_rest_of_line ();
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)
966 cfi_add_advance_loc (symbol_temp_new_now ());
971 e = XNEW (struct cfi_escape_data);
972 do_parse_cons_expression (&e->exp, 1);
976 while (*input_line_pointer++ == ',');
979 insn = alloc_cfi_insn_data ();
980 insn->insn = CFI_escape;
983 --input_line_pointer;
984 demand_empty_rest_of_line ();
988 dot_cfi_personality (int ignored ATTRIBUTE_UNUSED)
990 struct fde_entry *fde;
993 if (frchain_now->frch_cfi_data == NULL)
995 as_bad (_("CFI instruction used without previous .cfi_startproc"));
996 ignore_rest_of_line ();
1000 fde = frchain_now->frch_cfi_data->cur_fde_data;
1001 encoding = cfi_parse_const ();
1002 if (encoding == DW_EH_PE_omit)
1004 demand_empty_rest_of_line ();
1005 fde->per_encoding = encoding;
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
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))
1020 as_bad (_("invalid or unsupported encoding in .cfi_personality"));
1021 ignore_rest_of_line ();
1025 if (*input_line_pointer++ != ',')
1027 as_bad (_(".cfi_personality requires encoding and symbol arguments"));
1028 ignore_rest_of_line ();
1032 expression_and_evaluate (&fde->personality);
1033 switch (fde->personality.X_op)
1038 if ((encoding & 0x70) == DW_EH_PE_pcrel)
1039 encoding = DW_EH_PE_omit;
1042 encoding = DW_EH_PE_omit;
1046 fde->per_encoding = encoding;
1048 if (encoding == DW_EH_PE_omit)
1050 as_bad (_("wrong second argument to .cfi_personality"));
1051 ignore_rest_of_line ();
1055 demand_empty_rest_of_line ();
1059 dot_cfi_lsda (int ignored ATTRIBUTE_UNUSED)
1061 struct fde_entry *fde;
1064 if (frchain_now->frch_cfi_data == NULL)
1066 as_bad (_("CFI instruction used without previous .cfi_startproc"));
1067 ignore_rest_of_line ();
1071 fde = frchain_now->frch_cfi_data->cur_fde_data;
1072 encoding = cfi_parse_const ();
1073 if (encoding == DW_EH_PE_omit)
1075 demand_empty_rest_of_line ();
1076 fde->lsda_encoding = encoding;
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
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))
1091 as_bad (_("invalid or unsupported encoding in .cfi_lsda"));
1092 ignore_rest_of_line ();
1096 if (*input_line_pointer++ != ',')
1098 as_bad (_(".cfi_lsda requires encoding and symbol arguments"));
1099 ignore_rest_of_line ();
1103 fde->lsda_encoding = encoding;
1105 expression_and_evaluate (&fde->lsda);
1106 switch (fde->lsda.X_op)
1111 if ((encoding & 0x70) == DW_EH_PE_pcrel)
1112 encoding = DW_EH_PE_omit;
1115 encoding = DW_EH_PE_omit;
1119 fde->lsda_encoding = encoding;
1121 if (encoding == DW_EH_PE_omit)
1123 as_bad (_("wrong second argument to .cfi_lsda"));
1124 ignore_rest_of_line ();
1128 demand_empty_rest_of_line ();
1132 dot_cfi_val_encoded_addr (int ignored ATTRIBUTE_UNUSED)
1134 struct cfi_insn_data *insn_ptr;
1137 if (frchain_now->frch_cfi_data == NULL)
1139 as_bad (_("CFI instruction used without previous .cfi_startproc"));
1140 ignore_rest_of_line ();
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)
1148 cfi_add_advance_loc (symbol_temp_new_now ());
1150 insn_ptr = alloc_cfi_insn_data ();
1151 insn_ptr->insn = CFI_val_encoded_addr;
1153 insn_ptr->u.ea.reg = cfi_parse_reg ();
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
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)
1167 as_bad (_("invalid or unsupported encoding in .cfi_lsda"));
1168 encoding = DW_EH_PE_omit;
1171 cfi_parse_separator ();
1172 expression_and_evaluate (&insn_ptr->u.ea.exp);
1173 switch (insn_ptr->u.ea.exp.X_op)
1178 if ((encoding & 0x70) != DW_EH_PE_pcrel)
1182 encoding = DW_EH_PE_omit;
1186 insn_ptr->u.ea.encoding = encoding;
1187 if (encoding == DW_EH_PE_omit)
1189 as_bad (_("wrong third argument to .cfi_val_encoded_addr"));
1190 ignore_rest_of_line ();
1194 demand_empty_rest_of_line ();
1198 dot_cfi_label (int ignored ATTRIBUTE_UNUSED)
1200 char *name = read_symbol_name ();
1205 /* If the last address was not at the current PC, advance to current. */
1206 if (symbol_get_frag (frchain_now->frch_cfi_data->last_address) != frag_now
1207 || S_GET_VALUE (frchain_now->frch_cfi_data->last_address)
1209 cfi_add_advance_loc (symbol_temp_new_now ());
1211 cfi_add_label (name);
1214 demand_empty_rest_of_line ();
1218 dot_cfi_sections (int ignored ATTRIBUTE_UNUSED)
1223 if (is_name_beginner (*input_line_pointer) || *input_line_pointer == '"')
1229 saved_ilp = input_line_pointer;
1230 c = get_symbol_name (& name);
1232 if (strncmp (name, ".eh_frame", sizeof ".eh_frame") == 0
1234 sections |= CFI_EMIT_eh_frame;
1235 else if (strncmp (name, ".debug_frame", sizeof ".debug_frame") == 0)
1236 sections |= CFI_EMIT_debug_frame;
1237 #if SUPPORT_COMPACT_EH
1238 else if (strncmp (name, ".eh_frame_entry", sizeof ".eh_frame_entry") == 0)
1241 sections |= CFI_EMIT_eh_frame_compact;
1244 #ifdef tc_cfi_section_name
1245 else if (strcmp (name, tc_cfi_section_name) == 0)
1246 sections |= CFI_EMIT_target;
1250 *input_line_pointer = c;
1251 input_line_pointer = saved_ilp;
1255 *input_line_pointer = c;
1256 SKIP_WHITESPACE_AFTER_NAME ();
1257 if (*input_line_pointer == ',')
1259 name = input_line_pointer++;
1261 if (!is_name_beginner (*input_line_pointer) && *input_line_pointer != '"')
1263 input_line_pointer = name;
1267 else if (is_name_beginner (*input_line_pointer) || *input_line_pointer == '"')
1271 demand_empty_rest_of_line ();
1272 if (cfi_sections_set && cfi_sections != sections)
1273 as_bad (_("inconsistent uses of .cfi_sections"));
1274 cfi_sections = sections;
1278 dot_cfi_startproc (int ignored ATTRIBUTE_UNUSED)
1282 if (frchain_now->frch_cfi_data != NULL)
1284 as_bad (_("previous CFI entry not closed (missing .cfi_endproc)"));
1285 ignore_rest_of_line ();
1289 cfi_new_fde (symbol_temp_new_now ());
1292 if (is_name_beginner (*input_line_pointer) || *input_line_pointer == '"')
1294 char * saved_ilp = input_line_pointer;
1297 c = get_symbol_name (& name);
1299 if (strcmp (name, "simple") == 0)
1302 restore_line_pointer (c);
1305 input_line_pointer = saved_ilp;
1307 demand_empty_rest_of_line ();
1309 cfi_sections_set = TRUE;
1310 all_cfi_sections |= cfi_sections;
1311 cfi_set_sections ();
1312 frchain_now->frch_cfi_data->cur_cfa_offset = 0;
1314 tc_cfi_frame_initial_instructions ();
1316 if ((cfi_sections & CFI_EMIT_target) != 0)
1317 tc_cfi_startproc ();
1321 dot_cfi_endproc (int ignored ATTRIBUTE_UNUSED)
1323 if (frchain_now->frch_cfi_data == NULL)
1325 as_bad (_(".cfi_endproc without corresponding .cfi_startproc"));
1326 ignore_rest_of_line ();
1330 last_fde = frchain_now->frch_cfi_data->cur_fde_data;
1332 cfi_end_fde (symbol_temp_new_now ());
1334 demand_empty_rest_of_line ();
1336 cfi_sections_set = TRUE;
1337 if ((cfi_sections & CFI_EMIT_target) != 0)
1338 tc_cfi_endproc (last_fde);
1342 get_cfi_seg (segT cseg, const char *base, flagword flags, int align)
1344 /* Exclude .debug_frame sections for Compact EH. */
1345 if (SUPPORT_FRAME_LINKONCE || ((flags & SEC_DEBUGGING) == 0 && compact_eh))
1347 struct dwcfi_seg_list *l;
1349 l = dwcfi_hash_find_or_make (cseg, base, flags);
1352 subseg_set (cseg, l->subseg);
1356 cseg = subseg_new (base, 0);
1357 bfd_set_section_flags (stdoutput, cseg, flags);
1359 record_alignment (cseg, align);
1363 #if SUPPORT_COMPACT_EH
1365 dot_cfi_personality_id (int ignored ATTRIBUTE_UNUSED)
1367 struct fde_entry *fde;
1369 if (frchain_now->frch_cfi_data == NULL)
1371 as_bad (_("CFI instruction used without previous .cfi_startproc"));
1372 ignore_rest_of_line ();
1376 fde = frchain_now->frch_cfi_data->cur_fde_data;
1377 fde->personality_id = cfi_parse_const ();
1378 demand_empty_rest_of_line ();
1380 if (fde->personality_id == 0 || fde->personality_id > 3)
1382 as_bad (_("wrong argument to .cfi_personality_id"));
1388 dot_cfi_fde_data (int ignored ATTRIBUTE_UNUSED)
1390 if (frchain_now->frch_cfi_data == NULL)
1392 as_bad (_(".cfi_fde_data without corresponding .cfi_startproc"));
1393 ignore_rest_of_line ();
1397 last_fde = frchain_now->frch_cfi_data->cur_fde_data;
1399 cfi_sections_set = TRUE;
1400 if ((cfi_sections & CFI_EMIT_target) != 0
1401 || (cfi_sections & CFI_EMIT_eh_frame_compact) != 0)
1403 struct cfi_escape_data *head, **tail, *e;
1407 if (!is_it_end_of_statement ())
1412 e = XNEW (struct cfi_escape_data);
1413 do_parse_cons_expression (&e->exp, 1);
1418 while (*input_line_pointer++ == ',');
1419 --input_line_pointer;
1423 if (last_fde->lsda_encoding != DW_EH_PE_omit)
1424 last_fde->eh_header_type = EH_COMPACT_HAS_LSDA;
1425 else if (num_ops <= 3 && last_fde->per_encoding == DW_EH_PE_omit)
1426 last_fde->eh_header_type = EH_COMPACT_INLINE;
1428 last_fde->eh_header_type = EH_COMPACT_OUTLINE;
1430 if (last_fde->eh_header_type == EH_COMPACT_INLINE)
1433 last_fde->eh_data_size = num_ops;
1434 last_fde->eh_data = XNEWVEC (bfd_byte, num_ops);
1440 last_fde->eh_data[num_ops++] = e->exp.X_add_number;
1443 if (last_fde->eh_header_type == EH_COMPACT_INLINE)
1445 last_fde->eh_data[num_ops++] = tc_compact_eh_opcode_stop;
1448 demand_empty_rest_of_line ();
1451 /* Function to emit the compact unwinding opcodes stored in the
1452 fde's eh_data field. The end of the opcode data will be
1453 padded to the value in align. */
1456 output_compact_unwind_data (struct fde_entry *fde, int align)
1458 int data_size = fde->eh_data_size + 2;
1463 fde->eh_loc = symbol_temp_new_now ();
1466 if (fde->personality_id != 0)
1467 *p = fde->personality_id;
1468 else if (fde->per_encoding != DW_EH_PE_omit)
1471 emit_expr_encoded (&fde->personality, fde->per_encoding, FALSE);
1472 data_size += encoding_size (fde->per_encoding);
1477 amask = (1 << align) - 1;
1478 align_padding = ((data_size + amask) & ~amask) - data_size;
1480 p = frag_more (fde->eh_data_size + 1 + align_padding);
1481 memcpy (p, fde->eh_data, fde->eh_data_size);
1482 p += fde->eh_data_size;
1484 while (align_padding-- > 0)
1485 *(p++) = tc_compact_eh_opcode_pad;
1487 *(p++) = tc_compact_eh_opcode_stop;
1488 fde->eh_header_type = EH_COMPACT_OUTLINE_DONE;
1491 /* Handle the .cfi_inline_lsda directive. */
1493 dot_cfi_inline_lsda (int ignored ATTRIBUTE_UNUSED)
1497 long max_alignment = 28;
1501 as_bad (_("unexpected .cfi_inline_lsda"));
1502 ignore_rest_of_line ();
1506 if ((last_fde->sections & CFI_EMIT_eh_frame_compact) == 0)
1508 as_bad (_(".cfi_inline_lsda not valid for this frame"));
1509 ignore_rest_of_line ();
1513 if (last_fde->eh_header_type != EH_COMPACT_UNKNOWN
1514 && last_fde->eh_header_type != EH_COMPACT_HAS_LSDA)
1516 as_bad (_(".cfi_inline_lsda seen for frame without .cfi_lsda"));
1517 ignore_rest_of_line ();
1521 #ifdef md_flush_pending_output
1522 md_flush_pending_output ();
1525 align = get_absolute_expression ();
1526 if (align > max_alignment)
1528 align = max_alignment;
1529 as_bad (_("Alignment too large: %d. assumed."), align);
1533 as_warn (_("Alignment negative: 0 assumed."));
1537 demand_empty_rest_of_line ();
1538 ccseg = CUR_SEG (last_fde);
1540 /* Open .gnu_extab section. */
1541 get_cfi_seg (ccseg, ".gnu_extab",
1542 (SEC_ALLOC | SEC_LOAD | SEC_DATA
1543 | DWARF2_EH_FRAME_READ_ONLY),
1546 frag_align (align, 0, 0);
1547 record_alignment (now_seg, align);
1548 if (last_fde->eh_header_type == EH_COMPACT_HAS_LSDA)
1549 output_compact_unwind_data (last_fde, align);
1555 #else /* !SUPPORT_COMPACT_EH */
1557 dot_cfi_inline_lsda (int ignored ATTRIBUTE_UNUSED)
1559 as_bad (_(".cfi_inline_lsda is not supported for this target"));
1560 ignore_rest_of_line ();
1564 dot_cfi_fde_data (int ignored ATTRIBUTE_UNUSED)
1566 as_bad (_(".cfi_fde_data is not supported for this target"));
1567 ignore_rest_of_line ();
1571 dot_cfi_personality_id (int ignored ATTRIBUTE_UNUSED)
1573 as_bad (_(".cfi_personality_id is not supported for this target"));
1574 ignore_rest_of_line ();
1579 output_cfi_insn (struct cfi_insn_data *insn)
1586 case DW_CFA_advance_loc:
1588 symbolS *from = insn->u.ll.lab1;
1589 symbolS *to = insn->u.ll.lab2;
1591 if (symbol_get_frag (to) == symbol_get_frag (from))
1593 addressT delta = S_GET_VALUE (to) - S_GET_VALUE (from);
1594 addressT scaled = delta / DWARF2_LINE_MIN_INSN_LENGTH;
1597 out_one (DW_CFA_advance_loc + scaled);
1598 else if (scaled <= 0xFF)
1600 out_one (DW_CFA_advance_loc1);
1603 else if (scaled <= 0xFFFF)
1605 out_one (DW_CFA_advance_loc2);
1610 out_one (DW_CFA_advance_loc4);
1618 exp.X_op = O_subtract;
1619 exp.X_add_symbol = to;
1620 exp.X_op_symbol = from;
1621 exp.X_add_number = 0;
1623 /* The code in ehopt.c expects that one byte of the encoding
1624 is already allocated to the frag. This comes from the way
1625 that it scans the .eh_frame section looking first for the
1626 .byte DW_CFA_advance_loc4. */
1627 *frag_more (1) = DW_CFA_advance_loc4;
1629 frag_var (rs_cfa, 4, 0, DWARF2_LINE_MIN_INSN_LENGTH << 3,
1630 make_expr_symbol (&exp), frag_now_fix () - 1,
1636 case DW_CFA_def_cfa:
1637 offset = insn->u.ri.offset;
1640 out_one (DW_CFA_def_cfa_sf);
1641 out_uleb128 (insn->u.ri.reg);
1642 out_sleb128 (offset / DWARF2_CIE_DATA_ALIGNMENT);
1646 out_one (DW_CFA_def_cfa);
1647 out_uleb128 (insn->u.ri.reg);
1648 out_uleb128 (offset);
1652 case DW_CFA_def_cfa_register:
1653 case DW_CFA_undefined:
1654 case DW_CFA_same_value:
1655 out_one (insn->insn);
1656 out_uleb128 (insn->u.r);
1659 case DW_CFA_def_cfa_offset:
1663 out_one (DW_CFA_def_cfa_offset_sf);
1664 out_sleb128 (offset / DWARF2_CIE_DATA_ALIGNMENT);
1668 out_one (DW_CFA_def_cfa_offset);
1669 out_uleb128 (offset);
1673 case DW_CFA_restore:
1677 out_one (DW_CFA_restore + regno);
1681 out_one (DW_CFA_restore_extended);
1682 out_uleb128 (regno);
1687 regno = insn->u.ri.reg;
1688 offset = insn->u.ri.offset / DWARF2_CIE_DATA_ALIGNMENT;
1691 out_one (DW_CFA_offset_extended_sf);
1692 out_uleb128 (regno);
1693 out_sleb128 (offset);
1695 else if (regno <= 0x3F)
1697 out_one (DW_CFA_offset + regno);
1698 out_uleb128 (offset);
1702 out_one (DW_CFA_offset_extended);
1703 out_uleb128 (regno);
1704 out_uleb128 (offset);
1708 case DW_CFA_val_offset:
1709 regno = insn->u.ri.reg;
1710 offset = insn->u.ri.offset / DWARF2_CIE_DATA_ALIGNMENT;
1713 out_one (DW_CFA_val_offset_sf);
1714 out_uleb128 (regno);
1715 out_sleb128 (offset);
1719 out_one (DW_CFA_val_offset);
1720 out_uleb128 (regno);
1721 out_uleb128 (offset);
1725 case DW_CFA_register:
1726 out_one (DW_CFA_register);
1727 out_uleb128 (insn->u.rr.reg1);
1728 out_uleb128 (insn->u.rr.reg2);
1731 case DW_CFA_remember_state:
1732 case DW_CFA_restore_state:
1733 out_one (insn->insn);
1736 case DW_CFA_GNU_window_save:
1737 out_one (DW_CFA_GNU_window_save);
1742 struct cfi_escape_data *e;
1743 for (e = insn->u.esc; e ; e = e->next)
1744 emit_expr (&e->exp, 1);
1748 case CFI_val_encoded_addr:
1750 unsigned encoding = insn->u.ea.encoding;
1753 if (encoding == DW_EH_PE_omit)
1755 out_one (DW_CFA_val_expression);
1756 out_uleb128 (insn->u.ea.reg);
1758 switch (encoding & 0x7)
1760 case DW_EH_PE_absptr:
1761 enc_size = DWARF2_ADDR_SIZE (stdoutput);
1763 case DW_EH_PE_udata2:
1766 case DW_EH_PE_udata4:
1769 case DW_EH_PE_udata8:
1776 /* If the user has requested absolute encoding,
1777 then use the smaller DW_OP_addr encoding. */
1778 if (insn->u.ea.encoding == DW_EH_PE_absptr)
1780 out_uleb128 (1 + enc_size);
1781 out_one (DW_OP_addr);
1785 out_uleb128 (1 + 1 + enc_size);
1786 out_one (DW_OP_GNU_encoded_addr);
1789 if ((encoding & 0x70) == DW_EH_PE_pcrel)
1791 #if CFI_DIFF_EXPR_OK
1792 insn->u.ea.exp.X_op = O_subtract;
1793 insn->u.ea.exp.X_op_symbol = symbol_temp_new_now ();
1794 #elif defined (tc_cfi_emit_pcrel_expr)
1795 tc_cfi_emit_pcrel_expr (&insn->u.ea.exp, enc_size);
1802 emit_expr (&insn->u.ea.exp, enc_size);
1807 colon (insn->u.sym_name);
1816 output_cie (struct cie_entry *cie, bfd_boolean eh_frame, int align)
1818 symbolS *after_size_address, *end_address;
1820 struct cfi_insn_data *i;
1821 offsetT augmentation_size;
1823 enum dwarf2_format fmt = DWARF2_FORMAT (now_seg);
1825 cie->start_address = symbol_temp_new_now ();
1826 after_size_address = symbol_temp_make ();
1827 end_address = symbol_temp_make ();
1829 exp.X_op = O_subtract;
1830 exp.X_add_symbol = end_address;
1831 exp.X_op_symbol = after_size_address;
1832 exp.X_add_number = 0;
1834 if (eh_frame || fmt == dwarf2_format_32bit)
1835 emit_expr (&exp, 4); /* Length. */
1838 if (fmt == dwarf2_format_64bit)
1840 emit_expr (&exp, 8); /* Length. */
1842 symbol_set_value_now (after_size_address);
1844 out_four (0); /* CIE id. */
1847 out_four (-1); /* CIE id. */
1848 if (fmt != dwarf2_format_32bit)
1851 out_one (DW_CIE_VERSION); /* Version. */
1854 out_one ('z'); /* Augmentation. */
1855 if (cie->per_encoding != DW_EH_PE_omit)
1857 if (cie->lsda_encoding != DW_EH_PE_omit)
1861 if (cie->signal_frame)
1864 out_uleb128 (DWARF2_LINE_MIN_INSN_LENGTH); /* Code alignment. */
1865 out_sleb128 (DWARF2_CIE_DATA_ALIGNMENT); /* Data alignment. */
1866 if (DW_CIE_VERSION == 1) /* Return column. */
1867 out_one (cie->return_column);
1869 out_uleb128 (cie->return_column);
1872 augmentation_size = 1 + (cie->lsda_encoding != DW_EH_PE_omit);
1873 if (cie->per_encoding != DW_EH_PE_omit)
1874 augmentation_size += 1 + encoding_size (cie->per_encoding);
1875 out_uleb128 (augmentation_size); /* Augmentation size. */
1877 emit_expr_encoded (&cie->personality, cie->per_encoding, TRUE);
1879 if (cie->lsda_encoding != DW_EH_PE_omit)
1880 out_one (cie->lsda_encoding);
1883 switch (DWARF2_FDE_RELOC_SIZE)
1886 enc = DW_EH_PE_sdata2;
1889 enc = DW_EH_PE_sdata4;
1892 enc = DW_EH_PE_sdata8;
1897 #if CFI_DIFF_EXPR_OK || defined tc_cfi_emit_pcrel_expr
1898 enc |= DW_EH_PE_pcrel;
1900 #ifdef DWARF2_FDE_RELOC_ENCODING
1901 /* Allow target to override encoding. */
1902 enc = DWARF2_FDE_RELOC_ENCODING (enc);
1904 cie->fde_encoding = enc;
1910 for (i = cie->first; i != cie->last; i = i->next)
1912 if (CUR_SEG (i) != CUR_SEG (cie))
1914 output_cfi_insn (i);
1918 frag_align (align, DW_CFA_nop, 0);
1919 symbol_set_value_now (end_address);
1923 output_fde (struct fde_entry *fde, struct cie_entry *cie,
1924 bfd_boolean eh_frame, struct cfi_insn_data *first,
1927 symbolS *after_size_address, *end_address;
1929 offsetT augmentation_size;
1930 enum dwarf2_format fmt = DWARF2_FORMAT (now_seg);
1934 after_size_address = symbol_temp_make ();
1935 end_address = symbol_temp_make ();
1937 exp.X_op = O_subtract;
1938 exp.X_add_symbol = end_address;
1939 exp.X_op_symbol = after_size_address;
1940 exp.X_add_number = 0;
1941 if (eh_frame || fmt == dwarf2_format_32bit)
1945 if (fmt == dwarf2_format_64bit)
1949 emit_expr (&exp, offset_size); /* Length. */
1950 symbol_set_value_now (after_size_address);
1954 exp.X_op = O_subtract;
1955 exp.X_add_symbol = after_size_address;
1956 exp.X_op_symbol = cie->start_address;
1957 exp.X_add_number = 0;
1958 emit_expr (&exp, offset_size); /* CIE offset. */
1962 TC_DWARF2_EMIT_OFFSET (cie->start_address, offset_size);
1965 exp.X_op = O_symbol;
1968 bfd_reloc_code_real_type code
1969 = tc_cfi_reloc_for_encoding (cie->fde_encoding);
1970 if (code != BFD_RELOC_NONE)
1972 reloc_howto_type *howto = bfd_reloc_type_lookup (stdoutput, code);
1973 char *p = frag_more (4);
1974 md_number_to_chars (p, 0, 4);
1975 fix_new (frag_now, p - frag_now->fr_literal, 4, fde->start_address,
1976 0, howto->pc_relative, code);
1980 exp.X_op = O_subtract;
1981 exp.X_add_number = 0;
1982 #if CFI_DIFF_EXPR_OK
1983 exp.X_add_symbol = fde->start_address;
1984 exp.X_op_symbol = symbol_temp_new_now ();
1985 emit_expr (&exp, DWARF2_FDE_RELOC_SIZE); /* Code offset. */
1987 exp.X_op = O_symbol;
1988 exp.X_add_symbol = fde->start_address;
1990 #if defined(tc_cfi_emit_pcrel_expr)
1991 tc_cfi_emit_pcrel_expr (&exp, DWARF2_FDE_RELOC_SIZE); /* Code offset. */
1993 emit_expr (&exp, DWARF2_FDE_RELOC_SIZE); /* Code offset. */
1997 addr_size = DWARF2_FDE_RELOC_SIZE;
2001 exp.X_add_number = 0;
2002 exp.X_add_symbol = fde->start_address;
2003 addr_size = DWARF2_ADDR_SIZE (stdoutput);
2004 emit_expr (&exp, addr_size);
2007 exp.X_op = O_subtract;
2008 exp.X_add_symbol = fde->end_address;
2009 exp.X_op_symbol = fde->start_address; /* Code length. */
2010 exp.X_add_number = 0;
2011 emit_expr (&exp, addr_size);
2013 augmentation_size = encoding_size (fde->lsda_encoding);
2015 out_uleb128 (augmentation_size); /* Augmentation size. */
2017 emit_expr_encoded (&fde->lsda, cie->lsda_encoding, FALSE);
2019 for (; first; first = first->next)
2020 if (CUR_SEG (first) == CUR_SEG (fde))
2021 output_cfi_insn (first);
2023 frag_align (align, DW_CFA_nop, 0);
2024 symbol_set_value_now (end_address);
2027 static struct cie_entry *
2028 select_cie_for_fde (struct fde_entry *fde, bfd_boolean eh_frame,
2029 struct cfi_insn_data **pfirst, int align)
2031 struct cfi_insn_data *i, *j;
2032 struct cie_entry *cie;
2034 for (cie = cie_root; cie; cie = cie->next)
2036 if (CUR_SEG (cie) != CUR_SEG (fde))
2038 if (cie->return_column != fde->return_column
2039 || cie->signal_frame != fde->signal_frame
2040 || cie->per_encoding != fde->per_encoding
2041 || cie->lsda_encoding != fde->lsda_encoding)
2043 if (cie->per_encoding != DW_EH_PE_omit)
2045 if (cie->personality.X_op != fde->personality.X_op
2046 || cie->personality.X_add_number
2047 != fde->personality.X_add_number)
2049 switch (cie->personality.X_op)
2052 if (cie->personality.X_unsigned != fde->personality.X_unsigned)
2056 if (cie->personality.X_add_symbol
2057 != fde->personality.X_add_symbol)
2064 for (i = cie->first, j = fde->data;
2065 i != cie->last && j != NULL;
2066 i = i->next, j = j->next)
2068 if (i->insn != j->insn)
2072 case DW_CFA_advance_loc:
2073 case DW_CFA_remember_state:
2074 /* We reached the first advance/remember in the FDE,
2075 but did not reach the end of the CIE list. */
2079 case DW_CFA_def_cfa:
2080 if (i->u.ri.reg != j->u.ri.reg)
2082 if (i->u.ri.offset != j->u.ri.offset)
2086 case DW_CFA_register:
2087 if (i->u.rr.reg1 != j->u.rr.reg1)
2089 if (i->u.rr.reg2 != j->u.rr.reg2)
2093 case DW_CFA_def_cfa_register:
2094 case DW_CFA_restore:
2095 case DW_CFA_undefined:
2096 case DW_CFA_same_value:
2097 if (i->u.r != j->u.r)
2101 case DW_CFA_def_cfa_offset:
2102 if (i->u.i != j->u.i)
2107 case CFI_val_encoded_addr:
2109 /* Don't bother matching these for now. */
2117 /* Success if we reached the end of the CIE list, and we've either
2118 run out of FDE entries or we've encountered an advance,
2119 remember, or escape. */
2122 || j->insn == DW_CFA_advance_loc
2123 || j->insn == DW_CFA_remember_state
2124 || j->insn == CFI_escape
2125 || j->insn == CFI_val_encoded_addr
2126 || j->insn == CFI_label))
2135 cie = XNEW (struct cie_entry);
2136 cie->next = cie_root;
2138 SET_CUR_SEG (cie, CUR_SEG (fde));
2139 cie->return_column = fde->return_column;
2140 cie->signal_frame = fde->signal_frame;
2141 cie->per_encoding = fde->per_encoding;
2142 cie->lsda_encoding = fde->lsda_encoding;
2143 cie->personality = fde->personality;
2144 cie->first = fde->data;
2146 for (i = cie->first; i ; i = i->next)
2147 if (i->insn == DW_CFA_advance_loc
2148 || i->insn == DW_CFA_remember_state
2149 || i->insn == CFI_escape
2150 || i->insn == CFI_val_encoded_addr
2151 || i->insn == CFI_label)
2157 output_cie (cie, eh_frame, align);
2162 #ifdef md_reg_eh_frame_to_debug_frame
2164 cfi_change_reg_numbers (struct cfi_insn_data *insn, segT ccseg)
2166 for (; insn; insn = insn->next)
2168 if (CUR_SEG (insn) != ccseg)
2172 case DW_CFA_advance_loc:
2173 case DW_CFA_def_cfa_offset:
2174 case DW_CFA_remember_state:
2175 case DW_CFA_restore_state:
2176 case DW_CFA_GNU_window_save:
2181 case DW_CFA_def_cfa:
2183 insn->u.ri.reg = md_reg_eh_frame_to_debug_frame (insn->u.ri.reg);
2186 case DW_CFA_def_cfa_register:
2187 case DW_CFA_undefined:
2188 case DW_CFA_same_value:
2189 case DW_CFA_restore:
2190 insn->u.r = md_reg_eh_frame_to_debug_frame (insn->u.r);
2193 case DW_CFA_register:
2194 insn->u.rr.reg1 = md_reg_eh_frame_to_debug_frame (insn->u.rr.reg1);
2195 insn->u.rr.reg2 = md_reg_eh_frame_to_debug_frame (insn->u.rr.reg2);
2198 case CFI_val_encoded_addr:
2199 insn->u.ea.reg = md_reg_eh_frame_to_debug_frame (insn->u.ea.reg);
2208 #define cfi_change_reg_numbers(insn, cseg) do { } while (0)
2211 #if SUPPORT_COMPACT_EH
2213 cfi_emit_eh_header (symbolS *sym, bfd_vma addend)
2217 exp.X_add_number = addend;
2218 exp.X_add_symbol = sym;
2219 emit_expr_encoded (&exp, DW_EH_PE_sdata4 | DW_EH_PE_pcrel, FALSE);
2223 output_eh_header (struct fde_entry *fde)
2228 if (fde->eh_header_type == EH_COMPACT_INLINE)
2233 cfi_emit_eh_header (fde->start_address, addend);
2235 if (fde->eh_header_type == EH_COMPACT_INLINE)
2238 /* Inline entries always use PR1. */
2240 memcpy(p, fde->eh_data, 3);
2244 if (fde->eh_header_type == EH_COMPACT_LEGACY)
2246 else if (fde->eh_header_type == EH_COMPACT_OUTLINE
2247 || fde->eh_header_type == EH_COMPACT_OUTLINE_DONE)
2251 cfi_emit_eh_header (fde->eh_loc, addend);
2259 struct cie_entry *cie, *cie_next;
2260 segT cfi_seg, ccseg;
2261 struct fde_entry *fde;
2262 struct cfi_insn_data *first;
2263 int save_flag_traditional_format, seek_next_seg;
2265 if (all_fde_data == 0)
2268 cfi_sections_set = TRUE;
2269 if ((all_cfi_sections & CFI_EMIT_eh_frame) != 0
2270 || (all_cfi_sections & CFI_EMIT_eh_frame_compact) != 0)
2272 /* Make sure check_eh_frame doesn't do anything with our output. */
2273 save_flag_traditional_format = flag_traditional_format;
2274 flag_traditional_format = 1;
2276 if (!EH_FRAME_LINKONCE)
2278 /* Open .eh_frame section. */
2279 cfi_seg = get_cfi_seg (NULL, ".eh_frame",
2280 (SEC_ALLOC | SEC_LOAD | SEC_DATA
2281 | DWARF2_EH_FRAME_READ_ONLY),
2282 EH_FRAME_ALIGNMENT);
2283 #ifdef md_fix_up_eh_frame
2284 md_fix_up_eh_frame (cfi_seg);
2295 for (cie = cie_root; cie; cie = cie_next)
2297 cie_next = cie->next;
2298 free ((void *) cie);
2302 for (fde = all_fde_data; fde ; fde = fde->next)
2304 if ((fde->sections & CFI_EMIT_eh_frame) == 0
2305 && (fde->sections & CFI_EMIT_eh_frame_compact) == 0)
2308 #if SUPPORT_COMPACT_EH
2309 /* Emit a LEGACY format header if we have processed all
2310 of the .cfi directives without encountering either inline or
2311 out-of-line compact unwinding opcodes. */
2312 if (fde->eh_header_type == EH_COMPACT_HAS_LSDA
2313 || fde->eh_header_type == EH_COMPACT_UNKNOWN)
2314 fde->eh_header_type = EH_COMPACT_LEGACY;
2316 if (fde->eh_header_type != EH_COMPACT_LEGACY)
2319 if (EH_FRAME_LINKONCE)
2323 if (seek_next_seg && CUR_SEG (fde) != ccseg)
2330 ccseg = CUR_SEG (fde);
2331 /* Open .eh_frame section. */
2332 cfi_seg = get_cfi_seg (ccseg, ".eh_frame",
2333 (SEC_ALLOC | SEC_LOAD | SEC_DATA
2334 | DWARF2_EH_FRAME_READ_ONLY),
2335 EH_FRAME_ALIGNMENT);
2336 #ifdef md_fix_up_eh_frame
2337 md_fix_up_eh_frame (cfi_seg);
2343 SET_HANDLED (fde, 1);
2346 if (fde->end_address == NULL)
2348 as_bad (_("open CFI at the end of file; missing .cfi_endproc directive"));
2349 fde->end_address = fde->start_address;
2352 cie = select_cie_for_fde (fde, TRUE, &first, 2);
2353 fde->eh_loc = symbol_temp_new_now ();
2354 output_fde (fde, cie, TRUE, first,
2355 fde->next == NULL ? EH_FRAME_ALIGNMENT : 2);
2358 while (EH_FRAME_LINKONCE && seek_next_seg == 2);
2360 if (EH_FRAME_LINKONCE)
2361 for (fde = all_fde_data; fde ; fde = fde->next)
2362 SET_HANDLED (fde, 0);
2364 #if SUPPORT_COMPACT_EH
2367 /* Create remaining out of line table entries. */
2373 for (fde = all_fde_data; fde ; fde = fde->next)
2375 if ((fde->sections & CFI_EMIT_eh_frame) == 0
2376 && (fde->sections & CFI_EMIT_eh_frame_compact) == 0)
2379 if (fde->eh_header_type != EH_COMPACT_OUTLINE)
2383 if (seek_next_seg && CUR_SEG (fde) != ccseg)
2390 ccseg = CUR_SEG (fde);
2391 /* Open .gnu_extab section. */
2392 get_cfi_seg (ccseg, ".gnu_extab",
2393 (SEC_ALLOC | SEC_LOAD | SEC_DATA
2394 | DWARF2_EH_FRAME_READ_ONLY),
2398 SET_HANDLED (fde, 1);
2400 frag_align (1, 0, 0);
2401 record_alignment (now_seg, 1);
2402 output_compact_unwind_data (fde, 1);
2405 while (EH_FRAME_LINKONCE && seek_next_seg == 2);
2407 for (fde = all_fde_data; fde ; fde = fde->next)
2408 SET_HANDLED (fde, 0);
2410 /* Create index table fragments. */
2416 for (fde = all_fde_data; fde ; fde = fde->next)
2418 if ((fde->sections & CFI_EMIT_eh_frame) == 0
2419 && (fde->sections & CFI_EMIT_eh_frame_compact) == 0)
2424 if (seek_next_seg && CUR_SEG (fde) != ccseg)
2431 ccseg = CUR_SEG (fde);
2432 /* Open .eh_frame_entry section. */
2433 cfi_seg = get_cfi_seg (ccseg, ".eh_frame_entry",
2434 (SEC_ALLOC | SEC_LOAD | SEC_DATA
2435 | DWARF2_EH_FRAME_READ_ONLY),
2439 SET_HANDLED (fde, 1);
2441 output_eh_header (fde);
2444 while (seek_next_seg == 2);
2446 for (fde = all_fde_data; fde ; fde = fde->next)
2447 SET_HANDLED (fde, 0);
2449 #endif /* SUPPORT_COMPACT_EH */
2451 flag_traditional_format = save_flag_traditional_format;
2454 cfi_sections_set = TRUE;
2455 if ((all_cfi_sections & CFI_EMIT_debug_frame) != 0)
2457 int alignment = ffs (DWARF2_ADDR_SIZE (stdoutput)) - 1;
2459 if (!SUPPORT_FRAME_LINKONCE)
2460 get_cfi_seg (NULL, ".debug_frame",
2461 SEC_READONLY | SEC_DEBUGGING,
2469 for (cie = cie_root; cie; cie = cie_next)
2471 cie_next = cie->next;
2472 free ((void *) cie);
2476 for (fde = all_fde_data; fde ; fde = fde->next)
2478 if ((fde->sections & CFI_EMIT_debug_frame) == 0)
2481 if (SUPPORT_FRAME_LINKONCE)
2485 if (seek_next_seg && CUR_SEG (fde) != ccseg)
2492 ccseg = CUR_SEG (fde);
2493 /* Open .debug_frame section. */
2494 get_cfi_seg (ccseg, ".debug_frame",
2495 SEC_READONLY | SEC_DEBUGGING,
2499 SET_HANDLED (fde, 1);
2501 if (fde->end_address == NULL)
2503 as_bad (_("open CFI at the end of file; missing .cfi_endproc directive"));
2504 fde->end_address = fde->start_address;
2507 fde->per_encoding = DW_EH_PE_omit;
2508 fde->lsda_encoding = DW_EH_PE_omit;
2509 cfi_change_reg_numbers (fde->data, ccseg);
2510 cie = select_cie_for_fde (fde, FALSE, &first, alignment);
2511 output_fde (fde, cie, FALSE, first, alignment);
2514 while (SUPPORT_FRAME_LINKONCE && seek_next_seg == 2);
2516 if (SUPPORT_FRAME_LINKONCE)
2517 for (fde = all_fde_data; fde ; fde = fde->next)
2518 SET_HANDLED (fde, 0);
2522 #else /* TARGET_USE_CFIPOP */
2524 /* Emit an intelligible error message for missing support. */
2527 dot_cfi_dummy (int ignored ATTRIBUTE_UNUSED)
2529 as_bad (_("CFI is not supported for this target"));
2530 ignore_rest_of_line ();
2533 const pseudo_typeS cfi_pseudo_table[] =
2535 { "cfi_sections", dot_cfi_dummy, 0 },
2536 { "cfi_startproc", dot_cfi_dummy, 0 },
2537 { "cfi_endproc", dot_cfi_dummy, 0 },
2538 { "cfi_fde_data", dot_cfi_dummy, 0 },
2539 { "cfi_def_cfa", dot_cfi_dummy, 0 },
2540 { "cfi_def_cfa_register", dot_cfi_dummy, 0 },
2541 { "cfi_def_cfa_offset", dot_cfi_dummy, 0 },
2542 { "cfi_adjust_cfa_offset", dot_cfi_dummy, 0 },
2543 { "cfi_offset", dot_cfi_dummy, 0 },
2544 { "cfi_rel_offset", dot_cfi_dummy, 0 },
2545 { "cfi_register", dot_cfi_dummy, 0 },
2546 { "cfi_return_column", dot_cfi_dummy, 0 },
2547 { "cfi_restore", dot_cfi_dummy, 0 },
2548 { "cfi_undefined", dot_cfi_dummy, 0 },
2549 { "cfi_same_value", dot_cfi_dummy, 0 },
2550 { "cfi_remember_state", dot_cfi_dummy, 0 },
2551 { "cfi_restore_state", dot_cfi_dummy, 0 },
2552 { "cfi_window_save", dot_cfi_dummy, 0 },
2553 { "cfi_escape", dot_cfi_dummy, 0 },
2554 { "cfi_signal_frame", dot_cfi_dummy, 0 },
2555 { "cfi_personality", dot_cfi_dummy, 0 },
2556 { "cfi_personality_id", dot_cfi_dummy, 0 },
2557 { "cfi_lsda", dot_cfi_dummy, 0 },
2558 { "cfi_val_encoded_addr", dot_cfi_dummy, 0 },
2559 { "cfi_label", dot_cfi_dummy, 0 },
2560 { "cfi_inline_lsda", dot_cfi_dummy, 0 },
2561 { "cfi_val_offset", dot_cfi_dummy, 0 },
2569 #endif /* TARGET_USE_CFIPOP */