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