1 /* dw2gencfi.c - Support for generating Dwarf2 CFI information.
2 Copyright 2003 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 2, 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, 59 Temple Place - Suite 330, Boston, MA
23 #include "dw2gencfi.h"
26 /* We re-use DWARF2_LINE_MIN_INSN_LENGTH for the code alignment field
27 of the CIE. Default to 1 if not otherwise specified. */
28 #ifndef DWARF2_LINE_MIN_INSN_LENGTH
29 # define DWARF2_LINE_MIN_INSN_LENGTH 1
32 /* If TARGET_USE_CFIPOP is defined, it is required that the target
33 provide the following definitions. Otherwise provide them to
34 allow compilation to continue. */
35 #ifndef TARGET_USE_CFIPOP
36 # ifndef DWARF2_DEFAULT_RETURN_COLUMN
37 # define DWARF2_DEFAULT_RETURN_COLUMN 0
39 # ifndef DWARF2_CIE_DATA_ALIGNMENT
40 # define DWARF2_CIE_DATA_ALIGNMENT 1
44 #ifndef tc_cfi_frame_initial_instructions
45 # define tc_cfi_frame_initial_instructions() ((void)0)
51 struct cfi_insn_data *next;
72 struct cfi_escape_data {
73 struct cfi_escape_data *next;
81 struct fde_entry *next;
82 symbolS *start_address;
84 struct cfi_insn_data *data;
85 struct cfi_insn_data **last;
86 unsigned int return_column;
91 struct cie_entry *next;
92 symbolS *start_address;
93 unsigned int return_column;
94 struct cfi_insn_data *first, *last;
98 /* Current open FDE entry. */
99 static struct fde_entry *cur_fde_data;
100 static symbolS *last_address;
101 static offsetT cur_cfa_offset;
103 /* List of FDE entries. */
104 static struct fde_entry *all_fde_data;
105 static struct fde_entry **last_fde_data = &all_fde_data;
107 /* List of CIEs so that they could be reused. */
108 static struct cie_entry *cie_root;
110 /* Stack of old CFI data, for save/restore. */
113 struct cfa_save_data *next;
117 static struct cfa_save_data *cfa_save_stack;
119 /* Construct a new FDE structure and add it to the end of the fde list. */
121 static struct fde_entry *
122 alloc_fde_entry (void)
124 struct fde_entry *fde = xcalloc (1, sizeof (struct fde_entry));
127 *last_fde_data = fde;
128 last_fde_data = &fde->next;
130 fde->last = &fde->data;
131 fde->return_column = DWARF2_DEFAULT_RETURN_COLUMN;
136 /* The following functions are available for a backend to construct its
137 own unwind information, usually from legacy unwind directives. */
139 /* Construct a new INSN structure and add it to the end of the insn list
140 for the currently active FDE. */
142 static struct cfi_insn_data *
143 alloc_cfi_insn_data (void)
145 struct cfi_insn_data *insn = xcalloc (1, sizeof (struct cfi_insn_data));
147 *cur_fde_data->last = insn;
148 cur_fde_data->last = &insn->next;
153 /* Construct a new FDE structure that begins at LABEL. */
156 cfi_new_fde (symbolS *label)
158 struct fde_entry *fde = alloc_fde_entry ();
159 fde->start_address = label;
160 last_address = label;
163 /* End the currently open FDE. */
166 cfi_end_fde (symbolS *label)
168 cur_fde_data->end_address = label;
172 /* Set the return column for the current FDE. */
175 cfi_set_return_column (unsigned regno)
177 cur_fde_data->return_column = regno;
180 /* Universal functions to store new instructions. */
183 cfi_add_CFA_insn(int insn)
185 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
187 insn_ptr->insn = insn;
191 cfi_add_CFA_insn_reg (int insn, unsigned regno)
193 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
195 insn_ptr->insn = insn;
196 insn_ptr->u.r = regno;
200 cfi_add_CFA_insn_offset (int insn, offsetT offset)
202 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
204 insn_ptr->insn = insn;
205 insn_ptr->u.i = offset;
209 cfi_add_CFA_insn_reg_reg (int insn, unsigned reg1, unsigned reg2)
211 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
213 insn_ptr->insn = insn;
214 insn_ptr->u.rr.reg1 = reg1;
215 insn_ptr->u.rr.reg2 = reg2;
219 cfi_add_CFA_insn_reg_offset (int insn, unsigned regno, offsetT offset)
221 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
223 insn_ptr->insn = insn;
224 insn_ptr->u.ri.reg = regno;
225 insn_ptr->u.ri.offset = offset;
228 /* Add a CFI insn to advance the PC from the last address to LABEL. */
231 cfi_add_advance_loc (symbolS *label)
233 struct cfi_insn_data *insn = alloc_cfi_insn_data ();
235 insn->insn = DW_CFA_advance_loc;
236 insn->u.ll.lab1 = last_address;
237 insn->u.ll.lab2 = label;
239 last_address = label;
242 /* Add a DW_CFA_offset record to the CFI data. */
245 cfi_add_CFA_offset (unsigned regno, offsetT offset)
247 unsigned int abs_data_align;
249 cfi_add_CFA_insn_reg_offset (DW_CFA_offset, regno, offset);
251 abs_data_align = (DWARF2_CIE_DATA_ALIGNMENT < 0
252 ? -DWARF2_CIE_DATA_ALIGNMENT : DWARF2_CIE_DATA_ALIGNMENT);
253 if (offset % abs_data_align)
254 as_bad (_("register save offset not a multiple of %u"), abs_data_align);
257 /* Add a DW_CFA_def_cfa record to the CFI data. */
260 cfi_add_CFA_def_cfa (unsigned regno, offsetT offset)
262 cfi_add_CFA_insn_reg_offset (DW_CFA_def_cfa, regno, offset);
263 cur_cfa_offset = offset;
266 /* Add a DW_CFA_register record to the CFI data. */
269 cfi_add_CFA_register (unsigned reg1, unsigned reg2)
271 cfi_add_CFA_insn_reg_reg (DW_CFA_register, reg1, reg2);
274 /* Add a DW_CFA_def_cfa_register record to the CFI data. */
277 cfi_add_CFA_def_cfa_register (unsigned regno)
279 cfi_add_CFA_insn_reg (DW_CFA_def_cfa_register, regno);
282 /* Add a DW_CFA_def_cfa_offset record to the CFI data. */
285 cfi_add_CFA_def_cfa_offset (offsetT offset)
287 cfi_add_CFA_insn_offset (DW_CFA_def_cfa_offset, offset);
288 cur_cfa_offset = offset;
292 cfi_add_CFA_restore (unsigned regno)
294 cfi_add_CFA_insn_reg (DW_CFA_restore, regno);
298 cfi_add_CFA_undefined (unsigned regno)
300 cfi_add_CFA_insn_reg (DW_CFA_undefined, regno);
304 cfi_add_CFA_same_value (unsigned regno)
306 cfi_add_CFA_insn_reg (DW_CFA_same_value, regno);
310 cfi_add_CFA_remember_state (void)
312 struct cfa_save_data *p;
314 cfi_add_CFA_insn (DW_CFA_remember_state);
316 p = xmalloc (sizeof (*p));
317 p->cfa_offset = cur_cfa_offset;
318 p->next = cfa_save_stack;
323 cfi_add_CFA_restore_state (void)
325 struct cfa_save_data *p;
327 cfi_add_CFA_insn (DW_CFA_restore_state);
332 cur_cfa_offset = p->cfa_offset;
333 cfa_save_stack = p->next;
339 /* Parse CFI assembler directives. */
341 static void dot_cfi (int);
342 static void dot_cfi_escape (int);
343 static void dot_cfi_startproc (int);
344 static void dot_cfi_endproc (int);
346 /* Fake CFI type; outside the byte range of any real CFI insn. */
347 #define CFI_adjust_cfa_offset 0x100
348 #define CFI_return_column 0x101
349 #define CFI_rel_offset 0x102
350 #define CFI_escape 0x103
352 const pseudo_typeS cfi_pseudo_table[] =
354 { "cfi_startproc", dot_cfi_startproc, 0 },
355 { "cfi_endproc", dot_cfi_endproc, 0 },
356 { "cfi_def_cfa", dot_cfi, DW_CFA_def_cfa },
357 { "cfi_def_cfa_register", dot_cfi, DW_CFA_def_cfa_register },
358 { "cfi_def_cfa_offset", dot_cfi, DW_CFA_def_cfa_offset },
359 { "cfi_adjust_cfa_offset", dot_cfi, CFI_adjust_cfa_offset },
360 { "cfi_offset", dot_cfi, DW_CFA_offset },
361 { "cfi_rel_offset", dot_cfi, CFI_rel_offset },
362 { "cfi_register", dot_cfi, DW_CFA_register },
363 { "cfi_return_column", dot_cfi, CFI_return_column },
364 { "cfi_restore", dot_cfi, DW_CFA_restore },
365 { "cfi_undefined", dot_cfi, DW_CFA_undefined },
366 { "cfi_same_value", dot_cfi, DW_CFA_same_value },
367 { "cfi_remember_state", dot_cfi, DW_CFA_remember_state },
368 { "cfi_restore_state", dot_cfi, DW_CFA_restore_state },
369 { "cfi_escape", dot_cfi_escape, 0 },
374 cfi_parse_separator (void)
377 if (*input_line_pointer == ',')
378 input_line_pointer++;
380 as_bad (_("missing separator"));
389 #ifdef tc_regname_to_dw2regnum
391 if (is_name_beginner (*input_line_pointer)
392 || (*input_line_pointer == '%'
393 && is_name_beginner (*++input_line_pointer)))
397 name = input_line_pointer;
398 c = get_symbol_end ();
400 if ((regno = tc_regname_to_dw2regnum (name)) < 0)
402 as_bad (_("bad register expression"));
406 *input_line_pointer = c;
416 regno = exp.X_add_number;
420 as_bad (_("bad register expression"));
429 cfi_parse_const (void)
431 return get_absolute_expression ();
442 as_bad (_("CFI instruction used without previous .cfi_startproc"));
446 /* If the last address was not at the current PC, advance to current. */
447 if (symbol_get_frag (last_address) != frag_now
448 || S_GET_VALUE (last_address) != frag_now_fix ())
449 cfi_add_advance_loc (symbol_temp_new_now ());
454 reg1 = cfi_parse_reg ();
455 cfi_parse_separator ();
456 offset = cfi_parse_const ();
457 cfi_add_CFA_offset (reg1, offset);
461 reg1 = cfi_parse_reg ();
462 cfi_parse_separator ();
463 offset = cfi_parse_const ();
464 cfi_add_CFA_offset (reg1, offset - cur_cfa_offset);
468 reg1 = cfi_parse_reg ();
469 cfi_parse_separator ();
470 offset = cfi_parse_const ();
471 cfi_add_CFA_def_cfa (reg1, offset);
474 case DW_CFA_register:
475 reg1 = cfi_parse_reg ();
476 cfi_parse_separator ();
477 reg2 = cfi_parse_reg ();
478 cfi_add_CFA_register (reg1, reg2);
481 case DW_CFA_def_cfa_register:
482 reg1 = cfi_parse_reg ();
483 cfi_add_CFA_def_cfa_register (reg1);
486 case DW_CFA_def_cfa_offset:
487 offset = cfi_parse_const ();
488 cfi_add_CFA_def_cfa_offset (offset);
491 case CFI_adjust_cfa_offset:
492 offset = cfi_parse_const ();
493 cfi_add_CFA_def_cfa_offset (cur_cfa_offset + offset);
497 reg1 = cfi_parse_reg ();
498 cfi_add_CFA_restore (reg1);
501 case DW_CFA_undefined:
502 reg1 = cfi_parse_reg ();
503 cfi_add_CFA_undefined (reg1);
506 case DW_CFA_same_value:
507 reg1 = cfi_parse_reg ();
508 cfi_add_CFA_same_value (reg1);
511 case CFI_return_column:
512 reg1 = cfi_parse_reg ();
513 cfi_set_return_column (reg1);
516 case DW_CFA_remember_state:
517 cfi_add_CFA_remember_state ();
520 case DW_CFA_restore_state:
521 cfi_add_CFA_restore_state ();
528 demand_empty_rest_of_line ();
532 dot_cfi_escape (int ignored ATTRIBUTE_UNUSED)
534 struct cfi_escape_data *head, **tail, *e;
535 struct cfi_insn_data *insn;
539 as_bad (_("CFI instruction used without previous .cfi_startproc"));
543 /* If the last address was not at the current PC, advance to current. */
544 if (symbol_get_frag (last_address) != frag_now
545 || S_GET_VALUE (last_address) != frag_now_fix ())
546 cfi_add_advance_loc (symbol_temp_new_now ());
551 e = xmalloc (sizeof (*e));
552 do_parse_cons_expression (&e->exp, 1);
556 while (*input_line_pointer++ == ',');
559 insn = alloc_cfi_insn_data ();
560 insn->insn = CFI_escape;
565 dot_cfi_startproc (int ignored ATTRIBUTE_UNUSED)
571 as_bad (_("previous CFI entry not closed (missing .cfi_endproc)"));
575 cfi_new_fde (symbol_temp_new_now ());
578 if (is_name_beginner (*input_line_pointer))
582 name = input_line_pointer;
583 c = get_symbol_end ();
585 if (strcmp (name, "simple") == 0)
588 *input_line_pointer = c;
591 input_line_pointer = name;
593 demand_empty_rest_of_line ();
596 tc_cfi_frame_initial_instructions ();
600 dot_cfi_endproc (int ignored ATTRIBUTE_UNUSED)
604 as_bad (_(".cfi_endproc without corresponding .cfi_startproc"));
608 cfi_end_fde (symbol_temp_new_now ());
612 /* Emit a single byte into the current segment. */
617 FRAG_APPEND_1_CHAR (byte);
620 /* Emit a two-byte word into the current segment. */
625 md_number_to_chars (frag_more (2), data, 2);
628 /* Emit a four byte word into the current segment. */
633 md_number_to_chars (frag_more (4), data, 4);
636 /* Emit an unsigned "little-endian base 128" number. */
639 out_uleb128 (addressT value)
641 output_leb128 (frag_more (sizeof_leb128 (value, 0)), value, 0);
644 /* Emit an unsigned "little-endian base 128" number. */
647 out_sleb128 (offsetT value)
649 output_leb128 (frag_more (sizeof_leb128 (value, 1)), value, 1);
653 output_cfi_insn (struct cfi_insn_data *insn)
660 case DW_CFA_advance_loc:
662 symbolS *from = insn->u.ll.lab1;
663 symbolS *to = insn->u.ll.lab2;
665 if (symbol_get_frag (to) == symbol_get_frag (from))
667 addressT delta = S_GET_VALUE (to) - S_GET_VALUE (from);
668 addressT scaled = delta / DWARF2_LINE_MIN_INSN_LENGTH;
671 out_one (DW_CFA_advance_loc + scaled);
672 else if (delta <= 0xFF)
674 out_one (DW_CFA_advance_loc1);
677 else if (delta <= 0xFFFF)
679 out_one (DW_CFA_advance_loc2);
684 out_one (DW_CFA_advance_loc4);
692 exp.X_op = O_subtract;
693 exp.X_add_symbol = to;
694 exp.X_op_symbol = from;
695 exp.X_add_number = 0;
697 /* The code in ehopt.c expects that one byte of the encoding
698 is already allocated to the frag. This comes from the way
699 that it scans the .eh_frame section looking first for the
700 .byte DW_CFA_advance_loc4. */
703 frag_var (rs_cfa, 4, 0, DWARF2_LINE_MIN_INSN_LENGTH << 3,
704 make_expr_symbol (&exp), frag_now_fix () - 1,
711 offset = insn->u.ri.offset;
714 out_one (DW_CFA_def_cfa_sf);
715 out_uleb128 (insn->u.ri.reg);
716 out_uleb128 (offset);
720 out_one (DW_CFA_def_cfa);
721 out_uleb128 (insn->u.ri.reg);
722 out_uleb128 (offset);
726 case DW_CFA_def_cfa_register:
727 case DW_CFA_undefined:
728 case DW_CFA_same_value:
729 out_one (insn->insn);
730 out_uleb128 (insn->u.r);
733 case DW_CFA_def_cfa_offset:
737 out_one (DW_CFA_def_cfa_offset_sf);
738 out_sleb128 (offset);
742 out_one (DW_CFA_def_cfa_offset);
743 out_uleb128 (offset);
751 out_one (DW_CFA_restore + regno);
755 out_one (DW_CFA_restore_extended);
761 regno = insn->u.ri.reg;
762 offset = insn->u.ri.offset / DWARF2_CIE_DATA_ALIGNMENT;
765 out_one (DW_CFA_offset_extended_sf);
767 out_sleb128 (offset);
769 else if (regno <= 0x3F)
771 out_one (DW_CFA_offset + regno);
772 out_uleb128 (offset);
776 out_one (DW_CFA_offset_extended);
778 out_uleb128 (offset);
782 case DW_CFA_register:
783 out_one (DW_CFA_register);
784 out_uleb128 (insn->u.rr.reg1);
785 out_uleb128 (insn->u.rr.reg2);
788 case DW_CFA_remember_state:
789 case DW_CFA_restore_state:
790 out_one (insn->insn);
795 struct cfi_escape_data *e;
796 for (e = insn->u.esc; e ; e = e->next)
797 emit_expr (&e->exp, 1);
807 output_cie (struct cie_entry *cie)
809 symbolS *after_size_address, *end_address;
811 struct cfi_insn_data *i;
813 cie->start_address = symbol_temp_new_now ();
814 after_size_address = symbol_temp_make ();
815 end_address = symbol_temp_make ();
817 exp.X_op = O_subtract;
818 exp.X_add_symbol = end_address;
819 exp.X_op_symbol = after_size_address;
820 exp.X_add_number = 0;
822 emit_expr (&exp, 4); /* Length */
823 symbol_set_value_now (after_size_address);
824 out_four (0); /* CIE id */
825 out_one (DW_CIE_VERSION); /* Version */
826 out_one ('z'); /* Augmentation */
829 out_uleb128 (DWARF2_LINE_MIN_INSN_LENGTH); /* Code alignment */
830 out_sleb128 (DWARF2_CIE_DATA_ALIGNMENT); /* Data alignment */
831 out_one (cie->return_column); /* Return column */
832 out_uleb128 (1); /* Augmentation size */
833 out_one (DW_EH_PE_pcrel | DW_EH_PE_sdata4);
836 for (i = cie->first; i != cie->last; i = i->next)
839 frag_align (2, 0, 0);
840 symbol_set_value_now (end_address);
844 output_fde (struct fde_entry *fde, struct cie_entry *cie,
845 struct cfi_insn_data *first)
847 symbolS *after_size_address, *end_address;
850 after_size_address = symbol_temp_make ();
851 end_address = symbol_temp_make ();
853 exp.X_op = O_subtract;
854 exp.X_add_symbol = end_address;
855 exp.X_op_symbol = after_size_address;
856 exp.X_add_number = 0;
857 emit_expr (&exp, 4); /* Length */
858 symbol_set_value_now (after_size_address);
860 exp.X_add_symbol = after_size_address;
861 exp.X_op_symbol = cie->start_address;
862 emit_expr (&exp, 4); /* CIE offset */
864 exp.X_add_symbol = fde->start_address;
865 exp.X_op_symbol = symbol_temp_new_now ();
866 emit_expr (&exp, 4); /* Code offset */
868 exp.X_add_symbol = fde->end_address;
869 exp.X_op_symbol = fde->start_address; /* Code length */
872 out_uleb128 (0); /* Augmentation size */
874 for (; first; first = first->next)
875 output_cfi_insn (first);
877 frag_align (2, 0, 0);
878 symbol_set_value_now (end_address);
881 static struct cie_entry *
882 select_cie_for_fde (struct fde_entry *fde, struct cfi_insn_data **pfirst)
884 struct cfi_insn_data *i, *j;
885 struct cie_entry *cie;
887 for (cie = cie_root; cie; cie = cie->next)
889 if (cie->return_column != fde->return_column)
891 for (i = cie->first, j = fde->data;
892 i != cie->last && j != NULL;
893 i = i->next, j = j->next)
895 if (i->insn != j->insn)
899 case DW_CFA_advance_loc:
900 /* We reached the first advance in the FDE, but did not
901 reach the end of the CIE list. */
906 if (i->u.ri.reg != j->u.ri.reg)
908 if (i->u.ri.offset != j->u.ri.offset)
912 case DW_CFA_register:
913 if (i->u.rr.reg1 != j->u.rr.reg1)
915 if (i->u.rr.reg2 != j->u.rr.reg2)
919 case DW_CFA_def_cfa_register:
921 case DW_CFA_undefined:
922 case DW_CFA_same_value:
923 if (i->u.r != j->u.r)
927 case DW_CFA_def_cfa_offset:
928 if (i->u.i != j->u.i)
933 /* Don't bother matching these for now. */
941 /* Success if we reached the end of the CIE list, and we've either
942 run out of FDE entries or we've encountered an advance. */
943 if (i == cie->last && (!j || j->insn == DW_CFA_advance_loc))
952 cie = xmalloc (sizeof (struct cie_entry));
953 cie->next = cie_root;
955 cie->return_column = fde->return_column;
956 cie->first = fde->data;
958 for (i = cie->first; i ; i = i->next)
959 if (i->insn == DW_CFA_advance_loc)
974 struct fde_entry *fde;
975 int save_flag_traditional_format;
979 as_bad (_("open CFI at the end of file; missing .cfi_endproc directive"));
980 cur_fde_data->end_address = cur_fde_data->start_address;
983 if (all_fde_data == 0)
986 /* Open .eh_frame section. */
987 cfi_seg = subseg_new (".eh_frame", 0);
989 bfd_set_section_flags (stdoutput, cfi_seg,
990 SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_READONLY);
992 subseg_set (cfi_seg, 0);
993 record_alignment (cfi_seg, 2);
995 /* Make sure check_eh_frame doesn't do anything with our output. */
996 save_flag_traditional_format = flag_traditional_format;
997 flag_traditional_format = 1;
999 for (fde = all_fde_data; fde ; fde = fde->next)
1001 struct cfi_insn_data *first;
1002 struct cie_entry *cie;
1004 cie = select_cie_for_fde (fde, &first);
1005 output_fde (fde, cie, first);
1008 flag_traditional_format = save_flag_traditional_format;