1 /* Mach-O support for BFD.
2 Copyright 1999, 2000, 2001, 2002, 2003, 2004
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)
383 for (i = 0; i < mdata->header.ncmds; i++)
385 bfd_mach_o_load_command *cur = &mdata->commands[i];
386 if (cur->type != BFD_MACH_O_LC_SEGMENT)
390 bfd_mach_o_segment_command *seg = &cur->command.segment;
392 bfd_vma nbytes = seg->filesize;
393 bfd_vma curoff = seg->fileoff;
397 bfd_vma thisread = nbytes;
402 bfd_seek (abfd, curoff, SEEK_SET);
403 if (bfd_bread ((PTR) buf, thisread, abfd) != thisread)
406 bfd_seek (abfd, curoff, SEEK_SET);
407 if (bfd_bwrite ((PTR) buf, thisread, abfd) != thisread)
417 /* Now write header information. */
418 if (bfd_mach_o_write_header (abfd, &mdata->header) != 0)
421 for (i = 0; i < mdata->header.ncmds; i++)
423 unsigned char buf[8];
424 bfd_mach_o_load_command *cur = &mdata->commands[i];
425 unsigned long typeflag;
427 typeflag = cur->type_required ? cur->type & BFD_MACH_O_LC_REQ_DYLD : cur->type;
429 bfd_h_put_32 (abfd, typeflag, buf);
430 bfd_h_put_32 (abfd, cur->len, buf + 4);
432 bfd_seek (abfd, cur->offset, SEEK_SET);
433 if (bfd_bwrite ((PTR) buf, 8, abfd) != 8)
438 case BFD_MACH_O_LC_SEGMENT:
439 if (bfd_mach_o_scan_write_segment (abfd, cur) != 0)
442 case BFD_MACH_O_LC_SYMTAB:
443 if (bfd_mach_o_scan_write_symtab (abfd, cur) != 0)
446 case BFD_MACH_O_LC_SYMSEG:
448 case BFD_MACH_O_LC_THREAD:
449 case BFD_MACH_O_LC_UNIXTHREAD:
450 if (bfd_mach_o_scan_write_thread (abfd, cur) != 0)
453 case BFD_MACH_O_LC_LOADFVMLIB:
454 case BFD_MACH_O_LC_IDFVMLIB:
455 case BFD_MACH_O_LC_IDENT:
456 case BFD_MACH_O_LC_FVMFILE:
457 case BFD_MACH_O_LC_PREPAGE:
458 case BFD_MACH_O_LC_DYSYMTAB:
459 case BFD_MACH_O_LC_LOAD_DYLIB:
460 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB:
461 case BFD_MACH_O_LC_ID_DYLIB:
462 case BFD_MACH_O_LC_LOAD_DYLINKER:
463 case BFD_MACH_O_LC_ID_DYLINKER:
464 case BFD_MACH_O_LC_PREBOUND_DYLIB:
465 case BFD_MACH_O_LC_ROUTINES:
466 case BFD_MACH_O_LC_SUB_FRAMEWORK:
470 "unable to write unknown load command 0x%lx\n",
480 bfd_mach_o_sizeof_headers (a, b)
481 bfd *a ATTRIBUTE_UNUSED;
482 bfd_boolean b ATTRIBUTE_UNUSED;
487 /* Make an empty symbol. This is required only because
488 bfd_make_section_anyway wants to create a symbol for the section. */
491 bfd_mach_o_make_empty_symbol (abfd)
496 new = (asymbol *) bfd_zalloc (abfd, sizeof (asymbol));
504 bfd_mach_o_write_header (abfd, header)
506 bfd_mach_o_header *header;
508 unsigned char buf[28];
510 bfd_h_put_32 (abfd, header->magic, buf + 0);
511 bfd_h_put_32 (abfd, header->cputype, buf + 4);
512 bfd_h_put_32 (abfd, header->cpusubtype, buf + 8);
513 bfd_h_put_32 (abfd, header->filetype, buf + 12);
514 bfd_h_put_32 (abfd, header->ncmds, buf + 16);
515 bfd_h_put_32 (abfd, header->sizeofcmds, buf + 20);
516 bfd_h_put_32 (abfd, header->flags, buf + 24);
518 bfd_seek (abfd, 0, SEEK_SET);
519 if (bfd_bwrite ((PTR) buf, 28, abfd) != 28)
526 bfd_mach_o_read_header (abfd, header)
528 bfd_mach_o_header *header;
530 unsigned char buf[28];
531 bfd_vma (*get32) (const void *) = NULL;
533 bfd_seek (abfd, 0, SEEK_SET);
535 if (bfd_bread ((PTR) buf, 28, abfd) != 28)
538 if (bfd_getb32 (buf) == 0xfeedface)
540 header->byteorder = BFD_ENDIAN_BIG;
541 header->magic = 0xfeedface;
544 else if (bfd_getl32 (buf) == 0xfeedface)
546 header->byteorder = BFD_ENDIAN_LITTLE;
547 header->magic = 0xfeedface;
552 header->byteorder = BFD_ENDIAN_UNKNOWN;
556 header->cputype = (*get32) (buf + 4);
557 header->cpusubtype = (*get32) (buf + 8);
558 header->filetype = (*get32) (buf + 12);
559 header->ncmds = (*get32) (buf + 16);
560 header->sizeofcmds = (*get32) (buf + 20);
561 header->flags = (*get32) (buf + 24);
567 bfd_mach_o_make_bfd_section (abfd, section)
569 bfd_mach_o_section *section;
573 const char *prefix = "LC_SEGMENT";
574 unsigned int snamelen;
576 snamelen = strlen (prefix) + 1
577 + strlen (section->segname) + 1
578 + strlen (section->sectname) + 1;
580 sname = (char *) bfd_alloc (abfd, snamelen);
583 sprintf (sname, "%s.%s.%s", prefix, section->segname, section->sectname);
585 bfdsec = bfd_make_section_anyway (abfd, sname);
589 bfdsec->vma = section->addr;
590 bfdsec->lma = section->addr;
591 bfdsec->size = section->size;
592 bfdsec->filepos = section->offset;
593 bfdsec->alignment_power = section->align;
595 if (section->flags & BFD_MACH_O_S_ZEROFILL)
596 bfdsec->flags = SEC_ALLOC;
598 bfdsec->flags = SEC_HAS_CONTENTS | SEC_LOAD | SEC_ALLOC | SEC_CODE;
604 bfd_mach_o_scan_read_section (abfd, section, offset)
606 bfd_mach_o_section *section;
609 unsigned char buf[68];
611 bfd_seek (abfd, offset, SEEK_SET);
612 if (bfd_bread ((PTR) buf, 68, abfd) != 68)
615 memcpy (section->sectname, buf, 16);
616 section->sectname[16] = '\0';
617 memcpy (section->segname, buf + 16, 16);
618 section->segname[16] = '\0';
619 section->addr = bfd_h_get_32 (abfd, buf + 32);
620 section->size = bfd_h_get_32 (abfd, buf + 36);
621 section->offset = bfd_h_get_32 (abfd, buf + 40);
622 section->align = bfd_h_get_32 (abfd, buf + 44);
623 section->reloff = bfd_h_get_32 (abfd, buf + 48);
624 section->nreloc = bfd_h_get_32 (abfd, buf + 52);
625 section->flags = bfd_h_get_32 (abfd, buf + 56);
626 section->reserved1 = bfd_h_get_32 (abfd, buf + 60);
627 section->reserved2 = bfd_h_get_32 (abfd, buf + 64);
628 section->bfdsection = bfd_mach_o_make_bfd_section (abfd, section);
630 if (section->bfdsection == NULL)
637 bfd_mach_o_scan_write_section (abfd, section, offset)
639 bfd_mach_o_section *section;
642 unsigned char buf[68];
644 memcpy (buf, section->sectname, 16);
645 memcpy (buf + 16, section->segname, 16);
646 bfd_h_put_32 (abfd, section->addr, buf + 32);
647 bfd_h_put_32 (abfd, section->size, buf + 36);
648 bfd_h_put_32 (abfd, section->offset, buf + 40);
649 bfd_h_put_32 (abfd, section->align, buf + 44);
650 bfd_h_put_32 (abfd, section->reloff, buf + 48);
651 bfd_h_put_32 (abfd, section->nreloc, buf + 52);
652 bfd_h_put_32 (abfd, section->flags, buf + 56);
653 /* bfd_h_put_32 (abfd, section->reserved1, buf + 60); */
654 /* bfd_h_put_32 (abfd, section->reserved2, buf + 64); */
656 bfd_seek (abfd, offset, SEEK_SET);
657 if (bfd_bwrite ((PTR) buf, 68, abfd) != 68)
664 bfd_mach_o_scan_write_symtab_symbols (abfd, command)
666 bfd_mach_o_load_command *command;
668 bfd_mach_o_symtab_command *sym = &command->command.symtab;
672 for (i = 0; i < sym->nsyms; i++)
674 unsigned char buf[12];
675 bfd_vma symoff = sym->symoff + (i * 12);
676 unsigned char ntype = 0;
677 unsigned char nsect = 0;
680 s = &sym->symbols[i];
682 /* Don't set this from the symbol information; use stored values. */
684 if (s->flags & BSF_GLOBAL)
686 if (s->flags & BSF_DEBUGGING)
689 if (s->section == bfd_und_section_ptr)
691 else if (s->section == bfd_abs_section_ptr)
697 /* Instead just set from the stored values. */
698 ntype = (s->udata.i >> 24) & 0xff;
699 nsect = (s->udata.i >> 16) & 0xff;
700 ndesc = s->udata.i & 0xffff;
702 bfd_h_put_32 (abfd, s->name - sym->strtab, buf);
703 bfd_h_put_8 (abfd, ntype, buf + 4);
704 bfd_h_put_8 (abfd, nsect, buf + 5);
705 bfd_h_put_16 (abfd, ndesc, buf + 6);
706 bfd_h_put_32 (abfd, s->section->vma + s->value, buf + 8);
708 bfd_seek (abfd, symoff, SEEK_SET);
709 if (bfd_bwrite ((PTR) buf, 12, abfd) != 12)
711 fprintf (stderr, "bfd_mach_o_scan_write_symtab_symbols: unable to write %d bytes at %lu\n",
712 12, (unsigned long) symoff);
721 bfd_mach_o_scan_read_symtab_symbol (abfd, sym, s, i)
723 bfd_mach_o_symtab_command *sym;
727 bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
728 bfd_vma symoff = sym->symoff + (i * 12);
729 unsigned char buf[12];
730 unsigned char type = -1;
731 unsigned char section = -1;
733 unsigned long value = -1;
734 unsigned long stroff = -1;
735 unsigned int symtype = -1;
737 BFD_ASSERT (sym->strtab != NULL);
739 bfd_seek (abfd, symoff, SEEK_SET);
740 if (bfd_bread ((PTR) buf, 12, abfd) != 12)
742 fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: unable to read %d bytes at %lu\n",
743 12, (unsigned long) symoff);
747 stroff = bfd_h_get_32 (abfd, buf);
748 type = bfd_h_get_8 (abfd, buf + 4);
749 symtype = (type & 0x0e);
750 section = bfd_h_get_8 (abfd, buf + 5) - 1;
751 desc = bfd_h_get_16 (abfd, buf + 6);
752 value = bfd_h_get_32 (abfd, buf + 8);
754 if (stroff >= sym->strsize)
756 fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: symbol name out of range (%lu >= %lu)\n",
757 (unsigned long) stroff, (unsigned long) sym->strsize);
762 s->name = sym->strtab + stroff;
764 s->udata.i = (type << 24) | (section << 16) | desc;
767 if (type & BFD_MACH_O_N_STAB)
769 s->flags |= BSF_DEBUGGING;
770 s->section = bfd_und_section_ptr;
774 if (type & BFD_MACH_O_N_PEXT)
776 type &= ~BFD_MACH_O_N_PEXT;
777 s->flags |= BSF_GLOBAL;
780 if (type & BFD_MACH_O_N_EXT)
782 type &= ~BFD_MACH_O_N_EXT;
783 s->flags |= BSF_GLOBAL;
788 case BFD_MACH_O_N_UNDF:
789 s->section = bfd_und_section_ptr;
791 case BFD_MACH_O_N_PBUD:
792 s->section = bfd_und_section_ptr;
794 case BFD_MACH_O_N_ABS:
795 s->section = bfd_abs_section_ptr;
797 case BFD_MACH_O_N_SECT:
798 if ((section > 0) && (section <= mdata->nsects))
800 s->section = mdata->sections[section - 1]->bfdsection;
801 s->value = s->value - mdata->sections[section - 1]->addr;
805 /* Mach-O uses 0 to mean "no section"; not an error. */
808 fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: "
809 "symbol \"%s\" specified invalid section %d (max %lu): setting to undefined\n",
810 s->name, section, mdata->nsects);
812 s->section = bfd_und_section_ptr;
815 case BFD_MACH_O_N_INDR:
816 fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: "
817 "symbol \"%s\" is unsupported 'indirect' reference: setting to undefined\n",
819 s->section = bfd_und_section_ptr;
822 fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: "
823 "symbol \"%s\" specified invalid type field 0x%x: setting to undefined\n",
825 s->section = bfd_und_section_ptr;
834 bfd_mach_o_scan_read_symtab_strtab (abfd, sym)
836 bfd_mach_o_symtab_command *sym;
838 BFD_ASSERT (sym->strtab == NULL);
840 if (abfd->flags & BFD_IN_MEMORY)
842 struct bfd_in_memory *b;
844 b = (struct bfd_in_memory *) abfd->iostream;
846 if ((sym->stroff + sym->strsize) > b->size)
848 bfd_set_error (bfd_error_file_truncated);
851 sym->strtab = b->buffer + sym->stroff;
855 sym->strtab = bfd_alloc (abfd, sym->strsize);
856 if (sym->strtab == NULL)
859 bfd_seek (abfd, sym->stroff, SEEK_SET);
860 if (bfd_bread ((PTR) sym->strtab, sym->strsize, abfd) != sym->strsize)
862 fprintf (stderr, "bfd_mach_o_scan_read_symtab_strtab: unable to read %lu bytes at %lu\n",
863 sym->strsize, sym->stroff);
871 bfd_mach_o_scan_read_symtab_symbols (abfd, sym)
873 bfd_mach_o_symtab_command *sym;
878 BFD_ASSERT (sym->symbols == NULL);
879 sym->symbols = bfd_alloc (abfd, sym->nsyms * sizeof (asymbol));
881 if (sym->symbols == NULL)
883 fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbols: unable to allocate memory for symbols\n");
887 ret = bfd_mach_o_scan_read_symtab_strtab (abfd, sym);
891 for (i = 0; i < sym->nsyms; i++)
893 ret = bfd_mach_o_scan_read_symtab_symbol (abfd, sym, &sym->symbols[i], i);
902 bfd_mach_o_scan_read_dysymtab_symbol (abfd, dysym, sym, s, i)
904 bfd_mach_o_dysymtab_command *dysym;
905 bfd_mach_o_symtab_command *sym;
909 unsigned long isymoff = dysym->indirectsymoff + (i * 4);
910 unsigned long symindex;
911 unsigned char buf[4];
913 BFD_ASSERT (i < dysym->nindirectsyms);
915 bfd_seek (abfd, isymoff, SEEK_SET);
916 if (bfd_bread ((PTR) buf, 4, abfd) != 4)
918 fprintf (stderr, "bfd_mach_o_scan_read_dysymtab_symbol: unable to read %lu bytes at %lu\n",
919 (unsigned long) 4, isymoff);
922 symindex = bfd_h_get_32 (abfd, buf);
924 return bfd_mach_o_scan_read_symtab_symbol (abfd, sym, s, symindex);
928 bfd_mach_o_i386_flavour_string (flavour)
929 unsigned int flavour;
931 switch ((int) flavour)
933 case BFD_MACH_O_i386_NEW_THREAD_STATE: return "i386_NEW_THREAD_STATE";
934 case BFD_MACH_O_i386_FLOAT_STATE: return "i386_FLOAT_STATE";
935 case BFD_MACH_O_i386_ISA_PORT_MAP_STATE: return "i386_ISA_PORT_MAP_STATE";
936 case BFD_MACH_O_i386_V86_ASSIST_STATE: return "i386_V86_ASSIST_STATE";
937 case BFD_MACH_O_i386_REGS_SEGS_STATE: return "i386_REGS_SEGS_STATE";
938 case BFD_MACH_O_i386_THREAD_SYSCALL_STATE: return "i386_THREAD_SYSCALL_STATE";
939 case BFD_MACH_O_i386_THREAD_STATE_NONE: return "i386_THREAD_STATE_NONE";
940 case BFD_MACH_O_i386_SAVED_STATE: return "i386_SAVED_STATE";
941 case BFD_MACH_O_i386_THREAD_STATE: return "i386_THREAD_STATE";
942 case BFD_MACH_O_i386_THREAD_FPSTATE: return "i386_THREAD_FPSTATE";
943 case BFD_MACH_O_i386_THREAD_EXCEPTSTATE: return "i386_THREAD_EXCEPTSTATE";
944 case BFD_MACH_O_i386_THREAD_CTHREADSTATE: return "i386_THREAD_CTHREADSTATE";
945 default: return "UNKNOWN";
950 bfd_mach_o_ppc_flavour_string (flavour)
951 unsigned int flavour;
953 switch ((int) flavour)
955 case BFD_MACH_O_PPC_THREAD_STATE: return "PPC_THREAD_STATE";
956 case BFD_MACH_O_PPC_FLOAT_STATE: return "PPC_FLOAT_STATE";
957 case BFD_MACH_O_PPC_EXCEPTION_STATE: return "PPC_EXCEPTION_STATE";
958 case BFD_MACH_O_PPC_VECTOR_STATE: return "PPC_VECTOR_STATE";
959 default: return "UNKNOWN";
964 bfd_mach_o_scan_write_thread (abfd, command)
966 bfd_mach_o_load_command *command;
968 bfd_mach_o_thread_command *cmd = &command->command.thread;
970 unsigned char buf[8];
972 unsigned int nflavours;
974 BFD_ASSERT ((command->type == BFD_MACH_O_LC_THREAD)
975 || (command->type == BFD_MACH_O_LC_UNIXTHREAD));
979 for (i = 0; i < cmd->nflavours; i++)
981 BFD_ASSERT ((cmd->flavours[i].size % 4) == 0);
982 BFD_ASSERT (cmd->flavours[i].offset == (command->offset + offset + 8));
984 bfd_h_put_32 (abfd, cmd->flavours[i].flavour, buf);
985 bfd_h_put_32 (abfd, (cmd->flavours[i].size / 4), buf + 4);
987 bfd_seek (abfd, command->offset + offset, SEEK_SET);
988 if (bfd_bwrite ((PTR) buf, 8, abfd) != 8)
991 offset += cmd->flavours[i].size + 8;
998 bfd_mach_o_scan_read_dylinker (abfd, command)
1000 bfd_mach_o_load_command *command;
1002 bfd_mach_o_dylinker_command *cmd = &command->command.dylinker;
1003 unsigned char buf[4];
1004 unsigned int nameoff;
1009 BFD_ASSERT ((command->type == BFD_MACH_O_LC_ID_DYLINKER)
1010 || (command->type == BFD_MACH_O_LC_LOAD_DYLINKER));
1012 bfd_seek (abfd, command->offset + 8, SEEK_SET);
1013 if (bfd_bread ((PTR) buf, 4, abfd) != 4)
1016 nameoff = bfd_h_get_32 (abfd, buf + 0);
1018 cmd->name_offset = command->offset + nameoff;
1019 cmd->name_len = command->len - nameoff;
1021 if (command->type == BFD_MACH_O_LC_LOAD_DYLINKER)
1022 prefix = "LC_LOAD_DYLINKER";
1023 else if (command->type == BFD_MACH_O_LC_ID_DYLINKER)
1024 prefix = "LC_ID_DYLINKER";
1028 sname = (char *) bfd_alloc (abfd, strlen (prefix) + 1);
1031 strcpy (sname, prefix);
1033 bfdsec = bfd_make_section_anyway (abfd, sname);
1039 bfdsec->size = command->len - 8;
1040 bfdsec->filepos = command->offset + 8;
1041 bfdsec->alignment_power = 0;
1042 bfdsec->flags = SEC_HAS_CONTENTS;
1044 cmd->section = bfdsec;
1050 bfd_mach_o_scan_read_dylib (abfd, command)
1052 bfd_mach_o_load_command *command;
1054 bfd_mach_o_dylib_command *cmd = &command->command.dylib;
1055 unsigned char buf[16];
1056 unsigned int nameoff;
1061 BFD_ASSERT ((command->type == BFD_MACH_O_LC_ID_DYLIB)
1062 || (command->type == BFD_MACH_O_LC_LOAD_DYLIB)
1063 || (command->type == BFD_MACH_O_LC_LOAD_WEAK_DYLIB));
1065 bfd_seek (abfd, command->offset + 8, SEEK_SET);
1066 if (bfd_bread ((PTR) buf, 16, abfd) != 16)
1069 nameoff = bfd_h_get_32 (abfd, buf + 0);
1070 cmd->timestamp = bfd_h_get_32 (abfd, buf + 4);
1071 cmd->current_version = bfd_h_get_32 (abfd, buf + 8);
1072 cmd->compatibility_version = bfd_h_get_32 (abfd, buf + 12);
1074 cmd->name_offset = command->offset + nameoff;
1075 cmd->name_len = command->len - nameoff;
1077 if (command->type == BFD_MACH_O_LC_LOAD_DYLIB)
1078 prefix = "LC_LOAD_DYLIB";
1079 else if (command->type == BFD_MACH_O_LC_LOAD_WEAK_DYLIB)
1080 prefix = "LC_LOAD_WEAK_DYLIB";
1081 else if (command->type == BFD_MACH_O_LC_ID_DYLIB)
1082 prefix = "LC_ID_DYLIB";
1086 sname = (char *) bfd_alloc (abfd, strlen (prefix) + 1);
1089 strcpy (sname, prefix);
1091 bfdsec = bfd_make_section_anyway (abfd, sname);
1097 bfdsec->size = command->len - 8;
1098 bfdsec->filepos = command->offset + 8;
1099 bfdsec->alignment_power = 0;
1100 bfdsec->flags = SEC_HAS_CONTENTS;
1102 cmd->section = bfdsec;
1108 bfd_mach_o_scan_read_prebound_dylib (abfd, command)
1109 bfd *abfd ATTRIBUTE_UNUSED;
1110 bfd_mach_o_load_command *command ATTRIBUTE_UNUSED;
1112 /* bfd_mach_o_prebound_dylib_command *cmd = &command->command.prebound_dylib; */
1114 BFD_ASSERT (command->type == BFD_MACH_O_LC_PREBOUND_DYLIB);
1119 bfd_mach_o_scan_read_thread (abfd, command)
1121 bfd_mach_o_load_command *command;
1123 bfd_mach_o_data_struct *mdata = NULL;
1124 bfd_mach_o_thread_command *cmd = &command->command.thread;
1125 unsigned char buf[8];
1127 unsigned int nflavours;
1130 BFD_ASSERT ((command->type == BFD_MACH_O_LC_THREAD)
1131 || (command->type == BFD_MACH_O_LC_UNIXTHREAD));
1133 BFD_ASSERT (bfd_mach_o_valid (abfd));
1134 mdata = abfd->tdata.mach_o_data;
1138 while (offset != command->len)
1140 if (offset >= command->len)
1143 bfd_seek (abfd, command->offset + offset, SEEK_SET);
1145 if (bfd_bread ((PTR) buf, 8, abfd) != 8)
1148 offset += 8 + bfd_h_get_32 (abfd, buf + 4) * 4;
1153 ((bfd_mach_o_thread_flavour *)
1154 bfd_alloc (abfd, nflavours * sizeof (bfd_mach_o_thread_flavour)));
1155 if (cmd->flavours == NULL)
1157 cmd->nflavours = nflavours;
1161 while (offset != command->len)
1163 if (offset >= command->len)
1166 if (nflavours >= cmd->nflavours)
1169 bfd_seek (abfd, command->offset + offset, SEEK_SET);
1171 if (bfd_bread ((PTR) buf, 8, abfd) != 8)
1174 cmd->flavours[nflavours].flavour = bfd_h_get_32 (abfd, buf);
1175 cmd->flavours[nflavours].offset = command->offset + offset + 8;
1176 cmd->flavours[nflavours].size = bfd_h_get_32 (abfd, buf + 4) * 4;
1177 offset += cmd->flavours[nflavours].size + 8;
1181 for (i = 0; i < nflavours; i++)
1184 unsigned int snamelen;
1186 const char *flavourstr;
1187 const char *prefix = "LC_THREAD";
1190 switch (mdata->header.cputype)
1192 case BFD_MACH_O_CPU_TYPE_POWERPC:
1193 flavourstr = bfd_mach_o_ppc_flavour_string (cmd->flavours[i].flavour);
1195 case BFD_MACH_O_CPU_TYPE_I386:
1196 flavourstr = bfd_mach_o_i386_flavour_string (cmd->flavours[i].flavour);
1199 flavourstr = "UNKNOWN_ARCHITECTURE";
1203 snamelen = strlen (prefix) + 1 + 20 + 1 + strlen (flavourstr) + 1;
1204 sname = (char *) bfd_alloc (abfd, snamelen);
1210 sprintf (sname, "%s.%s.%u", prefix, flavourstr, j);
1211 if (bfd_get_section_by_name (abfd, sname) == NULL)
1216 bfdsec = bfd_make_section (abfd, sname);
1220 bfdsec->size = cmd->flavours[i].size;
1221 bfdsec->filepos = cmd->flavours[i].offset;
1222 bfdsec->alignment_power = 0x0;
1223 bfdsec->flags = SEC_HAS_CONTENTS;
1225 cmd->section = bfdsec;
1232 bfd_mach_o_scan_write_symtab (abfd, command)
1234 bfd_mach_o_load_command *command;
1236 bfd_mach_o_symtab_command *seg = &command->command.symtab;
1237 unsigned char buf[16];
1239 BFD_ASSERT (command->type == BFD_MACH_O_LC_SYMTAB);
1241 bfd_h_put_32 (abfd, seg->symoff, buf);
1242 bfd_h_put_32 (abfd, seg->nsyms, buf + 4);
1243 bfd_h_put_32 (abfd, seg->stroff, buf + 8);
1244 bfd_h_put_32 (abfd, seg->strsize, buf + 12);
1246 bfd_seek (abfd, command->offset + 8, SEEK_SET);
1247 if (bfd_bwrite ((PTR) buf, 16, abfd) != 16)
1250 if (bfd_mach_o_scan_write_symtab_symbols (abfd, command) != 0)
1257 bfd_mach_o_scan_read_dysymtab (abfd, command)
1259 bfd_mach_o_load_command *command;
1261 bfd_mach_o_dysymtab_command *seg = &command->command.dysymtab;
1262 unsigned char buf[72];
1264 BFD_ASSERT (command->type == BFD_MACH_O_LC_DYSYMTAB);
1266 bfd_seek (abfd, command->offset + 8, SEEK_SET);
1267 if (bfd_bread ((PTR) buf, 72, abfd) != 72)
1270 seg->ilocalsym = bfd_h_get_32 (abfd, buf + 0);
1271 seg->nlocalsym = bfd_h_get_32 (abfd, buf + 4);
1272 seg->iextdefsym = bfd_h_get_32 (abfd, buf + 8);
1273 seg->nextdefsym = bfd_h_get_32 (abfd, buf + 12);
1274 seg->iundefsym = bfd_h_get_32 (abfd, buf + 16);
1275 seg->nundefsym = bfd_h_get_32 (abfd, buf + 20);
1276 seg->tocoff = bfd_h_get_32 (abfd, buf + 24);
1277 seg->ntoc = bfd_h_get_32 (abfd, buf + 28);
1278 seg->modtaboff = bfd_h_get_32 (abfd, buf + 32);
1279 seg->nmodtab = bfd_h_get_32 (abfd, buf + 36);
1280 seg->extrefsymoff = bfd_h_get_32 (abfd, buf + 40);
1281 seg->nextrefsyms = bfd_h_get_32 (abfd, buf + 44);
1282 seg->indirectsymoff = bfd_h_get_32 (abfd, buf + 48);
1283 seg->nindirectsyms = bfd_h_get_32 (abfd, buf + 52);
1284 seg->extreloff = bfd_h_get_32 (abfd, buf + 56);
1285 seg->nextrel = bfd_h_get_32 (abfd, buf + 60);
1286 seg->locreloff = bfd_h_get_32 (abfd, buf + 64);
1287 seg->nlocrel = bfd_h_get_32 (abfd, buf + 68);
1293 bfd_mach_o_scan_read_symtab (abfd, command)
1295 bfd_mach_o_load_command *command;
1297 bfd_mach_o_symtab_command *seg = &command->command.symtab;
1298 unsigned char buf[16];
1301 const char *prefix = "LC_SYMTAB.stabs";
1303 BFD_ASSERT (command->type == BFD_MACH_O_LC_SYMTAB);
1305 bfd_seek (abfd, command->offset + 8, SEEK_SET);
1306 if (bfd_bread ((PTR) buf, 16, abfd) != 16)
1309 seg->symoff = bfd_h_get_32 (abfd, buf);
1310 seg->nsyms = bfd_h_get_32 (abfd, buf + 4);
1311 seg->stroff = bfd_h_get_32 (abfd, buf + 8);
1312 seg->strsize = bfd_h_get_32 (abfd, buf + 12);
1313 seg->symbols = NULL;
1316 sname = (char *) bfd_alloc (abfd, strlen (prefix) + 1);
1319 strcpy (sname, prefix);
1321 bfdsec = bfd_make_section_anyway (abfd, sname);
1327 bfdsec->size = seg->nsyms * 12;
1328 bfdsec->filepos = seg->symoff;
1329 bfdsec->alignment_power = 0;
1330 bfdsec->flags = SEC_HAS_CONTENTS;
1332 seg->stabs_segment = bfdsec;
1334 prefix = "LC_SYMTAB.stabstr";
1335 sname = (char *) bfd_alloc (abfd, strlen (prefix) + 1);
1338 strcpy (sname, prefix);
1340 bfdsec = bfd_make_section_anyway (abfd, sname);
1346 bfdsec->size = seg->strsize;
1347 bfdsec->filepos = seg->stroff;
1348 bfdsec->alignment_power = 0;
1349 bfdsec->flags = SEC_HAS_CONTENTS;
1351 seg->stabstr_segment = bfdsec;
1357 bfd_mach_o_scan_read_segment (abfd, command)
1359 bfd_mach_o_load_command *command;
1361 unsigned char buf[48];
1362 bfd_mach_o_segment_command *seg = &command->command.segment;
1366 const char *prefix = "LC_SEGMENT";
1367 unsigned int snamelen;
1369 BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT);
1371 bfd_seek (abfd, command->offset + 8, SEEK_SET);
1372 if (bfd_bread ((PTR) buf, 48, abfd) != 48)
1375 memcpy (seg->segname, buf, 16);
1376 seg->vmaddr = bfd_h_get_32 (abfd, buf + 16);
1377 seg->vmsize = bfd_h_get_32 (abfd, buf + 20);
1378 seg->fileoff = bfd_h_get_32 (abfd, buf + 24);
1379 seg->filesize = bfd_h_get_32 (abfd, buf + 28);
1380 /* seg->maxprot = bfd_h_get_32 (abfd, buf + 32); */
1381 /* seg->initprot = bfd_h_get_32 (abfd, buf + 36); */
1382 seg->nsects = bfd_h_get_32 (abfd, buf + 40);
1383 seg->flags = bfd_h_get_32 (abfd, buf + 44);
1385 snamelen = strlen (prefix) + 1 + strlen (seg->segname) + 1;
1386 sname = (char *) bfd_alloc (abfd, snamelen);
1389 sprintf (sname, "%s.%s", prefix, seg->segname);
1391 bfdsec = bfd_make_section_anyway (abfd, sname);
1395 bfdsec->vma = seg->vmaddr;
1396 bfdsec->lma = seg->vmaddr;
1397 bfdsec->size = seg->filesize;
1398 bfdsec->filepos = seg->fileoff;
1399 bfdsec->alignment_power = 0x0;
1400 bfdsec->flags = SEC_HAS_CONTENTS | SEC_LOAD | SEC_ALLOC | SEC_CODE;
1402 seg->segment = bfdsec;
1404 if (seg->nsects != 0)
1407 ((bfd_mach_o_section *)
1408 bfd_alloc (abfd, seg->nsects * sizeof (bfd_mach_o_section)));
1409 if (seg->sections == NULL)
1412 for (i = 0; i < seg->nsects; i++)
1414 bfd_vma segoff = command->offset + 48 + 8 + (i * 68);
1416 if (bfd_mach_o_scan_read_section (abfd, &seg->sections[i],
1426 bfd_mach_o_scan_write_segment (abfd, command)
1428 bfd_mach_o_load_command *command;
1430 unsigned char buf[48];
1431 bfd_mach_o_segment_command *seg = &command->command.segment;
1434 BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT);
1436 memcpy (buf, seg->segname, 16);
1437 bfd_h_put_32 (abfd, seg->vmaddr, buf + 16);
1438 bfd_h_put_32 (abfd, seg->vmsize, buf + 20);
1439 bfd_h_put_32 (abfd, seg->fileoff, buf + 24);
1440 bfd_h_put_32 (abfd, seg->filesize, buf + 28);
1441 bfd_h_put_32 (abfd, 0 /* seg->maxprot */, buf + 32);
1442 bfd_h_put_32 (abfd, 0 /* seg->initprot */, buf + 36);
1443 bfd_h_put_32 (abfd, seg->nsects, buf + 40);
1444 bfd_h_put_32 (abfd, seg->flags, buf + 44);
1446 bfd_seek (abfd, command->offset + 8, SEEK_SET);
1447 if (bfd_bwrite ((PTR) buf, 48, abfd) != 48)
1452 bfd_vma nbytes = seg->filesize;
1453 bfd_vma curoff = seg->fileoff;
1457 bfd_vma thisread = nbytes;
1459 if (thisread > 1024)
1462 bfd_seek (abfd, curoff, SEEK_SET);
1463 if (bfd_bread ((PTR) buf, thisread, abfd) != thisread)
1466 bfd_seek (abfd, curoff, SEEK_SET);
1467 if (bfd_bwrite ((PTR) buf, thisread, abfd) != thisread)
1475 for (i = 0; i < seg->nsects; i++)
1477 bfd_vma segoff = command->offset + 48 + 8 + (i * 68);
1479 if (bfd_mach_o_scan_write_section (abfd, &seg->sections[i], segoff) != 0)
1487 bfd_mach_o_scan_read_command (abfd, command)
1489 bfd_mach_o_load_command *command;
1491 unsigned char buf[8];
1493 bfd_seek (abfd, command->offset, SEEK_SET);
1494 if (bfd_bread ((PTR) buf, 8, abfd) != 8)
1497 command->type = (bfd_h_get_32 (abfd, buf) & ~BFD_MACH_O_LC_REQ_DYLD);
1498 command->type_required = (bfd_h_get_32 (abfd, buf) & BFD_MACH_O_LC_REQ_DYLD
1500 command->len = bfd_h_get_32 (abfd, buf + 4);
1502 switch (command->type)
1504 case BFD_MACH_O_LC_SEGMENT:
1505 if (bfd_mach_o_scan_read_segment (abfd, command) != 0)
1508 case BFD_MACH_O_LC_SYMTAB:
1509 if (bfd_mach_o_scan_read_symtab (abfd, command) != 0)
1512 case BFD_MACH_O_LC_SYMSEG:
1514 case BFD_MACH_O_LC_THREAD:
1515 case BFD_MACH_O_LC_UNIXTHREAD:
1516 if (bfd_mach_o_scan_read_thread (abfd, command) != 0)
1519 case BFD_MACH_O_LC_LOAD_DYLINKER:
1520 case BFD_MACH_O_LC_ID_DYLINKER:
1521 if (bfd_mach_o_scan_read_dylinker (abfd, command) != 0)
1524 case BFD_MACH_O_LC_LOAD_DYLIB:
1525 case BFD_MACH_O_LC_ID_DYLIB:
1526 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB:
1527 if (bfd_mach_o_scan_read_dylib (abfd, command) != 0)
1530 case BFD_MACH_O_LC_PREBOUND_DYLIB:
1531 if (bfd_mach_o_scan_read_prebound_dylib (abfd, command) != 0)
1534 case BFD_MACH_O_LC_LOADFVMLIB:
1535 case BFD_MACH_O_LC_IDFVMLIB:
1536 case BFD_MACH_O_LC_IDENT:
1537 case BFD_MACH_O_LC_FVMFILE:
1538 case BFD_MACH_O_LC_PREPAGE:
1539 case BFD_MACH_O_LC_ROUTINES:
1540 case BFD_MACH_O_LC_SUB_FRAMEWORK:
1542 case BFD_MACH_O_LC_DYSYMTAB:
1543 if (bfd_mach_o_scan_read_dysymtab (abfd, command) != 0)
1546 case BFD_MACH_O_LC_SUB_UMBRELLA:
1547 case BFD_MACH_O_LC_SUB_CLIENT:
1548 case BFD_MACH_O_LC_SUB_LIBRARY:
1549 case BFD_MACH_O_LC_TWOLEVEL_HINTS:
1550 case BFD_MACH_O_LC_PREBIND_CKSUM:
1553 fprintf (stderr, "unable to read unknown load command 0x%lx\n",
1554 (unsigned long) command->type);
1562 bfd_mach_o_flatten_sections (abfd)
1565 bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
1571 for (i = 0; i < mdata->header.ncmds; i++)
1573 if (mdata->commands[i].type == BFD_MACH_O_LC_SEGMENT)
1575 bfd_mach_o_segment_command *seg;
1577 seg = &mdata->commands[i].command.segment;
1578 mdata->nsects += seg->nsects;
1582 mdata->sections = bfd_alloc (abfd,
1583 mdata->nsects * sizeof (bfd_mach_o_section *));
1586 for (i = 0; i < mdata->header.ncmds; i++)
1588 if (mdata->commands[i].type == BFD_MACH_O_LC_SEGMENT)
1590 bfd_mach_o_segment_command *seg;
1592 seg = &mdata->commands[i].command.segment;
1593 BFD_ASSERT (csect + seg->nsects <= mdata->nsects);
1595 for (j = 0; j < seg->nsects; j++)
1596 mdata->sections[csect++] = &seg->sections[j];
1602 bfd_mach_o_scan_start_address (abfd)
1605 bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
1606 bfd_mach_o_thread_command *cmd = NULL;
1609 for (i = 0; i < mdata->header.ncmds; i++)
1611 if ((mdata->commands[i].type == BFD_MACH_O_LC_THREAD) ||
1612 (mdata->commands[i].type == BFD_MACH_O_LC_UNIXTHREAD))
1615 cmd = &mdata->commands[i].command.thread;
1624 for (i = 0; i < cmd->nflavours; i++)
1626 if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_I386)
1627 && (cmd->flavours[i].flavour
1628 == (unsigned long) BFD_MACH_O_i386_THREAD_STATE))
1630 unsigned char buf[4];
1632 bfd_seek (abfd, cmd->flavours[i].offset + 40, SEEK_SET);
1634 if (bfd_bread (buf, 4, abfd) != 4)
1637 abfd->start_address = bfd_h_get_32 (abfd, buf);
1639 else if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_POWERPC)
1640 && (cmd->flavours[i].flavour == BFD_MACH_O_PPC_THREAD_STATE))
1642 unsigned char buf[4];
1644 bfd_seek (abfd, cmd->flavours[i].offset + 0, SEEK_SET);
1646 if (bfd_bread (buf, 4, abfd) != 4)
1649 abfd->start_address = bfd_h_get_32 (abfd, buf);
1657 bfd_mach_o_scan (abfd, header, mdata)
1659 bfd_mach_o_header *header;
1660 bfd_mach_o_data_struct *mdata;
1663 enum bfd_architecture cputype;
1664 unsigned long cpusubtype;
1666 mdata->header = *header;
1667 mdata->symbols = NULL;
1669 abfd->flags = (abfd->xvec->object_flags
1670 | (abfd->flags & (BFD_IN_MEMORY | BFD_IO_FUNCS)));
1671 abfd->tdata.mach_o_data = mdata;
1673 bfd_mach_o_convert_architecture (header->cputype, header->cpusubtype,
1674 &cputype, &cpusubtype);
1675 if (cputype == bfd_arch_unknown)
1677 fprintf (stderr, "bfd_mach_o_scan: unknown architecture 0x%lx/0x%lx\n",
1678 header->cputype, header->cpusubtype);
1682 bfd_set_arch_mach (abfd, cputype, cpusubtype);
1684 if (header->ncmds != 0)
1687 ((bfd_mach_o_load_command *)
1688 bfd_alloc (abfd, header->ncmds * sizeof (bfd_mach_o_load_command)));
1689 if (mdata->commands == NULL)
1692 for (i = 0; i < header->ncmds; i++)
1694 bfd_mach_o_load_command *cur = &mdata->commands[i];
1700 bfd_mach_o_load_command *prev = &mdata->commands[i - 1];
1701 cur->offset = prev->offset + prev->len;
1704 if (bfd_mach_o_scan_read_command (abfd, cur) < 0)
1709 if (bfd_mach_o_scan_start_address (abfd) < 0)
1712 fprintf (stderr, "bfd_mach_o_scan: unable to scan start address: %s\n",
1713 bfd_errmsg (bfd_get_error ()));
1714 abfd->tdata.mach_o_data = NULL;
1719 bfd_mach_o_flatten_sections (abfd);
1725 bfd_mach_o_mkobject (abfd)
1728 bfd_mach_o_data_struct *mdata = NULL;
1730 mdata = ((bfd_mach_o_data_struct *)
1731 bfd_alloc (abfd, sizeof (bfd_mach_o_data_struct)));
1734 abfd->tdata.mach_o_data = mdata;
1736 mdata->header.magic = 0;
1737 mdata->header.cputype = 0;
1738 mdata->header.cpusubtype = 0;
1739 mdata->header.filetype = 0;
1740 mdata->header.ncmds = 0;
1741 mdata->header.sizeofcmds = 0;
1742 mdata->header.flags = 0;
1743 mdata->header.byteorder = BFD_ENDIAN_UNKNOWN;
1744 mdata->commands = NULL;
1745 mdata->nsymbols = 0;
1746 mdata->symbols = NULL;
1748 mdata->sections = NULL;
1755 bfd_mach_o_object_p (abfd)
1758 struct bfd_preserve preserve;
1759 bfd_mach_o_header header;
1761 preserve.marker = NULL;
1762 if (bfd_mach_o_read_header (abfd, &header) != 0)
1765 if (! (header.byteorder == BFD_ENDIAN_BIG
1766 || header.byteorder == BFD_ENDIAN_LITTLE))
1768 fprintf (stderr, "unknown header byte-order value 0x%lx\n",
1769 (long) header.byteorder);
1773 if (! ((header.byteorder == BFD_ENDIAN_BIG
1774 && abfd->xvec->byteorder == BFD_ENDIAN_BIG
1775 && abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
1776 || (header.byteorder == BFD_ENDIAN_LITTLE
1777 && abfd->xvec->byteorder == BFD_ENDIAN_LITTLE
1778 && abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)))
1781 preserve.marker = bfd_zalloc (abfd, sizeof (bfd_mach_o_data_struct));
1782 if (preserve.marker == NULL
1783 || !bfd_preserve_save (abfd, &preserve))
1786 if (bfd_mach_o_scan (abfd, &header,
1787 (bfd_mach_o_data_struct *) preserve.marker) != 0)
1790 bfd_preserve_finish (abfd, &preserve);
1794 bfd_set_error (bfd_error_wrong_format);
1797 if (preserve.marker != NULL)
1798 bfd_preserve_restore (abfd, &preserve);
1803 bfd_mach_o_core_p (abfd)
1806 struct bfd_preserve preserve;
1807 bfd_mach_o_header header;
1809 preserve.marker = NULL;
1810 if (bfd_mach_o_read_header (abfd, &header) != 0)
1813 if (! (header.byteorder == BFD_ENDIAN_BIG
1814 || header.byteorder == BFD_ENDIAN_LITTLE))
1816 fprintf (stderr, "unknown header byte-order value 0x%lx\n",
1817 (long) header.byteorder);
1821 if (! ((header.byteorder == BFD_ENDIAN_BIG
1822 && abfd->xvec->byteorder == BFD_ENDIAN_BIG
1823 && abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
1824 || (header.byteorder == BFD_ENDIAN_LITTLE
1825 && abfd->xvec->byteorder == BFD_ENDIAN_LITTLE
1826 && abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)))
1829 if (header.filetype != BFD_MACH_O_MH_CORE)
1832 preserve.marker = bfd_zalloc (abfd, sizeof (bfd_mach_o_data_struct));
1833 if (preserve.marker == NULL
1834 || !bfd_preserve_save (abfd, &preserve))
1837 if (bfd_mach_o_scan (abfd, &header,
1838 (bfd_mach_o_data_struct *) preserve.marker) != 0)
1841 bfd_preserve_finish (abfd, &preserve);
1845 bfd_set_error (bfd_error_wrong_format);
1848 if (preserve.marker != NULL)
1849 bfd_preserve_restore (abfd, &preserve);
1853 typedef struct mach_o_fat_archentry
1855 unsigned long cputype;
1856 unsigned long cpusubtype;
1857 unsigned long offset;
1859 unsigned long align;
1861 } mach_o_fat_archentry;
1863 typedef struct mach_o_fat_data_struct
1865 unsigned long magic;
1866 unsigned long nfat_arch;
1867 mach_o_fat_archentry *archentries;
1868 } mach_o_fat_data_struct;
1871 bfd_mach_o_archive_p (abfd)
1874 mach_o_fat_data_struct *adata = NULL;
1875 unsigned char buf[20];
1878 bfd_seek (abfd, 0, SEEK_SET);
1879 if (bfd_bread ((PTR) buf, 8, abfd) != 8)
1882 adata = (mach_o_fat_data_struct *)
1883 bfd_alloc (abfd, sizeof (mach_o_fat_data_struct));
1887 adata->magic = bfd_getb32 (buf);
1888 adata->nfat_arch = bfd_getb32 (buf + 4);
1889 if (adata->magic != 0xcafebabe)
1892 adata->archentries = (mach_o_fat_archentry *)
1893 bfd_alloc (abfd, adata->nfat_arch * sizeof (mach_o_fat_archentry));
1894 if (adata->archentries == NULL)
1897 for (i = 0; i < adata->nfat_arch; i++)
1899 bfd_seek (abfd, 8 + 20 * i, SEEK_SET);
1901 if (bfd_bread ((PTR) buf, 20, abfd) != 20)
1903 adata->archentries[i].cputype = bfd_getb32 (buf);
1904 adata->archentries[i].cpusubtype = bfd_getb32 (buf + 4);
1905 adata->archentries[i].offset = bfd_getb32 (buf + 8);
1906 adata->archentries[i].size = bfd_getb32 (buf + 12);
1907 adata->archentries[i].align = bfd_getb32 (buf + 16);
1908 adata->archentries[i].abfd = NULL;
1911 abfd->tdata.mach_o_fat_data = adata;
1916 bfd_release (abfd, adata);
1917 bfd_set_error (bfd_error_wrong_format);
1922 bfd_mach_o_openr_next_archived_file (archive, prev)
1926 mach_o_fat_data_struct *adata;
1927 mach_o_fat_archentry *entry = NULL;
1930 adata = (mach_o_fat_data_struct *) archive->tdata.mach_o_fat_data;
1931 BFD_ASSERT (adata != NULL);
1933 /* Find index of previous entry. */
1935 i = 0; /* Start at first one. */
1938 for (i = 0; i < adata->nfat_arch; i++)
1940 if (adata->archentries[i].abfd == prev)
1944 if (i == adata->nfat_arch)
1947 bfd_set_error (bfd_error_bad_value);
1950 i++; /* Get next entry. */
1953 if (i >= adata->nfat_arch)
1955 bfd_set_error (bfd_error_no_more_archived_files);
1959 entry = &adata->archentries[i];
1960 if (entry->abfd == NULL)
1962 bfd *nbfd = _bfd_new_bfd_contained_in (archive);
1968 nbfd->origin = entry->offset;
1969 s = bfd_malloc (strlen (archive->filename) + 1);
1972 strcpy (s, archive->filename);
1974 nbfd->iostream = NULL;
1982 bfd_mach_o_lookup_section (abfd, section, mcommand, msection)
1985 bfd_mach_o_load_command **mcommand;
1986 bfd_mach_o_section **msection;
1988 struct mach_o_data_struct *md = abfd->tdata.mach_o_data;
1989 unsigned int i, j, num;
1991 bfd_mach_o_load_command *ncmd = NULL;
1992 bfd_mach_o_section *nsect = NULL;
1994 BFD_ASSERT (mcommand != NULL);
1995 BFD_ASSERT (msection != NULL);
1998 for (i = 0; i < md->header.ncmds; i++)
2000 struct bfd_mach_o_load_command *cmd = &md->commands[i];
2001 struct bfd_mach_o_segment_command *seg = NULL;
2003 if (cmd->type != BFD_MACH_O_LC_SEGMENT)
2005 seg = &cmd->command.segment;
2007 if (seg->segment == section)
2014 for (j = 0; j < seg->nsects; j++)
2016 struct bfd_mach_o_section *sect = &seg->sections[j];
2018 if (sect->bfdsection == section)
2033 bfd_mach_o_lookup_command (abfd, type, mcommand)
2035 bfd_mach_o_load_command_type type;
2036 bfd_mach_o_load_command **mcommand;
2038 struct mach_o_data_struct *md = NULL;
2039 bfd_mach_o_load_command *ncmd = NULL;
2040 unsigned int i, num;
2042 md = abfd->tdata.mach_o_data;
2044 BFD_ASSERT (md != NULL);
2045 BFD_ASSERT (mcommand != NULL);
2048 for (i = 0; i < md->header.ncmds; i++)
2050 struct bfd_mach_o_load_command *cmd = &md->commands[i];
2052 if (cmd->type != type)
2065 bfd_mach_o_stack_addr (type)
2066 enum bfd_mach_o_cpu_type type;
2070 case BFD_MACH_O_CPU_TYPE_MC680x0:
2072 case BFD_MACH_O_CPU_TYPE_MC88000:
2074 case BFD_MACH_O_CPU_TYPE_POWERPC:
2076 case BFD_MACH_O_CPU_TYPE_I386:
2078 case BFD_MACH_O_CPU_TYPE_SPARC:
2080 case BFD_MACH_O_CPU_TYPE_I860:
2082 case BFD_MACH_O_CPU_TYPE_HPPA:
2083 return 0xc0000000 - 0x04000000;
2090 bfd_mach_o_core_fetch_environment (abfd, rbuf, rlen)
2092 unsigned char **rbuf;
2095 bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
2096 unsigned long stackaddr = bfd_mach_o_stack_addr (mdata->header.cputype);
2099 for (i = 0; i < mdata->header.ncmds; i++)
2101 bfd_mach_o_load_command *cur = &mdata->commands[i];
2102 bfd_mach_o_segment_command *seg = NULL;
2104 if (cur->type != BFD_MACH_O_LC_SEGMENT)
2107 seg = &cur->command.segment;
2109 if ((seg->vmaddr + seg->vmsize) == stackaddr)
2111 unsigned long start = seg->fileoff;
2112 unsigned long end = seg->fileoff + seg->filesize;
2113 unsigned char *buf = bfd_malloc (1024);
2114 unsigned long size = 1024;
2118 bfd_size_type nread = 0;
2119 unsigned long offset;
2120 int found_nonnull = 0;
2122 if (size > (end - start))
2123 size = (end - start);
2125 buf = bfd_realloc (buf, size);
2127 bfd_seek (abfd, end - size, SEEK_SET);
2128 nread = bfd_bread (buf, size, abfd);
2133 for (offset = 4; offset <= size; offset += 4)
2137 val = *((unsigned long *) (buf + size - offset));
2138 if (! found_nonnull)
2143 else if (val == 0x0)
2145 unsigned long bottom;
2148 bottom = seg->fileoff + seg->filesize - offset;
2149 top = seg->fileoff + seg->filesize - 4;
2150 *rbuf = bfd_malloc (top - bottom);
2151 *rlen = top - bottom;
2153 memcpy (*rbuf, buf + size - *rlen, *rlen);
2158 if (size == (end - start))
2170 bfd_mach_o_core_file_failing_command (abfd)
2173 unsigned char *buf = NULL;
2174 unsigned int len = 0;
2177 ret = bfd_mach_o_core_fetch_environment (abfd, &buf, &len);
2185 bfd_mach_o_core_file_failing_signal (abfd)
2186 bfd *abfd ATTRIBUTE_UNUSED;
2192 bfd_mach_o_core_file_matches_executable_p (core_bfd, exec_bfd)
2193 bfd *core_bfd ATTRIBUTE_UNUSED;
2194 bfd *exec_bfd ATTRIBUTE_UNUSED;
2199 #define TARGET_NAME mach_o_be_vec
2200 #define TARGET_STRING "mach-o-be"
2201 #define TARGET_BIG_ENDIAN 1
2202 #define TARGET_ARCHIVE 0
2204 #include "mach-o-target.c"
2207 #undef TARGET_STRING
2208 #undef TARGET_BIG_ENDIAN
2209 #undef TARGET_ARCHIVE
2211 #define TARGET_NAME mach_o_le_vec
2212 #define TARGET_STRING "mach-o-le"
2213 #define TARGET_BIG_ENDIAN 0
2214 #define TARGET_ARCHIVE 0
2216 #include "mach-o-target.c"
2219 #undef TARGET_STRING
2220 #undef TARGET_BIG_ENDIAN
2221 #undef TARGET_ARCHIVE
2223 #define TARGET_NAME mach_o_fat_vec
2224 #define TARGET_STRING "mach-o-fat"
2225 #define TARGET_BIG_ENDIAN 1
2226 #define TARGET_ARCHIVE 1
2228 #include "mach-o-target.c"
2231 #undef TARGET_STRING
2232 #undef TARGET_BIG_ENDIAN
2233 #undef TARGET_ARCHIVE