2011-12-16 Shinichiro Hamaji <shinichiro.hamaji@gmail.com>
[binutils.git] / bfd / mach-o.c
blobdca86013d6419977e9a4688f80183eb1ebb1f324
1 /* Mach-O support for BFD.
2 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
3 2009, 2010, 2011
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. */
23 #include "sysdep.h"
24 #include "mach-o.h"
25 #include "bfd.h"
26 #include "libbfd.h"
27 #include "libiberty.h"
28 #include "aout/stab_gnu.h"
29 #include "mach-o/reloc.h"
30 #include "mach-o/external.h"
31 #include <ctype.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)))
40 unsigned int
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;
51 bfd_boolean
52 bfd_mach_o_valid (bfd *abfd)
54 if (abfd == NULL || abfd->xvec == NULL)
55 return FALSE;
57 if (abfd->xvec->flavour != bfd_target_mach_o_flavour)
58 return FALSE;
60 if (bfd_mach_o_get_data (abfd) == NULL)
61 return FALSE;
62 return TRUE;
65 static INLINE bfd_boolean
66 mach_o_wide_p (bfd_mach_o_header *header)
68 switch (header->version)
70 case 1:
71 return FALSE;
72 case 2:
73 return TRUE;
74 default:
75 BFD_FAIL ();
76 return FALSE;
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
88 by gdb. */
90 /* __TEXT Segment. */
91 static const mach_o_section_name_xlat text_section_names_xlat[] =
93 { ".text", "__text",
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[] =
132 { ".data", "__data",
133 SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR,
134 BFD_MACH_O_S_ATTR_NONE, 0},
135 { ".bss", "__bss",
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},
150 { ".dyld", "__dyld",
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},
258 /* Objc V1 */
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 },
277 { NULL, NULL }
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)
304 return sec;
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)
312 return sec;
314 return NULL;
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);
328 *segname = NULL;
330 if (bfd_name[0] != '.')
331 return NULL;
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;
340 return sec;
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;
349 return sec;
352 return NULL;
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). */
366 void
367 bfd_mach_o_convert_section_name_to_bfd (bfd *abfd, const char *segname,
368 const char *secname, const char **name,
369 flagword *flags)
371 const mach_o_section_name_xlat *xlat;
372 char *res;
373 unsigned int len;
374 const char *pfx = "";
376 *name = NULL;
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);
382 if (xlat)
384 len = strlen (xlat->bfd_name);
385 res = bfd_alloc (abfd, len+1);
386 if (res == NULL)
387 return;
388 memcpy (res, xlat->bfd_name, len+1);
389 *name = res;
390 *flags = xlat->bfd_flags;
391 return;
394 /* ... else we make up a bfd name from the segment concatenated with the
395 section. */
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.";
405 pfx = seg_pfx;
406 len += sizeof (seg_pfx) - 1;
409 res = bfd_alloc (abfd, len);
410 if (res == NULL)
411 return;
412 snprintf (res, len, "%s%.16s.%.16s", pfx, segname, secname);
413 *name = res;
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,
427 asection *sect,
428 bfd_mach_o_section *section)
430 const mach_o_section_name_xlat *xlat;
431 const char *name = bfd_get_section_name (abfd, sect);
432 const char *segname;
433 const char *dot;
434 unsigned int len;
435 unsigned int seglen;
436 unsigned int seclen;
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);
443 if (xlat)
445 strcpy (section->segname, segname);
446 strcpy (section->sectname, xlat->mach_o_name);
447 return xlat;
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)
453 name += 11;
455 /* Find a dot. */
456 dot = strchr (name, '.');
457 len = strlen (name);
459 /* Try to split name into segment and section names. */
460 if (dot && dot != name)
462 seglen = 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;
471 return NULL;
475 /* The segment and section names are both missing - don't make them
476 into dots. */
477 if (dot && dot == name)
478 return NULL;
480 /* Just duplicate the name into both segment and section. */
481 if (len > 16)
482 len = 16;
483 memcpy (section->segname, name, len);
484 section->segname[len] = 0;
485 memcpy (section->sectname, name, len);
486 section->sectname[len] = 0;
487 return NULL;
490 /* Return the size of an entry for section SEC.
491 Must be called only for symbol pointer section and symbol stubs
492 sections. */
494 unsigned int
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;
504 default:
505 BFD_FAIL ();
506 return 0;
510 /* Return the number of indirect symbols for a section.
511 Must be called only for symbol pointer section and symbol stubs
512 sections. */
514 unsigned int
515 bfd_mach_o_section_get_nbr_indirect (bfd *abfd, bfd_mach_o_section *sec)
517 unsigned int elsz;
519 elsz = bfd_mach_o_section_get_entry_size (abfd, sec);
520 if (elsz == 0)
521 return 0;
522 else
523 return sec->size / elsz;
527 /* Copy any private info we understand from the input symbol
528 to the output symbol. */
530 bfd_boolean
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)
536 return TRUE;
539 /* Copy any private info we understand from the input section
540 to the output section. */
542 bfd_boolean
543 bfd_mach_o_bfd_copy_private_section_data (bfd *ibfd ATTRIBUTE_UNUSED,
544 asection *isection,
545 bfd *obfd ATTRIBUTE_UNUSED,
546 asection *osection)
548 if (osection->used_by_bfd == NULL)
549 osection->used_by_bfd = isection->used_by_bfd;
550 else
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;
558 return TRUE;
561 /* Copy any private info we understand from the input bfd
562 to the output bfd. */
564 bfd_boolean
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)
569 return TRUE;
571 BFD_ASSERT (bfd_mach_o_valid (ibfd));
572 BFD_ASSERT (bfd_mach_o_valid (obfd));
574 /* FIXME: copy commands. */
576 return TRUE;
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. */
583 bfd_boolean
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);
588 if (!mdata)
589 return FALSE;
591 mdata->header.flags = flags;
592 return TRUE;
595 /* Count the total number of symbols. */
597 static long
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)
603 return 0;
604 return mdata->symtab->nsyms;
607 long
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 *));
615 long
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;
621 unsigned long j;
623 if (nsyms < 0)
624 return nsyms;
626 if (nsyms == 0)
628 /* Do not try to read symbols if there are none. */
629 alocation[0] = NULL;
630 return 0;
633 if (!bfd_mach_o_read_symtab_symbols (abfd))
635 (*_bfd_error_handler)
636 (_("bfd_mach_o_canonicalize_symtab: unable to load symbols"));
637 return 0;
640 BFD_ASSERT (sym->symbols != NULL);
642 for (j = 0; j < sym->nsyms; j++)
643 alocation[j] = &sym->symbols[j].symbol;
645 alocation[j] = NULL;
647 return nsyms;
650 long
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,
656 asymbol **ret)
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;
661 asymbol *s;
662 unsigned long count, i, j, n;
663 size_t size;
664 char *names;
665 char *nul_name;
667 *ret = NULL;
669 if (dysymtab == NULL || symtab == NULL || symtab->symbols == NULL)
670 return 0;
672 if (dysymtab->nindirectsyms == 0)
673 return 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);
687 if (s == NULL)
688 return -1;
689 names = (char *) (s + count);
690 nul_name = names;
691 *names++ = 0;
693 n = 0;
694 for (i = 0; i < mdata->nsects; i++)
696 bfd_mach_o_section *sec = mdata->sections[i];
697 unsigned int first, last;
698 bfd_vma addr;
699 bfd_vma entry_size;
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);
708 addr = sec->addr;
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;
717 s->udata.p = NULL;
719 if (isym < symtab->nsyms
720 && symtab->symbols[isym].symbol.name)
722 const char *sym = symtab->symbols[isym].symbol.name;
723 size_t len;
725 s->name = names;
726 len = strlen (sym);
727 memcpy (names, sym, len);
728 names += len;
729 memcpy (names, "$stub", sizeof ("$stub"));
730 names += sizeof ("$stub");
732 else
733 s->name = nul_name;
735 addr += entry_size;
736 s++;
737 n++;
739 break;
740 default:
741 break;
745 return n;
748 void
749 bfd_mach_o_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
750 asymbol *symbol,
751 symbol_info *ret)
753 bfd_symbol_info (symbol, ret);
756 void
757 bfd_mach_o_print_symbol (bfd *abfd,
758 void * afile,
759 asymbol *symbol,
760 bfd_print_symbol_type how)
762 FILE *file = (FILE *) afile;
763 const char *name;
764 bfd_mach_o_asymbol *asym = (bfd_mach_o_asymbol *)symbol;
766 switch (how)
768 case bfd_print_symbol_name:
769 fprintf (file, "%s", symbol->name);
770 break;
771 default:
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);
775 else
776 switch (asym->n_type & BFD_MACH_O_N_TYPE)
778 case BFD_MACH_O_N_UNDF:
779 if (symbol->value == 0)
780 name = "UND";
781 else
782 name = "COM";
783 break;
784 case BFD_MACH_O_N_ABS:
785 name = "ABS";
786 break;
787 case BFD_MACH_O_N_INDR:
788 name = "INDR";
789 break;
790 case BFD_MACH_O_N_PBUD:
791 name = "PBUD";
792 break;
793 case BFD_MACH_O_N_SECT:
794 name = "SECT";
795 break;
796 default:
797 name = "???";
798 break;
800 if (name == NULL)
801 name = "";
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);
811 static void
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;
819 switch (mtype)
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;
826 break;
827 case BFD_MACH_O_CPU_TYPE_X86_64:
828 *type = bfd_arch_i386;
829 *subtype = bfd_mach_x86_64;
830 break;
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;
839 break;
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;
845 break;
846 case BFD_MACH_O_CPU_TYPE_POWERPC_64:
847 *type = bfd_arch_powerpc;
848 *subtype = bfd_mach_ppc64;
849 break;
850 default:
851 *type = bfd_arch_unknown;
852 break;
856 static bfd_boolean
857 bfd_mach_o_write_header (bfd *abfd, bfd_mach_o_header *header)
859 struct mach_o_header_external raw;
860 unsigned int size;
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)
878 return FALSE;
880 return TRUE;
883 static int
884 bfd_mach_o_write_thread (bfd *abfd, bfd_mach_o_load_command *command)
886 bfd_mach_o_thread_command *cmd = &command->command.thread;
887 unsigned int i;
888 struct mach_o_thread_command_external raw;
889 unsigned int offset;
891 BFD_ASSERT ((command->type == BFD_MACH_O_LC_THREAD)
892 || (command->type == BFD_MACH_O_LC_UNIXTHREAD));
894 offset = 8;
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))
906 return -1;
908 offset += cmd->flavours[i].size + sizeof (raw);
911 return 0;
914 long
915 bfd_mach_o_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
916 asection *asect)
918 return (asect->reloc_count + 1) * sizeof (arelent *);
921 static int
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;
929 bfd_vma addr;
930 bfd_vma symnum;
931 asymbol **sym;
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)
938 unsigned int j;
940 /* Scattered relocation.
941 Extract section and offset from r_value. */
942 res->sym_ptr_ptr = NULL;
943 res->addend = 0;
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;
951 break;
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;
960 else
962 unsigned int num = BFD_MACH_O_GET_R_SYMBOLNUM (symnum);
963 res->addend = 0;
964 res->address = addr;
965 if (symnum & BFD_MACH_O_R_EXTERN)
967 sym = syms + num;
968 reloc.r_extern = 1;
970 else
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;
981 reloc.r_extern = 0;
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))
991 return -1;
992 return 0;
995 static int
996 bfd_mach_o_canonicalize_relocs (bfd *abfd, unsigned long filepos,
997 unsigned long count,
998 arelent *res, asymbol **syms)
1000 unsigned long i;
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;
1006 native_relocs =
1007 (struct mach_o_reloc_info_external *) bfd_malloc (native_size);
1008 if (native_relocs == NULL)
1009 return -1;
1011 if (bfd_seek (abfd, filepos, SEEK_SET) != 0
1012 || bfd_bread (native_relocs, native_size, abfd) != native_size)
1013 goto err;
1015 for (i = 0; i < count; i++)
1017 if (bfd_mach_o_canonicalize_one_reloc (abfd, &native_relocs[i],
1018 &res[i], syms) < 0)
1019 goto err;
1021 free (native_relocs);
1022 return i;
1023 err:
1024 free (native_relocs);
1025 return -1;
1028 long
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);
1033 unsigned long i;
1034 arelent *res;
1036 if (asect->reloc_count == 0)
1037 return 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)
1041 return 0;
1043 if (asect->relocation == NULL)
1045 res = bfd_malloc (asect->reloc_count * sizeof (arelent));
1046 if (res == NULL)
1047 return -1;
1049 if (bfd_mach_o_canonicalize_relocs (abfd, asect->rel_filepos,
1050 asect->reloc_count, res, syms) < 0)
1052 free (res);
1053 return -1;
1055 asect->relocation = res;
1058 res = asect->relocation;
1059 for (i = 0; i < asect->reloc_count; i++)
1060 rels[i] = &res[i];
1061 rels[i] = NULL;
1063 return i;
1066 long
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)
1072 return 1;
1073 return (mdata->dysymtab->nextrel + mdata->dysymtab->nlocrel + 1)
1074 * sizeof (arelent *);
1077 long
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);
1084 unsigned long i;
1085 arelent *res;
1087 if (dysymtab == NULL)
1088 return 0;
1089 if (dysymtab->nextrel == 0 && dysymtab->nlocrel == 0)
1090 return 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)
1094 return 0;
1096 if (mdata->dyn_reloc_cache == NULL)
1098 res = bfd_malloc ((dysymtab->nextrel + dysymtab->nlocrel)
1099 * sizeof (arelent));
1100 if (res == NULL)
1101 return -1;
1103 if (bfd_mach_o_canonicalize_relocs (abfd, dysymtab->extreloff,
1104 dysymtab->nextrel, res, syms) < 0)
1106 free (res);
1107 return -1;
1110 if (bfd_mach_o_canonicalize_relocs (abfd, dysymtab->locreloff,
1111 dysymtab->nlocrel,
1112 res + dysymtab->nextrel, syms) < 0)
1114 free (res);
1115 return -1;
1118 mdata->dyn_reloc_cache = res;
1121 res = mdata->dyn_reloc_cache;
1122 for (i = 0; i < dysymtab->nextrel + dysymtab->nlocrel; i++)
1123 rels[i] = &res[i];
1124 rels[i] = NULL;
1125 return i;
1128 static bfd_boolean
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);
1132 unsigned int i;
1133 arelent **entries;
1134 asection *sec;
1135 bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
1137 sec = section->bfdsection;
1138 if (sec->reloc_count == 0)
1139 return TRUE;
1141 if (bed->_bfd_mach_o_swap_reloc_out == NULL)
1142 return TRUE;
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)
1152 return FALSE;
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))
1164 return FALSE;
1166 /* Lower the relocation info. */
1167 if (pinfo->r_scattered)
1169 unsigned long v;
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);
1180 else
1182 unsigned long v;
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)
1195 return FALSE;
1197 return TRUE;
1200 static int
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)
1219 return -1;
1221 return 0;
1224 static int
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)
1244 return -1;
1246 return 0;
1249 static int
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))
1260 return -1;
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))
1274 return -1;
1276 for (sec = seg->sect_head; sec != NULL; sec = sec->next)
1277 if (bfd_mach_o_write_section_32 (abfd, sec))
1278 return -1;
1280 return 0;
1283 static int
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))
1294 return -1;
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))
1308 return -1;
1310 for (sec = seg->sect_head; sec != NULL; sec = sec->next)
1311 if (bfd_mach_o_write_section_64 (abfd, sec))
1312 return -1;
1314 return 0;
1317 static bfd_boolean
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;
1322 unsigned long i;
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)
1334 return FALSE;
1336 sym->nsyms = bfd_get_symcount (abfd);
1337 mdata->filelen += sym->nsyms * symlen;
1339 strtab = _bfd_stringtab_init ();
1340 if (strtab == NULL)
1341 return FALSE;
1343 if (sym->nsyms > 0)
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
1346 0 entry. */
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')
1357 str_index = 0;
1358 else
1360 str_index = _bfd_stringtab_add (strtab, s->symbol.name, TRUE, FALSE);
1361 if (str_index == (bfd_size_type) -1)
1362 goto err;
1365 if (wide)
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,
1374 raw.n_value);
1376 if (bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
1377 goto err;
1379 else
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,
1388 raw.n_value);
1390 if (bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
1391 goto err;
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)
1399 goto err;
1400 _bfd_stringtab_free (strtab);
1402 /* The command. */
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))
1413 return FALSE;
1416 return TRUE;
1418 err:
1419 _bfd_stringtab_free (strtab);
1420 return FALSE;
1423 /* Process the symbols and generate Mach-O specific fields.
1424 Number them. */
1426 static bfd_boolean
1427 bfd_mach_o_mangle_symbols (bfd *abfd)
1429 unsigned long i;
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;
1451 else
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;
1467 return TRUE;
1470 bfd_boolean
1471 bfd_mach_o_write_contents (bfd *abfd)
1473 unsigned int i;
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))
1478 return FALSE;
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;
1487 else
1488 mdata->header.filetype = BFD_MACH_O_MH_OBJECT;
1490 if (!bfd_mach_o_write_header (abfd, &mdata->header))
1491 return FALSE;
1493 /* Assign a number to each symbols. */
1494 if (!bfd_mach_o_mangle_symbols (abfd))
1495 return FALSE;
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)
1510 return FALSE;
1512 switch (cur->type)
1514 case BFD_MACH_O_LC_SEGMENT:
1515 if (bfd_mach_o_write_segment_32 (abfd, cur) != 0)
1516 return FALSE;
1517 break;
1518 case BFD_MACH_O_LC_SEGMENT_64:
1519 if (bfd_mach_o_write_segment_64 (abfd, cur) != 0)
1520 return FALSE;
1521 break;
1522 case BFD_MACH_O_LC_SYMTAB:
1523 if (!bfd_mach_o_write_symtab (abfd, cur))
1524 return FALSE;
1525 break;
1526 case BFD_MACH_O_LC_SYMSEG:
1527 break;
1528 case BFD_MACH_O_LC_THREAD:
1529 case BFD_MACH_O_LC_UNIXTHREAD:
1530 if (bfd_mach_o_write_thread (abfd, cur) != 0)
1531 return FALSE;
1532 break;
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:
1549 break;
1550 default:
1551 (*_bfd_error_handler) (_("unable to write unknown load command 0x%lx"),
1552 (unsigned long) cur->type);
1553 return FALSE;
1557 return TRUE;
1560 static void
1561 bfd_mach_o_append_section_to_segment (bfd_mach_o_segment_command *seg,
1562 asection *sec)
1564 bfd_mach_o_section *s = (bfd_mach_o_section *)sec->used_by_bfd;
1565 if (seg->sect_head == NULL)
1566 seg->sect_head = s;
1567 else
1568 seg->sect_tail->next = s;
1569 seg->sect_tail = s;
1572 /* Create section Mach-O flags from BFD flags. */
1574 static void
1575 bfd_mach_o_set_section_flags_from_bfd (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
1577 flagword bfd_flags;
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;
1590 else
1591 s->flags = BFD_MACH_O_S_REGULAR;
1594 /* Build Mach-O load commands from the sections. */
1596 bfd_boolean
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;
1602 asection *sec;
1603 bfd_mach_o_load_command *cmd;
1604 bfd_mach_o_load_command *symtab_cmd;
1605 int target_index;
1607 /* Return now if commands are already built. */
1608 if (mdata->header.ncmds)
1609 return FALSE;
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)
1617 return FALSE;
1618 cmd = &mdata->commands[0];
1619 seg = &cmd->command.segment;
1621 seg->nsects = bfd_count_sections (abfd);
1623 /* Set segment command. */
1624 if (wide)
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;
1631 else
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));
1655 seg->vmaddr = 0;
1656 seg->fileoff = mdata->filelen;
1657 seg->filesize = 0;
1658 seg->maxprot = BFD_MACH_O_PROT_READ | BFD_MACH_O_PROT_WRITE
1659 | BFD_MACH_O_PROT_EXECUTE;
1660 seg->initprot = seg->maxprot;
1661 seg->flags = 0;
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. */
1668 target_index = 0;
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;
1687 else
1688 msect->offset = 0;
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;
1698 return TRUE;
1701 /* Set the contents of a section. */
1703 bfd_boolean
1704 bfd_mach_o_set_section_contents (bfd *abfd,
1705 asection *section,
1706 const void * location,
1707 file_ptr offset,
1708 bfd_size_type count)
1710 file_ptr pos;
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))
1715 return FALSE;
1717 if (count == 0)
1718 return TRUE;
1720 pos = section->filepos + offset;
1721 if (bfd_seek (abfd, pos, SEEK_SET) != 0
1722 || bfd_bwrite (location, count, abfd) != count)
1723 return FALSE;
1725 return TRUE;
1729 bfd_mach_o_sizeof_headers (bfd *a ATTRIBUTE_UNUSED,
1730 struct bfd_link_info *info ATTRIBUTE_UNUSED)
1732 return 0;
1735 /* Make an empty symbol. This is required only because
1736 bfd_make_section_anyway wants to create a symbol for the section. */
1738 asymbol *
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)
1745 return new_symbol;
1746 new_symbol->the_bfd = abfd;
1747 new_symbol->udata.i = 0;
1748 return new_symbol;
1751 static bfd_boolean
1752 bfd_mach_o_read_header (bfd *abfd, bfd_mach_o_header *header)
1754 struct mach_o_header_external raw;
1755 unsigned int size;
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)
1761 return FALSE;
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;
1768 get32 = bfd_getb32;
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;
1775 get32 = bfd_getl32;
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;
1782 get32 = bfd_getb32;
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;
1789 get32 = bfd_getl32;
1791 else
1793 header->byteorder = BFD_ENDIAN_UNKNOWN;
1794 return FALSE;
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)
1803 return FALSE;
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);
1815 return TRUE;
1818 bfd_boolean
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);
1825 if (s == NULL)
1827 flagword bfd_flags;
1828 static const mach_o_section_name_xlat * xlat;
1830 s = (bfd_mach_o_section *) bfd_zalloc (abfd, sizeof (*s));
1831 if (s == NULL)
1832 return FALSE;
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
1839 values. */
1840 xlat = bfd_mach_o_convert_section_name_to_mach_o (abfd, sec, s);
1841 if (xlat != NULL)
1843 s->flags = xlat->macho_sectype | xlat->macho_secattr;
1844 s->align = xlat->sectalign > bfdalign ? xlat->sectalign
1845 : bfdalign;
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);
1851 else
1852 /* Create default flags. */
1853 bfd_mach_o_set_section_flags_from_bfd (abfd, sec);
1856 return _bfd_generic_new_section_hook (abfd, sec);
1859 static void
1860 bfd_mach_o_init_section_from_mach_o (bfd *abfd, asection *sec,
1861 unsigned long prot)
1863 flagword flags;
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;
1877 else
1879 flags = SEC_ALLOC;
1880 if ((section->flags & BFD_MACH_O_SECTION_TYPE_MASK)
1881 != BFD_MACH_O_S_ZEROFILL)
1883 flags |= SEC_LOAD;
1884 if (prot & BFD_MACH_O_PROT_EXECUTE)
1885 flags |= SEC_CODE;
1886 if (prot & BFD_MACH_O_PROT_WRITE)
1887 flags |= SEC_DATA;
1888 else if (prot & BFD_MACH_O_PROT_READ)
1889 flags |= SEC_READONLY;
1893 else
1895 if ((flags & SEC_DEBUGGING) == 0)
1896 flags |= SEC_ALLOC;
1899 if (section->offset != 0)
1900 flags |= SEC_HAS_CONTENTS;
1901 if (section->nreloc != 0)
1902 flags |= SEC_RELOC;
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;
1916 static asection *
1917 bfd_mach_o_make_bfd_section (bfd *abfd,
1918 const unsigned char *segname,
1919 const unsigned char *sectname)
1921 const char *sname;
1922 flagword flags;
1924 bfd_mach_o_convert_section_name_to_bfd
1925 (abfd, (const char *)segname, (const char *)sectname, &sname, &flags);
1926 if (sname == NULL)
1927 return NULL;
1929 return bfd_make_section_anyway_with_flags (abfd, sname, flags);
1932 static asection *
1933 bfd_mach_o_read_section_32 (bfd *abfd,
1934 unsigned int offset,
1935 unsigned long prot)
1937 struct mach_o_section_32_external raw;
1938 asection *sec;
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))
1944 return NULL;
1946 sec = bfd_mach_o_make_bfd_section (abfd, raw.segname, raw.sectname);
1947 if (sec == NULL)
1948 return NULL;
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);
1968 return sec;
1971 static asection *
1972 bfd_mach_o_read_section_64 (bfd *abfd,
1973 unsigned int offset,
1974 unsigned long prot)
1976 struct mach_o_section_64_external raw;
1977 asection *sec;
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))
1983 return NULL;
1985 sec = bfd_mach_o_make_bfd_section (abfd, raw.segname, raw.sectname);
1986 if (sec == NULL)
1987 return NULL;
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);
2007 return sec;
2010 static asection *
2011 bfd_mach_o_read_section (bfd *abfd,
2012 unsigned int offset,
2013 unsigned long prot,
2014 unsigned int wide)
2016 if (wide)
2017 return bfd_mach_o_read_section_64 (abfd, offset, prot);
2018 else
2019 return bfd_mach_o_read_section_32 (abfd, offset, prot);
2022 static bfd_boolean
2023 bfd_mach_o_read_symtab_symbol (bfd *abfd,
2024 bfd_mach_o_symtab_command *sym,
2025 bfd_mach_o_asymbol *s,
2026 unsigned long i)
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;
2036 short desc = -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);
2049 return FALSE;
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);
2057 if (wide)
2058 value = bfd_h_get_64 (abfd, raw.n_value);
2059 else
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);
2068 return FALSE;
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;
2076 s->n_type = type;
2077 s->n_sect = section;
2078 s->n_desc = desc;
2080 if (type & BFD_MACH_O_N_STAB)
2082 s->symbol.flags |= BSF_DEBUGGING;
2083 s->symbol.section = bfd_und_section_ptr;
2084 switch (type)
2086 case N_FUN:
2087 case N_STSYM:
2088 case N_LCSYM:
2089 case N_BNSYM:
2090 case N_SLINE:
2091 case N_ENSYM:
2092 case N_ECOMM:
2093 case N_ECOML:
2094 case N_GSYM:
2095 if ((section > 0) && (section <= mdata->nsects))
2097 s->symbol.section = mdata->sections[section - 1]->bfdsection;
2098 s->symbol.value =
2099 s->symbol.value - mdata->sections[section - 1]->addr;
2101 break;
2104 else
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;
2115 switch (symtype)
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;
2125 else
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;
2131 break;
2132 case BFD_MACH_O_N_PBUD:
2133 s->symbol.section = bfd_und_section_ptr;
2134 break;
2135 case BFD_MACH_O_N_ABS:
2136 s->symbol.section = bfd_abs_section_ptr;
2137 break;
2138 case BFD_MACH_O_N_SECT:
2139 if ((section > 0) && (section <= mdata->nsects))
2141 s->symbol.section = mdata->sections[section - 1]->bfdsection;
2142 s->symbol.value =
2143 s->symbol.value - mdata->sections[section - 1]->addr;
2145 else
2147 /* Mach-O uses 0 to mean "no section"; not an error. */
2148 if (section != 0)
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;
2156 break;
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;
2164 break;
2165 default:
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;
2170 break;
2174 return TRUE;
2177 bfd_boolean
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. */
2184 if (sym == NULL)
2185 return FALSE;
2187 /* Success if already loaded. */
2188 if (sym->strtab)
2189 return TRUE;
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);
2200 return FALSE;
2202 sym->strtab = (char *) b->buffer + sym->stroff;
2204 else
2206 sym->strtab = bfd_alloc (abfd, sym->strsize);
2207 if (sym->strtab == NULL)
2208 return FALSE;
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);
2214 return FALSE;
2218 return TRUE;
2221 bfd_boolean
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;
2226 unsigned long i;
2228 if (sym == NULL || sym->symbols)
2230 /* Return now if there are no symbols or if already loaded. */
2231 return TRUE;
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"));
2239 return FALSE;
2242 if (!bfd_mach_o_read_symtab_strtab (abfd))
2243 return FALSE;
2245 for (i = 0; i < sym->nsyms; i++)
2247 if (!bfd_mach_o_read_symtab_symbol (abfd, sym, &sym->symbols[i], i))
2248 return FALSE;
2251 return TRUE;
2254 static const char *
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";
2276 static const char *
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";
2291 static int
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))
2303 return -1;
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)
2311 return -1;
2312 if (bfd_seek (abfd, cmd->name_offset, SEEK_SET) != 0
2313 || bfd_bread (cmd->name_str, cmd->name_len, abfd) != cmd->name_len)
2314 return -1;
2315 return 0;
2318 static int
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:
2332 break;
2333 default:
2334 BFD_FAIL ();
2335 return -1;
2338 if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
2339 || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
2340 return -1;
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)
2351 return -1;
2352 if (bfd_seek (abfd, cmd->name_offset, SEEK_SET) != 0
2353 || bfd_bread (cmd->name_str, cmd->name_len, abfd) != cmd->name_len)
2354 return -1;
2355 return 0;
2358 static int
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);
2365 return 0;
2368 static int
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;
2375 unsigned int i;
2377 BFD_ASSERT ((command->type == BFD_MACH_O_LC_THREAD)
2378 || (command->type == BFD_MACH_O_LC_UNIXTHREAD));
2380 /* Count the number of threads. */
2381 offset = 8;
2382 nflavours = 0;
2383 while (offset != command->len)
2385 struct mach_o_thread_command_external raw;
2387 if (offset >= command->len)
2388 return -1;
2390 if (bfd_seek (abfd, command->offset + offset, SEEK_SET) != 0
2391 || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
2392 return -1;
2394 offset += sizeof (raw) + bfd_h_get_32 (abfd, raw.count) * 4;
2395 nflavours++;
2398 /* Allocate threads. */
2399 cmd->flavours = bfd_alloc
2400 (abfd, nflavours * sizeof (bfd_mach_o_thread_flavour));
2401 if (cmd->flavours == NULL)
2402 return -1;
2403 cmd->nflavours = nflavours;
2405 offset = 8;
2406 nflavours = 0;
2407 while (offset != command->len)
2409 struct mach_o_thread_command_external raw;
2411 if (offset >= command->len)
2412 return -1;
2414 if (nflavours >= cmd->nflavours)
2415 return -1;
2417 if (bfd_seek (abfd, command->offset + offset, SEEK_SET) != 0
2418 || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
2419 return -1;
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);
2425 nflavours++;
2428 for (i = 0; i < nflavours; i++)
2430 asection *bfdsec;
2431 unsigned int snamelen;
2432 char *sname;
2433 const char *flavourstr;
2434 const char *prefix = "LC_THREAD";
2435 unsigned int j = 0;
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);
2442 break;
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);
2446 break;
2447 default:
2448 flavourstr = "UNKNOWN_ARCHITECTURE";
2449 break;
2452 snamelen = strlen (prefix) + 1 + 20 + 1 + strlen (flavourstr) + 1;
2453 sname = bfd_alloc (abfd, snamelen);
2454 if (sname == NULL)
2455 return -1;
2457 for (;;)
2459 sprintf (sname, "%s.%s.%u", prefix, flavourstr, j);
2460 if (bfd_get_section_by_name (abfd, sname) == NULL)
2461 break;
2462 j++;
2465 bfdsec = bfd_make_section_with_flags (abfd, sname, SEC_HAS_CONTENTS);
2467 bfdsec->vma = 0;
2468 bfdsec->lma = 0;
2469 bfdsec->size = cmd->flavours[i].size;
2470 bfdsec->filepos = cmd->flavours[i].offset;
2471 bfdsec->alignment_power = 0x0;
2473 cmd->section = bfdsec;
2476 return 0;
2479 static int
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))
2492 return -1;
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)
2516 unsigned int i;
2517 int wide = bfd_mach_o_wide_p (abfd);
2518 unsigned int module_len = wide ? 56 : 52;
2520 cmd->dylib_module =
2521 bfd_alloc (abfd, cmd->nmodtab * sizeof (bfd_mach_o_dylib_module));
2522 if (cmd->dylib_module == NULL)
2523 return -1;
2525 if (bfd_seek (abfd, cmd->modtaboff, SEEK_SET) != 0)
2526 return -1;
2528 for (i = 0; i < cmd->nmodtab; i++)
2530 bfd_mach_o_dylib_module *module = &cmd->dylib_module[i];
2531 unsigned long v;
2532 unsigned char buf[56];
2534 if (bfd_bread ((void *) buf, module_len, abfd) != module_len)
2535 return -1;
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;
2552 if (wide)
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);
2557 else
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);
2565 if (cmd->ntoc != 0)
2567 unsigned int i;
2569 cmd->dylib_toc = bfd_alloc
2570 (abfd, cmd->ntoc * sizeof (bfd_mach_o_dylib_table_of_content));
2571 if (cmd->dylib_toc == NULL)
2572 return -1;
2574 if (bfd_seek (abfd, cmd->tocoff, SEEK_SET) != 0)
2575 return -1;
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))
2583 return -1;
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)
2592 unsigned int i;
2594 cmd->indirect_syms = bfd_alloc
2595 (abfd, cmd->nindirectsyms * sizeof (unsigned int));
2596 if (cmd->indirect_syms == NULL)
2597 return -1;
2599 if (bfd_seek (abfd, cmd->indirectsymoff, SEEK_SET) != 0)
2600 return -1;
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))
2608 return -1;
2610 *is = bfd_h_get_32 (abfd, raw);
2614 if (cmd->nextrefsyms != 0)
2616 unsigned long v;
2617 unsigned int i;
2619 cmd->ext_refs = bfd_alloc
2620 (abfd, cmd->nextrefsyms * sizeof (bfd_mach_o_dylib_reference));
2621 if (cmd->ext_refs == NULL)
2622 return -1;
2624 if (bfd_seek (abfd, cmd->extrefsymoff, SEEK_SET) != 0)
2625 return -1;
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))
2633 return -1;
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;
2643 else
2645 ref->isym = v & 0xffffff;
2646 ref->flags = (v >> 24) & 0xff;
2651 if (mdata->dysymtab)
2652 return -1;
2653 mdata->dysymtab = cmd;
2655 return 0;
2658 static int
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))
2669 return -1;
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;
2681 if (mdata->symtab)
2682 return -1;
2683 mdata->symtab = symtab;
2684 return 0;
2687 static int
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)
2696 return -1;
2698 return 0;
2701 static int
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))
2709 return -1;
2711 cmd->dataoff = bfd_get_32 (abfd, raw.dataoff);
2712 cmd->datasize = bfd_get_32 (abfd, raw.datasize);
2713 return 0;
2716 static int
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;
2721 unsigned long off;
2723 if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
2724 || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
2725 return -1;
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)
2732 return -1;
2733 if (bfd_seek (abfd, cmd->stroff, SEEK_SET) != 0
2734 || bfd_bread ((void *) cmd->str, cmd->str_len, abfd) != cmd->str_len)
2735 return -1;
2736 return 0;
2739 static int
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))
2747 return -1;
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);
2759 return 0;
2762 static bfd_boolean
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;
2767 unsigned int ver;
2769 if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
2770 || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
2771 return FALSE;
2773 ver = bfd_get_32 (abfd, raw.version);
2774 cmd->rel = ver >> 16;
2775 cmd->maj = ver >> 8;
2776 cmd->min = ver;
2777 cmd->reserved = bfd_get_32 (abfd, raw.reserved);
2778 return TRUE;
2781 static int
2782 bfd_mach_o_read_segment (bfd *abfd,
2783 bfd_mach_o_load_command *command,
2784 unsigned int wide)
2786 bfd_mach_o_segment_command *seg = &command->command.segment;
2787 unsigned long i;
2789 if (wide)
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))
2797 return -1;
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);
2811 else
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))
2819 return -1;
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++)
2838 bfd_vma segoff;
2839 asection *sec;
2841 if (wide)
2842 segoff = command->offset + BFD_MACH_O_LC_SEGMENT_64_SIZE
2843 + (i * BFD_MACH_O_SECTION_64_SIZE);
2844 else
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);
2849 if (sec == NULL)
2850 return -1;
2852 bfd_mach_o_append_section_to_segment (seg, sec);
2855 return 0;
2858 static int
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);
2864 static int
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);
2870 static int
2871 bfd_mach_o_read_command (bfd *abfd, bfd_mach_o_load_command *command)
2873 struct mach_o_load_command_external raw;
2874 unsigned int cmd;
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)
2879 return -1;
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)
2890 return -1;
2891 break;
2892 case BFD_MACH_O_LC_SEGMENT_64:
2893 if (bfd_mach_o_read_segment_64 (abfd, command) != 0)
2894 return -1;
2895 break;
2896 case BFD_MACH_O_LC_SYMTAB:
2897 if (bfd_mach_o_read_symtab (abfd, command) != 0)
2898 return -1;
2899 break;
2900 case BFD_MACH_O_LC_SYMSEG:
2901 break;
2902 case BFD_MACH_O_LC_THREAD:
2903 case BFD_MACH_O_LC_UNIXTHREAD:
2904 if (bfd_mach_o_read_thread (abfd, command) != 0)
2905 return -1;
2906 break;
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)
2910 return -1;
2911 break;
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)
2918 return -1;
2919 break;
2920 case BFD_MACH_O_LC_PREBOUND_DYLIB:
2921 if (bfd_mach_o_read_prebound_dylib (abfd, command) != 0)
2922 return -1;
2923 break;
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:
2931 break;
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)
2938 return -1;
2939 break;
2940 case BFD_MACH_O_LC_DYSYMTAB:
2941 if (bfd_mach_o_read_dysymtab (abfd, command) != 0)
2942 return -1;
2943 break;
2944 case BFD_MACH_O_LC_TWOLEVEL_HINTS:
2945 case BFD_MACH_O_LC_PREBIND_CKSUM:
2946 break;
2947 case BFD_MACH_O_LC_UUID:
2948 if (bfd_mach_o_read_uuid (abfd, command) != 0)
2949 return -1;
2950 break;
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)
2955 return -1;
2956 break;
2957 case BFD_MACH_O_LC_DYLD_INFO:
2958 if (bfd_mach_o_read_dyld_info (abfd, command) != 0)
2959 return -1;
2960 break;
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))
2964 return -1;
2965 break;
2966 default:
2967 (*_bfd_error_handler)(_("%B: unable to read unknown load command 0x%lx"),
2968 abfd, (unsigned long) command->type);
2969 break;
2972 return 0;
2975 static void
2976 bfd_mach_o_flatten_sections (bfd *abfd)
2978 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
2979 long csect = 0;
2980 unsigned long i;
2982 /* Count total number of sections. */
2983 mdata->nsects = 0;
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. */
3002 csect = 0;
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;
3021 static bfd_boolean
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;
3026 unsigned long i;
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;
3033 break;
3036 if (cmd == NULL)
3037 return FALSE;
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)
3050 return FALSE;
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)
3061 return FALSE;
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)
3072 return FALSE;
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)
3083 return FALSE;
3085 abfd->start_address = bfd_h_get_64 (abfd, buf);
3089 return TRUE;
3092 bfd_boolean
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)
3104 return FALSE;
3106 return bfd_default_set_arch_mach (abfd, arch, machine);
3109 static bfd_boolean
3110 bfd_mach_o_scan (bfd *abfd,
3111 bfd_mach_o_header *header,
3112 bfd_mach_o_data_struct *mdata)
3114 unsigned int i;
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;
3129 break;
3130 case BFD_MACH_O_MH_EXECUTE:
3131 abfd->flags |= EXEC_P;
3132 break;
3133 case BFD_MACH_O_MH_DYLIB:
3134 case BFD_MACH_O_MH_BUNDLE:
3135 abfd->flags |= DYNAMIC;
3136 break;
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);
3148 return FALSE;
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)
3158 return FALSE;
3160 for (i = 0; i < header->ncmds; i++)
3162 bfd_mach_o_load_command *cur = &mdata->commands[i];
3164 if (i == 0)
3165 cur->offset = hdrsize;
3166 else
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)
3173 return FALSE;
3177 if (bfd_mach_o_scan_start_address (abfd) < 0)
3178 return FALSE;
3180 bfd_mach_o_flatten_sections (abfd);
3181 return TRUE;
3184 bfd_boolean
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));
3190 if (mdata == NULL)
3191 return FALSE;
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;
3203 mdata->nsects = 0;
3204 mdata->sections = NULL;
3205 mdata->dyn_reloc_cache = NULL;
3207 return TRUE;
3210 static bfd_boolean
3211 bfd_mach_o_gen_mkobject (bfd *abfd)
3213 bfd_mach_o_data_struct *mdata;
3215 if (!bfd_mach_o_mkobject_init (abfd))
3216 return FALSE;
3218 mdata = bfd_mach_o_get_data (abfd);
3219 mdata->header.magic = BFD_MACH_O_MH_MAGIC;
3220 mdata->header.cputype = 0;
3221 mdata->header.cpusubtype = 0;
3222 mdata->header.byteorder = abfd->xvec->byteorder;
3223 mdata->header.version = 1;
3225 return TRUE;
3228 const bfd_target *
3229 bfd_mach_o_header_p (bfd *abfd,
3230 bfd_mach_o_filetype filetype,
3231 bfd_mach_o_cpu_type cputype)
3233 struct bfd_preserve preserve;
3234 bfd_mach_o_header header;
3236 preserve.marker = NULL;
3237 if (!bfd_mach_o_read_header (abfd, &header))
3238 goto wrong;
3240 if (! (header.byteorder == BFD_ENDIAN_BIG
3241 || header.byteorder == BFD_ENDIAN_LITTLE))
3243 (*_bfd_error_handler) (_("unknown header byte-order value 0x%lx"),
3244 (unsigned long) header.byteorder);
3245 goto wrong;
3248 if (! ((header.byteorder == BFD_ENDIAN_BIG
3249 && abfd->xvec->byteorder == BFD_ENDIAN_BIG
3250 && abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
3251 || (header.byteorder == BFD_ENDIAN_LITTLE
3252 && abfd->xvec->byteorder == BFD_ENDIAN_LITTLE
3253 && abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)))
3254 goto wrong;
3256 /* Check cputype and filetype.
3257 In case of wildcard, do not accept magics that are handled by existing
3258 targets. */
3259 if (cputype)
3261 if (header.cputype != cputype)
3262 goto wrong;
3264 if (filetype)
3266 if (header.filetype != filetype)
3267 goto wrong;
3269 else
3271 switch (header.filetype)
3273 case BFD_MACH_O_MH_CORE:
3274 /* Handled by core_p */
3275 goto wrong;
3276 default:
3277 break;
3281 preserve.marker = bfd_zalloc (abfd, sizeof (bfd_mach_o_data_struct));
3282 if (preserve.marker == NULL
3283 || !bfd_preserve_save (abfd, &preserve))
3284 goto fail;
3286 if (!bfd_mach_o_scan (abfd, &header,
3287 (bfd_mach_o_data_struct *) preserve.marker))
3288 goto wrong;
3290 bfd_preserve_finish (abfd, &preserve);
3291 return abfd->xvec;
3293 wrong:
3294 bfd_set_error (bfd_error_wrong_format);
3296 fail:
3297 if (preserve.marker != NULL)
3298 bfd_preserve_restore (abfd, &preserve);
3299 return NULL;
3302 static const bfd_target *
3303 bfd_mach_o_gen_object_p (bfd *abfd)
3305 return bfd_mach_o_header_p (abfd, 0, 0);
3308 static const bfd_target *
3309 bfd_mach_o_gen_core_p (bfd *abfd)
3311 return bfd_mach_o_header_p (abfd, BFD_MACH_O_MH_CORE, 0);
3314 typedef struct mach_o_fat_archentry
3316 unsigned long cputype;
3317 unsigned long cpusubtype;
3318 unsigned long offset;
3319 unsigned long size;
3320 unsigned long align;
3321 } mach_o_fat_archentry;
3323 typedef struct mach_o_fat_data_struct
3325 unsigned long magic;
3326 unsigned long nfat_arch;
3327 mach_o_fat_archentry *archentries;
3328 } mach_o_fat_data_struct;
3330 const bfd_target *
3331 bfd_mach_o_archive_p (bfd *abfd)
3333 mach_o_fat_data_struct *adata = NULL;
3334 struct mach_o_fat_header_external hdr;
3335 unsigned long i;
3337 if (bfd_seek (abfd, 0, SEEK_SET) != 0
3338 || bfd_bread (&hdr, sizeof (hdr), abfd) != sizeof (hdr))
3339 goto error;
3341 adata = bfd_alloc (abfd, sizeof (mach_o_fat_data_struct));
3342 if (adata == NULL)
3343 goto error;
3345 adata->magic = bfd_getb32 (hdr.magic);
3346 adata->nfat_arch = bfd_getb32 (hdr.nfat_arch);
3347 if (adata->magic != 0xcafebabe)
3348 goto error;
3349 /* Avoid matching Java bytecode files, which have the same magic number.
3350 In the Java bytecode file format this field contains the JVM version,
3351 which starts at 43.0. */
3352 if (adata->nfat_arch > 30)
3353 goto error;
3355 adata->archentries =
3356 bfd_alloc (abfd, adata->nfat_arch * sizeof (mach_o_fat_archentry));
3357 if (adata->archentries == NULL)
3358 goto error;
3360 for (i = 0; i < adata->nfat_arch; i++)
3362 struct mach_o_fat_arch_external arch;
3363 if (bfd_bread (&arch, sizeof (arch), abfd) != sizeof (arch))
3364 goto error;
3365 adata->archentries[i].cputype = bfd_getb32 (arch.cputype);
3366 adata->archentries[i].cpusubtype = bfd_getb32 (arch.cpusubtype);
3367 adata->archentries[i].offset = bfd_getb32 (arch.offset);
3368 adata->archentries[i].size = bfd_getb32 (arch.size);
3369 adata->archentries[i].align = bfd_getb32 (arch.align);
3372 abfd->tdata.mach_o_fat_data = adata;
3373 return abfd->xvec;
3375 error:
3376 if (adata != NULL)
3377 bfd_release (abfd, adata);
3378 bfd_set_error (bfd_error_wrong_format);
3379 return NULL;
3382 bfd *
3383 bfd_mach_o_openr_next_archived_file (bfd *archive, bfd *prev)
3385 mach_o_fat_data_struct *adata;
3386 mach_o_fat_archentry *entry = NULL;
3387 unsigned long i;
3388 bfd *nbfd;
3389 enum bfd_architecture arch_type;
3390 unsigned long arch_subtype;
3392 adata = (mach_o_fat_data_struct *) archive->tdata.mach_o_fat_data;
3393 BFD_ASSERT (adata != NULL);
3395 /* Find index of previous entry. */
3396 if (prev == NULL)
3397 i = 0; /* Start at first one. */
3398 else
3400 for (i = 0; i < adata->nfat_arch; i++)
3402 if (adata->archentries[i].offset == prev->origin)
3403 break;
3406 if (i == adata->nfat_arch)
3408 /* Not found. */
3409 bfd_set_error (bfd_error_bad_value);
3410 return NULL;
3412 i++; /* Get next entry. */
3415 if (i >= adata->nfat_arch)
3417 bfd_set_error (bfd_error_no_more_archived_files);
3418 return NULL;
3421 entry = &adata->archentries[i];
3422 nbfd = _bfd_new_bfd_contained_in (archive);
3423 if (nbfd == NULL)
3424 return NULL;
3426 nbfd->origin = entry->offset;
3428 bfd_mach_o_convert_architecture (entry->cputype, entry->cpusubtype,
3429 &arch_type, &arch_subtype);
3431 /* Create the member filename. Use ARCH_NAME. */
3432 nbfd->filename = bfd_printable_arch_mach (arch_type, arch_subtype);
3433 nbfd->iostream = NULL;
3434 bfd_set_arch_mach (nbfd, arch_type, arch_subtype);
3436 return nbfd;
3439 /* If ABFD format is FORMAT and architecture is ARCH, return it.
3440 If ABFD is a fat image containing a member that corresponds to FORMAT
3441 and ARCH, returns it.
3442 In other case, returns NULL.
3443 This function allows transparent uses of fat images. */
3444 bfd *
3445 bfd_mach_o_fat_extract (bfd *abfd,
3446 bfd_format format,
3447 const bfd_arch_info_type *arch)
3449 bfd *res;
3450 mach_o_fat_data_struct *adata;
3451 unsigned int i;
3453 if (bfd_check_format (abfd, format))
3455 if (bfd_get_arch_info (abfd) == arch)
3456 return abfd;
3457 return NULL;
3459 if (!bfd_check_format (abfd, bfd_archive)
3460 || abfd->xvec != &mach_o_fat_vec)
3461 return NULL;
3463 /* This is a Mach-O fat image. */
3464 adata = (mach_o_fat_data_struct *) abfd->tdata.mach_o_fat_data;
3465 BFD_ASSERT (adata != NULL);
3467 for (i = 0; i < adata->nfat_arch; i++)
3469 struct mach_o_fat_archentry *e = &adata->archentries[i];
3470 enum bfd_architecture cpu_type;
3471 unsigned long cpu_subtype;
3473 bfd_mach_o_convert_architecture (e->cputype, e->cpusubtype,
3474 &cpu_type, &cpu_subtype);
3475 if (cpu_type != arch->arch || cpu_subtype != arch->mach)
3476 continue;
3478 /* The architecture is found. */
3479 res = _bfd_new_bfd_contained_in (abfd);
3480 if (res == NULL)
3481 return NULL;
3483 res->origin = e->offset;
3485 res->filename = bfd_printable_arch_mach (cpu_type, cpu_subtype);
3486 res->iostream = NULL;
3488 if (bfd_check_format (res, format))
3490 BFD_ASSERT (bfd_get_arch_info (res) == arch);
3491 return res;
3493 bfd_close (res);
3494 return NULL;
3497 return NULL;
3501 bfd_mach_o_lookup_command (bfd *abfd,
3502 bfd_mach_o_load_command_type type,
3503 bfd_mach_o_load_command **mcommand)
3505 struct mach_o_data_struct *md = bfd_mach_o_get_data (abfd);
3506 bfd_mach_o_load_command *ncmd = NULL;
3507 unsigned int i, num;
3509 BFD_ASSERT (md != NULL);
3510 BFD_ASSERT (mcommand != NULL);
3512 num = 0;
3513 for (i = 0; i < md->header.ncmds; i++)
3515 struct bfd_mach_o_load_command *cmd = &md->commands[i];
3517 if (cmd->type != type)
3518 continue;
3520 if (num == 0)
3521 ncmd = cmd;
3522 num++;
3525 *mcommand = ncmd;
3526 return num;
3529 unsigned long
3530 bfd_mach_o_stack_addr (enum bfd_mach_o_cpu_type type)
3532 switch (type)
3534 case BFD_MACH_O_CPU_TYPE_MC680x0:
3535 return 0x04000000;
3536 case BFD_MACH_O_CPU_TYPE_MC88000:
3537 return 0xffffe000;
3538 case BFD_MACH_O_CPU_TYPE_POWERPC:
3539 return 0xc0000000;
3540 case BFD_MACH_O_CPU_TYPE_I386:
3541 return 0xc0000000;
3542 case BFD_MACH_O_CPU_TYPE_SPARC:
3543 return 0xf0000000;
3544 case BFD_MACH_O_CPU_TYPE_I860:
3545 return 0;
3546 case BFD_MACH_O_CPU_TYPE_HPPA:
3547 return 0xc0000000 - 0x04000000;
3548 default:
3549 return 0;
3553 const bfd_mach_o_xlat_name bfd_mach_o_section_type_name[] =
3555 { "regular", BFD_MACH_O_S_REGULAR},
3556 { "zerofill", BFD_MACH_O_S_ZEROFILL},
3557 { "cstring_literals", BFD_MACH_O_S_CSTRING_LITERALS},
3558 { "4byte_literals", BFD_MACH_O_S_4BYTE_LITERALS},
3559 { "8byte_literals", BFD_MACH_O_S_8BYTE_LITERALS},
3560 { "literal_pointers", BFD_MACH_O_S_LITERAL_POINTERS},
3561 { "non_lazy_symbol_pointers", BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS},
3562 { "lazy_symbol_pointers", BFD_MACH_O_S_LAZY_SYMBOL_POINTERS},
3563 { "symbol_stubs", BFD_MACH_O_S_SYMBOL_STUBS},
3564 { "mod_init_func_pointers", BFD_MACH_O_S_MOD_INIT_FUNC_POINTERS},
3565 { "mod_fini_func_pointers", BFD_MACH_O_S_MOD_FINI_FUNC_POINTERS},
3566 { "coalesced", BFD_MACH_O_S_COALESCED},
3567 { "gb_zerofill", BFD_MACH_O_S_GB_ZEROFILL},
3568 { "interposing", BFD_MACH_O_S_INTERPOSING},
3569 { "16byte_literals", BFD_MACH_O_S_16BYTE_LITERALS},
3570 { "dtrace_dof", BFD_MACH_O_S_DTRACE_DOF},
3571 { "symbol_stubs", BFD_MACH_O_S_SYMBOL_STUBS},
3572 { "lazy_dylib_symbol_pointers", BFD_MACH_O_S_LAZY_DYLIB_SYMBOL_POINTERS},
3573 { NULL, 0}
3576 const bfd_mach_o_xlat_name bfd_mach_o_section_attribute_name[] =
3578 { "loc_reloc", BFD_MACH_O_S_ATTR_LOC_RELOC },
3579 { "ext_reloc", BFD_MACH_O_S_ATTR_EXT_RELOC },
3580 { "some_instructions", BFD_MACH_O_S_ATTR_SOME_INSTRUCTIONS },
3581 { "debug", BFD_MACH_O_S_ATTR_DEBUG },
3582 { "modifying_code", BFD_MACH_O_S_SELF_MODIFYING_CODE },
3583 { "live_support", BFD_MACH_O_S_ATTR_LIVE_SUPPORT },
3584 { "no_dead_strip", BFD_MACH_O_S_ATTR_NO_DEAD_STRIP },
3585 { "strip_static_syms", BFD_MACH_O_S_ATTR_STRIP_STATIC_SYMS },
3586 { "no_toc", BFD_MACH_O_S_ATTR_NO_TOC },
3587 { "pure_instructions", BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS },
3588 { "self_modifying_code", BFD_MACH_O_S_SELF_MODIFYING_CODE },
3589 { NULL, 0}
3592 /* Get the section type from NAME. Return 256 if NAME is unknown. */
3594 unsigned int
3595 bfd_mach_o_get_section_type_from_name (const char *name)
3597 const bfd_mach_o_xlat_name *x;
3599 for (x = bfd_mach_o_section_type_name; x->name; x++)
3600 if (strcmp (x->name, name) == 0)
3601 return x->val;
3602 /* Maximum section ID = 0xff. */
3603 return 256;
3606 /* Get the section attribute from NAME. Return -1 if NAME is unknown. */
3608 unsigned int
3609 bfd_mach_o_get_section_attribute_from_name (const char *name)
3611 const bfd_mach_o_xlat_name *x;
3613 for (x = bfd_mach_o_section_attribute_name; x->name; x++)
3614 if (strcmp (x->name, name) == 0)
3615 return x->val;
3616 return (unsigned int)-1;
3620 bfd_mach_o_core_fetch_environment (bfd *abfd,
3621 unsigned char **rbuf,
3622 unsigned int *rlen)
3624 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
3625 unsigned long stackaddr = bfd_mach_o_stack_addr (mdata->header.cputype);
3626 unsigned int i = 0;
3628 for (i = 0; i < mdata->header.ncmds; i++)
3630 bfd_mach_o_load_command *cur = &mdata->commands[i];
3631 bfd_mach_o_segment_command *seg = NULL;
3633 if (cur->type != BFD_MACH_O_LC_SEGMENT)
3634 continue;
3636 seg = &cur->command.segment;
3638 if ((seg->vmaddr + seg->vmsize) == stackaddr)
3640 unsigned long start = seg->fileoff;
3641 unsigned long end = seg->fileoff + seg->filesize;
3642 unsigned char *buf = bfd_malloc (1024);
3643 unsigned long size = 1024;
3645 for (;;)
3647 bfd_size_type nread = 0;
3648 unsigned long offset;
3649 int found_nonnull = 0;
3651 if (size > (end - start))
3652 size = (end - start);
3654 buf = bfd_realloc_or_free (buf, size);
3655 if (buf == NULL)
3656 return -1;
3658 if (bfd_seek (abfd, end - size, SEEK_SET) != 0)
3660 free (buf);
3661 return -1;
3664 nread = bfd_bread (buf, size, abfd);
3666 if (nread != size)
3668 free (buf);
3669 return -1;
3672 for (offset = 4; offset <= size; offset += 4)
3674 unsigned long val;
3676 val = *((unsigned long *) (buf + size - offset));
3677 if (! found_nonnull)
3679 if (val != 0)
3680 found_nonnull = 1;
3682 else if (val == 0x0)
3684 unsigned long bottom;
3685 unsigned long top;
3687 bottom = seg->fileoff + seg->filesize - offset;
3688 top = seg->fileoff + seg->filesize - 4;
3689 *rbuf = bfd_malloc (top - bottom);
3690 *rlen = top - bottom;
3692 memcpy (*rbuf, buf + size - *rlen, *rlen);
3693 free (buf);
3694 return 0;
3698 if (size == (end - start))
3699 break;
3701 size *= 2;
3704 free (buf);
3708 return -1;
3711 char *
3712 bfd_mach_o_core_file_failing_command (bfd *abfd)
3714 unsigned char *buf = NULL;
3715 unsigned int len = 0;
3716 int ret = -1;
3718 ret = bfd_mach_o_core_fetch_environment (abfd, &buf, &len);
3719 if (ret < 0)
3720 return NULL;
3722 return (char *) buf;
3726 bfd_mach_o_core_file_failing_signal (bfd *abfd ATTRIBUTE_UNUSED)
3728 return 0;
3731 bfd_boolean
3732 bfd_mach_o_find_nearest_line (bfd *abfd,
3733 asection *section,
3734 asymbol **symbols,
3735 bfd_vma offset,
3736 const char **filename_ptr,
3737 const char **functionname_ptr,
3738 unsigned int *line_ptr)
3740 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
3741 /* TODO: Handle executables and dylibs by using dSYMs. */
3742 if (mdata->header.filetype != BFD_MACH_O_MH_OBJECT)
3743 return FALSE;
3744 if (_bfd_dwarf2_find_nearest_line (abfd, dwarf_debug_sections,
3745 section, symbols, offset,
3746 filename_ptr, functionname_ptr,
3747 line_ptr, 0,
3748 &mdata->dwarf2_find_line_info))
3749 return TRUE;
3750 return FALSE;
3753 bfd_boolean
3754 bfd_mach_o_close_and_cleanup (bfd *abfd)
3756 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
3757 if (bfd_get_format (abfd) == bfd_object && mdata != NULL)
3759 _bfd_dwarf2_cleanup_debug_info (abfd, &mdata->dwarf2_find_line_info);
3760 bfd_mach_o_free_cached_info (abfd);
3763 return _bfd_generic_close_and_cleanup (abfd);
3766 bfd_boolean bfd_mach_o_free_cached_info (bfd *abfd)
3768 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
3769 asection *asect;
3770 free (mdata->dyn_reloc_cache);
3771 mdata->dyn_reloc_cache = NULL;
3772 for (asect = abfd->sections; asect != NULL; asect = asect->next)
3774 free (asect->relocation);
3775 asect->relocation = NULL;
3778 return TRUE;
3781 #define bfd_mach_o_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
3782 #define bfd_mach_o_bfd_reloc_name_lookup _bfd_norelocs_bfd_reloc_name_lookup
3784 #define bfd_mach_o_swap_reloc_in NULL
3785 #define bfd_mach_o_swap_reloc_out NULL
3786 #define bfd_mach_o_print_thread NULL
3787 #define bfd_mach_o_tgt_seg_table NULL
3789 #define TARGET_NAME mach_o_be_vec
3790 #define TARGET_STRING "mach-o-be"
3791 #define TARGET_ARCHITECTURE bfd_arch_unknown
3792 #define TARGET_BIG_ENDIAN 1
3793 #define TARGET_ARCHIVE 0
3794 #define TARGET_PRIORITY 1
3795 #include "mach-o-target.c"
3797 #undef TARGET_NAME
3798 #undef TARGET_STRING
3799 #undef TARGET_ARCHITECTURE
3800 #undef TARGET_BIG_ENDIAN
3801 #undef TARGET_ARCHIVE
3802 #undef TARGET_PRIORITY
3804 #define TARGET_NAME mach_o_le_vec
3805 #define TARGET_STRING "mach-o-le"
3806 #define TARGET_ARCHITECTURE bfd_arch_unknown
3807 #define TARGET_BIG_ENDIAN 0
3808 #define TARGET_ARCHIVE 0
3809 #define TARGET_PRIORITY 1
3811 #include "mach-o-target.c"
3813 #undef TARGET_NAME
3814 #undef TARGET_STRING
3815 #undef TARGET_ARCHITECTURE
3816 #undef TARGET_BIG_ENDIAN
3817 #undef TARGET_ARCHIVE
3818 #undef TARGET_PRIORITY
3820 /* Not yet handled: creating an archive. */
3821 #define bfd_mach_o_mkarchive _bfd_noarchive_mkarchive
3823 /* Not used. */
3824 #define bfd_mach_o_read_ar_hdr _bfd_noarchive_read_ar_hdr
3825 #define bfd_mach_o_write_ar_hdr _bfd_noarchive_write_ar_hdr
3826 #define bfd_mach_o_slurp_armap _bfd_noarchive_slurp_armap
3827 #define bfd_mach_o_slurp_extended_name_table _bfd_noarchive_slurp_extended_name_table
3828 #define bfd_mach_o_construct_extended_name_table _bfd_noarchive_construct_extended_name_table
3829 #define bfd_mach_o_truncate_arname _bfd_noarchive_truncate_arname
3830 #define bfd_mach_o_write_armap _bfd_noarchive_write_armap
3831 #define bfd_mach_o_get_elt_at_index _bfd_noarchive_get_elt_at_index
3832 #define bfd_mach_o_generic_stat_arch_elt _bfd_noarchive_generic_stat_arch_elt
3833 #define bfd_mach_o_update_armap_timestamp _bfd_noarchive_update_armap_timestamp
3835 #define TARGET_NAME mach_o_fat_vec
3836 #define TARGET_STRING "mach-o-fat"
3837 #define TARGET_ARCHITECTURE bfd_arch_unknown
3838 #define TARGET_BIG_ENDIAN 1
3839 #define TARGET_ARCHIVE 1
3840 #define TARGET_PRIORITY 0
3842 #include "mach-o-target.c"
3844 #undef TARGET_NAME
3845 #undef TARGET_STRING
3846 #undef TARGET_ARCHITECTURE
3847 #undef TARGET_BIG_ENDIAN
3848 #undef TARGET_ARCHIVE
3849 #undef TARGET_PRIORITY