* dw2gencfi.c (struct cfi_escape_data): New.
[binutils-gdb.git] / gas / dw2gencfi.c
1 /* dw2gencfi.c - Support for generating Dwarf2 CFI information.
2 Copyright 2003 Free Software Foundation, Inc.
3 Contributed by Michal Ludvig <mludvig@suse.cz>
4
5 This file is part of GAS, the GNU Assembler.
6
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.
11
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.
16
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. */
21
22 #include "as.h"
23 #include "dw2gencfi.h"
24
25
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
31
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
43
44 #ifndef tc_cfi_frame_initial_instructions
45 # define tc_cfi_frame_initial_instructions() ((void)0)
46 #endif
47
48
49 struct cfi_insn_data
50 {
51 struct cfi_insn_data *next;
52 int insn;
53 union {
54 struct {
55 unsigned reg;
56 offsetT offset;
57 } ri;
58
59 struct {
60 unsigned reg1;
61 unsigned reg2;
62 } rr;
63
64 unsigned r;
65 offsetT i;
66
67 struct {
68 symbolS *lab1;
69 symbolS *lab2;
70 } ll;
71
72 struct cfi_escape_data {
73 struct cfi_escape_data *next;
74 expressionS exp;
75 } *esc;
76 } u;
77 };
78
79 struct fde_entry
80 {
81 struct fde_entry *next;
82 symbolS *start_address;
83 symbolS *end_address;
84 struct cfi_insn_data *data;
85 struct cfi_insn_data **last;
86 unsigned int return_column;
87 };
88
89 struct cie_entry
90 {
91 struct cie_entry *next;
92 symbolS *start_address;
93 unsigned int return_column;
94 struct cfi_insn_data *first, *last;
95 };
96
97
98 /* Current open FDE entry. */
99 static struct fde_entry *cur_fde_data;
100 static symbolS *last_address;
101 static offsetT cur_cfa_offset;
102
103 /* List of FDE entries. */
104 static struct fde_entry *all_fde_data;
105 static struct fde_entry **last_fde_data = &all_fde_data;
106
107 /* List of CIEs so that they could be reused. */
108 static struct cie_entry *cie_root;
109
110 /* Stack of old CFI data, for save/restore. */
111 struct cfa_save_data
112 {
113 struct cfa_save_data *next;
114 offsetT cfa_offset;
115 };
116
117 static struct cfa_save_data *cfa_save_stack;
118 \f
119 /* Construct a new FDE structure and add it to the end of the fde list. */
120
121 static struct fde_entry *
122 alloc_fde_entry (void)
123 {
124 struct fde_entry *fde = xcalloc (1, sizeof (struct fde_entry));
125
126 cur_fde_data = fde;
127 *last_fde_data = fde;
128 last_fde_data = &fde->next;
129
130 fde->last = &fde->data;
131 fde->return_column = DWARF2_DEFAULT_RETURN_COLUMN;
132
133 return fde;
134 }
135
136 /* The following functions are available for a backend to construct its
137 own unwind information, usually from legacy unwind directives. */
138
139 /* Construct a new INSN structure and add it to the end of the insn list
140 for the currently active FDE. */
141
142 static struct cfi_insn_data *
143 alloc_cfi_insn_data (void)
144 {
145 struct cfi_insn_data *insn = xcalloc (1, sizeof (struct cfi_insn_data));
146
147 *cur_fde_data->last = insn;
148 cur_fde_data->last = &insn->next;
149
150 return insn;
151 }
152
153 /* Construct a new FDE structure that begins at LABEL. */
154
155 void
156 cfi_new_fde (symbolS *label)
157 {
158 struct fde_entry *fde = alloc_fde_entry ();
159 fde->start_address = label;
160 last_address = label;
161 }
162
163 /* End the currently open FDE. */
164
165 void
166 cfi_end_fde (symbolS *label)
167 {
168 cur_fde_data->end_address = label;
169 cur_fde_data = NULL;
170 }
171
172 /* Set the return column for the current FDE. */
173
174 void
175 cfi_set_return_column (unsigned regno)
176 {
177 cur_fde_data->return_column = regno;
178 }
179
180 /* Universal functions to store new instructions. */
181
182 static void
183 cfi_add_CFA_insn(int insn)
184 {
185 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
186
187 insn_ptr->insn = insn;
188 }
189
190 static void
191 cfi_add_CFA_insn_reg (int insn, unsigned regno)
192 {
193 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
194
195 insn_ptr->insn = insn;
196 insn_ptr->u.r = regno;
197 }
198
199 static void
200 cfi_add_CFA_insn_offset (int insn, offsetT offset)
201 {
202 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
203
204 insn_ptr->insn = insn;
205 insn_ptr->u.i = offset;
206 }
207
208 static void
209 cfi_add_CFA_insn_reg_reg (int insn, unsigned reg1, unsigned reg2)
210 {
211 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
212
213 insn_ptr->insn = insn;
214 insn_ptr->u.rr.reg1 = reg1;
215 insn_ptr->u.rr.reg2 = reg2;
216 }
217
218 static void
219 cfi_add_CFA_insn_reg_offset (int insn, unsigned regno, offsetT offset)
220 {
221 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
222
223 insn_ptr->insn = insn;
224 insn_ptr->u.ri.reg = regno;
225 insn_ptr->u.ri.offset = offset;
226 }
227
228 /* Add a CFI insn to advance the PC from the last address to LABEL. */
229
230 void
231 cfi_add_advance_loc (symbolS *label)
232 {
233 struct cfi_insn_data *insn = alloc_cfi_insn_data ();
234
235 insn->insn = DW_CFA_advance_loc;
236 insn->u.ll.lab1 = last_address;
237 insn->u.ll.lab2 = label;
238
239 last_address = label;
240 }
241
242 /* Add a DW_CFA_offset record to the CFI data. */
243
244 void
245 cfi_add_CFA_offset (unsigned regno, offsetT offset)
246 {
247 unsigned int abs_data_align;
248
249 cfi_add_CFA_insn_reg_offset (DW_CFA_offset, regno, offset);
250
251 abs_data_align = (DWARF2_CIE_DATA_ALIGNMENT < 0
252 ? -DWARF2_CIE_DATA_ALIGNMENT : DWARF2_CIE_DATA_ALIGNMENT);
253 if (offset % abs_data_align)
254 as_bad (_("register save offset not a multiple of %u"), abs_data_align);
255 }
256
257 /* Add a DW_CFA_def_cfa record to the CFI data. */
258
259 void
260 cfi_add_CFA_def_cfa (unsigned regno, offsetT offset)
261 {
262 cfi_add_CFA_insn_reg_offset (DW_CFA_def_cfa, regno, offset);
263 cur_cfa_offset = offset;
264 }
265
266 /* Add a DW_CFA_register record to the CFI data. */
267
268 void
269 cfi_add_CFA_register (unsigned reg1, unsigned reg2)
270 {
271 cfi_add_CFA_insn_reg_reg (DW_CFA_register, reg1, reg2);
272 }
273
274 /* Add a DW_CFA_def_cfa_register record to the CFI data. */
275
276 void
277 cfi_add_CFA_def_cfa_register (unsigned regno)
278 {
279 cfi_add_CFA_insn_reg (DW_CFA_def_cfa_register, regno);
280 }
281
282 /* Add a DW_CFA_def_cfa_offset record to the CFI data. */
283
284 void
285 cfi_add_CFA_def_cfa_offset (offsetT offset)
286 {
287 cfi_add_CFA_insn_offset (DW_CFA_def_cfa_offset, offset);
288 cur_cfa_offset = offset;
289 }
290
291 void
292 cfi_add_CFA_restore (unsigned regno)
293 {
294 cfi_add_CFA_insn_reg (DW_CFA_restore, regno);
295 }
296
297 void
298 cfi_add_CFA_undefined (unsigned regno)
299 {
300 cfi_add_CFA_insn_reg (DW_CFA_undefined, regno);
301 }
302
303 void
304 cfi_add_CFA_same_value (unsigned regno)
305 {
306 cfi_add_CFA_insn_reg (DW_CFA_same_value, regno);
307 }
308
309 void
310 cfi_add_CFA_remember_state (void)
311 {
312 struct cfa_save_data *p;
313
314 cfi_add_CFA_insn (DW_CFA_remember_state);
315
316 p = xmalloc (sizeof (*p));
317 p->cfa_offset = cur_cfa_offset;
318 p->next = cfa_save_stack;
319 cfa_save_stack = p;
320 }
321
322 void
323 cfi_add_CFA_restore_state (void)
324 {
325 struct cfa_save_data *p;
326
327 cfi_add_CFA_insn (DW_CFA_restore_state);
328
329 p = cfa_save_stack;
330 if (p)
331 {
332 cur_cfa_offset = p->cfa_offset;
333 cfa_save_stack = p->next;
334 free (p);
335 }
336 }
337
338 \f
339 /* Parse CFI assembler directives. */
340
341 static void dot_cfi (int);
342 static void dot_cfi_escape (int);
343 static void dot_cfi_startproc (int);
344 static void dot_cfi_endproc (int);
345
346 /* Fake CFI type; outside the byte range of any real CFI insn. */
347 #define CFI_adjust_cfa_offset 0x100
348 #define CFI_return_column 0x101
349 #define CFI_rel_offset 0x102
350 #define CFI_escape 0x103
351
352 const pseudo_typeS cfi_pseudo_table[] =
353 {
354 { "cfi_startproc", dot_cfi_startproc, 0 },
355 { "cfi_endproc", dot_cfi_endproc, 0 },
356 { "cfi_def_cfa", dot_cfi, DW_CFA_def_cfa },
357 { "cfi_def_cfa_register", dot_cfi, DW_CFA_def_cfa_register },
358 { "cfi_def_cfa_offset", dot_cfi, DW_CFA_def_cfa_offset },
359 { "cfi_adjust_cfa_offset", dot_cfi, CFI_adjust_cfa_offset },
360 { "cfi_offset", dot_cfi, DW_CFA_offset },
361 { "cfi_rel_offset", dot_cfi, CFI_rel_offset },
362 { "cfi_register", dot_cfi, DW_CFA_register },
363 { "cfi_return_column", dot_cfi, CFI_return_column },
364 { "cfi_restore", dot_cfi, DW_CFA_restore },
365 { "cfi_undefined", dot_cfi, DW_CFA_undefined },
366 { "cfi_same_value", dot_cfi, DW_CFA_same_value },
367 { "cfi_remember_state", dot_cfi, DW_CFA_remember_state },
368 { "cfi_restore_state", dot_cfi, DW_CFA_restore_state },
369 { "cfi_escape", dot_cfi_escape, 0 },
370 { NULL, NULL, 0 }
371 };
372
373 static void
374 cfi_parse_separator (void)
375 {
376 SKIP_WHITESPACE ();
377 if (*input_line_pointer == ',')
378 input_line_pointer++;
379 else
380 as_bad (_("missing separator"));
381 }
382
383 static unsigned
384 cfi_parse_reg (void)
385 {
386 int regno;
387 expressionS exp;
388
389 #ifdef tc_regname_to_dw2regnum
390 SKIP_WHITESPACE ();
391 if (is_name_beginner (*input_line_pointer)
392 || (*input_line_pointer == '%'
393 && is_name_beginner (*++input_line_pointer)))
394 {
395 char *name, c;
396
397 name = input_line_pointer;
398 c = get_symbol_end ();
399
400 if ((regno = tc_regname_to_dw2regnum (name)) < 0)
401 {
402 as_bad (_("bad register expression"));
403 regno = 0;
404 }
405
406 *input_line_pointer = c;
407 return regno;
408 }
409 #endif
410
411 expression (&exp);
412 switch (exp.X_op)
413 {
414 case O_register:
415 case O_constant:
416 regno = exp.X_add_number;
417 break;
418
419 default:
420 as_bad (_("bad register expression"));
421 regno = 0;
422 break;
423 }
424
425 return regno;
426 }
427
428 static offsetT
429 cfi_parse_const (void)
430 {
431 return get_absolute_expression ();
432 }
433
434 static void
435 dot_cfi (int arg)
436 {
437 offsetT offset;
438 unsigned reg1, reg2;
439
440 if (!cur_fde_data)
441 {
442 as_bad (_("CFI instruction used without previous .cfi_startproc"));
443 return;
444 }
445
446 /* If the last address was not at the current PC, advance to current. */
447 if (symbol_get_frag (last_address) != frag_now
448 || S_GET_VALUE (last_address) != frag_now_fix ())
449 cfi_add_advance_loc (symbol_temp_new_now ());
450
451 switch (arg)
452 {
453 case DW_CFA_offset:
454 reg1 = cfi_parse_reg ();
455 cfi_parse_separator ();
456 offset = cfi_parse_const ();
457 cfi_add_CFA_offset (reg1, offset);
458 break;
459
460 case CFI_rel_offset:
461 reg1 = cfi_parse_reg ();
462 cfi_parse_separator ();
463 offset = cfi_parse_const ();
464 cfi_add_CFA_offset (reg1, offset - cur_cfa_offset);
465 break;
466
467 case DW_CFA_def_cfa:
468 reg1 = cfi_parse_reg ();
469 cfi_parse_separator ();
470 offset = cfi_parse_const ();
471 cfi_add_CFA_def_cfa (reg1, offset);
472 break;
473
474 case DW_CFA_register:
475 reg1 = cfi_parse_reg ();
476 cfi_parse_separator ();
477 reg2 = cfi_parse_reg ();
478 cfi_add_CFA_register (reg1, reg2);
479 break;
480
481 case DW_CFA_def_cfa_register:
482 reg1 = cfi_parse_reg ();
483 cfi_add_CFA_def_cfa_register (reg1);
484 break;
485
486 case DW_CFA_def_cfa_offset:
487 offset = cfi_parse_const ();
488 cfi_add_CFA_def_cfa_offset (offset);
489 break;
490
491 case CFI_adjust_cfa_offset:
492 offset = cfi_parse_const ();
493 cfi_add_CFA_def_cfa_offset (cur_cfa_offset + offset);
494 break;
495
496 case DW_CFA_restore:
497 reg1 = cfi_parse_reg ();
498 cfi_add_CFA_restore (reg1);
499 break;
500
501 case DW_CFA_undefined:
502 reg1 = cfi_parse_reg ();
503 cfi_add_CFA_undefined (reg1);
504 break;
505
506 case DW_CFA_same_value:
507 reg1 = cfi_parse_reg ();
508 cfi_add_CFA_same_value (reg1);
509 break;
510
511 case CFI_return_column:
512 reg1 = cfi_parse_reg ();
513 cfi_set_return_column (reg1);
514 break;
515
516 case DW_CFA_remember_state:
517 cfi_add_CFA_remember_state ();
518 break;
519
520 case DW_CFA_restore_state:
521 cfi_add_CFA_restore_state ();
522 break;
523
524 default:
525 abort ();
526 }
527
528 demand_empty_rest_of_line ();
529 }
530
531 static void
532 dot_cfi_escape (int ignored ATTRIBUTE_UNUSED)
533 {
534 struct cfi_escape_data *head, **tail, *e;
535 struct cfi_insn_data *insn;
536
537 if (!cur_fde_data)
538 {
539 as_bad (_("CFI instruction used without previous .cfi_startproc"));
540 return;
541 }
542
543 /* If the last address was not at the current PC, advance to current. */
544 if (symbol_get_frag (last_address) != frag_now
545 || S_GET_VALUE (last_address) != frag_now_fix ())
546 cfi_add_advance_loc (symbol_temp_new_now ());
547
548 tail = &head;
549 do
550 {
551 e = xmalloc (sizeof (*e));
552 do_parse_cons_expression (&e->exp, 1);
553 *tail = e;
554 tail = &e->next;
555 }
556 while (*input_line_pointer++ == ',');
557 *tail = NULL;
558
559 insn = alloc_cfi_insn_data ();
560 insn->insn = CFI_escape;
561 insn->u.esc = head;
562 }
563
564 static void
565 dot_cfi_startproc (int ignored ATTRIBUTE_UNUSED)
566 {
567 int simple = 0;
568
569 if (cur_fde_data)
570 {
571 as_bad (_("previous CFI entry not closed (missing .cfi_endproc)"));
572 return;
573 }
574
575 cfi_new_fde (symbol_temp_new_now ());
576
577 SKIP_WHITESPACE ();
578 if (is_name_beginner (*input_line_pointer))
579 {
580 char *name, c;
581
582 name = input_line_pointer;
583 c = get_symbol_end ();
584
585 if (strcmp (name, "simple") == 0)
586 {
587 simple = 1;
588 *input_line_pointer = c;
589 }
590 else
591 input_line_pointer = name;
592 }
593 demand_empty_rest_of_line ();
594
595 if (!simple)
596 tc_cfi_frame_initial_instructions ();
597 }
598
599 static void
600 dot_cfi_endproc (int ignored ATTRIBUTE_UNUSED)
601 {
602 if (! cur_fde_data)
603 {
604 as_bad (_(".cfi_endproc without corresponding .cfi_startproc"));
605 return;
606 }
607
608 cfi_end_fde (symbol_temp_new_now ());
609 }
610
611 \f
612 /* Emit a single byte into the current segment. */
613
614 static inline void
615 out_one (int byte)
616 {
617 FRAG_APPEND_1_CHAR (byte);
618 }
619
620 /* Emit a two-byte word into the current segment. */
621
622 static inline void
623 out_two (int data)
624 {
625 md_number_to_chars (frag_more (2), data, 2);
626 }
627
628 /* Emit a four byte word into the current segment. */
629
630 static inline void
631 out_four (int data)
632 {
633 md_number_to_chars (frag_more (4), data, 4);
634 }
635
636 /* Emit an unsigned "little-endian base 128" number. */
637
638 static void
639 out_uleb128 (addressT value)
640 {
641 output_leb128 (frag_more (sizeof_leb128 (value, 0)), value, 0);
642 }
643
644 /* Emit an unsigned "little-endian base 128" number. */
645
646 static void
647 out_sleb128 (offsetT value)
648 {
649 output_leb128 (frag_more (sizeof_leb128 (value, 1)), value, 1);
650 }
651
652 static void
653 output_cfi_insn (struct cfi_insn_data *insn)
654 {
655 offsetT offset;
656 unsigned int regno;
657
658 switch (insn->insn)
659 {
660 case DW_CFA_advance_loc:
661 {
662 symbolS *from = insn->u.ll.lab1;
663 symbolS *to = insn->u.ll.lab2;
664
665 if (symbol_get_frag (to) == symbol_get_frag (from))
666 {
667 addressT delta = S_GET_VALUE (to) - S_GET_VALUE (from);
668 addressT scaled = delta / DWARF2_LINE_MIN_INSN_LENGTH;
669
670 if (scaled <= 0x3F)
671 out_one (DW_CFA_advance_loc + scaled);
672 else if (delta <= 0xFF)
673 {
674 out_one (DW_CFA_advance_loc1);
675 out_one (delta);
676 }
677 else if (delta <= 0xFFFF)
678 {
679 out_one (DW_CFA_advance_loc2);
680 out_two (delta);
681 }
682 else
683 {
684 out_one (DW_CFA_advance_loc4);
685 out_four (delta);
686 }
687 }
688 else
689 {
690 expressionS exp;
691
692 exp.X_op = O_subtract;
693 exp.X_add_symbol = to;
694 exp.X_op_symbol = from;
695 exp.X_add_number = 0;
696
697 /* The code in ehopt.c expects that one byte of the encoding
698 is already allocated to the frag. This comes from the way
699 that it scans the .eh_frame section looking first for the
700 .byte DW_CFA_advance_loc4. */
701 frag_more (1);
702
703 frag_var (rs_cfa, 4, 0, DWARF2_LINE_MIN_INSN_LENGTH << 3,
704 make_expr_symbol (&exp), frag_now_fix () - 1,
705 (char *) frag_now);
706 }
707 }
708 break;
709
710 case DW_CFA_def_cfa:
711 offset = insn->u.ri.offset;
712 if (offset < 0)
713 {
714 out_one (DW_CFA_def_cfa_sf);
715 out_uleb128 (insn->u.ri.reg);
716 out_uleb128 (offset);
717 }
718 else
719 {
720 out_one (DW_CFA_def_cfa);
721 out_uleb128 (insn->u.ri.reg);
722 out_uleb128 (offset);
723 }
724 break;
725
726 case DW_CFA_def_cfa_register:
727 case DW_CFA_undefined:
728 case DW_CFA_same_value:
729 out_one (insn->insn);
730 out_uleb128 (insn->u.r);
731 break;
732
733 case DW_CFA_def_cfa_offset:
734 offset = insn->u.i;
735 if (offset < 0)
736 {
737 out_one (DW_CFA_def_cfa_offset_sf);
738 out_sleb128 (offset);
739 }
740 else
741 {
742 out_one (DW_CFA_def_cfa_offset);
743 out_uleb128 (offset);
744 }
745 break;
746
747 case DW_CFA_restore:
748 regno = insn->u.r;
749 if (regno <= 0x3F)
750 {
751 out_one (DW_CFA_restore + regno);
752 }
753 else
754 {
755 out_one (DW_CFA_restore_extended);
756 out_uleb128 (regno);
757 }
758 break;
759
760 case DW_CFA_offset:
761 regno = insn->u.ri.reg;
762 offset = insn->u.ri.offset / DWARF2_CIE_DATA_ALIGNMENT;
763 if (offset < 0)
764 {
765 out_one (DW_CFA_offset_extended_sf);
766 out_uleb128 (regno);
767 out_sleb128 (offset);
768 }
769 else if (regno <= 0x3F)
770 {
771 out_one (DW_CFA_offset + regno);
772 out_uleb128 (offset);
773 }
774 else
775 {
776 out_one (DW_CFA_offset_extended);
777 out_uleb128 (regno);
778 out_uleb128 (offset);
779 }
780 break;
781
782 case DW_CFA_register:
783 out_one (DW_CFA_register);
784 out_uleb128 (insn->u.rr.reg1);
785 out_uleb128 (insn->u.rr.reg2);
786 break;
787
788 case DW_CFA_remember_state:
789 case DW_CFA_restore_state:
790 out_one (insn->insn);
791 break;
792
793 case CFI_escape:
794 {
795 struct cfi_escape_data *e;
796 for (e = insn->u.esc; e ; e = e->next)
797 emit_expr (&e->exp, 1);
798 break;
799 }
800
801 default:
802 abort ();
803 }
804 }
805
806 static void
807 output_cie (struct cie_entry *cie)
808 {
809 symbolS *after_size_address, *end_address;
810 expressionS exp;
811 struct cfi_insn_data *i;
812
813 cie->start_address = symbol_temp_new_now ();
814 after_size_address = symbol_temp_make ();
815 end_address = symbol_temp_make ();
816
817 exp.X_op = O_subtract;
818 exp.X_add_symbol = end_address;
819 exp.X_op_symbol = after_size_address;
820 exp.X_add_number = 0;
821
822 emit_expr (&exp, 4); /* Length */
823 symbol_set_value_now (after_size_address);
824 out_four (0); /* CIE id */
825 out_one (DW_CIE_VERSION); /* Version */
826 out_one ('z'); /* Augmentation */
827 out_one ('R');
828 out_one (0);
829 out_uleb128 (DWARF2_LINE_MIN_INSN_LENGTH); /* Code alignment */
830 out_sleb128 (DWARF2_CIE_DATA_ALIGNMENT); /* Data alignment */
831 out_one (cie->return_column); /* Return column */
832 out_uleb128 (1); /* Augmentation size */
833 out_one (DW_EH_PE_pcrel | DW_EH_PE_sdata4);
834
835 if (cie->first)
836 for (i = cie->first; i != cie->last; i = i->next)
837 output_cfi_insn (i);
838
839 frag_align (2, 0, 0);
840 symbol_set_value_now (end_address);
841 }
842
843 static void
844 output_fde (struct fde_entry *fde, struct cie_entry *cie,
845 struct cfi_insn_data *first)
846 {
847 symbolS *after_size_address, *end_address;
848 expressionS exp;
849
850 after_size_address = symbol_temp_make ();
851 end_address = symbol_temp_make ();
852
853 exp.X_op = O_subtract;
854 exp.X_add_symbol = end_address;
855 exp.X_op_symbol = after_size_address;
856 exp.X_add_number = 0;
857 emit_expr (&exp, 4); /* Length */
858 symbol_set_value_now (after_size_address);
859
860 exp.X_add_symbol = after_size_address;
861 exp.X_op_symbol = cie->start_address;
862 emit_expr (&exp, 4); /* CIE offset */
863
864 exp.X_add_symbol = fde->start_address;
865 exp.X_op_symbol = symbol_temp_new_now ();
866 emit_expr (&exp, 4); /* Code offset */
867
868 exp.X_add_symbol = fde->end_address;
869 exp.X_op_symbol = fde->start_address; /* Code length */
870 emit_expr (&exp, 4);
871
872 out_uleb128 (0); /* Augmentation size */
873
874 for (; first; first = first->next)
875 output_cfi_insn (first);
876
877 frag_align (2, 0, 0);
878 symbol_set_value_now (end_address);
879 }
880
881 static struct cie_entry *
882 select_cie_for_fde (struct fde_entry *fde, struct cfi_insn_data **pfirst)
883 {
884 struct cfi_insn_data *i, *j;
885 struct cie_entry *cie;
886
887 for (cie = cie_root; cie; cie = cie->next)
888 {
889 if (cie->return_column != fde->return_column)
890 continue;
891 for (i = cie->first, j = fde->data;
892 i != cie->last && j != NULL;
893 i = i->next, j = j->next)
894 {
895 if (i->insn != j->insn)
896 goto fail;
897 switch (i->insn)
898 {
899 case DW_CFA_advance_loc:
900 /* We reached the first advance in the FDE, but did not
901 reach the end of the CIE list. */
902 goto fail;
903
904 case DW_CFA_offset:
905 case DW_CFA_def_cfa:
906 if (i->u.ri.reg != j->u.ri.reg)
907 goto fail;
908 if (i->u.ri.offset != j->u.ri.offset)
909 goto fail;
910 break;
911
912 case DW_CFA_register:
913 if (i->u.rr.reg1 != j->u.rr.reg1)
914 goto fail;
915 if (i->u.rr.reg2 != j->u.rr.reg2)
916 goto fail;
917 break;
918
919 case DW_CFA_def_cfa_register:
920 case DW_CFA_restore:
921 case DW_CFA_undefined:
922 case DW_CFA_same_value:
923 if (i->u.r != j->u.r)
924 goto fail;
925 break;
926
927 case DW_CFA_def_cfa_offset:
928 if (i->u.i != j->u.i)
929 goto fail;
930 break;
931
932 case CFI_escape:
933 /* Don't bother matching these for now. */
934 goto fail;
935
936 default:
937 abort ();
938 }
939 }
940
941 /* Success if we reached the end of the CIE list, and we've either
942 run out of FDE entries or we've encountered an advance. */
943 if (i == cie->last && (!j || j->insn == DW_CFA_advance_loc))
944 {
945 *pfirst = j;
946 return cie;
947 }
948
949 fail:;
950 }
951
952 cie = xmalloc (sizeof (struct cie_entry));
953 cie->next = cie_root;
954 cie_root = cie;
955 cie->return_column = fde->return_column;
956 cie->first = fde->data;
957
958 for (i = cie->first; i ; i = i->next)
959 if (i->insn == DW_CFA_advance_loc)
960 break;
961
962 cie->last = i;
963 *pfirst = i;
964
965 output_cie (cie);
966
967 return cie;
968 }
969
970 void
971 cfi_finish (void)
972 {
973 segT cfi_seg;
974 struct fde_entry *fde;
975 int save_flag_traditional_format;
976
977 if (cur_fde_data)
978 {
979 as_bad (_("open CFI at the end of file; missing .cfi_endproc directive"));
980 cur_fde_data->end_address = cur_fde_data->start_address;
981 }
982
983 if (all_fde_data == 0)
984 return;
985
986 /* Open .eh_frame section. */
987 cfi_seg = subseg_new (".eh_frame", 0);
988 #ifdef BFD_ASSEMBLER
989 bfd_set_section_flags (stdoutput, cfi_seg,
990 SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_READONLY);
991 #endif
992 subseg_set (cfi_seg, 0);
993 record_alignment (cfi_seg, 2);
994
995 /* Make sure check_eh_frame doesn't do anything with our output. */
996 save_flag_traditional_format = flag_traditional_format;
997 flag_traditional_format = 1;
998
999 for (fde = all_fde_data; fde ; fde = fde->next)
1000 {
1001 struct cfi_insn_data *first;
1002 struct cie_entry *cie;
1003
1004 cie = select_cie_for_fde (fde, &first);
1005 output_fde (fde, cie, first);
1006 }
1007
1008 flag_traditional_format = save_flag_traditional_format;
1009 }