bfd/
[deliverable/binutils-gdb.git] / gas / config / obj-macho.c
CommitLineData
e57f8c65 1/* Mach-O object file format
68588f95 2 Copyright 2009, 2011, 2012 Free Software Foundation, Inc.
e57f8c65
TG
3
4 This file is part of GAS, the GNU Assembler.
5
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 3,
9 or (at your option) any later version.
10
11 GAS is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
14 the GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to the Free
18 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
19 02110-1301, USA. */
20
a4551119
TG
21/* Here we handle the mach-o directives that are common to all architectures.
22
23 Most significant are mach-o named sections and a variety of symbol type
24 decorations. */
25
26/* Mach-O supports multiple, named segments each of which may contain
27 multiple named sections. Thus the concept of subsectioning is
28 handled by (say) having a __TEXT segment with appropriate flags from
29 which subsections are generated like __text, __const etc.
30
31 The well-known as short-hand section switch directives like .text, .data
32 etc. are mapped onto predefined segment/section pairs using facilites
33 supplied by the mach-o port of bfd.
34
35 A number of additional mach-o short-hand section switch directives are
36 also defined. */
37
e57f8c65
TG
38#define OBJ_HEADER "obj-macho.h"
39
40#include "as.h"
74a6005f
TG
41#include "subsegs.h"
42#include "symbols.h"
43#include "write.h"
e57f8c65 44#include "mach-o.h"
74a6005f 45#include "mach-o/loader.h"
a4551119
TG
46#include "obj-macho.h"
47
c3402d20
IS
48#include <string.h>
49
8cf6e084
IS
50/* Forward decls. */
51static segT obj_mach_o_segT_from_bfd_name (const char *, int);
bcf0aac6 52
a4551119
TG
53/* TODO: Implement "-dynamic"/"-static" command line options. */
54
55static int obj_mach_o_is_static;
56
bcf0aac6
IS
57/* TODO: Implement the "-n" command line option to suppress the initial
58 switch to the text segment. */
59static int obj_mach_o_start_with_text_section = 1;
60
a4551119
TG
61/* Allow for special re-ordering on output. */
62
bcf0aac6
IS
63static int obj_mach_o_seen_objc_section;
64
65/* Start-up: At present, just create the sections we want. */
66void
67mach_o_begin (void)
68{
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)
73 {
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)
77 {
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;
81 }
82 }
83}
e57f8c65 84
0c9ef0f0
TG
85/* Remember the subsections_by_symbols state in case we need to reset
86 the file flags. */
0c9ef0f0 87
b22161d6 88static int obj_mach_o_subsections_by_symbols;
e57f8c65 89
a4551119
TG
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.
95
96 It trims leading and trailing space. */
97
98static int
99collect_16char_name (char *dest, const char *msg, int require_comma)
100{
101 char c, *namstart;
102
103 SKIP_WHITESPACE ();
104 namstart = input_line_pointer;
105
106 while ( (c = *input_line_pointer) != ','
107 && !is_end_of_line[(unsigned char) c])
108 input_line_pointer++;
109
110 {
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)
116 {
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;
122 }
123 if (len > 0)
124 memcpy (dest, namstart, len);
125 }
126
127 if (c != ',' && require_comma)
128 {
129 as_bad (_("expected a %s name followed by a `,'"), msg);
130 return 1;
131 }
132
133 return 0;
134}
135
8cf6e084
IS
136static int
137obj_mach_o_get_section_names (char *seg, char *sec,
138 unsigned segl, unsigned secl)
139{
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))
144 {
145 ignore_rest_of_line ();
146 return 0;
147 }
148 input_line_pointer++; /* Skip the terminating ',' */
149
150 /* Parse section name, which can be empty. */
151 memset (sec, 0, secl);
152 collect_16char_name (sec, "section", 0);
153 return 1;
154}
155
156/* Build (or get) a section from the mach-o description - which includes
157 optional definitions for type, attributes, alignment and stub size.
158
159 BFD supplies default values for sections which have a canonical name. */
160
161#define SECT_TYPE_SPECIFIED 0x0001
162#define SECT_ATTR_SPECIFIED 0x0002
163#define SECT_ALGN_SPECIFIED 0x0004
164
165static segT
166obj_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)
170{
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;
177
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);
181
182 /* TODO: more checking of whether overides are acually allowed. */
183
184 if (xlat != NULL)
185 {
186 name = xstrdup (xlat->bfd_name);
187 sectype = xlat->macho_sectype;
188 if (specified_mask & SECT_TYPE_SPECIFIED)
189 {
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;
197 }
198 secattr = xlat->macho_secattr;
199 secalign = xlat->sectalign;
200 flags = xlat->bfd_flags;
201 }
202 else
203 {
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;
210
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;
224 }
225
226 /* For now, just use what the user provided. */
227
228 if (specified_mask & SECT_ATTR_SPECIFIED)
229 secattr = usecattr;
230
231 if (specified_mask & SECT_ALGN_SPECIFIED)
232 secalign = ualign;
233
234 /* Sub-segments don't exists as is on Mach-O. */
235 sec = subseg_new (name, 0);
236
237 oldflags = bfd_get_section_flags (stdoutput, sec);
238 msect = bfd_mach_o_get_mach_o_section (sec);
239
240 if (oldflags == SEC_NO_FLAGS)
241 {
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 ()));
247
248 strncpy (msect->segname, segname, sizeof (msect->segname));
249 strncpy (msect->sectname, sectname, sizeof (msect->sectname));
250
251 msect->align = secalign;
252 msect->flags = sectype | secattr;
253 msect->reserved2 = stub_size;
254
255 if (sectype == BFD_MACH_O_S_ZEROFILL
256 || sectype == BFD_MACH_O_S_GB_ZEROFILL)
257 seg_info (sec)->bss = 1;
258 }
259 else if (flags != SEC_NO_FLAGS)
260 {
261 if (flags != oldflags
262 || msect->flags != (secattr | sectype))
263 as_warn (_("Ignoring changed section attributes for %s"), name);
264 }
265
266 return sec;
267}
268
a4551119
TG
269/* .section
270
271 The '.section' specification syntax looks like:
272 .section <segment> , <section> [, type [, attribs [, size]]]
273
274 White space is allowed everywhere between elements.
275
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 ",,").
280
281 There is only a single section type for any entry.
282
283 There may be multiple attributes, they are delimited by `+'.
284
285 Not all section types and attributes are accepted by the Darwin system
286 assemblers as user-specifiable - although, at present, we do here. */
74a6005f
TG
287
288static void
289obj_mach_o_section (int ignore ATTRIBUTE_UNUSED)
290{
a4551119 291 unsigned int sectype = BFD_MACH_O_S_REGULAR;
8cf6e084 292 unsigned int specified_mask = 0;
74a6005f
TG
293 unsigned int secattr = 0;
294 offsetT sizeof_stub = 0;
8cf6e084 295 segT new_seg;
a4551119
TG
296 char segname[17];
297 char sectname[17];
74a6005f 298
ab76eeaf
IS
299#ifdef md_flush_pending_output
300 md_flush_pending_output ();
301#endif
302
8cf6e084
IS
303 /* Get the User's segment annd section names. */
304 if (! obj_mach_o_get_section_names (segname, sectname, 17, 17))
305 return;
74a6005f 306
8cf6e084 307 /* Parse section type, if present. */
74a6005f
TG
308 if (*input_line_pointer == ',')
309 {
8cf6e084
IS
310 char *p;
311 char c;
a4551119
TG
312 char tmpc;
313 int len;
74a6005f 314 input_line_pointer++;
a4551119 315 SKIP_WHITESPACE ();
74a6005f 316 p = input_line_pointer;
a4551119
TG
317 while ((c = *input_line_pointer) != ','
318 && !is_end_of_line[(unsigned char) c])
319 input_line_pointer++;
320
321 len = input_line_pointer - p;
322 /* strip trailing spaces. */
323 while (len > 0 && p[len-1] == ' ')
324 len--;
325 tmpc = p[len];
326
327 /* Temporarily make a string from the token. */
328 p[len] = 0;
ab76eeaf 329 sectype = bfd_mach_o_get_section_type_from_name (stdoutput, p);
a4551119 330 if (sectype > 255) /* Max Section ID == 255. */
74a6005f
TG
331 {
332 as_bad (_("unknown or invalid section type '%s'"), p);
ab76eeaf
IS
333 p[len] = tmpc;
334 ignore_rest_of_line ();
335 return;
74a6005f 336 }
a4551119 337 else
8cf6e084 338 specified_mask |= SECT_TYPE_SPECIFIED;
a4551119 339 /* Restore. */
ab76eeaf 340 p[len] = tmpc;
a4551119
TG
341
342 /* Parse attributes.
343 TODO: check validity of attributes for section type. */
8cf6e084
IS
344 if ((specified_mask & SECT_TYPE_SPECIFIED)
345 && c == ',')
74a6005f
TG
346 {
347 do
348 {
349 int attr;
350
a4551119 351 /* Skip initial `,' and subsequent `+'. */
74a6005f 352 input_line_pointer++;
a4551119
TG
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++;
359
360 len = input_line_pointer - p;
361 /* strip trailing spaces. */
362 while (len > 0 && p[len-1] == ' ')
363 len--;
364 tmpc = p[len];
365
366 /* Temporarily make a string from the token. */
367 p[len] ='\0';
74a6005f 368 attr = bfd_mach_o_get_section_attribute_from_name (p);
a4551119 369 if (attr == -1)
ab76eeaf
IS
370 {
371 as_bad (_("unknown or invalid section attribute '%s'"), p);
372 p[len] = tmpc;
373 ignore_rest_of_line ();
374 return;
375 }
74a6005f 376 else
a4551119 377 {
8cf6e084 378 specified_mask |= SECT_ATTR_SPECIFIED;
a4551119
TG
379 secattr |= attr;
380 }
381 /* Restore. */
382 p[len] = tmpc;
74a6005f
TG
383 }
384 while (*input_line_pointer == '+');
385
386 /* Parse sizeof_stub. */
8cf6e084
IS
387 if ((specified_mask & SECT_ATTR_SPECIFIED)
388 && *input_line_pointer == ',')
74a6005f
TG
389 {
390 if (sectype != BFD_MACH_O_S_SYMBOL_STUBS)
ab76eeaf
IS
391 {
392 as_bad (_("unexpected section size information"));
393 ignore_rest_of_line ();
394 return;
395 }
74a6005f 396
a4551119 397 input_line_pointer++;
74a6005f
TG
398 sizeof_stub = get_absolute_expression ();
399 }
8cf6e084
IS
400 else if ((specified_mask & SECT_ATTR_SPECIFIED)
401 && sectype == BFD_MACH_O_S_SYMBOL_STUBS)
ab76eeaf
IS
402 {
403 as_bad (_("missing sizeof_stub expression"));
404 ignore_rest_of_line ();
405 return;
406 }
74a6005f
TG
407 }
408 }
74a6005f 409
8cf6e084
IS
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)
a4551119 414 {
8cf6e084
IS
415 subseg_set (new_seg, 0);
416 demand_empty_rest_of_line ();
a4551119 417 }
8cf6e084 418}
74a6005f 419
8cf6e084 420/* .zerofill segname, sectname [, symbolname, size [, align]]
74a6005f 421
8cf6e084 422 Zerofill switches, temporarily, to a sect of type 'zerofill'.
74a6005f 423
8cf6e084
IS
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. */
426
427static void
428obj_mach_o_zerofill (int ignore ATTRIBUTE_UNUSED)
429{
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;
facf03f2 437 offsetT size = 0;
8cf6e084
IS
438
439#ifdef md_flush_pending_output
440 md_flush_pending_output ();
441#endif
442
443 /* Get the User's segment annd section names. */
444 if (! obj_mach_o_get_section_names (segname, sectname, 17, 17))
445 return;
446
447 /* Parse variable definition, if present. */
448 if (*input_line_pointer == ',')
74a6005f 449 {
8cf6e084
IS
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;
457
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;
465
466 if (name == p)
a4551119 467 {
8cf6e084
IS
468 as_bad (_("expected symbol name"));
469 ignore_rest_of_line ();
470 goto done;
471 }
472
473 SKIP_WHITESPACE ();
474 if (*input_line_pointer == ',')
475 input_line_pointer++;
476
477 expression_and_evaluate (&exp);
478 if (exp.X_op != O_constant
479 && exp.X_op != O_absent)
480 {
481 as_bad (_("bad or irreducible absolute expression"));
482 ignore_rest_of_line ();
483 goto done;
484 }
485 else if (exp.X_op == O_absent)
486 {
487 as_bad (_("missing size expression"));
488 ignore_rest_of_line ();
489 goto done;
490 }
491
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)
495 {
496 as_warn (_("size (%ld) out of range, ignored"),
497 (long) exp.X_add_number);
498 ignore_rest_of_line ();
499 goto done;
500 }
501
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))
505 {
506 as_bad (_("symbol `%s' is already defined"), name);
507 *p = c;
508 ignore_rest_of_line ();
509 goto done;
510 }
511
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);
518
519 *p = c; /* Restore the termination char. */
520
521 SKIP_WHITESPACE ();
522 if (*input_line_pointer == ',')
523 {
524 align = (unsigned int) parse_align (0);
525 if (align == (unsigned int) -1)
526 {
527 as_warn (_("align value not recognized, using size"));
528 align = size;
529 }
530 if (align > 15)
531 {
532 as_warn (_("Alignment (%lu) too large: 15 assumed."),
533 (unsigned long)align);
534 align = 15;
535 }
536 specified_mask |= SECT_ALGN_SPECIFIED;
a4551119 537 }
74a6005f 538 }
8cf6e084
IS
539 /* else just a section definition. */
540
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;
548
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;
554
555 subseg_set (new_seg, 0);
556
557 if (sym != NULL)
74a6005f 558 {
8cf6e084
IS
559 char *pfrag;
560
561 if (align)
562 {
563 record_alignment (new_seg, align);
564 frag_align (align, 0, 0);
565 }
566
567 /* Detach from old frag. */
568 if (S_GET_SEGMENT (sym) == new_seg)
569 symbol_get_frag (sym)->fr_symbol = NULL;
570
571 symbol_set_frag (sym, frag_now);
572 pfrag = frag_var (rs_org, 1, 1, 0, sym, size, NULL);
573 *pfrag = 0;
574
575 S_SET_SEGMENT (sym, new_seg);
576 if (new_seg == bss_section)
577 S_CLEAR_EXTERNAL (sym);
74a6005f 578 }
8cf6e084
IS
579
580done:
581 /* switch back to the section that was current before the .zerofill. */
582 subseg_set (old_seg, 0);
74a6005f
TG
583}
584
a4551119
TG
585static segT
586obj_mach_o_segT_from_bfd_name (const char *nam, int must_succeed)
74a6005f 587{
a4551119
TG
588 const mach_o_section_name_xlat *xlat;
589 const char *segn;
590 segT sec;
591
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)
596 {
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;
601 }
602
603 sec = bfd_get_section_by_name (stdoutput, nam);
604 if (sec == NULL)
605 {
606 bfd_mach_o_section *msect;
607
608 sec = subseg_force_new (xlat->bfd_name, 0);
609
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;
614
615 if ((msect->flags & BFD_MACH_O_SECTION_TYPE_MASK)
616 == BFD_MACH_O_S_ZEROFILL)
617 seg_info (sec)->bss = 1;
618 }
619
620 return sec;
621}
622
623static const char * const known_sections[] =
624{
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"
74a6005f
TG
643};
644
a4551119 645/* Interface for a known non-optional section directive. */
74a6005f
TG
646
647static void
648obj_mach_o_known_section (int sect_index)
649{
a4551119
TG
650 segT section;
651
652#ifdef md_flush_pending_output
653 md_flush_pending_output ();
654#endif
655
656 section = obj_mach_o_segT_from_bfd_name (known_sections[sect_index], 1);
657 if (section != NULL)
658 subseg_set (section, 0);
659
660 /* else, we leave the section as it was; there was a fatal error anyway. */
661}
662
663static const char * const objc_sections[] =
664{
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. */
690};
691
692/* This currently does the same as known_sections, but kept separate for
693 ease of maintenance. */
694
695static void
696obj_mach_o_objc_section (int sect_index)
697{
698 segT section;
699
700#ifdef md_flush_pending_output
701 md_flush_pending_output ();
702#endif
703
704 section = obj_mach_o_segT_from_bfd_name (objc_sections[sect_index], 1);
705 if (section != NULL)
706 {
bcf0aac6
IS
707 obj_mach_o_seen_objc_section = 1; /* We need to ensure that certain
708 sections are present and in the
709 right order. */
a4551119
TG
710 subseg_set (section, 0);
711 }
712
713 /* else, we leave the section as it was; there was a fatal error anyway. */
714}
715
716/* Debug section directives. */
717
718static const char * const debug_sections[] =
719{
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"
734};
735
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. */
739
740static void
741obj_mach_o_debug_section (int sect_index)
742{
743 segT section;
744
745#ifdef md_flush_pending_output
746 md_flush_pending_output ();
747#endif
748
749 section = obj_mach_o_segT_from_bfd_name (debug_sections[sect_index], 1);
750 if (section != NULL)
751 subseg_set (section, 0);
752
753 /* else, we leave the section as it was; there was a fatal error anyway. */
754}
755
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. */
758
759struct opt_tgt_sect
760{
761 const char *name;
762 unsigned x86_val;
763 unsigned ppc_val;
764};
765
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'. */
768
769static const struct opt_tgt_sect tgt_sections[] =
770{
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 */
783};
784
785/* Interface for an optional section directive. */
786
787static void
788obj_mach_o_opt_tgt_section (int sect_index)
789{
790 const struct opt_tgt_sect *tgtsct = &tgt_sections[sect_index];
791 segT section;
74a6005f
TG
792
793#ifdef md_flush_pending_output
794 md_flush_pending_output ();
795#endif
796
a4551119
TG
797 section = obj_mach_o_segT_from_bfd_name (tgtsct->name, 0);
798 if (section == NULL)
74a6005f 799 {
a4551119
TG
800 as_bad (_("%s is not used for the selected target"), tgtsct->name);
801 /* Leave the section as it is. */
74a6005f
TG
802 }
803 else
804 {
a4551119
TG
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
814 }
815}
74a6005f 816
a4551119
TG
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. */
74a6005f 821
a4551119
TG
822static void
823obj_mach_o_base_section (int sect_index)
824{
825 segT section;
826
827#ifdef md_flush_pending_output
828 md_flush_pending_output ();
829#endif
830
831 /* We don't support numeric (or any other) qualifications on the
832 well-known section shorthands. */
833 demand_empty_rest_of_line ();
834
835 switch (sect_index)
836 {
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)
843 {
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;
847 }
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)
858 {
859 bss_section = obj_mach_o_segT_from_bfd_name (BSS_SECTION_NAME, 1);
860 seg_info (bss_section)->bss = 1;
861 }
862 section = bss_section;
863 break;
864 default:
865 as_fatal (_("internal error: base section index out of range"));
866 return;
867 break;
74a6005f 868 }
a4551119 869 subseg_set (section, 0);
74a6005f
TG
870}
871
a4551119
TG
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. */
74a6005f
TG
875
876static symbolS *
a4551119
TG
877obj_mach_o_common_parse (int is_local, symbolS *symbolP,
878 addressT size)
74a6005f
TG
879{
880 addressT align = 0;
b22161d6 881 bfd_mach_o_asymbol *s;
74a6005f 882
8cf6e084
IS
883 SKIP_WHITESPACE ();
884
a4551119
TG
885 /* Both comm and lcomm take an optional alignment, as a power
886 of two between 1 and 15. */
74a6005f
TG
887 if (*input_line_pointer == ',')
888 {
a4551119 889 /* We expect a power of 2. */
74a6005f
TG
890 align = parse_align (0);
891 if (align == (addressT) -1)
892 return NULL;
a4551119
TG
893 if (align > 15)
894 {
895 as_warn (_("Alignment (%lu) too large: 15 assumed."),
896 (unsigned long)align);
897 align = 15;
898 }
74a6005f
TG
899 }
900
b22161d6 901 s = (bfd_mach_o_asymbol *) symbol_get_bfdsym (symbolP);
a4551119
TG
902 if (is_local)
903 {
904 /* Create the BSS section on demand. */
905 if (bss_section == NULL)
906 {
907 bss_section = obj_mach_o_segT_from_bfd_name (BSS_SECTION_NAME, 1);
908 seg_info (bss_section)->bss = 1;
909 }
910 bss_alloc (symbolP, size, align);
b22161d6 911 s->n_type = BFD_MACH_O_N_SECT;
a4551119
TG
912 S_CLEAR_EXTERNAL (symbolP);
913 }
914 else
915 {
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);
b22161d6 920 s->n_type = BFD_MACH_O_N_UNDF | BFD_MACH_O_N_EXT;
a4551119 921 }
74a6005f 922
b22161d6
IS
923 /* This is a data object (whatever we choose that to mean). */
924 s->symbol.flags |= BSF_OBJECT;
925
926 /* We've set symbol qualifiers, so validate if you can. */
927 s->symbol.udata.i = SYM_MACHO_FIELDS_NOT_VALIDATED;
74a6005f
TG
928
929 return symbolP;
930}
931
932static void
a4551119 933obj_mach_o_comm (int is_local)
74a6005f 934{
a4551119 935 s_comm_internal (is_local, obj_mach_o_common_parse);
74a6005f
TG
936}
937
0c9ef0f0
TG
938/* Set properties that apply to the whole file. At present, the only
939 one defined, is subsections_via_symbols. */
940
941typedef 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;
946
947static void
948obj_mach_o_fileprop (int prop)
74a6005f 949{
0c9ef0f0
TG
950 if (prop < 0 || prop >= OBJ_MACH_O_FILE_PROP_MAX)
951 as_fatal (_("internal error: bad file property ID %d"), prop);
952
953 switch ((obj_mach_o_file_properties) prop)
954 {
955 case OBJ_MACH_O_FILE_PROP_SUBSECTS_VIA_SYMS:
499963bc 956 obj_mach_o_subsections_by_symbols = 1;
0c9ef0f0
TG
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;
964 }
74a6005f
TG
965}
966
b22161d6
IS
967/* Temporary markers for symbol reference data.
968 Lazy will remain in place. */
969#define LAZY 0x01
970#define REFE 0x02
971
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. */
975
976typedef 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;
988
989/* Set Mach-O-specific symbol qualifiers. */
990
991static int
992obj_mach_o_set_symbol_qualifier (symbolS *sym, int type)
993{
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;
999
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.
1003
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. */
1007
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) ;
1013
1014 if (sec != NULL)
1015 sectype = sec->flags & BFD_MACH_O_SECTION_TYPE_MASK;
1016
1017 switch ((obj_mach_o_symbol_type) type)
1018 {
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))
1022 {
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;
1027 }
1028 else
1029 {
1030 s->n_type &= ~BFD_MACH_O_N_EXT;
1031 S_CLEAR_EXTERNAL (sym);
1032 }
1033 break;
1034
1035 case OBJ_MACH_O_SYM_PRIV_EXT:
1036 s->n_type |= BFD_MACH_O_N_PEXT ;
50d10658 1037 s->n_desc &= ~LAZY; /* The native tool switches this off too. */
b22161d6
IS
1038 /* We follow the system tools in marking PEXT as also global. */
1039 /* Fall through. */
1040
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;
1046
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;
1053
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;
1060
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;
1065
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;
1070
1071 case OBJ_MACH_O_SYM_WEAK_DEF:
1072 if (is_defined && sectype != BFD_MACH_O_S_COALESCED)
1073 {
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;
1078 }
1079 else
1080 s->n_desc |= BFD_MACH_O_N_WEAK_DEF;
1081 break;
1082
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;
1088
1089 default:
1090 break;
1091 }
1092
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;
1097}
1098
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. */
1103
1104static void
1105obj_mach_o_sym_qual (int ntype)
1106{
1107 char *name;
1108 char c;
1109 symbolS *symbolP;
1110
1111#ifdef md_flush_pending_output
1112 md_flush_pending_output ();
1113#endif
1114
1115 do
1116 {
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 == ',')
1125 {
1126 input_line_pointer++;
1127 SKIP_WHITESPACE ();
1128 if (is_end_of_line[(unsigned char) *input_line_pointer])
1129 c = '\n';
1130 }
1131 }
1132 while (c == ',');
1133
1134 demand_empty_rest_of_line ();
1135}
1136
50d10658
IS
1137typedef struct obj_mach_o_indirect_sym
1138{
1139 symbolS *sym;
1140 segT sect;
1141 struct obj_mach_o_indirect_sym *next;
1142} obj_mach_o_indirect_sym;
1143
1144/* We store in order an maintain a pointer to the last one - to save reversing
1145 later. */
1146obj_mach_o_indirect_sym *indirect_syms;
1147obj_mach_o_indirect_sym *indirect_syms_tail;
a4551119
TG
1148
1149static void
50d10658 1150obj_mach_o_indirect_symbol (int arg ATTRIBUTE_UNUSED)
a4551119 1151{
50d10658
IS
1152 bfd_mach_o_section *sec = bfd_mach_o_get_mach_o_section (now_seg);
1153
1154#ifdef md_flush_pending_output
1155 md_flush_pending_output ();
1156#endif
1157
1158 if (obj_mach_o_is_static)
1159 as_bad (_("use of .indirect_symbols requires `-dynamic'"));
1160
1161 switch (sec->flags & BFD_MACH_O_SECTION_TYPE_MASK)
1162 {
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:
1166 {
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);
1173
1174 if (elsize == 0)
1175 {
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;
1182 }
1183 *input_line_pointer = c;
1184
1185 isym = (obj_mach_o_indirect_sym *)
1186 xmalloc (sizeof (obj_mach_o_indirect_sym));
1187
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;
1198 }
1199 break;
1200
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;
1206 }
1207 demand_empty_rest_of_line ();
a4551119
TG
1208}
1209
e57f8c65
TG
1210const pseudo_typeS mach_o_pseudo_table[] =
1211{
a4551119 1212 /* Section directives. */
74a6005f 1213 { "comm", obj_mach_o_comm, 0 },
a4551119
TG
1214 { "lcomm", obj_mach_o_comm, 1 },
1215
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 */
1219
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},
1229
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},
1236
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. */
1261
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. */
1274
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. */
1286
1287 { "section", obj_mach_o_section, 0},
8cf6e084 1288 { "zerofill", obj_mach_o_zerofill, 0},
a4551119 1289
b22161d6
IS
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 */
1300
50d10658 1301 { "indirect_symbol", obj_mach_o_indirect_symbol, 0},
a4551119
TG
1302
1303 /* File flags. */
0c9ef0f0
TG
1304 { "subsections_via_symbols", obj_mach_o_fileprop,
1305 OBJ_MACH_O_FILE_PROP_SUBSECTS_VIA_SYMS},
e57f8c65
TG
1306
1307 {NULL, NULL, 0}
1308};
68588f95 1309
b22161d6
IS
1310/* Determine the default n_type value for a symbol from its section. */
1311
1312static unsigned
1313obj_mach_o_type_for_symbol (bfd_mach_o_asymbol *s)
1314{
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;
1322}
1323
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.
1326
1327 The earlier we can pick up a problem, the better the diagnostics will be.
1328
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). */
1333
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. */
1337
1338void obj_macho_frob_label (struct symbol *sp)
1339{
50d10658
IS
1340 bfd_mach_o_asymbol *s;
1341 unsigned base_type;
1342 bfd_mach_o_section *sec;
b22161d6
IS
1343 int sectype = -1;
1344
50d10658
IS
1345 /* Leave local symbols alone. */
1346
1347 if (S_IS_LOCAL (sp))
1348 return;
1349
1350 s = (bfd_mach_o_asymbol *) symbol_get_bfdsym (sp);
1351 /* Leave debug symbols alone. */
b22161d6 1352 if ((s->n_type & BFD_MACH_O_N_STAB) != 0)
50d10658
IS
1353 return;
1354
1355 /* This is the base symbol type, that we mask in. */
1356 base_type = obj_mach_o_type_for_symbol (s);
1357
1358 sec = bfd_mach_o_get_mach_o_section (s->symbol.section);
b22161d6
IS
1359 if (sec != NULL)
1360 sectype = sec->flags & BFD_MACH_O_SECTION_TYPE_MASK;
1361
1362 /* If there is a pre-existing qualifier, we can make some checks about
1363 validity now. */
1364
1365 if(s->symbol.udata.i == SYM_MACHO_FIELDS_NOT_VALIDATED)
1366 {
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);
1371
1372 /* Have we changed from an undefined to defined ref? */
1373 s->n_desc &= ~(REFE | LAZY);
1374 }
1375
1376 s->n_type &= ~BFD_MACH_O_N_TYPE;
1377 s->n_type |= base_type;
1378}
1379
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). */
1383
1384int
1385obj_macho_frob_symbol (struct symbol *sp)
1386{
50d10658
IS
1387 bfd_mach_o_asymbol *s;
1388 unsigned base_type;
1389 bfd_mach_o_section *sec;
b22161d6 1390 int sectype = -1;
50d10658
IS
1391
1392 /* Leave local symbols alone. */
1393 if (S_IS_LOCAL (sp))
1394 return 0;
1395
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;
1400
1401 base_type = obj_mach_o_type_for_symbol (s);
1402 sec = bfd_mach_o_get_mach_o_section (s->symbol.section);
b22161d6
IS
1403 if (sec != NULL)
1404 sectype = sec->flags & BFD_MACH_O_SECTION_TYPE_MASK;
1405
50d10658 1406 if (s->symbol.section == bfd_und_section_ptr)
b22161d6
IS
1407 {
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;
1412
50d10658
IS
1413 /* Undefined syms, become extern. */
1414 s->n_type |= BFD_MACH_O_N_EXT;
1415 S_SET_EXTERNAL (sp);
1416 }
1417 else if (s->symbol.section == bfd_com_section_ptr)
1418 {
1419 /* ... so do comm. */
1420 s->n_type |= BFD_MACH_O_N_EXT;
1421 S_SET_EXTERNAL (sp);
b22161d6
IS
1422 }
1423 else
1424 {
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); */
1434 }
1435
1436 if (s->symbol.udata.i == SYM_MACHO_FIELDS_UNSET)
1437 {
1438 /* Anything here that should be added that is non-standard. */
1439 s->n_desc &= ~BFD_MACH_O_REFERENCE_MASK;
50d10658 1440 s->symbol.udata.i = SYM_MACHO_FIELDS_NOT_VALIDATED;
b22161d6
IS
1441 }
1442 else if (s->symbol.udata.i == SYM_MACHO_FIELDS_NOT_VALIDATED)
1443 {
1444 /* Try to validate any combinations. */
1445 if (s->n_desc & BFD_MACH_O_N_WEAK_DEF)
1446 {
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);
1456 }
1457
1458 }
1459 else
1460 as_bad (_("internal error: [%s] unexpected code [%lx] in frob symbol"),
1461 s->symbol.name, (unsigned long)s->symbol.udata.i);
1462
1463 s->n_type &= ~BFD_MACH_O_N_TYPE;
1464 s->n_type |= base_type;
1465
1466 if (s->symbol.flags & BSF_GLOBAL)
1467 s->n_type |= BFD_MACH_O_N_EXT;
1468
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);
1472
1473 return 0;
1474}
1475
c3402d20
IS
1476/* Zerofill and GB Zerofill sections must be sorted to follow all other
1477 sections in their segments.
1478
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.
1481
1482 We follow this for now - if nothing else, it makes comparison easier.
1483
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). */
1487
1488typedef struct obj_mach_o_set_vma_data
1489{
1490 bfd_vma vma;
1491 unsigned vma_pass;
1492 unsigned zerofill_seen;
1493 unsigned gb_zerofill_seen;
1494} obj_mach_o_set_vma_data;
1495
1496/* We do (possibly) three passes through to set the vma, so that:
1497
1498 zerofill sections get VMAs after all others in their segment
1499 GB zerofill get VMAs last.
1500
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. */
1503
1504static void
1505obj_mach_o_set_section_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *v_p)
1506{
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;
1512
1513 zf = 0;
1514 if (sectype == BFD_MACH_O_S_ZEROFILL)
1515 {
1516 zf = 1;
1517 p->zerofill_seen = zf;
1518 }
1519 else if (sectype == BFD_MACH_O_S_GB_ZEROFILL)
1520 {
1521 zf = 2;
1522 p->gb_zerofill_seen = zf;
1523 }
1524
1525 if (p->vma_pass != zf)
1526 return;
1527
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);
1531
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);
1535
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;
1541}
1542
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. */
1545
1546void obj_mach_o_post_relax_hook (void)
1547{
1548 obj_mach_o_set_vma_data d;
1549
1550 memset (&d, 0, sizeof (d));
1551
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);
1557}
1558
50d10658
IS
1559static void
1560obj_mach_o_set_indirect_symbols (bfd *abfd, asection *sec,
1561 void *xxx ATTRIBUTE_UNUSED)
1562{
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;
1566
1567 /* See if we have any indirect syms to consider. */
1568 if (indirect_syms == NULL)
1569 return;
1570
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. */
1574
1575 switch (ms->flags & BFD_MACH_O_SECTION_TYPE_MASK)
1576 {
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:
1582 {
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);
1590
1591 for (isym = indirect_syms; isym != NULL; isym = isym->next)
1592 {
1593 if (isym->sect == sec)
1594 {
1595 nactual++;
1596 if (list == NULL)
1597 list = isym;
1598 else
1599 list_tail->next = isym;
1600 list_tail = isym;
1601 }
1602 }
1603
1604 /* If none are in this section, stop here. */
1605 if (nactual == 0)
1606 break;
1607
1608 /* If we somehow added indirect symbols to a section with a zero
1609 entry size, we're dead ... */
1610 gas_assert (eltsiz != 0);
1611
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
1618 {
1619 unsigned n;
1620 bfd_mach_o_asymbol *sym;
1621 ms->indirect_syms =
1622 bfd_zalloc (abfd,
1623 nactual * sizeof (bfd_mach_o_asymbol *));
1624
1625 if (ms->indirect_syms == NULL)
1626 {
1627 as_fatal (_("internal error: failed to allocate %d indirect"
1628 "symbol pointers"), nactual);
1629 }
1630
1631 for (isym = list, n = 0; isym != NULL; isym = isym->next, n++)
1632 {
687be931 1633 sym = (bfd_mach_o_asymbol *)symbol_get_bfdsym (isym->sym);
50d10658
IS
1634 /* Array is init to NULL & NULL signals a local symbol
1635 If the section is lazy-bound, we need to keep the
687be931
IS
1636 reference to the symbol, since dyld can override.
1637
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)
50d10658
IS
1642 ;
1643 else
1644 {
50d10658
IS
1645 if (sym == NULL)
1646 ;
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)
1652 {
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;
1661 }
1662 }
1663 }
1664 }
1665 }
1666 break;
1667
1668 default:
1669 break;
1670 }
1671}
1672
1673/* The process of relocation could alter what's externally visible, thus we
1674 leave setting the indirect symbols until last. */
1675
1676void
1677obj_mach_o_frob_file_after_relocs (void)
1678{
1679 bfd_map_over_sections (stdoutput, obj_mach_o_set_indirect_symbols, (char *) 0);
1680}
1681
34dd18bc
IS
1682/* Reverse relocations order to make ld happy. */
1683
1684void
1685obj_mach_o_reorder_section_relocs (asection *sec, arelent **rels, unsigned int n)
1686{
1687 unsigned int i;
1688 unsigned int max = n / 2;
1689
1690 for (i = 0; i < max; i++)
1691 {
1692 arelent *r = rels[i];
1693 rels[i] = rels[n - i - 1];
1694 rels[n - i - 1] = r;
1695 }
1696 bfd_set_reloc (stdoutput, sec, rels, n);
1697}
1698
68588f95
IS
1699/* Support stabs for mach-o. */
1700
1701void
1702obj_mach_o_process_stab (int what, const char *string,
1703 int type, int other, int desc)
1704{
1705 symbolS *symbolP;
1706 bfd_mach_o_asymbol *s;
1707
1708 switch (what)
1709 {
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;
1715
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;
1722
1723 default:
1724 as_bad(_("unrecognized stab type '%c'"), (char)what);
1725 abort ();
1726 break;
1727 }
1728
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;
1734
1735 /* It's a debug symbol. */
1736 s->symbol.flags |= BSF_DEBUGGING;
1737}
This page took 0.266596 seconds and 4 git commands to generate.