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
;
106 /* Construct a new FDE structure and add it to the end of the fde list. */
108 static struct fde_entry
*
109 alloc_fde_entry (void)
111 struct fde_entry
*fde
= xcalloc (1, sizeof (struct fde_entry
));
114 *last_fde_data
= fde
;
115 last_fde_data
= &fde
->next
;
117 fde
->last
= &fde
->data
;
118 fde
->return_column
= DWARF2_DEFAULT_RETURN_COLUMN
;
123 /* The following functions are available for a backend to construct its
124 own unwind information, usually from legacy unwind directives. */
126 /* Construct a new INSN structure and add it to the end of the insn list
127 for the currently active FDE. */
129 static struct cfi_insn_data
*
130 alloc_cfi_insn_data (void)
132 struct cfi_insn_data
*insn
= xcalloc (1, sizeof (struct cfi_insn_data
));
134 *cur_fde_data
->last
= insn
;
135 cur_fde_data
->last
= &insn
->next
;
140 /* Construct a new FDE structure that begins at LABEL. */
143 cfi_new_fde (symbolS
*label
)
145 struct fde_entry
*fde
= alloc_fde_entry ();
146 fde
->start_address
= label
;
147 last_address
= label
;
150 /* End the currently open FDE. */
153 cfi_end_fde (symbolS
*label
)
155 cur_fde_data
->end_address
= label
;
159 /* Set the return column for the current FDE. */
162 cfi_set_return_column (unsigned regno
)
164 cur_fde_data
->return_column
= regno
;
167 /* Universal functions to store new instructions. */
170 cfi_add_CFA_insn(int insn
)
172 struct cfi_insn_data
*insn_ptr
= alloc_cfi_insn_data ();
174 insn_ptr
->insn
= insn
;
178 cfi_add_CFA_insn_reg (int insn
, unsigned regno
)
180 struct cfi_insn_data
*insn_ptr
= alloc_cfi_insn_data ();
182 insn_ptr
->insn
= insn
;
183 insn_ptr
->u
.r
= regno
;
187 cfi_add_CFA_insn_offset (int insn
, offsetT offset
)
189 struct cfi_insn_data
*insn_ptr
= alloc_cfi_insn_data ();
191 insn_ptr
->insn
= insn
;
192 insn_ptr
->u
.i
= offset
;
196 cfi_add_CFA_insn_reg_reg (int insn
, unsigned reg1
, unsigned reg2
)
198 struct cfi_insn_data
*insn_ptr
= alloc_cfi_insn_data ();
200 insn_ptr
->insn
= insn
;
201 insn_ptr
->u
.rr
.reg1
= reg1
;
202 insn_ptr
->u
.rr
.reg2
= reg2
;
206 cfi_add_CFA_insn_reg_offset (int insn
, unsigned regno
, offsetT offset
)
208 struct cfi_insn_data
*insn_ptr
= alloc_cfi_insn_data ();
210 insn_ptr
->insn
= insn
;
211 insn_ptr
->u
.ri
.reg
= regno
;
212 insn_ptr
->u
.ri
.offset
= offset
;
215 /* Add a CFI insn to advance the PC from the last address to LABEL. */
218 cfi_add_advance_loc (symbolS
*label
)
220 struct cfi_insn_data
*insn
= alloc_cfi_insn_data ();
222 insn
->insn
= DW_CFA_advance_loc
;
223 insn
->u
.ll
.lab1
= last_address
;
224 insn
->u
.ll
.lab2
= label
;
226 last_address
= label
;
229 /* Add a DW_CFA_offset record to the CFI data. */
232 cfi_add_CFA_offset (unsigned regno
, offsetT offset
)
234 cfi_add_CFA_insn_reg_offset (DW_CFA_offset
, regno
, offset
);
237 /* Add a DW_CFA_def_cfa record to the CFI data. */
240 cfi_add_CFA_def_cfa (unsigned regno
, offsetT offset
)
242 cfi_add_CFA_insn_reg_offset (DW_CFA_def_cfa
, regno
, offset
);
243 cur_cfa_offset
= offset
;
246 /* Add a DW_CFA_register record to the CFI data. */
249 cfi_add_CFA_register (unsigned reg1
, unsigned reg2
)
251 cfi_add_CFA_insn_reg_reg (DW_CFA_register
, reg1
, reg2
);
254 /* Add a DW_CFA_def_cfa_register record to the CFI data. */
257 cfi_add_CFA_def_cfa_register (unsigned regno
)
259 cfi_add_CFA_insn_reg (DW_CFA_def_cfa_register
, regno
);
262 /* Add a DW_CFA_def_cfa_offset record to the CFI data. */
265 cfi_add_CFA_def_cfa_offset (offsetT offset
)
267 cfi_add_CFA_insn_offset (DW_CFA_def_cfa_offset
, offset
);
268 cur_cfa_offset
= offset
;
272 cfi_add_CFA_restore (unsigned regno
)
274 cfi_add_CFA_insn_reg (DW_CFA_restore
, regno
);
278 cfi_add_CFA_undefined (unsigned regno
)
280 cfi_add_CFA_insn_reg (DW_CFA_undefined
, regno
);
284 cfi_add_CFA_same_value (unsigned regno
)
286 cfi_add_CFA_insn_reg (DW_CFA_same_value
, regno
);
290 cfi_add_CFA_remember_state (void)
292 cfi_add_CFA_insn (DW_CFA_remember_state
);
296 cfi_add_CFA_restore_state (void)
298 cfi_add_CFA_insn (DW_CFA_restore_state
);
302 cfi_add_CFA_nop (void)
304 cfi_add_CFA_insn (DW_CFA_nop
);
308 /* Parse CFI assembler directives. */
310 static void dot_cfi (int);
311 static void dot_cfi_startproc (int);
312 static void dot_cfi_endproc (int);
314 /* Fake CFI type; outside the byte range of any real CFI insn. */
315 #define CFI_adjust_cfa_offset 0x100
316 #define CFI_return_column 0x101
318 const pseudo_typeS cfi_pseudo_table
[] =
320 { "cfi_startproc", dot_cfi_startproc
, 0 },
321 { "cfi_endproc", dot_cfi_endproc
, 0 },
322 { "cfi_def_cfa", dot_cfi
, DW_CFA_def_cfa
},
323 { "cfi_def_cfa_register", dot_cfi
, DW_CFA_def_cfa_register
},
324 { "cfi_def_cfa_offset", dot_cfi
, DW_CFA_def_cfa_offset
},
325 { "cfi_adjust_cfa_offset", dot_cfi
, CFI_adjust_cfa_offset
},
326 { "cfi_offset", dot_cfi
, DW_CFA_offset
},
327 { "cfi_register", dot_cfi
, DW_CFA_register
},
328 { "cfi_return_column", dot_cfi
, CFI_return_column
},
329 { "cfi_restore", dot_cfi
, DW_CFA_restore
},
330 { "cfi_undefined", dot_cfi
, DW_CFA_undefined
},
331 { "cfi_same_value", dot_cfi
, DW_CFA_same_value
},
332 { "cfi_remember_state", dot_cfi
, DW_CFA_remember_state
},
333 { "cfi_restore_state", dot_cfi
, DW_CFA_restore_state
},
334 { "cfi_nop", dot_cfi
, DW_CFA_nop
},
339 cfi_parse_separator (void)
342 if (*input_line_pointer
== ',')
343 input_line_pointer
++;
345 as_bad (_("missing separator"));
354 #ifdef tc_regname_to_dw2regnum
356 if (is_name_beginner (*input_line_pointer
)
357 || (*input_line_pointer
== '%'
358 && is_name_beginner (*++input_line_pointer
)))
362 name
= input_line_pointer
;
363 c
= get_symbol_end ();
365 if ((regno
= tc_regname_to_dw2regnum (name
)) < 0)
367 as_bad (_("bad register expression"));
371 *input_line_pointer
= c
;
381 regno
= exp
.X_add_number
;
385 as_bad (_("bad register expression"));
394 cfi_parse_const (void)
396 return get_absolute_expression ();
407 as_bad (_("CFI instruction used without previous .cfi_startproc"));
411 /* If the last address was not at the current PC, advance to current. */
412 if (symbol_get_frag (last_address
) != frag_now
413 || S_GET_VALUE (last_address
) != frag_now_fix ())
414 cfi_add_advance_loc (symbol_temp_new_now ());
419 reg1
= cfi_parse_reg ();
420 cfi_parse_separator ();
421 offset
= cfi_parse_const ();
422 cfi_add_CFA_offset (reg1
, offset
);
426 reg1
= cfi_parse_reg ();
427 cfi_parse_separator ();
428 offset
= cfi_parse_const ();
429 cfi_add_CFA_def_cfa (reg1
, offset
);
432 case DW_CFA_register
:
433 reg1
= cfi_parse_reg ();
434 cfi_parse_separator ();
435 reg2
= cfi_parse_reg ();
436 cfi_add_CFA_register (reg1
, reg2
);
439 case DW_CFA_def_cfa_register
:
440 reg1
= cfi_parse_reg ();
441 cfi_add_CFA_def_cfa_register (reg1
);
444 case DW_CFA_def_cfa_offset
:
445 offset
= cfi_parse_const ();
446 cfi_add_CFA_def_cfa_offset (offset
);
449 case CFI_adjust_cfa_offset
:
450 offset
= cfi_parse_const ();
451 cfi_add_CFA_def_cfa_offset (cur_cfa_offset
+ offset
);
455 reg1
= cfi_parse_reg ();
456 cfi_add_CFA_restore (reg1
);
459 case DW_CFA_undefined
:
460 reg1
= cfi_parse_reg ();
461 cfi_add_CFA_undefined (reg1
);
464 case DW_CFA_same_value
:
465 reg1
= cfi_parse_reg ();
466 cfi_add_CFA_same_value (reg1
);
469 case CFI_return_column
:
470 reg1
= cfi_parse_reg ();
471 cfi_set_return_column (reg1
);
474 case DW_CFA_remember_state
:
475 cfi_add_CFA_remember_state ();
478 case DW_CFA_restore_state
:
479 cfi_add_CFA_restore_state ();
490 demand_empty_rest_of_line ();
494 dot_cfi_startproc (int ignored ATTRIBUTE_UNUSED
)
500 as_bad (_("previous CFI entry not closed (missing .cfi_endproc)"));
504 cfi_new_fde (symbol_temp_new_now ());
507 if (is_name_beginner (*input_line_pointer
))
511 name
= input_line_pointer
;
512 c
= get_symbol_end ();
514 if (strcmp (name
, "simple") == 0)
517 *input_line_pointer
= c
;
520 input_line_pointer
= name
;
522 demand_empty_rest_of_line ();
525 tc_cfi_frame_initial_instructions ();
529 dot_cfi_endproc (int ignored ATTRIBUTE_UNUSED
)
533 as_bad (_(".cfi_endproc without corresponding .cfi_startproc"));
537 cfi_end_fde (symbol_temp_new_now ());
541 /* Emit a single byte into the current segment. */
546 FRAG_APPEND_1_CHAR (byte
);
549 /* Emit a two-byte word into the current segment. */
554 md_number_to_chars (frag_more (2), data
, 2);
557 /* Emit a four byte word into the current segment. */
562 md_number_to_chars (frag_more (4), data
, 4);
565 /* Emit an unsigned "little-endian base 128" number. */
568 out_uleb128 (addressT value
)
570 output_leb128 (frag_more (sizeof_leb128 (value
, 0)), value
, 0);
573 /* Emit an unsigned "little-endian base 128" number. */
576 out_sleb128 (offsetT value
)
578 output_leb128 (frag_more (sizeof_leb128 (value
, 1)), value
, 1);
582 output_cfi_insn (struct cfi_insn_data
*insn
)
589 case DW_CFA_advance_loc
:
591 symbolS
*from
= insn
->u
.ll
.lab1
;
592 symbolS
*to
= insn
->u
.ll
.lab2
;
594 if (symbol_get_frag (to
) == symbol_get_frag (from
))
596 addressT delta
= S_GET_VALUE (to
) - S_GET_VALUE (from
);
597 addressT scaled
= delta
/ DWARF2_LINE_MIN_INSN_LENGTH
;
600 out_one (DW_CFA_advance_loc
+ scaled
);
601 else if (delta
<= 0xFF)
603 out_one (DW_CFA_advance_loc1
);
606 else if (delta
<= 0xFFFF)
608 out_one (DW_CFA_advance_loc2
);
613 out_one (DW_CFA_advance_loc4
);
621 exp
.X_op
= O_subtract
;
622 exp
.X_add_symbol
= to
;
623 exp
.X_op_symbol
= from
;
624 exp
.X_add_number
= 0;
626 /* The code in ehopt.c expects that one byte of the encoding
627 is already allocated to the frag. This comes from the way
628 that it scans the .eh_frame section looking first for the
629 .byte DW_CFA_advance_loc4. */
632 frag_var (rs_cfa
, 4, 0, DWARF2_LINE_MIN_INSN_LENGTH
<< 3,
633 make_expr_symbol (&exp
), frag_now_fix () - 1,
640 offset
= insn
->u
.ri
.offset
;
643 out_one (DW_CFA_def_cfa_sf
);
644 out_uleb128 (insn
->u
.ri
.reg
);
645 out_uleb128 (offset
);
649 out_one (DW_CFA_def_cfa
);
650 out_uleb128 (insn
->u
.ri
.reg
);
651 out_uleb128 (offset
);
655 case DW_CFA_def_cfa_register
:
656 case DW_CFA_undefined
:
657 case DW_CFA_same_value
:
658 out_one (insn
->insn
);
659 out_uleb128 (insn
->u
.r
);
662 case DW_CFA_def_cfa_offset
:
666 out_one (DW_CFA_def_cfa_offset_sf
);
667 out_sleb128 (offset
);
671 out_one (DW_CFA_def_cfa_offset
);
672 out_uleb128 (offset
);
680 out_one (DW_CFA_restore
+ regno
);
684 out_one (DW_CFA_restore_extended
);
690 regno
= insn
->u
.ri
.reg
;
691 offset
= insn
->u
.ri
.offset
/ DWARF2_CIE_DATA_ALIGNMENT
;
694 out_one (DW_CFA_offset_extended_sf
);
696 out_sleb128 (offset
);
698 else if (regno
<= 0x3F)
700 out_one (DW_CFA_offset
+ regno
);
701 out_uleb128 (offset
);
705 out_one (DW_CFA_offset_extended
);
707 out_uleb128 (offset
);
711 case DW_CFA_register
:
712 out_one (DW_CFA_register
);
713 out_uleb128 (insn
->u
.rr
.reg1
);
714 out_uleb128 (insn
->u
.rr
.reg2
);
717 case DW_CFA_remember_state
:
718 case DW_CFA_restore_state
:
720 out_one (insn
->insn
);
729 output_cie (struct cie_entry
*cie
)
731 symbolS
*after_size_address
, *end_address
;
733 struct cfi_insn_data
*i
;
735 cie
->start_address
= symbol_temp_new_now ();
736 after_size_address
= symbol_temp_make ();
737 end_address
= symbol_temp_make ();
739 exp
.X_op
= O_subtract
;
740 exp
.X_add_symbol
= end_address
;
741 exp
.X_op_symbol
= after_size_address
;
742 exp
.X_add_number
= 0;
744 emit_expr (&exp
, 4); /* Length */
745 symbol_set_value_now (after_size_address
);
746 out_four (0); /* CIE id */
747 out_one (DW_CIE_VERSION
); /* Version */
748 out_one ('z'); /* Augmentation */
751 out_uleb128 (DWARF2_LINE_MIN_INSN_LENGTH
); /* Code alignment */
752 out_sleb128 (DWARF2_CIE_DATA_ALIGNMENT
); /* Data alignment */
753 out_one (cie
->return_column
); /* Return column */
754 out_uleb128 (1); /* Augmentation size */
755 out_one (DW_EH_PE_pcrel
| DW_EH_PE_sdata4
);
758 for (i
= cie
->first
; i
!= cie
->last
; i
= i
->next
)
761 frag_align (2, 0, 0);
762 symbol_set_value_now (end_address
);
766 output_fde (struct fde_entry
*fde
, struct cie_entry
*cie
,
767 struct cfi_insn_data
*first
)
769 symbolS
*after_size_address
, *end_address
;
772 after_size_address
= symbol_temp_make ();
773 end_address
= symbol_temp_make ();
775 exp
.X_op
= O_subtract
;
776 exp
.X_add_symbol
= end_address
;
777 exp
.X_op_symbol
= after_size_address
;
778 exp
.X_add_number
= 0;
779 emit_expr (&exp
, 4); /* Length */
780 symbol_set_value_now (after_size_address
);
782 exp
.X_add_symbol
= after_size_address
;
783 exp
.X_op_symbol
= cie
->start_address
;
784 emit_expr (&exp
, 4); /* CIE offset */
786 exp
.X_add_symbol
= fde
->start_address
;
787 exp
.X_op_symbol
= symbol_temp_new_now ();
788 emit_expr (&exp
, 4); /* Code offset */
790 exp
.X_add_symbol
= fde
->end_address
;
791 exp
.X_op_symbol
= fde
->start_address
; /* Code length */
794 out_uleb128 (0); /* Augmentation size */
796 for (; first
; first
= first
->next
)
797 output_cfi_insn (first
);
799 frag_align (2, 0, 0);
800 symbol_set_value_now (end_address
);
803 static struct cie_entry
*
804 select_cie_for_fde (struct fde_entry
*fde
, struct cfi_insn_data
**pfirst
)
806 struct cfi_insn_data
*i
, *j
;
807 struct cie_entry
*cie
;
809 for (cie
= cie_root
; cie
; cie
= cie
->next
)
811 if (cie
->return_column
!= fde
->return_column
)
813 for (i
= cie
->first
, j
= fde
->data
;
814 i
!= cie
->last
&& j
!= NULL
;
815 i
= i
->next
, j
= j
->next
)
817 if (i
->insn
!= j
->insn
)
821 case DW_CFA_advance_loc
:
822 /* We reached the first advance in the FDE, but did not
823 reach the end of the CIE list. */
828 if (i
->u
.ri
.reg
!= j
->u
.ri
.reg
)
830 if (i
->u
.ri
.offset
!= j
->u
.ri
.offset
)
834 case DW_CFA_register
:
835 if (i
->u
.rr
.reg1
!= j
->u
.rr
.reg1
)
837 if (i
->u
.rr
.reg2
!= j
->u
.rr
.reg2
)
841 case DW_CFA_def_cfa_register
:
843 case DW_CFA_undefined
:
844 case DW_CFA_same_value
:
845 if (i
->u
.r
!= j
->u
.r
)
849 case DW_CFA_def_cfa_offset
:
850 if (i
->u
.i
!= j
->u
.i
)
859 /* Success if we reached the end of the CIE list, and we've either
860 run out of FDE entries or we've encountered an advance. */
861 if (i
== cie
->last
&& (!j
|| j
->insn
== DW_CFA_advance_loc
))
870 cie
= xmalloc (sizeof (struct cie_entry
));
871 cie
->next
= cie_root
;
873 cie
->return_column
= fde
->return_column
;
874 cie
->first
= fde
->data
;
876 for (i
= cie
->first
; i
; i
= i
->next
)
877 if (i
->insn
== DW_CFA_advance_loc
)
892 struct fde_entry
*fde
;
893 int save_flag_traditional_format
;
897 as_bad (_("open CFI at the end of file; missing .cfi_endproc directive"));
898 cur_fde_data
->end_address
= cur_fde_data
->start_address
;
901 if (all_fde_data
== 0)
904 /* Open .eh_frame section. */
905 cfi_seg
= subseg_new (".eh_frame", 0);
907 bfd_set_section_flags (stdoutput
, cfi_seg
,
908 SEC_ALLOC
| SEC_LOAD
| SEC_DATA
| SEC_READONLY
);
910 subseg_set (cfi_seg
, 0);
911 record_alignment (cfi_seg
, 2);
913 /* Make sure check_eh_frame doesn't do anything with our output. */
914 save_flag_traditional_format
= flag_traditional_format
;
915 flag_traditional_format
= 1;
917 for (fde
= all_fde_data
; fde
; fde
= fde
->next
)
919 struct cfi_insn_data
*first
;
920 struct cie_entry
*cie
;
922 cie
= select_cie_for_fde (fde
, &first
);
923 output_fde (fde
, cie
, first
);
926 flag_traditional_format
= save_flag_traditional_format
;