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