1 /* Mach-O support for BFD.
2 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4 Free Software Foundation, Inc.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
27 #include "libiberty.h"
28 #include "aout/stab_gnu.h"
29 #include "mach-o/reloc.h"
30 #include "mach-o/external.h"
33 #define bfd_mach_o_object_p bfd_mach_o_gen_object_p
34 #define bfd_mach_o_core_p bfd_mach_o_gen_core_p
35 #define bfd_mach_o_mkobject bfd_mach_o_gen_mkobject
37 #define FILE_ALIGN(off, algn) \
38 (((off) + ((file_ptr) 1 << (algn)) - 1) & ((file_ptr) -1 << (algn)))
40 static int bfd_mach_o_read_symtab_symbols (bfd *);
43 bfd_mach_o_version (bfd *abfd)
45 bfd_mach_o_data_struct *mdata = NULL;
47 BFD_ASSERT (bfd_mach_o_valid (abfd));
48 mdata = bfd_mach_o_get_data (abfd);
50 return mdata->header.version;
54 bfd_mach_o_valid (bfd *abfd)
56 if (abfd == NULL || abfd->xvec == NULL)
59 if (abfd->xvec->flavour != bfd_target_mach_o_flavour)
62 if (bfd_mach_o_get_data (abfd) == NULL)
67 static INLINE bfd_boolean
68 mach_o_wide_p (bfd_mach_o_header *header)
70 switch (header->version)
82 static INLINE bfd_boolean
83 bfd_mach_o_wide_p (bfd *abfd)
85 return mach_o_wide_p (&bfd_mach_o_get_data (abfd)->header);
88 /* Tables to translate well known Mach-O segment/section names to bfd
89 names. Use of canonical names (such as .text or .debug_frame) is required
92 struct mach_o_section_name_xlat
95 const char *mach_o_name;
99 static const struct mach_o_section_name_xlat dwarf_section_names_xlat[] =
101 { ".debug_frame", "__debug_frame", SEC_DEBUGGING },
102 { ".debug_info", "__debug_info", SEC_DEBUGGING },
103 { ".debug_abbrev", "__debug_abbrev", SEC_DEBUGGING },
104 { ".debug_aranges", "__debug_aranges", SEC_DEBUGGING },
105 { ".debug_macinfo", "__debug_macinfo", SEC_DEBUGGING },
106 { ".debug_line", "__debug_line", SEC_DEBUGGING },
107 { ".debug_loc", "__debug_loc", SEC_DEBUGGING },
108 { ".debug_pubnames", "__debug_pubnames", SEC_DEBUGGING },
109 { ".debug_pubtypes", "__debug_pubtypes", SEC_DEBUGGING },
110 { ".debug_str", "__debug_str", SEC_DEBUGGING },
111 { ".debug_ranges", "__debug_ranges", SEC_DEBUGGING },
115 static const struct mach_o_section_name_xlat text_section_names_xlat[] =
117 { ".text", "__text", SEC_CODE | SEC_LOAD },
118 { ".const", "__const", SEC_READONLY | SEC_DATA | SEC_LOAD },
119 { ".cstring", "__cstring", SEC_READONLY | SEC_DATA | SEC_LOAD },
120 { ".eh_frame", "__eh_frame", SEC_READONLY | SEC_LOAD },
124 static const struct mach_o_section_name_xlat data_section_names_xlat[] =
126 { ".data", "__data", SEC_DATA | SEC_LOAD },
127 { ".const_data", "__const", SEC_DATA | SEC_LOAD },
128 { ".dyld", "__dyld", SEC_DATA | SEC_LOAD },
129 { ".lazy_symbol_ptr", "__la_symbol_ptr", SEC_DATA | SEC_LOAD },
130 { ".non_lazy_symbol_ptr", "__nl_symbol_ptr", SEC_DATA | SEC_LOAD },
131 { ".bss", "__bss", SEC_NO_FLAGS },
135 struct mach_o_segment_name_xlat
138 const struct mach_o_section_name_xlat *sections;
141 static const struct mach_o_segment_name_xlat segsec_names_xlat[] =
143 { "__DWARF", dwarf_section_names_xlat },
144 { "__TEXT", text_section_names_xlat },
145 { "__DATA", data_section_names_xlat },
149 /* Mach-O to bfd names. */
152 bfd_mach_o_normalize_section_name (const char *segname, const char *sectname,
153 const char **name, flagword *flags)
155 const struct mach_o_segment_name_xlat *seg;
158 *flags = SEC_NO_FLAGS;
160 for (seg = segsec_names_xlat; seg->segname; seg++)
162 if (strcmp (seg->segname, segname) == 0)
164 const struct mach_o_section_name_xlat *sec;
166 for (sec = seg->sections; sec->mach_o_name; sec++)
168 if (strcmp (sec->mach_o_name, sectname) == 0)
170 *name = sec->bfd_name;
181 bfd_mach_o_convert_section_name_to_bfd (bfd *abfd, bfd_mach_o_section *section,
182 const char **name, flagword *flags)
186 const char *pfx = "";
188 /* First search for a canonical name. */
189 bfd_mach_o_normalize_section_name (section->segname, section->sectname,
192 /* Return now if found. */
196 len = strlen (section->segname) + 1
197 + strlen (section->sectname) + 1;
199 /* Put "LC_SEGMENT." prefix if the segment name is weird (ie doesn't start
200 with an underscore. */
201 if (section->segname[0] != '_')
203 static const char seg_pfx[] = "LC_SEGMENT.";
206 len += sizeof (seg_pfx) - 1;
209 res = bfd_alloc (abfd, len);
212 snprintf (res, len, "%s%s.%s", pfx, section->segname, section->sectname);
214 *flags = SEC_NO_FLAGS;
217 /* Convert a bfd section name to a Mach-O segment + section name. */
220 bfd_mach_o_convert_section_name_to_mach_o (bfd *abfd ATTRIBUTE_UNUSED,
222 bfd_mach_o_section *section)
224 const struct mach_o_segment_name_xlat *seg;
225 const char *name = bfd_get_section_name (abfd, sect);
231 /* List of well known names. They all start with a dot. */
233 for (seg = segsec_names_xlat; seg->segname; seg++)
235 const struct mach_o_section_name_xlat *sec;
237 for (sec = seg->sections; sec->mach_o_name; sec++)
239 if (strcmp (sec->bfd_name, name) == 0)
241 strcpy (section->segname, seg->segname);
242 strcpy (section->sectname, sec->mach_o_name);
248 /* Strip LC_SEGMENT. prefix. */
249 if (strncmp (name, "LC_SEGMENT.", 11) == 0)
253 dot = strchr (name, '.');
256 /* Try to split name into segment and section names. */
257 if (dot && dot != name)
260 seclen = len - (dot + 1 - name);
262 if (seglen < 16 && seclen < 16)
264 memcpy (section->segname, name, seglen);
265 section->segname[seglen] = 0;
266 memcpy (section->sectname, dot + 1, seclen);
267 section->sectname[seclen] = 0;
274 memcpy (section->segname, name, len);
275 section->segname[len] = 0;
276 memcpy (section->sectname, name, len);
277 section->sectname[len] = 0;
280 /* Return the size of an entry for section SEC.
281 Must be called only for symbol pointer section and symbol stubs
285 bfd_mach_o_section_get_entry_size (bfd *abfd, bfd_mach_o_section *sec)
287 switch (sec->flags & BFD_MACH_O_SECTION_TYPE_MASK)
289 case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS:
290 case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS:
291 return bfd_mach_o_wide_p (abfd) ? 8 : 4;
292 case BFD_MACH_O_S_SYMBOL_STUBS:
293 return sec->reserved2;
300 /* Return the number of indirect symbols for a section.
301 Must be called only for symbol pointer section and symbol stubs
305 bfd_mach_o_section_get_nbr_indirect (bfd *abfd, bfd_mach_o_section *sec)
309 elsz = bfd_mach_o_section_get_entry_size (abfd, sec);
313 return sec->size / elsz;
317 /* Copy any private info we understand from the input symbol
318 to the output symbol. */
321 bfd_mach_o_bfd_copy_private_symbol_data (bfd *ibfd ATTRIBUTE_UNUSED,
322 asymbol *isymbol ATTRIBUTE_UNUSED,
323 bfd *obfd ATTRIBUTE_UNUSED,
324 asymbol *osymbol ATTRIBUTE_UNUSED)
329 /* Copy any private info we understand from the input section
330 to the output section. */
333 bfd_mach_o_bfd_copy_private_section_data (bfd *ibfd ATTRIBUTE_UNUSED,
334 asection *isection ATTRIBUTE_UNUSED,
335 bfd *obfd ATTRIBUTE_UNUSED,
336 asection *osection ATTRIBUTE_UNUSED)
341 /* Copy any private info we understand from the input bfd
342 to the output bfd. */
345 bfd_mach_o_bfd_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
347 if (bfd_get_flavour (ibfd) != bfd_target_mach_o_flavour
348 || bfd_get_flavour (obfd) != bfd_target_mach_o_flavour)
351 BFD_ASSERT (bfd_mach_o_valid (ibfd));
352 BFD_ASSERT (bfd_mach_o_valid (obfd));
354 /* FIXME: copy commands. */
359 /* Count the total number of symbols. */
362 bfd_mach_o_count_symbols (bfd *abfd)
364 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
366 if (mdata->symtab == NULL)
368 return mdata->symtab->nsyms;
372 bfd_mach_o_get_symtab_upper_bound (bfd *abfd)
374 long nsyms = bfd_mach_o_count_symbols (abfd);
376 return ((nsyms + 1) * sizeof (asymbol *));
380 bfd_mach_o_canonicalize_symtab (bfd *abfd, asymbol **alocation)
382 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
383 long nsyms = bfd_mach_o_count_symbols (abfd);
384 bfd_mach_o_symtab_command *sym = mdata->symtab;
392 /* Do not try to read symbols if there are none. */
397 if (bfd_mach_o_read_symtab_symbols (abfd) != 0)
399 (*_bfd_error_handler) (_("bfd_mach_o_canonicalize_symtab: unable to load symbols"));
403 BFD_ASSERT (sym->symbols != NULL);
405 for (j = 0; j < sym->nsyms; j++)
406 alocation[j] = &sym->symbols[j].symbol;
414 bfd_mach_o_get_synthetic_symtab (bfd *abfd,
415 long symcount ATTRIBUTE_UNUSED,
416 asymbol **syms ATTRIBUTE_UNUSED,
417 long dynsymcount ATTRIBUTE_UNUSED,
418 asymbol **dynsyms ATTRIBUTE_UNUSED,
421 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
422 bfd_mach_o_dysymtab_command *dysymtab = mdata->dysymtab;
423 bfd_mach_o_symtab_command *symtab = mdata->symtab;
425 unsigned long count, i, j, n;
432 if (dysymtab == NULL || symtab == NULL || symtab->symbols == NULL)
435 if (dysymtab->nindirectsyms == 0)
438 count = dysymtab->nindirectsyms;
439 size = count * sizeof (asymbol) + 1;
441 for (j = 0; j < count; j++)
443 unsigned int isym = dysymtab->indirect_syms[j];
445 if (isym < symtab->nsyms && symtab->symbols[isym].symbol.name)
446 size += strlen (symtab->symbols[isym].symbol.name) + sizeof ("$stub");
449 s = *ret = (asymbol *) bfd_malloc (size);
452 names = (char *) (s + count);
457 for (i = 0; i < mdata->nsects; i++)
459 bfd_mach_o_section *sec = mdata->sections[i];
460 unsigned int first, last;
464 switch (sec->flags & BFD_MACH_O_SECTION_TYPE_MASK)
466 case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS:
467 case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS:
468 case BFD_MACH_O_S_SYMBOL_STUBS:
469 first = sec->reserved1;
470 last = first + bfd_mach_o_section_get_nbr_indirect (abfd, sec);
472 entry_size = bfd_mach_o_section_get_entry_size (abfd, sec);
473 for (j = first; j < last; j++)
475 unsigned int isym = dysymtab->indirect_syms[j];
477 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
478 s->section = sec->bfdsection;
479 s->value = addr - sec->addr;
482 if (isym < symtab->nsyms
483 && symtab->symbols[isym].symbol.name)
485 const char *sym = symtab->symbols[isym].symbol.name;
490 memcpy (names, sym, len);
492 memcpy (names, "$stub", sizeof ("$stub"));
493 names += sizeof ("$stub");
512 bfd_mach_o_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
516 bfd_symbol_info (symbol, ret);
520 bfd_mach_o_print_symbol (bfd *abfd,
523 bfd_print_symbol_type how)
525 FILE *file = (FILE *) afile;
527 bfd_mach_o_asymbol *asym = (bfd_mach_o_asymbol *)symbol;
531 case bfd_print_symbol_name:
532 fprintf (file, "%s", symbol->name);
535 bfd_print_symbol_vandf (abfd, (void *) file, symbol);
536 if (asym->n_type & BFD_MACH_O_N_STAB)
537 name = bfd_get_stab_name (asym->n_type);
539 switch (asym->n_type & BFD_MACH_O_N_TYPE)
541 case BFD_MACH_O_N_UNDF:
544 case BFD_MACH_O_N_ABS:
547 case BFD_MACH_O_N_INDR:
550 case BFD_MACH_O_N_PBUD:
553 case BFD_MACH_O_N_SECT:
562 fprintf (file, " %02x %-6s %02x %04x",
563 asym->n_type, name, asym->n_sect, asym->n_desc);
564 if ((asym->n_type & BFD_MACH_O_N_STAB) == 0
565 && (asym->n_type & BFD_MACH_O_N_TYPE) == BFD_MACH_O_N_SECT)
566 fprintf (file, " %-5s", symbol->section->name);
567 fprintf (file, " %s", symbol->name);
572 bfd_mach_o_convert_architecture (bfd_mach_o_cpu_type mtype,
573 bfd_mach_o_cpu_subtype msubtype ATTRIBUTE_UNUSED,
574 enum bfd_architecture *type,
575 unsigned long *subtype)
577 *subtype = bfd_arch_unknown;
581 case BFD_MACH_O_CPU_TYPE_VAX: *type = bfd_arch_vax; break;
582 case BFD_MACH_O_CPU_TYPE_MC680x0: *type = bfd_arch_m68k; break;
583 case BFD_MACH_O_CPU_TYPE_I386:
584 *type = bfd_arch_i386;
585 *subtype = bfd_mach_i386_i386;
587 case BFD_MACH_O_CPU_TYPE_X86_64:
588 *type = bfd_arch_i386;
589 *subtype = bfd_mach_x86_64;
591 case BFD_MACH_O_CPU_TYPE_MIPS: *type = bfd_arch_mips; break;
592 case BFD_MACH_O_CPU_TYPE_MC98000: *type = bfd_arch_m98k; break;
593 case BFD_MACH_O_CPU_TYPE_HPPA: *type = bfd_arch_hppa; break;
594 case BFD_MACH_O_CPU_TYPE_ARM: *type = bfd_arch_arm; break;
595 case BFD_MACH_O_CPU_TYPE_MC88000: *type = bfd_arch_m88k; break;
596 case BFD_MACH_O_CPU_TYPE_SPARC:
597 *type = bfd_arch_sparc;
598 *subtype = bfd_mach_sparc;
600 case BFD_MACH_O_CPU_TYPE_I860: *type = bfd_arch_i860; break;
601 case BFD_MACH_O_CPU_TYPE_ALPHA: *type = bfd_arch_alpha; break;
602 case BFD_MACH_O_CPU_TYPE_POWERPC:
603 *type = bfd_arch_powerpc;
604 *subtype = bfd_mach_ppc;
606 case BFD_MACH_O_CPU_TYPE_POWERPC_64:
607 *type = bfd_arch_powerpc;
608 *subtype = bfd_mach_ppc64;
611 *type = bfd_arch_unknown;
617 bfd_mach_o_write_header (bfd *abfd, bfd_mach_o_header *header)
619 struct mach_o_header_external raw;
622 size = mach_o_wide_p (header) ?
623 BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE;
625 bfd_h_put_32 (abfd, header->magic, raw.magic);
626 bfd_h_put_32 (abfd, header->cputype, raw.cputype);
627 bfd_h_put_32 (abfd, header->cpusubtype, raw.cpusubtype);
628 bfd_h_put_32 (abfd, header->filetype, raw.filetype);
629 bfd_h_put_32 (abfd, header->ncmds, raw.ncmds);
630 bfd_h_put_32 (abfd, header->sizeofcmds, raw.sizeofcmds);
631 bfd_h_put_32 (abfd, header->flags, raw.flags);
633 if (mach_o_wide_p (header))
634 bfd_h_put_32 (abfd, header->reserved, raw.reserved);
636 if (bfd_seek (abfd, 0, SEEK_SET) != 0
637 || bfd_bwrite (&raw, size, abfd) != size)
644 bfd_mach_o_write_thread (bfd *abfd, bfd_mach_o_load_command *command)
646 bfd_mach_o_thread_command *cmd = &command->command.thread;
648 struct mach_o_thread_command_external raw;
651 BFD_ASSERT ((command->type == BFD_MACH_O_LC_THREAD)
652 || (command->type == BFD_MACH_O_LC_UNIXTHREAD));
655 for (i = 0; i < cmd->nflavours; i++)
657 BFD_ASSERT ((cmd->flavours[i].size % 4) == 0);
658 BFD_ASSERT (cmd->flavours[i].offset ==
659 (command->offset + offset + BFD_MACH_O_LC_SIZE));
661 bfd_h_put_32 (abfd, cmd->flavours[i].flavour, raw.flavour);
662 bfd_h_put_32 (abfd, (cmd->flavours[i].size / 4), raw.count);
664 if (bfd_seek (abfd, command->offset + offset, SEEK_SET) != 0
665 || bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
668 offset += cmd->flavours[i].size + sizeof (raw);
675 bfd_mach_o_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
678 return (asect->reloc_count + 1) * sizeof (arelent *);
682 bfd_mach_o_canonicalize_one_reloc (bfd *abfd,
683 struct mach_o_reloc_info_external *raw,
684 arelent *res, asymbol **syms)
686 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
687 bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
688 bfd_mach_o_reloc_info reloc;
693 addr = bfd_get_32 (abfd, raw->r_address);
694 symnum = bfd_get_32 (abfd, raw->r_symbolnum);
696 if (addr & BFD_MACH_O_SR_SCATTERED)
700 /* Scattered relocation.
701 Extract section and offset from r_value. */
702 res->sym_ptr_ptr = NULL;
704 for (j = 0; j < mdata->nsects; j++)
706 bfd_mach_o_section *sect = mdata->sections[j];
707 if (symnum >= sect->addr && symnum < sect->addr + sect->size)
709 res->sym_ptr_ptr = sect->bfdsection->symbol_ptr_ptr;
710 res->addend = symnum - sect->addr;
714 res->address = BFD_MACH_O_GET_SR_ADDRESS (addr);
715 reloc.r_type = BFD_MACH_O_GET_SR_TYPE (addr);
716 reloc.r_length = BFD_MACH_O_GET_SR_LENGTH (addr);
717 reloc.r_pcrel = addr & BFD_MACH_O_SR_PCREL;
718 reloc.r_scattered = 1;
722 unsigned int num = BFD_MACH_O_GET_R_SYMBOLNUM (symnum);
725 if (symnum & BFD_MACH_O_R_EXTERN)
732 BFD_ASSERT (num != 0);
733 BFD_ASSERT (num <= mdata->nsects);
734 sym = mdata->sections[num - 1]->bfdsection->symbol_ptr_ptr;
735 /* For a symbol defined in section S, the addend (stored in the
736 binary) contains the address of the section. To comply with
737 bfd conventio, substract the section address.
738 Use the address from the header, so that the user can modify
739 the vma of the section. */
740 res->addend = -mdata->sections[num - 1]->addr;
743 res->sym_ptr_ptr = sym;
744 reloc.r_type = BFD_MACH_O_GET_R_TYPE (symnum);
745 reloc.r_length = BFD_MACH_O_GET_R_LENGTH (symnum);
746 reloc.r_pcrel = (symnum & BFD_MACH_O_R_PCREL) ? 1 : 0;
747 reloc.r_scattered = 0;
750 if (!(*bed->_bfd_mach_o_swap_reloc_in)(res, &reloc))
756 bfd_mach_o_canonicalize_relocs (bfd *abfd, unsigned long filepos,
758 arelent *res, asymbol **syms)
761 struct mach_o_reloc_info_external *native_relocs;
762 bfd_size_type native_size;
764 /* Allocate and read relocs. */
765 native_size = count * BFD_MACH_O_RELENT_SIZE;
767 (struct mach_o_reloc_info_external *) bfd_malloc (native_size);
768 if (native_relocs == NULL)
771 if (bfd_seek (abfd, filepos, SEEK_SET) != 0
772 || bfd_bread (native_relocs, native_size, abfd) != native_size)
775 for (i = 0; i < count; i++)
777 if (bfd_mach_o_canonicalize_one_reloc (abfd, &native_relocs[i],
781 free (native_relocs);
784 free (native_relocs);
789 bfd_mach_o_canonicalize_reloc (bfd *abfd, asection *asect,
790 arelent **rels, asymbol **syms)
792 bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
796 if (asect->reloc_count == 0)
799 /* No need to go further if we don't know how to read relocs. */
800 if (bed->_bfd_mach_o_swap_reloc_in == NULL)
803 res = bfd_malloc (asect->reloc_count * sizeof (arelent));
807 if (bfd_mach_o_canonicalize_relocs (abfd, asect->rel_filepos,
808 asect->reloc_count, res, syms) < 0)
814 for (i = 0; i < asect->reloc_count; i++)
817 asect->relocation = res;
823 bfd_mach_o_get_dynamic_reloc_upper_bound (bfd *abfd)
825 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
827 if (mdata->dysymtab == NULL)
829 return (mdata->dysymtab->nextrel + mdata->dysymtab->nlocrel)
830 * sizeof (arelent *);
834 bfd_mach_o_canonicalize_dynamic_reloc (bfd *abfd, arelent **rels,
835 struct bfd_symbol **syms)
837 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
838 bfd_mach_o_dysymtab_command *dysymtab = mdata->dysymtab;
839 bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
843 if (dysymtab == NULL)
845 if (dysymtab->nextrel == 0 && dysymtab->nlocrel == 0)
848 /* No need to go further if we don't know how to read relocs. */
849 if (bed->_bfd_mach_o_swap_reloc_in == NULL)
852 res = bfd_malloc ((dysymtab->nextrel + dysymtab->nlocrel) * sizeof (arelent));
856 if (bfd_mach_o_canonicalize_relocs (abfd, dysymtab->extreloff,
857 dysymtab->nextrel, res, syms) < 0)
863 if (bfd_mach_o_canonicalize_relocs (abfd, dysymtab->locreloff,
865 res + dysymtab->nextrel, syms) < 0)
871 for (i = 0; i < dysymtab->nextrel + dysymtab->nlocrel; i++)
878 bfd_mach_o_write_relocs (bfd *abfd, bfd_mach_o_section *section)
880 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
884 bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
886 sec = section->bfdsection;
887 if (sec->reloc_count == 0)
890 if (bed->_bfd_mach_o_swap_reloc_out == NULL)
893 /* Allocate relocation room. */
894 mdata->filelen = FILE_ALIGN(mdata->filelen, 2);
895 section->nreloc = sec->reloc_count;
896 sec->rel_filepos = mdata->filelen;
897 section->reloff = sec->rel_filepos;
898 mdata->filelen += sec->reloc_count * BFD_MACH_O_RELENT_SIZE;
900 if (bfd_seek (abfd, section->reloff, SEEK_SET) != 0)
903 /* Convert and write. */
904 entries = section->bfdsection->orelocation;
905 for (i = 0; i < section->nreloc; i++)
907 arelent *rel = entries[i];
908 struct mach_o_reloc_info_external raw;
909 bfd_mach_o_reloc_info info, *pinfo = &info;
911 /* Convert relocation to an intermediate representation. */
912 if (!(*bed->_bfd_mach_o_swap_reloc_out) (rel, pinfo))
915 /* Lower the relocation info. */
916 if (pinfo->r_scattered)
920 v = BFD_MACH_O_SR_SCATTERED
921 | (pinfo->r_pcrel ? BFD_MACH_O_SR_PCREL : 0)
922 | BFD_MACH_O_SET_SR_LENGTH(pinfo->r_length)
923 | BFD_MACH_O_SET_SR_TYPE(pinfo->r_type)
924 | BFD_MACH_O_SET_SR_ADDRESS(pinfo->r_address);
925 /* Note: scattered relocs have field in reverse order... */
926 bfd_put_32 (abfd, v, raw.r_address);
927 bfd_put_32 (abfd, pinfo->r_value, raw.r_symbolnum);
933 bfd_put_32 (abfd, pinfo->r_address, raw.r_address);
934 v = BFD_MACH_O_SET_R_SYMBOLNUM (pinfo->r_value)
935 | (pinfo->r_pcrel ? BFD_MACH_O_R_PCREL : 0)
936 | BFD_MACH_O_SET_R_LENGTH (pinfo->r_length)
937 | (pinfo->r_extern ? BFD_MACH_O_R_EXTERN : 0)
938 | BFD_MACH_O_SET_R_TYPE (pinfo->r_type);
939 bfd_put_32 (abfd, v, raw.r_symbolnum);
942 if (bfd_bwrite (&raw, BFD_MACH_O_RELENT_SIZE, abfd)
943 != BFD_MACH_O_RELENT_SIZE)
950 bfd_mach_o_write_section_32 (bfd *abfd, bfd_mach_o_section *section)
952 struct mach_o_section_32_external raw;
954 memcpy (raw.sectname, section->sectname, 16);
955 memcpy (raw.segname + 16, section->segname, 16);
956 bfd_h_put_32 (abfd, section->addr, raw.addr);
957 bfd_h_put_32 (abfd, section->size, raw.size);
958 bfd_h_put_32 (abfd, section->offset, raw.offset);
959 bfd_h_put_32 (abfd, section->align, raw.align);
960 bfd_h_put_32 (abfd, section->reloff, raw.reloff);
961 bfd_h_put_32 (abfd, section->nreloc, raw.nreloc);
962 bfd_h_put_32 (abfd, section->flags, raw.flags);
963 bfd_h_put_32 (abfd, section->reserved1, raw.reserved1);
964 bfd_h_put_32 (abfd, section->reserved2, raw.reserved2);
966 if (bfd_bwrite (&raw, BFD_MACH_O_SECTION_SIZE, abfd)
967 != BFD_MACH_O_SECTION_SIZE)
974 bfd_mach_o_write_section_64 (bfd *abfd, bfd_mach_o_section *section)
976 struct mach_o_section_64_external raw;
978 memcpy (raw.sectname, section->sectname, 16);
979 memcpy (raw.segname, section->segname, 16);
980 bfd_h_put_64 (abfd, section->addr, raw.addr);
981 bfd_h_put_64 (abfd, section->size, raw.size);
982 bfd_h_put_32 (abfd, section->offset, raw.offset);
983 bfd_h_put_32 (abfd, section->align, raw.align);
984 bfd_h_put_32 (abfd, section->reloff, raw.reloff);
985 bfd_h_put_32 (abfd, section->nreloc, raw.nreloc);
986 bfd_h_put_32 (abfd, section->flags, raw.flags);
987 bfd_h_put_32 (abfd, section->reserved1, raw.reserved1);
988 bfd_h_put_32 (abfd, section->reserved2, raw.reserved2);
989 bfd_h_put_32 (abfd, section->reserved3, raw.reserved3);
991 if (bfd_bwrite (&raw, BFD_MACH_O_SECTION_64_SIZE, abfd)
992 != BFD_MACH_O_SECTION_64_SIZE)
999 bfd_mach_o_write_segment_32 (bfd *abfd, bfd_mach_o_load_command *command)
1001 struct mach_o_segment_command_32_external raw;
1002 bfd_mach_o_segment_command *seg = &command->command.segment;
1005 BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT);
1007 for (i = 0; i < seg->nsects; i++)
1008 if (!bfd_mach_o_write_relocs (abfd, &seg->sections[i]))
1011 memcpy (raw.segname, seg->segname, 16);
1012 bfd_h_put_32 (abfd, seg->vmaddr, raw.vmaddr);
1013 bfd_h_put_32 (abfd, seg->vmsize, raw.vmsize);
1014 bfd_h_put_32 (abfd, seg->fileoff, raw.fileoff);
1015 bfd_h_put_32 (abfd, seg->filesize, raw.filesize);
1016 bfd_h_put_32 (abfd, seg->maxprot, raw.maxprot);
1017 bfd_h_put_32 (abfd, seg->initprot, raw.initprot);
1018 bfd_h_put_32 (abfd, seg->nsects, raw.nsects);
1019 bfd_h_put_32 (abfd, seg->flags, raw.flags);
1021 if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
1022 || bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
1025 for (i = 0; i < seg->nsects; i++)
1026 if (bfd_mach_o_write_section_32 (abfd, &seg->sections[i]))
1033 bfd_mach_o_write_segment_64 (bfd *abfd, bfd_mach_o_load_command *command)
1035 struct mach_o_segment_command_64_external raw;
1036 bfd_mach_o_segment_command *seg = &command->command.segment;
1039 BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT_64);
1041 for (i = 0; i < seg->nsects; i++)
1042 if (!bfd_mach_o_write_relocs (abfd, &seg->sections[i]))
1045 memcpy (raw.segname, seg->segname, 16);
1046 bfd_h_put_64 (abfd, seg->vmaddr, raw.vmaddr);
1047 bfd_h_put_64 (abfd, seg->vmsize, raw.vmsize);
1048 bfd_h_put_64 (abfd, seg->fileoff, raw.fileoff);
1049 bfd_h_put_64 (abfd, seg->filesize, raw.filesize);
1050 bfd_h_put_32 (abfd, seg->maxprot, raw.maxprot);
1051 bfd_h_put_32 (abfd, seg->initprot, raw.initprot);
1052 bfd_h_put_32 (abfd, seg->nsects, raw.nsects);
1053 bfd_h_put_32 (abfd, seg->flags, raw.flags);
1055 if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
1056 || bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
1059 for (i = 0; i < seg->nsects; i++)
1060 if (bfd_mach_o_write_section_64 (abfd, &seg->sections[i]))
1067 bfd_mach_o_write_symtab (bfd *abfd, bfd_mach_o_load_command *command)
1069 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
1070 bfd_mach_o_symtab_command *sym = &command->command.symtab;
1072 unsigned int wide = bfd_mach_o_wide_p (abfd);
1073 unsigned int symlen = wide ? BFD_MACH_O_NLIST_64_SIZE : BFD_MACH_O_NLIST_SIZE;
1074 struct bfd_strtab_hash *strtab;
1075 asymbol **symbols = bfd_get_outsymbols (abfd);
1077 BFD_ASSERT (command->type == BFD_MACH_O_LC_SYMTAB);
1079 /* Write the symbols first. */
1080 mdata->filelen = FILE_ALIGN(mdata->filelen, wide ? 3 : 2);
1081 sym->symoff = mdata->filelen;
1082 if (bfd_seek (abfd, sym->symoff, SEEK_SET) != 0)
1085 sym->nsyms = bfd_get_symcount (abfd);
1086 mdata->filelen += sym->nsyms * symlen;
1088 strtab = _bfd_stringtab_init ();
1092 for (i = 0; i < sym->nsyms; i++)
1094 bfd_size_type str_index;
1095 bfd_mach_o_asymbol *s = (bfd_mach_o_asymbol *)symbols[i];
1097 /* Compute name index. */
1098 /* An index of 0 always means the empty string. */
1099 if (s->symbol.name == 0 || s->symbol.name[0] == '\0')
1103 str_index = _bfd_stringtab_add (strtab, s->symbol.name, TRUE, FALSE);
1104 if (str_index == (bfd_size_type) -1)
1110 struct mach_o_nlist_64_external raw;
1112 bfd_h_put_32 (abfd, str_index, raw.n_strx);
1113 bfd_h_put_8 (abfd, s->n_type, raw.n_type);
1114 bfd_h_put_8 (abfd, s->n_sect, raw.n_sect);
1115 bfd_h_put_16 (abfd, s->n_desc, raw.n_desc);
1116 bfd_h_put_64 (abfd, s->symbol.section->vma + s->symbol.value,
1119 if (bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
1124 struct mach_o_nlist_external raw;
1126 bfd_h_put_32 (abfd, str_index, raw.n_strx);
1127 bfd_h_put_8 (abfd, s->n_type, raw.n_type);
1128 bfd_h_put_8 (abfd, s->n_sect, raw.n_sect);
1129 bfd_h_put_16 (abfd, s->n_desc, raw.n_desc);
1130 bfd_h_put_32 (abfd, s->symbol.section->vma + s->symbol.value,
1133 if (bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
1137 sym->strsize = _bfd_stringtab_size (strtab);
1138 sym->stroff = mdata->filelen;
1139 mdata->filelen += sym->strsize;
1141 if (_bfd_stringtab_emit (abfd, strtab) != TRUE)
1143 _bfd_stringtab_free (strtab);
1147 struct mach_o_symtab_command_external raw;
1149 bfd_h_put_32 (abfd, sym->symoff, raw.symoff);
1150 bfd_h_put_32 (abfd, sym->nsyms, raw.nsyms);
1151 bfd_h_put_32 (abfd, sym->stroff, raw.stroff);
1152 bfd_h_put_32 (abfd, sym->strsize, raw.strsize);
1154 if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
1155 || bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
1162 _bfd_stringtab_free (strtab);
1166 /* Process the symbols and generate Mach-O specific fields.
1170 bfd_mach_o_mangle_symbols (bfd *abfd)
1173 asymbol **symbols = bfd_get_outsymbols (abfd);
1175 for (i = 0; i < bfd_get_symcount (abfd); i++)
1177 bfd_mach_o_asymbol *s = (bfd_mach_o_asymbol *)symbols[i];
1179 if (s->n_type == BFD_MACH_O_N_UNDF && !(s->symbol.flags & BSF_DEBUGGING))
1181 /* As genuine Mach-O symbols type shouldn't be N_UNDF (undefined
1182 symbols should be N_UNDEF | N_EXT), we suppose the back-end
1183 values haven't been set. */
1184 if (s->symbol.section == bfd_abs_section_ptr)
1185 s->n_type = BFD_MACH_O_N_ABS;
1186 else if (s->symbol.section == bfd_und_section_ptr)
1188 s->n_type = BFD_MACH_O_N_UNDF;
1189 if (s->symbol.flags & BSF_WEAK)
1190 s->n_desc |= BFD_MACH_O_N_WEAK_REF;
1192 else if (s->symbol.section == bfd_com_section_ptr)
1193 s->n_type = BFD_MACH_O_N_UNDF | BFD_MACH_O_N_EXT;
1195 s->n_type = BFD_MACH_O_N_SECT;
1197 if (s->symbol.flags & BSF_GLOBAL)
1198 s->n_type |= BFD_MACH_O_N_EXT;
1201 /* Compute section index. */
1202 if (s->symbol.section != bfd_abs_section_ptr
1203 && s->symbol.section != bfd_und_section_ptr
1204 && s->symbol.section != bfd_com_section_ptr)
1205 s->n_sect = s->symbol.section->target_index;
1207 /* Number symbols. */
1208 s->symbol.udata.i = i;
1214 bfd_mach_o_write_contents (bfd *abfd)
1217 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
1219 if (mdata->header.ncmds == 0)
1220 if (!bfd_mach_o_build_commands (abfd))
1223 /* Now write header information. */
1224 if (mdata->header.filetype == 0)
1226 if (abfd->flags & EXEC_P)
1227 mdata->header.filetype = BFD_MACH_O_MH_EXECUTE;
1228 else if (abfd->flags & DYNAMIC)
1229 mdata->header.filetype = BFD_MACH_O_MH_DYLIB;
1231 mdata->header.filetype = BFD_MACH_O_MH_OBJECT;
1233 if (!bfd_mach_o_write_header (abfd, &mdata->header))
1236 /* Assign a number to each symbols. */
1237 if (!bfd_mach_o_mangle_symbols (abfd))
1240 for (i = 0; i < mdata->header.ncmds; i++)
1242 struct mach_o_load_command_external raw;
1243 bfd_mach_o_load_command *cur = &mdata->commands[i];
1244 unsigned long typeflag;
1246 typeflag = cur->type | (cur->type_required ? BFD_MACH_O_LC_REQ_DYLD : 0);
1248 bfd_h_put_32 (abfd, typeflag, raw.cmd);
1249 bfd_h_put_32 (abfd, cur->len, raw.cmdsize);
1251 if (bfd_seek (abfd, cur->offset, SEEK_SET) != 0
1252 || bfd_bwrite (&raw, BFD_MACH_O_LC_SIZE, abfd) != 8)
1257 case BFD_MACH_O_LC_SEGMENT:
1258 if (bfd_mach_o_write_segment_32 (abfd, cur) != 0)
1261 case BFD_MACH_O_LC_SEGMENT_64:
1262 if (bfd_mach_o_write_segment_64 (abfd, cur) != 0)
1265 case BFD_MACH_O_LC_SYMTAB:
1266 if (!bfd_mach_o_write_symtab (abfd, cur))
1269 case BFD_MACH_O_LC_SYMSEG:
1271 case BFD_MACH_O_LC_THREAD:
1272 case BFD_MACH_O_LC_UNIXTHREAD:
1273 if (bfd_mach_o_write_thread (abfd, cur) != 0)
1276 case BFD_MACH_O_LC_LOADFVMLIB:
1277 case BFD_MACH_O_LC_IDFVMLIB:
1278 case BFD_MACH_O_LC_IDENT:
1279 case BFD_MACH_O_LC_FVMFILE:
1280 case BFD_MACH_O_LC_PREPAGE:
1281 case BFD_MACH_O_LC_DYSYMTAB:
1282 case BFD_MACH_O_LC_LOAD_DYLIB:
1283 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB:
1284 case BFD_MACH_O_LC_ID_DYLIB:
1285 case BFD_MACH_O_LC_REEXPORT_DYLIB:
1286 case BFD_MACH_O_LC_LOAD_DYLINKER:
1287 case BFD_MACH_O_LC_ID_DYLINKER:
1288 case BFD_MACH_O_LC_PREBOUND_DYLIB:
1289 case BFD_MACH_O_LC_ROUTINES:
1290 case BFD_MACH_O_LC_SUB_FRAMEWORK:
1293 (*_bfd_error_handler) (_("unable to write unknown load command 0x%lx"),
1294 (unsigned long) cur->type);
1302 /* Build Mach-O load commands from the sections. */
1305 bfd_mach_o_build_commands (bfd *abfd)
1307 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
1308 unsigned int wide = mach_o_wide_p (&mdata->header);
1309 bfd_mach_o_segment_command *seg;
1310 bfd_mach_o_section *sections;
1312 bfd_mach_o_load_command *cmd;
1313 bfd_mach_o_load_command *symtab_cmd;
1316 /* Return now if commands are already built. */
1317 if (mdata->header.ncmds)
1320 /* Very simple version: 1 command (segment) containing all sections. */
1321 mdata->header.ncmds = 2;
1322 mdata->commands = bfd_alloc (abfd, mdata->header.ncmds
1323 * sizeof (bfd_mach_o_load_command));
1324 if (mdata->commands == NULL)
1326 cmd = &mdata->commands[0];
1327 seg = &cmd->command.segment;
1329 seg->nsects = bfd_count_sections (abfd);
1330 sections = bfd_alloc (abfd, seg->nsects * sizeof (bfd_mach_o_section));
1331 if (sections == NULL)
1333 seg->sections = sections;
1335 /* Set segment command. */
1338 cmd->type = BFD_MACH_O_LC_SEGMENT_64;
1339 cmd->offset = BFD_MACH_O_HEADER_64_SIZE;
1340 cmd->len = BFD_MACH_O_LC_SEGMENT_64_SIZE
1341 + BFD_MACH_O_SECTION_64_SIZE * seg->nsects;
1345 cmd->type = BFD_MACH_O_LC_SEGMENT;
1346 cmd->offset = BFD_MACH_O_HEADER_SIZE;
1347 cmd->len = BFD_MACH_O_LC_SEGMENT_SIZE
1348 + BFD_MACH_O_SECTION_SIZE * seg->nsects;
1350 cmd->type_required = FALSE;
1351 mdata->header.sizeofcmds = cmd->len;
1352 mdata->filelen = cmd->offset + cmd->len;
1354 /* Set symtab command. */
1355 symtab_cmd = &mdata->commands[1];
1357 symtab_cmd->type = BFD_MACH_O_LC_SYMTAB;
1358 symtab_cmd->offset = cmd->offset + cmd->len;
1359 symtab_cmd->len = 6 * 4;
1360 symtab_cmd->type_required = FALSE;
1362 mdata->header.sizeofcmds += symtab_cmd->len;
1363 mdata->filelen += symtab_cmd->len;
1365 /* Fill segment command. */
1366 memset (seg->segname, 0, sizeof (seg->segname));
1368 seg->fileoff = mdata->filelen;
1370 seg->maxprot = BFD_MACH_O_PROT_READ | BFD_MACH_O_PROT_WRITE
1371 | BFD_MACH_O_PROT_EXECUTE;
1372 seg->initprot = seg->maxprot;
1375 /* Create Mach-O sections. */
1377 for (sec = abfd->sections; sec; sec = sec->next)
1379 sections->bfdsection = sec;
1380 bfd_mach_o_convert_section_name_to_mach_o (abfd, sec, sections);
1381 sections->addr = bfd_get_section_vma (abfd, sec);
1382 sections->size = bfd_get_section_size (sec);
1383 sections->align = bfd_get_section_alignment (abfd, sec);
1385 if (sections->size != 0)
1387 mdata->filelen = FILE_ALIGN (mdata->filelen, sections->align);
1388 sections->offset = mdata->filelen;
1391 sections->offset = 0;
1392 sections->reloff = 0;
1393 sections->nreloc = 0;
1394 sections->reserved1 = 0;
1395 sections->reserved2 = 0;
1396 sections->reserved3 = 0;
1398 sec->filepos = sections->offset;
1399 sec->target_index = ++target_index;
1401 mdata->filelen += sections->size;
1404 seg->filesize = mdata->filelen - seg->fileoff;
1405 seg->vmsize = seg->filesize;
1410 /* Set the contents of a section. */
1413 bfd_mach_o_set_section_contents (bfd *abfd,
1415 const void * location,
1417 bfd_size_type count)
1421 /* This must be done first, because bfd_set_section_contents is
1422 going to set output_has_begun to TRUE. */
1423 if (! abfd->output_has_begun && ! bfd_mach_o_build_commands (abfd))
1429 pos = section->filepos + offset;
1430 if (bfd_seek (abfd, pos, SEEK_SET) != 0
1431 || bfd_bwrite (location, count, abfd) != count)
1438 bfd_mach_o_sizeof_headers (bfd *a ATTRIBUTE_UNUSED,
1439 struct bfd_link_info *info ATTRIBUTE_UNUSED)
1444 /* Make an empty symbol. This is required only because
1445 bfd_make_section_anyway wants to create a symbol for the section. */
1448 bfd_mach_o_make_empty_symbol (bfd *abfd)
1450 asymbol *new_symbol;
1452 new_symbol = bfd_zalloc (abfd, sizeof (bfd_mach_o_asymbol));
1453 if (new_symbol == NULL)
1455 new_symbol->the_bfd = abfd;
1456 new_symbol->udata.i = 0;
1461 bfd_mach_o_read_header (bfd *abfd, bfd_mach_o_header *header)
1463 struct mach_o_header_external raw;
1465 bfd_vma (*get32) (const void *) = NULL;
1467 /* Just read the magic number. */
1468 if (bfd_seek (abfd, 0, SEEK_SET) != 0
1469 || bfd_bread (raw.magic, sizeof (raw.magic), abfd) != 4)
1472 if (bfd_getb32 (raw.magic) == BFD_MACH_O_MH_MAGIC)
1474 header->byteorder = BFD_ENDIAN_BIG;
1475 header->magic = BFD_MACH_O_MH_MAGIC;
1476 header->version = 1;
1479 else if (bfd_getl32 (raw.magic) == BFD_MACH_O_MH_MAGIC)
1481 header->byteorder = BFD_ENDIAN_LITTLE;
1482 header->magic = BFD_MACH_O_MH_MAGIC;
1483 header->version = 1;
1486 else if (bfd_getb32 (raw.magic) == BFD_MACH_O_MH_MAGIC_64)
1488 header->byteorder = BFD_ENDIAN_BIG;
1489 header->magic = BFD_MACH_O_MH_MAGIC_64;
1490 header->version = 2;
1493 else if (bfd_getl32 (raw.magic) == BFD_MACH_O_MH_MAGIC_64)
1495 header->byteorder = BFD_ENDIAN_LITTLE;
1496 header->magic = BFD_MACH_O_MH_MAGIC_64;
1497 header->version = 2;
1502 header->byteorder = BFD_ENDIAN_UNKNOWN;
1506 /* Once the size of the header is known, read the full header. */
1507 size = mach_o_wide_p (header) ?
1508 BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE;
1510 if (bfd_seek (abfd, 0, SEEK_SET) != 0
1511 || bfd_bread (&raw, size, abfd) != size)
1514 header->cputype = (*get32) (raw.cputype);
1515 header->cpusubtype = (*get32) (raw.cpusubtype);
1516 header->filetype = (*get32) (raw.filetype);
1517 header->ncmds = (*get32) (raw.ncmds);
1518 header->sizeofcmds = (*get32) (raw.sizeofcmds);
1519 header->flags = (*get32) (raw.flags);
1521 if (mach_o_wide_p (header))
1522 header->reserved = (*get32) (raw.reserved);
1528 bfd_mach_o_make_bfd_section (bfd *abfd, bfd_mach_o_section *section,
1535 bfd_mach_o_convert_section_name_to_bfd (abfd, section, &sname, &flags);
1539 if (flags == SEC_NO_FLAGS)
1541 /* Try to guess flags. */
1542 if (section->flags & BFD_MACH_O_S_ATTR_DEBUG)
1543 flags = SEC_DEBUGGING;
1547 if ((section->flags & BFD_MACH_O_SECTION_TYPE_MASK)
1548 != BFD_MACH_O_S_ZEROFILL)
1551 if (prot & BFD_MACH_O_PROT_EXECUTE)
1553 if (prot & BFD_MACH_O_PROT_WRITE)
1555 else if (prot & BFD_MACH_O_PROT_READ)
1556 flags |= SEC_READONLY;
1562 if ((flags & SEC_DEBUGGING) == 0)
1566 if (section->offset != 0)
1567 flags |= SEC_HAS_CONTENTS;
1568 if (section->nreloc != 0)
1571 bfdsec = bfd_make_section_anyway_with_flags (abfd, sname, flags);
1575 bfdsec->vma = section->addr;
1576 bfdsec->lma = section->addr;
1577 bfdsec->size = section->size;
1578 bfdsec->filepos = section->offset;
1579 bfdsec->alignment_power = section->align;
1580 bfdsec->segment_mark = 0;
1581 bfdsec->reloc_count = section->nreloc;
1582 bfdsec->rel_filepos = section->reloff;
1588 bfd_mach_o_read_section_32 (bfd *abfd,
1589 bfd_mach_o_section *section,
1590 unsigned int offset,
1593 struct mach_o_section_32_external raw;
1595 if (bfd_seek (abfd, offset, SEEK_SET) != 0
1596 || (bfd_bread (&raw, BFD_MACH_O_SECTION_SIZE, abfd)
1597 != BFD_MACH_O_SECTION_SIZE))
1600 memcpy (section->sectname, raw.sectname, 16);
1601 section->sectname[16] = '\0';
1602 memcpy (section->segname, raw.segname, 16);
1603 section->segname[16] = '\0';
1604 section->addr = bfd_h_get_32 (abfd, raw.addr);
1605 section->size = bfd_h_get_32 (abfd, raw.size);
1606 section->offset = bfd_h_get_32 (abfd, raw.offset);
1607 section->align = bfd_h_get_32 (abfd, raw.align);
1608 section->reloff = bfd_h_get_32 (abfd, raw.reloff);
1609 section->nreloc = bfd_h_get_32 (abfd, raw.nreloc);
1610 section->flags = bfd_h_get_32 (abfd, raw.flags);
1611 section->reserved1 = bfd_h_get_32 (abfd, raw.reserved1);
1612 section->reserved2 = bfd_h_get_32 (abfd, raw.reserved2);
1613 section->reserved3 = 0;
1614 section->bfdsection = bfd_mach_o_make_bfd_section (abfd, section, prot);
1616 if (section->bfdsection == NULL)
1623 bfd_mach_o_read_section_64 (bfd *abfd,
1624 bfd_mach_o_section *section,
1625 unsigned int offset,
1628 struct mach_o_section_64_external raw;
1630 if (bfd_seek (abfd, offset, SEEK_SET) != 0
1631 || (bfd_bread (&raw, BFD_MACH_O_SECTION_64_SIZE, abfd)
1632 != BFD_MACH_O_SECTION_64_SIZE))
1635 memcpy (section->sectname, raw.sectname, 16);
1636 section->sectname[16] = '\0';
1637 memcpy (section->segname, raw.segname, 16);
1638 section->segname[16] = '\0';
1639 section->addr = bfd_h_get_64 (abfd, raw.addr);
1640 section->size = bfd_h_get_64 (abfd, raw.size);
1641 section->offset = bfd_h_get_32 (abfd, raw.offset);
1642 section->align = bfd_h_get_32 (abfd, raw.align);
1643 section->reloff = bfd_h_get_32 (abfd, raw.reloff);
1644 section->nreloc = bfd_h_get_32 (abfd, raw.nreloc);
1645 section->flags = bfd_h_get_32 (abfd, raw.flags);
1646 section->reserved1 = bfd_h_get_32 (abfd, raw.reserved1);
1647 section->reserved2 = bfd_h_get_32 (abfd, raw.reserved2);
1648 section->reserved3 = bfd_h_get_32 (abfd, raw.reserved3);
1649 section->bfdsection = bfd_mach_o_make_bfd_section (abfd, section, prot);
1651 if (section->bfdsection == NULL)
1658 bfd_mach_o_read_section (bfd *abfd,
1659 bfd_mach_o_section *section,
1660 unsigned int offset,
1665 return bfd_mach_o_read_section_64 (abfd, section, offset, prot);
1667 return bfd_mach_o_read_section_32 (abfd, section, offset, prot);
1671 bfd_mach_o_read_symtab_symbol (bfd *abfd,
1672 bfd_mach_o_symtab_command *sym,
1673 bfd_mach_o_asymbol *s,
1676 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
1677 unsigned int wide = mach_o_wide_p (&mdata->header);
1678 unsigned int symwidth =
1679 wide ? BFD_MACH_O_NLIST_64_SIZE : BFD_MACH_O_NLIST_SIZE;
1680 unsigned int symoff = sym->symoff + (i * symwidth);
1681 struct mach_o_nlist_64_external raw;
1682 unsigned char type = -1;
1683 unsigned char section = -1;
1685 symvalue value = -1;
1686 unsigned long stroff = -1;
1687 unsigned int symtype = -1;
1689 BFD_ASSERT (sym->strtab != NULL);
1691 if (bfd_seek (abfd, symoff, SEEK_SET) != 0
1692 || bfd_bread (&raw, symwidth, abfd) != symwidth)
1694 (*_bfd_error_handler)
1695 (_("bfd_mach_o_read_symtab_symbol: unable to read %d bytes at %lu"),
1696 symwidth, (unsigned long) symoff);
1700 stroff = bfd_h_get_32 (abfd, raw.n_strx);
1701 type = bfd_h_get_8 (abfd, raw.n_type);
1702 symtype = type & BFD_MACH_O_N_TYPE;
1703 section = bfd_h_get_8 (abfd, raw.n_sect);
1704 desc = bfd_h_get_16 (abfd, raw.n_desc);
1706 value = bfd_h_get_64 (abfd, raw.n_value);
1708 value = bfd_h_get_32 (abfd, raw.n_value);
1710 if (stroff >= sym->strsize)
1712 (*_bfd_error_handler)
1713 (_("bfd_mach_o_read_symtab_symbol: name out of range (%lu >= %lu)"),
1714 (unsigned long) stroff,
1715 (unsigned long) sym->strsize);
1719 s->symbol.the_bfd = abfd;
1720 s->symbol.name = sym->strtab + stroff;
1721 s->symbol.value = value;
1722 s->symbol.flags = 0x0;
1723 s->symbol.udata.i = 0;
1725 s->n_sect = section;
1728 if (type & BFD_MACH_O_N_STAB)
1730 s->symbol.flags |= BSF_DEBUGGING;
1731 s->symbol.section = bfd_und_section_ptr;
1743 if ((section > 0) && (section <= mdata->nsects))
1745 s->symbol.section = mdata->sections[section - 1]->bfdsection;
1747 s->symbol.value - mdata->sections[section - 1]->addr;
1754 if (type & BFD_MACH_O_N_PEXT)
1755 s->symbol.flags |= BSF_GLOBAL;
1757 if (type & BFD_MACH_O_N_EXT)
1758 s->symbol.flags |= BSF_GLOBAL;
1760 if (!(type & (BFD_MACH_O_N_PEXT | BFD_MACH_O_N_EXT)))
1761 s->symbol.flags |= BSF_LOCAL;
1765 case BFD_MACH_O_N_UNDF:
1766 if (type == (BFD_MACH_O_N_UNDF | BFD_MACH_O_N_EXT)
1767 && s->symbol.value != 0)
1769 /* A common symbol. */
1770 s->symbol.section = bfd_com_section_ptr;
1771 s->symbol.flags = BSF_NO_FLAGS;
1775 s->symbol.section = bfd_und_section_ptr;
1776 if (s->n_desc & BFD_MACH_O_N_WEAK_REF)
1777 s->symbol.flags |= BSF_WEAK;
1780 case BFD_MACH_O_N_PBUD:
1781 s->symbol.section = bfd_und_section_ptr;
1783 case BFD_MACH_O_N_ABS:
1784 s->symbol.section = bfd_abs_section_ptr;
1786 case BFD_MACH_O_N_SECT:
1787 if ((section > 0) && (section <= mdata->nsects))
1789 s->symbol.section = mdata->sections[section - 1]->bfdsection;
1791 s->symbol.value - mdata->sections[section - 1]->addr;
1795 /* Mach-O uses 0 to mean "no section"; not an error. */
1798 (*_bfd_error_handler) (_("bfd_mach_o_read_symtab_symbol: "
1799 "symbol \"%s\" specified invalid section %d (max %lu): setting to undefined"),
1800 s->symbol.name, section, mdata->nsects);
1802 s->symbol.section = bfd_und_section_ptr;
1805 case BFD_MACH_O_N_INDR:
1806 (*_bfd_error_handler) (_("bfd_mach_o_read_symtab_symbol: "
1807 "symbol \"%s\" is unsupported 'indirect' reference: setting to undefined"),
1809 s->symbol.section = bfd_und_section_ptr;
1812 (*_bfd_error_handler) (_("bfd_mach_o_read_symtab_symbol: "
1813 "symbol \"%s\" specified invalid type field 0x%x: setting to undefined"),
1814 s->symbol.name, symtype);
1815 s->symbol.section = bfd_und_section_ptr;
1824 bfd_mach_o_read_symtab_strtab (bfd *abfd)
1826 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
1827 bfd_mach_o_symtab_command *sym = mdata->symtab;
1829 /* Fail if there is no symtab. */
1833 /* Success if already loaded. */
1837 if (abfd->flags & BFD_IN_MEMORY)
1839 struct bfd_in_memory *b;
1841 b = (struct bfd_in_memory *) abfd->iostream;
1843 if ((sym->stroff + sym->strsize) > b->size)
1845 bfd_set_error (bfd_error_file_truncated);
1848 sym->strtab = (char *) b->buffer + sym->stroff;
1852 sym->strtab = bfd_alloc (abfd, sym->strsize);
1853 if (sym->strtab == NULL)
1856 if (bfd_seek (abfd, sym->stroff, SEEK_SET) != 0
1857 || bfd_bread ((void *) sym->strtab, sym->strsize, abfd) != sym->strsize)
1859 bfd_set_error (bfd_error_file_truncated);
1868 bfd_mach_o_read_symtab_symbols (bfd *abfd)
1870 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
1871 bfd_mach_o_symtab_command *sym = mdata->symtab;
1875 if (sym == NULL || sym->symbols)
1877 /* Return now if there are no symbols or if already loaded. */
1881 sym->symbols = bfd_alloc (abfd, sym->nsyms * sizeof (bfd_mach_o_asymbol));
1883 if (sym->symbols == NULL)
1885 (*_bfd_error_handler) (_("bfd_mach_o_read_symtab_symbols: unable to allocate memory for symbols"));
1889 ret = bfd_mach_o_read_symtab_strtab (abfd);
1893 for (i = 0; i < sym->nsyms; i++)
1895 ret = bfd_mach_o_read_symtab_symbol (abfd, sym, &sym->symbols[i], i);
1904 bfd_mach_o_read_dysymtab_symbol (bfd *abfd,
1905 bfd_mach_o_dysymtab_command *dysym,
1906 bfd_mach_o_symtab_command *sym,
1907 bfd_mach_o_asymbol *s,
1910 unsigned long isymoff = dysym->indirectsymoff + (i * 4);
1911 unsigned long sym_index;
1912 unsigned char raw[4];
1914 BFD_ASSERT (i < dysym->nindirectsyms);
1916 if (bfd_seek (abfd, isymoff, SEEK_SET) != 0
1917 || bfd_bread (raw, sizeof (raw), abfd) != sizeof (raw))
1919 (*_bfd_error_handler)
1920 (_("bfd_mach_o_read_dysymtab_symbol: unable to read %lu bytes at %lu"),
1921 (unsigned long) sizeof (raw), isymoff);
1924 sym_index = bfd_h_get_32 (abfd, raw);
1926 return bfd_mach_o_read_symtab_symbol (abfd, sym, s, sym_index);
1930 bfd_mach_o_i386_flavour_string (unsigned int flavour)
1932 switch ((int) flavour)
1934 case BFD_MACH_O_x86_THREAD_STATE32: return "x86_THREAD_STATE32";
1935 case BFD_MACH_O_x86_FLOAT_STATE32: return "x86_FLOAT_STATE32";
1936 case BFD_MACH_O_x86_EXCEPTION_STATE32: return "x86_EXCEPTION_STATE32";
1937 case BFD_MACH_O_x86_THREAD_STATE64: return "x86_THREAD_STATE64";
1938 case BFD_MACH_O_x86_FLOAT_STATE64: return "x86_FLOAT_STATE64";
1939 case BFD_MACH_O_x86_EXCEPTION_STATE64: return "x86_EXCEPTION_STATE64";
1940 case BFD_MACH_O_x86_THREAD_STATE: return "x86_THREAD_STATE";
1941 case BFD_MACH_O_x86_FLOAT_STATE: return "x86_FLOAT_STATE";
1942 case BFD_MACH_O_x86_EXCEPTION_STATE: return "x86_EXCEPTION_STATE";
1943 case BFD_MACH_O_x86_DEBUG_STATE32: return "x86_DEBUG_STATE32";
1944 case BFD_MACH_O_x86_DEBUG_STATE64: return "x86_DEBUG_STATE64";
1945 case BFD_MACH_O_x86_DEBUG_STATE: return "x86_DEBUG_STATE";
1946 case BFD_MACH_O_x86_THREAD_STATE_NONE: return "x86_THREAD_STATE_NONE";
1947 default: return "UNKNOWN";
1952 bfd_mach_o_ppc_flavour_string (unsigned int flavour)
1954 switch ((int) flavour)
1956 case BFD_MACH_O_PPC_THREAD_STATE: return "PPC_THREAD_STATE";
1957 case BFD_MACH_O_PPC_FLOAT_STATE: return "PPC_FLOAT_STATE";
1958 case BFD_MACH_O_PPC_EXCEPTION_STATE: return "PPC_EXCEPTION_STATE";
1959 case BFD_MACH_O_PPC_VECTOR_STATE: return "PPC_VECTOR_STATE";
1960 case BFD_MACH_O_PPC_THREAD_STATE64: return "PPC_THREAD_STATE64";
1961 case BFD_MACH_O_PPC_EXCEPTION_STATE64: return "PPC_EXCEPTION_STATE64";
1962 default: return "UNKNOWN";
1967 bfd_mach_o_read_dylinker (bfd *abfd, bfd_mach_o_load_command *command)
1969 bfd_mach_o_dylinker_command *cmd = &command->command.dylinker;
1970 struct mach_o_str_command_external raw;
1971 unsigned int nameoff;
1973 BFD_ASSERT ((command->type == BFD_MACH_O_LC_ID_DYLINKER)
1974 || (command->type == BFD_MACH_O_LC_LOAD_DYLINKER));
1976 if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
1977 || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
1980 nameoff = bfd_h_get_32 (abfd, raw.str);
1982 cmd->name_offset = command->offset + nameoff;
1983 cmd->name_len = command->len - nameoff;
1984 cmd->name_str = bfd_alloc (abfd, cmd->name_len);
1985 if (cmd->name_str == NULL)
1987 if (bfd_seek (abfd, cmd->name_offset, SEEK_SET) != 0
1988 || bfd_bread (cmd->name_str, cmd->name_len, abfd) != cmd->name_len)
1994 bfd_mach_o_read_dylib (bfd *abfd, bfd_mach_o_load_command *command)
1996 bfd_mach_o_dylib_command *cmd = &command->command.dylib;
1997 struct mach_o_dylib_command_external raw;
1998 unsigned int nameoff;
2000 switch (command->type)
2002 case BFD_MACH_O_LC_LOAD_DYLIB:
2003 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB:
2004 case BFD_MACH_O_LC_ID_DYLIB:
2005 case BFD_MACH_O_LC_REEXPORT_DYLIB:
2012 if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
2013 || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
2016 nameoff = bfd_h_get_32 (abfd, raw.name);
2017 cmd->timestamp = bfd_h_get_32 (abfd, raw.timestamp);
2018 cmd->current_version = bfd_h_get_32 (abfd, raw.current_version);
2019 cmd->compatibility_version = bfd_h_get_32 (abfd, raw.compatibility_version);
2021 cmd->name_offset = command->offset + nameoff;
2022 cmd->name_len = command->len - nameoff;
2023 cmd->name_str = bfd_alloc (abfd, cmd->name_len);
2024 if (cmd->name_str == NULL)
2026 if (bfd_seek (abfd, cmd->name_offset, SEEK_SET) != 0
2027 || bfd_bread (cmd->name_str, cmd->name_len, abfd) != cmd->name_len)
2033 bfd_mach_o_read_prebound_dylib (bfd *abfd ATTRIBUTE_UNUSED,
2034 bfd_mach_o_load_command *command ATTRIBUTE_UNUSED)
2036 /* bfd_mach_o_prebound_dylib_command *cmd = &command->command.prebound_dylib; */
2038 BFD_ASSERT (command->type == BFD_MACH_O_LC_PREBOUND_DYLIB);
2043 bfd_mach_o_read_thread (bfd *abfd, bfd_mach_o_load_command *command)
2045 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
2046 bfd_mach_o_thread_command *cmd = &command->command.thread;
2047 unsigned int offset;
2048 unsigned int nflavours;
2051 BFD_ASSERT ((command->type == BFD_MACH_O_LC_THREAD)
2052 || (command->type == BFD_MACH_O_LC_UNIXTHREAD));
2054 /* Count the number of threads. */
2057 while (offset != command->len)
2059 struct mach_o_thread_command_external raw;
2061 if (offset >= command->len)
2064 if (bfd_seek (abfd, command->offset + offset, SEEK_SET) != 0
2065 || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
2068 offset += sizeof (raw) + bfd_h_get_32 (abfd, raw.count) * 4;
2072 /* Allocate threads. */
2073 cmd->flavours = bfd_alloc
2074 (abfd, nflavours * sizeof (bfd_mach_o_thread_flavour));
2075 if (cmd->flavours == NULL)
2077 cmd->nflavours = nflavours;
2081 while (offset != command->len)
2083 struct mach_o_thread_command_external raw;
2085 if (offset >= command->len)
2088 if (nflavours >= cmd->nflavours)
2091 if (bfd_seek (abfd, command->offset + offset, SEEK_SET) != 0
2092 || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
2095 cmd->flavours[nflavours].flavour = bfd_h_get_32 (abfd, raw.flavour);
2096 cmd->flavours[nflavours].offset = command->offset + offset + sizeof (raw);
2097 cmd->flavours[nflavours].size = bfd_h_get_32 (abfd, raw.count) * 4;
2098 offset += cmd->flavours[nflavours].size + sizeof (raw);
2102 for (i = 0; i < nflavours; i++)
2105 unsigned int snamelen;
2107 const char *flavourstr;
2108 const char *prefix = "LC_THREAD";
2111 switch (mdata->header.cputype)
2113 case BFD_MACH_O_CPU_TYPE_POWERPC:
2114 case BFD_MACH_O_CPU_TYPE_POWERPC_64:
2115 flavourstr = bfd_mach_o_ppc_flavour_string (cmd->flavours[i].flavour);
2117 case BFD_MACH_O_CPU_TYPE_I386:
2118 case BFD_MACH_O_CPU_TYPE_X86_64:
2119 flavourstr = bfd_mach_o_i386_flavour_string (cmd->flavours[i].flavour);
2122 flavourstr = "UNKNOWN_ARCHITECTURE";
2126 snamelen = strlen (prefix) + 1 + 20 + 1 + strlen (flavourstr) + 1;
2127 sname = bfd_alloc (abfd, snamelen);
2133 sprintf (sname, "%s.%s.%u", prefix, flavourstr, j);
2134 if (bfd_get_section_by_name (abfd, sname) == NULL)
2139 bfdsec = bfd_make_section_with_flags (abfd, sname, SEC_HAS_CONTENTS);
2143 bfdsec->size = cmd->flavours[i].size;
2144 bfdsec->filepos = cmd->flavours[i].offset;
2145 bfdsec->alignment_power = 0x0;
2147 cmd->section = bfdsec;
2154 bfd_mach_o_read_dysymtab (bfd *abfd, bfd_mach_o_load_command *command)
2156 bfd_mach_o_dysymtab_command *cmd = &command->command.dysymtab;
2157 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
2159 BFD_ASSERT (command->type == BFD_MACH_O_LC_DYSYMTAB);
2162 struct mach_o_dysymtab_command_external raw;
2164 if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
2165 || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
2168 cmd->ilocalsym = bfd_h_get_32 (abfd, raw.ilocalsym);
2169 cmd->nlocalsym = bfd_h_get_32 (abfd, raw.nlocalsym);
2170 cmd->iextdefsym = bfd_h_get_32 (abfd, raw.iextdefsym);
2171 cmd->nextdefsym = bfd_h_get_32 (abfd, raw.nextdefsym);
2172 cmd->iundefsym = bfd_h_get_32 (abfd, raw.iundefsym);
2173 cmd->nundefsym = bfd_h_get_32 (abfd, raw.nundefsym);
2174 cmd->tocoff = bfd_h_get_32 (abfd, raw.tocoff);
2175 cmd->ntoc = bfd_h_get_32 (abfd, raw.ntoc);
2176 cmd->modtaboff = bfd_h_get_32 (abfd, raw.modtaboff);
2177 cmd->nmodtab = bfd_h_get_32 (abfd, raw.nmodtab);
2178 cmd->extrefsymoff = bfd_h_get_32 (abfd, raw.extrefsymoff);
2179 cmd->nextrefsyms = bfd_h_get_32 (abfd, raw.nextrefsyms);
2180 cmd->indirectsymoff = bfd_h_get_32 (abfd, raw.indirectsymoff);
2181 cmd->nindirectsyms = bfd_h_get_32 (abfd, raw.nindirectsyms);
2182 cmd->extreloff = bfd_h_get_32 (abfd, raw.extreloff);
2183 cmd->nextrel = bfd_h_get_32 (abfd, raw.nextrel);
2184 cmd->locreloff = bfd_h_get_32 (abfd, raw.locreloff);
2185 cmd->nlocrel = bfd_h_get_32 (abfd, raw.nlocrel);
2188 if (cmd->nmodtab != 0)
2191 int wide = bfd_mach_o_wide_p (abfd);
2192 unsigned int module_len = wide ? 56 : 52;
2195 bfd_alloc (abfd, cmd->nmodtab * sizeof (bfd_mach_o_dylib_module));
2196 if (cmd->dylib_module == NULL)
2199 if (bfd_seek (abfd, cmd->modtaboff, SEEK_SET) != 0)
2202 for (i = 0; i < cmd->nmodtab; i++)
2204 bfd_mach_o_dylib_module *module = &cmd->dylib_module[i];
2206 unsigned char buf[56];
2208 if (bfd_bread ((void *) buf, module_len, abfd) != module_len)
2211 module->module_name_idx = bfd_h_get_32 (abfd, buf + 0);
2212 module->iextdefsym = bfd_h_get_32 (abfd, buf + 4);
2213 module->nextdefsym = bfd_h_get_32 (abfd, buf + 8);
2214 module->irefsym = bfd_h_get_32 (abfd, buf + 12);
2215 module->nrefsym = bfd_h_get_32 (abfd, buf + 16);
2216 module->ilocalsym = bfd_h_get_32 (abfd, buf + 20);
2217 module->nlocalsym = bfd_h_get_32 (abfd, buf + 24);
2218 module->iextrel = bfd_h_get_32 (abfd, buf + 28);
2219 module->nextrel = bfd_h_get_32 (abfd, buf + 32);
2220 v = bfd_h_get_32 (abfd, buf +36);
2221 module->iinit = v & 0xffff;
2222 module->iterm = (v >> 16) & 0xffff;
2223 v = bfd_h_get_32 (abfd, buf + 40);
2224 module->ninit = v & 0xffff;
2225 module->nterm = (v >> 16) & 0xffff;
2228 module->objc_module_info_size = bfd_h_get_32 (abfd, buf + 44);
2229 module->objc_module_info_addr = bfd_h_get_64 (abfd, buf + 48);
2233 module->objc_module_info_addr = bfd_h_get_32 (abfd, buf + 44);
2234 module->objc_module_info_size = bfd_h_get_32 (abfd, buf + 48);
2243 cmd->dylib_toc = bfd_alloc
2244 (abfd, cmd->ntoc * sizeof (bfd_mach_o_dylib_table_of_content));
2245 if (cmd->dylib_toc == NULL)
2248 if (bfd_seek (abfd, cmd->tocoff, SEEK_SET) != 0)
2251 for (i = 0; i < cmd->ntoc; i++)
2253 struct mach_o_dylib_table_of_contents_external raw;
2254 bfd_mach_o_dylib_table_of_content *toc = &cmd->dylib_toc[i];
2256 if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
2259 toc->symbol_index = bfd_h_get_32 (abfd, raw.symbol_index);
2260 toc->module_index = bfd_h_get_32 (abfd, raw.module_index);
2264 if (cmd->nindirectsyms != 0)
2268 cmd->indirect_syms = bfd_alloc
2269 (abfd, cmd->nindirectsyms * sizeof (unsigned int));
2270 if (cmd->indirect_syms == NULL)
2273 if (bfd_seek (abfd, cmd->indirectsymoff, SEEK_SET) != 0)
2276 for (i = 0; i < cmd->nindirectsyms; i++)
2278 unsigned char raw[4];
2279 unsigned int *is = &cmd->indirect_syms[i];
2281 if (bfd_bread (raw, sizeof (raw), abfd) != sizeof (raw))
2284 *is = bfd_h_get_32 (abfd, raw);
2288 if (cmd->nextrefsyms != 0)
2293 cmd->ext_refs = bfd_alloc
2294 (abfd, cmd->nextrefsyms * sizeof (bfd_mach_o_dylib_reference));
2295 if (cmd->ext_refs == NULL)
2298 if (bfd_seek (abfd, cmd->extrefsymoff, SEEK_SET) != 0)
2301 for (i = 0; i < cmd->nextrefsyms; i++)
2303 unsigned char raw[4];
2304 bfd_mach_o_dylib_reference *ref = &cmd->ext_refs[i];
2306 if (bfd_bread (raw, sizeof (raw), abfd) != sizeof (raw))
2309 /* Fields isym and flags are written as bit-fields, thus we need
2310 a specific processing for endianness. */
2311 v = bfd_h_get_32 (abfd, raw);
2312 if (bfd_big_endian (abfd))
2314 ref->isym = (v >> 8) & 0xffffff;
2315 ref->flags = v & 0xff;
2319 ref->isym = v & 0xffffff;
2320 ref->flags = (v >> 24) & 0xff;
2325 if (mdata->dysymtab)
2327 mdata->dysymtab = cmd;
2333 bfd_mach_o_read_symtab (bfd *abfd, bfd_mach_o_load_command *command)
2335 bfd_mach_o_symtab_command *symtab = &command->command.symtab;
2336 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
2337 struct mach_o_symtab_command_external raw;
2339 BFD_ASSERT (command->type == BFD_MACH_O_LC_SYMTAB);
2341 if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
2342 || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
2345 symtab->symoff = bfd_h_get_32 (abfd, raw.symoff);
2346 symtab->nsyms = bfd_h_get_32 (abfd, raw.nsyms);
2347 symtab->stroff = bfd_h_get_32 (abfd, raw.stroff);
2348 symtab->strsize = bfd_h_get_32 (abfd, raw.strsize);
2349 symtab->symbols = NULL;
2350 symtab->strtab = NULL;
2352 if (symtab->nsyms != 0)
2353 abfd->flags |= HAS_SYMS;
2357 mdata->symtab = symtab;
2362 bfd_mach_o_read_uuid (bfd *abfd, bfd_mach_o_load_command *command)
2364 bfd_mach_o_uuid_command *cmd = &command->command.uuid;
2366 BFD_ASSERT (command->type == BFD_MACH_O_LC_UUID);
2368 if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
2369 || bfd_bread (cmd->uuid, 16, abfd) != 16)
2376 bfd_mach_o_read_linkedit (bfd *abfd, bfd_mach_o_load_command *command)
2378 bfd_mach_o_linkedit_command *cmd = &command->command.linkedit;
2379 struct mach_o_linkedit_data_command_external raw;
2381 if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
2382 || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
2385 cmd->dataoff = bfd_get_32 (abfd, raw.dataoff);
2386 cmd->datasize = bfd_get_32 (abfd, raw.datasize);
2391 bfd_mach_o_read_str (bfd *abfd, bfd_mach_o_load_command *command)
2393 bfd_mach_o_str_command *cmd = &command->command.str;
2394 struct mach_o_str_command_external raw;
2397 if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
2398 || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
2401 off = bfd_get_32 (abfd, raw.str);
2402 cmd->stroff = command->offset + off;
2403 cmd->str_len = command->len - off;
2404 cmd->str = bfd_alloc (abfd, cmd->str_len);
2405 if (cmd->str == NULL)
2407 if (bfd_seek (abfd, cmd->stroff, SEEK_SET) != 0
2408 || bfd_bread ((void *) cmd->str, cmd->str_len, abfd) != cmd->str_len)
2414 bfd_mach_o_read_dyld_info (bfd *abfd, bfd_mach_o_load_command *command)
2416 bfd_mach_o_dyld_info_command *cmd = &command->command.dyld_info;
2417 struct mach_o_dyld_info_command_external raw;
2419 if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
2420 || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
2423 cmd->rebase_off = bfd_get_32 (abfd, raw.rebase_off);
2424 cmd->rebase_size = bfd_get_32 (abfd, raw.rebase_size);
2425 cmd->bind_off = bfd_get_32 (abfd, raw.bind_off);
2426 cmd->bind_size = bfd_get_32 (abfd, raw.bind_size);
2427 cmd->weak_bind_off = bfd_get_32 (abfd, raw.weak_bind_off);
2428 cmd->weak_bind_size = bfd_get_32 (abfd, raw.weak_bind_size);
2429 cmd->lazy_bind_off = bfd_get_32 (abfd, raw.lazy_bind_off);
2430 cmd->lazy_bind_size = bfd_get_32 (abfd, raw.lazy_bind_size);
2431 cmd->export_off = bfd_get_32 (abfd, raw.export_off);
2432 cmd->export_size = bfd_get_32 (abfd, raw.export_size);
2437 bfd_mach_o_read_version_min (bfd *abfd, bfd_mach_o_load_command *command)
2439 bfd_mach_o_version_min_command *cmd = &command->command.version_min;
2440 struct mach_o_version_min_command_external raw;
2443 if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
2444 || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
2447 ver = bfd_get_32 (abfd, raw.version);
2448 cmd->rel = ver >> 16;
2449 cmd->maj = ver >> 8;
2451 cmd->reserved = bfd_get_32 (abfd, raw.reserved);
2456 bfd_mach_o_read_segment (bfd *abfd,
2457 bfd_mach_o_load_command *command,
2460 bfd_mach_o_segment_command *seg = &command->command.segment;
2465 struct mach_o_segment_command_64_external raw;
2467 BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT_64);
2469 if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
2470 || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
2473 memcpy (seg->segname, raw.segname, 16);
2474 seg->segname[16] = '\0';
2476 seg->vmaddr = bfd_h_get_64 (abfd, raw.vmaddr);
2477 seg->vmsize = bfd_h_get_64 (abfd, raw.vmsize);
2478 seg->fileoff = bfd_h_get_64 (abfd, raw.fileoff);
2479 seg->filesize = bfd_h_get_64 (abfd, raw.filesize);
2480 seg->maxprot = bfd_h_get_32 (abfd, raw.maxprot);
2481 seg->initprot = bfd_h_get_32 (abfd, raw.initprot);
2482 seg->nsects = bfd_h_get_32 (abfd, raw.nsects);
2483 seg->flags = bfd_h_get_32 (abfd, raw.flags);
2487 struct mach_o_segment_command_32_external raw;
2489 BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT);
2491 if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
2492 || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
2495 memcpy (seg->segname, raw.segname, 16);
2496 seg->segname[16] = '\0';
2498 seg->vmaddr = bfd_h_get_32 (abfd, raw.vmaddr);
2499 seg->vmsize = bfd_h_get_32 (abfd, raw.vmsize);
2500 seg->fileoff = bfd_h_get_32 (abfd, raw.fileoff);
2501 seg->filesize = bfd_h_get_32 (abfd, raw.filesize);
2502 seg->maxprot = bfd_h_get_32 (abfd, raw.maxprot);
2503 seg->initprot = bfd_h_get_32 (abfd, raw.initprot);
2504 seg->nsects = bfd_h_get_32 (abfd, raw.nsects);
2505 seg->flags = bfd_h_get_32 (abfd, raw.flags);
2508 if (seg->nsects != 0)
2510 seg->sections = bfd_alloc (abfd, seg->nsects
2511 * sizeof (bfd_mach_o_section));
2512 if (seg->sections == NULL)
2515 for (i = 0; i < seg->nsects; i++)
2519 segoff = command->offset + BFD_MACH_O_LC_SEGMENT_64_SIZE
2520 + (i * BFD_MACH_O_SECTION_64_SIZE);
2522 segoff = command->offset + BFD_MACH_O_LC_SEGMENT_SIZE
2523 + (i * BFD_MACH_O_SECTION_SIZE);
2525 if (bfd_mach_o_read_section
2526 (abfd, &seg->sections[i], segoff, seg->initprot, wide) != 0)
2535 bfd_mach_o_read_segment_32 (bfd *abfd, bfd_mach_o_load_command *command)
2537 return bfd_mach_o_read_segment (abfd, command, 0);
2541 bfd_mach_o_read_segment_64 (bfd *abfd, bfd_mach_o_load_command *command)
2543 return bfd_mach_o_read_segment (abfd, command, 1);
2547 bfd_mach_o_read_command (bfd *abfd, bfd_mach_o_load_command *command)
2549 struct mach_o_load_command_external raw;
2552 /* Read command type and length. */
2553 if (bfd_seek (abfd, command->offset, SEEK_SET) != 0
2554 || bfd_bread (&raw, BFD_MACH_O_LC_SIZE, abfd) != BFD_MACH_O_LC_SIZE)
2557 cmd = bfd_h_get_32 (abfd, raw.cmd);
2558 command->type = cmd & ~BFD_MACH_O_LC_REQ_DYLD;
2559 command->type_required = cmd & BFD_MACH_O_LC_REQ_DYLD ? TRUE : FALSE;
2560 command->len = bfd_h_get_32 (abfd, raw.cmdsize);
2562 switch (command->type)
2564 case BFD_MACH_O_LC_SEGMENT:
2565 if (bfd_mach_o_read_segment_32 (abfd, command) != 0)
2568 case BFD_MACH_O_LC_SEGMENT_64:
2569 if (bfd_mach_o_read_segment_64 (abfd, command) != 0)
2572 case BFD_MACH_O_LC_SYMTAB:
2573 if (bfd_mach_o_read_symtab (abfd, command) != 0)
2576 case BFD_MACH_O_LC_SYMSEG:
2578 case BFD_MACH_O_LC_THREAD:
2579 case BFD_MACH_O_LC_UNIXTHREAD:
2580 if (bfd_mach_o_read_thread (abfd, command) != 0)
2583 case BFD_MACH_O_LC_LOAD_DYLINKER:
2584 case BFD_MACH_O_LC_ID_DYLINKER:
2585 if (bfd_mach_o_read_dylinker (abfd, command) != 0)
2588 case BFD_MACH_O_LC_LOAD_DYLIB:
2589 case BFD_MACH_O_LC_ID_DYLIB:
2590 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB:
2591 case BFD_MACH_O_LC_REEXPORT_DYLIB:
2592 if (bfd_mach_o_read_dylib (abfd, command) != 0)
2595 case BFD_MACH_O_LC_PREBOUND_DYLIB:
2596 if (bfd_mach_o_read_prebound_dylib (abfd, command) != 0)
2599 case BFD_MACH_O_LC_LOADFVMLIB:
2600 case BFD_MACH_O_LC_IDFVMLIB:
2601 case BFD_MACH_O_LC_IDENT:
2602 case BFD_MACH_O_LC_FVMFILE:
2603 case BFD_MACH_O_LC_PREPAGE:
2604 case BFD_MACH_O_LC_ROUTINES:
2605 case BFD_MACH_O_LC_ROUTINES_64:
2607 case BFD_MACH_O_LC_SUB_FRAMEWORK:
2608 case BFD_MACH_O_LC_SUB_UMBRELLA:
2609 case BFD_MACH_O_LC_SUB_LIBRARY:
2610 case BFD_MACH_O_LC_SUB_CLIENT:
2611 case BFD_MACH_O_LC_RPATH:
2612 if (bfd_mach_o_read_str (abfd, command) != 0)
2615 case BFD_MACH_O_LC_DYSYMTAB:
2616 if (bfd_mach_o_read_dysymtab (abfd, command) != 0)
2619 case BFD_MACH_O_LC_TWOLEVEL_HINTS:
2620 case BFD_MACH_O_LC_PREBIND_CKSUM:
2622 case BFD_MACH_O_LC_UUID:
2623 if (bfd_mach_o_read_uuid (abfd, command) != 0)
2626 case BFD_MACH_O_LC_CODE_SIGNATURE:
2627 case BFD_MACH_O_LC_SEGMENT_SPLIT_INFO:
2628 case BFD_MACH_O_LC_FUNCTION_STARTS:
2629 if (bfd_mach_o_read_linkedit (abfd, command) != 0)
2632 case BFD_MACH_O_LC_DYLD_INFO:
2633 if (bfd_mach_o_read_dyld_info (abfd, command) != 0)
2636 case BFD_MACH_O_LC_VERSION_MIN_MACOSX:
2637 case BFD_MACH_O_LC_VERSION_MIN_IPHONEOS:
2638 if (!bfd_mach_o_read_version_min (abfd, command))
2642 (*_bfd_error_handler) (_("unable to read unknown load command 0x%lx"),
2643 (unsigned long) command->type);
2651 bfd_mach_o_flatten_sections (bfd *abfd)
2653 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
2657 /* Count total number of sections. */
2660 for (i = 0; i < mdata->header.ncmds; i++)
2662 if (mdata->commands[i].type == BFD_MACH_O_LC_SEGMENT
2663 || mdata->commands[i].type == BFD_MACH_O_LC_SEGMENT_64)
2665 bfd_mach_o_segment_command *seg;
2667 seg = &mdata->commands[i].command.segment;
2668 mdata->nsects += seg->nsects;
2672 /* Allocate sections array. */
2673 mdata->sections = bfd_alloc (abfd,
2674 mdata->nsects * sizeof (bfd_mach_o_section *));
2676 /* Fill the array. */
2679 for (i = 0; i < mdata->header.ncmds; i++)
2681 if (mdata->commands[i].type == BFD_MACH_O_LC_SEGMENT
2682 || mdata->commands[i].type == BFD_MACH_O_LC_SEGMENT_64)
2684 bfd_mach_o_segment_command *seg;
2686 seg = &mdata->commands[i].command.segment;
2687 BFD_ASSERT (csect + seg->nsects <= mdata->nsects);
2689 for (j = 0; j < seg->nsects; j++)
2690 mdata->sections[csect++] = &seg->sections[j];
2696 bfd_mach_o_scan_start_address (bfd *abfd)
2698 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
2699 bfd_mach_o_thread_command *cmd = NULL;
2702 for (i = 0; i < mdata->header.ncmds; i++)
2704 if ((mdata->commands[i].type == BFD_MACH_O_LC_THREAD) ||
2705 (mdata->commands[i].type == BFD_MACH_O_LC_UNIXTHREAD))
2708 cmd = &mdata->commands[i].command.thread;
2717 for (i = 0; i < cmd->nflavours; i++)
2719 if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_I386)
2720 && (cmd->flavours[i].flavour
2721 == (unsigned long) BFD_MACH_O_x86_THREAD_STATE32))
2723 unsigned char buf[4];
2725 if (bfd_seek (abfd, cmd->flavours[i].offset + 40, SEEK_SET) != 0
2726 || bfd_bread (buf, 4, abfd) != 4)
2729 abfd->start_address = bfd_h_get_32 (abfd, buf);
2731 else if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_POWERPC)
2732 && (cmd->flavours[i].flavour == BFD_MACH_O_PPC_THREAD_STATE))
2734 unsigned char buf[4];
2736 if (bfd_seek (abfd, cmd->flavours[i].offset + 0, SEEK_SET) != 0
2737 || bfd_bread (buf, 4, abfd) != 4)
2740 abfd->start_address = bfd_h_get_32 (abfd, buf);
2742 else if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_POWERPC_64)
2743 && (cmd->flavours[i].flavour == BFD_MACH_O_PPC_THREAD_STATE64))
2745 unsigned char buf[8];
2747 if (bfd_seek (abfd, cmd->flavours[i].offset + 0, SEEK_SET) != 0
2748 || bfd_bread (buf, 8, abfd) != 8)
2751 abfd->start_address = bfd_h_get_64 (abfd, buf);
2753 else if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_X86_64)
2754 && (cmd->flavours[i].flavour == BFD_MACH_O_x86_THREAD_STATE64))
2756 unsigned char buf[8];
2758 if (bfd_seek (abfd, cmd->flavours[i].offset + (16 * 8), SEEK_SET) != 0
2759 || bfd_bread (buf, 8, abfd) != 8)
2762 abfd->start_address = bfd_h_get_64 (abfd, buf);
2770 bfd_mach_o_set_arch_mach (bfd *abfd,
2771 enum bfd_architecture arch,
2772 unsigned long machine)
2774 bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
2776 /* If this isn't the right architecture for this backend, and this
2777 isn't the generic backend, fail. */
2778 if (arch != bed->arch
2779 && arch != bfd_arch_unknown
2780 && bed->arch != bfd_arch_unknown)
2783 return bfd_default_set_arch_mach (abfd, arch, machine);
2787 bfd_mach_o_scan (bfd *abfd,
2788 bfd_mach_o_header *header,
2789 bfd_mach_o_data_struct *mdata)
2792 enum bfd_architecture cputype;
2793 unsigned long cpusubtype;
2794 unsigned int hdrsize;
2796 hdrsize = mach_o_wide_p (header) ?
2797 BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE;
2799 mdata->header = *header;
2801 abfd->flags = abfd->flags & BFD_IN_MEMORY;
2802 switch (header->filetype)
2804 case BFD_MACH_O_MH_OBJECT:
2805 abfd->flags |= HAS_RELOC;
2807 case BFD_MACH_O_MH_EXECUTE:
2808 abfd->flags |= EXEC_P;
2810 case BFD_MACH_O_MH_DYLIB:
2811 case BFD_MACH_O_MH_BUNDLE:
2812 abfd->flags |= DYNAMIC;
2816 abfd->tdata.mach_o_data = mdata;
2818 bfd_mach_o_convert_architecture (header->cputype, header->cpusubtype,
2819 &cputype, &cpusubtype);
2820 if (cputype == bfd_arch_unknown)
2822 (*_bfd_error_handler) (_("bfd_mach_o_scan: unknown architecture 0x%lx/0x%lx"),
2823 header->cputype, header->cpusubtype);
2827 bfd_set_arch_mach (abfd, cputype, cpusubtype);
2829 if (header->ncmds != 0)
2831 mdata->commands = bfd_alloc
2832 (abfd, header->ncmds * sizeof (bfd_mach_o_load_command));
2833 if (mdata->commands == NULL)
2836 for (i = 0; i < header->ncmds; i++)
2838 bfd_mach_o_load_command *cur = &mdata->commands[i];
2841 cur->offset = hdrsize;
2844 bfd_mach_o_load_command *prev = &mdata->commands[i - 1];
2845 cur->offset = prev->offset + prev->len;
2848 if (bfd_mach_o_read_command (abfd, cur) < 0)
2853 if (bfd_mach_o_scan_start_address (abfd) < 0)
2856 bfd_mach_o_flatten_sections (abfd);
2861 bfd_mach_o_mkobject_init (bfd *abfd)
2863 bfd_mach_o_data_struct *mdata = NULL;
2865 mdata = bfd_alloc (abfd, sizeof (bfd_mach_o_data_struct));
2868 abfd->tdata.mach_o_data = mdata;
2870 mdata->header.magic = 0;
2871 mdata->header.cputype = 0;
2872 mdata->header.cpusubtype = 0;
2873 mdata->header.filetype = 0;
2874 mdata->header.ncmds = 0;
2875 mdata->header.sizeofcmds = 0;
2876 mdata->header.flags = 0;
2877 mdata->header.byteorder = BFD_ENDIAN_UNKNOWN;
2878 mdata->commands = NULL;
2880 mdata->sections = NULL;
2886 bfd_mach_o_gen_mkobject (bfd *abfd)
2888 bfd_mach_o_data_struct *mdata;
2890 if (!bfd_mach_o_mkobject_init (abfd))
2893 mdata = bfd_mach_o_get_data (abfd);
2894 mdata->header.magic = BFD_MACH_O_MH_MAGIC;
2895 mdata->header.cputype = 0;
2896 mdata->header.cpusubtype = 0;
2897 mdata->header.byteorder = abfd->xvec->byteorder;
2898 mdata->header.version = 1;
2904 bfd_mach_o_header_p (bfd *abfd,
2905 bfd_mach_o_filetype filetype,
2906 bfd_mach_o_cpu_type cputype)
2908 struct bfd_preserve preserve;
2909 bfd_mach_o_header header;
2911 preserve.marker = NULL;
2912 if (!bfd_mach_o_read_header (abfd, &header))
2915 if (! (header.byteorder == BFD_ENDIAN_BIG
2916 || header.byteorder == BFD_ENDIAN_LITTLE))
2918 (*_bfd_error_handler) (_("unknown header byte-order value 0x%lx"),
2919 (unsigned long) header.byteorder);
2923 if (! ((header.byteorder == BFD_ENDIAN_BIG
2924 && abfd->xvec->byteorder == BFD_ENDIAN_BIG
2925 && abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
2926 || (header.byteorder == BFD_ENDIAN_LITTLE
2927 && abfd->xvec->byteorder == BFD_ENDIAN_LITTLE
2928 && abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)))
2931 /* Check cputype and filetype.
2932 In case of wildcard, do not accept magics that are handled by existing
2936 if (header.cputype != cputype)
2941 switch (header.cputype)
2943 case BFD_MACH_O_CPU_TYPE_I386:
2944 /* Handled by mach-o-i386 */
2952 if (header.filetype != filetype)
2957 switch (header.filetype)
2959 case BFD_MACH_O_MH_CORE:
2960 /* Handled by core_p */
2967 preserve.marker = bfd_zalloc (abfd, sizeof (bfd_mach_o_data_struct));
2968 if (preserve.marker == NULL
2969 || !bfd_preserve_save (abfd, &preserve))
2972 if (bfd_mach_o_scan (abfd, &header,
2973 (bfd_mach_o_data_struct *) preserve.marker) != 0)
2976 bfd_preserve_finish (abfd, &preserve);
2980 bfd_set_error (bfd_error_wrong_format);
2983 if (preserve.marker != NULL)
2984 bfd_preserve_restore (abfd, &preserve);
2988 static const bfd_target *
2989 bfd_mach_o_gen_object_p (bfd *abfd)
2991 return bfd_mach_o_header_p (abfd, 0, 0);
2994 static const bfd_target *
2995 bfd_mach_o_gen_core_p (bfd *abfd)
2997 return bfd_mach_o_header_p (abfd, BFD_MACH_O_MH_CORE, 0);
3000 typedef struct mach_o_fat_archentry
3002 unsigned long cputype;
3003 unsigned long cpusubtype;
3004 unsigned long offset;
3006 unsigned long align;
3007 } mach_o_fat_archentry;
3009 typedef struct mach_o_fat_data_struct
3011 unsigned long magic;
3012 unsigned long nfat_arch;
3013 mach_o_fat_archentry *archentries;
3014 } mach_o_fat_data_struct;
3017 bfd_mach_o_archive_p (bfd *abfd)
3019 mach_o_fat_data_struct *adata = NULL;
3020 struct mach_o_fat_header_external hdr;
3023 if (bfd_seek (abfd, 0, SEEK_SET) != 0
3024 || bfd_bread (&hdr, sizeof (hdr), abfd) != sizeof (hdr))
3027 adata = bfd_alloc (abfd, sizeof (mach_o_fat_data_struct));
3031 adata->magic = bfd_getb32 (hdr.magic);
3032 adata->nfat_arch = bfd_getb32 (hdr.nfat_arch);
3033 if (adata->magic != 0xcafebabe)
3035 /* Avoid matching Java bytecode files, which have the same magic number.
3036 In the Java bytecode file format this field contains the JVM version,
3037 which starts at 43.0. */
3038 if (adata->nfat_arch > 30)
3041 adata->archentries =
3042 bfd_alloc (abfd, adata->nfat_arch * sizeof (mach_o_fat_archentry));
3043 if (adata->archentries == NULL)
3046 for (i = 0; i < adata->nfat_arch; i++)
3048 struct mach_o_fat_arch_external arch;
3049 if (bfd_bread (&arch, sizeof (arch), abfd) != sizeof (arch))
3051 adata->archentries[i].cputype = bfd_getb32 (arch.cputype);
3052 adata->archentries[i].cpusubtype = bfd_getb32 (arch.cpusubtype);
3053 adata->archentries[i].offset = bfd_getb32 (arch.offset);
3054 adata->archentries[i].size = bfd_getb32 (arch.size);
3055 adata->archentries[i].align = bfd_getb32 (arch.align);
3058 abfd->tdata.mach_o_fat_data = adata;
3063 bfd_release (abfd, adata);
3064 bfd_set_error (bfd_error_wrong_format);
3069 bfd_mach_o_openr_next_archived_file (bfd *archive, bfd *prev)
3071 mach_o_fat_data_struct *adata;
3072 mach_o_fat_archentry *entry = NULL;
3075 enum bfd_architecture arch_type;
3076 unsigned long arch_subtype;
3078 adata = (mach_o_fat_data_struct *) archive->tdata.mach_o_fat_data;
3079 BFD_ASSERT (adata != NULL);
3081 /* Find index of previous entry. */
3083 i = 0; /* Start at first one. */
3086 for (i = 0; i < adata->nfat_arch; i++)
3088 if (adata->archentries[i].offset == prev->origin)
3092 if (i == adata->nfat_arch)
3095 bfd_set_error (bfd_error_bad_value);
3098 i++; /* Get next entry. */
3101 if (i >= adata->nfat_arch)
3103 bfd_set_error (bfd_error_no_more_archived_files);
3107 entry = &adata->archentries[i];
3108 nbfd = _bfd_new_bfd_contained_in (archive);
3112 nbfd->origin = entry->offset;
3114 bfd_mach_o_convert_architecture (entry->cputype, entry->cpusubtype,
3115 &arch_type, &arch_subtype);
3116 /* Create the member filename.
3117 Use FILENAME:ARCH_NAME. */
3120 const char *arch_name;
3121 size_t arch_file_len = strlen (bfd_get_filename (archive));
3123 arch_name = bfd_printable_arch_mach (arch_type, arch_subtype);
3124 s = bfd_malloc (arch_file_len + 1 + strlen (arch_name) + 1);
3127 memcpy (s, bfd_get_filename (archive), arch_file_len);
3128 s[arch_file_len] = ':';
3129 strcpy (s + arch_file_len + 1, arch_name);
3132 nbfd->iostream = NULL;
3133 bfd_set_arch_mach (nbfd, arch_type, arch_subtype);
3138 /* If ABFD format is FORMAT and architecture is ARCH, return it.
3139 If ABFD is a fat image containing a member that corresponds to FORMAT
3140 and ARCH, returns it.
3141 In other case, returns NULL.
3142 This function allows transparent uses of fat images. */
3144 bfd_mach_o_fat_extract (bfd *abfd,
3146 const bfd_arch_info_type *arch)
3149 mach_o_fat_data_struct *adata;
3152 if (bfd_check_format (abfd, format))
3154 if (bfd_get_arch_info (abfd) == arch)
3158 if (!bfd_check_format (abfd, bfd_archive)
3159 || abfd->xvec != &mach_o_fat_vec)
3162 /* This is a Mach-O fat image. */
3163 adata = (mach_o_fat_data_struct *) abfd->tdata.mach_o_fat_data;
3164 BFD_ASSERT (adata != NULL);
3166 for (i = 0; i < adata->nfat_arch; i++)
3168 struct mach_o_fat_archentry *e = &adata->archentries[i];
3169 enum bfd_architecture cpu_type;
3170 unsigned long cpu_subtype;
3172 bfd_mach_o_convert_architecture (e->cputype, e->cpusubtype,
3173 &cpu_type, &cpu_subtype);
3174 if (cpu_type != arch->arch || cpu_subtype != arch->mach)
3177 /* The architecture is found. */
3178 res = _bfd_new_bfd_contained_in (abfd);
3182 res->origin = e->offset;
3184 res->filename = strdup (abfd->filename);
3185 res->iostream = NULL;
3187 if (bfd_check_format (res, format))
3189 BFD_ASSERT (bfd_get_arch_info (res) == arch);
3200 bfd_mach_o_lookup_section (bfd *abfd,
3202 bfd_mach_o_load_command **mcommand,
3203 bfd_mach_o_section **msection)
3205 struct mach_o_data_struct *md = bfd_mach_o_get_data (abfd);
3206 unsigned int i, j, num;
3208 bfd_mach_o_load_command *ncmd = NULL;
3209 bfd_mach_o_section *nsect = NULL;
3211 BFD_ASSERT (mcommand != NULL);
3212 BFD_ASSERT (msection != NULL);
3215 for (i = 0; i < md->header.ncmds; i++)
3217 struct bfd_mach_o_load_command *cmd = &md->commands[i];
3218 struct bfd_mach_o_segment_command *seg = NULL;
3220 if (cmd->type != BFD_MACH_O_LC_SEGMENT
3221 || cmd->type != BFD_MACH_O_LC_SEGMENT_64)
3223 seg = &cmd->command.segment;
3225 for (j = 0; j < seg->nsects; j++)
3227 struct bfd_mach_o_section *sect = &seg->sections[j];
3229 if (sect->bfdsection == section)
3247 bfd_mach_o_lookup_command (bfd *abfd,
3248 bfd_mach_o_load_command_type type,
3249 bfd_mach_o_load_command **mcommand)
3251 struct mach_o_data_struct *md = bfd_mach_o_get_data (abfd);
3252 bfd_mach_o_load_command *ncmd = NULL;
3253 unsigned int i, num;
3255 BFD_ASSERT (md != NULL);
3256 BFD_ASSERT (mcommand != NULL);
3259 for (i = 0; i < md->header.ncmds; i++)
3261 struct bfd_mach_o_load_command *cmd = &md->commands[i];
3263 if (cmd->type != type)
3276 bfd_mach_o_stack_addr (enum bfd_mach_o_cpu_type type)
3280 case BFD_MACH_O_CPU_TYPE_MC680x0:
3282 case BFD_MACH_O_CPU_TYPE_MC88000:
3284 case BFD_MACH_O_CPU_TYPE_POWERPC:
3286 case BFD_MACH_O_CPU_TYPE_I386:
3288 case BFD_MACH_O_CPU_TYPE_SPARC:
3290 case BFD_MACH_O_CPU_TYPE_I860:
3292 case BFD_MACH_O_CPU_TYPE_HPPA:
3293 return 0xc0000000 - 0x04000000;
3299 typedef struct bfd_mach_o_xlat_name
3304 bfd_mach_o_xlat_name;
3307 bfd_mach_o_print_flags (const bfd_mach_o_xlat_name *table,
3313 for (; table->name; table++)
3315 if (table->val & val)
3318 fprintf (file, "+");
3319 fprintf (file, "%s", table->name);
3327 fprintf (file, "+");
3328 fprintf (file, "0x%lx", val);
3332 fprintf (file, "-");
3336 bfd_mach_o_get_name_or_null (const bfd_mach_o_xlat_name *table,
3339 for (; table->name; table++)
3340 if (table->val == val)
3346 bfd_mach_o_get_name (const bfd_mach_o_xlat_name *table, unsigned long val)
3348 const char *res = bfd_mach_o_get_name_or_null (table, val);
3356 static bfd_mach_o_xlat_name bfd_mach_o_cpu_name[] =
3358 { "vax", BFD_MACH_O_CPU_TYPE_VAX },
3359 { "mc680x0", BFD_MACH_O_CPU_TYPE_MC680x0 },
3360 { "i386", BFD_MACH_O_CPU_TYPE_I386 },
3361 { "mips", BFD_MACH_O_CPU_TYPE_MIPS },
3362 { "mc98000", BFD_MACH_O_CPU_TYPE_MC98000 },
3363 { "hppa", BFD_MACH_O_CPU_TYPE_HPPA },
3364 { "arm", BFD_MACH_O_CPU_TYPE_ARM },
3365 { "mc88000", BFD_MACH_O_CPU_TYPE_MC88000 },
3366 { "sparc", BFD_MACH_O_CPU_TYPE_SPARC },
3367 { "i860", BFD_MACH_O_CPU_TYPE_I860 },
3368 { "alpha", BFD_MACH_O_CPU_TYPE_ALPHA },
3369 { "powerpc", BFD_MACH_O_CPU_TYPE_POWERPC },
3370 { "powerpc_64", BFD_MACH_O_CPU_TYPE_POWERPC_64 },
3371 { "x86_64", BFD_MACH_O_CPU_TYPE_X86_64 },
3375 static bfd_mach_o_xlat_name bfd_mach_o_filetype_name[] =
3377 { "object", BFD_MACH_O_MH_OBJECT },
3378 { "execute", BFD_MACH_O_MH_EXECUTE },
3379 { "fvmlib", BFD_MACH_O_MH_FVMLIB },
3380 { "core", BFD_MACH_O_MH_CORE },
3381 { "preload", BFD_MACH_O_MH_PRELOAD },
3382 { "dylib", BFD_MACH_O_MH_DYLIB },
3383 { "dylinker", BFD_MACH_O_MH_DYLINKER },
3384 { "bundle", BFD_MACH_O_MH_BUNDLE },
3385 { "dylib_stub", BFD_MACH_O_MH_DYLIB_STUB },
3386 { "dym", BFD_MACH_O_MH_DSYM },
3387 { "kext_bundle", BFD_MACH_O_MH_KEXT_BUNDLE },
3391 static bfd_mach_o_xlat_name bfd_mach_o_header_flags_name[] =
3393 { "noundefs", BFD_MACH_O_MH_NOUNDEFS },
3394 { "incrlink", BFD_MACH_O_MH_INCRLINK },
3395 { "dyldlink", BFD_MACH_O_MH_DYLDLINK },
3396 { "bindatload", BFD_MACH_O_MH_BINDATLOAD },
3397 { "prebound", BFD_MACH_O_MH_PREBOUND },
3398 { "split_segs", BFD_MACH_O_MH_SPLIT_SEGS },
3399 { "lazy_init", BFD_MACH_O_MH_LAZY_INIT },
3400 { "twolevel", BFD_MACH_O_MH_TWOLEVEL },
3401 { "force_flat", BFD_MACH_O_MH_FORCE_FLAT },
3402 { "nomultidefs", BFD_MACH_O_MH_NOMULTIDEFS },
3403 { "nofixprebinding", BFD_MACH_O_MH_NOFIXPREBINDING },
3404 { "prebindable", BFD_MACH_O_MH_PREBINDABLE },
3405 { "allmodsbound", BFD_MACH_O_MH_ALLMODSBOUND },
3406 { "subsections_via_symbols", BFD_MACH_O_MH_SUBSECTIONS_VIA_SYMBOLS },
3407 { "canonical", BFD_MACH_O_MH_CANONICAL },
3408 { "weak_defines", BFD_MACH_O_MH_WEAK_DEFINES },
3409 { "binds_to_weak", BFD_MACH_O_MH_BINDS_TO_WEAK },
3410 { "allow_stack_execution", BFD_MACH_O_MH_ALLOW_STACK_EXECUTION },
3411 { "root_safe", BFD_MACH_O_MH_ROOT_SAFE },
3412 { "setuid_safe", BFD_MACH_O_MH_SETUID_SAFE },
3413 { "no_reexported_dylibs", BFD_MACH_O_MH_NO_REEXPORTED_DYLIBS },
3414 { "pie", BFD_MACH_O_MH_PIE },
3418 static bfd_mach_o_xlat_name bfd_mach_o_section_type_name[] =
3420 { "regular", BFD_MACH_O_S_REGULAR},
3421 { "zerofill", BFD_MACH_O_S_ZEROFILL},
3422 { "cstring_literals", BFD_MACH_O_S_CSTRING_LITERALS},
3423 { "4byte_literals", BFD_MACH_O_S_4BYTE_LITERALS},
3424 { "8byte_literals", BFD_MACH_O_S_8BYTE_LITERALS},
3425 { "literal_pointers", BFD_MACH_O_S_LITERAL_POINTERS},
3426 { "non_lazy_symbol_pointers", BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS},
3427 { "lazy_symbol_pointers", BFD_MACH_O_S_LAZY_SYMBOL_POINTERS},
3428 { "symbol_stubs", BFD_MACH_O_S_SYMBOL_STUBS},
3429 { "mod_init_func_pointers", BFD_MACH_O_S_MOD_INIT_FUNC_POINTERS},
3430 { "mod_fini_func_pointers", BFD_MACH_O_S_MOD_FINI_FUNC_POINTERS},
3431 { "coalesced", BFD_MACH_O_S_COALESCED},
3432 { "gb_zerofill", BFD_MACH_O_S_GB_ZEROFILL},
3433 { "interposing", BFD_MACH_O_S_INTERPOSING},
3434 { "16byte_literals", BFD_MACH_O_S_16BYTE_LITERALS},
3435 { "dtrace_dof", BFD_MACH_O_S_DTRACE_DOF},
3436 { "lazy_dylib_symbol_pointers", BFD_MACH_O_S_LAZY_DYLIB_SYMBOL_POINTERS},
3440 static bfd_mach_o_xlat_name bfd_mach_o_section_attribute_name[] =
3442 { "loc_reloc", BFD_MACH_O_S_ATTR_LOC_RELOC },
3443 { "ext_reloc", BFD_MACH_O_S_ATTR_EXT_RELOC },
3444 { "some_instructions", BFD_MACH_O_S_ATTR_SOME_INSTRUCTIONS },
3445 { "debug", BFD_MACH_O_S_ATTR_DEBUG },
3446 { "modifying_code", BFD_MACH_O_S_SELF_MODIFYING_CODE },
3447 { "live_support", BFD_MACH_O_S_ATTR_LIVE_SUPPORT },
3448 { "no_dead_strip", BFD_MACH_O_S_ATTR_NO_DEAD_STRIP },
3449 { "strip_static_syms", BFD_MACH_O_S_ATTR_STRIP_STATIC_SYMS },
3450 { "no_toc", BFD_MACH_O_S_ATTR_NO_TOC },
3451 { "pure_instructions", BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS },
3455 static bfd_mach_o_xlat_name bfd_mach_o_load_command_name[] =
3457 { "segment", BFD_MACH_O_LC_SEGMENT},
3458 { "symtab", BFD_MACH_O_LC_SYMTAB},
3459 { "symseg", BFD_MACH_O_LC_SYMSEG},
3460 { "thread", BFD_MACH_O_LC_THREAD},
3461 { "unixthread", BFD_MACH_O_LC_UNIXTHREAD},
3462 { "loadfvmlib", BFD_MACH_O_LC_LOADFVMLIB},
3463 { "idfvmlib", BFD_MACH_O_LC_IDFVMLIB},
3464 { "ident", BFD_MACH_O_LC_IDENT},
3465 { "fvmfile", BFD_MACH_O_LC_FVMFILE},
3466 { "prepage", BFD_MACH_O_LC_PREPAGE},
3467 { "dysymtab", BFD_MACH_O_LC_DYSYMTAB},
3468 { "load_dylib", BFD_MACH_O_LC_LOAD_DYLIB},
3469 { "id_dylib", BFD_MACH_O_LC_ID_DYLIB},
3470 { "load_dylinker", BFD_MACH_O_LC_LOAD_DYLINKER},
3471 { "id_dylinker", BFD_MACH_O_LC_ID_DYLINKER},
3472 { "prebound_dylib", BFD_MACH_O_LC_PREBOUND_DYLIB},
3473 { "routines", BFD_MACH_O_LC_ROUTINES},
3474 { "sub_framework", BFD_MACH_O_LC_SUB_FRAMEWORK},
3475 { "sub_umbrella", BFD_MACH_O_LC_SUB_UMBRELLA},
3476 { "sub_client", BFD_MACH_O_LC_SUB_CLIENT},
3477 { "sub_library", BFD_MACH_O_LC_SUB_LIBRARY},
3478 { "twolevel_hints", BFD_MACH_O_LC_TWOLEVEL_HINTS},
3479 { "prebind_cksum", BFD_MACH_O_LC_PREBIND_CKSUM},
3480 { "load_weak_dylib", BFD_MACH_O_LC_LOAD_WEAK_DYLIB},
3481 { "segment_64", BFD_MACH_O_LC_SEGMENT_64},
3482 { "routines_64", BFD_MACH_O_LC_ROUTINES_64},
3483 { "uuid", BFD_MACH_O_LC_UUID},
3484 { "rpath", BFD_MACH_O_LC_RPATH},
3485 { "code_signature", BFD_MACH_O_LC_CODE_SIGNATURE},
3486 { "segment_split_info", BFD_MACH_O_LC_SEGMENT_SPLIT_INFO},
3487 { "reexport_dylib", BFD_MACH_O_LC_REEXPORT_DYLIB},
3488 { "lazy_load_dylib", BFD_MACH_O_LC_LAZY_LOAD_DYLIB},
3489 { "encryption_info", BFD_MACH_O_LC_ENCRYPTION_INFO},
3490 { "dyld_info", BFD_MACH_O_LC_DYLD_INFO},
3491 { "load_upward_lib", BFD_MACH_O_LC_LOAD_UPWARD_DYLIB},
3492 { "version_min_macosx", BFD_MACH_O_LC_VERSION_MIN_MACOSX},
3493 { "version_min_iphoneos", BFD_MACH_O_LC_VERSION_MIN_IPHONEOS},
3494 { "function_starts", BFD_MACH_O_LC_FUNCTION_STARTS},
3495 { "dyld_environment", BFD_MACH_O_LC_DYLD_ENVIRONMENT},
3499 /* Get the section type from NAME. Return -1 if NAME is unknown. */
3502 bfd_mach_o_get_section_type_from_name (const char *name)
3504 bfd_mach_o_xlat_name *x;
3506 for (x = bfd_mach_o_section_type_name; x->name; x++)
3507 if (strcmp (x->name, name) == 0)
3509 return (unsigned int)-1;
3512 /* Get the section attribute from NAME. Return -1 if NAME is unknown. */
3515 bfd_mach_o_get_section_attribute_from_name (const char *name)
3517 bfd_mach_o_xlat_name *x;
3519 for (x = bfd_mach_o_section_attribute_name; x->name; x++)
3520 if (strcmp (x->name, name) == 0)
3522 return (unsigned int)-1;
3526 bfd_mach_o_print_private_header (bfd *abfd, FILE *file)
3528 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
3529 bfd_mach_o_header *h = &mdata->header;
3531 fputs (_("Mach-O header:\n"), file);
3532 fprintf (file, _(" magic : %08lx\n"), h->magic);
3533 fprintf (file, _(" cputype : %08lx (%s)\n"), h->cputype,
3534 bfd_mach_o_get_name (bfd_mach_o_cpu_name, h->cputype));
3535 fprintf (file, _(" cpusubtype: %08lx\n"), h->cpusubtype);
3536 fprintf (file, _(" filetype : %08lx (%s)\n"),
3538 bfd_mach_o_get_name (bfd_mach_o_filetype_name, h->filetype));
3539 fprintf (file, _(" ncmds : %08lx (%lu)\n"), h->ncmds, h->ncmds);
3540 fprintf (file, _(" sizeofcmds: %08lx\n"), h->sizeofcmds);
3541 fprintf (file, _(" flags : %08lx ("), h->flags);
3542 bfd_mach_o_print_flags (bfd_mach_o_header_flags_name, h->flags, file);
3543 fputs (_(")\n"), file);
3544 fprintf (file, _(" reserved : %08x\n"), h->reserved);
3548 bfd_mach_o_print_section_map (bfd *abfd, FILE *file)
3550 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
3552 unsigned int sec_nbr = 0;
3554 fputs (_("Segments and Sections:\n"), file);
3555 fputs (_(" #: Segment name Section name Address\n"), file);
3557 for (i = 0; i < mdata->header.ncmds; i++)
3559 bfd_mach_o_segment_command *seg;
3561 if (mdata->commands[i].type != BFD_MACH_O_LC_SEGMENT
3562 && mdata->commands[i].type != BFD_MACH_O_LC_SEGMENT_64)
3565 seg = &mdata->commands[i].command.segment;
3567 fprintf (file, "[Segment %-16s ", seg->segname);
3568 fprintf_vma (file, seg->vmaddr);
3569 fprintf (file, "-");
3570 fprintf_vma (file, seg->vmaddr + seg->vmsize - 1);
3572 fputc (seg->initprot & BFD_MACH_O_PROT_READ ? 'r' : '-', file);
3573 fputc (seg->initprot & BFD_MACH_O_PROT_WRITE ? 'w' : '-', file);
3574 fputc (seg->initprot & BFD_MACH_O_PROT_EXECUTE ? 'x' : '-', file);
3575 fprintf (file, "]\n");
3576 for (j = 0; j < seg->nsects; j++)
3578 bfd_mach_o_section *sec = &seg->sections[j];
3579 fprintf (file, "%02u: %-16s %-16s ", ++sec_nbr,
3580 sec->segname, sec->sectname);
3581 fprintf_vma (file, sec->addr);
3582 fprintf (file, " ");
3583 fprintf_vma (file, sec->size);
3584 fprintf (file, " %08lx\n", sec->flags);
3590 bfd_mach_o_print_section (bfd *abfd ATTRIBUTE_UNUSED,
3591 bfd_mach_o_section *sec, FILE *file)
3593 fprintf (file, " Section: %-16s %-16s (bfdname: %s)\n",
3594 sec->sectname, sec->segname, sec->bfdsection->name);
3595 fprintf (file, " addr: ");
3596 fprintf_vma (file, sec->addr);
3597 fprintf (file, " size: ");
3598 fprintf_vma (file, sec->size);
3599 fprintf (file, " offset: ");
3600 fprintf_vma (file, sec->offset);
3601 fprintf (file, "\n");
3602 fprintf (file, " align: %ld", sec->align);
3603 fprintf (file, " nreloc: %lu reloff: ", sec->nreloc);
3604 fprintf_vma (file, sec->reloff);
3605 fprintf (file, "\n");
3606 fprintf (file, " flags: %08lx (type: %s", sec->flags,
3607 bfd_mach_o_get_name (bfd_mach_o_section_type_name,
3608 sec->flags & BFD_MACH_O_SECTION_TYPE_MASK));
3609 fprintf (file, " attr: ");
3610 bfd_mach_o_print_flags (bfd_mach_o_section_attribute_name,
3611 sec->flags & BFD_MACH_O_SECTION_ATTRIBUTES_MASK,
3613 fprintf (file, ")\n");
3614 switch (sec->flags & BFD_MACH_O_SECTION_TYPE_MASK)
3616 case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS:
3617 case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS:
3618 case BFD_MACH_O_S_SYMBOL_STUBS:
3619 fprintf (file, " first indirect sym: %lu", sec->reserved1);
3620 fprintf (file, " (%u entries)",
3621 bfd_mach_o_section_get_nbr_indirect (abfd, sec));
3624 fprintf (file, " reserved1: 0x%lx", sec->reserved1);
3627 switch (sec->flags & BFD_MACH_O_SECTION_TYPE_MASK)
3629 case BFD_MACH_O_S_SYMBOL_STUBS:
3630 fprintf (file, " stub size: %lu", sec->reserved2);
3633 fprintf (file, " reserved2: 0x%lx", sec->reserved2);
3636 fprintf (file, " reserved3: 0x%lx\n", sec->reserved3);
3640 bfd_mach_o_print_segment (bfd *abfd ATTRIBUTE_UNUSED,
3641 bfd_mach_o_load_command *cmd, FILE *file)
3643 bfd_mach_o_segment_command *seg = &cmd->command.segment;
3646 fprintf (file, " name: %s\n", *seg->segname ? seg->segname : "*none*");
3647 fprintf (file, " vmaddr: ");
3648 fprintf_vma (file, seg->vmaddr);
3649 fprintf (file, " vmsize: ");
3650 fprintf_vma (file, seg->vmsize);
3651 fprintf (file, "\n");
3652 fprintf (file, " fileoff: ");
3653 fprintf_vma (file, seg->fileoff);
3654 fprintf (file, " filesize: ");
3655 fprintf_vma (file, (bfd_vma)seg->filesize);
3656 fprintf (file, " endoff: ");
3657 fprintf_vma (file, (bfd_vma)(seg->fileoff + seg->filesize));
3658 fprintf (file, "\n");
3659 fprintf (file, " nsects: %lu ", seg->nsects);
3660 fprintf (file, " flags: %lx\n", seg->flags);
3661 for (i = 0; i < seg->nsects; i++)
3662 bfd_mach_o_print_section (abfd, &seg->sections[i], file);
3666 bfd_mach_o_print_dysymtab (bfd *abfd ATTRIBUTE_UNUSED,
3667 bfd_mach_o_load_command *cmd, FILE *file)
3669 bfd_mach_o_dysymtab_command *dysymtab = &cmd->command.dysymtab;
3670 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
3673 fprintf (file, " local symbols: idx: %10lu num: %-8lu",
3674 dysymtab->ilocalsym, dysymtab->nlocalsym);
3675 fprintf (file, " (nxtidx: %lu)\n",
3676 dysymtab->ilocalsym + dysymtab->nlocalsym);
3677 fprintf (file, " external symbols: idx: %10lu num: %-8lu",
3678 dysymtab->iextdefsym, dysymtab->nextdefsym);
3679 fprintf (file, " (nxtidx: %lu)\n",
3680 dysymtab->iextdefsym + dysymtab->nextdefsym);
3681 fprintf (file, " undefined symbols: idx: %10lu num: %-8lu",
3682 dysymtab->iundefsym, dysymtab->nundefsym);
3683 fprintf (file, " (nxtidx: %lu)\n",
3684 dysymtab->iundefsym + dysymtab->nundefsym);
3685 fprintf (file, " table of content: off: 0x%08lx num: %-8lu",
3686 dysymtab->tocoff, dysymtab->ntoc);
3687 fprintf (file, " (endoff: 0x%08lx)\n",
3689 + dysymtab->ntoc * BFD_MACH_O_TABLE_OF_CONTENT_SIZE);
3690 fprintf (file, " module table: off: 0x%08lx num: %-8lu",
3691 dysymtab->modtaboff, dysymtab->nmodtab);
3692 fprintf (file, " (endoff: 0x%08lx)\n",
3693 dysymtab->modtaboff + dysymtab->nmodtab
3694 * (mach_o_wide_p (&mdata->header) ?
3695 BFD_MACH_O_DYLIB_MODULE_64_SIZE : BFD_MACH_O_DYLIB_MODULE_SIZE));
3696 fprintf (file, " external reference table: off: 0x%08lx num: %-8lu",
3697 dysymtab->extrefsymoff, dysymtab->nextrefsyms);
3698 fprintf (file, " (endoff: 0x%08lx)\n",
3699 dysymtab->extrefsymoff
3700 + dysymtab->nextrefsyms * BFD_MACH_O_REFERENCE_SIZE);
3701 fprintf (file, " indirect symbol table: off: 0x%08lx num: %-8lu",
3702 dysymtab->indirectsymoff, dysymtab->nindirectsyms);
3703 fprintf (file, " (endoff: 0x%08lx)\n",
3704 dysymtab->indirectsymoff
3705 + dysymtab->nindirectsyms * BFD_MACH_O_INDIRECT_SYMBOL_SIZE);
3706 fprintf (file, " external relocation table: off: 0x%08lx num: %-8lu",
3707 dysymtab->extreloff, dysymtab->nextrel);
3708 fprintf (file, " (endoff: 0x%08lx)\n",
3709 dysymtab->extreloff + dysymtab->nextrel * BFD_MACH_O_RELENT_SIZE);
3710 fprintf (file, " local relocation table: off: 0x%08lx num: %-8lu",
3711 dysymtab->locreloff, dysymtab->nlocrel);
3712 fprintf (file, " (endoff: 0x%08lx)\n",
3713 dysymtab->locreloff + dysymtab->nlocrel * BFD_MACH_O_RELENT_SIZE);
3715 if (dysymtab->ntoc > 0
3716 || dysymtab->nindirectsyms > 0
3717 || dysymtab->nextrefsyms > 0)
3719 /* Try to read the symbols to display the toc or indirect symbols. */
3720 bfd_mach_o_read_symtab_symbols (abfd);
3722 else if (dysymtab->nmodtab > 0)
3724 /* Try to read the strtab to display modules name. */
3725 bfd_mach_o_read_symtab_strtab (abfd);
3728 for (i = 0; i < dysymtab->nmodtab; i++)
3730 bfd_mach_o_dylib_module *module = &dysymtab->dylib_module[i];
3731 fprintf (file, " module %u:\n", i);
3732 fprintf (file, " name: %lu", module->module_name_idx);
3733 if (mdata->symtab && mdata->symtab->strtab)
3734 fprintf (file, ": %s",
3735 mdata->symtab->strtab + module->module_name_idx);
3736 fprintf (file, "\n");
3737 fprintf (file, " extdefsym: idx: %8lu num: %lu\n",
3738 module->iextdefsym, module->nextdefsym);
3739 fprintf (file, " refsym: idx: %8lu num: %lu\n",
3740 module->irefsym, module->nrefsym);
3741 fprintf (file, " localsym: idx: %8lu num: %lu\n",
3742 module->ilocalsym, module->nlocalsym);
3743 fprintf (file, " extrel: idx: %8lu num: %lu\n",
3744 module->iextrel, module->nextrel);
3745 fprintf (file, " init: idx: %8u num: %u\n",
3746 module->iinit, module->ninit);
3747 fprintf (file, " term: idx: %8u num: %u\n",
3748 module->iterm, module->nterm);
3749 fprintf (file, " objc_module_info: addr: ");
3750 fprintf_vma (file, module->objc_module_info_addr);
3751 fprintf (file, " size: %lu\n", module->objc_module_info_size);
3754 if (dysymtab->ntoc > 0)
3756 bfd_mach_o_symtab_command *symtab = mdata->symtab;
3758 fprintf (file, " table of content: (symbol/module)\n");
3759 for (i = 0; i < dysymtab->ntoc; i++)
3761 bfd_mach_o_dylib_table_of_content *toc = &dysymtab->dylib_toc[i];
3763 fprintf (file, " %4u: ", i);
3764 if (symtab && symtab->symbols && toc->symbol_index < symtab->nsyms)
3766 const char *name = symtab->symbols[toc->symbol_index].symbol.name;
3767 fprintf (file, "%s (%lu)", name ? name : "*invalid*",
3771 fprintf (file, "%lu", toc->symbol_index);
3773 fprintf (file, " / ");
3774 if (symtab && symtab->strtab
3775 && toc->module_index < dysymtab->nmodtab)
3777 bfd_mach_o_dylib_module *mod;
3778 mod = &dysymtab->dylib_module[toc->module_index];
3779 fprintf (file, "%s (%lu)",
3780 symtab->strtab + mod->module_name_idx,
3784 fprintf (file, "%lu", toc->module_index);
3786 fprintf (file, "\n");
3790 if (dysymtab->nindirectsyms != 0)
3792 fprintf (file, " indirect symbols:\n");
3794 for (i = 0; i < mdata->nsects; i++)
3796 bfd_mach_o_section *sec = mdata->sections[i];
3797 unsigned int j, first, last;
3798 bfd_mach_o_symtab_command *symtab = mdata->symtab;
3802 switch (sec->flags & BFD_MACH_O_SECTION_TYPE_MASK)
3804 case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS:
3805 case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS:
3806 case BFD_MACH_O_S_SYMBOL_STUBS:
3807 first = sec->reserved1;
3808 last = first + bfd_mach_o_section_get_nbr_indirect (abfd, sec);
3810 entry_size = bfd_mach_o_section_get_entry_size (abfd, sec);
3811 fprintf (file, " for section %s.%s:\n",
3812 sec->segname, sec->sectname);
3813 for (j = first; j < last; j++)
3815 unsigned int isym = dysymtab->indirect_syms[j];
3817 fprintf (file, " ");
3818 fprintf_vma (file, addr);
3819 fprintf (file, " %5u: 0x%08x", j, isym);
3820 if (isym & BFD_MACH_O_INDIRECT_SYMBOL_LOCAL)
3821 fprintf (file, " LOCAL");
3822 if (isym & BFD_MACH_O_INDIRECT_SYMBOL_ABS)
3823 fprintf (file, " ABSOLUTE");
3824 if (symtab && symtab->symbols
3825 && isym < symtab->nsyms
3826 && symtab->symbols[isym].symbol.name)
3827 fprintf (file, " %s", symtab->symbols[isym].symbol.name);
3828 fprintf (file, "\n");
3837 if (dysymtab->nextrefsyms > 0)
3839 bfd_mach_o_symtab_command *symtab = mdata->symtab;
3841 fprintf (file, " external reference table: (symbol flags)\n");
3842 for (i = 0; i < dysymtab->nextrefsyms; i++)
3844 bfd_mach_o_dylib_reference *ref = &dysymtab->ext_refs[i];
3846 fprintf (file, " %4u: %5lu 0x%02lx", i, ref->isym, ref->flags);
3847 if (symtab && symtab->symbols
3848 && ref->isym < symtab->nsyms
3849 && symtab->symbols[ref->isym].symbol.name)
3850 fprintf (file, " %s", symtab->symbols[ref->isym].symbol.name);
3851 fprintf (file, "\n");
3858 bfd_mach_o_print_dyld_info (bfd *abfd ATTRIBUTE_UNUSED,
3859 bfd_mach_o_load_command *cmd, FILE *file)
3861 bfd_mach_o_dyld_info_command *info = &cmd->command.dyld_info;
3863 fprintf (file, " rebase: off: 0x%08x size: %-8u\n",
3864 info->rebase_off, info->rebase_size);
3865 fprintf (file, " bind: off: 0x%08x size: %-8u\n",
3866 info->bind_off, info->bind_size);
3867 fprintf (file, " weak bind: off: 0x%08x size: %-8u\n",
3868 info->weak_bind_off, info->weak_bind_size);
3869 fprintf (file, " lazy bind: off: 0x%08x size: %-8u\n",
3870 info->lazy_bind_off, info->lazy_bind_size);
3871 fprintf (file, " export: off: 0x%08x size: %-8u\n",
3872 info->export_off, info->export_size);
3876 bfd_mach_o_bfd_print_private_bfd_data (bfd *abfd, void * ptr)
3878 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
3879 FILE *file = (FILE *) ptr;
3882 bfd_mach_o_print_private_header (abfd, file);
3885 for (i = 0; i < mdata->header.ncmds; i++)
3887 bfd_mach_o_load_command *cmd = &mdata->commands[i];
3888 const char *cmd_name;
3890 cmd_name = bfd_mach_o_get_name_or_null
3891 (bfd_mach_o_load_command_name, cmd->type);
3892 fprintf (file, "Load command ");
3893 if (cmd_name == NULL)
3894 fprintf (file, "0x%02x:", cmd->type);
3896 fprintf (file, "%s:", cmd_name);
3900 case BFD_MACH_O_LC_SEGMENT:
3901 case BFD_MACH_O_LC_SEGMENT_64:
3902 bfd_mach_o_print_segment (abfd, cmd, file);
3904 case BFD_MACH_O_LC_UUID:
3906 bfd_mach_o_uuid_command *uuid = &cmd->command.uuid;
3909 for (j = 0; j < sizeof (uuid->uuid); j ++)
3910 fprintf (file, " %02x", uuid->uuid[j]);
3914 case BFD_MACH_O_LC_LOAD_DYLIB:
3915 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB:
3916 case BFD_MACH_O_LC_REEXPORT_DYLIB:
3917 case BFD_MACH_O_LC_ID_DYLIB:
3919 bfd_mach_o_dylib_command *dylib = &cmd->command.dylib;
3920 fprintf (file, " %s\n", dylib->name_str);
3921 fprintf (file, " time stamp: 0x%08lx\n",
3923 fprintf (file, " current version: 0x%08lx\n",
3924 dylib->current_version);
3925 fprintf (file, " comptibility version: 0x%08lx\n",
3926 dylib->compatibility_version);
3929 case BFD_MACH_O_LC_LOAD_DYLINKER:
3930 case BFD_MACH_O_LC_ID_DYLINKER:
3931 fprintf (file, " %s\n", cmd->command.dylinker.name_str);
3933 case BFD_MACH_O_LC_SYMTAB:
3935 bfd_mach_o_symtab_command *symtab = &cmd->command.symtab;
3938 " symoff: 0x%08x nsyms: %8u (endoff: 0x%08x)\n",
3939 symtab->symoff, symtab->nsyms,
3940 symtab->symoff + symtab->nsyms
3941 * (mach_o_wide_p (&mdata->header)
3942 ? BFD_MACH_O_NLIST_64_SIZE : BFD_MACH_O_NLIST_SIZE));
3944 " stroff: 0x%08x strsize: %8u (endoff: 0x%08x)\n",
3945 symtab->stroff, symtab->strsize,
3946 symtab->stroff + symtab->strsize);
3949 case BFD_MACH_O_LC_DYSYMTAB:
3950 fprintf (file, "\n");
3951 bfd_mach_o_print_dysymtab (abfd, cmd, file);
3953 case BFD_MACH_O_LC_CODE_SIGNATURE:
3954 case BFD_MACH_O_LC_SEGMENT_SPLIT_INFO:
3955 case BFD_MACH_O_LC_FUNCTION_STARTS:
3957 bfd_mach_o_linkedit_command *linkedit = &cmd->command.linkedit;
3960 " dataoff: 0x%08lx datasize: 0x%08lx (endoff: 0x%08lx)\n",
3961 linkedit->dataoff, linkedit->datasize,
3962 linkedit->dataoff + linkedit->datasize);
3965 case BFD_MACH_O_LC_SUB_FRAMEWORK:
3966 case BFD_MACH_O_LC_SUB_UMBRELLA:
3967 case BFD_MACH_O_LC_SUB_LIBRARY:
3968 case BFD_MACH_O_LC_SUB_CLIENT:
3969 case BFD_MACH_O_LC_RPATH:
3971 bfd_mach_o_str_command *str = &cmd->command.str;
3972 fprintf (file, " %s\n", str->str);
3975 case BFD_MACH_O_LC_THREAD:
3976 case BFD_MACH_O_LC_UNIXTHREAD:
3978 bfd_mach_o_thread_command *thread = &cmd->command.thread;
3980 bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
3982 fprintf (file, " nflavours: %lu\n", thread->nflavours);
3983 for (j = 0; j < thread->nflavours; j++)
3985 bfd_mach_o_thread_flavour *flavour = &thread->flavours[j];
3987 fprintf (file, " %2u: flavour: 0x%08lx offset: 0x%08lx"
3989 j, flavour->flavour, flavour->offset,
3991 if (bed->_bfd_mach_o_print_thread)
3993 char *buf = bfd_malloc (flavour->size);
3996 && bfd_seek (abfd, flavour->offset, SEEK_SET) == 0
3997 && (bfd_bread (buf, flavour->size, abfd)
3999 (*bed->_bfd_mach_o_print_thread)(abfd, flavour,
4006 case BFD_MACH_O_LC_DYLD_INFO:
4007 fprintf (file, "\n");
4008 bfd_mach_o_print_dyld_info (abfd, cmd, file);
4010 case BFD_MACH_O_LC_VERSION_MIN_MACOSX:
4011 case BFD_MACH_O_LC_VERSION_MIN_IPHONEOS:
4013 bfd_mach_o_version_min_command *ver = &cmd->command.version_min;
4015 fprintf (file, " %u.%u.%u\n", ver->rel, ver->maj, ver->min);
4019 fprintf (file, "\n");
4020 fprintf (file, " offset: 0x%08lx\n", (unsigned long)cmd->offset);
4021 fprintf (file, " size: 0x%08lx\n", (unsigned long)cmd->len);
4027 bfd_mach_o_print_section_map (abfd, file);
4033 bfd_mach_o_core_fetch_environment (bfd *abfd,
4034 unsigned char **rbuf,
4037 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
4038 unsigned long stackaddr = bfd_mach_o_stack_addr (mdata->header.cputype);
4041 for (i = 0; i < mdata->header.ncmds; i++)
4043 bfd_mach_o_load_command *cur = &mdata->commands[i];
4044 bfd_mach_o_segment_command *seg = NULL;
4046 if (cur->type != BFD_MACH_O_LC_SEGMENT)
4049 seg = &cur->command.segment;
4051 if ((seg->vmaddr + seg->vmsize) == stackaddr)
4053 unsigned long start = seg->fileoff;
4054 unsigned long end = seg->fileoff + seg->filesize;
4055 unsigned char *buf = bfd_malloc (1024);
4056 unsigned long size = 1024;
4060 bfd_size_type nread = 0;
4061 unsigned long offset;
4062 int found_nonnull = 0;
4064 if (size > (end - start))
4065 size = (end - start);
4067 buf = bfd_realloc_or_free (buf, size);
4071 if (bfd_seek (abfd, end - size, SEEK_SET) != 0)
4077 nread = bfd_bread (buf, size, abfd);
4085 for (offset = 4; offset <= size; offset += 4)
4089 val = *((unsigned long *) (buf + size - offset));
4090 if (! found_nonnull)
4095 else if (val == 0x0)
4097 unsigned long bottom;
4100 bottom = seg->fileoff + seg->filesize - offset;
4101 top = seg->fileoff + seg->filesize - 4;
4102 *rbuf = bfd_malloc (top - bottom);
4103 *rlen = top - bottom;
4105 memcpy (*rbuf, buf + size - *rlen, *rlen);
4111 if (size == (end - start))
4125 bfd_mach_o_core_file_failing_command (bfd *abfd)
4127 unsigned char *buf = NULL;
4128 unsigned int len = 0;
4131 ret = bfd_mach_o_core_fetch_environment (abfd, &buf, &len);
4135 return (char *) buf;
4139 bfd_mach_o_core_file_failing_signal (bfd *abfd ATTRIBUTE_UNUSED)
4144 #define bfd_mach_o_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
4145 #define bfd_mach_o_bfd_reloc_name_lookup _bfd_norelocs_bfd_reloc_name_lookup
4147 #define bfd_mach_o_swap_reloc_in NULL
4148 #define bfd_mach_o_swap_reloc_out NULL
4149 #define bfd_mach_o_print_thread NULL
4151 #define TARGET_NAME mach_o_be_vec
4152 #define TARGET_STRING "mach-o-be"
4153 #define TARGET_ARCHITECTURE bfd_arch_unknown
4154 #define TARGET_BIG_ENDIAN 1
4155 #define TARGET_ARCHIVE 0
4156 #include "mach-o-target.c"
4159 #undef TARGET_STRING
4160 #undef TARGET_ARCHITECTURE
4161 #undef TARGET_BIG_ENDIAN
4162 #undef TARGET_ARCHIVE
4164 #define TARGET_NAME mach_o_le_vec
4165 #define TARGET_STRING "mach-o-le"
4166 #define TARGET_ARCHITECTURE bfd_arch_unknown
4167 #define TARGET_BIG_ENDIAN 0
4168 #define TARGET_ARCHIVE 0
4170 #include "mach-o-target.c"
4173 #undef TARGET_STRING
4174 #undef TARGET_ARCHITECTURE
4175 #undef TARGET_BIG_ENDIAN
4176 #undef TARGET_ARCHIVE
4178 /* Not yet handled: creating an archive. */
4179 #define bfd_mach_o_mkarchive _bfd_noarchive_mkarchive
4182 #define bfd_mach_o_read_ar_hdr _bfd_noarchive_read_ar_hdr
4183 #define bfd_mach_o_write_ar_hdr _bfd_noarchive_write_ar_hdr
4184 #define bfd_mach_o_slurp_armap _bfd_noarchive_slurp_armap
4185 #define bfd_mach_o_slurp_extended_name_table _bfd_noarchive_slurp_extended_name_table
4186 #define bfd_mach_o_construct_extended_name_table _bfd_noarchive_construct_extended_name_table
4187 #define bfd_mach_o_truncate_arname _bfd_noarchive_truncate_arname
4188 #define bfd_mach_o_write_armap _bfd_noarchive_write_armap
4189 #define bfd_mach_o_get_elt_at_index _bfd_noarchive_get_elt_at_index
4190 #define bfd_mach_o_generic_stat_arch_elt _bfd_noarchive_generic_stat_arch_elt
4191 #define bfd_mach_o_update_armap_timestamp _bfd_noarchive_update_armap_timestamp
4193 #define TARGET_NAME mach_o_fat_vec
4194 #define TARGET_STRING "mach-o-fat"
4195 #define TARGET_ARCHITECTURE bfd_arch_unknown
4196 #define TARGET_BIG_ENDIAN 1
4197 #define TARGET_ARCHIVE 1
4199 #include "mach-o-target.c"
4202 #undef TARGET_STRING
4203 #undef TARGET_ARCHITECTURE
4204 #undef TARGET_BIG_ENDIAN
4205 #undef TARGET_ARCHIVE