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 \
76 _bfd_generic_section_already_linked
77 #define bfd_mach_o_bfd_copy_private_header_data _bfd_generic_bfd_copy_private_header_data
79 static bfd_boolean bfd_mach_o_bfd_copy_private_symbol_data
80 PARAMS ((bfd *, asymbol *, bfd *, asymbol *));
81 static bfd_boolean bfd_mach_o_bfd_copy_private_section_data
82 PARAMS ((bfd *, asection *, bfd *, asection *));
83 static bfd_boolean bfd_mach_o_bfd_copy_private_bfd_data
84 PARAMS ((bfd *, bfd *));
85 static long bfd_mach_o_count_symbols
87 static long bfd_mach_o_get_symtab_upper_bound
89 static long bfd_mach_o_canonicalize_symtab
90 PARAMS ((bfd *, asymbol **));
91 static void bfd_mach_o_get_symbol_info
92 PARAMS ((bfd *, asymbol *, symbol_info *));
93 static void bfd_mach_o_print_symbol
94 PARAMS ((bfd *, PTR, asymbol *, bfd_print_symbol_type));
95 static void bfd_mach_o_convert_architecture
96 PARAMS ((bfd_mach_o_cpu_type, bfd_mach_o_cpu_subtype,
97 enum bfd_architecture *, unsigned long *));
98 static bfd_boolean bfd_mach_o_write_contents
100 static int bfd_mach_o_sizeof_headers
101 PARAMS ((bfd *, bfd_boolean));
102 static asymbol * bfd_mach_o_make_empty_symbol
104 static int bfd_mach_o_write_header
105 PARAMS ((bfd *, bfd_mach_o_header *));
106 static int bfd_mach_o_read_header
107 PARAMS ((bfd *, bfd_mach_o_header *));
108 static asection * bfd_mach_o_make_bfd_section
109 PARAMS ((bfd *, bfd_mach_o_section *));
110 static int bfd_mach_o_scan_read_section
111 PARAMS ((bfd *, bfd_mach_o_section *, bfd_vma));
112 static int bfd_mach_o_scan_write_section
113 PARAMS ((bfd *, bfd_mach_o_section *, bfd_vma));
114 static int bfd_mach_o_scan_write_symtab_symbols
115 PARAMS ((bfd *, bfd_mach_o_load_command *));
116 static int bfd_mach_o_scan_write_thread
117 PARAMS ((bfd *, bfd_mach_o_load_command *));
118 static int bfd_mach_o_scan_read_dylinker
119 PARAMS ((bfd *, bfd_mach_o_load_command *));
120 static int bfd_mach_o_scan_read_dylib
121 PARAMS ((bfd *, bfd_mach_o_load_command *));
122 static int bfd_mach_o_scan_read_prebound_dylib
123 PARAMS ((bfd *, bfd_mach_o_load_command *));
124 static int bfd_mach_o_scan_read_thread
125 PARAMS ((bfd *, bfd_mach_o_load_command *));
126 static int bfd_mach_o_scan_write_symtab
127 PARAMS ((bfd *, bfd_mach_o_load_command *));
128 static int bfd_mach_o_scan_read_dysymtab
129 PARAMS ((bfd *, bfd_mach_o_load_command *));
130 static int bfd_mach_o_scan_read_symtab
131 PARAMS ((bfd *, bfd_mach_o_load_command *));
132 static int bfd_mach_o_scan_read_segment
133 PARAMS ((bfd *, bfd_mach_o_load_command *));
134 static int bfd_mach_o_scan_write_segment
135 PARAMS ((bfd *, bfd_mach_o_load_command *));
136 static int bfd_mach_o_scan_read_command
137 PARAMS ((bfd *, bfd_mach_o_load_command *));
138 static void bfd_mach_o_flatten_sections
140 static const char * bfd_mach_o_i386_flavour_string
141 PARAMS ((unsigned int));
142 static const char * bfd_mach_o_ppc_flavour_string
143 PARAMS ((unsigned int));
145 /* The flags field of a section structure is separated into two parts a section
146 type and section attributes. The section types are mutually exclusive (it
147 can only have one type) but the section attributes are not (it may have more
148 than one attribute). */
150 #define SECTION_TYPE 0x000000ff /* 256 section types. */
151 #define SECTION_ATTRIBUTES 0xffffff00 /* 24 section attributes. */
153 /* Constants for the section attributes part of the flags field of a section
156 #define SECTION_ATTRIBUTES_USR 0xff000000 /* User-settable attributes. */
157 #define S_ATTR_PURE_INSTRUCTIONS 0x80000000 /* Section contains only true machine instructions. */
158 #define SECTION_ATTRIBUTES_SYS 0x00ffff00 /* System setable attributes. */
159 #define S_ATTR_SOME_INSTRUCTIONS 0x00000400 /* Section contains some machine instructions. */
160 #define S_ATTR_EXT_RELOC 0x00000200 /* Section has external relocation entries. */
161 #define S_ATTR_LOC_RELOC 0x00000100 /* Section has local relocation entries. */
172 bfd_mach_o_valid (abfd)
175 if (abfd == NULL || abfd->xvec == NULL)
178 if (! ((abfd->xvec == &mach_o_be_vec)
179 || (abfd->xvec == &mach_o_le_vec)
180 || (abfd->xvec == &mach_o_fat_vec)))
183 if (abfd->tdata.mach_o_data == NULL)
188 /* Copy any private info we understand from the input symbol
189 to the output symbol. */
192 bfd_mach_o_bfd_copy_private_symbol_data (ibfd, isymbol, obfd, osymbol)
193 bfd *ibfd ATTRIBUTE_UNUSED;
194 asymbol *isymbol ATTRIBUTE_UNUSED;
195 bfd *obfd ATTRIBUTE_UNUSED;
196 asymbol *osymbol ATTRIBUTE_UNUSED;
201 /* Copy any private info we understand from the input section
202 to the output section. */
205 bfd_mach_o_bfd_copy_private_section_data (ibfd, isection, obfd, osection)
206 bfd *ibfd ATTRIBUTE_UNUSED;
207 asection *isection ATTRIBUTE_UNUSED;
208 bfd *obfd ATTRIBUTE_UNUSED;
209 asection *osection ATTRIBUTE_UNUSED;
214 /* Copy any private info we understand from the input bfd
215 to the output bfd. */
218 bfd_mach_o_bfd_copy_private_bfd_data (ibfd, obfd)
222 BFD_ASSERT (bfd_mach_o_valid (ibfd));
223 BFD_ASSERT (bfd_mach_o_valid (obfd));
225 obfd->tdata.mach_o_data = ibfd->tdata.mach_o_data;
226 obfd->tdata.mach_o_data->ibfd = ibfd;
231 bfd_mach_o_count_symbols (abfd)
234 bfd_mach_o_data_struct *mdata = NULL;
238 BFD_ASSERT (bfd_mach_o_valid (abfd));
239 mdata = abfd->tdata.mach_o_data;
241 for (i = 0; i < mdata->header.ncmds; i++)
242 if (mdata->commands[i].type == BFD_MACH_O_LC_SYMTAB)
244 bfd_mach_o_symtab_command *sym = &mdata->commands[i].command.symtab;
252 bfd_mach_o_get_symtab_upper_bound (abfd)
255 long nsyms = bfd_mach_o_count_symbols (abfd);
260 return ((nsyms + 1) * sizeof (asymbol *));
264 bfd_mach_o_canonicalize_symtab (abfd, alocation)
268 bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
269 long nsyms = bfd_mach_o_count_symbols (abfd);
270 asymbol **csym = alocation;
276 for (i = 0; i < mdata->header.ncmds; i++)
278 if (mdata->commands[i].type == BFD_MACH_O_LC_SYMTAB)
280 bfd_mach_o_symtab_command *sym = &mdata->commands[i].command.symtab;
282 if (bfd_mach_o_scan_read_symtab_symbols (abfd, &mdata->commands[i].command.symtab) != 0)
284 fprintf (stderr, "bfd_mach_o_canonicalize_symtab: unable to load symbols for section %lu\n", i);
288 BFD_ASSERT (sym->symbols != NULL);
290 for (j = 0; j < sym->nsyms; j++)
292 BFD_ASSERT (csym < (alocation + nsyms));
293 *csym++ = &sym->symbols[j];
304 bfd_mach_o_get_symbol_info (abfd, symbol, ret)
305 bfd *abfd ATTRIBUTE_UNUSED;
309 bfd_symbol_info (symbol, ret);
313 bfd_mach_o_print_symbol (abfd, afile, symbol, how)
317 bfd_print_symbol_type how;
319 FILE *file = (FILE *) afile;
323 case bfd_print_symbol_name:
324 fprintf (file, "%s", symbol->name);
327 bfd_print_symbol_vandf (abfd, (PTR) file, symbol);
328 fprintf (file, " %-5s %s", symbol->section->name, symbol->name);
333 bfd_mach_o_convert_architecture (mtype, msubtype, type, subtype)
334 bfd_mach_o_cpu_type mtype;
335 bfd_mach_o_cpu_subtype msubtype ATTRIBUTE_UNUSED;
336 enum bfd_architecture *type;
337 unsigned long *subtype;
339 *subtype = bfd_arch_unknown;
343 case BFD_MACH_O_CPU_TYPE_VAX: *type = bfd_arch_vax; break;
344 case BFD_MACH_O_CPU_TYPE_MC680x0: *type = bfd_arch_m68k; break;
345 case BFD_MACH_O_CPU_TYPE_I386: *type = bfd_arch_i386; break;
346 case BFD_MACH_O_CPU_TYPE_MIPS: *type = bfd_arch_mips; break;
347 case BFD_MACH_O_CPU_TYPE_MC98000: *type = bfd_arch_m98k; break;
348 case BFD_MACH_O_CPU_TYPE_HPPA: *type = bfd_arch_hppa; break;
349 case BFD_MACH_O_CPU_TYPE_ARM: *type = bfd_arch_arm; break;
350 case BFD_MACH_O_CPU_TYPE_MC88000: *type = bfd_arch_m88k; break;
351 case BFD_MACH_O_CPU_TYPE_SPARC: *type = bfd_arch_sparc; break;
352 case BFD_MACH_O_CPU_TYPE_I860: *type = bfd_arch_i860; break;
353 case BFD_MACH_O_CPU_TYPE_ALPHA: *type = bfd_arch_alpha; break;
354 case BFD_MACH_O_CPU_TYPE_POWERPC: *type = bfd_arch_powerpc; break;
355 default: *type = bfd_arch_unknown; break;
360 case bfd_arch_i386: *subtype = bfd_mach_i386_i386; break;
361 case bfd_arch_sparc: *subtype = bfd_mach_sparc; break;
363 *subtype = bfd_arch_unknown;
368 bfd_mach_o_write_contents (abfd)
374 bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
376 /* Write data sections first in case they overlap header data to be
379 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
382 /* Now write header information. */
383 if (bfd_mach_o_write_header (abfd, &mdata->header) != 0)
386 for (i = 0; i < mdata->header.ncmds; i++)
388 unsigned char buf[8];
389 bfd_mach_o_load_command *cur = &mdata->commands[i];
390 unsigned long typeflag;
392 typeflag = cur->type_required ? cur->type & BFD_MACH_O_LC_REQ_DYLD : cur->type;
394 bfd_h_put_32 (abfd, typeflag, buf);
395 bfd_h_put_32 (abfd, cur->len, buf + 4);
397 bfd_seek (abfd, cur->offset, SEEK_SET);
398 if (bfd_bwrite ((PTR) buf, 8, abfd) != 8)
403 case BFD_MACH_O_LC_SEGMENT:
404 if (bfd_mach_o_scan_write_segment (abfd, cur) != 0)
407 case BFD_MACH_O_LC_SYMTAB:
408 if (bfd_mach_o_scan_write_symtab (abfd, cur) != 0)
411 case BFD_MACH_O_LC_SYMSEG:
413 case BFD_MACH_O_LC_THREAD:
414 case BFD_MACH_O_LC_UNIXTHREAD:
415 if (bfd_mach_o_scan_write_thread (abfd, cur) != 0)
418 case BFD_MACH_O_LC_LOADFVMLIB:
419 case BFD_MACH_O_LC_IDFVMLIB:
420 case BFD_MACH_O_LC_IDENT:
421 case BFD_MACH_O_LC_FVMFILE:
422 case BFD_MACH_O_LC_PREPAGE:
423 case BFD_MACH_O_LC_DYSYMTAB:
424 case BFD_MACH_O_LC_LOAD_DYLIB:
425 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB:
426 case BFD_MACH_O_LC_ID_DYLIB:
427 case BFD_MACH_O_LC_LOAD_DYLINKER:
428 case BFD_MACH_O_LC_ID_DYLINKER:
429 case BFD_MACH_O_LC_PREBOUND_DYLIB:
430 case BFD_MACH_O_LC_ROUTINES:
431 case BFD_MACH_O_LC_SUB_FRAMEWORK:
435 "unable to write unknown load command 0x%lx\n",
445 bfd_mach_o_sizeof_headers (a, b)
446 bfd *a ATTRIBUTE_UNUSED;
447 bfd_boolean b ATTRIBUTE_UNUSED;
452 /* Make an empty symbol. This is required only because
453 bfd_make_section_anyway wants to create a symbol for the section. */
456 bfd_mach_o_make_empty_symbol (abfd)
461 new = (asymbol *) bfd_zalloc (abfd, sizeof (asymbol));
469 bfd_mach_o_write_header (abfd, header)
471 bfd_mach_o_header *header;
473 unsigned char buf[28];
475 bfd_h_put_32 (abfd, header->magic, buf + 0);
476 bfd_h_put_32 (abfd, header->cputype, buf + 4);
477 bfd_h_put_32 (abfd, header->cpusubtype, buf + 8);
478 bfd_h_put_32 (abfd, header->filetype, buf + 12);
479 bfd_h_put_32 (abfd, header->ncmds, buf + 16);
480 bfd_h_put_32 (abfd, header->sizeofcmds, buf + 20);
481 bfd_h_put_32 (abfd, header->flags, buf + 24);
483 bfd_seek (abfd, 0, SEEK_SET);
484 if (bfd_bwrite ((PTR) buf, 28, abfd) != 28)
491 bfd_mach_o_read_header (abfd, header)
493 bfd_mach_o_header *header;
495 unsigned char buf[28];
496 bfd_vma (*get32) (const void *) = NULL;
498 bfd_seek (abfd, 0, SEEK_SET);
500 if (bfd_bread ((PTR) buf, 28, abfd) != 28)
503 if (bfd_getb32 (buf) == 0xfeedface)
505 header->byteorder = BFD_ENDIAN_BIG;
506 header->magic = 0xfeedface;
509 else if (bfd_getl32 (buf) == 0xfeedface)
511 header->byteorder = BFD_ENDIAN_LITTLE;
512 header->magic = 0xfeedface;
517 header->byteorder = BFD_ENDIAN_UNKNOWN;
521 header->cputype = (*get32) (buf + 4);
522 header->cpusubtype = (*get32) (buf + 8);
523 header->filetype = (*get32) (buf + 12);
524 header->ncmds = (*get32) (buf + 16);
525 header->sizeofcmds = (*get32) (buf + 20);
526 header->flags = (*get32) (buf + 24);
532 bfd_mach_o_make_bfd_section (abfd, section)
534 bfd_mach_o_section *section;
538 const char *prefix = "LC_SEGMENT";
539 unsigned int snamelen;
541 snamelen = strlen (prefix) + 1
542 + strlen (section->segname) + 1
543 + strlen (section->sectname) + 1;
545 sname = (char *) bfd_alloc (abfd, snamelen);
548 sprintf (sname, "%s.%s.%s", prefix, section->segname, section->sectname);
550 bfdsec = bfd_make_section_anyway (abfd, sname);
554 bfdsec->vma = section->addr;
555 bfdsec->lma = section->addr;
556 bfdsec->size = section->size;
557 bfdsec->filepos = section->offset;
558 bfdsec->alignment_power = section->align;
560 if (section->flags & BFD_MACH_O_S_ZEROFILL)
561 bfdsec->flags = SEC_ALLOC;
563 bfdsec->flags = SEC_HAS_CONTENTS | SEC_LOAD | SEC_ALLOC | SEC_CODE;
569 bfd_mach_o_scan_read_section (abfd, section, offset)
571 bfd_mach_o_section *section;
574 unsigned char buf[68];
576 bfd_seek (abfd, offset, SEEK_SET);
577 if (bfd_bread ((PTR) buf, 68, abfd) != 68)
580 memcpy (section->sectname, buf, 16);
581 section->sectname[16] = '\0';
582 memcpy (section->segname, buf + 16, 16);
583 section->segname[16] = '\0';
584 section->addr = bfd_h_get_32 (abfd, buf + 32);
585 section->size = bfd_h_get_32 (abfd, buf + 36);
586 section->offset = bfd_h_get_32 (abfd, buf + 40);
587 section->align = bfd_h_get_32 (abfd, buf + 44);
588 section->reloff = bfd_h_get_32 (abfd, buf + 48);
589 section->nreloc = bfd_h_get_32 (abfd, buf + 52);
590 section->flags = bfd_h_get_32 (abfd, buf + 56);
591 section->reserved1 = bfd_h_get_32 (abfd, buf + 60);
592 section->reserved2 = bfd_h_get_32 (abfd, buf + 64);
593 section->bfdsection = bfd_mach_o_make_bfd_section (abfd, section);
595 if (section->bfdsection == NULL)
602 bfd_mach_o_scan_write_section (abfd, section, offset)
604 bfd_mach_o_section *section;
607 unsigned char buf[68];
609 memcpy (buf, section->sectname, 16);
610 memcpy (buf + 16, section->segname, 16);
611 bfd_h_put_32 (abfd, section->addr, buf + 32);
612 bfd_h_put_32 (abfd, section->size, buf + 36);
613 bfd_h_put_32 (abfd, section->offset, buf + 40);
614 bfd_h_put_32 (abfd, section->align, buf + 44);
615 bfd_h_put_32 (abfd, section->reloff, buf + 48);
616 bfd_h_put_32 (abfd, section->nreloc, buf + 52);
617 bfd_h_put_32 (abfd, section->flags, buf + 56);
618 /* bfd_h_put_32 (abfd, section->reserved1, buf + 60); */
619 /* bfd_h_put_32 (abfd, section->reserved2, buf + 64); */
621 bfd_seek (abfd, offset, SEEK_SET);
622 if (bfd_bwrite ((PTR) buf, 68, abfd) != 68)
629 bfd_mach_o_scan_write_symtab_symbols (abfd, command)
631 bfd_mach_o_load_command *command;
633 bfd_mach_o_symtab_command *sym = &command->command.symtab;
637 for (i = 0; i < sym->nsyms; i++)
639 unsigned char buf[12];
640 bfd_vma symoff = sym->symoff + (i * 12);
641 unsigned char ntype = 0;
642 unsigned char nsect = 0;
645 s = &sym->symbols[i];
647 /* Instead just set from the stored values. */
648 ntype = (s->udata.i >> 24) & 0xff;
649 nsect = (s->udata.i >> 16) & 0xff;
650 ndesc = s->udata.i & 0xffff;
652 bfd_h_put_32 (abfd, s->name - sym->strtab, buf);
653 bfd_h_put_8 (abfd, ntype, buf + 4);
654 bfd_h_put_8 (abfd, nsect, buf + 5);
655 bfd_h_put_16 (abfd, ndesc, buf + 6);
656 bfd_h_put_32 (abfd, s->section->vma + s->value, buf + 8);
658 bfd_seek (abfd, symoff, SEEK_SET);
659 if (bfd_bwrite ((PTR) buf, 12, abfd) != 12)
661 fprintf (stderr, "bfd_mach_o_scan_write_symtab_symbols: unable to write %d bytes at %lu\n",
662 12, (unsigned long) symoff);
671 bfd_mach_o_scan_read_symtab_symbol (abfd, sym, s, i)
673 bfd_mach_o_symtab_command *sym;
677 bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
678 bfd_vma symoff = sym->symoff + (i * 12);
679 unsigned char buf[12];
680 unsigned char type = -1;
681 unsigned char section = -1;
683 unsigned long value = -1;
684 unsigned long stroff = -1;
685 unsigned int symtype = -1;
687 BFD_ASSERT (sym->strtab != NULL);
689 bfd_seek (abfd, symoff, SEEK_SET);
690 if (bfd_bread ((PTR) buf, 12, abfd) != 12)
692 fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: unable to read %d bytes at %lu\n",
693 12, (unsigned long) symoff);
697 stroff = bfd_h_get_32 (abfd, buf);
698 type = bfd_h_get_8 (abfd, buf + 4);
699 symtype = (type & 0x0e);
700 section = bfd_h_get_8 (abfd, buf + 5) - 1;
701 desc = bfd_h_get_16 (abfd, buf + 6);
702 value = bfd_h_get_32 (abfd, buf + 8);
704 if (stroff >= sym->strsize)
706 fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: symbol name out of range (%lu >= %lu)\n",
707 (unsigned long) stroff, (unsigned long) sym->strsize);
712 s->name = sym->strtab + stroff;
714 s->udata.i = (type << 24) | (section << 16) | desc;
717 if (type & BFD_MACH_O_N_STAB)
719 s->flags |= BSF_DEBUGGING;
720 s->section = bfd_und_section_ptr;
724 if (type & BFD_MACH_O_N_PEXT)
726 type &= ~BFD_MACH_O_N_PEXT;
727 s->flags |= BSF_GLOBAL;
730 if (type & BFD_MACH_O_N_EXT)
732 type &= ~BFD_MACH_O_N_EXT;
733 s->flags |= BSF_GLOBAL;
738 case BFD_MACH_O_N_UNDF:
739 s->section = bfd_und_section_ptr;
741 case BFD_MACH_O_N_PBUD:
742 s->section = bfd_und_section_ptr;
744 case BFD_MACH_O_N_ABS:
745 s->section = bfd_abs_section_ptr;
747 case BFD_MACH_O_N_SECT:
748 if ((section > 0) && (section <= mdata->nsects))
750 s->section = mdata->sections[section - 1]->bfdsection;
751 s->value = s->value - mdata->sections[section - 1]->addr;
755 /* Mach-O uses 0 to mean "no section"; not an error. */
758 fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: "
759 "symbol \"%s\" specified invalid section %d (max %lu): setting to undefined\n",
760 s->name, section, mdata->nsects);
762 s->section = bfd_und_section_ptr;
765 case BFD_MACH_O_N_INDR:
766 fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: "
767 "symbol \"%s\" is unsupported 'indirect' reference: setting to undefined\n",
769 s->section = bfd_und_section_ptr;
772 fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: "
773 "symbol \"%s\" specified invalid type field 0x%x: setting to undefined\n",
775 s->section = bfd_und_section_ptr;
784 bfd_mach_o_scan_read_symtab_strtab (abfd, sym)
786 bfd_mach_o_symtab_command *sym;
788 BFD_ASSERT (sym->strtab == NULL);
790 if (abfd->flags & BFD_IN_MEMORY)
792 struct bfd_in_memory *b;
794 b = (struct bfd_in_memory *) abfd->iostream;
796 if ((sym->stroff + sym->strsize) > b->size)
798 bfd_set_error (bfd_error_file_truncated);
801 sym->strtab = (char *) b->buffer + sym->stroff;
805 sym->strtab = bfd_alloc (abfd, sym->strsize);
806 if (sym->strtab == NULL)
809 bfd_seek (abfd, sym->stroff, SEEK_SET);
810 if (bfd_bread ((PTR) sym->strtab, sym->strsize, abfd) != sym->strsize)
812 fprintf (stderr, "bfd_mach_o_scan_read_symtab_strtab: unable to read %lu bytes at %lu\n",
813 sym->strsize, sym->stroff);
821 bfd_mach_o_scan_read_symtab_symbols (abfd, sym)
823 bfd_mach_o_symtab_command *sym;
828 BFD_ASSERT (sym->symbols == NULL);
829 sym->symbols = bfd_alloc (abfd, sym->nsyms * sizeof (asymbol));
831 if (sym->symbols == NULL)
833 fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbols: unable to allocate memory for symbols\n");
837 ret = bfd_mach_o_scan_read_symtab_strtab (abfd, sym);
841 for (i = 0; i < sym->nsyms; i++)
843 ret = bfd_mach_o_scan_read_symtab_symbol (abfd, sym, &sym->symbols[i], i);
852 bfd_mach_o_scan_read_dysymtab_symbol (abfd, dysym, sym, s, i)
854 bfd_mach_o_dysymtab_command *dysym;
855 bfd_mach_o_symtab_command *sym;
859 unsigned long isymoff = dysym->indirectsymoff + (i * 4);
860 unsigned long symindex;
861 unsigned char buf[4];
863 BFD_ASSERT (i < dysym->nindirectsyms);
865 bfd_seek (abfd, isymoff, SEEK_SET);
866 if (bfd_bread ((PTR) buf, 4, abfd) != 4)
868 fprintf (stderr, "bfd_mach_o_scan_read_dysymtab_symbol: unable to read %lu bytes at %lu\n",
869 (unsigned long) 4, isymoff);
872 symindex = bfd_h_get_32 (abfd, buf);
874 return bfd_mach_o_scan_read_symtab_symbol (abfd, sym, s, symindex);
878 bfd_mach_o_i386_flavour_string (flavour)
879 unsigned int flavour;
881 switch ((int) flavour)
883 case BFD_MACH_O_i386_NEW_THREAD_STATE: return "i386_NEW_THREAD_STATE";
884 case BFD_MACH_O_i386_FLOAT_STATE: return "i386_FLOAT_STATE";
885 case BFD_MACH_O_i386_ISA_PORT_MAP_STATE: return "i386_ISA_PORT_MAP_STATE";
886 case BFD_MACH_O_i386_V86_ASSIST_STATE: return "i386_V86_ASSIST_STATE";
887 case BFD_MACH_O_i386_REGS_SEGS_STATE: return "i386_REGS_SEGS_STATE";
888 case BFD_MACH_O_i386_THREAD_SYSCALL_STATE: return "i386_THREAD_SYSCALL_STATE";
889 case BFD_MACH_O_i386_THREAD_STATE_NONE: return "i386_THREAD_STATE_NONE";
890 case BFD_MACH_O_i386_SAVED_STATE: return "i386_SAVED_STATE";
891 case BFD_MACH_O_i386_THREAD_STATE: return "i386_THREAD_STATE";
892 case BFD_MACH_O_i386_THREAD_FPSTATE: return "i386_THREAD_FPSTATE";
893 case BFD_MACH_O_i386_THREAD_EXCEPTSTATE: return "i386_THREAD_EXCEPTSTATE";
894 case BFD_MACH_O_i386_THREAD_CTHREADSTATE: return "i386_THREAD_CTHREADSTATE";
895 default: return "UNKNOWN";
900 bfd_mach_o_ppc_flavour_string (flavour)
901 unsigned int flavour;
903 switch ((int) flavour)
905 case BFD_MACH_O_PPC_THREAD_STATE: return "PPC_THREAD_STATE";
906 case BFD_MACH_O_PPC_FLOAT_STATE: return "PPC_FLOAT_STATE";
907 case BFD_MACH_O_PPC_EXCEPTION_STATE: return "PPC_EXCEPTION_STATE";
908 case BFD_MACH_O_PPC_VECTOR_STATE: return "PPC_VECTOR_STATE";
909 default: return "UNKNOWN";
914 bfd_mach_o_scan_write_thread (abfd, command)
916 bfd_mach_o_load_command *command;
918 bfd_mach_o_thread_command *cmd = &command->command.thread;
920 unsigned char buf[8];
922 unsigned int nflavours;
924 BFD_ASSERT ((command->type == BFD_MACH_O_LC_THREAD)
925 || (command->type == BFD_MACH_O_LC_UNIXTHREAD));
929 for (i = 0; i < cmd->nflavours; i++)
931 BFD_ASSERT ((cmd->flavours[i].size % 4) == 0);
932 BFD_ASSERT (cmd->flavours[i].offset == (command->offset + offset + 8));
934 bfd_h_put_32 (abfd, cmd->flavours[i].flavour, buf);
935 bfd_h_put_32 (abfd, (cmd->flavours[i].size / 4), buf + 4);
937 bfd_seek (abfd, command->offset + offset, SEEK_SET);
938 if (bfd_bwrite ((PTR) buf, 8, abfd) != 8)
941 offset += cmd->flavours[i].size + 8;
948 bfd_mach_o_scan_read_dylinker (abfd, command)
950 bfd_mach_o_load_command *command;
952 bfd_mach_o_dylinker_command *cmd = &command->command.dylinker;
953 unsigned char buf[4];
954 unsigned int nameoff;
959 BFD_ASSERT ((command->type == BFD_MACH_O_LC_ID_DYLINKER)
960 || (command->type == BFD_MACH_O_LC_LOAD_DYLINKER));
962 bfd_seek (abfd, command->offset + 8, SEEK_SET);
963 if (bfd_bread ((PTR) buf, 4, abfd) != 4)
966 nameoff = bfd_h_get_32 (abfd, buf + 0);
968 cmd->name_offset = command->offset + nameoff;
969 cmd->name_len = command->len - nameoff;
971 if (command->type == BFD_MACH_O_LC_LOAD_DYLINKER)
972 prefix = "LC_LOAD_DYLINKER";
973 else if (command->type == BFD_MACH_O_LC_ID_DYLINKER)
974 prefix = "LC_ID_DYLINKER";
978 sname = (char *) bfd_alloc (abfd, strlen (prefix) + 1);
981 strcpy (sname, prefix);
983 bfdsec = bfd_make_section_anyway (abfd, sname);
989 bfdsec->size = command->len - 8;
990 bfdsec->filepos = command->offset + 8;
991 bfdsec->alignment_power = 0;
992 bfdsec->flags = SEC_HAS_CONTENTS;
994 cmd->section = bfdsec;
1000 bfd_mach_o_scan_read_dylib (abfd, command)
1002 bfd_mach_o_load_command *command;
1004 bfd_mach_o_dylib_command *cmd = &command->command.dylib;
1005 unsigned char buf[16];
1006 unsigned int nameoff;
1011 BFD_ASSERT ((command->type == BFD_MACH_O_LC_ID_DYLIB)
1012 || (command->type == BFD_MACH_O_LC_LOAD_DYLIB)
1013 || (command->type == BFD_MACH_O_LC_LOAD_WEAK_DYLIB));
1015 bfd_seek (abfd, command->offset + 8, SEEK_SET);
1016 if (bfd_bread ((PTR) buf, 16, abfd) != 16)
1019 nameoff = bfd_h_get_32 (abfd, buf + 0);
1020 cmd->timestamp = bfd_h_get_32 (abfd, buf + 4);
1021 cmd->current_version = bfd_h_get_32 (abfd, buf + 8);
1022 cmd->compatibility_version = bfd_h_get_32 (abfd, buf + 12);
1024 cmd->name_offset = command->offset + nameoff;
1025 cmd->name_len = command->len - nameoff;
1027 if (command->type == BFD_MACH_O_LC_LOAD_DYLIB)
1028 prefix = "LC_LOAD_DYLIB";
1029 else if (command->type == BFD_MACH_O_LC_LOAD_WEAK_DYLIB)
1030 prefix = "LC_LOAD_WEAK_DYLIB";
1031 else if (command->type == BFD_MACH_O_LC_ID_DYLIB)
1032 prefix = "LC_ID_DYLIB";
1036 sname = (char *) bfd_alloc (abfd, strlen (prefix) + 1);
1039 strcpy (sname, prefix);
1041 bfdsec = bfd_make_section_anyway (abfd, sname);
1047 bfdsec->size = command->len - 8;
1048 bfdsec->filepos = command->offset + 8;
1049 bfdsec->alignment_power = 0;
1050 bfdsec->flags = SEC_HAS_CONTENTS;
1052 cmd->section = bfdsec;
1058 bfd_mach_o_scan_read_prebound_dylib (abfd, command)
1059 bfd *abfd ATTRIBUTE_UNUSED;
1060 bfd_mach_o_load_command *command ATTRIBUTE_UNUSED;
1062 /* bfd_mach_o_prebound_dylib_command *cmd = &command->command.prebound_dylib; */
1064 BFD_ASSERT (command->type == BFD_MACH_O_LC_PREBOUND_DYLIB);
1069 bfd_mach_o_scan_read_thread (abfd, command)
1071 bfd_mach_o_load_command *command;
1073 bfd_mach_o_data_struct *mdata = NULL;
1074 bfd_mach_o_thread_command *cmd = &command->command.thread;
1075 unsigned char buf[8];
1077 unsigned int nflavours;
1080 BFD_ASSERT ((command->type == BFD_MACH_O_LC_THREAD)
1081 || (command->type == BFD_MACH_O_LC_UNIXTHREAD));
1083 BFD_ASSERT (bfd_mach_o_valid (abfd));
1084 mdata = abfd->tdata.mach_o_data;
1088 while (offset != command->len)
1090 if (offset >= command->len)
1093 bfd_seek (abfd, command->offset + offset, SEEK_SET);
1095 if (bfd_bread ((PTR) buf, 8, abfd) != 8)
1098 offset += 8 + bfd_h_get_32 (abfd, buf + 4) * 4;
1103 ((bfd_mach_o_thread_flavour *)
1104 bfd_alloc (abfd, nflavours * sizeof (bfd_mach_o_thread_flavour)));
1105 if (cmd->flavours == NULL)
1107 cmd->nflavours = nflavours;
1111 while (offset != command->len)
1113 if (offset >= command->len)
1116 if (nflavours >= cmd->nflavours)
1119 bfd_seek (abfd, command->offset + offset, SEEK_SET);
1121 if (bfd_bread ((PTR) buf, 8, abfd) != 8)
1124 cmd->flavours[nflavours].flavour = bfd_h_get_32 (abfd, buf);
1125 cmd->flavours[nflavours].offset = command->offset + offset + 8;
1126 cmd->flavours[nflavours].size = bfd_h_get_32 (abfd, buf + 4) * 4;
1127 offset += cmd->flavours[nflavours].size + 8;
1131 for (i = 0; i < nflavours; i++)
1134 unsigned int snamelen;
1136 const char *flavourstr;
1137 const char *prefix = "LC_THREAD";
1140 switch (mdata->header.cputype)
1142 case BFD_MACH_O_CPU_TYPE_POWERPC:
1143 flavourstr = bfd_mach_o_ppc_flavour_string (cmd->flavours[i].flavour);
1145 case BFD_MACH_O_CPU_TYPE_I386:
1146 flavourstr = bfd_mach_o_i386_flavour_string (cmd->flavours[i].flavour);
1149 flavourstr = "UNKNOWN_ARCHITECTURE";
1153 snamelen = strlen (prefix) + 1 + 20 + 1 + strlen (flavourstr) + 1;
1154 sname = (char *) bfd_alloc (abfd, snamelen);
1160 sprintf (sname, "%s.%s.%u", prefix, flavourstr, j);
1161 if (bfd_get_section_by_name (abfd, sname) == NULL)
1166 bfdsec = bfd_make_section (abfd, sname);
1170 bfdsec->size = cmd->flavours[i].size;
1171 bfdsec->filepos = cmd->flavours[i].offset;
1172 bfdsec->alignment_power = 0x0;
1173 bfdsec->flags = SEC_HAS_CONTENTS;
1175 cmd->section = bfdsec;
1182 bfd_mach_o_scan_write_symtab (abfd, command)
1184 bfd_mach_o_load_command *command;
1186 bfd_mach_o_symtab_command *seg = &command->command.symtab;
1187 unsigned char buf[16];
1189 BFD_ASSERT (command->type == BFD_MACH_O_LC_SYMTAB);
1191 bfd_h_put_32 (abfd, seg->symoff, buf);
1192 bfd_h_put_32 (abfd, seg->nsyms, buf + 4);
1193 bfd_h_put_32 (abfd, seg->stroff, buf + 8);
1194 bfd_h_put_32 (abfd, seg->strsize, buf + 12);
1196 bfd_seek (abfd, command->offset + 8, SEEK_SET);
1197 if (bfd_bwrite ((PTR) buf, 16, abfd) != 16)
1200 if (bfd_mach_o_scan_write_symtab_symbols (abfd, command) != 0)
1207 bfd_mach_o_scan_read_dysymtab (abfd, command)
1209 bfd_mach_o_load_command *command;
1211 bfd_mach_o_dysymtab_command *seg = &command->command.dysymtab;
1212 unsigned char buf[72];
1214 BFD_ASSERT (command->type == BFD_MACH_O_LC_DYSYMTAB);
1216 bfd_seek (abfd, command->offset + 8, SEEK_SET);
1217 if (bfd_bread ((PTR) buf, 72, abfd) != 72)
1220 seg->ilocalsym = bfd_h_get_32 (abfd, buf + 0);
1221 seg->nlocalsym = bfd_h_get_32 (abfd, buf + 4);
1222 seg->iextdefsym = bfd_h_get_32 (abfd, buf + 8);
1223 seg->nextdefsym = bfd_h_get_32 (abfd, buf + 12);
1224 seg->iundefsym = bfd_h_get_32 (abfd, buf + 16);
1225 seg->nundefsym = bfd_h_get_32 (abfd, buf + 20);
1226 seg->tocoff = bfd_h_get_32 (abfd, buf + 24);
1227 seg->ntoc = bfd_h_get_32 (abfd, buf + 28);
1228 seg->modtaboff = bfd_h_get_32 (abfd, buf + 32);
1229 seg->nmodtab = bfd_h_get_32 (abfd, buf + 36);
1230 seg->extrefsymoff = bfd_h_get_32 (abfd, buf + 40);
1231 seg->nextrefsyms = bfd_h_get_32 (abfd, buf + 44);
1232 seg->indirectsymoff = bfd_h_get_32 (abfd, buf + 48);
1233 seg->nindirectsyms = bfd_h_get_32 (abfd, buf + 52);
1234 seg->extreloff = bfd_h_get_32 (abfd, buf + 56);
1235 seg->nextrel = bfd_h_get_32 (abfd, buf + 60);
1236 seg->locreloff = bfd_h_get_32 (abfd, buf + 64);
1237 seg->nlocrel = bfd_h_get_32 (abfd, buf + 68);
1243 bfd_mach_o_scan_read_symtab (abfd, command)
1245 bfd_mach_o_load_command *command;
1247 bfd_mach_o_symtab_command *seg = &command->command.symtab;
1248 unsigned char buf[16];
1251 const char *prefix = "LC_SYMTAB.stabs";
1253 BFD_ASSERT (command->type == BFD_MACH_O_LC_SYMTAB);
1255 bfd_seek (abfd, command->offset + 8, SEEK_SET);
1256 if (bfd_bread ((PTR) buf, 16, abfd) != 16)
1259 seg->symoff = bfd_h_get_32 (abfd, buf);
1260 seg->nsyms = bfd_h_get_32 (abfd, buf + 4);
1261 seg->stroff = bfd_h_get_32 (abfd, buf + 8);
1262 seg->strsize = bfd_h_get_32 (abfd, buf + 12);
1263 seg->symbols = NULL;
1266 sname = (char *) bfd_alloc (abfd, strlen (prefix) + 1);
1269 strcpy (sname, prefix);
1271 bfdsec = bfd_make_section_anyway (abfd, sname);
1277 bfdsec->size = seg->nsyms * 12;
1278 bfdsec->filepos = seg->symoff;
1279 bfdsec->alignment_power = 0;
1280 bfdsec->flags = SEC_HAS_CONTENTS;
1282 seg->stabs_segment = bfdsec;
1284 prefix = "LC_SYMTAB.stabstr";
1285 sname = (char *) bfd_alloc (abfd, strlen (prefix) + 1);
1288 strcpy (sname, prefix);
1290 bfdsec = bfd_make_section_anyway (abfd, sname);
1296 bfdsec->size = seg->strsize;
1297 bfdsec->filepos = seg->stroff;
1298 bfdsec->alignment_power = 0;
1299 bfdsec->flags = SEC_HAS_CONTENTS;
1301 seg->stabstr_segment = bfdsec;
1307 bfd_mach_o_scan_read_segment (abfd, command)
1309 bfd_mach_o_load_command *command;
1311 unsigned char buf[48];
1312 bfd_mach_o_segment_command *seg = &command->command.segment;
1316 const char *prefix = "LC_SEGMENT";
1317 unsigned int snamelen;
1319 BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT);
1321 bfd_seek (abfd, command->offset + 8, SEEK_SET);
1322 if (bfd_bread ((PTR) buf, 48, abfd) != 48)
1325 memcpy (seg->segname, buf, 16);
1326 seg->vmaddr = bfd_h_get_32 (abfd, buf + 16);
1327 seg->vmsize = bfd_h_get_32 (abfd, buf + 20);
1328 seg->fileoff = bfd_h_get_32 (abfd, buf + 24);
1329 seg->filesize = bfd_h_get_32 (abfd, buf + 28);
1330 /* seg->maxprot = bfd_h_get_32 (abfd, buf + 32); */
1331 /* seg->initprot = bfd_h_get_32 (abfd, buf + 36); */
1332 seg->nsects = bfd_h_get_32 (abfd, buf + 40);
1333 seg->flags = bfd_h_get_32 (abfd, buf + 44);
1335 snamelen = strlen (prefix) + 1 + strlen (seg->segname) + 1;
1336 sname = (char *) bfd_alloc (abfd, snamelen);
1339 sprintf (sname, "%s.%s", prefix, seg->segname);
1341 bfdsec = bfd_make_section_anyway (abfd, sname);
1345 bfdsec->vma = seg->vmaddr;
1346 bfdsec->lma = seg->vmaddr;
1347 bfdsec->size = seg->filesize;
1348 bfdsec->filepos = seg->fileoff;
1349 bfdsec->alignment_power = 0x0;
1350 bfdsec->flags = SEC_HAS_CONTENTS | SEC_LOAD | SEC_ALLOC | SEC_CODE;
1352 seg->segment = bfdsec;
1354 if (seg->nsects != 0)
1357 ((bfd_mach_o_section *)
1358 bfd_alloc (abfd, seg->nsects * sizeof (bfd_mach_o_section)));
1359 if (seg->sections == NULL)
1362 for (i = 0; i < seg->nsects; i++)
1364 bfd_vma segoff = command->offset + 48 + 8 + (i * 68);
1366 if (bfd_mach_o_scan_read_section (abfd, &seg->sections[i],
1376 bfd_mach_o_scan_write_segment (abfd, command)
1378 bfd_mach_o_load_command *command;
1380 unsigned char buf[48];
1381 bfd_mach_o_segment_command *seg = &command->command.segment;
1384 BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT);
1386 memcpy (buf, seg->segname, 16);
1387 bfd_h_put_32 (abfd, seg->vmaddr, buf + 16);
1388 bfd_h_put_32 (abfd, seg->vmsize, buf + 20);
1389 bfd_h_put_32 (abfd, seg->fileoff, buf + 24);
1390 bfd_h_put_32 (abfd, seg->filesize, buf + 28);
1391 bfd_h_put_32 (abfd, 0 /* seg->maxprot */, buf + 32);
1392 bfd_h_put_32 (abfd, 0 /* seg->initprot */, buf + 36);
1393 bfd_h_put_32 (abfd, seg->nsects, buf + 40);
1394 bfd_h_put_32 (abfd, seg->flags, buf + 44);
1396 bfd_seek (abfd, command->offset + 8, SEEK_SET);
1397 if (bfd_bwrite ((PTR) buf, 48, abfd) != 48)
1402 bfd_vma nbytes = seg->filesize;
1403 bfd_vma curoff = seg->fileoff;
1407 bfd_vma thisread = nbytes;
1409 if (thisread > 1024)
1412 bfd_seek (abfd, curoff, SEEK_SET);
1413 if (bfd_bread ((PTR) buf, thisread, abfd) != thisread)
1416 bfd_seek (abfd, curoff, SEEK_SET);
1417 if (bfd_bwrite ((PTR) buf, thisread, abfd) != thisread)
1425 for (i = 0; i < seg->nsects; i++)
1427 bfd_vma segoff = command->offset + 48 + 8 + (i * 68);
1429 if (bfd_mach_o_scan_write_section (abfd, &seg->sections[i], segoff) != 0)
1437 bfd_mach_o_scan_read_command (abfd, command)
1439 bfd_mach_o_load_command *command;
1441 unsigned char buf[8];
1443 bfd_seek (abfd, command->offset, SEEK_SET);
1444 if (bfd_bread ((PTR) buf, 8, abfd) != 8)
1447 command->type = (bfd_h_get_32 (abfd, buf) & ~BFD_MACH_O_LC_REQ_DYLD);
1448 command->type_required = (bfd_h_get_32 (abfd, buf) & BFD_MACH_O_LC_REQ_DYLD
1450 command->len = bfd_h_get_32 (abfd, buf + 4);
1452 switch (command->type)
1454 case BFD_MACH_O_LC_SEGMENT:
1455 if (bfd_mach_o_scan_read_segment (abfd, command) != 0)
1458 case BFD_MACH_O_LC_SYMTAB:
1459 if (bfd_mach_o_scan_read_symtab (abfd, command) != 0)
1462 case BFD_MACH_O_LC_SYMSEG:
1464 case BFD_MACH_O_LC_THREAD:
1465 case BFD_MACH_O_LC_UNIXTHREAD:
1466 if (bfd_mach_o_scan_read_thread (abfd, command) != 0)
1469 case BFD_MACH_O_LC_LOAD_DYLINKER:
1470 case BFD_MACH_O_LC_ID_DYLINKER:
1471 if (bfd_mach_o_scan_read_dylinker (abfd, command) != 0)
1474 case BFD_MACH_O_LC_LOAD_DYLIB:
1475 case BFD_MACH_O_LC_ID_DYLIB:
1476 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB:
1477 if (bfd_mach_o_scan_read_dylib (abfd, command) != 0)
1480 case BFD_MACH_O_LC_PREBOUND_DYLIB:
1481 if (bfd_mach_o_scan_read_prebound_dylib (abfd, command) != 0)
1484 case BFD_MACH_O_LC_LOADFVMLIB:
1485 case BFD_MACH_O_LC_IDFVMLIB:
1486 case BFD_MACH_O_LC_IDENT:
1487 case BFD_MACH_O_LC_FVMFILE:
1488 case BFD_MACH_O_LC_PREPAGE:
1489 case BFD_MACH_O_LC_ROUTINES:
1490 case BFD_MACH_O_LC_SUB_FRAMEWORK:
1492 case BFD_MACH_O_LC_DYSYMTAB:
1493 if (bfd_mach_o_scan_read_dysymtab (abfd, command) != 0)
1496 case BFD_MACH_O_LC_SUB_UMBRELLA:
1497 case BFD_MACH_O_LC_SUB_CLIENT:
1498 case BFD_MACH_O_LC_SUB_LIBRARY:
1499 case BFD_MACH_O_LC_TWOLEVEL_HINTS:
1500 case BFD_MACH_O_LC_PREBIND_CKSUM:
1503 fprintf (stderr, "unable to read unknown load command 0x%lx\n",
1504 (unsigned long) command->type);
1512 bfd_mach_o_flatten_sections (abfd)
1515 bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
1521 for (i = 0; i < mdata->header.ncmds; i++)
1523 if (mdata->commands[i].type == BFD_MACH_O_LC_SEGMENT)
1525 bfd_mach_o_segment_command *seg;
1527 seg = &mdata->commands[i].command.segment;
1528 mdata->nsects += seg->nsects;
1532 mdata->sections = bfd_alloc (abfd,
1533 mdata->nsects * sizeof (bfd_mach_o_section *));
1536 for (i = 0; i < mdata->header.ncmds; i++)
1538 if (mdata->commands[i].type == BFD_MACH_O_LC_SEGMENT)
1540 bfd_mach_o_segment_command *seg;
1542 seg = &mdata->commands[i].command.segment;
1543 BFD_ASSERT (csect + seg->nsects <= mdata->nsects);
1545 for (j = 0; j < seg->nsects; j++)
1546 mdata->sections[csect++] = &seg->sections[j];
1552 bfd_mach_o_scan_start_address (abfd)
1555 bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
1556 bfd_mach_o_thread_command *cmd = NULL;
1559 for (i = 0; i < mdata->header.ncmds; i++)
1561 if ((mdata->commands[i].type == BFD_MACH_O_LC_THREAD) ||
1562 (mdata->commands[i].type == BFD_MACH_O_LC_UNIXTHREAD))
1565 cmd = &mdata->commands[i].command.thread;
1574 for (i = 0; i < cmd->nflavours; i++)
1576 if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_I386)
1577 && (cmd->flavours[i].flavour
1578 == (unsigned long) BFD_MACH_O_i386_THREAD_STATE))
1580 unsigned char buf[4];
1582 bfd_seek (abfd, cmd->flavours[i].offset + 40, SEEK_SET);
1584 if (bfd_bread (buf, 4, abfd) != 4)
1587 abfd->start_address = bfd_h_get_32 (abfd, buf);
1589 else if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_POWERPC)
1590 && (cmd->flavours[i].flavour == BFD_MACH_O_PPC_THREAD_STATE))
1592 unsigned char buf[4];
1594 bfd_seek (abfd, cmd->flavours[i].offset + 0, SEEK_SET);
1596 if (bfd_bread (buf, 4, abfd) != 4)
1599 abfd->start_address = bfd_h_get_32 (abfd, buf);
1607 bfd_mach_o_scan (abfd, header, mdata)
1609 bfd_mach_o_header *header;
1610 bfd_mach_o_data_struct *mdata;
1613 enum bfd_architecture cputype;
1614 unsigned long cpusubtype;
1616 mdata->header = *header;
1617 mdata->symbols = NULL;
1619 abfd->flags = (abfd->xvec->object_flags
1620 | (abfd->flags & (BFD_IN_MEMORY | BFD_IO_FUNCS)));
1621 abfd->tdata.mach_o_data = mdata;
1623 bfd_mach_o_convert_architecture (header->cputype, header->cpusubtype,
1624 &cputype, &cpusubtype);
1625 if (cputype == bfd_arch_unknown)
1627 fprintf (stderr, "bfd_mach_o_scan: unknown architecture 0x%lx/0x%lx\n",
1628 header->cputype, header->cpusubtype);
1632 bfd_set_arch_mach (abfd, cputype, cpusubtype);
1634 if (header->ncmds != 0)
1637 ((bfd_mach_o_load_command *)
1638 bfd_alloc (abfd, header->ncmds * sizeof (bfd_mach_o_load_command)));
1639 if (mdata->commands == NULL)
1642 for (i = 0; i < header->ncmds; i++)
1644 bfd_mach_o_load_command *cur = &mdata->commands[i];
1650 bfd_mach_o_load_command *prev = &mdata->commands[i - 1];
1651 cur->offset = prev->offset + prev->len;
1654 if (bfd_mach_o_scan_read_command (abfd, cur) < 0)
1659 if (bfd_mach_o_scan_start_address (abfd) < 0)
1662 bfd_mach_o_flatten_sections (abfd);
1667 bfd_mach_o_mkobject (abfd)
1670 bfd_mach_o_data_struct *mdata = NULL;
1672 mdata = ((bfd_mach_o_data_struct *)
1673 bfd_alloc (abfd, sizeof (bfd_mach_o_data_struct)));
1676 abfd->tdata.mach_o_data = mdata;
1678 mdata->header.magic = 0;
1679 mdata->header.cputype = 0;
1680 mdata->header.cpusubtype = 0;
1681 mdata->header.filetype = 0;
1682 mdata->header.ncmds = 0;
1683 mdata->header.sizeofcmds = 0;
1684 mdata->header.flags = 0;
1685 mdata->header.byteorder = BFD_ENDIAN_UNKNOWN;
1686 mdata->commands = NULL;
1687 mdata->nsymbols = 0;
1688 mdata->symbols = NULL;
1690 mdata->sections = NULL;
1697 bfd_mach_o_object_p (abfd)
1700 struct bfd_preserve preserve;
1701 bfd_mach_o_header header;
1703 preserve.marker = NULL;
1704 if (bfd_mach_o_read_header (abfd, &header) != 0)
1707 if (! (header.byteorder == BFD_ENDIAN_BIG
1708 || header.byteorder == BFD_ENDIAN_LITTLE))
1710 fprintf (stderr, "unknown header byte-order value 0x%lx\n",
1711 (long) header.byteorder);
1715 if (! ((header.byteorder == BFD_ENDIAN_BIG
1716 && abfd->xvec->byteorder == BFD_ENDIAN_BIG
1717 && abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
1718 || (header.byteorder == BFD_ENDIAN_LITTLE
1719 && abfd->xvec->byteorder == BFD_ENDIAN_LITTLE
1720 && abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)))
1723 preserve.marker = bfd_zalloc (abfd, sizeof (bfd_mach_o_data_struct));
1724 if (preserve.marker == NULL
1725 || !bfd_preserve_save (abfd, &preserve))
1728 if (bfd_mach_o_scan (abfd, &header,
1729 (bfd_mach_o_data_struct *) preserve.marker) != 0)
1732 bfd_preserve_finish (abfd, &preserve);
1736 bfd_set_error (bfd_error_wrong_format);
1739 if (preserve.marker != NULL)
1740 bfd_preserve_restore (abfd, &preserve);
1745 bfd_mach_o_core_p (abfd)
1748 struct bfd_preserve preserve;
1749 bfd_mach_o_header header;
1751 preserve.marker = NULL;
1752 if (bfd_mach_o_read_header (abfd, &header) != 0)
1755 if (! (header.byteorder == BFD_ENDIAN_BIG
1756 || header.byteorder == BFD_ENDIAN_LITTLE))
1758 fprintf (stderr, "unknown header byte-order value 0x%lx\n",
1759 (long) header.byteorder);
1763 if (! ((header.byteorder == BFD_ENDIAN_BIG
1764 && abfd->xvec->byteorder == BFD_ENDIAN_BIG
1765 && abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
1766 || (header.byteorder == BFD_ENDIAN_LITTLE
1767 && abfd->xvec->byteorder == BFD_ENDIAN_LITTLE
1768 && abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)))
1771 if (header.filetype != BFD_MACH_O_MH_CORE)
1774 preserve.marker = bfd_zalloc (abfd, sizeof (bfd_mach_o_data_struct));
1775 if (preserve.marker == NULL
1776 || !bfd_preserve_save (abfd, &preserve))
1779 if (bfd_mach_o_scan (abfd, &header,
1780 (bfd_mach_o_data_struct *) preserve.marker) != 0)
1783 bfd_preserve_finish (abfd, &preserve);
1787 bfd_set_error (bfd_error_wrong_format);
1790 if (preserve.marker != NULL)
1791 bfd_preserve_restore (abfd, &preserve);
1795 typedef struct mach_o_fat_archentry
1797 unsigned long cputype;
1798 unsigned long cpusubtype;
1799 unsigned long offset;
1801 unsigned long align;
1803 } mach_o_fat_archentry;
1805 typedef struct mach_o_fat_data_struct
1807 unsigned long magic;
1808 unsigned long nfat_arch;
1809 mach_o_fat_archentry *archentries;
1810 } mach_o_fat_data_struct;
1813 bfd_mach_o_archive_p (abfd)
1816 mach_o_fat_data_struct *adata = NULL;
1817 unsigned char buf[20];
1820 bfd_seek (abfd, 0, SEEK_SET);
1821 if (bfd_bread ((PTR) buf, 8, abfd) != 8)
1824 adata = (mach_o_fat_data_struct *)
1825 bfd_alloc (abfd, sizeof (mach_o_fat_data_struct));
1829 adata->magic = bfd_getb32 (buf);
1830 adata->nfat_arch = bfd_getb32 (buf + 4);
1831 if (adata->magic != 0xcafebabe)
1834 adata->archentries = (mach_o_fat_archentry *)
1835 bfd_alloc (abfd, adata->nfat_arch * sizeof (mach_o_fat_archentry));
1836 if (adata->archentries == NULL)
1839 for (i = 0; i < adata->nfat_arch; i++)
1841 bfd_seek (abfd, 8 + 20 * i, SEEK_SET);
1843 if (bfd_bread ((PTR) buf, 20, abfd) != 20)
1845 adata->archentries[i].cputype = bfd_getb32 (buf);
1846 adata->archentries[i].cpusubtype = bfd_getb32 (buf + 4);
1847 adata->archentries[i].offset = bfd_getb32 (buf + 8);
1848 adata->archentries[i].size = bfd_getb32 (buf + 12);
1849 adata->archentries[i].align = bfd_getb32 (buf + 16);
1850 adata->archentries[i].abfd = NULL;
1853 abfd->tdata.mach_o_fat_data = adata;
1858 bfd_release (abfd, adata);
1859 bfd_set_error (bfd_error_wrong_format);
1864 bfd_mach_o_openr_next_archived_file (archive, prev)
1868 mach_o_fat_data_struct *adata;
1869 mach_o_fat_archentry *entry = NULL;
1872 adata = (mach_o_fat_data_struct *) archive->tdata.mach_o_fat_data;
1873 BFD_ASSERT (adata != NULL);
1875 /* Find index of previous entry. */
1877 i = 0; /* Start at first one. */
1880 for (i = 0; i < adata->nfat_arch; i++)
1882 if (adata->archentries[i].abfd == prev)
1886 if (i == adata->nfat_arch)
1889 bfd_set_error (bfd_error_bad_value);
1892 i++; /* Get next entry. */
1895 if (i >= adata->nfat_arch)
1897 bfd_set_error (bfd_error_no_more_archived_files);
1901 entry = &adata->archentries[i];
1902 if (entry->abfd == NULL)
1904 bfd *nbfd = _bfd_new_bfd_contained_in (archive);
1910 nbfd->origin = entry->offset;
1911 s = bfd_malloc (strlen (archive->filename) + 1);
1914 strcpy (s, archive->filename);
1916 nbfd->iostream = NULL;
1924 bfd_mach_o_lookup_section (abfd, section, mcommand, msection)
1927 bfd_mach_o_load_command **mcommand;
1928 bfd_mach_o_section **msection;
1930 struct mach_o_data_struct *md = abfd->tdata.mach_o_data;
1931 unsigned int i, j, num;
1933 bfd_mach_o_load_command *ncmd = NULL;
1934 bfd_mach_o_section *nsect = NULL;
1936 BFD_ASSERT (mcommand != NULL);
1937 BFD_ASSERT (msection != NULL);
1940 for (i = 0; i < md->header.ncmds; i++)
1942 struct bfd_mach_o_load_command *cmd = &md->commands[i];
1943 struct bfd_mach_o_segment_command *seg = NULL;
1945 if (cmd->type != BFD_MACH_O_LC_SEGMENT)
1947 seg = &cmd->command.segment;
1949 if (seg->segment == section)
1956 for (j = 0; j < seg->nsects; j++)
1958 struct bfd_mach_o_section *sect = &seg->sections[j];
1960 if (sect->bfdsection == section)
1975 bfd_mach_o_lookup_command (abfd, type, mcommand)
1977 bfd_mach_o_load_command_type type;
1978 bfd_mach_o_load_command **mcommand;
1980 struct mach_o_data_struct *md = NULL;
1981 bfd_mach_o_load_command *ncmd = NULL;
1982 unsigned int i, num;
1984 md = abfd->tdata.mach_o_data;
1986 BFD_ASSERT (md != NULL);
1987 BFD_ASSERT (mcommand != NULL);
1990 for (i = 0; i < md->header.ncmds; i++)
1992 struct bfd_mach_o_load_command *cmd = &md->commands[i];
1994 if (cmd->type != type)
2007 bfd_mach_o_stack_addr (type)
2008 enum bfd_mach_o_cpu_type type;
2012 case BFD_MACH_O_CPU_TYPE_MC680x0:
2014 case BFD_MACH_O_CPU_TYPE_MC88000:
2016 case BFD_MACH_O_CPU_TYPE_POWERPC:
2018 case BFD_MACH_O_CPU_TYPE_I386:
2020 case BFD_MACH_O_CPU_TYPE_SPARC:
2022 case BFD_MACH_O_CPU_TYPE_I860:
2024 case BFD_MACH_O_CPU_TYPE_HPPA:
2025 return 0xc0000000 - 0x04000000;
2032 bfd_mach_o_core_fetch_environment (abfd, rbuf, rlen)
2034 unsigned char **rbuf;
2037 bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
2038 unsigned long stackaddr = bfd_mach_o_stack_addr (mdata->header.cputype);
2041 for (i = 0; i < mdata->header.ncmds; i++)
2043 bfd_mach_o_load_command *cur = &mdata->commands[i];
2044 bfd_mach_o_segment_command *seg = NULL;
2046 if (cur->type != BFD_MACH_O_LC_SEGMENT)
2049 seg = &cur->command.segment;
2051 if ((seg->vmaddr + seg->vmsize) == stackaddr)
2053 unsigned long start = seg->fileoff;
2054 unsigned long end = seg->fileoff + seg->filesize;
2055 unsigned char *buf = bfd_malloc (1024);
2056 unsigned long size = 1024;
2060 bfd_size_type nread = 0;
2061 unsigned long offset;
2062 int found_nonnull = 0;
2064 if (size > (end - start))
2065 size = (end - start);
2067 buf = bfd_realloc (buf, size);
2069 bfd_seek (abfd, end - size, SEEK_SET);
2070 nread = bfd_bread (buf, size, abfd);
2075 for (offset = 4; offset <= size; offset += 4)
2079 val = *((unsigned long *) (buf + size - offset));
2080 if (! found_nonnull)
2085 else if (val == 0x0)
2087 unsigned long bottom;
2090 bottom = seg->fileoff + seg->filesize - offset;
2091 top = seg->fileoff + seg->filesize - 4;
2092 *rbuf = bfd_malloc (top - bottom);
2093 *rlen = top - bottom;
2095 memcpy (*rbuf, buf + size - *rlen, *rlen);
2100 if (size == (end - start))
2112 bfd_mach_o_core_file_failing_command (abfd)
2115 unsigned char *buf = NULL;
2116 unsigned int len = 0;
2119 ret = bfd_mach_o_core_fetch_environment (abfd, &buf, &len);
2123 return (char *) buf;
2127 bfd_mach_o_core_file_failing_signal (abfd)
2128 bfd *abfd ATTRIBUTE_UNUSED;
2134 bfd_mach_o_core_file_matches_executable_p (core_bfd, exec_bfd)
2135 bfd *core_bfd ATTRIBUTE_UNUSED;
2136 bfd *exec_bfd ATTRIBUTE_UNUSED;
2141 #define TARGET_NAME mach_o_be_vec
2142 #define TARGET_STRING "mach-o-be"
2143 #define TARGET_BIG_ENDIAN 1
2144 #define TARGET_ARCHIVE 0
2146 #include "mach-o-target.c"
2149 #undef TARGET_STRING
2150 #undef TARGET_BIG_ENDIAN
2151 #undef TARGET_ARCHIVE
2153 #define TARGET_NAME mach_o_le_vec
2154 #define TARGET_STRING "mach-o-le"
2155 #define TARGET_BIG_ENDIAN 0
2156 #define TARGET_ARCHIVE 0
2158 #include "mach-o-target.c"
2161 #undef TARGET_STRING
2162 #undef TARGET_BIG_ENDIAN
2163 #undef TARGET_ARCHIVE
2165 #define TARGET_NAME mach_o_fat_vec
2166 #define TARGET_STRING "mach-o-fat"
2167 #define TARGET_BIG_ENDIAN 1
2168 #define TARGET_ARCHIVE 1
2170 #include "mach-o-target.c"
2173 #undef TARGET_STRING
2174 #undef TARGET_BIG_ENDIAN
2175 #undef TARGET_ARCHIVE