1 /* Mach-O support for BFD.
2 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005
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 2 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 #include "libiberty.h"
29 #define BFD_IO_FUNCS 0
32 #define bfd_mach_o_mkarchive _bfd_noarchive_mkarchive
33 #define bfd_mach_o_read_ar_hdr _bfd_noarchive_read_ar_hdr
34 #define bfd_mach_o_slurp_armap _bfd_noarchive_slurp_armap
35 #define bfd_mach_o_slurp_extended_name_table _bfd_noarchive_slurp_extended_name_table
36 #define bfd_mach_o_construct_extended_name_table _bfd_noarchive_construct_extended_name_table
37 #define bfd_mach_o_truncate_arname _bfd_noarchive_truncate_arname
38 #define bfd_mach_o_write_armap _bfd_noarchive_write_armap
39 #define bfd_mach_o_get_elt_at_index _bfd_noarchive_get_elt_at_index
40 #define bfd_mach_o_generic_stat_arch_elt _bfd_noarchive_generic_stat_arch_elt
41 #define bfd_mach_o_update_armap_timestamp _bfd_noarchive_update_armap_timestamp
42 #define bfd_mach_o_close_and_cleanup _bfd_generic_close_and_cleanup
43 #define bfd_mach_o_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
44 #define bfd_mach_o_new_section_hook _bfd_generic_new_section_hook
45 #define bfd_mach_o_get_section_contents_in_window _bfd_generic_get_section_contents_in_window
46 #define bfd_mach_o_bfd_is_local_label_name _bfd_nosymbols_bfd_is_local_label_name
47 #define bfd_mach_o_bfd_is_target_special_symbol ((bfd_boolean (*) (bfd *, asymbol *)) bfd_false)
48 #define bfd_mach_o_bfd_is_local_label_name _bfd_nosymbols_bfd_is_local_label_name
49 #define bfd_mach_o_get_lineno _bfd_nosymbols_get_lineno
50 #define bfd_mach_o_find_nearest_line _bfd_nosymbols_find_nearest_line
51 #define bfd_mach_o_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
52 #define bfd_mach_o_read_minisymbols _bfd_generic_read_minisymbols
53 #define bfd_mach_o_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
54 #define bfd_mach_o_get_reloc_upper_bound _bfd_norelocs_get_reloc_upper_bound
55 #define bfd_mach_o_canonicalize_reloc _bfd_norelocs_canonicalize_reloc
56 #define bfd_mach_o_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
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_bfd_print_private_bfd_data _bfd_generic_bfd_print_private_bfd_data
69 #define bfd_mach_o_get_section_contents _bfd_generic_get_section_contents
70 #define bfd_mach_o_set_section_contents _bfd_generic_set_section_contents
71 #define bfd_mach_o_bfd_gc_sections bfd_generic_gc_sections
72 #define bfd_mach_o_bfd_merge_sections bfd_generic_merge_sections
73 #define bfd_mach_o_bfd_is_group_section bfd_generic_is_group_section
74 #define bfd_mach_o_bfd_discard_group bfd_generic_discard_group
75 #define bfd_mach_o_section_already_linked _bfd_generic_section_already_linked
76 #define bfd_mach_o_bfd_copy_private_header_data _bfd_generic_bfd_copy_private_header_data
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",
568 bfd_mach_o_sizeof_headers (bfd
*a ATTRIBUTE_UNUSED
,
569 bfd_boolean b 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
;
636 snamelen
= strlen (prefix
) + 1
637 + strlen (section
->segname
) + 1
638 + strlen (section
->sectname
) + 1;
640 sname
= bfd_alloc (abfd
, snamelen
);
643 sprintf (sname
, "%s.%s.%s", prefix
, section
->segname
, section
->sectname
);
645 bfdsec
= bfd_make_section_anyway (abfd
, sname
);
649 bfdsec
->vma
= section
->addr
;
650 bfdsec
->lma
= section
->addr
;
651 bfdsec
->size
= section
->size
;
652 bfdsec
->filepos
= section
->offset
;
653 bfdsec
->alignment_power
= section
->align
;
655 if (section
->flags
& BFD_MACH_O_S_ZEROFILL
)
656 bfdsec
->flags
= SEC_ALLOC
;
658 bfdsec
->flags
= SEC_HAS_CONTENTS
| SEC_LOAD
| SEC_ALLOC
| SEC_CODE
;
664 bfd_mach_o_scan_read_section (bfd
*abfd
,
665 bfd_mach_o_section
*section
,
668 unsigned char buf
[68];
670 bfd_seek (abfd
, offset
, SEEK_SET
);
671 if (bfd_bread ((PTR
) buf
, 68, abfd
) != 68)
674 memcpy (section
->sectname
, buf
, 16);
675 section
->sectname
[16] = '\0';
676 memcpy (section
->segname
, buf
+ 16, 16);
677 section
->segname
[16] = '\0';
678 section
->addr
= bfd_h_get_32 (abfd
, buf
+ 32);
679 section
->size
= bfd_h_get_32 (abfd
, buf
+ 36);
680 section
->offset
= bfd_h_get_32 (abfd
, buf
+ 40);
681 section
->align
= bfd_h_get_32 (abfd
, buf
+ 44);
682 section
->reloff
= bfd_h_get_32 (abfd
, buf
+ 48);
683 section
->nreloc
= bfd_h_get_32 (abfd
, buf
+ 52);
684 section
->flags
= bfd_h_get_32 (abfd
, buf
+ 56);
685 section
->reserved1
= bfd_h_get_32 (abfd
, buf
+ 60);
686 section
->reserved2
= bfd_h_get_32 (abfd
, buf
+ 64);
687 section
->bfdsection
= bfd_mach_o_make_bfd_section (abfd
, section
);
689 if (section
->bfdsection
== NULL
)
696 bfd_mach_o_scan_read_symtab_symbol (bfd
*abfd
,
697 bfd_mach_o_symtab_command
*sym
,
701 bfd_mach_o_data_struct
*mdata
= abfd
->tdata
.mach_o_data
;
702 bfd_vma symoff
= sym
->symoff
+ (i
* 12);
703 unsigned char buf
[12];
704 unsigned char type
= -1;
705 unsigned char section
= -1;
707 unsigned long value
= -1;
708 unsigned long stroff
= -1;
709 unsigned int symtype
= -1;
711 BFD_ASSERT (sym
->strtab
!= NULL
);
713 bfd_seek (abfd
, symoff
, SEEK_SET
);
714 if (bfd_bread ((PTR
) buf
, 12, abfd
) != 12)
716 fprintf (stderr
, "bfd_mach_o_scan_read_symtab_symbol: unable to read %d bytes at %lu\n",
717 12, (unsigned long) symoff
);
721 stroff
= bfd_h_get_32 (abfd
, buf
);
722 type
= bfd_h_get_8 (abfd
, buf
+ 4);
723 symtype
= (type
& 0x0e);
724 section
= bfd_h_get_8 (abfd
, buf
+ 5) - 1;
725 desc
= bfd_h_get_16 (abfd
, buf
+ 6);
726 value
= bfd_h_get_32 (abfd
, buf
+ 8);
728 if (stroff
>= sym
->strsize
)
730 fprintf (stderr
, "bfd_mach_o_scan_read_symtab_symbol: symbol name out of range (%lu >= %lu)\n",
731 (unsigned long) stroff
, (unsigned long) sym
->strsize
);
736 s
->name
= sym
->strtab
+ stroff
;
738 s
->udata
.i
= (type
<< 24) | (section
<< 16) | desc
;
741 if (type
& BFD_MACH_O_N_STAB
)
743 s
->flags
|= BSF_DEBUGGING
;
744 s
->section
= bfd_und_section_ptr
;
748 if (type
& BFD_MACH_O_N_PEXT
)
750 type
&= ~BFD_MACH_O_N_PEXT
;
751 s
->flags
|= BSF_GLOBAL
;
754 if (type
& BFD_MACH_O_N_EXT
)
756 type
&= ~BFD_MACH_O_N_EXT
;
757 s
->flags
|= BSF_GLOBAL
;
762 case BFD_MACH_O_N_UNDF
:
763 s
->section
= bfd_und_section_ptr
;
765 case BFD_MACH_O_N_PBUD
:
766 s
->section
= bfd_und_section_ptr
;
768 case BFD_MACH_O_N_ABS
:
769 s
->section
= bfd_abs_section_ptr
;
771 case BFD_MACH_O_N_SECT
:
772 if ((section
> 0) && (section
<= mdata
->nsects
))
774 s
->section
= mdata
->sections
[section
- 1]->bfdsection
;
775 s
->value
= s
->value
- mdata
->sections
[section
- 1]->addr
;
779 /* Mach-O uses 0 to mean "no section"; not an error. */
782 fprintf (stderr
, "bfd_mach_o_scan_read_symtab_symbol: "
783 "symbol \"%s\" specified invalid section %d (max %lu): setting to undefined\n",
784 s
->name
, section
, mdata
->nsects
);
786 s
->section
= bfd_und_section_ptr
;
789 case BFD_MACH_O_N_INDR
:
790 fprintf (stderr
, "bfd_mach_o_scan_read_symtab_symbol: "
791 "symbol \"%s\" is unsupported 'indirect' reference: setting to undefined\n",
793 s
->section
= bfd_und_section_ptr
;
796 fprintf (stderr
, "bfd_mach_o_scan_read_symtab_symbol: "
797 "symbol \"%s\" specified invalid type field 0x%x: setting to undefined\n",
799 s
->section
= bfd_und_section_ptr
;
808 bfd_mach_o_scan_read_symtab_strtab (bfd
*abfd
,
809 bfd_mach_o_symtab_command
*sym
)
811 BFD_ASSERT (sym
->strtab
== NULL
);
813 if (abfd
->flags
& BFD_IN_MEMORY
)
815 struct bfd_in_memory
*b
;
817 b
= (struct bfd_in_memory
*) abfd
->iostream
;
819 if ((sym
->stroff
+ sym
->strsize
) > b
->size
)
821 bfd_set_error (bfd_error_file_truncated
);
824 sym
->strtab
= (char *) b
->buffer
+ sym
->stroff
;
828 sym
->strtab
= bfd_alloc (abfd
, sym
->strsize
);
829 if (sym
->strtab
== NULL
)
832 bfd_seek (abfd
, sym
->stroff
, SEEK_SET
);
833 if (bfd_bread ((PTR
) sym
->strtab
, sym
->strsize
, abfd
) != sym
->strsize
)
835 fprintf (stderr
, "bfd_mach_o_scan_read_symtab_strtab: unable to read %lu bytes at %lu\n",
836 sym
->strsize
, sym
->stroff
);
844 bfd_mach_o_scan_read_symtab_symbols (bfd
*abfd
,
845 bfd_mach_o_symtab_command
*sym
)
850 BFD_ASSERT (sym
->symbols
== NULL
);
851 sym
->symbols
= bfd_alloc (abfd
, sym
->nsyms
* sizeof (asymbol
));
853 if (sym
->symbols
== NULL
)
855 fprintf (stderr
, "bfd_mach_o_scan_read_symtab_symbols: unable to allocate memory for symbols\n");
859 ret
= bfd_mach_o_scan_read_symtab_strtab (abfd
, sym
);
863 for (i
= 0; i
< sym
->nsyms
; i
++)
865 ret
= bfd_mach_o_scan_read_symtab_symbol (abfd
, sym
, &sym
->symbols
[i
], i
);
874 bfd_mach_o_scan_read_dysymtab_symbol (bfd
*abfd
,
875 bfd_mach_o_dysymtab_command
*dysym
,
876 bfd_mach_o_symtab_command
*sym
,
880 unsigned long isymoff
= dysym
->indirectsymoff
+ (i
* 4);
881 unsigned long symindex
;
882 unsigned char buf
[4];
884 BFD_ASSERT (i
< dysym
->nindirectsyms
);
886 bfd_seek (abfd
, isymoff
, SEEK_SET
);
887 if (bfd_bread ((PTR
) buf
, 4, abfd
) != 4)
889 fprintf (stderr
, "bfd_mach_o_scan_read_dysymtab_symbol: unable to read %lu bytes at %lu\n",
890 (unsigned long) 4, isymoff
);
893 symindex
= bfd_h_get_32 (abfd
, buf
);
895 return bfd_mach_o_scan_read_symtab_symbol (abfd
, sym
, s
, symindex
);
899 bfd_mach_o_i386_flavour_string (unsigned int flavour
)
901 switch ((int) flavour
)
903 case BFD_MACH_O_i386_NEW_THREAD_STATE
: return "i386_NEW_THREAD_STATE";
904 case BFD_MACH_O_i386_FLOAT_STATE
: return "i386_FLOAT_STATE";
905 case BFD_MACH_O_i386_ISA_PORT_MAP_STATE
: return "i386_ISA_PORT_MAP_STATE";
906 case BFD_MACH_O_i386_V86_ASSIST_STATE
: return "i386_V86_ASSIST_STATE";
907 case BFD_MACH_O_i386_REGS_SEGS_STATE
: return "i386_REGS_SEGS_STATE";
908 case BFD_MACH_O_i386_THREAD_SYSCALL_STATE
: return "i386_THREAD_SYSCALL_STATE";
909 case BFD_MACH_O_i386_THREAD_STATE_NONE
: return "i386_THREAD_STATE_NONE";
910 case BFD_MACH_O_i386_SAVED_STATE
: return "i386_SAVED_STATE";
911 case BFD_MACH_O_i386_THREAD_STATE
: return "i386_THREAD_STATE";
912 case BFD_MACH_O_i386_THREAD_FPSTATE
: return "i386_THREAD_FPSTATE";
913 case BFD_MACH_O_i386_THREAD_EXCEPTSTATE
: return "i386_THREAD_EXCEPTSTATE";
914 case BFD_MACH_O_i386_THREAD_CTHREADSTATE
: return "i386_THREAD_CTHREADSTATE";
915 default: return "UNKNOWN";
920 bfd_mach_o_ppc_flavour_string (unsigned int flavour
)
922 switch ((int) flavour
)
924 case BFD_MACH_O_PPC_THREAD_STATE
: return "PPC_THREAD_STATE";
925 case BFD_MACH_O_PPC_FLOAT_STATE
: return "PPC_FLOAT_STATE";
926 case BFD_MACH_O_PPC_EXCEPTION_STATE
: return "PPC_EXCEPTION_STATE";
927 case BFD_MACH_O_PPC_VECTOR_STATE
: return "PPC_VECTOR_STATE";
928 default: return "UNKNOWN";
933 bfd_mach_o_scan_read_dylinker (bfd
*abfd
,
934 bfd_mach_o_load_command
*command
)
936 bfd_mach_o_dylinker_command
*cmd
= &command
->command
.dylinker
;
937 unsigned char buf
[4];
938 unsigned int nameoff
;
943 BFD_ASSERT ((command
->type
== BFD_MACH_O_LC_ID_DYLINKER
)
944 || (command
->type
== BFD_MACH_O_LC_LOAD_DYLINKER
));
946 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
947 if (bfd_bread ((PTR
) buf
, 4, abfd
) != 4)
950 nameoff
= bfd_h_get_32 (abfd
, buf
+ 0);
952 cmd
->name_offset
= command
->offset
+ nameoff
;
953 cmd
->name_len
= command
->len
- nameoff
;
955 if (command
->type
== BFD_MACH_O_LC_LOAD_DYLINKER
)
956 prefix
= "LC_LOAD_DYLINKER";
957 else if (command
->type
== BFD_MACH_O_LC_ID_DYLINKER
)
958 prefix
= "LC_ID_DYLINKER";
962 sname
= bfd_alloc (abfd
, strlen (prefix
) + 1);
965 strcpy (sname
, prefix
);
967 bfdsec
= bfd_make_section_anyway (abfd
, sname
);
973 bfdsec
->size
= command
->len
- 8;
974 bfdsec
->filepos
= command
->offset
+ 8;
975 bfdsec
->alignment_power
= 0;
976 bfdsec
->flags
= SEC_HAS_CONTENTS
;
978 cmd
->section
= bfdsec
;
984 bfd_mach_o_scan_read_dylib (bfd
*abfd
, bfd_mach_o_load_command
*command
)
986 bfd_mach_o_dylib_command
*cmd
= &command
->command
.dylib
;
987 unsigned char buf
[16];
988 unsigned int nameoff
;
993 BFD_ASSERT ((command
->type
== BFD_MACH_O_LC_ID_DYLIB
)
994 || (command
->type
== BFD_MACH_O_LC_LOAD_DYLIB
)
995 || (command
->type
== BFD_MACH_O_LC_LOAD_WEAK_DYLIB
));
997 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
998 if (bfd_bread ((PTR
) buf
, 16, abfd
) != 16)
1001 nameoff
= bfd_h_get_32 (abfd
, buf
+ 0);
1002 cmd
->timestamp
= bfd_h_get_32 (abfd
, buf
+ 4);
1003 cmd
->current_version
= bfd_h_get_32 (abfd
, buf
+ 8);
1004 cmd
->compatibility_version
= bfd_h_get_32 (abfd
, buf
+ 12);
1006 cmd
->name_offset
= command
->offset
+ nameoff
;
1007 cmd
->name_len
= command
->len
- nameoff
;
1009 if (command
->type
== BFD_MACH_O_LC_LOAD_DYLIB
)
1010 prefix
= "LC_LOAD_DYLIB";
1011 else if (command
->type
== BFD_MACH_O_LC_LOAD_WEAK_DYLIB
)
1012 prefix
= "LC_LOAD_WEAK_DYLIB";
1013 else if (command
->type
== BFD_MACH_O_LC_ID_DYLIB
)
1014 prefix
= "LC_ID_DYLIB";
1018 sname
= bfd_alloc (abfd
, strlen (prefix
) + 1);
1021 strcpy (sname
, prefix
);
1023 bfdsec
= bfd_make_section_anyway (abfd
, sname
);
1029 bfdsec
->size
= command
->len
- 8;
1030 bfdsec
->filepos
= command
->offset
+ 8;
1031 bfdsec
->alignment_power
= 0;
1032 bfdsec
->flags
= SEC_HAS_CONTENTS
;
1034 cmd
->section
= bfdsec
;
1040 bfd_mach_o_scan_read_prebound_dylib (bfd
*abfd ATTRIBUTE_UNUSED
,
1041 bfd_mach_o_load_command
*command ATTRIBUTE_UNUSED
)
1043 /* bfd_mach_o_prebound_dylib_command *cmd = &command->command.prebound_dylib; */
1045 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_PREBOUND_DYLIB
);
1050 bfd_mach_o_scan_read_thread (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1052 bfd_mach_o_data_struct
*mdata
= NULL
;
1053 bfd_mach_o_thread_command
*cmd
= &command
->command
.thread
;
1054 unsigned char buf
[8];
1056 unsigned int nflavours
;
1059 BFD_ASSERT ((command
->type
== BFD_MACH_O_LC_THREAD
)
1060 || (command
->type
== BFD_MACH_O_LC_UNIXTHREAD
));
1062 BFD_ASSERT (bfd_mach_o_valid (abfd
));
1063 mdata
= abfd
->tdata
.mach_o_data
;
1067 while (offset
!= command
->len
)
1069 if (offset
>= command
->len
)
1072 bfd_seek (abfd
, command
->offset
+ offset
, SEEK_SET
);
1074 if (bfd_bread ((PTR
) buf
, 8, abfd
) != 8)
1077 offset
+= 8 + bfd_h_get_32 (abfd
, buf
+ 4) * 4;
1081 cmd
->flavours
= bfd_alloc (abfd
, nflavours
* sizeof (bfd_mach_o_thread_flavour
));
1082 if (cmd
->flavours
== NULL
)
1084 cmd
->nflavours
= nflavours
;
1088 while (offset
!= command
->len
)
1090 if (offset
>= command
->len
)
1093 if (nflavours
>= cmd
->nflavours
)
1096 bfd_seek (abfd
, command
->offset
+ offset
, SEEK_SET
);
1098 if (bfd_bread ((PTR
) buf
, 8, abfd
) != 8)
1101 cmd
->flavours
[nflavours
].flavour
= bfd_h_get_32 (abfd
, buf
);
1102 cmd
->flavours
[nflavours
].offset
= command
->offset
+ offset
+ 8;
1103 cmd
->flavours
[nflavours
].size
= bfd_h_get_32 (abfd
, buf
+ 4) * 4;
1104 offset
+= cmd
->flavours
[nflavours
].size
+ 8;
1108 for (i
= 0; i
< nflavours
; i
++)
1111 unsigned int snamelen
;
1113 const char *flavourstr
;
1114 const char *prefix
= "LC_THREAD";
1117 switch (mdata
->header
.cputype
)
1119 case BFD_MACH_O_CPU_TYPE_POWERPC
:
1120 flavourstr
= bfd_mach_o_ppc_flavour_string (cmd
->flavours
[i
].flavour
);
1122 case BFD_MACH_O_CPU_TYPE_I386
:
1123 flavourstr
= bfd_mach_o_i386_flavour_string (cmd
->flavours
[i
].flavour
);
1126 flavourstr
= "UNKNOWN_ARCHITECTURE";
1130 snamelen
= strlen (prefix
) + 1 + 20 + 1 + strlen (flavourstr
) + 1;
1131 sname
= bfd_alloc (abfd
, snamelen
);
1137 sprintf (sname
, "%s.%s.%u", prefix
, flavourstr
, j
);
1138 if (bfd_get_section_by_name (abfd
, sname
) == NULL
)
1143 bfdsec
= bfd_make_section (abfd
, sname
);
1147 bfdsec
->size
= cmd
->flavours
[i
].size
;
1148 bfdsec
->filepos
= cmd
->flavours
[i
].offset
;
1149 bfdsec
->alignment_power
= 0x0;
1150 bfdsec
->flags
= SEC_HAS_CONTENTS
;
1152 cmd
->section
= bfdsec
;
1159 bfd_mach_o_scan_read_dysymtab (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1161 bfd_mach_o_dysymtab_command
*seg
= &command
->command
.dysymtab
;
1162 unsigned char buf
[72];
1164 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_DYSYMTAB
);
1166 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
1167 if (bfd_bread ((PTR
) buf
, 72, abfd
) != 72)
1170 seg
->ilocalsym
= bfd_h_get_32 (abfd
, buf
+ 0);
1171 seg
->nlocalsym
= bfd_h_get_32 (abfd
, buf
+ 4);
1172 seg
->iextdefsym
= bfd_h_get_32 (abfd
, buf
+ 8);
1173 seg
->nextdefsym
= bfd_h_get_32 (abfd
, buf
+ 12);
1174 seg
->iundefsym
= bfd_h_get_32 (abfd
, buf
+ 16);
1175 seg
->nundefsym
= bfd_h_get_32 (abfd
, buf
+ 20);
1176 seg
->tocoff
= bfd_h_get_32 (abfd
, buf
+ 24);
1177 seg
->ntoc
= bfd_h_get_32 (abfd
, buf
+ 28);
1178 seg
->modtaboff
= bfd_h_get_32 (abfd
, buf
+ 32);
1179 seg
->nmodtab
= bfd_h_get_32 (abfd
, buf
+ 36);
1180 seg
->extrefsymoff
= bfd_h_get_32 (abfd
, buf
+ 40);
1181 seg
->nextrefsyms
= bfd_h_get_32 (abfd
, buf
+ 44);
1182 seg
->indirectsymoff
= bfd_h_get_32 (abfd
, buf
+ 48);
1183 seg
->nindirectsyms
= bfd_h_get_32 (abfd
, buf
+ 52);
1184 seg
->extreloff
= bfd_h_get_32 (abfd
, buf
+ 56);
1185 seg
->nextrel
= bfd_h_get_32 (abfd
, buf
+ 60);
1186 seg
->locreloff
= bfd_h_get_32 (abfd
, buf
+ 64);
1187 seg
->nlocrel
= bfd_h_get_32 (abfd
, buf
+ 68);
1193 bfd_mach_o_scan_read_symtab (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1195 bfd_mach_o_symtab_command
*seg
= &command
->command
.symtab
;
1196 unsigned char buf
[16];
1199 const char *prefix
= "LC_SYMTAB.stabs";
1201 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SYMTAB
);
1203 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
1204 if (bfd_bread ((PTR
) buf
, 16, abfd
) != 16)
1207 seg
->symoff
= bfd_h_get_32 (abfd
, buf
);
1208 seg
->nsyms
= bfd_h_get_32 (abfd
, buf
+ 4);
1209 seg
->stroff
= bfd_h_get_32 (abfd
, buf
+ 8);
1210 seg
->strsize
= bfd_h_get_32 (abfd
, buf
+ 12);
1211 seg
->symbols
= NULL
;
1214 sname
= bfd_alloc (abfd
, strlen (prefix
) + 1);
1217 strcpy (sname
, prefix
);
1219 bfdsec
= bfd_make_section_anyway (abfd
, sname
);
1225 bfdsec
->size
= seg
->nsyms
* 12;
1226 bfdsec
->filepos
= seg
->symoff
;
1227 bfdsec
->alignment_power
= 0;
1228 bfdsec
->flags
= SEC_HAS_CONTENTS
;
1230 seg
->stabs_segment
= bfdsec
;
1232 prefix
= "LC_SYMTAB.stabstr";
1233 sname
= bfd_alloc (abfd
, strlen (prefix
) + 1);
1236 strcpy (sname
, prefix
);
1238 bfdsec
= bfd_make_section_anyway (abfd
, sname
);
1244 bfdsec
->size
= seg
->strsize
;
1245 bfdsec
->filepos
= seg
->stroff
;
1246 bfdsec
->alignment_power
= 0;
1247 bfdsec
->flags
= SEC_HAS_CONTENTS
;
1249 seg
->stabstr_segment
= bfdsec
;
1255 bfd_mach_o_scan_read_segment (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1257 unsigned char buf
[48];
1258 bfd_mach_o_segment_command
*seg
= &command
->command
.segment
;
1262 const char *prefix
= "LC_SEGMENT";
1263 unsigned int snamelen
;
1265 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SEGMENT
);
1267 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
1268 if (bfd_bread ((PTR
) buf
, 48, abfd
) != 48)
1271 memcpy (seg
->segname
, buf
, 16);
1272 seg
->vmaddr
= bfd_h_get_32 (abfd
, buf
+ 16);
1273 seg
->vmsize
= bfd_h_get_32 (abfd
, buf
+ 20);
1274 seg
->fileoff
= bfd_h_get_32 (abfd
, buf
+ 24);
1275 seg
->filesize
= bfd_h_get_32 (abfd
, buf
+ 28);
1276 /* seg->maxprot = bfd_h_get_32 (abfd, buf + 32); */
1277 /* seg->initprot = bfd_h_get_32 (abfd, buf + 36); */
1278 seg
->nsects
= bfd_h_get_32 (abfd
, buf
+ 40);
1279 seg
->flags
= bfd_h_get_32 (abfd
, buf
+ 44);
1281 snamelen
= strlen (prefix
) + 1 + strlen (seg
->segname
) + 1;
1282 sname
= bfd_alloc (abfd
, snamelen
);
1285 sprintf (sname
, "%s.%s", prefix
, seg
->segname
);
1287 bfdsec
= bfd_make_section_anyway (abfd
, sname
);
1291 bfdsec
->vma
= seg
->vmaddr
;
1292 bfdsec
->lma
= seg
->vmaddr
;
1293 bfdsec
->size
= seg
->filesize
;
1294 bfdsec
->filepos
= seg
->fileoff
;
1295 bfdsec
->alignment_power
= 0x0;
1296 bfdsec
->flags
= SEC_HAS_CONTENTS
| SEC_LOAD
| SEC_ALLOC
| SEC_CODE
;
1298 seg
->segment
= bfdsec
;
1300 if (seg
->nsects
!= 0)
1302 seg
->sections
= bfd_alloc (abfd
, seg
->nsects
* sizeof (bfd_mach_o_section
));
1303 if (seg
->sections
== NULL
)
1306 for (i
= 0; i
< seg
->nsects
; i
++)
1308 bfd_vma segoff
= command
->offset
+ 48 + 8 + (i
* 68);
1310 if (bfd_mach_o_scan_read_section (abfd
, &seg
->sections
[i
],
1320 bfd_mach_o_scan_read_command (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1322 unsigned char buf
[8];
1324 bfd_seek (abfd
, command
->offset
, SEEK_SET
);
1325 if (bfd_bread ((PTR
) buf
, 8, abfd
) != 8)
1328 command
->type
= (bfd_h_get_32 (abfd
, buf
) & ~BFD_MACH_O_LC_REQ_DYLD
);
1329 command
->type_required
= (bfd_h_get_32 (abfd
, buf
) & BFD_MACH_O_LC_REQ_DYLD
1331 command
->len
= bfd_h_get_32 (abfd
, buf
+ 4);
1333 switch (command
->type
)
1335 case BFD_MACH_O_LC_SEGMENT
:
1336 if (bfd_mach_o_scan_read_segment (abfd
, command
) != 0)
1339 case BFD_MACH_O_LC_SYMTAB
:
1340 if (bfd_mach_o_scan_read_symtab (abfd
, command
) != 0)
1343 case BFD_MACH_O_LC_SYMSEG
:
1345 case BFD_MACH_O_LC_THREAD
:
1346 case BFD_MACH_O_LC_UNIXTHREAD
:
1347 if (bfd_mach_o_scan_read_thread (abfd
, command
) != 0)
1350 case BFD_MACH_O_LC_LOAD_DYLINKER
:
1351 case BFD_MACH_O_LC_ID_DYLINKER
:
1352 if (bfd_mach_o_scan_read_dylinker (abfd
, command
) != 0)
1355 case BFD_MACH_O_LC_LOAD_DYLIB
:
1356 case BFD_MACH_O_LC_ID_DYLIB
:
1357 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB
:
1358 if (bfd_mach_o_scan_read_dylib (abfd
, command
) != 0)
1361 case BFD_MACH_O_LC_PREBOUND_DYLIB
:
1362 if (bfd_mach_o_scan_read_prebound_dylib (abfd
, command
) != 0)
1365 case BFD_MACH_O_LC_LOADFVMLIB
:
1366 case BFD_MACH_O_LC_IDFVMLIB
:
1367 case BFD_MACH_O_LC_IDENT
:
1368 case BFD_MACH_O_LC_FVMFILE
:
1369 case BFD_MACH_O_LC_PREPAGE
:
1370 case BFD_MACH_O_LC_ROUTINES
:
1371 case BFD_MACH_O_LC_SUB_FRAMEWORK
:
1373 case BFD_MACH_O_LC_DYSYMTAB
:
1374 if (bfd_mach_o_scan_read_dysymtab (abfd
, command
) != 0)
1377 case BFD_MACH_O_LC_SUB_UMBRELLA
:
1378 case BFD_MACH_O_LC_SUB_CLIENT
:
1379 case BFD_MACH_O_LC_SUB_LIBRARY
:
1380 case BFD_MACH_O_LC_TWOLEVEL_HINTS
:
1381 case BFD_MACH_O_LC_PREBIND_CKSUM
:
1384 fprintf (stderr
, "unable to read unknown load command 0x%lx\n",
1385 (unsigned long) command
->type
);
1393 bfd_mach_o_flatten_sections (bfd
*abfd
)
1395 bfd_mach_o_data_struct
*mdata
= abfd
->tdata
.mach_o_data
;
1401 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
1403 if (mdata
->commands
[i
].type
== BFD_MACH_O_LC_SEGMENT
)
1405 bfd_mach_o_segment_command
*seg
;
1407 seg
= &mdata
->commands
[i
].command
.segment
;
1408 mdata
->nsects
+= seg
->nsects
;
1412 mdata
->sections
= bfd_alloc (abfd
,
1413 mdata
->nsects
* sizeof (bfd_mach_o_section
*));
1416 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
1418 if (mdata
->commands
[i
].type
== BFD_MACH_O_LC_SEGMENT
)
1420 bfd_mach_o_segment_command
*seg
;
1422 seg
= &mdata
->commands
[i
].command
.segment
;
1423 BFD_ASSERT (csect
+ seg
->nsects
<= mdata
->nsects
);
1425 for (j
= 0; j
< seg
->nsects
; j
++)
1426 mdata
->sections
[csect
++] = &seg
->sections
[j
];
1432 bfd_mach_o_scan_start_address (bfd
*abfd
)
1434 bfd_mach_o_data_struct
*mdata
= abfd
->tdata
.mach_o_data
;
1435 bfd_mach_o_thread_command
*cmd
= NULL
;
1438 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
1440 if ((mdata
->commands
[i
].type
== BFD_MACH_O_LC_THREAD
) ||
1441 (mdata
->commands
[i
].type
== BFD_MACH_O_LC_UNIXTHREAD
))
1444 cmd
= &mdata
->commands
[i
].command
.thread
;
1453 for (i
= 0; i
< cmd
->nflavours
; i
++)
1455 if ((mdata
->header
.cputype
== BFD_MACH_O_CPU_TYPE_I386
)
1456 && (cmd
->flavours
[i
].flavour
1457 == (unsigned long) BFD_MACH_O_i386_THREAD_STATE
))
1459 unsigned char buf
[4];
1461 bfd_seek (abfd
, cmd
->flavours
[i
].offset
+ 40, SEEK_SET
);
1463 if (bfd_bread (buf
, 4, abfd
) != 4)
1466 abfd
->start_address
= bfd_h_get_32 (abfd
, buf
);
1468 else if ((mdata
->header
.cputype
== BFD_MACH_O_CPU_TYPE_POWERPC
)
1469 && (cmd
->flavours
[i
].flavour
== BFD_MACH_O_PPC_THREAD_STATE
))
1471 unsigned char buf
[4];
1473 bfd_seek (abfd
, cmd
->flavours
[i
].offset
+ 0, SEEK_SET
);
1475 if (bfd_bread (buf
, 4, abfd
) != 4)
1478 abfd
->start_address
= bfd_h_get_32 (abfd
, buf
);
1486 bfd_mach_o_scan (bfd
*abfd
,
1487 bfd_mach_o_header
*header
,
1488 bfd_mach_o_data_struct
*mdata
)
1491 enum bfd_architecture cputype
;
1492 unsigned long cpusubtype
;
1494 mdata
->header
= *header
;
1495 mdata
->symbols
= NULL
;
1497 abfd
->flags
= (abfd
->xvec
->object_flags
1498 | (abfd
->flags
& (BFD_IN_MEMORY
| BFD_IO_FUNCS
)));
1499 abfd
->tdata
.mach_o_data
= mdata
;
1501 bfd_mach_o_convert_architecture (header
->cputype
, header
->cpusubtype
,
1502 &cputype
, &cpusubtype
);
1503 if (cputype
== bfd_arch_unknown
)
1505 fprintf (stderr
, "bfd_mach_o_scan: unknown architecture 0x%lx/0x%lx\n",
1506 header
->cputype
, header
->cpusubtype
);
1510 bfd_set_arch_mach (abfd
, cputype
, cpusubtype
);
1512 if (header
->ncmds
!= 0)
1514 mdata
->commands
= bfd_alloc (abfd
, header
->ncmds
* sizeof (bfd_mach_o_load_command
));
1515 if (mdata
->commands
== NULL
)
1518 for (i
= 0; i
< header
->ncmds
; i
++)
1520 bfd_mach_o_load_command
*cur
= &mdata
->commands
[i
];
1526 bfd_mach_o_load_command
*prev
= &mdata
->commands
[i
- 1];
1527 cur
->offset
= prev
->offset
+ prev
->len
;
1530 if (bfd_mach_o_scan_read_command (abfd
, cur
) < 0)
1535 if (bfd_mach_o_scan_start_address (abfd
) < 0)
1538 bfd_mach_o_flatten_sections (abfd
);
1543 bfd_mach_o_mkobject (bfd
*abfd
)
1545 bfd_mach_o_data_struct
*mdata
= NULL
;
1547 mdata
= bfd_alloc (abfd
, sizeof (bfd_mach_o_data_struct
));
1550 abfd
->tdata
.mach_o_data
= mdata
;
1552 mdata
->header
.magic
= 0;
1553 mdata
->header
.cputype
= 0;
1554 mdata
->header
.cpusubtype
= 0;
1555 mdata
->header
.filetype
= 0;
1556 mdata
->header
.ncmds
= 0;
1557 mdata
->header
.sizeofcmds
= 0;
1558 mdata
->header
.flags
= 0;
1559 mdata
->header
.byteorder
= BFD_ENDIAN_UNKNOWN
;
1560 mdata
->commands
= NULL
;
1561 mdata
->nsymbols
= 0;
1562 mdata
->symbols
= NULL
;
1564 mdata
->sections
= NULL
;
1571 bfd_mach_o_object_p (bfd
*abfd
)
1573 struct bfd_preserve preserve
;
1574 bfd_mach_o_header header
;
1576 preserve
.marker
= NULL
;
1577 if (bfd_mach_o_read_header (abfd
, &header
) != 0)
1580 if (! (header
.byteorder
== BFD_ENDIAN_BIG
1581 || header
.byteorder
== BFD_ENDIAN_LITTLE
))
1583 fprintf (stderr
, "unknown header byte-order value 0x%lx\n",
1584 (long) header
.byteorder
);
1588 if (! ((header
.byteorder
== BFD_ENDIAN_BIG
1589 && abfd
->xvec
->byteorder
== BFD_ENDIAN_BIG
1590 && abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
1591 || (header
.byteorder
== BFD_ENDIAN_LITTLE
1592 && abfd
->xvec
->byteorder
== BFD_ENDIAN_LITTLE
1593 && abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)))
1596 preserve
.marker
= bfd_zalloc (abfd
, sizeof (bfd_mach_o_data_struct
));
1597 if (preserve
.marker
== NULL
1598 || !bfd_preserve_save (abfd
, &preserve
))
1601 if (bfd_mach_o_scan (abfd
, &header
,
1602 (bfd_mach_o_data_struct
*) preserve
.marker
) != 0)
1605 bfd_preserve_finish (abfd
, &preserve
);
1609 bfd_set_error (bfd_error_wrong_format
);
1612 if (preserve
.marker
!= NULL
)
1613 bfd_preserve_restore (abfd
, &preserve
);
1618 bfd_mach_o_core_p (bfd
*abfd
)
1620 struct bfd_preserve preserve
;
1621 bfd_mach_o_header header
;
1623 preserve
.marker
= NULL
;
1624 if (bfd_mach_o_read_header (abfd
, &header
) != 0)
1627 if (! (header
.byteorder
== BFD_ENDIAN_BIG
1628 || header
.byteorder
== BFD_ENDIAN_LITTLE
))
1630 fprintf (stderr
, "unknown header byte-order value 0x%lx\n",
1631 (long) header
.byteorder
);
1635 if (! ((header
.byteorder
== BFD_ENDIAN_BIG
1636 && abfd
->xvec
->byteorder
== BFD_ENDIAN_BIG
1637 && abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
1638 || (header
.byteorder
== BFD_ENDIAN_LITTLE
1639 && abfd
->xvec
->byteorder
== BFD_ENDIAN_LITTLE
1640 && abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)))
1643 if (header
.filetype
!= BFD_MACH_O_MH_CORE
)
1646 preserve
.marker
= bfd_zalloc (abfd
, sizeof (bfd_mach_o_data_struct
));
1647 if (preserve
.marker
== NULL
1648 || !bfd_preserve_save (abfd
, &preserve
))
1651 if (bfd_mach_o_scan (abfd
, &header
,
1652 (bfd_mach_o_data_struct
*) preserve
.marker
) != 0)
1655 bfd_preserve_finish (abfd
, &preserve
);
1659 bfd_set_error (bfd_error_wrong_format
);
1662 if (preserve
.marker
!= NULL
)
1663 bfd_preserve_restore (abfd
, &preserve
);
1667 typedef struct mach_o_fat_archentry
1669 unsigned long cputype
;
1670 unsigned long cpusubtype
;
1671 unsigned long offset
;
1673 unsigned long align
;
1675 } mach_o_fat_archentry
;
1677 typedef struct mach_o_fat_data_struct
1679 unsigned long magic
;
1680 unsigned long nfat_arch
;
1681 mach_o_fat_archentry
*archentries
;
1682 } mach_o_fat_data_struct
;
1685 bfd_mach_o_archive_p (bfd
*abfd
)
1687 mach_o_fat_data_struct
*adata
= NULL
;
1688 unsigned char buf
[20];
1691 bfd_seek (abfd
, 0, SEEK_SET
);
1692 if (bfd_bread ((PTR
) buf
, 8, abfd
) != 8)
1695 adata
= bfd_alloc (abfd
, sizeof (mach_o_fat_data_struct
));
1699 adata
->magic
= bfd_getb32 (buf
);
1700 adata
->nfat_arch
= bfd_getb32 (buf
+ 4);
1701 if (adata
->magic
!= 0xcafebabe)
1704 adata
->archentries
=
1705 bfd_alloc (abfd
, adata
->nfat_arch
* sizeof (mach_o_fat_archentry
));
1706 if (adata
->archentries
== NULL
)
1709 for (i
= 0; i
< adata
->nfat_arch
; i
++)
1711 bfd_seek (abfd
, 8 + 20 * i
, SEEK_SET
);
1713 if (bfd_bread ((PTR
) buf
, 20, abfd
) != 20)
1715 adata
->archentries
[i
].cputype
= bfd_getb32 (buf
);
1716 adata
->archentries
[i
].cpusubtype
= bfd_getb32 (buf
+ 4);
1717 adata
->archentries
[i
].offset
= bfd_getb32 (buf
+ 8);
1718 adata
->archentries
[i
].size
= bfd_getb32 (buf
+ 12);
1719 adata
->archentries
[i
].align
= bfd_getb32 (buf
+ 16);
1720 adata
->archentries
[i
].abfd
= NULL
;
1723 abfd
->tdata
.mach_o_fat_data
= adata
;
1728 bfd_release (abfd
, adata
);
1729 bfd_set_error (bfd_error_wrong_format
);
1734 bfd_mach_o_openr_next_archived_file (bfd
*archive
, bfd
*prev
)
1736 mach_o_fat_data_struct
*adata
;
1737 mach_o_fat_archentry
*entry
= NULL
;
1740 adata
= (mach_o_fat_data_struct
*) archive
->tdata
.mach_o_fat_data
;
1741 BFD_ASSERT (adata
!= NULL
);
1743 /* Find index of previous entry. */
1745 i
= 0; /* Start at first one. */
1748 for (i
= 0; i
< adata
->nfat_arch
; i
++)
1750 if (adata
->archentries
[i
].abfd
== prev
)
1754 if (i
== adata
->nfat_arch
)
1757 bfd_set_error (bfd_error_bad_value
);
1760 i
++; /* Get next entry. */
1763 if (i
>= adata
->nfat_arch
)
1765 bfd_set_error (bfd_error_no_more_archived_files
);
1769 entry
= &adata
->archentries
[i
];
1770 if (entry
->abfd
== NULL
)
1772 bfd
*nbfd
= _bfd_new_bfd_contained_in (archive
);
1778 nbfd
->origin
= entry
->offset
;
1779 s
= bfd_malloc (strlen (archive
->filename
) + 1);
1782 strcpy (s
, archive
->filename
);
1784 nbfd
->iostream
= NULL
;
1792 bfd_mach_o_lookup_section (bfd
*abfd
,
1794 bfd_mach_o_load_command
**mcommand
,
1795 bfd_mach_o_section
**msection
)
1797 struct mach_o_data_struct
*md
= abfd
->tdata
.mach_o_data
;
1798 unsigned int i
, j
, num
;
1800 bfd_mach_o_load_command
*ncmd
= NULL
;
1801 bfd_mach_o_section
*nsect
= NULL
;
1803 BFD_ASSERT (mcommand
!= NULL
);
1804 BFD_ASSERT (msection
!= NULL
);
1807 for (i
= 0; i
< md
->header
.ncmds
; i
++)
1809 struct bfd_mach_o_load_command
*cmd
= &md
->commands
[i
];
1810 struct bfd_mach_o_segment_command
*seg
= NULL
;
1812 if (cmd
->type
!= BFD_MACH_O_LC_SEGMENT
)
1814 seg
= &cmd
->command
.segment
;
1816 if (seg
->segment
== section
)
1823 for (j
= 0; j
< seg
->nsects
; j
++)
1825 struct bfd_mach_o_section
*sect
= &seg
->sections
[j
];
1827 if (sect
->bfdsection
== section
)
1842 bfd_mach_o_lookup_command (bfd
*abfd
,
1843 bfd_mach_o_load_command_type type
,
1844 bfd_mach_o_load_command
**mcommand
)
1846 struct mach_o_data_struct
*md
= NULL
;
1847 bfd_mach_o_load_command
*ncmd
= NULL
;
1848 unsigned int i
, num
;
1850 md
= abfd
->tdata
.mach_o_data
;
1852 BFD_ASSERT (md
!= NULL
);
1853 BFD_ASSERT (mcommand
!= NULL
);
1856 for (i
= 0; i
< md
->header
.ncmds
; i
++)
1858 struct bfd_mach_o_load_command
*cmd
= &md
->commands
[i
];
1860 if (cmd
->type
!= type
)
1873 bfd_mach_o_stack_addr (enum bfd_mach_o_cpu_type type
)
1877 case BFD_MACH_O_CPU_TYPE_MC680x0
:
1879 case BFD_MACH_O_CPU_TYPE_MC88000
:
1881 case BFD_MACH_O_CPU_TYPE_POWERPC
:
1883 case BFD_MACH_O_CPU_TYPE_I386
:
1885 case BFD_MACH_O_CPU_TYPE_SPARC
:
1887 case BFD_MACH_O_CPU_TYPE_I860
:
1889 case BFD_MACH_O_CPU_TYPE_HPPA
:
1890 return 0xc0000000 - 0x04000000;
1897 bfd_mach_o_core_fetch_environment (bfd
*abfd
,
1898 unsigned char **rbuf
,
1901 bfd_mach_o_data_struct
*mdata
= abfd
->tdata
.mach_o_data
;
1902 unsigned long stackaddr
= bfd_mach_o_stack_addr (mdata
->header
.cputype
);
1905 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
1907 bfd_mach_o_load_command
*cur
= &mdata
->commands
[i
];
1908 bfd_mach_o_segment_command
*seg
= NULL
;
1910 if (cur
->type
!= BFD_MACH_O_LC_SEGMENT
)
1913 seg
= &cur
->command
.segment
;
1915 if ((seg
->vmaddr
+ seg
->vmsize
) == stackaddr
)
1917 unsigned long start
= seg
->fileoff
;
1918 unsigned long end
= seg
->fileoff
+ seg
->filesize
;
1919 unsigned char *buf
= bfd_malloc (1024);
1920 unsigned long size
= 1024;
1924 bfd_size_type nread
= 0;
1925 unsigned long offset
;
1926 int found_nonnull
= 0;
1928 if (size
> (end
- start
))
1929 size
= (end
- start
);
1931 buf
= bfd_realloc (buf
, size
);
1933 bfd_seek (abfd
, end
- size
, SEEK_SET
);
1934 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
);
1964 if (size
== (end
- start
))
1976 bfd_mach_o_core_file_failing_command (bfd
*abfd
)
1978 unsigned char *buf
= NULL
;
1979 unsigned int len
= 0;
1982 ret
= bfd_mach_o_core_fetch_environment (abfd
, &buf
, &len
);
1986 return (char *) buf
;
1990 bfd_mach_o_core_file_failing_signal (bfd
*abfd ATTRIBUTE_UNUSED
)
1996 bfd_mach_o_core_file_matches_executable_p (bfd
*core_bfd ATTRIBUTE_UNUSED
,
1997 bfd
*exec_bfd ATTRIBUTE_UNUSED
)
2002 #define TARGET_NAME mach_o_be_vec
2003 #define TARGET_STRING "mach-o-be"
2004 #define TARGET_BIG_ENDIAN 1
2005 #define TARGET_ARCHIVE 0
2007 #include "mach-o-target.c"
2010 #undef TARGET_STRING
2011 #undef TARGET_BIG_ENDIAN
2012 #undef TARGET_ARCHIVE
2014 #define TARGET_NAME mach_o_le_vec
2015 #define TARGET_STRING "mach-o-le"
2016 #define TARGET_BIG_ENDIAN 0
2017 #define TARGET_ARCHIVE 0
2019 #include "mach-o-target.c"
2022 #undef TARGET_STRING
2023 #undef TARGET_BIG_ENDIAN
2024 #undef TARGET_ARCHIVE
2026 #define TARGET_NAME mach_o_fat_vec
2027 #define TARGET_STRING "mach-o-fat"
2028 #define TARGET_BIG_ENDIAN 1
2029 #define TARGET_ARCHIVE 1
2031 #include "mach-o-target.c"
2034 #undef TARGET_STRING
2035 #undef TARGET_BIG_ENDIAN
2036 #undef TARGET_ARCHIVE