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"
26 #ifdef TARGET_USE_CFIPOP
28 /* We re-use DWARF2_LINE_MIN_INSN_LENGTH for the code alignment field
29 of the CIE. Default to 1 if not otherwise specified. */
30 #ifndef DWARF2_LINE_MIN_INSN_LENGTH
31 # define DWARF2_LINE_MIN_INSN_LENGTH 1
34 /* By default, use 32-bit relocations from .eh_frame into .text. */
35 #ifndef DWARF2_FDE_RELOC_SIZE
36 # define DWARF2_FDE_RELOC_SIZE 4
39 /* By default, use a read-only .eh_frame section. */
40 #ifndef DWARF2_EH_FRAME_READ_ONLY
41 # define DWARF2_EH_FRAME_READ_ONLY SEC_READONLY
44 #ifndef EH_FRAME_ALIGNMENT
45 # define EH_FRAME_ALIGNMENT (bfd_get_arch_size (stdoutput) == 64 ? 3 : 2)
48 #ifndef tc_cfi_frame_initial_instructions
49 # define tc_cfi_frame_initial_instructions() ((void)0)
55 struct cfi_insn_data
*next
;
76 struct cfi_escape_data
{
77 struct cfi_escape_data
*next
;
85 struct fde_entry
*next
;
86 symbolS
*start_address
;
88 struct cfi_insn_data
*data
;
89 struct cfi_insn_data
**last
;
90 unsigned char per_encoding
;
91 unsigned char lsda_encoding
;
92 expressionS personality
;
94 unsigned int return_column
;
95 unsigned int signal_frame
;
100 struct cie_entry
*next
;
101 symbolS
*start_address
;
102 unsigned int return_column
;
103 unsigned int signal_frame
;
104 unsigned char per_encoding
;
105 unsigned char lsda_encoding
;
106 expressionS personality
;
107 struct cfi_insn_data
*first
, *last
;
111 /* List of FDE entries. */
112 static struct fde_entry
*all_fde_data
;
113 static struct fde_entry
**last_fde_data
= &all_fde_data
;
115 /* List of CIEs so that they could be reused. */
116 static struct cie_entry
*cie_root
;
118 /* Stack of old CFI data, for save/restore. */
121 struct cfa_save_data
*next
;
125 /* Current open FDE entry. */
128 struct fde_entry
*cur_fde_data
;
129 symbolS
*last_address
;
130 offsetT cur_cfa_offset
;
131 struct cfa_save_data
*cfa_save_stack
;
134 /* Construct a new FDE structure and add it to the end of the fde list. */
136 static struct fde_entry
*
137 alloc_fde_entry (void)
139 struct fde_entry
*fde
= xcalloc (1, sizeof (struct fde_entry
));
141 frchain_now
->frch_cfi_data
= xcalloc (1, sizeof (struct frch_cfi_data
));
142 frchain_now
->frch_cfi_data
->cur_fde_data
= fde
;
143 *last_fde_data
= fde
;
144 last_fde_data
= &fde
->next
;
146 fde
->last
= &fde
->data
;
147 fde
->return_column
= DWARF2_DEFAULT_RETURN_COLUMN
;
148 fde
->per_encoding
= DW_EH_PE_omit
;
149 fde
->lsda_encoding
= DW_EH_PE_omit
;
154 /* The following functions are available for a backend to construct its
155 own unwind information, usually from legacy unwind directives. */
157 /* Construct a new INSN structure and add it to the end of the insn list
158 for the currently active FDE. */
160 static struct cfi_insn_data
*
161 alloc_cfi_insn_data (void)
163 struct cfi_insn_data
*insn
= xcalloc (1, sizeof (struct cfi_insn_data
));
164 struct fde_entry
*cur_fde_data
= frchain_now
->frch_cfi_data
->cur_fde_data
;
166 *cur_fde_data
->last
= insn
;
167 cur_fde_data
->last
= &insn
->next
;
172 /* Construct a new FDE structure that begins at LABEL. */
175 cfi_new_fde (symbolS
*label
)
177 struct fde_entry
*fde
= alloc_fde_entry ();
178 fde
->start_address
= label
;
179 frchain_now
->frch_cfi_data
->last_address
= label
;
182 /* End the currently open FDE. */
185 cfi_end_fde (symbolS
*label
)
187 frchain_now
->frch_cfi_data
->cur_fde_data
->end_address
= label
;
188 free (frchain_now
->frch_cfi_data
);
189 frchain_now
->frch_cfi_data
= NULL
;
192 /* Set the return column for the current FDE. */
195 cfi_set_return_column (unsigned regno
)
197 frchain_now
->frch_cfi_data
->cur_fde_data
->return_column
= regno
;
200 /* Universal functions to store new instructions. */
203 cfi_add_CFA_insn(int insn
)
205 struct cfi_insn_data
*insn_ptr
= alloc_cfi_insn_data ();
207 insn_ptr
->insn
= insn
;
211 cfi_add_CFA_insn_reg (int insn
, unsigned regno
)
213 struct cfi_insn_data
*insn_ptr
= alloc_cfi_insn_data ();
215 insn_ptr
->insn
= insn
;
216 insn_ptr
->u
.r
= regno
;
220 cfi_add_CFA_insn_offset (int insn
, offsetT offset
)
222 struct cfi_insn_data
*insn_ptr
= alloc_cfi_insn_data ();
224 insn_ptr
->insn
= insn
;
225 insn_ptr
->u
.i
= offset
;
229 cfi_add_CFA_insn_reg_reg (int insn
, unsigned reg1
, unsigned reg2
)
231 struct cfi_insn_data
*insn_ptr
= alloc_cfi_insn_data ();
233 insn_ptr
->insn
= insn
;
234 insn_ptr
->u
.rr
.reg1
= reg1
;
235 insn_ptr
->u
.rr
.reg2
= reg2
;
239 cfi_add_CFA_insn_reg_offset (int insn
, unsigned regno
, offsetT offset
)
241 struct cfi_insn_data
*insn_ptr
= alloc_cfi_insn_data ();
243 insn_ptr
->insn
= insn
;
244 insn_ptr
->u
.ri
.reg
= regno
;
245 insn_ptr
->u
.ri
.offset
= offset
;
248 /* Add a CFI insn to advance the PC from the last address to LABEL. */
251 cfi_add_advance_loc (symbolS
*label
)
253 struct cfi_insn_data
*insn
= alloc_cfi_insn_data ();
255 insn
->insn
= DW_CFA_advance_loc
;
256 insn
->u
.ll
.lab1
= frchain_now
->frch_cfi_data
->last_address
;
257 insn
->u
.ll
.lab2
= label
;
259 frchain_now
->frch_cfi_data
->last_address
= label
;
262 /* Add a DW_CFA_offset record to the CFI data. */
265 cfi_add_CFA_offset (unsigned regno
, offsetT offset
)
267 unsigned int abs_data_align
;
269 assert (DWARF2_CIE_DATA_ALIGNMENT
!= 0);
270 cfi_add_CFA_insn_reg_offset (DW_CFA_offset
, regno
, offset
);
272 abs_data_align
= (DWARF2_CIE_DATA_ALIGNMENT
< 0
273 ? -DWARF2_CIE_DATA_ALIGNMENT
: DWARF2_CIE_DATA_ALIGNMENT
);
274 if (offset
% abs_data_align
)
275 as_bad (_("register save offset not a multiple of %u"), abs_data_align
);
278 /* Add a DW_CFA_def_cfa record to the CFI data. */
281 cfi_add_CFA_def_cfa (unsigned regno
, offsetT offset
)
283 cfi_add_CFA_insn_reg_offset (DW_CFA_def_cfa
, regno
, offset
);
284 frchain_now
->frch_cfi_data
->cur_cfa_offset
= offset
;
287 /* Add a DW_CFA_register record to the CFI data. */
290 cfi_add_CFA_register (unsigned reg1
, unsigned reg2
)
292 cfi_add_CFA_insn_reg_reg (DW_CFA_register
, reg1
, reg2
);
295 /* Add a DW_CFA_def_cfa_register record to the CFI data. */
298 cfi_add_CFA_def_cfa_register (unsigned regno
)
300 cfi_add_CFA_insn_reg (DW_CFA_def_cfa_register
, regno
);
303 /* Add a DW_CFA_def_cfa_offset record to the CFI data. */
306 cfi_add_CFA_def_cfa_offset (offsetT offset
)
308 cfi_add_CFA_insn_offset (DW_CFA_def_cfa_offset
, offset
);
309 frchain_now
->frch_cfi_data
->cur_cfa_offset
= offset
;
313 cfi_add_CFA_restore (unsigned regno
)
315 cfi_add_CFA_insn_reg (DW_CFA_restore
, regno
);
319 cfi_add_CFA_undefined (unsigned regno
)
321 cfi_add_CFA_insn_reg (DW_CFA_undefined
, regno
);
325 cfi_add_CFA_same_value (unsigned regno
)
327 cfi_add_CFA_insn_reg (DW_CFA_same_value
, regno
);
331 cfi_add_CFA_remember_state (void)
333 struct cfa_save_data
*p
;
335 cfi_add_CFA_insn (DW_CFA_remember_state
);
337 p
= xmalloc (sizeof (*p
));
338 p
->cfa_offset
= frchain_now
->frch_cfi_data
->cur_cfa_offset
;
339 p
->next
= frchain_now
->frch_cfi_data
->cfa_save_stack
;
340 frchain_now
->frch_cfi_data
->cfa_save_stack
= p
;
344 cfi_add_CFA_restore_state (void)
346 struct cfa_save_data
*p
;
348 cfi_add_CFA_insn (DW_CFA_restore_state
);
350 p
= frchain_now
->frch_cfi_data
->cfa_save_stack
;
353 frchain_now
->frch_cfi_data
->cur_cfa_offset
= p
->cfa_offset
;
354 frchain_now
->frch_cfi_data
->cfa_save_stack
= p
->next
;
358 as_bad (_("CFI state restore without previous remember"));
362 /* Parse CFI assembler directives. */
364 static void dot_cfi (int);
365 static void dot_cfi_escape (int);
366 static void dot_cfi_startproc (int);
367 static void dot_cfi_endproc (int);
368 static void dot_cfi_personality (int);
369 static void dot_cfi_lsda (int);
371 /* Fake CFI type; outside the byte range of any real CFI insn. */
372 #define CFI_adjust_cfa_offset 0x100
373 #define CFI_return_column 0x101
374 #define CFI_rel_offset 0x102
375 #define CFI_escape 0x103
376 #define CFI_signal_frame 0x104
378 const pseudo_typeS cfi_pseudo_table
[] =
380 { "cfi_startproc", dot_cfi_startproc
, 0 },
381 { "cfi_endproc", dot_cfi_endproc
, 0 },
382 { "cfi_def_cfa", dot_cfi
, DW_CFA_def_cfa
},
383 { "cfi_def_cfa_register", dot_cfi
, DW_CFA_def_cfa_register
},
384 { "cfi_def_cfa_offset", dot_cfi
, DW_CFA_def_cfa_offset
},
385 { "cfi_adjust_cfa_offset", dot_cfi
, CFI_adjust_cfa_offset
},
386 { "cfi_offset", dot_cfi
, DW_CFA_offset
},
387 { "cfi_rel_offset", dot_cfi
, CFI_rel_offset
},
388 { "cfi_register", dot_cfi
, DW_CFA_register
},
389 { "cfi_return_column", dot_cfi
, CFI_return_column
},
390 { "cfi_restore", dot_cfi
, DW_CFA_restore
},
391 { "cfi_undefined", dot_cfi
, DW_CFA_undefined
},
392 { "cfi_same_value", dot_cfi
, DW_CFA_same_value
},
393 { "cfi_remember_state", dot_cfi
, DW_CFA_remember_state
},
394 { "cfi_restore_state", dot_cfi
, DW_CFA_restore_state
},
395 { "cfi_window_save", dot_cfi
, DW_CFA_GNU_window_save
},
396 { "cfi_escape", dot_cfi_escape
, 0 },
397 { "cfi_signal_frame", dot_cfi
, CFI_signal_frame
},
398 { "cfi_personality", dot_cfi_personality
, 0 },
399 { "cfi_lsda", dot_cfi_lsda
, 0 },
404 cfi_parse_separator (void)
407 if (*input_line_pointer
== ',')
408 input_line_pointer
++;
410 as_bad (_("missing separator"));
413 #ifndef tc_parse_to_dw2regnum
415 tc_parse_to_dw2regnum(expressionS
*exp
)
417 # ifdef tc_regname_to_dw2regnum
419 if (is_name_beginner (*input_line_pointer
)
420 || (*input_line_pointer
== '%'
421 && is_name_beginner (*++input_line_pointer
)))
425 name
= input_line_pointer
;
426 c
= get_symbol_end ();
428 exp
->X_op
= O_constant
;
429 exp
->X_add_number
= tc_regname_to_dw2regnum (name
);
431 *input_line_pointer
= c
;
435 expression_and_evaluate (exp
);
445 tc_parse_to_dw2regnum (&exp
);
450 regno
= exp
.X_add_number
;
460 as_bad (_("bad register expression"));
468 cfi_parse_const (void)
470 return get_absolute_expression ();
479 if (frchain_now
->frch_cfi_data
== NULL
)
481 as_bad (_("CFI instruction used without previous .cfi_startproc"));
482 ignore_rest_of_line ();
486 /* If the last address was not at the current PC, advance to current. */
487 if (symbol_get_frag (frchain_now
->frch_cfi_data
->last_address
) != frag_now
488 || S_GET_VALUE (frchain_now
->frch_cfi_data
->last_address
)
490 cfi_add_advance_loc (symbol_temp_new_now ());
495 reg1
= cfi_parse_reg ();
496 cfi_parse_separator ();
497 offset
= cfi_parse_const ();
498 cfi_add_CFA_offset (reg1
, offset
);
502 reg1
= cfi_parse_reg ();
503 cfi_parse_separator ();
504 offset
= cfi_parse_const ();
505 cfi_add_CFA_offset (reg1
,
506 offset
- frchain_now
->frch_cfi_data
->cur_cfa_offset
);
510 reg1
= cfi_parse_reg ();
511 cfi_parse_separator ();
512 offset
= cfi_parse_const ();
513 cfi_add_CFA_def_cfa (reg1
, offset
);
516 case DW_CFA_register
:
517 reg1
= cfi_parse_reg ();
518 cfi_parse_separator ();
519 reg2
= cfi_parse_reg ();
520 cfi_add_CFA_register (reg1
, reg2
);
523 case DW_CFA_def_cfa_register
:
524 reg1
= cfi_parse_reg ();
525 cfi_add_CFA_def_cfa_register (reg1
);
528 case DW_CFA_def_cfa_offset
:
529 offset
= cfi_parse_const ();
530 cfi_add_CFA_def_cfa_offset (offset
);
533 case CFI_adjust_cfa_offset
:
534 offset
= cfi_parse_const ();
535 cfi_add_CFA_def_cfa_offset (frchain_now
->frch_cfi_data
->cur_cfa_offset
542 reg1
= cfi_parse_reg ();
543 cfi_add_CFA_restore (reg1
);
545 if (*input_line_pointer
!= ',')
547 ++input_line_pointer
;
551 case DW_CFA_undefined
:
554 reg1
= cfi_parse_reg ();
555 cfi_add_CFA_undefined (reg1
);
557 if (*input_line_pointer
!= ',')
559 ++input_line_pointer
;
563 case DW_CFA_same_value
:
564 reg1
= cfi_parse_reg ();
565 cfi_add_CFA_same_value (reg1
);
568 case CFI_return_column
:
569 reg1
= cfi_parse_reg ();
570 cfi_set_return_column (reg1
);
573 case DW_CFA_remember_state
:
574 cfi_add_CFA_remember_state ();
577 case DW_CFA_restore_state
:
578 cfi_add_CFA_restore_state ();
581 case DW_CFA_GNU_window_save
:
582 cfi_add_CFA_insn (DW_CFA_GNU_window_save
);
585 case CFI_signal_frame
:
586 frchain_now
->frch_cfi_data
->cur_fde_data
->signal_frame
= 1;
593 demand_empty_rest_of_line ();
597 dot_cfi_escape (int ignored ATTRIBUTE_UNUSED
)
599 struct cfi_escape_data
*head
, **tail
, *e
;
600 struct cfi_insn_data
*insn
;
602 if (frchain_now
->frch_cfi_data
== NULL
)
604 as_bad (_("CFI instruction used without previous .cfi_startproc"));
605 ignore_rest_of_line ();
609 /* If the last address was not at the current PC, advance to current. */
610 if (symbol_get_frag (frchain_now
->frch_cfi_data
->last_address
) != frag_now
611 || S_GET_VALUE (frchain_now
->frch_cfi_data
->last_address
)
613 cfi_add_advance_loc (symbol_temp_new_now ());
618 e
= xmalloc (sizeof (*e
));
619 do_parse_cons_expression (&e
->exp
, 1);
623 while (*input_line_pointer
++ == ',');
626 insn
= alloc_cfi_insn_data ();
627 insn
->insn
= CFI_escape
;
630 --input_line_pointer
;
631 demand_empty_rest_of_line ();
635 dot_cfi_personality (int ignored ATTRIBUTE_UNUSED
)
637 struct fde_entry
*fde
;
640 if (frchain_now
->frch_cfi_data
== NULL
)
642 as_bad (_("CFI instruction used without previous .cfi_startproc"));
643 ignore_rest_of_line ();
647 fde
= frchain_now
->frch_cfi_data
->cur_fde_data
;
648 encoding
= get_absolute_expression ();
649 if (encoding
== DW_EH_PE_omit
)
651 demand_empty_rest_of_line ();
652 fde
->per_encoding
= encoding
;
656 if ((encoding
& 0xff) != encoding
657 || ((encoding
& 0x70) != 0
658 #if defined DIFF_EXPR_OK || defined tc_cfi_emit_pcrel_expr
659 && (encoding
& 0x70) != DW_EH_PE_pcrel
662 /* leb128 can be handled, but does something actually need it? */
663 || (encoding
& 7) == DW_EH_PE_uleb128
664 || (encoding
& 7) > DW_EH_PE_udata8
)
666 as_bad (_("invalid or unsupported encoding in .cfi_personality"));
667 ignore_rest_of_line ();
671 if (*input_line_pointer
++ != ',')
673 as_bad (_(".cfi_personality requires encoding and symbol arguments"));
674 ignore_rest_of_line ();
678 expression_and_evaluate (&fde
->personality
);
679 switch (fde
->personality
.X_op
)
684 if ((encoding
& 0x70) == DW_EH_PE_pcrel
)
685 encoding
= DW_EH_PE_omit
;
688 encoding
= DW_EH_PE_omit
;
692 fde
->per_encoding
= encoding
;
694 if (encoding
== DW_EH_PE_omit
)
696 as_bad (_("wrong second argument to .cfi_personality"));
697 ignore_rest_of_line ();
701 demand_empty_rest_of_line ();
705 dot_cfi_lsda (int ignored ATTRIBUTE_UNUSED
)
707 struct fde_entry
*fde
;
710 if (frchain_now
->frch_cfi_data
== NULL
)
712 as_bad (_("CFI instruction used without previous .cfi_startproc"));
713 ignore_rest_of_line ();
717 fde
= frchain_now
->frch_cfi_data
->cur_fde_data
;
718 encoding
= get_absolute_expression ();
719 if (encoding
== DW_EH_PE_omit
)
721 demand_empty_rest_of_line ();
722 fde
->lsda_encoding
= encoding
;
726 if ((encoding
& 0xff) != encoding
727 || ((encoding
& 0x70) != 0
728 #if defined DIFF_EXPR_OK || defined tc_cfi_emit_pcrel_expr
729 && (encoding
& 0x70) != DW_EH_PE_pcrel
732 /* leb128 can be handled, but does something actually need it? */
733 || (encoding
& 7) == DW_EH_PE_uleb128
734 || (encoding
& 7) > DW_EH_PE_udata8
)
736 as_bad (_("invalid or unsupported encoding in .cfi_lsda"));
737 ignore_rest_of_line ();
741 if (*input_line_pointer
++ != ',')
743 as_bad (_(".cfi_lsda requires encoding and symbol arguments"));
744 ignore_rest_of_line ();
748 fde
->lsda_encoding
= encoding
;
750 expression_and_evaluate (&fde
->lsda
);
751 switch (fde
->lsda
.X_op
)
756 if ((encoding
& 0x70) == DW_EH_PE_pcrel
)
757 encoding
= DW_EH_PE_omit
;
760 encoding
= DW_EH_PE_omit
;
764 fde
->lsda_encoding
= encoding
;
766 if (encoding
== DW_EH_PE_omit
)
768 as_bad (_("wrong second argument to .cfi_lsda"));
769 ignore_rest_of_line ();
773 demand_empty_rest_of_line ();
777 dot_cfi_startproc (int ignored ATTRIBUTE_UNUSED
)
781 if (frchain_now
->frch_cfi_data
!= NULL
)
783 as_bad (_("previous CFI entry not closed (missing .cfi_endproc)"));
784 ignore_rest_of_line ();
788 cfi_new_fde (symbol_temp_new_now ());
791 if (is_name_beginner (*input_line_pointer
))
795 name
= input_line_pointer
;
796 c
= get_symbol_end ();
798 if (strcmp (name
, "simple") == 0)
801 *input_line_pointer
= c
;
804 input_line_pointer
= name
;
806 demand_empty_rest_of_line ();
808 frchain_now
->frch_cfi_data
->cur_cfa_offset
= 0;
810 tc_cfi_frame_initial_instructions ();
814 dot_cfi_endproc (int ignored ATTRIBUTE_UNUSED
)
816 if (frchain_now
->frch_cfi_data
== NULL
)
818 as_bad (_(".cfi_endproc without corresponding .cfi_startproc"));
819 ignore_rest_of_line ();
823 cfi_end_fde (symbol_temp_new_now ());
825 demand_empty_rest_of_line ();
829 /* Emit a single byte into the current segment. */
834 FRAG_APPEND_1_CHAR (byte
);
837 /* Emit a two-byte word into the current segment. */
842 md_number_to_chars (frag_more (2), data
, 2);
845 /* Emit a four byte word into the current segment. */
850 md_number_to_chars (frag_more (4), data
, 4);
853 /* Emit an unsigned "little-endian base 128" number. */
856 out_uleb128 (addressT value
)
858 output_leb128 (frag_more (sizeof_leb128 (value
, 0)), value
, 0);
861 /* Emit an unsigned "little-endian base 128" number. */
864 out_sleb128 (offsetT value
)
866 output_leb128 (frag_more (sizeof_leb128 (value
, 1)), value
, 1);
870 output_cfi_insn (struct cfi_insn_data
*insn
)
877 case DW_CFA_advance_loc
:
879 symbolS
*from
= insn
->u
.ll
.lab1
;
880 symbolS
*to
= insn
->u
.ll
.lab2
;
882 if (symbol_get_frag (to
) == symbol_get_frag (from
))
884 addressT delta
= S_GET_VALUE (to
) - S_GET_VALUE (from
);
885 addressT scaled
= delta
/ DWARF2_LINE_MIN_INSN_LENGTH
;
888 out_one (DW_CFA_advance_loc
+ scaled
);
889 else if (delta
<= 0xFF)
891 out_one (DW_CFA_advance_loc1
);
894 else if (delta
<= 0xFFFF)
896 out_one (DW_CFA_advance_loc2
);
901 out_one (DW_CFA_advance_loc4
);
909 exp
.X_op
= O_subtract
;
910 exp
.X_add_symbol
= to
;
911 exp
.X_op_symbol
= from
;
912 exp
.X_add_number
= 0;
914 /* The code in ehopt.c expects that one byte of the encoding
915 is already allocated to the frag. This comes from the way
916 that it scans the .eh_frame section looking first for the
917 .byte DW_CFA_advance_loc4. */
920 frag_var (rs_cfa
, 4, 0, DWARF2_LINE_MIN_INSN_LENGTH
<< 3,
921 make_expr_symbol (&exp
), frag_now_fix () - 1,
928 offset
= insn
->u
.ri
.offset
;
931 out_one (DW_CFA_def_cfa_sf
);
932 out_uleb128 (insn
->u
.ri
.reg
);
933 out_sleb128 (offset
/ DWARF2_CIE_DATA_ALIGNMENT
);
937 out_one (DW_CFA_def_cfa
);
938 out_uleb128 (insn
->u
.ri
.reg
);
939 out_uleb128 (offset
);
943 case DW_CFA_def_cfa_register
:
944 case DW_CFA_undefined
:
945 case DW_CFA_same_value
:
946 out_one (insn
->insn
);
947 out_uleb128 (insn
->u
.r
);
950 case DW_CFA_def_cfa_offset
:
954 out_one (DW_CFA_def_cfa_offset_sf
);
955 out_sleb128 (offset
/ DWARF2_CIE_DATA_ALIGNMENT
);
959 out_one (DW_CFA_def_cfa_offset
);
960 out_uleb128 (offset
);
968 out_one (DW_CFA_restore
+ regno
);
972 out_one (DW_CFA_restore_extended
);
978 regno
= insn
->u
.ri
.reg
;
979 offset
= insn
->u
.ri
.offset
/ DWARF2_CIE_DATA_ALIGNMENT
;
982 out_one (DW_CFA_offset_extended_sf
);
984 out_sleb128 (offset
);
986 else if (regno
<= 0x3F)
988 out_one (DW_CFA_offset
+ regno
);
989 out_uleb128 (offset
);
993 out_one (DW_CFA_offset_extended
);
995 out_uleb128 (offset
);
999 case DW_CFA_register
:
1000 out_one (DW_CFA_register
);
1001 out_uleb128 (insn
->u
.rr
.reg1
);
1002 out_uleb128 (insn
->u
.rr
.reg2
);
1005 case DW_CFA_remember_state
:
1006 case DW_CFA_restore_state
:
1007 out_one (insn
->insn
);
1010 case DW_CFA_GNU_window_save
:
1011 out_one (DW_CFA_GNU_window_save
);
1016 struct cfi_escape_data
*e
;
1017 for (e
= insn
->u
.esc
; e
; e
= e
->next
)
1018 emit_expr (&e
->exp
, 1);
1028 encoding_size (unsigned char encoding
)
1030 if (encoding
== DW_EH_PE_omit
)
1032 switch (encoding
& 0x7)
1035 return bfd_get_arch_size (stdoutput
) == 64 ? 8 : 4;
1036 case DW_EH_PE_udata2
:
1038 case DW_EH_PE_udata4
:
1040 case DW_EH_PE_udata8
:
1048 output_cie (struct cie_entry
*cie
)
1050 symbolS
*after_size_address
, *end_address
;
1052 struct cfi_insn_data
*i
;
1053 offsetT augmentation_size
;
1056 cie
->start_address
= symbol_temp_new_now ();
1057 after_size_address
= symbol_temp_make ();
1058 end_address
= symbol_temp_make ();
1060 exp
.X_op
= O_subtract
;
1061 exp
.X_add_symbol
= end_address
;
1062 exp
.X_op_symbol
= after_size_address
;
1063 exp
.X_add_number
= 0;
1065 emit_expr (&exp
, 4); /* Length. */
1066 symbol_set_value_now (after_size_address
);
1067 out_four (0); /* CIE id. */
1068 out_one (DW_CIE_VERSION
); /* Version. */
1069 out_one ('z'); /* Augmentation. */
1070 if (cie
->per_encoding
!= DW_EH_PE_omit
)
1072 if (cie
->lsda_encoding
!= DW_EH_PE_omit
)
1075 if (cie
->signal_frame
)
1078 out_uleb128 (DWARF2_LINE_MIN_INSN_LENGTH
); /* Code alignment. */
1079 out_sleb128 (DWARF2_CIE_DATA_ALIGNMENT
); /* Data alignment. */
1080 if (DW_CIE_VERSION
== 1) /* Return column. */
1081 out_one (cie
->return_column
);
1083 out_uleb128 (cie
->return_column
);
1084 augmentation_size
= 1 + (cie
->lsda_encoding
!= DW_EH_PE_omit
);
1085 if (cie
->per_encoding
!= DW_EH_PE_omit
)
1086 augmentation_size
+= 1 + encoding_size (cie
->per_encoding
);
1087 out_uleb128 (augmentation_size
); /* Augmentation size. */
1088 if (cie
->per_encoding
!= DW_EH_PE_omit
)
1090 offsetT size
= encoding_size (cie
->per_encoding
);
1091 out_one (cie
->per_encoding
);
1092 exp
= cie
->personality
;
1093 if ((cie
->per_encoding
& 0x70) == DW_EH_PE_pcrel
)
1096 exp
.X_op
= O_subtract
;
1097 exp
.X_op_symbol
= symbol_temp_new_now ();
1098 emit_expr (&exp
, size
);
1099 #elif defined (tc_cfi_emit_pcrel_expr)
1100 tc_cfi_emit_pcrel_expr (&exp
, size
);
1106 emit_expr (&exp
, size
);
1108 if (cie
->lsda_encoding
!= DW_EH_PE_omit
)
1109 out_one (cie
->lsda_encoding
);
1111 switch (DWARF2_FDE_RELOC_SIZE
)
1114 enc
= DW_EH_PE_sdata2
;
1117 enc
= DW_EH_PE_sdata4
;
1120 enc
= DW_EH_PE_sdata8
;
1125 #if defined DIFF_EXPR_OK || defined tc_cfi_emit_pcrel_expr
1126 enc
|= DW_EH_PE_pcrel
;
1131 for (i
= cie
->first
; i
!= cie
->last
; i
= i
->next
)
1132 output_cfi_insn (i
);
1134 frag_align (2, DW_CFA_nop
, 0);
1135 symbol_set_value_now (end_address
);
1139 output_fde (struct fde_entry
*fde
, struct cie_entry
*cie
,
1140 struct cfi_insn_data
*first
, int align
)
1142 symbolS
*after_size_address
, *end_address
;
1144 offsetT augmentation_size
;
1146 after_size_address
= symbol_temp_make ();
1147 end_address
= symbol_temp_make ();
1149 exp
.X_op
= O_subtract
;
1150 exp
.X_add_symbol
= end_address
;
1151 exp
.X_op_symbol
= after_size_address
;
1152 exp
.X_add_number
= 0;
1153 emit_expr (&exp
, 4); /* Length. */
1154 symbol_set_value_now (after_size_address
);
1156 exp
.X_add_symbol
= after_size_address
;
1157 exp
.X_op_symbol
= cie
->start_address
;
1158 emit_expr (&exp
, 4); /* CIE offset. */
1161 exp
.X_add_symbol
= fde
->start_address
;
1162 exp
.X_op_symbol
= symbol_temp_new_now ();
1163 emit_expr (&exp
, DWARF2_FDE_RELOC_SIZE
); /* Code offset. */
1165 exp
.X_op
= O_symbol
;
1166 exp
.X_add_symbol
= fde
->start_address
;
1167 exp
.X_op_symbol
= NULL
;
1168 #ifdef tc_cfi_emit_pcrel_expr
1169 tc_cfi_emit_pcrel_expr (&exp
, DWARF2_FDE_RELOC_SIZE
); /* Code offset. */
1171 emit_expr (&exp
, DWARF2_FDE_RELOC_SIZE
); /* Code offset. */
1173 exp
.X_op
= O_subtract
;
1176 exp
.X_add_symbol
= fde
->end_address
;
1177 exp
.X_op_symbol
= fde
->start_address
; /* Code length. */
1178 emit_expr (&exp
, DWARF2_FDE_RELOC_SIZE
);
1180 augmentation_size
= encoding_size (fde
->lsda_encoding
);
1181 out_uleb128 (augmentation_size
); /* Augmentation size. */
1183 if (fde
->lsda_encoding
!= DW_EH_PE_omit
)
1186 if ((fde
->lsda_encoding
& 0x70) == DW_EH_PE_pcrel
)
1189 exp
.X_op
= O_subtract
;
1190 exp
.X_op_symbol
= symbol_temp_new_now ();
1191 emit_expr (&exp
, augmentation_size
);
1192 #elif defined (tc_cfi_emit_pcrel_expr)
1193 tc_cfi_emit_pcrel_expr (&exp
, augmentation_size
);
1199 emit_expr (&exp
, augmentation_size
);
1202 for (; first
; first
= first
->next
)
1203 output_cfi_insn (first
);
1205 frag_align (align
, DW_CFA_nop
, 0);
1206 symbol_set_value_now (end_address
);
1209 static struct cie_entry
*
1210 select_cie_for_fde (struct fde_entry
*fde
, struct cfi_insn_data
**pfirst
)
1212 struct cfi_insn_data
*i
, *j
;
1213 struct cie_entry
*cie
;
1215 for (cie
= cie_root
; cie
; cie
= cie
->next
)
1217 if (cie
->return_column
!= fde
->return_column
1218 || cie
->signal_frame
!= fde
->signal_frame
1219 || cie
->per_encoding
!= fde
->per_encoding
1220 || cie
->lsda_encoding
!= fde
->lsda_encoding
)
1222 if (cie
->per_encoding
!= DW_EH_PE_omit
)
1224 if (cie
->personality
.X_op
!= fde
->personality
.X_op
1225 || cie
->personality
.X_add_number
1226 != fde
->personality
.X_add_number
)
1228 switch (cie
->personality
.X_op
)
1231 if (cie
->personality
.X_unsigned
!= fde
->personality
.X_unsigned
)
1235 if (cie
->personality
.X_add_symbol
1236 != fde
->personality
.X_add_symbol
)
1243 for (i
= cie
->first
, j
= fde
->data
;
1244 i
!= cie
->last
&& j
!= NULL
;
1245 i
= i
->next
, j
= j
->next
)
1247 if (i
->insn
!= j
->insn
)
1251 case DW_CFA_advance_loc
:
1252 case DW_CFA_remember_state
:
1253 /* We reached the first advance/remember in the FDE,
1254 but did not reach the end of the CIE list. */
1258 case DW_CFA_def_cfa
:
1259 if (i
->u
.ri
.reg
!= j
->u
.ri
.reg
)
1261 if (i
->u
.ri
.offset
!= j
->u
.ri
.offset
)
1265 case DW_CFA_register
:
1266 if (i
->u
.rr
.reg1
!= j
->u
.rr
.reg1
)
1268 if (i
->u
.rr
.reg2
!= j
->u
.rr
.reg2
)
1272 case DW_CFA_def_cfa_register
:
1273 case DW_CFA_restore
:
1274 case DW_CFA_undefined
:
1275 case DW_CFA_same_value
:
1276 if (i
->u
.r
!= j
->u
.r
)
1280 case DW_CFA_def_cfa_offset
:
1281 if (i
->u
.i
!= j
->u
.i
)
1286 /* Don't bother matching these for now. */
1294 /* Success if we reached the end of the CIE list, and we've either
1295 run out of FDE entries or we've encountered an advance,
1296 remember, or escape. */
1299 || j
->insn
== DW_CFA_advance_loc
1300 || j
->insn
== DW_CFA_remember_state
1301 || j
->insn
== CFI_escape
))
1310 cie
= xmalloc (sizeof (struct cie_entry
));
1311 cie
->next
= cie_root
;
1313 cie
->return_column
= fde
->return_column
;
1314 cie
->signal_frame
= fde
->signal_frame
;
1315 cie
->per_encoding
= fde
->per_encoding
;
1316 cie
->lsda_encoding
= fde
->lsda_encoding
;
1317 cie
->personality
= fde
->personality
;
1318 cie
->first
= fde
->data
;
1320 for (i
= cie
->first
; i
; i
= i
->next
)
1321 if (i
->insn
== DW_CFA_advance_loc
1322 || i
->insn
== DW_CFA_remember_state
1323 || i
->insn
== CFI_escape
)
1338 struct fde_entry
*fde
;
1339 int save_flag_traditional_format
;
1341 if (all_fde_data
== 0)
1344 /* Open .eh_frame section. */
1345 cfi_seg
= subseg_new (".eh_frame", 0);
1346 bfd_set_section_flags (stdoutput
, cfi_seg
,
1347 SEC_ALLOC
| SEC_LOAD
| SEC_DATA
1348 | DWARF2_EH_FRAME_READ_ONLY
);
1349 subseg_set (cfi_seg
, 0);
1350 record_alignment (cfi_seg
, EH_FRAME_ALIGNMENT
);
1352 /* Make sure check_eh_frame doesn't do anything with our output. */
1353 save_flag_traditional_format
= flag_traditional_format
;
1354 flag_traditional_format
= 1;
1356 for (fde
= all_fde_data
; fde
; fde
= fde
->next
)
1358 struct cfi_insn_data
*first
;
1359 struct cie_entry
*cie
;
1361 if (fde
->end_address
== NULL
)
1363 as_bad (_("open CFI at the end of file; missing .cfi_endproc directive"));
1364 fde
->end_address
= fde
->start_address
;
1367 cie
= select_cie_for_fde (fde
, &first
);
1368 output_fde (fde
, cie
, first
, fde
->next
== NULL
? EH_FRAME_ALIGNMENT
: 2);
1371 flag_traditional_format
= save_flag_traditional_format
;
1374 #else /* TARGET_USE_CFIPOP */
1379 #endif /* TARGET_USE_CFIPOP */