1 /* dw2gencfi.c - Support for generating Dwarf2 CFI information.
2 Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
4 Contributed by Michal Ludvig <mludvig@suse.cz>
6 This file is part of GAS, the GNU Assembler.
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
24 #include "dw2gencfi.h"
26 #include "dwarf2dbg.h"
28 #ifdef TARGET_USE_CFIPOP
30 /* By default, use difference expressions if DIFF_EXPR_OK is defined. */
31 #ifndef CFI_DIFF_EXPR_OK
33 # define CFI_DIFF_EXPR_OK 1
35 # define CFI_DIFF_EXPR_OK 0
39 #ifndef CFI_DIFF_LSDA_OK
40 # define CFI_DIFF_LSDA_OK CFI_DIFF_EXPR_OK
43 #if CFI_DIFF_EXPR_OK == 1 && CFI_DIFF_LSDA_OK == 0
44 # error "CFI_DIFF_EXPR_OK should imply CFI_DIFF_LSDA_OK"
47 /* We re-use DWARF2_LINE_MIN_INSN_LENGTH for the code alignment field
48 of the CIE. Default to 1 if not otherwise specified. */
49 #ifndef DWARF2_LINE_MIN_INSN_LENGTH
50 # define DWARF2_LINE_MIN_INSN_LENGTH 1
53 /* By default, use 32-bit relocations from .eh_frame into .text. */
54 #ifndef DWARF2_FDE_RELOC_SIZE
55 # define DWARF2_FDE_RELOC_SIZE 4
58 /* By default, use a read-only .eh_frame section. */
59 #ifndef DWARF2_EH_FRAME_READ_ONLY
60 # define DWARF2_EH_FRAME_READ_ONLY SEC_READONLY
63 #ifndef EH_FRAME_ALIGNMENT
64 # define EH_FRAME_ALIGNMENT (bfd_get_arch_size (stdoutput) == 64 ? 3 : 2)
67 #ifndef tc_cfi_frame_initial_instructions
68 # define tc_cfi_frame_initial_instructions() ((void)0)
72 # define DWARF2_FORMAT(SEC) dwarf2_format_32bit
75 #ifndef DWARF2_ADDR_SIZE
76 # define DWARF2_ADDR_SIZE(bfd) (bfd_arch_bits_per_address (bfd) / 8)
79 struct cfi_escape_data
{
80 struct cfi_escape_data
*next
;
86 struct cfi_insn_data
*next
;
107 struct cfi_escape_data
*esc
;
110 unsigned reg
, encoding
;
118 struct fde_entry
*next
;
119 symbolS
*start_address
;
120 symbolS
*end_address
;
121 struct cfi_insn_data
*data
;
122 struct cfi_insn_data
**last
;
123 unsigned char per_encoding
;
124 unsigned char lsda_encoding
;
125 expressionS personality
;
127 unsigned int return_column
;
128 unsigned int signal_frame
;
133 struct cie_entry
*next
;
134 symbolS
*start_address
;
135 unsigned int return_column
;
136 unsigned int signal_frame
;
137 unsigned char per_encoding
;
138 unsigned char lsda_encoding
;
139 expressionS personality
;
140 struct cfi_insn_data
*first
, *last
;
144 /* List of FDE entries. */
145 static struct fde_entry
*all_fde_data
;
146 static struct fde_entry
**last_fde_data
= &all_fde_data
;
148 /* List of CIEs so that they could be reused. */
149 static struct cie_entry
*cie_root
;
151 /* Stack of old CFI data, for save/restore. */
154 struct cfa_save_data
*next
;
158 /* Current open FDE entry. */
161 struct fde_entry
*cur_fde_data
;
162 symbolS
*last_address
;
163 offsetT cur_cfa_offset
;
164 struct cfa_save_data
*cfa_save_stack
;
167 /* Construct a new FDE structure and add it to the end of the fde list. */
169 static struct fde_entry
*
170 alloc_fde_entry (void)
172 struct fde_entry
*fde
= (struct fde_entry
*)
173 xcalloc (1, sizeof (struct fde_entry
));
175 frchain_now
->frch_cfi_data
= (struct frch_cfi_data
*)
176 xcalloc (1, sizeof (struct frch_cfi_data
));
177 frchain_now
->frch_cfi_data
->cur_fde_data
= fde
;
178 *last_fde_data
= fde
;
179 last_fde_data
= &fde
->next
;
181 fde
->last
= &fde
->data
;
182 fde
->return_column
= DWARF2_DEFAULT_RETURN_COLUMN
;
183 fde
->per_encoding
= DW_EH_PE_omit
;
184 fde
->lsda_encoding
= DW_EH_PE_omit
;
189 /* The following functions are available for a backend to construct its
190 own unwind information, usually from legacy unwind directives. */
192 /* Construct a new INSN structure and add it to the end of the insn list
193 for the currently active FDE. */
195 static struct cfi_insn_data
*
196 alloc_cfi_insn_data (void)
198 struct cfi_insn_data
*insn
= (struct cfi_insn_data
*)
199 xcalloc (1, sizeof (struct cfi_insn_data
));
200 struct fde_entry
*cur_fde_data
= frchain_now
->frch_cfi_data
->cur_fde_data
;
202 *cur_fde_data
->last
= insn
;
203 cur_fde_data
->last
= &insn
->next
;
208 /* Construct a new FDE structure that begins at LABEL. */
211 cfi_new_fde (symbolS
*label
)
213 struct fde_entry
*fde
= alloc_fde_entry ();
214 fde
->start_address
= label
;
215 frchain_now
->frch_cfi_data
->last_address
= label
;
218 /* End the currently open FDE. */
221 cfi_end_fde (symbolS
*label
)
223 frchain_now
->frch_cfi_data
->cur_fde_data
->end_address
= label
;
224 free (frchain_now
->frch_cfi_data
);
225 frchain_now
->frch_cfi_data
= NULL
;
228 /* Set the return column for the current FDE. */
231 cfi_set_return_column (unsigned regno
)
233 frchain_now
->frch_cfi_data
->cur_fde_data
->return_column
= regno
;
236 /* Universal functions to store new instructions. */
239 cfi_add_CFA_insn(int insn
)
241 struct cfi_insn_data
*insn_ptr
= alloc_cfi_insn_data ();
243 insn_ptr
->insn
= insn
;
247 cfi_add_CFA_insn_reg (int insn
, unsigned regno
)
249 struct cfi_insn_data
*insn_ptr
= alloc_cfi_insn_data ();
251 insn_ptr
->insn
= insn
;
252 insn_ptr
->u
.r
= regno
;
256 cfi_add_CFA_insn_offset (int insn
, offsetT offset
)
258 struct cfi_insn_data
*insn_ptr
= alloc_cfi_insn_data ();
260 insn_ptr
->insn
= insn
;
261 insn_ptr
->u
.i
= offset
;
265 cfi_add_CFA_insn_reg_reg (int insn
, unsigned reg1
, unsigned reg2
)
267 struct cfi_insn_data
*insn_ptr
= alloc_cfi_insn_data ();
269 insn_ptr
->insn
= insn
;
270 insn_ptr
->u
.rr
.reg1
= reg1
;
271 insn_ptr
->u
.rr
.reg2
= reg2
;
275 cfi_add_CFA_insn_reg_offset (int insn
, unsigned regno
, offsetT offset
)
277 struct cfi_insn_data
*insn_ptr
= alloc_cfi_insn_data ();
279 insn_ptr
->insn
= insn
;
280 insn_ptr
->u
.ri
.reg
= regno
;
281 insn_ptr
->u
.ri
.offset
= offset
;
284 /* Add a CFI insn to advance the PC from the last address to LABEL. */
287 cfi_add_advance_loc (symbolS
*label
)
289 struct cfi_insn_data
*insn
= alloc_cfi_insn_data ();
291 insn
->insn
= DW_CFA_advance_loc
;
292 insn
->u
.ll
.lab1
= frchain_now
->frch_cfi_data
->last_address
;
293 insn
->u
.ll
.lab2
= label
;
295 frchain_now
->frch_cfi_data
->last_address
= label
;
298 /* Add a DW_CFA_offset record to the CFI data. */
301 cfi_add_CFA_offset (unsigned regno
, offsetT offset
)
303 unsigned int abs_data_align
;
305 gas_assert (DWARF2_CIE_DATA_ALIGNMENT
!= 0);
306 cfi_add_CFA_insn_reg_offset (DW_CFA_offset
, regno
, offset
);
308 abs_data_align
= (DWARF2_CIE_DATA_ALIGNMENT
< 0
309 ? -DWARF2_CIE_DATA_ALIGNMENT
: DWARF2_CIE_DATA_ALIGNMENT
);
310 if (offset
% abs_data_align
)
311 as_bad (_("register save offset not a multiple of %u"), abs_data_align
);
314 /* Add a DW_CFA_def_cfa record to the CFI data. */
317 cfi_add_CFA_def_cfa (unsigned regno
, offsetT offset
)
319 cfi_add_CFA_insn_reg_offset (DW_CFA_def_cfa
, regno
, offset
);
320 frchain_now
->frch_cfi_data
->cur_cfa_offset
= offset
;
323 /* Add a DW_CFA_register record to the CFI data. */
326 cfi_add_CFA_register (unsigned reg1
, unsigned reg2
)
328 cfi_add_CFA_insn_reg_reg (DW_CFA_register
, reg1
, reg2
);
331 /* Add a DW_CFA_def_cfa_register record to the CFI data. */
334 cfi_add_CFA_def_cfa_register (unsigned regno
)
336 cfi_add_CFA_insn_reg (DW_CFA_def_cfa_register
, regno
);
339 /* Add a DW_CFA_def_cfa_offset record to the CFI data. */
342 cfi_add_CFA_def_cfa_offset (offsetT offset
)
344 cfi_add_CFA_insn_offset (DW_CFA_def_cfa_offset
, offset
);
345 frchain_now
->frch_cfi_data
->cur_cfa_offset
= offset
;
349 cfi_add_CFA_restore (unsigned regno
)
351 cfi_add_CFA_insn_reg (DW_CFA_restore
, regno
);
355 cfi_add_CFA_undefined (unsigned regno
)
357 cfi_add_CFA_insn_reg (DW_CFA_undefined
, regno
);
361 cfi_add_CFA_same_value (unsigned regno
)
363 cfi_add_CFA_insn_reg (DW_CFA_same_value
, regno
);
367 cfi_add_CFA_remember_state (void)
369 struct cfa_save_data
*p
;
371 cfi_add_CFA_insn (DW_CFA_remember_state
);
373 p
= (struct cfa_save_data
*) xmalloc (sizeof (*p
));
374 p
->cfa_offset
= frchain_now
->frch_cfi_data
->cur_cfa_offset
;
375 p
->next
= frchain_now
->frch_cfi_data
->cfa_save_stack
;
376 frchain_now
->frch_cfi_data
->cfa_save_stack
= p
;
380 cfi_add_CFA_restore_state (void)
382 struct cfa_save_data
*p
;
384 cfi_add_CFA_insn (DW_CFA_restore_state
);
386 p
= frchain_now
->frch_cfi_data
->cfa_save_stack
;
389 frchain_now
->frch_cfi_data
->cur_cfa_offset
= p
->cfa_offset
;
390 frchain_now
->frch_cfi_data
->cfa_save_stack
= p
->next
;
394 as_bad (_("CFI state restore without previous remember"));
398 /* Parse CFI assembler directives. */
400 static void dot_cfi (int);
401 static void dot_cfi_escape (int);
402 static void dot_cfi_sections (int);
403 static void dot_cfi_startproc (int);
404 static void dot_cfi_endproc (int);
405 static void dot_cfi_personality (int);
406 static void dot_cfi_lsda (int);
407 static void dot_cfi_val_encoded_addr (int);
409 /* Fake CFI type; outside the byte range of any real CFI insn. */
410 #define CFI_adjust_cfa_offset 0x100
411 #define CFI_return_column 0x101
412 #define CFI_rel_offset 0x102
413 #define CFI_escape 0x103
414 #define CFI_signal_frame 0x104
415 #define CFI_val_encoded_addr 0x105
417 const pseudo_typeS cfi_pseudo_table
[] =
419 { "cfi_sections", dot_cfi_sections
, 0 },
420 { "cfi_startproc", dot_cfi_startproc
, 0 },
421 { "cfi_endproc", dot_cfi_endproc
, 0 },
422 { "cfi_def_cfa", dot_cfi
, DW_CFA_def_cfa
},
423 { "cfi_def_cfa_register", dot_cfi
, DW_CFA_def_cfa_register
},
424 { "cfi_def_cfa_offset", dot_cfi
, DW_CFA_def_cfa_offset
},
425 { "cfi_adjust_cfa_offset", dot_cfi
, CFI_adjust_cfa_offset
},
426 { "cfi_offset", dot_cfi
, DW_CFA_offset
},
427 { "cfi_rel_offset", dot_cfi
, CFI_rel_offset
},
428 { "cfi_register", dot_cfi
, DW_CFA_register
},
429 { "cfi_return_column", dot_cfi
, CFI_return_column
},
430 { "cfi_restore", dot_cfi
, DW_CFA_restore
},
431 { "cfi_undefined", dot_cfi
, DW_CFA_undefined
},
432 { "cfi_same_value", dot_cfi
, DW_CFA_same_value
},
433 { "cfi_remember_state", dot_cfi
, DW_CFA_remember_state
},
434 { "cfi_restore_state", dot_cfi
, DW_CFA_restore_state
},
435 { "cfi_window_save", dot_cfi
, DW_CFA_GNU_window_save
},
436 { "cfi_escape", dot_cfi_escape
, 0 },
437 { "cfi_signal_frame", dot_cfi
, CFI_signal_frame
},
438 { "cfi_personality", dot_cfi_personality
, 0 },
439 { "cfi_lsda", dot_cfi_lsda
, 0 },
440 { "cfi_val_encoded_addr", dot_cfi_val_encoded_addr
, 0 },
445 cfi_parse_separator (void)
448 if (*input_line_pointer
== ',')
449 input_line_pointer
++;
451 as_bad (_("missing separator"));
454 #ifndef tc_parse_to_dw2regnum
456 tc_parse_to_dw2regnum(expressionS
*exp
)
458 # ifdef tc_regname_to_dw2regnum
460 if (is_name_beginner (*input_line_pointer
)
461 || (*input_line_pointer
== '%'
462 && is_name_beginner (*++input_line_pointer
)))
466 name
= input_line_pointer
;
467 c
= get_symbol_end ();
469 exp
->X_op
= O_constant
;
470 exp
->X_add_number
= tc_regname_to_dw2regnum (name
);
472 *input_line_pointer
= c
;
476 expression_and_evaluate (exp
);
486 tc_parse_to_dw2regnum (&exp
);
491 regno
= exp
.X_add_number
;
501 as_bad (_("bad register expression"));
509 cfi_parse_const (void)
511 return get_absolute_expression ();
520 if (frchain_now
->frch_cfi_data
== NULL
)
522 as_bad (_("CFI instruction used without previous .cfi_startproc"));
523 ignore_rest_of_line ();
527 /* If the last address was not at the current PC, advance to current. */
528 if (symbol_get_frag (frchain_now
->frch_cfi_data
->last_address
) != frag_now
529 || S_GET_VALUE (frchain_now
->frch_cfi_data
->last_address
)
531 cfi_add_advance_loc (symbol_temp_new_now ());
536 reg1
= cfi_parse_reg ();
537 cfi_parse_separator ();
538 offset
= cfi_parse_const ();
539 cfi_add_CFA_offset (reg1
, offset
);
543 reg1
= cfi_parse_reg ();
544 cfi_parse_separator ();
545 offset
= cfi_parse_const ();
546 cfi_add_CFA_offset (reg1
,
547 offset
- frchain_now
->frch_cfi_data
->cur_cfa_offset
);
551 reg1
= cfi_parse_reg ();
552 cfi_parse_separator ();
553 offset
= cfi_parse_const ();
554 cfi_add_CFA_def_cfa (reg1
, offset
);
557 case DW_CFA_register
:
558 reg1
= cfi_parse_reg ();
559 cfi_parse_separator ();
560 reg2
= cfi_parse_reg ();
561 cfi_add_CFA_register (reg1
, reg2
);
564 case DW_CFA_def_cfa_register
:
565 reg1
= cfi_parse_reg ();
566 cfi_add_CFA_def_cfa_register (reg1
);
569 case DW_CFA_def_cfa_offset
:
570 offset
= cfi_parse_const ();
571 cfi_add_CFA_def_cfa_offset (offset
);
574 case CFI_adjust_cfa_offset
:
575 offset
= cfi_parse_const ();
576 cfi_add_CFA_def_cfa_offset (frchain_now
->frch_cfi_data
->cur_cfa_offset
583 reg1
= cfi_parse_reg ();
584 cfi_add_CFA_restore (reg1
);
586 if (*input_line_pointer
!= ',')
588 ++input_line_pointer
;
592 case DW_CFA_undefined
:
595 reg1
= cfi_parse_reg ();
596 cfi_add_CFA_undefined (reg1
);
598 if (*input_line_pointer
!= ',')
600 ++input_line_pointer
;
604 case DW_CFA_same_value
:
605 reg1
= cfi_parse_reg ();
606 cfi_add_CFA_same_value (reg1
);
609 case CFI_return_column
:
610 reg1
= cfi_parse_reg ();
611 cfi_set_return_column (reg1
);
614 case DW_CFA_remember_state
:
615 cfi_add_CFA_remember_state ();
618 case DW_CFA_restore_state
:
619 cfi_add_CFA_restore_state ();
622 case DW_CFA_GNU_window_save
:
623 cfi_add_CFA_insn (DW_CFA_GNU_window_save
);
626 case CFI_signal_frame
:
627 frchain_now
->frch_cfi_data
->cur_fde_data
->signal_frame
= 1;
634 demand_empty_rest_of_line ();
638 dot_cfi_escape (int ignored ATTRIBUTE_UNUSED
)
640 struct cfi_escape_data
*head
, **tail
, *e
;
641 struct cfi_insn_data
*insn
;
643 if (frchain_now
->frch_cfi_data
== NULL
)
645 as_bad (_("CFI instruction used without previous .cfi_startproc"));
646 ignore_rest_of_line ();
650 /* If the last address was not at the current PC, advance to current. */
651 if (symbol_get_frag (frchain_now
->frch_cfi_data
->last_address
) != frag_now
652 || S_GET_VALUE (frchain_now
->frch_cfi_data
->last_address
)
654 cfi_add_advance_loc (symbol_temp_new_now ());
659 e
= (struct cfi_escape_data
*) xmalloc (sizeof (*e
));
660 do_parse_cons_expression (&e
->exp
, 1);
664 while (*input_line_pointer
++ == ',');
667 insn
= alloc_cfi_insn_data ();
668 insn
->insn
= CFI_escape
;
671 --input_line_pointer
;
672 demand_empty_rest_of_line ();
676 dot_cfi_personality (int ignored ATTRIBUTE_UNUSED
)
678 struct fde_entry
*fde
;
681 if (frchain_now
->frch_cfi_data
== NULL
)
683 as_bad (_("CFI instruction used without previous .cfi_startproc"));
684 ignore_rest_of_line ();
688 fde
= frchain_now
->frch_cfi_data
->cur_fde_data
;
689 encoding
= cfi_parse_const ();
690 if (encoding
== DW_EH_PE_omit
)
692 demand_empty_rest_of_line ();
693 fde
->per_encoding
= encoding
;
697 if ((encoding
& 0xff) != encoding
698 || ((encoding
& 0x70) != 0
699 #if CFI_DIFF_EXPR_OK || defined tc_cfi_emit_pcrel_expr
700 && (encoding
& 0x70) != DW_EH_PE_pcrel
703 /* leb128 can be handled, but does something actually need it? */
704 || (encoding
& 7) == DW_EH_PE_uleb128
705 || (encoding
& 7) > DW_EH_PE_udata8
)
707 as_bad (_("invalid or unsupported encoding in .cfi_personality"));
708 ignore_rest_of_line ();
712 if (*input_line_pointer
++ != ',')
714 as_bad (_(".cfi_personality requires encoding and symbol arguments"));
715 ignore_rest_of_line ();
719 expression_and_evaluate (&fde
->personality
);
720 switch (fde
->personality
.X_op
)
725 if ((encoding
& 0x70) == DW_EH_PE_pcrel
)
726 encoding
= DW_EH_PE_omit
;
729 encoding
= DW_EH_PE_omit
;
733 fde
->per_encoding
= encoding
;
735 if (encoding
== DW_EH_PE_omit
)
737 as_bad (_("wrong second argument to .cfi_personality"));
738 ignore_rest_of_line ();
742 demand_empty_rest_of_line ();
746 dot_cfi_lsda (int ignored ATTRIBUTE_UNUSED
)
748 struct fde_entry
*fde
;
751 if (frchain_now
->frch_cfi_data
== NULL
)
753 as_bad (_("CFI instruction used without previous .cfi_startproc"));
754 ignore_rest_of_line ();
758 fde
= frchain_now
->frch_cfi_data
->cur_fde_data
;
759 encoding
= cfi_parse_const ();
760 if (encoding
== DW_EH_PE_omit
)
762 demand_empty_rest_of_line ();
763 fde
->lsda_encoding
= encoding
;
767 if ((encoding
& 0xff) != encoding
768 || ((encoding
& 0x70) != 0
769 #if CFI_DIFF_LSDA_OK || defined tc_cfi_emit_pcrel_expr
770 && (encoding
& 0x70) != DW_EH_PE_pcrel
773 /* leb128 can be handled, but does something actually need it? */
774 || (encoding
& 7) == DW_EH_PE_uleb128
775 || (encoding
& 7) > DW_EH_PE_udata8
)
777 as_bad (_("invalid or unsupported encoding in .cfi_lsda"));
778 ignore_rest_of_line ();
782 if (*input_line_pointer
++ != ',')
784 as_bad (_(".cfi_lsda requires encoding and symbol arguments"));
785 ignore_rest_of_line ();
789 fde
->lsda_encoding
= encoding
;
791 expression_and_evaluate (&fde
->lsda
);
792 switch (fde
->lsda
.X_op
)
797 if ((encoding
& 0x70) == DW_EH_PE_pcrel
)
798 encoding
= DW_EH_PE_omit
;
801 encoding
= DW_EH_PE_omit
;
805 fde
->lsda_encoding
= encoding
;
807 if (encoding
== DW_EH_PE_omit
)
809 as_bad (_("wrong second argument to .cfi_lsda"));
810 ignore_rest_of_line ();
814 demand_empty_rest_of_line ();
818 dot_cfi_val_encoded_addr (int ignored ATTRIBUTE_UNUSED
)
820 struct cfi_insn_data
*insn_ptr
;
823 if (frchain_now
->frch_cfi_data
== NULL
)
825 as_bad (_("CFI instruction used without previous .cfi_startproc"));
826 ignore_rest_of_line ();
830 /* If the last address was not at the current PC, advance to current. */
831 if (symbol_get_frag (frchain_now
->frch_cfi_data
->last_address
) != frag_now
832 || S_GET_VALUE (frchain_now
->frch_cfi_data
->last_address
)
834 cfi_add_advance_loc (symbol_temp_new_now ());
836 insn_ptr
= alloc_cfi_insn_data ();
837 insn_ptr
->insn
= CFI_val_encoded_addr
;
839 insn_ptr
->u
.ea
.reg
= cfi_parse_reg ();
841 cfi_parse_separator ();
842 encoding
= cfi_parse_const ();
843 if ((encoding
& 0xff) != encoding
844 || ((encoding
& 0x70) != 0
845 #if CFI_DIFF_EXPR_OK || defined tc_cfi_emit_pcrel_expr
846 && (encoding
& 0x70) != DW_EH_PE_pcrel
849 /* leb128 can be handled, but does something actually need it? */
850 || (encoding
& 7) == DW_EH_PE_uleb128
851 || (encoding
& 7) > DW_EH_PE_udata8
)
853 as_bad (_("invalid or unsupported encoding in .cfi_lsda"));
854 encoding
= DW_EH_PE_omit
;
857 cfi_parse_separator ();
858 expression_and_evaluate (&insn_ptr
->u
.ea
.exp
);
859 switch (insn_ptr
->u
.ea
.exp
.X_op
)
864 if ((encoding
& 0x70) != DW_EH_PE_pcrel
)
867 encoding
= DW_EH_PE_omit
;
871 insn_ptr
->u
.ea
.encoding
= encoding
;
872 if (encoding
== DW_EH_PE_omit
)
874 as_bad (_("wrong third argument to .cfi_val_encoded_addr"));
875 ignore_rest_of_line ();
879 demand_empty_rest_of_line ();
882 /* By default emit .eh_frame only, not .debug_frame. */
883 #define CFI_EMIT_eh_frame (1 << 0)
884 #define CFI_EMIT_debug_frame (1 << 1)
885 static int cfi_sections
= CFI_EMIT_eh_frame
;
888 dot_cfi_sections (int ignored ATTRIBUTE_UNUSED
)
893 if (is_name_beginner (*input_line_pointer
))
898 name
= input_line_pointer
;
899 c
= get_symbol_end ();
901 if (strcmp (name
, ".eh_frame") == 0)
902 sections
|= CFI_EMIT_eh_frame
;
903 else if (strcmp (name
, ".debug_frame") == 0)
904 sections
|= CFI_EMIT_debug_frame
;
907 *input_line_pointer
= c
;
908 input_line_pointer
= name
;
912 *input_line_pointer
= c
;
914 if (*input_line_pointer
== ',')
916 name
= input_line_pointer
++;
918 if (!is_name_beginner (*input_line_pointer
))
920 input_line_pointer
= name
;
924 else if (is_name_beginner (*input_line_pointer
))
928 demand_empty_rest_of_line ();
929 cfi_sections
= sections
;
933 dot_cfi_startproc (int ignored ATTRIBUTE_UNUSED
)
937 if (frchain_now
->frch_cfi_data
!= NULL
)
939 as_bad (_("previous CFI entry not closed (missing .cfi_endproc)"));
940 ignore_rest_of_line ();
944 cfi_new_fde (symbol_temp_new_now ());
947 if (is_name_beginner (*input_line_pointer
))
951 name
= input_line_pointer
;
952 c
= get_symbol_end ();
954 if (strcmp (name
, "simple") == 0)
957 *input_line_pointer
= c
;
960 input_line_pointer
= name
;
962 demand_empty_rest_of_line ();
964 frchain_now
->frch_cfi_data
->cur_cfa_offset
= 0;
966 tc_cfi_frame_initial_instructions ();
970 dot_cfi_endproc (int ignored ATTRIBUTE_UNUSED
)
972 if (frchain_now
->frch_cfi_data
== NULL
)
974 as_bad (_(".cfi_endproc without corresponding .cfi_startproc"));
975 ignore_rest_of_line ();
979 cfi_end_fde (symbol_temp_new_now ());
981 demand_empty_rest_of_line ();
985 /* Emit a single byte into the current segment. */
990 FRAG_APPEND_1_CHAR (byte
);
993 /* Emit a two-byte word into the current segment. */
998 md_number_to_chars (frag_more (2), data
, 2);
1001 /* Emit a four byte word into the current segment. */
1006 md_number_to_chars (frag_more (4), data
, 4);
1009 /* Emit an unsigned "little-endian base 128" number. */
1012 out_uleb128 (addressT value
)
1014 output_leb128 (frag_more (sizeof_leb128 (value
, 0)), value
, 0);
1017 /* Emit an unsigned "little-endian base 128" number. */
1020 out_sleb128 (offsetT value
)
1022 output_leb128 (frag_more (sizeof_leb128 (value
, 1)), value
, 1);
1026 output_cfi_insn (struct cfi_insn_data
*insn
)
1033 case DW_CFA_advance_loc
:
1035 symbolS
*from
= insn
->u
.ll
.lab1
;
1036 symbolS
*to
= insn
->u
.ll
.lab2
;
1038 if (symbol_get_frag (to
) == symbol_get_frag (from
))
1040 addressT delta
= S_GET_VALUE (to
) - S_GET_VALUE (from
);
1041 addressT scaled
= delta
/ DWARF2_LINE_MIN_INSN_LENGTH
;
1044 out_one (DW_CFA_advance_loc
+ scaled
);
1045 else if (scaled
<= 0xFF)
1047 out_one (DW_CFA_advance_loc1
);
1050 else if (scaled
<= 0xFFFF)
1052 out_one (DW_CFA_advance_loc2
);
1057 out_one (DW_CFA_advance_loc4
);
1065 exp
.X_op
= O_subtract
;
1066 exp
.X_add_symbol
= to
;
1067 exp
.X_op_symbol
= from
;
1068 exp
.X_add_number
= 0;
1070 /* The code in ehopt.c expects that one byte of the encoding
1071 is already allocated to the frag. This comes from the way
1072 that it scans the .eh_frame section looking first for the
1073 .byte DW_CFA_advance_loc4. */
1074 *frag_more (1) = DW_CFA_advance_loc4
;
1076 frag_var (rs_cfa
, 4, 0, DWARF2_LINE_MIN_INSN_LENGTH
<< 3,
1077 make_expr_symbol (&exp
), frag_now_fix () - 1,
1083 case DW_CFA_def_cfa
:
1084 offset
= insn
->u
.ri
.offset
;
1087 out_one (DW_CFA_def_cfa_sf
);
1088 out_uleb128 (insn
->u
.ri
.reg
);
1089 out_sleb128 (offset
/ DWARF2_CIE_DATA_ALIGNMENT
);
1093 out_one (DW_CFA_def_cfa
);
1094 out_uleb128 (insn
->u
.ri
.reg
);
1095 out_uleb128 (offset
);
1099 case DW_CFA_def_cfa_register
:
1100 case DW_CFA_undefined
:
1101 case DW_CFA_same_value
:
1102 out_one (insn
->insn
);
1103 out_uleb128 (insn
->u
.r
);
1106 case DW_CFA_def_cfa_offset
:
1110 out_one (DW_CFA_def_cfa_offset_sf
);
1111 out_sleb128 (offset
/ DWARF2_CIE_DATA_ALIGNMENT
);
1115 out_one (DW_CFA_def_cfa_offset
);
1116 out_uleb128 (offset
);
1120 case DW_CFA_restore
:
1124 out_one (DW_CFA_restore
+ regno
);
1128 out_one (DW_CFA_restore_extended
);
1129 out_uleb128 (regno
);
1134 regno
= insn
->u
.ri
.reg
;
1135 offset
= insn
->u
.ri
.offset
/ DWARF2_CIE_DATA_ALIGNMENT
;
1138 out_one (DW_CFA_offset_extended_sf
);
1139 out_uleb128 (regno
);
1140 out_sleb128 (offset
);
1142 else if (regno
<= 0x3F)
1144 out_one (DW_CFA_offset
+ regno
);
1145 out_uleb128 (offset
);
1149 out_one (DW_CFA_offset_extended
);
1150 out_uleb128 (regno
);
1151 out_uleb128 (offset
);
1155 case DW_CFA_register
:
1156 out_one (DW_CFA_register
);
1157 out_uleb128 (insn
->u
.rr
.reg1
);
1158 out_uleb128 (insn
->u
.rr
.reg2
);
1161 case DW_CFA_remember_state
:
1162 case DW_CFA_restore_state
:
1163 out_one (insn
->insn
);
1166 case DW_CFA_GNU_window_save
:
1167 out_one (DW_CFA_GNU_window_save
);
1172 struct cfi_escape_data
*e
;
1173 for (e
= insn
->u
.esc
; e
; e
= e
->next
)
1174 emit_expr (&e
->exp
, 1);
1178 case CFI_val_encoded_addr
:
1180 unsigned encoding
= insn
->u
.ea
.encoding
;
1181 offsetT encoding_size
;
1183 if (encoding
== DW_EH_PE_omit
)
1185 out_one (DW_CFA_val_expression
);
1186 out_uleb128 (insn
->u
.ea
.reg
);
1188 switch (encoding
& 0x7)
1190 case DW_EH_PE_absptr
:
1191 encoding_size
= DWARF2_ADDR_SIZE (stdoutput
);
1193 case DW_EH_PE_udata2
:
1196 case DW_EH_PE_udata4
:
1199 case DW_EH_PE_udata8
:
1206 /* If the user has requested absolute encoding,
1207 then use the smaller DW_OP_addr encoding. */
1208 if (insn
->u
.ea
.encoding
== DW_EH_PE_absptr
)
1210 out_uleb128 (1 + encoding_size
);
1211 out_one (DW_OP_addr
);
1215 out_uleb128 (1 + 1 + encoding_size
);
1216 out_one (DW_OP_GNU_encoded_addr
);
1219 if ((encoding
& 0x70) == DW_EH_PE_pcrel
)
1221 #if CFI_DIFF_EXPR_OK
1222 insn
->u
.ea
.exp
.X_op
= O_subtract
;
1223 insn
->u
.ea
.exp
.X_op_symbol
= symbol_temp_new_now ();
1224 #elif defined (tc_cfi_emit_pcrel_expr)
1225 tc_cfi_emit_pcrel_expr (&insn
->u
.ea
.exp
, encoding_size
);
1232 emit_expr (&insn
->u
.ea
.exp
, encoding_size
);
1242 encoding_size (unsigned char encoding
)
1244 if (encoding
== DW_EH_PE_omit
)
1246 switch (encoding
& 0x7)
1249 return bfd_get_arch_size (stdoutput
) == 64 ? 8 : 4;
1250 case DW_EH_PE_udata2
:
1252 case DW_EH_PE_udata4
:
1254 case DW_EH_PE_udata8
:
1262 output_cie (struct cie_entry
*cie
, bfd_boolean eh_frame
, int align
)
1264 symbolS
*after_size_address
, *end_address
;
1266 struct cfi_insn_data
*i
;
1267 offsetT augmentation_size
;
1269 enum dwarf2_format fmt
= DWARF2_FORMAT (now_seg
);
1271 cie
->start_address
= symbol_temp_new_now ();
1272 after_size_address
= symbol_temp_make ();
1273 end_address
= symbol_temp_make ();
1275 exp
.X_op
= O_subtract
;
1276 exp
.X_add_symbol
= end_address
;
1277 exp
.X_op_symbol
= after_size_address
;
1278 exp
.X_add_number
= 0;
1280 if (eh_frame
|| fmt
== dwarf2_format_32bit
)
1281 emit_expr (&exp
, 4); /* Length. */
1284 if (fmt
== dwarf2_format_64bit
)
1286 emit_expr (&exp
, 8); /* Length. */
1288 symbol_set_value_now (after_size_address
);
1290 out_four (0); /* CIE id. */
1293 out_four (-1); /* CIE id. */
1294 if (fmt
!= dwarf2_format_32bit
)
1297 out_one (DW_CIE_VERSION
); /* Version. */
1300 out_one ('z'); /* Augmentation. */
1301 if (cie
->per_encoding
!= DW_EH_PE_omit
)
1303 if (cie
->lsda_encoding
!= DW_EH_PE_omit
)
1307 if (cie
->signal_frame
)
1310 out_uleb128 (DWARF2_LINE_MIN_INSN_LENGTH
); /* Code alignment. */
1311 out_sleb128 (DWARF2_CIE_DATA_ALIGNMENT
); /* Data alignment. */
1312 if (DW_CIE_VERSION
== 1) /* Return column. */
1313 out_one (cie
->return_column
);
1315 out_uleb128 (cie
->return_column
);
1318 augmentation_size
= 1 + (cie
->lsda_encoding
!= DW_EH_PE_omit
);
1319 if (cie
->per_encoding
!= DW_EH_PE_omit
)
1320 augmentation_size
+= 1 + encoding_size (cie
->per_encoding
);
1321 out_uleb128 (augmentation_size
); /* Augmentation size. */
1323 if (cie
->per_encoding
!= DW_EH_PE_omit
)
1325 offsetT size
= encoding_size (cie
->per_encoding
);
1326 out_one (cie
->per_encoding
);
1327 exp
= cie
->personality
;
1328 if ((cie
->per_encoding
& 0x70) == DW_EH_PE_pcrel
)
1330 #if CFI_DIFF_EXPR_OK
1331 exp
.X_op
= O_subtract
;
1332 exp
.X_op_symbol
= symbol_temp_new_now ();
1333 emit_expr (&exp
, size
);
1334 #elif defined (tc_cfi_emit_pcrel_expr)
1335 tc_cfi_emit_pcrel_expr (&exp
, size
);
1341 emit_expr (&exp
, size
);
1344 if (cie
->lsda_encoding
!= DW_EH_PE_omit
)
1345 out_one (cie
->lsda_encoding
);
1348 switch (DWARF2_FDE_RELOC_SIZE
)
1351 enc
= DW_EH_PE_sdata2
;
1354 enc
= DW_EH_PE_sdata4
;
1357 enc
= DW_EH_PE_sdata8
;
1362 #if CFI_DIFF_EXPR_OK || defined tc_cfi_emit_pcrel_expr
1363 enc
|= DW_EH_PE_pcrel
;
1369 for (i
= cie
->first
; i
!= cie
->last
; i
= i
->next
)
1370 output_cfi_insn (i
);
1372 frag_align (align
, DW_CFA_nop
, 0);
1373 symbol_set_value_now (end_address
);
1377 output_fde (struct fde_entry
*fde
, struct cie_entry
*cie
,
1378 bfd_boolean eh_frame
, struct cfi_insn_data
*first
,
1381 symbolS
*after_size_address
, *end_address
;
1383 offsetT augmentation_size
;
1384 enum dwarf2_format fmt
= DWARF2_FORMAT (now_seg
);
1388 after_size_address
= symbol_temp_make ();
1389 end_address
= symbol_temp_make ();
1391 exp
.X_op
= O_subtract
;
1392 exp
.X_add_symbol
= end_address
;
1393 exp
.X_op_symbol
= after_size_address
;
1394 exp
.X_add_number
= 0;
1395 if (eh_frame
|| fmt
== dwarf2_format_32bit
)
1399 if (fmt
== dwarf2_format_64bit
)
1403 emit_expr (&exp
, offset_size
); /* Length. */
1404 symbol_set_value_now (after_size_address
);
1408 exp
.X_add_symbol
= after_size_address
;
1409 exp
.X_op_symbol
= cie
->start_address
;
1413 exp
.X_op
= O_symbol
;
1414 exp
.X_add_symbol
= cie
->start_address
;
1415 exp
.X_op_symbol
= NULL
;
1417 emit_expr (&exp
, offset_size
); /* CIE offset. */
1421 #if CFI_DIFF_EXPR_OK
1422 exp
.X_add_symbol
= fde
->start_address
;
1423 exp
.X_op_symbol
= symbol_temp_new_now ();
1424 emit_expr (&exp
, DWARF2_FDE_RELOC_SIZE
); /* Code offset. */
1426 exp
.X_op
= O_symbol
;
1427 exp
.X_add_symbol
= fde
->start_address
;
1428 exp
.X_op_symbol
= NULL
;
1429 #ifdef tc_cfi_emit_pcrel_expr
1430 tc_cfi_emit_pcrel_expr (&exp
, DWARF2_FDE_RELOC_SIZE
); /* Code offset. */
1432 emit_expr (&exp
, DWARF2_FDE_RELOC_SIZE
); /* Code offset. */
1435 addr_size
= DWARF2_FDE_RELOC_SIZE
;
1439 exp
.X_add_symbol
= fde
->start_address
;
1440 addr_size
= DWARF2_ADDR_SIZE (stdoutput
);
1441 emit_expr (&exp
, addr_size
);
1444 exp
.X_op
= O_subtract
;
1445 exp
.X_add_symbol
= fde
->end_address
;
1446 exp
.X_op_symbol
= fde
->start_address
; /* Code length. */
1447 emit_expr (&exp
, addr_size
);
1449 augmentation_size
= encoding_size (fde
->lsda_encoding
);
1451 out_uleb128 (augmentation_size
); /* Augmentation size. */
1453 if (fde
->lsda_encoding
!= DW_EH_PE_omit
)
1456 if ((fde
->lsda_encoding
& 0x70) == DW_EH_PE_pcrel
)
1458 #if CFI_DIFF_LSDA_OK
1459 exp
.X_op
= O_subtract
;
1460 exp
.X_op_symbol
= symbol_temp_new_now ();
1461 emit_expr (&exp
, augmentation_size
);
1462 #elif defined (tc_cfi_emit_pcrel_expr)
1463 tc_cfi_emit_pcrel_expr (&exp
, augmentation_size
);
1469 emit_expr (&exp
, augmentation_size
);
1472 for (; first
; first
= first
->next
)
1473 output_cfi_insn (first
);
1475 frag_align (align
, DW_CFA_nop
, 0);
1476 symbol_set_value_now (end_address
);
1479 static struct cie_entry
*
1480 select_cie_for_fde (struct fde_entry
*fde
, bfd_boolean eh_frame
,
1481 struct cfi_insn_data
**pfirst
, int align
)
1483 struct cfi_insn_data
*i
, *j
;
1484 struct cie_entry
*cie
;
1486 for (cie
= cie_root
; cie
; cie
= cie
->next
)
1488 if (cie
->return_column
!= fde
->return_column
1489 || cie
->signal_frame
!= fde
->signal_frame
1490 || cie
->per_encoding
!= fde
->per_encoding
1491 || cie
->lsda_encoding
!= fde
->lsda_encoding
)
1493 if (cie
->per_encoding
!= DW_EH_PE_omit
)
1495 if (cie
->personality
.X_op
!= fde
->personality
.X_op
1496 || cie
->personality
.X_add_number
1497 != fde
->personality
.X_add_number
)
1499 switch (cie
->personality
.X_op
)
1502 if (cie
->personality
.X_unsigned
!= fde
->personality
.X_unsigned
)
1506 if (cie
->personality
.X_add_symbol
1507 != fde
->personality
.X_add_symbol
)
1514 for (i
= cie
->first
, j
= fde
->data
;
1515 i
!= cie
->last
&& j
!= NULL
;
1516 i
= i
->next
, j
= j
->next
)
1518 if (i
->insn
!= j
->insn
)
1522 case DW_CFA_advance_loc
:
1523 case DW_CFA_remember_state
:
1524 /* We reached the first advance/remember in the FDE,
1525 but did not reach the end of the CIE list. */
1529 case DW_CFA_def_cfa
:
1530 if (i
->u
.ri
.reg
!= j
->u
.ri
.reg
)
1532 if (i
->u
.ri
.offset
!= j
->u
.ri
.offset
)
1536 case DW_CFA_register
:
1537 if (i
->u
.rr
.reg1
!= j
->u
.rr
.reg1
)
1539 if (i
->u
.rr
.reg2
!= j
->u
.rr
.reg2
)
1543 case DW_CFA_def_cfa_register
:
1544 case DW_CFA_restore
:
1545 case DW_CFA_undefined
:
1546 case DW_CFA_same_value
:
1547 if (i
->u
.r
!= j
->u
.r
)
1551 case DW_CFA_def_cfa_offset
:
1552 if (i
->u
.i
!= j
->u
.i
)
1557 case CFI_val_encoded_addr
:
1558 /* Don't bother matching these for now. */
1566 /* Success if we reached the end of the CIE list, and we've either
1567 run out of FDE entries or we've encountered an advance,
1568 remember, or escape. */
1571 || j
->insn
== DW_CFA_advance_loc
1572 || j
->insn
== DW_CFA_remember_state
1573 || j
->insn
== CFI_escape
1574 || j
->insn
== CFI_val_encoded_addr
))
1583 cie
= (struct cie_entry
*) xmalloc (sizeof (struct cie_entry
));
1584 cie
->next
= cie_root
;
1586 cie
->return_column
= fde
->return_column
;
1587 cie
->signal_frame
= fde
->signal_frame
;
1588 cie
->per_encoding
= fde
->per_encoding
;
1589 cie
->lsda_encoding
= fde
->lsda_encoding
;
1590 cie
->personality
= fde
->personality
;
1591 cie
->first
= fde
->data
;
1593 for (i
= cie
->first
; i
; i
= i
->next
)
1594 if (i
->insn
== DW_CFA_advance_loc
1595 || i
->insn
== DW_CFA_remember_state
1596 || i
->insn
== CFI_escape
1597 || i
->insn
== CFI_val_encoded_addr
)
1603 output_cie (cie
, eh_frame
, align
);
1608 #ifdef md_reg_eh_frame_to_debug_frame
1610 cfi_change_reg_numbers (struct cfi_insn_data
*insn
)
1612 for (; insn
; insn
= insn
->next
)
1615 case DW_CFA_advance_loc
:
1616 case DW_CFA_def_cfa_offset
:
1617 case DW_CFA_remember_state
:
1618 case DW_CFA_restore_state
:
1619 case DW_CFA_GNU_window_save
:
1623 case DW_CFA_def_cfa
:
1625 insn
->u
.ri
.reg
= md_reg_eh_frame_to_debug_frame (insn
->u
.ri
.reg
);
1628 case DW_CFA_def_cfa_register
:
1629 case DW_CFA_undefined
:
1630 case DW_CFA_same_value
:
1631 case DW_CFA_restore
:
1632 insn
->u
.r
= md_reg_eh_frame_to_debug_frame (insn
->u
.r
);
1635 case DW_CFA_register
:
1636 insn
->u
.rr
.reg1
= md_reg_eh_frame_to_debug_frame (insn
->u
.rr
.reg1
);
1637 insn
->u
.rr
.reg2
= md_reg_eh_frame_to_debug_frame (insn
->u
.rr
.reg2
);
1640 case CFI_val_encoded_addr
:
1641 insn
->u
.ea
.reg
= md_reg_eh_frame_to_debug_frame (insn
->u
.ea
.reg
);
1649 #define cfi_change_reg_numbers(insn) do { } while (0)
1656 struct fde_entry
*fde
;
1657 int save_flag_traditional_format
;
1659 if (all_fde_data
== 0)
1662 if ((cfi_sections
& CFI_EMIT_eh_frame
) != 0)
1664 /* Open .eh_frame section. */
1665 cfi_seg
= subseg_new (".eh_frame", 0);
1666 bfd_set_section_flags (stdoutput
, cfi_seg
,
1667 SEC_ALLOC
| SEC_LOAD
| SEC_DATA
1668 | DWARF2_EH_FRAME_READ_ONLY
);
1669 subseg_set (cfi_seg
, 0);
1670 record_alignment (cfi_seg
, EH_FRAME_ALIGNMENT
);
1672 #ifdef md_fix_up_eh_frame
1673 md_fix_up_eh_frame (cfi_seg
);
1676 /* Make sure check_eh_frame doesn't do anything with our output. */
1677 save_flag_traditional_format
= flag_traditional_format
;
1678 flag_traditional_format
= 1;
1680 for (fde
= all_fde_data
; fde
; fde
= fde
->next
)
1682 struct cfi_insn_data
*first
;
1683 struct cie_entry
*cie
;
1685 if (fde
->end_address
== NULL
)
1687 as_bad (_("open CFI at the end of file; missing .cfi_endproc directive"));
1688 fde
->end_address
= fde
->start_address
;
1691 cie
= select_cie_for_fde (fde
, TRUE
, &first
, 2);
1692 output_fde (fde
, cie
, TRUE
, first
,
1693 fde
->next
== NULL
? EH_FRAME_ALIGNMENT
: 2);
1696 flag_traditional_format
= save_flag_traditional_format
;
1699 if ((cfi_sections
& CFI_EMIT_debug_frame
) != 0)
1701 struct cie_entry
*cie
, *cie_next
;
1702 int alignment
= ffs (DWARF2_ADDR_SIZE (stdoutput
)) - 1;
1704 for (cie
= cie_root
; cie
; cie
= cie_next
)
1706 cie_next
= cie
->next
;
1707 free ((void *) cie
);
1711 /* Open .debug_frame section. */
1712 cfi_seg
= subseg_new (".debug_frame", 0);
1713 bfd_set_section_flags (stdoutput
, cfi_seg
,
1714 SEC_READONLY
| SEC_DEBUGGING
);
1715 subseg_set (cfi_seg
, 0);
1716 record_alignment (cfi_seg
, alignment
);
1718 for (fde
= all_fde_data
; fde
; fde
= fde
->next
)
1720 struct cfi_insn_data
*first
;
1722 if (fde
->end_address
== NULL
)
1724 as_bad (_("open CFI at the end of file; missing .cfi_endproc directive"));
1725 fde
->end_address
= fde
->start_address
;
1728 fde
->per_encoding
= DW_EH_PE_omit
;
1729 fde
->lsda_encoding
= DW_EH_PE_omit
;
1730 cfi_change_reg_numbers (fde
->data
);
1731 cie
= select_cie_for_fde (fde
, FALSE
, &first
, alignment
);
1732 output_fde (fde
, cie
, FALSE
, first
, alignment
);
1737 #else /* TARGET_USE_CFIPOP */
1742 #endif /* TARGET_USE_CFIPOP */