1 /* Mach-O support for BFD.
2 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
26 #include "libiberty.h"
27 #include "aout/stab_gnu.h"
30 #define bfd_mach_o_object_p bfd_mach_o_gen_object_p
31 #define bfd_mach_o_core_p bfd_mach_o_gen_core_p
32 #define bfd_mach_o_mkobject bfd_false
35 bfd_mach_o_version (bfd *abfd)
37 bfd_mach_o_data_struct *mdata = NULL;
39 BFD_ASSERT (bfd_mach_o_valid (abfd));
40 mdata = abfd->tdata.mach_o_data;
42 return mdata->header.version;
46 bfd_mach_o_valid (bfd *abfd)
48 if (abfd == NULL || abfd->xvec == NULL)
51 if (abfd->xvec->flavour != bfd_target_mach_o_flavour)
54 if (abfd->tdata.mach_o_data == NULL)
59 static INLINE bfd_boolean
60 mach_o_wide_p (bfd_mach_o_header *header)
62 switch (header->version)
74 static INLINE bfd_boolean
75 bfd_mach_o_wide_p (bfd *abfd)
77 return mach_o_wide_p (&abfd->tdata.mach_o_data->header);
80 /* Tables to translate well known Mach-O segment/section names to bfd
81 names. Use of canonical names (such as .text or .debug_frame) is required
84 struct mach_o_section_name_xlat
87 const char *mach_o_name;
90 static const struct mach_o_section_name_xlat dwarf_section_names_xlat[] =
92 { ".debug_frame", "__debug_frame" },
93 { ".debug_info", "__debug_info" },
94 { ".debug_abbrev", "__debug_abbrev" },
95 { ".debug_aranges", "__debug_aranges" },
96 { ".debug_macinfo", "__debug_macinfo" },
97 { ".debug_line", "__debug_line" },
98 { ".debug_loc", "__debug_loc" },
99 { ".debug_pubnames", "__debug_pubnames" },
100 { ".debug_pubtypes", "__debug_pubtypes" },
101 { ".debug_str", "__debug_str" },
102 { ".debug_ranges", "__debug_ranges" },
106 static const struct mach_o_section_name_xlat text_section_names_xlat[] =
108 { ".text", "__text" },
109 { ".cstring", "__cstring" },
110 { ".eh_frame", "__eh_frame" },
114 static const struct mach_o_section_name_xlat data_section_names_xlat[] =
116 { ".data", "__data" },
121 struct mach_o_segment_name_xlat
124 const struct mach_o_section_name_xlat *sections;
127 static const struct mach_o_segment_name_xlat segsec_names_xlat[] =
129 { "__DWARF", dwarf_section_names_xlat },
130 { "__TEXT", text_section_names_xlat },
131 { "__DATA", data_section_names_xlat },
136 /* Mach-O to bfd names. */
139 bfd_mach_o_convert_section_name_to_bfd (bfd *abfd, bfd_mach_o_section *section)
141 const struct mach_o_segment_name_xlat *seg;
144 const char *pfx = "";
146 for (seg = segsec_names_xlat; seg->segname; seg++)
148 if (strcmp (seg->segname, section->segname) == 0)
150 const struct mach_o_section_name_xlat *sec;
152 for (sec = seg->sections; sec->mach_o_name; sec++)
154 if (strcmp (sec->mach_o_name, section->sectname) == 0)
156 len = strlen (sec->bfd_name);
157 res = bfd_alloc (abfd, len + 1);
161 strcpy (res, sec->bfd_name);
168 len = strlen (section->segname) + 1
169 + strlen (section->sectname) + 1;
171 /* Put "LC_SEGMENT." prefix if the segment name is weird (ie doesn't start
172 with an underscore. */
173 if (section->segname[0] != '_')
175 static const char seg_pfx[] = "LC_SEGMENT.";
178 len += sizeof (seg_pfx) - 1;
181 res = bfd_alloc (abfd, len);
184 snprintf (res, len, "%s%s.%s", pfx, section->segname, section->sectname);
188 /* Convert a bfd section name to a Mach-O segment + section name. */
191 bfd_mach_o_convert_section_name_to_mach_o (bfd *abfd ATTRIBUTE_UNUSED,
193 bfd_mach_o_section *section)
195 const struct mach_o_segment_name_xlat *seg;
196 const char *name = bfd_get_section_name (abfd, sect);
202 /* List of well known names. They all start with a dot. */
204 for (seg = segsec_names_xlat; seg->segname; seg++)
206 const struct mach_o_section_name_xlat *sec;
208 for (sec = seg->sections; sec->mach_o_name; sec++)
210 if (strcmp (sec->bfd_name, name) == 0)
212 strcpy (section->segname, seg->segname);
213 strcpy (section->sectname, sec->mach_o_name);
219 /* Strip LC_SEGMENT. prefix. */
220 if (strncmp (name, "LC_SEGMENT.", 11) == 0)
224 dot = strchr (name, '.');
227 /* Try to split name into segment and section names. */
228 if (dot && dot != name)
231 seclen = len - (dot + 1 - name);
233 if (seglen < 16 && seclen < 16)
235 memcpy (section->segname, name, seglen);
236 section->segname[seglen] = 0;
237 memcpy (section->sectname, dot + 1, seclen);
238 section->sectname[seclen] = 0;
245 memcpy (section->segname, name, len);
246 section->segname[len] = 0;
247 memcpy (section->sectname, name, len);
248 section->sectname[len] = 0;
251 /* Copy any private info we understand from the input symbol
252 to the output symbol. */
255 bfd_mach_o_bfd_copy_private_symbol_data (bfd *ibfd ATTRIBUTE_UNUSED,
256 asymbol *isymbol ATTRIBUTE_UNUSED,
257 bfd *obfd ATTRIBUTE_UNUSED,
258 asymbol *osymbol ATTRIBUTE_UNUSED)
263 /* Copy any private info we understand from the input section
264 to the output section. */
267 bfd_mach_o_bfd_copy_private_section_data (bfd *ibfd ATTRIBUTE_UNUSED,
268 asection *isection ATTRIBUTE_UNUSED,
269 bfd *obfd ATTRIBUTE_UNUSED,
270 asection *osection ATTRIBUTE_UNUSED)
275 /* Copy any private info we understand from the input bfd
276 to the output bfd. */
279 bfd_mach_o_bfd_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
281 if (bfd_get_flavour (ibfd) != bfd_target_mach_o_flavour
282 || bfd_get_flavour (obfd) != bfd_target_mach_o_flavour)
285 BFD_ASSERT (bfd_mach_o_valid (ibfd));
286 BFD_ASSERT (bfd_mach_o_valid (obfd));
288 /* FIXME: copy commands. */
293 /* Count the total number of symbols. Traverse all sections. */
296 bfd_mach_o_count_symbols (bfd *abfd)
298 bfd_mach_o_data_struct *mdata = NULL;
302 BFD_ASSERT (bfd_mach_o_valid (abfd));
303 mdata = abfd->tdata.mach_o_data;
305 for (i = 0; i < mdata->header.ncmds; i++)
306 if (mdata->commands[i].type == BFD_MACH_O_LC_SYMTAB)
308 bfd_mach_o_symtab_command *sym = &mdata->commands[i].command.symtab;
316 bfd_mach_o_get_symtab_upper_bound (bfd *abfd)
318 long nsyms = bfd_mach_o_count_symbols (abfd);
323 return ((nsyms + 1) * sizeof (asymbol *));
327 bfd_mach_o_canonicalize_symtab (bfd *abfd, asymbol **alocation)
329 bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
330 long nsyms = bfd_mach_o_count_symbols (abfd);
331 asymbol **csym = alocation;
337 for (i = 0; i < mdata->header.ncmds; i++)
339 if (mdata->commands[i].type == BFD_MACH_O_LC_SYMTAB)
341 bfd_mach_o_symtab_command *sym = &mdata->commands[i].command.symtab;
343 if (bfd_mach_o_scan_read_symtab_symbols (abfd, &mdata->commands[i].command.symtab) != 0)
345 fprintf (stderr, "bfd_mach_o_canonicalize_symtab: unable to load symbols for section %lu\n", i);
349 BFD_ASSERT (sym->symbols != NULL);
351 for (j = 0; j < sym->nsyms; j++)
353 BFD_ASSERT (csym < (alocation + nsyms));
354 *csym++ = &sym->symbols[j];
365 bfd_mach_o_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
369 bfd_symbol_info (symbol, ret);
373 bfd_mach_o_print_symbol (bfd *abfd,
376 bfd_print_symbol_type how)
378 FILE *file = (FILE *) afile;
386 case bfd_print_symbol_name:
387 fprintf (file, "%s", symbol->name);
390 bfd_print_symbol_vandf (abfd, (PTR) file, symbol);
391 ntype = BFD_MACH_O_SYM_NTYPE (symbol);
392 nsect = BFD_MACH_O_SYM_NSECT (symbol);
393 ndesc = BFD_MACH_O_SYM_NDESC (symbol);
394 if (ntype & BFD_MACH_O_N_STAB)
395 name = bfd_get_stab_name (ntype);
397 switch (ntype & BFD_MACH_O_N_TYPE)
399 case BFD_MACH_O_N_UNDF:
402 case BFD_MACH_O_N_ABS:
405 case BFD_MACH_O_N_INDR:
408 case BFD_MACH_O_N_PBUD:
411 case BFD_MACH_O_N_SECT:
420 fprintf (file, " %02x %-6s %02x %04x", ntype, name, nsect, ndesc);
421 if ((ntype & BFD_MACH_O_N_STAB) == 0
422 && (ntype & BFD_MACH_O_N_TYPE) == BFD_MACH_O_N_SECT)
423 fprintf (file, " %-5s", symbol->section->name);
424 fprintf (file, " %s", symbol->name);
429 bfd_mach_o_convert_architecture (bfd_mach_o_cpu_type mtype,
430 bfd_mach_o_cpu_subtype msubtype ATTRIBUTE_UNUSED,
431 enum bfd_architecture *type,
432 unsigned long *subtype)
434 *subtype = bfd_arch_unknown;
438 case BFD_MACH_O_CPU_TYPE_VAX: *type = bfd_arch_vax; break;
439 case BFD_MACH_O_CPU_TYPE_MC680x0: *type = bfd_arch_m68k; break;
440 case BFD_MACH_O_CPU_TYPE_I386:
441 *type = bfd_arch_i386;
442 *subtype = bfd_mach_i386_i386;
444 case BFD_MACH_O_CPU_TYPE_X86_64:
445 *type = bfd_arch_i386;
446 *subtype = bfd_mach_x86_64;
448 case BFD_MACH_O_CPU_TYPE_MIPS: *type = bfd_arch_mips; break;
449 case BFD_MACH_O_CPU_TYPE_MC98000: *type = bfd_arch_m98k; break;
450 case BFD_MACH_O_CPU_TYPE_HPPA: *type = bfd_arch_hppa; break;
451 case BFD_MACH_O_CPU_TYPE_ARM: *type = bfd_arch_arm; break;
452 case BFD_MACH_O_CPU_TYPE_MC88000: *type = bfd_arch_m88k; break;
453 case BFD_MACH_O_CPU_TYPE_SPARC:
454 *type = bfd_arch_sparc;
455 *subtype = bfd_mach_sparc;
457 case BFD_MACH_O_CPU_TYPE_I860: *type = bfd_arch_i860; break;
458 case BFD_MACH_O_CPU_TYPE_ALPHA: *type = bfd_arch_alpha; break;
459 case BFD_MACH_O_CPU_TYPE_POWERPC:
460 *type = bfd_arch_powerpc;
461 *subtype = bfd_mach_ppc;
463 case BFD_MACH_O_CPU_TYPE_POWERPC_64:
464 *type = bfd_arch_powerpc;
465 *subtype = bfd_mach_ppc64;
468 *type = bfd_arch_unknown;
474 bfd_mach_o_write_header (bfd *abfd, bfd_mach_o_header *header)
476 unsigned char buf[32];
479 size = mach_o_wide_p (header) ?
480 BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE;
482 bfd_h_put_32 (abfd, header->magic, buf + 0);
483 bfd_h_put_32 (abfd, header->cputype, buf + 4);
484 bfd_h_put_32 (abfd, header->cpusubtype, buf + 8);
485 bfd_h_put_32 (abfd, header->filetype, buf + 12);
486 bfd_h_put_32 (abfd, header->ncmds, buf + 16);
487 bfd_h_put_32 (abfd, header->sizeofcmds, buf + 20);
488 bfd_h_put_32 (abfd, header->flags, buf + 24);
490 if (mach_o_wide_p (header))
491 bfd_h_put_32 (abfd, header->reserved, buf + 28);
493 if (bfd_seek (abfd, 0, SEEK_SET) != 0
494 || bfd_bwrite ((PTR) buf, size, abfd) != size)
501 bfd_mach_o_scan_write_thread (bfd *abfd, bfd_mach_o_load_command *command)
503 bfd_mach_o_thread_command *cmd = &command->command.thread;
505 unsigned char buf[8];
507 unsigned int nflavours;
509 BFD_ASSERT ((command->type == BFD_MACH_O_LC_THREAD)
510 || (command->type == BFD_MACH_O_LC_UNIXTHREAD));
514 for (i = 0; i < cmd->nflavours; i++)
516 BFD_ASSERT ((cmd->flavours[i].size % 4) == 0);
517 BFD_ASSERT (cmd->flavours[i].offset == (command->offset + offset + 8));
519 bfd_h_put_32 (abfd, cmd->flavours[i].flavour, buf);
520 bfd_h_put_32 (abfd, (cmd->flavours[i].size / 4), buf + 4);
522 if (bfd_seek (abfd, command->offset + offset, SEEK_SET) != 0
523 || bfd_bwrite ((PTR) buf, 8, abfd) != 8)
526 offset += cmd->flavours[i].size + 8;
533 bfd_mach_o_scan_write_section_32 (bfd *abfd,
534 bfd_mach_o_section *section,
537 unsigned char buf[BFD_MACH_O_SECTION_SIZE];
539 memcpy (buf, section->sectname, 16);
540 memcpy (buf + 16, section->segname, 16);
541 bfd_h_put_32 (abfd, section->addr, buf + 32);
542 bfd_h_put_32 (abfd, section->size, buf + 36);
543 bfd_h_put_32 (abfd, section->offset, buf + 40);
544 bfd_h_put_32 (abfd, section->align, buf + 44);
545 bfd_h_put_32 (abfd, section->reloff, buf + 48);
546 bfd_h_put_32 (abfd, section->nreloc, buf + 52);
547 bfd_h_put_32 (abfd, section->flags, buf + 56);
548 bfd_h_put_32 (abfd, section->reserved1, buf + 60);
549 bfd_h_put_32 (abfd, section->reserved2, buf + 64);
551 if (bfd_seek (abfd, offset, SEEK_SET) != 0
552 || (bfd_bwrite ((PTR) buf, BFD_MACH_O_SECTION_SIZE, abfd)
553 != BFD_MACH_O_SECTION_SIZE))
560 bfd_mach_o_scan_write_section_64 (bfd *abfd,
561 bfd_mach_o_section *section,
564 unsigned char buf[BFD_MACH_O_SECTION_64_SIZE];
566 memcpy (buf, section->sectname, 16);
567 memcpy (buf + 16, section->segname, 16);
568 bfd_h_put_64 (abfd, section->addr, buf + 32);
569 bfd_h_put_64 (abfd, section->size, buf + 40);
570 bfd_h_put_32 (abfd, section->offset, buf + 48);
571 bfd_h_put_32 (abfd, section->align, buf + 52);
572 bfd_h_put_32 (abfd, section->reloff, buf + 56);
573 bfd_h_put_32 (abfd, section->nreloc, buf + 60);
574 bfd_h_put_32 (abfd, section->flags, buf + 64);
575 bfd_h_put_32 (abfd, section->reserved1, buf + 68);
576 bfd_h_put_32 (abfd, section->reserved2, buf + 72);
577 bfd_h_put_32 (abfd, section->reserved3, buf + 76);
579 if (bfd_seek (abfd, offset, SEEK_SET) != 0
580 || (bfd_bwrite ((PTR) buf, BFD_MACH_O_SECTION_64_SIZE, abfd)
581 != BFD_MACH_O_SECTION_64_SIZE))
588 bfd_mach_o_scan_write_segment_32 (bfd *abfd, bfd_mach_o_load_command *command)
590 unsigned char buf[BFD_MACH_O_LC_SEGMENT_SIZE];
591 bfd_mach_o_segment_command *seg = &command->command.segment;
594 BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT);
596 memcpy (buf, seg->segname, 16);
598 bfd_h_put_32 (abfd, seg->vmaddr, buf + 16);
599 bfd_h_put_32 (abfd, seg->vmsize, buf + 20);
600 bfd_h_put_32 (abfd, seg->fileoff, buf + 24);
601 bfd_h_put_32 (abfd, seg->filesize, buf + 28);
602 bfd_h_put_32 (abfd, seg->maxprot, buf + 32);
603 bfd_h_put_32 (abfd, seg->initprot, buf + 36);
604 bfd_h_put_32 (abfd, seg->nsects, buf + 40);
605 bfd_h_put_32 (abfd, seg->flags, buf + 44);
607 if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
608 || (bfd_bwrite ((PTR) buf, BFD_MACH_O_LC_SEGMENT_SIZE, abfd)
609 != BFD_MACH_O_LC_SEGMENT_SIZE))
612 for (i = 0; i < seg->nsects; i++)
614 bfd_vma segoff = command->offset + BFD_MACH_O_LC_SEGMENT_SIZE
615 + (i * BFD_MACH_O_SECTION_SIZE);
617 if (bfd_mach_o_scan_write_section_32 (abfd, &seg->sections[i], segoff))
624 bfd_mach_o_scan_write_segment_64 (bfd *abfd, bfd_mach_o_load_command *command)
626 unsigned char buf[BFD_MACH_O_LC_SEGMENT_64_SIZE];
627 bfd_mach_o_segment_command *seg = &command->command.segment;
630 BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT_64);
632 memcpy (buf, seg->segname, 16);
634 bfd_h_put_64 (abfd, seg->vmaddr, buf + 16);
635 bfd_h_put_64 (abfd, seg->vmsize, buf + 24);
636 bfd_h_put_64 (abfd, seg->fileoff, buf + 32);
637 bfd_h_put_64 (abfd, seg->filesize, buf + 40);
638 bfd_h_put_32 (abfd, seg->maxprot, buf + 48);
639 bfd_h_put_32 (abfd, seg->initprot, buf + 52);
640 bfd_h_put_32 (abfd, seg->nsects, buf + 56);
641 bfd_h_put_32 (abfd, seg->flags, buf + 60);
643 if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
644 || (bfd_bwrite ((PTR) buf, BFD_MACH_O_LC_SEGMENT_64_SIZE, abfd)
645 != BFD_MACH_O_LC_SEGMENT_64_SIZE))
648 for (i = 0; i < seg->nsects; i++)
650 bfd_vma segoff = command->offset + BFD_MACH_O_LC_SEGMENT_64_SIZE
651 + (i * BFD_MACH_O_SECTION_64_SIZE);
653 if (bfd_mach_o_scan_write_section_64 (abfd, &seg->sections[i], segoff))
660 bfd_mach_o_scan_write_symtab (bfd *abfd, bfd_mach_o_load_command *command)
662 bfd_mach_o_symtab_command *sym = &command->command.symtab;
663 unsigned char buf[16];
665 unsigned int wide = bfd_mach_o_wide_p (abfd);
666 unsigned int symlen = wide ? 16 : 12;
667 struct bfd_strtab_hash *strtab;
668 asymbol **symbols = bfd_get_outsymbols (abfd);
670 BFD_ASSERT (command->type == BFD_MACH_O_LC_SYMTAB);
672 /* Write the symbols first. */
673 if (bfd_seek (abfd, sym->symoff, SEEK_SET) != 0)
676 sym->nsyms = bfd_get_symcount (abfd);
678 strtab = _bfd_stringtab_init ();
682 for (i = 0; i < sym->nsyms; i++)
684 unsigned char buf[16];
689 asymbol *s = symbols[i];
692 /* An index of 0 always means the empty string. */
693 if (s->name == 0 || s->name[0] == '\0')
697 index = _bfd_stringtab_add (strtab, s->name, TRUE, FALSE);
698 if (index == (bfd_size_type) -1)
702 /* Get back-end specific values. */
703 ntype = BFD_MACH_O_SYM_NTYPE (s);
704 nsect = BFD_MACH_O_SYM_NSECT (s);
705 ndesc = BFD_MACH_O_SYM_NDESC (s);
707 if (ntype == BFD_MACH_O_N_UNDF && !(s->flags & BSF_DEBUGGING))
709 /* As genuine Mach-O symbols type shouldn't be N_UNDF (undefined
710 symbols should be N_UNDEF | N_EXT), we suppose the back-end
711 values haven't been set. */
712 if (s->flags & (BSF_LOCAL | BSF_GLOBAL))
714 if (s->section == bfd_abs_section_ptr)
715 ntype = BFD_MACH_O_N_ABS;
716 else if (s->section == bfd_und_section_ptr)
717 ntype = BFD_MACH_O_N_UNDF;
718 else if (s->section == bfd_com_section_ptr)
719 ntype = BFD_MACH_O_N_UNDF | BFD_MACH_O_N_EXT;
721 ntype = BFD_MACH_O_N_SECT;
723 ntype |= (s->flags & BSF_GLOBAL) ? BFD_MACH_O_N_EXT : 0;
727 /* Compute section index. */
728 if (s->section != bfd_abs_section_ptr
729 && s->section != bfd_und_section_ptr
730 && s->section != bfd_com_section_ptr)
731 nsect = s->section->target_index;
733 bfd_h_put_32 (abfd, index, buf);
734 bfd_h_put_8 (abfd, ntype, buf + 4);
735 bfd_h_put_8 (abfd, nsect, buf + 5);
736 bfd_h_put_16 (abfd, ndesc, buf + 6);
738 bfd_h_put_64 (abfd, s->section->vma + s->value, buf + 8);
740 bfd_h_put_32 (abfd, s->section->vma + s->value, buf + 8);
742 if (bfd_bwrite ((PTR) buf, symlen, abfd) != symlen)
745 sym->strsize = _bfd_stringtab_size (strtab);
746 sym->stroff = sym->symoff + sym->nsyms * symlen;
748 if (_bfd_stringtab_emit (abfd, strtab) != TRUE)
750 _bfd_stringtab_free (strtab);
753 bfd_h_put_32 (abfd, sym->symoff, buf);
754 bfd_h_put_32 (abfd, sym->nsyms, buf + 4);
755 bfd_h_put_32 (abfd, sym->stroff, buf + 8);
756 bfd_h_put_32 (abfd, sym->strsize, buf + 12);
758 if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
759 || bfd_bwrite ((PTR) buf, 16, abfd) != 16)
765 _bfd_stringtab_free (strtab);
770 bfd_mach_o_write_contents (bfd *abfd)
773 bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
775 /* Now write header information. */
776 if (mdata->header.filetype == 0)
778 if (abfd->flags & EXEC_P)
779 mdata->header.filetype = BFD_MACH_O_MH_EXECUTE;
780 else if (abfd->flags & DYNAMIC)
781 mdata->header.filetype = BFD_MACH_O_MH_DYLIB;
783 mdata->header.filetype = BFD_MACH_O_MH_OBJECT;
785 if (!bfd_mach_o_write_header (abfd, &mdata->header))
788 for (i = 0; i < mdata->header.ncmds; i++)
790 unsigned char buf[8];
791 bfd_mach_o_load_command *cur = &mdata->commands[i];
792 unsigned long typeflag;
794 typeflag = cur->type | (cur->type_required ? BFD_MACH_O_LC_REQ_DYLD : 0);
796 bfd_h_put_32 (abfd, typeflag, buf);
797 bfd_h_put_32 (abfd, cur->len, buf + 4);
799 if (bfd_seek (abfd, cur->offset, SEEK_SET) != 0
800 || bfd_bwrite ((PTR) buf, 8, abfd) != 8)
805 case BFD_MACH_O_LC_SEGMENT:
806 if (bfd_mach_o_scan_write_segment_32 (abfd, cur) != 0)
809 case BFD_MACH_O_LC_SEGMENT_64:
810 if (bfd_mach_o_scan_write_segment_64 (abfd, cur) != 0)
813 case BFD_MACH_O_LC_SYMTAB:
814 if (!bfd_mach_o_scan_write_symtab (abfd, cur))
817 case BFD_MACH_O_LC_SYMSEG:
819 case BFD_MACH_O_LC_THREAD:
820 case BFD_MACH_O_LC_UNIXTHREAD:
821 if (bfd_mach_o_scan_write_thread (abfd, cur) != 0)
824 case BFD_MACH_O_LC_LOADFVMLIB:
825 case BFD_MACH_O_LC_IDFVMLIB:
826 case BFD_MACH_O_LC_IDENT:
827 case BFD_MACH_O_LC_FVMFILE:
828 case BFD_MACH_O_LC_PREPAGE:
829 case BFD_MACH_O_LC_DYSYMTAB:
830 case BFD_MACH_O_LC_LOAD_DYLIB:
831 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB:
832 case BFD_MACH_O_LC_ID_DYLIB:
833 case BFD_MACH_O_LC_LOAD_DYLINKER:
834 case BFD_MACH_O_LC_ID_DYLINKER:
835 case BFD_MACH_O_LC_PREBOUND_DYLIB:
836 case BFD_MACH_O_LC_ROUTINES:
837 case BFD_MACH_O_LC_SUB_FRAMEWORK:
841 "unable to write unknown load command 0x%lx\n",
842 (unsigned long) cur->type);
850 /* Build Mach-O load commands from the sections. */
853 bfd_mach_o_build_commands (bfd *abfd)
855 bfd_mach_o_data_struct *mdata = bfd_get_mach_o_data (abfd);
856 unsigned int wide = mach_o_wide_p (&mdata->header);
857 bfd_mach_o_segment_command *seg;
858 bfd_mach_o_section *sections;
861 bfd_mach_o_load_command *cmd;
862 bfd_mach_o_load_command *symtab_cmd;
865 /* Return now if commands are already built. */
866 if (mdata->header.ncmds)
869 /* Very simple version: 1 command (segment) containing all sections. */
870 mdata->header.ncmds = 2;
871 mdata->commands = bfd_alloc (abfd, mdata->header.ncmds
872 * sizeof (bfd_mach_o_load_command));
873 if (mdata->commands == NULL)
875 cmd = &mdata->commands[0];
876 seg = &cmd->command.segment;
878 seg->nsects = bfd_count_sections (abfd);
879 sections = bfd_alloc (abfd, seg->nsects * sizeof (bfd_mach_o_section));
880 if (sections == NULL)
882 seg->sections = sections;
884 /* Set segment command. */
887 cmd->type = BFD_MACH_O_LC_SEGMENT_64;
888 cmd->offset = BFD_MACH_O_HEADER_64_SIZE;
889 cmd->len = BFD_MACH_O_LC_SEGMENT_64_SIZE
890 + BFD_MACH_O_SECTION_64_SIZE * seg->nsects;
894 cmd->type = BFD_MACH_O_LC_SEGMENT;
895 cmd->offset = BFD_MACH_O_HEADER_SIZE;
896 cmd->len = BFD_MACH_O_LC_SEGMENT_SIZE
897 + BFD_MACH_O_SECTION_SIZE * seg->nsects;
899 cmd->type_required = FALSE;
900 mdata->header.sizeofcmds = cmd->len;
901 filepos = cmd->offset + cmd->len;
903 /* Set symtab command. */
904 symtab_cmd = &mdata->commands[1];
906 symtab_cmd->type = BFD_MACH_O_LC_SYMTAB;
907 symtab_cmd->offset = cmd->offset + cmd->len;
908 symtab_cmd->len = 6 * 4;
909 symtab_cmd->type_required = FALSE;
911 mdata->header.sizeofcmds += symtab_cmd->len;
912 filepos += symtab_cmd->len;
914 /* Fill segment command. */
915 memset (seg->segname, 0, sizeof (seg->segname));
917 seg->fileoff = filepos;
919 seg->maxprot = BFD_MACH_O_PROT_READ | BFD_MACH_O_PROT_WRITE
920 | BFD_MACH_O_PROT_EXECUTE;
921 seg->initprot = seg->maxprot;
924 /* Create Mach-O sections. */
926 for (sec = abfd->sections; sec; sec = sec->next)
928 sections->bfdsection = sec;
929 bfd_mach_o_convert_section_name_to_mach_o (abfd, sec, sections);
930 sections->addr = bfd_get_section_vma (abfd, sec);
931 sections->size = bfd_get_section_size (sec);
932 sections->align = bfd_get_section_alignment (abfd, sec);
934 filepos = (filepos + ((file_ptr) 1 << sections->align) - 1)
935 & ((file_ptr) -1 << sections->align);
936 sections->offset = filepos;
937 sections->reloff = 0;
938 sections->nreloc = 0;
939 sections->reserved1 = 0;
940 sections->reserved2 = 0;
941 sections->reserved3 = 0;
943 sec->filepos = filepos;
944 sec->target_index = ++target_index;
946 filepos += sections->size;
949 seg->filesize = filepos - seg->fileoff;
950 seg->vmsize = seg->filesize;
952 /* Fill symtab command.
953 Note: we don't know the number of symbols.
954 Also, symtab is at the end since the length of the symbol table (and
955 string table) is not known. */
956 symtab_cmd->command.symtab.symoff = filepos;
961 /* Set the contents of a section. */
964 bfd_mach_o_set_section_contents (bfd *abfd,
966 const void * location,
972 /* This must be done first, because bfd_set_section_contents is
973 going to set output_has_begun to TRUE. */
974 if (! abfd->output_has_begun && ! bfd_mach_o_build_commands (abfd))
980 pos = section->filepos + offset;
981 if (bfd_seek (abfd, pos, SEEK_SET) != 0
982 || bfd_bwrite (location, count, abfd) != count)
989 bfd_mach_o_sizeof_headers (bfd *a ATTRIBUTE_UNUSED,
990 struct bfd_link_info *info ATTRIBUTE_UNUSED)
995 /* Make an empty symbol. This is required only because
996 bfd_make_section_anyway wants to create a symbol for the section. */
999 bfd_mach_o_make_empty_symbol (bfd *abfd)
1003 new = bfd_zalloc (abfd, sizeof (* new));
1006 new->the_bfd = abfd;
1011 bfd_mach_o_read_header (bfd *abfd, bfd_mach_o_header *header)
1013 unsigned char buf[32];
1015 bfd_vma (*get32) (const void *) = NULL;
1017 /* Just read the magic number. */
1018 if (bfd_seek (abfd, 0, SEEK_SET) != 0
1019 || bfd_bread ((PTR) buf, 4, abfd) != 4)
1022 if (bfd_getb32 (buf) == BFD_MACH_O_MH_MAGIC)
1024 header->byteorder = BFD_ENDIAN_BIG;
1025 header->magic = BFD_MACH_O_MH_MAGIC;
1026 header->version = 1;
1029 else if (bfd_getl32 (buf) == BFD_MACH_O_MH_MAGIC)
1031 header->byteorder = BFD_ENDIAN_LITTLE;
1032 header->magic = BFD_MACH_O_MH_MAGIC;
1033 header->version = 1;
1036 else if (bfd_getb32 (buf) == BFD_MACH_O_MH_MAGIC_64)
1038 header->byteorder = BFD_ENDIAN_BIG;
1039 header->magic = BFD_MACH_O_MH_MAGIC_64;
1040 header->version = 2;
1043 else if (bfd_getl32 (buf) == BFD_MACH_O_MH_MAGIC_64)
1045 header->byteorder = BFD_ENDIAN_LITTLE;
1046 header->magic = BFD_MACH_O_MH_MAGIC_64;
1047 header->version = 2;
1052 header->byteorder = BFD_ENDIAN_UNKNOWN;
1056 /* Once the size of the header is known, read the full header. */
1057 size = mach_o_wide_p (header) ?
1058 BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE;
1060 if (bfd_seek (abfd, 0, SEEK_SET) != 0
1061 || bfd_bread ((PTR) buf, size, abfd) != size)
1064 header->cputype = (*get32) (buf + 4);
1065 header->cpusubtype = (*get32) (buf + 8);
1066 header->filetype = (*get32) (buf + 12);
1067 header->ncmds = (*get32) (buf + 16);
1068 header->sizeofcmds = (*get32) (buf + 20);
1069 header->flags = (*get32) (buf + 24);
1071 if (mach_o_wide_p (header))
1072 header->reserved = (*get32) (buf + 28);
1078 bfd_mach_o_make_bfd_section (bfd *abfd, bfd_mach_o_section *section,
1085 sname = bfd_mach_o_convert_section_name_to_bfd (abfd, section);
1089 if (section->flags & BFD_MACH_O_S_ATTR_DEBUG)
1090 flags = SEC_HAS_CONTENTS | SEC_DEBUGGING;
1094 if ((section->flags & BFD_MACH_O_SECTION_TYPE_MASK)
1095 != BFD_MACH_O_S_ZEROFILL)
1097 flags |= SEC_HAS_CONTENTS | SEC_LOAD;
1098 if (prot & BFD_MACH_O_PROT_EXECUTE)
1100 if (prot & BFD_MACH_O_PROT_WRITE)
1102 else if (prot & BFD_MACH_O_PROT_READ)
1103 flags |= SEC_READONLY;
1106 bfdsec = bfd_make_section_anyway_with_flags (abfd, sname, flags);
1110 bfdsec->vma = section->addr;
1111 bfdsec->lma = section->addr;
1112 bfdsec->size = section->size;
1113 bfdsec->filepos = section->offset;
1114 bfdsec->alignment_power = section->align;
1115 bfdsec->segment_mark = 0;
1121 bfd_mach_o_scan_read_section_32 (bfd *abfd,
1122 bfd_mach_o_section *section,
1126 unsigned char buf[BFD_MACH_O_SECTION_SIZE];
1128 if (bfd_seek (abfd, offset, SEEK_SET) != 0
1129 || (bfd_bread ((PTR) buf, BFD_MACH_O_SECTION_SIZE, abfd)
1130 != BFD_MACH_O_SECTION_SIZE))
1133 memcpy (section->sectname, buf, 16);
1134 section->sectname[16] = '\0';
1135 memcpy (section->segname, buf + 16, 16);
1136 section->segname[16] = '\0';
1137 section->addr = bfd_h_get_32 (abfd, buf + 32);
1138 section->size = bfd_h_get_32 (abfd, buf + 36);
1139 section->offset = bfd_h_get_32 (abfd, buf + 40);
1140 section->align = bfd_h_get_32 (abfd, buf + 44);
1141 section->reloff = bfd_h_get_32 (abfd, buf + 48);
1142 section->nreloc = bfd_h_get_32 (abfd, buf + 52);
1143 section->flags = bfd_h_get_32 (abfd, buf + 56);
1144 section->reserved1 = bfd_h_get_32 (abfd, buf + 60);
1145 section->reserved2 = bfd_h_get_32 (abfd, buf + 64);
1146 section->reserved3 = 0;
1147 section->bfdsection = bfd_mach_o_make_bfd_section (abfd, section, prot);
1149 if (section->bfdsection == NULL)
1156 bfd_mach_o_scan_read_section_64 (bfd *abfd,
1157 bfd_mach_o_section *section,
1161 unsigned char buf[BFD_MACH_O_SECTION_64_SIZE];
1163 if (bfd_seek (abfd, offset, SEEK_SET) != 0
1164 || (bfd_bread ((PTR) buf, BFD_MACH_O_SECTION_64_SIZE, abfd)
1165 != BFD_MACH_O_SECTION_64_SIZE))
1168 memcpy (section->sectname, buf, 16);
1169 section->sectname[16] = '\0';
1170 memcpy (section->segname, buf + 16, 16);
1171 section->segname[16] = '\0';
1172 section->addr = bfd_h_get_64 (abfd, buf + 32);
1173 section->size = bfd_h_get_64 (abfd, buf + 40);
1174 section->offset = bfd_h_get_32 (abfd, buf + 48);
1175 section->align = bfd_h_get_32 (abfd, buf + 52);
1176 section->reloff = bfd_h_get_32 (abfd, buf + 56);
1177 section->nreloc = bfd_h_get_32 (abfd, buf + 60);
1178 section->flags = bfd_h_get_32 (abfd, buf + 64);
1179 section->reserved1 = bfd_h_get_32 (abfd, buf + 68);
1180 section->reserved2 = bfd_h_get_32 (abfd, buf + 72);
1181 section->reserved3 = bfd_h_get_32 (abfd, buf + 76);
1182 section->bfdsection = bfd_mach_o_make_bfd_section (abfd, section, prot);
1184 if (section->bfdsection == NULL)
1191 bfd_mach_o_scan_read_section (bfd *abfd,
1192 bfd_mach_o_section *section,
1198 return bfd_mach_o_scan_read_section_64 (abfd, section, offset, prot);
1200 return bfd_mach_o_scan_read_section_32 (abfd, section, offset, prot);
1204 bfd_mach_o_scan_read_symtab_symbol (bfd *abfd,
1205 bfd_mach_o_symtab_command *sym,
1209 bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
1210 unsigned int wide = mach_o_wide_p (&mdata->header);
1211 unsigned int symwidth = wide ? 16 : 12;
1212 bfd_vma symoff = sym->symoff + (i * symwidth);
1213 unsigned char buf[16];
1214 unsigned char type = -1;
1215 unsigned char section = -1;
1217 symvalue value = -1;
1218 unsigned long stroff = -1;
1219 unsigned int symtype = -1;
1221 BFD_ASSERT (sym->strtab != NULL);
1223 if (bfd_seek (abfd, symoff, SEEK_SET) != 0
1224 || bfd_bread ((PTR) buf, symwidth, abfd) != symwidth)
1226 fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: unable to read %d bytes at %lu\n",
1227 symwidth, (unsigned long) symoff);
1231 stroff = bfd_h_get_32 (abfd, buf);
1232 type = bfd_h_get_8 (abfd, buf + 4);
1233 symtype = type & BFD_MACH_O_N_TYPE;
1234 section = bfd_h_get_8 (abfd, buf + 5);
1235 desc = bfd_h_get_16 (abfd, buf + 6);
1237 value = bfd_h_get_64 (abfd, buf + 8);
1239 value = bfd_h_get_32 (abfd, buf + 8);
1241 if (stroff >= sym->strsize)
1243 fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: symbol name out of range (%lu >= %lu)\n",
1244 (unsigned long) stroff, (unsigned long) sym->strsize);
1249 s->name = sym->strtab + stroff;
1251 s->udata.i = (type << 24) | (section << 16) | desc;
1254 if (type & BFD_MACH_O_N_STAB)
1256 s->flags |= BSF_DEBUGGING;
1257 s->section = bfd_und_section_ptr;
1269 if ((section > 0) && (section <= mdata->nsects))
1271 s->section = mdata->sections[section - 1]->bfdsection;
1272 s->value = s->value - mdata->sections[section - 1]->addr;
1279 if (type & BFD_MACH_O_N_PEXT)
1280 s->flags |= BSF_GLOBAL;
1282 if (type & BFD_MACH_O_N_EXT)
1283 s->flags |= BSF_GLOBAL;
1285 if (!(type & (BFD_MACH_O_N_PEXT | BFD_MACH_O_N_EXT)))
1286 s->flags |= BSF_LOCAL;
1290 case BFD_MACH_O_N_UNDF:
1291 if (type == (BFD_MACH_O_N_UNDF | BFD_MACH_O_N_EXT)
1294 /* A common symbol. */
1295 s->section = bfd_com_section_ptr;
1296 s->flags = BSF_NO_FLAGS;
1299 s->section = bfd_und_section_ptr;
1301 case BFD_MACH_O_N_PBUD:
1302 s->section = bfd_und_section_ptr;
1304 case BFD_MACH_O_N_ABS:
1305 s->section = bfd_abs_section_ptr;
1307 case BFD_MACH_O_N_SECT:
1308 if ((section > 0) && (section <= mdata->nsects))
1310 s->section = mdata->sections[section - 1]->bfdsection;
1311 s->value = s->value - mdata->sections[section - 1]->addr;
1315 /* Mach-O uses 0 to mean "no section"; not an error. */
1318 fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: "
1319 "symbol \"%s\" specified invalid section %d (max %lu): setting to undefined\n",
1320 s->name, section, mdata->nsects);
1322 s->section = bfd_und_section_ptr;
1325 case BFD_MACH_O_N_INDR:
1326 fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: "
1327 "symbol \"%s\" is unsupported 'indirect' reference: setting to undefined\n",
1329 s->section = bfd_und_section_ptr;
1332 fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: "
1333 "symbol \"%s\" specified invalid type field 0x%x: setting to undefined\n",
1335 s->section = bfd_und_section_ptr;
1344 bfd_mach_o_scan_read_symtab_strtab (bfd *abfd,
1345 bfd_mach_o_symtab_command *sym)
1347 BFD_ASSERT (sym->strtab == NULL);
1349 if (abfd->flags & BFD_IN_MEMORY)
1351 struct bfd_in_memory *b;
1353 b = (struct bfd_in_memory *) abfd->iostream;
1355 if ((sym->stroff + sym->strsize) > b->size)
1357 bfd_set_error (bfd_error_file_truncated);
1360 sym->strtab = (char *) b->buffer + sym->stroff;
1364 sym->strtab = bfd_alloc (abfd, sym->strsize);
1365 if (sym->strtab == NULL)
1368 if (bfd_seek (abfd, sym->stroff, SEEK_SET) != 0
1369 || bfd_bread ((PTR) sym->strtab, sym->strsize, abfd) != sym->strsize)
1371 fprintf (stderr, "bfd_mach_o_scan_read_symtab_strtab: unable to read %lu bytes at %lu\n",
1372 sym->strsize, sym->stroff);
1380 bfd_mach_o_scan_read_symtab_symbols (bfd *abfd,
1381 bfd_mach_o_symtab_command *sym)
1386 BFD_ASSERT (sym->symbols == NULL);
1387 sym->symbols = bfd_alloc (abfd, sym->nsyms * sizeof (asymbol));
1389 if (sym->symbols == NULL)
1391 fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbols: unable to allocate memory for symbols\n");
1395 ret = bfd_mach_o_scan_read_symtab_strtab (abfd, sym);
1399 for (i = 0; i < sym->nsyms; i++)
1401 ret = bfd_mach_o_scan_read_symtab_symbol (abfd, sym, &sym->symbols[i], i);
1410 bfd_mach_o_scan_read_dysymtab_symbol (bfd *abfd,
1411 bfd_mach_o_dysymtab_command *dysym,
1412 bfd_mach_o_symtab_command *sym,
1416 unsigned long isymoff = dysym->indirectsymoff + (i * 4);
1417 unsigned long symindex;
1418 unsigned char buf[4];
1420 BFD_ASSERT (i < dysym->nindirectsyms);
1422 if (bfd_seek (abfd, isymoff, SEEK_SET) != 0
1423 || bfd_bread ((PTR) buf, 4, abfd) != 4)
1425 fprintf (stderr, "bfd_mach_o_scan_read_dysymtab_symbol: unable to read %lu bytes at %lu\n",
1426 (unsigned long) 4, isymoff);
1429 symindex = bfd_h_get_32 (abfd, buf);
1431 return bfd_mach_o_scan_read_symtab_symbol (abfd, sym, s, symindex);
1435 bfd_mach_o_i386_flavour_string (unsigned int flavour)
1437 switch ((int) flavour)
1439 case BFD_MACH_O_x86_THREAD_STATE32: return "x86_THREAD_STATE32";
1440 case BFD_MACH_O_x86_FLOAT_STATE32: return "x86_FLOAT_STATE32";
1441 case BFD_MACH_O_x86_EXCEPTION_STATE32: return "x86_EXCEPTION_STATE32";
1442 case BFD_MACH_O_x86_THREAD_STATE64: return "x86_THREAD_STATE64";
1443 case BFD_MACH_O_x86_FLOAT_STATE64: return "x86_FLOAT_STATE64";
1444 case BFD_MACH_O_x86_EXCEPTION_STATE64: return "x86_EXCEPTION_STATE64";
1445 case BFD_MACH_O_x86_THREAD_STATE: return "x86_THREAD_STATE";
1446 case BFD_MACH_O_x86_FLOAT_STATE: return "x86_FLOAT_STATE";
1447 case BFD_MACH_O_x86_EXCEPTION_STATE: return "x86_EXCEPTION_STATE";
1448 case BFD_MACH_O_x86_DEBUG_STATE32: return "x86_DEBUG_STATE32";
1449 case BFD_MACH_O_x86_DEBUG_STATE64: return "x86_DEBUG_STATE64";
1450 case BFD_MACH_O_x86_DEBUG_STATE: return "x86_DEBUG_STATE";
1451 case BFD_MACH_O_THREAD_STATE_NONE: return "THREAD_STATE_NONE";
1452 default: return "UNKNOWN";
1457 bfd_mach_o_ppc_flavour_string (unsigned int flavour)
1459 switch ((int) flavour)
1461 case BFD_MACH_O_PPC_THREAD_STATE: return "PPC_THREAD_STATE";
1462 case BFD_MACH_O_PPC_FLOAT_STATE: return "PPC_FLOAT_STATE";
1463 case BFD_MACH_O_PPC_EXCEPTION_STATE: return "PPC_EXCEPTION_STATE";
1464 case BFD_MACH_O_PPC_VECTOR_STATE: return "PPC_VECTOR_STATE";
1465 default: return "UNKNOWN";
1470 bfd_mach_o_scan_read_dylinker (bfd *abfd,
1471 bfd_mach_o_load_command *command)
1473 bfd_mach_o_dylinker_command *cmd = &command->command.dylinker;
1474 unsigned char buf[4];
1475 unsigned int nameoff;
1480 BFD_ASSERT ((command->type == BFD_MACH_O_LC_ID_DYLINKER)
1481 || (command->type == BFD_MACH_O_LC_LOAD_DYLINKER));
1483 if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
1484 || bfd_bread ((PTR) buf, 4, abfd) != 4)
1487 nameoff = bfd_h_get_32 (abfd, buf + 0);
1489 cmd->name_offset = command->offset + nameoff;
1490 cmd->name_len = command->len - nameoff;
1492 if (command->type == BFD_MACH_O_LC_LOAD_DYLINKER)
1493 prefix = "LC_LOAD_DYLINKER";
1494 else if (command->type == BFD_MACH_O_LC_ID_DYLINKER)
1495 prefix = "LC_ID_DYLINKER";
1499 sname = bfd_alloc (abfd, strlen (prefix) + 1);
1502 strcpy (sname, prefix);
1504 bfdsec = bfd_make_section_anyway_with_flags (abfd, sname, SEC_HAS_CONTENTS);
1510 bfdsec->size = command->len - nameoff;
1511 bfdsec->filepos = command->offset + nameoff;
1512 bfdsec->alignment_power = 0;
1514 cmd->section = bfdsec;
1520 bfd_mach_o_scan_read_dylib (bfd *abfd, bfd_mach_o_load_command *command)
1522 bfd_mach_o_dylib_command *cmd = &command->command.dylib;
1523 unsigned char buf[16];
1524 unsigned int nameoff;
1529 BFD_ASSERT ((command->type == BFD_MACH_O_LC_ID_DYLIB)
1530 || (command->type == BFD_MACH_O_LC_LOAD_DYLIB)
1531 || (command->type == BFD_MACH_O_LC_LOAD_WEAK_DYLIB));
1533 if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
1534 || bfd_bread ((PTR) buf, 16, abfd) != 16)
1537 nameoff = bfd_h_get_32 (abfd, buf + 0);
1538 cmd->timestamp = bfd_h_get_32 (abfd, buf + 4);
1539 cmd->current_version = bfd_h_get_32 (abfd, buf + 8);
1540 cmd->compatibility_version = bfd_h_get_32 (abfd, buf + 12);
1542 cmd->name_offset = command->offset + nameoff;
1543 cmd->name_len = command->len - nameoff;
1545 if (command->type == BFD_MACH_O_LC_LOAD_DYLIB)
1546 prefix = "LC_LOAD_DYLIB";
1547 else if (command->type == BFD_MACH_O_LC_LOAD_WEAK_DYLIB)
1548 prefix = "LC_LOAD_WEAK_DYLIB";
1549 else if (command->type == BFD_MACH_O_LC_ID_DYLIB)
1550 prefix = "LC_ID_DYLIB";
1554 sname = bfd_alloc (abfd, strlen (prefix) + 1);
1557 strcpy (sname, prefix);
1559 bfdsec = bfd_make_section_anyway_with_flags (abfd, sname, SEC_HAS_CONTENTS);
1565 bfdsec->size = command->len - 8;
1566 bfdsec->filepos = command->offset + 8;
1567 bfdsec->alignment_power = 0;
1569 cmd->section = bfdsec;
1575 bfd_mach_o_scan_read_prebound_dylib (bfd *abfd ATTRIBUTE_UNUSED,
1576 bfd_mach_o_load_command *command ATTRIBUTE_UNUSED)
1578 /* bfd_mach_o_prebound_dylib_command *cmd = &command->command.prebound_dylib; */
1580 BFD_ASSERT (command->type == BFD_MACH_O_LC_PREBOUND_DYLIB);
1585 bfd_mach_o_scan_read_thread (bfd *abfd, bfd_mach_o_load_command *command)
1587 bfd_mach_o_data_struct *mdata = NULL;
1588 bfd_mach_o_thread_command *cmd = &command->command.thread;
1589 unsigned char buf[8];
1591 unsigned int nflavours;
1594 BFD_ASSERT ((command->type == BFD_MACH_O_LC_THREAD)
1595 || (command->type == BFD_MACH_O_LC_UNIXTHREAD));
1597 BFD_ASSERT (bfd_mach_o_valid (abfd));
1598 mdata = abfd->tdata.mach_o_data;
1602 while (offset != command->len)
1604 if (offset >= command->len)
1607 if (bfd_seek (abfd, command->offset + offset, SEEK_SET) != 0
1608 || bfd_bread ((PTR) buf, 8, abfd) != 8)
1611 offset += 8 + bfd_h_get_32 (abfd, buf + 4) * 4;
1615 cmd->flavours = bfd_alloc (abfd, nflavours * sizeof (bfd_mach_o_thread_flavour));
1616 if (cmd->flavours == NULL)
1618 cmd->nflavours = nflavours;
1622 while (offset != command->len)
1624 if (offset >= command->len)
1627 if (nflavours >= cmd->nflavours)
1630 if (bfd_seek (abfd, command->offset + offset, SEEK_SET) != 0
1631 || bfd_bread ((PTR) buf, 8, abfd) != 8)
1634 cmd->flavours[nflavours].flavour = bfd_h_get_32 (abfd, buf);
1635 cmd->flavours[nflavours].offset = command->offset + offset + 8;
1636 cmd->flavours[nflavours].size = bfd_h_get_32 (abfd, buf + 4) * 4;
1637 offset += cmd->flavours[nflavours].size + 8;
1641 for (i = 0; i < nflavours; i++)
1644 unsigned int snamelen;
1646 const char *flavourstr;
1647 const char *prefix = "LC_THREAD";
1650 switch (mdata->header.cputype)
1652 case BFD_MACH_O_CPU_TYPE_POWERPC:
1653 case BFD_MACH_O_CPU_TYPE_POWERPC_64:
1654 flavourstr = bfd_mach_o_ppc_flavour_string (cmd->flavours[i].flavour);
1656 case BFD_MACH_O_CPU_TYPE_I386:
1657 case BFD_MACH_O_CPU_TYPE_X86_64:
1658 flavourstr = bfd_mach_o_i386_flavour_string (cmd->flavours[i].flavour);
1661 flavourstr = "UNKNOWN_ARCHITECTURE";
1665 snamelen = strlen (prefix) + 1 + 20 + 1 + strlen (flavourstr) + 1;
1666 sname = bfd_alloc (abfd, snamelen);
1672 sprintf (sname, "%s.%s.%u", prefix, flavourstr, j);
1673 if (bfd_get_section_by_name (abfd, sname) == NULL)
1678 bfdsec = bfd_make_section_with_flags (abfd, sname, SEC_HAS_CONTENTS);
1682 bfdsec->size = cmd->flavours[i].size;
1683 bfdsec->filepos = cmd->flavours[i].offset;
1684 bfdsec->alignment_power = 0x0;
1686 cmd->section = bfdsec;
1693 bfd_mach_o_scan_read_dysymtab (bfd *abfd, bfd_mach_o_load_command *command)
1695 bfd_mach_o_dysymtab_command *seg = &command->command.dysymtab;
1696 unsigned char buf[72];
1698 BFD_ASSERT (command->type == BFD_MACH_O_LC_DYSYMTAB);
1700 if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
1701 || bfd_bread ((PTR) buf, 72, abfd) != 72)
1704 seg->ilocalsym = bfd_h_get_32 (abfd, buf + 0);
1705 seg->nlocalsym = bfd_h_get_32 (abfd, buf + 4);
1706 seg->iextdefsym = bfd_h_get_32 (abfd, buf + 8);
1707 seg->nextdefsym = bfd_h_get_32 (abfd, buf + 12);
1708 seg->iundefsym = bfd_h_get_32 (abfd, buf + 16);
1709 seg->nundefsym = bfd_h_get_32 (abfd, buf + 20);
1710 seg->tocoff = bfd_h_get_32 (abfd, buf + 24);
1711 seg->ntoc = bfd_h_get_32 (abfd, buf + 28);
1712 seg->modtaboff = bfd_h_get_32 (abfd, buf + 32);
1713 seg->nmodtab = bfd_h_get_32 (abfd, buf + 36);
1714 seg->extrefsymoff = bfd_h_get_32 (abfd, buf + 40);
1715 seg->nextrefsyms = bfd_h_get_32 (abfd, buf + 44);
1716 seg->indirectsymoff = bfd_h_get_32 (abfd, buf + 48);
1717 seg->nindirectsyms = bfd_h_get_32 (abfd, buf + 52);
1718 seg->extreloff = bfd_h_get_32 (abfd, buf + 56);
1719 seg->nextrel = bfd_h_get_32 (abfd, buf + 60);
1720 seg->locreloff = bfd_h_get_32 (abfd, buf + 64);
1721 seg->nlocrel = bfd_h_get_32 (abfd, buf + 68);
1727 bfd_mach_o_scan_read_symtab (bfd *abfd, bfd_mach_o_load_command *command)
1729 bfd_mach_o_symtab_command *seg = &command->command.symtab;
1730 unsigned char buf[16];
1732 BFD_ASSERT (command->type == BFD_MACH_O_LC_SYMTAB);
1734 if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
1735 || bfd_bread ((PTR) buf, 16, abfd) != 16)
1738 seg->symoff = bfd_h_get_32 (abfd, buf);
1739 seg->nsyms = bfd_h_get_32 (abfd, buf + 4);
1740 seg->stroff = bfd_h_get_32 (abfd, buf + 8);
1741 seg->strsize = bfd_h_get_32 (abfd, buf + 12);
1742 seg->symbols = NULL;
1745 if (seg->nsyms != 0)
1746 abfd->flags |= HAS_SYMS;
1752 bfd_mach_o_scan_read_uuid (bfd *abfd, bfd_mach_o_load_command *command)
1754 bfd_mach_o_uuid_command *cmd = &command->command.uuid;
1757 static const char prefix[] = "LC_UUID";
1759 BFD_ASSERT (command->type == BFD_MACH_O_LC_UUID);
1761 if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
1762 || bfd_bread ((PTR) cmd->uuid, 16, abfd) != 16)
1765 sname = bfd_alloc (abfd, strlen (prefix) + 1);
1768 strcpy (sname, prefix);
1770 bfdsec = bfd_make_section_anyway_with_flags (abfd, sname, SEC_HAS_CONTENTS);
1776 bfdsec->size = command->len - 8;
1777 bfdsec->filepos = command->offset + 8;
1778 bfdsec->alignment_power = 0;
1780 cmd->section = bfdsec;
1786 bfd_mach_o_scan_read_segment (bfd *abfd,
1787 bfd_mach_o_load_command *command,
1790 unsigned char buf[64];
1791 bfd_mach_o_segment_command *seg = &command->command.segment;
1796 BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT_64);
1798 if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
1799 || bfd_bread ((PTR) buf, 64, abfd) != 64)
1802 memcpy (seg->segname, buf, 16);
1803 seg->segname[16] = '\0';
1805 seg->vmaddr = bfd_h_get_64 (abfd, buf + 16);
1806 seg->vmsize = bfd_h_get_64 (abfd, buf + 24);
1807 seg->fileoff = bfd_h_get_64 (abfd, buf + 32);
1808 seg->filesize = bfd_h_get_64 (abfd, buf + 40);
1809 seg->maxprot = bfd_h_get_32 (abfd, buf + 48);
1810 seg->initprot = bfd_h_get_32 (abfd, buf + 52);
1811 seg->nsects = bfd_h_get_32 (abfd, buf + 56);
1812 seg->flags = bfd_h_get_32 (abfd, buf + 60);
1816 BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT);
1818 if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
1819 || bfd_bread ((PTR) buf, 48, abfd) != 48)
1822 memcpy (seg->segname, buf, 16);
1823 seg->segname[16] = '\0';
1825 seg->vmaddr = bfd_h_get_32 (abfd, buf + 16);
1826 seg->vmsize = bfd_h_get_32 (abfd, buf + 20);
1827 seg->fileoff = bfd_h_get_32 (abfd, buf + 24);
1828 seg->filesize = bfd_h_get_32 (abfd, buf + 28);
1829 seg->maxprot = bfd_h_get_32 (abfd, buf + 32);
1830 seg->initprot = bfd_h_get_32 (abfd, buf + 36);
1831 seg->nsects = bfd_h_get_32 (abfd, buf + 40);
1832 seg->flags = bfd_h_get_32 (abfd, buf + 44);
1835 if (seg->nsects != 0)
1837 seg->sections = bfd_alloc (abfd, seg->nsects
1838 * sizeof (bfd_mach_o_section));
1839 if (seg->sections == NULL)
1842 for (i = 0; i < seg->nsects; i++)
1846 segoff = command->offset + BFD_MACH_O_LC_SEGMENT_64_SIZE
1847 + (i * BFD_MACH_O_SECTION_64_SIZE);
1849 segoff = command->offset + BFD_MACH_O_LC_SEGMENT_SIZE
1850 + (i * BFD_MACH_O_SECTION_SIZE);
1852 if (bfd_mach_o_scan_read_section
1853 (abfd, &seg->sections[i], segoff, seg->initprot, wide) != 0)
1862 bfd_mach_o_scan_read_segment_32 (bfd *abfd, bfd_mach_o_load_command *command)
1864 return bfd_mach_o_scan_read_segment (abfd, command, 0);
1868 bfd_mach_o_scan_read_segment_64 (bfd *abfd, bfd_mach_o_load_command *command)
1870 return bfd_mach_o_scan_read_segment (abfd, command, 1);
1874 bfd_mach_o_scan_read_command (bfd *abfd, bfd_mach_o_load_command *command)
1876 unsigned char buf[8];
1878 if (bfd_seek (abfd, command->offset, SEEK_SET) != 0
1879 || bfd_bread ((PTR) buf, 8, abfd) != 8)
1882 command->type = bfd_h_get_32 (abfd, buf) & ~BFD_MACH_O_LC_REQ_DYLD;
1883 command->type_required = (bfd_h_get_32 (abfd, buf) & BFD_MACH_O_LC_REQ_DYLD
1885 command->len = bfd_h_get_32 (abfd, buf + 4);
1887 switch (command->type)
1889 case BFD_MACH_O_LC_SEGMENT:
1890 if (bfd_mach_o_scan_read_segment_32 (abfd, command) != 0)
1893 case BFD_MACH_O_LC_SEGMENT_64:
1894 if (bfd_mach_o_scan_read_segment_64 (abfd, command) != 0)
1897 case BFD_MACH_O_LC_SYMTAB:
1898 if (bfd_mach_o_scan_read_symtab (abfd, command) != 0)
1901 case BFD_MACH_O_LC_SYMSEG:
1903 case BFD_MACH_O_LC_THREAD:
1904 case BFD_MACH_O_LC_UNIXTHREAD:
1905 if (bfd_mach_o_scan_read_thread (abfd, command) != 0)
1908 case BFD_MACH_O_LC_LOAD_DYLINKER:
1909 case BFD_MACH_O_LC_ID_DYLINKER:
1910 if (bfd_mach_o_scan_read_dylinker (abfd, command) != 0)
1913 case BFD_MACH_O_LC_LOAD_DYLIB:
1914 case BFD_MACH_O_LC_ID_DYLIB:
1915 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB:
1916 if (bfd_mach_o_scan_read_dylib (abfd, command) != 0)
1919 case BFD_MACH_O_LC_PREBOUND_DYLIB:
1920 if (bfd_mach_o_scan_read_prebound_dylib (abfd, command) != 0)
1923 case BFD_MACH_O_LC_LOADFVMLIB:
1924 case BFD_MACH_O_LC_IDFVMLIB:
1925 case BFD_MACH_O_LC_IDENT:
1926 case BFD_MACH_O_LC_FVMFILE:
1927 case BFD_MACH_O_LC_PREPAGE:
1928 case BFD_MACH_O_LC_ROUTINES:
1929 case BFD_MACH_O_LC_SUB_FRAMEWORK:
1931 case BFD_MACH_O_LC_DYSYMTAB:
1932 if (bfd_mach_o_scan_read_dysymtab (abfd, command) != 0)
1935 case BFD_MACH_O_LC_SUB_UMBRELLA:
1936 case BFD_MACH_O_LC_SUB_CLIENT:
1937 case BFD_MACH_O_LC_SUB_LIBRARY:
1938 case BFD_MACH_O_LC_TWOLEVEL_HINTS:
1939 case BFD_MACH_O_LC_PREBIND_CKSUM:
1941 case BFD_MACH_O_LC_UUID:
1942 if (bfd_mach_o_scan_read_uuid (abfd, command) != 0)
1945 case BFD_MACH_O_LC_CODE_SIGNATURE:
1946 case BFD_MACH_O_LC_SEGMENT_SPLIT_INFO:
1947 case BFD_MACH_O_LC_REEXPORT_DYLIB:
1950 fprintf (stderr, "unable to read unknown load command 0x%lx\n",
1951 (unsigned long) command->type);
1959 bfd_mach_o_flatten_sections (bfd *abfd)
1961 bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
1965 /* Count total number of sections. */
1968 for (i = 0; i < mdata->header.ncmds; i++)
1970 if (mdata->commands[i].type == BFD_MACH_O_LC_SEGMENT
1971 || mdata->commands[i].type == BFD_MACH_O_LC_SEGMENT_64)
1973 bfd_mach_o_segment_command *seg;
1975 seg = &mdata->commands[i].command.segment;
1976 mdata->nsects += seg->nsects;
1980 /* Allocate sections array. */
1981 mdata->sections = bfd_alloc (abfd,
1982 mdata->nsects * sizeof (bfd_mach_o_section *));
1984 /* Fill the array. */
1987 for (i = 0; i < mdata->header.ncmds; i++)
1989 if (mdata->commands[i].type == BFD_MACH_O_LC_SEGMENT
1990 || mdata->commands[i].type == BFD_MACH_O_LC_SEGMENT_64)
1992 bfd_mach_o_segment_command *seg;
1994 seg = &mdata->commands[i].command.segment;
1995 BFD_ASSERT (csect + seg->nsects <= mdata->nsects);
1997 for (j = 0; j < seg->nsects; j++)
1998 mdata->sections[csect++] = &seg->sections[j];
2004 bfd_mach_o_scan_start_address (bfd *abfd)
2006 bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
2007 bfd_mach_o_thread_command *cmd = NULL;
2010 for (i = 0; i < mdata->header.ncmds; i++)
2012 if ((mdata->commands[i].type == BFD_MACH_O_LC_THREAD) ||
2013 (mdata->commands[i].type == BFD_MACH_O_LC_UNIXTHREAD))
2016 cmd = &mdata->commands[i].command.thread;
2025 for (i = 0; i < cmd->nflavours; i++)
2027 if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_I386)
2028 && (cmd->flavours[i].flavour
2029 == (unsigned long) BFD_MACH_O_x86_THREAD_STATE32))
2031 unsigned char buf[4];
2033 if (bfd_seek (abfd, cmd->flavours[i].offset + 40, SEEK_SET) != 0
2034 || bfd_bread (buf, 4, abfd) != 4)
2037 abfd->start_address = bfd_h_get_32 (abfd, buf);
2039 else if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_POWERPC)
2040 && (cmd->flavours[i].flavour == BFD_MACH_O_PPC_THREAD_STATE))
2042 unsigned char buf[4];
2044 if (bfd_seek (abfd, cmd->flavours[i].offset + 0, SEEK_SET) != 0
2045 || bfd_bread (buf, 4, abfd) != 4)
2048 abfd->start_address = bfd_h_get_32 (abfd, buf);
2050 else if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_POWERPC_64)
2051 && (cmd->flavours[i].flavour == BFD_MACH_O_PPC_THREAD_STATE_64))
2053 unsigned char buf[8];
2055 if (bfd_seek (abfd, cmd->flavours[i].offset + 0, SEEK_SET) != 0
2056 || bfd_bread (buf, 8, abfd) != 8)
2059 abfd->start_address = bfd_h_get_64 (abfd, buf);
2061 else if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_X86_64)
2062 && (cmd->flavours[i].flavour == BFD_MACH_O_x86_THREAD_STATE64))
2064 unsigned char buf[8];
2066 if (bfd_seek (abfd, cmd->flavours[i].offset + (16 * 8), SEEK_SET) != 0
2067 || bfd_bread (buf, 8, abfd) != 8)
2070 abfd->start_address = bfd_h_get_64 (abfd, buf);
2078 bfd_mach_o_scan (bfd *abfd,
2079 bfd_mach_o_header *header,
2080 bfd_mach_o_data_struct *mdata)
2083 enum bfd_architecture cputype;
2084 unsigned long cpusubtype;
2085 unsigned int hdrsize;
2087 hdrsize = mach_o_wide_p (header) ?
2088 BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE;
2090 mdata->header = *header;
2091 mdata->symbols = NULL;
2093 abfd->flags = abfd->flags & BFD_IN_MEMORY;
2094 switch (header->filetype)
2096 case BFD_MACH_O_MH_OBJECT:
2097 abfd->flags |= HAS_RELOC;
2099 case BFD_MACH_O_MH_EXECUTE:
2100 abfd->flags |= EXEC_P;
2102 case BFD_MACH_O_MH_DYLIB:
2103 case BFD_MACH_O_MH_BUNDLE:
2104 abfd->flags |= DYNAMIC;
2108 abfd->tdata.mach_o_data = mdata;
2110 bfd_mach_o_convert_architecture (header->cputype, header->cpusubtype,
2111 &cputype, &cpusubtype);
2112 if (cputype == bfd_arch_unknown)
2114 fprintf (stderr, "bfd_mach_o_scan: unknown architecture 0x%lx/0x%lx\n",
2115 header->cputype, header->cpusubtype);
2119 bfd_set_arch_mach (abfd, cputype, cpusubtype);
2121 if (header->ncmds != 0)
2123 mdata->commands = bfd_alloc (abfd, header->ncmds * sizeof (bfd_mach_o_load_command));
2124 if (mdata->commands == NULL)
2127 for (i = 0; i < header->ncmds; i++)
2129 bfd_mach_o_load_command *cur = &mdata->commands[i];
2132 cur->offset = hdrsize;
2135 bfd_mach_o_load_command *prev = &mdata->commands[i - 1];
2136 cur->offset = prev->offset + prev->len;
2139 if (bfd_mach_o_scan_read_command (abfd, cur) < 0)
2144 if (bfd_mach_o_scan_start_address (abfd) < 0)
2147 bfd_mach_o_flatten_sections (abfd);
2152 bfd_mach_o_mkobject_init (bfd *abfd)
2154 bfd_mach_o_data_struct *mdata = NULL;
2156 mdata = bfd_alloc (abfd, sizeof (bfd_mach_o_data_struct));
2159 abfd->tdata.mach_o_data = mdata;
2161 mdata->header.magic = 0;
2162 mdata->header.cputype = 0;
2163 mdata->header.cpusubtype = 0;
2164 mdata->header.filetype = 0;
2165 mdata->header.ncmds = 0;
2166 mdata->header.sizeofcmds = 0;
2167 mdata->header.flags = 0;
2168 mdata->header.byteorder = BFD_ENDIAN_UNKNOWN;
2169 mdata->commands = NULL;
2170 mdata->nsymbols = 0;
2171 mdata->symbols = NULL;
2173 mdata->sections = NULL;
2180 bfd_mach_o_header_p (bfd *abfd,
2181 bfd_mach_o_filetype filetype,
2182 bfd_mach_o_cpu_type cputype)
2184 struct bfd_preserve preserve;
2185 bfd_mach_o_header header;
2187 preserve.marker = NULL;
2188 if (!bfd_mach_o_read_header (abfd, &header))
2191 if (! (header.byteorder == BFD_ENDIAN_BIG
2192 || header.byteorder == BFD_ENDIAN_LITTLE))
2194 fprintf (stderr, "unknown header byte-order value 0x%lx\n",
2195 (unsigned long) header.byteorder);
2199 if (! ((header.byteorder == BFD_ENDIAN_BIG
2200 && abfd->xvec->byteorder == BFD_ENDIAN_BIG
2201 && abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
2202 || (header.byteorder == BFD_ENDIAN_LITTLE
2203 && abfd->xvec->byteorder == BFD_ENDIAN_LITTLE
2204 && abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)))
2207 /* Check cputype and filetype.
2208 In case of wildcard, do not accept magics that are handled by existing
2212 if (header.cputype != cputype)
2217 switch (header.cputype)
2219 case BFD_MACH_O_CPU_TYPE_I386:
2220 /* Handled by mach-o-i386 */
2228 if (header.filetype != filetype)
2233 switch (header.filetype)
2235 case BFD_MACH_O_MH_CORE:
2236 /* Handled by core_p */
2243 preserve.marker = bfd_zalloc (abfd, sizeof (bfd_mach_o_data_struct));
2244 if (preserve.marker == NULL
2245 || !bfd_preserve_save (abfd, &preserve))
2248 if (bfd_mach_o_scan (abfd, &header,
2249 (bfd_mach_o_data_struct *) preserve.marker) != 0)
2252 bfd_preserve_finish (abfd, &preserve);
2256 bfd_set_error (bfd_error_wrong_format);
2259 if (preserve.marker != NULL)
2260 bfd_preserve_restore (abfd, &preserve);
2264 static const bfd_target *
2265 bfd_mach_o_gen_object_p (bfd *abfd)
2267 return bfd_mach_o_header_p (abfd, 0, 0);
2270 static const bfd_target *
2271 bfd_mach_o_gen_core_p (bfd *abfd)
2273 return bfd_mach_o_header_p (abfd, BFD_MACH_O_MH_CORE, 0);
2276 typedef struct mach_o_fat_archentry
2278 unsigned long cputype;
2279 unsigned long cpusubtype;
2280 unsigned long offset;
2282 unsigned long align;
2283 } mach_o_fat_archentry;
2285 typedef struct mach_o_fat_data_struct
2287 unsigned long magic;
2288 unsigned long nfat_arch;
2289 mach_o_fat_archentry *archentries;
2290 } mach_o_fat_data_struct;
2293 bfd_mach_o_archive_p (bfd *abfd)
2295 mach_o_fat_data_struct *adata = NULL;
2296 unsigned char buf[20];
2299 if (bfd_seek (abfd, 0, SEEK_SET) != 0
2300 || bfd_bread ((PTR) buf, 8, abfd) != 8)
2303 adata = bfd_alloc (abfd, sizeof (mach_o_fat_data_struct));
2307 adata->magic = bfd_getb32 (buf);
2308 adata->nfat_arch = bfd_getb32 (buf + 4);
2309 if (adata->magic != 0xcafebabe)
2311 /* Avoid matching Java bytecode files, which have the same magic number.
2312 In the Java bytecode file format this field contains the JVM version,
2313 which starts at 43.0. */
2314 if (adata->nfat_arch > 30)
2317 adata->archentries =
2318 bfd_alloc (abfd, adata->nfat_arch * sizeof (mach_o_fat_archentry));
2319 if (adata->archentries == NULL)
2322 for (i = 0; i < adata->nfat_arch; i++)
2324 if (bfd_seek (abfd, 8 + 20 * i, SEEK_SET) != 0
2325 || bfd_bread ((PTR) buf, 20, abfd) != 20)
2327 adata->archentries[i].cputype = bfd_getb32 (buf);
2328 adata->archentries[i].cpusubtype = bfd_getb32 (buf + 4);
2329 adata->archentries[i].offset = bfd_getb32 (buf + 8);
2330 adata->archentries[i].size = bfd_getb32 (buf + 12);
2331 adata->archentries[i].align = bfd_getb32 (buf + 16);
2334 abfd->tdata.mach_o_fat_data = adata;
2339 bfd_release (abfd, adata);
2340 bfd_set_error (bfd_error_wrong_format);
2345 bfd_mach_o_openr_next_archived_file (bfd *archive, bfd *prev)
2347 mach_o_fat_data_struct *adata;
2348 mach_o_fat_archentry *entry = NULL;
2351 enum bfd_architecture arch_type;
2352 unsigned long arch_subtype;
2354 adata = (mach_o_fat_data_struct *) archive->tdata.mach_o_fat_data;
2355 BFD_ASSERT (adata != NULL);
2357 /* Find index of previous entry. */
2359 i = 0; /* Start at first one. */
2362 for (i = 0; i < adata->nfat_arch; i++)
2364 if (adata->archentries[i].offset == prev->origin)
2368 if (i == adata->nfat_arch)
2371 bfd_set_error (bfd_error_bad_value);
2374 i++; /* Get next entry. */
2377 if (i >= adata->nfat_arch)
2379 bfd_set_error (bfd_error_no_more_archived_files);
2383 entry = &adata->archentries[i];
2384 nbfd = _bfd_new_bfd_contained_in (archive);
2388 nbfd->origin = entry->offset;
2390 bfd_mach_o_convert_architecture (entry->cputype, entry->cpusubtype,
2391 &arch_type, &arch_subtype);
2392 /* Create the member filename.
2393 Use FILENAME:ARCH_NAME. */
2396 const char *arch_name;
2397 size_t arch_file_len = strlen (bfd_get_filename (archive));
2399 arch_name = bfd_printable_arch_mach (arch_type, arch_subtype);
2400 s = bfd_malloc (arch_file_len + 1 + strlen (arch_name) + 1);
2403 memcpy (s, bfd_get_filename (archive), arch_file_len);
2404 s[arch_file_len] = ':';
2405 strcpy (s + arch_file_len + 1, arch_name);
2408 nbfd->iostream = NULL;
2409 bfd_set_arch_mach (nbfd, arch_type, arch_subtype);
2414 /* If ABFD format is FORMAT and architecture is ARCH, return it.
2415 If ABFD is a fat image containing a member that corresponds to FORMAT
2416 and ARCH, returns it.
2417 In other case, returns NULL.
2418 This function allows transparent uses of fat images. */
2420 bfd_mach_o_fat_extract (bfd *abfd,
2422 const bfd_arch_info_type *arch)
2425 mach_o_fat_data_struct *adata;
2428 if (bfd_check_format (abfd, format))
2430 if (bfd_get_arch_info (abfd) == arch)
2434 if (!bfd_check_format (abfd, bfd_archive)
2435 || abfd->xvec != &mach_o_fat_vec)
2438 /* This is a Mach-O fat image. */
2439 adata = (mach_o_fat_data_struct *) abfd->tdata.mach_o_fat_data;
2440 BFD_ASSERT (adata != NULL);
2442 for (i = 0; i < adata->nfat_arch; i++)
2444 struct mach_o_fat_archentry *e = &adata->archentries[i];
2445 enum bfd_architecture cpu_type;
2446 unsigned long cpu_subtype;
2448 bfd_mach_o_convert_architecture (e->cputype, e->cpusubtype,
2449 &cpu_type, &cpu_subtype);
2450 if (cpu_type != arch->arch || cpu_subtype != arch->mach)
2453 /* The architecture is found. */
2454 res = _bfd_new_bfd_contained_in (abfd);
2458 res->origin = e->offset;
2460 res->filename = strdup (abfd->filename);
2461 res->iostream = NULL;
2463 if (bfd_check_format (res, format))
2465 BFD_ASSERT (bfd_get_arch_info (res) == arch);
2476 bfd_mach_o_lookup_section (bfd *abfd,
2478 bfd_mach_o_load_command **mcommand,
2479 bfd_mach_o_section **msection)
2481 struct mach_o_data_struct *md = abfd->tdata.mach_o_data;
2482 unsigned int i, j, num;
2484 bfd_mach_o_load_command *ncmd = NULL;
2485 bfd_mach_o_section *nsect = NULL;
2487 BFD_ASSERT (mcommand != NULL);
2488 BFD_ASSERT (msection != NULL);
2491 for (i = 0; i < md->header.ncmds; i++)
2493 struct bfd_mach_o_load_command *cmd = &md->commands[i];
2494 struct bfd_mach_o_segment_command *seg = NULL;
2496 if (cmd->type != BFD_MACH_O_LC_SEGMENT
2497 || cmd->type != BFD_MACH_O_LC_SEGMENT_64)
2499 seg = &cmd->command.segment;
2501 if (seg->segment == section)
2508 for (j = 0; j < seg->nsects; j++)
2510 struct bfd_mach_o_section *sect = &seg->sections[j];
2512 if (sect->bfdsection == section)
2527 bfd_mach_o_lookup_command (bfd *abfd,
2528 bfd_mach_o_load_command_type type,
2529 bfd_mach_o_load_command **mcommand)
2531 struct mach_o_data_struct *md = NULL;
2532 bfd_mach_o_load_command *ncmd = NULL;
2533 unsigned int i, num;
2535 md = abfd->tdata.mach_o_data;
2537 BFD_ASSERT (md != NULL);
2538 BFD_ASSERT (mcommand != NULL);
2541 for (i = 0; i < md->header.ncmds; i++)
2543 struct bfd_mach_o_load_command *cmd = &md->commands[i];
2545 if (cmd->type != type)
2558 bfd_mach_o_stack_addr (enum bfd_mach_o_cpu_type type)
2562 case BFD_MACH_O_CPU_TYPE_MC680x0:
2564 case BFD_MACH_O_CPU_TYPE_MC88000:
2566 case BFD_MACH_O_CPU_TYPE_POWERPC:
2568 case BFD_MACH_O_CPU_TYPE_I386:
2570 case BFD_MACH_O_CPU_TYPE_SPARC:
2572 case BFD_MACH_O_CPU_TYPE_I860:
2574 case BFD_MACH_O_CPU_TYPE_HPPA:
2575 return 0xc0000000 - 0x04000000;
2582 bfd_mach_o_bfd_print_private_bfd_data (bfd *abfd, PTR ptr)
2584 bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
2585 FILE *file = (FILE *) ptr;
2587 unsigned int sec_nbr = 0;
2589 fprintf (file, _("Segments and Sections:\n"));
2590 fprintf (file, _(" #: Segment name Section name Address\n"));
2592 for (i = 0; i < mdata->header.ncmds; i++)
2594 bfd_mach_o_segment_command *seg;
2596 if (mdata->commands[i].type != BFD_MACH_O_LC_SEGMENT
2597 && mdata->commands[i].type != BFD_MACH_O_LC_SEGMENT_64)
2600 seg = &mdata->commands[i].command.segment;
2602 fprintf (file, "[Segment %-16s ", seg->segname);
2603 fprintf_vma (file, seg->vmaddr);
2604 fprintf (file, "-");
2605 fprintf_vma (file, seg->vmaddr + seg->vmsize - 1);
2607 fputc (seg->initprot & BFD_MACH_O_PROT_READ ? 'r' : '-', file);
2608 fputc (seg->initprot & BFD_MACH_O_PROT_WRITE ? 'w' : '-', file);
2609 fputc (seg->initprot & BFD_MACH_O_PROT_EXECUTE ? 'x' : '-', file);
2610 fprintf (file, "]\n");
2611 for (j = 0; j < seg->nsects; j++)
2613 bfd_mach_o_section *sec = &seg->sections[j];
2614 fprintf (file, "%02u: %-16s %-16s ", ++sec_nbr,
2615 sec->segname, sec->sectname);
2616 fprintf_vma (file, sec->addr);
2617 fprintf (file, " ");
2618 fprintf_vma (file, sec->size);
2619 fprintf (file, " %08lx\n", sec->flags);
2623 for (i = 0; i < mdata->header.ncmds; i++)
2625 bfd_mach_o_load_command *cmd = &mdata->commands[i];
2629 case BFD_MACH_O_LC_SEGMENT:
2630 case BFD_MACH_O_LC_SEGMENT_64:
2632 case BFD_MACH_O_LC_UUID:
2634 bfd_mach_o_uuid_command *uuid = &cmd->command.uuid;
2639 for (i = 0; i < sizeof (uuid->uuid); i++)
2640 fprintf (file, " %02x", uuid->uuid[i]);
2644 case BFD_MACH_O_LC_LOAD_DYLIB:
2646 bfd_mach_o_dylib_command *dylib = &cmd->command.dylib;
2647 bfd_byte *data = NULL;
2649 if (! bfd_malloc_and_get_section (abfd, dylib->section, &data))
2657 data + dylib->name_offset - cmd->offset - 8);
2658 fprintf (file, " time stamp: 0x%08lx\n",
2660 fprintf (file, " current version: 0x%08lx\n",
2661 dylib->current_version);
2662 fprintf (file, " comptibility version: 0x%08lx\n",
2663 dylib->compatibility_version);
2667 case BFD_MACH_O_LC_LOAD_DYLINKER:
2669 bfd_mach_o_dylinker_command *linker = &cmd->command.dylinker;
2670 bfd_byte *data = NULL;
2672 if (! bfd_malloc_and_get_section (abfd, linker->section, &data))
2679 "LOAD_DYLINKER: %s\n",
2680 data + linker->name_offset - cmd->offset - 8);
2684 case BFD_MACH_O_LC_SYMTAB:
2686 bfd_mach_o_symtab_command *symtab = &cmd->command.symtab;
2688 "LC_SYMTAB: nsyms: %lu, strsize: %lu\n",
2689 symtab->nsyms, symtab->strsize);
2692 case BFD_MACH_O_LC_DYSYMTAB:
2694 bfd_mach_o_dysymtab_command *dysymtab = &cmd->command.dysymtab;
2697 " local symbols: index: %lu number: %lu\n",
2698 dysymtab->ilocalsym, dysymtab->nlocalsym);
2700 " external symbols: index: %lu number: %lu\n",
2701 dysymtab->iextdefsym, dysymtab->nextdefsym);
2703 " undefined symbols: index: %lu number: %lu\n",
2704 dysymtab->iundefsym, dysymtab->nundefsym);
2706 " ntoc: offset: %lu number: %lu\n",
2707 dysymtab->tocoff, dysymtab->ntoc);
2709 " module table: offset: %lu number: %lu\n",
2710 dysymtab->modtaboff, dysymtab->nmodtab);
2714 fprintf (file, "LC_%d\n", cmd->type);
2723 bfd_mach_o_core_fetch_environment (bfd *abfd,
2724 unsigned char **rbuf,
2727 bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
2728 unsigned long stackaddr = bfd_mach_o_stack_addr (mdata->header.cputype);
2731 for (i = 0; i < mdata->header.ncmds; i++)
2733 bfd_mach_o_load_command *cur = &mdata->commands[i];
2734 bfd_mach_o_segment_command *seg = NULL;
2736 if (cur->type != BFD_MACH_O_LC_SEGMENT)
2739 seg = &cur->command.segment;
2741 if ((seg->vmaddr + seg->vmsize) == stackaddr)
2743 unsigned long start = seg->fileoff;
2744 unsigned long end = seg->fileoff + seg->filesize;
2745 unsigned char *buf = bfd_malloc (1024);
2746 unsigned long size = 1024;
2750 bfd_size_type nread = 0;
2751 unsigned long offset;
2752 int found_nonnull = 0;
2754 if (size > (end - start))
2755 size = (end - start);
2757 buf = bfd_realloc_or_free (buf, size);
2761 if (bfd_seek (abfd, end - size, SEEK_SET) != 0)
2767 nread = bfd_bread (buf, size, abfd);
2775 for (offset = 4; offset <= size; offset += 4)
2779 val = *((unsigned long *) (buf + size - offset));
2780 if (! found_nonnull)
2785 else if (val == 0x0)
2787 unsigned long bottom;
2790 bottom = seg->fileoff + seg->filesize - offset;
2791 top = seg->fileoff + seg->filesize - 4;
2792 *rbuf = bfd_malloc (top - bottom);
2793 *rlen = top - bottom;
2795 memcpy (*rbuf, buf + size - *rlen, *rlen);
2801 if (size == (end - start))
2815 bfd_mach_o_core_file_failing_command (bfd *abfd)
2817 unsigned char *buf = NULL;
2818 unsigned int len = 0;
2821 ret = bfd_mach_o_core_fetch_environment (abfd, &buf, &len);
2825 return (char *) buf;
2829 bfd_mach_o_core_file_failing_signal (bfd *abfd ATTRIBUTE_UNUSED)
2834 #define TARGET_NAME mach_o_be_vec
2835 #define TARGET_STRING "mach-o-be"
2836 #define TARGET_BIG_ENDIAN 1
2837 #define TARGET_ARCHIVE 0
2839 #include "mach-o-target.c"
2842 #undef TARGET_STRING
2843 #undef TARGET_BIG_ENDIAN
2844 #undef TARGET_ARCHIVE
2846 #define TARGET_NAME mach_o_le_vec
2847 #define TARGET_STRING "mach-o-le"
2848 #define TARGET_BIG_ENDIAN 0
2849 #define TARGET_ARCHIVE 0
2851 #include "mach-o-target.c"
2854 #undef TARGET_STRING
2855 #undef TARGET_BIG_ENDIAN
2856 #undef TARGET_ARCHIVE
2858 #define TARGET_NAME mach_o_fat_vec
2859 #define TARGET_STRING "mach-o-fat"
2860 #define TARGET_BIG_ENDIAN 1
2861 #define TARGET_ARCHIVE 1
2863 #include "mach-o-target.c"
2866 #undef TARGET_STRING
2867 #undef TARGET_BIG_ENDIAN
2868 #undef TARGET_ARCHIVE