1 /* Mach-O support for BFD.
2 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
26 #include "libiberty.h"
27 #include "aout/stab_gnu.h"
31 #define BFD_IO_FUNCS 0
34 #define bfd_mach_o_mkarchive _bfd_noarchive_mkarchive
35 #define bfd_mach_o_read_ar_hdr _bfd_noarchive_read_ar_hdr
36 #define bfd_mach_o_slurp_armap _bfd_noarchive_slurp_armap
37 #define bfd_mach_o_slurp_extended_name_table _bfd_noarchive_slurp_extended_name_table
38 #define bfd_mach_o_construct_extended_name_table _bfd_noarchive_construct_extended_name_table
39 #define bfd_mach_o_truncate_arname _bfd_noarchive_truncate_arname
40 #define bfd_mach_o_write_armap _bfd_noarchive_write_armap
41 #define bfd_mach_o_get_elt_at_index _bfd_noarchive_get_elt_at_index
42 #define bfd_mach_o_generic_stat_arch_elt _bfd_noarchive_generic_stat_arch_elt
43 #define bfd_mach_o_update_armap_timestamp _bfd_noarchive_update_armap_timestamp
44 #define bfd_mach_o_close_and_cleanup _bfd_generic_close_and_cleanup
45 #define bfd_mach_o_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
46 #define bfd_mach_o_new_section_hook _bfd_generic_new_section_hook
47 #define bfd_mach_o_get_section_contents_in_window _bfd_generic_get_section_contents_in_window
48 #define bfd_mach_o_bfd_is_local_label_name _bfd_nosymbols_bfd_is_local_label_name
49 #define bfd_mach_o_bfd_is_target_special_symbol ((bfd_boolean (*) (bfd *, asymbol *)) bfd_false)
50 #define bfd_mach_o_bfd_is_local_label_name _bfd_nosymbols_bfd_is_local_label_name
51 #define bfd_mach_o_get_lineno _bfd_nosymbols_get_lineno
52 #define bfd_mach_o_find_nearest_line _bfd_nosymbols_find_nearest_line
53 #define bfd_mach_o_find_inliner_info _bfd_nosymbols_find_inliner_info
54 #define bfd_mach_o_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
55 #define bfd_mach_o_read_minisymbols _bfd_generic_read_minisymbols
56 #define bfd_mach_o_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
57 #define bfd_mach_o_bfd_get_relocated_section_contents bfd_generic_get_relocated_section_contents
58 #define bfd_mach_o_bfd_relax_section bfd_generic_relax_section
59 #define bfd_mach_o_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
60 #define bfd_mach_o_bfd_link_hash_table_free _bfd_generic_link_hash_table_free
61 #define bfd_mach_o_bfd_link_add_symbols _bfd_generic_link_add_symbols
62 #define bfd_mach_o_bfd_link_just_syms _bfd_generic_link_just_syms
63 #define bfd_mach_o_bfd_final_link _bfd_generic_final_link
64 #define bfd_mach_o_bfd_link_split_section _bfd_generic_link_split_section
65 #define bfd_mach_o_set_arch_mach bfd_default_set_arch_mach
66 #define bfd_mach_o_bfd_merge_private_bfd_data _bfd_generic_bfd_merge_private_bfd_data
67 #define bfd_mach_o_bfd_set_private_flags _bfd_generic_bfd_set_private_flags
68 #define bfd_mach_o_get_section_contents _bfd_generic_get_section_contents
69 #define bfd_mach_o_set_section_contents _bfd_generic_set_section_contents
70 #define bfd_mach_o_bfd_gc_sections bfd_generic_gc_sections
71 #define bfd_mach_o_bfd_merge_sections bfd_generic_merge_sections
72 #define bfd_mach_o_bfd_is_group_section bfd_generic_is_group_section
73 #define bfd_mach_o_bfd_discard_group bfd_generic_discard_group
74 #define bfd_mach_o_section_already_linked _bfd_generic_section_already_linked
75 #define bfd_mach_o_bfd_define_common_symbol bfd_generic_define_common_symbol
76 #define bfd_mach_o_bfd_copy_private_header_data _bfd_generic_bfd_copy_private_header_data
77 #define bfd_mach_o_core_file_matches_executable_p generic_core_file_matches_executable_p
80 bfd_mach_o_version (bfd
*abfd
)
82 bfd_mach_o_data_struct
*mdata
= NULL
;
84 BFD_ASSERT (bfd_mach_o_valid (abfd
));
85 mdata
= abfd
->tdata
.mach_o_data
;
87 return mdata
->header
.version
;
91 bfd_mach_o_valid (bfd
*abfd
)
93 if (abfd
== NULL
|| abfd
->xvec
== NULL
)
96 if (! ((abfd
->xvec
== &mach_o_be_vec
)
97 || (abfd
->xvec
== &mach_o_le_vec
)
98 || (abfd
->xvec
== &mach_o_fat_vec
)))
101 if (abfd
->tdata
.mach_o_data
== NULL
)
106 /* Copy any private info we understand from the input symbol
107 to the output symbol. */
110 bfd_mach_o_bfd_copy_private_symbol_data (bfd
*ibfd ATTRIBUTE_UNUSED
,
111 asymbol
*isymbol ATTRIBUTE_UNUSED
,
112 bfd
*obfd ATTRIBUTE_UNUSED
,
113 asymbol
*osymbol ATTRIBUTE_UNUSED
)
118 /* Copy any private info we understand from the input section
119 to the output section. */
122 bfd_mach_o_bfd_copy_private_section_data (bfd
*ibfd ATTRIBUTE_UNUSED
,
123 asection
*isection ATTRIBUTE_UNUSED
,
124 bfd
*obfd ATTRIBUTE_UNUSED
,
125 asection
*osection ATTRIBUTE_UNUSED
)
130 /* Copy any private info we understand from the input bfd
131 to the output bfd. */
134 bfd_mach_o_bfd_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
136 BFD_ASSERT (bfd_mach_o_valid (ibfd
));
137 BFD_ASSERT (bfd_mach_o_valid (obfd
));
139 obfd
->tdata
.mach_o_data
= ibfd
->tdata
.mach_o_data
;
140 obfd
->tdata
.mach_o_data
->ibfd
= ibfd
;
145 bfd_mach_o_count_symbols (bfd
*abfd
)
147 bfd_mach_o_data_struct
*mdata
= NULL
;
151 BFD_ASSERT (bfd_mach_o_valid (abfd
));
152 mdata
= abfd
->tdata
.mach_o_data
;
154 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
155 if (mdata
->commands
[i
].type
== BFD_MACH_O_LC_SYMTAB
)
157 bfd_mach_o_symtab_command
*sym
= &mdata
->commands
[i
].command
.symtab
;
165 bfd_mach_o_get_symtab_upper_bound (bfd
*abfd
)
167 long nsyms
= bfd_mach_o_count_symbols (abfd
);
172 return ((nsyms
+ 1) * sizeof (asymbol
*));
176 bfd_mach_o_canonicalize_symtab (bfd
*abfd
, asymbol
**alocation
)
178 bfd_mach_o_data_struct
*mdata
= abfd
->tdata
.mach_o_data
;
179 long nsyms
= bfd_mach_o_count_symbols (abfd
);
180 asymbol
**csym
= alocation
;
186 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
188 if (mdata
->commands
[i
].type
== BFD_MACH_O_LC_SYMTAB
)
190 bfd_mach_o_symtab_command
*sym
= &mdata
->commands
[i
].command
.symtab
;
192 if (bfd_mach_o_scan_read_symtab_symbols (abfd
, &mdata
->commands
[i
].command
.symtab
) != 0)
194 fprintf (stderr
, "bfd_mach_o_canonicalize_symtab: unable to load symbols for section %lu\n", i
);
198 BFD_ASSERT (sym
->symbols
!= NULL
);
200 for (j
= 0; j
< sym
->nsyms
; j
++)
202 BFD_ASSERT (csym
< (alocation
+ nsyms
));
203 *csym
++ = &sym
->symbols
[j
];
214 bfd_mach_o_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
218 bfd_symbol_info (symbol
, ret
);
222 bfd_mach_o_print_symbol (bfd
*abfd
,
225 bfd_print_symbol_type how
)
227 FILE *file
= (FILE *) afile
;
235 case bfd_print_symbol_name
:
236 fprintf (file
, "%s", symbol
->name
);
239 bfd_print_symbol_vandf (abfd
, (PTR
) file
, symbol
);
240 ntype
= BFD_MACH_O_SYM_NTYPE (symbol
);
241 nsect
= BFD_MACH_O_SYM_NSECT (symbol
);
242 ndesc
= BFD_MACH_O_SYM_NDESC (symbol
);
243 if (ntype
& BFD_MACH_O_N_STAB
)
244 name
= bfd_get_stab_name (ntype
);
246 switch (ntype
& BFD_MACH_O_N_TYPE
)
248 case BFD_MACH_O_N_UNDF
:
251 case BFD_MACH_O_N_ABS
:
254 case BFD_MACH_O_N_INDR
:
257 case BFD_MACH_O_N_PBUD
:
260 case BFD_MACH_O_N_SECT
:
269 fprintf (file
, " %02x %-6s %02x %04x", ntype
, name
, nsect
, ndesc
);
270 if ((ntype
& BFD_MACH_O_N_STAB
) == 0
271 && (ntype
& BFD_MACH_O_N_TYPE
) == BFD_MACH_O_N_SECT
)
272 fprintf (file
, " %-5s", symbol
->section
->name
);
273 fprintf (file
, " %s", symbol
->name
);
278 bfd_mach_o_convert_architecture (bfd_mach_o_cpu_type mtype
,
279 bfd_mach_o_cpu_subtype msubtype ATTRIBUTE_UNUSED
,
280 enum bfd_architecture
*type
,
281 unsigned long *subtype
)
283 *subtype
= bfd_arch_unknown
;
287 case BFD_MACH_O_CPU_TYPE_VAX
: *type
= bfd_arch_vax
; break;
288 case BFD_MACH_O_CPU_TYPE_MC680x0
: *type
= bfd_arch_m68k
; break;
289 case BFD_MACH_O_CPU_TYPE_I386
:
290 *type
= bfd_arch_i386
;
291 *subtype
= bfd_mach_i386_i386
;
293 case BFD_MACH_O_CPU_TYPE_X86_64
:
294 *type
= bfd_arch_i386
;
295 *subtype
= bfd_mach_x86_64
;
297 case BFD_MACH_O_CPU_TYPE_MIPS
: *type
= bfd_arch_mips
; break;
298 case BFD_MACH_O_CPU_TYPE_MC98000
: *type
= bfd_arch_m98k
; break;
299 case BFD_MACH_O_CPU_TYPE_HPPA
: *type
= bfd_arch_hppa
; break;
300 case BFD_MACH_O_CPU_TYPE_ARM
: *type
= bfd_arch_arm
; break;
301 case BFD_MACH_O_CPU_TYPE_MC88000
: *type
= bfd_arch_m88k
; break;
302 case BFD_MACH_O_CPU_TYPE_SPARC
:
303 *type
= bfd_arch_sparc
;
304 *subtype
= bfd_mach_sparc
;
306 case BFD_MACH_O_CPU_TYPE_I860
: *type
= bfd_arch_i860
; break;
307 case BFD_MACH_O_CPU_TYPE_ALPHA
: *type
= bfd_arch_alpha
; break;
308 case BFD_MACH_O_CPU_TYPE_POWERPC
:
309 *type
= bfd_arch_powerpc
;
310 *subtype
= bfd_mach_ppc
;
312 case BFD_MACH_O_CPU_TYPE_POWERPC_64
:
313 *type
= bfd_arch_powerpc
;
314 *subtype
= bfd_mach_ppc64
;
317 *type
= bfd_arch_unknown
;
323 bfd_mach_o_write_header (bfd
*abfd
, bfd_mach_o_header
*header
)
325 unsigned char buf
[32];
328 size
= (header
->version
== 2) ? 32 : 28;
330 bfd_h_put_32 (abfd
, header
->magic
, buf
+ 0);
331 bfd_h_put_32 (abfd
, header
->cputype
, buf
+ 4);
332 bfd_h_put_32 (abfd
, header
->cpusubtype
, buf
+ 8);
333 bfd_h_put_32 (abfd
, header
->filetype
, buf
+ 12);
334 bfd_h_put_32 (abfd
, header
->ncmds
, buf
+ 16);
335 bfd_h_put_32 (abfd
, header
->sizeofcmds
, buf
+ 20);
336 bfd_h_put_32 (abfd
, header
->flags
, buf
+ 24);
338 if (header
->version
== 2)
339 bfd_h_put_32 (abfd
, header
->reserved
, buf
+ 28);
341 bfd_seek (abfd
, 0, SEEK_SET
);
342 if (bfd_bwrite ((PTR
) buf
, size
, abfd
) != size
)
349 bfd_mach_o_scan_write_thread (bfd
*abfd
, bfd_mach_o_load_command
*command
)
351 bfd_mach_o_thread_command
*cmd
= &command
->command
.thread
;
353 unsigned char buf
[8];
355 unsigned int nflavours
;
357 BFD_ASSERT ((command
->type
== BFD_MACH_O_LC_THREAD
)
358 || (command
->type
== BFD_MACH_O_LC_UNIXTHREAD
));
362 for (i
= 0; i
< cmd
->nflavours
; i
++)
364 BFD_ASSERT ((cmd
->flavours
[i
].size
% 4) == 0);
365 BFD_ASSERT (cmd
->flavours
[i
].offset
== (command
->offset
+ offset
+ 8));
367 bfd_h_put_32 (abfd
, cmd
->flavours
[i
].flavour
, buf
);
368 bfd_h_put_32 (abfd
, (cmd
->flavours
[i
].size
/ 4), buf
+ 4);
370 bfd_seek (abfd
, command
->offset
+ offset
, SEEK_SET
);
371 if (bfd_bwrite ((PTR
) buf
, 8, abfd
) != 8)
374 offset
+= cmd
->flavours
[i
].size
+ 8;
381 bfd_mach_o_scan_write_section_32 (bfd
*abfd
,
382 bfd_mach_o_section
*section
,
385 unsigned char buf
[68];
387 memcpy (buf
, section
->sectname
, 16);
388 memcpy (buf
+ 16, section
->segname
, 16);
389 bfd_h_put_32 (abfd
, section
->addr
, buf
+ 32);
390 bfd_h_put_32 (abfd
, section
->size
, buf
+ 36);
391 bfd_h_put_32 (abfd
, section
->offset
, buf
+ 40);
392 bfd_h_put_32 (abfd
, section
->align
, buf
+ 44);
393 bfd_h_put_32 (abfd
, section
->reloff
, buf
+ 48);
394 bfd_h_put_32 (abfd
, section
->nreloc
, buf
+ 52);
395 bfd_h_put_32 (abfd
, section
->flags
, buf
+ 56);
396 bfd_h_put_32 (abfd
, section
->reserved1
, buf
+ 60);
397 bfd_h_put_32 (abfd
, section
->reserved2
, buf
+ 64);
399 bfd_seek (abfd
, offset
, SEEK_SET
);
400 if (bfd_bwrite ((PTR
) buf
, 68, abfd
) != 68)
407 bfd_mach_o_scan_write_section_64 (bfd
*abfd
,
408 bfd_mach_o_section
*section
,
411 unsigned char buf
[80];
413 memcpy (buf
, section
->sectname
, 16);
414 memcpy (buf
+ 16, section
->segname
, 16);
415 bfd_h_put_64 (abfd
, section
->addr
, buf
+ 32);
416 bfd_h_put_64 (abfd
, section
->size
, buf
+ 40);
417 bfd_h_put_32 (abfd
, section
->offset
, buf
+ 48);
418 bfd_h_put_32 (abfd
, section
->align
, buf
+ 52);
419 bfd_h_put_32 (abfd
, section
->reloff
, buf
+ 56);
420 bfd_h_put_32 (abfd
, section
->nreloc
, buf
+ 60);
421 bfd_h_put_32 (abfd
, section
->flags
, buf
+ 64);
422 bfd_h_put_32 (abfd
, section
->reserved1
, buf
+ 68);
423 bfd_h_put_32 (abfd
, section
->reserved2
, buf
+ 72);
424 bfd_h_put_32 (abfd
, section
->reserved3
, buf
+ 76);
426 bfd_seek (abfd
, offset
, SEEK_SET
);
427 if (bfd_bwrite ((PTR
) buf
, 80, abfd
) != 80)
434 bfd_mach_o_scan_write_section (bfd
*abfd
,
435 bfd_mach_o_section
*section
,
440 return bfd_mach_o_scan_write_section_64 (abfd
, section
, offset
);
442 return bfd_mach_o_scan_write_section_32 (abfd
, section
, offset
);
446 bfd_mach_o_scan_write_segment (bfd
*abfd
,
447 bfd_mach_o_load_command
*command
,
450 unsigned char buf
[64];
451 bfd_mach_o_segment_command
*seg
= &command
->command
.segment
;
456 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SEGMENT_64
);
458 memcpy (buf
, seg
->segname
, 16);
460 bfd_h_put_64 (abfd
, seg
->vmaddr
, buf
+ 16);
461 bfd_h_put_64 (abfd
, seg
->vmsize
, buf
+ 24);
462 bfd_h_put_64 (abfd
, seg
->fileoff
, buf
+ 32);
463 bfd_h_put_64 (abfd
, seg
->filesize
, buf
+ 40);
464 bfd_h_put_32 (abfd
, seg
->maxprot
, buf
+ 48);
465 bfd_h_put_32 (abfd
, seg
->initprot
, buf
+ 52);
466 bfd_h_put_32 (abfd
, seg
->nsects
, buf
+ 56);
467 bfd_h_put_32 (abfd
, seg
->flags
, buf
+ 60);
469 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
470 if (bfd_bwrite ((PTR
) buf
, 64, abfd
) != 64)
475 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SEGMENT
);
477 memcpy (buf
, seg
->segname
, 16);
479 bfd_h_put_32 (abfd
, seg
->vmaddr
, buf
+ 16);
480 bfd_h_put_32 (abfd
, seg
->vmsize
, buf
+ 20);
481 bfd_h_put_32 (abfd
, seg
->fileoff
, buf
+ 24);
482 bfd_h_put_32 (abfd
, seg
->filesize
, buf
+ 28);
483 bfd_h_put_32 (abfd
, seg
->maxprot
, buf
+ 32);
484 bfd_h_put_32 (abfd
, seg
->initprot
, buf
+ 36);
485 bfd_h_put_32 (abfd
, seg
->nsects
, buf
+ 40);
486 bfd_h_put_32 (abfd
, seg
->flags
, buf
+ 44);
488 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
489 if (bfd_bwrite ((PTR
) buf
, 48, abfd
) != 48)
495 bfd_vma nbytes
= seg
->filesize
;
496 bfd_vma curoff
= seg
->fileoff
;
500 bfd_vma thiswrite
= nbytes
;
502 if (thiswrite
> 1024)
505 bfd_seek (abfd
, curoff
, SEEK_SET
);
506 if (bfd_bread ((PTR
) buf
, thiswrite
, abfd
) != thiswrite
)
509 bfd_seek (abfd
, curoff
, SEEK_SET
);
510 if (bfd_bwrite ((PTR
) buf
, thiswrite
, abfd
) != thiswrite
)
518 for (i
= 0; i
< seg
->nsects
; i
++)
522 segoff
= command
->offset
+ 64 + 8 + (i
* 80);
524 segoff
= command
->offset
+ 48 + 8 + (i
* 68);
526 if (bfd_mach_o_scan_write_section
527 (abfd
, &seg
->sections
[i
], segoff
, wide
) != 0)
535 bfd_mach_o_scan_write_segment_32 (bfd
*abfd
, bfd_mach_o_load_command
*command
)
537 return bfd_mach_o_scan_write_segment (abfd
, command
, 0);
541 bfd_mach_o_scan_write_segment_64 (bfd
*abfd
, bfd_mach_o_load_command
*command
)
543 return bfd_mach_o_scan_write_segment (abfd
, command
, 1);
547 bfd_mach_o_scan_write_symtab_symbols (bfd
*abfd
, bfd_mach_o_load_command
*command
)
549 bfd_mach_o_symtab_command
*sym
= &command
->command
.symtab
;
553 for (i
= 0; i
< sym
->nsyms
; i
++)
555 unsigned char buf
[12];
556 bfd_vma symoff
= sym
->symoff
+ (i
* 12);
557 unsigned char ntype
= 0;
558 unsigned char nsect
= 0;
561 s
= &sym
->symbols
[i
];
563 /* Instead just set from the stored values. */
564 ntype
= BFD_MACH_O_SYM_NTYPE (s
);
565 nsect
= BFD_MACH_O_SYM_NSECT (s
);
566 ndesc
= BFD_MACH_O_SYM_NDESC (s
);
568 bfd_h_put_32 (abfd
, s
->name
- sym
->strtab
, buf
);
569 bfd_h_put_8 (abfd
, ntype
, buf
+ 4);
570 bfd_h_put_8 (abfd
, nsect
, buf
+ 5);
571 bfd_h_put_16 (abfd
, ndesc
, buf
+ 6);
572 bfd_h_put_32 (abfd
, s
->section
->vma
+ s
->value
, buf
+ 8);
574 bfd_seek (abfd
, symoff
, SEEK_SET
);
575 if (bfd_bwrite ((PTR
) buf
, 12, abfd
) != 12)
577 fprintf (stderr
, "bfd_mach_o_scan_write_symtab_symbols: unable to write %d bytes at %lu\n",
578 12, (unsigned long) symoff
);
587 bfd_mach_o_scan_write_symtab (bfd
*abfd
, bfd_mach_o_load_command
*command
)
589 bfd_mach_o_symtab_command
*seg
= &command
->command
.symtab
;
590 unsigned char buf
[16];
592 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SYMTAB
);
594 bfd_h_put_32 (abfd
, seg
->symoff
, buf
);
595 bfd_h_put_32 (abfd
, seg
->nsyms
, buf
+ 4);
596 bfd_h_put_32 (abfd
, seg
->stroff
, buf
+ 8);
597 bfd_h_put_32 (abfd
, seg
->strsize
, buf
+ 12);
599 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
600 if (bfd_bwrite ((PTR
) buf
, 16, abfd
) != 16)
603 if (bfd_mach_o_scan_write_symtab_symbols (abfd
, command
) != 0)
610 bfd_mach_o_write_contents (bfd
*abfd
)
615 bfd_mach_o_data_struct
*mdata
= abfd
->tdata
.mach_o_data
;
617 /* Write data sections first in case they overlap header data to be
620 for (s
= abfd
->sections
; s
!= (asection
*) NULL
; s
= s
->next
)
623 /* Now write header information. */
624 if (bfd_mach_o_write_header (abfd
, &mdata
->header
) != 0)
627 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
629 unsigned char buf
[8];
630 bfd_mach_o_load_command
*cur
= &mdata
->commands
[i
];
631 unsigned long typeflag
;
633 typeflag
= cur
->type_required
? cur
->type
& BFD_MACH_O_LC_REQ_DYLD
: cur
->type
;
635 bfd_h_put_32 (abfd
, typeflag
, buf
);
636 bfd_h_put_32 (abfd
, cur
->len
, buf
+ 4);
638 bfd_seek (abfd
, cur
->offset
, SEEK_SET
);
639 if (bfd_bwrite ((PTR
) buf
, 8, abfd
) != 8)
644 case BFD_MACH_O_LC_SEGMENT
:
645 if (bfd_mach_o_scan_write_segment_32 (abfd
, cur
) != 0)
648 case BFD_MACH_O_LC_SEGMENT_64
:
649 if (bfd_mach_o_scan_write_segment_64 (abfd
, cur
) != 0)
652 case BFD_MACH_O_LC_SYMTAB
:
653 if (bfd_mach_o_scan_write_symtab (abfd
, cur
) != 0)
656 case BFD_MACH_O_LC_SYMSEG
:
658 case BFD_MACH_O_LC_THREAD
:
659 case BFD_MACH_O_LC_UNIXTHREAD
:
660 if (bfd_mach_o_scan_write_thread (abfd
, cur
) != 0)
663 case BFD_MACH_O_LC_LOADFVMLIB
:
664 case BFD_MACH_O_LC_IDFVMLIB
:
665 case BFD_MACH_O_LC_IDENT
:
666 case BFD_MACH_O_LC_FVMFILE
:
667 case BFD_MACH_O_LC_PREPAGE
:
668 case BFD_MACH_O_LC_DYSYMTAB
:
669 case BFD_MACH_O_LC_LOAD_DYLIB
:
670 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB
:
671 case BFD_MACH_O_LC_ID_DYLIB
:
672 case BFD_MACH_O_LC_LOAD_DYLINKER
:
673 case BFD_MACH_O_LC_ID_DYLINKER
:
674 case BFD_MACH_O_LC_PREBOUND_DYLIB
:
675 case BFD_MACH_O_LC_ROUTINES
:
676 case BFD_MACH_O_LC_SUB_FRAMEWORK
:
680 "unable to write unknown load command 0x%lx\n",
681 (unsigned long) cur
->type
);
690 bfd_mach_o_sizeof_headers (bfd
*a ATTRIBUTE_UNUSED
,
691 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
696 /* Make an empty symbol. This is required only because
697 bfd_make_section_anyway wants to create a symbol for the section. */
700 bfd_mach_o_make_empty_symbol (bfd
*abfd
)
704 new = bfd_zalloc (abfd
, sizeof (* new));
712 bfd_mach_o_read_header (bfd
*abfd
, bfd_mach_o_header
*header
)
714 unsigned char buf
[32];
716 bfd_vma (*get32
) (const void *) = NULL
;
718 bfd_seek (abfd
, 0, SEEK_SET
);
720 /* Just read the magic number. */
721 if (bfd_bread ((PTR
) buf
, 4, abfd
) != 4)
724 if (bfd_getb32 (buf
) == 0xfeedface)
726 header
->byteorder
= BFD_ENDIAN_BIG
;
727 header
->magic
= 0xfeedface;
731 else if (bfd_getl32 (buf
) == 0xfeedface)
733 header
->byteorder
= BFD_ENDIAN_LITTLE
;
734 header
->magic
= 0xfeedface;
738 else if (bfd_getb32 (buf
) == 0xfeedfacf)
740 header
->byteorder
= BFD_ENDIAN_BIG
;
741 header
->magic
= 0xfeedfacf;
745 else if (bfd_getl32 (buf
) == 0xfeedfacf)
747 header
->byteorder
= BFD_ENDIAN_LITTLE
;
748 header
->magic
= 0xfeedfacf;
754 header
->byteorder
= BFD_ENDIAN_UNKNOWN
;
758 /* Once the size of the header is known, read the full header. */
759 size
= (header
->version
== 2) ? 32 : 28;
761 bfd_seek (abfd
, 0, SEEK_SET
);
762 if (bfd_bread ((PTR
) buf
, size
, abfd
) != size
)
765 header
->cputype
= (*get32
) (buf
+ 4);
766 header
->cpusubtype
= (*get32
) (buf
+ 8);
767 header
->filetype
= (*get32
) (buf
+ 12);
768 header
->ncmds
= (*get32
) (buf
+ 16);
769 header
->sizeofcmds
= (*get32
) (buf
+ 20);
770 header
->flags
= (*get32
) (buf
+ 24);
772 if (header
->version
== 2)
773 header
->reserved
= (*get32
) (buf
+ 28);
779 bfd_mach_o_make_bfd_section (bfd
*abfd
, bfd_mach_o_section
*section
,
784 const char *prefix
= "LC_SEGMENT";
785 unsigned int snamelen
;
788 snamelen
= strlen (prefix
) + 1
789 + strlen (section
->segname
) + 1
790 + strlen (section
->sectname
) + 1;
792 sname
= bfd_alloc (abfd
, snamelen
);
796 /* Use canonical dwarf section names for dwarf sections. */
797 if (strcmp (section
->segname
, "__DWARF") == 0
798 && strncmp (section
->sectname
, "__", 2) == 0)
799 sprintf (sname
, ".%s", section
->sectname
+ 2);
800 else if (strcmp (section
->segname
, "__TEXT") == 0)
802 if (strcmp (section
->sectname
, "__eh_frame") == 0)
803 strcpy (sname
, ".eh_frame");
804 else if (section
->sectname
[0])
805 sprintf (sname
, "%s.%s", section
->segname
, section
->sectname
);
807 strcpy (sname
, section
->segname
);
809 else if (strcmp (section
->segname
, "__DATA") == 0)
811 if (section
->sectname
[0])
812 sprintf (sname
, "%s.%s", section
->segname
, section
->sectname
);
814 strcpy (sname
, section
->segname
);
817 sprintf (sname
, "%s.%s.%s", prefix
, section
->segname
, section
->sectname
);
819 if (section
->flags
& BFD_MACH_O_S_ATTR_DEBUG
)
820 flags
= SEC_HAS_CONTENTS
| SEC_DEBUGGING
;
824 if ((section
->flags
& BFD_MACH_O_SECTION_TYPE_MASK
)
825 != BFD_MACH_O_S_ZEROFILL
)
827 flags
|= SEC_HAS_CONTENTS
| SEC_LOAD
;
828 if (prot
& BFD_MACH_O_PROT_EXECUTE
)
830 if (prot
& BFD_MACH_O_PROT_WRITE
)
832 else if (prot
& BFD_MACH_O_PROT_READ
)
833 flags
|= SEC_READONLY
;
836 bfdsec
= bfd_make_section_anyway_with_flags (abfd
, sname
, flags
);
840 bfdsec
->vma
= section
->addr
;
841 bfdsec
->lma
= section
->addr
;
842 bfdsec
->size
= section
->size
;
843 bfdsec
->filepos
= section
->offset
;
844 bfdsec
->alignment_power
= section
->align
;
845 bfdsec
->segment_mark
= 0;
851 bfd_mach_o_scan_read_section_32 (bfd
*abfd
,
852 bfd_mach_o_section
*section
,
856 unsigned char buf
[68];
858 bfd_seek (abfd
, offset
, SEEK_SET
);
859 if (bfd_bread ((PTR
) buf
, 68, abfd
) != 68)
862 memcpy (section
->sectname
, buf
, 16);
863 section
->sectname
[16] = '\0';
864 memcpy (section
->segname
, buf
+ 16, 16);
865 section
->segname
[16] = '\0';
866 section
->addr
= bfd_h_get_32 (abfd
, buf
+ 32);
867 section
->size
= bfd_h_get_32 (abfd
, buf
+ 36);
868 section
->offset
= bfd_h_get_32 (abfd
, buf
+ 40);
869 section
->align
= bfd_h_get_32 (abfd
, buf
+ 44);
870 section
->reloff
= bfd_h_get_32 (abfd
, buf
+ 48);
871 section
->nreloc
= bfd_h_get_32 (abfd
, buf
+ 52);
872 section
->flags
= bfd_h_get_32 (abfd
, buf
+ 56);
873 section
->reserved1
= bfd_h_get_32 (abfd
, buf
+ 60);
874 section
->reserved2
= bfd_h_get_32 (abfd
, buf
+ 64);
875 section
->reserved3
= 0;
876 section
->bfdsection
= bfd_mach_o_make_bfd_section (abfd
, section
, prot
);
878 if (section
->bfdsection
== NULL
)
885 bfd_mach_o_scan_read_section_64 (bfd
*abfd
,
886 bfd_mach_o_section
*section
,
890 unsigned char buf
[80];
892 bfd_seek (abfd
, offset
, SEEK_SET
);
893 if (bfd_bread ((PTR
) buf
, 80, abfd
) != 80)
896 memcpy (section
->sectname
, buf
, 16);
897 section
->sectname
[16] = '\0';
898 memcpy (section
->segname
, buf
+ 16, 16);
899 section
->segname
[16] = '\0';
900 section
->addr
= bfd_h_get_64 (abfd
, buf
+ 32);
901 section
->size
= bfd_h_get_64 (abfd
, buf
+ 40);
902 section
->offset
= bfd_h_get_32 (abfd
, buf
+ 48);
903 section
->align
= bfd_h_get_32 (abfd
, buf
+ 52);
904 section
->reloff
= bfd_h_get_32 (abfd
, buf
+ 56);
905 section
->nreloc
= bfd_h_get_32 (abfd
, buf
+ 60);
906 section
->flags
= bfd_h_get_32 (abfd
, buf
+ 64);
907 section
->reserved1
= bfd_h_get_32 (abfd
, buf
+ 68);
908 section
->reserved2
= bfd_h_get_32 (abfd
, buf
+ 72);
909 section
->reserved3
= bfd_h_get_32 (abfd
, buf
+ 76);
910 section
->bfdsection
= bfd_mach_o_make_bfd_section (abfd
, section
, prot
);
912 if (section
->bfdsection
== NULL
)
919 bfd_mach_o_scan_read_section (bfd
*abfd
,
920 bfd_mach_o_section
*section
,
926 return bfd_mach_o_scan_read_section_64 (abfd
, section
, offset
, prot
);
928 return bfd_mach_o_scan_read_section_32 (abfd
, section
, offset
, prot
);
932 bfd_mach_o_scan_read_symtab_symbol (bfd
*abfd
,
933 bfd_mach_o_symtab_command
*sym
,
937 bfd_mach_o_data_struct
*mdata
= abfd
->tdata
.mach_o_data
;
938 unsigned int wide
= (mdata
->header
.version
== 2);
939 unsigned int symwidth
= wide
? 16 : 12;
940 bfd_vma symoff
= sym
->symoff
+ (i
* symwidth
);
941 unsigned char buf
[16];
942 unsigned char type
= -1;
943 unsigned char section
= -1;
946 unsigned long stroff
= -1;
947 unsigned int symtype
= -1;
949 BFD_ASSERT (sym
->strtab
!= NULL
);
951 bfd_seek (abfd
, symoff
, SEEK_SET
);
952 if (bfd_bread ((PTR
) buf
, symwidth
, abfd
) != symwidth
)
954 fprintf (stderr
, "bfd_mach_o_scan_read_symtab_symbol: unable to read %d bytes at %lu\n",
955 symwidth
, (unsigned long) symoff
);
959 stroff
= bfd_h_get_32 (abfd
, buf
);
960 type
= bfd_h_get_8 (abfd
, buf
+ 4);
961 symtype
= (type
& 0x0e);
962 section
= bfd_h_get_8 (abfd
, buf
+ 5);
963 desc
= bfd_h_get_16 (abfd
, buf
+ 6);
965 value
= bfd_h_get_64 (abfd
, buf
+ 8);
967 value
= bfd_h_get_32 (abfd
, buf
+ 8);
969 if (stroff
>= sym
->strsize
)
971 fprintf (stderr
, "bfd_mach_o_scan_read_symtab_symbol: symbol name out of range (%lu >= %lu)\n",
972 (unsigned long) stroff
, (unsigned long) sym
->strsize
);
977 s
->name
= sym
->strtab
+ stroff
;
979 s
->udata
.i
= (type
<< 24) | (section
<< 16) | desc
;
982 if (type
& BFD_MACH_O_N_STAB
)
984 s
->flags
|= BSF_DEBUGGING
;
985 s
->section
= bfd_und_section_ptr
;
997 if ((section
> 0) && (section
<= mdata
->nsects
))
999 s
->section
= mdata
->sections
[section
- 1]->bfdsection
;
1000 s
->value
= s
->value
- mdata
->sections
[section
- 1]->addr
;
1007 if (type
& BFD_MACH_O_N_PEXT
)
1008 s
->flags
|= BSF_GLOBAL
;
1010 if (type
& BFD_MACH_O_N_EXT
)
1011 s
->flags
|= BSF_GLOBAL
;
1013 if (!(type
& (BFD_MACH_O_N_PEXT
| BFD_MACH_O_N_EXT
)))
1014 s
->flags
|= BSF_LOCAL
;
1018 case BFD_MACH_O_N_UNDF
:
1019 s
->section
= bfd_und_section_ptr
;
1021 case BFD_MACH_O_N_PBUD
:
1022 s
->section
= bfd_und_section_ptr
;
1024 case BFD_MACH_O_N_ABS
:
1025 s
->section
= bfd_abs_section_ptr
;
1027 case BFD_MACH_O_N_SECT
:
1028 if ((section
> 0) && (section
<= mdata
->nsects
))
1030 s
->section
= mdata
->sections
[section
- 1]->bfdsection
;
1031 s
->value
= s
->value
- mdata
->sections
[section
- 1]->addr
;
1035 /* Mach-O uses 0 to mean "no section"; not an error. */
1038 fprintf (stderr
, "bfd_mach_o_scan_read_symtab_symbol: "
1039 "symbol \"%s\" specified invalid section %d (max %lu): setting to undefined\n",
1040 s
->name
, section
, mdata
->nsects
);
1042 s
->section
= bfd_und_section_ptr
;
1045 case BFD_MACH_O_N_INDR
:
1046 fprintf (stderr
, "bfd_mach_o_scan_read_symtab_symbol: "
1047 "symbol \"%s\" is unsupported 'indirect' reference: setting to undefined\n",
1049 s
->section
= bfd_und_section_ptr
;
1052 fprintf (stderr
, "bfd_mach_o_scan_read_symtab_symbol: "
1053 "symbol \"%s\" specified invalid type field 0x%x: setting to undefined\n",
1055 s
->section
= bfd_und_section_ptr
;
1064 bfd_mach_o_scan_read_symtab_strtab (bfd
*abfd
,
1065 bfd_mach_o_symtab_command
*sym
)
1067 BFD_ASSERT (sym
->strtab
== NULL
);
1069 if (abfd
->flags
& BFD_IN_MEMORY
)
1071 struct bfd_in_memory
*b
;
1073 b
= (struct bfd_in_memory
*) abfd
->iostream
;
1075 if ((sym
->stroff
+ sym
->strsize
) > b
->size
)
1077 bfd_set_error (bfd_error_file_truncated
);
1080 sym
->strtab
= (char *) b
->buffer
+ sym
->stroff
;
1084 sym
->strtab
= bfd_alloc (abfd
, sym
->strsize
);
1085 if (sym
->strtab
== NULL
)
1088 bfd_seek (abfd
, sym
->stroff
, SEEK_SET
);
1089 if (bfd_bread ((PTR
) sym
->strtab
, sym
->strsize
, abfd
) != sym
->strsize
)
1091 fprintf (stderr
, "bfd_mach_o_scan_read_symtab_strtab: unable to read %lu bytes at %lu\n",
1092 sym
->strsize
, sym
->stroff
);
1100 bfd_mach_o_scan_read_symtab_symbols (bfd
*abfd
,
1101 bfd_mach_o_symtab_command
*sym
)
1106 BFD_ASSERT (sym
->symbols
== NULL
);
1107 sym
->symbols
= bfd_alloc (abfd
, sym
->nsyms
* sizeof (asymbol
));
1109 if (sym
->symbols
== NULL
)
1111 fprintf (stderr
, "bfd_mach_o_scan_read_symtab_symbols: unable to allocate memory for symbols\n");
1115 ret
= bfd_mach_o_scan_read_symtab_strtab (abfd
, sym
);
1119 for (i
= 0; i
< sym
->nsyms
; i
++)
1121 ret
= bfd_mach_o_scan_read_symtab_symbol (abfd
, sym
, &sym
->symbols
[i
], i
);
1130 bfd_mach_o_scan_read_dysymtab_symbol (bfd
*abfd
,
1131 bfd_mach_o_dysymtab_command
*dysym
,
1132 bfd_mach_o_symtab_command
*sym
,
1136 unsigned long isymoff
= dysym
->indirectsymoff
+ (i
* 4);
1137 unsigned long symindex
;
1138 unsigned char buf
[4];
1140 BFD_ASSERT (i
< dysym
->nindirectsyms
);
1142 bfd_seek (abfd
, isymoff
, SEEK_SET
);
1143 if (bfd_bread ((PTR
) buf
, 4, abfd
) != 4)
1145 fprintf (stderr
, "bfd_mach_o_scan_read_dysymtab_symbol: unable to read %lu bytes at %lu\n",
1146 (unsigned long) 4, isymoff
);
1149 symindex
= bfd_h_get_32 (abfd
, buf
);
1151 return bfd_mach_o_scan_read_symtab_symbol (abfd
, sym
, s
, symindex
);
1155 bfd_mach_o_i386_flavour_string (unsigned int flavour
)
1157 switch ((int) flavour
)
1159 case BFD_MACH_O_x86_THREAD_STATE32
: return "x86_THREAD_STATE32";
1160 case BFD_MACH_O_x86_FLOAT_STATE32
: return "x86_FLOAT_STATE32";
1161 case BFD_MACH_O_x86_EXCEPTION_STATE32
: return "x86_EXCEPTION_STATE32";
1162 case BFD_MACH_O_x86_THREAD_STATE64
: return "x86_THREAD_STATE64";
1163 case BFD_MACH_O_x86_FLOAT_STATE64
: return "x86_FLOAT_STATE64";
1164 case BFD_MACH_O_x86_EXCEPTION_STATE64
: return "x86_EXCEPTION_STATE64";
1165 case BFD_MACH_O_x86_THREAD_STATE
: return "x86_THREAD_STATE";
1166 case BFD_MACH_O_x86_FLOAT_STATE
: return "x86_FLOAT_STATE";
1167 case BFD_MACH_O_x86_EXCEPTION_STATE
: return "x86_EXCEPTION_STATE";
1168 case BFD_MACH_O_x86_DEBUG_STATE32
: return "x86_DEBUG_STATE32";
1169 case BFD_MACH_O_x86_DEBUG_STATE64
: return "x86_DEBUG_STATE64";
1170 case BFD_MACH_O_x86_DEBUG_STATE
: return "x86_DEBUG_STATE";
1171 case BFD_MACH_O_THREAD_STATE_NONE
: return "THREAD_STATE_NONE";
1172 default: return "UNKNOWN";
1177 bfd_mach_o_ppc_flavour_string (unsigned int flavour
)
1179 switch ((int) flavour
)
1181 case BFD_MACH_O_PPC_THREAD_STATE
: return "PPC_THREAD_STATE";
1182 case BFD_MACH_O_PPC_FLOAT_STATE
: return "PPC_FLOAT_STATE";
1183 case BFD_MACH_O_PPC_EXCEPTION_STATE
: return "PPC_EXCEPTION_STATE";
1184 case BFD_MACH_O_PPC_VECTOR_STATE
: return "PPC_VECTOR_STATE";
1185 default: return "UNKNOWN";
1190 bfd_mach_o_scan_read_dylinker (bfd
*abfd
,
1191 bfd_mach_o_load_command
*command
)
1193 bfd_mach_o_dylinker_command
*cmd
= &command
->command
.dylinker
;
1194 unsigned char buf
[4];
1195 unsigned int nameoff
;
1200 BFD_ASSERT ((command
->type
== BFD_MACH_O_LC_ID_DYLINKER
)
1201 || (command
->type
== BFD_MACH_O_LC_LOAD_DYLINKER
));
1203 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
1204 if (bfd_bread ((PTR
) buf
, 4, abfd
) != 4)
1207 nameoff
= bfd_h_get_32 (abfd
, buf
+ 0);
1209 cmd
->name_offset
= command
->offset
+ nameoff
;
1210 cmd
->name_len
= command
->len
- nameoff
;
1212 if (command
->type
== BFD_MACH_O_LC_LOAD_DYLINKER
)
1213 prefix
= "LC_LOAD_DYLINKER";
1214 else if (command
->type
== BFD_MACH_O_LC_ID_DYLINKER
)
1215 prefix
= "LC_ID_DYLINKER";
1219 sname
= bfd_alloc (abfd
, strlen (prefix
) + 1);
1222 strcpy (sname
, prefix
);
1224 bfdsec
= bfd_make_section_anyway_with_flags (abfd
, sname
, SEC_HAS_CONTENTS
);
1230 bfdsec
->size
= command
->len
- nameoff
;
1231 bfdsec
->filepos
= command
->offset
+ nameoff
;
1232 bfdsec
->alignment_power
= 0;
1234 cmd
->section
= bfdsec
;
1240 bfd_mach_o_scan_read_dylib (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1242 bfd_mach_o_dylib_command
*cmd
= &command
->command
.dylib
;
1243 unsigned char buf
[16];
1244 unsigned int nameoff
;
1249 BFD_ASSERT ((command
->type
== BFD_MACH_O_LC_ID_DYLIB
)
1250 || (command
->type
== BFD_MACH_O_LC_LOAD_DYLIB
)
1251 || (command
->type
== BFD_MACH_O_LC_LOAD_WEAK_DYLIB
));
1253 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
1254 if (bfd_bread ((PTR
) buf
, 16, abfd
) != 16)
1257 nameoff
= bfd_h_get_32 (abfd
, buf
+ 0);
1258 cmd
->timestamp
= bfd_h_get_32 (abfd
, buf
+ 4);
1259 cmd
->current_version
= bfd_h_get_32 (abfd
, buf
+ 8);
1260 cmd
->compatibility_version
= bfd_h_get_32 (abfd
, buf
+ 12);
1262 cmd
->name_offset
= command
->offset
+ nameoff
;
1263 cmd
->name_len
= command
->len
- nameoff
;
1265 if (command
->type
== BFD_MACH_O_LC_LOAD_DYLIB
)
1266 prefix
= "LC_LOAD_DYLIB";
1267 else if (command
->type
== BFD_MACH_O_LC_LOAD_WEAK_DYLIB
)
1268 prefix
= "LC_LOAD_WEAK_DYLIB";
1269 else if (command
->type
== BFD_MACH_O_LC_ID_DYLIB
)
1270 prefix
= "LC_ID_DYLIB";
1274 sname
= bfd_alloc (abfd
, strlen (prefix
) + 1);
1277 strcpy (sname
, prefix
);
1279 bfdsec
= bfd_make_section_anyway_with_flags (abfd
, sname
, SEC_HAS_CONTENTS
);
1285 bfdsec
->size
= command
->len
- 8;
1286 bfdsec
->filepos
= command
->offset
+ 8;
1287 bfdsec
->alignment_power
= 0;
1289 cmd
->section
= bfdsec
;
1295 bfd_mach_o_scan_read_prebound_dylib (bfd
*abfd ATTRIBUTE_UNUSED
,
1296 bfd_mach_o_load_command
*command ATTRIBUTE_UNUSED
)
1298 /* bfd_mach_o_prebound_dylib_command *cmd = &command->command.prebound_dylib; */
1300 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_PREBOUND_DYLIB
);
1305 bfd_mach_o_scan_read_thread (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1307 bfd_mach_o_data_struct
*mdata
= NULL
;
1308 bfd_mach_o_thread_command
*cmd
= &command
->command
.thread
;
1309 unsigned char buf
[8];
1311 unsigned int nflavours
;
1314 BFD_ASSERT ((command
->type
== BFD_MACH_O_LC_THREAD
)
1315 || (command
->type
== BFD_MACH_O_LC_UNIXTHREAD
));
1317 BFD_ASSERT (bfd_mach_o_valid (abfd
));
1318 mdata
= abfd
->tdata
.mach_o_data
;
1322 while (offset
!= command
->len
)
1324 if (offset
>= command
->len
)
1327 bfd_seek (abfd
, command
->offset
+ offset
, SEEK_SET
);
1329 if (bfd_bread ((PTR
) buf
, 8, abfd
) != 8)
1332 offset
+= 8 + bfd_h_get_32 (abfd
, buf
+ 4) * 4;
1336 cmd
->flavours
= bfd_alloc (abfd
, nflavours
* sizeof (bfd_mach_o_thread_flavour
));
1337 if (cmd
->flavours
== NULL
)
1339 cmd
->nflavours
= nflavours
;
1343 while (offset
!= command
->len
)
1345 if (offset
>= command
->len
)
1348 if (nflavours
>= cmd
->nflavours
)
1351 bfd_seek (abfd
, command
->offset
+ offset
, SEEK_SET
);
1353 if (bfd_bread ((PTR
) buf
, 8, abfd
) != 8)
1356 cmd
->flavours
[nflavours
].flavour
= bfd_h_get_32 (abfd
, buf
);
1357 cmd
->flavours
[nflavours
].offset
= command
->offset
+ offset
+ 8;
1358 cmd
->flavours
[nflavours
].size
= bfd_h_get_32 (abfd
, buf
+ 4) * 4;
1359 offset
+= cmd
->flavours
[nflavours
].size
+ 8;
1363 for (i
= 0; i
< nflavours
; i
++)
1366 unsigned int snamelen
;
1368 const char *flavourstr
;
1369 const char *prefix
= "LC_THREAD";
1372 switch (mdata
->header
.cputype
)
1374 case BFD_MACH_O_CPU_TYPE_POWERPC
:
1375 case BFD_MACH_O_CPU_TYPE_POWERPC_64
:
1376 flavourstr
= bfd_mach_o_ppc_flavour_string (cmd
->flavours
[i
].flavour
);
1378 case BFD_MACH_O_CPU_TYPE_I386
:
1379 case BFD_MACH_O_CPU_TYPE_X86_64
:
1380 flavourstr
= bfd_mach_o_i386_flavour_string (cmd
->flavours
[i
].flavour
);
1383 flavourstr
= "UNKNOWN_ARCHITECTURE";
1387 snamelen
= strlen (prefix
) + 1 + 20 + 1 + strlen (flavourstr
) + 1;
1388 sname
= bfd_alloc (abfd
, snamelen
);
1394 sprintf (sname
, "%s.%s.%u", prefix
, flavourstr
, j
);
1395 if (bfd_get_section_by_name (abfd
, sname
) == NULL
)
1400 bfdsec
= bfd_make_section_with_flags (abfd
, sname
, SEC_HAS_CONTENTS
);
1404 bfdsec
->size
= cmd
->flavours
[i
].size
;
1405 bfdsec
->filepos
= cmd
->flavours
[i
].offset
;
1406 bfdsec
->alignment_power
= 0x0;
1408 cmd
->section
= bfdsec
;
1415 bfd_mach_o_scan_read_dysymtab (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1417 bfd_mach_o_dysymtab_command
*seg
= &command
->command
.dysymtab
;
1418 unsigned char buf
[72];
1420 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_DYSYMTAB
);
1422 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
1423 if (bfd_bread ((PTR
) buf
, 72, abfd
) != 72)
1426 seg
->ilocalsym
= bfd_h_get_32 (abfd
, buf
+ 0);
1427 seg
->nlocalsym
= bfd_h_get_32 (abfd
, buf
+ 4);
1428 seg
->iextdefsym
= bfd_h_get_32 (abfd
, buf
+ 8);
1429 seg
->nextdefsym
= bfd_h_get_32 (abfd
, buf
+ 12);
1430 seg
->iundefsym
= bfd_h_get_32 (abfd
, buf
+ 16);
1431 seg
->nundefsym
= bfd_h_get_32 (abfd
, buf
+ 20);
1432 seg
->tocoff
= bfd_h_get_32 (abfd
, buf
+ 24);
1433 seg
->ntoc
= bfd_h_get_32 (abfd
, buf
+ 28);
1434 seg
->modtaboff
= bfd_h_get_32 (abfd
, buf
+ 32);
1435 seg
->nmodtab
= bfd_h_get_32 (abfd
, buf
+ 36);
1436 seg
->extrefsymoff
= bfd_h_get_32 (abfd
, buf
+ 40);
1437 seg
->nextrefsyms
= bfd_h_get_32 (abfd
, buf
+ 44);
1438 seg
->indirectsymoff
= bfd_h_get_32 (abfd
, buf
+ 48);
1439 seg
->nindirectsyms
= bfd_h_get_32 (abfd
, buf
+ 52);
1440 seg
->extreloff
= bfd_h_get_32 (abfd
, buf
+ 56);
1441 seg
->nextrel
= bfd_h_get_32 (abfd
, buf
+ 60);
1442 seg
->locreloff
= bfd_h_get_32 (abfd
, buf
+ 64);
1443 seg
->nlocrel
= bfd_h_get_32 (abfd
, buf
+ 68);
1449 bfd_mach_o_scan_read_symtab (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1451 bfd_mach_o_symtab_command
*seg
= &command
->command
.symtab
;
1452 unsigned char buf
[16];
1455 const char *prefix
= "LC_SYMTAB.stabs";
1456 int nlist_size
= (bfd_mach_o_version (abfd
) > 1) ? 16 : 12;
1458 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SYMTAB
);
1460 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
1461 if (bfd_bread ((PTR
) buf
, 16, abfd
) != 16)
1464 seg
->symoff
= bfd_h_get_32 (abfd
, buf
);
1465 seg
->nsyms
= bfd_h_get_32 (abfd
, buf
+ 4);
1466 seg
->stroff
= bfd_h_get_32 (abfd
, buf
+ 8);
1467 seg
->strsize
= bfd_h_get_32 (abfd
, buf
+ 12);
1468 seg
->symbols
= NULL
;
1471 sname
= bfd_alloc (abfd
, strlen (prefix
) + 1);
1474 strcpy (sname
, prefix
);
1476 bfdsec
= bfd_make_section_anyway_with_flags (abfd
, sname
, SEC_HAS_CONTENTS
);
1482 bfdsec
->size
= seg
->nsyms
* nlist_size
;
1483 bfdsec
->filepos
= seg
->symoff
;
1484 bfdsec
->alignment_power
= 0;
1486 seg
->stabs_segment
= bfdsec
;
1488 if (seg
->nsyms
!= 0)
1489 abfd
->flags
|= HAS_SYMS
;
1491 prefix
= "LC_SYMTAB.stabstr";
1492 sname
= bfd_alloc (abfd
, strlen (prefix
) + 1);
1495 strcpy (sname
, prefix
);
1497 bfdsec
= bfd_make_section_anyway_with_flags (abfd
, sname
, SEC_HAS_CONTENTS
);
1503 bfdsec
->size
= seg
->strsize
;
1504 bfdsec
->filepos
= seg
->stroff
;
1505 bfdsec
->alignment_power
= 0;
1507 seg
->stabstr_segment
= bfdsec
;
1513 bfd_mach_o_scan_read_uuid (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1515 bfd_mach_o_uuid_command
*cmd
= &command
->command
.uuid
;
1518 static const char prefix
[] = "LC_UUID";
1520 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_UUID
);
1522 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
1523 if (bfd_bread ((PTR
) cmd
->uuid
, 16, abfd
) != 16)
1526 sname
= bfd_alloc (abfd
, strlen (prefix
) + 1);
1529 strcpy (sname
, prefix
);
1531 bfdsec
= bfd_make_section_anyway_with_flags (abfd
, sname
, SEC_HAS_CONTENTS
);
1537 bfdsec
->size
= command
->len
- 8;
1538 bfdsec
->filepos
= command
->offset
+ 8;
1539 bfdsec
->alignment_power
= 0;
1541 cmd
->section
= bfdsec
;
1547 bfd_mach_o_scan_read_segment (bfd
*abfd
,
1548 bfd_mach_o_load_command
*command
,
1551 unsigned char buf
[64];
1552 bfd_mach_o_segment_command
*seg
= &command
->command
.segment
;
1556 const char *prefix
= "LC_SEGMENT";
1557 unsigned int snamelen
;
1561 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SEGMENT_64
);
1563 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
1564 if (bfd_bread ((PTR
) buf
, 64, abfd
) != 64)
1567 memcpy (seg
->segname
, buf
, 16);
1568 seg
->segname
[16] = '\0';
1570 seg
->vmaddr
= bfd_h_get_64 (abfd
, buf
+ 16);
1571 seg
->vmsize
= bfd_h_get_64 (abfd
, buf
+ 24);
1572 seg
->fileoff
= bfd_h_get_64 (abfd
, buf
+ 32);
1573 seg
->filesize
= bfd_h_get_64 (abfd
, buf
+ 40);
1574 seg
->maxprot
= bfd_h_get_32 (abfd
, buf
+ 48);
1575 seg
->initprot
= bfd_h_get_32 (abfd
, buf
+ 52);
1576 seg
->nsects
= bfd_h_get_32 (abfd
, buf
+ 56);
1577 seg
->flags
= bfd_h_get_32 (abfd
, buf
+ 60);
1581 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SEGMENT
);
1583 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
1584 if (bfd_bread ((PTR
) buf
, 48, abfd
) != 48)
1587 memcpy (seg
->segname
, buf
, 16);
1588 seg
->segname
[16] = '\0';
1590 seg
->vmaddr
= bfd_h_get_32 (abfd
, buf
+ 16);
1591 seg
->vmsize
= bfd_h_get_32 (abfd
, buf
+ 20);
1592 seg
->fileoff
= bfd_h_get_32 (abfd
, buf
+ 24);
1593 seg
->filesize
= bfd_h_get_32 (abfd
, buf
+ 28);
1594 seg
->maxprot
= bfd_h_get_32 (abfd
, buf
+ 32);
1595 seg
->initprot
= bfd_h_get_32 (abfd
, buf
+ 36);
1596 seg
->nsects
= bfd_h_get_32 (abfd
, buf
+ 40);
1597 seg
->flags
= bfd_h_get_32 (abfd
, buf
+ 44);
1600 snamelen
= strlen (prefix
) + 1 + strlen (seg
->segname
) + 1;
1601 sname
= bfd_alloc (abfd
, snamelen
);
1604 if (strcmp (seg
->segname
, "__TEXT") == 0
1605 || strcmp (seg
->segname
, "__DATA") == 0
1606 || strcmp (seg
->segname
, "__IMPORT") == 0
1607 || strcmp (seg
->segname
, "__LINKEDIT") == 0)
1608 strcpy (sname
, seg
->segname
);
1610 sprintf (sname
, "%s.%s", prefix
, seg
->segname
);
1612 bfdsec
= bfd_make_section_anyway (abfd
, sname
);
1616 bfdsec
->vma
= seg
->vmaddr
;
1617 bfdsec
->lma
= seg
->vmaddr
;
1618 bfdsec
->size
= seg
->filesize
;
1619 bfdsec
->filepos
= seg
->fileoff
;
1620 bfdsec
->alignment_power
= 0x0;
1621 bfdsec
->flags
= SEC_HAS_CONTENTS
;
1622 bfdsec
->segment_mark
= 1;
1624 seg
->segment
= bfdsec
;
1626 if (seg
->nsects
!= 0)
1628 seg
->sections
= bfd_alloc (abfd
, seg
->nsects
* sizeof (bfd_mach_o_section
));
1629 if (seg
->sections
== NULL
)
1632 for (i
= 0; i
< seg
->nsects
; i
++)
1636 segoff
= command
->offset
+ 64 + 8 + (i
* 80);
1638 segoff
= command
->offset
+ 48 + 8 + (i
* 68);
1640 if (bfd_mach_o_scan_read_section
1641 (abfd
, &seg
->sections
[i
], segoff
, seg
->initprot
, wide
) != 0)
1650 bfd_mach_o_scan_read_segment_32 (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1652 return bfd_mach_o_scan_read_segment (abfd
, command
, 0);
1656 bfd_mach_o_scan_read_segment_64 (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1658 return bfd_mach_o_scan_read_segment (abfd
, command
, 1);
1662 bfd_mach_o_scan_read_command (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1664 unsigned char buf
[8];
1666 bfd_seek (abfd
, command
->offset
, SEEK_SET
);
1667 if (bfd_bread ((PTR
) buf
, 8, abfd
) != 8)
1670 command
->type
= (bfd_h_get_32 (abfd
, buf
) & ~BFD_MACH_O_LC_REQ_DYLD
);
1671 command
->type_required
= (bfd_h_get_32 (abfd
, buf
) & BFD_MACH_O_LC_REQ_DYLD
1673 command
->len
= bfd_h_get_32 (abfd
, buf
+ 4);
1675 switch (command
->type
)
1677 case BFD_MACH_O_LC_SEGMENT
:
1678 if (bfd_mach_o_scan_read_segment_32 (abfd
, command
) != 0)
1681 case BFD_MACH_O_LC_SEGMENT_64
:
1682 if (bfd_mach_o_scan_read_segment_64 (abfd
, command
) != 0)
1685 case BFD_MACH_O_LC_SYMTAB
:
1686 if (bfd_mach_o_scan_read_symtab (abfd
, command
) != 0)
1689 case BFD_MACH_O_LC_SYMSEG
:
1691 case BFD_MACH_O_LC_THREAD
:
1692 case BFD_MACH_O_LC_UNIXTHREAD
:
1693 if (bfd_mach_o_scan_read_thread (abfd
, command
) != 0)
1696 case BFD_MACH_O_LC_LOAD_DYLINKER
:
1697 case BFD_MACH_O_LC_ID_DYLINKER
:
1698 if (bfd_mach_o_scan_read_dylinker (abfd
, command
) != 0)
1701 case BFD_MACH_O_LC_LOAD_DYLIB
:
1702 case BFD_MACH_O_LC_ID_DYLIB
:
1703 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB
:
1704 if (bfd_mach_o_scan_read_dylib (abfd
, command
) != 0)
1707 case BFD_MACH_O_LC_PREBOUND_DYLIB
:
1708 if (bfd_mach_o_scan_read_prebound_dylib (abfd
, command
) != 0)
1711 case BFD_MACH_O_LC_LOADFVMLIB
:
1712 case BFD_MACH_O_LC_IDFVMLIB
:
1713 case BFD_MACH_O_LC_IDENT
:
1714 case BFD_MACH_O_LC_FVMFILE
:
1715 case BFD_MACH_O_LC_PREPAGE
:
1716 case BFD_MACH_O_LC_ROUTINES
:
1717 case BFD_MACH_O_LC_SUB_FRAMEWORK
:
1719 case BFD_MACH_O_LC_DYSYMTAB
:
1720 if (bfd_mach_o_scan_read_dysymtab (abfd
, command
) != 0)
1723 case BFD_MACH_O_LC_SUB_UMBRELLA
:
1724 case BFD_MACH_O_LC_SUB_CLIENT
:
1725 case BFD_MACH_O_LC_SUB_LIBRARY
:
1726 case BFD_MACH_O_LC_TWOLEVEL_HINTS
:
1727 case BFD_MACH_O_LC_PREBIND_CKSUM
:
1729 case BFD_MACH_O_LC_UUID
:
1730 if (bfd_mach_o_scan_read_uuid (abfd
, command
) != 0)
1733 case BFD_MACH_O_LC_CODE_SIGNATURE
:
1734 case BFD_MACH_O_LC_SEGMENT_SPLIT_INFO
:
1735 case BFD_MACH_O_LC_REEXPORT_DYLIB
:
1738 fprintf (stderr
, "unable to read unknown load command 0x%lx\n",
1739 (unsigned long) command
->type
);
1747 bfd_mach_o_flatten_sections (bfd
*abfd
)
1749 bfd_mach_o_data_struct
*mdata
= abfd
->tdata
.mach_o_data
;
1753 /* Count total number of sections. */
1756 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
1758 if (mdata
->commands
[i
].type
== BFD_MACH_O_LC_SEGMENT
1759 || mdata
->commands
[i
].type
== BFD_MACH_O_LC_SEGMENT_64
)
1761 bfd_mach_o_segment_command
*seg
;
1763 seg
= &mdata
->commands
[i
].command
.segment
;
1764 mdata
->nsects
+= seg
->nsects
;
1768 /* Allocate sections array. */
1769 mdata
->sections
= bfd_alloc (abfd
,
1770 mdata
->nsects
* sizeof (bfd_mach_o_section
*));
1772 /* Fill the array. */
1775 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
1777 if (mdata
->commands
[i
].type
== BFD_MACH_O_LC_SEGMENT
1778 || mdata
->commands
[i
].type
== BFD_MACH_O_LC_SEGMENT_64
)
1780 bfd_mach_o_segment_command
*seg
;
1782 seg
= &mdata
->commands
[i
].command
.segment
;
1783 BFD_ASSERT (csect
+ seg
->nsects
<= mdata
->nsects
);
1785 for (j
= 0; j
< seg
->nsects
; j
++)
1786 mdata
->sections
[csect
++] = &seg
->sections
[j
];
1792 bfd_mach_o_scan_start_address (bfd
*abfd
)
1794 bfd_mach_o_data_struct
*mdata
= abfd
->tdata
.mach_o_data
;
1795 bfd_mach_o_thread_command
*cmd
= NULL
;
1798 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
1800 if ((mdata
->commands
[i
].type
== BFD_MACH_O_LC_THREAD
) ||
1801 (mdata
->commands
[i
].type
== BFD_MACH_O_LC_UNIXTHREAD
))
1804 cmd
= &mdata
->commands
[i
].command
.thread
;
1813 for (i
= 0; i
< cmd
->nflavours
; i
++)
1815 if ((mdata
->header
.cputype
== BFD_MACH_O_CPU_TYPE_I386
)
1816 && (cmd
->flavours
[i
].flavour
1817 == (unsigned long) BFD_MACH_O_x86_THREAD_STATE32
))
1819 unsigned char buf
[4];
1821 bfd_seek (abfd
, cmd
->flavours
[i
].offset
+ 40, SEEK_SET
);
1823 if (bfd_bread (buf
, 4, abfd
) != 4)
1826 abfd
->start_address
= bfd_h_get_32 (abfd
, buf
);
1828 else if ((mdata
->header
.cputype
== BFD_MACH_O_CPU_TYPE_POWERPC
)
1829 && (cmd
->flavours
[i
].flavour
== BFD_MACH_O_PPC_THREAD_STATE
))
1831 unsigned char buf
[4];
1833 bfd_seek (abfd
, cmd
->flavours
[i
].offset
+ 0, SEEK_SET
);
1835 if (bfd_bread (buf
, 4, abfd
) != 4)
1838 abfd
->start_address
= bfd_h_get_32 (abfd
, buf
);
1840 else if ((mdata
->header
.cputype
== BFD_MACH_O_CPU_TYPE_POWERPC_64
)
1841 && (cmd
->flavours
[i
].flavour
== BFD_MACH_O_PPC_THREAD_STATE_64
))
1843 unsigned char buf
[8];
1845 bfd_seek (abfd
, cmd
->flavours
[i
].offset
+ 0, SEEK_SET
);
1847 if (bfd_bread (buf
, 8, abfd
) != 8)
1850 abfd
->start_address
= bfd_h_get_64 (abfd
, buf
);
1852 else if ((mdata
->header
.cputype
== BFD_MACH_O_CPU_TYPE_X86_64
)
1853 && (cmd
->flavours
[i
].flavour
== BFD_MACH_O_x86_THREAD_STATE64
))
1855 unsigned char buf
[8];
1857 bfd_seek (abfd
, cmd
->flavours
[i
].offset
+ (16 * 8), SEEK_SET
);
1859 if (bfd_bread (buf
, 8, abfd
) != 8)
1862 abfd
->start_address
= bfd_h_get_64 (abfd
, buf
);
1870 bfd_mach_o_scan (bfd
*abfd
,
1871 bfd_mach_o_header
*header
,
1872 bfd_mach_o_data_struct
*mdata
)
1875 enum bfd_architecture cputype
;
1876 unsigned long cpusubtype
;
1877 unsigned int hdrsize
;
1879 hdrsize
= (header
->version
== 2) ? 32 : 28;
1881 mdata
->header
= *header
;
1882 mdata
->symbols
= NULL
;
1884 abfd
->flags
= abfd
->flags
& (BFD_IN_MEMORY
| BFD_IO_FUNCS
);
1885 switch (header
->filetype
)
1887 case BFD_MACH_O_MH_OBJECT
:
1888 abfd
->flags
|= HAS_RELOC
;
1890 case BFD_MACH_O_MH_EXECUTE
:
1891 abfd
->flags
|= EXEC_P
;
1893 case BFD_MACH_O_MH_DYLIB
:
1894 case BFD_MACH_O_MH_BUNDLE
:
1895 abfd
->flags
|= DYNAMIC
;
1899 abfd
->tdata
.mach_o_data
= mdata
;
1901 bfd_mach_o_convert_architecture (header
->cputype
, header
->cpusubtype
,
1902 &cputype
, &cpusubtype
);
1903 if (cputype
== bfd_arch_unknown
)
1905 fprintf (stderr
, "bfd_mach_o_scan: unknown architecture 0x%lx/0x%lx\n",
1906 header
->cputype
, header
->cpusubtype
);
1910 bfd_set_arch_mach (abfd
, cputype
, cpusubtype
);
1912 if (header
->ncmds
!= 0)
1914 mdata
->commands
= bfd_alloc (abfd
, header
->ncmds
* sizeof (bfd_mach_o_load_command
));
1915 if (mdata
->commands
== NULL
)
1918 for (i
= 0; i
< header
->ncmds
; i
++)
1920 bfd_mach_o_load_command
*cur
= &mdata
->commands
[i
];
1923 cur
->offset
= hdrsize
;
1926 bfd_mach_o_load_command
*prev
= &mdata
->commands
[i
- 1];
1927 cur
->offset
= prev
->offset
+ prev
->len
;
1930 if (bfd_mach_o_scan_read_command (abfd
, cur
) < 0)
1935 if (bfd_mach_o_scan_start_address (abfd
) < 0)
1938 bfd_mach_o_flatten_sections (abfd
);
1943 bfd_mach_o_mkobject (bfd
*abfd
)
1945 bfd_mach_o_data_struct
*mdata
= NULL
;
1947 mdata
= bfd_alloc (abfd
, sizeof (bfd_mach_o_data_struct
));
1950 abfd
->tdata
.mach_o_data
= mdata
;
1952 mdata
->header
.magic
= 0;
1953 mdata
->header
.cputype
= 0;
1954 mdata
->header
.cpusubtype
= 0;
1955 mdata
->header
.filetype
= 0;
1956 mdata
->header
.ncmds
= 0;
1957 mdata
->header
.sizeofcmds
= 0;
1958 mdata
->header
.flags
= 0;
1959 mdata
->header
.byteorder
= BFD_ENDIAN_UNKNOWN
;
1960 mdata
->commands
= NULL
;
1961 mdata
->nsymbols
= 0;
1962 mdata
->symbols
= NULL
;
1964 mdata
->sections
= NULL
;
1971 bfd_mach_o_object_p (bfd
*abfd
)
1973 struct bfd_preserve preserve
;
1974 bfd_mach_o_header header
;
1976 preserve
.marker
= NULL
;
1977 if (bfd_mach_o_read_header (abfd
, &header
) != 0)
1980 if (! (header
.byteorder
== BFD_ENDIAN_BIG
1981 || header
.byteorder
== BFD_ENDIAN_LITTLE
))
1983 fprintf (stderr
, "unknown header byte-order value 0x%lx\n",
1984 (unsigned long) header
.byteorder
);
1988 if (! ((header
.byteorder
== BFD_ENDIAN_BIG
1989 && abfd
->xvec
->byteorder
== BFD_ENDIAN_BIG
1990 && abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
1991 || (header
.byteorder
== BFD_ENDIAN_LITTLE
1992 && abfd
->xvec
->byteorder
== BFD_ENDIAN_LITTLE
1993 && abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)))
1996 preserve
.marker
= bfd_zalloc (abfd
, sizeof (bfd_mach_o_data_struct
));
1997 if (preserve
.marker
== NULL
1998 || !bfd_preserve_save (abfd
, &preserve
))
2001 if (bfd_mach_o_scan (abfd
, &header
,
2002 (bfd_mach_o_data_struct
*) preserve
.marker
) != 0)
2005 bfd_preserve_finish (abfd
, &preserve
);
2009 bfd_set_error (bfd_error_wrong_format
);
2012 if (preserve
.marker
!= NULL
)
2013 bfd_preserve_restore (abfd
, &preserve
);
2018 bfd_mach_o_core_p (bfd
*abfd
)
2020 struct bfd_preserve preserve
;
2021 bfd_mach_o_header header
;
2023 preserve
.marker
= NULL
;
2024 if (bfd_mach_o_read_header (abfd
, &header
) != 0)
2027 if (! (header
.byteorder
== BFD_ENDIAN_BIG
2028 || header
.byteorder
== BFD_ENDIAN_LITTLE
))
2030 fprintf (stderr
, "unknown header byte-order value 0x%lx\n",
2031 (unsigned long) header
.byteorder
);
2035 if (! ((header
.byteorder
== BFD_ENDIAN_BIG
2036 && abfd
->xvec
->byteorder
== BFD_ENDIAN_BIG
2037 && abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
2038 || (header
.byteorder
== BFD_ENDIAN_LITTLE
2039 && abfd
->xvec
->byteorder
== BFD_ENDIAN_LITTLE
2040 && abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)))
2043 if (header
.filetype
!= BFD_MACH_O_MH_CORE
)
2046 preserve
.marker
= bfd_zalloc (abfd
, sizeof (bfd_mach_o_data_struct
));
2047 if (preserve
.marker
== NULL
2048 || !bfd_preserve_save (abfd
, &preserve
))
2051 if (bfd_mach_o_scan (abfd
, &header
,
2052 (bfd_mach_o_data_struct
*) preserve
.marker
) != 0)
2055 bfd_preserve_finish (abfd
, &preserve
);
2059 bfd_set_error (bfd_error_wrong_format
);
2062 if (preserve
.marker
!= NULL
)
2063 bfd_preserve_restore (abfd
, &preserve
);
2067 typedef struct mach_o_fat_archentry
2069 unsigned long cputype
;
2070 unsigned long cpusubtype
;
2071 unsigned long offset
;
2073 unsigned long align
;
2074 } mach_o_fat_archentry
;
2076 typedef struct mach_o_fat_data_struct
2078 unsigned long magic
;
2079 unsigned long nfat_arch
;
2080 mach_o_fat_archentry
*archentries
;
2081 } mach_o_fat_data_struct
;
2084 bfd_mach_o_archive_p (bfd
*abfd
)
2086 mach_o_fat_data_struct
*adata
= NULL
;
2087 unsigned char buf
[20];
2090 bfd_seek (abfd
, 0, SEEK_SET
);
2091 if (bfd_bread ((PTR
) buf
, 8, abfd
) != 8)
2094 adata
= bfd_alloc (abfd
, sizeof (mach_o_fat_data_struct
));
2098 adata
->magic
= bfd_getb32 (buf
);
2099 adata
->nfat_arch
= bfd_getb32 (buf
+ 4);
2100 if (adata
->magic
!= 0xcafebabe)
2102 /* Avoid matching Java bytecode files, which have the same magic number.
2103 In the Java bytecode file format this field contains the JVM version,
2104 which starts at 43.0. */
2105 if (adata
->nfat_arch
> 30)
2108 adata
->archentries
=
2109 bfd_alloc (abfd
, adata
->nfat_arch
* sizeof (mach_o_fat_archentry
));
2110 if (adata
->archentries
== NULL
)
2113 for (i
= 0; i
< adata
->nfat_arch
; i
++)
2115 bfd_seek (abfd
, 8 + 20 * i
, SEEK_SET
);
2117 if (bfd_bread ((PTR
) buf
, 20, abfd
) != 20)
2119 adata
->archentries
[i
].cputype
= bfd_getb32 (buf
);
2120 adata
->archentries
[i
].cpusubtype
= bfd_getb32 (buf
+ 4);
2121 adata
->archentries
[i
].offset
= bfd_getb32 (buf
+ 8);
2122 adata
->archentries
[i
].size
= bfd_getb32 (buf
+ 12);
2123 adata
->archentries
[i
].align
= bfd_getb32 (buf
+ 16);
2126 abfd
->tdata
.mach_o_fat_data
= adata
;
2131 bfd_release (abfd
, adata
);
2132 bfd_set_error (bfd_error_wrong_format
);
2137 bfd_mach_o_openr_next_archived_file (bfd
*archive
, bfd
*prev
)
2139 mach_o_fat_data_struct
*adata
;
2140 mach_o_fat_archentry
*entry
= NULL
;
2143 enum bfd_architecture arch_type
;
2144 unsigned long arch_subtype
;
2146 adata
= (mach_o_fat_data_struct
*) archive
->tdata
.mach_o_fat_data
;
2147 BFD_ASSERT (adata
!= NULL
);
2149 /* Find index of previous entry. */
2151 i
= 0; /* Start at first one. */
2154 for (i
= 0; i
< adata
->nfat_arch
; i
++)
2156 if (adata
->archentries
[i
].offset
== prev
->origin
)
2160 if (i
== adata
->nfat_arch
)
2163 bfd_set_error (bfd_error_bad_value
);
2166 i
++; /* Get next entry. */
2169 if (i
>= adata
->nfat_arch
)
2171 bfd_set_error (bfd_error_no_more_archived_files
);
2175 entry
= &adata
->archentries
[i
];
2176 nbfd
= _bfd_new_bfd_contained_in (archive
);
2180 nbfd
->origin
= entry
->offset
;
2182 bfd_mach_o_convert_architecture (entry
->cputype
, entry
->cpusubtype
,
2183 &arch_type
, &arch_subtype
);
2184 /* Create the member filename.
2185 Use FILENAME:ARCH_NAME. */
2188 const char *arch_name
;
2189 size_t arch_file_len
= strlen (bfd_get_filename (archive
));
2191 arch_name
= bfd_printable_arch_mach (arch_type
, arch_subtype
);
2192 s
= bfd_malloc (arch_file_len
+ 1 + strlen (arch_name
) + 1);
2195 memcpy (s
, bfd_get_filename (archive
), arch_file_len
);
2196 s
[arch_file_len
] = ':';
2197 strcpy (s
+ arch_file_len
+ 1, arch_name
);
2200 nbfd
->iostream
= NULL
;
2201 bfd_set_arch_mach (nbfd
, arch_type
, arch_subtype
);
2206 /* If ABFD format is FORMAT and architecture is ARCH, return it.
2207 If ABFD is a fat image containing a member that corresponds to FORMAT
2208 and ARCH, returns it.
2209 In other case, returns NULL.
2210 This function allows transparent uses of fat images. */
2212 bfd_mach_o_fat_extract (bfd
*abfd
,
2214 const bfd_arch_info_type
*arch
)
2217 mach_o_fat_data_struct
*adata
;
2220 if (bfd_check_format (abfd
, format
))
2222 if (bfd_get_arch_info (abfd
) == arch
)
2226 if (!bfd_check_format (abfd
, bfd_archive
)
2227 || abfd
->xvec
!= &mach_o_fat_vec
)
2230 /* This is a Mach-O fat image. */
2231 adata
= (mach_o_fat_data_struct
*) abfd
->tdata
.mach_o_fat_data
;
2232 BFD_ASSERT (adata
!= NULL
);
2234 for (i
= 0; i
< adata
->nfat_arch
; i
++)
2236 struct mach_o_fat_archentry
*e
= &adata
->archentries
[i
];
2237 enum bfd_architecture cpu_type
;
2238 unsigned long cpu_subtype
;
2240 bfd_mach_o_convert_architecture (e
->cputype
, e
->cpusubtype
,
2241 &cpu_type
, &cpu_subtype
);
2242 if (cpu_type
!= arch
->arch
|| cpu_subtype
!= arch
->mach
)
2245 /* The architecture is found. */
2246 res
= _bfd_new_bfd_contained_in (abfd
);
2250 res
->origin
= e
->offset
;
2252 res
->filename
= strdup (abfd
->filename
);
2253 res
->iostream
= NULL
;
2255 if (bfd_check_format (res
, format
))
2257 BFD_ASSERT (bfd_get_arch_info (res
) == arch
);
2268 bfd_mach_o_lookup_section (bfd
*abfd
,
2270 bfd_mach_o_load_command
**mcommand
,
2271 bfd_mach_o_section
**msection
)
2273 struct mach_o_data_struct
*md
= abfd
->tdata
.mach_o_data
;
2274 unsigned int i
, j
, num
;
2276 bfd_mach_o_load_command
*ncmd
= NULL
;
2277 bfd_mach_o_section
*nsect
= NULL
;
2279 BFD_ASSERT (mcommand
!= NULL
);
2280 BFD_ASSERT (msection
!= NULL
);
2283 for (i
= 0; i
< md
->header
.ncmds
; i
++)
2285 struct bfd_mach_o_load_command
*cmd
= &md
->commands
[i
];
2286 struct bfd_mach_o_segment_command
*seg
= NULL
;
2288 if (cmd
->type
!= BFD_MACH_O_LC_SEGMENT
2289 || cmd
->type
!= BFD_MACH_O_LC_SEGMENT_64
)
2291 seg
= &cmd
->command
.segment
;
2293 if (seg
->segment
== section
)
2300 for (j
= 0; j
< seg
->nsects
; j
++)
2302 struct bfd_mach_o_section
*sect
= &seg
->sections
[j
];
2304 if (sect
->bfdsection
== section
)
2319 bfd_mach_o_lookup_command (bfd
*abfd
,
2320 bfd_mach_o_load_command_type type
,
2321 bfd_mach_o_load_command
**mcommand
)
2323 struct mach_o_data_struct
*md
= NULL
;
2324 bfd_mach_o_load_command
*ncmd
= NULL
;
2325 unsigned int i
, num
;
2327 md
= abfd
->tdata
.mach_o_data
;
2329 BFD_ASSERT (md
!= NULL
);
2330 BFD_ASSERT (mcommand
!= NULL
);
2333 for (i
= 0; i
< md
->header
.ncmds
; i
++)
2335 struct bfd_mach_o_load_command
*cmd
= &md
->commands
[i
];
2337 if (cmd
->type
!= type
)
2350 bfd_mach_o_stack_addr (enum bfd_mach_o_cpu_type type
)
2354 case BFD_MACH_O_CPU_TYPE_MC680x0
:
2356 case BFD_MACH_O_CPU_TYPE_MC88000
:
2358 case BFD_MACH_O_CPU_TYPE_POWERPC
:
2360 case BFD_MACH_O_CPU_TYPE_I386
:
2362 case BFD_MACH_O_CPU_TYPE_SPARC
:
2364 case BFD_MACH_O_CPU_TYPE_I860
:
2366 case BFD_MACH_O_CPU_TYPE_HPPA
:
2367 return 0xc0000000 - 0x04000000;
2374 bfd_mach_o_bfd_print_private_bfd_data (bfd
*abfd
, PTR ptr
)
2376 bfd_mach_o_data_struct
*mdata
= abfd
->tdata
.mach_o_data
;
2377 FILE *file
= (FILE *) ptr
;
2379 unsigned int sec_nbr
= 0;
2381 fprintf (file
, _("Segments and Sections:\n"));
2382 fprintf (file
, _(" #: Segment name Section name Address\n"));
2384 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
2386 bfd_mach_o_segment_command
*seg
;
2388 if (mdata
->commands
[i
].type
!= BFD_MACH_O_LC_SEGMENT
2389 && mdata
->commands
[i
].type
!= BFD_MACH_O_LC_SEGMENT_64
)
2392 seg
= &mdata
->commands
[i
].command
.segment
;
2394 fprintf (file
, "[Segment %-16s ", seg
->segname
);
2395 fprintf_vma (file
, seg
->vmaddr
);
2396 fprintf (file
, "-");
2397 fprintf_vma (file
, seg
->vmaddr
+ seg
->vmsize
- 1);
2399 fputc (seg
->initprot
& BFD_MACH_O_PROT_READ
? 'r' : '-', file
);
2400 fputc (seg
->initprot
& BFD_MACH_O_PROT_WRITE
? 'w' : '-', file
);
2401 fputc (seg
->initprot
& BFD_MACH_O_PROT_EXECUTE
? 'x' : '-', file
);
2402 fprintf (file
, "]\n");
2403 for (j
= 0; j
< seg
->nsects
; j
++)
2405 bfd_mach_o_section
*sec
= &seg
->sections
[j
];
2406 fprintf (file
, "%02u: %-16s %-16s ", ++sec_nbr
,
2407 sec
->segname
, sec
->sectname
);
2408 fprintf_vma (file
, sec
->addr
);
2409 fprintf (file
, " ");
2410 fprintf_vma (file
, sec
->size
);
2411 fprintf (file
, " %08lx\n", sec
->flags
);
2415 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
2417 bfd_mach_o_load_command
*cmd
= &mdata
->commands
[i
];
2421 case BFD_MACH_O_LC_SEGMENT
:
2422 case BFD_MACH_O_LC_SEGMENT_64
:
2424 case BFD_MACH_O_LC_UUID
:
2426 bfd_mach_o_uuid_command
*uuid
= &cmd
->command
.uuid
;
2431 for (i
= 0; i
< sizeof (uuid
->uuid
); i
++)
2432 fprintf (file
, " %02x", uuid
->uuid
[i
]);
2436 case BFD_MACH_O_LC_LOAD_DYLIB
:
2438 bfd_mach_o_dylib_command
*dylib
= &cmd
->command
.dylib
;
2439 bfd_byte
*data
= NULL
;
2441 if (! bfd_malloc_and_get_section (abfd
, dylib
->section
, &data
))
2449 data
+ dylib
->name_offset
- cmd
->offset
- 8);
2450 fprintf (file
, " time stamp: 0x%08lx\n",
2452 fprintf (file
, " current version: 0x%08lx\n",
2453 dylib
->current_version
);
2454 fprintf (file
, " comptibility version: 0x%08lx\n",
2455 dylib
->compatibility_version
);
2459 case BFD_MACH_O_LC_LOAD_DYLINKER
:
2461 bfd_mach_o_dylinker_command
*linker
= &cmd
->command
.dylinker
;
2462 bfd_byte
*data
= NULL
;
2464 if (! bfd_malloc_and_get_section (abfd
, linker
->section
, &data
))
2471 "LOAD_DYLINKER: %s\n",
2472 data
+ linker
->name_offset
- cmd
->offset
- 8);
2476 case BFD_MACH_O_LC_SYMTAB
:
2478 bfd_mach_o_symtab_command
*symtab
= &cmd
->command
.symtab
;
2480 "LC_SYMTAB: nsyms: %lu, strsize: %lu\n",
2481 symtab
->nsyms
, symtab
->strsize
);
2484 case BFD_MACH_O_LC_DYSYMTAB
:
2486 bfd_mach_o_dysymtab_command
*dysymtab
= &cmd
->command
.dysymtab
;
2489 " local symbols: index: %lu number: %lu\n",
2490 dysymtab
->ilocalsym
, dysymtab
->nlocalsym
);
2492 " external symbols: index: %lu number: %lu\n",
2493 dysymtab
->iextdefsym
, dysymtab
->nextdefsym
);
2495 " undefined symbols: index: %lu number: %lu\n",
2496 dysymtab
->iundefsym
, dysymtab
->nundefsym
);
2498 " ntoc: offset: %lu number: %lu\n",
2499 dysymtab
->tocoff
, dysymtab
->ntoc
);
2501 " module table: offset: %lu number: %lu\n",
2502 dysymtab
->modtaboff
, dysymtab
->nmodtab
);
2506 fprintf (file
, "LC_%d\n", cmd
->type
);
2515 bfd_mach_o_core_fetch_environment (bfd
*abfd
,
2516 unsigned char **rbuf
,
2519 bfd_mach_o_data_struct
*mdata
= abfd
->tdata
.mach_o_data
;
2520 unsigned long stackaddr
= bfd_mach_o_stack_addr (mdata
->header
.cputype
);
2523 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
2525 bfd_mach_o_load_command
*cur
= &mdata
->commands
[i
];
2526 bfd_mach_o_segment_command
*seg
= NULL
;
2528 if (cur
->type
!= BFD_MACH_O_LC_SEGMENT
)
2531 seg
= &cur
->command
.segment
;
2533 if ((seg
->vmaddr
+ seg
->vmsize
) == stackaddr
)
2535 unsigned long start
= seg
->fileoff
;
2536 unsigned long end
= seg
->fileoff
+ seg
->filesize
;
2537 unsigned char *buf
= bfd_malloc (1024);
2538 unsigned long size
= 1024;
2542 bfd_size_type nread
= 0;
2543 unsigned long offset
;
2544 int found_nonnull
= 0;
2546 if (size
> (end
- start
))
2547 size
= (end
- start
);
2549 buf
= bfd_realloc_or_free (buf
, size
);
2553 bfd_seek (abfd
, end
- size
, SEEK_SET
);
2554 nread
= bfd_bread (buf
, size
, abfd
);
2562 for (offset
= 4; offset
<= size
; offset
+= 4)
2566 val
= *((unsigned long *) (buf
+ size
- offset
));
2567 if (! found_nonnull
)
2572 else if (val
== 0x0)
2574 unsigned long bottom
;
2577 bottom
= seg
->fileoff
+ seg
->filesize
- offset
;
2578 top
= seg
->fileoff
+ seg
->filesize
- 4;
2579 *rbuf
= bfd_malloc (top
- bottom
);
2580 *rlen
= top
- bottom
;
2582 memcpy (*rbuf
, buf
+ size
- *rlen
, *rlen
);
2588 if (size
== (end
- start
))
2602 bfd_mach_o_core_file_failing_command (bfd
*abfd
)
2604 unsigned char *buf
= NULL
;
2605 unsigned int len
= 0;
2608 ret
= bfd_mach_o_core_fetch_environment (abfd
, &buf
, &len
);
2612 return (char *) buf
;
2616 bfd_mach_o_core_file_failing_signal (bfd
*abfd ATTRIBUTE_UNUSED
)
2621 #define TARGET_NAME mach_o_be_vec
2622 #define TARGET_STRING "mach-o-be"
2623 #define TARGET_BIG_ENDIAN 1
2624 #define TARGET_ARCHIVE 0
2626 #include "mach-o-target.c"
2629 #undef TARGET_STRING
2630 #undef TARGET_BIG_ENDIAN
2631 #undef TARGET_ARCHIVE
2633 #define TARGET_NAME mach_o_le_vec
2634 #define TARGET_STRING "mach-o-le"
2635 #define TARGET_BIG_ENDIAN 0
2636 #define TARGET_ARCHIVE 0
2638 #include "mach-o-target.c"
2641 #undef TARGET_STRING
2642 #undef TARGET_BIG_ENDIAN
2643 #undef TARGET_ARCHIVE
2645 #define TARGET_NAME mach_o_fat_vec
2646 #define TARGET_STRING "mach-o-fat"
2647 #define TARGET_BIG_ENDIAN 1
2648 #define TARGET_ARCHIVE 1
2650 #include "mach-o-target.c"
2653 #undef TARGET_STRING
2654 #undef TARGET_BIG_ENDIAN
2655 #undef TARGET_ARCHIVE