1 /* Functions for generic Darwin as target machine for GNU C compiler. 2 Copyright (C) 1989-2019 Free Software Foundation, Inc. 3 Contributed by Apple Computer Inc. 4 5 This file is part of GCC. 6 7 GCC 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 GCC 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 GCC; see the file COPYING3. If not see 19 <http://www.gnu.org/licenses/>. */ 20 21 #define IN_TARGET_CODE 1 22 23 #include "config.h" 24 #include "system.h" 25 #include "coretypes.h" 26 #include "backend.h" 27 #include "target.h" 28 #include "rtl.h" 29 #include "tree.h" 30 #include "gimple.h" 31 #include "cfghooks.h" 32 #include "df.h" 33 #include "memmodel.h" 34 #include "tm_p.h" 35 #include "stringpool.h" 36 #include "attribs.h" 37 #include "insn-config.h" 38 #include "emit-rtl.h" 39 #include "cgraph.h" 40 #include "lto-streamer.h" 41 #include "output.h" 42 #include "varasm.h" 43 #include "stor-layout.h" 44 #include "explow.h" 45 #include "expr.h" 46 #include "langhooks.h" 47 #include "toplev.h" 48 #include "lto-section-names.h" 49 #include "intl.h" 50 51 /* Darwin supports a feature called fix-and-continue, which is used 52 for rapid turn around debugging. When code is compiled with the 53 -mfix-and-continue flag, two changes are made to the generated code 54 that allow the system to do things that it would normally not be 55 able to do easily. These changes allow gdb to load in 56 recompilation of a translation unit that has been changed into a 57 running program and replace existing functions and methods of that 58 translation unit with versions of those functions and methods 59 from the newly compiled translation unit. The new functions access 60 the existing static symbols from the old translation unit, if the 61 symbol existed in the unit to be replaced, and from the new 62 translation unit, otherwise. 63 64 The changes are to insert 5 nops at the beginning of all functions 65 and to use indirection to get at static symbols. The 5 nops 66 are required by consumers of the generated code. Currently, gdb 67 uses this to patch in a jump to the overriding function, this 68 allows all uses of the old name to forward to the replacement, 69 including existing function pointers and virtual methods. See 70 rs6000_emit_prologue for the code that handles the nop insertions. 71 72 The added indirection allows gdb to redirect accesses to static 73 symbols from the newly loaded translation unit to the existing 74 symbol, if any. @code{static} symbols are special and are handled by 75 setting the second word in the .non_lazy_symbol_pointer data 76 structure to symbol. See indirect_data for the code that handles 77 the extra indirection, and machopic_output_indirection and its use 78 of MACHO_SYMBOL_FLAG_STATIC for the code that handles @code{static} 79 symbol indirection. */ 80 81 typedef struct GTY(()) cdtor_record { 82 rtx symbol; 83 int priority; /* [con/de]structor priority */ 84 int position; /* original position */ 85 } cdtor_record; 86 87 static GTY(()) vec<cdtor_record, va_gc> *ctors = NULL; 88 static GTY(()) vec<cdtor_record, va_gc> *dtors = NULL; 89 90 /* A flag to determine whether we are running c++ or obj-c++. This has to be 91 settable from non-c-family contexts too (i.e. we can't use the c_dialect_ 92 functions). */ 93 int darwin_running_cxx; 94 95 /* Some code-gen now depends on OS major version numbers (at least). */ 96 int generating_for_darwin_version ; 97 98 /* For older linkers we need to emit special sections (marked 'coalesced') for 99 for weak or single-definition items. */ 100 static bool ld_uses_coal_sects = false; 101 102 /* Very old (ld_classic) linkers need a symbol to mark the start of 103 each FDE. */ 104 static bool ld_needs_eh_markers = false; 105 106 /* Section names. */ 107 section * darwin_sections[NUM_DARWIN_SECTIONS]; 108 109 /* While we transition to using in-tests instead of ifdef'd code. */ 110 #if !HAVE_lo_sum 111 #define gen_macho_high(a,b) (a) 112 #define gen_macho_low(a,b,c) (a) 113 #endif 114 115 /* True if we're setting __attribute__ ((ms_struct)). */ 116 int darwin_ms_struct = false; 117 118 /* Earlier versions of Darwin as do not recognize an alignment field in 119 .comm directives, this should be set for versions that allow it. */ 120 int emit_aligned_common = false; 121 122 /* A get_unnamed_section callback used to switch to an ObjC section. 123 DIRECTIVE is as for output_section_asm_op. */ 124 125 static void 126 output_objc_section_asm_op (const void *directive) 127 { 128 static bool been_here = false; 129 130 /* The NeXT ObjC Runtime requires these sections to be present and in 131 order in the object. The code below implements this by emitting 132 a section header for each ObjC section the first time that an ObjC 133 section is requested. */ 134 if (! been_here) 135 { 136 section *saved_in_section = in_section; 137 static const enum darwin_section_enum tomark[] = 138 { 139 /* written, cold -> hot */ 140 objc_cat_cls_meth_section, 141 objc_cat_inst_meth_section, 142 objc_string_object_section, 143 objc_constant_string_object_section, 144 objc_selector_refs_section, 145 objc_selector_fixup_section, 146 objc_cls_refs_section, 147 objc_class_section, 148 objc_meta_class_section, 149 /* shared, hot -> cold */ 150 objc_cls_meth_section, 151 objc_inst_meth_section, 152 objc_protocol_section, 153 objc_class_names_section, 154 objc_meth_var_types_section, 155 objc_meth_var_names_section, 156 objc_category_section, 157 objc_class_vars_section, 158 objc_instance_vars_section, 159 objc_module_info_section, 160 objc_symbols_section, 161 }; 162 /* ABI=1 */ 163 static const enum darwin_section_enum tomarkv1[] = 164 { 165 objc1_protocol_ext_section, 166 objc1_class_ext_section, 167 objc1_prop_list_section 168 } ; 169 /* ABI=2 */ 170 static const enum darwin_section_enum tomarkv2[] = 171 { 172 objc2_message_refs_section, 173 objc2_classdefs_section, 174 objc2_metadata_section, 175 objc2_classrefs_section, 176 objc2_classlist_section, 177 objc2_categorylist_section, 178 objc2_selector_refs_section, 179 objc2_nonlazy_class_section, 180 objc2_nonlazy_category_section, 181 objc2_protocollist_section, 182 objc2_protocolrefs_section, 183 objc2_super_classrefs_section, 184 objc2_image_info_section, 185 objc2_constant_string_object_section 186 } ; 187 size_t i; 188 189 been_here = true; 190 if (flag_objc_abi < 2) 191 { 192 for (i = 0; i < ARRAY_SIZE (tomark); i++) 193 switch_to_section (darwin_sections[tomark[i]]); 194 if (flag_objc_abi == 1) 195 for (i = 0; i < ARRAY_SIZE (tomarkv1); i++) 196 switch_to_section (darwin_sections[tomarkv1[i]]); 197 } 198 else 199 for (i = 0; i < ARRAY_SIZE (tomarkv2); i++) 200 switch_to_section (darwin_sections[tomarkv2[i]]); 201 /* Make sure we don't get varasm.c out of sync with us. */ 202 switch_to_section (saved_in_section); 203 } 204 output_section_asm_op (directive); 205 } 206 207 208 /* Private flag applied to disable section-anchors in a particular section. */ 209 #define SECTION_NO_ANCHOR SECTION_MACH_DEP 210 211 212 /* Implement TARGET_ASM_INIT_SECTIONS. */ 213 214 void 215 darwin_init_sections (void) 216 { 217 #define DEF_SECTION(NAME, FLAGS, DIRECTIVE, OBJC) \ 218 darwin_sections[NAME] = \ 219 get_unnamed_section (FLAGS, (OBJC \ 220 ? output_objc_section_asm_op \ 221 : output_section_asm_op), \ 222 "\t" DIRECTIVE); 223 #include "config/darwin-sections.def" 224 #undef DEF_SECTION 225 226 readonly_data_section = darwin_sections[const_section]; 227 exception_section = darwin_sections[darwin_exception_section]; 228 eh_frame_section = darwin_sections[darwin_eh_frame_section]; 229 230 /* If our linker is new enough to coalesce weak symbols, then we 231 can just put picbase_thunks into the text section. */ 232 if (! ld_uses_coal_sects ) 233 darwin_sections[picbase_thunk_section] = text_section; 234 } 235 236 int 237 name_needs_quotes (const char *name) 238 { 239 int c; 240 while ((c = *name++) != '\0') 241 if (! ISIDNUM (c) 242 && c != '.' && c != '$' && c != '_' ) 243 return 1; 244 return 0; 245 } 246 247 /* Return true if SYM_REF can be used without an indirection. */ 248 int 249 machopic_symbol_defined_p (rtx sym_ref) 250 { 251 if (MACHO_SYMBOL_DEFINED_P (sym_ref)) 252 return true; 253 254 /* If a symbol references local and is not an extern to this 255 file, then the symbol might be able to declared as defined. */ 256 if (SYMBOL_REF_LOCAL_P (sym_ref) && ! SYMBOL_REF_EXTERNAL_P (sym_ref)) 257 { 258 /* If the symbol references a variable and the variable is a 259 common symbol, then this symbol is not defined. */ 260 if (MACHO_SYMBOL_VARIABLE_P (sym_ref)) 261 { 262 tree decl = SYMBOL_REF_DECL (sym_ref); 263 if (!decl) 264 return true; 265 if (DECL_COMMON (decl)) 266 return false; 267 } 268 return true; 269 } 270 return false; 271 } 272 273 /* This module assumes that (const (symbol_ref "foo")) is a legal pic 274 reference, which will not be changed. */ 275 276 enum machopic_addr_class 277 machopic_classify_symbol (rtx sym_ref) 278 { 279 bool function_p; 280 281 function_p = SYMBOL_REF_FUNCTION_P (sym_ref); 282 if (machopic_symbol_defined_p (sym_ref)) 283 return (function_p 284 ? MACHOPIC_DEFINED_FUNCTION : MACHOPIC_DEFINED_DATA); 285 else 286 return (function_p 287 ? MACHOPIC_UNDEFINED_FUNCTION : MACHOPIC_UNDEFINED_DATA); 288 } 289 290 #ifndef TARGET_FIX_AND_CONTINUE 291 #define TARGET_FIX_AND_CONTINUE 0 292 #endif 293 294 /* Indicate when fix-and-continue style code generation is being used 295 and when a reference to data should be indirected so that it can be 296 rebound in a new translation unit to reference the original instance 297 of that data. Symbol names that are for code generation local to 298 the translation unit are bound to the new translation unit; 299 currently this means symbols that begin with L or _OBJC_; 300 otherwise, we indicate that an indirect reference should be made to 301 permit the runtime to rebind new instances of the translation unit 302 to the original instance of the data. */ 303 304 static int 305 indirect_data (rtx sym_ref) 306 { 307 int lprefix; 308 const char *name; 309 310 /* If we aren't generating fix-and-continue code, don't do anything 311 special. */ 312 if (TARGET_FIX_AND_CONTINUE == 0) 313 return 0; 314 315 /* Otherwise, all symbol except symbols that begin with L or _OBJC_ 316 are indirected. Symbols that begin with L and _OBJC_ are always 317 bound to the current translation unit as they are used for 318 generated local data of the translation unit. */ 319 320 name = XSTR (sym_ref, 0); 321 322 lprefix = (((name[0] == '*' || name[0] == '&') 323 && (name[1] == 'L' || (name[1] == '"' && name[2] == 'L'))) 324 || (strncmp (name, "_OBJC_", 6) == 0)); 325 326 return ! lprefix; 327 } 328 329 static int 330 machopic_data_defined_p (rtx sym_ref) 331 { 332 if (indirect_data (sym_ref)) 333 return 0; 334 335 switch (machopic_classify_symbol (sym_ref)) 336 { 337 case MACHOPIC_DEFINED_DATA: 338 case MACHOPIC_DEFINED_FUNCTION: 339 return 1; 340 default: 341 return 0; 342 } 343 } 344 345 void 346 machopic_define_symbol (rtx mem) 347 { 348 rtx sym_ref; 349 350 gcc_assert (GET_CODE (mem) == MEM); 351 sym_ref = XEXP (mem, 0); 352 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_DEFINED; 353 } 354 355 /* Return either ORIG or: 356 357 (const:P (unspec:P [ORIG] UNSPEC_MACHOPIC_OFFSET)) 358 359 depending on MACHO_DYNAMIC_NO_PIC_P. */ 360 rtx 361 machopic_gen_offset (rtx orig) 362 { 363 if (MACHO_DYNAMIC_NO_PIC_P) 364 return orig; 365 else 366 { 367 /* Play games to avoid marking the function as needing pic if we 368 are being called as part of the cost-estimation process. */ 369 if (current_ir_type () != IR_GIMPLE || currently_expanding_to_rtl) 370 crtl->uses_pic_offset_table = 1; 371 orig = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, orig), 372 UNSPEC_MACHOPIC_OFFSET); 373 return gen_rtx_CONST (Pmode, orig); 374 } 375 } 376 377 static GTY(()) const char * function_base_func_name; 378 static GTY(()) int current_pic_label_num; 379 static GTY(()) int emitted_pic_label_num; 380 381 static void 382 update_pic_label_number_if_needed (void) 383 { 384 const char *current_name; 385 386 /* When we are generating _get_pc thunks within stubs, there is no current 387 function. */ 388 if (current_function_decl) 389 { 390 current_name = 391 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl)); 392 if (function_base_func_name != current_name) 393 { 394 ++current_pic_label_num; 395 function_base_func_name = current_name; 396 } 397 } 398 else 399 { 400 ++current_pic_label_num; 401 function_base_func_name = "L_machopic_stub_dummy"; 402 } 403 } 404 405 void 406 machopic_output_function_base_name (FILE *file) 407 { 408 /* If dynamic-no-pic is on, we should not get here. */ 409 gcc_assert (!MACHO_DYNAMIC_NO_PIC_P); 410 411 update_pic_label_number_if_needed (); 412 fprintf (file, "L%d$pb", current_pic_label_num); 413 } 414 415 char curr_picbasename[32]; 416 417 const char * 418 machopic_get_function_picbase (void) 419 { 420 /* If dynamic-no-pic is on, we should not get here. */ 421 gcc_assert (!MACHO_DYNAMIC_NO_PIC_P); 422 423 update_pic_label_number_if_needed (); 424 snprintf (curr_picbasename, 32, "L%d$pb", current_pic_label_num); 425 return (const char *) curr_picbasename; 426 } 427 428 bool 429 machopic_should_output_picbase_label (void) 430 { 431 update_pic_label_number_if_needed (); 432 433 if (current_pic_label_num == emitted_pic_label_num) 434 return false; 435 436 emitted_pic_label_num = current_pic_label_num; 437 return true; 438 } 439 440 /* The suffix attached to non-lazy pointer symbols. */ 441 #define NON_LAZY_POINTER_SUFFIX "$non_lazy_ptr" 442 /* The suffix attached to stub symbols. */ 443 #define STUB_SUFFIX "$stub" 444 445 typedef struct GTY ((for_user)) machopic_indirection 446 { 447 /* The SYMBOL_REF for the entity referenced. */ 448 rtx symbol; 449 /* The name of the stub or non-lazy pointer. */ 450 const char * ptr_name; 451 /* True iff this entry is for a stub (as opposed to a non-lazy 452 pointer). */ 453 bool stub_p; 454 /* True iff this stub or pointer has been referenced. */ 455 bool used; 456 /* True iff a non-lazy symbol pointer should be emitted into the .data 457 section, rather than the non-lazy symbol pointers section. The cases 458 for which this occurred seem to have been unintentional, and later 459 toolchains emit all of the indirections to the 'usual' section. We 460 are keeping this in case it is necessary to preserve compatibility with 461 older toolchains. */ 462 bool nlsp_in_data_section; 463 } machopic_indirection; 464 465 struct indirection_hasher : ggc_ptr_hash<machopic_indirection> 466 { 467 typedef const char *compare_type; 468 static hashval_t hash (machopic_indirection *); 469 static bool equal (machopic_indirection *, const char *); 470 }; 471 472 /* A table mapping stub names and non-lazy pointer names to 473 SYMBOL_REFs for the stubbed-to and pointed-to entities. */ 474 475 static GTY (()) hash_table<indirection_hasher> *machopic_indirections; 476 477 /* Return a hash value for a SLOT in the indirections hash table. */ 478 479 hashval_t 480 indirection_hasher::hash (machopic_indirection *p) 481 { 482 return htab_hash_string (p->ptr_name); 483 } 484 485 /* Returns true if the KEY is the same as that associated with 486 SLOT. */ 487 488 bool 489 indirection_hasher::equal (machopic_indirection *s, const char *k) 490 { 491 return strcmp (s->ptr_name, k) == 0; 492 } 493 494 /* Return the name of the non-lazy pointer (if STUB_P is false) or 495 stub (if STUB_B is true) corresponding to the given name. 496 497 PR71767 - If we have a situation like: 498 499 global_weak_symbol: 500 .... 501 Lnon_weak_local: 502 .... 503 504 ld64 will be unable to split this into two atoms (because the "L" makes 505 the second symbol 'invisible'). This means that legitimate direct accesses 506 to the second symbol will appear to be direct accesses to an atom of type 507 weak, global which are not allowed. 508 509 To avoid this, we make any data-section indirections have a leading 'l' 510 (lower-case L) which has a special meaning: linker can see this and use 511 it to determine atoms, but it is not placed into the final symbol table. 512 513 Symbols in the non-lazy symbol pointers section (or stubs) do not have this 514 problem because ld64 already knows the size of each entry. 515 */ 516 517 const char * 518 machopic_indirection_name (rtx sym_ref, bool stub_p) 519 { 520 const char *name = XSTR (sym_ref, 0); 521 tree id = maybe_get_identifier (name); 522 if (id) 523 { 524 tree id_orig = id; 525 526 while (IDENTIFIER_TRANSPARENT_ALIAS (id)) 527 id = TREE_CHAIN (id); 528 if (id != id_orig) 529 name = IDENTIFIER_POINTER (id); 530 } 531 532 const char *prefix = user_label_prefix; 533 /* If we are emitting the label 'verbatim' then omit the U_L_P and count 534 the name without the leading '*'. */ 535 if (name[0] == '*') 536 { 537 prefix = ""; 538 ++name; 539 } 540 541 /* Here we are undoing a number of causes that placed some indirections 542 (apparently erroneously) into the .data section. Specifically, some 543 symbols that are ABI mandated indirections and some hidden symbols 544 were being placed there - which cause difficulties with later 545 versions of ld64. Iff (after these checks) some symbol still gets an 546 indirection in the data section, we want to adjust the indirection 547 name to be linker visible to deal with PR71767 (notes above). */ 548 bool nlsp_in_data_section = 549 ! MACHO_SYMBOL_MUST_INDIRECT_P (sym_ref) 550 && ! MACHO_SYMBOL_HIDDEN_VIS_P (sym_ref) 551 && (machopic_symbol_defined_p (sym_ref) || SYMBOL_REF_LOCAL_P (sym_ref)) 552 && ! indirect_data (sym_ref); 553 554 const char *suffix = stub_p ? STUB_SUFFIX : NON_LAZY_POINTER_SUFFIX; 555 /* If the indirection is in the data section, let the linker see it. */ 556 char L_or_l = (!stub_p && nlsp_in_data_section) ? 'l' : 'L'; 557 /* We have mangled symbols with spaces and punctuation which typically 558 need surrounding in quotes for the assembler to consume them. */ 559 const char *quote = name_needs_quotes (name) ? "\"" : ""; 560 char *buffer = XALLOCAVEC (char, 2 /* strlen ("&L") or ("&l") */ 561 + strlen (prefix) 562 + strlen (name) 563 + strlen (suffix) 564 + 2 * strlen (quote) 565 + 1 /* '\0' */); 566 567 /* Construct the name of the non-lazy pointer or stub. */ 568 sprintf (buffer, "&%s%c%s%s%s%s", quote, L_or_l, prefix, name, 569 suffix, quote); 570 571 if (!machopic_indirections) 572 machopic_indirections = hash_table<indirection_hasher>::create_ggc (37); 573 574 machopic_indirection **slot 575 = machopic_indirections->find_slot_with_hash (buffer, 576 htab_hash_string (buffer), 577 INSERT); 578 machopic_indirection *p; 579 if (*slot) 580 p = *slot; 581 else 582 { 583 p = ggc_alloc<machopic_indirection> (); 584 p->symbol = sym_ref; 585 p->ptr_name = xstrdup (buffer); 586 p->stub_p = stub_p; 587 p->used = false; 588 p->nlsp_in_data_section = nlsp_in_data_section; 589 *slot = p; 590 } 591 592 return p->ptr_name; 593 } 594 595 /* Return the name of the stub for the mcount function. */ 596 597 const char* 598 machopic_mcount_stub_name (void) 599 { 600 rtx symbol = gen_rtx_SYMBOL_REF (Pmode, "*mcount"); 601 return machopic_indirection_name (symbol, /*stub_p=*/true); 602 } 603 604 /* If NAME is the name of a stub or a non-lazy pointer , mark the stub 605 or non-lazy pointer as used -- and mark the object to which the 606 pointer/stub refers as used as well, since the pointer/stub will 607 emit a reference to it. */ 608 609 void 610 machopic_validate_stub_or_non_lazy_ptr (const char *name) 611 { 612 machopic_indirection *p 613 = machopic_indirections->find_with_hash (name, htab_hash_string (name)); 614 if (p && ! p->used) 615 { 616 const char *real_name; 617 tree id; 618 619 p->used = true; 620 621 /* Do what output_addr_const will do when we actually call it. */ 622 if (SYMBOL_REF_DECL (p->symbol)) 623 mark_decl_referenced (SYMBOL_REF_DECL (p->symbol)); 624 625 real_name = targetm.strip_name_encoding (XSTR (p->symbol, 0)); 626 627 id = maybe_get_identifier (real_name); 628 if (id) 629 mark_referenced (id); 630 } 631 } 632 633 /* Transform ORIG, which may be any data source, to the corresponding 634 source using indirections. */ 635 636 rtx 637 machopic_indirect_data_reference (rtx orig, rtx reg) 638 { 639 rtx ptr_ref = orig; 640 641 if (! MACHOPIC_INDIRECT) 642 return orig; 643 644 if (GET_CODE (orig) == SYMBOL_REF) 645 { 646 int defined = machopic_data_defined_p (orig); 647 648 if (defined && MACHO_DYNAMIC_NO_PIC_P) 649 { 650 if (DARWIN_PPC) 651 { 652 /* Create a new register for CSE opportunities. */ 653 rtx hi_reg = (!can_create_pseudo_p () ? reg : gen_reg_rtx (Pmode)); 654 emit_insn (gen_macho_high (hi_reg, orig)); 655 emit_insn (gen_macho_low (reg, hi_reg, orig)); 656 return reg; 657 } 658 else if (DARWIN_X86) 659 return orig; 660 else 661 /* some other cpu -- writeme! */ 662 gcc_unreachable (); 663 } 664 else if (defined && ! MACHO_SYMBOL_MUST_INDIRECT_P (orig)) 665 { 666 rtx offset = NULL; 667 if (DARWIN_PPC || HAVE_lo_sum) 668 offset = machopic_gen_offset (orig); 669 670 if (DARWIN_PPC) 671 { 672 rtx hi_sum_reg = (!can_create_pseudo_p () 673 ? reg 674 : gen_reg_rtx (Pmode)); 675 676 gcc_assert (reg); 677 678 emit_insn (gen_rtx_SET (hi_sum_reg, 679 gen_rtx_PLUS (Pmode, pic_offset_table_rtx, 680 gen_rtx_HIGH (Pmode, offset)))); 681 emit_insn (gen_rtx_SET (reg, 682 gen_rtx_LO_SUM (Pmode, hi_sum_reg, 683 copy_rtx (offset)))); 684 685 orig = reg; 686 } 687 else if (HAVE_lo_sum) 688 { 689 gcc_assert (reg); 690 691 emit_insn (gen_rtx_SET (reg, gen_rtx_HIGH (Pmode, offset))); 692 emit_insn (gen_rtx_SET (reg, gen_rtx_LO_SUM (Pmode, reg, 693 copy_rtx (offset)))); 694 emit_use (pic_offset_table_rtx); 695 696 orig = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, reg); 697 } 698 return orig; 699 } 700 701 ptr_ref = (gen_rtx_SYMBOL_REF 702 (Pmode, 703 machopic_indirection_name (orig, /*stub_p=*/false))); 704 705 SYMBOL_REF_DATA (ptr_ref) = SYMBOL_REF_DATA (orig); 706 SYMBOL_REF_FLAGS (ptr_ref) |= MACHO_SYMBOL_FLAG_INDIRECTION; 707 708 ptr_ref = gen_const_mem (Pmode, ptr_ref); 709 machopic_define_symbol (ptr_ref); 710 711 if (DARWIN_X86 712 && reg 713 && MACHO_DYNAMIC_NO_PIC_P) 714 { 715 emit_insn (gen_rtx_SET (reg, ptr_ref)); 716 ptr_ref = reg; 717 } 718 719 return ptr_ref; 720 } 721 else if (GET_CODE (orig) == CONST) 722 { 723 /* If "(const (plus ...", walk the PLUS and return that result. 724 PLUS processing (below) will restore the "(const ..." if 725 appropriate. */ 726 if (GET_CODE (XEXP (orig, 0)) == PLUS) 727 return machopic_indirect_data_reference (XEXP (orig, 0), reg); 728 else 729 return orig; 730 } 731 else if (GET_CODE (orig) == MEM) 732 { 733 XEXP (ptr_ref, 0) = 734 machopic_indirect_data_reference (XEXP (orig, 0), reg); 735 return ptr_ref; 736 } 737 else if (GET_CODE (orig) == PLUS) 738 { 739 rtx base, result; 740 /* When the target is i386, this code prevents crashes due to the 741 compiler's ignorance on how to move the PIC base register to 742 other registers. (The reload phase sometimes introduces such 743 insns.) */ 744 if (GET_CODE (XEXP (orig, 0)) == REG 745 && REGNO (XEXP (orig, 0)) == PIC_OFFSET_TABLE_REGNUM 746 /* Prevent the same register from being erroneously used 747 as both the base and index registers. */ 748 && (DARWIN_X86 && (GET_CODE (XEXP (orig, 1)) == CONST)) 749 && reg) 750 { 751 emit_move_insn (reg, XEXP (orig, 0)); 752 XEXP (ptr_ref, 0) = reg; 753 return ptr_ref; 754 } 755 756 /* Legitimize both operands of the PLUS. */ 757 base = machopic_indirect_data_reference (XEXP (orig, 0), reg); 758 orig = machopic_indirect_data_reference (XEXP (orig, 1), 759 (base == reg ? 0 : reg)); 760 if (MACHOPIC_INDIRECT && (GET_CODE (orig) == CONST_INT)) 761 result = plus_constant (Pmode, base, INTVAL (orig)); 762 else 763 result = gen_rtx_PLUS (Pmode, base, orig); 764 765 if (MACHOPIC_JUST_INDIRECT && GET_CODE (base) == MEM) 766 { 767 if (reg) 768 { 769 emit_move_insn (reg, result); 770 result = reg; 771 } 772 else 773 { 774 result = force_reg (GET_MODE (result), result); 775 } 776 } 777 778 return result; 779 } 780 return ptr_ref; 781 } 782 783 /* Transform TARGET (a MEM), which is a function call target, to the 784 corresponding symbol_stub if necessary. Return a new MEM. */ 785 786 rtx 787 machopic_indirect_call_target (rtx target) 788 { 789 if (! darwin_symbol_stubs) 790 return target; 791 792 if (GET_CODE (target) != MEM) 793 return target; 794 795 if (MACHOPIC_INDIRECT 796 && GET_CODE (XEXP (target, 0)) == SYMBOL_REF 797 && ! MACHO_SYMBOL_DEFINED_P (XEXP (target, 0))) 798 { 799 rtx sym_ref = XEXP (target, 0); 800 const char *stub_name = machopic_indirection_name (sym_ref, 801 /*stub_p=*/true); 802 machine_mode mode = GET_MODE (sym_ref); 803 804 XEXP (target, 0) = gen_rtx_SYMBOL_REF (mode, stub_name); 805 SYMBOL_REF_DATA (XEXP (target, 0)) = SYMBOL_REF_DATA (sym_ref); 806 SYMBOL_REF_FLAGS (XEXP (target, 0)) |= MACHO_SYMBOL_FLAG_INDIRECTION; 807 MEM_READONLY_P (target) = 1; 808 MEM_NOTRAP_P (target) = 1; 809 } 810 811 return target; 812 } 813 814 rtx 815 machopic_legitimize_pic_address (rtx orig, machine_mode mode, rtx reg) 816 { 817 rtx pic_ref = orig; 818 819 if (! MACHOPIC_INDIRECT) 820 return orig; 821 822 /* First handle a simple SYMBOL_REF or LABEL_REF */ 823 if (GET_CODE (orig) == LABEL_REF 824 || GET_CODE (orig) == SYMBOL_REF) 825 { 826 /* addr(foo) = &func+(foo-func) */ 827 orig = machopic_indirect_data_reference (orig, reg); 828 829 if (GET_CODE (orig) == PLUS 830 && GET_CODE (XEXP (orig, 0)) == REG) 831 { 832 if (reg == 0) 833 return force_reg (mode, orig); 834 835 emit_move_insn (reg, orig); 836 return reg; 837 } 838 839 if (GET_CODE (orig) == MEM) 840 { 841 if (reg == 0) 842 { 843 gcc_assert (!lra_in_progress); 844 reg = gen_reg_rtx (Pmode); 845 } 846 847 #if HAVE_lo_sum 848 if (MACHO_DYNAMIC_NO_PIC_P 849 && (GET_CODE (XEXP (orig, 0)) == SYMBOL_REF 850 || GET_CODE (XEXP (orig, 0)) == LABEL_REF)) 851 { 852 #if defined (TARGET_TOC) /* ppc */ 853 rtx temp_reg = (!can_create_pseudo_p () 854 ? reg : 855 gen_reg_rtx (Pmode)); 856 rtx asym = XEXP (orig, 0); 857 rtx mem; 858 859 emit_insn (gen_macho_high (temp_reg, asym)); 860 mem = gen_const_mem (GET_MODE (orig), 861 gen_rtx_LO_SUM (Pmode, temp_reg, 862 copy_rtx (asym))); 863 emit_insn (gen_rtx_SET (reg, mem)); 864 #else 865 /* Some other CPU -- WriteMe! but right now there are no other 866 platforms that can use dynamic-no-pic */ 867 gcc_unreachable (); 868 #endif 869 pic_ref = reg; 870 } 871 else 872 if (GET_CODE (XEXP (orig, 0)) == SYMBOL_REF 873 || GET_CODE (XEXP (orig, 0)) == LABEL_REF) 874 { 875 rtx offset = machopic_gen_offset (XEXP (orig, 0)); 876 #if defined (TARGET_TOC) /* i.e., PowerPC */ 877 /* Generating a new reg may expose opportunities for 878 common subexpression elimination. */ 879 rtx hi_sum_reg = (!can_create_pseudo_p () 880 ? reg 881 : gen_reg_rtx (Pmode)); 882 rtx mem; 883 rtx sum; 884 885 sum = gen_rtx_HIGH (Pmode, offset); 886 if (! MACHO_DYNAMIC_NO_PIC_P) 887 sum = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, sum); 888 889 emit_insn (gen_rtx_SET (hi_sum_reg, sum)); 890 891 mem = gen_const_mem (GET_MODE (orig), 892 gen_rtx_LO_SUM (Pmode, 893 hi_sum_reg, 894 copy_rtx (offset))); 895 rtx_insn *insn = emit_insn (gen_rtx_SET (reg, mem)); 896 set_unique_reg_note (insn, REG_EQUAL, pic_ref); 897 898 pic_ref = reg; 899 #else 900 emit_use (gen_rtx_REG (Pmode, PIC_OFFSET_TABLE_REGNUM)); 901 902 emit_insn (gen_rtx_SET (reg, 903 gen_rtx_HIGH (Pmode, 904 gen_rtx_CONST (Pmode, 905 offset)))); 906 emit_insn (gen_rtx_SET (reg, 907 gen_rtx_LO_SUM (Pmode, reg, 908 gen_rtx_CONST (Pmode, 909 copy_rtx (offset))))); 910 pic_ref = gen_rtx_PLUS (Pmode, 911 pic_offset_table_rtx, reg); 912 #endif 913 } 914 else 915 #endif /* HAVE_lo_sum */ 916 { 917 rtx pic = pic_offset_table_rtx; 918 if (GET_CODE (pic) != REG) 919 { 920 emit_move_insn (reg, pic); 921 pic = reg; 922 } 923 #if 0 924 emit_use (gen_rtx_REG (Pmode, PIC_OFFSET_TABLE_REGNUM)); 925 #endif 926 927 if (lra_in_progress) 928 df_set_regs_ever_live (REGNO (pic), true); 929 pic_ref = gen_rtx_PLUS (Pmode, pic, 930 machopic_gen_offset (XEXP (orig, 0))); 931 } 932 933 #if !defined (TARGET_TOC) 934 emit_move_insn (reg, pic_ref); 935 pic_ref = gen_const_mem (GET_MODE (orig), reg); 936 #endif 937 } 938 else 939 { 940 941 #if HAVE_lo_sum 942 if (GET_CODE (orig) == SYMBOL_REF 943 || GET_CODE (orig) == LABEL_REF) 944 { 945 rtx offset = machopic_gen_offset (orig); 946 #if defined (TARGET_TOC) /* i.e., PowerPC */ 947 rtx hi_sum_reg; 948 949 if (reg == 0) 950 { 951 gcc_assert (!lra_in_progress); 952 reg = gen_reg_rtx (Pmode); 953 } 954 955 hi_sum_reg = reg; 956 957 emit_insn (gen_rtx_SET (hi_sum_reg, 958 (MACHO_DYNAMIC_NO_PIC_P) 959 ? gen_rtx_HIGH (Pmode, offset) 960 : gen_rtx_PLUS (Pmode, 961 pic_offset_table_rtx, 962 gen_rtx_HIGH (Pmode, 963 offset)))); 964 emit_insn (gen_rtx_SET (reg, 965 gen_rtx_LO_SUM (Pmode, 966 hi_sum_reg, 967 copy_rtx (offset)))); 968 pic_ref = reg; 969 #else 970 emit_insn (gen_rtx_SET (reg, gen_rtx_HIGH (Pmode, offset))); 971 emit_insn (gen_rtx_SET (reg, 972 gen_rtx_LO_SUM (Pmode, reg, 973 copy_rtx (offset)))); 974 pic_ref = gen_rtx_PLUS (Pmode, 975 pic_offset_table_rtx, reg); 976 #endif 977 } 978 else 979 #endif /* HAVE_lo_sum */ 980 { 981 if (REG_P (orig) 982 || GET_CODE (orig) == SUBREG) 983 { 984 return orig; 985 } 986 else 987 { 988 rtx pic = pic_offset_table_rtx; 989 if (GET_CODE (pic) != REG) 990 { 991 emit_move_insn (reg, pic); 992 pic = reg; 993 } 994 #if 0 995 emit_use (pic_offset_table_rtx); 996 #endif 997 if (lra_in_progress) 998 df_set_regs_ever_live (REGNO (pic), true); 999 pic_ref = gen_rtx_PLUS (Pmode, 1000 pic, 1001 machopic_gen_offset (orig)); 1002 } 1003 } 1004 } 1005 1006 if (GET_CODE (pic_ref) != REG) 1007 { 1008 if (reg != 0) 1009 { 1010 emit_move_insn (reg, pic_ref); 1011 return reg; 1012 } 1013 else 1014 { 1015 return force_reg (mode, pic_ref); 1016 } 1017 } 1018 else 1019 { 1020 return pic_ref; 1021 } 1022 } 1023 else if (GET_CODE (orig) == PLUS 1024 && (GET_CODE (XEXP (orig, 0)) == MEM 1025 || GET_CODE (XEXP (orig, 0)) == SYMBOL_REF 1026 || GET_CODE (XEXP (orig, 0)) == LABEL_REF) 1027 && XEXP (orig, 0) != pic_offset_table_rtx 1028 && GET_CODE (XEXP (orig, 1)) != REG) 1029 1030 { 1031 rtx base; 1032 int is_complex = (GET_CODE (XEXP (orig, 0)) == MEM); 1033 1034 base = machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg); 1035 orig = machopic_legitimize_pic_address (XEXP (orig, 1), 1036 Pmode, (base == reg ? 0 : reg)); 1037 if (GET_CODE (orig) == CONST_INT) 1038 { 1039 pic_ref = plus_constant (Pmode, base, INTVAL (orig)); 1040 is_complex = 1; 1041 } 1042 else 1043 pic_ref = gen_rtx_PLUS (Pmode, base, orig); 1044 1045 if (reg && is_complex) 1046 { 1047 emit_move_insn (reg, pic_ref); 1048 pic_ref = reg; 1049 } 1050 /* Likewise, should we set special REG_NOTEs here? */ 1051 } 1052 else if (GET_CODE (orig) == CONST) 1053 { 1054 return machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg); 1055 } 1056 else if (GET_CODE (orig) == MEM 1057 && GET_CODE (XEXP (orig, 0)) == SYMBOL_REF) 1058 { 1059 rtx addr = machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg); 1060 addr = replace_equiv_address (orig, addr); 1061 emit_move_insn (reg, addr); 1062 pic_ref = reg; 1063 } 1064 1065 return pic_ref; 1066 } 1067 1068 /* Callbacks to output the stub or non-lazy pointers. 1069 Each works on the item in *SLOT,if it has been used. 1070 DATA is the FILE* for assembly output. 1071 Called from htab_traverses, invoked from machopic_finish(). */ 1072 1073 int 1074 machopic_output_data_section_indirection (machopic_indirection **slot, 1075 FILE *asm_out_file) 1076 { 1077 machopic_indirection *p = *slot; 1078 1079 if (!p->used || !p->nlsp_in_data_section) 1080 return 1; 1081 1082 rtx symbol = p->symbol; 1083 /* The original symbol name. */ 1084 const char *sym_name = XSTR (symbol, 0); 1085 /* The name of the indirection symbol. */ 1086 const char *ptr_name = p->ptr_name; 1087 1088 switch_to_section (data_section); 1089 assemble_align (GET_MODE_ALIGNMENT (Pmode)); 1090 assemble_label (asm_out_file, ptr_name); 1091 assemble_integer (gen_rtx_SYMBOL_REF (Pmode, sym_name), 1092 GET_MODE_SIZE (Pmode), 1093 GET_MODE_ALIGNMENT (Pmode), 1); 1094 1095 return 1; 1096 } 1097 1098 int 1099 machopic_output_stub_indirection (machopic_indirection **slot, 1100 FILE *asm_out_file) 1101 { 1102 machopic_indirection *p = *slot; 1103 1104 if (!p->used || !p->stub_p) 1105 return 1; 1106 1107 rtx symbol = p->symbol; 1108 /* The original symbol name. */ 1109 const char *sym_name = XSTR (symbol, 0); 1110 /* The name of the stub symbol. */ 1111 const char *ptr_name = p->ptr_name; 1112 1113 tree id = maybe_get_identifier (sym_name); 1114 if (id) 1115 { 1116 tree id_orig = id; 1117 1118 while (IDENTIFIER_TRANSPARENT_ALIAS (id)) 1119 id = TREE_CHAIN (id); 1120 if (id != id_orig) 1121 sym_name = IDENTIFIER_POINTER (id); 1122 } 1123 1124 char *sym = XALLOCAVEC (char, strlen (sym_name) + 2); 1125 if (sym_name[0] == '*' || sym_name[0] == '&') 1126 strcpy (sym, sym_name + 1); 1127 else if (sym_name[0] == '-' || sym_name[0] == '+') 1128 strcpy (sym, sym_name); 1129 else 1130 sprintf (sym, "%s%s", user_label_prefix, sym_name); 1131 1132 char *stub = XALLOCAVEC (char, strlen (ptr_name) + 2); 1133 if (ptr_name[0] == '*' || ptr_name[0] == '&') 1134 strcpy (stub, ptr_name + 1); 1135 else 1136 sprintf (stub, "%s%s", user_label_prefix, ptr_name); 1137 1138 machopic_output_stub (asm_out_file, sym, stub); 1139 1140 return 1; 1141 } 1142 1143 int 1144 machopic_output_indirection (machopic_indirection **slot, FILE *asm_out_file) 1145 { 1146 machopic_indirection *p = *slot; 1147 1148 if (!p->used || p->stub_p || p->nlsp_in_data_section) 1149 return 1; 1150 1151 rtx symbol = p->symbol; 1152 /* The original symbol name. */ 1153 const char *sym_name = XSTR (symbol, 0); 1154 /* The nonlazy-stub symbol name. */ 1155 const char *ptr_name = p->ptr_name; 1156 1157 switch_to_section (darwin_sections[machopic_nl_symbol_ptr_section]); 1158 1159 /* Mach-O symbols are passed around in code through indirect references and 1160 the original symbol_ref hasn't passed through the generic handling and 1161 reference-catching in output_operand, so we need to manually mark weak 1162 references as such. */ 1163 1164 if (SYMBOL_REF_WEAK (symbol)) 1165 { 1166 tree decl = SYMBOL_REF_DECL (symbol); 1167 gcc_checking_assert (DECL_P (decl)); 1168 1169 if (decl != NULL_TREE 1170 && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl) 1171 /* Handle only actual external-only definitions, not 1172 e.g. extern inline code or variables for which 1173 storage has been allocated. */ 1174 && !TREE_STATIC (decl)) 1175 { 1176 fputs ("\t.weak_reference ", asm_out_file); 1177 assemble_name (asm_out_file, sym_name); 1178 fputc ('\n', asm_out_file); 1179 } 1180 } 1181 1182 assemble_name (asm_out_file, ptr_name); 1183 fprintf (asm_out_file, ":\n"); 1184 1185 fprintf (asm_out_file, "\t.indirect_symbol "); 1186 assemble_name (asm_out_file, sym_name); 1187 fprintf (asm_out_file, "\n"); 1188 1189 /* Variables that are marked with MACHO_SYMBOL_FLAG_STATIC need to 1190 have their symbol name instead of 0 in the second entry of 1191 the non-lazy symbol pointer data structure when they are 1192 defined. This allows the runtime to rebind newer instances 1193 of the translation unit with the original instance of the 1194 symbol. */ 1195 1196 rtx init = const0_rtx; 1197 if (MACHO_SYMBOL_STATIC_P (symbol) && machopic_symbol_defined_p (symbol)) 1198 init = gen_rtx_SYMBOL_REF (Pmode, sym_name); 1199 1200 assemble_integer (init, GET_MODE_SIZE (Pmode), 1201 GET_MODE_ALIGNMENT (Pmode), 1); 1202 1203 return 1; 1204 } 1205 1206 static void 1207 machopic_finish (FILE *asm_out_file) 1208 { 1209 if (!machopic_indirections) 1210 return; 1211 1212 /* First output an symbol indirections that have been placed into .data 1213 (we don't expect these now). */ 1214 machopic_indirections->traverse_noresize 1215 <FILE *, machopic_output_data_section_indirection> (asm_out_file); 1216 1217 machopic_indirections->traverse_noresize 1218 <FILE *, machopic_output_stub_indirection> (asm_out_file); 1219 1220 machopic_indirections->traverse_noresize 1221 <FILE *, machopic_output_indirection> (asm_out_file); 1222 } 1223 1224 int 1225 machopic_operand_p (rtx op) 1226 { 1227 if (MACHOPIC_JUST_INDIRECT) 1228 return (GET_CODE (op) == SYMBOL_REF 1229 && machopic_symbol_defined_p (op)); 1230 else 1231 return (GET_CODE (op) == CONST 1232 && GET_CODE (XEXP (op, 0)) == UNSPEC 1233 && XINT (XEXP (op, 0), 1) == UNSPEC_MACHOPIC_OFFSET); 1234 } 1235 1236 /* This function: 1237 computes and caches a series of flags that characterise the symbol's 1238 properties that affect Mach-O code gen (including accidental cases 1239 from older toolchains). 1240 1241 TODO: 1242 Here we also need to do enough analysis to determine if a symbol's 1243 name needs to be made linker-visible. This is more tricky - since 1244 it depends on whether we've previously seen a global weak definition 1245 in the same section. 1246 */ 1247 1248 void 1249 darwin_encode_section_info (tree decl, rtx rtl, int first) 1250 { 1251 /* Careful not to prod global register variables. */ 1252 if (!MEM_P (rtl)) 1253 return; 1254 1255 /* Do the standard encoding things first; this sets: 1256 SYMBOL_FLAG_FUNCTION, 1257 SYMBOL_FLAG_LOCAL, (binds_local_p) 1258 TLS_MODEL, SYMBOL_FLAG_SMALL 1259 SYMBOL_FLAG_EXTERNAL. */ 1260 default_encode_section_info (decl, rtl, first); 1261 1262 if (! VAR_OR_FUNCTION_DECL_P (decl)) 1263 return; 1264 1265 rtx sym_ref = XEXP (rtl, 0); 1266 if (VAR_P (decl)) 1267 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_VARIABLE; 1268 1269 /* Only really common if there's no initialiser. */ 1270 bool really_common_p = (DECL_COMMON (decl) 1271 && (DECL_INITIAL (decl) == NULL 1272 || (!in_lto_p 1273 && DECL_INITIAL (decl) == error_mark_node))); 1274 1275 /* For Darwin, if we have specified visibility and it's not the default 1276 that's counted 'hidden'. */ 1277 if (DECL_VISIBILITY_SPECIFIED (decl) 1278 && DECL_VISIBILITY (decl) != VISIBILITY_DEFAULT) 1279 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_HIDDEN_VIS; 1280 1281 if (!DECL_EXTERNAL (decl) 1282 && (!TREE_PUBLIC (decl) || !DECL_WEAK (decl)) 1283 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)) 1284 && ((TREE_STATIC (decl) 1285 && (!DECL_COMMON (decl) || !TREE_PUBLIC (decl))) 1286 || (!DECL_COMMON (decl) && DECL_INITIAL (decl) 1287 && DECL_INITIAL (decl) != error_mark_node))) 1288 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_DEFINED; 1289 1290 if (! TREE_PUBLIC (decl)) 1291 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_STATIC; 1292 1293 /* Short cut check for Darwin 'must indirect' rules. */ 1294 if (really_common_p 1295 || (DECL_WEAK (decl) && ! MACHO_SYMBOL_HIDDEN_VIS_P (sym_ref)) 1296 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))) 1297 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_MUST_INDIRECT; 1298 } 1299 1300 void 1301 darwin_mark_decl_preserved (const char *name) 1302 { 1303 /* Actually we shouldn't mark any local symbol this way, but for now 1304 this only happens with ObjC meta-data. */ 1305 if (darwin_label_is_anonymous_local_objc_name (name)) 1306 return; 1307 1308 fprintf (asm_out_file, "\t.no_dead_strip "); 1309 assemble_name (asm_out_file, name); 1310 fputc ('\n', asm_out_file); 1311 } 1312 1313 static section * 1314 darwin_rodata_section (int use_coal, bool zsize, int reloc) 1315 { 1316 return (use_coal 1317 ? darwin_sections[const_coal_section] 1318 : (zsize ? darwin_sections[zobj_const_section] 1319 : reloc ? darwin_sections[const_data_section] 1320 : darwin_sections[const_section])); 1321 } 1322 1323 static section * 1324 darwin_mergeable_string_section (tree exp, 1325 unsigned HOST_WIDE_INT align) 1326 { 1327 /* Darwin's ld expects to see non-writable string literals in the .cstring 1328 section. Later versions of ld check and complain when CFStrings are 1329 enabled. Therefore we shall force the strings into .cstring since we 1330 don't support writable ones anyway. */ 1331 if ((darwin_constant_cfstrings || flag_merge_constants) 1332 && TREE_CODE (exp) == STRING_CST 1333 && TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE 1334 && align <= 256 1335 && (int_size_in_bytes (TREE_TYPE (exp)) 1336 == TREE_STRING_LENGTH (exp)) 1337 && ((size_t) TREE_STRING_LENGTH (exp) 1338 == strlen (TREE_STRING_POINTER (exp)) + 1)) 1339 return darwin_sections[cstring_section]; 1340 1341 if (DARWIN_SECTION_ANCHORS && flag_section_anchors 1342 && TREE_CODE (exp) == STRING_CST 1343 && TREE_STRING_LENGTH (exp) == 0) 1344 return darwin_sections[zobj_const_section]; 1345 1346 return readonly_data_section; 1347 } 1348 1349 #ifndef HAVE_GAS_LITERAL16 1350 #define HAVE_GAS_LITERAL16 0 1351 #endif 1352 1353 static section * 1354 darwin_mergeable_constant_section (tree exp, 1355 unsigned HOST_WIDE_INT align, 1356 bool zsize) 1357 { 1358 machine_mode mode = DECL_MODE (exp); 1359 unsigned int modesize = GET_MODE_BITSIZE (mode); 1360 1361 if (DARWIN_SECTION_ANCHORS 1362 && flag_section_anchors 1363 && zsize) 1364 return darwin_sections[zobj_const_section]; 1365 1366 if (flag_merge_constants 1367 && mode != VOIDmode 1368 && mode != BLKmode 1369 && modesize <= align 1370 && align >= 8 1371 && align <= 256 1372 && (align & (align -1)) == 0) 1373 { 1374 tree size = TYPE_SIZE_UNIT (TREE_TYPE (exp)); 1375 1376 if (TREE_CODE (size) == INTEGER_CST) 1377 { 1378 if (wi::to_wide (size) == 4) 1379 return darwin_sections[literal4_section]; 1380 else if (wi::to_wide (size) == 8) 1381 return darwin_sections[literal8_section]; 1382 else if (HAVE_GAS_LITERAL16 1383 && TARGET_64BIT 1384 && wi::to_wide (size) == 16) 1385 return darwin_sections[literal16_section]; 1386 } 1387 } 1388 1389 return readonly_data_section; 1390 } 1391 1392 section * 1393 darwin_tm_clone_table_section (void) 1394 { 1395 return get_named_section (NULL, 1396 "__DATA,__tm_clone_table,regular,no_dead_strip", 1397 3); 1398 } 1399 1400 int 1401 machopic_reloc_rw_mask (void) 1402 { 1403 return MACHOPIC_INDIRECT ? 3 : 0; 1404 } 1405 1406 /* We have to deal with ObjC/C++ metadata section placement in the common 1407 code, since it will also be called from LTO. 1408 1409 Return metadata attributes, if present (searching for ABI=2 first) 1410 Return NULL_TREE if no such attributes are found. */ 1411 1412 static tree 1413 is_objc_metadata (tree decl) 1414 { 1415 if (DECL_P (decl) 1416 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == CONST_DECL) 1417 && DECL_ATTRIBUTES (decl)) 1418 { 1419 tree meta = lookup_attribute ("OBJC2META", DECL_ATTRIBUTES (decl)); 1420 if (meta) 1421 return meta; 1422 meta = lookup_attribute ("OBJC1META", DECL_ATTRIBUTES (decl)); 1423 if (meta) 1424 return meta; 1425 } 1426 return NULL_TREE; 1427 } 1428 1429 static int classes_seen; 1430 static int objc_metadata_seen; 1431 1432 /* Return the section required for Objective C ABI 2 metadata. */ 1433 static section * 1434 darwin_objc2_section (tree decl ATTRIBUTE_UNUSED, tree meta, section * base) 1435 { 1436 const char *p; 1437 tree ident = TREE_VALUE (meta); 1438 gcc_assert (TREE_CODE (ident) == IDENTIFIER_NODE); 1439 p = IDENTIFIER_POINTER (ident); 1440 1441 gcc_checking_assert (flag_next_runtime == 1 && flag_objc_abi == 2); 1442 1443 objc_metadata_seen = 1; 1444 1445 if (base == data_section) 1446 base = darwin_sections[objc2_metadata_section]; 1447 1448 /* Most of the OBJC2 META-data end up in the base section, so check it 1449 first. */ 1450 if (!strncmp (p, "V2_BASE", 7)) 1451 return base; 1452 else if (!strncmp (p, "V2_STRG", 7)) 1453 return darwin_sections[cstring_section]; 1454 1455 else if (!strncmp (p, "G2_META", 7) || !strncmp (p, "G2_CLAS", 7)) 1456 return darwin_sections[objc2_classdefs_section]; 1457 else if (!strncmp (p, "V2_MREF", 7)) 1458 return darwin_sections[objc2_message_refs_section]; 1459 else if (!strncmp (p, "V2_CLRF", 7)) 1460 return darwin_sections[objc2_classrefs_section]; 1461 else if (!strncmp (p, "V2_SURF", 7)) 1462 return darwin_sections[objc2_super_classrefs_section]; 1463 else if (!strncmp (p, "V2_NLCL", 7)) 1464 return darwin_sections[objc2_nonlazy_class_section]; 1465 else if (!strncmp (p, "V2_CLAB", 7)) 1466 { 1467 classes_seen = 1; 1468 return darwin_sections[objc2_classlist_section]; 1469 } 1470 else if (!strncmp (p, "V2_SRFS", 7)) 1471 return darwin_sections[objc2_selector_refs_section]; 1472 else if (!strncmp (p, "V2_NLCA", 7)) 1473 return darwin_sections[objc2_nonlazy_category_section]; 1474 else if (!strncmp (p, "V2_CALA", 7)) 1475 return darwin_sections[objc2_categorylist_section]; 1476 1477 else if (!strncmp (p, "V2_PLST", 7)) 1478 return darwin_sections[objc2_protocollist_section]; 1479 else if (!strncmp (p, "V2_PRFS", 7)) 1480 return darwin_sections[objc2_protocolrefs_section]; 1481 1482 else if (!strncmp (p, "V2_INFO", 7)) 1483 return darwin_sections[objc2_image_info_section]; 1484 1485 else if (!strncmp (p, "V2_EHTY", 7)) 1486 return ld_uses_coal_sects ? darwin_sections[data_coal_section] 1487 : data_section; 1488 1489 else if (!strncmp (p, "V2_CSTR", 7)) 1490 return darwin_sections[objc2_constant_string_object_section]; 1491 1492 /* Not recognized, default. */ 1493 return base; 1494 } 1495 1496 /* Return the section required for Objective C ABI 0/1 metadata. */ 1497 static section * 1498 darwin_objc1_section (tree decl ATTRIBUTE_UNUSED, tree meta, section * base) 1499 { 1500 const char *p; 1501 tree ident = TREE_VALUE (meta); 1502 gcc_assert (TREE_CODE (ident) == IDENTIFIER_NODE); 1503 p = IDENTIFIER_POINTER (ident); 1504 1505 gcc_checking_assert (flag_next_runtime == 1 && flag_objc_abi < 2); 1506 1507 objc_metadata_seen = 1; 1508 1509 /* String sections first, cos there are lots of strings. */ 1510 if (!strncmp (p, "V1_STRG", 7)) 1511 return darwin_sections[cstring_section]; 1512 else if (!strncmp (p, "V1_CLSN", 7)) 1513 return darwin_sections[objc_class_names_section]; 1514 else if (!strncmp (p, "V1_METN", 7)) 1515 return darwin_sections[objc_meth_var_names_section]; 1516 else if (!strncmp (p, "V1_METT", 7)) 1517 return darwin_sections[objc_meth_var_types_section]; 1518 1519 else if (!strncmp (p, "V1_CLAS", 7)) 1520 { 1521 classes_seen = 1; 1522 return darwin_sections[objc_class_section]; 1523 } 1524 else if (!strncmp (p, "V1_META", 7)) 1525 return darwin_sections[objc_meta_class_section]; 1526 else if (!strncmp (p, "V1_CATG", 7)) 1527 return darwin_sections[objc_category_section]; 1528 else if (!strncmp (p, "V1_PROT", 7)) 1529 return darwin_sections[objc_protocol_section]; 1530 1531 else if (!strncmp (p, "V1_CLCV", 7)) 1532 return darwin_sections[objc_class_vars_section]; 1533 else if (!strncmp (p, "V1_CLIV", 7)) 1534 return darwin_sections[objc_instance_vars_section]; 1535 1536 else if (!strncmp (p, "V1_CLCM", 7)) 1537 return darwin_sections[objc_cls_meth_section]; 1538 else if (!strncmp (p, "V1_CLIM", 7)) 1539 return darwin_sections[objc_inst_meth_section]; 1540 else if (!strncmp (p, "V1_CACM", 7)) 1541 return darwin_sections[objc_cat_cls_meth_section]; 1542 else if (!strncmp (p, "V1_CAIM", 7)) 1543 return darwin_sections[objc_cat_inst_meth_section]; 1544 else if (!strncmp (p, "V1_PNSM", 7)) 1545 return darwin_sections[objc_cat_inst_meth_section]; 1546 else if (!strncmp (p, "V1_PCLM", 7)) 1547 return darwin_sections[objc_cat_cls_meth_section]; 1548 1549 else if (!strncmp (p, "V1_CLPR", 7)) 1550 return darwin_sections[objc_cat_cls_meth_section]; 1551 else if (!strncmp (p, "V1_CAPR", 7)) 1552 return darwin_sections[objc_category_section]; /* ??? CHECK me. */ 1553 1554 else if (!strncmp (p, "V1_PRFS", 7)) 1555 return darwin_sections[objc_cat_cls_meth_section]; 1556 else if (!strncmp (p, "V1_CLRF", 7)) 1557 return darwin_sections[objc_cls_refs_section]; 1558 else if (!strncmp (p, "V1_SRFS", 7)) 1559 return darwin_sections[objc_selector_refs_section]; 1560 1561 else if (!strncmp (p, "V1_MODU", 7)) 1562 return darwin_sections[objc_module_info_section]; 1563 else if (!strncmp (p, "V1_SYMT", 7)) 1564 return darwin_sections[objc_symbols_section]; 1565 else if (!strncmp (p, "V1_INFO", 7)) 1566 return darwin_sections[objc_image_info_section]; 1567 1568 else if (!strncmp (p, "V1_PLST", 7)) 1569 return darwin_sections[objc1_prop_list_section]; 1570 else if (!strncmp (p, "V1_PEXT", 7)) 1571 return darwin_sections[objc1_protocol_ext_section]; 1572 else if (!strncmp (p, "V1_CEXT", 7)) 1573 return darwin_sections[objc1_class_ext_section]; 1574 1575 else if (!strncmp (p, "V2_CSTR", 7)) 1576 return darwin_sections[objc_constant_string_object_section]; 1577 1578 return base; 1579 } 1580 1581 section * 1582 machopic_select_section (tree decl, 1583 int reloc, 1584 unsigned HOST_WIDE_INT align) 1585 { 1586 bool zsize, one, weak, use_coal, ro; 1587 section *base_section = NULL; 1588 1589 weak = (DECL_P (decl) 1590 && DECL_WEAK (decl) 1591 && !lookup_attribute ("weak_import", DECL_ATTRIBUTES (decl))); 1592 1593 zsize = (DECL_P (decl) 1594 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == CONST_DECL) 1595 && tree_to_uhwi (DECL_SIZE_UNIT (decl)) == 0); 1596 1597 one = DECL_P (decl) 1598 && TREE_CODE (decl) == VAR_DECL 1599 && DECL_COMDAT_GROUP (decl); 1600 1601 use_coal = (weak || one) && ld_uses_coal_sects; 1602 1603 ro = TREE_READONLY (decl) || TREE_CONSTANT (decl) ; 1604 1605 switch (categorize_decl_for_section (decl, reloc)) 1606 { 1607 case SECCAT_TEXT: 1608 gcc_unreachable (); 1609 break; 1610 1611 case SECCAT_RODATA: 1612 case SECCAT_SRODATA: 1613 base_section = darwin_rodata_section (use_coal, zsize, reloc); 1614 break; 1615 1616 case SECCAT_RODATA_MERGE_STR: 1617 base_section = darwin_mergeable_string_section (decl, align); 1618 break; 1619 1620 case SECCAT_RODATA_MERGE_STR_INIT: 1621 base_section = darwin_mergeable_string_section (DECL_INITIAL (decl), align); 1622 break; 1623 1624 case SECCAT_RODATA_MERGE_CONST: 1625 base_section = darwin_mergeable_constant_section (decl, align, zsize); 1626 break; 1627 1628 case SECCAT_DATA: 1629 case SECCAT_DATA_REL: 1630 case SECCAT_DATA_REL_LOCAL: 1631 case SECCAT_DATA_REL_RO: 1632 case SECCAT_DATA_REL_RO_LOCAL: 1633 case SECCAT_SDATA: 1634 case SECCAT_TDATA: 1635 if (use_coal) 1636 { 1637 if (ro) 1638 base_section = darwin_sections[const_data_coal_section]; 1639 else 1640 base_section = darwin_sections[data_coal_section]; 1641 } 1642 else if (DARWIN_SECTION_ANCHORS 1643 && flag_section_anchors 1644 && zsize) 1645 { 1646 /* If we're doing section anchors, then punt zero-sized objects into 1647 their own sections so that they don't interfere with offset 1648 computation for the remaining vars. This does not need to be done 1649 for stuff in mergeable sections, since these are ineligible for 1650 anchors. */ 1651 if (ro) 1652 base_section = darwin_sections[zobj_const_data_section]; 1653 else 1654 base_section = darwin_sections[zobj_data_section]; 1655 } 1656 else if (ro) 1657 base_section = darwin_sections[const_data_section]; 1658 else 1659 base_section = data_section; 1660 break; 1661 case SECCAT_BSS: 1662 case SECCAT_SBSS: 1663 case SECCAT_TBSS: 1664 if (use_coal) 1665 base_section = darwin_sections[data_coal_section]; 1666 else 1667 { 1668 if (!TREE_PUBLIC (decl)) 1669 base_section = lcomm_section; 1670 else if (bss_noswitch_section) 1671 base_section = bss_noswitch_section; 1672 else 1673 base_section = data_section; 1674 } 1675 break; 1676 1677 default: 1678 gcc_unreachable (); 1679 } 1680 1681 /* Darwin weird special cases. 1682 a) OBJC Meta-data. */ 1683 if (DECL_P (decl) 1684 && (TREE_CODE (decl) == VAR_DECL 1685 || TREE_CODE (decl) == CONST_DECL) 1686 && DECL_ATTRIBUTES (decl)) 1687 { 1688 tree meta = lookup_attribute ("OBJC2META", DECL_ATTRIBUTES (decl)); 1689 if (meta) 1690 return darwin_objc2_section (decl, meta, base_section); 1691 meta = lookup_attribute ("OBJC1META", DECL_ATTRIBUTES (decl)); 1692 if (meta) 1693 return darwin_objc1_section (decl, meta, base_section); 1694 meta = lookup_attribute ("OBJC1METG", DECL_ATTRIBUTES (decl)); 1695 if (meta) 1696 return base_section; /* GNU runtime is happy with it all in one pot. */ 1697 } 1698 1699 /* b) Constant string objects. */ 1700 if (TREE_CODE (decl) == CONSTRUCTOR 1701 && TREE_TYPE (decl) 1702 && TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE 1703 && TYPE_NAME (TREE_TYPE (decl))) 1704 { 1705 tree name = TYPE_NAME (TREE_TYPE (decl)); 1706 if (TREE_CODE (name) == TYPE_DECL) 1707 name = DECL_NAME (name); 1708 1709 if (!strcmp (IDENTIFIER_POINTER (name), "__builtin_ObjCString")) 1710 { 1711 if (flag_next_runtime) 1712 { 1713 if (flag_objc_abi == 2) 1714 return darwin_sections[objc2_constant_string_object_section]; 1715 else 1716 return darwin_sections[objc_constant_string_object_section]; 1717 } 1718 else 1719 return darwin_sections[objc_string_object_section]; 1720 } 1721 else if (!strcmp (IDENTIFIER_POINTER (name), "__builtin_CFString")) 1722 return darwin_sections[cfstring_constant_object_section]; 1723 else 1724 return base_section; 1725 } 1726 /* c) legacy meta-data selection. */ 1727 else if (TREE_CODE (decl) == VAR_DECL 1728 && DECL_NAME (decl) 1729 && TREE_CODE (DECL_NAME (decl)) == IDENTIFIER_NODE 1730 && IDENTIFIER_POINTER (DECL_NAME (decl)) 1731 && flag_next_runtime 1732 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "_OBJC_", 6)) 1733 { 1734 const char *name = IDENTIFIER_POINTER (DECL_NAME (decl)); 1735 static bool warned_objc_46 = false; 1736 /* We shall assert that zero-sized objects are an error in ObjC 1737 meta-data. */ 1738 gcc_assert (tree_to_uhwi (DECL_SIZE_UNIT (decl)) != 0); 1739 1740 /* ??? This mechanism for determining the metadata section is 1741 broken when LTO is in use, since the frontend that generated 1742 the data is not identified. We will keep the capability for 1743 the short term - in case any non-Objective-C programs are using 1744 it to place data in specified sections. */ 1745 if (!warned_objc_46) 1746 { 1747 location_t loc = DECL_SOURCE_LOCATION (decl); 1748 warning_at (loc, 0, "the use of _OBJC_-prefixed variable names" 1749 " to select meta-data sections is deprecated at 4.6" 1750 " and will be removed in 4.7"); 1751 warned_objc_46 = true; 1752 } 1753 1754 if (!strncmp (name, "_OBJC_CLASS_METHODS_", 20)) 1755 return darwin_sections[objc_cls_meth_section]; 1756 else if (!strncmp (name, "_OBJC_INSTANCE_METHODS_", 23)) 1757 return darwin_sections[objc_inst_meth_section]; 1758 else if (!strncmp (name, "_OBJC_CATEGORY_CLASS_METHODS_", 29)) 1759 return darwin_sections[objc_cat_cls_meth_section]; 1760 else if (!strncmp (name, "_OBJC_CATEGORY_INSTANCE_METHODS_", 32)) 1761 return darwin_sections[objc_cat_inst_meth_section]; 1762 else if (!strncmp (name, "_OBJC_CLASS_VARIABLES_", 22)) 1763 return darwin_sections[objc_class_vars_section]; 1764 else if (!strncmp (name, "_OBJC_INSTANCE_VARIABLES_", 25)) 1765 return darwin_sections[objc_instance_vars_section]; 1766 else if (!strncmp (name, "_OBJC_CLASS_PROTOCOLS_", 22)) 1767 return darwin_sections[objc_cat_cls_meth_section]; 1768 else if (!strncmp (name, "_OBJC_CLASS_NAME_", 17)) 1769 return darwin_sections[objc_class_names_section]; 1770 else if (!strncmp (name, "_OBJC_METH_VAR_NAME_", 20)) 1771 return darwin_sections[objc_meth_var_names_section]; 1772 else if (!strncmp (name, "_OBJC_METH_VAR_TYPE_", 20)) 1773 return darwin_sections[objc_meth_var_types_section]; 1774 else if (!strncmp (name, "_OBJC_CLASS_REFERENCES", 22)) 1775 return darwin_sections[objc_cls_refs_section]; 1776 else if (!strncmp (name, "_OBJC_CLASS_", 12)) 1777 return darwin_sections[objc_class_section]; 1778 else if (!strncmp (name, "_OBJC_METACLASS_", 16)) 1779 return darwin_sections[objc_meta_class_section]; 1780 else if (!strncmp (name, "_OBJC_CATEGORY_", 15)) 1781 return darwin_sections[objc_category_section]; 1782 else if (!strncmp (name, "_OBJC_SELECTOR_REFERENCES", 25)) 1783 return darwin_sections[objc_selector_refs_section]; 1784 else if (!strncmp (name, "_OBJC_SELECTOR_FIXUP", 20)) 1785 return darwin_sections[objc_selector_fixup_section]; 1786 else if (!strncmp (name, "_OBJC_SYMBOLS", 13)) 1787 return darwin_sections[objc_symbols_section]; 1788 else if (!strncmp (name, "_OBJC_MODULES", 13)) 1789 return darwin_sections[objc_module_info_section]; 1790 else if (!strncmp (name, "_OBJC_IMAGE_INFO", 16)) 1791 return darwin_sections[objc_image_info_section]; 1792 else if (!strncmp (name, "_OBJC_PROTOCOL_INSTANCE_METHODS_", 32)) 1793 return darwin_sections[objc_cat_inst_meth_section]; 1794 else if (!strncmp (name, "_OBJC_PROTOCOL_CLASS_METHODS_", 29)) 1795 return darwin_sections[objc_cat_cls_meth_section]; 1796 else if (!strncmp (name, "_OBJC_PROTOCOL_REFS_", 20)) 1797 return darwin_sections[objc_cat_cls_meth_section]; 1798 else if (!strncmp (name, "_OBJC_PROTOCOL_", 15)) 1799 return darwin_sections[objc_protocol_section]; 1800 else 1801 return base_section; 1802 } 1803 1804 return base_section; 1805 } 1806 1807 /* This can be called with address expressions as "rtx". 1808 They must go in "const". */ 1809 1810 section * 1811 machopic_select_rtx_section (machine_mode mode, rtx x, 1812 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED) 1813 { 1814 if (GET_MODE_SIZE (mode) == 8 1815 && (GET_CODE (x) == CONST_INT 1816 || GET_CODE (x) == CONST_WIDE_INT 1817 || GET_CODE (x) == CONST_DOUBLE)) 1818 return darwin_sections[literal8_section]; 1819 else if (GET_MODE_SIZE (mode) == 4 1820 && (GET_CODE (x) == CONST_INT 1821 || GET_CODE (x) == CONST_WIDE_INT 1822 || GET_CODE (x) == CONST_DOUBLE)) 1823 return darwin_sections[literal4_section]; 1824 else if (HAVE_GAS_LITERAL16 1825 && TARGET_64BIT 1826 && GET_MODE_SIZE (mode) == 16 1827 && (GET_CODE (x) == CONST_INT 1828 || GET_CODE (x) == CONST_WIDE_INT 1829 || GET_CODE (x) == CONST_DOUBLE 1830 || GET_CODE (x) == CONST_VECTOR)) 1831 return darwin_sections[literal16_section]; 1832 else if (MACHOPIC_INDIRECT 1833 && (GET_CODE (x) == SYMBOL_REF 1834 || GET_CODE (x) == CONST 1835 || GET_CODE (x) == LABEL_REF)) 1836 return darwin_sections[const_data_section]; 1837 else 1838 return darwin_sections[const_section]; 1839 } 1840 1841 void 1842 machopic_asm_out_constructor (rtx symbol, int priority ATTRIBUTE_UNUSED) 1843 { 1844 cdtor_record new_elt = {symbol, priority, vec_safe_length (ctors)}; 1845 1846 vec_safe_push (ctors, new_elt); 1847 1848 if (! MACHOPIC_INDIRECT) 1849 fprintf (asm_out_file, ".reference .constructors_used\n"); 1850 } 1851 1852 void 1853 machopic_asm_out_destructor (rtx symbol, int priority ATTRIBUTE_UNUSED) 1854 { 1855 cdtor_record new_elt = {symbol, priority, vec_safe_length (dtors)}; 1856 1857 vec_safe_push (dtors, new_elt); 1858 1859 if (! MACHOPIC_INDIRECT) 1860 fprintf (asm_out_file, ".reference .destructors_used\n"); 1861 } 1862 1863 static int 1864 sort_cdtor_records (const void * a, const void * b) 1865 { 1866 const cdtor_record *cda = (const cdtor_record *)a; 1867 const cdtor_record *cdb = (const cdtor_record *)b; 1868 if (cda->priority > cdb->priority) 1869 return 1; 1870 if (cda->priority < cdb->priority) 1871 return -1; 1872 if (cda->position > cdb->position) 1873 return 1; 1874 if (cda->position < cdb->position) 1875 return -1; 1876 return 0; 1877 } 1878 1879 static void 1880 finalize_ctors () 1881 { 1882 unsigned int i; 1883 cdtor_record *elt; 1884 1885 if (MACHOPIC_INDIRECT) 1886 switch_to_section (darwin_sections[mod_init_section]); 1887 else 1888 switch_to_section (darwin_sections[constructor_section]); 1889 1890 if (vec_safe_length (ctors) > 1) 1891 ctors->qsort (sort_cdtor_records); 1892 FOR_EACH_VEC_SAFE_ELT (ctors, i, elt) 1893 { 1894 assemble_align (POINTER_SIZE); 1895 assemble_integer (elt->symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1); 1896 } 1897 } 1898 1899 static void 1900 finalize_dtors () 1901 { 1902 unsigned int i; 1903 cdtor_record *elt; 1904 1905 if (MACHOPIC_INDIRECT) 1906 switch_to_section (darwin_sections[mod_term_section]); 1907 else 1908 switch_to_section (darwin_sections[destructor_section]); 1909 1910 if (vec_safe_length (dtors) > 1) 1911 dtors->qsort (sort_cdtor_records); 1912 FOR_EACH_VEC_SAFE_ELT (dtors, i, elt) 1913 { 1914 assemble_align (POINTER_SIZE); 1915 assemble_integer (elt->symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1); 1916 } 1917 } 1918 1919 void 1920 darwin_globalize_label (FILE *stream, const char *name) 1921 { 1922 if (!!strncmp (name, "_OBJC_", 6)) 1923 default_globalize_label (stream, name); 1924 } 1925 1926 /* This routine returns non-zero if 'name' starts with the special objective-c 1927 anonymous file-scope static name. It accommodates c++'s mangling of such 1928 symbols (in this case the symbols will have form _ZL{d}*_OBJC_* d=digit). */ 1929 1930 int 1931 darwin_label_is_anonymous_local_objc_name (const char *name) 1932 { 1933 const unsigned char *p = (const unsigned char *) name; 1934 if (*p != '_') 1935 return 0; 1936 if (p[1] == 'Z' && p[2] == 'L') 1937 { 1938 p += 3; 1939 while (*p >= '0' && *p <= '9') 1940 p++; 1941 } 1942 return (!strncmp ((const char *)p, "_OBJC_", 6)); 1943 } 1944 1945 /* LTO support for Mach-O. 1946 1947 This version uses three mach-o sections to encapsulate the (unlimited 1948 number of) lto sections. 1949 1950 __GNU_LTO, __lto_sections contains the concatented GNU LTO section data. 1951 __GNU_LTO, __section_names contains the GNU LTO section names. 1952 __GNU_LTO, __section_index contains an array of values that index these. 1953 1954 Indexed thus: 1955 <section offset from the start of __GNU_LTO, __lto_sections>, 1956 <section length> 1957 <name offset from the start of __GNU_LTO, __section_names, 1958 <name length>. 1959 1960 At present, for both m32 and m64 mach-o files each of these fields is 1961 represented by a uint32_t. This is because, AFAICT, a mach-o object 1962 cannot exceed 4Gb because the section_64 offset field (see below) is 32bits. 1963 1964 uint32_t offset; 1965 "offset An integer specifying the offset to this section in the file." */ 1966 1967 /* Count lto section numbers. */ 1968 static unsigned int lto_section_num = 0; 1969 1970 /* A vector of information about LTO sections, at present, we only have 1971 the name. TODO: see if we can get the data length somehow. */ 1972 typedef struct GTY (()) darwin_lto_section_e { 1973 const char *sectname; 1974 } darwin_lto_section_e ; 1975 1976 static GTY (()) vec<darwin_lto_section_e, va_gc> *lto_section_names; 1977 1978 /* Section wrapper scheme (used here to wrap the unlimited number of LTO 1979 sections into three Mach-O ones). 1980 NOTE: These names MUST be kept in sync with those in 1981 libiberty/simple-object-mach-o. */ 1982 #define LTO_SECTS_SECTION "__wrapper_sects" 1983 #define LTO_NAMES_SECTION "__wrapper_names" 1984 #define LTO_INDEX_SECTION "__wrapper_index" 1985 1986 /* File to temporarily store LTO data. This is appended to asm_out_file 1987 in darwin_end_file. */ 1988 static FILE *lto_asm_out_file, *saved_asm_out_file; 1989 static char *lto_asm_out_name; 1990 static enum debug_info_levels saved_debug_info_level; 1991 1992 /* Prepare asm_out_file for LTO output. For darwin, this means hiding 1993 asm_out_file and switching to an alternative output file. */ 1994 void 1995 darwin_asm_lto_start (void) 1996 { 1997 gcc_assert (! saved_asm_out_file); 1998 saved_asm_out_file = asm_out_file; 1999 saved_debug_info_level = debug_info_level; 2000 debug_info_level = DINFO_LEVEL_NONE; 2001 if (! lto_asm_out_name) 2002 lto_asm_out_name = make_temp_file (".lto.s"); 2003 lto_asm_out_file = fopen (lto_asm_out_name, "a"); 2004 if (lto_asm_out_file == NULL) 2005 fatal_error (input_location, 2006 "failed to open temporary file %s for LTO output", 2007 lto_asm_out_name); 2008 asm_out_file = lto_asm_out_file; 2009 } 2010 2011 /* Restore asm_out_file. */ 2012 void 2013 darwin_asm_lto_end (void) 2014 { 2015 gcc_assert (saved_asm_out_file); 2016 fclose (lto_asm_out_file); 2017 asm_out_file = saved_asm_out_file; 2018 saved_asm_out_file = NULL; 2019 debug_info_level = saved_debug_info_level; 2020 } 2021 2022 static void 2023 darwin_asm_dwarf_section (const char *name, unsigned int flags, 2024 tree decl, bool is_for_lto); 2025 2026 /* Called for the TARGET_ASM_NAMED_SECTION hook. */ 2027 2028 void 2029 darwin_asm_named_section (const char *name, 2030 unsigned int flags, 2031 tree decl ATTRIBUTE_UNUSED) 2032 { 2033 /* LTO sections go in a special section that encapsulates the (unlimited) 2034 number of GNU LTO sections within a single mach-o one. */ 2035 if (strncmp (name, LTO_SECTION_NAME_PREFIX, 2036 strlen (LTO_SECTION_NAME_PREFIX)) == 0) 2037 { 2038 darwin_lto_section_e e; 2039 /* We expect certain flags to be set... */ 2040 gcc_assert ((flags & (SECTION_DEBUG | SECTION_NAMED)) 2041 == (SECTION_DEBUG | SECTION_NAMED)); 2042 2043 /* Switch to our combined section. */ 2044 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n", 2045 LTO_SEGMENT_NAME, LTO_SECTS_SECTION); 2046 /* Output a label for the start of this sub-section. */ 2047 fprintf (asm_out_file, "L_GNU_LTO%d:\t;# %s\n", 2048 lto_section_num, name); 2049 /* We have to jump through hoops to get the values of the intra-section 2050 offsets... */ 2051 fprintf (asm_out_file, "\t.set L$gnu$lto$offs%d,L_GNU_LTO%d-L_GNU_LTO0\n", 2052 lto_section_num, lto_section_num); 2053 fprintf (asm_out_file, 2054 "\t.set L$gnu$lto$size%d,L_GNU_LTO%d-L_GNU_LTO%d\n", 2055 lto_section_num, lto_section_num+1, lto_section_num); 2056 lto_section_num++; 2057 e.sectname = xstrdup (name); 2058 /* Keep the names, we'll need to make a table later. 2059 TODO: check that we do not revisit sections, that would break 2060 the assumption of how this is done. */ 2061 if (lto_section_names == NULL) 2062 vec_alloc (lto_section_names, 16); 2063 vec_safe_push (lto_section_names, e); 2064 } 2065 else if (strncmp (name, "__DWARF,", 8) == 0) 2066 darwin_asm_dwarf_section (name, flags, decl, false); 2067 else if (strncmp (name, "__GNU_DWARF_LTO,", 16) == 0) 2068 darwin_asm_dwarf_section (name, flags, decl, true); 2069 else 2070 fprintf (asm_out_file, "\t.section %s\n", name); 2071 } 2072 2073 void 2074 darwin_unique_section (tree decl ATTRIBUTE_UNUSED, int reloc ATTRIBUTE_UNUSED) 2075 { 2076 /* Darwin does not use unique sections. */ 2077 } 2078 2079 /* Handle __attribute__ ((apple_kext_compatibility)). 2080 This only applies to darwin kexts for 2.95 compatibility -- it shrinks the 2081 vtable for classes with this attribute (and their descendants) by not 2082 outputting the new 3.0 nondeleting destructor. This means that such 2083 objects CANNOT be allocated on the stack or as globals UNLESS they have 2084 a completely empty `operator delete'. 2085 Luckily, this fits in with the Darwin kext model. 2086 2087 This attribute also disables gcc3's potential overlaying of derived 2088 class data members on the padding at the end of the base class. */ 2089 2090 tree 2091 darwin_handle_kext_attribute (tree *node, tree name, 2092 tree args ATTRIBUTE_UNUSED, 2093 int flags ATTRIBUTE_UNUSED, 2094 bool *no_add_attrs) 2095 { 2096 /* APPLE KEXT stuff -- only applies with pure static C++ code. */ 2097 if (! TARGET_KEXTABI) 2098 { 2099 warning (0, "%qE 2.95 vtable-compatibility attribute applies " 2100 "only when compiling a kext", name); 2101 2102 *no_add_attrs = true; 2103 } 2104 else if (TREE_CODE (*node) != RECORD_TYPE) 2105 { 2106 warning (0, "%qE 2.95 vtable-compatibility attribute applies " 2107 "only to C++ classes", name); 2108 2109 *no_add_attrs = true; 2110 } 2111 2112 return NULL_TREE; 2113 } 2114 2115 /* Handle a "weak_import" attribute; arguments as in 2116 struct attribute_spec.handler. */ 2117 2118 tree 2119 darwin_handle_weak_import_attribute (tree *node, tree name, 2120 tree ARG_UNUSED (args), 2121 int ARG_UNUSED (flags), 2122 bool * no_add_attrs) 2123 { 2124 if (TREE_CODE (*node) != FUNCTION_DECL && TREE_CODE (*node) != VAR_DECL) 2125 { 2126 warning (OPT_Wattributes, "%qE attribute ignored", 2127 name); 2128 *no_add_attrs = true; 2129 } 2130 else 2131 declare_weak (*node); 2132 2133 return NULL_TREE; 2134 } 2135 2136 /* Emit a label for an FDE, making it global and/or weak if appropriate. 2137 The third parameter is nonzero if this is for exception handling. 2138 The fourth parameter is nonzero if this is just a placeholder for an 2139 FDE that we are omitting. */ 2140 2141 void 2142 darwin_emit_unwind_label (FILE *file, tree decl, int for_eh, int empty) 2143 { 2144 char *lab ; 2145 char buf[32]; 2146 static int invok_count = 0; 2147 static tree last_fun_decl = NULL_TREE; 2148 2149 /* Modern linkers can produce distinct FDEs without compiler support. */ 2150 if (! for_eh || ! ld_needs_eh_markers) 2151 return; 2152 2153 /* FIXME: This only works when the eh for all sections of a function are 2154 emitted at the same time. If that changes, we would need to use a lookup 2155 table of some form to determine what to do. Also, we should emit the 2156 unadorned label for the partition containing the public label for a 2157 function. This is of limited use, probably, since we do not currently 2158 enable partitioning. */ 2159 strcpy (buf, ".eh"); 2160 if (decl && TREE_CODE (decl) == FUNCTION_DECL) 2161 { 2162 if (decl == last_fun_decl) 2163 { 2164 invok_count++; 2165 snprintf (buf, 31, "$$part$$%d.eh", invok_count); 2166 } 2167 else 2168 { 2169 last_fun_decl = decl; 2170 invok_count = 0; 2171 } 2172 } 2173 2174 lab = concat (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), buf, NULL); 2175 2176 if (TREE_PUBLIC (decl)) 2177 { 2178 targetm.asm_out.globalize_label (file, lab); 2179 if (DECL_VISIBILITY (decl) == VISIBILITY_HIDDEN) 2180 { 2181 fputs ("\t.private_extern ", file); 2182 assemble_name (file, lab); 2183 fputc ('\n', file); 2184 } 2185 } 2186 2187 if (DECL_WEAK (decl)) 2188 { 2189 fputs ("\t.weak_definition ", file); 2190 assemble_name (file, lab); 2191 fputc ('\n', file); 2192 } 2193 2194 assemble_name (file, lab); 2195 if (empty) 2196 { 2197 fputs (" = 0\n", file); 2198 2199 /* Mark the absolute .eh and .eh1 style labels as needed to 2200 ensure that we don't dead code strip them and keep such 2201 labels from another instantiation point until we can fix this 2202 properly with group comdat support. */ 2203 darwin_mark_decl_preserved (lab); 2204 } 2205 else 2206 fputs (":\n", file); 2207 2208 free (lab); 2209 } 2210 2211 static GTY(()) unsigned long except_table_label_num; 2212 2213 void 2214 darwin_emit_except_table_label (FILE *file) 2215 { 2216 char section_start_label[30]; 2217 2218 ASM_GENERATE_INTERNAL_LABEL (section_start_label, "GCC_except_table", 2219 except_table_label_num++); 2220 ASM_OUTPUT_LABEL (file, section_start_label); 2221 } 2222 /* Generate a PC-relative reference to a Mach-O non-lazy-symbol. */ 2223 2224 void 2225 darwin_non_lazy_pcrel (FILE *file, rtx addr) 2226 { 2227 const char *nlp_name; 2228 2229 gcc_assert (GET_CODE (addr) == SYMBOL_REF); 2230 2231 nlp_name = machopic_indirection_name (addr, /*stub_p=*/false); 2232 fputs ("\t.long\t", file); 2233 ASM_OUTPUT_LABELREF (file, nlp_name); 2234 fputs ("-.", file); 2235 } 2236 2237 /* If this is uncommented, details of each allocation will be printed 2238 in the asm right before the actual code. WARNING - this will cause some 2239 test-suite fails (since the printout will contain items that some tests 2240 are not expecting) -- so don't leave it on by default (it bloats the 2241 asm too). */ 2242 /*#define DEBUG_DARWIN_MEM_ALLOCATORS*/ 2243 2244 /* The first two of these routines are ostensibly just intended to put 2245 names into the asm. However, they are both hijacked in order to ensure 2246 that zero-sized items do not make their way into the output. Consequently, 2247 we also need to make these participate in provisions for dealing with 2248 such items in section anchors. */ 2249 2250 /* The implementation of ASM_DECLARE_OBJECT_NAME. */ 2251 /* The RTTI data (e.g., __ti4name) is common and public (and static), 2252 but it does need to be referenced via indirect PIC data pointers. 2253 The machopic_define_symbol calls are telling the machopic subsystem 2254 that the name *is* defined in this module, so it doesn't need to 2255 make them indirect. */ 2256 void 2257 darwin_asm_declare_object_name (FILE *file, 2258 const char *nam, tree decl) 2259 { 2260 const char *xname = nam; 2261 unsigned HOST_WIDE_INT size; 2262 bool local_def, weak; 2263 2264 weak = (DECL_P (decl) 2265 && DECL_WEAK (decl) 2266 && !lookup_attribute ("weak_import", 2267 DECL_ATTRIBUTES (decl))); 2268 2269 local_def = DECL_INITIAL (decl) || (TREE_STATIC (decl) 2270 && (!DECL_COMMON (decl) 2271 || !TREE_PUBLIC (decl))); 2272 2273 if (GET_CODE (XEXP (DECL_RTL (decl), 0)) != SYMBOL_REF) 2274 xname = IDENTIFIER_POINTER (DECL_NAME (decl)); 2275 2276 if (local_def) 2277 { 2278 (* targetm.encode_section_info) (decl, DECL_RTL (decl), false); 2279 if (!weak) 2280 machopic_define_symbol (DECL_RTL (decl)); 2281 } 2282 2283 size = tree_to_uhwi (DECL_SIZE_UNIT (decl)); 2284 2285 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS 2286 fprintf (file, "# dadon: %s %s (%llu, %u) local %d weak %d" 2287 " stat %d com %d pub %d t-const %d t-ro %d init %lx\n", 2288 xname, (TREE_CODE (decl) == VAR_DECL?"var":"const"), 2289 (unsigned long long)size, DECL_ALIGN (decl), local_def, 2290 DECL_WEAK (decl), TREE_STATIC (decl), DECL_COMMON (decl), 2291 TREE_PUBLIC (decl), TREE_CONSTANT (decl), TREE_READONLY (decl), 2292 (unsigned long)DECL_INITIAL (decl)); 2293 #endif 2294 2295 /* Darwin needs help to support local zero-sized objects. 2296 They must be made at least one byte, and the section containing must be 2297 marked as unsuitable for section-anchors (see storage allocators below). 2298 2299 For non-zero objects this output is handled by varasm.c. 2300 */ 2301 if (!size) 2302 { 2303 unsigned int l2align = 0; 2304 2305 /* The align must be honored, even for zero-sized. */ 2306 if (DECL_ALIGN (decl)) 2307 { 2308 l2align = floor_log2 (DECL_ALIGN (decl) / BITS_PER_UNIT); 2309 fprintf (file, "\t.align\t%u\n", l2align); 2310 } 2311 2312 ASM_OUTPUT_LABEL (file, xname); 2313 size = 1; 2314 fprintf (file, "\t.space\t" HOST_WIDE_INT_PRINT_UNSIGNED"\n", size); 2315 2316 /* Check that we've correctly picked up the zero-sized item and placed it 2317 properly. */ 2318 gcc_assert ((!DARWIN_SECTION_ANCHORS || !flag_section_anchors) 2319 || (in_section 2320 && (in_section->common.flags & SECTION_NO_ANCHOR))); 2321 } 2322 else 2323 ASM_OUTPUT_LABEL (file, xname); 2324 } 2325 2326 /* The implementation of ASM_DECLARE_CONSTANT_NAME. */ 2327 void 2328 darwin_asm_declare_constant_name (FILE *file, const char *name, 2329 const_tree exp ATTRIBUTE_UNUSED, 2330 HOST_WIDE_INT size) 2331 { 2332 assemble_label (file, name); 2333 /* As for other items, we need at least one byte. */ 2334 if (!size) 2335 { 2336 fputs ("\t.space\t1\n", file); 2337 /* Check that we've correctly picked up the zero-sized item and placed it 2338 properly. */ 2339 gcc_assert ((!DARWIN_SECTION_ANCHORS || !flag_section_anchors) 2340 || (in_section 2341 && (in_section->common.flags & SECTION_NO_ANCHOR))); 2342 } 2343 } 2344 2345 /* Darwin storage allocators. 2346 2347 Zerofill sections are desirable for large blank data since, otherwise, these 2348 data bloat objects (PR33210). 2349 2350 However, section anchors don't work in .zerofill sections (one cannot switch 2351 to a zerofill section). Ergo, for Darwin targets using section anchors we need 2352 to put (at least some) data into 'normal' switchable sections. 2353 2354 Here we set a relatively arbitrary value for the size of an object to trigger 2355 zerofill when section anchors are enabled (anything bigger than a page for 2356 current Darwin implementations). FIXME: there ought to be some objective way 2357 to make this choice. 2358 2359 When section anchor are off this is ignored anyway. */ 2360 2361 #define BYTES_ZFILL 4096 2362 2363 /* Emit a chunk of data for items coalesced by the linker. */ 2364 static void 2365 darwin_emit_weak_or_comdat (FILE *fp, tree decl, const char *name, 2366 unsigned HOST_WIDE_INT size, 2367 bool use_coal, 2368 unsigned int align) 2369 { 2370 /* Since the sections used here are coalesced, they will not be eligible 2371 for section anchors, and therefore we don't need to break that out. 2372 CHECKME: for modern linker on PowerPC. */ 2373 if (TREE_READONLY (decl) || TREE_CONSTANT (decl)) 2374 switch_to_section (use_coal ? darwin_sections[const_data_coal_section] 2375 : darwin_sections[const_data_section]); 2376 else 2377 switch_to_section (use_coal ? darwin_sections[data_coal_section] 2378 : data_section); 2379 2380 /* To be consistent, we'll allow darwin_asm_declare_object_name to assemble 2381 the align info for zero-sized items... but do it here otherwise. */ 2382 if (size && align) 2383 fprintf (fp, "\t.align\t%d\n", floor_log2 (align / BITS_PER_UNIT)); 2384 2385 if (TREE_PUBLIC (decl)) 2386 darwin_globalize_label (fp, name); 2387 2388 /* ... and we let it deal with outputting one byte of zero for them too. */ 2389 darwin_asm_declare_object_name (fp, name, decl); 2390 if (size) 2391 assemble_zeros (size); 2392 } 2393 2394 /* Emit a chunk of data for ObjC meta-data that got placed in BSS erroneously. */ 2395 static void 2396 darwin_emit_objc_zeroed (FILE *fp, tree decl, const char *name, 2397 unsigned HOST_WIDE_INT size, 2398 unsigned int align, tree meta) 2399 { 2400 section *ocs = data_section; 2401 2402 if (TREE_PURPOSE (meta) == get_identifier("OBJC2META")) 2403 ocs = darwin_objc2_section (decl, meta, ocs); 2404 else 2405 ocs = darwin_objc1_section (decl, meta, ocs); 2406 2407 switch_to_section (ocs); 2408 2409 /* We shall declare that zero-sized meta-data are not valid (yet). */ 2410 gcc_assert (size); 2411 fprintf (fp, "\t.align\t%d\n", floor_log2 (align / BITS_PER_UNIT)); 2412 2413 /* ... and we let it deal with outputting one byte of zero for them too. */ 2414 darwin_asm_declare_object_name (fp, name, decl); 2415 assemble_zeros (size); 2416 } 2417 2418 /* This routine emits 'local' storage: 2419 2420 When Section Anchors are off this routine emits .zerofill commands in 2421 sections named for their alignment. 2422 2423 When Section Anchors are on, smaller (non-zero-sized) items are placed in 2424 the .static_data section so that the section anchoring system can see them. 2425 Larger items are still placed in .zerofill sections, addressing PR33210. 2426 The routine has no checking - it is all assumed to be done by the caller. 2427 */ 2428 static void 2429 darwin_emit_local_bss (FILE *fp, tree decl, const char *name, 2430 unsigned HOST_WIDE_INT size, 2431 unsigned int l2align) 2432 { 2433 /* FIXME: We have a fudge to make this work with Java even when the target does 2434 not use sections anchors -- Java seems to need at least one small item in a 2435 non-zerofill segment. */ 2436 if ((DARWIN_SECTION_ANCHORS && flag_section_anchors && size < BYTES_ZFILL) 2437 || (size && size <= 2)) 2438 { 2439 /* Put smaller objects in _static_data, where the section anchors system 2440 can get them. 2441 However, if they are zero-sized punt them to yet a different section 2442 (that is not allowed to participate in anchoring). */ 2443 if (!size) 2444 { 2445 fputs ("\t.section\t__DATA,__zobj_bss\n", fp); 2446 in_section = darwin_sections[zobj_bss_section]; 2447 size = 1; 2448 } 2449 else 2450 { 2451 fputs ("\t.static_data\n", fp); 2452 in_section = darwin_sections[static_data_section]; 2453 } 2454 2455 if (l2align) 2456 fprintf (fp, "\t.align\t%u\n", l2align); 2457 2458 assemble_name (fp, name); 2459 fprintf (fp, ":\n\t.space\t" HOST_WIDE_INT_PRINT_UNSIGNED"\n", size); 2460 } 2461 else 2462 { 2463 /* When we are on a non-section anchor target, we can get zero-sized 2464 items here. However, all we need to do is to bump them to one byte 2465 and the section alignment will take care of the rest. */ 2466 char secnam[64]; 2467 unsigned int flags ; 2468 snprintf (secnam, 64, "__DATA,__%sbss%u", ((size)?"":"zo_"), 2469 (unsigned) l2align); 2470 /* We can't anchor (yet, if ever) in zerofill sections, because we can't 2471 switch to them and emit a label. */ 2472 flags = SECTION_BSS|SECTION_WRITE|SECTION_NO_ANCHOR; 2473 in_section = get_section (secnam, flags, NULL); 2474 fprintf (fp, "\t.zerofill %s,", secnam); 2475 assemble_name (fp, name); 2476 if (!size) 2477 size = 1; 2478 2479 if (l2align) 2480 fprintf (fp, "," HOST_WIDE_INT_PRINT_UNSIGNED",%u\n", 2481 size, (unsigned) l2align); 2482 else 2483 fprintf (fp, "," HOST_WIDE_INT_PRINT_UNSIGNED"\n", size); 2484 } 2485 2486 (*targetm.encode_section_info) (decl, DECL_RTL (decl), false); 2487 /* This is defined as a file-scope var, so we know to notify machopic. */ 2488 machopic_define_symbol (DECL_RTL (decl)); 2489 } 2490 2491 /* Emit a chunk of common. */ 2492 static void 2493 darwin_emit_common (FILE *fp, const char *name, 2494 unsigned HOST_WIDE_INT size, unsigned int align) 2495 { 2496 unsigned HOST_WIDE_INT rounded; 2497 unsigned int l2align; 2498 2499 /* Earlier systems complain if the alignment exceeds the page size. 2500 The magic number is 4096 * 8 - hard-coded for legacy systems. */ 2501 if (!emit_aligned_common && (align > 32768UL)) 2502 align = 4096UL; /* In units. */ 2503 else 2504 align /= BITS_PER_UNIT; 2505 2506 /* Make sure we have a meaningful align. */ 2507 if (!align) 2508 align = 1; 2509 2510 /* For earlier toolchains, we need to emit the var as a rounded size to 2511 tell ld the alignment. */ 2512 if (size < align) 2513 rounded = align; 2514 else 2515 rounded = (size + (align-1)) & ~(align-1); 2516 2517 l2align = floor_log2 (align); 2518 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT); 2519 2520 in_section = comm_section; 2521 /* We mustn't allow multiple public symbols to share an address when using 2522 the normal OSX toolchain. */ 2523 if (!size) 2524 { 2525 /* Put at least one byte. */ 2526 size = 1; 2527 /* This section can no longer participate in section anchoring. */ 2528 comm_section->common.flags |= SECTION_NO_ANCHOR; 2529 } 2530 2531 fputs ("\t.comm\t", fp); 2532 assemble_name (fp, name); 2533 fprintf (fp, "," HOST_WIDE_INT_PRINT_UNSIGNED, 2534 emit_aligned_common?size:rounded); 2535 if (l2align && emit_aligned_common) 2536 fprintf (fp, ",%u", l2align); 2537 fputs ("\n", fp); 2538 } 2539 2540 /* Output a var which is all zero - into aligned BSS sections, common, lcomm 2541 or coalescable data sections (for weak or comdat) as appropriate. */ 2542 2543 void 2544 darwin_output_aligned_bss (FILE *fp, tree decl, const char *name, 2545 unsigned HOST_WIDE_INT size, unsigned int align) 2546 { 2547 unsigned int l2align; 2548 bool one, pub, weak; 2549 tree meta; 2550 2551 pub = TREE_PUBLIC (decl); 2552 one = DECL_ONE_ONLY (decl); 2553 weak = (DECL_P (decl) 2554 && DECL_WEAK (decl) 2555 && !lookup_attribute ("weak_import", 2556 DECL_ATTRIBUTES (decl))); 2557 2558 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS 2559 fprintf (fp, "# albss: %s (%lld,%d) ro %d cst %d stat %d com %d" 2560 " pub %d weak %d one %d init %lx\n", 2561 name, (long long)size, (int)align, TREE_READONLY (decl), 2562 TREE_CONSTANT (decl), TREE_STATIC (decl), DECL_COMMON (decl), 2563 pub, weak, one, (unsigned long)DECL_INITIAL (decl)); 2564 #endif 2565 2566 /* ObjC metadata can get put in BSS because varasm.c decides it's BSS 2567 before the target has a chance to comment. */ 2568 if ((meta = is_objc_metadata (decl))) 2569 { 2570 darwin_emit_objc_zeroed (fp, decl, name, size, DECL_ALIGN (decl), meta); 2571 return; 2572 } 2573 2574 /* Check that any initializer is valid. */ 2575 gcc_assert ((DECL_INITIAL (decl) == NULL) 2576 || (DECL_INITIAL (decl) == error_mark_node) 2577 || initializer_zerop (DECL_INITIAL (decl))); 2578 2579 gcc_assert (DECL_SECTION_NAME (decl) == NULL); 2580 gcc_assert (!DECL_COMMON (decl)); 2581 2582 /* Pick up the correct alignment. */ 2583 if (!size || !align) 2584 align = DECL_ALIGN (decl); 2585 2586 l2align = floor_log2 (align / BITS_PER_UNIT); 2587 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT); 2588 2589 last_assemble_variable_decl = decl; 2590 2591 /* We would rather not have to check this here - but it seems that we might 2592 be passed a decl that should be in coalesced space. */ 2593 if (one || weak) 2594 { 2595 /* Weak or COMDAT objects are put in mergeable sections. */ 2596 darwin_emit_weak_or_comdat (fp, decl, name, size, 2597 ld_uses_coal_sects, DECL_ALIGN (decl)); 2598 return; 2599 } 2600 2601 /* If this is not public, then emit according to local rules. */ 2602 if (!pub) 2603 { 2604 darwin_emit_local_bss (fp, decl, name, size, l2align); 2605 return; 2606 } 2607 2608 /* So we have a public symbol (small item fudge for Java, see above). */ 2609 if ((DARWIN_SECTION_ANCHORS && flag_section_anchors && size < BYTES_ZFILL) 2610 || (size && size <= 2)) 2611 { 2612 /* Put smaller objects in data, where the section anchors system can get 2613 them. However, if they are zero-sized punt them to yet a different 2614 section (that is not allowed to participate in anchoring). */ 2615 if (!size) 2616 { 2617 fputs ("\t.section\t__DATA,__zobj_data\n", fp); 2618 in_section = darwin_sections[zobj_data_section]; 2619 size = 1; 2620 } 2621 else 2622 { 2623 fputs ("\t.data\n", fp); 2624 in_section = data_section; 2625 } 2626 2627 if (l2align) 2628 fprintf (fp, "\t.align\t%u\n", l2align); 2629 2630 assemble_name (fp, name); 2631 fprintf (fp, ":\n\t.space\t" HOST_WIDE_INT_PRINT_UNSIGNED"\n", size); 2632 } 2633 else 2634 { 2635 char secnam[64]; 2636 unsigned int flags ; 2637 /* When we are on a non-section anchor target, we can get zero-sized 2638 items here. However, all we need to do is to bump them to one byte 2639 and the section alignment will take care of the rest. */ 2640 snprintf (secnam, 64, "__DATA,__%spu_bss%u", ((size)?"":"zo_"), l2align); 2641 2642 /* We can't anchor in zerofill sections, because we can't switch 2643 to them and emit a label. */ 2644 flags = SECTION_BSS|SECTION_WRITE|SECTION_NO_ANCHOR; 2645 in_section = get_section (secnam, flags, NULL); 2646 fprintf (fp, "\t.zerofill %s,", secnam); 2647 assemble_name (fp, name); 2648 if (!size) 2649 size = 1; 2650 2651 if (l2align) 2652 fprintf (fp, "," HOST_WIDE_INT_PRINT_UNSIGNED",%u\n", size, l2align); 2653 else 2654 fprintf (fp, "," HOST_WIDE_INT_PRINT_UNSIGNED"\n", size); 2655 } 2656 (* targetm.encode_section_info) (decl, DECL_RTL (decl), false); 2657 } 2658 2659 /* Output a chunk of common, with alignment specified (where the target 2660 supports this). */ 2661 void 2662 darwin_asm_output_aligned_decl_common (FILE *fp, tree decl, const char *name, 2663 unsigned HOST_WIDE_INT size, 2664 unsigned int align) 2665 { 2666 unsigned int l2align; 2667 bool one, weak; 2668 tree meta; 2669 2670 /* No corresponding var. */ 2671 if (decl==NULL) 2672 { 2673 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS 2674 fprintf (fp, "# adcom: %s (%d,%d) decl=0x0\n", name, (int)size, (int)align); 2675 #endif 2676 darwin_emit_common (fp, name, size, align); 2677 return; 2678 } 2679 2680 one = DECL_ONE_ONLY (decl); 2681 weak = (DECL_P (decl) 2682 && DECL_WEAK (decl) 2683 && !lookup_attribute ("weak_import", 2684 DECL_ATTRIBUTES (decl))); 2685 2686 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS 2687 fprintf (fp, "# adcom: %s (%lld,%d) ro %d cst %d stat %d com %d pub %d" 2688 " weak %d one %d init %lx\n", 2689 name, (long long)size, (int)align, TREE_READONLY (decl), 2690 TREE_CONSTANT (decl), TREE_STATIC (decl), DECL_COMMON (decl), 2691 TREE_PUBLIC (decl), weak, one, (unsigned long)DECL_INITIAL (decl)); 2692 #endif 2693 2694 /* ObjC metadata can get put in BSS because varasm.c decides it's BSS 2695 before the target has a chance to comment. */ 2696 if ((meta = is_objc_metadata (decl))) 2697 { 2698 darwin_emit_objc_zeroed (fp, decl, name, size, DECL_ALIGN (decl), meta); 2699 return; 2700 } 2701 2702 /* We shouldn't be messing with this if the decl has a section name. */ 2703 gcc_assert (DECL_SECTION_NAME (decl) == NULL); 2704 2705 /* We would rather not have to check this here - but it seems that we might 2706 be passed a decl that should be in coalesced space. */ 2707 if (one || weak) 2708 { 2709 /* Weak or COMDAT objects are put in mergable sections. */ 2710 darwin_emit_weak_or_comdat (fp, decl, name, size, 2711 ld_uses_coal_sects, DECL_ALIGN (decl)); 2712 return; 2713 } 2714 2715 /* We should only get here for DECL_COMMON, with a zero init (and, in 2716 principle, only for public symbols too - although we deal with local 2717 ones below). */ 2718 2719 /* Check the initializer is OK. */ 2720 gcc_assert (DECL_COMMON (decl) 2721 && ((DECL_INITIAL (decl) == NULL) 2722 || (DECL_INITIAL (decl) == error_mark_node) 2723 || initializer_zerop (DECL_INITIAL (decl)))); 2724 2725 last_assemble_variable_decl = decl; 2726 2727 if (!size || !align) 2728 align = DECL_ALIGN (decl); 2729 2730 l2align = floor_log2 (align / BITS_PER_UNIT); 2731 /* Check we aren't asking for more aligment than the platform allows. */ 2732 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT); 2733 2734 if (TREE_PUBLIC (decl) != 0) 2735 darwin_emit_common (fp, name, size, align); 2736 else 2737 darwin_emit_local_bss (fp, decl, name, size, l2align); 2738 } 2739 2740 /* Output a chunk of BSS with alignment specfied. */ 2741 void 2742 darwin_asm_output_aligned_decl_local (FILE *fp, tree decl, const char *name, 2743 unsigned HOST_WIDE_INT size, 2744 unsigned int align) 2745 { 2746 unsigned long l2align; 2747 bool one, weak; 2748 tree meta; 2749 2750 one = DECL_ONE_ONLY (decl); 2751 weak = (DECL_P (decl) 2752 && DECL_WEAK (decl) 2753 && !lookup_attribute ("weak_import", 2754 DECL_ATTRIBUTES (decl))); 2755 2756 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS 2757 fprintf (fp, "# adloc: %s (%lld,%d) ro %d cst %d stat %d one %d pub %d" 2758 " weak %d init %lx\n", 2759 name, (long long)size, (int)align, TREE_READONLY (decl), 2760 TREE_CONSTANT (decl), TREE_STATIC (decl), one, TREE_PUBLIC (decl), 2761 weak , (unsigned long)DECL_INITIAL (decl)); 2762 #endif 2763 2764 /* ObjC metadata can get put in BSS because varasm.c decides it's BSS 2765 before the target has a chance to comment. */ 2766 if ((meta = is_objc_metadata (decl))) 2767 { 2768 darwin_emit_objc_zeroed (fp, decl, name, size, DECL_ALIGN (decl), meta); 2769 return; 2770 } 2771 2772 /* We shouldn't be messing with this if the decl has a section name. */ 2773 gcc_assert (DECL_SECTION_NAME (decl) == NULL); 2774 2775 /* We would rather not have to check this here - but it seems that we might 2776 be passed a decl that should be in coalesced space. */ 2777 if (one || weak) 2778 { 2779 /* Weak or COMDAT objects are put in mergable sections. */ 2780 darwin_emit_weak_or_comdat (fp, decl, name, size, 2781 ld_uses_coal_sects, DECL_ALIGN (decl)); 2782 return; 2783 } 2784 2785 /* .. and it should be suitable for placement in local mem. */ 2786 gcc_assert(!TREE_PUBLIC (decl) && !DECL_COMMON (decl)); 2787 /* .. and any initializer must be all-zero. */ 2788 gcc_assert ((DECL_INITIAL (decl) == NULL) 2789 || (DECL_INITIAL (decl) == error_mark_node) 2790 || initializer_zerop (DECL_INITIAL (decl))); 2791 2792 last_assemble_variable_decl = decl; 2793 2794 if (!size || !align) 2795 align = DECL_ALIGN (decl); 2796 2797 l2align = floor_log2 (align / BITS_PER_UNIT); 2798 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT); 2799 2800 darwin_emit_local_bss (fp, decl, name, size, l2align); 2801 } 2802 2803 /* Emit an assembler directive to set visibility for a symbol. The 2804 only supported visibilities are VISIBILITY_DEFAULT and 2805 VISIBILITY_HIDDEN; the latter corresponds to Darwin's "private 2806 extern". There is no MACH-O equivalent of ELF's 2807 VISIBILITY_INTERNAL or VISIBILITY_PROTECTED. */ 2808 2809 void 2810 darwin_assemble_visibility (tree decl, int vis) 2811 { 2812 if (vis == VISIBILITY_DEFAULT) 2813 ; 2814 else if (vis == VISIBILITY_HIDDEN || vis == VISIBILITY_INTERNAL) 2815 { 2816 fputs ("\t.private_extern ", asm_out_file); 2817 assemble_name (asm_out_file, 2818 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)))); 2819 fputs ("\n", asm_out_file); 2820 } 2821 else 2822 warning (OPT_Wattributes, "protected visibility attribute " 2823 "not supported in this configuration; ignored"); 2824 } 2825 2826 /* vec used by darwin_asm_dwarf_section. 2827 Maybe a hash tab would be better here - but the intention is that this is 2828 a very short list (fewer than 16 items) and each entry should (ideally, 2829 eventually) only be presented once. 2830 2831 A structure to hold a dwarf debug section used entry. */ 2832 2833 typedef struct GTY(()) dwarf_sect_used_entry { 2834 const char *name; 2835 unsigned count; 2836 } 2837 dwarf_sect_used_entry; 2838 2839 2840 /* A list of used __DWARF sections. */ 2841 static GTY (()) vec<dwarf_sect_used_entry, va_gc> *dwarf_sect_names_table; 2842 2843 /* This is called when we are asked to assemble a named section and the 2844 name begins with __DWARF,. We keep a list of the section names (without 2845 the __DWARF, prefix) and use this to emit our required start label on the 2846 first switch to each section. */ 2847 2848 static void 2849 darwin_asm_dwarf_section (const char *name, unsigned int flags, 2850 tree ARG_UNUSED (decl), bool is_for_lto) 2851 { 2852 unsigned i; 2853 int namelen, extra = 0; 2854 const char *sect, *lto_add = ""; 2855 char sname[64]; 2856 dwarf_sect_used_entry *ref; 2857 bool found = false; 2858 2859 gcc_checking_assert ((flags & (SECTION_DEBUG | SECTION_NAMED)) 2860 == (SECTION_DEBUG | SECTION_NAMED)); 2861 2862 /* We know that the name starts with __DWARF, or __GNU_DAWRF_LTO */ 2863 sect = strchr (name, ',') + 1; 2864 namelen = strchr (sect, ',') - sect; 2865 gcc_checking_assert (namelen); 2866 2867 /* The section switch is output as written... */ 2868 fprintf (asm_out_file, "\t.section %s\n", name); 2869 2870 /* ... but the string we keep to make section start labels needs 2871 adjustment for lto cases. */ 2872 if (is_for_lto) 2873 { 2874 lto_add = "_lto"; 2875 extra = 4; 2876 } 2877 2878 snprintf (sname, 64, "%.*s%.*s", namelen, sect, extra, lto_add); 2879 namelen += extra; 2880 2881 if (dwarf_sect_names_table == NULL) 2882 vec_alloc (dwarf_sect_names_table, 16); 2883 else 2884 for (i = 0; 2885 dwarf_sect_names_table->iterate (i, &ref); 2886 i++) 2887 { 2888 if (!ref) 2889 break; 2890 if (!strcmp (ref->name, sname)) 2891 { 2892 found = true; 2893 ref->count++; 2894 break; 2895 } 2896 } 2897 2898 if (!found) 2899 { 2900 dwarf_sect_used_entry e; 2901 fprintf (asm_out_file, "Lsection%.*s:\n", namelen, sname); 2902 e.count = 1; 2903 e.name = xstrdup (sname); 2904 vec_safe_push (dwarf_sect_names_table, e); 2905 } 2906 } 2907 2908 /* Output a difference of two labels that will be an assembly time 2909 constant if the two labels are local. (.long lab1-lab2 will be 2910 very different if lab1 is at the boundary between two sections; it 2911 will be relocated according to the second section, not the first, 2912 so one ends up with a difference between labels in different 2913 sections, which is bad in the dwarf2 eh context for instance.) */ 2914 2915 static int darwin_dwarf_label_counter; 2916 2917 void 2918 darwin_asm_output_dwarf_delta (FILE *file, int size, 2919 const char *lab1, const char *lab2, 2920 HOST_WIDE_INT offset) 2921 { 2922 int islocaldiff = (lab1[0] == '*' && lab1[1] == 'L' 2923 && lab2[0] == '*' && lab2[1] == 'L'); 2924 const char *directive = (size == 8 ? ".quad" : ".long"); 2925 2926 if (islocaldiff) 2927 fprintf (file, "\t.set L$set$%d,", darwin_dwarf_label_counter); 2928 else 2929 fprintf (file, "\t%s\t", directive); 2930 2931 assemble_name_raw (file, lab1); 2932 fprintf (file, "-"); 2933 assemble_name_raw (file, lab2); 2934 if (offset != 0) 2935 fprintf (file, "+" HOST_WIDE_INT_PRINT_DEC, offset); 2936 if (islocaldiff) 2937 fprintf (file, "\n\t%s L$set$%d", directive, darwin_dwarf_label_counter++); 2938 } 2939 2940 /* Output an offset in a DWARF section on Darwin. On Darwin, DWARF section 2941 offsets are not represented using relocs in .o files; either the 2942 section never leaves the .o file, or the linker or other tool is 2943 responsible for parsing the DWARF and updating the offsets. */ 2944 2945 void 2946 darwin_asm_output_dwarf_offset (FILE *file, int size, const char * lab, 2947 HOST_WIDE_INT offset, section *base) 2948 { 2949 char sname[64]; 2950 int namelen, extra = 0; 2951 bool is_for_lto; 2952 const char *lto_add = ""; 2953 2954 gcc_checking_assert (base->common.flags & SECTION_NAMED); 2955 is_for_lto = strncmp (base->named.name, "__GNU_DWARF_LTO,", 16) == 0; 2956 gcc_checking_assert (is_for_lto 2957 || strncmp (base->named.name, "__DWARF,", 8) == 0); 2958 const char *name = strchr (base->named.name, ',') + 1; 2959 gcc_checking_assert (name); 2960 2961 namelen = strchr (name, ',') - (name); 2962 if (is_for_lto) 2963 { 2964 lto_add = "_lto"; 2965 extra = 4; 2966 } 2967 snprintf (sname, 64, "*Lsection%.*s%.*s", namelen, name, extra, lto_add); 2968 darwin_asm_output_dwarf_delta (file, size, lab, sname, offset); 2969 } 2970 2971 /* Called from the within the TARGET_ASM_FILE_START for each target. */ 2972 2973 void 2974 darwin_file_start (void) 2975 { 2976 /* Nothing to do. */ 2977 } 2978 2979 /* Called for the TARGET_ASM_FILE_END hook. 2980 Emit the mach-o pic indirection data, the lto data and, finally a flag 2981 to tell the linker that it can break the file object into sections and 2982 move those around for efficiency. */ 2983 2984 void 2985 darwin_file_end (void) 2986 { 2987 if (!vec_safe_is_empty (ctors)) 2988 finalize_ctors (); 2989 if (!vec_safe_is_empty (dtors)) 2990 finalize_dtors (); 2991 2992 /* If we are expecting to output NeXT ObjC meta-data, (and we actually see 2993 some) then we output the fix-and-continue marker (Image Info). 2994 This applies to Objective C, Objective C++ and LTO with either language 2995 as part of the input. */ 2996 if (flag_next_runtime && objc_metadata_seen) 2997 { 2998 unsigned int flags = 0; 2999 if (flag_objc_abi >= 2) 3000 { 3001 flags = 16; 3002 switch_to_section (darwin_sections[objc2_image_info_section]); 3003 } 3004 else 3005 switch_to_section (darwin_sections[objc_image_info_section]); 3006 3007 ASM_OUTPUT_ALIGN (asm_out_file, 2); 3008 fputs ("L_OBJC_ImageInfo:\n", asm_out_file); 3009 3010 flags |= (flag_replace_objc_classes && classes_seen) ? 1 : 0; 3011 flags |= flag_objc_gc ? 2 : 0; 3012 3013 fprintf (asm_out_file, "\t.long\t0\n\t.long\t%u\n", flags); 3014 } 3015 3016 machopic_finish (asm_out_file); 3017 if (lang_GNU_CXX ()) 3018 { 3019 switch_to_section (darwin_sections[constructor_section]); 3020 switch_to_section (darwin_sections[destructor_section]); 3021 ASM_OUTPUT_ALIGN (asm_out_file, 1); 3022 } 3023 3024 /* If there was LTO assembler output, append it to asm_out_file. */ 3025 if (lto_asm_out_name) 3026 { 3027 int n; 3028 char *buf, *lto_asm_txt; 3029 3030 /* Shouldn't be here if we failed to switch back. */ 3031 gcc_assert (! saved_asm_out_file); 3032 3033 lto_asm_out_file = fopen (lto_asm_out_name, "r"); 3034 if (lto_asm_out_file == NULL) 3035 fatal_error (input_location, 3036 "failed to open temporary file %s with LTO output", 3037 lto_asm_out_name); 3038 fseek (lto_asm_out_file, 0, SEEK_END); 3039 n = ftell (lto_asm_out_file); 3040 if (n > 0) 3041 { 3042 fseek (lto_asm_out_file, 0, SEEK_SET); 3043 lto_asm_txt = buf = (char *) xmalloc (n + 1); 3044 while (fgets (lto_asm_txt, n, lto_asm_out_file)) 3045 fputs (lto_asm_txt, asm_out_file); 3046 /* Put a termination label. */ 3047 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n", 3048 LTO_SEGMENT_NAME, LTO_SECTS_SECTION); 3049 fprintf (asm_out_file, "L_GNU_LTO%d:\t;# end of lto\n", 3050 lto_section_num); 3051 /* Make sure our termination label stays in this section. */ 3052 fputs ("\t.space\t1\n", asm_out_file); 3053 } 3054 3055 /* Remove the temporary file. */ 3056 fclose (lto_asm_out_file); 3057 unlink_if_ordinary (lto_asm_out_name); 3058 free (lto_asm_out_name); 3059 } 3060 3061 /* Output the names and indices. */ 3062 if (lto_section_names && lto_section_names->length ()) 3063 { 3064 int count; 3065 darwin_lto_section_e *ref; 3066 /* For now, we'll make the offsets 4 bytes and unaligned - we'll fix 3067 the latter up ourselves. */ 3068 const char *op = integer_asm_op (4,0); 3069 3070 /* Emit the names. */ 3071 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n", 3072 LTO_SEGMENT_NAME, LTO_NAMES_SECTION); 3073 FOR_EACH_VEC_ELT (*lto_section_names, count, ref) 3074 { 3075 fprintf (asm_out_file, "L_GNU_LTO_NAME%d:\n", count); 3076 /* We have to jump through hoops to get the values of the intra-section 3077 offsets... */ 3078 fprintf (asm_out_file, 3079 "\t.set L$gnu$lto$noff%d,L_GNU_LTO_NAME%d-L_GNU_LTO_NAME0\n", 3080 count, count); 3081 fprintf (asm_out_file, 3082 "\t.set L$gnu$lto$nsiz%d,L_GNU_LTO_NAME%d-L_GNU_LTO_NAME%d\n", 3083 count, count+1, count); 3084 fprintf (asm_out_file, "\t.asciz\t\"%s\"\n", ref->sectname); 3085 } 3086 fprintf (asm_out_file, "L_GNU_LTO_NAME%d:\t;# end\n", lto_section_num); 3087 /* make sure our termination label stays in this section. */ 3088 fputs ("\t.space\t1\n", asm_out_file); 3089 3090 /* Emit the Index. */ 3091 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n", 3092 LTO_SEGMENT_NAME, LTO_INDEX_SECTION); 3093 fputs ("\t.align\t2\n", asm_out_file); 3094 fputs ("# Section offset, Section length, Name offset, Name length\n", 3095 asm_out_file); 3096 FOR_EACH_VEC_ELT (*lto_section_names, count, ref) 3097 { 3098 fprintf (asm_out_file, "%s L$gnu$lto$offs%d\t;# %s\n", 3099 op, count, ref->sectname); 3100 fprintf (asm_out_file, "%s L$gnu$lto$size%d\n", op, count); 3101 fprintf (asm_out_file, "%s L$gnu$lto$noff%d\n", op, count); 3102 fprintf (asm_out_file, "%s L$gnu$lto$nsiz%d\n", op, count); 3103 } 3104 } 3105 3106 /* If we have section anchors, then we must prevent the linker from 3107 re-arranging data. */ 3108 if (!DARWIN_SECTION_ANCHORS || !flag_section_anchors) 3109 fprintf (asm_out_file, "\t.subsections_via_symbols\n"); 3110 } 3111 3112 /* TODO: Add a language hook for identifying if a decl is a vtable. */ 3113 #define DARWIN_VTABLE_P(DECL) 0 3114 3115 /* Cross-module name binding. Darwin does not support overriding 3116 functions at dynamic-link time, except for vtables in kexts. */ 3117 3118 bool 3119 darwin_binds_local_p (const_tree decl) 3120 { 3121 return default_binds_local_p_1 (decl, 3122 TARGET_KEXTABI && DARWIN_VTABLE_P (decl)); 3123 } 3124 3125 /* The Darwin's implementation of TARGET_ASM_OUTPUT_ANCHOR. Define the 3126 anchor relative to ".", the current section position. We cannot use 3127 the default one because ASM_OUTPUT_DEF is wrong for Darwin. */ 3128 void 3129 darwin_asm_output_anchor (rtx symbol) 3130 { 3131 fprintf (asm_out_file, "\t.set\t"); 3132 assemble_name (asm_out_file, XSTR (symbol, 0)); 3133 fprintf (asm_out_file, ", . + " HOST_WIDE_INT_PRINT_DEC "\n", 3134 SYMBOL_REF_BLOCK_OFFSET (symbol)); 3135 } 3136 3137 /* Disable section anchoring on any section containing a zero-sized 3138 object. */ 3139 bool 3140 darwin_use_anchors_for_symbol_p (const_rtx symbol) 3141 { 3142 if (DARWIN_SECTION_ANCHORS && flag_section_anchors) 3143 { 3144 section *sect; 3145 /* If the section contains a zero-sized object it's ineligible. */ 3146 sect = SYMBOL_REF_BLOCK (symbol)->sect; 3147 /* This should have the effect of disabling anchors for vars that follow 3148 any zero-sized one, in a given section. */ 3149 if (sect->common.flags & SECTION_NO_ANCHOR) 3150 return false; 3151 3152 /* Also check the normal reasons for suppressing. */ 3153 return default_use_anchors_for_symbol_p (symbol); 3154 } 3155 else 3156 return false; 3157 } 3158 3159 /* Set the darwin specific attributes on TYPE. */ 3160 void 3161 darwin_set_default_type_attributes (tree type) 3162 { 3163 if (darwin_ms_struct 3164 && TREE_CODE (type) == RECORD_TYPE) 3165 TYPE_ATTRIBUTES (type) = tree_cons (get_identifier ("ms_struct"), 3166 NULL_TREE, 3167 TYPE_ATTRIBUTES (type)); 3168 } 3169 3170 /* True, iff we're generating code for loadable kernel extensions. */ 3171 3172 bool 3173 darwin_kextabi_p (void) { 3174 return flag_apple_kext; 3175 } 3176 3177 void 3178 darwin_override_options (void) 3179 { 3180 /* Keep track of which (major) version we're generating code for. */ 3181 if (darwin_macosx_version_min) 3182 { 3183 if (strverscmp (darwin_macosx_version_min, "10.6") >= 0) 3184 generating_for_darwin_version = 10; 3185 else if (strverscmp (darwin_macosx_version_min, "10.5") >= 0) 3186 generating_for_darwin_version = 9; 3187 3188 /* Earlier versions are not specifically accounted, until required. */ 3189 } 3190 3191 /* Older Darwin ld could not coalesce weak entities without them being 3192 placed in special sections. */ 3193 if (darwin_target_linker 3194 && (strverscmp (darwin_target_linker, MIN_LD64_NO_COAL_SECTS) < 0)) 3195 ld_uses_coal_sects = true; 3196 3197 /* In principle, this should be c-family only. However, we really need to 3198 set sensible defaults for LTO as well, since the section selection stuff 3199 should check for correctness re. the ABI. TODO: check and provide the 3200 flags (runtime & ABI) from the lto wrapper). */ 3201 3202 /* Unless set, force ABI=2 for NeXT and m64, 0 otherwise. */ 3203 if (!global_options_set.x_flag_objc_abi) 3204 global_options.x_flag_objc_abi 3205 = (!flag_next_runtime) 3206 ? 0 3207 : (TARGET_64BIT ? 2 3208 : (generating_for_darwin_version >= 9) ? 1 3209 : 0); 3210 3211 if (global_options_set.x_flag_objc_abi && flag_next_runtime) 3212 { 3213 if (TARGET_64BIT && global_options.x_flag_objc_abi != 2) 3214 /* The Objective-C family ABI 2 is the only valid version NeXT/m64. */ 3215 error_at (UNKNOWN_LOCATION, 3216 "%<-fobjc-abi-version%> 2 must be used for 64 bit targets" 3217 " with %<-fnext-runtime%>"); 3218 else if (!TARGET_64BIT && global_options.x_flag_objc_abi >= 2) 3219 /* ABI versions 0 and 1 are the only valid versions NeXT/m32. */ 3220 error_at (UNKNOWN_LOCATION, 3221 "%<-fobjc-abi-version%> %d is not supported for 32 bit" 3222 " targets with %<-fnext-runtime%>", 3223 global_options.x_flag_objc_abi); 3224 } 3225 3226 /* Don't emit DWARF3/4 unless specifically selected. This is a 3227 workaround for tool bugs. */ 3228 if (!global_options_set.x_dwarf_strict) 3229 dwarf_strict = 1; 3230 if (!global_options_set.x_dwarf_version) 3231 dwarf_version = 2; 3232 3233 if (global_options_set.x_dwarf_split_debug_info) 3234 { 3235 inform (input_location, 3236 "%<-gsplit-dwarf%> is not supported on this platform, ignored"); 3237 dwarf_split_debug_info = 0; 3238 global_options_set.x_dwarf_split_debug_info = 0; 3239 } 3240 3241 /* Do not allow unwind tables to be generated by default for m32. 3242 fnon-call-exceptions will override this, regardless of what we do. */ 3243 if (generating_for_darwin_version < 10 3244 && !global_options_set.x_flag_asynchronous_unwind_tables 3245 && !TARGET_64BIT) 3246 global_options.x_flag_asynchronous_unwind_tables = 0; 3247 3248 /* Disable -freorder-blocks-and-partition when unwind tables are being 3249 emitted for Darwin < 9 (OSX 10.5). 3250 The strategy is, "Unless the User has specifically set/unset an unwind 3251 flag we will switch off -freorder-blocks-and-partition when unwind tables 3252 will be generated". If the User specifically sets flags... we assume 3253 (s)he knows why... */ 3254 if (generating_for_darwin_version < 9 3255 && global_options_set.x_flag_reorder_blocks_and_partition 3256 && ((global_options.x_flag_exceptions /* User, c++, java */ 3257 && !global_options_set.x_flag_exceptions) /* User specified... */ 3258 || (global_options.x_flag_unwind_tables 3259 && !global_options_set.x_flag_unwind_tables) 3260 || (global_options.x_flag_non_call_exceptions 3261 && !global_options_set.x_flag_non_call_exceptions) 3262 || (global_options.x_flag_asynchronous_unwind_tables 3263 && !global_options_set.x_flag_asynchronous_unwind_tables))) 3264 { 3265 inform (input_location, 3266 "%<-freorder-blocks-and-partition%> does not work with " 3267 "exceptions on this architecture"); 3268 flag_reorder_blocks_and_partition = 0; 3269 flag_reorder_blocks = 1; 3270 } 3271 3272 /* FIXME: flag_objc_sjlj_exceptions is no longer needed since there is only 3273 one valid choice of exception scheme for each runtime. */ 3274 if (!global_options_set.x_flag_objc_sjlj_exceptions) 3275 global_options.x_flag_objc_sjlj_exceptions = 3276 flag_next_runtime && !TARGET_64BIT; 3277 3278 /* FIXME: and this could be eliminated then too. */ 3279 if (!global_options_set.x_flag_exceptions 3280 && flag_objc_exceptions 3281 && TARGET_64BIT) 3282 flag_exceptions = 1; 3283 3284 if (flag_mkernel || flag_apple_kext) 3285 { 3286 /* -mkernel implies -fapple-kext for C++ */ 3287 if (lang_GNU_CXX ()) 3288 flag_apple_kext = 1; 3289 3290 flag_no_common = 1; 3291 3292 /* No EH in kexts. */ 3293 flag_exceptions = 0; 3294 /* No -fnon-call-exceptions data in kexts. */ 3295 flag_non_call_exceptions = 0; 3296 /* so no tables either.. */ 3297 flag_unwind_tables = 0; 3298 flag_asynchronous_unwind_tables = 0; 3299 } 3300 3301 if (flag_var_tracking_uninit == 0 3302 && generating_for_darwin_version >= 9 3303 && (flag_gtoggle ? (debug_info_level == DINFO_LEVEL_NONE) 3304 : (debug_info_level >= DINFO_LEVEL_NORMAL)) 3305 && write_symbols == DWARF2_DEBUG) 3306 flag_var_tracking_uninit = flag_var_tracking; 3307 3308 /* Final check on PCI options; for Darwin these are not dependent on the PIE 3309 ones, although PIE does require PIC to support it. */ 3310 if (MACHO_DYNAMIC_NO_PIC_P) 3311 { 3312 if (flag_pic) 3313 warning_at (UNKNOWN_LOCATION, 0, 3314 "%<-mdynamic-no-pic%> overrides %<-fpic%>, %<-fPIC%>," 3315 " %<-fpie%> or %<-fPIE%>"); 3316 flag_pic = 0; 3317 } 3318 else if (flag_pic == 1 3319 || (flag_pic == 0 && !(flag_mkernel || flag_apple_kext))) 3320 { 3321 /* Darwin's -fpic is -fPIC. 3322 We only support "static" code in the kernel and kernel exts. */ 3323 flag_pic = 2; 3324 } 3325 3326 /* Linkers >= ld64-62.1 (at least) are capable of making the necessary PIC 3327 indirections and we no longer need to emit pic symbol stubs. 3328 However, if we are generating code for earlier ones (or for use in the 3329 kernel) the stubs might still be required, and this will be set true. 3330 If the user sets it on or off - then that takes precedence. 3331 3332 Linkers that don't need stubs, don't need the EH symbol markers either. 3333 */ 3334 3335 if (!global_options_set.x_darwin_symbol_stubs) 3336 { 3337 if (darwin_target_linker) 3338 { 3339 if (strverscmp (darwin_target_linker, MIN_LD64_OMIT_STUBS) < 0) 3340 { 3341 darwin_symbol_stubs = true; 3342 ld_needs_eh_markers = true; 3343 } 3344 } 3345 else if (generating_for_darwin_version < 9) 3346 { 3347 /* If we don't know the linker version and we're targeting an old 3348 system, we know no better than to assume the use of an earlier 3349 linker. */ 3350 darwin_symbol_stubs = true; 3351 ld_needs_eh_markers = true; 3352 } 3353 } 3354 else if (DARWIN_X86 && darwin_symbol_stubs && TARGET_64BIT) 3355 { 3356 inform (input_location, 3357 "%<-msymbol-stubs%> is not required for 64b code (ignored)"); 3358 darwin_symbol_stubs = false; 3359 } 3360 3361 if (generating_for_darwin_version >= 9) 3362 /* Later systems can support aligned common. */ 3363 emit_aligned_common = true; 3364 3365 /* The c_dialect...() macros are not available to us here. */ 3366 darwin_running_cxx = (strstr (lang_hooks.name, "C++") != 0); 3367 } 3368 3369 #if DARWIN_PPC 3370 /* Add $LDBL128 suffix to long double builtins for ppc darwin. */ 3371 3372 static void 3373 darwin_patch_builtin (enum built_in_function fncode) 3374 { 3375 tree fn = builtin_decl_explicit (fncode); 3376 tree sym; 3377 char *newname; 3378 3379 if (!fn) 3380 return; 3381 3382 sym = DECL_ASSEMBLER_NAME (fn); 3383 newname = ACONCAT (("_", IDENTIFIER_POINTER (sym), "$LDBL128", NULL)); 3384 3385 set_user_assembler_name (fn, newname); 3386 3387 fn = builtin_decl_implicit (fncode); 3388 if (fn) 3389 set_user_assembler_name (fn, newname); 3390 } 3391 3392 void 3393 darwin_patch_builtins (void) 3394 { 3395 if (LONG_DOUBLE_TYPE_SIZE != 128) 3396 return; 3397 3398 #define PATCH_BUILTIN(fncode) darwin_patch_builtin (fncode); 3399 #define PATCH_BUILTIN_NO64(fncode) \ 3400 if (!TARGET_64BIT) \ 3401 darwin_patch_builtin (fncode); 3402 #define PATCH_BUILTIN_VARIADIC(fncode) \ 3403 if (!TARGET_64BIT \ 3404 && (strverscmp (darwin_macosx_version_min, "10.3.9") >= 0)) \ 3405 darwin_patch_builtin (fncode); 3406 #include "darwin-ppc-ldouble-patch.def" 3407 #undef PATCH_BUILTIN 3408 #undef PATCH_BUILTIN_NO64 3409 #undef PATCH_BUILTIN_VARIADIC 3410 } 3411 #endif 3412 3413 /* CFStrings implementation. */ 3414 static GTY(()) tree cfstring_class_reference = NULL_TREE; 3415 static GTY(()) tree cfstring_type_node = NULL_TREE; 3416 static GTY(()) tree ccfstring_type_node = NULL_TREE; 3417 static GTY(()) tree pccfstring_type_node = NULL_TREE; 3418 static GTY(()) tree pcint_type_node = NULL_TREE; 3419 static GTY(()) tree pcchar_type_node = NULL_TREE; 3420 3421 static enum built_in_function darwin_builtin_cfstring; 3422 3423 /* Store all constructed constant CFStrings in a hash table so that 3424 they get uniqued properly. */ 3425 3426 typedef struct GTY ((for_user)) cfstring_descriptor { 3427 /* The string literal. */ 3428 tree literal; 3429 /* The resulting constant CFString. */ 3430 tree constructor; 3431 } cfstring_descriptor; 3432 3433 struct cfstring_hasher : ggc_ptr_hash<cfstring_descriptor> 3434 { 3435 static hashval_t hash (cfstring_descriptor *); 3436 static bool equal (cfstring_descriptor *, cfstring_descriptor *); 3437 }; 3438 3439 static GTY (()) hash_table<cfstring_hasher> *cfstring_htab; 3440 3441 static tree 3442 add_builtin_field_decl (tree type, const char *name, tree **chain) 3443 { 3444 tree field = build_decl (BUILTINS_LOCATION, FIELD_DECL, 3445 get_identifier (name), type); 3446 3447 if (*chain != NULL) 3448 **chain = field; 3449 *chain = &DECL_CHAIN (field); 3450 3451 return field; 3452 } 3453 3454 tree 3455 darwin_init_cfstring_builtins (unsigned builtin_cfstring) 3456 { 3457 tree cfsfun, fields, pccfstring_ftype_pcchar; 3458 tree *chain = NULL; 3459 3460 darwin_builtin_cfstring = 3461 (enum built_in_function) builtin_cfstring; 3462 3463 /* struct __builtin_CFString { 3464 const int *isa; (will point at 3465 int flags; __CFConstantStringClassReference) 3466 const char *str; 3467 long length; 3468 }; */ 3469 3470 pcint_type_node = build_pointer_type 3471 (build_qualified_type (integer_type_node, TYPE_QUAL_CONST)); 3472 3473 pcchar_type_node = build_pointer_type 3474 (build_qualified_type (char_type_node, TYPE_QUAL_CONST)); 3475 3476 cfstring_type_node = (*lang_hooks.types.make_type) (RECORD_TYPE); 3477 3478 /* Have to build backwards for finish struct. */ 3479 fields = add_builtin_field_decl (long_integer_type_node, "length", &chain); 3480 add_builtin_field_decl (pcchar_type_node, "str", &chain); 3481 add_builtin_field_decl (integer_type_node, "flags", &chain); 3482 add_builtin_field_decl (pcint_type_node, "isa", &chain); 3483 finish_builtin_struct (cfstring_type_node, "__builtin_CFString", 3484 fields, NULL_TREE); 3485 3486 /* const struct __builtin_CFstring * 3487 __builtin___CFStringMakeConstantString (const char *); */ 3488 3489 ccfstring_type_node = build_qualified_type 3490 (cfstring_type_node, TYPE_QUAL_CONST); 3491 pccfstring_type_node = build_pointer_type (ccfstring_type_node); 3492 pccfstring_ftype_pcchar = build_function_type_list 3493 (pccfstring_type_node, pcchar_type_node, NULL_TREE); 3494 3495 cfsfun = build_decl (BUILTINS_LOCATION, FUNCTION_DECL, 3496 get_identifier ("__builtin___CFStringMakeConstantString"), 3497 pccfstring_ftype_pcchar); 3498 3499 TREE_PUBLIC (cfsfun) = 1; 3500 DECL_EXTERNAL (cfsfun) = 1; 3501 DECL_ARTIFICIAL (cfsfun) = 1; 3502 /* Make a lang-specific section - dup_lang_specific_decl makes a new node 3503 in place of the existing, which may be NULL. */ 3504 DECL_LANG_SPECIFIC (cfsfun) = NULL; 3505 (*lang_hooks.dup_lang_specific_decl) (cfsfun); 3506 DECL_BUILT_IN_CLASS (cfsfun) = BUILT_IN_MD; 3507 DECL_FUNCTION_CODE (cfsfun) = darwin_builtin_cfstring; 3508 lang_hooks.builtin_function (cfsfun); 3509 3510 /* extern int __CFConstantStringClassReference[]; */ 3511 cfstring_class_reference = build_decl (BUILTINS_LOCATION, VAR_DECL, 3512 get_identifier ("__CFConstantStringClassReference"), 3513 build_array_type (integer_type_node, NULL_TREE)); 3514 3515 TREE_PUBLIC (cfstring_class_reference) = 1; 3516 DECL_ARTIFICIAL (cfstring_class_reference) = 1; 3517 (*lang_hooks.decls.pushdecl) (cfstring_class_reference); 3518 DECL_EXTERNAL (cfstring_class_reference) = 1; 3519 rest_of_decl_compilation (cfstring_class_reference, 0, 0); 3520 3521 /* Initialize the hash table used to hold the constant CFString objects. */ 3522 cfstring_htab = hash_table<cfstring_hasher>::create_ggc (31); 3523 3524 return cfstring_type_node; 3525 } 3526 3527 tree 3528 darwin_fold_builtin (tree fndecl, int n_args, tree *argp, 3529 bool ARG_UNUSED (ignore)) 3530 { 3531 unsigned int fcode = DECL_FUNCTION_CODE (fndecl); 3532 3533 if (fcode == darwin_builtin_cfstring) 3534 { 3535 if (!darwin_constant_cfstrings) 3536 { 3537 error ("built-in function %qD requires the" 3538 " %<-mconstant-cfstrings%> flag", fndecl); 3539 return error_mark_node; 3540 } 3541 3542 if (n_args != 1) 3543 { 3544 error ("built-in function %qD takes one argument only", fndecl); 3545 return error_mark_node; 3546 } 3547 3548 return darwin_build_constant_cfstring (*argp); 3549 } 3550 3551 return NULL_TREE; 3552 } 3553 3554 void 3555 darwin_rename_builtins (void) 3556 { 3557 /* The system ___divdc3 routine in libSystem on darwin10 is not 3558 accurate to 1ulp, ours is, so we avoid ever using the system name 3559 for this routine and instead install a non-conflicting name that 3560 is accurate. 3561 3562 When -ffast-math or -funsafe-math-optimizations is given, we can 3563 use the faster version. */ 3564 if (!flag_unsafe_math_optimizations) 3565 { 3566 enum built_in_function dcode 3567 = (enum built_in_function)(BUILT_IN_COMPLEX_DIV_MIN 3568 + DCmode - MIN_MODE_COMPLEX_FLOAT); 3569 tree fn = builtin_decl_explicit (dcode); 3570 /* Fortran and c call TARGET_INIT_BUILTINS and 3571 TARGET_INIT_LIBFUNCS at different times, so we have to put a 3572 call into each to ensure that at least one of them is called 3573 after build_common_builtin_nodes. A better fix is to add a 3574 new hook to run after build_common_builtin_nodes runs. */ 3575 if (fn) 3576 set_user_assembler_name (fn, "___ieee_divdc3"); 3577 fn = builtin_decl_implicit (dcode); 3578 if (fn) 3579 set_user_assembler_name (fn, "___ieee_divdc3"); 3580 } 3581 } 3582 3583 bool 3584 darwin_libc_has_function (enum function_class fn_class) 3585 { 3586 if (fn_class == function_sincos) 3587 return false; 3588 if (fn_class == function_c99_math_complex 3589 || fn_class == function_c99_misc) 3590 return (TARGET_64BIT 3591 || strverscmp (darwin_macosx_version_min, "10.3") >= 0); 3592 3593 return true; 3594 } 3595 3596 hashval_t 3597 cfstring_hasher::hash (cfstring_descriptor *ptr) 3598 { 3599 tree str = ptr->literal; 3600 const unsigned char *p = (const unsigned char *) TREE_STRING_POINTER (str); 3601 int i, len = TREE_STRING_LENGTH (str); 3602 hashval_t h = len; 3603 3604 for (i = 0; i < len; i++) 3605 h = ((h * 613) + p[i]); 3606 3607 return h; 3608 } 3609 3610 bool 3611 cfstring_hasher::equal (cfstring_descriptor *ptr1, cfstring_descriptor *ptr2) 3612 { 3613 tree str1 = ptr1->literal; 3614 tree str2 = ptr2->literal; 3615 int len1 = TREE_STRING_LENGTH (str1); 3616 3617 return (len1 == TREE_STRING_LENGTH (str2) 3618 && !memcmp (TREE_STRING_POINTER (str1), TREE_STRING_POINTER (str2), 3619 len1)); 3620 } 3621 3622 tree 3623 darwin_build_constant_cfstring (tree str) 3624 { 3625 struct cfstring_descriptor *desc, key; 3626 tree addr; 3627 3628 if (!str) 3629 { 3630 error ("CFString literal is missing"); 3631 return error_mark_node; 3632 } 3633 3634 STRIP_NOPS (str); 3635 3636 if (TREE_CODE (str) == ADDR_EXPR) 3637 str = TREE_OPERAND (str, 0); 3638 3639 if (TREE_CODE (str) != STRING_CST) 3640 { 3641 error ("CFString literal expression is not a string constant"); 3642 return error_mark_node; 3643 } 3644 3645 /* Perhaps we already constructed a constant CFString just like this one? */ 3646 key.literal = str; 3647 cfstring_descriptor **loc = cfstring_htab->find_slot (&key, INSERT); 3648 desc = *loc; 3649 3650 if (!desc) 3651 { 3652 tree var, constructor, field; 3653 vec<constructor_elt, va_gc> *v = NULL; 3654 int length = TREE_STRING_LENGTH (str) - 1; 3655 3656 if (darwin_warn_nonportable_cfstrings) 3657 { 3658 const char *s = TREE_STRING_POINTER (str); 3659 int l = 0; 3660 3661 for (l = 0; l < length; l++) 3662 if (!s[l] || !isascii (s[l])) 3663 { 3664 warning (darwin_warn_nonportable_cfstrings, 3665 s[l] ? G_("non-ASCII character in CFString literal") 3666 : G_("embedded NUL in CFString literal")); 3667 break; 3668 } 3669 } 3670 3671 *loc = desc = ggc_cleared_alloc<cfstring_descriptor> (); 3672 desc->literal = str; 3673 3674 /* isa *. */ 3675 field = TYPE_FIELDS (ccfstring_type_node); 3676 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE, 3677 build1 (ADDR_EXPR, TREE_TYPE (field), 3678 cfstring_class_reference)); 3679 /* flags */ 3680 field = DECL_CHAIN (field); 3681 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE, 3682 build_int_cst (TREE_TYPE (field), 0x000007c8)); 3683 /* string *. */ 3684 field = DECL_CHAIN (field); 3685 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE, 3686 build1 (ADDR_EXPR, TREE_TYPE (field), str)); 3687 /* length */ 3688 field = DECL_CHAIN (field); 3689 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE, 3690 build_int_cst (TREE_TYPE (field), length)); 3691 3692 constructor = build_constructor (ccfstring_type_node, v); 3693 TREE_READONLY (constructor) = 1; 3694 TREE_CONSTANT (constructor) = 1; 3695 TREE_STATIC (constructor) = 1; 3696 3697 /* Fromage: The C++ flavor of 'build_unary_op' expects constructor nodes 3698 to have the TREE_HAS_CONSTRUCTOR (...) bit set. However, this file is 3699 being built without any knowledge of C++ tree accessors; hence, we shall 3700 use the generic accessor that TREE_HAS_CONSTRUCTOR actually maps to! */ 3701 if (darwin_running_cxx) 3702 TREE_LANG_FLAG_4 (constructor) = 1; /* TREE_HAS_CONSTRUCTOR */ 3703 3704 /* Create an anonymous global variable for this CFString. */ 3705 var = build_decl (input_location, CONST_DECL, 3706 NULL, TREE_TYPE (constructor)); 3707 DECL_ARTIFICIAL (var) = 1; 3708 TREE_STATIC (var) = 1; 3709 DECL_INITIAL (var) = constructor; 3710 /* FIXME: This should use a translation_unit_decl to indicate file scope. */ 3711 DECL_CONTEXT (var) = NULL_TREE; 3712 desc->constructor = var; 3713 } 3714 3715 addr = build1 (ADDR_EXPR, pccfstring_type_node, desc->constructor); 3716 TREE_CONSTANT (addr) = 1; 3717 3718 return addr; 3719 } 3720 3721 bool 3722 darwin_cfstring_p (tree str) 3723 { 3724 struct cfstring_descriptor key; 3725 3726 if (!str) 3727 return false; 3728 3729 STRIP_NOPS (str); 3730 3731 if (TREE_CODE (str) == ADDR_EXPR) 3732 str = TREE_OPERAND (str, 0); 3733 3734 if (TREE_CODE (str) != STRING_CST) 3735 return false; 3736 3737 key.literal = str; 3738 cfstring_descriptor **loc = cfstring_htab->find_slot (&key, NO_INSERT); 3739 3740 if (loc) 3741 return true; 3742 3743 return false; 3744 } 3745 3746 void 3747 darwin_enter_string_into_cfstring_table (tree str) 3748 { 3749 struct cfstring_descriptor key; 3750 3751 key.literal = str; 3752 cfstring_descriptor **loc = cfstring_htab->find_slot (&key, INSERT); 3753 3754 if (!*loc) 3755 { 3756 *loc = ggc_cleared_alloc<cfstring_descriptor> (); 3757 ((struct cfstring_descriptor *)*loc)->literal = str; 3758 } 3759 } 3760 3761 /* Choose named function section based on its frequency. */ 3762 3763 section * 3764 darwin_function_section (tree decl, enum node_frequency freq, 3765 bool startup, bool exit) 3766 { 3767 /* Decide if we need to put this in a coalescable section. */ 3768 bool weak = (decl 3769 && DECL_WEAK (decl) 3770 && (!DECL_ATTRIBUTES (decl) 3771 || !lookup_attribute ("weak_import", 3772 DECL_ATTRIBUTES (decl)))); 3773 3774 bool use_coal = weak && ld_uses_coal_sects; 3775 /* If there is a specified section name, we should not be trying to 3776 override. */ 3777 if (decl && DECL_SECTION_NAME (decl) != NULL) 3778 return get_named_section (decl, NULL, 0); 3779 3780 /* We always put unlikely executed stuff in the cold section. */ 3781 if (freq == NODE_FREQUENCY_UNLIKELY_EXECUTED) 3782 return (use_coal) ? darwin_sections[text_cold_coal_section] 3783 : darwin_sections[text_cold_section]; 3784 3785 /* If we have LTO *and* feedback information, then let LTO handle 3786 the function ordering, it makes a better job (for normal, hot, 3787 startup and exit - hence the bailout for cold above). */ 3788 if (in_lto_p && flag_profile_values) 3789 goto default_function_sections; 3790 3791 /* Non-cold startup code should go to startup subsection. */ 3792 if (startup) 3793 return (use_coal) ? darwin_sections[text_startup_coal_section] 3794 : darwin_sections[text_startup_section]; 3795 3796 /* Similarly for exit. */ 3797 if (exit) 3798 return (use_coal) ? darwin_sections[text_exit_coal_section] 3799 : darwin_sections[text_exit_section]; 3800 3801 /* Place hot code. */ 3802 if (freq == NODE_FREQUENCY_HOT) 3803 return (use_coal) ? darwin_sections[text_hot_coal_section] 3804 : darwin_sections[text_hot_section]; 3805 3806 /* Otherwise, default to the 'normal' non-reordered sections. */ 3807 default_function_sections: 3808 return (use_coal) ? darwin_sections[text_coal_section] 3809 : text_section; 3810 } 3811 3812 #include "gt-darwin.h" 3813