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