* c-arm.texi: Add tutorial on ARM unwinding pseudo ops.
[binutils.git] / gas / dw2gencfi.c
blob520d1e1734ada55eff695af6485c4e782828bc7e
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)
10 any later version.
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
20 02110-1301, USA. */
22 #include "as.h"
23 #include "dw2gencfi.h"
24 #include "subsegs.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
32 #endif
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
37 #endif
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
42 #endif
44 #ifndef EH_FRAME_ALIGNMENT
45 # define EH_FRAME_ALIGNMENT (bfd_get_arch_size (stdoutput) == 64 ? 3 : 2)
46 #endif
48 #ifndef tc_cfi_frame_initial_instructions
49 # define tc_cfi_frame_initial_instructions() ((void)0)
50 #endif
53 struct cfi_insn_data
55 struct cfi_insn_data *next;
56 int insn;
57 union {
58 struct {
59 unsigned reg;
60 offsetT offset;
61 } ri;
63 struct {
64 unsigned reg1;
65 unsigned reg2;
66 } rr;
68 unsigned r;
69 offsetT i;
71 struct {
72 symbolS *lab1;
73 symbolS *lab2;
74 } ll;
76 struct cfi_escape_data {
77 struct cfi_escape_data *next;
78 expressionS exp;
79 } *esc;
80 } u;
83 struct fde_entry
85 struct fde_entry *next;
86 symbolS *start_address;
87 symbolS *end_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;
93 expressionS lsda;
94 unsigned int return_column;
95 unsigned int signal_frame;
98 struct cie_entry
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. */
119 struct cfa_save_data
121 struct cfa_save_data *next;
122 offsetT cfa_offset;
125 /* Current open FDE entry. */
126 struct frch_cfi_data
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;
151 return fde;
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;
169 return insn;
172 /* Construct a new FDE structure that begins at LABEL. */
174 void
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. */
184 void
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. */
194 void
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. */
202 static void
203 cfi_add_CFA_insn(int insn)
205 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
207 insn_ptr->insn = insn;
210 static void
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;
219 static void
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;
228 static void
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;
238 static void
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. */
250 void
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. */
264 void
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. */
280 void
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. */
289 void
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. */
297 void
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. */
305 void
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;
312 void
313 cfi_add_CFA_restore (unsigned regno)
315 cfi_add_CFA_insn_reg (DW_CFA_restore, regno);
318 void
319 cfi_add_CFA_undefined (unsigned regno)
321 cfi_add_CFA_insn_reg (DW_CFA_undefined, regno);
324 void
325 cfi_add_CFA_same_value (unsigned regno)
327 cfi_add_CFA_insn_reg (DW_CFA_same_value, regno);
330 void
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;
343 void
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;
351 if (p)
353 frchain_now->frch_cfi_data->cur_cfa_offset = p->cfa_offset;
354 frchain_now->frch_cfi_data->cfa_save_stack = p->next;
355 free (p);
357 else
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 },
400 { NULL, NULL, 0 }
403 static void
404 cfi_parse_separator (void)
406 SKIP_WHITESPACE ();
407 if (*input_line_pointer == ',')
408 input_line_pointer++;
409 else
410 as_bad (_("missing separator"));
413 #ifndef tc_parse_to_dw2regnum
414 static void
415 tc_parse_to_dw2regnum(expressionS *exp)
417 # ifdef tc_regname_to_dw2regnum
418 SKIP_WHITESPACE ();
419 if (is_name_beginner (*input_line_pointer)
420 || (*input_line_pointer == '%'
421 && is_name_beginner (*++input_line_pointer)))
423 char *name, c;
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;
433 else
434 # endif
435 expression_and_evaluate (exp);
437 #endif
439 static unsigned
440 cfi_parse_reg (void)
442 int regno;
443 expressionS exp;
445 tc_parse_to_dw2regnum (&exp);
446 switch (exp.X_op)
448 case O_register:
449 case O_constant:
450 regno = exp.X_add_number;
451 break;
453 default:
454 regno = -1;
455 break;
458 if (regno < 0)
460 as_bad (_("bad register expression"));
461 regno = 0;
464 return regno;
467 static offsetT
468 cfi_parse_const (void)
470 return get_absolute_expression ();
473 static void
474 dot_cfi (int arg)
476 offsetT offset;
477 unsigned reg1, reg2;
479 if (frchain_now->frch_cfi_data == NULL)
481 as_bad (_("CFI instruction used without previous .cfi_startproc"));
482 ignore_rest_of_line ();
483 return;
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)
489 != frag_now_fix ())
490 cfi_add_advance_loc (symbol_temp_new_now ());
492 switch (arg)
494 case DW_CFA_offset:
495 reg1 = cfi_parse_reg ();
496 cfi_parse_separator ();
497 offset = cfi_parse_const ();
498 cfi_add_CFA_offset (reg1, offset);
499 break;
501 case CFI_rel_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);
507 break;
509 case DW_CFA_def_cfa:
510 reg1 = cfi_parse_reg ();
511 cfi_parse_separator ();
512 offset = cfi_parse_const ();
513 cfi_add_CFA_def_cfa (reg1, offset);
514 break;
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);
521 break;
523 case DW_CFA_def_cfa_register:
524 reg1 = cfi_parse_reg ();
525 cfi_add_CFA_def_cfa_register (reg1);
526 break;
528 case DW_CFA_def_cfa_offset:
529 offset = cfi_parse_const ();
530 cfi_add_CFA_def_cfa_offset (offset);
531 break;
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
536 + offset);
537 break;
539 case DW_CFA_restore:
540 for (;;)
542 reg1 = cfi_parse_reg ();
543 cfi_add_CFA_restore (reg1);
544 SKIP_WHITESPACE ();
545 if (*input_line_pointer != ',')
546 break;
547 ++input_line_pointer;
549 break;
551 case DW_CFA_undefined:
552 for (;;)
554 reg1 = cfi_parse_reg ();
555 cfi_add_CFA_undefined (reg1);
556 SKIP_WHITESPACE ();
557 if (*input_line_pointer != ',')
558 break;
559 ++input_line_pointer;
561 break;
563 case DW_CFA_same_value:
564 reg1 = cfi_parse_reg ();
565 cfi_add_CFA_same_value (reg1);
566 break;
568 case CFI_return_column:
569 reg1 = cfi_parse_reg ();
570 cfi_set_return_column (reg1);
571 break;
573 case DW_CFA_remember_state:
574 cfi_add_CFA_remember_state ();
575 break;
577 case DW_CFA_restore_state:
578 cfi_add_CFA_restore_state ();
579 break;
581 case DW_CFA_GNU_window_save:
582 cfi_add_CFA_insn (DW_CFA_GNU_window_save);
583 break;
585 case CFI_signal_frame:
586 frchain_now->frch_cfi_data->cur_fde_data->signal_frame = 1;
587 break;
589 default:
590 abort ();
593 demand_empty_rest_of_line ();
596 static void
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 ();
606 return;
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)
612 != frag_now_fix ())
613 cfi_add_advance_loc (symbol_temp_new_now ());
615 tail = &head;
618 e = xmalloc (sizeof (*e));
619 do_parse_cons_expression (&e->exp, 1);
620 *tail = e;
621 tail = &e->next;
623 while (*input_line_pointer++ == ',');
624 *tail = NULL;
626 insn = alloc_cfi_insn_data ();
627 insn->insn = CFI_escape;
628 insn->u.esc = head;
630 --input_line_pointer;
631 demand_empty_rest_of_line ();
634 static void
635 dot_cfi_personality (int ignored ATTRIBUTE_UNUSED)
637 struct fde_entry *fde;
638 offsetT encoding;
640 if (frchain_now->frch_cfi_data == NULL)
642 as_bad (_("CFI instruction used without previous .cfi_startproc"));
643 ignore_rest_of_line ();
644 return;
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;
653 return;
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
660 #endif
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 ();
668 return;
671 if (*input_line_pointer++ != ',')
673 as_bad (_(".cfi_personality requires encoding and symbol arguments"));
674 ignore_rest_of_line ();
675 return;
678 expression_and_evaluate (&fde->personality);
679 switch (fde->personality.X_op)
681 case O_symbol:
682 break;
683 case O_constant:
684 if ((encoding & 0x70) == DW_EH_PE_pcrel)
685 encoding = DW_EH_PE_omit;
686 break;
687 default:
688 encoding = DW_EH_PE_omit;
689 break;
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 ();
698 return;
701 demand_empty_rest_of_line ();
704 static void
705 dot_cfi_lsda (int ignored ATTRIBUTE_UNUSED)
707 struct fde_entry *fde;
708 offsetT encoding;
710 if (frchain_now->frch_cfi_data == NULL)
712 as_bad (_("CFI instruction used without previous .cfi_startproc"));
713 ignore_rest_of_line ();
714 return;
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;
723 return;
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
730 #endif
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 ();
738 return;
741 if (*input_line_pointer++ != ',')
743 as_bad (_(".cfi_lsda requires encoding and symbol arguments"));
744 ignore_rest_of_line ();
745 return;
748 fde->lsda_encoding = encoding;
750 expression_and_evaluate (&fde->lsda);
751 switch (fde->lsda.X_op)
753 case O_symbol:
754 break;
755 case O_constant:
756 if ((encoding & 0x70) == DW_EH_PE_pcrel)
757 encoding = DW_EH_PE_omit;
758 break;
759 default:
760 encoding = DW_EH_PE_omit;
761 break;
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 ();
770 return;
773 demand_empty_rest_of_line ();
776 static void
777 dot_cfi_startproc (int ignored ATTRIBUTE_UNUSED)
779 int simple = 0;
781 if (frchain_now->frch_cfi_data != NULL)
783 as_bad (_("previous CFI entry not closed (missing .cfi_endproc)"));
784 ignore_rest_of_line ();
785 return;
788 cfi_new_fde (symbol_temp_new_now ());
790 SKIP_WHITESPACE ();
791 if (is_name_beginner (*input_line_pointer))
793 char *name, c;
795 name = input_line_pointer;
796 c = get_symbol_end ();
798 if (strcmp (name, "simple") == 0)
800 simple = 1;
801 *input_line_pointer = c;
803 else
804 input_line_pointer = name;
806 demand_empty_rest_of_line ();
808 frchain_now->frch_cfi_data->cur_cfa_offset = 0;
809 if (!simple)
810 tc_cfi_frame_initial_instructions ();
813 static void
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 ();
820 return;
823 cfi_end_fde (symbol_temp_new_now ());
825 demand_empty_rest_of_line ();
829 /* Emit a single byte into the current segment. */
831 static inline void
832 out_one (int byte)
834 FRAG_APPEND_1_CHAR (byte);
837 /* Emit a two-byte word into the current segment. */
839 static inline void
840 out_two (int data)
842 md_number_to_chars (frag_more (2), data, 2);
845 /* Emit a four byte word into the current segment. */
847 static inline void
848 out_four (int data)
850 md_number_to_chars (frag_more (4), data, 4);
853 /* Emit an unsigned "little-endian base 128" number. */
855 static void
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. */
863 static void
864 out_sleb128 (offsetT value)
866 output_leb128 (frag_more (sizeof_leb128 (value, 1)), value, 1);
869 static void
870 output_cfi_insn (struct cfi_insn_data *insn)
872 offsetT offset;
873 unsigned int regno;
875 switch (insn->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;
887 if (scaled <= 0x3F)
888 out_one (DW_CFA_advance_loc + scaled);
889 else if (delta <= 0xFF)
891 out_one (DW_CFA_advance_loc1);
892 out_one (delta);
894 else if (delta <= 0xFFFF)
896 out_one (DW_CFA_advance_loc2);
897 out_two (delta);
899 else
901 out_one (DW_CFA_advance_loc4);
902 out_four (delta);
905 else
907 expressionS exp;
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. */
918 frag_more (1);
920 frag_var (rs_cfa, 4, 0, DWARF2_LINE_MIN_INSN_LENGTH << 3,
921 make_expr_symbol (&exp), frag_now_fix () - 1,
922 (char *) frag_now);
925 break;
927 case DW_CFA_def_cfa:
928 offset = insn->u.ri.offset;
929 if (offset < 0)
931 out_one (DW_CFA_def_cfa_sf);
932 out_uleb128 (insn->u.ri.reg);
933 out_sleb128 (offset / DWARF2_CIE_DATA_ALIGNMENT);
935 else
937 out_one (DW_CFA_def_cfa);
938 out_uleb128 (insn->u.ri.reg);
939 out_uleb128 (offset);
941 break;
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);
948 break;
950 case DW_CFA_def_cfa_offset:
951 offset = insn->u.i;
952 if (offset < 0)
954 out_one (DW_CFA_def_cfa_offset_sf);
955 out_sleb128 (offset / DWARF2_CIE_DATA_ALIGNMENT);
957 else
959 out_one (DW_CFA_def_cfa_offset);
960 out_uleb128 (offset);
962 break;
964 case DW_CFA_restore:
965 regno = insn->u.r;
966 if (regno <= 0x3F)
968 out_one (DW_CFA_restore + regno);
970 else
972 out_one (DW_CFA_restore_extended);
973 out_uleb128 (regno);
975 break;
977 case DW_CFA_offset:
978 regno = insn->u.ri.reg;
979 offset = insn->u.ri.offset / DWARF2_CIE_DATA_ALIGNMENT;
980 if (offset < 0)
982 out_one (DW_CFA_offset_extended_sf);
983 out_uleb128 (regno);
984 out_sleb128 (offset);
986 else if (regno <= 0x3F)
988 out_one (DW_CFA_offset + regno);
989 out_uleb128 (offset);
991 else
993 out_one (DW_CFA_offset_extended);
994 out_uleb128 (regno);
995 out_uleb128 (offset);
997 break;
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);
1003 break;
1005 case DW_CFA_remember_state:
1006 case DW_CFA_restore_state:
1007 out_one (insn->insn);
1008 break;
1010 case DW_CFA_GNU_window_save:
1011 out_one (DW_CFA_GNU_window_save);
1012 break;
1014 case CFI_escape:
1016 struct cfi_escape_data *e;
1017 for (e = insn->u.esc; e ; e = e->next)
1018 emit_expr (&e->exp, 1);
1019 break;
1022 default:
1023 abort ();
1027 static offsetT
1028 encoding_size (unsigned char encoding)
1030 if (encoding == DW_EH_PE_omit)
1031 return 0;
1032 switch (encoding & 0x7)
1034 case 0:
1035 return bfd_get_arch_size (stdoutput) == 64 ? 8 : 4;
1036 case DW_EH_PE_udata2:
1037 return 2;
1038 case DW_EH_PE_udata4:
1039 return 4;
1040 case DW_EH_PE_udata8:
1041 return 8;
1042 default:
1043 abort ();
1047 static void
1048 output_cie (struct cie_entry *cie)
1050 symbolS *after_size_address, *end_address;
1051 expressionS exp;
1052 struct cfi_insn_data *i;
1053 offsetT augmentation_size;
1054 int enc;
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)
1071 out_one ('P');
1072 if (cie->lsda_encoding != DW_EH_PE_omit)
1073 out_one ('L');
1074 out_one ('R');
1075 if (cie->signal_frame)
1076 out_one ('S');
1077 out_one (0);
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);
1082 else
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)
1095 #ifdef DIFF_EXPR_OK
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);
1101 #else
1102 abort ();
1103 #endif
1105 else
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)
1113 case 2:
1114 enc = DW_EH_PE_sdata2;
1115 break;
1116 case 4:
1117 enc = DW_EH_PE_sdata4;
1118 break;
1119 case 8:
1120 enc = DW_EH_PE_sdata8;
1121 break;
1122 default:
1123 abort ();
1125 #if defined DIFF_EXPR_OK || defined tc_cfi_emit_pcrel_expr
1126 enc |= DW_EH_PE_pcrel;
1127 #endif
1128 out_one (enc);
1130 if (cie->first)
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);
1138 static void
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;
1143 expressionS exp;
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. */
1160 #ifdef DIFF_EXPR_OK
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. */
1164 #else
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. */
1170 #else
1171 emit_expr (&exp, DWARF2_FDE_RELOC_SIZE); /* Code offset. */
1172 #endif
1173 exp.X_op = O_subtract;
1174 #endif
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)
1185 exp = fde->lsda;
1186 if ((fde->lsda_encoding & 0x70) == DW_EH_PE_pcrel)
1188 #ifdef DIFF_EXPR_OK
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);
1194 #else
1195 abort ();
1196 #endif
1198 else
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)
1221 continue;
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)
1227 continue;
1228 switch (cie->personality.X_op)
1230 case O_constant:
1231 if (cie->personality.X_unsigned != fde->personality.X_unsigned)
1232 continue;
1233 break;
1234 case O_symbol:
1235 if (cie->personality.X_add_symbol
1236 != fde->personality.X_add_symbol)
1237 continue;
1238 break;
1239 default:
1240 abort ();
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)
1248 goto fail;
1249 switch (i->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. */
1255 goto fail;
1257 case DW_CFA_offset:
1258 case DW_CFA_def_cfa:
1259 if (i->u.ri.reg != j->u.ri.reg)
1260 goto fail;
1261 if (i->u.ri.offset != j->u.ri.offset)
1262 goto fail;
1263 break;
1265 case DW_CFA_register:
1266 if (i->u.rr.reg1 != j->u.rr.reg1)
1267 goto fail;
1268 if (i->u.rr.reg2 != j->u.rr.reg2)
1269 goto fail;
1270 break;
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)
1277 goto fail;
1278 break;
1280 case DW_CFA_def_cfa_offset:
1281 if (i->u.i != j->u.i)
1282 goto fail;
1283 break;
1285 case CFI_escape:
1286 /* Don't bother matching these for now. */
1287 goto fail;
1289 default:
1290 abort ();
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. */
1297 if (i == cie->last
1298 && (!j
1299 || j->insn == DW_CFA_advance_loc
1300 || j->insn == DW_CFA_remember_state
1301 || j->insn == CFI_escape))
1303 *pfirst = j;
1304 return cie;
1307 fail:;
1310 cie = xmalloc (sizeof (struct cie_entry));
1311 cie->next = cie_root;
1312 cie_root = cie;
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)
1324 break;
1326 cie->last = i;
1327 *pfirst = i;
1329 output_cie (cie);
1331 return cie;
1334 void
1335 cfi_finish (void)
1337 segT cfi_seg;
1338 struct fde_entry *fde;
1339 int save_flag_traditional_format;
1341 if (all_fde_data == 0)
1342 return;
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 */
1375 void
1376 cfi_finish (void)
1379 #endif /* TARGET_USE_CFIPOP */