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 /* Add a CFI insn to advance the PC from the last address to LABEL. */
170 cfi_add_advance_loc (symbolS
*label
)
172 struct cfi_insn_data
*insn
= alloc_cfi_insn_data ();
174 insn
->insn
= DW_CFA_advance_loc
;
175 insn
->u
.ll
.lab1
= last_address
;
176 insn
->u
.ll
.lab2
= label
;
178 last_address
= label
;
181 /* Add a DW_CFA_offset record to the CFI data. */
184 cfi_add_CFA_offset (unsigned regno
, offsetT offset
)
186 struct cfi_insn_data
*insn
= alloc_cfi_insn_data ();
188 insn
->insn
= DW_CFA_offset
;
189 insn
->u
.ri
.reg
= regno
;
190 insn
->u
.ri
.offset
= offset
;
193 /* Add a DW_CFA_def_cfa record to the CFI data. */
196 cfi_add_CFA_def_cfa (unsigned regno
, offsetT offset
)
198 struct cfi_insn_data
*insn
= alloc_cfi_insn_data ();
200 insn
->insn
= DW_CFA_def_cfa
;
201 insn
->u
.ri
.reg
= regno
;
202 insn
->u
.ri
.offset
= offset
;
204 cur_cfa_offset
= offset
;
207 /* Add a DW_CFA_register record to the CFI data. */
210 cfi_add_CFA_register (unsigned reg1
, unsigned reg2
)
212 struct cfi_insn_data
*insn
= alloc_cfi_insn_data ();
214 insn
->insn
= DW_CFA_register
;
215 insn
->u
.rr
.reg1
= reg1
;
216 insn
->u
.rr
.reg2
= reg2
;
219 /* Add a DW_CFA_def_cfa_register record to the CFI data. */
222 cfi_add_CFA_def_cfa_register (unsigned regno
)
224 struct cfi_insn_data
*insn
= alloc_cfi_insn_data ();
226 insn
->insn
= DW_CFA_def_cfa_register
;
230 /* Add a DW_CFA_def_cfa_offset record to the CFI data. */
233 cfi_add_CFA_def_cfa_offset (offsetT offset
)
235 struct cfi_insn_data
*insn
= alloc_cfi_insn_data ();
237 insn
->insn
= DW_CFA_def_cfa_offset
;
240 cur_cfa_offset
= offset
;
244 /* Parse CFI assembler directives. */
246 static void dot_cfi (int);
247 static void dot_cfi_startproc (int);
248 static void dot_cfi_endproc (int);
250 /* Fake CFI type; outside the byte range of any real CFI insn. */
251 #define CFI_adjust_cfa_offset 0x100
253 const pseudo_typeS cfi_pseudo_table
[] =
255 { "cfi_startproc", dot_cfi_startproc
, 0 },
256 { "cfi_endproc", dot_cfi_endproc
, 0 },
257 { "cfi_def_cfa", dot_cfi
, DW_CFA_def_cfa
},
258 { "cfi_def_cfa_register", dot_cfi
, DW_CFA_def_cfa_register
},
259 { "cfi_def_cfa_offset", dot_cfi
, DW_CFA_def_cfa_offset
},
260 { "cfi_adjust_cfa_offset", dot_cfi
, CFI_adjust_cfa_offset
},
261 { "cfi_offset", dot_cfi
, DW_CFA_offset
},
262 { "cfi_register", dot_cfi
, DW_CFA_register
},
267 cfi_parse_separator (void)
270 if (*input_line_pointer
== ',')
271 input_line_pointer
++;
273 as_bad (_("missing separator"));
282 #ifdef tc_regname_to_dw2regnum
284 if (is_name_beginner (*input_line_pointer
)
285 || (*input_line_pointer
== '%'
286 && is_name_beginner (*++input_line_pointer
)))
290 name
= input_line_pointer
;
291 c
= get_symbol_end ();
293 if ((regno
= tc_regname_to_dw2regnum (name
)) < 0)
295 as_bad (_("bad register expression"));
299 *input_line_pointer
= c
;
309 regno
= exp
.X_add_number
;
313 as_bad (_("bad register expression"));
322 cfi_parse_const (void)
324 return get_absolute_expression ();
335 as_bad (_("CFI instruction used without previous .cfi_startproc"));
339 /* If the last address was not at the current PC, advance to current. */
340 if (symbol_get_frag (last_address
) != frag_now
341 || S_GET_VALUE (last_address
) != frag_now_fix ())
342 cfi_add_advance_loc (symbol_temp_new_now ());
346 /* Instructions that take two arguments (register, integer). */
349 reg1
= cfi_parse_reg ();
350 cfi_parse_separator ();
351 offset
= cfi_parse_const ();
353 if (arg
== DW_CFA_def_cfa
)
354 cfi_add_CFA_def_cfa (reg1
, offset
);
356 cfi_add_CFA_offset (reg1
, offset
);
359 /* Instructions that take two arguments (register, register). */
360 case DW_CFA_register
:
361 reg1
= cfi_parse_reg ();
362 cfi_parse_separator ();
363 reg2
= cfi_parse_reg ();
365 cfi_add_CFA_register (reg1
, reg2
);
368 /* Instructions that take one register argument. */
369 case DW_CFA_def_cfa_register
:
370 reg1
= cfi_parse_reg ();
371 cfi_add_CFA_def_cfa_register (reg1
);
374 /* Instructions that take one integer argument. */
375 case DW_CFA_def_cfa_offset
:
376 offset
= cfi_parse_const ();
377 cfi_add_CFA_def_cfa_offset (offset
);
380 /* Special handling for pseudo-instruction. */
381 case CFI_adjust_cfa_offset
:
382 offset
= cfi_parse_const ();
383 cfi_add_CFA_def_cfa_offset (cur_cfa_offset
+ offset
);
390 demand_empty_rest_of_line ();
394 dot_cfi_startproc (int ignored ATTRIBUTE_UNUSED
)
400 as_bad (_("previous CFI entry not closed (missing .cfi_endproc)"));
404 cfi_new_fde (symbol_temp_new_now ());
407 if (is_name_beginner (*input_line_pointer
))
411 name
= input_line_pointer
;
412 c
= get_symbol_end ();
414 if (strcmp (name
, "simple") == 0)
417 *input_line_pointer
= c
;
420 input_line_pointer
= name
;
422 demand_empty_rest_of_line ();
425 tc_cfi_frame_initial_instructions ();
429 dot_cfi_endproc (int ignored ATTRIBUTE_UNUSED
)
433 as_bad (_(".cfi_endproc without corresponding .cfi_startproc"));
437 cfi_end_fde (symbol_temp_new_now ());
441 /* Emit a single byte into the current segment. */
446 FRAG_APPEND_1_CHAR (byte
);
449 /* Emit a two-byte word into the current segment. */
454 md_number_to_chars (frag_more (2), data
, 2);
457 /* Emit a four byte word into the current segment. */
462 md_number_to_chars (frag_more (4), data
, 4);
465 /* Emit an unsigned "little-endian base 128" number. */
468 out_uleb128 (addressT value
)
470 output_leb128 (frag_more (sizeof_leb128 (value
, 0)), value
, 0);
473 /* Emit an unsigned "little-endian base 128" number. */
476 out_sleb128 (offsetT value
)
478 output_leb128 (frag_more (sizeof_leb128 (value
, 1)), value
, 1);
482 output_cfi_insn (struct cfi_insn_data
*insn
)
489 case DW_CFA_advance_loc
:
491 symbolS
*from
= insn
->u
.ll
.lab1
;
492 symbolS
*to
= insn
->u
.ll
.lab2
;
494 if (symbol_get_frag (to
) == symbol_get_frag (from
))
496 addressT delta
= S_GET_VALUE (to
) - S_GET_VALUE (from
);
497 addressT scaled
= delta
/ DWARF2_LINE_MIN_INSN_LENGTH
;
500 out_one (DW_CFA_advance_loc
+ scaled
);
501 else if (delta
<= 0xFF)
503 out_one (DW_CFA_advance_loc1
);
506 else if (delta
<= 0xFFFF)
508 out_one (DW_CFA_advance_loc2
);
513 out_one (DW_CFA_advance_loc4
);
521 exp
.X_op
= O_subtract
;
522 exp
.X_add_symbol
= to
;
523 exp
.X_op_symbol
= from
;
524 exp
.X_add_number
= 0;
526 /* The code in ehopt.c expects that one byte of the encoding
527 is already allocated to the frag. This comes from the way
528 that it scans the .eh_frame section looking first for the
529 .byte DW_CFA_advance_loc4. */
532 frag_var (rs_cfa
, 4, 0, DWARF2_LINE_MIN_INSN_LENGTH
<< 3,
533 make_expr_symbol (&exp
), frag_now_fix () - 1,
540 offset
= insn
->u
.ri
.offset
;
543 out_one (DW_CFA_def_cfa_sf
);
544 out_uleb128 (insn
->u
.ri
.reg
);
545 out_uleb128 (offset
);
549 out_one (DW_CFA_def_cfa
);
550 out_uleb128 (insn
->u
.ri
.reg
);
551 out_uleb128 (offset
);
555 case DW_CFA_def_cfa_register
:
556 out_one (DW_CFA_def_cfa_register
);
557 out_uleb128 (insn
->u
.i
);
560 case DW_CFA_def_cfa_offset
:
564 out_one (DW_CFA_def_cfa_offset_sf
);
565 out_sleb128 (offset
);
569 out_one (DW_CFA_def_cfa_offset
);
570 out_uleb128 (offset
);
575 regno
= insn
->u
.ri
.reg
;
576 offset
= insn
->u
.ri
.offset
/ DWARF2_CIE_DATA_ALIGNMENT
;
579 out_one (DW_CFA_offset_extended_sf
);
581 out_sleb128 (offset
);
583 else if (regno
<= 0x3F)
585 out_one (DW_CFA_offset
+ regno
);
586 out_uleb128 (offset
);
590 out_one (DW_CFA_offset_extended
);
592 out_uleb128 (offset
);
596 case DW_CFA_register
:
597 out_one (DW_CFA_register
);
598 out_uleb128 (insn
->u
.rr
.reg1
);
599 out_uleb128 (insn
->u
.rr
.reg2
);
603 out_one (DW_CFA_nop
);
612 output_cie (struct cie_entry
*cie
)
614 symbolS
*after_size_address
, *end_address
;
616 struct cfi_insn_data
*i
;
618 cie
->start_address
= symbol_temp_new_now ();
619 after_size_address
= symbol_temp_make ();
620 end_address
= symbol_temp_make ();
622 exp
.X_op
= O_subtract
;
623 exp
.X_add_symbol
= end_address
;
624 exp
.X_op_symbol
= after_size_address
;
625 exp
.X_add_number
= 0;
627 emit_expr (&exp
, 4); /* Length */
628 symbol_set_value_now (after_size_address
);
629 out_four (0); /* CIE id */
630 out_one (DW_CIE_VERSION
); /* Version */
631 out_one ('z'); /* Augmentation */
634 out_uleb128 (DWARF2_LINE_MIN_INSN_LENGTH
); /* Code alignment */
635 out_sleb128 (DWARF2_CIE_DATA_ALIGNMENT
); /* Data alignment */
636 out_one (cie
->return_column
); /* Return column */
637 out_uleb128 (1); /* Augmentation size */
638 out_one (DW_EH_PE_pcrel
| DW_EH_PE_sdata4
);
641 for (i
= cie
->first
; i
!= cie
->last
; i
= i
->next
)
644 frag_align (2, 0, 0);
645 symbol_set_value_now (end_address
);
649 output_fde (struct fde_entry
*fde
, struct cie_entry
*cie
,
650 struct cfi_insn_data
*first
)
652 symbolS
*after_size_address
, *end_address
;
655 after_size_address
= symbol_temp_make ();
656 end_address
= symbol_temp_make ();
658 exp
.X_op
= O_subtract
;
659 exp
.X_add_symbol
= end_address
;
660 exp
.X_op_symbol
= after_size_address
;
661 exp
.X_add_number
= 0;
662 emit_expr (&exp
, 4); /* Length */
663 symbol_set_value_now (after_size_address
);
665 exp
.X_add_symbol
= after_size_address
;
666 exp
.X_op_symbol
= cie
->start_address
;
667 emit_expr (&exp
, 4); /* CIE offset */
669 exp
.X_add_symbol
= fde
->start_address
;
670 exp
.X_op_symbol
= symbol_temp_new_now ();
671 emit_expr (&exp
, 4); /* Code offset */
673 exp
.X_add_symbol
= fde
->end_address
;
674 exp
.X_op_symbol
= fde
->start_address
; /* Code length */
677 out_uleb128 (0); /* Augmentation size */
679 for (; first
; first
= first
->next
)
680 output_cfi_insn (first
);
682 frag_align (2, 0, 0);
683 symbol_set_value_now (end_address
);
686 static struct cie_entry
*
687 select_cie_for_fde (struct fde_entry
*fde
, struct cfi_insn_data
**pfirst
)
689 struct cfi_insn_data
*i
, *j
;
690 struct cie_entry
*cie
;
692 for (cie
= cie_root
; cie
; cie
= cie
->next
)
694 if (cie
->return_column
!= fde
->return_column
)
696 for (i
= cie
->first
, j
= fde
->data
;
697 i
!= cie
->last
&& j
!= NULL
;
698 i
= i
->next
, j
= j
->next
)
700 if (i
->insn
!= j
->insn
)
704 case DW_CFA_advance_loc
:
705 /* We reached the first advance in the FDE, but did not
706 reach the end of the CIE list. */
711 if (i
->u
.ri
.reg
!= j
->u
.ri
.reg
)
713 if (i
->u
.ri
.offset
!= j
->u
.ri
.offset
)
717 case DW_CFA_register
:
718 if (i
->u
.rr
.reg1
!= j
->u
.rr
.reg1
)
720 if (i
->u
.rr
.reg2
!= j
->u
.rr
.reg2
)
724 case DW_CFA_def_cfa_register
:
725 if (i
->u
.r
!= j
->u
.r
)
729 case DW_CFA_def_cfa_offset
:
730 if (i
->u
.i
!= j
->u
.i
)
739 /* Success if we reached the end of the CIE list, and we've either
740 run out of FDE entries or we've encountered an advance. */
741 if (i
== cie
->last
&& (!j
|| j
->insn
== DW_CFA_advance_loc
))
750 cie
= xmalloc (sizeof (struct cie_entry
));
751 cie
->next
= cie_root
;
753 cie
->return_column
= fde
->return_column
;
754 cie
->first
= fde
->data
;
756 for (i
= cie
->first
; i
; i
= i
->next
)
757 if (i
->insn
== DW_CFA_advance_loc
)
772 struct fde_entry
*fde
;
773 int save_flag_traditional_format
;
777 as_bad (_("open CFI at the end of file; missing .cfi_endproc directive"));
778 cur_fde_data
->end_address
= cur_fde_data
->start_address
;
781 if (all_fde_data
== 0)
784 /* Open .eh_frame section. */
785 cfi_seg
= subseg_new (".eh_frame", 0);
787 bfd_set_section_flags (stdoutput
, cfi_seg
,
788 SEC_ALLOC
| SEC_LOAD
| SEC_DATA
| SEC_READONLY
);
790 subseg_set (cfi_seg
, 0);
791 record_alignment (cfi_seg
, 2);
793 /* Make sure check_eh_frame doesn't do anything with our output. */
794 save_flag_traditional_format
= flag_traditional_format
;
795 flag_traditional_format
= 1;
797 for (fde
= all_fde_data
; fde
; fde
= fde
->next
)
799 struct cfi_insn_data
*first
;
800 struct cie_entry
*cie
;
802 cie
= select_cie_for_fde (fde
, &first
);
803 output_fde (fde
, cie
, first
);
806 flag_traditional_format
= save_flag_traditional_format
;