]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gas/dw2gencfi.c
Update year range in copyright notice of binutils files
[thirdparty/binutils-gdb.git] / gas / dw2gencfi.c
1 /* dw2gencfi.c - Support for generating Dwarf2 CFI information.
2 Copyright (C) 2003-2023 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 3, 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, 51 Franklin Street - Fifth Floor, Boston, MA
20 02110-1301, USA. */
21
22 #include "as.h"
23 #include "dw2gencfi.h"
24 #include "subsegs.h"
25 #include "dwarf2dbg.h"
26 #include "gen-sframe.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 tc_cfi_startproc
72 # define tc_cfi_startproc() ((void)0)
73 #endif
74
75 #ifndef tc_cfi_endproc
76 # define tc_cfi_endproc(fde) ((void) (fde))
77 #endif
78
79 #define EH_FRAME_LINKONCE (SUPPORT_FRAME_LINKONCE || compact_eh \
80 || TARGET_MULTIPLE_EH_FRAME_SECTIONS)
81
82 #ifndef DWARF2_FORMAT
83 #define DWARF2_FORMAT(SEC) dwarf2_format_32bit
84 #endif
85
86 #ifndef DWARF2_ADDR_SIZE
87 #define DWARF2_ADDR_SIZE(bfd) (bfd_arch_bits_per_address (bfd) / 8)
88 #endif
89
90 #if MULTIPLE_FRAME_SECTIONS
91 #define CUR_SEG(structp) structp->cur_seg
92 #define SET_CUR_SEG(structp, seg) structp->cur_seg = seg
93 #define HANDLED(structp) structp->handled
94 #define SET_HANDLED(structp, val) structp->handled = val
95 #else
96 #define CUR_SEG(structp) NULL
97 #define SET_CUR_SEG(structp, seg) (void) (0 && seg)
98 #define HANDLED(structp) 0
99 #define SET_HANDLED(structp, val) (void) (0 && val)
100 #endif
101
102 #ifndef tc_cfi_reloc_for_encoding
103 #define tc_cfi_reloc_for_encoding(e) BFD_RELOC_NONE
104 #endif
105
106 /* Targets which support SFrame format will define this and return true. */
107 #ifndef support_sframe_p
108 # define support_sframe_p() false
109 #endif
110
111 /* Private segment collection list. */
112 struct dwcfi_seg_list
113 {
114 segT seg;
115 int subseg;
116 char * seg_name;
117 };
118
119 #ifdef SUPPORT_COMPACT_EH
120 static bool compact_eh;
121 #else
122 #define compact_eh 0
123 #endif
124
125 static htab_t dwcfi_hash;
126 \f
127 /* Emit a single byte into the current segment. */
128
129 static inline void
130 out_one (int byte)
131 {
132 FRAG_APPEND_1_CHAR (byte);
133 }
134
135 /* Emit a two-byte word into the current segment. */
136
137 static inline void
138 out_two (int data)
139 {
140 md_number_to_chars (frag_more (2), data, 2);
141 }
142
143 /* Emit a four byte word into the current segment. */
144
145 static inline void
146 out_four (int data)
147 {
148 md_number_to_chars (frag_more (4), data, 4);
149 }
150
151 /* Emit an unsigned "little-endian base 128" number. */
152
153 static void
154 out_uleb128 (addressT value)
155 {
156 output_leb128 (frag_more (sizeof_leb128 (value, 0)), value, 0);
157 }
158
159 /* Emit an unsigned "little-endian base 128" number. */
160
161 static void
162 out_sleb128 (offsetT value)
163 {
164 output_leb128 (frag_more (sizeof_leb128 (value, 1)), value, 1);
165 }
166
167 static unsigned int
168 encoding_size (unsigned char encoding)
169 {
170 if (encoding == DW_EH_PE_omit)
171 return 0;
172 switch (encoding & 0x7)
173 {
174 case 0:
175 return bfd_get_arch_size (stdoutput) == 64 ? 8 : 4;
176 case DW_EH_PE_udata2:
177 return 2;
178 case DW_EH_PE_udata4:
179 return 4;
180 case DW_EH_PE_udata8:
181 return 8;
182 default:
183 abort ();
184 }
185 }
186
187 /* Emit expression EXP in ENCODING. If EMIT_ENCODING is true, first
188 emit a byte containing ENCODING. */
189
190 static void
191 emit_expr_encoded (expressionS *exp, int encoding, bool emit_encoding)
192 {
193 unsigned int size = encoding_size (encoding);
194 bfd_reloc_code_real_type code;
195
196 if (encoding == DW_EH_PE_omit)
197 return;
198
199 if (emit_encoding)
200 out_one (encoding);
201
202 code = tc_cfi_reloc_for_encoding (encoding);
203 if (code != BFD_RELOC_NONE)
204 {
205 reloc_howto_type *howto = bfd_reloc_type_lookup (stdoutput, code);
206 char *p = frag_more (size);
207 gas_assert (size == (unsigned) howto->bitsize / 8);
208 md_number_to_chars (p, 0, size);
209 fix_new (frag_now, p - frag_now->fr_literal, size, exp->X_add_symbol,
210 exp->X_add_number, howto->pc_relative, code);
211 }
212 else if ((encoding & 0x70) == DW_EH_PE_pcrel)
213 {
214 #if CFI_DIFF_EXPR_OK
215 expressionS tmp = *exp;
216 tmp.X_op = O_subtract;
217 tmp.X_op_symbol = symbol_temp_new_now ();
218 emit_expr (&tmp, size);
219 #elif defined (tc_cfi_emit_pcrel_expr)
220 tc_cfi_emit_pcrel_expr (exp, size);
221 #else
222 abort ();
223 #endif
224 }
225 else
226 emit_expr (exp, size);
227 }
228 \f
229 /* Build based on segment the derived .debug_...
230 segment name containing origin segment's postfix name part. */
231
232 static char *
233 get_debugseg_name (segT seg, const char *base_name)
234 {
235 const char * name;
236 const char * dollar;
237 const char * dot;
238
239 if (!seg
240 || (name = bfd_section_name (seg)) == NULL
241 || *name == 0)
242 return notes_strdup (base_name);
243
244 dollar = strchr (name, '$');
245 dot = strchr (name + 1, '.');
246
247 if (!dollar && !dot)
248 {
249 if (!strcmp (base_name, ".eh_frame_entry")
250 && strcmp (name, ".text") != 0)
251 return notes_concat (base_name, ".", name, NULL);
252
253 name = "";
254 }
255 else if (!dollar)
256 name = dot;
257 else if (!dot)
258 name = dollar;
259 else if (dot < dollar)
260 name = dot;
261 else
262 name = dollar;
263
264 return notes_concat (base_name, name, NULL);
265 }
266
267 /* Allocate a dwcfi_seg_list structure. */
268
269 static struct dwcfi_seg_list *
270 alloc_debugseg_item (segT seg, int subseg, char *name)
271 {
272 struct dwcfi_seg_list *r;
273
274 r = notes_alloc (sizeof (*r) + strlen (name));
275 r->seg = seg;
276 r->subseg = subseg;
277 r->seg_name = name;
278 return r;
279 }
280
281 static segT
282 is_now_linkonce_segment (void)
283 {
284 if (compact_eh)
285 return now_seg;
286
287 if (TARGET_MULTIPLE_EH_FRAME_SECTIONS)
288 return now_seg;
289
290 if ((bfd_section_flags (now_seg)
291 & (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD
292 | SEC_LINK_DUPLICATES_ONE_ONLY | SEC_LINK_DUPLICATES_SAME_SIZE
293 | SEC_LINK_DUPLICATES_SAME_CONTENTS)) != 0)
294 return now_seg;
295 return NULL;
296 }
297
298 /* Generate debug... segment with same linkonce properties
299 of based segment. */
300
301 static segT
302 make_debug_seg (segT cseg, char *name, int sflags)
303 {
304 segT save_seg = now_seg;
305 int save_subseg = now_subseg;
306 segT r;
307 flagword flags;
308
309 r = subseg_new (name, 0);
310
311 /* Check if code segment is marked as linked once. */
312 if (!cseg)
313 flags = 0;
314 else
315 flags = (bfd_section_flags (cseg)
316 & (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD
317 | SEC_LINK_DUPLICATES_ONE_ONLY | SEC_LINK_DUPLICATES_SAME_SIZE
318 | SEC_LINK_DUPLICATES_SAME_CONTENTS));
319
320 /* Add standard section flags. */
321 flags |= sflags;
322
323 /* Apply possibly linked once flags to new generated segment, too. */
324 if (!bfd_set_section_flags (r, flags))
325 as_bad (_("bfd_set_section_flags: %s"),
326 bfd_errmsg (bfd_get_error ()));
327
328 /* Restore to previous segment. */
329 if (save_seg != NULL)
330 subseg_set (save_seg, save_subseg);
331 return r;
332 }
333
334 static struct dwcfi_seg_list *
335 dwcfi_hash_find (char *name)
336 {
337 return (struct dwcfi_seg_list *) str_hash_find (dwcfi_hash, name);
338 }
339
340 static struct dwcfi_seg_list *
341 dwcfi_hash_find_or_make (segT cseg, const char *base_name, int flags)
342 {
343 struct dwcfi_seg_list *item;
344 char *name;
345
346 /* Initialize dwcfi_hash once. */
347 if (!dwcfi_hash)
348 dwcfi_hash = str_htab_create ();
349
350 name = get_debugseg_name (cseg, base_name);
351
352 item = dwcfi_hash_find (name);
353 if (!item)
354 {
355 item = alloc_debugseg_item (make_debug_seg (cseg, name, flags), 0, name);
356
357 str_hash_insert (dwcfi_hash, item->seg_name, item, 0);
358 }
359 else
360 notes_free (name);
361
362 return item;
363 }
364
365 /* ??? Share this with dwarf2cfg.c. */
366 #ifndef TC_DWARF2_EMIT_OFFSET
367 #define TC_DWARF2_EMIT_OFFSET generic_dwarf2_emit_offset
368
369 /* Create an offset to .dwarf2_*. */
370
371 static void
372 generic_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
373 {
374 expressionS exp;
375
376 exp.X_op = O_symbol;
377 exp.X_add_symbol = symbol;
378 exp.X_add_number = 0;
379 emit_expr (&exp, size);
380 }
381 #endif
382
383 struct cfi_escape_data
384 {
385 struct cfi_escape_data *next;
386 expressionS exp;
387 };
388
389 struct cie_entry
390 {
391 struct cie_entry *next;
392 #if MULTIPLE_FRAME_SECTIONS
393 segT cur_seg;
394 #endif
395 symbolS *start_address;
396 unsigned int return_column;
397 unsigned int signal_frame;
398 unsigned char fde_encoding;
399 unsigned char per_encoding;
400 unsigned char lsda_encoding;
401 expressionS personality;
402 #ifdef tc_cie_entry_extras
403 tc_cie_entry_extras
404 #endif
405 struct cfi_insn_data *first, *last;
406 };
407
408 /* List of FDE entries. */
409
410 struct fde_entry *all_fde_data;
411 static struct fde_entry **last_fde_data = &all_fde_data;
412
413 /* List of CIEs so that they could be reused. */
414 static struct cie_entry *cie_root;
415
416 /* Construct a new FDE structure and add it to the end of the fde list. */
417
418 static struct fde_entry *
419 alloc_fde_entry (void)
420 {
421 struct fde_entry *fde = XCNEW (struct fde_entry);
422
423 frchain_now->frch_cfi_data = XCNEW (struct frch_cfi_data);
424 frchain_now->frch_cfi_data->cur_fde_data = fde;
425 *last_fde_data = fde;
426 last_fde_data = &fde->next;
427 SET_CUR_SEG (fde, is_now_linkonce_segment ());
428 SET_HANDLED (fde, 0);
429 fde->last = &fde->data;
430 fde->return_column = DWARF2_DEFAULT_RETURN_COLUMN;
431 fde->per_encoding = DW_EH_PE_omit;
432 fde->lsda_encoding = DW_EH_PE_omit;
433 fde->eh_header_type = EH_COMPACT_UNKNOWN;
434 #ifdef tc_fde_entry_init_extra
435 tc_fde_entry_init_extra (fde)
436 #endif
437
438 return fde;
439 }
440
441 /* The following functions are available for a backend to construct its
442 own unwind information, usually from legacy unwind directives. */
443
444 /* Construct a new INSN structure and add it to the end of the insn list
445 for the currently active FDE. */
446
447 static bool cfi_sections_set = false;
448 static int cfi_sections = CFI_EMIT_eh_frame;
449 int all_cfi_sections = 0;
450 static struct fde_entry *last_fde;
451
452 static struct cfi_insn_data *
453 alloc_cfi_insn_data (void)
454 {
455 struct cfi_insn_data *insn = XCNEW (struct cfi_insn_data);
456 struct fde_entry *cur_fde_data = frchain_now->frch_cfi_data->cur_fde_data;
457
458 *cur_fde_data->last = insn;
459 cur_fde_data->last = &insn->next;
460 SET_CUR_SEG (insn, is_now_linkonce_segment ());
461 return insn;
462 }
463
464 /* Construct a new FDE structure that begins at LABEL. */
465
466 void
467 cfi_new_fde (symbolS *label)
468 {
469 struct fde_entry *fde = alloc_fde_entry ();
470 fde->start_address = label;
471 frchain_now->frch_cfi_data->last_address = label;
472 }
473
474 /* End the currently open FDE. */
475
476 void
477 cfi_end_fde (symbolS *label)
478 {
479 frchain_now->frch_cfi_data->cur_fde_data->end_address = label;
480 free (frchain_now->frch_cfi_data);
481 frchain_now->frch_cfi_data = NULL;
482 }
483
484 /* Set the return column for the current FDE. */
485
486 void
487 cfi_set_return_column (unsigned regno)
488 {
489 frchain_now->frch_cfi_data->cur_fde_data->return_column = regno;
490 }
491
492 void
493 cfi_set_sections (void)
494 {
495 frchain_now->frch_cfi_data->cur_fde_data->sections = all_cfi_sections;
496 cfi_sections_set = true;
497 }
498
499 /* Universal functions to store new instructions. */
500
501 static void
502 cfi_add_CFA_insn (int insn)
503 {
504 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
505
506 insn_ptr->insn = insn;
507 }
508
509 static void
510 cfi_add_CFA_insn_reg (int insn, unsigned regno)
511 {
512 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
513
514 insn_ptr->insn = insn;
515 insn_ptr->u.r = regno;
516 }
517
518 static void
519 cfi_add_CFA_insn_offset (int insn, offsetT offset)
520 {
521 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
522
523 insn_ptr->insn = insn;
524 insn_ptr->u.i = offset;
525 }
526
527 static void
528 cfi_add_CFA_insn_reg_reg (int insn, unsigned reg1, unsigned reg2)
529 {
530 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
531
532 insn_ptr->insn = insn;
533 insn_ptr->u.rr.reg1 = reg1;
534 insn_ptr->u.rr.reg2 = reg2;
535 }
536
537 static void
538 cfi_add_CFA_insn_reg_offset (int insn, unsigned regno, offsetT offset)
539 {
540 struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data ();
541
542 insn_ptr->insn = insn;
543 insn_ptr->u.ri.reg = regno;
544 insn_ptr->u.ri.offset = offset;
545 }
546
547 /* Add a CFI insn to advance the PC from the last address to LABEL. */
548
549 void
550 cfi_add_advance_loc (symbolS *label)
551 {
552 struct cfi_insn_data *insn = alloc_cfi_insn_data ();
553
554 insn->insn = DW_CFA_advance_loc;
555 insn->u.ll.lab1 = frchain_now->frch_cfi_data->last_address;
556 insn->u.ll.lab2 = label;
557
558 frchain_now->frch_cfi_data->last_address = label;
559 }
560
561 /* Add a CFI insn to label the current position in the CFI segment. */
562
563 void
564 cfi_add_label (const char *name)
565 {
566 unsigned int len = strlen (name) + 1;
567 struct cfi_insn_data *insn = alloc_cfi_insn_data ();
568
569 insn->insn = CFI_label;
570 obstack_grow (&notes, name, len);
571 insn->u.sym_name = (char *) obstack_finish (&notes);
572 }
573
574 /* Add a DW_CFA_offset record to the CFI data. */
575
576 void
577 cfi_add_CFA_offset (unsigned regno, offsetT offset)
578 {
579 unsigned int abs_data_align;
580
581 gas_assert (DWARF2_CIE_DATA_ALIGNMENT != 0);
582 cfi_add_CFA_insn_reg_offset (DW_CFA_offset, regno, offset);
583
584 abs_data_align = (DWARF2_CIE_DATA_ALIGNMENT < 0
585 ? -DWARF2_CIE_DATA_ALIGNMENT : DWARF2_CIE_DATA_ALIGNMENT);
586 if (offset % abs_data_align)
587 as_bad (_("register save offset not a multiple of %u"), abs_data_align);
588 }
589
590 /* Add a DW_CFA_val_offset record to the CFI data. */
591
592 void
593 cfi_add_CFA_val_offset (unsigned regno, offsetT offset)
594 {
595 unsigned int abs_data_align;
596
597 gas_assert (DWARF2_CIE_DATA_ALIGNMENT != 0);
598 cfi_add_CFA_insn_reg_offset (DW_CFA_val_offset, regno, offset);
599
600 abs_data_align = (DWARF2_CIE_DATA_ALIGNMENT < 0
601 ? -DWARF2_CIE_DATA_ALIGNMENT : DWARF2_CIE_DATA_ALIGNMENT);
602 if (offset % abs_data_align)
603 as_bad (_("register save offset not a multiple of %u"), abs_data_align);
604 }
605
606 /* Add a DW_CFA_def_cfa record to the CFI data. */
607
608 void
609 cfi_add_CFA_def_cfa (unsigned regno, offsetT offset)
610 {
611 cfi_add_CFA_insn_reg_offset (DW_CFA_def_cfa, regno, offset);
612 frchain_now->frch_cfi_data->cur_cfa_offset = offset;
613 }
614
615 /* Add a DW_CFA_register record to the CFI data. */
616
617 void
618 cfi_add_CFA_register (unsigned reg1, unsigned reg2)
619 {
620 cfi_add_CFA_insn_reg_reg (DW_CFA_register, reg1, reg2);
621 }
622
623 /* Add a DW_CFA_def_cfa_register record to the CFI data. */
624
625 void
626 cfi_add_CFA_def_cfa_register (unsigned regno)
627 {
628 cfi_add_CFA_insn_reg (DW_CFA_def_cfa_register, regno);
629 }
630
631 /* Add a DW_CFA_def_cfa_offset record to the CFI data. */
632
633 void
634 cfi_add_CFA_def_cfa_offset (offsetT offset)
635 {
636 cfi_add_CFA_insn_offset (DW_CFA_def_cfa_offset, offset);
637 frchain_now->frch_cfi_data->cur_cfa_offset = offset;
638 }
639
640 void
641 cfi_add_CFA_restore (unsigned regno)
642 {
643 cfi_add_CFA_insn_reg (DW_CFA_restore, regno);
644 }
645
646 void
647 cfi_add_CFA_undefined (unsigned regno)
648 {
649 cfi_add_CFA_insn_reg (DW_CFA_undefined, regno);
650 }
651
652 void
653 cfi_add_CFA_same_value (unsigned regno)
654 {
655 cfi_add_CFA_insn_reg (DW_CFA_same_value, regno);
656 }
657
658 void
659 cfi_add_CFA_remember_state (void)
660 {
661 struct cfa_save_data *p;
662
663 cfi_add_CFA_insn (DW_CFA_remember_state);
664
665 p = XNEW (struct cfa_save_data);
666 p->cfa_offset = frchain_now->frch_cfi_data->cur_cfa_offset;
667 p->next = frchain_now->frch_cfi_data->cfa_save_stack;
668 frchain_now->frch_cfi_data->cfa_save_stack = p;
669 }
670
671 void
672 cfi_add_CFA_restore_state (void)
673 {
674 struct cfa_save_data *p;
675
676 cfi_add_CFA_insn (DW_CFA_restore_state);
677
678 p = frchain_now->frch_cfi_data->cfa_save_stack;
679 if (p)
680 {
681 frchain_now->frch_cfi_data->cur_cfa_offset = p->cfa_offset;
682 frchain_now->frch_cfi_data->cfa_save_stack = p->next;
683 free (p);
684 }
685 else
686 as_bad (_("CFI state restore without previous remember"));
687 }
688
689 \f
690 /* Parse CFI assembler directives. */
691
692 static void dot_cfi (int);
693 static void dot_cfi_escape (int);
694 static void dot_cfi_sections (int);
695 static void dot_cfi_startproc (int);
696 static void dot_cfi_endproc (int);
697 static void dot_cfi_fde_data (int);
698 static void dot_cfi_personality (int);
699 static void dot_cfi_personality_id (int);
700 static void dot_cfi_lsda (int);
701 static void dot_cfi_val_encoded_addr (int);
702 static void dot_cfi_inline_lsda (int);
703 static void dot_cfi_label (int);
704
705 const pseudo_typeS cfi_pseudo_table[] =
706 {
707 { "cfi_sections", dot_cfi_sections, 0 },
708 { "cfi_startproc", dot_cfi_startproc, 0 },
709 { "cfi_endproc", dot_cfi_endproc, 0 },
710 { "cfi_fde_data", dot_cfi_fde_data, 0 },
711 { "cfi_def_cfa", dot_cfi, DW_CFA_def_cfa },
712 { "cfi_def_cfa_register", dot_cfi, DW_CFA_def_cfa_register },
713 { "cfi_def_cfa_offset", dot_cfi, DW_CFA_def_cfa_offset },
714 { "cfi_adjust_cfa_offset", dot_cfi, CFI_adjust_cfa_offset },
715 { "cfi_offset", dot_cfi, DW_CFA_offset },
716 { "cfi_rel_offset", dot_cfi, CFI_rel_offset },
717 { "cfi_register", dot_cfi, DW_CFA_register },
718 { "cfi_return_column", dot_cfi, CFI_return_column },
719 { "cfi_restore", dot_cfi, DW_CFA_restore },
720 { "cfi_undefined", dot_cfi, DW_CFA_undefined },
721 { "cfi_same_value", dot_cfi, DW_CFA_same_value },
722 { "cfi_remember_state", dot_cfi, DW_CFA_remember_state },
723 { "cfi_restore_state", dot_cfi, DW_CFA_restore_state },
724 { "cfi_window_save", dot_cfi, DW_CFA_GNU_window_save },
725 { "cfi_negate_ra_state", dot_cfi, DW_CFA_AARCH64_negate_ra_state },
726 { "cfi_escape", dot_cfi_escape, 0 },
727 { "cfi_signal_frame", dot_cfi, CFI_signal_frame },
728 { "cfi_personality", dot_cfi_personality, 0 },
729 { "cfi_personality_id", dot_cfi_personality_id, 0 },
730 { "cfi_lsda", dot_cfi_lsda, 0 },
731 { "cfi_val_encoded_addr", dot_cfi_val_encoded_addr, 0 },
732 { "cfi_inline_lsda", dot_cfi_inline_lsda, 0 },
733 { "cfi_label", dot_cfi_label, 0 },
734 { "cfi_val_offset", dot_cfi, DW_CFA_val_offset },
735 { NULL, NULL, 0 }
736 };
737
738 static void
739 cfi_parse_separator (void)
740 {
741 SKIP_WHITESPACE ();
742 if (*input_line_pointer == ',')
743 input_line_pointer++;
744 else
745 as_bad (_("missing separator"));
746 }
747
748 #ifndef tc_parse_to_dw2regnum
749 static void
750 tc_parse_to_dw2regnum (expressionS *exp)
751 {
752 # ifdef tc_regname_to_dw2regnum
753 SKIP_WHITESPACE ();
754 if (is_name_beginner (*input_line_pointer)
755 || (*input_line_pointer == '%'
756 && is_name_beginner (*++input_line_pointer)))
757 {
758 char *name, c;
759
760 c = get_symbol_name (& name);
761
762 exp->X_op = O_constant;
763 exp->X_add_number = tc_regname_to_dw2regnum (name);
764
765 restore_line_pointer (c);
766 }
767 else
768 # endif
769 expression_and_evaluate (exp);
770 }
771 #endif
772
773 static unsigned
774 cfi_parse_reg (void)
775 {
776 int regno;
777 expressionS exp;
778
779 tc_parse_to_dw2regnum (&exp);
780 switch (exp.X_op)
781 {
782 case O_register:
783 case O_constant:
784 regno = exp.X_add_number;
785 break;
786
787 default:
788 regno = -1;
789 break;
790 }
791
792 if (regno < 0)
793 {
794 as_bad (_("bad register expression"));
795 regno = 0;
796 }
797
798 return regno;
799 }
800
801 static offsetT
802 cfi_parse_const (void)
803 {
804 return get_absolute_expression ();
805 }
806
807 static void
808 dot_cfi (int arg)
809 {
810 offsetT offset;
811 unsigned reg1, reg2;
812
813 if (frchain_now->frch_cfi_data == NULL)
814 {
815 as_bad (_("CFI instruction used without previous .cfi_startproc"));
816 ignore_rest_of_line ();
817 return;
818 }
819
820 /* If the last address was not at the current PC, advance to current. */
821 if (symbol_get_frag (frchain_now->frch_cfi_data->last_address) != frag_now
822 || (S_GET_VALUE (frchain_now->frch_cfi_data->last_address)
823 != frag_now_fix ()))
824 cfi_add_advance_loc (symbol_temp_new_now ());
825
826 switch (arg)
827 {
828 case DW_CFA_offset:
829 reg1 = cfi_parse_reg ();
830 cfi_parse_separator ();
831 offset = cfi_parse_const ();
832 cfi_add_CFA_offset (reg1, offset);
833 break;
834
835 case DW_CFA_val_offset:
836 reg1 = cfi_parse_reg ();
837 cfi_parse_separator ();
838 offset = cfi_parse_const ();
839 cfi_add_CFA_val_offset (reg1, offset);
840 break;
841
842 case CFI_rel_offset:
843 reg1 = cfi_parse_reg ();
844 cfi_parse_separator ();
845 offset = cfi_parse_const ();
846 cfi_add_CFA_offset (reg1,
847 offset - frchain_now->frch_cfi_data->cur_cfa_offset);
848 break;
849
850 case DW_CFA_def_cfa:
851 reg1 = cfi_parse_reg ();
852 cfi_parse_separator ();
853 offset = cfi_parse_const ();
854 cfi_add_CFA_def_cfa (reg1, offset);
855 break;
856
857 case DW_CFA_register:
858 reg1 = cfi_parse_reg ();
859 cfi_parse_separator ();
860 reg2 = cfi_parse_reg ();
861 cfi_add_CFA_register (reg1, reg2);
862 break;
863
864 case DW_CFA_def_cfa_register:
865 reg1 = cfi_parse_reg ();
866 cfi_add_CFA_def_cfa_register (reg1);
867 break;
868
869 case DW_CFA_def_cfa_offset:
870 offset = cfi_parse_const ();
871 cfi_add_CFA_def_cfa_offset (offset);
872 break;
873
874 case CFI_adjust_cfa_offset:
875 offset = cfi_parse_const ();
876 cfi_add_CFA_def_cfa_offset (frchain_now->frch_cfi_data->cur_cfa_offset
877 + offset);
878 break;
879
880 case DW_CFA_restore:
881 for (;;)
882 {
883 reg1 = cfi_parse_reg ();
884 cfi_add_CFA_restore (reg1);
885 SKIP_WHITESPACE ();
886 if (*input_line_pointer != ',')
887 break;
888 ++input_line_pointer;
889 }
890 break;
891
892 case DW_CFA_undefined:
893 for (;;)
894 {
895 reg1 = cfi_parse_reg ();
896 cfi_add_CFA_undefined (reg1);
897 SKIP_WHITESPACE ();
898 if (*input_line_pointer != ',')
899 break;
900 ++input_line_pointer;
901 }
902 break;
903
904 case DW_CFA_same_value:
905 reg1 = cfi_parse_reg ();
906 cfi_add_CFA_same_value (reg1);
907 break;
908
909 case CFI_return_column:
910 reg1 = cfi_parse_reg ();
911 cfi_set_return_column (reg1);
912 break;
913
914 case DW_CFA_remember_state:
915 cfi_add_CFA_remember_state ();
916 break;
917
918 case DW_CFA_restore_state:
919 cfi_add_CFA_restore_state ();
920 break;
921
922 case DW_CFA_GNU_window_save:
923 cfi_add_CFA_insn (DW_CFA_GNU_window_save);
924 break;
925
926 case CFI_signal_frame:
927 frchain_now->frch_cfi_data->cur_fde_data->signal_frame = 1;
928 break;
929
930 default:
931 abort ();
932 }
933
934 demand_empty_rest_of_line ();
935 }
936
937 static void
938 dot_cfi_escape (int ignored ATTRIBUTE_UNUSED)
939 {
940 struct cfi_escape_data *head, **tail, *e;
941 struct cfi_insn_data *insn;
942
943 if (frchain_now->frch_cfi_data == NULL)
944 {
945 as_bad (_("CFI instruction used without previous .cfi_startproc"));
946 ignore_rest_of_line ();
947 return;
948 }
949
950 /* If the last address was not at the current PC, advance to current. */
951 if (symbol_get_frag (frchain_now->frch_cfi_data->last_address) != frag_now
952 || (S_GET_VALUE (frchain_now->frch_cfi_data->last_address)
953 != frag_now_fix ()))
954 cfi_add_advance_loc (symbol_temp_new_now ());
955
956 tail = &head;
957 do
958 {
959 e = XNEW (struct cfi_escape_data);
960 do_parse_cons_expression (&e->exp, 1);
961 *tail = e;
962 tail = &e->next;
963 }
964 while (*input_line_pointer++ == ',');
965 *tail = NULL;
966
967 insn = alloc_cfi_insn_data ();
968 insn->insn = CFI_escape;
969 insn->u.esc = head;
970
971 --input_line_pointer;
972 demand_empty_rest_of_line ();
973 }
974
975 static void
976 dot_cfi_personality (int ignored ATTRIBUTE_UNUSED)
977 {
978 struct fde_entry *fde;
979 offsetT encoding;
980
981 if (frchain_now->frch_cfi_data == NULL)
982 {
983 as_bad (_("CFI instruction used without previous .cfi_startproc"));
984 ignore_rest_of_line ();
985 return;
986 }
987
988 fde = frchain_now->frch_cfi_data->cur_fde_data;
989 encoding = cfi_parse_const ();
990 if (encoding == DW_EH_PE_omit)
991 {
992 demand_empty_rest_of_line ();
993 fde->per_encoding = encoding;
994 return;
995 }
996
997 if ((encoding & 0xff) != encoding
998 || ((((encoding & 0x70) != 0
999 #if CFI_DIFF_EXPR_OK || defined tc_cfi_emit_pcrel_expr
1000 && (encoding & 0x70) != DW_EH_PE_pcrel
1001 #endif
1002 )
1003 /* leb128 can be handled, but does something actually need it? */
1004 || (encoding & 7) == DW_EH_PE_uleb128
1005 || (encoding & 7) > DW_EH_PE_udata8)
1006 && tc_cfi_reloc_for_encoding (encoding) == BFD_RELOC_NONE))
1007 {
1008 as_bad (_("invalid or unsupported encoding in .cfi_personality"));
1009 ignore_rest_of_line ();
1010 return;
1011 }
1012
1013 if (*input_line_pointer++ != ',')
1014 {
1015 as_bad (_(".cfi_personality requires encoding and symbol arguments"));
1016 ignore_rest_of_line ();
1017 return;
1018 }
1019
1020 expression_and_evaluate (&fde->personality);
1021 switch (fde->personality.X_op)
1022 {
1023 case O_symbol:
1024 break;
1025 case O_constant:
1026 if ((encoding & 0x70) == DW_EH_PE_pcrel)
1027 encoding = DW_EH_PE_omit;
1028 break;
1029 default:
1030 encoding = DW_EH_PE_omit;
1031 break;
1032 }
1033
1034 fde->per_encoding = encoding;
1035
1036 if (encoding == DW_EH_PE_omit)
1037 {
1038 as_bad (_("wrong second argument to .cfi_personality"));
1039 ignore_rest_of_line ();
1040 return;
1041 }
1042
1043 demand_empty_rest_of_line ();
1044 }
1045
1046 static void
1047 dot_cfi_lsda (int ignored ATTRIBUTE_UNUSED)
1048 {
1049 struct fde_entry *fde;
1050 offsetT encoding;
1051
1052 if (frchain_now->frch_cfi_data == NULL)
1053 {
1054 as_bad (_("CFI instruction used without previous .cfi_startproc"));
1055 ignore_rest_of_line ();
1056 return;
1057 }
1058
1059 fde = frchain_now->frch_cfi_data->cur_fde_data;
1060 encoding = cfi_parse_const ();
1061 if (encoding == DW_EH_PE_omit)
1062 {
1063 demand_empty_rest_of_line ();
1064 fde->lsda_encoding = encoding;
1065 return;
1066 }
1067
1068 if ((encoding & 0xff) != encoding
1069 || ((((encoding & 0x70) != 0
1070 #if CFI_DIFF_LSDA_OK || defined tc_cfi_emit_pcrel_expr
1071 && (encoding & 0x70) != DW_EH_PE_pcrel
1072 #endif
1073 )
1074 /* leb128 can be handled, but does something actually need it? */
1075 || (encoding & 7) == DW_EH_PE_uleb128
1076 || (encoding & 7) > DW_EH_PE_udata8)
1077 && tc_cfi_reloc_for_encoding (encoding) == BFD_RELOC_NONE))
1078 {
1079 as_bad (_("invalid or unsupported encoding in .cfi_lsda"));
1080 ignore_rest_of_line ();
1081 return;
1082 }
1083
1084 if (*input_line_pointer++ != ',')
1085 {
1086 as_bad (_(".cfi_lsda requires encoding and symbol arguments"));
1087 ignore_rest_of_line ();
1088 return;
1089 }
1090
1091 fde->lsda_encoding = encoding;
1092
1093 expression_and_evaluate (&fde->lsda);
1094 switch (fde->lsda.X_op)
1095 {
1096 case O_symbol:
1097 break;
1098 case O_constant:
1099 if ((encoding & 0x70) == DW_EH_PE_pcrel)
1100 encoding = DW_EH_PE_omit;
1101 break;
1102 default:
1103 encoding = DW_EH_PE_omit;
1104 break;
1105 }
1106
1107 fde->lsda_encoding = encoding;
1108
1109 if (encoding == DW_EH_PE_omit)
1110 {
1111 as_bad (_("wrong second argument to .cfi_lsda"));
1112 ignore_rest_of_line ();
1113 return;
1114 }
1115
1116 demand_empty_rest_of_line ();
1117 }
1118
1119 static void
1120 dot_cfi_val_encoded_addr (int ignored ATTRIBUTE_UNUSED)
1121 {
1122 struct cfi_insn_data *insn_ptr;
1123 offsetT encoding;
1124
1125 if (frchain_now->frch_cfi_data == NULL)
1126 {
1127 as_bad (_("CFI instruction used without previous .cfi_startproc"));
1128 ignore_rest_of_line ();
1129 return;
1130 }
1131
1132 /* If the last address was not at the current PC, advance to current. */
1133 if (symbol_get_frag (frchain_now->frch_cfi_data->last_address) != frag_now
1134 || (S_GET_VALUE (frchain_now->frch_cfi_data->last_address)
1135 != frag_now_fix ()))
1136 cfi_add_advance_loc (symbol_temp_new_now ());
1137
1138 insn_ptr = alloc_cfi_insn_data ();
1139 insn_ptr->insn = CFI_val_encoded_addr;
1140
1141 insn_ptr->u.ea.reg = cfi_parse_reg ();
1142
1143 cfi_parse_separator ();
1144 encoding = cfi_parse_const ();
1145 if ((encoding & 0xff) != encoding
1146 || ((encoding & 0x70) != 0
1147 #if CFI_DIFF_EXPR_OK || defined tc_cfi_emit_pcrel_expr
1148 && (encoding & 0x70) != DW_EH_PE_pcrel
1149 #endif
1150 )
1151 /* leb128 can be handled, but does something actually need it? */
1152 || (encoding & 7) == DW_EH_PE_uleb128
1153 || (encoding & 7) > DW_EH_PE_udata8)
1154 {
1155 as_bad (_("invalid or unsupported encoding in .cfi_lsda"));
1156 encoding = DW_EH_PE_omit;
1157 }
1158
1159 cfi_parse_separator ();
1160 expression_and_evaluate (&insn_ptr->u.ea.exp);
1161 switch (insn_ptr->u.ea.exp.X_op)
1162 {
1163 case O_symbol:
1164 break;
1165 case O_constant:
1166 if ((encoding & 0x70) != DW_EH_PE_pcrel)
1167 break;
1168 /* Fall through. */
1169 default:
1170 encoding = DW_EH_PE_omit;
1171 break;
1172 }
1173
1174 insn_ptr->u.ea.encoding = encoding;
1175 if (encoding == DW_EH_PE_omit)
1176 {
1177 as_bad (_("wrong third argument to .cfi_val_encoded_addr"));
1178 ignore_rest_of_line ();
1179 return;
1180 }
1181
1182 demand_empty_rest_of_line ();
1183 }
1184
1185 static void
1186 dot_cfi_label (int ignored ATTRIBUTE_UNUSED)
1187 {
1188 char *name;
1189
1190 if (frchain_now->frch_cfi_data == NULL)
1191 {
1192 as_bad (_("CFI instruction used without previous .cfi_startproc"));
1193 ignore_rest_of_line ();
1194 return;
1195 }
1196
1197 name = read_symbol_name ();
1198 if (name == NULL)
1199 return;
1200
1201 /* If the last address was not at the current PC, advance to current. */
1202 if (symbol_get_frag (frchain_now->frch_cfi_data->last_address) != frag_now
1203 || (S_GET_VALUE (frchain_now->frch_cfi_data->last_address)
1204 != frag_now_fix ()))
1205 cfi_add_advance_loc (symbol_temp_new_now ());
1206
1207 cfi_add_label (name);
1208 free (name);
1209
1210 demand_empty_rest_of_line ();
1211 }
1212
1213 static void
1214 dot_cfi_sections (int ignored ATTRIBUTE_UNUSED)
1215 {
1216 int sections = 0;
1217
1218 SKIP_WHITESPACE ();
1219 if (is_name_beginner (*input_line_pointer) || *input_line_pointer == '"')
1220 while (1)
1221 {
1222 char * saved_ilp;
1223 char *name, c;
1224
1225 saved_ilp = input_line_pointer;
1226 c = get_symbol_name (& name);
1227
1228 if (startswith (name, ".eh_frame")
1229 && name[9] != '_')
1230 sections |= CFI_EMIT_eh_frame;
1231 else if (startswith (name, ".debug_frame"))
1232 sections |= CFI_EMIT_debug_frame;
1233 #if SUPPORT_COMPACT_EH
1234 else if (startswith (name, ".eh_frame_entry"))
1235 {
1236 compact_eh = true;
1237 sections |= CFI_EMIT_eh_frame_compact;
1238 }
1239 #endif
1240 #ifdef tc_cfi_section_name
1241 else if (strcmp (name, tc_cfi_section_name) == 0)
1242 sections |= CFI_EMIT_target;
1243 #endif
1244 else if (startswith (name, ".sframe"))
1245 sections |= CFI_EMIT_sframe;
1246 else
1247 {
1248 *input_line_pointer = c;
1249 input_line_pointer = saved_ilp;
1250 break;
1251 }
1252
1253 *input_line_pointer = c;
1254 SKIP_WHITESPACE_AFTER_NAME ();
1255 if (*input_line_pointer == ',')
1256 {
1257 name = input_line_pointer++;
1258 SKIP_WHITESPACE ();
1259 if (!is_name_beginner (*input_line_pointer)
1260 && *input_line_pointer != '"')
1261 {
1262 input_line_pointer = name;
1263 break;
1264 }
1265 }
1266 else if (is_name_beginner (*input_line_pointer)
1267 || *input_line_pointer == '"')
1268 break;
1269 }
1270
1271 demand_empty_rest_of_line ();
1272 if (cfi_sections_set
1273 && (sections & (CFI_EMIT_eh_frame | CFI_EMIT_eh_frame_compact))
1274 && ((cfi_sections & (CFI_EMIT_eh_frame | CFI_EMIT_eh_frame_compact))
1275 != (sections & (CFI_EMIT_eh_frame | CFI_EMIT_eh_frame_compact))))
1276 as_bad (_("inconsistent uses of .cfi_sections"));
1277 cfi_sections = sections;
1278 }
1279
1280 static void
1281 dot_cfi_startproc (int ignored ATTRIBUTE_UNUSED)
1282 {
1283 int simple = 0;
1284
1285 if (frchain_now->frch_cfi_data != NULL)
1286 {
1287 as_bad (_("previous CFI entry not closed (missing .cfi_endproc)"));
1288 ignore_rest_of_line ();
1289 return;
1290 }
1291
1292 cfi_new_fde (symbol_temp_new_now ());
1293
1294 SKIP_WHITESPACE ();
1295 if (is_name_beginner (*input_line_pointer) || *input_line_pointer == '"')
1296 {
1297 char * saved_ilp = input_line_pointer;
1298 char *name, c;
1299
1300 c = get_symbol_name (& name);
1301
1302 if (strcmp (name, "simple") == 0)
1303 {
1304 simple = 1;
1305 restore_line_pointer (c);
1306 }
1307 else
1308 input_line_pointer = saved_ilp;
1309 }
1310 demand_empty_rest_of_line ();
1311
1312 cfi_sections_set = true;
1313 all_cfi_sections |= cfi_sections;
1314 cfi_set_sections ();
1315 frchain_now->frch_cfi_data->cur_cfa_offset = 0;
1316 if (!simple)
1317 tc_cfi_frame_initial_instructions ();
1318
1319 if ((cfi_sections & CFI_EMIT_target) != 0)
1320 tc_cfi_startproc ();
1321 }
1322
1323 static void
1324 dot_cfi_endproc (int ignored ATTRIBUTE_UNUSED)
1325 {
1326 if (frchain_now->frch_cfi_data == NULL)
1327 {
1328 as_bad (_(".cfi_endproc without corresponding .cfi_startproc"));
1329 ignore_rest_of_line ();
1330 return;
1331 }
1332
1333 last_fde = frchain_now->frch_cfi_data->cur_fde_data;
1334
1335 cfi_end_fde (symbol_temp_new_now ());
1336
1337 demand_empty_rest_of_line ();
1338
1339 cfi_sections_set = true;
1340 if ((cfi_sections & CFI_EMIT_target) != 0)
1341 tc_cfi_endproc (last_fde);
1342 }
1343
1344 static segT
1345 get_cfi_seg (segT cseg, const char *base, flagword flags, int align)
1346 {
1347 /* Exclude .debug_frame sections for Compact EH. */
1348 if (SUPPORT_FRAME_LINKONCE || ((flags & SEC_DEBUGGING) == 0 && compact_eh)
1349 || ((flags & SEC_DEBUGGING) == 0 && TARGET_MULTIPLE_EH_FRAME_SECTIONS))
1350 {
1351 segT iseg = cseg;
1352 struct dwcfi_seg_list *l;
1353
1354 l = dwcfi_hash_find_or_make (cseg, base, flags);
1355
1356 cseg = l->seg;
1357 subseg_set (cseg, l->subseg);
1358
1359 if (TARGET_MULTIPLE_EH_FRAME_SECTIONS
1360 && (flags & DWARF2_EH_FRAME_READ_ONLY))
1361 {
1362 const frchainS *ifrch = seg_info (iseg)->frchainP;
1363 const frchainS *frch = seg_info (cseg)->frchainP;
1364 expressionS exp;
1365
1366 exp.X_op = O_symbol;
1367 exp.X_add_symbol = (symbolS *) local_symbol_make (cseg->name, cseg, frch->frch_root, 0);
1368 exp.X_add_number = 0;
1369 subseg_set (iseg, ifrch->frch_subseg);
1370 fix_new_exp (ifrch->frch_root, 0, 0, &exp, 0, BFD_RELOC_NONE);
1371
1372 /* Restore the original segment info. */
1373 subseg_set (cseg, l->subseg);
1374 }
1375 }
1376 else
1377 {
1378 cseg = subseg_new (base, 0);
1379 bfd_set_section_flags (cseg, flags);
1380 }
1381 record_alignment (cseg, align);
1382 return cseg;
1383 }
1384
1385 #if SUPPORT_COMPACT_EH
1386 static void
1387 dot_cfi_personality_id (int ignored ATTRIBUTE_UNUSED)
1388 {
1389 struct fde_entry *fde;
1390
1391 if (frchain_now->frch_cfi_data == NULL)
1392 {
1393 as_bad (_("CFI instruction used without previous .cfi_startproc"));
1394 ignore_rest_of_line ();
1395 return;
1396 }
1397
1398 fde = frchain_now->frch_cfi_data->cur_fde_data;
1399 fde->personality_id = cfi_parse_const ();
1400 demand_empty_rest_of_line ();
1401
1402 if (fde->personality_id == 0 || fde->personality_id > 3)
1403 {
1404 as_bad (_("wrong argument to .cfi_personality_id"));
1405 return;
1406 }
1407 }
1408
1409 static void
1410 dot_cfi_fde_data (int ignored ATTRIBUTE_UNUSED)
1411 {
1412 if (frchain_now->frch_cfi_data == NULL)
1413 {
1414 as_bad (_(".cfi_fde_data without corresponding .cfi_startproc"));
1415 ignore_rest_of_line ();
1416 return;
1417 }
1418
1419 last_fde = frchain_now->frch_cfi_data->cur_fde_data;
1420
1421 cfi_sections_set = true;
1422 if ((cfi_sections & CFI_EMIT_target) != 0
1423 || (cfi_sections & CFI_EMIT_eh_frame_compact) != 0)
1424 {
1425 struct cfi_escape_data *head, **tail, *e;
1426 int num_ops = 0;
1427
1428 tail = &head;
1429 if (!is_it_end_of_statement ())
1430 {
1431 num_ops = 0;
1432 do
1433 {
1434 e = XNEW (struct cfi_escape_data);
1435 do_parse_cons_expression (&e->exp, 1);
1436 *tail = e;
1437 tail = &e->next;
1438 num_ops++;
1439 }
1440 while (*input_line_pointer++ == ',');
1441 --input_line_pointer;
1442 }
1443 *tail = NULL;
1444
1445 if (last_fde->lsda_encoding != DW_EH_PE_omit)
1446 last_fde->eh_header_type = EH_COMPACT_HAS_LSDA;
1447 else if (num_ops <= 3 && last_fde->per_encoding == DW_EH_PE_omit)
1448 last_fde->eh_header_type = EH_COMPACT_INLINE;
1449 else
1450 last_fde->eh_header_type = EH_COMPACT_OUTLINE;
1451
1452 if (last_fde->eh_header_type == EH_COMPACT_INLINE)
1453 num_ops = 3;
1454
1455 last_fde->eh_data_size = num_ops;
1456 last_fde->eh_data = XNEWVEC (bfd_byte, num_ops);
1457 num_ops = 0;
1458 while (head)
1459 {
1460 e = head;
1461 head = e->next;
1462 last_fde->eh_data[num_ops++] = e->exp.X_add_number;
1463 free (e);
1464 }
1465 if (last_fde->eh_header_type == EH_COMPACT_INLINE)
1466 while (num_ops < 3)
1467 last_fde->eh_data[num_ops++] = tc_compact_eh_opcode_stop;
1468 }
1469
1470 demand_empty_rest_of_line ();
1471 }
1472
1473 /* Function to emit the compact unwinding opcodes stored in the
1474 fde's eh_data field. The end of the opcode data will be
1475 padded to the value in align. */
1476
1477 static void
1478 output_compact_unwind_data (struct fde_entry *fde, int align)
1479 {
1480 int data_size = fde->eh_data_size + 2;
1481 int align_padding;
1482 int amask;
1483 char *p;
1484
1485 fde->eh_loc = symbol_temp_new_now ();
1486
1487 p = frag_more (1);
1488 if (fde->personality_id != 0)
1489 *p = fde->personality_id;
1490 else if (fde->per_encoding != DW_EH_PE_omit)
1491 {
1492 *p = 0;
1493 emit_expr_encoded (&fde->personality, fde->per_encoding, false);
1494 data_size += encoding_size (fde->per_encoding);
1495 }
1496 else
1497 *p = 1;
1498
1499 amask = (1 << align) - 1;
1500 align_padding = ((data_size + amask) & ~amask) - data_size;
1501
1502 p = frag_more (fde->eh_data_size + 1 + align_padding);
1503 memcpy (p, fde->eh_data, fde->eh_data_size);
1504 p += fde->eh_data_size;
1505
1506 while (align_padding-- > 0)
1507 *(p++) = tc_compact_eh_opcode_pad;
1508
1509 *(p++) = tc_compact_eh_opcode_stop;
1510 fde->eh_header_type = EH_COMPACT_OUTLINE_DONE;
1511 }
1512
1513 /* Handle the .cfi_inline_lsda directive. */
1514 static void
1515 dot_cfi_inline_lsda (int ignored ATTRIBUTE_UNUSED)
1516 {
1517 segT ccseg;
1518 int align;
1519 long max_alignment = 28;
1520
1521 if (!last_fde)
1522 {
1523 as_bad (_("unexpected .cfi_inline_lsda"));
1524 ignore_rest_of_line ();
1525 return;
1526 }
1527
1528 if ((last_fde->sections & CFI_EMIT_eh_frame_compact) == 0)
1529 {
1530 as_bad (_(".cfi_inline_lsda not valid for this frame"));
1531 ignore_rest_of_line ();
1532 return;
1533 }
1534
1535 if (last_fde->eh_header_type != EH_COMPACT_UNKNOWN
1536 && last_fde->eh_header_type != EH_COMPACT_HAS_LSDA)
1537 {
1538 as_bad (_(".cfi_inline_lsda seen for frame without .cfi_lsda"));
1539 ignore_rest_of_line ();
1540 return;
1541 }
1542
1543 #ifdef md_flush_pending_output
1544 md_flush_pending_output ();
1545 #endif
1546
1547 align = get_absolute_expression ();
1548 if (align > max_alignment)
1549 {
1550 align = max_alignment;
1551 as_bad (_("Alignment too large: %d. assumed."), align);
1552 }
1553 else if (align < 0)
1554 {
1555 as_warn (_("Alignment negative: 0 assumed."));
1556 align = 0;
1557 }
1558
1559 demand_empty_rest_of_line ();
1560 ccseg = CUR_SEG (last_fde);
1561
1562 /* Open .gnu_extab section. */
1563 get_cfi_seg (ccseg, ".gnu_extab",
1564 (SEC_ALLOC | SEC_LOAD | SEC_DATA
1565 | DWARF2_EH_FRAME_READ_ONLY),
1566 1);
1567
1568 frag_align (align, 0, 0);
1569 record_alignment (now_seg, align);
1570 if (last_fde->eh_header_type == EH_COMPACT_HAS_LSDA)
1571 output_compact_unwind_data (last_fde, align);
1572
1573 last_fde = NULL;
1574
1575 return;
1576 }
1577 #else /* !SUPPORT_COMPACT_EH */
1578 static void
1579 dot_cfi_inline_lsda (int ignored ATTRIBUTE_UNUSED)
1580 {
1581 as_bad (_(".cfi_inline_lsda is not supported for this target"));
1582 ignore_rest_of_line ();
1583 }
1584
1585 static void
1586 dot_cfi_fde_data (int ignored ATTRIBUTE_UNUSED)
1587 {
1588 as_bad (_(".cfi_fde_data is not supported for this target"));
1589 ignore_rest_of_line ();
1590 }
1591
1592 static void
1593 dot_cfi_personality_id (int ignored ATTRIBUTE_UNUSED)
1594 {
1595 as_bad (_(".cfi_personality_id is not supported for this target"));
1596 ignore_rest_of_line ();
1597 }
1598 #endif
1599 \f
1600 static void
1601 output_cfi_insn (struct cfi_insn_data *insn)
1602 {
1603 offsetT offset;
1604 unsigned int regno;
1605
1606 switch (insn->insn)
1607 {
1608 case DW_CFA_advance_loc:
1609 {
1610 symbolS *from = insn->u.ll.lab1;
1611 symbolS *to = insn->u.ll.lab2;
1612
1613 if (symbol_get_frag (to) == symbol_get_frag (from))
1614 {
1615 addressT delta = S_GET_VALUE (to) - S_GET_VALUE (from);
1616 addressT scaled = delta / DWARF2_LINE_MIN_INSN_LENGTH;
1617
1618 if (scaled == 0)
1619 ;
1620 else if (scaled <= 0x3F)
1621 out_one (DW_CFA_advance_loc + scaled);
1622 else if (scaled <= 0xFF)
1623 {
1624 out_one (DW_CFA_advance_loc1);
1625 out_one (scaled);
1626 }
1627 else if (scaled <= 0xFFFF)
1628 {
1629 out_one (DW_CFA_advance_loc2);
1630 out_two (scaled);
1631 }
1632 else
1633 {
1634 out_one (DW_CFA_advance_loc4);
1635 out_four (scaled);
1636 }
1637 }
1638 else
1639 {
1640 expressionS exp;
1641
1642 exp.X_op = O_subtract;
1643 exp.X_add_symbol = to;
1644 exp.X_op_symbol = from;
1645 exp.X_add_number = 0;
1646
1647 /* The code in ehopt.c expects that one byte of the encoding
1648 is already allocated to the frag. This comes from the way
1649 that it scans the .eh_frame section looking first for the
1650 .byte DW_CFA_advance_loc4. Call frag_grow with the sum of
1651 room needed by frag_more and frag_var to preallocate space
1652 ensuring that the DW_CFA_advance_loc4 is in the fixed part
1653 of the rs_cfa frag, so that the relax machinery can remove
1654 the advance_loc should it advance by zero. */
1655 frag_grow (5);
1656 *frag_more (1) = DW_CFA_advance_loc4;
1657
1658 frag_var (rs_cfa, 4, 0, DWARF2_LINE_MIN_INSN_LENGTH << 3,
1659 make_expr_symbol (&exp), frag_now_fix () - 1,
1660 (char *) frag_now);
1661 }
1662 }
1663 break;
1664
1665 case DW_CFA_def_cfa:
1666 offset = insn->u.ri.offset;
1667 if (offset < 0)
1668 {
1669 out_one (DW_CFA_def_cfa_sf);
1670 out_uleb128 (insn->u.ri.reg);
1671 out_sleb128 (offset / DWARF2_CIE_DATA_ALIGNMENT);
1672 }
1673 else
1674 {
1675 out_one (DW_CFA_def_cfa);
1676 out_uleb128 (insn->u.ri.reg);
1677 out_uleb128 (offset);
1678 }
1679 break;
1680
1681 case DW_CFA_def_cfa_register:
1682 case DW_CFA_undefined:
1683 case DW_CFA_same_value:
1684 out_one (insn->insn);
1685 out_uleb128 (insn->u.r);
1686 break;
1687
1688 case DW_CFA_def_cfa_offset:
1689 offset = insn->u.i;
1690 if (offset < 0)
1691 {
1692 out_one (DW_CFA_def_cfa_offset_sf);
1693 out_sleb128 (offset / DWARF2_CIE_DATA_ALIGNMENT);
1694 }
1695 else
1696 {
1697 out_one (DW_CFA_def_cfa_offset);
1698 out_uleb128 (offset);
1699 }
1700 break;
1701
1702 case DW_CFA_restore:
1703 regno = insn->u.r;
1704 if (regno <= 0x3F)
1705 {
1706 out_one (DW_CFA_restore + regno);
1707 }
1708 else
1709 {
1710 out_one (DW_CFA_restore_extended);
1711 out_uleb128 (regno);
1712 }
1713 break;
1714
1715 case DW_CFA_offset:
1716 regno = insn->u.ri.reg;
1717 offset = insn->u.ri.offset / DWARF2_CIE_DATA_ALIGNMENT;
1718 if (offset < 0)
1719 {
1720 out_one (DW_CFA_offset_extended_sf);
1721 out_uleb128 (regno);
1722 out_sleb128 (offset);
1723 }
1724 else if (regno <= 0x3F)
1725 {
1726 out_one (DW_CFA_offset + regno);
1727 out_uleb128 (offset);
1728 }
1729 else
1730 {
1731 out_one (DW_CFA_offset_extended);
1732 out_uleb128 (regno);
1733 out_uleb128 (offset);
1734 }
1735 break;
1736
1737 case DW_CFA_val_offset:
1738 regno = insn->u.ri.reg;
1739 offset = insn->u.ri.offset / DWARF2_CIE_DATA_ALIGNMENT;
1740 if (offset < 0)
1741 {
1742 out_one (DW_CFA_val_offset_sf);
1743 out_uleb128 (regno);
1744 out_sleb128 (offset);
1745 }
1746 else
1747 {
1748 out_one (DW_CFA_val_offset);
1749 out_uleb128 (regno);
1750 out_uleb128 (offset);
1751 }
1752 break;
1753
1754 case DW_CFA_register:
1755 out_one (DW_CFA_register);
1756 out_uleb128 (insn->u.rr.reg1);
1757 out_uleb128 (insn->u.rr.reg2);
1758 break;
1759
1760 case DW_CFA_remember_state:
1761 case DW_CFA_restore_state:
1762 out_one (insn->insn);
1763 break;
1764
1765 case DW_CFA_GNU_window_save:
1766 out_one (DW_CFA_GNU_window_save);
1767 break;
1768
1769 case CFI_escape:
1770 {
1771 struct cfi_escape_data *e;
1772 for (e = insn->u.esc; e ; e = e->next)
1773 emit_expr (&e->exp, 1);
1774 break;
1775 }
1776
1777 case CFI_val_encoded_addr:
1778 {
1779 unsigned encoding = insn->u.ea.encoding;
1780 offsetT enc_size;
1781
1782 if (encoding == DW_EH_PE_omit)
1783 break;
1784 out_one (DW_CFA_val_expression);
1785 out_uleb128 (insn->u.ea.reg);
1786
1787 switch (encoding & 0x7)
1788 {
1789 case DW_EH_PE_absptr:
1790 enc_size = DWARF2_ADDR_SIZE (stdoutput);
1791 break;
1792 case DW_EH_PE_udata2:
1793 enc_size = 2;
1794 break;
1795 case DW_EH_PE_udata4:
1796 enc_size = 4;
1797 break;
1798 case DW_EH_PE_udata8:
1799 enc_size = 8;
1800 break;
1801 default:
1802 abort ();
1803 }
1804
1805 /* If the user has requested absolute encoding,
1806 then use the smaller DW_OP_addr encoding. */
1807 if (insn->u.ea.encoding == DW_EH_PE_absptr)
1808 {
1809 out_uleb128 (1 + enc_size);
1810 out_one (DW_OP_addr);
1811 }
1812 else
1813 {
1814 out_uleb128 (1 + 1 + enc_size);
1815 out_one (DW_OP_GNU_encoded_addr);
1816 out_one (encoding);
1817
1818 if ((encoding & 0x70) == DW_EH_PE_pcrel)
1819 {
1820 #if CFI_DIFF_EXPR_OK
1821 insn->u.ea.exp.X_op = O_subtract;
1822 insn->u.ea.exp.X_op_symbol = symbol_temp_new_now ();
1823 #elif defined (tc_cfi_emit_pcrel_expr)
1824 tc_cfi_emit_pcrel_expr (&insn->u.ea.exp, enc_size);
1825 break;
1826 #else
1827 abort ();
1828 #endif
1829 }
1830 }
1831 emit_expr (&insn->u.ea.exp, enc_size);
1832 }
1833 break;
1834
1835 case CFI_label:
1836 colon (insn->u.sym_name);
1837 break;
1838
1839 default:
1840 abort ();
1841 }
1842 }
1843
1844 static void
1845 output_cie (struct cie_entry *cie, bool eh_frame, int align)
1846 {
1847 symbolS *after_size_address, *end_address;
1848 expressionS exp;
1849 struct cfi_insn_data *i;
1850 offsetT augmentation_size;
1851 int enc;
1852 enum dwarf2_format fmt = DWARF2_FORMAT (now_seg);
1853
1854 cie->start_address = symbol_temp_new_now ();
1855 after_size_address = symbol_temp_make ();
1856 end_address = symbol_temp_make ();
1857
1858 exp.X_op = O_subtract;
1859 exp.X_add_symbol = end_address;
1860 exp.X_op_symbol = after_size_address;
1861 exp.X_add_number = 0;
1862
1863 if (eh_frame || fmt == dwarf2_format_32bit)
1864 emit_expr (&exp, 4); /* Length. */
1865 else
1866 {
1867 if (fmt == dwarf2_format_64bit)
1868 out_four (-1);
1869 emit_expr (&exp, 8); /* Length. */
1870 }
1871 symbol_set_value_now (after_size_address);
1872 if (eh_frame)
1873 out_four (0); /* CIE id. */
1874 else
1875 {
1876 out_four (-1); /* CIE id. */
1877 if (fmt != dwarf2_format_32bit)
1878 out_four (-1);
1879 }
1880 out_one (flag_dwarf_cie_version); /* Version. */
1881 if (eh_frame)
1882 {
1883 out_one ('z'); /* Augmentation. */
1884 if (cie->per_encoding != DW_EH_PE_omit)
1885 out_one ('P');
1886 if (cie->lsda_encoding != DW_EH_PE_omit)
1887 out_one ('L');
1888 out_one ('R');
1889 #ifdef tc_output_cie_extra
1890 tc_output_cie_extra (cie);
1891 #endif
1892 }
1893 if (cie->signal_frame)
1894 out_one ('S');
1895 out_one (0);
1896 if (flag_dwarf_cie_version >= 4)
1897 {
1898 /* For now we are assuming a flat address space with 4 or 8 byte
1899 addresses. */
1900 int address_size = dwarf2_format_32bit ? 4 : 8;
1901 out_one (address_size); /* Address size. */
1902 out_one (0); /* Segment size. */
1903 }
1904 out_uleb128 (DWARF2_LINE_MIN_INSN_LENGTH); /* Code alignment. */
1905 out_sleb128 (DWARF2_CIE_DATA_ALIGNMENT); /* Data alignment. */
1906 if (flag_dwarf_cie_version == 1) /* Return column. */
1907 {
1908 if ((cie->return_column & 0xff) != cie->return_column)
1909 as_bad (_("return column number %d overflows in CIE version 1"),
1910 cie->return_column);
1911 out_one (cie->return_column);
1912 }
1913 else
1914 out_uleb128 (cie->return_column);
1915 if (eh_frame)
1916 {
1917 augmentation_size = 1 + (cie->lsda_encoding != DW_EH_PE_omit);
1918 if (cie->per_encoding != DW_EH_PE_omit)
1919 augmentation_size += 1 + encoding_size (cie->per_encoding);
1920 out_uleb128 (augmentation_size); /* Augmentation size. */
1921
1922 emit_expr_encoded (&cie->personality, cie->per_encoding, true);
1923
1924 if (cie->lsda_encoding != DW_EH_PE_omit)
1925 out_one (cie->lsda_encoding);
1926 }
1927
1928 switch (DWARF2_FDE_RELOC_SIZE)
1929 {
1930 case 2:
1931 enc = DW_EH_PE_sdata2;
1932 break;
1933 case 4:
1934 enc = DW_EH_PE_sdata4;
1935 break;
1936 case 8:
1937 enc = DW_EH_PE_sdata8;
1938 break;
1939 default:
1940 abort ();
1941 }
1942 #if CFI_DIFF_EXPR_OK || defined tc_cfi_emit_pcrel_expr
1943 enc |= DW_EH_PE_pcrel;
1944 #endif
1945 #ifdef DWARF2_FDE_RELOC_ENCODING
1946 /* Allow target to override encoding. */
1947 enc = DWARF2_FDE_RELOC_ENCODING (enc);
1948 #endif
1949 cie->fde_encoding = enc;
1950 if (eh_frame)
1951 out_one (enc);
1952
1953 if (cie->first)
1954 {
1955 for (i = cie->first; i != cie->last; i = i->next)
1956 {
1957 if (CUR_SEG (i) != CUR_SEG (cie))
1958 continue;
1959 output_cfi_insn (i);
1960 }
1961 }
1962
1963 frag_align (align, DW_CFA_nop, 0);
1964 symbol_set_value_now (end_address);
1965 }
1966
1967 static void
1968 output_fde (struct fde_entry *fde, struct cie_entry *cie,
1969 bool eh_frame, struct cfi_insn_data *first,
1970 int align)
1971 {
1972 symbolS *after_size_address, *end_address;
1973 expressionS exp;
1974 offsetT augmentation_size;
1975 enum dwarf2_format fmt = DWARF2_FORMAT (now_seg);
1976 unsigned int offset_size;
1977 unsigned int addr_size;
1978
1979 after_size_address = symbol_temp_make ();
1980 end_address = symbol_temp_make ();
1981
1982 exp.X_op = O_subtract;
1983 exp.X_add_symbol = end_address;
1984 exp.X_op_symbol = after_size_address;
1985 exp.X_add_number = 0;
1986 if (eh_frame || fmt == dwarf2_format_32bit)
1987 offset_size = 4;
1988 else
1989 {
1990 if (fmt == dwarf2_format_64bit)
1991 out_four (-1);
1992 offset_size = 8;
1993 }
1994 emit_expr (&exp, offset_size); /* Length. */
1995 symbol_set_value_now (after_size_address);
1996
1997 if (eh_frame)
1998 {
1999 exp.X_op = O_subtract;
2000 exp.X_add_symbol = after_size_address;
2001 exp.X_op_symbol = cie->start_address;
2002 exp.X_add_number = 0;
2003 emit_expr (&exp, offset_size); /* CIE offset. */
2004 }
2005 else
2006 {
2007 TC_DWARF2_EMIT_OFFSET (cie->start_address, offset_size);
2008 }
2009
2010 exp.X_op = O_symbol;
2011 if (eh_frame)
2012 {
2013 bfd_reloc_code_real_type code
2014 = tc_cfi_reloc_for_encoding (cie->fde_encoding);
2015 addr_size = DWARF2_FDE_RELOC_SIZE;
2016 if (code != BFD_RELOC_NONE)
2017 {
2018 reloc_howto_type *howto = bfd_reloc_type_lookup (stdoutput, code);
2019 char *p = frag_more (addr_size);
2020 gas_assert (addr_size == (unsigned) howto->bitsize / 8);
2021 md_number_to_chars (p, 0, addr_size);
2022 fix_new (frag_now, p - frag_now->fr_literal, addr_size,
2023 fde->start_address, 0, howto->pc_relative, code);
2024 }
2025 else
2026 {
2027 exp.X_op = O_subtract;
2028 exp.X_add_number = 0;
2029 #if CFI_DIFF_EXPR_OK
2030 exp.X_add_symbol = fde->start_address;
2031 exp.X_op_symbol = symbol_temp_new_now ();
2032 emit_expr (&exp, addr_size); /* Code offset. */
2033 #else
2034 exp.X_op = O_symbol;
2035 exp.X_add_symbol = fde->start_address;
2036
2037 #if defined(tc_cfi_emit_pcrel_expr)
2038 tc_cfi_emit_pcrel_expr (&exp, addr_size); /* Code offset. */
2039 #else
2040 emit_expr (&exp, addr_size); /* Code offset. */
2041 #endif
2042 #endif
2043 }
2044 }
2045 else
2046 {
2047 exp.X_add_number = 0;
2048 exp.X_add_symbol = fde->start_address;
2049 addr_size = DWARF2_ADDR_SIZE (stdoutput);
2050 emit_expr (&exp, addr_size);
2051 }
2052
2053 exp.X_op = O_subtract;
2054 exp.X_add_symbol = fde->end_address;
2055 exp.X_op_symbol = fde->start_address; /* Code length. */
2056 exp.X_add_number = 0;
2057 emit_expr (&exp, addr_size);
2058
2059 augmentation_size = encoding_size (fde->lsda_encoding);
2060 if (eh_frame)
2061 out_uleb128 (augmentation_size); /* Augmentation size. */
2062
2063 emit_expr_encoded (&fde->lsda, cie->lsda_encoding, false);
2064
2065 for (; first; first = first->next)
2066 if (CUR_SEG (first) == CUR_SEG (fde))
2067 output_cfi_insn (first);
2068
2069 frag_align (align, DW_CFA_nop, 0);
2070 symbol_set_value_now (end_address);
2071 }
2072
2073 /* Allow these insns to be put in the initial sequence of a CIE.
2074 If J is non-NULL, then compare I and J insns for a match. */
2075
2076 static inline bool
2077 initial_cie_insn (const struct cfi_insn_data *i, const struct cfi_insn_data *j)
2078 {
2079 if (j && i->insn != j->insn)
2080 return false;
2081 switch (i->insn)
2082 {
2083 case DW_CFA_offset:
2084 case DW_CFA_def_cfa:
2085 case DW_CFA_val_offset:
2086 if (j)
2087 {
2088 if (i->u.ri.reg != j->u.ri.reg)
2089 return false;
2090 if (i->u.ri.offset != j->u.ri.offset)
2091 return false;
2092 }
2093 break;
2094
2095 case DW_CFA_register:
2096 if (j)
2097 {
2098 if (i->u.rr.reg1 != j->u.rr.reg1)
2099 return false;
2100 if (i->u.rr.reg2 != j->u.rr.reg2)
2101 return false;
2102 }
2103 break;
2104
2105 case DW_CFA_def_cfa_register:
2106 case DW_CFA_restore:
2107 case DW_CFA_undefined:
2108 case DW_CFA_same_value:
2109 if (j)
2110 {
2111 if (i->u.r != j->u.r)
2112 return false;
2113 }
2114 break;
2115
2116 case DW_CFA_def_cfa_offset:
2117 if (j)
2118 {
2119 if (i->u.i != j->u.i)
2120 return false;
2121 }
2122 break;
2123
2124 default:
2125 return false;
2126 }
2127 return true;
2128 }
2129
2130 static struct cie_entry *
2131 select_cie_for_fde (struct fde_entry *fde, bool eh_frame,
2132 struct cfi_insn_data **pfirst, int align)
2133 {
2134 struct cfi_insn_data *i, *j;
2135 struct cie_entry *cie;
2136
2137 for (cie = cie_root; cie; cie = cie->next)
2138 {
2139 if (CUR_SEG (cie) != CUR_SEG (fde))
2140 continue;
2141 #ifdef tc_cie_fde_equivalent_extra
2142 if (!tc_cie_fde_equivalent_extra (cie, fde))
2143 continue;
2144 #endif
2145 if (cie->return_column != fde->return_column
2146 || cie->signal_frame != fde->signal_frame
2147 || cie->per_encoding != fde->per_encoding
2148 || cie->lsda_encoding != fde->lsda_encoding)
2149 continue;
2150 if (cie->per_encoding != DW_EH_PE_omit)
2151 {
2152 if (cie->personality.X_op != fde->personality.X_op
2153 || (cie->personality.X_add_number
2154 != fde->personality.X_add_number))
2155 continue;
2156 switch (cie->personality.X_op)
2157 {
2158 case O_constant:
2159 if (cie->personality.X_unsigned != fde->personality.X_unsigned)
2160 continue;
2161 break;
2162 case O_symbol:
2163 if (cie->personality.X_add_symbol
2164 != fde->personality.X_add_symbol)
2165 continue;
2166 break;
2167 default:
2168 abort ();
2169 }
2170 }
2171 for (i = cie->first, j = fde->data;
2172 i != cie->last && j != NULL;
2173 i = i->next, j = j->next)
2174 {
2175 if (!initial_cie_insn (i, j))
2176 break;
2177 }
2178
2179 if (i == cie->last)
2180 {
2181 *pfirst = j;
2182 return cie;
2183 }
2184 }
2185
2186 cie = XNEW (struct cie_entry);
2187 cie->next = cie_root;
2188 cie_root = cie;
2189 SET_CUR_SEG (cie, CUR_SEG (fde));
2190 cie->return_column = fde->return_column;
2191 cie->signal_frame = fde->signal_frame;
2192 cie->per_encoding = fde->per_encoding;
2193 cie->lsda_encoding = fde->lsda_encoding;
2194 cie->personality = fde->personality;
2195 cie->first = fde->data;
2196 #ifdef tc_cie_entry_init_extra
2197 tc_cie_entry_init_extra (cie, fde)
2198 #endif
2199
2200 for (i = cie->first; i ; i = i->next)
2201 if (!initial_cie_insn (i, NULL))
2202 break;
2203
2204 cie->last = i;
2205 *pfirst = i;
2206
2207 output_cie (cie, eh_frame, align);
2208
2209 return cie;
2210 }
2211
2212 #ifdef md_reg_eh_frame_to_debug_frame
2213 static void
2214 cfi_change_reg_numbers (struct cfi_insn_data *insn, segT ccseg)
2215 {
2216 for (; insn; insn = insn->next)
2217 {
2218 if (CUR_SEG (insn) != ccseg)
2219 continue;
2220 switch (insn->insn)
2221 {
2222 case DW_CFA_advance_loc:
2223 case DW_CFA_def_cfa_offset:
2224 case DW_CFA_remember_state:
2225 case DW_CFA_restore_state:
2226 case DW_CFA_GNU_window_save:
2227 case CFI_escape:
2228 case CFI_label:
2229 break;
2230
2231 case DW_CFA_def_cfa:
2232 case DW_CFA_offset:
2233 insn->u.ri.reg = md_reg_eh_frame_to_debug_frame (insn->u.ri.reg);
2234 break;
2235
2236 case DW_CFA_def_cfa_register:
2237 case DW_CFA_undefined:
2238 case DW_CFA_same_value:
2239 case DW_CFA_restore:
2240 insn->u.r = md_reg_eh_frame_to_debug_frame (insn->u.r);
2241 break;
2242
2243 case DW_CFA_register:
2244 insn->u.rr.reg1 = md_reg_eh_frame_to_debug_frame (insn->u.rr.reg1);
2245 insn->u.rr.reg2 = md_reg_eh_frame_to_debug_frame (insn->u.rr.reg2);
2246 break;
2247
2248 case CFI_val_encoded_addr:
2249 insn->u.ea.reg = md_reg_eh_frame_to_debug_frame (insn->u.ea.reg);
2250 break;
2251
2252 default:
2253 abort ();
2254 }
2255 }
2256 }
2257 #else
2258 #define cfi_change_reg_numbers(insn, cseg) do { } while (0)
2259 #endif
2260
2261 #if SUPPORT_COMPACT_EH
2262 static void
2263 cfi_emit_eh_header (symbolS *sym, bfd_vma addend)
2264 {
2265 expressionS exp;
2266
2267 exp.X_add_number = addend;
2268 exp.X_add_symbol = sym;
2269 emit_expr_encoded (&exp, DW_EH_PE_sdata4 | DW_EH_PE_pcrel, false);
2270 }
2271
2272 static void
2273 output_eh_header (struct fde_entry *fde)
2274 {
2275 char *p;
2276 bfd_vma addend;
2277
2278 if (fde->eh_header_type == EH_COMPACT_INLINE)
2279 addend = 0;
2280 else
2281 addend = 1;
2282
2283 cfi_emit_eh_header (fde->start_address, addend);
2284
2285 if (fde->eh_header_type == EH_COMPACT_INLINE)
2286 {
2287 p = frag_more (4);
2288 /* Inline entries always use PR1. */
2289 *(p++) = 1;
2290 memcpy(p, fde->eh_data, 3);
2291 }
2292 else
2293 {
2294 if (fde->eh_header_type == EH_COMPACT_LEGACY)
2295 addend = 1;
2296 else if (fde->eh_header_type == EH_COMPACT_OUTLINE
2297 || fde->eh_header_type == EH_COMPACT_OUTLINE_DONE)
2298 addend = 0;
2299 else
2300 abort ();
2301 cfi_emit_eh_header (fde->eh_loc, addend);
2302 }
2303 }
2304 #endif
2305
2306 void
2307 cfi_finish (void)
2308 {
2309 struct cie_entry *cie, *cie_next;
2310 segT cfi_seg, ccseg;
2311 struct fde_entry *fde;
2312 struct cfi_insn_data *first;
2313 int save_flag_traditional_format, seek_next_seg;
2314
2315 if (all_fde_data == 0)
2316 return;
2317
2318 cfi_sections_set = true;
2319 if ((all_cfi_sections & CFI_EMIT_eh_frame) != 0
2320 || (all_cfi_sections & CFI_EMIT_eh_frame_compact) != 0)
2321 {
2322 /* Make sure check_eh_frame doesn't do anything with our output. */
2323 save_flag_traditional_format = flag_traditional_format;
2324 flag_traditional_format = 1;
2325
2326 if (!EH_FRAME_LINKONCE)
2327 {
2328 /* Open .eh_frame section. */
2329 cfi_seg = get_cfi_seg (NULL, ".eh_frame",
2330 (SEC_ALLOC | SEC_LOAD | SEC_DATA
2331 | DWARF2_EH_FRAME_READ_ONLY),
2332 EH_FRAME_ALIGNMENT);
2333 #ifdef md_fix_up_eh_frame
2334 md_fix_up_eh_frame (cfi_seg);
2335 #else
2336 (void) cfi_seg;
2337 #endif
2338 }
2339
2340 do
2341 {
2342 ccseg = NULL;
2343 seek_next_seg = 0;
2344
2345 for (cie = cie_root; cie; cie = cie_next)
2346 {
2347 cie_next = cie->next;
2348 free ((void *) cie);
2349 }
2350 cie_root = NULL;
2351
2352 for (fde = all_fde_data; fde ; fde = fde->next)
2353 {
2354 if ((fde->sections & CFI_EMIT_eh_frame) == 0
2355 && (fde->sections & CFI_EMIT_eh_frame_compact) == 0)
2356 continue;
2357
2358 #if SUPPORT_COMPACT_EH
2359 /* Emit a LEGACY format header if we have processed all
2360 of the .cfi directives without encountering either inline or
2361 out-of-line compact unwinding opcodes. */
2362 if (fde->eh_header_type == EH_COMPACT_HAS_LSDA
2363 || fde->eh_header_type == EH_COMPACT_UNKNOWN)
2364 fde->eh_header_type = EH_COMPACT_LEGACY;
2365
2366 if (fde->eh_header_type != EH_COMPACT_LEGACY)
2367 continue;
2368 #endif
2369 if (EH_FRAME_LINKONCE)
2370 {
2371 if (HANDLED (fde))
2372 continue;
2373 if (seek_next_seg && CUR_SEG (fde) != ccseg)
2374 {
2375 seek_next_seg = 2;
2376 continue;
2377 }
2378 if (!seek_next_seg)
2379 {
2380 ccseg = CUR_SEG (fde);
2381 /* Open .eh_frame section. */
2382 cfi_seg = get_cfi_seg (ccseg, ".eh_frame",
2383 (SEC_ALLOC | SEC_LOAD | SEC_DATA
2384 | DWARF2_EH_FRAME_READ_ONLY),
2385 EH_FRAME_ALIGNMENT);
2386 #ifdef md_fix_up_eh_frame
2387 md_fix_up_eh_frame (cfi_seg);
2388 #else
2389 (void) cfi_seg;
2390 #endif
2391 seek_next_seg = 1;
2392 }
2393 SET_HANDLED (fde, 1);
2394 }
2395
2396 if (fde->end_address == NULL)
2397 {
2398 as_bad (_("open CFI at the end of file; "
2399 "missing .cfi_endproc directive"));
2400 fde->end_address = fde->start_address;
2401 }
2402
2403 cie = select_cie_for_fde (fde, true, &first, 2);
2404 fde->eh_loc = symbol_temp_new_now ();
2405 output_fde (fde, cie, true, first,
2406 fde->next == NULL ? EH_FRAME_ALIGNMENT : 2);
2407 }
2408 }
2409 while (EH_FRAME_LINKONCE && seek_next_seg == 2);
2410
2411 if (EH_FRAME_LINKONCE)
2412 for (fde = all_fde_data; fde ; fde = fde->next)
2413 SET_HANDLED (fde, 0);
2414
2415 #if SUPPORT_COMPACT_EH
2416 if (compact_eh)
2417 {
2418 /* Create remaining out of line table entries. */
2419 do
2420 {
2421 ccseg = NULL;
2422 seek_next_seg = 0;
2423
2424 for (fde = all_fde_data; fde ; fde = fde->next)
2425 {
2426 if ((fde->sections & CFI_EMIT_eh_frame) == 0
2427 && (fde->sections & CFI_EMIT_eh_frame_compact) == 0)
2428 continue;
2429
2430 if (fde->eh_header_type != EH_COMPACT_OUTLINE)
2431 continue;
2432 if (HANDLED (fde))
2433 continue;
2434 if (seek_next_seg && CUR_SEG (fde) != ccseg)
2435 {
2436 seek_next_seg = 2;
2437 continue;
2438 }
2439 if (!seek_next_seg)
2440 {
2441 ccseg = CUR_SEG (fde);
2442 /* Open .gnu_extab section. */
2443 get_cfi_seg (ccseg, ".gnu_extab",
2444 (SEC_ALLOC | SEC_LOAD | SEC_DATA
2445 | DWARF2_EH_FRAME_READ_ONLY),
2446 1);
2447 seek_next_seg = 1;
2448 }
2449 SET_HANDLED (fde, 1);
2450
2451 frag_align (1, 0, 0);
2452 record_alignment (now_seg, 1);
2453 output_compact_unwind_data (fde, 1);
2454 }
2455 }
2456 while (EH_FRAME_LINKONCE && seek_next_seg == 2);
2457
2458 for (fde = all_fde_data; fde ; fde = fde->next)
2459 SET_HANDLED (fde, 0);
2460
2461 /* Create index table fragments. */
2462 do
2463 {
2464 ccseg = NULL;
2465 seek_next_seg = 0;
2466
2467 for (fde = all_fde_data; fde ; fde = fde->next)
2468 {
2469 if ((fde->sections & CFI_EMIT_eh_frame) == 0
2470 && (fde->sections & CFI_EMIT_eh_frame_compact) == 0)
2471 continue;
2472
2473 if (HANDLED (fde))
2474 continue;
2475 if (seek_next_seg && CUR_SEG (fde) != ccseg)
2476 {
2477 seek_next_seg = 2;
2478 continue;
2479 }
2480 if (!seek_next_seg)
2481 {
2482 ccseg = CUR_SEG (fde);
2483 /* Open .eh_frame_entry section. */
2484 cfi_seg = get_cfi_seg (ccseg, ".eh_frame_entry",
2485 (SEC_ALLOC | SEC_LOAD | SEC_DATA
2486 | DWARF2_EH_FRAME_READ_ONLY),
2487 2);
2488 seek_next_seg = 1;
2489 }
2490 SET_HANDLED (fde, 1);
2491
2492 output_eh_header (fde);
2493 }
2494 }
2495 while (seek_next_seg == 2);
2496
2497 for (fde = all_fde_data; fde ; fde = fde->next)
2498 SET_HANDLED (fde, 0);
2499 }
2500 #endif /* SUPPORT_COMPACT_EH */
2501
2502 flag_traditional_format = save_flag_traditional_format;
2503 }
2504
2505 cfi_sections_set = true;
2506 /* Generate SFrame section if the user specifies:
2507 - the command line option to gas, or
2508 - .sframe in the .cfi_sections directive. */
2509 if (flag_gen_sframe || (all_cfi_sections & CFI_EMIT_sframe) != 0)
2510 {
2511 if (support_sframe_p ())
2512 {
2513 segT sframe_seg;
2514 int alignment = ffs (DWARF2_ADDR_SIZE (stdoutput)) - 1;
2515
2516 if (!SUPPORT_FRAME_LINKONCE)
2517 sframe_seg = get_cfi_seg (NULL, ".sframe",
2518 (SEC_ALLOC | SEC_LOAD | SEC_DATA
2519 | DWARF2_EH_FRAME_READ_ONLY),
2520 alignment);
2521 output_sframe (sframe_seg);
2522 }
2523 else
2524 as_bad (_(".sframe not supported for target"));
2525 }
2526
2527 cfi_sections_set = true;
2528 if ((all_cfi_sections & CFI_EMIT_debug_frame) != 0)
2529 {
2530 int alignment = ffs (DWARF2_ADDR_SIZE (stdoutput)) - 1;
2531
2532 if (!SUPPORT_FRAME_LINKONCE)
2533 get_cfi_seg (NULL, ".debug_frame",
2534 SEC_READONLY | SEC_DEBUGGING,
2535 alignment);
2536
2537 do
2538 {
2539 ccseg = NULL;
2540 seek_next_seg = 0;
2541
2542 for (cie = cie_root; cie; cie = cie_next)
2543 {
2544 cie_next = cie->next;
2545 free ((void *) cie);
2546 }
2547 cie_root = NULL;
2548
2549 for (fde = all_fde_data; fde ; fde = fde->next)
2550 {
2551 if ((fde->sections & CFI_EMIT_debug_frame) == 0)
2552 continue;
2553
2554 if (SUPPORT_FRAME_LINKONCE)
2555 {
2556 if (HANDLED (fde))
2557 continue;
2558 if (seek_next_seg && CUR_SEG (fde) != ccseg)
2559 {
2560 seek_next_seg = 2;
2561 continue;
2562 }
2563 if (!seek_next_seg)
2564 {
2565 ccseg = CUR_SEG (fde);
2566 /* Open .debug_frame section. */
2567 get_cfi_seg (ccseg, ".debug_frame",
2568 SEC_READONLY | SEC_DEBUGGING,
2569 alignment);
2570 seek_next_seg = 1;
2571 }
2572 SET_HANDLED (fde, 1);
2573 }
2574 if (fde->end_address == NULL)
2575 {
2576 as_bad (_("open CFI at the end of file; "
2577 "missing .cfi_endproc directive"));
2578 fde->end_address = fde->start_address;
2579 }
2580
2581 fde->per_encoding = DW_EH_PE_omit;
2582 fde->lsda_encoding = DW_EH_PE_omit;
2583 cfi_change_reg_numbers (fde->data, ccseg);
2584 cie = select_cie_for_fde (fde, false, &first, alignment);
2585 output_fde (fde, cie, false, first, alignment);
2586 }
2587 }
2588 while (SUPPORT_FRAME_LINKONCE && seek_next_seg == 2);
2589
2590 if (SUPPORT_FRAME_LINKONCE)
2591 for (fde = all_fde_data; fde ; fde = fde->next)
2592 SET_HANDLED (fde, 0);
2593 }
2594 if (dwcfi_hash)
2595 htab_delete (dwcfi_hash);
2596 }
2597
2598 #else /* TARGET_USE_CFIPOP */
2599
2600 /* Emit an intelligible error message for missing support. */
2601
2602 static void
2603 dot_cfi_dummy (int ignored ATTRIBUTE_UNUSED)
2604 {
2605 as_bad (_("CFI is not supported for this target"));
2606 ignore_rest_of_line ();
2607 }
2608
2609 const pseudo_typeS cfi_pseudo_table[] =
2610 {
2611 { "cfi_sections", dot_cfi_dummy, 0 },
2612 { "cfi_startproc", dot_cfi_dummy, 0 },
2613 { "cfi_endproc", dot_cfi_dummy, 0 },
2614 { "cfi_fde_data", dot_cfi_dummy, 0 },
2615 { "cfi_def_cfa", dot_cfi_dummy, 0 },
2616 { "cfi_def_cfa_register", dot_cfi_dummy, 0 },
2617 { "cfi_def_cfa_offset", dot_cfi_dummy, 0 },
2618 { "cfi_adjust_cfa_offset", dot_cfi_dummy, 0 },
2619 { "cfi_offset", dot_cfi_dummy, 0 },
2620 { "cfi_rel_offset", dot_cfi_dummy, 0 },
2621 { "cfi_register", dot_cfi_dummy, 0 },
2622 { "cfi_return_column", dot_cfi_dummy, 0 },
2623 { "cfi_restore", dot_cfi_dummy, 0 },
2624 { "cfi_undefined", dot_cfi_dummy, 0 },
2625 { "cfi_same_value", dot_cfi_dummy, 0 },
2626 { "cfi_remember_state", dot_cfi_dummy, 0 },
2627 { "cfi_restore_state", dot_cfi_dummy, 0 },
2628 { "cfi_window_save", dot_cfi_dummy, 0 },
2629 { "cfi_escape", dot_cfi_dummy, 0 },
2630 { "cfi_signal_frame", dot_cfi_dummy, 0 },
2631 { "cfi_personality", dot_cfi_dummy, 0 },
2632 { "cfi_personality_id", dot_cfi_dummy, 0 },
2633 { "cfi_lsda", dot_cfi_dummy, 0 },
2634 { "cfi_val_encoded_addr", dot_cfi_dummy, 0 },
2635 { "cfi_label", dot_cfi_dummy, 0 },
2636 { "cfi_inline_lsda", dot_cfi_dummy, 0 },
2637 { "cfi_val_offset", dot_cfi_dummy, 0 },
2638 { NULL, NULL, 0 }
2639 };
2640
2641 void
2642 cfi_finish (void)
2643 {
2644 }
2645 #endif /* TARGET_USE_CFIPOP */