mach-o.c revision 1.10 1 /* Mach-O support for BFD.
2 Copyright (C) 1999-2025 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
20
21 #include "sysdep.h"
22 #include <limits.h>
23 #include "bfd.h"
24 #include "libbfd.h"
25 #include "libiberty.h"
26 #include "mach-o.h"
27 #include "aout/stab_gnu.h"
28 #include "mach-o/reloc.h"
29 #include "mach-o/external.h"
30 #include <ctype.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #define bfd_mach_o_object_p bfd_mach_o_gen_object_p
35 #define bfd_mach_o_core_p bfd_mach_o_gen_core_p
36 #define bfd_mach_o_mkobject bfd_mach_o_gen_mkobject
37
38 #define FILE_ALIGN(off, algn) \
39 (((off) + ((ufile_ptr) 1 << (algn)) - 1) & ((ufile_ptr) -1 << (algn)))
40
41 static bool
42 bfd_mach_o_read_dyld_content (bfd *abfd, bfd_mach_o_dyld_info_command *cmd);
43
44 unsigned int
45 bfd_mach_o_version (bfd *abfd)
46 {
47 bfd_mach_o_data_struct *mdata = NULL;
48
49 BFD_ASSERT (bfd_mach_o_valid (abfd));
50 mdata = bfd_mach_o_get_data (abfd);
51
52 return mdata->header.version;
53 }
54
55 bool
56 bfd_mach_o_valid (bfd *abfd)
57 {
58 if (abfd == NULL || abfd->xvec == NULL)
59 return false;
60
61 if (abfd->xvec->flavour != bfd_target_mach_o_flavour)
62 return false;
63
64 if (bfd_mach_o_get_data (abfd) == NULL)
65 return false;
66 return true;
67 }
68
69 static inline bool
70 mach_o_wide_p (bfd_mach_o_header *header)
71 {
72 switch (header->version)
73 {
74 case 1:
75 return false;
76 case 2:
77 return true;
78 default:
79 BFD_FAIL ();
80 return false;
81 }
82 }
83
84 static inline bool
85 bfd_mach_o_wide_p (bfd *abfd)
86 {
87 return mach_o_wide_p (&bfd_mach_o_get_data (abfd)->header);
88 }
89
90 /* Tables to translate well known Mach-O segment/section names to bfd
91 names. Use of canonical names (such as .text or .debug_frame) is required
92 by gdb. */
93
94 /* __TEXT Segment. */
95 static const mach_o_section_name_xlat text_section_names_xlat[] =
96 {
97 { ".text", "__text",
98 SEC_CODE | SEC_LOAD, BFD_MACH_O_S_REGULAR,
99 BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS, 0},
100 { ".const", "__const",
101 SEC_READONLY | SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
102 BFD_MACH_O_S_ATTR_NONE, 0},
103 { ".static_const", "__static_const",
104 SEC_READONLY | SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
105 BFD_MACH_O_S_ATTR_NONE, 0},
106 { ".cstring", "__cstring",
107 SEC_READONLY | SEC_DATA | SEC_LOAD | SEC_MERGE | SEC_STRINGS,
108 BFD_MACH_O_S_CSTRING_LITERALS,
109 BFD_MACH_O_S_ATTR_NONE, 0},
110 { ".literal4", "__literal4",
111 SEC_READONLY | SEC_DATA | SEC_LOAD, BFD_MACH_O_S_4BYTE_LITERALS,
112 BFD_MACH_O_S_ATTR_NONE, 2},
113 { ".literal8", "__literal8",
114 SEC_READONLY | SEC_DATA | SEC_LOAD, BFD_MACH_O_S_8BYTE_LITERALS,
115 BFD_MACH_O_S_ATTR_NONE, 3},
116 { ".literal16", "__literal16",
117 SEC_READONLY | SEC_DATA | SEC_LOAD, BFD_MACH_O_S_16BYTE_LITERALS,
118 BFD_MACH_O_S_ATTR_NONE, 4},
119 { ".constructor", "__constructor",
120 SEC_CODE | SEC_LOAD, BFD_MACH_O_S_REGULAR,
121 BFD_MACH_O_S_ATTR_NONE, 0},
122 { ".destructor", "__destructor",
123 SEC_CODE | SEC_LOAD, BFD_MACH_O_S_REGULAR,
124 BFD_MACH_O_S_ATTR_NONE, 0},
125 { ".eh_frame", "__eh_frame",
126 SEC_READONLY | SEC_DATA | SEC_LOAD, BFD_MACH_O_S_COALESCED,
127 BFD_MACH_O_S_ATTR_LIVE_SUPPORT
128 | BFD_MACH_O_S_ATTR_STRIP_STATIC_SYMS
129 | BFD_MACH_O_S_ATTR_NO_TOC, 2},
130 { NULL, NULL, 0, 0, 0, 0}
131 };
132
133 /* __DATA Segment. */
134 static const mach_o_section_name_xlat data_section_names_xlat[] =
135 {
136 { ".data", "__data",
137 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
138 BFD_MACH_O_S_ATTR_NONE, 0},
139 { ".bss", "__bss",
140 SEC_NO_FLAGS, BFD_MACH_O_S_ZEROFILL,
141 BFD_MACH_O_S_ATTR_NONE, 0},
142 { ".const_data", "__const",
143 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
144 BFD_MACH_O_S_ATTR_NONE, 0},
145 { ".static_data", "__static_data",
146 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
147 BFD_MACH_O_S_ATTR_NONE, 0},
148 { ".mod_init_func", "__mod_init_func",
149 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_MOD_INIT_FUNC_POINTERS,
150 BFD_MACH_O_S_ATTR_NONE, 2},
151 { ".mod_term_func", "__mod_term_func",
152 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_MOD_FINI_FUNC_POINTERS,
153 BFD_MACH_O_S_ATTR_NONE, 2},
154 { ".dyld", "__dyld",
155 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
156 BFD_MACH_O_S_ATTR_NONE, 0},
157 { ".cfstring", "__cfstring",
158 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
159 BFD_MACH_O_S_ATTR_NONE, 2},
160 { NULL, NULL, 0, 0, 0, 0}
161 };
162
163 /* __DWARF Segment. */
164 static const mach_o_section_name_xlat dwarf_section_names_xlat[] =
165 {
166 { ".debug_frame", "__debug_frame",
167 SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
168 BFD_MACH_O_S_ATTR_DEBUG, 0},
169 { ".debug_info", "__debug_info",
170 SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
171 BFD_MACH_O_S_ATTR_DEBUG, 0},
172 { ".debug_abbrev", "__debug_abbrev",
173 SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
174 BFD_MACH_O_S_ATTR_DEBUG, 0},
175 { ".debug_aranges", "__debug_aranges",
176 SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
177 BFD_MACH_O_S_ATTR_DEBUG, 0},
178 { ".debug_macinfo", "__debug_macinfo",
179 SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
180 BFD_MACH_O_S_ATTR_DEBUG, 0},
181 { ".debug_line", "__debug_line",
182 SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
183 BFD_MACH_O_S_ATTR_DEBUG, 0},
184 { ".debug_loc", "__debug_loc",
185 SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
186 BFD_MACH_O_S_ATTR_DEBUG, 0},
187 { ".debug_pubnames", "__debug_pubnames",
188 SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
189 BFD_MACH_O_S_ATTR_DEBUG, 0},
190 { ".debug_pubtypes", "__debug_pubtypes",
191 SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
192 BFD_MACH_O_S_ATTR_DEBUG, 0},
193 { ".debug_str", "__debug_str",
194 SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
195 BFD_MACH_O_S_ATTR_DEBUG, 0},
196 { ".debug_ranges", "__debug_ranges",
197 SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
198 BFD_MACH_O_S_ATTR_DEBUG, 0},
199 { ".debug_macro", "__debug_macro",
200 SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
201 BFD_MACH_O_S_ATTR_DEBUG, 0},
202 { ".debug_gdb_scripts", "__debug_gdb_scri",
203 SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
204 BFD_MACH_O_S_ATTR_DEBUG, 0},
205 { NULL, NULL, 0, 0, 0, 0}
206 };
207
208 /* __OBJC Segment. */
209 static const mach_o_section_name_xlat objc_section_names_xlat[] =
210 {
211 { ".objc_class", "__class",
212 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
213 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
214 { ".objc_meta_class", "__meta_class",
215 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
216 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
217 { ".objc_cat_cls_meth", "__cat_cls_meth",
218 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
219 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
220 { ".objc_cat_inst_meth", "__cat_inst_meth",
221 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
222 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
223 { ".objc_protocol", "__protocol",
224 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
225 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
226 { ".objc_string_object", "__string_object",
227 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
228 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
229 { ".objc_cls_meth", "__cls_meth",
230 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
231 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
232 { ".objc_inst_meth", "__inst_meth",
233 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
234 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
235 { ".objc_cls_refs", "__cls_refs",
236 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_LITERAL_POINTERS,
237 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
238 { ".objc_message_refs", "__message_refs",
239 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_LITERAL_POINTERS,
240 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
241 { ".objc_symbols", "__symbols",
242 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
243 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
244 { ".objc_category", "__category",
245 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
246 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
247 { ".objc_class_vars", "__class_vars",
248 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
249 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
250 { ".objc_instance_vars", "__instance_vars",
251 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
252 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
253 { ".objc_module_info", "__module_info",
254 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
255 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
256 { ".objc_selector_strs", "__selector_strs",
257 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_CSTRING_LITERALS,
258 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
259 { ".objc_image_info", "__image_info",
260 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
261 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
262 { ".objc_selector_fixup", "__sel_fixup",
263 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
264 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
265 /* Objc V1 */
266 { ".objc1_class_ext", "__class_ext",
267 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
268 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
269 { ".objc1_property_list", "__property",
270 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
271 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
272 { ".objc1_protocol_ext", "__protocol_ext",
273 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
274 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
275 { NULL, NULL, 0, 0, 0, 0}
276 };
277
278 static const mach_o_segment_name_xlat segsec_names_xlat[] =
279 {
280 { "__TEXT", text_section_names_xlat },
281 { "__DATA", data_section_names_xlat },
282 { "__DWARF", dwarf_section_names_xlat },
283 { "__OBJC", objc_section_names_xlat },
284 { NULL, NULL }
285 };
286
287 static const char dsym_subdir[] = ".dSYM/Contents/Resources/DWARF";
288
289 /* For both cases bfd-name => mach-o name and vice versa, the specific target
290 is checked before the generic. This allows a target (e.g. ppc for cstring)
291 to override the generic definition with a more specific one. */
292
293 /* Fetch the translation from a Mach-O section designation (segment, section)
294 as a bfd short name, if one exists. Otherwise return NULL.
295
296 Allow the segment and section names to be unterminated 16 byte arrays. */
297
298 const mach_o_section_name_xlat *
299 bfd_mach_o_section_data_for_mach_sect (bfd *abfd, const char *segname,
300 const char *sectname)
301 {
302 const struct mach_o_segment_name_xlat *seg;
303 const mach_o_section_name_xlat *sec;
304 bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
305
306 /* First try any target-specific translations defined... */
307 if (bed->segsec_names_xlat)
308 for (seg = bed->segsec_names_xlat; seg->segname; seg++)
309 if (strncmp (seg->segname, segname, BFD_MACH_O_SEGNAME_SIZE) == 0)
310 for (sec = seg->sections; sec->mach_o_name; sec++)
311 if (strncmp (sec->mach_o_name, sectname,
312 BFD_MACH_O_SECTNAME_SIZE) == 0)
313 return sec;
314
315 /* ... and then the Mach-O generic ones. */
316 for (seg = segsec_names_xlat; seg->segname; seg++)
317 if (strncmp (seg->segname, segname, BFD_MACH_O_SEGNAME_SIZE) == 0)
318 for (sec = seg->sections; sec->mach_o_name; sec++)
319 if (strncmp (sec->mach_o_name, sectname,
320 BFD_MACH_O_SECTNAME_SIZE) == 0)
321 return sec;
322
323 return NULL;
324 }
325
326 /* If the bfd_name for this section is a 'canonical' form for which we
327 know the Mach-O data, return the segment name and the data for the
328 Mach-O equivalent. Otherwise return NULL. */
329
330 const mach_o_section_name_xlat *
331 bfd_mach_o_section_data_for_bfd_name (bfd *abfd, const char *bfd_name,
332 const char **segname)
333 {
334 const struct mach_o_segment_name_xlat *seg;
335 const mach_o_section_name_xlat *sec;
336 bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
337 *segname = NULL;
338
339 if (bfd_name[0] != '.')
340 return NULL;
341
342 /* First try any target-specific translations defined... */
343 if (bed->segsec_names_xlat)
344 for (seg = bed->segsec_names_xlat; seg->segname; seg++)
345 for (sec = seg->sections; sec->bfd_name; sec++)
346 if (strcmp (bfd_name, sec->bfd_name) == 0)
347 {
348 *segname = seg->segname;
349 return sec;
350 }
351
352 /* ... and then the Mach-O generic ones. */
353 for (seg = segsec_names_xlat; seg->segname; seg++)
354 for (sec = seg->sections; sec->bfd_name; sec++)
355 if (strcmp (bfd_name, sec->bfd_name) == 0)
356 {
357 *segname = seg->segname;
358 return sec;
359 }
360
361 return NULL;
362 }
363
364 /* Convert Mach-O section name to BFD.
365
366 Try to use standard/canonical names, for which we have tables including
367 default flag settings - which are returned. Otherwise forge a new name
368 in the form "<segmentname>.<sectionname>" this will be prefixed with
369 LC_SEGMENT. if the segment name does not begin with an underscore.
370
371 SEGNAME and SECTNAME are 16 byte arrays (they do not need to be NUL-
372 terminated if the name length is exactly 16 bytes - but must be if the name
373 length is less than 16 characters). */
374
375 void
376 bfd_mach_o_convert_section_name_to_bfd (bfd *abfd, const char *segname,
377 const char *secname, const char **name,
378 flagword *flags)
379 {
380 const mach_o_section_name_xlat *xlat;
381 char *res;
382 size_t len;
383 const char *pfx = "";
384
385 *name = NULL;
386 *flags = SEC_NO_FLAGS;
387
388 /* First search for a canonical name...
389 xlat will be non-null if there is an entry for segname, secname. */
390 xlat = bfd_mach_o_section_data_for_mach_sect (abfd, segname, secname);
391 if (xlat)
392 {
393 len = strlen (xlat->bfd_name);
394 res = bfd_alloc (abfd, len + 1);
395 if (res == NULL)
396 return;
397 memcpy (res, xlat->bfd_name, len + 1);
398 *name = res;
399 *flags = xlat->bfd_flags;
400 return;
401 }
402
403 /* ... else we make up a bfd name from the segment concatenated with the
404 section. */
405
406 len = 16 + 1 + 16 + 1;
407
408 /* Put "LC_SEGMENT." prefix if the segment name is weird (ie doesn't start
409 with an underscore. */
410 if (segname[0] != '_')
411 {
412 static const char seg_pfx[] = "LC_SEGMENT.";
413
414 pfx = seg_pfx;
415 len += sizeof (seg_pfx) - 1;
416 }
417
418 res = bfd_alloc (abfd, len);
419 if (res == NULL)
420 return;
421 snprintf (res, len, "%s%.16s.%.16s", pfx, segname, secname);
422 *name = res;
423 }
424
425 /* Convert a bfd section name to a Mach-O segment + section name.
426
427 If the name is a canonical one for which we have a Darwin match
428 return the translation table - which contains defaults for flags,
429 type, attribute and default alignment data.
430
431 Otherwise, expand the bfd_name (assumed to be in the form
432 "[LC_SEGMENT.]<segmentname>.<sectionname>") and return NULL. */
433
434 static const mach_o_section_name_xlat *
435 bfd_mach_o_convert_section_name_to_mach_o (bfd *abfd ATTRIBUTE_UNUSED,
436 asection *sect,
437 bfd_mach_o_section *section)
438 {
439 const mach_o_section_name_xlat *xlat;
440 const char *name = bfd_section_name (sect);
441 const char *segname;
442 const char *dot;
443 size_t len;
444 size_t seglen;
445 size_t seclen;
446
447 memset (section->segname, 0, BFD_MACH_O_SEGNAME_SIZE + 1);
448 memset (section->sectname, 0, BFD_MACH_O_SECTNAME_SIZE + 1);
449
450 /* See if is a canonical name ... */
451 xlat = bfd_mach_o_section_data_for_bfd_name (abfd, name, &segname);
452 if (xlat)
453 {
454 strcpy (section->segname, segname);
455 strcpy (section->sectname, xlat->mach_o_name);
456 return xlat;
457 }
458
459 /* .. else we convert our constructed one back to Mach-O.
460 Strip LC_SEGMENT. prefix, if present. */
461 if (strncmp (name, "LC_SEGMENT.", 11) == 0)
462 name += 11;
463
464 /* Find a dot. */
465 dot = strchr (name, '.');
466 len = strlen (name);
467
468 /* Try to split name into segment and section names. */
469 if (dot && dot != name)
470 {
471 seglen = dot - name;
472 seclen = len - (dot + 1 - name);
473
474 if (seglen <= BFD_MACH_O_SEGNAME_SIZE
475 && seclen <= BFD_MACH_O_SECTNAME_SIZE)
476 {
477 memcpy (section->segname, name, seglen);
478 section->segname[seglen] = 0;
479 memcpy (section->sectname, dot + 1, seclen);
480 section->sectname[seclen] = 0;
481 return NULL;
482 }
483 }
484
485 /* The segment and section names are both missing - don't make them
486 into dots. */
487 if (dot && dot == name)
488 return NULL;
489
490 /* Just duplicate the name into both segment and section. */
491 if (len > 16)
492 len = 16;
493 memcpy (section->segname, name, len);
494 section->segname[len] = 0;
495 memcpy (section->sectname, name, len);
496 section->sectname[len] = 0;
497 return NULL;
498 }
499
500 /* Return the size of an entry for section SEC.
501 Must be called only for symbol pointer section and symbol stubs
502 sections. */
503
504 unsigned int
505 bfd_mach_o_section_get_entry_size (bfd *abfd, bfd_mach_o_section *sec)
506 {
507 switch (sec->flags & BFD_MACH_O_SECTION_TYPE_MASK)
508 {
509 case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS:
510 case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS:
511 return bfd_mach_o_wide_p (abfd) ? 8 : 4;
512 case BFD_MACH_O_S_SYMBOL_STUBS:
513 return sec->reserved2;
514 default:
515 BFD_FAIL ();
516 return 0;
517 }
518 }
519
520 /* Return the number of indirect symbols for a section.
521 Must be called only for symbol pointer section and symbol stubs
522 sections. */
523
524 unsigned int
525 bfd_mach_o_section_get_nbr_indirect (bfd *abfd, bfd_mach_o_section *sec)
526 {
527 unsigned int elsz;
528
529 /* FIXME: This array is set by the assembler but does not seem to be
530 set anywhere for objcopy. Since bfd_mach_o_build_dysymtab will
531 not fill in output bfd_mach_o_dysymtab_command indirect_syms when
532 this array is NULL we may as well return zero for the size.
533 This is enough to stop objcopy allocating huge amounts of memory
534 for indirect symbols in fuzzed object files. */
535 if (sec->indirect_syms == NULL)
536 return 0;
537
538 elsz = bfd_mach_o_section_get_entry_size (abfd, sec);
539 if (elsz == 0)
540 return 0;
541 else
542 return sec->size / elsz;
543 }
544
545 /* Append command CMD to ABFD. Note that header.ncmds is not updated. */
546
547 static void
548 bfd_mach_o_append_command (bfd *abfd, bfd_mach_o_load_command *cmd)
549 {
550 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
551
552 if (mdata->last_command != NULL)
553 mdata->last_command->next = cmd;
554 else
555 mdata->first_command = cmd;
556 mdata->last_command = cmd;
557 cmd->next = NULL;
558 }
559
560 /* Copy any private info we understand from the input symbol
561 to the output symbol. */
562
563 bool
564 bfd_mach_o_bfd_copy_private_symbol_data (bfd *ibfd ATTRIBUTE_UNUSED,
565 asymbol *isymbol,
566 bfd *obfd ATTRIBUTE_UNUSED,
567 asymbol *osymbol)
568 {
569 bfd_mach_o_asymbol *os, *is;
570
571 os = (bfd_mach_o_asymbol *)osymbol;
572 is = (bfd_mach_o_asymbol *)isymbol;
573 os->n_type = is->n_type;
574 os->n_sect = is->n_sect;
575 os->n_desc = is->n_desc;
576 os->symbol.udata.i = is->symbol.udata.i;
577
578 return true;
579 }
580
581 /* Copy any private info we understand from the input section
582 to the output section. */
583
584 bool
585 bfd_mach_o_bfd_copy_private_section_data (bfd *ibfd, asection *isection,
586 bfd *obfd, asection *osection,
587 struct bfd_link_info *link_info)
588 {
589 if (link_info != NULL
590 || ibfd->xvec->flavour != bfd_target_mach_o_flavour
591 || obfd->xvec->flavour != bfd_target_mach_o_flavour)
592 return true;
593
594 bfd_mach_o_section *os = bfd_mach_o_get_mach_o_section (osection);
595 bfd_mach_o_section *is = bfd_mach_o_get_mach_o_section (isection);
596
597 BFD_ASSERT (is != NULL && os != NULL);
598
599 os->flags = is->flags;
600 os->reserved1 = is->reserved1;
601 os->reserved2 = is->reserved2;
602 os->reserved3 = is->reserved3;
603
604 return true;
605 }
606
607 static const char *
608 cputype (unsigned long value)
609 {
610 switch (value)
611 {
612 case BFD_MACH_O_CPU_TYPE_VAX: return "VAX";
613 case BFD_MACH_O_CPU_TYPE_MC680x0: return "MC68k";
614 case BFD_MACH_O_CPU_TYPE_I386: return "I386";
615 case BFD_MACH_O_CPU_TYPE_MIPS: return "MIPS";
616 case BFD_MACH_O_CPU_TYPE_MC98000: return "MC98k";
617 case BFD_MACH_O_CPU_TYPE_HPPA: return "HPPA";
618 case BFD_MACH_O_CPU_TYPE_ARM: return "ARM";
619 case BFD_MACH_O_CPU_TYPE_MC88000: return "MC88K";
620 case BFD_MACH_O_CPU_TYPE_SPARC: return "SPARC";
621 case BFD_MACH_O_CPU_TYPE_I860: return "I860";
622 case BFD_MACH_O_CPU_TYPE_ALPHA: return "ALPHA";
623 case BFD_MACH_O_CPU_TYPE_POWERPC: return "PPC";
624 case BFD_MACH_O_CPU_TYPE_POWERPC_64: return "PPC64";
625 case BFD_MACH_O_CPU_TYPE_X86_64: return "X86_64";
626 case BFD_MACH_O_CPU_TYPE_ARM64: return "ARM64";
627 default: return _("<unknown>");
628 }
629 }
630
631 static const char *
632 cpusubtype (unsigned long cpu_type, unsigned long cpu_subtype, char *buffer)
633 {
634 buffer[0] = 0;
635 switch (cpu_subtype & BFD_MACH_O_CPU_SUBTYPE_MASK)
636 {
637 case 0:
638 break;
639 case BFD_MACH_O_CPU_SUBTYPE_LIB64:
640 sprintf (buffer, " (LIB64)"); break;
641 default:
642 sprintf (buffer, _("<unknown mask flags>")); break;
643 }
644
645 cpu_subtype &= ~ BFD_MACH_O_CPU_SUBTYPE_MASK;
646
647 switch (cpu_type)
648 {
649 case BFD_MACH_O_CPU_TYPE_X86_64:
650 case BFD_MACH_O_CPU_TYPE_I386:
651 switch (cpu_subtype)
652 {
653 case BFD_MACH_O_CPU_SUBTYPE_X86_ALL:
654 return strcat (buffer, " (X86_ALL)");
655 default:
656 break;
657 }
658 break;
659
660 case BFD_MACH_O_CPU_TYPE_ARM:
661 switch (cpu_subtype)
662 {
663 case BFD_MACH_O_CPU_SUBTYPE_ARM_ALL:
664 return strcat (buffer, " (ARM_ALL)");
665 case BFD_MACH_O_CPU_SUBTYPE_ARM_V4T:
666 return strcat (buffer, " (ARM_V4T)");
667 case BFD_MACH_O_CPU_SUBTYPE_ARM_V6:
668 return strcat (buffer, " (ARM_V6)");
669 case BFD_MACH_O_CPU_SUBTYPE_ARM_V5TEJ:
670 return strcat (buffer, " (ARM_V5TEJ)");
671 case BFD_MACH_O_CPU_SUBTYPE_ARM_XSCALE:
672 return strcat (buffer, " (ARM_XSCALE)");
673 case BFD_MACH_O_CPU_SUBTYPE_ARM_V7:
674 return strcat (buffer, " (ARM_V7)");
675 default:
676 break;
677 }
678 break;
679
680 case BFD_MACH_O_CPU_TYPE_ARM64:
681 switch (cpu_subtype)
682 {
683 case BFD_MACH_O_CPU_SUBTYPE_ARM64_ALL:
684 return strcat (buffer, " (ARM64_ALL)");
685 case BFD_MACH_O_CPU_SUBTYPE_ARM64_V8:
686 return strcat (buffer, " (ARM64_V8)");
687 default:
688 break;
689 }
690 break;
691
692 default:
693 break;
694 }
695
696 if (cpu_subtype != 0)
697 return strcat (buffer, _(" (<unknown>)"));
698
699 return buffer;
700 }
701
702 bool
703 bfd_mach_o_bfd_print_private_bfd_data (bfd *abfd, void *ptr)
704 {
705 FILE * file = (FILE *) ptr;
706 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
707 char buff[128];
708
709 fprintf (file, _(" MACH-O header:\n"));
710 fprintf (file, _(" magic: %#lx\n"), (long) mdata->header.magic);
711 fprintf (file, _(" cputype: %#lx (%s)\n"), (long) mdata->header.cputype,
712 cputype (mdata->header.cputype));
713 fprintf (file, _(" cpusubtype: %#lx%s\n"), (long) mdata->header.cpusubtype,
714 cpusubtype (mdata->header.cputype, mdata->header.cpusubtype, buff));
715 fprintf (file, _(" filetype: %#lx\n"), (long) mdata->header.filetype);
716 fprintf (file, _(" ncmds: %#lx\n"), (long) mdata->header.ncmds);
717 fprintf (file, _(" sizeocmds: %#lx\n"), (long) mdata->header.sizeofcmds);
718 fprintf (file, _(" flags: %#lx\n"), (long) mdata->header.flags);
719 fprintf (file, _(" version: %x\n"), mdata->header.version);
720
721 return true;
722 }
723
724 /* Copy any private info we understand from the input bfd
725 to the output bfd. */
726
727 bool
728 bfd_mach_o_bfd_copy_private_header_data (bfd *ibfd, bfd *obfd)
729 {
730 bfd_mach_o_data_struct *imdata;
731 bfd_mach_o_data_struct *omdata;
732 bfd_mach_o_load_command *icmd;
733
734 if (bfd_get_flavour (ibfd) != bfd_target_mach_o_flavour
735 || bfd_get_flavour (obfd) != bfd_target_mach_o_flavour)
736 return true;
737
738 BFD_ASSERT (bfd_mach_o_valid (ibfd));
739 BFD_ASSERT (bfd_mach_o_valid (obfd));
740
741 imdata = bfd_mach_o_get_data (ibfd);
742 omdata = bfd_mach_o_get_data (obfd);
743
744 /* Copy header flags. */
745 omdata->header.flags = imdata->header.flags;
746
747 /* PR 23299. Copy the cputype. */
748 if (imdata->header.cputype != omdata->header.cputype)
749 {
750 if (omdata->header.cputype == 0)
751 omdata->header.cputype = imdata->header.cputype;
752 else if (imdata->header.cputype != 0)
753 /* Urg - what has happened ? */
754 _bfd_error_handler (_("incompatible cputypes in mach-o files: %ld vs %ld"),
755 (long) imdata->header.cputype,
756 (long) omdata->header.cputype);
757 }
758
759 /* Copy the cpusubtype. */
760 omdata->header.cpusubtype = imdata->header.cpusubtype;
761
762 /* Copy commands. */
763 for (icmd = imdata->first_command; icmd != NULL; icmd = icmd->next)
764 {
765 bfd_mach_o_load_command *ocmd;
766
767 switch (icmd->type)
768 {
769 case BFD_MACH_O_LC_LOAD_DYLIB:
770 case BFD_MACH_O_LC_LOAD_DYLINKER:
771 case BFD_MACH_O_LC_DYLD_INFO:
772 /* Command is copied. */
773 ocmd = bfd_alloc (obfd, sizeof (bfd_mach_o_load_command));
774 if (ocmd == NULL)
775 return false;
776
777 /* Copy common fields. */
778 ocmd->type = icmd->type;
779 ocmd->type_required = icmd->type_required;
780 ocmd->offset = 0;
781 ocmd->len = icmd->len;
782 break;
783
784 default:
785 /* Command is not copied. */
786 continue;
787 break;
788 }
789
790 switch (icmd->type)
791 {
792 case BFD_MACH_O_LC_LOAD_DYLIB:
793 {
794 bfd_mach_o_dylib_command *idy = &icmd->command.dylib;
795 bfd_mach_o_dylib_command *ody = &ocmd->command.dylib;
796
797 ody->name_offset = idy->name_offset;
798 ody->timestamp = idy->timestamp;
799 ody->current_version = idy->current_version;
800 ody->compatibility_version = idy->compatibility_version;
801 ody->name_str = idy->name_str;
802 }
803 break;
804
805 case BFD_MACH_O_LC_LOAD_DYLINKER:
806 {
807 bfd_mach_o_dylinker_command *idy = &icmd->command.dylinker;
808 bfd_mach_o_dylinker_command *ody = &ocmd->command.dylinker;
809
810 ody->name_offset = idy->name_offset;
811 ody->name_str = idy->name_str;
812 }
813 break;
814
815 case BFD_MACH_O_LC_DYLD_INFO:
816 {
817 bfd_mach_o_dyld_info_command *idy = &icmd->command.dyld_info;
818 bfd_mach_o_dyld_info_command *ody = &ocmd->command.dyld_info;
819
820 if (bfd_mach_o_read_dyld_content (ibfd, idy))
821 {
822 ody->rebase_size = idy->rebase_size;
823 ody->rebase_content = idy->rebase_content;
824
825 ody->bind_size = idy->bind_size;
826 ody->bind_content = idy->bind_content;
827
828 ody->weak_bind_size = idy->weak_bind_size;
829 ody->weak_bind_content = idy->weak_bind_content;
830
831 ody->lazy_bind_size = idy->lazy_bind_size;
832 ody->lazy_bind_content = idy->lazy_bind_content;
833
834 ody->export_size = idy->export_size;
835 ody->export_content = idy->export_content;
836 }
837 /* PR 17512L: file: 730e492d. */
838 else
839 {
840 ody->rebase_size =
841 ody->bind_size =
842 ody->weak_bind_size =
843 ody->lazy_bind_size =
844 ody->export_size = 0;
845 ody->rebase_content =
846 ody->bind_content =
847 ody->weak_bind_content =
848 ody->lazy_bind_content =
849 ody->export_content = NULL;
850 }
851 }
852 break;
853
854 default:
855 /* That command should be handled. */
856 abort ();
857 }
858
859 /* Insert command. */
860 bfd_mach_o_append_command (obfd, ocmd);
861 }
862
863 return true;
864 }
865
866 /* This allows us to set up to 32 bits of flags (unless we invent some
867 fiendish scheme to subdivide). For now, we'll just set the file flags
868 without error checking - just overwrite. */
869
870 bool
871 bfd_mach_o_bfd_set_private_flags (bfd *abfd, flagword flags)
872 {
873 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
874
875 if (!mdata)
876 return false;
877
878 mdata->header.flags = flags;
879 return true;
880 }
881
882 /* Count the total number of symbols. */
883
884 static long
885 bfd_mach_o_count_symbols (bfd *abfd)
886 {
887 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
888
889 if (mdata->symtab == NULL)
890 return 0;
891 return mdata->symtab->nsyms;
892 }
893
894 long
895 bfd_mach_o_get_symtab_upper_bound (bfd *abfd)
896 {
897 long nsyms = bfd_mach_o_count_symbols (abfd);
898
899 return ((nsyms + 1) * sizeof (asymbol *));
900 }
901
902 long
903 bfd_mach_o_canonicalize_symtab (bfd *abfd, asymbol **alocation)
904 {
905 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
906 long nsyms = bfd_mach_o_count_symbols (abfd);
907 bfd_mach_o_symtab_command *sym = mdata->symtab;
908 unsigned long j;
909
910 if (nsyms < 0)
911 return nsyms;
912
913 if (nsyms == 0)
914 {
915 /* Do not try to read symbols if there are none. */
916 alocation[0] = NULL;
917 return 0;
918 }
919
920 if (!bfd_mach_o_read_symtab_symbols (abfd))
921 {
922 _bfd_error_handler
923 (_("bfd_mach_o_canonicalize_symtab: unable to load symbols"));
924 return -1;
925 }
926
927 BFD_ASSERT (sym->symbols != NULL);
928
929 for (j = 0; j < sym->nsyms; j++)
930 alocation[j] = &sym->symbols[j].symbol;
931
932 alocation[j] = NULL;
933
934 return nsyms;
935 }
936
937 /* Create synthetic symbols for indirect symbols. */
938
939 long
940 bfd_mach_o_get_synthetic_symtab (bfd *abfd,
941 long symcount ATTRIBUTE_UNUSED,
942 asymbol **syms ATTRIBUTE_UNUSED,
943 long dynsymcount ATTRIBUTE_UNUSED,
944 asymbol **dynsyms ATTRIBUTE_UNUSED,
945 asymbol **ret)
946 {
947 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
948 bfd_mach_o_dysymtab_command *dysymtab = mdata->dysymtab;
949 bfd_mach_o_symtab_command *symtab = mdata->symtab;
950 asymbol *s;
951 char * s_start;
952 unsigned long count, i, j, n;
953 size_t size;
954 char *names;
955 const char stub [] = "$stub";
956
957 *ret = NULL;
958
959 /* Stop now if no symbols or no indirect symbols. */
960 if (dysymtab == NULL || dysymtab->nindirectsyms == 0
961 || symtab == NULL || symtab->symbols == NULL)
962 return 0;
963
964 /* We need to allocate a bfd symbol for every indirect symbol and to
965 allocate the memory for its name. */
966 count = dysymtab->nindirectsyms;
967 size = 0;
968 for (j = 0; j < count; j++)
969 {
970 unsigned int isym = dysymtab->indirect_syms[j];
971 const char *str;
972
973 /* Some indirect symbols are anonymous. */
974 if (isym < symtab->nsyms
975 && (str = symtab->symbols[isym].symbol.name) != NULL)
976 {
977 /* PR 17512: file: f5b8eeba. */
978 size += strnlen (str, symtab->strsize - (str - symtab->strtab));
979 size += sizeof (stub);
980 }
981 }
982
983 s_start = bfd_malloc (size + count * sizeof (asymbol));
984 s = *ret = (asymbol *) s_start;
985 if (s == NULL)
986 return -1;
987 names = (char *) (s + count);
988
989 n = 0;
990 for (i = 0; i < mdata->nsects; i++)
991 {
992 bfd_mach_o_section *sec = mdata->sections[i];
993 unsigned int first, last;
994 bfd_vma addr;
995 unsigned int entry_size;
996
997 switch (sec->flags & BFD_MACH_O_SECTION_TYPE_MASK)
998 {
999 case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS:
1000 case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS:
1001 case BFD_MACH_O_S_SYMBOL_STUBS:
1002 /* Only these sections have indirect symbols. */
1003 first = sec->reserved1;
1004 last = first + bfd_mach_o_section_get_nbr_indirect (abfd, sec);
1005 addr = sec->addr;
1006 entry_size = bfd_mach_o_section_get_entry_size (abfd, sec);
1007
1008 /* PR 17512: file: 08e15eec. */
1009 if (first >= count || last > count || first > last)
1010 goto fail;
1011
1012 for (j = first; j < last; j++)
1013 {
1014 unsigned int isym = dysymtab->indirect_syms[j];
1015 const char *str;
1016 size_t len;
1017
1018 if (isym < symtab->nsyms
1019 && (str = symtab->symbols[isym].symbol.name) != NULL)
1020 {
1021 /* PR 17512: file: 04d64d9b. */
1022 if (n >= count)
1023 goto fail;
1024 len = strnlen (str, symtab->strsize - (str - symtab->strtab));
1025 /* PR 17512: file: 47dfd4d2, 18f340a4. */
1026 if (size < len + sizeof (stub))
1027 goto fail;
1028 memcpy (names, str, len);
1029 memcpy (names + len, stub, sizeof (stub));
1030 s->name = names;
1031 names += len + sizeof (stub);
1032 size -= len + sizeof (stub);
1033 s->the_bfd = symtab->symbols[isym].symbol.the_bfd;
1034 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
1035 s->section = sec->bfdsection;
1036 s->value = addr - sec->addr;
1037 s->udata.p = NULL;
1038 s++;
1039 n++;
1040 }
1041 addr += entry_size;
1042 }
1043 break;
1044 default:
1045 break;
1046 }
1047 }
1048
1049 return n;
1050
1051 fail:
1052 free (s_start);
1053 * ret = NULL;
1054 return -1;
1055 }
1056
1057 void
1058 bfd_mach_o_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
1059 asymbol *symbol,
1060 symbol_info *ret)
1061 {
1062 bfd_symbol_info (symbol, ret);
1063 }
1064
1065 void
1066 bfd_mach_o_print_symbol (bfd *abfd,
1067 void * afile,
1068 asymbol *symbol,
1069 bfd_print_symbol_type how)
1070 {
1071 FILE *file = (FILE *) afile;
1072 const char *name;
1073 bfd_mach_o_asymbol *asym = (bfd_mach_o_asymbol *)symbol;
1074
1075 switch (how)
1076 {
1077 case bfd_print_symbol_name:
1078 fprintf (file, "%s", symbol->name);
1079 break;
1080 default:
1081 bfd_print_symbol_vandf (abfd, (void *) file, symbol);
1082 if (asym->n_type & BFD_MACH_O_N_STAB)
1083 name = bfd_get_stab_name (asym->n_type);
1084 else
1085 switch (asym->n_type & BFD_MACH_O_N_TYPE)
1086 {
1087 case BFD_MACH_O_N_UNDF:
1088 if (symbol->value == 0)
1089 name = "UND";
1090 else
1091 name = "COM";
1092 break;
1093 case BFD_MACH_O_N_ABS:
1094 name = "ABS";
1095 break;
1096 case BFD_MACH_O_N_INDR:
1097 name = "INDR";
1098 break;
1099 case BFD_MACH_O_N_PBUD:
1100 name = "PBUD";
1101 break;
1102 case BFD_MACH_O_N_SECT:
1103 name = "SECT";
1104 break;
1105 default:
1106 name = "???";
1107 break;
1108 }
1109 if (name == NULL)
1110 name = "";
1111 fprintf (file, " %02x %-6s %02x %04x",
1112 asym->n_type, name, asym->n_sect, asym->n_desc);
1113 if ((asym->n_type & BFD_MACH_O_N_STAB) == 0
1114 && (asym->n_type & BFD_MACH_O_N_TYPE) == BFD_MACH_O_N_SECT)
1115 fprintf (file, " [%s]", symbol->section->name);
1116 fprintf (file, " %s", symbol->name);
1117 }
1118 }
1119
1120 static void
1121 bfd_mach_o_convert_architecture (bfd_mach_o_cpu_type mtype,
1122 bfd_mach_o_cpu_subtype msubtype,
1123 enum bfd_architecture *type,
1124 unsigned long *subtype)
1125 {
1126 *subtype = bfd_arch_unknown;
1127
1128 switch (mtype)
1129 {
1130 case BFD_MACH_O_CPU_TYPE_VAX:
1131 *type = bfd_arch_vax;
1132 break;
1133 case BFD_MACH_O_CPU_TYPE_MC680x0:
1134 *type = bfd_arch_m68k;
1135 break;
1136 case BFD_MACH_O_CPU_TYPE_I386:
1137 *type = bfd_arch_i386;
1138 *subtype = bfd_mach_i386_i386;
1139 break;
1140 case BFD_MACH_O_CPU_TYPE_X86_64:
1141 *type = bfd_arch_i386;
1142 *subtype = bfd_mach_x86_64;
1143 break;
1144 case BFD_MACH_O_CPU_TYPE_MIPS:
1145 *type = bfd_arch_mips;
1146 break;
1147 case BFD_MACH_O_CPU_TYPE_MC98000:
1148 *type = bfd_arch_m98k;
1149 break;
1150 case BFD_MACH_O_CPU_TYPE_HPPA:
1151 *type = bfd_arch_hppa;
1152 break;
1153 case BFD_MACH_O_CPU_TYPE_ARM:
1154 *type = bfd_arch_arm;
1155 switch (msubtype)
1156 {
1157 case BFD_MACH_O_CPU_SUBTYPE_ARM_V4T:
1158 *subtype = bfd_mach_arm_4T;
1159 break;
1160 case BFD_MACH_O_CPU_SUBTYPE_ARM_V6:
1161 *subtype = bfd_mach_arm_4T; /* Best fit ? */
1162 break;
1163 case BFD_MACH_O_CPU_SUBTYPE_ARM_V5TEJ:
1164 *subtype = bfd_mach_arm_5TE;
1165 break;
1166 case BFD_MACH_O_CPU_SUBTYPE_ARM_XSCALE:
1167 *subtype = bfd_mach_arm_XScale;
1168 break;
1169 case BFD_MACH_O_CPU_SUBTYPE_ARM_V7:
1170 *subtype = bfd_mach_arm_5TE; /* Best fit ? */
1171 break;
1172 case BFD_MACH_O_CPU_SUBTYPE_ARM_ALL:
1173 default:
1174 break;
1175 }
1176 break;
1177 case BFD_MACH_O_CPU_TYPE_SPARC:
1178 *type = bfd_arch_sparc;
1179 *subtype = bfd_mach_sparc;
1180 break;
1181 case BFD_MACH_O_CPU_TYPE_ALPHA:
1182 *type = bfd_arch_alpha;
1183 break;
1184 case BFD_MACH_O_CPU_TYPE_POWERPC:
1185 *type = bfd_arch_powerpc;
1186 *subtype = bfd_mach_ppc;
1187 break;
1188 case BFD_MACH_O_CPU_TYPE_POWERPC_64:
1189 *type = bfd_arch_powerpc;
1190 *subtype = bfd_mach_ppc64;
1191 break;
1192 case BFD_MACH_O_CPU_TYPE_ARM64:
1193 *type = bfd_arch_aarch64;
1194 *subtype = bfd_mach_aarch64;
1195 break;
1196 default:
1197 *type = bfd_arch_unknown;
1198 break;
1199 }
1200 }
1201
1202 /* Write n NUL bytes to ABFD so that LEN + n is a multiple of 4. Return the
1203 number of bytes written or -1 in case of error. */
1204
1205 static int
1206 bfd_mach_o_pad4 (bfd *abfd, size_t len)
1207 {
1208 if (len % 4 != 0)
1209 {
1210 char pad[4] = {0,0,0,0};
1211 unsigned int padlen = 4 - (len % 4);
1212
1213 if (bfd_write (pad, padlen, abfd) != padlen)
1214 return -1;
1215
1216 return padlen;
1217 }
1218 else
1219 return 0;
1220 }
1221
1222 /* Likewise, but for a command. */
1223
1224 static int
1225 bfd_mach_o_pad_command (bfd *abfd, size_t len)
1226 {
1227 size_t align = bfd_mach_o_wide_p (abfd) ? 8 : 4;
1228
1229 if (len % align != 0)
1230 {
1231 char pad[8] = {0};
1232 size_t padlen = align - (len % align);
1233
1234 if (bfd_write (pad, padlen, abfd) != padlen)
1235 return -1;
1236
1237 return padlen;
1238 }
1239 else
1240 return 0;
1241 }
1242
1243 static bool
1244 bfd_mach_o_write_header (bfd *abfd, bfd_mach_o_header *header)
1245 {
1246 struct mach_o_header_external raw;
1247 size_t size;
1248
1249 size = mach_o_wide_p (header) ?
1250 BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE;
1251
1252 bfd_h_put_32 (abfd, header->magic, raw.magic);
1253 bfd_h_put_32 (abfd, header->cputype, raw.cputype);
1254 bfd_h_put_32 (abfd, header->cpusubtype, raw.cpusubtype);
1255 bfd_h_put_32 (abfd, header->filetype, raw.filetype);
1256 bfd_h_put_32 (abfd, header->ncmds, raw.ncmds);
1257 bfd_h_put_32 (abfd, header->sizeofcmds, raw.sizeofcmds);
1258 bfd_h_put_32 (abfd, header->flags, raw.flags);
1259
1260 if (mach_o_wide_p (header))
1261 bfd_h_put_32 (abfd, header->reserved, raw.reserved);
1262
1263 if (bfd_seek (abfd, 0, SEEK_SET) != 0
1264 || bfd_write (&raw, size, abfd) != size)
1265 return false;
1266
1267 return true;
1268 }
1269
1270 static bool
1271 bfd_mach_o_write_thread (bfd *abfd, bfd_mach_o_load_command *command)
1272 {
1273 bfd_mach_o_thread_command *cmd = &command->command.thread;
1274 unsigned int i;
1275 struct mach_o_thread_command_external raw;
1276 size_t offset;
1277
1278 BFD_ASSERT ((command->type == BFD_MACH_O_LC_THREAD)
1279 || (command->type == BFD_MACH_O_LC_UNIXTHREAD));
1280
1281 offset = BFD_MACH_O_LC_SIZE;
1282 for (i = 0; i < cmd->nflavours; i++)
1283 {
1284 BFD_ASSERT ((cmd->flavours[i].size % 4) == 0);
1285 BFD_ASSERT (cmd->flavours[i].offset
1286 == command->offset + offset + BFD_MACH_O_LC_SIZE);
1287
1288 bfd_h_put_32 (abfd, cmd->flavours[i].flavour, raw.flavour);
1289 bfd_h_put_32 (abfd, (cmd->flavours[i].size / 4), raw.count);
1290
1291 if (bfd_seek (abfd, command->offset + offset, SEEK_SET) != 0
1292 || bfd_write (&raw, sizeof (raw), abfd) != sizeof (raw))
1293 return false;
1294
1295 offset += cmd->flavours[i].size + sizeof (raw);
1296 }
1297
1298 return true;
1299 }
1300
1301 static bool
1302 bfd_mach_o_write_dylinker (bfd *abfd, bfd_mach_o_load_command *command)
1303 {
1304 bfd_mach_o_dylinker_command *cmd = &command->command.dylinker;
1305 struct mach_o_str_command_external raw;
1306 size_t namelen;
1307
1308 bfd_h_put_32 (abfd, cmd->name_offset, raw.str);
1309
1310 if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
1311 || bfd_write (&raw, sizeof (raw), abfd) != sizeof (raw))
1312 return false;
1313
1314 namelen = strlen (cmd->name_str) + 1;
1315 if (bfd_write (cmd->name_str, namelen, abfd) != namelen)
1316 return false;
1317
1318 if (bfd_mach_o_pad_command (abfd, namelen) < 0)
1319 return false;
1320
1321 return true;
1322 }
1323
1324 static bool
1325 bfd_mach_o_write_dylib (bfd *abfd, bfd_mach_o_load_command *command)
1326 {
1327 bfd_mach_o_dylib_command *cmd = &command->command.dylib;
1328 struct mach_o_dylib_command_external raw;
1329 size_t namelen;
1330
1331 bfd_h_put_32 (abfd, cmd->name_offset, raw.name);
1332 bfd_h_put_32 (abfd, cmd->timestamp, raw.timestamp);
1333 bfd_h_put_32 (abfd, cmd->current_version, raw.current_version);
1334 bfd_h_put_32 (abfd, cmd->compatibility_version, raw.compatibility_version);
1335
1336 if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
1337 || bfd_write (&raw, sizeof (raw), abfd) != sizeof (raw))
1338 return false;
1339
1340 namelen = strlen (cmd->name_str) + 1;
1341 if (bfd_write (cmd->name_str, namelen, abfd) != namelen)
1342 return false;
1343
1344 if (bfd_mach_o_pad_command (abfd, namelen) < 0)
1345 return false;
1346
1347 return true;
1348 }
1349
1350 static bool
1351 bfd_mach_o_write_main (bfd *abfd, bfd_mach_o_load_command *command)
1352 {
1353 bfd_mach_o_main_command *cmd = &command->command.main;
1354 struct mach_o_entry_point_command_external raw;
1355
1356 bfd_h_put_64 (abfd, cmd->entryoff, raw.entryoff);
1357 bfd_h_put_64 (abfd, cmd->stacksize, raw.stacksize);
1358
1359 if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
1360 || bfd_write (&raw, sizeof (raw), abfd) != sizeof (raw))
1361 return false;
1362
1363 return true;
1364 }
1365
1366 static bool
1367 bfd_mach_o_write_dyld_info (bfd *abfd, bfd_mach_o_load_command *command)
1368 {
1369 bfd_mach_o_dyld_info_command *cmd = &command->command.dyld_info;
1370 struct mach_o_dyld_info_command_external raw;
1371
1372 bfd_h_put_32 (abfd, cmd->rebase_off, raw.rebase_off);
1373 bfd_h_put_32 (abfd, cmd->rebase_size, raw.rebase_size);
1374 bfd_h_put_32 (abfd, cmd->bind_off, raw.bind_off);
1375 bfd_h_put_32 (abfd, cmd->bind_size, raw.bind_size);
1376 bfd_h_put_32 (abfd, cmd->weak_bind_off, raw.weak_bind_off);
1377 bfd_h_put_32 (abfd, cmd->weak_bind_size, raw.weak_bind_size);
1378 bfd_h_put_32 (abfd, cmd->lazy_bind_off, raw.lazy_bind_off);
1379 bfd_h_put_32 (abfd, cmd->lazy_bind_size, raw.lazy_bind_size);
1380 bfd_h_put_32 (abfd, cmd->export_off, raw.export_off);
1381 bfd_h_put_32 (abfd, cmd->export_size, raw.export_size);
1382
1383 if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
1384 || bfd_write (&raw, sizeof (raw), abfd) != sizeof (raw))
1385 return false;
1386
1387 if (cmd->rebase_size != 0)
1388 if (bfd_seek (abfd, cmd->rebase_off, SEEK_SET) != 0
1389 || (bfd_write (cmd->rebase_content, cmd->rebase_size, abfd) !=
1390 cmd->rebase_size))
1391 return false;
1392
1393 if (cmd->bind_size != 0)
1394 if (bfd_seek (abfd, cmd->bind_off, SEEK_SET) != 0
1395 || (bfd_write (cmd->bind_content, cmd->bind_size, abfd) !=
1396 cmd->bind_size))
1397 return false;
1398
1399 if (cmd->weak_bind_size != 0)
1400 if (bfd_seek (abfd, cmd->weak_bind_off, SEEK_SET) != 0
1401 || (bfd_write (cmd->weak_bind_content, cmd->weak_bind_size, abfd) !=
1402 cmd->weak_bind_size))
1403 return false;
1404
1405 if (cmd->lazy_bind_size != 0)
1406 if (bfd_seek (abfd, cmd->lazy_bind_off, SEEK_SET) != 0
1407 || (bfd_write (cmd->lazy_bind_content, cmd->lazy_bind_size, abfd) !=
1408 cmd->lazy_bind_size))
1409 return false;
1410
1411 if (cmd->export_size != 0)
1412 if (bfd_seek (abfd, cmd->export_off, SEEK_SET) != 0
1413 || (bfd_write (cmd->export_content, cmd->export_size, abfd) !=
1414 cmd->export_size))
1415 return false;
1416
1417 return true;
1418 }
1419
1420 long
1421 bfd_mach_o_get_reloc_upper_bound (bfd *abfd, asection *asect)
1422 {
1423 size_t count, raw;
1424
1425 count = asect->reloc_count;
1426 if (count >= LONG_MAX / sizeof (arelent *)
1427 || _bfd_mul_overflow (count, BFD_MACH_O_RELENT_SIZE, &raw))
1428 {
1429 bfd_set_error (bfd_error_file_too_big);
1430 return -1;
1431 }
1432 if (!bfd_write_p (abfd))
1433 {
1434 ufile_ptr filesize = bfd_get_file_size (abfd);
1435 if (filesize != 0 && raw > filesize)
1436 {
1437 bfd_set_error (bfd_error_file_truncated);
1438 return -1;
1439 }
1440 }
1441 return (count + 1) * sizeof (arelent *);
1442 }
1443
1444 /* In addition to the need to byte-swap the symbol number, the bit positions
1445 of the fields in the relocation information vary per target endian-ness. */
1446
1447 void
1448 bfd_mach_o_swap_in_non_scattered_reloc (bfd *abfd, bfd_mach_o_reloc_info *rel,
1449 unsigned char *fields)
1450 {
1451 unsigned char info = fields[3];
1452
1453 if (bfd_big_endian (abfd))
1454 {
1455 rel->r_value = (fields[0] << 16) | (fields[1] << 8) | fields[2];
1456 rel->r_type = (info >> BFD_MACH_O_BE_TYPE_SHIFT) & BFD_MACH_O_TYPE_MASK;
1457 rel->r_pcrel = (info & BFD_MACH_O_BE_PCREL) ? 1 : 0;
1458 rel->r_length = (info >> BFD_MACH_O_BE_LENGTH_SHIFT)
1459 & BFD_MACH_O_LENGTH_MASK;
1460 rel->r_extern = (info & BFD_MACH_O_BE_EXTERN) ? 1 : 0;
1461 }
1462 else
1463 {
1464 rel->r_value = (fields[2] << 16) | (fields[1] << 8) | fields[0];
1465 rel->r_type = (info >> BFD_MACH_O_LE_TYPE_SHIFT) & BFD_MACH_O_TYPE_MASK;
1466 rel->r_pcrel = (info & BFD_MACH_O_LE_PCREL) ? 1 : 0;
1467 rel->r_length = (info >> BFD_MACH_O_LE_LENGTH_SHIFT)
1468 & BFD_MACH_O_LENGTH_MASK;
1469 rel->r_extern = (info & BFD_MACH_O_LE_EXTERN) ? 1 : 0;
1470 }
1471 }
1472
1473 /* Set syms_ptr_ptr and addend of RES. */
1474
1475 bool
1476 bfd_mach_o_canonicalize_non_scattered_reloc (bfd *abfd,
1477 bfd_mach_o_reloc_info *reloc,
1478 arelent *res, asymbol **syms)
1479 {
1480 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
1481 unsigned int num;
1482 asymbol **sym;
1483
1484 /* Non-scattered relocation. */
1485 reloc->r_scattered = 0;
1486 res->addend = 0;
1487
1488 num = reloc->r_value;
1489
1490 if (reloc->r_extern)
1491 {
1492 /* PR 17512: file: 8396-1185-0.004. */
1493 if (num >= (unsigned) bfd_mach_o_count_symbols (abfd))
1494 sym = &bfd_und_section_ptr->symbol;
1495 else if (syms == NULL)
1496 sym = &bfd_und_section_ptr->symbol;
1497 else
1498 /* An external symbol number. */
1499 sym = syms + num;
1500 }
1501 else if (num == 0x00ffffff || num == 0)
1502 {
1503 /* The 'symnum' in a non-scattered PAIR is 0x00ffffff. But as this
1504 is generic code, we don't know wether this is really a PAIR.
1505 This value is almost certainly not a valid section number, hence
1506 this specific case to avoid an assertion failure.
1507 Target specific swap_reloc_in routine should adjust that. */
1508 sym = &bfd_abs_section_ptr->symbol;
1509 }
1510 else
1511 {
1512 /* PR 17512: file: 006-2964-0.004. */
1513 if (num > mdata->nsects)
1514 {
1515 _bfd_error_handler (_("\
1516 malformed mach-o reloc: section index is greater than the number of sections"));
1517 return false;
1518 }
1519
1520 /* A section number. */
1521 sym = &mdata->sections[num - 1]->bfdsection->symbol;
1522 /* For a symbol defined in section S, the addend (stored in the
1523 binary) contains the address of the section. To comply with
1524 bfd convention, subtract the section address.
1525 Use the address from the header, so that the user can modify
1526 the vma of the section. */
1527 res->addend = -mdata->sections[num - 1]->addr;
1528 }
1529
1530 /* Note: Pairs for PPC LO/HI/HA are not scattered, but contain the offset
1531 in the lower 16bits of the address value. So we have to find the
1532 'symbol' from the preceding reloc. We do this even though the
1533 section symbol is probably not needed here, because NULL symbol
1534 values cause an assert in generic BFD code. This must be done in
1535 the PPC swap_reloc_in routine. */
1536 res->sym_ptr_ptr = sym;
1537
1538 return true;
1539 }
1540
1541 /* Do most of the work for canonicalize_relocs on RAW: create internal
1542 representation RELOC and set most fields of RES using symbol table SYMS.
1543 Each target still has to set the howto of RES and possibly adjust other
1544 fields.
1545 Previously the Mach-O hook point was simply swap_in, but some targets
1546 (like arm64) don't follow the generic rules (symnum is a value for the
1547 non-scattered relocation ADDEND). */
1548
1549 bool
1550 bfd_mach_o_pre_canonicalize_one_reloc (bfd *abfd,
1551 struct mach_o_reloc_info_external *raw,
1552 bfd_mach_o_reloc_info *reloc,
1553 arelent *res, asymbol **syms)
1554 {
1555 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
1556 bfd_vma addr;
1557
1558 addr = bfd_get_32 (abfd, raw->r_address);
1559 res->sym_ptr_ptr = &bfd_und_section_ptr->symbol;
1560 res->addend = 0;
1561
1562 if (addr & BFD_MACH_O_SR_SCATTERED)
1563 {
1564 unsigned int j;
1565 bfd_vma symnum = bfd_get_32 (abfd, raw->r_symbolnum);
1566
1567 /* Scattered relocation, can't be extern. */
1568 reloc->r_scattered = 1;
1569 reloc->r_extern = 0;
1570
1571 /* Extract section and offset from r_value (symnum). */
1572 reloc->r_value = symnum;
1573 /* FIXME: This breaks when a symbol in a reloc exactly follows the
1574 end of the data for the section (e.g. in a calculation of section
1575 data length). At present, the symbol will end up associated with
1576 the following section or, if it falls within alignment padding, as
1577 the undefined section symbol. */
1578 for (j = 0; j < mdata->nsects; j++)
1579 {
1580 bfd_mach_o_section *sect = mdata->sections[j];
1581 if (symnum >= sect->addr && symnum < sect->addr + sect->size)
1582 {
1583 res->sym_ptr_ptr = §->bfdsection->symbol;
1584 res->addend = symnum - sect->addr;
1585 break;
1586 }
1587 }
1588
1589 /* Extract the info and address fields from r_address. */
1590 reloc->r_type = BFD_MACH_O_GET_SR_TYPE (addr);
1591 reloc->r_length = BFD_MACH_O_GET_SR_LENGTH (addr);
1592 reloc->r_pcrel = addr & BFD_MACH_O_SR_PCREL;
1593 reloc->r_address = BFD_MACH_O_GET_SR_TYPE (addr);
1594 res->address = BFD_MACH_O_GET_SR_ADDRESS (addr);
1595 }
1596 else
1597 {
1598 /* Non-scattered relocation. */
1599 reloc->r_scattered = 0;
1600 reloc->r_address = addr;
1601 res->address = addr;
1602
1603 /* The value and info fields have to be extracted dependent on target
1604 endian-ness. */
1605 bfd_mach_o_swap_in_non_scattered_reloc (abfd, reloc, raw->r_symbolnum);
1606
1607 if (!bfd_mach_o_canonicalize_non_scattered_reloc (abfd, reloc,
1608 res, syms))
1609 return false;
1610 }
1611
1612 /* We have set up a reloc with all the information present, so the swapper
1613 can modify address, value and addend fields, if necessary, to convey
1614 information in the generic BFD reloc that is mach-o specific. */
1615
1616 return true;
1617 }
1618
1619 static int
1620 bfd_mach_o_canonicalize_relocs (bfd *abfd, unsigned long filepos,
1621 unsigned long count,
1622 arelent *res, asymbol **syms)
1623 {
1624 bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
1625 unsigned long i;
1626 struct mach_o_reloc_info_external *native_relocs = NULL;
1627 size_t native_size;
1628
1629 /* Allocate and read relocs. */
1630 if (_bfd_mul_overflow (count, BFD_MACH_O_RELENT_SIZE, &native_size))
1631 /* PR 17512: file: 09477b57. */
1632 goto err;
1633
1634 if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
1635 return -1;
1636 native_relocs = (struct mach_o_reloc_info_external *)
1637 _bfd_malloc_and_read (abfd, native_size, native_size);
1638 if (native_relocs == NULL)
1639 return -1;
1640
1641 for (i = 0; i < count; i++)
1642 {
1643 if (!(*bed->_bfd_mach_o_canonicalize_one_reloc)(abfd, &native_relocs[i],
1644 &res[i], syms, res))
1645 goto err;
1646 }
1647 free (native_relocs);
1648 return i;
1649
1650 err:
1651 free (native_relocs);
1652 if (bfd_get_error () == bfd_error_no_error)
1653 bfd_set_error (bfd_error_invalid_operation);
1654 return -1;
1655 }
1656
1657 long
1658 bfd_mach_o_canonicalize_reloc (bfd *abfd, asection *asect,
1659 arelent **rels, asymbol **syms)
1660 {
1661 bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
1662 unsigned long i;
1663 arelent *res;
1664
1665 if (asect->reloc_count == 0)
1666 return 0;
1667
1668 /* No need to go further if we don't know how to read relocs. */
1669 if (bed->_bfd_mach_o_canonicalize_one_reloc == NULL)
1670 return 0;
1671
1672 if (asect->relocation == NULL)
1673 {
1674 size_t amt;
1675
1676 if (_bfd_mul_overflow (asect->reloc_count, sizeof (arelent), &amt))
1677 {
1678 bfd_set_error (bfd_error_file_too_big);
1679 return -1;
1680 }
1681 res = bfd_malloc (amt);
1682 if (res == NULL)
1683 return -1;
1684
1685 if (bfd_mach_o_canonicalize_relocs (abfd, asect->rel_filepos,
1686 asect->reloc_count, res, syms) < 0)
1687 {
1688 free (res);
1689 return -1;
1690 }
1691 asect->relocation = res;
1692 }
1693
1694 res = asect->relocation;
1695 for (i = 0; i < asect->reloc_count; i++)
1696 rels[i] = &res[i];
1697 rels[i] = NULL;
1698
1699 return i;
1700 }
1701
1702 long
1703 bfd_mach_o_get_dynamic_reloc_upper_bound (bfd *abfd)
1704 {
1705 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
1706 bfd_mach_o_dysymtab_command *dysymtab = mdata->dysymtab;
1707
1708 if (dysymtab == NULL)
1709 return 1;
1710
1711 ufile_ptr filesize = bfd_get_file_size (abfd);
1712 size_t amt;
1713
1714 if (filesize != 0)
1715 {
1716 if (dysymtab->extreloff > filesize
1717 || dysymtab->nextrel > ((filesize - dysymtab->extreloff)
1718 / BFD_MACH_O_RELENT_SIZE)
1719 || dysymtab->locreloff > filesize
1720 || dysymtab->nlocrel > ((filesize - dysymtab->locreloff)
1721 / BFD_MACH_O_RELENT_SIZE))
1722 {
1723 bfd_set_error (bfd_error_file_truncated);
1724 return -1;
1725 }
1726 }
1727 if (dysymtab->nextrel + dysymtab->nlocrel < dysymtab->nextrel
1728 || _bfd_mul_overflow (dysymtab->nextrel + dysymtab->nlocrel,
1729 sizeof (arelent), &amt))
1730 {
1731 bfd_set_error (bfd_error_file_too_big);
1732 return -1;
1733 }
1734
1735 return (dysymtab->nextrel + dysymtab->nlocrel + 1) * sizeof (arelent *);
1736 }
1737
1738 long
1739 bfd_mach_o_canonicalize_dynamic_reloc (bfd *abfd, arelent **rels,
1740 struct bfd_symbol **syms)
1741 {
1742 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
1743 bfd_mach_o_dysymtab_command *dysymtab = mdata->dysymtab;
1744 bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
1745 unsigned long i;
1746 arelent *res;
1747
1748 if (dysymtab == NULL)
1749 return 0;
1750 if (dysymtab->nextrel == 0 && dysymtab->nlocrel == 0)
1751 return 0;
1752
1753 /* No need to go further if we don't know how to read relocs. */
1754 if (bed->_bfd_mach_o_canonicalize_one_reloc == NULL)
1755 return 0;
1756
1757 if (mdata->dyn_reloc_cache == NULL)
1758 {
1759 size_t amt = (dysymtab->nextrel + dysymtab->nlocrel) * sizeof (arelent);
1760 res = bfd_malloc (amt);
1761 if (res == NULL)
1762 return -1;
1763
1764 if (bfd_mach_o_canonicalize_relocs (abfd, dysymtab->extreloff,
1765 dysymtab->nextrel, res, syms) < 0)
1766 {
1767 free (res);
1768 return -1;
1769 }
1770
1771 if (bfd_mach_o_canonicalize_relocs (abfd, dysymtab->locreloff,
1772 dysymtab->nlocrel,
1773 res + dysymtab->nextrel, syms) < 0)
1774 {
1775 free (res);
1776 return -1;
1777 }
1778
1779 mdata->dyn_reloc_cache = res;
1780 }
1781
1782 res = mdata->dyn_reloc_cache;
1783 for (i = 0; i < dysymtab->nextrel + dysymtab->nlocrel; i++)
1784 rels[i] = &res[i];
1785 rels[i] = NULL;
1786 return i;
1787 }
1788
1789 /* In addition to the need to byte-swap the symbol number, the bit positions
1790 of the fields in the relocation information vary per target endian-ness. */
1791
1792 static void
1793 bfd_mach_o_swap_out_non_scattered_reloc (bfd *abfd, unsigned char *fields,
1794 bfd_mach_o_reloc_info *rel)
1795 {
1796 unsigned char info = 0;
1797
1798 BFD_ASSERT (rel->r_type <= 15);
1799 BFD_ASSERT (rel->r_length <= 3);
1800
1801 if (bfd_big_endian (abfd))
1802 {
1803 fields[0] = (rel->r_value >> 16) & 0xff;
1804 fields[1] = (rel->r_value >> 8) & 0xff;
1805 fields[2] = rel->r_value & 0xff;
1806 info |= rel->r_type << BFD_MACH_O_BE_TYPE_SHIFT;
1807 info |= rel->r_pcrel ? BFD_MACH_O_BE_PCREL : 0;
1808 info |= rel->r_length << BFD_MACH_O_BE_LENGTH_SHIFT;
1809 info |= rel->r_extern ? BFD_MACH_O_BE_EXTERN : 0;
1810 }
1811 else
1812 {
1813 fields[2] = (rel->r_value >> 16) & 0xff;
1814 fields[1] = (rel->r_value >> 8) & 0xff;
1815 fields[0] = rel->r_value & 0xff;
1816 info |= rel->r_type << BFD_MACH_O_LE_TYPE_SHIFT;
1817 info |= rel->r_pcrel ? BFD_MACH_O_LE_PCREL : 0;
1818 info |= rel->r_length << BFD_MACH_O_LE_LENGTH_SHIFT;
1819 info |= rel->r_extern ? BFD_MACH_O_LE_EXTERN : 0;
1820 }
1821 fields[3] = info;
1822 }
1823
1824 static bool
1825 bfd_mach_o_write_relocs (bfd *abfd, bfd_mach_o_section *section)
1826 {
1827 unsigned int i;
1828 arelent **entries;
1829 asection *sec;
1830 bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
1831
1832 sec = section->bfdsection;
1833 if (sec->reloc_count == 0)
1834 return true;
1835
1836 if (bed->_bfd_mach_o_swap_reloc_out == NULL)
1837 return true;
1838
1839 if (bfd_seek (abfd, section->reloff, SEEK_SET) != 0)
1840 return false;
1841
1842 /* Convert and write. */
1843 entries = section->bfdsection->orelocation;
1844 for (i = 0; i < section->nreloc; i++)
1845 {
1846 arelent *rel = entries[i];
1847 struct mach_o_reloc_info_external raw;
1848 bfd_mach_o_reloc_info info, *pinfo = &info;
1849
1850 /* Convert relocation to an intermediate representation. */
1851 if (!(*bed->_bfd_mach_o_swap_reloc_out) (rel, pinfo))
1852 return false;
1853
1854 /* Lower the relocation info. */
1855 if (pinfo->r_scattered)
1856 {
1857 unsigned long v;
1858
1859 v = BFD_MACH_O_SR_SCATTERED
1860 | (pinfo->r_pcrel ? BFD_MACH_O_SR_PCREL : 0)
1861 | BFD_MACH_O_SET_SR_LENGTH (pinfo->r_length)
1862 | BFD_MACH_O_SET_SR_TYPE (pinfo->r_type)
1863 | BFD_MACH_O_SET_SR_ADDRESS (pinfo->r_address);
1864 /* Note: scattered relocs have field in reverse order... */
1865 bfd_put_32 (abfd, v, raw.r_address);
1866 bfd_put_32 (abfd, pinfo->r_value, raw.r_symbolnum);
1867 }
1868 else
1869 {
1870 bfd_put_32 (abfd, pinfo->r_address, raw.r_address);
1871 bfd_mach_o_swap_out_non_scattered_reloc (abfd, raw.r_symbolnum,
1872 pinfo);
1873 }
1874
1875 if (bfd_write (&raw, BFD_MACH_O_RELENT_SIZE, abfd)
1876 != BFD_MACH_O_RELENT_SIZE)
1877 return false;
1878 }
1879 return true;
1880 }
1881
1882 static bool
1883 bfd_mach_o_write_section_32 (bfd *abfd, bfd_mach_o_section *section)
1884 {
1885 struct mach_o_section_32_external raw;
1886
1887 memcpy (raw.sectname, section->sectname, 16);
1888 memcpy (raw.segname, section->segname, 16);
1889 bfd_h_put_32 (abfd, section->addr, raw.addr);
1890 bfd_h_put_32 (abfd, section->size, raw.size);
1891 bfd_h_put_32 (abfd, section->offset, raw.offset);
1892 bfd_h_put_32 (abfd, section->align, raw.align);
1893 bfd_h_put_32 (abfd, section->reloff, raw.reloff);
1894 bfd_h_put_32 (abfd, section->nreloc, raw.nreloc);
1895 bfd_h_put_32 (abfd, section->flags, raw.flags);
1896 bfd_h_put_32 (abfd, section->reserved1, raw.reserved1);
1897 bfd_h_put_32 (abfd, section->reserved2, raw.reserved2);
1898
1899 if (bfd_write (&raw, BFD_MACH_O_SECTION_SIZE, abfd)
1900 != BFD_MACH_O_SECTION_SIZE)
1901 return false;
1902
1903 return true;
1904 }
1905
1906 static bool
1907 bfd_mach_o_write_section_64 (bfd *abfd, bfd_mach_o_section *section)
1908 {
1909 struct mach_o_section_64_external raw;
1910
1911 memcpy (raw.sectname, section->sectname, 16);
1912 memcpy (raw.segname, section->segname, 16);
1913 bfd_h_put_64 (abfd, section->addr, raw.addr);
1914 bfd_h_put_64 (abfd, section->size, raw.size);
1915 bfd_h_put_32 (abfd, section->offset, raw.offset);
1916 bfd_h_put_32 (abfd, section->align, raw.align);
1917 bfd_h_put_32 (abfd, section->reloff, raw.reloff);
1918 bfd_h_put_32 (abfd, section->nreloc, raw.nreloc);
1919 bfd_h_put_32 (abfd, section->flags, raw.flags);
1920 bfd_h_put_32 (abfd, section->reserved1, raw.reserved1);
1921 bfd_h_put_32 (abfd, section->reserved2, raw.reserved2);
1922 bfd_h_put_32 (abfd, section->reserved3, raw.reserved3);
1923
1924 if (bfd_write (&raw, BFD_MACH_O_SECTION_64_SIZE, abfd)
1925 != BFD_MACH_O_SECTION_64_SIZE)
1926 return false;
1927
1928 return true;
1929 }
1930
1931 static bool
1932 bfd_mach_o_write_segment_32 (bfd *abfd, bfd_mach_o_load_command *command)
1933 {
1934 struct mach_o_segment_command_32_external raw;
1935 bfd_mach_o_segment_command *seg = &command->command.segment;
1936 bfd_mach_o_section *sec;
1937
1938 BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT);
1939
1940 for (sec = seg->sect_head; sec != NULL; sec = sec->next)
1941 if (!bfd_mach_o_write_relocs (abfd, sec))
1942 return false;
1943
1944 memcpy (raw.segname, seg->segname, 16);
1945 bfd_h_put_32 (abfd, seg->vmaddr, raw.vmaddr);
1946 bfd_h_put_32 (abfd, seg->vmsize, raw.vmsize);
1947 bfd_h_put_32 (abfd, seg->fileoff, raw.fileoff);
1948 bfd_h_put_32 (abfd, seg->filesize, raw.filesize);
1949 bfd_h_put_32 (abfd, seg->maxprot, raw.maxprot);
1950 bfd_h_put_32 (abfd, seg->initprot, raw.initprot);
1951 bfd_h_put_32 (abfd, seg->nsects, raw.nsects);
1952 bfd_h_put_32 (abfd, seg->flags, raw.flags);
1953
1954 if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
1955 || bfd_write (&raw, sizeof (raw), abfd) != sizeof (raw))
1956 return false;
1957
1958 for (sec = seg->sect_head; sec != NULL; sec = sec->next)
1959 if (!bfd_mach_o_write_section_32 (abfd, sec))
1960 return false;
1961
1962 return true;
1963 }
1964
1965 static bool
1966 bfd_mach_o_write_segment_64 (bfd *abfd, bfd_mach_o_load_command *command)
1967 {
1968 struct mach_o_segment_command_64_external raw;
1969 bfd_mach_o_segment_command *seg = &command->command.segment;
1970 bfd_mach_o_section *sec;
1971
1972 BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT_64);
1973
1974 for (sec = seg->sect_head; sec != NULL; sec = sec->next)
1975 if (!bfd_mach_o_write_relocs (abfd, sec))
1976 return false;
1977
1978 memcpy (raw.segname, seg->segname, 16);
1979 bfd_h_put_64 (abfd, seg->vmaddr, raw.vmaddr);
1980 bfd_h_put_64 (abfd, seg->vmsize, raw.vmsize);
1981 bfd_h_put_64 (abfd, seg->fileoff, raw.fileoff);
1982 bfd_h_put_64 (abfd, seg->filesize, raw.filesize);
1983 bfd_h_put_32 (abfd, seg->maxprot, raw.maxprot);
1984 bfd_h_put_32 (abfd, seg->initprot, raw.initprot);
1985 bfd_h_put_32 (abfd, seg->nsects, raw.nsects);
1986 bfd_h_put_32 (abfd, seg->flags, raw.flags);
1987
1988 if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
1989 || bfd_write (&raw, sizeof (raw), abfd) != sizeof (raw))
1990 return false;
1991
1992 for (sec = seg->sect_head; sec != NULL; sec = sec->next)
1993 if (!bfd_mach_o_write_section_64 (abfd, sec))
1994 return false;
1995
1996 return true;
1997 }
1998
1999 static bool
2000 bfd_mach_o_write_symtab_content (bfd *abfd, bfd_mach_o_symtab_command *sym)
2001 {
2002 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
2003 unsigned long i;
2004 unsigned int wide = bfd_mach_o_wide_p (abfd);
2005 struct bfd_strtab_hash *strtab;
2006 asymbol **symbols = bfd_get_outsymbols (abfd);
2007 int padlen;
2008
2009 /* Write the symbols first. */
2010 if (bfd_seek (abfd, sym->symoff, SEEK_SET) != 0)
2011 return false;
2012
2013 strtab = _bfd_stringtab_init ();
2014 if (strtab == NULL)
2015 return false;
2016
2017 if (sym->nsyms > 0)
2018 /* Although we don't strictly need to do this, for compatibility with
2019 Darwin system tools, actually output an empty string for the index
2020 0 entry. */
2021 _bfd_stringtab_add (strtab, "", true, false);
2022
2023 for (i = 0; i < sym->nsyms; i++)
2024 {
2025 bfd_size_type str_index;
2026 bfd_mach_o_asymbol *s = (bfd_mach_o_asymbol *)symbols[i];
2027
2028 if (s->symbol.name == 0 || s->symbol.name[0] == '\0')
2029 /* An index of 0 always means the empty string. */
2030 str_index = 0;
2031 else
2032 {
2033 str_index = _bfd_stringtab_add (strtab, s->symbol.name, true, false);
2034
2035 if (str_index == (bfd_size_type) -1)
2036 goto err;
2037 }
2038
2039 if (wide)
2040 {
2041 struct mach_o_nlist_64_external raw;
2042
2043 bfd_h_put_32 (abfd, str_index, raw.n_strx);
2044 bfd_h_put_8 (abfd, s->n_type, raw.n_type);
2045 bfd_h_put_8 (abfd, s->n_sect, raw.n_sect);
2046 bfd_h_put_16 (abfd, s->n_desc, raw.n_desc);
2047 bfd_h_put_64 (abfd, s->symbol.section->vma + s->symbol.value,
2048 raw.n_value);
2049
2050 if (bfd_write (&raw, sizeof (raw), abfd) != sizeof (raw))
2051 goto err;
2052 }
2053 else
2054 {
2055 struct mach_o_nlist_external raw;
2056
2057 bfd_h_put_32 (abfd, str_index, raw.n_strx);
2058 bfd_h_put_8 (abfd, s->n_type, raw.n_type);
2059 bfd_h_put_8 (abfd, s->n_sect, raw.n_sect);
2060 bfd_h_put_16 (abfd, s->n_desc, raw.n_desc);
2061 bfd_h_put_32 (abfd, s->symbol.section->vma + s->symbol.value,
2062 raw.n_value);
2063
2064 if (bfd_write (&raw, sizeof (raw), abfd) != sizeof (raw))
2065 goto err;
2066 }
2067 }
2068 sym->strsize = _bfd_stringtab_size (strtab);
2069 sym->stroff = mdata->filelen;
2070 mdata->filelen += sym->strsize;
2071
2072 if (bfd_seek (abfd, sym->stroff, SEEK_SET) != 0)
2073 goto err;
2074
2075 if (!_bfd_stringtab_emit (abfd, strtab))
2076 goto err;
2077
2078 _bfd_stringtab_free (strtab);
2079
2080 /* Pad string table. */
2081 padlen = bfd_mach_o_pad4 (abfd, sym->strsize);
2082 if (padlen < 0)
2083 return false;
2084 mdata->filelen += padlen;
2085 sym->strsize += padlen;
2086
2087 return true;
2088
2089 err:
2090 _bfd_stringtab_free (strtab);
2091 sym->strsize = 0;
2092 return false;
2093 }
2094
2095 static bool
2096 bfd_mach_o_write_symtab (bfd *abfd, bfd_mach_o_load_command *command)
2097 {
2098 bfd_mach_o_symtab_command *sym = &command->command.symtab;
2099 struct mach_o_symtab_command_external raw;
2100
2101 BFD_ASSERT (command->type == BFD_MACH_O_LC_SYMTAB);
2102
2103 /* The command. */
2104 bfd_h_put_32 (abfd, sym->symoff, raw.symoff);
2105 bfd_h_put_32 (abfd, sym->nsyms, raw.nsyms);
2106 bfd_h_put_32 (abfd, sym->stroff, raw.stroff);
2107 bfd_h_put_32 (abfd, sym->strsize, raw.strsize);
2108
2109 if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
2110 || bfd_write (&raw, sizeof (raw), abfd) != sizeof (raw))
2111 return false;
2112
2113 return true;
2114 }
2115
2116 /* Count the number of indirect symbols in the image.
2117 Requires that the sections are in their final order. */
2118
2119 static unsigned int
2120 bfd_mach_o_count_indirect_symbols (bfd *abfd, bfd_mach_o_data_struct *mdata)
2121 {
2122 unsigned int i;
2123 unsigned int nisyms = 0;
2124
2125 for (i = 0; i < mdata->nsects; ++i)
2126 {
2127 bfd_mach_o_section *sec = mdata->sections[i];
2128
2129 switch (sec->flags & BFD_MACH_O_SECTION_TYPE_MASK)
2130 {
2131 case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS:
2132 case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS:
2133 case BFD_MACH_O_S_SYMBOL_STUBS:
2134 nisyms += bfd_mach_o_section_get_nbr_indirect (abfd, sec);
2135 break;
2136 default:
2137 break;
2138 }
2139 }
2140 return nisyms;
2141 }
2142
2143 /* Create the dysymtab. */
2144
2145 static bool
2146 bfd_mach_o_build_dysymtab (bfd *abfd, bfd_mach_o_dysymtab_command *cmd)
2147 {
2148 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
2149
2150 /* TODO:
2151 We are not going to try and fill these in yet and, moreover, we are
2152 going to bail if they are already set. */
2153 if (cmd->nmodtab != 0
2154 || cmd->ntoc != 0
2155 || cmd->nextrefsyms != 0)
2156 {
2157 _bfd_error_handler (_("sorry: modtab, toc and extrefsyms are not yet"
2158 " implemented for dysymtab commands."));
2159 return false;
2160 }
2161
2162 cmd->ilocalsym = 0;
2163
2164 if (bfd_get_symcount (abfd) > 0)
2165 {
2166 asymbol **symbols = bfd_get_outsymbols (abfd);
2167 unsigned long i;
2168
2169 /* Count the number of each kind of symbol. */
2170 for (i = 0; i < bfd_get_symcount (abfd); ++i)
2171 {
2172 bfd_mach_o_asymbol *s = (bfd_mach_o_asymbol *)symbols[i];
2173 if (s->n_type & (BFD_MACH_O_N_EXT | BFD_MACH_O_N_PEXT))
2174 break;
2175 }
2176 cmd->nlocalsym = i;
2177 cmd->iextdefsym = i;
2178 for (; i < bfd_get_symcount (abfd); ++i)
2179 {
2180 bfd_mach_o_asymbol *s = (bfd_mach_o_asymbol *)symbols[i];
2181 if ((s->n_type & BFD_MACH_O_N_TYPE) == BFD_MACH_O_N_UNDF)
2182 break;
2183 }
2184 cmd->nextdefsym = i - cmd->nlocalsym;
2185 cmd->iundefsym = cmd->nextdefsym + cmd->iextdefsym;
2186 cmd->nundefsym = bfd_get_symcount (abfd)
2187 - cmd->nlocalsym
2188 - cmd->nextdefsym;
2189 }
2190 else
2191 {
2192 cmd->nlocalsym = 0;
2193 cmd->iextdefsym = 0;
2194 cmd->nextdefsym = 0;
2195 cmd->iundefsym = 0;
2196 cmd->nundefsym = 0;
2197 }
2198
2199 cmd->nindirectsyms = bfd_mach_o_count_indirect_symbols (abfd, mdata);
2200 if (cmd->nindirectsyms > 0)
2201 {
2202 unsigned i;
2203 unsigned n;
2204 size_t amt;
2205
2206 mdata->filelen = FILE_ALIGN (mdata->filelen, 2);
2207 cmd->indirectsymoff = mdata->filelen;
2208 if (_bfd_mul_overflow (cmd->nindirectsyms, 4, &amt))
2209 return false;
2210 mdata->filelen += amt;
2211
2212 cmd->indirect_syms = bfd_zalloc (abfd, amt);
2213 if (cmd->indirect_syms == NULL)
2214 return false;
2215
2216 n = 0;
2217 for (i = 0; i < mdata->nsects; ++i)
2218 {
2219 bfd_mach_o_section *sec = mdata->sections[i];
2220
2221 switch (sec->flags & BFD_MACH_O_SECTION_TYPE_MASK)
2222 {
2223 case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS:
2224 case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS:
2225 case BFD_MACH_O_S_SYMBOL_STUBS:
2226 {
2227 unsigned j, num;
2228 bfd_mach_o_asymbol **isyms = sec->indirect_syms;
2229
2230 num = bfd_mach_o_section_get_nbr_indirect (abfd, sec);
2231 if (isyms == NULL || num == 0)
2232 break;
2233 /* Record the starting index in the reserved1 field. */
2234 sec->reserved1 = n;
2235 for (j = 0; j < num; j++, n++)
2236 {
2237 if (isyms[j] == NULL)
2238 cmd->indirect_syms[n] = BFD_MACH_O_INDIRECT_SYM_LOCAL;
2239 else if (isyms[j]->symbol.section == bfd_abs_section_ptr
2240 && ! (isyms[j]->n_type & BFD_MACH_O_N_EXT))
2241 cmd->indirect_syms[n] = BFD_MACH_O_INDIRECT_SYM_LOCAL
2242 | BFD_MACH_O_INDIRECT_SYM_ABS;
2243 else
2244 cmd->indirect_syms[n] = isyms[j]->symbol.udata.i;
2245 }
2246 }
2247 break;
2248 default:
2249 break;
2250 }
2251 }
2252 }
2253
2254 return true;
2255 }
2256
2257 /* Write a dysymtab command.
2258 TODO: Possibly coalesce writes of smaller objects. */
2259
2260 static bool
2261 bfd_mach_o_write_dysymtab (bfd *abfd, bfd_mach_o_load_command *command)
2262 {
2263 bfd_mach_o_dysymtab_command *cmd = &command->command.dysymtab;
2264
2265 BFD_ASSERT (command->type == BFD_MACH_O_LC_DYSYMTAB);
2266
2267 if (cmd->nmodtab != 0)
2268 {
2269 unsigned int i;
2270
2271 if (bfd_seek (abfd, cmd->modtaboff, SEEK_SET) != 0)
2272 return false;
2273
2274 for (i = 0; i < cmd->nmodtab; i++)
2275 {
2276 bfd_mach_o_dylib_module *module = &cmd->dylib_module[i];
2277 unsigned int iinit;
2278 unsigned int ninit;
2279
2280 iinit = module->iinit & 0xffff;
2281 iinit |= ((module->iterm & 0xffff) << 16);
2282
2283 ninit = module->ninit & 0xffff;
2284 ninit |= ((module->nterm & 0xffff) << 16);
2285
2286 if (bfd_mach_o_wide_p (abfd))
2287 {
2288 struct mach_o_dylib_module_64_external w;
2289
2290 bfd_h_put_32 (abfd, module->module_name_idx, &w.module_name);
2291 bfd_h_put_32 (abfd, module->iextdefsym, &w.iextdefsym);
2292 bfd_h_put_32 (abfd, module->nextdefsym, &w.nextdefsym);
2293 bfd_h_put_32 (abfd, module->irefsym, &w.irefsym);
2294 bfd_h_put_32 (abfd, module->nrefsym, &w.nrefsym);
2295 bfd_h_put_32 (abfd, module->ilocalsym, &w.ilocalsym);
2296 bfd_h_put_32 (abfd, module->nlocalsym, &w.nlocalsym);
2297 bfd_h_put_32 (abfd, module->iextrel, &w.iextrel);
2298 bfd_h_put_32 (abfd, module->nextrel, &w.nextrel);
2299 bfd_h_put_32 (abfd, iinit, &w.iinit_iterm);
2300 bfd_h_put_32 (abfd, ninit, &w.ninit_nterm);
2301 bfd_h_put_64 (abfd, module->objc_module_info_addr,
2302 &w.objc_module_info_addr);
2303 bfd_h_put_32 (abfd, module->objc_module_info_size,
2304 &w.objc_module_info_size);
2305
2306 if (bfd_write (&w, sizeof (w), abfd) != sizeof (w))
2307 return false;
2308 }
2309 else
2310 {
2311 struct mach_o_dylib_module_external n;
2312
2313 bfd_h_put_32 (abfd, module->module_name_idx, &n.module_name);
2314 bfd_h_put_32 (abfd, module->iextdefsym, &n.iextdefsym);
2315 bfd_h_put_32 (abfd, module->nextdefsym, &n.nextdefsym);
2316 bfd_h_put_32 (abfd, module->irefsym, &n.irefsym);
2317 bfd_h_put_32 (abfd, module->nrefsym, &n.nrefsym);
2318 bfd_h_put_32 (abfd, module->ilocalsym, &n.ilocalsym);
2319 bfd_h_put_32 (abfd, module->nlocalsym, &n.nlocalsym);
2320 bfd_h_put_32 (abfd, module->iextrel, &n.iextrel);
2321 bfd_h_put_32 (abfd, module->nextrel, &n.nextrel);
2322 bfd_h_put_32 (abfd, iinit, &n.iinit_iterm);
2323 bfd_h_put_32 (abfd, ninit, &n.ninit_nterm);
2324 bfd_h_put_32 (abfd, module->objc_module_info_addr,
2325 &n.objc_module_info_addr);
2326 bfd_h_put_32 (abfd, module->objc_module_info_size,
2327 &n.objc_module_info_size);
2328
2329 if (bfd_write (&n, sizeof (n), abfd) != sizeof (n))
2330 return false;
2331 }
2332 }
2333 }
2334
2335 if (cmd->ntoc != 0)
2336 {
2337 unsigned int i;
2338
2339 if (bfd_seek (abfd, cmd->tocoff, SEEK_SET) != 0)
2340 return false;
2341
2342 for (i = 0; i < cmd->ntoc; i++)
2343 {
2344 struct mach_o_dylib_table_of_contents_external raw;
2345 bfd_mach_o_dylib_table_of_content *toc = &cmd->dylib_toc[i];
2346
2347 bfd_h_put_32 (abfd, toc->symbol_index, &raw.symbol_index);
2348 bfd_h_put_32 (abfd, toc->module_index, &raw.module_index);
2349
2350 if (bfd_write (&raw, sizeof (raw), abfd) != sizeof (raw))
2351 return false;
2352 }
2353 }
2354
2355 if (cmd->nindirectsyms > 0)
2356 {
2357 unsigned int i;
2358
2359 if (bfd_seek (abfd, cmd->indirectsymoff, SEEK_SET) != 0)
2360 return false;
2361
2362 for (i = 0; i < cmd->nindirectsyms; ++i)
2363 {
2364 unsigned char raw[4];
2365
2366 bfd_h_put_32 (abfd, cmd->indirect_syms[i], &raw);
2367 if (bfd_write (raw, sizeof (raw), abfd) != sizeof (raw))
2368 return false;
2369 }
2370 }
2371
2372 if (cmd->nextrefsyms != 0)
2373 {
2374 unsigned int i;
2375
2376 if (bfd_seek (abfd, cmd->extrefsymoff, SEEK_SET) != 0)
2377 return false;
2378
2379 for (i = 0; i < cmd->nextrefsyms; i++)
2380 {
2381 unsigned long v;
2382 unsigned char raw[4];
2383 bfd_mach_o_dylib_reference *ref = &cmd->ext_refs[i];
2384
2385 /* Fields isym and flags are written as bit-fields, thus we need
2386 a specific processing for endianness. */
2387
2388 if (bfd_big_endian (abfd))
2389 {
2390 v = ((ref->isym & 0xffffff) << 8);
2391 v |= ref->flags & 0xff;
2392 }
2393 else
2394 {
2395 v = ref->isym & 0xffffff;
2396 v |= ((ref->flags & 0xff) << 24);
2397 }
2398
2399 bfd_h_put_32 (abfd, v, raw);
2400 if (bfd_write (raw, sizeof (raw), abfd) != sizeof (raw))
2401 return false;
2402 }
2403 }
2404
2405 /* The command. */
2406 if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0)
2407 return false;
2408 else
2409 {
2410 struct mach_o_dysymtab_command_external raw;
2411
2412 bfd_h_put_32 (abfd, cmd->ilocalsym, &raw.ilocalsym);
2413 bfd_h_put_32 (abfd, cmd->nlocalsym, &raw.nlocalsym);
2414 bfd_h_put_32 (abfd, cmd->iextdefsym, &raw.iextdefsym);
2415 bfd_h_put_32 (abfd, cmd->nextdefsym, &raw.nextdefsym);
2416 bfd_h_put_32 (abfd, cmd->iundefsym, &raw.iundefsym);
2417 bfd_h_put_32 (abfd, cmd->nundefsym, &raw.nundefsym);
2418 bfd_h_put_32 (abfd, cmd->tocoff, &raw.tocoff);
2419 bfd_h_put_32 (abfd, cmd->ntoc, &raw.ntoc);
2420 bfd_h_put_32 (abfd, cmd->modtaboff, &raw.modtaboff);
2421 bfd_h_put_32 (abfd, cmd->nmodtab, &raw.nmodtab);
2422 bfd_h_put_32 (abfd, cmd->extrefsymoff, &raw.extrefsymoff);
2423 bfd_h_put_32 (abfd, cmd->nextrefsyms, &raw.nextrefsyms);
2424 bfd_h_put_32 (abfd, cmd->indirectsymoff, &raw.indirectsymoff);
2425 bfd_h_put_32 (abfd, cmd->nindirectsyms, &raw.nindirectsyms);
2426 bfd_h_put_32 (abfd, cmd->extreloff, &raw.extreloff);
2427 bfd_h_put_32 (abfd, cmd->nextrel, &raw.nextrel);
2428 bfd_h_put_32 (abfd, cmd->locreloff, &raw.locreloff);
2429 bfd_h_put_32 (abfd, cmd->nlocrel, &raw.nlocrel);
2430
2431 if (bfd_write (&raw, sizeof (raw), abfd) != sizeof (raw))
2432 return false;
2433 }
2434
2435 return true;
2436 }
2437
2438 static unsigned
2439 bfd_mach_o_primary_symbol_sort_key (bfd_mach_o_asymbol *s)
2440 {
2441 unsigned mtyp = s->n_type & BFD_MACH_O_N_TYPE;
2442
2443 /* Just leave debug symbols where they are (pretend they are local, and
2444 then they will just be sorted on position). */
2445 if (s->n_type & BFD_MACH_O_N_STAB)
2446 return 0;
2447
2448 /* Local (we should never see an undefined local AFAICT). */
2449 if (! (s->n_type & (BFD_MACH_O_N_EXT | BFD_MACH_O_N_PEXT)))
2450 return 0;
2451
2452 /* Common symbols look like undefined externs. */
2453 if (mtyp == BFD_MACH_O_N_UNDF)
2454 return 2;
2455
2456 /* A defined non-local, non-debug symbol. */
2457 return 1;
2458 }
2459
2460 static int
2461 bfd_mach_o_cf_symbols (const void *a, const void *b)
2462 {
2463 bfd_mach_o_asymbol *sa = *(bfd_mach_o_asymbol **) a;
2464 bfd_mach_o_asymbol *sb = *(bfd_mach_o_asymbol **) b;
2465 unsigned int soa, sob;
2466
2467 soa = bfd_mach_o_primary_symbol_sort_key (sa);
2468 sob = bfd_mach_o_primary_symbol_sort_key (sb);
2469 if (soa < sob)
2470 return -1;
2471
2472 if (soa > sob)
2473 return 1;
2474
2475 /* If it's local or stab, just preserve the input order. */
2476 if (soa == 0)
2477 {
2478 if (sa->symbol.udata.i < sb->symbol.udata.i)
2479 return -1;
2480 if (sa->symbol.udata.i > sb->symbol.udata.i)
2481 return 1;
2482
2483 /* This is probably an error. */
2484 return 0;
2485 }
2486
2487 /* The second sort key is name. */
2488 return strcmp (sa->symbol.name, sb->symbol.name);
2489 }
2490
2491 /* Process the symbols.
2492
2493 This should be OK for single-module files - but it is not likely to work
2494 for multi-module shared libraries.
2495
2496 (a) If the application has not filled in the relevant mach-o fields, make
2497 an estimate.
2498
2499 (b) Order them, like this:
2500 ( i) local.
2501 (unsorted)
2502 ( ii) external defined
2503 (by name)
2504 (iii) external undefined/common
2505 (by name)
2506 ( iv) common
2507 (by name)
2508 */
2509
2510 static bool
2511 bfd_mach_o_mangle_symbols (bfd *abfd)
2512 {
2513 unsigned long i;
2514 asymbol **symbols = bfd_get_outsymbols (abfd);
2515
2516 if (symbols == NULL || bfd_get_symcount (abfd) == 0)
2517 return true;
2518
2519 for (i = 0; i < bfd_get_symcount (abfd); i++)
2520 {
2521 bfd_mach_o_asymbol *s = (bfd_mach_o_asymbol *)symbols[i];
2522
2523 /* We use this value, which is out-of-range as a symbol index, to signal
2524 that the mach-o-specific data are not filled in and need to be created
2525 from the bfd values. It is much preferable for the application to do
2526 this, since more meaningful diagnostics can be made that way. */
2527
2528 if (s->symbol.udata.i == SYM_MACHO_FIELDS_UNSET)
2529 {
2530 /* No symbol information has been set - therefore determine
2531 it from the bfd symbol flags/info. */
2532 if (s->symbol.section == bfd_abs_section_ptr)
2533 s->n_type = BFD_MACH_O_N_ABS;
2534 else if (s->symbol.section == bfd_und_section_ptr)
2535 {
2536 s->n_type = BFD_MACH_O_N_UNDF;
2537 if (s->symbol.flags & BSF_WEAK)
2538 s->n_desc |= BFD_MACH_O_N_WEAK_REF;
2539 /* mach-o automatically makes undefined symbols extern. */
2540 s->n_type |= BFD_MACH_O_N_EXT;
2541 s->symbol.flags |= BSF_GLOBAL;
2542 }
2543 else if (s->symbol.section == bfd_com_section_ptr)
2544 {
2545 s->n_type = BFD_MACH_O_N_UNDF | BFD_MACH_O_N_EXT;
2546 s->symbol.flags |= BSF_GLOBAL;
2547 }
2548 else
2549 s->n_type = BFD_MACH_O_N_SECT;
2550 }
2551
2552 /* Update external symbol bit in case objcopy changed it. */
2553 if (s->symbol.flags & BSF_GLOBAL)
2554 s->n_type |= BFD_MACH_O_N_EXT;
2555 else
2556 s->n_type &= ~BFD_MACH_O_N_EXT;
2557
2558 /* Put the section index in, where required. */
2559 if ((s->symbol.section != bfd_abs_section_ptr
2560 && s->symbol.section != bfd_und_section_ptr
2561 && s->symbol.section != bfd_com_section_ptr)
2562 || ((s->n_type & BFD_MACH_O_N_STAB) != 0
2563 && s->symbol.name == NULL))
2564 s->n_sect = s->symbol.section->output_section->target_index;
2565
2566 /* Number to preserve order for local and debug syms. */
2567 s->symbol.udata.i = i;
2568 }
2569
2570 /* Sort the symbols. */
2571 qsort ((void *) symbols, (size_t) bfd_get_symcount (abfd),
2572 sizeof (asymbol *), bfd_mach_o_cf_symbols);
2573
2574 for (i = 0; i < bfd_get_symcount (abfd); ++i)
2575 {
2576 bfd_mach_o_asymbol *s = (bfd_mach_o_asymbol *)symbols[i];
2577 s->symbol.udata.i = i; /* renumber. */
2578 }
2579
2580 return true;
2581 }
2582
2583 /* We build a flat table of sections, which can be re-ordered if necessary.
2584 Fill in the section number and other mach-o-specific data. */
2585
2586 static bool
2587 bfd_mach_o_mangle_sections (bfd *abfd, bfd_mach_o_data_struct *mdata)
2588 {
2589 asection *sec;
2590 unsigned target_index;
2591 unsigned nsect;
2592 size_t amt;
2593
2594 nsect = bfd_count_sections (abfd);
2595
2596 /* Don't do it if it's already set - assume the application knows what it's
2597 doing. */
2598 if (mdata->nsects == nsect
2599 && (mdata->nsects == 0 || mdata->sections != NULL))
2600 return true;
2601
2602 /* We need to check that this can be done... */
2603 if (nsect > 255)
2604 {
2605 _bfd_error_handler (_("mach-o: there are too many sections (%u)"
2606 " maximum is 255,\n"), nsect);
2607 return false;
2608 }
2609
2610 mdata->nsects = nsect;
2611 amt = mdata->nsects * sizeof (bfd_mach_o_section *);
2612 mdata->sections = bfd_alloc (abfd, amt);
2613 if (mdata->sections == NULL)
2614 return false;
2615
2616 /* Create Mach-O sections.
2617 Section type, attribute and align should have been set when the
2618 section was created - either read in or specified. */
2619 target_index = 0;
2620 for (sec = abfd->sections; sec; sec = sec->next)
2621 {
2622 unsigned bfd_align = bfd_section_alignment (sec);
2623 bfd_mach_o_section *msect = bfd_mach_o_get_mach_o_section (sec);
2624
2625 mdata->sections[target_index] = msect;
2626
2627 msect->addr = bfd_section_vma (sec);
2628 msect->size = bfd_section_size (sec);
2629
2630 /* Use the largest alignment set, in case it was bumped after the
2631 section was created. */
2632 msect->align = msect->align > bfd_align ? msect->align : bfd_align;
2633
2634 msect->offset = 0;
2635 sec->target_index = ++target_index;
2636 }
2637
2638 return true;
2639 }
2640
2641 bool
2642 bfd_mach_o_write_contents (bfd *abfd)
2643 {
2644 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
2645 bfd_mach_o_load_command *cmd;
2646 bfd_mach_o_symtab_command *symtab = NULL;
2647 bfd_mach_o_dysymtab_command *dysymtab = NULL;
2648 bfd_mach_o_segment_command *linkedit = NULL;
2649
2650 /* Make the commands, if not already present. */
2651 if (!abfd->output_has_begun && !bfd_mach_o_build_commands (abfd))
2652 return false;
2653 abfd->output_has_begun = true;
2654
2655 /* Write the header. */
2656 if (!bfd_mach_o_write_header (abfd, &mdata->header))
2657 return false;
2658
2659 /* First pass: allocate the linkedit segment. */
2660 for (cmd = mdata->first_command; cmd != NULL; cmd = cmd->next)
2661 switch (cmd->type)
2662 {
2663 case BFD_MACH_O_LC_SEGMENT_64:
2664 case BFD_MACH_O_LC_SEGMENT:
2665 if (strcmp (cmd->command.segment.segname, "__LINKEDIT") == 0)
2666 linkedit = &cmd->command.segment;
2667 break;
2668 case BFD_MACH_O_LC_SYMTAB:
2669 symtab = &cmd->command.symtab;
2670 break;
2671 case BFD_MACH_O_LC_DYSYMTAB:
2672 dysymtab = &cmd->command.dysymtab;
2673 break;
2674 case BFD_MACH_O_LC_DYLD_INFO:
2675 {
2676 bfd_mach_o_dyld_info_command *di = &cmd->command.dyld_info;
2677
2678 di->rebase_off = di->rebase_size != 0 ? mdata->filelen : 0;
2679 mdata->filelen += di->rebase_size;
2680 di->bind_off = di->bind_size != 0 ? mdata->filelen : 0;
2681 mdata->filelen += di->bind_size;
2682 di->weak_bind_off = di->weak_bind_size != 0 ? mdata->filelen : 0;
2683 mdata->filelen += di->weak_bind_size;
2684 di->lazy_bind_off = di->lazy_bind_size != 0 ? mdata->filelen : 0;
2685 mdata->filelen += di->lazy_bind_size;
2686 di->export_off = di->export_size != 0 ? mdata->filelen : 0;
2687 mdata->filelen += di->export_size;
2688 }
2689 break;
2690 case BFD_MACH_O_LC_LOAD_DYLIB:
2691 case BFD_MACH_O_LC_LOAD_DYLINKER:
2692 case BFD_MACH_O_LC_MAIN:
2693 /* Nothing to do. */
2694 break;
2695 default:
2696 _bfd_error_handler
2697 (_("unable to allocate data for load command %#x"),
2698 cmd->type);
2699 break;
2700 }
2701
2702 /* Specially handle symtab and dysymtab. */
2703
2704 /* Pre-allocate the symbol table (but not the string table). The reason
2705 is that the dysymtab is after the symbol table but before the string
2706 table (required by the native strip tool). */
2707 if (symtab != NULL)
2708 {
2709 unsigned int symlen;
2710 unsigned int wide = bfd_mach_o_wide_p (abfd);
2711
2712 symlen = wide ? BFD_MACH_O_NLIST_64_SIZE : BFD_MACH_O_NLIST_SIZE;
2713
2714 /* Align for symbols. */
2715 mdata->filelen = FILE_ALIGN (mdata->filelen, wide ? 3 : 2);
2716 symtab->symoff = mdata->filelen;
2717
2718 symtab->nsyms = bfd_get_symcount (abfd);
2719 mdata->filelen += symtab->nsyms * symlen;
2720 }
2721
2722 /* Build the dysymtab. */
2723 if (dysymtab != NULL)
2724 if (!bfd_mach_o_build_dysymtab (abfd, dysymtab))
2725 return false;
2726
2727 /* Write symtab and strtab. */
2728 if (symtab != NULL)
2729 if (!bfd_mach_o_write_symtab_content (abfd, symtab))
2730 return false;
2731
2732 /* Adjust linkedit size. */
2733 if (linkedit != NULL)
2734 {
2735 /* bfd_vma pagemask = bfd_mach_o_get_backend_data (abfd)->page_size - 1; */
2736
2737 linkedit->vmsize = mdata->filelen - linkedit->fileoff;
2738 /* linkedit->vmsize = (linkedit->vmsize + pagemask) & ~pagemask; */
2739 linkedit->filesize = mdata->filelen - linkedit->fileoff;
2740
2741 linkedit->initprot = BFD_MACH_O_PROT_READ;
2742 linkedit->maxprot = BFD_MACH_O_PROT_READ | BFD_MACH_O_PROT_WRITE
2743 | BFD_MACH_O_PROT_EXECUTE;
2744 }
2745
2746 /* Second pass: write commands. */
2747 for (cmd = mdata->first_command; cmd != NULL; cmd = cmd->next)
2748 {
2749 struct mach_o_load_command_external raw;
2750 unsigned long typeflag;
2751
2752 typeflag = cmd->type | (cmd->type_required ? BFD_MACH_O_LC_REQ_DYLD : 0);
2753
2754 bfd_h_put_32 (abfd, typeflag, raw.cmd);
2755 bfd_h_put_32 (abfd, cmd->len, raw.cmdsize);
2756
2757 if (bfd_seek (abfd, cmd->offset, SEEK_SET) != 0
2758 || bfd_write (&raw, BFD_MACH_O_LC_SIZE, abfd) != 8)
2759 return false;
2760
2761 switch (cmd->type)
2762 {
2763 case BFD_MACH_O_LC_SEGMENT:
2764 if (!bfd_mach_o_write_segment_32 (abfd, cmd))
2765 return false;
2766 break;
2767 case BFD_MACH_O_LC_SEGMENT_64:
2768 if (!bfd_mach_o_write_segment_64 (abfd, cmd))
2769 return false;
2770 break;
2771 case BFD_MACH_O_LC_SYMTAB:
2772 if (!bfd_mach_o_write_symtab (abfd, cmd))
2773 return false;
2774 break;
2775 case BFD_MACH_O_LC_DYSYMTAB:
2776 if (!bfd_mach_o_write_dysymtab (abfd, cmd))
2777 return false;
2778 break;
2779 case BFD_MACH_O_LC_THREAD:
2780 case BFD_MACH_O_LC_UNIXTHREAD:
2781 if (!bfd_mach_o_write_thread (abfd, cmd))
2782 return false;
2783 break;
2784 case BFD_MACH_O_LC_LOAD_DYLIB:
2785 if (!bfd_mach_o_write_dylib (abfd, cmd))
2786 return false;
2787 break;
2788 case BFD_MACH_O_LC_LOAD_DYLINKER:
2789 if (!bfd_mach_o_write_dylinker (abfd, cmd))
2790 return false;
2791 break;
2792 case BFD_MACH_O_LC_MAIN:
2793 if (!bfd_mach_o_write_main (abfd, cmd))
2794 return false;
2795 break;
2796 case BFD_MACH_O_LC_DYLD_INFO:
2797 if (!bfd_mach_o_write_dyld_info (abfd, cmd))
2798 return false;
2799 break;
2800 default:
2801 _bfd_error_handler
2802 (_("unable to write unknown load command %#x"),
2803 cmd->type);
2804 return false;
2805 }
2806 }
2807
2808 return true;
2809 }
2810
2811 static void
2812 bfd_mach_o_append_section_to_segment (bfd_mach_o_segment_command *seg,
2813 bfd_mach_o_section *s)
2814 {
2815 if (seg->sect_head == NULL)
2816 seg->sect_head = s;
2817 else
2818 seg->sect_tail->next = s;
2819 seg->sect_tail = s;
2820 }
2821
2822 /* Create section Mach-O flags from BFD flags. */
2823
2824 static void
2825 bfd_mach_o_set_section_flags_from_bfd (bfd *abfd ATTRIBUTE_UNUSED,
2826 asection *sec)
2827 {
2828 flagword bfd_flags;
2829 bfd_mach_o_section *s = bfd_mach_o_get_mach_o_section (sec);
2830
2831 /* Create default flags. */
2832 bfd_flags = bfd_section_flags (sec);
2833 if ((bfd_flags & SEC_CODE) == SEC_CODE)
2834 s->flags = BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS
2835 | BFD_MACH_O_S_ATTR_SOME_INSTRUCTIONS
2836 | BFD_MACH_O_S_REGULAR;
2837 else if ((bfd_flags & (SEC_ALLOC | SEC_LOAD)) == SEC_ALLOC)
2838 s->flags = BFD_MACH_O_S_ZEROFILL;
2839 else if (bfd_flags & SEC_DEBUGGING)
2840 s->flags = BFD_MACH_O_S_REGULAR | BFD_MACH_O_S_ATTR_DEBUG;
2841 else
2842 s->flags = BFD_MACH_O_S_REGULAR;
2843 }
2844
2845 static bool
2846 bfd_mach_o_build_obj_seg_command (bfd *abfd, bfd_mach_o_segment_command *seg)
2847 {
2848 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
2849 unsigned int i, j;
2850
2851 seg->vmaddr = 0;
2852 seg->fileoff = mdata->filelen;
2853 seg->initprot = BFD_MACH_O_PROT_READ | BFD_MACH_O_PROT_WRITE
2854 | BFD_MACH_O_PROT_EXECUTE;
2855 seg->maxprot = seg->initprot;
2856
2857 /* Append sections to the segment.
2858
2859 This is a little tedious, we have to honor the need to account zerofill
2860 sections after all the rest. This forces us to do the calculation of
2861 total vmsize in three passes so that any alignment increments are
2862 properly accounted. */
2863 for (i = 0; i < mdata->nsects; ++i)
2864 {
2865 bfd_mach_o_section *s = mdata->sections[i];
2866 asection *sec = s->bfdsection;
2867
2868 /* Although we account for zerofill section sizes in vm order, they are
2869 placed in the file in source sequence. */
2870 bfd_mach_o_append_section_to_segment (seg, s);
2871 s->offset = 0;
2872
2873 /* Zerofill sections have zero file size & offset, the only content
2874 written to the file is the symbols. */
2875 if ((s->flags & BFD_MACH_O_SECTION_TYPE_MASK) == BFD_MACH_O_S_ZEROFILL
2876 || ((s->flags & BFD_MACH_O_SECTION_TYPE_MASK)
2877 == BFD_MACH_O_S_GB_ZEROFILL))
2878 continue;
2879
2880 /* The Darwin system tools (in MH_OBJECT files, at least) always account
2881 sections, even those with zero size. */
2882 if (s->size > 0)
2883 {
2884 seg->vmsize = FILE_ALIGN (seg->vmsize, s->align);
2885 seg->vmsize += s->size;
2886
2887 /* MH_OBJECT files have unaligned content. */
2888 if (1)
2889 {
2890 seg->filesize = FILE_ALIGN (seg->filesize, s->align);
2891 mdata->filelen = FILE_ALIGN (mdata->filelen, s->align);
2892 }
2893 seg->filesize += s->size;
2894
2895 /* The system tools write even zero-sized sections with an offset
2896 field set to the current file position. */
2897 s->offset = mdata->filelen;
2898 }
2899
2900 sec->filepos = s->offset;
2901 mdata->filelen += s->size;
2902 }
2903
2904 /* Now pass through again, for zerofill, only now we just update the
2905 vmsize, and then for zerofill_GB. */
2906 for (j = 0; j < 2; j++)
2907 {
2908 unsigned int stype;
2909
2910 if (j == 0)
2911 stype = BFD_MACH_O_S_ZEROFILL;
2912 else
2913 stype = BFD_MACH_O_S_GB_ZEROFILL;
2914
2915 for (i = 0; i < mdata->nsects; ++i)
2916 {
2917 bfd_mach_o_section *s = mdata->sections[i];
2918
2919 if ((s->flags & BFD_MACH_O_SECTION_TYPE_MASK) != stype)
2920 continue;
2921
2922 if (s->size > 0)
2923 {
2924 seg->vmsize = FILE_ALIGN (seg->vmsize, s->align);
2925 seg->vmsize += s->size;
2926 }
2927 }
2928 }
2929
2930 /* Allocate space for the relocations. */
2931 mdata->filelen = FILE_ALIGN (mdata->filelen, 2);
2932
2933 for (i = 0; i < mdata->nsects; ++i)
2934 {
2935 bfd_mach_o_section *ms = mdata->sections[i];
2936 asection *sec = ms->bfdsection;
2937
2938 ms->nreloc = sec->reloc_count;
2939 if (ms->nreloc == 0)
2940 {
2941 /* Clear nreloc and reloff if there is no relocs. */
2942 ms->reloff = 0;
2943 continue;
2944 }
2945 sec->rel_filepos = mdata->filelen;
2946 ms->reloff = sec->rel_filepos;
2947 mdata->filelen += sec->reloc_count * BFD_MACH_O_RELENT_SIZE;
2948 }
2949
2950 return true;
2951 }
2952
2953 static bool
2954 bfd_mach_o_build_exec_seg_command (bfd *abfd, bfd_mach_o_segment_command *seg)
2955 {
2956 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
2957 unsigned int i;
2958 bfd_vma pagemask = bfd_mach_o_get_backend_data (abfd)->page_size - 1;
2959 bfd_vma vma;
2960 bfd_mach_o_section *s;
2961
2962 seg->vmsize = 0;
2963
2964 seg->fileoff = mdata->filelen;
2965 seg->maxprot = 0;
2966 seg->initprot = 0;
2967 seg->flags = 0;
2968
2969 /* Append sections to the segment. We assume they are properly ordered
2970 by vma (but we check that). */
2971 vma = 0;
2972 for (i = 0; i < mdata->nsects; ++i)
2973 {
2974 s = mdata->sections[i];
2975
2976 /* Consider only sections for this segment. */
2977 if (strcmp (seg->segname, s->segname) != 0)
2978 continue;
2979
2980 bfd_mach_o_append_section_to_segment (seg, s);
2981
2982 if (s->addr < vma)
2983 {
2984 _bfd_error_handler
2985 /* xgettext:c-format */
2986 (_("section address (%#" PRIx64 ") "
2987 "below start of segment (%#" PRIx64 ")"),
2988 (uint64_t) s->addr, (uint64_t) vma);
2989 return false;
2990 }
2991
2992 vma = s->addr + s->size;
2993 }
2994
2995 /* Set segment file offset: make it page aligned. */
2996 vma = seg->sect_head->addr;
2997 seg->vmaddr = vma & ~pagemask;
2998 if ((mdata->filelen & pagemask) > (vma & pagemask))
2999 mdata->filelen += pagemask + 1;
3000 seg->fileoff = mdata->filelen & ~pagemask;
3001 mdata->filelen = seg->fileoff + (vma & pagemask);
3002
3003 /* Set section file offset. */
3004 for (s = seg->sect_head; s != NULL; s = s->next)
3005 {
3006 asection *sec = s->bfdsection;
3007 flagword flags = bfd_section_flags (sec);
3008
3009 /* Adjust segment size. */
3010 seg->vmsize = FILE_ALIGN (seg->vmsize, s->align);
3011 seg->vmsize += s->size;
3012
3013 /* File offset and length. */
3014 seg->filesize = FILE_ALIGN (seg->filesize, s->align);
3015
3016 if ((s->flags & BFD_MACH_O_SECTION_TYPE_MASK) != BFD_MACH_O_S_ZEROFILL
3017 && ((s->flags & BFD_MACH_O_SECTION_TYPE_MASK)
3018 != BFD_MACH_O_S_GB_ZEROFILL))
3019 {
3020 mdata->filelen = FILE_ALIGN (mdata->filelen, s->align);
3021
3022 s->offset = mdata->filelen;
3023 s->bfdsection->filepos = s->offset;
3024
3025 seg->filesize += s->size;
3026 mdata->filelen += s->size;
3027 }
3028 else
3029 {
3030 s->offset = 0;
3031 s->bfdsection->filepos = 0;
3032 }
3033
3034 /* Set protection. */
3035 if (flags & SEC_LOAD)
3036 {
3037 if (flags & SEC_CODE)
3038 seg->initprot |= BFD_MACH_O_PROT_READ | BFD_MACH_O_PROT_EXECUTE;
3039 if ((flags & (SEC_DATA | SEC_READONLY)) == SEC_DATA)
3040 seg->initprot |= BFD_MACH_O_PROT_WRITE | BFD_MACH_O_PROT_READ;
3041 }
3042
3043 /* Relocs shouldn't appear in non-object files. */
3044 if (s->bfdsection->reloc_count != 0)
3045 return false;
3046 }
3047
3048 /* Set maxprot. */
3049 if (seg->initprot != 0)
3050 seg->maxprot = BFD_MACH_O_PROT_READ | BFD_MACH_O_PROT_WRITE
3051 | BFD_MACH_O_PROT_EXECUTE;
3052 else
3053 seg->maxprot = 0;
3054
3055 /* Round segment size (and file size). */
3056 seg->vmsize = (seg->vmsize + pagemask) & ~pagemask;
3057 seg->filesize = (seg->filesize + pagemask) & ~pagemask;
3058 mdata->filelen = (mdata->filelen + pagemask) & ~pagemask;
3059
3060 return true;
3061 }
3062
3063 /* Layout the commands: set commands size and offset, set ncmds and sizeofcmds
3064 fields in header. */
3065
3066 static bool
3067 bfd_mach_o_layout_commands (bfd_mach_o_data_struct *mdata)
3068 {
3069 unsigned wide = mach_o_wide_p (&mdata->header);
3070 unsigned int hdrlen;
3071 ufile_ptr offset;
3072 bfd_mach_o_load_command *cmd;
3073 unsigned int align;
3074 bool ret = true;
3075
3076 hdrlen = wide ? BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE;
3077 align = wide ? 8 - 1 : 4 - 1;
3078 offset = hdrlen;
3079 mdata->header.ncmds = 0;
3080
3081 for (cmd = mdata->first_command; cmd; cmd = cmd->next)
3082 {
3083 mdata->header.ncmds++;
3084 cmd->offset = offset;
3085
3086 switch (cmd->type)
3087 {
3088 case BFD_MACH_O_LC_SEGMENT_64:
3089 cmd->len = BFD_MACH_O_LC_SEGMENT_64_SIZE
3090 + BFD_MACH_O_SECTION_64_SIZE * cmd->command.segment.nsects;
3091 break;
3092 case BFD_MACH_O_LC_SEGMENT:
3093 cmd->len = BFD_MACH_O_LC_SEGMENT_SIZE
3094 + BFD_MACH_O_SECTION_SIZE * cmd->command.segment.nsects;
3095 break;
3096 case BFD_MACH_O_LC_SYMTAB:
3097 cmd->len = sizeof (struct mach_o_symtab_command_external)
3098 + BFD_MACH_O_LC_SIZE;
3099 break;
3100 case BFD_MACH_O_LC_DYSYMTAB:
3101 cmd->len = sizeof (struct mach_o_dysymtab_command_external)
3102 + BFD_MACH_O_LC_SIZE;
3103 break;
3104 case BFD_MACH_O_LC_LOAD_DYLIB:
3105 cmd->len = sizeof (struct mach_o_dylib_command_external)
3106 + BFD_MACH_O_LC_SIZE;
3107 cmd->command.dylib.name_offset = cmd->len;
3108 cmd->len += strlen (cmd->command.dylib.name_str);
3109 cmd->len = (cmd->len + align) & ~align;
3110 break;
3111 case BFD_MACH_O_LC_LOAD_DYLINKER:
3112 cmd->len = sizeof (struct mach_o_str_command_external)
3113 + BFD_MACH_O_LC_SIZE;
3114 cmd->command.dylinker.name_offset = cmd->len;
3115 cmd->len += strlen (cmd->command.dylinker.name_str);
3116 cmd->len = (cmd->len + align) & ~align;
3117 break;
3118 case BFD_MACH_O_LC_MAIN:
3119 cmd->len = sizeof (struct mach_o_entry_point_command_external)
3120 + BFD_MACH_O_LC_SIZE;
3121 break;
3122 case BFD_MACH_O_LC_DYLD_INFO:
3123 cmd->len = sizeof (struct mach_o_dyld_info_command_external)
3124 + BFD_MACH_O_LC_SIZE;
3125 break;
3126 default:
3127 _bfd_error_handler
3128 (_("unable to layout unknown load command %#x"),
3129 cmd->type);
3130 ret = false;
3131 break;
3132 }
3133
3134 BFD_ASSERT (cmd->len % (align + 1) == 0);
3135 offset += cmd->len;
3136 }
3137 mdata->header.sizeofcmds = offset - hdrlen;
3138 mdata->filelen = offset;
3139
3140 return ret;
3141 }
3142
3143 /* Subroutine of bfd_mach_o_build_commands: set type, name and nsects of a
3144 segment. */
3145
3146 static void
3147 bfd_mach_o_init_segment (bfd_mach_o_data_struct *mdata,
3148 bfd_mach_o_load_command *cmd,
3149 const char *segname, unsigned int nbr_sect)
3150 {
3151 bfd_mach_o_segment_command *seg = &cmd->command.segment;
3152 unsigned wide = mach_o_wide_p (&mdata->header);
3153
3154 /* Init segment command. */
3155 cmd->type = wide ? BFD_MACH_O_LC_SEGMENT_64 : BFD_MACH_O_LC_SEGMENT;
3156 cmd->type_required = false;
3157
3158 strcpy (seg->segname, segname);
3159 seg->nsects = nbr_sect;
3160
3161 seg->vmaddr = 0;
3162 seg->vmsize = 0;
3163
3164 seg->fileoff = 0;
3165 seg->filesize = 0;
3166 seg->maxprot = 0;
3167 seg->initprot = 0;
3168 seg->flags = 0;
3169 seg->sect_head = NULL;
3170 seg->sect_tail = NULL;
3171 }
3172
3173 /* Build Mach-O load commands (currently assuming an MH_OBJECT file).
3174 TODO: Other file formats, rebuilding symtab/dysymtab commands for strip
3175 and copy functionality. */
3176
3177 bool
3178 bfd_mach_o_build_commands (bfd *abfd)
3179 {
3180 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
3181 unsigned wide = mach_o_wide_p (&mdata->header);
3182 unsigned int nbr_segcmd = 0;
3183 bfd_mach_o_load_command *commands;
3184 unsigned int nbr_commands;
3185 int symtab_idx = -1;
3186 int dysymtab_idx = -1;
3187 int main_idx = -1;
3188 unsigned int i;
3189
3190 /* Return now if already built. */
3191 if (mdata->header.ncmds != 0)
3192 return true;
3193
3194 /* Fill in the file type, if not already set. */
3195 if (mdata->header.filetype == 0)
3196 {
3197 if (abfd->flags & EXEC_P)
3198 mdata->header.filetype = BFD_MACH_O_MH_EXECUTE;
3199 else if (abfd->flags & DYNAMIC)
3200 mdata->header.filetype = BFD_MACH_O_MH_DYLIB;
3201 else
3202 mdata->header.filetype = BFD_MACH_O_MH_OBJECT;
3203 }
3204
3205 /* If hasn't already been done, flatten sections list, and sort
3206 if/when required. Must be done before the symbol table is adjusted,
3207 since that depends on properly numbered sections. */
3208 if (mdata->nsects == 0 || mdata->sections == NULL)
3209 if (! bfd_mach_o_mangle_sections (abfd, mdata))
3210 return false;
3211
3212 /* Order the symbol table, fill-in/check mach-o specific fields and
3213 partition out any indirect symbols. */
3214 if (!bfd_mach_o_mangle_symbols (abfd))
3215 return false;
3216
3217 /* Segment commands. */
3218 if (mdata->header.filetype == BFD_MACH_O_MH_OBJECT)
3219 {
3220 /* Only one segment for all the sections. But the segment is
3221 optional if there is no sections. */
3222 nbr_segcmd = (mdata->nsects > 0) ? 1 : 0;
3223 }
3224 else
3225 {
3226 bfd_mach_o_section *prev_sect = NULL;
3227
3228 /* One pagezero segment and one linkedit segment. */
3229 nbr_segcmd = 2;
3230
3231 /* Create one segment for associated segment name in sections.
3232 Assume that sections with the same segment name are consecutive. */
3233 for (i = 0; i < mdata->nsects; i++)
3234 {
3235 bfd_mach_o_section *this_sect = mdata->sections[i];
3236
3237 if (prev_sect == NULL
3238 || strcmp (prev_sect->segname, this_sect->segname) != 0)
3239 {
3240 nbr_segcmd++;
3241 prev_sect = this_sect;
3242 }
3243 }
3244 }
3245
3246 nbr_commands = nbr_segcmd;
3247
3248 /* One command for the symbol table (only if there are symbols. */
3249 if (bfd_get_symcount (abfd) > 0)
3250 symtab_idx = nbr_commands++;
3251
3252 /* FIXME:
3253 This is a rather crude test for whether we should build a dysymtab. */
3254 if (bfd_mach_o_should_emit_dysymtab ()
3255 && bfd_get_symcount (abfd))
3256 {
3257 /* If there should be a case where a dysymtab could be emitted without
3258 a symtab (seems improbable), this would need amending. */
3259 dysymtab_idx = nbr_commands++;
3260 }
3261
3262 /* Add an entry point command. */
3263 if (mdata->header.filetype == BFD_MACH_O_MH_EXECUTE
3264 && bfd_get_start_address (abfd) != 0)
3265 main_idx = nbr_commands++;
3266
3267 /* Well, we must have a header, at least. */
3268 mdata->filelen = wide ? BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE;
3269
3270 /* A bit unusual, but no content is valid;
3271 as -n empty.s -o empty.o */
3272 if (nbr_commands == 0)
3273 {
3274 /* Layout commands (well none...) and set headers command fields. */
3275 return bfd_mach_o_layout_commands (mdata);
3276 }
3277
3278 /* Create commands for segments (and symtabs), prepend them. */
3279 commands = bfd_zalloc (abfd, nbr_commands * sizeof (bfd_mach_o_load_command));
3280 if (commands == NULL)
3281 return false;
3282 for (i = 0; i < nbr_commands - 1; i++)
3283 commands[i].next = &commands[i + 1];
3284 commands[nbr_commands - 1].next = mdata->first_command;
3285 if (mdata->first_command == NULL)
3286 mdata->last_command = &commands[nbr_commands - 1];
3287 mdata->first_command = &commands[0];
3288
3289 if (mdata->header.filetype == BFD_MACH_O_MH_OBJECT && nbr_segcmd != 0)
3290 {
3291 /* For object file, there is only one segment. */
3292 bfd_mach_o_init_segment (mdata, &commands[0], "", mdata->nsects);
3293 }
3294 else if (nbr_segcmd != 0)
3295 {
3296 bfd_mach_o_load_command *cmd;
3297
3298 BFD_ASSERT (nbr_segcmd >= 2);
3299
3300 /* The pagezero. */
3301 cmd = &commands[0];
3302 bfd_mach_o_init_segment (mdata, cmd, "__PAGEZERO", 0);
3303
3304 /* Segments from sections. */
3305 cmd++;
3306 for (i = 0; i < mdata->nsects;)
3307 {
3308 const char *segname = mdata->sections[i]->segname;
3309 unsigned int nbr_sect = 1;
3310
3311 /* Count number of sections for this segment. */
3312 for (i++; i < mdata->nsects; i++)
3313 if (strcmp (mdata->sections[i]->segname, segname) == 0)
3314 nbr_sect++;
3315 else
3316 break;
3317
3318 bfd_mach_o_init_segment (mdata, cmd, segname, nbr_sect);
3319 cmd++;
3320 }
3321
3322 /* The linkedit. */
3323 bfd_mach_o_init_segment (mdata, cmd, "__LINKEDIT", 0);
3324 }
3325
3326 if (symtab_idx >= 0)
3327 {
3328 /* Init symtab command. */
3329 bfd_mach_o_load_command *cmd = &commands[symtab_idx];
3330
3331 cmd->type = BFD_MACH_O_LC_SYMTAB;
3332 cmd->type_required = false;
3333 }
3334
3335 /* If required, setup symtab command, see comment above about the quality
3336 of this test. */
3337 if (dysymtab_idx >= 0)
3338 {
3339 bfd_mach_o_load_command *cmd = &commands[dysymtab_idx];
3340
3341 cmd->type = BFD_MACH_O_LC_DYSYMTAB;
3342 cmd->type_required = false;
3343 }
3344
3345 /* Create the main command. */
3346 if (main_idx >= 0)
3347 {
3348 bfd_mach_o_load_command *cmd = &commands[main_idx];
3349
3350 cmd->type = BFD_MACH_O_LC_MAIN;
3351 cmd->type_required = true;
3352
3353 cmd->command.main.entryoff = 0;
3354 cmd->command.main.stacksize = 0;
3355 }
3356
3357 /* Layout commands. */
3358 if (! bfd_mach_o_layout_commands (mdata))
3359 return false;
3360
3361 /* So, now we have sized the commands and the filelen set to that.
3362 Now we can build the segment command and set the section file offsets. */
3363 if (mdata->header.filetype == BFD_MACH_O_MH_OBJECT)
3364 {
3365 for (i = 0; i < nbr_segcmd; i++)
3366 if (!bfd_mach_o_build_obj_seg_command
3367 (abfd, &commands[i].command.segment))
3368 return false;
3369 }
3370 else
3371 {
3372 bfd_vma maxvma = 0;
3373
3374 /* Skip pagezero and linkedit segments. */
3375 for (i = 1; i < nbr_segcmd - 1; i++)
3376 {
3377 bfd_mach_o_segment_command *seg = &commands[i].command.segment;
3378
3379 if (!bfd_mach_o_build_exec_seg_command (abfd, seg))
3380 return false;
3381
3382 if (seg->vmaddr + seg->vmsize > maxvma)
3383 maxvma = seg->vmaddr + seg->vmsize;
3384 }
3385
3386 /* Set the size of __PAGEZERO. */
3387 commands[0].command.segment.vmsize =
3388 commands[1].command.segment.vmaddr;
3389
3390 /* Set the vma and fileoff of __LINKEDIT. */
3391 commands[nbr_segcmd - 1].command.segment.vmaddr = maxvma;
3392 commands[nbr_segcmd - 1].command.segment.fileoff = mdata->filelen;
3393
3394 /* Set entry point (once segments have been laid out). */
3395 if (main_idx >= 0)
3396 commands[main_idx].command.main.entryoff =
3397 bfd_get_start_address (abfd) - commands[1].command.segment.vmaddr;
3398 }
3399
3400 return true;
3401 }
3402
3403 /* Set the contents of a section. */
3404
3405 bool
3406 bfd_mach_o_set_section_contents (bfd *abfd,
3407 asection *section,
3408 const void * location,
3409 file_ptr offset,
3410 bfd_size_type count)
3411 {
3412 file_ptr pos;
3413
3414 /* Trying to write the first section contents will trigger the creation of
3415 the load commands if they are not already present. */
3416 if (!abfd->output_has_begun && !bfd_mach_o_build_commands (abfd))
3417 return false;
3418
3419 if (count == 0)
3420 return true;
3421
3422 pos = section->filepos + offset;
3423 if (bfd_seek (abfd, pos, SEEK_SET) != 0
3424 || bfd_write (location, count, abfd) != count)
3425 return false;
3426
3427 return true;
3428 }
3429
3430 int
3431 bfd_mach_o_sizeof_headers (bfd *a ATTRIBUTE_UNUSED,
3432 struct bfd_link_info *info ATTRIBUTE_UNUSED)
3433 {
3434 return 0;
3435 }
3436
3437 /* Make an empty symbol. This is required only because
3438 bfd_make_section_anyway wants to create a symbol for the section. */
3439
3440 asymbol *
3441 bfd_mach_o_make_empty_symbol (bfd *abfd)
3442 {
3443 asymbol *new_symbol;
3444
3445 new_symbol = bfd_zalloc (abfd, sizeof (bfd_mach_o_asymbol));
3446 if (new_symbol == NULL)
3447 return new_symbol;
3448 new_symbol->the_bfd = abfd;
3449 new_symbol->udata.i = SYM_MACHO_FIELDS_UNSET;
3450 return new_symbol;
3451 }
3452
3453 static bool
3454 bfd_mach_o_read_header (bfd *abfd, file_ptr hdr_off, bfd_mach_o_header *header)
3455 {
3456 struct mach_o_header_external raw;
3457 unsigned int size;
3458 bfd_vma (*get32) (const void *) = NULL;
3459
3460 /* Just read the magic number. */
3461 if (bfd_seek (abfd, hdr_off, SEEK_SET) != 0
3462 || bfd_read (raw.magic, sizeof (raw.magic), abfd) != 4)
3463 return false;
3464
3465 if (bfd_getb32 (raw.magic) == BFD_MACH_O_MH_MAGIC)
3466 {
3467 header->byteorder = BFD_ENDIAN_BIG;
3468 header->magic = BFD_MACH_O_MH_MAGIC;
3469 header->version = 1;
3470 get32 = bfd_getb32;
3471 }
3472 else if (bfd_getl32 (raw.magic) == BFD_MACH_O_MH_MAGIC)
3473 {
3474 header->byteorder = BFD_ENDIAN_LITTLE;
3475 header->magic = BFD_MACH_O_MH_MAGIC;
3476 header->version = 1;
3477 get32 = bfd_getl32;
3478 }
3479 else if (bfd_getb32 (raw.magic) == BFD_MACH_O_MH_MAGIC_64)
3480 {
3481 header->byteorder = BFD_ENDIAN_BIG;
3482 header->magic = BFD_MACH_O_MH_MAGIC_64;
3483 header->version = 2;
3484 get32 = bfd_getb32;
3485 }
3486 else if (bfd_getl32 (raw.magic) == BFD_MACH_O_MH_MAGIC_64)
3487 {
3488 header->byteorder = BFD_ENDIAN_LITTLE;
3489 header->magic = BFD_MACH_O_MH_MAGIC_64;
3490 header->version = 2;
3491 get32 = bfd_getl32;
3492 }
3493 else
3494 {
3495 header->byteorder = BFD_ENDIAN_UNKNOWN;
3496 return false;
3497 }
3498
3499 /* Once the size of the header is known, read the full header. */
3500 size = mach_o_wide_p (header) ?
3501 BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE;
3502
3503 if (bfd_seek (abfd, hdr_off, SEEK_SET) != 0
3504 || bfd_read (&raw, size, abfd) != size)
3505 return false;
3506
3507 header->cputype = (*get32) (raw.cputype);
3508 header->cpusubtype = (*get32) (raw.cpusubtype);
3509 header->filetype = (*get32) (raw.filetype);
3510 header->ncmds = (*get32) (raw.ncmds);
3511 header->sizeofcmds = (*get32) (raw.sizeofcmds);
3512 header->flags = (*get32) (raw.flags);
3513
3514 if (mach_o_wide_p (header))
3515 header->reserved = (*get32) (raw.reserved);
3516 else
3517 header->reserved = 0;
3518
3519 return true;
3520 }
3521
3522 bool
3523 bfd_mach_o_new_section_hook (bfd *abfd, asection *sec)
3524 {
3525 bfd_mach_o_section *s = bfd_zalloc (abfd, sizeof (*s));
3526 if (s == NULL)
3527 return false;
3528 sec->used_by_bfd = s;
3529 s->bfdsection = sec;
3530
3531 /* Create the Darwin seg/sect name pair from the bfd name.
3532 If this is a canonical name for which a specific paiting exists
3533 there will also be defined flags, type, attribute and alignment
3534 values. */
3535 const mach_o_section_name_xlat *xlat
3536 = bfd_mach_o_convert_section_name_to_mach_o (abfd, sec, s);
3537 if (xlat != NULL)
3538 {
3539 s->flags = xlat->macho_sectype | xlat->macho_secattr;
3540 unsigned bfdalign = bfd_section_alignment (sec);
3541 s->align = xlat->sectalign > bfdalign ? xlat->sectalign : bfdalign;
3542 bfd_set_section_alignment (sec, s->align);
3543 flagword bfd_flags = bfd_section_flags (sec);
3544 if (bfd_flags == SEC_NO_FLAGS)
3545 bfd_set_section_flags (sec, xlat->bfd_flags);
3546 }
3547 else
3548 /* Create default flags. */
3549 bfd_mach_o_set_section_flags_from_bfd (abfd, sec);
3550
3551 return _bfd_generic_new_section_hook (abfd, sec);
3552 }
3553
3554 static void
3555 bfd_mach_o_init_section_from_mach_o (asection *sec, unsigned long prot)
3556 {
3557 flagword flags;
3558 bfd_mach_o_section *section;
3559
3560 flags = bfd_section_flags (sec);
3561 section = bfd_mach_o_get_mach_o_section (sec);
3562
3563 /* TODO: see if we should use the xlat system for doing this by
3564 preference and fall back to this for unknown sections. */
3565
3566 if (flags == SEC_NO_FLAGS)
3567 {
3568 /* Try to guess flags. */
3569 if (section->flags & BFD_MACH_O_S_ATTR_DEBUG)
3570 flags = SEC_DEBUGGING;
3571 else
3572 {
3573 flags = SEC_ALLOC;
3574 if ((section->flags & BFD_MACH_O_SECTION_TYPE_MASK)
3575 != BFD_MACH_O_S_ZEROFILL)
3576 {
3577 flags |= SEC_LOAD;
3578 if (prot & BFD_MACH_O_PROT_EXECUTE)
3579 flags |= SEC_CODE;
3580 if (prot & BFD_MACH_O_PROT_WRITE)
3581 flags |= SEC_DATA;
3582 else if (prot & BFD_MACH_O_PROT_READ)
3583 flags |= SEC_READONLY;
3584 }
3585 }
3586 }
3587 else
3588 {
3589 if ((flags & SEC_DEBUGGING) == 0)
3590 flags |= SEC_ALLOC;
3591 }
3592
3593 if (section->offset != 0)
3594 flags |= SEC_HAS_CONTENTS;
3595 if (section->nreloc != 0)
3596 flags |= SEC_RELOC;
3597
3598 bfd_set_section_flags (sec, flags);
3599
3600 sec->vma = section->addr;
3601 sec->lma = section->addr;
3602 sec->size = section->size;
3603 sec->filepos = section->offset;
3604 sec->alignment_power = section->align;
3605 sec->segment_mark = 0;
3606 sec->reloc_count = section->nreloc;
3607 sec->rel_filepos = section->reloff;
3608 }
3609
3610 static asection *
3611 bfd_mach_o_make_bfd_section (bfd *abfd,
3612 const unsigned char *segname,
3613 const unsigned char *sectname)
3614 {
3615 const char *sname;
3616 flagword flags;
3617
3618 bfd_mach_o_convert_section_name_to_bfd
3619 (abfd, (const char *)segname, (const char *)sectname, &sname, &flags);
3620 if (sname == NULL)
3621 return NULL;
3622
3623 return bfd_make_section_anyway_with_flags (abfd, sname, flags);
3624 }
3625
3626 static asection *
3627 bfd_mach_o_read_section_32 (bfd *abfd, unsigned long prot)
3628 {
3629 struct mach_o_section_32_external raw;
3630 asection *sec;
3631 bfd_mach_o_section *section;
3632
3633 if (bfd_read (&raw, BFD_MACH_O_SECTION_SIZE, abfd)
3634 != BFD_MACH_O_SECTION_SIZE)
3635 return NULL;
3636
3637 sec = bfd_mach_o_make_bfd_section (abfd, raw.segname, raw.sectname);
3638 if (sec == NULL)
3639 return NULL;
3640
3641 section = bfd_mach_o_get_mach_o_section (sec);
3642 memcpy (section->segname, raw.segname, sizeof (raw.segname));
3643 section->segname[BFD_MACH_O_SEGNAME_SIZE] = 0;
3644 memcpy (section->sectname, raw.sectname, sizeof (raw.sectname));
3645 section->sectname[BFD_MACH_O_SECTNAME_SIZE] = 0;
3646 section->addr = bfd_h_get_32 (abfd, raw.addr);
3647 section->size = bfd_h_get_32 (abfd, raw.size);
3648 section->offset = bfd_h_get_32 (abfd, raw.offset);
3649 section->align = bfd_h_get_32 (abfd, raw.align);
3650 /* PR 17512: file: 0017eb76. */
3651 if (section->align >= 31)
3652 {
3653 _bfd_error_handler
3654 (_("bfd_mach_o_read_section_32: overlarge alignment value: %#lx"),
3655 section->align);
3656 section->align = 30;
3657 }
3658 section->reloff = bfd_h_get_32 (abfd, raw.reloff);
3659 section->nreloc = bfd_h_get_32 (abfd, raw.nreloc);
3660 section->flags = bfd_h_get_32 (abfd, raw.flags);
3661 section->reserved1 = bfd_h_get_32 (abfd, raw.reserved1);
3662 section->reserved2 = bfd_h_get_32 (abfd, raw.reserved2);
3663 section->reserved3 = 0;
3664
3665 bfd_mach_o_init_section_from_mach_o (sec, prot);
3666
3667 return sec;
3668 }
3669
3670 static asection *
3671 bfd_mach_o_read_section_64 (bfd *abfd, unsigned long prot)
3672 {
3673 struct mach_o_section_64_external raw;
3674 asection *sec;
3675 bfd_mach_o_section *section;
3676
3677 if (bfd_read (&raw, BFD_MACH_O_SECTION_64_SIZE, abfd)
3678 != BFD_MACH_O_SECTION_64_SIZE)
3679 return NULL;
3680
3681 sec = bfd_mach_o_make_bfd_section (abfd, raw.segname, raw.sectname);
3682 if (sec == NULL)
3683 return NULL;
3684
3685 section = bfd_mach_o_get_mach_o_section (sec);
3686 memcpy (section->segname, raw.segname, sizeof (raw.segname));
3687 section->segname[BFD_MACH_O_SEGNAME_SIZE] = 0;
3688 memcpy (section->sectname, raw.sectname, sizeof (raw.sectname));
3689 section->sectname[BFD_MACH_O_SECTNAME_SIZE] = 0;
3690 section->addr = bfd_h_get_64 (abfd, raw.addr);
3691 section->size = bfd_h_get_64 (abfd, raw.size);
3692 section->offset = bfd_h_get_32 (abfd, raw.offset);
3693 section->align = bfd_h_get_32 (abfd, raw.align);
3694 if (section->align >= 63)
3695 {
3696 _bfd_error_handler
3697 (_("bfd_mach_o_read_section_64: overlarge alignment value: %#lx"),
3698 section->align);
3699 section->align = 62;
3700 }
3701 section->reloff = bfd_h_get_32 (abfd, raw.reloff);
3702 section->nreloc = bfd_h_get_32 (abfd, raw.nreloc);
3703 section->flags = bfd_h_get_32 (abfd, raw.flags);
3704 section->reserved1 = bfd_h_get_32 (abfd, raw.reserved1);
3705 section->reserved2 = bfd_h_get_32 (abfd, raw.reserved2);
3706 section->reserved3 = bfd_h_get_32 (abfd, raw.reserved3);
3707
3708 bfd_mach_o_init_section_from_mach_o (sec, prot);
3709
3710 return sec;
3711 }
3712
3713 static asection *
3714 bfd_mach_o_read_section (bfd *abfd, unsigned long prot, unsigned int wide)
3715 {
3716 if (wide)
3717 return bfd_mach_o_read_section_64 (abfd, prot);
3718 else
3719 return bfd_mach_o_read_section_32 (abfd, prot);
3720 }
3721
3722 static bool
3723 bfd_mach_o_read_symtab_symbol (bfd *abfd,
3724 bfd_mach_o_symtab_command *sym,
3725 bfd_mach_o_asymbol *s,
3726 unsigned long i)
3727 {
3728 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
3729 unsigned int wide = mach_o_wide_p (&mdata->header);
3730 unsigned int symwidth =
3731 wide ? BFD_MACH_O_NLIST_64_SIZE : BFD_MACH_O_NLIST_SIZE;
3732 unsigned int symoff = sym->symoff + (i * symwidth);
3733 struct mach_o_nlist_64_external raw;
3734 unsigned char type = -1;
3735 unsigned char section = -1;
3736 short desc = -1;
3737 symvalue value = -1;
3738 unsigned long stroff = -1;
3739 unsigned int symtype = -1;
3740
3741 BFD_ASSERT (sym->strtab != NULL);
3742
3743 if (bfd_seek (abfd, symoff, SEEK_SET) != 0
3744 || bfd_read (&raw, symwidth, abfd) != symwidth)
3745 {
3746 _bfd_error_handler
3747 /* xgettext:c-format */
3748 (_("bfd_mach_o_read_symtab_symbol: unable to read %d bytes at %u"),
3749 symwidth, symoff);
3750 return false;
3751 }
3752
3753 stroff = bfd_h_get_32 (abfd, raw.n_strx);
3754 type = bfd_h_get_8 (abfd, raw.n_type);
3755 symtype = type & BFD_MACH_O_N_TYPE;
3756 section = bfd_h_get_8 (abfd, raw.n_sect);
3757 desc = bfd_h_get_16 (abfd, raw.n_desc);
3758 if (wide)
3759 value = bfd_h_get_64 (abfd, raw.n_value);
3760 else
3761 value = bfd_h_get_32 (abfd, raw.n_value);
3762
3763 if (stroff >= sym->strsize)
3764 {
3765 _bfd_error_handler
3766 /* xgettext:c-format */
3767 (_("bfd_mach_o_read_symtab_symbol: name out of range (%lu >= %u)"),
3768 stroff,
3769 sym->strsize);
3770 return false;
3771 }
3772
3773 s->symbol.the_bfd = abfd;
3774 s->symbol.name = sym->strtab + stroff;
3775 s->symbol.value = value;
3776 s->symbol.flags = 0x0;
3777 s->symbol.udata.i = i;
3778 s->n_type = type;
3779 s->n_sect = section;
3780 s->n_desc = desc;
3781
3782 if (type & BFD_MACH_O_N_STAB)
3783 {
3784 s->symbol.flags |= BSF_DEBUGGING;
3785 s->symbol.section = bfd_und_section_ptr;
3786 switch (type)
3787 {
3788 case N_FUN:
3789 case N_STSYM:
3790 case N_LCSYM:
3791 case N_BNSYM:
3792 case N_SLINE:
3793 case N_ENSYM:
3794 case N_ECOMM:
3795 case N_ECOML:
3796 case N_GSYM:
3797 if ((section > 0) && (section <= mdata->nsects))
3798 {
3799 s->symbol.section = mdata->sections[section - 1]->bfdsection;
3800 s->symbol.value =
3801 s->symbol.value - mdata->sections[section - 1]->addr;
3802 }
3803 break;
3804 }
3805 }
3806 else
3807 {
3808 if (type & (BFD_MACH_O_N_PEXT | BFD_MACH_O_N_EXT))
3809 s->symbol.flags |= BSF_GLOBAL;
3810 else
3811 s->symbol.flags |= BSF_LOCAL;
3812
3813 switch (symtype)
3814 {
3815 case BFD_MACH_O_N_UNDF:
3816 if (type == (BFD_MACH_O_N_UNDF | BFD_MACH_O_N_EXT)
3817 && s->symbol.value != 0)
3818 {
3819 /* A common symbol. */
3820 s->symbol.section = bfd_com_section_ptr;
3821 s->symbol.flags = BSF_NO_FLAGS;
3822 }
3823 else
3824 {
3825 s->symbol.section = bfd_und_section_ptr;
3826 if (s->n_desc & BFD_MACH_O_N_WEAK_REF)
3827 s->symbol.flags |= BSF_WEAK;
3828 }
3829 break;
3830 case BFD_MACH_O_N_PBUD:
3831 s->symbol.section = bfd_und_section_ptr;
3832 break;
3833 case BFD_MACH_O_N_ABS:
3834 s->symbol.section = bfd_abs_section_ptr;
3835 break;
3836 case BFD_MACH_O_N_SECT:
3837 if ((section > 0) && (section <= mdata->nsects))
3838 {
3839 s->symbol.section = mdata->sections[section - 1]->bfdsection;
3840 s->symbol.value =
3841 s->symbol.value - mdata->sections[section - 1]->addr;
3842 }
3843 else
3844 {
3845 /* Mach-O uses 0 to mean "no section"; not an error. */
3846 if (section != 0)
3847 {
3848 _bfd_error_handler
3849 /* xgettext:c-format */
3850 (_("bfd_mach_o_read_symtab_symbol: "
3851 "symbol \"%s\" specified invalid section %d (max %lu): "
3852 "setting to undefined"),
3853 s->symbol.name, section, mdata->nsects);
3854 }
3855 s->symbol.section = bfd_und_section_ptr;
3856 }
3857 break;
3858 case BFD_MACH_O_N_INDR:
3859 /* FIXME: we don't follow the BFD convention as this indirect symbol
3860 won't be followed by the referenced one. This looks harmless
3861 unless we start using the linker. */
3862 s->symbol.flags |= BSF_INDIRECT;
3863 s->symbol.section = bfd_ind_section_ptr;
3864 s->symbol.value = 0;
3865 break;
3866 default:
3867 _bfd_error_handler
3868 /* xgettext:c-format */
3869 (_("bfd_mach_o_read_symtab_symbol: "
3870 "symbol \"%s\" specified invalid type field 0x%x: "
3871 "setting to undefined"), s->symbol.name, symtype);
3872 s->symbol.section = bfd_und_section_ptr;
3873 break;
3874 }
3875 }
3876
3877 return true;
3878 }
3879
3880 bool
3881 bfd_mach_o_read_symtab_strtab (bfd *abfd)
3882 {
3883 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
3884 bfd_mach_o_symtab_command *sym = mdata->symtab;
3885
3886 /* Fail if there is no symtab. */
3887 if (sym == NULL)
3888 return false;
3889
3890 /* Success if already loaded. */
3891 if (sym->strtab)
3892 return true;
3893
3894 if (abfd->flags & BFD_IN_MEMORY)
3895 {
3896 struct bfd_in_memory *b;
3897
3898 b = (struct bfd_in_memory *) abfd->iostream;
3899
3900 if ((sym->stroff + sym->strsize) > b->size)
3901 {
3902 bfd_set_error (bfd_error_file_truncated);
3903 return false;
3904 }
3905 sym->strtab = (char *) b->buffer + sym->stroff;
3906 }
3907 else
3908 {
3909 /* See PR 21840 for a reproducer. */
3910 if ((sym->strsize + 1) == 0)
3911 return false;
3912 if (bfd_seek (abfd, sym->stroff, SEEK_SET) != 0)
3913 return false;
3914 sym->strtab = (char *) _bfd_alloc_and_read (abfd, sym->strsize + 1,
3915 sym->strsize);
3916 if (sym->strtab == NULL)
3917 return false;
3918
3919 /* Zero terminate the string table. */
3920 sym->strtab[sym->strsize] = 0;
3921 }
3922
3923 return true;
3924 }
3925
3926 bool
3927 bfd_mach_o_read_symtab_symbols (bfd *abfd)
3928 {
3929 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
3930 bfd_mach_o_symtab_command *sym = mdata->symtab;
3931 unsigned long i;
3932 size_t amt;
3933 ufile_ptr filesize;
3934
3935 if (sym == NULL || sym->nsyms == 0 || sym->symbols)
3936 /* Return now if there are no symbols or if already loaded. */
3937 return true;
3938
3939 filesize = bfd_get_file_size (abfd);
3940 if (filesize != 0)
3941 {
3942 unsigned int wide = mach_o_wide_p (&mdata->header);
3943 unsigned int symwidth
3944 = wide ? BFD_MACH_O_NLIST_64_SIZE : BFD_MACH_O_NLIST_SIZE;
3945
3946 if (sym->symoff > filesize
3947 || sym->nsyms > (filesize - sym->symoff) / symwidth)
3948 {
3949 bfd_set_error (bfd_error_file_truncated);
3950 sym->nsyms = 0;
3951 return false;
3952 }
3953 }
3954 if (_bfd_mul_overflow (sym->nsyms, sizeof (bfd_mach_o_asymbol), &amt)
3955 || (sym->symbols = bfd_alloc (abfd, amt)) == NULL)
3956 {
3957 bfd_set_error (bfd_error_no_memory);
3958 sym->nsyms = 0;
3959 return false;
3960 }
3961
3962 if (!bfd_mach_o_read_symtab_strtab (abfd))
3963 goto fail;
3964
3965 for (i = 0; i < sym->nsyms; i++)
3966 if (!bfd_mach_o_read_symtab_symbol (abfd, sym, &sym->symbols[i], i))
3967 goto fail;
3968
3969 return true;
3970
3971 fail:
3972 bfd_release (abfd, sym->symbols);
3973 sym->symbols = NULL;
3974 sym->nsyms = 0;
3975 return false;
3976 }
3977
3978 static const char *
3979 bfd_mach_o_i386_flavour_string (unsigned int flavour)
3980 {
3981 switch ((int) flavour)
3982 {
3983 case BFD_MACH_O_x86_THREAD_STATE32: return "x86_THREAD_STATE32";
3984 case BFD_MACH_O_x86_FLOAT_STATE32: return "x86_FLOAT_STATE32";
3985 case BFD_MACH_O_x86_EXCEPTION_STATE32: return "x86_EXCEPTION_STATE32";
3986 case BFD_MACH_O_x86_THREAD_STATE64: return "x86_THREAD_STATE64";
3987 case BFD_MACH_O_x86_FLOAT_STATE64: return "x86_FLOAT_STATE64";
3988 case BFD_MACH_O_x86_EXCEPTION_STATE64: return "x86_EXCEPTION_STATE64";
3989 case BFD_MACH_O_x86_THREAD_STATE: return "x86_THREAD_STATE";
3990 case BFD_MACH_O_x86_FLOAT_STATE: return "x86_FLOAT_STATE";
3991 case BFD_MACH_O_x86_EXCEPTION_STATE: return "x86_EXCEPTION_STATE";
3992 case BFD_MACH_O_x86_DEBUG_STATE32: return "x86_DEBUG_STATE32";
3993 case BFD_MACH_O_x86_DEBUG_STATE64: return "x86_DEBUG_STATE64";
3994 case BFD_MACH_O_x86_DEBUG_STATE: return "x86_DEBUG_STATE";
3995 case BFD_MACH_O_x86_THREAD_STATE_NONE: return "x86_THREAD_STATE_NONE";
3996 default: return "UNKNOWN";
3997 }
3998 }
3999
4000 static const char *
4001 bfd_mach_o_ppc_flavour_string (unsigned int flavour)
4002 {
4003 switch ((int) flavour)
4004 {
4005 case BFD_MACH_O_PPC_THREAD_STATE: return "PPC_THREAD_STATE";
4006 case BFD_MACH_O_PPC_FLOAT_STATE: return "PPC_FLOAT_STATE";
4007 case BFD_MACH_O_PPC_EXCEPTION_STATE: return "PPC_EXCEPTION_STATE";
4008 case BFD_MACH_O_PPC_VECTOR_STATE: return "PPC_VECTOR_STATE";
4009 case BFD_MACH_O_PPC_THREAD_STATE64: return "PPC_THREAD_STATE64";
4010 case BFD_MACH_O_PPC_EXCEPTION_STATE64: return "PPC_EXCEPTION_STATE64";
4011 default: return "UNKNOWN";
4012 }
4013 }
4014
4015 static unsigned char *
4016 bfd_mach_o_alloc_and_read (bfd *abfd, file_ptr filepos,
4017 size_t size, size_t extra)
4018 {
4019 if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
4020 return NULL;
4021 unsigned char *ret = _bfd_alloc_and_read (abfd, size + extra, size);
4022 if (ret && extra != 0)
4023 memset (ret + size, 0, extra);
4024 return ret;
4025 }
4026
4027 static bool
4028 bfd_mach_o_read_dylinker (bfd *abfd, bfd_mach_o_load_command *command)
4029 {
4030 bfd_mach_o_dylinker_command *cmd = &command->command.dylinker;
4031 struct mach_o_str_command_external raw;
4032 unsigned int nameoff;
4033 size_t namelen;
4034
4035 if (command->len < sizeof (raw) + 8)
4036 return false;
4037 if (bfd_read (&raw, sizeof (raw), abfd) != sizeof (raw))
4038 return false;
4039
4040 nameoff = bfd_h_get_32 (abfd, raw.str);
4041 if (nameoff > command->len)
4042 return false;
4043
4044 cmd->name_offset = nameoff;
4045 namelen = command->len - nameoff;
4046 nameoff += command->offset;
4047 cmd->name_str = (char *) bfd_mach_o_alloc_and_read (abfd, nameoff,
4048 namelen, 1);
4049 return cmd->name_str != NULL;
4050 }
4051
4052 static bool
4053 bfd_mach_o_read_dylib (bfd *abfd, bfd_mach_o_load_command *command)
4054 {
4055 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
4056 bfd_mach_o_dylib_command *cmd = &command->command.dylib;
4057 struct mach_o_dylib_command_external raw;
4058 unsigned int nameoff;
4059 size_t namelen;
4060 file_ptr pos;
4061
4062 if (command->len < sizeof (raw) + 8)
4063 return false;
4064 switch (command->type)
4065 {
4066 case BFD_MACH_O_LC_LOAD_DYLIB:
4067 case BFD_MACH_O_LC_LAZY_LOAD_DYLIB:
4068 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB:
4069 case BFD_MACH_O_LC_ID_DYLIB:
4070 case BFD_MACH_O_LC_REEXPORT_DYLIB:
4071 case BFD_MACH_O_LC_LOAD_UPWARD_DYLIB:
4072 break;
4073 default:
4074 BFD_FAIL ();
4075 return false;
4076 }
4077
4078 if (bfd_read (&raw, sizeof (raw), abfd) != sizeof (raw))
4079 return false;
4080
4081 nameoff = bfd_h_get_32 (abfd, raw.name);
4082 if (nameoff > command->len)
4083 return false;
4084 cmd->timestamp = bfd_h_get_32 (abfd, raw.timestamp);
4085 cmd->current_version = bfd_h_get_32 (abfd, raw.current_version);
4086 cmd->compatibility_version = bfd_h_get_32 (abfd, raw.compatibility_version);
4087
4088 cmd->name_offset = command->offset + nameoff;
4089 namelen = command->len - nameoff;
4090 pos = mdata->hdr_offset + cmd->name_offset;
4091 cmd->name_str = (char *) bfd_mach_o_alloc_and_read (abfd, pos, namelen, 1);
4092 return cmd->name_str != NULL;
4093 }
4094
4095 static bool
4096 bfd_mach_o_read_prebound_dylib (bfd *abfd,
4097 bfd_mach_o_load_command *command)
4098 {
4099 bfd_mach_o_prebound_dylib_command *cmd = &command->command.prebound_dylib;
4100 struct mach_o_prebound_dylib_command_external raw;
4101 unsigned int nameoff;
4102 unsigned int modoff;
4103 unsigned int str_len;
4104 unsigned char *str;
4105
4106 if (command->len < sizeof (raw) + 8)
4107 return false;
4108 if (bfd_read (&raw, sizeof (raw), abfd) != sizeof (raw))
4109 return false;
4110
4111 nameoff = bfd_h_get_32 (abfd, raw.name);
4112 modoff = bfd_h_get_32 (abfd, raw.linked_modules);
4113 if (nameoff > command->len || modoff > command->len)
4114 return false;
4115
4116 str_len = command->len - sizeof (raw);
4117 str = _bfd_alloc_and_read (abfd, str_len, str_len);
4118 if (str == NULL)
4119 return false;
4120
4121 cmd->name_offset = command->offset + nameoff;
4122 cmd->nmodules = bfd_h_get_32 (abfd, raw.nmodules);
4123 cmd->linked_modules_offset = command->offset + modoff;
4124
4125 cmd->name_str = (char *)str + nameoff - (sizeof (raw) + BFD_MACH_O_LC_SIZE);
4126 cmd->linked_modules = str + modoff - (sizeof (raw) + BFD_MACH_O_LC_SIZE);
4127 return true;
4128 }
4129
4130 static bool
4131 bfd_mach_o_read_prebind_cksum (bfd *abfd,
4132 bfd_mach_o_load_command *command)
4133 {
4134 bfd_mach_o_prebind_cksum_command *cmd = &command->command.prebind_cksum;
4135 struct mach_o_prebind_cksum_command_external raw;
4136
4137 if (command->len < sizeof (raw) + 8)
4138 return false;
4139 if (bfd_read (&raw, sizeof (raw), abfd) != sizeof (raw))
4140 return false;
4141
4142 cmd->cksum = bfd_get_32 (abfd, raw.cksum);
4143 return true;
4144 }
4145
4146 static bool
4147 bfd_mach_o_read_twolevel_hints (bfd *abfd,
4148 bfd_mach_o_load_command *command)
4149 {
4150 bfd_mach_o_twolevel_hints_command *cmd = &command->command.twolevel_hints;
4151 struct mach_o_twolevel_hints_command_external raw;
4152
4153 if (command->len < sizeof (raw) + 8)
4154 return false;
4155 if (bfd_read (&raw, sizeof (raw), abfd) != sizeof (raw))
4156 return false;
4157
4158 cmd->offset = bfd_get_32 (abfd, raw.offset);
4159 cmd->nhints = bfd_get_32 (abfd, raw.nhints);
4160 return true;
4161 }
4162
4163 static bool
4164 bfd_mach_o_read_fvmlib (bfd *abfd, bfd_mach_o_load_command *command)
4165 {
4166 bfd_mach_o_fvmlib_command *fvm = &command->command.fvmlib;
4167 struct mach_o_fvmlib_command_external raw;
4168 unsigned int nameoff;
4169 size_t namelen;
4170
4171 if (command->len < sizeof (raw) + 8)
4172 return false;
4173 if (bfd_read (&raw, sizeof (raw), abfd) != sizeof (raw))
4174 return false;
4175
4176 nameoff = bfd_h_get_32 (abfd, raw.name);
4177 if (nameoff > command->len)
4178 return false;
4179 fvm->minor_version = bfd_h_get_32 (abfd, raw.minor_version);
4180 fvm->header_addr = bfd_h_get_32 (abfd, raw.header_addr);
4181
4182 fvm->name_offset = command->offset + nameoff;
4183 namelen = command->len - nameoff;
4184 fvm->name_str = (char *) bfd_mach_o_alloc_and_read (abfd, fvm->name_offset,
4185 namelen, 1);
4186 return fvm->name_str != NULL;
4187 }
4188
4189 static bool
4190 bfd_mach_o_read_thread (bfd *abfd, bfd_mach_o_load_command *command)
4191 {
4192 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
4193 bfd_mach_o_thread_command *cmd = &command->command.thread;
4194 unsigned int offset;
4195 unsigned int nflavours;
4196 unsigned int i;
4197 struct mach_o_thread_command_external raw;
4198 size_t amt;
4199
4200 BFD_ASSERT ((command->type == BFD_MACH_O_LC_THREAD)
4201 || (command->type == BFD_MACH_O_LC_UNIXTHREAD));
4202
4203 /* Count the number of threads. */
4204 offset = 8;
4205 nflavours = 0;
4206 while (offset + sizeof (raw) <= command->len)
4207 {
4208 unsigned int count;
4209
4210 if (bfd_seek (abfd, command->offset + offset, SEEK_SET) != 0
4211 || bfd_read (&raw, sizeof (raw), abfd) != sizeof (raw))
4212 return false;
4213
4214 count = bfd_h_get_32 (abfd, raw.count);
4215 if (count > (unsigned) -1 / 4
4216 || command->len - (offset + sizeof (raw)) < count * 4)
4217 return false;
4218 offset += sizeof (raw) + count * 4;
4219 nflavours++;
4220 }
4221 if (nflavours == 0 || offset != command->len)
4222 return false;
4223
4224 /* Allocate threads. */
4225 if (_bfd_mul_overflow (nflavours, sizeof (bfd_mach_o_thread_flavour), &amt))
4226 {
4227 bfd_set_error (bfd_error_file_too_big);
4228 return false;
4229 }
4230 cmd->flavours = bfd_alloc (abfd, amt);
4231 if (cmd->flavours == NULL)
4232 return false;
4233 cmd->nflavours = nflavours;
4234
4235 offset = 8;
4236 nflavours = 0;
4237 while (offset != command->len)
4238 {
4239 if (bfd_seek (abfd, command->offset + offset, SEEK_SET) != 0
4240 || bfd_read (&raw, sizeof (raw), abfd) != sizeof (raw))
4241 return false;
4242
4243 cmd->flavours[nflavours].flavour = bfd_h_get_32 (abfd, raw.flavour);
4244 cmd->flavours[nflavours].offset = command->offset + offset + sizeof (raw);
4245 cmd->flavours[nflavours].size = bfd_h_get_32 (abfd, raw.count) * 4;
4246 offset += cmd->flavours[nflavours].size + sizeof (raw);
4247 nflavours++;
4248 }
4249
4250 for (i = 0; i < nflavours; i++)
4251 {
4252 asection *bfdsec;
4253 size_t snamelen;
4254 char *sname;
4255 const char *flavourstr;
4256 const char *prefix = "LC_THREAD";
4257 unsigned int j = 0;
4258
4259 switch (mdata->header.cputype)
4260 {
4261 case BFD_MACH_O_CPU_TYPE_POWERPC:
4262 case BFD_MACH_O_CPU_TYPE_POWERPC_64:
4263 flavourstr =
4264 bfd_mach_o_ppc_flavour_string (cmd->flavours[i].flavour);
4265 break;
4266 case BFD_MACH_O_CPU_TYPE_I386:
4267 case BFD_MACH_O_CPU_TYPE_X86_64:
4268 flavourstr =
4269 bfd_mach_o_i386_flavour_string (cmd->flavours[i].flavour);
4270 break;
4271 default:
4272 flavourstr = "UNKNOWN_ARCHITECTURE";
4273 break;
4274 }
4275
4276 snamelen = strlen (prefix) + 1 + 20 + 1 + strlen (flavourstr) + 1;
4277 sname = bfd_alloc (abfd, snamelen);
4278 if (sname == NULL)
4279 return false;
4280
4281 for (;;)
4282 {
4283 sprintf (sname, "%s.%s.%u", prefix, flavourstr, j);
4284 if (bfd_get_section_by_name (abfd, sname) == NULL)
4285 break;
4286 j++;
4287 }
4288
4289 bfdsec = bfd_make_section_with_flags (abfd, sname, SEC_HAS_CONTENTS);
4290
4291 bfdsec->vma = 0;
4292 bfdsec->lma = 0;
4293 bfdsec->size = cmd->flavours[i].size;
4294 bfdsec->filepos = cmd->flavours[i].offset;
4295 bfdsec->alignment_power = 0x0;
4296
4297 cmd->section = bfdsec;
4298 }
4299
4300 return true;
4301 }
4302
4303 static bool
4304 bfd_mach_o_read_dysymtab (bfd *abfd, bfd_mach_o_load_command *command,
4305 ufile_ptr filesize)
4306 {
4307 bfd_mach_o_dysymtab_command *cmd = &command->command.dysymtab;
4308 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
4309
4310 BFD_ASSERT (command->type == BFD_MACH_O_LC_DYSYMTAB);
4311
4312 {
4313 struct mach_o_dysymtab_command_external raw;
4314
4315 if (command->len < sizeof (raw) + 8)
4316 return false;
4317 if (bfd_read (&raw, sizeof (raw), abfd) != sizeof (raw))
4318 return false;
4319
4320 cmd->ilocalsym = bfd_h_get_32 (abfd, raw.ilocalsym);
4321 cmd->nlocalsym = bfd_h_get_32 (abfd, raw.nlocalsym);
4322 cmd->iextdefsym = bfd_h_get_32 (abfd, raw.iextdefsym);
4323 cmd->nextdefsym = bfd_h_get_32 (abfd, raw.nextdefsym);
4324 cmd->iundefsym = bfd_h_get_32 (abfd, raw.iundefsym);
4325 cmd->nundefsym = bfd_h_get_32 (abfd, raw.nundefsym);
4326 cmd->tocoff = bfd_h_get_32 (abfd, raw.tocoff);
4327 cmd->ntoc = bfd_h_get_32 (abfd, raw.ntoc);
4328 cmd->modtaboff = bfd_h_get_32 (abfd, raw.modtaboff);
4329 cmd->nmodtab = bfd_h_get_32 (abfd, raw.nmodtab);
4330 cmd->extrefsymoff = bfd_h_get_32 (abfd, raw.extrefsymoff);
4331 cmd->nextrefsyms = bfd_h_get_32 (abfd, raw.nextrefsyms);
4332 cmd->indirectsymoff = bfd_h_get_32 (abfd, raw.indirectsymoff);
4333 cmd->nindirectsyms = bfd_h_get_32 (abfd, raw.nindirectsyms);
4334 cmd->extreloff = bfd_h_get_32 (abfd, raw.extreloff);
4335 cmd->nextrel = bfd_h_get_32 (abfd, raw.nextrel);
4336 cmd->locreloff = bfd_h_get_32 (abfd, raw.locreloff);
4337 cmd->nlocrel = bfd_h_get_32 (abfd, raw.nlocrel);
4338 }
4339
4340 if (cmd->nmodtab != 0)
4341 {
4342 unsigned int i;
4343 int wide = bfd_mach_o_wide_p (abfd);
4344 unsigned int module_len = wide ? 56 : 52;
4345 size_t amt;
4346
4347 if (cmd->modtaboff > filesize
4348 || cmd->nmodtab > (filesize - cmd->modtaboff) / module_len)
4349 {
4350 bfd_set_error (bfd_error_file_truncated);
4351 return false;
4352 }
4353 if (_bfd_mul_overflow (cmd->nmodtab,
4354 sizeof (bfd_mach_o_dylib_module), &amt))
4355 {
4356 bfd_set_error (bfd_error_file_too_big);
4357 return false;
4358 }
4359 cmd->dylib_module = bfd_alloc (abfd, amt);
4360 if (cmd->dylib_module == NULL)
4361 return false;
4362
4363 if (bfd_seek (abfd, cmd->modtaboff, SEEK_SET) != 0)
4364 return false;
4365
4366 for (i = 0; i < cmd->nmodtab; i++)
4367 {
4368 bfd_mach_o_dylib_module *module = &cmd->dylib_module[i];
4369 unsigned long v;
4370 unsigned char buf[56];
4371
4372 if (bfd_read (buf, module_len, abfd) != module_len)
4373 return false;
4374
4375 module->module_name_idx = bfd_h_get_32 (abfd, buf + 0);
4376 module->iextdefsym = bfd_h_get_32 (abfd, buf + 4);
4377 module->nextdefsym = bfd_h_get_32 (abfd, buf + 8);
4378 module->irefsym = bfd_h_get_32 (abfd, buf + 12);
4379 module->nrefsym = bfd_h_get_32 (abfd, buf + 16);
4380 module->ilocalsym = bfd_h_get_32 (abfd, buf + 20);
4381 module->nlocalsym = bfd_h_get_32 (abfd, buf + 24);
4382 module->iextrel = bfd_h_get_32 (abfd, buf + 28);
4383 module->nextrel = bfd_h_get_32 (abfd, buf + 32);
4384 v = bfd_h_get_32 (abfd, buf +36);
4385 module->iinit = v & 0xffff;
4386 module->iterm = (v >> 16) & 0xffff;
4387 v = bfd_h_get_32 (abfd, buf + 40);
4388 module->ninit = v & 0xffff;
4389 module->nterm = (v >> 16) & 0xffff;
4390 if (wide)
4391 {
4392 module->objc_module_info_size = bfd_h_get_32 (abfd, buf + 44);
4393 module->objc_module_info_addr = bfd_h_get_64 (abfd, buf + 48);
4394 }
4395 else
4396 {
4397 module->objc_module_info_addr = bfd_h_get_32 (abfd, buf + 44);
4398 module->objc_module_info_size = bfd_h_get_32 (abfd, buf + 48);
4399 }
4400 }
4401 }
4402
4403 if (cmd->ntoc != 0)
4404 {
4405 unsigned long i;
4406 size_t amt;
4407 struct mach_o_dylib_table_of_contents_external raw;
4408
4409 if (cmd->tocoff > filesize
4410 || cmd->ntoc > (filesize - cmd->tocoff) / sizeof (raw))
4411 {
4412 bfd_set_error (bfd_error_file_truncated);
4413 return false;
4414 }
4415 if (_bfd_mul_overflow (cmd->ntoc,
4416 sizeof (bfd_mach_o_dylib_table_of_content), &amt))
4417 {
4418 bfd_set_error (bfd_error_file_too_big);
4419 return false;
4420 }
4421 cmd->dylib_toc = bfd_alloc (abfd, amt);
4422 if (cmd->dylib_toc == NULL)
4423 return false;
4424
4425 if (bfd_seek (abfd, cmd->tocoff, SEEK_SET) != 0)
4426 return false;
4427
4428 for (i = 0; i < cmd->ntoc; i++)
4429 {
4430 bfd_mach_o_dylib_table_of_content *toc = &cmd->dylib_toc[i];
4431
4432 if (bfd_read (&raw, sizeof (raw), abfd) != sizeof (raw))
4433 return false;
4434
4435 toc->symbol_index = bfd_h_get_32 (abfd, raw.symbol_index);
4436 toc->module_index = bfd_h_get_32 (abfd, raw.module_index);
4437 }
4438 }
4439
4440 if (cmd->nindirectsyms != 0)
4441 {
4442 unsigned int i;
4443 size_t amt;
4444
4445 if (cmd->indirectsymoff > filesize
4446 || cmd->nindirectsyms > (filesize - cmd->indirectsymoff) / 4)
4447 {
4448 bfd_set_error (bfd_error_file_truncated);
4449 return false;
4450 }
4451 if (_bfd_mul_overflow (cmd->nindirectsyms, sizeof (unsigned int), &amt))
4452 {
4453 bfd_set_error (bfd_error_file_too_big);
4454 return false;
4455 }
4456 cmd->indirect_syms = bfd_alloc (abfd, amt);
4457 if (cmd->indirect_syms == NULL)
4458 return false;
4459
4460 if (bfd_seek (abfd, cmd->indirectsymoff, SEEK_SET) != 0)
4461 return false;
4462
4463 for (i = 0; i < cmd->nindirectsyms; i++)
4464 {
4465 unsigned char raw[4];
4466 unsigned int *is = &cmd->indirect_syms[i];
4467
4468 if (bfd_read (raw, sizeof (raw), abfd) != sizeof (raw))
4469 return false;
4470
4471 *is = bfd_h_get_32 (abfd, raw);
4472 }
4473 }
4474
4475 if (cmd->nextrefsyms != 0)
4476 {
4477 unsigned long v;
4478 unsigned int i;
4479 size_t amt;
4480
4481 if (cmd->extrefsymoff > filesize
4482 || cmd->nextrefsyms > (filesize - cmd->extrefsymoff) / 4)
4483 {
4484 bfd_set_error (bfd_error_file_truncated);
4485 return false;
4486 }
4487 if (_bfd_mul_overflow (cmd->nextrefsyms,
4488 sizeof (bfd_mach_o_dylib_reference), &amt))
4489 {
4490 bfd_set_error (bfd_error_file_too_big);
4491 return false;
4492 }
4493 cmd->ext_refs = bfd_alloc (abfd, amt);
4494 if (cmd->ext_refs == NULL)
4495 return false;
4496
4497 if (bfd_seek (abfd, cmd->extrefsymoff, SEEK_SET) != 0)
4498 return false;
4499
4500 for (i = 0; i < cmd->nextrefsyms; i++)
4501 {
4502 unsigned char raw[4];
4503 bfd_mach_o_dylib_reference *ref = &cmd->ext_refs[i];
4504
4505 if (bfd_read (raw, sizeof (raw), abfd) != sizeof (raw))
4506 return false;
4507
4508 /* Fields isym and flags are written as bit-fields, thus we need
4509 a specific processing for endianness. */
4510 v = bfd_h_get_32 (abfd, raw);
4511 if (bfd_big_endian (abfd))
4512 {
4513 ref->isym = (v >> 8) & 0xffffff;
4514 ref->flags = v & 0xff;
4515 }
4516 else
4517 {
4518 ref->isym = v & 0xffffff;
4519 ref->flags = (v >> 24) & 0xff;
4520 }
4521 }
4522 }
4523
4524 if (mdata->dysymtab)
4525 return false;
4526 mdata->dysymtab = cmd;
4527
4528 return true;
4529 }
4530
4531 static bool
4532 bfd_mach_o_read_symtab (bfd *abfd, bfd_mach_o_load_command *command,
4533 ufile_ptr filesize)
4534 {
4535 bfd_mach_o_symtab_command *symtab = &command->command.symtab;
4536 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
4537 struct mach_o_symtab_command_external raw;
4538
4539 BFD_ASSERT (command->type == BFD_MACH_O_LC_SYMTAB);
4540
4541 if (command->len < sizeof (raw) + 8)
4542 return false;
4543 if (bfd_read (&raw, sizeof (raw), abfd) != sizeof (raw))
4544 return false;
4545
4546 symtab->symoff = bfd_h_get_32 (abfd, raw.symoff);
4547 symtab->nsyms = bfd_h_get_32 (abfd, raw.nsyms);
4548 symtab->stroff = bfd_h_get_32 (abfd, raw.stroff);
4549 symtab->strsize = bfd_h_get_32 (abfd, raw.strsize);
4550 symtab->symbols = NULL;
4551 symtab->strtab = NULL;
4552
4553 if (symtab->symoff > filesize
4554 || symtab->nsyms > (filesize - symtab->symoff) / BFD_MACH_O_NLIST_SIZE
4555 || symtab->stroff > filesize
4556 || symtab->strsize > filesize - symtab->stroff)
4557 {
4558 bfd_set_error (bfd_error_file_truncated);
4559 return false;
4560 }
4561
4562 if (symtab->nsyms != 0)
4563 abfd->flags |= HAS_SYMS;
4564
4565 if (mdata->symtab)
4566 return false;
4567 mdata->symtab = symtab;
4568 return true;
4569 }
4570
4571 static bool
4572 bfd_mach_o_read_uuid (bfd *abfd, bfd_mach_o_load_command *command)
4573 {
4574 bfd_mach_o_uuid_command *cmd = &command->command.uuid;
4575
4576 BFD_ASSERT (command->type == BFD_MACH_O_LC_UUID);
4577
4578 if (command->len < 16 + 8)
4579 return false;
4580 if (bfd_read (cmd->uuid, 16, abfd) != 16)
4581 return false;
4582
4583 return true;
4584 }
4585
4586 static bool
4587 bfd_mach_o_read_linkedit (bfd *abfd, bfd_mach_o_load_command *command)
4588 {
4589 bfd_mach_o_linkedit_command *cmd = &command->command.linkedit;
4590 struct mach_o_linkedit_data_command_external raw;
4591
4592 if (command->len < sizeof (raw) + 8)
4593 return false;
4594 if (bfd_read (&raw, sizeof (raw), abfd) != sizeof (raw))
4595 return false;
4596
4597 cmd->dataoff = bfd_get_32 (abfd, raw.dataoff);
4598 cmd->datasize = bfd_get_32 (abfd, raw.datasize);
4599 return true;
4600 }
4601
4602 static bool
4603 bfd_mach_o_read_str (bfd *abfd, bfd_mach_o_load_command *command)
4604 {
4605 bfd_mach_o_str_command *cmd = &command->command.str;
4606 struct mach_o_str_command_external raw;
4607 unsigned long off;
4608
4609 if (command->len < sizeof (raw) + 8)
4610 return false;
4611 if (bfd_read (&raw, sizeof (raw), abfd) != sizeof (raw))
4612 return false;
4613
4614 off = bfd_get_32 (abfd, raw.str);
4615 if (off > command->len)
4616 return false;
4617
4618 cmd->stroff = command->offset + off;
4619 cmd->str_len = command->len - off;
4620 cmd->str = (char *) bfd_mach_o_alloc_and_read (abfd, cmd->stroff,
4621 cmd->str_len, 1);
4622 return cmd->str != NULL;
4623 }
4624
4625 static bool
4626 bfd_mach_o_read_dyld_content (bfd *abfd, bfd_mach_o_dyld_info_command *cmd)
4627 {
4628 /* Read rebase content. */
4629 if (cmd->rebase_content == NULL && cmd->rebase_size != 0)
4630 {
4631 cmd->rebase_content
4632 = bfd_mach_o_alloc_and_read (abfd, cmd->rebase_off,
4633 cmd->rebase_size, 0);
4634 if (cmd->rebase_content == NULL)
4635 return false;
4636 }
4637
4638 /* Read bind content. */
4639 if (cmd->bind_content == NULL && cmd->bind_size != 0)
4640 {
4641 cmd->bind_content
4642 = bfd_mach_o_alloc_and_read (abfd, cmd->bind_off,
4643 cmd->bind_size, 0);
4644 if (cmd->bind_content == NULL)
4645 return false;
4646 }
4647
4648 /* Read weak bind content. */
4649 if (cmd->weak_bind_content == NULL && cmd->weak_bind_size != 0)
4650 {
4651 cmd->weak_bind_content
4652 = bfd_mach_o_alloc_and_read (abfd, cmd->weak_bind_off,
4653 cmd->weak_bind_size, 0);
4654 if (cmd->weak_bind_content == NULL)
4655 return false;
4656 }
4657
4658 /* Read lazy bind content. */
4659 if (cmd->lazy_bind_content == NULL && cmd->lazy_bind_size != 0)
4660 {
4661 cmd->lazy_bind_content
4662 = bfd_mach_o_alloc_and_read (abfd, cmd->lazy_bind_off,
4663 cmd->lazy_bind_size, 0);
4664 if (cmd->lazy_bind_content == NULL)
4665 return false;
4666 }
4667
4668 /* Read export content. */
4669 if (cmd->export_content == NULL && cmd->export_size != 0)
4670 {
4671 cmd->export_content
4672 = bfd_mach_o_alloc_and_read (abfd, cmd->export_off,
4673 cmd->export_size, 0);
4674 if (cmd->export_content == NULL)
4675 return false;
4676 }
4677
4678 return true;
4679 }
4680
4681 static bool
4682 bfd_mach_o_read_dyld_info (bfd *abfd, bfd_mach_o_load_command *command)
4683 {
4684 bfd_mach_o_dyld_info_command *cmd = &command->command.dyld_info;
4685 struct mach_o_dyld_info_command_external raw;
4686
4687 if (command->len < sizeof (raw) + 8)
4688 return false;
4689 if (bfd_read (&raw, sizeof (raw), abfd) != sizeof (raw))
4690 return false;
4691
4692 cmd->rebase_off = bfd_get_32 (abfd, raw.rebase_off);
4693 cmd->rebase_size = bfd_get_32 (abfd, raw.rebase_size);
4694 cmd->rebase_content = NULL;
4695 cmd->bind_off = bfd_get_32 (abfd, raw.bind_off);
4696 cmd->bind_size = bfd_get_32 (abfd, raw.bind_size);
4697 cmd->bind_content = NULL;
4698 cmd->weak_bind_off = bfd_get_32 (abfd, raw.weak_bind_off);
4699 cmd->weak_bind_size = bfd_get_32 (abfd, raw.weak_bind_size);
4700 cmd->weak_bind_content = NULL;
4701 cmd->lazy_bind_off = bfd_get_32 (abfd, raw.lazy_bind_off);
4702 cmd->lazy_bind_size = bfd_get_32 (abfd, raw.lazy_bind_size);
4703 cmd->lazy_bind_content = NULL;
4704 cmd->export_off = bfd_get_32 (abfd, raw.export_off);
4705 cmd->export_size = bfd_get_32 (abfd, raw.export_size);
4706 cmd->export_content = NULL;
4707 return true;
4708 }
4709
4710 static bool
4711 bfd_mach_o_read_version_min (bfd *abfd, bfd_mach_o_load_command *command)
4712 {
4713 bfd_mach_o_version_min_command *cmd = &command->command.version_min;
4714 struct mach_o_version_min_command_external raw;
4715
4716 if (command->len < sizeof (raw) + 8)
4717 return false;
4718 if (bfd_read (&raw, sizeof (raw), abfd) != sizeof (raw))
4719 return false;
4720
4721 cmd->version = bfd_get_32 (abfd, raw.version);
4722 cmd->sdk = bfd_get_32 (abfd, raw.sdk);
4723 return true;
4724 }
4725
4726 static bool
4727 bfd_mach_o_read_encryption_info (bfd *abfd, bfd_mach_o_load_command *command)
4728 {
4729 bfd_mach_o_encryption_info_command *cmd = &command->command.encryption_info;
4730 struct mach_o_encryption_info_command_external raw;
4731
4732 if (command->len < sizeof (raw) + 8)
4733 return false;
4734 if (bfd_read (&raw, sizeof (raw), abfd) != sizeof (raw))
4735 return false;
4736
4737 cmd->cryptoff = bfd_get_32 (abfd, raw.cryptoff);
4738 cmd->cryptsize = bfd_get_32 (abfd, raw.cryptsize);
4739 cmd->cryptid = bfd_get_32 (abfd, raw.cryptid);
4740 return true;
4741 }
4742
4743 static bool
4744 bfd_mach_o_read_encryption_info_64 (bfd *abfd, bfd_mach_o_load_command *command)
4745 {
4746 bfd_mach_o_encryption_info_command *cmd = &command->command.encryption_info;
4747 struct mach_o_encryption_info_64_command_external raw;
4748
4749 if (command->len < sizeof (raw) + 8)
4750 return false;
4751 if (bfd_read (&raw, sizeof (raw), abfd) != sizeof (raw))
4752 return false;
4753
4754 cmd->cryptoff = bfd_get_32 (abfd, raw.cryptoff);
4755 cmd->cryptsize = bfd_get_32 (abfd, raw.cryptsize);
4756 cmd->cryptid = bfd_get_32 (abfd, raw.cryptid);
4757 return true;
4758 }
4759
4760 static bool
4761 bfd_mach_o_read_main (bfd *abfd, bfd_mach_o_load_command *command)
4762 {
4763 bfd_mach_o_main_command *cmd = &command->command.main;
4764 struct mach_o_entry_point_command_external raw;
4765
4766 if (command->len < sizeof (raw) + 8)
4767 return false;
4768 if (bfd_read (&raw, sizeof (raw), abfd) != sizeof (raw))
4769 return false;
4770
4771 cmd->entryoff = bfd_get_64 (abfd, raw.entryoff);
4772 cmd->stacksize = bfd_get_64 (abfd, raw.stacksize);
4773 return true;
4774 }
4775
4776 static bool
4777 bfd_mach_o_read_source_version (bfd *abfd, bfd_mach_o_load_command *command)
4778 {
4779 bfd_mach_o_source_version_command *cmd = &command->command.source_version;
4780 struct mach_o_source_version_command_external raw;
4781 uint64_t ver;
4782
4783 if (command->len < sizeof (raw) + 8)
4784 return false;
4785 if (bfd_read (&raw, sizeof (raw), abfd) != sizeof (raw))
4786 return false;
4787
4788 ver = bfd_get_64 (abfd, raw.version);
4789 /* Note: we use a serie of shift to avoid shift > 32 (for which gcc
4790 generates warnings) in case of the host doesn't support 64 bit
4791 integers. */
4792 cmd->e = ver & 0x3ff;
4793 ver >>= 10;
4794 cmd->d = ver & 0x3ff;
4795 ver >>= 10;
4796 cmd->c = ver & 0x3ff;
4797 ver >>= 10;
4798 cmd->b = ver & 0x3ff;
4799 ver >>= 10;
4800 cmd->a = ver & 0xffffff;
4801 return true;
4802 }
4803
4804 static bool
4805 bfd_mach_o_read_note (bfd *abfd, bfd_mach_o_load_command *command)
4806 {
4807 bfd_mach_o_note_command *cmd = &command->command.note;
4808 struct mach_o_note_command_external raw;
4809
4810 if (command->len < sizeof (raw) + 8)
4811 return false;
4812 if (bfd_read (&raw, sizeof (raw), abfd) != sizeof (raw))
4813 return false;
4814
4815 memcpy (cmd->data_owner, raw.data_owner, 16);
4816 cmd->offset = bfd_get_64 (abfd, raw.offset);
4817 cmd->size = bfd_get_64 (abfd, raw.size);
4818 return true;
4819 }
4820
4821 static bool
4822 bfd_mach_o_read_build_version (bfd *abfd, bfd_mach_o_load_command *command)
4823 {
4824 bfd_mach_o_build_version_command *cmd = &command->command.build_version;
4825 struct mach_o_build_version_command_external raw;
4826
4827 if (command->len < sizeof (raw) + 8)
4828 return false;
4829 if (bfd_read (&raw, sizeof (raw), abfd) != sizeof (raw))
4830 return false;
4831
4832 cmd->platform = bfd_get_32 (abfd, raw.platform);
4833 cmd->minos = bfd_get_32 (abfd, raw.minos);
4834 cmd->sdk = bfd_get_32 (abfd, raw.sdk);
4835 cmd->ntools = bfd_get_32 (abfd, raw.ntools);
4836 return true;
4837 }
4838
4839 static bool
4840 bfd_mach_o_read_segment (bfd *abfd,
4841 bfd_mach_o_load_command *command,
4842 unsigned int wide)
4843 {
4844 bfd_mach_o_segment_command *seg = &command->command.segment;
4845 unsigned long i;
4846
4847 if (wide)
4848 {
4849 struct mach_o_segment_command_64_external raw;
4850
4851 BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT_64);
4852
4853 if (command->len < sizeof (raw) + 8)
4854 return false;
4855 if (bfd_read (&raw, sizeof (raw), abfd) != sizeof (raw))
4856 return false;
4857
4858 memcpy (seg->segname, raw.segname, 16);
4859 seg->segname[16] = '\0';
4860
4861 seg->vmaddr = bfd_h_get_64 (abfd, raw.vmaddr);
4862 seg->vmsize = bfd_h_get_64 (abfd, raw.vmsize);
4863 seg->fileoff = bfd_h_get_64 (abfd, raw.fileoff);
4864 seg->filesize = bfd_h_get_64 (abfd, raw.filesize);
4865 seg->maxprot = bfd_h_get_32 (abfd, raw.maxprot);
4866 seg->initprot = bfd_h_get_32 (abfd, raw.initprot);
4867 seg->nsects = bfd_h_get_32 (abfd, raw.nsects);
4868 seg->flags = bfd_h_get_32 (abfd, raw.flags);
4869 }
4870 else
4871 {
4872 struct mach_o_segment_command_32_external raw;
4873
4874 BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT);
4875
4876 if (command->len < sizeof (raw) + 8)
4877 return false;
4878 if (bfd_read (&raw, sizeof (raw), abfd) != sizeof (raw))
4879 return false;
4880
4881 memcpy (seg->segname, raw.segname, 16);
4882 seg->segname[16] = '\0';
4883
4884 seg->vmaddr = bfd_h_get_32 (abfd, raw.vmaddr);
4885 seg->vmsize = bfd_h_get_32 (abfd, raw.vmsize);
4886 seg->fileoff = bfd_h_get_32 (abfd, raw.fileoff);
4887 seg->filesize = bfd_h_get_32 (abfd, raw.filesize);
4888 seg->maxprot = bfd_h_get_32 (abfd, raw.maxprot);
4889 seg->initprot = bfd_h_get_32 (abfd, raw.initprot);
4890 seg->nsects = bfd_h_get_32 (abfd, raw.nsects);
4891 seg->flags = bfd_h_get_32 (abfd, raw.flags);
4892 }
4893 seg->sect_head = NULL;
4894 seg->sect_tail = NULL;
4895
4896 for (i = 0; i < seg->nsects; i++)
4897 {
4898 asection *sec;
4899
4900 sec = bfd_mach_o_read_section (abfd, seg->initprot, wide);
4901 if (sec == NULL)
4902 return false;
4903
4904 bfd_mach_o_append_section_to_segment
4905 (seg, bfd_mach_o_get_mach_o_section (sec));
4906 }
4907
4908 return true;
4909 }
4910
4911 static bool
4912 bfd_mach_o_read_segment_32 (bfd *abfd, bfd_mach_o_load_command *command)
4913 {
4914 return bfd_mach_o_read_segment (abfd, command, 0);
4915 }
4916
4917 static bool
4918 bfd_mach_o_read_segment_64 (bfd *abfd, bfd_mach_o_load_command *command)
4919 {
4920 return bfd_mach_o_read_segment (abfd, command, 1);
4921 }
4922
4923 static bool
4924 bfd_mach_o_read_command (bfd *abfd, bfd_mach_o_load_command *command,
4925 ufile_ptr filesize)
4926 {
4927 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
4928 struct mach_o_load_command_external raw;
4929 unsigned int cmd;
4930
4931 /* Read command type and length. */
4932 if (bfd_seek (abfd, mdata->hdr_offset + command->offset, SEEK_SET) != 0
4933 || bfd_read (&raw, BFD_MACH_O_LC_SIZE, abfd) != BFD_MACH_O_LC_SIZE)
4934 return false;
4935
4936 cmd = bfd_h_get_32 (abfd, raw.cmd);
4937 command->type = cmd & ~BFD_MACH_O_LC_REQ_DYLD;
4938 command->type_required = (cmd & BFD_MACH_O_LC_REQ_DYLD) != 0;
4939 command->len = bfd_h_get_32 (abfd, raw.cmdsize);
4940 if (command->len < 8 || command->len % 4 != 0)
4941 return false;
4942
4943 switch (command->type)
4944 {
4945 case BFD_MACH_O_LC_SEGMENT:
4946 if (!bfd_mach_o_read_segment_32 (abfd, command))
4947 return false;
4948 break;
4949 case BFD_MACH_O_LC_SEGMENT_64:
4950 if (!bfd_mach_o_read_segment_64 (abfd, command))
4951 return false;
4952 break;
4953 case BFD_MACH_O_LC_SYMTAB:
4954 if (!bfd_mach_o_read_symtab (abfd, command, filesize))
4955 return false;
4956 break;
4957 case BFD_MACH_O_LC_SYMSEG:
4958 break;
4959 case BFD_MACH_O_LC_THREAD:
4960 case BFD_MACH_O_LC_UNIXTHREAD:
4961 if (!bfd_mach_o_read_thread (abfd, command))
4962 return false;
4963 break;
4964 case BFD_MACH_O_LC_LOAD_DYLINKER:
4965 case BFD_MACH_O_LC_ID_DYLINKER:
4966 case BFD_MACH_O_LC_DYLD_ENVIRONMENT:
4967 if (!bfd_mach_o_read_dylinker (abfd, command))
4968 return false;
4969 break;
4970 case BFD_MACH_O_LC_LOAD_DYLIB:
4971 case BFD_MACH_O_LC_LAZY_LOAD_DYLIB:
4972 case BFD_MACH_O_LC_ID_DYLIB:
4973 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB:
4974 case BFD_MACH_O_LC_REEXPORT_DYLIB:
4975 case BFD_MACH_O_LC_LOAD_UPWARD_DYLIB:
4976 if (!bfd_mach_o_read_dylib (abfd, command))
4977 return false;
4978 break;
4979 case BFD_MACH_O_LC_PREBOUND_DYLIB:
4980 if (!bfd_mach_o_read_prebound_dylib (abfd, command))
4981 return false;
4982 break;
4983 case BFD_MACH_O_LC_LOADFVMLIB:
4984 case BFD_MACH_O_LC_IDFVMLIB:
4985 if (!bfd_mach_o_read_fvmlib (abfd, command))
4986 return false;
4987 break;
4988 case BFD_MACH_O_LC_IDENT:
4989 case BFD_MACH_O_LC_FVMFILE:
4990 case BFD_MACH_O_LC_PREPAGE:
4991 case BFD_MACH_O_LC_ROUTINES:
4992 case BFD_MACH_O_LC_ROUTINES_64:
4993 break;
4994 case BFD_MACH_O_LC_SUB_FRAMEWORK:
4995 case BFD_MACH_O_LC_SUB_UMBRELLA:
4996 case BFD_MACH_O_LC_SUB_LIBRARY:
4997 case BFD_MACH_O_LC_SUB_CLIENT:
4998 case BFD_MACH_O_LC_RPATH:
4999 if (!bfd_mach_o_read_str (abfd, command))
5000 return false;
5001 break;
5002 case BFD_MACH_O_LC_DYSYMTAB:
5003 if (!bfd_mach_o_read_dysymtab (abfd, command, filesize))
5004 return false;
5005 break;
5006 case BFD_MACH_O_LC_PREBIND_CKSUM:
5007 if (!bfd_mach_o_read_prebind_cksum (abfd, command))
5008 return false;
5009 break;
5010 case BFD_MACH_O_LC_TWOLEVEL_HINTS:
5011 if (!bfd_mach_o_read_twolevel_hints (abfd, command))
5012 return false;
5013 break;
5014 case BFD_MACH_O_LC_UUID:
5015 if (!bfd_mach_o_read_uuid (abfd, command))
5016 return false;
5017 break;
5018 case BFD_MACH_O_LC_CODE_SIGNATURE:
5019 case BFD_MACH_O_LC_SEGMENT_SPLIT_INFO:
5020 case BFD_MACH_O_LC_FUNCTION_STARTS:
5021 case BFD_MACH_O_LC_DATA_IN_CODE:
5022 case BFD_MACH_O_LC_DYLIB_CODE_SIGN_DRS:
5023 case BFD_MACH_O_LC_LINKER_OPTIMIZATION_HINT:
5024 case BFD_MACH_O_LC_DYLD_EXPORTS_TRIE:
5025 case BFD_MACH_O_LC_DYLD_CHAINED_FIXUPS:
5026 if (!bfd_mach_o_read_linkedit (abfd, command))
5027 return false;
5028 break;
5029 case BFD_MACH_O_LC_ENCRYPTION_INFO:
5030 if (!bfd_mach_o_read_encryption_info (abfd, command))
5031 return false;
5032 break;
5033 case BFD_MACH_O_LC_ENCRYPTION_INFO_64:
5034 if (!bfd_mach_o_read_encryption_info_64 (abfd, command))
5035 return false;
5036 break;
5037 case BFD_MACH_O_LC_DYLD_INFO:
5038 if (!bfd_mach_o_read_dyld_info (abfd, command))
5039 return false;
5040 break;
5041 case BFD_MACH_O_LC_VERSION_MIN_MACOSX:
5042 case BFD_MACH_O_LC_VERSION_MIN_IPHONEOS:
5043 case BFD_MACH_O_LC_VERSION_MIN_WATCHOS:
5044 case BFD_MACH_O_LC_VERSION_MIN_TVOS:
5045 if (!bfd_mach_o_read_version_min (abfd, command))
5046 return false;
5047 break;
5048 case BFD_MACH_O_LC_MAIN:
5049 if (!bfd_mach_o_read_main (abfd, command))
5050 return false;
5051 break;
5052 case BFD_MACH_O_LC_SOURCE_VERSION:
5053 if (!bfd_mach_o_read_source_version (abfd, command))
5054 return false;
5055 break;
5056 case BFD_MACH_O_LC_LINKER_OPTIONS:
5057 break;
5058 case BFD_MACH_O_LC_NOTE:
5059 if (!bfd_mach_o_read_note (abfd, command))
5060 return false;
5061 break;
5062 case BFD_MACH_O_LC_BUILD_VERSION:
5063 if (!bfd_mach_o_read_build_version (abfd, command))
5064 return false;
5065 break;
5066 default:
5067 command->len = 0;
5068 _bfd_error_handler (_("%pB: unknown load command %#x"),
5069 abfd, command->type);
5070 return false;
5071 }
5072
5073 return true;
5074 }
5075
5076 static bool
5077 bfd_mach_o_flatten_sections (bfd *abfd)
5078 {
5079 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
5080 bfd_mach_o_load_command *cmd;
5081 unsigned long csect;
5082 size_t amt;
5083
5084 /* Count total number of sections. */
5085 mdata->nsects = 0;
5086
5087 for (cmd = mdata->first_command; cmd != NULL; cmd = cmd->next)
5088 {
5089 if (cmd->type == BFD_MACH_O_LC_SEGMENT
5090 || cmd->type == BFD_MACH_O_LC_SEGMENT_64)
5091 {
5092 bfd_mach_o_segment_command *seg = &cmd->command.segment;
5093
5094 mdata->nsects += seg->nsects;
5095 }
5096 }
5097
5098 /* Allocate sections array. */
5099 if (_bfd_mul_overflow (mdata->nsects, sizeof (bfd_mach_o_section *), &amt))
5100 {
5101 bfd_set_error (bfd_error_file_too_big);
5102 return false;
5103 }
5104 mdata->sections = bfd_alloc (abfd, amt);
5105 if (mdata->sections == NULL && mdata->nsects != 0)
5106 return false;
5107
5108 /* Fill the array. */
5109 csect = 0;
5110
5111 for (cmd = mdata->first_command; cmd != NULL; cmd = cmd->next)
5112 {
5113 if (cmd->type == BFD_MACH_O_LC_SEGMENT
5114 || cmd->type == BFD_MACH_O_LC_SEGMENT_64)
5115 {
5116 bfd_mach_o_segment_command *seg = &cmd->command.segment;
5117 bfd_mach_o_section *sec;
5118
5119 BFD_ASSERT (csect + seg->nsects <= mdata->nsects);
5120
5121 for (sec = seg->sect_head; sec != NULL; sec = sec->next)
5122 mdata->sections[csect++] = sec;
5123 }
5124 }
5125 BFD_ASSERT (mdata->nsects == csect);
5126 return true;
5127 }
5128
5129 static bool
5130 bfd_mach_o_scan_start_address (bfd *abfd)
5131 {
5132 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
5133 bfd_mach_o_thread_command *thr = NULL;
5134 bfd_mach_o_load_command *cmd;
5135 unsigned long i;
5136
5137 for (cmd = mdata->first_command; cmd != NULL; cmd = cmd->next)
5138 if (cmd->type == BFD_MACH_O_LC_THREAD
5139 || cmd->type == BFD_MACH_O_LC_UNIXTHREAD)
5140 {
5141 thr = &cmd->command.thread;
5142 break;
5143 }
5144 else if (cmd->type == BFD_MACH_O_LC_MAIN && mdata->nsects > 1)
5145 {
5146 bfd_mach_o_main_command *main_cmd = &cmd->command.main;
5147 bfd_mach_o_section *text_sect = mdata->sections[0];
5148
5149 if (text_sect)
5150 {
5151 abfd->start_address = main_cmd->entryoff
5152 + (text_sect->addr - text_sect->offset);
5153 return true;
5154 }
5155 }
5156
5157 /* An object file has no start address, so do not fail if not found. */
5158 if (thr == NULL)
5159 return true;
5160
5161 /* FIXME: create a subtarget hook ? */
5162 for (i = 0; i < thr->nflavours; i++)
5163 {
5164 if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_I386)
5165 && (thr->flavours[i].flavour == BFD_MACH_O_x86_THREAD_STATE32))
5166 {
5167 unsigned char buf[4];
5168
5169 if (bfd_seek (abfd, thr->flavours[i].offset + 40, SEEK_SET) != 0
5170 || bfd_read (buf, 4, abfd) != 4)
5171 return false;
5172
5173 abfd->start_address = bfd_h_get_32 (abfd, buf);
5174 }
5175 else if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_POWERPC)
5176 && (thr->flavours[i].flavour == BFD_MACH_O_PPC_THREAD_STATE))
5177 {
5178 unsigned char buf[4];
5179
5180 if (bfd_seek (abfd, thr->flavours[i].offset + 0, SEEK_SET) != 0
5181 || bfd_read (buf, 4, abfd) != 4)
5182 return false;
5183
5184 abfd->start_address = bfd_h_get_32 (abfd, buf);
5185 }
5186 else if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_POWERPC_64)
5187 && (thr->flavours[i].flavour == BFD_MACH_O_PPC_THREAD_STATE64))
5188 {
5189 unsigned char buf[8];
5190
5191 if (bfd_seek (abfd, thr->flavours[i].offset + 0, SEEK_SET) != 0
5192 || bfd_read (buf, 8, abfd) != 8)
5193 return false;
5194
5195 abfd->start_address = bfd_h_get_64 (abfd, buf);
5196 }
5197 else if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_X86_64)
5198 && (thr->flavours[i].flavour == BFD_MACH_O_x86_THREAD_STATE64))
5199 {
5200 unsigned char buf[8];
5201
5202 if (bfd_seek (abfd, thr->flavours[i].offset + (16 * 8), SEEK_SET) != 0
5203 || bfd_read (buf, 8, abfd) != 8)
5204 return false;
5205
5206 abfd->start_address = bfd_h_get_64 (abfd, buf);
5207 }
5208 }
5209
5210 return true;
5211 }
5212
5213 bool
5214 bfd_mach_o_set_arch_mach (bfd *abfd,
5215 enum bfd_architecture arch,
5216 unsigned long machine)
5217 {
5218 bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
5219
5220 /* If this isn't the right architecture for this backend, and this
5221 isn't the generic backend, fail. */
5222 if (arch != bed->arch
5223 && arch != bfd_arch_unknown
5224 && bed->arch != bfd_arch_unknown)
5225 return false;
5226
5227 return bfd_default_set_arch_mach (abfd, arch, machine);
5228 }
5229
5230 static bool
5231 bfd_mach_o_scan (bfd *abfd,
5232 bfd_mach_o_header *header,
5233 bfd_mach_o_data_struct *mdata)
5234 {
5235 unsigned int i;
5236 enum bfd_architecture cpu_type;
5237 unsigned long cpu_subtype;
5238 unsigned int hdrsize;
5239
5240 hdrsize = mach_o_wide_p (header) ?
5241 BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE;
5242
5243 mdata->header = *header;
5244
5245 abfd->flags = abfd->flags & BFD_IN_MEMORY;
5246 switch (header->filetype)
5247 {
5248 case BFD_MACH_O_MH_OBJECT:
5249 abfd->flags |= HAS_RELOC;
5250 break;
5251 case BFD_MACH_O_MH_EXECUTE:
5252 abfd->flags |= EXEC_P;
5253 break;
5254 case BFD_MACH_O_MH_DYLIB:
5255 case BFD_MACH_O_MH_BUNDLE:
5256 abfd->flags |= DYNAMIC;
5257 break;
5258 }
5259
5260 bfd_mach_o_convert_architecture (header->cputype, header->cpusubtype,
5261 &cpu_type, &cpu_subtype);
5262 if (cpu_type == bfd_arch_unknown)
5263 {
5264 _bfd_error_handler
5265 /* xgettext:c-format */
5266 (_("bfd_mach_o_scan: unknown architecture 0x%lx/0x%lx"),
5267 header->cputype, header->cpusubtype);
5268 return false;
5269 }
5270
5271 bfd_set_arch_mach (abfd, cpu_type, cpu_subtype);
5272
5273 if (header->ncmds != 0)
5274 {
5275 bfd_mach_o_load_command *cmd;
5276 size_t amt;
5277 ufile_ptr filesize = bfd_get_file_size (abfd);
5278
5279 if (filesize == 0)
5280 filesize = (ufile_ptr) -1;
5281
5282 mdata->first_command = NULL;
5283 mdata->last_command = NULL;
5284
5285 if (header->ncmds > (filesize - hdrsize) / BFD_MACH_O_LC_SIZE)
5286 {
5287 bfd_set_error (bfd_error_file_truncated);
5288 return false;
5289 }
5290 if (_bfd_mul_overflow (header->ncmds,
5291 sizeof (bfd_mach_o_load_command), &amt))
5292 {
5293 bfd_set_error (bfd_error_file_too_big);
5294 return false;
5295 }
5296 cmd = bfd_alloc (abfd, amt);
5297 if (cmd == NULL)
5298 return false;
5299
5300 for (i = 0; i < header->ncmds; i++)
5301 {
5302 bfd_mach_o_load_command *cur = &cmd[i];
5303
5304 bfd_mach_o_append_command (abfd, cur);
5305
5306 if (i == 0)
5307 cur->offset = hdrsize;
5308 else
5309 {
5310 bfd_mach_o_load_command *prev = &cmd[i - 1];
5311 cur->offset = prev->offset + prev->len;
5312 }
5313
5314 if (!bfd_mach_o_read_command (abfd, cur, filesize))
5315 {
5316 bfd_set_error (bfd_error_wrong_format);
5317 return false;
5318 }
5319 }
5320 }
5321
5322 /* Sections should be flatten before scanning start address. */
5323 if (!bfd_mach_o_flatten_sections (abfd))
5324 return false;
5325 if (!bfd_mach_o_scan_start_address (abfd))
5326 return false;
5327
5328 return true;
5329 }
5330
5331 bool
5332 bfd_mach_o_mkobject_init (bfd *abfd)
5333 {
5334 bfd_mach_o_data_struct *mdata = NULL;
5335
5336 mdata = bfd_zalloc (abfd, sizeof (bfd_mach_o_data_struct));
5337 if (mdata == NULL)
5338 return false;
5339 abfd->tdata.mach_o_data = mdata;
5340
5341 mdata->header.magic = 0;
5342 mdata->header.cputype = 0;
5343 mdata->header.cpusubtype = 0;
5344 mdata->header.filetype = 0;
5345 mdata->header.ncmds = 0;
5346 mdata->header.sizeofcmds = 0;
5347 mdata->header.flags = 0;
5348 mdata->header.byteorder = BFD_ENDIAN_UNKNOWN;
5349 mdata->first_command = NULL;
5350 mdata->last_command = NULL;
5351 mdata->nsects = 0;
5352 mdata->sections = NULL;
5353 mdata->dyn_reloc_cache = NULL;
5354
5355 return true;
5356 }
5357
5358 static bool
5359 bfd_mach_o_gen_mkobject (bfd *abfd)
5360 {
5361 bfd_mach_o_data_struct *mdata;
5362
5363 if (!bfd_mach_o_mkobject_init (abfd))
5364 return false;
5365
5366 mdata = bfd_mach_o_get_data (abfd);
5367 mdata->header.magic = BFD_MACH_O_MH_MAGIC;
5368 mdata->header.cputype = 0;
5369 mdata->header.cpusubtype = 0;
5370 mdata->header.byteorder = abfd->xvec->byteorder;
5371 mdata->header.version = 1;
5372
5373 return true;
5374 }
5375
5376 bfd_cleanup
5377 bfd_mach_o_header_p (bfd *abfd,
5378 file_ptr hdr_off,
5379 bfd_mach_o_filetype file_type,
5380 bfd_mach_o_cpu_type cpu_type)
5381 {
5382 bfd_mach_o_header header;
5383 bfd_mach_o_data_struct *mdata;
5384
5385 if (!bfd_mach_o_read_header (abfd, hdr_off, &header))
5386 goto wrong;
5387
5388 if (! (header.byteorder == BFD_ENDIAN_BIG
5389 || header.byteorder == BFD_ENDIAN_LITTLE))
5390 {
5391 _bfd_error_handler (_("unknown header byte-order value %#x"),
5392 header.byteorder);
5393 goto wrong;
5394 }
5395
5396 if (! ((header.byteorder == BFD_ENDIAN_BIG
5397 && abfd->xvec->byteorder == BFD_ENDIAN_BIG
5398 && abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
5399 || (header.byteorder == BFD_ENDIAN_LITTLE
5400 && abfd->xvec->byteorder == BFD_ENDIAN_LITTLE
5401 && abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)))
5402 goto wrong;
5403
5404 /* Check cputype and filetype.
5405 In case of wildcard, do not accept magics that are handled by existing
5406 targets. */
5407 if (cpu_type)
5408 {
5409 if (header.cputype != cpu_type)
5410 goto wrong;
5411 }
5412 else
5413 {
5414 #ifndef BFD64
5415 /* Do not recognize 64 architectures if not configured for 64bit targets.
5416 This could happen only for generic targets. */
5417 if (mach_o_wide_p (&header))
5418 goto wrong;
5419 #endif
5420 }
5421
5422 if (file_type)
5423 {
5424 if (header.filetype != file_type)
5425 goto wrong;
5426 }
5427 else
5428 {
5429 switch (header.filetype)
5430 {
5431 case BFD_MACH_O_MH_CORE:
5432 /* Handled by core_p */
5433 goto wrong;
5434 default:
5435 break;
5436 }
5437 }
5438
5439 mdata = (bfd_mach_o_data_struct *) bfd_zalloc (abfd, sizeof (*mdata));
5440 if (mdata == NULL)
5441 return NULL;
5442 abfd->tdata.mach_o_data = mdata;
5443
5444 mdata->hdr_offset = hdr_off;
5445
5446 if (!bfd_mach_o_scan (abfd, &header, mdata))
5447 {
5448 bfd_release (abfd, mdata);
5449 return NULL;
5450 }
5451
5452 return _bfd_no_cleanup;
5453
5454 wrong:
5455 bfd_set_error (bfd_error_wrong_format);
5456 return NULL;
5457 }
5458
5459 static bfd_cleanup
5460 bfd_mach_o_gen_object_p (bfd *abfd)
5461 {
5462 return bfd_mach_o_header_p (abfd, 0, 0, 0);
5463 }
5464
5465 static bfd_cleanup
5466 bfd_mach_o_gen_core_p (bfd *abfd)
5467 {
5468 return bfd_mach_o_header_p (abfd, 0, BFD_MACH_O_MH_CORE, 0);
5469 }
5470
5471 /* Return the base address of ABFD, ie the address at which the image is
5472 mapped. The possible initial pagezero is ignored. */
5473
5474 bfd_vma
5475 bfd_mach_o_get_base_address (bfd *abfd)
5476 {
5477 bfd_mach_o_data_struct *mdata;
5478 bfd_mach_o_load_command *cmd;
5479
5480 /* Check for Mach-O. */
5481 if (!bfd_mach_o_valid (abfd))
5482 return 0;
5483 mdata = bfd_mach_o_get_data (abfd);
5484
5485 for (cmd = mdata->first_command; cmd != NULL; cmd = cmd->next)
5486 {
5487 if ((cmd->type == BFD_MACH_O_LC_SEGMENT
5488 || cmd->type == BFD_MACH_O_LC_SEGMENT_64))
5489 {
5490 struct bfd_mach_o_segment_command *segcmd = &cmd->command.segment;
5491
5492 if (segcmd->initprot != 0)
5493 return segcmd->vmaddr;
5494 }
5495 }
5496 return 0;
5497 }
5498
5499 typedef struct mach_o_fat_archentry
5500 {
5501 unsigned long cputype;
5502 unsigned long cpusubtype;
5503 unsigned long offset;
5504 unsigned long size;
5505 unsigned long align;
5506 } mach_o_fat_archentry;
5507
5508 typedef struct mach_o_fat_data_struct
5509 {
5510 unsigned long magic;
5511 unsigned long nfat_arch;
5512 mach_o_fat_archentry *archentries;
5513 } mach_o_fat_data_struct;
5514
5515 /* Check for overlapping archive elements. Note that we can't allow
5516 multiple elements at the same offset even if one is empty, because
5517 bfd_mach_o_fat_openr_next_archived_file assume distinct offsets. */
5518 static bool
5519 overlap_previous (const mach_o_fat_archentry *elt, unsigned long i)
5520 {
5521 unsigned long j = i;
5522 while (j-- != 0)
5523 if (elt[i].offset == elt[j].offset
5524 || (elt[i].offset > elt[j].offset
5525 ? elt[i].offset - elt[j].offset < elt[j].size
5526 : elt[j].offset - elt[i].offset < elt[i].size))
5527 return true;
5528 return false;
5529 }
5530
5531 bfd_cleanup
5532 bfd_mach_o_fat_archive_p (bfd *abfd)
5533 {
5534 mach_o_fat_data_struct *adata = NULL;
5535 struct mach_o_fat_header_external hdr;
5536 unsigned long i;
5537 size_t amt;
5538 ufile_ptr filesize;
5539
5540 if (bfd_seek (abfd, 0, SEEK_SET) != 0
5541 || bfd_read (&hdr, sizeof (hdr), abfd) != sizeof (hdr))
5542 {
5543 if (bfd_get_error () != bfd_error_system_call)
5544 goto wrong;
5545 goto error;
5546 }
5547
5548 adata = bfd_alloc (abfd, sizeof (mach_o_fat_data_struct));
5549 if (adata == NULL)
5550 goto error;
5551
5552 adata->magic = bfd_getb32 (hdr.magic);
5553 adata->nfat_arch = bfd_getb32 (hdr.nfat_arch);
5554 if (adata->magic != 0xcafebabe)
5555 goto wrong;
5556 /* Avoid matching Java bytecode files, which have the same magic number.
5557 In the Java bytecode file format this field contains the JVM version,
5558 which starts at 43.0. */
5559 if (adata->nfat_arch > 30)
5560 goto wrong;
5561
5562 if (_bfd_mul_overflow (adata->nfat_arch,
5563 sizeof (mach_o_fat_archentry), &amt))
5564 {
5565 bfd_set_error (bfd_error_file_too_big);
5566 goto error;
5567 }
5568 adata->archentries = bfd_alloc (abfd, amt);
5569 if (adata->archentries == NULL)
5570 goto error;
5571
5572 filesize = bfd_get_file_size (abfd);
5573 for (i = 0; i < adata->nfat_arch; i++)
5574 {
5575 struct mach_o_fat_arch_external arch;
5576 if (bfd_read (&arch, sizeof (arch), abfd) != sizeof (arch))
5577 goto error;
5578 adata->archentries[i].cputype = bfd_getb32 (arch.cputype);
5579 adata->archentries[i].cpusubtype = bfd_getb32 (arch.cpusubtype);
5580 adata->archentries[i].offset = bfd_getb32 (arch.offset);
5581 adata->archentries[i].size = bfd_getb32 (arch.size);
5582 adata->archentries[i].align = bfd_getb32 (arch.align);
5583 if ((filesize != 0
5584 && (adata->archentries[i].offset > filesize
5585 || (adata->archentries[i].size
5586 > filesize - adata->archentries[i].offset)))
5587 || (adata->archentries[i].offset
5588 < sizeof (hdr) + adata->nfat_arch * sizeof (arch))
5589 || overlap_previous (adata->archentries, i))
5590 {
5591 bfd_release (abfd, adata);
5592 bfd_set_error (bfd_error_malformed_archive);
5593 return NULL;
5594 }
5595 }
5596
5597 abfd->tdata.mach_o_fat_data = adata;
5598
5599 return _bfd_no_cleanup;
5600
5601 wrong:
5602 bfd_set_error (bfd_error_wrong_format);
5603 error:
5604 if (adata != NULL)
5605 bfd_release (abfd, adata);
5606 return NULL;
5607 }
5608
5609 /* Set the filename for a fat binary member ABFD, whose bfd architecture is
5610 ARCH_TYPE/ARCH_SUBTYPE and corresponding entry in header is ENTRY.
5611 Set arelt_data and origin fields too. */
5612
5613 static bool
5614 bfd_mach_o_fat_member_init (bfd *abfd,
5615 enum bfd_architecture arch_type,
5616 unsigned long arch_subtype,
5617 mach_o_fat_archentry *entry)
5618 {
5619 struct areltdata *areltdata;
5620 /* Create the member filename. Use ARCH_NAME. */
5621 const bfd_arch_info_type *ap = bfd_lookup_arch (arch_type, arch_subtype);
5622 const char *filename;
5623
5624 if (ap)
5625 {
5626 /* Use the architecture name if known. */
5627 filename = bfd_set_filename (abfd, ap->printable_name);
5628 }
5629 else
5630 {
5631 /* Forge a uniq id. */
5632 char buf[2 + 8 + 1 + 2 + 8 + 1];
5633 snprintf (buf, sizeof (buf), "0x%lx-0x%lx",
5634 entry->cputype, entry->cpusubtype);
5635 filename = bfd_set_filename (abfd, buf);
5636 }
5637 if (!filename)
5638 return false;
5639
5640 areltdata = bfd_zmalloc (sizeof (struct areltdata));
5641 if (areltdata == NULL)
5642 return false;
5643 areltdata->parsed_size = entry->size;
5644 abfd->arelt_data = areltdata;
5645 abfd->iostream = NULL;
5646 abfd->origin = entry->offset;
5647 return true;
5648 }
5649
5650 bfd *
5651 bfd_mach_o_fat_openr_next_archived_file (bfd *archive, bfd *prev)
5652 {
5653 mach_o_fat_data_struct *adata;
5654 mach_o_fat_archentry *entry = NULL;
5655 unsigned long i;
5656 bfd *nbfd;
5657 enum bfd_architecture arch_type;
5658 unsigned long arch_subtype;
5659
5660 adata = (mach_o_fat_data_struct *) archive->tdata.mach_o_fat_data;
5661 BFD_ASSERT (adata != NULL);
5662
5663 /* Find index of previous entry. */
5664 if (prev == NULL)
5665 {
5666 /* Start at first one. */
5667 i = 0;
5668 }
5669 else
5670 {
5671 /* Find index of PREV. */
5672 for (i = 0; i < adata->nfat_arch; i++)
5673 {
5674 if (adata->archentries[i].offset == prev->origin)
5675 break;
5676 }
5677
5678 if (i == adata->nfat_arch)
5679 {
5680 /* Not found. */
5681 bfd_set_error (bfd_error_bad_value);
5682 return NULL;
5683 }
5684
5685 /* Get next entry. */
5686 i++;
5687 }
5688
5689 if (i >= adata->nfat_arch)
5690 {
5691 bfd_set_error (bfd_error_no_more_archived_files);
5692 return NULL;
5693 }
5694
5695 entry = &adata->archentries[i];
5696 nbfd = _bfd_new_bfd_contained_in (archive);
5697 if (nbfd == NULL)
5698 return NULL;
5699
5700 bfd_mach_o_convert_architecture (entry->cputype, entry->cpusubtype,
5701 &arch_type, &arch_subtype);
5702
5703 if (!bfd_mach_o_fat_member_init (nbfd, arch_type, arch_subtype, entry))
5704 {
5705 bfd_close (nbfd);
5706 return NULL;
5707 }
5708
5709 bfd_set_arch_mach (nbfd, arch_type, arch_subtype);
5710
5711 return nbfd;
5712 }
5713
5714 /* Analogous to stat call. */
5715
5716 static int
5717 bfd_mach_o_fat_stat_arch_elt (bfd *abfd, struct stat *buf)
5718 {
5719 if (abfd->arelt_data == NULL)
5720 {
5721 bfd_set_error (bfd_error_invalid_operation);
5722 return -1;
5723 }
5724
5725 buf->st_mtime = 0;
5726 buf->st_uid = 0;
5727 buf->st_gid = 0;
5728 buf->st_mode = 0644;
5729 buf->st_size = arelt_size (abfd);
5730
5731 return 0;
5732 }
5733
5734 /* If ABFD format is FORMAT and architecture is ARCH, return it.
5735 If ABFD is a fat image containing a member that corresponds to FORMAT
5736 and ARCH, returns it.
5737 In other case, returns NULL.
5738 This function allows transparent uses of fat images. */
5739
5740 bfd *
5741 bfd_mach_o_fat_extract (bfd *abfd,
5742 bfd_format format,
5743 const bfd_arch_info_type *arch)
5744 {
5745 bfd *res;
5746 mach_o_fat_data_struct *adata;
5747 unsigned int i;
5748
5749 if (bfd_check_format (abfd, format))
5750 {
5751 if (bfd_get_arch_info (abfd) == arch)
5752 return abfd;
5753 return NULL;
5754 }
5755 if (!bfd_check_format (abfd, bfd_archive)
5756 || abfd->xvec != &mach_o_fat_vec)
5757 return NULL;
5758
5759 /* This is a Mach-O fat image. */
5760 adata = (mach_o_fat_data_struct *) abfd->tdata.mach_o_fat_data;
5761 BFD_ASSERT (adata != NULL);
5762
5763 for (i = 0; i < adata->nfat_arch; i++)
5764 {
5765 struct mach_o_fat_archentry *e = &adata->archentries[i];
5766 enum bfd_architecture cpu_type;
5767 unsigned long cpu_subtype;
5768
5769 bfd_mach_o_convert_architecture (e->cputype, e->cpusubtype,
5770 &cpu_type, &cpu_subtype);
5771 if (cpu_type != arch->arch || cpu_subtype != arch->mach)
5772 continue;
5773
5774 /* The architecture is found. */
5775 res = _bfd_new_bfd_contained_in (abfd);
5776 if (res == NULL)
5777 return NULL;
5778
5779 if (bfd_mach_o_fat_member_init (res, cpu_type, cpu_subtype, e)
5780 && bfd_check_format (res, format))
5781 {
5782 BFD_ASSERT (bfd_get_arch_info (res) == arch);
5783 return res;
5784 }
5785 bfd_close (res);
5786 return NULL;
5787 }
5788
5789 return NULL;
5790 }
5791
5792 static bool
5793 bfd_mach_o_fat_close_and_cleanup (bfd *abfd)
5794 {
5795 _bfd_unlink_from_archive_parent (abfd);
5796 return true;
5797 }
5798
5799 int
5800 bfd_mach_o_lookup_command (bfd *abfd,
5801 bfd_mach_o_load_command_type type,
5802 bfd_mach_o_load_command **mcommand)
5803 {
5804 struct mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
5805 struct bfd_mach_o_load_command *cmd;
5806 unsigned int num;
5807
5808 BFD_ASSERT (mdata != NULL);
5809 BFD_ASSERT (mcommand != NULL);
5810
5811 num = 0;
5812 for (cmd = mdata->first_command; cmd != NULL; cmd = cmd->next)
5813 {
5814 if (cmd->type != type)
5815 continue;
5816
5817 if (num == 0)
5818 *mcommand = cmd;
5819 num++;
5820 }
5821
5822 return num;
5823 }
5824
5825 unsigned long
5826 bfd_mach_o_stack_addr (enum bfd_mach_o_cpu_type type)
5827 {
5828 switch (type)
5829 {
5830 case BFD_MACH_O_CPU_TYPE_MC680x0:
5831 return 0x04000000;
5832 case BFD_MACH_O_CPU_TYPE_POWERPC:
5833 return 0xc0000000;
5834 case BFD_MACH_O_CPU_TYPE_I386:
5835 return 0xc0000000;
5836 case BFD_MACH_O_CPU_TYPE_SPARC:
5837 return 0xf0000000;
5838 case BFD_MACH_O_CPU_TYPE_HPPA:
5839 return 0xc0000000 - 0x04000000;
5840 default:
5841 return 0;
5842 }
5843 }
5844
5845 /* The following two tables should be kept, as far as possible, in order of
5846 most frequently used entries to optimize their use from gas. */
5847
5848 const bfd_mach_o_xlat_name bfd_mach_o_section_type_name[] =
5849 {
5850 { "regular", BFD_MACH_O_S_REGULAR},
5851 { "coalesced", BFD_MACH_O_S_COALESCED},
5852 { "zerofill", BFD_MACH_O_S_ZEROFILL},
5853 { "cstring_literals", BFD_MACH_O_S_CSTRING_LITERALS},
5854 { "4byte_literals", BFD_MACH_O_S_4BYTE_LITERALS},
5855 { "8byte_literals", BFD_MACH_O_S_8BYTE_LITERALS},
5856 { "16byte_literals", BFD_MACH_O_S_16BYTE_LITERALS},
5857 { "literal_pointers", BFD_MACH_O_S_LITERAL_POINTERS},
5858 { "mod_init_func_pointers", BFD_MACH_O_S_MOD_INIT_FUNC_POINTERS},
5859 { "mod_fini_func_pointers", BFD_MACH_O_S_MOD_FINI_FUNC_POINTERS},
5860 { "gb_zerofill", BFD_MACH_O_S_GB_ZEROFILL},
5861 { "interposing", BFD_MACH_O_S_INTERPOSING},
5862 { "dtrace_dof", BFD_MACH_O_S_DTRACE_DOF},
5863 { "non_lazy_symbol_pointers", BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS},
5864 { "lazy_symbol_pointers", BFD_MACH_O_S_LAZY_SYMBOL_POINTERS},
5865 { "symbol_stubs", BFD_MACH_O_S_SYMBOL_STUBS},
5866 { "lazy_dylib_symbol_pointers", BFD_MACH_O_S_LAZY_DYLIB_SYMBOL_POINTERS},
5867 { NULL, 0}
5868 };
5869
5870 const bfd_mach_o_xlat_name bfd_mach_o_section_attribute_name[] =
5871 {
5872 { "pure_instructions", BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS },
5873 { "some_instructions", BFD_MACH_O_S_ATTR_SOME_INSTRUCTIONS },
5874 { "loc_reloc", BFD_MACH_O_S_ATTR_LOC_RELOC },
5875 { "ext_reloc", BFD_MACH_O_S_ATTR_EXT_RELOC },
5876 { "debug", BFD_MACH_O_S_ATTR_DEBUG },
5877 { "live_support", BFD_MACH_O_S_ATTR_LIVE_SUPPORT },
5878 { "no_dead_strip", BFD_MACH_O_S_ATTR_NO_DEAD_STRIP },
5879 { "strip_static_syms", BFD_MACH_O_S_ATTR_STRIP_STATIC_SYMS },
5880 { "no_toc", BFD_MACH_O_S_ATTR_NO_TOC },
5881 { "self_modifying_code", BFD_MACH_O_S_SELF_MODIFYING_CODE },
5882 { "modifying_code", BFD_MACH_O_S_SELF_MODIFYING_CODE },
5883 { NULL, 0}
5884 };
5885
5886 /* Get the section type from NAME. Return 256 if NAME is unknown. */
5887
5888 unsigned int
5889 bfd_mach_o_get_section_type_from_name (bfd *abfd, const char *name)
5890 {
5891 const bfd_mach_o_xlat_name *x;
5892 bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
5893
5894 for (x = bfd_mach_o_section_type_name; x->name; x++)
5895 if (strcmp (x->name, name) == 0)
5896 {
5897 /* We found it... does the target support it? */
5898 if (bed->bfd_mach_o_section_type_valid_for_target == NULL
5899 || bed->bfd_mach_o_section_type_valid_for_target (x->val))
5900 return x->val; /* OK. */
5901 else
5902 break; /* Not supported. */
5903 }
5904 /* Maximum section ID = 0xff. */
5905 return 256;
5906 }
5907
5908 /* Get the section attribute from NAME. Return -1 if NAME is unknown. */
5909
5910 unsigned int
5911 bfd_mach_o_get_section_attribute_from_name (const char *name)
5912 {
5913 const bfd_mach_o_xlat_name *x;
5914
5915 for (x = bfd_mach_o_section_attribute_name; x->name; x++)
5916 if (strcmp (x->name, name) == 0)
5917 return x->val;
5918 return (unsigned int)-1;
5919 }
5920
5921 int
5922 bfd_mach_o_core_fetch_environment (bfd *abfd,
5923 unsigned char **rbuf,
5924 unsigned int *rlen)
5925 {
5926 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
5927 unsigned long stackaddr = bfd_mach_o_stack_addr (mdata->header.cputype);
5928 bfd_mach_o_load_command *cmd;
5929
5930 for (cmd = mdata->first_command; cmd != NULL; cmd = cmd->next)
5931 {
5932 bfd_mach_o_segment_command *seg;
5933
5934 if (cmd->type != BFD_MACH_O_LC_SEGMENT)
5935 continue;
5936
5937 seg = &cmd->command.segment;
5938
5939 if ((seg->vmaddr + seg->vmsize) == stackaddr)
5940 {
5941 unsigned long start = seg->fileoff;
5942 unsigned long end = seg->fileoff + seg->filesize;
5943 unsigned char *buf = bfd_malloc (1024);
5944 unsigned long size = 1024;
5945
5946 if (buf == NULL)
5947 return -1;
5948 for (;;)
5949 {
5950 bfd_size_type nread = 0;
5951 unsigned long offset;
5952 int found_nonnull = 0;
5953
5954 if (size > (end - start))
5955 size = (end - start);
5956
5957 buf = bfd_realloc_or_free (buf, size);
5958 if (buf == NULL)
5959 return -1;
5960
5961 if (bfd_seek (abfd, end - size, SEEK_SET) != 0)
5962 {
5963 free (buf);
5964 return -1;
5965 }
5966
5967 nread = bfd_read (buf, size, abfd);
5968
5969 if (nread != size)
5970 {
5971 free (buf);
5972 return -1;
5973 }
5974
5975 for (offset = 4; offset <= size; offset += 4)
5976 {
5977 unsigned long val;
5978
5979 val = bfd_get_32(abfd, buf + size - offset);
5980
5981 if (! found_nonnull)
5982 {
5983 if (val != 0)
5984 found_nonnull = 1;
5985 }
5986 else if (val == 0x0)
5987 {
5988 unsigned long bottom;
5989 unsigned long top;
5990
5991 bottom = seg->fileoff + seg->filesize - offset;
5992 top = seg->fileoff + seg->filesize - 4;
5993 *rbuf = bfd_malloc (top - bottom);
5994 if (*rbuf == NULL)
5995 return -1;
5996 *rlen = top - bottom;
5997
5998 memcpy (*rbuf, buf + size - *rlen, *rlen);
5999 free (buf);
6000 return 0;
6001 }
6002 }
6003
6004 if (size == (end - start))
6005 break;
6006
6007 size *= 2;
6008 }
6009
6010 free (buf);
6011 }
6012 }
6013
6014 return -1;
6015 }
6016
6017 char *
6018 bfd_mach_o_core_file_failing_command (bfd *abfd)
6019 {
6020 unsigned char *buf = NULL;
6021 unsigned int len = 0;
6022 int ret;
6023
6024 ret = bfd_mach_o_core_fetch_environment (abfd, &buf, &len);
6025 if (ret < 0 || len == 0)
6026 return NULL;
6027 buf[len - 1] = 0;
6028 return (char *) buf;
6029 }
6030
6031 int
6032 bfd_mach_o_core_file_failing_signal (bfd *abfd ATTRIBUTE_UNUSED)
6033 {
6034 return 0;
6035 }
6036
6037 static bfd_mach_o_uuid_command *
6038 bfd_mach_o_lookup_uuid_command (bfd *abfd)
6039 {
6040 bfd_mach_o_load_command *uuid_cmd = NULL;
6041 int ncmd = bfd_mach_o_lookup_command (abfd, BFD_MACH_O_LC_UUID, &uuid_cmd);
6042 if (ncmd != 1 || uuid_cmd == NULL)
6043 return NULL;
6044 return &uuid_cmd->command.uuid;
6045 }
6046
6047 /* Return true if ABFD is a dSYM file and its UUID matches UUID_CMD. */
6048
6049 static bool
6050 bfd_mach_o_dsym_for_uuid_p (bfd *abfd, const bfd_mach_o_uuid_command *uuid_cmd)
6051 {
6052 bfd_mach_o_uuid_command *dsym_uuid_cmd;
6053
6054 BFD_ASSERT (abfd);
6055 BFD_ASSERT (uuid_cmd);
6056
6057 if (!bfd_check_format (abfd, bfd_object))
6058 return false;
6059
6060 if (bfd_get_flavour (abfd) != bfd_target_mach_o_flavour
6061 || bfd_mach_o_get_data (abfd) == NULL
6062 || bfd_mach_o_get_data (abfd)->header.filetype != BFD_MACH_O_MH_DSYM)
6063 return false;
6064
6065 dsym_uuid_cmd = bfd_mach_o_lookup_uuid_command (abfd);
6066 if (dsym_uuid_cmd == NULL)
6067 return false;
6068
6069 if (memcmp (uuid_cmd->uuid, dsym_uuid_cmd->uuid,
6070 sizeof (uuid_cmd->uuid)) != 0)
6071 return false;
6072
6073 return true;
6074 }
6075
6076 /* Find a BFD in DSYM_FILENAME which matches ARCH and UUID_CMD.
6077 The caller is responsible for closing the returned BFD object and
6078 its my_archive if the returned BFD is in a fat dSYM. */
6079
6080 static bfd *
6081 bfd_mach_o_find_dsym (const char *dsym_filename,
6082 const bfd_mach_o_uuid_command *uuid_cmd,
6083 const bfd_arch_info_type *arch)
6084 {
6085 bfd *base_dsym_bfd, *dsym_bfd;
6086
6087 BFD_ASSERT (uuid_cmd);
6088
6089 base_dsym_bfd = bfd_openr (dsym_filename, NULL);
6090 if (base_dsym_bfd == NULL)
6091 return NULL;
6092
6093 dsym_bfd = bfd_mach_o_fat_extract (base_dsym_bfd, bfd_object, arch);
6094 if (bfd_mach_o_dsym_for_uuid_p (dsym_bfd, uuid_cmd))
6095 return dsym_bfd;
6096
6097 bfd_close (dsym_bfd);
6098 if (base_dsym_bfd != dsym_bfd)
6099 bfd_close (base_dsym_bfd);
6100
6101 return NULL;
6102 }
6103
6104 /* Return a BFD created from a dSYM file for ABFD.
6105 The caller is responsible for closing the returned BFD object, its
6106 filename, and its my_archive if the returned BFD is in a fat dSYM. */
6107
6108 static bfd *
6109 bfd_mach_o_follow_dsym (bfd *abfd)
6110 {
6111 char *dsym_filename;
6112 bfd_mach_o_uuid_command *uuid_cmd;
6113 bfd *dsym_bfd, *base_bfd = abfd;
6114 const char *base_basename;
6115
6116 if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_mach_o_flavour)
6117 return NULL;
6118
6119 if (abfd->my_archive && !bfd_is_thin_archive (abfd->my_archive))
6120 base_bfd = abfd->my_archive;
6121 /* BFD may have been opened from a stream. */
6122 if (bfd_get_filename (base_bfd) == NULL)
6123 {
6124 bfd_set_error (bfd_error_invalid_operation);
6125 return NULL;
6126 }
6127 base_basename = lbasename (bfd_get_filename (base_bfd));
6128
6129 uuid_cmd = bfd_mach_o_lookup_uuid_command (abfd);
6130 if (uuid_cmd == NULL)
6131 return NULL;
6132
6133 /* TODO: We assume the DWARF file has the same as the binary's.
6134 It seems apple's GDB checks all files in the dSYM bundle directory.
6135 http://opensource.apple.com/source/gdb/gdb-1708/src/gdb/macosx/macosx-tdep.c
6136 */
6137 dsym_filename = (char *)bfd_malloc (strlen (bfd_get_filename (base_bfd))
6138 + strlen (dsym_subdir) + 1
6139 + strlen (base_basename) + 1);
6140 if (dsym_filename == NULL)
6141 return NULL;
6142
6143 sprintf (dsym_filename, "%s%s/%s",
6144 bfd_get_filename (base_bfd), dsym_subdir, base_basename);
6145
6146 dsym_bfd = bfd_mach_o_find_dsym (dsym_filename, uuid_cmd,
6147 bfd_get_arch_info (abfd));
6148 if (dsym_bfd == NULL)
6149 free (dsym_filename);
6150
6151 return dsym_bfd;
6152 }
6153
6154 bool
6155 bfd_mach_o_find_nearest_line (bfd *abfd,
6156 asymbol **symbols,
6157 asection *section,
6158 bfd_vma offset,
6159 const char **filename_ptr,
6160 const char **functionname_ptr,
6161 unsigned int *line_ptr,
6162 unsigned int *discriminator_ptr)
6163 {
6164 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
6165 if (mdata == NULL)
6166 return false;
6167 switch (mdata->header.filetype)
6168 {
6169 case BFD_MACH_O_MH_OBJECT:
6170 break;
6171 case BFD_MACH_O_MH_EXECUTE:
6172 case BFD_MACH_O_MH_DYLIB:
6173 case BFD_MACH_O_MH_BUNDLE:
6174 case BFD_MACH_O_MH_KEXT_BUNDLE:
6175 if (mdata->dwarf2_find_line_info == NULL)
6176 {
6177 mdata->dsym_bfd = bfd_mach_o_follow_dsym (abfd);
6178 /* When we couldn't find dSYM for this binary, we look for
6179 the debug information in the binary itself. In this way,
6180 we won't try finding separated dSYM again because
6181 mdata->dwarf2_find_line_info will be filled. */
6182 if (! mdata->dsym_bfd)
6183 break;
6184 if (! _bfd_dwarf2_slurp_debug_info (abfd, mdata->dsym_bfd,
6185 dwarf_debug_sections, symbols,
6186 &mdata->dwarf2_find_line_info,
6187 false))
6188 return false;
6189 }
6190 break;
6191 default:
6192 return false;
6193 }
6194 return _bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
6195 filename_ptr, functionname_ptr,
6196 line_ptr, discriminator_ptr,
6197 dwarf_debug_sections,
6198 &mdata->dwarf2_find_line_info);
6199 }
6200
6201 bool
6202 bfd_mach_o_close_and_cleanup (bfd *abfd)
6203 {
6204 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
6205 if (bfd_get_format (abfd) == bfd_object && mdata != NULL)
6206 {
6207 if (mdata->dsym_bfd != NULL)
6208 {
6209 bfd *fat_bfd = mdata->dsym_bfd->my_archive;
6210 #if 0
6211 /* FIXME: PR 19435: This calculation to find the memory allocated by
6212 bfd_mach_o_follow_dsym for the filename does not always end up
6213 selecting the correct pointer. Unfortunately this problem is
6214 very hard to reproduce on a non Mach-O native system, so until it
6215 can be traced and fixed on such a system, this code will remain
6216 commented out. This does mean that there will be a memory leak,
6217 but it is small, and happens when we are closing down, so it
6218 should not matter too much. */
6219 char *dsym_filename = (char *)(fat_bfd
6220 ? bfd_get_filename (fat_bfd)
6221 : bfd_get_filename (mdata->dsym_bfd));
6222 #endif
6223 bfd_close (mdata->dsym_bfd);
6224 mdata->dsym_bfd = NULL;
6225 if (fat_bfd)
6226 bfd_close (fat_bfd);
6227 #if 0
6228 free (dsym_filename);
6229 #endif
6230 }
6231 }
6232
6233 return _bfd_generic_close_and_cleanup (abfd);
6234 }
6235
6236 bool
6237 bfd_mach_o_bfd_free_cached_info (bfd *abfd)
6238 {
6239 bfd_mach_o_data_struct *mdata;
6240
6241 if ((bfd_get_format (abfd) == bfd_object
6242 || bfd_get_format (abfd) == bfd_core)
6243 && (mdata = bfd_mach_o_get_data (abfd)) != NULL)
6244 {
6245 _bfd_dwarf2_cleanup_debug_info (abfd, &mdata->dwarf2_find_line_info);
6246 free (mdata->dyn_reloc_cache);
6247 mdata->dyn_reloc_cache = NULL;
6248
6249 for (asection *asect = abfd->sections; asect; asect = asect->next)
6250 {
6251 free (asect->relocation);
6252 asect->relocation = NULL;
6253 }
6254 }
6255
6256 /* Do not call _bfd_generic_bfd_free_cached_info here.
6257 bfd_mach_o_close_and_cleanup uses tdata. */
6258 return true;
6259 }
6260
6261 #define bfd_mach_o_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
6262 #define bfd_mach_o_bfd_reloc_name_lookup _bfd_norelocs_bfd_reloc_name_lookup
6263
6264 #define bfd_mach_o_canonicalize_one_reloc NULL
6265 #define bfd_mach_o_swap_reloc_out NULL
6266 #define bfd_mach_o_print_thread NULL
6267 #define bfd_mach_o_tgt_seg_table NULL
6268 #define bfd_mach_o_section_type_valid_for_tgt NULL
6269
6270 #define TARGET_NAME mach_o_be_vec
6271 #define TARGET_STRING "mach-o-be"
6272 #define TARGET_ARCHITECTURE bfd_arch_unknown
6273 #define TARGET_PAGESIZE 1
6274 #define TARGET_BIG_ENDIAN 1
6275 #define TARGET_ARCHIVE 0
6276 #define TARGET_PRIORITY 1
6277 #include "mach-o-target.c"
6278
6279 #undef TARGET_NAME
6280 #undef TARGET_STRING
6281 #undef TARGET_ARCHITECTURE
6282 #undef TARGET_PAGESIZE
6283 #undef TARGET_BIG_ENDIAN
6284 #undef TARGET_ARCHIVE
6285 #undef TARGET_PRIORITY
6286
6287 #define TARGET_NAME mach_o_le_vec
6288 #define TARGET_STRING "mach-o-le"
6289 #define TARGET_ARCHITECTURE bfd_arch_unknown
6290 #define TARGET_PAGESIZE 1
6291 #define TARGET_BIG_ENDIAN 0
6292 #define TARGET_ARCHIVE 0
6293 #define TARGET_PRIORITY 1
6294
6295 #include "mach-o-target.c"
6296
6297 #undef TARGET_NAME
6298 #undef TARGET_STRING
6299 #undef TARGET_ARCHITECTURE
6300 #undef TARGET_PAGESIZE
6301 #undef TARGET_BIG_ENDIAN
6302 #undef TARGET_ARCHIVE
6303 #undef TARGET_PRIORITY
6304
6305 /* Not yet handled: creating an archive. */
6306 #define bfd_mach_o_mkarchive _bfd_noarchive_mkarchive
6307
6308 #define bfd_mach_o_close_and_cleanup bfd_mach_o_fat_close_and_cleanup
6309
6310 /* Not used. */
6311 #define bfd_mach_o_generic_stat_arch_elt bfd_mach_o_fat_stat_arch_elt
6312 #define bfd_mach_o_openr_next_archived_file bfd_mach_o_fat_openr_next_archived_file
6313 #define bfd_mach_o_archive_p bfd_mach_o_fat_archive_p
6314
6315 #define TARGET_NAME mach_o_fat_vec
6316 #define TARGET_STRING "mach-o-fat"
6317 #define TARGET_ARCHITECTURE bfd_arch_unknown
6318 #define TARGET_PAGESIZE 1
6319 #define TARGET_BIG_ENDIAN 1
6320 #define TARGET_ARCHIVE 1
6321 #define TARGET_PRIORITY 0
6322
6323 #include "mach-o-target.c"
6324
6325 #undef TARGET_NAME
6326 #undef TARGET_STRING
6327 #undef TARGET_ARCHITECTURE
6328 #undef TARGET_PAGESIZE
6329 #undef TARGET_BIG_ENDIAN
6330 #undef TARGET_ARCHIVE
6331 #undef TARGET_PRIORITY
6332