1 /* Mach-O object file format
2 Copyright 2009, 2011, 2012 Free Software Foundation, Inc.
4 This file is part of GAS, the GNU Assembler.
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.
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.
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
21 /* Here we handle the mach-o directives that are common to all architectures.
23 Most significant are mach-o named sections and a variety of symbol type
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.
31 The well-known as short-hand section switch directives like .text, .data
32 etc. are mapped onto predefined segment/section pairs using facilites
33 supplied by the mach-o port of bfd.
35 A number of additional mach-o short-hand section switch directives are
38 #define OBJ_HEADER "obj-macho.h"
45 #include "mach-o/loader.h"
46 #include "obj-macho.h"
51 static segT obj_mach_o_segT_from_bfd_name (const char *, int);
53 /* TODO: Implement "-dynamic"/"-static" command line options. */
55 static int obj_mach_o_is_static;
57 /* TODO: Implement the "-n" command line option to suppress the initial
58 switch to the text segment. */
59 static int obj_mach_o_start_with_text_section = 1;
61 /* Allow for special re-ordering on output. */
63 static int obj_mach_o_seen_objc_section;
65 /* Start-up: At present, just create the sections we want. */
69 /* Mach-O only defines the .text section by default, and even this can
70 be suppressed by a flag. In the latter event, the first code MUST
71 be a section definition. */
72 if (obj_mach_o_start_with_text_section)
74 text_section = obj_mach_o_segT_from_bfd_name (TEXT_SECTION_NAME, 1);
75 subseg_set (text_section, 0);
76 if (obj_mach_o_is_static)
78 bfd_mach_o_section *mo_sec
79 = bfd_mach_o_get_mach_o_section (text_section);
80 mo_sec->flags &= ~BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS;
85 /* Remember the subsections_by_symbols state in case we need to reset
88 static int obj_mach_o_subsections_by_symbols;
90 /* This will put at most 16 characters (terminated by a ',' or newline) from
91 the input stream into dest. If there are more than 16 chars before the
92 delimiter, a warning is given and the string is truncated. On completion of
93 this function, input_line_pointer will point to the char after the ',' or
96 It trims leading and trailing space. */
99 collect_16char_name (char *dest, const char *msg, int require_comma)
104 namstart = input_line_pointer;
106 while ( (c = *input_line_pointer) != ','
107 && !is_end_of_line[(unsigned char) c])
108 input_line_pointer++;
111 int len = input_line_pointer - namstart; /* could be zero. */
112 /* lose any trailing space. */
113 while (len > 0 && namstart[len-1] == ' ')
117 *input_line_pointer = '\0'; /* make a temp string. */
118 as_bad (_("the %s name '%s' is too long (maximum 16 characters)"),
120 *input_line_pointer = c; /* restore for printing. */
124 memcpy (dest, namstart, len);
127 if (c != ',' && require_comma)
129 as_bad (_("expected a %s name followed by a `,'"), msg);
137 obj_mach_o_get_section_names (char *seg, char *sec,
138 unsigned segl, unsigned secl)
140 /* Zero-length segment and section names are allowed. */
141 /* Parse segment name. */
142 memset (seg, 0, segl);
143 if (collect_16char_name (seg, "segment", 1))
145 ignore_rest_of_line ();
148 input_line_pointer++; /* Skip the terminating ',' */
150 /* Parse section name, which can be empty. */
151 memset (sec, 0, secl);
152 collect_16char_name (sec, "section", 0);
156 /* Build (or get) a section from the mach-o description - which includes
157 optional definitions for type, attributes, alignment and stub size.
159 BFD supplies default values for sections which have a canonical name. */
161 #define SECT_TYPE_SPECIFIED 0x0001
162 #define SECT_ATTR_SPECIFIED 0x0002
163 #define SECT_ALGN_SPECIFIED 0x0004
164 #define SECT_STUB_SPECIFIED 0x0008
167 obj_mach_o_make_or_get_sect (char * segname, char * sectname,
168 unsigned int specified_mask,
169 unsigned int usectype, unsigned int usecattr,
170 unsigned int ualign, offsetT stub_size)
172 unsigned int sectype, secattr, secalign;
173 flagword oldflags, flags;
176 bfd_mach_o_section *msect;
177 const mach_o_section_name_xlat *xlat;
179 /* This provides default bfd flags and default mach-o section type and
180 attributes along with the canonical name. */
181 xlat = bfd_mach_o_section_data_for_mach_sect (stdoutput, segname, sectname);
183 /* TODO: more checking of whether overides are acually allowed. */
187 name = xstrdup (xlat->bfd_name);
188 sectype = xlat->macho_sectype;
189 if (specified_mask & SECT_TYPE_SPECIFIED)
191 if ((sectype == BFD_MACH_O_S_ZEROFILL
192 || sectype == BFD_MACH_O_S_GB_ZEROFILL)
193 && sectype != usectype)
194 as_bad (_("cannot overide zerofill section type for `%s,%s'"),
199 secattr = xlat->macho_secattr;
200 secalign = xlat->sectalign;
201 flags = xlat->bfd_flags;
205 /* There is no normal BFD section name for this section. Create one.
206 The name created doesn't really matter as it will never be written
208 size_t seglen = strlen (segname);
209 size_t sectlen = strlen (sectname);
212 n = xmalloc (seglen + 1 + sectlen + 1);
213 memcpy (n, segname, seglen);
215 memcpy (n + seglen + 1, sectname, sectlen);
216 n[seglen + 1 + sectlen] = 0;
218 if (specified_mask & SECT_TYPE_SPECIFIED)
221 sectype = BFD_MACH_O_S_REGULAR;
222 secattr = BFD_MACH_O_S_ATTR_NONE;
224 flags = SEC_NO_FLAGS;
227 /* For now, just use what the user provided. */
229 if (specified_mask & SECT_ATTR_SPECIFIED)
232 if (specified_mask & SECT_ALGN_SPECIFIED)
235 /* Sub-segments don't exists as is on Mach-O. */
236 sec = subseg_new (name, 0);
238 oldflags = bfd_get_section_flags (stdoutput, sec);
239 msect = bfd_mach_o_get_mach_o_section (sec);
241 if (oldflags == SEC_NO_FLAGS)
243 /* New, so just use the defaults or what's specified. */
244 if (! bfd_set_section_flags (stdoutput, sec, flags))
245 as_warn (_("failed to set flags for \"%s\": %s"),
246 bfd_section_name (stdoutput, sec),
247 bfd_errmsg (bfd_get_error ()));
249 strncpy (msect->segname, segname, sizeof (msect->segname));
250 strncpy (msect->sectname, sectname, sizeof (msect->sectname));
252 msect->align = secalign;
253 msect->flags = sectype | secattr;
255 if (sectype == BFD_MACH_O_S_ZEROFILL
256 || sectype == BFD_MACH_O_S_GB_ZEROFILL)
257 seg_info (sec)->bss = 1;
259 else if (flags != SEC_NO_FLAGS)
261 if (flags != oldflags
262 || msect->flags != (secattr | sectype))
263 as_warn (_("Ignoring changed section attributes for %s"), name);
266 if (specified_mask & SECT_STUB_SPECIFIED)
267 /* At present, the stub size is not supplied from the BFD tables. */
268 msect->reserved2 = stub_size;
275 The '.section' specification syntax looks like:
276 .section <segment> , <section> [, type [, attribs [, size]]]
278 White space is allowed everywhere between elements.
280 <segment> and <section> may be from 0 to 16 chars in length - they may
281 contain spaces but leading and trailing space will be trimmed. It is
282 mandatory that they be present (or that zero-length names are indicated
285 There is only a single section type for any entry.
287 There may be multiple attributes, they are delimited by `+'.
289 Not all section types and attributes are accepted by the Darwin system
290 assemblers as user-specifiable - although, at present, we do here. */
293 obj_mach_o_section (int ignore ATTRIBUTE_UNUSED)
295 unsigned int sectype = BFD_MACH_O_S_REGULAR;
296 unsigned int specified_mask = 0;
297 unsigned int secattr = 0;
298 offsetT sizeof_stub = 0;
303 #ifdef md_flush_pending_output
304 md_flush_pending_output ();
307 /* Get the User's segment annd section names. */
308 if (! obj_mach_o_get_section_names (segname, sectname, 17, 17))
311 /* Parse section type, if present. */
312 if (*input_line_pointer == ',')
318 input_line_pointer++;
320 p = input_line_pointer;
321 while ((c = *input_line_pointer) != ','
322 && !is_end_of_line[(unsigned char) c])
323 input_line_pointer++;
325 len = input_line_pointer - p;
326 /* strip trailing spaces. */
327 while (len > 0 && p[len-1] == ' ')
331 /* Temporarily make a string from the token. */
333 sectype = bfd_mach_o_get_section_type_from_name (stdoutput, p);
334 if (sectype > 255) /* Max Section ID == 255. */
336 as_bad (_("unknown or invalid section type '%s'"), p);
338 ignore_rest_of_line ();
342 specified_mask |= SECT_TYPE_SPECIFIED;
347 TODO: check validity of attributes for section type. */
348 if ((specified_mask & SECT_TYPE_SPECIFIED)
355 /* Skip initial `,' and subsequent `+'. */
356 input_line_pointer++;
358 p = input_line_pointer;
359 while ((c = *input_line_pointer) != '+'
361 && !is_end_of_line[(unsigned char) c])
362 input_line_pointer++;
364 len = input_line_pointer - p;
365 /* strip trailing spaces. */
366 while (len > 0 && p[len-1] == ' ')
370 /* Temporarily make a string from the token. */
372 attr = bfd_mach_o_get_section_attribute_from_name (p);
375 as_bad (_("unknown or invalid section attribute '%s'"), p);
377 ignore_rest_of_line ();
382 specified_mask |= SECT_ATTR_SPECIFIED;
388 while (*input_line_pointer == '+');
390 /* Parse sizeof_stub. */
391 if ((specified_mask & SECT_ATTR_SPECIFIED)
392 && *input_line_pointer == ',')
394 if (sectype != BFD_MACH_O_S_SYMBOL_STUBS)
396 as_bad (_("unexpected section size information"));
397 ignore_rest_of_line ();
401 input_line_pointer++;
402 sizeof_stub = get_absolute_expression ();
403 specified_mask |= SECT_STUB_SPECIFIED;
405 else if ((specified_mask & SECT_ATTR_SPECIFIED)
406 && sectype == BFD_MACH_O_S_SYMBOL_STUBS)
408 as_bad (_("missing sizeof_stub expression"));
409 ignore_rest_of_line ();
415 new_seg = obj_mach_o_make_or_get_sect (segname, sectname, specified_mask,
416 sectype, secattr, 0 /*align */,
420 subseg_set (new_seg, 0);
421 demand_empty_rest_of_line ();
425 /* .zerofill segname, sectname [, symbolname, size [, align]]
427 Zerofill switches, temporarily, to a sect of type 'zerofill'.
429 If a variable name is given, it defines that in the section.
430 Otherwise it just creates the section if it doesn't exist. */
433 obj_mach_o_zerofill (int ignore ATTRIBUTE_UNUSED)
437 segT old_seg = now_seg;
440 unsigned int align = 0;
441 unsigned int specified_mask = 0;
444 #ifdef md_flush_pending_output
445 md_flush_pending_output ();
448 /* Get the User's segment annd section names. */
449 if (! obj_mach_o_get_section_names (segname, sectname, 17, 17))
452 /* Parse variable definition, if present. */
453 if (*input_line_pointer == ',')
455 /* Parse symbol, size [.align]
456 We follow the method of s_common_internal, with the difference
457 that the symbol cannot be a duplicate-common. */
463 input_line_pointer++; /* Skip ',' */
465 name = input_line_pointer;
466 c = get_symbol_end ();
467 /* Just after name is now '\0'. */
468 p = input_line_pointer;
473 as_bad (_("expected symbol name"));
474 ignore_rest_of_line ();
479 if (*input_line_pointer == ',')
480 input_line_pointer++;
482 expression_and_evaluate (&exp);
483 if (exp.X_op != O_constant
484 && exp.X_op != O_absent)
486 as_bad (_("bad or irreducible absolute expression"));
487 ignore_rest_of_line ();
490 else if (exp.X_op == O_absent)
492 as_bad (_("missing size expression"));
493 ignore_rest_of_line ();
497 size = exp.X_add_number;
498 size &= ((offsetT) 2 << (stdoutput->arch_info->bits_per_address - 1)) - 1;
499 if (exp.X_add_number != size || !exp.X_unsigned)
501 as_warn (_("size (%ld) out of range, ignored"),
502 (long) exp.X_add_number);
503 ignore_rest_of_line ();
507 *p = 0; /* Make the name into a c string for err messages. */
508 sym = symbol_find_or_make (name);
509 if (S_IS_DEFINED (sym) || symbol_equated_p (sym))
511 as_bad (_("symbol `%s' is already defined"), name);
513 ignore_rest_of_line ();
517 size = S_GET_VALUE (sym);
519 size = exp.X_add_number;
520 else if (size != exp.X_add_number)
521 as_warn (_("size of \"%s\" is already %ld; not changing to %ld"),
522 name, (long) size, (long) exp.X_add_number);
524 *p = c; /* Restore the termination char. */
527 if (*input_line_pointer == ',')
529 align = (unsigned int) parse_align (0);
530 if (align == (unsigned int) -1)
532 as_warn (_("align value not recognized, using size"));
537 as_warn (_("Alignment (%lu) too large: 15 assumed."),
538 (unsigned long)align);
541 specified_mask |= SECT_ALGN_SPECIFIED;
544 /* else just a section definition. */
546 specified_mask |= SECT_TYPE_SPECIFIED;
547 new_seg = obj_mach_o_make_or_get_sect (segname, sectname, specified_mask,
548 BFD_MACH_O_S_ZEROFILL,
549 BFD_MACH_O_S_ATTR_NONE,
550 align, (offsetT) 0 /*stub size*/);
554 /* In case the user specifies the bss section by mach-o name.
555 Create it on demand */
556 if (strcmp (new_seg->name, BSS_SECTION_NAME) == 0
557 && bss_section == NULL)
558 bss_section = new_seg;
560 subseg_set (new_seg, 0);
568 record_alignment (new_seg, align);
569 frag_align (align, 0, 0);
572 /* Detach from old frag. */
573 if (S_GET_SEGMENT (sym) == new_seg)
574 symbol_get_frag (sym)->fr_symbol = NULL;
576 symbol_set_frag (sym, frag_now);
577 pfrag = frag_var (rs_org, 1, 1, 0, sym, size, NULL);
580 S_SET_SEGMENT (sym, new_seg);
581 if (new_seg == bss_section)
582 S_CLEAR_EXTERNAL (sym);
586 /* switch back to the section that was current before the .zerofill. */
587 subseg_set (old_seg, 0);
591 obj_mach_o_segT_from_bfd_name (const char *nam, int must_succeed)
593 const mach_o_section_name_xlat *xlat;
597 /* BFD has tables of flags and default attributes for all the sections that
598 have a 'canonical' name. */
599 xlat = bfd_mach_o_section_data_for_bfd_name (stdoutput, nam, &segn);
603 as_fatal (_("BFD is out of sync with GAS, "
604 "unhandled well-known section type `%s'"), nam);
608 sec = bfd_get_section_by_name (stdoutput, nam);
611 bfd_mach_o_section *msect;
613 sec = subseg_force_new (xlat->bfd_name, 0);
615 /* Set default type, attributes and alignment. */
616 msect = bfd_mach_o_get_mach_o_section (sec);
617 msect->flags = xlat->macho_sectype | xlat->macho_secattr;
618 msect->align = xlat->sectalign;
620 if ((msect->flags & BFD_MACH_O_SECTION_TYPE_MASK)
621 == BFD_MACH_O_S_ZEROFILL)
622 seg_info (sec)->bss = 1;
628 static const char * const known_sections[] =
633 /* 2 */ ".static_const",
637 /* 6 */ ".literal16",
638 /* 7 */ ".constructor",
639 /* 8 */ ".destructor",
642 /* 10 */ ".const_data",
643 /* 11 */ ".static_data",
644 /* 12 */ ".mod_init_func",
645 /* 13 */ ".mod_term_func",
650 /* Interface for a known non-optional section directive. */
653 obj_mach_o_known_section (int sect_index)
657 #ifdef md_flush_pending_output
658 md_flush_pending_output ();
661 section = obj_mach_o_segT_from_bfd_name (known_sections[sect_index], 1);
663 subseg_set (section, 0);
665 /* else, we leave the section as it was; there was a fatal error anyway. */
668 static const char * const objc_sections[] =
671 /* 1 */ ".objc_class",
672 /* 2 */ ".objc_meta_class",
673 /* 3 */ ".objc_cat_cls_meth",
674 /* 4 */ ".objc_cat_inst_meth",
675 /* 5 */ ".objc_protocol",
676 /* 6 */ ".objc_string_object",
677 /* 7 */ ".objc_cls_meth",
678 /* 8 */ ".objc_inst_meth",
679 /* 9 */ ".objc_cls_refs",
680 /* 10 */ ".objc_message_refs",
681 /* 11 */ ".objc_symbols",
682 /* 12 */ ".objc_category",
683 /* 13 */ ".objc_class_vars",
684 /* 14 */ ".objc_instance_vars",
685 /* 15 */ ".objc_module_info",
686 /* 16 */ ".cstring", /* objc_class_names Alias for .cstring */
687 /* 17 */ ".cstring", /* Alias objc_meth_var_types for .cstring */
688 /* 18 */ ".cstring", /* objc_meth_var_names Alias for .cstring */
689 /* 19 */ ".objc_selector_strs",
690 /* 20 */ ".objc_image_info", /* extension. */
691 /* 21 */ ".objc_selector_fixup", /* extension. */
692 /* 22 */ ".objc1_class_ext", /* ObjC-1 extension. */
693 /* 23 */ ".objc1_property_list", /* ObjC-1 extension. */
694 /* 24 */ ".objc1_protocol_ext" /* ObjC-1 extension. */
697 /* This currently does the same as known_sections, but kept separate for
698 ease of maintenance. */
701 obj_mach_o_objc_section (int sect_index)
705 #ifdef md_flush_pending_output
706 md_flush_pending_output ();
709 section = obj_mach_o_segT_from_bfd_name (objc_sections[sect_index], 1);
712 obj_mach_o_seen_objc_section = 1; /* We need to ensure that certain
713 sections are present and in the
715 subseg_set (section, 0);
718 /* else, we leave the section as it was; there was a fatal error anyway. */
721 /* Debug section directives. */
723 static const char * const debug_sections[] =
727 /* 1 */ ".debug_frame",
728 /* 2 */ ".debug_info",
729 /* 3 */ ".debug_abbrev",
730 /* 4 */ ".debug_aranges",
731 /* 5 */ ".debug_macinfo",
732 /* 6 */ ".debug_line",
733 /* 7 */ ".debug_loc",
734 /* 8 */ ".debug_pubnames",
735 /* 9 */ ".debug_pubtypes",
736 /* 10 */ ".debug_str",
737 /* 11 */ ".debug_ranges",
738 /* 12 */ ".debug_macro"
741 /* ??? Maybe these should be conditional on gdwarf-*.
742 It`s also likely that we will need to be able to set them from the cfi
746 obj_mach_o_debug_section (int sect_index)
750 #ifdef md_flush_pending_output
751 md_flush_pending_output ();
754 section = obj_mach_o_segT_from_bfd_name (debug_sections[sect_index], 1);
756 subseg_set (section, 0);
758 /* else, we leave the section as it was; there was a fatal error anyway. */
761 /* This could be moved to the tc-xx files, but there is so little dependency
762 there, that the code might as well be shared. */
771 /* The extensions here are for specific sections that are generated by GCC
772 and Darwin system tools, but don't have directives in the `system as'. */
774 static const struct opt_tgt_sect tgt_sections[] =
776 /* 0 */ { NULL, 0, 0},
777 /* 1 */ { ".lazy_symbol_pointer", 0, 0},
778 /* 2 */ { ".lazy_symbol_pointer2", 0, 0}, /* X86 - extension */
779 /* 3 */ { ".lazy_symbol_pointer3", 0, 0}, /* X86 - extension */
780 /* 4 */ { ".non_lazy_symbol_pointer", 0, 0},
781 /* 5 */ { ".non_lazy_symbol_pointer_x86", 0, 0}, /* X86 - extension */
782 /* 6 */ { ".symbol_stub", 16, 20},
783 /* 7 */ { ".symbol_stub1", 0, 16}, /* PPC - extension */
784 /* 8 */ { ".picsymbol_stub", 26, 36},
785 /* 9 */ { ".picsymbol_stub1", 0, 32}, /* PPC - extension */
786 /* 10 */ { ".picsymbol_stub2", 25, 0}, /* X86 - extension */
787 /* 11 */ { ".picsymbol_stub3", 5, 0}, /* X86 - extension */
790 /* Interface for an optional section directive. */
793 obj_mach_o_opt_tgt_section (int sect_index)
795 const struct opt_tgt_sect *tgtsct = &tgt_sections[sect_index];
798 #ifdef md_flush_pending_output
799 md_flush_pending_output ();
802 section = obj_mach_o_segT_from_bfd_name (tgtsct->name, 0);
805 as_bad (_("%s is not used for the selected target"), tgtsct->name);
806 /* Leave the section as it is. */
810 bfd_mach_o_section *mo_sec = bfd_mach_o_get_mach_o_section (section);
811 subseg_set (section, 0);
812 #if defined (TC_I386)
813 mo_sec->reserved2 = tgtsct->x86_val;
814 #elif defined (TC_PPC)
815 mo_sec->reserved2 = tgtsct->ppc_val;
817 mo_sec->reserved2 = 0;
822 /* We don't necessarily have the three 'base' sections on mach-o.
823 Normally, we would start up with only the 'text' section defined.
824 However, even that can be suppressed with (TODO) c/l option "-n".
825 Thus, we have to be able to create all three sections on-demand. */
828 obj_mach_o_base_section (int sect_index)
832 #ifdef md_flush_pending_output
833 md_flush_pending_output ();
836 /* We don't support numeric (or any other) qualifications on the
837 well-known section shorthands. */
838 demand_empty_rest_of_line ();
842 /* Handle the three sections that are globally known within GAS.
843 For Mach-O, these are created on demand rather than at startup. */
845 if (text_section == NULL)
846 text_section = obj_mach_o_segT_from_bfd_name (TEXT_SECTION_NAME, 1);
847 if (obj_mach_o_is_static)
849 bfd_mach_o_section *mo_sec
850 = bfd_mach_o_get_mach_o_section (text_section);
851 mo_sec->flags &= ~BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS;
853 section = text_section;
856 if (data_section == NULL)
857 data_section = obj_mach_o_segT_from_bfd_name (DATA_SECTION_NAME, 1);
858 section = data_section;
861 /* ??? maybe this achieves very little, as an addition. */
862 if (bss_section == NULL)
864 bss_section = obj_mach_o_segT_from_bfd_name (BSS_SECTION_NAME, 1);
865 seg_info (bss_section)->bss = 1;
867 section = bss_section;
870 as_fatal (_("internal error: base section index out of range"));
874 subseg_set (section, 0);
877 /* This finishes off parsing a .comm or .lcomm statement, which both can have
878 an (optional) alignment field. It also allows us to create the bss section
882 obj_mach_o_common_parse (int is_local, symbolS *symbolP,
886 bfd_mach_o_asymbol *s;
890 /* Both comm and lcomm take an optional alignment, as a power
891 of two between 1 and 15. */
892 if (*input_line_pointer == ',')
894 /* We expect a power of 2. */
895 align = parse_align (0);
896 if (align == (addressT) -1)
900 as_warn (_("Alignment (%lu) too large: 15 assumed."),
901 (unsigned long)align);
906 s = (bfd_mach_o_asymbol *) symbol_get_bfdsym (symbolP);
909 /* Create the BSS section on demand. */
910 if (bss_section == NULL)
912 bss_section = obj_mach_o_segT_from_bfd_name (BSS_SECTION_NAME, 1);
913 seg_info (bss_section)->bss = 1;
915 bss_alloc (symbolP, size, align);
916 s->n_type = BFD_MACH_O_N_SECT;
917 S_CLEAR_EXTERNAL (symbolP);
921 S_SET_VALUE (symbolP, size);
922 S_SET_ALIGN (symbolP, align);
923 S_SET_EXTERNAL (symbolP);
924 S_SET_SEGMENT (symbolP, bfd_com_section_ptr);
925 s->n_type = BFD_MACH_O_N_UNDF | BFD_MACH_O_N_EXT;
928 /* This is a data object (whatever we choose that to mean). */
929 s->symbol.flags |= BSF_OBJECT;
931 /* We've set symbol qualifiers, so validate if you can. */
932 s->symbol.udata.i = SYM_MACHO_FIELDS_NOT_VALIDATED;
938 obj_mach_o_comm (int is_local)
940 s_comm_internal (is_local, obj_mach_o_common_parse);
943 /* Set properties that apply to the whole file. At present, the only
944 one defined, is subsections_via_symbols. */
946 typedef enum obj_mach_o_file_properties {
947 OBJ_MACH_O_FILE_PROP_NONE = 0,
948 OBJ_MACH_O_FILE_PROP_SUBSECTS_VIA_SYMS,
949 OBJ_MACH_O_FILE_PROP_MAX
950 } obj_mach_o_file_properties;
953 obj_mach_o_fileprop (int prop)
955 if (prop < 0 || prop >= OBJ_MACH_O_FILE_PROP_MAX)
956 as_fatal (_("internal error: bad file property ID %d"), prop);
958 switch ((obj_mach_o_file_properties) prop)
960 case OBJ_MACH_O_FILE_PROP_SUBSECTS_VIA_SYMS:
961 obj_mach_o_subsections_by_symbols = 1;
962 if (!bfd_set_private_flags (stdoutput,
963 BFD_MACH_O_MH_SUBSECTIONS_VIA_SYMBOLS))
964 as_bad (_("failed to set subsections by symbols"));
965 demand_empty_rest_of_line ();
972 /* Temporary markers for symbol reference data.
973 Lazy will remain in place. */
977 /* We have a bunch of qualifiers that may be applied to symbols.
978 .globl is handled here so that we might make sure that conflicting qualifiers
979 are caught where possible. */
981 typedef enum obj_mach_o_symbol_type {
982 OBJ_MACH_O_SYM_UNK = 0,
983 OBJ_MACH_O_SYM_LOCAL = 1,
984 OBJ_MACH_O_SYM_GLOBL = 2,
985 OBJ_MACH_O_SYM_REFERENCE = 3,
986 OBJ_MACH_O_SYM_WEAK_REF = 4,
987 OBJ_MACH_O_SYM_LAZY_REF = 5,
988 OBJ_MACH_O_SYM_WEAK_DEF = 6,
989 OBJ_MACH_O_SYM_PRIV_EXT = 7,
990 OBJ_MACH_O_SYM_NO_DEAD_STRIP = 8,
991 OBJ_MACH_O_SYM_WEAK = 9
992 } obj_mach_o_symbol_type;
994 /* Set Mach-O-specific symbol qualifiers. */
997 obj_mach_o_set_symbol_qualifier (symbolS *sym, int type)
1000 bfd_mach_o_asymbol *s = (bfd_mach_o_asymbol *) symbol_get_bfdsym (sym);
1001 bfd_mach_o_section *sec;
1005 /* If the symbol is defined, then we can do more rigorous checking on
1006 the validity of the qualifiers. Otherwise, we are stuck with waiting
1007 until it's defined - or until write the file.
1009 In certain cases (e.g. when a symbol qualifier is intended to introduce
1010 an undefined symbol in a stubs section) we should check that the current
1011 section is appropriate to the qualifier. */
1013 is_defined = s->symbol.section != bfd_und_section_ptr;
1015 sec = bfd_mach_o_get_mach_o_section (s->symbol.section) ;
1017 sec = bfd_mach_o_get_mach_o_section (now_seg) ;
1020 sectype = sec->flags & BFD_MACH_O_SECTION_TYPE_MASK;
1022 switch ((obj_mach_o_symbol_type) type)
1024 case OBJ_MACH_O_SYM_LOCAL:
1025 /* This is an extension over the system tools. */
1026 if (s->n_type & (BFD_MACH_O_N_PEXT | BFD_MACH_O_N_EXT))
1028 as_bad (_("'%s' previously declared as '%s'."), s->symbol.name,
1029 (s->n_type & BFD_MACH_O_N_PEXT) ? "private extern"
1035 s->n_type &= ~BFD_MACH_O_N_EXT;
1036 S_CLEAR_EXTERNAL (sym);
1040 case OBJ_MACH_O_SYM_PRIV_EXT:
1041 s->n_type |= BFD_MACH_O_N_PEXT ;
1042 s->n_desc &= ~LAZY; /* The native tool switches this off too. */
1043 /* We follow the system tools in marking PEXT as also global. */
1046 case OBJ_MACH_O_SYM_GLOBL:
1047 /* It's not an error to define a symbol and then make it global. */
1048 s->n_type |= BFD_MACH_O_N_EXT;
1049 S_SET_EXTERNAL (sym);
1052 case OBJ_MACH_O_SYM_REFERENCE:
1054 s->n_desc |= BFD_MACH_O_N_NO_DEAD_STRIP;
1056 s->n_desc |= (REFE | BFD_MACH_O_N_NO_DEAD_STRIP);
1059 case OBJ_MACH_O_SYM_LAZY_REF:
1061 s->n_desc |= BFD_MACH_O_N_NO_DEAD_STRIP;
1063 s->n_desc |= (REFE | LAZY | BFD_MACH_O_N_NO_DEAD_STRIP);
1066 /* Force ld to retain the symbol - even if it appears unused. */
1067 case OBJ_MACH_O_SYM_NO_DEAD_STRIP:
1068 s->n_desc |= BFD_MACH_O_N_NO_DEAD_STRIP ;
1071 /* Mach-O's idea of weak ... */
1072 case OBJ_MACH_O_SYM_WEAK_REF:
1073 s->n_desc |= BFD_MACH_O_N_WEAK_REF ;
1076 case OBJ_MACH_O_SYM_WEAK_DEF:
1077 if (is_defined && sectype != BFD_MACH_O_S_COALESCED)
1079 as_bad (_("'%s' can't be a weak_definition (currently only"
1080 " supported in sections of type coalesced)"),
1085 s->n_desc |= BFD_MACH_O_N_WEAK_DEF;
1088 case OBJ_MACH_O_SYM_WEAK:
1089 /* A generic 'weak' - we try to figure out what it means at
1090 symbol frob time. */
1098 /* We've seen some kind of qualifier - check validity if or when the entity
1100 s->symbol.udata.i = SYM_MACHO_FIELDS_NOT_VALIDATED;
1104 /* Respond to symbol qualifiers.
1106 .<qualifier> symbol [, symbol]*
1107 a list of symbols is an extension over the Darwin system as. */
1110 obj_mach_o_sym_qual (int ntype)
1116 #ifdef md_flush_pending_output
1117 md_flush_pending_output ();
1122 name = input_line_pointer;
1123 c = get_symbol_end ();
1124 symbolP = symbol_find_or_make (name);
1125 obj_mach_o_set_symbol_qualifier (symbolP, ntype);
1126 *input_line_pointer = c;
1128 c = *input_line_pointer;
1131 input_line_pointer++;
1133 if (is_end_of_line[(unsigned char) *input_line_pointer])
1139 demand_empty_rest_of_line ();
1142 typedef struct obj_mach_o_indirect_sym
1146 struct obj_mach_o_indirect_sym *next;
1147 } obj_mach_o_indirect_sym;
1149 /* We store in order an maintain a pointer to the last one - to save reversing
1151 obj_mach_o_indirect_sym *indirect_syms;
1152 obj_mach_o_indirect_sym *indirect_syms_tail;
1155 obj_mach_o_indirect_symbol (int arg ATTRIBUTE_UNUSED)
1157 bfd_mach_o_section *sec = bfd_mach_o_get_mach_o_section (now_seg);
1159 #ifdef md_flush_pending_output
1160 md_flush_pending_output ();
1163 if (obj_mach_o_is_static)
1164 as_bad (_("use of .indirect_symbols requires `-dynamic'"));
1166 switch (sec->flags & BFD_MACH_O_SECTION_TYPE_MASK)
1168 case BFD_MACH_O_S_SYMBOL_STUBS:
1169 case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS:
1170 case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS:
1172 obj_mach_o_indirect_sym *isym;
1173 char *name = input_line_pointer;
1174 char c = get_symbol_end ();
1175 symbolS *sym = symbol_find_or_make (name);
1176 unsigned int elsize =
1177 bfd_mach_o_section_get_entry_size (stdoutput, sec);
1181 as_bad (_("attempt to add an indirect_symbol to a stub or"
1182 " reference section with a zero-sized element at %s"),
1184 *input_line_pointer = c;
1185 ignore_rest_of_line ();
1188 *input_line_pointer = c;
1190 /* The indirect symbols are validated after the symbol table is
1191 frozen, we must make sure that if a local symbol is used as an
1192 indirect, it is promoted to a 'real' one. Fetching the bfd sym
1194 symbol_get_bfdsym (sym);
1195 isym = (obj_mach_o_indirect_sym *)
1196 xmalloc (sizeof (obj_mach_o_indirect_sym));
1198 /* Just record the data for now, we will validate it when we
1199 compute the output in obj_mach_o_set_indirect_symbols. */
1201 isym->sect = now_seg;
1203 if (indirect_syms == NULL)
1204 indirect_syms = isym;
1206 indirect_syms_tail->next = isym;
1207 indirect_syms_tail = isym;
1212 as_bad (_("an .indirect_symbol must be in a symbol pointer"
1213 " or stub section."));
1214 ignore_rest_of_line ();
1217 demand_empty_rest_of_line ();
1220 const pseudo_typeS mach_o_pseudo_table[] =
1222 /* Section directives. */
1223 { "comm", obj_mach_o_comm, 0 },
1224 { "lcomm", obj_mach_o_comm, 1 },
1226 { "text", obj_mach_o_base_section, 1},
1227 { "data", obj_mach_o_base_section, 2},
1228 { "bss", obj_mach_o_base_section, 3}, /* extension */
1230 { "const", obj_mach_o_known_section, 1},
1231 { "static_const", obj_mach_o_known_section, 2},
1232 { "cstring", obj_mach_o_known_section, 3},
1233 { "literal4", obj_mach_o_known_section, 4},
1234 { "literal8", obj_mach_o_known_section, 5},
1235 { "literal16", obj_mach_o_known_section, 6},
1236 { "constructor", obj_mach_o_known_section, 7},
1237 { "destructor", obj_mach_o_known_section, 8},
1238 { "eh_frame", obj_mach_o_known_section, 9},
1240 { "const_data", obj_mach_o_known_section, 10},
1241 { "static_data", obj_mach_o_known_section, 11},
1242 { "mod_init_func", obj_mach_o_known_section, 12},
1243 { "mod_term_func", obj_mach_o_known_section, 13},
1244 { "dyld", obj_mach_o_known_section, 14},
1245 { "cfstring", obj_mach_o_known_section, 15},
1247 { "objc_class", obj_mach_o_objc_section, 1},
1248 { "objc_meta_class", obj_mach_o_objc_section, 2},
1249 { "objc_cat_cls_meth", obj_mach_o_objc_section, 3},
1250 { "objc_cat_inst_meth", obj_mach_o_objc_section, 4},
1251 { "objc_protocol", obj_mach_o_objc_section, 5},
1252 { "objc_string_object", obj_mach_o_objc_section, 6},
1253 { "objc_cls_meth", obj_mach_o_objc_section, 7},
1254 { "objc_inst_meth", obj_mach_o_objc_section, 8},
1255 { "objc_cls_refs", obj_mach_o_objc_section, 9},
1256 { "objc_message_refs", obj_mach_o_objc_section, 10},
1257 { "objc_symbols", obj_mach_o_objc_section, 11},
1258 { "objc_category", obj_mach_o_objc_section, 12},
1259 { "objc_class_vars", obj_mach_o_objc_section, 13},
1260 { "objc_instance_vars", obj_mach_o_objc_section, 14},
1261 { "objc_module_info", obj_mach_o_objc_section, 15},
1262 { "objc_class_names", obj_mach_o_objc_section, 16}, /* Alias for .cstring */
1263 { "objc_meth_var_types", obj_mach_o_objc_section, 17}, /* Alias for .cstring */
1264 { "objc_meth_var_names", obj_mach_o_objc_section, 18}, /* Alias for .cstring */
1265 { "objc_selector_strs", obj_mach_o_objc_section, 19},
1266 { "objc_image_info", obj_mach_o_objc_section, 20}, /* extension. */
1267 { "objc_selector_fixup", obj_mach_o_objc_section, 21}, /* extension. */
1268 { "objc1_class_ext", obj_mach_o_objc_section, 22}, /* ObjC-1 extension. */
1269 { "objc1_property_list", obj_mach_o_objc_section, 23}, /* ObjC-1 extension. */
1270 { "objc1_protocol_ext", obj_mach_o_objc_section, 24}, /* ObjC-1 extension. */
1272 { "debug_frame", obj_mach_o_debug_section, 1}, /* extension. */
1273 { "debug_info", obj_mach_o_debug_section, 2}, /* extension. */
1274 { "debug_abbrev", obj_mach_o_debug_section, 3}, /* extension. */
1275 { "debug_aranges", obj_mach_o_debug_section, 4}, /* extension. */
1276 { "debug_macinfo", obj_mach_o_debug_section, 5}, /* extension. */
1277 { "debug_line", obj_mach_o_debug_section, 6}, /* extension. */
1278 { "debug_loc", obj_mach_o_debug_section, 7}, /* extension. */
1279 { "debug_pubnames", obj_mach_o_debug_section, 8}, /* extension. */
1280 { "debug_pubtypes", obj_mach_o_debug_section, 9}, /* extension. */
1281 { "debug_str", obj_mach_o_debug_section, 10}, /* extension. */
1282 { "debug_ranges", obj_mach_o_debug_section, 11}, /* extension. */
1283 { "debug_macro", obj_mach_o_debug_section, 12}, /* extension. */
1285 { "lazy_symbol_pointer", obj_mach_o_opt_tgt_section, 1},
1286 { "lazy_symbol_pointer2", obj_mach_o_opt_tgt_section, 2}, /* extension. */
1287 { "lazy_symbol_pointer3", obj_mach_o_opt_tgt_section, 3}, /* extension. */
1288 { "non_lazy_symbol_pointer", obj_mach_o_opt_tgt_section, 4},
1289 { "non_lazy_symbol_pointer_x86", obj_mach_o_opt_tgt_section, 5}, /* extension. */
1290 { "symbol_stub", obj_mach_o_opt_tgt_section, 6},
1291 { "symbol_stub1", obj_mach_o_opt_tgt_section, 7}, /* extension. */
1292 { "picsymbol_stub", obj_mach_o_opt_tgt_section, 8}, /* extension. */
1293 { "picsymbol_stub1", obj_mach_o_opt_tgt_section, 9}, /* extension. */
1294 { "picsymbol_stub2", obj_mach_o_opt_tgt_section, 4}, /* extension. */
1295 { "picsymbol_stub3", obj_mach_o_opt_tgt_section, 4}, /* extension. */
1297 { "section", obj_mach_o_section, 0},
1298 { "zerofill", obj_mach_o_zerofill, 0},
1300 /* Symbol qualifiers. */
1301 {"local", obj_mach_o_sym_qual, OBJ_MACH_O_SYM_LOCAL},
1302 {"globl", obj_mach_o_sym_qual, OBJ_MACH_O_SYM_GLOBL},
1303 {"reference", obj_mach_o_sym_qual, OBJ_MACH_O_SYM_REFERENCE},
1304 {"weak_reference", obj_mach_o_sym_qual, OBJ_MACH_O_SYM_WEAK_REF},
1305 {"lazy_reference", obj_mach_o_sym_qual, OBJ_MACH_O_SYM_LAZY_REF},
1306 {"weak_definition", obj_mach_o_sym_qual, OBJ_MACH_O_SYM_WEAK_DEF},
1307 {"private_extern", obj_mach_o_sym_qual, OBJ_MACH_O_SYM_PRIV_EXT},
1308 {"no_dead_strip", obj_mach_o_sym_qual, OBJ_MACH_O_SYM_NO_DEAD_STRIP},
1309 {"weak", obj_mach_o_sym_qual, OBJ_MACH_O_SYM_WEAK}, /* ext */
1311 { "indirect_symbol", obj_mach_o_indirect_symbol, 0},
1314 { "subsections_via_symbols", obj_mach_o_fileprop,
1315 OBJ_MACH_O_FILE_PROP_SUBSECTS_VIA_SYMS},
1320 /* Determine the default n_type value for a symbol from its section. */
1323 obj_mach_o_type_for_symbol (bfd_mach_o_asymbol *s)
1325 if (s->symbol.section == bfd_abs_section_ptr)
1326 return BFD_MACH_O_N_ABS;
1327 else if (s->symbol.section == bfd_com_section_ptr
1328 || s->symbol.section == bfd_und_section_ptr)
1329 return BFD_MACH_O_N_UNDF;
1331 return BFD_MACH_O_N_SECT;
1334 /* We need to check the correspondence between some kinds of symbols and their
1335 sections. Common and BSS vars will seen via the obj_macho_comm() function.
1337 The earlier we can pick up a problem, the better the diagnostics will be.
1339 However, when symbol type information is attached, the symbol section will
1340 quite possibly be unknown. So we are stuck with checking (most of the)
1341 validity at the time the file is written (unfortunately, then one doesn't
1342 get line number information in the diagnostic). */
1344 /* Here we pick up the case where symbol qualifiers have been applied that
1345 are possibly incompatible with the section etc. that the symbol is defined
1348 void obj_macho_frob_label (struct symbol *sp)
1350 bfd_mach_o_asymbol *s;
1352 bfd_mach_o_section *sec;
1355 /* Leave local symbols alone. */
1357 if (S_IS_LOCAL (sp))
1360 s = (bfd_mach_o_asymbol *) symbol_get_bfdsym (sp);
1361 /* Leave debug symbols alone. */
1362 if ((s->n_type & BFD_MACH_O_N_STAB) != 0)
1365 /* This is the base symbol type, that we mask in. */
1366 base_type = obj_mach_o_type_for_symbol (s);
1368 sec = bfd_mach_o_get_mach_o_section (s->symbol.section);
1370 sectype = sec->flags & BFD_MACH_O_SECTION_TYPE_MASK;
1372 /* If there is a pre-existing qualifier, we can make some checks about
1375 if(s->symbol.udata.i == SYM_MACHO_FIELDS_NOT_VALIDATED)
1377 if ((s->n_desc & BFD_MACH_O_N_WEAK_DEF)
1378 && sectype != BFD_MACH_O_S_COALESCED)
1379 as_bad (_("'%s' can't be a weak_definition (currently only supported"
1380 " in sections of type coalesced)"), s->symbol.name);
1382 /* Have we changed from an undefined to defined ref? */
1383 s->n_desc &= ~(REFE | LAZY);
1386 s->n_type &= ~BFD_MACH_O_N_TYPE;
1387 s->n_type |= base_type;
1390 /* This is the fall-back, we come here when we get to the end of the file and
1391 the symbol is not defined - or there are combinations of qualifiers required
1392 (e.g. global + weak_def). */
1395 obj_macho_frob_symbol (struct symbol *sp)
1397 bfd_mach_o_asymbol *s;
1399 bfd_mach_o_section *sec;
1402 /* Leave local symbols alone. */
1403 if (S_IS_LOCAL (sp))
1406 s = (bfd_mach_o_asymbol *) symbol_get_bfdsym (sp);
1407 /* Leave debug symbols alone. */
1408 if ((s->n_type & BFD_MACH_O_N_STAB) != 0)
1411 base_type = obj_mach_o_type_for_symbol (s);
1412 sec = bfd_mach_o_get_mach_o_section (s->symbol.section);
1414 sectype = sec->flags & BFD_MACH_O_SECTION_TYPE_MASK;
1416 if (s->symbol.section == bfd_und_section_ptr)
1418 /* ??? Do we really gain much from implementing this as well as the
1419 mach-o specific ones? */
1420 if (s->symbol.flags & BSF_WEAK)
1421 s->n_desc |= BFD_MACH_O_N_WEAK_REF;
1423 /* Undefined syms, become extern. */
1424 s->n_type |= BFD_MACH_O_N_EXT;
1425 S_SET_EXTERNAL (sp);
1427 else if (s->symbol.section == bfd_com_section_ptr)
1429 /* ... so do comm. */
1430 s->n_type |= BFD_MACH_O_N_EXT;
1431 S_SET_EXTERNAL (sp);
1435 if ((s->symbol.flags & BSF_WEAK)
1436 && (sectype == BFD_MACH_O_S_COALESCED)
1437 && (s->n_type & (BFD_MACH_O_N_PEXT | BFD_MACH_O_N_EXT)))
1438 s->n_desc |= BFD_MACH_O_N_WEAK_DEF;
1439 /* ??? we should do this - but then that reveals that the semantics of weak
1440 are different from what's supported in mach-o object files.
1442 as_bad (_("'%s' can't be a weak_definition."),
1446 if (s->symbol.udata.i == SYM_MACHO_FIELDS_UNSET)
1448 /* Anything here that should be added that is non-standard. */
1449 s->n_desc &= ~BFD_MACH_O_REFERENCE_MASK;
1450 s->symbol.udata.i = SYM_MACHO_FIELDS_NOT_VALIDATED;
1452 else if (s->symbol.udata.i == SYM_MACHO_FIELDS_NOT_VALIDATED)
1454 /* Try to validate any combinations. */
1455 if (s->n_desc & BFD_MACH_O_N_WEAK_DEF)
1457 if (s->symbol.section == bfd_und_section_ptr)
1458 as_bad (_("'%s' can't be a weak_definition (since it is"
1459 " undefined)"), s->symbol.name);
1460 else if (sectype != BFD_MACH_O_S_COALESCED)
1461 as_bad (_("'%s' can't be a weak_definition (currently only supported"
1462 " in sections of type coalesced)"), s->symbol.name);
1463 else if (! (s->n_type & (BFD_MACH_O_N_PEXT | BFD_MACH_O_N_EXT)))
1464 as_bad (_("Non-global symbol: '%s' can't be a weak_definition."),
1470 as_bad (_("internal error: [%s] unexpected code [%lx] in frob symbol"),
1471 s->symbol.name, (unsigned long)s->symbol.udata.i);
1473 s->n_type &= ~BFD_MACH_O_N_TYPE;
1474 s->n_type |= base_type;
1476 if (s->symbol.flags & BSF_GLOBAL)
1477 s->n_type |= BFD_MACH_O_N_EXT;
1479 /* This cuts both ways - we promote some things to external above. */
1480 if (s->n_type & (BFD_MACH_O_N_PEXT | BFD_MACH_O_N_EXT))
1481 S_SET_EXTERNAL (sp);
1486 /* Zerofill and GB Zerofill sections must be sorted to follow all other
1487 sections in their segments.
1489 The native 'as' leaves the sections physically in the order they appear in
1490 the source, and adjusts the section VMAs to meet the constraint.
1492 We follow this for now - if nothing else, it makes comparison easier.
1494 An alternative implementation would be to sort the sections as ld requires.
1495 It might be advantageous to implement such a scheme in the future (or even
1496 to make the style of section ordering user-selectable). */
1498 typedef struct obj_mach_o_set_vma_data
1502 unsigned zerofill_seen;
1503 unsigned gb_zerofill_seen;
1504 } obj_mach_o_set_vma_data;
1506 /* We do (possibly) three passes through to set the vma, so that:
1508 zerofill sections get VMAs after all others in their segment
1509 GB zerofill get VMAs last.
1511 As we go, we notice if we see any Zerofill or GB Zerofill sections, so that
1512 we can skip the additional passes if there's nothing to do. */
1515 obj_mach_o_set_section_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *v_p)
1517 bfd_mach_o_section *ms = bfd_mach_o_get_mach_o_section (sec);
1518 unsigned bfd_align = bfd_get_section_alignment (abfd, sec);
1519 obj_mach_o_set_vma_data *p = (struct obj_mach_o_set_vma_data *)v_p;
1520 unsigned sectype = (ms->flags & BFD_MACH_O_SECTION_TYPE_MASK);
1524 if (sectype == BFD_MACH_O_S_ZEROFILL)
1527 p->zerofill_seen = zf;
1529 else if (sectype == BFD_MACH_O_S_GB_ZEROFILL)
1532 p->gb_zerofill_seen = zf;
1535 if (p->vma_pass != zf)
1538 /* We know the section size now - so make a vma for the section just
1540 ms->size = bfd_get_section_size (sec);
1542 /* Make sure that the align agrees, and set to the largest value chosen. */
1543 ms->align = ms->align > bfd_align ? ms->align : bfd_align;
1544 bfd_set_section_alignment (abfd, sec, ms->align);
1546 p->vma += (1 << ms->align) - 1;
1547 p->vma &= ~((1 << ms->align) - 1);
1549 bfd_set_section_vma (abfd, sec, p->vma);
1553 /* (potentially) three passes over the sections, setting VMA. We skip the
1554 {gb}zerofill passes if we didn't see any of the relevant sections. */
1556 void obj_mach_o_post_relax_hook (void)
1558 obj_mach_o_set_vma_data d;
1560 memset (&d, 0, sizeof (d));
1562 bfd_map_over_sections (stdoutput, obj_mach_o_set_section_vma, (char *) &d);
1563 if ((d.vma_pass = d.zerofill_seen) != 0)
1564 bfd_map_over_sections (stdoutput, obj_mach_o_set_section_vma, (char *) &d);
1565 if ((d.vma_pass = d.gb_zerofill_seen) != 0)
1566 bfd_map_over_sections (stdoutput, obj_mach_o_set_section_vma, (char *) &d);
1570 obj_mach_o_set_indirect_symbols (bfd *abfd, asection *sec,
1571 void *xxx ATTRIBUTE_UNUSED)
1573 bfd_vma sect_size = bfd_section_size (abfd, sec);
1574 bfd_mach_o_section *ms = bfd_mach_o_get_mach_o_section (sec);
1577 /* See if we have any indirect syms to consider. */
1578 if (indirect_syms == NULL)
1581 /* Process indirect symbols.
1582 Check for errors, if OK attach them as a flat array to the section
1583 for which they are defined. */
1585 switch (ms->flags & BFD_MACH_O_SECTION_TYPE_MASK)
1587 case BFD_MACH_O_S_SYMBOL_STUBS:
1588 case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS:
1591 case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS:
1593 unsigned int nactual = 0;
1595 obj_mach_o_indirect_sym *isym;
1596 obj_mach_o_indirect_sym *list = NULL;
1597 obj_mach_o_indirect_sym *list_tail = NULL;
1598 unsigned long eltsiz =
1599 bfd_mach_o_section_get_entry_size (abfd, ms);
1601 for (isym = indirect_syms; isym != NULL; isym = isym->next)
1603 if (isym->sect == sec)
1609 list_tail->next = isym;
1614 /* If none are in this section, stop here. */
1618 /* If we somehow added indirect symbols to a section with a zero
1619 entry size, we're dead ... */
1620 gas_assert (eltsiz != 0);
1622 ncalc = (unsigned int) (sect_size / eltsiz);
1623 if (nactual != ncalc)
1624 as_bad (_("the number of .indirect_symbols defined in section %s"
1625 " does not match the number expected (%d defined, %d"
1626 " expected)"), sec->name, nactual, ncalc);
1630 bfd_mach_o_asymbol *sym;
1633 nactual * sizeof (bfd_mach_o_asymbol *));
1635 if (ms->indirect_syms == NULL)
1637 as_fatal (_("internal error: failed to allocate %d indirect"
1638 "symbol pointers"), nactual);
1641 for (isym = list, n = 0; isym != NULL; isym = isym->next, n++)
1643 sym = (bfd_mach_o_asymbol *)symbol_get_bfdsym (isym->sym);
1644 /* Array is init to NULL & NULL signals a local symbol
1645 If the section is lazy-bound, we need to keep the
1646 reference to the symbol, since dyld can override.
1648 Absolute symbols are handled specially. */
1649 if (sym->symbol.section == bfd_abs_section_ptr)
1650 ms->indirect_syms[n] = sym;
1651 else if (S_IS_LOCAL (isym->sym) && ! lazy)
1657 /* If the symbols is external ... */
1658 else if (S_IS_EXTERNAL (isym->sym)
1659 || (sym->n_type & BFD_MACH_O_N_EXT)
1660 || ! S_IS_DEFINED (isym->sym)
1663 sym->n_desc &= ~LAZY;
1664 /* ... it can be lazy, if not defined or hidden. */
1665 if ((sym->n_type & BFD_MACH_O_N_TYPE)
1666 == BFD_MACH_O_N_UNDF
1667 && ! (sym->n_type & BFD_MACH_O_N_PEXT)
1668 && (sym->n_type & BFD_MACH_O_N_EXT))
1669 sym->n_desc |= lazy;
1670 ms->indirect_syms[n] = sym;
1683 /* The process of relocation could alter what's externally visible, thus we
1684 leave setting the indirect symbols until last. */
1687 obj_mach_o_frob_file_after_relocs (void)
1689 bfd_map_over_sections (stdoutput, obj_mach_o_set_indirect_symbols, (char *) 0);
1692 /* Reverse relocations order to make ld happy. */
1695 obj_mach_o_reorder_section_relocs (asection *sec, arelent **rels, unsigned int n)
1698 unsigned int max = n / 2;
1700 for (i = 0; i < max; i++)
1702 arelent *r = rels[i];
1703 rels[i] = rels[n - i - 1];
1704 rels[n - i - 1] = r;
1706 bfd_set_reloc (stdoutput, sec, rels, n);
1709 /* Support stabs for mach-o. */
1712 obj_mach_o_process_stab (int what, const char *string,
1713 int type, int other, int desc)
1716 bfd_mach_o_asymbol *s;
1721 symbolP = symbol_new ("", now_seg, frag_now_fix (), frag_now);
1722 /* Special stabd NULL name indicator. */
1723 S_SET_NAME (symbolP, NULL);
1728 symbolP = symbol_new (string, undefined_section, (valueT) 0,
1729 &zero_address_frag);
1730 pseudo_set (symbolP);
1734 as_bad(_("unrecognized stab type '%c'"), (char)what);
1739 s = (bfd_mach_o_asymbol *) symbol_get_bfdsym (symbolP);
1742 /* For stabd, this will eventually get overwritten by the section number. */
1745 /* It's a debug symbol. */
1746 s->symbol.flags |= BSF_DEBUGGING;