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