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