1 /* Mach-O support for BFD.
2 Copyright (C) 1999-2020 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
25 #include "libiberty.h"
27 #include "aout/stab_gnu.h"
28 #include "mach-o/reloc.h"
29 #include "mach-o/external.h"
34 #define bfd_mach_o_object_p bfd_mach_o_gen_object_p
35 #define bfd_mach_o_core_p bfd_mach_o_gen_core_p
36 #define bfd_mach_o_mkobject bfd_mach_o_gen_mkobject
38 #define FILE_ALIGN(off, algn) \
39 (((off) + ((file_ptr) 1 << (algn)) - 1) & ((file_ptr) -1U << (algn)))
42 bfd_mach_o_read_dyld_content (bfd
*abfd
, bfd_mach_o_dyld_info_command
*cmd
);
45 bfd_mach_o_version (bfd
*abfd
)
47 bfd_mach_o_data_struct
*mdata
= NULL
;
49 BFD_ASSERT (bfd_mach_o_valid (abfd
));
50 mdata
= bfd_mach_o_get_data (abfd
);
52 return mdata
->header
.version
;
56 bfd_mach_o_valid (bfd
*abfd
)
58 if (abfd
== NULL
|| abfd
->xvec
== NULL
)
61 if (abfd
->xvec
->flavour
!= bfd_target_mach_o_flavour
)
64 if (bfd_mach_o_get_data (abfd
) == NULL
)
69 static INLINE bfd_boolean
70 mach_o_wide_p (bfd_mach_o_header
*header
)
72 switch (header
->version
)
84 static INLINE bfd_boolean
85 bfd_mach_o_wide_p (bfd
*abfd
)
87 return mach_o_wide_p (&bfd_mach_o_get_data (abfd
)->header
);
90 /* Tables to translate well known Mach-O segment/section names to bfd
91 names. Use of canonical names (such as .text or .debug_frame) is required
95 static const mach_o_section_name_xlat text_section_names_xlat
[] =
98 SEC_CODE
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
99 BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS
, 0},
100 { ".const", "__const",
101 SEC_READONLY
| SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
102 BFD_MACH_O_S_ATTR_NONE
, 0},
103 { ".static_const", "__static_const",
104 SEC_READONLY
| SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
105 BFD_MACH_O_S_ATTR_NONE
, 0},
106 { ".cstring", "__cstring",
107 SEC_READONLY
| SEC_DATA
| SEC_LOAD
| SEC_MERGE
| SEC_STRINGS
,
108 BFD_MACH_O_S_CSTRING_LITERALS
,
109 BFD_MACH_O_S_ATTR_NONE
, 0},
110 { ".literal4", "__literal4",
111 SEC_READONLY
| SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_4BYTE_LITERALS
,
112 BFD_MACH_O_S_ATTR_NONE
, 2},
113 { ".literal8", "__literal8",
114 SEC_READONLY
| SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_8BYTE_LITERALS
,
115 BFD_MACH_O_S_ATTR_NONE
, 3},
116 { ".literal16", "__literal16",
117 SEC_READONLY
| SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_16BYTE_LITERALS
,
118 BFD_MACH_O_S_ATTR_NONE
, 4},
119 { ".constructor", "__constructor",
120 SEC_CODE
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
121 BFD_MACH_O_S_ATTR_NONE
, 0},
122 { ".destructor", "__destructor",
123 SEC_CODE
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
124 BFD_MACH_O_S_ATTR_NONE
, 0},
125 { ".eh_frame", "__eh_frame",
126 SEC_READONLY
| SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_COALESCED
,
127 BFD_MACH_O_S_ATTR_LIVE_SUPPORT
128 | BFD_MACH_O_S_ATTR_STRIP_STATIC_SYMS
129 | BFD_MACH_O_S_ATTR_NO_TOC
, 2},
130 { NULL
, NULL
, 0, 0, 0, 0}
133 /* __DATA Segment. */
134 static const mach_o_section_name_xlat data_section_names_xlat
[] =
137 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
138 BFD_MACH_O_S_ATTR_NONE
, 0},
140 SEC_NO_FLAGS
, BFD_MACH_O_S_ZEROFILL
,
141 BFD_MACH_O_S_ATTR_NONE
, 0},
142 { ".const_data", "__const",
143 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
144 BFD_MACH_O_S_ATTR_NONE
, 0},
145 { ".static_data", "__static_data",
146 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
147 BFD_MACH_O_S_ATTR_NONE
, 0},
148 { ".mod_init_func", "__mod_init_func",
149 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_MOD_INIT_FUNC_POINTERS
,
150 BFD_MACH_O_S_ATTR_NONE
, 2},
151 { ".mod_term_func", "__mod_term_func",
152 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_MOD_FINI_FUNC_POINTERS
,
153 BFD_MACH_O_S_ATTR_NONE
, 2},
155 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
156 BFD_MACH_O_S_ATTR_NONE
, 0},
157 { ".cfstring", "__cfstring",
158 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
159 BFD_MACH_O_S_ATTR_NONE
, 2},
160 { NULL
, NULL
, 0, 0, 0, 0}
163 /* __DWARF Segment. */
164 static const mach_o_section_name_xlat dwarf_section_names_xlat
[] =
166 { ".debug_frame", "__debug_frame",
167 SEC_DEBUGGING
, BFD_MACH_O_S_REGULAR
,
168 BFD_MACH_O_S_ATTR_DEBUG
, 0},
169 { ".debug_info", "__debug_info",
170 SEC_DEBUGGING
, BFD_MACH_O_S_REGULAR
,
171 BFD_MACH_O_S_ATTR_DEBUG
, 0},
172 { ".debug_abbrev", "__debug_abbrev",
173 SEC_DEBUGGING
, BFD_MACH_O_S_REGULAR
,
174 BFD_MACH_O_S_ATTR_DEBUG
, 0},
175 { ".debug_aranges", "__debug_aranges",
176 SEC_DEBUGGING
, BFD_MACH_O_S_REGULAR
,
177 BFD_MACH_O_S_ATTR_DEBUG
, 0},
178 { ".debug_macinfo", "__debug_macinfo",
179 SEC_DEBUGGING
, BFD_MACH_O_S_REGULAR
,
180 BFD_MACH_O_S_ATTR_DEBUG
, 0},
181 { ".debug_line", "__debug_line",
182 SEC_DEBUGGING
, BFD_MACH_O_S_REGULAR
,
183 BFD_MACH_O_S_ATTR_DEBUG
, 0},
184 { ".debug_loc", "__debug_loc",
185 SEC_DEBUGGING
, BFD_MACH_O_S_REGULAR
,
186 BFD_MACH_O_S_ATTR_DEBUG
, 0},
187 { ".debug_pubnames", "__debug_pubnames",
188 SEC_DEBUGGING
, BFD_MACH_O_S_REGULAR
,
189 BFD_MACH_O_S_ATTR_DEBUG
, 0},
190 { ".debug_pubtypes", "__debug_pubtypes",
191 SEC_DEBUGGING
, BFD_MACH_O_S_REGULAR
,
192 BFD_MACH_O_S_ATTR_DEBUG
, 0},
193 { ".debug_str", "__debug_str",
194 SEC_DEBUGGING
, BFD_MACH_O_S_REGULAR
,
195 BFD_MACH_O_S_ATTR_DEBUG
, 0},
196 { ".debug_ranges", "__debug_ranges",
197 SEC_DEBUGGING
, BFD_MACH_O_S_REGULAR
,
198 BFD_MACH_O_S_ATTR_DEBUG
, 0},
199 { ".debug_macro", "__debug_macro",
200 SEC_DEBUGGING
, BFD_MACH_O_S_REGULAR
,
201 BFD_MACH_O_S_ATTR_DEBUG
, 0},
202 { ".debug_gdb_scripts", "__debug_gdb_scri",
203 SEC_DEBUGGING
, BFD_MACH_O_S_REGULAR
,
204 BFD_MACH_O_S_ATTR_DEBUG
, 0},
205 { NULL
, NULL
, 0, 0, 0, 0}
208 /* __OBJC Segment. */
209 static const mach_o_section_name_xlat objc_section_names_xlat
[] =
211 { ".objc_class", "__class",
212 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
213 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP
, 0},
214 { ".objc_meta_class", "__meta_class",
215 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
216 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP
, 0},
217 { ".objc_cat_cls_meth", "__cat_cls_meth",
218 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
219 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP
, 0},
220 { ".objc_cat_inst_meth", "__cat_inst_meth",
221 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
222 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP
, 0},
223 { ".objc_protocol", "__protocol",
224 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
225 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP
, 0},
226 { ".objc_string_object", "__string_object",
227 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
228 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP
, 0},
229 { ".objc_cls_meth", "__cls_meth",
230 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
231 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP
, 0},
232 { ".objc_inst_meth", "__inst_meth",
233 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
234 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP
, 0},
235 { ".objc_cls_refs", "__cls_refs",
236 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_LITERAL_POINTERS
,
237 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP
, 0},
238 { ".objc_message_refs", "__message_refs",
239 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_LITERAL_POINTERS
,
240 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP
, 0},
241 { ".objc_symbols", "__symbols",
242 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
243 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP
, 0},
244 { ".objc_category", "__category",
245 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
246 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP
, 0},
247 { ".objc_class_vars", "__class_vars",
248 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
249 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP
, 0},
250 { ".objc_instance_vars", "__instance_vars",
251 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
252 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP
, 0},
253 { ".objc_module_info", "__module_info",
254 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
255 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP
, 0},
256 { ".objc_selector_strs", "__selector_strs",
257 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_CSTRING_LITERALS
,
258 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP
, 0},
259 { ".objc_image_info", "__image_info",
260 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
261 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP
, 0},
262 { ".objc_selector_fixup", "__sel_fixup",
263 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
264 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP
, 0},
266 { ".objc1_class_ext", "__class_ext",
267 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
268 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP
, 0},
269 { ".objc1_property_list", "__property",
270 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
271 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP
, 0},
272 { ".objc1_protocol_ext", "__protocol_ext",
273 SEC_DATA
| SEC_LOAD
, BFD_MACH_O_S_REGULAR
,
274 BFD_MACH_O_S_ATTR_NO_DEAD_STRIP
, 0},
275 { NULL
, NULL
, 0, 0, 0, 0}
278 static const mach_o_segment_name_xlat segsec_names_xlat
[] =
280 { "__TEXT", text_section_names_xlat
},
281 { "__DATA", data_section_names_xlat
},
282 { "__DWARF", dwarf_section_names_xlat
},
283 { "__OBJC", objc_section_names_xlat
},
287 static const char dsym_subdir
[] = ".dSYM/Contents/Resources/DWARF";
289 /* For both cases bfd-name => mach-o name and vice versa, the specific target
290 is checked before the generic. This allows a target (e.g. ppc for cstring)
291 to override the generic definition with a more specific one. */
293 /* Fetch the translation from a Mach-O section designation (segment, section)
294 as a bfd short name, if one exists. Otherwise return NULL.
296 Allow the segment and section names to be unterminated 16 byte arrays. */
298 const mach_o_section_name_xlat
*
299 bfd_mach_o_section_data_for_mach_sect (bfd
*abfd
, const char *segname
,
300 const char *sectname
)
302 const struct mach_o_segment_name_xlat
*seg
;
303 const mach_o_section_name_xlat
*sec
;
304 bfd_mach_o_backend_data
*bed
= bfd_mach_o_get_backend_data (abfd
);
306 /* First try any target-specific translations defined... */
307 if (bed
->segsec_names_xlat
)
308 for (seg
= bed
->segsec_names_xlat
; seg
->segname
; seg
++)
309 if (strncmp (seg
->segname
, segname
, BFD_MACH_O_SEGNAME_SIZE
) == 0)
310 for (sec
= seg
->sections
; sec
->mach_o_name
; sec
++)
311 if (strncmp (sec
->mach_o_name
, sectname
,
312 BFD_MACH_O_SECTNAME_SIZE
) == 0)
315 /* ... and then the Mach-O generic ones. */
316 for (seg
= segsec_names_xlat
; seg
->segname
; seg
++)
317 if (strncmp (seg
->segname
, segname
, BFD_MACH_O_SEGNAME_SIZE
) == 0)
318 for (sec
= seg
->sections
; sec
->mach_o_name
; sec
++)
319 if (strncmp (sec
->mach_o_name
, sectname
,
320 BFD_MACH_O_SECTNAME_SIZE
) == 0)
326 /* If the bfd_name for this section is a 'canonical' form for which we
327 know the Mach-O data, return the segment name and the data for the
328 Mach-O equivalent. Otherwise return NULL. */
330 const mach_o_section_name_xlat
*
331 bfd_mach_o_section_data_for_bfd_name (bfd
*abfd
, const char *bfd_name
,
332 const char **segname
)
334 const struct mach_o_segment_name_xlat
*seg
;
335 const mach_o_section_name_xlat
*sec
;
336 bfd_mach_o_backend_data
*bed
= bfd_mach_o_get_backend_data (abfd
);
339 if (bfd_name
[0] != '.')
342 /* First try any target-specific translations defined... */
343 if (bed
->segsec_names_xlat
)
344 for (seg
= bed
->segsec_names_xlat
; seg
->segname
; seg
++)
345 for (sec
= seg
->sections
; sec
->bfd_name
; sec
++)
346 if (strcmp (bfd_name
, sec
->bfd_name
) == 0)
348 *segname
= seg
->segname
;
352 /* ... and then the Mach-O generic ones. */
353 for (seg
= segsec_names_xlat
; seg
->segname
; seg
++)
354 for (sec
= seg
->sections
; sec
->bfd_name
; sec
++)
355 if (strcmp (bfd_name
, sec
->bfd_name
) == 0)
357 *segname
= seg
->segname
;
364 /* Convert Mach-O section name to BFD.
366 Try to use standard/canonical names, for which we have tables including
367 default flag settings - which are returned. Otherwise forge a new name
368 in the form "<segmentname>.<sectionname>" this will be prefixed with
369 LC_SEGMENT. if the segment name does not begin with an underscore.
371 SEGNAME and SECTNAME are 16 byte arrays (they do not need to be NUL-
372 terminated if the name length is exactly 16 bytes - but must be if the name
373 length is less than 16 characters). */
376 bfd_mach_o_convert_section_name_to_bfd (bfd
*abfd
, const char *segname
,
377 const char *secname
, const char **name
,
380 const mach_o_section_name_xlat
*xlat
;
383 const char *pfx
= "";
386 *flags
= SEC_NO_FLAGS
;
388 /* First search for a canonical name...
389 xlat will be non-null if there is an entry for segname, secname. */
390 xlat
= bfd_mach_o_section_data_for_mach_sect (abfd
, segname
, secname
);
393 len
= strlen (xlat
->bfd_name
);
394 res
= bfd_alloc (abfd
, len
+ 1);
397 memcpy (res
, xlat
->bfd_name
, len
+1);
399 *flags
= xlat
->bfd_flags
;
403 /* ... else we make up a bfd name from the segment concatenated with the
406 len
= 16 + 1 + 16 + 1;
408 /* Put "LC_SEGMENT." prefix if the segment name is weird (ie doesn't start
409 with an underscore. */
410 if (segname
[0] != '_')
412 static const char seg_pfx
[] = "LC_SEGMENT.";
415 len
+= sizeof (seg_pfx
) - 1;
418 res
= bfd_alloc (abfd
, len
);
421 snprintf (res
, len
, "%s%.16s.%.16s", pfx
, segname
, secname
);
425 /* Convert a bfd section name to a Mach-O segment + section name.
427 If the name is a canonical one for which we have a Darwin match
428 return the translation table - which contains defaults for flags,
429 type, attribute and default alignment data.
431 Otherwise, expand the bfd_name (assumed to be in the form
432 "[LC_SEGMENT.]<segmentname>.<sectionname>") and return NULL. */
434 static const mach_o_section_name_xlat
*
435 bfd_mach_o_convert_section_name_to_mach_o (bfd
*abfd ATTRIBUTE_UNUSED
,
437 bfd_mach_o_section
*section
)
439 const mach_o_section_name_xlat
*xlat
;
440 const char *name
= bfd_section_name (sect
);
447 memset (section
->segname
, 0, BFD_MACH_O_SEGNAME_SIZE
+ 1);
448 memset (section
->sectname
, 0, BFD_MACH_O_SECTNAME_SIZE
+ 1);
450 /* See if is a canonical name ... */
451 xlat
= bfd_mach_o_section_data_for_bfd_name (abfd
, name
, &segname
);
454 strcpy (section
->segname
, segname
);
455 strcpy (section
->sectname
, xlat
->mach_o_name
);
459 /* .. else we convert our constructed one back to Mach-O.
460 Strip LC_SEGMENT. prefix, if present. */
461 if (strncmp (name
, "LC_SEGMENT.", 11) == 0)
465 dot
= strchr (name
, '.');
468 /* Try to split name into segment and section names. */
469 if (dot
&& dot
!= name
)
472 seclen
= len
- (dot
+ 1 - name
);
474 if (seglen
<= BFD_MACH_O_SEGNAME_SIZE
475 && seclen
<= BFD_MACH_O_SECTNAME_SIZE
)
477 memcpy (section
->segname
, name
, seglen
);
478 section
->segname
[seglen
] = 0;
479 memcpy (section
->sectname
, dot
+ 1, seclen
);
480 section
->sectname
[seclen
] = 0;
485 /* The segment and section names are both missing - don't make them
487 if (dot
&& dot
== name
)
490 /* Just duplicate the name into both segment and section. */
493 memcpy (section
->segname
, name
, len
);
494 section
->segname
[len
] = 0;
495 memcpy (section
->sectname
, name
, len
);
496 section
->sectname
[len
] = 0;
500 /* Return the size of an entry for section SEC.
501 Must be called only for symbol pointer section and symbol stubs
505 bfd_mach_o_section_get_entry_size (bfd
*abfd
, bfd_mach_o_section
*sec
)
507 switch (sec
->flags
& BFD_MACH_O_SECTION_TYPE_MASK
)
509 case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS
:
510 case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS
:
511 return bfd_mach_o_wide_p (abfd
) ? 8 : 4;
512 case BFD_MACH_O_S_SYMBOL_STUBS
:
513 return sec
->reserved2
;
520 /* Return the number of indirect symbols for a section.
521 Must be called only for symbol pointer section and symbol stubs
525 bfd_mach_o_section_get_nbr_indirect (bfd
*abfd
, bfd_mach_o_section
*sec
)
529 elsz
= bfd_mach_o_section_get_entry_size (abfd
, sec
);
533 return sec
->size
/ elsz
;
536 /* Append command CMD to ABFD. Note that header.ncmds is not updated. */
539 bfd_mach_o_append_command (bfd
*abfd
, bfd_mach_o_load_command
*cmd
)
541 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
543 if (mdata
->last_command
!= NULL
)
544 mdata
->last_command
->next
= cmd
;
546 mdata
->first_command
= cmd
;
547 mdata
->last_command
= cmd
;
551 /* Copy any private info we understand from the input symbol
552 to the output symbol. */
555 bfd_mach_o_bfd_copy_private_symbol_data (bfd
*ibfd ATTRIBUTE_UNUSED
,
557 bfd
*obfd ATTRIBUTE_UNUSED
,
560 bfd_mach_o_asymbol
*os
, *is
;
562 os
= (bfd_mach_o_asymbol
*)osymbol
;
563 is
= (bfd_mach_o_asymbol
*)isymbol
;
564 os
->n_type
= is
->n_type
;
565 os
->n_sect
= is
->n_sect
;
566 os
->n_desc
= is
->n_desc
;
567 os
->symbol
.udata
.i
= is
->symbol
.udata
.i
;
572 /* Copy any private info we understand from the input section
573 to the output section. */
576 bfd_mach_o_bfd_copy_private_section_data (bfd
*ibfd
, asection
*isection
,
577 bfd
*obfd
, asection
*osection
)
579 bfd_mach_o_section
*os
= bfd_mach_o_get_mach_o_section (osection
);
580 bfd_mach_o_section
*is
= bfd_mach_o_get_mach_o_section (isection
);
582 if (ibfd
->xvec
->flavour
!= bfd_target_mach_o_flavour
583 || obfd
->xvec
->flavour
!= bfd_target_mach_o_flavour
)
586 BFD_ASSERT (is
!= NULL
&& os
!= NULL
);
588 os
->flags
= is
->flags
;
589 os
->reserved1
= is
->reserved1
;
590 os
->reserved2
= is
->reserved2
;
591 os
->reserved3
= is
->reserved3
;
597 cputype (unsigned long value
)
601 case BFD_MACH_O_CPU_TYPE_VAX
: return "VAX";
602 case BFD_MACH_O_CPU_TYPE_MC680x0
: return "MC68k";
603 case BFD_MACH_O_CPU_TYPE_I386
: return "I386";
604 case BFD_MACH_O_CPU_TYPE_MIPS
: return "MIPS";
605 case BFD_MACH_O_CPU_TYPE_MC98000
: return "MC98k";
606 case BFD_MACH_O_CPU_TYPE_HPPA
: return "HPPA";
607 case BFD_MACH_O_CPU_TYPE_ARM
: return "ARM";
608 case BFD_MACH_O_CPU_TYPE_MC88000
: return "MC88K";
609 case BFD_MACH_O_CPU_TYPE_SPARC
: return "SPARC";
610 case BFD_MACH_O_CPU_TYPE_I860
: return "I860";
611 case BFD_MACH_O_CPU_TYPE_ALPHA
: return "ALPHA";
612 case BFD_MACH_O_CPU_TYPE_POWERPC
: return "PPC";
613 case BFD_MACH_O_CPU_TYPE_POWERPC_64
: return "PPC64";
614 case BFD_MACH_O_CPU_TYPE_X86_64
: return "X86_64";
615 case BFD_MACH_O_CPU_TYPE_ARM64
: return "ARM64";
616 default: return _("<unknown>");
621 cpusubtype (unsigned long cpu_type
, unsigned long cpu_subtype
)
623 static char buffer
[128];
626 switch (cpu_subtype
& BFD_MACH_O_CPU_SUBTYPE_MASK
)
630 case BFD_MACH_O_CPU_SUBTYPE_LIB64
:
631 sprintf (buffer
, " (LIB64)"); break;
633 sprintf (buffer
, _("<unknown mask flags>")); break;
636 cpu_subtype
&= ~ BFD_MACH_O_CPU_SUBTYPE_MASK
;
640 case BFD_MACH_O_CPU_TYPE_X86_64
:
641 case BFD_MACH_O_CPU_TYPE_I386
:
644 case BFD_MACH_O_CPU_SUBTYPE_X86_ALL
:
645 return strcat (buffer
, " (X86_ALL)");
651 case BFD_MACH_O_CPU_TYPE_ARM
:
654 case BFD_MACH_O_CPU_SUBTYPE_ARM_ALL
:
655 return strcat (buffer
, " (ARM_ALL)");
656 case BFD_MACH_O_CPU_SUBTYPE_ARM_V4T
:
657 return strcat (buffer
, " (ARM_V4T)");
658 case BFD_MACH_O_CPU_SUBTYPE_ARM_V6
:
659 return strcat (buffer
, " (ARM_V6)");
660 case BFD_MACH_O_CPU_SUBTYPE_ARM_V5TEJ
:
661 return strcat (buffer
, " (ARM_V5TEJ)");
662 case BFD_MACH_O_CPU_SUBTYPE_ARM_XSCALE
:
663 return strcat (buffer
, " (ARM_XSCALE)");
664 case BFD_MACH_O_CPU_SUBTYPE_ARM_V7
:
665 return strcat (buffer
, " (ARM_V7)");
671 case BFD_MACH_O_CPU_TYPE_ARM64
:
674 case BFD_MACH_O_CPU_SUBTYPE_ARM64_ALL
:
675 return strcat (buffer
, " (ARM64_ALL)");
676 case BFD_MACH_O_CPU_SUBTYPE_ARM64_V8
:
677 return strcat (buffer
, " (ARM64_V8)");
687 if (cpu_subtype
!= 0)
688 return strcat (buffer
, _(" (<unknown>)"));
694 bfd_mach_o_bfd_print_private_bfd_data (bfd
*abfd
, void *ptr
)
696 FILE * file
= (FILE *) ptr
;
697 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
699 fprintf (file
, _(" MACH-O header:\n"));
700 fprintf (file
, _(" magic: %#lx\n"), (long) mdata
->header
.magic
);
701 fprintf (file
, _(" cputype: %#lx (%s)\n"), (long) mdata
->header
.cputype
,
702 cputype (mdata
->header
.cputype
));
703 fprintf (file
, _(" cpusubtype: %#lx%s\n"), (long) mdata
->header
.cpusubtype
,
704 cpusubtype (mdata
->header
.cputype
, mdata
->header
.cpusubtype
));
705 fprintf (file
, _(" filetype: %#lx\n"), (long) mdata
->header
.filetype
);
706 fprintf (file
, _(" ncmds: %#lx\n"), (long) mdata
->header
.ncmds
);
707 fprintf (file
, _(" sizeocmds: %#lx\n"), (long) mdata
->header
.sizeofcmds
);
708 fprintf (file
, _(" flags: %#lx\n"), (long) mdata
->header
.flags
);
709 fprintf (file
, _(" version: %x\n"), mdata
->header
.version
);
714 /* Copy any private info we understand from the input bfd
715 to the output bfd. */
718 bfd_mach_o_bfd_copy_private_header_data (bfd
*ibfd
, bfd
*obfd
)
720 bfd_mach_o_data_struct
*imdata
;
721 bfd_mach_o_data_struct
*omdata
;
722 bfd_mach_o_load_command
*icmd
;
724 if (bfd_get_flavour (ibfd
) != bfd_target_mach_o_flavour
725 || bfd_get_flavour (obfd
) != bfd_target_mach_o_flavour
)
728 BFD_ASSERT (bfd_mach_o_valid (ibfd
));
729 BFD_ASSERT (bfd_mach_o_valid (obfd
));
731 imdata
= bfd_mach_o_get_data (ibfd
);
732 omdata
= bfd_mach_o_get_data (obfd
);
734 /* Copy header flags. */
735 omdata
->header
.flags
= imdata
->header
.flags
;
737 /* PR 23299. Copy the cputype. */
738 if (imdata
->header
.cputype
!= omdata
->header
.cputype
)
740 if (omdata
->header
.cputype
== 0)
741 omdata
->header
.cputype
= imdata
->header
.cputype
;
742 else if (imdata
->header
.cputype
!= 0)
743 /* Urg - what has happened ? */
744 _bfd_error_handler (_("incompatible cputypes in mach-o files: %ld vs %ld"),
745 (long) imdata
->header
.cputype
,
746 (long) omdata
->header
.cputype
);
749 /* Copy the cpusubtype. */
750 omdata
->header
.cpusubtype
= imdata
->header
.cpusubtype
;
753 for (icmd
= imdata
->first_command
; icmd
!= NULL
; icmd
= icmd
->next
)
755 bfd_mach_o_load_command
*ocmd
;
759 case BFD_MACH_O_LC_LOAD_DYLIB
:
760 case BFD_MACH_O_LC_LOAD_DYLINKER
:
761 case BFD_MACH_O_LC_DYLD_INFO
:
762 /* Command is copied. */
763 ocmd
= bfd_alloc (obfd
, sizeof (bfd_mach_o_load_command
));
767 /* Copy common fields. */
768 ocmd
->type
= icmd
->type
;
769 ocmd
->type_required
= icmd
->type_required
;
771 ocmd
->len
= icmd
->len
;
775 /* Command is not copied. */
782 case BFD_MACH_O_LC_LOAD_DYLIB
:
784 bfd_mach_o_dylib_command
*idy
= &icmd
->command
.dylib
;
785 bfd_mach_o_dylib_command
*ody
= &ocmd
->command
.dylib
;
787 ody
->name_offset
= idy
->name_offset
;
788 ody
->timestamp
= idy
->timestamp
;
789 ody
->current_version
= idy
->current_version
;
790 ody
->compatibility_version
= idy
->compatibility_version
;
791 ody
->name_str
= idy
->name_str
;
795 case BFD_MACH_O_LC_LOAD_DYLINKER
:
797 bfd_mach_o_dylinker_command
*idy
= &icmd
->command
.dylinker
;
798 bfd_mach_o_dylinker_command
*ody
= &ocmd
->command
.dylinker
;
800 ody
->name_offset
= idy
->name_offset
;
801 ody
->name_str
= idy
->name_str
;
805 case BFD_MACH_O_LC_DYLD_INFO
:
807 bfd_mach_o_dyld_info_command
*idy
= &icmd
->command
.dyld_info
;
808 bfd_mach_o_dyld_info_command
*ody
= &ocmd
->command
.dyld_info
;
810 if (bfd_mach_o_read_dyld_content (ibfd
, idy
))
812 ody
->rebase_size
= idy
->rebase_size
;
813 ody
->rebase_content
= idy
->rebase_content
;
815 ody
->bind_size
= idy
->bind_size
;
816 ody
->bind_content
= idy
->bind_content
;
818 ody
->weak_bind_size
= idy
->weak_bind_size
;
819 ody
->weak_bind_content
= idy
->weak_bind_content
;
821 ody
->lazy_bind_size
= idy
->lazy_bind_size
;
822 ody
->lazy_bind_content
= idy
->lazy_bind_content
;
824 ody
->export_size
= idy
->export_size
;
825 ody
->export_content
= idy
->export_content
;
827 /* PR 17512L: file: 730e492d. */
832 ody
->weak_bind_size
=
833 ody
->lazy_bind_size
=
834 ody
->export_size
= 0;
835 ody
->rebase_content
=
837 ody
->weak_bind_content
=
838 ody
->lazy_bind_content
=
839 ody
->export_content
= NULL
;
845 /* That command should be handled. */
849 /* Insert command. */
850 bfd_mach_o_append_command (obfd
, ocmd
);
856 /* This allows us to set up to 32 bits of flags (unless we invent some
857 fiendish scheme to subdivide). For now, we'll just set the file flags
858 without error checking - just overwrite. */
861 bfd_mach_o_bfd_set_private_flags (bfd
*abfd
, flagword flags
)
863 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
868 mdata
->header
.flags
= flags
;
872 /* Count the total number of symbols. */
875 bfd_mach_o_count_symbols (bfd
*abfd
)
877 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
879 if (mdata
->symtab
== NULL
)
881 return mdata
->symtab
->nsyms
;
885 bfd_mach_o_get_symtab_upper_bound (bfd
*abfd
)
887 long nsyms
= bfd_mach_o_count_symbols (abfd
);
889 return ((nsyms
+ 1) * sizeof (asymbol
*));
893 bfd_mach_o_canonicalize_symtab (bfd
*abfd
, asymbol
**alocation
)
895 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
896 long nsyms
= bfd_mach_o_count_symbols (abfd
);
897 bfd_mach_o_symtab_command
*sym
= mdata
->symtab
;
905 /* Do not try to read symbols if there are none. */
910 if (!bfd_mach_o_read_symtab_symbols (abfd
))
913 (_("bfd_mach_o_canonicalize_symtab: unable to load symbols"));
917 BFD_ASSERT (sym
->symbols
!= NULL
);
919 for (j
= 0; j
< sym
->nsyms
; j
++)
920 alocation
[j
] = &sym
->symbols
[j
].symbol
;
927 /* Create synthetic symbols for indirect symbols. */
930 bfd_mach_o_get_synthetic_symtab (bfd
*abfd
,
931 long symcount ATTRIBUTE_UNUSED
,
932 asymbol
**syms ATTRIBUTE_UNUSED
,
933 long dynsymcount ATTRIBUTE_UNUSED
,
934 asymbol
**dynsyms ATTRIBUTE_UNUSED
,
937 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
938 bfd_mach_o_dysymtab_command
*dysymtab
= mdata
->dysymtab
;
939 bfd_mach_o_symtab_command
*symtab
= mdata
->symtab
;
943 unsigned long count
, i
, j
, n
;
947 const char stub
[] = "$stub";
951 /* Stop now if no symbols or no indirect symbols. */
952 if (dysymtab
== NULL
|| dysymtab
->nindirectsyms
== 0
953 || symtab
== NULL
|| symtab
->symbols
== NULL
)
956 /* We need to allocate a bfd symbol for every indirect symbol and to
957 allocate the memory for its name. */
958 count
= dysymtab
->nindirectsyms
;
959 size
= count
* sizeof (asymbol
) + 1;
961 for (j
= 0; j
< count
; j
++)
964 unsigned int isym
= dysymtab
->indirect_syms
[j
];
966 /* Some indirect symbols are anonymous. */
967 if (isym
< symtab
->nsyms
&& (strng
= symtab
->symbols
[isym
].symbol
.name
))
968 /* PR 17512: file: f5b8eeba. */
969 size
+= strnlen (strng
, symtab
->strsize
- (strng
- symtab
->strtab
)) + sizeof (stub
);
972 s_start
= bfd_malloc (size
);
973 s
= *ret
= (asymbol
*) s_start
;
976 names
= (char *) (s
+ count
);
979 s_end
= s_start
+ size
;
982 for (i
= 0; i
< mdata
->nsects
; i
++)
984 bfd_mach_o_section
*sec
= mdata
->sections
[i
];
985 unsigned int first
, last
;
989 switch (sec
->flags
& BFD_MACH_O_SECTION_TYPE_MASK
)
991 case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS
:
992 case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS
:
993 case BFD_MACH_O_S_SYMBOL_STUBS
:
994 /* Only these sections have indirect symbols. */
995 first
= sec
->reserved1
;
996 last
= first
+ bfd_mach_o_section_get_nbr_indirect (abfd
, sec
);
998 entry_size
= bfd_mach_o_section_get_entry_size (abfd
, sec
);
1000 /* PR 17512: file: 08e15eec. */
1001 if (first
>= count
|| last
>= count
|| first
> last
)
1004 for (j
= first
; j
< last
; j
++)
1006 unsigned int isym
= dysymtab
->indirect_syms
[j
];
1008 /* PR 17512: file: 04d64d9b. */
1009 if (((char *) s
) + sizeof (* s
) > s_end
)
1012 s
->flags
= BSF_GLOBAL
| BSF_SYNTHETIC
;
1013 s
->section
= sec
->bfdsection
;
1014 s
->value
= addr
- sec
->addr
;
1017 if (isym
< symtab
->nsyms
1018 && symtab
->symbols
[isym
].symbol
.name
)
1020 const char *sym
= symtab
->symbols
[isym
].symbol
.name
;
1025 /* PR 17512: file: 47dfd4d2. */
1026 if (names
+ len
>= s_end
)
1028 memcpy (names
, sym
, len
);
1030 /* PR 17512: file: 18f340a4. */
1031 if (names
+ sizeof (stub
) >= s_end
)
1033 memcpy (names
, stub
, sizeof (stub
));
1034 names
+= sizeof (stub
);
1058 bfd_mach_o_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
1062 bfd_symbol_info (symbol
, ret
);
1066 bfd_mach_o_print_symbol (bfd
*abfd
,
1069 bfd_print_symbol_type how
)
1071 FILE *file
= (FILE *) afile
;
1073 bfd_mach_o_asymbol
*asym
= (bfd_mach_o_asymbol
*)symbol
;
1077 case bfd_print_symbol_name
:
1078 fprintf (file
, "%s", symbol
->name
);
1081 bfd_print_symbol_vandf (abfd
, (void *) file
, symbol
);
1082 if (asym
->n_type
& BFD_MACH_O_N_STAB
)
1083 name
= bfd_get_stab_name (asym
->n_type
);
1085 switch (asym
->n_type
& BFD_MACH_O_N_TYPE
)
1087 case BFD_MACH_O_N_UNDF
:
1088 if (symbol
->value
== 0)
1093 case BFD_MACH_O_N_ABS
:
1096 case BFD_MACH_O_N_INDR
:
1099 case BFD_MACH_O_N_PBUD
:
1102 case BFD_MACH_O_N_SECT
:
1111 fprintf (file
, " %02x %-6s %02x %04x",
1112 asym
->n_type
, name
, asym
->n_sect
, asym
->n_desc
);
1113 if ((asym
->n_type
& BFD_MACH_O_N_STAB
) == 0
1114 && (asym
->n_type
& BFD_MACH_O_N_TYPE
) == BFD_MACH_O_N_SECT
)
1115 fprintf (file
, " [%s]", symbol
->section
->name
);
1116 fprintf (file
, " %s", symbol
->name
);
1121 bfd_mach_o_convert_architecture (bfd_mach_o_cpu_type mtype
,
1122 bfd_mach_o_cpu_subtype msubtype
,
1123 enum bfd_architecture
*type
,
1124 unsigned long *subtype
)
1126 *subtype
= bfd_arch_unknown
;
1130 case BFD_MACH_O_CPU_TYPE_VAX
:
1131 *type
= bfd_arch_vax
;
1133 case BFD_MACH_O_CPU_TYPE_MC680x0
:
1134 *type
= bfd_arch_m68k
;
1136 case BFD_MACH_O_CPU_TYPE_I386
:
1137 *type
= bfd_arch_i386
;
1138 *subtype
= bfd_mach_i386_i386
;
1140 case BFD_MACH_O_CPU_TYPE_X86_64
:
1141 *type
= bfd_arch_i386
;
1142 *subtype
= bfd_mach_x86_64
;
1144 case BFD_MACH_O_CPU_TYPE_MIPS
:
1145 *type
= bfd_arch_mips
;
1147 case BFD_MACH_O_CPU_TYPE_MC98000
:
1148 *type
= bfd_arch_m98k
;
1150 case BFD_MACH_O_CPU_TYPE_HPPA
:
1151 *type
= bfd_arch_hppa
;
1153 case BFD_MACH_O_CPU_TYPE_ARM
:
1154 *type
= bfd_arch_arm
;
1157 case BFD_MACH_O_CPU_SUBTYPE_ARM_V4T
:
1158 *subtype
= bfd_mach_arm_4T
;
1160 case BFD_MACH_O_CPU_SUBTYPE_ARM_V6
:
1161 *subtype
= bfd_mach_arm_4T
; /* Best fit ? */
1163 case BFD_MACH_O_CPU_SUBTYPE_ARM_V5TEJ
:
1164 *subtype
= bfd_mach_arm_5TE
;
1166 case BFD_MACH_O_CPU_SUBTYPE_ARM_XSCALE
:
1167 *subtype
= bfd_mach_arm_XScale
;
1169 case BFD_MACH_O_CPU_SUBTYPE_ARM_V7
:
1170 *subtype
= bfd_mach_arm_5TE
; /* Best fit ? */
1172 case BFD_MACH_O_CPU_SUBTYPE_ARM_ALL
:
1177 case BFD_MACH_O_CPU_TYPE_SPARC
:
1178 *type
= bfd_arch_sparc
;
1179 *subtype
= bfd_mach_sparc
;
1181 case BFD_MACH_O_CPU_TYPE_ALPHA
:
1182 *type
= bfd_arch_alpha
;
1184 case BFD_MACH_O_CPU_TYPE_POWERPC
:
1185 *type
= bfd_arch_powerpc
;
1186 *subtype
= bfd_mach_ppc
;
1188 case BFD_MACH_O_CPU_TYPE_POWERPC_64
:
1189 *type
= bfd_arch_powerpc
;
1190 *subtype
= bfd_mach_ppc64
;
1192 case BFD_MACH_O_CPU_TYPE_ARM64
:
1193 *type
= bfd_arch_aarch64
;
1194 *subtype
= bfd_mach_aarch64
;
1197 *type
= bfd_arch_unknown
;
1202 /* Write n NUL bytes to ABFD so that LEN + n is a multiple of 4. Return the
1203 number of bytes written or -1 in case of error. */
1206 bfd_mach_o_pad4 (bfd
*abfd
, unsigned int len
)
1210 char pad
[4] = {0,0,0,0};
1211 unsigned int padlen
= 4 - (len
% 4);
1213 if (bfd_bwrite (pad
, padlen
, abfd
) != padlen
)
1222 /* Likewise, but for a command. */
1225 bfd_mach_o_pad_command (bfd
*abfd
, unsigned int len
)
1227 unsigned int align
= bfd_mach_o_wide_p (abfd
) ? 8 : 4;
1229 if (len
% align
!= 0)
1232 unsigned int padlen
= align
- (len
% align
);
1234 if (bfd_bwrite (pad
, padlen
, abfd
) != padlen
)
1244 bfd_mach_o_write_header (bfd
*abfd
, bfd_mach_o_header
*header
)
1246 struct mach_o_header_external raw
;
1249 size
= mach_o_wide_p (header
) ?
1250 BFD_MACH_O_HEADER_64_SIZE
: BFD_MACH_O_HEADER_SIZE
;
1252 bfd_h_put_32 (abfd
, header
->magic
, raw
.magic
);
1253 bfd_h_put_32 (abfd
, header
->cputype
, raw
.cputype
);
1254 bfd_h_put_32 (abfd
, header
->cpusubtype
, raw
.cpusubtype
);
1255 bfd_h_put_32 (abfd
, header
->filetype
, raw
.filetype
);
1256 bfd_h_put_32 (abfd
, header
->ncmds
, raw
.ncmds
);
1257 bfd_h_put_32 (abfd
, header
->sizeofcmds
, raw
.sizeofcmds
);
1258 bfd_h_put_32 (abfd
, header
->flags
, raw
.flags
);
1260 if (mach_o_wide_p (header
))
1261 bfd_h_put_32 (abfd
, header
->reserved
, raw
.reserved
);
1263 if (bfd_seek (abfd
, 0, SEEK_SET
) != 0
1264 || bfd_bwrite (&raw
, size
, abfd
) != size
)
1271 bfd_mach_o_write_thread (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1273 bfd_mach_o_thread_command
*cmd
= &command
->command
.thread
;
1275 struct mach_o_thread_command_external raw
;
1276 unsigned int offset
;
1278 BFD_ASSERT ((command
->type
== BFD_MACH_O_LC_THREAD
)
1279 || (command
->type
== BFD_MACH_O_LC_UNIXTHREAD
));
1281 offset
= BFD_MACH_O_LC_SIZE
;
1282 for (i
= 0; i
< cmd
->nflavours
; i
++)
1284 BFD_ASSERT ((cmd
->flavours
[i
].size
% 4) == 0);
1285 BFD_ASSERT (cmd
->flavours
[i
].offset
==
1286 (command
->offset
+ offset
+ BFD_MACH_O_LC_SIZE
));
1288 bfd_h_put_32 (abfd
, cmd
->flavours
[i
].flavour
, raw
.flavour
);
1289 bfd_h_put_32 (abfd
, (cmd
->flavours
[i
].size
/ 4), raw
.count
);
1291 if (bfd_seek (abfd
, command
->offset
+ offset
, SEEK_SET
) != 0
1292 || bfd_bwrite (&raw
, sizeof (raw
), abfd
) != sizeof (raw
))
1295 offset
+= cmd
->flavours
[i
].size
+ sizeof (raw
);
1302 bfd_mach_o_write_dylinker (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1304 bfd_mach_o_dylinker_command
*cmd
= &command
->command
.dylinker
;
1305 struct mach_o_str_command_external raw
;
1306 unsigned int namelen
;
1308 bfd_h_put_32 (abfd
, cmd
->name_offset
, raw
.str
);
1310 if (bfd_seek (abfd
, command
->offset
+ BFD_MACH_O_LC_SIZE
, SEEK_SET
) != 0
1311 || bfd_bwrite (&raw
, sizeof (raw
), abfd
) != sizeof (raw
))
1314 namelen
= strlen (cmd
->name_str
) + 1;
1315 if (bfd_bwrite (cmd
->name_str
, namelen
, abfd
) != namelen
)
1318 if (bfd_mach_o_pad_command (abfd
, namelen
) < 0)
1325 bfd_mach_o_write_dylib (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1327 bfd_mach_o_dylib_command
*cmd
= &command
->command
.dylib
;
1328 struct mach_o_dylib_command_external raw
;
1329 unsigned int namelen
;
1331 bfd_h_put_32 (abfd
, cmd
->name_offset
, raw
.name
);
1332 bfd_h_put_32 (abfd
, cmd
->timestamp
, raw
.timestamp
);
1333 bfd_h_put_32 (abfd
, cmd
->current_version
, raw
.current_version
);
1334 bfd_h_put_32 (abfd
, cmd
->compatibility_version
, raw
.compatibility_version
);
1336 if (bfd_seek (abfd
, command
->offset
+ BFD_MACH_O_LC_SIZE
, SEEK_SET
) != 0
1337 || bfd_bwrite (&raw
, sizeof (raw
), abfd
) != sizeof (raw
))
1340 namelen
= strlen (cmd
->name_str
) + 1;
1341 if (bfd_bwrite (cmd
->name_str
, namelen
, abfd
) != namelen
)
1344 if (bfd_mach_o_pad_command (abfd
, namelen
) < 0)
1351 bfd_mach_o_write_main (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1353 bfd_mach_o_main_command
*cmd
= &command
->command
.main
;
1354 struct mach_o_entry_point_command_external raw
;
1356 bfd_h_put_64 (abfd
, cmd
->entryoff
, raw
.entryoff
);
1357 bfd_h_put_64 (abfd
, cmd
->stacksize
, raw
.stacksize
);
1359 if (bfd_seek (abfd
, command
->offset
+ BFD_MACH_O_LC_SIZE
, SEEK_SET
) != 0
1360 || bfd_bwrite (&raw
, sizeof (raw
), abfd
) != sizeof (raw
))
1367 bfd_mach_o_write_dyld_info (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1369 bfd_mach_o_dyld_info_command
*cmd
= &command
->command
.dyld_info
;
1370 struct mach_o_dyld_info_command_external raw
;
1372 bfd_h_put_32 (abfd
, cmd
->rebase_off
, raw
.rebase_off
);
1373 bfd_h_put_32 (abfd
, cmd
->rebase_size
, raw
.rebase_size
);
1374 bfd_h_put_32 (abfd
, cmd
->bind_off
, raw
.bind_off
);
1375 bfd_h_put_32 (abfd
, cmd
->bind_size
, raw
.bind_size
);
1376 bfd_h_put_32 (abfd
, cmd
->weak_bind_off
, raw
.weak_bind_off
);
1377 bfd_h_put_32 (abfd
, cmd
->weak_bind_size
, raw
.weak_bind_size
);
1378 bfd_h_put_32 (abfd
, cmd
->lazy_bind_off
, raw
.lazy_bind_off
);
1379 bfd_h_put_32 (abfd
, cmd
->lazy_bind_size
, raw
.lazy_bind_size
);
1380 bfd_h_put_32 (abfd
, cmd
->export_off
, raw
.export_off
);
1381 bfd_h_put_32 (abfd
, cmd
->export_size
, raw
.export_size
);
1383 if (bfd_seek (abfd
, command
->offset
+ BFD_MACH_O_LC_SIZE
, SEEK_SET
) != 0
1384 || bfd_bwrite (&raw
, sizeof (raw
), abfd
) != sizeof (raw
))
1387 if (cmd
->rebase_size
!= 0)
1388 if (bfd_seek (abfd
, cmd
->rebase_off
, SEEK_SET
) != 0
1389 || (bfd_bwrite (cmd
->rebase_content
, cmd
->rebase_size
, abfd
) !=
1393 if (cmd
->bind_size
!= 0)
1394 if (bfd_seek (abfd
, cmd
->bind_off
, SEEK_SET
) != 0
1395 || (bfd_bwrite (cmd
->bind_content
, cmd
->bind_size
, abfd
) !=
1399 if (cmd
->weak_bind_size
!= 0)
1400 if (bfd_seek (abfd
, cmd
->weak_bind_off
, SEEK_SET
) != 0
1401 || (bfd_bwrite (cmd
->weak_bind_content
, cmd
->weak_bind_size
, abfd
) !=
1402 cmd
->weak_bind_size
))
1405 if (cmd
->lazy_bind_size
!= 0)
1406 if (bfd_seek (abfd
, cmd
->lazy_bind_off
, SEEK_SET
) != 0
1407 || (bfd_bwrite (cmd
->lazy_bind_content
, cmd
->lazy_bind_size
, abfd
) !=
1408 cmd
->lazy_bind_size
))
1411 if (cmd
->export_size
!= 0)
1412 if (bfd_seek (abfd
, cmd
->export_off
, SEEK_SET
) != 0
1413 || (bfd_bwrite (cmd
->export_content
, cmd
->export_size
, abfd
) !=
1421 bfd_mach_o_get_reloc_upper_bound (bfd
*abfd ATTRIBUTE_UNUSED
,
1424 #if SIZEOF_LONG == SIZEOF_INT
1425 if (asect
->reloc_count
>= LONG_MAX
/ sizeof (arelent
*))
1427 bfd_set_error (bfd_error_file_too_big
);
1431 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
1434 /* In addition to the need to byte-swap the symbol number, the bit positions
1435 of the fields in the relocation information vary per target endian-ness. */
1438 bfd_mach_o_swap_in_non_scattered_reloc (bfd
*abfd
, bfd_mach_o_reloc_info
*rel
,
1439 unsigned char *fields
)
1441 unsigned char info
= fields
[3];
1443 if (bfd_big_endian (abfd
))
1445 rel
->r_value
= (fields
[0] << 16) | (fields
[1] << 8) | fields
[2];
1446 rel
->r_type
= (info
>> BFD_MACH_O_BE_TYPE_SHIFT
) & BFD_MACH_O_TYPE_MASK
;
1447 rel
->r_pcrel
= (info
& BFD_MACH_O_BE_PCREL
) ? 1 : 0;
1448 rel
->r_length
= (info
>> BFD_MACH_O_BE_LENGTH_SHIFT
)
1449 & BFD_MACH_O_LENGTH_MASK
;
1450 rel
->r_extern
= (info
& BFD_MACH_O_BE_EXTERN
) ? 1 : 0;
1454 rel
->r_value
= (fields
[2] << 16) | (fields
[1] << 8) | fields
[0];
1455 rel
->r_type
= (info
>> BFD_MACH_O_LE_TYPE_SHIFT
) & BFD_MACH_O_TYPE_MASK
;
1456 rel
->r_pcrel
= (info
& BFD_MACH_O_LE_PCREL
) ? 1 : 0;
1457 rel
->r_length
= (info
>> BFD_MACH_O_LE_LENGTH_SHIFT
)
1458 & BFD_MACH_O_LENGTH_MASK
;
1459 rel
->r_extern
= (info
& BFD_MACH_O_LE_EXTERN
) ? 1 : 0;
1463 /* Set syms_ptr_ptr and addend of RES. */
1466 bfd_mach_o_canonicalize_non_scattered_reloc (bfd
*abfd
,
1467 bfd_mach_o_reloc_info
*reloc
,
1468 arelent
*res
, asymbol
**syms
)
1470 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
1474 /* Non-scattered relocation. */
1475 reloc
->r_scattered
= 0;
1478 num
= reloc
->r_value
;
1480 if (reloc
->r_extern
)
1482 /* PR 17512: file: 8396-1185-0.004. */
1483 if (num
>= (unsigned) bfd_mach_o_count_symbols (abfd
))
1484 sym
= bfd_und_section_ptr
->symbol_ptr_ptr
;
1485 else if (syms
== NULL
)
1486 sym
= bfd_und_section_ptr
->symbol_ptr_ptr
;
1488 /* An external symbol number. */
1491 else if (num
== 0x00ffffff || num
== 0)
1493 /* The 'symnum' in a non-scattered PAIR is 0x00ffffff. But as this
1494 is generic code, we don't know wether this is really a PAIR.
1495 This value is almost certainly not a valid section number, hence
1496 this specific case to avoid an assertion failure.
1497 Target specific swap_reloc_in routine should adjust that. */
1498 sym
= bfd_abs_section_ptr
->symbol_ptr_ptr
;
1502 /* PR 17512: file: 006-2964-0.004. */
1503 if (num
> mdata
->nsects
)
1505 _bfd_error_handler (_("\
1506 malformed mach-o reloc: section index is greater than the number of sections"));
1510 /* A section number. */
1511 sym
= mdata
->sections
[num
- 1]->bfdsection
->symbol_ptr_ptr
;
1512 /* For a symbol defined in section S, the addend (stored in the
1513 binary) contains the address of the section. To comply with
1514 bfd convention, subtract the section address.
1515 Use the address from the header, so that the user can modify
1516 the vma of the section. */
1517 res
->addend
= -mdata
->sections
[num
- 1]->addr
;
1520 /* Note: Pairs for PPC LO/HI/HA are not scattered, but contain the offset
1521 in the lower 16bits of the address value. So we have to find the
1522 'symbol' from the preceding reloc. We do this even though the
1523 section symbol is probably not needed here, because NULL symbol
1524 values cause an assert in generic BFD code. This must be done in
1525 the PPC swap_reloc_in routine. */
1526 res
->sym_ptr_ptr
= sym
;
1531 /* Do most of the work for canonicalize_relocs on RAW: create internal
1532 representation RELOC and set most fields of RES using symbol table SYMS.
1533 Each target still has to set the howto of RES and possibly adjust other
1535 Previously the Mach-O hook point was simply swap_in, but some targets
1536 (like arm64) don't follow the generic rules (symnum is a value for the
1537 non-scattered relocation ADDEND). */
1540 bfd_mach_o_pre_canonicalize_one_reloc (bfd
*abfd
,
1541 struct mach_o_reloc_info_external
*raw
,
1542 bfd_mach_o_reloc_info
*reloc
,
1543 arelent
*res
, asymbol
**syms
)
1545 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
1548 addr
= bfd_get_32 (abfd
, raw
->r_address
);
1549 res
->sym_ptr_ptr
= NULL
;
1552 if (addr
& BFD_MACH_O_SR_SCATTERED
)
1555 bfd_vma symnum
= bfd_get_32 (abfd
, raw
->r_symbolnum
);
1557 /* Scattered relocation, can't be extern. */
1558 reloc
->r_scattered
= 1;
1559 reloc
->r_extern
= 0;
1561 /* Extract section and offset from r_value (symnum). */
1562 reloc
->r_value
= symnum
;
1563 /* FIXME: This breaks when a symbol in a reloc exactly follows the
1564 end of the data for the section (e.g. in a calculation of section
1565 data length). At present, the symbol will end up associated with
1566 the following section or, if it falls within alignment padding, as
1567 null - which will assert later. */
1568 for (j
= 0; j
< mdata
->nsects
; j
++)
1570 bfd_mach_o_section
*sect
= mdata
->sections
[j
];
1571 if (symnum
>= sect
->addr
&& symnum
< sect
->addr
+ sect
->size
)
1573 res
->sym_ptr_ptr
= sect
->bfdsection
->symbol_ptr_ptr
;
1574 res
->addend
= symnum
- sect
->addr
;
1579 /* Extract the info and address fields from r_address. */
1580 reloc
->r_type
= BFD_MACH_O_GET_SR_TYPE (addr
);
1581 reloc
->r_length
= BFD_MACH_O_GET_SR_LENGTH (addr
);
1582 reloc
->r_pcrel
= addr
& BFD_MACH_O_SR_PCREL
;
1583 reloc
->r_address
= BFD_MACH_O_GET_SR_TYPE (addr
);
1584 res
->address
= BFD_MACH_O_GET_SR_ADDRESS (addr
);
1588 /* Non-scattered relocation. */
1589 reloc
->r_scattered
= 0;
1590 reloc
->r_address
= addr
;
1591 res
->address
= addr
;
1593 /* The value and info fields have to be extracted dependent on target
1595 bfd_mach_o_swap_in_non_scattered_reloc (abfd
, reloc
, raw
->r_symbolnum
);
1597 if (!bfd_mach_o_canonicalize_non_scattered_reloc (abfd
, reloc
,
1602 /* We have set up a reloc with all the information present, so the swapper
1603 can modify address, value and addend fields, if necessary, to convey
1604 information in the generic BFD reloc that is mach-o specific. */
1610 bfd_mach_o_canonicalize_relocs (bfd
*abfd
, unsigned long filepos
,
1611 unsigned long count
,
1612 arelent
*res
, asymbol
**syms
)
1614 bfd_mach_o_backend_data
*bed
= bfd_mach_o_get_backend_data (abfd
);
1616 struct mach_o_reloc_info_external
*native_relocs
= NULL
;
1619 /* Allocate and read relocs. */
1620 if (_bfd_mul_overflow (count
, BFD_MACH_O_RELENT_SIZE
, &native_size
))
1621 /* PR 17512: file: 09477b57. */
1624 if (bfd_seek (abfd
, filepos
, SEEK_SET
) != 0)
1626 native_relocs
= (struct mach_o_reloc_info_external
*)
1627 _bfd_malloc_and_read (abfd
, native_size
, native_size
);
1628 if (native_relocs
== NULL
)
1631 for (i
= 0; i
< count
; i
++)
1633 if (!(*bed
->_bfd_mach_o_canonicalize_one_reloc
)(abfd
, &native_relocs
[i
],
1634 &res
[i
], syms
, res
))
1637 free (native_relocs
);
1641 free (native_relocs
);
1642 if (bfd_get_error () == bfd_error_no_error
)
1643 bfd_set_error (bfd_error_invalid_operation
);
1648 bfd_mach_o_canonicalize_reloc (bfd
*abfd
, asection
*asect
,
1649 arelent
**rels
, asymbol
**syms
)
1651 bfd_mach_o_backend_data
*bed
= bfd_mach_o_get_backend_data (abfd
);
1655 if (asect
->reloc_count
== 0)
1658 /* No need to go further if we don't know how to read relocs. */
1659 if (bed
->_bfd_mach_o_canonicalize_one_reloc
== NULL
)
1662 if (asect
->relocation
== NULL
)
1666 if (_bfd_mul_overflow (asect
->reloc_count
, sizeof (arelent
), &amt
))
1668 res
= bfd_malloc (amt
);
1672 if (bfd_mach_o_canonicalize_relocs (abfd
, asect
->rel_filepos
,
1673 asect
->reloc_count
, res
, syms
) < 0)
1678 asect
->relocation
= res
;
1681 res
= asect
->relocation
;
1682 for (i
= 0; i
< asect
->reloc_count
; i
++)
1690 bfd_mach_o_get_dynamic_reloc_upper_bound (bfd
*abfd
)
1692 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
1694 if (mdata
->dysymtab
== NULL
)
1696 return (mdata
->dysymtab
->nextrel
+ mdata
->dysymtab
->nlocrel
+ 1)
1697 * sizeof (arelent
*);
1701 bfd_mach_o_canonicalize_dynamic_reloc (bfd
*abfd
, arelent
**rels
,
1702 struct bfd_symbol
**syms
)
1704 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
1705 bfd_mach_o_dysymtab_command
*dysymtab
= mdata
->dysymtab
;
1706 bfd_mach_o_backend_data
*bed
= bfd_mach_o_get_backend_data (abfd
);
1710 if (dysymtab
== NULL
)
1712 if (dysymtab
->nextrel
== 0 && dysymtab
->nlocrel
== 0)
1715 /* No need to go further if we don't know how to read relocs. */
1716 if (bed
->_bfd_mach_o_canonicalize_one_reloc
== NULL
)
1719 if (mdata
->dyn_reloc_cache
== NULL
)
1721 ufile_ptr filesize
= bfd_get_file_size (abfd
);
1726 if (dysymtab
->extreloff
> filesize
1727 || dysymtab
->nextrel
> ((filesize
- dysymtab
->extreloff
)
1728 / BFD_MACH_O_RELENT_SIZE
)
1729 || dysymtab
->locreloff
> filesize
1730 || dysymtab
->nlocrel
> ((filesize
- dysymtab
->locreloff
)
1731 / BFD_MACH_O_RELENT_SIZE
))
1733 bfd_set_error (bfd_error_file_truncated
);
1737 if (_bfd_mul_overflow (dysymtab
->nextrel
+ dysymtab
->nlocrel
,
1738 sizeof (arelent
), &amt
))
1740 bfd_set_error (bfd_error_file_too_big
);
1744 res
= bfd_malloc (amt
);
1748 if (bfd_mach_o_canonicalize_relocs (abfd
, dysymtab
->extreloff
,
1749 dysymtab
->nextrel
, res
, syms
) < 0)
1755 if (bfd_mach_o_canonicalize_relocs (abfd
, dysymtab
->locreloff
,
1757 res
+ dysymtab
->nextrel
, syms
) < 0)
1763 mdata
->dyn_reloc_cache
= res
;
1766 res
= mdata
->dyn_reloc_cache
;
1767 for (i
= 0; i
< dysymtab
->nextrel
+ dysymtab
->nlocrel
; i
++)
1773 /* In addition to the need to byte-swap the symbol number, the bit positions
1774 of the fields in the relocation information vary per target endian-ness. */
1777 bfd_mach_o_swap_out_non_scattered_reloc (bfd
*abfd
, unsigned char *fields
,
1778 bfd_mach_o_reloc_info
*rel
)
1780 unsigned char info
= 0;
1782 BFD_ASSERT (rel
->r_type
<= 15);
1783 BFD_ASSERT (rel
->r_length
<= 3);
1785 if (bfd_big_endian (abfd
))
1787 fields
[0] = (rel
->r_value
>> 16) & 0xff;
1788 fields
[1] = (rel
->r_value
>> 8) & 0xff;
1789 fields
[2] = rel
->r_value
& 0xff;
1790 info
|= rel
->r_type
<< BFD_MACH_O_BE_TYPE_SHIFT
;
1791 info
|= rel
->r_pcrel
? BFD_MACH_O_BE_PCREL
: 0;
1792 info
|= rel
->r_length
<< BFD_MACH_O_BE_LENGTH_SHIFT
;
1793 info
|= rel
->r_extern
? BFD_MACH_O_BE_EXTERN
: 0;
1797 fields
[2] = (rel
->r_value
>> 16) & 0xff;
1798 fields
[1] = (rel
->r_value
>> 8) & 0xff;
1799 fields
[0] = rel
->r_value
& 0xff;
1800 info
|= rel
->r_type
<< BFD_MACH_O_LE_TYPE_SHIFT
;
1801 info
|= rel
->r_pcrel
? BFD_MACH_O_LE_PCREL
: 0;
1802 info
|= rel
->r_length
<< BFD_MACH_O_LE_LENGTH_SHIFT
;
1803 info
|= rel
->r_extern
? BFD_MACH_O_LE_EXTERN
: 0;
1809 bfd_mach_o_write_relocs (bfd
*abfd
, bfd_mach_o_section
*section
)
1814 bfd_mach_o_backend_data
*bed
= bfd_mach_o_get_backend_data (abfd
);
1816 sec
= section
->bfdsection
;
1817 if (sec
->reloc_count
== 0)
1820 if (bed
->_bfd_mach_o_swap_reloc_out
== NULL
)
1823 if (bfd_seek (abfd
, section
->reloff
, SEEK_SET
) != 0)
1826 /* Convert and write. */
1827 entries
= section
->bfdsection
->orelocation
;
1828 for (i
= 0; i
< section
->nreloc
; i
++)
1830 arelent
*rel
= entries
[i
];
1831 struct mach_o_reloc_info_external raw
;
1832 bfd_mach_o_reloc_info info
, *pinfo
= &info
;
1834 /* Convert relocation to an intermediate representation. */
1835 if (!(*bed
->_bfd_mach_o_swap_reloc_out
) (rel
, pinfo
))
1838 /* Lower the relocation info. */
1839 if (pinfo
->r_scattered
)
1843 v
= BFD_MACH_O_SR_SCATTERED
1844 | (pinfo
->r_pcrel
? BFD_MACH_O_SR_PCREL
: 0)
1845 | BFD_MACH_O_SET_SR_LENGTH (pinfo
->r_length
)
1846 | BFD_MACH_O_SET_SR_TYPE (pinfo
->r_type
)
1847 | BFD_MACH_O_SET_SR_ADDRESS (pinfo
->r_address
);
1848 /* Note: scattered relocs have field in reverse order... */
1849 bfd_put_32 (abfd
, v
, raw
.r_address
);
1850 bfd_put_32 (abfd
, pinfo
->r_value
, raw
.r_symbolnum
);
1854 bfd_put_32 (abfd
, pinfo
->r_address
, raw
.r_address
);
1855 bfd_mach_o_swap_out_non_scattered_reloc (abfd
, raw
.r_symbolnum
,
1859 if (bfd_bwrite (&raw
, BFD_MACH_O_RELENT_SIZE
, abfd
)
1860 != BFD_MACH_O_RELENT_SIZE
)
1867 bfd_mach_o_write_section_32 (bfd
*abfd
, bfd_mach_o_section
*section
)
1869 struct mach_o_section_32_external raw
;
1871 memcpy (raw
.sectname
, section
->sectname
, 16);
1872 memcpy (raw
.segname
, section
->segname
, 16);
1873 bfd_h_put_32 (abfd
, section
->addr
, raw
.addr
);
1874 bfd_h_put_32 (abfd
, section
->size
, raw
.size
);
1875 bfd_h_put_32 (abfd
, section
->offset
, raw
.offset
);
1876 bfd_h_put_32 (abfd
, section
->align
, raw
.align
);
1877 bfd_h_put_32 (abfd
, section
->reloff
, raw
.reloff
);
1878 bfd_h_put_32 (abfd
, section
->nreloc
, raw
.nreloc
);
1879 bfd_h_put_32 (abfd
, section
->flags
, raw
.flags
);
1880 bfd_h_put_32 (abfd
, section
->reserved1
, raw
.reserved1
);
1881 bfd_h_put_32 (abfd
, section
->reserved2
, raw
.reserved2
);
1883 if (bfd_bwrite (&raw
, BFD_MACH_O_SECTION_SIZE
, abfd
)
1884 != BFD_MACH_O_SECTION_SIZE
)
1891 bfd_mach_o_write_section_64 (bfd
*abfd
, bfd_mach_o_section
*section
)
1893 struct mach_o_section_64_external raw
;
1895 memcpy (raw
.sectname
, section
->sectname
, 16);
1896 memcpy (raw
.segname
, section
->segname
, 16);
1897 bfd_h_put_64 (abfd
, section
->addr
, raw
.addr
);
1898 bfd_h_put_64 (abfd
, section
->size
, raw
.size
);
1899 bfd_h_put_32 (abfd
, section
->offset
, raw
.offset
);
1900 bfd_h_put_32 (abfd
, section
->align
, raw
.align
);
1901 bfd_h_put_32 (abfd
, section
->reloff
, raw
.reloff
);
1902 bfd_h_put_32 (abfd
, section
->nreloc
, raw
.nreloc
);
1903 bfd_h_put_32 (abfd
, section
->flags
, raw
.flags
);
1904 bfd_h_put_32 (abfd
, section
->reserved1
, raw
.reserved1
);
1905 bfd_h_put_32 (abfd
, section
->reserved2
, raw
.reserved2
);
1906 bfd_h_put_32 (abfd
, section
->reserved3
, raw
.reserved3
);
1908 if (bfd_bwrite (&raw
, BFD_MACH_O_SECTION_64_SIZE
, abfd
)
1909 != BFD_MACH_O_SECTION_64_SIZE
)
1916 bfd_mach_o_write_segment_32 (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1918 struct mach_o_segment_command_32_external raw
;
1919 bfd_mach_o_segment_command
*seg
= &command
->command
.segment
;
1920 bfd_mach_o_section
*sec
;
1922 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SEGMENT
);
1924 for (sec
= seg
->sect_head
; sec
!= NULL
; sec
= sec
->next
)
1925 if (!bfd_mach_o_write_relocs (abfd
, sec
))
1928 memcpy (raw
.segname
, seg
->segname
, 16);
1929 bfd_h_put_32 (abfd
, seg
->vmaddr
, raw
.vmaddr
);
1930 bfd_h_put_32 (abfd
, seg
->vmsize
, raw
.vmsize
);
1931 bfd_h_put_32 (abfd
, seg
->fileoff
, raw
.fileoff
);
1932 bfd_h_put_32 (abfd
, seg
->filesize
, raw
.filesize
);
1933 bfd_h_put_32 (abfd
, seg
->maxprot
, raw
.maxprot
);
1934 bfd_h_put_32 (abfd
, seg
->initprot
, raw
.initprot
);
1935 bfd_h_put_32 (abfd
, seg
->nsects
, raw
.nsects
);
1936 bfd_h_put_32 (abfd
, seg
->flags
, raw
.flags
);
1938 if (bfd_seek (abfd
, command
->offset
+ BFD_MACH_O_LC_SIZE
, SEEK_SET
) != 0
1939 || bfd_bwrite (&raw
, sizeof (raw
), abfd
) != sizeof (raw
))
1942 for (sec
= seg
->sect_head
; sec
!= NULL
; sec
= sec
->next
)
1943 if (!bfd_mach_o_write_section_32 (abfd
, sec
))
1950 bfd_mach_o_write_segment_64 (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1952 struct mach_o_segment_command_64_external raw
;
1953 bfd_mach_o_segment_command
*seg
= &command
->command
.segment
;
1954 bfd_mach_o_section
*sec
;
1956 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SEGMENT_64
);
1958 for (sec
= seg
->sect_head
; sec
!= NULL
; sec
= sec
->next
)
1959 if (!bfd_mach_o_write_relocs (abfd
, sec
))
1962 memcpy (raw
.segname
, seg
->segname
, 16);
1963 bfd_h_put_64 (abfd
, seg
->vmaddr
, raw
.vmaddr
);
1964 bfd_h_put_64 (abfd
, seg
->vmsize
, raw
.vmsize
);
1965 bfd_h_put_64 (abfd
, seg
->fileoff
, raw
.fileoff
);
1966 bfd_h_put_64 (abfd
, seg
->filesize
, raw
.filesize
);
1967 bfd_h_put_32 (abfd
, seg
->maxprot
, raw
.maxprot
);
1968 bfd_h_put_32 (abfd
, seg
->initprot
, raw
.initprot
);
1969 bfd_h_put_32 (abfd
, seg
->nsects
, raw
.nsects
);
1970 bfd_h_put_32 (abfd
, seg
->flags
, raw
.flags
);
1972 if (bfd_seek (abfd
, command
->offset
+ BFD_MACH_O_LC_SIZE
, SEEK_SET
) != 0
1973 || bfd_bwrite (&raw
, sizeof (raw
), abfd
) != sizeof (raw
))
1976 for (sec
= seg
->sect_head
; sec
!= NULL
; sec
= sec
->next
)
1977 if (!bfd_mach_o_write_section_64 (abfd
, sec
))
1984 bfd_mach_o_write_symtab_content (bfd
*abfd
, bfd_mach_o_symtab_command
*sym
)
1986 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
1988 unsigned int wide
= bfd_mach_o_wide_p (abfd
);
1989 struct bfd_strtab_hash
*strtab
;
1990 asymbol
**symbols
= bfd_get_outsymbols (abfd
);
1993 /* Write the symbols first. */
1994 if (bfd_seek (abfd
, sym
->symoff
, SEEK_SET
) != 0)
1997 strtab
= _bfd_stringtab_init ();
2002 /* Although we don't strictly need to do this, for compatibility with
2003 Darwin system tools, actually output an empty string for the index
2005 _bfd_stringtab_add (strtab
, "", TRUE
, FALSE
);
2007 for (i
= 0; i
< sym
->nsyms
; i
++)
2009 bfd_size_type str_index
;
2010 bfd_mach_o_asymbol
*s
= (bfd_mach_o_asymbol
*)symbols
[i
];
2012 if (s
->symbol
.name
== 0 || s
->symbol
.name
[0] == '\0')
2013 /* An index of 0 always means the empty string. */
2017 str_index
= _bfd_stringtab_add (strtab
, s
->symbol
.name
, TRUE
, FALSE
);
2019 if (str_index
== (bfd_size_type
) -1)
2025 struct mach_o_nlist_64_external raw
;
2027 bfd_h_put_32 (abfd
, str_index
, raw
.n_strx
);
2028 bfd_h_put_8 (abfd
, s
->n_type
, raw
.n_type
);
2029 bfd_h_put_8 (abfd
, s
->n_sect
, raw
.n_sect
);
2030 bfd_h_put_16 (abfd
, s
->n_desc
, raw
.n_desc
);
2031 bfd_h_put_64 (abfd
, s
->symbol
.section
->vma
+ s
->symbol
.value
,
2034 if (bfd_bwrite (&raw
, sizeof (raw
), abfd
) != sizeof (raw
))
2039 struct mach_o_nlist_external raw
;
2041 bfd_h_put_32 (abfd
, str_index
, raw
.n_strx
);
2042 bfd_h_put_8 (abfd
, s
->n_type
, raw
.n_type
);
2043 bfd_h_put_8 (abfd
, s
->n_sect
, raw
.n_sect
);
2044 bfd_h_put_16 (abfd
, s
->n_desc
, raw
.n_desc
);
2045 bfd_h_put_32 (abfd
, s
->symbol
.section
->vma
+ s
->symbol
.value
,
2048 if (bfd_bwrite (&raw
, sizeof (raw
), abfd
) != sizeof (raw
))
2052 sym
->strsize
= _bfd_stringtab_size (strtab
);
2053 sym
->stroff
= mdata
->filelen
;
2054 mdata
->filelen
+= sym
->strsize
;
2056 if (bfd_seek (abfd
, sym
->stroff
, SEEK_SET
) != 0)
2059 if (!_bfd_stringtab_emit (abfd
, strtab
))
2062 /* Pad string table. */
2063 padlen
= bfd_mach_o_pad4 (abfd
, sym
->strsize
);
2066 mdata
->filelen
+= padlen
;
2067 sym
->strsize
+= padlen
;
2072 _bfd_stringtab_free (strtab
);
2078 bfd_mach_o_write_symtab (bfd
*abfd
, bfd_mach_o_load_command
*command
)
2080 bfd_mach_o_symtab_command
*sym
= &command
->command
.symtab
;
2081 struct mach_o_symtab_command_external raw
;
2083 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SYMTAB
);
2086 bfd_h_put_32 (abfd
, sym
->symoff
, raw
.symoff
);
2087 bfd_h_put_32 (abfd
, sym
->nsyms
, raw
.nsyms
);
2088 bfd_h_put_32 (abfd
, sym
->stroff
, raw
.stroff
);
2089 bfd_h_put_32 (abfd
, sym
->strsize
, raw
.strsize
);
2091 if (bfd_seek (abfd
, command
->offset
+ BFD_MACH_O_LC_SIZE
, SEEK_SET
) != 0
2092 || bfd_bwrite (&raw
, sizeof (raw
), abfd
) != sizeof (raw
))
2098 /* Count the number of indirect symbols in the image.
2099 Requires that the sections are in their final order. */
2102 bfd_mach_o_count_indirect_symbols (bfd
*abfd
, bfd_mach_o_data_struct
*mdata
)
2105 unsigned int nisyms
= 0;
2107 for (i
= 0; i
< mdata
->nsects
; ++i
)
2109 bfd_mach_o_section
*sec
= mdata
->sections
[i
];
2111 switch (sec
->flags
& BFD_MACH_O_SECTION_TYPE_MASK
)
2113 case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS
:
2114 case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS
:
2115 case BFD_MACH_O_S_SYMBOL_STUBS
:
2116 nisyms
+= bfd_mach_o_section_get_nbr_indirect (abfd
, sec
);
2125 /* Create the dysymtab. */
2128 bfd_mach_o_build_dysymtab (bfd
*abfd
, bfd_mach_o_dysymtab_command
*cmd
)
2130 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
2133 We are not going to try and fill these in yet and, moreover, we are
2134 going to bail if they are already set. */
2135 if (cmd
->nmodtab
!= 0
2137 || cmd
->nextrefsyms
!= 0)
2139 _bfd_error_handler (_("sorry: modtab, toc and extrefsyms are not yet"
2140 " implemented for dysymtab commands."));
2146 if (bfd_get_symcount (abfd
) > 0)
2148 asymbol
**symbols
= bfd_get_outsymbols (abfd
);
2151 /* Count the number of each kind of symbol. */
2152 for (i
= 0; i
< bfd_get_symcount (abfd
); ++i
)
2154 bfd_mach_o_asymbol
*s
= (bfd_mach_o_asymbol
*)symbols
[i
];
2155 if (s
->n_type
& (BFD_MACH_O_N_EXT
| BFD_MACH_O_N_PEXT
))
2159 cmd
->iextdefsym
= i
;
2160 for (; i
< bfd_get_symcount (abfd
); ++i
)
2162 bfd_mach_o_asymbol
*s
= (bfd_mach_o_asymbol
*)symbols
[i
];
2163 if ((s
->n_type
& BFD_MACH_O_N_TYPE
) == BFD_MACH_O_N_UNDF
)
2166 cmd
->nextdefsym
= i
- cmd
->nlocalsym
;
2167 cmd
->iundefsym
= cmd
->nextdefsym
+ cmd
->iextdefsym
;
2168 cmd
->nundefsym
= bfd_get_symcount (abfd
)
2175 cmd
->iextdefsym
= 0;
2176 cmd
->nextdefsym
= 0;
2181 cmd
->nindirectsyms
= bfd_mach_o_count_indirect_symbols (abfd
, mdata
);
2182 if (cmd
->nindirectsyms
> 0)
2188 mdata
->filelen
= FILE_ALIGN (mdata
->filelen
, 2);
2189 cmd
->indirectsymoff
= mdata
->filelen
;
2190 if (_bfd_mul_overflow (cmd
->nindirectsyms
, 4, &amt
))
2192 mdata
->filelen
+= amt
;
2194 cmd
->indirect_syms
= bfd_zalloc (abfd
, amt
);
2195 if (cmd
->indirect_syms
== NULL
)
2199 for (i
= 0; i
< mdata
->nsects
; ++i
)
2201 bfd_mach_o_section
*sec
= mdata
->sections
[i
];
2203 switch (sec
->flags
& BFD_MACH_O_SECTION_TYPE_MASK
)
2205 case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS
:
2206 case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS
:
2207 case BFD_MACH_O_S_SYMBOL_STUBS
:
2210 bfd_mach_o_asymbol
**isyms
= sec
->indirect_syms
;
2212 num
= bfd_mach_o_section_get_nbr_indirect (abfd
, sec
);
2213 if (isyms
== NULL
|| num
== 0)
2215 /* Record the starting index in the reserved1 field. */
2217 for (j
= 0; j
< num
; j
++, n
++)
2219 if (isyms
[j
] == NULL
)
2220 cmd
->indirect_syms
[n
] = BFD_MACH_O_INDIRECT_SYM_LOCAL
;
2221 else if (isyms
[j
]->symbol
.section
== bfd_abs_section_ptr
2222 && ! (isyms
[j
]->n_type
& BFD_MACH_O_N_EXT
))
2223 cmd
->indirect_syms
[n
] = BFD_MACH_O_INDIRECT_SYM_LOCAL
2224 | BFD_MACH_O_INDIRECT_SYM_ABS
;
2226 cmd
->indirect_syms
[n
] = isyms
[j
]->symbol
.udata
.i
;
2239 /* Write a dysymtab command.
2240 TODO: Possibly coalesce writes of smaller objects. */
2243 bfd_mach_o_write_dysymtab (bfd
*abfd
, bfd_mach_o_load_command
*command
)
2245 bfd_mach_o_dysymtab_command
*cmd
= &command
->command
.dysymtab
;
2247 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_DYSYMTAB
);
2249 if (cmd
->nmodtab
!= 0)
2253 if (bfd_seek (abfd
, cmd
->modtaboff
, SEEK_SET
) != 0)
2256 for (i
= 0; i
< cmd
->nmodtab
; i
++)
2258 bfd_mach_o_dylib_module
*module
= &cmd
->dylib_module
[i
];
2262 iinit
= module
->iinit
& 0xffff;
2263 iinit
|= ((module
->iterm
& 0xffff) << 16);
2265 ninit
= module
->ninit
& 0xffff;
2266 ninit
|= ((module
->nterm
& 0xffff) << 16);
2268 if (bfd_mach_o_wide_p (abfd
))
2270 struct mach_o_dylib_module_64_external w
;
2272 bfd_h_put_32 (abfd
, module
->module_name_idx
, &w
.module_name
);
2273 bfd_h_put_32 (abfd
, module
->iextdefsym
, &w
.iextdefsym
);
2274 bfd_h_put_32 (abfd
, module
->nextdefsym
, &w
.nextdefsym
);
2275 bfd_h_put_32 (abfd
, module
->irefsym
, &w
.irefsym
);
2276 bfd_h_put_32 (abfd
, module
->nrefsym
, &w
.nrefsym
);
2277 bfd_h_put_32 (abfd
, module
->ilocalsym
, &w
.ilocalsym
);
2278 bfd_h_put_32 (abfd
, module
->nlocalsym
, &w
.nlocalsym
);
2279 bfd_h_put_32 (abfd
, module
->iextrel
, &w
.iextrel
);
2280 bfd_h_put_32 (abfd
, module
->nextrel
, &w
.nextrel
);
2281 bfd_h_put_32 (abfd
, iinit
, &w
.iinit_iterm
);
2282 bfd_h_put_32 (abfd
, ninit
, &w
.ninit_nterm
);
2283 bfd_h_put_64 (abfd
, module
->objc_module_info_addr
,
2284 &w
.objc_module_info_addr
);
2285 bfd_h_put_32 (abfd
, module
->objc_module_info_size
,
2286 &w
.objc_module_info_size
);
2288 if (bfd_bwrite ((void *) &w
, sizeof (w
), abfd
) != sizeof (w
))
2293 struct mach_o_dylib_module_external n
;
2295 bfd_h_put_32 (abfd
, module
->module_name_idx
, &n
.module_name
);
2296 bfd_h_put_32 (abfd
, module
->iextdefsym
, &n
.iextdefsym
);
2297 bfd_h_put_32 (abfd
, module
->nextdefsym
, &n
.nextdefsym
);
2298 bfd_h_put_32 (abfd
, module
->irefsym
, &n
.irefsym
);
2299 bfd_h_put_32 (abfd
, module
->nrefsym
, &n
.nrefsym
);
2300 bfd_h_put_32 (abfd
, module
->ilocalsym
, &n
.ilocalsym
);
2301 bfd_h_put_32 (abfd
, module
->nlocalsym
, &n
.nlocalsym
);
2302 bfd_h_put_32 (abfd
, module
->iextrel
, &n
.iextrel
);
2303 bfd_h_put_32 (abfd
, module
->nextrel
, &n
.nextrel
);
2304 bfd_h_put_32 (abfd
, iinit
, &n
.iinit_iterm
);
2305 bfd_h_put_32 (abfd
, ninit
, &n
.ninit_nterm
);
2306 bfd_h_put_32 (abfd
, module
->objc_module_info_addr
,
2307 &n
.objc_module_info_addr
);
2308 bfd_h_put_32 (abfd
, module
->objc_module_info_size
,
2309 &n
.objc_module_info_size
);
2311 if (bfd_bwrite ((void *) &n
, sizeof (n
), abfd
) != sizeof (n
))
2321 if (bfd_seek (abfd
, cmd
->tocoff
, SEEK_SET
) != 0)
2324 for (i
= 0; i
< cmd
->ntoc
; i
++)
2326 struct mach_o_dylib_table_of_contents_external raw
;
2327 bfd_mach_o_dylib_table_of_content
*toc
= &cmd
->dylib_toc
[i
];
2329 bfd_h_put_32 (abfd
, toc
->symbol_index
, &raw
.symbol_index
);
2330 bfd_h_put_32 (abfd
, toc
->module_index
, &raw
.module_index
);
2332 if (bfd_bwrite (&raw
, sizeof (raw
), abfd
) != sizeof (raw
))
2337 if (cmd
->nindirectsyms
> 0)
2341 if (bfd_seek (abfd
, cmd
->indirectsymoff
, SEEK_SET
) != 0)
2344 for (i
= 0; i
< cmd
->nindirectsyms
; ++i
)
2346 unsigned char raw
[4];
2348 bfd_h_put_32 (abfd
, cmd
->indirect_syms
[i
], &raw
);
2349 if (bfd_bwrite (raw
, sizeof (raw
), abfd
) != sizeof (raw
))
2354 if (cmd
->nextrefsyms
!= 0)
2358 if (bfd_seek (abfd
, cmd
->extrefsymoff
, SEEK_SET
) != 0)
2361 for (i
= 0; i
< cmd
->nextrefsyms
; i
++)
2364 unsigned char raw
[4];
2365 bfd_mach_o_dylib_reference
*ref
= &cmd
->ext_refs
[i
];
2367 /* Fields isym and flags are written as bit-fields, thus we need
2368 a specific processing for endianness. */
2370 if (bfd_big_endian (abfd
))
2372 v
= ((ref
->isym
& 0xffffff) << 8);
2373 v
|= ref
->flags
& 0xff;
2377 v
= ref
->isym
& 0xffffff;
2378 v
|= ((ref
->flags
& 0xff) << 24);
2381 bfd_h_put_32 (abfd
, v
, raw
);
2382 if (bfd_bwrite (raw
, sizeof (raw
), abfd
) != sizeof (raw
))
2388 if (bfd_seek (abfd
, command
->offset
+ BFD_MACH_O_LC_SIZE
, SEEK_SET
) != 0)
2392 struct mach_o_dysymtab_command_external raw
;
2394 bfd_h_put_32 (abfd
, cmd
->ilocalsym
, &raw
.ilocalsym
);
2395 bfd_h_put_32 (abfd
, cmd
->nlocalsym
, &raw
.nlocalsym
);
2396 bfd_h_put_32 (abfd
, cmd
->iextdefsym
, &raw
.iextdefsym
);
2397 bfd_h_put_32 (abfd
, cmd
->nextdefsym
, &raw
.nextdefsym
);
2398 bfd_h_put_32 (abfd
, cmd
->iundefsym
, &raw
.iundefsym
);
2399 bfd_h_put_32 (abfd
, cmd
->nundefsym
, &raw
.nundefsym
);
2400 bfd_h_put_32 (abfd
, cmd
->tocoff
, &raw
.tocoff
);
2401 bfd_h_put_32 (abfd
, cmd
->ntoc
, &raw
.ntoc
);
2402 bfd_h_put_32 (abfd
, cmd
->modtaboff
, &raw
.modtaboff
);
2403 bfd_h_put_32 (abfd
, cmd
->nmodtab
, &raw
.nmodtab
);
2404 bfd_h_put_32 (abfd
, cmd
->extrefsymoff
, &raw
.extrefsymoff
);
2405 bfd_h_put_32 (abfd
, cmd
->nextrefsyms
, &raw
.nextrefsyms
);
2406 bfd_h_put_32 (abfd
, cmd
->indirectsymoff
, &raw
.indirectsymoff
);
2407 bfd_h_put_32 (abfd
, cmd
->nindirectsyms
, &raw
.nindirectsyms
);
2408 bfd_h_put_32 (abfd
, cmd
->extreloff
, &raw
.extreloff
);
2409 bfd_h_put_32 (abfd
, cmd
->nextrel
, &raw
.nextrel
);
2410 bfd_h_put_32 (abfd
, cmd
->locreloff
, &raw
.locreloff
);
2411 bfd_h_put_32 (abfd
, cmd
->nlocrel
, &raw
.nlocrel
);
2413 if (bfd_bwrite (&raw
, sizeof (raw
), abfd
) != sizeof (raw
))
2421 bfd_mach_o_primary_symbol_sort_key (bfd_mach_o_asymbol
*s
)
2423 unsigned mtyp
= s
->n_type
& BFD_MACH_O_N_TYPE
;
2425 /* Just leave debug symbols where they are (pretend they are local, and
2426 then they will just be sorted on position). */
2427 if (s
->n_type
& BFD_MACH_O_N_STAB
)
2430 /* Local (we should never see an undefined local AFAICT). */
2431 if (! (s
->n_type
& (BFD_MACH_O_N_EXT
| BFD_MACH_O_N_PEXT
)))
2434 /* Common symbols look like undefined externs. */
2435 if (mtyp
== BFD_MACH_O_N_UNDF
)
2438 /* A defined non-local, non-debug symbol. */
2443 bfd_mach_o_cf_symbols (const void *a
, const void *b
)
2445 bfd_mach_o_asymbol
*sa
= *(bfd_mach_o_asymbol
**) a
;
2446 bfd_mach_o_asymbol
*sb
= *(bfd_mach_o_asymbol
**) b
;
2447 unsigned int soa
, sob
;
2449 soa
= bfd_mach_o_primary_symbol_sort_key (sa
);
2450 sob
= bfd_mach_o_primary_symbol_sort_key (sb
);
2457 /* If it's local or stab, just preserve the input order. */
2460 if (sa
->symbol
.udata
.i
< sb
->symbol
.udata
.i
)
2462 if (sa
->symbol
.udata
.i
> sb
->symbol
.udata
.i
)
2465 /* This is probably an error. */
2469 /* The second sort key is name. */
2470 return strcmp (sa
->symbol
.name
, sb
->symbol
.name
);
2473 /* Process the symbols.
2475 This should be OK for single-module files - but it is not likely to work
2476 for multi-module shared libraries.
2478 (a) If the application has not filled in the relevant mach-o fields, make
2481 (b) Order them, like this:
2484 ( ii) external defined
2486 (iii) external undefined/common
2493 bfd_mach_o_mangle_symbols (bfd
*abfd
)
2496 asymbol
**symbols
= bfd_get_outsymbols (abfd
);
2498 if (symbols
== NULL
|| bfd_get_symcount (abfd
) == 0)
2501 for (i
= 0; i
< bfd_get_symcount (abfd
); i
++)
2503 bfd_mach_o_asymbol
*s
= (bfd_mach_o_asymbol
*)symbols
[i
];
2505 /* We use this value, which is out-of-range as a symbol index, to signal
2506 that the mach-o-specific data are not filled in and need to be created
2507 from the bfd values. It is much preferable for the application to do
2508 this, since more meaningful diagnostics can be made that way. */
2510 if (s
->symbol
.udata
.i
== SYM_MACHO_FIELDS_UNSET
)
2512 /* No symbol information has been set - therefore determine
2513 it from the bfd symbol flags/info. */
2514 if (s
->symbol
.section
== bfd_abs_section_ptr
)
2515 s
->n_type
= BFD_MACH_O_N_ABS
;
2516 else if (s
->symbol
.section
== bfd_und_section_ptr
)
2518 s
->n_type
= BFD_MACH_O_N_UNDF
;
2519 if (s
->symbol
.flags
& BSF_WEAK
)
2520 s
->n_desc
|= BFD_MACH_O_N_WEAK_REF
;
2521 /* mach-o automatically makes undefined symbols extern. */
2522 s
->n_type
|= BFD_MACH_O_N_EXT
;
2523 s
->symbol
.flags
|= BSF_GLOBAL
;
2525 else if (s
->symbol
.section
== bfd_com_section_ptr
)
2527 s
->n_type
= BFD_MACH_O_N_UNDF
| BFD_MACH_O_N_EXT
;
2528 s
->symbol
.flags
|= BSF_GLOBAL
;
2531 s
->n_type
= BFD_MACH_O_N_SECT
;
2534 /* Update external symbol bit in case objcopy changed it. */
2535 if (s
->symbol
.flags
& BSF_GLOBAL
)
2536 s
->n_type
|= BFD_MACH_O_N_EXT
;
2538 s
->n_type
&= ~BFD_MACH_O_N_EXT
;
2540 /* Put the section index in, where required. */
2541 if ((s
->symbol
.section
!= bfd_abs_section_ptr
2542 && s
->symbol
.section
!= bfd_und_section_ptr
2543 && s
->symbol
.section
!= bfd_com_section_ptr
)
2544 || ((s
->n_type
& BFD_MACH_O_N_STAB
) != 0
2545 && s
->symbol
.name
== NULL
))
2546 s
->n_sect
= s
->symbol
.section
->output_section
->target_index
;
2548 /* Number to preserve order for local and debug syms. */
2549 s
->symbol
.udata
.i
= i
;
2552 /* Sort the symbols. */
2553 qsort ((void *) symbols
, (size_t) bfd_get_symcount (abfd
),
2554 sizeof (asymbol
*), bfd_mach_o_cf_symbols
);
2556 for (i
= 0; i
< bfd_get_symcount (abfd
); ++i
)
2558 bfd_mach_o_asymbol
*s
= (bfd_mach_o_asymbol
*)symbols
[i
];
2559 s
->symbol
.udata
.i
= i
; /* renumber. */
2565 /* We build a flat table of sections, which can be re-ordered if necessary.
2566 Fill in the section number and other mach-o-specific data. */
2569 bfd_mach_o_mangle_sections (bfd
*abfd
, bfd_mach_o_data_struct
*mdata
)
2572 unsigned target_index
;
2576 nsect
= bfd_count_sections (abfd
);
2578 /* Don't do it if it's already set - assume the application knows what it's
2580 if (mdata
->nsects
== nsect
2581 && (mdata
->nsects
== 0 || mdata
->sections
!= NULL
))
2584 /* We need to check that this can be done... */
2587 _bfd_error_handler (_("mach-o: there are too many sections (%u)"
2588 " maximum is 255,\n"), nsect
);
2592 mdata
->nsects
= nsect
;
2593 amt
= mdata
->nsects
* sizeof (bfd_mach_o_section
*);
2594 mdata
->sections
= bfd_alloc (abfd
, amt
);
2595 if (mdata
->sections
== NULL
)
2598 /* Create Mach-O sections.
2599 Section type, attribute and align should have been set when the
2600 section was created - either read in or specified. */
2602 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2604 unsigned bfd_align
= bfd_section_alignment (sec
);
2605 bfd_mach_o_section
*msect
= bfd_mach_o_get_mach_o_section (sec
);
2607 mdata
->sections
[target_index
] = msect
;
2609 msect
->addr
= bfd_section_vma (sec
);
2610 msect
->size
= bfd_section_size (sec
);
2612 /* Use the largest alignment set, in case it was bumped after the
2613 section was created. */
2614 msect
->align
= msect
->align
> bfd_align
? msect
->align
: bfd_align
;
2617 sec
->target_index
= ++target_index
;
2624 bfd_mach_o_write_contents (bfd
*abfd
)
2626 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
2627 bfd_mach_o_load_command
*cmd
;
2628 bfd_mach_o_symtab_command
*symtab
= NULL
;
2629 bfd_mach_o_dysymtab_command
*dysymtab
= NULL
;
2630 bfd_mach_o_segment_command
*linkedit
= NULL
;
2632 /* Make the commands, if not already present. */
2633 if (!abfd
->output_has_begun
&& !bfd_mach_o_build_commands (abfd
))
2635 abfd
->output_has_begun
= TRUE
;
2637 /* Write the header. */
2638 if (!bfd_mach_o_write_header (abfd
, &mdata
->header
))
2641 /* First pass: allocate the linkedit segment. */
2642 for (cmd
= mdata
->first_command
; cmd
!= NULL
; cmd
= cmd
->next
)
2645 case BFD_MACH_O_LC_SEGMENT_64
:
2646 case BFD_MACH_O_LC_SEGMENT
:
2647 if (strcmp (cmd
->command
.segment
.segname
, "__LINKEDIT") == 0)
2648 linkedit
= &cmd
->command
.segment
;
2650 case BFD_MACH_O_LC_SYMTAB
:
2651 symtab
= &cmd
->command
.symtab
;
2653 case BFD_MACH_O_LC_DYSYMTAB
:
2654 dysymtab
= &cmd
->command
.dysymtab
;
2656 case BFD_MACH_O_LC_DYLD_INFO
:
2658 bfd_mach_o_dyld_info_command
*di
= &cmd
->command
.dyld_info
;
2660 if (di
->rebase_size
!= 0)
2662 di
->rebase_off
= mdata
->filelen
;
2663 mdata
->filelen
+= di
->rebase_size
;
2665 if (di
->bind_size
!= 0)
2667 di
->bind_off
= mdata
->filelen
;
2668 mdata
->filelen
+= di
->bind_size
;
2670 if (di
->weak_bind_size
!= 0)
2672 di
->weak_bind_off
= mdata
->filelen
;
2673 mdata
->filelen
+= di
->weak_bind_size
;
2675 if (di
->lazy_bind_size
!= 0)
2677 di
->lazy_bind_off
= mdata
->filelen
;
2678 mdata
->filelen
+= di
->lazy_bind_size
;
2680 if (di
->export_size
!= 0)
2682 di
->export_off
= mdata
->filelen
;
2683 mdata
->filelen
+= di
->export_size
;
2687 case BFD_MACH_O_LC_LOAD_DYLIB
:
2688 case BFD_MACH_O_LC_LOAD_DYLINKER
:
2689 case BFD_MACH_O_LC_MAIN
:
2690 /* Nothing to do. */
2694 (_("unable to allocate data for load command %#x"),
2699 /* Specially handle symtab and dysymtab. */
2701 /* Pre-allocate the symbol table (but not the string table). The reason
2702 is that the dysymtab is after the symbol table but before the string
2703 table (required by the native strip tool). */
2706 unsigned int symlen
;
2707 unsigned int wide
= bfd_mach_o_wide_p (abfd
);
2709 symlen
= wide
? BFD_MACH_O_NLIST_64_SIZE
: BFD_MACH_O_NLIST_SIZE
;
2711 /* Align for symbols. */
2712 mdata
->filelen
= FILE_ALIGN (mdata
->filelen
, wide
? 3 : 2);
2713 symtab
->symoff
= mdata
->filelen
;
2715 symtab
->nsyms
= bfd_get_symcount (abfd
);
2716 mdata
->filelen
+= symtab
->nsyms
* symlen
;
2719 /* Build the dysymtab. */
2720 if (dysymtab
!= NULL
)
2721 if (!bfd_mach_o_build_dysymtab (abfd
, dysymtab
))
2724 /* Write symtab and strtab. */
2726 if (!bfd_mach_o_write_symtab_content (abfd
, symtab
))
2729 /* Adjust linkedit size. */
2730 if (linkedit
!= NULL
)
2732 /* bfd_vma pagemask = bfd_mach_o_get_backend_data (abfd)->page_size - 1; */
2734 linkedit
->vmsize
= mdata
->filelen
- linkedit
->fileoff
;
2735 /* linkedit->vmsize = (linkedit->vmsize + pagemask) & ~pagemask; */
2736 linkedit
->filesize
= mdata
->filelen
- linkedit
->fileoff
;
2738 linkedit
->initprot
= BFD_MACH_O_PROT_READ
;
2739 linkedit
->maxprot
= BFD_MACH_O_PROT_READ
| BFD_MACH_O_PROT_WRITE
2740 | BFD_MACH_O_PROT_EXECUTE
;
2743 /* Second pass: write commands. */
2744 for (cmd
= mdata
->first_command
; cmd
!= NULL
; cmd
= cmd
->next
)
2746 struct mach_o_load_command_external raw
;
2747 unsigned long typeflag
;
2749 typeflag
= cmd
->type
| (cmd
->type_required
? BFD_MACH_O_LC_REQ_DYLD
: 0);
2751 bfd_h_put_32 (abfd
, typeflag
, raw
.cmd
);
2752 bfd_h_put_32 (abfd
, cmd
->len
, raw
.cmdsize
);
2754 if (bfd_seek (abfd
, cmd
->offset
, SEEK_SET
) != 0
2755 || bfd_bwrite (&raw
, BFD_MACH_O_LC_SIZE
, abfd
) != 8)
2760 case BFD_MACH_O_LC_SEGMENT
:
2761 if (!bfd_mach_o_write_segment_32 (abfd
, cmd
))
2764 case BFD_MACH_O_LC_SEGMENT_64
:
2765 if (!bfd_mach_o_write_segment_64 (abfd
, cmd
))
2768 case BFD_MACH_O_LC_SYMTAB
:
2769 if (!bfd_mach_o_write_symtab (abfd
, cmd
))
2772 case BFD_MACH_O_LC_DYSYMTAB
:
2773 if (!bfd_mach_o_write_dysymtab (abfd
, cmd
))
2776 case BFD_MACH_O_LC_THREAD
:
2777 case BFD_MACH_O_LC_UNIXTHREAD
:
2778 if (!bfd_mach_o_write_thread (abfd
, cmd
))
2781 case BFD_MACH_O_LC_LOAD_DYLIB
:
2782 if (!bfd_mach_o_write_dylib (abfd
, cmd
))
2785 case BFD_MACH_O_LC_LOAD_DYLINKER
:
2786 if (!bfd_mach_o_write_dylinker (abfd
, cmd
))
2789 case BFD_MACH_O_LC_MAIN
:
2790 if (!bfd_mach_o_write_main (abfd
, cmd
))
2793 case BFD_MACH_O_LC_DYLD_INFO
:
2794 if (!bfd_mach_o_write_dyld_info (abfd
, cmd
))
2799 (_("unable to write unknown load command %#x"),
2809 bfd_mach_o_append_section_to_segment (bfd_mach_o_segment_command
*seg
,
2810 bfd_mach_o_section
*s
)
2812 if (seg
->sect_head
== NULL
)
2815 seg
->sect_tail
->next
= s
;
2819 /* Create section Mach-O flags from BFD flags. */
2822 bfd_mach_o_set_section_flags_from_bfd (bfd
*abfd ATTRIBUTE_UNUSED
,
2826 bfd_mach_o_section
*s
= bfd_mach_o_get_mach_o_section (sec
);
2828 /* Create default flags. */
2829 bfd_flags
= bfd_section_flags (sec
);
2830 if ((bfd_flags
& SEC_CODE
) == SEC_CODE
)
2831 s
->flags
= BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS
2832 | BFD_MACH_O_S_ATTR_SOME_INSTRUCTIONS
2833 | BFD_MACH_O_S_REGULAR
;
2834 else if ((bfd_flags
& (SEC_ALLOC
| SEC_LOAD
)) == SEC_ALLOC
)
2835 s
->flags
= BFD_MACH_O_S_ZEROFILL
;
2836 else if (bfd_flags
& SEC_DEBUGGING
)
2837 s
->flags
= BFD_MACH_O_S_REGULAR
| BFD_MACH_O_S_ATTR_DEBUG
;
2839 s
->flags
= BFD_MACH_O_S_REGULAR
;
2843 bfd_mach_o_build_obj_seg_command (bfd
*abfd
, bfd_mach_o_segment_command
*seg
)
2845 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
2849 seg
->fileoff
= mdata
->filelen
;
2850 seg
->initprot
= BFD_MACH_O_PROT_READ
| BFD_MACH_O_PROT_WRITE
2851 | BFD_MACH_O_PROT_EXECUTE
;
2852 seg
->maxprot
= seg
->initprot
;
2854 /* Append sections to the segment.
2856 This is a little tedious, we have to honor the need to account zerofill
2857 sections after all the rest. This forces us to do the calculation of
2858 total vmsize in three passes so that any alignment increments are
2859 properly accounted. */
2860 for (i
= 0; i
< mdata
->nsects
; ++i
)
2862 bfd_mach_o_section
*s
= mdata
->sections
[i
];
2863 asection
*sec
= s
->bfdsection
;
2865 /* Although we account for zerofill section sizes in vm order, they are
2866 placed in the file in source sequence. */
2867 bfd_mach_o_append_section_to_segment (seg
, s
);
2870 /* Zerofill sections have zero file size & offset, the only content
2871 written to the file is the symbols. */
2872 if ((s
->flags
& BFD_MACH_O_SECTION_TYPE_MASK
) == BFD_MACH_O_S_ZEROFILL
2873 || ((s
->flags
& BFD_MACH_O_SECTION_TYPE_MASK
)
2874 == BFD_MACH_O_S_GB_ZEROFILL
))
2877 /* The Darwin system tools (in MH_OBJECT files, at least) always account
2878 sections, even those with zero size. */
2881 seg
->vmsize
= FILE_ALIGN (seg
->vmsize
, s
->align
);
2882 seg
->vmsize
+= s
->size
;
2884 /* MH_OBJECT files have unaligned content. */
2887 seg
->filesize
= FILE_ALIGN (seg
->filesize
, s
->align
);
2888 mdata
->filelen
= FILE_ALIGN (mdata
->filelen
, s
->align
);
2890 seg
->filesize
+= s
->size
;
2892 /* The system tools write even zero-sized sections with an offset
2893 field set to the current file position. */
2894 s
->offset
= mdata
->filelen
;
2897 sec
->filepos
= s
->offset
;
2898 mdata
->filelen
+= s
->size
;
2901 /* Now pass through again, for zerofill, only now we just update the
2902 vmsize, and then for zerofill_GB. */
2903 for (j
= 0; j
< 2; j
++)
2908 stype
= BFD_MACH_O_S_ZEROFILL
;
2910 stype
= BFD_MACH_O_S_GB_ZEROFILL
;
2912 for (i
= 0; i
< mdata
->nsects
; ++i
)
2914 bfd_mach_o_section
*s
= mdata
->sections
[i
];
2916 if ((s
->flags
& BFD_MACH_O_SECTION_TYPE_MASK
) != stype
)
2921 seg
->vmsize
= FILE_ALIGN (seg
->vmsize
, s
->align
);
2922 seg
->vmsize
+= s
->size
;
2927 /* Allocate space for the relocations. */
2928 mdata
->filelen
= FILE_ALIGN (mdata
->filelen
, 2);
2930 for (i
= 0; i
< mdata
->nsects
; ++i
)
2932 bfd_mach_o_section
*ms
= mdata
->sections
[i
];
2933 asection
*sec
= ms
->bfdsection
;
2935 ms
->nreloc
= sec
->reloc_count
;
2936 if (ms
->nreloc
== 0)
2938 /* Clear nreloc and reloff if there is no relocs. */
2942 sec
->rel_filepos
= mdata
->filelen
;
2943 ms
->reloff
= sec
->rel_filepos
;
2944 mdata
->filelen
+= sec
->reloc_count
* BFD_MACH_O_RELENT_SIZE
;
2951 bfd_mach_o_build_exec_seg_command (bfd
*abfd
, bfd_mach_o_segment_command
*seg
)
2953 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
2955 bfd_vma pagemask
= bfd_mach_o_get_backend_data (abfd
)->page_size
- 1;
2957 bfd_mach_o_section
*s
;
2961 seg
->fileoff
= mdata
->filelen
;
2966 /* Append sections to the segment. We assume they are properly ordered
2967 by vma (but we check that). */
2969 for (i
= 0; i
< mdata
->nsects
; ++i
)
2971 s
= mdata
->sections
[i
];
2973 /* Consider only sections for this segment. */
2974 if (strcmp (seg
->segname
, s
->segname
) != 0)
2977 bfd_mach_o_append_section_to_segment (seg
, s
);
2982 /* xgettext:c-format */
2983 (_("section address (%#" PRIx64
") "
2984 "below start of segment (%#" PRIx64
")"),
2985 (uint64_t) s
->addr
, (uint64_t) vma
);
2989 vma
= s
->addr
+ s
->size
;
2992 /* Set segment file offset: make it page aligned. */
2993 vma
= seg
->sect_head
->addr
;
2994 seg
->vmaddr
= vma
& ~pagemask
;
2995 if ((mdata
->filelen
& pagemask
) > (vma
& pagemask
))
2996 mdata
->filelen
+= pagemask
+ 1;
2997 seg
->fileoff
= mdata
->filelen
& ~pagemask
;
2998 mdata
->filelen
= seg
->fileoff
+ (vma
& pagemask
);
3000 /* Set section file offset. */
3001 for (s
= seg
->sect_head
; s
!= NULL
; s
= s
->next
)
3003 asection
*sec
= s
->bfdsection
;
3004 flagword flags
= bfd_section_flags (sec
);
3006 /* Adjust segment size. */
3007 seg
->vmsize
= FILE_ALIGN (seg
->vmsize
, s
->align
);
3008 seg
->vmsize
+= s
->size
;
3010 /* File offset and length. */
3011 seg
->filesize
= FILE_ALIGN (seg
->filesize
, s
->align
);
3013 if ((s
->flags
& BFD_MACH_O_SECTION_TYPE_MASK
) != BFD_MACH_O_S_ZEROFILL
3014 && ((s
->flags
& BFD_MACH_O_SECTION_TYPE_MASK
)
3015 != BFD_MACH_O_S_GB_ZEROFILL
))
3017 mdata
->filelen
= FILE_ALIGN (mdata
->filelen
, s
->align
);
3019 s
->offset
= mdata
->filelen
;
3020 s
->bfdsection
->filepos
= s
->offset
;
3022 seg
->filesize
+= s
->size
;
3023 mdata
->filelen
+= s
->size
;
3028 s
->bfdsection
->filepos
= 0;
3031 /* Set protection. */
3032 if (flags
& SEC_LOAD
)
3034 if (flags
& SEC_CODE
)
3035 seg
->initprot
|= BFD_MACH_O_PROT_READ
| BFD_MACH_O_PROT_EXECUTE
;
3036 if ((flags
& (SEC_DATA
| SEC_READONLY
)) == SEC_DATA
)
3037 seg
->initprot
|= BFD_MACH_O_PROT_WRITE
| BFD_MACH_O_PROT_READ
;
3040 /* Relocs shouldn't appear in non-object files. */
3041 if (s
->bfdsection
->reloc_count
!= 0)
3046 if (seg
->initprot
!= 0)
3047 seg
->maxprot
= BFD_MACH_O_PROT_READ
| BFD_MACH_O_PROT_WRITE
3048 | BFD_MACH_O_PROT_EXECUTE
;
3052 /* Round segment size (and file size). */
3053 seg
->vmsize
= (seg
->vmsize
+ pagemask
) & ~pagemask
;
3054 seg
->filesize
= (seg
->filesize
+ pagemask
) & ~pagemask
;
3055 mdata
->filelen
= (mdata
->filelen
+ pagemask
) & ~pagemask
;
3060 /* Layout the commands: set commands size and offset, set ncmds and sizeofcmds
3061 fields in header. */
3064 bfd_mach_o_layout_commands (bfd_mach_o_data_struct
*mdata
)
3066 unsigned wide
= mach_o_wide_p (&mdata
->header
);
3067 unsigned int hdrlen
;
3069 bfd_mach_o_load_command
*cmd
;
3071 bfd_boolean ret
= TRUE
;
3073 hdrlen
= wide
? BFD_MACH_O_HEADER_64_SIZE
: BFD_MACH_O_HEADER_SIZE
;
3074 align
= wide
? 8 - 1 : 4 - 1;
3076 mdata
->header
.ncmds
= 0;
3078 for (cmd
= mdata
->first_command
; cmd
; cmd
= cmd
->next
)
3080 mdata
->header
.ncmds
++;
3081 cmd
->offset
= offset
;
3085 case BFD_MACH_O_LC_SEGMENT_64
:
3086 cmd
->len
= BFD_MACH_O_LC_SEGMENT_64_SIZE
3087 + BFD_MACH_O_SECTION_64_SIZE
* cmd
->command
.segment
.nsects
;
3089 case BFD_MACH_O_LC_SEGMENT
:
3090 cmd
->len
= BFD_MACH_O_LC_SEGMENT_SIZE
3091 + BFD_MACH_O_SECTION_SIZE
* cmd
->command
.segment
.nsects
;
3093 case BFD_MACH_O_LC_SYMTAB
:
3094 cmd
->len
= sizeof (struct mach_o_symtab_command_external
)
3095 + BFD_MACH_O_LC_SIZE
;
3097 case BFD_MACH_O_LC_DYSYMTAB
:
3098 cmd
->len
= sizeof (struct mach_o_dysymtab_command_external
)
3099 + BFD_MACH_O_LC_SIZE
;
3101 case BFD_MACH_O_LC_LOAD_DYLIB
:
3102 cmd
->len
= sizeof (struct mach_o_dylib_command_external
)
3103 + BFD_MACH_O_LC_SIZE
;
3104 cmd
->command
.dylib
.name_offset
= cmd
->len
;
3105 cmd
->len
+= strlen (cmd
->command
.dylib
.name_str
);
3106 cmd
->len
= (cmd
->len
+ align
) & ~align
;
3108 case BFD_MACH_O_LC_LOAD_DYLINKER
:
3109 cmd
->len
= sizeof (struct mach_o_str_command_external
)
3110 + BFD_MACH_O_LC_SIZE
;
3111 cmd
->command
.dylinker
.name_offset
= cmd
->len
;
3112 cmd
->len
+= strlen (cmd
->command
.dylinker
.name_str
);
3113 cmd
->len
= (cmd
->len
+ align
) & ~align
;
3115 case BFD_MACH_O_LC_MAIN
:
3116 cmd
->len
= sizeof (struct mach_o_entry_point_command_external
)
3117 + BFD_MACH_O_LC_SIZE
;
3119 case BFD_MACH_O_LC_DYLD_INFO
:
3120 cmd
->len
= sizeof (struct mach_o_dyld_info_command_external
)
3121 + BFD_MACH_O_LC_SIZE
;
3125 (_("unable to layout unknown load command %#x"),
3131 BFD_ASSERT (cmd
->len
% (align
+ 1) == 0);
3134 mdata
->header
.sizeofcmds
= offset
- hdrlen
;
3135 mdata
->filelen
= offset
;
3140 /* Subroutine of bfd_mach_o_build_commands: set type, name and nsects of a
3144 bfd_mach_o_init_segment (bfd_mach_o_data_struct
*mdata
,
3145 bfd_mach_o_load_command
*cmd
,
3146 const char *segname
, unsigned int nbr_sect
)
3148 bfd_mach_o_segment_command
*seg
= &cmd
->command
.segment
;
3149 unsigned wide
= mach_o_wide_p (&mdata
->header
);
3151 /* Init segment command. */
3152 cmd
->type
= wide
? BFD_MACH_O_LC_SEGMENT_64
: BFD_MACH_O_LC_SEGMENT
;
3153 cmd
->type_required
= FALSE
;
3155 strcpy (seg
->segname
, segname
);
3156 seg
->nsects
= nbr_sect
;
3166 seg
->sect_head
= NULL
;
3167 seg
->sect_tail
= NULL
;
3170 /* Build Mach-O load commands (currently assuming an MH_OBJECT file).
3171 TODO: Other file formats, rebuilding symtab/dysymtab commands for strip
3172 and copy functionality. */
3175 bfd_mach_o_build_commands (bfd
*abfd
)
3177 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
3178 unsigned wide
= mach_o_wide_p (&mdata
->header
);
3179 unsigned int nbr_segcmd
= 0;
3180 bfd_mach_o_load_command
*commands
;
3181 unsigned int nbr_commands
;
3182 int symtab_idx
= -1;
3183 int dysymtab_idx
= -1;
3187 /* Return now if already built. */
3188 if (mdata
->header
.ncmds
!= 0)
3191 /* Fill in the file type, if not already set. */
3192 if (mdata
->header
.filetype
== 0)
3194 if (abfd
->flags
& EXEC_P
)
3195 mdata
->header
.filetype
= BFD_MACH_O_MH_EXECUTE
;
3196 else if (abfd
->flags
& DYNAMIC
)
3197 mdata
->header
.filetype
= BFD_MACH_O_MH_DYLIB
;
3199 mdata
->header
.filetype
= BFD_MACH_O_MH_OBJECT
;
3202 /* If hasn't already been done, flatten sections list, and sort
3203 if/when required. Must be done before the symbol table is adjusted,
3204 since that depends on properly numbered sections. */
3205 if (mdata
->nsects
== 0 || mdata
->sections
== NULL
)
3206 if (! bfd_mach_o_mangle_sections (abfd
, mdata
))
3209 /* Order the symbol table, fill-in/check mach-o specific fields and
3210 partition out any indirect symbols. */
3211 if (!bfd_mach_o_mangle_symbols (abfd
))
3214 /* Segment commands. */
3215 if (mdata
->header
.filetype
== BFD_MACH_O_MH_OBJECT
)
3217 /* Only one segment for all the sections. But the segment is
3218 optional if there is no sections. */
3219 nbr_segcmd
= (mdata
->nsects
> 0) ? 1 : 0;
3223 bfd_mach_o_section
*prev_sect
= NULL
;
3225 /* One pagezero segment and one linkedit segment. */
3228 /* Create one segment for associated segment name in sections.
3229 Assume that sections with the same segment name are consecutive. */
3230 for (i
= 0; i
< mdata
->nsects
; i
++)
3232 bfd_mach_o_section
*this_sect
= mdata
->sections
[i
];
3234 if (prev_sect
== NULL
3235 || strcmp (prev_sect
->segname
, this_sect
->segname
) != 0)
3238 prev_sect
= this_sect
;
3243 nbr_commands
= nbr_segcmd
;
3245 /* One command for the symbol table (only if there are symbols. */
3246 if (bfd_get_symcount (abfd
) > 0)
3247 symtab_idx
= nbr_commands
++;
3250 This is a rather crude test for whether we should build a dysymtab. */
3251 if (bfd_mach_o_should_emit_dysymtab ()
3252 && bfd_get_symcount (abfd
))
3254 /* If there should be a case where a dysymtab could be emitted without
3255 a symtab (seems improbable), this would need amending. */
3256 dysymtab_idx
= nbr_commands
++;
3259 /* Add an entry point command. */
3260 if (mdata
->header
.filetype
== BFD_MACH_O_MH_EXECUTE
3261 && bfd_get_start_address (abfd
) != 0)
3262 main_idx
= nbr_commands
++;
3264 /* Well, we must have a header, at least. */
3265 mdata
->filelen
= wide
? BFD_MACH_O_HEADER_64_SIZE
: BFD_MACH_O_HEADER_SIZE
;
3267 /* A bit unusual, but no content is valid;
3268 as -n empty.s -o empty.o */
3269 if (nbr_commands
== 0)
3271 /* Layout commands (well none...) and set headers command fields. */
3272 return bfd_mach_o_layout_commands (mdata
);
3275 /* Create commands for segments (and symtabs), prepend them. */
3276 commands
= bfd_zalloc (abfd
, nbr_commands
* sizeof (bfd_mach_o_load_command
));
3277 if (commands
== NULL
)
3279 for (i
= 0; i
< nbr_commands
- 1; i
++)
3280 commands
[i
].next
= &commands
[i
+ 1];
3281 commands
[nbr_commands
- 1].next
= mdata
->first_command
;
3282 if (mdata
->first_command
== NULL
)
3283 mdata
->last_command
= &commands
[nbr_commands
- 1];
3284 mdata
->first_command
= &commands
[0];
3286 if (mdata
->header
.filetype
== BFD_MACH_O_MH_OBJECT
&& nbr_segcmd
!= 0)
3288 /* For object file, there is only one segment. */
3289 bfd_mach_o_init_segment (mdata
, &commands
[0], "", mdata
->nsects
);
3291 else if (nbr_segcmd
!= 0)
3293 bfd_mach_o_load_command
*cmd
;
3295 BFD_ASSERT (nbr_segcmd
>= 2);
3299 bfd_mach_o_init_segment (mdata
, cmd
, "__PAGEZERO", 0);
3301 /* Segments from sections. */
3303 for (i
= 0; i
< mdata
->nsects
;)
3305 const char *segname
= mdata
->sections
[i
]->segname
;
3306 unsigned int nbr_sect
= 1;
3308 /* Count number of sections for this segment. */
3309 for (i
++; i
< mdata
->nsects
; i
++)
3310 if (strcmp (mdata
->sections
[i
]->segname
, segname
) == 0)
3315 bfd_mach_o_init_segment (mdata
, cmd
, segname
, nbr_sect
);
3320 bfd_mach_o_init_segment (mdata
, cmd
, "__LINKEDIT", 0);
3323 if (symtab_idx
>= 0)
3325 /* Init symtab command. */
3326 bfd_mach_o_load_command
*cmd
= &commands
[symtab_idx
];
3328 cmd
->type
= BFD_MACH_O_LC_SYMTAB
;
3329 cmd
->type_required
= FALSE
;
3332 /* If required, setup symtab command, see comment above about the quality
3334 if (dysymtab_idx
>= 0)
3336 bfd_mach_o_load_command
*cmd
= &commands
[dysymtab_idx
];
3338 cmd
->type
= BFD_MACH_O_LC_DYSYMTAB
;
3339 cmd
->type_required
= FALSE
;
3342 /* Create the main command. */
3345 bfd_mach_o_load_command
*cmd
= &commands
[main_idx
];
3347 cmd
->type
= BFD_MACH_O_LC_MAIN
;
3348 cmd
->type_required
= TRUE
;
3350 cmd
->command
.main
.entryoff
= 0;
3351 cmd
->command
.main
.stacksize
= 0;
3354 /* Layout commands. */
3355 if (! bfd_mach_o_layout_commands (mdata
))
3358 /* So, now we have sized the commands and the filelen set to that.
3359 Now we can build the segment command and set the section file offsets. */
3360 if (mdata
->header
.filetype
== BFD_MACH_O_MH_OBJECT
)
3362 for (i
= 0; i
< nbr_segcmd
; i
++)
3363 if (!bfd_mach_o_build_obj_seg_command
3364 (abfd
, &commands
[i
].command
.segment
))
3371 /* Skip pagezero and linkedit segments. */
3372 for (i
= 1; i
< nbr_segcmd
- 1; i
++)
3374 bfd_mach_o_segment_command
*seg
= &commands
[i
].command
.segment
;
3376 if (!bfd_mach_o_build_exec_seg_command (abfd
, seg
))
3379 if (seg
->vmaddr
+ seg
->vmsize
> maxvma
)
3380 maxvma
= seg
->vmaddr
+ seg
->vmsize
;
3383 /* Set the size of __PAGEZERO. */
3384 commands
[0].command
.segment
.vmsize
=
3385 commands
[1].command
.segment
.vmaddr
;
3387 /* Set the vma and fileoff of __LINKEDIT. */
3388 commands
[nbr_segcmd
- 1].command
.segment
.vmaddr
= maxvma
;
3389 commands
[nbr_segcmd
- 1].command
.segment
.fileoff
= mdata
->filelen
;
3391 /* Set entry point (once segments have been laid out). */
3393 commands
[main_idx
].command
.main
.entryoff
=
3394 bfd_get_start_address (abfd
) - commands
[1].command
.segment
.vmaddr
;
3400 /* Set the contents of a section. */
3403 bfd_mach_o_set_section_contents (bfd
*abfd
,
3405 const void * location
,
3407 bfd_size_type count
)
3411 /* Trying to write the first section contents will trigger the creation of
3412 the load commands if they are not already present. */
3413 if (!abfd
->output_has_begun
&& !bfd_mach_o_build_commands (abfd
))
3419 pos
= section
->filepos
+ offset
;
3420 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
3421 || bfd_bwrite (location
, count
, abfd
) != count
)
3428 bfd_mach_o_sizeof_headers (bfd
*a ATTRIBUTE_UNUSED
,
3429 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
3434 /* Make an empty symbol. This is required only because
3435 bfd_make_section_anyway wants to create a symbol for the section. */
3438 bfd_mach_o_make_empty_symbol (bfd
*abfd
)
3440 asymbol
*new_symbol
;
3442 new_symbol
= bfd_zalloc (abfd
, sizeof (bfd_mach_o_asymbol
));
3443 if (new_symbol
== NULL
)
3445 new_symbol
->the_bfd
= abfd
;
3446 new_symbol
->udata
.i
= SYM_MACHO_FIELDS_UNSET
;
3451 bfd_mach_o_read_header (bfd
*abfd
, file_ptr hdr_off
, bfd_mach_o_header
*header
)
3453 struct mach_o_header_external raw
;
3455 bfd_vma (*get32
) (const void *) = NULL
;
3457 /* Just read the magic number. */
3458 if (bfd_seek (abfd
, hdr_off
, SEEK_SET
) != 0
3459 || bfd_bread (raw
.magic
, sizeof (raw
.magic
), abfd
) != 4)
3462 if (bfd_getb32 (raw
.magic
) == BFD_MACH_O_MH_MAGIC
)
3464 header
->byteorder
= BFD_ENDIAN_BIG
;
3465 header
->magic
= BFD_MACH_O_MH_MAGIC
;
3466 header
->version
= 1;
3469 else if (bfd_getl32 (raw
.magic
) == BFD_MACH_O_MH_MAGIC
)
3471 header
->byteorder
= BFD_ENDIAN_LITTLE
;
3472 header
->magic
= BFD_MACH_O_MH_MAGIC
;
3473 header
->version
= 1;
3476 else if (bfd_getb32 (raw
.magic
) == BFD_MACH_O_MH_MAGIC_64
)
3478 header
->byteorder
= BFD_ENDIAN_BIG
;
3479 header
->magic
= BFD_MACH_O_MH_MAGIC_64
;
3480 header
->version
= 2;
3483 else if (bfd_getl32 (raw
.magic
) == BFD_MACH_O_MH_MAGIC_64
)
3485 header
->byteorder
= BFD_ENDIAN_LITTLE
;
3486 header
->magic
= BFD_MACH_O_MH_MAGIC_64
;
3487 header
->version
= 2;
3492 header
->byteorder
= BFD_ENDIAN_UNKNOWN
;
3496 /* Once the size of the header is known, read the full header. */
3497 size
= mach_o_wide_p (header
) ?
3498 BFD_MACH_O_HEADER_64_SIZE
: BFD_MACH_O_HEADER_SIZE
;
3500 if (bfd_seek (abfd
, hdr_off
, SEEK_SET
) != 0
3501 || bfd_bread (&raw
, size
, abfd
) != size
)
3504 header
->cputype
= (*get32
) (raw
.cputype
);
3505 header
->cpusubtype
= (*get32
) (raw
.cpusubtype
);
3506 header
->filetype
= (*get32
) (raw
.filetype
);
3507 header
->ncmds
= (*get32
) (raw
.ncmds
);
3508 header
->sizeofcmds
= (*get32
) (raw
.sizeofcmds
);
3509 header
->flags
= (*get32
) (raw
.flags
);
3511 if (mach_o_wide_p (header
))
3512 header
->reserved
= (*get32
) (raw
.reserved
);
3514 header
->reserved
= 0;
3520 bfd_mach_o_new_section_hook (bfd
*abfd
, asection
*sec
)
3522 bfd_mach_o_section
*s
;
3523 unsigned bfdalign
= bfd_section_alignment (sec
);
3525 s
= bfd_mach_o_get_mach_o_section (sec
);
3529 static const mach_o_section_name_xlat
* xlat
;
3531 s
= (bfd_mach_o_section
*) bfd_zalloc (abfd
, sizeof (*s
));
3534 sec
->used_by_bfd
= s
;
3535 s
->bfdsection
= sec
;
3537 /* Create the Darwin seg/sect name pair from the bfd name.
3538 If this is a canonical name for which a specific paiting exists
3539 there will also be defined flags, type, attribute and alignment
3541 xlat
= bfd_mach_o_convert_section_name_to_mach_o (abfd
, sec
, s
);
3544 s
->flags
= xlat
->macho_sectype
| xlat
->macho_secattr
;
3545 s
->align
= xlat
->sectalign
> bfdalign
? xlat
->sectalign
3547 bfd_set_section_alignment (sec
, s
->align
);
3548 bfd_flags
= bfd_section_flags (sec
);
3549 if (bfd_flags
== SEC_NO_FLAGS
)
3550 bfd_set_section_flags (sec
, xlat
->bfd_flags
);
3553 /* Create default flags. */
3554 bfd_mach_o_set_section_flags_from_bfd (abfd
, sec
);
3557 return _bfd_generic_new_section_hook (abfd
, sec
);
3561 bfd_mach_o_init_section_from_mach_o (asection
*sec
, unsigned long prot
)
3564 bfd_mach_o_section
*section
;
3566 flags
= bfd_section_flags (sec
);
3567 section
= bfd_mach_o_get_mach_o_section (sec
);
3569 /* TODO: see if we should use the xlat system for doing this by
3570 preference and fall back to this for unknown sections. */
3572 if (flags
== SEC_NO_FLAGS
)
3574 /* Try to guess flags. */
3575 if (section
->flags
& BFD_MACH_O_S_ATTR_DEBUG
)
3576 flags
= SEC_DEBUGGING
;
3580 if ((section
->flags
& BFD_MACH_O_SECTION_TYPE_MASK
)
3581 != BFD_MACH_O_S_ZEROFILL
)
3584 if (prot
& BFD_MACH_O_PROT_EXECUTE
)
3586 if (prot
& BFD_MACH_O_PROT_WRITE
)
3588 else if (prot
& BFD_MACH_O_PROT_READ
)
3589 flags
|= SEC_READONLY
;
3595 if ((flags
& SEC_DEBUGGING
) == 0)
3599 if (section
->offset
!= 0)
3600 flags
|= SEC_HAS_CONTENTS
;
3601 if (section
->nreloc
!= 0)
3604 bfd_set_section_flags (sec
, flags
);
3606 sec
->vma
= section
->addr
;
3607 sec
->lma
= section
->addr
;
3608 sec
->size
= section
->size
;
3609 sec
->filepos
= section
->offset
;
3610 sec
->alignment_power
= section
->align
;
3611 sec
->segment_mark
= 0;
3612 sec
->reloc_count
= section
->nreloc
;
3613 sec
->rel_filepos
= section
->reloff
;
3617 bfd_mach_o_make_bfd_section (bfd
*abfd
,
3618 const unsigned char *segname
,
3619 const unsigned char *sectname
)
3624 bfd_mach_o_convert_section_name_to_bfd
3625 (abfd
, (const char *)segname
, (const char *)sectname
, &sname
, &flags
);
3629 return bfd_make_section_anyway_with_flags (abfd
, sname
, flags
);
3633 bfd_mach_o_read_section_32 (bfd
*abfd
, unsigned long prot
)
3635 struct mach_o_section_32_external raw
;
3637 bfd_mach_o_section
*section
;
3639 if (bfd_bread (&raw
, BFD_MACH_O_SECTION_SIZE
, abfd
)
3640 != BFD_MACH_O_SECTION_SIZE
)
3643 sec
= bfd_mach_o_make_bfd_section (abfd
, raw
.segname
, raw
.sectname
);
3647 section
= bfd_mach_o_get_mach_o_section (sec
);
3648 memcpy (section
->segname
, raw
.segname
, sizeof (raw
.segname
));
3649 section
->segname
[BFD_MACH_O_SEGNAME_SIZE
] = 0;
3650 memcpy (section
->sectname
, raw
.sectname
, sizeof (raw
.sectname
));
3651 section
->sectname
[BFD_MACH_O_SECTNAME_SIZE
] = 0;
3652 section
->addr
= bfd_h_get_32 (abfd
, raw
.addr
);
3653 section
->size
= bfd_h_get_32 (abfd
, raw
.size
);
3654 section
->offset
= bfd_h_get_32 (abfd
, raw
.offset
);
3655 section
->align
= bfd_h_get_32 (abfd
, raw
.align
);
3656 /* PR 17512: file: 0017eb76. */
3657 if (section
->align
> 64)
3660 (_("bfd_mach_o_read_section_32: overlarge alignment value: %#lx, "
3661 "using 32 instead"), section
->align
);
3662 section
->align
= 32;
3664 section
->reloff
= bfd_h_get_32 (abfd
, raw
.reloff
);
3665 section
->nreloc
= bfd_h_get_32 (abfd
, raw
.nreloc
);
3666 section
->flags
= bfd_h_get_32 (abfd
, raw
.flags
);
3667 section
->reserved1
= bfd_h_get_32 (abfd
, raw
.reserved1
);
3668 section
->reserved2
= bfd_h_get_32 (abfd
, raw
.reserved2
);
3669 section
->reserved3
= 0;
3671 bfd_mach_o_init_section_from_mach_o (sec
, prot
);
3677 bfd_mach_o_read_section_64 (bfd
*abfd
, unsigned long prot
)
3679 struct mach_o_section_64_external raw
;
3681 bfd_mach_o_section
*section
;
3683 if (bfd_bread (&raw
, BFD_MACH_O_SECTION_64_SIZE
, abfd
)
3684 != BFD_MACH_O_SECTION_64_SIZE
)
3687 sec
= bfd_mach_o_make_bfd_section (abfd
, raw
.segname
, raw
.sectname
);
3691 section
= bfd_mach_o_get_mach_o_section (sec
);
3692 memcpy (section
->segname
, raw
.segname
, sizeof (raw
.segname
));
3693 section
->segname
[BFD_MACH_O_SEGNAME_SIZE
] = 0;
3694 memcpy (section
->sectname
, raw
.sectname
, sizeof (raw
.sectname
));
3695 section
->sectname
[BFD_MACH_O_SECTNAME_SIZE
] = 0;
3696 section
->addr
= bfd_h_get_64 (abfd
, raw
.addr
);
3697 section
->size
= bfd_h_get_64 (abfd
, raw
.size
);
3698 section
->offset
= bfd_h_get_32 (abfd
, raw
.offset
);
3699 section
->align
= bfd_h_get_32 (abfd
, raw
.align
);
3700 if (section
->align
> 64)
3703 (_("bfd_mach_o_read_section_64: overlarge alignment value: %#lx, "
3704 "using 32 instead"), section
->align
);
3705 section
->align
= 32;
3707 section
->reloff
= bfd_h_get_32 (abfd
, raw
.reloff
);
3708 section
->nreloc
= bfd_h_get_32 (abfd
, raw
.nreloc
);
3709 section
->flags
= bfd_h_get_32 (abfd
, raw
.flags
);
3710 section
->reserved1
= bfd_h_get_32 (abfd
, raw
.reserved1
);
3711 section
->reserved2
= bfd_h_get_32 (abfd
, raw
.reserved2
);
3712 section
->reserved3
= bfd_h_get_32 (abfd
, raw
.reserved3
);
3714 bfd_mach_o_init_section_from_mach_o (sec
, prot
);
3720 bfd_mach_o_read_section (bfd
*abfd
, unsigned long prot
, unsigned int wide
)
3723 return bfd_mach_o_read_section_64 (abfd
, prot
);
3725 return bfd_mach_o_read_section_32 (abfd
, prot
);
3729 bfd_mach_o_read_symtab_symbol (bfd
*abfd
,
3730 bfd_mach_o_symtab_command
*sym
,
3731 bfd_mach_o_asymbol
*s
,
3734 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
3735 unsigned int wide
= mach_o_wide_p (&mdata
->header
);
3736 unsigned int symwidth
=
3737 wide
? BFD_MACH_O_NLIST_64_SIZE
: BFD_MACH_O_NLIST_SIZE
;
3738 unsigned int symoff
= sym
->symoff
+ (i
* symwidth
);
3739 struct mach_o_nlist_64_external raw
;
3740 unsigned char type
= -1;
3741 unsigned char section
= -1;
3743 symvalue value
= -1;
3744 unsigned long stroff
= -1;
3745 unsigned int symtype
= -1;
3747 BFD_ASSERT (sym
->strtab
!= NULL
);
3749 if (bfd_seek (abfd
, symoff
, SEEK_SET
) != 0
3750 || bfd_bread (&raw
, symwidth
, abfd
) != symwidth
)
3753 /* xgettext:c-format */
3754 (_("bfd_mach_o_read_symtab_symbol: unable to read %d bytes at %u"),
3759 stroff
= bfd_h_get_32 (abfd
, raw
.n_strx
);
3760 type
= bfd_h_get_8 (abfd
, raw
.n_type
);
3761 symtype
= type
& BFD_MACH_O_N_TYPE
;
3762 section
= bfd_h_get_8 (abfd
, raw
.n_sect
);
3763 desc
= bfd_h_get_16 (abfd
, raw
.n_desc
);
3765 value
= bfd_h_get_64 (abfd
, raw
.n_value
);
3767 value
= bfd_h_get_32 (abfd
, raw
.n_value
);
3769 if (stroff
>= sym
->strsize
)
3772 /* xgettext:c-format */
3773 (_("bfd_mach_o_read_symtab_symbol: name out of range (%lu >= %u)"),
3779 s
->symbol
.the_bfd
= abfd
;
3780 s
->symbol
.name
= sym
->strtab
+ stroff
;
3781 s
->symbol
.value
= value
;
3782 s
->symbol
.flags
= 0x0;
3783 s
->symbol
.udata
.i
= i
;
3785 s
->n_sect
= section
;
3788 if (type
& BFD_MACH_O_N_STAB
)
3790 s
->symbol
.flags
|= BSF_DEBUGGING
;
3791 s
->symbol
.section
= bfd_und_section_ptr
;
3803 if ((section
> 0) && (section
<= mdata
->nsects
))
3805 s
->symbol
.section
= mdata
->sections
[section
- 1]->bfdsection
;
3807 s
->symbol
.value
- mdata
->sections
[section
- 1]->addr
;
3814 if (type
& (BFD_MACH_O_N_PEXT
| BFD_MACH_O_N_EXT
))
3815 s
->symbol
.flags
|= BSF_GLOBAL
;
3817 s
->symbol
.flags
|= BSF_LOCAL
;
3821 case BFD_MACH_O_N_UNDF
:
3822 if (type
== (BFD_MACH_O_N_UNDF
| BFD_MACH_O_N_EXT
)
3823 && s
->symbol
.value
!= 0)
3825 /* A common symbol. */
3826 s
->symbol
.section
= bfd_com_section_ptr
;
3827 s
->symbol
.flags
= BSF_NO_FLAGS
;
3831 s
->symbol
.section
= bfd_und_section_ptr
;
3832 if (s
->n_desc
& BFD_MACH_O_N_WEAK_REF
)
3833 s
->symbol
.flags
|= BSF_WEAK
;
3836 case BFD_MACH_O_N_PBUD
:
3837 s
->symbol
.section
= bfd_und_section_ptr
;
3839 case BFD_MACH_O_N_ABS
:
3840 s
->symbol
.section
= bfd_abs_section_ptr
;
3842 case BFD_MACH_O_N_SECT
:
3843 if ((section
> 0) && (section
<= mdata
->nsects
))
3845 s
->symbol
.section
= mdata
->sections
[section
- 1]->bfdsection
;
3847 s
->symbol
.value
- mdata
->sections
[section
- 1]->addr
;
3851 /* Mach-O uses 0 to mean "no section"; not an error. */
3855 /* xgettext:c-format */
3856 (_("bfd_mach_o_read_symtab_symbol: "
3857 "symbol \"%s\" specified invalid section %d (max %lu): "
3858 "setting to undefined"),
3859 s
->symbol
.name
, section
, mdata
->nsects
);
3861 s
->symbol
.section
= bfd_und_section_ptr
;
3864 case BFD_MACH_O_N_INDR
:
3865 /* FIXME: we don't follow the BFD convention as this indirect symbol
3866 won't be followed by the referenced one. This looks harmless
3867 unless we start using the linker. */
3868 s
->symbol
.flags
|= BSF_INDIRECT
;
3869 s
->symbol
.section
= bfd_ind_section_ptr
;
3870 s
->symbol
.value
= 0;
3874 /* xgettext:c-format */
3875 (_("bfd_mach_o_read_symtab_symbol: "
3876 "symbol \"%s\" specified invalid type field 0x%x: "
3877 "setting to undefined"), s
->symbol
.name
, symtype
);
3878 s
->symbol
.section
= bfd_und_section_ptr
;
3887 bfd_mach_o_read_symtab_strtab (bfd
*abfd
)
3889 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
3890 bfd_mach_o_symtab_command
*sym
= mdata
->symtab
;
3892 /* Fail if there is no symtab. */
3896 /* Success if already loaded. */
3900 if (abfd
->flags
& BFD_IN_MEMORY
)
3902 struct bfd_in_memory
*b
;
3904 b
= (struct bfd_in_memory
*) abfd
->iostream
;
3906 if ((sym
->stroff
+ sym
->strsize
) > b
->size
)
3908 bfd_set_error (bfd_error_file_truncated
);
3911 sym
->strtab
= (char *) b
->buffer
+ sym
->stroff
;
3915 /* See PR 21840 for a reproducer. */
3916 if ((sym
->strsize
+ 1) == 0)
3918 if (bfd_seek (abfd
, sym
->stroff
, SEEK_SET
) != 0)
3920 sym
->strtab
= (char *) _bfd_alloc_and_read (abfd
, sym
->strsize
+ 1,
3922 if (sym
->strtab
== NULL
)
3925 /* Zero terminate the string table. */
3926 sym
->strtab
[sym
->strsize
] = 0;
3933 bfd_mach_o_read_symtab_symbols (bfd
*abfd
)
3935 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
3936 bfd_mach_o_symtab_command
*sym
= mdata
->symtab
;
3941 if (sym
== NULL
|| sym
->nsyms
== 0 || sym
->symbols
)
3942 /* Return now if there are no symbols or if already loaded. */
3945 filesize
= bfd_get_file_size (abfd
);
3948 unsigned int wide
= mach_o_wide_p (&mdata
->header
);
3949 unsigned int symwidth
3950 = wide
? BFD_MACH_O_NLIST_64_SIZE
: BFD_MACH_O_NLIST_SIZE
;
3952 if (sym
->symoff
> filesize
3953 || sym
->nsyms
> (filesize
- sym
->symoff
) / symwidth
)
3955 bfd_set_error (bfd_error_file_truncated
);
3960 if (_bfd_mul_overflow (sym
->nsyms
, sizeof (bfd_mach_o_asymbol
), &amt
)
3961 || (sym
->symbols
= bfd_alloc (abfd
, amt
)) == NULL
)
3963 bfd_set_error (bfd_error_no_memory
);
3968 if (!bfd_mach_o_read_symtab_strtab (abfd
))
3971 for (i
= 0; i
< sym
->nsyms
; i
++)
3972 if (!bfd_mach_o_read_symtab_symbol (abfd
, sym
, &sym
->symbols
[i
], i
))
3978 bfd_release (abfd
, sym
->symbols
);
3979 sym
->symbols
= NULL
;
3985 bfd_mach_o_i386_flavour_string (unsigned int flavour
)
3987 switch ((int) flavour
)
3989 case BFD_MACH_O_x86_THREAD_STATE32
: return "x86_THREAD_STATE32";
3990 case BFD_MACH_O_x86_FLOAT_STATE32
: return "x86_FLOAT_STATE32";
3991 case BFD_MACH_O_x86_EXCEPTION_STATE32
: return "x86_EXCEPTION_STATE32";
3992 case BFD_MACH_O_x86_THREAD_STATE64
: return "x86_THREAD_STATE64";
3993 case BFD_MACH_O_x86_FLOAT_STATE64
: return "x86_FLOAT_STATE64";
3994 case BFD_MACH_O_x86_EXCEPTION_STATE64
: return "x86_EXCEPTION_STATE64";
3995 case BFD_MACH_O_x86_THREAD_STATE
: return "x86_THREAD_STATE";
3996 case BFD_MACH_O_x86_FLOAT_STATE
: return "x86_FLOAT_STATE";
3997 case BFD_MACH_O_x86_EXCEPTION_STATE
: return "x86_EXCEPTION_STATE";
3998 case BFD_MACH_O_x86_DEBUG_STATE32
: return "x86_DEBUG_STATE32";
3999 case BFD_MACH_O_x86_DEBUG_STATE64
: return "x86_DEBUG_STATE64";
4000 case BFD_MACH_O_x86_DEBUG_STATE
: return "x86_DEBUG_STATE";
4001 case BFD_MACH_O_x86_THREAD_STATE_NONE
: return "x86_THREAD_STATE_NONE";
4002 default: return "UNKNOWN";
4007 bfd_mach_o_ppc_flavour_string (unsigned int flavour
)
4009 switch ((int) flavour
)
4011 case BFD_MACH_O_PPC_THREAD_STATE
: return "PPC_THREAD_STATE";
4012 case BFD_MACH_O_PPC_FLOAT_STATE
: return "PPC_FLOAT_STATE";
4013 case BFD_MACH_O_PPC_EXCEPTION_STATE
: return "PPC_EXCEPTION_STATE";
4014 case BFD_MACH_O_PPC_VECTOR_STATE
: return "PPC_VECTOR_STATE";
4015 case BFD_MACH_O_PPC_THREAD_STATE64
: return "PPC_THREAD_STATE64";
4016 case BFD_MACH_O_PPC_EXCEPTION_STATE64
: return "PPC_EXCEPTION_STATE64";
4017 default: return "UNKNOWN";
4021 static unsigned char *
4022 bfd_mach_o_alloc_and_read (bfd
*abfd
, file_ptr filepos
, size_t size
)
4024 if (bfd_seek (abfd
, filepos
, SEEK_SET
) != 0)
4026 return _bfd_alloc_and_read (abfd
, size
, size
);
4030 bfd_mach_o_read_dylinker (bfd
*abfd
, bfd_mach_o_load_command
*command
)
4032 bfd_mach_o_dylinker_command
*cmd
= &command
->command
.dylinker
;
4033 struct mach_o_str_command_external raw
;
4034 unsigned int nameoff
;
4035 unsigned int namelen
;
4037 if (command
->len
< sizeof (raw
) + 8)
4039 if (bfd_bread (&raw
, sizeof (raw
), abfd
) != sizeof (raw
))
4042 nameoff
= bfd_h_get_32 (abfd
, raw
.str
);
4043 if (nameoff
> command
->len
)
4046 cmd
->name_offset
= nameoff
;
4047 namelen
= command
->len
- nameoff
;
4048 nameoff
+= command
->offset
;
4049 cmd
->name_str
= (char *) bfd_mach_o_alloc_and_read (abfd
, nameoff
, namelen
);
4050 return cmd
->name_str
!= NULL
;
4054 bfd_mach_o_read_dylib (bfd
*abfd
, bfd_mach_o_load_command
*command
)
4056 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
4057 bfd_mach_o_dylib_command
*cmd
= &command
->command
.dylib
;
4058 struct mach_o_dylib_command_external raw
;
4059 unsigned int nameoff
;
4060 unsigned int namelen
;
4063 if (command
->len
< sizeof (raw
) + 8)
4065 switch (command
->type
)
4067 case BFD_MACH_O_LC_LOAD_DYLIB
:
4068 case BFD_MACH_O_LC_LAZY_LOAD_DYLIB
:
4069 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB
:
4070 case BFD_MACH_O_LC_ID_DYLIB
:
4071 case BFD_MACH_O_LC_REEXPORT_DYLIB
:
4072 case BFD_MACH_O_LC_LOAD_UPWARD_DYLIB
:
4079 if (bfd_bread (&raw
, sizeof (raw
), abfd
) != sizeof (raw
))
4082 nameoff
= bfd_h_get_32 (abfd
, raw
.name
);
4083 if (nameoff
> command
->len
)
4085 cmd
->timestamp
= bfd_h_get_32 (abfd
, raw
.timestamp
);
4086 cmd
->current_version
= bfd_h_get_32 (abfd
, raw
.current_version
);
4087 cmd
->compatibility_version
= bfd_h_get_32 (abfd
, raw
.compatibility_version
);
4089 cmd
->name_offset
= command
->offset
+ nameoff
;
4090 namelen
= command
->len
- nameoff
;
4091 pos
= mdata
->hdr_offset
+ cmd
->name_offset
;
4092 cmd
->name_str
= (char *) bfd_mach_o_alloc_and_read (abfd
, pos
, namelen
);
4093 return cmd
->name_str
!= NULL
;
4097 bfd_mach_o_read_prebound_dylib (bfd
*abfd
,
4098 bfd_mach_o_load_command
*command
)
4100 bfd_mach_o_prebound_dylib_command
*cmd
= &command
->command
.prebound_dylib
;
4101 struct mach_o_prebound_dylib_command_external raw
;
4102 unsigned int nameoff
;
4103 unsigned int modoff
;
4104 unsigned int str_len
;
4107 if (command
->len
< sizeof (raw
) + 8)
4109 if (bfd_bread (&raw
, sizeof (raw
), abfd
) != sizeof (raw
))
4112 nameoff
= bfd_h_get_32 (abfd
, raw
.name
);
4113 modoff
= bfd_h_get_32 (abfd
, raw
.linked_modules
);
4114 if (nameoff
> command
->len
|| modoff
> command
->len
)
4117 str_len
= command
->len
- sizeof (raw
);
4118 str
= _bfd_alloc_and_read (abfd
, str_len
, str_len
);
4122 cmd
->name_offset
= command
->offset
+ nameoff
;
4123 cmd
->nmodules
= bfd_h_get_32 (abfd
, raw
.nmodules
);
4124 cmd
->linked_modules_offset
= command
->offset
+ modoff
;
4126 cmd
->name_str
= (char *)str
+ nameoff
- (sizeof (raw
) + BFD_MACH_O_LC_SIZE
);
4127 cmd
->linked_modules
= str
+ modoff
- (sizeof (raw
) + BFD_MACH_O_LC_SIZE
);
4132 bfd_mach_o_read_prebind_cksum (bfd
*abfd
,
4133 bfd_mach_o_load_command
*command
)
4135 bfd_mach_o_prebind_cksum_command
*cmd
= &command
->command
.prebind_cksum
;
4136 struct mach_o_prebind_cksum_command_external raw
;
4138 if (command
->len
< sizeof (raw
) + 8)
4140 if (bfd_bread (&raw
, sizeof (raw
), abfd
) != sizeof (raw
))
4143 cmd
->cksum
= bfd_get_32 (abfd
, raw
.cksum
);
4148 bfd_mach_o_read_twolevel_hints (bfd
*abfd
,
4149 bfd_mach_o_load_command
*command
)
4151 bfd_mach_o_twolevel_hints_command
*cmd
= &command
->command
.twolevel_hints
;
4152 struct mach_o_twolevel_hints_command_external raw
;
4154 if (command
->len
< sizeof (raw
) + 8)
4156 if (bfd_bread (&raw
, sizeof (raw
), abfd
) != sizeof (raw
))
4159 cmd
->offset
= bfd_get_32 (abfd
, raw
.offset
);
4160 cmd
->nhints
= bfd_get_32 (abfd
, raw
.nhints
);
4165 bfd_mach_o_read_fvmlib (bfd
*abfd
, bfd_mach_o_load_command
*command
)
4167 bfd_mach_o_fvmlib_command
*fvm
= &command
->command
.fvmlib
;
4168 struct mach_o_fvmlib_command_external raw
;
4169 unsigned int nameoff
;
4170 unsigned int namelen
;
4172 if (command
->len
< sizeof (raw
) + 8)
4174 if (bfd_bread (&raw
, sizeof (raw
), abfd
) != sizeof (raw
))
4177 nameoff
= bfd_h_get_32 (abfd
, raw
.name
);
4178 if (nameoff
> command
->len
)
4180 fvm
->minor_version
= bfd_h_get_32 (abfd
, raw
.minor_version
);
4181 fvm
->header_addr
= bfd_h_get_32 (abfd
, raw
.header_addr
);
4183 fvm
->name_offset
= command
->offset
+ nameoff
;
4184 namelen
= command
->len
- nameoff
;
4185 fvm
->name_str
= (char *) bfd_mach_o_alloc_and_read (abfd
, fvm
->name_offset
,
4187 return fvm
->name_str
!= NULL
;
4191 bfd_mach_o_read_thread (bfd
*abfd
, bfd_mach_o_load_command
*command
)
4193 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
4194 bfd_mach_o_thread_command
*cmd
= &command
->command
.thread
;
4195 unsigned int offset
;
4196 unsigned int nflavours
;
4198 struct mach_o_thread_command_external raw
;
4201 BFD_ASSERT ((command
->type
== BFD_MACH_O_LC_THREAD
)
4202 || (command
->type
== BFD_MACH_O_LC_UNIXTHREAD
));
4204 /* Count the number of threads. */
4207 while (offset
+ sizeof (raw
) <= command
->len
)
4211 if (bfd_seek (abfd
, command
->offset
+ offset
, SEEK_SET
) != 0
4212 || bfd_bread (&raw
, sizeof (raw
), abfd
) != sizeof (raw
))
4215 count
= bfd_h_get_32 (abfd
, raw
.count
);
4216 if (count
> (unsigned) -1 / 4
4217 || command
->len
- (offset
+ sizeof (raw
)) < count
* 4)
4219 offset
+= sizeof (raw
) + count
* 4;
4222 if (nflavours
== 0 || offset
!= command
->len
)
4225 /* Allocate threads. */
4226 if (_bfd_mul_overflow (nflavours
, sizeof (bfd_mach_o_thread_flavour
), &amt
))
4228 bfd_set_error (bfd_error_file_too_big
);
4231 cmd
->flavours
= bfd_alloc (abfd
, amt
);
4232 if (cmd
->flavours
== NULL
)
4234 cmd
->nflavours
= nflavours
;
4238 while (offset
!= command
->len
)
4240 if (bfd_seek (abfd
, command
->offset
+ offset
, SEEK_SET
) != 0
4241 || bfd_bread (&raw
, sizeof (raw
), abfd
) != sizeof (raw
))
4244 cmd
->flavours
[nflavours
].flavour
= bfd_h_get_32 (abfd
, raw
.flavour
);
4245 cmd
->flavours
[nflavours
].offset
= command
->offset
+ offset
+ sizeof (raw
);
4246 cmd
->flavours
[nflavours
].size
= bfd_h_get_32 (abfd
, raw
.count
) * 4;
4247 offset
+= cmd
->flavours
[nflavours
].size
+ sizeof (raw
);
4251 for (i
= 0; i
< nflavours
; i
++)
4254 unsigned int snamelen
;
4256 const char *flavourstr
;
4257 const char *prefix
= "LC_THREAD";
4260 switch (mdata
->header
.cputype
)
4262 case BFD_MACH_O_CPU_TYPE_POWERPC
:
4263 case BFD_MACH_O_CPU_TYPE_POWERPC_64
:
4265 bfd_mach_o_ppc_flavour_string (cmd
->flavours
[i
].flavour
);
4267 case BFD_MACH_O_CPU_TYPE_I386
:
4268 case BFD_MACH_O_CPU_TYPE_X86_64
:
4270 bfd_mach_o_i386_flavour_string (cmd
->flavours
[i
].flavour
);
4273 flavourstr
= "UNKNOWN_ARCHITECTURE";
4277 snamelen
= strlen (prefix
) + 1 + 20 + 1 + strlen (flavourstr
) + 1;
4278 sname
= bfd_alloc (abfd
, snamelen
);
4284 sprintf (sname
, "%s.%s.%u", prefix
, flavourstr
, j
);
4285 if (bfd_get_section_by_name (abfd
, sname
) == NULL
)
4290 bfdsec
= bfd_make_section_with_flags (abfd
, sname
, SEC_HAS_CONTENTS
);
4294 bfdsec
->size
= cmd
->flavours
[i
].size
;
4295 bfdsec
->filepos
= cmd
->flavours
[i
].offset
;
4296 bfdsec
->alignment_power
= 0x0;
4298 cmd
->section
= bfdsec
;
4305 bfd_mach_o_read_dysymtab (bfd
*abfd
, bfd_mach_o_load_command
*command
,
4308 bfd_mach_o_dysymtab_command
*cmd
= &command
->command
.dysymtab
;
4309 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
4311 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_DYSYMTAB
);
4314 struct mach_o_dysymtab_command_external raw
;
4316 if (command
->len
< sizeof (raw
) + 8)
4318 if (bfd_bread (&raw
, sizeof (raw
), abfd
) != sizeof (raw
))
4321 cmd
->ilocalsym
= bfd_h_get_32 (abfd
, raw
.ilocalsym
);
4322 cmd
->nlocalsym
= bfd_h_get_32 (abfd
, raw
.nlocalsym
);
4323 cmd
->iextdefsym
= bfd_h_get_32 (abfd
, raw
.iextdefsym
);
4324 cmd
->nextdefsym
= bfd_h_get_32 (abfd
, raw
.nextdefsym
);
4325 cmd
->iundefsym
= bfd_h_get_32 (abfd
, raw
.iundefsym
);
4326 cmd
->nundefsym
= bfd_h_get_32 (abfd
, raw
.nundefsym
);
4327 cmd
->tocoff
= bfd_h_get_32 (abfd
, raw
.tocoff
);
4328 cmd
->ntoc
= bfd_h_get_32 (abfd
, raw
.ntoc
);
4329 cmd
->modtaboff
= bfd_h_get_32 (abfd
, raw
.modtaboff
);
4330 cmd
->nmodtab
= bfd_h_get_32 (abfd
, raw
.nmodtab
);
4331 cmd
->extrefsymoff
= bfd_h_get_32 (abfd
, raw
.extrefsymoff
);
4332 cmd
->nextrefsyms
= bfd_h_get_32 (abfd
, raw
.nextrefsyms
);
4333 cmd
->indirectsymoff
= bfd_h_get_32 (abfd
, raw
.indirectsymoff
);
4334 cmd
->nindirectsyms
= bfd_h_get_32 (abfd
, raw
.nindirectsyms
);
4335 cmd
->extreloff
= bfd_h_get_32 (abfd
, raw
.extreloff
);
4336 cmd
->nextrel
= bfd_h_get_32 (abfd
, raw
.nextrel
);
4337 cmd
->locreloff
= bfd_h_get_32 (abfd
, raw
.locreloff
);
4338 cmd
->nlocrel
= bfd_h_get_32 (abfd
, raw
.nlocrel
);
4341 if (cmd
->nmodtab
!= 0)
4344 int wide
= bfd_mach_o_wide_p (abfd
);
4345 unsigned int module_len
= wide
? 56 : 52;
4348 if (cmd
->modtaboff
> filesize
4349 || cmd
->nmodtab
> (filesize
- cmd
->modtaboff
) / module_len
)
4351 bfd_set_error (bfd_error_file_truncated
);
4354 if (_bfd_mul_overflow (cmd
->nmodtab
,
4355 sizeof (bfd_mach_o_dylib_module
), &amt
))
4357 bfd_set_error (bfd_error_file_too_big
);
4360 cmd
->dylib_module
= bfd_alloc (abfd
, amt
);
4361 if (cmd
->dylib_module
== NULL
)
4364 if (bfd_seek (abfd
, cmd
->modtaboff
, SEEK_SET
) != 0)
4367 for (i
= 0; i
< cmd
->nmodtab
; i
++)
4369 bfd_mach_o_dylib_module
*module
= &cmd
->dylib_module
[i
];
4371 unsigned char buf
[56];
4373 if (bfd_bread ((void *) buf
, module_len
, abfd
) != module_len
)
4376 module
->module_name_idx
= bfd_h_get_32 (abfd
, buf
+ 0);
4377 module
->iextdefsym
= bfd_h_get_32 (abfd
, buf
+ 4);
4378 module
->nextdefsym
= bfd_h_get_32 (abfd
, buf
+ 8);
4379 module
->irefsym
= bfd_h_get_32 (abfd
, buf
+ 12);
4380 module
->nrefsym
= bfd_h_get_32 (abfd
, buf
+ 16);
4381 module
->ilocalsym
= bfd_h_get_32 (abfd
, buf
+ 20);
4382 module
->nlocalsym
= bfd_h_get_32 (abfd
, buf
+ 24);
4383 module
->iextrel
= bfd_h_get_32 (abfd
, buf
+ 28);
4384 module
->nextrel
= bfd_h_get_32 (abfd
, buf
+ 32);
4385 v
= bfd_h_get_32 (abfd
, buf
+36);
4386 module
->iinit
= v
& 0xffff;
4387 module
->iterm
= (v
>> 16) & 0xffff;
4388 v
= bfd_h_get_32 (abfd
, buf
+ 40);
4389 module
->ninit
= v
& 0xffff;
4390 module
->nterm
= (v
>> 16) & 0xffff;
4393 module
->objc_module_info_size
= bfd_h_get_32 (abfd
, buf
+ 44);
4394 module
->objc_module_info_addr
= bfd_h_get_64 (abfd
, buf
+ 48);
4398 module
->objc_module_info_addr
= bfd_h_get_32 (abfd
, buf
+ 44);
4399 module
->objc_module_info_size
= bfd_h_get_32 (abfd
, buf
+ 48);
4408 struct mach_o_dylib_table_of_contents_external raw
;
4410 if (cmd
->tocoff
> filesize
4411 || cmd
->ntoc
> (filesize
- cmd
->tocoff
) / sizeof (raw
))
4413 bfd_set_error (bfd_error_file_truncated
);
4416 if (_bfd_mul_overflow (cmd
->ntoc
,
4417 sizeof (bfd_mach_o_dylib_table_of_content
), &amt
))
4419 bfd_set_error (bfd_error_file_too_big
);
4422 cmd
->dylib_toc
= bfd_alloc (abfd
, amt
);
4423 if (cmd
->dylib_toc
== NULL
)
4426 if (bfd_seek (abfd
, cmd
->tocoff
, SEEK_SET
) != 0)
4429 for (i
= 0; i
< cmd
->ntoc
; i
++)
4431 bfd_mach_o_dylib_table_of_content
*toc
= &cmd
->dylib_toc
[i
];
4433 if (bfd_bread (&raw
, sizeof (raw
), abfd
) != sizeof (raw
))
4436 toc
->symbol_index
= bfd_h_get_32 (abfd
, raw
.symbol_index
);
4437 toc
->module_index
= bfd_h_get_32 (abfd
, raw
.module_index
);
4441 if (cmd
->nindirectsyms
!= 0)
4446 if (cmd
->indirectsymoff
> filesize
4447 || cmd
->nindirectsyms
> (filesize
- cmd
->indirectsymoff
) / 4)
4449 bfd_set_error (bfd_error_file_truncated
);
4452 if (_bfd_mul_overflow (cmd
->nindirectsyms
, sizeof (unsigned int), &amt
))
4454 bfd_set_error (bfd_error_file_too_big
);
4457 cmd
->indirect_syms
= bfd_alloc (abfd
, amt
);
4458 if (cmd
->indirect_syms
== NULL
)
4461 if (bfd_seek (abfd
, cmd
->indirectsymoff
, SEEK_SET
) != 0)
4464 for (i
= 0; i
< cmd
->nindirectsyms
; i
++)
4466 unsigned char raw
[4];
4467 unsigned int *is
= &cmd
->indirect_syms
[i
];
4469 if (bfd_bread (raw
, sizeof (raw
), abfd
) != sizeof (raw
))
4472 *is
= bfd_h_get_32 (abfd
, raw
);
4476 if (cmd
->nextrefsyms
!= 0)
4482 if (cmd
->extrefsymoff
> filesize
4483 || cmd
->nextrefsyms
> (filesize
- cmd
->extrefsymoff
) / 4)
4485 bfd_set_error (bfd_error_file_truncated
);
4488 if (_bfd_mul_overflow (cmd
->nextrefsyms
,
4489 sizeof (bfd_mach_o_dylib_reference
), &amt
))
4491 bfd_set_error (bfd_error_file_too_big
);
4494 cmd
->ext_refs
= bfd_alloc (abfd
, amt
);
4495 if (cmd
->ext_refs
== NULL
)
4498 if (bfd_seek (abfd
, cmd
->extrefsymoff
, SEEK_SET
) != 0)
4501 for (i
= 0; i
< cmd
->nextrefsyms
; i
++)
4503 unsigned char raw
[4];
4504 bfd_mach_o_dylib_reference
*ref
= &cmd
->ext_refs
[i
];
4506 if (bfd_bread (raw
, sizeof (raw
), abfd
) != sizeof (raw
))
4509 /* Fields isym and flags are written as bit-fields, thus we need
4510 a specific processing for endianness. */
4511 v
= bfd_h_get_32 (abfd
, raw
);
4512 if (bfd_big_endian (abfd
))
4514 ref
->isym
= (v
>> 8) & 0xffffff;
4515 ref
->flags
= v
& 0xff;
4519 ref
->isym
= v
& 0xffffff;
4520 ref
->flags
= (v
>> 24) & 0xff;
4525 if (mdata
->dysymtab
)
4527 mdata
->dysymtab
= cmd
;
4533 bfd_mach_o_read_symtab (bfd
*abfd
, bfd_mach_o_load_command
*command
,
4536 bfd_mach_o_symtab_command
*symtab
= &command
->command
.symtab
;
4537 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
4538 struct mach_o_symtab_command_external raw
;
4540 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SYMTAB
);
4542 if (command
->len
< sizeof (raw
) + 8)
4544 if (bfd_bread (&raw
, sizeof (raw
), abfd
) != sizeof (raw
))
4547 symtab
->symoff
= bfd_h_get_32 (abfd
, raw
.symoff
);
4548 symtab
->nsyms
= bfd_h_get_32 (abfd
, raw
.nsyms
);
4549 symtab
->stroff
= bfd_h_get_32 (abfd
, raw
.stroff
);
4550 symtab
->strsize
= bfd_h_get_32 (abfd
, raw
.strsize
);
4551 symtab
->symbols
= NULL
;
4552 symtab
->strtab
= NULL
;
4554 if (symtab
->symoff
> filesize
4555 || symtab
->nsyms
> (filesize
- symtab
->symoff
) / BFD_MACH_O_NLIST_SIZE
4556 || symtab
->stroff
> filesize
4557 || symtab
->strsize
> filesize
- symtab
->stroff
)
4559 bfd_set_error (bfd_error_file_truncated
);
4563 if (symtab
->nsyms
!= 0)
4564 abfd
->flags
|= HAS_SYMS
;
4568 mdata
->symtab
= symtab
;
4573 bfd_mach_o_read_uuid (bfd
*abfd
, bfd_mach_o_load_command
*command
)
4575 bfd_mach_o_uuid_command
*cmd
= &command
->command
.uuid
;
4577 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_UUID
);
4579 if (command
->len
< 16 + 8)
4581 if (bfd_bread (cmd
->uuid
, 16, abfd
) != 16)
4588 bfd_mach_o_read_linkedit (bfd
*abfd
, bfd_mach_o_load_command
*command
)
4590 bfd_mach_o_linkedit_command
*cmd
= &command
->command
.linkedit
;
4591 struct mach_o_linkedit_data_command_external raw
;
4593 if (command
->len
< sizeof (raw
) + 8)
4595 if (bfd_bread (&raw
, sizeof (raw
), abfd
) != sizeof (raw
))
4598 cmd
->dataoff
= bfd_get_32 (abfd
, raw
.dataoff
);
4599 cmd
->datasize
= bfd_get_32 (abfd
, raw
.datasize
);
4604 bfd_mach_o_read_str (bfd
*abfd
, bfd_mach_o_load_command
*command
)
4606 bfd_mach_o_str_command
*cmd
= &command
->command
.str
;
4607 struct mach_o_str_command_external raw
;
4610 if (command
->len
< sizeof (raw
) + 8)
4612 if (bfd_bread (&raw
, sizeof (raw
), abfd
) != sizeof (raw
))
4615 off
= bfd_get_32 (abfd
, raw
.str
);
4616 if (off
> command
->len
)
4619 cmd
->stroff
= command
->offset
+ off
;
4620 cmd
->str_len
= command
->len
- off
;
4621 cmd
->str
= (char *) bfd_mach_o_alloc_and_read (abfd
, cmd
->stroff
,
4623 return cmd
->str
!= NULL
;
4627 bfd_mach_o_read_dyld_content (bfd
*abfd
, bfd_mach_o_dyld_info_command
*cmd
)
4629 /* Read rebase content. */
4630 if (cmd
->rebase_content
== NULL
&& cmd
->rebase_size
!= 0)
4633 = bfd_mach_o_alloc_and_read (abfd
, cmd
->rebase_off
, cmd
->rebase_size
);
4634 if (cmd
->rebase_content
== NULL
)
4638 /* Read bind content. */
4639 if (cmd
->bind_content
== NULL
&& cmd
->bind_size
!= 0)
4642 = bfd_mach_o_alloc_and_read (abfd
, cmd
->bind_off
, cmd
->bind_size
);
4643 if (cmd
->bind_content
== NULL
)
4647 /* Read weak bind content. */
4648 if (cmd
->weak_bind_content
== NULL
&& cmd
->weak_bind_size
!= 0)
4650 cmd
->weak_bind_content
= bfd_mach_o_alloc_and_read
4651 (abfd
, cmd
->weak_bind_off
, cmd
->weak_bind_size
);
4652 if (cmd
->weak_bind_content
== NULL
)
4656 /* Read lazy bind content. */
4657 if (cmd
->lazy_bind_content
== NULL
&& cmd
->lazy_bind_size
!= 0)
4659 cmd
->lazy_bind_content
= bfd_mach_o_alloc_and_read
4660 (abfd
, cmd
->lazy_bind_off
, cmd
->lazy_bind_size
);
4661 if (cmd
->lazy_bind_content
== NULL
)
4665 /* Read export content. */
4666 if (cmd
->export_content
== NULL
&& cmd
->export_size
!= 0)
4668 cmd
->export_content
= bfd_mach_o_alloc_and_read
4669 (abfd
, cmd
->export_off
, cmd
->export_size
);
4670 if (cmd
->export_content
== NULL
)
4678 bfd_mach_o_read_dyld_info (bfd
*abfd
, bfd_mach_o_load_command
*command
)
4680 bfd_mach_o_dyld_info_command
*cmd
= &command
->command
.dyld_info
;
4681 struct mach_o_dyld_info_command_external raw
;
4683 if (command
->len
< sizeof (raw
) + 8)
4685 if (bfd_bread (&raw
, sizeof (raw
), abfd
) != sizeof (raw
))
4688 cmd
->rebase_off
= bfd_get_32 (abfd
, raw
.rebase_off
);
4689 cmd
->rebase_size
= bfd_get_32 (abfd
, raw
.rebase_size
);
4690 cmd
->rebase_content
= NULL
;
4691 cmd
->bind_off
= bfd_get_32 (abfd
, raw
.bind_off
);
4692 cmd
->bind_size
= bfd_get_32 (abfd
, raw
.bind_size
);
4693 cmd
->bind_content
= NULL
;
4694 cmd
->weak_bind_off
= bfd_get_32 (abfd
, raw
.weak_bind_off
);
4695 cmd
->weak_bind_size
= bfd_get_32 (abfd
, raw
.weak_bind_size
);
4696 cmd
->weak_bind_content
= NULL
;
4697 cmd
->lazy_bind_off
= bfd_get_32 (abfd
, raw
.lazy_bind_off
);
4698 cmd
->lazy_bind_size
= bfd_get_32 (abfd
, raw
.lazy_bind_size
);
4699 cmd
->lazy_bind_content
= NULL
;
4700 cmd
->export_off
= bfd_get_32 (abfd
, raw
.export_off
);
4701 cmd
->export_size
= bfd_get_32 (abfd
, raw
.export_size
);
4702 cmd
->export_content
= NULL
;
4707 bfd_mach_o_read_version_min (bfd
*abfd
, bfd_mach_o_load_command
*command
)
4709 bfd_mach_o_version_min_command
*cmd
= &command
->command
.version_min
;
4710 struct mach_o_version_min_command_external raw
;
4712 if (command
->len
< sizeof (raw
) + 8)
4714 if (bfd_bread (&raw
, sizeof (raw
), abfd
) != sizeof (raw
))
4717 cmd
->version
= bfd_get_32 (abfd
, raw
.version
);
4718 cmd
->sdk
= bfd_get_32 (abfd
, raw
.sdk
);
4723 bfd_mach_o_read_encryption_info (bfd
*abfd
, bfd_mach_o_load_command
*command
)
4725 bfd_mach_o_encryption_info_command
*cmd
= &command
->command
.encryption_info
;
4726 struct mach_o_encryption_info_command_external raw
;
4728 if (command
->len
< sizeof (raw
) + 8)
4730 if (bfd_bread (&raw
, sizeof (raw
), abfd
) != sizeof (raw
))
4733 cmd
->cryptoff
= bfd_get_32 (abfd
, raw
.cryptoff
);
4734 cmd
->cryptsize
= bfd_get_32 (abfd
, raw
.cryptsize
);
4735 cmd
->cryptid
= bfd_get_32 (abfd
, raw
.cryptid
);
4740 bfd_mach_o_read_encryption_info_64 (bfd
*abfd
, bfd_mach_o_load_command
*command
)
4742 bfd_mach_o_encryption_info_command
*cmd
= &command
->command
.encryption_info
;
4743 struct mach_o_encryption_info_64_command_external raw
;
4745 if (command
->len
< sizeof (raw
) + 8)
4747 if (bfd_bread (&raw
, sizeof (raw
), abfd
) != sizeof (raw
))
4750 cmd
->cryptoff
= bfd_get_32 (abfd
, raw
.cryptoff
);
4751 cmd
->cryptsize
= bfd_get_32 (abfd
, raw
.cryptsize
);
4752 cmd
->cryptid
= bfd_get_32 (abfd
, raw
.cryptid
);
4757 bfd_mach_o_read_main (bfd
*abfd
, bfd_mach_o_load_command
*command
)
4759 bfd_mach_o_main_command
*cmd
= &command
->command
.main
;
4760 struct mach_o_entry_point_command_external raw
;
4762 if (command
->len
< sizeof (raw
) + 8)
4764 if (bfd_bread (&raw
, sizeof (raw
), abfd
) != sizeof (raw
))
4767 cmd
->entryoff
= bfd_get_64 (abfd
, raw
.entryoff
);
4768 cmd
->stacksize
= bfd_get_64 (abfd
, raw
.stacksize
);
4773 bfd_mach_o_read_source_version (bfd
*abfd
, bfd_mach_o_load_command
*command
)
4775 bfd_mach_o_source_version_command
*cmd
= &command
->command
.source_version
;
4776 struct mach_o_source_version_command_external raw
;
4779 if (command
->len
< sizeof (raw
) + 8)
4781 if (bfd_bread (&raw
, sizeof (raw
), abfd
) != sizeof (raw
))
4784 ver
= bfd_get_64 (abfd
, raw
.version
);
4785 /* Note: we use a serie of shift to avoid shift > 32 (for which gcc
4786 generates warnings) in case of the host doesn't support 64 bit
4788 cmd
->e
= ver
& 0x3ff;
4790 cmd
->d
= ver
& 0x3ff;
4792 cmd
->c
= ver
& 0x3ff;
4794 cmd
->b
= ver
& 0x3ff;
4796 cmd
->a
= ver
& 0xffffff;
4801 bfd_mach_o_read_note (bfd
*abfd
, bfd_mach_o_load_command
*command
)
4803 bfd_mach_o_note_command
*cmd
= &command
->command
.note
;
4804 struct mach_o_note_command_external raw
;
4806 if (command
->len
< sizeof (raw
) + 8)
4808 if (bfd_bread (&raw
, sizeof (raw
), abfd
) != sizeof (raw
))
4811 memcpy (cmd
->data_owner
, raw
.data_owner
, 16);
4812 cmd
->offset
= bfd_get_64 (abfd
, raw
.offset
);
4813 cmd
->size
= bfd_get_64 (abfd
, raw
.size
);
4818 bfd_mach_o_read_build_version (bfd
*abfd
, bfd_mach_o_load_command
*command
)
4820 bfd_mach_o_build_version_command
*cmd
= &command
->command
.build_version
;
4821 struct mach_o_build_version_command_external raw
;
4823 if (command
->len
< sizeof (raw
) + 8)
4825 if (bfd_bread (&raw
, sizeof (raw
), abfd
) != sizeof (raw
))
4828 cmd
->platform
= bfd_get_32 (abfd
, raw
.platform
);
4829 cmd
->minos
= bfd_get_32 (abfd
, raw
.minos
);
4830 cmd
->sdk
= bfd_get_32 (abfd
, raw
.sdk
);
4831 cmd
->ntools
= bfd_get_32 (abfd
, raw
.ntools
);
4836 bfd_mach_o_read_segment (bfd
*abfd
,
4837 bfd_mach_o_load_command
*command
,
4840 bfd_mach_o_segment_command
*seg
= &command
->command
.segment
;
4845 struct mach_o_segment_command_64_external raw
;
4847 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SEGMENT_64
);
4849 if (command
->len
< sizeof (raw
) + 8)
4851 if (bfd_bread (&raw
, sizeof (raw
), abfd
) != sizeof (raw
))
4854 memcpy (seg
->segname
, raw
.segname
, 16);
4855 seg
->segname
[16] = '\0';
4857 seg
->vmaddr
= bfd_h_get_64 (abfd
, raw
.vmaddr
);
4858 seg
->vmsize
= bfd_h_get_64 (abfd
, raw
.vmsize
);
4859 seg
->fileoff
= bfd_h_get_64 (abfd
, raw
.fileoff
);
4860 seg
->filesize
= bfd_h_get_64 (abfd
, raw
.filesize
);
4861 seg
->maxprot
= bfd_h_get_32 (abfd
, raw
.maxprot
);
4862 seg
->initprot
= bfd_h_get_32 (abfd
, raw
.initprot
);
4863 seg
->nsects
= bfd_h_get_32 (abfd
, raw
.nsects
);
4864 seg
->flags
= bfd_h_get_32 (abfd
, raw
.flags
);
4868 struct mach_o_segment_command_32_external raw
;
4870 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SEGMENT
);
4872 if (command
->len
< sizeof (raw
) + 8)
4874 if (bfd_bread (&raw
, sizeof (raw
), abfd
) != sizeof (raw
))
4877 memcpy (seg
->segname
, raw
.segname
, 16);
4878 seg
->segname
[16] = '\0';
4880 seg
->vmaddr
= bfd_h_get_32 (abfd
, raw
.vmaddr
);
4881 seg
->vmsize
= bfd_h_get_32 (abfd
, raw
.vmsize
);
4882 seg
->fileoff
= bfd_h_get_32 (abfd
, raw
.fileoff
);
4883 seg
->filesize
= bfd_h_get_32 (abfd
, raw
.filesize
);
4884 seg
->maxprot
= bfd_h_get_32 (abfd
, raw
.maxprot
);
4885 seg
->initprot
= bfd_h_get_32 (abfd
, raw
.initprot
);
4886 seg
->nsects
= bfd_h_get_32 (abfd
, raw
.nsects
);
4887 seg
->flags
= bfd_h_get_32 (abfd
, raw
.flags
);
4889 seg
->sect_head
= NULL
;
4890 seg
->sect_tail
= NULL
;
4892 for (i
= 0; i
< seg
->nsects
; i
++)
4896 sec
= bfd_mach_o_read_section (abfd
, seg
->initprot
, wide
);
4900 bfd_mach_o_append_section_to_segment
4901 (seg
, bfd_mach_o_get_mach_o_section (sec
));
4908 bfd_mach_o_read_segment_32 (bfd
*abfd
, bfd_mach_o_load_command
*command
)
4910 return bfd_mach_o_read_segment (abfd
, command
, 0);
4914 bfd_mach_o_read_segment_64 (bfd
*abfd
, bfd_mach_o_load_command
*command
)
4916 return bfd_mach_o_read_segment (abfd
, command
, 1);
4920 bfd_mach_o_read_command (bfd
*abfd
, bfd_mach_o_load_command
*command
,
4923 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
4924 struct mach_o_load_command_external raw
;
4927 /* Read command type and length. */
4928 if (bfd_seek (abfd
, mdata
->hdr_offset
+ command
->offset
, SEEK_SET
) != 0
4929 || bfd_bread (&raw
, BFD_MACH_O_LC_SIZE
, abfd
) != BFD_MACH_O_LC_SIZE
)
4932 cmd
= bfd_h_get_32 (abfd
, raw
.cmd
);
4933 command
->type
= cmd
& ~BFD_MACH_O_LC_REQ_DYLD
;
4934 command
->type_required
= cmd
& BFD_MACH_O_LC_REQ_DYLD
? TRUE
: FALSE
;
4935 command
->len
= bfd_h_get_32 (abfd
, raw
.cmdsize
);
4936 if (command
->len
< 8 || command
->len
% 4 != 0)
4939 switch (command
->type
)
4941 case BFD_MACH_O_LC_SEGMENT
:
4942 if (!bfd_mach_o_read_segment_32 (abfd
, command
))
4945 case BFD_MACH_O_LC_SEGMENT_64
:
4946 if (!bfd_mach_o_read_segment_64 (abfd
, command
))
4949 case BFD_MACH_O_LC_SYMTAB
:
4950 if (!bfd_mach_o_read_symtab (abfd
, command
, filesize
))
4953 case BFD_MACH_O_LC_SYMSEG
:
4955 case BFD_MACH_O_LC_THREAD
:
4956 case BFD_MACH_O_LC_UNIXTHREAD
:
4957 if (!bfd_mach_o_read_thread (abfd
, command
))
4960 case BFD_MACH_O_LC_LOAD_DYLINKER
:
4961 case BFD_MACH_O_LC_ID_DYLINKER
:
4962 case BFD_MACH_O_LC_DYLD_ENVIRONMENT
:
4963 if (!bfd_mach_o_read_dylinker (abfd
, command
))
4966 case BFD_MACH_O_LC_LOAD_DYLIB
:
4967 case BFD_MACH_O_LC_LAZY_LOAD_DYLIB
:
4968 case BFD_MACH_O_LC_ID_DYLIB
:
4969 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB
:
4970 case BFD_MACH_O_LC_REEXPORT_DYLIB
:
4971 case BFD_MACH_O_LC_LOAD_UPWARD_DYLIB
:
4972 if (!bfd_mach_o_read_dylib (abfd
, command
))
4975 case BFD_MACH_O_LC_PREBOUND_DYLIB
:
4976 if (!bfd_mach_o_read_prebound_dylib (abfd
, command
))
4979 case BFD_MACH_O_LC_LOADFVMLIB
:
4980 case BFD_MACH_O_LC_IDFVMLIB
:
4981 if (!bfd_mach_o_read_fvmlib (abfd
, command
))
4984 case BFD_MACH_O_LC_IDENT
:
4985 case BFD_MACH_O_LC_FVMFILE
:
4986 case BFD_MACH_O_LC_PREPAGE
:
4987 case BFD_MACH_O_LC_ROUTINES
:
4988 case BFD_MACH_O_LC_ROUTINES_64
:
4990 case BFD_MACH_O_LC_SUB_FRAMEWORK
:
4991 case BFD_MACH_O_LC_SUB_UMBRELLA
:
4992 case BFD_MACH_O_LC_SUB_LIBRARY
:
4993 case BFD_MACH_O_LC_SUB_CLIENT
:
4994 case BFD_MACH_O_LC_RPATH
:
4995 if (!bfd_mach_o_read_str (abfd
, command
))
4998 case BFD_MACH_O_LC_DYSYMTAB
:
4999 if (!bfd_mach_o_read_dysymtab (abfd
, command
, filesize
))
5002 case BFD_MACH_O_LC_PREBIND_CKSUM
:
5003 if (!bfd_mach_o_read_prebind_cksum (abfd
, command
))
5006 case BFD_MACH_O_LC_TWOLEVEL_HINTS
:
5007 if (!bfd_mach_o_read_twolevel_hints (abfd
, command
))
5010 case BFD_MACH_O_LC_UUID
:
5011 if (!bfd_mach_o_read_uuid (abfd
, command
))
5014 case BFD_MACH_O_LC_CODE_SIGNATURE
:
5015 case BFD_MACH_O_LC_SEGMENT_SPLIT_INFO
:
5016 case BFD_MACH_O_LC_FUNCTION_STARTS
:
5017 case BFD_MACH_O_LC_DATA_IN_CODE
:
5018 case BFD_MACH_O_LC_DYLIB_CODE_SIGN_DRS
:
5019 case BFD_MACH_O_LC_LINKER_OPTIMIZATION_HINT
:
5020 case BFD_MACH_O_LC_DYLD_EXPORTS_TRIE
:
5021 case BFD_MACH_O_LC_DYLD_CHAINED_FIXUPS
:
5022 if (!bfd_mach_o_read_linkedit (abfd
, command
))
5025 case BFD_MACH_O_LC_ENCRYPTION_INFO
:
5026 if (!bfd_mach_o_read_encryption_info (abfd
, command
))
5029 case BFD_MACH_O_LC_ENCRYPTION_INFO_64
:
5030 if (!bfd_mach_o_read_encryption_info_64 (abfd
, command
))
5033 case BFD_MACH_O_LC_DYLD_INFO
:
5034 if (!bfd_mach_o_read_dyld_info (abfd
, command
))
5037 case BFD_MACH_O_LC_VERSION_MIN_MACOSX
:
5038 case BFD_MACH_O_LC_VERSION_MIN_IPHONEOS
:
5039 case BFD_MACH_O_LC_VERSION_MIN_WATCHOS
:
5040 case BFD_MACH_O_LC_VERSION_MIN_TVOS
:
5041 if (!bfd_mach_o_read_version_min (abfd
, command
))
5044 case BFD_MACH_O_LC_MAIN
:
5045 if (!bfd_mach_o_read_main (abfd
, command
))
5048 case BFD_MACH_O_LC_SOURCE_VERSION
:
5049 if (!bfd_mach_o_read_source_version (abfd
, command
))
5052 case BFD_MACH_O_LC_LINKER_OPTIONS
:
5054 case BFD_MACH_O_LC_NOTE
:
5055 if (!bfd_mach_o_read_note (abfd
, command
))
5058 case BFD_MACH_O_LC_BUILD_VERSION
:
5059 if (!bfd_mach_o_read_build_version (abfd
, command
))
5064 _bfd_error_handler (_("%pB: unknown load command %#x"),
5065 abfd
, command
->type
);
5073 bfd_mach_o_flatten_sections (bfd
*abfd
)
5075 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
5076 bfd_mach_o_load_command
*cmd
;
5080 /* Count total number of sections. */
5083 for (cmd
= mdata
->first_command
; cmd
!= NULL
; cmd
= cmd
->next
)
5085 if (cmd
->type
== BFD_MACH_O_LC_SEGMENT
5086 || cmd
->type
== BFD_MACH_O_LC_SEGMENT_64
)
5088 bfd_mach_o_segment_command
*seg
= &cmd
->command
.segment
;
5090 mdata
->nsects
+= seg
->nsects
;
5094 /* Allocate sections array. */
5095 if (_bfd_mul_overflow (mdata
->nsects
, sizeof (bfd_mach_o_section
*), &amt
))
5097 bfd_set_error (bfd_error_file_too_big
);
5100 mdata
->sections
= bfd_alloc (abfd
, amt
);
5101 if (mdata
->sections
== NULL
&& mdata
->nsects
!= 0)
5104 /* Fill the array. */
5107 for (cmd
= mdata
->first_command
; cmd
!= NULL
; cmd
= cmd
->next
)
5109 if (cmd
->type
== BFD_MACH_O_LC_SEGMENT
5110 || cmd
->type
== BFD_MACH_O_LC_SEGMENT_64
)
5112 bfd_mach_o_segment_command
*seg
= &cmd
->command
.segment
;
5113 bfd_mach_o_section
*sec
;
5115 BFD_ASSERT (csect
+ seg
->nsects
<= mdata
->nsects
);
5117 for (sec
= seg
->sect_head
; sec
!= NULL
; sec
= sec
->next
)
5118 mdata
->sections
[csect
++] = sec
;
5125 bfd_mach_o_scan_start_address (bfd
*abfd
)
5127 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
5128 bfd_mach_o_thread_command
*thr
= NULL
;
5129 bfd_mach_o_load_command
*cmd
;
5132 for (cmd
= mdata
->first_command
; cmd
!= NULL
; cmd
= cmd
->next
)
5133 if (cmd
->type
== BFD_MACH_O_LC_THREAD
5134 || cmd
->type
== BFD_MACH_O_LC_UNIXTHREAD
)
5136 thr
= &cmd
->command
.thread
;
5139 else if (cmd
->type
== BFD_MACH_O_LC_MAIN
&& mdata
->nsects
> 1)
5141 bfd_mach_o_main_command
*main_cmd
= &cmd
->command
.main
;
5142 bfd_mach_o_section
*text_sect
= mdata
->sections
[0];
5146 abfd
->start_address
= main_cmd
->entryoff
5147 + (text_sect
->addr
- text_sect
->offset
);
5152 /* An object file has no start address, so do not fail if not found. */
5156 /* FIXME: create a subtarget hook ? */
5157 for (i
= 0; i
< thr
->nflavours
; i
++)
5159 if ((mdata
->header
.cputype
== BFD_MACH_O_CPU_TYPE_I386
)
5160 && (thr
->flavours
[i
].flavour
== BFD_MACH_O_x86_THREAD_STATE32
))
5162 unsigned char buf
[4];
5164 if (bfd_seek (abfd
, thr
->flavours
[i
].offset
+ 40, SEEK_SET
) != 0
5165 || bfd_bread (buf
, 4, abfd
) != 4)
5168 abfd
->start_address
= bfd_h_get_32 (abfd
, buf
);
5170 else if ((mdata
->header
.cputype
== BFD_MACH_O_CPU_TYPE_POWERPC
)
5171 && (thr
->flavours
[i
].flavour
== BFD_MACH_O_PPC_THREAD_STATE
))
5173 unsigned char buf
[4];
5175 if (bfd_seek (abfd
, thr
->flavours
[i
].offset
+ 0, SEEK_SET
) != 0
5176 || bfd_bread (buf
, 4, abfd
) != 4)
5179 abfd
->start_address
= bfd_h_get_32 (abfd
, buf
);
5181 else if ((mdata
->header
.cputype
== BFD_MACH_O_CPU_TYPE_POWERPC_64
)
5182 && (thr
->flavours
[i
].flavour
== BFD_MACH_O_PPC_THREAD_STATE64
))
5184 unsigned char buf
[8];
5186 if (bfd_seek (abfd
, thr
->flavours
[i
].offset
+ 0, SEEK_SET
) != 0
5187 || bfd_bread (buf
, 8, abfd
) != 8)
5190 abfd
->start_address
= bfd_h_get_64 (abfd
, buf
);
5192 else if ((mdata
->header
.cputype
== BFD_MACH_O_CPU_TYPE_X86_64
)
5193 && (thr
->flavours
[i
].flavour
== BFD_MACH_O_x86_THREAD_STATE64
))
5195 unsigned char buf
[8];
5197 if (bfd_seek (abfd
, thr
->flavours
[i
].offset
+ (16 * 8), SEEK_SET
) != 0
5198 || bfd_bread (buf
, 8, abfd
) != 8)
5201 abfd
->start_address
= bfd_h_get_64 (abfd
, buf
);
5209 bfd_mach_o_set_arch_mach (bfd
*abfd
,
5210 enum bfd_architecture arch
,
5211 unsigned long machine
)
5213 bfd_mach_o_backend_data
*bed
= bfd_mach_o_get_backend_data (abfd
);
5215 /* If this isn't the right architecture for this backend, and this
5216 isn't the generic backend, fail. */
5217 if (arch
!= bed
->arch
5218 && arch
!= bfd_arch_unknown
5219 && bed
->arch
!= bfd_arch_unknown
)
5222 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
5226 bfd_mach_o_scan (bfd
*abfd
,
5227 bfd_mach_o_header
*header
,
5228 bfd_mach_o_data_struct
*mdata
)
5231 enum bfd_architecture cpu_type
;
5232 unsigned long cpu_subtype
;
5233 unsigned int hdrsize
;
5235 hdrsize
= mach_o_wide_p (header
) ?
5236 BFD_MACH_O_HEADER_64_SIZE
: BFD_MACH_O_HEADER_SIZE
;
5238 mdata
->header
= *header
;
5240 abfd
->flags
= abfd
->flags
& BFD_IN_MEMORY
;
5241 switch (header
->filetype
)
5243 case BFD_MACH_O_MH_OBJECT
:
5244 abfd
->flags
|= HAS_RELOC
;
5246 case BFD_MACH_O_MH_EXECUTE
:
5247 abfd
->flags
|= EXEC_P
;
5249 case BFD_MACH_O_MH_DYLIB
:
5250 case BFD_MACH_O_MH_BUNDLE
:
5251 abfd
->flags
|= DYNAMIC
;
5255 abfd
->tdata
.mach_o_data
= mdata
;
5257 bfd_mach_o_convert_architecture (header
->cputype
, header
->cpusubtype
,
5258 &cpu_type
, &cpu_subtype
);
5259 if (cpu_type
== bfd_arch_unknown
)
5262 /* xgettext:c-format */
5263 (_("bfd_mach_o_scan: unknown architecture 0x%lx/0x%lx"),
5264 header
->cputype
, header
->cpusubtype
);
5268 bfd_set_arch_mach (abfd
, cpu_type
, cpu_subtype
);
5270 if (header
->ncmds
!= 0)
5272 bfd_mach_o_load_command
*cmd
;
5274 ufile_ptr filesize
= bfd_get_file_size (abfd
);
5277 filesize
= (ufile_ptr
) -1;
5279 mdata
->first_command
= NULL
;
5280 mdata
->last_command
= NULL
;
5282 if (header
->ncmds
> (filesize
- hdrsize
) / BFD_MACH_O_LC_SIZE
)
5284 bfd_set_error (bfd_error_file_truncated
);
5287 if (_bfd_mul_overflow (header
->ncmds
,
5288 sizeof (bfd_mach_o_load_command
), &amt
))
5290 bfd_set_error (bfd_error_file_too_big
);
5293 cmd
= bfd_alloc (abfd
, amt
);
5297 for (i
= 0; i
< header
->ncmds
; i
++)
5299 bfd_mach_o_load_command
*cur
= &cmd
[i
];
5301 bfd_mach_o_append_command (abfd
, cur
);
5304 cur
->offset
= hdrsize
;
5307 bfd_mach_o_load_command
*prev
= &cmd
[i
- 1];
5308 cur
->offset
= prev
->offset
+ prev
->len
;
5311 if (!bfd_mach_o_read_command (abfd
, cur
, filesize
))
5316 /* Sections should be flatten before scanning start address. */
5317 if (!bfd_mach_o_flatten_sections (abfd
))
5319 if (!bfd_mach_o_scan_start_address (abfd
))
5326 bfd_mach_o_mkobject_init (bfd
*abfd
)
5328 bfd_mach_o_data_struct
*mdata
= NULL
;
5330 mdata
= bfd_zalloc (abfd
, sizeof (bfd_mach_o_data_struct
));
5333 abfd
->tdata
.mach_o_data
= mdata
;
5335 mdata
->header
.magic
= 0;
5336 mdata
->header
.cputype
= 0;
5337 mdata
->header
.cpusubtype
= 0;
5338 mdata
->header
.filetype
= 0;
5339 mdata
->header
.ncmds
= 0;
5340 mdata
->header
.sizeofcmds
= 0;
5341 mdata
->header
.flags
= 0;
5342 mdata
->header
.byteorder
= BFD_ENDIAN_UNKNOWN
;
5343 mdata
->first_command
= NULL
;
5344 mdata
->last_command
= NULL
;
5346 mdata
->sections
= NULL
;
5347 mdata
->dyn_reloc_cache
= NULL
;
5353 bfd_mach_o_gen_mkobject (bfd
*abfd
)
5355 bfd_mach_o_data_struct
*mdata
;
5357 if (!bfd_mach_o_mkobject_init (abfd
))
5360 mdata
= bfd_mach_o_get_data (abfd
);
5361 mdata
->header
.magic
= BFD_MACH_O_MH_MAGIC
;
5362 mdata
->header
.cputype
= 0;
5363 mdata
->header
.cpusubtype
= 0;
5364 mdata
->header
.byteorder
= abfd
->xvec
->byteorder
;
5365 mdata
->header
.version
= 1;
5371 bfd_mach_o_header_p (bfd
*abfd
,
5373 bfd_mach_o_filetype file_type
,
5374 bfd_mach_o_cpu_type cpu_type
)
5376 bfd_mach_o_header header
;
5377 bfd_mach_o_data_struct
*mdata
;
5379 if (!bfd_mach_o_read_header (abfd
, hdr_off
, &header
))
5382 if (! (header
.byteorder
== BFD_ENDIAN_BIG
5383 || header
.byteorder
== BFD_ENDIAN_LITTLE
))
5385 _bfd_error_handler (_("unknown header byte-order value %#x"),
5390 if (! ((header
.byteorder
== BFD_ENDIAN_BIG
5391 && abfd
->xvec
->byteorder
== BFD_ENDIAN_BIG
5392 && abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
5393 || (header
.byteorder
== BFD_ENDIAN_LITTLE
5394 && abfd
->xvec
->byteorder
== BFD_ENDIAN_LITTLE
5395 && abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)))
5398 /* Check cputype and filetype.
5399 In case of wildcard, do not accept magics that are handled by existing
5403 if (header
.cputype
!= cpu_type
)
5409 /* Do not recognize 64 architectures if not configured for 64bit targets.
5410 This could happen only for generic targets. */
5411 if (mach_o_wide_p (&header
))
5418 if (header
.filetype
!= file_type
)
5423 switch (header
.filetype
)
5425 case BFD_MACH_O_MH_CORE
:
5426 /* Handled by core_p */
5433 mdata
= (bfd_mach_o_data_struct
*) bfd_zalloc (abfd
, sizeof (*mdata
));
5436 mdata
->hdr_offset
= hdr_off
;
5438 if (!bfd_mach_o_scan (abfd
, &header
, mdata
))
5441 return _bfd_no_cleanup
;
5444 bfd_set_error (bfd_error_wrong_format
);
5451 bfd_mach_o_gen_object_p (bfd
*abfd
)
5453 return bfd_mach_o_header_p (abfd
, 0, 0, 0);
5457 bfd_mach_o_gen_core_p (bfd
*abfd
)
5459 return bfd_mach_o_header_p (abfd
, 0, BFD_MACH_O_MH_CORE
, 0);
5462 /* Return the base address of ABFD, ie the address at which the image is
5463 mapped. The possible initial pagezero is ignored. */
5466 bfd_mach_o_get_base_address (bfd
*abfd
)
5468 bfd_mach_o_data_struct
*mdata
;
5469 bfd_mach_o_load_command
*cmd
;
5471 /* Check for Mach-O. */
5472 if (!bfd_mach_o_valid (abfd
))
5474 mdata
= bfd_mach_o_get_data (abfd
);
5476 for (cmd
= mdata
->first_command
; cmd
!= NULL
; cmd
= cmd
->next
)
5478 if ((cmd
->type
== BFD_MACH_O_LC_SEGMENT
5479 || cmd
->type
== BFD_MACH_O_LC_SEGMENT_64
))
5481 struct bfd_mach_o_segment_command
*segcmd
= &cmd
->command
.segment
;
5483 if (segcmd
->initprot
!= 0)
5484 return segcmd
->vmaddr
;
5490 typedef struct mach_o_fat_archentry
5492 unsigned long cputype
;
5493 unsigned long cpusubtype
;
5494 unsigned long offset
;
5496 unsigned long align
;
5497 } mach_o_fat_archentry
;
5499 typedef struct mach_o_fat_data_struct
5501 unsigned long magic
;
5502 unsigned long nfat_arch
;
5503 mach_o_fat_archentry
*archentries
;
5504 } mach_o_fat_data_struct
;
5507 bfd_mach_o_fat_archive_p (bfd
*abfd
)
5509 mach_o_fat_data_struct
*adata
= NULL
;
5510 struct mach_o_fat_header_external hdr
;
5514 if (bfd_seek (abfd
, 0, SEEK_SET
) != 0
5515 || bfd_bread (&hdr
, sizeof (hdr
), abfd
) != sizeof (hdr
))
5518 adata
= bfd_alloc (abfd
, sizeof (mach_o_fat_data_struct
));
5522 adata
->magic
= bfd_getb32 (hdr
.magic
);
5523 adata
->nfat_arch
= bfd_getb32 (hdr
.nfat_arch
);
5524 if (adata
->magic
!= 0xcafebabe)
5526 /* Avoid matching Java bytecode files, which have the same magic number.
5527 In the Java bytecode file format this field contains the JVM version,
5528 which starts at 43.0. */
5529 if (adata
->nfat_arch
> 30)
5532 if (_bfd_mul_overflow (adata
->nfat_arch
,
5533 sizeof (mach_o_fat_archentry
), &amt
))
5535 bfd_set_error (bfd_error_file_too_big
);
5538 adata
->archentries
= bfd_alloc (abfd
, amt
);
5539 if (adata
->archentries
== NULL
)
5542 for (i
= 0; i
< adata
->nfat_arch
; i
++)
5544 struct mach_o_fat_arch_external arch
;
5545 if (bfd_bread (&arch
, sizeof (arch
), abfd
) != sizeof (arch
))
5547 adata
->archentries
[i
].cputype
= bfd_getb32 (arch
.cputype
);
5548 adata
->archentries
[i
].cpusubtype
= bfd_getb32 (arch
.cpusubtype
);
5549 adata
->archentries
[i
].offset
= bfd_getb32 (arch
.offset
);
5550 adata
->archentries
[i
].size
= bfd_getb32 (arch
.size
);
5551 adata
->archentries
[i
].align
= bfd_getb32 (arch
.align
);
5554 abfd
->tdata
.mach_o_fat_data
= adata
;
5556 return _bfd_no_cleanup
;
5560 bfd_release (abfd
, adata
);
5561 bfd_set_error (bfd_error_wrong_format
);
5565 /* Set the filename for a fat binary member ABFD, whose bfd architecture is
5566 ARCH_TYPE/ARCH_SUBTYPE and corresponding entry in header is ENTRY.
5567 Set arelt_data and origin fields too. */
5570 bfd_mach_o_fat_member_init (bfd
*abfd
,
5571 enum bfd_architecture arch_type
,
5572 unsigned long arch_subtype
,
5573 mach_o_fat_archentry
*entry
)
5575 struct areltdata
*areltdata
;
5576 /* Create the member filename. Use ARCH_NAME. */
5577 const bfd_arch_info_type
*ap
= bfd_lookup_arch (arch_type
, arch_subtype
);
5578 const char *filename
;
5582 /* Use the architecture name if known. */
5583 filename
= bfd_set_filename (abfd
, ap
->printable_name
);
5587 /* Forge a uniq id. */
5588 char buf
[2 + 8 + 1 + 2 + 8 + 1];
5589 snprintf (buf
, sizeof (buf
), "0x%lx-0x%lx",
5590 entry
->cputype
, entry
->cpusubtype
);
5591 filename
= bfd_set_filename (abfd
, buf
);
5596 areltdata
= bfd_zmalloc (sizeof (struct areltdata
));
5597 if (areltdata
== NULL
)
5599 areltdata
->parsed_size
= entry
->size
;
5600 abfd
->arelt_data
= areltdata
;
5601 abfd
->iostream
= NULL
;
5602 abfd
->origin
= entry
->offset
;
5607 bfd_mach_o_fat_openr_next_archived_file (bfd
*archive
, bfd
*prev
)
5609 mach_o_fat_data_struct
*adata
;
5610 mach_o_fat_archentry
*entry
= NULL
;
5613 enum bfd_architecture arch_type
;
5614 unsigned long arch_subtype
;
5616 adata
= (mach_o_fat_data_struct
*) archive
->tdata
.mach_o_fat_data
;
5617 BFD_ASSERT (adata
!= NULL
);
5619 /* Find index of previous entry. */
5622 /* Start at first one. */
5627 /* Find index of PREV. */
5628 for (i
= 0; i
< adata
->nfat_arch
; i
++)
5630 if (adata
->archentries
[i
].offset
== prev
->origin
)
5634 if (i
== adata
->nfat_arch
)
5637 bfd_set_error (bfd_error_bad_value
);
5641 /* Get next entry. */
5645 if (i
>= adata
->nfat_arch
)
5647 bfd_set_error (bfd_error_no_more_archived_files
);
5651 entry
= &adata
->archentries
[i
];
5652 nbfd
= _bfd_new_bfd_contained_in (archive
);
5656 bfd_mach_o_convert_architecture (entry
->cputype
, entry
->cpusubtype
,
5657 &arch_type
, &arch_subtype
);
5659 if (!bfd_mach_o_fat_member_init (nbfd
, arch_type
, arch_subtype
, entry
))
5665 bfd_set_arch_mach (nbfd
, arch_type
, arch_subtype
);
5670 /* Analogous to stat call. */
5673 bfd_mach_o_fat_stat_arch_elt (bfd
*abfd
, struct stat
*buf
)
5675 if (abfd
->arelt_data
== NULL
)
5677 bfd_set_error (bfd_error_invalid_operation
);
5684 buf
->st_mode
= 0644;
5685 buf
->st_size
= arelt_size (abfd
);
5690 /* If ABFD format is FORMAT and architecture is ARCH, return it.
5691 If ABFD is a fat image containing a member that corresponds to FORMAT
5692 and ARCH, returns it.
5693 In other case, returns NULL.
5694 This function allows transparent uses of fat images. */
5697 bfd_mach_o_fat_extract (bfd
*abfd
,
5699 const bfd_arch_info_type
*arch
)
5702 mach_o_fat_data_struct
*adata
;
5705 if (bfd_check_format (abfd
, format
))
5707 if (bfd_get_arch_info (abfd
) == arch
)
5711 if (!bfd_check_format (abfd
, bfd_archive
)
5712 || abfd
->xvec
!= &mach_o_fat_vec
)
5715 /* This is a Mach-O fat image. */
5716 adata
= (mach_o_fat_data_struct
*) abfd
->tdata
.mach_o_fat_data
;
5717 BFD_ASSERT (adata
!= NULL
);
5719 for (i
= 0; i
< adata
->nfat_arch
; i
++)
5721 struct mach_o_fat_archentry
*e
= &adata
->archentries
[i
];
5722 enum bfd_architecture cpu_type
;
5723 unsigned long cpu_subtype
;
5725 bfd_mach_o_convert_architecture (e
->cputype
, e
->cpusubtype
,
5726 &cpu_type
, &cpu_subtype
);
5727 if (cpu_type
!= arch
->arch
|| cpu_subtype
!= arch
->mach
)
5730 /* The architecture is found. */
5731 res
= _bfd_new_bfd_contained_in (abfd
);
5735 if (bfd_mach_o_fat_member_init (res
, cpu_type
, cpu_subtype
, e
)
5736 && bfd_check_format (res
, format
))
5738 BFD_ASSERT (bfd_get_arch_info (res
) == arch
);
5749 bfd_mach_o_fat_close_and_cleanup (bfd
*abfd
)
5751 _bfd_unlink_from_archive_parent (abfd
);
5756 bfd_mach_o_lookup_command (bfd
*abfd
,
5757 bfd_mach_o_load_command_type type
,
5758 bfd_mach_o_load_command
**mcommand
)
5760 struct mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
5761 struct bfd_mach_o_load_command
*cmd
;
5764 BFD_ASSERT (mdata
!= NULL
);
5765 BFD_ASSERT (mcommand
!= NULL
);
5768 for (cmd
= mdata
->first_command
; cmd
!= NULL
; cmd
= cmd
->next
)
5770 if (cmd
->type
!= type
)
5782 bfd_mach_o_stack_addr (enum bfd_mach_o_cpu_type type
)
5786 case BFD_MACH_O_CPU_TYPE_MC680x0
:
5788 case BFD_MACH_O_CPU_TYPE_POWERPC
:
5790 case BFD_MACH_O_CPU_TYPE_I386
:
5792 case BFD_MACH_O_CPU_TYPE_SPARC
:
5794 case BFD_MACH_O_CPU_TYPE_HPPA
:
5795 return 0xc0000000 - 0x04000000;
5801 /* The following two tables should be kept, as far as possible, in order of
5802 most frequently used entries to optimize their use from gas. */
5804 const bfd_mach_o_xlat_name bfd_mach_o_section_type_name
[] =
5806 { "regular", BFD_MACH_O_S_REGULAR
},
5807 { "coalesced", BFD_MACH_O_S_COALESCED
},
5808 { "zerofill", BFD_MACH_O_S_ZEROFILL
},
5809 { "cstring_literals", BFD_MACH_O_S_CSTRING_LITERALS
},
5810 { "4byte_literals", BFD_MACH_O_S_4BYTE_LITERALS
},
5811 { "8byte_literals", BFD_MACH_O_S_8BYTE_LITERALS
},
5812 { "16byte_literals", BFD_MACH_O_S_16BYTE_LITERALS
},
5813 { "literal_pointers", BFD_MACH_O_S_LITERAL_POINTERS
},
5814 { "mod_init_func_pointers", BFD_MACH_O_S_MOD_INIT_FUNC_POINTERS
},
5815 { "mod_fini_func_pointers", BFD_MACH_O_S_MOD_FINI_FUNC_POINTERS
},
5816 { "gb_zerofill", BFD_MACH_O_S_GB_ZEROFILL
},
5817 { "interposing", BFD_MACH_O_S_INTERPOSING
},
5818 { "dtrace_dof", BFD_MACH_O_S_DTRACE_DOF
},
5819 { "non_lazy_symbol_pointers", BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS
},
5820 { "lazy_symbol_pointers", BFD_MACH_O_S_LAZY_SYMBOL_POINTERS
},
5821 { "symbol_stubs", BFD_MACH_O_S_SYMBOL_STUBS
},
5822 { "lazy_dylib_symbol_pointers", BFD_MACH_O_S_LAZY_DYLIB_SYMBOL_POINTERS
},
5826 const bfd_mach_o_xlat_name bfd_mach_o_section_attribute_name
[] =
5828 { "pure_instructions", BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS
},
5829 { "some_instructions", BFD_MACH_O_S_ATTR_SOME_INSTRUCTIONS
},
5830 { "loc_reloc", BFD_MACH_O_S_ATTR_LOC_RELOC
},
5831 { "ext_reloc", BFD_MACH_O_S_ATTR_EXT_RELOC
},
5832 { "debug", BFD_MACH_O_S_ATTR_DEBUG
},
5833 { "live_support", BFD_MACH_O_S_ATTR_LIVE_SUPPORT
},
5834 { "no_dead_strip", BFD_MACH_O_S_ATTR_NO_DEAD_STRIP
},
5835 { "strip_static_syms", BFD_MACH_O_S_ATTR_STRIP_STATIC_SYMS
},
5836 { "no_toc", BFD_MACH_O_S_ATTR_NO_TOC
},
5837 { "self_modifying_code", BFD_MACH_O_S_SELF_MODIFYING_CODE
},
5838 { "modifying_code", BFD_MACH_O_S_SELF_MODIFYING_CODE
},
5842 /* Get the section type from NAME. Return 256 if NAME is unknown. */
5845 bfd_mach_o_get_section_type_from_name (bfd
*abfd
, const char *name
)
5847 const bfd_mach_o_xlat_name
*x
;
5848 bfd_mach_o_backend_data
*bed
= bfd_mach_o_get_backend_data (abfd
);
5850 for (x
= bfd_mach_o_section_type_name
; x
->name
; x
++)
5851 if (strcmp (x
->name
, name
) == 0)
5853 /* We found it... does the target support it? */
5854 if (bed
->bfd_mach_o_section_type_valid_for_target
== NULL
5855 || bed
->bfd_mach_o_section_type_valid_for_target (x
->val
))
5856 return x
->val
; /* OK. */
5858 break; /* Not supported. */
5860 /* Maximum section ID = 0xff. */
5864 /* Get the section attribute from NAME. Return -1 if NAME is unknown. */
5867 bfd_mach_o_get_section_attribute_from_name (const char *name
)
5869 const bfd_mach_o_xlat_name
*x
;
5871 for (x
= bfd_mach_o_section_attribute_name
; x
->name
; x
++)
5872 if (strcmp (x
->name
, name
) == 0)
5874 return (unsigned int)-1;
5878 bfd_mach_o_core_fetch_environment (bfd
*abfd
,
5879 unsigned char **rbuf
,
5882 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
5883 unsigned long stackaddr
= bfd_mach_o_stack_addr (mdata
->header
.cputype
);
5884 bfd_mach_o_load_command
*cmd
;
5886 for (cmd
= mdata
->first_command
; cmd
!= NULL
; cmd
= cmd
->next
)
5888 bfd_mach_o_segment_command
*seg
;
5890 if (cmd
->type
!= BFD_MACH_O_LC_SEGMENT
)
5893 seg
= &cmd
->command
.segment
;
5895 if ((seg
->vmaddr
+ seg
->vmsize
) == stackaddr
)
5897 unsigned long start
= seg
->fileoff
;
5898 unsigned long end
= seg
->fileoff
+ seg
->filesize
;
5899 unsigned char *buf
= bfd_malloc (1024);
5900 unsigned long size
= 1024;
5906 bfd_size_type nread
= 0;
5907 unsigned long offset
;
5908 int found_nonnull
= 0;
5910 if (size
> (end
- start
))
5911 size
= (end
- start
);
5913 buf
= bfd_realloc_or_free (buf
, size
);
5917 if (bfd_seek (abfd
, end
- size
, SEEK_SET
) != 0)
5923 nread
= bfd_bread (buf
, size
, abfd
);
5931 for (offset
= 4; offset
<= size
; offset
+= 4)
5935 val
= *((unsigned long *) (buf
+ size
- offset
));
5936 if (! found_nonnull
)
5941 else if (val
== 0x0)
5943 unsigned long bottom
;
5946 bottom
= seg
->fileoff
+ seg
->filesize
- offset
;
5947 top
= seg
->fileoff
+ seg
->filesize
- 4;
5948 *rbuf
= bfd_malloc (top
- bottom
);
5951 *rlen
= top
- bottom
;
5953 memcpy (*rbuf
, buf
+ size
- *rlen
, *rlen
);
5959 if (size
== (end
- start
))
5973 bfd_mach_o_core_file_failing_command (bfd
*abfd
)
5975 unsigned char *buf
= NULL
;
5976 unsigned int len
= 0;
5979 ret
= bfd_mach_o_core_fetch_environment (abfd
, &buf
, &len
);
5983 return (char *) buf
;
5987 bfd_mach_o_core_file_failing_signal (bfd
*abfd ATTRIBUTE_UNUSED
)
5992 static bfd_mach_o_uuid_command
*
5993 bfd_mach_o_lookup_uuid_command (bfd
*abfd
)
5995 bfd_mach_o_load_command
*uuid_cmd
= NULL
;
5996 int ncmd
= bfd_mach_o_lookup_command (abfd
, BFD_MACH_O_LC_UUID
, &uuid_cmd
);
5997 if (ncmd
!= 1 || uuid_cmd
== NULL
)
5999 return &uuid_cmd
->command
.uuid
;
6002 /* Return true if ABFD is a dSYM file and its UUID matches UUID_CMD. */
6005 bfd_mach_o_dsym_for_uuid_p (bfd
*abfd
, const bfd_mach_o_uuid_command
*uuid_cmd
)
6007 bfd_mach_o_uuid_command
*dsym_uuid_cmd
;
6010 BFD_ASSERT (uuid_cmd
);
6012 if (!bfd_check_format (abfd
, bfd_object
))
6015 if (bfd_get_flavour (abfd
) != bfd_target_mach_o_flavour
6016 || bfd_mach_o_get_data (abfd
) == NULL
6017 || bfd_mach_o_get_data (abfd
)->header
.filetype
!= BFD_MACH_O_MH_DSYM
)
6020 dsym_uuid_cmd
= bfd_mach_o_lookup_uuid_command (abfd
);
6021 if (dsym_uuid_cmd
== NULL
)
6024 if (memcmp (uuid_cmd
->uuid
, dsym_uuid_cmd
->uuid
,
6025 sizeof (uuid_cmd
->uuid
)) != 0)
6031 /* Find a BFD in DSYM_FILENAME which matches ARCH and UUID_CMD.
6032 The caller is responsible for closing the returned BFD object and
6033 its my_archive if the returned BFD is in a fat dSYM. */
6036 bfd_mach_o_find_dsym (const char *dsym_filename
,
6037 const bfd_mach_o_uuid_command
*uuid_cmd
,
6038 const bfd_arch_info_type
*arch
)
6040 bfd
*base_dsym_bfd
, *dsym_bfd
;
6042 BFD_ASSERT (uuid_cmd
);
6044 base_dsym_bfd
= bfd_openr (dsym_filename
, NULL
);
6045 if (base_dsym_bfd
== NULL
)
6048 dsym_bfd
= bfd_mach_o_fat_extract (base_dsym_bfd
, bfd_object
, arch
);
6049 if (bfd_mach_o_dsym_for_uuid_p (dsym_bfd
, uuid_cmd
))
6052 bfd_close (dsym_bfd
);
6053 if (base_dsym_bfd
!= dsym_bfd
)
6054 bfd_close (base_dsym_bfd
);
6059 /* Return a BFD created from a dSYM file for ABFD.
6060 The caller is responsible for closing the returned BFD object, its
6061 filename, and its my_archive if the returned BFD is in a fat dSYM. */
6064 bfd_mach_o_follow_dsym (bfd
*abfd
)
6066 char *dsym_filename
;
6067 bfd_mach_o_uuid_command
*uuid_cmd
;
6068 bfd
*dsym_bfd
, *base_bfd
= abfd
;
6069 const char *base_basename
;
6071 if (abfd
== NULL
|| bfd_get_flavour (abfd
) != bfd_target_mach_o_flavour
)
6074 if (abfd
->my_archive
&& !bfd_is_thin_archive (abfd
->my_archive
))
6075 base_bfd
= abfd
->my_archive
;
6076 /* BFD may have been opened from a stream. */
6077 if (bfd_get_filename (base_bfd
) == NULL
)
6079 bfd_set_error (bfd_error_invalid_operation
);
6082 base_basename
= lbasename (bfd_get_filename (base_bfd
));
6084 uuid_cmd
= bfd_mach_o_lookup_uuid_command (abfd
);
6085 if (uuid_cmd
== NULL
)
6088 /* TODO: We assume the DWARF file has the same as the binary's.
6089 It seems apple's GDB checks all files in the dSYM bundle directory.
6090 http://opensource.apple.com/source/gdb/gdb-1708/src/gdb/macosx/macosx-tdep.c
6092 dsym_filename
= (char *)bfd_malloc (strlen (bfd_get_filename (base_bfd
))
6093 + strlen (dsym_subdir
) + 1
6094 + strlen (base_basename
) + 1);
6095 if (dsym_filename
== NULL
)
6098 sprintf (dsym_filename
, "%s%s/%s",
6099 bfd_get_filename (base_bfd
), dsym_subdir
, base_basename
);
6101 dsym_bfd
= bfd_mach_o_find_dsym (dsym_filename
, uuid_cmd
,
6102 bfd_get_arch_info (abfd
));
6103 if (dsym_bfd
== NULL
)
6104 free (dsym_filename
);
6110 bfd_mach_o_find_nearest_line (bfd
*abfd
,
6114 const char **filename_ptr
,
6115 const char **functionname_ptr
,
6116 unsigned int *line_ptr
,
6117 unsigned int *discriminator_ptr
)
6119 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
6122 switch (mdata
->header
.filetype
)
6124 case BFD_MACH_O_MH_OBJECT
:
6126 case BFD_MACH_O_MH_EXECUTE
:
6127 case BFD_MACH_O_MH_DYLIB
:
6128 case BFD_MACH_O_MH_BUNDLE
:
6129 case BFD_MACH_O_MH_KEXT_BUNDLE
:
6130 if (mdata
->dwarf2_find_line_info
== NULL
)
6132 mdata
->dsym_bfd
= bfd_mach_o_follow_dsym (abfd
);
6133 /* When we couldn't find dSYM for this binary, we look for
6134 the debug information in the binary itself. In this way,
6135 we won't try finding separated dSYM again because
6136 mdata->dwarf2_find_line_info will be filled. */
6137 if (! mdata
->dsym_bfd
)
6139 if (! _bfd_dwarf2_slurp_debug_info (abfd
, mdata
->dsym_bfd
,
6140 dwarf_debug_sections
, symbols
,
6141 &mdata
->dwarf2_find_line_info
,
6149 return _bfd_dwarf2_find_nearest_line (abfd
, symbols
, NULL
, section
, offset
,
6150 filename_ptr
, functionname_ptr
,
6151 line_ptr
, discriminator_ptr
,
6152 dwarf_debug_sections
,
6153 &mdata
->dwarf2_find_line_info
);
6157 bfd_mach_o_close_and_cleanup (bfd
*abfd
)
6159 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
6160 if (bfd_get_format (abfd
) == bfd_object
&& mdata
!= NULL
)
6162 _bfd_dwarf2_cleanup_debug_info (abfd
, &mdata
->dwarf2_find_line_info
);
6163 bfd_mach_o_free_cached_info (abfd
);
6164 if (mdata
->dsym_bfd
!= NULL
)
6166 bfd
*fat_bfd
= mdata
->dsym_bfd
->my_archive
;
6168 /* FIXME: PR 19435: This calculation to find the memory allocated by
6169 bfd_mach_o_follow_dsym for the filename does not always end up
6170 selecting the correct pointer. Unfortunately this problem is
6171 very hard to reproduce on a non Mach-O native system, so until it
6172 can be traced and fixed on such a system, this code will remain
6173 commented out. This does mean that there will be a memory leak,
6174 but it is small, and happens when we are closing down, so it
6175 should not matter too much. */
6176 char *dsym_filename
= (char *)(fat_bfd
6177 ? bfd_get_filename (fat_bfd
)
6178 : bfd_get_filename (mdata
->dsym_bfd
));
6180 bfd_close (mdata
->dsym_bfd
);
6181 mdata
->dsym_bfd
= NULL
;
6183 bfd_close (fat_bfd
);
6185 free (dsym_filename
);
6190 return _bfd_generic_close_and_cleanup (abfd
);
6194 bfd_mach_o_free_cached_info (bfd
*abfd
)
6196 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
6198 free (mdata
->dyn_reloc_cache
);
6199 mdata
->dyn_reloc_cache
= NULL
;
6200 for (asect
= abfd
->sections
; asect
!= NULL
; asect
= asect
->next
)
6202 free (asect
->relocation
);
6203 asect
->relocation
= NULL
;
6209 #define bfd_mach_o_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
6210 #define bfd_mach_o_bfd_reloc_name_lookup _bfd_norelocs_bfd_reloc_name_lookup
6212 #define bfd_mach_o_canonicalize_one_reloc NULL
6213 #define bfd_mach_o_swap_reloc_out NULL
6214 #define bfd_mach_o_print_thread NULL
6215 #define bfd_mach_o_tgt_seg_table NULL
6216 #define bfd_mach_o_section_type_valid_for_tgt NULL
6218 #define TARGET_NAME mach_o_be_vec
6219 #define TARGET_STRING "mach-o-be"
6220 #define TARGET_ARCHITECTURE bfd_arch_unknown
6221 #define TARGET_PAGESIZE 1
6222 #define TARGET_BIG_ENDIAN 1
6223 #define TARGET_ARCHIVE 0
6224 #define TARGET_PRIORITY 1
6225 #include "mach-o-target.c"
6228 #undef TARGET_STRING
6229 #undef TARGET_ARCHITECTURE
6230 #undef TARGET_PAGESIZE
6231 #undef TARGET_BIG_ENDIAN
6232 #undef TARGET_ARCHIVE
6233 #undef TARGET_PRIORITY
6235 #define TARGET_NAME mach_o_le_vec
6236 #define TARGET_STRING "mach-o-le"
6237 #define TARGET_ARCHITECTURE bfd_arch_unknown
6238 #define TARGET_PAGESIZE 1
6239 #define TARGET_BIG_ENDIAN 0
6240 #define TARGET_ARCHIVE 0
6241 #define TARGET_PRIORITY 1
6243 #include "mach-o-target.c"
6246 #undef TARGET_STRING
6247 #undef TARGET_ARCHITECTURE
6248 #undef TARGET_PAGESIZE
6249 #undef TARGET_BIG_ENDIAN
6250 #undef TARGET_ARCHIVE
6251 #undef TARGET_PRIORITY
6253 /* Not yet handled: creating an archive. */
6254 #define bfd_mach_o_mkarchive _bfd_noarchive_mkarchive
6256 #define bfd_mach_o_close_and_cleanup bfd_mach_o_fat_close_and_cleanup
6259 #define bfd_mach_o_generic_stat_arch_elt bfd_mach_o_fat_stat_arch_elt
6260 #define bfd_mach_o_openr_next_archived_file bfd_mach_o_fat_openr_next_archived_file
6261 #define bfd_mach_o_archive_p bfd_mach_o_fat_archive_p
6263 #define TARGET_NAME mach_o_fat_vec
6264 #define TARGET_STRING "mach-o-fat"
6265 #define TARGET_ARCHITECTURE bfd_arch_unknown
6266 #define TARGET_PAGESIZE 1
6267 #define TARGET_BIG_ENDIAN 1
6268 #define TARGET_ARCHIVE 1
6269 #define TARGET_PRIORITY 0
6271 #include "mach-o-target.c"
6274 #undef TARGET_STRING
6275 #undef TARGET_ARCHITECTURE
6276 #undef TARGET_PAGESIZE
6277 #undef TARGET_BIG_ENDIAN
6278 #undef TARGET_ARCHIVE
6279 #undef TARGET_PRIORITY