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)))
41 bfd_mach_o_version (bfd *abfd)
43 bfd_mach_o_data_struct *mdata = NULL;
45 BFD_ASSERT (bfd_mach_o_valid (abfd));
46 mdata = bfd_mach_o_get_data (abfd);
48 return mdata->header.version;
52 bfd_mach_o_valid (bfd *abfd)
54 if (abfd == NULL || abfd->xvec == NULL)
57 if (abfd->xvec->flavour != bfd_target_mach_o_flavour)
60 if (bfd_mach_o_get_data (abfd) == NULL)
65 static INLINE bfd_boolean
66 mach_o_wide_p (bfd_mach_o_header *header)
68 switch (header->version)
80 static INLINE bfd_boolean
81 bfd_mach_o_wide_p (bfd *abfd)
83 return mach_o_wide_p (&bfd_mach_o_get_data (abfd)->header);
86 /* Tables to translate well known Mach-O segment/section names to bfd
87 names. Use of canonical names (such as .text or .debug_frame) is required
90 struct mach_o_section_name_xlat
93 const char *mach_o_name;
97 static const struct mach_o_section_name_xlat dwarf_section_names_xlat[] =
99 { ".debug_frame", "__debug_frame", SEC_DEBUGGING },
100 { ".debug_info", "__debug_info", SEC_DEBUGGING },
101 { ".debug_abbrev", "__debug_abbrev", SEC_DEBUGGING },
102 { ".debug_aranges", "__debug_aranges", SEC_DEBUGGING },
103 { ".debug_macinfo", "__debug_macinfo", SEC_DEBUGGING },
104 { ".debug_line", "__debug_line", SEC_DEBUGGING },
105 { ".debug_loc", "__debug_loc", SEC_DEBUGGING },
106 { ".debug_pubnames", "__debug_pubnames", SEC_DEBUGGING },
107 { ".debug_pubtypes", "__debug_pubtypes", SEC_DEBUGGING },
108 { ".debug_str", "__debug_str", SEC_DEBUGGING },
109 { ".debug_ranges", "__debug_ranges", SEC_DEBUGGING },
113 static const struct mach_o_section_name_xlat text_section_names_xlat[] =
115 { ".text", "__text", SEC_CODE | SEC_LOAD },
116 { ".const", "__const", SEC_READONLY | SEC_DATA | SEC_LOAD },
117 { ".cstring", "__cstring", SEC_READONLY | SEC_DATA | SEC_LOAD },
118 { ".eh_frame", "__eh_frame", SEC_READONLY | SEC_LOAD },
122 static const struct mach_o_section_name_xlat data_section_names_xlat[] =
124 { ".data", "__data", SEC_DATA | SEC_LOAD },
125 { ".const_data", "__const", SEC_DATA | SEC_LOAD },
126 { ".dyld", "__dyld", SEC_DATA | SEC_LOAD },
127 { ".lazy_symbol_ptr", "__la_symbol_ptr", SEC_DATA | SEC_LOAD },
128 { ".non_lazy_symbol_ptr", "__nl_symbol_ptr", SEC_DATA | SEC_LOAD },
129 { ".bss", "__bss", SEC_NO_FLAGS },
133 struct mach_o_segment_name_xlat
138 /* List of known sections for the segment. */
139 const struct mach_o_section_name_xlat *sections;
142 /* List of known segment names. */
144 static const struct mach_o_segment_name_xlat segsec_names_xlat[] =
146 { "__TEXT", text_section_names_xlat },
147 { "__DATA", data_section_names_xlat },
148 { "__DWARF", dwarf_section_names_xlat },
152 /* Mach-O to bfd names. */
155 bfd_mach_o_normalize_section_name (const char *segname, const char *sectname,
156 const char **name, flagword *flags)
158 const struct mach_o_segment_name_xlat *seg;
161 *flags = SEC_NO_FLAGS;
163 for (seg = segsec_names_xlat; seg->segname; seg++)
165 if (strncmp (seg->segname, segname, BFD_MACH_O_SEGNAME_SIZE) == 0)
167 const struct mach_o_section_name_xlat *sec;
169 for (sec = seg->sections; sec->mach_o_name; sec++)
171 if (strncmp (sec->mach_o_name, sectname,
172 BFD_MACH_O_SECTNAME_SIZE) == 0)
174 *name = sec->bfd_name;
184 /* Convert Mach-O section name to BFD. Try to use standard names, otherwise
185 forge a new name. SEGNAME and SECTNAME are 16 bytes strings. */
188 bfd_mach_o_convert_section_name_to_bfd
189 (bfd *abfd, const char *segname, const char *sectname,
190 const char **name, flagword *flags)
194 const char *pfx = "";
196 /* First search for a canonical name. */
197 bfd_mach_o_normalize_section_name (segname, sectname, name, flags);
199 /* Return now if found. */
203 len = 16 + 1 + 16 + 1;
205 /* Put "LC_SEGMENT." prefix if the segment name is weird (ie doesn't start
206 with an underscore. */
207 if (segname[0] != '_')
209 static const char seg_pfx[] = "LC_SEGMENT.";
212 len += sizeof (seg_pfx) - 1;
215 res = bfd_alloc (abfd, len);
218 snprintf (res, len, "%s%.16s.%.16s", pfx, segname, sectname);
220 *flags = SEC_NO_FLAGS;
223 /* Convert a bfd section name to a Mach-O segment + section name. */
226 bfd_mach_o_convert_section_name_to_mach_o (bfd *abfd ATTRIBUTE_UNUSED,
228 bfd_mach_o_section *section)
230 const struct mach_o_segment_name_xlat *seg;
231 const char *name = bfd_get_section_name (abfd, sect);
237 /* List of well known names. They all start with a dot. */
239 for (seg = segsec_names_xlat; seg->segname; seg++)
241 const struct mach_o_section_name_xlat *sec;
243 for (sec = seg->sections; sec->mach_o_name; sec++)
245 if (strcmp (sec->bfd_name, name) == 0)
247 strcpy (section->segname, seg->segname);
248 strcpy (section->sectname, sec->mach_o_name);
254 /* Strip LC_SEGMENT. prefix. */
255 if (strncmp (name, "LC_SEGMENT.", 11) == 0)
259 dot = strchr (name, '.');
262 /* Try to split name into segment and section names. */
263 if (dot && dot != name)
266 seclen = len - (dot + 1 - name);
268 if (seglen < 16 && seclen < 16)
270 memcpy (section->segname, name, seglen);
271 section->segname[seglen] = 0;
272 memcpy (section->sectname, dot + 1, seclen);
273 section->sectname[seclen] = 0;
280 memcpy (section->segname, name, len);
281 section->segname[len] = 0;
282 memcpy (section->sectname, name, len);
283 section->sectname[len] = 0;
286 /* Return the size of an entry for section SEC.
287 Must be called only for symbol pointer section and symbol stubs
291 bfd_mach_o_section_get_entry_size (bfd *abfd, bfd_mach_o_section *sec)
293 switch (sec->flags & BFD_MACH_O_SECTION_TYPE_MASK)
295 case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS:
296 case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS:
297 return bfd_mach_o_wide_p (abfd) ? 8 : 4;
298 case BFD_MACH_O_S_SYMBOL_STUBS:
299 return sec->reserved2;
306 /* Return the number of indirect symbols for a section.
307 Must be called only for symbol pointer section and symbol stubs
311 bfd_mach_o_section_get_nbr_indirect (bfd *abfd, bfd_mach_o_section *sec)
315 elsz = bfd_mach_o_section_get_entry_size (abfd, sec);
319 return sec->size / elsz;
323 /* Copy any private info we understand from the input symbol
324 to the output symbol. */
327 bfd_mach_o_bfd_copy_private_symbol_data (bfd *ibfd ATTRIBUTE_UNUSED,
328 asymbol *isymbol ATTRIBUTE_UNUSED,
329 bfd *obfd ATTRIBUTE_UNUSED,
330 asymbol *osymbol ATTRIBUTE_UNUSED)
335 /* Copy any private info we understand from the input section
336 to the output section. */
339 bfd_mach_o_bfd_copy_private_section_data (bfd *ibfd ATTRIBUTE_UNUSED,
340 asection *isection ATTRIBUTE_UNUSED,
341 bfd *obfd ATTRIBUTE_UNUSED,
342 asection *osection ATTRIBUTE_UNUSED)
347 /* Copy any private info we understand from the input bfd
348 to the output bfd. */
351 bfd_mach_o_bfd_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
353 if (bfd_get_flavour (ibfd) != bfd_target_mach_o_flavour
354 || bfd_get_flavour (obfd) != bfd_target_mach_o_flavour)
357 BFD_ASSERT (bfd_mach_o_valid (ibfd));
358 BFD_ASSERT (bfd_mach_o_valid (obfd));
360 /* FIXME: copy commands. */
365 /* Count the total number of symbols. */
368 bfd_mach_o_count_symbols (bfd *abfd)
370 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
372 if (mdata->symtab == NULL)
374 return mdata->symtab->nsyms;
378 bfd_mach_o_get_symtab_upper_bound (bfd *abfd)
380 long nsyms = bfd_mach_o_count_symbols (abfd);
382 return ((nsyms + 1) * sizeof (asymbol *));
386 bfd_mach_o_canonicalize_symtab (bfd *abfd, asymbol **alocation)
388 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
389 long nsyms = bfd_mach_o_count_symbols (abfd);
390 bfd_mach_o_symtab_command *sym = mdata->symtab;
398 /* Do not try to read symbols if there are none. */
403 if (!bfd_mach_o_read_symtab_symbols (abfd))
405 (*_bfd_error_handler)
406 (_("bfd_mach_o_canonicalize_symtab: unable to load symbols"));
410 BFD_ASSERT (sym->symbols != NULL);
412 for (j = 0; j < sym->nsyms; j++)
413 alocation[j] = &sym->symbols[j].symbol;
421 bfd_mach_o_get_synthetic_symtab (bfd *abfd,
422 long symcount ATTRIBUTE_UNUSED,
423 asymbol **syms ATTRIBUTE_UNUSED,
424 long dynsymcount ATTRIBUTE_UNUSED,
425 asymbol **dynsyms ATTRIBUTE_UNUSED,
428 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
429 bfd_mach_o_dysymtab_command *dysymtab = mdata->dysymtab;
430 bfd_mach_o_symtab_command *symtab = mdata->symtab;
432 unsigned long count, i, j, n;
439 if (dysymtab == NULL || symtab == NULL || symtab->symbols == NULL)
442 if (dysymtab->nindirectsyms == 0)
445 count = dysymtab->nindirectsyms;
446 size = count * sizeof (asymbol) + 1;
448 for (j = 0; j < count; j++)
450 unsigned int isym = dysymtab->indirect_syms[j];
452 if (isym < symtab->nsyms && symtab->symbols[isym].symbol.name)
453 size += strlen (symtab->symbols[isym].symbol.name) + sizeof ("$stub");
456 s = *ret = (asymbol *) bfd_malloc (size);
459 names = (char *) (s + count);
464 for (i = 0; i < mdata->nsects; i++)
466 bfd_mach_o_section *sec = mdata->sections[i];
467 unsigned int first, last;
471 switch (sec->flags & BFD_MACH_O_SECTION_TYPE_MASK)
473 case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS:
474 case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS:
475 case BFD_MACH_O_S_SYMBOL_STUBS:
476 first = sec->reserved1;
477 last = first + bfd_mach_o_section_get_nbr_indirect (abfd, sec);
479 entry_size = bfd_mach_o_section_get_entry_size (abfd, sec);
480 for (j = first; j < last; j++)
482 unsigned int isym = dysymtab->indirect_syms[j];
484 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
485 s->section = sec->bfdsection;
486 s->value = addr - sec->addr;
489 if (isym < symtab->nsyms
490 && symtab->symbols[isym].symbol.name)
492 const char *sym = symtab->symbols[isym].symbol.name;
497 memcpy (names, sym, len);
499 memcpy (names, "$stub", sizeof ("$stub"));
500 names += sizeof ("$stub");
519 bfd_mach_o_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
523 bfd_symbol_info (symbol, ret);
527 bfd_mach_o_print_symbol (bfd *abfd,
530 bfd_print_symbol_type how)
532 FILE *file = (FILE *) afile;
534 bfd_mach_o_asymbol *asym = (bfd_mach_o_asymbol *)symbol;
538 case bfd_print_symbol_name:
539 fprintf (file, "%s", symbol->name);
542 bfd_print_symbol_vandf (abfd, (void *) file, symbol);
543 if (asym->n_type & BFD_MACH_O_N_STAB)
544 name = bfd_get_stab_name (asym->n_type);
546 switch (asym->n_type & BFD_MACH_O_N_TYPE)
548 case BFD_MACH_O_N_UNDF:
549 if (symbol->value == 0)
554 case BFD_MACH_O_N_ABS:
557 case BFD_MACH_O_N_INDR:
560 case BFD_MACH_O_N_PBUD:
563 case BFD_MACH_O_N_SECT:
572 fprintf (file, " %02x %-6s %02x %04x",
573 asym->n_type, name, asym->n_sect, asym->n_desc);
574 if ((asym->n_type & BFD_MACH_O_N_STAB) == 0
575 && (asym->n_type & BFD_MACH_O_N_TYPE) == BFD_MACH_O_N_SECT)
576 fprintf (file, " [%s]", symbol->section->name);
577 fprintf (file, " %s", symbol->name);
582 bfd_mach_o_convert_architecture (bfd_mach_o_cpu_type mtype,
583 bfd_mach_o_cpu_subtype msubtype ATTRIBUTE_UNUSED,
584 enum bfd_architecture *type,
585 unsigned long *subtype)
587 *subtype = bfd_arch_unknown;
591 case BFD_MACH_O_CPU_TYPE_VAX: *type = bfd_arch_vax; break;
592 case BFD_MACH_O_CPU_TYPE_MC680x0: *type = bfd_arch_m68k; break;
593 case BFD_MACH_O_CPU_TYPE_I386:
594 *type = bfd_arch_i386;
595 *subtype = bfd_mach_i386_i386;
597 case BFD_MACH_O_CPU_TYPE_X86_64:
598 *type = bfd_arch_i386;
599 *subtype = bfd_mach_x86_64;
601 case BFD_MACH_O_CPU_TYPE_MIPS: *type = bfd_arch_mips; break;
602 case BFD_MACH_O_CPU_TYPE_MC98000: *type = bfd_arch_m98k; break;
603 case BFD_MACH_O_CPU_TYPE_HPPA: *type = bfd_arch_hppa; break;
604 case BFD_MACH_O_CPU_TYPE_ARM: *type = bfd_arch_arm; break;
605 case BFD_MACH_O_CPU_TYPE_MC88000: *type = bfd_arch_m88k; break;
606 case BFD_MACH_O_CPU_TYPE_SPARC:
607 *type = bfd_arch_sparc;
608 *subtype = bfd_mach_sparc;
610 case BFD_MACH_O_CPU_TYPE_I860: *type = bfd_arch_i860; break;
611 case BFD_MACH_O_CPU_TYPE_ALPHA: *type = bfd_arch_alpha; break;
612 case BFD_MACH_O_CPU_TYPE_POWERPC:
613 *type = bfd_arch_powerpc;
614 *subtype = bfd_mach_ppc;
616 case BFD_MACH_O_CPU_TYPE_POWERPC_64:
617 *type = bfd_arch_powerpc;
618 *subtype = bfd_mach_ppc64;
621 *type = bfd_arch_unknown;
627 bfd_mach_o_write_header (bfd *abfd, bfd_mach_o_header *header)
629 struct mach_o_header_external raw;
632 size = mach_o_wide_p (header) ?
633 BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE;
635 bfd_h_put_32 (abfd, header->magic, raw.magic);
636 bfd_h_put_32 (abfd, header->cputype, raw.cputype);
637 bfd_h_put_32 (abfd, header->cpusubtype, raw.cpusubtype);
638 bfd_h_put_32 (abfd, header->filetype, raw.filetype);
639 bfd_h_put_32 (abfd, header->ncmds, raw.ncmds);
640 bfd_h_put_32 (abfd, header->sizeofcmds, raw.sizeofcmds);
641 bfd_h_put_32 (abfd, header->flags, raw.flags);
643 if (mach_o_wide_p (header))
644 bfd_h_put_32 (abfd, header->reserved, raw.reserved);
646 if (bfd_seek (abfd, 0, SEEK_SET) != 0
647 || bfd_bwrite (&raw, size, abfd) != size)
654 bfd_mach_o_write_thread (bfd *abfd, bfd_mach_o_load_command *command)
656 bfd_mach_o_thread_command *cmd = &command->command.thread;
658 struct mach_o_thread_command_external raw;
661 BFD_ASSERT ((command->type == BFD_MACH_O_LC_THREAD)
662 || (command->type == BFD_MACH_O_LC_UNIXTHREAD));
665 for (i = 0; i < cmd->nflavours; i++)
667 BFD_ASSERT ((cmd->flavours[i].size % 4) == 0);
668 BFD_ASSERT (cmd->flavours[i].offset ==
669 (command->offset + offset + BFD_MACH_O_LC_SIZE));
671 bfd_h_put_32 (abfd, cmd->flavours[i].flavour, raw.flavour);
672 bfd_h_put_32 (abfd, (cmd->flavours[i].size / 4), raw.count);
674 if (bfd_seek (abfd, command->offset + offset, SEEK_SET) != 0
675 || bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
678 offset += cmd->flavours[i].size + sizeof (raw);
685 bfd_mach_o_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
688 return (asect->reloc_count + 1) * sizeof (arelent *);
692 bfd_mach_o_canonicalize_one_reloc (bfd *abfd,
693 struct mach_o_reloc_info_external *raw,
694 arelent *res, asymbol **syms)
696 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
697 bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
698 bfd_mach_o_reloc_info reloc;
703 addr = bfd_get_32 (abfd, raw->r_address);
704 symnum = bfd_get_32 (abfd, raw->r_symbolnum);
706 if (addr & BFD_MACH_O_SR_SCATTERED)
710 /* Scattered relocation.
711 Extract section and offset from r_value. */
712 res->sym_ptr_ptr = NULL;
714 for (j = 0; j < mdata->nsects; j++)
716 bfd_mach_o_section *sect = mdata->sections[j];
717 if (symnum >= sect->addr && symnum < sect->addr + sect->size)
719 res->sym_ptr_ptr = sect->bfdsection->symbol_ptr_ptr;
720 res->addend = symnum - sect->addr;
724 res->address = BFD_MACH_O_GET_SR_ADDRESS (addr);
725 reloc.r_type = BFD_MACH_O_GET_SR_TYPE (addr);
726 reloc.r_length = BFD_MACH_O_GET_SR_LENGTH (addr);
727 reloc.r_pcrel = addr & BFD_MACH_O_SR_PCREL;
728 reloc.r_scattered = 1;
732 unsigned int num = BFD_MACH_O_GET_R_SYMBOLNUM (symnum);
735 if (symnum & BFD_MACH_O_R_EXTERN)
742 BFD_ASSERT (num != 0);
743 BFD_ASSERT (num <= mdata->nsects);
744 sym = mdata->sections[num - 1]->bfdsection->symbol_ptr_ptr;
745 /* For a symbol defined in section S, the addend (stored in the
746 binary) contains the address of the section. To comply with
747 bfd conventio, substract the section address.
748 Use the address from the header, so that the user can modify
749 the vma of the section. */
750 res->addend = -mdata->sections[num - 1]->addr;
753 res->sym_ptr_ptr = sym;
754 reloc.r_type = BFD_MACH_O_GET_R_TYPE (symnum);
755 reloc.r_length = BFD_MACH_O_GET_R_LENGTH (symnum);
756 reloc.r_pcrel = (symnum & BFD_MACH_O_R_PCREL) ? 1 : 0;
757 reloc.r_scattered = 0;
760 if (!(*bed->_bfd_mach_o_swap_reloc_in)(res, &reloc))
766 bfd_mach_o_canonicalize_relocs (bfd *abfd, unsigned long filepos,
768 arelent *res, asymbol **syms)
771 struct mach_o_reloc_info_external *native_relocs;
772 bfd_size_type native_size;
774 /* Allocate and read relocs. */
775 native_size = count * BFD_MACH_O_RELENT_SIZE;
777 (struct mach_o_reloc_info_external *) bfd_malloc (native_size);
778 if (native_relocs == NULL)
781 if (bfd_seek (abfd, filepos, SEEK_SET) != 0
782 || bfd_bread (native_relocs, native_size, abfd) != native_size)
785 for (i = 0; i < count; i++)
787 if (bfd_mach_o_canonicalize_one_reloc (abfd, &native_relocs[i],
791 free (native_relocs);
794 free (native_relocs);
799 bfd_mach_o_canonicalize_reloc (bfd *abfd, asection *asect,
800 arelent **rels, asymbol **syms)
802 bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
806 if (asect->reloc_count == 0)
809 /* No need to go further if we don't know how to read relocs. */
810 if (bed->_bfd_mach_o_swap_reloc_in == NULL)
813 res = bfd_malloc (asect->reloc_count * sizeof (arelent));
817 if (bfd_mach_o_canonicalize_relocs (abfd, asect->rel_filepos,
818 asect->reloc_count, res, syms) < 0)
824 for (i = 0; i < asect->reloc_count; i++)
827 asect->relocation = res;
833 bfd_mach_o_get_dynamic_reloc_upper_bound (bfd *abfd)
835 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
837 if (mdata->dysymtab == NULL)
839 return (mdata->dysymtab->nextrel + mdata->dysymtab->nlocrel)
840 * sizeof (arelent *);
844 bfd_mach_o_canonicalize_dynamic_reloc (bfd *abfd, arelent **rels,
845 struct bfd_symbol **syms)
847 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
848 bfd_mach_o_dysymtab_command *dysymtab = mdata->dysymtab;
849 bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
853 if (dysymtab == NULL)
855 if (dysymtab->nextrel == 0 && dysymtab->nlocrel == 0)
858 /* No need to go further if we don't know how to read relocs. */
859 if (bed->_bfd_mach_o_swap_reloc_in == NULL)
862 res = bfd_malloc ((dysymtab->nextrel + dysymtab->nlocrel) * sizeof (arelent));
866 if (bfd_mach_o_canonicalize_relocs (abfd, dysymtab->extreloff,
867 dysymtab->nextrel, res, syms) < 0)
873 if (bfd_mach_o_canonicalize_relocs (abfd, dysymtab->locreloff,
875 res + dysymtab->nextrel, syms) < 0)
881 for (i = 0; i < dysymtab->nextrel + dysymtab->nlocrel; i++)
888 bfd_mach_o_write_relocs (bfd *abfd, bfd_mach_o_section *section)
890 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
894 bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
896 sec = section->bfdsection;
897 if (sec->reloc_count == 0)
900 if (bed->_bfd_mach_o_swap_reloc_out == NULL)
903 /* Allocate relocation room. */
904 mdata->filelen = FILE_ALIGN(mdata->filelen, 2);
905 section->nreloc = sec->reloc_count;
906 sec->rel_filepos = mdata->filelen;
907 section->reloff = sec->rel_filepos;
908 mdata->filelen += sec->reloc_count * BFD_MACH_O_RELENT_SIZE;
910 if (bfd_seek (abfd, section->reloff, SEEK_SET) != 0)
913 /* Convert and write. */
914 entries = section->bfdsection->orelocation;
915 for (i = 0; i < section->nreloc; i++)
917 arelent *rel = entries[i];
918 struct mach_o_reloc_info_external raw;
919 bfd_mach_o_reloc_info info, *pinfo = &info;
921 /* Convert relocation to an intermediate representation. */
922 if (!(*bed->_bfd_mach_o_swap_reloc_out) (rel, pinfo))
925 /* Lower the relocation info. */
926 if (pinfo->r_scattered)
930 v = BFD_MACH_O_SR_SCATTERED
931 | (pinfo->r_pcrel ? BFD_MACH_O_SR_PCREL : 0)
932 | BFD_MACH_O_SET_SR_LENGTH(pinfo->r_length)
933 | BFD_MACH_O_SET_SR_TYPE(pinfo->r_type)
934 | BFD_MACH_O_SET_SR_ADDRESS(pinfo->r_address);
935 /* Note: scattered relocs have field in reverse order... */
936 bfd_put_32 (abfd, v, raw.r_address);
937 bfd_put_32 (abfd, pinfo->r_value, raw.r_symbolnum);
943 bfd_put_32 (abfd, pinfo->r_address, raw.r_address);
944 v = BFD_MACH_O_SET_R_SYMBOLNUM (pinfo->r_value)
945 | (pinfo->r_pcrel ? BFD_MACH_O_R_PCREL : 0)
946 | BFD_MACH_O_SET_R_LENGTH (pinfo->r_length)
947 | (pinfo->r_extern ? BFD_MACH_O_R_EXTERN : 0)
948 | BFD_MACH_O_SET_R_TYPE (pinfo->r_type);
949 bfd_put_32 (abfd, v, raw.r_symbolnum);
952 if (bfd_bwrite (&raw, BFD_MACH_O_RELENT_SIZE, abfd)
953 != BFD_MACH_O_RELENT_SIZE)
960 bfd_mach_o_write_section_32 (bfd *abfd, bfd_mach_o_section *section)
962 struct mach_o_section_32_external raw;
964 memcpy (raw.sectname, section->sectname, 16);
965 memcpy (raw.segname, section->segname, 16);
966 bfd_h_put_32 (abfd, section->addr, raw.addr);
967 bfd_h_put_32 (abfd, section->size, raw.size);
968 bfd_h_put_32 (abfd, section->offset, raw.offset);
969 bfd_h_put_32 (abfd, section->align, raw.align);
970 bfd_h_put_32 (abfd, section->reloff, raw.reloff);
971 bfd_h_put_32 (abfd, section->nreloc, raw.nreloc);
972 bfd_h_put_32 (abfd, section->flags, raw.flags);
973 bfd_h_put_32 (abfd, section->reserved1, raw.reserved1);
974 bfd_h_put_32 (abfd, section->reserved2, raw.reserved2);
976 if (bfd_bwrite (&raw, BFD_MACH_O_SECTION_SIZE, abfd)
977 != BFD_MACH_O_SECTION_SIZE)
984 bfd_mach_o_write_section_64 (bfd *abfd, bfd_mach_o_section *section)
986 struct mach_o_section_64_external raw;
988 memcpy (raw.sectname, section->sectname, 16);
989 memcpy (raw.segname, section->segname, 16);
990 bfd_h_put_64 (abfd, section->addr, raw.addr);
991 bfd_h_put_64 (abfd, section->size, raw.size);
992 bfd_h_put_32 (abfd, section->offset, raw.offset);
993 bfd_h_put_32 (abfd, section->align, raw.align);
994 bfd_h_put_32 (abfd, section->reloff, raw.reloff);
995 bfd_h_put_32 (abfd, section->nreloc, raw.nreloc);
996 bfd_h_put_32 (abfd, section->flags, raw.flags);
997 bfd_h_put_32 (abfd, section->reserved1, raw.reserved1);
998 bfd_h_put_32 (abfd, section->reserved2, raw.reserved2);
999 bfd_h_put_32 (abfd, section->reserved3, raw.reserved3);
1001 if (bfd_bwrite (&raw, BFD_MACH_O_SECTION_64_SIZE, abfd)
1002 != BFD_MACH_O_SECTION_64_SIZE)
1009 bfd_mach_o_write_segment_32 (bfd *abfd, bfd_mach_o_load_command *command)
1011 struct mach_o_segment_command_32_external raw;
1012 bfd_mach_o_segment_command *seg = &command->command.segment;
1013 bfd_mach_o_section *sec;
1015 BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT);
1017 for (sec = seg->sect_head; sec != NULL; sec = sec->next)
1018 if (!bfd_mach_o_write_relocs (abfd, sec))
1021 memcpy (raw.segname, seg->segname, 16);
1022 bfd_h_put_32 (abfd, seg->vmaddr, raw.vmaddr);
1023 bfd_h_put_32 (abfd, seg->vmsize, raw.vmsize);
1024 bfd_h_put_32 (abfd, seg->fileoff, raw.fileoff);
1025 bfd_h_put_32 (abfd, seg->filesize, raw.filesize);
1026 bfd_h_put_32 (abfd, seg->maxprot, raw.maxprot);
1027 bfd_h_put_32 (abfd, seg->initprot, raw.initprot);
1028 bfd_h_put_32 (abfd, seg->nsects, raw.nsects);
1029 bfd_h_put_32 (abfd, seg->flags, raw.flags);
1031 if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
1032 || bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
1035 for (sec = seg->sect_head; sec != NULL; sec = sec->next)
1036 if (bfd_mach_o_write_section_32 (abfd, sec))
1043 bfd_mach_o_write_segment_64 (bfd *abfd, bfd_mach_o_load_command *command)
1045 struct mach_o_segment_command_64_external raw;
1046 bfd_mach_o_segment_command *seg = &command->command.segment;
1047 bfd_mach_o_section *sec;
1049 BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT_64);
1051 for (sec = seg->sect_head; sec != NULL; sec = sec->next)
1052 if (!bfd_mach_o_write_relocs (abfd, sec))
1055 memcpy (raw.segname, seg->segname, 16);
1056 bfd_h_put_64 (abfd, seg->vmaddr, raw.vmaddr);
1057 bfd_h_put_64 (abfd, seg->vmsize, raw.vmsize);
1058 bfd_h_put_64 (abfd, seg->fileoff, raw.fileoff);
1059 bfd_h_put_64 (abfd, seg->filesize, raw.filesize);
1060 bfd_h_put_32 (abfd, seg->maxprot, raw.maxprot);
1061 bfd_h_put_32 (abfd, seg->initprot, raw.initprot);
1062 bfd_h_put_32 (abfd, seg->nsects, raw.nsects);
1063 bfd_h_put_32 (abfd, seg->flags, raw.flags);
1065 if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
1066 || bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
1069 for (sec = seg->sect_head; sec != NULL; sec = sec->next)
1070 if (bfd_mach_o_write_section_64 (abfd, sec))
1077 bfd_mach_o_write_symtab (bfd *abfd, bfd_mach_o_load_command *command)
1079 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
1080 bfd_mach_o_symtab_command *sym = &command->command.symtab;
1082 unsigned int wide = bfd_mach_o_wide_p (abfd);
1083 unsigned int symlen = wide ? BFD_MACH_O_NLIST_64_SIZE : BFD_MACH_O_NLIST_SIZE;
1084 struct bfd_strtab_hash *strtab;
1085 asymbol **symbols = bfd_get_outsymbols (abfd);
1087 BFD_ASSERT (command->type == BFD_MACH_O_LC_SYMTAB);
1089 /* Write the symbols first. */
1090 mdata->filelen = FILE_ALIGN(mdata->filelen, wide ? 3 : 2);
1091 sym->symoff = mdata->filelen;
1092 if (bfd_seek (abfd, sym->symoff, SEEK_SET) != 0)
1095 sym->nsyms = bfd_get_symcount (abfd);
1096 mdata->filelen += sym->nsyms * symlen;
1098 strtab = _bfd_stringtab_init ();
1102 for (i = 0; i < sym->nsyms; i++)
1104 bfd_size_type str_index;
1105 bfd_mach_o_asymbol *s = (bfd_mach_o_asymbol *)symbols[i];
1107 /* Compute name index. */
1108 /* An index of 0 always means the empty string. */
1109 if (s->symbol.name == 0 || s->symbol.name[0] == '\0')
1113 str_index = _bfd_stringtab_add (strtab, s->symbol.name, TRUE, FALSE);
1114 if (str_index == (bfd_size_type) -1)
1120 struct mach_o_nlist_64_external raw;
1122 bfd_h_put_32 (abfd, str_index, raw.n_strx);
1123 bfd_h_put_8 (abfd, s->n_type, raw.n_type);
1124 bfd_h_put_8 (abfd, s->n_sect, raw.n_sect);
1125 bfd_h_put_16 (abfd, s->n_desc, raw.n_desc);
1126 bfd_h_put_64 (abfd, s->symbol.section->vma + s->symbol.value,
1129 if (bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
1134 struct mach_o_nlist_external raw;
1136 bfd_h_put_32 (abfd, str_index, raw.n_strx);
1137 bfd_h_put_8 (abfd, s->n_type, raw.n_type);
1138 bfd_h_put_8 (abfd, s->n_sect, raw.n_sect);
1139 bfd_h_put_16 (abfd, s->n_desc, raw.n_desc);
1140 bfd_h_put_32 (abfd, s->symbol.section->vma + s->symbol.value,
1143 if (bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
1147 sym->strsize = _bfd_stringtab_size (strtab);
1148 sym->stroff = mdata->filelen;
1149 mdata->filelen += sym->strsize;
1151 if (_bfd_stringtab_emit (abfd, strtab) != TRUE)
1153 _bfd_stringtab_free (strtab);
1157 struct mach_o_symtab_command_external raw;
1159 bfd_h_put_32 (abfd, sym->symoff, raw.symoff);
1160 bfd_h_put_32 (abfd, sym->nsyms, raw.nsyms);
1161 bfd_h_put_32 (abfd, sym->stroff, raw.stroff);
1162 bfd_h_put_32 (abfd, sym->strsize, raw.strsize);
1164 if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
1165 || bfd_bwrite (&raw, sizeof (raw), abfd) != sizeof (raw))
1172 _bfd_stringtab_free (strtab);
1176 /* Process the symbols and generate Mach-O specific fields.
1180 bfd_mach_o_mangle_symbols (bfd *abfd)
1183 asymbol **symbols = bfd_get_outsymbols (abfd);
1185 for (i = 0; i < bfd_get_symcount (abfd); i++)
1187 bfd_mach_o_asymbol *s = (bfd_mach_o_asymbol *)symbols[i];
1189 if (s->n_type == BFD_MACH_O_N_UNDF && !(s->symbol.flags & BSF_DEBUGGING))
1191 /* As genuine Mach-O symbols type shouldn't be N_UNDF (undefined
1192 symbols should be N_UNDEF | N_EXT), we suppose the back-end
1193 values haven't been set. */
1194 if (s->symbol.section == bfd_abs_section_ptr)
1195 s->n_type = BFD_MACH_O_N_ABS;
1196 else if (s->symbol.section == bfd_und_section_ptr)
1198 s->n_type = BFD_MACH_O_N_UNDF;
1199 if (s->symbol.flags & BSF_WEAK)
1200 s->n_desc |= BFD_MACH_O_N_WEAK_REF;
1202 else if (s->symbol.section == bfd_com_section_ptr)
1203 s->n_type = BFD_MACH_O_N_UNDF | BFD_MACH_O_N_EXT;
1205 s->n_type = BFD_MACH_O_N_SECT;
1207 if (s->symbol.flags & BSF_GLOBAL)
1208 s->n_type |= BFD_MACH_O_N_EXT;
1211 /* Compute section index. */
1212 if (s->symbol.section != bfd_abs_section_ptr
1213 && s->symbol.section != bfd_und_section_ptr
1214 && s->symbol.section != bfd_com_section_ptr)
1215 s->n_sect = s->symbol.section->target_index;
1217 /* Number symbols. */
1218 s->symbol.udata.i = i;
1224 bfd_mach_o_write_contents (bfd *abfd)
1227 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
1229 if (mdata->header.ncmds == 0)
1230 if (!bfd_mach_o_build_commands (abfd))
1233 /* Now write header information. */
1234 if (mdata->header.filetype == 0)
1236 if (abfd->flags & EXEC_P)
1237 mdata->header.filetype = BFD_MACH_O_MH_EXECUTE;
1238 else if (abfd->flags & DYNAMIC)
1239 mdata->header.filetype = BFD_MACH_O_MH_DYLIB;
1241 mdata->header.filetype = BFD_MACH_O_MH_OBJECT;
1243 if (!bfd_mach_o_write_header (abfd, &mdata->header))
1246 /* Assign a number to each symbols. */
1247 if (!bfd_mach_o_mangle_symbols (abfd))
1250 for (i = 0; i < mdata->header.ncmds; i++)
1252 struct mach_o_load_command_external raw;
1253 bfd_mach_o_load_command *cur = &mdata->commands[i];
1254 unsigned long typeflag;
1256 typeflag = cur->type | (cur->type_required ? BFD_MACH_O_LC_REQ_DYLD : 0);
1258 bfd_h_put_32 (abfd, typeflag, raw.cmd);
1259 bfd_h_put_32 (abfd, cur->len, raw.cmdsize);
1261 if (bfd_seek (abfd, cur->offset, SEEK_SET) != 0
1262 || bfd_bwrite (&raw, BFD_MACH_O_LC_SIZE, abfd) != 8)
1267 case BFD_MACH_O_LC_SEGMENT:
1268 if (bfd_mach_o_write_segment_32 (abfd, cur) != 0)
1271 case BFD_MACH_O_LC_SEGMENT_64:
1272 if (bfd_mach_o_write_segment_64 (abfd, cur) != 0)
1275 case BFD_MACH_O_LC_SYMTAB:
1276 if (!bfd_mach_o_write_symtab (abfd, cur))
1279 case BFD_MACH_O_LC_SYMSEG:
1281 case BFD_MACH_O_LC_THREAD:
1282 case BFD_MACH_O_LC_UNIXTHREAD:
1283 if (bfd_mach_o_write_thread (abfd, cur) != 0)
1286 case BFD_MACH_O_LC_LOADFVMLIB:
1287 case BFD_MACH_O_LC_IDFVMLIB:
1288 case BFD_MACH_O_LC_IDENT:
1289 case BFD_MACH_O_LC_FVMFILE:
1290 case BFD_MACH_O_LC_PREPAGE:
1291 case BFD_MACH_O_LC_DYSYMTAB:
1292 case BFD_MACH_O_LC_LOAD_DYLIB:
1293 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB:
1294 case BFD_MACH_O_LC_ID_DYLIB:
1295 case BFD_MACH_O_LC_REEXPORT_DYLIB:
1296 case BFD_MACH_O_LC_LOAD_UPWARD_DYLIB:
1297 case BFD_MACH_O_LC_LOAD_DYLINKER:
1298 case BFD_MACH_O_LC_ID_DYLINKER:
1299 case BFD_MACH_O_LC_PREBOUND_DYLIB:
1300 case BFD_MACH_O_LC_ROUTINES:
1301 case BFD_MACH_O_LC_SUB_FRAMEWORK:
1304 (*_bfd_error_handler) (_("unable to write unknown load command 0x%lx"),
1305 (unsigned long) cur->type);
1314 bfd_mach_o_append_section_to_segment (bfd_mach_o_segment_command *seg,
1317 bfd_mach_o_section *s = (bfd_mach_o_section *)sec->used_by_bfd;
1318 if (seg->sect_head == NULL)
1321 seg->sect_tail->next = s;
1325 /* Create section Mach-O flags from BFD flags. */
1328 bfd_mach_o_set_section_flags_from_bfd (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
1331 bfd_mach_o_section *s = bfd_mach_o_get_mach_o_section (sec);
1333 /* Create default flags. */
1334 bfd_flags = bfd_get_section_flags (abfd, sec);
1335 if ((bfd_flags & SEC_CODE) == SEC_CODE)
1336 s->flags = BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS
1337 | BFD_MACH_O_S_ATTR_SOME_INSTRUCTIONS
1338 | BFD_MACH_O_S_REGULAR;
1339 else if ((bfd_flags & (SEC_ALLOC | SEC_LOAD)) == SEC_ALLOC)
1340 s->flags = BFD_MACH_O_S_ZEROFILL;
1341 else if (bfd_flags & SEC_DEBUGGING)
1342 s->flags = BFD_MACH_O_S_REGULAR | BFD_MACH_O_S_ATTR_DEBUG;
1344 s->flags = BFD_MACH_O_S_REGULAR;
1347 /* Build Mach-O load commands from the sections. */
1350 bfd_mach_o_build_commands (bfd *abfd)
1352 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
1353 unsigned int wide = mach_o_wide_p (&mdata->header);
1354 bfd_mach_o_segment_command *seg;
1356 bfd_mach_o_load_command *cmd;
1357 bfd_mach_o_load_command *symtab_cmd;
1360 /* Return now if commands are already built. */
1361 if (mdata->header.ncmds)
1364 /* Very simple version: a command (segment) to contain all the sections and
1365 a command for the symbol table. */
1366 mdata->header.ncmds = 2;
1367 mdata->commands = bfd_alloc (abfd, mdata->header.ncmds
1368 * sizeof (bfd_mach_o_load_command));
1369 if (mdata->commands == NULL)
1371 cmd = &mdata->commands[0];
1372 seg = &cmd->command.segment;
1374 seg->nsects = bfd_count_sections (abfd);
1376 /* Set segment command. */
1379 cmd->type = BFD_MACH_O_LC_SEGMENT_64;
1380 cmd->offset = BFD_MACH_O_HEADER_64_SIZE;
1381 cmd->len = BFD_MACH_O_LC_SEGMENT_64_SIZE
1382 + BFD_MACH_O_SECTION_64_SIZE * seg->nsects;
1386 cmd->type = BFD_MACH_O_LC_SEGMENT;
1387 cmd->offset = BFD_MACH_O_HEADER_SIZE;
1388 cmd->len = BFD_MACH_O_LC_SEGMENT_SIZE
1389 + BFD_MACH_O_SECTION_SIZE * seg->nsects;
1391 cmd->type_required = FALSE;
1392 mdata->header.sizeofcmds = cmd->len;
1393 mdata->filelen = cmd->offset + cmd->len;
1395 /* Set symtab command. */
1396 symtab_cmd = &mdata->commands[1];
1398 symtab_cmd->type = BFD_MACH_O_LC_SYMTAB;
1399 symtab_cmd->offset = cmd->offset + cmd->len;
1400 symtab_cmd->len = 6 * 4;
1401 symtab_cmd->type_required = FALSE;
1403 mdata->header.sizeofcmds += symtab_cmd->len;
1404 mdata->filelen += symtab_cmd->len;
1406 /* Fill segment command. */
1407 memset (seg->segname, 0, sizeof (seg->segname));
1409 seg->fileoff = mdata->filelen;
1411 seg->maxprot = BFD_MACH_O_PROT_READ | BFD_MACH_O_PROT_WRITE
1412 | BFD_MACH_O_PROT_EXECUTE;
1413 seg->initprot = seg->maxprot;
1415 seg->sect_head = NULL;
1416 seg->sect_tail = NULL;
1418 /* Create Mach-O sections. */
1420 for (sec = abfd->sections; sec; sec = sec->next)
1422 bfd_mach_o_section *msect = bfd_mach_o_get_mach_o_section (sec);
1424 bfd_mach_o_append_section_to_segment (seg, sec);
1426 if (msect->flags == 0)
1428 /* We suppose it hasn't been set. Convert from BFD flags. */
1429 bfd_mach_o_set_section_flags_from_bfd (abfd, sec);
1431 msect->addr = bfd_get_section_vma (abfd, sec);
1432 msect->size = bfd_get_section_size (sec);
1433 msect->align = bfd_get_section_alignment (abfd, sec);
1435 if (msect->size != 0)
1437 mdata->filelen = FILE_ALIGN (mdata->filelen, msect->align);
1438 msect->offset = mdata->filelen;
1443 sec->filepos = msect->offset;
1444 sec->target_index = ++target_index;
1446 mdata->filelen += msect->size;
1448 seg->filesize = mdata->filelen - seg->fileoff;
1449 seg->vmsize = seg->filesize;
1454 /* Set the contents of a section. */
1457 bfd_mach_o_set_section_contents (bfd *abfd,
1459 const void * location,
1461 bfd_size_type count)
1465 /* This must be done first, because bfd_set_section_contents is
1466 going to set output_has_begun to TRUE. */
1467 if (! abfd->output_has_begun && ! bfd_mach_o_build_commands (abfd))
1473 pos = section->filepos + offset;
1474 if (bfd_seek (abfd, pos, SEEK_SET) != 0
1475 || bfd_bwrite (location, count, abfd) != count)
1482 bfd_mach_o_sizeof_headers (bfd *a ATTRIBUTE_UNUSED,
1483 struct bfd_link_info *info ATTRIBUTE_UNUSED)
1488 /* Make an empty symbol. This is required only because
1489 bfd_make_section_anyway wants to create a symbol for the section. */
1492 bfd_mach_o_make_empty_symbol (bfd *abfd)
1494 asymbol *new_symbol;
1496 new_symbol = bfd_zalloc (abfd, sizeof (bfd_mach_o_asymbol));
1497 if (new_symbol == NULL)
1499 new_symbol->the_bfd = abfd;
1500 new_symbol->udata.i = 0;
1505 bfd_mach_o_read_header (bfd *abfd, bfd_mach_o_header *header)
1507 struct mach_o_header_external raw;
1509 bfd_vma (*get32) (const void *) = NULL;
1511 /* Just read the magic number. */
1512 if (bfd_seek (abfd, 0, SEEK_SET) != 0
1513 || bfd_bread (raw.magic, sizeof (raw.magic), abfd) != 4)
1516 if (bfd_getb32 (raw.magic) == BFD_MACH_O_MH_MAGIC)
1518 header->byteorder = BFD_ENDIAN_BIG;
1519 header->magic = BFD_MACH_O_MH_MAGIC;
1520 header->version = 1;
1523 else if (bfd_getl32 (raw.magic) == BFD_MACH_O_MH_MAGIC)
1525 header->byteorder = BFD_ENDIAN_LITTLE;
1526 header->magic = BFD_MACH_O_MH_MAGIC;
1527 header->version = 1;
1530 else if (bfd_getb32 (raw.magic) == BFD_MACH_O_MH_MAGIC_64)
1532 header->byteorder = BFD_ENDIAN_BIG;
1533 header->magic = BFD_MACH_O_MH_MAGIC_64;
1534 header->version = 2;
1537 else if (bfd_getl32 (raw.magic) == BFD_MACH_O_MH_MAGIC_64)
1539 header->byteorder = BFD_ENDIAN_LITTLE;
1540 header->magic = BFD_MACH_O_MH_MAGIC_64;
1541 header->version = 2;
1546 header->byteorder = BFD_ENDIAN_UNKNOWN;
1550 /* Once the size of the header is known, read the full header. */
1551 size = mach_o_wide_p (header) ?
1552 BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE;
1554 if (bfd_seek (abfd, 0, SEEK_SET) != 0
1555 || bfd_bread (&raw, size, abfd) != size)
1558 header->cputype = (*get32) (raw.cputype);
1559 header->cpusubtype = (*get32) (raw.cpusubtype);
1560 header->filetype = (*get32) (raw.filetype);
1561 header->ncmds = (*get32) (raw.ncmds);
1562 header->sizeofcmds = (*get32) (raw.sizeofcmds);
1563 header->flags = (*get32) (raw.flags);
1565 if (mach_o_wide_p (header))
1566 header->reserved = (*get32) (raw.reserved);
1572 bfd_mach_o_new_section_hook (bfd *abfd, asection *sec)
1574 bfd_mach_o_section *s;
1576 s = bfd_mach_o_get_mach_o_section (sec);
1581 s = (bfd_mach_o_section *) bfd_zalloc (abfd, sizeof (*s));
1584 sec->used_by_bfd = s;
1585 s->bfdsection = sec;
1587 /* Create default name. */
1588 bfd_mach_o_convert_section_name_to_mach_o (abfd, sec, s);
1590 /* Create default flags. */
1591 bfd_flags = bfd_get_section_flags (abfd, sec);
1592 if ((bfd_flags & SEC_CODE) == SEC_CODE)
1593 s->flags = BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS
1594 | BFD_MACH_O_S_ATTR_SOME_INSTRUCTIONS
1595 | BFD_MACH_O_S_REGULAR;
1596 else if ((bfd_flags & (SEC_ALLOC | SEC_LOAD)) == SEC_ALLOC)
1597 s->flags = BFD_MACH_O_S_ZEROFILL;
1598 else if (bfd_flags & SEC_DEBUGGING)
1599 s->flags = BFD_MACH_O_S_REGULAR | BFD_MACH_O_S_ATTR_DEBUG;
1601 s->flags = BFD_MACH_O_S_REGULAR;
1604 return _bfd_generic_new_section_hook (abfd, sec);
1608 bfd_mach_o_init_section_from_mach_o (bfd *abfd, asection *sec,
1612 bfd_mach_o_section *section;
1614 flags = bfd_get_section_flags (abfd, sec);
1615 section = bfd_mach_o_get_mach_o_section (sec);
1617 if (flags == SEC_NO_FLAGS)
1619 /* Try to guess flags. */
1620 if (section->flags & BFD_MACH_O_S_ATTR_DEBUG)
1621 flags = SEC_DEBUGGING;
1625 if ((section->flags & BFD_MACH_O_SECTION_TYPE_MASK)
1626 != BFD_MACH_O_S_ZEROFILL)
1629 if (prot & BFD_MACH_O_PROT_EXECUTE)
1631 if (prot & BFD_MACH_O_PROT_WRITE)
1633 else if (prot & BFD_MACH_O_PROT_READ)
1634 flags |= SEC_READONLY;
1640 if ((flags & SEC_DEBUGGING) == 0)
1644 if (section->offset != 0)
1645 flags |= SEC_HAS_CONTENTS;
1646 if (section->nreloc != 0)
1649 bfd_set_section_flags (abfd, sec, flags);
1651 sec->vma = section->addr;
1652 sec->lma = section->addr;
1653 sec->size = section->size;
1654 sec->filepos = section->offset;
1655 sec->alignment_power = section->align;
1656 sec->segment_mark = 0;
1657 sec->reloc_count = section->nreloc;
1658 sec->rel_filepos = section->reloff;
1662 bfd_mach_o_make_bfd_section (bfd *abfd,
1663 const unsigned char *segname,
1664 const unsigned char *sectname)
1669 bfd_mach_o_convert_section_name_to_bfd
1670 (abfd, (const char *)segname, (const char *)sectname, &sname, &flags);
1674 return bfd_make_section_anyway_with_flags (abfd, sname, flags);
1678 bfd_mach_o_read_section_32 (bfd *abfd,
1679 unsigned int offset,
1682 struct mach_o_section_32_external raw;
1684 bfd_mach_o_section *section;
1686 if (bfd_seek (abfd, offset, SEEK_SET) != 0
1687 || (bfd_bread (&raw, BFD_MACH_O_SECTION_SIZE, abfd)
1688 != BFD_MACH_O_SECTION_SIZE))
1691 sec = bfd_mach_o_make_bfd_section (abfd, raw.segname, raw.sectname);
1695 section = bfd_mach_o_get_mach_o_section (sec);
1696 memcpy (section->segname, raw.segname, sizeof (raw.segname));
1697 section->segname[BFD_MACH_O_SEGNAME_SIZE] = 0;
1698 memcpy (section->sectname, raw.sectname, sizeof (raw.sectname));
1699 section->sectname[BFD_MACH_O_SECTNAME_SIZE] = 0;
1700 section->addr = bfd_h_get_32 (abfd, raw.addr);
1701 section->size = bfd_h_get_32 (abfd, raw.size);
1702 section->offset = bfd_h_get_32 (abfd, raw.offset);
1703 section->align = bfd_h_get_32 (abfd, raw.align);
1704 section->reloff = bfd_h_get_32 (abfd, raw.reloff);
1705 section->nreloc = bfd_h_get_32 (abfd, raw.nreloc);
1706 section->flags = bfd_h_get_32 (abfd, raw.flags);
1707 section->reserved1 = bfd_h_get_32 (abfd, raw.reserved1);
1708 section->reserved2 = bfd_h_get_32 (abfd, raw.reserved2);
1709 section->reserved3 = 0;
1711 bfd_mach_o_init_section_from_mach_o (abfd, sec, prot);
1717 bfd_mach_o_read_section_64 (bfd *abfd,
1718 unsigned int offset,
1721 struct mach_o_section_64_external raw;
1723 bfd_mach_o_section *section;
1725 if (bfd_seek (abfd, offset, SEEK_SET) != 0
1726 || (bfd_bread (&raw, BFD_MACH_O_SECTION_64_SIZE, abfd)
1727 != BFD_MACH_O_SECTION_64_SIZE))
1730 sec = bfd_mach_o_make_bfd_section (abfd, raw.segname, raw.sectname);
1734 section = bfd_mach_o_get_mach_o_section (sec);
1735 memcpy (section->segname, raw.segname, sizeof (raw.segname));
1736 section->segname[BFD_MACH_O_SEGNAME_SIZE] = 0;
1737 memcpy (section->sectname, raw.sectname, sizeof (raw.sectname));
1738 section->sectname[BFD_MACH_O_SECTNAME_SIZE] = 0;
1739 section->addr = bfd_h_get_64 (abfd, raw.addr);
1740 section->size = bfd_h_get_64 (abfd, raw.size);
1741 section->offset = bfd_h_get_32 (abfd, raw.offset);
1742 section->align = bfd_h_get_32 (abfd, raw.align);
1743 section->reloff = bfd_h_get_32 (abfd, raw.reloff);
1744 section->nreloc = bfd_h_get_32 (abfd, raw.nreloc);
1745 section->flags = bfd_h_get_32 (abfd, raw.flags);
1746 section->reserved1 = bfd_h_get_32 (abfd, raw.reserved1);
1747 section->reserved2 = bfd_h_get_32 (abfd, raw.reserved2);
1748 section->reserved3 = bfd_h_get_32 (abfd, raw.reserved3);
1750 bfd_mach_o_init_section_from_mach_o (abfd, sec, prot);
1756 bfd_mach_o_read_section (bfd *abfd,
1757 unsigned int offset,
1762 return bfd_mach_o_read_section_64 (abfd, offset, prot);
1764 return bfd_mach_o_read_section_32 (abfd, offset, prot);
1768 bfd_mach_o_read_symtab_symbol (bfd *abfd,
1769 bfd_mach_o_symtab_command *sym,
1770 bfd_mach_o_asymbol *s,
1773 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
1774 unsigned int wide = mach_o_wide_p (&mdata->header);
1775 unsigned int symwidth =
1776 wide ? BFD_MACH_O_NLIST_64_SIZE : BFD_MACH_O_NLIST_SIZE;
1777 unsigned int symoff = sym->symoff + (i * symwidth);
1778 struct mach_o_nlist_64_external raw;
1779 unsigned char type = -1;
1780 unsigned char section = -1;
1782 symvalue value = -1;
1783 unsigned long stroff = -1;
1784 unsigned int symtype = -1;
1786 BFD_ASSERT (sym->strtab != NULL);
1788 if (bfd_seek (abfd, symoff, SEEK_SET) != 0
1789 || bfd_bread (&raw, symwidth, abfd) != symwidth)
1791 (*_bfd_error_handler)
1792 (_("bfd_mach_o_read_symtab_symbol: unable to read %d bytes at %lu"),
1793 symwidth, (unsigned long) symoff);
1797 stroff = bfd_h_get_32 (abfd, raw.n_strx);
1798 type = bfd_h_get_8 (abfd, raw.n_type);
1799 symtype = type & BFD_MACH_O_N_TYPE;
1800 section = bfd_h_get_8 (abfd, raw.n_sect);
1801 desc = bfd_h_get_16 (abfd, raw.n_desc);
1803 value = bfd_h_get_64 (abfd, raw.n_value);
1805 value = bfd_h_get_32 (abfd, raw.n_value);
1807 if (stroff >= sym->strsize)
1809 (*_bfd_error_handler)
1810 (_("bfd_mach_o_read_symtab_symbol: name out of range (%lu >= %lu)"),
1811 (unsigned long) stroff,
1812 (unsigned long) sym->strsize);
1816 s->symbol.the_bfd = abfd;
1817 s->symbol.name = sym->strtab + stroff;
1818 s->symbol.value = value;
1819 s->symbol.flags = 0x0;
1820 s->symbol.udata.i = 0;
1822 s->n_sect = section;
1825 if (type & BFD_MACH_O_N_STAB)
1827 s->symbol.flags |= BSF_DEBUGGING;
1828 s->symbol.section = bfd_und_section_ptr;
1840 if ((section > 0) && (section <= mdata->nsects))
1842 s->symbol.section = mdata->sections[section - 1]->bfdsection;
1844 s->symbol.value - mdata->sections[section - 1]->addr;
1851 if (type & BFD_MACH_O_N_PEXT)
1852 s->symbol.flags |= BSF_GLOBAL;
1854 if (type & BFD_MACH_O_N_EXT)
1855 s->symbol.flags |= BSF_GLOBAL;
1857 if (!(type & (BFD_MACH_O_N_PEXT | BFD_MACH_O_N_EXT)))
1858 s->symbol.flags |= BSF_LOCAL;
1862 case BFD_MACH_O_N_UNDF:
1863 if (type == (BFD_MACH_O_N_UNDF | BFD_MACH_O_N_EXT)
1864 && s->symbol.value != 0)
1866 /* A common symbol. */
1867 s->symbol.section = bfd_com_section_ptr;
1868 s->symbol.flags = BSF_NO_FLAGS;
1872 s->symbol.section = bfd_und_section_ptr;
1873 if (s->n_desc & BFD_MACH_O_N_WEAK_REF)
1874 s->symbol.flags |= BSF_WEAK;
1877 case BFD_MACH_O_N_PBUD:
1878 s->symbol.section = bfd_und_section_ptr;
1880 case BFD_MACH_O_N_ABS:
1881 s->symbol.section = bfd_abs_section_ptr;
1883 case BFD_MACH_O_N_SECT:
1884 if ((section > 0) && (section <= mdata->nsects))
1886 s->symbol.section = mdata->sections[section - 1]->bfdsection;
1888 s->symbol.value - mdata->sections[section - 1]->addr;
1892 /* Mach-O uses 0 to mean "no section"; not an error. */
1895 (*_bfd_error_handler) (_("bfd_mach_o_read_symtab_symbol: "
1896 "symbol \"%s\" specified invalid section %d (max %lu): setting to undefined"),
1897 s->symbol.name, section, mdata->nsects);
1899 s->symbol.section = bfd_und_section_ptr;
1902 case BFD_MACH_O_N_INDR:
1903 /* FIXME: we don't follow the BFD convention as this indirect symbol
1904 won't be followed by the referenced one. This looks harmless
1905 unless we start using the linker. */
1906 s->symbol.flags |= BSF_INDIRECT;
1907 s->symbol.section = bfd_ind_section_ptr;
1908 s->symbol.value = 0;
1911 (*_bfd_error_handler) (_("bfd_mach_o_read_symtab_symbol: "
1912 "symbol \"%s\" specified invalid type field 0x%x: setting to undefined"),
1913 s->symbol.name, symtype);
1914 s->symbol.section = bfd_und_section_ptr;
1923 bfd_mach_o_read_symtab_strtab (bfd *abfd)
1925 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
1926 bfd_mach_o_symtab_command *sym = mdata->symtab;
1928 /* Fail if there is no symtab. */
1932 /* Success if already loaded. */
1936 if (abfd->flags & BFD_IN_MEMORY)
1938 struct bfd_in_memory *b;
1940 b = (struct bfd_in_memory *) abfd->iostream;
1942 if ((sym->stroff + sym->strsize) > b->size)
1944 bfd_set_error (bfd_error_file_truncated);
1947 sym->strtab = (char *) b->buffer + sym->stroff;
1951 sym->strtab = bfd_alloc (abfd, sym->strsize);
1952 if (sym->strtab == NULL)
1955 if (bfd_seek (abfd, sym->stroff, SEEK_SET) != 0
1956 || bfd_bread (sym->strtab, sym->strsize, abfd) != sym->strsize)
1958 bfd_set_error (bfd_error_file_truncated);
1967 bfd_mach_o_read_symtab_symbols (bfd *abfd)
1969 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
1970 bfd_mach_o_symtab_command *sym = mdata->symtab;
1973 if (sym == NULL || sym->symbols)
1975 /* Return now if there are no symbols or if already loaded. */
1979 sym->symbols = bfd_alloc (abfd, sym->nsyms * sizeof (bfd_mach_o_asymbol));
1981 if (sym->symbols == NULL)
1983 (*_bfd_error_handler) (_("bfd_mach_o_read_symtab_symbols: unable to allocate memory for symbols"));
1987 if (!bfd_mach_o_read_symtab_strtab (abfd))
1990 for (i = 0; i < sym->nsyms; i++)
1992 if (!bfd_mach_o_read_symtab_symbol (abfd, sym, &sym->symbols[i], i))
2000 bfd_mach_o_i386_flavour_string (unsigned int flavour)
2002 switch ((int) flavour)
2004 case BFD_MACH_O_x86_THREAD_STATE32: return "x86_THREAD_STATE32";
2005 case BFD_MACH_O_x86_FLOAT_STATE32: return "x86_FLOAT_STATE32";
2006 case BFD_MACH_O_x86_EXCEPTION_STATE32: return "x86_EXCEPTION_STATE32";
2007 case BFD_MACH_O_x86_THREAD_STATE64: return "x86_THREAD_STATE64";
2008 case BFD_MACH_O_x86_FLOAT_STATE64: return "x86_FLOAT_STATE64";
2009 case BFD_MACH_O_x86_EXCEPTION_STATE64: return "x86_EXCEPTION_STATE64";
2010 case BFD_MACH_O_x86_THREAD_STATE: return "x86_THREAD_STATE";
2011 case BFD_MACH_O_x86_FLOAT_STATE: return "x86_FLOAT_STATE";
2012 case BFD_MACH_O_x86_EXCEPTION_STATE: return "x86_EXCEPTION_STATE";
2013 case BFD_MACH_O_x86_DEBUG_STATE32: return "x86_DEBUG_STATE32";
2014 case BFD_MACH_O_x86_DEBUG_STATE64: return "x86_DEBUG_STATE64";
2015 case BFD_MACH_O_x86_DEBUG_STATE: return "x86_DEBUG_STATE";
2016 case BFD_MACH_O_x86_THREAD_STATE_NONE: return "x86_THREAD_STATE_NONE";
2017 default: return "UNKNOWN";
2022 bfd_mach_o_ppc_flavour_string (unsigned int flavour)
2024 switch ((int) flavour)
2026 case BFD_MACH_O_PPC_THREAD_STATE: return "PPC_THREAD_STATE";
2027 case BFD_MACH_O_PPC_FLOAT_STATE: return "PPC_FLOAT_STATE";
2028 case BFD_MACH_O_PPC_EXCEPTION_STATE: return "PPC_EXCEPTION_STATE";
2029 case BFD_MACH_O_PPC_VECTOR_STATE: return "PPC_VECTOR_STATE";
2030 case BFD_MACH_O_PPC_THREAD_STATE64: return "PPC_THREAD_STATE64";
2031 case BFD_MACH_O_PPC_EXCEPTION_STATE64: return "PPC_EXCEPTION_STATE64";
2032 default: return "UNKNOWN";
2037 bfd_mach_o_read_dylinker (bfd *abfd, bfd_mach_o_load_command *command)
2039 bfd_mach_o_dylinker_command *cmd = &command->command.dylinker;
2040 struct mach_o_str_command_external raw;
2041 unsigned int nameoff;
2043 BFD_ASSERT ((command->type == BFD_MACH_O_LC_ID_DYLINKER)
2044 || (command->type == BFD_MACH_O_LC_LOAD_DYLINKER));
2046 if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
2047 || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
2050 nameoff = bfd_h_get_32 (abfd, raw.str);
2052 cmd->name_offset = command->offset + nameoff;
2053 cmd->name_len = command->len - nameoff;
2054 cmd->name_str = bfd_alloc (abfd, cmd->name_len);
2055 if (cmd->name_str == NULL)
2057 if (bfd_seek (abfd, cmd->name_offset, SEEK_SET) != 0
2058 || bfd_bread (cmd->name_str, cmd->name_len, abfd) != cmd->name_len)
2064 bfd_mach_o_read_dylib (bfd *abfd, bfd_mach_o_load_command *command)
2066 bfd_mach_o_dylib_command *cmd = &command->command.dylib;
2067 struct mach_o_dylib_command_external raw;
2068 unsigned int nameoff;
2070 switch (command->type)
2072 case BFD_MACH_O_LC_LOAD_DYLIB:
2073 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB:
2074 case BFD_MACH_O_LC_ID_DYLIB:
2075 case BFD_MACH_O_LC_REEXPORT_DYLIB:
2076 case BFD_MACH_O_LC_LOAD_UPWARD_DYLIB:
2083 if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
2084 || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
2087 nameoff = bfd_h_get_32 (abfd, raw.name);
2088 cmd->timestamp = bfd_h_get_32 (abfd, raw.timestamp);
2089 cmd->current_version = bfd_h_get_32 (abfd, raw.current_version);
2090 cmd->compatibility_version = bfd_h_get_32 (abfd, raw.compatibility_version);
2092 cmd->name_offset = command->offset + nameoff;
2093 cmd->name_len = command->len - nameoff;
2094 cmd->name_str = bfd_alloc (abfd, cmd->name_len);
2095 if (cmd->name_str == NULL)
2097 if (bfd_seek (abfd, cmd->name_offset, SEEK_SET) != 0
2098 || bfd_bread (cmd->name_str, cmd->name_len, abfd) != cmd->name_len)
2104 bfd_mach_o_read_prebound_dylib (bfd *abfd ATTRIBUTE_UNUSED,
2105 bfd_mach_o_load_command *command ATTRIBUTE_UNUSED)
2107 /* bfd_mach_o_prebound_dylib_command *cmd = &command->command.prebound_dylib; */
2109 BFD_ASSERT (command->type == BFD_MACH_O_LC_PREBOUND_DYLIB);
2114 bfd_mach_o_read_thread (bfd *abfd, bfd_mach_o_load_command *command)
2116 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
2117 bfd_mach_o_thread_command *cmd = &command->command.thread;
2118 unsigned int offset;
2119 unsigned int nflavours;
2122 BFD_ASSERT ((command->type == BFD_MACH_O_LC_THREAD)
2123 || (command->type == BFD_MACH_O_LC_UNIXTHREAD));
2125 /* Count the number of threads. */
2128 while (offset != command->len)
2130 struct mach_o_thread_command_external raw;
2132 if (offset >= command->len)
2135 if (bfd_seek (abfd, command->offset + offset, SEEK_SET) != 0
2136 || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
2139 offset += sizeof (raw) + bfd_h_get_32 (abfd, raw.count) * 4;
2143 /* Allocate threads. */
2144 cmd->flavours = bfd_alloc
2145 (abfd, nflavours * sizeof (bfd_mach_o_thread_flavour));
2146 if (cmd->flavours == NULL)
2148 cmd->nflavours = nflavours;
2152 while (offset != command->len)
2154 struct mach_o_thread_command_external raw;
2156 if (offset >= command->len)
2159 if (nflavours >= cmd->nflavours)
2162 if (bfd_seek (abfd, command->offset + offset, SEEK_SET) != 0
2163 || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
2166 cmd->flavours[nflavours].flavour = bfd_h_get_32 (abfd, raw.flavour);
2167 cmd->flavours[nflavours].offset = command->offset + offset + sizeof (raw);
2168 cmd->flavours[nflavours].size = bfd_h_get_32 (abfd, raw.count) * 4;
2169 offset += cmd->flavours[nflavours].size + sizeof (raw);
2173 for (i = 0; i < nflavours; i++)
2176 unsigned int snamelen;
2178 const char *flavourstr;
2179 const char *prefix = "LC_THREAD";
2182 switch (mdata->header.cputype)
2184 case BFD_MACH_O_CPU_TYPE_POWERPC:
2185 case BFD_MACH_O_CPU_TYPE_POWERPC_64:
2186 flavourstr = bfd_mach_o_ppc_flavour_string (cmd->flavours[i].flavour);
2188 case BFD_MACH_O_CPU_TYPE_I386:
2189 case BFD_MACH_O_CPU_TYPE_X86_64:
2190 flavourstr = bfd_mach_o_i386_flavour_string (cmd->flavours[i].flavour);
2193 flavourstr = "UNKNOWN_ARCHITECTURE";
2197 snamelen = strlen (prefix) + 1 + 20 + 1 + strlen (flavourstr) + 1;
2198 sname = bfd_alloc (abfd, snamelen);
2204 sprintf (sname, "%s.%s.%u", prefix, flavourstr, j);
2205 if (bfd_get_section_by_name (abfd, sname) == NULL)
2210 bfdsec = bfd_make_section_with_flags (abfd, sname, SEC_HAS_CONTENTS);
2214 bfdsec->size = cmd->flavours[i].size;
2215 bfdsec->filepos = cmd->flavours[i].offset;
2216 bfdsec->alignment_power = 0x0;
2218 cmd->section = bfdsec;
2225 bfd_mach_o_read_dysymtab (bfd *abfd, bfd_mach_o_load_command *command)
2227 bfd_mach_o_dysymtab_command *cmd = &command->command.dysymtab;
2228 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
2230 BFD_ASSERT (command->type == BFD_MACH_O_LC_DYSYMTAB);
2233 struct mach_o_dysymtab_command_external raw;
2235 if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
2236 || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
2239 cmd->ilocalsym = bfd_h_get_32 (abfd, raw.ilocalsym);
2240 cmd->nlocalsym = bfd_h_get_32 (abfd, raw.nlocalsym);
2241 cmd->iextdefsym = bfd_h_get_32 (abfd, raw.iextdefsym);
2242 cmd->nextdefsym = bfd_h_get_32 (abfd, raw.nextdefsym);
2243 cmd->iundefsym = bfd_h_get_32 (abfd, raw.iundefsym);
2244 cmd->nundefsym = bfd_h_get_32 (abfd, raw.nundefsym);
2245 cmd->tocoff = bfd_h_get_32 (abfd, raw.tocoff);
2246 cmd->ntoc = bfd_h_get_32 (abfd, raw.ntoc);
2247 cmd->modtaboff = bfd_h_get_32 (abfd, raw.modtaboff);
2248 cmd->nmodtab = bfd_h_get_32 (abfd, raw.nmodtab);
2249 cmd->extrefsymoff = bfd_h_get_32 (abfd, raw.extrefsymoff);
2250 cmd->nextrefsyms = bfd_h_get_32 (abfd, raw.nextrefsyms);
2251 cmd->indirectsymoff = bfd_h_get_32 (abfd, raw.indirectsymoff);
2252 cmd->nindirectsyms = bfd_h_get_32 (abfd, raw.nindirectsyms);
2253 cmd->extreloff = bfd_h_get_32 (abfd, raw.extreloff);
2254 cmd->nextrel = bfd_h_get_32 (abfd, raw.nextrel);
2255 cmd->locreloff = bfd_h_get_32 (abfd, raw.locreloff);
2256 cmd->nlocrel = bfd_h_get_32 (abfd, raw.nlocrel);
2259 if (cmd->nmodtab != 0)
2262 int wide = bfd_mach_o_wide_p (abfd);
2263 unsigned int module_len = wide ? 56 : 52;
2266 bfd_alloc (abfd, cmd->nmodtab * sizeof (bfd_mach_o_dylib_module));
2267 if (cmd->dylib_module == NULL)
2270 if (bfd_seek (abfd, cmd->modtaboff, SEEK_SET) != 0)
2273 for (i = 0; i < cmd->nmodtab; i++)
2275 bfd_mach_o_dylib_module *module = &cmd->dylib_module[i];
2277 unsigned char buf[56];
2279 if (bfd_bread ((void *) buf, module_len, abfd) != module_len)
2282 module->module_name_idx = bfd_h_get_32 (abfd, buf + 0);
2283 module->iextdefsym = bfd_h_get_32 (abfd, buf + 4);
2284 module->nextdefsym = bfd_h_get_32 (abfd, buf + 8);
2285 module->irefsym = bfd_h_get_32 (abfd, buf + 12);
2286 module->nrefsym = bfd_h_get_32 (abfd, buf + 16);
2287 module->ilocalsym = bfd_h_get_32 (abfd, buf + 20);
2288 module->nlocalsym = bfd_h_get_32 (abfd, buf + 24);
2289 module->iextrel = bfd_h_get_32 (abfd, buf + 28);
2290 module->nextrel = bfd_h_get_32 (abfd, buf + 32);
2291 v = bfd_h_get_32 (abfd, buf +36);
2292 module->iinit = v & 0xffff;
2293 module->iterm = (v >> 16) & 0xffff;
2294 v = bfd_h_get_32 (abfd, buf + 40);
2295 module->ninit = v & 0xffff;
2296 module->nterm = (v >> 16) & 0xffff;
2299 module->objc_module_info_size = bfd_h_get_32 (abfd, buf + 44);
2300 module->objc_module_info_addr = bfd_h_get_64 (abfd, buf + 48);
2304 module->objc_module_info_addr = bfd_h_get_32 (abfd, buf + 44);
2305 module->objc_module_info_size = bfd_h_get_32 (abfd, buf + 48);
2314 cmd->dylib_toc = bfd_alloc
2315 (abfd, cmd->ntoc * sizeof (bfd_mach_o_dylib_table_of_content));
2316 if (cmd->dylib_toc == NULL)
2319 if (bfd_seek (abfd, cmd->tocoff, SEEK_SET) != 0)
2322 for (i = 0; i < cmd->ntoc; i++)
2324 struct mach_o_dylib_table_of_contents_external raw;
2325 bfd_mach_o_dylib_table_of_content *toc = &cmd->dylib_toc[i];
2327 if (bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
2330 toc->symbol_index = bfd_h_get_32 (abfd, raw.symbol_index);
2331 toc->module_index = bfd_h_get_32 (abfd, raw.module_index);
2335 if (cmd->nindirectsyms != 0)
2339 cmd->indirect_syms = bfd_alloc
2340 (abfd, cmd->nindirectsyms * sizeof (unsigned int));
2341 if (cmd->indirect_syms == NULL)
2344 if (bfd_seek (abfd, cmd->indirectsymoff, SEEK_SET) != 0)
2347 for (i = 0; i < cmd->nindirectsyms; i++)
2349 unsigned char raw[4];
2350 unsigned int *is = &cmd->indirect_syms[i];
2352 if (bfd_bread (raw, sizeof (raw), abfd) != sizeof (raw))
2355 *is = bfd_h_get_32 (abfd, raw);
2359 if (cmd->nextrefsyms != 0)
2364 cmd->ext_refs = bfd_alloc
2365 (abfd, cmd->nextrefsyms * sizeof (bfd_mach_o_dylib_reference));
2366 if (cmd->ext_refs == NULL)
2369 if (bfd_seek (abfd, cmd->extrefsymoff, SEEK_SET) != 0)
2372 for (i = 0; i < cmd->nextrefsyms; i++)
2374 unsigned char raw[4];
2375 bfd_mach_o_dylib_reference *ref = &cmd->ext_refs[i];
2377 if (bfd_bread (raw, sizeof (raw), abfd) != sizeof (raw))
2380 /* Fields isym and flags are written as bit-fields, thus we need
2381 a specific processing for endianness. */
2382 v = bfd_h_get_32 (abfd, raw);
2383 if (bfd_big_endian (abfd))
2385 ref->isym = (v >> 8) & 0xffffff;
2386 ref->flags = v & 0xff;
2390 ref->isym = v & 0xffffff;
2391 ref->flags = (v >> 24) & 0xff;
2396 if (mdata->dysymtab)
2398 mdata->dysymtab = cmd;
2404 bfd_mach_o_read_symtab (bfd *abfd, bfd_mach_o_load_command *command)
2406 bfd_mach_o_symtab_command *symtab = &command->command.symtab;
2407 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
2408 struct mach_o_symtab_command_external raw;
2410 BFD_ASSERT (command->type == BFD_MACH_O_LC_SYMTAB);
2412 if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
2413 || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
2416 symtab->symoff = bfd_h_get_32 (abfd, raw.symoff);
2417 symtab->nsyms = bfd_h_get_32 (abfd, raw.nsyms);
2418 symtab->stroff = bfd_h_get_32 (abfd, raw.stroff);
2419 symtab->strsize = bfd_h_get_32 (abfd, raw.strsize);
2420 symtab->symbols = NULL;
2421 symtab->strtab = NULL;
2423 if (symtab->nsyms != 0)
2424 abfd->flags |= HAS_SYMS;
2428 mdata->symtab = symtab;
2433 bfd_mach_o_read_uuid (bfd *abfd, bfd_mach_o_load_command *command)
2435 bfd_mach_o_uuid_command *cmd = &command->command.uuid;
2437 BFD_ASSERT (command->type == BFD_MACH_O_LC_UUID);
2439 if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
2440 || bfd_bread (cmd->uuid, 16, abfd) != 16)
2447 bfd_mach_o_read_linkedit (bfd *abfd, bfd_mach_o_load_command *command)
2449 bfd_mach_o_linkedit_command *cmd = &command->command.linkedit;
2450 struct mach_o_linkedit_data_command_external raw;
2452 if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
2453 || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
2456 cmd->dataoff = bfd_get_32 (abfd, raw.dataoff);
2457 cmd->datasize = bfd_get_32 (abfd, raw.datasize);
2462 bfd_mach_o_read_str (bfd *abfd, bfd_mach_o_load_command *command)
2464 bfd_mach_o_str_command *cmd = &command->command.str;
2465 struct mach_o_str_command_external raw;
2468 if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
2469 || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
2472 off = bfd_get_32 (abfd, raw.str);
2473 cmd->stroff = command->offset + off;
2474 cmd->str_len = command->len - off;
2475 cmd->str = bfd_alloc (abfd, cmd->str_len);
2476 if (cmd->str == NULL)
2478 if (bfd_seek (abfd, cmd->stroff, SEEK_SET) != 0
2479 || bfd_bread ((void *) cmd->str, cmd->str_len, abfd) != cmd->str_len)
2485 bfd_mach_o_read_dyld_info (bfd *abfd, bfd_mach_o_load_command *command)
2487 bfd_mach_o_dyld_info_command *cmd = &command->command.dyld_info;
2488 struct mach_o_dyld_info_command_external raw;
2490 if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
2491 || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
2494 cmd->rebase_off = bfd_get_32 (abfd, raw.rebase_off);
2495 cmd->rebase_size = bfd_get_32 (abfd, raw.rebase_size);
2496 cmd->bind_off = bfd_get_32 (abfd, raw.bind_off);
2497 cmd->bind_size = bfd_get_32 (abfd, raw.bind_size);
2498 cmd->weak_bind_off = bfd_get_32 (abfd, raw.weak_bind_off);
2499 cmd->weak_bind_size = bfd_get_32 (abfd, raw.weak_bind_size);
2500 cmd->lazy_bind_off = bfd_get_32 (abfd, raw.lazy_bind_off);
2501 cmd->lazy_bind_size = bfd_get_32 (abfd, raw.lazy_bind_size);
2502 cmd->export_off = bfd_get_32 (abfd, raw.export_off);
2503 cmd->export_size = bfd_get_32 (abfd, raw.export_size);
2508 bfd_mach_o_read_version_min (bfd *abfd, bfd_mach_o_load_command *command)
2510 bfd_mach_o_version_min_command *cmd = &command->command.version_min;
2511 struct mach_o_version_min_command_external raw;
2514 if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
2515 || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
2518 ver = bfd_get_32 (abfd, raw.version);
2519 cmd->rel = ver >> 16;
2520 cmd->maj = ver >> 8;
2522 cmd->reserved = bfd_get_32 (abfd, raw.reserved);
2527 bfd_mach_o_read_segment (bfd *abfd,
2528 bfd_mach_o_load_command *command,
2531 bfd_mach_o_segment_command *seg = &command->command.segment;
2536 struct mach_o_segment_command_64_external raw;
2538 BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT_64);
2540 if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
2541 || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
2544 memcpy (seg->segname, raw.segname, 16);
2545 seg->segname[16] = '\0';
2547 seg->vmaddr = bfd_h_get_64 (abfd, raw.vmaddr);
2548 seg->vmsize = bfd_h_get_64 (abfd, raw.vmsize);
2549 seg->fileoff = bfd_h_get_64 (abfd, raw.fileoff);
2550 seg->filesize = bfd_h_get_64 (abfd, raw.filesize);
2551 seg->maxprot = bfd_h_get_32 (abfd, raw.maxprot);
2552 seg->initprot = bfd_h_get_32 (abfd, raw.initprot);
2553 seg->nsects = bfd_h_get_32 (abfd, raw.nsects);
2554 seg->flags = bfd_h_get_32 (abfd, raw.flags);
2558 struct mach_o_segment_command_32_external raw;
2560 BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT);
2562 if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0
2563 || bfd_bread (&raw, sizeof (raw), abfd) != sizeof (raw))
2566 memcpy (seg->segname, raw.segname, 16);
2567 seg->segname[16] = '\0';
2569 seg->vmaddr = bfd_h_get_32 (abfd, raw.vmaddr);
2570 seg->vmsize = bfd_h_get_32 (abfd, raw.vmsize);
2571 seg->fileoff = bfd_h_get_32 (abfd, raw.fileoff);
2572 seg->filesize = bfd_h_get_32 (abfd, raw.filesize);
2573 seg->maxprot = bfd_h_get_32 (abfd, raw.maxprot);
2574 seg->initprot = bfd_h_get_32 (abfd, raw.initprot);
2575 seg->nsects = bfd_h_get_32 (abfd, raw.nsects);
2576 seg->flags = bfd_h_get_32 (abfd, raw.flags);
2578 seg->sect_head = NULL;
2579 seg->sect_tail = NULL;
2581 for (i = 0; i < seg->nsects; i++)
2587 segoff = command->offset + BFD_MACH_O_LC_SEGMENT_64_SIZE
2588 + (i * BFD_MACH_O_SECTION_64_SIZE);
2590 segoff = command->offset + BFD_MACH_O_LC_SEGMENT_SIZE
2591 + (i * BFD_MACH_O_SECTION_SIZE);
2593 sec = bfd_mach_o_read_section (abfd, segoff, seg->initprot, wide);
2597 bfd_mach_o_append_section_to_segment (seg, sec);
2604 bfd_mach_o_read_segment_32 (bfd *abfd, bfd_mach_o_load_command *command)
2606 return bfd_mach_o_read_segment (abfd, command, 0);
2610 bfd_mach_o_read_segment_64 (bfd *abfd, bfd_mach_o_load_command *command)
2612 return bfd_mach_o_read_segment (abfd, command, 1);
2616 bfd_mach_o_read_command (bfd *abfd, bfd_mach_o_load_command *command)
2618 struct mach_o_load_command_external raw;
2621 /* Read command type and length. */
2622 if (bfd_seek (abfd, command->offset, SEEK_SET) != 0
2623 || bfd_bread (&raw, BFD_MACH_O_LC_SIZE, abfd) != BFD_MACH_O_LC_SIZE)
2626 cmd = bfd_h_get_32 (abfd, raw.cmd);
2627 command->type = cmd & ~BFD_MACH_O_LC_REQ_DYLD;
2628 command->type_required = cmd & BFD_MACH_O_LC_REQ_DYLD ? TRUE : FALSE;
2629 command->len = bfd_h_get_32 (abfd, raw.cmdsize);
2631 switch (command->type)
2633 case BFD_MACH_O_LC_SEGMENT:
2634 if (bfd_mach_o_read_segment_32 (abfd, command) != 0)
2637 case BFD_MACH_O_LC_SEGMENT_64:
2638 if (bfd_mach_o_read_segment_64 (abfd, command) != 0)
2641 case BFD_MACH_O_LC_SYMTAB:
2642 if (bfd_mach_o_read_symtab (abfd, command) != 0)
2645 case BFD_MACH_O_LC_SYMSEG:
2647 case BFD_MACH_O_LC_THREAD:
2648 case BFD_MACH_O_LC_UNIXTHREAD:
2649 if (bfd_mach_o_read_thread (abfd, command) != 0)
2652 case BFD_MACH_O_LC_LOAD_DYLINKER:
2653 case BFD_MACH_O_LC_ID_DYLINKER:
2654 if (bfd_mach_o_read_dylinker (abfd, command) != 0)
2657 case BFD_MACH_O_LC_LOAD_DYLIB:
2658 case BFD_MACH_O_LC_ID_DYLIB:
2659 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB:
2660 case BFD_MACH_O_LC_REEXPORT_DYLIB:
2661 case BFD_MACH_O_LC_LOAD_UPWARD_DYLIB:
2662 if (bfd_mach_o_read_dylib (abfd, command) != 0)
2665 case BFD_MACH_O_LC_PREBOUND_DYLIB:
2666 if (bfd_mach_o_read_prebound_dylib (abfd, command) != 0)
2669 case BFD_MACH_O_LC_LOADFVMLIB:
2670 case BFD_MACH_O_LC_IDFVMLIB:
2671 case BFD_MACH_O_LC_IDENT:
2672 case BFD_MACH_O_LC_FVMFILE:
2673 case BFD_MACH_O_LC_PREPAGE:
2674 case BFD_MACH_O_LC_ROUTINES:
2675 case BFD_MACH_O_LC_ROUTINES_64:
2677 case BFD_MACH_O_LC_SUB_FRAMEWORK:
2678 case BFD_MACH_O_LC_SUB_UMBRELLA:
2679 case BFD_MACH_O_LC_SUB_LIBRARY:
2680 case BFD_MACH_O_LC_SUB_CLIENT:
2681 case BFD_MACH_O_LC_RPATH:
2682 if (bfd_mach_o_read_str (abfd, command) != 0)
2685 case BFD_MACH_O_LC_DYSYMTAB:
2686 if (bfd_mach_o_read_dysymtab (abfd, command) != 0)
2689 case BFD_MACH_O_LC_TWOLEVEL_HINTS:
2690 case BFD_MACH_O_LC_PREBIND_CKSUM:
2692 case BFD_MACH_O_LC_UUID:
2693 if (bfd_mach_o_read_uuid (abfd, command) != 0)
2696 case BFD_MACH_O_LC_CODE_SIGNATURE:
2697 case BFD_MACH_O_LC_SEGMENT_SPLIT_INFO:
2698 case BFD_MACH_O_LC_FUNCTION_STARTS:
2699 if (bfd_mach_o_read_linkedit (abfd, command) != 0)
2702 case BFD_MACH_O_LC_DYLD_INFO:
2703 if (bfd_mach_o_read_dyld_info (abfd, command) != 0)
2706 case BFD_MACH_O_LC_VERSION_MIN_MACOSX:
2707 case BFD_MACH_O_LC_VERSION_MIN_IPHONEOS:
2708 if (!bfd_mach_o_read_version_min (abfd, command))
2712 (*_bfd_error_handler)(_("%B: unable to read unknown load command 0x%lx"),
2713 abfd, (unsigned long) command->type);
2721 bfd_mach_o_flatten_sections (bfd *abfd)
2723 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
2727 /* Count total number of sections. */
2730 for (i = 0; i < mdata->header.ncmds; i++)
2732 if (mdata->commands[i].type == BFD_MACH_O_LC_SEGMENT
2733 || mdata->commands[i].type == BFD_MACH_O_LC_SEGMENT_64)
2735 bfd_mach_o_segment_command *seg;
2737 seg = &mdata->commands[i].command.segment;
2738 mdata->nsects += seg->nsects;
2742 /* Allocate sections array. */
2743 mdata->sections = bfd_alloc (abfd,
2744 mdata->nsects * sizeof (bfd_mach_o_section *));
2746 /* Fill the array. */
2749 for (i = 0; i < mdata->header.ncmds; i++)
2751 if (mdata->commands[i].type == BFD_MACH_O_LC_SEGMENT
2752 || mdata->commands[i].type == BFD_MACH_O_LC_SEGMENT_64)
2754 bfd_mach_o_segment_command *seg;
2755 bfd_mach_o_section *sec;
2757 seg = &mdata->commands[i].command.segment;
2758 BFD_ASSERT (csect + seg->nsects <= mdata->nsects);
2760 for (sec = seg->sect_head; sec != NULL; sec = sec->next)
2761 mdata->sections[csect++] = sec;
2767 bfd_mach_o_scan_start_address (bfd *abfd)
2769 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
2770 bfd_mach_o_thread_command *cmd = NULL;
2773 for (i = 0; i < mdata->header.ncmds; i++)
2774 if ((mdata->commands[i].type == BFD_MACH_O_LC_THREAD) ||
2775 (mdata->commands[i].type == BFD_MACH_O_LC_UNIXTHREAD))
2777 cmd = &mdata->commands[i].command.thread;
2784 /* FIXME: create a subtarget hook ? */
2785 for (i = 0; i < cmd->nflavours; i++)
2787 if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_I386)
2788 && (cmd->flavours[i].flavour
2789 == (unsigned long) BFD_MACH_O_x86_THREAD_STATE32))
2791 unsigned char buf[4];
2793 if (bfd_seek (abfd, cmd->flavours[i].offset + 40, SEEK_SET) != 0
2794 || bfd_bread (buf, 4, abfd) != 4)
2797 abfd->start_address = bfd_h_get_32 (abfd, buf);
2799 else if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_POWERPC)
2800 && (cmd->flavours[i].flavour == BFD_MACH_O_PPC_THREAD_STATE))
2802 unsigned char buf[4];
2804 if (bfd_seek (abfd, cmd->flavours[i].offset + 0, SEEK_SET) != 0
2805 || bfd_bread (buf, 4, abfd) != 4)
2808 abfd->start_address = bfd_h_get_32 (abfd, buf);
2810 else if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_POWERPC_64)
2811 && (cmd->flavours[i].flavour == BFD_MACH_O_PPC_THREAD_STATE64))
2813 unsigned char buf[8];
2815 if (bfd_seek (abfd, cmd->flavours[i].offset + 0, SEEK_SET) != 0
2816 || bfd_bread (buf, 8, abfd) != 8)
2819 abfd->start_address = bfd_h_get_64 (abfd, buf);
2821 else if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_X86_64)
2822 && (cmd->flavours[i].flavour == BFD_MACH_O_x86_THREAD_STATE64))
2824 unsigned char buf[8];
2826 if (bfd_seek (abfd, cmd->flavours[i].offset + (16 * 8), SEEK_SET) != 0
2827 || bfd_bread (buf, 8, abfd) != 8)
2830 abfd->start_address = bfd_h_get_64 (abfd, buf);
2838 bfd_mach_o_set_arch_mach (bfd *abfd,
2839 enum bfd_architecture arch,
2840 unsigned long machine)
2842 bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
2844 /* If this isn't the right architecture for this backend, and this
2845 isn't the generic backend, fail. */
2846 if (arch != bed->arch
2847 && arch != bfd_arch_unknown
2848 && bed->arch != bfd_arch_unknown)
2851 return bfd_default_set_arch_mach (abfd, arch, machine);
2855 bfd_mach_o_scan (bfd *abfd,
2856 bfd_mach_o_header *header,
2857 bfd_mach_o_data_struct *mdata)
2860 enum bfd_architecture cputype;
2861 unsigned long cpusubtype;
2862 unsigned int hdrsize;
2864 hdrsize = mach_o_wide_p (header) ?
2865 BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE;
2867 mdata->header = *header;
2869 abfd->flags = abfd->flags & BFD_IN_MEMORY;
2870 switch (header->filetype)
2872 case BFD_MACH_O_MH_OBJECT:
2873 abfd->flags |= HAS_RELOC;
2875 case BFD_MACH_O_MH_EXECUTE:
2876 abfd->flags |= EXEC_P;
2878 case BFD_MACH_O_MH_DYLIB:
2879 case BFD_MACH_O_MH_BUNDLE:
2880 abfd->flags |= DYNAMIC;
2884 abfd->tdata.mach_o_data = mdata;
2886 bfd_mach_o_convert_architecture (header->cputype, header->cpusubtype,
2887 &cputype, &cpusubtype);
2888 if (cputype == bfd_arch_unknown)
2890 (*_bfd_error_handler)
2891 (_("bfd_mach_o_scan: unknown architecture 0x%lx/0x%lx"),
2892 header->cputype, header->cpusubtype);
2896 bfd_set_arch_mach (abfd, cputype, cpusubtype);
2898 if (header->ncmds != 0)
2900 mdata->commands = bfd_alloc
2901 (abfd, header->ncmds * sizeof (bfd_mach_o_load_command));
2902 if (mdata->commands == NULL)
2905 for (i = 0; i < header->ncmds; i++)
2907 bfd_mach_o_load_command *cur = &mdata->commands[i];
2910 cur->offset = hdrsize;
2913 bfd_mach_o_load_command *prev = &mdata->commands[i - 1];
2914 cur->offset = prev->offset + prev->len;
2917 if (bfd_mach_o_read_command (abfd, cur) < 0)
2922 if (bfd_mach_o_scan_start_address (abfd) < 0)
2925 bfd_mach_o_flatten_sections (abfd);
2930 bfd_mach_o_mkobject_init (bfd *abfd)
2932 bfd_mach_o_data_struct *mdata = NULL;
2934 mdata = bfd_alloc (abfd, sizeof (bfd_mach_o_data_struct));
2937 abfd->tdata.mach_o_data = mdata;
2939 mdata->header.magic = 0;
2940 mdata->header.cputype = 0;
2941 mdata->header.cpusubtype = 0;
2942 mdata->header.filetype = 0;
2943 mdata->header.ncmds = 0;
2944 mdata->header.sizeofcmds = 0;
2945 mdata->header.flags = 0;
2946 mdata->header.byteorder = BFD_ENDIAN_UNKNOWN;
2947 mdata->commands = NULL;
2949 mdata->sections = NULL;
2955 bfd_mach_o_gen_mkobject (bfd *abfd)
2957 bfd_mach_o_data_struct *mdata;
2959 if (!bfd_mach_o_mkobject_init (abfd))
2962 mdata = bfd_mach_o_get_data (abfd);
2963 mdata->header.magic = BFD_MACH_O_MH_MAGIC;
2964 mdata->header.cputype = 0;
2965 mdata->header.cpusubtype = 0;
2966 mdata->header.byteorder = abfd->xvec->byteorder;
2967 mdata->header.version = 1;
2973 bfd_mach_o_header_p (bfd *abfd,
2974 bfd_mach_o_filetype filetype,
2975 bfd_mach_o_cpu_type cputype)
2977 struct bfd_preserve preserve;
2978 bfd_mach_o_header header;
2980 preserve.marker = NULL;
2981 if (!bfd_mach_o_read_header (abfd, &header))
2984 if (! (header.byteorder == BFD_ENDIAN_BIG
2985 || header.byteorder == BFD_ENDIAN_LITTLE))
2987 (*_bfd_error_handler) (_("unknown header byte-order value 0x%lx"),
2988 (unsigned long) header.byteorder);
2992 if (! ((header.byteorder == BFD_ENDIAN_BIG
2993 && abfd->xvec->byteorder == BFD_ENDIAN_BIG
2994 && abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
2995 || (header.byteorder == BFD_ENDIAN_LITTLE
2996 && abfd->xvec->byteorder == BFD_ENDIAN_LITTLE
2997 && abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)))
3000 /* Check cputype and filetype.
3001 In case of wildcard, do not accept magics that are handled by existing
3005 if (header.cputype != cputype)
3010 switch (header.cputype)
3012 case BFD_MACH_O_CPU_TYPE_I386:
3013 /* Handled by mach-o-i386 */
3021 if (header.filetype != filetype)
3026 switch (header.filetype)
3028 case BFD_MACH_O_MH_CORE:
3029 /* Handled by core_p */
3036 preserve.marker = bfd_zalloc (abfd, sizeof (bfd_mach_o_data_struct));
3037 if (preserve.marker == NULL
3038 || !bfd_preserve_save (abfd, &preserve))
3041 if (!bfd_mach_o_scan (abfd, &header,
3042 (bfd_mach_o_data_struct *) preserve.marker))
3045 bfd_preserve_finish (abfd, &preserve);
3049 bfd_set_error (bfd_error_wrong_format);
3052 if (preserve.marker != NULL)
3053 bfd_preserve_restore (abfd, &preserve);
3057 static const bfd_target *
3058 bfd_mach_o_gen_object_p (bfd *abfd)
3060 return bfd_mach_o_header_p (abfd, 0, 0);
3063 static const bfd_target *
3064 bfd_mach_o_gen_core_p (bfd *abfd)
3066 return bfd_mach_o_header_p (abfd, BFD_MACH_O_MH_CORE, 0);
3069 typedef struct mach_o_fat_archentry
3071 unsigned long cputype;
3072 unsigned long cpusubtype;
3073 unsigned long offset;
3075 unsigned long align;
3076 } mach_o_fat_archentry;
3078 typedef struct mach_o_fat_data_struct
3080 unsigned long magic;
3081 unsigned long nfat_arch;
3082 mach_o_fat_archentry *archentries;
3083 } mach_o_fat_data_struct;
3086 bfd_mach_o_archive_p (bfd *abfd)
3088 mach_o_fat_data_struct *adata = NULL;
3089 struct mach_o_fat_header_external hdr;
3092 if (bfd_seek (abfd, 0, SEEK_SET) != 0
3093 || bfd_bread (&hdr, sizeof (hdr), abfd) != sizeof (hdr))
3096 adata = bfd_alloc (abfd, sizeof (mach_o_fat_data_struct));
3100 adata->magic = bfd_getb32 (hdr.magic);
3101 adata->nfat_arch = bfd_getb32 (hdr.nfat_arch);
3102 if (adata->magic != 0xcafebabe)
3104 /* Avoid matching Java bytecode files, which have the same magic number.
3105 In the Java bytecode file format this field contains the JVM version,
3106 which starts at 43.0. */
3107 if (adata->nfat_arch > 30)
3110 adata->archentries =
3111 bfd_alloc (abfd, adata->nfat_arch * sizeof (mach_o_fat_archentry));
3112 if (adata->archentries == NULL)
3115 for (i = 0; i < adata->nfat_arch; i++)
3117 struct mach_o_fat_arch_external arch;
3118 if (bfd_bread (&arch, sizeof (arch), abfd) != sizeof (arch))
3120 adata->archentries[i].cputype = bfd_getb32 (arch.cputype);
3121 adata->archentries[i].cpusubtype = bfd_getb32 (arch.cpusubtype);
3122 adata->archentries[i].offset = bfd_getb32 (arch.offset);
3123 adata->archentries[i].size = bfd_getb32 (arch.size);
3124 adata->archentries[i].align = bfd_getb32 (arch.align);
3127 abfd->tdata.mach_o_fat_data = adata;
3132 bfd_release (abfd, adata);
3133 bfd_set_error (bfd_error_wrong_format);
3138 bfd_mach_o_openr_next_archived_file (bfd *archive, bfd *prev)
3140 mach_o_fat_data_struct *adata;
3141 mach_o_fat_archentry *entry = NULL;
3144 enum bfd_architecture arch_type;
3145 unsigned long arch_subtype;
3147 adata = (mach_o_fat_data_struct *) archive->tdata.mach_o_fat_data;
3148 BFD_ASSERT (adata != NULL);
3150 /* Find index of previous entry. */
3152 i = 0; /* Start at first one. */
3155 for (i = 0; i < adata->nfat_arch; i++)
3157 if (adata->archentries[i].offset == prev->origin)
3161 if (i == adata->nfat_arch)
3164 bfd_set_error (bfd_error_bad_value);
3167 i++; /* Get next entry. */
3170 if (i >= adata->nfat_arch)
3172 bfd_set_error (bfd_error_no_more_archived_files);
3176 entry = &adata->archentries[i];
3177 nbfd = _bfd_new_bfd_contained_in (archive);
3181 nbfd->origin = entry->offset;
3183 bfd_mach_o_convert_architecture (entry->cputype, entry->cpusubtype,
3184 &arch_type, &arch_subtype);
3186 /* Create the member filename. Use ARCH_NAME. */
3187 nbfd->filename = bfd_printable_arch_mach (arch_type, arch_subtype);
3188 nbfd->iostream = NULL;
3189 bfd_set_arch_mach (nbfd, arch_type, arch_subtype);
3194 /* If ABFD format is FORMAT and architecture is ARCH, return it.
3195 If ABFD is a fat image containing a member that corresponds to FORMAT
3196 and ARCH, returns it.
3197 In other case, returns NULL.
3198 This function allows transparent uses of fat images. */
3200 bfd_mach_o_fat_extract (bfd *abfd,
3202 const bfd_arch_info_type *arch)
3205 mach_o_fat_data_struct *adata;
3208 if (bfd_check_format (abfd, format))
3210 if (bfd_get_arch_info (abfd) == arch)
3214 if (!bfd_check_format (abfd, bfd_archive)
3215 || abfd->xvec != &mach_o_fat_vec)
3218 /* This is a Mach-O fat image. */
3219 adata = (mach_o_fat_data_struct *) abfd->tdata.mach_o_fat_data;
3220 BFD_ASSERT (adata != NULL);
3222 for (i = 0; i < adata->nfat_arch; i++)
3224 struct mach_o_fat_archentry *e = &adata->archentries[i];
3225 enum bfd_architecture cpu_type;
3226 unsigned long cpu_subtype;
3228 bfd_mach_o_convert_architecture (e->cputype, e->cpusubtype,
3229 &cpu_type, &cpu_subtype);
3230 if (cpu_type != arch->arch || cpu_subtype != arch->mach)
3233 /* The architecture is found. */
3234 res = _bfd_new_bfd_contained_in (abfd);
3238 res->origin = e->offset;
3240 res->filename = bfd_printable_arch_mach (cpu_type, cpu_subtype);
3241 res->iostream = NULL;
3243 if (bfd_check_format (res, format))
3245 BFD_ASSERT (bfd_get_arch_info (res) == arch);
3256 bfd_mach_o_lookup_command (bfd *abfd,
3257 bfd_mach_o_load_command_type type,
3258 bfd_mach_o_load_command **mcommand)
3260 struct mach_o_data_struct *md = bfd_mach_o_get_data (abfd);
3261 bfd_mach_o_load_command *ncmd = NULL;
3262 unsigned int i, num;
3264 BFD_ASSERT (md != NULL);
3265 BFD_ASSERT (mcommand != NULL);
3268 for (i = 0; i < md->header.ncmds; i++)
3270 struct bfd_mach_o_load_command *cmd = &md->commands[i];
3272 if (cmd->type != type)
3285 bfd_mach_o_stack_addr (enum bfd_mach_o_cpu_type type)
3289 case BFD_MACH_O_CPU_TYPE_MC680x0:
3291 case BFD_MACH_O_CPU_TYPE_MC88000:
3293 case BFD_MACH_O_CPU_TYPE_POWERPC:
3295 case BFD_MACH_O_CPU_TYPE_I386:
3297 case BFD_MACH_O_CPU_TYPE_SPARC:
3299 case BFD_MACH_O_CPU_TYPE_I860:
3301 case BFD_MACH_O_CPU_TYPE_HPPA:
3302 return 0xc0000000 - 0x04000000;
3308 const bfd_mach_o_xlat_name bfd_mach_o_section_type_name[] =
3310 { "regular", BFD_MACH_O_S_REGULAR},
3311 { "zerofill", BFD_MACH_O_S_ZEROFILL},
3312 { "cstring_literals", BFD_MACH_O_S_CSTRING_LITERALS},
3313 { "4byte_literals", BFD_MACH_O_S_4BYTE_LITERALS},
3314 { "8byte_literals", BFD_MACH_O_S_8BYTE_LITERALS},
3315 { "literal_pointers", BFD_MACH_O_S_LITERAL_POINTERS},
3316 { "non_lazy_symbol_pointers", BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS},
3317 { "lazy_symbol_pointers", BFD_MACH_O_S_LAZY_SYMBOL_POINTERS},
3318 { "symbol_stubs", BFD_MACH_O_S_SYMBOL_STUBS},
3319 { "mod_init_func_pointers", BFD_MACH_O_S_MOD_INIT_FUNC_POINTERS},
3320 { "mod_fini_func_pointers", BFD_MACH_O_S_MOD_FINI_FUNC_POINTERS},
3321 { "coalesced", BFD_MACH_O_S_COALESCED},
3322 { "gb_zerofill", BFD_MACH_O_S_GB_ZEROFILL},
3323 { "interposing", BFD_MACH_O_S_INTERPOSING},
3324 { "16byte_literals", BFD_MACH_O_S_16BYTE_LITERALS},
3325 { "dtrace_dof", BFD_MACH_O_S_DTRACE_DOF},
3326 { "lazy_dylib_symbol_pointers", BFD_MACH_O_S_LAZY_DYLIB_SYMBOL_POINTERS},
3330 const bfd_mach_o_xlat_name bfd_mach_o_section_attribute_name[] =
3332 { "loc_reloc", BFD_MACH_O_S_ATTR_LOC_RELOC },
3333 { "ext_reloc", BFD_MACH_O_S_ATTR_EXT_RELOC },
3334 { "some_instructions", BFD_MACH_O_S_ATTR_SOME_INSTRUCTIONS },
3335 { "debug", BFD_MACH_O_S_ATTR_DEBUG },
3336 { "modifying_code", BFD_MACH_O_S_SELF_MODIFYING_CODE },
3337 { "live_support", BFD_MACH_O_S_ATTR_LIVE_SUPPORT },
3338 { "no_dead_strip", BFD_MACH_O_S_ATTR_NO_DEAD_STRIP },
3339 { "strip_static_syms", BFD_MACH_O_S_ATTR_STRIP_STATIC_SYMS },
3340 { "no_toc", BFD_MACH_O_S_ATTR_NO_TOC },
3341 { "pure_instructions", BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS },
3345 /* Get the section type from NAME. Return -1 if NAME is unknown. */
3348 bfd_mach_o_get_section_type_from_name (const char *name)
3350 const bfd_mach_o_xlat_name *x;
3352 for (x = bfd_mach_o_section_type_name; x->name; x++)
3353 if (strcmp (x->name, name) == 0)
3355 return (unsigned int)-1;
3358 /* Get the section attribute from NAME. Return -1 if NAME is unknown. */
3361 bfd_mach_o_get_section_attribute_from_name (const char *name)
3363 const bfd_mach_o_xlat_name *x;
3365 for (x = bfd_mach_o_section_attribute_name; x->name; x++)
3366 if (strcmp (x->name, name) == 0)
3368 return (unsigned int)-1;
3372 bfd_mach_o_core_fetch_environment (bfd *abfd,
3373 unsigned char **rbuf,
3376 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
3377 unsigned long stackaddr = bfd_mach_o_stack_addr (mdata->header.cputype);
3380 for (i = 0; i < mdata->header.ncmds; i++)
3382 bfd_mach_o_load_command *cur = &mdata->commands[i];
3383 bfd_mach_o_segment_command *seg = NULL;
3385 if (cur->type != BFD_MACH_O_LC_SEGMENT)
3388 seg = &cur->command.segment;
3390 if ((seg->vmaddr + seg->vmsize) == stackaddr)
3392 unsigned long start = seg->fileoff;
3393 unsigned long end = seg->fileoff + seg->filesize;
3394 unsigned char *buf = bfd_malloc (1024);
3395 unsigned long size = 1024;
3399 bfd_size_type nread = 0;
3400 unsigned long offset;
3401 int found_nonnull = 0;
3403 if (size > (end - start))
3404 size = (end - start);
3406 buf = bfd_realloc_or_free (buf, size);
3410 if (bfd_seek (abfd, end - size, SEEK_SET) != 0)
3416 nread = bfd_bread (buf, size, abfd);
3424 for (offset = 4; offset <= size; offset += 4)
3428 val = *((unsigned long *) (buf + size - offset));
3429 if (! found_nonnull)
3434 else if (val == 0x0)
3436 unsigned long bottom;
3439 bottom = seg->fileoff + seg->filesize - offset;
3440 top = seg->fileoff + seg->filesize - 4;
3441 *rbuf = bfd_malloc (top - bottom);
3442 *rlen = top - bottom;
3444 memcpy (*rbuf, buf + size - *rlen, *rlen);
3450 if (size == (end - start))
3464 bfd_mach_o_core_file_failing_command (bfd *abfd)
3466 unsigned char *buf = NULL;
3467 unsigned int len = 0;
3470 ret = bfd_mach_o_core_fetch_environment (abfd, &buf, &len);
3474 return (char *) buf;
3478 bfd_mach_o_core_file_failing_signal (bfd *abfd ATTRIBUTE_UNUSED)
3483 #define bfd_mach_o_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
3484 #define bfd_mach_o_bfd_reloc_name_lookup _bfd_norelocs_bfd_reloc_name_lookup
3486 #define bfd_mach_o_swap_reloc_in NULL
3487 #define bfd_mach_o_swap_reloc_out NULL
3488 #define bfd_mach_o_print_thread NULL
3490 #define TARGET_NAME mach_o_be_vec
3491 #define TARGET_STRING "mach-o-be"
3492 #define TARGET_ARCHITECTURE bfd_arch_unknown
3493 #define TARGET_BIG_ENDIAN 1
3494 #define TARGET_ARCHIVE 0
3495 #include "mach-o-target.c"
3498 #undef TARGET_STRING
3499 #undef TARGET_ARCHITECTURE
3500 #undef TARGET_BIG_ENDIAN
3501 #undef TARGET_ARCHIVE
3503 #define TARGET_NAME mach_o_le_vec
3504 #define TARGET_STRING "mach-o-le"
3505 #define TARGET_ARCHITECTURE bfd_arch_unknown
3506 #define TARGET_BIG_ENDIAN 0
3507 #define TARGET_ARCHIVE 0
3509 #include "mach-o-target.c"
3512 #undef TARGET_STRING
3513 #undef TARGET_ARCHITECTURE
3514 #undef TARGET_BIG_ENDIAN
3515 #undef TARGET_ARCHIVE
3517 /* Not yet handled: creating an archive. */
3518 #define bfd_mach_o_mkarchive _bfd_noarchive_mkarchive
3521 #define bfd_mach_o_read_ar_hdr _bfd_noarchive_read_ar_hdr
3522 #define bfd_mach_o_write_ar_hdr _bfd_noarchive_write_ar_hdr
3523 #define bfd_mach_o_slurp_armap _bfd_noarchive_slurp_armap
3524 #define bfd_mach_o_slurp_extended_name_table _bfd_noarchive_slurp_extended_name_table
3525 #define bfd_mach_o_construct_extended_name_table _bfd_noarchive_construct_extended_name_table
3526 #define bfd_mach_o_truncate_arname _bfd_noarchive_truncate_arname
3527 #define bfd_mach_o_write_armap _bfd_noarchive_write_armap
3528 #define bfd_mach_o_get_elt_at_index _bfd_noarchive_get_elt_at_index
3529 #define bfd_mach_o_generic_stat_arch_elt _bfd_noarchive_generic_stat_arch_elt
3530 #define bfd_mach_o_update_armap_timestamp _bfd_noarchive_update_armap_timestamp
3532 #define TARGET_NAME mach_o_fat_vec
3533 #define TARGET_STRING "mach-o-fat"
3534 #define TARGET_ARCHITECTURE bfd_arch_unknown
3535 #define TARGET_BIG_ENDIAN 1
3536 #define TARGET_ARCHIVE 1
3538 #include "mach-o-target.c"
3541 #undef TARGET_STRING
3542 #undef TARGET_ARCHITECTURE
3543 #undef TARGET_BIG_ENDIAN
3544 #undef TARGET_ARCHIVE