cb633293aab5c6508ea6b344c22ab038d74d7caf
[deliverable/binutils-gdb.git] / gas / dw2gencfi.c
1 /* dw2gencfi.c - Support for generating Dwarf2 CFI information.
2 Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
4 Contributed by Michal Ludvig <mludvig@suse.cz>
5
6 This file is part of GAS, the GNU Assembler.
7
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
21 02110-1301, USA. */
22
23 #include "as.h"
24 #include "dw2gencfi.h"
25 #include "subsegs.h"
26 #include "dwarf2dbg.h"
27
28 #ifdef TARGET_USE_CFIPOP
29
30 /* By default, use difference expressions if DIFF_EXPR_OK is defined. */
31 #ifndef CFI_DIFF_EXPR_OK
32 # ifdef DIFF_EXPR_OK
33 # define CFI_DIFF_EXPR_OK 1
34 # else
35 # define CFI_DIFF_EXPR_OK 0
36 # endif
37 #endif
38
39 #ifndef CFI_DIFF_LSDA_OK
40 #define CFI_DIFF_LSDA_OK CFI_DIFF_EXPR_OK
41 #endif
42
43 #if CFI_DIFF_EXPR_OK == 1 && CFI_DIFF_LSDA_OK == 0
44 # error "CFI_DIFF_EXPR_OK should imply CFI_DIFF_LSDA_OK"
45 #endif
46
47 /* We re-use DWARF2_LINE_MIN_INSN_LENGTH for the code alignment field
48 of the CIE. Default to 1 if not otherwise specified. */
49 #ifndef DWARF2_LINE_MIN_INSN_LENGTH
50 #define DWARF2_LINE_MIN_INSN_LENGTH 1
51 #endif
52
53 /* By default, use 32-bit relocations from .eh_frame into .text. */
54 #ifndef DWARF2_FDE_RELOC_SIZE
55 #define DWARF2_FDE_RELOC_SIZE 4
56 #endif
57
58 /* By default, use a read-only .eh_frame section. */
59 #ifndef DWARF2_EH_FRAME_READ_ONLY
60 #define DWARF2_EH_FRAME_READ_ONLY SEC_READONLY
61 #endif
62
63 #ifndef EH_FRAME_ALIGNMENT
64 #define EH_FRAME_ALIGNMENT (bfd_get_arch_size (stdoutput) == 64 ? 3 : 2)
65 #endif
66
67 #ifndef tc_cfi_frame_initial_instructions
68 #define tc_cfi_frame_initial_instructions() ((void)0)
69 #endif
70
71 #ifndef DWARF2_FORMAT
72 #define DWARF2_FORMAT(SEC) dwarf2_format_32bit
73 #endif
74
75 #ifndef DWARF2_ADDR_SIZE
76 #define DWARF2_ADDR_SIZE(bfd) (bfd_arch_bits_per_address (bfd) / 8)
77 #endif
78
79 #if defined (TE_PE) || defined (TE_PEP)
80 #define SUPPORT_FRAME_LINKONCE 1
81 #else
82 #define SUPPORT_FRAME_LINKONCE 0
83 #endif
84
85 #if SUPPORT_FRAME_LINKONCE
86 /* Private segment collection list. */
87 struct dwcfi_seg_list
88 {
89 segT seg;
90 int subseg;
91 char * seg_name;
92 };
93
94 #define FRAME_NAME ".eh_frame"
95
96 static struct hash_control *dwcfi_hash;
97
98 /* Build based on segment the derived .debug_...
99 segment name containing origin segment's postfix name part. */
100
101 static char *
102 get_debugseg_name (segT seg, const char *base_name)
103 {
104 const char *name;
105
106 if (!seg)
107 name = "";
108 else
109 {
110 const char * dollar;
111 const char * dot;
112
113 name = bfd_get_section_name (stdoutput, seg);
114
115 dollar = strchr (name, '$');
116 dot = strchr (name + 1, '.');
117
118 if (!dollar && !dot)
119 name = "";
120 else if (!dollar)
121 name = dot;
122 else if (!dot)
123 name = dollar;
124 else if (dot < dollar)
125 name = dot;
126 else
127 name = dollar;
128 }
129
130 return concat (base_name, name, NULL);
131 }
132
133 /* Allocate a dwcfi_seg_list structure. */
134
135 static struct dwcfi_seg_list *
136 alloc_debugseg_item (segT seg, int subseg, char *name)
137 {
138 struct dwcfi_seg_list *r;
139
140 r = (struct dwcfi_seg_list *)
141 xmalloc (sizeof (struct dwcfi_seg_list) + strlen (name));
142 r->seg = seg;
143 r->subseg = subseg;
144 r->seg_name = name;
145 return r;
146 }
147
148 static segT
149 is_now_linkonce_segment (void)
150 {
151 if ((bfd_get_section_flags (stdoutput, now_seg)
152 & (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD
153 | SEC_LINK_DUPLICATES_ONE_ONLY | SEC_LINK_DUPLICATES_SAME_SIZE
154 | SEC_LINK_DUPLICATES_SAME_CONTENTS)) != 0)
155 return now_seg;
156 return NULL;
157 }
158
159 /* Generate debug... segment with same linkonce properties
160 of based segment. */
161
162 static segT
163 make_debug_seg (segT cseg, char *name, int sflags)
164 {
165 segT save_seg = now_seg;
166 int save_subseg = now_subseg;
167 segT r;
168 flagword flags;
169
170 r = subseg_new (name, 0);
171
172 /* Check if code segment is marked as linked once. */
173 if (!cseg)
174 flags = 0;
175 else
176 flags = bfd_get_section_flags (stdoutput, cseg)
177 & (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD
178 | SEC_LINK_DUPLICATES_ONE_ONLY | SEC_LINK_DUPLICATES_SAME_SIZE
179 | SEC_LINK_DUPLICATES_SAME_CONTENTS);
180
181 /* Add standard section flags. */
182 flags |= sflags;
183
184 /* Apply possibly linked once flags to new generated segment, too. */
185 if (!bfd_set_section_flags (stdoutput, r, flags))
186 as_bad (_("bfd_set_section_flags: %s"),
187 bfd_errmsg (bfd_get_error ()));
188
189 /* Restore to previous segment. */
190 if (save_seg != NULL)
191 subseg_set (save_seg, save_subseg);
192 return r;
193 }
194
195 static void
196 dwcfi_hash_insert (const char *name, struct dwcfi_seg_list *item)
197 {
198 const char *error_string;
199
200 if ((error_string = hash_jam (dwcfi_hash, name, (char *) item)))
201 as_fatal (_("Inserting \"%s\" into structure table failed: %s"),
202 name, error_string);
203 }
204
205 static struct dwcfi_seg_list *
206 dwcfi_hash_find (char *name)
207 {
208 return (struct dwcfi_seg_list *) hash_find (dwcfi_hash, name);
209 }
210
211 static struct dwcfi_seg_list *
212 dwcfi_hash_find_or_make (segT cseg, const char *base_name, int flags)
213 {
214 struct dwcfi_seg_list *item;
215 char *name;
216
217 /* Initialize dwcfi_hash once. */
218 if (!dwcfi_hash)
219 dwcfi_hash = hash_new ();
220
221 name = get_debugseg_name (cseg, base_name);
222
223 item = dwcfi_hash_find (name);
224 if (!item)
225 {
226 item = alloc_debugseg_item (make_debug_seg (cseg, name, flags), 0, name);
227
228 dwcfi_hash_insert (item->seg_name, item);
229 }
230 else
231 free (name);
232
233 return item;
234 }
235 #endif
236
237 /* ??? Share this with dwarf2cfg.c. */
238 #ifndef TC_DWARF2_EMIT_OFFSET
239 #define TC_DWARF2_EMIT_OFFSET generic_dwarf2_emit_offset
240
241 /* Create an offset to .dwarf2_*. */
242
243 static void
244 generic_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
245 {
246 expressionS exp;
247
248 exp.X_op = O_symbol;
249 exp.X_add_symbol = symbol;
250 exp.X_add_number = 0;
251 emit_expr (&exp, size);
252 }
253 #endif
254
255 struct cfi_escape_data
256 {
257 struct cfi_escape_data *next;
258 expressionS exp;
259 };
260
261 struct cfi_insn_data
262 {
263 struct cfi_insn_data *next;
264 #if SUPPORT_FRAME_LINKONCE
265 segT cur_seg;
266 #endif
267 int insn;
268 union
269 {
270 struct
271 {
272 unsigned reg;
273 offsetT offset;
274 } ri;
275
276 struct
277 {
278 unsigned reg1;
279 unsigned reg2;
280 } rr;
281
282 unsigned r;
283 offsetT i;
284
285 struct
286 {
287 symbolS *lab1;
288 symbolS *lab2;
289 } ll;
290
291 struct cfi_escape_data *esc;
292
293 struct
294 {
295 unsigned reg, encoding;
296 expressionS exp;
297 } ea;
298 } u;
299 };
300
301 struct fde_entry
302 {
303 struct fde_entry *next;
304 #if SUPPORT_FRAME_LINKONCE
305 segT cseg;
306 #endif
307 symbolS *start_address;
308 symbolS *end_address;
309 struct cfi_insn_data *data;
310 struct cfi_insn_data **last;
311 unsigned char per_encoding;
312 unsigned char lsda_encoding;
313 expressionS personality;
314 expressionS lsda;
315 unsigned int return_column;
316 unsigned int signal_frame;
317 #if SUPPORT_FRAME_LINKONCE
318 int handled;
319 #endif
320 };
321
322 struct cie_entry
323 {
324 struct cie_entry *next;
325 #if SUPPORT_FRAME_LINKONCE
326 segT cur_seg;
327 #endif
328 symbolS *start_address;
329 unsigned int return_column;
330 unsigned int signal_frame;
331 unsigned char per_encoding;
332 unsigned char lsda_encoding;
333 expressionS personality;
334 struct cfi_insn_data *first, *last;
335 };
336
337 /* List of FDE entries. */
338
339 static struct fde_entry *all_fde_data;
340 static struct fde_entry **last_fde_data = &all_fde_data;
341
342 /* List of CIEs so that they could be reused. */
343 static struct cie_entry *cie_root;
344
345 /* Stack of old CFI data, for save/restore. */
346 struct cfa_save_data
347 {
348 struct cfa_save_data *next;
349 offsetT cfa_offset;
350 };
351
352 /* Current open FDE entry. */
353 struct frch_cfi_data
354 {
355 struct fde_entry *cur_fde_data;
356 symbolS *last_address;
357 offsetT cur_cfa_offset;
358 struct cfa_save_data *cfa_save_stack;
359 };
360 \f
361 /* Construct a new FDE structure and add it to the end of the fde list. */
362
363 static struct fde_entry *
364 alloc_fde_entry (void)
365 {
366 struct fde_entry *fde = (struct fde_entry *)
367 xcalloc (1, sizeof (struct fde_entry));
368
369 frchain_now->frch_cfi_data = (struct frch_cfi_data *)
370 xcalloc (1, sizeof (struct frch_cfi_data));
371 frchain_now->frch_cfi_data->cur_fde_data = fde;
372 *last_fde_data = fde;
373 last_fde_data = &fde->next;
374 #if SUPPORT_FRAME_LINKONCE
375 fde->cseg = is_now_linkonce_segment ();
376 fde->handled = 0;
377 #endif
378 fde->last = &fde->data;
379 fde->return_column = DWARF2_DEFAULT_RETURN_COLUMN;
380 fde->per_encoding = DW_EH_PE_omit;
381 fde->lsda_encoding = DW_EH_PE_omit;
382
383 return fde;
384 }
385
386 /* The following functions are available for a backend to construct its
387 own unwind information, usually from legacy unwind directives. */
388
389 /* Construct a new INSN structure and add it to the end of the insn list
390 for the currently active FDE. */
391
392 static struct cfi_insn_data *
393 alloc_cfi_insn_data (void)
394 {
395 struct cfi_insn_data *insn = (struct cfi_insn_data *)
396 xcalloc (1, sizeof (struct cfi_insn_data));
397 struct fde_entry *cur_fde_data = frchain_now->frch_cfi_data->cur_fde_data;
398
399 *cur_fde_data->last = insn;
400 cur_fde_data->last = &insn->next;
401 #if SUPPORT_FRAME_LINKONCE
402 insn->cur_seg = is_now_linkonce_segment ();
403 #endif
404 return insn;
405 }
406
407 /* Construct a new FDE structure that begins at LABEL. */
408
409 void
410 cfi_new_fde (symbolS *label)
411 {
412 struct fde_entry *fde = alloc_fde_entry ();
413 fde->start_address = label;
414 frchain_now->frch_cfi_data->last_address = label;
415 }
416
417 /* End the currently open FDE. */
418
419 void
420 cfi_end_fde (symbolS *label)
421 {
422 frchain_now->frch_cfi_data->cur_fde_data->end_address = label;
423 free (frchain_now->frch_cfi_data);
424 frchain_now->frch_cfi_data = NULL;
425 }
426
427 /* Set the return column for the current FDE. */
428
429 void
430 cfi_set_return_column (unsigned regno)
431 {
432 frchain_now->frch_cfi_data->cur_fde_data->return_column = regno;
433 }
434
435 /* Universal functions to store new instructions. */
436
437 static void
438 cfi_add_CFA_insn (int insn)
439 {
440 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
441
442 insn_ptr->insn = insn;
443 }
444
445 static void
446 cfi_add_CFA_insn_reg (int insn, unsigned regno)
447 {
448 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
449
450 insn_ptr->insn = insn;
451 insn_ptr->u.r = regno;
452 }
453
454 static void
455 cfi_add_CFA_insn_offset (int insn, offsetT offset)
456 {
457 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
458
459 insn_ptr->insn = insn;
460 insn_ptr->u.i = offset;
461 }
462
463 static void
464 cfi_add_CFA_insn_reg_reg (int insn, unsigned reg1, unsigned reg2)
465 {
466 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
467
468 insn_ptr->insn = insn;
469 insn_ptr->u.rr.reg1 = reg1;
470 insn_ptr->u.rr.reg2 = reg2;
471 }
472
473 static void
474 cfi_add_CFA_insn_reg_offset (int insn, unsigned regno, offsetT offset)
475 {
476 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
477
478 insn_ptr->insn = insn;
479 insn_ptr->u.ri.reg = regno;
480 insn_ptr->u.ri.offset = offset;
481 }
482
483 /* Add a CFI insn to advance the PC from the last address to LABEL. */
484
485 void
486 cfi_add_advance_loc (symbolS *label)
487 {
488 struct cfi_insn_data *insn = alloc_cfi_insn_data ();
489
490 insn->insn = DW_CFA_advance_loc;
491 insn->u.ll.lab1 = frchain_now->frch_cfi_data->last_address;
492 insn->u.ll.lab2 = label;
493
494 frchain_now->frch_cfi_data->last_address = label;
495 }
496
497 /* Add a DW_CFA_offset record to the CFI data. */
498
499 void
500 cfi_add_CFA_offset (unsigned regno, offsetT offset)
501 {
502 unsigned int abs_data_align;
503
504 gas_assert (DWARF2_CIE_DATA_ALIGNMENT != 0);
505 cfi_add_CFA_insn_reg_offset (DW_CFA_offset, regno, offset);
506
507 abs_data_align = (DWARF2_CIE_DATA_ALIGNMENT < 0
508 ? -DWARF2_CIE_DATA_ALIGNMENT : DWARF2_CIE_DATA_ALIGNMENT);
509 if (offset % abs_data_align)
510 as_bad (_("register save offset not a multiple of %u"), abs_data_align);
511 }
512
513 /* Add a DW_CFA_def_cfa record to the CFI data. */
514
515 void
516 cfi_add_CFA_def_cfa (unsigned regno, offsetT offset)
517 {
518 cfi_add_CFA_insn_reg_offset (DW_CFA_def_cfa, regno, offset);
519 frchain_now->frch_cfi_data->cur_cfa_offset = offset;
520 }
521
522 /* Add a DW_CFA_register record to the CFI data. */
523
524 void
525 cfi_add_CFA_register (unsigned reg1, unsigned reg2)
526 {
527 cfi_add_CFA_insn_reg_reg (DW_CFA_register, reg1, reg2);
528 }
529
530 /* Add a DW_CFA_def_cfa_register record to the CFI data. */
531
532 void
533 cfi_add_CFA_def_cfa_register (unsigned regno)
534 {
535 cfi_add_CFA_insn_reg (DW_CFA_def_cfa_register, regno);
536 }
537
538 /* Add a DW_CFA_def_cfa_offset record to the CFI data. */
539
540 void
541 cfi_add_CFA_def_cfa_offset (offsetT offset)
542 {
543 cfi_add_CFA_insn_offset (DW_CFA_def_cfa_offset, offset);
544 frchain_now->frch_cfi_data->cur_cfa_offset = offset;
545 }
546
547 void
548 cfi_add_CFA_restore (unsigned regno)
549 {
550 cfi_add_CFA_insn_reg (DW_CFA_restore, regno);
551 }
552
553 void
554 cfi_add_CFA_undefined (unsigned regno)
555 {
556 cfi_add_CFA_insn_reg (DW_CFA_undefined, regno);
557 }
558
559 void
560 cfi_add_CFA_same_value (unsigned regno)
561 {
562 cfi_add_CFA_insn_reg (DW_CFA_same_value, regno);
563 }
564
565 void
566 cfi_add_CFA_remember_state (void)
567 {
568 struct cfa_save_data *p;
569
570 cfi_add_CFA_insn (DW_CFA_remember_state);
571
572 p = (struct cfa_save_data *) xmalloc (sizeof (*p));
573 p->cfa_offset = frchain_now->frch_cfi_data->cur_cfa_offset;
574 p->next = frchain_now->frch_cfi_data->cfa_save_stack;
575 frchain_now->frch_cfi_data->cfa_save_stack = p;
576 }
577
578 void
579 cfi_add_CFA_restore_state (void)
580 {
581 struct cfa_save_data *p;
582
583 cfi_add_CFA_insn (DW_CFA_restore_state);
584
585 p = frchain_now->frch_cfi_data->cfa_save_stack;
586 if (p)
587 {
588 frchain_now->frch_cfi_data->cur_cfa_offset = p->cfa_offset;
589 frchain_now->frch_cfi_data->cfa_save_stack = p->next;
590 free (p);
591 }
592 else
593 as_bad (_("CFI state restore without previous remember"));
594 }
595
596 \f
597 /* Parse CFI assembler directives. */
598
599 static void dot_cfi (int);
600 static void dot_cfi_escape (int);
601 static void dot_cfi_sections (int);
602 static void dot_cfi_startproc (int);
603 static void dot_cfi_endproc (int);
604 static void dot_cfi_personality (int);
605 static void dot_cfi_lsda (int);
606 static void dot_cfi_val_encoded_addr (int);
607
608 /* Fake CFI type; outside the byte range of any real CFI insn. */
609 #define CFI_adjust_cfa_offset 0x100
610 #define CFI_return_column 0x101
611 #define CFI_rel_offset 0x102
612 #define CFI_escape 0x103
613 #define CFI_signal_frame 0x104
614 #define CFI_val_encoded_addr 0x105
615
616 const pseudo_typeS cfi_pseudo_table[] =
617 {
618 { "cfi_sections", dot_cfi_sections, 0 },
619 { "cfi_startproc", dot_cfi_startproc, 0 },
620 { "cfi_endproc", dot_cfi_endproc, 0 },
621 { "cfi_def_cfa", dot_cfi, DW_CFA_def_cfa },
622 { "cfi_def_cfa_register", dot_cfi, DW_CFA_def_cfa_register },
623 { "cfi_def_cfa_offset", dot_cfi, DW_CFA_def_cfa_offset },
624 { "cfi_adjust_cfa_offset", dot_cfi, CFI_adjust_cfa_offset },
625 { "cfi_offset", dot_cfi, DW_CFA_offset },
626 { "cfi_rel_offset", dot_cfi, CFI_rel_offset },
627 { "cfi_register", dot_cfi, DW_CFA_register },
628 { "cfi_return_column", dot_cfi, CFI_return_column },
629 { "cfi_restore", dot_cfi, DW_CFA_restore },
630 { "cfi_undefined", dot_cfi, DW_CFA_undefined },
631 { "cfi_same_value", dot_cfi, DW_CFA_same_value },
632 { "cfi_remember_state", dot_cfi, DW_CFA_remember_state },
633 { "cfi_restore_state", dot_cfi, DW_CFA_restore_state },
634 { "cfi_window_save", dot_cfi, DW_CFA_GNU_window_save },
635 { "cfi_escape", dot_cfi_escape, 0 },
636 { "cfi_signal_frame", dot_cfi, CFI_signal_frame },
637 { "cfi_personality", dot_cfi_personality, 0 },
638 { "cfi_lsda", dot_cfi_lsda, 0 },
639 { "cfi_val_encoded_addr", dot_cfi_val_encoded_addr, 0 },
640 { NULL, NULL, 0 }
641 };
642
643 static void
644 cfi_parse_separator (void)
645 {
646 SKIP_WHITESPACE ();
647 if (*input_line_pointer == ',')
648 input_line_pointer++;
649 else
650 as_bad (_("missing separator"));
651 }
652
653 #ifndef tc_parse_to_dw2regnum
654 static void
655 tc_parse_to_dw2regnum (expressionS *exp)
656 {
657 # ifdef tc_regname_to_dw2regnum
658 SKIP_WHITESPACE ();
659 if (is_name_beginner (*input_line_pointer)
660 || (*input_line_pointer == '%'
661 && is_name_beginner (*++input_line_pointer)))
662 {
663 char *name, c;
664
665 name = input_line_pointer;
666 c = get_symbol_end ();
667
668 exp->X_op = O_constant;
669 exp->X_add_number = tc_regname_to_dw2regnum (name);
670
671 *input_line_pointer = c;
672 }
673 else
674 # endif
675 expression_and_evaluate (exp);
676 }
677 #endif
678
679 static unsigned
680 cfi_parse_reg (void)
681 {
682 int regno;
683 expressionS exp;
684
685 tc_parse_to_dw2regnum (&exp);
686 switch (exp.X_op)
687 {
688 case O_register:
689 case O_constant:
690 regno = exp.X_add_number;
691 break;
692
693 default:
694 regno = -1;
695 break;
696 }
697
698 if (regno < 0)
699 {
700 as_bad (_("bad register expression"));
701 regno = 0;
702 }
703
704 return regno;
705 }
706
707 static offsetT
708 cfi_parse_const (void)
709 {
710 return get_absolute_expression ();
711 }
712
713 static void
714 dot_cfi (int arg)
715 {
716 offsetT offset;
717 unsigned reg1, reg2;
718
719 if (frchain_now->frch_cfi_data == NULL)
720 {
721 as_bad (_("CFI instruction used without previous .cfi_startproc"));
722 ignore_rest_of_line ();
723 return;
724 }
725
726 /* If the last address was not at the current PC, advance to current. */
727 if (symbol_get_frag (frchain_now->frch_cfi_data->last_address) != frag_now
728 || S_GET_VALUE (frchain_now->frch_cfi_data->last_address)
729 != frag_now_fix ())
730 cfi_add_advance_loc (symbol_temp_new_now ());
731
732 switch (arg)
733 {
734 case DW_CFA_offset:
735 reg1 = cfi_parse_reg ();
736 cfi_parse_separator ();
737 offset = cfi_parse_const ();
738 cfi_add_CFA_offset (reg1, offset);
739 break;
740
741 case CFI_rel_offset:
742 reg1 = cfi_parse_reg ();
743 cfi_parse_separator ();
744 offset = cfi_parse_const ();
745 cfi_add_CFA_offset (reg1,
746 offset - frchain_now->frch_cfi_data->cur_cfa_offset);
747 break;
748
749 case DW_CFA_def_cfa:
750 reg1 = cfi_parse_reg ();
751 cfi_parse_separator ();
752 offset = cfi_parse_const ();
753 cfi_add_CFA_def_cfa (reg1, offset);
754 break;
755
756 case DW_CFA_register:
757 reg1 = cfi_parse_reg ();
758 cfi_parse_separator ();
759 reg2 = cfi_parse_reg ();
760 cfi_add_CFA_register (reg1, reg2);
761 break;
762
763 case DW_CFA_def_cfa_register:
764 reg1 = cfi_parse_reg ();
765 cfi_add_CFA_def_cfa_register (reg1);
766 break;
767
768 case DW_CFA_def_cfa_offset:
769 offset = cfi_parse_const ();
770 cfi_add_CFA_def_cfa_offset (offset);
771 break;
772
773 case CFI_adjust_cfa_offset:
774 offset = cfi_parse_const ();
775 cfi_add_CFA_def_cfa_offset (frchain_now->frch_cfi_data->cur_cfa_offset
776 + offset);
777 break;
778
779 case DW_CFA_restore:
780 for (;;)
781 {
782 reg1 = cfi_parse_reg ();
783 cfi_add_CFA_restore (reg1);
784 SKIP_WHITESPACE ();
785 if (*input_line_pointer != ',')
786 break;
787 ++input_line_pointer;
788 }
789 break;
790
791 case DW_CFA_undefined:
792 for (;;)
793 {
794 reg1 = cfi_parse_reg ();
795 cfi_add_CFA_undefined (reg1);
796 SKIP_WHITESPACE ();
797 if (*input_line_pointer != ',')
798 break;
799 ++input_line_pointer;
800 }
801 break;
802
803 case DW_CFA_same_value:
804 reg1 = cfi_parse_reg ();
805 cfi_add_CFA_same_value (reg1);
806 break;
807
808 case CFI_return_column:
809 reg1 = cfi_parse_reg ();
810 cfi_set_return_column (reg1);
811 break;
812
813 case DW_CFA_remember_state:
814 cfi_add_CFA_remember_state ();
815 break;
816
817 case DW_CFA_restore_state:
818 cfi_add_CFA_restore_state ();
819 break;
820
821 case DW_CFA_GNU_window_save:
822 cfi_add_CFA_insn (DW_CFA_GNU_window_save);
823 break;
824
825 case CFI_signal_frame:
826 frchain_now->frch_cfi_data->cur_fde_data->signal_frame = 1;
827 break;
828
829 default:
830 abort ();
831 }
832
833 demand_empty_rest_of_line ();
834 }
835
836 static void
837 dot_cfi_escape (int ignored ATTRIBUTE_UNUSED)
838 {
839 struct cfi_escape_data *head, **tail, *e;
840 struct cfi_insn_data *insn;
841
842 if (frchain_now->frch_cfi_data == NULL)
843 {
844 as_bad (_("CFI instruction used without previous .cfi_startproc"));
845 ignore_rest_of_line ();
846 return;
847 }
848
849 /* If the last address was not at the current PC, advance to current. */
850 if (symbol_get_frag (frchain_now->frch_cfi_data->last_address) != frag_now
851 || S_GET_VALUE (frchain_now->frch_cfi_data->last_address)
852 != frag_now_fix ())
853 cfi_add_advance_loc (symbol_temp_new_now ());
854
855 tail = &head;
856 do
857 {
858 e = (struct cfi_escape_data *) xmalloc (sizeof (*e));
859 do_parse_cons_expression (&e->exp, 1);
860 *tail = e;
861 tail = &e->next;
862 }
863 while (*input_line_pointer++ == ',');
864 *tail = NULL;
865
866 insn = alloc_cfi_insn_data ();
867 insn->insn = CFI_escape;
868 insn->u.esc = head;
869
870 --input_line_pointer;
871 demand_empty_rest_of_line ();
872 }
873
874 static void
875 dot_cfi_personality (int ignored ATTRIBUTE_UNUSED)
876 {
877 struct fde_entry *fde;
878 offsetT encoding;
879
880 if (frchain_now->frch_cfi_data == NULL)
881 {
882 as_bad (_("CFI instruction used without previous .cfi_startproc"));
883 ignore_rest_of_line ();
884 return;
885 }
886
887 fde = frchain_now->frch_cfi_data->cur_fde_data;
888 encoding = cfi_parse_const ();
889 if (encoding == DW_EH_PE_omit)
890 {
891 demand_empty_rest_of_line ();
892 fde->per_encoding = encoding;
893 return;
894 }
895
896 if ((encoding & 0xff) != encoding
897 || ((encoding & 0x70) != 0
898 #if CFI_DIFF_EXPR_OK || defined tc_cfi_emit_pcrel_expr
899 && (encoding & 0x70) != DW_EH_PE_pcrel
900 #endif
901 )
902 /* leb128 can be handled, but does something actually need it? */
903 || (encoding & 7) == DW_EH_PE_uleb128
904 || (encoding & 7) > DW_EH_PE_udata8)
905 {
906 as_bad (_("invalid or unsupported encoding in .cfi_personality"));
907 ignore_rest_of_line ();
908 return;
909 }
910
911 if (*input_line_pointer++ != ',')
912 {
913 as_bad (_(".cfi_personality requires encoding and symbol arguments"));
914 ignore_rest_of_line ();
915 return;
916 }
917
918 expression_and_evaluate (&fde->personality);
919 switch (fde->personality.X_op)
920 {
921 case O_symbol:
922 break;
923 case O_constant:
924 if ((encoding & 0x70) == DW_EH_PE_pcrel)
925 encoding = DW_EH_PE_omit;
926 break;
927 default:
928 encoding = DW_EH_PE_omit;
929 break;
930 }
931
932 fde->per_encoding = encoding;
933
934 if (encoding == DW_EH_PE_omit)
935 {
936 as_bad (_("wrong second argument to .cfi_personality"));
937 ignore_rest_of_line ();
938 return;
939 }
940
941 demand_empty_rest_of_line ();
942 }
943
944 static void
945 dot_cfi_lsda (int ignored ATTRIBUTE_UNUSED)
946 {
947 struct fde_entry *fde;
948 offsetT encoding;
949
950 if (frchain_now->frch_cfi_data == NULL)
951 {
952 as_bad (_("CFI instruction used without previous .cfi_startproc"));
953 ignore_rest_of_line ();
954 return;
955 }
956
957 fde = frchain_now->frch_cfi_data->cur_fde_data;
958 encoding = cfi_parse_const ();
959 if (encoding == DW_EH_PE_omit)
960 {
961 demand_empty_rest_of_line ();
962 fde->lsda_encoding = encoding;
963 return;
964 }
965
966 if ((encoding & 0xff) != encoding
967 || ((encoding & 0x70) != 0
968 #if CFI_DIFF_LSDA_OK || defined tc_cfi_emit_pcrel_expr
969 && (encoding & 0x70) != DW_EH_PE_pcrel
970 #endif
971 )
972 /* leb128 can be handled, but does something actually need it? */
973 || (encoding & 7) == DW_EH_PE_uleb128
974 || (encoding & 7) > DW_EH_PE_udata8)
975 {
976 as_bad (_("invalid or unsupported encoding in .cfi_lsda"));
977 ignore_rest_of_line ();
978 return;
979 }
980
981 if (*input_line_pointer++ != ',')
982 {
983 as_bad (_(".cfi_lsda requires encoding and symbol arguments"));
984 ignore_rest_of_line ();
985 return;
986 }
987
988 fde->lsda_encoding = encoding;
989
990 expression_and_evaluate (&fde->lsda);
991 switch (fde->lsda.X_op)
992 {
993 case O_symbol:
994 break;
995 case O_constant:
996 if ((encoding & 0x70) == DW_EH_PE_pcrel)
997 encoding = DW_EH_PE_omit;
998 break;
999 default:
1000 encoding = DW_EH_PE_omit;
1001 break;
1002 }
1003
1004 fde->lsda_encoding = encoding;
1005
1006 if (encoding == DW_EH_PE_omit)
1007 {
1008 as_bad (_("wrong second argument to .cfi_lsda"));
1009 ignore_rest_of_line ();
1010 return;
1011 }
1012
1013 demand_empty_rest_of_line ();
1014 }
1015
1016 static void
1017 dot_cfi_val_encoded_addr (int ignored ATTRIBUTE_UNUSED)
1018 {
1019 struct cfi_insn_data *insn_ptr;
1020 offsetT encoding;
1021
1022 if (frchain_now->frch_cfi_data == NULL)
1023 {
1024 as_bad (_("CFI instruction used without previous .cfi_startproc"));
1025 ignore_rest_of_line ();
1026 return;
1027 }
1028
1029 /* If the last address was not at the current PC, advance to current. */
1030 if (symbol_get_frag (frchain_now->frch_cfi_data->last_address) != frag_now
1031 || S_GET_VALUE (frchain_now->frch_cfi_data->last_address)
1032 != frag_now_fix ())
1033 cfi_add_advance_loc (symbol_temp_new_now ());
1034
1035 insn_ptr = alloc_cfi_insn_data ();
1036 insn_ptr->insn = CFI_val_encoded_addr;
1037
1038 insn_ptr->u.ea.reg = cfi_parse_reg ();
1039
1040 cfi_parse_separator ();
1041 encoding = cfi_parse_const ();
1042 if ((encoding & 0xff) != encoding
1043 || ((encoding & 0x70) != 0
1044 #if CFI_DIFF_EXPR_OK || defined tc_cfi_emit_pcrel_expr
1045 && (encoding & 0x70) != DW_EH_PE_pcrel
1046 #endif
1047 )
1048 /* leb128 can be handled, but does something actually need it? */
1049 || (encoding & 7) == DW_EH_PE_uleb128
1050 || (encoding & 7) > DW_EH_PE_udata8)
1051 {
1052 as_bad (_("invalid or unsupported encoding in .cfi_lsda"));
1053 encoding = DW_EH_PE_omit;
1054 }
1055
1056 cfi_parse_separator ();
1057 expression_and_evaluate (&insn_ptr->u.ea.exp);
1058 switch (insn_ptr->u.ea.exp.X_op)
1059 {
1060 case O_symbol:
1061 break;
1062 case O_constant:
1063 if ((encoding & 0x70) != DW_EH_PE_pcrel)
1064 break;
1065 default:
1066 encoding = DW_EH_PE_omit;
1067 break;
1068 }
1069
1070 insn_ptr->u.ea.encoding = encoding;
1071 if (encoding == DW_EH_PE_omit)
1072 {
1073 as_bad (_("wrong third argument to .cfi_val_encoded_addr"));
1074 ignore_rest_of_line ();
1075 return;
1076 }
1077
1078 demand_empty_rest_of_line ();
1079 }
1080
1081 /* By default emit .eh_frame only, not .debug_frame. */
1082 #define CFI_EMIT_eh_frame (1 << 0)
1083 #define CFI_EMIT_debug_frame (1 << 1)
1084 static int cfi_sections = CFI_EMIT_eh_frame;
1085
1086 static void
1087 dot_cfi_sections (int ignored ATTRIBUTE_UNUSED)
1088 {
1089 int sections = 0;
1090
1091 SKIP_WHITESPACE ();
1092 if (is_name_beginner (*input_line_pointer))
1093 while (1)
1094 {
1095 char *name, c;
1096
1097 name = input_line_pointer;
1098 c = get_symbol_end ();
1099
1100 if (strncmp (name, ".eh_frame", sizeof ".eh_frame") == 0
1101 && name[9] != '_')
1102 sections |= CFI_EMIT_eh_frame;
1103 else if (strncmp (name, ".debug_frame", sizeof ".debug_frame") == 0)
1104 sections |= CFI_EMIT_debug_frame;
1105 else
1106 {
1107 *input_line_pointer = c;
1108 input_line_pointer = name;
1109 break;
1110 }
1111
1112 *input_line_pointer = c;
1113 SKIP_WHITESPACE ();
1114 if (*input_line_pointer == ',')
1115 {
1116 name = input_line_pointer++;
1117 SKIP_WHITESPACE ();
1118 if (!is_name_beginner (*input_line_pointer))
1119 {
1120 input_line_pointer = name;
1121 break;
1122 }
1123 }
1124 else if (is_name_beginner (*input_line_pointer))
1125 break;
1126 }
1127
1128 demand_empty_rest_of_line ();
1129 cfi_sections = sections;
1130 }
1131
1132 static void
1133 dot_cfi_startproc (int ignored ATTRIBUTE_UNUSED)
1134 {
1135 int simple = 0;
1136
1137 if (frchain_now->frch_cfi_data != NULL)
1138 {
1139 as_bad (_("previous CFI entry not closed (missing .cfi_endproc)"));
1140 ignore_rest_of_line ();
1141 return;
1142 }
1143
1144 cfi_new_fde (symbol_temp_new_now ());
1145
1146 SKIP_WHITESPACE ();
1147 if (is_name_beginner (*input_line_pointer))
1148 {
1149 char *name, c;
1150
1151 name = input_line_pointer;
1152 c = get_symbol_end ();
1153
1154 if (strcmp (name, "simple") == 0)
1155 {
1156 simple = 1;
1157 *input_line_pointer = c;
1158 }
1159 else
1160 input_line_pointer = name;
1161 }
1162 demand_empty_rest_of_line ();
1163
1164 frchain_now->frch_cfi_data->cur_cfa_offset = 0;
1165 if (!simple)
1166 tc_cfi_frame_initial_instructions ();
1167 }
1168
1169 static void
1170 dot_cfi_endproc (int ignored ATTRIBUTE_UNUSED)
1171 {
1172 if (frchain_now->frch_cfi_data == NULL)
1173 {
1174 as_bad (_(".cfi_endproc without corresponding .cfi_startproc"));
1175 ignore_rest_of_line ();
1176 return;
1177 }
1178
1179 cfi_end_fde (symbol_temp_new_now ());
1180
1181 demand_empty_rest_of_line ();
1182 }
1183
1184 \f
1185 /* Emit a single byte into the current segment. */
1186
1187 static inline void
1188 out_one (int byte)
1189 {
1190 FRAG_APPEND_1_CHAR (byte);
1191 }
1192
1193 /* Emit a two-byte word into the current segment. */
1194
1195 static inline void
1196 out_two (int data)
1197 {
1198 md_number_to_chars (frag_more (2), data, 2);
1199 }
1200
1201 /* Emit a four byte word into the current segment. */
1202
1203 static inline void
1204 out_four (int data)
1205 {
1206 md_number_to_chars (frag_more (4), data, 4);
1207 }
1208
1209 /* Emit an unsigned "little-endian base 128" number. */
1210
1211 static void
1212 out_uleb128 (addressT value)
1213 {
1214 output_leb128 (frag_more (sizeof_leb128 (value, 0)), value, 0);
1215 }
1216
1217 /* Emit an unsigned "little-endian base 128" number. */
1218
1219 static void
1220 out_sleb128 (offsetT value)
1221 {
1222 output_leb128 (frag_more (sizeof_leb128 (value, 1)), value, 1);
1223 }
1224
1225 static void
1226 output_cfi_insn (struct cfi_insn_data *insn)
1227 {
1228 offsetT offset;
1229 unsigned int regno;
1230
1231 switch (insn->insn)
1232 {
1233 case DW_CFA_advance_loc:
1234 {
1235 symbolS *from = insn->u.ll.lab1;
1236 symbolS *to = insn->u.ll.lab2;
1237
1238 if (symbol_get_frag (to) == symbol_get_frag (from))
1239 {
1240 addressT delta = S_GET_VALUE (to) - S_GET_VALUE (from);
1241 addressT scaled = delta / DWARF2_LINE_MIN_INSN_LENGTH;
1242
1243 if (scaled <= 0x3F)
1244 out_one (DW_CFA_advance_loc + scaled);
1245 else if (scaled <= 0xFF)
1246 {
1247 out_one (DW_CFA_advance_loc1);
1248 out_one (scaled);
1249 }
1250 else if (scaled <= 0xFFFF)
1251 {
1252 out_one (DW_CFA_advance_loc2);
1253 out_two (scaled);
1254 }
1255 else
1256 {
1257 out_one (DW_CFA_advance_loc4);
1258 out_four (scaled);
1259 }
1260 }
1261 else
1262 {
1263 expressionS exp;
1264
1265 exp.X_op = O_subtract;
1266 exp.X_add_symbol = to;
1267 exp.X_op_symbol = from;
1268 exp.X_add_number = 0;
1269
1270 /* The code in ehopt.c expects that one byte of the encoding
1271 is already allocated to the frag. This comes from the way
1272 that it scans the .eh_frame section looking first for the
1273 .byte DW_CFA_advance_loc4. */
1274 *frag_more (1) = DW_CFA_advance_loc4;
1275
1276 frag_var (rs_cfa, 4, 0, DWARF2_LINE_MIN_INSN_LENGTH << 3,
1277 make_expr_symbol (&exp), frag_now_fix () - 1,
1278 (char *) frag_now);
1279 }
1280 }
1281 break;
1282
1283 case DW_CFA_def_cfa:
1284 offset = insn->u.ri.offset;
1285 if (offset < 0)
1286 {
1287 out_one (DW_CFA_def_cfa_sf);
1288 out_uleb128 (insn->u.ri.reg);
1289 out_sleb128 (offset / DWARF2_CIE_DATA_ALIGNMENT);
1290 }
1291 else
1292 {
1293 out_one (DW_CFA_def_cfa);
1294 out_uleb128 (insn->u.ri.reg);
1295 out_uleb128 (offset);
1296 }
1297 break;
1298
1299 case DW_CFA_def_cfa_register:
1300 case DW_CFA_undefined:
1301 case DW_CFA_same_value:
1302 out_one (insn->insn);
1303 out_uleb128 (insn->u.r);
1304 break;
1305
1306 case DW_CFA_def_cfa_offset:
1307 offset = insn->u.i;
1308 if (offset < 0)
1309 {
1310 out_one (DW_CFA_def_cfa_offset_sf);
1311 out_sleb128 (offset / DWARF2_CIE_DATA_ALIGNMENT);
1312 }
1313 else
1314 {
1315 out_one (DW_CFA_def_cfa_offset);
1316 out_uleb128 (offset);
1317 }
1318 break;
1319
1320 case DW_CFA_restore:
1321 regno = insn->u.r;
1322 if (regno <= 0x3F)
1323 {
1324 out_one (DW_CFA_restore + regno);
1325 }
1326 else
1327 {
1328 out_one (DW_CFA_restore_extended);
1329 out_uleb128 (regno);
1330 }
1331 break;
1332
1333 case DW_CFA_offset:
1334 regno = insn->u.ri.reg;
1335 offset = insn->u.ri.offset / DWARF2_CIE_DATA_ALIGNMENT;
1336 if (offset < 0)
1337 {
1338 out_one (DW_CFA_offset_extended_sf);
1339 out_uleb128 (regno);
1340 out_sleb128 (offset);
1341 }
1342 else if (regno <= 0x3F)
1343 {
1344 out_one (DW_CFA_offset + regno);
1345 out_uleb128 (offset);
1346 }
1347 else
1348 {
1349 out_one (DW_CFA_offset_extended);
1350 out_uleb128 (regno);
1351 out_uleb128 (offset);
1352 }
1353 break;
1354
1355 case DW_CFA_register:
1356 out_one (DW_CFA_register);
1357 out_uleb128 (insn->u.rr.reg1);
1358 out_uleb128 (insn->u.rr.reg2);
1359 break;
1360
1361 case DW_CFA_remember_state:
1362 case DW_CFA_restore_state:
1363 out_one (insn->insn);
1364 break;
1365
1366 case DW_CFA_GNU_window_save:
1367 out_one (DW_CFA_GNU_window_save);
1368 break;
1369
1370 case CFI_escape:
1371 {
1372 struct cfi_escape_data *e;
1373 for (e = insn->u.esc; e ; e = e->next)
1374 emit_expr (&e->exp, 1);
1375 break;
1376 }
1377
1378 case CFI_val_encoded_addr:
1379 {
1380 unsigned encoding = insn->u.ea.encoding;
1381 offsetT encoding_size;
1382
1383 if (encoding == DW_EH_PE_omit)
1384 break;
1385 out_one (DW_CFA_val_expression);
1386 out_uleb128 (insn->u.ea.reg);
1387
1388 switch (encoding & 0x7)
1389 {
1390 case DW_EH_PE_absptr:
1391 encoding_size = DWARF2_ADDR_SIZE (stdoutput);
1392 break;
1393 case DW_EH_PE_udata2:
1394 encoding_size = 2;
1395 break;
1396 case DW_EH_PE_udata4:
1397 encoding_size = 4;
1398 break;
1399 case DW_EH_PE_udata8:
1400 encoding_size = 8;
1401 break;
1402 default:
1403 abort ();
1404 }
1405
1406 /* If the user has requested absolute encoding,
1407 then use the smaller DW_OP_addr encoding. */
1408 if (insn->u.ea.encoding == DW_EH_PE_absptr)
1409 {
1410 out_uleb128 (1 + encoding_size);
1411 out_one (DW_OP_addr);
1412 }
1413 else
1414 {
1415 out_uleb128 (1 + 1 + encoding_size);
1416 out_one (DW_OP_GNU_encoded_addr);
1417 out_one (encoding);
1418
1419 if ((encoding & 0x70) == DW_EH_PE_pcrel)
1420 {
1421 #if CFI_DIFF_EXPR_OK
1422 insn->u.ea.exp.X_op = O_subtract;
1423 insn->u.ea.exp.X_op_symbol = symbol_temp_new_now ();
1424 #elif defined (tc_cfi_emit_pcrel_expr)
1425 tc_cfi_emit_pcrel_expr (&insn->u.ea.exp, encoding_size);
1426 break;
1427 #else
1428 abort ();
1429 #endif
1430 }
1431 }
1432 emit_expr (&insn->u.ea.exp, encoding_size);
1433 }
1434 break;
1435
1436 default:
1437 abort ();
1438 }
1439 }
1440
1441 static offsetT
1442 encoding_size (unsigned char encoding)
1443 {
1444 if (encoding == DW_EH_PE_omit)
1445 return 0;
1446 switch (encoding & 0x7)
1447 {
1448 case 0:
1449 return bfd_get_arch_size (stdoutput) == 64 ? 8 : 4;
1450 case DW_EH_PE_udata2:
1451 return 2;
1452 case DW_EH_PE_udata4:
1453 return 4;
1454 case DW_EH_PE_udata8:
1455 return 8;
1456 default:
1457 abort ();
1458 }
1459 }
1460
1461 static void
1462 output_cie (struct cie_entry *cie, bfd_boolean eh_frame, int align)
1463 {
1464 symbolS *after_size_address, *end_address;
1465 expressionS exp;
1466 struct cfi_insn_data *i;
1467 offsetT augmentation_size;
1468 int enc;
1469 enum dwarf2_format fmt = DWARF2_FORMAT (now_seg);
1470
1471 cie->start_address = symbol_temp_new_now ();
1472 after_size_address = symbol_temp_make ();
1473 end_address = symbol_temp_make ();
1474
1475 exp.X_op = O_subtract;
1476 exp.X_add_symbol = end_address;
1477 exp.X_op_symbol = after_size_address;
1478 exp.X_add_number = 0;
1479
1480 if (eh_frame || fmt == dwarf2_format_32bit)
1481 emit_expr (&exp, 4); /* Length. */
1482 else
1483 {
1484 if (fmt == dwarf2_format_64bit)
1485 out_four (-1);
1486 emit_expr (&exp, 8); /* Length. */
1487 }
1488 symbol_set_value_now (after_size_address);
1489 if (eh_frame)
1490 out_four (0); /* CIE id. */
1491 else
1492 {
1493 out_four (-1); /* CIE id. */
1494 if (fmt != dwarf2_format_32bit)
1495 out_four (-1);
1496 }
1497 out_one (DW_CIE_VERSION); /* Version. */
1498 if (eh_frame)
1499 {
1500 out_one ('z'); /* Augmentation. */
1501 if (cie->per_encoding != DW_EH_PE_omit)
1502 out_one ('P');
1503 if (cie->lsda_encoding != DW_EH_PE_omit)
1504 out_one ('L');
1505 out_one ('R');
1506 }
1507 if (cie->signal_frame)
1508 out_one ('S');
1509 out_one (0);
1510 out_uleb128 (DWARF2_LINE_MIN_INSN_LENGTH); /* Code alignment. */
1511 out_sleb128 (DWARF2_CIE_DATA_ALIGNMENT); /* Data alignment. */
1512 if (DW_CIE_VERSION == 1) /* Return column. */
1513 out_one (cie->return_column);
1514 else
1515 out_uleb128 (cie->return_column);
1516 if (eh_frame)
1517 {
1518 augmentation_size = 1 + (cie->lsda_encoding != DW_EH_PE_omit);
1519 if (cie->per_encoding != DW_EH_PE_omit)
1520 augmentation_size += 1 + encoding_size (cie->per_encoding);
1521 out_uleb128 (augmentation_size); /* Augmentation size. */
1522
1523 if (cie->per_encoding != DW_EH_PE_omit)
1524 {
1525 offsetT size = encoding_size (cie->per_encoding);
1526 out_one (cie->per_encoding);
1527 exp = cie->personality;
1528 if ((cie->per_encoding & 0x70) == DW_EH_PE_pcrel)
1529 {
1530 #if CFI_DIFF_EXPR_OK
1531 exp.X_op = O_subtract;
1532 exp.X_op_symbol = symbol_temp_new_now ();
1533 emit_expr (&exp, size);
1534 #elif defined (tc_cfi_emit_pcrel_expr)
1535 tc_cfi_emit_pcrel_expr (&exp, size);
1536 #else
1537 abort ();
1538 #endif
1539 }
1540 else
1541 emit_expr (&exp, size);
1542 }
1543
1544 if (cie->lsda_encoding != DW_EH_PE_omit)
1545 out_one (cie->lsda_encoding);
1546 }
1547
1548 switch (DWARF2_FDE_RELOC_SIZE)
1549 {
1550 case 2:
1551 enc = DW_EH_PE_sdata2;
1552 break;
1553 case 4:
1554 enc = DW_EH_PE_sdata4;
1555 break;
1556 case 8:
1557 enc = DW_EH_PE_sdata8;
1558 break;
1559 default:
1560 abort ();
1561 }
1562 #if CFI_DIFF_EXPR_OK || defined tc_cfi_emit_pcrel_expr
1563 enc |= DW_EH_PE_pcrel;
1564 #endif
1565 if (eh_frame)
1566 out_one (enc);
1567
1568 if (cie->first)
1569 {
1570 for (i = cie->first; i != cie->last; i = i->next)
1571 {
1572 #if SUPPORT_FRAME_LINKONCE
1573 if (i->cur_seg != cie->cur_seg)
1574 continue;
1575 #endif
1576 output_cfi_insn (i);
1577 }
1578 }
1579
1580 frag_align (align, DW_CFA_nop, 0);
1581 symbol_set_value_now (end_address);
1582 }
1583
1584 static void
1585 output_fde (struct fde_entry *fde, struct cie_entry *cie,
1586 bfd_boolean eh_frame, struct cfi_insn_data *first,
1587 int align)
1588 {
1589 symbolS *after_size_address, *end_address;
1590 expressionS exp;
1591 offsetT augmentation_size;
1592 enum dwarf2_format fmt = DWARF2_FORMAT (now_seg);
1593 int offset_size;
1594 int addr_size;
1595
1596 after_size_address = symbol_temp_make ();
1597 end_address = symbol_temp_make ();
1598
1599 exp.X_op = O_subtract;
1600 exp.X_add_symbol = end_address;
1601 exp.X_op_symbol = after_size_address;
1602 exp.X_add_number = 0;
1603 if (eh_frame || fmt == dwarf2_format_32bit)
1604 offset_size = 4;
1605 else
1606 {
1607 if (fmt == dwarf2_format_64bit)
1608 out_four (-1);
1609 offset_size = 8;
1610 }
1611 emit_expr (&exp, offset_size); /* Length. */
1612 symbol_set_value_now (after_size_address);
1613
1614 if (eh_frame)
1615 {
1616 exp.X_op = O_subtract;
1617 exp.X_add_symbol = after_size_address;
1618 exp.X_op_symbol = cie->start_address;
1619 exp.X_add_number = 0;
1620 emit_expr (&exp, offset_size); /* CIE offset. */
1621 }
1622 else
1623 {
1624 TC_DWARF2_EMIT_OFFSET (cie->start_address, offset_size);
1625 }
1626
1627 if (eh_frame)
1628 {
1629 exp.X_op = O_subtract;
1630 exp.X_add_number = 0;
1631 #if CFI_DIFF_EXPR_OK
1632 exp.X_add_symbol = fde->start_address;
1633 exp.X_op_symbol = symbol_temp_new_now ();
1634 emit_expr (&exp, DWARF2_FDE_RELOC_SIZE); /* Code offset. */
1635 #else
1636 exp.X_op = O_symbol;
1637 exp.X_add_symbol = fde->start_address;
1638 #ifdef tc_cfi_emit_pcrel_expr
1639 tc_cfi_emit_pcrel_expr (&exp, DWARF2_FDE_RELOC_SIZE); /* Code offset. */
1640 #else
1641 emit_expr (&exp, DWARF2_FDE_RELOC_SIZE); /* Code offset. */
1642 #endif
1643 #endif
1644 addr_size = DWARF2_FDE_RELOC_SIZE;
1645 }
1646 else
1647 {
1648 exp.X_op = O_symbol;
1649 exp.X_add_symbol = fde->start_address;
1650 exp.X_add_number = 0;
1651 addr_size = DWARF2_ADDR_SIZE (stdoutput);
1652 emit_expr (&exp, addr_size);
1653 }
1654
1655 exp.X_op = O_subtract;
1656 exp.X_add_symbol = fde->end_address;
1657 exp.X_op_symbol = fde->start_address; /* Code length. */
1658 exp.X_add_number = 0;
1659 emit_expr (&exp, addr_size);
1660
1661 augmentation_size = encoding_size (fde->lsda_encoding);
1662 if (eh_frame)
1663 out_uleb128 (augmentation_size); /* Augmentation size. */
1664
1665 if (fde->lsda_encoding != DW_EH_PE_omit)
1666 {
1667 exp = fde->lsda;
1668 if ((fde->lsda_encoding & 0x70) == DW_EH_PE_pcrel)
1669 {
1670 #if CFI_DIFF_LSDA_OK
1671 exp.X_op = O_subtract;
1672 exp.X_op_symbol = symbol_temp_new_now ();
1673 emit_expr (&exp, augmentation_size);
1674 #elif defined (tc_cfi_emit_pcrel_expr)
1675 tc_cfi_emit_pcrel_expr (&exp, augmentation_size);
1676 #else
1677 abort ();
1678 #endif
1679 }
1680 else
1681 emit_expr (&exp, augmentation_size);
1682 }
1683
1684 for (; first; first = first->next)
1685 #if SUPPORT_FRAME_LINKONCE
1686 if (first->cur_seg == fde->cseg)
1687 #endif
1688 output_cfi_insn (first);
1689
1690 frag_align (align, DW_CFA_nop, 0);
1691 symbol_set_value_now (end_address);
1692 }
1693
1694 static struct cie_entry *
1695 select_cie_for_fde (struct fde_entry *fde, bfd_boolean eh_frame,
1696 struct cfi_insn_data **pfirst, int align)
1697 {
1698 struct cfi_insn_data *i, *j;
1699 struct cie_entry *cie;
1700
1701 for (cie = cie_root; cie; cie = cie->next)
1702 {
1703 #if SUPPORT_FRAME_LINKONCE
1704 if (cie->cur_seg != fde->cseg)
1705 continue;
1706 #endif
1707 if (cie->return_column != fde->return_column
1708 || cie->signal_frame != fde->signal_frame
1709 || cie->per_encoding != fde->per_encoding
1710 || cie->lsda_encoding != fde->lsda_encoding)
1711 continue;
1712 if (cie->per_encoding != DW_EH_PE_omit)
1713 {
1714 if (cie->personality.X_op != fde->personality.X_op
1715 || cie->personality.X_add_number
1716 != fde->personality.X_add_number)
1717 continue;
1718 switch (cie->personality.X_op)
1719 {
1720 case O_constant:
1721 if (cie->personality.X_unsigned != fde->personality.X_unsigned)
1722 continue;
1723 break;
1724 case O_symbol:
1725 if (cie->personality.X_add_symbol
1726 != fde->personality.X_add_symbol)
1727 continue;
1728 break;
1729 default:
1730 abort ();
1731 }
1732 }
1733 for (i = cie->first, j = fde->data;
1734 i != cie->last && j != NULL;
1735 i = i->next, j = j->next)
1736 {
1737 if (i->insn != j->insn)
1738 goto fail;
1739 switch (i->insn)
1740 {
1741 case DW_CFA_advance_loc:
1742 case DW_CFA_remember_state:
1743 /* We reached the first advance/remember in the FDE,
1744 but did not reach the end of the CIE list. */
1745 goto fail;
1746
1747 case DW_CFA_offset:
1748 case DW_CFA_def_cfa:
1749 if (i->u.ri.reg != j->u.ri.reg)
1750 goto fail;
1751 if (i->u.ri.offset != j->u.ri.offset)
1752 goto fail;
1753 break;
1754
1755 case DW_CFA_register:
1756 if (i->u.rr.reg1 != j->u.rr.reg1)
1757 goto fail;
1758 if (i->u.rr.reg2 != j->u.rr.reg2)
1759 goto fail;
1760 break;
1761
1762 case DW_CFA_def_cfa_register:
1763 case DW_CFA_restore:
1764 case DW_CFA_undefined:
1765 case DW_CFA_same_value:
1766 if (i->u.r != j->u.r)
1767 goto fail;
1768 break;
1769
1770 case DW_CFA_def_cfa_offset:
1771 if (i->u.i != j->u.i)
1772 goto fail;
1773 break;
1774
1775 case CFI_escape:
1776 case CFI_val_encoded_addr:
1777 /* Don't bother matching these for now. */
1778 goto fail;
1779
1780 default:
1781 abort ();
1782 }
1783 }
1784
1785 /* Success if we reached the end of the CIE list, and we've either
1786 run out of FDE entries or we've encountered an advance,
1787 remember, or escape. */
1788 if (i == cie->last
1789 && (!j
1790 || j->insn == DW_CFA_advance_loc
1791 || j->insn == DW_CFA_remember_state
1792 || j->insn == CFI_escape
1793 || j->insn == CFI_val_encoded_addr))
1794 {
1795 *pfirst = j;
1796 return cie;
1797 }
1798
1799 fail:;
1800 }
1801
1802 cie = (struct cie_entry *) xmalloc (sizeof (struct cie_entry));
1803 cie->next = cie_root;
1804 cie_root = cie;
1805 #if SUPPORT_FRAME_LINKONCE
1806 cie->cur_seg = fde->cseg;
1807 #endif
1808 cie->return_column = fde->return_column;
1809 cie->signal_frame = fde->signal_frame;
1810 cie->per_encoding = fde->per_encoding;
1811 cie->lsda_encoding = fde->lsda_encoding;
1812 cie->personality = fde->personality;
1813 cie->first = fde->data;
1814
1815 for (i = cie->first; i ; i = i->next)
1816 if (i->insn == DW_CFA_advance_loc
1817 || i->insn == DW_CFA_remember_state
1818 || i->insn == CFI_escape
1819 || i->insn == CFI_val_encoded_addr)
1820 break;
1821
1822 cie->last = i;
1823 *pfirst = i;
1824
1825 output_cie (cie, eh_frame, align);
1826
1827 return cie;
1828 }
1829
1830 #ifdef md_reg_eh_frame_to_debug_frame
1831 static void
1832 cfi_change_reg_numbers (struct cfi_insn_data *insn, segT ccseg ATTRIBUTE_UNUSED)
1833 {
1834 for (; insn; insn = insn->next)
1835 {
1836 #if SUPPORT_FRAME_LINKONCE
1837 if (insn->cur_seg != ccseg)
1838 continue;
1839 #endif
1840 switch (insn->insn)
1841 {
1842 case DW_CFA_advance_loc:
1843 case DW_CFA_def_cfa_offset:
1844 case DW_CFA_remember_state:
1845 case DW_CFA_restore_state:
1846 case DW_CFA_GNU_window_save:
1847 case CFI_escape:
1848 break;
1849
1850 case DW_CFA_def_cfa:
1851 case DW_CFA_offset:
1852 insn->u.ri.reg = md_reg_eh_frame_to_debug_frame (insn->u.ri.reg);
1853 break;
1854
1855 case DW_CFA_def_cfa_register:
1856 case DW_CFA_undefined:
1857 case DW_CFA_same_value:
1858 case DW_CFA_restore:
1859 insn->u.r = md_reg_eh_frame_to_debug_frame (insn->u.r);
1860 break;
1861
1862 case DW_CFA_register:
1863 insn->u.rr.reg1 = md_reg_eh_frame_to_debug_frame (insn->u.rr.reg1);
1864 insn->u.rr.reg2 = md_reg_eh_frame_to_debug_frame (insn->u.rr.reg2);
1865 break;
1866
1867 case CFI_val_encoded_addr:
1868 insn->u.ea.reg = md_reg_eh_frame_to_debug_frame (insn->u.ea.reg);
1869 break;
1870
1871 default:
1872 abort ();
1873 }
1874 }
1875 }
1876 #else
1877 #define cfi_change_reg_numbers(insn, cseg) do { } while (0)
1878 #endif
1879
1880 static segT
1881 get_cfi_seg (segT cseg ATTRIBUTE_UNUSED,
1882 const char *base, flagword flags, int align)
1883 {
1884 #if SUPPORT_FRAME_LINKONCE
1885 struct dwcfi_seg_list *l;
1886
1887 l = dwcfi_hash_find_or_make (cseg, base, flags);
1888
1889 cseg = l->seg;
1890 subseg_set (cseg, l->subseg);
1891 #else
1892 cseg = subseg_new (base, 0);
1893 bfd_set_section_flags (stdoutput, cseg, flags);
1894 #endif
1895 record_alignment (cseg, align);
1896 return cseg;
1897 }
1898
1899 void
1900 cfi_finish (void)
1901 {
1902 struct cie_entry *cie, *cie_next;
1903 segT cfi_seg, ccseg;
1904 struct fde_entry *fde;
1905 struct cfi_insn_data *first;
1906 int save_flag_traditional_format, seek_next_seg;
1907
1908 if (all_fde_data == 0)
1909 return;
1910
1911 if ((cfi_sections & CFI_EMIT_eh_frame) != 0)
1912 {
1913 /* Make sure check_eh_frame doesn't do anything with our output. */
1914 save_flag_traditional_format = flag_traditional_format;
1915 flag_traditional_format = 1;
1916
1917 #if !SUPPORT_FRAME_LINKONCE
1918 /* Open .eh_frame section. */
1919 cfi_seg = get_cfi_seg (NULL, ".eh_frame",
1920 SEC_ALLOC | SEC_LOAD | SEC_DATA
1921 | DWARF2_EH_FRAME_READ_ONLY,
1922 EH_FRAME_ALIGNMENT);
1923 #ifdef md_fix_up_eh_frame
1924 md_fix_up_eh_frame (cfi_seg);
1925 #else
1926 (void) cfi_seg;
1927 #endif
1928 #endif
1929
1930 do
1931 {
1932 ccseg = NULL;
1933 seek_next_seg = 0;
1934
1935 for (cie = cie_root; cie; cie = cie_next)
1936 {
1937 cie_next = cie->next;
1938 free ((void *) cie);
1939 }
1940 cie_root = NULL;
1941
1942 for (fde = all_fde_data; fde ; fde = fde->next)
1943 {
1944 #if SUPPORT_FRAME_LINKONCE
1945 if (fde->handled != 0)
1946 continue;
1947 if (seek_next_seg && fde->cseg != ccseg)
1948 {
1949 seek_next_seg = 2;
1950 continue;
1951 }
1952 if (!seek_next_seg)
1953 {
1954 ccseg = fde->cseg;
1955 /* Open .eh_frame section. */
1956 cfi_seg = get_cfi_seg (ccseg, ".eh_frame",
1957 SEC_ALLOC | SEC_LOAD | SEC_DATA
1958 | DWARF2_EH_FRAME_READ_ONLY,
1959 EH_FRAME_ALIGNMENT);
1960 #ifdef md_fix_up_eh_frame
1961 md_fix_up_eh_frame (cfi_seg);
1962 #else
1963 (void) cfi_seg;
1964 #endif
1965 seek_next_seg = 1;
1966 }
1967 fde->handled = 1;
1968 #endif
1969
1970 if (fde->end_address == NULL)
1971 {
1972 as_bad (_("open CFI at the end of file; missing .cfi_endproc directive"));
1973 fde->end_address = fde->start_address;
1974 }
1975
1976 cie = select_cie_for_fde (fde, TRUE, &first, 2);
1977 output_fde (fde, cie, TRUE, first,
1978 fde->next == NULL ? EH_FRAME_ALIGNMENT : 2);
1979 }
1980 }
1981 while (SUPPORT_FRAME_LINKONCE && seek_next_seg == 2);
1982
1983 #if SUPPORT_FRAME_LINKONCE
1984 for (fde = all_fde_data; fde ; fde = fde->next)
1985 fde->handled = 0;
1986 #endif
1987
1988 flag_traditional_format = save_flag_traditional_format;
1989 }
1990
1991 if ((cfi_sections & CFI_EMIT_debug_frame) != 0)
1992 {
1993 int alignment = ffs (DWARF2_ADDR_SIZE (stdoutput)) - 1;
1994
1995 #if !SUPPORT_FRAME_LINKONCE
1996 get_cfi_seg (NULL, ".debug_frame",
1997 SEC_READONLY | SEC_DEBUGGING,
1998 alignment);
1999 #endif
2000 do
2001 {
2002 ccseg = NULL;
2003 seek_next_seg = 0;
2004
2005 for (cie = cie_root; cie; cie = cie_next)
2006 {
2007 cie_next = cie->next;
2008 free ((void *) cie);
2009 }
2010 cie_root = NULL;
2011
2012 for (fde = all_fde_data; fde ; fde = fde->next)
2013 {
2014 #if SUPPORT_FRAME_LINKONCE
2015 if (fde->handled != 0)
2016 continue;
2017 if (seek_next_seg && fde->cseg != ccseg)
2018 {
2019 seek_next_seg = 2;
2020 continue;
2021 }
2022 if (!seek_next_seg)
2023 {
2024 ccseg = fde->cseg;
2025 /* Open .debug_frame section. */
2026 get_cfi_seg (ccseg, ".debug_frame",
2027 SEC_READONLY | SEC_DEBUGGING,
2028 alignment);
2029 seek_next_seg = 1;
2030 }
2031 fde->handled = 1;
2032 #endif
2033 if (fde->end_address == NULL)
2034 {
2035 as_bad (_("open CFI at the end of file; missing .cfi_endproc directive"));
2036 fde->end_address = fde->start_address;
2037 }
2038
2039 fde->per_encoding = DW_EH_PE_omit;
2040 fde->lsda_encoding = DW_EH_PE_omit;
2041 cfi_change_reg_numbers (fde->data, ccseg);
2042 cie = select_cie_for_fde (fde, FALSE, &first, alignment);
2043 output_fde (fde, cie, FALSE, first, alignment);
2044 }
2045 }
2046 while (SUPPORT_FRAME_LINKONCE && seek_next_seg == 2);
2047
2048 #if SUPPORT_FRAME_LINKONCE
2049 for (fde = all_fde_data; fde ; fde = fde->next)
2050 fde->handled = 0;
2051 #endif
2052 }
2053 }
2054
2055 #else /* TARGET_USE_CFIPOP */
2056
2057 /* Emit an intelligible error message for missing support. */
2058
2059 static void
2060 dot_cfi_dummy (int ignored ATTRIBUTE_UNUSED)
2061 {
2062 as_bad (_("CFI is not supported for this target"));
2063 ignore_rest_of_line ();
2064 }
2065
2066 const pseudo_typeS cfi_pseudo_table[] =
2067 {
2068 { "cfi_sections", dot_cfi_dummy, 0 },
2069 { "cfi_startproc", dot_cfi_dummy, 0 },
2070 { "cfi_endproc", dot_cfi_dummy, 0 },
2071 { "cfi_def_cfa", dot_cfi_dummy, 0 },
2072 { "cfi_def_cfa_register", dot_cfi_dummy, 0 },
2073 { "cfi_def_cfa_offset", dot_cfi_dummy, 0 },
2074 { "cfi_adjust_cfa_offset", dot_cfi_dummy, 0 },
2075 { "cfi_offset", dot_cfi_dummy, 0 },
2076 { "cfi_rel_offset", dot_cfi_dummy, 0 },
2077 { "cfi_register", dot_cfi_dummy, 0 },
2078 { "cfi_return_column", dot_cfi_dummy, 0 },
2079 { "cfi_restore", dot_cfi_dummy, 0 },
2080 { "cfi_undefined", dot_cfi_dummy, 0 },
2081 { "cfi_same_value", dot_cfi_dummy, 0 },
2082 { "cfi_remember_state", dot_cfi_dummy, 0 },
2083 { "cfi_restore_state", dot_cfi_dummy, 0 },
2084 { "cfi_window_save", dot_cfi_dummy, 0 },
2085 { "cfi_escape", dot_cfi_dummy, 0 },
2086 { "cfi_signal_frame", dot_cfi_dummy, 0 },
2087 { "cfi_personality", dot_cfi_dummy, 0 },
2088 { "cfi_lsda", dot_cfi_dummy, 0 },
2089 { "cfi_val_encoded_addr", dot_cfi_dummy, 0 },
2090 { NULL, NULL, 0 }
2091 };
2092
2093 void
2094 cfi_finish (void)
2095 {
2096 }
2097 #endif /* TARGET_USE_CFIPOP */
This page took 0.070656 seconds and 3 git commands to generate.