bfd/
[binutils.git] / gas / config / obj-macho.c
blobf4706ab03ed6eee65595d313515bc89422e8be9f
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
19 02110-1301, USA. */
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
24 decorations. */
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
36 also defined. */
38 #define OBJ_HEADER "obj-macho.h"
40 #include "as.h"
41 #include "subsegs.h"
42 #include "symbols.h"
43 #include "write.h"
44 #include "mach-o.h"
45 #include "mach-o/loader.h"
46 #include "obj-macho.h"
48 #include <string.h>
50 /* Forward decls. */
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. */
66 void
67 mach_o_begin (void)
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
86 the file flags. */
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
94 to the newline.
96 It trims leading and trailing space. */
98 static int
99 collect_16char_name (char *dest, const char *msg, int require_comma)
101 char c, *namstart;
103 SKIP_WHITESPACE ();
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] == ' ')
114 len--;
115 if (len > 16)
117 *input_line_pointer = '\0'; /* make a temp string. */
118 as_bad (_("the %s name '%s' is too long (maximum 16 characters)"),
119 msg, namstart);
120 *input_line_pointer = c; /* restore for printing. */
121 len = 16;
123 if (len > 0)
124 memcpy (dest, namstart, len);
127 if (c != ',' && require_comma)
129 as_bad (_("expected a %s name followed by a `,'"), msg);
130 return 1;
133 return 0;
136 static int
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 ();
146 return 0;
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);
153 return 1;
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
165 static segT
166 obj_mach_o_make_or_get_sect (char * segname, char * sectname,
167 unsigned int specified_mask,
168 unsigned int usectype, unsigned int usecattr,
169 unsigned int ualign, offsetT stub_size)
171 unsigned int sectype, secattr, secalign;
172 flagword oldflags, flags;
173 const char *name;
174 segT sec;
175 bfd_mach_o_section *msect;
176 const mach_o_section_name_xlat *xlat;
178 /* This provides default bfd flags and default mach-o section type and
179 attributes along with the canonical name. */
180 xlat = bfd_mach_o_section_data_for_mach_sect (stdoutput, segname, sectname);
182 /* TODO: more checking of whether overides are acually allowed. */
184 if (xlat != NULL)
186 name = xstrdup (xlat->bfd_name);
187 sectype = xlat->macho_sectype;
188 if (specified_mask & SECT_TYPE_SPECIFIED)
190 if ((sectype == BFD_MACH_O_S_ZEROFILL
191 || sectype == BFD_MACH_O_S_GB_ZEROFILL)
192 && sectype != usectype)
193 as_bad (_("cannot overide zerofill section type for `%s,%s'"),
194 segname, sectname);
195 else
196 sectype = usectype;
198 secattr = xlat->macho_secattr;
199 secalign = xlat->sectalign;
200 flags = xlat->bfd_flags;
202 else
204 /* There is no normal BFD section name for this section. Create one.
205 The name created doesn't really matter as it will never be written
206 on disk. */
207 size_t seglen = strlen (segname);
208 size_t sectlen = strlen (sectname);
209 char *n;
211 n = xmalloc (seglen + 1 + sectlen + 1);
212 memcpy (n, segname, seglen);
213 n[seglen] = '.';
214 memcpy (n + seglen + 1, sectname, sectlen);
215 n[seglen + 1 + sectlen] = 0;
216 name = n;
217 if (specified_mask & SECT_TYPE_SPECIFIED)
218 sectype = usectype;
219 else
220 sectype = BFD_MACH_O_S_REGULAR;
221 secattr = BFD_MACH_O_S_ATTR_NONE;
222 secalign = 0;
223 flags = SEC_NO_FLAGS;
226 /* For now, just use what the user provided. */
228 if (specified_mask & SECT_ATTR_SPECIFIED)
229 secattr = usecattr;
231 if (specified_mask & SECT_ALGN_SPECIFIED)
232 secalign = ualign;
234 /* Sub-segments don't exists as is on Mach-O. */
235 sec = subseg_new (name, 0);
237 oldflags = bfd_get_section_flags (stdoutput, sec);
238 msect = bfd_mach_o_get_mach_o_section (sec);
240 if (oldflags == SEC_NO_FLAGS)
242 /* New, so just use the defaults or what's specified. */
243 if (! bfd_set_section_flags (stdoutput, sec, flags))
244 as_warn (_("failed to set flags for \"%s\": %s"),
245 bfd_section_name (stdoutput, sec),
246 bfd_errmsg (bfd_get_error ()));
248 strncpy (msect->segname, segname, sizeof (msect->segname));
249 strncpy (msect->sectname, sectname, sizeof (msect->sectname));
251 msect->align = secalign;
252 msect->flags = sectype | secattr;
253 msect->reserved2 = stub_size;
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 return sec;
269 /* .section
271 The '.section' specification syntax looks like:
272 .section <segment> , <section> [, type [, attribs [, size]]]
274 White space is allowed everywhere between elements.
276 <segment> and <section> may be from 0 to 16 chars in length - they may
277 contain spaces but leading and trailing space will be trimmed. It is
278 mandatory that they be present (or that zero-length names are indicated
279 by ",,").
281 There is only a single section type for any entry.
283 There may be multiple attributes, they are delimited by `+'.
285 Not all section types and attributes are accepted by the Darwin system
286 assemblers as user-specifiable - although, at present, we do here. */
288 static void
289 obj_mach_o_section (int ignore ATTRIBUTE_UNUSED)
291 unsigned int sectype = BFD_MACH_O_S_REGULAR;
292 unsigned int specified_mask = 0;
293 unsigned int secattr = 0;
294 offsetT sizeof_stub = 0;
295 segT new_seg;
296 char segname[17];
297 char sectname[17];
299 #ifdef md_flush_pending_output
300 md_flush_pending_output ();
301 #endif
303 /* Get the User's segment annd section names. */
304 if (! obj_mach_o_get_section_names (segname, sectname, 17, 17))
305 return;
307 /* Parse section type, if present. */
308 if (*input_line_pointer == ',')
310 char *p;
311 char c;
312 char tmpc;
313 int len;
314 input_line_pointer++;
315 SKIP_WHITESPACE ();
316 p = input_line_pointer;
317 while ((c = *input_line_pointer) != ','
318 && !is_end_of_line[(unsigned char) c])
319 input_line_pointer++;
321 len = input_line_pointer - p;
322 /* strip trailing spaces. */
323 while (len > 0 && p[len-1] == ' ')
324 len--;
325 tmpc = p[len];
327 /* Temporarily make a string from the token. */
328 p[len] = 0;
329 sectype = bfd_mach_o_get_section_type_from_name (stdoutput, p);
330 if (sectype > 255) /* Max Section ID == 255. */
332 as_bad (_("unknown or invalid section type '%s'"), p);
333 p[len] = tmpc;
334 ignore_rest_of_line ();
335 return;
337 else
338 specified_mask |= SECT_TYPE_SPECIFIED;
339 /* Restore. */
340 p[len] = tmpc;
342 /* Parse attributes.
343 TODO: check validity of attributes for section type. */
344 if ((specified_mask & SECT_TYPE_SPECIFIED)
345 && c == ',')
349 int attr;
351 /* Skip initial `,' and subsequent `+'. */
352 input_line_pointer++;
353 SKIP_WHITESPACE ();
354 p = input_line_pointer;
355 while ((c = *input_line_pointer) != '+'
356 && c != ','
357 && !is_end_of_line[(unsigned char) c])
358 input_line_pointer++;
360 len = input_line_pointer - p;
361 /* strip trailing spaces. */
362 while (len > 0 && p[len-1] == ' ')
363 len--;
364 tmpc = p[len];
366 /* Temporarily make a string from the token. */
367 p[len] ='\0';
368 attr = bfd_mach_o_get_section_attribute_from_name (p);
369 if (attr == -1)
371 as_bad (_("unknown or invalid section attribute '%s'"), p);
372 p[len] = tmpc;
373 ignore_rest_of_line ();
374 return;
376 else
378 specified_mask |= SECT_ATTR_SPECIFIED;
379 secattr |= attr;
381 /* Restore. */
382 p[len] = tmpc;
384 while (*input_line_pointer == '+');
386 /* Parse sizeof_stub. */
387 if ((specified_mask & SECT_ATTR_SPECIFIED)
388 && *input_line_pointer == ',')
390 if (sectype != BFD_MACH_O_S_SYMBOL_STUBS)
392 as_bad (_("unexpected section size information"));
393 ignore_rest_of_line ();
394 return;
397 input_line_pointer++;
398 sizeof_stub = get_absolute_expression ();
400 else if ((specified_mask & SECT_ATTR_SPECIFIED)
401 && sectype == BFD_MACH_O_S_SYMBOL_STUBS)
403 as_bad (_("missing sizeof_stub expression"));
404 ignore_rest_of_line ();
405 return;
410 new_seg = obj_mach_o_make_or_get_sect (segname, sectname, specified_mask,
411 sectype, secattr, 0 /*align */,
412 sizeof_stub);
413 if (new_seg != NULL)
415 subseg_set (new_seg, 0);
416 demand_empty_rest_of_line ();
420 /* .zerofill segname, sectname [, symbolname, size [, align]]
422 Zerofill switches, temporarily, to a sect of type 'zerofill'.
424 If a variable name is given, it defines that in the section.
425 Otherwise it just creates the section if it doesn't exist. */
427 static void
428 obj_mach_o_zerofill (int ignore ATTRIBUTE_UNUSED)
430 char segname[17];
431 char sectname[17];
432 segT old_seg = now_seg;
433 segT new_seg;
434 symbolS *sym = NULL;
435 unsigned int align = 0;
436 unsigned int specified_mask = 0;
437 offsetT size = 0;
439 #ifdef md_flush_pending_output
440 md_flush_pending_output ();
441 #endif
443 /* Get the User's segment annd section names. */
444 if (! obj_mach_o_get_section_names (segname, sectname, 17, 17))
445 return;
447 /* Parse variable definition, if present. */
448 if (*input_line_pointer == ',')
450 /* Parse symbol, size [.align]
451 We follow the method of s_common_internal, with the difference
452 that the symbol cannot be a duplicate-common. */
453 char *name;
454 char c;
455 char *p;
456 expressionS exp;
458 input_line_pointer++; /* Skip ',' */
459 SKIP_WHITESPACE ();
460 name = input_line_pointer;
461 c = get_symbol_end ();
462 /* Just after name is now '\0'. */
463 p = input_line_pointer;
464 *p = c;
466 if (name == p)
468 as_bad (_("expected symbol name"));
469 ignore_rest_of_line ();
470 goto done;
473 SKIP_WHITESPACE ();
474 if (*input_line_pointer == ',')
475 input_line_pointer++;
477 expression_and_evaluate (&exp);
478 if (exp.X_op != O_constant
479 && exp.X_op != O_absent)
481 as_bad (_("bad or irreducible absolute expression"));
482 ignore_rest_of_line ();
483 goto done;
485 else if (exp.X_op == O_absent)
487 as_bad (_("missing size expression"));
488 ignore_rest_of_line ();
489 goto done;
492 size = exp.X_add_number;
493 size &= ((offsetT) 2 << (stdoutput->arch_info->bits_per_address - 1)) - 1;
494 if (exp.X_add_number != size || !exp.X_unsigned)
496 as_warn (_("size (%ld) out of range, ignored"),
497 (long) exp.X_add_number);
498 ignore_rest_of_line ();
499 goto done;
502 *p = 0; /* Make the name into a c string for err messages. */
503 sym = symbol_find_or_make (name);
504 if (S_IS_DEFINED (sym) || symbol_equated_p (sym))
506 as_bad (_("symbol `%s' is already defined"), name);
507 *p = c;
508 ignore_rest_of_line ();
509 goto done;
512 size = S_GET_VALUE (sym);
513 if (size == 0)
514 size = exp.X_add_number;
515 else if (size != exp.X_add_number)
516 as_warn (_("size of \"%s\" is already %ld; not changing to %ld"),
517 name, (long) size, (long) exp.X_add_number);
519 *p = c; /* Restore the termination char. */
521 SKIP_WHITESPACE ();
522 if (*input_line_pointer == ',')
524 align = (unsigned int) parse_align (0);
525 if (align == (unsigned int) -1)
527 as_warn (_("align value not recognized, using size"));
528 align = size;
530 if (align > 15)
532 as_warn (_("Alignment (%lu) too large: 15 assumed."),
533 (unsigned long)align);
534 align = 15;
536 specified_mask |= SECT_ALGN_SPECIFIED;
539 /* else just a section definition. */
541 specified_mask |= SECT_TYPE_SPECIFIED;
542 new_seg = obj_mach_o_make_or_get_sect (segname, sectname, specified_mask,
543 BFD_MACH_O_S_ZEROFILL,
544 BFD_MACH_O_S_ATTR_NONE,
545 align, (offsetT) 0 /*stub size*/);
546 if (new_seg == NULL)
547 return;
549 /* In case the user specifies the bss section by mach-o name.
550 Create it on demand */
551 if (strcmp (new_seg->name, BSS_SECTION_NAME) == 0
552 && bss_section == NULL)
553 bss_section = new_seg;
555 subseg_set (new_seg, 0);
557 if (sym != NULL)
559 char *pfrag;
561 if (align)
563 record_alignment (new_seg, align);
564 frag_align (align, 0, 0);
567 /* Detach from old frag. */
568 if (S_GET_SEGMENT (sym) == new_seg)
569 symbol_get_frag (sym)->fr_symbol = NULL;
571 symbol_set_frag (sym, frag_now);
572 pfrag = frag_var (rs_org, 1, 1, 0, sym, size, NULL);
573 *pfrag = 0;
575 S_SET_SEGMENT (sym, new_seg);
576 if (new_seg == bss_section)
577 S_CLEAR_EXTERNAL (sym);
580 done:
581 /* switch back to the section that was current before the .zerofill. */
582 subseg_set (old_seg, 0);
585 static segT
586 obj_mach_o_segT_from_bfd_name (const char *nam, int must_succeed)
588 const mach_o_section_name_xlat *xlat;
589 const char *segn;
590 segT sec;
592 /* BFD has tables of flags and default attributes for all the sections that
593 have a 'canonical' name. */
594 xlat = bfd_mach_o_section_data_for_bfd_name (stdoutput, nam, &segn);
595 if (xlat == NULL)
597 if (must_succeed)
598 as_fatal (_("BFD is out of sync with GAS, "
599 "unhandled well-known section type `%s'"), nam);
600 return NULL;
603 sec = bfd_get_section_by_name (stdoutput, nam);
604 if (sec == NULL)
606 bfd_mach_o_section *msect;
608 sec = subseg_force_new (xlat->bfd_name, 0);
610 /* Set default type, attributes and alignment. */
611 msect = bfd_mach_o_get_mach_o_section (sec);
612 msect->flags = xlat->macho_sectype | xlat->macho_secattr;
613 msect->align = xlat->sectalign;
615 if ((msect->flags & BFD_MACH_O_SECTION_TYPE_MASK)
616 == BFD_MACH_O_S_ZEROFILL)
617 seg_info (sec)->bss = 1;
620 return sec;
623 static const char * const known_sections[] =
625 /* 0 */ NULL,
626 /* __TEXT */
627 /* 1 */ ".const",
628 /* 2 */ ".static_const",
629 /* 3 */ ".cstring",
630 /* 4 */ ".literal4",
631 /* 5 */ ".literal8",
632 /* 6 */ ".literal16",
633 /* 7 */ ".constructor",
634 /* 8 */ ".destructor",
635 /* 9 */ ".eh_frame",
636 /* __DATA */
637 /* 10 */ ".const_data",
638 /* 11 */ ".static_data",
639 /* 12 */ ".mod_init_func",
640 /* 13 */ ".mod_term_func",
641 /* 14 */ ".dyld",
642 /* 15 */ ".cfstring"
645 /* Interface for a known non-optional section directive. */
647 static void
648 obj_mach_o_known_section (int sect_index)
650 segT section;
652 #ifdef md_flush_pending_output
653 md_flush_pending_output ();
654 #endif
656 section = obj_mach_o_segT_from_bfd_name (known_sections[sect_index], 1);
657 if (section != NULL)
658 subseg_set (section, 0);
660 /* else, we leave the section as it was; there was a fatal error anyway. */
663 static const char * const objc_sections[] =
665 /* 0 */ NULL,
666 /* 1 */ ".objc_class",
667 /* 2 */ ".objc_meta_class",
668 /* 3 */ ".objc_cat_cls_meth",
669 /* 4 */ ".objc_cat_inst_meth",
670 /* 5 */ ".objc_protocol",
671 /* 6 */ ".objc_string_object",
672 /* 7 */ ".objc_cls_meth",
673 /* 8 */ ".objc_inst_meth",
674 /* 9 */ ".objc_cls_refs",
675 /* 10 */ ".objc_message_refs",
676 /* 11 */ ".objc_symbols",
677 /* 12 */ ".objc_category",
678 /* 13 */ ".objc_class_vars",
679 /* 14 */ ".objc_instance_vars",
680 /* 15 */ ".objc_module_info",
681 /* 16 */ ".cstring", /* objc_class_names Alias for .cstring */
682 /* 17 */ ".cstring", /* Alias objc_meth_var_types for .cstring */
683 /* 18 */ ".cstring", /* objc_meth_var_names Alias for .cstring */
684 /* 19 */ ".objc_selector_strs",
685 /* 20 */ ".objc_image_info", /* extension. */
686 /* 21 */ ".objc_selector_fixup", /* extension. */
687 /* 22 */ ".objc1_class_ext", /* ObjC-1 extension. */
688 /* 23 */ ".objc1_property_list", /* ObjC-1 extension. */
689 /* 24 */ ".objc1_protocol_ext" /* ObjC-1 extension. */
692 /* This currently does the same as known_sections, but kept separate for
693 ease of maintenance. */
695 static void
696 obj_mach_o_objc_section (int sect_index)
698 segT section;
700 #ifdef md_flush_pending_output
701 md_flush_pending_output ();
702 #endif
704 section = obj_mach_o_segT_from_bfd_name (objc_sections[sect_index], 1);
705 if (section != NULL)
707 obj_mach_o_seen_objc_section = 1; /* We need to ensure that certain
708 sections are present and in the
709 right order. */
710 subseg_set (section, 0);
713 /* else, we leave the section as it was; there was a fatal error anyway. */
716 /* Debug section directives. */
718 static const char * const debug_sections[] =
720 /* 0 */ NULL,
721 /* __DWARF */
722 /* 1 */ ".debug_frame",
723 /* 2 */ ".debug_info",
724 /* 3 */ ".debug_abbrev",
725 /* 4 */ ".debug_aranges",
726 /* 5 */ ".debug_macinfo",
727 /* 6 */ ".debug_line",
728 /* 7 */ ".debug_loc",
729 /* 8 */ ".debug_pubnames",
730 /* 9 */ ".debug_pubtypes",
731 /* 10 */ ".debug_str",
732 /* 11 */ ".debug_ranges",
733 /* 12 */ ".debug_macro"
736 /* ??? Maybe these should be conditional on gdwarf-*.
737 It`s also likely that we will need to be able to set them from the cfi
738 code. */
740 static void
741 obj_mach_o_debug_section (int sect_index)
743 segT section;
745 #ifdef md_flush_pending_output
746 md_flush_pending_output ();
747 #endif
749 section = obj_mach_o_segT_from_bfd_name (debug_sections[sect_index], 1);
750 if (section != NULL)
751 subseg_set (section, 0);
753 /* else, we leave the section as it was; there was a fatal error anyway. */
756 /* This could be moved to the tc-xx files, but there is so little dependency
757 there, that the code might as well be shared. */
759 struct opt_tgt_sect
761 const char *name;
762 unsigned x86_val;
763 unsigned ppc_val;
766 /* The extensions here are for specific sections that are generated by GCC
767 and Darwin system tools, but don't have directives in the `system as'. */
769 static const struct opt_tgt_sect tgt_sections[] =
771 /* 0 */ { NULL, 0, 0},
772 /* 1 */ { ".lazy_symbol_pointer", 0, 0},
773 /* 2 */ { ".lazy_symbol_pointer2", 0, 0}, /* X86 - extension */
774 /* 3 */ { ".lazy_symbol_pointer3", 0, 0}, /* X86 - extension */
775 /* 4 */ { ".non_lazy_symbol_pointer", 0, 0},
776 /* 5 */ { ".non_lazy_symbol_pointer_x86", 0, 0}, /* X86 - extension */
777 /* 6 */ { ".symbol_stub", 16, 20},
778 /* 7 */ { ".symbol_stub1", 0, 16}, /* PPC - extension */
779 /* 8 */ { ".picsymbol_stub", 26, 36},
780 /* 9 */ { ".picsymbol_stub1", 0, 32}, /* PPC - extension */
781 /* 10 */ { ".picsymbol_stub2", 25, 0}, /* X86 - extension */
782 /* 11 */ { ".picsymbol_stub3", 5, 0}, /* X86 - extension */
785 /* Interface for an optional section directive. */
787 static void
788 obj_mach_o_opt_tgt_section (int sect_index)
790 const struct opt_tgt_sect *tgtsct = &tgt_sections[sect_index];
791 segT section;
793 #ifdef md_flush_pending_output
794 md_flush_pending_output ();
795 #endif
797 section = obj_mach_o_segT_from_bfd_name (tgtsct->name, 0);
798 if (section == NULL)
800 as_bad (_("%s is not used for the selected target"), tgtsct->name);
801 /* Leave the section as it is. */
803 else
805 bfd_mach_o_section *mo_sec = bfd_mach_o_get_mach_o_section (section);
806 subseg_set (section, 0);
807 #if defined (TC_I386)
808 mo_sec->reserved2 = tgtsct->x86_val;
809 #elif defined (TC_PPC)
810 mo_sec->reserved2 = tgtsct->ppc_val;
811 #else
812 mo_sec->reserved2 = 0;
813 #endif
817 /* We don't necessarily have the three 'base' sections on mach-o.
818 Normally, we would start up with only the 'text' section defined.
819 However, even that can be suppressed with (TODO) c/l option "-n".
820 Thus, we have to be able to create all three sections on-demand. */
822 static void
823 obj_mach_o_base_section (int sect_index)
825 segT section;
827 #ifdef md_flush_pending_output
828 md_flush_pending_output ();
829 #endif
831 /* We don't support numeric (or any other) qualifications on the
832 well-known section shorthands. */
833 demand_empty_rest_of_line ();
835 switch (sect_index)
837 /* Handle the three sections that are globally known within GAS.
838 For Mach-O, these are created on demand rather than at startup. */
839 case 1:
840 if (text_section == NULL)
841 text_section = obj_mach_o_segT_from_bfd_name (TEXT_SECTION_NAME, 1);
842 if (obj_mach_o_is_static)
844 bfd_mach_o_section *mo_sec
845 = bfd_mach_o_get_mach_o_section (text_section);
846 mo_sec->flags &= ~BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS;
848 section = text_section;
849 break;
850 case 2:
851 if (data_section == NULL)
852 data_section = obj_mach_o_segT_from_bfd_name (DATA_SECTION_NAME, 1);
853 section = data_section;
854 break;
855 case 3:
856 /* ??? maybe this achieves very little, as an addition. */
857 if (bss_section == NULL)
859 bss_section = obj_mach_o_segT_from_bfd_name (BSS_SECTION_NAME, 1);
860 seg_info (bss_section)->bss = 1;
862 section = bss_section;
863 break;
864 default:
865 as_fatal (_("internal error: base section index out of range"));
866 return;
867 break;
869 subseg_set (section, 0);
872 /* This finishes off parsing a .comm or .lcomm statement, which both can have
873 an (optional) alignment field. It also allows us to create the bss section
874 on demand. */
876 static symbolS *
877 obj_mach_o_common_parse (int is_local, symbolS *symbolP,
878 addressT size)
880 addressT align = 0;
881 bfd_mach_o_asymbol *s;
883 SKIP_WHITESPACE ();
885 /* Both comm and lcomm take an optional alignment, as a power
886 of two between 1 and 15. */
887 if (*input_line_pointer == ',')
889 /* We expect a power of 2. */
890 align = parse_align (0);
891 if (align == (addressT) -1)
892 return NULL;
893 if (align > 15)
895 as_warn (_("Alignment (%lu) too large: 15 assumed."),
896 (unsigned long)align);
897 align = 15;
901 s = (bfd_mach_o_asymbol *) symbol_get_bfdsym (symbolP);
902 if (is_local)
904 /* Create the BSS section on demand. */
905 if (bss_section == NULL)
907 bss_section = obj_mach_o_segT_from_bfd_name (BSS_SECTION_NAME, 1);
908 seg_info (bss_section)->bss = 1;
910 bss_alloc (symbolP, size, align);
911 s->n_type = BFD_MACH_O_N_SECT;
912 S_CLEAR_EXTERNAL (symbolP);
914 else
916 S_SET_VALUE (symbolP, size);
917 S_SET_ALIGN (symbolP, align);
918 S_SET_EXTERNAL (symbolP);
919 S_SET_SEGMENT (symbolP, bfd_com_section_ptr);
920 s->n_type = BFD_MACH_O_N_UNDF | BFD_MACH_O_N_EXT;
923 /* This is a data object (whatever we choose that to mean). */
924 s->symbol.flags |= BSF_OBJECT;
926 /* We've set symbol qualifiers, so validate if you can. */
927 s->symbol.udata.i = SYM_MACHO_FIELDS_NOT_VALIDATED;
929 return symbolP;
932 static void
933 obj_mach_o_comm (int is_local)
935 s_comm_internal (is_local, obj_mach_o_common_parse);
938 /* Set properties that apply to the whole file. At present, the only
939 one defined, is subsections_via_symbols. */
941 typedef enum obj_mach_o_file_properties {
942 OBJ_MACH_O_FILE_PROP_NONE = 0,
943 OBJ_MACH_O_FILE_PROP_SUBSECTS_VIA_SYMS,
944 OBJ_MACH_O_FILE_PROP_MAX
945 } obj_mach_o_file_properties;
947 static void
948 obj_mach_o_fileprop (int prop)
950 if (prop < 0 || prop >= OBJ_MACH_O_FILE_PROP_MAX)
951 as_fatal (_("internal error: bad file property ID %d"), prop);
953 switch ((obj_mach_o_file_properties) prop)
955 case OBJ_MACH_O_FILE_PROP_SUBSECTS_VIA_SYMS:
956 obj_mach_o_subsections_by_symbols = 1;
957 if (!bfd_set_private_flags (stdoutput,
958 BFD_MACH_O_MH_SUBSECTIONS_VIA_SYMBOLS))
959 as_bad (_("failed to set subsections by symbols"));
960 demand_empty_rest_of_line ();
961 break;
962 default:
963 break;
967 /* Temporary markers for symbol reference data.
968 Lazy will remain in place. */
969 #define LAZY 0x01
970 #define REFE 0x02
972 /* We have a bunch of qualifiers that may be applied to symbols.
973 .globl is handled here so that we might make sure that conflicting qualifiers
974 are caught where possible. */
976 typedef enum obj_mach_o_symbol_type {
977 OBJ_MACH_O_SYM_UNK = 0,
978 OBJ_MACH_O_SYM_LOCAL = 1,
979 OBJ_MACH_O_SYM_GLOBL = 2,
980 OBJ_MACH_O_SYM_REFERENCE = 3,
981 OBJ_MACH_O_SYM_WEAK_REF = 4,
982 OBJ_MACH_O_SYM_LAZY_REF = 5,
983 OBJ_MACH_O_SYM_WEAK_DEF = 6,
984 OBJ_MACH_O_SYM_PRIV_EXT = 7,
985 OBJ_MACH_O_SYM_NO_DEAD_STRIP = 8,
986 OBJ_MACH_O_SYM_WEAK = 9
987 } obj_mach_o_symbol_type;
989 /* Set Mach-O-specific symbol qualifiers. */
991 static int
992 obj_mach_o_set_symbol_qualifier (symbolS *sym, int type)
994 int is_defined;
995 bfd_mach_o_asymbol *s = (bfd_mach_o_asymbol *) symbol_get_bfdsym (sym);
996 bfd_mach_o_section *sec;
997 int sectype = -1;
998 int err = 0;
1000 /* If the symbol is defined, then we can do more rigorous checking on
1001 the validity of the qualifiers. Otherwise, we are stuck with waiting
1002 until it's defined - or until write the file.
1004 In certain cases (e.g. when a symbol qualifier is intended to introduce
1005 an undefined symbol in a stubs section) we should check that the current
1006 section is appropriate to the qualifier. */
1008 is_defined = s->symbol.section != bfd_und_section_ptr;
1009 if (is_defined)
1010 sec = bfd_mach_o_get_mach_o_section (s->symbol.section) ;
1011 else
1012 sec = bfd_mach_o_get_mach_o_section (now_seg) ;
1014 if (sec != NULL)
1015 sectype = sec->flags & BFD_MACH_O_SECTION_TYPE_MASK;
1017 switch ((obj_mach_o_symbol_type) type)
1019 case OBJ_MACH_O_SYM_LOCAL:
1020 /* This is an extension over the system tools. */
1021 if (s->n_type & (BFD_MACH_O_N_PEXT | BFD_MACH_O_N_EXT))
1023 as_bad (_("'%s' previously declared as '%s'."), s->symbol.name,
1024 (s->n_type & BFD_MACH_O_N_PEXT) ? "private extern"
1025 : "global" );
1026 err = 1;
1028 else
1030 s->n_type &= ~BFD_MACH_O_N_EXT;
1031 S_CLEAR_EXTERNAL (sym);
1033 break;
1035 case OBJ_MACH_O_SYM_PRIV_EXT:
1036 s->n_type |= BFD_MACH_O_N_PEXT ;
1037 s->n_desc &= ~LAZY; /* The native tool switches this off too. */
1038 /* We follow the system tools in marking PEXT as also global. */
1039 /* Fall through. */
1041 case OBJ_MACH_O_SYM_GLOBL:
1042 /* It's not an error to define a symbol and then make it global. */
1043 s->n_type |= BFD_MACH_O_N_EXT;
1044 S_SET_EXTERNAL (sym);
1045 break;
1047 case OBJ_MACH_O_SYM_REFERENCE:
1048 if (is_defined)
1049 s->n_desc |= BFD_MACH_O_N_NO_DEAD_STRIP;
1050 else
1051 s->n_desc |= (REFE | BFD_MACH_O_N_NO_DEAD_STRIP);
1052 break;
1054 case OBJ_MACH_O_SYM_LAZY_REF:
1055 if (is_defined)
1056 s->n_desc |= BFD_MACH_O_N_NO_DEAD_STRIP;
1057 else
1058 s->n_desc |= (REFE | LAZY | BFD_MACH_O_N_NO_DEAD_STRIP);
1059 break;
1061 /* Force ld to retain the symbol - even if it appears unused. */
1062 case OBJ_MACH_O_SYM_NO_DEAD_STRIP:
1063 s->n_desc |= BFD_MACH_O_N_NO_DEAD_STRIP ;
1064 break;
1066 /* Mach-O's idea of weak ... */
1067 case OBJ_MACH_O_SYM_WEAK_REF:
1068 s->n_desc |= BFD_MACH_O_N_WEAK_REF ;
1069 break;
1071 case OBJ_MACH_O_SYM_WEAK_DEF:
1072 if (is_defined && sectype != BFD_MACH_O_S_COALESCED)
1074 as_bad (_("'%s' can't be a weak_definition (currently only"
1075 " supported in sections of type coalesced)"),
1076 s->symbol.name);
1077 err = 1;
1079 else
1080 s->n_desc |= BFD_MACH_O_N_WEAK_DEF;
1081 break;
1083 case OBJ_MACH_O_SYM_WEAK:
1084 /* A generic 'weak' - we try to figure out what it means at
1085 symbol frob time. */
1086 S_SET_WEAK (sym);
1087 break;
1089 default:
1090 break;
1093 /* We've seen some kind of qualifier - check validity if or when the entity
1094 is defined. */
1095 s->symbol.udata.i = SYM_MACHO_FIELDS_NOT_VALIDATED;
1096 return err;
1099 /* Respond to symbol qualifiers.
1100 All of the form:
1101 .<qualifier> symbol [, symbol]*
1102 a list of symbols is an extension over the Darwin system as. */
1104 static void
1105 obj_mach_o_sym_qual (int ntype)
1107 char *name;
1108 char c;
1109 symbolS *symbolP;
1111 #ifdef md_flush_pending_output
1112 md_flush_pending_output ();
1113 #endif
1117 name = input_line_pointer;
1118 c = get_symbol_end ();
1119 symbolP = symbol_find_or_make (name);
1120 obj_mach_o_set_symbol_qualifier (symbolP, ntype);
1121 *input_line_pointer = c;
1122 SKIP_WHITESPACE ();
1123 c = *input_line_pointer;
1124 if (c == ',')
1126 input_line_pointer++;
1127 SKIP_WHITESPACE ();
1128 if (is_end_of_line[(unsigned char) *input_line_pointer])
1129 c = '\n';
1132 while (c == ',');
1134 demand_empty_rest_of_line ();
1137 typedef struct obj_mach_o_indirect_sym
1139 symbolS *sym;
1140 segT sect;
1141 struct obj_mach_o_indirect_sym *next;
1142 } obj_mach_o_indirect_sym;
1144 /* We store in order an maintain a pointer to the last one - to save reversing
1145 later. */
1146 obj_mach_o_indirect_sym *indirect_syms;
1147 obj_mach_o_indirect_sym *indirect_syms_tail;
1149 static void
1150 obj_mach_o_indirect_symbol (int arg ATTRIBUTE_UNUSED)
1152 bfd_mach_o_section *sec = bfd_mach_o_get_mach_o_section (now_seg);
1154 #ifdef md_flush_pending_output
1155 md_flush_pending_output ();
1156 #endif
1158 if (obj_mach_o_is_static)
1159 as_bad (_("use of .indirect_symbols requires `-dynamic'"));
1161 switch (sec->flags & BFD_MACH_O_SECTION_TYPE_MASK)
1163 case BFD_MACH_O_S_SYMBOL_STUBS:
1164 case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS:
1165 case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS:
1167 obj_mach_o_indirect_sym *isym;
1168 char *name = input_line_pointer;
1169 char c = get_symbol_end ();
1170 symbolS *sym = symbol_find_or_make (name);
1171 unsigned int elsize =
1172 bfd_mach_o_section_get_entry_size (stdoutput, sec);
1174 if (elsize == 0)
1176 as_bad (_("attempt to add an indirect_symbol to a stub or"
1177 " reference section with a zero-sized element at %s"),
1178 name);
1179 *input_line_pointer = c;
1180 ignore_rest_of_line ();
1181 return;
1183 *input_line_pointer = c;
1185 isym = (obj_mach_o_indirect_sym *)
1186 xmalloc (sizeof (obj_mach_o_indirect_sym));
1188 /* Just record the data for now, we will validate it when we
1189 compute the output in obj_mach_o_set_indirect_symbols. */
1190 isym->sym = sym;
1191 isym->sect = now_seg;
1192 isym->next = NULL;
1193 if (indirect_syms == NULL)
1194 indirect_syms = isym;
1195 else
1196 indirect_syms_tail->next = isym;
1197 indirect_syms_tail = isym;
1199 break;
1201 default:
1202 as_bad (_("an .indirect_symbol must be in a symbol pointer"
1203 " or stub section."));
1204 ignore_rest_of_line ();
1205 return;
1207 demand_empty_rest_of_line ();
1210 const pseudo_typeS mach_o_pseudo_table[] =
1212 /* Section directives. */
1213 { "comm", obj_mach_o_comm, 0 },
1214 { "lcomm", obj_mach_o_comm, 1 },
1216 { "text", obj_mach_o_base_section, 1},
1217 { "data", obj_mach_o_base_section, 2},
1218 { "bss", obj_mach_o_base_section, 3}, /* extension */
1220 { "const", obj_mach_o_known_section, 1},
1221 { "static_const", obj_mach_o_known_section, 2},
1222 { "cstring", obj_mach_o_known_section, 3},
1223 { "literal4", obj_mach_o_known_section, 4},
1224 { "literal8", obj_mach_o_known_section, 5},
1225 { "literal16", obj_mach_o_known_section, 6},
1226 { "constructor", obj_mach_o_known_section, 7},
1227 { "destructor", obj_mach_o_known_section, 8},
1228 { "eh_frame", obj_mach_o_known_section, 9},
1230 { "const_data", obj_mach_o_known_section, 10},
1231 { "static_data", obj_mach_o_known_section, 11},
1232 { "mod_init_func", obj_mach_o_known_section, 12},
1233 { "mod_term_func", obj_mach_o_known_section, 13},
1234 { "dyld", obj_mach_o_known_section, 14},
1235 { "cfstring", obj_mach_o_known_section, 15},
1237 { "objc_class", obj_mach_o_objc_section, 1},
1238 { "objc_meta_class", obj_mach_o_objc_section, 2},
1239 { "objc_cat_cls_meth", obj_mach_o_objc_section, 3},
1240 { "objc_cat_inst_meth", obj_mach_o_objc_section, 4},
1241 { "objc_protocol", obj_mach_o_objc_section, 5},
1242 { "objc_string_object", obj_mach_o_objc_section, 6},
1243 { "objc_cls_meth", obj_mach_o_objc_section, 7},
1244 { "objc_inst_meth", obj_mach_o_objc_section, 8},
1245 { "objc_cls_refs", obj_mach_o_objc_section, 9},
1246 { "objc_message_refs", obj_mach_o_objc_section, 10},
1247 { "objc_symbols", obj_mach_o_objc_section, 11},
1248 { "objc_category", obj_mach_o_objc_section, 12},
1249 { "objc_class_vars", obj_mach_o_objc_section, 13},
1250 { "objc_instance_vars", obj_mach_o_objc_section, 14},
1251 { "objc_module_info", obj_mach_o_objc_section, 15},
1252 { "objc_class_names", obj_mach_o_objc_section, 16}, /* Alias for .cstring */
1253 { "objc_meth_var_types", obj_mach_o_objc_section, 17}, /* Alias for .cstring */
1254 { "objc_meth_var_names", obj_mach_o_objc_section, 18}, /* Alias for .cstring */
1255 { "objc_selector_strs", obj_mach_o_objc_section, 19},
1256 { "objc_image_info", obj_mach_o_objc_section, 20}, /* extension. */
1257 { "objc_selector_fixup", obj_mach_o_objc_section, 21}, /* extension. */
1258 { "objc1_class_ext", obj_mach_o_objc_section, 22}, /* ObjC-1 extension. */
1259 { "objc1_property_list", obj_mach_o_objc_section, 23}, /* ObjC-1 extension. */
1260 { "objc1_protocol_ext", obj_mach_o_objc_section, 24}, /* ObjC-1 extension. */
1262 { "debug_frame", obj_mach_o_debug_section, 1}, /* extension. */
1263 { "debug_info", obj_mach_o_debug_section, 2}, /* extension. */
1264 { "debug_abbrev", obj_mach_o_debug_section, 3}, /* extension. */
1265 { "debug_aranges", obj_mach_o_debug_section, 4}, /* extension. */
1266 { "debug_macinfo", obj_mach_o_debug_section, 5}, /* extension. */
1267 { "debug_line", obj_mach_o_debug_section, 6}, /* extension. */
1268 { "debug_loc", obj_mach_o_debug_section, 7}, /* extension. */
1269 { "debug_pubnames", obj_mach_o_debug_section, 8}, /* extension. */
1270 { "debug_pubtypes", obj_mach_o_debug_section, 9}, /* extension. */
1271 { "debug_str", obj_mach_o_debug_section, 10}, /* extension. */
1272 { "debug_ranges", obj_mach_o_debug_section, 11}, /* extension. */
1273 { "debug_macro", obj_mach_o_debug_section, 12}, /* extension. */
1275 { "lazy_symbol_pointer", obj_mach_o_opt_tgt_section, 1},
1276 { "lazy_symbol_pointer2", obj_mach_o_opt_tgt_section, 2}, /* extension. */
1277 { "lazy_symbol_pointer3", obj_mach_o_opt_tgt_section, 3}, /* extension. */
1278 { "non_lazy_symbol_pointer", obj_mach_o_opt_tgt_section, 4},
1279 { "non_lazy_symbol_pointer_x86", obj_mach_o_opt_tgt_section, 5}, /* extension. */
1280 { "symbol_stub", obj_mach_o_opt_tgt_section, 6},
1281 { "symbol_stub1", obj_mach_o_opt_tgt_section, 7}, /* extension. */
1282 { "picsymbol_stub", obj_mach_o_opt_tgt_section, 8}, /* extension. */
1283 { "picsymbol_stub1", obj_mach_o_opt_tgt_section, 9}, /* extension. */
1284 { "picsymbol_stub2", obj_mach_o_opt_tgt_section, 4}, /* extension. */
1285 { "picsymbol_stub3", obj_mach_o_opt_tgt_section, 4}, /* extension. */
1287 { "section", obj_mach_o_section, 0},
1288 { "zerofill", obj_mach_o_zerofill, 0},
1290 /* Symbol qualifiers. */
1291 {"local", obj_mach_o_sym_qual, OBJ_MACH_O_SYM_LOCAL},
1292 {"globl", obj_mach_o_sym_qual, OBJ_MACH_O_SYM_GLOBL},
1293 {"reference", obj_mach_o_sym_qual, OBJ_MACH_O_SYM_REFERENCE},
1294 {"weak_reference", obj_mach_o_sym_qual, OBJ_MACH_O_SYM_WEAK_REF},
1295 {"lazy_reference", obj_mach_o_sym_qual, OBJ_MACH_O_SYM_LAZY_REF},
1296 {"weak_definition", obj_mach_o_sym_qual, OBJ_MACH_O_SYM_WEAK_DEF},
1297 {"private_extern", obj_mach_o_sym_qual, OBJ_MACH_O_SYM_PRIV_EXT},
1298 {"no_dead_strip", obj_mach_o_sym_qual, OBJ_MACH_O_SYM_NO_DEAD_STRIP},
1299 {"weak", obj_mach_o_sym_qual, OBJ_MACH_O_SYM_WEAK}, /* ext */
1301 { "indirect_symbol", obj_mach_o_indirect_symbol, 0},
1303 /* File flags. */
1304 { "subsections_via_symbols", obj_mach_o_fileprop,
1305 OBJ_MACH_O_FILE_PROP_SUBSECTS_VIA_SYMS},
1307 {NULL, NULL, 0}
1310 /* Determine the default n_type value for a symbol from its section. */
1312 static unsigned
1313 obj_mach_o_type_for_symbol (bfd_mach_o_asymbol *s)
1315 if (s->symbol.section == bfd_abs_section_ptr)
1316 return BFD_MACH_O_N_ABS;
1317 else if (s->symbol.section == bfd_com_section_ptr
1318 || s->symbol.section == bfd_und_section_ptr)
1319 return BFD_MACH_O_N_UNDF;
1320 else
1321 return BFD_MACH_O_N_SECT;
1324 /* We need to check the correspondence between some kinds of symbols and their
1325 sections. Common and BSS vars will seen via the obj_macho_comm() function.
1327 The earlier we can pick up a problem, the better the diagnostics will be.
1329 However, when symbol type information is attached, the symbol section will
1330 quite possibly be unknown. So we are stuck with checking (most of the)
1331 validity at the time the file is written (unfortunately, then one doesn't
1332 get line number information in the diagnostic). */
1334 /* Here we pick up the case where symbol qualifiers have been applied that
1335 are possibly incompatible with the section etc. that the symbol is defined
1336 in. */
1338 void obj_macho_frob_label (struct symbol *sp)
1340 bfd_mach_o_asymbol *s;
1341 unsigned base_type;
1342 bfd_mach_o_section *sec;
1343 int sectype = -1;
1345 /* Leave local symbols alone. */
1347 if (S_IS_LOCAL (sp))
1348 return;
1350 s = (bfd_mach_o_asymbol *) symbol_get_bfdsym (sp);
1351 /* Leave debug symbols alone. */
1352 if ((s->n_type & BFD_MACH_O_N_STAB) != 0)
1353 return;
1355 /* This is the base symbol type, that we mask in. */
1356 base_type = obj_mach_o_type_for_symbol (s);
1358 sec = bfd_mach_o_get_mach_o_section (s->symbol.section);
1359 if (sec != NULL)
1360 sectype = sec->flags & BFD_MACH_O_SECTION_TYPE_MASK;
1362 /* If there is a pre-existing qualifier, we can make some checks about
1363 validity now. */
1365 if(s->symbol.udata.i == SYM_MACHO_FIELDS_NOT_VALIDATED)
1367 if ((s->n_desc & BFD_MACH_O_N_WEAK_DEF)
1368 && sectype != BFD_MACH_O_S_COALESCED)
1369 as_bad (_("'%s' can't be a weak_definition (currently only supported"
1370 " in sections of type coalesced)"), s->symbol.name);
1372 /* Have we changed from an undefined to defined ref? */
1373 s->n_desc &= ~(REFE | LAZY);
1376 s->n_type &= ~BFD_MACH_O_N_TYPE;
1377 s->n_type |= base_type;
1380 /* This is the fall-back, we come here when we get to the end of the file and
1381 the symbol is not defined - or there are combinations of qualifiers required
1382 (e.g. global + weak_def). */
1385 obj_macho_frob_symbol (struct symbol *sp)
1387 bfd_mach_o_asymbol *s;
1388 unsigned base_type;
1389 bfd_mach_o_section *sec;
1390 int sectype = -1;
1392 /* Leave local symbols alone. */
1393 if (S_IS_LOCAL (sp))
1394 return 0;
1396 s = (bfd_mach_o_asymbol *) symbol_get_bfdsym (sp);
1397 /* Leave debug symbols alone. */
1398 if ((s->n_type & BFD_MACH_O_N_STAB) != 0)
1399 return 0;
1401 base_type = obj_mach_o_type_for_symbol (s);
1402 sec = bfd_mach_o_get_mach_o_section (s->symbol.section);
1403 if (sec != NULL)
1404 sectype = sec->flags & BFD_MACH_O_SECTION_TYPE_MASK;
1406 if (s->symbol.section == bfd_und_section_ptr)
1408 /* ??? Do we really gain much from implementing this as well as the
1409 mach-o specific ones? */
1410 if (s->symbol.flags & BSF_WEAK)
1411 s->n_desc |= BFD_MACH_O_N_WEAK_REF;
1413 /* Undefined syms, become extern. */
1414 s->n_type |= BFD_MACH_O_N_EXT;
1415 S_SET_EXTERNAL (sp);
1417 else if (s->symbol.section == bfd_com_section_ptr)
1419 /* ... so do comm. */
1420 s->n_type |= BFD_MACH_O_N_EXT;
1421 S_SET_EXTERNAL (sp);
1423 else
1425 if ((s->symbol.flags & BSF_WEAK)
1426 && (sectype == BFD_MACH_O_S_COALESCED)
1427 && (s->n_type & (BFD_MACH_O_N_PEXT | BFD_MACH_O_N_EXT)))
1428 s->n_desc |= BFD_MACH_O_N_WEAK_DEF;
1429 /* ??? we should do this - but then that reveals that the semantics of weak
1430 are different from what's supported in mach-o object files.
1431 else
1432 as_bad (_("'%s' can't be a weak_definition."),
1433 s->symbol.name); */
1436 if (s->symbol.udata.i == SYM_MACHO_FIELDS_UNSET)
1438 /* Anything here that should be added that is non-standard. */
1439 s->n_desc &= ~BFD_MACH_O_REFERENCE_MASK;
1440 s->symbol.udata.i = SYM_MACHO_FIELDS_NOT_VALIDATED;
1442 else if (s->symbol.udata.i == SYM_MACHO_FIELDS_NOT_VALIDATED)
1444 /* Try to validate any combinations. */
1445 if (s->n_desc & BFD_MACH_O_N_WEAK_DEF)
1447 if (s->symbol.section == bfd_und_section_ptr)
1448 as_bad (_("'%s' can't be a weak_definition (since it is"
1449 " undefined)"), s->symbol.name);
1450 else if (sectype != BFD_MACH_O_S_COALESCED)
1451 as_bad (_("'%s' can't be a weak_definition (currently only supported"
1452 " in sections of type coalesced)"), s->symbol.name);
1453 else if (! (s->n_type & (BFD_MACH_O_N_PEXT | BFD_MACH_O_N_EXT)))
1454 as_bad (_("Non-global symbol: '%s' can't be a weak_definition."),
1455 s->symbol.name);
1459 else
1460 as_bad (_("internal error: [%s] unexpected code [%lx] in frob symbol"),
1461 s->symbol.name, (unsigned long)s->symbol.udata.i);
1463 s->n_type &= ~BFD_MACH_O_N_TYPE;
1464 s->n_type |= base_type;
1466 if (s->symbol.flags & BSF_GLOBAL)
1467 s->n_type |= BFD_MACH_O_N_EXT;
1469 /* This cuts both ways - we promote some things to external above. */
1470 if (s->n_type & (BFD_MACH_O_N_PEXT | BFD_MACH_O_N_EXT))
1471 S_SET_EXTERNAL (sp);
1473 return 0;
1476 /* Zerofill and GB Zerofill sections must be sorted to follow all other
1477 sections in their segments.
1479 The native 'as' leaves the sections physically in the order they appear in
1480 the source, and adjusts the section VMAs to meet the constraint.
1482 We follow this for now - if nothing else, it makes comparison easier.
1484 An alternative implementation would be to sort the sections as ld requires.
1485 It might be advantageous to implement such a scheme in the future (or even
1486 to make the style of section ordering user-selectable). */
1488 typedef struct obj_mach_o_set_vma_data
1490 bfd_vma vma;
1491 unsigned vma_pass;
1492 unsigned zerofill_seen;
1493 unsigned gb_zerofill_seen;
1494 } obj_mach_o_set_vma_data;
1496 /* We do (possibly) three passes through to set the vma, so that:
1498 zerofill sections get VMAs after all others in their segment
1499 GB zerofill get VMAs last.
1501 As we go, we notice if we see any Zerofill or GB Zerofill sections, so that
1502 we can skip the additional passes if there's nothing to do. */
1504 static void
1505 obj_mach_o_set_section_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *v_p)
1507 bfd_mach_o_section *ms = bfd_mach_o_get_mach_o_section (sec);
1508 unsigned bfd_align = bfd_get_section_alignment (abfd, sec);
1509 obj_mach_o_set_vma_data *p = (struct obj_mach_o_set_vma_data *)v_p;
1510 unsigned sectype = (ms->flags & BFD_MACH_O_SECTION_TYPE_MASK);
1511 unsigned zf;
1513 zf = 0;
1514 if (sectype == BFD_MACH_O_S_ZEROFILL)
1516 zf = 1;
1517 p->zerofill_seen = zf;
1519 else if (sectype == BFD_MACH_O_S_GB_ZEROFILL)
1521 zf = 2;
1522 p->gb_zerofill_seen = zf;
1525 if (p->vma_pass != zf)
1526 return;
1528 /* We know the section size now - so make a vma for the section just
1529 based on order. */
1530 ms->size = bfd_get_section_size (sec);
1532 /* Make sure that the align agrees, and set to the largest value chosen. */
1533 ms->align = ms->align > bfd_align ? ms->align : bfd_align;
1534 bfd_set_section_alignment (abfd, sec, ms->align);
1536 p->vma += (1 << ms->align) - 1;
1537 p->vma &= ~((1 << ms->align) - 1);
1538 ms->addr = p->vma;
1539 bfd_set_section_vma (abfd, sec, p->vma);
1540 p->vma += ms->size;
1543 /* (potentially) three passes over the sections, setting VMA. We skip the
1544 {gb}zerofill passes if we didn't see any of the relevant sections. */
1546 void obj_mach_o_post_relax_hook (void)
1548 obj_mach_o_set_vma_data d;
1550 memset (&d, 0, sizeof (d));
1552 bfd_map_over_sections (stdoutput, obj_mach_o_set_section_vma, (char *) &d);
1553 if ((d.vma_pass = d.zerofill_seen) != 0)
1554 bfd_map_over_sections (stdoutput, obj_mach_o_set_section_vma, (char *) &d);
1555 if ((d.vma_pass = d.gb_zerofill_seen) != 0)
1556 bfd_map_over_sections (stdoutput, obj_mach_o_set_section_vma, (char *) &d);
1559 static void
1560 obj_mach_o_set_indirect_symbols (bfd *abfd, asection *sec,
1561 void *xxx ATTRIBUTE_UNUSED)
1563 bfd_vma sect_size = bfd_section_size (abfd, sec);
1564 bfd_mach_o_section *ms = bfd_mach_o_get_mach_o_section (sec);
1565 unsigned lazy = 0;
1567 /* See if we have any indirect syms to consider. */
1568 if (indirect_syms == NULL)
1569 return;
1571 /* Process indirect symbols.
1572 Check for errors, if OK attach them as a flat array to the section
1573 for which they are defined. */
1575 switch (ms->flags & BFD_MACH_O_SECTION_TYPE_MASK)
1577 case BFD_MACH_O_S_SYMBOL_STUBS:
1578 case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS:
1579 lazy = LAZY;
1580 /* Fall through. */
1581 case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS:
1583 unsigned int nactual = 0;
1584 unsigned int ncalc;
1585 obj_mach_o_indirect_sym *isym;
1586 obj_mach_o_indirect_sym *list = NULL;
1587 obj_mach_o_indirect_sym *list_tail = NULL;
1588 unsigned long eltsiz =
1589 bfd_mach_o_section_get_entry_size (abfd, ms);
1591 for (isym = indirect_syms; isym != NULL; isym = isym->next)
1593 if (isym->sect == sec)
1595 nactual++;
1596 if (list == NULL)
1597 list = isym;
1598 else
1599 list_tail->next = isym;
1600 list_tail = isym;
1604 /* If none are in this section, stop here. */
1605 if (nactual == 0)
1606 break;
1608 /* If we somehow added indirect symbols to a section with a zero
1609 entry size, we're dead ... */
1610 gas_assert (eltsiz != 0);
1612 ncalc = (unsigned int) (sect_size / eltsiz);
1613 if (nactual != ncalc)
1614 as_bad (_("the number of .indirect_symbols defined in section %s"
1615 " does not match the number expected (%d defined, %d"
1616 " expected)"), sec->name, nactual, ncalc);
1617 else
1619 unsigned n;
1620 bfd_mach_o_asymbol *sym;
1621 ms->indirect_syms =
1622 bfd_zalloc (abfd,
1623 nactual * sizeof (bfd_mach_o_asymbol *));
1625 if (ms->indirect_syms == NULL)
1627 as_fatal (_("internal error: failed to allocate %d indirect"
1628 "symbol pointers"), nactual);
1631 for (isym = list, n = 0; isym != NULL; isym = isym->next, n++)
1633 sym = (bfd_mach_o_asymbol *)symbol_get_bfdsym (isym->sym);
1634 /* Array is init to NULL & NULL signals a local symbol
1635 If the section is lazy-bound, we need to keep the
1636 reference to the symbol, since dyld can override.
1638 Absolute symbols are handled specially. */
1639 if (sym->symbol.section == bfd_abs_section_ptr)
1640 ms->indirect_syms[n] = sym;
1641 else if (S_IS_LOCAL (isym->sym) && ! lazy)
1643 else
1645 if (sym == NULL)
1647 /* If the symbols is external ... */
1648 else if (S_IS_EXTERNAL (isym->sym)
1649 || (sym->n_type & BFD_MACH_O_N_EXT)
1650 || ! S_IS_DEFINED (isym->sym)
1651 || lazy)
1653 sym->n_desc &= ~LAZY;
1654 /* ... it can be lazy, if not defined or hidden. */
1655 if ((sym->n_type & BFD_MACH_O_N_TYPE)
1656 == BFD_MACH_O_N_UNDF
1657 && ! (sym->n_type & BFD_MACH_O_N_PEXT)
1658 && (sym->n_type & BFD_MACH_O_N_EXT))
1659 sym->n_desc |= lazy;
1660 ms->indirect_syms[n] = sym;
1666 break;
1668 default:
1669 break;
1673 /* The process of relocation could alter what's externally visible, thus we
1674 leave setting the indirect symbols until last. */
1676 void
1677 obj_mach_o_frob_file_after_relocs (void)
1679 bfd_map_over_sections (stdoutput, obj_mach_o_set_indirect_symbols, (char *) 0);
1682 /* Reverse relocations order to make ld happy. */
1684 void
1685 obj_mach_o_reorder_section_relocs (asection *sec, arelent **rels, unsigned int n)
1687 unsigned int i;
1688 unsigned int max = n / 2;
1690 for (i = 0; i < max; i++)
1692 arelent *r = rels[i];
1693 rels[i] = rels[n - i - 1];
1694 rels[n - i - 1] = r;
1696 bfd_set_reloc (stdoutput, sec, rels, n);
1699 /* Support stabs for mach-o. */
1701 void
1702 obj_mach_o_process_stab (int what, const char *string,
1703 int type, int other, int desc)
1705 symbolS *symbolP;
1706 bfd_mach_o_asymbol *s;
1708 switch (what)
1710 case 'd':
1711 symbolP = symbol_new ("", now_seg, frag_now_fix (), frag_now);
1712 /* Special stabd NULL name indicator. */
1713 S_SET_NAME (symbolP, NULL);
1714 break;
1716 case 'n':
1717 case 's':
1718 symbolP = symbol_new (string, undefined_section, (valueT) 0,
1719 &zero_address_frag);
1720 pseudo_set (symbolP);
1721 break;
1723 default:
1724 as_bad(_("unrecognized stab type '%c'"), (char)what);
1725 abort ();
1726 break;
1729 s = (bfd_mach_o_asymbol *) symbol_get_bfdsym (symbolP);
1730 s->n_type = type;
1731 s->n_desc = desc;
1732 /* For stabd, this will eventually get overwritten by the section number. */
1733 s->n_sect = other;
1735 /* It's a debug symbol. */
1736 s->symbol.flags |= BSF_DEBUGGING;