1 /* Mach-O object file format
2 Copyright (C) 2009-2015 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. */
60 static int obj_mach_o_start_with_text_section
= 1;
62 /* Allow for special re-ordering on output. */
64 static int obj_mach_o_seen_objc_section
;
66 /* Start-up: At present, just create the sections we want. */
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
)
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
)
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
;
86 /* Remember the subsections_by_symbols state in case we need to reset
89 static int obj_mach_o_subsections_by_symbols
;
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
97 It trims leading and trailing space. */
100 collect_16char_name (char *dest
, const char *msg
, int require_comma
)
105 namstart
= input_line_pointer
;
107 while ( (c
= *input_line_pointer
) != ','
108 && !is_end_of_line
[(unsigned char) c
])
109 input_line_pointer
++;
112 int len
= input_line_pointer
- namstart
; /* could be zero. */
113 /* lose any trailing space. */
114 while (len
> 0 && namstart
[len
-1] == ' ')
118 *input_line_pointer
= '\0'; /* make a temp string. */
119 as_bad (_("the %s name '%s' is too long (maximum 16 characters)"),
121 *input_line_pointer
= c
; /* restore for printing. */
125 memcpy (dest
, namstart
, len
);
128 if (c
!= ',' && require_comma
)
130 as_bad (_("expected a %s name followed by a `,'"), msg
);
138 obj_mach_o_get_section_names (char *seg
, char *sec
,
139 unsigned segl
, unsigned secl
)
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))
146 ignore_rest_of_line ();
149 input_line_pointer
++; /* Skip the terminating ',' */
151 /* Parse section name, which can be empty. */
152 memset (sec
, 0, secl
);
153 collect_16char_name (sec
, "section", 0);
157 /* Build (or get) a section from the mach-o description - which includes
158 optional definitions for type, attributes, alignment and stub size.
160 BFD supplies default values for sections which have a canonical name. */
162 #define SECT_TYPE_SPECIFIED 0x0001
163 #define SECT_ATTR_SPECIFIED 0x0002
164 #define SECT_ALGN_SPECIFIED 0x0004
165 #define SECT_STUB_SPECIFIED 0x0008
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
)
173 unsigned int sectype
, secattr
, secalign
;
174 flagword oldflags
, flags
;
177 bfd_mach_o_section
*msect
;
178 const mach_o_section_name_xlat
*xlat
;
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
);
184 /* TODO: more checking of whether overides are acually allowed. */
188 name
= xstrdup (xlat
->bfd_name
);
189 sectype
= xlat
->macho_sectype
;
190 if (specified_mask
& SECT_TYPE_SPECIFIED
)
192 if ((sectype
== BFD_MACH_O_S_ZEROFILL
193 || sectype
== BFD_MACH_O_S_GB_ZEROFILL
)
194 && sectype
!= usectype
)
195 as_bad (_("cannot overide zerofill section type for `%s,%s'"),
200 secattr
= xlat
->macho_secattr
;
201 secalign
= xlat
->sectalign
;
202 flags
= xlat
->bfd_flags
;
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
209 size_t seglen
= strlen (segname
);
210 size_t sectlen
= strlen (sectname
);
213 n
= xmalloc (seglen
+ 1 + sectlen
+ 1);
214 memcpy (n
, segname
, seglen
);
216 memcpy (n
+ seglen
+ 1, sectname
, sectlen
);
217 n
[seglen
+ 1 + sectlen
] = 0;
219 if (specified_mask
& SECT_TYPE_SPECIFIED
)
222 sectype
= BFD_MACH_O_S_REGULAR
;
223 secattr
= BFD_MACH_O_S_ATTR_NONE
;
225 flags
= SEC_NO_FLAGS
;
228 /* For now, just use what the user provided. */
230 if (specified_mask
& SECT_ATTR_SPECIFIED
)
233 if (specified_mask
& SECT_ALGN_SPECIFIED
)
236 /* Sub-segments don't exists as is on Mach-O. */
237 sec
= subseg_new (name
, 0);
239 oldflags
= bfd_get_section_flags (stdoutput
, sec
);
240 msect
= bfd_mach_o_get_mach_o_section (sec
);
242 if (oldflags
== SEC_NO_FLAGS
)
244 /* In the absence of canonical information, try to determine CODE and
245 DEBUG section flags from the mach-o section data. */
246 if (flags
== SEC_NO_FLAGS
247 && (specified_mask
& SECT_ATTR_SPECIFIED
)
248 && (secattr
& BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS
))
251 if (flags
== SEC_NO_FLAGS
252 && (specified_mask
& SECT_ATTR_SPECIFIED
)
253 && (secattr
& BFD_MACH_O_S_ATTR_DEBUG
))
254 flags
|= SEC_DEBUGGING
;
256 /* New, so just use the defaults or what's specified. */
257 if (! bfd_set_section_flags (stdoutput
, sec
, flags
))
258 as_warn (_("failed to set flags for \"%s\": %s"),
259 bfd_section_name (stdoutput
, sec
),
260 bfd_errmsg (bfd_get_error ()));
262 strncpy (msect
->segname
, segname
, sizeof (msect
->segname
));
263 strncpy (msect
->sectname
, sectname
, sizeof (msect
->sectname
));
265 msect
->align
= secalign
;
266 msect
->flags
= sectype
| secattr
;
268 if (sectype
== BFD_MACH_O_S_ZEROFILL
269 || sectype
== BFD_MACH_O_S_GB_ZEROFILL
)
270 seg_info (sec
)->bss
= 1;
272 else if (flags
!= SEC_NO_FLAGS
)
274 if (flags
!= oldflags
275 || msect
->flags
!= (secattr
| sectype
))
276 as_warn (_("Ignoring changed section attributes for %s"), name
);
279 if (specified_mask
& SECT_STUB_SPECIFIED
)
280 /* At present, the stub size is not supplied from the BFD tables. */
281 msect
->reserved2
= stub_size
;
288 The '.section' specification syntax looks like:
289 .section <segment> , <section> [, type [, attribs [, size]]]
291 White space is allowed everywhere between elements.
293 <segment> and <section> may be from 0 to 16 chars in length - they may
294 contain spaces but leading and trailing space will be trimmed. It is
295 mandatory that they be present (or that zero-length names are indicated
298 There is only a single section type for any entry.
300 There may be multiple attributes, they are delimited by `+'.
302 Not all section types and attributes are accepted by the Darwin system
303 assemblers as user-specifiable - although, at present, we do here. */
306 obj_mach_o_section (int ignore ATTRIBUTE_UNUSED
)
308 unsigned int sectype
= BFD_MACH_O_S_REGULAR
;
309 unsigned int specified_mask
= 0;
310 unsigned int secattr
= 0;
311 offsetT sizeof_stub
= 0;
316 #ifdef md_flush_pending_output
317 md_flush_pending_output ();
320 /* Get the User's segment annd section names. */
321 if (! obj_mach_o_get_section_names (segname
, sectname
, 17, 17))
324 /* Parse section type, if present. */
325 if (*input_line_pointer
== ',')
331 input_line_pointer
++;
333 p
= input_line_pointer
;
334 while ((c
= *input_line_pointer
) != ','
335 && !is_end_of_line
[(unsigned char) c
])
336 input_line_pointer
++;
338 len
= input_line_pointer
- p
;
339 /* strip trailing spaces. */
340 while (len
> 0 && p
[len
-1] == ' ')
344 /* Temporarily make a string from the token. */
346 sectype
= bfd_mach_o_get_section_type_from_name (stdoutput
, p
);
347 if (sectype
> 255) /* Max Section ID == 255. */
349 as_bad (_("unknown or invalid section type '%s'"), p
);
351 ignore_rest_of_line ();
355 specified_mask
|= SECT_TYPE_SPECIFIED
;
360 TODO: check validity of attributes for section type. */
361 if ((specified_mask
& SECT_TYPE_SPECIFIED
)
368 /* Skip initial `,' and subsequent `+'. */
369 input_line_pointer
++;
371 p
= input_line_pointer
;
372 while ((c
= *input_line_pointer
) != '+'
374 && !is_end_of_line
[(unsigned char) c
])
375 input_line_pointer
++;
377 len
= input_line_pointer
- p
;
378 /* strip trailing spaces. */
379 while (len
> 0 && p
[len
-1] == ' ')
383 /* Temporarily make a string from the token. */
385 attr
= bfd_mach_o_get_section_attribute_from_name (p
);
388 as_bad (_("unknown or invalid section attribute '%s'"), p
);
390 ignore_rest_of_line ();
395 specified_mask
|= SECT_ATTR_SPECIFIED
;
401 while (*input_line_pointer
== '+');
403 /* Parse sizeof_stub. */
404 if ((specified_mask
& SECT_ATTR_SPECIFIED
)
405 && *input_line_pointer
== ',')
407 if (sectype
!= BFD_MACH_O_S_SYMBOL_STUBS
)
409 as_bad (_("unexpected section size information"));
410 ignore_rest_of_line ();
414 input_line_pointer
++;
415 sizeof_stub
= get_absolute_expression ();
416 specified_mask
|= SECT_STUB_SPECIFIED
;
418 else if ((specified_mask
& SECT_ATTR_SPECIFIED
)
419 && sectype
== BFD_MACH_O_S_SYMBOL_STUBS
)
421 as_bad (_("missing sizeof_stub expression"));
422 ignore_rest_of_line ();
428 new_seg
= obj_mach_o_make_or_get_sect (segname
, sectname
, specified_mask
,
429 sectype
, secattr
, 0 /*align */,
433 subseg_set (new_seg
, 0);
434 demand_empty_rest_of_line ();
438 /* .zerofill segname, sectname [, symbolname, size [, align]]
440 Zerofill switches, temporarily, to a sect of type 'zerofill'.
442 If a variable name is given, it defines that in the section.
443 Otherwise it just creates the section if it doesn't exist. */
446 obj_mach_o_zerofill (int ignore ATTRIBUTE_UNUSED
)
450 segT old_seg
= now_seg
;
453 unsigned int align
= 0;
454 unsigned int specified_mask
= 0;
457 #ifdef md_flush_pending_output
458 md_flush_pending_output ();
461 /* Get the User's segment annd section names. */
462 if (! obj_mach_o_get_section_names (segname
, sectname
, 17, 17))
465 /* Parse variable definition, if present. */
466 if (*input_line_pointer
== ',')
468 /* Parse symbol, size [.align]
469 We follow the method of s_common_internal, with the difference
470 that the symbol cannot be a duplicate-common. */
476 input_line_pointer
++; /* Skip ',' */
478 c
= get_symbol_name (&name
);
479 /* Just after name is now '\0'. */
480 p
= input_line_pointer
;
485 as_bad (_("expected symbol name"));
486 ignore_rest_of_line ();
490 SKIP_WHITESPACE_AFTER_NAME ();
491 if (*input_line_pointer
== ',')
492 input_line_pointer
++;
494 expression_and_evaluate (&exp
);
495 if (exp
.X_op
!= O_constant
496 && exp
.X_op
!= O_absent
)
498 as_bad (_("bad or irreducible absolute expression"));
499 ignore_rest_of_line ();
502 else if (exp
.X_op
== O_absent
)
504 as_bad (_("missing size expression"));
505 ignore_rest_of_line ();
509 size
= exp
.X_add_number
;
510 size
&= ((offsetT
) 2 << (stdoutput
->arch_info
->bits_per_address
- 1)) - 1;
511 if (exp
.X_add_number
!= size
|| !exp
.X_unsigned
)
513 as_warn (_("size (%ld) out of range, ignored"),
514 (long) exp
.X_add_number
);
515 ignore_rest_of_line ();
519 *p
= 0; /* Make the name into a c string for err messages. */
520 sym
= symbol_find_or_make (name
);
521 if (S_IS_DEFINED (sym
) || symbol_equated_p (sym
))
523 as_bad (_("symbol `%s' is already defined"), name
);
525 ignore_rest_of_line ();
529 size
= S_GET_VALUE (sym
);
531 size
= exp
.X_add_number
;
532 else if (size
!= exp
.X_add_number
)
533 as_warn (_("size of \"%s\" is already %ld; not changing to %ld"),
534 name
, (long) size
, (long) exp
.X_add_number
);
536 *p
= c
; /* Restore the termination char. */
539 if (*input_line_pointer
== ',')
541 align
= (unsigned int) parse_align (0);
542 if (align
== (unsigned int) -1)
544 as_warn (_("align value not recognized, using size"));
549 as_warn (_("Alignment (%lu) too large: 15 assumed."),
550 (unsigned long)align
);
553 specified_mask
|= SECT_ALGN_SPECIFIED
;
556 /* else just a section definition. */
558 specified_mask
|= SECT_TYPE_SPECIFIED
;
559 new_seg
= obj_mach_o_make_or_get_sect (segname
, sectname
, specified_mask
,
560 BFD_MACH_O_S_ZEROFILL
,
561 BFD_MACH_O_S_ATTR_NONE
,
562 align
, (offsetT
) 0 /*stub size*/);
566 /* In case the user specifies the bss section by mach-o name.
567 Create it on demand */
568 if (strcmp (new_seg
->name
, BSS_SECTION_NAME
) == 0
569 && bss_section
== NULL
)
570 bss_section
= new_seg
;
572 subseg_set (new_seg
, 0);
580 record_alignment (new_seg
, align
);
581 frag_align (align
, 0, 0);
584 /* Detach from old frag. */
585 if (S_GET_SEGMENT (sym
) == new_seg
)
586 symbol_get_frag (sym
)->fr_symbol
= NULL
;
588 symbol_set_frag (sym
, frag_now
);
589 pfrag
= frag_var (rs_org
, 1, 1, 0, sym
, size
, NULL
);
592 S_SET_SEGMENT (sym
, new_seg
);
593 if (new_seg
== bss_section
)
594 S_CLEAR_EXTERNAL (sym
);
598 /* switch back to the section that was current before the .zerofill. */
599 subseg_set (old_seg
, 0);
603 obj_mach_o_segT_from_bfd_name (const char *nam
, int must_succeed
)
605 const mach_o_section_name_xlat
*xlat
;
609 /* BFD has tables of flags and default attributes for all the sections that
610 have a 'canonical' name. */
611 xlat
= bfd_mach_o_section_data_for_bfd_name (stdoutput
, nam
, &segn
);
615 as_fatal (_("BFD is out of sync with GAS, "
616 "unhandled well-known section type `%s'"), nam
);
620 sec
= bfd_get_section_by_name (stdoutput
, nam
);
623 bfd_mach_o_section
*msect
;
625 sec
= subseg_force_new (xlat
->bfd_name
, 0);
627 /* Set default type, attributes and alignment. */
628 msect
= bfd_mach_o_get_mach_o_section (sec
);
629 msect
->flags
= xlat
->macho_sectype
| xlat
->macho_secattr
;
630 msect
->align
= xlat
->sectalign
;
632 if ((msect
->flags
& BFD_MACH_O_SECTION_TYPE_MASK
)
633 == BFD_MACH_O_S_ZEROFILL
)
634 seg_info (sec
)->bss
= 1;
640 static const char * const known_sections
[] =
645 /* 2 */ ".static_const",
649 /* 6 */ ".literal16",
650 /* 7 */ ".constructor",
651 /* 8 */ ".destructor",
654 /* 10 */ ".const_data",
655 /* 11 */ ".static_data",
656 /* 12 */ ".mod_init_func",
657 /* 13 */ ".mod_term_func",
662 /* Interface for a known non-optional section directive. */
665 obj_mach_o_known_section (int sect_index
)
669 #ifdef md_flush_pending_output
670 md_flush_pending_output ();
673 section
= obj_mach_o_segT_from_bfd_name (known_sections
[sect_index
], 1);
675 subseg_set (section
, 0);
677 /* else, we leave the section as it was; there was a fatal error anyway. */
680 static const char * const objc_sections
[] =
683 /* 1 */ ".objc_class",
684 /* 2 */ ".objc_meta_class",
685 /* 3 */ ".objc_cat_cls_meth",
686 /* 4 */ ".objc_cat_inst_meth",
687 /* 5 */ ".objc_protocol",
688 /* 6 */ ".objc_string_object",
689 /* 7 */ ".objc_cls_meth",
690 /* 8 */ ".objc_inst_meth",
691 /* 9 */ ".objc_cls_refs",
692 /* 10 */ ".objc_message_refs",
693 /* 11 */ ".objc_symbols",
694 /* 12 */ ".objc_category",
695 /* 13 */ ".objc_class_vars",
696 /* 14 */ ".objc_instance_vars",
697 /* 15 */ ".objc_module_info",
698 /* 16 */ ".cstring", /* objc_class_names Alias for .cstring */
699 /* 17 */ ".cstring", /* Alias objc_meth_var_types for .cstring */
700 /* 18 */ ".cstring", /* objc_meth_var_names Alias for .cstring */
701 /* 19 */ ".objc_selector_strs",
702 /* 20 */ ".objc_image_info", /* extension. */
703 /* 21 */ ".objc_selector_fixup", /* extension. */
704 /* 22 */ ".objc1_class_ext", /* ObjC-1 extension. */
705 /* 23 */ ".objc1_property_list", /* ObjC-1 extension. */
706 /* 24 */ ".objc1_protocol_ext" /* ObjC-1 extension. */
709 /* This currently does the same as known_sections, but kept separate for
710 ease of maintenance. */
713 obj_mach_o_objc_section (int sect_index
)
717 #ifdef md_flush_pending_output
718 md_flush_pending_output ();
721 section
= obj_mach_o_segT_from_bfd_name (objc_sections
[sect_index
], 1);
724 obj_mach_o_seen_objc_section
= 1; /* We need to ensure that certain
725 sections are present and in the
727 subseg_set (section
, 0);
730 /* else, we leave the section as it was; there was a fatal error anyway. */
733 /* Debug section directives. */
735 static const char * const debug_sections
[] =
739 /* 1 */ ".debug_frame",
740 /* 2 */ ".debug_info",
741 /* 3 */ ".debug_abbrev",
742 /* 4 */ ".debug_aranges",
743 /* 5 */ ".debug_macinfo",
744 /* 6 */ ".debug_line",
745 /* 7 */ ".debug_loc",
746 /* 8 */ ".debug_pubnames",
747 /* 9 */ ".debug_pubtypes",
748 /* 10 */ ".debug_str",
749 /* 11 */ ".debug_ranges",
750 /* 12 */ ".debug_macro"
753 /* ??? Maybe these should be conditional on gdwarf-*.
754 It`s also likely that we will need to be able to set them from the cfi
758 obj_mach_o_debug_section (int sect_index
)
762 #ifdef md_flush_pending_output
763 md_flush_pending_output ();
766 section
= obj_mach_o_segT_from_bfd_name (debug_sections
[sect_index
], 1);
768 subseg_set (section
, 0);
770 /* else, we leave the section as it was; there was a fatal error anyway. */
773 /* This could be moved to the tc-xx files, but there is so little dependency
774 there, that the code might as well be shared. */
783 /* The extensions here are for specific sections that are generated by GCC
784 and Darwin system tools, but don't have directives in the `system as'. */
786 static const struct opt_tgt_sect tgt_sections
[] =
788 /* 0 */ { NULL
, 0, 0},
789 /* 1 */ { ".lazy_symbol_pointer", 0, 0},
790 /* 2 */ { ".lazy_symbol_pointer2", 0, 0}, /* X86 - extension */
791 /* 3 */ { ".lazy_symbol_pointer3", 0, 0}, /* X86 - extension */
792 /* 4 */ { ".non_lazy_symbol_pointer", 0, 0},
793 /* 5 */ { ".non_lazy_symbol_pointer_x86", 0, 0}, /* X86 - extension */
794 /* 6 */ { ".symbol_stub", 16, 20},
795 /* 7 */ { ".symbol_stub1", 0, 16}, /* PPC - extension */
796 /* 8 */ { ".picsymbol_stub", 26, 36},
797 /* 9 */ { ".picsymbol_stub1", 0, 32}, /* PPC - extension */
798 /* 10 */ { ".picsymbol_stub2", 25, 0}, /* X86 - extension */
799 /* 11 */ { ".picsymbol_stub3", 5, 0}, /* X86 - extension */
802 /* Interface for an optional section directive. */
805 obj_mach_o_opt_tgt_section (int sect_index
)
807 const struct opt_tgt_sect
*tgtsct
= &tgt_sections
[sect_index
];
810 #ifdef md_flush_pending_output
811 md_flush_pending_output ();
814 section
= obj_mach_o_segT_from_bfd_name (tgtsct
->name
, 0);
817 as_bad (_("%s is not used for the selected target"), tgtsct
->name
);
818 /* Leave the section as it is. */
822 bfd_mach_o_section
*mo_sec
= bfd_mach_o_get_mach_o_section (section
);
823 subseg_set (section
, 0);
824 #if defined (TC_I386)
825 mo_sec
->reserved2
= tgtsct
->x86_val
;
826 #elif defined (TC_PPC)
827 mo_sec
->reserved2
= tgtsct
->ppc_val
;
829 mo_sec
->reserved2
= 0;
834 /* We don't necessarily have the three 'base' sections on mach-o.
835 Normally, we would start up with only the 'text' section defined.
836 However, even that can be suppressed with (TODO) c/l option "-n".
837 Thus, we have to be able to create all three sections on-demand. */
840 obj_mach_o_base_section (int sect_index
)
844 #ifdef md_flush_pending_output
845 md_flush_pending_output ();
848 /* We don't support numeric (or any other) qualifications on the
849 well-known section shorthands. */
850 demand_empty_rest_of_line ();
854 /* Handle the three sections that are globally known within GAS.
855 For Mach-O, these are created on demand rather than at startup. */
857 if (text_section
== NULL
)
858 text_section
= obj_mach_o_segT_from_bfd_name (TEXT_SECTION_NAME
, 1);
859 if (obj_mach_o_is_static
)
861 bfd_mach_o_section
*mo_sec
862 = bfd_mach_o_get_mach_o_section (text_section
);
863 mo_sec
->flags
&= ~BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS
;
865 section
= text_section
;
868 if (data_section
== NULL
)
869 data_section
= obj_mach_o_segT_from_bfd_name (DATA_SECTION_NAME
, 1);
870 section
= data_section
;
873 /* ??? maybe this achieves very little, as an addition. */
874 if (bss_section
== NULL
)
876 bss_section
= obj_mach_o_segT_from_bfd_name (BSS_SECTION_NAME
, 1);
877 seg_info (bss_section
)->bss
= 1;
879 section
= bss_section
;
882 as_fatal (_("internal error: base section index out of range"));
886 subseg_set (section
, 0);
889 /* This finishes off parsing a .comm or .lcomm statement, which both can have
890 an (optional) alignment field. It also allows us to create the bss section
894 obj_mach_o_common_parse (int is_local
, symbolS
*symbolP
,
898 bfd_mach_o_asymbol
*s
;
902 /* Both comm and lcomm take an optional alignment, as a power
903 of two between 1 and 15. */
904 if (*input_line_pointer
== ',')
906 /* We expect a power of 2. */
907 align
= parse_align (0);
908 if (align
== (addressT
) -1)
912 as_warn (_("Alignment (%lu) too large: 15 assumed."),
913 (unsigned long)align
);
918 s
= (bfd_mach_o_asymbol
*) symbol_get_bfdsym (symbolP
);
921 /* Create the BSS section on demand. */
922 if (bss_section
== NULL
)
924 bss_section
= obj_mach_o_segT_from_bfd_name (BSS_SECTION_NAME
, 1);
925 seg_info (bss_section
)->bss
= 1;
927 bss_alloc (symbolP
, size
, align
);
928 s
->n_type
= BFD_MACH_O_N_SECT
;
929 S_CLEAR_EXTERNAL (symbolP
);
933 S_SET_VALUE (symbolP
, size
);
934 S_SET_ALIGN (symbolP
, align
);
935 S_SET_EXTERNAL (symbolP
);
936 S_SET_SEGMENT (symbolP
, bfd_com_section_ptr
);
937 s
->n_type
= BFD_MACH_O_N_UNDF
| BFD_MACH_O_N_EXT
;
940 /* This is a data object (whatever we choose that to mean). */
941 s
->symbol
.flags
|= BSF_OBJECT
;
943 /* We've set symbol qualifiers, so validate if you can. */
944 s
->symbol
.udata
.i
= SYM_MACHO_FIELDS_NOT_VALIDATED
;
950 obj_mach_o_comm (int is_local
)
952 s_comm_internal (is_local
, obj_mach_o_common_parse
);
955 /* Set properties that apply to the whole file. At present, the only
956 one defined, is subsections_via_symbols. */
958 typedef enum obj_mach_o_file_properties
{
959 OBJ_MACH_O_FILE_PROP_NONE
= 0,
960 OBJ_MACH_O_FILE_PROP_SUBSECTS_VIA_SYMS
,
961 OBJ_MACH_O_FILE_PROP_MAX
962 } obj_mach_o_file_properties
;
965 obj_mach_o_fileprop (int prop
)
967 if (prop
< 0 || prop
>= OBJ_MACH_O_FILE_PROP_MAX
)
968 as_fatal (_("internal error: bad file property ID %d"), prop
);
970 switch ((obj_mach_o_file_properties
) prop
)
972 case OBJ_MACH_O_FILE_PROP_SUBSECTS_VIA_SYMS
:
973 obj_mach_o_subsections_by_symbols
= 1;
974 if (!bfd_set_private_flags (stdoutput
,
975 BFD_MACH_O_MH_SUBSECTIONS_VIA_SYMBOLS
))
976 as_bad (_("failed to set subsections by symbols"));
977 demand_empty_rest_of_line ();
984 /* Temporary markers for symbol reference data.
985 Lazy will remain in place. */
989 /* We have a bunch of qualifiers that may be applied to symbols.
990 .globl is handled here so that we might make sure that conflicting qualifiers
991 are caught where possible. */
993 typedef enum obj_mach_o_symbol_type
{
994 OBJ_MACH_O_SYM_UNK
= 0,
995 OBJ_MACH_O_SYM_LOCAL
= 1,
996 OBJ_MACH_O_SYM_GLOBL
= 2,
997 OBJ_MACH_O_SYM_REFERENCE
= 3,
998 OBJ_MACH_O_SYM_WEAK_REF
= 4,
999 OBJ_MACH_O_SYM_LAZY_REF
= 5,
1000 OBJ_MACH_O_SYM_WEAK_DEF
= 6,
1001 OBJ_MACH_O_SYM_PRIV_EXT
= 7,
1002 OBJ_MACH_O_SYM_NO_DEAD_STRIP
= 8,
1003 OBJ_MACH_O_SYM_WEAK
= 9
1004 } obj_mach_o_symbol_type
;
1006 /* Set Mach-O-specific symbol qualifiers. */
1009 obj_mach_o_set_symbol_qualifier (symbolS
*sym
, int type
)
1012 bfd_mach_o_asymbol
*s
= (bfd_mach_o_asymbol
*) symbol_get_bfdsym (sym
);
1013 bfd_mach_o_section
*sec
;
1016 /* If the symbol is defined, then we can do more rigorous checking on
1017 the validity of the qualifiers. Otherwise, we are stuck with waiting
1018 until it's defined - or until write the file.
1020 In certain cases (e.g. when a symbol qualifier is intended to introduce
1021 an undefined symbol in a stubs section) we should check that the current
1022 section is appropriate to the qualifier. */
1024 is_defined
= s
->symbol
.section
!= bfd_und_section_ptr
;
1026 sec
= bfd_mach_o_get_mach_o_section (s
->symbol
.section
) ;
1028 sec
= bfd_mach_o_get_mach_o_section (now_seg
) ;
1031 sectype
= sec
->flags
& BFD_MACH_O_SECTION_TYPE_MASK
;
1033 switch ((obj_mach_o_symbol_type
) type
)
1035 case OBJ_MACH_O_SYM_LOCAL
:
1036 /* This is an extension over the system tools. */
1037 if (s
->n_type
& (BFD_MACH_O_N_PEXT
| BFD_MACH_O_N_EXT
))
1039 as_bad (_("'%s' previously declared as '%s'."), s
->symbol
.name
,
1040 (s
->n_type
& BFD_MACH_O_N_PEXT
) ? "private extern"
1042 s
->symbol
.udata
.i
= SYM_MACHO_FIELDS_UNSET
;
1047 s
->n_type
&= ~BFD_MACH_O_N_EXT
;
1048 S_CLEAR_EXTERNAL (sym
);
1052 case OBJ_MACH_O_SYM_PRIV_EXT
:
1053 s
->n_type
|= BFD_MACH_O_N_PEXT
;
1054 s
->n_desc
&= ~LAZY
; /* The native tool switches this off too. */
1055 /* We follow the system tools in marking PEXT as also global. */
1058 case OBJ_MACH_O_SYM_GLOBL
:
1059 /* It's not an error to define a symbol and then make it global. */
1060 s
->n_type
|= BFD_MACH_O_N_EXT
;
1061 S_SET_EXTERNAL (sym
);
1064 case OBJ_MACH_O_SYM_REFERENCE
:
1066 s
->n_desc
|= BFD_MACH_O_N_NO_DEAD_STRIP
;
1068 s
->n_desc
|= (REFE
| BFD_MACH_O_N_NO_DEAD_STRIP
);
1071 case OBJ_MACH_O_SYM_LAZY_REF
:
1073 s
->n_desc
|= BFD_MACH_O_N_NO_DEAD_STRIP
;
1075 s
->n_desc
|= (REFE
| LAZY
| BFD_MACH_O_N_NO_DEAD_STRIP
);
1078 /* Force ld to retain the symbol - even if it appears unused. */
1079 case OBJ_MACH_O_SYM_NO_DEAD_STRIP
:
1080 s
->n_desc
|= BFD_MACH_O_N_NO_DEAD_STRIP
;
1083 /* Mach-O's idea of weak ... */
1084 case OBJ_MACH_O_SYM_WEAK_REF
:
1085 s
->n_desc
|= BFD_MACH_O_N_WEAK_REF
;
1088 case OBJ_MACH_O_SYM_WEAK_DEF
:
1089 if (is_defined
&& sectype
!= BFD_MACH_O_S_COALESCED
)
1091 as_bad (_("'%s' can't be a weak_definition (currently only"
1092 " supported in sections of type coalesced)"),
1094 s
->symbol
.udata
.i
= SYM_MACHO_FIELDS_UNSET
;
1098 s
->n_desc
|= BFD_MACH_O_N_WEAK_DEF
;
1101 case OBJ_MACH_O_SYM_WEAK
:
1102 /* A generic 'weak' - we try to figure out what it means at
1103 symbol frob time. */
1111 /* We've seen some kind of qualifier - check validity if or when the entity
1113 s
->symbol
.udata
.i
= SYM_MACHO_FIELDS_NOT_VALIDATED
;
1117 /* Respond to symbol qualifiers.
1119 .<qualifier> symbol [, symbol]*
1120 a list of symbols is an extension over the Darwin system as. */
1123 obj_mach_o_sym_qual (int ntype
)
1129 #ifdef md_flush_pending_output
1130 md_flush_pending_output ();
1135 c
= get_symbol_name (&name
);
1136 symbolP
= symbol_find_or_make (name
);
1137 obj_mach_o_set_symbol_qualifier (symbolP
, ntype
);
1138 *input_line_pointer
= c
;
1139 SKIP_WHITESPACE_AFTER_NAME ();
1140 c
= *input_line_pointer
;
1143 input_line_pointer
++;
1145 if (is_end_of_line
[(unsigned char) *input_line_pointer
])
1151 demand_empty_rest_of_line ();
1154 typedef struct obj_mach_o_indirect_sym
1158 struct obj_mach_o_indirect_sym
*next
;
1159 } obj_mach_o_indirect_sym
;
1161 /* We store in order an maintain a pointer to the last one - to save reversing
1163 obj_mach_o_indirect_sym
*indirect_syms
;
1164 obj_mach_o_indirect_sym
*indirect_syms_tail
;
1167 obj_mach_o_indirect_symbol (int arg ATTRIBUTE_UNUSED
)
1169 bfd_mach_o_section
*sec
= bfd_mach_o_get_mach_o_section (now_seg
);
1171 #ifdef md_flush_pending_output
1172 md_flush_pending_output ();
1175 if (obj_mach_o_is_static
)
1176 as_bad (_("use of .indirect_symbols requires `-dynamic'"));
1178 switch (sec
->flags
& BFD_MACH_O_SECTION_TYPE_MASK
)
1180 case BFD_MACH_O_S_SYMBOL_STUBS
:
1181 case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS
:
1182 case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS
:
1184 obj_mach_o_indirect_sym
*isym
;
1186 char c
= get_symbol_name (&name
);
1187 symbolS
*sym
= symbol_find_or_make (name
);
1188 unsigned int elsize
=
1189 bfd_mach_o_section_get_entry_size (stdoutput
, sec
);
1193 as_bad (_("attempt to add an indirect_symbol to a stub or"
1194 " reference section with a zero-sized element at %s"),
1196 (void) restore_line_pointer (c
);
1197 ignore_rest_of_line ();
1200 (void) restore_line_pointer (c
);
1202 /* The indirect symbols are validated after the symbol table is
1203 frozen, we must make sure that if a local symbol is used as an
1204 indirect, it is promoted to a 'real' one. Fetching the bfd sym
1206 symbol_get_bfdsym (sym
);
1207 isym
= (obj_mach_o_indirect_sym
*)
1208 xmalloc (sizeof (obj_mach_o_indirect_sym
));
1210 /* Just record the data for now, we will validate it when we
1211 compute the output in obj_mach_o_set_indirect_symbols. */
1213 isym
->sect
= now_seg
;
1215 if (indirect_syms
== NULL
)
1216 indirect_syms
= isym
;
1218 indirect_syms_tail
->next
= isym
;
1219 indirect_syms_tail
= isym
;
1224 as_bad (_("an .indirect_symbol must be in a symbol pointer"
1225 " or stub section."));
1226 ignore_rest_of_line ();
1229 demand_empty_rest_of_line ();
1232 const pseudo_typeS mach_o_pseudo_table
[] =
1234 /* Section directives. */
1235 { "comm", obj_mach_o_comm
, 0 },
1236 { "lcomm", obj_mach_o_comm
, 1 },
1238 { "text", obj_mach_o_base_section
, 1},
1239 { "data", obj_mach_o_base_section
, 2},
1240 { "bss", obj_mach_o_base_section
, 3}, /* extension */
1242 { "const", obj_mach_o_known_section
, 1},
1243 { "static_const", obj_mach_o_known_section
, 2},
1244 { "cstring", obj_mach_o_known_section
, 3},
1245 { "literal4", obj_mach_o_known_section
, 4},
1246 { "literal8", obj_mach_o_known_section
, 5},
1247 { "literal16", obj_mach_o_known_section
, 6},
1248 { "constructor", obj_mach_o_known_section
, 7},
1249 { "destructor", obj_mach_o_known_section
, 8},
1250 { "eh_frame", obj_mach_o_known_section
, 9},
1252 { "const_data", obj_mach_o_known_section
, 10},
1253 { "static_data", obj_mach_o_known_section
, 11},
1254 { "mod_init_func", obj_mach_o_known_section
, 12},
1255 { "mod_term_func", obj_mach_o_known_section
, 13},
1256 { "dyld", obj_mach_o_known_section
, 14},
1257 { "cfstring", obj_mach_o_known_section
, 15},
1259 { "objc_class", obj_mach_o_objc_section
, 1},
1260 { "objc_meta_class", obj_mach_o_objc_section
, 2},
1261 { "objc_cat_cls_meth", obj_mach_o_objc_section
, 3},
1262 { "objc_cat_inst_meth", obj_mach_o_objc_section
, 4},
1263 { "objc_protocol", obj_mach_o_objc_section
, 5},
1264 { "objc_string_object", obj_mach_o_objc_section
, 6},
1265 { "objc_cls_meth", obj_mach_o_objc_section
, 7},
1266 { "objc_inst_meth", obj_mach_o_objc_section
, 8},
1267 { "objc_cls_refs", obj_mach_o_objc_section
, 9},
1268 { "objc_message_refs", obj_mach_o_objc_section
, 10},
1269 { "objc_symbols", obj_mach_o_objc_section
, 11},
1270 { "objc_category", obj_mach_o_objc_section
, 12},
1271 { "objc_class_vars", obj_mach_o_objc_section
, 13},
1272 { "objc_instance_vars", obj_mach_o_objc_section
, 14},
1273 { "objc_module_info", obj_mach_o_objc_section
, 15},
1274 { "objc_class_names", obj_mach_o_objc_section
, 16}, /* Alias for .cstring */
1275 { "objc_meth_var_types", obj_mach_o_objc_section
, 17}, /* Alias for .cstring */
1276 { "objc_meth_var_names", obj_mach_o_objc_section
, 18}, /* Alias for .cstring */
1277 { "objc_selector_strs", obj_mach_o_objc_section
, 19},
1278 { "objc_image_info", obj_mach_o_objc_section
, 20}, /* extension. */
1279 { "objc_selector_fixup", obj_mach_o_objc_section
, 21}, /* extension. */
1280 { "objc1_class_ext", obj_mach_o_objc_section
, 22}, /* ObjC-1 extension. */
1281 { "objc1_property_list", obj_mach_o_objc_section
, 23}, /* ObjC-1 extension. */
1282 { "objc1_protocol_ext", obj_mach_o_objc_section
, 24}, /* ObjC-1 extension. */
1284 { "debug_frame", obj_mach_o_debug_section
, 1}, /* extension. */
1285 { "debug_info", obj_mach_o_debug_section
, 2}, /* extension. */
1286 { "debug_abbrev", obj_mach_o_debug_section
, 3}, /* extension. */
1287 { "debug_aranges", obj_mach_o_debug_section
, 4}, /* extension. */
1288 { "debug_macinfo", obj_mach_o_debug_section
, 5}, /* extension. */
1289 { "debug_line", obj_mach_o_debug_section
, 6}, /* extension. */
1290 { "debug_loc", obj_mach_o_debug_section
, 7}, /* extension. */
1291 { "debug_pubnames", obj_mach_o_debug_section
, 8}, /* extension. */
1292 { "debug_pubtypes", obj_mach_o_debug_section
, 9}, /* extension. */
1293 { "debug_str", obj_mach_o_debug_section
, 10}, /* extension. */
1294 { "debug_ranges", obj_mach_o_debug_section
, 11}, /* extension. */
1295 { "debug_macro", obj_mach_o_debug_section
, 12}, /* extension. */
1297 { "lazy_symbol_pointer", obj_mach_o_opt_tgt_section
, 1},
1298 { "lazy_symbol_pointer2", obj_mach_o_opt_tgt_section
, 2}, /* extension. */
1299 { "lazy_symbol_pointer3", obj_mach_o_opt_tgt_section
, 3}, /* extension. */
1300 { "non_lazy_symbol_pointer", obj_mach_o_opt_tgt_section
, 4},
1301 { "non_lazy_symbol_pointer_x86", obj_mach_o_opt_tgt_section
, 5}, /* extension. */
1302 { "symbol_stub", obj_mach_o_opt_tgt_section
, 6},
1303 { "symbol_stub1", obj_mach_o_opt_tgt_section
, 7}, /* extension. */
1304 { "picsymbol_stub", obj_mach_o_opt_tgt_section
, 8}, /* extension. */
1305 { "picsymbol_stub1", obj_mach_o_opt_tgt_section
, 9}, /* extension. */
1306 { "picsymbol_stub2", obj_mach_o_opt_tgt_section
, 4}, /* extension. */
1307 { "picsymbol_stub3", obj_mach_o_opt_tgt_section
, 4}, /* extension. */
1309 { "section", obj_mach_o_section
, 0},
1310 { "zerofill", obj_mach_o_zerofill
, 0},
1312 /* Symbol qualifiers. */
1313 {"local", obj_mach_o_sym_qual
, OBJ_MACH_O_SYM_LOCAL
},
1314 {"globl", obj_mach_o_sym_qual
, OBJ_MACH_O_SYM_GLOBL
},
1315 {"reference", obj_mach_o_sym_qual
, OBJ_MACH_O_SYM_REFERENCE
},
1316 {"weak_reference", obj_mach_o_sym_qual
, OBJ_MACH_O_SYM_WEAK_REF
},
1317 {"lazy_reference", obj_mach_o_sym_qual
, OBJ_MACH_O_SYM_LAZY_REF
},
1318 {"weak_definition", obj_mach_o_sym_qual
, OBJ_MACH_O_SYM_WEAK_DEF
},
1319 {"private_extern", obj_mach_o_sym_qual
, OBJ_MACH_O_SYM_PRIV_EXT
},
1320 {"no_dead_strip", obj_mach_o_sym_qual
, OBJ_MACH_O_SYM_NO_DEAD_STRIP
},
1321 {"weak", obj_mach_o_sym_qual
, OBJ_MACH_O_SYM_WEAK
}, /* ext */
1323 { "indirect_symbol", obj_mach_o_indirect_symbol
, 0},
1326 { "subsections_via_symbols", obj_mach_o_fileprop
,
1327 OBJ_MACH_O_FILE_PROP_SUBSECTS_VIA_SYMS
},
1332 /* Determine the default n_type value for a symbol from its section. */
1335 obj_mach_o_type_for_symbol (bfd_mach_o_asymbol
*s
)
1337 if (s
->symbol
.section
== bfd_abs_section_ptr
)
1338 return BFD_MACH_O_N_ABS
;
1339 else if (s
->symbol
.section
== bfd_com_section_ptr
1340 || s
->symbol
.section
== bfd_und_section_ptr
)
1341 return BFD_MACH_O_N_UNDF
;
1343 return BFD_MACH_O_N_SECT
;
1347 obj_mach_o_frob_colon (const char *name
)
1349 if (!bfd_is_local_label_name (stdoutput
, name
))
1351 /* A non-local label will create a new subsection, so start a new
1353 frag_wane (frag_now
);
1358 /* We need to check the correspondence between some kinds of symbols and their
1359 sections. Common and BSS vars will seen via the obj_macho_comm() function.
1361 The earlier we can pick up a problem, the better the diagnostics will be.
1363 However, when symbol type information is attached, the symbol section will
1364 quite possibly be unknown. So we are stuck with checking (most of the)
1365 validity at the time the file is written (unfortunately, then one doesn't
1366 get line number information in the diagnostic). */
1368 /* Here we pick up the case where symbol qualifiers have been applied that
1369 are possibly incompatible with the section etc. that the symbol is defined
1372 void obj_mach_o_frob_label (struct symbol
*sp
)
1374 bfd_mach_o_asymbol
*s
;
1376 bfd_mach_o_section
*sec
;
1379 if (!bfd_is_local_label_name (stdoutput
, S_GET_NAME (sp
)))
1381 /* If this is a non-local label, it should have started a new sub-
1383 gas_assert (frag_now
->obj_frag_data
.subsection
== NULL
);
1384 frag_now
->obj_frag_data
.subsection
= sp
;
1387 /* Leave local symbols alone. */
1389 if (S_IS_LOCAL (sp
))
1392 s
= (bfd_mach_o_asymbol
*) symbol_get_bfdsym (sp
);
1393 /* Leave debug symbols alone. */
1394 if ((s
->n_type
& BFD_MACH_O_N_STAB
) != 0)
1397 /* This is the base symbol type, that we mask in. */
1398 base_type
= obj_mach_o_type_for_symbol (s
);
1400 sec
= bfd_mach_o_get_mach_o_section (s
->symbol
.section
);
1402 sectype
= sec
->flags
& BFD_MACH_O_SECTION_TYPE_MASK
;
1404 /* If there is a pre-existing qualifier, we can make some checks about
1407 if(s
->symbol
.udata
.i
== SYM_MACHO_FIELDS_NOT_VALIDATED
)
1409 if ((s
->n_desc
& BFD_MACH_O_N_WEAK_DEF
)
1410 && sectype
!= BFD_MACH_O_S_COALESCED
)
1412 as_bad (_("'%s' can't be a weak_definition (currently only supported"
1413 " in sections of type coalesced)"), s
->symbol
.name
);
1414 /* Don't cascade errors. */
1415 s
->symbol
.udata
.i
= SYM_MACHO_FIELDS_UNSET
;
1418 /* Have we changed from an undefined to defined ref? */
1419 s
->n_desc
&= ~(REFE
| LAZY
);
1422 s
->n_type
&= ~BFD_MACH_O_N_TYPE
;
1423 s
->n_type
|= base_type
;
1426 /* This is the fall-back, we come here when we get to the end of the file and
1427 the symbol is not defined - or there are combinations of qualifiers required
1428 (e.g. global + weak_def). */
1431 obj_mach_o_frob_symbol (struct symbol
*sp
)
1433 bfd_mach_o_asymbol
*s
;
1435 bfd_mach_o_section
*sec
;
1438 /* Leave local symbols alone. */
1439 if (S_IS_LOCAL (sp
))
1442 s
= (bfd_mach_o_asymbol
*) symbol_get_bfdsym (sp
);
1443 /* Leave debug symbols alone. */
1444 if ((s
->n_type
& BFD_MACH_O_N_STAB
) != 0)
1447 base_type
= obj_mach_o_type_for_symbol (s
);
1448 sec
= bfd_mach_o_get_mach_o_section (s
->symbol
.section
);
1450 sectype
= sec
->flags
& BFD_MACH_O_SECTION_TYPE_MASK
;
1452 if (s
->symbol
.section
== bfd_und_section_ptr
)
1454 /* ??? Do we really gain much from implementing this as well as the
1455 mach-o specific ones? */
1456 if (s
->symbol
.flags
& BSF_WEAK
)
1457 s
->n_desc
|= BFD_MACH_O_N_WEAK_REF
;
1459 /* Undefined syms, become extern. */
1460 s
->n_type
|= BFD_MACH_O_N_EXT
;
1461 S_SET_EXTERNAL (sp
);
1463 else if (s
->symbol
.section
== bfd_com_section_ptr
)
1465 /* ... so do comm. */
1466 s
->n_type
|= BFD_MACH_O_N_EXT
;
1467 S_SET_EXTERNAL (sp
);
1471 if ((s
->symbol
.flags
& BSF_WEAK
)
1472 && (sectype
== BFD_MACH_O_S_COALESCED
)
1473 && (s
->n_type
& (BFD_MACH_O_N_PEXT
| BFD_MACH_O_N_EXT
)))
1474 s
->n_desc
|= BFD_MACH_O_N_WEAK_DEF
;
1475 /* ??? we should do this - but then that reveals that the semantics of weak
1476 are different from what's supported in mach-o object files.
1478 as_bad (_("'%s' can't be a weak_definition."),
1482 if (s
->symbol
.udata
.i
== SYM_MACHO_FIELDS_UNSET
)
1484 /* Anything here that should be added that is non-standard. */
1485 s
->n_desc
&= ~BFD_MACH_O_REFERENCE_MASK
;
1487 else if (s
->symbol
.udata
.i
== SYM_MACHO_FIELDS_NOT_VALIDATED
)
1489 /* Try to validate any combinations. */
1490 if (s
->n_desc
& BFD_MACH_O_N_WEAK_DEF
)
1492 if (s
->symbol
.section
== bfd_und_section_ptr
)
1493 as_bad (_("'%s' can't be a weak_definition (since it is"
1494 " undefined)"), s
->symbol
.name
);
1495 else if (sectype
!= BFD_MACH_O_S_COALESCED
)
1496 as_bad (_("'%s' can't be a weak_definition (currently only supported"
1497 " in sections of type coalesced)"), s
->symbol
.name
);
1498 else if (! (s
->n_type
& (BFD_MACH_O_N_PEXT
| BFD_MACH_O_N_EXT
)))
1499 as_bad (_("Non-global symbol: '%s' can't be a weak_definition."),
1505 as_bad (_("internal error: [%s] unexpected code [%lx] in frob symbol"),
1506 s
->symbol
.name
, (unsigned long)s
->symbol
.udata
.i
);
1508 s
->n_type
&= ~BFD_MACH_O_N_TYPE
;
1509 s
->n_type
|= base_type
;
1511 if (s
->symbol
.flags
& BSF_GLOBAL
)
1512 s
->n_type
|= BFD_MACH_O_N_EXT
;
1514 /* This cuts both ways - we promote some things to external above. */
1515 if (s
->n_type
& (BFD_MACH_O_N_PEXT
| BFD_MACH_O_N_EXT
))
1516 S_SET_EXTERNAL (sp
);
1521 /* Support stabs for mach-o. */
1524 obj_mach_o_process_stab (int what
, const char *string
,
1525 int type
, int other
, int desc
)
1528 bfd_mach_o_asymbol
*s
;
1533 symbolP
= symbol_new ("", now_seg
, frag_now_fix (), frag_now
);
1534 /* Special stabd NULL name indicator. */
1535 S_SET_NAME (symbolP
, NULL
);
1540 symbolP
= symbol_new (string
, undefined_section
, (valueT
) 0,
1541 &zero_address_frag
);
1542 pseudo_set (symbolP
);
1546 as_bad(_("unrecognized stab type '%c'"), (char)what
);
1551 s
= (bfd_mach_o_asymbol
*) symbol_get_bfdsym (symbolP
);
1554 /* For stabd, this will eventually get overwritten by the section number. */
1557 /* It's a debug symbol. */
1558 s
->symbol
.flags
|= BSF_DEBUGGING
;
1560 /* We've set it - so check it, if you can, but don't try to create the
1562 s
->symbol
.udata
.i
= SYM_MACHO_FIELDS_NOT_VALIDATED
;
1565 /* This is a place to check for any errors that we can't detect until we know
1566 what remains undefined at the end of assembly. */
1569 obj_mach_o_check_before_writing (bfd
*abfd ATTRIBUTE_UNUSED
,
1571 void *unused ATTRIBUTE_UNUSED
)
1574 struct frchain
*frchp
;
1575 segment_info_type
*seginfo
= seg_info (sec
);
1577 if (seginfo
== NULL
)
1580 /* We are not allowed subtractions where either of the operands is
1581 undefined. So look through the frags for any fixes to check. */
1582 for (frchp
= seginfo
->frchainP
; frchp
!= NULL
; frchp
= frchp
->frch_next
)
1583 for (fixP
= frchp
->fix_root
; fixP
!= NULL
; fixP
= fixP
->fx_next
)
1585 if (fixP
->fx_addsy
!= NULL
1586 && fixP
->fx_subsy
!= NULL
1587 && (! S_IS_DEFINED (fixP
->fx_addsy
)
1588 || ! S_IS_DEFINED (fixP
->fx_subsy
)))
1590 segT add_symbol_segment
= S_GET_SEGMENT (fixP
->fx_addsy
);
1591 segT sub_symbol_segment
= S_GET_SEGMENT (fixP
->fx_subsy
);
1593 if (! S_IS_DEFINED (fixP
->fx_addsy
)
1594 && S_IS_DEFINED (fixP
->fx_subsy
))
1596 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1597 _("`%s' can't be undefined in `%s' - `%s' {%s section}"),
1598 S_GET_NAME (fixP
->fx_addsy
), S_GET_NAME (fixP
->fx_addsy
),
1599 S_GET_NAME (fixP
->fx_subsy
), segment_name (sub_symbol_segment
));
1601 else if (! S_IS_DEFINED (fixP
->fx_subsy
)
1602 && S_IS_DEFINED (fixP
->fx_addsy
))
1604 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1605 _("`%s' can't be undefined in `%s' {%s section} - `%s'"),
1606 S_GET_NAME (fixP
->fx_subsy
), S_GET_NAME (fixP
->fx_addsy
),
1607 segment_name (add_symbol_segment
), S_GET_NAME (fixP
->fx_subsy
));
1611 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1612 _("`%s' and `%s' can't be undefined in `%s' - `%s'"),
1613 S_GET_NAME (fixP
->fx_addsy
), S_GET_NAME (fixP
->fx_subsy
),
1614 S_GET_NAME (fixP
->fx_addsy
), S_GET_NAME (fixP
->fx_subsy
));
1620 /* Do any checks that we can't complete without knowing what's undefined. */
1622 obj_mach_o_pre_output_hook (void)
1624 bfd_map_over_sections (stdoutput
, obj_mach_o_check_before_writing
, (char *) 0);
1627 /* Here we count up frags in each subsection (where a sub-section is defined
1628 as starting with a non-local symbol).
1629 Note that, if there are no non-local symbols in a section, all the frags will
1630 be attached as one anonymous subsection. */
1633 obj_mach_o_set_subsections (bfd
*abfd ATTRIBUTE_UNUSED
,
1635 void *unused ATTRIBUTE_UNUSED
)
1637 segment_info_type
*seginfo
= seg_info (sec
);
1638 symbolS
*cur_subsection
= NULL
;
1639 struct obj_mach_o_symbol_data
*cur_subsection_data
= NULL
;
1643 /* Protect against sections not created by gas. */
1644 if (seginfo
== NULL
)
1647 /* Attach every frag to a subsection. */
1648 for (chain
= seginfo
->frchainP
; chain
!= NULL
; chain
= chain
->frch_next
)
1649 for (frag
= chain
->frch_root
; frag
!= NULL
; frag
= frag
->fr_next
)
1651 if (frag
->obj_frag_data
.subsection
== NULL
)
1652 frag
->obj_frag_data
.subsection
= cur_subsection
;
1655 cur_subsection
= frag
->obj_frag_data
.subsection
;
1656 cur_subsection_data
= symbol_get_obj (cur_subsection
);
1657 cur_subsection_data
->subsection_size
= 0;
1659 if (cur_subsection_data
!= NULL
)
1661 /* Update subsection size. */
1662 cur_subsection_data
->subsection_size
+= frag
->fr_fix
;
1667 /* Handle mach-o subsections-via-symbols counting up frags belonging to each
1671 obj_mach_o_pre_relax_hook (void)
1673 bfd_map_over_sections (stdoutput
, obj_mach_o_set_subsections
, (char *) 0);
1676 /* Zerofill and GB Zerofill sections must be sorted to follow all other
1677 sections in their segments.
1679 The native 'as' leaves the sections physically in the order they appear in
1680 the source, and adjusts the section VMAs to meet the constraint.
1682 We follow this for now - if nothing else, it makes comparison easier.
1684 An alternative implementation would be to sort the sections as ld requires.
1685 It might be advantageous to implement such a scheme in the future (or even
1686 to make the style of section ordering user-selectable). */
1688 typedef struct obj_mach_o_set_vma_data
1692 unsigned zerofill_seen
;
1693 unsigned gb_zerofill_seen
;
1694 } obj_mach_o_set_vma_data
;
1696 /* We do (possibly) three passes through to set the vma, so that:
1698 zerofill sections get VMAs after all others in their segment
1699 GB zerofill get VMAs last.
1701 As we go, we notice if we see any Zerofill or GB Zerofill sections, so that
1702 we can skip the additional passes if there's nothing to do. */
1705 obj_mach_o_set_section_vma (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
, void *v_p
)
1707 bfd_mach_o_section
*ms
= bfd_mach_o_get_mach_o_section (sec
);
1708 unsigned bfd_align
= bfd_get_section_alignment (abfd
, sec
);
1709 obj_mach_o_set_vma_data
*p
= (struct obj_mach_o_set_vma_data
*)v_p
;
1710 unsigned sectype
= (ms
->flags
& BFD_MACH_O_SECTION_TYPE_MASK
);
1714 if (sectype
== BFD_MACH_O_S_ZEROFILL
)
1717 p
->zerofill_seen
= zf
;
1719 else if (sectype
== BFD_MACH_O_S_GB_ZEROFILL
)
1722 p
->gb_zerofill_seen
= zf
;
1725 if (p
->vma_pass
!= zf
)
1728 /* We know the section size now - so make a vma for the section just
1730 ms
->size
= bfd_get_section_size (sec
);
1732 /* Make sure that the align agrees, and set to the largest value chosen. */
1733 ms
->align
= ms
->align
> bfd_align
? ms
->align
: bfd_align
;
1734 bfd_set_section_alignment (abfd
, sec
, ms
->align
);
1736 p
->vma
+= (1 << ms
->align
) - 1;
1737 p
->vma
&= ~((1 << ms
->align
) - 1);
1739 bfd_set_section_vma (abfd
, sec
, p
->vma
);
1743 /* (potentially) three passes over the sections, setting VMA. We skip the
1744 {gb}zerofill passes if we didn't see any of the relevant sections. */
1746 void obj_mach_o_post_relax_hook (void)
1748 obj_mach_o_set_vma_data d
;
1750 memset (&d
, 0, sizeof (d
));
1752 bfd_map_over_sections (stdoutput
, obj_mach_o_set_section_vma
, (char *) &d
);
1753 if ((d
.vma_pass
= d
.zerofill_seen
) != 0)
1754 bfd_map_over_sections (stdoutput
, obj_mach_o_set_section_vma
, (char *) &d
);
1755 if ((d
.vma_pass
= d
.gb_zerofill_seen
) != 0)
1756 bfd_map_over_sections (stdoutput
, obj_mach_o_set_section_vma
, (char *) &d
);
1760 obj_mach_o_set_indirect_symbols (bfd
*abfd
, asection
*sec
,
1761 void *xxx ATTRIBUTE_UNUSED
)
1763 bfd_vma sect_size
= bfd_section_size (abfd
, sec
);
1764 bfd_mach_o_section
*ms
= bfd_mach_o_get_mach_o_section (sec
);
1767 /* See if we have any indirect syms to consider. */
1768 if (indirect_syms
== NULL
)
1771 /* Process indirect symbols.
1772 Check for errors, if OK attach them as a flat array to the section
1773 for which they are defined. */
1775 switch (ms
->flags
& BFD_MACH_O_SECTION_TYPE_MASK
)
1777 case BFD_MACH_O_S_SYMBOL_STUBS
:
1778 case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS
:
1781 case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS
:
1783 unsigned int nactual
= 0;
1785 obj_mach_o_indirect_sym
*isym
;
1786 obj_mach_o_indirect_sym
*list
= NULL
;
1787 obj_mach_o_indirect_sym
*list_tail
= NULL
;
1788 unsigned long eltsiz
=
1789 bfd_mach_o_section_get_entry_size (abfd
, ms
);
1791 for (isym
= indirect_syms
; isym
!= NULL
; isym
= isym
->next
)
1793 if (isym
->sect
== sec
)
1799 list_tail
->next
= isym
;
1804 /* If none are in this section, stop here. */
1808 /* If we somehow added indirect symbols to a section with a zero
1809 entry size, we're dead ... */
1810 gas_assert (eltsiz
!= 0);
1812 ncalc
= (unsigned int) (sect_size
/ eltsiz
);
1813 if (nactual
!= ncalc
)
1814 as_bad (_("the number of .indirect_symbols defined in section %s"
1815 " does not match the number expected (%d defined, %d"
1816 " expected)"), sec
->name
, nactual
, ncalc
);
1820 bfd_mach_o_asymbol
*sym
;
1823 nactual
* sizeof (bfd_mach_o_asymbol
*));
1825 if (ms
->indirect_syms
== NULL
)
1827 as_fatal (_("internal error: failed to allocate %d indirect"
1828 "symbol pointers"), nactual
);
1831 for (isym
= list
, n
= 0; isym
!= NULL
; isym
= isym
->next
, n
++)
1833 sym
= (bfd_mach_o_asymbol
*)symbol_get_bfdsym (isym
->sym
);
1834 /* Array is init to NULL & NULL signals a local symbol
1835 If the section is lazy-bound, we need to keep the
1836 reference to the symbol, since dyld can override.
1838 Absolute symbols are handled specially. */
1839 if (sym
->symbol
.section
== bfd_abs_section_ptr
)
1840 ms
->indirect_syms
[n
] = sym
;
1841 else if (S_IS_LOCAL (isym
->sym
) && ! lazy
)
1847 /* If the symbols is external ... */
1848 else if (S_IS_EXTERNAL (isym
->sym
)
1849 || (sym
->n_type
& BFD_MACH_O_N_EXT
)
1850 || ! S_IS_DEFINED (isym
->sym
)
1853 sym
->n_desc
&= ~LAZY
;
1854 /* ... it can be lazy, if not defined or hidden. */
1855 if ((sym
->n_type
& BFD_MACH_O_N_TYPE
)
1856 == BFD_MACH_O_N_UNDF
1857 && ! (sym
->n_type
& BFD_MACH_O_N_PEXT
)
1858 && (sym
->n_type
& BFD_MACH_O_N_EXT
))
1859 sym
->n_desc
|= lazy
;
1860 ms
->indirect_syms
[n
] = sym
;
1873 /* The process of relocation could alter what's externally visible, thus we
1874 leave setting the indirect symbols until last. */
1877 obj_mach_o_frob_file_after_relocs (void)
1879 bfd_map_over_sections (stdoutput
, obj_mach_o_set_indirect_symbols
, (char *) 0);
1882 /* Reverse relocations order to make ld happy. */
1885 obj_mach_o_reorder_section_relocs (asection
*sec
, arelent
**rels
, unsigned int n
)
1888 unsigned int max
= n
/ 2;
1890 for (i
= 0; i
< max
; i
++)
1892 arelent
*r
= rels
[i
];
1893 rels
[i
] = rels
[n
- i
- 1];
1894 rels
[n
- i
- 1] = r
;
1896 bfd_set_reloc (stdoutput
, sec
, rels
, n
);
1899 /* Relocation rules are different in frame sections. */
1902 obj_mach_o_is_frame_section (segT sec
)
1905 l
= strlen (segment_name (sec
));
1906 if ((l
== 9 && strncmp (".eh_frame", segment_name (sec
), 9) == 0)
1907 || (l
== 12 && strncmp (".debug_frame", segment_name (sec
), 12) == 0))
1912 /* Unless we're in a frame section, we need to force relocs to be generated for
1913 local subtractions. We might eliminate them later (if they are within the
1914 same sub-section) but we don't know that at the point that this decision is
1918 obj_mach_o_allow_local_subtract (expressionS
* left ATTRIBUTE_UNUSED
,
1919 expressionS
* right ATTRIBUTE_UNUSED
,
1922 /* Don't interfere if it's one of the GAS internal sections. */
1923 if (! SEG_NORMAL (seg
))
1926 /* Allow in frame sections, otherwise emit a reloc. */
1927 return obj_mach_o_is_frame_section (seg
);
1931 obj_mach_o_in_different_subsection (symbolS
*a
, symbolS
*b
)
1936 if (S_GET_SEGMENT (a
) != S_GET_SEGMENT (b
)
1937 || !S_IS_DEFINED (a
)
1938 || !S_IS_DEFINED (b
))
1940 /* Not in the same segment, or undefined symbol. */
1944 fa
= symbol_get_frag (a
);
1945 fb
= symbol_get_frag (b
);
1946 if (fa
== NULL
|| fb
== NULL
)
1948 /* One of the symbols is not in a subsection. */
1952 return fa
->obj_frag_data
.subsection
!= fb
->obj_frag_data
.subsection
;
1956 obj_mach_o_force_reloc_sub_same (fixS
*fix
, segT seg
)
1958 if (! SEG_NORMAL (seg
))
1960 return obj_mach_o_in_different_subsection (fix
->fx_addsy
, fix
->fx_subsy
);
1964 obj_mach_o_force_reloc_sub_local (fixS
*fix
, segT seg ATTRIBUTE_UNUSED
)
1966 return obj_mach_o_in_different_subsection (fix
->fx_addsy
, fix
->fx_subsy
);
1970 obj_mach_o_force_reloc (fixS
*fix
)
1972 if (generic_force_reloc (fix
))
1975 /* Force a reloc if the target is not in the same subsection.
1976 FIXME: handle (a - b) where a and b belongs to the same subsection ? */
1977 if (fix
->fx_addsy
!= NULL
)
1979 symbolS
*subsec
= fix
->fx_frag
->obj_frag_data
.subsection
;
1980 symbolS
*targ
= fix
->fx_addsy
;
1982 /* There might be no subsections at all. */
1986 if (S_GET_SEGMENT (targ
) == absolute_section
)
1989 return obj_mach_o_in_different_subsection (targ
, subsec
);