daily update
[binutils.git] / gas / dw2gencfi.c
blob97e12377e76b9c8d6347544719c0ff3af4714da0
1 /* dw2gencfi.c - Support for generating Dwarf2 CFI information.
2 Copyright 2003, 2004, 2005, 2006 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 2, 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"
27 /* We re-use DWARF2_LINE_MIN_INSN_LENGTH for the code alignment field
28 of the CIE. Default to 1 if not otherwise specified. */
29 #ifndef DWARF2_LINE_MIN_INSN_LENGTH
30 # define DWARF2_LINE_MIN_INSN_LENGTH 1
31 #endif
33 /* If TARGET_USE_CFIPOP is defined, it is required that the target
34 provide the following definitions. Otherwise provide them to
35 allow compilation to continue. */
36 #ifndef TARGET_USE_CFIPOP
37 # ifndef DWARF2_DEFAULT_RETURN_COLUMN
38 # define DWARF2_DEFAULT_RETURN_COLUMN 0
39 # endif
40 # ifndef DWARF2_CIE_DATA_ALIGNMENT
41 # define DWARF2_CIE_DATA_ALIGNMENT 1
42 # endif
43 #endif
45 #ifndef EH_FRAME_ALIGNMENT
46 # define EH_FRAME_ALIGNMENT (bfd_get_arch_size (stdoutput) == 64 ? 3 : 2)
47 #endif
49 #ifndef tc_cfi_frame_initial_instructions
50 # define tc_cfi_frame_initial_instructions() ((void)0)
51 #endif
54 struct cfi_insn_data
56 struct cfi_insn_data *next;
57 int insn;
58 union {
59 struct {
60 unsigned reg;
61 offsetT offset;
62 } ri;
64 struct {
65 unsigned reg1;
66 unsigned reg2;
67 } rr;
69 unsigned r;
70 offsetT i;
72 struct {
73 symbolS *lab1;
74 symbolS *lab2;
75 } ll;
77 struct cfi_escape_data {
78 struct cfi_escape_data *next;
79 expressionS exp;
80 } *esc;
81 } u;
84 struct fde_entry
86 struct fde_entry *next;
87 symbolS *start_address;
88 symbolS *end_address;
89 struct cfi_insn_data *data;
90 struct cfi_insn_data **last;
91 unsigned char per_encoding;
92 unsigned char lsda_encoding;
93 expressionS personality;
94 expressionS lsda;
95 unsigned int return_column;
96 unsigned int signal_frame;
99 struct cie_entry
101 struct cie_entry *next;
102 symbolS *start_address;
103 unsigned int return_column;
104 unsigned int signal_frame;
105 unsigned char per_encoding;
106 unsigned char lsda_encoding;
107 expressionS personality;
108 struct cfi_insn_data *first, *last;
112 /* List of FDE entries. */
113 static struct fde_entry *all_fde_data;
114 static struct fde_entry **last_fde_data = &all_fde_data;
116 /* List of CIEs so that they could be reused. */
117 static struct cie_entry *cie_root;
119 /* Stack of old CFI data, for save/restore. */
120 struct cfa_save_data
122 struct cfa_save_data *next;
123 offsetT cfa_offset;
126 /* Current open FDE entry. */
127 struct frch_cfi_data
129 struct fde_entry *cur_fde_data;
130 symbolS *last_address;
131 offsetT cur_cfa_offset;
132 struct cfa_save_data *cfa_save_stack;
135 /* Construct a new FDE structure and add it to the end of the fde list. */
137 static struct fde_entry *
138 alloc_fde_entry (void)
140 struct fde_entry *fde = xcalloc (1, sizeof (struct fde_entry));
142 frchain_now->frch_cfi_data = xcalloc (1, sizeof (struct frch_cfi_data));
143 frchain_now->frch_cfi_data->cur_fde_data = fde;
144 *last_fde_data = fde;
145 last_fde_data = &fde->next;
147 fde->last = &fde->data;
148 fde->return_column = DWARF2_DEFAULT_RETURN_COLUMN;
149 fde->per_encoding = DW_EH_PE_omit;
150 fde->lsda_encoding = DW_EH_PE_omit;
152 return fde;
155 /* The following functions are available for a backend to construct its
156 own unwind information, usually from legacy unwind directives. */
158 /* Construct a new INSN structure and add it to the end of the insn list
159 for the currently active FDE. */
161 static struct cfi_insn_data *
162 alloc_cfi_insn_data (void)
164 struct cfi_insn_data *insn = xcalloc (1, sizeof (struct cfi_insn_data));
165 struct fde_entry *cur_fde_data = frchain_now->frch_cfi_data->cur_fde_data;
167 *cur_fde_data->last = insn;
168 cur_fde_data->last = &insn->next;
170 return insn;
173 /* Construct a new FDE structure that begins at LABEL. */
175 void
176 cfi_new_fde (symbolS *label)
178 struct fde_entry *fde = alloc_fde_entry ();
179 fde->start_address = label;
180 frchain_now->frch_cfi_data->last_address = label;
183 /* End the currently open FDE. */
185 void
186 cfi_end_fde (symbolS *label)
188 frchain_now->frch_cfi_data->cur_fde_data->end_address = label;
189 free (frchain_now->frch_cfi_data);
190 frchain_now->frch_cfi_data = NULL;
193 /* Set the return column for the current FDE. */
195 void
196 cfi_set_return_column (unsigned regno)
198 frchain_now->frch_cfi_data->cur_fde_data->return_column = regno;
201 /* Universal functions to store new instructions. */
203 static void
204 cfi_add_CFA_insn(int insn)
206 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
208 insn_ptr->insn = insn;
211 static void
212 cfi_add_CFA_insn_reg (int insn, unsigned regno)
214 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
216 insn_ptr->insn = insn;
217 insn_ptr->u.r = regno;
220 static void
221 cfi_add_CFA_insn_offset (int insn, offsetT offset)
223 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
225 insn_ptr->insn = insn;
226 insn_ptr->u.i = offset;
229 static void
230 cfi_add_CFA_insn_reg_reg (int insn, unsigned reg1, unsigned reg2)
232 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
234 insn_ptr->insn = insn;
235 insn_ptr->u.rr.reg1 = reg1;
236 insn_ptr->u.rr.reg2 = reg2;
239 static void
240 cfi_add_CFA_insn_reg_offset (int insn, unsigned regno, offsetT offset)
242 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
244 insn_ptr->insn = insn;
245 insn_ptr->u.ri.reg = regno;
246 insn_ptr->u.ri.offset = offset;
249 /* Add a CFI insn to advance the PC from the last address to LABEL. */
251 void
252 cfi_add_advance_loc (symbolS *label)
254 struct cfi_insn_data *insn = alloc_cfi_insn_data ();
256 insn->insn = DW_CFA_advance_loc;
257 insn->u.ll.lab1 = frchain_now->frch_cfi_data->last_address;
258 insn->u.ll.lab2 = label;
260 frchain_now->frch_cfi_data->last_address = label;
263 /* Add a DW_CFA_offset record to the CFI data. */
265 void
266 cfi_add_CFA_offset (unsigned regno, offsetT offset)
268 unsigned int abs_data_align;
270 assert (DWARF2_CIE_DATA_ALIGNMENT != 0);
271 cfi_add_CFA_insn_reg_offset (DW_CFA_offset, regno, offset);
273 abs_data_align = (DWARF2_CIE_DATA_ALIGNMENT < 0
274 ? -DWARF2_CIE_DATA_ALIGNMENT : DWARF2_CIE_DATA_ALIGNMENT);
275 if (offset % abs_data_align)
276 as_bad (_("register save offset not a multiple of %u"), abs_data_align);
279 /* Add a DW_CFA_def_cfa record to the CFI data. */
281 void
282 cfi_add_CFA_def_cfa (unsigned regno, offsetT offset)
284 cfi_add_CFA_insn_reg_offset (DW_CFA_def_cfa, regno, offset);
285 frchain_now->frch_cfi_data->cur_cfa_offset = offset;
288 /* Add a DW_CFA_register record to the CFI data. */
290 void
291 cfi_add_CFA_register (unsigned reg1, unsigned reg2)
293 cfi_add_CFA_insn_reg_reg (DW_CFA_register, reg1, reg2);
296 /* Add a DW_CFA_def_cfa_register record to the CFI data. */
298 void
299 cfi_add_CFA_def_cfa_register (unsigned regno)
301 cfi_add_CFA_insn_reg (DW_CFA_def_cfa_register, regno);
304 /* Add a DW_CFA_def_cfa_offset record to the CFI data. */
306 void
307 cfi_add_CFA_def_cfa_offset (offsetT offset)
309 cfi_add_CFA_insn_offset (DW_CFA_def_cfa_offset, offset);
310 frchain_now->frch_cfi_data->cur_cfa_offset = offset;
313 void
314 cfi_add_CFA_restore (unsigned regno)
316 cfi_add_CFA_insn_reg (DW_CFA_restore, regno);
319 void
320 cfi_add_CFA_undefined (unsigned regno)
322 cfi_add_CFA_insn_reg (DW_CFA_undefined, regno);
325 void
326 cfi_add_CFA_same_value (unsigned regno)
328 cfi_add_CFA_insn_reg (DW_CFA_same_value, regno);
331 void
332 cfi_add_CFA_remember_state (void)
334 struct cfa_save_data *p;
336 cfi_add_CFA_insn (DW_CFA_remember_state);
338 p = xmalloc (sizeof (*p));
339 p->cfa_offset = frchain_now->frch_cfi_data->cur_cfa_offset;
340 p->next = frchain_now->frch_cfi_data->cfa_save_stack;
341 frchain_now->frch_cfi_data->cfa_save_stack = p;
344 void
345 cfi_add_CFA_restore_state (void)
347 struct cfa_save_data *p;
349 cfi_add_CFA_insn (DW_CFA_restore_state);
351 p = frchain_now->frch_cfi_data->cfa_save_stack;
352 if (p)
354 frchain_now->frch_cfi_data->cur_cfa_offset = p->cfa_offset;
355 frchain_now->frch_cfi_data->cfa_save_stack = p->next;
356 free (p);
358 else
359 as_bad (_("CFI state restore without previous remember"));
363 /* Parse CFI assembler directives. */
365 static void dot_cfi (int);
366 static void dot_cfi_escape (int);
367 static void dot_cfi_startproc (int);
368 static void dot_cfi_endproc (int);
369 static void dot_cfi_personality (int);
370 static void dot_cfi_lsda (int);
372 /* Fake CFI type; outside the byte range of any real CFI insn. */
373 #define CFI_adjust_cfa_offset 0x100
374 #define CFI_return_column 0x101
375 #define CFI_rel_offset 0x102
376 #define CFI_escape 0x103
377 #define CFI_signal_frame 0x104
379 const pseudo_typeS cfi_pseudo_table[] =
381 { "cfi_startproc", dot_cfi_startproc, 0 },
382 { "cfi_endproc", dot_cfi_endproc, 0 },
383 { "cfi_def_cfa", dot_cfi, DW_CFA_def_cfa },
384 { "cfi_def_cfa_register", dot_cfi, DW_CFA_def_cfa_register },
385 { "cfi_def_cfa_offset", dot_cfi, DW_CFA_def_cfa_offset },
386 { "cfi_adjust_cfa_offset", dot_cfi, CFI_adjust_cfa_offset },
387 { "cfi_offset", dot_cfi, DW_CFA_offset },
388 { "cfi_rel_offset", dot_cfi, CFI_rel_offset },
389 { "cfi_register", dot_cfi, DW_CFA_register },
390 { "cfi_return_column", dot_cfi, CFI_return_column },
391 { "cfi_restore", dot_cfi, DW_CFA_restore },
392 { "cfi_undefined", dot_cfi, DW_CFA_undefined },
393 { "cfi_same_value", dot_cfi, DW_CFA_same_value },
394 { "cfi_remember_state", dot_cfi, DW_CFA_remember_state },
395 { "cfi_restore_state", dot_cfi, DW_CFA_restore_state },
396 { "cfi_window_save", dot_cfi, DW_CFA_GNU_window_save },
397 { "cfi_escape", dot_cfi_escape, 0 },
398 { "cfi_signal_frame", dot_cfi, CFI_signal_frame },
399 { "cfi_personality", dot_cfi_personality, 0 },
400 { "cfi_lsda", dot_cfi_lsda, 0 },
401 { NULL, NULL, 0 }
404 static void
405 cfi_parse_separator (void)
407 SKIP_WHITESPACE ();
408 if (*input_line_pointer == ',')
409 input_line_pointer++;
410 else
411 as_bad (_("missing separator"));
414 static unsigned
415 cfi_parse_reg (void)
417 int regno;
418 expressionS exp;
420 #ifdef tc_regname_to_dw2regnum
421 SKIP_WHITESPACE ();
422 if (is_name_beginner (*input_line_pointer)
423 || (*input_line_pointer == '%'
424 && is_name_beginner (*++input_line_pointer)))
426 char *name, c;
428 name = input_line_pointer;
429 c = get_symbol_end ();
431 if ((regno = tc_regname_to_dw2regnum (name)) < 0)
433 as_bad (_("bad register expression"));
434 regno = 0;
437 *input_line_pointer = c;
438 return regno;
440 #endif
442 expression_and_evaluate (&exp);
443 switch (exp.X_op)
445 case O_register:
446 case O_constant:
447 regno = exp.X_add_number;
448 break;
450 default:
451 as_bad (_("bad register expression"));
452 regno = 0;
453 break;
456 return regno;
459 static offsetT
460 cfi_parse_const (void)
462 return get_absolute_expression ();
465 static void
466 dot_cfi (int arg)
468 offsetT offset;
469 unsigned reg1, reg2;
471 if (frchain_now->frch_cfi_data == NULL)
473 as_bad (_("CFI instruction used without previous .cfi_startproc"));
474 ignore_rest_of_line ();
475 return;
478 /* If the last address was not at the current PC, advance to current. */
479 if (symbol_get_frag (frchain_now->frch_cfi_data->last_address) != frag_now
480 || S_GET_VALUE (frchain_now->frch_cfi_data->last_address)
481 != frag_now_fix ())
482 cfi_add_advance_loc (symbol_temp_new_now ());
484 switch (arg)
486 case DW_CFA_offset:
487 reg1 = cfi_parse_reg ();
488 cfi_parse_separator ();
489 offset = cfi_parse_const ();
490 cfi_add_CFA_offset (reg1, offset);
491 break;
493 case CFI_rel_offset:
494 reg1 = cfi_parse_reg ();
495 cfi_parse_separator ();
496 offset = cfi_parse_const ();
497 cfi_add_CFA_offset (reg1,
498 offset - frchain_now->frch_cfi_data->cur_cfa_offset);
499 break;
501 case DW_CFA_def_cfa:
502 reg1 = cfi_parse_reg ();
503 cfi_parse_separator ();
504 offset = cfi_parse_const ();
505 cfi_add_CFA_def_cfa (reg1, offset);
506 break;
508 case DW_CFA_register:
509 reg1 = cfi_parse_reg ();
510 cfi_parse_separator ();
511 reg2 = cfi_parse_reg ();
512 cfi_add_CFA_register (reg1, reg2);
513 break;
515 case DW_CFA_def_cfa_register:
516 reg1 = cfi_parse_reg ();
517 cfi_add_CFA_def_cfa_register (reg1);
518 break;
520 case DW_CFA_def_cfa_offset:
521 offset = cfi_parse_const ();
522 cfi_add_CFA_def_cfa_offset (offset);
523 break;
525 case CFI_adjust_cfa_offset:
526 offset = cfi_parse_const ();
527 cfi_add_CFA_def_cfa_offset (frchain_now->frch_cfi_data->cur_cfa_offset
528 + offset);
529 break;
531 case DW_CFA_restore:
532 for (;;)
534 reg1 = cfi_parse_reg ();
535 cfi_add_CFA_restore (reg1);
536 SKIP_WHITESPACE ();
537 if (*input_line_pointer != ',')
538 break;
539 ++input_line_pointer;
541 break;
543 case DW_CFA_undefined:
544 for (;;)
546 reg1 = cfi_parse_reg ();
547 cfi_add_CFA_undefined (reg1);
548 SKIP_WHITESPACE ();
549 if (*input_line_pointer != ',')
550 break;
551 ++input_line_pointer;
553 break;
555 case DW_CFA_same_value:
556 reg1 = cfi_parse_reg ();
557 cfi_add_CFA_same_value (reg1);
558 break;
560 case CFI_return_column:
561 reg1 = cfi_parse_reg ();
562 cfi_set_return_column (reg1);
563 break;
565 case DW_CFA_remember_state:
566 cfi_add_CFA_remember_state ();
567 break;
569 case DW_CFA_restore_state:
570 cfi_add_CFA_restore_state ();
571 break;
573 case DW_CFA_GNU_window_save:
574 cfi_add_CFA_insn (DW_CFA_GNU_window_save);
575 break;
577 case CFI_signal_frame:
578 frchain_now->frch_cfi_data->cur_fde_data->signal_frame = 1;
579 break;
581 default:
582 abort ();
585 demand_empty_rest_of_line ();
588 static void
589 dot_cfi_escape (int ignored ATTRIBUTE_UNUSED)
591 struct cfi_escape_data *head, **tail, *e;
592 struct cfi_insn_data *insn;
594 if (frchain_now->frch_cfi_data == NULL)
596 as_bad (_("CFI instruction used without previous .cfi_startproc"));
597 ignore_rest_of_line ();
598 return;
601 /* If the last address was not at the current PC, advance to current. */
602 if (symbol_get_frag (frchain_now->frch_cfi_data->last_address) != frag_now
603 || S_GET_VALUE (frchain_now->frch_cfi_data->last_address)
604 != frag_now_fix ())
605 cfi_add_advance_loc (symbol_temp_new_now ());
607 tail = &head;
610 e = xmalloc (sizeof (*e));
611 do_parse_cons_expression (&e->exp, 1);
612 *tail = e;
613 tail = &e->next;
615 while (*input_line_pointer++ == ',');
616 *tail = NULL;
618 insn = alloc_cfi_insn_data ();
619 insn->insn = CFI_escape;
620 insn->u.esc = head;
622 --input_line_pointer;
623 demand_empty_rest_of_line ();
626 static void
627 dot_cfi_personality (int ignored ATTRIBUTE_UNUSED)
629 struct fde_entry *fde;
630 offsetT encoding;
632 if (frchain_now->frch_cfi_data == NULL)
634 as_bad (_("CFI instruction used without previous .cfi_startproc"));
635 ignore_rest_of_line ();
636 return;
639 fde = frchain_now->frch_cfi_data->cur_fde_data;
640 encoding = get_absolute_expression ();
641 if (encoding == DW_EH_PE_omit)
643 demand_empty_rest_of_line ();
644 fde->per_encoding = encoding;
645 return;
648 if ((encoding & 0xff) != encoding
649 || ((encoding & 0x70) != 0
650 #if defined DIFF_EXPR_OK || defined tc_cfi_emit_pcrel_expr
651 && (encoding & 0x70) != DW_EH_PE_pcrel
652 #endif
654 /* leb128 can be handled, but does something actually need it? */
655 || (encoding & 7) == DW_EH_PE_uleb128
656 || (encoding & 7) > DW_EH_PE_udata8)
658 as_bad (_("invalid or unsupported encoding in .cfi_personality"));
659 ignore_rest_of_line ();
660 return;
663 if (*input_line_pointer++ != ',')
665 as_bad (_(".cfi_personality requires encoding and symbol arguments"));
666 ignore_rest_of_line ();
667 return;
670 expression_and_evaluate (&fde->personality);
671 switch (fde->personality.X_op)
673 case O_symbol:
674 break;
675 case O_constant:
676 if ((encoding & 0x70) == DW_EH_PE_pcrel)
677 encoding = DW_EH_PE_omit;
678 break;
679 default:
680 encoding = DW_EH_PE_omit;
681 break;
684 fde->per_encoding = encoding;
686 if (encoding == DW_EH_PE_omit)
688 as_bad (_("wrong second argument to .cfi_personality"));
689 ignore_rest_of_line ();
690 return;
693 demand_empty_rest_of_line ();
696 static void
697 dot_cfi_lsda (int ignored ATTRIBUTE_UNUSED)
699 struct fde_entry *fde;
700 offsetT encoding;
702 if (frchain_now->frch_cfi_data == NULL)
704 as_bad (_("CFI instruction used without previous .cfi_startproc"));
705 ignore_rest_of_line ();
706 return;
709 fde = frchain_now->frch_cfi_data->cur_fde_data;
710 encoding = get_absolute_expression ();
711 if (encoding == DW_EH_PE_omit)
713 demand_empty_rest_of_line ();
714 fde->lsda_encoding = encoding;
715 return;
718 if ((encoding & 0xff) != encoding
719 || ((encoding & 0x70) != 0
720 #if defined DIFF_EXPR_OK || defined tc_cfi_emit_pcrel_expr
721 && (encoding & 0x70) != DW_EH_PE_pcrel
722 #endif
724 /* leb128 can be handled, but does something actually need it? */
725 || (encoding & 7) == DW_EH_PE_uleb128
726 || (encoding & 7) > DW_EH_PE_udata8)
728 as_bad (_("invalid or unsupported encoding in .cfi_lsda"));
729 ignore_rest_of_line ();
730 return;
733 if (*input_line_pointer++ != ',')
735 as_bad (_(".cfi_lsda requires encoding and symbol arguments"));
736 ignore_rest_of_line ();
737 return;
740 fde->lsda_encoding = encoding;
742 expression_and_evaluate (&fde->lsda);
743 switch (fde->lsda.X_op)
745 case O_symbol:
746 break;
747 case O_constant:
748 if ((encoding & 0x70) == DW_EH_PE_pcrel)
749 encoding = DW_EH_PE_omit;
750 break;
751 default:
752 encoding = DW_EH_PE_omit;
753 break;
756 fde->lsda_encoding = encoding;
758 if (encoding == DW_EH_PE_omit)
760 as_bad (_("wrong second argument to .cfi_lsda"));
761 ignore_rest_of_line ();
762 return;
765 demand_empty_rest_of_line ();
768 static void
769 dot_cfi_startproc (int ignored ATTRIBUTE_UNUSED)
771 int simple = 0;
773 if (frchain_now->frch_cfi_data != NULL)
775 as_bad (_("previous CFI entry not closed (missing .cfi_endproc)"));
776 ignore_rest_of_line ();
777 return;
780 cfi_new_fde (symbol_temp_new_now ());
782 SKIP_WHITESPACE ();
783 if (is_name_beginner (*input_line_pointer))
785 char *name, c;
787 name = input_line_pointer;
788 c = get_symbol_end ();
790 if (strcmp (name, "simple") == 0)
792 simple = 1;
793 *input_line_pointer = c;
795 else
796 input_line_pointer = name;
798 demand_empty_rest_of_line ();
800 frchain_now->frch_cfi_data->cur_cfa_offset = 0;
801 if (!simple)
802 tc_cfi_frame_initial_instructions ();
805 static void
806 dot_cfi_endproc (int ignored ATTRIBUTE_UNUSED)
808 if (frchain_now->frch_cfi_data == NULL)
810 as_bad (_(".cfi_endproc without corresponding .cfi_startproc"));
811 ignore_rest_of_line ();
812 return;
815 cfi_end_fde (symbol_temp_new_now ());
817 demand_empty_rest_of_line ();
821 /* Emit a single byte into the current segment. */
823 static inline void
824 out_one (int byte)
826 FRAG_APPEND_1_CHAR (byte);
829 /* Emit a two-byte word into the current segment. */
831 static inline void
832 out_two (int data)
834 md_number_to_chars (frag_more (2), data, 2);
837 /* Emit a four byte word into the current segment. */
839 static inline void
840 out_four (int data)
842 md_number_to_chars (frag_more (4), data, 4);
845 /* Emit an unsigned "little-endian base 128" number. */
847 static void
848 out_uleb128 (addressT value)
850 output_leb128 (frag_more (sizeof_leb128 (value, 0)), value, 0);
853 /* Emit an unsigned "little-endian base 128" number. */
855 static void
856 out_sleb128 (offsetT value)
858 output_leb128 (frag_more (sizeof_leb128 (value, 1)), value, 1);
861 static void
862 output_cfi_insn (struct cfi_insn_data *insn)
864 offsetT offset;
865 unsigned int regno;
867 switch (insn->insn)
869 case DW_CFA_advance_loc:
871 symbolS *from = insn->u.ll.lab1;
872 symbolS *to = insn->u.ll.lab2;
874 if (symbol_get_frag (to) == symbol_get_frag (from))
876 addressT delta = S_GET_VALUE (to) - S_GET_VALUE (from);
877 addressT scaled = delta / DWARF2_LINE_MIN_INSN_LENGTH;
879 if (scaled <= 0x3F)
880 out_one (DW_CFA_advance_loc + scaled);
881 else if (delta <= 0xFF)
883 out_one (DW_CFA_advance_loc1);
884 out_one (delta);
886 else if (delta <= 0xFFFF)
888 out_one (DW_CFA_advance_loc2);
889 out_two (delta);
891 else
893 out_one (DW_CFA_advance_loc4);
894 out_four (delta);
897 else
899 expressionS exp;
901 exp.X_op = O_subtract;
902 exp.X_add_symbol = to;
903 exp.X_op_symbol = from;
904 exp.X_add_number = 0;
906 /* The code in ehopt.c expects that one byte of the encoding
907 is already allocated to the frag. This comes from the way
908 that it scans the .eh_frame section looking first for the
909 .byte DW_CFA_advance_loc4. */
910 frag_more (1);
912 frag_var (rs_cfa, 4, 0, DWARF2_LINE_MIN_INSN_LENGTH << 3,
913 make_expr_symbol (&exp), frag_now_fix () - 1,
914 (char *) frag_now);
917 break;
919 case DW_CFA_def_cfa:
920 offset = insn->u.ri.offset;
921 if (offset < 0)
923 out_one (DW_CFA_def_cfa_sf);
924 out_uleb128 (insn->u.ri.reg);
925 out_sleb128 (offset / DWARF2_CIE_DATA_ALIGNMENT);
927 else
929 out_one (DW_CFA_def_cfa);
930 out_uleb128 (insn->u.ri.reg);
931 out_uleb128 (offset);
933 break;
935 case DW_CFA_def_cfa_register:
936 case DW_CFA_undefined:
937 case DW_CFA_same_value:
938 out_one (insn->insn);
939 out_uleb128 (insn->u.r);
940 break;
942 case DW_CFA_def_cfa_offset:
943 offset = insn->u.i;
944 if (offset < 0)
946 out_one (DW_CFA_def_cfa_offset_sf);
947 out_sleb128 (offset / DWARF2_CIE_DATA_ALIGNMENT);
949 else
951 out_one (DW_CFA_def_cfa_offset);
952 out_uleb128 (offset);
954 break;
956 case DW_CFA_restore:
957 regno = insn->u.r;
958 if (regno <= 0x3F)
960 out_one (DW_CFA_restore + regno);
962 else
964 out_one (DW_CFA_restore_extended);
965 out_uleb128 (regno);
967 break;
969 case DW_CFA_offset:
970 regno = insn->u.ri.reg;
971 offset = insn->u.ri.offset / DWARF2_CIE_DATA_ALIGNMENT;
972 if (offset < 0)
974 out_one (DW_CFA_offset_extended_sf);
975 out_uleb128 (regno);
976 out_sleb128 (offset);
978 else if (regno <= 0x3F)
980 out_one (DW_CFA_offset + regno);
981 out_uleb128 (offset);
983 else
985 out_one (DW_CFA_offset_extended);
986 out_uleb128 (regno);
987 out_uleb128 (offset);
989 break;
991 case DW_CFA_register:
992 out_one (DW_CFA_register);
993 out_uleb128 (insn->u.rr.reg1);
994 out_uleb128 (insn->u.rr.reg2);
995 break;
997 case DW_CFA_remember_state:
998 case DW_CFA_restore_state:
999 out_one (insn->insn);
1000 break;
1002 case DW_CFA_GNU_window_save:
1003 out_one (DW_CFA_GNU_window_save);
1004 break;
1006 case CFI_escape:
1008 struct cfi_escape_data *e;
1009 for (e = insn->u.esc; e ; e = e->next)
1010 emit_expr (&e->exp, 1);
1011 break;
1014 default:
1015 abort ();
1019 static offsetT
1020 encoding_size (unsigned char encoding)
1022 if (encoding == DW_EH_PE_omit)
1023 return 0;
1024 switch (encoding & 0x7)
1026 case 0:
1027 return bfd_get_arch_size (stdoutput) == 64 ? 8 : 4;
1028 case DW_EH_PE_udata2:
1029 return 2;
1030 case DW_EH_PE_udata4:
1031 return 4;
1032 case DW_EH_PE_udata8:
1033 return 8;
1034 default:
1035 abort ();
1039 static void
1040 output_cie (struct cie_entry *cie)
1042 symbolS *after_size_address, *end_address;
1043 expressionS exp;
1044 struct cfi_insn_data *i;
1045 offsetT augmentation_size;
1047 cie->start_address = symbol_temp_new_now ();
1048 after_size_address = symbol_temp_make ();
1049 end_address = symbol_temp_make ();
1051 exp.X_op = O_subtract;
1052 exp.X_add_symbol = end_address;
1053 exp.X_op_symbol = after_size_address;
1054 exp.X_add_number = 0;
1056 emit_expr (&exp, 4); /* Length. */
1057 symbol_set_value_now (after_size_address);
1058 out_four (0); /* CIE id. */
1059 out_one (DW_CIE_VERSION); /* Version. */
1060 out_one ('z'); /* Augmentation. */
1061 if (cie->per_encoding != DW_EH_PE_omit)
1062 out_one ('P');
1063 if (cie->lsda_encoding != DW_EH_PE_omit)
1064 out_one ('L');
1065 out_one ('R');
1066 if (cie->signal_frame)
1067 out_one ('S');
1068 out_one (0);
1069 out_uleb128 (DWARF2_LINE_MIN_INSN_LENGTH); /* Code alignment. */
1070 out_sleb128 (DWARF2_CIE_DATA_ALIGNMENT); /* Data alignment. */
1071 if (DW_CIE_VERSION == 1) /* Return column. */
1072 out_one (cie->return_column);
1073 else
1074 out_uleb128 (cie->return_column);
1075 augmentation_size = 1 + (cie->lsda_encoding != DW_EH_PE_omit);
1076 if (cie->per_encoding != DW_EH_PE_omit)
1077 augmentation_size += 1 + encoding_size (cie->per_encoding);
1078 out_uleb128 (augmentation_size); /* Augmentation size. */
1079 if (cie->per_encoding != DW_EH_PE_omit)
1081 offsetT size = encoding_size (cie->per_encoding);
1082 out_one (cie->per_encoding);
1083 exp = cie->personality;
1084 if ((cie->per_encoding & 0x70) == DW_EH_PE_pcrel)
1086 #ifdef DIFF_EXPR_OK
1087 exp.X_op = O_subtract;
1088 exp.X_op_symbol = symbol_temp_new_now ();
1089 emit_expr (&exp, size);
1090 #elif defined (tc_cfi_emit_pcrel_expr)
1091 tc_cfi_emit_pcrel_expr (&exp, size);
1092 #else
1093 abort ();
1094 #endif
1096 else
1097 emit_expr (&exp, size);
1099 if (cie->lsda_encoding != DW_EH_PE_omit)
1100 out_one (cie->lsda_encoding);
1101 #if defined DIFF_EXPR_OK || defined tc_cfi_emit_pcrel_expr
1102 out_one (DW_EH_PE_pcrel | DW_EH_PE_sdata4);
1103 #else
1104 out_one (DW_EH_PE_sdata4);
1105 #endif
1107 if (cie->first)
1108 for (i = cie->first; i != cie->last; i = i->next)
1109 output_cfi_insn (i);
1111 frag_align (2, DW_CFA_nop, 0);
1112 symbol_set_value_now (end_address);
1115 static void
1116 output_fde (struct fde_entry *fde, struct cie_entry *cie,
1117 struct cfi_insn_data *first, int align)
1119 symbolS *after_size_address, *end_address;
1120 expressionS exp;
1121 offsetT augmentation_size;
1123 after_size_address = symbol_temp_make ();
1124 end_address = symbol_temp_make ();
1126 exp.X_op = O_subtract;
1127 exp.X_add_symbol = end_address;
1128 exp.X_op_symbol = after_size_address;
1129 exp.X_add_number = 0;
1130 emit_expr (&exp, 4); /* Length. */
1131 symbol_set_value_now (after_size_address);
1133 exp.X_add_symbol = after_size_address;
1134 exp.X_op_symbol = cie->start_address;
1135 emit_expr (&exp, 4); /* CIE offset. */
1137 #ifdef DIFF_EXPR_OK
1138 exp.X_add_symbol = fde->start_address;
1139 exp.X_op_symbol = symbol_temp_new_now ();
1140 emit_expr (&exp, 4); /* Code offset. */
1141 #else
1142 exp.X_op = O_symbol;
1143 exp.X_add_symbol = fde->start_address;
1144 exp.X_op_symbol = NULL;
1145 #ifdef tc_cfi_emit_pcrel_expr
1146 tc_cfi_emit_pcrel_expr (&exp, 4); /* Code offset. */
1147 #else
1148 emit_expr (&exp, 4); /* Code offset. */
1149 #endif
1150 exp.X_op = O_subtract;
1151 #endif
1153 exp.X_add_symbol = fde->end_address;
1154 exp.X_op_symbol = fde->start_address; /* Code length. */
1155 emit_expr (&exp, 4);
1157 augmentation_size = encoding_size (fde->lsda_encoding);
1158 out_uleb128 (augmentation_size); /* Augmentation size. */
1160 if (fde->lsda_encoding != DW_EH_PE_omit)
1162 exp = fde->lsda;
1163 if ((fde->lsda_encoding & 0x70) == DW_EH_PE_pcrel)
1165 #ifdef DIFF_EXPR_OK
1166 exp.X_op = O_subtract;
1167 exp.X_op_symbol = symbol_temp_new_now ();
1168 emit_expr (&exp, augmentation_size);
1169 #elif defined (tc_cfi_emit_pcrel_expr)
1170 tc_cfi_emit_pcrel_expr (&exp, augmentation_size);
1171 #else
1172 abort ();
1173 #endif
1175 else
1176 emit_expr (&exp, augmentation_size);
1179 for (; first; first = first->next)
1180 output_cfi_insn (first);
1182 frag_align (align, DW_CFA_nop, 0);
1183 symbol_set_value_now (end_address);
1186 static struct cie_entry *
1187 select_cie_for_fde (struct fde_entry *fde, struct cfi_insn_data **pfirst)
1189 struct cfi_insn_data *i, *j;
1190 struct cie_entry *cie;
1192 for (cie = cie_root; cie; cie = cie->next)
1194 if (cie->return_column != fde->return_column
1195 || cie->signal_frame != fde->signal_frame
1196 || cie->per_encoding != fde->per_encoding
1197 || cie->lsda_encoding != fde->lsda_encoding)
1198 continue;
1199 if (cie->per_encoding != DW_EH_PE_omit)
1201 if (cie->personality.X_op != fde->personality.X_op
1202 || cie->personality.X_add_number
1203 != fde->personality.X_add_number)
1204 continue;
1205 switch (cie->personality.X_op)
1207 case O_constant:
1208 if (cie->personality.X_unsigned != fde->personality.X_unsigned)
1209 continue;
1210 break;
1211 case O_symbol:
1212 if (cie->personality.X_add_symbol
1213 != fde->personality.X_add_symbol)
1214 continue;
1215 break;
1216 default:
1217 abort ();
1220 for (i = cie->first, j = fde->data;
1221 i != cie->last && j != NULL;
1222 i = i->next, j = j->next)
1224 if (i->insn != j->insn)
1225 goto fail;
1226 switch (i->insn)
1228 case DW_CFA_advance_loc:
1229 case DW_CFA_remember_state:
1230 /* We reached the first advance/remember in the FDE,
1231 but did not reach the end of the CIE list. */
1232 goto fail;
1234 case DW_CFA_offset:
1235 case DW_CFA_def_cfa:
1236 if (i->u.ri.reg != j->u.ri.reg)
1237 goto fail;
1238 if (i->u.ri.offset != j->u.ri.offset)
1239 goto fail;
1240 break;
1242 case DW_CFA_register:
1243 if (i->u.rr.reg1 != j->u.rr.reg1)
1244 goto fail;
1245 if (i->u.rr.reg2 != j->u.rr.reg2)
1246 goto fail;
1247 break;
1249 case DW_CFA_def_cfa_register:
1250 case DW_CFA_restore:
1251 case DW_CFA_undefined:
1252 case DW_CFA_same_value:
1253 if (i->u.r != j->u.r)
1254 goto fail;
1255 break;
1257 case DW_CFA_def_cfa_offset:
1258 if (i->u.i != j->u.i)
1259 goto fail;
1260 break;
1262 case CFI_escape:
1263 /* Don't bother matching these for now. */
1264 goto fail;
1266 default:
1267 abort ();
1271 /* Success if we reached the end of the CIE list, and we've either
1272 run out of FDE entries or we've encountered an advance,
1273 remember, or escape. */
1274 if (i == cie->last
1275 && (!j
1276 || j->insn == DW_CFA_advance_loc
1277 || j->insn == DW_CFA_remember_state
1278 || j->insn == CFI_escape))
1280 *pfirst = j;
1281 return cie;
1284 fail:;
1287 cie = xmalloc (sizeof (struct cie_entry));
1288 cie->next = cie_root;
1289 cie_root = cie;
1290 cie->return_column = fde->return_column;
1291 cie->signal_frame = fde->signal_frame;
1292 cie->per_encoding = fde->per_encoding;
1293 cie->lsda_encoding = fde->lsda_encoding;
1294 cie->personality = fde->personality;
1295 cie->first = fde->data;
1297 for (i = cie->first; i ; i = i->next)
1298 if (i->insn == DW_CFA_advance_loc
1299 || i->insn == DW_CFA_remember_state
1300 || i->insn == CFI_escape)
1301 break;
1303 cie->last = i;
1304 *pfirst = i;
1306 output_cie (cie);
1308 return cie;
1311 void
1312 cfi_finish (void)
1314 segT cfi_seg;
1315 struct fde_entry *fde;
1316 int save_flag_traditional_format;
1318 if (all_fde_data == 0)
1319 return;
1321 /* Open .eh_frame section. */
1322 cfi_seg = subseg_new (".eh_frame", 0);
1323 bfd_set_section_flags (stdoutput, cfi_seg,
1324 SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_READONLY);
1325 subseg_set (cfi_seg, 0);
1326 record_alignment (cfi_seg, EH_FRAME_ALIGNMENT);
1328 /* Make sure check_eh_frame doesn't do anything with our output. */
1329 save_flag_traditional_format = flag_traditional_format;
1330 flag_traditional_format = 1;
1332 for (fde = all_fde_data; fde ; fde = fde->next)
1334 struct cfi_insn_data *first;
1335 struct cie_entry *cie;
1337 if (fde->end_address == NULL)
1339 as_bad (_("open CFI at the end of file; missing .cfi_endproc directive"));
1340 fde->end_address = fde->start_address;
1343 cie = select_cie_for_fde (fde, &first);
1344 output_fde (fde, cie, first, fde->next == NULL ? EH_FRAME_ALIGNMENT : 2);
1347 flag_traditional_format = save_flag_traditional_format;