1 /* dw2gencfi.c - Support for generating Dwarf2 CFI information.
2 Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009
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"
27 #ifdef TARGET_USE_CFIPOP
29 /* By default, use difference expressions if DIFF_EXPR_OK is defined. */
30 #ifndef CFI_DIFF_EXPR_OK
32 # define CFI_DIFF_EXPR_OK 1
34 # define CFI_DIFF_EXPR_OK 0
38 /* We re-use DWARF2_LINE_MIN_INSN_LENGTH for the code alignment field
39 of the CIE. Default to 1 if not otherwise specified. */
40 #ifndef DWARF2_LINE_MIN_INSN_LENGTH
41 # define DWARF2_LINE_MIN_INSN_LENGTH 1
44 /* By default, use 32-bit relocations from .eh_frame into .text. */
45 #ifndef DWARF2_FDE_RELOC_SIZE
46 # define DWARF2_FDE_RELOC_SIZE 4
49 /* By default, use a read-only .eh_frame section. */
50 #ifndef DWARF2_EH_FRAME_READ_ONLY
51 # define DWARF2_EH_FRAME_READ_ONLY SEC_READONLY
54 #ifndef EH_FRAME_ALIGNMENT
55 # define EH_FRAME_ALIGNMENT (bfd_get_arch_size (stdoutput) == 64 ? 3 : 2)
58 #ifndef tc_cfi_frame_initial_instructions
59 # define tc_cfi_frame_initial_instructions() ((void)0)
62 #ifndef DWARF2_ADDR_SIZE
63 # define DWARF2_ADDR_SIZE(bfd) (bfd_arch_bits_per_address (bfd) / 8)
66 struct cfi_escape_data
{
67 struct cfi_escape_data
*next
;
73 struct cfi_insn_data
*next
;
94 struct cfi_escape_data
*esc
;
97 unsigned reg
, encoding
;
105 struct fde_entry
*next
;
106 symbolS
*start_address
;
107 symbolS
*end_address
;
108 struct cfi_insn_data
*data
;
109 struct cfi_insn_data
**last
;
110 unsigned char per_encoding
;
111 unsigned char lsda_encoding
;
112 expressionS personality
;
114 unsigned int return_column
;
115 unsigned int signal_frame
;
120 struct cie_entry
*next
;
121 symbolS
*start_address
;
122 unsigned int return_column
;
123 unsigned int signal_frame
;
124 unsigned char per_encoding
;
125 unsigned char lsda_encoding
;
126 expressionS personality
;
127 struct cfi_insn_data
*first
, *last
;
131 /* List of FDE entries. */
132 static struct fde_entry
*all_fde_data
;
133 static struct fde_entry
**last_fde_data
= &all_fde_data
;
135 /* List of CIEs so that they could be reused. */
136 static struct cie_entry
*cie_root
;
138 /* Stack of old CFI data, for save/restore. */
141 struct cfa_save_data
*next
;
145 /* Current open FDE entry. */
148 struct fde_entry
*cur_fde_data
;
149 symbolS
*last_address
;
150 offsetT cur_cfa_offset
;
151 struct cfa_save_data
*cfa_save_stack
;
154 /* Construct a new FDE structure and add it to the end of the fde list. */
156 static struct fde_entry
*
157 alloc_fde_entry (void)
159 struct fde_entry
*fde
= (struct fde_entry
*)
160 xcalloc (1, sizeof (struct fde_entry
));
162 frchain_now
->frch_cfi_data
= (struct frch_cfi_data
*)
163 xcalloc (1, sizeof (struct frch_cfi_data
));
164 frchain_now
->frch_cfi_data
->cur_fde_data
= fde
;
165 *last_fde_data
= fde
;
166 last_fde_data
= &fde
->next
;
168 fde
->last
= &fde
->data
;
169 fde
->return_column
= DWARF2_DEFAULT_RETURN_COLUMN
;
170 fde
->per_encoding
= DW_EH_PE_omit
;
171 fde
->lsda_encoding
= DW_EH_PE_omit
;
176 /* The following functions are available for a backend to construct its
177 own unwind information, usually from legacy unwind directives. */
179 /* Construct a new INSN structure and add it to the end of the insn list
180 for the currently active FDE. */
182 static struct cfi_insn_data
*
183 alloc_cfi_insn_data (void)
185 struct cfi_insn_data
*insn
= (struct cfi_insn_data
*)
186 xcalloc (1, sizeof (struct cfi_insn_data
));
187 struct fde_entry
*cur_fde_data
= frchain_now
->frch_cfi_data
->cur_fde_data
;
189 *cur_fde_data
->last
= insn
;
190 cur_fde_data
->last
= &insn
->next
;
195 /* Construct a new FDE structure that begins at LABEL. */
198 cfi_new_fde (symbolS
*label
)
200 struct fde_entry
*fde
= alloc_fde_entry ();
201 fde
->start_address
= label
;
202 frchain_now
->frch_cfi_data
->last_address
= label
;
205 /* End the currently open FDE. */
208 cfi_end_fde (symbolS
*label
)
210 frchain_now
->frch_cfi_data
->cur_fde_data
->end_address
= label
;
211 free (frchain_now
->frch_cfi_data
);
212 frchain_now
->frch_cfi_data
= NULL
;
215 /* Set the return column for the current FDE. */
218 cfi_set_return_column (unsigned regno
)
220 frchain_now
->frch_cfi_data
->cur_fde_data
->return_column
= regno
;
223 /* Universal functions to store new instructions. */
226 cfi_add_CFA_insn(int insn
)
228 struct cfi_insn_data
*insn_ptr
= alloc_cfi_insn_data ();
230 insn_ptr
->insn
= insn
;
234 cfi_add_CFA_insn_reg (int insn
, unsigned regno
)
236 struct cfi_insn_data
*insn_ptr
= alloc_cfi_insn_data ();
238 insn_ptr
->insn
= insn
;
239 insn_ptr
->u
.r
= regno
;
243 cfi_add_CFA_insn_offset (int insn
, offsetT offset
)
245 struct cfi_insn_data
*insn_ptr
= alloc_cfi_insn_data ();
247 insn_ptr
->insn
= insn
;
248 insn_ptr
->u
.i
= offset
;
252 cfi_add_CFA_insn_reg_reg (int insn
, unsigned reg1
, unsigned reg2
)
254 struct cfi_insn_data
*insn_ptr
= alloc_cfi_insn_data ();
256 insn_ptr
->insn
= insn
;
257 insn_ptr
->u
.rr
.reg1
= reg1
;
258 insn_ptr
->u
.rr
.reg2
= reg2
;
262 cfi_add_CFA_insn_reg_offset (int insn
, unsigned regno
, offsetT offset
)
264 struct cfi_insn_data
*insn_ptr
= alloc_cfi_insn_data ();
266 insn_ptr
->insn
= insn
;
267 insn_ptr
->u
.ri
.reg
= regno
;
268 insn_ptr
->u
.ri
.offset
= offset
;
271 /* Add a CFI insn to advance the PC from the last address to LABEL. */
274 cfi_add_advance_loc (symbolS
*label
)
276 struct cfi_insn_data
*insn
= alloc_cfi_insn_data ();
278 insn
->insn
= DW_CFA_advance_loc
;
279 insn
->u
.ll
.lab1
= frchain_now
->frch_cfi_data
->last_address
;
280 insn
->u
.ll
.lab2
= label
;
282 frchain_now
->frch_cfi_data
->last_address
= label
;
285 /* Add a DW_CFA_offset record to the CFI data. */
288 cfi_add_CFA_offset (unsigned regno
, offsetT offset
)
290 unsigned int abs_data_align
;
292 gas_assert (DWARF2_CIE_DATA_ALIGNMENT
!= 0);
293 cfi_add_CFA_insn_reg_offset (DW_CFA_offset
, regno
, offset
);
295 abs_data_align
= (DWARF2_CIE_DATA_ALIGNMENT
< 0
296 ? -DWARF2_CIE_DATA_ALIGNMENT
: DWARF2_CIE_DATA_ALIGNMENT
);
297 if (offset
% abs_data_align
)
298 as_bad (_("register save offset not a multiple of %u"), abs_data_align
);
301 /* Add a DW_CFA_def_cfa record to the CFI data. */
304 cfi_add_CFA_def_cfa (unsigned regno
, offsetT offset
)
306 cfi_add_CFA_insn_reg_offset (DW_CFA_def_cfa
, regno
, offset
);
307 frchain_now
->frch_cfi_data
->cur_cfa_offset
= offset
;
310 /* Add a DW_CFA_register record to the CFI data. */
313 cfi_add_CFA_register (unsigned reg1
, unsigned reg2
)
315 cfi_add_CFA_insn_reg_reg (DW_CFA_register
, reg1
, reg2
);
318 /* Add a DW_CFA_def_cfa_register record to the CFI data. */
321 cfi_add_CFA_def_cfa_register (unsigned regno
)
323 cfi_add_CFA_insn_reg (DW_CFA_def_cfa_register
, regno
);
326 /* Add a DW_CFA_def_cfa_offset record to the CFI data. */
329 cfi_add_CFA_def_cfa_offset (offsetT offset
)
331 cfi_add_CFA_insn_offset (DW_CFA_def_cfa_offset
, offset
);
332 frchain_now
->frch_cfi_data
->cur_cfa_offset
= offset
;
336 cfi_add_CFA_restore (unsigned regno
)
338 cfi_add_CFA_insn_reg (DW_CFA_restore
, regno
);
342 cfi_add_CFA_undefined (unsigned regno
)
344 cfi_add_CFA_insn_reg (DW_CFA_undefined
, regno
);
348 cfi_add_CFA_same_value (unsigned regno
)
350 cfi_add_CFA_insn_reg (DW_CFA_same_value
, regno
);
354 cfi_add_CFA_remember_state (void)
356 struct cfa_save_data
*p
;
358 cfi_add_CFA_insn (DW_CFA_remember_state
);
360 p
= (struct cfa_save_data
*) xmalloc (sizeof (*p
));
361 p
->cfa_offset
= frchain_now
->frch_cfi_data
->cur_cfa_offset
;
362 p
->next
= frchain_now
->frch_cfi_data
->cfa_save_stack
;
363 frchain_now
->frch_cfi_data
->cfa_save_stack
= p
;
367 cfi_add_CFA_restore_state (void)
369 struct cfa_save_data
*p
;
371 cfi_add_CFA_insn (DW_CFA_restore_state
);
373 p
= frchain_now
->frch_cfi_data
->cfa_save_stack
;
376 frchain_now
->frch_cfi_data
->cur_cfa_offset
= p
->cfa_offset
;
377 frchain_now
->frch_cfi_data
->cfa_save_stack
= p
->next
;
381 as_bad (_("CFI state restore without previous remember"));
385 /* Parse CFI assembler directives. */
387 static void dot_cfi (int);
388 static void dot_cfi_escape (int);
389 static void dot_cfi_startproc (int);
390 static void dot_cfi_endproc (int);
391 static void dot_cfi_personality (int);
392 static void dot_cfi_lsda (int);
393 static void dot_cfi_val_encoded_addr (int);
395 /* Fake CFI type; outside the byte range of any real CFI insn. */
396 #define CFI_adjust_cfa_offset 0x100
397 #define CFI_return_column 0x101
398 #define CFI_rel_offset 0x102
399 #define CFI_escape 0x103
400 #define CFI_signal_frame 0x104
401 #define CFI_val_encoded_addr 0x105
403 const pseudo_typeS cfi_pseudo_table
[] =
405 { "cfi_startproc", dot_cfi_startproc
, 0 },
406 { "cfi_endproc", dot_cfi_endproc
, 0 },
407 { "cfi_def_cfa", dot_cfi
, DW_CFA_def_cfa
},
408 { "cfi_def_cfa_register", dot_cfi
, DW_CFA_def_cfa_register
},
409 { "cfi_def_cfa_offset", dot_cfi
, DW_CFA_def_cfa_offset
},
410 { "cfi_adjust_cfa_offset", dot_cfi
, CFI_adjust_cfa_offset
},
411 { "cfi_offset", dot_cfi
, DW_CFA_offset
},
412 { "cfi_rel_offset", dot_cfi
, CFI_rel_offset
},
413 { "cfi_register", dot_cfi
, DW_CFA_register
},
414 { "cfi_return_column", dot_cfi
, CFI_return_column
},
415 { "cfi_restore", dot_cfi
, DW_CFA_restore
},
416 { "cfi_undefined", dot_cfi
, DW_CFA_undefined
},
417 { "cfi_same_value", dot_cfi
, DW_CFA_same_value
},
418 { "cfi_remember_state", dot_cfi
, DW_CFA_remember_state
},
419 { "cfi_restore_state", dot_cfi
, DW_CFA_restore_state
},
420 { "cfi_window_save", dot_cfi
, DW_CFA_GNU_window_save
},
421 { "cfi_escape", dot_cfi_escape
, 0 },
422 { "cfi_signal_frame", dot_cfi
, CFI_signal_frame
},
423 { "cfi_personality", dot_cfi_personality
, 0 },
424 { "cfi_lsda", dot_cfi_lsda
, 0 },
425 { "cfi_val_encoded_addr", dot_cfi_val_encoded_addr
, 0 },
430 cfi_parse_separator (void)
433 if (*input_line_pointer
== ',')
434 input_line_pointer
++;
436 as_bad (_("missing separator"));
439 #ifndef tc_parse_to_dw2regnum
441 tc_parse_to_dw2regnum(expressionS
*exp
)
443 # ifdef tc_regname_to_dw2regnum
445 if (is_name_beginner (*input_line_pointer
)
446 || (*input_line_pointer
== '%'
447 && is_name_beginner (*++input_line_pointer
)))
451 name
= input_line_pointer
;
452 c
= get_symbol_end ();
454 exp
->X_op
= O_constant
;
455 exp
->X_add_number
= tc_regname_to_dw2regnum (name
);
457 *input_line_pointer
= c
;
461 expression_and_evaluate (exp
);
471 tc_parse_to_dw2regnum (&exp
);
476 regno
= exp
.X_add_number
;
486 as_bad (_("bad register expression"));
494 cfi_parse_const (void)
496 return get_absolute_expression ();
505 if (frchain_now
->frch_cfi_data
== NULL
)
507 as_bad (_("CFI instruction used without previous .cfi_startproc"));
508 ignore_rest_of_line ();
512 /* If the last address was not at the current PC, advance to current. */
513 if (symbol_get_frag (frchain_now
->frch_cfi_data
->last_address
) != frag_now
514 || S_GET_VALUE (frchain_now
->frch_cfi_data
->last_address
)
516 cfi_add_advance_loc (symbol_temp_new_now ());
521 reg1
= cfi_parse_reg ();
522 cfi_parse_separator ();
523 offset
= cfi_parse_const ();
524 cfi_add_CFA_offset (reg1
, offset
);
528 reg1
= cfi_parse_reg ();
529 cfi_parse_separator ();
530 offset
= cfi_parse_const ();
531 cfi_add_CFA_offset (reg1
,
532 offset
- frchain_now
->frch_cfi_data
->cur_cfa_offset
);
536 reg1
= cfi_parse_reg ();
537 cfi_parse_separator ();
538 offset
= cfi_parse_const ();
539 cfi_add_CFA_def_cfa (reg1
, offset
);
542 case DW_CFA_register
:
543 reg1
= cfi_parse_reg ();
544 cfi_parse_separator ();
545 reg2
= cfi_parse_reg ();
546 cfi_add_CFA_register (reg1
, reg2
);
549 case DW_CFA_def_cfa_register
:
550 reg1
= cfi_parse_reg ();
551 cfi_add_CFA_def_cfa_register (reg1
);
554 case DW_CFA_def_cfa_offset
:
555 offset
= cfi_parse_const ();
556 cfi_add_CFA_def_cfa_offset (offset
);
559 case CFI_adjust_cfa_offset
:
560 offset
= cfi_parse_const ();
561 cfi_add_CFA_def_cfa_offset (frchain_now
->frch_cfi_data
->cur_cfa_offset
568 reg1
= cfi_parse_reg ();
569 cfi_add_CFA_restore (reg1
);
571 if (*input_line_pointer
!= ',')
573 ++input_line_pointer
;
577 case DW_CFA_undefined
:
580 reg1
= cfi_parse_reg ();
581 cfi_add_CFA_undefined (reg1
);
583 if (*input_line_pointer
!= ',')
585 ++input_line_pointer
;
589 case DW_CFA_same_value
:
590 reg1
= cfi_parse_reg ();
591 cfi_add_CFA_same_value (reg1
);
594 case CFI_return_column
:
595 reg1
= cfi_parse_reg ();
596 cfi_set_return_column (reg1
);
599 case DW_CFA_remember_state
:
600 cfi_add_CFA_remember_state ();
603 case DW_CFA_restore_state
:
604 cfi_add_CFA_restore_state ();
607 case DW_CFA_GNU_window_save
:
608 cfi_add_CFA_insn (DW_CFA_GNU_window_save
);
611 case CFI_signal_frame
:
612 frchain_now
->frch_cfi_data
->cur_fde_data
->signal_frame
= 1;
619 demand_empty_rest_of_line ();
623 dot_cfi_escape (int ignored ATTRIBUTE_UNUSED
)
625 struct cfi_escape_data
*head
, **tail
, *e
;
626 struct cfi_insn_data
*insn
;
628 if (frchain_now
->frch_cfi_data
== NULL
)
630 as_bad (_("CFI instruction used without previous .cfi_startproc"));
631 ignore_rest_of_line ();
635 /* If the last address was not at the current PC, advance to current. */
636 if (symbol_get_frag (frchain_now
->frch_cfi_data
->last_address
) != frag_now
637 || S_GET_VALUE (frchain_now
->frch_cfi_data
->last_address
)
639 cfi_add_advance_loc (symbol_temp_new_now ());
644 e
= (struct cfi_escape_data
*) xmalloc (sizeof (*e
));
645 do_parse_cons_expression (&e
->exp
, 1);
649 while (*input_line_pointer
++ == ',');
652 insn
= alloc_cfi_insn_data ();
653 insn
->insn
= CFI_escape
;
656 --input_line_pointer
;
657 demand_empty_rest_of_line ();
661 dot_cfi_personality (int ignored ATTRIBUTE_UNUSED
)
663 struct fde_entry
*fde
;
666 if (frchain_now
->frch_cfi_data
== NULL
)
668 as_bad (_("CFI instruction used without previous .cfi_startproc"));
669 ignore_rest_of_line ();
673 fde
= frchain_now
->frch_cfi_data
->cur_fde_data
;
674 encoding
= cfi_parse_const ();
675 if (encoding
== DW_EH_PE_omit
)
677 demand_empty_rest_of_line ();
678 fde
->per_encoding
= encoding
;
682 if ((encoding
& 0xff) != encoding
683 || ((encoding
& 0x70) != 0
684 #if CFI_DIFF_EXPR_OK || defined tc_cfi_emit_pcrel_expr
685 && (encoding
& 0x70) != DW_EH_PE_pcrel
688 /* leb128 can be handled, but does something actually need it? */
689 || (encoding
& 7) == DW_EH_PE_uleb128
690 || (encoding
& 7) > DW_EH_PE_udata8
)
692 as_bad (_("invalid or unsupported encoding in .cfi_personality"));
693 ignore_rest_of_line ();
697 if (*input_line_pointer
++ != ',')
699 as_bad (_(".cfi_personality requires encoding and symbol arguments"));
700 ignore_rest_of_line ();
704 expression_and_evaluate (&fde
->personality
);
705 switch (fde
->personality
.X_op
)
710 if ((encoding
& 0x70) == DW_EH_PE_pcrel
)
711 encoding
= DW_EH_PE_omit
;
714 encoding
= DW_EH_PE_omit
;
718 fde
->per_encoding
= encoding
;
720 if (encoding
== DW_EH_PE_omit
)
722 as_bad (_("wrong second argument to .cfi_personality"));
723 ignore_rest_of_line ();
727 demand_empty_rest_of_line ();
731 dot_cfi_lsda (int ignored ATTRIBUTE_UNUSED
)
733 struct fde_entry
*fde
;
736 if (frchain_now
->frch_cfi_data
== NULL
)
738 as_bad (_("CFI instruction used without previous .cfi_startproc"));
739 ignore_rest_of_line ();
743 fde
= frchain_now
->frch_cfi_data
->cur_fde_data
;
744 encoding
= cfi_parse_const ();
745 if (encoding
== DW_EH_PE_omit
)
747 demand_empty_rest_of_line ();
748 fde
->lsda_encoding
= encoding
;
752 if ((encoding
& 0xff) != encoding
753 || ((encoding
& 0x70) != 0
754 #if CFI_DIFF_EXPR_OK || defined tc_cfi_emit_pcrel_expr
755 && (encoding
& 0x70) != DW_EH_PE_pcrel
758 /* leb128 can be handled, but does something actually need it? */
759 || (encoding
& 7) == DW_EH_PE_uleb128
760 || (encoding
& 7) > DW_EH_PE_udata8
)
762 as_bad (_("invalid or unsupported encoding in .cfi_lsda"));
763 ignore_rest_of_line ();
767 if (*input_line_pointer
++ != ',')
769 as_bad (_(".cfi_lsda requires encoding and symbol arguments"));
770 ignore_rest_of_line ();
774 fde
->lsda_encoding
= encoding
;
776 expression_and_evaluate (&fde
->lsda
);
777 switch (fde
->lsda
.X_op
)
782 if ((encoding
& 0x70) == DW_EH_PE_pcrel
)
783 encoding
= DW_EH_PE_omit
;
786 encoding
= DW_EH_PE_omit
;
790 fde
->lsda_encoding
= encoding
;
792 if (encoding
== DW_EH_PE_omit
)
794 as_bad (_("wrong second argument to .cfi_lsda"));
795 ignore_rest_of_line ();
799 demand_empty_rest_of_line ();
803 dot_cfi_val_encoded_addr (int ignored ATTRIBUTE_UNUSED
)
805 struct cfi_insn_data
*insn_ptr
;
808 if (frchain_now
->frch_cfi_data
== NULL
)
810 as_bad (_("CFI instruction used without previous .cfi_startproc"));
811 ignore_rest_of_line ();
815 /* If the last address was not at the current PC, advance to current. */
816 if (symbol_get_frag (frchain_now
->frch_cfi_data
->last_address
) != frag_now
817 || S_GET_VALUE (frchain_now
->frch_cfi_data
->last_address
)
819 cfi_add_advance_loc (symbol_temp_new_now ());
821 insn_ptr
= alloc_cfi_insn_data ();
822 insn_ptr
->insn
= CFI_val_encoded_addr
;
824 insn_ptr
->u
.ea
.reg
= cfi_parse_reg ();
826 cfi_parse_separator ();
827 encoding
= cfi_parse_const ();
828 if ((encoding
& 0xff) != encoding
829 || ((encoding
& 0x70) != 0
830 #if CFI_DIFF_EXPR_OK || defined tc_cfi_emit_pcrel_expr
831 && (encoding
& 0x70) != DW_EH_PE_pcrel
834 /* leb128 can be handled, but does something actually need it? */
835 || (encoding
& 7) == DW_EH_PE_uleb128
836 || (encoding
& 7) > DW_EH_PE_udata8
)
838 as_bad (_("invalid or unsupported encoding in .cfi_lsda"));
839 encoding
= DW_EH_PE_omit
;
842 cfi_parse_separator ();
843 expression_and_evaluate (&insn_ptr
->u
.ea
.exp
);
844 switch (insn_ptr
->u
.ea
.exp
.X_op
)
849 if ((encoding
& 0x70) != DW_EH_PE_pcrel
)
852 encoding
= DW_EH_PE_omit
;
856 insn_ptr
->u
.ea
.encoding
= encoding
;
857 if (encoding
== DW_EH_PE_omit
)
859 as_bad (_("wrong third argument to .cfi_val_encoded_addr"));
860 ignore_rest_of_line ();
864 demand_empty_rest_of_line ();
868 dot_cfi_startproc (int ignored ATTRIBUTE_UNUSED
)
872 if (frchain_now
->frch_cfi_data
!= NULL
)
874 as_bad (_("previous CFI entry not closed (missing .cfi_endproc)"));
875 ignore_rest_of_line ();
879 cfi_new_fde (symbol_temp_new_now ());
882 if (is_name_beginner (*input_line_pointer
))
886 name
= input_line_pointer
;
887 c
= get_symbol_end ();
889 if (strcmp (name
, "simple") == 0)
892 *input_line_pointer
= c
;
895 input_line_pointer
= name
;
897 demand_empty_rest_of_line ();
899 frchain_now
->frch_cfi_data
->cur_cfa_offset
= 0;
901 tc_cfi_frame_initial_instructions ();
905 dot_cfi_endproc (int ignored ATTRIBUTE_UNUSED
)
907 if (frchain_now
->frch_cfi_data
== NULL
)
909 as_bad (_(".cfi_endproc without corresponding .cfi_startproc"));
910 ignore_rest_of_line ();
914 cfi_end_fde (symbol_temp_new_now ());
916 demand_empty_rest_of_line ();
920 /* Emit a single byte into the current segment. */
925 FRAG_APPEND_1_CHAR (byte
);
928 /* Emit a two-byte word into the current segment. */
933 md_number_to_chars (frag_more (2), data
, 2);
936 /* Emit a four byte word into the current segment. */
941 md_number_to_chars (frag_more (4), data
, 4);
944 /* Emit an unsigned "little-endian base 128" number. */
947 out_uleb128 (addressT value
)
949 output_leb128 (frag_more (sizeof_leb128 (value
, 0)), value
, 0);
952 /* Emit an unsigned "little-endian base 128" number. */
955 out_sleb128 (offsetT value
)
957 output_leb128 (frag_more (sizeof_leb128 (value
, 1)), value
, 1);
961 output_cfi_insn (struct cfi_insn_data
*insn
)
968 case DW_CFA_advance_loc
:
970 symbolS
*from
= insn
->u
.ll
.lab1
;
971 symbolS
*to
= insn
->u
.ll
.lab2
;
973 if (symbol_get_frag (to
) == symbol_get_frag (from
))
975 addressT delta
= S_GET_VALUE (to
) - S_GET_VALUE (from
);
976 addressT scaled
= delta
/ DWARF2_LINE_MIN_INSN_LENGTH
;
979 out_one (DW_CFA_advance_loc
+ scaled
);
980 else if (scaled
<= 0xFF)
982 out_one (DW_CFA_advance_loc1
);
985 else if (scaled
<= 0xFFFF)
987 out_one (DW_CFA_advance_loc2
);
992 out_one (DW_CFA_advance_loc4
);
1000 exp
.X_op
= O_subtract
;
1001 exp
.X_add_symbol
= to
;
1002 exp
.X_op_symbol
= from
;
1003 exp
.X_add_number
= 0;
1005 /* The code in ehopt.c expects that one byte of the encoding
1006 is already allocated to the frag. This comes from the way
1007 that it scans the .eh_frame section looking first for the
1008 .byte DW_CFA_advance_loc4. */
1009 *frag_more (1) = DW_CFA_advance_loc4
;
1011 frag_var (rs_cfa
, 4, 0, DWARF2_LINE_MIN_INSN_LENGTH
<< 3,
1012 make_expr_symbol (&exp
), frag_now_fix () - 1,
1018 case DW_CFA_def_cfa
:
1019 offset
= insn
->u
.ri
.offset
;
1022 out_one (DW_CFA_def_cfa_sf
);
1023 out_uleb128 (insn
->u
.ri
.reg
);
1024 out_sleb128 (offset
/ DWARF2_CIE_DATA_ALIGNMENT
);
1028 out_one (DW_CFA_def_cfa
);
1029 out_uleb128 (insn
->u
.ri
.reg
);
1030 out_uleb128 (offset
);
1034 case DW_CFA_def_cfa_register
:
1035 case DW_CFA_undefined
:
1036 case DW_CFA_same_value
:
1037 out_one (insn
->insn
);
1038 out_uleb128 (insn
->u
.r
);
1041 case DW_CFA_def_cfa_offset
:
1045 out_one (DW_CFA_def_cfa_offset_sf
);
1046 out_sleb128 (offset
/ DWARF2_CIE_DATA_ALIGNMENT
);
1050 out_one (DW_CFA_def_cfa_offset
);
1051 out_uleb128 (offset
);
1055 case DW_CFA_restore
:
1059 out_one (DW_CFA_restore
+ regno
);
1063 out_one (DW_CFA_restore_extended
);
1064 out_uleb128 (regno
);
1069 regno
= insn
->u
.ri
.reg
;
1070 offset
= insn
->u
.ri
.offset
/ DWARF2_CIE_DATA_ALIGNMENT
;
1073 out_one (DW_CFA_offset_extended_sf
);
1074 out_uleb128 (regno
);
1075 out_sleb128 (offset
);
1077 else if (regno
<= 0x3F)
1079 out_one (DW_CFA_offset
+ regno
);
1080 out_uleb128 (offset
);
1084 out_one (DW_CFA_offset_extended
);
1085 out_uleb128 (regno
);
1086 out_uleb128 (offset
);
1090 case DW_CFA_register
:
1091 out_one (DW_CFA_register
);
1092 out_uleb128 (insn
->u
.rr
.reg1
);
1093 out_uleb128 (insn
->u
.rr
.reg2
);
1096 case DW_CFA_remember_state
:
1097 case DW_CFA_restore_state
:
1098 out_one (insn
->insn
);
1101 case DW_CFA_GNU_window_save
:
1102 out_one (DW_CFA_GNU_window_save
);
1107 struct cfi_escape_data
*e
;
1108 for (e
= insn
->u
.esc
; e
; e
= e
->next
)
1109 emit_expr (&e
->exp
, 1);
1113 case CFI_val_encoded_addr
:
1115 unsigned encoding
= insn
->u
.ea
.encoding
;
1116 offsetT encoding_size
;
1118 if (encoding
== DW_EH_PE_omit
)
1120 out_one (DW_CFA_val_expression
);
1121 out_uleb128 (insn
->u
.ea
.reg
);
1123 switch (encoding
& 0x7)
1125 case DW_EH_PE_absptr
:
1126 encoding_size
= DWARF2_ADDR_SIZE (stdoutput
);
1128 case DW_EH_PE_udata2
:
1131 case DW_EH_PE_udata4
:
1134 case DW_EH_PE_udata8
:
1141 /* If the user has requested absolute encoding,
1142 then use the smaller DW_OP_addr encoding. */
1143 if (insn
->u
.ea
.encoding
== DW_EH_PE_absptr
)
1145 out_uleb128 (1 + encoding_size
);
1146 out_one (DW_OP_addr
);
1150 out_uleb128 (1 + 1 + encoding_size
);
1151 out_one (DW_OP_GNU_encoded_addr
);
1154 if ((encoding
& 0x70) == DW_EH_PE_pcrel
)
1156 #if CFI_DIFF_EXPR_OK
1157 insn
->u
.ea
.exp
.X_op
= O_subtract
;
1158 insn
->u
.ea
.exp
.X_op_symbol
= symbol_temp_new_now ();
1159 #elif defined (tc_cfi_emit_pcrel_expr)
1160 tc_cfi_emit_pcrel_expr (&insn
->u
.ea
.exp
, encoding_size
);
1167 emit_expr (&insn
->u
.ea
.exp
, encoding_size
);
1177 encoding_size (unsigned char encoding
)
1179 if (encoding
== DW_EH_PE_omit
)
1181 switch (encoding
& 0x7)
1184 return bfd_get_arch_size (stdoutput
) == 64 ? 8 : 4;
1185 case DW_EH_PE_udata2
:
1187 case DW_EH_PE_udata4
:
1189 case DW_EH_PE_udata8
:
1197 output_cie (struct cie_entry
*cie
)
1199 symbolS
*after_size_address
, *end_address
;
1201 struct cfi_insn_data
*i
;
1202 offsetT augmentation_size
;
1205 cie
->start_address
= symbol_temp_new_now ();
1206 after_size_address
= symbol_temp_make ();
1207 end_address
= symbol_temp_make ();
1209 exp
.X_op
= O_subtract
;
1210 exp
.X_add_symbol
= end_address
;
1211 exp
.X_op_symbol
= after_size_address
;
1212 exp
.X_add_number
= 0;
1214 emit_expr (&exp
, 4); /* Length. */
1215 symbol_set_value_now (after_size_address
);
1216 out_four (0); /* CIE id. */
1217 out_one (DW_CIE_VERSION
); /* Version. */
1218 out_one ('z'); /* Augmentation. */
1219 if (cie
->per_encoding
!= DW_EH_PE_omit
)
1221 if (cie
->lsda_encoding
!= DW_EH_PE_omit
)
1224 if (cie
->signal_frame
)
1227 out_uleb128 (DWARF2_LINE_MIN_INSN_LENGTH
); /* Code alignment. */
1228 out_sleb128 (DWARF2_CIE_DATA_ALIGNMENT
); /* Data alignment. */
1229 if (DW_CIE_VERSION
== 1) /* Return column. */
1230 out_one (cie
->return_column
);
1232 out_uleb128 (cie
->return_column
);
1233 augmentation_size
= 1 + (cie
->lsda_encoding
!= DW_EH_PE_omit
);
1234 if (cie
->per_encoding
!= DW_EH_PE_omit
)
1235 augmentation_size
+= 1 + encoding_size (cie
->per_encoding
);
1236 out_uleb128 (augmentation_size
); /* Augmentation size. */
1237 if (cie
->per_encoding
!= DW_EH_PE_omit
)
1239 offsetT size
= encoding_size (cie
->per_encoding
);
1240 out_one (cie
->per_encoding
);
1241 exp
= cie
->personality
;
1242 if ((cie
->per_encoding
& 0x70) == DW_EH_PE_pcrel
)
1244 #if CFI_DIFF_EXPR_OK
1245 exp
.X_op
= O_subtract
;
1246 exp
.X_op_symbol
= symbol_temp_new_now ();
1247 emit_expr (&exp
, size
);
1248 #elif defined (tc_cfi_emit_pcrel_expr)
1249 tc_cfi_emit_pcrel_expr (&exp
, size
);
1255 emit_expr (&exp
, size
);
1257 if (cie
->lsda_encoding
!= DW_EH_PE_omit
)
1258 out_one (cie
->lsda_encoding
);
1260 switch (DWARF2_FDE_RELOC_SIZE
)
1263 enc
= DW_EH_PE_sdata2
;
1266 enc
= DW_EH_PE_sdata4
;
1269 enc
= DW_EH_PE_sdata8
;
1274 #if CFI_DIFF_EXPR_OK || defined tc_cfi_emit_pcrel_expr
1275 enc
|= DW_EH_PE_pcrel
;
1280 for (i
= cie
->first
; i
!= cie
->last
; i
= i
->next
)
1281 output_cfi_insn (i
);
1283 frag_align (2, DW_CFA_nop
, 0);
1284 symbol_set_value_now (end_address
);
1288 output_fde (struct fde_entry
*fde
, struct cie_entry
*cie
,
1289 struct cfi_insn_data
*first
, int align
)
1291 symbolS
*after_size_address
, *end_address
;
1293 offsetT augmentation_size
;
1295 after_size_address
= symbol_temp_make ();
1296 end_address
= symbol_temp_make ();
1298 exp
.X_op
= O_subtract
;
1299 exp
.X_add_symbol
= end_address
;
1300 exp
.X_op_symbol
= after_size_address
;
1301 exp
.X_add_number
= 0;
1302 emit_expr (&exp
, 4); /* Length. */
1303 symbol_set_value_now (after_size_address
);
1305 exp
.X_add_symbol
= after_size_address
;
1306 exp
.X_op_symbol
= cie
->start_address
;
1307 emit_expr (&exp
, 4); /* CIE offset. */
1309 #if CFI_DIFF_EXPR_OK
1310 exp
.X_add_symbol
= fde
->start_address
;
1311 exp
.X_op_symbol
= symbol_temp_new_now ();
1312 emit_expr (&exp
, DWARF2_FDE_RELOC_SIZE
); /* Code offset. */
1314 exp
.X_op
= O_symbol
;
1315 exp
.X_add_symbol
= fde
->start_address
;
1316 exp
.X_op_symbol
= NULL
;
1317 #ifdef tc_cfi_emit_pcrel_expr
1318 tc_cfi_emit_pcrel_expr (&exp
, DWARF2_FDE_RELOC_SIZE
); /* Code offset. */
1320 emit_expr (&exp
, DWARF2_FDE_RELOC_SIZE
); /* Code offset. */
1322 exp
.X_op
= O_subtract
;
1325 exp
.X_add_symbol
= fde
->end_address
;
1326 exp
.X_op_symbol
= fde
->start_address
; /* Code length. */
1327 emit_expr (&exp
, DWARF2_FDE_RELOC_SIZE
);
1329 augmentation_size
= encoding_size (fde
->lsda_encoding
);
1330 out_uleb128 (augmentation_size
); /* Augmentation size. */
1332 if (fde
->lsda_encoding
!= DW_EH_PE_omit
)
1335 if ((fde
->lsda_encoding
& 0x70) == DW_EH_PE_pcrel
)
1337 #if CFI_DIFF_EXPR_OK
1338 exp
.X_op
= O_subtract
;
1339 exp
.X_op_symbol
= symbol_temp_new_now ();
1340 emit_expr (&exp
, augmentation_size
);
1341 #elif defined (tc_cfi_emit_pcrel_expr)
1342 tc_cfi_emit_pcrel_expr (&exp
, augmentation_size
);
1348 emit_expr (&exp
, augmentation_size
);
1351 for (; first
; first
= first
->next
)
1352 output_cfi_insn (first
);
1354 frag_align (align
, DW_CFA_nop
, 0);
1355 symbol_set_value_now (end_address
);
1358 static struct cie_entry
*
1359 select_cie_for_fde (struct fde_entry
*fde
, struct cfi_insn_data
**pfirst
)
1361 struct cfi_insn_data
*i
, *j
;
1362 struct cie_entry
*cie
;
1364 for (cie
= cie_root
; cie
; cie
= cie
->next
)
1366 if (cie
->return_column
!= fde
->return_column
1367 || cie
->signal_frame
!= fde
->signal_frame
1368 || cie
->per_encoding
!= fde
->per_encoding
1369 || cie
->lsda_encoding
!= fde
->lsda_encoding
)
1371 if (cie
->per_encoding
!= DW_EH_PE_omit
)
1373 if (cie
->personality
.X_op
!= fde
->personality
.X_op
1374 || cie
->personality
.X_add_number
1375 != fde
->personality
.X_add_number
)
1377 switch (cie
->personality
.X_op
)
1380 if (cie
->personality
.X_unsigned
!= fde
->personality
.X_unsigned
)
1384 if (cie
->personality
.X_add_symbol
1385 != fde
->personality
.X_add_symbol
)
1392 for (i
= cie
->first
, j
= fde
->data
;
1393 i
!= cie
->last
&& j
!= NULL
;
1394 i
= i
->next
, j
= j
->next
)
1396 if (i
->insn
!= j
->insn
)
1400 case DW_CFA_advance_loc
:
1401 case DW_CFA_remember_state
:
1402 /* We reached the first advance/remember in the FDE,
1403 but did not reach the end of the CIE list. */
1407 case DW_CFA_def_cfa
:
1408 if (i
->u
.ri
.reg
!= j
->u
.ri
.reg
)
1410 if (i
->u
.ri
.offset
!= j
->u
.ri
.offset
)
1414 case DW_CFA_register
:
1415 if (i
->u
.rr
.reg1
!= j
->u
.rr
.reg1
)
1417 if (i
->u
.rr
.reg2
!= j
->u
.rr
.reg2
)
1421 case DW_CFA_def_cfa_register
:
1422 case DW_CFA_restore
:
1423 case DW_CFA_undefined
:
1424 case DW_CFA_same_value
:
1425 if (i
->u
.r
!= j
->u
.r
)
1429 case DW_CFA_def_cfa_offset
:
1430 if (i
->u
.i
!= j
->u
.i
)
1435 case CFI_val_encoded_addr
:
1436 /* Don't bother matching these for now. */
1444 /* Success if we reached the end of the CIE list, and we've either
1445 run out of FDE entries or we've encountered an advance,
1446 remember, or escape. */
1449 || j
->insn
== DW_CFA_advance_loc
1450 || j
->insn
== DW_CFA_remember_state
1451 || j
->insn
== CFI_escape
1452 || j
->insn
== CFI_val_encoded_addr
))
1461 cie
= (struct cie_entry
*) xmalloc (sizeof (struct cie_entry
));
1462 cie
->next
= cie_root
;
1464 cie
->return_column
= fde
->return_column
;
1465 cie
->signal_frame
= fde
->signal_frame
;
1466 cie
->per_encoding
= fde
->per_encoding
;
1467 cie
->lsda_encoding
= fde
->lsda_encoding
;
1468 cie
->personality
= fde
->personality
;
1469 cie
->first
= fde
->data
;
1471 for (i
= cie
->first
; i
; i
= i
->next
)
1472 if (i
->insn
== DW_CFA_advance_loc
1473 || i
->insn
== DW_CFA_remember_state
1474 || i
->insn
== CFI_escape
1475 || i
->insn
== CFI_val_encoded_addr
)
1490 struct fde_entry
*fde
;
1491 int save_flag_traditional_format
;
1493 if (all_fde_data
== 0)
1496 /* Open .eh_frame section. */
1497 cfi_seg
= subseg_new (".eh_frame", 0);
1498 bfd_set_section_flags (stdoutput
, cfi_seg
,
1499 SEC_ALLOC
| SEC_LOAD
| SEC_DATA
1500 | DWARF2_EH_FRAME_READ_ONLY
);
1501 subseg_set (cfi_seg
, 0);
1502 record_alignment (cfi_seg
, EH_FRAME_ALIGNMENT
);
1504 #ifdef md_fix_up_eh_frame
1505 md_fix_up_eh_frame (cfi_seg
);
1508 /* Make sure check_eh_frame doesn't do anything with our output. */
1509 save_flag_traditional_format
= flag_traditional_format
;
1510 flag_traditional_format
= 1;
1512 for (fde
= all_fde_data
; fde
; fde
= fde
->next
)
1514 struct cfi_insn_data
*first
;
1515 struct cie_entry
*cie
;
1517 if (fde
->end_address
== NULL
)
1519 as_bad (_("open CFI at the end of file; missing .cfi_endproc directive"));
1520 fde
->end_address
= fde
->start_address
;
1523 cie
= select_cie_for_fde (fde
, &first
);
1524 output_fde (fde
, cie
, first
, fde
->next
== NULL
? EH_FRAME_ALIGNMENT
: 2);
1527 flag_traditional_format
= save_flag_traditional_format
;
1530 #else /* TARGET_USE_CFIPOP */
1535 #endif /* TARGET_USE_CFIPOP */