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)
1203 if (frchain_now->frch_cfi_data == NULL)
1205 as_bad (_("CFI instruction used without previous .cfi_startproc"));
1206 ignore_rest_of_line ();
1210 name = read_symbol_name ();
1214 /* If the last address was not at the current PC, advance to current. */
1215 if (symbol_get_frag (frchain_now->frch_cfi_data->last_address) != frag_now
1216 || (S_GET_VALUE (frchain_now->frch_cfi_data->last_address)
1217 != frag_now_fix ()))
1218 cfi_add_advance_loc (symbol_temp_new_now ());
1220 cfi_add_label (name);
1223 demand_empty_rest_of_line ();
1227 dot_cfi_sections (int ignored ATTRIBUTE_UNUSED)
1232 if (is_name_beginner (*input_line_pointer) || *input_line_pointer == '"')
1238 saved_ilp = input_line_pointer;
1239 c = get_symbol_name (& name);
1241 if (strncmp (name, ".eh_frame", sizeof ".eh_frame") == 0
1243 sections |= CFI_EMIT_eh_frame;
1244 else if (strncmp (name, ".debug_frame", sizeof ".debug_frame") == 0)
1245 sections |= CFI_EMIT_debug_frame;
1246 #if SUPPORT_COMPACT_EH
1247 else if (strncmp (name, ".eh_frame_entry",
1248 sizeof ".eh_frame_entry") == 0)
1251 sections |= CFI_EMIT_eh_frame_compact;
1254 #ifdef tc_cfi_section_name
1255 else if (strcmp (name, tc_cfi_section_name) == 0)
1256 sections |= CFI_EMIT_target;
1260 *input_line_pointer = c;
1261 input_line_pointer = saved_ilp;
1265 *input_line_pointer = c;
1266 SKIP_WHITESPACE_AFTER_NAME ();
1267 if (*input_line_pointer == ',')
1269 name = input_line_pointer++;
1271 if (!is_name_beginner (*input_line_pointer)
1272 && *input_line_pointer != '"')
1274 input_line_pointer = name;
1278 else if (is_name_beginner (*input_line_pointer)
1279 || *input_line_pointer == '"')
1283 demand_empty_rest_of_line ();
1284 if (cfi_sections_set
1285 && (sections & (CFI_EMIT_eh_frame | CFI_EMIT_eh_frame_compact))
1286 && ((cfi_sections & (CFI_EMIT_eh_frame | CFI_EMIT_eh_frame_compact))
1287 != (sections & (CFI_EMIT_eh_frame | CFI_EMIT_eh_frame_compact))))
1288 as_bad (_("inconsistent uses of .cfi_sections"));
1289 cfi_sections = sections;
1293 dot_cfi_startproc (int ignored ATTRIBUTE_UNUSED)
1297 if (frchain_now->frch_cfi_data != NULL)
1299 as_bad (_("previous CFI entry not closed (missing .cfi_endproc)"));
1300 ignore_rest_of_line ();
1304 cfi_new_fde (symbol_temp_new_now ());
1307 if (is_name_beginner (*input_line_pointer) || *input_line_pointer == '"')
1309 char * saved_ilp = input_line_pointer;
1312 c = get_symbol_name (& name);
1314 if (strcmp (name, "simple") == 0)
1317 restore_line_pointer (c);
1320 input_line_pointer = saved_ilp;
1322 demand_empty_rest_of_line ();
1324 cfi_sections_set = TRUE;
1325 all_cfi_sections |= cfi_sections;
1326 cfi_set_sections ();
1327 frchain_now->frch_cfi_data->cur_cfa_offset = 0;
1329 tc_cfi_frame_initial_instructions ();
1331 if ((cfi_sections & CFI_EMIT_target) != 0)
1332 tc_cfi_startproc ();
1336 dot_cfi_endproc (int ignored ATTRIBUTE_UNUSED)
1338 if (frchain_now->frch_cfi_data == NULL)
1340 as_bad (_(".cfi_endproc without corresponding .cfi_startproc"));
1341 ignore_rest_of_line ();
1345 last_fde = frchain_now->frch_cfi_data->cur_fde_data;
1347 cfi_end_fde (symbol_temp_new_now ());
1349 demand_empty_rest_of_line ();
1351 cfi_sections_set = TRUE;
1352 if ((cfi_sections & CFI_EMIT_target) != 0)
1353 tc_cfi_endproc (last_fde);
1357 get_cfi_seg (segT cseg, const char *base, flagword flags, int align)
1359 /* Exclude .debug_frame sections for Compact EH. */
1360 if (SUPPORT_FRAME_LINKONCE || ((flags & SEC_DEBUGGING) == 0 && compact_eh))
1362 struct dwcfi_seg_list *l;
1364 l = dwcfi_hash_find_or_make (cseg, base, flags);
1367 subseg_set (cseg, l->subseg);
1371 cseg = subseg_new (base, 0);
1372 bfd_set_section_flags (stdoutput, cseg, flags);
1374 record_alignment (cseg, align);
1378 #if SUPPORT_COMPACT_EH
1380 dot_cfi_personality_id (int ignored ATTRIBUTE_UNUSED)
1382 struct fde_entry *fde;
1384 if (frchain_now->frch_cfi_data == NULL)
1386 as_bad (_("CFI instruction used without previous .cfi_startproc"));
1387 ignore_rest_of_line ();
1391 fde = frchain_now->frch_cfi_data->cur_fde_data;
1392 fde->personality_id = cfi_parse_const ();
1393 demand_empty_rest_of_line ();
1395 if (fde->personality_id == 0 || fde->personality_id > 3)
1397 as_bad (_("wrong argument to .cfi_personality_id"));
1403 dot_cfi_fde_data (int ignored ATTRIBUTE_UNUSED)
1405 if (frchain_now->frch_cfi_data == NULL)
1407 as_bad (_(".cfi_fde_data without corresponding .cfi_startproc"));
1408 ignore_rest_of_line ();
1412 last_fde = frchain_now->frch_cfi_data->cur_fde_data;
1414 cfi_sections_set = TRUE;
1415 if ((cfi_sections & CFI_EMIT_target) != 0
1416 || (cfi_sections & CFI_EMIT_eh_frame_compact) != 0)
1418 struct cfi_escape_data *head, **tail, *e;
1422 if (!is_it_end_of_statement ())
1427 e = XNEW (struct cfi_escape_data);
1428 do_parse_cons_expression (&e->exp, 1);
1433 while (*input_line_pointer++ == ',');
1434 --input_line_pointer;
1438 if (last_fde->lsda_encoding != DW_EH_PE_omit)
1439 last_fde->eh_header_type = EH_COMPACT_HAS_LSDA;
1440 else if (num_ops <= 3 && last_fde->per_encoding == DW_EH_PE_omit)
1441 last_fde->eh_header_type = EH_COMPACT_INLINE;
1443 last_fde->eh_header_type = EH_COMPACT_OUTLINE;
1445 if (last_fde->eh_header_type == EH_COMPACT_INLINE)
1448 last_fde->eh_data_size = num_ops;
1449 last_fde->eh_data = XNEWVEC (bfd_byte, num_ops);
1455 last_fde->eh_data[num_ops++] = e->exp.X_add_number;
1458 if (last_fde->eh_header_type == EH_COMPACT_INLINE)
1460 last_fde->eh_data[num_ops++] = tc_compact_eh_opcode_stop;
1463 demand_empty_rest_of_line ();
1466 /* Function to emit the compact unwinding opcodes stored in the
1467 fde's eh_data field. The end of the opcode data will be
1468 padded to the value in align. */
1471 output_compact_unwind_data (struct fde_entry *fde, int align)
1473 int data_size = fde->eh_data_size + 2;
1478 fde->eh_loc = symbol_temp_new_now ();
1481 if (fde->personality_id != 0)
1482 *p = fde->personality_id;
1483 else if (fde->per_encoding != DW_EH_PE_omit)
1486 emit_expr_encoded (&fde->personality, fde->per_encoding, FALSE);
1487 data_size += encoding_size (fde->per_encoding);
1492 amask = (1 << align) - 1;
1493 align_padding = ((data_size + amask) & ~amask) - data_size;
1495 p = frag_more (fde->eh_data_size + 1 + align_padding);
1496 memcpy (p, fde->eh_data, fde->eh_data_size);
1497 p += fde->eh_data_size;
1499 while (align_padding-- > 0)
1500 *(p++) = tc_compact_eh_opcode_pad;
1502 *(p++) = tc_compact_eh_opcode_stop;
1503 fde->eh_header_type = EH_COMPACT_OUTLINE_DONE;
1506 /* Handle the .cfi_inline_lsda directive. */
1508 dot_cfi_inline_lsda (int ignored ATTRIBUTE_UNUSED)
1512 long max_alignment = 28;
1516 as_bad (_("unexpected .cfi_inline_lsda"));
1517 ignore_rest_of_line ();
1521 if ((last_fde->sections & CFI_EMIT_eh_frame_compact) == 0)
1523 as_bad (_(".cfi_inline_lsda not valid for this frame"));
1524 ignore_rest_of_line ();
1528 if (last_fde->eh_header_type != EH_COMPACT_UNKNOWN
1529 && last_fde->eh_header_type != EH_COMPACT_HAS_LSDA)
1531 as_bad (_(".cfi_inline_lsda seen for frame without .cfi_lsda"));
1532 ignore_rest_of_line ();
1536 #ifdef md_flush_pending_output
1537 md_flush_pending_output ();
1540 align = get_absolute_expression ();
1541 if (align > max_alignment)
1543 align = max_alignment;
1544 as_bad (_("Alignment too large: %d. assumed."), align);
1548 as_warn (_("Alignment negative: 0 assumed."));
1552 demand_empty_rest_of_line ();
1553 ccseg = CUR_SEG (last_fde);
1555 /* Open .gnu_extab section. */
1556 get_cfi_seg (ccseg, ".gnu_extab",
1557 (SEC_ALLOC | SEC_LOAD | SEC_DATA
1558 | DWARF2_EH_FRAME_READ_ONLY),
1561 frag_align (align, 0, 0);
1562 record_alignment (now_seg, align);
1563 if (last_fde->eh_header_type == EH_COMPACT_HAS_LSDA)
1564 output_compact_unwind_data (last_fde, align);
1570 #else /* !SUPPORT_COMPACT_EH */
1572 dot_cfi_inline_lsda (int ignored ATTRIBUTE_UNUSED)
1574 as_bad (_(".cfi_inline_lsda is not supported for this target"));
1575 ignore_rest_of_line ();
1579 dot_cfi_fde_data (int ignored ATTRIBUTE_UNUSED)
1581 as_bad (_(".cfi_fde_data is not supported for this target"));
1582 ignore_rest_of_line ();
1586 dot_cfi_personality_id (int ignored ATTRIBUTE_UNUSED)
1588 as_bad (_(".cfi_personality_id is not supported for this target"));
1589 ignore_rest_of_line ();
1594 output_cfi_insn (struct cfi_insn_data *insn)
1601 case DW_CFA_advance_loc:
1603 symbolS *from = insn->u.ll.lab1;
1604 symbolS *to = insn->u.ll.lab2;
1606 if (symbol_get_frag (to) == symbol_get_frag (from))
1608 addressT delta = S_GET_VALUE (to) - S_GET_VALUE (from);
1609 addressT scaled = delta / DWARF2_LINE_MIN_INSN_LENGTH;
1612 out_one (DW_CFA_advance_loc + scaled);
1613 else if (scaled <= 0xFF)
1615 out_one (DW_CFA_advance_loc1);
1618 else if (scaled <= 0xFFFF)
1620 out_one (DW_CFA_advance_loc2);
1625 out_one (DW_CFA_advance_loc4);
1633 exp.X_op = O_subtract;
1634 exp.X_add_symbol = to;
1635 exp.X_op_symbol = from;
1636 exp.X_add_number = 0;
1638 /* The code in ehopt.c expects that one byte of the encoding
1639 is already allocated to the frag. This comes from the way
1640 that it scans the .eh_frame section looking first for the
1641 .byte DW_CFA_advance_loc4. */
1642 *frag_more (1) = DW_CFA_advance_loc4;
1644 frag_var (rs_cfa, 4, 0, DWARF2_LINE_MIN_INSN_LENGTH << 3,
1645 make_expr_symbol (&exp), frag_now_fix () - 1,
1651 case DW_CFA_def_cfa:
1652 offset = insn->u.ri.offset;
1655 out_one (DW_CFA_def_cfa_sf);
1656 out_uleb128 (insn->u.ri.reg);
1657 out_sleb128 (offset / DWARF2_CIE_DATA_ALIGNMENT);
1661 out_one (DW_CFA_def_cfa);
1662 out_uleb128 (insn->u.ri.reg);
1663 out_uleb128 (offset);
1667 case DW_CFA_def_cfa_register:
1668 case DW_CFA_undefined:
1669 case DW_CFA_same_value:
1670 out_one (insn->insn);
1671 out_uleb128 (insn->u.r);
1674 case DW_CFA_def_cfa_offset:
1678 out_one (DW_CFA_def_cfa_offset_sf);
1679 out_sleb128 (offset / DWARF2_CIE_DATA_ALIGNMENT);
1683 out_one (DW_CFA_def_cfa_offset);
1684 out_uleb128 (offset);
1688 case DW_CFA_restore:
1692 out_one (DW_CFA_restore + regno);
1696 out_one (DW_CFA_restore_extended);
1697 out_uleb128 (regno);
1702 regno = insn->u.ri.reg;
1703 offset = insn->u.ri.offset / DWARF2_CIE_DATA_ALIGNMENT;
1706 out_one (DW_CFA_offset_extended_sf);
1707 out_uleb128 (regno);
1708 out_sleb128 (offset);
1710 else if (regno <= 0x3F)
1712 out_one (DW_CFA_offset + regno);
1713 out_uleb128 (offset);
1717 out_one (DW_CFA_offset_extended);
1718 out_uleb128 (regno);
1719 out_uleb128 (offset);
1723 case DW_CFA_val_offset:
1724 regno = insn->u.ri.reg;
1725 offset = insn->u.ri.offset / DWARF2_CIE_DATA_ALIGNMENT;
1728 out_one (DW_CFA_val_offset_sf);
1729 out_uleb128 (regno);
1730 out_sleb128 (offset);
1734 out_one (DW_CFA_val_offset);
1735 out_uleb128 (regno);
1736 out_uleb128 (offset);
1740 case DW_CFA_register:
1741 out_one (DW_CFA_register);
1742 out_uleb128 (insn->u.rr.reg1);
1743 out_uleb128 (insn->u.rr.reg2);
1746 case DW_CFA_remember_state:
1747 case DW_CFA_restore_state:
1748 out_one (insn->insn);
1751 case DW_CFA_GNU_window_save:
1752 out_one (DW_CFA_GNU_window_save);
1757 struct cfi_escape_data *e;
1758 for (e = insn->u.esc; e ; e = e->next)
1759 emit_expr (&e->exp, 1);
1763 case CFI_val_encoded_addr:
1765 unsigned encoding = insn->u.ea.encoding;
1768 if (encoding == DW_EH_PE_omit)
1770 out_one (DW_CFA_val_expression);
1771 out_uleb128 (insn->u.ea.reg);
1773 switch (encoding & 0x7)
1775 case DW_EH_PE_absptr:
1776 enc_size = DWARF2_ADDR_SIZE (stdoutput);
1778 case DW_EH_PE_udata2:
1781 case DW_EH_PE_udata4:
1784 case DW_EH_PE_udata8:
1791 /* If the user has requested absolute encoding,
1792 then use the smaller DW_OP_addr encoding. */
1793 if (insn->u.ea.encoding == DW_EH_PE_absptr)
1795 out_uleb128 (1 + enc_size);
1796 out_one (DW_OP_addr);
1800 out_uleb128 (1 + 1 + enc_size);
1801 out_one (DW_OP_GNU_encoded_addr);
1804 if ((encoding & 0x70) == DW_EH_PE_pcrel)
1806 #if CFI_DIFF_EXPR_OK
1807 insn->u.ea.exp.X_op = O_subtract;
1808 insn->u.ea.exp.X_op_symbol = symbol_temp_new_now ();
1809 #elif defined (tc_cfi_emit_pcrel_expr)
1810 tc_cfi_emit_pcrel_expr (&insn->u.ea.exp, enc_size);
1817 emit_expr (&insn->u.ea.exp, enc_size);
1822 colon (insn->u.sym_name);
1831 output_cie (struct cie_entry *cie, bfd_boolean eh_frame, int align)
1833 symbolS *after_size_address, *end_address;
1835 struct cfi_insn_data *i;
1836 offsetT augmentation_size;
1838 enum dwarf2_format fmt = DWARF2_FORMAT (now_seg);
1840 cie->start_address = symbol_temp_new_now ();
1841 after_size_address = symbol_temp_make ();
1842 end_address = symbol_temp_make ();
1844 exp.X_op = O_subtract;
1845 exp.X_add_symbol = end_address;
1846 exp.X_op_symbol = after_size_address;
1847 exp.X_add_number = 0;
1849 if (eh_frame || fmt == dwarf2_format_32bit)
1850 emit_expr (&exp, 4); /* Length. */
1853 if (fmt == dwarf2_format_64bit)
1855 emit_expr (&exp, 8); /* Length. */
1857 symbol_set_value_now (after_size_address);
1859 out_four (0); /* CIE id. */
1862 out_four (-1); /* CIE id. */
1863 if (fmt != dwarf2_format_32bit)
1866 out_one (DW_CIE_VERSION); /* Version. */
1869 out_one ('z'); /* Augmentation. */
1870 if (cie->per_encoding != DW_EH_PE_omit)
1872 if (cie->lsda_encoding != DW_EH_PE_omit)
1876 if (cie->signal_frame)
1879 out_uleb128 (DWARF2_LINE_MIN_INSN_LENGTH); /* Code alignment. */
1880 out_sleb128 (DWARF2_CIE_DATA_ALIGNMENT); /* Data alignment. */
1881 if (DW_CIE_VERSION == 1) /* Return column. */
1882 out_one (cie->return_column);
1884 out_uleb128 (cie->return_column);
1887 augmentation_size = 1 + (cie->lsda_encoding != DW_EH_PE_omit);
1888 if (cie->per_encoding != DW_EH_PE_omit)
1889 augmentation_size += 1 + encoding_size (cie->per_encoding);
1890 out_uleb128 (augmentation_size); /* Augmentation size. */
1892 emit_expr_encoded (&cie->personality, cie->per_encoding, TRUE);
1894 if (cie->lsda_encoding != DW_EH_PE_omit)
1895 out_one (cie->lsda_encoding);
1898 switch (DWARF2_FDE_RELOC_SIZE)
1901 enc = DW_EH_PE_sdata2;
1904 enc = DW_EH_PE_sdata4;
1907 enc = DW_EH_PE_sdata8;
1912 #if CFI_DIFF_EXPR_OK || defined tc_cfi_emit_pcrel_expr
1913 enc |= DW_EH_PE_pcrel;
1915 #ifdef DWARF2_FDE_RELOC_ENCODING
1916 /* Allow target to override encoding. */
1917 enc = DWARF2_FDE_RELOC_ENCODING (enc);
1919 cie->fde_encoding = enc;
1925 for (i = cie->first; i != cie->last; i = i->next)
1927 if (CUR_SEG (i) != CUR_SEG (cie))
1929 output_cfi_insn (i);
1933 frag_align (align, DW_CFA_nop, 0);
1934 symbol_set_value_now (end_address);
1938 output_fde (struct fde_entry *fde, struct cie_entry *cie,
1939 bfd_boolean eh_frame, struct cfi_insn_data *first,
1942 symbolS *after_size_address, *end_address;
1944 offsetT augmentation_size;
1945 enum dwarf2_format fmt = DWARF2_FORMAT (now_seg);
1946 unsigned int offset_size;
1947 unsigned int addr_size;
1949 after_size_address = symbol_temp_make ();
1950 end_address = symbol_temp_make ();
1952 exp.X_op = O_subtract;
1953 exp.X_add_symbol = end_address;
1954 exp.X_op_symbol = after_size_address;
1955 exp.X_add_number = 0;
1956 if (eh_frame || fmt == dwarf2_format_32bit)
1960 if (fmt == dwarf2_format_64bit)
1964 emit_expr (&exp, offset_size); /* Length. */
1965 symbol_set_value_now (after_size_address);
1969 exp.X_op = O_subtract;
1970 exp.X_add_symbol = after_size_address;
1971 exp.X_op_symbol = cie->start_address;
1972 exp.X_add_number = 0;
1973 emit_expr (&exp, offset_size); /* CIE offset. */
1977 TC_DWARF2_EMIT_OFFSET (cie->start_address, offset_size);
1980 exp.X_op = O_symbol;
1983 bfd_reloc_code_real_type code
1984 = tc_cfi_reloc_for_encoding (cie->fde_encoding);
1985 addr_size = DWARF2_FDE_RELOC_SIZE;
1986 if (code != BFD_RELOC_NONE)
1988 reloc_howto_type *howto = bfd_reloc_type_lookup (stdoutput, code);
1989 char *p = frag_more (addr_size);
1990 gas_assert (addr_size == (unsigned) howto->bitsize / 8);
1991 md_number_to_chars (p, 0, addr_size);
1992 fix_new (frag_now, p - frag_now->fr_literal, addr_size,
1993 fde->start_address, 0, howto->pc_relative, code);
1997 exp.X_op = O_subtract;
1998 exp.X_add_number = 0;
1999 #if CFI_DIFF_EXPR_OK
2000 exp.X_add_symbol = fde->start_address;
2001 exp.X_op_symbol = symbol_temp_new_now ();
2002 emit_expr (&exp, addr_size); /* Code offset. */
2004 exp.X_op = O_symbol;
2005 exp.X_add_symbol = fde->start_address;
2007 #if defined(tc_cfi_emit_pcrel_expr)
2008 tc_cfi_emit_pcrel_expr (&exp, addr_size); /* Code offset. */
2010 emit_expr (&exp, addr_size); /* Code offset. */
2017 exp.X_add_number = 0;
2018 exp.X_add_symbol = fde->start_address;
2019 addr_size = DWARF2_ADDR_SIZE (stdoutput);
2020 emit_expr (&exp, addr_size);
2023 exp.X_op = O_subtract;
2024 exp.X_add_symbol = fde->end_address;
2025 exp.X_op_symbol = fde->start_address; /* Code length. */
2026 exp.X_add_number = 0;
2027 emit_expr (&exp, addr_size);
2029 augmentation_size = encoding_size (fde->lsda_encoding);
2031 out_uleb128 (augmentation_size); /* Augmentation size. */
2033 emit_expr_encoded (&fde->lsda, cie->lsda_encoding, FALSE);
2035 for (; first; first = first->next)
2036 if (CUR_SEG (first) == CUR_SEG (fde))
2037 output_cfi_insn (first);
2039 frag_align (align, DW_CFA_nop, 0);
2040 symbol_set_value_now (end_address);
2043 static struct cie_entry *
2044 select_cie_for_fde (struct fde_entry *fde, bfd_boolean eh_frame,
2045 struct cfi_insn_data **pfirst, int align)
2047 struct cfi_insn_data *i, *j;
2048 struct cie_entry *cie;
2050 for (cie = cie_root; cie; cie = cie->next)
2052 if (CUR_SEG (cie) != CUR_SEG (fde))
2054 if (cie->return_column != fde->return_column
2055 || cie->signal_frame != fde->signal_frame
2056 || cie->per_encoding != fde->per_encoding
2057 || cie->lsda_encoding != fde->lsda_encoding)
2059 if (cie->per_encoding != DW_EH_PE_omit)
2061 if (cie->personality.X_op != fde->personality.X_op
2062 || (cie->personality.X_add_number
2063 != fde->personality.X_add_number))
2065 switch (cie->personality.X_op)
2068 if (cie->personality.X_unsigned != fde->personality.X_unsigned)
2072 if (cie->personality.X_add_symbol
2073 != fde->personality.X_add_symbol)
2080 for (i = cie->first, j = fde->data;
2081 i != cie->last && j != NULL;
2082 i = i->next, j = j->next)
2084 if (i->insn != j->insn)
2088 case DW_CFA_advance_loc:
2089 case DW_CFA_remember_state:
2090 /* We reached the first advance/remember in the FDE,
2091 but did not reach the end of the CIE list. */
2095 case DW_CFA_def_cfa:
2096 if (i->u.ri.reg != j->u.ri.reg)
2098 if (i->u.ri.offset != j->u.ri.offset)
2102 case DW_CFA_register:
2103 if (i->u.rr.reg1 != j->u.rr.reg1)
2105 if (i->u.rr.reg2 != j->u.rr.reg2)
2109 case DW_CFA_def_cfa_register:
2110 case DW_CFA_restore:
2111 case DW_CFA_undefined:
2112 case DW_CFA_same_value:
2113 if (i->u.r != j->u.r)
2117 case DW_CFA_def_cfa_offset:
2118 if (i->u.i != j->u.i)
2123 case CFI_val_encoded_addr:
2125 /* Don't bother matching these for now. */
2133 /* Success if we reached the end of the CIE list, and we've either
2134 run out of FDE entries or we've encountered an advance,
2135 remember, or escape. */
2138 || j->insn == DW_CFA_advance_loc
2139 || j->insn == DW_CFA_remember_state
2140 || j->insn == CFI_escape
2141 || j->insn == CFI_val_encoded_addr
2142 || j->insn == CFI_label))
2151 cie = XNEW (struct cie_entry);
2152 cie->next = cie_root;
2154 SET_CUR_SEG (cie, CUR_SEG (fde));
2155 cie->return_column = fde->return_column;
2156 cie->signal_frame = fde->signal_frame;
2157 cie->per_encoding = fde->per_encoding;
2158 cie->lsda_encoding = fde->lsda_encoding;
2159 cie->personality = fde->personality;
2160 cie->first = fde->data;
2162 for (i = cie->first; i ; i = i->next)
2163 if (i->insn == DW_CFA_advance_loc
2164 || i->insn == DW_CFA_remember_state
2165 || i->insn == CFI_escape
2166 || i->insn == CFI_val_encoded_addr
2167 || i->insn == CFI_label)
2173 output_cie (cie, eh_frame, align);
2178 #ifdef md_reg_eh_frame_to_debug_frame
2180 cfi_change_reg_numbers (struct cfi_insn_data *insn, segT ccseg)
2182 for (; insn; insn = insn->next)
2184 if (CUR_SEG (insn) != ccseg)
2188 case DW_CFA_advance_loc:
2189 case DW_CFA_def_cfa_offset:
2190 case DW_CFA_remember_state:
2191 case DW_CFA_restore_state:
2192 case DW_CFA_GNU_window_save:
2197 case DW_CFA_def_cfa:
2199 insn->u.ri.reg = md_reg_eh_frame_to_debug_frame (insn->u.ri.reg);
2202 case DW_CFA_def_cfa_register:
2203 case DW_CFA_undefined:
2204 case DW_CFA_same_value:
2205 case DW_CFA_restore:
2206 insn->u.r = md_reg_eh_frame_to_debug_frame (insn->u.r);
2209 case DW_CFA_register:
2210 insn->u.rr.reg1 = md_reg_eh_frame_to_debug_frame (insn->u.rr.reg1);
2211 insn->u.rr.reg2 = md_reg_eh_frame_to_debug_frame (insn->u.rr.reg2);
2214 case CFI_val_encoded_addr:
2215 insn->u.ea.reg = md_reg_eh_frame_to_debug_frame (insn->u.ea.reg);
2224 #define cfi_change_reg_numbers(insn, cseg) do { } while (0)
2227 #if SUPPORT_COMPACT_EH
2229 cfi_emit_eh_header (symbolS *sym, bfd_vma addend)
2233 exp.X_add_number = addend;
2234 exp.X_add_symbol = sym;
2235 emit_expr_encoded (&exp, DW_EH_PE_sdata4 | DW_EH_PE_pcrel, FALSE);
2239 output_eh_header (struct fde_entry *fde)
2244 if (fde->eh_header_type == EH_COMPACT_INLINE)
2249 cfi_emit_eh_header (fde->start_address, addend);
2251 if (fde->eh_header_type == EH_COMPACT_INLINE)
2254 /* Inline entries always use PR1. */
2256 memcpy(p, fde->eh_data, 3);
2260 if (fde->eh_header_type == EH_COMPACT_LEGACY)
2262 else if (fde->eh_header_type == EH_COMPACT_OUTLINE
2263 || fde->eh_header_type == EH_COMPACT_OUTLINE_DONE)
2267 cfi_emit_eh_header (fde->eh_loc, addend);
2275 struct cie_entry *cie, *cie_next;
2276 segT cfi_seg, ccseg;
2277 struct fde_entry *fde;
2278 struct cfi_insn_data *first;
2279 int save_flag_traditional_format, seek_next_seg;
2281 if (all_fde_data == 0)
2284 cfi_sections_set = TRUE;
2285 if ((all_cfi_sections & CFI_EMIT_eh_frame) != 0
2286 || (all_cfi_sections & CFI_EMIT_eh_frame_compact) != 0)
2288 /* Make sure check_eh_frame doesn't do anything with our output. */
2289 save_flag_traditional_format = flag_traditional_format;
2290 flag_traditional_format = 1;
2292 if (!EH_FRAME_LINKONCE)
2294 /* Open .eh_frame section. */
2295 cfi_seg = get_cfi_seg (NULL, ".eh_frame",
2296 (SEC_ALLOC | SEC_LOAD | SEC_DATA
2297 | DWARF2_EH_FRAME_READ_ONLY),
2298 EH_FRAME_ALIGNMENT);
2299 #ifdef md_fix_up_eh_frame
2300 md_fix_up_eh_frame (cfi_seg);
2311 for (cie = cie_root; cie; cie = cie_next)
2313 cie_next = cie->next;
2314 free ((void *) cie);
2318 for (fde = all_fde_data; fde ; fde = fde->next)
2320 if ((fde->sections & CFI_EMIT_eh_frame) == 0
2321 && (fde->sections & CFI_EMIT_eh_frame_compact) == 0)
2324 #if SUPPORT_COMPACT_EH
2325 /* Emit a LEGACY format header if we have processed all
2326 of the .cfi directives without encountering either inline or
2327 out-of-line compact unwinding opcodes. */
2328 if (fde->eh_header_type == EH_COMPACT_HAS_LSDA
2329 || fde->eh_header_type == EH_COMPACT_UNKNOWN)
2330 fde->eh_header_type = EH_COMPACT_LEGACY;
2332 if (fde->eh_header_type != EH_COMPACT_LEGACY)
2335 if (EH_FRAME_LINKONCE)
2339 if (seek_next_seg && CUR_SEG (fde) != ccseg)
2346 ccseg = CUR_SEG (fde);
2347 /* Open .eh_frame section. */
2348 cfi_seg = get_cfi_seg (ccseg, ".eh_frame",
2349 (SEC_ALLOC | SEC_LOAD | SEC_DATA
2350 | DWARF2_EH_FRAME_READ_ONLY),
2351 EH_FRAME_ALIGNMENT);
2352 #ifdef md_fix_up_eh_frame
2353 md_fix_up_eh_frame (cfi_seg);
2359 SET_HANDLED (fde, 1);
2362 if (fde->end_address == NULL)
2364 as_bad (_("open CFI at the end of file; "
2365 "missing .cfi_endproc directive"));
2366 fde->end_address = fde->start_address;
2369 cie = select_cie_for_fde (fde, TRUE, &first, 2);
2370 fde->eh_loc = symbol_temp_new_now ();
2371 output_fde (fde, cie, TRUE, first,
2372 fde->next == NULL ? EH_FRAME_ALIGNMENT : 2);
2375 while (EH_FRAME_LINKONCE && seek_next_seg == 2);
2377 if (EH_FRAME_LINKONCE)
2378 for (fde = all_fde_data; fde ; fde = fde->next)
2379 SET_HANDLED (fde, 0);
2381 #if SUPPORT_COMPACT_EH
2384 /* Create remaining out of line table entries. */
2390 for (fde = all_fde_data; fde ; fde = fde->next)
2392 if ((fde->sections & CFI_EMIT_eh_frame) == 0
2393 && (fde->sections & CFI_EMIT_eh_frame_compact) == 0)
2396 if (fde->eh_header_type != EH_COMPACT_OUTLINE)
2400 if (seek_next_seg && CUR_SEG (fde) != ccseg)
2407 ccseg = CUR_SEG (fde);
2408 /* Open .gnu_extab section. */
2409 get_cfi_seg (ccseg, ".gnu_extab",
2410 (SEC_ALLOC | SEC_LOAD | SEC_DATA
2411 | DWARF2_EH_FRAME_READ_ONLY),
2415 SET_HANDLED (fde, 1);
2417 frag_align (1, 0, 0);
2418 record_alignment (now_seg, 1);
2419 output_compact_unwind_data (fde, 1);
2422 while (EH_FRAME_LINKONCE && seek_next_seg == 2);
2424 for (fde = all_fde_data; fde ; fde = fde->next)
2425 SET_HANDLED (fde, 0);
2427 /* Create index table fragments. */
2433 for (fde = all_fde_data; fde ; fde = fde->next)
2435 if ((fde->sections & CFI_EMIT_eh_frame) == 0
2436 && (fde->sections & CFI_EMIT_eh_frame_compact) == 0)
2441 if (seek_next_seg && CUR_SEG (fde) != ccseg)
2448 ccseg = CUR_SEG (fde);
2449 /* Open .eh_frame_entry section. */
2450 cfi_seg = get_cfi_seg (ccseg, ".eh_frame_entry",
2451 (SEC_ALLOC | SEC_LOAD | SEC_DATA
2452 | DWARF2_EH_FRAME_READ_ONLY),
2456 SET_HANDLED (fde, 1);
2458 output_eh_header (fde);
2461 while (seek_next_seg == 2);
2463 for (fde = all_fde_data; fde ; fde = fde->next)
2464 SET_HANDLED (fde, 0);
2466 #endif /* SUPPORT_COMPACT_EH */
2468 flag_traditional_format = save_flag_traditional_format;
2471 cfi_sections_set = TRUE;
2472 if ((all_cfi_sections & CFI_EMIT_debug_frame) != 0)
2474 int alignment = ffs (DWARF2_ADDR_SIZE (stdoutput)) - 1;
2476 if (!SUPPORT_FRAME_LINKONCE)
2477 get_cfi_seg (NULL, ".debug_frame",
2478 SEC_READONLY | SEC_DEBUGGING,
2486 for (cie = cie_root; cie; cie = cie_next)
2488 cie_next = cie->next;
2489 free ((void *) cie);
2493 for (fde = all_fde_data; fde ; fde = fde->next)
2495 if ((fde->sections & CFI_EMIT_debug_frame) == 0)
2498 if (SUPPORT_FRAME_LINKONCE)
2502 if (seek_next_seg && CUR_SEG (fde) != ccseg)
2509 ccseg = CUR_SEG (fde);
2510 /* Open .debug_frame section. */
2511 get_cfi_seg (ccseg, ".debug_frame",
2512 SEC_READONLY | SEC_DEBUGGING,
2516 SET_HANDLED (fde, 1);
2518 if (fde->end_address == NULL)
2520 as_bad (_("open CFI at the end of file; "
2521 "missing .cfi_endproc directive"));
2522 fde->end_address = fde->start_address;
2525 fde->per_encoding = DW_EH_PE_omit;
2526 fde->lsda_encoding = DW_EH_PE_omit;
2527 cfi_change_reg_numbers (fde->data, ccseg);
2528 cie = select_cie_for_fde (fde, FALSE, &first, alignment);
2529 output_fde (fde, cie, FALSE, first, alignment);
2532 while (SUPPORT_FRAME_LINKONCE && seek_next_seg == 2);
2534 if (SUPPORT_FRAME_LINKONCE)
2535 for (fde = all_fde_data; fde ; fde = fde->next)
2536 SET_HANDLED (fde, 0);
2540 #else /* TARGET_USE_CFIPOP */
2542 /* Emit an intelligible error message for missing support. */
2545 dot_cfi_dummy (int ignored ATTRIBUTE_UNUSED)
2547 as_bad (_("CFI is not supported for this target"));
2548 ignore_rest_of_line ();
2551 const pseudo_typeS cfi_pseudo_table[] =
2553 { "cfi_sections", dot_cfi_dummy, 0 },
2554 { "cfi_startproc", dot_cfi_dummy, 0 },
2555 { "cfi_endproc", dot_cfi_dummy, 0 },
2556 { "cfi_fde_data", dot_cfi_dummy, 0 },
2557 { "cfi_def_cfa", dot_cfi_dummy, 0 },
2558 { "cfi_def_cfa_register", dot_cfi_dummy, 0 },
2559 { "cfi_def_cfa_offset", dot_cfi_dummy, 0 },
2560 { "cfi_adjust_cfa_offset", dot_cfi_dummy, 0 },
2561 { "cfi_offset", dot_cfi_dummy, 0 },
2562 { "cfi_rel_offset", dot_cfi_dummy, 0 },
2563 { "cfi_register", dot_cfi_dummy, 0 },
2564 { "cfi_return_column", dot_cfi_dummy, 0 },
2565 { "cfi_restore", dot_cfi_dummy, 0 },
2566 { "cfi_undefined", dot_cfi_dummy, 0 },
2567 { "cfi_same_value", dot_cfi_dummy, 0 },
2568 { "cfi_remember_state", dot_cfi_dummy, 0 },
2569 { "cfi_restore_state", dot_cfi_dummy, 0 },
2570 { "cfi_window_save", dot_cfi_dummy, 0 },
2571 { "cfi_escape", dot_cfi_dummy, 0 },
2572 { "cfi_signal_frame", dot_cfi_dummy, 0 },
2573 { "cfi_personality", dot_cfi_dummy, 0 },
2574 { "cfi_personality_id", dot_cfi_dummy, 0 },
2575 { "cfi_lsda", dot_cfi_dummy, 0 },
2576 { "cfi_val_encoded_addr", dot_cfi_dummy, 0 },
2577 { "cfi_label", dot_cfi_dummy, 0 },
2578 { "cfi_inline_lsda", dot_cfi_dummy, 0 },
2579 { "cfi_val_offset", dot_cfi_dummy, 0 },
2587 #endif /* TARGET_USE_CFIPOP */