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