1 /* dw2gencfi.c - Support for generating Dwarf2 CFI information.
2 Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009
3 Free Software Foundation, Inc.
4 Contributed by Michal Ludvig <mludvig@suse.cz>
6 This file is part of GAS, the GNU Assembler.
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
24 #include "dw2gencfi.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 /* We re-use DWARF2_LINE_MIN_INSN_LENGTH for the code alignment field
39 of the CIE. Default to 1 if not otherwise specified. */
40 #ifndef DWARF2_LINE_MIN_INSN_LENGTH
41 # define DWARF2_LINE_MIN_INSN_LENGTH 1
44 /* By default, use 32-bit relocations from .eh_frame into .text. */
45 #ifndef DWARF2_FDE_RELOC_SIZE
46 # define DWARF2_FDE_RELOC_SIZE 4
49 /* By default, use a read-only .eh_frame section. */
50 #ifndef DWARF2_EH_FRAME_READ_ONLY
51 # define DWARF2_EH_FRAME_READ_ONLY SEC_READONLY
54 #ifndef EH_FRAME_ALIGNMENT
55 # define EH_FRAME_ALIGNMENT (bfd_get_arch_size (stdoutput) == 64 ? 3 : 2)
58 #ifndef tc_cfi_frame_initial_instructions
59 # define tc_cfi_frame_initial_instructions() ((void)0)
62 #ifndef DWARF2_ADDR_SIZE
63 # define DWARF2_ADDR_SIZE(bfd) (bfd_arch_bits_per_address (bfd) / 8)
69 struct cfi_insn_data *next;
90 struct cfi_escape_data {
91 struct cfi_escape_data *next;
96 unsigned reg, encoding;
104 struct fde_entry *next;
105 symbolS *start_address;
106 symbolS *end_address;
107 struct cfi_insn_data *data;
108 struct cfi_insn_data **last;
109 unsigned char per_encoding;
110 unsigned char lsda_encoding;
111 expressionS personality;
113 unsigned int return_column;
114 unsigned int signal_frame;
119 struct cie_entry *next;
120 symbolS *start_address;
121 unsigned int return_column;
122 unsigned int signal_frame;
123 unsigned char per_encoding;
124 unsigned char lsda_encoding;
125 expressionS personality;
126 struct cfi_insn_data *first, *last;
130 /* List of FDE entries. */
131 static struct fde_entry *all_fde_data;
132 static struct fde_entry **last_fde_data = &all_fde_data;
134 /* List of CIEs so that they could be reused. */
135 static struct cie_entry *cie_root;
137 /* Stack of old CFI data, for save/restore. */
140 struct cfa_save_data *next;
144 /* Current open FDE entry. */
147 struct fde_entry *cur_fde_data;
148 symbolS *last_address;
149 offsetT cur_cfa_offset;
150 struct cfa_save_data *cfa_save_stack;
153 /* Construct a new FDE structure and add it to the end of the fde list. */
155 static struct fde_entry *
156 alloc_fde_entry (void)
158 struct fde_entry *fde = xcalloc (1, sizeof (struct fde_entry));
160 frchain_now->frch_cfi_data = xcalloc (1, sizeof (struct frch_cfi_data));
161 frchain_now->frch_cfi_data->cur_fde_data = fde;
162 *last_fde_data = fde;
163 last_fde_data = &fde->next;
165 fde->last = &fde->data;
166 fde->return_column = DWARF2_DEFAULT_RETURN_COLUMN;
167 fde->per_encoding = DW_EH_PE_omit;
168 fde->lsda_encoding = DW_EH_PE_omit;
173 /* The following functions are available for a backend to construct its
174 own unwind information, usually from legacy unwind directives. */
176 /* Construct a new INSN structure and add it to the end of the insn list
177 for the currently active FDE. */
179 static struct cfi_insn_data *
180 alloc_cfi_insn_data (void)
182 struct cfi_insn_data *insn = xcalloc (1, sizeof (struct cfi_insn_data));
183 struct fde_entry *cur_fde_data = frchain_now->frch_cfi_data->cur_fde_data;
185 *cur_fde_data->last = insn;
186 cur_fde_data->last = &insn->next;
191 /* Construct a new FDE structure that begins at LABEL. */
194 cfi_new_fde (symbolS *label)
196 struct fde_entry *fde = alloc_fde_entry ();
197 fde->start_address = label;
198 frchain_now->frch_cfi_data->last_address = label;
201 /* End the currently open FDE. */
204 cfi_end_fde (symbolS *label)
206 frchain_now->frch_cfi_data->cur_fde_data->end_address = label;
207 free (frchain_now->frch_cfi_data);
208 frchain_now->frch_cfi_data = NULL;
211 /* Set the return column for the current FDE. */
214 cfi_set_return_column (unsigned regno)
216 frchain_now->frch_cfi_data->cur_fde_data->return_column = regno;
219 /* Universal functions to store new instructions. */
222 cfi_add_CFA_insn(int insn)
224 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
226 insn_ptr->insn = insn;
230 cfi_add_CFA_insn_reg (int insn, unsigned regno)
232 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
234 insn_ptr->insn = insn;
235 insn_ptr->u.r = regno;
239 cfi_add_CFA_insn_offset (int insn, offsetT offset)
241 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
243 insn_ptr->insn = insn;
244 insn_ptr->u.i = offset;
248 cfi_add_CFA_insn_reg_reg (int insn, unsigned reg1, unsigned reg2)
250 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
252 insn_ptr->insn = insn;
253 insn_ptr->u.rr.reg1 = reg1;
254 insn_ptr->u.rr.reg2 = reg2;
258 cfi_add_CFA_insn_reg_offset (int insn, unsigned regno, offsetT offset)
260 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
262 insn_ptr->insn = insn;
263 insn_ptr->u.ri.reg = regno;
264 insn_ptr->u.ri.offset = offset;
267 /* Add a CFI insn to advance the PC from the last address to LABEL. */
270 cfi_add_advance_loc (symbolS *label)
272 struct cfi_insn_data *insn = alloc_cfi_insn_data ();
274 insn->insn = DW_CFA_advance_loc;
275 insn->u.ll.lab1 = frchain_now->frch_cfi_data->last_address;
276 insn->u.ll.lab2 = label;
278 frchain_now->frch_cfi_data->last_address = label;
281 /* Add a DW_CFA_offset record to the CFI data. */
284 cfi_add_CFA_offset (unsigned regno, offsetT offset)
286 unsigned int abs_data_align;
288 gas_assert (DWARF2_CIE_DATA_ALIGNMENT != 0);
289 cfi_add_CFA_insn_reg_offset (DW_CFA_offset, regno, offset);
291 abs_data_align = (DWARF2_CIE_DATA_ALIGNMENT < 0
292 ? -DWARF2_CIE_DATA_ALIGNMENT : DWARF2_CIE_DATA_ALIGNMENT);
293 if (offset % abs_data_align)
294 as_bad (_("register save offset not a multiple of %u"), abs_data_align);
297 /* Add a DW_CFA_def_cfa record to the CFI data. */
300 cfi_add_CFA_def_cfa (unsigned regno, offsetT offset)
302 cfi_add_CFA_insn_reg_offset (DW_CFA_def_cfa, regno, offset);
303 frchain_now->frch_cfi_data->cur_cfa_offset = offset;
306 /* Add a DW_CFA_register record to the CFI data. */
309 cfi_add_CFA_register (unsigned reg1, unsigned reg2)
311 cfi_add_CFA_insn_reg_reg (DW_CFA_register, reg1, reg2);
314 /* Add a DW_CFA_def_cfa_register record to the CFI data. */
317 cfi_add_CFA_def_cfa_register (unsigned regno)
319 cfi_add_CFA_insn_reg (DW_CFA_def_cfa_register, regno);
322 /* Add a DW_CFA_def_cfa_offset record to the CFI data. */
325 cfi_add_CFA_def_cfa_offset (offsetT offset)
327 cfi_add_CFA_insn_offset (DW_CFA_def_cfa_offset, offset);
328 frchain_now->frch_cfi_data->cur_cfa_offset = offset;
332 cfi_add_CFA_restore (unsigned regno)
334 cfi_add_CFA_insn_reg (DW_CFA_restore, regno);
338 cfi_add_CFA_undefined (unsigned regno)
340 cfi_add_CFA_insn_reg (DW_CFA_undefined, regno);
344 cfi_add_CFA_same_value (unsigned regno)
346 cfi_add_CFA_insn_reg (DW_CFA_same_value, regno);
350 cfi_add_CFA_remember_state (void)
352 struct cfa_save_data *p;
354 cfi_add_CFA_insn (DW_CFA_remember_state);
356 p = xmalloc (sizeof (*p));
357 p->cfa_offset = frchain_now->frch_cfi_data->cur_cfa_offset;
358 p->next = frchain_now->frch_cfi_data->cfa_save_stack;
359 frchain_now->frch_cfi_data->cfa_save_stack = p;
363 cfi_add_CFA_restore_state (void)
365 struct cfa_save_data *p;
367 cfi_add_CFA_insn (DW_CFA_restore_state);
369 p = frchain_now->frch_cfi_data->cfa_save_stack;
372 frchain_now->frch_cfi_data->cur_cfa_offset = p->cfa_offset;
373 frchain_now->frch_cfi_data->cfa_save_stack = p->next;
377 as_bad (_("CFI state restore without previous remember"));
381 /* Parse CFI assembler directives. */
383 static void dot_cfi (int);
384 static void dot_cfi_escape (int);
385 static void dot_cfi_startproc (int);
386 static void dot_cfi_endproc (int);
387 static void dot_cfi_personality (int);
388 static void dot_cfi_lsda (int);
389 static void dot_cfi_val_encoded_addr (int);
391 /* Fake CFI type; outside the byte range of any real CFI insn. */
392 #define CFI_adjust_cfa_offset 0x100
393 #define CFI_return_column 0x101
394 #define CFI_rel_offset 0x102
395 #define CFI_escape 0x103
396 #define CFI_signal_frame 0x104
397 #define CFI_val_encoded_addr 0x105
399 const pseudo_typeS cfi_pseudo_table[] =
401 { "cfi_startproc", dot_cfi_startproc, 0 },
402 { "cfi_endproc", dot_cfi_endproc, 0 },
403 { "cfi_def_cfa", dot_cfi, DW_CFA_def_cfa },
404 { "cfi_def_cfa_register", dot_cfi, DW_CFA_def_cfa_register },
405 { "cfi_def_cfa_offset", dot_cfi, DW_CFA_def_cfa_offset },
406 { "cfi_adjust_cfa_offset", dot_cfi, CFI_adjust_cfa_offset },
407 { "cfi_offset", dot_cfi, DW_CFA_offset },
408 { "cfi_rel_offset", dot_cfi, CFI_rel_offset },
409 { "cfi_register", dot_cfi, DW_CFA_register },
410 { "cfi_return_column", dot_cfi, CFI_return_column },
411 { "cfi_restore", dot_cfi, DW_CFA_restore },
412 { "cfi_undefined", dot_cfi, DW_CFA_undefined },
413 { "cfi_same_value", dot_cfi, DW_CFA_same_value },
414 { "cfi_remember_state", dot_cfi, DW_CFA_remember_state },
415 { "cfi_restore_state", dot_cfi, DW_CFA_restore_state },
416 { "cfi_window_save", dot_cfi, DW_CFA_GNU_window_save },
417 { "cfi_escape", dot_cfi_escape, 0 },
418 { "cfi_signal_frame", dot_cfi, CFI_signal_frame },
419 { "cfi_personality", dot_cfi_personality, 0 },
420 { "cfi_lsda", dot_cfi_lsda, 0 },
421 { "cfi_val_encoded_addr", dot_cfi_val_encoded_addr, 0 },
426 cfi_parse_separator (void)
429 if (*input_line_pointer == ',')
430 input_line_pointer++;
432 as_bad (_("missing separator"));
435 #ifndef tc_parse_to_dw2regnum
437 tc_parse_to_dw2regnum(expressionS *exp)
439 # ifdef tc_regname_to_dw2regnum
441 if (is_name_beginner (*input_line_pointer)
442 || (*input_line_pointer == '%'
443 && is_name_beginner (*++input_line_pointer)))
447 name = input_line_pointer;
448 c = get_symbol_end ();
450 exp->X_op = O_constant;
451 exp->X_add_number = tc_regname_to_dw2regnum (name);
453 *input_line_pointer = c;
457 expression_and_evaluate (exp);
467 tc_parse_to_dw2regnum (&exp);
472 regno = exp.X_add_number;
482 as_bad (_("bad register expression"));
490 cfi_parse_const (void)
492 return get_absolute_expression ();
501 if (frchain_now->frch_cfi_data == NULL)
503 as_bad (_("CFI instruction used without previous .cfi_startproc"));
504 ignore_rest_of_line ();
508 /* If the last address was not at the current PC, advance to current. */
509 if (symbol_get_frag (frchain_now->frch_cfi_data->last_address) != frag_now
510 || S_GET_VALUE (frchain_now->frch_cfi_data->last_address)
512 cfi_add_advance_loc (symbol_temp_new_now ());
517 reg1 = cfi_parse_reg ();
518 cfi_parse_separator ();
519 offset = cfi_parse_const ();
520 cfi_add_CFA_offset (reg1, offset);
524 reg1 = cfi_parse_reg ();
525 cfi_parse_separator ();
526 offset = cfi_parse_const ();
527 cfi_add_CFA_offset (reg1,
528 offset - frchain_now->frch_cfi_data->cur_cfa_offset);
532 reg1 = cfi_parse_reg ();
533 cfi_parse_separator ();
534 offset = cfi_parse_const ();
535 cfi_add_CFA_def_cfa (reg1, offset);
538 case DW_CFA_register:
539 reg1 = cfi_parse_reg ();
540 cfi_parse_separator ();
541 reg2 = cfi_parse_reg ();
542 cfi_add_CFA_register (reg1, reg2);
545 case DW_CFA_def_cfa_register:
546 reg1 = cfi_parse_reg ();
547 cfi_add_CFA_def_cfa_register (reg1);
550 case DW_CFA_def_cfa_offset:
551 offset = cfi_parse_const ();
552 cfi_add_CFA_def_cfa_offset (offset);
555 case CFI_adjust_cfa_offset:
556 offset = cfi_parse_const ();
557 cfi_add_CFA_def_cfa_offset (frchain_now->frch_cfi_data->cur_cfa_offset
564 reg1 = cfi_parse_reg ();
565 cfi_add_CFA_restore (reg1);
567 if (*input_line_pointer != ',')
569 ++input_line_pointer;
573 case DW_CFA_undefined:
576 reg1 = cfi_parse_reg ();
577 cfi_add_CFA_undefined (reg1);
579 if (*input_line_pointer != ',')
581 ++input_line_pointer;
585 case DW_CFA_same_value:
586 reg1 = cfi_parse_reg ();
587 cfi_add_CFA_same_value (reg1);
590 case CFI_return_column:
591 reg1 = cfi_parse_reg ();
592 cfi_set_return_column (reg1);
595 case DW_CFA_remember_state:
596 cfi_add_CFA_remember_state ();
599 case DW_CFA_restore_state:
600 cfi_add_CFA_restore_state ();
603 case DW_CFA_GNU_window_save:
604 cfi_add_CFA_insn (DW_CFA_GNU_window_save);
607 case CFI_signal_frame:
608 frchain_now->frch_cfi_data->cur_fde_data->signal_frame = 1;
615 demand_empty_rest_of_line ();
619 dot_cfi_escape (int ignored ATTRIBUTE_UNUSED)
621 struct cfi_escape_data *head, **tail, *e;
622 struct cfi_insn_data *insn;
624 if (frchain_now->frch_cfi_data == NULL)
626 as_bad (_("CFI instruction used without previous .cfi_startproc"));
627 ignore_rest_of_line ();
631 /* If the last address was not at the current PC, advance to current. */
632 if (symbol_get_frag (frchain_now->frch_cfi_data->last_address) != frag_now
633 || S_GET_VALUE (frchain_now->frch_cfi_data->last_address)
635 cfi_add_advance_loc (symbol_temp_new_now ());
640 e = xmalloc (sizeof (*e));
641 do_parse_cons_expression (&e->exp, 1);
645 while (*input_line_pointer++ == ',');
648 insn = alloc_cfi_insn_data ();
649 insn->insn = CFI_escape;
652 --input_line_pointer;
653 demand_empty_rest_of_line ();
657 dot_cfi_personality (int ignored ATTRIBUTE_UNUSED)
659 struct fde_entry *fde;
662 if (frchain_now->frch_cfi_data == NULL)
664 as_bad (_("CFI instruction used without previous .cfi_startproc"));
665 ignore_rest_of_line ();
669 fde = frchain_now->frch_cfi_data->cur_fde_data;
670 encoding = cfi_parse_const ();
671 if (encoding == DW_EH_PE_omit)
673 demand_empty_rest_of_line ();
674 fde->per_encoding = encoding;
678 if ((encoding & 0xff) != encoding
679 || ((encoding & 0x70) != 0
680 #if CFI_DIFF_EXPR_OK || defined tc_cfi_emit_pcrel_expr
681 && (encoding & 0x70) != DW_EH_PE_pcrel
684 /* leb128 can be handled, but does something actually need it? */
685 || (encoding & 7) == DW_EH_PE_uleb128
686 || (encoding & 7) > DW_EH_PE_udata8)
688 as_bad (_("invalid or unsupported encoding in .cfi_personality"));
689 ignore_rest_of_line ();
693 if (*input_line_pointer++ != ',')
695 as_bad (_(".cfi_personality requires encoding and symbol arguments"));
696 ignore_rest_of_line ();
700 expression_and_evaluate (&fde->personality);
701 switch (fde->personality.X_op)
706 if ((encoding & 0x70) == DW_EH_PE_pcrel)
707 encoding = DW_EH_PE_omit;
710 encoding = DW_EH_PE_omit;
714 fde->per_encoding = encoding;
716 if (encoding == DW_EH_PE_omit)
718 as_bad (_("wrong second argument to .cfi_personality"));
719 ignore_rest_of_line ();
723 demand_empty_rest_of_line ();
727 dot_cfi_lsda (int ignored ATTRIBUTE_UNUSED)
729 struct fde_entry *fde;
732 if (frchain_now->frch_cfi_data == NULL)
734 as_bad (_("CFI instruction used without previous .cfi_startproc"));
735 ignore_rest_of_line ();
739 fde = frchain_now->frch_cfi_data->cur_fde_data;
740 encoding = cfi_parse_const ();
741 if (encoding == DW_EH_PE_omit)
743 demand_empty_rest_of_line ();
744 fde->lsda_encoding = encoding;
748 if ((encoding & 0xff) != encoding
749 || ((encoding & 0x70) != 0
750 #if CFI_DIFF_EXPR_OK || defined tc_cfi_emit_pcrel_expr
751 && (encoding & 0x70) != DW_EH_PE_pcrel
754 /* leb128 can be handled, but does something actually need it? */
755 || (encoding & 7) == DW_EH_PE_uleb128
756 || (encoding & 7) > DW_EH_PE_udata8)
758 as_bad (_("invalid or unsupported encoding in .cfi_lsda"));
759 ignore_rest_of_line ();
763 if (*input_line_pointer++ != ',')
765 as_bad (_(".cfi_lsda requires encoding and symbol arguments"));
766 ignore_rest_of_line ();
770 fde->lsda_encoding = encoding;
772 expression_and_evaluate (&fde->lsda);
773 switch (fde->lsda.X_op)
778 if ((encoding & 0x70) == DW_EH_PE_pcrel)
779 encoding = DW_EH_PE_omit;
782 encoding = DW_EH_PE_omit;
786 fde->lsda_encoding = encoding;
788 if (encoding == DW_EH_PE_omit)
790 as_bad (_("wrong second argument to .cfi_lsda"));
791 ignore_rest_of_line ();
795 demand_empty_rest_of_line ();
799 dot_cfi_val_encoded_addr (int ignored ATTRIBUTE_UNUSED)
801 struct cfi_insn_data *insn_ptr;
804 if (frchain_now->frch_cfi_data == NULL)
806 as_bad (_("CFI instruction used without previous .cfi_startproc"));
807 ignore_rest_of_line ();
811 /* If the last address was not at the current PC, advance to current. */
812 if (symbol_get_frag (frchain_now->frch_cfi_data->last_address) != frag_now
813 || S_GET_VALUE (frchain_now->frch_cfi_data->last_address)
815 cfi_add_advance_loc (symbol_temp_new_now ());
817 insn_ptr = alloc_cfi_insn_data ();
818 insn_ptr->insn = CFI_val_encoded_addr;
820 insn_ptr->u.ea.reg = cfi_parse_reg ();
822 cfi_parse_separator ();
823 encoding = cfi_parse_const ();
824 if ((encoding & 0xff) != encoding
825 || ((encoding & 0x70) != 0
826 #if CFI_DIFF_EXPR_OK || defined tc_cfi_emit_pcrel_expr
827 && (encoding & 0x70) != DW_EH_PE_pcrel
830 /* leb128 can be handled, but does something actually need it? */
831 || (encoding & 7) == DW_EH_PE_uleb128
832 || (encoding & 7) > DW_EH_PE_udata8)
834 as_bad (_("invalid or unsupported encoding in .cfi_lsda"));
835 encoding = DW_EH_PE_omit;
838 cfi_parse_separator ();
839 expression_and_evaluate (&insn_ptr->u.ea.exp);
840 switch (insn_ptr->u.ea.exp.X_op)
845 if ((encoding & 0x70) != DW_EH_PE_pcrel)
848 encoding = DW_EH_PE_omit;
852 insn_ptr->u.ea.encoding = encoding;
853 if (encoding == DW_EH_PE_omit)
855 as_bad (_("wrong third argument to .cfi_val_encoded_addr"));
856 ignore_rest_of_line ();
860 demand_empty_rest_of_line ();
864 dot_cfi_startproc (int ignored ATTRIBUTE_UNUSED)
868 if (frchain_now->frch_cfi_data != NULL)
870 as_bad (_("previous CFI entry not closed (missing .cfi_endproc)"));
871 ignore_rest_of_line ();
875 cfi_new_fde (symbol_temp_new_now ());
878 if (is_name_beginner (*input_line_pointer))
882 name = input_line_pointer;
883 c = get_symbol_end ();
885 if (strcmp (name, "simple") == 0)
888 *input_line_pointer = c;
891 input_line_pointer = name;
893 demand_empty_rest_of_line ();
895 frchain_now->frch_cfi_data->cur_cfa_offset = 0;
897 tc_cfi_frame_initial_instructions ();
901 dot_cfi_endproc (int ignored ATTRIBUTE_UNUSED)
903 if (frchain_now->frch_cfi_data == NULL)
905 as_bad (_(".cfi_endproc without corresponding .cfi_startproc"));
906 ignore_rest_of_line ();
910 cfi_end_fde (symbol_temp_new_now ());
912 demand_empty_rest_of_line ();
916 /* Emit a single byte into the current segment. */
921 FRAG_APPEND_1_CHAR (byte);
924 /* Emit a two-byte word into the current segment. */
929 md_number_to_chars (frag_more (2), data, 2);
932 /* Emit a four byte word into the current segment. */
937 md_number_to_chars (frag_more (4), data, 4);
940 /* Emit an unsigned "little-endian base 128" number. */
943 out_uleb128 (addressT value)
945 output_leb128 (frag_more (sizeof_leb128 (value, 0)), value, 0);
948 /* Emit an unsigned "little-endian base 128" number. */
951 out_sleb128 (offsetT value)
953 output_leb128 (frag_more (sizeof_leb128 (value, 1)), value, 1);
957 output_cfi_insn (struct cfi_insn_data *insn)
964 case DW_CFA_advance_loc:
966 symbolS *from = insn->u.ll.lab1;
967 symbolS *to = insn->u.ll.lab2;
969 if (symbol_get_frag (to) == symbol_get_frag (from))
971 addressT delta = S_GET_VALUE (to) - S_GET_VALUE (from);
972 addressT scaled = delta / DWARF2_LINE_MIN_INSN_LENGTH;
975 out_one (DW_CFA_advance_loc + scaled);
976 else if (scaled <= 0xFF)
978 out_one (DW_CFA_advance_loc1);
981 else if (scaled <= 0xFFFF)
983 out_one (DW_CFA_advance_loc2);
988 out_one (DW_CFA_advance_loc4);
996 exp.X_op = O_subtract;
997 exp.X_add_symbol = to;
998 exp.X_op_symbol = from;
999 exp.X_add_number = 0;
1001 /* The code in ehopt.c expects that one byte of the encoding
1002 is already allocated to the frag. This comes from the way
1003 that it scans the .eh_frame section looking first for the
1004 .byte DW_CFA_advance_loc4. */
1005 *frag_more (1) = DW_CFA_advance_loc4;
1007 frag_var (rs_cfa, 4, 0, DWARF2_LINE_MIN_INSN_LENGTH << 3,
1008 make_expr_symbol (&exp), frag_now_fix () - 1,
1014 case DW_CFA_def_cfa:
1015 offset = insn->u.ri.offset;
1018 out_one (DW_CFA_def_cfa_sf);
1019 out_uleb128 (insn->u.ri.reg);
1020 out_sleb128 (offset / DWARF2_CIE_DATA_ALIGNMENT);
1024 out_one (DW_CFA_def_cfa);
1025 out_uleb128 (insn->u.ri.reg);
1026 out_uleb128 (offset);
1030 case DW_CFA_def_cfa_register:
1031 case DW_CFA_undefined:
1032 case DW_CFA_same_value:
1033 out_one (insn->insn);
1034 out_uleb128 (insn->u.r);
1037 case DW_CFA_def_cfa_offset:
1041 out_one (DW_CFA_def_cfa_offset_sf);
1042 out_sleb128 (offset / DWARF2_CIE_DATA_ALIGNMENT);
1046 out_one (DW_CFA_def_cfa_offset);
1047 out_uleb128 (offset);
1051 case DW_CFA_restore:
1055 out_one (DW_CFA_restore + regno);
1059 out_one (DW_CFA_restore_extended);
1060 out_uleb128 (regno);
1065 regno = insn->u.ri.reg;
1066 offset = insn->u.ri.offset / DWARF2_CIE_DATA_ALIGNMENT;
1069 out_one (DW_CFA_offset_extended_sf);
1070 out_uleb128 (regno);
1071 out_sleb128 (offset);
1073 else if (regno <= 0x3F)
1075 out_one (DW_CFA_offset + regno);
1076 out_uleb128 (offset);
1080 out_one (DW_CFA_offset_extended);
1081 out_uleb128 (regno);
1082 out_uleb128 (offset);
1086 case DW_CFA_register:
1087 out_one (DW_CFA_register);
1088 out_uleb128 (insn->u.rr.reg1);
1089 out_uleb128 (insn->u.rr.reg2);
1092 case DW_CFA_remember_state:
1093 case DW_CFA_restore_state:
1094 out_one (insn->insn);
1097 case DW_CFA_GNU_window_save:
1098 out_one (DW_CFA_GNU_window_save);
1103 struct cfi_escape_data *e;
1104 for (e = insn->u.esc; e ; e = e->next)
1105 emit_expr (&e->exp, 1);
1109 case CFI_val_encoded_addr:
1111 unsigned encoding = insn->u.ea.encoding;
1112 offsetT encoding_size;
1114 if (encoding == DW_EH_PE_omit)
1116 out_one (DW_CFA_val_expression);
1117 out_uleb128 (insn->u.ea.reg);
1119 switch (encoding & 0x7)
1121 case DW_EH_PE_absptr:
1122 encoding_size = DWARF2_ADDR_SIZE (stdoutput);
1124 case DW_EH_PE_udata2:
1127 case DW_EH_PE_udata4:
1130 case DW_EH_PE_udata8:
1137 /* If the user has requested absolute encoding,
1138 then use the smaller DW_OP_addr encoding. */
1139 if (insn->u.ea.encoding == DW_EH_PE_absptr)
1141 out_uleb128 (1 + encoding_size);
1142 out_one (DW_OP_addr);
1146 out_uleb128 (1 + 1 + encoding_size);
1147 out_one (DW_OP_GNU_encoded_addr);
1150 if ((encoding & 0x70) == DW_EH_PE_pcrel)
1152 #if CFI_DIFF_EXPR_OK
1153 insn->u.ea.exp.X_op = O_subtract;
1154 insn->u.ea.exp.X_op_symbol = symbol_temp_new_now ();
1155 #elif defined (tc_cfi_emit_pcrel_expr)
1156 tc_cfi_emit_pcrel_expr (&insn->u.ea.exp, encoding_size);
1163 emit_expr (&insn->u.ea.exp, encoding_size);
1173 encoding_size (unsigned char encoding)
1175 if (encoding == DW_EH_PE_omit)
1177 switch (encoding & 0x7)
1180 return bfd_get_arch_size (stdoutput) == 64 ? 8 : 4;
1181 case DW_EH_PE_udata2:
1183 case DW_EH_PE_udata4:
1185 case DW_EH_PE_udata8:
1193 output_cie (struct cie_entry *cie)
1195 symbolS *after_size_address, *end_address;
1197 struct cfi_insn_data *i;
1198 offsetT augmentation_size;
1201 cie->start_address = symbol_temp_new_now ();
1202 after_size_address = symbol_temp_make ();
1203 end_address = symbol_temp_make ();
1205 exp.X_op = O_subtract;
1206 exp.X_add_symbol = end_address;
1207 exp.X_op_symbol = after_size_address;
1208 exp.X_add_number = 0;
1210 emit_expr (&exp, 4); /* Length. */
1211 symbol_set_value_now (after_size_address);
1212 out_four (0); /* CIE id. */
1213 out_one (DW_CIE_VERSION); /* Version. */
1214 out_one ('z'); /* Augmentation. */
1215 if (cie->per_encoding != DW_EH_PE_omit)
1217 if (cie->lsda_encoding != DW_EH_PE_omit)
1220 if (cie->signal_frame)
1223 out_uleb128 (DWARF2_LINE_MIN_INSN_LENGTH); /* Code alignment. */
1224 out_sleb128 (DWARF2_CIE_DATA_ALIGNMENT); /* Data alignment. */
1225 if (DW_CIE_VERSION == 1) /* Return column. */
1226 out_one (cie->return_column);
1228 out_uleb128 (cie->return_column);
1229 augmentation_size = 1 + (cie->lsda_encoding != DW_EH_PE_omit);
1230 if (cie->per_encoding != DW_EH_PE_omit)
1231 augmentation_size += 1 + encoding_size (cie->per_encoding);
1232 out_uleb128 (augmentation_size); /* Augmentation size. */
1233 if (cie->per_encoding != DW_EH_PE_omit)
1235 offsetT size = encoding_size (cie->per_encoding);
1236 out_one (cie->per_encoding);
1237 exp = cie->personality;
1238 if ((cie->per_encoding & 0x70) == DW_EH_PE_pcrel)
1240 #if CFI_DIFF_EXPR_OK
1241 exp.X_op = O_subtract;
1242 exp.X_op_symbol = symbol_temp_new_now ();
1243 emit_expr (&exp, size);
1244 #elif defined (tc_cfi_emit_pcrel_expr)
1245 tc_cfi_emit_pcrel_expr (&exp, size);
1251 emit_expr (&exp, size);
1253 if (cie->lsda_encoding != DW_EH_PE_omit)
1254 out_one (cie->lsda_encoding);
1256 switch (DWARF2_FDE_RELOC_SIZE)
1259 enc = DW_EH_PE_sdata2;
1262 enc = DW_EH_PE_sdata4;
1265 enc = DW_EH_PE_sdata8;
1270 #if CFI_DIFF_EXPR_OK || defined tc_cfi_emit_pcrel_expr
1271 enc |= DW_EH_PE_pcrel;
1276 for (i = cie->first; i != cie->last; i = i->next)
1277 output_cfi_insn (i);
1279 frag_align (2, DW_CFA_nop, 0);
1280 symbol_set_value_now (end_address);
1284 output_fde (struct fde_entry *fde, struct cie_entry *cie,
1285 struct cfi_insn_data *first, int align)
1287 symbolS *after_size_address, *end_address;
1289 offsetT augmentation_size;
1291 after_size_address = symbol_temp_make ();
1292 end_address = symbol_temp_make ();
1294 exp.X_op = O_subtract;
1295 exp.X_add_symbol = end_address;
1296 exp.X_op_symbol = after_size_address;
1297 exp.X_add_number = 0;
1298 emit_expr (&exp, 4); /* Length. */
1299 symbol_set_value_now (after_size_address);
1301 exp.X_add_symbol = after_size_address;
1302 exp.X_op_symbol = cie->start_address;
1303 emit_expr (&exp, 4); /* CIE offset. */
1305 #if CFI_DIFF_EXPR_OK
1306 exp.X_add_symbol = fde->start_address;
1307 exp.X_op_symbol = symbol_temp_new_now ();
1308 emit_expr (&exp, DWARF2_FDE_RELOC_SIZE); /* Code offset. */
1310 exp.X_op = O_symbol;
1311 exp.X_add_symbol = fde->start_address;
1312 exp.X_op_symbol = NULL;
1313 #ifdef tc_cfi_emit_pcrel_expr
1314 tc_cfi_emit_pcrel_expr (&exp, DWARF2_FDE_RELOC_SIZE); /* Code offset. */
1316 emit_expr (&exp, DWARF2_FDE_RELOC_SIZE); /* Code offset. */
1318 exp.X_op = O_subtract;
1321 exp.X_add_symbol = fde->end_address;
1322 exp.X_op_symbol = fde->start_address; /* Code length. */
1323 emit_expr (&exp, DWARF2_FDE_RELOC_SIZE);
1325 augmentation_size = encoding_size (fde->lsda_encoding);
1326 out_uleb128 (augmentation_size); /* Augmentation size. */
1328 if (fde->lsda_encoding != DW_EH_PE_omit)
1331 if ((fde->lsda_encoding & 0x70) == DW_EH_PE_pcrel)
1333 #if CFI_DIFF_EXPR_OK
1334 exp.X_op = O_subtract;
1335 exp.X_op_symbol = symbol_temp_new_now ();
1336 emit_expr (&exp, augmentation_size);
1337 #elif defined (tc_cfi_emit_pcrel_expr)
1338 tc_cfi_emit_pcrel_expr (&exp, augmentation_size);
1344 emit_expr (&exp, augmentation_size);
1347 for (; first; first = first->next)
1348 output_cfi_insn (first);
1350 frag_align (align, DW_CFA_nop, 0);
1351 symbol_set_value_now (end_address);
1354 static struct cie_entry *
1355 select_cie_for_fde (struct fde_entry *fde, struct cfi_insn_data **pfirst)
1357 struct cfi_insn_data *i, *j;
1358 struct cie_entry *cie;
1360 for (cie = cie_root; cie; cie = cie->next)
1362 if (cie->return_column != fde->return_column
1363 || cie->signal_frame != fde->signal_frame
1364 || cie->per_encoding != fde->per_encoding
1365 || cie->lsda_encoding != fde->lsda_encoding)
1367 if (cie->per_encoding != DW_EH_PE_omit)
1369 if (cie->personality.X_op != fde->personality.X_op
1370 || cie->personality.X_add_number
1371 != fde->personality.X_add_number)
1373 switch (cie->personality.X_op)
1376 if (cie->personality.X_unsigned != fde->personality.X_unsigned)
1380 if (cie->personality.X_add_symbol
1381 != fde->personality.X_add_symbol)
1388 for (i = cie->first, j = fde->data;
1389 i != cie->last && j != NULL;
1390 i = i->next, j = j->next)
1392 if (i->insn != j->insn)
1396 case DW_CFA_advance_loc:
1397 case DW_CFA_remember_state:
1398 /* We reached the first advance/remember in the FDE,
1399 but did not reach the end of the CIE list. */
1403 case DW_CFA_def_cfa:
1404 if (i->u.ri.reg != j->u.ri.reg)
1406 if (i->u.ri.offset != j->u.ri.offset)
1410 case DW_CFA_register:
1411 if (i->u.rr.reg1 != j->u.rr.reg1)
1413 if (i->u.rr.reg2 != j->u.rr.reg2)
1417 case DW_CFA_def_cfa_register:
1418 case DW_CFA_restore:
1419 case DW_CFA_undefined:
1420 case DW_CFA_same_value:
1421 if (i->u.r != j->u.r)
1425 case DW_CFA_def_cfa_offset:
1426 if (i->u.i != j->u.i)
1431 case CFI_val_encoded_addr:
1432 /* Don't bother matching these for now. */
1440 /* Success if we reached the end of the CIE list, and we've either
1441 run out of FDE entries or we've encountered an advance,
1442 remember, or escape. */
1445 || j->insn == DW_CFA_advance_loc
1446 || j->insn == DW_CFA_remember_state
1447 || j->insn == CFI_escape
1448 || j->insn == CFI_val_encoded_addr))
1457 cie = xmalloc (sizeof (struct cie_entry));
1458 cie->next = cie_root;
1460 cie->return_column = fde->return_column;
1461 cie->signal_frame = fde->signal_frame;
1462 cie->per_encoding = fde->per_encoding;
1463 cie->lsda_encoding = fde->lsda_encoding;
1464 cie->personality = fde->personality;
1465 cie->first = fde->data;
1467 for (i = cie->first; i ; i = i->next)
1468 if (i->insn == DW_CFA_advance_loc
1469 || i->insn == DW_CFA_remember_state
1470 || i->insn == CFI_escape
1471 || i->insn == CFI_val_encoded_addr)
1486 struct fde_entry *fde;
1487 int save_flag_traditional_format;
1489 if (all_fde_data == 0)
1492 /* Open .eh_frame section. */
1493 cfi_seg = subseg_new (".eh_frame", 0);
1494 bfd_set_section_flags (stdoutput, cfi_seg,
1495 SEC_ALLOC | SEC_LOAD | SEC_DATA
1496 | DWARF2_EH_FRAME_READ_ONLY);
1497 subseg_set (cfi_seg, 0);
1498 record_alignment (cfi_seg, EH_FRAME_ALIGNMENT);
1500 #ifdef md_fix_up_eh_frame
1501 md_fix_up_eh_frame (cfi_seg);
1504 /* Make sure check_eh_frame doesn't do anything with our output. */
1505 save_flag_traditional_format = flag_traditional_format;
1506 flag_traditional_format = 1;
1508 for (fde = all_fde_data; fde ; fde = fde->next)
1510 struct cfi_insn_data *first;
1511 struct cie_entry *cie;
1513 if (fde->end_address == NULL)
1515 as_bad (_("open CFI at the end of file; missing .cfi_endproc directive"));
1516 fde->end_address = fde->start_address;
1519 cie = select_cie_for_fde (fde, &first);
1520 output_fde (fde, cie, first, fde->next == NULL ? EH_FRAME_ALIGNMENT : 2);
1523 flag_traditional_format = save_flag_traditional_format;
1526 #else /* TARGET_USE_CFIPOP */
1531 #endif /* TARGET_USE_CFIPOP */