1 /* Mach-O support for BFD.
2 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4 Free Software Foundation, Inc.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
27 #include "libiberty.h"
28 #include "aout/stab_gnu.h"
29 #include "mach-o/reloc.h"
30 #include "mach-o/external.h"
33 #define bfd_mach_o_object_p bfd_mach_o_gen_object_p
34 #define bfd_mach_o_core_p bfd_mach_o_gen_core_p
35 #define bfd_mach_o_mkobject bfd_mach_o_gen_mkobject
37 #define FILE_ALIGN(off, algn) \
38 (((off) + ((file_ptr) 1 << (algn)) - 1) & ((file_ptr) -1 << (algn)))
41 bfd_mach_o_version (bfd
*abfd
)
43 bfd_mach_o_data_struct
*mdata
= NULL
;
45 BFD_ASSERT (bfd_mach_o_valid (abfd
));
46 mdata
= bfd_mach_o_get_data (abfd
);
48 return mdata
->header
.version
;
52 bfd_mach_o_valid (bfd
*abfd
)
54 if (abfd
== NULL
|| abfd
->xvec
== NULL
)
57 if (abfd
->xvec
->flavour
!= bfd_target_mach_o_flavour
)
60 if (bfd_mach_o_get_data (abfd
) == NULL
)
65 static INLINE bfd_boolean
66 mach_o_wide_p (bfd_mach_o_header
*header
)
68 switch (header
->version
)
80 static INLINE bfd_boolean
81 bfd_mach_o_wide_p (bfd
*abfd
)
83 return mach_o_wide_p (&bfd_mach_o_get_data (abfd
)->header
);
86 /* Tables to translate well known Mach-O segment/section names to bfd
87 names. Use of canonical names (such as .text or .debug_frame) is required
91 static const mach_o_section_name_xlat text_section_names_xlat
[] =
94 SEC_CODE
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
95 BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS
, 0},
96 { ".const", "__const",
97 SEC_READONLY
| SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
98 BFD_MACH_O_S_ATTR_NONE
, 0},
99 { ".static_const", "__static_const",
100 SEC_READONLY
| SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
101 BFD_MACH_O_S_ATTR_NONE
, 0},
102 { ".cstring", "__cstring",
103 SEC_READONLY
| SEC_DATA
| SEC_LOAD
| SEC_MERGE
| SEC_STRINGS
,
104 BFD_MACH_O_S_CSTRING_LITERALS
,
105 BFD_MACH_O_S_ATTR_NONE
, 0},
106 { ".literal4", "__literal4",
107 SEC_READONLY
| SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_4BYTE_LITERALS
,
108 BFD_MACH_O_S_ATTR_NONE
, 2},
109 { ".literal8", "__literal8",
110 SEC_READONLY
| SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_8BYTE_LITERALS
,
111 BFD_MACH_O_S_ATTR_NONE
, 3},
112 { ".literal16", "__literal16",
113 SEC_READONLY
| SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_16BYTE_LITERALS
,
114 BFD_MACH_O_S_ATTR_NONE
, 4},
115 { ".constructor", "__constructor",
116 SEC_CODE
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
117 BFD_MACH_O_S_ATTR_NONE
, 0},
118 { ".destructor", "__destructor",
119 SEC_CODE
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
120 BFD_MACH_O_S_ATTR_NONE
, 0},
121 { ".eh_frame", "__eh_frame",
122 SEC_READONLY
| SEC_LOAD
, BFD_MACH_O_S_COALESCED
,
123 BFD_MACH_O_S_ATTR_LIVE_SUPPORT
124 | BFD_MACH_O_S_ATTR_STRIP_STATIC_SYMS
125 | BFD_MACH_O_S_ATTR_NO_TOC
, 3},
126 { NULL
, NULL
, 0, 0, 0, 0}
129 /* __DATA Segment. */
130 static const mach_o_section_name_xlat data_section_names_xlat
[] =
133 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
134 BFD_MACH_O_S_ATTR_NONE
, 0},
136 SEC_NO_FLAGS
, BFD_MACH_O_S_ZEROFILL
,
137 BFD_MACH_O_S_ATTR_NONE
, 0},
138 { ".const_data", "__const",
139 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
140 BFD_MACH_O_S_ATTR_NONE
, 0},
141 { ".static_data", "__static_data",
142 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
143 BFD_MACH_O_S_ATTR_NONE
, 0},
144 { ".mod_init_func", "__mod_init_func",
145 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_MOD_INIT_FUNC_POINTERS
,
146 BFD_MACH_O_S_ATTR_NONE
, 2},
147 { ".mod_term_func", "__mod_term_func",
148 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_MOD_FINI_FUNC_POINTERS
,
149 BFD_MACH_O_S_ATTR_NONE
, 2},
151 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
152 BFD_MACH_O_S_ATTR_NONE
, 0},
153 { ".cfstring", "__cfstring",
154 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
155 BFD_MACH_O_S_ATTR_NONE
, 2},
156 { NULL
, NULL
, 0, 0, 0, 0}
159 /* __DWARF Segment. */
160 static const mach_o_section_name_xlat dwarf_section_names_xlat
[] =
162 { ".debug_frame", "__debug_frame",
163 SEC_DEBUGGING
, BFD_MACH_O_S_REGULAR
,
164 BFD_MACH_O_S_ATTR_DEBUG
, 0},
165 { ".debug_info", "__debug_info",
166 SEC_DEBUGGING
, BFD_MACH_O_S_REGULAR
,
167 BFD_MACH_O_S_ATTR_DEBUG
, 0},
168 { ".debug_abbrev", "__debug_abbrev",
169 SEC_DEBUGGING
, BFD_MACH_O_S_REGULAR
,
170 BFD_MACH_O_S_ATTR_DEBUG
, 0},
171 { ".debug_aranges", "__debug_aranges",
172 SEC_DEBUGGING
, BFD_MACH_O_S_REGULAR
,
173 BFD_MACH_O_S_ATTR_DEBUG
, 0},
174 { ".debug_macinfo", "__debug_macinfo",
175 SEC_DEBUGGING
, BFD_MACH_O_S_REGULAR
,
176 BFD_MACH_O_S_ATTR_DEBUG
, 0},
177 { ".debug_line", "__debug_line",
178 SEC_DEBUGGING
, BFD_MACH_O_S_REGULAR
,
179 BFD_MACH_O_S_ATTR_DEBUG
, 0},
180 { ".debug_loc", "__debug_loc",
181 SEC_DEBUGGING
, BFD_MACH_O_S_REGULAR
,
182 BFD_MACH_O_S_ATTR_DEBUG
, 0},
183 { ".debug_pubnames", "__debug_pubnames",
184 SEC_DEBUGGING
, BFD_MACH_O_S_REGULAR
,
185 BFD_MACH_O_S_ATTR_DEBUG
, 0},
186 { ".debug_pubtypes", "__debug_pubtypes",
187 SEC_DEBUGGING
, BFD_MACH_O_S_REGULAR
,
188 BFD_MACH_O_S_ATTR_DEBUG
, 0},
189 { ".debug_str", "__debug_str",
190 SEC_DEBUGGING
, BFD_MACH_O_S_REGULAR
,
191 BFD_MACH_O_S_ATTR_DEBUG
, 0},
192 { ".debug_ranges", "__debug_ranges",
193 SEC_DEBUGGING
, BFD_MACH_O_S_REGULAR
,
194 BFD_MACH_O_S_ATTR_DEBUG
, 0},
195 { ".debug_macro", "__debug_macro",
196 SEC_DEBUGGING
, BFD_MACH_O_S_REGULAR
,
197 BFD_MACH_O_S_ATTR_DEBUG
, 0},
198 { NULL
, NULL
, 0, 0, 0, 0}
201 /* __OBJC Segment. */
202 static const mach_o_section_name_xlat objc_section_names_xlat
[] =
204 { ".objc_class", "__class",
205 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
206 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP
, 0},
207 { ".objc_meta_class", "__meta_class",
208 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
209 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP
, 0},
210 { ".objc_cat_cls_meth", "__cat_cls_meth",
211 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
212 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP
, 0},
213 { ".objc_cat_inst_meth", "__cat_inst_meth",
214 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
215 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP
, 0},
216 { ".objc_protocol", "__protocol",
217 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
218 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP
, 0},
219 { ".objc_string_object", "__string_object",
220 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
221 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP
, 0},
222 { ".objc_cls_meth", "__cls_meth",
223 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
224 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP
, 0},
225 { ".objc_inst_meth", "__inst_meth",
226 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
227 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP
, 0},
228 { ".objc_cls_refs", "__cls_refs",
229 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_LITERAL_POINTERS
,
230 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP
, 0},
231 { ".objc_message_refs", "__message_refs",
232 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_LITERAL_POINTERS
,
233 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP
, 0},
234 { ".objc_symbols", "__symbols",
235 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
236 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP
, 0},
237 { ".objc_category", "__category",
238 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
239 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP
, 0},
240 { ".objc_class_vars", "__class_vars",
241 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
242 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP
, 0},
243 { ".objc_instance_vars", "__instance_vars",
244 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
245 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP
, 0},
246 { ".objc_module_info", "__module_info",
247 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
248 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP
, 0},
249 { ".objc_selector_strs", "__selector_strs",
250 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_CSTRING_LITERALS
,
251 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP
, 0},
252 { ".objc_image_info", "__image_info",
253 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
254 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP
, 0},
255 { ".objc_selector_fixup", "__sel_fixup",
256 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
257 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP
, 0},
259 { ".objc1_class_ext", "__class_ext",
260 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
261 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP
, 0},
262 { ".objc1_property_list", "__property",
263 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
264 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP
, 0},
265 { ".objc1_protocol_ext", "__protocol_ext",
266 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
267 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP
, 0},
268 { NULL
, NULL
, 0, 0, 0, 0}
271 static const mach_o_segment_name_xlat segsec_names_xlat
[] =
273 { "__TEXT", text_section_names_xlat
},
274 { "__DATA", data_section_names_xlat
},
275 { "__DWARF", dwarf_section_names_xlat
},
276 { "__OBJC", objc_section_names_xlat
},
280 /* For both cases bfd-name => mach-o name and vice versa, the specific target
281 is checked before the generic. This allows a target (e.g. ppc for cstring)
282 to override the generic definition with a more specific one. */
284 /* Fetch the translation from a Mach-O section designation (segment, section)
285 as a bfd short name, if one exists. Otherwise return NULL.
287 Allow the segment and section names to be unterminated 16 byte arrays. */
289 const mach_o_section_name_xlat
*
290 bfd_mach_o_section_data_for_mach_sect (bfd
*abfd
, const char *segname
,
291 const char *sectname
)
293 const struct mach_o_segment_name_xlat
*seg
;
294 const mach_o_section_name_xlat
*sec
;
295 bfd_mach_o_backend_data
*bed
= bfd_mach_o_get_backend_data (abfd
);
297 /* First try any target-specific translations defined... */
298 if (bed
->segsec_names_xlat
)
299 for (seg
= bed
->segsec_names_xlat
; seg
->segname
; seg
++)
300 if (strncmp (seg
->segname
, segname
, BFD_MACH_O_SEGNAME_SIZE
) == 0)
301 for (sec
= seg
->sections
; sec
->mach_o_name
; sec
++)
302 if (strncmp (sec
->mach_o_name
, sectname
,
303 BFD_MACH_O_SECTNAME_SIZE
) == 0)
306 /* ... and then the Mach-O generic ones. */
307 for (seg
= segsec_names_xlat
; seg
->segname
; seg
++)
308 if (strncmp (seg
->segname
, segname
, BFD_MACH_O_SEGNAME_SIZE
) == 0)
309 for (sec
= seg
->sections
; sec
->mach_o_name
; sec
++)
310 if (strncmp (sec
->mach_o_name
, sectname
,
311 BFD_MACH_O_SECTNAME_SIZE
) == 0)
317 /* If the bfd_name for this section is a 'canonical' form for which we
318 know the Mach-O data, return the segment name and the data for the
319 Mach-O equivalent. Otherwise return NULL. */
321 const mach_o_section_name_xlat
*
322 bfd_mach_o_section_data_for_bfd_name (bfd
*abfd
, const char *bfd_name
,
323 const char **segname
)
325 const struct mach_o_segment_name_xlat
*seg
;
326 const mach_o_section_name_xlat
*sec
;
327 bfd_mach_o_backend_data
*bed
= bfd_mach_o_get_backend_data (abfd
);
330 if (bfd_name
[0] != '.')
333 /* First try any target-specific translations defined... */
334 if (bed
->segsec_names_xlat
)
335 for (seg
= bed
->segsec_names_xlat
; seg
->segname
; seg
++)
336 for (sec
= seg
->sections
; sec
->bfd_name
; sec
++)
337 if (strcmp (bfd_name
, sec
->bfd_name
) == 0)
339 *segname
= seg
->segname
;
343 /* ... and then the Mach-O generic ones. */
344 for (seg
= segsec_names_xlat
; seg
->segname
; seg
++)
345 for (sec
= seg
->sections
; sec
->bfd_name
; sec
++)
346 if (strcmp (bfd_name
, sec
->bfd_name
) == 0)
348 *segname
= seg
->segname
;
355 /* Convert Mach-O section name to BFD.
357 Try to use standard/canonical names, for which we have tables including
358 default flag settings - which are returned. Otherwise forge a new name
359 in the form "<segmentname>.<sectionname>" this will be prefixed with
360 LC_SEGMENT. if the segment name does not begin with an underscore.
362 SEGNAME and SECTNAME are 16 byte arrays (they do not need to be NUL-
363 terminated if the name length is exactly 16 bytes - but must be if the name
364 length is less than 16 characters). */
367 bfd_mach_o_convert_section_name_to_bfd (bfd
*abfd
, const char *segname
,
368 const char *secname
, const char **name
,
371 const mach_o_section_name_xlat
*xlat
;
374 const char *pfx
= "";
377 *flags
= SEC_NO_FLAGS
;
379 /* First search for a canonical name...
380 xlat will be non-null if there is an entry for segname, secname. */
381 xlat
= bfd_mach_o_section_data_for_mach_sect (abfd
, segname
, secname
);
384 len
= strlen (xlat
->bfd_name
);
385 res
= bfd_alloc (abfd
, len
+1);
388 memcpy (res
, xlat
->bfd_name
, len
+1);
390 *flags
= xlat
->bfd_flags
;
394 /* ... else we make up a bfd name from the segment concatenated with the
397 len
= 16 + 1 + 16 + 1;
399 /* Put "LC_SEGMENT." prefix if the segment name is weird (ie doesn't start
400 with an underscore. */
401 if (segname
[0] != '_')
403 static const char seg_pfx
[] = "LC_SEGMENT.";
406 len
+= sizeof (seg_pfx
) - 1;
409 res
= bfd_alloc (abfd
, len
);
412 snprintf (res
, len
, "%s%.16s.%.16s", pfx
, segname
, secname
);
416 /* Convert a bfd section name to a Mach-O segment + section name.
418 If the name is a canonical one for which we have a Darwin match
419 return the translation table - which contains defaults for flags,
420 type, attribute and default alignment data.
422 Otherwise, expand the bfd_name (assumed to be in the form
423 "[LC_SEGMENT.]<segmentname>.<sectionname>") and return NULL. */
425 static const mach_o_section_name_xlat
*
426 bfd_mach_o_convert_section_name_to_mach_o (bfd
*abfd ATTRIBUTE_UNUSED
,
428 bfd_mach_o_section
*section
)
430 const mach_o_section_name_xlat
*xlat
;
431 const char *name
= bfd_get_section_name (abfd
, sect
);
438 memset (section
->segname
, 0, BFD_MACH_O_SEGNAME_SIZE
+ 1);
439 memset (section
->sectname
, 0, BFD_MACH_O_SECTNAME_SIZE
+ 1);
441 /* See if is a canonical name ... */
442 xlat
= bfd_mach_o_section_data_for_bfd_name (abfd
, name
, &segname
);
445 strcpy (section
->segname
, segname
);
446 strcpy (section
->sectname
, xlat
->mach_o_name
);
450 /* .. else we convert our constructed one back to Mach-O.
451 Strip LC_SEGMENT. prefix, if present. */
452 if (strncmp (name
, "LC_SEGMENT.", 11) == 0)
456 dot
= strchr (name
, '.');
459 /* Try to split name into segment and section names. */
460 if (dot
&& dot
!= name
)
463 seclen
= len
- (dot
+ 1 - name
);
465 if (seglen
< 16 && seclen
< 16)
467 memcpy (section
->segname
, name
, seglen
);
468 section
->segname
[seglen
] = 0;
469 memcpy (section
->sectname
, dot
+ 1, seclen
);
470 section
->sectname
[seclen
] = 0;
475 /* The segment and section names are both missing - don't make them
477 if (dot
&& dot
== name
)
480 /* Just duplicate the name into both segment and section. */
483 memcpy (section
->segname
, name
, len
);
484 section
->segname
[len
] = 0;
485 memcpy (section
->sectname
, name
, len
);
486 section
->sectname
[len
] = 0;
490 /* Return the size of an entry for section SEC.
491 Must be called only for symbol pointer section and symbol stubs
495 bfd_mach_o_section_get_entry_size (bfd
*abfd
, bfd_mach_o_section
*sec
)
497 switch (sec
->flags
& BFD_MACH_O_SECTION_TYPE_MASK
)
499 case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS
:
500 case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS
:
501 return bfd_mach_o_wide_p (abfd
) ? 8 : 4;
502 case BFD_MACH_O_S_SYMBOL_STUBS
:
503 return sec
->reserved2
;
510 /* Return the number of indirect symbols for a section.
511 Must be called only for symbol pointer section and symbol stubs
515 bfd_mach_o_section_get_nbr_indirect (bfd
*abfd
, bfd_mach_o_section
*sec
)
519 elsz
= bfd_mach_o_section_get_entry_size (abfd
, sec
);
523 return sec
->size
/ elsz
;
527 /* Copy any private info we understand from the input symbol
528 to the output symbol. */
531 bfd_mach_o_bfd_copy_private_symbol_data (bfd
*ibfd ATTRIBUTE_UNUSED
,
532 asymbol
*isymbol ATTRIBUTE_UNUSED
,
533 bfd
*obfd ATTRIBUTE_UNUSED
,
534 asymbol
*osymbol ATTRIBUTE_UNUSED
)
539 /* Copy any private info we understand from the input section
540 to the output section. */
543 bfd_mach_o_bfd_copy_private_section_data (bfd
*ibfd ATTRIBUTE_UNUSED
,
545 bfd
*obfd ATTRIBUTE_UNUSED
,
548 if (osection
->used_by_bfd
== NULL
)
549 osection
->used_by_bfd
= isection
->used_by_bfd
;
551 if (isection
->used_by_bfd
!= NULL
)
552 memcpy (osection
->used_by_bfd
, isection
->used_by_bfd
,
553 sizeof (bfd_mach_o_section
));
555 if (osection
->used_by_bfd
!= NULL
)
556 ((bfd_mach_o_section
*)osection
->used_by_bfd
)->bfdsection
= osection
;
561 /* Copy any private info we understand from the input bfd
562 to the output bfd. */
565 bfd_mach_o_bfd_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
567 if (bfd_get_flavour (ibfd
) != bfd_target_mach_o_flavour
568 || bfd_get_flavour (obfd
) != bfd_target_mach_o_flavour
)
571 BFD_ASSERT (bfd_mach_o_valid (ibfd
));
572 BFD_ASSERT (bfd_mach_o_valid (obfd
));
574 /* FIXME: copy commands. */
579 /* This allows us to set up to 32 bits of flags (unless we invent some
580 fiendish scheme to subdivide). For now, we'll just set the file flags
581 without error checking - just overwrite. */
584 bfd_mach_o_bfd_set_private_flags (bfd
*abfd
, flagword flags
)
586 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
591 mdata
->header
.flags
= flags
;
595 /* Count the total number of symbols. */
598 bfd_mach_o_count_symbols (bfd
*abfd
)
600 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
602 if (mdata
->symtab
== NULL
)
604 return mdata
->symtab
->nsyms
;
608 bfd_mach_o_get_symtab_upper_bound (bfd
*abfd
)
610 long nsyms
= bfd_mach_o_count_symbols (abfd
);
612 return ((nsyms
+ 1) * sizeof (asymbol
*));
616 bfd_mach_o_canonicalize_symtab (bfd
*abfd
, asymbol
**alocation
)
618 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
619 long nsyms
= bfd_mach_o_count_symbols (abfd
);
620 bfd_mach_o_symtab_command
*sym
= mdata
->symtab
;
628 /* Do not try to read symbols if there are none. */
633 if (!bfd_mach_o_read_symtab_symbols (abfd
))
635 (*_bfd_error_handler
)
636 (_("bfd_mach_o_canonicalize_symtab: unable to load symbols"));
640 BFD_ASSERT (sym
->symbols
!= NULL
);
642 for (j
= 0; j
< sym
->nsyms
; j
++)
643 alocation
[j
] = &sym
->symbols
[j
].symbol
;
651 bfd_mach_o_get_synthetic_symtab (bfd
*abfd
,
652 long symcount ATTRIBUTE_UNUSED
,
653 asymbol
**syms ATTRIBUTE_UNUSED
,
654 long dynsymcount ATTRIBUTE_UNUSED
,
655 asymbol
**dynsyms ATTRIBUTE_UNUSED
,
658 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
659 bfd_mach_o_dysymtab_command
*dysymtab
= mdata
->dysymtab
;
660 bfd_mach_o_symtab_command
*symtab
= mdata
->symtab
;
662 unsigned long count
, i
, j
, n
;
669 if (dysymtab
== NULL
|| symtab
== NULL
|| symtab
->symbols
== NULL
)
672 if (dysymtab
->nindirectsyms
== 0)
675 count
= dysymtab
->nindirectsyms
;
676 size
= count
* sizeof (asymbol
) + 1;
678 for (j
= 0; j
< count
; j
++)
680 unsigned int isym
= dysymtab
->indirect_syms
[j
];
682 if (isym
< symtab
->nsyms
&& symtab
->symbols
[isym
].symbol
.name
)
683 size
+= strlen (symtab
->symbols
[isym
].symbol
.name
) + sizeof ("$stub");
686 s
= *ret
= (asymbol
*) bfd_malloc (size
);
689 names
= (char *) (s
+ count
);
694 for (i
= 0; i
< mdata
->nsects
; i
++)
696 bfd_mach_o_section
*sec
= mdata
->sections
[i
];
697 unsigned int first
, last
;
701 switch (sec
->flags
& BFD_MACH_O_SECTION_TYPE_MASK
)
703 case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS
:
704 case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS
:
705 case BFD_MACH_O_S_SYMBOL_STUBS
:
706 first
= sec
->reserved1
;
707 last
= first
+ bfd_mach_o_section_get_nbr_indirect (abfd
, sec
);
709 entry_size
= bfd_mach_o_section_get_entry_size (abfd
, sec
);
710 for (j
= first
; j
< last
; j
++)
712 unsigned int isym
= dysymtab
->indirect_syms
[j
];
714 s
->flags
= BSF_GLOBAL
| BSF_SYNTHETIC
;
715 s
->section
= sec
->bfdsection
;
716 s
->value
= addr
- sec
->addr
;
719 if (isym
< symtab
->nsyms
720 && symtab
->symbols
[isym
].symbol
.name
)
722 const char *sym
= symtab
->symbols
[isym
].symbol
.name
;
727 memcpy (names
, sym
, len
);
729 memcpy (names
, "$stub", sizeof ("$stub"));
730 names
+= sizeof ("$stub");
749 bfd_mach_o_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
753 bfd_symbol_info (symbol
, ret
);
757 bfd_mach_o_print_symbol (bfd
*abfd
,
760 bfd_print_symbol_type how
)
762 FILE *file
= (FILE *) afile
;
764 bfd_mach_o_asymbol
*asym
= (bfd_mach_o_asymbol
*)symbol
;
768 case bfd_print_symbol_name
:
769 fprintf (file
, "%s", symbol
->name
);
772 bfd_print_symbol_vandf (abfd
, (void *) file
, symbol
);
773 if (asym
->n_type
& BFD_MACH_O_N_STAB
)
774 name
= bfd_get_stab_name (asym
->n_type
);
776 switch (asym
->n_type
& BFD_MACH_O_N_TYPE
)
778 case BFD_MACH_O_N_UNDF
:
779 if (symbol
->value
== 0)
784 case BFD_MACH_O_N_ABS
:
787 case BFD_MACH_O_N_INDR
:
790 case BFD_MACH_O_N_PBUD
:
793 case BFD_MACH_O_N_SECT
:
802 fprintf (file
, " %02x %-6s %02x %04x",
803 asym
->n_type
, name
, asym
->n_sect
, asym
->n_desc
);
804 if ((asym
->n_type
& BFD_MACH_O_N_STAB
) == 0
805 && (asym
->n_type
& BFD_MACH_O_N_TYPE
) == BFD_MACH_O_N_SECT
)
806 fprintf (file
, " [%s]", symbol
->section
->name
);
807 fprintf (file
, " %s", symbol
->name
);
812 bfd_mach_o_convert_architecture (bfd_mach_o_cpu_type mtype
,
813 bfd_mach_o_cpu_subtype msubtype ATTRIBUTE_UNUSED
,
814 enum bfd_architecture
*type
,
815 unsigned long *subtype
)
817 *subtype
= bfd_arch_unknown
;
821 case BFD_MACH_O_CPU_TYPE_VAX
: *type
= bfd_arch_vax
; break;
822 case BFD_MACH_O_CPU_TYPE_MC680x0
: *type
= bfd_arch_m68k
; break;
823 case BFD_MACH_O_CPU_TYPE_I386
:
824 *type
= bfd_arch_i386
;
825 *subtype
= bfd_mach_i386_i386
;
827 case BFD_MACH_O_CPU_TYPE_X86_64
:
828 *type
= bfd_arch_i386
;
829 *subtype
= bfd_mach_x86_64
;
831 case BFD_MACH_O_CPU_TYPE_MIPS
: *type
= bfd_arch_mips
; break;
832 case BFD_MACH_O_CPU_TYPE_MC98000
: *type
= bfd_arch_m98k
; break;
833 case BFD_MACH_O_CPU_TYPE_HPPA
: *type
= bfd_arch_hppa
; break;
834 case BFD_MACH_O_CPU_TYPE_ARM
: *type
= bfd_arch_arm
; break;
835 case BFD_MACH_O_CPU_TYPE_MC88000
: *type
= bfd_arch_m88k
; break;
836 case BFD_MACH_O_CPU_TYPE_SPARC
:
837 *type
= bfd_arch_sparc
;
838 *subtype
= bfd_mach_sparc
;
840 case BFD_MACH_O_CPU_TYPE_I860
: *type
= bfd_arch_i860
; break;
841 case BFD_MACH_O_CPU_TYPE_ALPHA
: *type
= bfd_arch_alpha
; break;
842 case BFD_MACH_O_CPU_TYPE_POWERPC
:
843 *type
= bfd_arch_powerpc
;
844 *subtype
= bfd_mach_ppc
;
846 case BFD_MACH_O_CPU_TYPE_POWERPC_64
:
847 *type
= bfd_arch_powerpc
;
848 *subtype
= bfd_mach_ppc64
;
851 *type
= bfd_arch_unknown
;
857 bfd_mach_o_write_header (bfd
*abfd
, bfd_mach_o_header
*header
)
859 struct mach_o_header_external raw
;
862 size
= mach_o_wide_p (header
) ?
863 BFD_MACH_O_HEADER_64_SIZE
: BFD_MACH_O_HEADER_SIZE
;
865 bfd_h_put_32 (abfd
, header
->magic
, raw
.magic
);
866 bfd_h_put_32 (abfd
, header
->cputype
, raw
.cputype
);
867 bfd_h_put_32 (abfd
, header
->cpusubtype
, raw
.cpusubtype
);
868 bfd_h_put_32 (abfd
, header
->filetype
, raw
.filetype
);
869 bfd_h_put_32 (abfd
, header
->ncmds
, raw
.ncmds
);
870 bfd_h_put_32 (abfd
, header
->sizeofcmds
, raw
.sizeofcmds
);
871 bfd_h_put_32 (abfd
, header
->flags
, raw
.flags
);
873 if (mach_o_wide_p (header
))
874 bfd_h_put_32 (abfd
, header
->reserved
, raw
.reserved
);
876 if (bfd_seek (abfd
, 0, SEEK_SET
) != 0
877 || bfd_bwrite (&raw
, size
, abfd
) != size
)
884 bfd_mach_o_write_thread (bfd
*abfd
, bfd_mach_o_load_command
*command
)
886 bfd_mach_o_thread_command
*cmd
= &command
->command
.thread
;
888 struct mach_o_thread_command_external raw
;
891 BFD_ASSERT ((command
->type
== BFD_MACH_O_LC_THREAD
)
892 || (command
->type
== BFD_MACH_O_LC_UNIXTHREAD
));
895 for (i
= 0; i
< cmd
->nflavours
; i
++)
897 BFD_ASSERT ((cmd
->flavours
[i
].size
% 4) == 0);
898 BFD_ASSERT (cmd
->flavours
[i
].offset
==
899 (command
->offset
+ offset
+ BFD_MACH_O_LC_SIZE
));
901 bfd_h_put_32 (abfd
, cmd
->flavours
[i
].flavour
, raw
.flavour
);
902 bfd_h_put_32 (abfd
, (cmd
->flavours
[i
].size
/ 4), raw
.count
);
904 if (bfd_seek (abfd
, command
->offset
+ offset
, SEEK_SET
) != 0
905 || bfd_bwrite (&raw
, sizeof (raw
), abfd
) != sizeof (raw
))
908 offset
+= cmd
->flavours
[i
].size
+ sizeof (raw
);
915 bfd_mach_o_get_reloc_upper_bound (bfd
*abfd ATTRIBUTE_UNUSED
,
918 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
922 bfd_mach_o_canonicalize_one_reloc (bfd
*abfd
,
923 struct mach_o_reloc_info_external
*raw
,
924 arelent
*res
, asymbol
**syms
)
926 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
927 bfd_mach_o_backend_data
*bed
= bfd_mach_o_get_backend_data (abfd
);
928 bfd_mach_o_reloc_info reloc
;
933 addr
= bfd_get_32 (abfd
, raw
->r_address
);
934 symnum
= bfd_get_32 (abfd
, raw
->r_symbolnum
);
936 if (addr
& BFD_MACH_O_SR_SCATTERED
)
940 /* Scattered relocation.
941 Extract section and offset from r_value. */
942 res
->sym_ptr_ptr
= NULL
;
944 for (j
= 0; j
< mdata
->nsects
; j
++)
946 bfd_mach_o_section
*sect
= mdata
->sections
[j
];
947 if (symnum
>= sect
->addr
&& symnum
< sect
->addr
+ sect
->size
)
949 res
->sym_ptr_ptr
= sect
->bfdsection
->symbol_ptr_ptr
;
950 res
->addend
= symnum
- sect
->addr
;
954 res
->address
= BFD_MACH_O_GET_SR_ADDRESS (addr
);
955 reloc
.r_type
= BFD_MACH_O_GET_SR_TYPE (addr
);
956 reloc
.r_length
= BFD_MACH_O_GET_SR_LENGTH (addr
);
957 reloc
.r_pcrel
= addr
& BFD_MACH_O_SR_PCREL
;
958 reloc
.r_scattered
= 1;
962 unsigned int num
= BFD_MACH_O_GET_R_SYMBOLNUM (symnum
);
965 if (symnum
& BFD_MACH_O_R_EXTERN
)
972 BFD_ASSERT (num
!= 0);
973 BFD_ASSERT (num
<= mdata
->nsects
);
974 sym
= mdata
->sections
[num
- 1]->bfdsection
->symbol_ptr_ptr
;
975 /* For a symbol defined in section S, the addend (stored in the
976 binary) contains the address of the section. To comply with
977 bfd conventio, substract the section address.
978 Use the address from the header, so that the user can modify
979 the vma of the section. */
980 res
->addend
= -mdata
->sections
[num
- 1]->addr
;
983 res
->sym_ptr_ptr
= sym
;
984 reloc
.r_type
= BFD_MACH_O_GET_R_TYPE (symnum
);
985 reloc
.r_length
= BFD_MACH_O_GET_R_LENGTH (symnum
);
986 reloc
.r_pcrel
= (symnum
& BFD_MACH_O_R_PCREL
) ? 1 : 0;
987 reloc
.r_scattered
= 0;
990 if (!(*bed
->_bfd_mach_o_swap_reloc_in
)(res
, &reloc
))
996 bfd_mach_o_canonicalize_relocs (bfd
*abfd
, unsigned long filepos
,
998 arelent
*res
, asymbol
**syms
)
1001 struct mach_o_reloc_info_external
*native_relocs
;
1002 bfd_size_type native_size
;
1004 /* Allocate and read relocs. */
1005 native_size
= count
* BFD_MACH_O_RELENT_SIZE
;
1007 (struct mach_o_reloc_info_external
*) bfd_malloc (native_size
);
1008 if (native_relocs
== NULL
)
1011 if (bfd_seek (abfd
, filepos
, SEEK_SET
) != 0
1012 || bfd_bread (native_relocs
, native_size
, abfd
) != native_size
)
1015 for (i
= 0; i
< count
; i
++)
1017 if (bfd_mach_o_canonicalize_one_reloc (abfd
, &native_relocs
[i
],
1021 free (native_relocs
);
1024 free (native_relocs
);
1029 bfd_mach_o_canonicalize_reloc (bfd
*abfd
, asection
*asect
,
1030 arelent
**rels
, asymbol
**syms
)
1032 bfd_mach_o_backend_data
*bed
= bfd_mach_o_get_backend_data (abfd
);
1036 if (asect
->reloc_count
== 0)
1039 /* No need to go further if we don't know how to read relocs. */
1040 if (bed
->_bfd_mach_o_swap_reloc_in
== NULL
)
1043 res
= bfd_malloc (asect
->reloc_count
* sizeof (arelent
));
1047 if (bfd_mach_o_canonicalize_relocs (abfd
, asect
->rel_filepos
,
1048 asect
->reloc_count
, res
, syms
) < 0)
1054 for (i
= 0; i
< asect
->reloc_count
; i
++)
1057 asect
->relocation
= res
;
1063 bfd_mach_o_get_dynamic_reloc_upper_bound (bfd
*abfd
)
1065 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
1067 if (mdata
->dysymtab
== NULL
)
1069 return (mdata
->dysymtab
->nextrel
+ mdata
->dysymtab
->nlocrel
)
1070 * sizeof (arelent
*);
1074 bfd_mach_o_canonicalize_dynamic_reloc (bfd
*abfd
, arelent
**rels
,
1075 struct bfd_symbol
**syms
)
1077 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
1078 bfd_mach_o_dysymtab_command
*dysymtab
= mdata
->dysymtab
;
1079 bfd_mach_o_backend_data
*bed
= bfd_mach_o_get_backend_data (abfd
);
1083 if (dysymtab
== NULL
)
1085 if (dysymtab
->nextrel
== 0 && dysymtab
->nlocrel
== 0)
1088 /* No need to go further if we don't know how to read relocs. */
1089 if (bed
->_bfd_mach_o_swap_reloc_in
== NULL
)
1092 res
= bfd_malloc ((dysymtab
->nextrel
+ dysymtab
->nlocrel
) * sizeof (arelent
));
1096 if (bfd_mach_o_canonicalize_relocs (abfd
, dysymtab
->extreloff
,
1097 dysymtab
->nextrel
, res
, syms
) < 0)
1103 if (bfd_mach_o_canonicalize_relocs (abfd
, dysymtab
->locreloff
,
1105 res
+ dysymtab
->nextrel
, syms
) < 0)
1111 for (i
= 0; i
< dysymtab
->nextrel
+ dysymtab
->nlocrel
; i
++)
1118 bfd_mach_o_write_relocs (bfd
*abfd
, bfd_mach_o_section
*section
)
1120 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
1124 bfd_mach_o_backend_data
*bed
= bfd_mach_o_get_backend_data (abfd
);
1126 sec
= section
->bfdsection
;
1127 if (sec
->reloc_count
== 0)
1130 if (bed
->_bfd_mach_o_swap_reloc_out
== NULL
)
1133 /* Allocate relocation room. */
1134 mdata
->filelen
= FILE_ALIGN(mdata
->filelen
, 2);
1135 section
->nreloc
= sec
->reloc_count
;
1136 sec
->rel_filepos
= mdata
->filelen
;
1137 section
->reloff
= sec
->rel_filepos
;
1138 mdata
->filelen
+= sec
->reloc_count
* BFD_MACH_O_RELENT_SIZE
;
1140 if (bfd_seek (abfd
, section
->reloff
, SEEK_SET
) != 0)
1143 /* Convert and write. */
1144 entries
= section
->bfdsection
->orelocation
;
1145 for (i
= 0; i
< section
->nreloc
; i
++)
1147 arelent
*rel
= entries
[i
];
1148 struct mach_o_reloc_info_external raw
;
1149 bfd_mach_o_reloc_info info
, *pinfo
= &info
;
1151 /* Convert relocation to an intermediate representation. */
1152 if (!(*bed
->_bfd_mach_o_swap_reloc_out
) (rel
, pinfo
))
1155 /* Lower the relocation info. */
1156 if (pinfo
->r_scattered
)
1160 v
= BFD_MACH_O_SR_SCATTERED
1161 | (pinfo
->r_pcrel
? BFD_MACH_O_SR_PCREL
: 0)
1162 | BFD_MACH_O_SET_SR_LENGTH(pinfo
->r_length
)
1163 | BFD_MACH_O_SET_SR_TYPE(pinfo
->r_type
)
1164 | BFD_MACH_O_SET_SR_ADDRESS(pinfo
->r_address
);
1165 /* Note: scattered relocs have field in reverse order... */
1166 bfd_put_32 (abfd
, v
, raw
.r_address
);
1167 bfd_put_32 (abfd
, pinfo
->r_value
, raw
.r_symbolnum
);
1173 bfd_put_32 (abfd
, pinfo
->r_address
, raw
.r_address
);
1174 v
= BFD_MACH_O_SET_R_SYMBOLNUM (pinfo
->r_value
)
1175 | (pinfo
->r_pcrel
? BFD_MACH_O_R_PCREL
: 0)
1176 | BFD_MACH_O_SET_R_LENGTH (pinfo
->r_length
)
1177 | (pinfo
->r_extern
? BFD_MACH_O_R_EXTERN
: 0)
1178 | BFD_MACH_O_SET_R_TYPE (pinfo
->r_type
);
1179 bfd_put_32 (abfd
, v
, raw
.r_symbolnum
);
1182 if (bfd_bwrite (&raw
, BFD_MACH_O_RELENT_SIZE
, abfd
)
1183 != BFD_MACH_O_RELENT_SIZE
)
1190 bfd_mach_o_write_section_32 (bfd
*abfd
, bfd_mach_o_section
*section
)
1192 struct mach_o_section_32_external raw
;
1194 memcpy (raw
.sectname
, section
->sectname
, 16);
1195 memcpy (raw
.segname
, section
->segname
, 16);
1196 bfd_h_put_32 (abfd
, section
->addr
, raw
.addr
);
1197 bfd_h_put_32 (abfd
, section
->size
, raw
.size
);
1198 bfd_h_put_32 (abfd
, section
->offset
, raw
.offset
);
1199 bfd_h_put_32 (abfd
, section
->align
, raw
.align
);
1200 bfd_h_put_32 (abfd
, section
->reloff
, raw
.reloff
);
1201 bfd_h_put_32 (abfd
, section
->nreloc
, raw
.nreloc
);
1202 bfd_h_put_32 (abfd
, section
->flags
, raw
.flags
);
1203 bfd_h_put_32 (abfd
, section
->reserved1
, raw
.reserved1
);
1204 bfd_h_put_32 (abfd
, section
->reserved2
, raw
.reserved2
);
1206 if (bfd_bwrite (&raw
, BFD_MACH_O_SECTION_SIZE
, abfd
)
1207 != BFD_MACH_O_SECTION_SIZE
)
1214 bfd_mach_o_write_section_64 (bfd
*abfd
, bfd_mach_o_section
*section
)
1216 struct mach_o_section_64_external raw
;
1218 memcpy (raw
.sectname
, section
->sectname
, 16);
1219 memcpy (raw
.segname
, section
->segname
, 16);
1220 bfd_h_put_64 (abfd
, section
->addr
, raw
.addr
);
1221 bfd_h_put_64 (abfd
, section
->size
, raw
.size
);
1222 bfd_h_put_32 (abfd
, section
->offset
, raw
.offset
);
1223 bfd_h_put_32 (abfd
, section
->align
, raw
.align
);
1224 bfd_h_put_32 (abfd
, section
->reloff
, raw
.reloff
);
1225 bfd_h_put_32 (abfd
, section
->nreloc
, raw
.nreloc
);
1226 bfd_h_put_32 (abfd
, section
->flags
, raw
.flags
);
1227 bfd_h_put_32 (abfd
, section
->reserved1
, raw
.reserved1
);
1228 bfd_h_put_32 (abfd
, section
->reserved2
, raw
.reserved2
);
1229 bfd_h_put_32 (abfd
, section
->reserved3
, raw
.reserved3
);
1231 if (bfd_bwrite (&raw
, BFD_MACH_O_SECTION_64_SIZE
, abfd
)
1232 != BFD_MACH_O_SECTION_64_SIZE
)
1239 bfd_mach_o_write_segment_32 (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1241 struct mach_o_segment_command_32_external raw
;
1242 bfd_mach_o_segment_command
*seg
= &command
->command
.segment
;
1243 bfd_mach_o_section
*sec
;
1245 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SEGMENT
);
1247 for (sec
= seg
->sect_head
; sec
!= NULL
; sec
= sec
->next
)
1248 if (!bfd_mach_o_write_relocs (abfd
, sec
))
1251 memcpy (raw
.segname
, seg
->segname
, 16);
1252 bfd_h_put_32 (abfd
, seg
->vmaddr
, raw
.vmaddr
);
1253 bfd_h_put_32 (abfd
, seg
->vmsize
, raw
.vmsize
);
1254 bfd_h_put_32 (abfd
, seg
->fileoff
, raw
.fileoff
);
1255 bfd_h_put_32 (abfd
, seg
->filesize
, raw
.filesize
);
1256 bfd_h_put_32 (abfd
, seg
->maxprot
, raw
.maxprot
);
1257 bfd_h_put_32 (abfd
, seg
->initprot
, raw
.initprot
);
1258 bfd_h_put_32 (abfd
, seg
->nsects
, raw
.nsects
);
1259 bfd_h_put_32 (abfd
, seg
->flags
, raw
.flags
);
1261 if (bfd_seek (abfd
, command
->offset
+ BFD_MACH_O_LC_SIZE
, SEEK_SET
) != 0
1262 || bfd_bwrite (&raw
, sizeof (raw
), abfd
) != sizeof (raw
))
1265 for (sec
= seg
->sect_head
; sec
!= NULL
; sec
= sec
->next
)
1266 if (bfd_mach_o_write_section_32 (abfd
, sec
))
1273 bfd_mach_o_write_segment_64 (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1275 struct mach_o_segment_command_64_external raw
;
1276 bfd_mach_o_segment_command
*seg
= &command
->command
.segment
;
1277 bfd_mach_o_section
*sec
;
1279 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SEGMENT_64
);
1281 for (sec
= seg
->sect_head
; sec
!= NULL
; sec
= sec
->next
)
1282 if (!bfd_mach_o_write_relocs (abfd
, sec
))
1285 memcpy (raw
.segname
, seg
->segname
, 16);
1286 bfd_h_put_64 (abfd
, seg
->vmaddr
, raw
.vmaddr
);
1287 bfd_h_put_64 (abfd
, seg
->vmsize
, raw
.vmsize
);
1288 bfd_h_put_64 (abfd
, seg
->fileoff
, raw
.fileoff
);
1289 bfd_h_put_64 (abfd
, seg
->filesize
, raw
.filesize
);
1290 bfd_h_put_32 (abfd
, seg
->maxprot
, raw
.maxprot
);
1291 bfd_h_put_32 (abfd
, seg
->initprot
, raw
.initprot
);
1292 bfd_h_put_32 (abfd
, seg
->nsects
, raw
.nsects
);
1293 bfd_h_put_32 (abfd
, seg
->flags
, raw
.flags
);
1295 if (bfd_seek (abfd
, command
->offset
+ BFD_MACH_O_LC_SIZE
, SEEK_SET
) != 0
1296 || bfd_bwrite (&raw
, sizeof (raw
), abfd
) != sizeof (raw
))
1299 for (sec
= seg
->sect_head
; sec
!= NULL
; sec
= sec
->next
)
1300 if (bfd_mach_o_write_section_64 (abfd
, sec
))
1307 bfd_mach_o_write_symtab (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1309 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
1310 bfd_mach_o_symtab_command
*sym
= &command
->command
.symtab
;
1312 unsigned int wide
= bfd_mach_o_wide_p (abfd
);
1313 unsigned int symlen
= wide
? BFD_MACH_O_NLIST_64_SIZE
: BFD_MACH_O_NLIST_SIZE
;
1314 struct bfd_strtab_hash
*strtab
;
1315 asymbol
**symbols
= bfd_get_outsymbols (abfd
);
1317 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SYMTAB
);
1319 /* Write the symbols first. */
1320 mdata
->filelen
= FILE_ALIGN(mdata
->filelen
, wide
? 3 : 2);
1321 sym
->symoff
= mdata
->filelen
;
1322 if (bfd_seek (abfd
, sym
->symoff
, SEEK_SET
) != 0)
1325 sym
->nsyms
= bfd_get_symcount (abfd
);
1326 mdata
->filelen
+= sym
->nsyms
* symlen
;
1328 strtab
= _bfd_stringtab_init ();
1333 /* Although we don't strictly need to do this, for compatibility with
1334 Darwin system tools, actually output an empty string for the index
1336 _bfd_stringtab_add (strtab
, "", TRUE
, FALSE
);
1338 for (i
= 0; i
< sym
->nsyms
; i
++)
1340 bfd_size_type str_index
;
1341 bfd_mach_o_asymbol
*s
= (bfd_mach_o_asymbol
*)symbols
[i
];
1343 /* Compute name index. */
1344 /* An index of 0 always means the empty string. */
1345 if (s
->symbol
.name
== 0 || s
->symbol
.name
[0] == '\0')
1349 str_index
= _bfd_stringtab_add (strtab
, s
->symbol
.name
, TRUE
, FALSE
);
1350 if (str_index
== (bfd_size_type
) -1)
1356 struct mach_o_nlist_64_external raw
;
1358 bfd_h_put_32 (abfd
, str_index
, raw
.n_strx
);
1359 bfd_h_put_8 (abfd
, s
->n_type
, raw
.n_type
);
1360 bfd_h_put_8 (abfd
, s
->n_sect
, raw
.n_sect
);
1361 bfd_h_put_16 (abfd
, s
->n_desc
, raw
.n_desc
);
1362 bfd_h_put_64 (abfd
, s
->symbol
.section
->vma
+ s
->symbol
.value
,
1365 if (bfd_bwrite (&raw
, sizeof (raw
), abfd
) != sizeof (raw
))
1370 struct mach_o_nlist_external raw
;
1372 bfd_h_put_32 (abfd
, str_index
, raw
.n_strx
);
1373 bfd_h_put_8 (abfd
, s
->n_type
, raw
.n_type
);
1374 bfd_h_put_8 (abfd
, s
->n_sect
, raw
.n_sect
);
1375 bfd_h_put_16 (abfd
, s
->n_desc
, raw
.n_desc
);
1376 bfd_h_put_32 (abfd
, s
->symbol
.section
->vma
+ s
->symbol
.value
,
1379 if (bfd_bwrite (&raw
, sizeof (raw
), abfd
) != sizeof (raw
))
1383 sym
->strsize
= _bfd_stringtab_size (strtab
);
1384 sym
->stroff
= mdata
->filelen
;
1385 mdata
->filelen
+= sym
->strsize
;
1387 if (_bfd_stringtab_emit (abfd
, strtab
) != TRUE
)
1389 _bfd_stringtab_free (strtab
);
1393 struct mach_o_symtab_command_external raw
;
1395 bfd_h_put_32 (abfd
, sym
->symoff
, raw
.symoff
);
1396 bfd_h_put_32 (abfd
, sym
->nsyms
, raw
.nsyms
);
1397 bfd_h_put_32 (abfd
, sym
->stroff
, raw
.stroff
);
1398 bfd_h_put_32 (abfd
, sym
->strsize
, raw
.strsize
);
1400 if (bfd_seek (abfd
, command
->offset
+ BFD_MACH_O_LC_SIZE
, SEEK_SET
) != 0
1401 || bfd_bwrite (&raw
, sizeof (raw
), abfd
) != sizeof (raw
))
1408 _bfd_stringtab_free (strtab
);
1412 /* Process the symbols and generate Mach-O specific fields.
1416 bfd_mach_o_mangle_symbols (bfd
*abfd
)
1419 asymbol
**symbols
= bfd_get_outsymbols (abfd
);
1421 for (i
= 0; i
< bfd_get_symcount (abfd
); i
++)
1423 bfd_mach_o_asymbol
*s
= (bfd_mach_o_asymbol
*)symbols
[i
];
1425 if (s
->n_type
== BFD_MACH_O_N_UNDF
&& !(s
->symbol
.flags
& BSF_DEBUGGING
))
1427 /* As genuine Mach-O symbols type shouldn't be N_UNDF (undefined
1428 symbols should be N_UNDEF | N_EXT), we suppose the back-end
1429 values haven't been set. */
1430 if (s
->symbol
.section
== bfd_abs_section_ptr
)
1431 s
->n_type
= BFD_MACH_O_N_ABS
;
1432 else if (s
->symbol
.section
== bfd_und_section_ptr
)
1434 s
->n_type
= BFD_MACH_O_N_UNDF
;
1435 if (s
->symbol
.flags
& BSF_WEAK
)
1436 s
->n_desc
|= BFD_MACH_O_N_WEAK_REF
;
1438 else if (s
->symbol
.section
== bfd_com_section_ptr
)
1439 s
->n_type
= BFD_MACH_O_N_UNDF
| BFD_MACH_O_N_EXT
;
1441 s
->n_type
= BFD_MACH_O_N_SECT
;
1443 if (s
->symbol
.flags
& BSF_GLOBAL
)
1444 s
->n_type
|= BFD_MACH_O_N_EXT
;
1447 /* Compute section index. */
1448 if (s
->symbol
.section
!= bfd_abs_section_ptr
1449 && s
->symbol
.section
!= bfd_und_section_ptr
1450 && s
->symbol
.section
!= bfd_com_section_ptr
)
1451 s
->n_sect
= s
->symbol
.section
->target_index
;
1453 /* Number symbols. */
1454 s
->symbol
.udata
.i
= i
;
1460 bfd_mach_o_write_contents (bfd
*abfd
)
1463 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
1465 if (mdata
->header
.ncmds
== 0)
1466 if (!bfd_mach_o_build_commands (abfd
))
1469 /* Now write header information. */
1470 if (mdata
->header
.filetype
== 0)
1472 if (abfd
->flags
& EXEC_P
)
1473 mdata
->header
.filetype
= BFD_MACH_O_MH_EXECUTE
;
1474 else if (abfd
->flags
& DYNAMIC
)
1475 mdata
->header
.filetype
= BFD_MACH_O_MH_DYLIB
;
1477 mdata
->header
.filetype
= BFD_MACH_O_MH_OBJECT
;
1479 if (!bfd_mach_o_write_header (abfd
, &mdata
->header
))
1482 /* Assign a number to each symbols. */
1483 if (!bfd_mach_o_mangle_symbols (abfd
))
1486 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
1488 struct mach_o_load_command_external raw
;
1489 bfd_mach_o_load_command
*cur
= &mdata
->commands
[i
];
1490 unsigned long typeflag
;
1492 typeflag
= cur
->type
| (cur
->type_required
? BFD_MACH_O_LC_REQ_DYLD
: 0);
1494 bfd_h_put_32 (abfd
, typeflag
, raw
.cmd
);
1495 bfd_h_put_32 (abfd
, cur
->len
, raw
.cmdsize
);
1497 if (bfd_seek (abfd
, cur
->offset
, SEEK_SET
) != 0
1498 || bfd_bwrite (&raw
, BFD_MACH_O_LC_SIZE
, abfd
) != 8)
1503 case BFD_MACH_O_LC_SEGMENT
:
1504 if (bfd_mach_o_write_segment_32 (abfd
, cur
) != 0)
1507 case BFD_MACH_O_LC_SEGMENT_64
:
1508 if (bfd_mach_o_write_segment_64 (abfd
, cur
) != 0)
1511 case BFD_MACH_O_LC_SYMTAB
:
1512 if (!bfd_mach_o_write_symtab (abfd
, cur
))
1515 case BFD_MACH_O_LC_SYMSEG
:
1517 case BFD_MACH_O_LC_THREAD
:
1518 case BFD_MACH_O_LC_UNIXTHREAD
:
1519 if (bfd_mach_o_write_thread (abfd
, cur
) != 0)
1522 case BFD_MACH_O_LC_LOADFVMLIB
:
1523 case BFD_MACH_O_LC_IDFVMLIB
:
1524 case BFD_MACH_O_LC_IDENT
:
1525 case BFD_MACH_O_LC_FVMFILE
:
1526 case BFD_MACH_O_LC_PREPAGE
:
1527 case BFD_MACH_O_LC_DYSYMTAB
:
1528 case BFD_MACH_O_LC_LOAD_DYLIB
:
1529 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB
:
1530 case BFD_MACH_O_LC_ID_DYLIB
:
1531 case BFD_MACH_O_LC_REEXPORT_DYLIB
:
1532 case BFD_MACH_O_LC_LOAD_UPWARD_DYLIB
:
1533 case BFD_MACH_O_LC_LOAD_DYLINKER
:
1534 case BFD_MACH_O_LC_ID_DYLINKER
:
1535 case BFD_MACH_O_LC_PREBOUND_DYLIB
:
1536 case BFD_MACH_O_LC_ROUTINES
:
1537 case BFD_MACH_O_LC_SUB_FRAMEWORK
:
1540 (*_bfd_error_handler
) (_("unable to write unknown load command 0x%lx"),
1541 (unsigned long) cur
->type
);
1550 bfd_mach_o_append_section_to_segment (bfd_mach_o_segment_command
*seg
,
1553 bfd_mach_o_section
*s
= (bfd_mach_o_section
*)sec
->used_by_bfd
;
1554 if (seg
->sect_head
== NULL
)
1557 seg
->sect_tail
->next
= s
;
1561 /* Create section Mach-O flags from BFD flags. */
1564 bfd_mach_o_set_section_flags_from_bfd (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
)
1567 bfd_mach_o_section
*s
= bfd_mach_o_get_mach_o_section (sec
);
1569 /* Create default flags. */
1570 bfd_flags
= bfd_get_section_flags (abfd
, sec
);
1571 if ((bfd_flags
& SEC_CODE
) == SEC_CODE
)
1572 s
->flags
= BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS
1573 | BFD_MACH_O_S_ATTR_SOME_INSTRUCTIONS
1574 | BFD_MACH_O_S_REGULAR
;
1575 else if ((bfd_flags
& (SEC_ALLOC
| SEC_LOAD
)) == SEC_ALLOC
)
1576 s
->flags
= BFD_MACH_O_S_ZEROFILL
;
1577 else if (bfd_flags
& SEC_DEBUGGING
)
1578 s
->flags
= BFD_MACH_O_S_REGULAR
| BFD_MACH_O_S_ATTR_DEBUG
;
1580 s
->flags
= BFD_MACH_O_S_REGULAR
;
1583 /* Build Mach-O load commands from the sections. */
1586 bfd_mach_o_build_commands (bfd
*abfd
)
1588 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
1589 unsigned int wide
= mach_o_wide_p (&mdata
->header
);
1590 bfd_mach_o_segment_command
*seg
;
1592 bfd_mach_o_load_command
*cmd
;
1593 bfd_mach_o_load_command
*symtab_cmd
;
1596 /* Return now if commands are already built. */
1597 if (mdata
->header
.ncmds
)
1600 /* Very simple version: a command (segment) to contain all the sections and
1601 a command for the symbol table. */
1602 mdata
->header
.ncmds
= 2;
1603 mdata
->commands
= bfd_alloc (abfd
, mdata
->header
.ncmds
1604 * sizeof (bfd_mach_o_load_command
));
1605 if (mdata
->commands
== NULL
)
1607 cmd
= &mdata
->commands
[0];
1608 seg
= &cmd
->command
.segment
;
1610 seg
->nsects
= bfd_count_sections (abfd
);
1612 /* Set segment command. */
1615 cmd
->type
= BFD_MACH_O_LC_SEGMENT_64
;
1616 cmd
->offset
= BFD_MACH_O_HEADER_64_SIZE
;
1617 cmd
->len
= BFD_MACH_O_LC_SEGMENT_64_SIZE
1618 + BFD_MACH_O_SECTION_64_SIZE
* seg
->nsects
;
1622 cmd
->type
= BFD_MACH_O_LC_SEGMENT
;
1623 cmd
->offset
= BFD_MACH_O_HEADER_SIZE
;
1624 cmd
->len
= BFD_MACH_O_LC_SEGMENT_SIZE
1625 + BFD_MACH_O_SECTION_SIZE
* seg
->nsects
;
1627 cmd
->type_required
= FALSE
;
1628 mdata
->header
.sizeofcmds
= cmd
->len
;
1629 mdata
->filelen
= cmd
->offset
+ cmd
->len
;
1631 /* Set symtab command. */
1632 symtab_cmd
= &mdata
->commands
[1];
1634 symtab_cmd
->type
= BFD_MACH_O_LC_SYMTAB
;
1635 symtab_cmd
->offset
= cmd
->offset
+ cmd
->len
;
1636 symtab_cmd
->len
= 6 * 4;
1637 symtab_cmd
->type_required
= FALSE
;
1639 mdata
->header
.sizeofcmds
+= symtab_cmd
->len
;
1640 mdata
->filelen
+= symtab_cmd
->len
;
1642 /* Fill segment command. */
1643 memset (seg
->segname
, 0, sizeof (seg
->segname
));
1645 seg
->fileoff
= mdata
->filelen
;
1647 seg
->maxprot
= BFD_MACH_O_PROT_READ
| BFD_MACH_O_PROT_WRITE
1648 | BFD_MACH_O_PROT_EXECUTE
;
1649 seg
->initprot
= seg
->maxprot
;
1651 seg
->sect_head
= NULL
;
1652 seg
->sect_tail
= NULL
;
1654 /* Create Mach-O sections.
1655 Section type, attribute and align should have been set when the
1656 section was created - either read in or specified. */
1658 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1660 unsigned bfd_align
= bfd_get_section_alignment (abfd
, sec
);
1661 bfd_mach_o_section
*msect
= bfd_mach_o_get_mach_o_section (sec
);
1663 bfd_mach_o_append_section_to_segment (seg
, sec
);
1665 msect
->addr
= bfd_get_section_vma (abfd
, sec
);
1666 msect
->size
= bfd_get_section_size (sec
);
1667 /* Use the largest alignment set, in case it was bumped after the
1668 section was created. */
1669 msect
->align
= msect
->align
> bfd_align
? msect
->align
: bfd_align
;
1671 if (msect
->size
!= 0)
1673 mdata
->filelen
= FILE_ALIGN (mdata
->filelen
, msect
->align
);
1674 msect
->offset
= mdata
->filelen
;
1679 sec
->filepos
= msect
->offset
;
1680 sec
->target_index
= ++target_index
;
1682 mdata
->filelen
+= msect
->size
;
1684 seg
->filesize
= mdata
->filelen
- seg
->fileoff
;
1685 seg
->vmsize
= seg
->filesize
;
1690 /* Set the contents of a section. */
1693 bfd_mach_o_set_section_contents (bfd
*abfd
,
1695 const void * location
,
1697 bfd_size_type count
)
1701 /* This must be done first, because bfd_set_section_contents is
1702 going to set output_has_begun to TRUE. */
1703 if (! abfd
->output_has_begun
&& ! bfd_mach_o_build_commands (abfd
))
1709 pos
= section
->filepos
+ offset
;
1710 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
1711 || bfd_bwrite (location
, count
, abfd
) != count
)
1718 bfd_mach_o_sizeof_headers (bfd
*a ATTRIBUTE_UNUSED
,
1719 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
1724 /* Make an empty symbol. This is required only because
1725 bfd_make_section_anyway wants to create a symbol for the section. */
1728 bfd_mach_o_make_empty_symbol (bfd
*abfd
)
1730 asymbol
*new_symbol
;
1732 new_symbol
= bfd_zalloc (abfd
, sizeof (bfd_mach_o_asymbol
));
1733 if (new_symbol
== NULL
)
1735 new_symbol
->the_bfd
= abfd
;
1736 new_symbol
->udata
.i
= 0;
1741 bfd_mach_o_read_header (bfd
*abfd
, bfd_mach_o_header
*header
)
1743 struct mach_o_header_external raw
;
1745 bfd_vma (*get32
) (const void *) = NULL
;
1747 /* Just read the magic number. */
1748 if (bfd_seek (abfd
, 0, SEEK_SET
) != 0
1749 || bfd_bread (raw
.magic
, sizeof (raw
.magic
), abfd
) != 4)
1752 if (bfd_getb32 (raw
.magic
) == BFD_MACH_O_MH_MAGIC
)
1754 header
->byteorder
= BFD_ENDIAN_BIG
;
1755 header
->magic
= BFD_MACH_O_MH_MAGIC
;
1756 header
->version
= 1;
1759 else if (bfd_getl32 (raw
.magic
) == BFD_MACH_O_MH_MAGIC
)
1761 header
->byteorder
= BFD_ENDIAN_LITTLE
;
1762 header
->magic
= BFD_MACH_O_MH_MAGIC
;
1763 header
->version
= 1;
1766 else if (bfd_getb32 (raw
.magic
) == BFD_MACH_O_MH_MAGIC_64
)
1768 header
->byteorder
= BFD_ENDIAN_BIG
;
1769 header
->magic
= BFD_MACH_O_MH_MAGIC_64
;
1770 header
->version
= 2;
1773 else if (bfd_getl32 (raw
.magic
) == BFD_MACH_O_MH_MAGIC_64
)
1775 header
->byteorder
= BFD_ENDIAN_LITTLE
;
1776 header
->magic
= BFD_MACH_O_MH_MAGIC_64
;
1777 header
->version
= 2;
1782 header
->byteorder
= BFD_ENDIAN_UNKNOWN
;
1786 /* Once the size of the header is known, read the full header. */
1787 size
= mach_o_wide_p (header
) ?
1788 BFD_MACH_O_HEADER_64_SIZE
: BFD_MACH_O_HEADER_SIZE
;
1790 if (bfd_seek (abfd
, 0, SEEK_SET
) != 0
1791 || bfd_bread (&raw
, size
, abfd
) != size
)
1794 header
->cputype
= (*get32
) (raw
.cputype
);
1795 header
->cpusubtype
= (*get32
) (raw
.cpusubtype
);
1796 header
->filetype
= (*get32
) (raw
.filetype
);
1797 header
->ncmds
= (*get32
) (raw
.ncmds
);
1798 header
->sizeofcmds
= (*get32
) (raw
.sizeofcmds
);
1799 header
->flags
= (*get32
) (raw
.flags
);
1801 if (mach_o_wide_p (header
))
1802 header
->reserved
= (*get32
) (raw
.reserved
);
1808 bfd_mach_o_new_section_hook (bfd
*abfd
, asection
*sec
)
1810 bfd_mach_o_section
*s
;
1811 unsigned bfdalign
= bfd_get_section_alignment (abfd
, sec
);
1813 s
= bfd_mach_o_get_mach_o_section (sec
);
1817 static const mach_o_section_name_xlat
* xlat
;
1819 s
= (bfd_mach_o_section
*) bfd_zalloc (abfd
, sizeof (*s
));
1822 sec
->used_by_bfd
= s
;
1823 s
->bfdsection
= sec
;
1825 /* Create the Darwin seg/sect name pair from the bfd name.
1826 If this is a canonical name for which a specific paiting exists
1827 there will also be defined flags, type, attribute and alignment
1829 xlat
= bfd_mach_o_convert_section_name_to_mach_o (abfd
, sec
, s
);
1832 s
->flags
= xlat
->macho_sectype
| xlat
->macho_secattr
;
1833 s
->align
= xlat
->sectalign
> bfdalign
? xlat
->sectalign
1835 bfd_set_section_alignment (abfd
, sec
, s
->align
);
1836 bfd_flags
= bfd_get_section_flags (abfd
, sec
);
1837 if (bfd_flags
== SEC_NO_FLAGS
)
1838 bfd_set_section_flags (abfd
, sec
, xlat
->bfd_flags
);
1841 /* Create default flags. */
1842 bfd_mach_o_set_section_flags_from_bfd (abfd
, sec
);
1845 return _bfd_generic_new_section_hook (abfd
, sec
);
1849 bfd_mach_o_init_section_from_mach_o (bfd
*abfd
, asection
*sec
,
1853 bfd_mach_o_section
*section
;
1855 flags
= bfd_get_section_flags (abfd
, sec
);
1856 section
= bfd_mach_o_get_mach_o_section (sec
);
1858 /* TODO: see if we should use the xlat system for doing this by
1859 preference and fall back to this for unknown sections. */
1861 if (flags
== SEC_NO_FLAGS
)
1863 /* Try to guess flags. */
1864 if (section
->flags
& BFD_MACH_O_S_ATTR_DEBUG
)
1865 flags
= SEC_DEBUGGING
;
1869 if ((section
->flags
& BFD_MACH_O_SECTION_TYPE_MASK
)
1870 != BFD_MACH_O_S_ZEROFILL
)
1873 if (prot
& BFD_MACH_O_PROT_EXECUTE
)
1875 if (prot
& BFD_MACH_O_PROT_WRITE
)
1877 else if (prot
& BFD_MACH_O_PROT_READ
)
1878 flags
|= SEC_READONLY
;
1884 if ((flags
& SEC_DEBUGGING
) == 0)
1888 if (section
->offset
!= 0)
1889 flags
|= SEC_HAS_CONTENTS
;
1890 if (section
->nreloc
!= 0)
1893 bfd_set_section_flags (abfd
, sec
, flags
);
1895 sec
->vma
= section
->addr
;
1896 sec
->lma
= section
->addr
;
1897 sec
->size
= section
->size
;
1898 sec
->filepos
= section
->offset
;
1899 sec
->alignment_power
= section
->align
;
1900 sec
->segment_mark
= 0;
1901 sec
->reloc_count
= section
->nreloc
;
1902 sec
->rel_filepos
= section
->reloff
;
1906 bfd_mach_o_make_bfd_section (bfd
*abfd
,
1907 const unsigned char *segname
,
1908 const unsigned char *sectname
)
1913 bfd_mach_o_convert_section_name_to_bfd
1914 (abfd
, (const char *)segname
, (const char *)sectname
, &sname
, &flags
);
1918 return bfd_make_section_anyway_with_flags (abfd
, sname
, flags
);
1922 bfd_mach_o_read_section_32 (bfd
*abfd
,
1923 unsigned int offset
,
1926 struct mach_o_section_32_external raw
;
1928 bfd_mach_o_section
*section
;
1930 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0
1931 || (bfd_bread (&raw
, BFD_MACH_O_SECTION_SIZE
, abfd
)
1932 != BFD_MACH_O_SECTION_SIZE
))
1935 sec
= bfd_mach_o_make_bfd_section (abfd
, raw
.segname
, raw
.sectname
);
1939 section
= bfd_mach_o_get_mach_o_section (sec
);
1940 memcpy (section
->segname
, raw
.segname
, sizeof (raw
.segname
));
1941 section
->segname
[BFD_MACH_O_SEGNAME_SIZE
] = 0;
1942 memcpy (section
->sectname
, raw
.sectname
, sizeof (raw
.sectname
));
1943 section
->sectname
[BFD_MACH_O_SECTNAME_SIZE
] = 0;
1944 section
->addr
= bfd_h_get_32 (abfd
, raw
.addr
);
1945 section
->size
= bfd_h_get_32 (abfd
, raw
.size
);
1946 section
->offset
= bfd_h_get_32 (abfd
, raw
.offset
);
1947 section
->align
= bfd_h_get_32 (abfd
, raw
.align
);
1948 section
->reloff
= bfd_h_get_32 (abfd
, raw
.reloff
);
1949 section
->nreloc
= bfd_h_get_32 (abfd
, raw
.nreloc
);
1950 section
->flags
= bfd_h_get_32 (abfd
, raw
.flags
);
1951 section
->reserved1
= bfd_h_get_32 (abfd
, raw
.reserved1
);
1952 section
->reserved2
= bfd_h_get_32 (abfd
, raw
.reserved2
);
1953 section
->reserved3
= 0;
1955 bfd_mach_o_init_section_from_mach_o (abfd
, sec
, prot
);
1961 bfd_mach_o_read_section_64 (bfd
*abfd
,
1962 unsigned int offset
,
1965 struct mach_o_section_64_external raw
;
1967 bfd_mach_o_section
*section
;
1969 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0
1970 || (bfd_bread (&raw
, BFD_MACH_O_SECTION_64_SIZE
, abfd
)
1971 != BFD_MACH_O_SECTION_64_SIZE
))
1974 sec
= bfd_mach_o_make_bfd_section (abfd
, raw
.segname
, raw
.sectname
);
1978 section
= bfd_mach_o_get_mach_o_section (sec
);
1979 memcpy (section
->segname
, raw
.segname
, sizeof (raw
.segname
));
1980 section
->segname
[BFD_MACH_O_SEGNAME_SIZE
] = 0;
1981 memcpy (section
->sectname
, raw
.sectname
, sizeof (raw
.sectname
));
1982 section
->sectname
[BFD_MACH_O_SECTNAME_SIZE
] = 0;
1983 section
->addr
= bfd_h_get_64 (abfd
, raw
.addr
);
1984 section
->size
= bfd_h_get_64 (abfd
, raw
.size
);
1985 section
->offset
= bfd_h_get_32 (abfd
, raw
.offset
);
1986 section
->align
= bfd_h_get_32 (abfd
, raw
.align
);
1987 section
->reloff
= bfd_h_get_32 (abfd
, raw
.reloff
);
1988 section
->nreloc
= bfd_h_get_32 (abfd
, raw
.nreloc
);
1989 section
->flags
= bfd_h_get_32 (abfd
, raw
.flags
);
1990 section
->reserved1
= bfd_h_get_32 (abfd
, raw
.reserved1
);
1991 section
->reserved2
= bfd_h_get_32 (abfd
, raw
.reserved2
);
1992 section
->reserved3
= bfd_h_get_32 (abfd
, raw
.reserved3
);
1994 bfd_mach_o_init_section_from_mach_o (abfd
, sec
, prot
);
2000 bfd_mach_o_read_section (bfd
*abfd
,
2001 unsigned int offset
,
2006 return bfd_mach_o_read_section_64 (abfd
, offset
, prot
);
2008 return bfd_mach_o_read_section_32 (abfd
, offset
, prot
);
2012 bfd_mach_o_read_symtab_symbol (bfd
*abfd
,
2013 bfd_mach_o_symtab_command
*sym
,
2014 bfd_mach_o_asymbol
*s
,
2017 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
2018 unsigned int wide
= mach_o_wide_p (&mdata
->header
);
2019 unsigned int symwidth
=
2020 wide
? BFD_MACH_O_NLIST_64_SIZE
: BFD_MACH_O_NLIST_SIZE
;
2021 unsigned int symoff
= sym
->symoff
+ (i
* symwidth
);
2022 struct mach_o_nlist_64_external raw
;
2023 unsigned char type
= -1;
2024 unsigned char section
= -1;
2026 symvalue value
= -1;
2027 unsigned long stroff
= -1;
2028 unsigned int symtype
= -1;
2030 BFD_ASSERT (sym
->strtab
!= NULL
);
2032 if (bfd_seek (abfd
, symoff
, SEEK_SET
) != 0
2033 || bfd_bread (&raw
, symwidth
, abfd
) != symwidth
)
2035 (*_bfd_error_handler
)
2036 (_("bfd_mach_o_read_symtab_symbol: unable to read %d bytes at %lu"),
2037 symwidth
, (unsigned long) symoff
);
2041 stroff
= bfd_h_get_32 (abfd
, raw
.n_strx
);
2042 type
= bfd_h_get_8 (abfd
, raw
.n_type
);
2043 symtype
= type
& BFD_MACH_O_N_TYPE
;
2044 section
= bfd_h_get_8 (abfd
, raw
.n_sect
);
2045 desc
= bfd_h_get_16 (abfd
, raw
.n_desc
);
2047 value
= bfd_h_get_64 (abfd
, raw
.n_value
);
2049 value
= bfd_h_get_32 (abfd
, raw
.n_value
);
2051 if (stroff
>= sym
->strsize
)
2053 (*_bfd_error_handler
)
2054 (_("bfd_mach_o_read_symtab_symbol: name out of range (%lu >= %lu)"),
2055 (unsigned long) stroff
,
2056 (unsigned long) sym
->strsize
);
2060 s
->symbol
.the_bfd
= abfd
;
2061 s
->symbol
.name
= sym
->strtab
+ stroff
;
2062 s
->symbol
.value
= value
;
2063 s
->symbol
.flags
= 0x0;
2064 s
->symbol
.udata
.i
= 0;
2066 s
->n_sect
= section
;
2069 if (type
& BFD_MACH_O_N_STAB
)
2071 s
->symbol
.flags
|= BSF_DEBUGGING
;
2072 s
->symbol
.section
= bfd_und_section_ptr
;
2084 if ((section
> 0) && (section
<= mdata
->nsects
))
2086 s
->symbol
.section
= mdata
->sections
[section
- 1]->bfdsection
;
2088 s
->symbol
.value
- mdata
->sections
[section
- 1]->addr
;
2095 if (type
& BFD_MACH_O_N_PEXT
)
2096 s
->symbol
.flags
|= BSF_GLOBAL
;
2098 if (type
& BFD_MACH_O_N_EXT
)
2099 s
->symbol
.flags
|= BSF_GLOBAL
;
2101 if (!(type
& (BFD_MACH_O_N_PEXT
| BFD_MACH_O_N_EXT
)))
2102 s
->symbol
.flags
|= BSF_LOCAL
;
2106 case BFD_MACH_O_N_UNDF
:
2107 if (type
== (BFD_MACH_O_N_UNDF
| BFD_MACH_O_N_EXT
)
2108 && s
->symbol
.value
!= 0)
2110 /* A common symbol. */
2111 s
->symbol
.section
= bfd_com_section_ptr
;
2112 s
->symbol
.flags
= BSF_NO_FLAGS
;
2116 s
->symbol
.section
= bfd_und_section_ptr
;
2117 if (s
->n_desc
& BFD_MACH_O_N_WEAK_REF
)
2118 s
->symbol
.flags
|= BSF_WEAK
;
2121 case BFD_MACH_O_N_PBUD
:
2122 s
->symbol
.section
= bfd_und_section_ptr
;
2124 case BFD_MACH_O_N_ABS
:
2125 s
->symbol
.section
= bfd_abs_section_ptr
;
2127 case BFD_MACH_O_N_SECT
:
2128 if ((section
> 0) && (section
<= mdata
->nsects
))
2130 s
->symbol
.section
= mdata
->sections
[section
- 1]->bfdsection
;
2132 s
->symbol
.value
- mdata
->sections
[section
- 1]->addr
;
2136 /* Mach-O uses 0 to mean "no section"; not an error. */
2139 (*_bfd_error_handler
) (_("bfd_mach_o_read_symtab_symbol: "
2140 "symbol \"%s\" specified invalid section %d (max %lu): setting to undefined"),
2141 s
->symbol
.name
, section
, mdata
->nsects
);
2143 s
->symbol
.section
= bfd_und_section_ptr
;
2146 case BFD_MACH_O_N_INDR
:
2147 /* FIXME: we don't follow the BFD convention as this indirect symbol
2148 won't be followed by the referenced one. This looks harmless
2149 unless we start using the linker. */
2150 s
->symbol
.flags
|= BSF_INDIRECT
;
2151 s
->symbol
.section
= bfd_ind_section_ptr
;
2152 s
->symbol
.value
= 0;
2155 (*_bfd_error_handler
) (_("bfd_mach_o_read_symtab_symbol: "
2156 "symbol \"%s\" specified invalid type field 0x%x: setting to undefined"),
2157 s
->symbol
.name
, symtype
);
2158 s
->symbol
.section
= bfd_und_section_ptr
;
2167 bfd_mach_o_read_symtab_strtab (bfd
*abfd
)
2169 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
2170 bfd_mach_o_symtab_command
*sym
= mdata
->symtab
;
2172 /* Fail if there is no symtab. */
2176 /* Success if already loaded. */
2180 if (abfd
->flags
& BFD_IN_MEMORY
)
2182 struct bfd_in_memory
*b
;
2184 b
= (struct bfd_in_memory
*) abfd
->iostream
;
2186 if ((sym
->stroff
+ sym
->strsize
) > b
->size
)
2188 bfd_set_error (bfd_error_file_truncated
);
2191 sym
->strtab
= (char *) b
->buffer
+ sym
->stroff
;
2195 sym
->strtab
= bfd_alloc (abfd
, sym
->strsize
);
2196 if (sym
->strtab
== NULL
)
2199 if (bfd_seek (abfd
, sym
->stroff
, SEEK_SET
) != 0
2200 || bfd_bread (sym
->strtab
, sym
->strsize
, abfd
) != sym
->strsize
)
2202 bfd_set_error (bfd_error_file_truncated
);
2211 bfd_mach_o_read_symtab_symbols (bfd
*abfd
)
2213 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
2214 bfd_mach_o_symtab_command
*sym
= mdata
->symtab
;
2217 if (sym
== NULL
|| sym
->symbols
)
2219 /* Return now if there are no symbols or if already loaded. */
2223 sym
->symbols
= bfd_alloc (abfd
, sym
->nsyms
* sizeof (bfd_mach_o_asymbol
));
2225 if (sym
->symbols
== NULL
)
2227 (*_bfd_error_handler
) (_("bfd_mach_o_read_symtab_symbols: unable to allocate memory for symbols"));
2231 if (!bfd_mach_o_read_symtab_strtab (abfd
))
2234 for (i
= 0; i
< sym
->nsyms
; i
++)
2236 if (!bfd_mach_o_read_symtab_symbol (abfd
, sym
, &sym
->symbols
[i
], i
))
2244 bfd_mach_o_i386_flavour_string (unsigned int flavour
)
2246 switch ((int) flavour
)
2248 case BFD_MACH_O_x86_THREAD_STATE32
: return "x86_THREAD_STATE32";
2249 case BFD_MACH_O_x86_FLOAT_STATE32
: return "x86_FLOAT_STATE32";
2250 case BFD_MACH_O_x86_EXCEPTION_STATE32
: return "x86_EXCEPTION_STATE32";
2251 case BFD_MACH_O_x86_THREAD_STATE64
: return "x86_THREAD_STATE64";
2252 case BFD_MACH_O_x86_FLOAT_STATE64
: return "x86_FLOAT_STATE64";
2253 case BFD_MACH_O_x86_EXCEPTION_STATE64
: return "x86_EXCEPTION_STATE64";
2254 case BFD_MACH_O_x86_THREAD_STATE
: return "x86_THREAD_STATE";
2255 case BFD_MACH_O_x86_FLOAT_STATE
: return "x86_FLOAT_STATE";
2256 case BFD_MACH_O_x86_EXCEPTION_STATE
: return "x86_EXCEPTION_STATE";
2257 case BFD_MACH_O_x86_DEBUG_STATE32
: return "x86_DEBUG_STATE32";
2258 case BFD_MACH_O_x86_DEBUG_STATE64
: return "x86_DEBUG_STATE64";
2259 case BFD_MACH_O_x86_DEBUG_STATE
: return "x86_DEBUG_STATE";
2260 case BFD_MACH_O_x86_THREAD_STATE_NONE
: return "x86_THREAD_STATE_NONE";
2261 default: return "UNKNOWN";
2266 bfd_mach_o_ppc_flavour_string (unsigned int flavour
)
2268 switch ((int) flavour
)
2270 case BFD_MACH_O_PPC_THREAD_STATE
: return "PPC_THREAD_STATE";
2271 case BFD_MACH_O_PPC_FLOAT_STATE
: return "PPC_FLOAT_STATE";
2272 case BFD_MACH_O_PPC_EXCEPTION_STATE
: return "PPC_EXCEPTION_STATE";
2273 case BFD_MACH_O_PPC_VECTOR_STATE
: return "PPC_VECTOR_STATE";
2274 case BFD_MACH_O_PPC_THREAD_STATE64
: return "PPC_THREAD_STATE64";
2275 case BFD_MACH_O_PPC_EXCEPTION_STATE64
: return "PPC_EXCEPTION_STATE64";
2276 default: return "UNKNOWN";
2281 bfd_mach_o_read_dylinker (bfd
*abfd
, bfd_mach_o_load_command
*command
)
2283 bfd_mach_o_dylinker_command
*cmd
= &command
->command
.dylinker
;
2284 struct mach_o_str_command_external raw
;
2285 unsigned int nameoff
;
2287 BFD_ASSERT ((command
->type
== BFD_MACH_O_LC_ID_DYLINKER
)
2288 || (command
->type
== BFD_MACH_O_LC_LOAD_DYLINKER
));
2290 if (bfd_seek (abfd
, command
->offset
+ BFD_MACH_O_LC_SIZE
, SEEK_SET
) != 0
2291 || bfd_bread (&raw
, sizeof (raw
), abfd
) != sizeof (raw
))
2294 nameoff
= bfd_h_get_32 (abfd
, raw
.str
);
2296 cmd
->name_offset
= command
->offset
+ nameoff
;
2297 cmd
->name_len
= command
->len
- nameoff
;
2298 cmd
->name_str
= bfd_alloc (abfd
, cmd
->name_len
);
2299 if (cmd
->name_str
== NULL
)
2301 if (bfd_seek (abfd
, cmd
->name_offset
, SEEK_SET
) != 0
2302 || bfd_bread (cmd
->name_str
, cmd
->name_len
, abfd
) != cmd
->name_len
)
2308 bfd_mach_o_read_dylib (bfd
*abfd
, bfd_mach_o_load_command
*command
)
2310 bfd_mach_o_dylib_command
*cmd
= &command
->command
.dylib
;
2311 struct mach_o_dylib_command_external raw
;
2312 unsigned int nameoff
;
2314 switch (command
->type
)
2316 case BFD_MACH_O_LC_LOAD_DYLIB
:
2317 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB
:
2318 case BFD_MACH_O_LC_ID_DYLIB
:
2319 case BFD_MACH_O_LC_REEXPORT_DYLIB
:
2320 case BFD_MACH_O_LC_LOAD_UPWARD_DYLIB
:
2327 if (bfd_seek (abfd
, command
->offset
+ BFD_MACH_O_LC_SIZE
, SEEK_SET
) != 0
2328 || bfd_bread (&raw
, sizeof (raw
), abfd
) != sizeof (raw
))
2331 nameoff
= bfd_h_get_32 (abfd
, raw
.name
);
2332 cmd
->timestamp
= bfd_h_get_32 (abfd
, raw
.timestamp
);
2333 cmd
->current_version
= bfd_h_get_32 (abfd
, raw
.current_version
);
2334 cmd
->compatibility_version
= bfd_h_get_32 (abfd
, raw
.compatibility_version
);
2336 cmd
->name_offset
= command
->offset
+ nameoff
;
2337 cmd
->name_len
= command
->len
- nameoff
;
2338 cmd
->name_str
= bfd_alloc (abfd
, cmd
->name_len
);
2339 if (cmd
->name_str
== NULL
)
2341 if (bfd_seek (abfd
, cmd
->name_offset
, SEEK_SET
) != 0
2342 || bfd_bread (cmd
->name_str
, cmd
->name_len
, abfd
) != cmd
->name_len
)
2348 bfd_mach_o_read_prebound_dylib (bfd
*abfd ATTRIBUTE_UNUSED
,
2349 bfd_mach_o_load_command
*command ATTRIBUTE_UNUSED
)
2351 /* bfd_mach_o_prebound_dylib_command *cmd = &command->command.prebound_dylib; */
2353 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_PREBOUND_DYLIB
);
2358 bfd_mach_o_read_thread (bfd
*abfd
, bfd_mach_o_load_command
*command
)
2360 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
2361 bfd_mach_o_thread_command
*cmd
= &command
->command
.thread
;
2362 unsigned int offset
;
2363 unsigned int nflavours
;
2366 BFD_ASSERT ((command
->type
== BFD_MACH_O_LC_THREAD
)
2367 || (command
->type
== BFD_MACH_O_LC_UNIXTHREAD
));
2369 /* Count the number of threads. */
2372 while (offset
!= command
->len
)
2374 struct mach_o_thread_command_external raw
;
2376 if (offset
>= command
->len
)
2379 if (bfd_seek (abfd
, command
->offset
+ offset
, SEEK_SET
) != 0
2380 || bfd_bread (&raw
, sizeof (raw
), abfd
) != sizeof (raw
))
2383 offset
+= sizeof (raw
) + bfd_h_get_32 (abfd
, raw
.count
) * 4;
2387 /* Allocate threads. */
2388 cmd
->flavours
= bfd_alloc
2389 (abfd
, nflavours
* sizeof (bfd_mach_o_thread_flavour
));
2390 if (cmd
->flavours
== NULL
)
2392 cmd
->nflavours
= nflavours
;
2396 while (offset
!= command
->len
)
2398 struct mach_o_thread_command_external raw
;
2400 if (offset
>= command
->len
)
2403 if (nflavours
>= cmd
->nflavours
)
2406 if (bfd_seek (abfd
, command
->offset
+ offset
, SEEK_SET
) != 0
2407 || bfd_bread (&raw
, sizeof (raw
), abfd
) != sizeof (raw
))
2410 cmd
->flavours
[nflavours
].flavour
= bfd_h_get_32 (abfd
, raw
.flavour
);
2411 cmd
->flavours
[nflavours
].offset
= command
->offset
+ offset
+ sizeof (raw
);
2412 cmd
->flavours
[nflavours
].size
= bfd_h_get_32 (abfd
, raw
.count
) * 4;
2413 offset
+= cmd
->flavours
[nflavours
].size
+ sizeof (raw
);
2417 for (i
= 0; i
< nflavours
; i
++)
2420 unsigned int snamelen
;
2422 const char *flavourstr
;
2423 const char *prefix
= "LC_THREAD";
2426 switch (mdata
->header
.cputype
)
2428 case BFD_MACH_O_CPU_TYPE_POWERPC
:
2429 case BFD_MACH_O_CPU_TYPE_POWERPC_64
:
2430 flavourstr
= bfd_mach_o_ppc_flavour_string (cmd
->flavours
[i
].flavour
);
2432 case BFD_MACH_O_CPU_TYPE_I386
:
2433 case BFD_MACH_O_CPU_TYPE_X86_64
:
2434 flavourstr
= bfd_mach_o_i386_flavour_string (cmd
->flavours
[i
].flavour
);
2437 flavourstr
= "UNKNOWN_ARCHITECTURE";
2441 snamelen
= strlen (prefix
) + 1 + 20 + 1 + strlen (flavourstr
) + 1;
2442 sname
= bfd_alloc (abfd
, snamelen
);
2448 sprintf (sname
, "%s.%s.%u", prefix
, flavourstr
, j
);
2449 if (bfd_get_section_by_name (abfd
, sname
) == NULL
)
2454 bfdsec
= bfd_make_section_with_flags (abfd
, sname
, SEC_HAS_CONTENTS
);
2458 bfdsec
->size
= cmd
->flavours
[i
].size
;
2459 bfdsec
->filepos
= cmd
->flavours
[i
].offset
;
2460 bfdsec
->alignment_power
= 0x0;
2462 cmd
->section
= bfdsec
;
2469 bfd_mach_o_read_dysymtab (bfd
*abfd
, bfd_mach_o_load_command
*command
)
2471 bfd_mach_o_dysymtab_command
*cmd
= &command
->command
.dysymtab
;
2472 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
2474 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_DYSYMTAB
);
2477 struct mach_o_dysymtab_command_external raw
;
2479 if (bfd_seek (abfd
, command
->offset
+ BFD_MACH_O_LC_SIZE
, SEEK_SET
) != 0
2480 || bfd_bread (&raw
, sizeof (raw
), abfd
) != sizeof (raw
))
2483 cmd
->ilocalsym
= bfd_h_get_32 (abfd
, raw
.ilocalsym
);
2484 cmd
->nlocalsym
= bfd_h_get_32 (abfd
, raw
.nlocalsym
);
2485 cmd
->iextdefsym
= bfd_h_get_32 (abfd
, raw
.iextdefsym
);
2486 cmd
->nextdefsym
= bfd_h_get_32 (abfd
, raw
.nextdefsym
);
2487 cmd
->iundefsym
= bfd_h_get_32 (abfd
, raw
.iundefsym
);
2488 cmd
->nundefsym
= bfd_h_get_32 (abfd
, raw
.nundefsym
);
2489 cmd
->tocoff
= bfd_h_get_32 (abfd
, raw
.tocoff
);
2490 cmd
->ntoc
= bfd_h_get_32 (abfd
, raw
.ntoc
);
2491 cmd
->modtaboff
= bfd_h_get_32 (abfd
, raw
.modtaboff
);
2492 cmd
->nmodtab
= bfd_h_get_32 (abfd
, raw
.nmodtab
);
2493 cmd
->extrefsymoff
= bfd_h_get_32 (abfd
, raw
.extrefsymoff
);
2494 cmd
->nextrefsyms
= bfd_h_get_32 (abfd
, raw
.nextrefsyms
);
2495 cmd
->indirectsymoff
= bfd_h_get_32 (abfd
, raw
.indirectsymoff
);
2496 cmd
->nindirectsyms
= bfd_h_get_32 (abfd
, raw
.nindirectsyms
);
2497 cmd
->extreloff
= bfd_h_get_32 (abfd
, raw
.extreloff
);
2498 cmd
->nextrel
= bfd_h_get_32 (abfd
, raw
.nextrel
);
2499 cmd
->locreloff
= bfd_h_get_32 (abfd
, raw
.locreloff
);
2500 cmd
->nlocrel
= bfd_h_get_32 (abfd
, raw
.nlocrel
);
2503 if (cmd
->nmodtab
!= 0)
2506 int wide
= bfd_mach_o_wide_p (abfd
);
2507 unsigned int module_len
= wide
? 56 : 52;
2510 bfd_alloc (abfd
, cmd
->nmodtab
* sizeof (bfd_mach_o_dylib_module
));
2511 if (cmd
->dylib_module
== NULL
)
2514 if (bfd_seek (abfd
, cmd
->modtaboff
, SEEK_SET
) != 0)
2517 for (i
= 0; i
< cmd
->nmodtab
; i
++)
2519 bfd_mach_o_dylib_module
*module
= &cmd
->dylib_module
[i
];
2521 unsigned char buf
[56];
2523 if (bfd_bread ((void *) buf
, module_len
, abfd
) != module_len
)
2526 module
->module_name_idx
= bfd_h_get_32 (abfd
, buf
+ 0);
2527 module
->iextdefsym
= bfd_h_get_32 (abfd
, buf
+ 4);
2528 module
->nextdefsym
= bfd_h_get_32 (abfd
, buf
+ 8);
2529 module
->irefsym
= bfd_h_get_32 (abfd
, buf
+ 12);
2530 module
->nrefsym
= bfd_h_get_32 (abfd
, buf
+ 16);
2531 module
->ilocalsym
= bfd_h_get_32 (abfd
, buf
+ 20);
2532 module
->nlocalsym
= bfd_h_get_32 (abfd
, buf
+ 24);
2533 module
->iextrel
= bfd_h_get_32 (abfd
, buf
+ 28);
2534 module
->nextrel
= bfd_h_get_32 (abfd
, buf
+ 32);
2535 v
= bfd_h_get_32 (abfd
, buf
+36);
2536 module
->iinit
= v
& 0xffff;
2537 module
->iterm
= (v
>> 16) & 0xffff;
2538 v
= bfd_h_get_32 (abfd
, buf
+ 40);
2539 module
->ninit
= v
& 0xffff;
2540 module
->nterm
= (v
>> 16) & 0xffff;
2543 module
->objc_module_info_size
= bfd_h_get_32 (abfd
, buf
+ 44);
2544 module
->objc_module_info_addr
= bfd_h_get_64 (abfd
, buf
+ 48);
2548 module
->objc_module_info_addr
= bfd_h_get_32 (abfd
, buf
+ 44);
2549 module
->objc_module_info_size
= bfd_h_get_32 (abfd
, buf
+ 48);
2558 cmd
->dylib_toc
= bfd_alloc
2559 (abfd
, cmd
->ntoc
* sizeof (bfd_mach_o_dylib_table_of_content
));
2560 if (cmd
->dylib_toc
== NULL
)
2563 if (bfd_seek (abfd
, cmd
->tocoff
, SEEK_SET
) != 0)
2566 for (i
= 0; i
< cmd
->ntoc
; i
++)
2568 struct mach_o_dylib_table_of_contents_external raw
;
2569 bfd_mach_o_dylib_table_of_content
*toc
= &cmd
->dylib_toc
[i
];
2571 if (bfd_bread (&raw
, sizeof (raw
), abfd
) != sizeof (raw
))
2574 toc
->symbol_index
= bfd_h_get_32 (abfd
, raw
.symbol_index
);
2575 toc
->module_index
= bfd_h_get_32 (abfd
, raw
.module_index
);
2579 if (cmd
->nindirectsyms
!= 0)
2583 cmd
->indirect_syms
= bfd_alloc
2584 (abfd
, cmd
->nindirectsyms
* sizeof (unsigned int));
2585 if (cmd
->indirect_syms
== NULL
)
2588 if (bfd_seek (abfd
, cmd
->indirectsymoff
, SEEK_SET
) != 0)
2591 for (i
= 0; i
< cmd
->nindirectsyms
; i
++)
2593 unsigned char raw
[4];
2594 unsigned int *is
= &cmd
->indirect_syms
[i
];
2596 if (bfd_bread (raw
, sizeof (raw
), abfd
) != sizeof (raw
))
2599 *is
= bfd_h_get_32 (abfd
, raw
);
2603 if (cmd
->nextrefsyms
!= 0)
2608 cmd
->ext_refs
= bfd_alloc
2609 (abfd
, cmd
->nextrefsyms
* sizeof (bfd_mach_o_dylib_reference
));
2610 if (cmd
->ext_refs
== NULL
)
2613 if (bfd_seek (abfd
, cmd
->extrefsymoff
, SEEK_SET
) != 0)
2616 for (i
= 0; i
< cmd
->nextrefsyms
; i
++)
2618 unsigned char raw
[4];
2619 bfd_mach_o_dylib_reference
*ref
= &cmd
->ext_refs
[i
];
2621 if (bfd_bread (raw
, sizeof (raw
), abfd
) != sizeof (raw
))
2624 /* Fields isym and flags are written as bit-fields, thus we need
2625 a specific processing for endianness. */
2626 v
= bfd_h_get_32 (abfd
, raw
);
2627 if (bfd_big_endian (abfd
))
2629 ref
->isym
= (v
>> 8) & 0xffffff;
2630 ref
->flags
= v
& 0xff;
2634 ref
->isym
= v
& 0xffffff;
2635 ref
->flags
= (v
>> 24) & 0xff;
2640 if (mdata
->dysymtab
)
2642 mdata
->dysymtab
= cmd
;
2648 bfd_mach_o_read_symtab (bfd
*abfd
, bfd_mach_o_load_command
*command
)
2650 bfd_mach_o_symtab_command
*symtab
= &command
->command
.symtab
;
2651 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
2652 struct mach_o_symtab_command_external raw
;
2654 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SYMTAB
);
2656 if (bfd_seek (abfd
, command
->offset
+ BFD_MACH_O_LC_SIZE
, SEEK_SET
) != 0
2657 || bfd_bread (&raw
, sizeof (raw
), abfd
) != sizeof (raw
))
2660 symtab
->symoff
= bfd_h_get_32 (abfd
, raw
.symoff
);
2661 symtab
->nsyms
= bfd_h_get_32 (abfd
, raw
.nsyms
);
2662 symtab
->stroff
= bfd_h_get_32 (abfd
, raw
.stroff
);
2663 symtab
->strsize
= bfd_h_get_32 (abfd
, raw
.strsize
);
2664 symtab
->symbols
= NULL
;
2665 symtab
->strtab
= NULL
;
2667 if (symtab
->nsyms
!= 0)
2668 abfd
->flags
|= HAS_SYMS
;
2672 mdata
->symtab
= symtab
;
2677 bfd_mach_o_read_uuid (bfd
*abfd
, bfd_mach_o_load_command
*command
)
2679 bfd_mach_o_uuid_command
*cmd
= &command
->command
.uuid
;
2681 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_UUID
);
2683 if (bfd_seek (abfd
, command
->offset
+ BFD_MACH_O_LC_SIZE
, SEEK_SET
) != 0
2684 || bfd_bread (cmd
->uuid
, 16, abfd
) != 16)
2691 bfd_mach_o_read_linkedit (bfd
*abfd
, bfd_mach_o_load_command
*command
)
2693 bfd_mach_o_linkedit_command
*cmd
= &command
->command
.linkedit
;
2694 struct mach_o_linkedit_data_command_external raw
;
2696 if (bfd_seek (abfd
, command
->offset
+ BFD_MACH_O_LC_SIZE
, SEEK_SET
) != 0
2697 || bfd_bread (&raw
, sizeof (raw
), abfd
) != sizeof (raw
))
2700 cmd
->dataoff
= bfd_get_32 (abfd
, raw
.dataoff
);
2701 cmd
->datasize
= bfd_get_32 (abfd
, raw
.datasize
);
2706 bfd_mach_o_read_str (bfd
*abfd
, bfd_mach_o_load_command
*command
)
2708 bfd_mach_o_str_command
*cmd
= &command
->command
.str
;
2709 struct mach_o_str_command_external raw
;
2712 if (bfd_seek (abfd
, command
->offset
+ BFD_MACH_O_LC_SIZE
, SEEK_SET
) != 0
2713 || bfd_bread (&raw
, sizeof (raw
), abfd
) != sizeof (raw
))
2716 off
= bfd_get_32 (abfd
, raw
.str
);
2717 cmd
->stroff
= command
->offset
+ off
;
2718 cmd
->str_len
= command
->len
- off
;
2719 cmd
->str
= bfd_alloc (abfd
, cmd
->str_len
);
2720 if (cmd
->str
== NULL
)
2722 if (bfd_seek (abfd
, cmd
->stroff
, SEEK_SET
) != 0
2723 || bfd_bread ((void *) cmd
->str
, cmd
->str_len
, abfd
) != cmd
->str_len
)
2729 bfd_mach_o_read_dyld_info (bfd
*abfd
, bfd_mach_o_load_command
*command
)
2731 bfd_mach_o_dyld_info_command
*cmd
= &command
->command
.dyld_info
;
2732 struct mach_o_dyld_info_command_external raw
;
2734 if (bfd_seek (abfd
, command
->offset
+ BFD_MACH_O_LC_SIZE
, SEEK_SET
) != 0
2735 || bfd_bread (&raw
, sizeof (raw
), abfd
) != sizeof (raw
))
2738 cmd
->rebase_off
= bfd_get_32 (abfd
, raw
.rebase_off
);
2739 cmd
->rebase_size
= bfd_get_32 (abfd
, raw
.rebase_size
);
2740 cmd
->bind_off
= bfd_get_32 (abfd
, raw
.bind_off
);
2741 cmd
->bind_size
= bfd_get_32 (abfd
, raw
.bind_size
);
2742 cmd
->weak_bind_off
= bfd_get_32 (abfd
, raw
.weak_bind_off
);
2743 cmd
->weak_bind_size
= bfd_get_32 (abfd
, raw
.weak_bind_size
);
2744 cmd
->lazy_bind_off
= bfd_get_32 (abfd
, raw
.lazy_bind_off
);
2745 cmd
->lazy_bind_size
= bfd_get_32 (abfd
, raw
.lazy_bind_size
);
2746 cmd
->export_off
= bfd_get_32 (abfd
, raw
.export_off
);
2747 cmd
->export_size
= bfd_get_32 (abfd
, raw
.export_size
);
2752 bfd_mach_o_read_version_min (bfd
*abfd
, bfd_mach_o_load_command
*command
)
2754 bfd_mach_o_version_min_command
*cmd
= &command
->command
.version_min
;
2755 struct mach_o_version_min_command_external raw
;
2758 if (bfd_seek (abfd
, command
->offset
+ BFD_MACH_O_LC_SIZE
, SEEK_SET
) != 0
2759 || bfd_bread (&raw
, sizeof (raw
), abfd
) != sizeof (raw
))
2762 ver
= bfd_get_32 (abfd
, raw
.version
);
2763 cmd
->rel
= ver
>> 16;
2764 cmd
->maj
= ver
>> 8;
2766 cmd
->reserved
= bfd_get_32 (abfd
, raw
.reserved
);
2771 bfd_mach_o_read_segment (bfd
*abfd
,
2772 bfd_mach_o_load_command
*command
,
2775 bfd_mach_o_segment_command
*seg
= &command
->command
.segment
;
2780 struct mach_o_segment_command_64_external raw
;
2782 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SEGMENT_64
);
2784 if (bfd_seek (abfd
, command
->offset
+ BFD_MACH_O_LC_SIZE
, SEEK_SET
) != 0
2785 || bfd_bread (&raw
, sizeof (raw
), abfd
) != sizeof (raw
))
2788 memcpy (seg
->segname
, raw
.segname
, 16);
2789 seg
->segname
[16] = '\0';
2791 seg
->vmaddr
= bfd_h_get_64 (abfd
, raw
.vmaddr
);
2792 seg
->vmsize
= bfd_h_get_64 (abfd
, raw
.vmsize
);
2793 seg
->fileoff
= bfd_h_get_64 (abfd
, raw
.fileoff
);
2794 seg
->filesize
= bfd_h_get_64 (abfd
, raw
.filesize
);
2795 seg
->maxprot
= bfd_h_get_32 (abfd
, raw
.maxprot
);
2796 seg
->initprot
= bfd_h_get_32 (abfd
, raw
.initprot
);
2797 seg
->nsects
= bfd_h_get_32 (abfd
, raw
.nsects
);
2798 seg
->flags
= bfd_h_get_32 (abfd
, raw
.flags
);
2802 struct mach_o_segment_command_32_external raw
;
2804 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SEGMENT
);
2806 if (bfd_seek (abfd
, command
->offset
+ BFD_MACH_O_LC_SIZE
, SEEK_SET
) != 0
2807 || bfd_bread (&raw
, sizeof (raw
), abfd
) != sizeof (raw
))
2810 memcpy (seg
->segname
, raw
.segname
, 16);
2811 seg
->segname
[16] = '\0';
2813 seg
->vmaddr
= bfd_h_get_32 (abfd
, raw
.vmaddr
);
2814 seg
->vmsize
= bfd_h_get_32 (abfd
, raw
.vmsize
);
2815 seg
->fileoff
= bfd_h_get_32 (abfd
, raw
.fileoff
);
2816 seg
->filesize
= bfd_h_get_32 (abfd
, raw
.filesize
);
2817 seg
->maxprot
= bfd_h_get_32 (abfd
, raw
.maxprot
);
2818 seg
->initprot
= bfd_h_get_32 (abfd
, raw
.initprot
);
2819 seg
->nsects
= bfd_h_get_32 (abfd
, raw
.nsects
);
2820 seg
->flags
= bfd_h_get_32 (abfd
, raw
.flags
);
2822 seg
->sect_head
= NULL
;
2823 seg
->sect_tail
= NULL
;
2825 for (i
= 0; i
< seg
->nsects
; i
++)
2831 segoff
= command
->offset
+ BFD_MACH_O_LC_SEGMENT_64_SIZE
2832 + (i
* BFD_MACH_O_SECTION_64_SIZE
);
2834 segoff
= command
->offset
+ BFD_MACH_O_LC_SEGMENT_SIZE
2835 + (i
* BFD_MACH_O_SECTION_SIZE
);
2837 sec
= bfd_mach_o_read_section (abfd
, segoff
, seg
->initprot
, wide
);
2841 bfd_mach_o_append_section_to_segment (seg
, sec
);
2848 bfd_mach_o_read_segment_32 (bfd
*abfd
, bfd_mach_o_load_command
*command
)
2850 return bfd_mach_o_read_segment (abfd
, command
, 0);
2854 bfd_mach_o_read_segment_64 (bfd
*abfd
, bfd_mach_o_load_command
*command
)
2856 return bfd_mach_o_read_segment (abfd
, command
, 1);
2860 bfd_mach_o_read_command (bfd
*abfd
, bfd_mach_o_load_command
*command
)
2862 struct mach_o_load_command_external raw
;
2865 /* Read command type and length. */
2866 if (bfd_seek (abfd
, command
->offset
, SEEK_SET
) != 0
2867 || bfd_bread (&raw
, BFD_MACH_O_LC_SIZE
, abfd
) != BFD_MACH_O_LC_SIZE
)
2870 cmd
= bfd_h_get_32 (abfd
, raw
.cmd
);
2871 command
->type
= cmd
& ~BFD_MACH_O_LC_REQ_DYLD
;
2872 command
->type_required
= cmd
& BFD_MACH_O_LC_REQ_DYLD
? TRUE
: FALSE
;
2873 command
->len
= bfd_h_get_32 (abfd
, raw
.cmdsize
);
2875 switch (command
->type
)
2877 case BFD_MACH_O_LC_SEGMENT
:
2878 if (bfd_mach_o_read_segment_32 (abfd
, command
) != 0)
2881 case BFD_MACH_O_LC_SEGMENT_64
:
2882 if (bfd_mach_o_read_segment_64 (abfd
, command
) != 0)
2885 case BFD_MACH_O_LC_SYMTAB
:
2886 if (bfd_mach_o_read_symtab (abfd
, command
) != 0)
2889 case BFD_MACH_O_LC_SYMSEG
:
2891 case BFD_MACH_O_LC_THREAD
:
2892 case BFD_MACH_O_LC_UNIXTHREAD
:
2893 if (bfd_mach_o_read_thread (abfd
, command
) != 0)
2896 case BFD_MACH_O_LC_LOAD_DYLINKER
:
2897 case BFD_MACH_O_LC_ID_DYLINKER
:
2898 if (bfd_mach_o_read_dylinker (abfd
, command
) != 0)
2901 case BFD_MACH_O_LC_LOAD_DYLIB
:
2902 case BFD_MACH_O_LC_ID_DYLIB
:
2903 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB
:
2904 case BFD_MACH_O_LC_REEXPORT_DYLIB
:
2905 case BFD_MACH_O_LC_LOAD_UPWARD_DYLIB
:
2906 if (bfd_mach_o_read_dylib (abfd
, command
) != 0)
2909 case BFD_MACH_O_LC_PREBOUND_DYLIB
:
2910 if (bfd_mach_o_read_prebound_dylib (abfd
, command
) != 0)
2913 case BFD_MACH_O_LC_LOADFVMLIB
:
2914 case BFD_MACH_O_LC_IDFVMLIB
:
2915 case BFD_MACH_O_LC_IDENT
:
2916 case BFD_MACH_O_LC_FVMFILE
:
2917 case BFD_MACH_O_LC_PREPAGE
:
2918 case BFD_MACH_O_LC_ROUTINES
:
2919 case BFD_MACH_O_LC_ROUTINES_64
:
2921 case BFD_MACH_O_LC_SUB_FRAMEWORK
:
2922 case BFD_MACH_O_LC_SUB_UMBRELLA
:
2923 case BFD_MACH_O_LC_SUB_LIBRARY
:
2924 case BFD_MACH_O_LC_SUB_CLIENT
:
2925 case BFD_MACH_O_LC_RPATH
:
2926 if (bfd_mach_o_read_str (abfd
, command
) != 0)
2929 case BFD_MACH_O_LC_DYSYMTAB
:
2930 if (bfd_mach_o_read_dysymtab (abfd
, command
) != 0)
2933 case BFD_MACH_O_LC_TWOLEVEL_HINTS
:
2934 case BFD_MACH_O_LC_PREBIND_CKSUM
:
2936 case BFD_MACH_O_LC_UUID
:
2937 if (bfd_mach_o_read_uuid (abfd
, command
) != 0)
2940 case BFD_MACH_O_LC_CODE_SIGNATURE
:
2941 case BFD_MACH_O_LC_SEGMENT_SPLIT_INFO
:
2942 case BFD_MACH_O_LC_FUNCTION_STARTS
:
2943 if (bfd_mach_o_read_linkedit (abfd
, command
) != 0)
2946 case BFD_MACH_O_LC_DYLD_INFO
:
2947 if (bfd_mach_o_read_dyld_info (abfd
, command
) != 0)
2950 case BFD_MACH_O_LC_VERSION_MIN_MACOSX
:
2951 case BFD_MACH_O_LC_VERSION_MIN_IPHONEOS
:
2952 if (!bfd_mach_o_read_version_min (abfd
, command
))
2956 (*_bfd_error_handler
)(_("%B: unable to read unknown load command 0x%lx"),
2957 abfd
, (unsigned long) command
->type
);
2965 bfd_mach_o_flatten_sections (bfd
*abfd
)
2967 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
2971 /* Count total number of sections. */
2974 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
2976 if (mdata
->commands
[i
].type
== BFD_MACH_O_LC_SEGMENT
2977 || mdata
->commands
[i
].type
== BFD_MACH_O_LC_SEGMENT_64
)
2979 bfd_mach_o_segment_command
*seg
;
2981 seg
= &mdata
->commands
[i
].command
.segment
;
2982 mdata
->nsects
+= seg
->nsects
;
2986 /* Allocate sections array. */
2987 mdata
->sections
= bfd_alloc (abfd
,
2988 mdata
->nsects
* sizeof (bfd_mach_o_section
*));
2990 /* Fill the array. */
2993 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
2995 if (mdata
->commands
[i
].type
== BFD_MACH_O_LC_SEGMENT
2996 || mdata
->commands
[i
].type
== BFD_MACH_O_LC_SEGMENT_64
)
2998 bfd_mach_o_segment_command
*seg
;
2999 bfd_mach_o_section
*sec
;
3001 seg
= &mdata
->commands
[i
].command
.segment
;
3002 BFD_ASSERT (csect
+ seg
->nsects
<= mdata
->nsects
);
3004 for (sec
= seg
->sect_head
; sec
!= NULL
; sec
= sec
->next
)
3005 mdata
->sections
[csect
++] = sec
;
3011 bfd_mach_o_scan_start_address (bfd
*abfd
)
3013 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
3014 bfd_mach_o_thread_command
*cmd
= NULL
;
3017 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
3018 if ((mdata
->commands
[i
].type
== BFD_MACH_O_LC_THREAD
) ||
3019 (mdata
->commands
[i
].type
== BFD_MACH_O_LC_UNIXTHREAD
))
3021 cmd
= &mdata
->commands
[i
].command
.thread
;
3028 /* FIXME: create a subtarget hook ? */
3029 for (i
= 0; i
< cmd
->nflavours
; i
++)
3031 if ((mdata
->header
.cputype
== BFD_MACH_O_CPU_TYPE_I386
)
3032 && (cmd
->flavours
[i
].flavour
3033 == (unsigned long) BFD_MACH_O_x86_THREAD_STATE32
))
3035 unsigned char buf
[4];
3037 if (bfd_seek (abfd
, cmd
->flavours
[i
].offset
+ 40, SEEK_SET
) != 0
3038 || bfd_bread (buf
, 4, abfd
) != 4)
3041 abfd
->start_address
= bfd_h_get_32 (abfd
, buf
);
3043 else if ((mdata
->header
.cputype
== BFD_MACH_O_CPU_TYPE_POWERPC
)
3044 && (cmd
->flavours
[i
].flavour
== BFD_MACH_O_PPC_THREAD_STATE
))
3046 unsigned char buf
[4];
3048 if (bfd_seek (abfd
, cmd
->flavours
[i
].offset
+ 0, SEEK_SET
) != 0
3049 || bfd_bread (buf
, 4, abfd
) != 4)
3052 abfd
->start_address
= bfd_h_get_32 (abfd
, buf
);
3054 else if ((mdata
->header
.cputype
== BFD_MACH_O_CPU_TYPE_POWERPC_64
)
3055 && (cmd
->flavours
[i
].flavour
== BFD_MACH_O_PPC_THREAD_STATE64
))
3057 unsigned char buf
[8];
3059 if (bfd_seek (abfd
, cmd
->flavours
[i
].offset
+ 0, SEEK_SET
) != 0
3060 || bfd_bread (buf
, 8, abfd
) != 8)
3063 abfd
->start_address
= bfd_h_get_64 (abfd
, buf
);
3065 else if ((mdata
->header
.cputype
== BFD_MACH_O_CPU_TYPE_X86_64
)
3066 && (cmd
->flavours
[i
].flavour
== BFD_MACH_O_x86_THREAD_STATE64
))
3068 unsigned char buf
[8];
3070 if (bfd_seek (abfd
, cmd
->flavours
[i
].offset
+ (16 * 8), SEEK_SET
) != 0
3071 || bfd_bread (buf
, 8, abfd
) != 8)
3074 abfd
->start_address
= bfd_h_get_64 (abfd
, buf
);
3082 bfd_mach_o_set_arch_mach (bfd
*abfd
,
3083 enum bfd_architecture arch
,
3084 unsigned long machine
)
3086 bfd_mach_o_backend_data
*bed
= bfd_mach_o_get_backend_data (abfd
);
3088 /* If this isn't the right architecture for this backend, and this
3089 isn't the generic backend, fail. */
3090 if (arch
!= bed
->arch
3091 && arch
!= bfd_arch_unknown
3092 && bed
->arch
!= bfd_arch_unknown
)
3095 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
3099 bfd_mach_o_scan (bfd
*abfd
,
3100 bfd_mach_o_header
*header
,
3101 bfd_mach_o_data_struct
*mdata
)
3104 enum bfd_architecture cputype
;
3105 unsigned long cpusubtype
;
3106 unsigned int hdrsize
;
3108 hdrsize
= mach_o_wide_p (header
) ?
3109 BFD_MACH_O_HEADER_64_SIZE
: BFD_MACH_O_HEADER_SIZE
;
3111 mdata
->header
= *header
;
3113 abfd
->flags
= abfd
->flags
& BFD_IN_MEMORY
;
3114 switch (header
->filetype
)
3116 case BFD_MACH_O_MH_OBJECT
:
3117 abfd
->flags
|= HAS_RELOC
;
3119 case BFD_MACH_O_MH_EXECUTE
:
3120 abfd
->flags
|= EXEC_P
;
3122 case BFD_MACH_O_MH_DYLIB
:
3123 case BFD_MACH_O_MH_BUNDLE
:
3124 abfd
->flags
|= DYNAMIC
;
3128 abfd
->tdata
.mach_o_data
= mdata
;
3130 bfd_mach_o_convert_architecture (header
->cputype
, header
->cpusubtype
,
3131 &cputype
, &cpusubtype
);
3132 if (cputype
== bfd_arch_unknown
)
3134 (*_bfd_error_handler
)
3135 (_("bfd_mach_o_scan: unknown architecture 0x%lx/0x%lx"),
3136 header
->cputype
, header
->cpusubtype
);
3140 bfd_set_arch_mach (abfd
, cputype
, cpusubtype
);
3142 if (header
->ncmds
!= 0)
3144 mdata
->commands
= bfd_alloc
3145 (abfd
, header
->ncmds
* sizeof (bfd_mach_o_load_command
));
3146 if (mdata
->commands
== NULL
)
3149 for (i
= 0; i
< header
->ncmds
; i
++)
3151 bfd_mach_o_load_command
*cur
= &mdata
->commands
[i
];
3154 cur
->offset
= hdrsize
;
3157 bfd_mach_o_load_command
*prev
= &mdata
->commands
[i
- 1];
3158 cur
->offset
= prev
->offset
+ prev
->len
;
3161 if (bfd_mach_o_read_command (abfd
, cur
) < 0)
3166 if (bfd_mach_o_scan_start_address (abfd
) < 0)
3169 bfd_mach_o_flatten_sections (abfd
);
3174 bfd_mach_o_mkobject_init (bfd
*abfd
)
3176 bfd_mach_o_data_struct
*mdata
= NULL
;
3178 mdata
= bfd_alloc (abfd
, sizeof (bfd_mach_o_data_struct
));
3181 abfd
->tdata
.mach_o_data
= mdata
;
3183 mdata
->header
.magic
= 0;
3184 mdata
->header
.cputype
= 0;
3185 mdata
->header
.cpusubtype
= 0;
3186 mdata
->header
.filetype
= 0;
3187 mdata
->header
.ncmds
= 0;
3188 mdata
->header
.sizeofcmds
= 0;
3189 mdata
->header
.flags
= 0;
3190 mdata
->header
.byteorder
= BFD_ENDIAN_UNKNOWN
;
3191 mdata
->commands
= NULL
;
3193 mdata
->sections
= NULL
;
3199 bfd_mach_o_gen_mkobject (bfd
*abfd
)
3201 bfd_mach_o_data_struct
*mdata
;
3203 if (!bfd_mach_o_mkobject_init (abfd
))
3206 mdata
= bfd_mach_o_get_data (abfd
);
3207 mdata
->header
.magic
= BFD_MACH_O_MH_MAGIC
;
3208 mdata
->header
.cputype
= 0;
3209 mdata
->header
.cpusubtype
= 0;
3210 mdata
->header
.byteorder
= abfd
->xvec
->byteorder
;
3211 mdata
->header
.version
= 1;
3217 bfd_mach_o_header_p (bfd
*abfd
,
3218 bfd_mach_o_filetype filetype
,
3219 bfd_mach_o_cpu_type cputype
)
3221 struct bfd_preserve preserve
;
3222 bfd_mach_o_header header
;
3224 preserve
.marker
= NULL
;
3225 if (!bfd_mach_o_read_header (abfd
, &header
))
3228 if (! (header
.byteorder
== BFD_ENDIAN_BIG
3229 || header
.byteorder
== BFD_ENDIAN_LITTLE
))
3231 (*_bfd_error_handler
) (_("unknown header byte-order value 0x%lx"),
3232 (unsigned long) header
.byteorder
);
3236 if (! ((header
.byteorder
== BFD_ENDIAN_BIG
3237 && abfd
->xvec
->byteorder
== BFD_ENDIAN_BIG
3238 && abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
3239 || (header
.byteorder
== BFD_ENDIAN_LITTLE
3240 && abfd
->xvec
->byteorder
== BFD_ENDIAN_LITTLE
3241 && abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)))
3244 /* Check cputype and filetype.
3245 In case of wildcard, do not accept magics that are handled by existing
3249 if (header
.cputype
!= cputype
)
3254 switch (header
.cputype
)
3256 case BFD_MACH_O_CPU_TYPE_I386
:
3257 /* Handled by mach-o-i386 */
3265 if (header
.filetype
!= filetype
)
3270 switch (header
.filetype
)
3272 case BFD_MACH_O_MH_CORE
:
3273 /* Handled by core_p */
3280 preserve
.marker
= bfd_zalloc (abfd
, sizeof (bfd_mach_o_data_struct
));
3281 if (preserve
.marker
== NULL
3282 || !bfd_preserve_save (abfd
, &preserve
))
3285 if (!bfd_mach_o_scan (abfd
, &header
,
3286 (bfd_mach_o_data_struct
*) preserve
.marker
))
3289 bfd_preserve_finish (abfd
, &preserve
);
3293 bfd_set_error (bfd_error_wrong_format
);
3296 if (preserve
.marker
!= NULL
)
3297 bfd_preserve_restore (abfd
, &preserve
);
3301 static const bfd_target
*
3302 bfd_mach_o_gen_object_p (bfd
*abfd
)
3304 return bfd_mach_o_header_p (abfd
, 0, 0);
3307 static const bfd_target
*
3308 bfd_mach_o_gen_core_p (bfd
*abfd
)
3310 return bfd_mach_o_header_p (abfd
, BFD_MACH_O_MH_CORE
, 0);
3313 typedef struct mach_o_fat_archentry
3315 unsigned long cputype
;
3316 unsigned long cpusubtype
;
3317 unsigned long offset
;
3319 unsigned long align
;
3320 } mach_o_fat_archentry
;
3322 typedef struct mach_o_fat_data_struct
3324 unsigned long magic
;
3325 unsigned long nfat_arch
;
3326 mach_o_fat_archentry
*archentries
;
3327 } mach_o_fat_data_struct
;
3330 bfd_mach_o_archive_p (bfd
*abfd
)
3332 mach_o_fat_data_struct
*adata
= NULL
;
3333 struct mach_o_fat_header_external hdr
;
3336 if (bfd_seek (abfd
, 0, SEEK_SET
) != 0
3337 || bfd_bread (&hdr
, sizeof (hdr
), abfd
) != sizeof (hdr
))
3340 adata
= bfd_alloc (abfd
, sizeof (mach_o_fat_data_struct
));
3344 adata
->magic
= bfd_getb32 (hdr
.magic
);
3345 adata
->nfat_arch
= bfd_getb32 (hdr
.nfat_arch
);
3346 if (adata
->magic
!= 0xcafebabe)
3348 /* Avoid matching Java bytecode files, which have the same magic number.
3349 In the Java bytecode file format this field contains the JVM version,
3350 which starts at 43.0. */
3351 if (adata
->nfat_arch
> 30)
3354 adata
->archentries
=
3355 bfd_alloc (abfd
, adata
->nfat_arch
* sizeof (mach_o_fat_archentry
));
3356 if (adata
->archentries
== NULL
)
3359 for (i
= 0; i
< adata
->nfat_arch
; i
++)
3361 struct mach_o_fat_arch_external arch
;
3362 if (bfd_bread (&arch
, sizeof (arch
), abfd
) != sizeof (arch
))
3364 adata
->archentries
[i
].cputype
= bfd_getb32 (arch
.cputype
);
3365 adata
->archentries
[i
].cpusubtype
= bfd_getb32 (arch
.cpusubtype
);
3366 adata
->archentries
[i
].offset
= bfd_getb32 (arch
.offset
);
3367 adata
->archentries
[i
].size
= bfd_getb32 (arch
.size
);
3368 adata
->archentries
[i
].align
= bfd_getb32 (arch
.align
);
3371 abfd
->tdata
.mach_o_fat_data
= adata
;
3376 bfd_release (abfd
, adata
);
3377 bfd_set_error (bfd_error_wrong_format
);
3382 bfd_mach_o_openr_next_archived_file (bfd
*archive
, bfd
*prev
)
3384 mach_o_fat_data_struct
*adata
;
3385 mach_o_fat_archentry
*entry
= NULL
;
3388 enum bfd_architecture arch_type
;
3389 unsigned long arch_subtype
;
3391 adata
= (mach_o_fat_data_struct
*) archive
->tdata
.mach_o_fat_data
;
3392 BFD_ASSERT (adata
!= NULL
);
3394 /* Find index of previous entry. */
3396 i
= 0; /* Start at first one. */
3399 for (i
= 0; i
< adata
->nfat_arch
; i
++)
3401 if (adata
->archentries
[i
].offset
== prev
->origin
)
3405 if (i
== adata
->nfat_arch
)
3408 bfd_set_error (bfd_error_bad_value
);
3411 i
++; /* Get next entry. */
3414 if (i
>= adata
->nfat_arch
)
3416 bfd_set_error (bfd_error_no_more_archived_files
);
3420 entry
= &adata
->archentries
[i
];
3421 nbfd
= _bfd_new_bfd_contained_in (archive
);
3425 nbfd
->origin
= entry
->offset
;
3427 bfd_mach_o_convert_architecture (entry
->cputype
, entry
->cpusubtype
,
3428 &arch_type
, &arch_subtype
);
3430 /* Create the member filename. Use ARCH_NAME. */
3431 nbfd
->filename
= bfd_printable_arch_mach (arch_type
, arch_subtype
);
3432 nbfd
->iostream
= NULL
;
3433 bfd_set_arch_mach (nbfd
, arch_type
, arch_subtype
);
3438 /* If ABFD format is FORMAT and architecture is ARCH, return it.
3439 If ABFD is a fat image containing a member that corresponds to FORMAT
3440 and ARCH, returns it.
3441 In other case, returns NULL.
3442 This function allows transparent uses of fat images. */
3444 bfd_mach_o_fat_extract (bfd
*abfd
,
3446 const bfd_arch_info_type
*arch
)
3449 mach_o_fat_data_struct
*adata
;
3452 if (bfd_check_format (abfd
, format
))
3454 if (bfd_get_arch_info (abfd
) == arch
)
3458 if (!bfd_check_format (abfd
, bfd_archive
)
3459 || abfd
->xvec
!= &mach_o_fat_vec
)
3462 /* This is a Mach-O fat image. */
3463 adata
= (mach_o_fat_data_struct
*) abfd
->tdata
.mach_o_fat_data
;
3464 BFD_ASSERT (adata
!= NULL
);
3466 for (i
= 0; i
< adata
->nfat_arch
; i
++)
3468 struct mach_o_fat_archentry
*e
= &adata
->archentries
[i
];
3469 enum bfd_architecture cpu_type
;
3470 unsigned long cpu_subtype
;
3472 bfd_mach_o_convert_architecture (e
->cputype
, e
->cpusubtype
,
3473 &cpu_type
, &cpu_subtype
);
3474 if (cpu_type
!= arch
->arch
|| cpu_subtype
!= arch
->mach
)
3477 /* The architecture is found. */
3478 res
= _bfd_new_bfd_contained_in (abfd
);
3482 res
->origin
= e
->offset
;
3484 res
->filename
= bfd_printable_arch_mach (cpu_type
, cpu_subtype
);
3485 res
->iostream
= NULL
;
3487 if (bfd_check_format (res
, format
))
3489 BFD_ASSERT (bfd_get_arch_info (res
) == arch
);
3500 bfd_mach_o_lookup_command (bfd
*abfd
,
3501 bfd_mach_o_load_command_type type
,
3502 bfd_mach_o_load_command
**mcommand
)
3504 struct mach_o_data_struct
*md
= bfd_mach_o_get_data (abfd
);
3505 bfd_mach_o_load_command
*ncmd
= NULL
;
3506 unsigned int i
, num
;
3508 BFD_ASSERT (md
!= NULL
);
3509 BFD_ASSERT (mcommand
!= NULL
);
3512 for (i
= 0; i
< md
->header
.ncmds
; i
++)
3514 struct bfd_mach_o_load_command
*cmd
= &md
->commands
[i
];
3516 if (cmd
->type
!= type
)
3529 bfd_mach_o_stack_addr (enum bfd_mach_o_cpu_type type
)
3533 case BFD_MACH_O_CPU_TYPE_MC680x0
:
3535 case BFD_MACH_O_CPU_TYPE_MC88000
:
3537 case BFD_MACH_O_CPU_TYPE_POWERPC
:
3539 case BFD_MACH_O_CPU_TYPE_I386
:
3541 case BFD_MACH_O_CPU_TYPE_SPARC
:
3543 case BFD_MACH_O_CPU_TYPE_I860
:
3545 case BFD_MACH_O_CPU_TYPE_HPPA
:
3546 return 0xc0000000 - 0x04000000;
3552 const bfd_mach_o_xlat_name bfd_mach_o_section_type_name
[] =
3554 { "regular", BFD_MACH_O_S_REGULAR
},
3555 { "zerofill", BFD_MACH_O_S_ZEROFILL
},
3556 { "cstring_literals", BFD_MACH_O_S_CSTRING_LITERALS
},
3557 { "4byte_literals", BFD_MACH_O_S_4BYTE_LITERALS
},
3558 { "8byte_literals", BFD_MACH_O_S_8BYTE_LITERALS
},
3559 { "literal_pointers", BFD_MACH_O_S_LITERAL_POINTERS
},
3560 { "non_lazy_symbol_pointers", BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS
},
3561 { "lazy_symbol_pointers", BFD_MACH_O_S_LAZY_SYMBOL_POINTERS
},
3562 { "symbol_stubs", BFD_MACH_O_S_SYMBOL_STUBS
},
3563 { "mod_init_func_pointers", BFD_MACH_O_S_MOD_INIT_FUNC_POINTERS
},
3564 { "mod_fini_func_pointers", BFD_MACH_O_S_MOD_FINI_FUNC_POINTERS
},
3565 { "coalesced", BFD_MACH_O_S_COALESCED
},
3566 { "gb_zerofill", BFD_MACH_O_S_GB_ZEROFILL
},
3567 { "interposing", BFD_MACH_O_S_INTERPOSING
},
3568 { "16byte_literals", BFD_MACH_O_S_16BYTE_LITERALS
},
3569 { "dtrace_dof", BFD_MACH_O_S_DTRACE_DOF
},
3570 { "symbol_stubs", BFD_MACH_O_S_SYMBOL_STUBS
},
3571 { "lazy_dylib_symbol_pointers", BFD_MACH_O_S_LAZY_DYLIB_SYMBOL_POINTERS
},
3575 const bfd_mach_o_xlat_name bfd_mach_o_section_attribute_name
[] =
3577 { "loc_reloc", BFD_MACH_O_S_ATTR_LOC_RELOC
},
3578 { "ext_reloc", BFD_MACH_O_S_ATTR_EXT_RELOC
},
3579 { "some_instructions", BFD_MACH_O_S_ATTR_SOME_INSTRUCTIONS
},
3580 { "debug", BFD_MACH_O_S_ATTR_DEBUG
},
3581 { "modifying_code", BFD_MACH_O_S_SELF_MODIFYING_CODE
},
3582 { "live_support", BFD_MACH_O_S_ATTR_LIVE_SUPPORT
},
3583 { "no_dead_strip", BFD_MACH_O_S_ATTR_NO_DEAD_STRIP
},
3584 { "strip_static_syms", BFD_MACH_O_S_ATTR_STRIP_STATIC_SYMS
},
3585 { "no_toc", BFD_MACH_O_S_ATTR_NO_TOC
},
3586 { "pure_instructions", BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS
},
3587 { "self_modifying_code", BFD_MACH_O_S_SELF_MODIFYING_CODE
},
3591 /* Get the section type from NAME. Return 256 if NAME is unknown. */
3594 bfd_mach_o_get_section_type_from_name (const char *name
)
3596 const bfd_mach_o_xlat_name
*x
;
3598 for (x
= bfd_mach_o_section_type_name
; x
->name
; x
++)
3599 if (strcmp (x
->name
, name
) == 0)
3601 /* Maximum section ID = 0xff. */
3605 /* Get the section attribute from NAME. Return -1 if NAME is unknown. */
3608 bfd_mach_o_get_section_attribute_from_name (const char *name
)
3610 const bfd_mach_o_xlat_name
*x
;
3612 for (x
= bfd_mach_o_section_attribute_name
; x
->name
; x
++)
3613 if (strcmp (x
->name
, name
) == 0)
3615 return (unsigned int)-1;
3619 bfd_mach_o_core_fetch_environment (bfd
*abfd
,
3620 unsigned char **rbuf
,
3623 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
3624 unsigned long stackaddr
= bfd_mach_o_stack_addr (mdata
->header
.cputype
);
3627 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
3629 bfd_mach_o_load_command
*cur
= &mdata
->commands
[i
];
3630 bfd_mach_o_segment_command
*seg
= NULL
;
3632 if (cur
->type
!= BFD_MACH_O_LC_SEGMENT
)
3635 seg
= &cur
->command
.segment
;
3637 if ((seg
->vmaddr
+ seg
->vmsize
) == stackaddr
)
3639 unsigned long start
= seg
->fileoff
;
3640 unsigned long end
= seg
->fileoff
+ seg
->filesize
;
3641 unsigned char *buf
= bfd_malloc (1024);
3642 unsigned long size
= 1024;
3646 bfd_size_type nread
= 0;
3647 unsigned long offset
;
3648 int found_nonnull
= 0;
3650 if (size
> (end
- start
))
3651 size
= (end
- start
);
3653 buf
= bfd_realloc_or_free (buf
, size
);
3657 if (bfd_seek (abfd
, end
- size
, SEEK_SET
) != 0)
3663 nread
= bfd_bread (buf
, size
, abfd
);
3671 for (offset
= 4; offset
<= size
; offset
+= 4)
3675 val
= *((unsigned long *) (buf
+ size
- offset
));
3676 if (! found_nonnull
)
3681 else if (val
== 0x0)
3683 unsigned long bottom
;
3686 bottom
= seg
->fileoff
+ seg
->filesize
- offset
;
3687 top
= seg
->fileoff
+ seg
->filesize
- 4;
3688 *rbuf
= bfd_malloc (top
- bottom
);
3689 *rlen
= top
- bottom
;
3691 memcpy (*rbuf
, buf
+ size
- *rlen
, *rlen
);
3697 if (size
== (end
- start
))
3711 bfd_mach_o_core_file_failing_command (bfd
*abfd
)
3713 unsigned char *buf
= NULL
;
3714 unsigned int len
= 0;
3717 ret
= bfd_mach_o_core_fetch_environment (abfd
, &buf
, &len
);
3721 return (char *) buf
;
3725 bfd_mach_o_core_file_failing_signal (bfd
*abfd ATTRIBUTE_UNUSED
)
3731 bfd_mach_o_find_nearest_line (bfd
*abfd
,
3735 const char **filename_ptr
,
3736 const char **functionname_ptr
,
3737 unsigned int *line_ptr
)
3739 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
3740 /* TODO: Handle executables and dylibs by using dSYMs. */
3741 if (mdata
->header
.filetype
!= BFD_MACH_O_MH_OBJECT
)
3743 if (_bfd_dwarf2_find_nearest_line (abfd
, dwarf_debug_sections
,
3744 section
, symbols
, offset
,
3745 filename_ptr
, functionname_ptr
,
3747 &mdata
->dwarf2_find_line_info
))
3753 bfd_mach_o_close_and_cleanup (bfd
*abfd
)
3755 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
3756 if (bfd_get_format (abfd
) == bfd_object
&& mdata
!= NULL
)
3757 _bfd_dwarf2_cleanup_debug_info (abfd
, &mdata
->dwarf2_find_line_info
);
3759 return _bfd_generic_close_and_cleanup (abfd
);
3762 #define bfd_mach_o_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
3763 #define bfd_mach_o_bfd_reloc_name_lookup _bfd_norelocs_bfd_reloc_name_lookup
3765 #define bfd_mach_o_swap_reloc_in NULL
3766 #define bfd_mach_o_swap_reloc_out NULL
3767 #define bfd_mach_o_print_thread NULL
3768 #define bfd_mach_o_tgt_seg_table NULL
3770 #define TARGET_NAME mach_o_be_vec
3771 #define TARGET_STRING "mach-o-be"
3772 #define TARGET_ARCHITECTURE bfd_arch_unknown
3773 #define TARGET_BIG_ENDIAN 1
3774 #define TARGET_ARCHIVE 0
3775 #include "mach-o-target.c"
3778 #undef TARGET_STRING
3779 #undef TARGET_ARCHITECTURE
3780 #undef TARGET_BIG_ENDIAN
3781 #undef TARGET_ARCHIVE
3783 #define TARGET_NAME mach_o_le_vec
3784 #define TARGET_STRING "mach-o-le"
3785 #define TARGET_ARCHITECTURE bfd_arch_unknown
3786 #define TARGET_BIG_ENDIAN 0
3787 #define TARGET_ARCHIVE 0
3789 #include "mach-o-target.c"
3792 #undef TARGET_STRING
3793 #undef TARGET_ARCHITECTURE
3794 #undef TARGET_BIG_ENDIAN
3795 #undef TARGET_ARCHIVE
3797 /* Not yet handled: creating an archive. */
3798 #define bfd_mach_o_mkarchive _bfd_noarchive_mkarchive
3801 #define bfd_mach_o_read_ar_hdr _bfd_noarchive_read_ar_hdr
3802 #define bfd_mach_o_write_ar_hdr _bfd_noarchive_write_ar_hdr
3803 #define bfd_mach_o_slurp_armap _bfd_noarchive_slurp_armap
3804 #define bfd_mach_o_slurp_extended_name_table _bfd_noarchive_slurp_extended_name_table
3805 #define bfd_mach_o_construct_extended_name_table _bfd_noarchive_construct_extended_name_table
3806 #define bfd_mach_o_truncate_arname _bfd_noarchive_truncate_arname
3807 #define bfd_mach_o_write_armap _bfd_noarchive_write_armap
3808 #define bfd_mach_o_get_elt_at_index _bfd_noarchive_get_elt_at_index
3809 #define bfd_mach_o_generic_stat_arch_elt _bfd_noarchive_generic_stat_arch_elt
3810 #define bfd_mach_o_update_armap_timestamp _bfd_noarchive_update_armap_timestamp
3812 #define TARGET_NAME mach_o_fat_vec
3813 #define TARGET_STRING "mach-o-fat"
3814 #define TARGET_ARCHITECTURE bfd_arch_unknown
3815 #define TARGET_BIG_ENDIAN 1
3816 #define TARGET_ARCHIVE 1
3818 #include "mach-o-target.c"
3821 #undef TARGET_STRING
3822 #undef TARGET_ARCHITECTURE
3823 #undef TARGET_BIG_ENDIAN
3824 #undef TARGET_ARCHIVE