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"
26 /* Current target config. */
27 static struct cfi_config current_config;
29 /* This is the main entry point to the CFI machinery. */
30 static void dot_cfi (int arg);
32 const pseudo_typeS cfi_pseudo_table[] =
34 { "cfi_verbose", dot_cfi, CFI_verbose },
35 { "cfi_startproc", dot_cfi, CFI_startproc },
36 { "cfi_endproc", dot_cfi, CFI_endproc },
37 { "cfi_def_cfa", dot_cfi, CFA_def_cfa },
38 { "cfi_def_cfa_register", dot_cfi, CFA_def_cfa_register },
39 { "cfi_def_cfa_offset", dot_cfi, CFA_def_cfa_offset },
40 { "cfi_adjust_cfa_offset", dot_cfi, CFI_adjust_cfa_offset },
41 { "cfi_offset", dot_cfi, CFA_offset },
46 cfi_insn_str (enum cfi_insn insn)
54 case CFA_advance_loc1:
55 return "CFA_advance_loc1";
56 case CFA_advance_loc2:
57 return "CFA_advance_loc2";
58 case CFA_advance_loc4:
59 return "CFA_advance_loc4";
60 case CFA_offset_extended:
61 return "CFA_offset_extended";
62 case CFA_resotre_extended:
63 return "CFA_resotre_extended";
65 return "CFA_undefined";
67 return "CFA_same_value";
69 return "CFA_register";
70 case CFA_remember_state:
71 return "CFA_remember_state";
72 case CFA_restore_state:
73 return "CFA_restore_state";
76 case CFA_def_cfa_register:
77 return "CFA_def_cfa_register";
78 case CFA_def_cfa_offset:
79 return "CFA_def_cfa_offset";
81 return "CFA_advance_loc";
97 struct cfi_data *next;
102 addressT start_address;
103 addressT end_address;
104 addressT last_address;
105 const char *labelname;
106 struct cfi_data *data;
107 struct cfi_info *next;
110 static struct cfi_info *cfi_info;
112 static struct cfi_data *
113 alloc_cfi_data (void)
115 return (struct cfi_data *) xcalloc (sizeof (struct cfi_info), 1);
118 static struct cfi_info *
119 alloc_cfi_info (void)
121 return (struct cfi_info *) xcalloc (sizeof (struct cfi_info), 1);
124 /* Parse arguments. */
126 cfi_parse_arg (long *param, int resolvereg)
133 assert (param != NULL);
136 if (sscanf (input_line_pointer, "%li%n", &value, &nchars) >= 1)
138 input_line_pointer += nchars;
141 else if (resolvereg && (is_name_beginner (*input_line_pointer)))
143 name = input_line_pointer;
144 c = get_symbol_end ();
145 p = input_line_pointer;
147 if ((value = tc_regname_to_dw2regnum (name)) >= 0)
154 _("can't convert argument to a register number") :
155 _("can't convert argument to an integer"));
161 if (*input_line_pointer == ',')
163 input_line_pointer++;
171 cfi_parse_reg (long *param)
173 return cfi_parse_arg (param, 1);
177 cfi_parse_const (long *param)
179 return cfi_parse_arg (param, 0);
183 cfi_add_insn (enum cfi_insn insn, long param0, long param1)
185 struct cfi_data *data_ptr;
189 cfi_info->data = alloc_cfi_data ();
190 data_ptr = cfi_info->data;
194 data_ptr = cfi_info->data;
196 while (data_ptr && data_ptr->next)
197 data_ptr = data_ptr->next;
199 data_ptr->next = alloc_cfi_data ();
201 data_ptr = data_ptr->next;
204 data_ptr->insn = insn;
205 data_ptr->param[0] = param0;
206 data_ptr->param[1] = param1;
210 cfi_advance_loc (void)
212 addressT curr_address = frag_now_fix ();
213 if (cfi_info->last_address == curr_address)
215 cfi_add_insn (CFA_advance_loc,
216 (long) (curr_address - cfi_info->last_address), 0);
217 cfi_info->last_address = curr_address;
221 get_current_offset (struct cfi_info *info)
223 long current_offset = 0;
224 struct cfi_data *data = info->data;
229 if (data->insn == CFA_def_cfa)
230 current_offset = data->param[1];
231 else if (data->insn == CFA_def_cfa_offset)
232 current_offset = data->param[0];
236 return current_offset;
240 cfi_make_insn (int arg)
242 long param[2] = { 0, 0 };
246 as_bad (_("CFI instruction used without previous .cfi_startproc"));
254 /* Instructions that take two arguments (register, integer). */
257 if (cfi_parse_reg (¶m[0]) < 0)
259 as_bad (_("first argument to %s is not a register"),
263 if (cfi_parse_const (¶m[1]) < 0)
265 as_bad (_("second argument to %s is not a number"),
271 /* Instructions that take one register argument. */
272 case CFA_def_cfa_register:
273 if (cfi_parse_reg (¶m[0]) < 0)
275 as_bad (_("argument to %s is not a register"), cfi_insn_str (arg));
280 /* Instructions that take one integer argument. */
281 case CFA_def_cfa_offset:
282 if (cfi_parse_const (¶m[0]) < 0)
284 as_bad (_("argument to %s is not a number"), cfi_insn_str (arg));
289 /* Special handling for pseudo-instruction. */
290 case CFI_adjust_cfa_offset:
291 if (cfi_parse_const (¶m[0]) < 0)
293 as_bad (_("argument to %s is not a number"),
294 ".cfi_adjust_cfa_offset");
297 param[0] += get_current_offset (cfi_info);
298 arg = CFA_def_cfa_offset;
302 as_bad (_("unknown CFI instruction %d (%s)"), arg, cfi_insn_str (arg));
305 cfi_add_insn (arg, param[0], param[1]);
311 char symname[40], *symbase=".Llbl_cfi";
315 snprintf (symname, sizeof (symname), "%s_0x%lx",
316 symbase, (long) frag_now_fix ());
317 while ((symbolP = symbol_find (symname)))
319 if ((S_GET_VALUE (symbolP) == frag_now_fix ())
320 && (S_GET_SEGMENT (symbolP) == now_seg))
324 snprintf (symname, sizeof (symname), "%s_0x%lx_%u",
325 symbase, (long) frag_now_fix (), i++);
327 symbolP = (symbolS *) local_symbol_make (symname, now_seg,
328 (valueT) frag_now_fix (),
334 dot_cfi_startproc (void)
338 as_bad (_("previous CFI entry not closed (missing .cfi_endproc)"));
342 cfi_info = alloc_cfi_info ();
344 cfi_info->start_address = frag_now_fix ();
345 cfi_info->last_address = cfi_info->start_address;
346 cfi_info->labelname = S_GET_NAME (cfi_get_label ());
348 #ifdef tc_cfi_frame_initial_instructions
349 tc_cfi_frame_initial_instructions ();
353 #define cfi_is_advance_insn(insn) \
354 ((insn >= CFA_set_loc && insn <= CFA_advance_loc4) \
355 || insn == CFA_advance_loc)
368 /* Output CFI instructions to the file. */
371 output_data (char **p, unsigned long *size, enum data_types type, long value)
374 unsigned int ret_size;
393 as_warn (_("unknown type %d"), type);
397 if (*size < ret_size)
399 as_bad (_("output_data buffer is too small"));
408 printf ("\t.byte\t0x%x\n", (unsigned char) *ptr);
411 *(short *) ptr = (short) value & 0xFFFF;
413 printf ("\t.half\t0x%x\n", (unsigned short) *ptr);
416 *(int *) ptr = (int) value & 0xFFFFFFFF;
418 printf ("\t.long\t0x%x\n", (unsigned int) *ptr);
421 *(long long *) ptr = (long long) value & 0xFFFFFFFF;
423 printf ("\t.quad\t0x%x\n", (unsigned int) *ptr);
427 ret_size = output_leb128 (ptr, value, type == t_sleb128);
429 printf ("\t.%s\t0x%lx\n",
430 type == t_sleb128 ? "sleb128" : "uleb128",
434 as_warn ("unknown type %d", type);
445 cfi_output_insn (struct cfi_data *data, char **buf, unsigned long *buf_size)
447 char **pbuf = buf, *orig_buf = *buf;
451 as_fatal (_("cfi_output_insn called with NULL pointer"));
455 case CFA_advance_loc:
457 printf ("\t# %s(%ld)\n", cfi_insn_str (data->insn),
459 if (data->param[0] <= 0x3F)
461 output_data (pbuf, buf_size, t_byte, CFA_advance_loc +
462 (data->param[0] / current_config.code_align));
464 else if (data->param[0] <= 0xFF)
466 output_data (pbuf, buf_size, t_byte, CFA_advance_loc1);
467 output_data (pbuf, buf_size, t_byte,
468 data->param[0] / current_config.code_align);
470 else if (data->param[0] <= 0xFFFF)
472 output_data (pbuf, buf_size, t_byte, CFA_advance_loc2);
473 output_data (pbuf, buf_size, t_half,
474 data->param[0] / current_config.code_align);
478 output_data (pbuf, buf_size, t_byte, CFA_advance_loc4);
479 output_data (pbuf, buf_size, t_long,
480 data->param[0] / current_config.code_align);
486 printf ("\t# CFA_def_cfa(%ld,%ld)\n", data->param[0], data->param[1]);
487 output_data (pbuf, buf_size, t_byte, CFA_def_cfa);
488 output_data (pbuf, buf_size, t_uleb128, data->param[0]);
489 output_data (pbuf, buf_size, t_uleb128, data->param[1]);
492 case CFA_def_cfa_register:
493 case CFA_def_cfa_offset:
495 printf ("\t# %s(%ld)\n", cfi_insn_str (data->insn),
497 output_data (pbuf, buf_size, t_byte, data->insn);
498 output_data (pbuf, buf_size, t_uleb128, data->param[0]);
503 printf ("\t# %s(%ld,%ld)\n", cfi_insn_str (data->insn),
504 data->param[0], data->param[1]);
506 /* Check whether to use CFA_offset or CFA_offset_extended. */
507 if (data->param[0] <= 0x3F)
508 output_data (pbuf, buf_size, t_byte, CFA_offset + data->param[0]);
511 output_data (pbuf, buf_size, t_byte, CFA_offset_extended);
512 output_data (pbuf, buf_size, t_uleb128, data->param[0]);
514 output_data (pbuf, buf_size, t_uleb128,
515 data->param[1] / current_config.data_align);
520 printf ("\t# CFA_nop\n");
521 output_data (pbuf, buf_size, t_byte, CFA_nop);
525 as_warn ("CFA_unknown[%d](%ld,%ld)", data->insn,
526 data->param[0], data->param[1]);
528 size = *pbuf - orig_buf;
535 dot_cfi_endproc (void)
537 struct cfi_data *data_ptr;
538 char *cie_buf, *fde_buf, *pbuf, *where;
539 unsigned long buf_size, cie_size, fde_size, last_cie_offset;
540 unsigned long fde_initloc_offset, fde_len_offset;
541 void *saved_seg, *cfi_seg;
545 as_bad (_(".cfi_endproc without corresponding .cfi_startproc"));
548 cfi_info->end_address = frag_now_fix ();
550 /* Open .eh_frame section. */
552 cfi_seg = subseg_new (".eh_frame", 0);
553 bfd_set_section_flags (stdoutput, cfi_seg,
554 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
555 subseg_set (cfi_seg, 0);
558 cie_buf = xcalloc (1024, 1);
559 /* Skip space for CIE length. */
564 printf ("# CIE *****\n");
567 output_data (&pbuf, &buf_size, t_long, 0x0);
569 output_data (&pbuf, &buf_size, t_byte, 1);
571 output_data (&pbuf, &buf_size, t_byte, 0);
572 /* Code alignment. */
573 output_data (&pbuf, &buf_size, t_uleb128, current_config.code_align);
574 /* Data alignment. */
575 output_data (&pbuf, &buf_size, t_sleb128, current_config.data_align);
576 /* Return address column. */
577 output_data (&pbuf, &buf_size, t_byte, current_config.ra_column);
579 /* Build CFI instructions. */
580 data_ptr = cfi_info->data;
581 while (data_ptr && !cfi_is_advance_insn (data_ptr->insn))
583 cfi_output_insn (data_ptr, &pbuf, &buf_size);
584 data_ptr = data_ptr->next;
587 /* Align the whole data to current_config.eh_align. */
588 cie_size = pbuf - cie_buf;
589 cie_size += current_config.eh_align - cie_size % current_config.eh_align;
593 output_data (&pbuf, &buf_size, t_long, cie_size - 4);
595 /* OK, we built the CIE. Let's write it to the file... */
596 last_cie_offset = frag_now_fix ();
597 where = (unsigned char *) frag_more (cie_size);
598 memcpy (where, cie_buf, cie_size);
603 /* Build the FDE... */
604 fde_buf = xcalloc (1024, 1);
610 printf ("# FDE: start=0x%lx, end=0x%lx, delta=%d\n",
611 (long) cfi_info->start_address,
612 (long) cfi_info->end_address,
613 (int) (cfi_info->end_address - cfi_info->start_address));
616 /* FDE length (t_long, 4 bytes) - will be set later. */
617 fde_len_offset = pbuf - fde_buf;
621 /* CIE pointer - offset from here. */
622 output_data (&pbuf, &buf_size, t_long, cie_size + 4);
624 /* FDE initial location - this must be set relocatable! */
625 fde_initloc_offset = pbuf - fde_buf;
626 output_data (&pbuf, &buf_size, current_config.addr_length,
627 cfi_info->start_address);
629 /* FDE address range. */
630 output_data (&pbuf, &buf_size, current_config.addr_length,
631 cfi_info->end_address - cfi_info->start_address);
635 cfi_output_insn (data_ptr, &pbuf, &buf_size);
636 data_ptr = data_ptr->next;
639 fde_size = pbuf - fde_buf;
640 fde_size += current_config.eh_align - fde_size % current_config.eh_align;
642 /* Now we can set FDE length. */
643 pbuf = fde_buf + fde_len_offset;
645 output_data (&pbuf, &buf_size, t_long, fde_size - 4);
647 /* Adjust initloc offset. */
648 fde_initloc_offset += frag_now_fix ();
650 /* Copy FDE to objfile. */
651 where = (unsigned char *) frag_more (fde_size);
652 memcpy (where, fde_buf, fde_size);
654 /* Set relocation for initial address. */
655 buf_size = current_config.addr_length;
657 memset (&exp, 0, sizeof (exp));
659 exp.X_add_symbol = symbol_find (cfi_info->labelname);
660 fix_new_exp (frag_now, fde_initloc_offset,
661 current_config.addr_length,
662 &exp, 0, current_config.reloc_type);
670 /* Restore previous segment. */
671 subseg_set (saved_seg, 0);
682 dot_cfi_startproc ();
688 case CFA_def_cfa_register:
689 case CFA_def_cfa_offset:
691 case CFI_adjust_cfa_offset:
695 if (cfi_parse_const (¶m) >= 0)
696 verbose = (int) param;
701 as_bad (_("unknown CFI code 0x%x (%s)"), arg, cfi_insn_str (arg));
704 ignore_rest_of_line ();
708 cfi_set_config (struct cfi_config *cfg)
710 assert (cfg != NULL);
711 assert (cfg->addr_length > 0);
713 current_config = *cfg;
720 as_bad (_("open CFI at the end of file; missing .cfi_endproc directive"));