2009-06-25 Kai Tietz <kai.tietz@onevision.com>
[binutils.git] / gas / dw2gencfi.c
blob0b4a64fa1497e216632f2d2f5d6f7d9a7409349f
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)
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"
27 #ifdef TARGET_USE_CFIPOP
29 /* By default, use difference expressions if DIFF_EXPR_OK is defined. */
30 #ifndef CFI_DIFF_EXPR_OK
31 # ifdef DIFF_EXPR_OK
32 # define CFI_DIFF_EXPR_OK 1
33 # else
34 # define CFI_DIFF_EXPR_OK 0
35 # endif
36 #endif
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
42 #endif
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
47 #endif
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
52 #endif
54 #ifndef EH_FRAME_ALIGNMENT
55 # define EH_FRAME_ALIGNMENT (bfd_get_arch_size (stdoutput) == 64 ? 3 : 2)
56 #endif
58 #ifndef tc_cfi_frame_initial_instructions
59 # define tc_cfi_frame_initial_instructions() ((void)0)
60 #endif
62 #ifndef DWARF2_ADDR_SIZE
63 # define DWARF2_ADDR_SIZE(bfd) (bfd_arch_bits_per_address (bfd) / 8)
64 #endif
67 struct cfi_insn_data
69 struct cfi_insn_data *next;
70 int insn;
71 union {
72 struct {
73 unsigned reg;
74 offsetT offset;
75 } ri;
77 struct {
78 unsigned reg1;
79 unsigned reg2;
80 } rr;
82 unsigned r;
83 offsetT i;
85 struct {
86 symbolS *lab1;
87 symbolS *lab2;
88 } ll;
90 struct cfi_escape_data {
91 struct cfi_escape_data *next;
92 expressionS exp;
93 } *esc;
95 struct {
96 unsigned reg, encoding;
97 expressionS exp;
98 } ea;
99 } u;
102 struct fde_entry
104 struct fde_entry *next;
105 symbolS *start_address;
106 symbolS *end_address;
107 struct cfi_insn_data *data;
108 struct cfi_insn_data **last;
109 unsigned char per_encoding;
110 unsigned char lsda_encoding;
111 expressionS personality;
112 expressionS lsda;
113 unsigned int return_column;
114 unsigned int signal_frame;
117 struct cie_entry
119 struct cie_entry *next;
120 symbolS *start_address;
121 unsigned int return_column;
122 unsigned int signal_frame;
123 unsigned char per_encoding;
124 unsigned char lsda_encoding;
125 expressionS personality;
126 struct cfi_insn_data *first, *last;
130 /* List of FDE entries. */
131 static struct fde_entry *all_fde_data;
132 static struct fde_entry **last_fde_data = &all_fde_data;
134 /* List of CIEs so that they could be reused. */
135 static struct cie_entry *cie_root;
137 /* Stack of old CFI data, for save/restore. */
138 struct cfa_save_data
140 struct cfa_save_data *next;
141 offsetT cfa_offset;
144 /* Current open FDE entry. */
145 struct frch_cfi_data
147 struct fde_entry *cur_fde_data;
148 symbolS *last_address;
149 offsetT cur_cfa_offset;
150 struct cfa_save_data *cfa_save_stack;
153 /* Construct a new FDE structure and add it to the end of the fde list. */
155 static struct fde_entry *
156 alloc_fde_entry (void)
158 struct fde_entry *fde = xcalloc (1, sizeof (struct fde_entry));
160 frchain_now->frch_cfi_data = xcalloc (1, sizeof (struct frch_cfi_data));
161 frchain_now->frch_cfi_data->cur_fde_data = fde;
162 *last_fde_data = fde;
163 last_fde_data = &fde->next;
165 fde->last = &fde->data;
166 fde->return_column = DWARF2_DEFAULT_RETURN_COLUMN;
167 fde->per_encoding = DW_EH_PE_omit;
168 fde->lsda_encoding = DW_EH_PE_omit;
170 return fde;
173 /* The following functions are available for a backend to construct its
174 own unwind information, usually from legacy unwind directives. */
176 /* Construct a new INSN structure and add it to the end of the insn list
177 for the currently active FDE. */
179 static struct cfi_insn_data *
180 alloc_cfi_insn_data (void)
182 struct cfi_insn_data *insn = xcalloc (1, sizeof (struct cfi_insn_data));
183 struct fde_entry *cur_fde_data = frchain_now->frch_cfi_data->cur_fde_data;
185 *cur_fde_data->last = insn;
186 cur_fde_data->last = &insn->next;
188 return insn;
191 /* Construct a new FDE structure that begins at LABEL. */
193 void
194 cfi_new_fde (symbolS *label)
196 struct fde_entry *fde = alloc_fde_entry ();
197 fde->start_address = label;
198 frchain_now->frch_cfi_data->last_address = label;
201 /* End the currently open FDE. */
203 void
204 cfi_end_fde (symbolS *label)
206 frchain_now->frch_cfi_data->cur_fde_data->end_address = label;
207 free (frchain_now->frch_cfi_data);
208 frchain_now->frch_cfi_data = NULL;
211 /* Set the return column for the current FDE. */
213 void
214 cfi_set_return_column (unsigned regno)
216 frchain_now->frch_cfi_data->cur_fde_data->return_column = regno;
219 /* Universal functions to store new instructions. */
221 static void
222 cfi_add_CFA_insn(int insn)
224 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
226 insn_ptr->insn = insn;
229 static void
230 cfi_add_CFA_insn_reg (int insn, unsigned regno)
232 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
234 insn_ptr->insn = insn;
235 insn_ptr->u.r = regno;
238 static void
239 cfi_add_CFA_insn_offset (int insn, offsetT offset)
241 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
243 insn_ptr->insn = insn;
244 insn_ptr->u.i = offset;
247 static void
248 cfi_add_CFA_insn_reg_reg (int insn, unsigned reg1, unsigned reg2)
250 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
252 insn_ptr->insn = insn;
253 insn_ptr->u.rr.reg1 = reg1;
254 insn_ptr->u.rr.reg2 = reg2;
257 static void
258 cfi_add_CFA_insn_reg_offset (int insn, unsigned regno, offsetT offset)
260 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
262 insn_ptr->insn = insn;
263 insn_ptr->u.ri.reg = regno;
264 insn_ptr->u.ri.offset = offset;
267 /* Add a CFI insn to advance the PC from the last address to LABEL. */
269 void
270 cfi_add_advance_loc (symbolS *label)
272 struct cfi_insn_data *insn = alloc_cfi_insn_data ();
274 insn->insn = DW_CFA_advance_loc;
275 insn->u.ll.lab1 = frchain_now->frch_cfi_data->last_address;
276 insn->u.ll.lab2 = label;
278 frchain_now->frch_cfi_data->last_address = label;
281 /* Add a DW_CFA_offset record to the CFI data. */
283 void
284 cfi_add_CFA_offset (unsigned regno, offsetT offset)
286 unsigned int abs_data_align;
288 gas_assert (DWARF2_CIE_DATA_ALIGNMENT != 0);
289 cfi_add_CFA_insn_reg_offset (DW_CFA_offset, regno, offset);
291 abs_data_align = (DWARF2_CIE_DATA_ALIGNMENT < 0
292 ? -DWARF2_CIE_DATA_ALIGNMENT : DWARF2_CIE_DATA_ALIGNMENT);
293 if (offset % abs_data_align)
294 as_bad (_("register save offset not a multiple of %u"), abs_data_align);
297 /* Add a DW_CFA_def_cfa record to the CFI data. */
299 void
300 cfi_add_CFA_def_cfa (unsigned regno, offsetT offset)
302 cfi_add_CFA_insn_reg_offset (DW_CFA_def_cfa, regno, offset);
303 frchain_now->frch_cfi_data->cur_cfa_offset = offset;
306 /* Add a DW_CFA_register record to the CFI data. */
308 void
309 cfi_add_CFA_register (unsigned reg1, unsigned reg2)
311 cfi_add_CFA_insn_reg_reg (DW_CFA_register, reg1, reg2);
314 /* Add a DW_CFA_def_cfa_register record to the CFI data. */
316 void
317 cfi_add_CFA_def_cfa_register (unsigned regno)
319 cfi_add_CFA_insn_reg (DW_CFA_def_cfa_register, regno);
322 /* Add a DW_CFA_def_cfa_offset record to the CFI data. */
324 void
325 cfi_add_CFA_def_cfa_offset (offsetT offset)
327 cfi_add_CFA_insn_offset (DW_CFA_def_cfa_offset, offset);
328 frchain_now->frch_cfi_data->cur_cfa_offset = offset;
331 void
332 cfi_add_CFA_restore (unsigned regno)
334 cfi_add_CFA_insn_reg (DW_CFA_restore, regno);
337 void
338 cfi_add_CFA_undefined (unsigned regno)
340 cfi_add_CFA_insn_reg (DW_CFA_undefined, regno);
343 void
344 cfi_add_CFA_same_value (unsigned regno)
346 cfi_add_CFA_insn_reg (DW_CFA_same_value, regno);
349 void
350 cfi_add_CFA_remember_state (void)
352 struct cfa_save_data *p;
354 cfi_add_CFA_insn (DW_CFA_remember_state);
356 p = xmalloc (sizeof (*p));
357 p->cfa_offset = frchain_now->frch_cfi_data->cur_cfa_offset;
358 p->next = frchain_now->frch_cfi_data->cfa_save_stack;
359 frchain_now->frch_cfi_data->cfa_save_stack = p;
362 void
363 cfi_add_CFA_restore_state (void)
365 struct cfa_save_data *p;
367 cfi_add_CFA_insn (DW_CFA_restore_state);
369 p = frchain_now->frch_cfi_data->cfa_save_stack;
370 if (p)
372 frchain_now->frch_cfi_data->cur_cfa_offset = p->cfa_offset;
373 frchain_now->frch_cfi_data->cfa_save_stack = p->next;
374 free (p);
376 else
377 as_bad (_("CFI state restore without previous remember"));
381 /* Parse CFI assembler directives. */
383 static void dot_cfi (int);
384 static void dot_cfi_escape (int);
385 static void dot_cfi_startproc (int);
386 static void dot_cfi_endproc (int);
387 static void dot_cfi_personality (int);
388 static void dot_cfi_lsda (int);
389 static void dot_cfi_val_encoded_addr (int);
391 /* Fake CFI type; outside the byte range of any real CFI insn. */
392 #define CFI_adjust_cfa_offset 0x100
393 #define CFI_return_column 0x101
394 #define CFI_rel_offset 0x102
395 #define CFI_escape 0x103
396 #define CFI_signal_frame 0x104
397 #define CFI_val_encoded_addr 0x105
399 const pseudo_typeS cfi_pseudo_table[] =
401 { "cfi_startproc", dot_cfi_startproc, 0 },
402 { "cfi_endproc", dot_cfi_endproc, 0 },
403 { "cfi_def_cfa", dot_cfi, DW_CFA_def_cfa },
404 { "cfi_def_cfa_register", dot_cfi, DW_CFA_def_cfa_register },
405 { "cfi_def_cfa_offset", dot_cfi, DW_CFA_def_cfa_offset },
406 { "cfi_adjust_cfa_offset", dot_cfi, CFI_adjust_cfa_offset },
407 { "cfi_offset", dot_cfi, DW_CFA_offset },
408 { "cfi_rel_offset", dot_cfi, CFI_rel_offset },
409 { "cfi_register", dot_cfi, DW_CFA_register },
410 { "cfi_return_column", dot_cfi, CFI_return_column },
411 { "cfi_restore", dot_cfi, DW_CFA_restore },
412 { "cfi_undefined", dot_cfi, DW_CFA_undefined },
413 { "cfi_same_value", dot_cfi, DW_CFA_same_value },
414 { "cfi_remember_state", dot_cfi, DW_CFA_remember_state },
415 { "cfi_restore_state", dot_cfi, DW_CFA_restore_state },
416 { "cfi_window_save", dot_cfi, DW_CFA_GNU_window_save },
417 { "cfi_escape", dot_cfi_escape, 0 },
418 { "cfi_signal_frame", dot_cfi, CFI_signal_frame },
419 { "cfi_personality", dot_cfi_personality, 0 },
420 { "cfi_lsda", dot_cfi_lsda, 0 },
421 { "cfi_val_encoded_addr", dot_cfi_val_encoded_addr, 0 },
422 { NULL, NULL, 0 }
425 static void
426 cfi_parse_separator (void)
428 SKIP_WHITESPACE ();
429 if (*input_line_pointer == ',')
430 input_line_pointer++;
431 else
432 as_bad (_("missing separator"));
435 #ifndef tc_parse_to_dw2regnum
436 static void
437 tc_parse_to_dw2regnum(expressionS *exp)
439 # ifdef tc_regname_to_dw2regnum
440 SKIP_WHITESPACE ();
441 if (is_name_beginner (*input_line_pointer)
442 || (*input_line_pointer == '%'
443 && is_name_beginner (*++input_line_pointer)))
445 char *name, c;
447 name = input_line_pointer;
448 c = get_symbol_end ();
450 exp->X_op = O_constant;
451 exp->X_add_number = tc_regname_to_dw2regnum (name);
453 *input_line_pointer = c;
455 else
456 # endif
457 expression_and_evaluate (exp);
459 #endif
461 static unsigned
462 cfi_parse_reg (void)
464 int regno;
465 expressionS exp;
467 tc_parse_to_dw2regnum (&exp);
468 switch (exp.X_op)
470 case O_register:
471 case O_constant:
472 regno = exp.X_add_number;
473 break;
475 default:
476 regno = -1;
477 break;
480 if (regno < 0)
482 as_bad (_("bad register expression"));
483 regno = 0;
486 return regno;
489 static offsetT
490 cfi_parse_const (void)
492 return get_absolute_expression ();
495 static void
496 dot_cfi (int arg)
498 offsetT offset;
499 unsigned reg1, reg2;
501 if (frchain_now->frch_cfi_data == NULL)
503 as_bad (_("CFI instruction used without previous .cfi_startproc"));
504 ignore_rest_of_line ();
505 return;
508 /* If the last address was not at the current PC, advance to current. */
509 if (symbol_get_frag (frchain_now->frch_cfi_data->last_address) != frag_now
510 || S_GET_VALUE (frchain_now->frch_cfi_data->last_address)
511 != frag_now_fix ())
512 cfi_add_advance_loc (symbol_temp_new_now ());
514 switch (arg)
516 case DW_CFA_offset:
517 reg1 = cfi_parse_reg ();
518 cfi_parse_separator ();
519 offset = cfi_parse_const ();
520 cfi_add_CFA_offset (reg1, offset);
521 break;
523 case CFI_rel_offset:
524 reg1 = cfi_parse_reg ();
525 cfi_parse_separator ();
526 offset = cfi_parse_const ();
527 cfi_add_CFA_offset (reg1,
528 offset - frchain_now->frch_cfi_data->cur_cfa_offset);
529 break;
531 case DW_CFA_def_cfa:
532 reg1 = cfi_parse_reg ();
533 cfi_parse_separator ();
534 offset = cfi_parse_const ();
535 cfi_add_CFA_def_cfa (reg1, offset);
536 break;
538 case DW_CFA_register:
539 reg1 = cfi_parse_reg ();
540 cfi_parse_separator ();
541 reg2 = cfi_parse_reg ();
542 cfi_add_CFA_register (reg1, reg2);
543 break;
545 case DW_CFA_def_cfa_register:
546 reg1 = cfi_parse_reg ();
547 cfi_add_CFA_def_cfa_register (reg1);
548 break;
550 case DW_CFA_def_cfa_offset:
551 offset = cfi_parse_const ();
552 cfi_add_CFA_def_cfa_offset (offset);
553 break;
555 case CFI_adjust_cfa_offset:
556 offset = cfi_parse_const ();
557 cfi_add_CFA_def_cfa_offset (frchain_now->frch_cfi_data->cur_cfa_offset
558 + offset);
559 break;
561 case DW_CFA_restore:
562 for (;;)
564 reg1 = cfi_parse_reg ();
565 cfi_add_CFA_restore (reg1);
566 SKIP_WHITESPACE ();
567 if (*input_line_pointer != ',')
568 break;
569 ++input_line_pointer;
571 break;
573 case DW_CFA_undefined:
574 for (;;)
576 reg1 = cfi_parse_reg ();
577 cfi_add_CFA_undefined (reg1);
578 SKIP_WHITESPACE ();
579 if (*input_line_pointer != ',')
580 break;
581 ++input_line_pointer;
583 break;
585 case DW_CFA_same_value:
586 reg1 = cfi_parse_reg ();
587 cfi_add_CFA_same_value (reg1);
588 break;
590 case CFI_return_column:
591 reg1 = cfi_parse_reg ();
592 cfi_set_return_column (reg1);
593 break;
595 case DW_CFA_remember_state:
596 cfi_add_CFA_remember_state ();
597 break;
599 case DW_CFA_restore_state:
600 cfi_add_CFA_restore_state ();
601 break;
603 case DW_CFA_GNU_window_save:
604 cfi_add_CFA_insn (DW_CFA_GNU_window_save);
605 break;
607 case CFI_signal_frame:
608 frchain_now->frch_cfi_data->cur_fde_data->signal_frame = 1;
609 break;
611 default:
612 abort ();
615 demand_empty_rest_of_line ();
618 static void
619 dot_cfi_escape (int ignored ATTRIBUTE_UNUSED)
621 struct cfi_escape_data *head, **tail, *e;
622 struct cfi_insn_data *insn;
624 if (frchain_now->frch_cfi_data == NULL)
626 as_bad (_("CFI instruction used without previous .cfi_startproc"));
627 ignore_rest_of_line ();
628 return;
631 /* If the last address was not at the current PC, advance to current. */
632 if (symbol_get_frag (frchain_now->frch_cfi_data->last_address) != frag_now
633 || S_GET_VALUE (frchain_now->frch_cfi_data->last_address)
634 != frag_now_fix ())
635 cfi_add_advance_loc (symbol_temp_new_now ());
637 tail = &head;
640 e = xmalloc (sizeof (*e));
641 do_parse_cons_expression (&e->exp, 1);
642 *tail = e;
643 tail = &e->next;
645 while (*input_line_pointer++ == ',');
646 *tail = NULL;
648 insn = alloc_cfi_insn_data ();
649 insn->insn = CFI_escape;
650 insn->u.esc = head;
652 --input_line_pointer;
653 demand_empty_rest_of_line ();
656 static void
657 dot_cfi_personality (int ignored ATTRIBUTE_UNUSED)
659 struct fde_entry *fde;
660 offsetT encoding;
662 if (frchain_now->frch_cfi_data == NULL)
664 as_bad (_("CFI instruction used without previous .cfi_startproc"));
665 ignore_rest_of_line ();
666 return;
669 fde = frchain_now->frch_cfi_data->cur_fde_data;
670 encoding = cfi_parse_const ();
671 if (encoding == DW_EH_PE_omit)
673 demand_empty_rest_of_line ();
674 fde->per_encoding = encoding;
675 return;
678 if ((encoding & 0xff) != encoding
679 || ((encoding & 0x70) != 0
680 #if CFI_DIFF_EXPR_OK || defined tc_cfi_emit_pcrel_expr
681 && (encoding & 0x70) != DW_EH_PE_pcrel
682 #endif
684 /* leb128 can be handled, but does something actually need it? */
685 || (encoding & 7) == DW_EH_PE_uleb128
686 || (encoding & 7) > DW_EH_PE_udata8)
688 as_bad (_("invalid or unsupported encoding in .cfi_personality"));
689 ignore_rest_of_line ();
690 return;
693 if (*input_line_pointer++ != ',')
695 as_bad (_(".cfi_personality requires encoding and symbol arguments"));
696 ignore_rest_of_line ();
697 return;
700 expression_and_evaluate (&fde->personality);
701 switch (fde->personality.X_op)
703 case O_symbol:
704 break;
705 case O_constant:
706 if ((encoding & 0x70) == DW_EH_PE_pcrel)
707 encoding = DW_EH_PE_omit;
708 break;
709 default:
710 encoding = DW_EH_PE_omit;
711 break;
714 fde->per_encoding = encoding;
716 if (encoding == DW_EH_PE_omit)
718 as_bad (_("wrong second argument to .cfi_personality"));
719 ignore_rest_of_line ();
720 return;
723 demand_empty_rest_of_line ();
726 static void
727 dot_cfi_lsda (int ignored ATTRIBUTE_UNUSED)
729 struct fde_entry *fde;
730 offsetT encoding;
732 if (frchain_now->frch_cfi_data == NULL)
734 as_bad (_("CFI instruction used without previous .cfi_startproc"));
735 ignore_rest_of_line ();
736 return;
739 fde = frchain_now->frch_cfi_data->cur_fde_data;
740 encoding = cfi_parse_const ();
741 if (encoding == DW_EH_PE_omit)
743 demand_empty_rest_of_line ();
744 fde->lsda_encoding = encoding;
745 return;
748 if ((encoding & 0xff) != encoding
749 || ((encoding & 0x70) != 0
750 #if CFI_DIFF_EXPR_OK || defined tc_cfi_emit_pcrel_expr
751 && (encoding & 0x70) != DW_EH_PE_pcrel
752 #endif
754 /* leb128 can be handled, but does something actually need it? */
755 || (encoding & 7) == DW_EH_PE_uleb128
756 || (encoding & 7) > DW_EH_PE_udata8)
758 as_bad (_("invalid or unsupported encoding in .cfi_lsda"));
759 ignore_rest_of_line ();
760 return;
763 if (*input_line_pointer++ != ',')
765 as_bad (_(".cfi_lsda requires encoding and symbol arguments"));
766 ignore_rest_of_line ();
767 return;
770 fde->lsda_encoding = encoding;
772 expression_and_evaluate (&fde->lsda);
773 switch (fde->lsda.X_op)
775 case O_symbol:
776 break;
777 case O_constant:
778 if ((encoding & 0x70) == DW_EH_PE_pcrel)
779 encoding = DW_EH_PE_omit;
780 break;
781 default:
782 encoding = DW_EH_PE_omit;
783 break;
786 fde->lsda_encoding = encoding;
788 if (encoding == DW_EH_PE_omit)
790 as_bad (_("wrong second argument to .cfi_lsda"));
791 ignore_rest_of_line ();
792 return;
795 demand_empty_rest_of_line ();
798 static void
799 dot_cfi_val_encoded_addr (int ignored ATTRIBUTE_UNUSED)
801 struct cfi_insn_data *insn_ptr;
802 offsetT encoding;
804 if (frchain_now->frch_cfi_data == NULL)
806 as_bad (_("CFI instruction used without previous .cfi_startproc"));
807 ignore_rest_of_line ();
808 return;
811 /* If the last address was not at the current PC, advance to current. */
812 if (symbol_get_frag (frchain_now->frch_cfi_data->last_address) != frag_now
813 || S_GET_VALUE (frchain_now->frch_cfi_data->last_address)
814 != frag_now_fix ())
815 cfi_add_advance_loc (symbol_temp_new_now ());
817 insn_ptr = alloc_cfi_insn_data ();
818 insn_ptr->insn = CFI_val_encoded_addr;
820 insn_ptr->u.ea.reg = cfi_parse_reg ();
822 cfi_parse_separator ();
823 encoding = cfi_parse_const ();
824 if ((encoding & 0xff) != encoding
825 || ((encoding & 0x70) != 0
826 #if CFI_DIFF_EXPR_OK || defined tc_cfi_emit_pcrel_expr
827 && (encoding & 0x70) != DW_EH_PE_pcrel
828 #endif
830 /* leb128 can be handled, but does something actually need it? */
831 || (encoding & 7) == DW_EH_PE_uleb128
832 || (encoding & 7) > DW_EH_PE_udata8)
834 as_bad (_("invalid or unsupported encoding in .cfi_lsda"));
835 encoding = DW_EH_PE_omit;
838 cfi_parse_separator ();
839 expression_and_evaluate (&insn_ptr->u.ea.exp);
840 switch (insn_ptr->u.ea.exp.X_op)
842 case O_symbol:
843 break;
844 case O_constant:
845 if ((encoding & 0x70) != DW_EH_PE_pcrel)
846 break;
847 default:
848 encoding = DW_EH_PE_omit;
849 break;
852 insn_ptr->u.ea.encoding = encoding;
853 if (encoding == DW_EH_PE_omit)
855 as_bad (_("wrong third argument to .cfi_val_encoded_addr"));
856 ignore_rest_of_line ();
857 return;
860 demand_empty_rest_of_line ();
863 static void
864 dot_cfi_startproc (int ignored ATTRIBUTE_UNUSED)
866 int simple = 0;
868 if (frchain_now->frch_cfi_data != NULL)
870 as_bad (_("previous CFI entry not closed (missing .cfi_endproc)"));
871 ignore_rest_of_line ();
872 return;
875 cfi_new_fde (symbol_temp_new_now ());
877 SKIP_WHITESPACE ();
878 if (is_name_beginner (*input_line_pointer))
880 char *name, c;
882 name = input_line_pointer;
883 c = get_symbol_end ();
885 if (strcmp (name, "simple") == 0)
887 simple = 1;
888 *input_line_pointer = c;
890 else
891 input_line_pointer = name;
893 demand_empty_rest_of_line ();
895 frchain_now->frch_cfi_data->cur_cfa_offset = 0;
896 if (!simple)
897 tc_cfi_frame_initial_instructions ();
900 static void
901 dot_cfi_endproc (int ignored ATTRIBUTE_UNUSED)
903 if (frchain_now->frch_cfi_data == NULL)
905 as_bad (_(".cfi_endproc without corresponding .cfi_startproc"));
906 ignore_rest_of_line ();
907 return;
910 cfi_end_fde (symbol_temp_new_now ());
912 demand_empty_rest_of_line ();
916 /* Emit a single byte into the current segment. */
918 static inline void
919 out_one (int byte)
921 FRAG_APPEND_1_CHAR (byte);
924 /* Emit a two-byte word into the current segment. */
926 static inline void
927 out_two (int data)
929 md_number_to_chars (frag_more (2), data, 2);
932 /* Emit a four byte word into the current segment. */
934 static inline void
935 out_four (int data)
937 md_number_to_chars (frag_more (4), data, 4);
940 /* Emit an unsigned "little-endian base 128" number. */
942 static void
943 out_uleb128 (addressT value)
945 output_leb128 (frag_more (sizeof_leb128 (value, 0)), value, 0);
948 /* Emit an unsigned "little-endian base 128" number. */
950 static void
951 out_sleb128 (offsetT value)
953 output_leb128 (frag_more (sizeof_leb128 (value, 1)), value, 1);
956 static void
957 output_cfi_insn (struct cfi_insn_data *insn)
959 offsetT offset;
960 unsigned int regno;
962 switch (insn->insn)
964 case DW_CFA_advance_loc:
966 symbolS *from = insn->u.ll.lab1;
967 symbolS *to = insn->u.ll.lab2;
969 if (symbol_get_frag (to) == symbol_get_frag (from))
971 addressT delta = S_GET_VALUE (to) - S_GET_VALUE (from);
972 addressT scaled = delta / DWARF2_LINE_MIN_INSN_LENGTH;
974 if (scaled <= 0x3F)
975 out_one (DW_CFA_advance_loc + scaled);
976 else if (scaled <= 0xFF)
978 out_one (DW_CFA_advance_loc1);
979 out_one (scaled);
981 else if (scaled <= 0xFFFF)
983 out_one (DW_CFA_advance_loc2);
984 out_two (scaled);
986 else
988 out_one (DW_CFA_advance_loc4);
989 out_four (scaled);
992 else
994 expressionS exp;
996 exp.X_op = O_subtract;
997 exp.X_add_symbol = to;
998 exp.X_op_symbol = from;
999 exp.X_add_number = 0;
1001 /* The code in ehopt.c expects that one byte of the encoding
1002 is already allocated to the frag. This comes from the way
1003 that it scans the .eh_frame section looking first for the
1004 .byte DW_CFA_advance_loc4. */
1005 *frag_more (1) = DW_CFA_advance_loc4;
1007 frag_var (rs_cfa, 4, 0, DWARF2_LINE_MIN_INSN_LENGTH << 3,
1008 make_expr_symbol (&exp), frag_now_fix () - 1,
1009 (char *) frag_now);
1012 break;
1014 case DW_CFA_def_cfa:
1015 offset = insn->u.ri.offset;
1016 if (offset < 0)
1018 out_one (DW_CFA_def_cfa_sf);
1019 out_uleb128 (insn->u.ri.reg);
1020 out_sleb128 (offset / DWARF2_CIE_DATA_ALIGNMENT);
1022 else
1024 out_one (DW_CFA_def_cfa);
1025 out_uleb128 (insn->u.ri.reg);
1026 out_uleb128 (offset);
1028 break;
1030 case DW_CFA_def_cfa_register:
1031 case DW_CFA_undefined:
1032 case DW_CFA_same_value:
1033 out_one (insn->insn);
1034 out_uleb128 (insn->u.r);
1035 break;
1037 case DW_CFA_def_cfa_offset:
1038 offset = insn->u.i;
1039 if (offset < 0)
1041 out_one (DW_CFA_def_cfa_offset_sf);
1042 out_sleb128 (offset / DWARF2_CIE_DATA_ALIGNMENT);
1044 else
1046 out_one (DW_CFA_def_cfa_offset);
1047 out_uleb128 (offset);
1049 break;
1051 case DW_CFA_restore:
1052 regno = insn->u.r;
1053 if (regno <= 0x3F)
1055 out_one (DW_CFA_restore + regno);
1057 else
1059 out_one (DW_CFA_restore_extended);
1060 out_uleb128 (regno);
1062 break;
1064 case DW_CFA_offset:
1065 regno = insn->u.ri.reg;
1066 offset = insn->u.ri.offset / DWARF2_CIE_DATA_ALIGNMENT;
1067 if (offset < 0)
1069 out_one (DW_CFA_offset_extended_sf);
1070 out_uleb128 (regno);
1071 out_sleb128 (offset);
1073 else if (regno <= 0x3F)
1075 out_one (DW_CFA_offset + regno);
1076 out_uleb128 (offset);
1078 else
1080 out_one (DW_CFA_offset_extended);
1081 out_uleb128 (regno);
1082 out_uleb128 (offset);
1084 break;
1086 case DW_CFA_register:
1087 out_one (DW_CFA_register);
1088 out_uleb128 (insn->u.rr.reg1);
1089 out_uleb128 (insn->u.rr.reg2);
1090 break;
1092 case DW_CFA_remember_state:
1093 case DW_CFA_restore_state:
1094 out_one (insn->insn);
1095 break;
1097 case DW_CFA_GNU_window_save:
1098 out_one (DW_CFA_GNU_window_save);
1099 break;
1101 case CFI_escape:
1103 struct cfi_escape_data *e;
1104 for (e = insn->u.esc; e ; e = e->next)
1105 emit_expr (&e->exp, 1);
1106 break;
1109 case CFI_val_encoded_addr:
1111 unsigned encoding = insn->u.ea.encoding;
1112 offsetT encoding_size;
1114 if (encoding == DW_EH_PE_omit)
1115 break;
1116 out_one (DW_CFA_val_expression);
1117 out_uleb128 (insn->u.ea.reg);
1119 switch (encoding & 0x7)
1121 case DW_EH_PE_absptr:
1122 encoding_size = DWARF2_ADDR_SIZE (stdoutput);
1123 break;
1124 case DW_EH_PE_udata2:
1125 encoding_size = 2;
1126 break;
1127 case DW_EH_PE_udata4:
1128 encoding_size = 4;
1129 break;
1130 case DW_EH_PE_udata8:
1131 encoding_size = 8;
1132 break;
1133 default:
1134 abort ();
1137 /* If the user has requested absolute encoding,
1138 then use the smaller DW_OP_addr encoding. */
1139 if (insn->u.ea.encoding == DW_EH_PE_absptr)
1141 out_uleb128 (1 + encoding_size);
1142 out_one (DW_OP_addr);
1144 else
1146 out_uleb128 (1 + 1 + encoding_size);
1147 out_one (DW_OP_GNU_encoded_addr);
1148 out_one (encoding);
1150 if ((encoding & 0x70) == DW_EH_PE_pcrel)
1152 #if CFI_DIFF_EXPR_OK
1153 insn->u.ea.exp.X_op = O_subtract;
1154 insn->u.ea.exp.X_op_symbol = symbol_temp_new_now ();
1155 #elif defined (tc_cfi_emit_pcrel_expr)
1156 tc_cfi_emit_pcrel_expr (&insn->u.ea.exp, encoding_size);
1157 break;
1158 #else
1159 abort ();
1160 #endif
1163 emit_expr (&insn->u.ea.exp, encoding_size);
1165 break;
1167 default:
1168 abort ();
1172 static offsetT
1173 encoding_size (unsigned char encoding)
1175 if (encoding == DW_EH_PE_omit)
1176 return 0;
1177 switch (encoding & 0x7)
1179 case 0:
1180 return bfd_get_arch_size (stdoutput) == 64 ? 8 : 4;
1181 case DW_EH_PE_udata2:
1182 return 2;
1183 case DW_EH_PE_udata4:
1184 return 4;
1185 case DW_EH_PE_udata8:
1186 return 8;
1187 default:
1188 abort ();
1192 static void
1193 output_cie (struct cie_entry *cie)
1195 symbolS *after_size_address, *end_address;
1196 expressionS exp;
1197 struct cfi_insn_data *i;
1198 offsetT augmentation_size;
1199 int enc;
1201 cie->start_address = symbol_temp_new_now ();
1202 after_size_address = symbol_temp_make ();
1203 end_address = symbol_temp_make ();
1205 exp.X_op = O_subtract;
1206 exp.X_add_symbol = end_address;
1207 exp.X_op_symbol = after_size_address;
1208 exp.X_add_number = 0;
1210 emit_expr (&exp, 4); /* Length. */
1211 symbol_set_value_now (after_size_address);
1212 out_four (0); /* CIE id. */
1213 out_one (DW_CIE_VERSION); /* Version. */
1214 out_one ('z'); /* Augmentation. */
1215 if (cie->per_encoding != DW_EH_PE_omit)
1216 out_one ('P');
1217 if (cie->lsda_encoding != DW_EH_PE_omit)
1218 out_one ('L');
1219 out_one ('R');
1220 if (cie->signal_frame)
1221 out_one ('S');
1222 out_one (0);
1223 out_uleb128 (DWARF2_LINE_MIN_INSN_LENGTH); /* Code alignment. */
1224 out_sleb128 (DWARF2_CIE_DATA_ALIGNMENT); /* Data alignment. */
1225 if (DW_CIE_VERSION == 1) /* Return column. */
1226 out_one (cie->return_column);
1227 else
1228 out_uleb128 (cie->return_column);
1229 augmentation_size = 1 + (cie->lsda_encoding != DW_EH_PE_omit);
1230 if (cie->per_encoding != DW_EH_PE_omit)
1231 augmentation_size += 1 + encoding_size (cie->per_encoding);
1232 out_uleb128 (augmentation_size); /* Augmentation size. */
1233 if (cie->per_encoding != DW_EH_PE_omit)
1235 offsetT size = encoding_size (cie->per_encoding);
1236 out_one (cie->per_encoding);
1237 exp = cie->personality;
1238 if ((cie->per_encoding & 0x70) == DW_EH_PE_pcrel)
1240 #if CFI_DIFF_EXPR_OK
1241 exp.X_op = O_subtract;
1242 exp.X_op_symbol = symbol_temp_new_now ();
1243 emit_expr (&exp, size);
1244 #elif defined (tc_cfi_emit_pcrel_expr)
1245 tc_cfi_emit_pcrel_expr (&exp, size);
1246 #else
1247 abort ();
1248 #endif
1250 else
1251 emit_expr (&exp, size);
1253 if (cie->lsda_encoding != DW_EH_PE_omit)
1254 out_one (cie->lsda_encoding);
1256 switch (DWARF2_FDE_RELOC_SIZE)
1258 case 2:
1259 enc = DW_EH_PE_sdata2;
1260 break;
1261 case 4:
1262 enc = DW_EH_PE_sdata4;
1263 break;
1264 case 8:
1265 enc = DW_EH_PE_sdata8;
1266 break;
1267 default:
1268 abort ();
1270 #if CFI_DIFF_EXPR_OK || defined tc_cfi_emit_pcrel_expr
1271 enc |= DW_EH_PE_pcrel;
1272 #endif
1273 out_one (enc);
1275 if (cie->first)
1276 for (i = cie->first; i != cie->last; i = i->next)
1277 output_cfi_insn (i);
1279 frag_align (2, DW_CFA_nop, 0);
1280 symbol_set_value_now (end_address);
1283 static void
1284 output_fde (struct fde_entry *fde, struct cie_entry *cie,
1285 struct cfi_insn_data *first, int align)
1287 symbolS *after_size_address, *end_address;
1288 expressionS exp;
1289 offsetT augmentation_size;
1291 after_size_address = symbol_temp_make ();
1292 end_address = symbol_temp_make ();
1294 exp.X_op = O_subtract;
1295 exp.X_add_symbol = end_address;
1296 exp.X_op_symbol = after_size_address;
1297 exp.X_add_number = 0;
1298 emit_expr (&exp, 4); /* Length. */
1299 symbol_set_value_now (after_size_address);
1301 exp.X_add_symbol = after_size_address;
1302 exp.X_op_symbol = cie->start_address;
1303 emit_expr (&exp, 4); /* CIE offset. */
1305 #if CFI_DIFF_EXPR_OK
1306 exp.X_add_symbol = fde->start_address;
1307 exp.X_op_symbol = symbol_temp_new_now ();
1308 emit_expr (&exp, DWARF2_FDE_RELOC_SIZE); /* Code offset. */
1309 #else
1310 exp.X_op = O_symbol;
1311 exp.X_add_symbol = fde->start_address;
1312 exp.X_op_symbol = NULL;
1313 #ifdef tc_cfi_emit_pcrel_expr
1314 tc_cfi_emit_pcrel_expr (&exp, DWARF2_FDE_RELOC_SIZE); /* Code offset. */
1315 #else
1316 emit_expr (&exp, DWARF2_FDE_RELOC_SIZE); /* Code offset. */
1317 #endif
1318 exp.X_op = O_subtract;
1319 #endif
1321 exp.X_add_symbol = fde->end_address;
1322 exp.X_op_symbol = fde->start_address; /* Code length. */
1323 emit_expr (&exp, DWARF2_FDE_RELOC_SIZE);
1325 augmentation_size = encoding_size (fde->lsda_encoding);
1326 out_uleb128 (augmentation_size); /* Augmentation size. */
1328 if (fde->lsda_encoding != DW_EH_PE_omit)
1330 exp = fde->lsda;
1331 if ((fde->lsda_encoding & 0x70) == DW_EH_PE_pcrel)
1333 #if CFI_DIFF_EXPR_OK
1334 exp.X_op = O_subtract;
1335 exp.X_op_symbol = symbol_temp_new_now ();
1336 emit_expr (&exp, augmentation_size);
1337 #elif defined (tc_cfi_emit_pcrel_expr)
1338 tc_cfi_emit_pcrel_expr (&exp, augmentation_size);
1339 #else
1340 abort ();
1341 #endif
1343 else
1344 emit_expr (&exp, augmentation_size);
1347 for (; first; first = first->next)
1348 output_cfi_insn (first);
1350 frag_align (align, DW_CFA_nop, 0);
1351 symbol_set_value_now (end_address);
1354 static struct cie_entry *
1355 select_cie_for_fde (struct fde_entry *fde, struct cfi_insn_data **pfirst)
1357 struct cfi_insn_data *i, *j;
1358 struct cie_entry *cie;
1360 for (cie = cie_root; cie; cie = cie->next)
1362 if (cie->return_column != fde->return_column
1363 || cie->signal_frame != fde->signal_frame
1364 || cie->per_encoding != fde->per_encoding
1365 || cie->lsda_encoding != fde->lsda_encoding)
1366 continue;
1367 if (cie->per_encoding != DW_EH_PE_omit)
1369 if (cie->personality.X_op != fde->personality.X_op
1370 || cie->personality.X_add_number
1371 != fde->personality.X_add_number)
1372 continue;
1373 switch (cie->personality.X_op)
1375 case O_constant:
1376 if (cie->personality.X_unsigned != fde->personality.X_unsigned)
1377 continue;
1378 break;
1379 case O_symbol:
1380 if (cie->personality.X_add_symbol
1381 != fde->personality.X_add_symbol)
1382 continue;
1383 break;
1384 default:
1385 abort ();
1388 for (i = cie->first, j = fde->data;
1389 i != cie->last && j != NULL;
1390 i = i->next, j = j->next)
1392 if (i->insn != j->insn)
1393 goto fail;
1394 switch (i->insn)
1396 case DW_CFA_advance_loc:
1397 case DW_CFA_remember_state:
1398 /* We reached the first advance/remember in the FDE,
1399 but did not reach the end of the CIE list. */
1400 goto fail;
1402 case DW_CFA_offset:
1403 case DW_CFA_def_cfa:
1404 if (i->u.ri.reg != j->u.ri.reg)
1405 goto fail;
1406 if (i->u.ri.offset != j->u.ri.offset)
1407 goto fail;
1408 break;
1410 case DW_CFA_register:
1411 if (i->u.rr.reg1 != j->u.rr.reg1)
1412 goto fail;
1413 if (i->u.rr.reg2 != j->u.rr.reg2)
1414 goto fail;
1415 break;
1417 case DW_CFA_def_cfa_register:
1418 case DW_CFA_restore:
1419 case DW_CFA_undefined:
1420 case DW_CFA_same_value:
1421 if (i->u.r != j->u.r)
1422 goto fail;
1423 break;
1425 case DW_CFA_def_cfa_offset:
1426 if (i->u.i != j->u.i)
1427 goto fail;
1428 break;
1430 case CFI_escape:
1431 case CFI_val_encoded_addr:
1432 /* Don't bother matching these for now. */
1433 goto fail;
1435 default:
1436 abort ();
1440 /* Success if we reached the end of the CIE list, and we've either
1441 run out of FDE entries or we've encountered an advance,
1442 remember, or escape. */
1443 if (i == cie->last
1444 && (!j
1445 || j->insn == DW_CFA_advance_loc
1446 || j->insn == DW_CFA_remember_state
1447 || j->insn == CFI_escape
1448 || j->insn == CFI_val_encoded_addr))
1450 *pfirst = j;
1451 return cie;
1454 fail:;
1457 cie = xmalloc (sizeof (struct cie_entry));
1458 cie->next = cie_root;
1459 cie_root = cie;
1460 cie->return_column = fde->return_column;
1461 cie->signal_frame = fde->signal_frame;
1462 cie->per_encoding = fde->per_encoding;
1463 cie->lsda_encoding = fde->lsda_encoding;
1464 cie->personality = fde->personality;
1465 cie->first = fde->data;
1467 for (i = cie->first; i ; i = i->next)
1468 if (i->insn == DW_CFA_advance_loc
1469 || i->insn == DW_CFA_remember_state
1470 || i->insn == CFI_escape
1471 || i->insn == CFI_val_encoded_addr)
1472 break;
1474 cie->last = i;
1475 *pfirst = i;
1477 output_cie (cie);
1479 return cie;
1482 void
1483 cfi_finish (void)
1485 segT cfi_seg;
1486 struct fde_entry *fde;
1487 int save_flag_traditional_format;
1489 if (all_fde_data == 0)
1490 return;
1492 /* Open .eh_frame section. */
1493 cfi_seg = subseg_new (".eh_frame", 0);
1494 bfd_set_section_flags (stdoutput, cfi_seg,
1495 SEC_ALLOC | SEC_LOAD | SEC_DATA
1496 | DWARF2_EH_FRAME_READ_ONLY);
1497 subseg_set (cfi_seg, 0);
1498 record_alignment (cfi_seg, EH_FRAME_ALIGNMENT);
1500 #ifdef md_fix_up_eh_frame
1501 md_fix_up_eh_frame (cfi_seg);
1502 #endif
1504 /* Make sure check_eh_frame doesn't do anything with our output. */
1505 save_flag_traditional_format = flag_traditional_format;
1506 flag_traditional_format = 1;
1508 for (fde = all_fde_data; fde ; fde = fde->next)
1510 struct cfi_insn_data *first;
1511 struct cie_entry *cie;
1513 if (fde->end_address == NULL)
1515 as_bad (_("open CFI at the end of file; missing .cfi_endproc directive"));
1516 fde->end_address = fde->start_address;
1519 cie = select_cie_for_fde (fde, &first);
1520 output_fde (fde, cie, first, fde->next == NULL ? EH_FRAME_ALIGNMENT : 2);
1523 flag_traditional_format = save_flag_traditional_format;
1526 #else /* TARGET_USE_CFIPOP */
1527 void
1528 cfi_finish (void)
1531 #endif /* TARGET_USE_CFIPOP */