Avoid compiler warnings in gdb_curses.h on MinGW.
[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
8cf6e084
IS
48/* Forward decls. */
49static segT obj_mach_o_segT_from_bfd_name (const char *, int);
bcf0aac6 50
a4551119
TG
51/* TODO: Implement "-dynamic"/"-static" command line options. */
52
53static int obj_mach_o_is_static;
54
bcf0aac6
IS
55/* TODO: Implement the "-n" command line option to suppress the initial
56 switch to the text segment. */
57static int obj_mach_o_start_with_text_section = 1;
58
a4551119
TG
59/* Allow for special re-ordering on output. */
60
bcf0aac6
IS
61static int obj_mach_o_seen_objc_section;
62
63/* Start-up: At present, just create the sections we want. */
64void
65mach_o_begin (void)
66{
67 /* Mach-O only defines the .text section by default, and even this can
68 be suppressed by a flag. In the latter event, the first code MUST
69 be a section definition. */
70 if (obj_mach_o_start_with_text_section)
71 {
72 text_section = obj_mach_o_segT_from_bfd_name (TEXT_SECTION_NAME, 1);
73 subseg_set (text_section, 0);
74 if (obj_mach_o_is_static)
75 {
76 bfd_mach_o_section *mo_sec
77 = bfd_mach_o_get_mach_o_section (text_section);
78 mo_sec->flags &= ~BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS;
79 }
80 }
81}
e57f8c65 82
0c9ef0f0
TG
83/* Remember the subsections_by_symbols state in case we need to reset
84 the file flags. */
0c9ef0f0 85
b22161d6 86static int obj_mach_o_subsections_by_symbols;
e57f8c65 87
a4551119
TG
88/* This will put at most 16 characters (terminated by a ',' or newline) from
89 the input stream into dest. If there are more than 16 chars before the
90 delimiter, a warning is given and the string is truncated. On completion of
91 this function, input_line_pointer will point to the char after the ',' or
92 to the newline.
93
94 It trims leading and trailing space. */
95
96static int
97collect_16char_name (char *dest, const char *msg, int require_comma)
98{
99 char c, *namstart;
100
101 SKIP_WHITESPACE ();
102 namstart = input_line_pointer;
103
104 while ( (c = *input_line_pointer) != ','
105 && !is_end_of_line[(unsigned char) c])
106 input_line_pointer++;
107
108 {
109 int len = input_line_pointer - namstart; /* could be zero. */
110 /* lose any trailing space. */
111 while (len > 0 && namstart[len-1] == ' ')
112 len--;
113 if (len > 16)
114 {
115 *input_line_pointer = '\0'; /* make a temp string. */
116 as_bad (_("the %s name '%s' is too long (maximum 16 characters)"),
117 msg, namstart);
118 *input_line_pointer = c; /* restore for printing. */
119 len = 16;
120 }
121 if (len > 0)
122 memcpy (dest, namstart, len);
123 }
124
125 if (c != ',' && require_comma)
126 {
127 as_bad (_("expected a %s name followed by a `,'"), msg);
128 return 1;
129 }
130
131 return 0;
132}
133
8cf6e084
IS
134static int
135obj_mach_o_get_section_names (char *seg, char *sec,
136 unsigned segl, unsigned secl)
137{
138 /* Zero-length segment and section names are allowed. */
139 /* Parse segment name. */
140 memset (seg, 0, segl);
141 if (collect_16char_name (seg, "segment", 1))
142 {
143 ignore_rest_of_line ();
144 return 0;
145 }
146 input_line_pointer++; /* Skip the terminating ',' */
147
148 /* Parse section name, which can be empty. */
149 memset (sec, 0, secl);
150 collect_16char_name (sec, "section", 0);
151 return 1;
152}
153
154/* Build (or get) a section from the mach-o description - which includes
155 optional definitions for type, attributes, alignment and stub size.
156
157 BFD supplies default values for sections which have a canonical name. */
158
159#define SECT_TYPE_SPECIFIED 0x0001
160#define SECT_ATTR_SPECIFIED 0x0002
161#define SECT_ALGN_SPECIFIED 0x0004
162
163static segT
164obj_mach_o_make_or_get_sect (char * segname, char * sectname,
165 unsigned int specified_mask,
166 unsigned int usectype, unsigned int usecattr,
167 unsigned int ualign, offsetT stub_size)
168{
169 unsigned int sectype, secattr, secalign;
170 flagword oldflags, flags;
171 const char *name;
172 segT sec;
173 bfd_mach_o_section *msect;
174 const mach_o_section_name_xlat *xlat;
175
176 /* This provides default bfd flags and default mach-o section type and
177 attributes along with the canonical name. */
178 xlat = bfd_mach_o_section_data_for_mach_sect (stdoutput, segname, sectname);
179
180 /* TODO: more checking of whether overides are acually allowed. */
181
182 if (xlat != NULL)
183 {
184 name = xstrdup (xlat->bfd_name);
185 sectype = xlat->macho_sectype;
186 if (specified_mask & SECT_TYPE_SPECIFIED)
187 {
188 if ((sectype == BFD_MACH_O_S_ZEROFILL
189 || sectype == BFD_MACH_O_S_GB_ZEROFILL)
190 && sectype != usectype)
191 as_bad (_("cannot overide zerofill section type for `%s,%s'"),
192 segname, sectname);
193 else
194 sectype = usectype;
195 }
196 secattr = xlat->macho_secattr;
197 secalign = xlat->sectalign;
198 flags = xlat->bfd_flags;
199 }
200 else
201 {
202 /* There is no normal BFD section name for this section. Create one.
203 The name created doesn't really matter as it will never be written
204 on disk. */
205 size_t seglen = strlen (segname);
206 size_t sectlen = strlen (sectname);
207 char *n;
208
209 n = xmalloc (seglen + 1 + sectlen + 1);
210 memcpy (n, segname, seglen);
211 n[seglen] = '.';
212 memcpy (n + seglen + 1, sectname, sectlen);
213 n[seglen + 1 + sectlen] = 0;
214 name = n;
215 if (specified_mask & SECT_TYPE_SPECIFIED)
216 sectype = usectype;
217 else
218 sectype = BFD_MACH_O_S_REGULAR;
219 secattr = BFD_MACH_O_S_ATTR_NONE;
220 secalign = 0;
221 flags = SEC_NO_FLAGS;
222 }
223
224 /* For now, just use what the user provided. */
225
226 if (specified_mask & SECT_ATTR_SPECIFIED)
227 secattr = usecattr;
228
229 if (specified_mask & SECT_ALGN_SPECIFIED)
230 secalign = ualign;
231
232 /* Sub-segments don't exists as is on Mach-O. */
233 sec = subseg_new (name, 0);
234
235 oldflags = bfd_get_section_flags (stdoutput, sec);
236 msect = bfd_mach_o_get_mach_o_section (sec);
237
238 if (oldflags == SEC_NO_FLAGS)
239 {
240 /* New, so just use the defaults or what's specified. */
241 if (! bfd_set_section_flags (stdoutput, sec, flags))
242 as_warn (_("failed to set flags for \"%s\": %s"),
243 bfd_section_name (stdoutput, sec),
244 bfd_errmsg (bfd_get_error ()));
245
246 strncpy (msect->segname, segname, sizeof (msect->segname));
247 strncpy (msect->sectname, sectname, sizeof (msect->sectname));
248
249 msect->align = secalign;
250 msect->flags = sectype | secattr;
251 msect->reserved2 = stub_size;
252
253 if (sectype == BFD_MACH_O_S_ZEROFILL
254 || sectype == BFD_MACH_O_S_GB_ZEROFILL)
255 seg_info (sec)->bss = 1;
256 }
257 else if (flags != SEC_NO_FLAGS)
258 {
259 if (flags != oldflags
260 || msect->flags != (secattr | sectype))
261 as_warn (_("Ignoring changed section attributes for %s"), name);
262 }
263
264 return sec;
265}
266
a4551119
TG
267/* .section
268
269 The '.section' specification syntax looks like:
270 .section <segment> , <section> [, type [, attribs [, size]]]
271
272 White space is allowed everywhere between elements.
273
274 <segment> and <section> may be from 0 to 16 chars in length - they may
275 contain spaces but leading and trailing space will be trimmed. It is
276 mandatory that they be present (or that zero-length names are indicated
277 by ",,").
278
279 There is only a single section type for any entry.
280
281 There may be multiple attributes, they are delimited by `+'.
282
283 Not all section types and attributes are accepted by the Darwin system
284 assemblers as user-specifiable - although, at present, we do here. */
74a6005f
TG
285
286static void
287obj_mach_o_section (int ignore ATTRIBUTE_UNUSED)
288{
a4551119 289 unsigned int sectype = BFD_MACH_O_S_REGULAR;
8cf6e084 290 unsigned int specified_mask = 0;
74a6005f
TG
291 unsigned int secattr = 0;
292 offsetT sizeof_stub = 0;
8cf6e084 293 segT new_seg;
a4551119
TG
294 char segname[17];
295 char sectname[17];
74a6005f 296
ab76eeaf
IS
297#ifdef md_flush_pending_output
298 md_flush_pending_output ();
299#endif
300
8cf6e084
IS
301 /* Get the User's segment annd section names. */
302 if (! obj_mach_o_get_section_names (segname, sectname, 17, 17))
303 return;
74a6005f 304
8cf6e084 305 /* Parse section type, if present. */
74a6005f
TG
306 if (*input_line_pointer == ',')
307 {
8cf6e084
IS
308 char *p;
309 char c;
a4551119
TG
310 char tmpc;
311 int len;
74a6005f 312 input_line_pointer++;
a4551119 313 SKIP_WHITESPACE ();
74a6005f 314 p = input_line_pointer;
a4551119
TG
315 while ((c = *input_line_pointer) != ','
316 && !is_end_of_line[(unsigned char) c])
317 input_line_pointer++;
318
319 len = input_line_pointer - p;
320 /* strip trailing spaces. */
321 while (len > 0 && p[len-1] == ' ')
322 len--;
323 tmpc = p[len];
324
325 /* Temporarily make a string from the token. */
326 p[len] = 0;
ab76eeaf 327 sectype = bfd_mach_o_get_section_type_from_name (stdoutput, p);
a4551119 328 if (sectype > 255) /* Max Section ID == 255. */
74a6005f
TG
329 {
330 as_bad (_("unknown or invalid section type '%s'"), p);
ab76eeaf
IS
331 p[len] = tmpc;
332 ignore_rest_of_line ();
333 return;
74a6005f 334 }
a4551119 335 else
8cf6e084 336 specified_mask |= SECT_TYPE_SPECIFIED;
a4551119 337 /* Restore. */
ab76eeaf 338 p[len] = tmpc;
a4551119
TG
339
340 /* Parse attributes.
341 TODO: check validity of attributes for section type. */
8cf6e084
IS
342 if ((specified_mask & SECT_TYPE_SPECIFIED)
343 && c == ',')
74a6005f
TG
344 {
345 do
346 {
347 int attr;
348
a4551119 349 /* Skip initial `,' and subsequent `+'. */
74a6005f 350 input_line_pointer++;
a4551119
TG
351 SKIP_WHITESPACE ();
352 p = input_line_pointer;
353 while ((c = *input_line_pointer) != '+'
354 && c != ','
355 && !is_end_of_line[(unsigned char) c])
356 input_line_pointer++;
357
358 len = input_line_pointer - p;
359 /* strip trailing spaces. */
360 while (len > 0 && p[len-1] == ' ')
361 len--;
362 tmpc = p[len];
363
364 /* Temporarily make a string from the token. */
365 p[len] ='\0';
74a6005f 366 attr = bfd_mach_o_get_section_attribute_from_name (p);
a4551119 367 if (attr == -1)
ab76eeaf
IS
368 {
369 as_bad (_("unknown or invalid section attribute '%s'"), p);
370 p[len] = tmpc;
371 ignore_rest_of_line ();
372 return;
373 }
74a6005f 374 else
a4551119 375 {
8cf6e084 376 specified_mask |= SECT_ATTR_SPECIFIED;
a4551119
TG
377 secattr |= attr;
378 }
379 /* Restore. */
380 p[len] = tmpc;
74a6005f
TG
381 }
382 while (*input_line_pointer == '+');
383
384 /* Parse sizeof_stub. */
8cf6e084
IS
385 if ((specified_mask & SECT_ATTR_SPECIFIED)
386 && *input_line_pointer == ',')
74a6005f
TG
387 {
388 if (sectype != BFD_MACH_O_S_SYMBOL_STUBS)
ab76eeaf
IS
389 {
390 as_bad (_("unexpected section size information"));
391 ignore_rest_of_line ();
392 return;
393 }
74a6005f 394
a4551119 395 input_line_pointer++;
74a6005f
TG
396 sizeof_stub = get_absolute_expression ();
397 }
8cf6e084
IS
398 else if ((specified_mask & SECT_ATTR_SPECIFIED)
399 && sectype == BFD_MACH_O_S_SYMBOL_STUBS)
ab76eeaf
IS
400 {
401 as_bad (_("missing sizeof_stub expression"));
402 ignore_rest_of_line ();
403 return;
404 }
74a6005f
TG
405 }
406 }
74a6005f 407
8cf6e084
IS
408 new_seg = obj_mach_o_make_or_get_sect (segname, sectname, specified_mask,
409 sectype, secattr, 0 /*align */,
410 sizeof_stub);
411 if (new_seg != NULL)
a4551119 412 {
8cf6e084
IS
413 subseg_set (new_seg, 0);
414 demand_empty_rest_of_line ();
a4551119 415 }
8cf6e084 416}
74a6005f 417
8cf6e084 418/* .zerofill segname, sectname [, symbolname, size [, align]]
74a6005f 419
8cf6e084 420 Zerofill switches, temporarily, to a sect of type 'zerofill'.
74a6005f 421
8cf6e084
IS
422 If a variable name is given, it defines that in the section.
423 Otherwise it just creates the section if it doesn't exist. */
424
425static void
426obj_mach_o_zerofill (int ignore ATTRIBUTE_UNUSED)
427{
428 char segname[17];
429 char sectname[17];
430 segT old_seg = now_seg;
431 segT new_seg;
432 symbolS *sym = NULL;
433 unsigned int align = 0;
434 unsigned int specified_mask = 0;
435 offsetT size;
436
437#ifdef md_flush_pending_output
438 md_flush_pending_output ();
439#endif
440
441 /* Get the User's segment annd section names. */
442 if (! obj_mach_o_get_section_names (segname, sectname, 17, 17))
443 return;
444
445 /* Parse variable definition, if present. */
446 if (*input_line_pointer == ',')
74a6005f 447 {
8cf6e084
IS
448 /* Parse symbol, size [.align]
449 We follow the method of s_common_internal, with the difference
450 that the symbol cannot be a duplicate-common. */
451 char *name;
452 char c;
453 char *p;
454 expressionS exp;
455
456 input_line_pointer++; /* Skip ',' */
457 SKIP_WHITESPACE ();
458 name = input_line_pointer;
459 c = get_symbol_end ();
460 /* Just after name is now '\0'. */
461 p = input_line_pointer;
462 *p = c;
463
464 if (name == p)
a4551119 465 {
8cf6e084
IS
466 as_bad (_("expected symbol name"));
467 ignore_rest_of_line ();
468 goto done;
469 }
470
471 SKIP_WHITESPACE ();
472 if (*input_line_pointer == ',')
473 input_line_pointer++;
474
475 expression_and_evaluate (&exp);
476 if (exp.X_op != O_constant
477 && exp.X_op != O_absent)
478 {
479 as_bad (_("bad or irreducible absolute expression"));
480 ignore_rest_of_line ();
481 goto done;
482 }
483 else if (exp.X_op == O_absent)
484 {
485 as_bad (_("missing size expression"));
486 ignore_rest_of_line ();
487 goto done;
488 }
489
490 size = exp.X_add_number;
491 size &= ((offsetT) 2 << (stdoutput->arch_info->bits_per_address - 1)) - 1;
492 if (exp.X_add_number != size || !exp.X_unsigned)
493 {
494 as_warn (_("size (%ld) out of range, ignored"),
495 (long) exp.X_add_number);
496 ignore_rest_of_line ();
497 goto done;
498 }
499
500 *p = 0; /* Make the name into a c string for err messages. */
501 sym = symbol_find_or_make (name);
502 if (S_IS_DEFINED (sym) || symbol_equated_p (sym))
503 {
504 as_bad (_("symbol `%s' is already defined"), name);
505 *p = c;
506 ignore_rest_of_line ();
507 goto done;
508 }
509
510 size = S_GET_VALUE (sym);
511 if (size == 0)
512 size = exp.X_add_number;
513 else if (size != exp.X_add_number)
514 as_warn (_("size of \"%s\" is already %ld; not changing to %ld"),
515 name, (long) size, (long) exp.X_add_number);
516
517 *p = c; /* Restore the termination char. */
518
519 SKIP_WHITESPACE ();
520 if (*input_line_pointer == ',')
521 {
522 align = (unsigned int) parse_align (0);
523 if (align == (unsigned int) -1)
524 {
525 as_warn (_("align value not recognized, using size"));
526 align = size;
527 }
528 if (align > 15)
529 {
530 as_warn (_("Alignment (%lu) too large: 15 assumed."),
531 (unsigned long)align);
532 align = 15;
533 }
534 specified_mask |= SECT_ALGN_SPECIFIED;
a4551119 535 }
74a6005f 536 }
8cf6e084
IS
537 /* else just a section definition. */
538
539 specified_mask |= SECT_TYPE_SPECIFIED;
540 new_seg = obj_mach_o_make_or_get_sect (segname, sectname, specified_mask,
541 BFD_MACH_O_S_ZEROFILL,
542 BFD_MACH_O_S_ATTR_NONE,
543 align, (offsetT) 0 /*stub size*/);
544 if (new_seg == NULL)
545 return;
546
547 /* In case the user specifies the bss section by mach-o name.
548 Create it on demand */
549 if (strcmp (new_seg->name, BSS_SECTION_NAME) == 0
550 && bss_section == NULL)
551 bss_section = new_seg;
552
553 subseg_set (new_seg, 0);
554
555 if (sym != NULL)
74a6005f 556 {
8cf6e084
IS
557 char *pfrag;
558
559 if (align)
560 {
561 record_alignment (new_seg, align);
562 frag_align (align, 0, 0);
563 }
564
565 /* Detach from old frag. */
566 if (S_GET_SEGMENT (sym) == new_seg)
567 symbol_get_frag (sym)->fr_symbol = NULL;
568
569 symbol_set_frag (sym, frag_now);
570 pfrag = frag_var (rs_org, 1, 1, 0, sym, size, NULL);
571 *pfrag = 0;
572
573 S_SET_SEGMENT (sym, new_seg);
574 if (new_seg == bss_section)
575 S_CLEAR_EXTERNAL (sym);
74a6005f 576 }
8cf6e084
IS
577
578done:
579 /* switch back to the section that was current before the .zerofill. */
580 subseg_set (old_seg, 0);
74a6005f
TG
581}
582
a4551119
TG
583static segT
584obj_mach_o_segT_from_bfd_name (const char *nam, int must_succeed)
74a6005f 585{
a4551119
TG
586 const mach_o_section_name_xlat *xlat;
587 const char *segn;
588 segT sec;
589
590 /* BFD has tables of flags and default attributes for all the sections that
591 have a 'canonical' name. */
592 xlat = bfd_mach_o_section_data_for_bfd_name (stdoutput, nam, &segn);
593 if (xlat == NULL)
594 {
595 if (must_succeed)
596 as_fatal (_("BFD is out of sync with GAS, "
597 "unhandled well-known section type `%s'"), nam);
598 return NULL;
599 }
600
601 sec = bfd_get_section_by_name (stdoutput, nam);
602 if (sec == NULL)
603 {
604 bfd_mach_o_section *msect;
605
606 sec = subseg_force_new (xlat->bfd_name, 0);
607
608 /* Set default type, attributes and alignment. */
609 msect = bfd_mach_o_get_mach_o_section (sec);
610 msect->flags = xlat->macho_sectype | xlat->macho_secattr;
611 msect->align = xlat->sectalign;
612
613 if ((msect->flags & BFD_MACH_O_SECTION_TYPE_MASK)
614 == BFD_MACH_O_S_ZEROFILL)
615 seg_info (sec)->bss = 1;
616 }
617
618 return sec;
619}
620
621static const char * const known_sections[] =
622{
623 /* 0 */ NULL,
624 /* __TEXT */
625 /* 1 */ ".const",
626 /* 2 */ ".static_const",
627 /* 3 */ ".cstring",
628 /* 4 */ ".literal4",
629 /* 5 */ ".literal8",
630 /* 6 */ ".literal16",
631 /* 7 */ ".constructor",
632 /* 8 */ ".destructor",
633 /* 9 */ ".eh_frame",
634 /* __DATA */
635 /* 10 */ ".const_data",
636 /* 11 */ ".static_data",
637 /* 12 */ ".mod_init_func",
638 /* 13 */ ".mod_term_func",
639 /* 14 */ ".dyld",
640 /* 15 */ ".cfstring"
74a6005f
TG
641};
642
a4551119 643/* Interface for a known non-optional section directive. */
74a6005f
TG
644
645static void
646obj_mach_o_known_section (int sect_index)
647{
a4551119
TG
648 segT section;
649
650#ifdef md_flush_pending_output
651 md_flush_pending_output ();
652#endif
653
654 section = obj_mach_o_segT_from_bfd_name (known_sections[sect_index], 1);
655 if (section != NULL)
656 subseg_set (section, 0);
657
658 /* else, we leave the section as it was; there was a fatal error anyway. */
659}
660
661static const char * const objc_sections[] =
662{
663 /* 0 */ NULL,
664 /* 1 */ ".objc_class",
665 /* 2 */ ".objc_meta_class",
666 /* 3 */ ".objc_cat_cls_meth",
667 /* 4 */ ".objc_cat_inst_meth",
668 /* 5 */ ".objc_protocol",
669 /* 6 */ ".objc_string_object",
670 /* 7 */ ".objc_cls_meth",
671 /* 8 */ ".objc_inst_meth",
672 /* 9 */ ".objc_cls_refs",
673 /* 10 */ ".objc_message_refs",
674 /* 11 */ ".objc_symbols",
675 /* 12 */ ".objc_category",
676 /* 13 */ ".objc_class_vars",
677 /* 14 */ ".objc_instance_vars",
678 /* 15 */ ".objc_module_info",
679 /* 16 */ ".cstring", /* objc_class_names Alias for .cstring */
680 /* 17 */ ".cstring", /* Alias objc_meth_var_types for .cstring */
681 /* 18 */ ".cstring", /* objc_meth_var_names Alias for .cstring */
682 /* 19 */ ".objc_selector_strs",
683 /* 20 */ ".objc_image_info", /* extension. */
684 /* 21 */ ".objc_selector_fixup", /* extension. */
685 /* 22 */ ".objc1_class_ext", /* ObjC-1 extension. */
686 /* 23 */ ".objc1_property_list", /* ObjC-1 extension. */
687 /* 24 */ ".objc1_protocol_ext" /* ObjC-1 extension. */
688};
689
690/* This currently does the same as known_sections, but kept separate for
691 ease of maintenance. */
692
693static void
694obj_mach_o_objc_section (int sect_index)
695{
696 segT section;
697
698#ifdef md_flush_pending_output
699 md_flush_pending_output ();
700#endif
701
702 section = obj_mach_o_segT_from_bfd_name (objc_sections[sect_index], 1);
703 if (section != NULL)
704 {
bcf0aac6
IS
705 obj_mach_o_seen_objc_section = 1; /* We need to ensure that certain
706 sections are present and in the
707 right order. */
a4551119
TG
708 subseg_set (section, 0);
709 }
710
711 /* else, we leave the section as it was; there was a fatal error anyway. */
712}
713
714/* Debug section directives. */
715
716static const char * const debug_sections[] =
717{
718 /* 0 */ NULL,
719 /* __DWARF */
720 /* 1 */ ".debug_frame",
721 /* 2 */ ".debug_info",
722 /* 3 */ ".debug_abbrev",
723 /* 4 */ ".debug_aranges",
724 /* 5 */ ".debug_macinfo",
725 /* 6 */ ".debug_line",
726 /* 7 */ ".debug_loc",
727 /* 8 */ ".debug_pubnames",
728 /* 9 */ ".debug_pubtypes",
729 /* 10 */ ".debug_str",
730 /* 11 */ ".debug_ranges",
731 /* 12 */ ".debug_macro"
732};
733
734/* ??? Maybe these should be conditional on gdwarf-*.
735 It`s also likely that we will need to be able to set them from the cfi
736 code. */
737
738static void
739obj_mach_o_debug_section (int sect_index)
740{
741 segT section;
742
743#ifdef md_flush_pending_output
744 md_flush_pending_output ();
745#endif
746
747 section = obj_mach_o_segT_from_bfd_name (debug_sections[sect_index], 1);
748 if (section != NULL)
749 subseg_set (section, 0);
750
751 /* else, we leave the section as it was; there was a fatal error anyway. */
752}
753
754/* This could be moved to the tc-xx files, but there is so little dependency
755 there, that the code might as well be shared. */
756
757struct opt_tgt_sect
758{
759 const char *name;
760 unsigned x86_val;
761 unsigned ppc_val;
762};
763
764/* The extensions here are for specific sections that are generated by GCC
765 and Darwin system tools, but don't have directives in the `system as'. */
766
767static const struct opt_tgt_sect tgt_sections[] =
768{
769 /* 0 */ { NULL, 0, 0},
770 /* 1 */ { ".lazy_symbol_pointer", 0, 0},
771 /* 2 */ { ".lazy_symbol_pointer2", 0, 0}, /* X86 - extension */
772 /* 3 */ { ".lazy_symbol_pointer3", 0, 0}, /* X86 - extension */
773 /* 4 */ { ".non_lazy_symbol_pointer", 0, 0},
774 /* 5 */ { ".non_lazy_symbol_pointer_x86", 0, 0}, /* X86 - extension */
775 /* 6 */ { ".symbol_stub", 16, 20},
776 /* 7 */ { ".symbol_stub1", 0, 16}, /* PPC - extension */
777 /* 8 */ { ".picsymbol_stub", 26, 36},
778 /* 9 */ { ".picsymbol_stub1", 0, 32}, /* PPC - extension */
779 /* 10 */ { ".picsymbol_stub2", 25, 0}, /* X86 - extension */
780 /* 11 */ { ".picsymbol_stub3", 5, 0}, /* X86 - extension */
781};
782
783/* Interface for an optional section directive. */
784
785static void
786obj_mach_o_opt_tgt_section (int sect_index)
787{
788 const struct opt_tgt_sect *tgtsct = &tgt_sections[sect_index];
789 segT section;
74a6005f
TG
790
791#ifdef md_flush_pending_output
792 md_flush_pending_output ();
793#endif
794
a4551119
TG
795 section = obj_mach_o_segT_from_bfd_name (tgtsct->name, 0);
796 if (section == NULL)
74a6005f 797 {
a4551119
TG
798 as_bad (_("%s is not used for the selected target"), tgtsct->name);
799 /* Leave the section as it is. */
74a6005f
TG
800 }
801 else
802 {
a4551119
TG
803 bfd_mach_o_section *mo_sec = bfd_mach_o_get_mach_o_section (section);
804 subseg_set (section, 0);
805#if defined (TC_I386)
806 mo_sec->reserved2 = tgtsct->x86_val;
807#elif defined (TC_PPC)
808 mo_sec->reserved2 = tgtsct->ppc_val;
809#else
810 mo_sec->reserved2 = 0;
811#endif
812 }
813}
74a6005f 814
a4551119
TG
815/* We don't necessarily have the three 'base' sections on mach-o.
816 Normally, we would start up with only the 'text' section defined.
817 However, even that can be suppressed with (TODO) c/l option "-n".
818 Thus, we have to be able to create all three sections on-demand. */
74a6005f 819
a4551119
TG
820static void
821obj_mach_o_base_section (int sect_index)
822{
823 segT section;
824
825#ifdef md_flush_pending_output
826 md_flush_pending_output ();
827#endif
828
829 /* We don't support numeric (or any other) qualifications on the
830 well-known section shorthands. */
831 demand_empty_rest_of_line ();
832
833 switch (sect_index)
834 {
835 /* Handle the three sections that are globally known within GAS.
836 For Mach-O, these are created on demand rather than at startup. */
837 case 1:
838 if (text_section == NULL)
839 text_section = obj_mach_o_segT_from_bfd_name (TEXT_SECTION_NAME, 1);
840 if (obj_mach_o_is_static)
841 {
842 bfd_mach_o_section *mo_sec
843 = bfd_mach_o_get_mach_o_section (text_section);
844 mo_sec->flags &= ~BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS;
845 }
846 section = text_section;
847 break;
848 case 2:
849 if (data_section == NULL)
850 data_section = obj_mach_o_segT_from_bfd_name (DATA_SECTION_NAME, 1);
851 section = data_section;
852 break;
853 case 3:
854 /* ??? maybe this achieves very little, as an addition. */
855 if (bss_section == NULL)
856 {
857 bss_section = obj_mach_o_segT_from_bfd_name (BSS_SECTION_NAME, 1);
858 seg_info (bss_section)->bss = 1;
859 }
860 section = bss_section;
861 break;
862 default:
863 as_fatal (_("internal error: base section index out of range"));
864 return;
865 break;
74a6005f 866 }
a4551119 867 subseg_set (section, 0);
74a6005f
TG
868}
869
a4551119
TG
870/* This finishes off parsing a .comm or .lcomm statement, which both can have
871 an (optional) alignment field. It also allows us to create the bss section
872 on demand. */
74a6005f
TG
873
874static symbolS *
a4551119
TG
875obj_mach_o_common_parse (int is_local, symbolS *symbolP,
876 addressT size)
74a6005f
TG
877{
878 addressT align = 0;
b22161d6 879 bfd_mach_o_asymbol *s;
74a6005f 880
8cf6e084
IS
881 SKIP_WHITESPACE ();
882
a4551119
TG
883 /* Both comm and lcomm take an optional alignment, as a power
884 of two between 1 and 15. */
74a6005f
TG
885 if (*input_line_pointer == ',')
886 {
a4551119 887 /* We expect a power of 2. */
74a6005f
TG
888 align = parse_align (0);
889 if (align == (addressT) -1)
890 return NULL;
a4551119
TG
891 if (align > 15)
892 {
893 as_warn (_("Alignment (%lu) too large: 15 assumed."),
894 (unsigned long)align);
895 align = 15;
896 }
74a6005f
TG
897 }
898
b22161d6 899 s = (bfd_mach_o_asymbol *) symbol_get_bfdsym (symbolP);
a4551119
TG
900 if (is_local)
901 {
902 /* Create the BSS section on demand. */
903 if (bss_section == NULL)
904 {
905 bss_section = obj_mach_o_segT_from_bfd_name (BSS_SECTION_NAME, 1);
906 seg_info (bss_section)->bss = 1;
907 }
908 bss_alloc (symbolP, size, align);
b22161d6 909 s->n_type = BFD_MACH_O_N_SECT;
a4551119
TG
910 S_CLEAR_EXTERNAL (symbolP);
911 }
912 else
913 {
914 S_SET_VALUE (symbolP, size);
915 S_SET_ALIGN (symbolP, align);
916 S_SET_EXTERNAL (symbolP);
917 S_SET_SEGMENT (symbolP, bfd_com_section_ptr);
b22161d6 918 s->n_type = BFD_MACH_O_N_UNDF | BFD_MACH_O_N_EXT;
a4551119 919 }
74a6005f 920
b22161d6
IS
921 /* This is a data object (whatever we choose that to mean). */
922 s->symbol.flags |= BSF_OBJECT;
923
924 /* We've set symbol qualifiers, so validate if you can. */
925 s->symbol.udata.i = SYM_MACHO_FIELDS_NOT_VALIDATED;
74a6005f
TG
926
927 return symbolP;
928}
929
930static void
a4551119 931obj_mach_o_comm (int is_local)
74a6005f 932{
a4551119 933 s_comm_internal (is_local, obj_mach_o_common_parse);
74a6005f
TG
934}
935
0c9ef0f0
TG
936/* Set properties that apply to the whole file. At present, the only
937 one defined, is subsections_via_symbols. */
938
939typedef enum obj_mach_o_file_properties {
940 OBJ_MACH_O_FILE_PROP_NONE = 0,
941 OBJ_MACH_O_FILE_PROP_SUBSECTS_VIA_SYMS,
942 OBJ_MACH_O_FILE_PROP_MAX
943} obj_mach_o_file_properties;
944
945static void
946obj_mach_o_fileprop (int prop)
74a6005f 947{
0c9ef0f0
TG
948 if (prop < 0 || prop >= OBJ_MACH_O_FILE_PROP_MAX)
949 as_fatal (_("internal error: bad file property ID %d"), prop);
950
951 switch ((obj_mach_o_file_properties) prop)
952 {
953 case OBJ_MACH_O_FILE_PROP_SUBSECTS_VIA_SYMS:
499963bc 954 obj_mach_o_subsections_by_symbols = 1;
0c9ef0f0
TG
955 if (!bfd_set_private_flags (stdoutput,
956 BFD_MACH_O_MH_SUBSECTIONS_VIA_SYMBOLS))
957 as_bad (_("failed to set subsections by symbols"));
958 demand_empty_rest_of_line ();
959 break;
960 default:
961 break;
962 }
74a6005f
TG
963}
964
b22161d6
IS
965/* Temporary markers for symbol reference data.
966 Lazy will remain in place. */
967#define LAZY 0x01
968#define REFE 0x02
969
970/* We have a bunch of qualifiers that may be applied to symbols.
971 .globl is handled here so that we might make sure that conflicting qualifiers
972 are caught where possible. */
973
974typedef enum obj_mach_o_symbol_type {
975 OBJ_MACH_O_SYM_UNK = 0,
976 OBJ_MACH_O_SYM_LOCAL = 1,
977 OBJ_MACH_O_SYM_GLOBL = 2,
978 OBJ_MACH_O_SYM_REFERENCE = 3,
979 OBJ_MACH_O_SYM_WEAK_REF = 4,
980 OBJ_MACH_O_SYM_LAZY_REF = 5,
981 OBJ_MACH_O_SYM_WEAK_DEF = 6,
982 OBJ_MACH_O_SYM_PRIV_EXT = 7,
983 OBJ_MACH_O_SYM_NO_DEAD_STRIP = 8,
984 OBJ_MACH_O_SYM_WEAK = 9
985} obj_mach_o_symbol_type;
986
987/* Set Mach-O-specific symbol qualifiers. */
988
989static int
990obj_mach_o_set_symbol_qualifier (symbolS *sym, int type)
991{
992 int is_defined;
993 bfd_mach_o_asymbol *s = (bfd_mach_o_asymbol *) symbol_get_bfdsym (sym);
994 bfd_mach_o_section *sec;
995 int sectype = -1;
996 int err = 0;
997
998 /* If the symbol is defined, then we can do more rigorous checking on
999 the validity of the qualifiers. Otherwise, we are stuck with waiting
1000 until it's defined - or until write the file.
1001
1002 In certain cases (e.g. when a symbol qualifier is intended to introduce
1003 an undefined symbol in a stubs section) we should check that the current
1004 section is appropriate to the qualifier. */
1005
1006 is_defined = s->symbol.section != bfd_und_section_ptr;
1007 if (is_defined)
1008 sec = bfd_mach_o_get_mach_o_section (s->symbol.section) ;
1009 else
1010 sec = bfd_mach_o_get_mach_o_section (now_seg) ;
1011
1012 if (sec != NULL)
1013 sectype = sec->flags & BFD_MACH_O_SECTION_TYPE_MASK;
1014
1015 switch ((obj_mach_o_symbol_type) type)
1016 {
1017 case OBJ_MACH_O_SYM_LOCAL:
1018 /* This is an extension over the system tools. */
1019 if (s->n_type & (BFD_MACH_O_N_PEXT | BFD_MACH_O_N_EXT))
1020 {
1021 as_bad (_("'%s' previously declared as '%s'."), s->symbol.name,
1022 (s->n_type & BFD_MACH_O_N_PEXT) ? "private extern"
1023 : "global" );
1024 err = 1;
1025 }
1026 else
1027 {
1028 s->n_type &= ~BFD_MACH_O_N_EXT;
1029 S_CLEAR_EXTERNAL (sym);
1030 }
1031 break;
1032
1033 case OBJ_MACH_O_SYM_PRIV_EXT:
1034 s->n_type |= BFD_MACH_O_N_PEXT ;
50d10658 1035 s->n_desc &= ~LAZY; /* The native tool switches this off too. */
b22161d6
IS
1036 /* We follow the system tools in marking PEXT as also global. */
1037 /* Fall through. */
1038
1039 case OBJ_MACH_O_SYM_GLOBL:
1040 /* It's not an error to define a symbol and then make it global. */
1041 s->n_type |= BFD_MACH_O_N_EXT;
1042 S_SET_EXTERNAL (sym);
1043 break;
1044
1045 case OBJ_MACH_O_SYM_REFERENCE:
1046 if (is_defined)
1047 s->n_desc |= BFD_MACH_O_N_NO_DEAD_STRIP;
1048 else
1049 s->n_desc |= (REFE | BFD_MACH_O_N_NO_DEAD_STRIP);
1050 break;
1051
1052 case OBJ_MACH_O_SYM_LAZY_REF:
1053 if (is_defined)
1054 s->n_desc |= BFD_MACH_O_N_NO_DEAD_STRIP;
1055 else
1056 s->n_desc |= (REFE | LAZY | BFD_MACH_O_N_NO_DEAD_STRIP);
1057 break;
1058
1059 /* Force ld to retain the symbol - even if it appears unused. */
1060 case OBJ_MACH_O_SYM_NO_DEAD_STRIP:
1061 s->n_desc |= BFD_MACH_O_N_NO_DEAD_STRIP ;
1062 break;
1063
1064 /* Mach-O's idea of weak ... */
1065 case OBJ_MACH_O_SYM_WEAK_REF:
1066 s->n_desc |= BFD_MACH_O_N_WEAK_REF ;
1067 break;
1068
1069 case OBJ_MACH_O_SYM_WEAK_DEF:
1070 if (is_defined && sectype != BFD_MACH_O_S_COALESCED)
1071 {
1072 as_bad (_("'%s' can't be a weak_definition (currently only"
1073 " supported in sections of type coalesced)"),
1074 s->symbol.name);
1075 err = 1;
1076 }
1077 else
1078 s->n_desc |= BFD_MACH_O_N_WEAK_DEF;
1079 break;
1080
1081 case OBJ_MACH_O_SYM_WEAK:
1082 /* A generic 'weak' - we try to figure out what it means at
1083 symbol frob time. */
1084 S_SET_WEAK (sym);
1085 break;
1086
1087 default:
1088 break;
1089 }
1090
1091 /* We've seen some kind of qualifier - check validity if or when the entity
1092 is defined. */
1093 s->symbol.udata.i = SYM_MACHO_FIELDS_NOT_VALIDATED;
1094 return err;
1095}
1096
1097/* Respond to symbol qualifiers.
1098 All of the form:
1099 .<qualifier> symbol [, symbol]*
1100 a list of symbols is an extension over the Darwin system as. */
1101
1102static void
1103obj_mach_o_sym_qual (int ntype)
1104{
1105 char *name;
1106 char c;
1107 symbolS *symbolP;
1108
1109#ifdef md_flush_pending_output
1110 md_flush_pending_output ();
1111#endif
1112
1113 do
1114 {
1115 name = input_line_pointer;
1116 c = get_symbol_end ();
1117 symbolP = symbol_find_or_make (name);
1118 obj_mach_o_set_symbol_qualifier (symbolP, ntype);
1119 *input_line_pointer = c;
1120 SKIP_WHITESPACE ();
1121 c = *input_line_pointer;
1122 if (c == ',')
1123 {
1124 input_line_pointer++;
1125 SKIP_WHITESPACE ();
1126 if (is_end_of_line[(unsigned char) *input_line_pointer])
1127 c = '\n';
1128 }
1129 }
1130 while (c == ',');
1131
1132 demand_empty_rest_of_line ();
1133}
1134
50d10658
IS
1135typedef struct obj_mach_o_indirect_sym
1136{
1137 symbolS *sym;
1138 segT sect;
1139 struct obj_mach_o_indirect_sym *next;
1140} obj_mach_o_indirect_sym;
1141
1142/* We store in order an maintain a pointer to the last one - to save reversing
1143 later. */
1144obj_mach_o_indirect_sym *indirect_syms;
1145obj_mach_o_indirect_sym *indirect_syms_tail;
a4551119
TG
1146
1147static void
50d10658 1148obj_mach_o_indirect_symbol (int arg ATTRIBUTE_UNUSED)
a4551119 1149{
50d10658
IS
1150 bfd_mach_o_section *sec = bfd_mach_o_get_mach_o_section (now_seg);
1151
1152#ifdef md_flush_pending_output
1153 md_flush_pending_output ();
1154#endif
1155
1156 if (obj_mach_o_is_static)
1157 as_bad (_("use of .indirect_symbols requires `-dynamic'"));
1158
1159 switch (sec->flags & BFD_MACH_O_SECTION_TYPE_MASK)
1160 {
1161 case BFD_MACH_O_S_SYMBOL_STUBS:
1162 case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS:
1163 case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS:
1164 {
1165 obj_mach_o_indirect_sym *isym;
1166 char *name = input_line_pointer;
1167 char c = get_symbol_end ();
1168 symbolS *sym = symbol_find_or_make (name);
1169 unsigned int elsize =
1170 bfd_mach_o_section_get_entry_size (stdoutput, sec);
1171
1172 if (elsize == 0)
1173 {
1174 as_bad (_("attempt to add an indirect_symbol to a stub or"
1175 " reference section with a zero-sized element at %s"),
1176 name);
1177 *input_line_pointer = c;
1178 ignore_rest_of_line ();
1179 return;
1180 }
1181 *input_line_pointer = c;
1182
1183 isym = (obj_mach_o_indirect_sym *)
1184 xmalloc (sizeof (obj_mach_o_indirect_sym));
1185
1186 /* Just record the data for now, we will validate it when we
1187 compute the output in obj_mach_o_set_indirect_symbols. */
1188 isym->sym = sym;
1189 isym->sect = now_seg;
1190 isym->next = NULL;
1191 if (indirect_syms == NULL)
1192 indirect_syms = isym;
1193 else
1194 indirect_syms_tail->next = isym;
1195 indirect_syms_tail = isym;
1196 }
1197 break;
1198
1199 default:
1200 as_bad (_("an .indirect_symbol must be in a symbol pointer"
1201 " or stub section."));
1202 ignore_rest_of_line ();
1203 return;
1204 }
1205 demand_empty_rest_of_line ();
a4551119
TG
1206}
1207
e57f8c65
TG
1208const pseudo_typeS mach_o_pseudo_table[] =
1209{
a4551119 1210 /* Section directives. */
74a6005f 1211 { "comm", obj_mach_o_comm, 0 },
a4551119
TG
1212 { "lcomm", obj_mach_o_comm, 1 },
1213
1214 { "text", obj_mach_o_base_section, 1},
1215 { "data", obj_mach_o_base_section, 2},
1216 { "bss", obj_mach_o_base_section, 3}, /* extension */
1217
1218 { "const", obj_mach_o_known_section, 1},
1219 { "static_const", obj_mach_o_known_section, 2},
1220 { "cstring", obj_mach_o_known_section, 3},
1221 { "literal4", obj_mach_o_known_section, 4},
1222 { "literal8", obj_mach_o_known_section, 5},
1223 { "literal16", obj_mach_o_known_section, 6},
1224 { "constructor", obj_mach_o_known_section, 7},
1225 { "destructor", obj_mach_o_known_section, 8},
1226 { "eh_frame", obj_mach_o_known_section, 9},
1227
1228 { "const_data", obj_mach_o_known_section, 10},
1229 { "static_data", obj_mach_o_known_section, 11},
1230 { "mod_init_func", obj_mach_o_known_section, 12},
1231 { "mod_term_func", obj_mach_o_known_section, 13},
1232 { "dyld", obj_mach_o_known_section, 14},
1233 { "cfstring", obj_mach_o_known_section, 15},
1234
1235 { "objc_class", obj_mach_o_objc_section, 1},
1236 { "objc_meta_class", obj_mach_o_objc_section, 2},
1237 { "objc_cat_cls_meth", obj_mach_o_objc_section, 3},
1238 { "objc_cat_inst_meth", obj_mach_o_objc_section, 4},
1239 { "objc_protocol", obj_mach_o_objc_section, 5},
1240 { "objc_string_object", obj_mach_o_objc_section, 6},
1241 { "objc_cls_meth", obj_mach_o_objc_section, 7},
1242 { "objc_inst_meth", obj_mach_o_objc_section, 8},
1243 { "objc_cls_refs", obj_mach_o_objc_section, 9},
1244 { "objc_message_refs", obj_mach_o_objc_section, 10},
1245 { "objc_symbols", obj_mach_o_objc_section, 11},
1246 { "objc_category", obj_mach_o_objc_section, 12},
1247 { "objc_class_vars", obj_mach_o_objc_section, 13},
1248 { "objc_instance_vars", obj_mach_o_objc_section, 14},
1249 { "objc_module_info", obj_mach_o_objc_section, 15},
1250 { "objc_class_names", obj_mach_o_objc_section, 16}, /* Alias for .cstring */
1251 { "objc_meth_var_types", obj_mach_o_objc_section, 17}, /* Alias for .cstring */
1252 { "objc_meth_var_names", obj_mach_o_objc_section, 18}, /* Alias for .cstring */
1253 { "objc_selector_strs", obj_mach_o_objc_section, 19},
1254 { "objc_image_info", obj_mach_o_objc_section, 20}, /* extension. */
1255 { "objc_selector_fixup", obj_mach_o_objc_section, 21}, /* extension. */
1256 { "objc1_class_ext", obj_mach_o_objc_section, 22}, /* ObjC-1 extension. */
1257 { "objc1_property_list", obj_mach_o_objc_section, 23}, /* ObjC-1 extension. */
1258 { "objc1_protocol_ext", obj_mach_o_objc_section, 24}, /* ObjC-1 extension. */
1259
1260 { "debug_frame", obj_mach_o_debug_section, 1}, /* extension. */
1261 { "debug_info", obj_mach_o_debug_section, 2}, /* extension. */
1262 { "debug_abbrev", obj_mach_o_debug_section, 3}, /* extension. */
1263 { "debug_aranges", obj_mach_o_debug_section, 4}, /* extension. */
1264 { "debug_macinfo", obj_mach_o_debug_section, 5}, /* extension. */
1265 { "debug_line", obj_mach_o_debug_section, 6}, /* extension. */
1266 { "debug_loc", obj_mach_o_debug_section, 7}, /* extension. */
1267 { "debug_pubnames", obj_mach_o_debug_section, 8}, /* extension. */
1268 { "debug_pubtypes", obj_mach_o_debug_section, 9}, /* extension. */
1269 { "debug_str", obj_mach_o_debug_section, 10}, /* extension. */
1270 { "debug_ranges", obj_mach_o_debug_section, 11}, /* extension. */
1271 { "debug_macro", obj_mach_o_debug_section, 12}, /* extension. */
1272
1273 { "lazy_symbol_pointer", obj_mach_o_opt_tgt_section, 1},
1274 { "lazy_symbol_pointer2", obj_mach_o_opt_tgt_section, 2}, /* extension. */
1275 { "lazy_symbol_pointer3", obj_mach_o_opt_tgt_section, 3}, /* extension. */
1276 { "non_lazy_symbol_pointer", obj_mach_o_opt_tgt_section, 4},
1277 { "non_lazy_symbol_pointer_x86", obj_mach_o_opt_tgt_section, 5}, /* extension. */
1278 { "symbol_stub", obj_mach_o_opt_tgt_section, 6},
1279 { "symbol_stub1", obj_mach_o_opt_tgt_section, 7}, /* extension. */
1280 { "picsymbol_stub", obj_mach_o_opt_tgt_section, 8}, /* extension. */
1281 { "picsymbol_stub1", obj_mach_o_opt_tgt_section, 9}, /* extension. */
1282 { "picsymbol_stub2", obj_mach_o_opt_tgt_section, 4}, /* extension. */
1283 { "picsymbol_stub3", obj_mach_o_opt_tgt_section, 4}, /* extension. */
1284
1285 { "section", obj_mach_o_section, 0},
8cf6e084 1286 { "zerofill", obj_mach_o_zerofill, 0},
a4551119 1287
b22161d6
IS
1288 /* Symbol qualifiers. */
1289 {"local", obj_mach_o_sym_qual, OBJ_MACH_O_SYM_LOCAL},
1290 {"globl", obj_mach_o_sym_qual, OBJ_MACH_O_SYM_GLOBL},
1291 {"reference", obj_mach_o_sym_qual, OBJ_MACH_O_SYM_REFERENCE},
1292 {"weak_reference", obj_mach_o_sym_qual, OBJ_MACH_O_SYM_WEAK_REF},
1293 {"lazy_reference", obj_mach_o_sym_qual, OBJ_MACH_O_SYM_LAZY_REF},
1294 {"weak_definition", obj_mach_o_sym_qual, OBJ_MACH_O_SYM_WEAK_DEF},
1295 {"private_extern", obj_mach_o_sym_qual, OBJ_MACH_O_SYM_PRIV_EXT},
1296 {"no_dead_strip", obj_mach_o_sym_qual, OBJ_MACH_O_SYM_NO_DEAD_STRIP},
1297 {"weak", obj_mach_o_sym_qual, OBJ_MACH_O_SYM_WEAK}, /* ext */
1298
50d10658 1299 { "indirect_symbol", obj_mach_o_indirect_symbol, 0},
a4551119
TG
1300
1301 /* File flags. */
0c9ef0f0
TG
1302 { "subsections_via_symbols", obj_mach_o_fileprop,
1303 OBJ_MACH_O_FILE_PROP_SUBSECTS_VIA_SYMS},
e57f8c65
TG
1304
1305 {NULL, NULL, 0}
1306};
68588f95 1307
b22161d6
IS
1308/* Determine the default n_type value for a symbol from its section. */
1309
1310static unsigned
1311obj_mach_o_type_for_symbol (bfd_mach_o_asymbol *s)
1312{
1313 if (s->symbol.section == bfd_abs_section_ptr)
1314 return BFD_MACH_O_N_ABS;
1315 else if (s->symbol.section == bfd_com_section_ptr
1316 || s->symbol.section == bfd_und_section_ptr)
1317 return BFD_MACH_O_N_UNDF;
1318 else
1319 return BFD_MACH_O_N_SECT;
1320}
1321
1322/* We need to check the correspondence between some kinds of symbols and their
1323 sections. Common and BSS vars will seen via the obj_macho_comm() function.
1324
1325 The earlier we can pick up a problem, the better the diagnostics will be.
1326
1327 However, when symbol type information is attached, the symbol section will
1328 quite possibly be unknown. So we are stuck with checking (most of the)
1329 validity at the time the file is written (unfortunately, then one doesn't
1330 get line number information in the diagnostic). */
1331
1332/* Here we pick up the case where symbol qualifiers have been applied that
1333 are possibly incompatible with the section etc. that the symbol is defined
1334 in. */
1335
1336void obj_macho_frob_label (struct symbol *sp)
1337{
50d10658
IS
1338 bfd_mach_o_asymbol *s;
1339 unsigned base_type;
1340 bfd_mach_o_section *sec;
b22161d6
IS
1341 int sectype = -1;
1342
50d10658
IS
1343 /* Leave local symbols alone. */
1344
1345 if (S_IS_LOCAL (sp))
1346 return;
1347
1348 s = (bfd_mach_o_asymbol *) symbol_get_bfdsym (sp);
1349 /* Leave debug symbols alone. */
b22161d6 1350 if ((s->n_type & BFD_MACH_O_N_STAB) != 0)
50d10658
IS
1351 return;
1352
1353 /* This is the base symbol type, that we mask in. */
1354 base_type = obj_mach_o_type_for_symbol (s);
1355
1356 sec = bfd_mach_o_get_mach_o_section (s->symbol.section);
b22161d6
IS
1357 if (sec != NULL)
1358 sectype = sec->flags & BFD_MACH_O_SECTION_TYPE_MASK;
1359
1360 /* If there is a pre-existing qualifier, we can make some checks about
1361 validity now. */
1362
1363 if(s->symbol.udata.i == SYM_MACHO_FIELDS_NOT_VALIDATED)
1364 {
1365 if ((s->n_desc & BFD_MACH_O_N_WEAK_DEF)
1366 && sectype != BFD_MACH_O_S_COALESCED)
1367 as_bad (_("'%s' can't be a weak_definition (currently only supported"
1368 " in sections of type coalesced)"), s->symbol.name);
1369
1370 /* Have we changed from an undefined to defined ref? */
1371 s->n_desc &= ~(REFE | LAZY);
1372 }
1373
1374 s->n_type &= ~BFD_MACH_O_N_TYPE;
1375 s->n_type |= base_type;
1376}
1377
1378/* This is the fall-back, we come here when we get to the end of the file and
1379 the symbol is not defined - or there are combinations of qualifiers required
1380 (e.g. global + weak_def). */
1381
1382int
1383obj_macho_frob_symbol (struct symbol *sp)
1384{
50d10658
IS
1385 bfd_mach_o_asymbol *s;
1386 unsigned base_type;
1387 bfd_mach_o_section *sec;
b22161d6 1388 int sectype = -1;
50d10658
IS
1389
1390 /* Leave local symbols alone. */
1391 if (S_IS_LOCAL (sp))
1392 return 0;
1393
1394 s = (bfd_mach_o_asymbol *) symbol_get_bfdsym (sp);
1395 /* Leave debug symbols alone. */
1396 if ((s->n_type & BFD_MACH_O_N_STAB) != 0)
1397 return 0;
1398
1399 base_type = obj_mach_o_type_for_symbol (s);
1400 sec = bfd_mach_o_get_mach_o_section (s->symbol.section);
b22161d6
IS
1401 if (sec != NULL)
1402 sectype = sec->flags & BFD_MACH_O_SECTION_TYPE_MASK;
1403
50d10658 1404 if (s->symbol.section == bfd_und_section_ptr)
b22161d6
IS
1405 {
1406 /* ??? Do we really gain much from implementing this as well as the
1407 mach-o specific ones? */
1408 if (s->symbol.flags & BSF_WEAK)
1409 s->n_desc |= BFD_MACH_O_N_WEAK_REF;
1410
50d10658
IS
1411 /* Undefined syms, become extern. */
1412 s->n_type |= BFD_MACH_O_N_EXT;
1413 S_SET_EXTERNAL (sp);
1414 }
1415 else if (s->symbol.section == bfd_com_section_ptr)
1416 {
1417 /* ... so do comm. */
1418 s->n_type |= BFD_MACH_O_N_EXT;
1419 S_SET_EXTERNAL (sp);
b22161d6
IS
1420 }
1421 else
1422 {
1423 if ((s->symbol.flags & BSF_WEAK)
1424 && (sectype == BFD_MACH_O_S_COALESCED)
1425 && (s->n_type & (BFD_MACH_O_N_PEXT | BFD_MACH_O_N_EXT)))
1426 s->n_desc |= BFD_MACH_O_N_WEAK_DEF;
1427/* ??? we should do this - but then that reveals that the semantics of weak
1428 are different from what's supported in mach-o object files.
1429 else
1430 as_bad (_("'%s' can't be a weak_definition."),
1431 s->symbol.name); */
1432 }
1433
1434 if (s->symbol.udata.i == SYM_MACHO_FIELDS_UNSET)
1435 {
1436 /* Anything here that should be added that is non-standard. */
1437 s->n_desc &= ~BFD_MACH_O_REFERENCE_MASK;
50d10658 1438 s->symbol.udata.i = SYM_MACHO_FIELDS_NOT_VALIDATED;
b22161d6
IS
1439 }
1440 else if (s->symbol.udata.i == SYM_MACHO_FIELDS_NOT_VALIDATED)
1441 {
1442 /* Try to validate any combinations. */
1443 if (s->n_desc & BFD_MACH_O_N_WEAK_DEF)
1444 {
1445 if (s->symbol.section == bfd_und_section_ptr)
1446 as_bad (_("'%s' can't be a weak_definition (since it is"
1447 " undefined)"), s->symbol.name);
1448 else if (sectype != BFD_MACH_O_S_COALESCED)
1449 as_bad (_("'%s' can't be a weak_definition (currently only supported"
1450 " in sections of type coalesced)"), s->symbol.name);
1451 else if (! (s->n_type & (BFD_MACH_O_N_PEXT | BFD_MACH_O_N_EXT)))
1452 as_bad (_("Non-global symbol: '%s' can't be a weak_definition."),
1453 s->symbol.name);
1454 }
1455
1456 }
1457 else
1458 as_bad (_("internal error: [%s] unexpected code [%lx] in frob symbol"),
1459 s->symbol.name, (unsigned long)s->symbol.udata.i);
1460
1461 s->n_type &= ~BFD_MACH_O_N_TYPE;
1462 s->n_type |= base_type;
1463
1464 if (s->symbol.flags & BSF_GLOBAL)
1465 s->n_type |= BFD_MACH_O_N_EXT;
1466
1467 /* This cuts both ways - we promote some things to external above. */
1468 if (s->n_type & (BFD_MACH_O_N_PEXT | BFD_MACH_O_N_EXT))
1469 S_SET_EXTERNAL (sp);
1470
1471 return 0;
1472}
1473
50d10658
IS
1474static void
1475obj_mach_o_set_indirect_symbols (bfd *abfd, asection *sec,
1476 void *xxx ATTRIBUTE_UNUSED)
1477{
1478 bfd_vma sect_size = bfd_section_size (abfd, sec);
1479 bfd_mach_o_section *ms = bfd_mach_o_get_mach_o_section (sec);
1480 unsigned lazy = 0;
1481
1482 /* See if we have any indirect syms to consider. */
1483 if (indirect_syms == NULL)
1484 return;
1485
1486 /* Process indirect symbols.
1487 Check for errors, if OK attach them as a flat array to the section
1488 for which they are defined. */
1489
1490 switch (ms->flags & BFD_MACH_O_SECTION_TYPE_MASK)
1491 {
1492 case BFD_MACH_O_S_SYMBOL_STUBS:
1493 case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS:
1494 lazy = LAZY;
1495 /* Fall through. */
1496 case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS:
1497 {
1498 unsigned int nactual = 0;
1499 unsigned int ncalc;
1500 obj_mach_o_indirect_sym *isym;
1501 obj_mach_o_indirect_sym *list = NULL;
1502 obj_mach_o_indirect_sym *list_tail = NULL;
1503 unsigned long eltsiz =
1504 bfd_mach_o_section_get_entry_size (abfd, ms);
1505
1506 for (isym = indirect_syms; isym != NULL; isym = isym->next)
1507 {
1508 if (isym->sect == sec)
1509 {
1510 nactual++;
1511 if (list == NULL)
1512 list = isym;
1513 else
1514 list_tail->next = isym;
1515 list_tail = isym;
1516 }
1517 }
1518
1519 /* If none are in this section, stop here. */
1520 if (nactual == 0)
1521 break;
1522
1523 /* If we somehow added indirect symbols to a section with a zero
1524 entry size, we're dead ... */
1525 gas_assert (eltsiz != 0);
1526
1527 ncalc = (unsigned int) (sect_size / eltsiz);
1528 if (nactual != ncalc)
1529 as_bad (_("the number of .indirect_symbols defined in section %s"
1530 " does not match the number expected (%d defined, %d"
1531 " expected)"), sec->name, nactual, ncalc);
1532 else
1533 {
1534 unsigned n;
1535 bfd_mach_o_asymbol *sym;
1536 ms->indirect_syms =
1537 bfd_zalloc (abfd,
1538 nactual * sizeof (bfd_mach_o_asymbol *));
1539
1540 if (ms->indirect_syms == NULL)
1541 {
1542 as_fatal (_("internal error: failed to allocate %d indirect"
1543 "symbol pointers"), nactual);
1544 }
1545
1546 for (isym = list, n = 0; isym != NULL; isym = isym->next, n++)
1547 {
1548 /* Array is init to NULL & NULL signals a local symbol
1549 If the section is lazy-bound, we need to keep the
1550 reference to the symbol, since dyld can override. */
1551 if (S_IS_LOCAL (isym->sym) && ! lazy)
1552 ;
1553 else
1554 {
1555 sym = (bfd_mach_o_asymbol *)symbol_get_bfdsym (isym->sym);
1556 if (sym == NULL)
1557 ;
1558 /* If the symbols is external ... */
1559 else if (S_IS_EXTERNAL (isym->sym)
1560 || (sym->n_type & BFD_MACH_O_N_EXT)
1561 || ! S_IS_DEFINED (isym->sym)
1562 || lazy)
1563 {
1564 sym->n_desc &= ~LAZY;
1565 /* ... it can be lazy, if not defined or hidden. */
1566 if ((sym->n_type & BFD_MACH_O_N_TYPE)
1567 == BFD_MACH_O_N_UNDF
1568 && ! (sym->n_type & BFD_MACH_O_N_PEXT)
1569 && (sym->n_type & BFD_MACH_O_N_EXT))
1570 sym->n_desc |= lazy;
1571 ms->indirect_syms[n] = sym;
1572 }
1573 }
1574 }
1575 }
1576 }
1577 break;
1578
1579 default:
1580 break;
1581 }
1582}
1583
1584/* The process of relocation could alter what's externally visible, thus we
1585 leave setting the indirect symbols until last. */
1586
1587void
1588obj_mach_o_frob_file_after_relocs (void)
1589{
1590 bfd_map_over_sections (stdoutput, obj_mach_o_set_indirect_symbols, (char *) 0);
1591}
1592
68588f95
IS
1593/* Support stabs for mach-o. */
1594
1595void
1596obj_mach_o_process_stab (int what, const char *string,
1597 int type, int other, int desc)
1598{
1599 symbolS *symbolP;
1600 bfd_mach_o_asymbol *s;
1601
1602 switch (what)
1603 {
1604 case 'd':
1605 symbolP = symbol_new ("", now_seg, frag_now_fix (), frag_now);
1606 /* Special stabd NULL name indicator. */
1607 S_SET_NAME (symbolP, NULL);
1608 break;
1609
1610 case 'n':
1611 case 's':
1612 symbolP = symbol_new (string, undefined_section, (valueT) 0,
1613 &zero_address_frag);
1614 pseudo_set (symbolP);
1615 break;
1616
1617 default:
1618 as_bad(_("unrecognized stab type '%c'"), (char)what);
1619 abort ();
1620 break;
1621 }
1622
1623 s = (bfd_mach_o_asymbol *) symbol_get_bfdsym (symbolP);
1624 s->n_type = type;
1625 s->n_desc = desc;
1626 /* For stabd, this will eventually get overwritten by the section number. */
1627 s->n_sect = other;
1628
1629 /* It's a debug symbol. */
1630 s->symbol.flags |= BSF_DEBUGGING;
1631}
This page took 0.195726 seconds and 4 git commands to generate.