* dw2gencfi.c (cfi_finish): Set .eh_frame read-only.
[binutils.git] / gas / dw2gencfi.c
blob76408a436e117a742c5d73627674f42e7b86d49a
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)
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, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 #include "as.h"
23 #include "dw2gencfi.h"
26 /* We re-use DWARF2_LINE_MIN_INSN_LENGTH for the code alignment field
27 of the CIE. Default to 1 if not otherwise specified. */
28 #ifndef DWARF2_LINE_MIN_INSN_LENGTH
29 # define DWARF2_LINE_MIN_INSN_LENGTH 1
30 #endif
32 /* If TARGET_USE_CFIPOP is defined, it is required that the target
33 provide the following definitions. Otherwise provide them to
34 allow compilation to continue. */
35 #ifndef TARGET_USE_CFIPOP
36 # ifndef DWARF2_DEFAULT_RETURN_COLUMN
37 # define DWARF2_DEFAULT_RETURN_COLUMN 0
38 # endif
39 # ifndef DWARF2_CIE_DATA_ALIGNMENT
40 # define DWARF2_CIE_DATA_ALIGNMENT 1
41 # endif
42 #endif
44 #ifndef tc_cfi_frame_initial_instructions
45 # define tc_cfi_frame_initial_instructions() ((void)0)
46 #endif
49 struct cfi_insn_data
51 struct cfi_insn_data *next;
52 int insn;
53 union {
54 struct {
55 unsigned reg;
56 offsetT offset;
57 } ri;
59 struct {
60 unsigned reg1;
61 unsigned reg2;
62 } rr;
64 unsigned r;
65 offsetT i;
67 struct {
68 symbolS *lab1;
69 symbolS *lab2;
70 } ll;
71 } u;
74 struct fde_entry
76 struct fde_entry *next;
77 symbolS *start_address;
78 symbolS *end_address;
79 struct cfi_insn_data *data;
80 struct cfi_insn_data **last;
81 unsigned int return_column;
84 struct cie_entry
86 struct cie_entry *next;
87 symbolS *start_address;
88 unsigned int return_column;
89 struct cfi_insn_data *first, *last;
93 /* Current open FDE entry. */
94 static struct fde_entry *cur_fde_data;
95 static symbolS *last_address;
96 static offsetT cur_cfa_offset;
98 /* List of FDE entries. */
99 static struct fde_entry *all_fde_data;
100 static struct fde_entry **last_fde_data = &all_fde_data;
102 /* List of CIEs so that they could be reused. */
103 static struct cie_entry *cie_root;
106 /* Construct a new FDE structure and add it to the end of the fde list. */
108 static struct fde_entry *
109 alloc_fde_entry (void)
111 struct fde_entry *fde = xcalloc (1, sizeof (struct fde_entry));
113 cur_fde_data = fde;
114 *last_fde_data = fde;
115 last_fde_data = &fde->next;
117 fde->last = &fde->data;
118 fde->return_column = DWARF2_DEFAULT_RETURN_COLUMN;
120 return fde;
123 /* The following functions are available for a backend to construct its
124 own unwind information, usually from legacy unwind directives. */
126 /* Construct a new INSN structure and add it to the end of the insn list
127 for the currently active FDE. */
129 static struct cfi_insn_data *
130 alloc_cfi_insn_data (void)
132 struct cfi_insn_data *insn = xcalloc (1, sizeof (struct cfi_insn_data));
134 *cur_fde_data->last = insn;
135 cur_fde_data->last = &insn->next;
137 return insn;
140 /* Construct a new FDE structure that begins at LABEL. */
142 void
143 cfi_new_fde (symbolS *label)
145 struct fde_entry *fde = alloc_fde_entry ();
146 fde->start_address = label;
147 last_address = label;
150 /* End the currently open FDE. */
152 void
153 cfi_end_fde (symbolS *label)
155 cur_fde_data->end_address = label;
156 cur_fde_data = NULL;
159 /* Set the return column for the current FDE. */
161 void
162 cfi_set_return_column (unsigned regno)
164 cur_fde_data->return_column = regno;
167 /* Add a CFI insn to advance the PC from the last address to LABEL. */
169 void
170 cfi_add_advance_loc (symbolS *label)
172 struct cfi_insn_data *insn = alloc_cfi_insn_data ();
174 insn->insn = DW_CFA_advance_loc;
175 insn->u.ll.lab1 = last_address;
176 insn->u.ll.lab2 = label;
178 last_address = label;
181 /* Add a DW_CFA_offset record to the CFI data. */
183 void
184 cfi_add_CFA_offset (unsigned regno, offsetT offset)
186 struct cfi_insn_data *insn = alloc_cfi_insn_data ();
188 insn->insn = DW_CFA_offset;
189 insn->u.ri.reg = regno;
190 insn->u.ri.offset = offset;
193 /* Add a DW_CFA_def_cfa record to the CFI data. */
195 void
196 cfi_add_CFA_def_cfa (unsigned regno, offsetT offset)
198 struct cfi_insn_data *insn = alloc_cfi_insn_data ();
200 insn->insn = DW_CFA_def_cfa;
201 insn->u.ri.reg = regno;
202 insn->u.ri.offset = offset;
204 cur_cfa_offset = offset;
207 /* Add a DW_CFA_register record to the CFI data. */
209 void
210 cfi_add_CFA_register (unsigned reg1, unsigned reg2)
212 struct cfi_insn_data *insn = alloc_cfi_insn_data ();
214 insn->insn = DW_CFA_register;
215 insn->u.rr.reg1 = reg1;
216 insn->u.rr.reg2 = reg2;
219 /* Add a DW_CFA_def_cfa_register record to the CFI data. */
221 void
222 cfi_add_CFA_def_cfa_register (unsigned regno)
224 struct cfi_insn_data *insn = alloc_cfi_insn_data ();
226 insn->insn = DW_CFA_def_cfa_register;
227 insn->u.r = regno;
230 /* Add a DW_CFA_def_cfa_offset record to the CFI data. */
232 void
233 cfi_add_CFA_def_cfa_offset (offsetT offset)
235 struct cfi_insn_data *insn = alloc_cfi_insn_data ();
237 insn->insn = DW_CFA_def_cfa_offset;
238 insn->u.i = offset;
240 cur_cfa_offset = offset;
244 /* Parse CFI assembler directives. */
246 static void dot_cfi (int);
247 static void dot_cfi_startproc (int);
248 static void dot_cfi_endproc (int);
250 /* Fake CFI type; outside the byte range of any real CFI insn. */
251 #define CFI_adjust_cfa_offset 0x100
253 const pseudo_typeS cfi_pseudo_table[] =
255 { "cfi_startproc", dot_cfi_startproc, 0 },
256 { "cfi_endproc", dot_cfi_endproc, 0 },
257 { "cfi_def_cfa", dot_cfi, DW_CFA_def_cfa },
258 { "cfi_def_cfa_register", dot_cfi, DW_CFA_def_cfa_register },
259 { "cfi_def_cfa_offset", dot_cfi, DW_CFA_def_cfa_offset },
260 { "cfi_adjust_cfa_offset", dot_cfi, CFI_adjust_cfa_offset },
261 { "cfi_offset", dot_cfi, DW_CFA_offset },
262 { "cfi_register", dot_cfi, DW_CFA_register },
263 { NULL, NULL, 0 }
266 static void
267 cfi_parse_separator (void)
269 SKIP_WHITESPACE ();
270 if (*input_line_pointer == ',')
271 input_line_pointer++;
272 else
273 as_bad (_("missing separator"));
276 static unsigned
277 cfi_parse_reg (void)
279 int regno;
280 expressionS exp;
282 #ifdef tc_regname_to_dw2regnum
283 SKIP_WHITESPACE ();
284 if (is_name_beginner (*input_line_pointer)
285 || (*input_line_pointer == '%'
286 && is_name_beginner (*++input_line_pointer)))
288 char *name, c;
290 name = input_line_pointer;
291 c = get_symbol_end ();
293 if ((regno = tc_regname_to_dw2regnum (name)) < 0)
295 as_bad (_("bad register expression"));
296 regno = 0;
299 *input_line_pointer = c;
300 return regno;
302 #endif
304 expression (&exp);
305 switch (exp.X_op)
307 case O_register:
308 case O_constant:
309 regno = exp.X_add_number;
310 break;
312 default:
313 as_bad (_("bad register expression"));
314 regno = 0;
315 break;
318 return regno;
321 static offsetT
322 cfi_parse_const (void)
324 return get_absolute_expression ();
327 static void
328 dot_cfi (int arg)
330 offsetT offset;
331 unsigned reg1, reg2;
333 if (!cur_fde_data)
335 as_bad (_("CFI instruction used without previous .cfi_startproc"));
336 return;
339 /* If the last address was not at the current PC, advance to current. */
340 if (symbol_get_frag (last_address) != frag_now
341 || S_GET_VALUE (last_address) != frag_now_fix ())
342 cfi_add_advance_loc (symbol_temp_new_now ());
344 switch (arg)
346 /* Instructions that take two arguments (register, integer). */
347 case DW_CFA_offset:
348 case DW_CFA_def_cfa:
349 reg1 = cfi_parse_reg ();
350 cfi_parse_separator ();
351 offset = cfi_parse_const ();
353 if (arg == DW_CFA_def_cfa)
354 cfi_add_CFA_def_cfa (reg1, offset);
355 else
356 cfi_add_CFA_offset (reg1, offset);
357 break;
359 /* Instructions that take two arguments (register, register). */
360 case DW_CFA_register:
361 reg1 = cfi_parse_reg ();
362 cfi_parse_separator ();
363 reg2 = cfi_parse_reg ();
365 cfi_add_CFA_register (reg1, reg2);
366 break;
368 /* Instructions that take one register argument. */
369 case DW_CFA_def_cfa_register:
370 reg1 = cfi_parse_reg ();
371 cfi_add_CFA_def_cfa_register (reg1);
372 break;
374 /* Instructions that take one integer argument. */
375 case DW_CFA_def_cfa_offset:
376 offset = cfi_parse_const ();
377 cfi_add_CFA_def_cfa_offset (offset);
378 break;
380 /* Special handling for pseudo-instruction. */
381 case CFI_adjust_cfa_offset:
382 offset = cfi_parse_const ();
383 cfi_add_CFA_def_cfa_offset (cur_cfa_offset + offset);
384 break;
386 default:
387 abort ();
390 demand_empty_rest_of_line ();
393 static void
394 dot_cfi_startproc (int ignored ATTRIBUTE_UNUSED)
396 int simple = 0;
398 if (cur_fde_data)
400 as_bad (_("previous CFI entry not closed (missing .cfi_endproc)"));
401 return;
404 cfi_new_fde (symbol_temp_new_now ());
406 SKIP_WHITESPACE ();
407 if (is_name_beginner (*input_line_pointer))
409 char *name, c;
411 name = input_line_pointer;
412 c = get_symbol_end ();
414 if (strcmp (name, "simple") == 0)
416 simple = 1;
417 *input_line_pointer = c;
419 else
420 input_line_pointer = name;
422 demand_empty_rest_of_line ();
424 if (!simple)
425 tc_cfi_frame_initial_instructions ();
428 static void
429 dot_cfi_endproc (int ignored ATTRIBUTE_UNUSED)
431 if (! cur_fde_data)
433 as_bad (_(".cfi_endproc without corresponding .cfi_startproc"));
434 return;
437 cfi_end_fde (symbol_temp_new_now ());
441 /* Emit a single byte into the current segment. */
443 static inline void
444 out_one (int byte)
446 FRAG_APPEND_1_CHAR (byte);
449 /* Emit a two-byte word into the current segment. */
451 static inline void
452 out_two (int data)
454 md_number_to_chars (frag_more (2), data, 2);
457 /* Emit a four byte word into the current segment. */
459 static inline void
460 out_four (int data)
462 md_number_to_chars (frag_more (4), data, 4);
465 /* Emit an unsigned "little-endian base 128" number. */
467 static void
468 out_uleb128 (addressT value)
470 output_leb128 (frag_more (sizeof_leb128 (value, 0)), value, 0);
473 /* Emit an unsigned "little-endian base 128" number. */
475 static void
476 out_sleb128 (offsetT value)
478 output_leb128 (frag_more (sizeof_leb128 (value, 1)), value, 1);
481 static void
482 output_cfi_insn (struct cfi_insn_data *insn)
484 offsetT offset;
485 unsigned int regno;
487 switch (insn->insn)
489 case DW_CFA_advance_loc:
491 symbolS *from = insn->u.ll.lab1;
492 symbolS *to = insn->u.ll.lab2;
494 if (symbol_get_frag (to) == symbol_get_frag (from))
496 addressT delta = S_GET_VALUE (to) - S_GET_VALUE (from);
497 addressT scaled = delta / DWARF2_LINE_MIN_INSN_LENGTH;
499 if (scaled <= 0x3F)
500 out_one (DW_CFA_advance_loc + scaled);
501 else if (delta <= 0xFF)
503 out_one (DW_CFA_advance_loc1);
504 out_one (delta);
506 else if (delta <= 0xFFFF)
508 out_one (DW_CFA_advance_loc2);
509 out_two (delta);
511 else
513 out_one (DW_CFA_advance_loc4);
514 out_four (delta);
517 else
519 expressionS exp;
521 exp.X_op = O_subtract;
522 exp.X_add_symbol = to;
523 exp.X_op_symbol = from;
524 exp.X_add_number = 0;
526 /* The code in ehopt.c expects that one byte of the encoding
527 is already allocated to the frag. This comes from the way
528 that it scans the .eh_frame section looking first for the
529 .byte DW_CFA_advance_loc4. */
530 frag_more (1);
532 frag_var (rs_cfa, 4, 0, DWARF2_LINE_MIN_INSN_LENGTH << 3,
533 make_expr_symbol (&exp), frag_now_fix () - 1,
534 (char *) frag_now);
537 break;
539 case DW_CFA_def_cfa:
540 offset = insn->u.ri.offset;
541 if (offset < 0)
543 out_one (DW_CFA_def_cfa_sf);
544 out_uleb128 (insn->u.ri.reg);
545 out_uleb128 (offset);
547 else
549 out_one (DW_CFA_def_cfa);
550 out_uleb128 (insn->u.ri.reg);
551 out_uleb128 (offset);
553 break;
555 case DW_CFA_def_cfa_register:
556 out_one (DW_CFA_def_cfa_register);
557 out_uleb128 (insn->u.i);
558 break;
560 case DW_CFA_def_cfa_offset:
561 offset = insn->u.i;
562 if (offset < 0)
564 out_one (DW_CFA_def_cfa_offset_sf);
565 out_sleb128 (offset);
567 else
569 out_one (DW_CFA_def_cfa_offset);
570 out_uleb128 (offset);
572 break;
574 case DW_CFA_offset:
575 regno = insn->u.ri.reg;
576 offset = insn->u.ri.offset / DWARF2_CIE_DATA_ALIGNMENT;
577 if (offset < 0)
579 out_one (DW_CFA_offset_extended);
580 out_uleb128 (regno);
581 out_sleb128 (offset);
583 else if (regno <= 0x3F)
585 out_one (DW_CFA_offset + regno);
586 out_uleb128 (offset);
588 else
590 out_one (DW_CFA_offset_extended);
591 out_uleb128 (regno);
592 out_uleb128 (offset);
594 break;
596 case DW_CFA_register:
597 out_one (DW_CFA_register);
598 out_uleb128 (insn->u.rr.reg1);
599 out_uleb128 (insn->u.rr.reg2);
600 break;
602 case DW_CFA_nop:
603 out_one (DW_CFA_nop);
604 break;
606 default:
607 abort ();
611 static void
612 output_cie (struct cie_entry *cie)
614 symbolS *after_size_address, *end_address;
615 expressionS exp;
616 struct cfi_insn_data *i;
618 cie->start_address = symbol_temp_new_now ();
619 after_size_address = symbol_temp_make ();
620 end_address = symbol_temp_make ();
622 exp.X_op = O_subtract;
623 exp.X_add_symbol = end_address;
624 exp.X_op_symbol = after_size_address;
625 exp.X_add_number = 0;
627 emit_expr (&exp, 4); /* Length */
628 symbol_set_value_now (after_size_address);
629 out_four (0); /* CIE id */
630 out_one (DW_CIE_VERSION); /* Version */
631 out_one ('z'); /* Augmentation */
632 out_one ('R');
633 out_one (0);
634 out_uleb128 (DWARF2_LINE_MIN_INSN_LENGTH); /* Code alignment */
635 out_sleb128 (DWARF2_CIE_DATA_ALIGNMENT); /* Data alignment */
636 out_one (cie->return_column); /* Return column */
637 out_uleb128 (1); /* Augmentation size */
638 out_one (DW_EH_PE_pcrel | DW_EH_PE_sdata4);
640 if (cie->first)
641 for (i = cie->first; i != cie->last; i = i->next)
642 output_cfi_insn (i);
644 frag_align (2, 0, 0);
645 symbol_set_value_now (end_address);
648 static void
649 output_fde (struct fde_entry *fde, struct cie_entry *cie,
650 struct cfi_insn_data *first)
652 symbolS *after_size_address, *end_address;
653 expressionS exp;
655 after_size_address = symbol_temp_make ();
656 end_address = symbol_temp_make ();
658 exp.X_op = O_subtract;
659 exp.X_add_symbol = end_address;
660 exp.X_op_symbol = after_size_address;
661 exp.X_add_number = 0;
662 emit_expr (&exp, 4); /* Length */
663 symbol_set_value_now (after_size_address);
665 exp.X_add_symbol = after_size_address;
666 exp.X_op_symbol = cie->start_address;
667 emit_expr (&exp, 4); /* CIE offset */
669 exp.X_add_symbol = fde->start_address;
670 exp.X_op_symbol = symbol_temp_new_now ();
671 emit_expr (&exp, 4); /* Code offset */
673 exp.X_add_symbol = fde->end_address;
674 exp.X_op_symbol = fde->start_address; /* Code length */
675 emit_expr (&exp, 4);
677 out_uleb128 (0); /* Augmentation size */
679 for (; first; first = first->next)
680 output_cfi_insn (first);
682 frag_align (2, 0, 0);
683 symbol_set_value_now (end_address);
686 static struct cie_entry *
687 select_cie_for_fde (struct fde_entry *fde, struct cfi_insn_data **pfirst)
689 struct cfi_insn_data *i, *j;
690 struct cie_entry *cie;
692 for (cie = cie_root; cie; cie = cie->next)
694 if (cie->return_column != fde->return_column)
695 continue;
696 for (i = cie->first, j = fde->data;
697 i != cie->last && j != NULL;
698 i = i->next, j = j->next)
700 if (i->insn != j->insn)
701 goto fail;
702 switch (i->insn)
704 case DW_CFA_advance_loc:
705 /* We reached the first advance in the FDE, but did not
706 reach the end of the CIE list. */
707 goto fail;
709 case DW_CFA_offset:
710 case DW_CFA_def_cfa:
711 if (i->u.ri.reg != j->u.ri.reg)
712 goto fail;
713 if (i->u.ri.offset != j->u.ri.offset)
714 goto fail;
715 break;
717 case DW_CFA_register:
718 if (i->u.rr.reg1 != j->u.rr.reg1)
719 goto fail;
720 if (i->u.rr.reg2 != j->u.rr.reg2)
721 goto fail;
722 break;
724 case DW_CFA_def_cfa_register:
725 if (i->u.r != j->u.r)
726 goto fail;
727 break;
729 case DW_CFA_def_cfa_offset:
730 if (i->u.i != j->u.i)
731 goto fail;
732 break;
734 default:
735 abort ();
739 /* Success if we reached the end of the CIE list, and we've either
740 run out of FDE entries or we've encountered an advance. */
741 if (i == cie->last && (!j || j->insn == DW_CFA_advance_loc))
743 *pfirst = j;
744 return cie;
747 fail:;
750 cie = xmalloc (sizeof (struct cie_entry));
751 cie->next = cie_root;
752 cie_root = cie;
753 cie->return_column = fde->return_column;
754 cie->first = fde->data;
756 for (i = cie->first; i ; i = i->next)
757 if (i->insn == DW_CFA_advance_loc)
758 break;
760 cie->last = i;
761 *pfirst = i;
763 output_cie (cie);
765 return cie;
768 void
769 cfi_finish (void)
771 segT cfi_seg;
772 struct fde_entry *fde;
773 int save_flag_traditional_format;
775 if (cur_fde_data)
777 as_bad (_("open CFI at the end of file; missing .cfi_endproc directive"));
778 cur_fde_data->end_address = cur_fde_data->start_address;
781 if (all_fde_data == 0)
782 return;
784 /* Open .eh_frame section. */
785 cfi_seg = subseg_new (".eh_frame", 0);
786 #ifdef BFD_ASSEMBLER
787 bfd_set_section_flags (stdoutput, cfi_seg,
788 SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_READONLY);
789 #endif
790 subseg_set (cfi_seg, 0);
791 record_alignment (cfi_seg, 2);
793 /* Make sure check_eh_frame doesn't do anything with our output. */
794 save_flag_traditional_format = flag_traditional_format;
795 flag_traditional_format = 1;
797 for (fde = all_fde_data; fde ; fde = fde->next)
799 struct cfi_insn_data *first;
800 struct cie_entry *cie;
802 cie = select_cie_for_fde (fde, &first);
803 output_fde (fde, cie, first);
806 flag_traditional_format = save_flag_traditional_format;