1 /* Mach-O support for BFD.
2 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4 Free Software Foundation, Inc.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
27 #include "libiberty.h"
28 #include "aout/stab_gnu.h"
29 #include "mach-o/reloc.h"
30 #include "mach-o/external.h"
33 #define bfd_mach_o_object_p bfd_mach_o_gen_object_p
34 #define bfd_mach_o_core_p bfd_mach_o_gen_core_p
35 #define bfd_mach_o_mkobject bfd_mach_o_gen_mkobject
37 #define FILE_ALIGN(off, algn) \
38 (((off) + ((file_ptr) 1 << (algn)) - 1) & ((file_ptr) -1 << (algn)))
41 bfd_mach_o_version (bfd *abfd)
43 bfd_mach_o_data_struct *mdata = NULL;
45 BFD_ASSERT (bfd_mach_o_valid (abfd));
46 mdata = bfd_mach_o_get_data (abfd);
48 return mdata->header.version;
52 bfd_mach_o_valid (bfd *abfd)
54 if (abfd == NULL || abfd->xvec == NULL)
57 if (abfd->xvec->flavour != bfd_target_mach_o_flavour)
60 if (bfd_mach_o_get_data (abfd) == NULL)
65 static INLINE bfd_boolean
66 mach_o_wide_p (bfd_mach_o_header *header)
68 switch (header->version)
80 static INLINE bfd_boolean
81 bfd_mach_o_wide_p (bfd *abfd)
83 return mach_o_wide_p (&bfd_mach_o_get_data (abfd)->header);
86 /* Tables to translate well known Mach-O segment/section names to bfd
87 names. Use of canonical names (such as .text or .debug_frame) is required
91 static const mach_o_section_name_xlat text_section_names_xlat[] =
94 SEC_CODE | SEC_LOAD, BFD_MACH_O_S_REGULAR,
95 BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS, 0},
96 { ".const", "__const",
97 SEC_READONLY | SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
98 BFD_MACH_O_S_ATTR_NONE, 0},
99 { ".static_const", "__static_const",
100 SEC_READONLY | SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
101 BFD_MACH_O_S_ATTR_NONE, 0},
102 { ".cstring", "__cstring",
103 SEC_READONLY | SEC_DATA | SEC_LOAD | SEC_MERGE | SEC_STRINGS,
104 BFD_MACH_O_S_CSTRING_LITERALS,
105 BFD_MACH_O_S_ATTR_NONE, 0},
106 { ".literal4", "__literal4",
107 SEC_READONLY | SEC_DATA | SEC_LOAD, BFD_MACH_O_S_4BYTE_LITERALS,
108 BFD_MACH_O_S_ATTR_NONE, 2},
109 { ".literal8", "__literal8",
110 SEC_READONLY | SEC_DATA | SEC_LOAD, BFD_MACH_O_S_8BYTE_LITERALS,
111 BFD_MACH_O_S_ATTR_NONE, 3},
112 { ".literal16", "__literal16",
113 SEC_READONLY | SEC_DATA | SEC_LOAD, BFD_MACH_O_S_16BYTE_LITERALS,
114 BFD_MACH_O_S_ATTR_NONE, 4},
115 { ".constructor", "__constructor",
116 SEC_CODE | SEC_LOAD, BFD_MACH_O_S_REGULAR,
117 BFD_MACH_O_S_ATTR_NONE, 0},
118 { ".destructor", "__destructor",
119 SEC_CODE | SEC_LOAD, BFD_MACH_O_S_REGULAR,
120 BFD_MACH_O_S_ATTR_NONE, 0},
121 { ".eh_frame", "__eh_frame",
122 SEC_READONLY | SEC_LOAD, BFD_MACH_O_S_COALESCED,
123 BFD_MACH_O_S_ATTR_LIVE_SUPPORT
124 | BFD_MACH_O_S_ATTR_STRIP_STATIC_SYMS
125 | BFD_MACH_O_S_ATTR_NO_TOC, 3},
126 { NULL, NULL, 0, 0, 0, 0}
129 /* __DATA Segment. */
130 static const mach_o_section_name_xlat data_section_names_xlat[] =
133 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
134 BFD_MACH_O_S_ATTR_NONE, 0},
136 SEC_NO_FLAGS, BFD_MACH_O_S_ZEROFILL,
137 BFD_MACH_O_S_ATTR_NONE, 0},
138 { ".const_data", "__const",
139 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
140 BFD_MACH_O_S_ATTR_NONE, 0},
141 { ".static_data", "__static_data",
142 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
143 BFD_MACH_O_S_ATTR_NONE, 0},
144 { ".mod_init_func", "__mod_init_func",
145 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_MOD_INIT_FUNC_POINTERS,
146 BFD_MACH_O_S_ATTR_NONE, 2},
147 { ".mod_term_func", "__mod_term_func",
148 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_MOD_FINI_FUNC_POINTERS,
149 BFD_MACH_O_S_ATTR_NONE, 2},
151 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
152 BFD_MACH_O_S_ATTR_NONE, 0},
153 { ".cfstring", "__cfstring",
154 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
155 BFD_MACH_O_S_ATTR_NONE, 2},
156 { NULL, NULL, 0, 0, 0, 0}
159 /* __DWARF Segment. */
160 static const mach_o_section_name_xlat dwarf_section_names_xlat[] =
162 { ".debug_frame", "__debug_frame",
163 SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
164 BFD_MACH_O_S_ATTR_DEBUG, 0},
165 { ".debug_info", "__debug_info",
166 SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
167 BFD_MACH_O_S_ATTR_DEBUG, 0},
168 { ".debug_abbrev", "__debug_abbrev",
169 SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
170 BFD_MACH_O_S_ATTR_DEBUG, 0},
171 { ".debug_aranges", "__debug_aranges",
172 SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
173 BFD_MACH_O_S_ATTR_DEBUG, 0},
174 { ".debug_macinfo", "__debug_macinfo",
175 SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
176 BFD_MACH_O_S_ATTR_DEBUG, 0},
177 { ".debug_line", "__debug_line",
178 SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
179 BFD_MACH_O_S_ATTR_DEBUG, 0},
180 { ".debug_loc", "__debug_loc",
181 SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
182 BFD_MACH_O_S_ATTR_DEBUG, 0},
183 { ".debug_pubnames", "__debug_pubnames",
184 SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
185 BFD_MACH_O_S_ATTR_DEBUG, 0},
186 { ".debug_pubtypes", "__debug_pubtypes",
187 SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
188 BFD_MACH_O_S_ATTR_DEBUG, 0},
189 { ".debug_str", "__debug_str",
190 SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
191 BFD_MACH_O_S_ATTR_DEBUG, 0},
192 { ".debug_ranges", "__debug_ranges",
193 SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
194 BFD_MACH_O_S_ATTR_DEBUG, 0},
195 { ".debug_macro", "__debug_macro",
196 SEC_DEBUGGING, BFD_MACH_O_S_REGULAR,
197 BFD_MACH_O_S_ATTR_DEBUG, 0},
198 { NULL, NULL, 0, 0, 0, 0}
201 /* __OBJC Segment. */
202 static const mach_o_section_name_xlat objc_section_names_xlat[] =
204 { ".objc_class", "__class",
205 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
206 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
207 { ".objc_meta_class", "__meta_class",
208 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
209 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
210 { ".objc_cat_cls_meth", "__cat_cls_meth",
211 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
212 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
213 { ".objc_cat_inst_meth", "__cat_inst_meth",
214 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
215 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
216 { ".objc_protocol", "__protocol",
217 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
218 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
219 { ".objc_string_object", "__string_object",
220 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
221 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
222 { ".objc_cls_meth", "__cls_meth",
223 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
224 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
225 { ".objc_inst_meth", "__inst_meth",
226 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
227 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
228 { ".objc_cls_refs", "__cls_refs",
229 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_LITERAL_POINTERS,
230 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
231 { ".objc_message_refs", "__message_refs",
232 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_LITERAL_POINTERS,
233 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
234 { ".objc_symbols", "__symbols",
235 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
236 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
237 { ".objc_category", "__category",
238 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
239 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
240 { ".objc_class_vars", "__class_vars",
241 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
242 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
243 { ".objc_instance_vars", "__instance_vars",
244 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
245 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
246 { ".objc_module_info", "__module_info",
247 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
248 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
249 { ".objc_selector_strs", "__selector_strs",
250 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_CSTRING_LITERALS,
251 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
252 { ".objc_image_info", "__image_info",
253 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
254 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
255 { ".objc_selector_fixup", "__sel_fixup",
256 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
257 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
259 { ".objc1_class_ext", "__class_ext",
260 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
261 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
262 { ".objc1_property_list", "__property",
263 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
264 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
265 { ".objc1_protocol_ext", "__protocol_ext",
266 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
267 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0},
268 { NULL, NULL, 0, 0, 0, 0}
271 static const mach_o_segment_name_xlat segsec_names_xlat[] =
273 { "__TEXT", text_section_names_xlat },
274 { "__DATA", data_section_names_xlat },
275 { "__DWARF", dwarf_section_names_xlat },
276 { "__OBJC", objc_section_names_xlat },
280 /* For both cases bfd-name => mach-o name and vice versa, the specific target
281 is checked before the generic. This allows a target (e.g. ppc for cstring)
282 to override the generic definition with a more specific one. */
284 /* Fetch the translation from a Mach-O section designation (segment, section)
285 as a bfd short name, if one exists. Otherwise return NULL.
287 Allow the segment and section names to be unterminated 16 byte arrays. */
289 const mach_o_section_name_xlat *
290 bfd_mach_o_section_data_for_mach_sect (bfd *abfd, const char *segname,
291 const char *sectname)
293 const struct mach_o_segment_name_xlat *seg;
294 const mach_o_section_name_xlat *sec;
295 bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
297 /* First try any target-specific translations defined... */
298 if (bed->segsec_names_xlat)
299 for (seg = bed->segsec_names_xlat; seg->segname; seg++)
300 if (strncmp (seg->segname, segname, BFD_MACH_O_SEGNAME_SIZE) == 0)
301 for (sec = seg->sections; sec->mach_o_name; sec++)
302 if (strncmp (sec->mach_o_name, sectname,
303 BFD_MACH_O_SECTNAME_SIZE) == 0)
306 /* ... and then the Mach-O generic ones. */
307 for (seg = segsec_names_xlat; seg->segname; seg++)
308 if (strncmp (seg->segname, segname, BFD_MACH_O_SEGNAME_SIZE) == 0)
309 for (sec = seg->sections; sec->mach_o_name; sec++)
310 if (strncmp (sec->mach_o_name, sectname,
311 BFD_MACH_O_SECTNAME_SIZE) == 0)
317 /* If the bfd_name for this section is a 'canonical' form for which we
318 know the Mach-O data, return the segment name and the data for the
319 Mach-O equivalent. Otherwise return NULL. */
321 const mach_o_section_name_xlat *
322 bfd_mach_o_section_data_for_bfd_name (bfd *abfd, const char *bfd_name,
323 const char **segname)
325 const struct mach_o_segment_name_xlat *seg;
326 const mach_o_section_name_xlat *sec;
327 bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
330 if (bfd_name[0] != '.')
333 /* First try any target-specific translations defined... */
334 if (bed->segsec_names_xlat)
335 for (seg = bed->segsec_names_xlat; seg->segname; seg++)
336 for (sec = seg->sections; sec->bfd_name; sec++)
337 if (strcmp (bfd_name, sec->bfd_name) == 0)
339 *segname = seg->segname;
343 /* ... and then the Mach-O generic ones. */
344 for (seg = segsec_names_xlat; seg->segname; seg++)
345 for (sec = seg->sections; sec->bfd_name; sec++)
346 if (strcmp (bfd_name, sec->bfd_name) == 0)
348 *segname = seg->segname;
355 /* Convert Mach-O section name to BFD.
357 Try to use standard/canonical names, for which we have tables including
358 default flag settings - which are returned. Otherwise forge a new name
359 in the form "<segmentname>.<sectionname>" this will be prefixed with
360 LC_SEGMENT. if the segment name does not begin with an underscore.
362 SEGNAME and SECTNAME are 16 byte arrays (they do not need to be NUL-
363 terminated if the name length is exactly 16 bytes - but must be if the name
364 length is less than 16 characters). */
367 bfd_mach_o_convert_section_name_to_bfd (bfd *abfd, const char *segname,
368 const char *secname, const char **name,
371 const mach_o_section_name_xlat *xlat;
374 const char *pfx = "";
377 *flags = SEC_NO_FLAGS;
379 /* First search for a canonical name...
380 xlat will be non-null if there is an entry for segname, secname. */
381 xlat = bfd_mach_o_section_data_for_mach_sect (abfd, segname, secname);
384 len = strlen (xlat->bfd_name);
385 res = bfd_alloc (abfd, len+1);
388 memcpy (res, xlat->bfd_name, len+1);
390 *flags = xlat->bfd_flags;
394 /* ... else we make up a bfd name from the segment concatenated with the
397 len = 16 + 1 + 16 + 1;
399 /* Put "LC_SEGMENT." prefix if the segment name is weird (ie doesn't start
400 with an underscore. */
401 if (segname[0] != '_')
403 static const char seg_pfx[] = "LC_SEGMENT.";
406 len += sizeof (seg_pfx) - 1;
409 res = bfd_alloc (abfd, len);
412 snprintf (res, len, "%s%.16s.%.16s", pfx, segname, secname);
416 /* Convert a bfd section name to a Mach-O segment + section name.
418 If the name is a canonical one for which we have a Darwin match
419 return the translation table - which contains defaults for flags,
420 type, attribute and default alignment data.
422 Otherwise, expand the bfd_name (assumed to be in the form
423 "[LC_SEGMENT.]<segmentname>.<sectionname>") and return NULL. */
425 static const mach_o_section_name_xlat *
426 bfd_mach_o_convert_section_name_to_mach_o (bfd *abfd ATTRIBUTE_UNUSED,
428 bfd_mach_o_section *section)
430 const mach_o_section_name_xlat *xlat;
431 const char *name = bfd_get_section_name (abfd, sect);
438 memset (section->segname, 0, BFD_MACH_O_SEGNAME_SIZE + 1);
439 memset (section->sectname, 0, BFD_MACH_O_SECTNAME_SIZE + 1);
441 /* See if is a canonical name ... */
442 xlat = bfd_mach_o_section_data_for_bfd_name (abfd, name, &segname);
445 strcpy (section->segname, segname);
446 strcpy (section->sectname, xlat->mach_o_name);
450 /* .. else we convert our constructed one back to Mach-O.
451 Strip LC_SEGMENT. prefix, if present. */
452 if (strncmp (name, "LC_SEGMENT.", 11) == 0)
456 dot = strchr (name, '.');
459 /* Try to split name into segment and section names. */
460 if (dot && dot != name)
463 seclen = len - (dot + 1 - name);
465 if (seglen < 16 && seclen < 16)
467 memcpy (section->segname, name, seglen);
468 section->segname[seglen] = 0;
469 memcpy (section->sectname, dot + 1, seclen);
470 section->sectname[seclen] = 0;
475 /* The segment and section names are both missing - don't make them
477 if (dot && dot == name)
480 /* Just duplicate the name into both segment and section. */
483 memcpy (section->segname, name, len);
484 section->segname[len] = 0;
485 memcpy (section->sectname, name, len);
486 section->sectname[len] = 0;
490 /* Return the size of an entry for section SEC.
491 Must be called only for symbol pointer section and symbol stubs
495 bfd_mach_o_section_get_entry_size (bfd *abfd, bfd_mach_o_section *sec)
497 switch (sec->flags & BFD_MACH_O_SECTION_TYPE_MASK)
499 case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS:
500 case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS:
501 return bfd_mach_o_wide_p (abfd) ? 8 : 4;
502 case BFD_MACH_O_S_SYMBOL_STUBS:
503 return sec->reserved2;
510 /* Return the number of indirect symbols for a section.
511 Must be called only for symbol pointer section and symbol stubs
515 bfd_mach_o_section_get_nbr_indirect (bfd *abfd, bfd_mach_o_section *sec)
519 elsz = bfd_mach_o_section_get_entry_size (abfd, sec);
523 return sec->size / elsz;
527 /* Copy any private info we understand from the input symbol
528 to the output symbol. */
531 bfd_mach_o_bfd_copy_private_symbol_data (bfd *ibfd ATTRIBUTE_UNUSED,
532 asymbol *isymbol ATTRIBUTE_UNUSED,
533 bfd *obfd ATTRIBUTE_UNUSED,
534 asymbol *osymbol ATTRIBUTE_UNUSED)
539 /* Copy any private info we understand from the input section
540 to the output section. */
543 bfd_mach_o_bfd_copy_private_section_data (bfd *ibfd ATTRIBUTE_UNUSED,
545 bfd *obfd ATTRIBUTE_UNUSED,
548 if (osection->used_by_bfd == NULL)
549 osection->used_by_bfd = isection->used_by_bfd;
551 if (isection->used_by_bfd != NULL)
552 memcpy (osection->used_by_bfd, isection->used_by_bfd,
553 sizeof (bfd_mach_o_section));
555 if (osection->used_by_bfd != NULL)
556 ((bfd_mach_o_section *)osection->used_by_bfd)->bfdsection = osection;
561 /* Copy any private info we understand from the input bfd
562 to the output bfd. */
565 bfd_mach_o_bfd_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
567 if (bfd_get_flavour (ibfd) != bfd_target_mach_o_flavour
568 || bfd_get_flavour (obfd) != bfd_target_mach_o_flavour)
571 BFD_ASSERT (bfd_mach_o_valid (ibfd));
572 BFD_ASSERT (bfd_mach_o_valid (obfd));
574 /* FIXME: copy commands. */
579 /* This allows us to set up to 32 bits of flags (unless we invent some
580 fiendish scheme to subdivide). For now, we'll just set the file flags
581 without error checking - just overwrite. */
584 bfd_mach_o_bfd_set_private_flags (bfd *abfd, flagword flags)
586 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
591 mdata->header.flags = flags;
595 /* Count the total number of symbols. */
598 bfd_mach_o_count_symbols (bfd *abfd)
600 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
602 if (mdata->symtab == NULL)
604 return mdata->symtab->nsyms;
608 bfd_mach_o_get_symtab_upper_bound (bfd *abfd)
610 long nsyms = bfd_mach_o_count_symbols (abfd);
612 return ((nsyms + 1) * sizeof (asymbol *));
616 bfd_mach_o_canonicalize_symtab (bfd *abfd, asymbol **alocation)
618 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
619 long nsyms = bfd_mach_o_count_symbols (abfd);
620 bfd_mach_o_symtab_command *sym = mdata->symtab;
628 /* Do not try to read symbols if there are none. */
633 if (!bfd_mach_o_read_symtab_symbols (abfd))
635 (*_bfd_error_handler)
636 (_("bfd_mach_o_canonicalize_symtab: unable to load symbols"));
640 BFD_ASSERT (sym->symbols != NULL);
642 for (j = 0; j < sym->nsyms; j++)
643 alocation[j] = &sym->symbols[j].symbol;
651 bfd_mach_o_get_synthetic_symtab (bfd *abfd,
652 long symcount ATTRIBUTE_UNUSED,
653 asymbol **syms ATTRIBUTE_UNUSED,
654 long dynsymcount ATTRIBUTE_UNUSED,
655 asymbol **dynsyms ATTRIBUTE_UNUSED,
658 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
659 bfd_mach_o_dysymtab_command *dysymtab = mdata->dysymtab;
660 bfd_mach_o_symtab_command *symtab = mdata->symtab;
662 unsigned long count, i, j, n;
669 if (dysymtab == NULL || symtab == NULL || symtab->symbols == NULL)
672 if (dysymtab->nindirectsyms == 0)
675 count = dysymtab->nindirectsyms;
676 size = count * sizeof (asymbol) + 1;
678 for (j = 0; j < count; j++)
680 unsigned int isym = dysymtab->indirect_syms[j];
682 if (isym < symtab->nsyms && symtab->symbols[isym].symbol.name)
683 size += strlen (symtab->symbols[isym].symbol.name) + sizeof ("$stub");
686 s = *ret = (asymbol *) bfd_malloc (size);
689 names = (char *) (s + count);
694 for (i = 0; i < mdata->nsects; i++)
696 bfd_mach_o_section *sec = mdata->sections[i];
697 unsigned int first, last;
701 switch (sec->flags & BFD_MACH_O_SECTION_TYPE_MASK)
703 case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS:
704 case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS:
705 case BFD_MACH_O_S_SYMBOL_STUBS:
706 first = sec->reserved1;
707 last = first + bfd_mach_o_section_get_nbr_indirect (abfd, sec);
709 entry_size = bfd_mach_o_section_get_entry_size (abfd, sec);
710 for (j = first; j < last; j++)
712 unsigned int isym = dysymtab->indirect_syms[j];
714 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
715 s->section = sec->bfdsection;
716 s->value = addr - sec->addr;
719 if (isym < symtab->nsyms
720 && symtab->symbols[isym].symbol.name)
722 const char *sym = symtab->symbols[isym].symbol.name;
727 memcpy (names, sym, len);
729 memcpy (names, "$stub", sizeof ("$stub"));
730 names += sizeof ("$stub");
749 bfd_mach_o_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
753 bfd_symbol_info (symbol, ret);
757 bfd_mach_o_print_symbol (bfd *abfd,
760 bfd_print_symbol_type how)
762 FILE *file = (FILE *) afile;
764 bfd_mach_o_asymbol *asym = (bfd_mach_o_asymbol *)symbol;
768 case bfd_print_symbol_name:
769 fprintf (file, "%s", symbol->name);
772 bfd_print_symbol_vandf (abfd, (void *) file, symbol);
773 if (asym->n_type & BFD_MACH_O_N_STAB)
774 name = bfd_get_stab_name (asym->n_type);
776 switch (asym->n_type & BFD_MACH_O_N_TYPE)
778 case BFD_MACH_O_N_UNDF:
779 if (symbol->value == 0)
784 case BFD_MACH_O_N_ABS:
787 case BFD_MACH_O_N_INDR:
790 case BFD_MACH_O_N_PBUD:
793 case BFD_MACH_O_N_SECT:
802 fprintf (file, " %02x %-6s %02x %04x",
803 asym->n_type, name, asym->n_sect, asym->n_desc);
804 if ((asym->n_type & BFD_MACH_O_N_STAB) == 0
805 && (asym->n_type & BFD_MACH_O_N_TYPE) == BFD_MACH_O_N_SECT)
806 fprintf (file, " [%s]", symbol->section->name);
807 fprintf (file, " %s", symbol->name);
812 bfd_mach_o_convert_architecture (bfd_mach_o_cpu_type mtype,
813 bfd_mach_o_cpu_subtype msubtype ATTRIBUTE_UNUSED,
814 enum bfd_architecture *type,
815 unsigned long *subtype)
817 *subtype = bfd_arch_unknown;
821 case BFD_MACH_O_CPU_TYPE_VAX: *type = bfd_arch_vax; break;
822 case BFD_MACH_O_CPU_TYPE_MC680x0: *type = bfd_arch_m68k; break;
823 case BFD_MACH_O_CPU_TYPE_I386:
824 *type = bfd_arch_i386;
825 *subtype = bfd_mach_i386_i386;
827 case BFD_MACH_O_CPU_TYPE_X86_64:
828 *type = bfd_arch_i386;
829 *subtype = bfd_mach_x86_64;
831 case BFD_MACH_O_CPU_TYPE_MIPS: *type = bfd_arch_mips; break;
832 case BFD_MACH_O_CPU_TYPE_MC98000: *type = bfd_arch_m98k; break;
833 case BFD_MACH_O_CPU_TYPE_HPPA: *type = bfd_arch_hppa; break;
834 case BFD_MACH_O_CPU_TYPE_ARM: *type = bfd_arch_arm; break;
835 case BFD_MACH_O_CPU_TYPE_MC88000: *type = bfd_arch_m88k; break;
836 case BFD_MACH_O_CPU_TYPE_SPARC:
837 *type = bfd_arch_sparc;
838 *subtype = bfd_mach_sparc;
840 case BFD_MACH_O_CPU_TYPE_I860: *type = bfd_arch_i860; break;
841 case BFD_MACH_O_CPU_TYPE_ALPHA: *type = bfd_arch_alpha; break;
842 case BFD_MACH_O_CPU_TYPE_POWERPC:
843 *type = bfd_arch_powerpc;
844 *subtype = bfd_mach_ppc;
846 case BFD_MACH_O_CPU_TYPE_POWERPC_64:
847 *type = bfd_arch_powerpc;
848 *subtype = bfd_mach_ppc64;
851 *type = bfd_arch_unknown;
857 bfd_mach_o_write_header (bfd *abfd, bfd_mach_o_header *header)
859 struct mach_o_header_external raw;
862 size = mach_o_wide_p (header) ?
863 BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE;
865 bfd_h_put_32 (abfd, header->magic, raw.magic);
866 bfd_h_put_32 (abfd, header->cputype, raw.cputype);
867 bfd_h_put_32 (abfd, header->cpusubtype, raw.cpusubtype);
868 bfd_h_put_32 (abfd, header->filetype, raw.filetype);
869 bfd_h_put_32 (abfd, header->ncmds, raw.ncmds);
870 bfd_h_put_32 (abfd, header->sizeofcmds, raw.sizeofcmds);
871 bfd_h_put_32 (abfd, header->flags, raw.flags);
873 if (mach_o_wide_p (header))
874 bfd_h_put_32 (abfd, header->reserved, raw.reserved);
876 if (bfd_seek (abfd, 0, SEEK_SET) != 0
877 || bfd_bwrite (&raw, size, abfd) != size)
884 bfd_mach_o_write_thread (bfd *abfd, bfd_mach_o_load_command *command)
886 bfd_mach_o_thread_command *cmd = &command->command.thread;
888 struct mach_o_thread_command_external raw;
891 BFD_ASSERT ((command->type == BFD_MACH_O_LC_THREAD)
892 || (command->type == BFD_MACH_O_LC_UNIXTHREAD));
895 for (i = 0; i < cmd->nflavours; i++)
897 BFD_ASSERT ((cmd->flavours[i].size % 4) == 0);
898 BFD_ASSERT (cmd->flavours[i].offset ==
899 (command->offset + offset + BFD_MACH_O_LC_SIZE));
901 bfd_h_put_32 (abfd, cmd->flavours[i].flavour, raw.flavour);
902 bfd_h_put_32 (abfd, (cmd->flavours[i].size / 4), raw.count);
904 if (bfd_seek (abfd, command->offset + offset, SEEK_SET) != 0
905 || bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
908 offset += cmd->flavours[i].size + sizeof (raw);
915 bfd_mach_o_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
918 return (asect->reloc_count + 1) * sizeof (arelent *);
922 bfd_mach_o_canonicalize_one_reloc (bfd *abfd,
923 struct mach_o_reloc_info_external *raw,
924 arelent *res, asymbol **syms)
926 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
927 bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
928 bfd_mach_o_reloc_info reloc;
933 addr = bfd_get_32 (abfd, raw->r_address);
934 symnum = bfd_get_32 (abfd, raw->r_symbolnum);
936 if (addr & BFD_MACH_O_SR_SCATTERED)
940 /* Scattered relocation.
941 Extract section and offset from r_value. */
942 res->sym_ptr_ptr = NULL;
944 for (j = 0; j < mdata->nsects; j++)
946 bfd_mach_o_section *sect = mdata->sections[j];
947 if (symnum >= sect->addr && symnum < sect->addr + sect->size)
949 res->sym_ptr_ptr = sect->bfdsection->symbol_ptr_ptr;
950 res->addend = symnum - sect->addr;
954 res->address = BFD_MACH_O_GET_SR_ADDRESS (addr);
955 reloc.r_type = BFD_MACH_O_GET_SR_TYPE (addr);
956 reloc.r_length = BFD_MACH_O_GET_SR_LENGTH (addr);
957 reloc.r_pcrel = addr & BFD_MACH_O_SR_PCREL;
958 reloc.r_scattered = 1;
962 unsigned int num = BFD_MACH_O_GET_R_SYMBOLNUM (symnum);
965 if (symnum & BFD_MACH_O_R_EXTERN)
972 BFD_ASSERT (num != 0);
973 BFD_ASSERT (num <= mdata->nsects);
974 sym = mdata->sections[num - 1]->bfdsection->symbol_ptr_ptr;
975 /* For a symbol defined in section S, the addend (stored in the
976 binary) contains the address of the section. To comply with
977 bfd conventio, substract the section address.
978 Use the address from the header, so that the user can modify
979 the vma of the section. */
980 res->addend = -mdata->sections[num - 1]->addr;
983 res->sym_ptr_ptr = sym;
984 reloc.r_type = BFD_MACH_O_GET_R_TYPE (symnum);
985 reloc.r_length = BFD_MACH_O_GET_R_LENGTH (symnum);
986 reloc.r_pcrel = (symnum & BFD_MACH_O_R_PCREL) ? 1 : 0;
987 reloc.r_scattered = 0;
990 if (!(*bed->_bfd_mach_o_swap_reloc_in)(res, &reloc))
996 bfd_mach_o_canonicalize_relocs (bfd *abfd, unsigned long filepos,
998 arelent *res, asymbol **syms)
1001 struct mach_o_reloc_info_external *native_relocs;
1002 bfd_size_type native_size;
1004 /* Allocate and read relocs. */
1005 native_size = count * BFD_MACH_O_RELENT_SIZE;
1007 (struct mach_o_reloc_info_external *) bfd_malloc (native_size);
1008 if (native_relocs == NULL)
1011 if (bfd_seek (abfd, filepos, SEEK_SET) != 0
1012 || bfd_bread (native_relocs, native_size, abfd) != native_size)
1015 for (i = 0; i < count; i++)
1017 if (bfd_mach_o_canonicalize_one_reloc (abfd, &native_relocs[i],
1021 free (native_relocs);
1024 free (native_relocs);
1029 bfd_mach_o_canonicalize_reloc (bfd *abfd, asection *asect,
1030 arelent **rels, asymbol **syms)
1032 bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
1036 if (asect->reloc_count == 0)
1039 /* No need to go further if we don't know how to read relocs. */
1040 if (bed->_bfd_mach_o_swap_reloc_in == NULL)
1043 if (asect->relocation == NULL)
1045 res = bfd_malloc (asect->reloc_count * sizeof (arelent));
1049 if (bfd_mach_o_canonicalize_relocs (abfd, asect->rel_filepos,
1050 asect->reloc_count, res, syms) < 0)
1055 asect->relocation = res;
1058 res = asect->relocation;
1059 for (i = 0; i < asect->reloc_count; i++)
1067 bfd_mach_o_get_dynamic_reloc_upper_bound (bfd *abfd)
1069 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
1071 if (mdata->dysymtab == NULL)
1073 return (mdata->dysymtab->nextrel + mdata->dysymtab->nlocrel + 1)
1074 * sizeof (arelent *);
1078 bfd_mach_o_canonicalize_dynamic_reloc (bfd *abfd, arelent **rels,
1079 struct bfd_symbol **syms)
1081 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
1082 bfd_mach_o_dysymtab_command *dysymtab = mdata->dysymtab;
1083 bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
1087 if (dysymtab == NULL)
1089 if (dysymtab->nextrel == 0 && dysymtab->nlocrel == 0)
1092 /* No need to go further if we don't know how to read relocs. */
1093 if (bed->_bfd_mach_o_swap_reloc_in == NULL)
1096 if (mdata->dyn_reloc_cache == NULL)
1098 res = bfd_malloc ((dysymtab->nextrel + dysymtab->nlocrel)
1099 * sizeof (arelent));
1103 if (bfd_mach_o_canonicalize_relocs (abfd, dysymtab->extreloff,
1104 dysymtab->nextrel, res, syms) < 0)
1110 if (bfd_mach_o_canonicalize_relocs (abfd, dysymtab->locreloff,
1112 res + dysymtab->nextrel, syms) < 0)
1118 mdata->dyn_reloc_cache = res;
1121 res = mdata->dyn_reloc_cache;
1122 for (i = 0; i < dysymtab->nextrel + dysymtab->nlocrel; i++)
1129 bfd_mach_o_write_relocs (bfd *abfd, bfd_mach_o_section *section)
1131 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
1135 bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
1137 sec = section->bfdsection;
1138 if (sec->reloc_count == 0)
1141 if (bed->_bfd_mach_o_swap_reloc_out == NULL)
1144 /* Allocate relocation room. */
1145 mdata->filelen = FILE_ALIGN(mdata->filelen, 2);
1146 section->nreloc = sec->reloc_count;
1147 sec->rel_filepos = mdata->filelen;
1148 section->reloff = sec->rel_filepos;
1149 mdata->filelen += sec->reloc_count * BFD_MACH_O_RELENT_SIZE;
1151 if (bfd_seek (abfd, section->reloff, SEEK_SET) != 0)
1154 /* Convert and write. */
1155 entries = section->bfdsection->orelocation;
1156 for (i = 0; i < section->nreloc; i++)
1158 arelent *rel = entries[i];
1159 struct mach_o_reloc_info_external raw;
1160 bfd_mach_o_reloc_info info, *pinfo = &info;
1162 /* Convert relocation to an intermediate representation. */
1163 if (!(*bed->_bfd_mach_o_swap_reloc_out) (rel, pinfo))
1166 /* Lower the relocation info. */
1167 if (pinfo->r_scattered)
1171 v = BFD_MACH_O_SR_SCATTERED
1172 | (pinfo->r_pcrel ? BFD_MACH_O_SR_PCREL : 0)
1173 | BFD_MACH_O_SET_SR_LENGTH(pinfo->r_length)
1174 | BFD_MACH_O_SET_SR_TYPE(pinfo->r_type)
1175 | BFD_MACH_O_SET_SR_ADDRESS(pinfo->r_address);
1176 /* Note: scattered relocs have field in reverse order... */
1177 bfd_put_32 (abfd, v, raw.r_address);
1178 bfd_put_32 (abfd, pinfo->r_value, raw.r_symbolnum);
1184 bfd_put_32 (abfd, pinfo->r_address, raw.r_address);
1185 v = BFD_MACH_O_SET_R_SYMBOLNUM (pinfo->r_value)
1186 | (pinfo->r_pcrel ? BFD_MACH_O_R_PCREL : 0)
1187 | BFD_MACH_O_SET_R_LENGTH (pinfo->r_length)
1188 | (pinfo->r_extern ? BFD_MACH_O_R_EXTERN : 0)
1189 | BFD_MACH_O_SET_R_TYPE (pinfo->r_type);
1190 bfd_put_32 (abfd, v, raw.r_symbolnum);
1193 if (bfd_bwrite (&raw, BFD_MACH_O_RELENT_SIZE, abfd)
1194 != BFD_MACH_O_RELENT_SIZE)
1201 bfd_mach_o_write_section_32 (bfd *abfd, bfd_mach_o_section *section)
1203 struct mach_o_section_32_external raw;
1205 memcpy (raw.sectname, section->sectname, 16);
1206 memcpy (raw.segname, section->segname, 16);
1207 bfd_h_put_32 (abfd, section->addr, raw.addr);
1208 bfd_h_put_32 (abfd, section->size, raw.size);
1209 bfd_h_put_32 (abfd, section->offset, raw.offset);
1210 bfd_h_put_32 (abfd, section->align, raw.align);
1211 bfd_h_put_32 (abfd, section->reloff, raw.reloff);
1212 bfd_h_put_32 (abfd, section->nreloc, raw.nreloc);
1213 bfd_h_put_32 (abfd, section->flags, raw.flags);
1214 bfd_h_put_32 (abfd, section->reserved1, raw.reserved1);
1215 bfd_h_put_32 (abfd, section->reserved2, raw.reserved2);
1217 if (bfd_bwrite (&raw, BFD_MACH_O_SECTION_SIZE, abfd)
1218 != BFD_MACH_O_SECTION_SIZE)
1225 bfd_mach_o_write_section_64 (bfd *abfd, bfd_mach_o_section *section)
1227 struct mach_o_section_64_external raw;
1229 memcpy (raw.sectname, section->sectname, 16);
1230 memcpy (raw.segname, section->segname, 16);
1231 bfd_h_put_64 (abfd, section->addr, raw.addr);
1232 bfd_h_put_64 (abfd, section->size, raw.size);
1233 bfd_h_put_32 (abfd, section->offset, raw.offset);
1234 bfd_h_put_32 (abfd, section->align, raw.align);
1235 bfd_h_put_32 (abfd, section->reloff, raw.reloff);
1236 bfd_h_put_32 (abfd, section->nreloc, raw.nreloc);
1237 bfd_h_put_32 (abfd, section->flags, raw.flags);
1238 bfd_h_put_32 (abfd, section->reserved1, raw.reserved1);
1239 bfd_h_put_32 (abfd, section->reserved2, raw.reserved2);
1240 bfd_h_put_32 (abfd, section->reserved3, raw.reserved3);
1242 if (bfd_bwrite (&raw, BFD_MACH_O_SECTION_64_SIZE, abfd)
1243 != BFD_MACH_O_SECTION_64_SIZE)
1250 bfd_mach_o_write_segment_32 (bfd *abfd, bfd_mach_o_load_command *command)
1252 struct mach_o_segment_command_32_external raw;
1253 bfd_mach_o_segment_command *seg = &command->command.segment;
1254 bfd_mach_o_section *sec;
1256 BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT);
1258 for (sec = seg->sect_head; sec != NULL; sec = sec->next)
1259 if (!bfd_mach_o_write_relocs (abfd, sec))
1262 memcpy (raw.segname, seg->segname, 16);
1263 bfd_h_put_32 (abfd, seg->vmaddr, raw.vmaddr);
1264 bfd_h_put_32 (abfd, seg->vmsize, raw.vmsize);
1265 bfd_h_put_32 (abfd, seg->fileoff, raw.fileoff);
1266 bfd_h_put_32 (abfd, seg->filesize, raw.filesize);
1267 bfd_h_put_32 (abfd, seg->maxprot, raw.maxprot);
1268 bfd_h_put_32 (abfd, seg->initprot, raw.initprot);
1269 bfd_h_put_32 (abfd, seg->nsects, raw.nsects);
1270 bfd_h_put_32 (abfd, seg->flags, raw.flags);
1272 if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
1273 || bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
1276 for (sec = seg->sect_head; sec != NULL; sec = sec->next)
1277 if (bfd_mach_o_write_section_32 (abfd, sec))
1284 bfd_mach_o_write_segment_64 (bfd *abfd, bfd_mach_o_load_command *command)
1286 struct mach_o_segment_command_64_external raw;
1287 bfd_mach_o_segment_command *seg = &command->command.segment;
1288 bfd_mach_o_section *sec;
1290 BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT_64);
1292 for (sec = seg->sect_head; sec != NULL; sec = sec->next)
1293 if (!bfd_mach_o_write_relocs (abfd, sec))
1296 memcpy (raw.segname, seg->segname, 16);
1297 bfd_h_put_64 (abfd, seg->vmaddr, raw.vmaddr);
1298 bfd_h_put_64 (abfd, seg->vmsize, raw.vmsize);
1299 bfd_h_put_64 (abfd, seg->fileoff, raw.fileoff);
1300 bfd_h_put_64 (abfd, seg->filesize, raw.filesize);
1301 bfd_h_put_32 (abfd, seg->maxprot, raw.maxprot);
1302 bfd_h_put_32 (abfd, seg->initprot, raw.initprot);
1303 bfd_h_put_32 (abfd, seg->nsects, raw.nsects);
1304 bfd_h_put_32 (abfd, seg->flags, raw.flags);
1306 if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
1307 || bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
1310 for (sec = seg->sect_head; sec != NULL; sec = sec->next)
1311 if (bfd_mach_o_write_section_64 (abfd, sec))
1318 bfd_mach_o_write_symtab (bfd *abfd, bfd_mach_o_load_command *command)
1320 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
1321 bfd_mach_o_symtab_command *sym = &command->command.symtab;
1323 unsigned int wide = bfd_mach_o_wide_p (abfd);
1324 unsigned int symlen = wide ? BFD_MACH_O_NLIST_64_SIZE : BFD_MACH_O_NLIST_SIZE;
1325 struct bfd_strtab_hash *strtab;
1326 asymbol **symbols = bfd_get_outsymbols (abfd);
1328 BFD_ASSERT (command->type == BFD_MACH_O_LC_SYMTAB);
1330 /* Write the symbols first. */
1331 mdata->filelen = FILE_ALIGN(mdata->filelen, wide ? 3 : 2);
1332 sym->symoff = mdata->filelen;
1333 if (bfd_seek (abfd, sym->symoff, SEEK_SET) != 0)
1336 sym->nsyms = bfd_get_symcount (abfd);
1337 mdata->filelen += sym->nsyms * symlen;
1339 strtab = _bfd_stringtab_init ();
1344 /* Although we don't strictly need to do this, for compatibility with
1345 Darwin system tools, actually output an empty string for the index
1347 _bfd_stringtab_add (strtab, "", TRUE, FALSE);
1349 for (i = 0; i < sym->nsyms; i++)
1351 bfd_size_type str_index;
1352 bfd_mach_o_asymbol *s = (bfd_mach_o_asymbol *)symbols[i];
1354 /* Compute name index. */
1355 /* An index of 0 always means the empty string. */
1356 if (s->symbol.name == 0 || s->symbol.name[0] == '\0')
1360 str_index = _bfd_stringtab_add (strtab, s->symbol.name, TRUE, FALSE);
1361 if (str_index == (bfd_size_type) -1)
1367 struct mach_o_nlist_64_external raw;
1369 bfd_h_put_32 (abfd, str_index, raw.n_strx);
1370 bfd_h_put_8 (abfd, s->n_type, raw.n_type);
1371 bfd_h_put_8 (abfd, s->n_sect, raw.n_sect);
1372 bfd_h_put_16 (abfd, s->n_desc, raw.n_desc);
1373 bfd_h_put_64 (abfd, s->symbol.section->vma + s->symbol.value,
1376 if (bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
1381 struct mach_o_nlist_external raw;
1383 bfd_h_put_32 (abfd, str_index, raw.n_strx);
1384 bfd_h_put_8 (abfd, s->n_type, raw.n_type);
1385 bfd_h_put_8 (abfd, s->n_sect, raw.n_sect);
1386 bfd_h_put_16 (abfd, s->n_desc, raw.n_desc);
1387 bfd_h_put_32 (abfd, s->symbol.section->vma + s->symbol.value,
1390 if (bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
1394 sym->strsize = _bfd_stringtab_size (strtab);
1395 sym->stroff = mdata->filelen;
1396 mdata->filelen += sym->strsize;
1398 if (_bfd_stringtab_emit (abfd, strtab) != TRUE)
1400 _bfd_stringtab_free (strtab);
1404 struct mach_o_symtab_command_external raw;
1406 bfd_h_put_32 (abfd, sym->symoff, raw.symoff);
1407 bfd_h_put_32 (abfd, sym->nsyms, raw.nsyms);
1408 bfd_h_put_32 (abfd, sym->stroff, raw.stroff);
1409 bfd_h_put_32 (abfd, sym->strsize, raw.strsize);
1411 if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
1412 || bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
1419 _bfd_stringtab_free (strtab);
1423 /* Process the symbols and generate Mach-O specific fields.
1427 bfd_mach_o_mangle_symbols (bfd *abfd)
1430 asymbol **symbols = bfd_get_outsymbols (abfd);
1432 for (i = 0; i < bfd_get_symcount (abfd); i++)
1434 bfd_mach_o_asymbol *s = (bfd_mach_o_asymbol *)symbols[i];
1436 if (s->n_type == BFD_MACH_O_N_UNDF && !(s->symbol.flags & BSF_DEBUGGING))
1438 /* As genuine Mach-O symbols type shouldn't be N_UNDF (undefined
1439 symbols should be N_UNDEF | N_EXT), we suppose the back-end
1440 values haven't been set. */
1441 if (s->symbol.section == bfd_abs_section_ptr)
1442 s->n_type = BFD_MACH_O_N_ABS;
1443 else if (s->symbol.section == bfd_und_section_ptr)
1445 s->n_type = BFD_MACH_O_N_UNDF;
1446 if (s->symbol.flags & BSF_WEAK)
1447 s->n_desc |= BFD_MACH_O_N_WEAK_REF;
1449 else if (s->symbol.section == bfd_com_section_ptr)
1450 s->n_type = BFD_MACH_O_N_UNDF | BFD_MACH_O_N_EXT;
1452 s->n_type = BFD_MACH_O_N_SECT;
1454 if (s->symbol.flags & BSF_GLOBAL)
1455 s->n_type |= BFD_MACH_O_N_EXT;
1458 /* Compute section index. */
1459 if (s->symbol.section != bfd_abs_section_ptr
1460 && s->symbol.section != bfd_und_section_ptr
1461 && s->symbol.section != bfd_com_section_ptr)
1462 s->n_sect = s->symbol.section->target_index;
1464 /* Number symbols. */
1465 s->symbol.udata.i = i;
1471 bfd_mach_o_write_contents (bfd *abfd)
1474 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
1476 if (mdata->header.ncmds == 0)
1477 if (!bfd_mach_o_build_commands (abfd))
1480 /* Now write header information. */
1481 if (mdata->header.filetype == 0)
1483 if (abfd->flags & EXEC_P)
1484 mdata->header.filetype = BFD_MACH_O_MH_EXECUTE;
1485 else if (abfd->flags & DYNAMIC)
1486 mdata->header.filetype = BFD_MACH_O_MH_DYLIB;
1488 mdata->header.filetype = BFD_MACH_O_MH_OBJECT;
1490 if (!bfd_mach_o_write_header (abfd, &mdata->header))
1493 /* Assign a number to each symbols. */
1494 if (!bfd_mach_o_mangle_symbols (abfd))
1497 for (i = 0; i < mdata->header.ncmds; i++)
1499 struct mach_o_load_command_external raw;
1500 bfd_mach_o_load_command *cur = &mdata->commands[i];
1501 unsigned long typeflag;
1503 typeflag = cur->type | (cur->type_required ? BFD_MACH_O_LC_REQ_DYLD : 0);
1505 bfd_h_put_32 (abfd, typeflag, raw.cmd);
1506 bfd_h_put_32 (abfd, cur->len, raw.cmdsize);
1508 if (bfd_seek (abfd, cur->offset, SEEK_SET) != 0
1509 || bfd_bwrite (&raw, BFD_MACH_O_LC_SIZE, abfd) != 8)
1514 case BFD_MACH_O_LC_SEGMENT:
1515 if (bfd_mach_o_write_segment_32 (abfd, cur) != 0)
1518 case BFD_MACH_O_LC_SEGMENT_64:
1519 if (bfd_mach_o_write_segment_64 (abfd, cur) != 0)
1522 case BFD_MACH_O_LC_SYMTAB:
1523 if (!bfd_mach_o_write_symtab (abfd, cur))
1526 case BFD_MACH_O_LC_SYMSEG:
1528 case BFD_MACH_O_LC_THREAD:
1529 case BFD_MACH_O_LC_UNIXTHREAD:
1530 if (bfd_mach_o_write_thread (abfd, cur) != 0)
1533 case BFD_MACH_O_LC_LOADFVMLIB:
1534 case BFD_MACH_O_LC_IDFVMLIB:
1535 case BFD_MACH_O_LC_IDENT:
1536 case BFD_MACH_O_LC_FVMFILE:
1537 case BFD_MACH_O_LC_PREPAGE:
1538 case BFD_MACH_O_LC_DYSYMTAB:
1539 case BFD_MACH_O_LC_LOAD_DYLIB:
1540 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB:
1541 case BFD_MACH_O_LC_ID_DYLIB:
1542 case BFD_MACH_O_LC_REEXPORT_DYLIB:
1543 case BFD_MACH_O_LC_LOAD_UPWARD_DYLIB:
1544 case BFD_MACH_O_LC_LOAD_DYLINKER:
1545 case BFD_MACH_O_LC_ID_DYLINKER:
1546 case BFD_MACH_O_LC_PREBOUND_DYLIB:
1547 case BFD_MACH_O_LC_ROUTINES:
1548 case BFD_MACH_O_LC_SUB_FRAMEWORK:
1551 (*_bfd_error_handler) (_("unable to write unknown load command 0x%lx"),
1552 (unsigned long) cur->type);
1561 bfd_mach_o_append_section_to_segment (bfd_mach_o_segment_command *seg,
1564 bfd_mach_o_section *s = (bfd_mach_o_section *)sec->used_by_bfd;
1565 if (seg->sect_head == NULL)
1568 seg->sect_tail->next = s;
1572 /* Create section Mach-O flags from BFD flags. */
1575 bfd_mach_o_set_section_flags_from_bfd (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
1578 bfd_mach_o_section *s = bfd_mach_o_get_mach_o_section (sec);
1580 /* Create default flags. */
1581 bfd_flags = bfd_get_section_flags (abfd, sec);
1582 if ((bfd_flags & SEC_CODE) == SEC_CODE)
1583 s->flags = BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS
1584 | BFD_MACH_O_S_ATTR_SOME_INSTRUCTIONS
1585 | BFD_MACH_O_S_REGULAR;
1586 else if ((bfd_flags & (SEC_ALLOC | SEC_LOAD)) == SEC_ALLOC)
1587 s->flags = BFD_MACH_O_S_ZEROFILL;
1588 else if (bfd_flags & SEC_DEBUGGING)
1589 s->flags = BFD_MACH_O_S_REGULAR | BFD_MACH_O_S_ATTR_DEBUG;
1591 s->flags = BFD_MACH_O_S_REGULAR;
1594 /* Build Mach-O load commands from the sections. */
1597 bfd_mach_o_build_commands (bfd *abfd)
1599 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
1600 unsigned int wide = mach_o_wide_p (&mdata->header);
1601 bfd_mach_o_segment_command *seg;
1603 bfd_mach_o_load_command *cmd;
1604 bfd_mach_o_load_command *symtab_cmd;
1607 /* Return now if commands are already built. */
1608 if (mdata->header.ncmds)
1611 /* Very simple version: a command (segment) to contain all the sections and
1612 a command for the symbol table. */
1613 mdata->header.ncmds = 2;
1614 mdata->commands = bfd_alloc (abfd, mdata->header.ncmds
1615 * sizeof (bfd_mach_o_load_command));
1616 if (mdata->commands == NULL)
1618 cmd = &mdata->commands[0];
1619 seg = &cmd->command.segment;
1621 seg->nsects = bfd_count_sections (abfd);
1623 /* Set segment command. */
1626 cmd->type = BFD_MACH_O_LC_SEGMENT_64;
1627 cmd->offset = BFD_MACH_O_HEADER_64_SIZE;
1628 cmd->len = BFD_MACH_O_LC_SEGMENT_64_SIZE
1629 + BFD_MACH_O_SECTION_64_SIZE * seg->nsects;
1633 cmd->type = BFD_MACH_O_LC_SEGMENT;
1634 cmd->offset = BFD_MACH_O_HEADER_SIZE;
1635 cmd->len = BFD_MACH_O_LC_SEGMENT_SIZE
1636 + BFD_MACH_O_SECTION_SIZE * seg->nsects;
1638 cmd->type_required = FALSE;
1639 mdata->header.sizeofcmds = cmd->len;
1640 mdata->filelen = cmd->offset + cmd->len;
1642 /* Set symtab command. */
1643 symtab_cmd = &mdata->commands[1];
1645 symtab_cmd->type = BFD_MACH_O_LC_SYMTAB;
1646 symtab_cmd->offset = cmd->offset + cmd->len;
1647 symtab_cmd->len = 6 * 4;
1648 symtab_cmd->type_required = FALSE;
1650 mdata->header.sizeofcmds += symtab_cmd->len;
1651 mdata->filelen += symtab_cmd->len;
1653 /* Fill segment command. */
1654 memset (seg->segname, 0, sizeof (seg->segname));
1656 seg->fileoff = mdata->filelen;
1658 seg->maxprot = BFD_MACH_O_PROT_READ | BFD_MACH_O_PROT_WRITE
1659 | BFD_MACH_O_PROT_EXECUTE;
1660 seg->initprot = seg->maxprot;
1662 seg->sect_head = NULL;
1663 seg->sect_tail = NULL;
1665 /* Create Mach-O sections.
1666 Section type, attribute and align should have been set when the
1667 section was created - either read in or specified. */
1669 for (sec = abfd->sections; sec; sec = sec->next)
1671 unsigned bfd_align = bfd_get_section_alignment (abfd, sec);
1672 bfd_mach_o_section *msect = bfd_mach_o_get_mach_o_section (sec);
1674 bfd_mach_o_append_section_to_segment (seg, sec);
1676 msect->addr = bfd_get_section_vma (abfd, sec);
1677 msect->size = bfd_get_section_size (sec);
1678 /* Use the largest alignment set, in case it was bumped after the
1679 section was created. */
1680 msect->align = msect->align > bfd_align ? msect->align : bfd_align;
1682 if (msect->size != 0)
1684 mdata->filelen = FILE_ALIGN (mdata->filelen, msect->align);
1685 msect->offset = mdata->filelen;
1690 sec->filepos = msect->offset;
1691 sec->target_index = ++target_index;
1693 mdata->filelen += msect->size;
1695 seg->filesize = mdata->filelen - seg->fileoff;
1696 seg->vmsize = seg->filesize;
1701 /* Set the contents of a section. */
1704 bfd_mach_o_set_section_contents (bfd *abfd,
1706 const void * location,
1708 bfd_size_type count)
1712 /* This must be done first, because bfd_set_section_contents is
1713 going to set output_has_begun to TRUE. */
1714 if (! abfd->output_has_begun && ! bfd_mach_o_build_commands (abfd))
1720 pos = section->filepos + offset;
1721 if (bfd_seek (abfd, pos, SEEK_SET) != 0
1722 || bfd_bwrite (location, count, abfd) != count)
1729 bfd_mach_o_sizeof_headers (bfd *a ATTRIBUTE_UNUSED,
1730 struct bfd_link_info *info ATTRIBUTE_UNUSED)
1735 /* Make an empty symbol. This is required only because
1736 bfd_make_section_anyway wants to create a symbol for the section. */
1739 bfd_mach_o_make_empty_symbol (bfd *abfd)
1741 asymbol *new_symbol;
1743 new_symbol = bfd_zalloc (abfd, sizeof (bfd_mach_o_asymbol));
1744 if (new_symbol == NULL)
1746 new_symbol->the_bfd = abfd;
1747 new_symbol->udata.i = 0;
1752 bfd_mach_o_read_header (bfd *abfd, bfd_mach_o_header *header)
1754 struct mach_o_header_external raw;
1756 bfd_vma (*get32) (const void *) = NULL;
1758 /* Just read the magic number. */
1759 if (bfd_seek (abfd, 0, SEEK_SET) != 0
1760 || bfd_bread (raw.magic, sizeof (raw.magic), abfd) != 4)
1763 if (bfd_getb32 (raw.magic) == BFD_MACH_O_MH_MAGIC)
1765 header->byteorder = BFD_ENDIAN_BIG;
1766 header->magic = BFD_MACH_O_MH_MAGIC;
1767 header->version = 1;
1770 else if (bfd_getl32 (raw.magic) == BFD_MACH_O_MH_MAGIC)
1772 header->byteorder = BFD_ENDIAN_LITTLE;
1773 header->magic = BFD_MACH_O_MH_MAGIC;
1774 header->version = 1;
1777 else if (bfd_getb32 (raw.magic) == BFD_MACH_O_MH_MAGIC_64)
1779 header->byteorder = BFD_ENDIAN_BIG;
1780 header->magic = BFD_MACH_O_MH_MAGIC_64;
1781 header->version = 2;
1784 else if (bfd_getl32 (raw.magic) == BFD_MACH_O_MH_MAGIC_64)
1786 header->byteorder = BFD_ENDIAN_LITTLE;
1787 header->magic = BFD_MACH_O_MH_MAGIC_64;
1788 header->version = 2;
1793 header->byteorder = BFD_ENDIAN_UNKNOWN;
1797 /* Once the size of the header is known, read the full header. */
1798 size = mach_o_wide_p (header) ?
1799 BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE;
1801 if (bfd_seek (abfd, 0, SEEK_SET) != 0
1802 || bfd_bread (&raw, size, abfd) != size)
1805 header->cputype = (*get32) (raw.cputype);
1806 header->cpusubtype = (*get32) (raw.cpusubtype);
1807 header->filetype = (*get32) (raw.filetype);
1808 header->ncmds = (*get32) (raw.ncmds);
1809 header->sizeofcmds = (*get32) (raw.sizeofcmds);
1810 header->flags = (*get32) (raw.flags);
1812 if (mach_o_wide_p (header))
1813 header->reserved = (*get32) (raw.reserved);
1819 bfd_mach_o_new_section_hook (bfd *abfd, asection *sec)
1821 bfd_mach_o_section *s;
1822 unsigned bfdalign = bfd_get_section_alignment (abfd, sec);
1824 s = bfd_mach_o_get_mach_o_section (sec);
1828 static const mach_o_section_name_xlat * xlat;
1830 s = (bfd_mach_o_section *) bfd_zalloc (abfd, sizeof (*s));
1833 sec->used_by_bfd = s;
1834 s->bfdsection = sec;
1836 /* Create the Darwin seg/sect name pair from the bfd name.
1837 If this is a canonical name for which a specific paiting exists
1838 there will also be defined flags, type, attribute and alignment
1840 xlat = bfd_mach_o_convert_section_name_to_mach_o (abfd, sec, s);
1843 s->flags = xlat->macho_sectype | xlat->macho_secattr;
1844 s->align = xlat->sectalign > bfdalign ? xlat->sectalign
1846 bfd_set_section_alignment (abfd, sec, s->align);
1847 bfd_flags = bfd_get_section_flags (abfd, sec);
1848 if (bfd_flags == SEC_NO_FLAGS)
1849 bfd_set_section_flags (abfd, sec, xlat->bfd_flags);
1852 /* Create default flags. */
1853 bfd_mach_o_set_section_flags_from_bfd (abfd, sec);
1856 return _bfd_generic_new_section_hook (abfd, sec);
1860 bfd_mach_o_init_section_from_mach_o (bfd *abfd, asection *sec,
1864 bfd_mach_o_section *section;
1866 flags = bfd_get_section_flags (abfd, sec);
1867 section = bfd_mach_o_get_mach_o_section (sec);
1869 /* TODO: see if we should use the xlat system for doing this by
1870 preference and fall back to this for unknown sections. */
1872 if (flags == SEC_NO_FLAGS)
1874 /* Try to guess flags. */
1875 if (section->flags & BFD_MACH_O_S_ATTR_DEBUG)
1876 flags = SEC_DEBUGGING;
1880 if ((section->flags & BFD_MACH_O_SECTION_TYPE_MASK)
1881 != BFD_MACH_O_S_ZEROFILL)
1884 if (prot & BFD_MACH_O_PROT_EXECUTE)
1886 if (prot & BFD_MACH_O_PROT_WRITE)
1888 else if (prot & BFD_MACH_O_PROT_READ)
1889 flags |= SEC_READONLY;
1895 if ((flags & SEC_DEBUGGING) == 0)
1899 if (section->offset != 0)
1900 flags |= SEC_HAS_CONTENTS;
1901 if (section->nreloc != 0)
1904 bfd_set_section_flags (abfd, sec, flags);
1906 sec->vma = section->addr;
1907 sec->lma = section->addr;
1908 sec->size = section->size;
1909 sec->filepos = section->offset;
1910 sec->alignment_power = section->align;
1911 sec->segment_mark = 0;
1912 sec->reloc_count = section->nreloc;
1913 sec->rel_filepos = section->reloff;
1917 bfd_mach_o_make_bfd_section (bfd *abfd,
1918 const unsigned char *segname,
1919 const unsigned char *sectname)
1924 bfd_mach_o_convert_section_name_to_bfd
1925 (abfd, (const char *)segname, (const char *)sectname, &sname, &flags);
1929 return bfd_make_section_anyway_with_flags (abfd, sname, flags);
1933 bfd_mach_o_read_section_32 (bfd *abfd,
1934 unsigned int offset,
1937 struct mach_o_section_32_external raw;
1939 bfd_mach_o_section *section;
1941 if (bfd_seek (abfd, offset, SEEK_SET) != 0
1942 || (bfd_bread (&raw, BFD_MACH_O_SECTION_SIZE, abfd)
1943 != BFD_MACH_O_SECTION_SIZE))
1946 sec = bfd_mach_o_make_bfd_section (abfd, raw.segname, raw.sectname);
1950 section = bfd_mach_o_get_mach_o_section (sec);
1951 memcpy (section->segname, raw.segname, sizeof (raw.segname));
1952 section->segname[BFD_MACH_O_SEGNAME_SIZE] = 0;
1953 memcpy (section->sectname, raw.sectname, sizeof (raw.sectname));
1954 section->sectname[BFD_MACH_O_SECTNAME_SIZE] = 0;
1955 section->addr = bfd_h_get_32 (abfd, raw.addr);
1956 section->size = bfd_h_get_32 (abfd, raw.size);
1957 section->offset = bfd_h_get_32 (abfd, raw.offset);
1958 section->align = bfd_h_get_32 (abfd, raw.align);
1959 section->reloff = bfd_h_get_32 (abfd, raw.reloff);
1960 section->nreloc = bfd_h_get_32 (abfd, raw.nreloc);
1961 section->flags = bfd_h_get_32 (abfd, raw.flags);
1962 section->reserved1 = bfd_h_get_32 (abfd, raw.reserved1);
1963 section->reserved2 = bfd_h_get_32 (abfd, raw.reserved2);
1964 section->reserved3 = 0;
1966 bfd_mach_o_init_section_from_mach_o (abfd, sec, prot);
1972 bfd_mach_o_read_section_64 (bfd *abfd,
1973 unsigned int offset,
1976 struct mach_o_section_64_external raw;
1978 bfd_mach_o_section *section;
1980 if (bfd_seek (abfd, offset, SEEK_SET) != 0
1981 || (bfd_bread (&raw, BFD_MACH_O_SECTION_64_SIZE, abfd)
1982 != BFD_MACH_O_SECTION_64_SIZE))
1985 sec = bfd_mach_o_make_bfd_section (abfd, raw.segname, raw.sectname);
1989 section = bfd_mach_o_get_mach_o_section (sec);
1990 memcpy (section->segname, raw.segname, sizeof (raw.segname));
1991 section->segname[BFD_MACH_O_SEGNAME_SIZE] = 0;
1992 memcpy (section->sectname, raw.sectname, sizeof (raw.sectname));
1993 section->sectname[BFD_MACH_O_SECTNAME_SIZE] = 0;
1994 section->addr = bfd_h_get_64 (abfd, raw.addr);
1995 section->size = bfd_h_get_64 (abfd, raw.size);
1996 section->offset = bfd_h_get_32 (abfd, raw.offset);
1997 section->align = bfd_h_get_32 (abfd, raw.align);
1998 section->reloff = bfd_h_get_32 (abfd, raw.reloff);
1999 section->nreloc = bfd_h_get_32 (abfd, raw.nreloc);
2000 section->flags = bfd_h_get_32 (abfd, raw.flags);
2001 section->reserved1 = bfd_h_get_32 (abfd, raw.reserved1);
2002 section->reserved2 = bfd_h_get_32 (abfd, raw.reserved2);
2003 section->reserved3 = bfd_h_get_32 (abfd, raw.reserved3);
2005 bfd_mach_o_init_section_from_mach_o (abfd, sec, prot);
2011 bfd_mach_o_read_section (bfd *abfd,
2012 unsigned int offset,
2017 return bfd_mach_o_read_section_64 (abfd, offset, prot);
2019 return bfd_mach_o_read_section_32 (abfd, offset, prot);
2023 bfd_mach_o_read_symtab_symbol (bfd *abfd,
2024 bfd_mach_o_symtab_command *sym,
2025 bfd_mach_o_asymbol *s,
2028 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
2029 unsigned int wide = mach_o_wide_p (&mdata->header);
2030 unsigned int symwidth =
2031 wide ? BFD_MACH_O_NLIST_64_SIZE : BFD_MACH_O_NLIST_SIZE;
2032 unsigned int symoff = sym->symoff + (i * symwidth);
2033 struct mach_o_nlist_64_external raw;
2034 unsigned char type = -1;
2035 unsigned char section = -1;
2037 symvalue value = -1;
2038 unsigned long stroff = -1;
2039 unsigned int symtype = -1;
2041 BFD_ASSERT (sym->strtab != NULL);
2043 if (bfd_seek (abfd, symoff, SEEK_SET) != 0
2044 || bfd_bread (&raw, symwidth, abfd) != symwidth)
2046 (*_bfd_error_handler)
2047 (_("bfd_mach_o_read_symtab_symbol: unable to read %d bytes at %lu"),
2048 symwidth, (unsigned long) symoff);
2052 stroff = bfd_h_get_32 (abfd, raw.n_strx);
2053 type = bfd_h_get_8 (abfd, raw.n_type);
2054 symtype = type & BFD_MACH_O_N_TYPE;
2055 section = bfd_h_get_8 (abfd, raw.n_sect);
2056 desc = bfd_h_get_16 (abfd, raw.n_desc);
2058 value = bfd_h_get_64 (abfd, raw.n_value);
2060 value = bfd_h_get_32 (abfd, raw.n_value);
2062 if (stroff >= sym->strsize)
2064 (*_bfd_error_handler)
2065 (_("bfd_mach_o_read_symtab_symbol: name out of range (%lu >= %lu)"),
2066 (unsigned long) stroff,
2067 (unsigned long) sym->strsize);
2071 s->symbol.the_bfd = abfd;
2072 s->symbol.name = sym->strtab + stroff;
2073 s->symbol.value = value;
2074 s->symbol.flags = 0x0;
2075 s->symbol.udata.i = 0;
2077 s->n_sect = section;
2080 if (type & BFD_MACH_O_N_STAB)
2082 s->symbol.flags |= BSF_DEBUGGING;
2083 s->symbol.section = bfd_und_section_ptr;
2095 if ((section > 0) && (section <= mdata->nsects))
2097 s->symbol.section = mdata->sections[section - 1]->bfdsection;
2099 s->symbol.value - mdata->sections[section - 1]->addr;
2106 if (type & BFD_MACH_O_N_PEXT)
2107 s->symbol.flags |= BSF_GLOBAL;
2109 if (type & BFD_MACH_O_N_EXT)
2110 s->symbol.flags |= BSF_GLOBAL;
2112 if (!(type & (BFD_MACH_O_N_PEXT | BFD_MACH_O_N_EXT)))
2113 s->symbol.flags |= BSF_LOCAL;
2117 case BFD_MACH_O_N_UNDF:
2118 if (type == (BFD_MACH_O_N_UNDF | BFD_MACH_O_N_EXT)
2119 && s->symbol.value != 0)
2121 /* A common symbol. */
2122 s->symbol.section = bfd_com_section_ptr;
2123 s->symbol.flags = BSF_NO_FLAGS;
2127 s->symbol.section = bfd_und_section_ptr;
2128 if (s->n_desc & BFD_MACH_O_N_WEAK_REF)
2129 s->symbol.flags |= BSF_WEAK;
2132 case BFD_MACH_O_N_PBUD:
2133 s->symbol.section = bfd_und_section_ptr;
2135 case BFD_MACH_O_N_ABS:
2136 s->symbol.section = bfd_abs_section_ptr;
2138 case BFD_MACH_O_N_SECT:
2139 if ((section > 0) && (section <= mdata->nsects))
2141 s->symbol.section = mdata->sections[section - 1]->bfdsection;
2143 s->symbol.value - mdata->sections[section - 1]->addr;
2147 /* Mach-O uses 0 to mean "no section"; not an error. */
2150 (*_bfd_error_handler) (_("bfd_mach_o_read_symtab_symbol: "
2151 "symbol \"%s\" specified invalid section %d (max %lu): setting to undefined"),
2152 s->symbol.name, section, mdata->nsects);
2154 s->symbol.section = bfd_und_section_ptr;
2157 case BFD_MACH_O_N_INDR:
2158 /* FIXME: we don't follow the BFD convention as this indirect symbol
2159 won't be followed by the referenced one. This looks harmless
2160 unless we start using the linker. */
2161 s->symbol.flags |= BSF_INDIRECT;
2162 s->symbol.section = bfd_ind_section_ptr;
2163 s->symbol.value = 0;
2166 (*_bfd_error_handler) (_("bfd_mach_o_read_symtab_symbol: "
2167 "symbol \"%s\" specified invalid type field 0x%x: setting to undefined"),
2168 s->symbol.name, symtype);
2169 s->symbol.section = bfd_und_section_ptr;
2178 bfd_mach_o_read_symtab_strtab (bfd *abfd)
2180 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
2181 bfd_mach_o_symtab_command *sym = mdata->symtab;
2183 /* Fail if there is no symtab. */
2187 /* Success if already loaded. */
2191 if (abfd->flags & BFD_IN_MEMORY)
2193 struct bfd_in_memory *b;
2195 b = (struct bfd_in_memory *) abfd->iostream;
2197 if ((sym->stroff + sym->strsize) > b->size)
2199 bfd_set_error (bfd_error_file_truncated);
2202 sym->strtab = (char *) b->buffer + sym->stroff;
2206 sym->strtab = bfd_alloc (abfd, sym->strsize);
2207 if (sym->strtab == NULL)
2210 if (bfd_seek (abfd, sym->stroff, SEEK_SET) != 0
2211 || bfd_bread (sym->strtab, sym->strsize, abfd) != sym->strsize)
2213 bfd_set_error (bfd_error_file_truncated);
2222 bfd_mach_o_read_symtab_symbols (bfd *abfd)
2224 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
2225 bfd_mach_o_symtab_command *sym = mdata->symtab;
2228 if (sym == NULL || sym->symbols)
2230 /* Return now if there are no symbols or if already loaded. */
2234 sym->symbols = bfd_alloc (abfd, sym->nsyms * sizeof (bfd_mach_o_asymbol));
2236 if (sym->symbols == NULL)
2238 (*_bfd_error_handler) (_("bfd_mach_o_read_symtab_symbols: unable to allocate memory for symbols"));
2242 if (!bfd_mach_o_read_symtab_strtab (abfd))
2245 for (i = 0; i < sym->nsyms; i++)
2247 if (!bfd_mach_o_read_symtab_symbol (abfd, sym, &sym->symbols[i], i))
2255 bfd_mach_o_i386_flavour_string (unsigned int flavour)
2257 switch ((int) flavour)
2259 case BFD_MACH_O_x86_THREAD_STATE32: return "x86_THREAD_STATE32";
2260 case BFD_MACH_O_x86_FLOAT_STATE32: return "x86_FLOAT_STATE32";
2261 case BFD_MACH_O_x86_EXCEPTION_STATE32: return "x86_EXCEPTION_STATE32";
2262 case BFD_MACH_O_x86_THREAD_STATE64: return "x86_THREAD_STATE64";
2263 case BFD_MACH_O_x86_FLOAT_STATE64: return "x86_FLOAT_STATE64";
2264 case BFD_MACH_O_x86_EXCEPTION_STATE64: return "x86_EXCEPTION_STATE64";
2265 case BFD_MACH_O_x86_THREAD_STATE: return "x86_THREAD_STATE";
2266 case BFD_MACH_O_x86_FLOAT_STATE: return "x86_FLOAT_STATE";
2267 case BFD_MACH_O_x86_EXCEPTION_STATE: return "x86_EXCEPTION_STATE";
2268 case BFD_MACH_O_x86_DEBUG_STATE32: return "x86_DEBUG_STATE32";
2269 case BFD_MACH_O_x86_DEBUG_STATE64: return "x86_DEBUG_STATE64";
2270 case BFD_MACH_O_x86_DEBUG_STATE: return "x86_DEBUG_STATE";
2271 case BFD_MACH_O_x86_THREAD_STATE_NONE: return "x86_THREAD_STATE_NONE";
2272 default: return "UNKNOWN";
2277 bfd_mach_o_ppc_flavour_string (unsigned int flavour)
2279 switch ((int) flavour)
2281 case BFD_MACH_O_PPC_THREAD_STATE: return "PPC_THREAD_STATE";
2282 case BFD_MACH_O_PPC_FLOAT_STATE: return "PPC_FLOAT_STATE";
2283 case BFD_MACH_O_PPC_EXCEPTION_STATE: return "PPC_EXCEPTION_STATE";
2284 case BFD_MACH_O_PPC_VECTOR_STATE: return "PPC_VECTOR_STATE";
2285 case BFD_MACH_O_PPC_THREAD_STATE64: return "PPC_THREAD_STATE64";
2286 case BFD_MACH_O_PPC_EXCEPTION_STATE64: return "PPC_EXCEPTION_STATE64";
2287 default: return "UNKNOWN";
2292 bfd_mach_o_read_dylinker (bfd *abfd, bfd_mach_o_load_command *command)
2294 bfd_mach_o_dylinker_command *cmd = &command->command.dylinker;
2295 struct mach_o_str_command_external raw;
2296 unsigned int nameoff;
2298 BFD_ASSERT ((command->type == BFD_MACH_O_LC_ID_DYLINKER)
2299 || (command->type == BFD_MACH_O_LC_LOAD_DYLINKER));
2301 if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
2302 || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
2305 nameoff = bfd_h_get_32 (abfd, raw.str);
2307 cmd->name_offset = command->offset + nameoff;
2308 cmd->name_len = command->len - nameoff;
2309 cmd->name_str = bfd_alloc (abfd, cmd->name_len);
2310 if (cmd->name_str == NULL)
2312 if (bfd_seek (abfd, cmd->name_offset, SEEK_SET) != 0
2313 || bfd_bread (cmd->name_str, cmd->name_len, abfd) != cmd->name_len)
2319 bfd_mach_o_read_dylib (bfd *abfd, bfd_mach_o_load_command *command)
2321 bfd_mach_o_dylib_command *cmd = &command->command.dylib;
2322 struct mach_o_dylib_command_external raw;
2323 unsigned int nameoff;
2325 switch (command->type)
2327 case BFD_MACH_O_LC_LOAD_DYLIB:
2328 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB:
2329 case BFD_MACH_O_LC_ID_DYLIB:
2330 case BFD_MACH_O_LC_REEXPORT_DYLIB:
2331 case BFD_MACH_O_LC_LOAD_UPWARD_DYLIB:
2338 if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
2339 || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
2342 nameoff = bfd_h_get_32 (abfd, raw.name);
2343 cmd->timestamp = bfd_h_get_32 (abfd, raw.timestamp);
2344 cmd->current_version = bfd_h_get_32 (abfd, raw.current_version);
2345 cmd->compatibility_version = bfd_h_get_32 (abfd, raw.compatibility_version);
2347 cmd->name_offset = command->offset + nameoff;
2348 cmd->name_len = command->len - nameoff;
2349 cmd->name_str = bfd_alloc (abfd, cmd->name_len);
2350 if (cmd->name_str == NULL)
2352 if (bfd_seek (abfd, cmd->name_offset, SEEK_SET) != 0
2353 || bfd_bread (cmd->name_str, cmd->name_len, abfd) != cmd->name_len)
2359 bfd_mach_o_read_prebound_dylib (bfd *abfd ATTRIBUTE_UNUSED,
2360 bfd_mach_o_load_command *command ATTRIBUTE_UNUSED)
2362 /* bfd_mach_o_prebound_dylib_command *cmd = &command->command.prebound_dylib; */
2364 BFD_ASSERT (command->type == BFD_MACH_O_LC_PREBOUND_DYLIB);
2369 bfd_mach_o_read_thread (bfd *abfd, bfd_mach_o_load_command *command)
2371 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
2372 bfd_mach_o_thread_command *cmd = &command->command.thread;
2373 unsigned int offset;
2374 unsigned int nflavours;
2377 BFD_ASSERT ((command->type == BFD_MACH_O_LC_THREAD)
2378 || (command->type == BFD_MACH_O_LC_UNIXTHREAD));
2380 /* Count the number of threads. */
2383 while (offset != command->len)
2385 struct mach_o_thread_command_external raw;
2387 if (offset >= command->len)
2390 if (bfd_seek (abfd, command->offset + offset, SEEK_SET) != 0
2391 || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
2394 offset += sizeof (raw) + bfd_h_get_32 (abfd, raw.count) * 4;
2398 /* Allocate threads. */
2399 cmd->flavours = bfd_alloc
2400 (abfd, nflavours * sizeof (bfd_mach_o_thread_flavour));
2401 if (cmd->flavours == NULL)
2403 cmd->nflavours = nflavours;
2407 while (offset != command->len)
2409 struct mach_o_thread_command_external raw;
2411 if (offset >= command->len)
2414 if (nflavours >= cmd->nflavours)
2417 if (bfd_seek (abfd, command->offset + offset, SEEK_SET) != 0
2418 || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
2421 cmd->flavours[nflavours].flavour = bfd_h_get_32 (abfd, raw.flavour);
2422 cmd->flavours[nflavours].offset = command->offset + offset + sizeof (raw);
2423 cmd->flavours[nflavours].size = bfd_h_get_32 (abfd, raw.count) * 4;
2424 offset += cmd->flavours[nflavours].size + sizeof (raw);
2428 for (i = 0; i < nflavours; i++)
2431 unsigned int snamelen;
2433 const char *flavourstr;
2434 const char *prefix = "LC_THREAD";
2437 switch (mdata->header.cputype)
2439 case BFD_MACH_O_CPU_TYPE_POWERPC:
2440 case BFD_MACH_O_CPU_TYPE_POWERPC_64:
2441 flavourstr = bfd_mach_o_ppc_flavour_string (cmd->flavours[i].flavour);
2443 case BFD_MACH_O_CPU_TYPE_I386:
2444 case BFD_MACH_O_CPU_TYPE_X86_64:
2445 flavourstr = bfd_mach_o_i386_flavour_string (cmd->flavours[i].flavour);
2448 flavourstr = "UNKNOWN_ARCHITECTURE";
2452 snamelen = strlen (prefix) + 1 + 20 + 1 + strlen (flavourstr) + 1;
2453 sname = bfd_alloc (abfd, snamelen);
2459 sprintf (sname, "%s.%s.%u", prefix, flavourstr, j);
2460 if (bfd_get_section_by_name (abfd, sname) == NULL)
2465 bfdsec = bfd_make_section_with_flags (abfd, sname, SEC_HAS_CONTENTS);
2469 bfdsec->size = cmd->flavours[i].size;
2470 bfdsec->filepos = cmd->flavours[i].offset;
2471 bfdsec->alignment_power = 0x0;
2473 cmd->section = bfdsec;
2480 bfd_mach_o_read_dysymtab (bfd *abfd, bfd_mach_o_load_command *command)
2482 bfd_mach_o_dysymtab_command *cmd = &command->command.dysymtab;
2483 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
2485 BFD_ASSERT (command->type == BFD_MACH_O_LC_DYSYMTAB);
2488 struct mach_o_dysymtab_command_external raw;
2490 if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
2491 || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
2494 cmd->ilocalsym = bfd_h_get_32 (abfd, raw.ilocalsym);
2495 cmd->nlocalsym = bfd_h_get_32 (abfd, raw.nlocalsym);
2496 cmd->iextdefsym = bfd_h_get_32 (abfd, raw.iextdefsym);
2497 cmd->nextdefsym = bfd_h_get_32 (abfd, raw.nextdefsym);
2498 cmd->iundefsym = bfd_h_get_32 (abfd, raw.iundefsym);
2499 cmd->nundefsym = bfd_h_get_32 (abfd, raw.nundefsym);
2500 cmd->tocoff = bfd_h_get_32 (abfd, raw.tocoff);
2501 cmd->ntoc = bfd_h_get_32 (abfd, raw.ntoc);
2502 cmd->modtaboff = bfd_h_get_32 (abfd, raw.modtaboff);
2503 cmd->nmodtab = bfd_h_get_32 (abfd, raw.nmodtab);
2504 cmd->extrefsymoff = bfd_h_get_32 (abfd, raw.extrefsymoff);
2505 cmd->nextrefsyms = bfd_h_get_32 (abfd, raw.nextrefsyms);
2506 cmd->indirectsymoff = bfd_h_get_32 (abfd, raw.indirectsymoff);
2507 cmd->nindirectsyms = bfd_h_get_32 (abfd, raw.nindirectsyms);
2508 cmd->extreloff = bfd_h_get_32 (abfd, raw.extreloff);
2509 cmd->nextrel = bfd_h_get_32 (abfd, raw.nextrel);
2510 cmd->locreloff = bfd_h_get_32 (abfd, raw.locreloff);
2511 cmd->nlocrel = bfd_h_get_32 (abfd, raw.nlocrel);
2514 if (cmd->nmodtab != 0)
2517 int wide = bfd_mach_o_wide_p (abfd);
2518 unsigned int module_len = wide ? 56 : 52;
2521 bfd_alloc (abfd, cmd->nmodtab * sizeof (bfd_mach_o_dylib_module));
2522 if (cmd->dylib_module == NULL)
2525 if (bfd_seek (abfd, cmd->modtaboff, SEEK_SET) != 0)
2528 for (i = 0; i < cmd->nmodtab; i++)
2530 bfd_mach_o_dylib_module *module = &cmd->dylib_module[i];
2532 unsigned char buf[56];
2534 if (bfd_bread ((void *) buf, module_len, abfd) != module_len)
2537 module->module_name_idx = bfd_h_get_32 (abfd, buf + 0);
2538 module->iextdefsym = bfd_h_get_32 (abfd, buf + 4);
2539 module->nextdefsym = bfd_h_get_32 (abfd, buf + 8);
2540 module->irefsym = bfd_h_get_32 (abfd, buf + 12);
2541 module->nrefsym = bfd_h_get_32 (abfd, buf + 16);
2542 module->ilocalsym = bfd_h_get_32 (abfd, buf + 20);
2543 module->nlocalsym = bfd_h_get_32 (abfd, buf + 24);
2544 module->iextrel = bfd_h_get_32 (abfd, buf + 28);
2545 module->nextrel = bfd_h_get_32 (abfd, buf + 32);
2546 v = bfd_h_get_32 (abfd, buf +36);
2547 module->iinit = v & 0xffff;
2548 module->iterm = (v >> 16) & 0xffff;
2549 v = bfd_h_get_32 (abfd, buf + 40);
2550 module->ninit = v & 0xffff;
2551 module->nterm = (v >> 16) & 0xffff;
2554 module->objc_module_info_size = bfd_h_get_32 (abfd, buf + 44);
2555 module->objc_module_info_addr = bfd_h_get_64 (abfd, buf + 48);
2559 module->objc_module_info_addr = bfd_h_get_32 (abfd, buf + 44);
2560 module->objc_module_info_size = bfd_h_get_32 (abfd, buf + 48);
2569 cmd->dylib_toc = bfd_alloc
2570 (abfd, cmd->ntoc * sizeof (bfd_mach_o_dylib_table_of_content));
2571 if (cmd->dylib_toc == NULL)
2574 if (bfd_seek (abfd, cmd->tocoff, SEEK_SET) != 0)
2577 for (i = 0; i < cmd->ntoc; i++)
2579 struct mach_o_dylib_table_of_contents_external raw;
2580 bfd_mach_o_dylib_table_of_content *toc = &cmd->dylib_toc[i];
2582 if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
2585 toc->symbol_index = bfd_h_get_32 (abfd, raw.symbol_index);
2586 toc->module_index = bfd_h_get_32 (abfd, raw.module_index);
2590 if (cmd->nindirectsyms != 0)
2594 cmd->indirect_syms = bfd_alloc
2595 (abfd, cmd->nindirectsyms * sizeof (unsigned int));
2596 if (cmd->indirect_syms == NULL)
2599 if (bfd_seek (abfd, cmd->indirectsymoff, SEEK_SET) != 0)
2602 for (i = 0; i < cmd->nindirectsyms; i++)
2604 unsigned char raw[4];
2605 unsigned int *is = &cmd->indirect_syms[i];
2607 if (bfd_bread (raw, sizeof (raw), abfd) != sizeof (raw))
2610 *is = bfd_h_get_32 (abfd, raw);
2614 if (cmd->nextrefsyms != 0)
2619 cmd->ext_refs = bfd_alloc
2620 (abfd, cmd->nextrefsyms * sizeof (bfd_mach_o_dylib_reference));
2621 if (cmd->ext_refs == NULL)
2624 if (bfd_seek (abfd, cmd->extrefsymoff, SEEK_SET) != 0)
2627 for (i = 0; i < cmd->nextrefsyms; i++)
2629 unsigned char raw[4];
2630 bfd_mach_o_dylib_reference *ref = &cmd->ext_refs[i];
2632 if (bfd_bread (raw, sizeof (raw), abfd) != sizeof (raw))
2635 /* Fields isym and flags are written as bit-fields, thus we need
2636 a specific processing for endianness. */
2637 v = bfd_h_get_32 (abfd, raw);
2638 if (bfd_big_endian (abfd))
2640 ref->isym = (v >> 8) & 0xffffff;
2641 ref->flags = v & 0xff;
2645 ref->isym = v & 0xffffff;
2646 ref->flags = (v >> 24) & 0xff;
2651 if (mdata->dysymtab)
2653 mdata->dysymtab = cmd;
2659 bfd_mach_o_read_symtab (bfd *abfd, bfd_mach_o_load_command *command)
2661 bfd_mach_o_symtab_command *symtab = &command->command.symtab;
2662 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
2663 struct mach_o_symtab_command_external raw;
2665 BFD_ASSERT (command->type == BFD_MACH_O_LC_SYMTAB);
2667 if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
2668 || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
2671 symtab->symoff = bfd_h_get_32 (abfd, raw.symoff);
2672 symtab->nsyms = bfd_h_get_32 (abfd, raw.nsyms);
2673 symtab->stroff = bfd_h_get_32 (abfd, raw.stroff);
2674 symtab->strsize = bfd_h_get_32 (abfd, raw.strsize);
2675 symtab->symbols = NULL;
2676 symtab->strtab = NULL;
2678 if (symtab->nsyms != 0)
2679 abfd->flags |= HAS_SYMS;
2683 mdata->symtab = symtab;
2688 bfd_mach_o_read_uuid (bfd *abfd, bfd_mach_o_load_command *command)
2690 bfd_mach_o_uuid_command *cmd = &command->command.uuid;
2692 BFD_ASSERT (command->type == BFD_MACH_O_LC_UUID);
2694 if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
2695 || bfd_bread (cmd->uuid, 16, abfd) != 16)
2702 bfd_mach_o_read_linkedit (bfd *abfd, bfd_mach_o_load_command *command)
2704 bfd_mach_o_linkedit_command *cmd = &command->command.linkedit;
2705 struct mach_o_linkedit_data_command_external raw;
2707 if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
2708 || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
2711 cmd->dataoff = bfd_get_32 (abfd, raw.dataoff);
2712 cmd->datasize = bfd_get_32 (abfd, raw.datasize);
2717 bfd_mach_o_read_str (bfd *abfd, bfd_mach_o_load_command *command)
2719 bfd_mach_o_str_command *cmd = &command->command.str;
2720 struct mach_o_str_command_external raw;
2723 if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
2724 || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
2727 off = bfd_get_32 (abfd, raw.str);
2728 cmd->stroff = command->offset + off;
2729 cmd->str_len = command->len - off;
2730 cmd->str = bfd_alloc (abfd, cmd->str_len);
2731 if (cmd->str == NULL)
2733 if (bfd_seek (abfd, cmd->stroff, SEEK_SET) != 0
2734 || bfd_bread ((void *) cmd->str, cmd->str_len, abfd) != cmd->str_len)
2740 bfd_mach_o_read_dyld_info (bfd *abfd, bfd_mach_o_load_command *command)
2742 bfd_mach_o_dyld_info_command *cmd = &command->command.dyld_info;
2743 struct mach_o_dyld_info_command_external raw;
2745 if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
2746 || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
2749 cmd->rebase_off = bfd_get_32 (abfd, raw.rebase_off);
2750 cmd->rebase_size = bfd_get_32 (abfd, raw.rebase_size);
2751 cmd->bind_off = bfd_get_32 (abfd, raw.bind_off);
2752 cmd->bind_size = bfd_get_32 (abfd, raw.bind_size);
2753 cmd->weak_bind_off = bfd_get_32 (abfd, raw.weak_bind_off);
2754 cmd->weak_bind_size = bfd_get_32 (abfd, raw.weak_bind_size);
2755 cmd->lazy_bind_off = bfd_get_32 (abfd, raw.lazy_bind_off);
2756 cmd->lazy_bind_size = bfd_get_32 (abfd, raw.lazy_bind_size);
2757 cmd->export_off = bfd_get_32 (abfd, raw.export_off);
2758 cmd->export_size = bfd_get_32 (abfd, raw.export_size);
2763 bfd_mach_o_read_version_min (bfd *abfd, bfd_mach_o_load_command *command)
2765 bfd_mach_o_version_min_command *cmd = &command->command.version_min;
2766 struct mach_o_version_min_command_external raw;
2769 if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
2770 || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
2773 ver = bfd_get_32 (abfd, raw.version);
2774 cmd->rel = ver >> 16;
2775 cmd->maj = ver >> 8;
2777 cmd->reserved = bfd_get_32 (abfd, raw.reserved);
2782 bfd_mach_o_read_segment (bfd *abfd,
2783 bfd_mach_o_load_command *command,
2786 bfd_mach_o_segment_command *seg = &command->command.segment;
2791 struct mach_o_segment_command_64_external raw;
2793 BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT_64);
2795 if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
2796 || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
2799 memcpy (seg->segname, raw.segname, 16);
2800 seg->segname[16] = '\0';
2802 seg->vmaddr = bfd_h_get_64 (abfd, raw.vmaddr);
2803 seg->vmsize = bfd_h_get_64 (abfd, raw.vmsize);
2804 seg->fileoff = bfd_h_get_64 (abfd, raw.fileoff);
2805 seg->filesize = bfd_h_get_64 (abfd, raw.filesize);
2806 seg->maxprot = bfd_h_get_32 (abfd, raw.maxprot);
2807 seg->initprot = bfd_h_get_32 (abfd, raw.initprot);
2808 seg->nsects = bfd_h_get_32 (abfd, raw.nsects);
2809 seg->flags = bfd_h_get_32 (abfd, raw.flags);
2813 struct mach_o_segment_command_32_external raw;
2815 BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT);
2817 if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
2818 || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
2821 memcpy (seg->segname, raw.segname, 16);
2822 seg->segname[16] = '\0';
2824 seg->vmaddr = bfd_h_get_32 (abfd, raw.vmaddr);
2825 seg->vmsize = bfd_h_get_32 (abfd, raw.vmsize);
2826 seg->fileoff = bfd_h_get_32 (abfd, raw.fileoff);
2827 seg->filesize = bfd_h_get_32 (abfd, raw.filesize);
2828 seg->maxprot = bfd_h_get_32 (abfd, raw.maxprot);
2829 seg->initprot = bfd_h_get_32 (abfd, raw.initprot);
2830 seg->nsects = bfd_h_get_32 (abfd, raw.nsects);
2831 seg->flags = bfd_h_get_32 (abfd, raw.flags);
2833 seg->sect_head = NULL;
2834 seg->sect_tail = NULL;
2836 for (i = 0; i < seg->nsects; i++)
2842 segoff = command->offset + BFD_MACH_O_LC_SEGMENT_64_SIZE
2843 + (i * BFD_MACH_O_SECTION_64_SIZE);
2845 segoff = command->offset + BFD_MACH_O_LC_SEGMENT_SIZE
2846 + (i * BFD_MACH_O_SECTION_SIZE);
2848 sec = bfd_mach_o_read_section (abfd, segoff, seg->initprot, wide);
2852 bfd_mach_o_append_section_to_segment (seg, sec);
2859 bfd_mach_o_read_segment_32 (bfd *abfd, bfd_mach_o_load_command *command)
2861 return bfd_mach_o_read_segment (abfd, command, 0);
2865 bfd_mach_o_read_segment_64 (bfd *abfd, bfd_mach_o_load_command *command)
2867 return bfd_mach_o_read_segment (abfd, command, 1);
2871 bfd_mach_o_read_command (bfd *abfd, bfd_mach_o_load_command *command)
2873 struct mach_o_load_command_external raw;
2876 /* Read command type and length. */
2877 if (bfd_seek (abfd, command->offset, SEEK_SET) != 0
2878 || bfd_bread (&raw, BFD_MACH_O_LC_SIZE, abfd) != BFD_MACH_O_LC_SIZE)
2881 cmd = bfd_h_get_32 (abfd, raw.cmd);
2882 command->type = cmd & ~BFD_MACH_O_LC_REQ_DYLD;
2883 command->type_required = cmd & BFD_MACH_O_LC_REQ_DYLD ? TRUE : FALSE;
2884 command->len = bfd_h_get_32 (abfd, raw.cmdsize);
2886 switch (command->type)
2888 case BFD_MACH_O_LC_SEGMENT:
2889 if (bfd_mach_o_read_segment_32 (abfd, command) != 0)
2892 case BFD_MACH_O_LC_SEGMENT_64:
2893 if (bfd_mach_o_read_segment_64 (abfd, command) != 0)
2896 case BFD_MACH_O_LC_SYMTAB:
2897 if (bfd_mach_o_read_symtab (abfd, command) != 0)
2900 case BFD_MACH_O_LC_SYMSEG:
2902 case BFD_MACH_O_LC_THREAD:
2903 case BFD_MACH_O_LC_UNIXTHREAD:
2904 if (bfd_mach_o_read_thread (abfd, command) != 0)
2907 case BFD_MACH_O_LC_LOAD_DYLINKER:
2908 case BFD_MACH_O_LC_ID_DYLINKER:
2909 if (bfd_mach_o_read_dylinker (abfd, command) != 0)
2912 case BFD_MACH_O_LC_LOAD_DYLIB:
2913 case BFD_MACH_O_LC_ID_DYLIB:
2914 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB:
2915 case BFD_MACH_O_LC_REEXPORT_DYLIB:
2916 case BFD_MACH_O_LC_LOAD_UPWARD_DYLIB:
2917 if (bfd_mach_o_read_dylib (abfd, command) != 0)
2920 case BFD_MACH_O_LC_PREBOUND_DYLIB:
2921 if (bfd_mach_o_read_prebound_dylib (abfd, command) != 0)
2924 case BFD_MACH_O_LC_LOADFVMLIB:
2925 case BFD_MACH_O_LC_IDFVMLIB:
2926 case BFD_MACH_O_LC_IDENT:
2927 case BFD_MACH_O_LC_FVMFILE:
2928 case BFD_MACH_O_LC_PREPAGE:
2929 case BFD_MACH_O_LC_ROUTINES:
2930 case BFD_MACH_O_LC_ROUTINES_64:
2932 case BFD_MACH_O_LC_SUB_FRAMEWORK:
2933 case BFD_MACH_O_LC_SUB_UMBRELLA:
2934 case BFD_MACH_O_LC_SUB_LIBRARY:
2935 case BFD_MACH_O_LC_SUB_CLIENT:
2936 case BFD_MACH_O_LC_RPATH:
2937 if (bfd_mach_o_read_str (abfd, command) != 0)
2940 case BFD_MACH_O_LC_DYSYMTAB:
2941 if (bfd_mach_o_read_dysymtab (abfd, command) != 0)
2944 case BFD_MACH_O_LC_TWOLEVEL_HINTS:
2945 case BFD_MACH_O_LC_PREBIND_CKSUM:
2947 case BFD_MACH_O_LC_UUID:
2948 if (bfd_mach_o_read_uuid (abfd, command) != 0)
2951 case BFD_MACH_O_LC_CODE_SIGNATURE:
2952 case BFD_MACH_O_LC_SEGMENT_SPLIT_INFO:
2953 case BFD_MACH_O_LC_FUNCTION_STARTS:
2954 if (bfd_mach_o_read_linkedit (abfd, command) != 0)
2957 case BFD_MACH_O_LC_DYLD_INFO:
2958 if (bfd_mach_o_read_dyld_info (abfd, command) != 0)
2961 case BFD_MACH_O_LC_VERSION_MIN_MACOSX:
2962 case BFD_MACH_O_LC_VERSION_MIN_IPHONEOS:
2963 if (!bfd_mach_o_read_version_min (abfd, command))
2967 (*_bfd_error_handler)(_("%B: unable to read unknown load command 0x%lx"),
2968 abfd, (unsigned long) command->type);
2976 bfd_mach_o_flatten_sections (bfd *abfd)
2978 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
2982 /* Count total number of sections. */
2985 for (i = 0; i < mdata->header.ncmds; i++)
2987 if (mdata->commands[i].type == BFD_MACH_O_LC_SEGMENT
2988 || mdata->commands[i].type == BFD_MACH_O_LC_SEGMENT_64)
2990 bfd_mach_o_segment_command *seg;
2992 seg = &mdata->commands[i].command.segment;
2993 mdata->nsects += seg->nsects;
2997 /* Allocate sections array. */
2998 mdata->sections = bfd_alloc (abfd,
2999 mdata->nsects * sizeof (bfd_mach_o_section *));
3001 /* Fill the array. */
3004 for (i = 0; i < mdata->header.ncmds; i++)
3006 if (mdata->commands[i].type == BFD_MACH_O_LC_SEGMENT
3007 || mdata->commands[i].type == BFD_MACH_O_LC_SEGMENT_64)
3009 bfd_mach_o_segment_command *seg;
3010 bfd_mach_o_section *sec;
3012 seg = &mdata->commands[i].command.segment;
3013 BFD_ASSERT (csect + seg->nsects <= mdata->nsects);
3015 for (sec = seg->sect_head; sec != NULL; sec = sec->next)
3016 mdata->sections[csect++] = sec;
3022 bfd_mach_o_scan_start_address (bfd *abfd)
3024 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
3025 bfd_mach_o_thread_command *cmd = NULL;
3028 for (i = 0; i < mdata->header.ncmds; i++)
3029 if ((mdata->commands[i].type == BFD_MACH_O_LC_THREAD) ||
3030 (mdata->commands[i].type == BFD_MACH_O_LC_UNIXTHREAD))
3032 cmd = &mdata->commands[i].command.thread;
3039 /* FIXME: create a subtarget hook ? */
3040 for (i = 0; i < cmd->nflavours; i++)
3042 if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_I386)
3043 && (cmd->flavours[i].flavour
3044 == (unsigned long) BFD_MACH_O_x86_THREAD_STATE32))
3046 unsigned char buf[4];
3048 if (bfd_seek (abfd, cmd->flavours[i].offset + 40, SEEK_SET) != 0
3049 || bfd_bread (buf, 4, abfd) != 4)
3052 abfd->start_address = bfd_h_get_32 (abfd, buf);
3054 else if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_POWERPC)
3055 && (cmd->flavours[i].flavour == BFD_MACH_O_PPC_THREAD_STATE))
3057 unsigned char buf[4];
3059 if (bfd_seek (abfd, cmd->flavours[i].offset + 0, SEEK_SET) != 0
3060 || bfd_bread (buf, 4, abfd) != 4)
3063 abfd->start_address = bfd_h_get_32 (abfd, buf);
3065 else if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_POWERPC_64)
3066 && (cmd->flavours[i].flavour == BFD_MACH_O_PPC_THREAD_STATE64))
3068 unsigned char buf[8];
3070 if (bfd_seek (abfd, cmd->flavours[i].offset + 0, SEEK_SET) != 0
3071 || bfd_bread (buf, 8, abfd) != 8)
3074 abfd->start_address = bfd_h_get_64 (abfd, buf);
3076 else if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_X86_64)
3077 && (cmd->flavours[i].flavour == BFD_MACH_O_x86_THREAD_STATE64))
3079 unsigned char buf[8];
3081 if (bfd_seek (abfd, cmd->flavours[i].offset + (16 * 8), SEEK_SET) != 0
3082 || bfd_bread (buf, 8, abfd) != 8)
3085 abfd->start_address = bfd_h_get_64 (abfd, buf);
3093 bfd_mach_o_set_arch_mach (bfd *abfd,
3094 enum bfd_architecture arch,
3095 unsigned long machine)
3097 bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
3099 /* If this isn't the right architecture for this backend, and this
3100 isn't the generic backend, fail. */
3101 if (arch != bed->arch
3102 && arch != bfd_arch_unknown
3103 && bed->arch != bfd_arch_unknown)
3106 return bfd_default_set_arch_mach (abfd, arch, machine);
3110 bfd_mach_o_scan (bfd *abfd,
3111 bfd_mach_o_header *header,
3112 bfd_mach_o_data_struct *mdata)
3115 enum bfd_architecture cputype;
3116 unsigned long cpusubtype;
3117 unsigned int hdrsize;
3119 hdrsize = mach_o_wide_p (header) ?
3120 BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE;
3122 mdata->header = *header;
3124 abfd->flags = abfd->flags & BFD_IN_MEMORY;
3125 switch (header->filetype)
3127 case BFD_MACH_O_MH_OBJECT:
3128 abfd->flags |= HAS_RELOC;
3130 case BFD_MACH_O_MH_EXECUTE:
3131 abfd->flags |= EXEC_P;
3133 case BFD_MACH_O_MH_DYLIB:
3134 case BFD_MACH_O_MH_BUNDLE:
3135 abfd->flags |= DYNAMIC;
3139 abfd->tdata.mach_o_data = mdata;
3141 bfd_mach_o_convert_architecture (header->cputype, header->cpusubtype,
3142 &cputype, &cpusubtype);
3143 if (cputype == bfd_arch_unknown)
3145 (*_bfd_error_handler)
3146 (_("bfd_mach_o_scan: unknown architecture 0x%lx/0x%lx"),
3147 header->cputype, header->cpusubtype);
3151 bfd_set_arch_mach (abfd, cputype, cpusubtype);
3153 if (header->ncmds != 0)
3155 mdata->commands = bfd_alloc
3156 (abfd, header->ncmds * sizeof (bfd_mach_o_load_command));
3157 if (mdata->commands == NULL)
3160 for (i = 0; i < header->ncmds; i++)
3162 bfd_mach_o_load_command *cur = &mdata->commands[i];
3165 cur->offset = hdrsize;
3168 bfd_mach_o_load_command *prev = &mdata->commands[i - 1];
3169 cur->offset = prev->offset + prev->len;
3172 if (bfd_mach_o_read_command (abfd, cur) < 0)
3177 if (bfd_mach_o_scan_start_address (abfd) < 0)
3180 bfd_mach_o_flatten_sections (abfd);
3185 bfd_mach_o_mkobject_init (bfd *abfd)
3187 bfd_mach_o_data_struct *mdata = NULL;
3189 mdata = bfd_alloc (abfd, sizeof (bfd_mach_o_data_struct));
3192 abfd->tdata.mach_o_data = mdata;
3194 mdata->header.magic = 0;
3195 mdata->header.cputype = 0;
3196 mdata->header.cpusubtype = 0;
3197 mdata->header.filetype = 0;
3198 mdata->header.ncmds = 0;
3199 mdata->header.sizeofcmds = 0;
3200 mdata->header.flags = 0;
3201 mdata->header.byteorder = BFD_ENDIAN_UNKNOWN;
3202 mdata->commands = NULL;
3204 mdata->sections = NULL;
3210 bfd_mach_o_gen_mkobject (bfd *abfd)
3212 bfd_mach_o_data_struct *mdata;
3214 if (!bfd_mach_o_mkobject_init (abfd))
3217 mdata = bfd_mach_o_get_data (abfd);
3218 mdata->header.magic = BFD_MACH_O_MH_MAGIC;
3219 mdata->header.cputype = 0;
3220 mdata->header.cpusubtype = 0;
3221 mdata->header.byteorder = abfd->xvec->byteorder;
3222 mdata->header.version = 1;
3228 bfd_mach_o_header_p (bfd *abfd,
3229 bfd_mach_o_filetype filetype,
3230 bfd_mach_o_cpu_type cputype)
3232 struct bfd_preserve preserve;
3233 bfd_mach_o_header header;
3235 preserve.marker = NULL;
3236 if (!bfd_mach_o_read_header (abfd, &header))
3239 if (! (header.byteorder == BFD_ENDIAN_BIG
3240 || header.byteorder == BFD_ENDIAN_LITTLE))
3242 (*_bfd_error_handler) (_("unknown header byte-order value 0x%lx"),
3243 (unsigned long) header.byteorder);
3247 if (! ((header.byteorder == BFD_ENDIAN_BIG
3248 && abfd->xvec->byteorder == BFD_ENDIAN_BIG
3249 && abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
3250 || (header.byteorder == BFD_ENDIAN_LITTLE
3251 && abfd->xvec->byteorder == BFD_ENDIAN_LITTLE
3252 && abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)))
3255 /* Check cputype and filetype.
3256 In case of wildcard, do not accept magics that are handled by existing
3260 if (header.cputype != cputype)
3265 switch (header.cputype)
3267 case BFD_MACH_O_CPU_TYPE_I386:
3268 /* Handled by mach-o-i386 */
3276 if (header.filetype != filetype)
3281 switch (header.filetype)
3283 case BFD_MACH_O_MH_CORE:
3284 /* Handled by core_p */
3291 preserve.marker = bfd_zalloc (abfd, sizeof (bfd_mach_o_data_struct));
3292 if (preserve.marker == NULL
3293 || !bfd_preserve_save (abfd, &preserve))
3296 if (!bfd_mach_o_scan (abfd, &header,
3297 (bfd_mach_o_data_struct *) preserve.marker))
3300 bfd_preserve_finish (abfd, &preserve);
3304 bfd_set_error (bfd_error_wrong_format);
3307 if (preserve.marker != NULL)
3308 bfd_preserve_restore (abfd, &preserve);
3312 static const bfd_target *
3313 bfd_mach_o_gen_object_p (bfd *abfd)
3315 return bfd_mach_o_header_p (abfd, 0, 0);
3318 static const bfd_target *
3319 bfd_mach_o_gen_core_p (bfd *abfd)
3321 return bfd_mach_o_header_p (abfd, BFD_MACH_O_MH_CORE, 0);
3324 typedef struct mach_o_fat_archentry
3326 unsigned long cputype;
3327 unsigned long cpusubtype;
3328 unsigned long offset;
3330 unsigned long align;
3331 } mach_o_fat_archentry;
3333 typedef struct mach_o_fat_data_struct
3335 unsigned long magic;
3336 unsigned long nfat_arch;
3337 mach_o_fat_archentry *archentries;
3338 } mach_o_fat_data_struct;
3341 bfd_mach_o_archive_p (bfd *abfd)
3343 mach_o_fat_data_struct *adata = NULL;
3344 struct mach_o_fat_header_external hdr;
3347 if (bfd_seek (abfd, 0, SEEK_SET) != 0
3348 || bfd_bread (&hdr, sizeof (hdr), abfd) != sizeof (hdr))
3351 adata = bfd_alloc (abfd, sizeof (mach_o_fat_data_struct));
3355 adata->magic = bfd_getb32 (hdr.magic);
3356 adata->nfat_arch = bfd_getb32 (hdr.nfat_arch);
3357 if (adata->magic != 0xcafebabe)
3359 /* Avoid matching Java bytecode files, which have the same magic number.
3360 In the Java bytecode file format this field contains the JVM version,
3361 which starts at 43.0. */
3362 if (adata->nfat_arch > 30)
3365 adata->archentries =
3366 bfd_alloc (abfd, adata->nfat_arch * sizeof (mach_o_fat_archentry));
3367 if (adata->archentries == NULL)
3370 for (i = 0; i < adata->nfat_arch; i++)
3372 struct mach_o_fat_arch_external arch;
3373 if (bfd_bread (&arch, sizeof (arch), abfd) != sizeof (arch))
3375 adata->archentries[i].cputype = bfd_getb32 (arch.cputype);
3376 adata->archentries[i].cpusubtype = bfd_getb32 (arch.cpusubtype);
3377 adata->archentries[i].offset = bfd_getb32 (arch.offset);
3378 adata->archentries[i].size = bfd_getb32 (arch.size);
3379 adata->archentries[i].align = bfd_getb32 (arch.align);
3382 abfd->tdata.mach_o_fat_data = adata;
3387 bfd_release (abfd, adata);
3388 bfd_set_error (bfd_error_wrong_format);
3393 bfd_mach_o_openr_next_archived_file (bfd *archive, bfd *prev)
3395 mach_o_fat_data_struct *adata;
3396 mach_o_fat_archentry *entry = NULL;
3399 enum bfd_architecture arch_type;
3400 unsigned long arch_subtype;
3402 adata = (mach_o_fat_data_struct *) archive->tdata.mach_o_fat_data;
3403 BFD_ASSERT (adata != NULL);
3405 /* Find index of previous entry. */
3407 i = 0; /* Start at first one. */
3410 for (i = 0; i < adata->nfat_arch; i++)
3412 if (adata->archentries[i].offset == prev->origin)
3416 if (i == adata->nfat_arch)
3419 bfd_set_error (bfd_error_bad_value);
3422 i++; /* Get next entry. */
3425 if (i >= adata->nfat_arch)
3427 bfd_set_error (bfd_error_no_more_archived_files);
3431 entry = &adata->archentries[i];
3432 nbfd = _bfd_new_bfd_contained_in (archive);
3436 nbfd->origin = entry->offset;
3438 bfd_mach_o_convert_architecture (entry->cputype, entry->cpusubtype,
3439 &arch_type, &arch_subtype);
3441 /* Create the member filename. Use ARCH_NAME. */
3442 nbfd->filename = bfd_printable_arch_mach (arch_type, arch_subtype);
3443 nbfd->iostream = NULL;
3444 bfd_set_arch_mach (nbfd, arch_type, arch_subtype);
3449 /* If ABFD format is FORMAT and architecture is ARCH, return it.
3450 If ABFD is a fat image containing a member that corresponds to FORMAT
3451 and ARCH, returns it.
3452 In other case, returns NULL.
3453 This function allows transparent uses of fat images. */
3455 bfd_mach_o_fat_extract (bfd *abfd,
3457 const bfd_arch_info_type *arch)
3460 mach_o_fat_data_struct *adata;
3463 if (bfd_check_format (abfd, format))
3465 if (bfd_get_arch_info (abfd) == arch)
3469 if (!bfd_check_format (abfd, bfd_archive)
3470 || abfd->xvec != &mach_o_fat_vec)
3473 /* This is a Mach-O fat image. */
3474 adata = (mach_o_fat_data_struct *) abfd->tdata.mach_o_fat_data;
3475 BFD_ASSERT (adata != NULL);
3477 for (i = 0; i < adata->nfat_arch; i++)
3479 struct mach_o_fat_archentry *e = &adata->archentries[i];
3480 enum bfd_architecture cpu_type;
3481 unsigned long cpu_subtype;
3483 bfd_mach_o_convert_architecture (e->cputype, e->cpusubtype,
3484 &cpu_type, &cpu_subtype);
3485 if (cpu_type != arch->arch || cpu_subtype != arch->mach)
3488 /* The architecture is found. */
3489 res = _bfd_new_bfd_contained_in (abfd);
3493 res->origin = e->offset;
3495 res->filename = bfd_printable_arch_mach (cpu_type, cpu_subtype);
3496 res->iostream = NULL;
3498 if (bfd_check_format (res, format))
3500 BFD_ASSERT (bfd_get_arch_info (res) == arch);
3511 bfd_mach_o_lookup_command (bfd *abfd,
3512 bfd_mach_o_load_command_type type,
3513 bfd_mach_o_load_command **mcommand)
3515 struct mach_o_data_struct *md = bfd_mach_o_get_data (abfd);
3516 bfd_mach_o_load_command *ncmd = NULL;
3517 unsigned int i, num;
3519 BFD_ASSERT (md != NULL);
3520 BFD_ASSERT (mcommand != NULL);
3523 for (i = 0; i < md->header.ncmds; i++)
3525 struct bfd_mach_o_load_command *cmd = &md->commands[i];
3527 if (cmd->type != type)
3540 bfd_mach_o_stack_addr (enum bfd_mach_o_cpu_type type)
3544 case BFD_MACH_O_CPU_TYPE_MC680x0:
3546 case BFD_MACH_O_CPU_TYPE_MC88000:
3548 case BFD_MACH_O_CPU_TYPE_POWERPC:
3550 case BFD_MACH_O_CPU_TYPE_I386:
3552 case BFD_MACH_O_CPU_TYPE_SPARC:
3554 case BFD_MACH_O_CPU_TYPE_I860:
3556 case BFD_MACH_O_CPU_TYPE_HPPA:
3557 return 0xc0000000 - 0x04000000;
3563 const bfd_mach_o_xlat_name bfd_mach_o_section_type_name[] =
3565 { "regular", BFD_MACH_O_S_REGULAR},
3566 { "zerofill", BFD_MACH_O_S_ZEROFILL},
3567 { "cstring_literals", BFD_MACH_O_S_CSTRING_LITERALS},
3568 { "4byte_literals", BFD_MACH_O_S_4BYTE_LITERALS},
3569 { "8byte_literals", BFD_MACH_O_S_8BYTE_LITERALS},
3570 { "literal_pointers", BFD_MACH_O_S_LITERAL_POINTERS},
3571 { "non_lazy_symbol_pointers", BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS},
3572 { "lazy_symbol_pointers", BFD_MACH_O_S_LAZY_SYMBOL_POINTERS},
3573 { "symbol_stubs", BFD_MACH_O_S_SYMBOL_STUBS},
3574 { "mod_init_func_pointers", BFD_MACH_O_S_MOD_INIT_FUNC_POINTERS},
3575 { "mod_fini_func_pointers", BFD_MACH_O_S_MOD_FINI_FUNC_POINTERS},
3576 { "coalesced", BFD_MACH_O_S_COALESCED},
3577 { "gb_zerofill", BFD_MACH_O_S_GB_ZEROFILL},
3578 { "interposing", BFD_MACH_O_S_INTERPOSING},
3579 { "16byte_literals", BFD_MACH_O_S_16BYTE_LITERALS},
3580 { "dtrace_dof", BFD_MACH_O_S_DTRACE_DOF},
3581 { "symbol_stubs", BFD_MACH_O_S_SYMBOL_STUBS},
3582 { "lazy_dylib_symbol_pointers", BFD_MACH_O_S_LAZY_DYLIB_SYMBOL_POINTERS},
3586 const bfd_mach_o_xlat_name bfd_mach_o_section_attribute_name[] =
3588 { "loc_reloc", BFD_MACH_O_S_ATTR_LOC_RELOC },
3589 { "ext_reloc", BFD_MACH_O_S_ATTR_EXT_RELOC },
3590 { "some_instructions", BFD_MACH_O_S_ATTR_SOME_INSTRUCTIONS },
3591 { "debug", BFD_MACH_O_S_ATTR_DEBUG },
3592 { "modifying_code", BFD_MACH_O_S_SELF_MODIFYING_CODE },
3593 { "live_support", BFD_MACH_O_S_ATTR_LIVE_SUPPORT },
3594 { "no_dead_strip", BFD_MACH_O_S_ATTR_NO_DEAD_STRIP },
3595 { "strip_static_syms", BFD_MACH_O_S_ATTR_STRIP_STATIC_SYMS },
3596 { "no_toc", BFD_MACH_O_S_ATTR_NO_TOC },
3597 { "pure_instructions", BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS },
3598 { "self_modifying_code", BFD_MACH_O_S_SELF_MODIFYING_CODE },
3602 /* Get the section type from NAME. Return 256 if NAME is unknown. */
3605 bfd_mach_o_get_section_type_from_name (const char *name)
3607 const bfd_mach_o_xlat_name *x;
3609 for (x = bfd_mach_o_section_type_name; x->name; x++)
3610 if (strcmp (x->name, name) == 0)
3612 /* Maximum section ID = 0xff. */
3616 /* Get the section attribute from NAME. Return -1 if NAME is unknown. */
3619 bfd_mach_o_get_section_attribute_from_name (const char *name)
3621 const bfd_mach_o_xlat_name *x;
3623 for (x = bfd_mach_o_section_attribute_name; x->name; x++)
3624 if (strcmp (x->name, name) == 0)
3626 return (unsigned int)-1;
3630 bfd_mach_o_core_fetch_environment (bfd *abfd,
3631 unsigned char **rbuf,
3634 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
3635 unsigned long stackaddr = bfd_mach_o_stack_addr (mdata->header.cputype);
3638 for (i = 0; i < mdata->header.ncmds; i++)
3640 bfd_mach_o_load_command *cur = &mdata->commands[i];
3641 bfd_mach_o_segment_command *seg = NULL;
3643 if (cur->type != BFD_MACH_O_LC_SEGMENT)
3646 seg = &cur->command.segment;
3648 if ((seg->vmaddr + seg->vmsize) == stackaddr)
3650 unsigned long start = seg->fileoff;
3651 unsigned long end = seg->fileoff + seg->filesize;
3652 unsigned char *buf = bfd_malloc (1024);
3653 unsigned long size = 1024;
3657 bfd_size_type nread = 0;
3658 unsigned long offset;
3659 int found_nonnull = 0;
3661 if (size > (end - start))
3662 size = (end - start);
3664 buf = bfd_realloc_or_free (buf, size);
3668 if (bfd_seek (abfd, end - size, SEEK_SET) != 0)
3674 nread = bfd_bread (buf, size, abfd);
3682 for (offset = 4; offset <= size; offset += 4)
3686 val = *((unsigned long *) (buf + size - offset));
3687 if (! found_nonnull)
3692 else if (val == 0x0)
3694 unsigned long bottom;
3697 bottom = seg->fileoff + seg->filesize - offset;
3698 top = seg->fileoff + seg->filesize - 4;
3699 *rbuf = bfd_malloc (top - bottom);
3700 *rlen = top - bottom;
3702 memcpy (*rbuf, buf + size - *rlen, *rlen);
3708 if (size == (end - start))
3722 bfd_mach_o_core_file_failing_command (bfd *abfd)
3724 unsigned char *buf = NULL;
3725 unsigned int len = 0;
3728 ret = bfd_mach_o_core_fetch_environment (abfd, &buf, &len);
3732 return (char *) buf;
3736 bfd_mach_o_core_file_failing_signal (bfd *abfd ATTRIBUTE_UNUSED)
3742 bfd_mach_o_find_nearest_line (bfd *abfd,
3746 const char **filename_ptr,
3747 const char **functionname_ptr,
3748 unsigned int *line_ptr)
3750 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
3751 /* TODO: Handle executables and dylibs by using dSYMs. */
3752 if (mdata->header.filetype != BFD_MACH_O_MH_OBJECT)
3754 if (_bfd_dwarf2_find_nearest_line (abfd, dwarf_debug_sections,
3755 section, symbols, offset,
3756 filename_ptr, functionname_ptr,
3758 &mdata->dwarf2_find_line_info))
3764 bfd_mach_o_close_and_cleanup (bfd *abfd)
3766 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
3767 if (bfd_get_format (abfd) == bfd_object && mdata != NULL)
3768 _bfd_dwarf2_cleanup_debug_info (abfd, &mdata->dwarf2_find_line_info);
3770 bfd_mach_o_free_cached_info (abfd);
3772 return _bfd_generic_close_and_cleanup (abfd);
3775 bfd_boolean bfd_mach_o_free_cached_info (bfd *abfd)
3777 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
3779 free (mdata->dyn_reloc_cache);
3780 mdata->dyn_reloc_cache = NULL;
3781 for (asect = abfd->sections; asect != NULL; asect = asect->next)
3783 free (asect->relocation);
3784 asect->relocation = NULL;
3790 #define bfd_mach_o_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
3791 #define bfd_mach_o_bfd_reloc_name_lookup _bfd_norelocs_bfd_reloc_name_lookup
3793 #define bfd_mach_o_swap_reloc_in NULL
3794 #define bfd_mach_o_swap_reloc_out NULL
3795 #define bfd_mach_o_print_thread NULL
3796 #define bfd_mach_o_tgt_seg_table NULL
3798 #define TARGET_NAME mach_o_be_vec
3799 #define TARGET_STRING "mach-o-be"
3800 #define TARGET_ARCHITECTURE bfd_arch_unknown
3801 #define TARGET_BIG_ENDIAN 1
3802 #define TARGET_ARCHIVE 0
3803 #include "mach-o-target.c"
3806 #undef TARGET_STRING
3807 #undef TARGET_ARCHITECTURE
3808 #undef TARGET_BIG_ENDIAN
3809 #undef TARGET_ARCHIVE
3811 #define TARGET_NAME mach_o_le_vec
3812 #define TARGET_STRING "mach-o-le"
3813 #define TARGET_ARCHITECTURE bfd_arch_unknown
3814 #define TARGET_BIG_ENDIAN 0
3815 #define TARGET_ARCHIVE 0
3817 #include "mach-o-target.c"
3820 #undef TARGET_STRING
3821 #undef TARGET_ARCHITECTURE
3822 #undef TARGET_BIG_ENDIAN
3823 #undef TARGET_ARCHIVE
3825 /* Not yet handled: creating an archive. */
3826 #define bfd_mach_o_mkarchive _bfd_noarchive_mkarchive
3829 #define bfd_mach_o_read_ar_hdr _bfd_noarchive_read_ar_hdr
3830 #define bfd_mach_o_write_ar_hdr _bfd_noarchive_write_ar_hdr
3831 #define bfd_mach_o_slurp_armap _bfd_noarchive_slurp_armap
3832 #define bfd_mach_o_slurp_extended_name_table _bfd_noarchive_slurp_extended_name_table
3833 #define bfd_mach_o_construct_extended_name_table _bfd_noarchive_construct_extended_name_table
3834 #define bfd_mach_o_truncate_arname _bfd_noarchive_truncate_arname
3835 #define bfd_mach_o_write_armap _bfd_noarchive_write_armap
3836 #define bfd_mach_o_get_elt_at_index _bfd_noarchive_get_elt_at_index
3837 #define bfd_mach_o_generic_stat_arch_elt _bfd_noarchive_generic_stat_arch_elt
3838 #define bfd_mach_o_update_armap_timestamp _bfd_noarchive_update_armap_timestamp
3840 #define TARGET_NAME mach_o_fat_vec
3841 #define TARGET_STRING "mach-o-fat"
3842 #define TARGET_ARCHITECTURE bfd_arch_unknown
3843 #define TARGET_BIG_ENDIAN 1
3844 #define TARGET_ARCHIVE 1
3846 #include "mach-o-target.c"
3849 #undef TARGET_STRING
3850 #undef TARGET_ARCHITECTURE
3851 #undef TARGET_BIG_ENDIAN
3852 #undef TARGET_ARCHIVE