1 /* dw2gencfi.c - Support for generating Dwarf2 CFI information.
2 Copyright 2003 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)
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, 59 Temple Place - Suite 330, Boston, MA
24 #include "dw2gencfi.h"
31 struct cie_entry *next;
38 struct cfi_data *next;
43 addressT start_address;
45 addressT last_address;
46 const char *labelname;
47 struct cfi_data *data;
48 struct cfi_info *next;
51 /* Current open CFI entry. */
52 static struct cfi_info *cfi_info;
54 /* List of CIEs so that they could be reused. */
55 static struct cie_entry *cie_root;
57 /* Current target config. */
58 static struct cfi_config current_config;
60 /* This is the main entry point to the CFI machinery. */
61 static void dot_cfi (int arg);
63 const pseudo_typeS cfi_pseudo_table[] =
65 { "cfi_verbose", dot_cfi, CFI_verbose },
66 { "cfi_startproc", dot_cfi, CFI_startproc },
67 { "cfi_endproc", dot_cfi, CFI_endproc },
68 { "cfi_def_cfa", dot_cfi, CFA_def_cfa },
69 { "cfi_def_cfa_register", dot_cfi, CFA_def_cfa_register },
70 { "cfi_def_cfa_offset", dot_cfi, CFA_def_cfa_offset },
71 { "cfi_adjust_cfa_offset", dot_cfi, CFI_adjust_cfa_offset },
72 { "cfi_offset", dot_cfi, CFA_offset },
73 { "cfi_register", dot_cfi, CFA_register },
78 cfi_insn_str (enum cfi_insn insn)
86 case CFA_advance_loc1:
87 return "CFA_advance_loc1";
88 case CFA_advance_loc2:
89 return "CFA_advance_loc2";
90 case CFA_advance_loc4:
91 return "CFA_advance_loc4";
92 case CFA_offset_extended:
93 return "CFA_offset_extended";
94 case CFA_resotre_extended:
95 return "CFA_resotre_extended";
97 return "CFA_undefined";
99 return "CFA_same_value";
101 return "CFA_register";
102 case CFA_remember_state:
103 return "CFA_remember_state";
104 case CFA_restore_state:
105 return "CFA_restore_state";
107 return "CFA_def_cfa";
108 case CFA_def_cfa_register:
109 return "CFA_def_cfa_register";
110 case CFA_def_cfa_offset:
111 return "CFA_def_cfa_offset";
112 case CFA_advance_loc:
113 return "CFA_advance_loc";
117 return "CFA_restore";
122 return "CFA_unknown";
125 static struct cfi_data *
126 alloc_cfi_data (void)
128 return (struct cfi_data *) xcalloc (sizeof (struct cfi_info), 1);
131 static struct cfi_info *
132 alloc_cfi_info (void)
134 return (struct cfi_info *) xcalloc (sizeof (struct cfi_info), 1);
137 /* Parse arguments. */
139 cfi_parse_arg (long *param, int resolvereg)
145 assert (param != NULL);
148 if (sscanf (input_line_pointer, "%li%n", &value, &nchars) >= 1)
150 input_line_pointer += nchars;
153 #ifdef tc_regname_to_dw2regnum
154 else if (resolvereg && ((is_name_beginner (*input_line_pointer))
155 || (*input_line_pointer == '%'
156 && is_name_beginner (*(++input_line_pointer)))))
160 name = input_line_pointer;
161 c = get_symbol_end ();
162 p = input_line_pointer;
164 if ((value = tc_regname_to_dw2regnum (name)) >= 0)
172 _("can't convert argument to a register number") :
173 _("can't convert argument to an integer"));
179 if (*input_line_pointer == ',')
181 input_line_pointer++;
189 cfi_parse_reg (long *param)
191 return cfi_parse_arg (param, 1);
195 cfi_parse_const (long *param)
197 return cfi_parse_arg (param, 0);
201 cfi_add_insn (enum cfi_insn insn, long param0, long param1)
203 struct cfi_data *data_ptr;
207 cfi_info->data = alloc_cfi_data ();
208 data_ptr = cfi_info->data;
212 data_ptr = cfi_info->data;
214 while (data_ptr && data_ptr->next)
215 data_ptr = data_ptr->next;
217 data_ptr->next = alloc_cfi_data ();
219 data_ptr = data_ptr->next;
222 data_ptr->insn = insn;
223 data_ptr->param[0] = param0;
224 data_ptr->param[1] = param1;
228 cfi_advance_loc (void)
230 addressT curr_address = frag_now_fix ();
231 if (cfi_info->last_address == curr_address)
233 cfi_add_insn (CFA_advance_loc,
234 (long) (curr_address - cfi_info->last_address), 0);
235 cfi_info->last_address = curr_address;
239 get_current_offset (struct cfi_info *info)
241 long current_offset = 0;
242 struct cfi_data *data = info->data;
247 if (data->insn == CFA_def_cfa)
248 current_offset = data->param[1];
249 else if (data->insn == CFA_def_cfa_offset)
250 current_offset = data->param[0];
254 return current_offset;
258 cfi_make_insn (int arg)
260 long param[2] = { 0, 0 };
264 as_bad (_("CFI instruction used without previous .cfi_startproc"));
272 /* Instructions that take two arguments (register, integer). */
275 if (cfi_parse_reg (¶m[0]) < 0)
277 as_bad (_("first argument to %s is not a register"),
281 if (cfi_parse_const (¶m[1]) < 0)
283 as_bad (_("second argument to %s is not a number"),
290 if (cfi_parse_reg (¶m[0]) < 0)
292 as_bad (_("first argument to %s is not a register"),
296 if (cfi_parse_reg (¶m[1]) < 0)
298 as_bad (_("second argument to %s is not a register"),
304 /* Instructions that take one register argument. */
305 case CFA_def_cfa_register:
306 if (cfi_parse_reg (¶m[0]) < 0)
308 as_bad (_("argument to %s is not a register"), cfi_insn_str (arg));
313 /* Instructions that take one integer argument. */
314 case CFA_def_cfa_offset:
315 if (cfi_parse_const (¶m[0]) < 0)
317 as_bad (_("argument to %s is not a number"), cfi_insn_str (arg));
322 /* Special handling for pseudo-instruction. */
323 case CFI_adjust_cfa_offset:
324 if (cfi_parse_const (¶m[0]) < 0)
326 as_bad (_("argument to %s is not a number"),
327 ".cfi_adjust_cfa_offset");
330 param[0] += get_current_offset (cfi_info);
331 arg = CFA_def_cfa_offset;
335 as_bad (_("unknown CFI instruction %d (%s)"), arg, cfi_insn_str (arg));
338 cfi_add_insn (arg, param[0], param[1]);
344 char symname[40], *symbase=".Llbl_cfi";
348 snprintf (symname, sizeof (symname), "%s_0x%lx",
349 symbase, (long) frag_now_fix ());
350 while ((symbolP = symbol_find (symname)))
352 if ((S_GET_VALUE (symbolP) == frag_now_fix ())
353 && (S_GET_SEGMENT (symbolP) == now_seg))
356 snprintf (symname, sizeof (symname), "%s_0x%lx_%u",
357 symbase, (long) frag_now_fix (), i++);
360 symbolP = (symbolS *) local_symbol_make (symname, now_seg,
361 (valueT) frag_now_fix (),
364 symbolP = symbol_make (symname);
370 dot_cfi_startproc (void)
372 #ifdef tc_cfi_frame_initial_instructions
373 const char *simple = "simple";
378 as_bad (_("previous CFI entry not closed (missing .cfi_endproc)"));
382 #if defined(TARGET_USE_CFIPOP)
383 /* Because this file is linked even for architectures that
384 don't use CFI, we must wrap this call. */
385 if (current_config.addr_length == 0)
389 cfi_info = alloc_cfi_info ();
391 cfi_info->start_address = frag_now_fix ();
392 cfi_info->last_address = cfi_info->start_address;
393 cfi_info->labelname = S_GET_NAME (cfi_get_label ());
396 #ifdef tc_cfi_frame_initial_instructions
397 if (strncmp (simple, input_line_pointer, strlen (simple)) != 0)
398 tc_cfi_frame_initial_instructions ();
400 input_line_pointer += strlen (simple);
404 #define cfi_is_advance_insn(insn) \
405 ((insn >= CFA_set_loc && insn <= CFA_advance_loc4) \
406 || insn == CFA_advance_loc)
408 /* Output CFI instructions to the file. */
422 output_data (char **p, unsigned long *size, enum data_types type, long value)
425 unsigned int ret_size;
444 /* This should never happen - throw an internal error. */
445 as_fatal (_("unknown type %d"), type);
449 if (*size < ret_size)
451 as_bad (_("output_data buffer is too small"));
460 printf ("\t.byte\t0x%x\n", (unsigned char) *ptr);
463 *(short *) ptr = (short) value & 0xFFFF;
465 printf ("\t.half\t0x%x\n", (unsigned short) *ptr);
468 *(int *) ptr = (int) value & 0xFFFFFFFF;
470 printf ("\t.long\t0x%x\n", (unsigned int) *ptr);
473 *(long long *) ptr = (long long) value & 0xFFFFFFFF;
475 printf ("\t.quad\t0x%x\n", (unsigned int) *ptr);
479 ret_size = output_leb128 (ptr, value, type == t_sleb128);
481 printf ("\t.%s\t0x%lx\n",
482 type == t_sleb128 ? "sleb128" : "uleb128",
486 as_fatal (_("unknown type %d"), type);
497 cfi_output_insn (struct cfi_data *data, char **buf, unsigned long *buf_size)
499 char **pbuf = buf, *orig_buf = *buf;
503 as_fatal (_("cfi_output_insn called with NULL pointer"));
507 case CFA_advance_loc:
509 printf ("\t# %s(%ld)\n", cfi_insn_str (data->insn),
511 if (data->param[0] <= 0x3F)
513 output_data (pbuf, buf_size, t_byte, CFA_advance_loc +
514 (data->param[0] / current_config.code_align));
516 else if (data->param[0] <= 0xFF)
518 output_data (pbuf, buf_size, t_byte, CFA_advance_loc1);
519 output_data (pbuf, buf_size, t_byte,
520 data->param[0] / current_config.code_align);
522 else if (data->param[0] <= 0xFFFF)
524 output_data (pbuf, buf_size, t_byte, CFA_advance_loc2);
525 output_data (pbuf, buf_size, t_half,
526 data->param[0] / current_config.code_align);
530 output_data (pbuf, buf_size, t_byte, CFA_advance_loc4);
531 output_data (pbuf, buf_size, t_long,
532 data->param[0] / current_config.code_align);
538 printf ("\t# CFA_def_cfa(%ld,%ld)\n",
539 data->param[0], data->param[1]);
540 output_data (pbuf, buf_size, t_byte, CFA_def_cfa);
541 output_data (pbuf, buf_size, t_uleb128, data->param[0]);
542 output_data (pbuf, buf_size, t_uleb128, data->param[1]);
545 case CFA_def_cfa_register:
546 case CFA_def_cfa_offset:
548 printf ("\t# %s(%ld)\n", cfi_insn_str (data->insn),
550 output_data (pbuf, buf_size, t_byte, data->insn);
551 output_data (pbuf, buf_size, t_uleb128, data->param[0]);
556 printf ("\t# %s(%ld,%ld)\n", cfi_insn_str (data->insn),
557 data->param[0], data->param[1]);
559 /* Check whether to use CFA_offset or CFA_offset_extended. */
560 if (data->param[0] <= 0x3F)
561 output_data (pbuf, buf_size, t_byte, CFA_offset + data->param[0]);
564 output_data (pbuf, buf_size, t_byte, CFA_offset_extended);
565 output_data (pbuf, buf_size, t_uleb128, data->param[0]);
567 output_data (pbuf, buf_size, t_uleb128,
568 data->param[1] / current_config.data_align);
573 printf ("\t# %s(%ld,%ld)\n", cfi_insn_str (data->insn),
574 data->param[0], data->param[1]);
575 output_data (pbuf, buf_size, t_byte, CFA_register);
576 output_data (pbuf, buf_size, t_uleb128, data->param[0]);
577 output_data (pbuf, buf_size, t_uleb128, data->param[1]);
582 printf ("\t# CFA_nop\n");
583 output_data (pbuf, buf_size, t_byte, CFA_nop);
587 as_warn ("CFA_unknown[%d](%ld,%ld)", data->insn,
588 data->param[0], data->param[1]);
590 size = *pbuf - orig_buf;
597 dot_cfi_endproc (void)
599 struct cfi_data *data_ptr;
600 struct cie_entry *cie_ptr;
601 char *cie_buf, *fde_buf, *pbuf, *where;
602 unsigned long buf_size, cie_size, fde_size, last_cie_offset;
603 unsigned long fde_initloc_offset, fde_len_offset, fde_offset;
604 segT saved_seg, cfi_seg;
609 as_bad (_(".cfi_endproc without corresponding .cfi_startproc"));
612 cfi_info->end_address = frag_now_fix ();
614 /* Open .eh_frame section. */
616 cfi_seg = subseg_new (".eh_frame", 0);
618 bfd_set_section_flags (stdoutput, cfi_seg,
619 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
621 subseg_set (cfi_seg, 0);
624 cie_buf = xcalloc (1024, 1);
625 /* Skip space for CIE length. */
630 printf ("# CIE *****\n");
633 output_data (&pbuf, &buf_size, t_long, 0x0);
635 output_data (&pbuf, &buf_size, t_byte, 1);
637 output_data (&pbuf, &buf_size, t_byte, 0);
638 /* Code alignment. */
639 output_data (&pbuf, &buf_size, t_uleb128, current_config.code_align);
640 /* Data alignment. */
641 output_data (&pbuf, &buf_size, t_sleb128, current_config.data_align);
642 /* Return address column. */
643 output_data (&pbuf, &buf_size, t_byte, current_config.ra_column);
645 /* Build CFI instructions. */
646 data_ptr = cfi_info->data;
647 while (data_ptr && !cfi_is_advance_insn (data_ptr->insn))
649 cfi_output_insn (data_ptr, &pbuf, &buf_size);
650 data_ptr = data_ptr->next;
653 /* Align the whole data to current_config.eh_align. */
654 cie_size = pbuf - cie_buf;
655 cie_size += current_config.eh_align - cie_size % current_config.eh_align;
659 output_data (&pbuf, &buf_size, t_long, cie_size - 4);
661 /* OK, we built the CIE. Let's write it to the file... */
662 last_cie_offset = frag_now_fix ();
664 /* Check if we have already emitted the exactly same CIE.
665 If yes then use its offset instead and don't put out
670 if (cie_ptr->size == cie_size - 4
671 && memcmp (cie_ptr->data, cie_buf + 4, cie_ptr->size) == 0)
673 cie_ptr = cie_ptr->next;
676 /* If we have found the same CIE, use it... */
680 printf ("# Duplicate CIE found. Previous is at offset %lu\n",
682 last_cie_offset = cie_ptr->offset;
686 /* Otherwise join this CIE to the list. */
687 where = (unsigned char *) frag_more (cie_size);
688 memcpy (where, cie_buf, cie_size);
692 while (cie_ptr->next)
693 cie_ptr = cie_ptr->next;
694 cie_ptr->next = calloc (sizeof (struct cie_entry), 1);
695 cie_ptr = cie_ptr->next;
699 cie_root = calloc (sizeof (struct cie_entry), 1);
703 cie_ptr->size = cie_size - 4;
704 cie_ptr->data = calloc (cie_ptr->size, 1);
705 cie_ptr->offset = last_cie_offset;
706 memcpy (cie_ptr->data, cie_buf + 4, cie_ptr->size);
712 /* Build the FDE... */
713 fde_buf = xcalloc (1024, 1);
717 /* Offset of this FDE in current fragment. */
718 fde_offset = frag_now_fix ();
722 printf ("# FDE: start=0x%lx, end=0x%lx, delta=%d\n",
723 (long) cfi_info->start_address,
724 (long) cfi_info->end_address,
725 (int) (cfi_info->end_address - cfi_info->start_address));
728 /* FDE length (t_long, 4 bytes) - will be set later. */
729 fde_len_offset = pbuf - fde_buf;
733 /* CIE pointer - offset from here. */
734 output_data (&pbuf, &buf_size, t_long, fde_offset - last_cie_offset + 4);
736 /* FDE initial location - this must be set relocatable! */
737 fde_initloc_offset = pbuf - fde_buf + fde_offset;
738 output_data (&pbuf, &buf_size, current_config.addr_length,
739 cfi_info->start_address);
741 /* FDE address range. */
742 output_data (&pbuf, &buf_size, current_config.addr_length,
743 cfi_info->end_address - cfi_info->start_address);
747 cfi_output_insn (data_ptr, &pbuf, &buf_size);
748 data_ptr = data_ptr->next;
751 fde_size = pbuf - fde_buf;
752 fde_size += current_config.eh_align - fde_size % current_config.eh_align;
754 /* Now we can set FDE length. */
755 pbuf = fde_buf + fde_len_offset;
757 output_data (&pbuf, &buf_size, t_long, fde_size - 4);
759 /* Copy FDE to objfile. */
760 where = (unsigned char *) frag_more (fde_size);
761 memcpy (where, fde_buf, fde_size);
763 /* Set relocation for initial address. */
764 buf_size = current_config.addr_length;
765 memset (&exp, 0, sizeof (exp));
767 exp.X_add_symbol = symbol_find (cfi_info->labelname);
768 fix_new_exp (frag_now, fde_initloc_offset,
769 current_config.addr_length,
770 &exp, 0, current_config.reloc_type);
778 /* Restore previous segment. */
779 subseg_set (saved_seg, 0);
790 dot_cfi_startproc ();
796 case CFA_def_cfa_register:
797 case CFA_def_cfa_offset:
800 case CFI_adjust_cfa_offset:
804 if (cfi_parse_const (¶m) >= 0)
805 verbose = (int) param;
810 as_bad (_("unknown CFI code 0x%x (%s)"), arg, cfi_insn_str (arg));
813 ignore_rest_of_line ();
817 cfi_set_config (struct cfi_config *cfg)
819 assert (cfg != NULL);
820 assert (cfg->addr_length > 0);
822 current_config = *cfg;
829 as_bad (_("open CFI at the end of file; missing .cfi_endproc directive"));