Commit | Line | Data |
---|---|---|
e57f8c65 | 1 | /* Mach-O object file format |
a4551119 | 2 | Copyright 2009, 2011 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 | ||
48 | /* TODO: Implement "-dynamic"/"-static" command line options. */ | |
49 | ||
50 | static int obj_mach_o_is_static; | |
51 | ||
52 | /* Allow for special re-ordering on output. */ | |
53 | ||
54 | static int seen_objc_section; | |
e57f8c65 | 55 | |
0c9ef0f0 TG |
56 | /* Remember the subsections_by_symbols state in case we need to reset |
57 | the file flags. */ | |
58 | static int obj_mach_o_subsections_by_symbols; | |
59 | ||
e57f8c65 TG |
60 | static void |
61 | obj_mach_o_weak (int ignore ATTRIBUTE_UNUSED) | |
62 | { | |
63 | char *name; | |
64 | int c; | |
65 | symbolS *symbolP; | |
66 | ||
67 | do | |
68 | { | |
69 | /* Get symbol name. */ | |
70 | name = input_line_pointer; | |
71 | c = get_symbol_end (); | |
72 | symbolP = symbol_find_or_make (name); | |
73 | S_SET_WEAK (symbolP); | |
74 | *input_line_pointer = c; | |
75 | SKIP_WHITESPACE (); | |
76 | ||
77 | if (c != ',') | |
78 | break; | |
79 | input_line_pointer++; | |
80 | SKIP_WHITESPACE (); | |
81 | } | |
82 | while (*input_line_pointer != '\n'); | |
83 | demand_empty_rest_of_line (); | |
84 | } | |
85 | ||
a4551119 TG |
86 | /* This will put at most 16 characters (terminated by a ',' or newline) from |
87 | the input stream into dest. If there are more than 16 chars before the | |
88 | delimiter, a warning is given and the string is truncated. On completion of | |
89 | this function, input_line_pointer will point to the char after the ',' or | |
90 | to the newline. | |
91 | ||
92 | It trims leading and trailing space. */ | |
93 | ||
94 | static int | |
95 | collect_16char_name (char *dest, const char *msg, int require_comma) | |
96 | { | |
97 | char c, *namstart; | |
98 | ||
99 | SKIP_WHITESPACE (); | |
100 | namstart = input_line_pointer; | |
101 | ||
102 | while ( (c = *input_line_pointer) != ',' | |
103 | && !is_end_of_line[(unsigned char) c]) | |
104 | input_line_pointer++; | |
105 | ||
106 | { | |
107 | int len = input_line_pointer - namstart; /* could be zero. */ | |
108 | /* lose any trailing space. */ | |
109 | while (len > 0 && namstart[len-1] == ' ') | |
110 | len--; | |
111 | if (len > 16) | |
112 | { | |
113 | *input_line_pointer = '\0'; /* make a temp string. */ | |
114 | as_bad (_("the %s name '%s' is too long (maximum 16 characters)"), | |
115 | msg, namstart); | |
116 | *input_line_pointer = c; /* restore for printing. */ | |
117 | len = 16; | |
118 | } | |
119 | if (len > 0) | |
120 | memcpy (dest, namstart, len); | |
121 | } | |
122 | ||
123 | if (c != ',' && require_comma) | |
124 | { | |
125 | as_bad (_("expected a %s name followed by a `,'"), msg); | |
126 | return 1; | |
127 | } | |
128 | ||
129 | return 0; | |
130 | } | |
131 | ||
132 | /* .section | |
133 | ||
134 | The '.section' specification syntax looks like: | |
135 | .section <segment> , <section> [, type [, attribs [, size]]] | |
136 | ||
137 | White space is allowed everywhere between elements. | |
138 | ||
139 | <segment> and <section> may be from 0 to 16 chars in length - they may | |
140 | contain spaces but leading and trailing space will be trimmed. It is | |
141 | mandatory that they be present (or that zero-length names are indicated | |
142 | by ",,"). | |
143 | ||
144 | There is only a single section type for any entry. | |
145 | ||
146 | There may be multiple attributes, they are delimited by `+'. | |
147 | ||
148 | Not all section types and attributes are accepted by the Darwin system | |
149 | assemblers as user-specifiable - although, at present, we do here. */ | |
74a6005f TG |
150 | |
151 | static void | |
152 | obj_mach_o_section (int ignore ATTRIBUTE_UNUSED) | |
153 | { | |
154 | char *p; | |
74a6005f | 155 | char c; |
a4551119 TG |
156 | unsigned int sectype = BFD_MACH_O_S_REGULAR; |
157 | unsigned int defsectype = BFD_MACH_O_S_REGULAR; | |
158 | unsigned int sectype_given = 0; | |
74a6005f | 159 | unsigned int secattr = 0; |
a4551119 TG |
160 | unsigned int defsecattr = 0; |
161 | int secattr_given = 0; | |
162 | unsigned int secalign = 0; | |
74a6005f | 163 | offsetT sizeof_stub = 0; |
a4551119 | 164 | const mach_o_section_name_xlat * xlat; |
74a6005f TG |
165 | const char *name; |
166 | flagword oldflags, flags; | |
167 | asection *sec; | |
a4551119 TG |
168 | bfd_mach_o_section *msect; |
169 | char segname[17]; | |
170 | char sectname[17]; | |
74a6005f | 171 | |
ab76eeaf IS |
172 | #ifdef md_flush_pending_output |
173 | md_flush_pending_output (); | |
174 | #endif | |
175 | ||
a4551119 | 176 | /* Zero-length segment and section names are allowed. */ |
74a6005f | 177 | /* Parse segment name. */ |
a4551119 TG |
178 | memset (segname, 0, sizeof(segname)); |
179 | if (collect_16char_name (segname, "segment", 1)) | |
74a6005f | 180 | { |
74a6005f TG |
181 | ignore_rest_of_line (); |
182 | return; | |
183 | } | |
a4551119 | 184 | input_line_pointer++; /* Skip the terminating ',' */ |
74a6005f TG |
185 | |
186 | /* Parse section name. */ | |
a4551119 TG |
187 | memset (sectname, 0, sizeof(sectname)); |
188 | collect_16char_name (sectname, "section", 0); | |
74a6005f TG |
189 | |
190 | /* Parse type. */ | |
191 | if (*input_line_pointer == ',') | |
192 | { | |
a4551119 TG |
193 | char tmpc; |
194 | int len; | |
74a6005f | 195 | input_line_pointer++; |
a4551119 | 196 | SKIP_WHITESPACE (); |
74a6005f | 197 | p = input_line_pointer; |
a4551119 TG |
198 | while ((c = *input_line_pointer) != ',' |
199 | && !is_end_of_line[(unsigned char) c]) | |
200 | input_line_pointer++; | |
201 | ||
202 | len = input_line_pointer - p; | |
203 | /* strip trailing spaces. */ | |
204 | while (len > 0 && p[len-1] == ' ') | |
205 | len--; | |
206 | tmpc = p[len]; | |
207 | ||
208 | /* Temporarily make a string from the token. */ | |
209 | p[len] = 0; | |
ab76eeaf | 210 | sectype = bfd_mach_o_get_section_type_from_name (stdoutput, p); |
a4551119 | 211 | if (sectype > 255) /* Max Section ID == 255. */ |
74a6005f TG |
212 | { |
213 | as_bad (_("unknown or invalid section type '%s'"), p); | |
ab76eeaf IS |
214 | p[len] = tmpc; |
215 | ignore_rest_of_line (); | |
216 | return; | |
74a6005f | 217 | } |
a4551119 TG |
218 | else |
219 | sectype_given = 1; | |
220 | /* Restore. */ | |
ab76eeaf | 221 | p[len] = tmpc; |
a4551119 TG |
222 | |
223 | /* Parse attributes. | |
224 | TODO: check validity of attributes for section type. */ | |
225 | if (sectype_given && c == ',') | |
74a6005f TG |
226 | { |
227 | do | |
228 | { | |
229 | int attr; | |
230 | ||
a4551119 | 231 | /* Skip initial `,' and subsequent `+'. */ |
74a6005f | 232 | input_line_pointer++; |
a4551119 TG |
233 | SKIP_WHITESPACE (); |
234 | p = input_line_pointer; | |
235 | while ((c = *input_line_pointer) != '+' | |
236 | && c != ',' | |
237 | && !is_end_of_line[(unsigned char) c]) | |
238 | input_line_pointer++; | |
239 | ||
240 | len = input_line_pointer - p; | |
241 | /* strip trailing spaces. */ | |
242 | while (len > 0 && p[len-1] == ' ') | |
243 | len--; | |
244 | tmpc = p[len]; | |
245 | ||
246 | /* Temporarily make a string from the token. */ | |
247 | p[len] ='\0'; | |
74a6005f | 248 | attr = bfd_mach_o_get_section_attribute_from_name (p); |
a4551119 | 249 | if (attr == -1) |
ab76eeaf IS |
250 | { |
251 | as_bad (_("unknown or invalid section attribute '%s'"), p); | |
252 | p[len] = tmpc; | |
253 | ignore_rest_of_line (); | |
254 | return; | |
255 | } | |
74a6005f | 256 | else |
a4551119 TG |
257 | { |
258 | secattr_given = 1; | |
259 | secattr |= attr; | |
260 | } | |
261 | /* Restore. */ | |
262 | p[len] = tmpc; | |
74a6005f TG |
263 | } |
264 | while (*input_line_pointer == '+'); | |
265 | ||
266 | /* Parse sizeof_stub. */ | |
ab76eeaf | 267 | if (secattr_given && *input_line_pointer == ',') |
74a6005f TG |
268 | { |
269 | if (sectype != BFD_MACH_O_S_SYMBOL_STUBS) | |
ab76eeaf IS |
270 | { |
271 | as_bad (_("unexpected section size information")); | |
272 | ignore_rest_of_line (); | |
273 | return; | |
274 | } | |
74a6005f | 275 | |
a4551119 | 276 | input_line_pointer++; |
74a6005f TG |
277 | sizeof_stub = get_absolute_expression (); |
278 | } | |
ab76eeaf IS |
279 | else if (secattr_given && sectype == BFD_MACH_O_S_SYMBOL_STUBS) |
280 | { | |
281 | as_bad (_("missing sizeof_stub expression")); | |
282 | ignore_rest_of_line (); | |
283 | return; | |
284 | } | |
74a6005f TG |
285 | } |
286 | } | |
74a6005f | 287 | |
a4551119 TG |
288 | flags = SEC_NO_FLAGS; |
289 | /* This provides default bfd flags and default mach-o section type and | |
290 | attributes along with the canonical name. */ | |
291 | xlat = bfd_mach_o_section_data_for_mach_sect (stdoutput, segname, sectname); | |
292 | if (xlat != NULL) | |
293 | { | |
294 | name = xstrdup (xlat->bfd_name); | |
295 | flags = xlat->bfd_flags; | |
296 | defsectype = xlat->macho_sectype; | |
297 | defsecattr = xlat->macho_secattr; | |
298 | secalign = xlat->sectalign; | |
299 | } | |
300 | else | |
74a6005f TG |
301 | { |
302 | /* There is no normal BFD section name for this section. Create one. | |
303 | The name created doesn't really matter as it will never be written | |
304 | on disk. */ | |
305 | size_t seglen = strlen (segname); | |
306 | size_t sectlen = strlen (sectname); | |
307 | char *n; | |
308 | ||
309 | n = xmalloc (seglen + 1 + sectlen + 1); | |
310 | memcpy (n, segname, seglen); | |
311 | n[seglen] = '.'; | |
312 | memcpy (n + seglen + 1, sectname, sectlen); | |
313 | n[seglen + 1 + sectlen] = 0; | |
314 | name = n; | |
315 | } | |
316 | ||
74a6005f TG |
317 | /* Sub-segments don't exists as is on Mach-O. */ |
318 | sec = subseg_new (name, 0); | |
319 | ||
320 | oldflags = bfd_get_section_flags (stdoutput, sec); | |
a4551119 TG |
321 | msect = bfd_mach_o_get_mach_o_section (sec); |
322 | if (oldflags == SEC_NO_FLAGS) | |
74a6005f | 323 | { |
74a6005f TG |
324 | if (! bfd_set_section_flags (stdoutput, sec, flags)) |
325 | as_warn (_("error setting flags for \"%s\": %s"), | |
326 | bfd_section_name (stdoutput, sec), | |
327 | bfd_errmsg (bfd_get_error ())); | |
74a6005f TG |
328 | strncpy (msect->segname, segname, sizeof (msect->segname)); |
329 | msect->segname[16] = 0; | |
330 | strncpy (msect->sectname, sectname, sizeof (msect->sectname)); | |
331 | msect->sectname[16] = 0; | |
a4551119 TG |
332 | msect->align = secalign; |
333 | if (sectype_given) | |
334 | { | |
335 | msect->flags = sectype; | |
336 | if (secattr_given) | |
337 | msect->flags |= secattr; | |
338 | else | |
339 | msect->flags |= defsecattr; | |
340 | } | |
341 | else | |
342 | msect->flags = defsectype | defsecattr; | |
74a6005f TG |
343 | msect->reserved2 = sizeof_stub; |
344 | } | |
345 | else if (flags != SEC_NO_FLAGS) | |
346 | { | |
a4551119 TG |
347 | if (flags != oldflags |
348 | || msect->flags != (secattr | sectype)) | |
74a6005f TG |
349 | as_warn (_("Ignoring changed section attributes for %s"), name); |
350 | } | |
ab76eeaf | 351 | demand_empty_rest_of_line (); |
74a6005f TG |
352 | } |
353 | ||
a4551119 TG |
354 | static segT |
355 | obj_mach_o_segT_from_bfd_name (const char *nam, int must_succeed) | |
74a6005f | 356 | { |
a4551119 TG |
357 | const mach_o_section_name_xlat *xlat; |
358 | const char *segn; | |
359 | segT sec; | |
360 | ||
361 | /* BFD has tables of flags and default attributes for all the sections that | |
362 | have a 'canonical' name. */ | |
363 | xlat = bfd_mach_o_section_data_for_bfd_name (stdoutput, nam, &segn); | |
364 | if (xlat == NULL) | |
365 | { | |
366 | if (must_succeed) | |
367 | as_fatal (_("BFD is out of sync with GAS, " | |
368 | "unhandled well-known section type `%s'"), nam); | |
369 | return NULL; | |
370 | } | |
371 | ||
372 | sec = bfd_get_section_by_name (stdoutput, nam); | |
373 | if (sec == NULL) | |
374 | { | |
375 | bfd_mach_o_section *msect; | |
376 | ||
377 | sec = subseg_force_new (xlat->bfd_name, 0); | |
378 | ||
379 | /* Set default type, attributes and alignment. */ | |
380 | msect = bfd_mach_o_get_mach_o_section (sec); | |
381 | msect->flags = xlat->macho_sectype | xlat->macho_secattr; | |
382 | msect->align = xlat->sectalign; | |
383 | ||
384 | if ((msect->flags & BFD_MACH_O_SECTION_TYPE_MASK) | |
385 | == BFD_MACH_O_S_ZEROFILL) | |
386 | seg_info (sec)->bss = 1; | |
387 | } | |
388 | ||
389 | return sec; | |
390 | } | |
391 | ||
392 | static const char * const known_sections[] = | |
393 | { | |
394 | /* 0 */ NULL, | |
395 | /* __TEXT */ | |
396 | /* 1 */ ".const", | |
397 | /* 2 */ ".static_const", | |
398 | /* 3 */ ".cstring", | |
399 | /* 4 */ ".literal4", | |
400 | /* 5 */ ".literal8", | |
401 | /* 6 */ ".literal16", | |
402 | /* 7 */ ".constructor", | |
403 | /* 8 */ ".destructor", | |
404 | /* 9 */ ".eh_frame", | |
405 | /* __DATA */ | |
406 | /* 10 */ ".const_data", | |
407 | /* 11 */ ".static_data", | |
408 | /* 12 */ ".mod_init_func", | |
409 | /* 13 */ ".mod_term_func", | |
410 | /* 14 */ ".dyld", | |
411 | /* 15 */ ".cfstring" | |
74a6005f TG |
412 | }; |
413 | ||
a4551119 | 414 | /* Interface for a known non-optional section directive. */ |
74a6005f TG |
415 | |
416 | static void | |
417 | obj_mach_o_known_section (int sect_index) | |
418 | { | |
a4551119 TG |
419 | segT section; |
420 | ||
421 | #ifdef md_flush_pending_output | |
422 | md_flush_pending_output (); | |
423 | #endif | |
424 | ||
425 | section = obj_mach_o_segT_from_bfd_name (known_sections[sect_index], 1); | |
426 | if (section != NULL) | |
427 | subseg_set (section, 0); | |
428 | ||
429 | /* else, we leave the section as it was; there was a fatal error anyway. */ | |
430 | } | |
431 | ||
432 | static const char * const objc_sections[] = | |
433 | { | |
434 | /* 0 */ NULL, | |
435 | /* 1 */ ".objc_class", | |
436 | /* 2 */ ".objc_meta_class", | |
437 | /* 3 */ ".objc_cat_cls_meth", | |
438 | /* 4 */ ".objc_cat_inst_meth", | |
439 | /* 5 */ ".objc_protocol", | |
440 | /* 6 */ ".objc_string_object", | |
441 | /* 7 */ ".objc_cls_meth", | |
442 | /* 8 */ ".objc_inst_meth", | |
443 | /* 9 */ ".objc_cls_refs", | |
444 | /* 10 */ ".objc_message_refs", | |
445 | /* 11 */ ".objc_symbols", | |
446 | /* 12 */ ".objc_category", | |
447 | /* 13 */ ".objc_class_vars", | |
448 | /* 14 */ ".objc_instance_vars", | |
449 | /* 15 */ ".objc_module_info", | |
450 | /* 16 */ ".cstring", /* objc_class_names Alias for .cstring */ | |
451 | /* 17 */ ".cstring", /* Alias objc_meth_var_types for .cstring */ | |
452 | /* 18 */ ".cstring", /* objc_meth_var_names Alias for .cstring */ | |
453 | /* 19 */ ".objc_selector_strs", | |
454 | /* 20 */ ".objc_image_info", /* extension. */ | |
455 | /* 21 */ ".objc_selector_fixup", /* extension. */ | |
456 | /* 22 */ ".objc1_class_ext", /* ObjC-1 extension. */ | |
457 | /* 23 */ ".objc1_property_list", /* ObjC-1 extension. */ | |
458 | /* 24 */ ".objc1_protocol_ext" /* ObjC-1 extension. */ | |
459 | }; | |
460 | ||
461 | /* This currently does the same as known_sections, but kept separate for | |
462 | ease of maintenance. */ | |
463 | ||
464 | static void | |
465 | obj_mach_o_objc_section (int sect_index) | |
466 | { | |
467 | segT section; | |
468 | ||
469 | #ifdef md_flush_pending_output | |
470 | md_flush_pending_output (); | |
471 | #endif | |
472 | ||
473 | section = obj_mach_o_segT_from_bfd_name (objc_sections[sect_index], 1); | |
474 | if (section != NULL) | |
475 | { | |
476 | seen_objc_section = 1; /* We need to ensure that certain sections are | |
477 | present and in the right order. */ | |
478 | subseg_set (section, 0); | |
479 | } | |
480 | ||
481 | /* else, we leave the section as it was; there was a fatal error anyway. */ | |
482 | } | |
483 | ||
484 | /* Debug section directives. */ | |
485 | ||
486 | static const char * const debug_sections[] = | |
487 | { | |
488 | /* 0 */ NULL, | |
489 | /* __DWARF */ | |
490 | /* 1 */ ".debug_frame", | |
491 | /* 2 */ ".debug_info", | |
492 | /* 3 */ ".debug_abbrev", | |
493 | /* 4 */ ".debug_aranges", | |
494 | /* 5 */ ".debug_macinfo", | |
495 | /* 6 */ ".debug_line", | |
496 | /* 7 */ ".debug_loc", | |
497 | /* 8 */ ".debug_pubnames", | |
498 | /* 9 */ ".debug_pubtypes", | |
499 | /* 10 */ ".debug_str", | |
500 | /* 11 */ ".debug_ranges", | |
501 | /* 12 */ ".debug_macro" | |
502 | }; | |
503 | ||
504 | /* ??? Maybe these should be conditional on gdwarf-*. | |
505 | It`s also likely that we will need to be able to set them from the cfi | |
506 | code. */ | |
507 | ||
508 | static void | |
509 | obj_mach_o_debug_section (int sect_index) | |
510 | { | |
511 | segT section; | |
512 | ||
513 | #ifdef md_flush_pending_output | |
514 | md_flush_pending_output (); | |
515 | #endif | |
516 | ||
517 | section = obj_mach_o_segT_from_bfd_name (debug_sections[sect_index], 1); | |
518 | if (section != NULL) | |
519 | subseg_set (section, 0); | |
520 | ||
521 | /* else, we leave the section as it was; there was a fatal error anyway. */ | |
522 | } | |
523 | ||
524 | /* This could be moved to the tc-xx files, but there is so little dependency | |
525 | there, that the code might as well be shared. */ | |
526 | ||
527 | struct opt_tgt_sect | |
528 | { | |
529 | const char *name; | |
530 | unsigned x86_val; | |
531 | unsigned ppc_val; | |
532 | }; | |
533 | ||
534 | /* The extensions here are for specific sections that are generated by GCC | |
535 | and Darwin system tools, but don't have directives in the `system as'. */ | |
536 | ||
537 | static const struct opt_tgt_sect tgt_sections[] = | |
538 | { | |
539 | /* 0 */ { NULL, 0, 0}, | |
540 | /* 1 */ { ".lazy_symbol_pointer", 0, 0}, | |
541 | /* 2 */ { ".lazy_symbol_pointer2", 0, 0}, /* X86 - extension */ | |
542 | /* 3 */ { ".lazy_symbol_pointer3", 0, 0}, /* X86 - extension */ | |
543 | /* 4 */ { ".non_lazy_symbol_pointer", 0, 0}, | |
544 | /* 5 */ { ".non_lazy_symbol_pointer_x86", 0, 0}, /* X86 - extension */ | |
545 | /* 6 */ { ".symbol_stub", 16, 20}, | |
546 | /* 7 */ { ".symbol_stub1", 0, 16}, /* PPC - extension */ | |
547 | /* 8 */ { ".picsymbol_stub", 26, 36}, | |
548 | /* 9 */ { ".picsymbol_stub1", 0, 32}, /* PPC - extension */ | |
549 | /* 10 */ { ".picsymbol_stub2", 25, 0}, /* X86 - extension */ | |
550 | /* 11 */ { ".picsymbol_stub3", 5, 0}, /* X86 - extension */ | |
551 | }; | |
552 | ||
553 | /* Interface for an optional section directive. */ | |
554 | ||
555 | static void | |
556 | obj_mach_o_opt_tgt_section (int sect_index) | |
557 | { | |
558 | const struct opt_tgt_sect *tgtsct = &tgt_sections[sect_index]; | |
559 | segT section; | |
74a6005f TG |
560 | |
561 | #ifdef md_flush_pending_output | |
562 | md_flush_pending_output (); | |
563 | #endif | |
564 | ||
a4551119 TG |
565 | section = obj_mach_o_segT_from_bfd_name (tgtsct->name, 0); |
566 | if (section == NULL) | |
74a6005f | 567 | { |
a4551119 TG |
568 | as_bad (_("%s is not used for the selected target"), tgtsct->name); |
569 | /* Leave the section as it is. */ | |
74a6005f TG |
570 | } |
571 | else | |
572 | { | |
a4551119 TG |
573 | bfd_mach_o_section *mo_sec = bfd_mach_o_get_mach_o_section (section); |
574 | subseg_set (section, 0); | |
575 | #if defined (TC_I386) | |
576 | mo_sec->reserved2 = tgtsct->x86_val; | |
577 | #elif defined (TC_PPC) | |
578 | mo_sec->reserved2 = tgtsct->ppc_val; | |
579 | #else | |
580 | mo_sec->reserved2 = 0; | |
581 | #endif | |
582 | } | |
583 | } | |
74a6005f | 584 | |
a4551119 TG |
585 | /* We don't necessarily have the three 'base' sections on mach-o. |
586 | Normally, we would start up with only the 'text' section defined. | |
587 | However, even that can be suppressed with (TODO) c/l option "-n". | |
588 | Thus, we have to be able to create all three sections on-demand. */ | |
74a6005f | 589 | |
a4551119 TG |
590 | static void |
591 | obj_mach_o_base_section (int sect_index) | |
592 | { | |
593 | segT section; | |
594 | ||
595 | #ifdef md_flush_pending_output | |
596 | md_flush_pending_output (); | |
597 | #endif | |
598 | ||
599 | /* We don't support numeric (or any other) qualifications on the | |
600 | well-known section shorthands. */ | |
601 | demand_empty_rest_of_line (); | |
602 | ||
603 | switch (sect_index) | |
604 | { | |
605 | /* Handle the three sections that are globally known within GAS. | |
606 | For Mach-O, these are created on demand rather than at startup. */ | |
607 | case 1: | |
608 | if (text_section == NULL) | |
609 | text_section = obj_mach_o_segT_from_bfd_name (TEXT_SECTION_NAME, 1); | |
610 | if (obj_mach_o_is_static) | |
611 | { | |
612 | bfd_mach_o_section *mo_sec | |
613 | = bfd_mach_o_get_mach_o_section (text_section); | |
614 | mo_sec->flags &= ~BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS; | |
615 | } | |
616 | section = text_section; | |
617 | break; | |
618 | case 2: | |
619 | if (data_section == NULL) | |
620 | data_section = obj_mach_o_segT_from_bfd_name (DATA_SECTION_NAME, 1); | |
621 | section = data_section; | |
622 | break; | |
623 | case 3: | |
624 | /* ??? maybe this achieves very little, as an addition. */ | |
625 | if (bss_section == NULL) | |
626 | { | |
627 | bss_section = obj_mach_o_segT_from_bfd_name (BSS_SECTION_NAME, 1); | |
628 | seg_info (bss_section)->bss = 1; | |
629 | } | |
630 | section = bss_section; | |
631 | break; | |
632 | default: | |
633 | as_fatal (_("internal error: base section index out of range")); | |
634 | return; | |
635 | break; | |
74a6005f | 636 | } |
a4551119 | 637 | subseg_set (section, 0); |
74a6005f TG |
638 | } |
639 | ||
a4551119 TG |
640 | /* This finishes off parsing a .comm or .lcomm statement, which both can have |
641 | an (optional) alignment field. It also allows us to create the bss section | |
642 | on demand. */ | |
74a6005f TG |
643 | |
644 | static symbolS * | |
a4551119 TG |
645 | obj_mach_o_common_parse (int is_local, symbolS *symbolP, |
646 | addressT size) | |
74a6005f TG |
647 | { |
648 | addressT align = 0; | |
649 | ||
a4551119 TG |
650 | /* Both comm and lcomm take an optional alignment, as a power |
651 | of two between 1 and 15. */ | |
74a6005f TG |
652 | if (*input_line_pointer == ',') |
653 | { | |
a4551119 | 654 | /* We expect a power of 2. */ |
74a6005f TG |
655 | align = parse_align (0); |
656 | if (align == (addressT) -1) | |
657 | return NULL; | |
a4551119 TG |
658 | if (align > 15) |
659 | { | |
660 | as_warn (_("Alignment (%lu) too large: 15 assumed."), | |
661 | (unsigned long)align); | |
662 | align = 15; | |
663 | } | |
74a6005f TG |
664 | } |
665 | ||
a4551119 TG |
666 | if (is_local) |
667 | { | |
668 | /* Create the BSS section on demand. */ | |
669 | if (bss_section == NULL) | |
670 | { | |
671 | bss_section = obj_mach_o_segT_from_bfd_name (BSS_SECTION_NAME, 1); | |
672 | seg_info (bss_section)->bss = 1; | |
673 | } | |
674 | bss_alloc (symbolP, size, align); | |
675 | S_CLEAR_EXTERNAL (symbolP); | |
676 | } | |
677 | else | |
678 | { | |
679 | S_SET_VALUE (symbolP, size); | |
680 | S_SET_ALIGN (symbolP, align); | |
681 | S_SET_EXTERNAL (symbolP); | |
682 | S_SET_SEGMENT (symbolP, bfd_com_section_ptr); | |
683 | } | |
74a6005f TG |
684 | |
685 | symbol_get_bfdsym (symbolP)->flags |= BSF_OBJECT; | |
686 | ||
687 | return symbolP; | |
688 | } | |
689 | ||
690 | static void | |
a4551119 | 691 | obj_mach_o_comm (int is_local) |
74a6005f | 692 | { |
a4551119 | 693 | s_comm_internal (is_local, obj_mach_o_common_parse); |
74a6005f TG |
694 | } |
695 | ||
0c9ef0f0 TG |
696 | /* Set properties that apply to the whole file. At present, the only |
697 | one defined, is subsections_via_symbols. */ | |
698 | ||
699 | typedef enum obj_mach_o_file_properties { | |
700 | OBJ_MACH_O_FILE_PROP_NONE = 0, | |
701 | OBJ_MACH_O_FILE_PROP_SUBSECTS_VIA_SYMS, | |
702 | OBJ_MACH_O_FILE_PROP_MAX | |
703 | } obj_mach_o_file_properties; | |
704 | ||
705 | static void | |
706 | obj_mach_o_fileprop (int prop) | |
74a6005f | 707 | { |
0c9ef0f0 TG |
708 | if (prop < 0 || prop >= OBJ_MACH_O_FILE_PROP_MAX) |
709 | as_fatal (_("internal error: bad file property ID %d"), prop); | |
710 | ||
711 | switch ((obj_mach_o_file_properties) prop) | |
712 | { | |
713 | case OBJ_MACH_O_FILE_PROP_SUBSECTS_VIA_SYMS: | |
499963bc | 714 | obj_mach_o_subsections_by_symbols = 1; |
0c9ef0f0 TG |
715 | if (!bfd_set_private_flags (stdoutput, |
716 | BFD_MACH_O_MH_SUBSECTIONS_VIA_SYMBOLS)) | |
717 | as_bad (_("failed to set subsections by symbols")); | |
718 | demand_empty_rest_of_line (); | |
719 | break; | |
720 | default: | |
721 | break; | |
722 | } | |
74a6005f TG |
723 | } |
724 | ||
a4551119 TG |
725 | /* Dummy function to allow test-code to work while we are working |
726 | on things. */ | |
727 | ||
728 | static void | |
729 | obj_mach_o_placeholder (int arg ATTRIBUTE_UNUSED) | |
730 | { | |
731 | ignore_rest_of_line (); | |
732 | } | |
733 | ||
e57f8c65 TG |
734 | const pseudo_typeS mach_o_pseudo_table[] = |
735 | { | |
a4551119 | 736 | /* Section directives. */ |
74a6005f | 737 | { "comm", obj_mach_o_comm, 0 }, |
a4551119 TG |
738 | { "lcomm", obj_mach_o_comm, 1 }, |
739 | ||
740 | { "text", obj_mach_o_base_section, 1}, | |
741 | { "data", obj_mach_o_base_section, 2}, | |
742 | { "bss", obj_mach_o_base_section, 3}, /* extension */ | |
743 | ||
744 | { "const", obj_mach_o_known_section, 1}, | |
745 | { "static_const", obj_mach_o_known_section, 2}, | |
746 | { "cstring", obj_mach_o_known_section, 3}, | |
747 | { "literal4", obj_mach_o_known_section, 4}, | |
748 | { "literal8", obj_mach_o_known_section, 5}, | |
749 | { "literal16", obj_mach_o_known_section, 6}, | |
750 | { "constructor", obj_mach_o_known_section, 7}, | |
751 | { "destructor", obj_mach_o_known_section, 8}, | |
752 | { "eh_frame", obj_mach_o_known_section, 9}, | |
753 | ||
754 | { "const_data", obj_mach_o_known_section, 10}, | |
755 | { "static_data", obj_mach_o_known_section, 11}, | |
756 | { "mod_init_func", obj_mach_o_known_section, 12}, | |
757 | { "mod_term_func", obj_mach_o_known_section, 13}, | |
758 | { "dyld", obj_mach_o_known_section, 14}, | |
759 | { "cfstring", obj_mach_o_known_section, 15}, | |
760 | ||
761 | { "objc_class", obj_mach_o_objc_section, 1}, | |
762 | { "objc_meta_class", obj_mach_o_objc_section, 2}, | |
763 | { "objc_cat_cls_meth", obj_mach_o_objc_section, 3}, | |
764 | { "objc_cat_inst_meth", obj_mach_o_objc_section, 4}, | |
765 | { "objc_protocol", obj_mach_o_objc_section, 5}, | |
766 | { "objc_string_object", obj_mach_o_objc_section, 6}, | |
767 | { "objc_cls_meth", obj_mach_o_objc_section, 7}, | |
768 | { "objc_inst_meth", obj_mach_o_objc_section, 8}, | |
769 | { "objc_cls_refs", obj_mach_o_objc_section, 9}, | |
770 | { "objc_message_refs", obj_mach_o_objc_section, 10}, | |
771 | { "objc_symbols", obj_mach_o_objc_section, 11}, | |
772 | { "objc_category", obj_mach_o_objc_section, 12}, | |
773 | { "objc_class_vars", obj_mach_o_objc_section, 13}, | |
774 | { "objc_instance_vars", obj_mach_o_objc_section, 14}, | |
775 | { "objc_module_info", obj_mach_o_objc_section, 15}, | |
776 | { "objc_class_names", obj_mach_o_objc_section, 16}, /* Alias for .cstring */ | |
777 | { "objc_meth_var_types", obj_mach_o_objc_section, 17}, /* Alias for .cstring */ | |
778 | { "objc_meth_var_names", obj_mach_o_objc_section, 18}, /* Alias for .cstring */ | |
779 | { "objc_selector_strs", obj_mach_o_objc_section, 19}, | |
780 | { "objc_image_info", obj_mach_o_objc_section, 20}, /* extension. */ | |
781 | { "objc_selector_fixup", obj_mach_o_objc_section, 21}, /* extension. */ | |
782 | { "objc1_class_ext", obj_mach_o_objc_section, 22}, /* ObjC-1 extension. */ | |
783 | { "objc1_property_list", obj_mach_o_objc_section, 23}, /* ObjC-1 extension. */ | |
784 | { "objc1_protocol_ext", obj_mach_o_objc_section, 24}, /* ObjC-1 extension. */ | |
785 | ||
786 | { "debug_frame", obj_mach_o_debug_section, 1}, /* extension. */ | |
787 | { "debug_info", obj_mach_o_debug_section, 2}, /* extension. */ | |
788 | { "debug_abbrev", obj_mach_o_debug_section, 3}, /* extension. */ | |
789 | { "debug_aranges", obj_mach_o_debug_section, 4}, /* extension. */ | |
790 | { "debug_macinfo", obj_mach_o_debug_section, 5}, /* extension. */ | |
791 | { "debug_line", obj_mach_o_debug_section, 6}, /* extension. */ | |
792 | { "debug_loc", obj_mach_o_debug_section, 7}, /* extension. */ | |
793 | { "debug_pubnames", obj_mach_o_debug_section, 8}, /* extension. */ | |
794 | { "debug_pubtypes", obj_mach_o_debug_section, 9}, /* extension. */ | |
795 | { "debug_str", obj_mach_o_debug_section, 10}, /* extension. */ | |
796 | { "debug_ranges", obj_mach_o_debug_section, 11}, /* extension. */ | |
797 | { "debug_macro", obj_mach_o_debug_section, 12}, /* extension. */ | |
798 | ||
799 | { "lazy_symbol_pointer", obj_mach_o_opt_tgt_section, 1}, | |
800 | { "lazy_symbol_pointer2", obj_mach_o_opt_tgt_section, 2}, /* extension. */ | |
801 | { "lazy_symbol_pointer3", obj_mach_o_opt_tgt_section, 3}, /* extension. */ | |
802 | { "non_lazy_symbol_pointer", obj_mach_o_opt_tgt_section, 4}, | |
803 | { "non_lazy_symbol_pointer_x86", obj_mach_o_opt_tgt_section, 5}, /* extension. */ | |
804 | { "symbol_stub", obj_mach_o_opt_tgt_section, 6}, | |
805 | { "symbol_stub1", obj_mach_o_opt_tgt_section, 7}, /* extension. */ | |
806 | { "picsymbol_stub", obj_mach_o_opt_tgt_section, 8}, /* extension. */ | |
807 | { "picsymbol_stub1", obj_mach_o_opt_tgt_section, 9}, /* extension. */ | |
808 | { "picsymbol_stub2", obj_mach_o_opt_tgt_section, 4}, /* extension. */ | |
809 | { "picsymbol_stub3", obj_mach_o_opt_tgt_section, 4}, /* extension. */ | |
810 | ||
811 | { "section", obj_mach_o_section, 0}, | |
812 | ||
813 | /* Symbol-related. */ | |
814 | { "indirect_symbol", obj_mach_o_placeholder, 0}, | |
815 | { "weak_definition", obj_mach_o_placeholder, 0}, | |
816 | { "private_extern", obj_mach_o_placeholder, 0}, | |
817 | { "weak", obj_mach_o_weak, 0}, /* extension */ | |
818 | ||
819 | /* File flags. */ | |
0c9ef0f0 TG |
820 | { "subsections_via_symbols", obj_mach_o_fileprop, |
821 | OBJ_MACH_O_FILE_PROP_SUBSECTS_VIA_SYMS}, | |
e57f8c65 TG |
822 | |
823 | {NULL, NULL, 0} | |
824 | }; |