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_copy_private_header_data _bfd_generic_bfd_copy_private_header_data
76 #define bfd_mach_o_core_file_matches_executable_p generic_core_file_matches_executable_p
79 bfd_mach_o_version (bfd
*abfd
)
81 bfd_mach_o_data_struct
*mdata
= NULL
;
83 BFD_ASSERT (bfd_mach_o_valid (abfd
));
84 mdata
= abfd
->tdata
.mach_o_data
;
86 return mdata
->header
.version
;
90 bfd_mach_o_valid (bfd
*abfd
)
92 if (abfd
== NULL
|| abfd
->xvec
== NULL
)
95 if (! ((abfd
->xvec
== &mach_o_be_vec
)
96 || (abfd
->xvec
== &mach_o_le_vec
)
97 || (abfd
->xvec
== &mach_o_fat_vec
)))
100 if (abfd
->tdata
.mach_o_data
== NULL
)
105 /* Copy any private info we understand from the input symbol
106 to the output symbol. */
109 bfd_mach_o_bfd_copy_private_symbol_data (bfd
*ibfd ATTRIBUTE_UNUSED
,
110 asymbol
*isymbol ATTRIBUTE_UNUSED
,
111 bfd
*obfd ATTRIBUTE_UNUSED
,
112 asymbol
*osymbol ATTRIBUTE_UNUSED
)
117 /* Copy any private info we understand from the input section
118 to the output section. */
121 bfd_mach_o_bfd_copy_private_section_data (bfd
*ibfd ATTRIBUTE_UNUSED
,
122 asection
*isection ATTRIBUTE_UNUSED
,
123 bfd
*obfd ATTRIBUTE_UNUSED
,
124 asection
*osection ATTRIBUTE_UNUSED
)
129 /* Copy any private info we understand from the input bfd
130 to the output bfd. */
133 bfd_mach_o_bfd_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
135 BFD_ASSERT (bfd_mach_o_valid (ibfd
));
136 BFD_ASSERT (bfd_mach_o_valid (obfd
));
138 obfd
->tdata
.mach_o_data
= ibfd
->tdata
.mach_o_data
;
139 obfd
->tdata
.mach_o_data
->ibfd
= ibfd
;
144 bfd_mach_o_count_symbols (bfd
*abfd
)
146 bfd_mach_o_data_struct
*mdata
= NULL
;
150 BFD_ASSERT (bfd_mach_o_valid (abfd
));
151 mdata
= abfd
->tdata
.mach_o_data
;
153 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
154 if (mdata
->commands
[i
].type
== BFD_MACH_O_LC_SYMTAB
)
156 bfd_mach_o_symtab_command
*sym
= &mdata
->commands
[i
].command
.symtab
;
164 bfd_mach_o_get_symtab_upper_bound (bfd
*abfd
)
166 long nsyms
= bfd_mach_o_count_symbols (abfd
);
171 return ((nsyms
+ 1) * sizeof (asymbol
*));
175 bfd_mach_o_canonicalize_symtab (bfd
*abfd
, asymbol
**alocation
)
177 bfd_mach_o_data_struct
*mdata
= abfd
->tdata
.mach_o_data
;
178 long nsyms
= bfd_mach_o_count_symbols (abfd
);
179 asymbol
**csym
= alocation
;
185 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
187 if (mdata
->commands
[i
].type
== BFD_MACH_O_LC_SYMTAB
)
189 bfd_mach_o_symtab_command
*sym
= &mdata
->commands
[i
].command
.symtab
;
191 if (bfd_mach_o_scan_read_symtab_symbols (abfd
, &mdata
->commands
[i
].command
.symtab
) != 0)
193 fprintf (stderr
, "bfd_mach_o_canonicalize_symtab: unable to load symbols for section %lu\n", i
);
197 BFD_ASSERT (sym
->symbols
!= NULL
);
199 for (j
= 0; j
< sym
->nsyms
; j
++)
201 BFD_ASSERT (csym
< (alocation
+ nsyms
));
202 *csym
++ = &sym
->symbols
[j
];
213 bfd_mach_o_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
217 bfd_symbol_info (symbol
, ret
);
221 bfd_mach_o_print_symbol (bfd
*abfd
,
224 bfd_print_symbol_type how
)
226 FILE *file
= (FILE *) afile
;
234 case bfd_print_symbol_name
:
235 fprintf (file
, "%s", symbol
->name
);
238 bfd_print_symbol_vandf (abfd
, (PTR
) file
, symbol
);
239 ntype
= BFD_MACH_O_SYM_NTYPE (symbol
);
240 nsect
= BFD_MACH_O_SYM_NSECT (symbol
);
241 ndesc
= BFD_MACH_O_SYM_NDESC (symbol
);
242 if (ntype
& BFD_MACH_O_N_STAB
)
243 name
= bfd_get_stab_name (ntype
);
245 switch (ntype
& BFD_MACH_O_N_TYPE
)
247 case BFD_MACH_O_N_UNDF
:
250 case BFD_MACH_O_N_ABS
:
253 case BFD_MACH_O_N_INDR
:
256 case BFD_MACH_O_N_PBUD
:
259 case BFD_MACH_O_N_SECT
:
268 fprintf (file
, " %02x %-6s %02x %04x", ntype
, name
, nsect
, ndesc
);
269 if ((ntype
& BFD_MACH_O_N_STAB
) == 0
270 && (ntype
& BFD_MACH_O_N_TYPE
) == BFD_MACH_O_N_SECT
)
271 fprintf (file
, " %-5s", symbol
->section
->name
);
272 fprintf (file
, " %s", symbol
->name
);
277 bfd_mach_o_convert_architecture (bfd_mach_o_cpu_type mtype
,
278 bfd_mach_o_cpu_subtype msubtype ATTRIBUTE_UNUSED
,
279 enum bfd_architecture
*type
,
280 unsigned long *subtype
)
282 *subtype
= bfd_arch_unknown
;
286 case BFD_MACH_O_CPU_TYPE_VAX
: *type
= bfd_arch_vax
; break;
287 case BFD_MACH_O_CPU_TYPE_MC680x0
: *type
= bfd_arch_m68k
; break;
288 case BFD_MACH_O_CPU_TYPE_I386
:
289 *type
= bfd_arch_i386
;
290 *subtype
= bfd_mach_i386_i386
;
292 case BFD_MACH_O_CPU_TYPE_X86_64
:
293 *type
= bfd_arch_i386
;
294 *subtype
= bfd_mach_x86_64
;
296 case BFD_MACH_O_CPU_TYPE_MIPS
: *type
= bfd_arch_mips
; break;
297 case BFD_MACH_O_CPU_TYPE_MC98000
: *type
= bfd_arch_m98k
; break;
298 case BFD_MACH_O_CPU_TYPE_HPPA
: *type
= bfd_arch_hppa
; break;
299 case BFD_MACH_O_CPU_TYPE_ARM
: *type
= bfd_arch_arm
; break;
300 case BFD_MACH_O_CPU_TYPE_MC88000
: *type
= bfd_arch_m88k
; break;
301 case BFD_MACH_O_CPU_TYPE_SPARC
:
302 *type
= bfd_arch_sparc
;
303 *subtype
= bfd_mach_sparc
;
305 case BFD_MACH_O_CPU_TYPE_I860
: *type
= bfd_arch_i860
; break;
306 case BFD_MACH_O_CPU_TYPE_ALPHA
: *type
= bfd_arch_alpha
; break;
307 case BFD_MACH_O_CPU_TYPE_POWERPC
:
308 *type
= bfd_arch_powerpc
;
309 *subtype
= bfd_mach_ppc
;
311 case BFD_MACH_O_CPU_TYPE_POWERPC_64
:
312 *type
= bfd_arch_powerpc
;
313 *subtype
= bfd_mach_ppc64
;
316 *type
= bfd_arch_unknown
;
322 bfd_mach_o_write_header (bfd
*abfd
, bfd_mach_o_header
*header
)
324 unsigned char buf
[32];
327 size
= (header
->version
== 2) ? 32 : 28;
329 bfd_h_put_32 (abfd
, header
->magic
, buf
+ 0);
330 bfd_h_put_32 (abfd
, header
->cputype
, buf
+ 4);
331 bfd_h_put_32 (abfd
, header
->cpusubtype
, buf
+ 8);
332 bfd_h_put_32 (abfd
, header
->filetype
, buf
+ 12);
333 bfd_h_put_32 (abfd
, header
->ncmds
, buf
+ 16);
334 bfd_h_put_32 (abfd
, header
->sizeofcmds
, buf
+ 20);
335 bfd_h_put_32 (abfd
, header
->flags
, buf
+ 24);
337 if (header
->version
== 2)
338 bfd_h_put_32 (abfd
, header
->reserved
, buf
+ 28);
340 bfd_seek (abfd
, 0, SEEK_SET
);
341 if (bfd_bwrite ((PTR
) buf
, size
, abfd
) != size
)
348 bfd_mach_o_scan_write_thread (bfd
*abfd
, bfd_mach_o_load_command
*command
)
350 bfd_mach_o_thread_command
*cmd
= &command
->command
.thread
;
352 unsigned char buf
[8];
354 unsigned int nflavours
;
356 BFD_ASSERT ((command
->type
== BFD_MACH_O_LC_THREAD
)
357 || (command
->type
== BFD_MACH_O_LC_UNIXTHREAD
));
361 for (i
= 0; i
< cmd
->nflavours
; i
++)
363 BFD_ASSERT ((cmd
->flavours
[i
].size
% 4) == 0);
364 BFD_ASSERT (cmd
->flavours
[i
].offset
== (command
->offset
+ offset
+ 8));
366 bfd_h_put_32 (abfd
, cmd
->flavours
[i
].flavour
, buf
);
367 bfd_h_put_32 (abfd
, (cmd
->flavours
[i
].size
/ 4), buf
+ 4);
369 bfd_seek (abfd
, command
->offset
+ offset
, SEEK_SET
);
370 if (bfd_bwrite ((PTR
) buf
, 8, abfd
) != 8)
373 offset
+= cmd
->flavours
[i
].size
+ 8;
380 bfd_mach_o_scan_write_section_32 (bfd
*abfd
,
381 bfd_mach_o_section
*section
,
384 unsigned char buf
[68];
386 memcpy (buf
, section
->sectname
, 16);
387 memcpy (buf
+ 16, section
->segname
, 16);
388 bfd_h_put_32 (abfd
, section
->addr
, buf
+ 32);
389 bfd_h_put_32 (abfd
, section
->size
, buf
+ 36);
390 bfd_h_put_32 (abfd
, section
->offset
, buf
+ 40);
391 bfd_h_put_32 (abfd
, section
->align
, buf
+ 44);
392 bfd_h_put_32 (abfd
, section
->reloff
, buf
+ 48);
393 bfd_h_put_32 (abfd
, section
->nreloc
, buf
+ 52);
394 bfd_h_put_32 (abfd
, section
->flags
, buf
+ 56);
395 bfd_h_put_32 (abfd
, section
->reserved1
, buf
+ 60);
396 bfd_h_put_32 (abfd
, section
->reserved2
, buf
+ 64);
398 bfd_seek (abfd
, offset
, SEEK_SET
);
399 if (bfd_bwrite ((PTR
) buf
, 68, abfd
) != 68)
406 bfd_mach_o_scan_write_section_64 (bfd
*abfd
,
407 bfd_mach_o_section
*section
,
410 unsigned char buf
[80];
412 memcpy (buf
, section
->sectname
, 16);
413 memcpy (buf
+ 16, section
->segname
, 16);
414 bfd_h_put_64 (abfd
, section
->addr
, buf
+ 32);
415 bfd_h_put_64 (abfd
, section
->size
, buf
+ 40);
416 bfd_h_put_32 (abfd
, section
->offset
, buf
+ 48);
417 bfd_h_put_32 (abfd
, section
->align
, buf
+ 52);
418 bfd_h_put_32 (abfd
, section
->reloff
, buf
+ 56);
419 bfd_h_put_32 (abfd
, section
->nreloc
, buf
+ 60);
420 bfd_h_put_32 (abfd
, section
->flags
, buf
+ 64);
421 bfd_h_put_32 (abfd
, section
->reserved1
, buf
+ 68);
422 bfd_h_put_32 (abfd
, section
->reserved2
, buf
+ 72);
423 bfd_h_put_32 (abfd
, section
->reserved3
, buf
+ 76);
425 bfd_seek (abfd
, offset
, SEEK_SET
);
426 if (bfd_bwrite ((PTR
) buf
, 80, abfd
) != 80)
433 bfd_mach_o_scan_write_section (bfd
*abfd
,
434 bfd_mach_o_section
*section
,
439 return bfd_mach_o_scan_write_section_64 (abfd
, section
, offset
);
441 return bfd_mach_o_scan_write_section_32 (abfd
, section
, offset
);
445 bfd_mach_o_scan_write_segment (bfd
*abfd
,
446 bfd_mach_o_load_command
*command
,
449 unsigned char buf
[64];
450 bfd_mach_o_segment_command
*seg
= &command
->command
.segment
;
455 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SEGMENT_64
);
457 memcpy (buf
, seg
->segname
, 16);
459 bfd_h_put_64 (abfd
, seg
->vmaddr
, buf
+ 16);
460 bfd_h_put_64 (abfd
, seg
->vmsize
, buf
+ 24);
461 bfd_h_put_64 (abfd
, seg
->fileoff
, buf
+ 32);
462 bfd_h_put_64 (abfd
, seg
->filesize
, buf
+ 40);
463 bfd_h_put_32 (abfd
, seg
->maxprot
, buf
+ 48);
464 bfd_h_put_32 (abfd
, seg
->initprot
, buf
+ 52);
465 bfd_h_put_32 (abfd
, seg
->nsects
, buf
+ 56);
466 bfd_h_put_32 (abfd
, seg
->flags
, buf
+ 60);
468 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
469 if (bfd_bwrite ((PTR
) buf
, 64, abfd
) != 64)
474 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SEGMENT
);
476 memcpy (buf
, seg
->segname
, 16);
478 bfd_h_put_32 (abfd
, seg
->vmaddr
, buf
+ 16);
479 bfd_h_put_32 (abfd
, seg
->vmsize
, buf
+ 20);
480 bfd_h_put_32 (abfd
, seg
->fileoff
, buf
+ 24);
481 bfd_h_put_32 (abfd
, seg
->filesize
, buf
+ 28);
482 bfd_h_put_32 (abfd
, seg
->maxprot
, buf
+ 32);
483 bfd_h_put_32 (abfd
, seg
->initprot
, buf
+ 36);
484 bfd_h_put_32 (abfd
, seg
->nsects
, buf
+ 40);
485 bfd_h_put_32 (abfd
, seg
->flags
, buf
+ 44);
487 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
488 if (bfd_bwrite ((PTR
) buf
, 48, abfd
) != 48)
494 bfd_vma nbytes
= seg
->filesize
;
495 bfd_vma curoff
= seg
->fileoff
;
499 bfd_vma thiswrite
= nbytes
;
501 if (thiswrite
> 1024)
504 bfd_seek (abfd
, curoff
, SEEK_SET
);
505 if (bfd_bread ((PTR
) buf
, thiswrite
, abfd
) != thiswrite
)
508 bfd_seek (abfd
, curoff
, SEEK_SET
);
509 if (bfd_bwrite ((PTR
) buf
, thiswrite
, abfd
) != thiswrite
)
517 for (i
= 0; i
< seg
->nsects
; i
++)
521 segoff
= command
->offset
+ 64 + 8 + (i
* 80);
523 segoff
= command
->offset
+ 48 + 8 + (i
* 68);
525 if (bfd_mach_o_scan_write_section
526 (abfd
, &seg
->sections
[i
], segoff
, wide
) != 0)
534 bfd_mach_o_scan_write_segment_32 (bfd
*abfd
, bfd_mach_o_load_command
*command
)
536 return bfd_mach_o_scan_write_segment (abfd
, command
, 0);
540 bfd_mach_o_scan_write_segment_64 (bfd
*abfd
, bfd_mach_o_load_command
*command
)
542 return bfd_mach_o_scan_write_segment (abfd
, command
, 1);
546 bfd_mach_o_scan_write_symtab_symbols (bfd
*abfd
, bfd_mach_o_load_command
*command
)
548 bfd_mach_o_symtab_command
*sym
= &command
->command
.symtab
;
552 for (i
= 0; i
< sym
->nsyms
; i
++)
554 unsigned char buf
[12];
555 bfd_vma symoff
= sym
->symoff
+ (i
* 12);
556 unsigned char ntype
= 0;
557 unsigned char nsect
= 0;
560 s
= &sym
->symbols
[i
];
562 /* Instead just set from the stored values. */
563 ntype
= BFD_MACH_O_SYM_NTYPE (s
);
564 nsect
= BFD_MACH_O_SYM_NSECT (s
);
565 ndesc
= BFD_MACH_O_SYM_NDESC (s
);
567 bfd_h_put_32 (abfd
, s
->name
- sym
->strtab
, buf
);
568 bfd_h_put_8 (abfd
, ntype
, buf
+ 4);
569 bfd_h_put_8 (abfd
, nsect
, buf
+ 5);
570 bfd_h_put_16 (abfd
, ndesc
, buf
+ 6);
571 bfd_h_put_32 (abfd
, s
->section
->vma
+ s
->value
, buf
+ 8);
573 bfd_seek (abfd
, symoff
, SEEK_SET
);
574 if (bfd_bwrite ((PTR
) buf
, 12, abfd
) != 12)
576 fprintf (stderr
, "bfd_mach_o_scan_write_symtab_symbols: unable to write %d bytes at %lu\n",
577 12, (unsigned long) symoff
);
586 bfd_mach_o_scan_write_symtab (bfd
*abfd
, bfd_mach_o_load_command
*command
)
588 bfd_mach_o_symtab_command
*seg
= &command
->command
.symtab
;
589 unsigned char buf
[16];
591 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SYMTAB
);
593 bfd_h_put_32 (abfd
, seg
->symoff
, buf
);
594 bfd_h_put_32 (abfd
, seg
->nsyms
, buf
+ 4);
595 bfd_h_put_32 (abfd
, seg
->stroff
, buf
+ 8);
596 bfd_h_put_32 (abfd
, seg
->strsize
, buf
+ 12);
598 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
599 if (bfd_bwrite ((PTR
) buf
, 16, abfd
) != 16)
602 if (bfd_mach_o_scan_write_symtab_symbols (abfd
, command
) != 0)
609 bfd_mach_o_write_contents (bfd
*abfd
)
614 bfd_mach_o_data_struct
*mdata
= abfd
->tdata
.mach_o_data
;
616 /* Write data sections first in case they overlap header data to be
619 for (s
= abfd
->sections
; s
!= (asection
*) NULL
; s
= s
->next
)
622 /* Now write header information. */
623 if (bfd_mach_o_write_header (abfd
, &mdata
->header
) != 0)
626 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
628 unsigned char buf
[8];
629 bfd_mach_o_load_command
*cur
= &mdata
->commands
[i
];
630 unsigned long typeflag
;
632 typeflag
= cur
->type_required
? cur
->type
& BFD_MACH_O_LC_REQ_DYLD
: cur
->type
;
634 bfd_h_put_32 (abfd
, typeflag
, buf
);
635 bfd_h_put_32 (abfd
, cur
->len
, buf
+ 4);
637 bfd_seek (abfd
, cur
->offset
, SEEK_SET
);
638 if (bfd_bwrite ((PTR
) buf
, 8, abfd
) != 8)
643 case BFD_MACH_O_LC_SEGMENT
:
644 if (bfd_mach_o_scan_write_segment_32 (abfd
, cur
) != 0)
647 case BFD_MACH_O_LC_SEGMENT_64
:
648 if (bfd_mach_o_scan_write_segment_64 (abfd
, cur
) != 0)
651 case BFD_MACH_O_LC_SYMTAB
:
652 if (bfd_mach_o_scan_write_symtab (abfd
, cur
) != 0)
655 case BFD_MACH_O_LC_SYMSEG
:
657 case BFD_MACH_O_LC_THREAD
:
658 case BFD_MACH_O_LC_UNIXTHREAD
:
659 if (bfd_mach_o_scan_write_thread (abfd
, cur
) != 0)
662 case BFD_MACH_O_LC_LOADFVMLIB
:
663 case BFD_MACH_O_LC_IDFVMLIB
:
664 case BFD_MACH_O_LC_IDENT
:
665 case BFD_MACH_O_LC_FVMFILE
:
666 case BFD_MACH_O_LC_PREPAGE
:
667 case BFD_MACH_O_LC_DYSYMTAB
:
668 case BFD_MACH_O_LC_LOAD_DYLIB
:
669 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB
:
670 case BFD_MACH_O_LC_ID_DYLIB
:
671 case BFD_MACH_O_LC_LOAD_DYLINKER
:
672 case BFD_MACH_O_LC_ID_DYLINKER
:
673 case BFD_MACH_O_LC_PREBOUND_DYLIB
:
674 case BFD_MACH_O_LC_ROUTINES
:
675 case BFD_MACH_O_LC_SUB_FRAMEWORK
:
679 "unable to write unknown load command 0x%lx\n",
680 (unsigned long) cur
->type
);
689 bfd_mach_o_sizeof_headers (bfd
*a ATTRIBUTE_UNUSED
,
690 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
695 /* Make an empty symbol. This is required only because
696 bfd_make_section_anyway wants to create a symbol for the section. */
699 bfd_mach_o_make_empty_symbol (bfd
*abfd
)
703 new = bfd_zalloc (abfd
, sizeof (* new));
711 bfd_mach_o_read_header (bfd
*abfd
, bfd_mach_o_header
*header
)
713 unsigned char buf
[32];
715 bfd_vma (*get32
) (const void *) = NULL
;
717 bfd_seek (abfd
, 0, SEEK_SET
);
719 /* Just read the magic number. */
720 if (bfd_bread ((PTR
) buf
, 4, abfd
) != 4)
723 if (bfd_getb32 (buf
) == 0xfeedface)
725 header
->byteorder
= BFD_ENDIAN_BIG
;
726 header
->magic
= 0xfeedface;
730 else if (bfd_getl32 (buf
) == 0xfeedface)
732 header
->byteorder
= BFD_ENDIAN_LITTLE
;
733 header
->magic
= 0xfeedface;
737 else if (bfd_getb32 (buf
) == 0xfeedfacf)
739 header
->byteorder
= BFD_ENDIAN_BIG
;
740 header
->magic
= 0xfeedfacf;
744 else if (bfd_getl32 (buf
) == 0xfeedfacf)
746 header
->byteorder
= BFD_ENDIAN_LITTLE
;
747 header
->magic
= 0xfeedfacf;
753 header
->byteorder
= BFD_ENDIAN_UNKNOWN
;
757 /* Once the size of the header is known, read the full header. */
758 size
= (header
->version
== 2) ? 32 : 28;
760 bfd_seek (abfd
, 0, SEEK_SET
);
761 if (bfd_bread ((PTR
) buf
, size
, abfd
) != size
)
764 header
->cputype
= (*get32
) (buf
+ 4);
765 header
->cpusubtype
= (*get32
) (buf
+ 8);
766 header
->filetype
= (*get32
) (buf
+ 12);
767 header
->ncmds
= (*get32
) (buf
+ 16);
768 header
->sizeofcmds
= (*get32
) (buf
+ 20);
769 header
->flags
= (*get32
) (buf
+ 24);
771 if (header
->version
== 2)
772 header
->reserved
= (*get32
) (buf
+ 28);
778 bfd_mach_o_make_bfd_section (bfd
*abfd
, bfd_mach_o_section
*section
,
783 const char *prefix
= "LC_SEGMENT";
784 unsigned int snamelen
;
787 snamelen
= strlen (prefix
) + 1
788 + strlen (section
->segname
) + 1
789 + strlen (section
->sectname
) + 1;
791 sname
= bfd_alloc (abfd
, snamelen
);
795 /* Use canonical dwarf section names for dwarf sections. */
796 if (strcmp (section
->segname
, "__DWARF") == 0
797 && strncmp (section
->sectname
, "__", 2) == 0)
798 sprintf (sname
, ".%s", section
->sectname
+ 2);
799 else if (strcmp (section
->segname
, "__TEXT") == 0)
801 if (strcmp (section
->sectname
, "__eh_frame") == 0)
802 strcpy (sname
, ".eh_frame");
803 else if (section
->sectname
[0])
804 sprintf (sname
, "%s.%s", section
->segname
, section
->sectname
);
806 strcpy (sname
, section
->segname
);
808 else if (strcmp (section
->segname
, "__DATA") == 0)
810 if (section
->sectname
[0])
811 sprintf (sname
, "%s.%s", section
->segname
, section
->sectname
);
813 strcpy (sname
, section
->segname
);
816 sprintf (sname
, "%s.%s.%s", prefix
, section
->segname
, section
->sectname
);
818 if (section
->flags
& BFD_MACH_O_S_ATTR_DEBUG
)
819 flags
= SEC_HAS_CONTENTS
| SEC_DEBUGGING
;
823 if ((section
->flags
& BFD_MACH_O_SECTION_TYPE_MASK
)
824 != BFD_MACH_O_S_ZEROFILL
)
826 flags
|= SEC_HAS_CONTENTS
| SEC_LOAD
;
827 if (prot
& BFD_MACH_O_PROT_EXECUTE
)
829 if (prot
& BFD_MACH_O_PROT_WRITE
)
831 else if (prot
& BFD_MACH_O_PROT_READ
)
832 flags
|= SEC_READONLY
;
835 bfdsec
= bfd_make_section_anyway_with_flags (abfd
, sname
, flags
);
839 bfdsec
->vma
= section
->addr
;
840 bfdsec
->lma
= section
->addr
;
841 bfdsec
->size
= section
->size
;
842 bfdsec
->filepos
= section
->offset
;
843 bfdsec
->alignment_power
= section
->align
;
844 bfdsec
->segment_mark
= 0;
850 bfd_mach_o_scan_read_section_32 (bfd
*abfd
,
851 bfd_mach_o_section
*section
,
855 unsigned char buf
[68];
857 bfd_seek (abfd
, offset
, SEEK_SET
);
858 if (bfd_bread ((PTR
) buf
, 68, abfd
) != 68)
861 memcpy (section
->sectname
, buf
, 16);
862 section
->sectname
[16] = '\0';
863 memcpy (section
->segname
, buf
+ 16, 16);
864 section
->segname
[16] = '\0';
865 section
->addr
= bfd_h_get_32 (abfd
, buf
+ 32);
866 section
->size
= bfd_h_get_32 (abfd
, buf
+ 36);
867 section
->offset
= bfd_h_get_32 (abfd
, buf
+ 40);
868 section
->align
= bfd_h_get_32 (abfd
, buf
+ 44);
869 section
->reloff
= bfd_h_get_32 (abfd
, buf
+ 48);
870 section
->nreloc
= bfd_h_get_32 (abfd
, buf
+ 52);
871 section
->flags
= bfd_h_get_32 (abfd
, buf
+ 56);
872 section
->reserved1
= bfd_h_get_32 (abfd
, buf
+ 60);
873 section
->reserved2
= bfd_h_get_32 (abfd
, buf
+ 64);
874 section
->reserved3
= 0;
875 section
->bfdsection
= bfd_mach_o_make_bfd_section (abfd
, section
, prot
);
877 if (section
->bfdsection
== NULL
)
884 bfd_mach_o_scan_read_section_64 (bfd
*abfd
,
885 bfd_mach_o_section
*section
,
889 unsigned char buf
[80];
891 bfd_seek (abfd
, offset
, SEEK_SET
);
892 if (bfd_bread ((PTR
) buf
, 80, abfd
) != 80)
895 memcpy (section
->sectname
, buf
, 16);
896 section
->sectname
[16] = '\0';
897 memcpy (section
->segname
, buf
+ 16, 16);
898 section
->segname
[16] = '\0';
899 section
->addr
= bfd_h_get_64 (abfd
, buf
+ 32);
900 section
->size
= bfd_h_get_64 (abfd
, buf
+ 40);
901 section
->offset
= bfd_h_get_32 (abfd
, buf
+ 48);
902 section
->align
= bfd_h_get_32 (abfd
, buf
+ 52);
903 section
->reloff
= bfd_h_get_32 (abfd
, buf
+ 56);
904 section
->nreloc
= bfd_h_get_32 (abfd
, buf
+ 60);
905 section
->flags
= bfd_h_get_32 (abfd
, buf
+ 64);
906 section
->reserved1
= bfd_h_get_32 (abfd
, buf
+ 68);
907 section
->reserved2
= bfd_h_get_32 (abfd
, buf
+ 72);
908 section
->reserved3
= bfd_h_get_32 (abfd
, buf
+ 76);
909 section
->bfdsection
= bfd_mach_o_make_bfd_section (abfd
, section
, prot
);
911 if (section
->bfdsection
== NULL
)
918 bfd_mach_o_scan_read_section (bfd
*abfd
,
919 bfd_mach_o_section
*section
,
925 return bfd_mach_o_scan_read_section_64 (abfd
, section
, offset
, prot
);
927 return bfd_mach_o_scan_read_section_32 (abfd
, section
, offset
, prot
);
931 bfd_mach_o_scan_read_symtab_symbol (bfd
*abfd
,
932 bfd_mach_o_symtab_command
*sym
,
936 bfd_mach_o_data_struct
*mdata
= abfd
->tdata
.mach_o_data
;
937 unsigned int wide
= (mdata
->header
.version
== 2);
938 unsigned int symwidth
= wide
? 16 : 12;
939 bfd_vma symoff
= sym
->symoff
+ (i
* symwidth
);
940 unsigned char buf
[16];
941 unsigned char type
= -1;
942 unsigned char section
= -1;
945 unsigned long stroff
= -1;
946 unsigned int symtype
= -1;
948 BFD_ASSERT (sym
->strtab
!= NULL
);
950 bfd_seek (abfd
, symoff
, SEEK_SET
);
951 if (bfd_bread ((PTR
) buf
, symwidth
, abfd
) != symwidth
)
953 fprintf (stderr
, "bfd_mach_o_scan_read_symtab_symbol: unable to read %d bytes at %lu\n",
954 symwidth
, (unsigned long) symoff
);
958 stroff
= bfd_h_get_32 (abfd
, buf
);
959 type
= bfd_h_get_8 (abfd
, buf
+ 4);
960 symtype
= (type
& 0x0e);
961 section
= bfd_h_get_8 (abfd
, buf
+ 5);
962 desc
= bfd_h_get_16 (abfd
, buf
+ 6);
964 value
= bfd_h_get_64 (abfd
, buf
+ 8);
966 value
= bfd_h_get_32 (abfd
, buf
+ 8);
968 if (stroff
>= sym
->strsize
)
970 fprintf (stderr
, "bfd_mach_o_scan_read_symtab_symbol: symbol name out of range (%lu >= %lu)\n",
971 (unsigned long) stroff
, (unsigned long) sym
->strsize
);
976 s
->name
= sym
->strtab
+ stroff
;
978 s
->udata
.i
= (type
<< 24) | (section
<< 16) | desc
;
981 if (type
& BFD_MACH_O_N_STAB
)
983 s
->flags
|= BSF_DEBUGGING
;
984 s
->section
= bfd_und_section_ptr
;
996 if ((section
> 0) && (section
<= mdata
->nsects
))
998 s
->section
= mdata
->sections
[section
- 1]->bfdsection
;
999 s
->value
= s
->value
- mdata
->sections
[section
- 1]->addr
;
1006 if (type
& BFD_MACH_O_N_PEXT
)
1007 s
->flags
|= BSF_GLOBAL
;
1009 if (type
& BFD_MACH_O_N_EXT
)
1010 s
->flags
|= BSF_GLOBAL
;
1012 if (!(type
& (BFD_MACH_O_N_PEXT
| BFD_MACH_O_N_EXT
)))
1013 s
->flags
|= BSF_LOCAL
;
1017 case BFD_MACH_O_N_UNDF
:
1018 s
->section
= bfd_und_section_ptr
;
1020 case BFD_MACH_O_N_PBUD
:
1021 s
->section
= bfd_und_section_ptr
;
1023 case BFD_MACH_O_N_ABS
:
1024 s
->section
= bfd_abs_section_ptr
;
1026 case BFD_MACH_O_N_SECT
:
1027 if ((section
> 0) && (section
<= mdata
->nsects
))
1029 s
->section
= mdata
->sections
[section
- 1]->bfdsection
;
1030 s
->value
= s
->value
- mdata
->sections
[section
- 1]->addr
;
1034 /* Mach-O uses 0 to mean "no section"; not an error. */
1037 fprintf (stderr
, "bfd_mach_o_scan_read_symtab_symbol: "
1038 "symbol \"%s\" specified invalid section %d (max %lu): setting to undefined\n",
1039 s
->name
, section
, mdata
->nsects
);
1041 s
->section
= bfd_und_section_ptr
;
1044 case BFD_MACH_O_N_INDR
:
1045 fprintf (stderr
, "bfd_mach_o_scan_read_symtab_symbol: "
1046 "symbol \"%s\" is unsupported 'indirect' reference: setting to undefined\n",
1048 s
->section
= bfd_und_section_ptr
;
1051 fprintf (stderr
, "bfd_mach_o_scan_read_symtab_symbol: "
1052 "symbol \"%s\" specified invalid type field 0x%x: setting to undefined\n",
1054 s
->section
= bfd_und_section_ptr
;
1063 bfd_mach_o_scan_read_symtab_strtab (bfd
*abfd
,
1064 bfd_mach_o_symtab_command
*sym
)
1066 BFD_ASSERT (sym
->strtab
== NULL
);
1068 if (abfd
->flags
& BFD_IN_MEMORY
)
1070 struct bfd_in_memory
*b
;
1072 b
= (struct bfd_in_memory
*) abfd
->iostream
;
1074 if ((sym
->stroff
+ sym
->strsize
) > b
->size
)
1076 bfd_set_error (bfd_error_file_truncated
);
1079 sym
->strtab
= (char *) b
->buffer
+ sym
->stroff
;
1083 sym
->strtab
= bfd_alloc (abfd
, sym
->strsize
);
1084 if (sym
->strtab
== NULL
)
1087 bfd_seek (abfd
, sym
->stroff
, SEEK_SET
);
1088 if (bfd_bread ((PTR
) sym
->strtab
, sym
->strsize
, abfd
) != sym
->strsize
)
1090 fprintf (stderr
, "bfd_mach_o_scan_read_symtab_strtab: unable to read %lu bytes at %lu\n",
1091 sym
->strsize
, sym
->stroff
);
1099 bfd_mach_o_scan_read_symtab_symbols (bfd
*abfd
,
1100 bfd_mach_o_symtab_command
*sym
)
1105 BFD_ASSERT (sym
->symbols
== NULL
);
1106 sym
->symbols
= bfd_alloc (abfd
, sym
->nsyms
* sizeof (asymbol
));
1108 if (sym
->symbols
== NULL
)
1110 fprintf (stderr
, "bfd_mach_o_scan_read_symtab_symbols: unable to allocate memory for symbols\n");
1114 ret
= bfd_mach_o_scan_read_symtab_strtab (abfd
, sym
);
1118 for (i
= 0; i
< sym
->nsyms
; i
++)
1120 ret
= bfd_mach_o_scan_read_symtab_symbol (abfd
, sym
, &sym
->symbols
[i
], i
);
1129 bfd_mach_o_scan_read_dysymtab_symbol (bfd
*abfd
,
1130 bfd_mach_o_dysymtab_command
*dysym
,
1131 bfd_mach_o_symtab_command
*sym
,
1135 unsigned long isymoff
= dysym
->indirectsymoff
+ (i
* 4);
1136 unsigned long symindex
;
1137 unsigned char buf
[4];
1139 BFD_ASSERT (i
< dysym
->nindirectsyms
);
1141 bfd_seek (abfd
, isymoff
, SEEK_SET
);
1142 if (bfd_bread ((PTR
) buf
, 4, abfd
) != 4)
1144 fprintf (stderr
, "bfd_mach_o_scan_read_dysymtab_symbol: unable to read %lu bytes at %lu\n",
1145 (unsigned long) 4, isymoff
);
1148 symindex
= bfd_h_get_32 (abfd
, buf
);
1150 return bfd_mach_o_scan_read_symtab_symbol (abfd
, sym
, s
, symindex
);
1154 bfd_mach_o_i386_flavour_string (unsigned int flavour
)
1156 switch ((int) flavour
)
1158 case BFD_MACH_O_x86_THREAD_STATE32
: return "x86_THREAD_STATE32";
1159 case BFD_MACH_O_x86_FLOAT_STATE32
: return "x86_FLOAT_STATE32";
1160 case BFD_MACH_O_x86_EXCEPTION_STATE32
: return "x86_EXCEPTION_STATE32";
1161 case BFD_MACH_O_x86_THREAD_STATE64
: return "x86_THREAD_STATE64";
1162 case BFD_MACH_O_x86_FLOAT_STATE64
: return "x86_FLOAT_STATE64";
1163 case BFD_MACH_O_x86_EXCEPTION_STATE64
: return "x86_EXCEPTION_STATE64";
1164 case BFD_MACH_O_x86_THREAD_STATE
: return "x86_THREAD_STATE";
1165 case BFD_MACH_O_x86_FLOAT_STATE
: return "x86_FLOAT_STATE";
1166 case BFD_MACH_O_x86_EXCEPTION_STATE
: return "x86_EXCEPTION_STATE";
1167 case BFD_MACH_O_x86_DEBUG_STATE32
: return "x86_DEBUG_STATE32";
1168 case BFD_MACH_O_x86_DEBUG_STATE64
: return "x86_DEBUG_STATE64";
1169 case BFD_MACH_O_x86_DEBUG_STATE
: return "x86_DEBUG_STATE";
1170 case BFD_MACH_O_THREAD_STATE_NONE
: return "THREAD_STATE_NONE";
1171 default: return "UNKNOWN";
1176 bfd_mach_o_ppc_flavour_string (unsigned int flavour
)
1178 switch ((int) flavour
)
1180 case BFD_MACH_O_PPC_THREAD_STATE
: return "PPC_THREAD_STATE";
1181 case BFD_MACH_O_PPC_FLOAT_STATE
: return "PPC_FLOAT_STATE";
1182 case BFD_MACH_O_PPC_EXCEPTION_STATE
: return "PPC_EXCEPTION_STATE";
1183 case BFD_MACH_O_PPC_VECTOR_STATE
: return "PPC_VECTOR_STATE";
1184 default: return "UNKNOWN";
1189 bfd_mach_o_scan_read_dylinker (bfd
*abfd
,
1190 bfd_mach_o_load_command
*command
)
1192 bfd_mach_o_dylinker_command
*cmd
= &command
->command
.dylinker
;
1193 unsigned char buf
[4];
1194 unsigned int nameoff
;
1199 BFD_ASSERT ((command
->type
== BFD_MACH_O_LC_ID_DYLINKER
)
1200 || (command
->type
== BFD_MACH_O_LC_LOAD_DYLINKER
));
1202 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
1203 if (bfd_bread ((PTR
) buf
, 4, abfd
) != 4)
1206 nameoff
= bfd_h_get_32 (abfd
, buf
+ 0);
1208 cmd
->name_offset
= command
->offset
+ nameoff
;
1209 cmd
->name_len
= command
->len
- nameoff
;
1211 if (command
->type
== BFD_MACH_O_LC_LOAD_DYLINKER
)
1212 prefix
= "LC_LOAD_DYLINKER";
1213 else if (command
->type
== BFD_MACH_O_LC_ID_DYLINKER
)
1214 prefix
= "LC_ID_DYLINKER";
1218 sname
= bfd_alloc (abfd
, strlen (prefix
) + 1);
1221 strcpy (sname
, prefix
);
1223 bfdsec
= bfd_make_section_anyway_with_flags (abfd
, sname
, SEC_HAS_CONTENTS
);
1229 bfdsec
->size
= command
->len
- nameoff
;
1230 bfdsec
->filepos
= command
->offset
+ nameoff
;
1231 bfdsec
->alignment_power
= 0;
1233 cmd
->section
= bfdsec
;
1239 bfd_mach_o_scan_read_dylib (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1241 bfd_mach_o_dylib_command
*cmd
= &command
->command
.dylib
;
1242 unsigned char buf
[16];
1243 unsigned int nameoff
;
1248 BFD_ASSERT ((command
->type
== BFD_MACH_O_LC_ID_DYLIB
)
1249 || (command
->type
== BFD_MACH_O_LC_LOAD_DYLIB
)
1250 || (command
->type
== BFD_MACH_O_LC_LOAD_WEAK_DYLIB
));
1252 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
1253 if (bfd_bread ((PTR
) buf
, 16, abfd
) != 16)
1256 nameoff
= bfd_h_get_32 (abfd
, buf
+ 0);
1257 cmd
->timestamp
= bfd_h_get_32 (abfd
, buf
+ 4);
1258 cmd
->current_version
= bfd_h_get_32 (abfd
, buf
+ 8);
1259 cmd
->compatibility_version
= bfd_h_get_32 (abfd
, buf
+ 12);
1261 cmd
->name_offset
= command
->offset
+ nameoff
;
1262 cmd
->name_len
= command
->len
- nameoff
;
1264 if (command
->type
== BFD_MACH_O_LC_LOAD_DYLIB
)
1265 prefix
= "LC_LOAD_DYLIB";
1266 else if (command
->type
== BFD_MACH_O_LC_LOAD_WEAK_DYLIB
)
1267 prefix
= "LC_LOAD_WEAK_DYLIB";
1268 else if (command
->type
== BFD_MACH_O_LC_ID_DYLIB
)
1269 prefix
= "LC_ID_DYLIB";
1273 sname
= bfd_alloc (abfd
, strlen (prefix
) + 1);
1276 strcpy (sname
, prefix
);
1278 bfdsec
= bfd_make_section_anyway_with_flags (abfd
, sname
, SEC_HAS_CONTENTS
);
1284 bfdsec
->size
= command
->len
- 8;
1285 bfdsec
->filepos
= command
->offset
+ 8;
1286 bfdsec
->alignment_power
= 0;
1288 cmd
->section
= bfdsec
;
1294 bfd_mach_o_scan_read_prebound_dylib (bfd
*abfd ATTRIBUTE_UNUSED
,
1295 bfd_mach_o_load_command
*command ATTRIBUTE_UNUSED
)
1297 /* bfd_mach_o_prebound_dylib_command *cmd = &command->command.prebound_dylib; */
1299 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_PREBOUND_DYLIB
);
1304 bfd_mach_o_scan_read_thread (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1306 bfd_mach_o_data_struct
*mdata
= NULL
;
1307 bfd_mach_o_thread_command
*cmd
= &command
->command
.thread
;
1308 unsigned char buf
[8];
1310 unsigned int nflavours
;
1313 BFD_ASSERT ((command
->type
== BFD_MACH_O_LC_THREAD
)
1314 || (command
->type
== BFD_MACH_O_LC_UNIXTHREAD
));
1316 BFD_ASSERT (bfd_mach_o_valid (abfd
));
1317 mdata
= abfd
->tdata
.mach_o_data
;
1321 while (offset
!= command
->len
)
1323 if (offset
>= command
->len
)
1326 bfd_seek (abfd
, command
->offset
+ offset
, SEEK_SET
);
1328 if (bfd_bread ((PTR
) buf
, 8, abfd
) != 8)
1331 offset
+= 8 + bfd_h_get_32 (abfd
, buf
+ 4) * 4;
1335 cmd
->flavours
= bfd_alloc (abfd
, nflavours
* sizeof (bfd_mach_o_thread_flavour
));
1336 if (cmd
->flavours
== NULL
)
1338 cmd
->nflavours
= nflavours
;
1342 while (offset
!= command
->len
)
1344 if (offset
>= command
->len
)
1347 if (nflavours
>= cmd
->nflavours
)
1350 bfd_seek (abfd
, command
->offset
+ offset
, SEEK_SET
);
1352 if (bfd_bread ((PTR
) buf
, 8, abfd
) != 8)
1355 cmd
->flavours
[nflavours
].flavour
= bfd_h_get_32 (abfd
, buf
);
1356 cmd
->flavours
[nflavours
].offset
= command
->offset
+ offset
+ 8;
1357 cmd
->flavours
[nflavours
].size
= bfd_h_get_32 (abfd
, buf
+ 4) * 4;
1358 offset
+= cmd
->flavours
[nflavours
].size
+ 8;
1362 for (i
= 0; i
< nflavours
; i
++)
1365 unsigned int snamelen
;
1367 const char *flavourstr
;
1368 const char *prefix
= "LC_THREAD";
1371 switch (mdata
->header
.cputype
)
1373 case BFD_MACH_O_CPU_TYPE_POWERPC
:
1374 case BFD_MACH_O_CPU_TYPE_POWERPC_64
:
1375 flavourstr
= bfd_mach_o_ppc_flavour_string (cmd
->flavours
[i
].flavour
);
1377 case BFD_MACH_O_CPU_TYPE_I386
:
1378 case BFD_MACH_O_CPU_TYPE_X86_64
:
1379 flavourstr
= bfd_mach_o_i386_flavour_string (cmd
->flavours
[i
].flavour
);
1382 flavourstr
= "UNKNOWN_ARCHITECTURE";
1386 snamelen
= strlen (prefix
) + 1 + 20 + 1 + strlen (flavourstr
) + 1;
1387 sname
= bfd_alloc (abfd
, snamelen
);
1393 sprintf (sname
, "%s.%s.%u", prefix
, flavourstr
, j
);
1394 if (bfd_get_section_by_name (abfd
, sname
) == NULL
)
1399 bfdsec
= bfd_make_section_with_flags (abfd
, sname
, SEC_HAS_CONTENTS
);
1403 bfdsec
->size
= cmd
->flavours
[i
].size
;
1404 bfdsec
->filepos
= cmd
->flavours
[i
].offset
;
1405 bfdsec
->alignment_power
= 0x0;
1407 cmd
->section
= bfdsec
;
1414 bfd_mach_o_scan_read_dysymtab (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1416 bfd_mach_o_dysymtab_command
*seg
= &command
->command
.dysymtab
;
1417 unsigned char buf
[72];
1419 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_DYSYMTAB
);
1421 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
1422 if (bfd_bread ((PTR
) buf
, 72, abfd
) != 72)
1425 seg
->ilocalsym
= bfd_h_get_32 (abfd
, buf
+ 0);
1426 seg
->nlocalsym
= bfd_h_get_32 (abfd
, buf
+ 4);
1427 seg
->iextdefsym
= bfd_h_get_32 (abfd
, buf
+ 8);
1428 seg
->nextdefsym
= bfd_h_get_32 (abfd
, buf
+ 12);
1429 seg
->iundefsym
= bfd_h_get_32 (abfd
, buf
+ 16);
1430 seg
->nundefsym
= bfd_h_get_32 (abfd
, buf
+ 20);
1431 seg
->tocoff
= bfd_h_get_32 (abfd
, buf
+ 24);
1432 seg
->ntoc
= bfd_h_get_32 (abfd
, buf
+ 28);
1433 seg
->modtaboff
= bfd_h_get_32 (abfd
, buf
+ 32);
1434 seg
->nmodtab
= bfd_h_get_32 (abfd
, buf
+ 36);
1435 seg
->extrefsymoff
= bfd_h_get_32 (abfd
, buf
+ 40);
1436 seg
->nextrefsyms
= bfd_h_get_32 (abfd
, buf
+ 44);
1437 seg
->indirectsymoff
= bfd_h_get_32 (abfd
, buf
+ 48);
1438 seg
->nindirectsyms
= bfd_h_get_32 (abfd
, buf
+ 52);
1439 seg
->extreloff
= bfd_h_get_32 (abfd
, buf
+ 56);
1440 seg
->nextrel
= bfd_h_get_32 (abfd
, buf
+ 60);
1441 seg
->locreloff
= bfd_h_get_32 (abfd
, buf
+ 64);
1442 seg
->nlocrel
= bfd_h_get_32 (abfd
, buf
+ 68);
1448 bfd_mach_o_scan_read_symtab (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1450 bfd_mach_o_symtab_command
*seg
= &command
->command
.symtab
;
1451 unsigned char buf
[16];
1454 const char *prefix
= "LC_SYMTAB.stabs";
1455 int nlist_size
= (bfd_mach_o_version (abfd
) > 1) ? 16 : 12;
1457 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SYMTAB
);
1459 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
1460 if (bfd_bread ((PTR
) buf
, 16, abfd
) != 16)
1463 seg
->symoff
= bfd_h_get_32 (abfd
, buf
);
1464 seg
->nsyms
= bfd_h_get_32 (abfd
, buf
+ 4);
1465 seg
->stroff
= bfd_h_get_32 (abfd
, buf
+ 8);
1466 seg
->strsize
= bfd_h_get_32 (abfd
, buf
+ 12);
1467 seg
->symbols
= NULL
;
1470 sname
= bfd_alloc (abfd
, strlen (prefix
) + 1);
1473 strcpy (sname
, prefix
);
1475 bfdsec
= bfd_make_section_anyway_with_flags (abfd
, sname
, SEC_HAS_CONTENTS
);
1481 bfdsec
->size
= seg
->nsyms
* nlist_size
;
1482 bfdsec
->filepos
= seg
->symoff
;
1483 bfdsec
->alignment_power
= 0;
1485 seg
->stabs_segment
= bfdsec
;
1487 if (seg
->nsyms
!= 0)
1488 abfd
->flags
|= HAS_SYMS
;
1490 prefix
= "LC_SYMTAB.stabstr";
1491 sname
= bfd_alloc (abfd
, strlen (prefix
) + 1);
1494 strcpy (sname
, prefix
);
1496 bfdsec
= bfd_make_section_anyway_with_flags (abfd
, sname
, SEC_HAS_CONTENTS
);
1502 bfdsec
->size
= seg
->strsize
;
1503 bfdsec
->filepos
= seg
->stroff
;
1504 bfdsec
->alignment_power
= 0;
1506 seg
->stabstr_segment
= bfdsec
;
1512 bfd_mach_o_scan_read_uuid (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1514 bfd_mach_o_uuid_command
*cmd
= &command
->command
.uuid
;
1517 static const char prefix
[] = "LC_UUID";
1519 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_UUID
);
1521 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
1522 if (bfd_bread ((PTR
) cmd
->uuid
, 16, abfd
) != 16)
1525 sname
= bfd_alloc (abfd
, strlen (prefix
) + 1);
1528 strcpy (sname
, prefix
);
1530 bfdsec
= bfd_make_section_anyway_with_flags (abfd
, sname
, SEC_HAS_CONTENTS
);
1536 bfdsec
->size
= command
->len
- 8;
1537 bfdsec
->filepos
= command
->offset
+ 8;
1538 bfdsec
->alignment_power
= 0;
1540 cmd
->section
= bfdsec
;
1546 bfd_mach_o_scan_read_segment (bfd
*abfd
,
1547 bfd_mach_o_load_command
*command
,
1550 unsigned char buf
[64];
1551 bfd_mach_o_segment_command
*seg
= &command
->command
.segment
;
1555 const char *prefix
= "LC_SEGMENT";
1556 unsigned int snamelen
;
1560 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SEGMENT_64
);
1562 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
1563 if (bfd_bread ((PTR
) buf
, 64, abfd
) != 64)
1566 memcpy (seg
->segname
, buf
, 16);
1567 seg
->segname
[16] = '\0';
1569 seg
->vmaddr
= bfd_h_get_64 (abfd
, buf
+ 16);
1570 seg
->vmsize
= bfd_h_get_64 (abfd
, buf
+ 24);
1571 seg
->fileoff
= bfd_h_get_64 (abfd
, buf
+ 32);
1572 seg
->filesize
= bfd_h_get_64 (abfd
, buf
+ 40);
1573 seg
->maxprot
= bfd_h_get_32 (abfd
, buf
+ 48);
1574 seg
->initprot
= bfd_h_get_32 (abfd
, buf
+ 52);
1575 seg
->nsects
= bfd_h_get_32 (abfd
, buf
+ 56);
1576 seg
->flags
= bfd_h_get_32 (abfd
, buf
+ 60);
1580 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SEGMENT
);
1582 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
1583 if (bfd_bread ((PTR
) buf
, 48, abfd
) != 48)
1586 memcpy (seg
->segname
, buf
, 16);
1587 seg
->segname
[16] = '\0';
1589 seg
->vmaddr
= bfd_h_get_32 (abfd
, buf
+ 16);
1590 seg
->vmsize
= bfd_h_get_32 (abfd
, buf
+ 20);
1591 seg
->fileoff
= bfd_h_get_32 (abfd
, buf
+ 24);
1592 seg
->filesize
= bfd_h_get_32 (abfd
, buf
+ 28);
1593 seg
->maxprot
= bfd_h_get_32 (abfd
, buf
+ 32);
1594 seg
->initprot
= bfd_h_get_32 (abfd
, buf
+ 36);
1595 seg
->nsects
= bfd_h_get_32 (abfd
, buf
+ 40);
1596 seg
->flags
= bfd_h_get_32 (abfd
, buf
+ 44);
1599 snamelen
= strlen (prefix
) + 1 + strlen (seg
->segname
) + 1;
1600 sname
= bfd_alloc (abfd
, snamelen
);
1603 if (strcmp (seg
->segname
, "__TEXT") == 0
1604 || strcmp (seg
->segname
, "__DATA") == 0
1605 || strcmp (seg
->segname
, "__IMPORT") == 0
1606 || strcmp (seg
->segname
, "__LINKEDIT") == 0)
1607 strcpy (sname
, seg
->segname
);
1609 sprintf (sname
, "%s.%s", prefix
, seg
->segname
);
1611 bfdsec
= bfd_make_section_anyway (abfd
, sname
);
1615 bfdsec
->vma
= seg
->vmaddr
;
1616 bfdsec
->lma
= seg
->vmaddr
;
1617 bfdsec
->size
= seg
->filesize
;
1618 bfdsec
->filepos
= seg
->fileoff
;
1619 bfdsec
->alignment_power
= 0x0;
1620 bfdsec
->flags
= SEC_HAS_CONTENTS
;
1621 bfdsec
->segment_mark
= 1;
1623 seg
->segment
= bfdsec
;
1625 if (seg
->nsects
!= 0)
1627 seg
->sections
= bfd_alloc (abfd
, seg
->nsects
* sizeof (bfd_mach_o_section
));
1628 if (seg
->sections
== NULL
)
1631 for (i
= 0; i
< seg
->nsects
; i
++)
1635 segoff
= command
->offset
+ 64 + 8 + (i
* 80);
1637 segoff
= command
->offset
+ 48 + 8 + (i
* 68);
1639 if (bfd_mach_o_scan_read_section
1640 (abfd
, &seg
->sections
[i
], segoff
, seg
->initprot
, wide
) != 0)
1649 bfd_mach_o_scan_read_segment_32 (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1651 return bfd_mach_o_scan_read_segment (abfd
, command
, 0);
1655 bfd_mach_o_scan_read_segment_64 (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1657 return bfd_mach_o_scan_read_segment (abfd
, command
, 1);
1661 bfd_mach_o_scan_read_command (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1663 unsigned char buf
[8];
1665 bfd_seek (abfd
, command
->offset
, SEEK_SET
);
1666 if (bfd_bread ((PTR
) buf
, 8, abfd
) != 8)
1669 command
->type
= (bfd_h_get_32 (abfd
, buf
) & ~BFD_MACH_O_LC_REQ_DYLD
);
1670 command
->type_required
= (bfd_h_get_32 (abfd
, buf
) & BFD_MACH_O_LC_REQ_DYLD
1672 command
->len
= bfd_h_get_32 (abfd
, buf
+ 4);
1674 switch (command
->type
)
1676 case BFD_MACH_O_LC_SEGMENT
:
1677 if (bfd_mach_o_scan_read_segment_32 (abfd
, command
) != 0)
1680 case BFD_MACH_O_LC_SEGMENT_64
:
1681 if (bfd_mach_o_scan_read_segment_64 (abfd
, command
) != 0)
1684 case BFD_MACH_O_LC_SYMTAB
:
1685 if (bfd_mach_o_scan_read_symtab (abfd
, command
) != 0)
1688 case BFD_MACH_O_LC_SYMSEG
:
1690 case BFD_MACH_O_LC_THREAD
:
1691 case BFD_MACH_O_LC_UNIXTHREAD
:
1692 if (bfd_mach_o_scan_read_thread (abfd
, command
) != 0)
1695 case BFD_MACH_O_LC_LOAD_DYLINKER
:
1696 case BFD_MACH_O_LC_ID_DYLINKER
:
1697 if (bfd_mach_o_scan_read_dylinker (abfd
, command
) != 0)
1700 case BFD_MACH_O_LC_LOAD_DYLIB
:
1701 case BFD_MACH_O_LC_ID_DYLIB
:
1702 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB
:
1703 if (bfd_mach_o_scan_read_dylib (abfd
, command
) != 0)
1706 case BFD_MACH_O_LC_PREBOUND_DYLIB
:
1707 if (bfd_mach_o_scan_read_prebound_dylib (abfd
, command
) != 0)
1710 case BFD_MACH_O_LC_LOADFVMLIB
:
1711 case BFD_MACH_O_LC_IDFVMLIB
:
1712 case BFD_MACH_O_LC_IDENT
:
1713 case BFD_MACH_O_LC_FVMFILE
:
1714 case BFD_MACH_O_LC_PREPAGE
:
1715 case BFD_MACH_O_LC_ROUTINES
:
1716 case BFD_MACH_O_LC_SUB_FRAMEWORK
:
1718 case BFD_MACH_O_LC_DYSYMTAB
:
1719 if (bfd_mach_o_scan_read_dysymtab (abfd
, command
) != 0)
1722 case BFD_MACH_O_LC_SUB_UMBRELLA
:
1723 case BFD_MACH_O_LC_SUB_CLIENT
:
1724 case BFD_MACH_O_LC_SUB_LIBRARY
:
1725 case BFD_MACH_O_LC_TWOLEVEL_HINTS
:
1726 case BFD_MACH_O_LC_PREBIND_CKSUM
:
1728 case BFD_MACH_O_LC_UUID
:
1729 if (bfd_mach_o_scan_read_uuid (abfd
, command
) != 0)
1732 case BFD_MACH_O_LC_CODE_SIGNATURE
:
1733 case BFD_MACH_O_LC_SEGMENT_SPLIT_INFO
:
1734 case BFD_MACH_O_LC_REEXPORT_DYLIB
:
1737 fprintf (stderr
, "unable to read unknown load command 0x%lx\n",
1738 (unsigned long) command
->type
);
1746 bfd_mach_o_flatten_sections (bfd
*abfd
)
1748 bfd_mach_o_data_struct
*mdata
= abfd
->tdata
.mach_o_data
;
1752 /* Count total number of sections. */
1755 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
1757 if (mdata
->commands
[i
].type
== BFD_MACH_O_LC_SEGMENT
1758 || mdata
->commands
[i
].type
== BFD_MACH_O_LC_SEGMENT_64
)
1760 bfd_mach_o_segment_command
*seg
;
1762 seg
= &mdata
->commands
[i
].command
.segment
;
1763 mdata
->nsects
+= seg
->nsects
;
1767 /* Allocate sections array. */
1768 mdata
->sections
= bfd_alloc (abfd
,
1769 mdata
->nsects
* sizeof (bfd_mach_o_section
*));
1771 /* Fill the array. */
1774 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
1776 if (mdata
->commands
[i
].type
== BFD_MACH_O_LC_SEGMENT
1777 || mdata
->commands
[i
].type
== BFD_MACH_O_LC_SEGMENT_64
)
1779 bfd_mach_o_segment_command
*seg
;
1781 seg
= &mdata
->commands
[i
].command
.segment
;
1782 BFD_ASSERT (csect
+ seg
->nsects
<= mdata
->nsects
);
1784 for (j
= 0; j
< seg
->nsects
; j
++)
1785 mdata
->sections
[csect
++] = &seg
->sections
[j
];
1791 bfd_mach_o_scan_start_address (bfd
*abfd
)
1793 bfd_mach_o_data_struct
*mdata
= abfd
->tdata
.mach_o_data
;
1794 bfd_mach_o_thread_command
*cmd
= NULL
;
1797 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
1799 if ((mdata
->commands
[i
].type
== BFD_MACH_O_LC_THREAD
) ||
1800 (mdata
->commands
[i
].type
== BFD_MACH_O_LC_UNIXTHREAD
))
1803 cmd
= &mdata
->commands
[i
].command
.thread
;
1812 for (i
= 0; i
< cmd
->nflavours
; i
++)
1814 if ((mdata
->header
.cputype
== BFD_MACH_O_CPU_TYPE_I386
)
1815 && (cmd
->flavours
[i
].flavour
1816 == (unsigned long) BFD_MACH_O_x86_THREAD_STATE32
))
1818 unsigned char buf
[4];
1820 bfd_seek (abfd
, cmd
->flavours
[i
].offset
+ 40, SEEK_SET
);
1822 if (bfd_bread (buf
, 4, abfd
) != 4)
1825 abfd
->start_address
= bfd_h_get_32 (abfd
, buf
);
1827 else if ((mdata
->header
.cputype
== BFD_MACH_O_CPU_TYPE_POWERPC
)
1828 && (cmd
->flavours
[i
].flavour
== BFD_MACH_O_PPC_THREAD_STATE
))
1830 unsigned char buf
[4];
1832 bfd_seek (abfd
, cmd
->flavours
[i
].offset
+ 0, SEEK_SET
);
1834 if (bfd_bread (buf
, 4, abfd
) != 4)
1837 abfd
->start_address
= bfd_h_get_32 (abfd
, buf
);
1839 else if ((mdata
->header
.cputype
== BFD_MACH_O_CPU_TYPE_POWERPC_64
)
1840 && (cmd
->flavours
[i
].flavour
== BFD_MACH_O_PPC_THREAD_STATE_64
))
1842 unsigned char buf
[8];
1844 bfd_seek (abfd
, cmd
->flavours
[i
].offset
+ 0, SEEK_SET
);
1846 if (bfd_bread (buf
, 8, abfd
) != 8)
1849 abfd
->start_address
= bfd_h_get_64 (abfd
, buf
);
1851 else if ((mdata
->header
.cputype
== BFD_MACH_O_CPU_TYPE_X86_64
)
1852 && (cmd
->flavours
[i
].flavour
== BFD_MACH_O_x86_THREAD_STATE64
))
1854 unsigned char buf
[8];
1856 bfd_seek (abfd
, cmd
->flavours
[i
].offset
+ (16 * 8), SEEK_SET
);
1858 if (bfd_bread (buf
, 8, abfd
) != 8)
1861 abfd
->start_address
= bfd_h_get_64 (abfd
, buf
);
1869 bfd_mach_o_scan (bfd
*abfd
,
1870 bfd_mach_o_header
*header
,
1871 bfd_mach_o_data_struct
*mdata
)
1874 enum bfd_architecture cputype
;
1875 unsigned long cpusubtype
;
1876 unsigned int hdrsize
;
1878 hdrsize
= (header
->version
== 2) ? 32 : 28;
1880 mdata
->header
= *header
;
1881 mdata
->symbols
= NULL
;
1883 abfd
->flags
= abfd
->flags
& (BFD_IN_MEMORY
| BFD_IO_FUNCS
);
1884 switch (header
->filetype
)
1886 case BFD_MACH_O_MH_OBJECT
:
1887 abfd
->flags
|= HAS_RELOC
;
1889 case BFD_MACH_O_MH_EXECUTE
:
1890 abfd
->flags
|= EXEC_P
;
1892 case BFD_MACH_O_MH_DYLIB
:
1893 case BFD_MACH_O_MH_BUNDLE
:
1894 abfd
->flags
|= DYNAMIC
;
1898 abfd
->tdata
.mach_o_data
= mdata
;
1900 bfd_mach_o_convert_architecture (header
->cputype
, header
->cpusubtype
,
1901 &cputype
, &cpusubtype
);
1902 if (cputype
== bfd_arch_unknown
)
1904 fprintf (stderr
, "bfd_mach_o_scan: unknown architecture 0x%lx/0x%lx\n",
1905 header
->cputype
, header
->cpusubtype
);
1909 bfd_set_arch_mach (abfd
, cputype
, cpusubtype
);
1911 if (header
->ncmds
!= 0)
1913 mdata
->commands
= bfd_alloc (abfd
, header
->ncmds
* sizeof (bfd_mach_o_load_command
));
1914 if (mdata
->commands
== NULL
)
1917 for (i
= 0; i
< header
->ncmds
; i
++)
1919 bfd_mach_o_load_command
*cur
= &mdata
->commands
[i
];
1922 cur
->offset
= hdrsize
;
1925 bfd_mach_o_load_command
*prev
= &mdata
->commands
[i
- 1];
1926 cur
->offset
= prev
->offset
+ prev
->len
;
1929 if (bfd_mach_o_scan_read_command (abfd
, cur
) < 0)
1934 if (bfd_mach_o_scan_start_address (abfd
) < 0)
1937 bfd_mach_o_flatten_sections (abfd
);
1942 bfd_mach_o_mkobject (bfd
*abfd
)
1944 bfd_mach_o_data_struct
*mdata
= NULL
;
1946 mdata
= bfd_alloc (abfd
, sizeof (bfd_mach_o_data_struct
));
1949 abfd
->tdata
.mach_o_data
= mdata
;
1951 mdata
->header
.magic
= 0;
1952 mdata
->header
.cputype
= 0;
1953 mdata
->header
.cpusubtype
= 0;
1954 mdata
->header
.filetype
= 0;
1955 mdata
->header
.ncmds
= 0;
1956 mdata
->header
.sizeofcmds
= 0;
1957 mdata
->header
.flags
= 0;
1958 mdata
->header
.byteorder
= BFD_ENDIAN_UNKNOWN
;
1959 mdata
->commands
= NULL
;
1960 mdata
->nsymbols
= 0;
1961 mdata
->symbols
= NULL
;
1963 mdata
->sections
= NULL
;
1970 bfd_mach_o_object_p (bfd
*abfd
)
1972 struct bfd_preserve preserve
;
1973 bfd_mach_o_header header
;
1975 preserve
.marker
= NULL
;
1976 if (bfd_mach_o_read_header (abfd
, &header
) != 0)
1979 if (! (header
.byteorder
== BFD_ENDIAN_BIG
1980 || header
.byteorder
== BFD_ENDIAN_LITTLE
))
1982 fprintf (stderr
, "unknown header byte-order value 0x%lx\n",
1983 (unsigned long) header
.byteorder
);
1987 if (! ((header
.byteorder
== BFD_ENDIAN_BIG
1988 && abfd
->xvec
->byteorder
== BFD_ENDIAN_BIG
1989 && abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
1990 || (header
.byteorder
== BFD_ENDIAN_LITTLE
1991 && abfd
->xvec
->byteorder
== BFD_ENDIAN_LITTLE
1992 && abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)))
1995 preserve
.marker
= bfd_zalloc (abfd
, sizeof (bfd_mach_o_data_struct
));
1996 if (preserve
.marker
== NULL
1997 || !bfd_preserve_save (abfd
, &preserve
))
2000 if (bfd_mach_o_scan (abfd
, &header
,
2001 (bfd_mach_o_data_struct
*) preserve
.marker
) != 0)
2004 bfd_preserve_finish (abfd
, &preserve
);
2008 bfd_set_error (bfd_error_wrong_format
);
2011 if (preserve
.marker
!= NULL
)
2012 bfd_preserve_restore (abfd
, &preserve
);
2017 bfd_mach_o_core_p (bfd
*abfd
)
2019 struct bfd_preserve preserve
;
2020 bfd_mach_o_header header
;
2022 preserve
.marker
= NULL
;
2023 if (bfd_mach_o_read_header (abfd
, &header
) != 0)
2026 if (! (header
.byteorder
== BFD_ENDIAN_BIG
2027 || header
.byteorder
== BFD_ENDIAN_LITTLE
))
2029 fprintf (stderr
, "unknown header byte-order value 0x%lx\n",
2030 (unsigned long) header
.byteorder
);
2034 if (! ((header
.byteorder
== BFD_ENDIAN_BIG
2035 && abfd
->xvec
->byteorder
== BFD_ENDIAN_BIG
2036 && abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
2037 || (header
.byteorder
== BFD_ENDIAN_LITTLE
2038 && abfd
->xvec
->byteorder
== BFD_ENDIAN_LITTLE
2039 && abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)))
2042 if (header
.filetype
!= BFD_MACH_O_MH_CORE
)
2045 preserve
.marker
= bfd_zalloc (abfd
, sizeof (bfd_mach_o_data_struct
));
2046 if (preserve
.marker
== NULL
2047 || !bfd_preserve_save (abfd
, &preserve
))
2050 if (bfd_mach_o_scan (abfd
, &header
,
2051 (bfd_mach_o_data_struct
*) preserve
.marker
) != 0)
2054 bfd_preserve_finish (abfd
, &preserve
);
2058 bfd_set_error (bfd_error_wrong_format
);
2061 if (preserve
.marker
!= NULL
)
2062 bfd_preserve_restore (abfd
, &preserve
);
2066 typedef struct mach_o_fat_archentry
2068 unsigned long cputype
;
2069 unsigned long cpusubtype
;
2070 unsigned long offset
;
2072 unsigned long align
;
2073 } mach_o_fat_archentry
;
2075 typedef struct mach_o_fat_data_struct
2077 unsigned long magic
;
2078 unsigned long nfat_arch
;
2079 mach_o_fat_archentry
*archentries
;
2080 } mach_o_fat_data_struct
;
2083 bfd_mach_o_archive_p (bfd
*abfd
)
2085 mach_o_fat_data_struct
*adata
= NULL
;
2086 unsigned char buf
[20];
2089 bfd_seek (abfd
, 0, SEEK_SET
);
2090 if (bfd_bread ((PTR
) buf
, 8, abfd
) != 8)
2093 adata
= bfd_alloc (abfd
, sizeof (mach_o_fat_data_struct
));
2097 adata
->magic
= bfd_getb32 (buf
);
2098 adata
->nfat_arch
= bfd_getb32 (buf
+ 4);
2099 if (adata
->magic
!= 0xcafebabe)
2101 /* Avoid matching Java bytecode files, which have the same magic number.
2102 In the Java bytecode file format this field contains the JVM version,
2103 which starts at 43.0. */
2104 if (adata
->nfat_arch
> 30)
2107 adata
->archentries
=
2108 bfd_alloc (abfd
, adata
->nfat_arch
* sizeof (mach_o_fat_archentry
));
2109 if (adata
->archentries
== NULL
)
2112 for (i
= 0; i
< adata
->nfat_arch
; i
++)
2114 bfd_seek (abfd
, 8 + 20 * i
, SEEK_SET
);
2116 if (bfd_bread ((PTR
) buf
, 20, abfd
) != 20)
2118 adata
->archentries
[i
].cputype
= bfd_getb32 (buf
);
2119 adata
->archentries
[i
].cpusubtype
= bfd_getb32 (buf
+ 4);
2120 adata
->archentries
[i
].offset
= bfd_getb32 (buf
+ 8);
2121 adata
->archentries
[i
].size
= bfd_getb32 (buf
+ 12);
2122 adata
->archentries
[i
].align
= bfd_getb32 (buf
+ 16);
2125 abfd
->tdata
.mach_o_fat_data
= adata
;
2130 bfd_release (abfd
, adata
);
2131 bfd_set_error (bfd_error_wrong_format
);
2136 bfd_mach_o_openr_next_archived_file (bfd
*archive
, bfd
*prev
)
2138 mach_o_fat_data_struct
*adata
;
2139 mach_o_fat_archentry
*entry
= NULL
;
2142 enum bfd_architecture arch_type
;
2143 unsigned long arch_subtype
;
2145 adata
= (mach_o_fat_data_struct
*) archive
->tdata
.mach_o_fat_data
;
2146 BFD_ASSERT (adata
!= NULL
);
2148 /* Find index of previous entry. */
2150 i
= 0; /* Start at first one. */
2153 for (i
= 0; i
< adata
->nfat_arch
; i
++)
2155 if (adata
->archentries
[i
].offset
== prev
->origin
)
2159 if (i
== adata
->nfat_arch
)
2162 bfd_set_error (bfd_error_bad_value
);
2165 i
++; /* Get next entry. */
2168 if (i
>= adata
->nfat_arch
)
2170 bfd_set_error (bfd_error_no_more_archived_files
);
2174 entry
= &adata
->archentries
[i
];
2175 nbfd
= _bfd_new_bfd_contained_in (archive
);
2179 nbfd
->origin
= entry
->offset
;
2181 bfd_mach_o_convert_architecture (entry
->cputype
, entry
->cpusubtype
,
2182 &arch_type
, &arch_subtype
);
2183 /* Create the member filename.
2184 Use FILENAME:ARCH_NAME. */
2187 const char *arch_name
;
2188 size_t arch_file_len
= strlen (bfd_get_filename (archive
));
2190 arch_name
= bfd_printable_arch_mach (arch_type
, arch_subtype
);
2191 s
= bfd_malloc (arch_file_len
+ 1 + strlen (arch_name
) + 1);
2194 memcpy (s
, bfd_get_filename (archive
), arch_file_len
);
2195 s
[arch_file_len
] = ':';
2196 strcpy (s
+ arch_file_len
+ 1, arch_name
);
2199 nbfd
->iostream
= NULL
;
2200 bfd_set_arch_mach (nbfd
, arch_type
, arch_subtype
);
2205 /* If ABFD format is FORMAT and architecture is ARCH, return it.
2206 If ABFD is a fat image containing a member that corresponds to FORMAT
2207 and ARCH, returns it.
2208 In other case, returns NULL.
2209 This function allows transparent uses of fat images. */
2211 bfd_mach_o_fat_extract (bfd
*abfd
,
2213 const bfd_arch_info_type
*arch
)
2216 mach_o_fat_data_struct
*adata
;
2219 if (bfd_check_format (abfd
, format
))
2221 if (bfd_get_arch_info (abfd
) == arch
)
2225 if (!bfd_check_format (abfd
, bfd_archive
)
2226 || abfd
->xvec
!= &mach_o_fat_vec
)
2229 /* This is a Mach-O fat image. */
2230 adata
= (mach_o_fat_data_struct
*) abfd
->tdata
.mach_o_fat_data
;
2231 BFD_ASSERT (adata
!= NULL
);
2233 for (i
= 0; i
< adata
->nfat_arch
; i
++)
2235 struct mach_o_fat_archentry
*e
= &adata
->archentries
[i
];
2236 enum bfd_architecture cpu_type
;
2237 unsigned long cpu_subtype
;
2239 bfd_mach_o_convert_architecture (e
->cputype
, e
->cpusubtype
,
2240 &cpu_type
, &cpu_subtype
);
2241 if (cpu_type
!= arch
->arch
|| cpu_subtype
!= arch
->mach
)
2244 /* The architecture is found. */
2245 res
= _bfd_new_bfd_contained_in (abfd
);
2249 res
->origin
= e
->offset
;
2251 res
->filename
= strdup (abfd
->filename
);
2252 res
->iostream
= NULL
;
2254 if (bfd_check_format (res
, format
))
2256 BFD_ASSERT (bfd_get_arch_info (res
) == arch
);
2267 bfd_mach_o_lookup_section (bfd
*abfd
,
2269 bfd_mach_o_load_command
**mcommand
,
2270 bfd_mach_o_section
**msection
)
2272 struct mach_o_data_struct
*md
= abfd
->tdata
.mach_o_data
;
2273 unsigned int i
, j
, num
;
2275 bfd_mach_o_load_command
*ncmd
= NULL
;
2276 bfd_mach_o_section
*nsect
= NULL
;
2278 BFD_ASSERT (mcommand
!= NULL
);
2279 BFD_ASSERT (msection
!= NULL
);
2282 for (i
= 0; i
< md
->header
.ncmds
; i
++)
2284 struct bfd_mach_o_load_command
*cmd
= &md
->commands
[i
];
2285 struct bfd_mach_o_segment_command
*seg
= NULL
;
2287 if (cmd
->type
!= BFD_MACH_O_LC_SEGMENT
2288 || cmd
->type
!= BFD_MACH_O_LC_SEGMENT_64
)
2290 seg
= &cmd
->command
.segment
;
2292 if (seg
->segment
== section
)
2299 for (j
= 0; j
< seg
->nsects
; j
++)
2301 struct bfd_mach_o_section
*sect
= &seg
->sections
[j
];
2303 if (sect
->bfdsection
== section
)
2318 bfd_mach_o_lookup_command (bfd
*abfd
,
2319 bfd_mach_o_load_command_type type
,
2320 bfd_mach_o_load_command
**mcommand
)
2322 struct mach_o_data_struct
*md
= NULL
;
2323 bfd_mach_o_load_command
*ncmd
= NULL
;
2324 unsigned int i
, num
;
2326 md
= abfd
->tdata
.mach_o_data
;
2328 BFD_ASSERT (md
!= NULL
);
2329 BFD_ASSERT (mcommand
!= NULL
);
2332 for (i
= 0; i
< md
->header
.ncmds
; i
++)
2334 struct bfd_mach_o_load_command
*cmd
= &md
->commands
[i
];
2336 if (cmd
->type
!= type
)
2349 bfd_mach_o_stack_addr (enum bfd_mach_o_cpu_type type
)
2353 case BFD_MACH_O_CPU_TYPE_MC680x0
:
2355 case BFD_MACH_O_CPU_TYPE_MC88000
:
2357 case BFD_MACH_O_CPU_TYPE_POWERPC
:
2359 case BFD_MACH_O_CPU_TYPE_I386
:
2361 case BFD_MACH_O_CPU_TYPE_SPARC
:
2363 case BFD_MACH_O_CPU_TYPE_I860
:
2365 case BFD_MACH_O_CPU_TYPE_HPPA
:
2366 return 0xc0000000 - 0x04000000;
2373 bfd_mach_o_bfd_print_private_bfd_data (bfd
*abfd
, PTR ptr
)
2375 bfd_mach_o_data_struct
*mdata
= abfd
->tdata
.mach_o_data
;
2376 FILE *file
= (FILE *) ptr
;
2378 unsigned int sec_nbr
= 0;
2380 fprintf (file
, _("Segments and Sections:\n"));
2381 fprintf (file
, _(" #: Segment name Section name Address\n"));
2383 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
2385 bfd_mach_o_segment_command
*seg
;
2387 if (mdata
->commands
[i
].type
!= BFD_MACH_O_LC_SEGMENT
2388 && mdata
->commands
[i
].type
!= BFD_MACH_O_LC_SEGMENT_64
)
2391 seg
= &mdata
->commands
[i
].command
.segment
;
2393 fprintf (file
, "[Segment %-16s ", seg
->segname
);
2394 fprintf_vma (file
, seg
->vmaddr
);
2395 fprintf (file
, "-");
2396 fprintf_vma (file
, seg
->vmaddr
+ seg
->vmsize
- 1);
2398 fputc (seg
->initprot
& BFD_MACH_O_PROT_READ
? 'r' : '-', file
);
2399 fputc (seg
->initprot
& BFD_MACH_O_PROT_WRITE
? 'w' : '-', file
);
2400 fputc (seg
->initprot
& BFD_MACH_O_PROT_EXECUTE
? 'x' : '-', file
);
2401 fprintf (file
, "]\n");
2402 for (j
= 0; j
< seg
->nsects
; j
++)
2404 bfd_mach_o_section
*sec
= &seg
->sections
[j
];
2405 fprintf (file
, "%02u: %-16s %-16s ", ++sec_nbr
,
2406 sec
->segname
, sec
->sectname
);
2407 fprintf_vma (file
, sec
->addr
);
2408 fprintf (file
, " ");
2409 fprintf_vma (file
, sec
->size
);
2410 fprintf (file
, " %08lx\n", sec
->flags
);
2414 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
2416 bfd_mach_o_load_command
*cmd
= &mdata
->commands
[i
];
2420 case BFD_MACH_O_LC_SEGMENT
:
2421 case BFD_MACH_O_LC_SEGMENT_64
:
2423 case BFD_MACH_O_LC_UUID
:
2425 bfd_mach_o_uuid_command
*uuid
= &cmd
->command
.uuid
;
2430 for (i
= 0; i
< sizeof (uuid
->uuid
); i
++)
2431 fprintf (file
, " %02x", uuid
->uuid
[i
]);
2435 case BFD_MACH_O_LC_LOAD_DYLIB
:
2437 bfd_mach_o_dylib_command
*dylib
= &cmd
->command
.dylib
;
2438 bfd_byte
*data
= NULL
;
2440 if (! bfd_malloc_and_get_section (abfd
, dylib
->section
, &data
))
2448 data
+ dylib
->name_offset
- cmd
->offset
- 8);
2449 fprintf (file
, " time stamp: 0x%08lx\n",
2451 fprintf (file
, " current version: 0x%08lx\n",
2452 dylib
->current_version
);
2453 fprintf (file
, " comptibility version: 0x%08lx\n",
2454 dylib
->compatibility_version
);
2458 case BFD_MACH_O_LC_LOAD_DYLINKER
:
2460 bfd_mach_o_dylinker_command
*linker
= &cmd
->command
.dylinker
;
2461 bfd_byte
*data
= NULL
;
2463 if (! bfd_malloc_and_get_section (abfd
, linker
->section
, &data
))
2470 "LOAD_DYLINKER: %s\n",
2471 data
+ linker
->name_offset
- cmd
->offset
- 8);
2475 case BFD_MACH_O_LC_SYMTAB
:
2477 bfd_mach_o_symtab_command
*symtab
= &cmd
->command
.symtab
;
2479 "LC_SYMTAB: nsyms: %lu, strsize: %lu\n",
2480 symtab
->nsyms
, symtab
->strsize
);
2483 case BFD_MACH_O_LC_DYSYMTAB
:
2485 bfd_mach_o_dysymtab_command
*dysymtab
= &cmd
->command
.dysymtab
;
2488 " local symbols: index: %lu number: %lu\n",
2489 dysymtab
->ilocalsym
, dysymtab
->nlocalsym
);
2491 " external symbols: index: %lu number: %lu\n",
2492 dysymtab
->iextdefsym
, dysymtab
->nextdefsym
);
2494 " undefined symbols: index: %lu number: %lu\n",
2495 dysymtab
->iundefsym
, dysymtab
->nundefsym
);
2497 " ntoc: offset: %lu number: %lu\n",
2498 dysymtab
->tocoff
, dysymtab
->ntoc
);
2500 " module table: offset: %lu number: %lu\n",
2501 dysymtab
->modtaboff
, dysymtab
->nmodtab
);
2505 fprintf (file
, "LC_%d\n", cmd
->type
);
2514 bfd_mach_o_core_fetch_environment (bfd
*abfd
,
2515 unsigned char **rbuf
,
2518 bfd_mach_o_data_struct
*mdata
= abfd
->tdata
.mach_o_data
;
2519 unsigned long stackaddr
= bfd_mach_o_stack_addr (mdata
->header
.cputype
);
2522 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
2524 bfd_mach_o_load_command
*cur
= &mdata
->commands
[i
];
2525 bfd_mach_o_segment_command
*seg
= NULL
;
2527 if (cur
->type
!= BFD_MACH_O_LC_SEGMENT
)
2530 seg
= &cur
->command
.segment
;
2532 if ((seg
->vmaddr
+ seg
->vmsize
) == stackaddr
)
2534 unsigned long start
= seg
->fileoff
;
2535 unsigned long end
= seg
->fileoff
+ seg
->filesize
;
2536 unsigned char *buf
= bfd_malloc (1024);
2537 unsigned long size
= 1024;
2541 bfd_size_type nread
= 0;
2542 unsigned long offset
;
2543 int found_nonnull
= 0;
2545 if (size
> (end
- start
))
2546 size
= (end
- start
);
2548 buf
= bfd_realloc_or_free (buf
, size
);
2552 bfd_seek (abfd
, end
- size
, SEEK_SET
);
2553 nread
= bfd_bread (buf
, size
, abfd
);
2561 for (offset
= 4; offset
<= size
; offset
+= 4)
2565 val
= *((unsigned long *) (buf
+ size
- offset
));
2566 if (! found_nonnull
)
2571 else if (val
== 0x0)
2573 unsigned long bottom
;
2576 bottom
= seg
->fileoff
+ seg
->filesize
- offset
;
2577 top
= seg
->fileoff
+ seg
->filesize
- 4;
2578 *rbuf
= bfd_malloc (top
- bottom
);
2579 *rlen
= top
- bottom
;
2581 memcpy (*rbuf
, buf
+ size
- *rlen
, *rlen
);
2587 if (size
== (end
- start
))
2601 bfd_mach_o_core_file_failing_command (bfd
*abfd
)
2603 unsigned char *buf
= NULL
;
2604 unsigned int len
= 0;
2607 ret
= bfd_mach_o_core_fetch_environment (abfd
, &buf
, &len
);
2611 return (char *) buf
;
2615 bfd_mach_o_core_file_failing_signal (bfd
*abfd ATTRIBUTE_UNUSED
)
2620 #define TARGET_NAME mach_o_be_vec
2621 #define TARGET_STRING "mach-o-be"
2622 #define TARGET_BIG_ENDIAN 1
2623 #define TARGET_ARCHIVE 0
2625 #include "mach-o-target.c"
2628 #undef TARGET_STRING
2629 #undef TARGET_BIG_ENDIAN
2630 #undef TARGET_ARCHIVE
2632 #define TARGET_NAME mach_o_le_vec
2633 #define TARGET_STRING "mach-o-le"
2634 #define TARGET_BIG_ENDIAN 0
2635 #define TARGET_ARCHIVE 0
2637 #include "mach-o-target.c"
2640 #undef TARGET_STRING
2641 #undef TARGET_BIG_ENDIAN
2642 #undef TARGET_ARCHIVE
2644 #define TARGET_NAME mach_o_fat_vec
2645 #define TARGET_STRING "mach-o-fat"
2646 #define TARGET_BIG_ENDIAN 1
2647 #define TARGET_ARCHIVE 1
2649 #include "mach-o-target.c"
2652 #undef TARGET_STRING
2653 #undef TARGET_BIG_ENDIAN
2654 #undef TARGET_ARCHIVE