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"
49 static segT
obj_mach_o_segT_from_bfd_name (const char *, int);
51 /* TODO: Implement "-dynamic"/"-static" command line options. */
53 static int obj_mach_o_is_static
;
55 /* TODO: Implement the "-n" command line option to suppress the initial
56 switch to the text segment. */
57 static int obj_mach_o_start_with_text_section
= 1;
59 /* Allow for special re-ordering on output. */
61 static int obj_mach_o_seen_objc_section
;
63 /* Start-up: At present, just create the sections we want. */
67 /* Mach-O only defines the .text section by default, and even this can
68 be suppressed by a flag. In the latter event, the first code MUST
69 be a section definition. */
70 if (obj_mach_o_start_with_text_section
)
72 text_section
= obj_mach_o_segT_from_bfd_name (TEXT_SECTION_NAME
, 1);
73 subseg_set (text_section
, 0);
74 if (obj_mach_o_is_static
)
76 bfd_mach_o_section
*mo_sec
77 = bfd_mach_o_get_mach_o_section (text_section
);
78 mo_sec
->flags
&= ~BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS
;
83 /* Remember the subsections_by_symbols state in case we need to reset
86 static int obj_mach_o_subsections_by_symbols
;
88 /* This will put at most 16 characters (terminated by a ',' or newline) from
89 the input stream into dest. If there are more than 16 chars before the
90 delimiter, a warning is given and the string is truncated. On completion of
91 this function, input_line_pointer will point to the char after the ',' or
94 It trims leading and trailing space. */
97 collect_16char_name (char *dest
, const char *msg
, int require_comma
)
102 namstart
= input_line_pointer
;
104 while ( (c
= *input_line_pointer
) != ','
105 && !is_end_of_line
[(unsigned char) c
])
106 input_line_pointer
++;
109 int len
= input_line_pointer
- namstart
; /* could be zero. */
110 /* lose any trailing space. */
111 while (len
> 0 && namstart
[len
-1] == ' ')
115 *input_line_pointer
= '\0'; /* make a temp string. */
116 as_bad (_("the %s name '%s' is too long (maximum 16 characters)"),
118 *input_line_pointer
= c
; /* restore for printing. */
122 memcpy (dest
, namstart
, len
);
125 if (c
!= ',' && require_comma
)
127 as_bad (_("expected a %s name followed by a `,'"), msg
);
135 obj_mach_o_get_section_names (char *seg
, char *sec
,
136 unsigned segl
, unsigned secl
)
138 /* Zero-length segment and section names are allowed. */
139 /* Parse segment name. */
140 memset (seg
, 0, segl
);
141 if (collect_16char_name (seg
, "segment", 1))
143 ignore_rest_of_line ();
146 input_line_pointer
++; /* Skip the terminating ',' */
148 /* Parse section name, which can be empty. */
149 memset (sec
, 0, secl
);
150 collect_16char_name (sec
, "section", 0);
154 /* Build (or get) a section from the mach-o description - which includes
155 optional definitions for type, attributes, alignment and stub size.
157 BFD supplies default values for sections which have a canonical name. */
159 #define SECT_TYPE_SPECIFIED 0x0001
160 #define SECT_ATTR_SPECIFIED 0x0002
161 #define SECT_ALGN_SPECIFIED 0x0004
164 obj_mach_o_make_or_get_sect (char * segname
, char * sectname
,
165 unsigned int specified_mask
,
166 unsigned int usectype
, unsigned int usecattr
,
167 unsigned int ualign
, offsetT stub_size
)
169 unsigned int sectype
, secattr
, secalign
;
170 flagword oldflags
, flags
;
173 bfd_mach_o_section
*msect
;
174 const mach_o_section_name_xlat
*xlat
;
176 /* This provides default bfd flags and default mach-o section type and
177 attributes along with the canonical name. */
178 xlat
= bfd_mach_o_section_data_for_mach_sect (stdoutput
, segname
, sectname
);
180 /* TODO: more checking of whether overides are acually allowed. */
184 name
= xstrdup (xlat
->bfd_name
);
185 sectype
= xlat
->macho_sectype
;
186 if (specified_mask
& SECT_TYPE_SPECIFIED
)
188 if ((sectype
== BFD_MACH_O_S_ZEROFILL
189 || sectype
== BFD_MACH_O_S_GB_ZEROFILL
)
190 && sectype
!= usectype
)
191 as_bad (_("cannot overide zerofill section type for `%s,%s'"),
196 secattr
= xlat
->macho_secattr
;
197 secalign
= xlat
->sectalign
;
198 flags
= xlat
->bfd_flags
;
202 /* There is no normal BFD section name for this section. Create one.
203 The name created doesn't really matter as it will never be written
205 size_t seglen
= strlen (segname
);
206 size_t sectlen
= strlen (sectname
);
209 n
= xmalloc (seglen
+ 1 + sectlen
+ 1);
210 memcpy (n
, segname
, seglen
);
212 memcpy (n
+ seglen
+ 1, sectname
, sectlen
);
213 n
[seglen
+ 1 + sectlen
] = 0;
215 if (specified_mask
& SECT_TYPE_SPECIFIED
)
218 sectype
= BFD_MACH_O_S_REGULAR
;
219 secattr
= BFD_MACH_O_S_ATTR_NONE
;
221 flags
= SEC_NO_FLAGS
;
224 /* For now, just use what the user provided. */
226 if (specified_mask
& SECT_ATTR_SPECIFIED
)
229 if (specified_mask
& SECT_ALGN_SPECIFIED
)
232 /* Sub-segments don't exists as is on Mach-O. */
233 sec
= subseg_new (name
, 0);
235 oldflags
= bfd_get_section_flags (stdoutput
, sec
);
236 msect
= bfd_mach_o_get_mach_o_section (sec
);
238 if (oldflags
== SEC_NO_FLAGS
)
240 /* New, so just use the defaults or what's specified. */
241 if (! bfd_set_section_flags (stdoutput
, sec
, flags
))
242 as_warn (_("failed to set flags for \"%s\": %s"),
243 bfd_section_name (stdoutput
, sec
),
244 bfd_errmsg (bfd_get_error ()));
246 strncpy (msect
->segname
, segname
, sizeof (msect
->segname
));
247 strncpy (msect
->sectname
, sectname
, sizeof (msect
->sectname
));
249 msect
->align
= secalign
;
250 msect
->flags
= sectype
| secattr
;
251 msect
->reserved2
= stub_size
;
253 if (sectype
== BFD_MACH_O_S_ZEROFILL
254 || sectype
== BFD_MACH_O_S_GB_ZEROFILL
)
255 seg_info (sec
)->bss
= 1;
257 else if (flags
!= SEC_NO_FLAGS
)
259 if (flags
!= oldflags
260 || msect
->flags
!= (secattr
| sectype
))
261 as_warn (_("Ignoring changed section attributes for %s"), name
);
269 The '.section' specification syntax looks like:
270 .section <segment> , <section> [, type [, attribs [, size]]]
272 White space is allowed everywhere between elements.
274 <segment> and <section> may be from 0 to 16 chars in length - they may
275 contain spaces but leading and trailing space will be trimmed. It is
276 mandatory that they be present (or that zero-length names are indicated
279 There is only a single section type for any entry.
281 There may be multiple attributes, they are delimited by `+'.
283 Not all section types and attributes are accepted by the Darwin system
284 assemblers as user-specifiable - although, at present, we do here. */
287 obj_mach_o_section (int ignore ATTRIBUTE_UNUSED
)
289 unsigned int sectype
= BFD_MACH_O_S_REGULAR
;
290 unsigned int specified_mask
= 0;
291 unsigned int secattr
= 0;
292 offsetT sizeof_stub
= 0;
297 #ifdef md_flush_pending_output
298 md_flush_pending_output ();
301 /* Get the User's segment annd section names. */
302 if (! obj_mach_o_get_section_names (segname
, sectname
, 17, 17))
305 /* Parse section type, if present. */
306 if (*input_line_pointer
== ',')
312 input_line_pointer
++;
314 p
= input_line_pointer
;
315 while ((c
= *input_line_pointer
) != ','
316 && !is_end_of_line
[(unsigned char) c
])
317 input_line_pointer
++;
319 len
= input_line_pointer
- p
;
320 /* strip trailing spaces. */
321 while (len
> 0 && p
[len
-1] == ' ')
325 /* Temporarily make a string from the token. */
327 sectype
= bfd_mach_o_get_section_type_from_name (stdoutput
, p
);
328 if (sectype
> 255) /* Max Section ID == 255. */
330 as_bad (_("unknown or invalid section type '%s'"), p
);
332 ignore_rest_of_line ();
336 specified_mask
|= SECT_TYPE_SPECIFIED
;
341 TODO: check validity of attributes for section type. */
342 if ((specified_mask
& SECT_TYPE_SPECIFIED
)
349 /* Skip initial `,' and subsequent `+'. */
350 input_line_pointer
++;
352 p
= input_line_pointer
;
353 while ((c
= *input_line_pointer
) != '+'
355 && !is_end_of_line
[(unsigned char) c
])
356 input_line_pointer
++;
358 len
= input_line_pointer
- p
;
359 /* strip trailing spaces. */
360 while (len
> 0 && p
[len
-1] == ' ')
364 /* Temporarily make a string from the token. */
366 attr
= bfd_mach_o_get_section_attribute_from_name (p
);
369 as_bad (_("unknown or invalid section attribute '%s'"), p
);
371 ignore_rest_of_line ();
376 specified_mask
|= SECT_ATTR_SPECIFIED
;
382 while (*input_line_pointer
== '+');
384 /* Parse sizeof_stub. */
385 if ((specified_mask
& SECT_ATTR_SPECIFIED
)
386 && *input_line_pointer
== ',')
388 if (sectype
!= BFD_MACH_O_S_SYMBOL_STUBS
)
390 as_bad (_("unexpected section size information"));
391 ignore_rest_of_line ();
395 input_line_pointer
++;
396 sizeof_stub
= get_absolute_expression ();
398 else if ((specified_mask
& SECT_ATTR_SPECIFIED
)
399 && sectype
== BFD_MACH_O_S_SYMBOL_STUBS
)
401 as_bad (_("missing sizeof_stub expression"));
402 ignore_rest_of_line ();
408 new_seg
= obj_mach_o_make_or_get_sect (segname
, sectname
, specified_mask
,
409 sectype
, secattr
, 0 /*align */,
413 subseg_set (new_seg
, 0);
414 demand_empty_rest_of_line ();
418 /* .zerofill segname, sectname [, symbolname, size [, align]]
420 Zerofill switches, temporarily, to a sect of type 'zerofill'.
422 If a variable name is given, it defines that in the section.
423 Otherwise it just creates the section if it doesn't exist. */
426 obj_mach_o_zerofill (int ignore ATTRIBUTE_UNUSED
)
430 segT old_seg
= now_seg
;
433 unsigned int align
= 0;
434 unsigned int specified_mask
= 0;
437 #ifdef md_flush_pending_output
438 md_flush_pending_output ();
441 /* Get the User's segment annd section names. */
442 if (! obj_mach_o_get_section_names (segname
, sectname
, 17, 17))
445 /* Parse variable definition, if present. */
446 if (*input_line_pointer
== ',')
448 /* Parse symbol, size [.align]
449 We follow the method of s_common_internal, with the difference
450 that the symbol cannot be a duplicate-common. */
456 input_line_pointer
++; /* Skip ',' */
458 name
= input_line_pointer
;
459 c
= get_symbol_end ();
460 /* Just after name is now '\0'. */
461 p
= input_line_pointer
;
466 as_bad (_("expected symbol name"));
467 ignore_rest_of_line ();
472 if (*input_line_pointer
== ',')
473 input_line_pointer
++;
475 expression_and_evaluate (&exp
);
476 if (exp
.X_op
!= O_constant
477 && exp
.X_op
!= O_absent
)
479 as_bad (_("bad or irreducible absolute expression"));
480 ignore_rest_of_line ();
483 else if (exp
.X_op
== O_absent
)
485 as_bad (_("missing size expression"));
486 ignore_rest_of_line ();
490 size
= exp
.X_add_number
;
491 size
&= ((offsetT
) 2 << (stdoutput
->arch_info
->bits_per_address
- 1)) - 1;
492 if (exp
.X_add_number
!= size
|| !exp
.X_unsigned
)
494 as_warn (_("size (%ld) out of range, ignored"),
495 (long) exp
.X_add_number
);
496 ignore_rest_of_line ();
500 *p
= 0; /* Make the name into a c string for err messages. */
501 sym
= symbol_find_or_make (name
);
502 if (S_IS_DEFINED (sym
) || symbol_equated_p (sym
))
504 as_bad (_("symbol `%s' is already defined"), name
);
506 ignore_rest_of_line ();
510 size
= S_GET_VALUE (sym
);
512 size
= exp
.X_add_number
;
513 else if (size
!= exp
.X_add_number
)
514 as_warn (_("size of \"%s\" is already %ld; not changing to %ld"),
515 name
, (long) size
, (long) exp
.X_add_number
);
517 *p
= c
; /* Restore the termination char. */
520 if (*input_line_pointer
== ',')
522 align
= (unsigned int) parse_align (0);
523 if (align
== (unsigned int) -1)
525 as_warn (_("align value not recognized, using size"));
530 as_warn (_("Alignment (%lu) too large: 15 assumed."),
531 (unsigned long)align
);
534 specified_mask
|= SECT_ALGN_SPECIFIED
;
537 /* else just a section definition. */
539 specified_mask
|= SECT_TYPE_SPECIFIED
;
540 new_seg
= obj_mach_o_make_or_get_sect (segname
, sectname
, specified_mask
,
541 BFD_MACH_O_S_ZEROFILL
,
542 BFD_MACH_O_S_ATTR_NONE
,
543 align
, (offsetT
) 0 /*stub size*/);
547 /* In case the user specifies the bss section by mach-o name.
548 Create it on demand */
549 if (strcmp (new_seg
->name
, BSS_SECTION_NAME
) == 0
550 && bss_section
== NULL
)
551 bss_section
= new_seg
;
553 subseg_set (new_seg
, 0);
561 record_alignment (new_seg
, align
);
562 frag_align (align
, 0, 0);
565 /* Detach from old frag. */
566 if (S_GET_SEGMENT (sym
) == new_seg
)
567 symbol_get_frag (sym
)->fr_symbol
= NULL
;
569 symbol_set_frag (sym
, frag_now
);
570 pfrag
= frag_var (rs_org
, 1, 1, 0, sym
, size
, NULL
);
573 S_SET_SEGMENT (sym
, new_seg
);
574 if (new_seg
== bss_section
)
575 S_CLEAR_EXTERNAL (sym
);
579 /* switch back to the section that was current before the .zerofill. */
580 subseg_set (old_seg
, 0);
584 obj_mach_o_segT_from_bfd_name (const char *nam
, int must_succeed
)
586 const mach_o_section_name_xlat
*xlat
;
590 /* BFD has tables of flags and default attributes for all the sections that
591 have a 'canonical' name. */
592 xlat
= bfd_mach_o_section_data_for_bfd_name (stdoutput
, nam
, &segn
);
596 as_fatal (_("BFD is out of sync with GAS, "
597 "unhandled well-known section type `%s'"), nam
);
601 sec
= bfd_get_section_by_name (stdoutput
, nam
);
604 bfd_mach_o_section
*msect
;
606 sec
= subseg_force_new (xlat
->bfd_name
, 0);
608 /* Set default type, attributes and alignment. */
609 msect
= bfd_mach_o_get_mach_o_section (sec
);
610 msect
->flags
= xlat
->macho_sectype
| xlat
->macho_secattr
;
611 msect
->align
= xlat
->sectalign
;
613 if ((msect
->flags
& BFD_MACH_O_SECTION_TYPE_MASK
)
614 == BFD_MACH_O_S_ZEROFILL
)
615 seg_info (sec
)->bss
= 1;
621 static const char * const known_sections
[] =
626 /* 2 */ ".static_const",
630 /* 6 */ ".literal16",
631 /* 7 */ ".constructor",
632 /* 8 */ ".destructor",
635 /* 10 */ ".const_data",
636 /* 11 */ ".static_data",
637 /* 12 */ ".mod_init_func",
638 /* 13 */ ".mod_term_func",
643 /* Interface for a known non-optional section directive. */
646 obj_mach_o_known_section (int sect_index
)
650 #ifdef md_flush_pending_output
651 md_flush_pending_output ();
654 section
= obj_mach_o_segT_from_bfd_name (known_sections
[sect_index
], 1);
656 subseg_set (section
, 0);
658 /* else, we leave the section as it was; there was a fatal error anyway. */
661 static const char * const objc_sections
[] =
664 /* 1 */ ".objc_class",
665 /* 2 */ ".objc_meta_class",
666 /* 3 */ ".objc_cat_cls_meth",
667 /* 4 */ ".objc_cat_inst_meth",
668 /* 5 */ ".objc_protocol",
669 /* 6 */ ".objc_string_object",
670 /* 7 */ ".objc_cls_meth",
671 /* 8 */ ".objc_inst_meth",
672 /* 9 */ ".objc_cls_refs",
673 /* 10 */ ".objc_message_refs",
674 /* 11 */ ".objc_symbols",
675 /* 12 */ ".objc_category",
676 /* 13 */ ".objc_class_vars",
677 /* 14 */ ".objc_instance_vars",
678 /* 15 */ ".objc_module_info",
679 /* 16 */ ".cstring", /* objc_class_names Alias for .cstring */
680 /* 17 */ ".cstring", /* Alias objc_meth_var_types for .cstring */
681 /* 18 */ ".cstring", /* objc_meth_var_names Alias for .cstring */
682 /* 19 */ ".objc_selector_strs",
683 /* 20 */ ".objc_image_info", /* extension. */
684 /* 21 */ ".objc_selector_fixup", /* extension. */
685 /* 22 */ ".objc1_class_ext", /* ObjC-1 extension. */
686 /* 23 */ ".objc1_property_list", /* ObjC-1 extension. */
687 /* 24 */ ".objc1_protocol_ext" /* ObjC-1 extension. */
690 /* This currently does the same as known_sections, but kept separate for
691 ease of maintenance. */
694 obj_mach_o_objc_section (int sect_index
)
698 #ifdef md_flush_pending_output
699 md_flush_pending_output ();
702 section
= obj_mach_o_segT_from_bfd_name (objc_sections
[sect_index
], 1);
705 obj_mach_o_seen_objc_section
= 1; /* We need to ensure that certain
706 sections are present and in the
708 subseg_set (section
, 0);
711 /* else, we leave the section as it was; there was a fatal error anyway. */
714 /* Debug section directives. */
716 static const char * const debug_sections
[] =
720 /* 1 */ ".debug_frame",
721 /* 2 */ ".debug_info",
722 /* 3 */ ".debug_abbrev",
723 /* 4 */ ".debug_aranges",
724 /* 5 */ ".debug_macinfo",
725 /* 6 */ ".debug_line",
726 /* 7 */ ".debug_loc",
727 /* 8 */ ".debug_pubnames",
728 /* 9 */ ".debug_pubtypes",
729 /* 10 */ ".debug_str",
730 /* 11 */ ".debug_ranges",
731 /* 12 */ ".debug_macro"
734 /* ??? Maybe these should be conditional on gdwarf-*.
735 It`s also likely that we will need to be able to set them from the cfi
739 obj_mach_o_debug_section (int sect_index
)
743 #ifdef md_flush_pending_output
744 md_flush_pending_output ();
747 section
= obj_mach_o_segT_from_bfd_name (debug_sections
[sect_index
], 1);
749 subseg_set (section
, 0);
751 /* else, we leave the section as it was; there was a fatal error anyway. */
754 /* This could be moved to the tc-xx files, but there is so little dependency
755 there, that the code might as well be shared. */
764 /* The extensions here are for specific sections that are generated by GCC
765 and Darwin system tools, but don't have directives in the `system as'. */
767 static const struct opt_tgt_sect tgt_sections
[] =
769 /* 0 */ { NULL
, 0, 0},
770 /* 1 */ { ".lazy_symbol_pointer", 0, 0},
771 /* 2 */ { ".lazy_symbol_pointer2", 0, 0}, /* X86 - extension */
772 /* 3 */ { ".lazy_symbol_pointer3", 0, 0}, /* X86 - extension */
773 /* 4 */ { ".non_lazy_symbol_pointer", 0, 0},
774 /* 5 */ { ".non_lazy_symbol_pointer_x86", 0, 0}, /* X86 - extension */
775 /* 6 */ { ".symbol_stub", 16, 20},
776 /* 7 */ { ".symbol_stub1", 0, 16}, /* PPC - extension */
777 /* 8 */ { ".picsymbol_stub", 26, 36},
778 /* 9 */ { ".picsymbol_stub1", 0, 32}, /* PPC - extension */
779 /* 10 */ { ".picsymbol_stub2", 25, 0}, /* X86 - extension */
780 /* 11 */ { ".picsymbol_stub3", 5, 0}, /* X86 - extension */
783 /* Interface for an optional section directive. */
786 obj_mach_o_opt_tgt_section (int sect_index
)
788 const struct opt_tgt_sect
*tgtsct
= &tgt_sections
[sect_index
];
791 #ifdef md_flush_pending_output
792 md_flush_pending_output ();
795 section
= obj_mach_o_segT_from_bfd_name (tgtsct
->name
, 0);
798 as_bad (_("%s is not used for the selected target"), tgtsct
->name
);
799 /* Leave the section as it is. */
803 bfd_mach_o_section
*mo_sec
= bfd_mach_o_get_mach_o_section (section
);
804 subseg_set (section
, 0);
805 #if defined (TC_I386)
806 mo_sec
->reserved2
= tgtsct
->x86_val
;
807 #elif defined (TC_PPC)
808 mo_sec
->reserved2
= tgtsct
->ppc_val
;
810 mo_sec
->reserved2
= 0;
815 /* We don't necessarily have the three 'base' sections on mach-o.
816 Normally, we would start up with only the 'text' section defined.
817 However, even that can be suppressed with (TODO) c/l option "-n".
818 Thus, we have to be able to create all three sections on-demand. */
821 obj_mach_o_base_section (int sect_index
)
825 #ifdef md_flush_pending_output
826 md_flush_pending_output ();
829 /* We don't support numeric (or any other) qualifications on the
830 well-known section shorthands. */
831 demand_empty_rest_of_line ();
835 /* Handle the three sections that are globally known within GAS.
836 For Mach-O, these are created on demand rather than at startup. */
838 if (text_section
== NULL
)
839 text_section
= obj_mach_o_segT_from_bfd_name (TEXT_SECTION_NAME
, 1);
840 if (obj_mach_o_is_static
)
842 bfd_mach_o_section
*mo_sec
843 = bfd_mach_o_get_mach_o_section (text_section
);
844 mo_sec
->flags
&= ~BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS
;
846 section
= text_section
;
849 if (data_section
== NULL
)
850 data_section
= obj_mach_o_segT_from_bfd_name (DATA_SECTION_NAME
, 1);
851 section
= data_section
;
854 /* ??? maybe this achieves very little, as an addition. */
855 if (bss_section
== NULL
)
857 bss_section
= obj_mach_o_segT_from_bfd_name (BSS_SECTION_NAME
, 1);
858 seg_info (bss_section
)->bss
= 1;
860 section
= bss_section
;
863 as_fatal (_("internal error: base section index out of range"));
867 subseg_set (section
, 0);
870 /* This finishes off parsing a .comm or .lcomm statement, which both can have
871 an (optional) alignment field. It also allows us to create the bss section
875 obj_mach_o_common_parse (int is_local
, symbolS
*symbolP
,
879 bfd_mach_o_asymbol
*s
;
883 /* Both comm and lcomm take an optional alignment, as a power
884 of two between 1 and 15. */
885 if (*input_line_pointer
== ',')
887 /* We expect a power of 2. */
888 align
= parse_align (0);
889 if (align
== (addressT
) -1)
893 as_warn (_("Alignment (%lu) too large: 15 assumed."),
894 (unsigned long)align
);
899 s
= (bfd_mach_o_asymbol
*) symbol_get_bfdsym (symbolP
);
902 /* Create the BSS section on demand. */
903 if (bss_section
== NULL
)
905 bss_section
= obj_mach_o_segT_from_bfd_name (BSS_SECTION_NAME
, 1);
906 seg_info (bss_section
)->bss
= 1;
908 bss_alloc (symbolP
, size
, align
);
909 s
->n_type
= BFD_MACH_O_N_SECT
;
910 S_CLEAR_EXTERNAL (symbolP
);
914 S_SET_VALUE (symbolP
, size
);
915 S_SET_ALIGN (symbolP
, align
);
916 S_SET_EXTERNAL (symbolP
);
917 S_SET_SEGMENT (symbolP
, bfd_com_section_ptr
);
918 s
->n_type
= BFD_MACH_O_N_UNDF
| BFD_MACH_O_N_EXT
;
921 /* This is a data object (whatever we choose that to mean). */
922 s
->symbol
.flags
|= BSF_OBJECT
;
924 /* We've set symbol qualifiers, so validate if you can. */
925 s
->symbol
.udata
.i
= SYM_MACHO_FIELDS_NOT_VALIDATED
;
931 obj_mach_o_comm (int is_local
)
933 s_comm_internal (is_local
, obj_mach_o_common_parse
);
936 /* Set properties that apply to the whole file. At present, the only
937 one defined, is subsections_via_symbols. */
939 typedef enum obj_mach_o_file_properties
{
940 OBJ_MACH_O_FILE_PROP_NONE
= 0,
941 OBJ_MACH_O_FILE_PROP_SUBSECTS_VIA_SYMS
,
942 OBJ_MACH_O_FILE_PROP_MAX
943 } obj_mach_o_file_properties
;
946 obj_mach_o_fileprop (int prop
)
948 if (prop
< 0 || prop
>= OBJ_MACH_O_FILE_PROP_MAX
)
949 as_fatal (_("internal error: bad file property ID %d"), prop
);
951 switch ((obj_mach_o_file_properties
) prop
)
953 case OBJ_MACH_O_FILE_PROP_SUBSECTS_VIA_SYMS
:
954 obj_mach_o_subsections_by_symbols
= 1;
955 if (!bfd_set_private_flags (stdoutput
,
956 BFD_MACH_O_MH_SUBSECTIONS_VIA_SYMBOLS
))
957 as_bad (_("failed to set subsections by symbols"));
958 demand_empty_rest_of_line ();
965 /* Temporary markers for symbol reference data.
966 Lazy will remain in place. */
970 /* We have a bunch of qualifiers that may be applied to symbols.
971 .globl is handled here so that we might make sure that conflicting qualifiers
972 are caught where possible. */
974 typedef enum obj_mach_o_symbol_type
{
975 OBJ_MACH_O_SYM_UNK
= 0,
976 OBJ_MACH_O_SYM_LOCAL
= 1,
977 OBJ_MACH_O_SYM_GLOBL
= 2,
978 OBJ_MACH_O_SYM_REFERENCE
= 3,
979 OBJ_MACH_O_SYM_WEAK_REF
= 4,
980 OBJ_MACH_O_SYM_LAZY_REF
= 5,
981 OBJ_MACH_O_SYM_WEAK_DEF
= 6,
982 OBJ_MACH_O_SYM_PRIV_EXT
= 7,
983 OBJ_MACH_O_SYM_NO_DEAD_STRIP
= 8,
984 OBJ_MACH_O_SYM_WEAK
= 9
985 } obj_mach_o_symbol_type
;
987 /* Set Mach-O-specific symbol qualifiers. */
990 obj_mach_o_set_symbol_qualifier (symbolS
*sym
, int type
)
993 bfd_mach_o_asymbol
*s
= (bfd_mach_o_asymbol
*) symbol_get_bfdsym (sym
);
994 bfd_mach_o_section
*sec
;
998 /* If the symbol is defined, then we can do more rigorous checking on
999 the validity of the qualifiers. Otherwise, we are stuck with waiting
1000 until it's defined - or until write the file.
1002 In certain cases (e.g. when a symbol qualifier is intended to introduce
1003 an undefined symbol in a stubs section) we should check that the current
1004 section is appropriate to the qualifier. */
1006 is_defined
= s
->symbol
.section
!= bfd_und_section_ptr
;
1008 sec
= bfd_mach_o_get_mach_o_section (s
->symbol
.section
) ;
1010 sec
= bfd_mach_o_get_mach_o_section (now_seg
) ;
1013 sectype
= sec
->flags
& BFD_MACH_O_SECTION_TYPE_MASK
;
1015 switch ((obj_mach_o_symbol_type
) type
)
1017 case OBJ_MACH_O_SYM_LOCAL
:
1018 /* This is an extension over the system tools. */
1019 if (s
->n_type
& (BFD_MACH_O_N_PEXT
| BFD_MACH_O_N_EXT
))
1021 as_bad (_("'%s' previously declared as '%s'."), s
->symbol
.name
,
1022 (s
->n_type
& BFD_MACH_O_N_PEXT
) ? "private extern"
1028 s
->n_type
&= ~BFD_MACH_O_N_EXT
;
1029 S_CLEAR_EXTERNAL (sym
);
1033 case OBJ_MACH_O_SYM_PRIV_EXT
:
1034 s
->n_type
|= BFD_MACH_O_N_PEXT
;
1035 /* We follow the system tools in marking PEXT as also global. */
1038 case OBJ_MACH_O_SYM_GLOBL
:
1039 /* It's not an error to define a symbol and then make it global. */
1040 s
->n_type
|= BFD_MACH_O_N_EXT
;
1041 S_SET_EXTERNAL (sym
);
1044 case OBJ_MACH_O_SYM_REFERENCE
:
1046 s
->n_desc
|= BFD_MACH_O_N_NO_DEAD_STRIP
;
1048 s
->n_desc
|= (REFE
| BFD_MACH_O_N_NO_DEAD_STRIP
);
1051 case OBJ_MACH_O_SYM_LAZY_REF
:
1053 s
->n_desc
|= BFD_MACH_O_N_NO_DEAD_STRIP
;
1055 s
->n_desc
|= (REFE
| LAZY
| BFD_MACH_O_N_NO_DEAD_STRIP
);
1058 /* Force ld to retain the symbol - even if it appears unused. */
1059 case OBJ_MACH_O_SYM_NO_DEAD_STRIP
:
1060 s
->n_desc
|= BFD_MACH_O_N_NO_DEAD_STRIP
;
1063 /* Mach-O's idea of weak ... */
1064 case OBJ_MACH_O_SYM_WEAK_REF
:
1065 s
->n_desc
|= BFD_MACH_O_N_WEAK_REF
;
1068 case OBJ_MACH_O_SYM_WEAK_DEF
:
1069 if (is_defined
&& sectype
!= BFD_MACH_O_S_COALESCED
)
1071 as_bad (_("'%s' can't be a weak_definition (currently only"
1072 " supported in sections of type coalesced)"),
1077 s
->n_desc
|= BFD_MACH_O_N_WEAK_DEF
;
1080 case OBJ_MACH_O_SYM_WEAK
:
1081 /* A generic 'weak' - we try to figure out what it means at
1082 symbol frob time. */
1090 /* We've seen some kind of qualifier - check validity if or when the entity
1092 s
->symbol
.udata
.i
= SYM_MACHO_FIELDS_NOT_VALIDATED
;
1096 /* Respond to symbol qualifiers.
1098 .<qualifier> symbol [, symbol]*
1099 a list of symbols is an extension over the Darwin system as. */
1102 obj_mach_o_sym_qual (int ntype
)
1108 #ifdef md_flush_pending_output
1109 md_flush_pending_output ();
1114 name
= input_line_pointer
;
1115 c
= get_symbol_end ();
1116 symbolP
= symbol_find_or_make (name
);
1117 obj_mach_o_set_symbol_qualifier (symbolP
, ntype
);
1118 *input_line_pointer
= c
;
1120 c
= *input_line_pointer
;
1123 input_line_pointer
++;
1125 if (is_end_of_line
[(unsigned char) *input_line_pointer
])
1131 demand_empty_rest_of_line ();
1134 /* Dummy function to allow test-code to work while we are working
1138 obj_mach_o_placeholder (int arg ATTRIBUTE_UNUSED
)
1140 ignore_rest_of_line ();
1143 const pseudo_typeS mach_o_pseudo_table
[] =
1145 /* Section directives. */
1146 { "comm", obj_mach_o_comm
, 0 },
1147 { "lcomm", obj_mach_o_comm
, 1 },
1149 { "text", obj_mach_o_base_section
, 1},
1150 { "data", obj_mach_o_base_section
, 2},
1151 { "bss", obj_mach_o_base_section
, 3}, /* extension */
1153 { "const", obj_mach_o_known_section
, 1},
1154 { "static_const", obj_mach_o_known_section
, 2},
1155 { "cstring", obj_mach_o_known_section
, 3},
1156 { "literal4", obj_mach_o_known_section
, 4},
1157 { "literal8", obj_mach_o_known_section
, 5},
1158 { "literal16", obj_mach_o_known_section
, 6},
1159 { "constructor", obj_mach_o_known_section
, 7},
1160 { "destructor", obj_mach_o_known_section
, 8},
1161 { "eh_frame", obj_mach_o_known_section
, 9},
1163 { "const_data", obj_mach_o_known_section
, 10},
1164 { "static_data", obj_mach_o_known_section
, 11},
1165 { "mod_init_func", obj_mach_o_known_section
, 12},
1166 { "mod_term_func", obj_mach_o_known_section
, 13},
1167 { "dyld", obj_mach_o_known_section
, 14},
1168 { "cfstring", obj_mach_o_known_section
, 15},
1170 { "objc_class", obj_mach_o_objc_section
, 1},
1171 { "objc_meta_class", obj_mach_o_objc_section
, 2},
1172 { "objc_cat_cls_meth", obj_mach_o_objc_section
, 3},
1173 { "objc_cat_inst_meth", obj_mach_o_objc_section
, 4},
1174 { "objc_protocol", obj_mach_o_objc_section
, 5},
1175 { "objc_string_object", obj_mach_o_objc_section
, 6},
1176 { "objc_cls_meth", obj_mach_o_objc_section
, 7},
1177 { "objc_inst_meth", obj_mach_o_objc_section
, 8},
1178 { "objc_cls_refs", obj_mach_o_objc_section
, 9},
1179 { "objc_message_refs", obj_mach_o_objc_section
, 10},
1180 { "objc_symbols", obj_mach_o_objc_section
, 11},
1181 { "objc_category", obj_mach_o_objc_section
, 12},
1182 { "objc_class_vars", obj_mach_o_objc_section
, 13},
1183 { "objc_instance_vars", obj_mach_o_objc_section
, 14},
1184 { "objc_module_info", obj_mach_o_objc_section
, 15},
1185 { "objc_class_names", obj_mach_o_objc_section
, 16}, /* Alias for .cstring */
1186 { "objc_meth_var_types", obj_mach_o_objc_section
, 17}, /* Alias for .cstring */
1187 { "objc_meth_var_names", obj_mach_o_objc_section
, 18}, /* Alias for .cstring */
1188 { "objc_selector_strs", obj_mach_o_objc_section
, 19},
1189 { "objc_image_info", obj_mach_o_objc_section
, 20}, /* extension. */
1190 { "objc_selector_fixup", obj_mach_o_objc_section
, 21}, /* extension. */
1191 { "objc1_class_ext", obj_mach_o_objc_section
, 22}, /* ObjC-1 extension. */
1192 { "objc1_property_list", obj_mach_o_objc_section
, 23}, /* ObjC-1 extension. */
1193 { "objc1_protocol_ext", obj_mach_o_objc_section
, 24}, /* ObjC-1 extension. */
1195 { "debug_frame", obj_mach_o_debug_section
, 1}, /* extension. */
1196 { "debug_info", obj_mach_o_debug_section
, 2}, /* extension. */
1197 { "debug_abbrev", obj_mach_o_debug_section
, 3}, /* extension. */
1198 { "debug_aranges", obj_mach_o_debug_section
, 4}, /* extension. */
1199 { "debug_macinfo", obj_mach_o_debug_section
, 5}, /* extension. */
1200 { "debug_line", obj_mach_o_debug_section
, 6}, /* extension. */
1201 { "debug_loc", obj_mach_o_debug_section
, 7}, /* extension. */
1202 { "debug_pubnames", obj_mach_o_debug_section
, 8}, /* extension. */
1203 { "debug_pubtypes", obj_mach_o_debug_section
, 9}, /* extension. */
1204 { "debug_str", obj_mach_o_debug_section
, 10}, /* extension. */
1205 { "debug_ranges", obj_mach_o_debug_section
, 11}, /* extension. */
1206 { "debug_macro", obj_mach_o_debug_section
, 12}, /* extension. */
1208 { "lazy_symbol_pointer", obj_mach_o_opt_tgt_section
, 1},
1209 { "lazy_symbol_pointer2", obj_mach_o_opt_tgt_section
, 2}, /* extension. */
1210 { "lazy_symbol_pointer3", obj_mach_o_opt_tgt_section
, 3}, /* extension. */
1211 { "non_lazy_symbol_pointer", obj_mach_o_opt_tgt_section
, 4},
1212 { "non_lazy_symbol_pointer_x86", obj_mach_o_opt_tgt_section
, 5}, /* extension. */
1213 { "symbol_stub", obj_mach_o_opt_tgt_section
, 6},
1214 { "symbol_stub1", obj_mach_o_opt_tgt_section
, 7}, /* extension. */
1215 { "picsymbol_stub", obj_mach_o_opt_tgt_section
, 8}, /* extension. */
1216 { "picsymbol_stub1", obj_mach_o_opt_tgt_section
, 9}, /* extension. */
1217 { "picsymbol_stub2", obj_mach_o_opt_tgt_section
, 4}, /* extension. */
1218 { "picsymbol_stub3", obj_mach_o_opt_tgt_section
, 4}, /* extension. */
1220 { "section", obj_mach_o_section
, 0},
1221 { "zerofill", obj_mach_o_zerofill
, 0},
1223 /* Symbol qualifiers. */
1224 {"local", obj_mach_o_sym_qual
, OBJ_MACH_O_SYM_LOCAL
},
1225 {"globl", obj_mach_o_sym_qual
, OBJ_MACH_O_SYM_GLOBL
},
1226 {"reference", obj_mach_o_sym_qual
, OBJ_MACH_O_SYM_REFERENCE
},
1227 {"weak_reference", obj_mach_o_sym_qual
, OBJ_MACH_O_SYM_WEAK_REF
},
1228 {"lazy_reference", obj_mach_o_sym_qual
, OBJ_MACH_O_SYM_LAZY_REF
},
1229 {"weak_definition", obj_mach_o_sym_qual
, OBJ_MACH_O_SYM_WEAK_DEF
},
1230 {"private_extern", obj_mach_o_sym_qual
, OBJ_MACH_O_SYM_PRIV_EXT
},
1231 {"no_dead_strip", obj_mach_o_sym_qual
, OBJ_MACH_O_SYM_NO_DEAD_STRIP
},
1232 {"weak", obj_mach_o_sym_qual
, OBJ_MACH_O_SYM_WEAK
}, /* ext */
1234 {"indirect_symbol", obj_mach_o_placeholder
, 0},
1237 { "subsections_via_symbols", obj_mach_o_fileprop
,
1238 OBJ_MACH_O_FILE_PROP_SUBSECTS_VIA_SYMS
},
1243 /* Determine the default n_type value for a symbol from its section. */
1246 obj_mach_o_type_for_symbol (bfd_mach_o_asymbol
*s
)
1248 if (s
->symbol
.section
== bfd_abs_section_ptr
)
1249 return BFD_MACH_O_N_ABS
;
1250 else if (s
->symbol
.section
== bfd_com_section_ptr
1251 || s
->symbol
.section
== bfd_und_section_ptr
)
1252 return BFD_MACH_O_N_UNDF
;
1254 return BFD_MACH_O_N_SECT
;
1257 /* We need to check the correspondence between some kinds of symbols and their
1258 sections. Common and BSS vars will seen via the obj_macho_comm() function.
1260 The earlier we can pick up a problem, the better the diagnostics will be.
1262 However, when symbol type information is attached, the symbol section will
1263 quite possibly be unknown. So we are stuck with checking (most of the)
1264 validity at the time the file is written (unfortunately, then one doesn't
1265 get line number information in the diagnostic). */
1267 /* Here we pick up the case where symbol qualifiers have been applied that
1268 are possibly incompatible with the section etc. that the symbol is defined
1271 void obj_macho_frob_label (struct symbol
*sp
)
1273 bfd_mach_o_asymbol
*s
= (bfd_mach_o_asymbol
*) symbol_get_bfdsym (sp
);
1274 /* This is the base symbol type, that we mask in. */
1275 unsigned base_type
= obj_mach_o_type_for_symbol (s
);
1276 bfd_mach_o_section
*sec
= bfd_mach_o_get_mach_o_section (s
->symbol
.section
);
1279 if ((s
->n_type
& BFD_MACH_O_N_STAB
) != 0)
1280 return; /* Leave alone. */
1283 sectype
= sec
->flags
& BFD_MACH_O_SECTION_TYPE_MASK
;
1285 /* If there is a pre-existing qualifier, we can make some checks about
1288 if(s
->symbol
.udata
.i
== SYM_MACHO_FIELDS_NOT_VALIDATED
)
1290 if ((s
->n_desc
& BFD_MACH_O_N_WEAK_DEF
)
1291 && sectype
!= BFD_MACH_O_S_COALESCED
)
1292 as_bad (_("'%s' can't be a weak_definition (currently only supported"
1293 " in sections of type coalesced)"), s
->symbol
.name
);
1295 /* Have we changed from an undefined to defined ref? */
1296 s
->n_desc
&= ~(REFE
| LAZY
);
1299 s
->n_type
&= ~BFD_MACH_O_N_TYPE
;
1300 s
->n_type
|= base_type
;
1303 /* This is the fall-back, we come here when we get to the end of the file and
1304 the symbol is not defined - or there are combinations of qualifiers required
1305 (e.g. global + weak_def). */
1308 obj_macho_frob_symbol (struct symbol
*sp
)
1310 bfd_mach_o_asymbol
*s
= (bfd_mach_o_asymbol
*) symbol_get_bfdsym (sp
);
1311 unsigned base_type
= obj_mach_o_type_for_symbol (s
);
1312 bfd_mach_o_section
*sec
= bfd_mach_o_get_mach_o_section (s
->symbol
.section
);
1316 sectype
= sec
->flags
& BFD_MACH_O_SECTION_TYPE_MASK
;
1318 if ((s
->n_type
& BFD_MACH_O_N_STAB
) != 0)
1319 return 0; /* Leave alone. */
1320 else if (s
->symbol
.section
== bfd_und_section_ptr
)
1322 /* ??? Do we really gain much from implementing this as well as the
1323 mach-o specific ones? */
1324 if (s
->symbol
.flags
& BSF_WEAK
)
1325 s
->n_desc
|= BFD_MACH_O_N_WEAK_REF
;
1327 /* Undefined references, become extern. */
1328 if (s
->n_desc
& REFE
)
1331 s
->n_type
|= BFD_MACH_O_N_EXT
;
1334 /* So do undefined 'no_dead_strip's. */
1335 if (s
->n_desc
& BFD_MACH_O_N_NO_DEAD_STRIP
)
1336 s
->n_type
|= BFD_MACH_O_N_EXT
;
1341 if ((s
->symbol
.flags
& BSF_WEAK
)
1342 && (sectype
== BFD_MACH_O_S_COALESCED
)
1343 && (s
->n_type
& (BFD_MACH_O_N_PEXT
| BFD_MACH_O_N_EXT
)))
1344 s
->n_desc
|= BFD_MACH_O_N_WEAK_DEF
;
1345 /* ??? we should do this - but then that reveals that the semantics of weak
1346 are different from what's supported in mach-o object files.
1348 as_bad (_("'%s' can't be a weak_definition."),
1352 if (s
->symbol
.udata
.i
== SYM_MACHO_FIELDS_UNSET
)
1354 /* Anything here that should be added that is non-standard. */
1355 s
->n_desc
&= ~BFD_MACH_O_REFERENCE_MASK
;
1357 else if (s
->symbol
.udata
.i
== SYM_MACHO_FIELDS_NOT_VALIDATED
)
1359 /* Try to validate any combinations. */
1360 if (s
->n_desc
& BFD_MACH_O_N_WEAK_DEF
)
1362 if (s
->symbol
.section
== bfd_und_section_ptr
)
1363 as_bad (_("'%s' can't be a weak_definition (since it is"
1364 " undefined)"), s
->symbol
.name
);
1365 else if (sectype
!= BFD_MACH_O_S_COALESCED
)
1366 as_bad (_("'%s' can't be a weak_definition (currently only supported"
1367 " in sections of type coalesced)"), s
->symbol
.name
);
1368 else if (! (s
->n_type
& (BFD_MACH_O_N_PEXT
| BFD_MACH_O_N_EXT
)))
1369 as_bad (_("Non-global symbol: '%s' can't be a weak_definition."),
1375 as_bad (_("internal error: [%s] unexpected code [%lx] in frob symbol"),
1376 s
->symbol
.name
, (unsigned long)s
->symbol
.udata
.i
);
1378 s
->n_type
&= ~BFD_MACH_O_N_TYPE
;
1379 s
->n_type
|= base_type
;
1381 if (s
->symbol
.flags
& BSF_GLOBAL
)
1382 s
->n_type
|= BFD_MACH_O_N_EXT
;
1384 /* This cuts both ways - we promote some things to external above. */
1385 if (s
->n_type
& (BFD_MACH_O_N_PEXT
| BFD_MACH_O_N_EXT
))
1386 S_SET_EXTERNAL (sp
);
1391 /* Support stabs for mach-o. */
1394 obj_mach_o_process_stab (int what
, const char *string
,
1395 int type
, int other
, int desc
)
1398 bfd_mach_o_asymbol
*s
;
1403 symbolP
= symbol_new ("", now_seg
, frag_now_fix (), frag_now
);
1404 /* Special stabd NULL name indicator. */
1405 S_SET_NAME (symbolP
, NULL
);
1410 symbolP
= symbol_new (string
, undefined_section
, (valueT
) 0,
1411 &zero_address_frag
);
1412 pseudo_set (symbolP
);
1416 as_bad(_("unrecognized stab type '%c'"), (char)what
);
1421 s
= (bfd_mach_o_asymbol
*) symbol_get_bfdsym (symbolP
);
1424 /* For stabd, this will eventually get overwritten by the section number. */
1427 /* It's a debug symbol. */
1428 s
->symbol
.flags
|= BSF_DEBUGGING
;