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