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