1 /* dw2gencfi.c - Support for generating Dwarf2 CFI information.
2 Copyright 2003 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 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
;
76 struct fde_entry
*next
;
77 symbolS
*start_address
;
79 struct cfi_insn_data
*data
;
80 struct cfi_insn_data
**last
;
81 unsigned int return_column
;
86 struct cie_entry
*next
;
87 symbolS
*start_address
;
88 unsigned int return_column
;
89 struct cfi_insn_data
*first
, *last
;
93 /* Current open FDE entry. */
94 static struct fde_entry
*cur_fde_data
;
95 static symbolS
*last_address
;
96 static offsetT cur_cfa_offset
;
98 /* List of FDE entries. */
99 static struct fde_entry
*all_fde_data
;
100 static struct fde_entry
**last_fde_data
= &all_fde_data
;
102 /* List of CIEs so that they could be reused. */
103 static struct cie_entry
*cie_root
;
105 /* Stack of old CFI data, for save/restore. */
108 struct cfa_save_data
*next
;
112 static struct cfa_save_data
*cfa_save_stack
;
114 /* Construct a new FDE structure and add it to the end of the fde list. */
116 static struct fde_entry
*
117 alloc_fde_entry (void)
119 struct fde_entry
*fde
= xcalloc (1, sizeof (struct fde_entry
));
122 *last_fde_data
= fde
;
123 last_fde_data
= &fde
->next
;
125 fde
->last
= &fde
->data
;
126 fde
->return_column
= DWARF2_DEFAULT_RETURN_COLUMN
;
131 /* The following functions are available for a backend to construct its
132 own unwind information, usually from legacy unwind directives. */
134 /* Construct a new INSN structure and add it to the end of the insn list
135 for the currently active FDE. */
137 static struct cfi_insn_data
*
138 alloc_cfi_insn_data (void)
140 struct cfi_insn_data
*insn
= xcalloc (1, sizeof (struct cfi_insn_data
));
142 *cur_fde_data
->last
= insn
;
143 cur_fde_data
->last
= &insn
->next
;
148 /* Construct a new FDE structure that begins at LABEL. */
151 cfi_new_fde (symbolS
*label
)
153 struct fde_entry
*fde
= alloc_fde_entry ();
154 fde
->start_address
= label
;
155 last_address
= label
;
158 /* End the currently open FDE. */
161 cfi_end_fde (symbolS
*label
)
163 cur_fde_data
->end_address
= label
;
167 /* Set the return column for the current FDE. */
170 cfi_set_return_column (unsigned regno
)
172 cur_fde_data
->return_column
= regno
;
175 /* Universal functions to store new instructions. */
178 cfi_add_CFA_insn(int insn
)
180 struct cfi_insn_data
*insn_ptr
= alloc_cfi_insn_data ();
182 insn_ptr
->insn
= insn
;
186 cfi_add_CFA_insn_reg (int insn
, unsigned regno
)
188 struct cfi_insn_data
*insn_ptr
= alloc_cfi_insn_data ();
190 insn_ptr
->insn
= insn
;
191 insn_ptr
->u
.r
= regno
;
195 cfi_add_CFA_insn_offset (int insn
, offsetT offset
)
197 struct cfi_insn_data
*insn_ptr
= alloc_cfi_insn_data ();
199 insn_ptr
->insn
= insn
;
200 insn_ptr
->u
.i
= offset
;
204 cfi_add_CFA_insn_reg_reg (int insn
, unsigned reg1
, unsigned reg2
)
206 struct cfi_insn_data
*insn_ptr
= alloc_cfi_insn_data ();
208 insn_ptr
->insn
= insn
;
209 insn_ptr
->u
.rr
.reg1
= reg1
;
210 insn_ptr
->u
.rr
.reg2
= reg2
;
214 cfi_add_CFA_insn_reg_offset (int insn
, unsigned regno
, offsetT offset
)
216 struct cfi_insn_data
*insn_ptr
= alloc_cfi_insn_data ();
218 insn_ptr
->insn
= insn
;
219 insn_ptr
->u
.ri
.reg
= regno
;
220 insn_ptr
->u
.ri
.offset
= offset
;
223 /* Add a CFI insn to advance the PC from the last address to LABEL. */
226 cfi_add_advance_loc (symbolS
*label
)
228 struct cfi_insn_data
*insn
= alloc_cfi_insn_data ();
230 insn
->insn
= DW_CFA_advance_loc
;
231 insn
->u
.ll
.lab1
= last_address
;
232 insn
->u
.ll
.lab2
= label
;
234 last_address
= label
;
237 /* Add a DW_CFA_offset record to the CFI data. */
240 cfi_add_CFA_offset (unsigned regno
, offsetT offset
)
242 unsigned int abs_data_align
;
244 cfi_add_CFA_insn_reg_offset (DW_CFA_offset
, regno
, offset
);
246 abs_data_align
= (DWARF2_CIE_DATA_ALIGNMENT
< 0
247 ? -DWARF2_CIE_DATA_ALIGNMENT
: DWARF2_CIE_DATA_ALIGNMENT
);
248 if (offset
% abs_data_align
)
249 as_bad (_("register save offset not a multiple of %u"), abs_data_align
);
252 /* Add a DW_CFA_def_cfa record to the CFI data. */
255 cfi_add_CFA_def_cfa (unsigned regno
, offsetT offset
)
257 cfi_add_CFA_insn_reg_offset (DW_CFA_def_cfa
, regno
, offset
);
258 cur_cfa_offset
= offset
;
261 /* Add a DW_CFA_register record to the CFI data. */
264 cfi_add_CFA_register (unsigned reg1
, unsigned reg2
)
266 cfi_add_CFA_insn_reg_reg (DW_CFA_register
, reg1
, reg2
);
269 /* Add a DW_CFA_def_cfa_register record to the CFI data. */
272 cfi_add_CFA_def_cfa_register (unsigned regno
)
274 cfi_add_CFA_insn_reg (DW_CFA_def_cfa_register
, regno
);
277 /* Add a DW_CFA_def_cfa_offset record to the CFI data. */
280 cfi_add_CFA_def_cfa_offset (offsetT offset
)
282 cfi_add_CFA_insn_offset (DW_CFA_def_cfa_offset
, offset
);
283 cur_cfa_offset
= offset
;
287 cfi_add_CFA_restore (unsigned regno
)
289 cfi_add_CFA_insn_reg (DW_CFA_restore
, regno
);
293 cfi_add_CFA_undefined (unsigned regno
)
295 cfi_add_CFA_insn_reg (DW_CFA_undefined
, regno
);
299 cfi_add_CFA_same_value (unsigned regno
)
301 cfi_add_CFA_insn_reg (DW_CFA_same_value
, regno
);
305 cfi_add_CFA_remember_state (void)
307 struct cfa_save_data
*p
;
309 cfi_add_CFA_insn (DW_CFA_remember_state
);
311 p
= xmalloc (sizeof (*p
));
312 p
->cfa_offset
= cur_cfa_offset
;
313 p
->next
= cfa_save_stack
;
318 cfi_add_CFA_restore_state (void)
320 struct cfa_save_data
*p
;
322 cfi_add_CFA_insn (DW_CFA_restore_state
);
327 cur_cfa_offset
= p
->cfa_offset
;
328 cfa_save_stack
= p
->next
;
334 cfi_add_CFA_nop (void)
336 cfi_add_CFA_insn (DW_CFA_nop
);
340 /* Parse CFI assembler directives. */
342 static void dot_cfi (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
351 const pseudo_typeS cfi_pseudo_table
[] =
353 { "cfi_startproc", dot_cfi_startproc
, 0 },
354 { "cfi_endproc", dot_cfi_endproc
, 0 },
355 { "cfi_def_cfa", dot_cfi
, DW_CFA_def_cfa
},
356 { "cfi_def_cfa_register", dot_cfi
, DW_CFA_def_cfa_register
},
357 { "cfi_def_cfa_offset", dot_cfi
, DW_CFA_def_cfa_offset
},
358 { "cfi_adjust_cfa_offset", dot_cfi
, CFI_adjust_cfa_offset
},
359 { "cfi_offset", dot_cfi
, DW_CFA_offset
},
360 { "cfi_rel_offset", dot_cfi
, CFI_rel_offset
},
361 { "cfi_register", dot_cfi
, DW_CFA_register
},
362 { "cfi_return_column", dot_cfi
, CFI_return_column
},
363 { "cfi_restore", dot_cfi
, DW_CFA_restore
},
364 { "cfi_undefined", dot_cfi
, DW_CFA_undefined
},
365 { "cfi_same_value", dot_cfi
, DW_CFA_same_value
},
366 { "cfi_remember_state", dot_cfi
, DW_CFA_remember_state
},
367 { "cfi_restore_state", dot_cfi
, DW_CFA_restore_state
},
368 { "cfi_nop", dot_cfi
, DW_CFA_nop
},
373 cfi_parse_separator (void)
376 if (*input_line_pointer
== ',')
377 input_line_pointer
++;
379 as_bad (_("missing separator"));
388 #ifdef tc_regname_to_dw2regnum
390 if (is_name_beginner (*input_line_pointer
)
391 || (*input_line_pointer
== '%'
392 && is_name_beginner (*++input_line_pointer
)))
396 name
= input_line_pointer
;
397 c
= get_symbol_end ();
399 if ((regno
= tc_regname_to_dw2regnum (name
)) < 0)
401 as_bad (_("bad register expression"));
405 *input_line_pointer
= c
;
415 regno
= exp
.X_add_number
;
419 as_bad (_("bad register expression"));
428 cfi_parse_const (void)
430 return get_absolute_expression ();
441 as_bad (_("CFI instruction used without previous .cfi_startproc"));
445 /* If the last address was not at the current PC, advance to current. */
446 if (symbol_get_frag (last_address
) != frag_now
447 || S_GET_VALUE (last_address
) != frag_now_fix ())
448 cfi_add_advance_loc (symbol_temp_new_now ());
453 reg1
= cfi_parse_reg ();
454 cfi_parse_separator ();
455 offset
= cfi_parse_const ();
456 cfi_add_CFA_offset (reg1
, offset
);
460 reg1
= cfi_parse_reg ();
461 cfi_parse_separator ();
462 offset
= cfi_parse_const ();
463 cfi_add_CFA_offset (reg1
, offset
- cur_cfa_offset
);
467 reg1
= cfi_parse_reg ();
468 cfi_parse_separator ();
469 offset
= cfi_parse_const ();
470 cfi_add_CFA_def_cfa (reg1
, offset
);
473 case DW_CFA_register
:
474 reg1
= cfi_parse_reg ();
475 cfi_parse_separator ();
476 reg2
= cfi_parse_reg ();
477 cfi_add_CFA_register (reg1
, reg2
);
480 case DW_CFA_def_cfa_register
:
481 reg1
= cfi_parse_reg ();
482 cfi_add_CFA_def_cfa_register (reg1
);
485 case DW_CFA_def_cfa_offset
:
486 offset
= cfi_parse_const ();
487 cfi_add_CFA_def_cfa_offset (offset
);
490 case CFI_adjust_cfa_offset
:
491 offset
= cfi_parse_const ();
492 cfi_add_CFA_def_cfa_offset (cur_cfa_offset
+ offset
);
496 reg1
= cfi_parse_reg ();
497 cfi_add_CFA_restore (reg1
);
500 case DW_CFA_undefined
:
501 reg1
= cfi_parse_reg ();
502 cfi_add_CFA_undefined (reg1
);
505 case DW_CFA_same_value
:
506 reg1
= cfi_parse_reg ();
507 cfi_add_CFA_same_value (reg1
);
510 case CFI_return_column
:
511 reg1
= cfi_parse_reg ();
512 cfi_set_return_column (reg1
);
515 case DW_CFA_remember_state
:
516 cfi_add_CFA_remember_state ();
519 case DW_CFA_restore_state
:
520 cfi_add_CFA_restore_state ();
531 demand_empty_rest_of_line ();
535 dot_cfi_startproc (int ignored ATTRIBUTE_UNUSED
)
541 as_bad (_("previous CFI entry not closed (missing .cfi_endproc)"));
545 cfi_new_fde (symbol_temp_new_now ());
548 if (is_name_beginner (*input_line_pointer
))
552 name
= input_line_pointer
;
553 c
= get_symbol_end ();
555 if (strcmp (name
, "simple") == 0)
558 *input_line_pointer
= c
;
561 input_line_pointer
= name
;
563 demand_empty_rest_of_line ();
566 tc_cfi_frame_initial_instructions ();
570 dot_cfi_endproc (int ignored ATTRIBUTE_UNUSED
)
574 as_bad (_(".cfi_endproc without corresponding .cfi_startproc"));
578 cfi_end_fde (symbol_temp_new_now ());
582 /* Emit a single byte into the current segment. */
587 FRAG_APPEND_1_CHAR (byte
);
590 /* Emit a two-byte word into the current segment. */
595 md_number_to_chars (frag_more (2), data
, 2);
598 /* Emit a four byte word into the current segment. */
603 md_number_to_chars (frag_more (4), data
, 4);
606 /* Emit an unsigned "little-endian base 128" number. */
609 out_uleb128 (addressT value
)
611 output_leb128 (frag_more (sizeof_leb128 (value
, 0)), value
, 0);
614 /* Emit an unsigned "little-endian base 128" number. */
617 out_sleb128 (offsetT value
)
619 output_leb128 (frag_more (sizeof_leb128 (value
, 1)), value
, 1);
623 output_cfi_insn (struct cfi_insn_data
*insn
)
630 case DW_CFA_advance_loc
:
632 symbolS
*from
= insn
->u
.ll
.lab1
;
633 symbolS
*to
= insn
->u
.ll
.lab2
;
635 if (symbol_get_frag (to
) == symbol_get_frag (from
))
637 addressT delta
= S_GET_VALUE (to
) - S_GET_VALUE (from
);
638 addressT scaled
= delta
/ DWARF2_LINE_MIN_INSN_LENGTH
;
641 out_one (DW_CFA_advance_loc
+ scaled
);
642 else if (delta
<= 0xFF)
644 out_one (DW_CFA_advance_loc1
);
647 else if (delta
<= 0xFFFF)
649 out_one (DW_CFA_advance_loc2
);
654 out_one (DW_CFA_advance_loc4
);
662 exp
.X_op
= O_subtract
;
663 exp
.X_add_symbol
= to
;
664 exp
.X_op_symbol
= from
;
665 exp
.X_add_number
= 0;
667 /* The code in ehopt.c expects that one byte of the encoding
668 is already allocated to the frag. This comes from the way
669 that it scans the .eh_frame section looking first for the
670 .byte DW_CFA_advance_loc4. */
673 frag_var (rs_cfa
, 4, 0, DWARF2_LINE_MIN_INSN_LENGTH
<< 3,
674 make_expr_symbol (&exp
), frag_now_fix () - 1,
681 offset
= insn
->u
.ri
.offset
;
684 out_one (DW_CFA_def_cfa_sf
);
685 out_uleb128 (insn
->u
.ri
.reg
);
686 out_uleb128 (offset
);
690 out_one (DW_CFA_def_cfa
);
691 out_uleb128 (insn
->u
.ri
.reg
);
692 out_uleb128 (offset
);
696 case DW_CFA_def_cfa_register
:
697 case DW_CFA_undefined
:
698 case DW_CFA_same_value
:
699 out_one (insn
->insn
);
700 out_uleb128 (insn
->u
.r
);
703 case DW_CFA_def_cfa_offset
:
707 out_one (DW_CFA_def_cfa_offset_sf
);
708 out_sleb128 (offset
);
712 out_one (DW_CFA_def_cfa_offset
);
713 out_uleb128 (offset
);
721 out_one (DW_CFA_restore
+ regno
);
725 out_one (DW_CFA_restore_extended
);
731 regno
= insn
->u
.ri
.reg
;
732 offset
= insn
->u
.ri
.offset
/ DWARF2_CIE_DATA_ALIGNMENT
;
735 out_one (DW_CFA_offset_extended_sf
);
737 out_sleb128 (offset
);
739 else if (regno
<= 0x3F)
741 out_one (DW_CFA_offset
+ regno
);
742 out_uleb128 (offset
);
746 out_one (DW_CFA_offset_extended
);
748 out_uleb128 (offset
);
752 case DW_CFA_register
:
753 out_one (DW_CFA_register
);
754 out_uleb128 (insn
->u
.rr
.reg1
);
755 out_uleb128 (insn
->u
.rr
.reg2
);
758 case DW_CFA_remember_state
:
759 case DW_CFA_restore_state
:
761 out_one (insn
->insn
);
770 output_cie (struct cie_entry
*cie
)
772 symbolS
*after_size_address
, *end_address
;
774 struct cfi_insn_data
*i
;
776 cie
->start_address
= symbol_temp_new_now ();
777 after_size_address
= symbol_temp_make ();
778 end_address
= symbol_temp_make ();
780 exp
.X_op
= O_subtract
;
781 exp
.X_add_symbol
= end_address
;
782 exp
.X_op_symbol
= after_size_address
;
783 exp
.X_add_number
= 0;
785 emit_expr (&exp
, 4); /* Length */
786 symbol_set_value_now (after_size_address
);
787 out_four (0); /* CIE id */
788 out_one (DW_CIE_VERSION
); /* Version */
789 out_one ('z'); /* Augmentation */
792 out_uleb128 (DWARF2_LINE_MIN_INSN_LENGTH
); /* Code alignment */
793 out_sleb128 (DWARF2_CIE_DATA_ALIGNMENT
); /* Data alignment */
794 out_one (cie
->return_column
); /* Return column */
795 out_uleb128 (1); /* Augmentation size */
796 out_one (DW_EH_PE_pcrel
| DW_EH_PE_sdata4
);
799 for (i
= cie
->first
; i
!= cie
->last
; i
= i
->next
)
802 frag_align (2, 0, 0);
803 symbol_set_value_now (end_address
);
807 output_fde (struct fde_entry
*fde
, struct cie_entry
*cie
,
808 struct cfi_insn_data
*first
)
810 symbolS
*after_size_address
, *end_address
;
813 after_size_address
= symbol_temp_make ();
814 end_address
= symbol_temp_make ();
816 exp
.X_op
= O_subtract
;
817 exp
.X_add_symbol
= end_address
;
818 exp
.X_op_symbol
= after_size_address
;
819 exp
.X_add_number
= 0;
820 emit_expr (&exp
, 4); /* Length */
821 symbol_set_value_now (after_size_address
);
823 exp
.X_add_symbol
= after_size_address
;
824 exp
.X_op_symbol
= cie
->start_address
;
825 emit_expr (&exp
, 4); /* CIE offset */
827 exp
.X_add_symbol
= fde
->start_address
;
828 exp
.X_op_symbol
= symbol_temp_new_now ();
829 emit_expr (&exp
, 4); /* Code offset */
831 exp
.X_add_symbol
= fde
->end_address
;
832 exp
.X_op_symbol
= fde
->start_address
; /* Code length */
835 out_uleb128 (0); /* Augmentation size */
837 for (; first
; first
= first
->next
)
838 output_cfi_insn (first
);
840 frag_align (2, 0, 0);
841 symbol_set_value_now (end_address
);
844 static struct cie_entry
*
845 select_cie_for_fde (struct fde_entry
*fde
, struct cfi_insn_data
**pfirst
)
847 struct cfi_insn_data
*i
, *j
;
848 struct cie_entry
*cie
;
850 for (cie
= cie_root
; cie
; cie
= cie
->next
)
852 if (cie
->return_column
!= fde
->return_column
)
854 for (i
= cie
->first
, j
= fde
->data
;
855 i
!= cie
->last
&& j
!= NULL
;
856 i
= i
->next
, j
= j
->next
)
858 if (i
->insn
!= j
->insn
)
862 case DW_CFA_advance_loc
:
863 /* We reached the first advance in the FDE, but did not
864 reach the end of the CIE list. */
869 if (i
->u
.ri
.reg
!= j
->u
.ri
.reg
)
871 if (i
->u
.ri
.offset
!= j
->u
.ri
.offset
)
875 case DW_CFA_register
:
876 if (i
->u
.rr
.reg1
!= j
->u
.rr
.reg1
)
878 if (i
->u
.rr
.reg2
!= j
->u
.rr
.reg2
)
882 case DW_CFA_def_cfa_register
:
884 case DW_CFA_undefined
:
885 case DW_CFA_same_value
:
886 if (i
->u
.r
!= j
->u
.r
)
890 case DW_CFA_def_cfa_offset
:
891 if (i
->u
.i
!= j
->u
.i
)
900 /* Success if we reached the end of the CIE list, and we've either
901 run out of FDE entries or we've encountered an advance. */
902 if (i
== cie
->last
&& (!j
|| j
->insn
== DW_CFA_advance_loc
))
911 cie
= xmalloc (sizeof (struct cie_entry
));
912 cie
->next
= cie_root
;
914 cie
->return_column
= fde
->return_column
;
915 cie
->first
= fde
->data
;
917 for (i
= cie
->first
; i
; i
= i
->next
)
918 if (i
->insn
== DW_CFA_advance_loc
)
933 struct fde_entry
*fde
;
934 int save_flag_traditional_format
;
938 as_bad (_("open CFI at the end of file; missing .cfi_endproc directive"));
939 cur_fde_data
->end_address
= cur_fde_data
->start_address
;
942 if (all_fde_data
== 0)
945 /* Open .eh_frame section. */
946 cfi_seg
= subseg_new (".eh_frame", 0);
948 bfd_set_section_flags (stdoutput
, cfi_seg
,
949 SEC_ALLOC
| SEC_LOAD
| SEC_DATA
| SEC_READONLY
);
951 subseg_set (cfi_seg
, 0);
952 record_alignment (cfi_seg
, 2);
954 /* Make sure check_eh_frame doesn't do anything with our output. */
955 save_flag_traditional_format
= flag_traditional_format
;
956 flag_traditional_format
= 1;
958 for (fde
= all_fde_data
; fde
; fde
= fde
->next
)
960 struct cfi_insn_data
*first
;
961 struct cie_entry
*cie
;
963 cie
= select_cie_for_fde (fde
, &first
);
964 output_fde (fde
, cie
, first
);
967 flag_traditional_format
= save_flag_traditional_format
;