bfd/
[binutils/dougsmingw.git] / gas / dw2gencfi.c
blob36bd8d8d1db48b16a072eb9bf3625d38756591ab
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)
11 any later version.
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
21 02110-1301, USA. */
23 #include "as.h"
24 #include "dw2gencfi.h"
25 #include "subsegs.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
32 # ifdef DIFF_EXPR_OK
33 # define CFI_DIFF_EXPR_OK 1
34 # else
35 # define CFI_DIFF_EXPR_OK 0
36 # endif
37 #endif
39 #ifndef CFI_DIFF_LSDA_OK
40 # define CFI_DIFF_LSDA_OK CFI_DIFF_EXPR_OK
41 #endif
43 #if CFI_DIFF_EXPR_OK == 1 && CFI_DIFF_LSDA_OK == 0
44 # error "CFI_DIFF_EXPR_OK should imply CFI_DIFF_LSDA_OK"
45 #endif
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
51 #endif
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
56 #endif
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
61 #endif
63 #ifndef EH_FRAME_ALIGNMENT
64 # define EH_FRAME_ALIGNMENT (bfd_get_arch_size (stdoutput) == 64 ? 3 : 2)
65 #endif
67 #ifndef tc_cfi_frame_initial_instructions
68 # define tc_cfi_frame_initial_instructions() ((void)0)
69 #endif
71 #ifndef DWARF2_FORMAT
72 # define DWARF2_FORMAT(SEC) dwarf2_format_32bit
73 #endif
75 #ifndef DWARF2_ADDR_SIZE
76 # define DWARF2_ADDR_SIZE(bfd) (bfd_arch_bits_per_address (bfd) / 8)
77 #endif
79 struct cfi_escape_data {
80 struct cfi_escape_data *next;
81 expressionS exp;
84 struct cfi_insn_data
86 struct cfi_insn_data *next;
87 int insn;
88 union {
89 struct {
90 unsigned reg;
91 offsetT offset;
92 } ri;
94 struct {
95 unsigned reg1;
96 unsigned reg2;
97 } rr;
99 unsigned r;
100 offsetT i;
102 struct {
103 symbolS *lab1;
104 symbolS *lab2;
105 } ll;
107 struct cfi_escape_data *esc;
109 struct {
110 unsigned reg, encoding;
111 expressionS exp;
112 } ea;
113 } u;
116 struct fde_entry
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;
126 expressionS lsda;
127 unsigned int return_column;
128 unsigned int signal_frame;
131 struct cie_entry
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. */
152 struct cfa_save_data
154 struct cfa_save_data *next;
155 offsetT cfa_offset;
158 /* Current open FDE entry. */
159 struct frch_cfi_data
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;
186 return fde;
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;
205 return insn;
208 /* Construct a new FDE structure that begins at LABEL. */
210 void
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. */
220 void
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. */
230 void
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. */
238 static void
239 cfi_add_CFA_insn(int insn)
241 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
243 insn_ptr->insn = insn;
246 static void
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;
255 static void
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;
264 static void
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;
274 static void
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. */
286 void
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. */
300 void
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. */
316 void
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. */
325 void
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. */
333 void
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. */
341 void
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;
348 void
349 cfi_add_CFA_restore (unsigned regno)
351 cfi_add_CFA_insn_reg (DW_CFA_restore, regno);
354 void
355 cfi_add_CFA_undefined (unsigned regno)
357 cfi_add_CFA_insn_reg (DW_CFA_undefined, regno);
360 void
361 cfi_add_CFA_same_value (unsigned regno)
363 cfi_add_CFA_insn_reg (DW_CFA_same_value, regno);
366 void
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;
379 void
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;
387 if (p)
389 frchain_now->frch_cfi_data->cur_cfa_offset = p->cfa_offset;
390 frchain_now->frch_cfi_data->cfa_save_stack = p->next;
391 free (p);
393 else
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 },
441 { NULL, NULL, 0 }
444 static void
445 cfi_parse_separator (void)
447 SKIP_WHITESPACE ();
448 if (*input_line_pointer == ',')
449 input_line_pointer++;
450 else
451 as_bad (_("missing separator"));
454 #ifndef tc_parse_to_dw2regnum
455 static void
456 tc_parse_to_dw2regnum(expressionS *exp)
458 # ifdef tc_regname_to_dw2regnum
459 SKIP_WHITESPACE ();
460 if (is_name_beginner (*input_line_pointer)
461 || (*input_line_pointer == '%'
462 && is_name_beginner (*++input_line_pointer)))
464 char *name, c;
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;
474 else
475 # endif
476 expression_and_evaluate (exp);
478 #endif
480 static unsigned
481 cfi_parse_reg (void)
483 int regno;
484 expressionS exp;
486 tc_parse_to_dw2regnum (&exp);
487 switch (exp.X_op)
489 case O_register:
490 case O_constant:
491 regno = exp.X_add_number;
492 break;
494 default:
495 regno = -1;
496 break;
499 if (regno < 0)
501 as_bad (_("bad register expression"));
502 regno = 0;
505 return regno;
508 static offsetT
509 cfi_parse_const (void)
511 return get_absolute_expression ();
514 static void
515 dot_cfi (int arg)
517 offsetT offset;
518 unsigned reg1, reg2;
520 if (frchain_now->frch_cfi_data == NULL)
522 as_bad (_("CFI instruction used without previous .cfi_startproc"));
523 ignore_rest_of_line ();
524 return;
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)
530 != frag_now_fix ())
531 cfi_add_advance_loc (symbol_temp_new_now ());
533 switch (arg)
535 case DW_CFA_offset:
536 reg1 = cfi_parse_reg ();
537 cfi_parse_separator ();
538 offset = cfi_parse_const ();
539 cfi_add_CFA_offset (reg1, offset);
540 break;
542 case CFI_rel_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);
548 break;
550 case DW_CFA_def_cfa:
551 reg1 = cfi_parse_reg ();
552 cfi_parse_separator ();
553 offset = cfi_parse_const ();
554 cfi_add_CFA_def_cfa (reg1, offset);
555 break;
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);
562 break;
564 case DW_CFA_def_cfa_register:
565 reg1 = cfi_parse_reg ();
566 cfi_add_CFA_def_cfa_register (reg1);
567 break;
569 case DW_CFA_def_cfa_offset:
570 offset = cfi_parse_const ();
571 cfi_add_CFA_def_cfa_offset (offset);
572 break;
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
577 + offset);
578 break;
580 case DW_CFA_restore:
581 for (;;)
583 reg1 = cfi_parse_reg ();
584 cfi_add_CFA_restore (reg1);
585 SKIP_WHITESPACE ();
586 if (*input_line_pointer != ',')
587 break;
588 ++input_line_pointer;
590 break;
592 case DW_CFA_undefined:
593 for (;;)
595 reg1 = cfi_parse_reg ();
596 cfi_add_CFA_undefined (reg1);
597 SKIP_WHITESPACE ();
598 if (*input_line_pointer != ',')
599 break;
600 ++input_line_pointer;
602 break;
604 case DW_CFA_same_value:
605 reg1 = cfi_parse_reg ();
606 cfi_add_CFA_same_value (reg1);
607 break;
609 case CFI_return_column:
610 reg1 = cfi_parse_reg ();
611 cfi_set_return_column (reg1);
612 break;
614 case DW_CFA_remember_state:
615 cfi_add_CFA_remember_state ();
616 break;
618 case DW_CFA_restore_state:
619 cfi_add_CFA_restore_state ();
620 break;
622 case DW_CFA_GNU_window_save:
623 cfi_add_CFA_insn (DW_CFA_GNU_window_save);
624 break;
626 case CFI_signal_frame:
627 frchain_now->frch_cfi_data->cur_fde_data->signal_frame = 1;
628 break;
630 default:
631 abort ();
634 demand_empty_rest_of_line ();
637 static void
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 ();
647 return;
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)
653 != frag_now_fix ())
654 cfi_add_advance_loc (symbol_temp_new_now ());
656 tail = &head;
659 e = (struct cfi_escape_data *) xmalloc (sizeof (*e));
660 do_parse_cons_expression (&e->exp, 1);
661 *tail = e;
662 tail = &e->next;
664 while (*input_line_pointer++ == ',');
665 *tail = NULL;
667 insn = alloc_cfi_insn_data ();
668 insn->insn = CFI_escape;
669 insn->u.esc = head;
671 --input_line_pointer;
672 demand_empty_rest_of_line ();
675 static void
676 dot_cfi_personality (int ignored ATTRIBUTE_UNUSED)
678 struct fde_entry *fde;
679 offsetT encoding;
681 if (frchain_now->frch_cfi_data == NULL)
683 as_bad (_("CFI instruction used without previous .cfi_startproc"));
684 ignore_rest_of_line ();
685 return;
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;
694 return;
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
701 #endif
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 ();
709 return;
712 if (*input_line_pointer++ != ',')
714 as_bad (_(".cfi_personality requires encoding and symbol arguments"));
715 ignore_rest_of_line ();
716 return;
719 expression_and_evaluate (&fde->personality);
720 switch (fde->personality.X_op)
722 case O_symbol:
723 break;
724 case O_constant:
725 if ((encoding & 0x70) == DW_EH_PE_pcrel)
726 encoding = DW_EH_PE_omit;
727 break;
728 default:
729 encoding = DW_EH_PE_omit;
730 break;
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 ();
739 return;
742 demand_empty_rest_of_line ();
745 static void
746 dot_cfi_lsda (int ignored ATTRIBUTE_UNUSED)
748 struct fde_entry *fde;
749 offsetT encoding;
751 if (frchain_now->frch_cfi_data == NULL)
753 as_bad (_("CFI instruction used without previous .cfi_startproc"));
754 ignore_rest_of_line ();
755 return;
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;
764 return;
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
771 #endif
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 ();
779 return;
782 if (*input_line_pointer++ != ',')
784 as_bad (_(".cfi_lsda requires encoding and symbol arguments"));
785 ignore_rest_of_line ();
786 return;
789 fde->lsda_encoding = encoding;
791 expression_and_evaluate (&fde->lsda);
792 switch (fde->lsda.X_op)
794 case O_symbol:
795 break;
796 case O_constant:
797 if ((encoding & 0x70) == DW_EH_PE_pcrel)
798 encoding = DW_EH_PE_omit;
799 break;
800 default:
801 encoding = DW_EH_PE_omit;
802 break;
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 ();
811 return;
814 demand_empty_rest_of_line ();
817 static void
818 dot_cfi_val_encoded_addr (int ignored ATTRIBUTE_UNUSED)
820 struct cfi_insn_data *insn_ptr;
821 offsetT encoding;
823 if (frchain_now->frch_cfi_data == NULL)
825 as_bad (_("CFI instruction used without previous .cfi_startproc"));
826 ignore_rest_of_line ();
827 return;
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)
833 != frag_now_fix ())
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
847 #endif
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)
861 case O_symbol:
862 break;
863 case O_constant:
864 if ((encoding & 0x70) != DW_EH_PE_pcrel)
865 break;
866 default:
867 encoding = DW_EH_PE_omit;
868 break;
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 ();
876 return;
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;
887 static void
888 dot_cfi_sections (int ignored ATTRIBUTE_UNUSED)
890 int sections = 0;
892 SKIP_WHITESPACE ();
893 if (is_name_beginner (*input_line_pointer))
894 while (1)
896 char *name, c;
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;
905 else
907 *input_line_pointer = c;
908 input_line_pointer = name;
909 break;
912 *input_line_pointer = c;
913 SKIP_WHITESPACE ();
914 if (*input_line_pointer == ',')
916 name = input_line_pointer++;
917 SKIP_WHITESPACE ();
918 if (!is_name_beginner (*input_line_pointer))
920 input_line_pointer = name;
921 break;
924 else if (is_name_beginner (*input_line_pointer))
925 break;
928 demand_empty_rest_of_line ();
929 cfi_sections = sections;
932 static void
933 dot_cfi_startproc (int ignored ATTRIBUTE_UNUSED)
935 int simple = 0;
937 if (frchain_now->frch_cfi_data != NULL)
939 as_bad (_("previous CFI entry not closed (missing .cfi_endproc)"));
940 ignore_rest_of_line ();
941 return;
944 cfi_new_fde (symbol_temp_new_now ());
946 SKIP_WHITESPACE ();
947 if (is_name_beginner (*input_line_pointer))
949 char *name, c;
951 name = input_line_pointer;
952 c = get_symbol_end ();
954 if (strcmp (name, "simple") == 0)
956 simple = 1;
957 *input_line_pointer = c;
959 else
960 input_line_pointer = name;
962 demand_empty_rest_of_line ();
964 frchain_now->frch_cfi_data->cur_cfa_offset = 0;
965 if (!simple)
966 tc_cfi_frame_initial_instructions ();
969 static void
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 ();
976 return;
979 cfi_end_fde (symbol_temp_new_now ());
981 demand_empty_rest_of_line ();
985 /* Emit a single byte into the current segment. */
987 static inline void
988 out_one (int byte)
990 FRAG_APPEND_1_CHAR (byte);
993 /* Emit a two-byte word into the current segment. */
995 static inline void
996 out_two (int data)
998 md_number_to_chars (frag_more (2), data, 2);
1001 /* Emit a four byte word into the current segment. */
1003 static inline void
1004 out_four (int data)
1006 md_number_to_chars (frag_more (4), data, 4);
1009 /* Emit an unsigned "little-endian base 128" number. */
1011 static void
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. */
1019 static void
1020 out_sleb128 (offsetT value)
1022 output_leb128 (frag_more (sizeof_leb128 (value, 1)), value, 1);
1025 static void
1026 output_cfi_insn (struct cfi_insn_data *insn)
1028 offsetT offset;
1029 unsigned int regno;
1031 switch (insn->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;
1043 if (scaled <= 0x3F)
1044 out_one (DW_CFA_advance_loc + scaled);
1045 else if (scaled <= 0xFF)
1047 out_one (DW_CFA_advance_loc1);
1048 out_one (scaled);
1050 else if (scaled <= 0xFFFF)
1052 out_one (DW_CFA_advance_loc2);
1053 out_two (scaled);
1055 else
1057 out_one (DW_CFA_advance_loc4);
1058 out_four (scaled);
1061 else
1063 expressionS exp;
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,
1078 (char *) frag_now);
1081 break;
1083 case DW_CFA_def_cfa:
1084 offset = insn->u.ri.offset;
1085 if (offset < 0)
1087 out_one (DW_CFA_def_cfa_sf);
1088 out_uleb128 (insn->u.ri.reg);
1089 out_sleb128 (offset / DWARF2_CIE_DATA_ALIGNMENT);
1091 else
1093 out_one (DW_CFA_def_cfa);
1094 out_uleb128 (insn->u.ri.reg);
1095 out_uleb128 (offset);
1097 break;
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);
1104 break;
1106 case DW_CFA_def_cfa_offset:
1107 offset = insn->u.i;
1108 if (offset < 0)
1110 out_one (DW_CFA_def_cfa_offset_sf);
1111 out_sleb128 (offset / DWARF2_CIE_DATA_ALIGNMENT);
1113 else
1115 out_one (DW_CFA_def_cfa_offset);
1116 out_uleb128 (offset);
1118 break;
1120 case DW_CFA_restore:
1121 regno = insn->u.r;
1122 if (regno <= 0x3F)
1124 out_one (DW_CFA_restore + regno);
1126 else
1128 out_one (DW_CFA_restore_extended);
1129 out_uleb128 (regno);
1131 break;
1133 case DW_CFA_offset:
1134 regno = insn->u.ri.reg;
1135 offset = insn->u.ri.offset / DWARF2_CIE_DATA_ALIGNMENT;
1136 if (offset < 0)
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);
1147 else
1149 out_one (DW_CFA_offset_extended);
1150 out_uleb128 (regno);
1151 out_uleb128 (offset);
1153 break;
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);
1159 break;
1161 case DW_CFA_remember_state:
1162 case DW_CFA_restore_state:
1163 out_one (insn->insn);
1164 break;
1166 case DW_CFA_GNU_window_save:
1167 out_one (DW_CFA_GNU_window_save);
1168 break;
1170 case CFI_escape:
1172 struct cfi_escape_data *e;
1173 for (e = insn->u.esc; e ; e = e->next)
1174 emit_expr (&e->exp, 1);
1175 break;
1178 case CFI_val_encoded_addr:
1180 unsigned encoding = insn->u.ea.encoding;
1181 offsetT encoding_size;
1183 if (encoding == DW_EH_PE_omit)
1184 break;
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);
1192 break;
1193 case DW_EH_PE_udata2:
1194 encoding_size = 2;
1195 break;
1196 case DW_EH_PE_udata4:
1197 encoding_size = 4;
1198 break;
1199 case DW_EH_PE_udata8:
1200 encoding_size = 8;
1201 break;
1202 default:
1203 abort ();
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);
1213 else
1215 out_uleb128 (1 + 1 + encoding_size);
1216 out_one (DW_OP_GNU_encoded_addr);
1217 out_one (encoding);
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);
1226 break;
1227 #else
1228 abort ();
1229 #endif
1232 emit_expr (&insn->u.ea.exp, encoding_size);
1234 break;
1236 default:
1237 abort ();
1241 static offsetT
1242 encoding_size (unsigned char encoding)
1244 if (encoding == DW_EH_PE_omit)
1245 return 0;
1246 switch (encoding & 0x7)
1248 case 0:
1249 return bfd_get_arch_size (stdoutput) == 64 ? 8 : 4;
1250 case DW_EH_PE_udata2:
1251 return 2;
1252 case DW_EH_PE_udata4:
1253 return 4;
1254 case DW_EH_PE_udata8:
1255 return 8;
1256 default:
1257 abort ();
1261 static void
1262 output_cie (struct cie_entry *cie, bfd_boolean eh_frame, int align)
1264 symbolS *after_size_address, *end_address;
1265 expressionS exp;
1266 struct cfi_insn_data *i;
1267 offsetT augmentation_size;
1268 int enc;
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. */
1282 else
1284 if (fmt == dwarf2_format_64bit)
1285 out_four (-1);
1286 emit_expr (&exp, 8); /* Length. */
1288 symbol_set_value_now (after_size_address);
1289 if (eh_frame)
1290 out_four (0); /* CIE id. */
1291 else
1293 out_four (-1); /* CIE id. */
1294 if (fmt != dwarf2_format_32bit)
1295 out_four (-1);
1297 out_one (DW_CIE_VERSION); /* Version. */
1298 if (eh_frame)
1300 out_one ('z'); /* Augmentation. */
1301 if (cie->per_encoding != DW_EH_PE_omit)
1302 out_one ('P');
1303 if (cie->lsda_encoding != DW_EH_PE_omit)
1304 out_one ('L');
1305 out_one ('R');
1307 if (cie->signal_frame)
1308 out_one ('S');
1309 out_one (0);
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);
1314 else
1315 out_uleb128 (cie->return_column);
1316 if (eh_frame)
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);
1336 #else
1337 abort ();
1338 #endif
1340 else
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)
1350 case 2:
1351 enc = DW_EH_PE_sdata2;
1352 break;
1353 case 4:
1354 enc = DW_EH_PE_sdata4;
1355 break;
1356 case 8:
1357 enc = DW_EH_PE_sdata8;
1358 break;
1359 default:
1360 abort ();
1362 #if CFI_DIFF_EXPR_OK || defined tc_cfi_emit_pcrel_expr
1363 enc |= DW_EH_PE_pcrel;
1364 #endif
1365 if (eh_frame)
1366 out_one (enc);
1368 if (cie->first)
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);
1376 static void
1377 output_fde (struct fde_entry *fde, struct cie_entry *cie,
1378 bfd_boolean eh_frame, struct cfi_insn_data *first,
1379 int align)
1381 symbolS *after_size_address, *end_address;
1382 expressionS exp;
1383 offsetT augmentation_size;
1384 enum dwarf2_format fmt = DWARF2_FORMAT (now_seg);
1385 int offset_size;
1386 int addr_size;
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)
1396 offset_size = 4;
1397 else
1399 if (fmt == dwarf2_format_64bit)
1400 out_four (-1);
1401 offset_size = 8;
1403 emit_expr (&exp, offset_size); /* Length. */
1404 symbol_set_value_now (after_size_address);
1406 if (eh_frame)
1408 exp.X_add_symbol = after_size_address;
1409 exp.X_op_symbol = cie->start_address;
1411 else
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. */
1419 if (eh_frame)
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. */
1425 #else
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. */
1431 #else
1432 emit_expr (&exp, DWARF2_FDE_RELOC_SIZE); /* Code offset. */
1433 #endif
1434 #endif
1435 addr_size = DWARF2_FDE_RELOC_SIZE;
1437 else
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);
1450 if (eh_frame)
1451 out_uleb128 (augmentation_size); /* Augmentation size. */
1453 if (fde->lsda_encoding != DW_EH_PE_omit)
1455 exp = fde->lsda;
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);
1464 #else
1465 abort ();
1466 #endif
1468 else
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)
1492 continue;
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)
1498 continue;
1499 switch (cie->personality.X_op)
1501 case O_constant:
1502 if (cie->personality.X_unsigned != fde->personality.X_unsigned)
1503 continue;
1504 break;
1505 case O_symbol:
1506 if (cie->personality.X_add_symbol
1507 != fde->personality.X_add_symbol)
1508 continue;
1509 break;
1510 default:
1511 abort ();
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)
1519 goto fail;
1520 switch (i->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. */
1526 goto fail;
1528 case DW_CFA_offset:
1529 case DW_CFA_def_cfa:
1530 if (i->u.ri.reg != j->u.ri.reg)
1531 goto fail;
1532 if (i->u.ri.offset != j->u.ri.offset)
1533 goto fail;
1534 break;
1536 case DW_CFA_register:
1537 if (i->u.rr.reg1 != j->u.rr.reg1)
1538 goto fail;
1539 if (i->u.rr.reg2 != j->u.rr.reg2)
1540 goto fail;
1541 break;
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)
1548 goto fail;
1549 break;
1551 case DW_CFA_def_cfa_offset:
1552 if (i->u.i != j->u.i)
1553 goto fail;
1554 break;
1556 case CFI_escape:
1557 case CFI_val_encoded_addr:
1558 /* Don't bother matching these for now. */
1559 goto fail;
1561 default:
1562 abort ();
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. */
1569 if (i == cie->last
1570 && (!j
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))
1576 *pfirst = j;
1577 return cie;
1580 fail:;
1583 cie = (struct cie_entry *) xmalloc (sizeof (struct cie_entry));
1584 cie->next = cie_root;
1585 cie_root = cie;
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)
1598 break;
1600 cie->last = i;
1601 *pfirst = i;
1603 output_cie (cie, eh_frame, align);
1605 return cie;
1608 #ifdef md_reg_eh_frame_to_debug_frame
1609 static void
1610 cfi_change_reg_numbers (struct cfi_insn_data *insn)
1612 for (; insn; insn = insn->next)
1613 switch (insn->insn)
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:
1620 case CFI_escape:
1621 break;
1623 case DW_CFA_def_cfa:
1624 case DW_CFA_offset:
1625 insn->u.ri.reg = md_reg_eh_frame_to_debug_frame (insn->u.ri.reg);
1626 break;
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);
1633 break;
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);
1638 break;
1640 case CFI_val_encoded_addr:
1641 insn->u.ea.reg = md_reg_eh_frame_to_debug_frame (insn->u.ea.reg);
1642 break;
1644 default:
1645 abort ();
1648 #else
1649 #define cfi_change_reg_numbers(insn) do { } while (0)
1650 #endif
1652 void
1653 cfi_finish (void)
1655 segT cfi_seg;
1656 struct fde_entry *fde;
1657 int save_flag_traditional_format;
1659 if (all_fde_data == 0)
1660 return;
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);
1674 #endif
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);
1709 cie_root = NULL;
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 */
1738 void
1739 cfi_finish (void)
1742 #endif /* TARGET_USE_CFIPOP */