1 /* Mach-O object file format 2 Copyright (C) 2009-2024 Free Software Foundation, Inc. 3 4 This file is part of GAS, the GNU Assembler. 5 6 GAS is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as 8 published by the Free Software Foundation; either version 3, 9 or (at your option) any later version. 10 11 GAS is distributed in the hope that it will be useful, but 12 WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 14 the GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with GAS; see the file COPYING. If not, write to the Free 18 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 19 02110-1301, USA. */ 20 21 /* Here we handle the mach-o directives that are common to all architectures. 22 23 Most significant are mach-o named sections and a variety of symbol type 24 decorations. */ 25 26 /* Mach-O supports multiple, named segments each of which may contain 27 multiple named sections. Thus the concept of subsectioning is 28 handled by (say) having a __TEXT segment with appropriate flags from 29 which subsections are generated like __text, __const etc. 30 31 The well-known as short-hand section switch directives like .text, .data 32 etc. are mapped onto predefined segment/section pairs using facilities 33 supplied by the mach-o port of bfd. 34 35 A number of additional mach-o short-hand section switch directives are 36 also defined. */ 37 38 #define OBJ_HEADER "obj-macho.h" 39 40 #include "as.h" 41 #include "subsegs.h" 42 #include "symbols.h" 43 #include "write.h" 44 #include "mach-o.h" 45 #include "mach-o/loader.h" 46 #include "obj-macho.h" 47 48 #include <string.h> 49 50 /* Forward decls. */ 51 static segT obj_mach_o_segT_from_bfd_name (const char *, int); 52 53 /* TODO: Implement "-dynamic"/"-static" command line options. */ 54 55 static int obj_mach_o_is_static; 56 57 /* TODO: Implement the "-n" command line option to suppress the initial 58 switch to the text segment. */ 59 60 static int obj_mach_o_start_with_text_section = 1; 61 62 /* Allow for special re-ordering on output. */ 63 64 static int obj_mach_o_seen_objc_section; 65 66 /* Start-up: At present, just create the sections we want. */ 67 void 68 mach_o_begin (void) 69 { 70 /* Mach-O only defines the .text section by default, and even this can 71 be suppressed by a flag. In the latter event, the first code MUST 72 be a section definition. */ 73 if (obj_mach_o_start_with_text_section) 74 { 75 text_section = obj_mach_o_segT_from_bfd_name (TEXT_SECTION_NAME, 1); 76 subseg_set (text_section, 0); 77 if (obj_mach_o_is_static) 78 { 79 bfd_mach_o_section *mo_sec 80 = bfd_mach_o_get_mach_o_section (text_section); 81 mo_sec->flags &= ~BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS; 82 } 83 } 84 } 85 86 /* Remember the subsections_by_symbols state in case we need to reset 87 the file flags. */ 88 89 static int obj_mach_o_subsections_by_symbols; 90 91 /* This will put at most 16 characters (terminated by a ',' or newline) from 92 the input stream into dest. If there are more than 16 chars before the 93 delimiter, a warning is given and the string is truncated. On completion of 94 this function, input_line_pointer will point to the char after the ',' or 95 to the newline. 96 97 It trims leading and trailing space. */ 98 99 static int 100 collect_16char_name (char *dest, const char *msg, int require_comma) 101 { 102 char c, *namstart; 103 104 SKIP_WHITESPACE (); 105 namstart = input_line_pointer; 106 107 while ( (c = *input_line_pointer) != ',' 108 && !is_end_of_line[(unsigned char) c]) 109 input_line_pointer++; 110 111 { 112 int len = input_line_pointer - namstart; /* could be zero. */ 113 /* lose any trailing space. */ 114 while (len > 0 && namstart[len-1] == ' ') 115 len--; 116 if (len > 16) 117 { 118 *input_line_pointer = '\0'; /* make a temp string. */ 119 as_bad (_("the %s name '%s' is too long (maximum 16 characters)"), 120 msg, namstart); 121 *input_line_pointer = c; /* restore for printing. */ 122 len = 16; 123 } 124 if (len > 0) 125 memcpy (dest, namstart, len); 126 } 127 128 if (c != ',' && require_comma) 129 { 130 as_bad (_("expected a %s name followed by a `,'"), msg); 131 return 1; 132 } 133 134 return 0; 135 } 136 137 static int 138 obj_mach_o_get_section_names (char *seg, char *sec, 139 unsigned segl, unsigned secl) 140 { 141 /* Zero-length segment and section names are allowed. */ 142 /* Parse segment name. */ 143 memset (seg, 0, segl); 144 if (collect_16char_name (seg, _("segment"), 1)) 145 { 146 ignore_rest_of_line (); 147 return 0; 148 } 149 input_line_pointer++; /* Skip the terminating ',' */ 150 151 /* Parse section name, which can be empty. */ 152 memset (sec, 0, secl); 153 collect_16char_name (sec, _("section"), 0); 154 return 1; 155 } 156 157 /* Build (or get) a section from the mach-o description - which includes 158 optional definitions for type, attributes, alignment and stub size. 159 160 BFD supplies default values for sections which have a canonical name. */ 161 162 #define SECT_TYPE_SPECIFIED 0x0001 163 #define SECT_ATTR_SPECIFIED 0x0002 164 #define SECT_ALGN_SPECIFIED 0x0004 165 #define SECT_STUB_SPECIFIED 0x0008 166 167 static segT 168 obj_mach_o_make_or_get_sect (char * segname, char * sectname, 169 unsigned int specified_mask, 170 unsigned int usectype, unsigned int usecattr, 171 unsigned int ualign, offsetT stub_size) 172 { 173 unsigned int sectype, secattr, secalign; 174 flagword oldflags, flags; 175 const char *name; 176 segT sec; 177 bfd_mach_o_section *msect; 178 const mach_o_section_name_xlat *xlat; 179 180 /* This provides default bfd flags and default mach-o section type and 181 attributes along with the canonical name. */ 182 xlat = bfd_mach_o_section_data_for_mach_sect (stdoutput, segname, sectname); 183 184 /* TODO: more checking of whether overrides are actually allowed. */ 185 186 if (xlat != NULL) 187 { 188 name = xstrdup (xlat->bfd_name); 189 sectype = xlat->macho_sectype; 190 if (specified_mask & SECT_TYPE_SPECIFIED) 191 { 192 if ((sectype == BFD_MACH_O_S_ZEROFILL 193 || sectype == BFD_MACH_O_S_GB_ZEROFILL) 194 && sectype != usectype) 195 as_bad (_("cannot override zerofill section type for `%s,%s'"), 196 segname, sectname); 197 else 198 sectype = usectype; 199 } 200 secattr = xlat->macho_secattr; 201 secalign = xlat->sectalign; 202 flags = xlat->bfd_flags; 203 } 204 else 205 { 206 /* There is no normal BFD section name for this section. Create one. 207 The name created doesn't really matter as it will never be written 208 on disk. */ 209 name = concat (segname, ".", sectname, (char *) NULL); 210 if (specified_mask & SECT_TYPE_SPECIFIED) 211 sectype = usectype; 212 else 213 sectype = BFD_MACH_O_S_REGULAR; 214 secattr = BFD_MACH_O_S_ATTR_NONE; 215 secalign = 0; 216 flags = SEC_NO_FLAGS; 217 } 218 219 /* For now, just use what the user provided. */ 220 221 if (specified_mask & SECT_ATTR_SPECIFIED) 222 secattr = usecattr; 223 224 if (specified_mask & SECT_ALGN_SPECIFIED) 225 secalign = ualign; 226 227 /* Sub-segments don't exists as is on Mach-O. */ 228 sec = subseg_new (name, 0); 229 230 oldflags = bfd_section_flags (sec); 231 msect = bfd_mach_o_get_mach_o_section (sec); 232 233 if (oldflags == SEC_NO_FLAGS) 234 { 235 /* In the absence of canonical information, try to determine CODE and 236 DEBUG section flags from the mach-o section data. */ 237 if (flags == SEC_NO_FLAGS 238 && (specified_mask & SECT_ATTR_SPECIFIED) 239 && (secattr & BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS)) 240 flags |= SEC_CODE; 241 242 if (flags == SEC_NO_FLAGS 243 && (specified_mask & SECT_ATTR_SPECIFIED) 244 && (secattr & BFD_MACH_O_S_ATTR_DEBUG)) 245 flags |= SEC_DEBUGGING; 246 247 /* New, so just use the defaults or what's specified. */ 248 if (!bfd_set_section_flags (sec, flags)) 249 as_warn (_("failed to set flags for \"%s\": %s"), 250 bfd_section_name (sec), 251 bfd_errmsg (bfd_get_error ())); 252 253 strncpy (msect->segname, segname, BFD_MACH_O_SEGNAME_SIZE); 254 msect->segname[BFD_MACH_O_SEGNAME_SIZE] = 0; 255 strncpy (msect->sectname, sectname, BFD_MACH_O_SECTNAME_SIZE); 256 msect->sectname[BFD_MACH_O_SECTNAME_SIZE] = 0; 257 258 msect->align = secalign; 259 msect->flags = sectype | secattr; 260 261 if (sectype == BFD_MACH_O_S_ZEROFILL 262 || sectype == BFD_MACH_O_S_GB_ZEROFILL) 263 seg_info (sec)->bss = 1; 264 } 265 else if (flags != SEC_NO_FLAGS) 266 { 267 if (flags != oldflags 268 || msect->flags != (secattr | sectype)) 269 as_warn (_("Ignoring changed section attributes for %s"), name); 270 } 271 272 if (specified_mask & SECT_STUB_SPECIFIED) 273 /* At present, the stub size is not supplied from the BFD tables. */ 274 msect->reserved2 = stub_size; 275 276 return sec; 277 } 278 279 /* .section 280 281 The '.section' specification syntax looks like: 282 .section <segment> , <section> [, type [, attribs [, size]]] 283 284 White space is allowed everywhere between elements. 285 286 <segment> and <section> may be from 0 to 16 chars in length - they may 287 contain spaces but leading and trailing space will be trimmed. It is 288 mandatory that they be present (or that zero-length names are indicated 289 by ",,"). 290 291 There is only a single section type for any entry. 292 293 There may be multiple attributes, they are delimited by `+'. 294 295 Not all section types and attributes are accepted by the Darwin system 296 assemblers as user-specifiable - although, at present, we do here. */ 297 298 static void 299 obj_mach_o_section (int ignore ATTRIBUTE_UNUSED) 300 { 301 unsigned int sectype = BFD_MACH_O_S_REGULAR; 302 unsigned int specified_mask = 0; 303 unsigned int secattr = 0; 304 offsetT sizeof_stub = 0; 305 segT new_seg; 306 char segname[17]; 307 char sectname[17]; 308 309 #ifdef md_flush_pending_output 310 md_flush_pending_output (); 311 #endif 312 313 /* Get the User's segment and section names. */ 314 if (! obj_mach_o_get_section_names (segname, sectname, 17, 17)) 315 return; 316 317 /* Parse section type, if present. */ 318 if (*input_line_pointer == ',') 319 { 320 char *p; 321 char c; 322 char tmpc; 323 int len; 324 input_line_pointer++; 325 SKIP_WHITESPACE (); 326 p = input_line_pointer; 327 while ((c = *input_line_pointer) != ',' 328 && !is_end_of_line[(unsigned char) c]) 329 input_line_pointer++; 330 331 len = input_line_pointer - p; 332 /* strip trailing spaces. */ 333 while (len > 0 && p[len-1] == ' ') 334 len--; 335 tmpc = p[len]; 336 337 /* Temporarily make a string from the token. */ 338 p[len] = 0; 339 sectype = bfd_mach_o_get_section_type_from_name (stdoutput, p); 340 if (sectype > 255) /* Max Section ID == 255. */ 341 { 342 as_bad (_("unknown or invalid section type '%s'"), p); 343 p[len] = tmpc; 344 ignore_rest_of_line (); 345 return; 346 } 347 else 348 specified_mask |= SECT_TYPE_SPECIFIED; 349 /* Restore. */ 350 p[len] = tmpc; 351 352 /* Parse attributes. 353 TODO: check validity of attributes for section type. */ 354 if ((specified_mask & SECT_TYPE_SPECIFIED) 355 && c == ',') 356 { 357 do 358 { 359 int attr; 360 361 /* Skip initial `,' and subsequent `+'. */ 362 input_line_pointer++; 363 SKIP_WHITESPACE (); 364 p = input_line_pointer; 365 while ((c = *input_line_pointer) != '+' 366 && c != ',' 367 && !is_end_of_line[(unsigned char) c]) 368 input_line_pointer++; 369 370 len = input_line_pointer - p; 371 /* strip trailing spaces. */ 372 while (len > 0 && p[len-1] == ' ') 373 len--; 374 tmpc = p[len]; 375 376 /* Temporarily make a string from the token. */ 377 p[len] ='\0'; 378 attr = bfd_mach_o_get_section_attribute_from_name (p); 379 if (attr == -1) 380 { 381 as_bad (_("unknown or invalid section attribute '%s'"), p); 382 p[len] = tmpc; 383 ignore_rest_of_line (); 384 return; 385 } 386 else 387 { 388 specified_mask |= SECT_ATTR_SPECIFIED; 389 secattr |= attr; 390 } 391 /* Restore. */ 392 p[len] = tmpc; 393 } 394 while (*input_line_pointer == '+'); 395 396 /* Parse sizeof_stub. */ 397 if ((specified_mask & SECT_ATTR_SPECIFIED) 398 && *input_line_pointer == ',') 399 { 400 if (sectype != BFD_MACH_O_S_SYMBOL_STUBS) 401 { 402 as_bad (_("unexpected section size information")); 403 ignore_rest_of_line (); 404 return; 405 } 406 407 input_line_pointer++; 408 sizeof_stub = get_absolute_expression (); 409 specified_mask |= SECT_STUB_SPECIFIED; 410 } 411 else if ((specified_mask & SECT_ATTR_SPECIFIED) 412 && sectype == BFD_MACH_O_S_SYMBOL_STUBS) 413 { 414 as_bad (_("missing sizeof_stub expression")); 415 ignore_rest_of_line (); 416 return; 417 } 418 } 419 } 420 421 new_seg = obj_mach_o_make_or_get_sect (segname, sectname, specified_mask, 422 sectype, secattr, 0 /*align */, 423 sizeof_stub); 424 if (new_seg != NULL) 425 { 426 subseg_set (new_seg, 0); 427 demand_empty_rest_of_line (); 428 } 429 } 430 431 /* .zerofill segname, sectname [, symbolname, size [, align]] 432 433 Zerofill switches, temporarily, to a sect of type 'zerofill'. 434 435 If a variable name is given, it defines that in the section. 436 Otherwise it just creates the section if it doesn't exist. */ 437 438 static void 439 obj_mach_o_zerofill (int ignore ATTRIBUTE_UNUSED) 440 { 441 char segname[17]; 442 char sectname[17]; 443 segT old_seg = now_seg; 444 segT new_seg; 445 symbolS *sym = NULL; 446 unsigned int align = 0; 447 unsigned int specified_mask = 0; 448 offsetT size = 0; 449 450 #ifdef md_flush_pending_output 451 md_flush_pending_output (); 452 #endif 453 454 /* Get the User's segment and section names. */ 455 if (! obj_mach_o_get_section_names (segname, sectname, 17, 17)) 456 return; 457 458 /* Parse variable definition, if present. */ 459 if (*input_line_pointer == ',') 460 { 461 /* Parse symbol, size [.align] 462 We follow the method of s_common_internal, with the difference 463 that the symbol cannot be a duplicate-common. */ 464 char *name; 465 char c; 466 char *p; 467 expressionS exp; 468 469 input_line_pointer++; /* Skip ',' */ 470 SKIP_WHITESPACE (); 471 c = get_symbol_name (&name); 472 /* Just after name is now '\0'. */ 473 p = input_line_pointer; 474 *p = c; 475 476 if (name == p) 477 { 478 as_bad (_("expected symbol name")); 479 ignore_rest_of_line (); 480 goto done; 481 } 482 483 SKIP_WHITESPACE_AFTER_NAME (); 484 if (*input_line_pointer == ',') 485 input_line_pointer++; 486 487 expression_and_evaluate (&exp); 488 if (exp.X_op != O_constant 489 && exp.X_op != O_absent) 490 { 491 as_bad (_("bad or irreducible absolute expression")); 492 ignore_rest_of_line (); 493 goto done; 494 } 495 else if (exp.X_op == O_absent) 496 { 497 as_bad (_("missing size expression")); 498 ignore_rest_of_line (); 499 goto done; 500 } 501 502 size = exp.X_add_number; 503 size &= ((valueT) 2 << (stdoutput->arch_info->bits_per_address - 1)) - 1; 504 if (exp.X_add_number != size || !exp.X_unsigned) 505 { 506 as_warn (_("size (%ld) out of range, ignored"), 507 (long) exp.X_add_number); 508 ignore_rest_of_line (); 509 goto done; 510 } 511 512 *p = 0; /* Make the name into a c string for err messages. */ 513 sym = symbol_find_or_make (name); 514 if (S_IS_DEFINED (sym) || symbol_equated_p (sym)) 515 { 516 as_bad (_("symbol `%s' is already defined"), name); 517 *p = c; 518 ignore_rest_of_line (); 519 goto done; 520 } 521 522 size = S_GET_VALUE (sym); 523 if (size == 0) 524 size = exp.X_add_number; 525 else if (size != exp.X_add_number) 526 as_warn (_("size of \"%s\" is already %ld; not changing to %ld"), 527 name, (long) size, (long) exp.X_add_number); 528 529 *p = c; /* Restore the termination char. */ 530 531 SKIP_WHITESPACE (); 532 if (*input_line_pointer == ',') 533 { 534 align = (unsigned int) parse_align (0); 535 if (align == (unsigned int) -1) 536 { 537 as_warn (_("align value not recognized, using size")); 538 align = size; 539 } 540 if (align > 15) 541 { 542 as_warn (_("Alignment (%lu) too large: 15 assumed."), 543 (unsigned long)align); 544 align = 15; 545 } 546 specified_mask |= SECT_ALGN_SPECIFIED; 547 } 548 } 549 /* else just a section definition. */ 550 551 specified_mask |= SECT_TYPE_SPECIFIED; 552 new_seg = obj_mach_o_make_or_get_sect (segname, sectname, specified_mask, 553 BFD_MACH_O_S_ZEROFILL, 554 BFD_MACH_O_S_ATTR_NONE, 555 align, (offsetT) 0 /*stub size*/); 556 if (new_seg == NULL) 557 return; 558 559 /* In case the user specifies the bss section by mach-o name. 560 Create it on demand */ 561 if (strcmp (new_seg->name, BSS_SECTION_NAME) == 0 562 && bss_section == NULL) 563 bss_section = new_seg; 564 565 subseg_set (new_seg, 0); 566 567 if (sym != NULL) 568 { 569 char *pfrag; 570 571 if (align) 572 { 573 record_alignment (new_seg, align); 574 frag_align (align, 0, 0); 575 } 576 577 /* Detach from old frag. */ 578 if (S_GET_SEGMENT (sym) == new_seg) 579 symbol_get_frag (sym)->fr_symbol = NULL; 580 581 symbol_set_frag (sym, frag_now); 582 pfrag = frag_var (rs_org, 1, 1, 0, sym, size, NULL); 583 *pfrag = 0; 584 585 S_SET_SEGMENT (sym, new_seg); 586 if (new_seg == bss_section) 587 S_CLEAR_EXTERNAL (sym); 588 } 589 590 done: 591 /* switch back to the section that was current before the .zerofill. */ 592 subseg_set (old_seg, 0); 593 } 594 595 static segT 596 obj_mach_o_segT_from_bfd_name (const char *nam, int must_succeed) 597 { 598 const mach_o_section_name_xlat *xlat; 599 const char *segn; 600 segT sec; 601 602 /* BFD has tables of flags and default attributes for all the sections that 603 have a 'canonical' name. */ 604 xlat = bfd_mach_o_section_data_for_bfd_name (stdoutput, nam, &segn); 605 if (xlat == NULL) 606 { 607 if (must_succeed) 608 as_fatal (_("BFD is out of sync with GAS, " 609 "unhandled well-known section type `%s'"), nam); 610 return NULL; 611 } 612 613 sec = bfd_get_section_by_name (stdoutput, nam); 614 if (sec == NULL) 615 { 616 bfd_mach_o_section *msect; 617 618 sec = subseg_force_new (xlat->bfd_name, 0); 619 620 /* Set default type, attributes and alignment. */ 621 msect = bfd_mach_o_get_mach_o_section (sec); 622 msect->flags = xlat->macho_sectype | xlat->macho_secattr; 623 msect->align = xlat->sectalign; 624 625 if ((msect->flags & BFD_MACH_O_SECTION_TYPE_MASK) 626 == BFD_MACH_O_S_ZEROFILL) 627 seg_info (sec)->bss = 1; 628 } 629 630 return sec; 631 } 632 633 static const char * const known_sections[] = 634 { 635 /* 0 */ NULL, 636 /* __TEXT */ 637 /* 1 */ ".const", 638 /* 2 */ ".static_const", 639 /* 3 */ ".cstring", 640 /* 4 */ ".literal4", 641 /* 5 */ ".literal8", 642 /* 6 */ ".literal16", 643 /* 7 */ ".constructor", 644 /* 8 */ ".destructor", 645 /* 9 */ ".eh_frame", 646 /* __DATA */ 647 /* 10 */ ".const_data", 648 /* 11 */ ".static_data", 649 /* 12 */ ".mod_init_func", 650 /* 13 */ ".mod_term_func", 651 /* 14 */ ".dyld", 652 /* 15 */ ".cfstring" 653 }; 654 655 /* Interface for a known non-optional section directive. */ 656 657 static void 658 obj_mach_o_known_section (int sect_index) 659 { 660 segT section; 661 662 #ifdef md_flush_pending_output 663 md_flush_pending_output (); 664 #endif 665 666 section = obj_mach_o_segT_from_bfd_name (known_sections[sect_index], 1); 667 if (section != NULL) 668 subseg_set (section, 0); 669 670 /* else, we leave the section as it was; there was a fatal error anyway. */ 671 } 672 673 static const char * const objc_sections[] = 674 { 675 /* 0 */ NULL, 676 /* 1 */ ".objc_class", 677 /* 2 */ ".objc_meta_class", 678 /* 3 */ ".objc_cat_cls_meth", 679 /* 4 */ ".objc_cat_inst_meth", 680 /* 5 */ ".objc_protocol", 681 /* 6 */ ".objc_string_object", 682 /* 7 */ ".objc_cls_meth", 683 /* 8 */ ".objc_inst_meth", 684 /* 9 */ ".objc_cls_refs", 685 /* 10 */ ".objc_message_refs", 686 /* 11 */ ".objc_symbols", 687 /* 12 */ ".objc_category", 688 /* 13 */ ".objc_class_vars", 689 /* 14 */ ".objc_instance_vars", 690 /* 15 */ ".objc_module_info", 691 /* 16 */ ".cstring", /* objc_class_names Alias for .cstring */ 692 /* 17 */ ".cstring", /* Alias objc_meth_var_types for .cstring */ 693 /* 18 */ ".cstring", /* objc_meth_var_names Alias for .cstring */ 694 /* 19 */ ".objc_selector_strs", 695 /* 20 */ ".objc_image_info", /* extension. */ 696 /* 21 */ ".objc_selector_fixup", /* extension. */ 697 /* 22 */ ".objc1_class_ext", /* ObjC-1 extension. */ 698 /* 23 */ ".objc1_property_list", /* ObjC-1 extension. */ 699 /* 24 */ ".objc1_protocol_ext" /* ObjC-1 extension. */ 700 }; 701 702 /* This currently does the same as known_sections, but kept separate for 703 ease of maintenance. */ 704 705 static void 706 obj_mach_o_objc_section (int sect_index) 707 { 708 segT section; 709 710 #ifdef md_flush_pending_output 711 md_flush_pending_output (); 712 #endif 713 714 section = obj_mach_o_segT_from_bfd_name (objc_sections[sect_index], 1); 715 if (section != NULL) 716 { 717 obj_mach_o_seen_objc_section = 1; /* We need to ensure that certain 718 sections are present and in the 719 right order. */ 720 subseg_set (section, 0); 721 } 722 723 /* else, we leave the section as it was; there was a fatal error anyway. */ 724 } 725 726 /* Debug section directives. */ 727 728 static const char * const debug_sections[] = 729 { 730 /* 0 */ NULL, 731 /* __DWARF */ 732 /* 1 */ ".debug_frame", 733 /* 2 */ ".debug_info", 734 /* 3 */ ".debug_abbrev", 735 /* 4 */ ".debug_aranges", 736 /* 5 */ ".debug_macinfo", 737 /* 6 */ ".debug_line", 738 /* 7 */ ".debug_loc", 739 /* 8 */ ".debug_pubnames", 740 /* 9 */ ".debug_pubtypes", 741 /* 10 */ ".debug_str", 742 /* 11 */ ".debug_ranges", 743 /* 12 */ ".debug_macro" 744 }; 745 746 /* ??? Maybe these should be conditional on gdwarf-*. 747 It`s also likely that we will need to be able to set them from the cfi 748 code. */ 749 750 static void 751 obj_mach_o_debug_section (int sect_index) 752 { 753 segT section; 754 755 #ifdef md_flush_pending_output 756 md_flush_pending_output (); 757 #endif 758 759 section = obj_mach_o_segT_from_bfd_name (debug_sections[sect_index], 1); 760 if (section != NULL) 761 subseg_set (section, 0); 762 763 /* else, we leave the section as it was; there was a fatal error anyway. */ 764 } 765 766 /* This could be moved to the tc-xx files, but there is so little dependency 767 there, that the code might as well be shared. */ 768 769 struct opt_tgt_sect 770 { 771 const char *name; 772 unsigned x86_val; 773 unsigned ppc_val; 774 }; 775 776 /* The extensions here are for specific sections that are generated by GCC 777 and Darwin system tools, but don't have directives in the `system as'. */ 778 779 static const struct opt_tgt_sect tgt_sections[] = 780 { 781 /* 0 */ { NULL, 0, 0}, 782 /* 1 */ { ".lazy_symbol_pointer", 0, 0}, 783 /* 2 */ { ".lazy_symbol_pointer2", 0, 0}, /* X86 - extension */ 784 /* 3 */ { ".lazy_symbol_pointer3", 0, 0}, /* X86 - extension */ 785 /* 4 */ { ".non_lazy_symbol_pointer", 0, 0}, 786 /* 5 */ { ".non_lazy_symbol_pointer_x86", 0, 0}, /* X86 - extension */ 787 /* 6 */ { ".symbol_stub", 16, 20}, 788 /* 7 */ { ".symbol_stub1", 0, 16}, /* PPC - extension */ 789 /* 8 */ { ".picsymbol_stub", 26, 36}, 790 /* 9 */ { ".picsymbol_stub1", 0, 32}, /* PPC - extension */ 791 /* 10 */ { ".picsymbol_stub2", 25, 0}, /* X86 - extension */ 792 /* 11 */ { ".picsymbol_stub3", 5, 0}, /* X86 - extension */ 793 }; 794 795 /* Interface for an optional section directive. */ 796 797 static void 798 obj_mach_o_opt_tgt_section (int sect_index) 799 { 800 const struct opt_tgt_sect *tgtsct = &tgt_sections[sect_index]; 801 segT section; 802 803 #ifdef md_flush_pending_output 804 md_flush_pending_output (); 805 #endif 806 807 section = obj_mach_o_segT_from_bfd_name (tgtsct->name, 0); 808 if (section == NULL) 809 { 810 as_bad (_("%s is not used for the selected target"), tgtsct->name); 811 /* Leave the section as it is. */ 812 } 813 else 814 { 815 bfd_mach_o_section *mo_sec = bfd_mach_o_get_mach_o_section (section); 816 subseg_set (section, 0); 817 #if defined (TC_I386) 818 mo_sec->reserved2 = tgtsct->x86_val; 819 #elif defined (TC_PPC) 820 mo_sec->reserved2 = tgtsct->ppc_val; 821 #else 822 mo_sec->reserved2 = 0; 823 #endif 824 } 825 } 826 827 /* We don't necessarily have the three 'base' sections on mach-o. 828 Normally, we would start up with only the 'text' section defined. 829 However, even that can be suppressed with (TODO) c/l option "-n". 830 Thus, we have to be able to create all three sections on-demand. */ 831 832 static void 833 obj_mach_o_base_section (int sect_index) 834 { 835 segT section; 836 837 #ifdef md_flush_pending_output 838 md_flush_pending_output (); 839 #endif 840 841 /* We don't support numeric (or any other) qualifications on the 842 well-known section shorthands. */ 843 demand_empty_rest_of_line (); 844 845 switch (sect_index) 846 { 847 /* Handle the three sections that are globally known within GAS. 848 For Mach-O, these are created on demand rather than at startup. */ 849 case 1: 850 if (text_section == NULL) 851 text_section = obj_mach_o_segT_from_bfd_name (TEXT_SECTION_NAME, 1); 852 if (obj_mach_o_is_static) 853 { 854 bfd_mach_o_section *mo_sec 855 = bfd_mach_o_get_mach_o_section (text_section); 856 mo_sec->flags &= ~BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS; 857 } 858 section = text_section; 859 break; 860 case 2: 861 if (data_section == NULL) 862 data_section = obj_mach_o_segT_from_bfd_name (DATA_SECTION_NAME, 1); 863 section = data_section; 864 break; 865 case 3: 866 /* ??? maybe this achieves very little, as an addition. */ 867 if (bss_section == NULL) 868 { 869 bss_section = obj_mach_o_segT_from_bfd_name (BSS_SECTION_NAME, 1); 870 seg_info (bss_section)->bss = 1; 871 } 872 section = bss_section; 873 break; 874 default: 875 as_fatal (_("internal error: base section index out of range")); 876 return; 877 break; 878 } 879 subseg_set (section, 0); 880 } 881 882 /* This finishes off parsing a .comm or .lcomm statement, which both can have 883 an (optional) alignment field. It also allows us to create the bss section 884 on demand. */ 885 886 static symbolS * 887 obj_mach_o_common_parse (int is_local, symbolS *symbolP, 888 addressT size) 889 { 890 addressT align = 0; 891 bfd_mach_o_asymbol *s; 892 893 SKIP_WHITESPACE (); 894 895 /* Both comm and lcomm take an optional alignment, as a power 896 of two between 1 and 15. */ 897 if (*input_line_pointer == ',') 898 { 899 /* We expect a power of 2. */ 900 align = parse_align (0); 901 if (align == (addressT) -1) 902 return NULL; 903 if (align > 15) 904 { 905 as_warn (_("Alignment (%lu) too large: 15 assumed."), 906 (unsigned long)align); 907 align = 15; 908 } 909 } 910 911 s = (bfd_mach_o_asymbol *) symbol_get_bfdsym (symbolP); 912 if (is_local) 913 { 914 /* Create the BSS section on demand. */ 915 if (bss_section == NULL) 916 { 917 bss_section = obj_mach_o_segT_from_bfd_name (BSS_SECTION_NAME, 1); 918 seg_info (bss_section)->bss = 1; 919 } 920 bss_alloc (symbolP, size, align); 921 s->n_type = BFD_MACH_O_N_SECT; 922 S_CLEAR_EXTERNAL (symbolP); 923 } 924 else 925 { 926 S_SET_VALUE (symbolP, size); 927 S_SET_ALIGN (symbolP, align); 928 S_SET_EXTERNAL (symbolP); 929 S_SET_SEGMENT (symbolP, bfd_com_section_ptr); 930 s->n_type = BFD_MACH_O_N_UNDF | BFD_MACH_O_N_EXT; 931 } 932 933 /* This is a data object (whatever we choose that to mean). */ 934 s->symbol.flags |= BSF_OBJECT; 935 936 /* We've set symbol qualifiers, so validate if you can. */ 937 s->symbol.udata.i = SYM_MACHO_FIELDS_NOT_VALIDATED; 938 939 return symbolP; 940 } 941 942 static void 943 obj_mach_o_comm (int is_local) 944 { 945 s_comm_internal (is_local, obj_mach_o_common_parse); 946 } 947 948 /* Set properties that apply to the whole file. At present, the only 949 one defined, is subsections_via_symbols. */ 950 951 typedef enum obj_mach_o_file_properties { 952 OBJ_MACH_O_FILE_PROP_NONE = 0, 953 OBJ_MACH_O_FILE_PROP_SUBSECTS_VIA_SYMS, 954 OBJ_MACH_O_FILE_PROP_MAX 955 } obj_mach_o_file_properties; 956 957 static void 958 obj_mach_o_fileprop (int prop) 959 { 960 if (prop < 0 || prop >= OBJ_MACH_O_FILE_PROP_MAX) 961 as_fatal (_("internal error: bad file property ID %d"), prop); 962 963 switch ((obj_mach_o_file_properties) prop) 964 { 965 case OBJ_MACH_O_FILE_PROP_SUBSECTS_VIA_SYMS: 966 obj_mach_o_subsections_by_symbols = 1; 967 if (!bfd_set_private_flags (stdoutput, 968 BFD_MACH_O_MH_SUBSECTIONS_VIA_SYMBOLS)) 969 as_bad (_("failed to set subsections by symbols")); 970 demand_empty_rest_of_line (); 971 break; 972 default: 973 break; 974 } 975 } 976 977 /* Temporary markers for symbol reference data. 978 Lazy will remain in place. */ 979 #define LAZY 0x01 980 #define REFE 0x02 981 982 /* We have a bunch of qualifiers that may be applied to symbols. 983 .globl is handled here so that we might make sure that conflicting qualifiers 984 are caught where possible. */ 985 986 typedef enum obj_mach_o_symbol_type { 987 OBJ_MACH_O_SYM_UNK = 0, 988 OBJ_MACH_O_SYM_LOCAL = 1, 989 OBJ_MACH_O_SYM_GLOBL = 2, 990 OBJ_MACH_O_SYM_REFERENCE = 3, 991 OBJ_MACH_O_SYM_WEAK_REF = 4, 992 OBJ_MACH_O_SYM_LAZY_REF = 5, 993 OBJ_MACH_O_SYM_WEAK_DEF = 6, 994 OBJ_MACH_O_SYM_PRIV_EXT = 7, 995 OBJ_MACH_O_SYM_NO_DEAD_STRIP = 8, 996 OBJ_MACH_O_SYM_WEAK = 9 997 } obj_mach_o_symbol_type; 998 999 /* Set Mach-O-specific symbol qualifiers. */ 1000 1001 static int 1002 obj_mach_o_set_symbol_qualifier (symbolS *sym, int type) 1003 { 1004 int is_defined; 1005 bfd_mach_o_asymbol *s = (bfd_mach_o_asymbol *) symbol_get_bfdsym (sym); 1006 bfd_mach_o_section *sec; 1007 int sectype = -1; 1008 1009 /* If the symbol is defined, then we can do more rigorous checking on 1010 the validity of the qualifiers. Otherwise, we are stuck with waiting 1011 until it's defined - or until write the file. 1012 1013 In certain cases (e.g. when a symbol qualifier is intended to introduce 1014 an undefined symbol in a stubs section) we should check that the current 1015 section is appropriate to the qualifier. */ 1016 1017 is_defined = s->symbol.section != bfd_und_section_ptr; 1018 if (is_defined) 1019 sec = bfd_mach_o_get_mach_o_section (s->symbol.section) ; 1020 else 1021 sec = bfd_mach_o_get_mach_o_section (now_seg) ; 1022 1023 if (sec != NULL) 1024 sectype = sec->flags & BFD_MACH_O_SECTION_TYPE_MASK; 1025 1026 switch ((obj_mach_o_symbol_type) type) 1027 { 1028 case OBJ_MACH_O_SYM_LOCAL: 1029 /* This is an extension over the system tools. */ 1030 if (s->n_type & (BFD_MACH_O_N_PEXT | BFD_MACH_O_N_EXT)) 1031 { 1032 as_bad (_("'%s' previously declared as '%s'."), s->symbol.name, 1033 (s->n_type & BFD_MACH_O_N_PEXT) ? "private extern" 1034 : "global" ); 1035 s->symbol.udata.i = SYM_MACHO_FIELDS_UNSET; 1036 return 1; 1037 } 1038 else 1039 { 1040 s->n_type &= ~BFD_MACH_O_N_EXT; 1041 S_CLEAR_EXTERNAL (sym); 1042 } 1043 break; 1044 1045 case OBJ_MACH_O_SYM_PRIV_EXT: 1046 s->n_type |= BFD_MACH_O_N_PEXT ; 1047 s->n_desc &= ~LAZY; /* The native tool switches this off too. */ 1048 /* We follow the system tools in marking PEXT as also global. */ 1049 /* Fall through. */ 1050 1051 case OBJ_MACH_O_SYM_GLOBL: 1052 /* It's not an error to define a symbol and then make it global. */ 1053 s->n_type |= BFD_MACH_O_N_EXT; 1054 S_SET_EXTERNAL (sym); 1055 break; 1056 1057 case OBJ_MACH_O_SYM_REFERENCE: 1058 if (is_defined) 1059 s->n_desc |= BFD_MACH_O_N_NO_DEAD_STRIP; 1060 else 1061 s->n_desc |= (REFE | BFD_MACH_O_N_NO_DEAD_STRIP); 1062 break; 1063 1064 case OBJ_MACH_O_SYM_LAZY_REF: 1065 if (is_defined) 1066 s->n_desc |= BFD_MACH_O_N_NO_DEAD_STRIP; 1067 else 1068 s->n_desc |= (REFE | LAZY | BFD_MACH_O_N_NO_DEAD_STRIP); 1069 break; 1070 1071 /* Force ld to retain the symbol - even if it appears unused. */ 1072 case OBJ_MACH_O_SYM_NO_DEAD_STRIP: 1073 s->n_desc |= BFD_MACH_O_N_NO_DEAD_STRIP ; 1074 break; 1075 1076 /* Mach-O's idea of weak ... */ 1077 case OBJ_MACH_O_SYM_WEAK_REF: 1078 s->n_desc |= BFD_MACH_O_N_WEAK_REF ; 1079 break; 1080 1081 case OBJ_MACH_O_SYM_WEAK_DEF: 1082 if (is_defined && sectype != BFD_MACH_O_S_COALESCED) 1083 { 1084 as_bad (_("'%s' can't be a weak_definition (currently only" 1085 " supported in sections of type coalesced)"), 1086 s->symbol.name); 1087 s->symbol.udata.i = SYM_MACHO_FIELDS_UNSET; 1088 return 1; 1089 } 1090 else 1091 s->n_desc |= BFD_MACH_O_N_WEAK_DEF; 1092 break; 1093 1094 case OBJ_MACH_O_SYM_WEAK: 1095 /* A generic 'weak' - we try to figure out what it means at 1096 symbol frob time. */ 1097 S_SET_WEAK (sym); 1098 break; 1099 1100 default: 1101 break; 1102 } 1103 1104 /* We've seen some kind of qualifier - check validity if or when the entity 1105 is defined. */ 1106 s->symbol.udata.i = SYM_MACHO_FIELDS_NOT_VALIDATED; 1107 return 0; 1108 } 1109 1110 /* Respond to symbol qualifiers. 1111 All of the form: 1112 .<qualifier> symbol [, symbol]* 1113 a list of symbols is an extension over the Darwin system as. */ 1114 1115 static void 1116 obj_mach_o_sym_qual (int ntype) 1117 { 1118 char *name; 1119 char c; 1120 symbolS *symbolP; 1121 1122 #ifdef md_flush_pending_output 1123 md_flush_pending_output (); 1124 #endif 1125 1126 do 1127 { 1128 c = get_symbol_name (&name); 1129 symbolP = symbol_find_or_make (name); 1130 obj_mach_o_set_symbol_qualifier (symbolP, ntype); 1131 *input_line_pointer = c; 1132 SKIP_WHITESPACE_AFTER_NAME (); 1133 c = *input_line_pointer; 1134 if (c == ',') 1135 { 1136 input_line_pointer++; 1137 SKIP_WHITESPACE (); 1138 if (is_end_of_line[(unsigned char) *input_line_pointer]) 1139 c = '\n'; 1140 } 1141 } 1142 while (c == ','); 1143 1144 demand_empty_rest_of_line (); 1145 } 1146 1147 typedef struct obj_mach_o_indirect_sym 1148 { 1149 symbolS *sym; 1150 segT sect; 1151 struct obj_mach_o_indirect_sym *next; 1152 } obj_mach_o_indirect_sym; 1153 1154 /* We store in order an maintain a pointer to the last one - to save reversing 1155 later. */ 1156 obj_mach_o_indirect_sym *indirect_syms; 1157 obj_mach_o_indirect_sym *indirect_syms_tail; 1158 1159 static void 1160 obj_mach_o_indirect_symbol (int arg ATTRIBUTE_UNUSED) 1161 { 1162 bfd_mach_o_section *sec = bfd_mach_o_get_mach_o_section (now_seg); 1163 1164 #ifdef md_flush_pending_output 1165 md_flush_pending_output (); 1166 #endif 1167 1168 if (obj_mach_o_is_static) 1169 as_bad (_("use of .indirect_symbols requires `-dynamic'")); 1170 1171 switch (sec->flags & BFD_MACH_O_SECTION_TYPE_MASK) 1172 { 1173 case BFD_MACH_O_S_SYMBOL_STUBS: 1174 case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS: 1175 case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS: 1176 { 1177 obj_mach_o_indirect_sym *isym; 1178 char *name; 1179 char c = get_symbol_name (&name); 1180 symbolS *sym = symbol_find_or_make (name); 1181 unsigned int elsize = 1182 bfd_mach_o_section_get_entry_size (stdoutput, sec); 1183 1184 if (elsize == 0) 1185 { 1186 as_bad (_("attempt to add an indirect_symbol to a stub or" 1187 " reference section with a zero-sized element at %s"), 1188 name); 1189 (void) restore_line_pointer (c); 1190 ignore_rest_of_line (); 1191 return; 1192 } 1193 (void) restore_line_pointer (c); 1194 1195 /* The indirect symbols are validated after the symbol table is 1196 frozen, we must make sure that if a local symbol is used as an 1197 indirect, it is promoted to a 'real' one. Fetching the bfd sym 1198 achieves this. */ 1199 symbol_get_bfdsym (sym); 1200 isym = XNEW (obj_mach_o_indirect_sym); 1201 1202 /* Just record the data for now, we will validate it when we 1203 compute the output in obj_mach_o_set_indirect_symbols. */ 1204 isym->sym = sym; 1205 isym->sect = now_seg; 1206 isym->next = NULL; 1207 if (indirect_syms == NULL) 1208 indirect_syms = isym; 1209 else 1210 indirect_syms_tail->next = isym; 1211 indirect_syms_tail = isym; 1212 } 1213 break; 1214 1215 default: 1216 as_bad (_("an .indirect_symbol must be in a symbol pointer" 1217 " or stub section.")); 1218 ignore_rest_of_line (); 1219 return; 1220 } 1221 demand_empty_rest_of_line (); 1222 } 1223 1224 const pseudo_typeS mach_o_pseudo_table[] = 1225 { 1226 /* Section directives. */ 1227 { "comm", obj_mach_o_comm, 0 }, 1228 { "lcomm", obj_mach_o_comm, 1 }, 1229 1230 { "text", obj_mach_o_base_section, 1}, 1231 { "data", obj_mach_o_base_section, 2}, 1232 { "bss", obj_mach_o_base_section, 3}, /* extension */ 1233 1234 { "const", obj_mach_o_known_section, 1}, 1235 { "static_const", obj_mach_o_known_section, 2}, 1236 { "cstring", obj_mach_o_known_section, 3}, 1237 { "literal4", obj_mach_o_known_section, 4}, 1238 { "literal8", obj_mach_o_known_section, 5}, 1239 { "literal16", obj_mach_o_known_section, 6}, 1240 { "constructor", obj_mach_o_known_section, 7}, 1241 { "destructor", obj_mach_o_known_section, 8}, 1242 { "eh_frame", obj_mach_o_known_section, 9}, 1243 1244 { "const_data", obj_mach_o_known_section, 10}, 1245 { "static_data", obj_mach_o_known_section, 11}, 1246 { "mod_init_func", obj_mach_o_known_section, 12}, 1247 { "mod_term_func", obj_mach_o_known_section, 13}, 1248 { "dyld", obj_mach_o_known_section, 14}, 1249 { "cfstring", obj_mach_o_known_section, 15}, 1250 1251 { "objc_class", obj_mach_o_objc_section, 1}, 1252 { "objc_meta_class", obj_mach_o_objc_section, 2}, 1253 { "objc_cat_cls_meth", obj_mach_o_objc_section, 3}, 1254 { "objc_cat_inst_meth", obj_mach_o_objc_section, 4}, 1255 { "objc_protocol", obj_mach_o_objc_section, 5}, 1256 { "objc_string_object", obj_mach_o_objc_section, 6}, 1257 { "objc_cls_meth", obj_mach_o_objc_section, 7}, 1258 { "objc_inst_meth", obj_mach_o_objc_section, 8}, 1259 { "objc_cls_refs", obj_mach_o_objc_section, 9}, 1260 { "objc_message_refs", obj_mach_o_objc_section, 10}, 1261 { "objc_symbols", obj_mach_o_objc_section, 11}, 1262 { "objc_category", obj_mach_o_objc_section, 12}, 1263 { "objc_class_vars", obj_mach_o_objc_section, 13}, 1264 { "objc_instance_vars", obj_mach_o_objc_section, 14}, 1265 { "objc_module_info", obj_mach_o_objc_section, 15}, 1266 { "objc_class_names", obj_mach_o_objc_section, 16}, /* Alias for .cstring */ 1267 { "objc_meth_var_types", obj_mach_o_objc_section, 17}, /* Alias for .cstring */ 1268 { "objc_meth_var_names", obj_mach_o_objc_section, 18}, /* Alias for .cstring */ 1269 { "objc_selector_strs", obj_mach_o_objc_section, 19}, 1270 { "objc_image_info", obj_mach_o_objc_section, 20}, /* extension. */ 1271 { "objc_selector_fixup", obj_mach_o_objc_section, 21}, /* extension. */ 1272 { "objc1_class_ext", obj_mach_o_objc_section, 22}, /* ObjC-1 extension. */ 1273 { "objc1_property_list", obj_mach_o_objc_section, 23}, /* ObjC-1 extension. */ 1274 { "objc1_protocol_ext", obj_mach_o_objc_section, 24}, /* ObjC-1 extension. */ 1275 1276 { "debug_frame", obj_mach_o_debug_section, 1}, /* extension. */ 1277 { "debug_info", obj_mach_o_debug_section, 2}, /* extension. */ 1278 { "debug_abbrev", obj_mach_o_debug_section, 3}, /* extension. */ 1279 { "debug_aranges", obj_mach_o_debug_section, 4}, /* extension. */ 1280 { "debug_macinfo", obj_mach_o_debug_section, 5}, /* extension. */ 1281 { "debug_line", obj_mach_o_debug_section, 6}, /* extension. */ 1282 { "debug_loc", obj_mach_o_debug_section, 7}, /* extension. */ 1283 { "debug_pubnames", obj_mach_o_debug_section, 8}, /* extension. */ 1284 { "debug_pubtypes", obj_mach_o_debug_section, 9}, /* extension. */ 1285 { "debug_str", obj_mach_o_debug_section, 10}, /* extension. */ 1286 { "debug_ranges", obj_mach_o_debug_section, 11}, /* extension. */ 1287 { "debug_macro", obj_mach_o_debug_section, 12}, /* extension. */ 1288 1289 { "lazy_symbol_pointer", obj_mach_o_opt_tgt_section, 1}, 1290 { "lazy_symbol_pointer2", obj_mach_o_opt_tgt_section, 2}, /* extension. */ 1291 { "lazy_symbol_pointer3", obj_mach_o_opt_tgt_section, 3}, /* extension. */ 1292 { "non_lazy_symbol_pointer", obj_mach_o_opt_tgt_section, 4}, 1293 { "non_lazy_symbol_pointer_x86", obj_mach_o_opt_tgt_section, 5}, /* extension. */ 1294 { "symbol_stub", obj_mach_o_opt_tgt_section, 6}, 1295 { "symbol_stub1", obj_mach_o_opt_tgt_section, 7}, /* extension. */ 1296 { "picsymbol_stub", obj_mach_o_opt_tgt_section, 8}, /* extension. */ 1297 { "picsymbol_stub1", obj_mach_o_opt_tgt_section, 9}, /* extension. */ 1298 { "picsymbol_stub2", obj_mach_o_opt_tgt_section, 4}, /* extension. */ 1299 { "picsymbol_stub3", obj_mach_o_opt_tgt_section, 4}, /* extension. */ 1300 1301 { "section", obj_mach_o_section, 0}, 1302 { "zerofill", obj_mach_o_zerofill, 0}, 1303 1304 /* Symbol qualifiers. */ 1305 {"local", obj_mach_o_sym_qual, OBJ_MACH_O_SYM_LOCAL}, 1306 {"globl", obj_mach_o_sym_qual, OBJ_MACH_O_SYM_GLOBL}, 1307 {"reference", obj_mach_o_sym_qual, OBJ_MACH_O_SYM_REFERENCE}, 1308 {"weak_reference", obj_mach_o_sym_qual, OBJ_MACH_O_SYM_WEAK_REF}, 1309 {"lazy_reference", obj_mach_o_sym_qual, OBJ_MACH_O_SYM_LAZY_REF}, 1310 {"weak_definition", obj_mach_o_sym_qual, OBJ_MACH_O_SYM_WEAK_DEF}, 1311 {"private_extern", obj_mach_o_sym_qual, OBJ_MACH_O_SYM_PRIV_EXT}, 1312 {"no_dead_strip", obj_mach_o_sym_qual, OBJ_MACH_O_SYM_NO_DEAD_STRIP}, 1313 {"weak", obj_mach_o_sym_qual, OBJ_MACH_O_SYM_WEAK}, /* ext */ 1314 1315 { "indirect_symbol", obj_mach_o_indirect_symbol, 0}, 1316 1317 /* File flags. */ 1318 { "subsections_via_symbols", obj_mach_o_fileprop, 1319 OBJ_MACH_O_FILE_PROP_SUBSECTS_VIA_SYMS}, 1320 1321 {NULL, NULL, 0} 1322 }; 1323 1324 /* Determine the default n_type value for a symbol from its section. */ 1325 1326 static unsigned 1327 obj_mach_o_type_for_symbol (bfd_mach_o_asymbol *s) 1328 { 1329 if (s->symbol.section == bfd_abs_section_ptr) 1330 return BFD_MACH_O_N_ABS; 1331 else if (s->symbol.section == bfd_com_section_ptr 1332 || s->symbol.section == bfd_und_section_ptr) 1333 return BFD_MACH_O_N_UNDF; 1334 else 1335 return BFD_MACH_O_N_SECT; 1336 } 1337 1338 void 1339 obj_mach_o_frob_colon (const char *name) 1340 { 1341 if (!bfd_is_local_label_name (stdoutput, name)) 1342 { 1343 /* A non-local label will create a new subsection, so start a new 1344 frag. */ 1345 frag_wane (frag_now); 1346 frag_new (0); 1347 } 1348 } 1349 1350 /* We need to check the correspondence between some kinds of symbols and their 1351 sections. Common and BSS vars will seen via the obj_macho_comm() function. 1352 1353 The earlier we can pick up a problem, the better the diagnostics will be. 1354 1355 However, when symbol type information is attached, the symbol section will 1356 quite possibly be unknown. So we are stuck with checking (most of the) 1357 validity at the time the file is written (unfortunately, then one doesn't 1358 get line number information in the diagnostic). */ 1359 1360 /* Here we pick up the case where symbol qualifiers have been applied that 1361 are possibly incompatible with the section etc. that the symbol is defined 1362 in. */ 1363 1364 void obj_mach_o_frob_label (struct symbol *sp) 1365 { 1366 bfd_mach_o_asymbol *s; 1367 unsigned base_type; 1368 bfd_mach_o_section *sec; 1369 int sectype = -1; 1370 1371 if (!bfd_is_local_label_name (stdoutput, S_GET_NAME (sp))) 1372 { 1373 /* If this is a non-local label, it should have started a new sub- 1374 section. */ 1375 gas_assert (frag_now->obj_frag_data.subsection == NULL); 1376 frag_now->obj_frag_data.subsection = sp; 1377 } 1378 1379 /* Leave local symbols alone. */ 1380 1381 if (S_IS_LOCAL (sp)) 1382 return; 1383 1384 s = (bfd_mach_o_asymbol *) symbol_get_bfdsym (sp); 1385 /* Leave debug symbols alone. */ 1386 if ((s->n_type & BFD_MACH_O_N_STAB) != 0) 1387 return; 1388 1389 /* This is the base symbol type, that we mask in. */ 1390 base_type = obj_mach_o_type_for_symbol (s); 1391 1392 sec = bfd_mach_o_get_mach_o_section (s->symbol.section); 1393 if (sec != NULL) 1394 sectype = sec->flags & BFD_MACH_O_SECTION_TYPE_MASK; 1395 1396 /* If there is a pre-existing qualifier, we can make some checks about 1397 validity now. */ 1398 1399 if(s->symbol.udata.i == SYM_MACHO_FIELDS_NOT_VALIDATED) 1400 { 1401 if ((s->n_desc & BFD_MACH_O_N_WEAK_DEF) 1402 && sectype != BFD_MACH_O_S_COALESCED) 1403 { 1404 as_bad (_("'%s' can't be a weak_definition (currently only supported" 1405 " in sections of type coalesced)"), s->symbol.name); 1406 /* Don't cascade errors. */ 1407 s->symbol.udata.i = SYM_MACHO_FIELDS_UNSET; 1408 } 1409 1410 /* Have we changed from an undefined to defined ref? */ 1411 s->n_desc &= ~(REFE | LAZY); 1412 } 1413 1414 s->n_type &= ~BFD_MACH_O_N_TYPE; 1415 s->n_type |= base_type; 1416 } 1417 1418 /* This is the fall-back, we come here when we get to the end of the file and 1419 the symbol is not defined - or there are combinations of qualifiers required 1420 (e.g. global + weak_def). */ 1421 1422 int 1423 obj_mach_o_frob_symbol (struct symbol *sp) 1424 { 1425 bfd_mach_o_asymbol *s; 1426 unsigned base_type; 1427 bfd_mach_o_section *sec; 1428 int sectype = -1; 1429 1430 /* Leave local symbols alone. */ 1431 if (S_IS_LOCAL (sp)) 1432 return 0; 1433 1434 s = (bfd_mach_o_asymbol *) symbol_get_bfdsym (sp); 1435 /* Leave debug symbols alone. */ 1436 if ((s->n_type & BFD_MACH_O_N_STAB) != 0) 1437 return 0; 1438 1439 base_type = obj_mach_o_type_for_symbol (s); 1440 sec = bfd_mach_o_get_mach_o_section (s->symbol.section); 1441 if (sec != NULL) 1442 sectype = sec->flags & BFD_MACH_O_SECTION_TYPE_MASK; 1443 1444 if (s->symbol.section == bfd_und_section_ptr) 1445 { 1446 /* ??? Do we really gain much from implementing this as well as the 1447 mach-o specific ones? */ 1448 if (s->symbol.flags & BSF_WEAK) 1449 s->n_desc |= BFD_MACH_O_N_WEAK_REF; 1450 1451 /* Undefined syms, become extern. */ 1452 s->n_type |= BFD_MACH_O_N_EXT; 1453 S_SET_EXTERNAL (sp); 1454 } 1455 else if (s->symbol.section == bfd_com_section_ptr) 1456 { 1457 /* ... so do comm. */ 1458 s->n_type |= BFD_MACH_O_N_EXT; 1459 S_SET_EXTERNAL (sp); 1460 } 1461 else 1462 { 1463 if ((s->symbol.flags & BSF_WEAK) 1464 && (sectype == BFD_MACH_O_S_COALESCED) 1465 && (s->n_type & (BFD_MACH_O_N_PEXT | BFD_MACH_O_N_EXT))) 1466 s->n_desc |= BFD_MACH_O_N_WEAK_DEF; 1467 /* ??? we should do this - but then that reveals that the semantics of weak 1468 are different from what's supported in mach-o object files. 1469 else 1470 as_bad (_("'%s' can't be a weak_definition."), 1471 s->symbol.name); */ 1472 } 1473 1474 if (s->symbol.udata.i == SYM_MACHO_FIELDS_UNSET) 1475 { 1476 /* Anything here that should be added that is non-standard. */ 1477 s->n_desc &= ~BFD_MACH_O_REFERENCE_MASK; 1478 } 1479 else if (s->symbol.udata.i == SYM_MACHO_FIELDS_NOT_VALIDATED) 1480 { 1481 /* Try to validate any combinations. */ 1482 if (s->n_desc & BFD_MACH_O_N_WEAK_DEF) 1483 { 1484 if (s->symbol.section == bfd_und_section_ptr) 1485 as_bad (_("'%s' can't be a weak_definition (since it is" 1486 " undefined)"), s->symbol.name); 1487 else if (sectype != BFD_MACH_O_S_COALESCED) 1488 as_bad (_("'%s' can't be a weak_definition (currently only supported" 1489 " in sections of type coalesced)"), s->symbol.name); 1490 else if (! (s->n_type & (BFD_MACH_O_N_PEXT | BFD_MACH_O_N_EXT))) 1491 as_bad (_("Non-global symbol: '%s' can't be a weak_definition."), 1492 s->symbol.name); 1493 } 1494 1495 } 1496 else 1497 as_bad (_("internal error: [%s] unexpected code [%lx] in frob symbol"), 1498 s->symbol.name, (unsigned long)s->symbol.udata.i); 1499 1500 s->n_type &= ~BFD_MACH_O_N_TYPE; 1501 s->n_type |= base_type; 1502 1503 if (s->symbol.flags & BSF_GLOBAL) 1504 s->n_type |= BFD_MACH_O_N_EXT; 1505 1506 /* This cuts both ways - we promote some things to external above. */ 1507 if (s->n_type & (BFD_MACH_O_N_PEXT | BFD_MACH_O_N_EXT)) 1508 S_SET_EXTERNAL (sp); 1509 1510 return 0; 1511 } 1512 1513 /* Support stabs for mach-o. */ 1514 1515 void 1516 obj_mach_o_process_stab (int what, const char *string, 1517 int type, int other, int desc) 1518 { 1519 symbolS *symbolP; 1520 bfd_mach_o_asymbol *s; 1521 1522 switch (what) 1523 { 1524 case 'd': 1525 symbolP = symbol_new ("", now_seg, frag_now, frag_now_fix ()); 1526 /* Special stabd NULL name indicator. */ 1527 S_SET_NAME (symbolP, NULL); 1528 break; 1529 1530 case 'n': 1531 case 's': 1532 symbolP = symbol_new (string, undefined_section, 1533 &zero_address_frag, 0); 1534 pseudo_set (symbolP); 1535 break; 1536 1537 default: 1538 as_bad(_("unrecognized stab type '%c'"), (char)what); 1539 abort (); 1540 break; 1541 } 1542 1543 s = (bfd_mach_o_asymbol *) symbol_get_bfdsym (symbolP); 1544 s->n_type = type; 1545 s->n_desc = desc; 1546 /* For stabd, this will eventually get overwritten by the section number. */ 1547 s->n_sect = other; 1548 1549 /* It's a debug symbol. */ 1550 s->symbol.flags |= BSF_DEBUGGING; 1551 1552 /* We've set it - so check it, if you can, but don't try to create the 1553 flags. */ 1554 s->symbol.udata.i = SYM_MACHO_FIELDS_NOT_VALIDATED; 1555 } 1556 1557 /* This is a place to check for any errors that we can't detect until we know 1558 what remains undefined at the end of assembly. */ 1559 1560 static void 1561 obj_mach_o_check_before_writing (bfd *abfd ATTRIBUTE_UNUSED, 1562 asection *sec, 1563 void *unused ATTRIBUTE_UNUSED) 1564 { 1565 fixS *fixP; 1566 struct frchain *frchp; 1567 segment_info_type *seginfo = seg_info (sec); 1568 1569 if (seginfo == NULL) 1570 return; 1571 1572 /* We are not allowed subtractions where either of the operands is 1573 undefined. So look through the frags for any fixes to check. */ 1574 for (frchp = seginfo->frchainP; frchp != NULL; frchp = frchp->frch_next) 1575 for (fixP = frchp->fix_root; fixP != NULL; fixP = fixP->fx_next) 1576 { 1577 if (fixP->fx_addsy != NULL 1578 && fixP->fx_subsy != NULL 1579 && (! S_IS_DEFINED (fixP->fx_addsy) 1580 || ! S_IS_DEFINED (fixP->fx_subsy))) 1581 { 1582 segT add_symbol_segment = S_GET_SEGMENT (fixP->fx_addsy); 1583 segT sub_symbol_segment = S_GET_SEGMENT (fixP->fx_subsy); 1584 1585 if (! S_IS_DEFINED (fixP->fx_addsy) 1586 && S_IS_DEFINED (fixP->fx_subsy)) 1587 { 1588 as_bad_where (fixP->fx_file, fixP->fx_line, 1589 _("`%s' can't be undefined in `%s' - `%s' {%s section}"), 1590 S_GET_NAME (fixP->fx_addsy), S_GET_NAME (fixP->fx_addsy), 1591 S_GET_NAME (fixP->fx_subsy), segment_name (sub_symbol_segment)); 1592 } 1593 else if (! S_IS_DEFINED (fixP->fx_subsy) 1594 && S_IS_DEFINED (fixP->fx_addsy)) 1595 { 1596 as_bad_where (fixP->fx_file, fixP->fx_line, 1597 _("`%s' can't be undefined in `%s' {%s section} - `%s'"), 1598 S_GET_NAME (fixP->fx_subsy), S_GET_NAME (fixP->fx_addsy), 1599 segment_name (add_symbol_segment), S_GET_NAME (fixP->fx_subsy)); 1600 } 1601 else 1602 { 1603 as_bad_where (fixP->fx_file, fixP->fx_line, 1604 _("`%s' and `%s' can't be undefined in `%s' - `%s'"), 1605 S_GET_NAME (fixP->fx_addsy), S_GET_NAME (fixP->fx_subsy), 1606 S_GET_NAME (fixP->fx_addsy), S_GET_NAME (fixP->fx_subsy)); 1607 } 1608 } 1609 } 1610 } 1611 1612 /* Do any checks that we can't complete without knowing what's undefined. */ 1613 void 1614 obj_mach_o_pre_output_hook (void) 1615 { 1616 bfd_map_over_sections (stdoutput, obj_mach_o_check_before_writing, (char *) 0); 1617 } 1618 1619 /* Here we count up frags in each subsection (where a sub-section is defined 1620 as starting with a non-local symbol). 1621 Note that, if there are no non-local symbols in a section, all the frags will 1622 be attached as one anonymous subsection. */ 1623 1624 static void 1625 obj_mach_o_set_subsections (bfd *abfd ATTRIBUTE_UNUSED, 1626 asection *sec, 1627 void *unused ATTRIBUTE_UNUSED) 1628 { 1629 segment_info_type *seginfo = seg_info (sec); 1630 symbolS *cur_subsection = NULL; 1631 struct obj_mach_o_symbol_data *cur_subsection_data = NULL; 1632 fragS *frag; 1633 frchainS *chain; 1634 1635 /* Protect against sections not created by gas. */ 1636 if (seginfo == NULL) 1637 return; 1638 1639 /* Attach every frag to a subsection. */ 1640 for (chain = seginfo->frchainP; chain != NULL; chain = chain->frch_next) 1641 for (frag = chain->frch_root; frag != NULL; frag = frag->fr_next) 1642 { 1643 if (frag->obj_frag_data.subsection == NULL) 1644 frag->obj_frag_data.subsection = cur_subsection; 1645 else 1646 { 1647 cur_subsection = frag->obj_frag_data.subsection; 1648 cur_subsection_data = symbol_get_obj (cur_subsection); 1649 cur_subsection_data->subsection_size = 0; 1650 } 1651 if (cur_subsection_data != NULL) 1652 { 1653 /* Update subsection size. */ 1654 cur_subsection_data->subsection_size += frag->fr_fix; 1655 } 1656 } 1657 } 1658 1659 /* Handle mach-o subsections-via-symbols counting up frags belonging to each 1660 sub-section. */ 1661 1662 void 1663 obj_mach_o_pre_relax_hook (void) 1664 { 1665 bfd_map_over_sections (stdoutput, obj_mach_o_set_subsections, (char *) 0); 1666 } 1667 1668 /* Zerofill and GB Zerofill sections must be sorted to follow all other 1669 sections in their segments. 1670 1671 The native 'as' leaves the sections physically in the order they appear in 1672 the source, and adjusts the section VMAs to meet the constraint. 1673 1674 We follow this for now - if nothing else, it makes comparison easier. 1675 1676 An alternative implementation would be to sort the sections as ld requires. 1677 It might be advantageous to implement such a scheme in the future (or even 1678 to make the style of section ordering user-selectable). */ 1679 1680 typedef struct obj_mach_o_set_vma_data 1681 { 1682 bfd_vma vma; 1683 unsigned vma_pass; 1684 unsigned zerofill_seen; 1685 unsigned gb_zerofill_seen; 1686 } obj_mach_o_set_vma_data; 1687 1688 /* We do (possibly) three passes through to set the vma, so that: 1689 1690 zerofill sections get VMAs after all others in their segment 1691 GB zerofill get VMAs last. 1692 1693 As we go, we notice if we see any Zerofill or GB Zerofill sections, so that 1694 we can skip the additional passes if there's nothing to do. */ 1695 1696 static void 1697 obj_mach_o_set_section_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *v_p) 1698 { 1699 bfd_mach_o_section *ms = bfd_mach_o_get_mach_o_section (sec); 1700 unsigned bfd_align = bfd_section_alignment (sec); 1701 obj_mach_o_set_vma_data *p = (struct obj_mach_o_set_vma_data *)v_p; 1702 unsigned sectype = (ms->flags & BFD_MACH_O_SECTION_TYPE_MASK); 1703 unsigned zf; 1704 1705 zf = 0; 1706 if (sectype == BFD_MACH_O_S_ZEROFILL) 1707 { 1708 zf = 1; 1709 p->zerofill_seen = zf; 1710 } 1711 else if (sectype == BFD_MACH_O_S_GB_ZEROFILL) 1712 { 1713 zf = 2; 1714 p->gb_zerofill_seen = zf; 1715 } 1716 1717 if (p->vma_pass != zf) 1718 return; 1719 1720 /* We know the section size now - so make a vma for the section just 1721 based on order. */ 1722 ms->size = bfd_section_size (sec); 1723 1724 /* Make sure that the align agrees, and set to the largest value chosen. */ 1725 ms->align = ms->align > bfd_align ? ms->align : bfd_align; 1726 bfd_set_section_alignment (sec, ms->align); 1727 1728 p->vma += (1 << ms->align) - 1; 1729 p->vma &= ~((1 << ms->align) - 1); 1730 ms->addr = p->vma; 1731 bfd_set_section_vma (sec, p->vma); 1732 p->vma += ms->size; 1733 } 1734 1735 /* (potentially) three passes over the sections, setting VMA. We skip the 1736 {gb}zerofill passes if we didn't see any of the relevant sections. */ 1737 1738 void obj_mach_o_post_relax_hook (void) 1739 { 1740 obj_mach_o_set_vma_data d; 1741 1742 memset (&d, 0, sizeof (d)); 1743 1744 bfd_map_over_sections (stdoutput, obj_mach_o_set_section_vma, (char *) &d); 1745 if ((d.vma_pass = d.zerofill_seen) != 0) 1746 bfd_map_over_sections (stdoutput, obj_mach_o_set_section_vma, (char *) &d); 1747 if ((d.vma_pass = d.gb_zerofill_seen) != 0) 1748 bfd_map_over_sections (stdoutput, obj_mach_o_set_section_vma, (char *) &d); 1749 } 1750 1751 static void 1752 obj_mach_o_set_indirect_symbols (bfd *abfd, asection *sec, 1753 void *xxx ATTRIBUTE_UNUSED) 1754 { 1755 bfd_vma sect_size = bfd_section_size (sec); 1756 bfd_mach_o_section *ms = bfd_mach_o_get_mach_o_section (sec); 1757 unsigned lazy = 0; 1758 1759 /* See if we have any indirect syms to consider. */ 1760 if (indirect_syms == NULL) 1761 return; 1762 1763 /* Process indirect symbols. 1764 Check for errors, if OK attach them as a flat array to the section 1765 for which they are defined. */ 1766 1767 switch (ms->flags & BFD_MACH_O_SECTION_TYPE_MASK) 1768 { 1769 case BFD_MACH_O_S_SYMBOL_STUBS: 1770 case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS: 1771 lazy = LAZY; 1772 /* Fall through. */ 1773 case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS: 1774 { 1775 unsigned int nactual = 0; 1776 unsigned int ncalc; 1777 obj_mach_o_indirect_sym *isym; 1778 obj_mach_o_indirect_sym *list = NULL; 1779 obj_mach_o_indirect_sym *list_tail = NULL; 1780 unsigned long eltsiz = 1781 bfd_mach_o_section_get_entry_size (abfd, ms); 1782 1783 for (isym = indirect_syms; isym != NULL; isym = isym->next) 1784 { 1785 if (isym->sect == sec) 1786 { 1787 nactual++; 1788 if (list == NULL) 1789 list = isym; 1790 else 1791 list_tail->next = isym; 1792 list_tail = isym; 1793 } 1794 } 1795 1796 /* If none are in this section, stop here. */ 1797 if (nactual == 0) 1798 break; 1799 1800 /* If we somehow added indirect symbols to a section with a zero 1801 entry size, we're dead ... */ 1802 gas_assert (eltsiz != 0); 1803 1804 ncalc = (unsigned int) (sect_size / eltsiz); 1805 if (nactual != ncalc) 1806 as_bad (_("the number of .indirect_symbols defined in section %s" 1807 " does not match the number expected (%d defined, %d" 1808 " expected)"), sec->name, nactual, ncalc); 1809 else 1810 { 1811 unsigned n; 1812 bfd_mach_o_asymbol *sym; 1813 1814 /* FIXME: It seems that there can be more indirect symbols 1815 than is computed by the loop above. So be paranoid and 1816 allocate enough space for every symbol to be indirect. 1817 See PR 21939 for an example of where this is needed. */ 1818 if (nactual < bfd_get_symcount (abfd)) 1819 nactual = bfd_get_symcount (abfd); 1820 1821 ms->indirect_syms = 1822 bfd_zalloc (abfd, 1823 nactual * sizeof (bfd_mach_o_asymbol *)); 1824 1825 if (ms->indirect_syms == NULL) 1826 as_fatal (_("internal error: failed to allocate %d indirect" 1827 "symbol pointers"), nactual); 1828 1829 for (isym = list, n = 0; isym != NULL; isym = isym->next, n++) 1830 { 1831 sym = (bfd_mach_o_asymbol *)symbol_get_bfdsym (isym->sym); 1832 /* Array is init to NULL & NULL signals a local symbol 1833 If the section is lazy-bound, we need to keep the 1834 reference to the symbol, since dyld can override. 1835 1836 Absolute symbols are handled specially. */ 1837 if (sym->symbol.section == bfd_abs_section_ptr) 1838 { 1839 if (n >= nactual) 1840 as_fatal (_("internal error: more indirect mach-o symbols than expected")); 1841 ms->indirect_syms[n] = sym; 1842 } 1843 else if (S_IS_LOCAL (isym->sym) && ! lazy) 1844 ; 1845 else 1846 { 1847 if (sym == NULL) 1848 ; 1849 /* If the symbols is external ... */ 1850 else if (S_IS_EXTERNAL (isym->sym) 1851 || (sym->n_type & BFD_MACH_O_N_EXT) 1852 || ! S_IS_DEFINED (isym->sym) 1853 || lazy) 1854 { 1855 sym->n_desc &= ~LAZY; 1856 /* ... it can be lazy, if not defined or hidden. */ 1857 if ((sym->n_type & BFD_MACH_O_N_TYPE) 1858 == BFD_MACH_O_N_UNDF 1859 && ! (sym->n_type & BFD_MACH_O_N_PEXT) 1860 && (sym->n_type & BFD_MACH_O_N_EXT)) 1861 sym->n_desc |= lazy; 1862 if (n >= nactual) 1863 as_fatal (_("internal error: more indirect mach-o symbols than expected")); 1864 ms->indirect_syms[n] = sym; 1865 } 1866 } 1867 } 1868 } 1869 } 1870 break; 1871 1872 default: 1873 break; 1874 } 1875 } 1876 1877 /* The process of relocation could alter what's externally visible, thus we 1878 leave setting the indirect symbols until last. */ 1879 1880 void 1881 obj_mach_o_frob_file_after_relocs (void) 1882 { 1883 bfd_map_over_sections (stdoutput, obj_mach_o_set_indirect_symbols, (char *) 0); 1884 } 1885 1886 /* Reverse relocations order to make ld happy. */ 1887 1888 void 1889 obj_mach_o_reorder_section_relocs (asection *sec, arelent **rels, unsigned int n) 1890 { 1891 unsigned int i; 1892 unsigned int max = n / 2; 1893 1894 for (i = 0; i < max; i++) 1895 { 1896 arelent *r = rels[i]; 1897 rels[i] = rels[n - i - 1]; 1898 rels[n - i - 1] = r; 1899 } 1900 bfd_set_reloc (stdoutput, sec, rels, n); 1901 } 1902 1903 /* Relocation rules are different in frame sections. */ 1904 1905 static int 1906 obj_mach_o_is_frame_section (segT sec) 1907 { 1908 int l; 1909 l = strlen (segment_name (sec)); 1910 if ((l == 9 && startswith (segment_name (sec), ".eh_frame")) 1911 || (l == 12 && startswith (segment_name (sec), ".debug_frame"))) 1912 return 1; 1913 return 0; 1914 } 1915 1916 /* Unless we're in a frame section, we need to force relocs to be generated for 1917 local subtractions. We might eliminate them later (if they are within the 1918 same sub-section) but we don't know that at the point that this decision is 1919 being made. */ 1920 1921 int 1922 obj_mach_o_allow_local_subtract (expressionS * left ATTRIBUTE_UNUSED, 1923 expressionS * right ATTRIBUTE_UNUSED, 1924 segT seg) 1925 { 1926 /* Don't interfere if it's one of the GAS internal sections. */ 1927 if (! SEG_NORMAL (seg)) 1928 return 1; 1929 1930 /* Allow in frame sections, otherwise emit a reloc. */ 1931 return obj_mach_o_is_frame_section (seg); 1932 } 1933 1934 int 1935 obj_mach_o_in_different_subsection (symbolS *a, symbolS *b) 1936 { 1937 fragS *fa; 1938 fragS *fb; 1939 1940 if (S_GET_SEGMENT (a) != S_GET_SEGMENT (b) 1941 || !S_IS_DEFINED (a) 1942 || !S_IS_DEFINED (b)) 1943 { 1944 /* Not in the same segment, or undefined symbol. */ 1945 return 1; 1946 } 1947 1948 fa = symbol_get_frag (a); 1949 fb = symbol_get_frag (b); 1950 if (fa == NULL || fb == NULL) 1951 { 1952 /* One of the symbols is not in a subsection. */ 1953 return 1; 1954 } 1955 1956 return fa->obj_frag_data.subsection != fb->obj_frag_data.subsection; 1957 } 1958 1959 int 1960 obj_mach_o_force_reloc_sub_same (fixS *fix, segT seg) 1961 { 1962 if (! SEG_NORMAL (seg)) 1963 return 1; 1964 return obj_mach_o_in_different_subsection (fix->fx_addsy, fix->fx_subsy); 1965 } 1966 1967 int 1968 obj_mach_o_force_reloc_sub_local (fixS *fix, segT seg ATTRIBUTE_UNUSED) 1969 { 1970 return obj_mach_o_in_different_subsection (fix->fx_addsy, fix->fx_subsy); 1971 } 1972 1973 int 1974 obj_mach_o_force_reloc (fixS *fix) 1975 { 1976 if (generic_force_reloc (fix)) 1977 return 1; 1978 1979 /* Force a reloc if the target is not in the same subsection. 1980 FIXME: handle (a - b) where a and b belongs to the same subsection ? */ 1981 if (fix->fx_addsy != NULL) 1982 { 1983 symbolS *subsec = fix->fx_frag->obj_frag_data.subsection; 1984 symbolS *targ = fix->fx_addsy; 1985 1986 /* There might be no subsections at all. */ 1987 if (subsec == NULL) 1988 return 0; 1989 1990 if (S_GET_SEGMENT (targ) == absolute_section) 1991 return 0; 1992 1993 return obj_mach_o_in_different_subsection (targ, subsec); 1994 } 1995 return 0; 1996 } 1997