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>
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 unsigned int 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 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);
205 else if ((encoding & 0x70) == DW_EH_PE_pcrel)
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);
219 emit_expr (exp, size);
222 /* Build based on segment the derived .debug_...
223 segment name containing origin segment's postfix name part. */
226 get_debugseg_name (segT seg, const char *base_name)
237 name = bfd_get_section_name (stdoutput, seg);
239 dollar = strchr (name, '$');
240 dot = strchr (name + 1, '.');
244 if (!strcmp (base_name, ".eh_frame_entry")
245 && strcmp (name, ".text") != 0)
246 return concat (base_name, ".", name, NULL);
254 else if (dot < dollar)
260 return concat (base_name, name, NULL);
263 /* Allocate a dwcfi_seg_list structure. */
265 static struct dwcfi_seg_list *
266 alloc_debugseg_item (segT seg, int subseg, char *name)
268 struct dwcfi_seg_list *r;
270 r = (struct dwcfi_seg_list *)
271 xmalloc (sizeof (struct dwcfi_seg_list) + strlen (name));
279 is_now_linkonce_segment (void)
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)
292 /* Generate debug... segment with same linkonce properties
296 make_debug_seg (segT cseg, char *name, int sflags)
298 segT save_seg = now_seg;
299 int save_subseg = now_subseg;
303 r = subseg_new (name, 0);
305 /* Check if code segment is marked as linked once. */
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);
314 /* Add standard section flags. */
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 ()));
322 /* Restore to previous segment. */
323 if (save_seg != NULL)
324 subseg_set (save_seg, save_subseg);
329 dwcfi_hash_insert (const char *name, struct dwcfi_seg_list *item)
331 const char *error_string;
333 if ((error_string = hash_jam (dwcfi_hash, name, (char *) item)))
334 as_fatal (_("Inserting \"%s\" into structure table failed: %s"),
338 static struct dwcfi_seg_list *
339 dwcfi_hash_find (char *name)
341 return (struct dwcfi_seg_list *) hash_find (dwcfi_hash, name);
344 static struct dwcfi_seg_list *
345 dwcfi_hash_find_or_make (segT cseg, const char *base_name, int flags)
347 struct dwcfi_seg_list *item;
350 /* Initialize dwcfi_hash once. */
352 dwcfi_hash = hash_new ();
354 name = get_debugseg_name (cseg, base_name);
356 item = dwcfi_hash_find (name);
359 item = alloc_debugseg_item (make_debug_seg (cseg, name, flags), 0, name);
361 dwcfi_hash_insert (item->seg_name, item);
369 /* ??? Share this with dwarf2cfg.c. */
370 #ifndef TC_DWARF2_EMIT_OFFSET
371 #define TC_DWARF2_EMIT_OFFSET generic_dwarf2_emit_offset
373 /* Create an offset to .dwarf2_*. */
376 generic_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
381 exp.X_add_symbol = symbol;
382 exp.X_add_number = 0;
383 emit_expr (&exp, size);
387 struct cfi_escape_data
389 struct cfi_escape_data *next;
395 struct cie_entry *next;
396 #if MULTIPLE_FRAME_SECTIONS
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;
409 /* List of FDE entries. */
411 struct fde_entry *all_fde_data;
412 static struct fde_entry **last_fde_data = &all_fde_data;
414 /* List of CIEs so that they could be reused. */
415 static struct cie_entry *cie_root;
417 /* Stack of old CFI data, for save/restore. */
420 struct cfa_save_data *next;
424 /* Current open FDE entry. */
427 struct fde_entry *cur_fde_data;
428 symbolS *last_address;
429 offsetT cur_cfa_offset;
430 struct cfa_save_data *cfa_save_stack;
433 /* Construct a new FDE structure and add it to the end of the fde list. */
435 static struct fde_entry *
436 alloc_fde_entry (void)
438 struct fde_entry *fde = XCNEW (struct fde_entry);
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;
455 /* The following functions are available for a backend to construct its
456 own unwind information, usually from legacy unwind directives. */
458 /* Construct a new INSN structure and add it to the end of the insn list
459 for the currently active FDE. */
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;
466 static struct cfi_insn_data *
467 alloc_cfi_insn_data (void)
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;
472 *cur_fde_data->last = insn;
473 cur_fde_data->last = &insn->next;
474 SET_CUR_SEG (insn, is_now_linkonce_segment ());
478 /* Construct a new FDE structure that begins at LABEL. */
481 cfi_new_fde (symbolS *label)
483 struct fde_entry *fde = alloc_fde_entry ();
484 fde->start_address = label;
485 frchain_now->frch_cfi_data->last_address = label;
488 /* End the currently open FDE. */
491 cfi_end_fde (symbolS *label)
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;
498 /* Set the return column for the current FDE. */
501 cfi_set_return_column (unsigned regno)
503 frchain_now->frch_cfi_data->cur_fde_data->return_column = regno;
507 cfi_set_sections (void)
509 frchain_now->frch_cfi_data->cur_fde_data->sections = all_cfi_sections;
510 cfi_sections_set = TRUE;
513 /* Universal functions to store new instructions. */
516 cfi_add_CFA_insn (int insn)
518 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
520 insn_ptr->insn = insn;
524 cfi_add_CFA_insn_reg (int insn, unsigned regno)
526 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
528 insn_ptr->insn = insn;
529 insn_ptr->u.r = regno;
533 cfi_add_CFA_insn_offset (int insn, offsetT offset)
535 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
537 insn_ptr->insn = insn;
538 insn_ptr->u.i = offset;
542 cfi_add_CFA_insn_reg_reg (int insn, unsigned reg1, unsigned reg2)
544 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
546 insn_ptr->insn = insn;
547 insn_ptr->u.rr.reg1 = reg1;
548 insn_ptr->u.rr.reg2 = reg2;
552 cfi_add_CFA_insn_reg_offset (int insn, unsigned regno, offsetT offset)
554 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
556 insn_ptr->insn = insn;
557 insn_ptr->u.ri.reg = regno;
558 insn_ptr->u.ri.offset = offset;
561 /* Add a CFI insn to advance the PC from the last address to LABEL. */
564 cfi_add_advance_loc (symbolS *label)
566 struct cfi_insn_data *insn = alloc_cfi_insn_data ();
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;
572 frchain_now->frch_cfi_data->last_address = label;
575 /* Add a CFI insn to label the current position in the CFI segment. */
578 cfi_add_label (const char *name)
580 unsigned int len = strlen (name) + 1;
581 struct cfi_insn_data *insn = alloc_cfi_insn_data ();
583 insn->insn = CFI_label;
584 obstack_grow (¬es, name, len);
585 insn->u.sym_name = (char *) obstack_finish (¬es);
588 /* Add a DW_CFA_offset record to the CFI data. */
591 cfi_add_CFA_offset (unsigned regno, offsetT offset)
593 unsigned int abs_data_align;
595 gas_assert (DWARF2_CIE_DATA_ALIGNMENT != 0);
596 cfi_add_CFA_insn_reg_offset (DW_CFA_offset, regno, offset);
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);
604 /* Add a DW_CFA_val_offset record to the CFI data. */
607 cfi_add_CFA_val_offset (unsigned regno, offsetT offset)
609 unsigned int abs_data_align;
611 gas_assert (DWARF2_CIE_DATA_ALIGNMENT != 0);
612 cfi_add_CFA_insn_reg_offset (DW_CFA_val_offset, regno, offset);
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);
620 /* Add a DW_CFA_def_cfa record to the CFI data. */
623 cfi_add_CFA_def_cfa (unsigned regno, offsetT offset)
625 cfi_add_CFA_insn_reg_offset (DW_CFA_def_cfa, regno, offset);
626 frchain_now->frch_cfi_data->cur_cfa_offset = offset;
629 /* Add a DW_CFA_register record to the CFI data. */
632 cfi_add_CFA_register (unsigned reg1, unsigned reg2)
634 cfi_add_CFA_insn_reg_reg (DW_CFA_register, reg1, reg2);
637 /* Add a DW_CFA_def_cfa_register record to the CFI data. */
640 cfi_add_CFA_def_cfa_register (unsigned regno)
642 cfi_add_CFA_insn_reg (DW_CFA_def_cfa_register, regno);
645 /* Add a DW_CFA_def_cfa_offset record to the CFI data. */
648 cfi_add_CFA_def_cfa_offset (offsetT offset)
650 cfi_add_CFA_insn_offset (DW_CFA_def_cfa_offset, offset);
651 frchain_now->frch_cfi_data->cur_cfa_offset = offset;
655 cfi_add_CFA_restore (unsigned regno)
657 cfi_add_CFA_insn_reg (DW_CFA_restore, regno);
661 cfi_add_CFA_undefined (unsigned regno)
663 cfi_add_CFA_insn_reg (DW_CFA_undefined, regno);
667 cfi_add_CFA_same_value (unsigned regno)
669 cfi_add_CFA_insn_reg (DW_CFA_same_value, regno);
673 cfi_add_CFA_remember_state (void)
675 struct cfa_save_data *p;
677 cfi_add_CFA_insn (DW_CFA_remember_state);
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;
686 cfi_add_CFA_restore_state (void)
688 struct cfa_save_data *p;
690 cfi_add_CFA_insn (DW_CFA_restore_state);
692 p = frchain_now->frch_cfi_data->cfa_save_stack;
695 frchain_now->frch_cfi_data->cur_cfa_offset = p->cfa_offset;
696 frchain_now->frch_cfi_data->cfa_save_stack = p->next;
700 as_bad (_("CFI state restore without previous remember"));
704 /* Parse CFI assembler directives. */
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);
719 const pseudo_typeS cfi_pseudo_table[] =
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 },
752 cfi_parse_separator (void)
755 if (*input_line_pointer == ',')
756 input_line_pointer++;
758 as_bad (_("missing separator"));
761 #ifndef tc_parse_to_dw2regnum
763 tc_parse_to_dw2regnum (expressionS *exp)
765 # ifdef tc_regname_to_dw2regnum
767 if (is_name_beginner (*input_line_pointer)
768 || (*input_line_pointer == '%'
769 && is_name_beginner (*++input_line_pointer)))
773 c = get_symbol_name (& name);
775 exp->X_op = O_constant;
776 exp->X_add_number = tc_regname_to_dw2regnum (name);
778 restore_line_pointer (c);
782 expression_and_evaluate (exp);
792 tc_parse_to_dw2regnum (&exp);
797 regno = exp.X_add_number;
807 as_bad (_("bad register expression"));
815 cfi_parse_const (void)
817 return get_absolute_expression ();
826 if (frchain_now->frch_cfi_data == NULL)
828 as_bad (_("CFI instruction used without previous .cfi_startproc"));
829 ignore_rest_of_line ();
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)
837 cfi_add_advance_loc (symbol_temp_new_now ());
842 reg1 = cfi_parse_reg ();
843 cfi_parse_separator ();
844 offset = cfi_parse_const ();
845 cfi_add_CFA_offset (reg1, offset);
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);
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);
864 reg1 = cfi_parse_reg ();
865 cfi_parse_separator ();
866 offset = cfi_parse_const ();
867 cfi_add_CFA_def_cfa (reg1, offset);
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);
877 case DW_CFA_def_cfa_register:
878 reg1 = cfi_parse_reg ();
879 cfi_add_CFA_def_cfa_register (reg1);
882 case DW_CFA_def_cfa_offset:
883 offset = cfi_parse_const ();
884 cfi_add_CFA_def_cfa_offset (offset);
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
896 reg1 = cfi_parse_reg ();
897 cfi_add_CFA_restore (reg1);
899 if (*input_line_pointer != ',')
901 ++input_line_pointer;
905 case DW_CFA_undefined:
908 reg1 = cfi_parse_reg ();
909 cfi_add_CFA_undefined (reg1);
911 if (*input_line_pointer != ',')
913 ++input_line_pointer;
917 case DW_CFA_same_value:
918 reg1 = cfi_parse_reg ();
919 cfi_add_CFA_same_value (reg1);
922 case CFI_return_column:
923 reg1 = cfi_parse_reg ();
924 cfi_set_return_column (reg1);
927 case DW_CFA_remember_state:
928 cfi_add_CFA_remember_state ();
931 case DW_CFA_restore_state:
932 cfi_add_CFA_restore_state ();
935 case DW_CFA_GNU_window_save:
936 cfi_add_CFA_insn (DW_CFA_GNU_window_save);
939 case CFI_signal_frame:
940 frchain_now->frch_cfi_data->cur_fde_data->signal_frame = 1;
947 demand_empty_rest_of_line ();
951 dot_cfi_escape (int ignored ATTRIBUTE_UNUSED)
953 struct cfi_escape_data *head, **tail, *e;
954 struct cfi_insn_data *insn;
956 if (frchain_now->frch_cfi_data == NULL)
958 as_bad (_("CFI instruction used without previous .cfi_startproc"));
959 ignore_rest_of_line ();
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)
967 cfi_add_advance_loc (symbol_temp_new_now ());
972 e = XNEW (struct cfi_escape_data);
973 do_parse_cons_expression (&e->exp, 1);
977 while (*input_line_pointer++ == ',');
980 insn = alloc_cfi_insn_data ();
981 insn->insn = CFI_escape;
984 --input_line_pointer;
985 demand_empty_rest_of_line ();
989 dot_cfi_personality (int ignored ATTRIBUTE_UNUSED)
991 struct fde_entry *fde;
994 if (frchain_now->frch_cfi_data == NULL)
996 as_bad (_("CFI instruction used without previous .cfi_startproc"));
997 ignore_rest_of_line ();
1001 fde = frchain_now->frch_cfi_data->cur_fde_data;
1002 encoding = cfi_parse_const ();
1003 if (encoding == DW_EH_PE_omit)
1005 demand_empty_rest_of_line ();
1006 fde->per_encoding = encoding;
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
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))
1021 as_bad (_("invalid or unsupported encoding in .cfi_personality"));
1022 ignore_rest_of_line ();
1026 if (*input_line_pointer++ != ',')
1028 as_bad (_(".cfi_personality requires encoding and symbol arguments"));
1029 ignore_rest_of_line ();
1033 expression_and_evaluate (&fde->personality);
1034 switch (fde->personality.X_op)
1039 if ((encoding & 0x70) == DW_EH_PE_pcrel)
1040 encoding = DW_EH_PE_omit;
1043 encoding = DW_EH_PE_omit;
1047 fde->per_encoding = encoding;
1049 if (encoding == DW_EH_PE_omit)
1051 as_bad (_("wrong second argument to .cfi_personality"));
1052 ignore_rest_of_line ();
1056 demand_empty_rest_of_line ();
1060 dot_cfi_lsda (int ignored ATTRIBUTE_UNUSED)
1062 struct fde_entry *fde;
1065 if (frchain_now->frch_cfi_data == NULL)
1067 as_bad (_("CFI instruction used without previous .cfi_startproc"));
1068 ignore_rest_of_line ();
1072 fde = frchain_now->frch_cfi_data->cur_fde_data;
1073 encoding = cfi_parse_const ();
1074 if (encoding == DW_EH_PE_omit)
1076 demand_empty_rest_of_line ();
1077 fde->lsda_encoding = encoding;
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
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))
1092 as_bad (_("invalid or unsupported encoding in .cfi_lsda"));
1093 ignore_rest_of_line ();
1097 if (*input_line_pointer++ != ',')
1099 as_bad (_(".cfi_lsda requires encoding and symbol arguments"));
1100 ignore_rest_of_line ();
1104 fde->lsda_encoding = encoding;
1106 expression_and_evaluate (&fde->lsda);
1107 switch (fde->lsda.X_op)
1112 if ((encoding & 0x70) == DW_EH_PE_pcrel)
1113 encoding = DW_EH_PE_omit;
1116 encoding = DW_EH_PE_omit;
1120 fde->lsda_encoding = encoding;
1122 if (encoding == DW_EH_PE_omit)
1124 as_bad (_("wrong second argument to .cfi_lsda"));
1125 ignore_rest_of_line ();
1129 demand_empty_rest_of_line ();
1133 dot_cfi_val_encoded_addr (int ignored ATTRIBUTE_UNUSED)
1135 struct cfi_insn_data *insn_ptr;
1138 if (frchain_now->frch_cfi_data == NULL)
1140 as_bad (_("CFI instruction used without previous .cfi_startproc"));
1141 ignore_rest_of_line ();
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 ());
1151 insn_ptr = alloc_cfi_insn_data ();
1152 insn_ptr->insn = CFI_val_encoded_addr;
1154 insn_ptr->u.ea.reg = cfi_parse_reg ();
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
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)
1168 as_bad (_("invalid or unsupported encoding in .cfi_lsda"));
1169 encoding = DW_EH_PE_omit;
1172 cfi_parse_separator ();
1173 expression_and_evaluate (&insn_ptr->u.ea.exp);
1174 switch (insn_ptr->u.ea.exp.X_op)
1179 if ((encoding & 0x70) != DW_EH_PE_pcrel)
1183 encoding = DW_EH_PE_omit;
1187 insn_ptr->u.ea.encoding = encoding;
1188 if (encoding == DW_EH_PE_omit)
1190 as_bad (_("wrong third argument to .cfi_val_encoded_addr"));
1191 ignore_rest_of_line ();
1195 demand_empty_rest_of_line ();
1199 dot_cfi_label (int ignored ATTRIBUTE_UNUSED)
1201 char *name = read_symbol_name ();
1206 /* If the last address was not at the current PC, advance to current. */
1207 if (symbol_get_frag (frchain_now->frch_cfi_data->last_address) != frag_now
1208 || (S_GET_VALUE (frchain_now->frch_cfi_data->last_address)
1209 != frag_now_fix ()))
1210 cfi_add_advance_loc (symbol_temp_new_now ());
1212 cfi_add_label (name);
1215 demand_empty_rest_of_line ();
1219 dot_cfi_sections (int ignored ATTRIBUTE_UNUSED)
1224 if (is_name_beginner (*input_line_pointer) || *input_line_pointer == '"')
1230 saved_ilp = input_line_pointer;
1231 c = get_symbol_name (& name);
1233 if (strncmp (name, ".eh_frame", sizeof ".eh_frame") == 0
1235 sections |= CFI_EMIT_eh_frame;
1236 else if (strncmp (name, ".debug_frame", sizeof ".debug_frame") == 0)
1237 sections |= CFI_EMIT_debug_frame;
1238 #if SUPPORT_COMPACT_EH
1239 else if (strncmp (name, ".eh_frame_entry",
1240 sizeof ".eh_frame_entry") == 0)
1243 sections |= CFI_EMIT_eh_frame_compact;
1246 #ifdef tc_cfi_section_name
1247 else if (strcmp (name, tc_cfi_section_name) == 0)
1248 sections |= CFI_EMIT_target;
1252 *input_line_pointer = c;
1253 input_line_pointer = saved_ilp;
1257 *input_line_pointer = c;
1258 SKIP_WHITESPACE_AFTER_NAME ();
1259 if (*input_line_pointer == ',')
1261 name = input_line_pointer++;
1263 if (!is_name_beginner (*input_line_pointer)
1264 && *input_line_pointer != '"')
1266 input_line_pointer = name;
1270 else if (is_name_beginner (*input_line_pointer)
1271 || *input_line_pointer == '"')
1275 demand_empty_rest_of_line ();
1276 if (cfi_sections_set
1277 && (sections & (CFI_EMIT_eh_frame | CFI_EMIT_eh_frame_compact))
1278 && ((cfi_sections & (CFI_EMIT_eh_frame | CFI_EMIT_eh_frame_compact))
1279 != (sections & (CFI_EMIT_eh_frame | CFI_EMIT_eh_frame_compact))))
1280 as_bad (_("inconsistent uses of .cfi_sections"));
1281 cfi_sections = sections;
1285 dot_cfi_startproc (int ignored ATTRIBUTE_UNUSED)
1289 if (frchain_now->frch_cfi_data != NULL)
1291 as_bad (_("previous CFI entry not closed (missing .cfi_endproc)"));
1292 ignore_rest_of_line ();
1296 cfi_new_fde (symbol_temp_new_now ());
1299 if (is_name_beginner (*input_line_pointer) || *input_line_pointer == '"')
1301 char * saved_ilp = input_line_pointer;
1304 c = get_symbol_name (& name);
1306 if (strcmp (name, "simple") == 0)
1309 restore_line_pointer (c);
1312 input_line_pointer = saved_ilp;
1314 demand_empty_rest_of_line ();
1316 cfi_sections_set = TRUE;
1317 all_cfi_sections |= cfi_sections;
1318 cfi_set_sections ();
1319 frchain_now->frch_cfi_data->cur_cfa_offset = 0;
1321 tc_cfi_frame_initial_instructions ();
1323 if ((cfi_sections & CFI_EMIT_target) != 0)
1324 tc_cfi_startproc ();
1328 dot_cfi_endproc (int ignored ATTRIBUTE_UNUSED)
1330 if (frchain_now->frch_cfi_data == NULL)
1332 as_bad (_(".cfi_endproc without corresponding .cfi_startproc"));
1333 ignore_rest_of_line ();
1337 last_fde = frchain_now->frch_cfi_data->cur_fde_data;
1339 cfi_end_fde (symbol_temp_new_now ());
1341 demand_empty_rest_of_line ();
1343 cfi_sections_set = TRUE;
1344 if ((cfi_sections & CFI_EMIT_target) != 0)
1345 tc_cfi_endproc (last_fde);
1349 get_cfi_seg (segT cseg, const char *base, flagword flags, int align)
1351 /* Exclude .debug_frame sections for Compact EH. */
1352 if (SUPPORT_FRAME_LINKONCE || ((flags & SEC_DEBUGGING) == 0 && compact_eh))
1354 struct dwcfi_seg_list *l;
1356 l = dwcfi_hash_find_or_make (cseg, base, flags);
1359 subseg_set (cseg, l->subseg);
1363 cseg = subseg_new (base, 0);
1364 bfd_set_section_flags (stdoutput, cseg, flags);
1366 record_alignment (cseg, align);
1370 #if SUPPORT_COMPACT_EH
1372 dot_cfi_personality_id (int ignored ATTRIBUTE_UNUSED)
1374 struct fde_entry *fde;
1376 if (frchain_now->frch_cfi_data == NULL)
1378 as_bad (_("CFI instruction used without previous .cfi_startproc"));
1379 ignore_rest_of_line ();
1383 fde = frchain_now->frch_cfi_data->cur_fde_data;
1384 fde->personality_id = cfi_parse_const ();
1385 demand_empty_rest_of_line ();
1387 if (fde->personality_id == 0 || fde->personality_id > 3)
1389 as_bad (_("wrong argument to .cfi_personality_id"));
1395 dot_cfi_fde_data (int ignored ATTRIBUTE_UNUSED)
1397 if (frchain_now->frch_cfi_data == NULL)
1399 as_bad (_(".cfi_fde_data without corresponding .cfi_startproc"));
1400 ignore_rest_of_line ();
1404 last_fde = frchain_now->frch_cfi_data->cur_fde_data;
1406 cfi_sections_set = TRUE;
1407 if ((cfi_sections & CFI_EMIT_target) != 0
1408 || (cfi_sections & CFI_EMIT_eh_frame_compact) != 0)
1410 struct cfi_escape_data *head, **tail, *e;
1414 if (!is_it_end_of_statement ())
1419 e = XNEW (struct cfi_escape_data);
1420 do_parse_cons_expression (&e->exp, 1);
1425 while (*input_line_pointer++ == ',');
1426 --input_line_pointer;
1430 if (last_fde->lsda_encoding != DW_EH_PE_omit)
1431 last_fde->eh_header_type = EH_COMPACT_HAS_LSDA;
1432 else if (num_ops <= 3 && last_fde->per_encoding == DW_EH_PE_omit)
1433 last_fde->eh_header_type = EH_COMPACT_INLINE;
1435 last_fde->eh_header_type = EH_COMPACT_OUTLINE;
1437 if (last_fde->eh_header_type == EH_COMPACT_INLINE)
1440 last_fde->eh_data_size = num_ops;
1441 last_fde->eh_data = XNEWVEC (bfd_byte, num_ops);
1447 last_fde->eh_data[num_ops++] = e->exp.X_add_number;
1450 if (last_fde->eh_header_type == EH_COMPACT_INLINE)
1452 last_fde->eh_data[num_ops++] = tc_compact_eh_opcode_stop;
1455 demand_empty_rest_of_line ();
1458 /* Function to emit the compact unwinding opcodes stored in the
1459 fde's eh_data field. The end of the opcode data will be
1460 padded to the value in align. */
1463 output_compact_unwind_data (struct fde_entry *fde, int align)
1465 int data_size = fde->eh_data_size + 2;
1470 fde->eh_loc = symbol_temp_new_now ();
1473 if (fde->personality_id != 0)
1474 *p = fde->personality_id;
1475 else if (fde->per_encoding != DW_EH_PE_omit)
1478 emit_expr_encoded (&fde->personality, fde->per_encoding, FALSE);
1479 data_size += encoding_size (fde->per_encoding);
1484 amask = (1 << align) - 1;
1485 align_padding = ((data_size + amask) & ~amask) - data_size;
1487 p = frag_more (fde->eh_data_size + 1 + align_padding);
1488 memcpy (p, fde->eh_data, fde->eh_data_size);
1489 p += fde->eh_data_size;
1491 while (align_padding-- > 0)
1492 *(p++) = tc_compact_eh_opcode_pad;
1494 *(p++) = tc_compact_eh_opcode_stop;
1495 fde->eh_header_type = EH_COMPACT_OUTLINE_DONE;
1498 /* Handle the .cfi_inline_lsda directive. */
1500 dot_cfi_inline_lsda (int ignored ATTRIBUTE_UNUSED)
1504 long max_alignment = 28;
1508 as_bad (_("unexpected .cfi_inline_lsda"));
1509 ignore_rest_of_line ();
1513 if ((last_fde->sections & CFI_EMIT_eh_frame_compact) == 0)
1515 as_bad (_(".cfi_inline_lsda not valid for this frame"));
1516 ignore_rest_of_line ();
1520 if (last_fde->eh_header_type != EH_COMPACT_UNKNOWN
1521 && last_fde->eh_header_type != EH_COMPACT_HAS_LSDA)
1523 as_bad (_(".cfi_inline_lsda seen for frame without .cfi_lsda"));
1524 ignore_rest_of_line ();
1528 #ifdef md_flush_pending_output
1529 md_flush_pending_output ();
1532 align = get_absolute_expression ();
1533 if (align > max_alignment)
1535 align = max_alignment;
1536 as_bad (_("Alignment too large: %d. assumed."), align);
1540 as_warn (_("Alignment negative: 0 assumed."));
1544 demand_empty_rest_of_line ();
1545 ccseg = CUR_SEG (last_fde);
1547 /* Open .gnu_extab section. */
1548 get_cfi_seg (ccseg, ".gnu_extab",
1549 (SEC_ALLOC | SEC_LOAD | SEC_DATA
1550 | DWARF2_EH_FRAME_READ_ONLY),
1553 frag_align (align, 0, 0);
1554 record_alignment (now_seg, align);
1555 if (last_fde->eh_header_type == EH_COMPACT_HAS_LSDA)
1556 output_compact_unwind_data (last_fde, align);
1562 #else /* !SUPPORT_COMPACT_EH */
1564 dot_cfi_inline_lsda (int ignored ATTRIBUTE_UNUSED)
1566 as_bad (_(".cfi_inline_lsda is not supported for this target"));
1567 ignore_rest_of_line ();
1571 dot_cfi_fde_data (int ignored ATTRIBUTE_UNUSED)
1573 as_bad (_(".cfi_fde_data is not supported for this target"));
1574 ignore_rest_of_line ();
1578 dot_cfi_personality_id (int ignored ATTRIBUTE_UNUSED)
1580 as_bad (_(".cfi_personality_id is not supported for this target"));
1581 ignore_rest_of_line ();
1586 output_cfi_insn (struct cfi_insn_data *insn)
1593 case DW_CFA_advance_loc:
1595 symbolS *from = insn->u.ll.lab1;
1596 symbolS *to = insn->u.ll.lab2;
1598 if (symbol_get_frag (to) == symbol_get_frag (from))
1600 addressT delta = S_GET_VALUE (to) - S_GET_VALUE (from);
1601 addressT scaled = delta / DWARF2_LINE_MIN_INSN_LENGTH;
1604 out_one (DW_CFA_advance_loc + scaled);
1605 else if (scaled <= 0xFF)
1607 out_one (DW_CFA_advance_loc1);
1610 else if (scaled <= 0xFFFF)
1612 out_one (DW_CFA_advance_loc2);
1617 out_one (DW_CFA_advance_loc4);
1625 exp.X_op = O_subtract;
1626 exp.X_add_symbol = to;
1627 exp.X_op_symbol = from;
1628 exp.X_add_number = 0;
1630 /* The code in ehopt.c expects that one byte of the encoding
1631 is already allocated to the frag. This comes from the way
1632 that it scans the .eh_frame section looking first for the
1633 .byte DW_CFA_advance_loc4. */
1634 *frag_more (1) = DW_CFA_advance_loc4;
1636 frag_var (rs_cfa, 4, 0, DWARF2_LINE_MIN_INSN_LENGTH << 3,
1637 make_expr_symbol (&exp), frag_now_fix () - 1,
1643 case DW_CFA_def_cfa:
1644 offset = insn->u.ri.offset;
1647 out_one (DW_CFA_def_cfa_sf);
1648 out_uleb128 (insn->u.ri.reg);
1649 out_sleb128 (offset / DWARF2_CIE_DATA_ALIGNMENT);
1653 out_one (DW_CFA_def_cfa);
1654 out_uleb128 (insn->u.ri.reg);
1655 out_uleb128 (offset);
1659 case DW_CFA_def_cfa_register:
1660 case DW_CFA_undefined:
1661 case DW_CFA_same_value:
1662 out_one (insn->insn);
1663 out_uleb128 (insn->u.r);
1666 case DW_CFA_def_cfa_offset:
1670 out_one (DW_CFA_def_cfa_offset_sf);
1671 out_sleb128 (offset / DWARF2_CIE_DATA_ALIGNMENT);
1675 out_one (DW_CFA_def_cfa_offset);
1676 out_uleb128 (offset);
1680 case DW_CFA_restore:
1684 out_one (DW_CFA_restore + regno);
1688 out_one (DW_CFA_restore_extended);
1689 out_uleb128 (regno);
1694 regno = insn->u.ri.reg;
1695 offset = insn->u.ri.offset / DWARF2_CIE_DATA_ALIGNMENT;
1698 out_one (DW_CFA_offset_extended_sf);
1699 out_uleb128 (regno);
1700 out_sleb128 (offset);
1702 else if (regno <= 0x3F)
1704 out_one (DW_CFA_offset + regno);
1705 out_uleb128 (offset);
1709 out_one (DW_CFA_offset_extended);
1710 out_uleb128 (regno);
1711 out_uleb128 (offset);
1715 case DW_CFA_val_offset:
1716 regno = insn->u.ri.reg;
1717 offset = insn->u.ri.offset / DWARF2_CIE_DATA_ALIGNMENT;
1720 out_one (DW_CFA_val_offset_sf);
1721 out_uleb128 (regno);
1722 out_sleb128 (offset);
1726 out_one (DW_CFA_val_offset);
1727 out_uleb128 (regno);
1728 out_uleb128 (offset);
1732 case DW_CFA_register:
1733 out_one (DW_CFA_register);
1734 out_uleb128 (insn->u.rr.reg1);
1735 out_uleb128 (insn->u.rr.reg2);
1738 case DW_CFA_remember_state:
1739 case DW_CFA_restore_state:
1740 out_one (insn->insn);
1743 case DW_CFA_GNU_window_save:
1744 out_one (DW_CFA_GNU_window_save);
1749 struct cfi_escape_data *e;
1750 for (e = insn->u.esc; e ; e = e->next)
1751 emit_expr (&e->exp, 1);
1755 case CFI_val_encoded_addr:
1757 unsigned encoding = insn->u.ea.encoding;
1760 if (encoding == DW_EH_PE_omit)
1762 out_one (DW_CFA_val_expression);
1763 out_uleb128 (insn->u.ea.reg);
1765 switch (encoding & 0x7)
1767 case DW_EH_PE_absptr:
1768 enc_size = DWARF2_ADDR_SIZE (stdoutput);
1770 case DW_EH_PE_udata2:
1773 case DW_EH_PE_udata4:
1776 case DW_EH_PE_udata8:
1783 /* If the user has requested absolute encoding,
1784 then use the smaller DW_OP_addr encoding. */
1785 if (insn->u.ea.encoding == DW_EH_PE_absptr)
1787 out_uleb128 (1 + enc_size);
1788 out_one (DW_OP_addr);
1792 out_uleb128 (1 + 1 + enc_size);
1793 out_one (DW_OP_GNU_encoded_addr);
1796 if ((encoding & 0x70) == DW_EH_PE_pcrel)
1798 #if CFI_DIFF_EXPR_OK
1799 insn->u.ea.exp.X_op = O_subtract;
1800 insn->u.ea.exp.X_op_symbol = symbol_temp_new_now ();
1801 #elif defined (tc_cfi_emit_pcrel_expr)
1802 tc_cfi_emit_pcrel_expr (&insn->u.ea.exp, enc_size);
1809 emit_expr (&insn->u.ea.exp, enc_size);
1814 colon (insn->u.sym_name);
1823 output_cie (struct cie_entry *cie, bfd_boolean eh_frame, int align)
1825 symbolS *after_size_address, *end_address;
1827 struct cfi_insn_data *i;
1828 offsetT augmentation_size;
1830 enum dwarf2_format fmt = DWARF2_FORMAT (now_seg);
1832 cie->start_address = symbol_temp_new_now ();
1833 after_size_address = symbol_temp_make ();
1834 end_address = symbol_temp_make ();
1836 exp.X_op = O_subtract;
1837 exp.X_add_symbol = end_address;
1838 exp.X_op_symbol = after_size_address;
1839 exp.X_add_number = 0;
1841 if (eh_frame || fmt == dwarf2_format_32bit)
1842 emit_expr (&exp, 4); /* Length. */
1845 if (fmt == dwarf2_format_64bit)
1847 emit_expr (&exp, 8); /* Length. */
1849 symbol_set_value_now (after_size_address);
1851 out_four (0); /* CIE id. */
1854 out_four (-1); /* CIE id. */
1855 if (fmt != dwarf2_format_32bit)
1858 out_one (DW_CIE_VERSION); /* Version. */
1861 out_one ('z'); /* Augmentation. */
1862 if (cie->per_encoding != DW_EH_PE_omit)
1864 if (cie->lsda_encoding != DW_EH_PE_omit)
1868 if (cie->signal_frame)
1871 out_uleb128 (DWARF2_LINE_MIN_INSN_LENGTH); /* Code alignment. */
1872 out_sleb128 (DWARF2_CIE_DATA_ALIGNMENT); /* Data alignment. */
1873 if (DW_CIE_VERSION == 1) /* Return column. */
1874 out_one (cie->return_column);
1876 out_uleb128 (cie->return_column);
1879 augmentation_size = 1 + (cie->lsda_encoding != DW_EH_PE_omit);
1880 if (cie->per_encoding != DW_EH_PE_omit)
1881 augmentation_size += 1 + encoding_size (cie->per_encoding);
1882 out_uleb128 (augmentation_size); /* Augmentation size. */
1884 emit_expr_encoded (&cie->personality, cie->per_encoding, TRUE);
1886 if (cie->lsda_encoding != DW_EH_PE_omit)
1887 out_one (cie->lsda_encoding);
1890 switch (DWARF2_FDE_RELOC_SIZE)
1893 enc = DW_EH_PE_sdata2;
1896 enc = DW_EH_PE_sdata4;
1899 enc = DW_EH_PE_sdata8;
1904 #if CFI_DIFF_EXPR_OK || defined tc_cfi_emit_pcrel_expr
1905 enc |= DW_EH_PE_pcrel;
1907 #ifdef DWARF2_FDE_RELOC_ENCODING
1908 /* Allow target to override encoding. */
1909 enc = DWARF2_FDE_RELOC_ENCODING (enc);
1911 cie->fde_encoding = enc;
1917 for (i = cie->first; i != cie->last; i = i->next)
1919 if (CUR_SEG (i) != CUR_SEG (cie))
1921 output_cfi_insn (i);
1925 frag_align (align, DW_CFA_nop, 0);
1926 symbol_set_value_now (end_address);
1930 output_fde (struct fde_entry *fde, struct cie_entry *cie,
1931 bfd_boolean eh_frame, struct cfi_insn_data *first,
1934 symbolS *after_size_address, *end_address;
1936 offsetT augmentation_size;
1937 enum dwarf2_format fmt = DWARF2_FORMAT (now_seg);
1938 unsigned int offset_size;
1939 unsigned int addr_size;
1941 after_size_address = symbol_temp_make ();
1942 end_address = symbol_temp_make ();
1944 exp.X_op = O_subtract;
1945 exp.X_add_symbol = end_address;
1946 exp.X_op_symbol = after_size_address;
1947 exp.X_add_number = 0;
1948 if (eh_frame || fmt == dwarf2_format_32bit)
1952 if (fmt == dwarf2_format_64bit)
1956 emit_expr (&exp, offset_size); /* Length. */
1957 symbol_set_value_now (after_size_address);
1961 exp.X_op = O_subtract;
1962 exp.X_add_symbol = after_size_address;
1963 exp.X_op_symbol = cie->start_address;
1964 exp.X_add_number = 0;
1965 emit_expr (&exp, offset_size); /* CIE offset. */
1969 TC_DWARF2_EMIT_OFFSET (cie->start_address, offset_size);
1972 exp.X_op = O_symbol;
1975 bfd_reloc_code_real_type code
1976 = tc_cfi_reloc_for_encoding (cie->fde_encoding);
1977 addr_size = DWARF2_FDE_RELOC_SIZE;
1978 if (code != BFD_RELOC_NONE)
1980 reloc_howto_type *howto = bfd_reloc_type_lookup (stdoutput, code);
1981 char *p = frag_more (addr_size);
1982 gas_assert (addr_size == (unsigned) howto->bitsize / 8);
1983 md_number_to_chars (p, 0, addr_size);
1984 fix_new (frag_now, p - frag_now->fr_literal, addr_size,
1985 fde->start_address, 0, howto->pc_relative, code);
1989 exp.X_op = O_subtract;
1990 exp.X_add_number = 0;
1991 #if CFI_DIFF_EXPR_OK
1992 exp.X_add_symbol = fde->start_address;
1993 exp.X_op_symbol = symbol_temp_new_now ();
1994 emit_expr (&exp, addr_size); /* Code offset. */
1996 exp.X_op = O_symbol;
1997 exp.X_add_symbol = fde->start_address;
1999 #if defined(tc_cfi_emit_pcrel_expr)
2000 tc_cfi_emit_pcrel_expr (&exp, addr_size); /* Code offset. */
2002 emit_expr (&exp, addr_size); /* Code offset. */
2009 exp.X_add_number = 0;
2010 exp.X_add_symbol = fde->start_address;
2011 addr_size = DWARF2_ADDR_SIZE (stdoutput);
2012 emit_expr (&exp, addr_size);
2015 exp.X_op = O_subtract;
2016 exp.X_add_symbol = fde->end_address;
2017 exp.X_op_symbol = fde->start_address; /* Code length. */
2018 exp.X_add_number = 0;
2019 emit_expr (&exp, addr_size);
2021 augmentation_size = encoding_size (fde->lsda_encoding);
2023 out_uleb128 (augmentation_size); /* Augmentation size. */
2025 emit_expr_encoded (&fde->lsda, cie->lsda_encoding, FALSE);
2027 for (; first; first = first->next)
2028 if (CUR_SEG (first) == CUR_SEG (fde))
2029 output_cfi_insn (first);
2031 frag_align (align, DW_CFA_nop, 0);
2032 symbol_set_value_now (end_address);
2035 static struct cie_entry *
2036 select_cie_for_fde (struct fde_entry *fde, bfd_boolean eh_frame,
2037 struct cfi_insn_data **pfirst, int align)
2039 struct cfi_insn_data *i, *j;
2040 struct cie_entry *cie;
2042 for (cie = cie_root; cie; cie = cie->next)
2044 if (CUR_SEG (cie) != CUR_SEG (fde))
2046 if (cie->return_column != fde->return_column
2047 || cie->signal_frame != fde->signal_frame
2048 || cie->per_encoding != fde->per_encoding
2049 || cie->lsda_encoding != fde->lsda_encoding)
2051 if (cie->per_encoding != DW_EH_PE_omit)
2053 if (cie->personality.X_op != fde->personality.X_op
2054 || (cie->personality.X_add_number
2055 != fde->personality.X_add_number))
2057 switch (cie->personality.X_op)
2060 if (cie->personality.X_unsigned != fde->personality.X_unsigned)
2064 if (cie->personality.X_add_symbol
2065 != fde->personality.X_add_symbol)
2072 for (i = cie->first, j = fde->data;
2073 i != cie->last && j != NULL;
2074 i = i->next, j = j->next)
2076 if (i->insn != j->insn)
2080 case DW_CFA_advance_loc:
2081 case DW_CFA_remember_state:
2082 /* We reached the first advance/remember in the FDE,
2083 but did not reach the end of the CIE list. */
2087 case DW_CFA_def_cfa:
2088 if (i->u.ri.reg != j->u.ri.reg)
2090 if (i->u.ri.offset != j->u.ri.offset)
2094 case DW_CFA_register:
2095 if (i->u.rr.reg1 != j->u.rr.reg1)
2097 if (i->u.rr.reg2 != j->u.rr.reg2)
2101 case DW_CFA_def_cfa_register:
2102 case DW_CFA_restore:
2103 case DW_CFA_undefined:
2104 case DW_CFA_same_value:
2105 if (i->u.r != j->u.r)
2109 case DW_CFA_def_cfa_offset:
2110 if (i->u.i != j->u.i)
2115 case CFI_val_encoded_addr:
2117 /* Don't bother matching these for now. */
2125 /* Success if we reached the end of the CIE list, and we've either
2126 run out of FDE entries or we've encountered an advance,
2127 remember, or escape. */
2130 || j->insn == DW_CFA_advance_loc
2131 || j->insn == DW_CFA_remember_state
2132 || j->insn == CFI_escape
2133 || j->insn == CFI_val_encoded_addr
2134 || j->insn == CFI_label))
2143 cie = XNEW (struct cie_entry);
2144 cie->next = cie_root;
2146 SET_CUR_SEG (cie, CUR_SEG (fde));
2147 cie->return_column = fde->return_column;
2148 cie->signal_frame = fde->signal_frame;
2149 cie->per_encoding = fde->per_encoding;
2150 cie->lsda_encoding = fde->lsda_encoding;
2151 cie->personality = fde->personality;
2152 cie->first = fde->data;
2154 for (i = cie->first; i ; i = i->next)
2155 if (i->insn == DW_CFA_advance_loc
2156 || i->insn == DW_CFA_remember_state
2157 || i->insn == CFI_escape
2158 || i->insn == CFI_val_encoded_addr
2159 || i->insn == CFI_label)
2165 output_cie (cie, eh_frame, align);
2170 #ifdef md_reg_eh_frame_to_debug_frame
2172 cfi_change_reg_numbers (struct cfi_insn_data *insn, segT ccseg)
2174 for (; insn; insn = insn->next)
2176 if (CUR_SEG (insn) != ccseg)
2180 case DW_CFA_advance_loc:
2181 case DW_CFA_def_cfa_offset:
2182 case DW_CFA_remember_state:
2183 case DW_CFA_restore_state:
2184 case DW_CFA_GNU_window_save:
2189 case DW_CFA_def_cfa:
2191 insn->u.ri.reg = md_reg_eh_frame_to_debug_frame (insn->u.ri.reg);
2194 case DW_CFA_def_cfa_register:
2195 case DW_CFA_undefined:
2196 case DW_CFA_same_value:
2197 case DW_CFA_restore:
2198 insn->u.r = md_reg_eh_frame_to_debug_frame (insn->u.r);
2201 case DW_CFA_register:
2202 insn->u.rr.reg1 = md_reg_eh_frame_to_debug_frame (insn->u.rr.reg1);
2203 insn->u.rr.reg2 = md_reg_eh_frame_to_debug_frame (insn->u.rr.reg2);
2206 case CFI_val_encoded_addr:
2207 insn->u.ea.reg = md_reg_eh_frame_to_debug_frame (insn->u.ea.reg);
2216 #define cfi_change_reg_numbers(insn, cseg) do { } while (0)
2219 #if SUPPORT_COMPACT_EH
2221 cfi_emit_eh_header (symbolS *sym, bfd_vma addend)
2225 exp.X_add_number = addend;
2226 exp.X_add_symbol = sym;
2227 emit_expr_encoded (&exp, DW_EH_PE_sdata4 | DW_EH_PE_pcrel, FALSE);
2231 output_eh_header (struct fde_entry *fde)
2236 if (fde->eh_header_type == EH_COMPACT_INLINE)
2241 cfi_emit_eh_header (fde->start_address, addend);
2243 if (fde->eh_header_type == EH_COMPACT_INLINE)
2246 /* Inline entries always use PR1. */
2248 memcpy(p, fde->eh_data, 3);
2252 if (fde->eh_header_type == EH_COMPACT_LEGACY)
2254 else if (fde->eh_header_type == EH_COMPACT_OUTLINE
2255 || fde->eh_header_type == EH_COMPACT_OUTLINE_DONE)
2259 cfi_emit_eh_header (fde->eh_loc, addend);
2267 struct cie_entry *cie, *cie_next;
2268 segT cfi_seg, ccseg;
2269 struct fde_entry *fde;
2270 struct cfi_insn_data *first;
2271 int save_flag_traditional_format, seek_next_seg;
2273 if (all_fde_data == 0)
2276 cfi_sections_set = TRUE;
2277 if ((all_cfi_sections & CFI_EMIT_eh_frame) != 0
2278 || (all_cfi_sections & CFI_EMIT_eh_frame_compact) != 0)
2280 /* Make sure check_eh_frame doesn't do anything with our output. */
2281 save_flag_traditional_format = flag_traditional_format;
2282 flag_traditional_format = 1;
2284 if (!EH_FRAME_LINKONCE)
2286 /* Open .eh_frame section. */
2287 cfi_seg = get_cfi_seg (NULL, ".eh_frame",
2288 (SEC_ALLOC | SEC_LOAD | SEC_DATA
2289 | DWARF2_EH_FRAME_READ_ONLY),
2290 EH_FRAME_ALIGNMENT);
2291 #ifdef md_fix_up_eh_frame
2292 md_fix_up_eh_frame (cfi_seg);
2303 for (cie = cie_root; cie; cie = cie_next)
2305 cie_next = cie->next;
2306 free ((void *) cie);
2310 for (fde = all_fde_data; fde ; fde = fde->next)
2312 if ((fde->sections & CFI_EMIT_eh_frame) == 0
2313 && (fde->sections & CFI_EMIT_eh_frame_compact) == 0)
2316 #if SUPPORT_COMPACT_EH
2317 /* Emit a LEGACY format header if we have processed all
2318 of the .cfi directives without encountering either inline or
2319 out-of-line compact unwinding opcodes. */
2320 if (fde->eh_header_type == EH_COMPACT_HAS_LSDA
2321 || fde->eh_header_type == EH_COMPACT_UNKNOWN)
2322 fde->eh_header_type = EH_COMPACT_LEGACY;
2324 if (fde->eh_header_type != EH_COMPACT_LEGACY)
2327 if (EH_FRAME_LINKONCE)
2331 if (seek_next_seg && CUR_SEG (fde) != ccseg)
2338 ccseg = CUR_SEG (fde);
2339 /* Open .eh_frame section. */
2340 cfi_seg = get_cfi_seg (ccseg, ".eh_frame",
2341 (SEC_ALLOC | SEC_LOAD | SEC_DATA
2342 | DWARF2_EH_FRAME_READ_ONLY),
2343 EH_FRAME_ALIGNMENT);
2344 #ifdef md_fix_up_eh_frame
2345 md_fix_up_eh_frame (cfi_seg);
2351 SET_HANDLED (fde, 1);
2354 if (fde->end_address == NULL)
2356 as_bad (_("open CFI at the end of file; "
2357 "missing .cfi_endproc directive"));
2358 fde->end_address = fde->start_address;
2361 cie = select_cie_for_fde (fde, TRUE, &first, 2);
2362 fde->eh_loc = symbol_temp_new_now ();
2363 output_fde (fde, cie, TRUE, first,
2364 fde->next == NULL ? EH_FRAME_ALIGNMENT : 2);
2367 while (EH_FRAME_LINKONCE && seek_next_seg == 2);
2369 if (EH_FRAME_LINKONCE)
2370 for (fde = all_fde_data; fde ; fde = fde->next)
2371 SET_HANDLED (fde, 0);
2373 #if SUPPORT_COMPACT_EH
2376 /* Create remaining out of line table entries. */
2382 for (fde = all_fde_data; fde ; fde = fde->next)
2384 if ((fde->sections & CFI_EMIT_eh_frame) == 0
2385 && (fde->sections & CFI_EMIT_eh_frame_compact) == 0)
2388 if (fde->eh_header_type != EH_COMPACT_OUTLINE)
2392 if (seek_next_seg && CUR_SEG (fde) != ccseg)
2399 ccseg = CUR_SEG (fde);
2400 /* Open .gnu_extab section. */
2401 get_cfi_seg (ccseg, ".gnu_extab",
2402 (SEC_ALLOC | SEC_LOAD | SEC_DATA
2403 | DWARF2_EH_FRAME_READ_ONLY),
2407 SET_HANDLED (fde, 1);
2409 frag_align (1, 0, 0);
2410 record_alignment (now_seg, 1);
2411 output_compact_unwind_data (fde, 1);
2414 while (EH_FRAME_LINKONCE && seek_next_seg == 2);
2416 for (fde = all_fde_data; fde ; fde = fde->next)
2417 SET_HANDLED (fde, 0);
2419 /* Create index table fragments. */
2425 for (fde = all_fde_data; fde ; fde = fde->next)
2427 if ((fde->sections & CFI_EMIT_eh_frame) == 0
2428 && (fde->sections & CFI_EMIT_eh_frame_compact) == 0)
2433 if (seek_next_seg && CUR_SEG (fde) != ccseg)
2440 ccseg = CUR_SEG (fde);
2441 /* Open .eh_frame_entry section. */
2442 cfi_seg = get_cfi_seg (ccseg, ".eh_frame_entry",
2443 (SEC_ALLOC | SEC_LOAD | SEC_DATA
2444 | DWARF2_EH_FRAME_READ_ONLY),
2448 SET_HANDLED (fde, 1);
2450 output_eh_header (fde);
2453 while (seek_next_seg == 2);
2455 for (fde = all_fde_data; fde ; fde = fde->next)
2456 SET_HANDLED (fde, 0);
2458 #endif /* SUPPORT_COMPACT_EH */
2460 flag_traditional_format = save_flag_traditional_format;
2463 cfi_sections_set = TRUE;
2464 if ((all_cfi_sections & CFI_EMIT_debug_frame) != 0)
2466 int alignment = ffs (DWARF2_ADDR_SIZE (stdoutput)) - 1;
2468 if (!SUPPORT_FRAME_LINKONCE)
2469 get_cfi_seg (NULL, ".debug_frame",
2470 SEC_READONLY | SEC_DEBUGGING,
2478 for (cie = cie_root; cie; cie = cie_next)
2480 cie_next = cie->next;
2481 free ((void *) cie);
2485 for (fde = all_fde_data; fde ; fde = fde->next)
2487 if ((fde->sections & CFI_EMIT_debug_frame) == 0)
2490 if (SUPPORT_FRAME_LINKONCE)
2494 if (seek_next_seg && CUR_SEG (fde) != ccseg)
2501 ccseg = CUR_SEG (fde);
2502 /* Open .debug_frame section. */
2503 get_cfi_seg (ccseg, ".debug_frame",
2504 SEC_READONLY | SEC_DEBUGGING,
2508 SET_HANDLED (fde, 1);
2510 if (fde->end_address == NULL)
2512 as_bad (_("open CFI at the end of file; "
2513 "missing .cfi_endproc directive"));
2514 fde->end_address = fde->start_address;
2517 fde->per_encoding = DW_EH_PE_omit;
2518 fde->lsda_encoding = DW_EH_PE_omit;
2519 cfi_change_reg_numbers (fde->data, ccseg);
2520 cie = select_cie_for_fde (fde, FALSE, &first, alignment);
2521 output_fde (fde, cie, FALSE, first, alignment);
2524 while (SUPPORT_FRAME_LINKONCE && seek_next_seg == 2);
2526 if (SUPPORT_FRAME_LINKONCE)
2527 for (fde = all_fde_data; fde ; fde = fde->next)
2528 SET_HANDLED (fde, 0);
2532 #else /* TARGET_USE_CFIPOP */
2534 /* Emit an intelligible error message for missing support. */
2537 dot_cfi_dummy (int ignored ATTRIBUTE_UNUSED)
2539 as_bad (_("CFI is not supported for this target"));
2540 ignore_rest_of_line ();
2543 const pseudo_typeS cfi_pseudo_table[] =
2545 { "cfi_sections", dot_cfi_dummy, 0 },
2546 { "cfi_startproc", dot_cfi_dummy, 0 },
2547 { "cfi_endproc", dot_cfi_dummy, 0 },
2548 { "cfi_fde_data", dot_cfi_dummy, 0 },
2549 { "cfi_def_cfa", dot_cfi_dummy, 0 },
2550 { "cfi_def_cfa_register", dot_cfi_dummy, 0 },
2551 { "cfi_def_cfa_offset", dot_cfi_dummy, 0 },
2552 { "cfi_adjust_cfa_offset", dot_cfi_dummy, 0 },
2553 { "cfi_offset", dot_cfi_dummy, 0 },
2554 { "cfi_rel_offset", dot_cfi_dummy, 0 },
2555 { "cfi_register", dot_cfi_dummy, 0 },
2556 { "cfi_return_column", dot_cfi_dummy, 0 },
2557 { "cfi_restore", dot_cfi_dummy, 0 },
2558 { "cfi_undefined", dot_cfi_dummy, 0 },
2559 { "cfi_same_value", dot_cfi_dummy, 0 },
2560 { "cfi_remember_state", dot_cfi_dummy, 0 },
2561 { "cfi_restore_state", dot_cfi_dummy, 0 },
2562 { "cfi_window_save", dot_cfi_dummy, 0 },
2563 { "cfi_escape", dot_cfi_dummy, 0 },
2564 { "cfi_signal_frame", dot_cfi_dummy, 0 },
2565 { "cfi_personality", dot_cfi_dummy, 0 },
2566 { "cfi_personality_id", dot_cfi_dummy, 0 },
2567 { "cfi_lsda", dot_cfi_dummy, 0 },
2568 { "cfi_val_encoded_addr", dot_cfi_dummy, 0 },
2569 { "cfi_label", dot_cfi_dummy, 0 },
2570 { "cfi_inline_lsda", dot_cfi_dummy, 0 },
2571 { "cfi_val_offset", dot_cfi_dummy, 0 },
2579 #endif /* TARGET_USE_CFIPOP */