1 /* dw2gencfi.c - Support for generating Dwarf2 CFI information.
2 Copyright 2003, 2004, 2005, 2006, 2007 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 3, 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, 51 Franklin Street - Fifth Floor, Boston, MA
23 #include "dw2gencfi.h"
27 /* We re-use DWARF2_LINE_MIN_INSN_LENGTH for the code alignment field
28 of the CIE. Default to 1 if not otherwise specified. */
29 #ifndef DWARF2_LINE_MIN_INSN_LENGTH
30 # define DWARF2_LINE_MIN_INSN_LENGTH 1
33 /* If TARGET_USE_CFIPOP is defined, it is required that the target
34 provide the following definitions. Otherwise provide them to
35 allow compilation to continue. */
36 #ifndef TARGET_USE_CFIPOP
37 # ifndef DWARF2_DEFAULT_RETURN_COLUMN
38 # define DWARF2_DEFAULT_RETURN_COLUMN 0
40 # ifndef DWARF2_CIE_DATA_ALIGNMENT
41 # define DWARF2_CIE_DATA_ALIGNMENT 1
45 #ifndef EH_FRAME_ALIGNMENT
46 # define EH_FRAME_ALIGNMENT (bfd_get_arch_size (stdoutput) == 64 ? 3 : 2)
49 #ifndef tc_cfi_frame_initial_instructions
50 # define tc_cfi_frame_initial_instructions() ((void)0)
56 struct cfi_insn_data
*next
;
77 struct cfi_escape_data
{
78 struct cfi_escape_data
*next
;
86 struct fde_entry
*next
;
87 symbolS
*start_address
;
89 struct cfi_insn_data
*data
;
90 struct cfi_insn_data
**last
;
91 unsigned char per_encoding
;
92 unsigned char lsda_encoding
;
93 expressionS personality
;
95 unsigned int return_column
;
96 unsigned int signal_frame
;
101 struct cie_entry
*next
;
102 symbolS
*start_address
;
103 unsigned int return_column
;
104 unsigned int signal_frame
;
105 unsigned char per_encoding
;
106 unsigned char lsda_encoding
;
107 expressionS personality
;
108 struct cfi_insn_data
*first
, *last
;
112 /* List of FDE entries. */
113 static struct fde_entry
*all_fde_data
;
114 static struct fde_entry
**last_fde_data
= &all_fde_data
;
116 /* List of CIEs so that they could be reused. */
117 static struct cie_entry
*cie_root
;
119 /* Stack of old CFI data, for save/restore. */
122 struct cfa_save_data
*next
;
126 /* Current open FDE entry. */
129 struct fde_entry
*cur_fde_data
;
130 symbolS
*last_address
;
131 offsetT cur_cfa_offset
;
132 struct cfa_save_data
*cfa_save_stack
;
135 /* Construct a new FDE structure and add it to the end of the fde list. */
137 static struct fde_entry
*
138 alloc_fde_entry (void)
140 struct fde_entry
*fde
= xcalloc (1, sizeof (struct fde_entry
));
142 frchain_now
->frch_cfi_data
= xcalloc (1, sizeof (struct frch_cfi_data
));
143 frchain_now
->frch_cfi_data
->cur_fde_data
= fde
;
144 *last_fde_data
= fde
;
145 last_fde_data
= &fde
->next
;
147 fde
->last
= &fde
->data
;
148 fde
->return_column
= DWARF2_DEFAULT_RETURN_COLUMN
;
149 fde
->per_encoding
= DW_EH_PE_omit
;
150 fde
->lsda_encoding
= DW_EH_PE_omit
;
155 /* The following functions are available for a backend to construct its
156 own unwind information, usually from legacy unwind directives. */
158 /* Construct a new INSN structure and add it to the end of the insn list
159 for the currently active FDE. */
161 static struct cfi_insn_data
*
162 alloc_cfi_insn_data (void)
164 struct cfi_insn_data
*insn
= xcalloc (1, sizeof (struct cfi_insn_data
));
165 struct fde_entry
*cur_fde_data
= frchain_now
->frch_cfi_data
->cur_fde_data
;
167 *cur_fde_data
->last
= insn
;
168 cur_fde_data
->last
= &insn
->next
;
173 /* Construct a new FDE structure that begins at LABEL. */
176 cfi_new_fde (symbolS
*label
)
178 struct fde_entry
*fde
= alloc_fde_entry ();
179 fde
->start_address
= label
;
180 frchain_now
->frch_cfi_data
->last_address
= label
;
183 /* End the currently open FDE. */
186 cfi_end_fde (symbolS
*label
)
188 frchain_now
->frch_cfi_data
->cur_fde_data
->end_address
= label
;
189 free (frchain_now
->frch_cfi_data
);
190 frchain_now
->frch_cfi_data
= NULL
;
193 /* Set the return column for the current FDE. */
196 cfi_set_return_column (unsigned regno
)
198 frchain_now
->frch_cfi_data
->cur_fde_data
->return_column
= regno
;
201 /* Universal functions to store new instructions. */
204 cfi_add_CFA_insn(int insn
)
206 struct cfi_insn_data
*insn_ptr
= alloc_cfi_insn_data ();
208 insn_ptr
->insn
= insn
;
212 cfi_add_CFA_insn_reg (int insn
, unsigned regno
)
214 struct cfi_insn_data
*insn_ptr
= alloc_cfi_insn_data ();
216 insn_ptr
->insn
= insn
;
217 insn_ptr
->u
.r
= regno
;
221 cfi_add_CFA_insn_offset (int insn
, offsetT offset
)
223 struct cfi_insn_data
*insn_ptr
= alloc_cfi_insn_data ();
225 insn_ptr
->insn
= insn
;
226 insn_ptr
->u
.i
= offset
;
230 cfi_add_CFA_insn_reg_reg (int insn
, unsigned reg1
, unsigned reg2
)
232 struct cfi_insn_data
*insn_ptr
= alloc_cfi_insn_data ();
234 insn_ptr
->insn
= insn
;
235 insn_ptr
->u
.rr
.reg1
= reg1
;
236 insn_ptr
->u
.rr
.reg2
= reg2
;
240 cfi_add_CFA_insn_reg_offset (int insn
, unsigned regno
, offsetT offset
)
242 struct cfi_insn_data
*insn_ptr
= alloc_cfi_insn_data ();
244 insn_ptr
->insn
= insn
;
245 insn_ptr
->u
.ri
.reg
= regno
;
246 insn_ptr
->u
.ri
.offset
= offset
;
249 /* Add a CFI insn to advance the PC from the last address to LABEL. */
252 cfi_add_advance_loc (symbolS
*label
)
254 struct cfi_insn_data
*insn
= alloc_cfi_insn_data ();
256 insn
->insn
= DW_CFA_advance_loc
;
257 insn
->u
.ll
.lab1
= frchain_now
->frch_cfi_data
->last_address
;
258 insn
->u
.ll
.lab2
= label
;
260 frchain_now
->frch_cfi_data
->last_address
= label
;
263 /* Add a DW_CFA_offset record to the CFI data. */
266 cfi_add_CFA_offset (unsigned regno
, offsetT offset
)
268 unsigned int abs_data_align
;
270 assert (DWARF2_CIE_DATA_ALIGNMENT
!= 0);
271 cfi_add_CFA_insn_reg_offset (DW_CFA_offset
, regno
, offset
);
273 abs_data_align
= (DWARF2_CIE_DATA_ALIGNMENT
< 0
274 ? -DWARF2_CIE_DATA_ALIGNMENT
: DWARF2_CIE_DATA_ALIGNMENT
);
275 if (offset
% abs_data_align
)
276 as_bad (_("register save offset not a multiple of %u"), abs_data_align
);
279 /* Add a DW_CFA_def_cfa record to the CFI data. */
282 cfi_add_CFA_def_cfa (unsigned regno
, offsetT offset
)
284 cfi_add_CFA_insn_reg_offset (DW_CFA_def_cfa
, regno
, offset
);
285 frchain_now
->frch_cfi_data
->cur_cfa_offset
= offset
;
288 /* Add a DW_CFA_register record to the CFI data. */
291 cfi_add_CFA_register (unsigned reg1
, unsigned reg2
)
293 cfi_add_CFA_insn_reg_reg (DW_CFA_register
, reg1
, reg2
);
296 /* Add a DW_CFA_def_cfa_register record to the CFI data. */
299 cfi_add_CFA_def_cfa_register (unsigned regno
)
301 cfi_add_CFA_insn_reg (DW_CFA_def_cfa_register
, regno
);
304 /* Add a DW_CFA_def_cfa_offset record to the CFI data. */
307 cfi_add_CFA_def_cfa_offset (offsetT offset
)
309 cfi_add_CFA_insn_offset (DW_CFA_def_cfa_offset
, offset
);
310 frchain_now
->frch_cfi_data
->cur_cfa_offset
= offset
;
314 cfi_add_CFA_restore (unsigned regno
)
316 cfi_add_CFA_insn_reg (DW_CFA_restore
, regno
);
320 cfi_add_CFA_undefined (unsigned regno
)
322 cfi_add_CFA_insn_reg (DW_CFA_undefined
, regno
);
326 cfi_add_CFA_same_value (unsigned regno
)
328 cfi_add_CFA_insn_reg (DW_CFA_same_value
, regno
);
332 cfi_add_CFA_remember_state (void)
334 struct cfa_save_data
*p
;
336 cfi_add_CFA_insn (DW_CFA_remember_state
);
338 p
= xmalloc (sizeof (*p
));
339 p
->cfa_offset
= frchain_now
->frch_cfi_data
->cur_cfa_offset
;
340 p
->next
= frchain_now
->frch_cfi_data
->cfa_save_stack
;
341 frchain_now
->frch_cfi_data
->cfa_save_stack
= p
;
345 cfi_add_CFA_restore_state (void)
347 struct cfa_save_data
*p
;
349 cfi_add_CFA_insn (DW_CFA_restore_state
);
351 p
= frchain_now
->frch_cfi_data
->cfa_save_stack
;
354 frchain_now
->frch_cfi_data
->cur_cfa_offset
= p
->cfa_offset
;
355 frchain_now
->frch_cfi_data
->cfa_save_stack
= p
->next
;
359 as_bad (_("CFI state restore without previous remember"));
363 /* Parse CFI assembler directives. */
365 static void dot_cfi (int);
366 static void dot_cfi_escape (int);
367 static void dot_cfi_startproc (int);
368 static void dot_cfi_endproc (int);
369 static void dot_cfi_personality (int);
370 static void dot_cfi_lsda (int);
372 /* Fake CFI type; outside the byte range of any real CFI insn. */
373 #define CFI_adjust_cfa_offset 0x100
374 #define CFI_return_column 0x101
375 #define CFI_rel_offset 0x102
376 #define CFI_escape 0x103
377 #define CFI_signal_frame 0x104
379 const pseudo_typeS cfi_pseudo_table
[] =
381 { "cfi_startproc", dot_cfi_startproc
, 0 },
382 { "cfi_endproc", dot_cfi_endproc
, 0 },
383 { "cfi_def_cfa", dot_cfi
, DW_CFA_def_cfa
},
384 { "cfi_def_cfa_register", dot_cfi
, DW_CFA_def_cfa_register
},
385 { "cfi_def_cfa_offset", dot_cfi
, DW_CFA_def_cfa_offset
},
386 { "cfi_adjust_cfa_offset", dot_cfi
, CFI_adjust_cfa_offset
},
387 { "cfi_offset", dot_cfi
, DW_CFA_offset
},
388 { "cfi_rel_offset", dot_cfi
, CFI_rel_offset
},
389 { "cfi_register", dot_cfi
, DW_CFA_register
},
390 { "cfi_return_column", dot_cfi
, CFI_return_column
},
391 { "cfi_restore", dot_cfi
, DW_CFA_restore
},
392 { "cfi_undefined", dot_cfi
, DW_CFA_undefined
},
393 { "cfi_same_value", dot_cfi
, DW_CFA_same_value
},
394 { "cfi_remember_state", dot_cfi
, DW_CFA_remember_state
},
395 { "cfi_restore_state", dot_cfi
, DW_CFA_restore_state
},
396 { "cfi_window_save", dot_cfi
, DW_CFA_GNU_window_save
},
397 { "cfi_escape", dot_cfi_escape
, 0 },
398 { "cfi_signal_frame", dot_cfi
, CFI_signal_frame
},
399 { "cfi_personality", dot_cfi_personality
, 0 },
400 { "cfi_lsda", dot_cfi_lsda
, 0 },
405 cfi_parse_separator (void)
408 if (*input_line_pointer
== ',')
409 input_line_pointer
++;
411 as_bad (_("missing separator"));
420 #ifdef tc_regname_to_dw2regnum
422 if (is_name_beginner (*input_line_pointer
)
423 || (*input_line_pointer
== '%'
424 && is_name_beginner (*++input_line_pointer
)))
428 name
= input_line_pointer
;
429 c
= get_symbol_end ();
431 if ((regno
= tc_regname_to_dw2regnum (name
)) < 0)
433 as_bad (_("bad register expression"));
437 *input_line_pointer
= c
;
442 expression_and_evaluate (&exp
);
447 regno
= exp
.X_add_number
;
451 as_bad (_("bad register expression"));
460 cfi_parse_const (void)
462 return get_absolute_expression ();
471 if (frchain_now
->frch_cfi_data
== NULL
)
473 as_bad (_("CFI instruction used without previous .cfi_startproc"));
474 ignore_rest_of_line ();
478 /* If the last address was not at the current PC, advance to current. */
479 if (symbol_get_frag (frchain_now
->frch_cfi_data
->last_address
) != frag_now
480 || S_GET_VALUE (frchain_now
->frch_cfi_data
->last_address
)
482 cfi_add_advance_loc (symbol_temp_new_now ());
487 reg1
= cfi_parse_reg ();
488 cfi_parse_separator ();
489 offset
= cfi_parse_const ();
490 cfi_add_CFA_offset (reg1
, offset
);
494 reg1
= cfi_parse_reg ();
495 cfi_parse_separator ();
496 offset
= cfi_parse_const ();
497 cfi_add_CFA_offset (reg1
,
498 offset
- frchain_now
->frch_cfi_data
->cur_cfa_offset
);
502 reg1
= cfi_parse_reg ();
503 cfi_parse_separator ();
504 offset
= cfi_parse_const ();
505 cfi_add_CFA_def_cfa (reg1
, offset
);
508 case DW_CFA_register
:
509 reg1
= cfi_parse_reg ();
510 cfi_parse_separator ();
511 reg2
= cfi_parse_reg ();
512 cfi_add_CFA_register (reg1
, reg2
);
515 case DW_CFA_def_cfa_register
:
516 reg1
= cfi_parse_reg ();
517 cfi_add_CFA_def_cfa_register (reg1
);
520 case DW_CFA_def_cfa_offset
:
521 offset
= cfi_parse_const ();
522 cfi_add_CFA_def_cfa_offset (offset
);
525 case CFI_adjust_cfa_offset
:
526 offset
= cfi_parse_const ();
527 cfi_add_CFA_def_cfa_offset (frchain_now
->frch_cfi_data
->cur_cfa_offset
534 reg1
= cfi_parse_reg ();
535 cfi_add_CFA_restore (reg1
);
537 if (*input_line_pointer
!= ',')
539 ++input_line_pointer
;
543 case DW_CFA_undefined
:
546 reg1
= cfi_parse_reg ();
547 cfi_add_CFA_undefined (reg1
);
549 if (*input_line_pointer
!= ',')
551 ++input_line_pointer
;
555 case DW_CFA_same_value
:
556 reg1
= cfi_parse_reg ();
557 cfi_add_CFA_same_value (reg1
);
560 case CFI_return_column
:
561 reg1
= cfi_parse_reg ();
562 cfi_set_return_column (reg1
);
565 case DW_CFA_remember_state
:
566 cfi_add_CFA_remember_state ();
569 case DW_CFA_restore_state
:
570 cfi_add_CFA_restore_state ();
573 case DW_CFA_GNU_window_save
:
574 cfi_add_CFA_insn (DW_CFA_GNU_window_save
);
577 case CFI_signal_frame
:
578 frchain_now
->frch_cfi_data
->cur_fde_data
->signal_frame
= 1;
585 demand_empty_rest_of_line ();
589 dot_cfi_escape (int ignored ATTRIBUTE_UNUSED
)
591 struct cfi_escape_data
*head
, **tail
, *e
;
592 struct cfi_insn_data
*insn
;
594 if (frchain_now
->frch_cfi_data
== NULL
)
596 as_bad (_("CFI instruction used without previous .cfi_startproc"));
597 ignore_rest_of_line ();
601 /* If the last address was not at the current PC, advance to current. */
602 if (symbol_get_frag (frchain_now
->frch_cfi_data
->last_address
) != frag_now
603 || S_GET_VALUE (frchain_now
->frch_cfi_data
->last_address
)
605 cfi_add_advance_loc (symbol_temp_new_now ());
610 e
= xmalloc (sizeof (*e
));
611 do_parse_cons_expression (&e
->exp
, 1);
615 while (*input_line_pointer
++ == ',');
618 insn
= alloc_cfi_insn_data ();
619 insn
->insn
= CFI_escape
;
622 --input_line_pointer
;
623 demand_empty_rest_of_line ();
627 dot_cfi_personality (int ignored ATTRIBUTE_UNUSED
)
629 struct fde_entry
*fde
;
632 if (frchain_now
->frch_cfi_data
== NULL
)
634 as_bad (_("CFI instruction used without previous .cfi_startproc"));
635 ignore_rest_of_line ();
639 fde
= frchain_now
->frch_cfi_data
->cur_fde_data
;
640 encoding
= get_absolute_expression ();
641 if (encoding
== DW_EH_PE_omit
)
643 demand_empty_rest_of_line ();
644 fde
->per_encoding
= encoding
;
648 if ((encoding
& 0xff) != encoding
649 || ((encoding
& 0x70) != 0
650 #if defined DIFF_EXPR_OK || defined tc_cfi_emit_pcrel_expr
651 && (encoding
& 0x70) != DW_EH_PE_pcrel
654 /* leb128 can be handled, but does something actually need it? */
655 || (encoding
& 7) == DW_EH_PE_uleb128
656 || (encoding
& 7) > DW_EH_PE_udata8
)
658 as_bad (_("invalid or unsupported encoding in .cfi_personality"));
659 ignore_rest_of_line ();
663 if (*input_line_pointer
++ != ',')
665 as_bad (_(".cfi_personality requires encoding and symbol arguments"));
666 ignore_rest_of_line ();
670 expression_and_evaluate (&fde
->personality
);
671 switch (fde
->personality
.X_op
)
676 if ((encoding
& 0x70) == DW_EH_PE_pcrel
)
677 encoding
= DW_EH_PE_omit
;
680 encoding
= DW_EH_PE_omit
;
684 fde
->per_encoding
= encoding
;
686 if (encoding
== DW_EH_PE_omit
)
688 as_bad (_("wrong second argument to .cfi_personality"));
689 ignore_rest_of_line ();
693 demand_empty_rest_of_line ();
697 dot_cfi_lsda (int ignored ATTRIBUTE_UNUSED
)
699 struct fde_entry
*fde
;
702 if (frchain_now
->frch_cfi_data
== NULL
)
704 as_bad (_("CFI instruction used without previous .cfi_startproc"));
705 ignore_rest_of_line ();
709 fde
= frchain_now
->frch_cfi_data
->cur_fde_data
;
710 encoding
= get_absolute_expression ();
711 if (encoding
== DW_EH_PE_omit
)
713 demand_empty_rest_of_line ();
714 fde
->lsda_encoding
= encoding
;
718 if ((encoding
& 0xff) != encoding
719 || ((encoding
& 0x70) != 0
720 #if defined DIFF_EXPR_OK || defined tc_cfi_emit_pcrel_expr
721 && (encoding
& 0x70) != DW_EH_PE_pcrel
724 /* leb128 can be handled, but does something actually need it? */
725 || (encoding
& 7) == DW_EH_PE_uleb128
726 || (encoding
& 7) > DW_EH_PE_udata8
)
728 as_bad (_("invalid or unsupported encoding in .cfi_lsda"));
729 ignore_rest_of_line ();
733 if (*input_line_pointer
++ != ',')
735 as_bad (_(".cfi_lsda requires encoding and symbol arguments"));
736 ignore_rest_of_line ();
740 fde
->lsda_encoding
= encoding
;
742 expression_and_evaluate (&fde
->lsda
);
743 switch (fde
->lsda
.X_op
)
748 if ((encoding
& 0x70) == DW_EH_PE_pcrel
)
749 encoding
= DW_EH_PE_omit
;
752 encoding
= DW_EH_PE_omit
;
756 fde
->lsda_encoding
= encoding
;
758 if (encoding
== DW_EH_PE_omit
)
760 as_bad (_("wrong second argument to .cfi_lsda"));
761 ignore_rest_of_line ();
765 demand_empty_rest_of_line ();
769 dot_cfi_startproc (int ignored ATTRIBUTE_UNUSED
)
773 if (frchain_now
->frch_cfi_data
!= NULL
)
775 as_bad (_("previous CFI entry not closed (missing .cfi_endproc)"));
776 ignore_rest_of_line ();
780 cfi_new_fde (symbol_temp_new_now ());
783 if (is_name_beginner (*input_line_pointer
))
787 name
= input_line_pointer
;
788 c
= get_symbol_end ();
790 if (strcmp (name
, "simple") == 0)
793 *input_line_pointer
= c
;
796 input_line_pointer
= name
;
798 demand_empty_rest_of_line ();
800 frchain_now
->frch_cfi_data
->cur_cfa_offset
= 0;
802 tc_cfi_frame_initial_instructions ();
806 dot_cfi_endproc (int ignored ATTRIBUTE_UNUSED
)
808 if (frchain_now
->frch_cfi_data
== NULL
)
810 as_bad (_(".cfi_endproc without corresponding .cfi_startproc"));
811 ignore_rest_of_line ();
815 cfi_end_fde (symbol_temp_new_now ());
817 demand_empty_rest_of_line ();
821 /* Emit a single byte into the current segment. */
826 FRAG_APPEND_1_CHAR (byte
);
829 /* Emit a two-byte word into the current segment. */
834 md_number_to_chars (frag_more (2), data
, 2);
837 /* Emit a four byte word into the current segment. */
842 md_number_to_chars (frag_more (4), data
, 4);
845 /* Emit an unsigned "little-endian base 128" number. */
848 out_uleb128 (addressT value
)
850 output_leb128 (frag_more (sizeof_leb128 (value
, 0)), value
, 0);
853 /* Emit an unsigned "little-endian base 128" number. */
856 out_sleb128 (offsetT value
)
858 output_leb128 (frag_more (sizeof_leb128 (value
, 1)), value
, 1);
862 output_cfi_insn (struct cfi_insn_data
*insn
)
869 case DW_CFA_advance_loc
:
871 symbolS
*from
= insn
->u
.ll
.lab1
;
872 symbolS
*to
= insn
->u
.ll
.lab2
;
874 if (symbol_get_frag (to
) == symbol_get_frag (from
))
876 addressT delta
= S_GET_VALUE (to
) - S_GET_VALUE (from
);
877 addressT scaled
= delta
/ DWARF2_LINE_MIN_INSN_LENGTH
;
880 out_one (DW_CFA_advance_loc
+ scaled
);
881 else if (delta
<= 0xFF)
883 out_one (DW_CFA_advance_loc1
);
886 else if (delta
<= 0xFFFF)
888 out_one (DW_CFA_advance_loc2
);
893 out_one (DW_CFA_advance_loc4
);
901 exp
.X_op
= O_subtract
;
902 exp
.X_add_symbol
= to
;
903 exp
.X_op_symbol
= from
;
904 exp
.X_add_number
= 0;
906 /* The code in ehopt.c expects that one byte of the encoding
907 is already allocated to the frag. This comes from the way
908 that it scans the .eh_frame section looking first for the
909 .byte DW_CFA_advance_loc4. */
912 frag_var (rs_cfa
, 4, 0, DWARF2_LINE_MIN_INSN_LENGTH
<< 3,
913 make_expr_symbol (&exp
), frag_now_fix () - 1,
920 offset
= insn
->u
.ri
.offset
;
923 out_one (DW_CFA_def_cfa_sf
);
924 out_uleb128 (insn
->u
.ri
.reg
);
925 out_sleb128 (offset
/ DWARF2_CIE_DATA_ALIGNMENT
);
929 out_one (DW_CFA_def_cfa
);
930 out_uleb128 (insn
->u
.ri
.reg
);
931 out_uleb128 (offset
);
935 case DW_CFA_def_cfa_register
:
936 case DW_CFA_undefined
:
937 case DW_CFA_same_value
:
938 out_one (insn
->insn
);
939 out_uleb128 (insn
->u
.r
);
942 case DW_CFA_def_cfa_offset
:
946 out_one (DW_CFA_def_cfa_offset_sf
);
947 out_sleb128 (offset
/ DWARF2_CIE_DATA_ALIGNMENT
);
951 out_one (DW_CFA_def_cfa_offset
);
952 out_uleb128 (offset
);
960 out_one (DW_CFA_restore
+ regno
);
964 out_one (DW_CFA_restore_extended
);
970 regno
= insn
->u
.ri
.reg
;
971 offset
= insn
->u
.ri
.offset
/ DWARF2_CIE_DATA_ALIGNMENT
;
974 out_one (DW_CFA_offset_extended_sf
);
976 out_sleb128 (offset
);
978 else if (regno
<= 0x3F)
980 out_one (DW_CFA_offset
+ regno
);
981 out_uleb128 (offset
);
985 out_one (DW_CFA_offset_extended
);
987 out_uleb128 (offset
);
991 case DW_CFA_register
:
992 out_one (DW_CFA_register
);
993 out_uleb128 (insn
->u
.rr
.reg1
);
994 out_uleb128 (insn
->u
.rr
.reg2
);
997 case DW_CFA_remember_state
:
998 case DW_CFA_restore_state
:
999 out_one (insn
->insn
);
1002 case DW_CFA_GNU_window_save
:
1003 out_one (DW_CFA_GNU_window_save
);
1008 struct cfi_escape_data
*e
;
1009 for (e
= insn
->u
.esc
; e
; e
= e
->next
)
1010 emit_expr (&e
->exp
, 1);
1020 encoding_size (unsigned char encoding
)
1022 if (encoding
== DW_EH_PE_omit
)
1024 switch (encoding
& 0x7)
1027 return bfd_get_arch_size (stdoutput
) == 64 ? 8 : 4;
1028 case DW_EH_PE_udata2
:
1030 case DW_EH_PE_udata4
:
1032 case DW_EH_PE_udata8
:
1040 output_cie (struct cie_entry
*cie
)
1042 symbolS
*after_size_address
, *end_address
;
1044 struct cfi_insn_data
*i
;
1045 offsetT augmentation_size
;
1047 cie
->start_address
= symbol_temp_new_now ();
1048 after_size_address
= symbol_temp_make ();
1049 end_address
= symbol_temp_make ();
1051 exp
.X_op
= O_subtract
;
1052 exp
.X_add_symbol
= end_address
;
1053 exp
.X_op_symbol
= after_size_address
;
1054 exp
.X_add_number
= 0;
1056 emit_expr (&exp
, 4); /* Length. */
1057 symbol_set_value_now (after_size_address
);
1058 out_four (0); /* CIE id. */
1059 out_one (DW_CIE_VERSION
); /* Version. */
1060 out_one ('z'); /* Augmentation. */
1061 if (cie
->per_encoding
!= DW_EH_PE_omit
)
1063 if (cie
->lsda_encoding
!= DW_EH_PE_omit
)
1066 if (cie
->signal_frame
)
1069 out_uleb128 (DWARF2_LINE_MIN_INSN_LENGTH
); /* Code alignment. */
1070 out_sleb128 (DWARF2_CIE_DATA_ALIGNMENT
); /* Data alignment. */
1071 if (DW_CIE_VERSION
== 1) /* Return column. */
1072 out_one (cie
->return_column
);
1074 out_uleb128 (cie
->return_column
);
1075 augmentation_size
= 1 + (cie
->lsda_encoding
!= DW_EH_PE_omit
);
1076 if (cie
->per_encoding
!= DW_EH_PE_omit
)
1077 augmentation_size
+= 1 + encoding_size (cie
->per_encoding
);
1078 out_uleb128 (augmentation_size
); /* Augmentation size. */
1079 if (cie
->per_encoding
!= DW_EH_PE_omit
)
1081 offsetT size
= encoding_size (cie
->per_encoding
);
1082 out_one (cie
->per_encoding
);
1083 exp
= cie
->personality
;
1084 if ((cie
->per_encoding
& 0x70) == DW_EH_PE_pcrel
)
1087 exp
.X_op
= O_subtract
;
1088 exp
.X_op_symbol
= symbol_temp_new_now ();
1089 emit_expr (&exp
, size
);
1090 #elif defined (tc_cfi_emit_pcrel_expr)
1091 tc_cfi_emit_pcrel_expr (&exp
, size
);
1097 emit_expr (&exp
, size
);
1099 if (cie
->lsda_encoding
!= DW_EH_PE_omit
)
1100 out_one (cie
->lsda_encoding
);
1101 #if defined DIFF_EXPR_OK || defined tc_cfi_emit_pcrel_expr
1102 out_one (DW_EH_PE_pcrel
| DW_EH_PE_sdata4
);
1104 out_one (DW_EH_PE_sdata4
);
1108 for (i
= cie
->first
; i
!= cie
->last
; i
= i
->next
)
1109 output_cfi_insn (i
);
1111 frag_align (2, DW_CFA_nop
, 0);
1112 symbol_set_value_now (end_address
);
1116 output_fde (struct fde_entry
*fde
, struct cie_entry
*cie
,
1117 struct cfi_insn_data
*first
, int align
)
1119 symbolS
*after_size_address
, *end_address
;
1121 offsetT augmentation_size
;
1123 after_size_address
= symbol_temp_make ();
1124 end_address
= symbol_temp_make ();
1126 exp
.X_op
= O_subtract
;
1127 exp
.X_add_symbol
= end_address
;
1128 exp
.X_op_symbol
= after_size_address
;
1129 exp
.X_add_number
= 0;
1130 emit_expr (&exp
, 4); /* Length. */
1131 symbol_set_value_now (after_size_address
);
1133 exp
.X_add_symbol
= after_size_address
;
1134 exp
.X_op_symbol
= cie
->start_address
;
1135 emit_expr (&exp
, 4); /* CIE offset. */
1138 exp
.X_add_symbol
= fde
->start_address
;
1139 exp
.X_op_symbol
= symbol_temp_new_now ();
1140 emit_expr (&exp
, 4); /* Code offset. */
1142 exp
.X_op
= O_symbol
;
1143 exp
.X_add_symbol
= fde
->start_address
;
1144 exp
.X_op_symbol
= NULL
;
1145 #ifdef tc_cfi_emit_pcrel_expr
1146 tc_cfi_emit_pcrel_expr (&exp
, 4); /* Code offset. */
1148 emit_expr (&exp
, 4); /* Code offset. */
1150 exp
.X_op
= O_subtract
;
1153 exp
.X_add_symbol
= fde
->end_address
;
1154 exp
.X_op_symbol
= fde
->start_address
; /* Code length. */
1155 emit_expr (&exp
, 4);
1157 augmentation_size
= encoding_size (fde
->lsda_encoding
);
1158 out_uleb128 (augmentation_size
); /* Augmentation size. */
1160 if (fde
->lsda_encoding
!= DW_EH_PE_omit
)
1163 if ((fde
->lsda_encoding
& 0x70) == DW_EH_PE_pcrel
)
1166 exp
.X_op
= O_subtract
;
1167 exp
.X_op_symbol
= symbol_temp_new_now ();
1168 emit_expr (&exp
, augmentation_size
);
1169 #elif defined (tc_cfi_emit_pcrel_expr)
1170 tc_cfi_emit_pcrel_expr (&exp
, augmentation_size
);
1176 emit_expr (&exp
, augmentation_size
);
1179 for (; first
; first
= first
->next
)
1180 output_cfi_insn (first
);
1182 frag_align (align
, DW_CFA_nop
, 0);
1183 symbol_set_value_now (end_address
);
1186 static struct cie_entry
*
1187 select_cie_for_fde (struct fde_entry
*fde
, struct cfi_insn_data
**pfirst
)
1189 struct cfi_insn_data
*i
, *j
;
1190 struct cie_entry
*cie
;
1192 for (cie
= cie_root
; cie
; cie
= cie
->next
)
1194 if (cie
->return_column
!= fde
->return_column
1195 || cie
->signal_frame
!= fde
->signal_frame
1196 || cie
->per_encoding
!= fde
->per_encoding
1197 || cie
->lsda_encoding
!= fde
->lsda_encoding
)
1199 if (cie
->per_encoding
!= DW_EH_PE_omit
)
1201 if (cie
->personality
.X_op
!= fde
->personality
.X_op
1202 || cie
->personality
.X_add_number
1203 != fde
->personality
.X_add_number
)
1205 switch (cie
->personality
.X_op
)
1208 if (cie
->personality
.X_unsigned
!= fde
->personality
.X_unsigned
)
1212 if (cie
->personality
.X_add_symbol
1213 != fde
->personality
.X_add_symbol
)
1220 for (i
= cie
->first
, j
= fde
->data
;
1221 i
!= cie
->last
&& j
!= NULL
;
1222 i
= i
->next
, j
= j
->next
)
1224 if (i
->insn
!= j
->insn
)
1228 case DW_CFA_advance_loc
:
1229 case DW_CFA_remember_state
:
1230 /* We reached the first advance/remember in the FDE,
1231 but did not reach the end of the CIE list. */
1235 case DW_CFA_def_cfa
:
1236 if (i
->u
.ri
.reg
!= j
->u
.ri
.reg
)
1238 if (i
->u
.ri
.offset
!= j
->u
.ri
.offset
)
1242 case DW_CFA_register
:
1243 if (i
->u
.rr
.reg1
!= j
->u
.rr
.reg1
)
1245 if (i
->u
.rr
.reg2
!= j
->u
.rr
.reg2
)
1249 case DW_CFA_def_cfa_register
:
1250 case DW_CFA_restore
:
1251 case DW_CFA_undefined
:
1252 case DW_CFA_same_value
:
1253 if (i
->u
.r
!= j
->u
.r
)
1257 case DW_CFA_def_cfa_offset
:
1258 if (i
->u
.i
!= j
->u
.i
)
1263 /* Don't bother matching these for now. */
1271 /* Success if we reached the end of the CIE list, and we've either
1272 run out of FDE entries or we've encountered an advance,
1273 remember, or escape. */
1276 || j
->insn
== DW_CFA_advance_loc
1277 || j
->insn
== DW_CFA_remember_state
1278 || j
->insn
== CFI_escape
))
1287 cie
= xmalloc (sizeof (struct cie_entry
));
1288 cie
->next
= cie_root
;
1290 cie
->return_column
= fde
->return_column
;
1291 cie
->signal_frame
= fde
->signal_frame
;
1292 cie
->per_encoding
= fde
->per_encoding
;
1293 cie
->lsda_encoding
= fde
->lsda_encoding
;
1294 cie
->personality
= fde
->personality
;
1295 cie
->first
= fde
->data
;
1297 for (i
= cie
->first
; i
; i
= i
->next
)
1298 if (i
->insn
== DW_CFA_advance_loc
1299 || i
->insn
== DW_CFA_remember_state
1300 || i
->insn
== CFI_escape
)
1315 struct fde_entry
*fde
;
1316 int save_flag_traditional_format
;
1318 if (all_fde_data
== 0)
1321 /* Open .eh_frame section. */
1322 cfi_seg
= subseg_new (".eh_frame", 0);
1323 bfd_set_section_flags (stdoutput
, cfi_seg
,
1324 SEC_ALLOC
| SEC_LOAD
| SEC_DATA
| SEC_READONLY
);
1325 subseg_set (cfi_seg
, 0);
1326 record_alignment (cfi_seg
, EH_FRAME_ALIGNMENT
);
1328 /* Make sure check_eh_frame doesn't do anything with our output. */
1329 save_flag_traditional_format
= flag_traditional_format
;
1330 flag_traditional_format
= 1;
1332 for (fde
= all_fde_data
; fde
; fde
= fde
->next
)
1334 struct cfi_insn_data
*first
;
1335 struct cie_entry
*cie
;
1337 if (fde
->end_address
== NULL
)
1339 as_bad (_("open CFI at the end of file; missing .cfi_endproc directive"));
1340 fde
->end_address
= fde
->start_address
;
1343 cie
= select_cie_for_fde (fde
, &first
);
1344 output_fde (fde
, cie
, first
, fde
->next
== NULL
? EH_FRAME_ALIGNMENT
: 2);
1347 flag_traditional_format
= save_flag_traditional_format
;