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_get_lineno _bfd_nosymbols_get_lineno
48 #define bfd_mach_o_find_nearest_line _bfd_nosymbols_find_nearest_line
49 #define bfd_mach_o_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
50 #define bfd_mach_o_read_minisymbols _bfd_generic_read_minisymbols
51 #define bfd_mach_o_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
52 #define bfd_mach_o_get_reloc_upper_bound _bfd_norelocs_get_reloc_upper_bound
53 #define bfd_mach_o_canonicalize_reloc _bfd_norelocs_canonicalize_reloc
54 #define bfd_mach_o_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
55 #define bfd_mach_o_bfd_get_relocated_section_contents bfd_generic_get_relocated_section_contents
56 #define bfd_mach_o_bfd_relax_section bfd_generic_relax_section
57 #define bfd_mach_o_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
58 #define bfd_mach_o_bfd_link_hash_table_free _bfd_generic_link_hash_table_free
59 #define bfd_mach_o_bfd_link_add_symbols _bfd_generic_link_add_symbols
60 #define bfd_mach_o_bfd_link_just_syms _bfd_generic_link_just_syms
61 #define bfd_mach_o_bfd_final_link _bfd_generic_final_link
62 #define bfd_mach_o_bfd_link_split_section _bfd_generic_link_split_section
63 #define bfd_mach_o_set_arch_mach bfd_default_set_arch_mach
64 #define bfd_mach_o_bfd_merge_private_bfd_data _bfd_generic_bfd_merge_private_bfd_data
65 #define bfd_mach_o_bfd_set_private_flags _bfd_generic_bfd_set_private_flags
66 #define bfd_mach_o_bfd_print_private_bfd_data _bfd_generic_bfd_print_private_bfd_data
67 #define bfd_mach_o_get_section_contents _bfd_generic_get_section_contents
68 #define bfd_mach_o_set_section_contents _bfd_generic_set_section_contents
69 #define bfd_mach_o_bfd_gc_sections bfd_generic_gc_sections
70 #define bfd_mach_o_bfd_merge_sections bfd_generic_merge_sections
71 #define bfd_mach_o_bfd_is_group_section bfd_generic_is_group_section
72 #define bfd_mach_o_bfd_discard_group bfd_generic_discard_group
73 #define bfd_mach_o_bfd_copy_private_header_data _bfd_generic_bfd_copy_private_header_data
75 static bfd_boolean bfd_mach_o_bfd_copy_private_symbol_data
76 PARAMS ((bfd *, asymbol *, bfd *, asymbol *));
77 static bfd_boolean bfd_mach_o_bfd_copy_private_section_data
78 PARAMS ((bfd *, asection *, bfd *, asection *));
79 static bfd_boolean bfd_mach_o_bfd_copy_private_bfd_data
80 PARAMS ((bfd *, bfd *));
81 static long bfd_mach_o_count_symbols
83 static long bfd_mach_o_get_symtab_upper_bound
85 static long bfd_mach_o_canonicalize_symtab
86 PARAMS ((bfd *, asymbol **));
87 static void bfd_mach_o_get_symbol_info
88 PARAMS ((bfd *, asymbol *, symbol_info *));
89 static void bfd_mach_o_print_symbol
90 PARAMS ((bfd *, PTR, asymbol *, bfd_print_symbol_type));
91 static void bfd_mach_o_convert_architecture
92 PARAMS ((bfd_mach_o_cpu_type, bfd_mach_o_cpu_subtype,
93 enum bfd_architecture *, unsigned long *));
94 static bfd_boolean bfd_mach_o_write_contents
96 static int bfd_mach_o_sizeof_headers
97 PARAMS ((bfd *, bfd_boolean));
98 static asymbol * bfd_mach_o_make_empty_symbol
100 static int bfd_mach_o_write_header
101 PARAMS ((bfd *, bfd_mach_o_header *));
102 static int bfd_mach_o_read_header
103 PARAMS ((bfd *, bfd_mach_o_header *));
104 static asection * bfd_mach_o_make_bfd_section
105 PARAMS ((bfd *, bfd_mach_o_section *));
106 static int bfd_mach_o_scan_read_section
107 PARAMS ((bfd *, bfd_mach_o_section *, bfd_vma));
108 static int bfd_mach_o_scan_write_section
109 PARAMS ((bfd *, bfd_mach_o_section *, bfd_vma));
110 static int bfd_mach_o_scan_write_symtab_symbols
111 PARAMS ((bfd *, bfd_mach_o_load_command *));
112 static int bfd_mach_o_scan_write_thread
113 PARAMS ((bfd *, bfd_mach_o_load_command *));
114 static int bfd_mach_o_scan_read_dylinker
115 PARAMS ((bfd *, bfd_mach_o_load_command *));
116 static int bfd_mach_o_scan_read_dylib
117 PARAMS ((bfd *, bfd_mach_o_load_command *));
118 static int bfd_mach_o_scan_read_prebound_dylib
119 PARAMS ((bfd *, bfd_mach_o_load_command *));
120 static int bfd_mach_o_scan_read_thread
121 PARAMS ((bfd *, bfd_mach_o_load_command *));
122 static int bfd_mach_o_scan_write_symtab
123 PARAMS ((bfd *, bfd_mach_o_load_command *));
124 static int bfd_mach_o_scan_read_dysymtab
125 PARAMS ((bfd *, bfd_mach_o_load_command *));
126 static int bfd_mach_o_scan_read_symtab
127 PARAMS ((bfd *, bfd_mach_o_load_command *));
128 static int bfd_mach_o_scan_read_segment
129 PARAMS ((bfd *, bfd_mach_o_load_command *));
130 static int bfd_mach_o_scan_write_segment
131 PARAMS ((bfd *, bfd_mach_o_load_command *));
132 static int bfd_mach_o_scan_read_command
133 PARAMS ((bfd *, bfd_mach_o_load_command *));
134 static void bfd_mach_o_flatten_sections
136 static const char * bfd_mach_o_i386_flavour_string
137 PARAMS ((unsigned int));
138 static const char * bfd_mach_o_ppc_flavour_string
139 PARAMS ((unsigned int));
141 /* The flags field of a section structure is separated into two parts a section
142 type and section attributes. The section types are mutually exclusive (it
143 can only have one type) but the section attributes are not (it may have more
144 than one attribute). */
146 #define SECTION_TYPE 0x000000ff /* 256 section types. */
147 #define SECTION_ATTRIBUTES 0xffffff00 /* 24 section attributes. */
149 /* Constants for the section attributes part of the flags field of a section
152 #define SECTION_ATTRIBUTES_USR 0xff000000 /* User-settable attributes. */
153 #define S_ATTR_PURE_INSTRUCTIONS 0x80000000 /* Section contains only true machine instructions. */
154 #define SECTION_ATTRIBUTES_SYS 0x00ffff00 /* System setable attributes. */
155 #define S_ATTR_SOME_INSTRUCTIONS 0x00000400 /* Section contains some machine instructions. */
156 #define S_ATTR_EXT_RELOC 0x00000200 /* Section has external relocation entries. */
157 #define S_ATTR_LOC_RELOC 0x00000100 /* Section has local relocation entries. */
168 bfd_mach_o_valid (abfd)
171 if (abfd == NULL || abfd->xvec == NULL)
174 if (! ((abfd->xvec == &mach_o_be_vec)
175 || (abfd->xvec == &mach_o_le_vec)
176 || (abfd->xvec == &mach_o_fat_vec)))
179 if (abfd->tdata.mach_o_data == NULL)
184 /* Copy any private info we understand from the input symbol
185 to the output symbol. */
188 bfd_mach_o_bfd_copy_private_symbol_data (ibfd, isymbol, obfd, osymbol)
189 bfd *ibfd ATTRIBUTE_UNUSED;
190 asymbol *isymbol ATTRIBUTE_UNUSED;
191 bfd *obfd ATTRIBUTE_UNUSED;
192 asymbol *osymbol ATTRIBUTE_UNUSED;
197 /* Copy any private info we understand from the input section
198 to the output section. */
201 bfd_mach_o_bfd_copy_private_section_data (ibfd, isection, obfd, osection)
202 bfd *ibfd ATTRIBUTE_UNUSED;
203 asection *isection ATTRIBUTE_UNUSED;
204 bfd *obfd ATTRIBUTE_UNUSED;
205 asection *osection ATTRIBUTE_UNUSED;
210 /* Copy any private info we understand from the input bfd
211 to the output bfd. */
214 bfd_mach_o_bfd_copy_private_bfd_data (ibfd, obfd)
218 BFD_ASSERT (bfd_mach_o_valid (ibfd));
219 BFD_ASSERT (bfd_mach_o_valid (obfd));
221 obfd->tdata.mach_o_data = ibfd->tdata.mach_o_data;
222 obfd->tdata.mach_o_data->ibfd = ibfd;
227 bfd_mach_o_count_symbols (abfd)
230 bfd_mach_o_data_struct *mdata = NULL;
234 BFD_ASSERT (bfd_mach_o_valid (abfd));
235 mdata = abfd->tdata.mach_o_data;
237 for (i = 0; i < mdata->header.ncmds; i++)
238 if (mdata->commands[i].type == BFD_MACH_O_LC_SYMTAB)
240 bfd_mach_o_symtab_command *sym = &mdata->commands[i].command.symtab;
248 bfd_mach_o_get_symtab_upper_bound (abfd)
251 long nsyms = bfd_mach_o_count_symbols (abfd);
256 return ((nsyms + 1) * sizeof (asymbol *));
260 bfd_mach_o_canonicalize_symtab (abfd, alocation)
264 bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
265 long nsyms = bfd_mach_o_count_symbols (abfd);
266 asymbol **csym = alocation;
272 for (i = 0; i < mdata->header.ncmds; i++)
274 if (mdata->commands[i].type == BFD_MACH_O_LC_SYMTAB)
276 bfd_mach_o_symtab_command *sym = &mdata->commands[i].command.symtab;
278 if (bfd_mach_o_scan_read_symtab_symbols (abfd, &mdata->commands[i].command.symtab) != 0)
280 fprintf (stderr, "bfd_mach_o_canonicalize_symtab: unable to load symbols for section %lu\n", i);
284 BFD_ASSERT (sym->symbols != NULL);
286 for (j = 0; j < sym->nsyms; j++)
288 BFD_ASSERT (csym < (alocation + nsyms));
289 *csym++ = &sym->symbols[j];
300 bfd_mach_o_get_symbol_info (abfd, symbol, ret)
301 bfd *abfd ATTRIBUTE_UNUSED;
305 bfd_symbol_info (symbol, ret);
309 bfd_mach_o_print_symbol (abfd, afile, symbol, how)
313 bfd_print_symbol_type how;
315 FILE *file = (FILE *) afile;
319 case bfd_print_symbol_name:
320 fprintf (file, "%s", symbol->name);
323 bfd_print_symbol_vandf (abfd, (PTR) file, symbol);
324 fprintf (file, " %-5s %s", symbol->section->name, symbol->name);
329 bfd_mach_o_convert_architecture (mtype, msubtype, type, subtype)
330 bfd_mach_o_cpu_type mtype;
331 bfd_mach_o_cpu_subtype msubtype ATTRIBUTE_UNUSED;
332 enum bfd_architecture *type;
333 unsigned long *subtype;
335 *subtype = bfd_arch_unknown;
339 case BFD_MACH_O_CPU_TYPE_VAX: *type = bfd_arch_vax; break;
340 case BFD_MACH_O_CPU_TYPE_MC680x0: *type = bfd_arch_m68k; break;
341 case BFD_MACH_O_CPU_TYPE_I386: *type = bfd_arch_i386; break;
342 case BFD_MACH_O_CPU_TYPE_MIPS: *type = bfd_arch_mips; break;
343 case BFD_MACH_O_CPU_TYPE_MC98000: *type = bfd_arch_m98k; break;
344 case BFD_MACH_O_CPU_TYPE_HPPA: *type = bfd_arch_hppa; break;
345 case BFD_MACH_O_CPU_TYPE_ARM: *type = bfd_arch_arm; break;
346 case BFD_MACH_O_CPU_TYPE_MC88000: *type = bfd_arch_m88k; break;
347 case BFD_MACH_O_CPU_TYPE_SPARC: *type = bfd_arch_sparc; break;
348 case BFD_MACH_O_CPU_TYPE_I860: *type = bfd_arch_i860; break;
349 case BFD_MACH_O_CPU_TYPE_ALPHA: *type = bfd_arch_alpha; break;
350 case BFD_MACH_O_CPU_TYPE_POWERPC: *type = bfd_arch_powerpc; break;
351 default: *type = bfd_arch_unknown; break;
356 case bfd_arch_i386: *subtype = bfd_mach_i386_i386; break;
357 case bfd_arch_sparc: *subtype = bfd_mach_sparc; break;
359 *subtype = bfd_arch_unknown;
364 bfd_mach_o_write_contents (abfd)
370 bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
372 /* Write data sections first in case they overlap header data to be
375 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
379 for (i = 0; i < mdata->header.ncmds; i++)
381 bfd_mach_o_load_command *cur = &mdata->commands[i];
382 if (cur->type != BFD_MACH_O_LC_SEGMENT)
386 bfd_mach_o_segment_command *seg = &cur->command.segment;
388 bfd_vma nbytes = seg->filesize;
389 bfd_vma curoff = seg->fileoff;
393 bfd_vma thisread = nbytes;
398 bfd_seek (abfd, curoff, SEEK_SET);
399 if (bfd_bread ((PTR) buf, thisread, abfd) != thisread)
402 bfd_seek (abfd, curoff, SEEK_SET);
403 if (bfd_bwrite ((PTR) buf, thisread, abfd) != thisread)
413 /* Now write header information. */
414 if (bfd_mach_o_write_header (abfd, &mdata->header) != 0)
417 for (i = 0; i < mdata->header.ncmds; i++)
419 unsigned char buf[8];
420 bfd_mach_o_load_command *cur = &mdata->commands[i];
421 unsigned long typeflag;
423 typeflag = cur->type_required ? cur->type & BFD_MACH_O_LC_REQ_DYLD : cur->type;
425 bfd_h_put_32 (abfd, typeflag, buf);
426 bfd_h_put_32 (abfd, cur->len, buf + 4);
428 bfd_seek (abfd, cur->offset, SEEK_SET);
429 if (bfd_bwrite ((PTR) buf, 8, abfd) != 8)
434 case BFD_MACH_O_LC_SEGMENT:
435 if (bfd_mach_o_scan_write_segment (abfd, cur) != 0)
438 case BFD_MACH_O_LC_SYMTAB:
439 if (bfd_mach_o_scan_write_symtab (abfd, cur) != 0)
442 case BFD_MACH_O_LC_SYMSEG:
444 case BFD_MACH_O_LC_THREAD:
445 case BFD_MACH_O_LC_UNIXTHREAD:
446 if (bfd_mach_o_scan_write_thread (abfd, cur) != 0)
449 case BFD_MACH_O_LC_LOADFVMLIB:
450 case BFD_MACH_O_LC_IDFVMLIB:
451 case BFD_MACH_O_LC_IDENT:
452 case BFD_MACH_O_LC_FVMFILE:
453 case BFD_MACH_O_LC_PREPAGE:
454 case BFD_MACH_O_LC_DYSYMTAB:
455 case BFD_MACH_O_LC_LOAD_DYLIB:
456 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB:
457 case BFD_MACH_O_LC_ID_DYLIB:
458 case BFD_MACH_O_LC_LOAD_DYLINKER:
459 case BFD_MACH_O_LC_ID_DYLINKER:
460 case BFD_MACH_O_LC_PREBOUND_DYLIB:
461 case BFD_MACH_O_LC_ROUTINES:
462 case BFD_MACH_O_LC_SUB_FRAMEWORK:
466 "unable to write unknown load command 0x%lx\n",
476 bfd_mach_o_sizeof_headers (a, b)
477 bfd *a ATTRIBUTE_UNUSED;
478 bfd_boolean b ATTRIBUTE_UNUSED;
483 /* Make an empty symbol. This is required only because
484 bfd_make_section_anyway wants to create a symbol for the section. */
487 bfd_mach_o_make_empty_symbol (abfd)
492 new = (asymbol *) bfd_zalloc (abfd, sizeof (asymbol));
500 bfd_mach_o_write_header (abfd, header)
502 bfd_mach_o_header *header;
504 unsigned char buf[28];
506 bfd_h_put_32 (abfd, header->magic, buf + 0);
507 bfd_h_put_32 (abfd, header->cputype, buf + 4);
508 bfd_h_put_32 (abfd, header->cpusubtype, buf + 8);
509 bfd_h_put_32 (abfd, header->filetype, buf + 12);
510 bfd_h_put_32 (abfd, header->ncmds, buf + 16);
511 bfd_h_put_32 (abfd, header->sizeofcmds, buf + 20);
512 bfd_h_put_32 (abfd, header->flags, buf + 24);
514 bfd_seek (abfd, 0, SEEK_SET);
515 if (bfd_bwrite ((PTR) buf, 28, abfd) != 28)
522 bfd_mach_o_read_header (abfd, header)
524 bfd_mach_o_header *header;
526 unsigned char buf[28];
527 bfd_vma (*get32) (const void *) = NULL;
529 bfd_seek (abfd, 0, SEEK_SET);
531 if (bfd_bread ((PTR) buf, 28, abfd) != 28)
534 if (bfd_getb32 (buf) == 0xfeedface)
536 header->byteorder = BFD_ENDIAN_BIG;
537 header->magic = 0xfeedface;
540 else if (bfd_getl32 (buf) == 0xfeedface)
542 header->byteorder = BFD_ENDIAN_LITTLE;
543 header->magic = 0xfeedface;
548 header->byteorder = BFD_ENDIAN_UNKNOWN;
552 header->cputype = (*get32) (buf + 4);
553 header->cpusubtype = (*get32) (buf + 8);
554 header->filetype = (*get32) (buf + 12);
555 header->ncmds = (*get32) (buf + 16);
556 header->sizeofcmds = (*get32) (buf + 20);
557 header->flags = (*get32) (buf + 24);
563 bfd_mach_o_make_bfd_section (abfd, section)
565 bfd_mach_o_section *section;
569 const char *prefix = "LC_SEGMENT";
570 unsigned int snamelen;
572 snamelen = strlen (prefix) + 1
573 + strlen (section->segname) + 1
574 + strlen (section->sectname) + 1;
576 sname = (char *) bfd_alloc (abfd, snamelen);
579 sprintf (sname, "%s.%s.%s", prefix, section->segname, section->sectname);
581 bfdsec = bfd_make_section_anyway (abfd, sname);
585 bfdsec->vma = section->addr;
586 bfdsec->lma = section->addr;
587 bfdsec->_raw_size = section->size;
588 bfdsec->filepos = section->offset;
589 bfdsec->alignment_power = section->align;
591 if (section->flags & BFD_MACH_O_S_ZEROFILL)
592 bfdsec->flags = SEC_ALLOC;
594 bfdsec->flags = SEC_HAS_CONTENTS | SEC_LOAD | SEC_ALLOC | SEC_CODE;
600 bfd_mach_o_scan_read_section (abfd, section, offset)
602 bfd_mach_o_section *section;
605 unsigned char buf[68];
607 bfd_seek (abfd, offset, SEEK_SET);
608 if (bfd_bread ((PTR) buf, 68, abfd) != 68)
611 memcpy (section->sectname, buf, 16);
612 section->sectname[16] = '\0';
613 memcpy (section->segname, buf + 16, 16);
614 section->segname[16] = '\0';
615 section->addr = bfd_h_get_32 (abfd, buf + 32);
616 section->size = bfd_h_get_32 (abfd, buf + 36);
617 section->offset = bfd_h_get_32 (abfd, buf + 40);
618 section->align = bfd_h_get_32 (abfd, buf + 44);
619 section->reloff = bfd_h_get_32 (abfd, buf + 48);
620 section->nreloc = bfd_h_get_32 (abfd, buf + 52);
621 section->flags = bfd_h_get_32 (abfd, buf + 56);
622 section->reserved1 = bfd_h_get_32 (abfd, buf + 60);
623 section->reserved2 = bfd_h_get_32 (abfd, buf + 64);
624 section->bfdsection = bfd_mach_o_make_bfd_section (abfd, section);
626 if (section->bfdsection == NULL)
633 bfd_mach_o_scan_write_section (abfd, section, offset)
635 bfd_mach_o_section *section;
638 unsigned char buf[68];
640 memcpy (buf, section->sectname, 16);
641 memcpy (buf + 16, section->segname, 16);
642 bfd_h_put_32 (abfd, section->addr, buf + 32);
643 bfd_h_put_32 (abfd, section->size, buf + 36);
644 bfd_h_put_32 (abfd, section->offset, buf + 40);
645 bfd_h_put_32 (abfd, section->align, buf + 44);
646 bfd_h_put_32 (abfd, section->reloff, buf + 48);
647 bfd_h_put_32 (abfd, section->nreloc, buf + 52);
648 bfd_h_put_32 (abfd, section->flags, buf + 56);
649 /* bfd_h_put_32 (abfd, section->reserved1, buf + 60); */
650 /* bfd_h_put_32 (abfd, section->reserved2, buf + 64); */
652 bfd_seek (abfd, offset, SEEK_SET);
653 if (bfd_bwrite ((PTR) buf, 68, abfd) != 68)
660 bfd_mach_o_scan_write_symtab_symbols (abfd, command)
662 bfd_mach_o_load_command *command;
664 bfd_mach_o_symtab_command *sym = &command->command.symtab;
668 for (i = 0; i < sym->nsyms; i++)
670 unsigned char buf[12];
671 bfd_vma symoff = sym->symoff + (i * 12);
672 unsigned char ntype = 0;
673 unsigned char nsect = 0;
676 s = &sym->symbols[i];
678 /* Don't set this from the symbol information; use stored values. */
680 if (s->flags & BSF_GLOBAL)
682 if (s->flags & BSF_DEBUGGING)
685 if (s->section == bfd_und_section_ptr)
687 else if (s->section == bfd_abs_section_ptr)
693 /* Instead just set from the stored values. */
694 ntype = (s->udata.i >> 24) & 0xff;
695 nsect = (s->udata.i >> 16) & 0xff;
696 ndesc = s->udata.i & 0xffff;
698 bfd_h_put_32 (abfd, s->name - sym->strtab, buf);
699 bfd_h_put_8 (abfd, ntype, buf + 4);
700 bfd_h_put_8 (abfd, nsect, buf + 5);
701 bfd_h_put_16 (abfd, ndesc, buf + 6);
702 bfd_h_put_32 (abfd, s->section->vma + s->value, buf + 8);
704 bfd_seek (abfd, symoff, SEEK_SET);
705 if (bfd_bwrite ((PTR) buf, 12, abfd) != 12)
707 fprintf (stderr, "bfd_mach_o_scan_write_symtab_symbols: unable to write %d bytes at %lu\n",
708 12, (unsigned long) symoff);
717 bfd_mach_o_scan_read_symtab_symbol (abfd, sym, s, i)
719 bfd_mach_o_symtab_command *sym;
723 bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
724 bfd_vma symoff = sym->symoff + (i * 12);
725 unsigned char buf[12];
726 unsigned char type = -1;
727 unsigned char section = -1;
729 unsigned long value = -1;
730 unsigned long stroff = -1;
731 unsigned int symtype = -1;
733 BFD_ASSERT (sym->strtab != NULL);
735 bfd_seek (abfd, symoff, SEEK_SET);
736 if (bfd_bread ((PTR) buf, 12, abfd) != 12)
738 fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: unable to read %d bytes at %lu\n",
739 12, (unsigned long) symoff);
743 stroff = bfd_h_get_32 (abfd, buf);
744 type = bfd_h_get_8 (abfd, buf + 4);
745 symtype = (type & 0x0e);
746 section = bfd_h_get_8 (abfd, buf + 5) - 1;
747 desc = bfd_h_get_16 (abfd, buf + 6);
748 value = bfd_h_get_32 (abfd, buf + 8);
750 if (stroff >= sym->strsize)
752 fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: symbol name out of range (%lu >= %lu)\n",
753 (unsigned long) stroff, (unsigned long) sym->strsize);
758 s->name = sym->strtab + stroff;
760 s->udata.i = (type << 24) | (section << 16) | desc;
763 if (type & BFD_MACH_O_N_STAB)
765 s->flags |= BSF_DEBUGGING;
766 s->section = bfd_und_section_ptr;
770 if (type & BFD_MACH_O_N_PEXT)
772 type &= ~BFD_MACH_O_N_PEXT;
773 s->flags |= BSF_GLOBAL;
776 if (type & BFD_MACH_O_N_EXT)
778 type &= ~BFD_MACH_O_N_EXT;
779 s->flags |= BSF_GLOBAL;
784 case BFD_MACH_O_N_UNDF:
785 s->section = bfd_und_section_ptr;
787 case BFD_MACH_O_N_PBUD:
788 s->section = bfd_und_section_ptr;
790 case BFD_MACH_O_N_ABS:
791 s->section = bfd_abs_section_ptr;
793 case BFD_MACH_O_N_SECT:
794 if ((section > 0) && (section <= mdata->nsects))
796 s->section = mdata->sections[section - 1]->bfdsection;
797 s->value = s->value - mdata->sections[section - 1]->addr;
801 /* Mach-O uses 0 to mean "no section"; not an error. */
804 fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: "
805 "symbol \"%s\" specified invalid section %d (max %lu): setting to undefined\n",
806 s->name, section, mdata->nsects);
808 s->section = bfd_und_section_ptr;
811 case BFD_MACH_O_N_INDR:
812 fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: "
813 "symbol \"%s\" is unsupported 'indirect' reference: setting to undefined\n",
815 s->section = bfd_und_section_ptr;
818 fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: "
819 "symbol \"%s\" specified invalid type field 0x%x: setting to undefined\n",
821 s->section = bfd_und_section_ptr;
830 bfd_mach_o_scan_read_symtab_strtab (abfd, sym)
832 bfd_mach_o_symtab_command *sym;
834 BFD_ASSERT (sym->strtab == NULL);
836 if (abfd->flags & BFD_IN_MEMORY)
838 struct bfd_in_memory *b;
840 b = (struct bfd_in_memory *) abfd->iostream;
842 if ((sym->stroff + sym->strsize) > b->size)
844 bfd_set_error (bfd_error_file_truncated);
847 sym->strtab = b->buffer + sym->stroff;
851 sym->strtab = bfd_alloc (abfd, sym->strsize);
852 if (sym->strtab == NULL)
855 bfd_seek (abfd, sym->stroff, SEEK_SET);
856 if (bfd_bread ((PTR) sym->strtab, sym->strsize, abfd) != sym->strsize)
858 fprintf (stderr, "bfd_mach_o_scan_read_symtab_strtab: unable to read %lu bytes at %lu\n",
859 sym->strsize, sym->stroff);
867 bfd_mach_o_scan_read_symtab_symbols (abfd, sym)
869 bfd_mach_o_symtab_command *sym;
874 BFD_ASSERT (sym->symbols == NULL);
875 sym->symbols = bfd_alloc (abfd, sym->nsyms * sizeof (asymbol));
877 if (sym->symbols == NULL)
879 fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbols: unable to allocate memory for symbols\n");
883 ret = bfd_mach_o_scan_read_symtab_strtab (abfd, sym);
887 for (i = 0; i < sym->nsyms; i++)
889 ret = bfd_mach_o_scan_read_symtab_symbol (abfd, sym, &sym->symbols[i], i);
898 bfd_mach_o_scan_read_dysymtab_symbol (abfd, dysym, sym, s, i)
900 bfd_mach_o_dysymtab_command *dysym;
901 bfd_mach_o_symtab_command *sym;
905 unsigned long isymoff = dysym->indirectsymoff + (i * 4);
906 unsigned long symindex;
907 unsigned char buf[4];
909 BFD_ASSERT (i < dysym->nindirectsyms);
911 bfd_seek (abfd, isymoff, SEEK_SET);
912 if (bfd_bread ((PTR) buf, 4, abfd) != 4)
914 fprintf (stderr, "bfd_mach_o_scan_read_dysymtab_symbol: unable to read %lu bytes at %lu\n",
915 (unsigned long) 4, isymoff);
918 symindex = bfd_h_get_32 (abfd, buf);
920 return bfd_mach_o_scan_read_symtab_symbol (abfd, sym, s, symindex);
924 bfd_mach_o_i386_flavour_string (flavour)
925 unsigned int flavour;
927 switch ((int) flavour)
929 case BFD_MACH_O_i386_NEW_THREAD_STATE: return "i386_NEW_THREAD_STATE";
930 case BFD_MACH_O_i386_FLOAT_STATE: return "i386_FLOAT_STATE";
931 case BFD_MACH_O_i386_ISA_PORT_MAP_STATE: return "i386_ISA_PORT_MAP_STATE";
932 case BFD_MACH_O_i386_V86_ASSIST_STATE: return "i386_V86_ASSIST_STATE";
933 case BFD_MACH_O_i386_REGS_SEGS_STATE: return "i386_REGS_SEGS_STATE";
934 case BFD_MACH_O_i386_THREAD_SYSCALL_STATE: return "i386_THREAD_SYSCALL_STATE";
935 case BFD_MACH_O_i386_THREAD_STATE_NONE: return "i386_THREAD_STATE_NONE";
936 case BFD_MACH_O_i386_SAVED_STATE: return "i386_SAVED_STATE";
937 case BFD_MACH_O_i386_THREAD_STATE: return "i386_THREAD_STATE";
938 case BFD_MACH_O_i386_THREAD_FPSTATE: return "i386_THREAD_FPSTATE";
939 case BFD_MACH_O_i386_THREAD_EXCEPTSTATE: return "i386_THREAD_EXCEPTSTATE";
940 case BFD_MACH_O_i386_THREAD_CTHREADSTATE: return "i386_THREAD_CTHREADSTATE";
941 default: return "UNKNOWN";
946 bfd_mach_o_ppc_flavour_string (flavour)
947 unsigned int flavour;
949 switch ((int) flavour)
951 case BFD_MACH_O_PPC_THREAD_STATE: return "PPC_THREAD_STATE";
952 case BFD_MACH_O_PPC_FLOAT_STATE: return "PPC_FLOAT_STATE";
953 case BFD_MACH_O_PPC_EXCEPTION_STATE: return "PPC_EXCEPTION_STATE";
954 case BFD_MACH_O_PPC_VECTOR_STATE: return "PPC_VECTOR_STATE";
955 default: return "UNKNOWN";
960 bfd_mach_o_scan_write_thread (abfd, command)
962 bfd_mach_o_load_command *command;
964 bfd_mach_o_thread_command *cmd = &command->command.thread;
966 unsigned char buf[8];
968 unsigned int nflavours;
970 BFD_ASSERT ((command->type == BFD_MACH_O_LC_THREAD)
971 || (command->type == BFD_MACH_O_LC_UNIXTHREAD));
975 for (i = 0; i < cmd->nflavours; i++)
977 BFD_ASSERT ((cmd->flavours[i].size % 4) == 0);
978 BFD_ASSERT (cmd->flavours[i].offset == (command->offset + offset + 8));
980 bfd_h_put_32 (abfd, cmd->flavours[i].flavour, buf);
981 bfd_h_put_32 (abfd, (cmd->flavours[i].size / 4), buf + 4);
983 bfd_seek (abfd, command->offset + offset, SEEK_SET);
984 if (bfd_bwrite ((PTR) buf, 8, abfd) != 8)
987 offset += cmd->flavours[i].size + 8;
994 bfd_mach_o_scan_read_dylinker (abfd, command)
996 bfd_mach_o_load_command *command;
998 bfd_mach_o_dylinker_command *cmd = &command->command.dylinker;
999 unsigned char buf[4];
1000 unsigned int nameoff;
1005 BFD_ASSERT ((command->type == BFD_MACH_O_LC_ID_DYLINKER)
1006 || (command->type == BFD_MACH_O_LC_LOAD_DYLINKER));
1008 bfd_seek (abfd, command->offset + 8, SEEK_SET);
1009 if (bfd_bread ((PTR) buf, 4, abfd) != 4)
1012 nameoff = bfd_h_get_32 (abfd, buf + 0);
1014 cmd->name_offset = command->offset + nameoff;
1015 cmd->name_len = command->len - nameoff;
1017 if (command->type == BFD_MACH_O_LC_LOAD_DYLINKER)
1018 prefix = "LC_LOAD_DYLINKER";
1019 else if (command->type == BFD_MACH_O_LC_ID_DYLINKER)
1020 prefix = "LC_ID_DYLINKER";
1024 sname = (char *) bfd_alloc (abfd, strlen (prefix) + 1);
1027 strcpy (sname, prefix);
1029 bfdsec = bfd_make_section_anyway (abfd, sname);
1035 bfdsec->_raw_size = command->len - 8;
1036 bfdsec->filepos = command->offset + 8;
1037 bfdsec->alignment_power = 0;
1038 bfdsec->flags = SEC_HAS_CONTENTS;
1040 cmd->section = bfdsec;
1046 bfd_mach_o_scan_read_dylib (abfd, command)
1048 bfd_mach_o_load_command *command;
1050 bfd_mach_o_dylib_command *cmd = &command->command.dylib;
1051 unsigned char buf[16];
1052 unsigned int nameoff;
1057 BFD_ASSERT ((command->type == BFD_MACH_O_LC_ID_DYLIB)
1058 || (command->type == BFD_MACH_O_LC_LOAD_DYLIB)
1059 || (command->type == BFD_MACH_O_LC_LOAD_WEAK_DYLIB));
1061 bfd_seek (abfd, command->offset + 8, SEEK_SET);
1062 if (bfd_bread ((PTR) buf, 16, abfd) != 16)
1065 nameoff = bfd_h_get_32 (abfd, buf + 0);
1066 cmd->timestamp = bfd_h_get_32 (abfd, buf + 4);
1067 cmd->current_version = bfd_h_get_32 (abfd, buf + 8);
1068 cmd->compatibility_version = bfd_h_get_32 (abfd, buf + 12);
1070 cmd->name_offset = command->offset + nameoff;
1071 cmd->name_len = command->len - nameoff;
1073 if (command->type == BFD_MACH_O_LC_LOAD_DYLIB)
1074 prefix = "LC_LOAD_DYLIB";
1075 else if (command->type == BFD_MACH_O_LC_LOAD_WEAK_DYLIB)
1076 prefix = "LC_LOAD_WEAK_DYLIB";
1077 else if (command->type == BFD_MACH_O_LC_ID_DYLIB)
1078 prefix = "LC_ID_DYLIB";
1082 sname = (char *) bfd_alloc (abfd, strlen (prefix) + 1);
1085 strcpy (sname, prefix);
1087 bfdsec = bfd_make_section_anyway (abfd, sname);
1093 bfdsec->_raw_size = command->len - 8;
1094 bfdsec->filepos = command->offset + 8;
1095 bfdsec->alignment_power = 0;
1096 bfdsec->flags = SEC_HAS_CONTENTS;
1098 cmd->section = bfdsec;
1104 bfd_mach_o_scan_read_prebound_dylib (abfd, command)
1105 bfd *abfd ATTRIBUTE_UNUSED;
1106 bfd_mach_o_load_command *command ATTRIBUTE_UNUSED;
1108 /* bfd_mach_o_prebound_dylib_command *cmd = &command->command.prebound_dylib; */
1110 BFD_ASSERT (command->type == BFD_MACH_O_LC_PREBOUND_DYLIB);
1115 bfd_mach_o_scan_read_thread (abfd, command)
1117 bfd_mach_o_load_command *command;
1119 bfd_mach_o_data_struct *mdata = NULL;
1120 bfd_mach_o_thread_command *cmd = &command->command.thread;
1121 unsigned char buf[8];
1123 unsigned int nflavours;
1126 BFD_ASSERT ((command->type == BFD_MACH_O_LC_THREAD)
1127 || (command->type == BFD_MACH_O_LC_UNIXTHREAD));
1129 BFD_ASSERT (bfd_mach_o_valid (abfd));
1130 mdata = abfd->tdata.mach_o_data;
1134 while (offset != command->len)
1136 if (offset >= command->len)
1139 bfd_seek (abfd, command->offset + offset, SEEK_SET);
1141 if (bfd_bread ((PTR) buf, 8, abfd) != 8)
1144 offset += 8 + bfd_h_get_32 (abfd, buf + 4) * 4;
1149 ((bfd_mach_o_thread_flavour *)
1150 bfd_alloc (abfd, nflavours * sizeof (bfd_mach_o_thread_flavour)));
1151 if (cmd->flavours == NULL)
1153 cmd->nflavours = nflavours;
1157 while (offset != command->len)
1159 if (offset >= command->len)
1162 if (nflavours >= cmd->nflavours)
1165 bfd_seek (abfd, command->offset + offset, SEEK_SET);
1167 if (bfd_bread ((PTR) buf, 8, abfd) != 8)
1170 cmd->flavours[nflavours].flavour = bfd_h_get_32 (abfd, buf);
1171 cmd->flavours[nflavours].offset = command->offset + offset + 8;
1172 cmd->flavours[nflavours].size = bfd_h_get_32 (abfd, buf + 4) * 4;
1173 offset += cmd->flavours[nflavours].size + 8;
1177 for (i = 0; i < nflavours; i++)
1180 unsigned int snamelen;
1182 const char *flavourstr;
1183 const char *prefix = "LC_THREAD";
1186 switch (mdata->header.cputype)
1188 case BFD_MACH_O_CPU_TYPE_POWERPC:
1189 flavourstr = bfd_mach_o_ppc_flavour_string (cmd->flavours[i].flavour);
1191 case BFD_MACH_O_CPU_TYPE_I386:
1192 flavourstr = bfd_mach_o_i386_flavour_string (cmd->flavours[i].flavour);
1195 flavourstr = "UNKNOWN_ARCHITECTURE";
1199 snamelen = strlen (prefix) + 1 + 20 + 1 + strlen (flavourstr) + 1;
1200 sname = (char *) bfd_alloc (abfd, snamelen);
1206 sprintf (sname, "%s.%s.%u", prefix, flavourstr, j);
1207 if (bfd_get_section_by_name (abfd, sname) == NULL)
1212 bfdsec = bfd_make_section (abfd, sname);
1216 bfdsec->_raw_size = cmd->flavours[i].size;
1217 bfdsec->filepos = cmd->flavours[i].offset;
1218 bfdsec->alignment_power = 0x0;
1219 bfdsec->flags = SEC_HAS_CONTENTS;
1221 cmd->section = bfdsec;
1228 bfd_mach_o_scan_write_symtab (abfd, command)
1230 bfd_mach_o_load_command *command;
1232 bfd_mach_o_symtab_command *seg = &command->command.symtab;
1233 unsigned char buf[16];
1235 BFD_ASSERT (command->type == BFD_MACH_O_LC_SYMTAB);
1237 bfd_h_put_32 (abfd, seg->symoff, buf);
1238 bfd_h_put_32 (abfd, seg->nsyms, buf + 4);
1239 bfd_h_put_32 (abfd, seg->stroff, buf + 8);
1240 bfd_h_put_32 (abfd, seg->strsize, buf + 12);
1242 bfd_seek (abfd, command->offset + 8, SEEK_SET);
1243 if (bfd_bwrite ((PTR) buf, 16, abfd) != 16)
1246 if (bfd_mach_o_scan_write_symtab_symbols (abfd, command) != 0)
1253 bfd_mach_o_scan_read_dysymtab (abfd, command)
1255 bfd_mach_o_load_command *command;
1257 bfd_mach_o_dysymtab_command *seg = &command->command.dysymtab;
1258 unsigned char buf[72];
1260 BFD_ASSERT (command->type == BFD_MACH_O_LC_DYSYMTAB);
1262 bfd_seek (abfd, command->offset + 8, SEEK_SET);
1263 if (bfd_bread ((PTR) buf, 72, abfd) != 72)
1266 seg->ilocalsym = bfd_h_get_32 (abfd, buf + 0);
1267 seg->nlocalsym = bfd_h_get_32 (abfd, buf + 4);
1268 seg->iextdefsym = bfd_h_get_32 (abfd, buf + 8);
1269 seg->nextdefsym = bfd_h_get_32 (abfd, buf + 12);
1270 seg->iundefsym = bfd_h_get_32 (abfd, buf + 16);
1271 seg->nundefsym = bfd_h_get_32 (abfd, buf + 20);
1272 seg->tocoff = bfd_h_get_32 (abfd, buf + 24);
1273 seg->ntoc = bfd_h_get_32 (abfd, buf + 28);
1274 seg->modtaboff = bfd_h_get_32 (abfd, buf + 32);
1275 seg->nmodtab = bfd_h_get_32 (abfd, buf + 36);
1276 seg->extrefsymoff = bfd_h_get_32 (abfd, buf + 40);
1277 seg->nextrefsyms = bfd_h_get_32 (abfd, buf + 44);
1278 seg->indirectsymoff = bfd_h_get_32 (abfd, buf + 48);
1279 seg->nindirectsyms = bfd_h_get_32 (abfd, buf + 52);
1280 seg->extreloff = bfd_h_get_32 (abfd, buf + 56);
1281 seg->nextrel = bfd_h_get_32 (abfd, buf + 60);
1282 seg->locreloff = bfd_h_get_32 (abfd, buf + 64);
1283 seg->nlocrel = bfd_h_get_32 (abfd, buf + 68);
1289 bfd_mach_o_scan_read_symtab (abfd, command)
1291 bfd_mach_o_load_command *command;
1293 bfd_mach_o_symtab_command *seg = &command->command.symtab;
1294 unsigned char buf[16];
1297 const char *prefix = "LC_SYMTAB.stabs";
1299 BFD_ASSERT (command->type == BFD_MACH_O_LC_SYMTAB);
1301 bfd_seek (abfd, command->offset + 8, SEEK_SET);
1302 if (bfd_bread ((PTR) buf, 16, abfd) != 16)
1305 seg->symoff = bfd_h_get_32 (abfd, buf);
1306 seg->nsyms = bfd_h_get_32 (abfd, buf + 4);
1307 seg->stroff = bfd_h_get_32 (abfd, buf + 8);
1308 seg->strsize = bfd_h_get_32 (abfd, buf + 12);
1309 seg->symbols = NULL;
1312 sname = (char *) bfd_alloc (abfd, strlen (prefix) + 1);
1315 strcpy (sname, prefix);
1317 bfdsec = bfd_make_section_anyway (abfd, sname);
1323 bfdsec->_raw_size = seg->nsyms * 12;
1324 bfdsec->filepos = seg->symoff;
1325 bfdsec->alignment_power = 0;
1326 bfdsec->flags = SEC_HAS_CONTENTS;
1328 seg->stabs_segment = bfdsec;
1330 prefix = "LC_SYMTAB.stabstr";
1331 sname = (char *) bfd_alloc (abfd, strlen (prefix) + 1);
1334 strcpy (sname, prefix);
1336 bfdsec = bfd_make_section_anyway (abfd, sname);
1342 bfdsec->_raw_size = seg->strsize;
1343 bfdsec->filepos = seg->stroff;
1344 bfdsec->alignment_power = 0;
1345 bfdsec->flags = SEC_HAS_CONTENTS;
1347 seg->stabstr_segment = bfdsec;
1353 bfd_mach_o_scan_read_segment (abfd, command)
1355 bfd_mach_o_load_command *command;
1357 unsigned char buf[48];
1358 bfd_mach_o_segment_command *seg = &command->command.segment;
1362 const char *prefix = "LC_SEGMENT";
1363 unsigned int snamelen;
1365 BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT);
1367 bfd_seek (abfd, command->offset + 8, SEEK_SET);
1368 if (bfd_bread ((PTR) buf, 48, abfd) != 48)
1371 memcpy (seg->segname, buf, 16);
1372 seg->vmaddr = bfd_h_get_32 (abfd, buf + 16);
1373 seg->vmsize = bfd_h_get_32 (abfd, buf + 20);
1374 seg->fileoff = bfd_h_get_32 (abfd, buf + 24);
1375 seg->filesize = bfd_h_get_32 (abfd, buf + 28);
1376 /* seg->maxprot = bfd_h_get_32 (abfd, buf + 32); */
1377 /* seg->initprot = bfd_h_get_32 (abfd, buf + 36); */
1378 seg->nsects = bfd_h_get_32 (abfd, buf + 40);
1379 seg->flags = bfd_h_get_32 (abfd, buf + 44);
1381 snamelen = strlen (prefix) + 1 + strlen (seg->segname) + 1;
1382 sname = (char *) bfd_alloc (abfd, snamelen);
1385 sprintf (sname, "%s.%s", prefix, seg->segname);
1387 bfdsec = bfd_make_section_anyway (abfd, sname);
1391 bfdsec->vma = seg->vmaddr;
1392 bfdsec->lma = seg->vmaddr;
1393 bfdsec->_raw_size = seg->filesize;
1394 bfdsec->filepos = seg->fileoff;
1395 bfdsec->alignment_power = 0x0;
1396 bfdsec->flags = SEC_HAS_CONTENTS | SEC_LOAD | SEC_ALLOC | SEC_CODE;
1398 seg->segment = bfdsec;
1400 if (seg->nsects != 0)
1403 ((bfd_mach_o_section *)
1404 bfd_alloc (abfd, seg->nsects * sizeof (bfd_mach_o_section)));
1405 if (seg->sections == NULL)
1408 for (i = 0; i < seg->nsects; i++)
1410 bfd_vma segoff = command->offset + 48 + 8 + (i * 68);
1412 if (bfd_mach_o_scan_read_section (abfd, &seg->sections[i],
1422 bfd_mach_o_scan_write_segment (abfd, command)
1424 bfd_mach_o_load_command *command;
1426 unsigned char buf[48];
1427 bfd_mach_o_segment_command *seg = &command->command.segment;
1430 BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT);
1432 memcpy (buf, seg->segname, 16);
1433 bfd_h_put_32 (abfd, seg->vmaddr, buf + 16);
1434 bfd_h_put_32 (abfd, seg->vmsize, buf + 20);
1435 bfd_h_put_32 (abfd, seg->fileoff, buf + 24);
1436 bfd_h_put_32 (abfd, seg->filesize, buf + 28);
1437 bfd_h_put_32 (abfd, 0 /* seg->maxprot */, buf + 32);
1438 bfd_h_put_32 (abfd, 0 /* seg->initprot */, buf + 36);
1439 bfd_h_put_32 (abfd, seg->nsects, buf + 40);
1440 bfd_h_put_32 (abfd, seg->flags, buf + 44);
1442 bfd_seek (abfd, command->offset + 8, SEEK_SET);
1443 if (bfd_bwrite ((PTR) buf, 48, abfd) != 48)
1448 bfd_vma nbytes = seg->filesize;
1449 bfd_vma curoff = seg->fileoff;
1453 bfd_vma thisread = nbytes;
1455 if (thisread > 1024)
1458 bfd_seek (abfd, curoff, SEEK_SET);
1459 if (bfd_bread ((PTR) buf, thisread, abfd) != thisread)
1462 bfd_seek (abfd, curoff, SEEK_SET);
1463 if (bfd_bwrite ((PTR) buf, thisread, abfd) != thisread)
1471 for (i = 0; i < seg->nsects; i++)
1473 bfd_vma segoff = command->offset + 48 + 8 + (i * 68);
1475 if (bfd_mach_o_scan_write_section (abfd, &seg->sections[i], segoff) != 0)
1483 bfd_mach_o_scan_read_command (abfd, command)
1485 bfd_mach_o_load_command *command;
1487 unsigned char buf[8];
1489 bfd_seek (abfd, command->offset, SEEK_SET);
1490 if (bfd_bread ((PTR) buf, 8, abfd) != 8)
1493 command->type = (bfd_h_get_32 (abfd, buf) & ~BFD_MACH_O_LC_REQ_DYLD);
1494 command->type_required = (bfd_h_get_32 (abfd, buf) & BFD_MACH_O_LC_REQ_DYLD
1496 command->len = bfd_h_get_32 (abfd, buf + 4);
1498 switch (command->type)
1500 case BFD_MACH_O_LC_SEGMENT:
1501 if (bfd_mach_o_scan_read_segment (abfd, command) != 0)
1504 case BFD_MACH_O_LC_SYMTAB:
1505 if (bfd_mach_o_scan_read_symtab (abfd, command) != 0)
1508 case BFD_MACH_O_LC_SYMSEG:
1510 case BFD_MACH_O_LC_THREAD:
1511 case BFD_MACH_O_LC_UNIXTHREAD:
1512 if (bfd_mach_o_scan_read_thread (abfd, command) != 0)
1515 case BFD_MACH_O_LC_LOAD_DYLINKER:
1516 case BFD_MACH_O_LC_ID_DYLINKER:
1517 if (bfd_mach_o_scan_read_dylinker (abfd, command) != 0)
1520 case BFD_MACH_O_LC_LOAD_DYLIB:
1521 case BFD_MACH_O_LC_ID_DYLIB:
1522 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB:
1523 if (bfd_mach_o_scan_read_dylib (abfd, command) != 0)
1526 case BFD_MACH_O_LC_PREBOUND_DYLIB:
1527 if (bfd_mach_o_scan_read_prebound_dylib (abfd, command) != 0)
1530 case BFD_MACH_O_LC_LOADFVMLIB:
1531 case BFD_MACH_O_LC_IDFVMLIB:
1532 case BFD_MACH_O_LC_IDENT:
1533 case BFD_MACH_O_LC_FVMFILE:
1534 case BFD_MACH_O_LC_PREPAGE:
1535 case BFD_MACH_O_LC_ROUTINES:
1536 case BFD_MACH_O_LC_SUB_FRAMEWORK:
1538 case BFD_MACH_O_LC_DYSYMTAB:
1539 if (bfd_mach_o_scan_read_dysymtab (abfd, command) != 0)
1542 case BFD_MACH_O_LC_SUB_UMBRELLA:
1543 case BFD_MACH_O_LC_SUB_CLIENT:
1544 case BFD_MACH_O_LC_SUB_LIBRARY:
1545 case BFD_MACH_O_LC_TWOLEVEL_HINTS:
1546 case BFD_MACH_O_LC_PREBIND_CKSUM:
1549 fprintf (stderr, "unable to read unknown load command 0x%lx\n",
1550 (unsigned long) command->type);
1558 bfd_mach_o_flatten_sections (abfd)
1561 bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
1567 for (i = 0; i < mdata->header.ncmds; i++)
1569 if (mdata->commands[i].type == BFD_MACH_O_LC_SEGMENT)
1571 bfd_mach_o_segment_command *seg;
1573 seg = &mdata->commands[i].command.segment;
1574 mdata->nsects += seg->nsects;
1578 mdata->sections = bfd_alloc (abfd,
1579 mdata->nsects * sizeof (bfd_mach_o_section *));
1582 for (i = 0; i < mdata->header.ncmds; i++)
1584 if (mdata->commands[i].type == BFD_MACH_O_LC_SEGMENT)
1586 bfd_mach_o_segment_command *seg;
1588 seg = &mdata->commands[i].command.segment;
1589 BFD_ASSERT (csect + seg->nsects <= mdata->nsects);
1591 for (j = 0; j < seg->nsects; j++)
1592 mdata->sections[csect++] = &seg->sections[j];
1598 bfd_mach_o_scan_start_address (abfd)
1601 bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
1602 bfd_mach_o_thread_command *cmd = NULL;
1605 for (i = 0; i < mdata->header.ncmds; i++)
1607 if ((mdata->commands[i].type == BFD_MACH_O_LC_THREAD) ||
1608 (mdata->commands[i].type == BFD_MACH_O_LC_UNIXTHREAD))
1611 cmd = &mdata->commands[i].command.thread;
1620 for (i = 0; i < cmd->nflavours; i++)
1622 if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_I386)
1623 && (cmd->flavours[i].flavour
1624 == (unsigned long) BFD_MACH_O_i386_THREAD_STATE))
1626 unsigned char buf[4];
1628 bfd_seek (abfd, cmd->flavours[i].offset + 40, SEEK_SET);
1630 if (bfd_bread (buf, 4, abfd) != 4)
1633 abfd->start_address = bfd_h_get_32 (abfd, buf);
1635 else if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_POWERPC)
1636 && (cmd->flavours[i].flavour == BFD_MACH_O_PPC_THREAD_STATE))
1638 unsigned char buf[4];
1640 bfd_seek (abfd, cmd->flavours[i].offset + 0, SEEK_SET);
1642 if (bfd_bread (buf, 4, abfd) != 4)
1645 abfd->start_address = bfd_h_get_32 (abfd, buf);
1653 bfd_mach_o_scan (abfd, header, mdata)
1655 bfd_mach_o_header *header;
1656 bfd_mach_o_data_struct *mdata;
1659 enum bfd_architecture cputype;
1660 unsigned long cpusubtype;
1662 mdata->header = *header;
1663 mdata->symbols = NULL;
1665 abfd->flags = (abfd->xvec->object_flags
1666 | (abfd->flags & (BFD_IN_MEMORY | BFD_IO_FUNCS)));
1667 abfd->tdata.mach_o_data = mdata;
1669 bfd_mach_o_convert_architecture (header->cputype, header->cpusubtype,
1670 &cputype, &cpusubtype);
1671 if (cputype == bfd_arch_unknown)
1673 fprintf (stderr, "bfd_mach_o_scan: unknown architecture 0x%lx/0x%lx\n",
1674 header->cputype, header->cpusubtype);
1678 bfd_set_arch_mach (abfd, cputype, cpusubtype);
1680 if (header->ncmds != 0)
1683 ((bfd_mach_o_load_command *)
1684 bfd_alloc (abfd, header->ncmds * sizeof (bfd_mach_o_load_command)));
1685 if (mdata->commands == NULL)
1688 for (i = 0; i < header->ncmds; i++)
1690 bfd_mach_o_load_command *cur = &mdata->commands[i];
1696 bfd_mach_o_load_command *prev = &mdata->commands[i - 1];
1697 cur->offset = prev->offset + prev->len;
1700 if (bfd_mach_o_scan_read_command (abfd, cur) < 0)
1705 if (bfd_mach_o_scan_start_address (abfd) < 0)
1708 fprintf (stderr, "bfd_mach_o_scan: unable to scan start address: %s\n",
1709 bfd_errmsg (bfd_get_error ()));
1710 abfd->tdata.mach_o_data = NULL;
1715 bfd_mach_o_flatten_sections (abfd);
1721 bfd_mach_o_mkobject (abfd)
1724 bfd_mach_o_data_struct *mdata = NULL;
1726 mdata = ((bfd_mach_o_data_struct *)
1727 bfd_alloc (abfd, sizeof (bfd_mach_o_data_struct)));
1730 abfd->tdata.mach_o_data = mdata;
1732 mdata->header.magic = 0;
1733 mdata->header.cputype = 0;
1734 mdata->header.cpusubtype = 0;
1735 mdata->header.filetype = 0;
1736 mdata->header.ncmds = 0;
1737 mdata->header.sizeofcmds = 0;
1738 mdata->header.flags = 0;
1739 mdata->header.byteorder = BFD_ENDIAN_UNKNOWN;
1740 mdata->commands = NULL;
1741 mdata->nsymbols = 0;
1742 mdata->symbols = NULL;
1744 mdata->sections = NULL;
1751 bfd_mach_o_object_p (abfd)
1754 struct bfd_preserve preserve;
1755 bfd_mach_o_header header;
1757 preserve.marker = NULL;
1758 if (bfd_mach_o_read_header (abfd, &header) != 0)
1761 if (! (header.byteorder == BFD_ENDIAN_BIG
1762 || header.byteorder == BFD_ENDIAN_LITTLE))
1764 fprintf (stderr, "unknown header byte-order value 0x%lx\n",
1765 (long) header.byteorder);
1769 if (! ((header.byteorder == BFD_ENDIAN_BIG
1770 && abfd->xvec->byteorder == BFD_ENDIAN_BIG
1771 && abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
1772 || (header.byteorder == BFD_ENDIAN_LITTLE
1773 && abfd->xvec->byteorder == BFD_ENDIAN_LITTLE
1774 && abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)))
1777 preserve.marker = bfd_zalloc (abfd, sizeof (bfd_mach_o_data_struct));
1778 if (preserve.marker == NULL
1779 || !bfd_preserve_save (abfd, &preserve))
1782 if (bfd_mach_o_scan (abfd, &header,
1783 (bfd_mach_o_data_struct *) preserve.marker) != 0)
1786 bfd_preserve_finish (abfd, &preserve);
1790 bfd_set_error (bfd_error_wrong_format);
1793 if (preserve.marker != NULL)
1794 bfd_preserve_restore (abfd, &preserve);
1799 bfd_mach_o_core_p (abfd)
1802 struct bfd_preserve preserve;
1803 bfd_mach_o_header header;
1805 preserve.marker = NULL;
1806 if (bfd_mach_o_read_header (abfd, &header) != 0)
1809 if (! (header.byteorder == BFD_ENDIAN_BIG
1810 || header.byteorder == BFD_ENDIAN_LITTLE))
1812 fprintf (stderr, "unknown header byte-order value 0x%lx\n",
1813 (long) header.byteorder);
1817 if (! ((header.byteorder == BFD_ENDIAN_BIG
1818 && abfd->xvec->byteorder == BFD_ENDIAN_BIG
1819 && abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
1820 || (header.byteorder == BFD_ENDIAN_LITTLE
1821 && abfd->xvec->byteorder == BFD_ENDIAN_LITTLE
1822 && abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)))
1825 if (header.filetype != BFD_MACH_O_MH_CORE)
1828 preserve.marker = bfd_zalloc (abfd, sizeof (bfd_mach_o_data_struct));
1829 if (preserve.marker == NULL
1830 || !bfd_preserve_save (abfd, &preserve))
1833 if (bfd_mach_o_scan (abfd, &header,
1834 (bfd_mach_o_data_struct *) preserve.marker) != 0)
1837 bfd_preserve_finish (abfd, &preserve);
1841 bfd_set_error (bfd_error_wrong_format);
1844 if (preserve.marker != NULL)
1845 bfd_preserve_restore (abfd, &preserve);
1849 typedef struct mach_o_fat_archentry
1851 unsigned long cputype;
1852 unsigned long cpusubtype;
1853 unsigned long offset;
1855 unsigned long align;
1857 } mach_o_fat_archentry;
1859 typedef struct mach_o_fat_data_struct
1861 unsigned long magic;
1862 unsigned long nfat_arch;
1863 mach_o_fat_archentry *archentries;
1864 } mach_o_fat_data_struct;
1867 bfd_mach_o_archive_p (abfd)
1870 mach_o_fat_data_struct *adata = NULL;
1871 unsigned char buf[20];
1874 bfd_seek (abfd, 0, SEEK_SET);
1875 if (bfd_bread ((PTR) buf, 8, abfd) != 8)
1878 adata = (mach_o_fat_data_struct *)
1879 bfd_alloc (abfd, sizeof (mach_o_fat_data_struct));
1883 adata->magic = bfd_getb32 (buf);
1884 adata->nfat_arch = bfd_getb32 (buf + 4);
1885 if (adata->magic != 0xcafebabe)
1888 adata->archentries = (mach_o_fat_archentry *)
1889 bfd_alloc (abfd, adata->nfat_arch * sizeof (mach_o_fat_archentry));
1890 if (adata->archentries == NULL)
1893 for (i = 0; i < adata->nfat_arch; i++)
1895 bfd_seek (abfd, 8 + 20 * i, SEEK_SET);
1897 if (bfd_bread ((PTR) buf, 20, abfd) != 20)
1899 adata->archentries[i].cputype = bfd_getb32 (buf);
1900 adata->archentries[i].cpusubtype = bfd_getb32 (buf + 4);
1901 adata->archentries[i].offset = bfd_getb32 (buf + 8);
1902 adata->archentries[i].size = bfd_getb32 (buf + 12);
1903 adata->archentries[i].align = bfd_getb32 (buf + 16);
1904 adata->archentries[i].abfd = NULL;
1907 abfd->tdata.mach_o_fat_data = adata;
1912 bfd_release (abfd, adata);
1913 bfd_set_error (bfd_error_wrong_format);
1918 bfd_mach_o_openr_next_archived_file (archive, prev)
1922 mach_o_fat_data_struct *adata;
1923 mach_o_fat_archentry *entry = NULL;
1926 adata = (mach_o_fat_data_struct *) archive->tdata.mach_o_fat_data;
1927 BFD_ASSERT (adata != NULL);
1929 /* Find index of previous entry. */
1931 i = 0; /* Start at first one. */
1934 for (i = 0; i < adata->nfat_arch; i++)
1936 if (adata->archentries[i].abfd == prev)
1940 if (i == adata->nfat_arch)
1943 bfd_set_error (bfd_error_bad_value);
1946 i++; /* Get next entry. */
1949 if (i >= adata->nfat_arch)
1951 bfd_set_error (bfd_error_no_more_archived_files);
1955 entry = &adata->archentries[i];
1956 if (entry->abfd == NULL)
1958 bfd *nbfd = _bfd_new_bfd_contained_in (archive);
1964 nbfd->origin = entry->offset;
1965 s = bfd_malloc (strlen (archive->filename) + 1);
1968 strcpy (s, archive->filename);
1970 nbfd->iostream = NULL;
1978 bfd_mach_o_lookup_section (abfd, section, mcommand, msection)
1981 bfd_mach_o_load_command **mcommand;
1982 bfd_mach_o_section **msection;
1984 struct mach_o_data_struct *md = abfd->tdata.mach_o_data;
1985 unsigned int i, j, num;
1987 bfd_mach_o_load_command *ncmd = NULL;
1988 bfd_mach_o_section *nsect = NULL;
1990 BFD_ASSERT (mcommand != NULL);
1991 BFD_ASSERT (msection != NULL);
1994 for (i = 0; i < md->header.ncmds; i++)
1996 struct bfd_mach_o_load_command *cmd = &md->commands[i];
1997 struct bfd_mach_o_segment_command *seg = NULL;
1999 if (cmd->type != BFD_MACH_O_LC_SEGMENT)
2001 seg = &cmd->command.segment;
2003 if (seg->segment == section)
2010 for (j = 0; j < seg->nsects; j++)
2012 struct bfd_mach_o_section *sect = &seg->sections[j];
2014 if (sect->bfdsection == section)
2029 bfd_mach_o_lookup_command (abfd, type, mcommand)
2031 bfd_mach_o_load_command_type type;
2032 bfd_mach_o_load_command **mcommand;
2034 struct mach_o_data_struct *md = NULL;
2035 bfd_mach_o_load_command *ncmd = NULL;
2036 unsigned int i, num;
2038 md = abfd->tdata.mach_o_data;
2040 BFD_ASSERT (md != NULL);
2041 BFD_ASSERT (mcommand != NULL);
2044 for (i = 0; i < md->header.ncmds; i++)
2046 struct bfd_mach_o_load_command *cmd = &md->commands[i];
2048 if (cmd->type != type)
2061 bfd_mach_o_stack_addr (type)
2062 enum bfd_mach_o_cpu_type type;
2066 case BFD_MACH_O_CPU_TYPE_MC680x0:
2068 case BFD_MACH_O_CPU_TYPE_MC88000:
2070 case BFD_MACH_O_CPU_TYPE_POWERPC:
2072 case BFD_MACH_O_CPU_TYPE_I386:
2074 case BFD_MACH_O_CPU_TYPE_SPARC:
2076 case BFD_MACH_O_CPU_TYPE_I860:
2078 case BFD_MACH_O_CPU_TYPE_HPPA:
2079 return 0xc0000000 - 0x04000000;
2086 bfd_mach_o_core_fetch_environment (abfd, rbuf, rlen)
2088 unsigned char **rbuf;
2091 bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
2092 unsigned long stackaddr = bfd_mach_o_stack_addr (mdata->header.cputype);
2095 for (i = 0; i < mdata->header.ncmds; i++)
2097 bfd_mach_o_load_command *cur = &mdata->commands[i];
2098 bfd_mach_o_segment_command *seg = NULL;
2100 if (cur->type != BFD_MACH_O_LC_SEGMENT)
2103 seg = &cur->command.segment;
2105 if ((seg->vmaddr + seg->vmsize) == stackaddr)
2107 unsigned long start = seg->fileoff;
2108 unsigned long end = seg->fileoff + seg->filesize;
2109 unsigned char *buf = bfd_malloc (1024);
2110 unsigned long size = 1024;
2114 bfd_size_type nread = 0;
2115 unsigned long offset;
2116 int found_nonnull = 0;
2118 if (size > (end - start))
2119 size = (end - start);
2121 buf = bfd_realloc (buf, size);
2123 bfd_seek (abfd, end - size, SEEK_SET);
2124 nread = bfd_bread (buf, size, abfd);
2129 for (offset = 4; offset <= size; offset += 4)
2133 val = *((unsigned long *) (buf + size - offset));
2134 if (! found_nonnull)
2139 else if (val == 0x0)
2141 unsigned long bottom;
2144 bottom = seg->fileoff + seg->filesize - offset;
2145 top = seg->fileoff + seg->filesize - 4;
2146 *rbuf = bfd_malloc (top - bottom);
2147 *rlen = top - bottom;
2149 memcpy (*rbuf, buf + size - *rlen, *rlen);
2154 if (size == (end - start))
2166 bfd_mach_o_core_file_failing_command (abfd)
2169 unsigned char *buf = NULL;
2170 unsigned int len = 0;
2173 ret = bfd_mach_o_core_fetch_environment (abfd, &buf, &len);
2181 bfd_mach_o_core_file_failing_signal (abfd)
2182 bfd *abfd ATTRIBUTE_UNUSED;
2188 bfd_mach_o_core_file_matches_executable_p (core_bfd, exec_bfd)
2189 bfd *core_bfd ATTRIBUTE_UNUSED;
2190 bfd *exec_bfd ATTRIBUTE_UNUSED;
2195 #define TARGET_NAME mach_o_be_vec
2196 #define TARGET_STRING "mach-o-be"
2197 #define TARGET_BIG_ENDIAN 1
2198 #define TARGET_ARCHIVE 0
2200 #include "mach-o-target.c"
2203 #undef TARGET_STRING
2204 #undef TARGET_BIG_ENDIAN
2205 #undef TARGET_ARCHIVE
2207 #define TARGET_NAME mach_o_le_vec
2208 #define TARGET_STRING "mach-o-le"
2209 #define TARGET_BIG_ENDIAN 0
2210 #define TARGET_ARCHIVE 0
2212 #include "mach-o-target.c"
2215 #undef TARGET_STRING
2216 #undef TARGET_BIG_ENDIAN
2217 #undef TARGET_ARCHIVE
2219 #define TARGET_NAME mach_o_fat_vec
2220 #define TARGET_STRING "mach-o-fat"
2221 #define TARGET_BIG_ENDIAN 1
2222 #define TARGET_ARCHIVE 1
2224 #include "mach-o-target.c"
2227 #undef TARGET_STRING
2228 #undef TARGET_BIG_ENDIAN
2229 #undef TARGET_ARCHIVE