1 /* dw2gencfi.c - Support for generating Dwarf2 CFI information.
2 Copyright 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
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"
26 #ifdef TARGET_USE_CFIPOP
28 /* By default, use difference expressions if DIFF_EXPR_OK is defined. */
29 #ifndef CFI_DIFF_EXPR_OK
31 # define CFI_DIFF_EXPR_OK 1
33 # define CFI_DIFF_EXPR_OK 0
37 /* We re-use DWARF2_LINE_MIN_INSN_LENGTH for the code alignment field
38 of the CIE. Default to 1 if not otherwise specified. */
39 #ifndef DWARF2_LINE_MIN_INSN_LENGTH
40 # define DWARF2_LINE_MIN_INSN_LENGTH 1
43 /* By default, use 32-bit relocations from .eh_frame into .text. */
44 #ifndef DWARF2_FDE_RELOC_SIZE
45 # define DWARF2_FDE_RELOC_SIZE 4
48 /* By default, use a read-only .eh_frame section. */
49 #ifndef DWARF2_EH_FRAME_READ_ONLY
50 # define DWARF2_EH_FRAME_READ_ONLY SEC_READONLY
53 #ifndef EH_FRAME_ALIGNMENT
54 # define EH_FRAME_ALIGNMENT (bfd_get_arch_size (stdoutput) == 64 ? 3 : 2)
57 #ifndef tc_cfi_frame_initial_instructions
58 # define tc_cfi_frame_initial_instructions() ((void)0)
61 #ifndef DWARF2_ADDR_SIZE
62 # define DWARF2_ADDR_SIZE(bfd) (bfd_arch_bits_per_address (bfd) / 8)
68 struct cfi_insn_data *next;
89 struct cfi_escape_data {
90 struct cfi_escape_data *next;
95 unsigned reg, encoding;
103 struct fde_entry *next;
104 symbolS *start_address;
105 symbolS *end_address;
106 struct cfi_insn_data *data;
107 struct cfi_insn_data **last;
108 unsigned char per_encoding;
109 unsigned char lsda_encoding;
110 expressionS personality;
112 unsigned int return_column;
113 unsigned int signal_frame;
118 struct cie_entry *next;
119 symbolS *start_address;
120 unsigned int return_column;
121 unsigned int signal_frame;
122 unsigned char per_encoding;
123 unsigned char lsda_encoding;
124 expressionS personality;
125 struct cfi_insn_data *first, *last;
129 /* List of FDE entries. */
130 static struct fde_entry *all_fde_data;
131 static struct fde_entry **last_fde_data = &all_fde_data;
133 /* List of CIEs so that they could be reused. */
134 static struct cie_entry *cie_root;
136 /* Stack of old CFI data, for save/restore. */
139 struct cfa_save_data *next;
143 /* Current open FDE entry. */
146 struct fde_entry *cur_fde_data;
147 symbolS *last_address;
148 offsetT cur_cfa_offset;
149 struct cfa_save_data *cfa_save_stack;
152 /* Construct a new FDE structure and add it to the end of the fde list. */
154 static struct fde_entry *
155 alloc_fde_entry (void)
157 struct fde_entry *fde = xcalloc (1, sizeof (struct fde_entry));
159 frchain_now->frch_cfi_data = xcalloc (1, sizeof (struct frch_cfi_data));
160 frchain_now->frch_cfi_data->cur_fde_data = fde;
161 *last_fde_data = fde;
162 last_fde_data = &fde->next;
164 fde->last = &fde->data;
165 fde->return_column = DWARF2_DEFAULT_RETURN_COLUMN;
166 fde->per_encoding = DW_EH_PE_omit;
167 fde->lsda_encoding = DW_EH_PE_omit;
172 /* The following functions are available for a backend to construct its
173 own unwind information, usually from legacy unwind directives. */
175 /* Construct a new INSN structure and add it to the end of the insn list
176 for the currently active FDE. */
178 static struct cfi_insn_data *
179 alloc_cfi_insn_data (void)
181 struct cfi_insn_data *insn = xcalloc (1, sizeof (struct cfi_insn_data));
182 struct fde_entry *cur_fde_data = frchain_now->frch_cfi_data->cur_fde_data;
184 *cur_fde_data->last = insn;
185 cur_fde_data->last = &insn->next;
190 /* Construct a new FDE structure that begins at LABEL. */
193 cfi_new_fde (symbolS *label)
195 struct fde_entry *fde = alloc_fde_entry ();
196 fde->start_address = label;
197 frchain_now->frch_cfi_data->last_address = label;
200 /* End the currently open FDE. */
203 cfi_end_fde (symbolS *label)
205 frchain_now->frch_cfi_data->cur_fde_data->end_address = label;
206 free (frchain_now->frch_cfi_data);
207 frchain_now->frch_cfi_data = NULL;
210 /* Set the return column for the current FDE. */
213 cfi_set_return_column (unsigned regno)
215 frchain_now->frch_cfi_data->cur_fde_data->return_column = regno;
218 /* Universal functions to store new instructions. */
221 cfi_add_CFA_insn(int insn)
223 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
225 insn_ptr->insn = insn;
229 cfi_add_CFA_insn_reg (int insn, unsigned regno)
231 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
233 insn_ptr->insn = insn;
234 insn_ptr->u.r = regno;
238 cfi_add_CFA_insn_offset (int insn, offsetT offset)
240 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
242 insn_ptr->insn = insn;
243 insn_ptr->u.i = offset;
247 cfi_add_CFA_insn_reg_reg (int insn, unsigned reg1, unsigned reg2)
249 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
251 insn_ptr->insn = insn;
252 insn_ptr->u.rr.reg1 = reg1;
253 insn_ptr->u.rr.reg2 = reg2;
257 cfi_add_CFA_insn_reg_offset (int insn, unsigned regno, offsetT offset)
259 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
261 insn_ptr->insn = insn;
262 insn_ptr->u.ri.reg = regno;
263 insn_ptr->u.ri.offset = offset;
266 /* Add a CFI insn to advance the PC from the last address to LABEL. */
269 cfi_add_advance_loc (symbolS *label)
271 struct cfi_insn_data *insn = alloc_cfi_insn_data ();
273 insn->insn = DW_CFA_advance_loc;
274 insn->u.ll.lab1 = frchain_now->frch_cfi_data->last_address;
275 insn->u.ll.lab2 = label;
277 frchain_now->frch_cfi_data->last_address = label;
280 /* Add a DW_CFA_offset record to the CFI data. */
283 cfi_add_CFA_offset (unsigned regno, offsetT offset)
285 unsigned int abs_data_align;
287 assert (DWARF2_CIE_DATA_ALIGNMENT != 0);
288 cfi_add_CFA_insn_reg_offset (DW_CFA_offset, regno, offset);
290 abs_data_align = (DWARF2_CIE_DATA_ALIGNMENT < 0
291 ? -DWARF2_CIE_DATA_ALIGNMENT : DWARF2_CIE_DATA_ALIGNMENT);
292 if (offset % abs_data_align)
293 as_bad (_("register save offset not a multiple of %u"), abs_data_align);
296 /* Add a DW_CFA_def_cfa record to the CFI data. */
299 cfi_add_CFA_def_cfa (unsigned regno, offsetT offset)
301 cfi_add_CFA_insn_reg_offset (DW_CFA_def_cfa, regno, offset);
302 frchain_now->frch_cfi_data->cur_cfa_offset = offset;
305 /* Add a DW_CFA_register record to the CFI data. */
308 cfi_add_CFA_register (unsigned reg1, unsigned reg2)
310 cfi_add_CFA_insn_reg_reg (DW_CFA_register, reg1, reg2);
313 /* Add a DW_CFA_def_cfa_register record to the CFI data. */
316 cfi_add_CFA_def_cfa_register (unsigned regno)
318 cfi_add_CFA_insn_reg (DW_CFA_def_cfa_register, regno);
321 /* Add a DW_CFA_def_cfa_offset record to the CFI data. */
324 cfi_add_CFA_def_cfa_offset (offsetT offset)
326 cfi_add_CFA_insn_offset (DW_CFA_def_cfa_offset, offset);
327 frchain_now->frch_cfi_data->cur_cfa_offset = offset;
331 cfi_add_CFA_restore (unsigned regno)
333 cfi_add_CFA_insn_reg (DW_CFA_restore, regno);
337 cfi_add_CFA_undefined (unsigned regno)
339 cfi_add_CFA_insn_reg (DW_CFA_undefined, regno);
343 cfi_add_CFA_same_value (unsigned regno)
345 cfi_add_CFA_insn_reg (DW_CFA_same_value, regno);
349 cfi_add_CFA_remember_state (void)
351 struct cfa_save_data *p;
353 cfi_add_CFA_insn (DW_CFA_remember_state);
355 p = xmalloc (sizeof (*p));
356 p->cfa_offset = frchain_now->frch_cfi_data->cur_cfa_offset;
357 p->next = frchain_now->frch_cfi_data->cfa_save_stack;
358 frchain_now->frch_cfi_data->cfa_save_stack = p;
362 cfi_add_CFA_restore_state (void)
364 struct cfa_save_data *p;
366 cfi_add_CFA_insn (DW_CFA_restore_state);
368 p = frchain_now->frch_cfi_data->cfa_save_stack;
371 frchain_now->frch_cfi_data->cur_cfa_offset = p->cfa_offset;
372 frchain_now->frch_cfi_data->cfa_save_stack = p->next;
376 as_bad (_("CFI state restore without previous remember"));
380 /* Parse CFI assembler directives. */
382 static void dot_cfi (int);
383 static void dot_cfi_escape (int);
384 static void dot_cfi_startproc (int);
385 static void dot_cfi_endproc (int);
386 static void dot_cfi_personality (int);
387 static void dot_cfi_lsda (int);
388 static void dot_cfi_val_encoded_addr (int);
390 /* Fake CFI type; outside the byte range of any real CFI insn. */
391 #define CFI_adjust_cfa_offset 0x100
392 #define CFI_return_column 0x101
393 #define CFI_rel_offset 0x102
394 #define CFI_escape 0x103
395 #define CFI_signal_frame 0x104
396 #define CFI_val_encoded_addr 0x105
398 const pseudo_typeS cfi_pseudo_table[] =
400 { "cfi_startproc", dot_cfi_startproc, 0 },
401 { "cfi_endproc", dot_cfi_endproc, 0 },
402 { "cfi_def_cfa", dot_cfi, DW_CFA_def_cfa },
403 { "cfi_def_cfa_register", dot_cfi, DW_CFA_def_cfa_register },
404 { "cfi_def_cfa_offset", dot_cfi, DW_CFA_def_cfa_offset },
405 { "cfi_adjust_cfa_offset", dot_cfi, CFI_adjust_cfa_offset },
406 { "cfi_offset", dot_cfi, DW_CFA_offset },
407 { "cfi_rel_offset", dot_cfi, CFI_rel_offset },
408 { "cfi_register", dot_cfi, DW_CFA_register },
409 { "cfi_return_column", dot_cfi, CFI_return_column },
410 { "cfi_restore", dot_cfi, DW_CFA_restore },
411 { "cfi_undefined", dot_cfi, DW_CFA_undefined },
412 { "cfi_same_value", dot_cfi, DW_CFA_same_value },
413 { "cfi_remember_state", dot_cfi, DW_CFA_remember_state },
414 { "cfi_restore_state", dot_cfi, DW_CFA_restore_state },
415 { "cfi_window_save", dot_cfi, DW_CFA_GNU_window_save },
416 { "cfi_escape", dot_cfi_escape, 0 },
417 { "cfi_signal_frame", dot_cfi, CFI_signal_frame },
418 { "cfi_personality", dot_cfi_personality, 0 },
419 { "cfi_lsda", dot_cfi_lsda, 0 },
420 { "cfi_val_encoded_addr", dot_cfi_val_encoded_addr, 0 },
425 cfi_parse_separator (void)
428 if (*input_line_pointer == ',')
429 input_line_pointer++;
431 as_bad (_("missing separator"));
434 #ifndef tc_parse_to_dw2regnum
436 tc_parse_to_dw2regnum(expressionS *exp)
438 # ifdef tc_regname_to_dw2regnum
440 if (is_name_beginner (*input_line_pointer)
441 || (*input_line_pointer == '%'
442 && is_name_beginner (*++input_line_pointer)))
446 name = input_line_pointer;
447 c = get_symbol_end ();
449 exp->X_op = O_constant;
450 exp->X_add_number = tc_regname_to_dw2regnum (name);
452 *input_line_pointer = c;
456 expression_and_evaluate (exp);
466 tc_parse_to_dw2regnum (&exp);
471 regno = exp.X_add_number;
481 as_bad (_("bad register expression"));
489 cfi_parse_const (void)
491 return get_absolute_expression ();
500 if (frchain_now->frch_cfi_data == NULL)
502 as_bad (_("CFI instruction used without previous .cfi_startproc"));
503 ignore_rest_of_line ();
507 /* If the last address was not at the current PC, advance to current. */
508 if (symbol_get_frag (frchain_now->frch_cfi_data->last_address) != frag_now
509 || S_GET_VALUE (frchain_now->frch_cfi_data->last_address)
511 cfi_add_advance_loc (symbol_temp_new_now ());
516 reg1 = cfi_parse_reg ();
517 cfi_parse_separator ();
518 offset = cfi_parse_const ();
519 cfi_add_CFA_offset (reg1, offset);
523 reg1 = cfi_parse_reg ();
524 cfi_parse_separator ();
525 offset = cfi_parse_const ();
526 cfi_add_CFA_offset (reg1,
527 offset - frchain_now->frch_cfi_data->cur_cfa_offset);
531 reg1 = cfi_parse_reg ();
532 cfi_parse_separator ();
533 offset = cfi_parse_const ();
534 cfi_add_CFA_def_cfa (reg1, offset);
537 case DW_CFA_register:
538 reg1 = cfi_parse_reg ();
539 cfi_parse_separator ();
540 reg2 = cfi_parse_reg ();
541 cfi_add_CFA_register (reg1, reg2);
544 case DW_CFA_def_cfa_register:
545 reg1 = cfi_parse_reg ();
546 cfi_add_CFA_def_cfa_register (reg1);
549 case DW_CFA_def_cfa_offset:
550 offset = cfi_parse_const ();
551 cfi_add_CFA_def_cfa_offset (offset);
554 case CFI_adjust_cfa_offset:
555 offset = cfi_parse_const ();
556 cfi_add_CFA_def_cfa_offset (frchain_now->frch_cfi_data->cur_cfa_offset
563 reg1 = cfi_parse_reg ();
564 cfi_add_CFA_restore (reg1);
566 if (*input_line_pointer != ',')
568 ++input_line_pointer;
572 case DW_CFA_undefined:
575 reg1 = cfi_parse_reg ();
576 cfi_add_CFA_undefined (reg1);
578 if (*input_line_pointer != ',')
580 ++input_line_pointer;
584 case DW_CFA_same_value:
585 reg1 = cfi_parse_reg ();
586 cfi_add_CFA_same_value (reg1);
589 case CFI_return_column:
590 reg1 = cfi_parse_reg ();
591 cfi_set_return_column (reg1);
594 case DW_CFA_remember_state:
595 cfi_add_CFA_remember_state ();
598 case DW_CFA_restore_state:
599 cfi_add_CFA_restore_state ();
602 case DW_CFA_GNU_window_save:
603 cfi_add_CFA_insn (DW_CFA_GNU_window_save);
606 case CFI_signal_frame:
607 frchain_now->frch_cfi_data->cur_fde_data->signal_frame = 1;
614 demand_empty_rest_of_line ();
618 dot_cfi_escape (int ignored ATTRIBUTE_UNUSED)
620 struct cfi_escape_data *head, **tail, *e;
621 struct cfi_insn_data *insn;
623 if (frchain_now->frch_cfi_data == NULL)
625 as_bad (_("CFI instruction used without previous .cfi_startproc"));
626 ignore_rest_of_line ();
630 /* If the last address was not at the current PC, advance to current. */
631 if (symbol_get_frag (frchain_now->frch_cfi_data->last_address) != frag_now
632 || S_GET_VALUE (frchain_now->frch_cfi_data->last_address)
634 cfi_add_advance_loc (symbol_temp_new_now ());
639 e = xmalloc (sizeof (*e));
640 do_parse_cons_expression (&e->exp, 1);
644 while (*input_line_pointer++ == ',');
647 insn = alloc_cfi_insn_data ();
648 insn->insn = CFI_escape;
651 --input_line_pointer;
652 demand_empty_rest_of_line ();
656 dot_cfi_personality (int ignored ATTRIBUTE_UNUSED)
658 struct fde_entry *fde;
661 if (frchain_now->frch_cfi_data == NULL)
663 as_bad (_("CFI instruction used without previous .cfi_startproc"));
664 ignore_rest_of_line ();
668 fde = frchain_now->frch_cfi_data->cur_fde_data;
669 encoding = cfi_parse_const ();
670 if (encoding == DW_EH_PE_omit)
672 demand_empty_rest_of_line ();
673 fde->per_encoding = encoding;
677 if ((encoding & 0xff) != encoding
678 || ((encoding & 0x70) != 0
679 #if CFI_DIFF_EXPR_OK || defined tc_cfi_emit_pcrel_expr
680 && (encoding & 0x70) != DW_EH_PE_pcrel
683 /* leb128 can be handled, but does something actually need it? */
684 || (encoding & 7) == DW_EH_PE_uleb128
685 || (encoding & 7) > DW_EH_PE_udata8)
687 as_bad (_("invalid or unsupported encoding in .cfi_personality"));
688 ignore_rest_of_line ();
692 if (*input_line_pointer++ != ',')
694 as_bad (_(".cfi_personality requires encoding and symbol arguments"));
695 ignore_rest_of_line ();
699 expression_and_evaluate (&fde->personality);
700 switch (fde->personality.X_op)
705 if ((encoding & 0x70) == DW_EH_PE_pcrel)
706 encoding = DW_EH_PE_omit;
709 encoding = DW_EH_PE_omit;
713 fde->per_encoding = encoding;
715 if (encoding == DW_EH_PE_omit)
717 as_bad (_("wrong second argument to .cfi_personality"));
718 ignore_rest_of_line ();
722 demand_empty_rest_of_line ();
726 dot_cfi_lsda (int ignored ATTRIBUTE_UNUSED)
728 struct fde_entry *fde;
731 if (frchain_now->frch_cfi_data == NULL)
733 as_bad (_("CFI instruction used without previous .cfi_startproc"));
734 ignore_rest_of_line ();
738 fde = frchain_now->frch_cfi_data->cur_fde_data;
739 encoding = cfi_parse_const ();
740 if (encoding == DW_EH_PE_omit)
742 demand_empty_rest_of_line ();
743 fde->lsda_encoding = encoding;
747 if ((encoding & 0xff) != encoding
748 || ((encoding & 0x70) != 0
749 #if CFI_DIFF_EXPR_OK || defined tc_cfi_emit_pcrel_expr
750 && (encoding & 0x70) != DW_EH_PE_pcrel
753 /* leb128 can be handled, but does something actually need it? */
754 || (encoding & 7) == DW_EH_PE_uleb128
755 || (encoding & 7) > DW_EH_PE_udata8)
757 as_bad (_("invalid or unsupported encoding in .cfi_lsda"));
758 ignore_rest_of_line ();
762 if (*input_line_pointer++ != ',')
764 as_bad (_(".cfi_lsda requires encoding and symbol arguments"));
765 ignore_rest_of_line ();
769 fde->lsda_encoding = encoding;
771 expression_and_evaluate (&fde->lsda);
772 switch (fde->lsda.X_op)
777 if ((encoding & 0x70) == DW_EH_PE_pcrel)
778 encoding = DW_EH_PE_omit;
781 encoding = DW_EH_PE_omit;
785 fde->lsda_encoding = encoding;
787 if (encoding == DW_EH_PE_omit)
789 as_bad (_("wrong second argument to .cfi_lsda"));
790 ignore_rest_of_line ();
794 demand_empty_rest_of_line ();
798 dot_cfi_val_encoded_addr (int ignored ATTRIBUTE_UNUSED)
800 struct cfi_insn_data *insn_ptr;
803 if (frchain_now->frch_cfi_data == NULL)
805 as_bad (_("CFI instruction used without previous .cfi_startproc"));
806 ignore_rest_of_line ();
810 /* If the last address was not at the current PC, advance to current. */
811 if (symbol_get_frag (frchain_now->frch_cfi_data->last_address) != frag_now
812 || S_GET_VALUE (frchain_now->frch_cfi_data->last_address)
814 cfi_add_advance_loc (symbol_temp_new_now ());
816 insn_ptr = alloc_cfi_insn_data ();
817 insn_ptr->insn = CFI_val_encoded_addr;
819 insn_ptr->u.ea.reg = cfi_parse_reg ();
821 cfi_parse_separator ();
822 encoding = cfi_parse_const ();
823 if ((encoding & 0xff) != encoding
824 || ((encoding & 0x70) != 0
825 #if CFI_DIFF_EXPR_OK || defined tc_cfi_emit_pcrel_expr
826 && (encoding & 0x70) != DW_EH_PE_pcrel
829 /* leb128 can be handled, but does something actually need it? */
830 || (encoding & 7) == DW_EH_PE_uleb128
831 || (encoding & 7) > DW_EH_PE_udata8)
833 as_bad (_("invalid or unsupported encoding in .cfi_lsda"));
834 encoding = DW_EH_PE_omit;
837 cfi_parse_separator ();
838 expression_and_evaluate (&insn_ptr->u.ea.exp);
839 switch (insn_ptr->u.ea.exp.X_op)
844 if ((encoding & 0x70) != DW_EH_PE_pcrel)
847 encoding = DW_EH_PE_omit;
851 insn_ptr->u.ea.encoding = encoding;
852 if (encoding == DW_EH_PE_omit)
854 as_bad (_("wrong third argument to .cfi_val_encoded_addr"));
855 ignore_rest_of_line ();
859 demand_empty_rest_of_line ();
863 dot_cfi_startproc (int ignored ATTRIBUTE_UNUSED)
867 if (frchain_now->frch_cfi_data != NULL)
869 as_bad (_("previous CFI entry not closed (missing .cfi_endproc)"));
870 ignore_rest_of_line ();
874 cfi_new_fde (symbol_temp_new_now ());
877 if (is_name_beginner (*input_line_pointer))
881 name = input_line_pointer;
882 c = get_symbol_end ();
884 if (strcmp (name, "simple") == 0)
887 *input_line_pointer = c;
890 input_line_pointer = name;
892 demand_empty_rest_of_line ();
894 frchain_now->frch_cfi_data->cur_cfa_offset = 0;
896 tc_cfi_frame_initial_instructions ();
900 dot_cfi_endproc (int ignored ATTRIBUTE_UNUSED)
902 if (frchain_now->frch_cfi_data == NULL)
904 as_bad (_(".cfi_endproc without corresponding .cfi_startproc"));
905 ignore_rest_of_line ();
909 cfi_end_fde (symbol_temp_new_now ());
911 demand_empty_rest_of_line ();
915 /* Emit a single byte into the current segment. */
920 FRAG_APPEND_1_CHAR (byte);
923 /* Emit a two-byte word into the current segment. */
928 md_number_to_chars (frag_more (2), data, 2);
931 /* Emit a four byte word into the current segment. */
936 md_number_to_chars (frag_more (4), data, 4);
939 /* Emit an unsigned "little-endian base 128" number. */
942 out_uleb128 (addressT value)
944 output_leb128 (frag_more (sizeof_leb128 (value, 0)), value, 0);
947 /* Emit an unsigned "little-endian base 128" number. */
950 out_sleb128 (offsetT value)
952 output_leb128 (frag_more (sizeof_leb128 (value, 1)), value, 1);
956 output_cfi_insn (struct cfi_insn_data *insn)
963 case DW_CFA_advance_loc:
965 symbolS *from = insn->u.ll.lab1;
966 symbolS *to = insn->u.ll.lab2;
968 if (symbol_get_frag (to) == symbol_get_frag (from))
970 addressT delta = S_GET_VALUE (to) - S_GET_VALUE (from);
971 addressT scaled = delta / DWARF2_LINE_MIN_INSN_LENGTH;
974 out_one (DW_CFA_advance_loc + scaled);
975 else if (scaled <= 0xFF)
977 out_one (DW_CFA_advance_loc1);
980 else if (scaled <= 0xFFFF)
982 out_one (DW_CFA_advance_loc2);
987 out_one (DW_CFA_advance_loc4);
995 exp.X_op = O_subtract;
996 exp.X_add_symbol = to;
997 exp.X_op_symbol = from;
998 exp.X_add_number = 0;
1000 /* The code in ehopt.c expects that one byte of the encoding
1001 is already allocated to the frag. This comes from the way
1002 that it scans the .eh_frame section looking first for the
1003 .byte DW_CFA_advance_loc4. */
1006 frag_var (rs_cfa, 4, 0, DWARF2_LINE_MIN_INSN_LENGTH << 3,
1007 make_expr_symbol (&exp), frag_now_fix () - 1,
1013 case DW_CFA_def_cfa:
1014 offset = insn->u.ri.offset;
1017 out_one (DW_CFA_def_cfa_sf);
1018 out_uleb128 (insn->u.ri.reg);
1019 out_sleb128 (offset / DWARF2_CIE_DATA_ALIGNMENT);
1023 out_one (DW_CFA_def_cfa);
1024 out_uleb128 (insn->u.ri.reg);
1025 out_uleb128 (offset);
1029 case DW_CFA_def_cfa_register:
1030 case DW_CFA_undefined:
1031 case DW_CFA_same_value:
1032 out_one (insn->insn);
1033 out_uleb128 (insn->u.r);
1036 case DW_CFA_def_cfa_offset:
1040 out_one (DW_CFA_def_cfa_offset_sf);
1041 out_sleb128 (offset / DWARF2_CIE_DATA_ALIGNMENT);
1045 out_one (DW_CFA_def_cfa_offset);
1046 out_uleb128 (offset);
1050 case DW_CFA_restore:
1054 out_one (DW_CFA_restore + regno);
1058 out_one (DW_CFA_restore_extended);
1059 out_uleb128 (regno);
1064 regno = insn->u.ri.reg;
1065 offset = insn->u.ri.offset / DWARF2_CIE_DATA_ALIGNMENT;
1068 out_one (DW_CFA_offset_extended_sf);
1069 out_uleb128 (regno);
1070 out_sleb128 (offset);
1072 else if (regno <= 0x3F)
1074 out_one (DW_CFA_offset + regno);
1075 out_uleb128 (offset);
1079 out_one (DW_CFA_offset_extended);
1080 out_uleb128 (regno);
1081 out_uleb128 (offset);
1085 case DW_CFA_register:
1086 out_one (DW_CFA_register);
1087 out_uleb128 (insn->u.rr.reg1);
1088 out_uleb128 (insn->u.rr.reg2);
1091 case DW_CFA_remember_state:
1092 case DW_CFA_restore_state:
1093 out_one (insn->insn);
1096 case DW_CFA_GNU_window_save:
1097 out_one (DW_CFA_GNU_window_save);
1102 struct cfi_escape_data *e;
1103 for (e = insn->u.esc; e ; e = e->next)
1104 emit_expr (&e->exp, 1);
1108 case CFI_val_encoded_addr:
1110 unsigned encoding = insn->u.ea.encoding;
1111 offsetT encoding_size;
1113 if (encoding == DW_EH_PE_omit)
1115 out_one (DW_CFA_val_expression);
1116 out_uleb128 (insn->u.ea.reg);
1118 switch (encoding & 0x7)
1120 case DW_EH_PE_absptr:
1121 encoding_size = DWARF2_ADDR_SIZE (stdoutput);
1123 case DW_EH_PE_udata2:
1126 case DW_EH_PE_udata4:
1129 case DW_EH_PE_udata8:
1136 /* If the user has requested absolute encoding,
1137 then use the smaller DW_OP_addr encoding. */
1138 if (insn->u.ea.encoding == DW_EH_PE_absptr)
1140 out_uleb128 (1 + encoding_size);
1141 out_one (DW_OP_addr);
1145 out_uleb128 (1 + 1 + encoding_size);
1146 out_one (DW_OP_GNU_encoded_addr);
1149 if ((encoding & 0x70) == DW_EH_PE_pcrel)
1151 #if CFI_DIFF_EXPR_OK
1152 insn->u.ea.exp.X_op = O_subtract;
1153 insn->u.ea.exp.X_op_symbol = symbol_temp_new_now ();
1154 #elif defined (tc_cfi_emit_pcrel_expr)
1155 tc_cfi_emit_pcrel_expr (&insn->u.ea.exp, encoding_size);
1162 emit_expr (&insn->u.ea.exp, encoding_size);
1172 encoding_size (unsigned char encoding)
1174 if (encoding == DW_EH_PE_omit)
1176 switch (encoding & 0x7)
1179 return bfd_get_arch_size (stdoutput) == 64 ? 8 : 4;
1180 case DW_EH_PE_udata2:
1182 case DW_EH_PE_udata4:
1184 case DW_EH_PE_udata8:
1192 output_cie (struct cie_entry *cie)
1194 symbolS *after_size_address, *end_address;
1196 struct cfi_insn_data *i;
1197 offsetT augmentation_size;
1200 cie->start_address = symbol_temp_new_now ();
1201 after_size_address = symbol_temp_make ();
1202 end_address = symbol_temp_make ();
1204 exp.X_op = O_subtract;
1205 exp.X_add_symbol = end_address;
1206 exp.X_op_symbol = after_size_address;
1207 exp.X_add_number = 0;
1209 emit_expr (&exp, 4); /* Length. */
1210 symbol_set_value_now (after_size_address);
1211 out_four (0); /* CIE id. */
1212 out_one (DW_CIE_VERSION); /* Version. */
1213 out_one ('z'); /* Augmentation. */
1214 if (cie->per_encoding != DW_EH_PE_omit)
1216 if (cie->lsda_encoding != DW_EH_PE_omit)
1219 if (cie->signal_frame)
1222 out_uleb128 (DWARF2_LINE_MIN_INSN_LENGTH); /* Code alignment. */
1223 out_sleb128 (DWARF2_CIE_DATA_ALIGNMENT); /* Data alignment. */
1224 if (DW_CIE_VERSION == 1) /* Return column. */
1225 out_one (cie->return_column);
1227 out_uleb128 (cie->return_column);
1228 augmentation_size = 1 + (cie->lsda_encoding != DW_EH_PE_omit);
1229 if (cie->per_encoding != DW_EH_PE_omit)
1230 augmentation_size += 1 + encoding_size (cie->per_encoding);
1231 out_uleb128 (augmentation_size); /* Augmentation size. */
1232 if (cie->per_encoding != DW_EH_PE_omit)
1234 offsetT size = encoding_size (cie->per_encoding);
1235 out_one (cie->per_encoding);
1236 exp = cie->personality;
1237 if ((cie->per_encoding & 0x70) == DW_EH_PE_pcrel)
1239 #if CFI_DIFF_EXPR_OK
1240 exp.X_op = O_subtract;
1241 exp.X_op_symbol = symbol_temp_new_now ();
1242 emit_expr (&exp, size);
1243 #elif defined (tc_cfi_emit_pcrel_expr)
1244 tc_cfi_emit_pcrel_expr (&exp, size);
1250 emit_expr (&exp, size);
1252 if (cie->lsda_encoding != DW_EH_PE_omit)
1253 out_one (cie->lsda_encoding);
1255 switch (DWARF2_FDE_RELOC_SIZE)
1258 enc = DW_EH_PE_sdata2;
1261 enc = DW_EH_PE_sdata4;
1264 enc = DW_EH_PE_sdata8;
1269 #if CFI_DIFF_EXPR_OK || defined tc_cfi_emit_pcrel_expr
1270 enc |= DW_EH_PE_pcrel;
1275 for (i = cie->first; i != cie->last; i = i->next)
1276 output_cfi_insn (i);
1278 frag_align (2, DW_CFA_nop, 0);
1279 symbol_set_value_now (end_address);
1283 output_fde (struct fde_entry *fde, struct cie_entry *cie,
1284 struct cfi_insn_data *first, int align)
1286 symbolS *after_size_address, *end_address;
1288 offsetT augmentation_size;
1290 after_size_address = symbol_temp_make ();
1291 end_address = symbol_temp_make ();
1293 exp.X_op = O_subtract;
1294 exp.X_add_symbol = end_address;
1295 exp.X_op_symbol = after_size_address;
1296 exp.X_add_number = 0;
1297 emit_expr (&exp, 4); /* Length. */
1298 symbol_set_value_now (after_size_address);
1300 exp.X_add_symbol = after_size_address;
1301 exp.X_op_symbol = cie->start_address;
1302 emit_expr (&exp, 4); /* CIE offset. */
1304 #if CFI_DIFF_EXPR_OK
1305 exp.X_add_symbol = fde->start_address;
1306 exp.X_op_symbol = symbol_temp_new_now ();
1307 emit_expr (&exp, DWARF2_FDE_RELOC_SIZE); /* Code offset. */
1309 exp.X_op = O_symbol;
1310 exp.X_add_symbol = fde->start_address;
1311 exp.X_op_symbol = NULL;
1312 #ifdef tc_cfi_emit_pcrel_expr
1313 tc_cfi_emit_pcrel_expr (&exp, DWARF2_FDE_RELOC_SIZE); /* Code offset. */
1315 emit_expr (&exp, DWARF2_FDE_RELOC_SIZE); /* Code offset. */
1317 exp.X_op = O_subtract;
1320 exp.X_add_symbol = fde->end_address;
1321 exp.X_op_symbol = fde->start_address; /* Code length. */
1322 emit_expr (&exp, DWARF2_FDE_RELOC_SIZE);
1324 augmentation_size = encoding_size (fde->lsda_encoding);
1325 out_uleb128 (augmentation_size); /* Augmentation size. */
1327 if (fde->lsda_encoding != DW_EH_PE_omit)
1330 if ((fde->lsda_encoding & 0x70) == DW_EH_PE_pcrel)
1332 #if CFI_DIFF_EXPR_OK
1333 exp.X_op = O_subtract;
1334 exp.X_op_symbol = symbol_temp_new_now ();
1335 emit_expr (&exp, augmentation_size);
1336 #elif defined (tc_cfi_emit_pcrel_expr)
1337 tc_cfi_emit_pcrel_expr (&exp, augmentation_size);
1343 emit_expr (&exp, augmentation_size);
1346 for (; first; first = first->next)
1347 output_cfi_insn (first);
1349 frag_align (align, DW_CFA_nop, 0);
1350 symbol_set_value_now (end_address);
1353 static struct cie_entry *
1354 select_cie_for_fde (struct fde_entry *fde, struct cfi_insn_data **pfirst)
1356 struct cfi_insn_data *i, *j;
1357 struct cie_entry *cie;
1359 for (cie = cie_root; cie; cie = cie->next)
1361 if (cie->return_column != fde->return_column
1362 || cie->signal_frame != fde->signal_frame
1363 || cie->per_encoding != fde->per_encoding
1364 || cie->lsda_encoding != fde->lsda_encoding)
1366 if (cie->per_encoding != DW_EH_PE_omit)
1368 if (cie->personality.X_op != fde->personality.X_op
1369 || cie->personality.X_add_number
1370 != fde->personality.X_add_number)
1372 switch (cie->personality.X_op)
1375 if (cie->personality.X_unsigned != fde->personality.X_unsigned)
1379 if (cie->personality.X_add_symbol
1380 != fde->personality.X_add_symbol)
1387 for (i = cie->first, j = fde->data;
1388 i != cie->last && j != NULL;
1389 i = i->next, j = j->next)
1391 if (i->insn != j->insn)
1395 case DW_CFA_advance_loc:
1396 case DW_CFA_remember_state:
1397 /* We reached the first advance/remember in the FDE,
1398 but did not reach the end of the CIE list. */
1402 case DW_CFA_def_cfa:
1403 if (i->u.ri.reg != j->u.ri.reg)
1405 if (i->u.ri.offset != j->u.ri.offset)
1409 case DW_CFA_register:
1410 if (i->u.rr.reg1 != j->u.rr.reg1)
1412 if (i->u.rr.reg2 != j->u.rr.reg2)
1416 case DW_CFA_def_cfa_register:
1417 case DW_CFA_restore:
1418 case DW_CFA_undefined:
1419 case DW_CFA_same_value:
1420 if (i->u.r != j->u.r)
1424 case DW_CFA_def_cfa_offset:
1425 if (i->u.i != j->u.i)
1430 case CFI_val_encoded_addr:
1431 /* Don't bother matching these for now. */
1439 /* Success if we reached the end of the CIE list, and we've either
1440 run out of FDE entries or we've encountered an advance,
1441 remember, or escape. */
1444 || j->insn == DW_CFA_advance_loc
1445 || j->insn == DW_CFA_remember_state
1446 || j->insn == CFI_escape
1447 || j->insn == CFI_val_encoded_addr))
1456 cie = xmalloc (sizeof (struct cie_entry));
1457 cie->next = cie_root;
1459 cie->return_column = fde->return_column;
1460 cie->signal_frame = fde->signal_frame;
1461 cie->per_encoding = fde->per_encoding;
1462 cie->lsda_encoding = fde->lsda_encoding;
1463 cie->personality = fde->personality;
1464 cie->first = fde->data;
1466 for (i = cie->first; i ; i = i->next)
1467 if (i->insn == DW_CFA_advance_loc
1468 || i->insn == DW_CFA_remember_state
1469 || i->insn == CFI_escape
1470 || i->insn == CFI_val_encoded_addr)
1485 struct fde_entry *fde;
1486 int save_flag_traditional_format;
1488 if (all_fde_data == 0)
1491 /* Open .eh_frame section. */
1492 cfi_seg = subseg_new (".eh_frame", 0);
1493 bfd_set_section_flags (stdoutput, cfi_seg,
1494 SEC_ALLOC | SEC_LOAD | SEC_DATA
1495 | DWARF2_EH_FRAME_READ_ONLY);
1496 subseg_set (cfi_seg, 0);
1497 record_alignment (cfi_seg, EH_FRAME_ALIGNMENT);
1499 #ifdef md_fix_up_eh_frame
1500 md_fix_up_eh_frame (cfi_seg);
1503 /* Make sure check_eh_frame doesn't do anything with our output. */
1504 save_flag_traditional_format = flag_traditional_format;
1505 flag_traditional_format = 1;
1507 for (fde = all_fde_data; fde ; fde = fde->next)
1509 struct cfi_insn_data *first;
1510 struct cie_entry *cie;
1512 if (fde->end_address == NULL)
1514 as_bad (_("open CFI at the end of file; missing .cfi_endproc directive"));
1515 fde->end_address = fde->start_address;
1518 cie = select_cie_for_fde (fde, &first);
1519 output_fde (fde, cie, first, fde->next == NULL ? EH_FRAME_ALIGNMENT : 2);
1522 flag_traditional_format = save_flag_traditional_format;
1525 #else /* TARGET_USE_CFIPOP */
1530 #endif /* TARGET_USE_CFIPOP */