1 /* Mach-O support for BFD.
2 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, 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_find_inliner_info _bfd_nosymbols_find_inliner_info
52 #define bfd_mach_o_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
53 #define bfd_mach_o_read_minisymbols _bfd_generic_read_minisymbols
54 #define bfd_mach_o_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
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_section_already_linked _bfd_generic_section_already_linked
74 #define bfd_mach_o_bfd_copy_private_header_data _bfd_generic_bfd_copy_private_header_data
75 #define bfd_mach_o_core_file_matches_executable_p generic_core_file_matches_executable_p
78 /* The flags field of a section structure is separated into two parts a section
79 type and section attributes. The section types are mutually exclusive (it
80 can only have one type) but the section attributes are not (it may have more
81 than one attribute). */
83 #define SECTION_TYPE 0x000000ff /* 256 section types. */
84 #define SECTION_ATTRIBUTES 0xffffff00 /* 24 section attributes. */
86 /* Constants for the section attributes part of the flags field of a section
89 #define SECTION_ATTRIBUTES_USR 0xff000000 /* User-settable attributes. */
90 #define S_ATTR_PURE_INSTRUCTIONS 0x80000000 /* Section contains only true machine instructions. */
91 #define SECTION_ATTRIBUTES_SYS 0x00ffff00 /* System setable attributes. */
92 #define S_ATTR_SOME_INSTRUCTIONS 0x00000400 /* Section contains some machine instructions. */
93 #define S_ATTR_EXT_RELOC 0x00000200 /* Section has external relocation entries. */
94 #define S_ATTR_LOC_RELOC 0x00000100 /* Section has local relocation entries. */
105 bfd_mach_o_valid (bfd *abfd)
107 if (abfd == NULL || abfd->xvec == NULL)
110 if (! ((abfd->xvec == &mach_o_be_vec)
111 || (abfd->xvec == &mach_o_le_vec)
112 || (abfd->xvec == &mach_o_fat_vec)))
115 if (abfd->tdata.mach_o_data == NULL)
120 /* Copy any private info we understand from the input symbol
121 to the output symbol. */
124 bfd_mach_o_bfd_copy_private_symbol_data (bfd *ibfd ATTRIBUTE_UNUSED,
125 asymbol *isymbol ATTRIBUTE_UNUSED,
126 bfd *obfd ATTRIBUTE_UNUSED,
127 asymbol *osymbol ATTRIBUTE_UNUSED)
132 /* Copy any private info we understand from the input section
133 to the output section. */
136 bfd_mach_o_bfd_copy_private_section_data (bfd *ibfd ATTRIBUTE_UNUSED,
137 asection *isection ATTRIBUTE_UNUSED,
138 bfd *obfd ATTRIBUTE_UNUSED,
139 asection *osection ATTRIBUTE_UNUSED)
144 /* Copy any private info we understand from the input bfd
145 to the output bfd. */
148 bfd_mach_o_bfd_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
150 BFD_ASSERT (bfd_mach_o_valid (ibfd));
151 BFD_ASSERT (bfd_mach_o_valid (obfd));
153 obfd->tdata.mach_o_data = ibfd->tdata.mach_o_data;
154 obfd->tdata.mach_o_data->ibfd = ibfd;
159 bfd_mach_o_count_symbols (bfd *abfd)
161 bfd_mach_o_data_struct *mdata = NULL;
165 BFD_ASSERT (bfd_mach_o_valid (abfd));
166 mdata = abfd->tdata.mach_o_data;
168 for (i = 0; i < mdata->header.ncmds; i++)
169 if (mdata->commands[i].type == BFD_MACH_O_LC_SYMTAB)
171 bfd_mach_o_symtab_command *sym = &mdata->commands[i].command.symtab;
179 bfd_mach_o_get_symtab_upper_bound (bfd *abfd)
181 long nsyms = bfd_mach_o_count_symbols (abfd);
186 return ((nsyms + 1) * sizeof (asymbol *));
190 bfd_mach_o_canonicalize_symtab (bfd *abfd, asymbol **alocation)
192 bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
193 long nsyms = bfd_mach_o_count_symbols (abfd);
194 asymbol **csym = alocation;
200 for (i = 0; i < mdata->header.ncmds; i++)
202 if (mdata->commands[i].type == BFD_MACH_O_LC_SYMTAB)
204 bfd_mach_o_symtab_command *sym = &mdata->commands[i].command.symtab;
206 if (bfd_mach_o_scan_read_symtab_symbols (abfd, &mdata->commands[i].command.symtab) != 0)
208 fprintf (stderr, "bfd_mach_o_canonicalize_symtab: unable to load symbols for section %lu\n", i);
212 BFD_ASSERT (sym->symbols != NULL);
214 for (j = 0; j < sym->nsyms; j++)
216 BFD_ASSERT (csym < (alocation + nsyms));
217 *csym++ = &sym->symbols[j];
228 bfd_mach_o_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
232 bfd_symbol_info (symbol, ret);
236 bfd_mach_o_print_symbol (bfd *abfd,
239 bfd_print_symbol_type how)
241 FILE *file = (FILE *) afile;
245 case bfd_print_symbol_name:
246 fprintf (file, "%s", symbol->name);
249 bfd_print_symbol_vandf (abfd, (PTR) file, symbol);
250 fprintf (file, " %-5s %s", symbol->section->name, symbol->name);
255 bfd_mach_o_convert_architecture (bfd_mach_o_cpu_type mtype,
256 bfd_mach_o_cpu_subtype msubtype ATTRIBUTE_UNUSED,
257 enum bfd_architecture *type,
258 unsigned long *subtype)
260 *subtype = bfd_arch_unknown;
264 case BFD_MACH_O_CPU_TYPE_VAX: *type = bfd_arch_vax; break;
265 case BFD_MACH_O_CPU_TYPE_MC680x0: *type = bfd_arch_m68k; break;
266 case BFD_MACH_O_CPU_TYPE_I386: *type = bfd_arch_i386; break;
267 case BFD_MACH_O_CPU_TYPE_MIPS: *type = bfd_arch_mips; break;
268 case BFD_MACH_O_CPU_TYPE_MC98000: *type = bfd_arch_m98k; break;
269 case BFD_MACH_O_CPU_TYPE_HPPA: *type = bfd_arch_hppa; break;
270 case BFD_MACH_O_CPU_TYPE_ARM: *type = bfd_arch_arm; break;
271 case BFD_MACH_O_CPU_TYPE_MC88000: *type = bfd_arch_m88k; break;
272 case BFD_MACH_O_CPU_TYPE_SPARC: *type = bfd_arch_sparc; break;
273 case BFD_MACH_O_CPU_TYPE_I860: *type = bfd_arch_i860; break;
274 case BFD_MACH_O_CPU_TYPE_ALPHA: *type = bfd_arch_alpha; break;
275 case BFD_MACH_O_CPU_TYPE_POWERPC: *type = bfd_arch_powerpc; break;
276 default: *type = bfd_arch_unknown; break;
281 case bfd_arch_i386: *subtype = bfd_mach_i386_i386; break;
282 case bfd_arch_sparc: *subtype = bfd_mach_sparc; break;
284 *subtype = bfd_arch_unknown;
289 bfd_mach_o_write_header (bfd *abfd, bfd_mach_o_header *header)
291 unsigned char buf[28];
293 bfd_h_put_32 (abfd, header->magic, buf + 0);
294 bfd_h_put_32 (abfd, header->cputype, buf + 4);
295 bfd_h_put_32 (abfd, header->cpusubtype, buf + 8);
296 bfd_h_put_32 (abfd, header->filetype, buf + 12);
297 bfd_h_put_32 (abfd, header->ncmds, buf + 16);
298 bfd_h_put_32 (abfd, header->sizeofcmds, buf + 20);
299 bfd_h_put_32 (abfd, header->flags, buf + 24);
301 bfd_seek (abfd, 0, SEEK_SET);
302 if (bfd_bwrite ((PTR) buf, 28, abfd) != 28)
309 bfd_mach_o_scan_write_thread (bfd *abfd,
310 bfd_mach_o_load_command *command)
312 bfd_mach_o_thread_command *cmd = &command->command.thread;
314 unsigned char buf[8];
316 unsigned int nflavours;
318 BFD_ASSERT ((command->type == BFD_MACH_O_LC_THREAD)
319 || (command->type == BFD_MACH_O_LC_UNIXTHREAD));
323 for (i = 0; i < cmd->nflavours; i++)
325 BFD_ASSERT ((cmd->flavours[i].size % 4) == 0);
326 BFD_ASSERT (cmd->flavours[i].offset == (command->offset + offset + 8));
328 bfd_h_put_32 (abfd, cmd->flavours[i].flavour, buf);
329 bfd_h_put_32 (abfd, (cmd->flavours[i].size / 4), buf + 4);
331 bfd_seek (abfd, command->offset + offset, SEEK_SET);
332 if (bfd_bwrite ((PTR) buf, 8, abfd) != 8)
335 offset += cmd->flavours[i].size + 8;
342 bfd_mach_o_scan_write_section (bfd *abfd,
343 bfd_mach_o_section *section,
346 unsigned char buf[68];
348 memcpy (buf, section->sectname, 16);
349 memcpy (buf + 16, section->segname, 16);
350 bfd_h_put_32 (abfd, section->addr, buf + 32);
351 bfd_h_put_32 (abfd, section->size, buf + 36);
352 bfd_h_put_32 (abfd, section->offset, buf + 40);
353 bfd_h_put_32 (abfd, section->align, buf + 44);
354 bfd_h_put_32 (abfd, section->reloff, buf + 48);
355 bfd_h_put_32 (abfd, section->nreloc, buf + 52);
356 bfd_h_put_32 (abfd, section->flags, buf + 56);
357 /* bfd_h_put_32 (abfd, section->reserved1, buf + 60); */
358 /* bfd_h_put_32 (abfd, section->reserved2, buf + 64); */
360 bfd_seek (abfd, offset, SEEK_SET);
361 if (bfd_bwrite ((PTR) buf, 68, abfd) != 68)
368 bfd_mach_o_scan_write_segment (bfd *abfd, bfd_mach_o_load_command *command)
370 unsigned char buf[48];
371 bfd_mach_o_segment_command *seg = &command->command.segment;
374 BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT);
376 memcpy (buf, seg->segname, 16);
377 bfd_h_put_32 (abfd, seg->vmaddr, buf + 16);
378 bfd_h_put_32 (abfd, seg->vmsize, buf + 20);
379 bfd_h_put_32 (abfd, seg->fileoff, buf + 24);
380 bfd_h_put_32 (abfd, seg->filesize, buf + 28);
381 bfd_h_put_32 (abfd, 0 /* seg->maxprot */, buf + 32);
382 bfd_h_put_32 (abfd, 0 /* seg->initprot */, buf + 36);
383 bfd_h_put_32 (abfd, seg->nsects, buf + 40);
384 bfd_h_put_32 (abfd, seg->flags, buf + 44);
386 bfd_seek (abfd, command->offset + 8, SEEK_SET);
387 if (bfd_bwrite ((PTR) buf, 48, abfd) != 48)
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)
415 for (i = 0; i < seg->nsects; i++)
417 bfd_vma segoff = command->offset + 48 + 8 + (i * 68);
419 if (bfd_mach_o_scan_write_section (abfd, &seg->sections[i], segoff) != 0)
427 bfd_mach_o_scan_write_symtab_symbols (bfd *abfd,
428 bfd_mach_o_load_command *command)
430 bfd_mach_o_symtab_command *sym = &command->command.symtab;
434 for (i = 0; i < sym->nsyms; i++)
436 unsigned char buf[12];
437 bfd_vma symoff = sym->symoff + (i * 12);
438 unsigned char ntype = 0;
439 unsigned char nsect = 0;
442 s = &sym->symbols[i];
444 /* Instead just set from the stored values. */
445 ntype = (s->udata.i >> 24) & 0xff;
446 nsect = (s->udata.i >> 16) & 0xff;
447 ndesc = s->udata.i & 0xffff;
449 bfd_h_put_32 (abfd, s->name - sym->strtab, buf);
450 bfd_h_put_8 (abfd, ntype, buf + 4);
451 bfd_h_put_8 (abfd, nsect, buf + 5);
452 bfd_h_put_16 (abfd, ndesc, buf + 6);
453 bfd_h_put_32 (abfd, s->section->vma + s->value, buf + 8);
455 bfd_seek (abfd, symoff, SEEK_SET);
456 if (bfd_bwrite ((PTR) buf, 12, abfd) != 12)
458 fprintf (stderr, "bfd_mach_o_scan_write_symtab_symbols: unable to write %d bytes at %lu\n",
459 12, (unsigned long) symoff);
468 bfd_mach_o_scan_write_symtab (bfd *abfd, bfd_mach_o_load_command *command)
470 bfd_mach_o_symtab_command *seg = &command->command.symtab;
471 unsigned char buf[16];
473 BFD_ASSERT (command->type == BFD_MACH_O_LC_SYMTAB);
475 bfd_h_put_32 (abfd, seg->symoff, buf);
476 bfd_h_put_32 (abfd, seg->nsyms, buf + 4);
477 bfd_h_put_32 (abfd, seg->stroff, buf + 8);
478 bfd_h_put_32 (abfd, seg->strsize, buf + 12);
480 bfd_seek (abfd, command->offset + 8, SEEK_SET);
481 if (bfd_bwrite ((PTR) buf, 16, abfd) != 16)
484 if (bfd_mach_o_scan_write_symtab_symbols (abfd, command) != 0)
491 bfd_mach_o_write_contents (bfd *abfd)
496 bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
498 /* Write data sections first in case they overlap header data to be
501 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
504 /* Now write header information. */
505 if (bfd_mach_o_write_header (abfd, &mdata->header) != 0)
508 for (i = 0; i < mdata->header.ncmds; i++)
510 unsigned char buf[8];
511 bfd_mach_o_load_command *cur = &mdata->commands[i];
512 unsigned long typeflag;
514 typeflag = cur->type_required ? cur->type & BFD_MACH_O_LC_REQ_DYLD : cur->type;
516 bfd_h_put_32 (abfd, typeflag, buf);
517 bfd_h_put_32 (abfd, cur->len, buf + 4);
519 bfd_seek (abfd, cur->offset, SEEK_SET);
520 if (bfd_bwrite ((PTR) buf, 8, abfd) != 8)
525 case BFD_MACH_O_LC_SEGMENT:
526 if (bfd_mach_o_scan_write_segment (abfd, cur) != 0)
529 case BFD_MACH_O_LC_SYMTAB:
530 if (bfd_mach_o_scan_write_symtab (abfd, cur) != 0)
533 case BFD_MACH_O_LC_SYMSEG:
535 case BFD_MACH_O_LC_THREAD:
536 case BFD_MACH_O_LC_UNIXTHREAD:
537 if (bfd_mach_o_scan_write_thread (abfd, cur) != 0)
540 case BFD_MACH_O_LC_LOADFVMLIB:
541 case BFD_MACH_O_LC_IDFVMLIB:
542 case BFD_MACH_O_LC_IDENT:
543 case BFD_MACH_O_LC_FVMFILE:
544 case BFD_MACH_O_LC_PREPAGE:
545 case BFD_MACH_O_LC_DYSYMTAB:
546 case BFD_MACH_O_LC_LOAD_DYLIB:
547 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB:
548 case BFD_MACH_O_LC_ID_DYLIB:
549 case BFD_MACH_O_LC_LOAD_DYLINKER:
550 case BFD_MACH_O_LC_ID_DYLINKER:
551 case BFD_MACH_O_LC_PREBOUND_DYLIB:
552 case BFD_MACH_O_LC_ROUTINES:
553 case BFD_MACH_O_LC_SUB_FRAMEWORK:
557 "unable to write unknown load command 0x%lx\n",
567 bfd_mach_o_sizeof_headers (bfd *a ATTRIBUTE_UNUSED,
568 struct bfd_link_info *info ATTRIBUTE_UNUSED)
573 /* Make an empty symbol. This is required only because
574 bfd_make_section_anyway wants to create a symbol for the section. */
577 bfd_mach_o_make_empty_symbol (bfd *abfd)
581 new = bfd_zalloc (abfd, sizeof (* new));
589 bfd_mach_o_read_header (bfd *abfd, bfd_mach_o_header *header)
591 unsigned char buf[28];
592 bfd_vma (*get32) (const void *) = NULL;
594 bfd_seek (abfd, 0, SEEK_SET);
596 if (bfd_bread ((PTR) buf, 28, abfd) != 28)
599 if (bfd_getb32 (buf) == 0xfeedface)
601 header->byteorder = BFD_ENDIAN_BIG;
602 header->magic = 0xfeedface;
605 else if (bfd_getl32 (buf) == 0xfeedface)
607 header->byteorder = BFD_ENDIAN_LITTLE;
608 header->magic = 0xfeedface;
613 header->byteorder = BFD_ENDIAN_UNKNOWN;
617 header->cputype = (*get32) (buf + 4);
618 header->cpusubtype = (*get32) (buf + 8);
619 header->filetype = (*get32) (buf + 12);
620 header->ncmds = (*get32) (buf + 16);
621 header->sizeofcmds = (*get32) (buf + 20);
622 header->flags = (*get32) (buf + 24);
628 bfd_mach_o_make_bfd_section (bfd *abfd, bfd_mach_o_section *section)
632 const char *prefix = "LC_SEGMENT";
633 unsigned int snamelen;
636 snamelen = strlen (prefix) + 1
637 + strlen (section->segname) + 1
638 + strlen (section->sectname) + 1;
640 sname = bfd_alloc (abfd, snamelen);
643 sprintf (sname, "%s.%s.%s", prefix, section->segname, section->sectname);
646 if (!(section->flags & BFD_MACH_O_S_ZEROFILL))
647 flags = SEC_HAS_CONTENTS | SEC_LOAD | SEC_ALLOC | SEC_CODE;
648 bfdsec = bfd_make_section_anyway_with_flags (abfd, sname, flags);
652 bfdsec->vma = section->addr;
653 bfdsec->lma = section->addr;
654 bfdsec->size = section->size;
655 bfdsec->filepos = section->offset;
656 bfdsec->alignment_power = section->align;
662 bfd_mach_o_scan_read_section (bfd *abfd,
663 bfd_mach_o_section *section,
666 unsigned char buf[68];
668 bfd_seek (abfd, offset, SEEK_SET);
669 if (bfd_bread ((PTR) buf, 68, abfd) != 68)
672 memcpy (section->sectname, buf, 16);
673 section->sectname[16] = '\0';
674 memcpy (section->segname, buf + 16, 16);
675 section->segname[16] = '\0';
676 section->addr = bfd_h_get_32 (abfd, buf + 32);
677 section->size = bfd_h_get_32 (abfd, buf + 36);
678 section->offset = bfd_h_get_32 (abfd, buf + 40);
679 section->align = bfd_h_get_32 (abfd, buf + 44);
680 section->reloff = bfd_h_get_32 (abfd, buf + 48);
681 section->nreloc = bfd_h_get_32 (abfd, buf + 52);
682 section->flags = bfd_h_get_32 (abfd, buf + 56);
683 section->reserved1 = bfd_h_get_32 (abfd, buf + 60);
684 section->reserved2 = bfd_h_get_32 (abfd, buf + 64);
685 section->bfdsection = bfd_mach_o_make_bfd_section (abfd, section);
687 if (section->bfdsection == NULL)
694 bfd_mach_o_scan_read_symtab_symbol (bfd *abfd,
695 bfd_mach_o_symtab_command *sym,
699 bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
700 bfd_vma symoff = sym->symoff + (i * 12);
701 unsigned char buf[12];
702 unsigned char type = -1;
703 unsigned char section = -1;
705 unsigned long value = -1;
706 unsigned long stroff = -1;
707 unsigned int symtype = -1;
709 BFD_ASSERT (sym->strtab != NULL);
711 bfd_seek (abfd, symoff, SEEK_SET);
712 if (bfd_bread ((PTR) buf, 12, abfd) != 12)
714 fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: unable to read %d bytes at %lu\n",
715 12, (unsigned long) symoff);
719 stroff = bfd_h_get_32 (abfd, buf);
720 type = bfd_h_get_8 (abfd, buf + 4);
721 symtype = (type & 0x0e);
722 section = bfd_h_get_8 (abfd, buf + 5) - 1;
723 desc = bfd_h_get_16 (abfd, buf + 6);
724 value = bfd_h_get_32 (abfd, buf + 8);
726 if (stroff >= sym->strsize)
728 fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: symbol name out of range (%lu >= %lu)\n",
729 (unsigned long) stroff, (unsigned long) sym->strsize);
734 s->name = sym->strtab + stroff;
736 s->udata.i = (type << 24) | (section << 16) | desc;
739 if (type & BFD_MACH_O_N_STAB)
741 s->flags |= BSF_DEBUGGING;
742 s->section = bfd_und_section_ptr;
746 if (type & BFD_MACH_O_N_PEXT)
748 type &= ~BFD_MACH_O_N_PEXT;
749 s->flags |= BSF_GLOBAL;
752 if (type & BFD_MACH_O_N_EXT)
754 type &= ~BFD_MACH_O_N_EXT;
755 s->flags |= BSF_GLOBAL;
760 case BFD_MACH_O_N_UNDF:
761 s->section = bfd_und_section_ptr;
763 case BFD_MACH_O_N_PBUD:
764 s->section = bfd_und_section_ptr;
766 case BFD_MACH_O_N_ABS:
767 s->section = bfd_abs_section_ptr;
769 case BFD_MACH_O_N_SECT:
770 if ((section > 0) && (section <= mdata->nsects))
772 s->section = mdata->sections[section - 1]->bfdsection;
773 s->value = s->value - mdata->sections[section - 1]->addr;
777 /* Mach-O uses 0 to mean "no section"; not an error. */
780 fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: "
781 "symbol \"%s\" specified invalid section %d (max %lu): setting to undefined\n",
782 s->name, section, mdata->nsects);
784 s->section = bfd_und_section_ptr;
787 case BFD_MACH_O_N_INDR:
788 fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: "
789 "symbol \"%s\" is unsupported 'indirect' reference: setting to undefined\n",
791 s->section = bfd_und_section_ptr;
794 fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: "
795 "symbol \"%s\" specified invalid type field 0x%x: setting to undefined\n",
797 s->section = bfd_und_section_ptr;
806 bfd_mach_o_scan_read_symtab_strtab (bfd *abfd,
807 bfd_mach_o_symtab_command *sym)
809 BFD_ASSERT (sym->strtab == NULL);
811 if (abfd->flags & BFD_IN_MEMORY)
813 struct bfd_in_memory *b;
815 b = (struct bfd_in_memory *) abfd->iostream;
817 if ((sym->stroff + sym->strsize) > b->size)
819 bfd_set_error (bfd_error_file_truncated);
822 sym->strtab = (char *) b->buffer + sym->stroff;
826 sym->strtab = bfd_alloc (abfd, sym->strsize);
827 if (sym->strtab == NULL)
830 bfd_seek (abfd, sym->stroff, SEEK_SET);
831 if (bfd_bread ((PTR) sym->strtab, sym->strsize, abfd) != sym->strsize)
833 fprintf (stderr, "bfd_mach_o_scan_read_symtab_strtab: unable to read %lu bytes at %lu\n",
834 sym->strsize, sym->stroff);
842 bfd_mach_o_scan_read_symtab_symbols (bfd *abfd,
843 bfd_mach_o_symtab_command *sym)
848 BFD_ASSERT (sym->symbols == NULL);
849 sym->symbols = bfd_alloc (abfd, sym->nsyms * sizeof (asymbol));
851 if (sym->symbols == NULL)
853 fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbols: unable to allocate memory for symbols\n");
857 ret = bfd_mach_o_scan_read_symtab_strtab (abfd, sym);
861 for (i = 0; i < sym->nsyms; i++)
863 ret = bfd_mach_o_scan_read_symtab_symbol (abfd, sym, &sym->symbols[i], i);
872 bfd_mach_o_scan_read_dysymtab_symbol (bfd *abfd,
873 bfd_mach_o_dysymtab_command *dysym,
874 bfd_mach_o_symtab_command *sym,
878 unsigned long isymoff = dysym->indirectsymoff + (i * 4);
879 unsigned long symindex;
880 unsigned char buf[4];
882 BFD_ASSERT (i < dysym->nindirectsyms);
884 bfd_seek (abfd, isymoff, SEEK_SET);
885 if (bfd_bread ((PTR) buf, 4, abfd) != 4)
887 fprintf (stderr, "bfd_mach_o_scan_read_dysymtab_symbol: unable to read %lu bytes at %lu\n",
888 (unsigned long) 4, isymoff);
891 symindex = bfd_h_get_32 (abfd, buf);
893 return bfd_mach_o_scan_read_symtab_symbol (abfd, sym, s, symindex);
897 bfd_mach_o_i386_flavour_string (unsigned int flavour)
899 switch ((int) flavour)
901 case BFD_MACH_O_i386_NEW_THREAD_STATE: return "i386_NEW_THREAD_STATE";
902 case BFD_MACH_O_i386_FLOAT_STATE: return "i386_FLOAT_STATE";
903 case BFD_MACH_O_i386_ISA_PORT_MAP_STATE: return "i386_ISA_PORT_MAP_STATE";
904 case BFD_MACH_O_i386_V86_ASSIST_STATE: return "i386_V86_ASSIST_STATE";
905 case BFD_MACH_O_i386_REGS_SEGS_STATE: return "i386_REGS_SEGS_STATE";
906 case BFD_MACH_O_i386_THREAD_SYSCALL_STATE: return "i386_THREAD_SYSCALL_STATE";
907 case BFD_MACH_O_i386_THREAD_STATE_NONE: return "i386_THREAD_STATE_NONE";
908 case BFD_MACH_O_i386_SAVED_STATE: return "i386_SAVED_STATE";
909 case BFD_MACH_O_i386_THREAD_STATE: return "i386_THREAD_STATE";
910 case BFD_MACH_O_i386_THREAD_FPSTATE: return "i386_THREAD_FPSTATE";
911 case BFD_MACH_O_i386_THREAD_EXCEPTSTATE: return "i386_THREAD_EXCEPTSTATE";
912 case BFD_MACH_O_i386_THREAD_CTHREADSTATE: return "i386_THREAD_CTHREADSTATE";
913 default: return "UNKNOWN";
918 bfd_mach_o_ppc_flavour_string (unsigned int flavour)
920 switch ((int) flavour)
922 case BFD_MACH_O_PPC_THREAD_STATE: return "PPC_THREAD_STATE";
923 case BFD_MACH_O_PPC_FLOAT_STATE: return "PPC_FLOAT_STATE";
924 case BFD_MACH_O_PPC_EXCEPTION_STATE: return "PPC_EXCEPTION_STATE";
925 case BFD_MACH_O_PPC_VECTOR_STATE: return "PPC_VECTOR_STATE";
926 default: return "UNKNOWN";
931 bfd_mach_o_scan_read_dylinker (bfd *abfd,
932 bfd_mach_o_load_command *command)
934 bfd_mach_o_dylinker_command *cmd = &command->command.dylinker;
935 unsigned char buf[4];
936 unsigned int nameoff;
941 BFD_ASSERT ((command->type == BFD_MACH_O_LC_ID_DYLINKER)
942 || (command->type == BFD_MACH_O_LC_LOAD_DYLINKER));
944 bfd_seek (abfd, command->offset + 8, SEEK_SET);
945 if (bfd_bread ((PTR) buf, 4, abfd) != 4)
948 nameoff = bfd_h_get_32 (abfd, buf + 0);
950 cmd->name_offset = command->offset + nameoff;
951 cmd->name_len = command->len - nameoff;
953 if (command->type == BFD_MACH_O_LC_LOAD_DYLINKER)
954 prefix = "LC_LOAD_DYLINKER";
955 else if (command->type == BFD_MACH_O_LC_ID_DYLINKER)
956 prefix = "LC_ID_DYLINKER";
960 sname = bfd_alloc (abfd, strlen (prefix) + 1);
963 strcpy (sname, prefix);
965 bfdsec = bfd_make_section_anyway_with_flags (abfd, sname, SEC_HAS_CONTENTS);
971 bfdsec->size = command->len - 8;
972 bfdsec->filepos = command->offset + 8;
973 bfdsec->alignment_power = 0;
975 cmd->section = bfdsec;
981 bfd_mach_o_scan_read_dylib (bfd *abfd, bfd_mach_o_load_command *command)
983 bfd_mach_o_dylib_command *cmd = &command->command.dylib;
984 unsigned char buf[16];
985 unsigned int nameoff;
990 BFD_ASSERT ((command->type == BFD_MACH_O_LC_ID_DYLIB)
991 || (command->type == BFD_MACH_O_LC_LOAD_DYLIB)
992 || (command->type == BFD_MACH_O_LC_LOAD_WEAK_DYLIB));
994 bfd_seek (abfd, command->offset + 8, SEEK_SET);
995 if (bfd_bread ((PTR) buf, 16, abfd) != 16)
998 nameoff = bfd_h_get_32 (abfd, buf + 0);
999 cmd->timestamp = bfd_h_get_32 (abfd, buf + 4);
1000 cmd->current_version = bfd_h_get_32 (abfd, buf + 8);
1001 cmd->compatibility_version = bfd_h_get_32 (abfd, buf + 12);
1003 cmd->name_offset = command->offset + nameoff;
1004 cmd->name_len = command->len - nameoff;
1006 if (command->type == BFD_MACH_O_LC_LOAD_DYLIB)
1007 prefix = "LC_LOAD_DYLIB";
1008 else if (command->type == BFD_MACH_O_LC_LOAD_WEAK_DYLIB)
1009 prefix = "LC_LOAD_WEAK_DYLIB";
1010 else if (command->type == BFD_MACH_O_LC_ID_DYLIB)
1011 prefix = "LC_ID_DYLIB";
1015 sname = bfd_alloc (abfd, strlen (prefix) + 1);
1018 strcpy (sname, prefix);
1020 bfdsec = bfd_make_section_anyway_with_flags (abfd, sname, SEC_HAS_CONTENTS);
1026 bfdsec->size = command->len - 8;
1027 bfdsec->filepos = command->offset + 8;
1028 bfdsec->alignment_power = 0;
1030 cmd->section = bfdsec;
1036 bfd_mach_o_scan_read_prebound_dylib (bfd *abfd ATTRIBUTE_UNUSED,
1037 bfd_mach_o_load_command *command ATTRIBUTE_UNUSED)
1039 /* bfd_mach_o_prebound_dylib_command *cmd = &command->command.prebound_dylib; */
1041 BFD_ASSERT (command->type == BFD_MACH_O_LC_PREBOUND_DYLIB);
1046 bfd_mach_o_scan_read_thread (bfd *abfd, bfd_mach_o_load_command *command)
1048 bfd_mach_o_data_struct *mdata = NULL;
1049 bfd_mach_o_thread_command *cmd = &command->command.thread;
1050 unsigned char buf[8];
1052 unsigned int nflavours;
1055 BFD_ASSERT ((command->type == BFD_MACH_O_LC_THREAD)
1056 || (command->type == BFD_MACH_O_LC_UNIXTHREAD));
1058 BFD_ASSERT (bfd_mach_o_valid (abfd));
1059 mdata = abfd->tdata.mach_o_data;
1063 while (offset != command->len)
1065 if (offset >= command->len)
1068 bfd_seek (abfd, command->offset + offset, SEEK_SET);
1070 if (bfd_bread ((PTR) buf, 8, abfd) != 8)
1073 offset += 8 + bfd_h_get_32 (abfd, buf + 4) * 4;
1077 cmd->flavours = bfd_alloc (abfd, nflavours * sizeof (bfd_mach_o_thread_flavour));
1078 if (cmd->flavours == NULL)
1080 cmd->nflavours = nflavours;
1084 while (offset != command->len)
1086 if (offset >= command->len)
1089 if (nflavours >= cmd->nflavours)
1092 bfd_seek (abfd, command->offset + offset, SEEK_SET);
1094 if (bfd_bread ((PTR) buf, 8, abfd) != 8)
1097 cmd->flavours[nflavours].flavour = bfd_h_get_32 (abfd, buf);
1098 cmd->flavours[nflavours].offset = command->offset + offset + 8;
1099 cmd->flavours[nflavours].size = bfd_h_get_32 (abfd, buf + 4) * 4;
1100 offset += cmd->flavours[nflavours].size + 8;
1104 for (i = 0; i < nflavours; i++)
1107 unsigned int snamelen;
1109 const char *flavourstr;
1110 const char *prefix = "LC_THREAD";
1113 switch (mdata->header.cputype)
1115 case BFD_MACH_O_CPU_TYPE_POWERPC:
1116 flavourstr = bfd_mach_o_ppc_flavour_string (cmd->flavours[i].flavour);
1118 case BFD_MACH_O_CPU_TYPE_I386:
1119 flavourstr = bfd_mach_o_i386_flavour_string (cmd->flavours[i].flavour);
1122 flavourstr = "UNKNOWN_ARCHITECTURE";
1126 snamelen = strlen (prefix) + 1 + 20 + 1 + strlen (flavourstr) + 1;
1127 sname = bfd_alloc (abfd, snamelen);
1133 sprintf (sname, "%s.%s.%u", prefix, flavourstr, j);
1134 if (bfd_get_section_by_name (abfd, sname) == NULL)
1139 bfdsec = bfd_make_section_with_flags (abfd, sname, SEC_HAS_CONTENTS);
1143 bfdsec->size = cmd->flavours[i].size;
1144 bfdsec->filepos = cmd->flavours[i].offset;
1145 bfdsec->alignment_power = 0x0;
1147 cmd->section = bfdsec;
1154 bfd_mach_o_scan_read_dysymtab (bfd *abfd, bfd_mach_o_load_command *command)
1156 bfd_mach_o_dysymtab_command *seg = &command->command.dysymtab;
1157 unsigned char buf[72];
1159 BFD_ASSERT (command->type == BFD_MACH_O_LC_DYSYMTAB);
1161 bfd_seek (abfd, command->offset + 8, SEEK_SET);
1162 if (bfd_bread ((PTR) buf, 72, abfd) != 72)
1165 seg->ilocalsym = bfd_h_get_32 (abfd, buf + 0);
1166 seg->nlocalsym = bfd_h_get_32 (abfd, buf + 4);
1167 seg->iextdefsym = bfd_h_get_32 (abfd, buf + 8);
1168 seg->nextdefsym = bfd_h_get_32 (abfd, buf + 12);
1169 seg->iundefsym = bfd_h_get_32 (abfd, buf + 16);
1170 seg->nundefsym = bfd_h_get_32 (abfd, buf + 20);
1171 seg->tocoff = bfd_h_get_32 (abfd, buf + 24);
1172 seg->ntoc = bfd_h_get_32 (abfd, buf + 28);
1173 seg->modtaboff = bfd_h_get_32 (abfd, buf + 32);
1174 seg->nmodtab = bfd_h_get_32 (abfd, buf + 36);
1175 seg->extrefsymoff = bfd_h_get_32 (abfd, buf + 40);
1176 seg->nextrefsyms = bfd_h_get_32 (abfd, buf + 44);
1177 seg->indirectsymoff = bfd_h_get_32 (abfd, buf + 48);
1178 seg->nindirectsyms = bfd_h_get_32 (abfd, buf + 52);
1179 seg->extreloff = bfd_h_get_32 (abfd, buf + 56);
1180 seg->nextrel = bfd_h_get_32 (abfd, buf + 60);
1181 seg->locreloff = bfd_h_get_32 (abfd, buf + 64);
1182 seg->nlocrel = bfd_h_get_32 (abfd, buf + 68);
1188 bfd_mach_o_scan_read_symtab (bfd *abfd, bfd_mach_o_load_command *command)
1190 bfd_mach_o_symtab_command *seg = &command->command.symtab;
1191 unsigned char buf[16];
1194 const char *prefix = "LC_SYMTAB.stabs";
1196 BFD_ASSERT (command->type == BFD_MACH_O_LC_SYMTAB);
1198 bfd_seek (abfd, command->offset + 8, SEEK_SET);
1199 if (bfd_bread ((PTR) buf, 16, abfd) != 16)
1202 seg->symoff = bfd_h_get_32 (abfd, buf);
1203 seg->nsyms = bfd_h_get_32 (abfd, buf + 4);
1204 seg->stroff = bfd_h_get_32 (abfd, buf + 8);
1205 seg->strsize = bfd_h_get_32 (abfd, buf + 12);
1206 seg->symbols = NULL;
1209 sname = bfd_alloc (abfd, strlen (prefix) + 1);
1212 strcpy (sname, prefix);
1214 bfdsec = bfd_make_section_anyway_with_flags (abfd, sname, SEC_HAS_CONTENTS);
1220 bfdsec->size = seg->nsyms * 12;
1221 bfdsec->filepos = seg->symoff;
1222 bfdsec->alignment_power = 0;
1224 seg->stabs_segment = bfdsec;
1226 prefix = "LC_SYMTAB.stabstr";
1227 sname = bfd_alloc (abfd, strlen (prefix) + 1);
1230 strcpy (sname, prefix);
1232 bfdsec = bfd_make_section_anyway_with_flags (abfd, sname, SEC_HAS_CONTENTS);
1238 bfdsec->size = seg->strsize;
1239 bfdsec->filepos = seg->stroff;
1240 bfdsec->alignment_power = 0;
1242 seg->stabstr_segment = bfdsec;
1248 bfd_mach_o_scan_read_segment (bfd *abfd, bfd_mach_o_load_command *command)
1250 unsigned char buf[48];
1251 bfd_mach_o_segment_command *seg = &command->command.segment;
1255 const char *prefix = "LC_SEGMENT";
1256 unsigned int snamelen;
1259 BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT);
1261 bfd_seek (abfd, command->offset + 8, SEEK_SET);
1262 if (bfd_bread ((PTR) buf, 48, abfd) != 48)
1265 memcpy (seg->segname, buf, 16);
1266 seg->vmaddr = bfd_h_get_32 (abfd, buf + 16);
1267 seg->vmsize = bfd_h_get_32 (abfd, buf + 20);
1268 seg->fileoff = bfd_h_get_32 (abfd, buf + 24);
1269 seg->filesize = bfd_h_get_32 (abfd, buf + 28);
1270 /* seg->maxprot = bfd_h_get_32 (abfd, buf + 32); */
1271 /* seg->initprot = bfd_h_get_32 (abfd, buf + 36); */
1272 seg->nsects = bfd_h_get_32 (abfd, buf + 40);
1273 seg->flags = bfd_h_get_32 (abfd, buf + 44);
1275 snamelen = strlen (prefix) + 1 + strlen (seg->segname) + 1;
1276 sname = bfd_alloc (abfd, snamelen);
1279 sprintf (sname, "%s.%s", prefix, seg->segname);
1281 flags = SEC_HAS_CONTENTS | SEC_LOAD | SEC_ALLOC | SEC_CODE;
1282 bfdsec = bfd_make_section_anyway_with_flags (abfd, sname, flags);
1286 bfdsec->vma = seg->vmaddr;
1287 bfdsec->lma = seg->vmaddr;
1288 bfdsec->size = seg->filesize;
1289 bfdsec->filepos = seg->fileoff;
1290 bfdsec->alignment_power = 0x0;
1292 seg->segment = bfdsec;
1294 if (seg->nsects != 0)
1296 seg->sections = bfd_alloc (abfd, seg->nsects * sizeof (bfd_mach_o_section));
1297 if (seg->sections == NULL)
1300 for (i = 0; i < seg->nsects; i++)
1302 bfd_vma segoff = command->offset + 48 + 8 + (i * 68);
1304 if (bfd_mach_o_scan_read_section (abfd, &seg->sections[i],
1314 bfd_mach_o_scan_read_command (bfd *abfd, bfd_mach_o_load_command *command)
1316 unsigned char buf[8];
1318 bfd_seek (abfd, command->offset, SEEK_SET);
1319 if (bfd_bread ((PTR) buf, 8, abfd) != 8)
1322 command->type = (bfd_h_get_32 (abfd, buf) & ~BFD_MACH_O_LC_REQ_DYLD);
1323 command->type_required = (bfd_h_get_32 (abfd, buf) & BFD_MACH_O_LC_REQ_DYLD
1325 command->len = bfd_h_get_32 (abfd, buf + 4);
1327 switch (command->type)
1329 case BFD_MACH_O_LC_SEGMENT:
1330 if (bfd_mach_o_scan_read_segment (abfd, command) != 0)
1333 case BFD_MACH_O_LC_SYMTAB:
1334 if (bfd_mach_o_scan_read_symtab (abfd, command) != 0)
1337 case BFD_MACH_O_LC_SYMSEG:
1339 case BFD_MACH_O_LC_THREAD:
1340 case BFD_MACH_O_LC_UNIXTHREAD:
1341 if (bfd_mach_o_scan_read_thread (abfd, command) != 0)
1344 case BFD_MACH_O_LC_LOAD_DYLINKER:
1345 case BFD_MACH_O_LC_ID_DYLINKER:
1346 if (bfd_mach_o_scan_read_dylinker (abfd, command) != 0)
1349 case BFD_MACH_O_LC_LOAD_DYLIB:
1350 case BFD_MACH_O_LC_ID_DYLIB:
1351 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB:
1352 if (bfd_mach_o_scan_read_dylib (abfd, command) != 0)
1355 case BFD_MACH_O_LC_PREBOUND_DYLIB:
1356 if (bfd_mach_o_scan_read_prebound_dylib (abfd, command) != 0)
1359 case BFD_MACH_O_LC_LOADFVMLIB:
1360 case BFD_MACH_O_LC_IDFVMLIB:
1361 case BFD_MACH_O_LC_IDENT:
1362 case BFD_MACH_O_LC_FVMFILE:
1363 case BFD_MACH_O_LC_PREPAGE:
1364 case BFD_MACH_O_LC_ROUTINES:
1365 case BFD_MACH_O_LC_SUB_FRAMEWORK:
1367 case BFD_MACH_O_LC_DYSYMTAB:
1368 if (bfd_mach_o_scan_read_dysymtab (abfd, command) != 0)
1371 case BFD_MACH_O_LC_SUB_UMBRELLA:
1372 case BFD_MACH_O_LC_SUB_CLIENT:
1373 case BFD_MACH_O_LC_SUB_LIBRARY:
1374 case BFD_MACH_O_LC_TWOLEVEL_HINTS:
1375 case BFD_MACH_O_LC_PREBIND_CKSUM:
1378 fprintf (stderr, "unable to read unknown load command 0x%lx\n",
1379 (unsigned long) command->type);
1387 bfd_mach_o_flatten_sections (bfd *abfd)
1389 bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
1395 for (i = 0; i < mdata->header.ncmds; i++)
1397 if (mdata->commands[i].type == BFD_MACH_O_LC_SEGMENT)
1399 bfd_mach_o_segment_command *seg;
1401 seg = &mdata->commands[i].command.segment;
1402 mdata->nsects += seg->nsects;
1406 mdata->sections = bfd_alloc (abfd,
1407 mdata->nsects * sizeof (bfd_mach_o_section *));
1410 for (i = 0; i < mdata->header.ncmds; i++)
1412 if (mdata->commands[i].type == BFD_MACH_O_LC_SEGMENT)
1414 bfd_mach_o_segment_command *seg;
1416 seg = &mdata->commands[i].command.segment;
1417 BFD_ASSERT (csect + seg->nsects <= mdata->nsects);
1419 for (j = 0; j < seg->nsects; j++)
1420 mdata->sections[csect++] = &seg->sections[j];
1426 bfd_mach_o_scan_start_address (bfd *abfd)
1428 bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
1429 bfd_mach_o_thread_command *cmd = NULL;
1432 for (i = 0; i < mdata->header.ncmds; i++)
1434 if ((mdata->commands[i].type == BFD_MACH_O_LC_THREAD) ||
1435 (mdata->commands[i].type == BFD_MACH_O_LC_UNIXTHREAD))
1438 cmd = &mdata->commands[i].command.thread;
1447 for (i = 0; i < cmd->nflavours; i++)
1449 if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_I386)
1450 && (cmd->flavours[i].flavour
1451 == (unsigned long) BFD_MACH_O_i386_THREAD_STATE))
1453 unsigned char buf[4];
1455 bfd_seek (abfd, cmd->flavours[i].offset + 40, SEEK_SET);
1457 if (bfd_bread (buf, 4, abfd) != 4)
1460 abfd->start_address = bfd_h_get_32 (abfd, buf);
1462 else if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_POWERPC)
1463 && (cmd->flavours[i].flavour == BFD_MACH_O_PPC_THREAD_STATE))
1465 unsigned char buf[4];
1467 bfd_seek (abfd, cmd->flavours[i].offset + 0, SEEK_SET);
1469 if (bfd_bread (buf, 4, abfd) != 4)
1472 abfd->start_address = bfd_h_get_32 (abfd, buf);
1480 bfd_mach_o_scan (bfd *abfd,
1481 bfd_mach_o_header *header,
1482 bfd_mach_o_data_struct *mdata)
1485 enum bfd_architecture cputype;
1486 unsigned long cpusubtype;
1488 mdata->header = *header;
1489 mdata->symbols = NULL;
1491 abfd->flags = (abfd->xvec->object_flags
1492 | (abfd->flags & (BFD_IN_MEMORY | BFD_IO_FUNCS)));
1493 abfd->tdata.mach_o_data = mdata;
1495 bfd_mach_o_convert_architecture (header->cputype, header->cpusubtype,
1496 &cputype, &cpusubtype);
1497 if (cputype == bfd_arch_unknown)
1499 fprintf (stderr, "bfd_mach_o_scan: unknown architecture 0x%lx/0x%lx\n",
1500 header->cputype, header->cpusubtype);
1504 bfd_set_arch_mach (abfd, cputype, cpusubtype);
1506 if (header->ncmds != 0)
1508 mdata->commands = bfd_alloc (abfd, header->ncmds * sizeof (bfd_mach_o_load_command));
1509 if (mdata->commands == NULL)
1512 for (i = 0; i < header->ncmds; i++)
1514 bfd_mach_o_load_command *cur = &mdata->commands[i];
1520 bfd_mach_o_load_command *prev = &mdata->commands[i - 1];
1521 cur->offset = prev->offset + prev->len;
1524 if (bfd_mach_o_scan_read_command (abfd, cur) < 0)
1529 if (bfd_mach_o_scan_start_address (abfd) < 0)
1532 bfd_mach_o_flatten_sections (abfd);
1537 bfd_mach_o_mkobject (bfd *abfd)
1539 bfd_mach_o_data_struct *mdata = NULL;
1541 mdata = bfd_alloc (abfd, sizeof (bfd_mach_o_data_struct));
1544 abfd->tdata.mach_o_data = mdata;
1546 mdata->header.magic = 0;
1547 mdata->header.cputype = 0;
1548 mdata->header.cpusubtype = 0;
1549 mdata->header.filetype = 0;
1550 mdata->header.ncmds = 0;
1551 mdata->header.sizeofcmds = 0;
1552 mdata->header.flags = 0;
1553 mdata->header.byteorder = BFD_ENDIAN_UNKNOWN;
1554 mdata->commands = NULL;
1555 mdata->nsymbols = 0;
1556 mdata->symbols = NULL;
1558 mdata->sections = NULL;
1565 bfd_mach_o_object_p (bfd *abfd)
1567 struct bfd_preserve preserve;
1568 bfd_mach_o_header header;
1570 preserve.marker = NULL;
1571 if (bfd_mach_o_read_header (abfd, &header) != 0)
1574 if (! (header.byteorder == BFD_ENDIAN_BIG
1575 || header.byteorder == BFD_ENDIAN_LITTLE))
1577 fprintf (stderr, "unknown header byte-order value 0x%lx\n",
1578 (long) header.byteorder);
1582 if (! ((header.byteorder == BFD_ENDIAN_BIG
1583 && abfd->xvec->byteorder == BFD_ENDIAN_BIG
1584 && abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
1585 || (header.byteorder == BFD_ENDIAN_LITTLE
1586 && abfd->xvec->byteorder == BFD_ENDIAN_LITTLE
1587 && abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)))
1590 preserve.marker = bfd_zalloc (abfd, sizeof (bfd_mach_o_data_struct));
1591 if (preserve.marker == NULL
1592 || !bfd_preserve_save (abfd, &preserve))
1595 if (bfd_mach_o_scan (abfd, &header,
1596 (bfd_mach_o_data_struct *) preserve.marker) != 0)
1599 bfd_preserve_finish (abfd, &preserve);
1603 bfd_set_error (bfd_error_wrong_format);
1606 if (preserve.marker != NULL)
1607 bfd_preserve_restore (abfd, &preserve);
1612 bfd_mach_o_core_p (bfd *abfd)
1614 struct bfd_preserve preserve;
1615 bfd_mach_o_header header;
1617 preserve.marker = NULL;
1618 if (bfd_mach_o_read_header (abfd, &header) != 0)
1621 if (! (header.byteorder == BFD_ENDIAN_BIG
1622 || header.byteorder == BFD_ENDIAN_LITTLE))
1624 fprintf (stderr, "unknown header byte-order value 0x%lx\n",
1625 (long) header.byteorder);
1629 if (! ((header.byteorder == BFD_ENDIAN_BIG
1630 && abfd->xvec->byteorder == BFD_ENDIAN_BIG
1631 && abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
1632 || (header.byteorder == BFD_ENDIAN_LITTLE
1633 && abfd->xvec->byteorder == BFD_ENDIAN_LITTLE
1634 && abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)))
1637 if (header.filetype != BFD_MACH_O_MH_CORE)
1640 preserve.marker = bfd_zalloc (abfd, sizeof (bfd_mach_o_data_struct));
1641 if (preserve.marker == NULL
1642 || !bfd_preserve_save (abfd, &preserve))
1645 if (bfd_mach_o_scan (abfd, &header,
1646 (bfd_mach_o_data_struct *) preserve.marker) != 0)
1649 bfd_preserve_finish (abfd, &preserve);
1653 bfd_set_error (bfd_error_wrong_format);
1656 if (preserve.marker != NULL)
1657 bfd_preserve_restore (abfd, &preserve);
1661 typedef struct mach_o_fat_archentry
1663 unsigned long cputype;
1664 unsigned long cpusubtype;
1665 unsigned long offset;
1667 unsigned long align;
1669 } mach_o_fat_archentry;
1671 typedef struct mach_o_fat_data_struct
1673 unsigned long magic;
1674 unsigned long nfat_arch;
1675 mach_o_fat_archentry *archentries;
1676 } mach_o_fat_data_struct;
1679 bfd_mach_o_archive_p (bfd *abfd)
1681 mach_o_fat_data_struct *adata = NULL;
1682 unsigned char buf[20];
1685 bfd_seek (abfd, 0, SEEK_SET);
1686 if (bfd_bread ((PTR) buf, 8, abfd) != 8)
1689 adata = bfd_alloc (abfd, sizeof (mach_o_fat_data_struct));
1693 adata->magic = bfd_getb32 (buf);
1694 adata->nfat_arch = bfd_getb32 (buf + 4);
1695 if (adata->magic != 0xcafebabe)
1698 adata->archentries =
1699 bfd_alloc (abfd, adata->nfat_arch * sizeof (mach_o_fat_archentry));
1700 if (adata->archentries == NULL)
1703 for (i = 0; i < adata->nfat_arch; i++)
1705 bfd_seek (abfd, 8 + 20 * i, SEEK_SET);
1707 if (bfd_bread ((PTR) buf, 20, abfd) != 20)
1709 adata->archentries[i].cputype = bfd_getb32 (buf);
1710 adata->archentries[i].cpusubtype = bfd_getb32 (buf + 4);
1711 adata->archentries[i].offset = bfd_getb32 (buf + 8);
1712 adata->archentries[i].size = bfd_getb32 (buf + 12);
1713 adata->archentries[i].align = bfd_getb32 (buf + 16);
1714 adata->archentries[i].abfd = NULL;
1717 abfd->tdata.mach_o_fat_data = adata;
1722 bfd_release (abfd, adata);
1723 bfd_set_error (bfd_error_wrong_format);
1728 bfd_mach_o_openr_next_archived_file (bfd *archive, bfd *prev)
1730 mach_o_fat_data_struct *adata;
1731 mach_o_fat_archentry *entry = NULL;
1734 adata = (mach_o_fat_data_struct *) archive->tdata.mach_o_fat_data;
1735 BFD_ASSERT (adata != NULL);
1737 /* Find index of previous entry. */
1739 i = 0; /* Start at first one. */
1742 for (i = 0; i < adata->nfat_arch; i++)
1744 if (adata->archentries[i].abfd == prev)
1748 if (i == adata->nfat_arch)
1751 bfd_set_error (bfd_error_bad_value);
1754 i++; /* Get next entry. */
1757 if (i >= adata->nfat_arch)
1759 bfd_set_error (bfd_error_no_more_archived_files);
1763 entry = &adata->archentries[i];
1764 if (entry->abfd == NULL)
1766 bfd *nbfd = _bfd_new_bfd_contained_in (archive);
1772 nbfd->origin = entry->offset;
1773 s = bfd_malloc (strlen (archive->filename) + 1);
1776 strcpy (s, archive->filename);
1778 nbfd->iostream = NULL;
1786 bfd_mach_o_lookup_section (bfd *abfd,
1788 bfd_mach_o_load_command **mcommand,
1789 bfd_mach_o_section **msection)
1791 struct mach_o_data_struct *md = abfd->tdata.mach_o_data;
1792 unsigned int i, j, num;
1794 bfd_mach_o_load_command *ncmd = NULL;
1795 bfd_mach_o_section *nsect = NULL;
1797 BFD_ASSERT (mcommand != NULL);
1798 BFD_ASSERT (msection != NULL);
1801 for (i = 0; i < md->header.ncmds; i++)
1803 struct bfd_mach_o_load_command *cmd = &md->commands[i];
1804 struct bfd_mach_o_segment_command *seg = NULL;
1806 if (cmd->type != BFD_MACH_O_LC_SEGMENT)
1808 seg = &cmd->command.segment;
1810 if (seg->segment == section)
1817 for (j = 0; j < seg->nsects; j++)
1819 struct bfd_mach_o_section *sect = &seg->sections[j];
1821 if (sect->bfdsection == section)
1836 bfd_mach_o_lookup_command (bfd *abfd,
1837 bfd_mach_o_load_command_type type,
1838 bfd_mach_o_load_command **mcommand)
1840 struct mach_o_data_struct *md = NULL;
1841 bfd_mach_o_load_command *ncmd = NULL;
1842 unsigned int i, num;
1844 md = abfd->tdata.mach_o_data;
1846 BFD_ASSERT (md != NULL);
1847 BFD_ASSERT (mcommand != NULL);
1850 for (i = 0; i < md->header.ncmds; i++)
1852 struct bfd_mach_o_load_command *cmd = &md->commands[i];
1854 if (cmd->type != type)
1867 bfd_mach_o_stack_addr (enum bfd_mach_o_cpu_type type)
1871 case BFD_MACH_O_CPU_TYPE_MC680x0:
1873 case BFD_MACH_O_CPU_TYPE_MC88000:
1875 case BFD_MACH_O_CPU_TYPE_POWERPC:
1877 case BFD_MACH_O_CPU_TYPE_I386:
1879 case BFD_MACH_O_CPU_TYPE_SPARC:
1881 case BFD_MACH_O_CPU_TYPE_I860:
1883 case BFD_MACH_O_CPU_TYPE_HPPA:
1884 return 0xc0000000 - 0x04000000;
1891 bfd_mach_o_core_fetch_environment (bfd *abfd,
1892 unsigned char **rbuf,
1895 bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
1896 unsigned long stackaddr = bfd_mach_o_stack_addr (mdata->header.cputype);
1899 for (i = 0; i < mdata->header.ncmds; i++)
1901 bfd_mach_o_load_command *cur = &mdata->commands[i];
1902 bfd_mach_o_segment_command *seg = NULL;
1904 if (cur->type != BFD_MACH_O_LC_SEGMENT)
1907 seg = &cur->command.segment;
1909 if ((seg->vmaddr + seg->vmsize) == stackaddr)
1911 unsigned long start = seg->fileoff;
1912 unsigned long end = seg->fileoff + seg->filesize;
1913 unsigned char *buf = bfd_malloc (1024);
1914 unsigned long size = 1024;
1918 bfd_size_type nread = 0;
1919 unsigned long offset;
1920 int found_nonnull = 0;
1922 if (size > (end - start))
1923 size = (end - start);
1925 buf = bfd_realloc (buf, size);
1927 bfd_seek (abfd, end - size, SEEK_SET);
1928 nread = bfd_bread (buf, size, abfd);
1933 for (offset = 4; offset <= size; offset += 4)
1937 val = *((unsigned long *) (buf + size - offset));
1938 if (! found_nonnull)
1943 else if (val == 0x0)
1945 unsigned long bottom;
1948 bottom = seg->fileoff + seg->filesize - offset;
1949 top = seg->fileoff + seg->filesize - 4;
1950 *rbuf = bfd_malloc (top - bottom);
1951 *rlen = top - bottom;
1953 memcpy (*rbuf, buf + size - *rlen, *rlen);
1958 if (size == (end - start))
1970 bfd_mach_o_core_file_failing_command (bfd *abfd)
1972 unsigned char *buf = NULL;
1973 unsigned int len = 0;
1976 ret = bfd_mach_o_core_fetch_environment (abfd, &buf, &len);
1980 return (char *) buf;
1984 bfd_mach_o_core_file_failing_signal (bfd *abfd ATTRIBUTE_UNUSED)
1989 #define TARGET_NAME mach_o_be_vec
1990 #define TARGET_STRING "mach-o-be"
1991 #define TARGET_BIG_ENDIAN 1
1992 #define TARGET_ARCHIVE 0
1994 #include "mach-o-target.c"
1997 #undef TARGET_STRING
1998 #undef TARGET_BIG_ENDIAN
1999 #undef TARGET_ARCHIVE
2001 #define TARGET_NAME mach_o_le_vec
2002 #define TARGET_STRING "mach-o-le"
2003 #define TARGET_BIG_ENDIAN 0
2004 #define TARGET_ARCHIVE 0
2006 #include "mach-o-target.c"
2009 #undef TARGET_STRING
2010 #undef TARGET_BIG_ENDIAN
2011 #undef TARGET_ARCHIVE
2013 #define TARGET_NAME mach_o_fat_vec
2014 #define TARGET_STRING "mach-o-fat"
2015 #define TARGET_BIG_ENDIAN 1
2016 #define TARGET_ARCHIVE 1
2018 #include "mach-o-target.c"
2021 #undef TARGET_STRING
2022 #undef TARGET_BIG_ENDIAN
2023 #undef TARGET_ARCHIVE