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"
30 #define BFD_IO_FUNCS 0
33 #define bfd_mach_o_mkarchive _bfd_noarchive_mkarchive
34 #define bfd_mach_o_read_ar_hdr _bfd_noarchive_read_ar_hdr
35 #define bfd_mach_o_slurp_armap _bfd_noarchive_slurp_armap
36 #define bfd_mach_o_slurp_extended_name_table _bfd_noarchive_slurp_extended_name_table
37 #define bfd_mach_o_construct_extended_name_table _bfd_noarchive_construct_extended_name_table
38 #define bfd_mach_o_truncate_arname _bfd_noarchive_truncate_arname
39 #define bfd_mach_o_write_armap _bfd_noarchive_write_armap
40 #define bfd_mach_o_get_elt_at_index _bfd_noarchive_get_elt_at_index
41 #define bfd_mach_o_generic_stat_arch_elt _bfd_noarchive_generic_stat_arch_elt
42 #define bfd_mach_o_update_armap_timestamp _bfd_noarchive_update_armap_timestamp
43 #define bfd_mach_o_close_and_cleanup _bfd_generic_close_and_cleanup
44 #define bfd_mach_o_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
45 #define bfd_mach_o_new_section_hook _bfd_generic_new_section_hook
46 #define bfd_mach_o_get_section_contents_in_window _bfd_generic_get_section_contents_in_window
47 #define bfd_mach_o_bfd_is_local_label_name _bfd_nosymbols_bfd_is_local_label_name
48 #define bfd_mach_o_bfd_is_target_special_symbol ((bfd_boolean (*) (bfd *, asymbol *)) bfd_false)
49 #define bfd_mach_o_bfd_is_local_label_name _bfd_nosymbols_bfd_is_local_label_name
50 #define bfd_mach_o_get_lineno _bfd_nosymbols_get_lineno
51 #define bfd_mach_o_find_nearest_line _bfd_nosymbols_find_nearest_line
52 #define bfd_mach_o_find_inliner_info _bfd_nosymbols_find_inliner_info
53 #define bfd_mach_o_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
54 #define bfd_mach_o_read_minisymbols _bfd_generic_read_minisymbols
55 #define bfd_mach_o_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
56 #define bfd_mach_o_bfd_get_relocated_section_contents bfd_generic_get_relocated_section_contents
57 #define bfd_mach_o_bfd_relax_section bfd_generic_relax_section
58 #define bfd_mach_o_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
59 #define bfd_mach_o_bfd_link_hash_table_free _bfd_generic_link_hash_table_free
60 #define bfd_mach_o_bfd_link_add_symbols _bfd_generic_link_add_symbols
61 #define bfd_mach_o_bfd_link_just_syms _bfd_generic_link_just_syms
62 #define bfd_mach_o_bfd_final_link _bfd_generic_final_link
63 #define bfd_mach_o_bfd_link_split_section _bfd_generic_link_split_section
64 #define bfd_mach_o_set_arch_mach bfd_default_set_arch_mach
65 #define bfd_mach_o_bfd_merge_private_bfd_data _bfd_generic_bfd_merge_private_bfd_data
66 #define bfd_mach_o_bfd_set_private_flags _bfd_generic_bfd_set_private_flags
67 #define bfd_mach_o_bfd_print_private_bfd_data _bfd_generic_bfd_print_private_bfd_data
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 /* The flags field of a section structure is separated into two parts a section
80 type and section attributes. The section types are mutually exclusive (it
81 can only have one type) but the section attributes are not (it may have more
82 than one attribute). */
84 #define SECTION_TYPE 0x000000ff /* 256 section types. */
85 #define SECTION_ATTRIBUTES 0xffffff00 /* 24 section attributes. */
87 /* Constants for the section attributes part of the flags field of a section
90 #define SECTION_ATTRIBUTES_USR 0xff000000 /* User-settable attributes. */
91 #define S_ATTR_PURE_INSTRUCTIONS 0x80000000 /* Section contains only true machine instructions. */
92 #define SECTION_ATTRIBUTES_SYS 0x00ffff00 /* System setable attributes. */
93 #define S_ATTR_SOME_INSTRUCTIONS 0x00000400 /* Section contains some machine instructions. */
94 #define S_ATTR_EXT_RELOC 0x00000200 /* Section has external relocation entries. */
95 #define S_ATTR_LOC_RELOC 0x00000100 /* Section has local relocation entries. */
106 bfd_mach_o_valid (bfd
*abfd
)
108 if (abfd
== NULL
|| abfd
->xvec
== NULL
)
111 if (! ((abfd
->xvec
== &mach_o_be_vec
)
112 || (abfd
->xvec
== &mach_o_le_vec
)
113 || (abfd
->xvec
== &mach_o_fat_vec
)))
116 if (abfd
->tdata
.mach_o_data
== NULL
)
121 /* Copy any private info we understand from the input symbol
122 to the output symbol. */
125 bfd_mach_o_bfd_copy_private_symbol_data (bfd
*ibfd ATTRIBUTE_UNUSED
,
126 asymbol
*isymbol ATTRIBUTE_UNUSED
,
127 bfd
*obfd ATTRIBUTE_UNUSED
,
128 asymbol
*osymbol ATTRIBUTE_UNUSED
)
133 /* Copy any private info we understand from the input section
134 to the output section. */
137 bfd_mach_o_bfd_copy_private_section_data (bfd
*ibfd ATTRIBUTE_UNUSED
,
138 asection
*isection ATTRIBUTE_UNUSED
,
139 bfd
*obfd ATTRIBUTE_UNUSED
,
140 asection
*osection ATTRIBUTE_UNUSED
)
145 /* Copy any private info we understand from the input bfd
146 to the output bfd. */
149 bfd_mach_o_bfd_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
151 BFD_ASSERT (bfd_mach_o_valid (ibfd
));
152 BFD_ASSERT (bfd_mach_o_valid (obfd
));
154 obfd
->tdata
.mach_o_data
= ibfd
->tdata
.mach_o_data
;
155 obfd
->tdata
.mach_o_data
->ibfd
= ibfd
;
160 bfd_mach_o_count_symbols (bfd
*abfd
)
162 bfd_mach_o_data_struct
*mdata
= NULL
;
166 BFD_ASSERT (bfd_mach_o_valid (abfd
));
167 mdata
= abfd
->tdata
.mach_o_data
;
169 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
170 if (mdata
->commands
[i
].type
== BFD_MACH_O_LC_SYMTAB
)
172 bfd_mach_o_symtab_command
*sym
= &mdata
->commands
[i
].command
.symtab
;
180 bfd_mach_o_get_symtab_upper_bound (bfd
*abfd
)
182 long nsyms
= bfd_mach_o_count_symbols (abfd
);
187 return ((nsyms
+ 1) * sizeof (asymbol
*));
191 bfd_mach_o_canonicalize_symtab (bfd
*abfd
, asymbol
**alocation
)
193 bfd_mach_o_data_struct
*mdata
= abfd
->tdata
.mach_o_data
;
194 long nsyms
= bfd_mach_o_count_symbols (abfd
);
195 asymbol
**csym
= alocation
;
201 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
203 if (mdata
->commands
[i
].type
== BFD_MACH_O_LC_SYMTAB
)
205 bfd_mach_o_symtab_command
*sym
= &mdata
->commands
[i
].command
.symtab
;
207 if (bfd_mach_o_scan_read_symtab_symbols (abfd
, &mdata
->commands
[i
].command
.symtab
) != 0)
209 fprintf (stderr
, "bfd_mach_o_canonicalize_symtab: unable to load symbols for section %lu\n", i
);
213 BFD_ASSERT (sym
->symbols
!= NULL
);
215 for (j
= 0; j
< sym
->nsyms
; j
++)
217 BFD_ASSERT (csym
< (alocation
+ nsyms
));
218 *csym
++ = &sym
->symbols
[j
];
229 bfd_mach_o_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
233 bfd_symbol_info (symbol
, ret
);
237 bfd_mach_o_print_symbol (bfd
*abfd
,
240 bfd_print_symbol_type how
)
242 FILE *file
= (FILE *) afile
;
246 case bfd_print_symbol_name
:
247 fprintf (file
, "%s", symbol
->name
);
250 bfd_print_symbol_vandf (abfd
, (PTR
) file
, symbol
);
251 fprintf (file
, " %-5s %s", symbol
->section
->name
, symbol
->name
);
256 bfd_mach_o_convert_architecture (bfd_mach_o_cpu_type mtype
,
257 bfd_mach_o_cpu_subtype msubtype ATTRIBUTE_UNUSED
,
258 enum bfd_architecture
*type
,
259 unsigned long *subtype
)
261 *subtype
= bfd_arch_unknown
;
265 case BFD_MACH_O_CPU_TYPE_VAX
: *type
= bfd_arch_vax
; break;
266 case BFD_MACH_O_CPU_TYPE_MC680x0
: *type
= bfd_arch_m68k
; break;
267 case BFD_MACH_O_CPU_TYPE_I386
: *type
= bfd_arch_i386
; break;
268 case BFD_MACH_O_CPU_TYPE_MIPS
: *type
= bfd_arch_mips
; break;
269 case BFD_MACH_O_CPU_TYPE_MC98000
: *type
= bfd_arch_m98k
; break;
270 case BFD_MACH_O_CPU_TYPE_HPPA
: *type
= bfd_arch_hppa
; break;
271 case BFD_MACH_O_CPU_TYPE_ARM
: *type
= bfd_arch_arm
; break;
272 case BFD_MACH_O_CPU_TYPE_MC88000
: *type
= bfd_arch_m88k
; break;
273 case BFD_MACH_O_CPU_TYPE_SPARC
: *type
= bfd_arch_sparc
; break;
274 case BFD_MACH_O_CPU_TYPE_I860
: *type
= bfd_arch_i860
; break;
275 case BFD_MACH_O_CPU_TYPE_ALPHA
: *type
= bfd_arch_alpha
; break;
276 case BFD_MACH_O_CPU_TYPE_POWERPC
: *type
= bfd_arch_powerpc
; break;
277 default: *type
= bfd_arch_unknown
; break;
282 case bfd_arch_i386
: *subtype
= bfd_mach_i386_i386
; break;
283 case bfd_arch_sparc
: *subtype
= bfd_mach_sparc
; break;
285 *subtype
= bfd_arch_unknown
;
290 bfd_mach_o_write_header (bfd
*abfd
, bfd_mach_o_header
*header
)
292 unsigned char buf
[28];
294 bfd_h_put_32 (abfd
, header
->magic
, buf
+ 0);
295 bfd_h_put_32 (abfd
, header
->cputype
, buf
+ 4);
296 bfd_h_put_32 (abfd
, header
->cpusubtype
, buf
+ 8);
297 bfd_h_put_32 (abfd
, header
->filetype
, buf
+ 12);
298 bfd_h_put_32 (abfd
, header
->ncmds
, buf
+ 16);
299 bfd_h_put_32 (abfd
, header
->sizeofcmds
, buf
+ 20);
300 bfd_h_put_32 (abfd
, header
->flags
, buf
+ 24);
302 bfd_seek (abfd
, 0, SEEK_SET
);
303 if (bfd_bwrite ((PTR
) buf
, 28, abfd
) != 28)
310 bfd_mach_o_scan_write_thread (bfd
*abfd
,
311 bfd_mach_o_load_command
*command
)
313 bfd_mach_o_thread_command
*cmd
= &command
->command
.thread
;
315 unsigned char buf
[8];
317 unsigned int nflavours
;
319 BFD_ASSERT ((command
->type
== BFD_MACH_O_LC_THREAD
)
320 || (command
->type
== BFD_MACH_O_LC_UNIXTHREAD
));
324 for (i
= 0; i
< cmd
->nflavours
; i
++)
326 BFD_ASSERT ((cmd
->flavours
[i
].size
% 4) == 0);
327 BFD_ASSERT (cmd
->flavours
[i
].offset
== (command
->offset
+ offset
+ 8));
329 bfd_h_put_32 (abfd
, cmd
->flavours
[i
].flavour
, buf
);
330 bfd_h_put_32 (abfd
, (cmd
->flavours
[i
].size
/ 4), buf
+ 4);
332 bfd_seek (abfd
, command
->offset
+ offset
, SEEK_SET
);
333 if (bfd_bwrite ((PTR
) buf
, 8, abfd
) != 8)
336 offset
+= cmd
->flavours
[i
].size
+ 8;
343 bfd_mach_o_scan_write_section (bfd
*abfd
,
344 bfd_mach_o_section
*section
,
347 unsigned char buf
[68];
349 memcpy (buf
, section
->sectname
, 16);
350 memcpy (buf
+ 16, section
->segname
, 16);
351 bfd_h_put_32 (abfd
, section
->addr
, buf
+ 32);
352 bfd_h_put_32 (abfd
, section
->size
, buf
+ 36);
353 bfd_h_put_32 (abfd
, section
->offset
, buf
+ 40);
354 bfd_h_put_32 (abfd
, section
->align
, buf
+ 44);
355 bfd_h_put_32 (abfd
, section
->reloff
, buf
+ 48);
356 bfd_h_put_32 (abfd
, section
->nreloc
, buf
+ 52);
357 bfd_h_put_32 (abfd
, section
->flags
, buf
+ 56);
358 /* bfd_h_put_32 (abfd, section->reserved1, buf + 60); */
359 /* bfd_h_put_32 (abfd, section->reserved2, buf + 64); */
361 bfd_seek (abfd
, offset
, SEEK_SET
);
362 if (bfd_bwrite ((PTR
) buf
, 68, abfd
) != 68)
369 bfd_mach_o_scan_write_segment (bfd
*abfd
, bfd_mach_o_load_command
*command
)
371 unsigned char buf
[48];
372 bfd_mach_o_segment_command
*seg
= &command
->command
.segment
;
375 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SEGMENT
);
377 memcpy (buf
, seg
->segname
, 16);
378 bfd_h_put_32 (abfd
, seg
->vmaddr
, buf
+ 16);
379 bfd_h_put_32 (abfd
, seg
->vmsize
, buf
+ 20);
380 bfd_h_put_32 (abfd
, seg
->fileoff
, buf
+ 24);
381 bfd_h_put_32 (abfd
, seg
->filesize
, buf
+ 28);
382 bfd_h_put_32 (abfd
, 0 /* seg->maxprot */, buf
+ 32);
383 bfd_h_put_32 (abfd
, 0 /* seg->initprot */, buf
+ 36);
384 bfd_h_put_32 (abfd
, seg
->nsects
, buf
+ 40);
385 bfd_h_put_32 (abfd
, seg
->flags
, buf
+ 44);
387 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
388 if (bfd_bwrite ((PTR
) buf
, 48, abfd
) != 48)
393 bfd_vma nbytes
= seg
->filesize
;
394 bfd_vma curoff
= seg
->fileoff
;
398 bfd_vma thisread
= nbytes
;
403 bfd_seek (abfd
, curoff
, SEEK_SET
);
404 if (bfd_bread ((PTR
) buf
, thisread
, abfd
) != thisread
)
407 bfd_seek (abfd
, curoff
, SEEK_SET
);
408 if (bfd_bwrite ((PTR
) buf
, thisread
, abfd
) != thisread
)
416 for (i
= 0; i
< seg
->nsects
; i
++)
418 bfd_vma segoff
= command
->offset
+ 48 + 8 + (i
* 68);
420 if (bfd_mach_o_scan_write_section (abfd
, &seg
->sections
[i
], segoff
) != 0)
428 bfd_mach_o_scan_write_symtab_symbols (bfd
*abfd
,
429 bfd_mach_o_load_command
*command
)
431 bfd_mach_o_symtab_command
*sym
= &command
->command
.symtab
;
435 for (i
= 0; i
< sym
->nsyms
; i
++)
437 unsigned char buf
[12];
438 bfd_vma symoff
= sym
->symoff
+ (i
* 12);
439 unsigned char ntype
= 0;
440 unsigned char nsect
= 0;
443 s
= &sym
->symbols
[i
];
445 /* Instead just set from the stored values. */
446 ntype
= (s
->udata
.i
>> 24) & 0xff;
447 nsect
= (s
->udata
.i
>> 16) & 0xff;
448 ndesc
= s
->udata
.i
& 0xffff;
450 bfd_h_put_32 (abfd
, s
->name
- sym
->strtab
, buf
);
451 bfd_h_put_8 (abfd
, ntype
, buf
+ 4);
452 bfd_h_put_8 (abfd
, nsect
, buf
+ 5);
453 bfd_h_put_16 (abfd
, ndesc
, buf
+ 6);
454 bfd_h_put_32 (abfd
, s
->section
->vma
+ s
->value
, buf
+ 8);
456 bfd_seek (abfd
, symoff
, SEEK_SET
);
457 if (bfd_bwrite ((PTR
) buf
, 12, abfd
) != 12)
459 fprintf (stderr
, "bfd_mach_o_scan_write_symtab_symbols: unable to write %d bytes at %lu\n",
460 12, (unsigned long) symoff
);
469 bfd_mach_o_scan_write_symtab (bfd
*abfd
, bfd_mach_o_load_command
*command
)
471 bfd_mach_o_symtab_command
*seg
= &command
->command
.symtab
;
472 unsigned char buf
[16];
474 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SYMTAB
);
476 bfd_h_put_32 (abfd
, seg
->symoff
, buf
);
477 bfd_h_put_32 (abfd
, seg
->nsyms
, buf
+ 4);
478 bfd_h_put_32 (abfd
, seg
->stroff
, buf
+ 8);
479 bfd_h_put_32 (abfd
, seg
->strsize
, buf
+ 12);
481 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
482 if (bfd_bwrite ((PTR
) buf
, 16, abfd
) != 16)
485 if (bfd_mach_o_scan_write_symtab_symbols (abfd
, command
) != 0)
492 bfd_mach_o_write_contents (bfd
*abfd
)
497 bfd_mach_o_data_struct
*mdata
= abfd
->tdata
.mach_o_data
;
499 /* Write data sections first in case they overlap header data to be
502 for (s
= abfd
->sections
; s
!= (asection
*) NULL
; s
= s
->next
)
505 /* Now write header information. */
506 if (bfd_mach_o_write_header (abfd
, &mdata
->header
) != 0)
509 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
511 unsigned char buf
[8];
512 bfd_mach_o_load_command
*cur
= &mdata
->commands
[i
];
513 unsigned long typeflag
;
515 typeflag
= cur
->type_required
? cur
->type
& BFD_MACH_O_LC_REQ_DYLD
: cur
->type
;
517 bfd_h_put_32 (abfd
, typeflag
, buf
);
518 bfd_h_put_32 (abfd
, cur
->len
, buf
+ 4);
520 bfd_seek (abfd
, cur
->offset
, SEEK_SET
);
521 if (bfd_bwrite ((PTR
) buf
, 8, abfd
) != 8)
526 case BFD_MACH_O_LC_SEGMENT
:
527 if (bfd_mach_o_scan_write_segment (abfd
, cur
) != 0)
530 case BFD_MACH_O_LC_SYMTAB
:
531 if (bfd_mach_o_scan_write_symtab (abfd
, cur
) != 0)
534 case BFD_MACH_O_LC_SYMSEG
:
536 case BFD_MACH_O_LC_THREAD
:
537 case BFD_MACH_O_LC_UNIXTHREAD
:
538 if (bfd_mach_o_scan_write_thread (abfd
, cur
) != 0)
541 case BFD_MACH_O_LC_LOADFVMLIB
:
542 case BFD_MACH_O_LC_IDFVMLIB
:
543 case BFD_MACH_O_LC_IDENT
:
544 case BFD_MACH_O_LC_FVMFILE
:
545 case BFD_MACH_O_LC_PREPAGE
:
546 case BFD_MACH_O_LC_DYSYMTAB
:
547 case BFD_MACH_O_LC_LOAD_DYLIB
:
548 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB
:
549 case BFD_MACH_O_LC_ID_DYLIB
:
550 case BFD_MACH_O_LC_LOAD_DYLINKER
:
551 case BFD_MACH_O_LC_ID_DYLINKER
:
552 case BFD_MACH_O_LC_PREBOUND_DYLIB
:
553 case BFD_MACH_O_LC_ROUTINES
:
554 case BFD_MACH_O_LC_SUB_FRAMEWORK
:
558 "unable to write unknown load command 0x%lx\n",
559 (unsigned long) cur
->type
);
568 bfd_mach_o_sizeof_headers (bfd
*a ATTRIBUTE_UNUSED
,
569 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
574 /* Make an empty symbol. This is required only because
575 bfd_make_section_anyway wants to create a symbol for the section. */
578 bfd_mach_o_make_empty_symbol (bfd
*abfd
)
582 new = bfd_zalloc (abfd
, sizeof (* new));
590 bfd_mach_o_read_header (bfd
*abfd
, bfd_mach_o_header
*header
)
592 unsigned char buf
[28];
593 bfd_vma (*get32
) (const void *) = NULL
;
595 bfd_seek (abfd
, 0, SEEK_SET
);
597 if (bfd_bread ((PTR
) buf
, 28, abfd
) != 28)
600 if (bfd_getb32 (buf
) == 0xfeedface)
602 header
->byteorder
= BFD_ENDIAN_BIG
;
603 header
->magic
= 0xfeedface;
606 else if (bfd_getl32 (buf
) == 0xfeedface)
608 header
->byteorder
= BFD_ENDIAN_LITTLE
;
609 header
->magic
= 0xfeedface;
614 header
->byteorder
= BFD_ENDIAN_UNKNOWN
;
618 header
->cputype
= (*get32
) (buf
+ 4);
619 header
->cpusubtype
= (*get32
) (buf
+ 8);
620 header
->filetype
= (*get32
) (buf
+ 12);
621 header
->ncmds
= (*get32
) (buf
+ 16);
622 header
->sizeofcmds
= (*get32
) (buf
+ 20);
623 header
->flags
= (*get32
) (buf
+ 24);
629 bfd_mach_o_make_bfd_section (bfd
*abfd
, bfd_mach_o_section
*section
)
633 const char *prefix
= "LC_SEGMENT";
634 unsigned int snamelen
;
637 snamelen
= strlen (prefix
) + 1
638 + strlen (section
->segname
) + 1
639 + strlen (section
->sectname
) + 1;
641 sname
= bfd_alloc (abfd
, snamelen
);
644 sprintf (sname
, "%s.%s.%s", prefix
, section
->segname
, section
->sectname
);
647 if ((section
->flags
& SECTION_TYPE
) != BFD_MACH_O_S_ZEROFILL
)
648 flags
= SEC_HAS_CONTENTS
| SEC_LOAD
| SEC_ALLOC
| SEC_CODE
;
649 bfdsec
= bfd_make_section_anyway_with_flags (abfd
, sname
, flags
);
653 bfdsec
->vma
= section
->addr
;
654 bfdsec
->lma
= section
->addr
;
655 bfdsec
->size
= section
->size
;
656 bfdsec
->filepos
= section
->offset
;
657 bfdsec
->alignment_power
= section
->align
;
663 bfd_mach_o_scan_read_section (bfd
*abfd
,
664 bfd_mach_o_section
*section
,
667 unsigned char buf
[68];
669 bfd_seek (abfd
, offset
, SEEK_SET
);
670 if (bfd_bread ((PTR
) buf
, 68, abfd
) != 68)
673 memcpy (section
->sectname
, buf
, 16);
674 section
->sectname
[16] = '\0';
675 memcpy (section
->segname
, buf
+ 16, 16);
676 section
->segname
[16] = '\0';
677 section
->addr
= bfd_h_get_32 (abfd
, buf
+ 32);
678 section
->size
= bfd_h_get_32 (abfd
, buf
+ 36);
679 section
->offset
= bfd_h_get_32 (abfd
, buf
+ 40);
680 section
->align
= bfd_h_get_32 (abfd
, buf
+ 44);
681 section
->reloff
= bfd_h_get_32 (abfd
, buf
+ 48);
682 section
->nreloc
= bfd_h_get_32 (abfd
, buf
+ 52);
683 section
->flags
= bfd_h_get_32 (abfd
, buf
+ 56);
684 section
->reserved1
= bfd_h_get_32 (abfd
, buf
+ 60);
685 section
->reserved2
= bfd_h_get_32 (abfd
, buf
+ 64);
686 section
->bfdsection
= bfd_mach_o_make_bfd_section (abfd
, section
);
688 if (section
->bfdsection
== NULL
)
695 bfd_mach_o_scan_read_symtab_symbol (bfd
*abfd
,
696 bfd_mach_o_symtab_command
*sym
,
700 bfd_mach_o_data_struct
*mdata
= abfd
->tdata
.mach_o_data
;
701 bfd_vma symoff
= sym
->symoff
+ (i
* 12);
702 unsigned char buf
[12];
703 unsigned char type
= -1;
704 unsigned char section
= -1;
706 unsigned long value
= -1;
707 unsigned long stroff
= -1;
708 unsigned int symtype
= -1;
710 BFD_ASSERT (sym
->strtab
!= NULL
);
712 bfd_seek (abfd
, symoff
, SEEK_SET
);
713 if (bfd_bread ((PTR
) buf
, 12, abfd
) != 12)
715 fprintf (stderr
, "bfd_mach_o_scan_read_symtab_symbol: unable to read %d bytes at %lu\n",
716 12, (unsigned long) symoff
);
720 stroff
= bfd_h_get_32 (abfd
, buf
);
721 type
= bfd_h_get_8 (abfd
, buf
+ 4);
722 symtype
= (type
& 0x0e);
723 section
= bfd_h_get_8 (abfd
, buf
+ 5) - 1;
724 desc
= bfd_h_get_16 (abfd
, buf
+ 6);
725 value
= bfd_h_get_32 (abfd
, buf
+ 8);
727 if (stroff
>= sym
->strsize
)
729 fprintf (stderr
, "bfd_mach_o_scan_read_symtab_symbol: symbol name out of range (%lu >= %lu)\n",
730 (unsigned long) stroff
, (unsigned long) sym
->strsize
);
735 s
->name
= sym
->strtab
+ stroff
;
737 s
->udata
.i
= (type
<< 24) | (section
<< 16) | desc
;
740 if (type
& BFD_MACH_O_N_STAB
)
742 s
->flags
|= BSF_DEBUGGING
;
743 s
->section
= bfd_und_section_ptr
;
747 if (type
& BFD_MACH_O_N_PEXT
)
749 type
&= ~BFD_MACH_O_N_PEXT
;
750 s
->flags
|= BSF_GLOBAL
;
753 if (type
& BFD_MACH_O_N_EXT
)
755 type
&= ~BFD_MACH_O_N_EXT
;
756 s
->flags
|= BSF_GLOBAL
;
761 case BFD_MACH_O_N_UNDF
:
762 s
->section
= bfd_und_section_ptr
;
764 case BFD_MACH_O_N_PBUD
:
765 s
->section
= bfd_und_section_ptr
;
767 case BFD_MACH_O_N_ABS
:
768 s
->section
= bfd_abs_section_ptr
;
770 case BFD_MACH_O_N_SECT
:
771 if ((section
> 0) && (section
<= mdata
->nsects
))
773 s
->section
= mdata
->sections
[section
- 1]->bfdsection
;
774 s
->value
= s
->value
- mdata
->sections
[section
- 1]->addr
;
778 /* Mach-O uses 0 to mean "no section"; not an error. */
781 fprintf (stderr
, "bfd_mach_o_scan_read_symtab_symbol: "
782 "symbol \"%s\" specified invalid section %d (max %lu): setting to undefined\n",
783 s
->name
, section
, mdata
->nsects
);
785 s
->section
= bfd_und_section_ptr
;
788 case BFD_MACH_O_N_INDR
:
789 fprintf (stderr
, "bfd_mach_o_scan_read_symtab_symbol: "
790 "symbol \"%s\" is unsupported 'indirect' reference: setting to undefined\n",
792 s
->section
= bfd_und_section_ptr
;
795 fprintf (stderr
, "bfd_mach_o_scan_read_symtab_symbol: "
796 "symbol \"%s\" specified invalid type field 0x%x: setting to undefined\n",
798 s
->section
= bfd_und_section_ptr
;
807 bfd_mach_o_scan_read_symtab_strtab (bfd
*abfd
,
808 bfd_mach_o_symtab_command
*sym
)
810 BFD_ASSERT (sym
->strtab
== NULL
);
812 if (abfd
->flags
& BFD_IN_MEMORY
)
814 struct bfd_in_memory
*b
;
816 b
= (struct bfd_in_memory
*) abfd
->iostream
;
818 if ((sym
->stroff
+ sym
->strsize
) > b
->size
)
820 bfd_set_error (bfd_error_file_truncated
);
823 sym
->strtab
= (char *) b
->buffer
+ sym
->stroff
;
827 sym
->strtab
= bfd_alloc (abfd
, sym
->strsize
);
828 if (sym
->strtab
== NULL
)
831 bfd_seek (abfd
, sym
->stroff
, SEEK_SET
);
832 if (bfd_bread ((PTR
) sym
->strtab
, sym
->strsize
, abfd
) != sym
->strsize
)
834 fprintf (stderr
, "bfd_mach_o_scan_read_symtab_strtab: unable to read %lu bytes at %lu\n",
835 sym
->strsize
, sym
->stroff
);
843 bfd_mach_o_scan_read_symtab_symbols (bfd
*abfd
,
844 bfd_mach_o_symtab_command
*sym
)
849 BFD_ASSERT (sym
->symbols
== NULL
);
850 sym
->symbols
= bfd_alloc (abfd
, sym
->nsyms
* sizeof (asymbol
));
852 if (sym
->symbols
== NULL
)
854 fprintf (stderr
, "bfd_mach_o_scan_read_symtab_symbols: unable to allocate memory for symbols\n");
858 ret
= bfd_mach_o_scan_read_symtab_strtab (abfd
, sym
);
862 for (i
= 0; i
< sym
->nsyms
; i
++)
864 ret
= bfd_mach_o_scan_read_symtab_symbol (abfd
, sym
, &sym
->symbols
[i
], i
);
873 bfd_mach_o_scan_read_dysymtab_symbol (bfd
*abfd
,
874 bfd_mach_o_dysymtab_command
*dysym
,
875 bfd_mach_o_symtab_command
*sym
,
879 unsigned long isymoff
= dysym
->indirectsymoff
+ (i
* 4);
880 unsigned long symindex
;
881 unsigned char buf
[4];
883 BFD_ASSERT (i
< dysym
->nindirectsyms
);
885 bfd_seek (abfd
, isymoff
, SEEK_SET
);
886 if (bfd_bread ((PTR
) buf
, 4, abfd
) != 4)
888 fprintf (stderr
, "bfd_mach_o_scan_read_dysymtab_symbol: unable to read %lu bytes at %lu\n",
889 (unsigned long) 4, isymoff
);
892 symindex
= bfd_h_get_32 (abfd
, buf
);
894 return bfd_mach_o_scan_read_symtab_symbol (abfd
, sym
, s
, symindex
);
898 bfd_mach_o_i386_flavour_string (unsigned int flavour
)
900 switch ((int) flavour
)
902 case BFD_MACH_O_i386_NEW_THREAD_STATE
: return "i386_NEW_THREAD_STATE";
903 case BFD_MACH_O_i386_FLOAT_STATE
: return "i386_FLOAT_STATE";
904 case BFD_MACH_O_i386_ISA_PORT_MAP_STATE
: return "i386_ISA_PORT_MAP_STATE";
905 case BFD_MACH_O_i386_V86_ASSIST_STATE
: return "i386_V86_ASSIST_STATE";
906 case BFD_MACH_O_i386_REGS_SEGS_STATE
: return "i386_REGS_SEGS_STATE";
907 case BFD_MACH_O_i386_THREAD_SYSCALL_STATE
: return "i386_THREAD_SYSCALL_STATE";
908 case BFD_MACH_O_i386_THREAD_STATE_NONE
: return "i386_THREAD_STATE_NONE";
909 case BFD_MACH_O_i386_SAVED_STATE
: return "i386_SAVED_STATE";
910 case BFD_MACH_O_i386_THREAD_STATE
: return "i386_THREAD_STATE";
911 case BFD_MACH_O_i386_THREAD_FPSTATE
: return "i386_THREAD_FPSTATE";
912 case BFD_MACH_O_i386_THREAD_EXCEPTSTATE
: return "i386_THREAD_EXCEPTSTATE";
913 case BFD_MACH_O_i386_THREAD_CTHREADSTATE
: return "i386_THREAD_CTHREADSTATE";
914 default: return "UNKNOWN";
919 bfd_mach_o_ppc_flavour_string (unsigned int flavour
)
921 switch ((int) flavour
)
923 case BFD_MACH_O_PPC_THREAD_STATE
: return "PPC_THREAD_STATE";
924 case BFD_MACH_O_PPC_FLOAT_STATE
: return "PPC_FLOAT_STATE";
925 case BFD_MACH_O_PPC_EXCEPTION_STATE
: return "PPC_EXCEPTION_STATE";
926 case BFD_MACH_O_PPC_VECTOR_STATE
: return "PPC_VECTOR_STATE";
927 default: return "UNKNOWN";
932 bfd_mach_o_scan_read_dylinker (bfd
*abfd
,
933 bfd_mach_o_load_command
*command
)
935 bfd_mach_o_dylinker_command
*cmd
= &command
->command
.dylinker
;
936 unsigned char buf
[4];
937 unsigned int nameoff
;
942 BFD_ASSERT ((command
->type
== BFD_MACH_O_LC_ID_DYLINKER
)
943 || (command
->type
== BFD_MACH_O_LC_LOAD_DYLINKER
));
945 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
946 if (bfd_bread ((PTR
) buf
, 4, abfd
) != 4)
949 nameoff
= bfd_h_get_32 (abfd
, buf
+ 0);
951 cmd
->name_offset
= command
->offset
+ nameoff
;
952 cmd
->name_len
= command
->len
- nameoff
;
954 if (command
->type
== BFD_MACH_O_LC_LOAD_DYLINKER
)
955 prefix
= "LC_LOAD_DYLINKER";
956 else if (command
->type
== BFD_MACH_O_LC_ID_DYLINKER
)
957 prefix
= "LC_ID_DYLINKER";
961 sname
= bfd_alloc (abfd
, strlen (prefix
) + 1);
964 strcpy (sname
, prefix
);
966 bfdsec
= bfd_make_section_anyway_with_flags (abfd
, sname
, SEC_HAS_CONTENTS
);
972 bfdsec
->size
= command
->len
- 8;
973 bfdsec
->filepos
= command
->offset
+ 8;
974 bfdsec
->alignment_power
= 0;
976 cmd
->section
= bfdsec
;
982 bfd_mach_o_scan_read_dylib (bfd
*abfd
, bfd_mach_o_load_command
*command
)
984 bfd_mach_o_dylib_command
*cmd
= &command
->command
.dylib
;
985 unsigned char buf
[16];
986 unsigned int nameoff
;
991 BFD_ASSERT ((command
->type
== BFD_MACH_O_LC_ID_DYLIB
)
992 || (command
->type
== BFD_MACH_O_LC_LOAD_DYLIB
)
993 || (command
->type
== BFD_MACH_O_LC_LOAD_WEAK_DYLIB
));
995 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
996 if (bfd_bread ((PTR
) buf
, 16, abfd
) != 16)
999 nameoff
= bfd_h_get_32 (abfd
, buf
+ 0);
1000 cmd
->timestamp
= bfd_h_get_32 (abfd
, buf
+ 4);
1001 cmd
->current_version
= bfd_h_get_32 (abfd
, buf
+ 8);
1002 cmd
->compatibility_version
= bfd_h_get_32 (abfd
, buf
+ 12);
1004 cmd
->name_offset
= command
->offset
+ nameoff
;
1005 cmd
->name_len
= command
->len
- nameoff
;
1007 if (command
->type
== BFD_MACH_O_LC_LOAD_DYLIB
)
1008 prefix
= "LC_LOAD_DYLIB";
1009 else if (command
->type
== BFD_MACH_O_LC_LOAD_WEAK_DYLIB
)
1010 prefix
= "LC_LOAD_WEAK_DYLIB";
1011 else if (command
->type
== BFD_MACH_O_LC_ID_DYLIB
)
1012 prefix
= "LC_ID_DYLIB";
1016 sname
= bfd_alloc (abfd
, strlen (prefix
) + 1);
1019 strcpy (sname
, prefix
);
1021 bfdsec
= bfd_make_section_anyway_with_flags (abfd
, sname
, SEC_HAS_CONTENTS
);
1027 bfdsec
->size
= command
->len
- 8;
1028 bfdsec
->filepos
= command
->offset
+ 8;
1029 bfdsec
->alignment_power
= 0;
1031 cmd
->section
= bfdsec
;
1037 bfd_mach_o_scan_read_prebound_dylib (bfd
*abfd ATTRIBUTE_UNUSED
,
1038 bfd_mach_o_load_command
*command ATTRIBUTE_UNUSED
)
1040 /* bfd_mach_o_prebound_dylib_command *cmd = &command->command.prebound_dylib; */
1042 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_PREBOUND_DYLIB
);
1047 bfd_mach_o_scan_read_thread (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1049 bfd_mach_o_data_struct
*mdata
= NULL
;
1050 bfd_mach_o_thread_command
*cmd
= &command
->command
.thread
;
1051 unsigned char buf
[8];
1053 unsigned int nflavours
;
1056 BFD_ASSERT ((command
->type
== BFD_MACH_O_LC_THREAD
)
1057 || (command
->type
== BFD_MACH_O_LC_UNIXTHREAD
));
1059 BFD_ASSERT (bfd_mach_o_valid (abfd
));
1060 mdata
= abfd
->tdata
.mach_o_data
;
1064 while (offset
!= command
->len
)
1066 if (offset
>= command
->len
)
1069 bfd_seek (abfd
, command
->offset
+ offset
, SEEK_SET
);
1071 if (bfd_bread ((PTR
) buf
, 8, abfd
) != 8)
1074 offset
+= 8 + bfd_h_get_32 (abfd
, buf
+ 4) * 4;
1078 cmd
->flavours
= bfd_alloc (abfd
, nflavours
* sizeof (bfd_mach_o_thread_flavour
));
1079 if (cmd
->flavours
== NULL
)
1081 cmd
->nflavours
= nflavours
;
1085 while (offset
!= command
->len
)
1087 if (offset
>= command
->len
)
1090 if (nflavours
>= cmd
->nflavours
)
1093 bfd_seek (abfd
, command
->offset
+ offset
, SEEK_SET
);
1095 if (bfd_bread ((PTR
) buf
, 8, abfd
) != 8)
1098 cmd
->flavours
[nflavours
].flavour
= bfd_h_get_32 (abfd
, buf
);
1099 cmd
->flavours
[nflavours
].offset
= command
->offset
+ offset
+ 8;
1100 cmd
->flavours
[nflavours
].size
= bfd_h_get_32 (abfd
, buf
+ 4) * 4;
1101 offset
+= cmd
->flavours
[nflavours
].size
+ 8;
1105 for (i
= 0; i
< nflavours
; i
++)
1108 unsigned int snamelen
;
1110 const char *flavourstr
;
1111 const char *prefix
= "LC_THREAD";
1114 switch (mdata
->header
.cputype
)
1116 case BFD_MACH_O_CPU_TYPE_POWERPC
:
1117 flavourstr
= bfd_mach_o_ppc_flavour_string (cmd
->flavours
[i
].flavour
);
1119 case BFD_MACH_O_CPU_TYPE_I386
:
1120 flavourstr
= bfd_mach_o_i386_flavour_string (cmd
->flavours
[i
].flavour
);
1123 flavourstr
= "UNKNOWN_ARCHITECTURE";
1127 snamelen
= strlen (prefix
) + 1 + 20 + 1 + strlen (flavourstr
) + 1;
1128 sname
= bfd_alloc (abfd
, snamelen
);
1134 sprintf (sname
, "%s.%s.%u", prefix
, flavourstr
, j
);
1135 if (bfd_get_section_by_name (abfd
, sname
) == NULL
)
1140 bfdsec
= bfd_make_section_with_flags (abfd
, sname
, SEC_HAS_CONTENTS
);
1144 bfdsec
->size
= cmd
->flavours
[i
].size
;
1145 bfdsec
->filepos
= cmd
->flavours
[i
].offset
;
1146 bfdsec
->alignment_power
= 0x0;
1148 cmd
->section
= bfdsec
;
1155 bfd_mach_o_scan_read_dysymtab (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1157 bfd_mach_o_dysymtab_command
*seg
= &command
->command
.dysymtab
;
1158 unsigned char buf
[72];
1160 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_DYSYMTAB
);
1162 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
1163 if (bfd_bread ((PTR
) buf
, 72, abfd
) != 72)
1166 seg
->ilocalsym
= bfd_h_get_32 (abfd
, buf
+ 0);
1167 seg
->nlocalsym
= bfd_h_get_32 (abfd
, buf
+ 4);
1168 seg
->iextdefsym
= bfd_h_get_32 (abfd
, buf
+ 8);
1169 seg
->nextdefsym
= bfd_h_get_32 (abfd
, buf
+ 12);
1170 seg
->iundefsym
= bfd_h_get_32 (abfd
, buf
+ 16);
1171 seg
->nundefsym
= bfd_h_get_32 (abfd
, buf
+ 20);
1172 seg
->tocoff
= bfd_h_get_32 (abfd
, buf
+ 24);
1173 seg
->ntoc
= bfd_h_get_32 (abfd
, buf
+ 28);
1174 seg
->modtaboff
= bfd_h_get_32 (abfd
, buf
+ 32);
1175 seg
->nmodtab
= bfd_h_get_32 (abfd
, buf
+ 36);
1176 seg
->extrefsymoff
= bfd_h_get_32 (abfd
, buf
+ 40);
1177 seg
->nextrefsyms
= bfd_h_get_32 (abfd
, buf
+ 44);
1178 seg
->indirectsymoff
= bfd_h_get_32 (abfd
, buf
+ 48);
1179 seg
->nindirectsyms
= bfd_h_get_32 (abfd
, buf
+ 52);
1180 seg
->extreloff
= bfd_h_get_32 (abfd
, buf
+ 56);
1181 seg
->nextrel
= bfd_h_get_32 (abfd
, buf
+ 60);
1182 seg
->locreloff
= bfd_h_get_32 (abfd
, buf
+ 64);
1183 seg
->nlocrel
= bfd_h_get_32 (abfd
, buf
+ 68);
1189 bfd_mach_o_scan_read_symtab (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1191 bfd_mach_o_symtab_command
*seg
= &command
->command
.symtab
;
1192 unsigned char buf
[16];
1195 const char *prefix
= "LC_SYMTAB.stabs";
1197 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SYMTAB
);
1199 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
1200 if (bfd_bread ((PTR
) buf
, 16, abfd
) != 16)
1203 seg
->symoff
= bfd_h_get_32 (abfd
, buf
);
1204 seg
->nsyms
= bfd_h_get_32 (abfd
, buf
+ 4);
1205 seg
->stroff
= bfd_h_get_32 (abfd
, buf
+ 8);
1206 seg
->strsize
= bfd_h_get_32 (abfd
, buf
+ 12);
1207 seg
->symbols
= NULL
;
1210 sname
= bfd_alloc (abfd
, strlen (prefix
) + 1);
1213 strcpy (sname
, prefix
);
1215 bfdsec
= bfd_make_section_anyway_with_flags (abfd
, sname
, SEC_HAS_CONTENTS
);
1221 bfdsec
->size
= seg
->nsyms
* 12;
1222 bfdsec
->filepos
= seg
->symoff
;
1223 bfdsec
->alignment_power
= 0;
1225 seg
->stabs_segment
= bfdsec
;
1227 prefix
= "LC_SYMTAB.stabstr";
1228 sname
= bfd_alloc (abfd
, strlen (prefix
) + 1);
1231 strcpy (sname
, prefix
);
1233 bfdsec
= bfd_make_section_anyway_with_flags (abfd
, sname
, SEC_HAS_CONTENTS
);
1239 bfdsec
->size
= seg
->strsize
;
1240 bfdsec
->filepos
= seg
->stroff
;
1241 bfdsec
->alignment_power
= 0;
1243 seg
->stabstr_segment
= bfdsec
;
1249 bfd_mach_o_scan_read_segment (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1251 unsigned char buf
[48];
1252 bfd_mach_o_segment_command
*seg
= &command
->command
.segment
;
1256 const char *prefix
= "LC_SEGMENT";
1257 unsigned int snamelen
;
1260 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SEGMENT
);
1262 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
1263 if (bfd_bread ((PTR
) buf
, 48, abfd
) != 48)
1266 memcpy (seg
->segname
, buf
, 16);
1267 seg
->vmaddr
= bfd_h_get_32 (abfd
, buf
+ 16);
1268 seg
->vmsize
= bfd_h_get_32 (abfd
, buf
+ 20);
1269 seg
->fileoff
= bfd_h_get_32 (abfd
, buf
+ 24);
1270 seg
->filesize
= bfd_h_get_32 (abfd
, buf
+ 28);
1271 /* seg->maxprot = bfd_h_get_32 (abfd, buf + 32); */
1272 /* seg->initprot = bfd_h_get_32 (abfd, buf + 36); */
1273 seg
->nsects
= bfd_h_get_32 (abfd
, buf
+ 40);
1274 seg
->flags
= bfd_h_get_32 (abfd
, buf
+ 44);
1276 snamelen
= strlen (prefix
) + 1 + strlen (seg
->segname
) + 1;
1277 sname
= bfd_alloc (abfd
, snamelen
);
1280 sprintf (sname
, "%s.%s", prefix
, seg
->segname
);
1282 flags
= SEC_HAS_CONTENTS
| SEC_LOAD
| SEC_ALLOC
| SEC_CODE
;
1283 bfdsec
= bfd_make_section_anyway_with_flags (abfd
, sname
, flags
);
1287 bfdsec
->vma
= seg
->vmaddr
;
1288 bfdsec
->lma
= seg
->vmaddr
;
1289 bfdsec
->size
= seg
->filesize
;
1290 bfdsec
->filepos
= seg
->fileoff
;
1291 bfdsec
->alignment_power
= 0x0;
1293 seg
->segment
= bfdsec
;
1295 if (seg
->nsects
!= 0)
1297 seg
->sections
= bfd_alloc (abfd
, seg
->nsects
* sizeof (bfd_mach_o_section
));
1298 if (seg
->sections
== NULL
)
1301 for (i
= 0; i
< seg
->nsects
; i
++)
1303 bfd_vma segoff
= command
->offset
+ 48 + 8 + (i
* 68);
1305 if (bfd_mach_o_scan_read_section (abfd
, &seg
->sections
[i
],
1315 bfd_mach_o_scan_read_command (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1317 unsigned char buf
[8];
1319 bfd_seek (abfd
, command
->offset
, SEEK_SET
);
1320 if (bfd_bread ((PTR
) buf
, 8, abfd
) != 8)
1323 command
->type
= (bfd_h_get_32 (abfd
, buf
) & ~BFD_MACH_O_LC_REQ_DYLD
);
1324 command
->type_required
= (bfd_h_get_32 (abfd
, buf
) & BFD_MACH_O_LC_REQ_DYLD
1326 command
->len
= bfd_h_get_32 (abfd
, buf
+ 4);
1328 switch (command
->type
)
1330 case BFD_MACH_O_LC_SEGMENT
:
1331 if (bfd_mach_o_scan_read_segment (abfd
, command
) != 0)
1334 case BFD_MACH_O_LC_SYMTAB
:
1335 if (bfd_mach_o_scan_read_symtab (abfd
, command
) != 0)
1338 case BFD_MACH_O_LC_SYMSEG
:
1340 case BFD_MACH_O_LC_THREAD
:
1341 case BFD_MACH_O_LC_UNIXTHREAD
:
1342 if (bfd_mach_o_scan_read_thread (abfd
, command
) != 0)
1345 case BFD_MACH_O_LC_LOAD_DYLINKER
:
1346 case BFD_MACH_O_LC_ID_DYLINKER
:
1347 if (bfd_mach_o_scan_read_dylinker (abfd
, command
) != 0)
1350 case BFD_MACH_O_LC_LOAD_DYLIB
:
1351 case BFD_MACH_O_LC_ID_DYLIB
:
1352 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB
:
1353 if (bfd_mach_o_scan_read_dylib (abfd
, command
) != 0)
1356 case BFD_MACH_O_LC_PREBOUND_DYLIB
:
1357 if (bfd_mach_o_scan_read_prebound_dylib (abfd
, command
) != 0)
1360 case BFD_MACH_O_LC_LOADFVMLIB
:
1361 case BFD_MACH_O_LC_IDFVMLIB
:
1362 case BFD_MACH_O_LC_IDENT
:
1363 case BFD_MACH_O_LC_FVMFILE
:
1364 case BFD_MACH_O_LC_PREPAGE
:
1365 case BFD_MACH_O_LC_ROUTINES
:
1366 case BFD_MACH_O_LC_SUB_FRAMEWORK
:
1368 case BFD_MACH_O_LC_DYSYMTAB
:
1369 if (bfd_mach_o_scan_read_dysymtab (abfd
, command
) != 0)
1372 case BFD_MACH_O_LC_SUB_UMBRELLA
:
1373 case BFD_MACH_O_LC_SUB_CLIENT
:
1374 case BFD_MACH_O_LC_SUB_LIBRARY
:
1375 case BFD_MACH_O_LC_TWOLEVEL_HINTS
:
1376 case BFD_MACH_O_LC_PREBIND_CKSUM
:
1379 fprintf (stderr
, "unable to read unknown load command 0x%lx\n",
1380 (unsigned long) command
->type
);
1388 bfd_mach_o_flatten_sections (bfd
*abfd
)
1390 bfd_mach_o_data_struct
*mdata
= abfd
->tdata
.mach_o_data
;
1396 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
1398 if (mdata
->commands
[i
].type
== BFD_MACH_O_LC_SEGMENT
)
1400 bfd_mach_o_segment_command
*seg
;
1402 seg
= &mdata
->commands
[i
].command
.segment
;
1403 mdata
->nsects
+= seg
->nsects
;
1407 mdata
->sections
= bfd_alloc (abfd
,
1408 mdata
->nsects
* sizeof (bfd_mach_o_section
*));
1411 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
1413 if (mdata
->commands
[i
].type
== BFD_MACH_O_LC_SEGMENT
)
1415 bfd_mach_o_segment_command
*seg
;
1417 seg
= &mdata
->commands
[i
].command
.segment
;
1418 BFD_ASSERT (csect
+ seg
->nsects
<= mdata
->nsects
);
1420 for (j
= 0; j
< seg
->nsects
; j
++)
1421 mdata
->sections
[csect
++] = &seg
->sections
[j
];
1427 bfd_mach_o_scan_start_address (bfd
*abfd
)
1429 bfd_mach_o_data_struct
*mdata
= abfd
->tdata
.mach_o_data
;
1430 bfd_mach_o_thread_command
*cmd
= NULL
;
1433 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
1435 if ((mdata
->commands
[i
].type
== BFD_MACH_O_LC_THREAD
) ||
1436 (mdata
->commands
[i
].type
== BFD_MACH_O_LC_UNIXTHREAD
))
1439 cmd
= &mdata
->commands
[i
].command
.thread
;
1448 for (i
= 0; i
< cmd
->nflavours
; i
++)
1450 if ((mdata
->header
.cputype
== BFD_MACH_O_CPU_TYPE_I386
)
1451 && (cmd
->flavours
[i
].flavour
1452 == (unsigned long) BFD_MACH_O_i386_THREAD_STATE
))
1454 unsigned char buf
[4];
1456 bfd_seek (abfd
, cmd
->flavours
[i
].offset
+ 40, SEEK_SET
);
1458 if (bfd_bread (buf
, 4, abfd
) != 4)
1461 abfd
->start_address
= bfd_h_get_32 (abfd
, buf
);
1463 else if ((mdata
->header
.cputype
== BFD_MACH_O_CPU_TYPE_POWERPC
)
1464 && (cmd
->flavours
[i
].flavour
== BFD_MACH_O_PPC_THREAD_STATE
))
1466 unsigned char buf
[4];
1468 bfd_seek (abfd
, cmd
->flavours
[i
].offset
+ 0, SEEK_SET
);
1470 if (bfd_bread (buf
, 4, abfd
) != 4)
1473 abfd
->start_address
= bfd_h_get_32 (abfd
, buf
);
1481 bfd_mach_o_scan (bfd
*abfd
,
1482 bfd_mach_o_header
*header
,
1483 bfd_mach_o_data_struct
*mdata
)
1486 enum bfd_architecture cputype
;
1487 unsigned long cpusubtype
;
1489 mdata
->header
= *header
;
1490 mdata
->symbols
= NULL
;
1492 abfd
->flags
= (abfd
->xvec
->object_flags
1493 | (abfd
->flags
& (BFD_IN_MEMORY
| BFD_IO_FUNCS
)));
1494 abfd
->tdata
.mach_o_data
= mdata
;
1496 bfd_mach_o_convert_architecture (header
->cputype
, header
->cpusubtype
,
1497 &cputype
, &cpusubtype
);
1498 if (cputype
== bfd_arch_unknown
)
1500 fprintf (stderr
, "bfd_mach_o_scan: unknown architecture 0x%lx/0x%lx\n",
1501 header
->cputype
, header
->cpusubtype
);
1505 bfd_set_arch_mach (abfd
, cputype
, cpusubtype
);
1507 if (header
->ncmds
!= 0)
1509 mdata
->commands
= bfd_alloc (abfd
, header
->ncmds
* sizeof (bfd_mach_o_load_command
));
1510 if (mdata
->commands
== NULL
)
1513 for (i
= 0; i
< header
->ncmds
; i
++)
1515 bfd_mach_o_load_command
*cur
= &mdata
->commands
[i
];
1521 bfd_mach_o_load_command
*prev
= &mdata
->commands
[i
- 1];
1522 cur
->offset
= prev
->offset
+ prev
->len
;
1525 if (bfd_mach_o_scan_read_command (abfd
, cur
) < 0)
1530 if (bfd_mach_o_scan_start_address (abfd
) < 0)
1533 bfd_mach_o_flatten_sections (abfd
);
1538 bfd_mach_o_mkobject (bfd
*abfd
)
1540 bfd_mach_o_data_struct
*mdata
= NULL
;
1542 mdata
= bfd_alloc (abfd
, sizeof (bfd_mach_o_data_struct
));
1545 abfd
->tdata
.mach_o_data
= mdata
;
1547 mdata
->header
.magic
= 0;
1548 mdata
->header
.cputype
= 0;
1549 mdata
->header
.cpusubtype
= 0;
1550 mdata
->header
.filetype
= 0;
1551 mdata
->header
.ncmds
= 0;
1552 mdata
->header
.sizeofcmds
= 0;
1553 mdata
->header
.flags
= 0;
1554 mdata
->header
.byteorder
= BFD_ENDIAN_UNKNOWN
;
1555 mdata
->commands
= NULL
;
1556 mdata
->nsymbols
= 0;
1557 mdata
->symbols
= NULL
;
1559 mdata
->sections
= NULL
;
1566 bfd_mach_o_object_p (bfd
*abfd
)
1568 struct bfd_preserve preserve
;
1569 bfd_mach_o_header header
;
1571 preserve
.marker
= NULL
;
1572 if (bfd_mach_o_read_header (abfd
, &header
) != 0)
1575 if (! (header
.byteorder
== BFD_ENDIAN_BIG
1576 || header
.byteorder
== BFD_ENDIAN_LITTLE
))
1578 fprintf (stderr
, "unknown header byte-order value 0x%lx\n",
1579 (unsigned long) header
.byteorder
);
1583 if (! ((header
.byteorder
== BFD_ENDIAN_BIG
1584 && abfd
->xvec
->byteorder
== BFD_ENDIAN_BIG
1585 && abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
1586 || (header
.byteorder
== BFD_ENDIAN_LITTLE
1587 && abfd
->xvec
->byteorder
== BFD_ENDIAN_LITTLE
1588 && abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)))
1591 preserve
.marker
= bfd_zalloc (abfd
, sizeof (bfd_mach_o_data_struct
));
1592 if (preserve
.marker
== NULL
1593 || !bfd_preserve_save (abfd
, &preserve
))
1596 if (bfd_mach_o_scan (abfd
, &header
,
1597 (bfd_mach_o_data_struct
*) preserve
.marker
) != 0)
1600 bfd_preserve_finish (abfd
, &preserve
);
1604 bfd_set_error (bfd_error_wrong_format
);
1607 if (preserve
.marker
!= NULL
)
1608 bfd_preserve_restore (abfd
, &preserve
);
1613 bfd_mach_o_core_p (bfd
*abfd
)
1615 struct bfd_preserve preserve
;
1616 bfd_mach_o_header header
;
1618 preserve
.marker
= NULL
;
1619 if (bfd_mach_o_read_header (abfd
, &header
) != 0)
1622 if (! (header
.byteorder
== BFD_ENDIAN_BIG
1623 || header
.byteorder
== BFD_ENDIAN_LITTLE
))
1625 fprintf (stderr
, "unknown header byte-order value 0x%lx\n",
1626 (unsigned long) header
.byteorder
);
1630 if (! ((header
.byteorder
== BFD_ENDIAN_BIG
1631 && abfd
->xvec
->byteorder
== BFD_ENDIAN_BIG
1632 && abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
1633 || (header
.byteorder
== BFD_ENDIAN_LITTLE
1634 && abfd
->xvec
->byteorder
== BFD_ENDIAN_LITTLE
1635 && abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)))
1638 if (header
.filetype
!= BFD_MACH_O_MH_CORE
)
1641 preserve
.marker
= bfd_zalloc (abfd
, sizeof (bfd_mach_o_data_struct
));
1642 if (preserve
.marker
== NULL
1643 || !bfd_preserve_save (abfd
, &preserve
))
1646 if (bfd_mach_o_scan (abfd
, &header
,
1647 (bfd_mach_o_data_struct
*) preserve
.marker
) != 0)
1650 bfd_preserve_finish (abfd
, &preserve
);
1654 bfd_set_error (bfd_error_wrong_format
);
1657 if (preserve
.marker
!= NULL
)
1658 bfd_preserve_restore (abfd
, &preserve
);
1662 typedef struct mach_o_fat_archentry
1664 unsigned long cputype
;
1665 unsigned long cpusubtype
;
1666 unsigned long offset
;
1668 unsigned long align
;
1670 } mach_o_fat_archentry
;
1672 typedef struct mach_o_fat_data_struct
1674 unsigned long magic
;
1675 unsigned long nfat_arch
;
1676 mach_o_fat_archentry
*archentries
;
1677 } mach_o_fat_data_struct
;
1680 bfd_mach_o_archive_p (bfd
*abfd
)
1682 mach_o_fat_data_struct
*adata
= NULL
;
1683 unsigned char buf
[20];
1686 bfd_seek (abfd
, 0, SEEK_SET
);
1687 if (bfd_bread ((PTR
) buf
, 8, abfd
) != 8)
1690 adata
= bfd_alloc (abfd
, sizeof (mach_o_fat_data_struct
));
1694 adata
->magic
= bfd_getb32 (buf
);
1695 adata
->nfat_arch
= bfd_getb32 (buf
+ 4);
1696 if (adata
->magic
!= 0xcafebabe)
1699 adata
->archentries
=
1700 bfd_alloc (abfd
, adata
->nfat_arch
* sizeof (mach_o_fat_archentry
));
1701 if (adata
->archentries
== NULL
)
1704 for (i
= 0; i
< adata
->nfat_arch
; i
++)
1706 bfd_seek (abfd
, 8 + 20 * i
, SEEK_SET
);
1708 if (bfd_bread ((PTR
) buf
, 20, abfd
) != 20)
1710 adata
->archentries
[i
].cputype
= bfd_getb32 (buf
);
1711 adata
->archentries
[i
].cpusubtype
= bfd_getb32 (buf
+ 4);
1712 adata
->archentries
[i
].offset
= bfd_getb32 (buf
+ 8);
1713 adata
->archentries
[i
].size
= bfd_getb32 (buf
+ 12);
1714 adata
->archentries
[i
].align
= bfd_getb32 (buf
+ 16);
1715 adata
->archentries
[i
].abfd
= NULL
;
1718 abfd
->tdata
.mach_o_fat_data
= adata
;
1723 bfd_release (abfd
, adata
);
1724 bfd_set_error (bfd_error_wrong_format
);
1729 bfd_mach_o_openr_next_archived_file (bfd
*archive
, bfd
*prev
)
1731 mach_o_fat_data_struct
*adata
;
1732 mach_o_fat_archentry
*entry
= NULL
;
1735 adata
= (mach_o_fat_data_struct
*) archive
->tdata
.mach_o_fat_data
;
1736 BFD_ASSERT (adata
!= NULL
);
1738 /* Find index of previous entry. */
1740 i
= 0; /* Start at first one. */
1743 for (i
= 0; i
< adata
->nfat_arch
; i
++)
1745 if (adata
->archentries
[i
].abfd
== prev
)
1749 if (i
== adata
->nfat_arch
)
1752 bfd_set_error (bfd_error_bad_value
);
1755 i
++; /* Get next entry. */
1758 if (i
>= adata
->nfat_arch
)
1760 bfd_set_error (bfd_error_no_more_archived_files
);
1764 entry
= &adata
->archentries
[i
];
1765 if (entry
->abfd
== NULL
)
1767 bfd
*nbfd
= _bfd_new_bfd_contained_in (archive
);
1773 nbfd
->origin
= entry
->offset
;
1774 s
= bfd_malloc (strlen (archive
->filename
) + 1);
1777 strcpy (s
, archive
->filename
);
1779 nbfd
->iostream
= NULL
;
1787 bfd_mach_o_lookup_section (bfd
*abfd
,
1789 bfd_mach_o_load_command
**mcommand
,
1790 bfd_mach_o_section
**msection
)
1792 struct mach_o_data_struct
*md
= abfd
->tdata
.mach_o_data
;
1793 unsigned int i
, j
, num
;
1795 bfd_mach_o_load_command
*ncmd
= NULL
;
1796 bfd_mach_o_section
*nsect
= NULL
;
1798 BFD_ASSERT (mcommand
!= NULL
);
1799 BFD_ASSERT (msection
!= NULL
);
1802 for (i
= 0; i
< md
->header
.ncmds
; i
++)
1804 struct bfd_mach_o_load_command
*cmd
= &md
->commands
[i
];
1805 struct bfd_mach_o_segment_command
*seg
= NULL
;
1807 if (cmd
->type
!= BFD_MACH_O_LC_SEGMENT
)
1809 seg
= &cmd
->command
.segment
;
1811 if (seg
->segment
== section
)
1818 for (j
= 0; j
< seg
->nsects
; j
++)
1820 struct bfd_mach_o_section
*sect
= &seg
->sections
[j
];
1822 if (sect
->bfdsection
== section
)
1837 bfd_mach_o_lookup_command (bfd
*abfd
,
1838 bfd_mach_o_load_command_type type
,
1839 bfd_mach_o_load_command
**mcommand
)
1841 struct mach_o_data_struct
*md
= NULL
;
1842 bfd_mach_o_load_command
*ncmd
= NULL
;
1843 unsigned int i
, num
;
1845 md
= abfd
->tdata
.mach_o_data
;
1847 BFD_ASSERT (md
!= NULL
);
1848 BFD_ASSERT (mcommand
!= NULL
);
1851 for (i
= 0; i
< md
->header
.ncmds
; i
++)
1853 struct bfd_mach_o_load_command
*cmd
= &md
->commands
[i
];
1855 if (cmd
->type
!= type
)
1868 bfd_mach_o_stack_addr (enum bfd_mach_o_cpu_type type
)
1872 case BFD_MACH_O_CPU_TYPE_MC680x0
:
1874 case BFD_MACH_O_CPU_TYPE_MC88000
:
1876 case BFD_MACH_O_CPU_TYPE_POWERPC
:
1878 case BFD_MACH_O_CPU_TYPE_I386
:
1880 case BFD_MACH_O_CPU_TYPE_SPARC
:
1882 case BFD_MACH_O_CPU_TYPE_I860
:
1884 case BFD_MACH_O_CPU_TYPE_HPPA
:
1885 return 0xc0000000 - 0x04000000;
1892 bfd_mach_o_core_fetch_environment (bfd
*abfd
,
1893 unsigned char **rbuf
,
1896 bfd_mach_o_data_struct
*mdata
= abfd
->tdata
.mach_o_data
;
1897 unsigned long stackaddr
= bfd_mach_o_stack_addr (mdata
->header
.cputype
);
1900 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
1902 bfd_mach_o_load_command
*cur
= &mdata
->commands
[i
];
1903 bfd_mach_o_segment_command
*seg
= NULL
;
1905 if (cur
->type
!= BFD_MACH_O_LC_SEGMENT
)
1908 seg
= &cur
->command
.segment
;
1910 if ((seg
->vmaddr
+ seg
->vmsize
) == stackaddr
)
1912 unsigned long start
= seg
->fileoff
;
1913 unsigned long end
= seg
->fileoff
+ seg
->filesize
;
1914 unsigned char *buf
= bfd_malloc (1024);
1915 unsigned long size
= 1024;
1919 bfd_size_type nread
= 0;
1920 unsigned long offset
;
1921 int found_nonnull
= 0;
1923 if (size
> (end
- start
))
1924 size
= (end
- start
);
1926 buf
= bfd_realloc_or_free (buf
, size
);
1930 bfd_seek (abfd
, end
- size
, SEEK_SET
);
1931 nread
= bfd_bread (buf
, size
, abfd
);
1939 for (offset
= 4; offset
<= size
; offset
+= 4)
1943 val
= *((unsigned long *) (buf
+ size
- offset
));
1944 if (! found_nonnull
)
1949 else if (val
== 0x0)
1951 unsigned long bottom
;
1954 bottom
= seg
->fileoff
+ seg
->filesize
- offset
;
1955 top
= seg
->fileoff
+ seg
->filesize
- 4;
1956 *rbuf
= bfd_malloc (top
- bottom
);
1957 *rlen
= top
- bottom
;
1959 memcpy (*rbuf
, buf
+ size
- *rlen
, *rlen
);
1965 if (size
== (end
- start
))
1979 bfd_mach_o_core_file_failing_command (bfd
*abfd
)
1981 unsigned char *buf
= NULL
;
1982 unsigned int len
= 0;
1985 ret
= bfd_mach_o_core_fetch_environment (abfd
, &buf
, &len
);
1989 return (char *) buf
;
1993 bfd_mach_o_core_file_failing_signal (bfd
*abfd ATTRIBUTE_UNUSED
)
1998 #define TARGET_NAME mach_o_be_vec
1999 #define TARGET_STRING "mach-o-be"
2000 #define TARGET_BIG_ENDIAN 1
2001 #define TARGET_ARCHIVE 0
2003 #include "mach-o-target.c"
2006 #undef TARGET_STRING
2007 #undef TARGET_BIG_ENDIAN
2008 #undef TARGET_ARCHIVE
2010 #define TARGET_NAME mach_o_le_vec
2011 #define TARGET_STRING "mach-o-le"
2012 #define TARGET_BIG_ENDIAN 0
2013 #define TARGET_ARCHIVE 0
2015 #include "mach-o-target.c"
2018 #undef TARGET_STRING
2019 #undef TARGET_BIG_ENDIAN
2020 #undef TARGET_ARCHIVE
2022 #define TARGET_NAME mach_o_fat_vec
2023 #define TARGET_STRING "mach-o-fat"
2024 #define TARGET_BIG_ENDIAN 1
2025 #define TARGET_ARCHIVE 1
2027 #include "mach-o-target.c"
2030 #undef TARGET_STRING
2031 #undef TARGET_BIG_ENDIAN
2032 #undef TARGET_ARCHIVE