2009-10-16 H.J. Lu <hongjiu.lu@intel.com>
[binutils.git] / bfd / mach-o.c
blob641fd9bd4439c32a1ef3ee057f00cc7f7bdd42e9
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. */
22 #include "sysdep.h"
23 #include "mach-o.h"
24 #include "bfd.h"
25 #include "libbfd.h"
26 #include "libiberty.h"
27 #include "aout/stab_gnu.h"
28 #include <ctype.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
34 #define FILE_ALIGN(off, algn) \
35 (((off) + ((file_ptr) 1 << (algn)) - 1) & ((file_ptr) -1 << (algn)))
37 static int bfd_mach_o_read_symtab_symbols (bfd *);
39 unsigned int
40 bfd_mach_o_version (bfd *abfd)
42 bfd_mach_o_data_struct *mdata = NULL;
44 BFD_ASSERT (bfd_mach_o_valid (abfd));
45 mdata = bfd_mach_o_get_data (abfd);
47 return mdata->header.version;
50 bfd_boolean
51 bfd_mach_o_valid (bfd *abfd)
53 if (abfd == NULL || abfd->xvec == NULL)
54 return FALSE;
56 if (abfd->xvec->flavour != bfd_target_mach_o_flavour)
57 return FALSE;
59 if (bfd_mach_o_get_data (abfd) == NULL)
60 return FALSE;
61 return TRUE;
64 static INLINE bfd_boolean
65 mach_o_wide_p (bfd_mach_o_header *header)
67 switch (header->version)
69 case 1:
70 return FALSE;
71 case 2:
72 return TRUE;
73 default:
74 BFD_FAIL ();
75 return FALSE;
79 static INLINE bfd_boolean
80 bfd_mach_o_wide_p (bfd *abfd)
82 return mach_o_wide_p (&bfd_mach_o_get_data (abfd)->header);
85 /* Tables to translate well known Mach-O segment/section names to bfd
86 names. Use of canonical names (such as .text or .debug_frame) is required
87 by gdb. */
89 struct mach_o_section_name_xlat
91 const char *bfd_name;
92 const char *mach_o_name;
95 static const struct mach_o_section_name_xlat dwarf_section_names_xlat[] =
97 { ".debug_frame", "__debug_frame" },
98 { ".debug_info", "__debug_info" },
99 { ".debug_abbrev", "__debug_abbrev" },
100 { ".debug_aranges", "__debug_aranges" },
101 { ".debug_macinfo", "__debug_macinfo" },
102 { ".debug_line", "__debug_line" },
103 { ".debug_loc", "__debug_loc" },
104 { ".debug_pubnames", "__debug_pubnames" },
105 { ".debug_pubtypes", "__debug_pubtypes" },
106 { ".debug_str", "__debug_str" },
107 { ".debug_ranges", "__debug_ranges" },
108 { NULL, NULL}
111 static const struct mach_o_section_name_xlat text_section_names_xlat[] =
113 { ".text", "__text" },
114 { ".const", "__const" },
115 { ".cstring", "__cstring" },
116 { ".eh_frame", "__eh_frame" },
117 { NULL, NULL}
120 static const struct mach_o_section_name_xlat data_section_names_xlat[] =
122 { ".data", "__data" },
123 { ".bss", "__bss" },
124 { NULL, NULL}
127 struct mach_o_segment_name_xlat
129 const char *segname;
130 const struct mach_o_section_name_xlat *sections;
133 static const struct mach_o_segment_name_xlat segsec_names_xlat[] =
135 { "__DWARF", dwarf_section_names_xlat },
136 { "__TEXT", text_section_names_xlat },
137 { "__DATA", data_section_names_xlat },
138 { NULL, NULL }
142 /* Mach-O to bfd names. */
144 static char *
145 bfd_mach_o_convert_section_name_to_bfd (bfd *abfd, bfd_mach_o_section *section)
147 const struct mach_o_segment_name_xlat *seg;
148 char *res;
149 unsigned int len;
150 const char *pfx = "";
152 for (seg = segsec_names_xlat; seg->segname; seg++)
154 if (strcmp (seg->segname, section->segname) == 0)
156 const struct mach_o_section_name_xlat *sec;
158 for (sec = seg->sections; sec->mach_o_name; sec++)
160 if (strcmp (sec->mach_o_name, section->sectname) == 0)
162 len = strlen (sec->bfd_name);
163 res = bfd_alloc (abfd, len + 1);
165 if (res == NULL)
166 return NULL;
167 strcpy (res, sec->bfd_name);
168 return res;
174 len = strlen (section->segname) + 1
175 + strlen (section->sectname) + 1;
177 /* Put "LC_SEGMENT." prefix if the segment name is weird (ie doesn't start
178 with an underscore. */
179 if (section->segname[0] != '_')
181 static const char seg_pfx[] = "LC_SEGMENT.";
183 pfx = seg_pfx;
184 len += sizeof (seg_pfx) - 1;
187 res = bfd_alloc (abfd, len);
188 if (res == NULL)
189 return NULL;
190 snprintf (res, len, "%s%s.%s", pfx, section->segname, section->sectname);
191 return res;
194 /* Convert a bfd section name to a Mach-O segment + section name. */
196 static void
197 bfd_mach_o_convert_section_name_to_mach_o (bfd *abfd ATTRIBUTE_UNUSED,
198 asection *sect,
199 bfd_mach_o_section *section)
201 const struct mach_o_segment_name_xlat *seg;
202 const char *name = bfd_get_section_name (abfd, sect);
203 const char *dot;
204 unsigned int len;
205 unsigned int seglen;
206 unsigned int seclen;
208 /* List of well known names. They all start with a dot. */
209 if (name[0] == '.')
210 for (seg = segsec_names_xlat; seg->segname; seg++)
212 const struct mach_o_section_name_xlat *sec;
214 for (sec = seg->sections; sec->mach_o_name; sec++)
216 if (strcmp (sec->bfd_name, name) == 0)
218 strcpy (section->segname, seg->segname);
219 strcpy (section->sectname, sec->mach_o_name);
220 return;
225 /* Strip LC_SEGMENT. prefix. */
226 if (strncmp (name, "LC_SEGMENT.", 11) == 0)
227 name += 11;
229 /* Find a dot. */
230 dot = strchr (name, '.');
231 len = strlen (name);
233 /* Try to split name into segment and section names. */
234 if (dot && dot != name)
236 seglen = dot - name;
237 seclen = len - (dot + 1 - name);
239 if (seglen < 16 && seclen < 16)
241 memcpy (section->segname, name, seglen);
242 section->segname[seglen] = 0;
243 memcpy (section->sectname, dot + 1, seclen);
244 section->sectname[seclen] = 0;
245 return;
249 if (len > 16)
250 len = 16;
251 memcpy (section->segname, name, len);
252 section->segname[len] = 0;
253 memcpy (section->sectname, name, len);
254 section->sectname[len] = 0;
257 /* Return the size of an entry for section SEC.
258 Must be called only for symbol pointer section and symbol stubs
259 sections. */
261 static unsigned int
262 bfd_mach_o_section_get_entry_size (bfd *abfd, bfd_mach_o_section *sec)
264 switch (sec->flags & BFD_MACH_O_SECTION_TYPE_MASK)
266 case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS:
267 case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS:
268 return bfd_mach_o_wide_p (abfd) ? 8 : 4;
269 case BFD_MACH_O_S_SYMBOL_STUBS:
270 return sec->reserved2;
271 default:
272 BFD_FAIL ();
273 return 0;
277 /* Return the number of indirect symbols for a section.
278 Must be called only for symbol pointer section and symbol stubs
279 sections. */
281 static unsigned int
282 bfd_mach_o_section_get_nbr_indirect (bfd *abfd, bfd_mach_o_section *sec)
284 unsigned int elsz;
286 elsz = bfd_mach_o_section_get_entry_size (abfd, sec);
287 if (elsz == 0)
288 return 0;
289 else
290 return sec->size / elsz;
294 /* Copy any private info we understand from the input symbol
295 to the output symbol. */
297 bfd_boolean
298 bfd_mach_o_bfd_copy_private_symbol_data (bfd *ibfd ATTRIBUTE_UNUSED,
299 asymbol *isymbol ATTRIBUTE_UNUSED,
300 bfd *obfd ATTRIBUTE_UNUSED,
301 asymbol *osymbol ATTRIBUTE_UNUSED)
303 return TRUE;
306 /* Copy any private info we understand from the input section
307 to the output section. */
309 bfd_boolean
310 bfd_mach_o_bfd_copy_private_section_data (bfd *ibfd ATTRIBUTE_UNUSED,
311 asection *isection ATTRIBUTE_UNUSED,
312 bfd *obfd ATTRIBUTE_UNUSED,
313 asection *osection ATTRIBUTE_UNUSED)
315 return TRUE;
318 /* Copy any private info we understand from the input bfd
319 to the output bfd. */
321 bfd_boolean
322 bfd_mach_o_bfd_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
324 if (bfd_get_flavour (ibfd) != bfd_target_mach_o_flavour
325 || bfd_get_flavour (obfd) != bfd_target_mach_o_flavour)
326 return TRUE;
328 BFD_ASSERT (bfd_mach_o_valid (ibfd));
329 BFD_ASSERT (bfd_mach_o_valid (obfd));
331 /* FIXME: copy commands. */
333 return TRUE;
336 /* Count the total number of symbols. */
338 static long
339 bfd_mach_o_count_symbols (bfd *abfd)
341 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
343 if (mdata->symtab == NULL)
344 return 0;
345 return mdata->symtab->nsyms;
348 long
349 bfd_mach_o_get_symtab_upper_bound (bfd *abfd)
351 long nsyms = bfd_mach_o_count_symbols (abfd);
353 return ((nsyms + 1) * sizeof (asymbol *));
356 long
357 bfd_mach_o_canonicalize_symtab (bfd *abfd, asymbol **alocation)
359 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
360 long nsyms = bfd_mach_o_count_symbols (abfd);
361 bfd_mach_o_symtab_command *sym = mdata->symtab;
362 unsigned long j;
364 if (nsyms < 0)
365 return nsyms;
367 if (bfd_mach_o_read_symtab_symbols (abfd) != 0)
369 fprintf (stderr,
370 "bfd_mach_o_canonicalize_symtab: unable to load symbols\n");
371 return 0;
374 BFD_ASSERT (sym->symbols != NULL);
376 for (j = 0; j < sym->nsyms; j++)
377 alocation[j] = &sym->symbols[j].symbol;
379 alocation[j] = NULL;
381 return nsyms;
384 long
385 bfd_mach_o_get_synthetic_symtab (bfd *abfd,
386 long symcount ATTRIBUTE_UNUSED,
387 asymbol **syms ATTRIBUTE_UNUSED,
388 long dynsymcount ATTRIBUTE_UNUSED,
389 asymbol **dynsyms ATTRIBUTE_UNUSED,
390 asymbol **ret)
392 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
393 bfd_mach_o_dysymtab_command *dysymtab = mdata->dysymtab;
394 bfd_mach_o_symtab_command *symtab = mdata->symtab;
395 asymbol *s;
396 unsigned long count, i, j, n;
397 size_t size;
398 char *names;
399 char *nul_name;
401 *ret = NULL;
403 if (dysymtab == NULL || symtab == NULL || symtab->symbols == NULL)
404 return 0;
406 if (dysymtab->nindirectsyms == 0)
407 return 0;
409 count = dysymtab->nindirectsyms;
410 size = count * sizeof (asymbol) + 1;
412 for (j = 0; j < count; j++)
414 unsigned int isym = dysymtab->indirect_syms[j];
416 if (isym < symtab->nsyms && symtab->symbols[isym].symbol.name)
417 size += strlen (symtab->symbols[isym].symbol.name) + sizeof ("$stub");
420 s = *ret = (asymbol *) bfd_malloc (size);
421 if (s == NULL)
422 return -1;
423 names = (char *) (s + count);
424 nul_name = names;
425 *names++ = 0;
427 n = 0;
428 for (i = 0; i < mdata->nsects; i++)
430 bfd_mach_o_section *sec = mdata->sections[i];
431 unsigned int j, first, last;
432 bfd_mach_o_symtab_command *symtab = mdata->symtab;
433 bfd_vma addr;
434 bfd_vma entry_size;
436 switch (sec->flags & BFD_MACH_O_SECTION_TYPE_MASK)
438 case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS:
439 case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS:
440 case BFD_MACH_O_S_SYMBOL_STUBS:
441 first = sec->reserved1;
442 last = first + bfd_mach_o_section_get_nbr_indirect (abfd, sec);
443 addr = sec->addr;
444 entry_size = bfd_mach_o_section_get_entry_size (abfd, sec);
445 for (j = first; j < last; j++)
447 unsigned int isym = dysymtab->indirect_syms[j];
449 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
450 s->section = sec->bfdsection;
451 s->value = addr - sec->addr;
452 s->udata.p = NULL;
454 if (isym < symtab->nsyms
455 && symtab->symbols[isym].symbol.name)
457 const char *sym = symtab->symbols[isym].symbol.name;
458 size_t len;
460 s->name = names;
461 len = strlen (sym);
462 memcpy (names, sym, len);
463 names += len;
464 memcpy (names, "$stub", sizeof ("$stub"));
465 names += sizeof ("$stub");
467 else
468 s->name = nul_name;
470 addr += entry_size;
471 s++;
472 n++;
474 break;
475 default:
476 break;
480 return n;
483 void
484 bfd_mach_o_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
485 asymbol *symbol,
486 symbol_info *ret)
488 bfd_symbol_info (symbol, ret);
491 void
492 bfd_mach_o_print_symbol (bfd *abfd,
493 PTR afile,
494 asymbol *symbol,
495 bfd_print_symbol_type how)
497 FILE *file = (FILE *) afile;
498 const char *name;
499 bfd_mach_o_asymbol *asym = (bfd_mach_o_asymbol *)symbol;
501 switch (how)
503 case bfd_print_symbol_name:
504 fprintf (file, "%s", symbol->name);
505 break;
506 default:
507 bfd_print_symbol_vandf (abfd, (PTR) file, symbol);
508 if (asym->n_type & BFD_MACH_O_N_STAB)
509 name = bfd_get_stab_name (asym->n_type);
510 else
511 switch (asym->n_type & BFD_MACH_O_N_TYPE)
513 case BFD_MACH_O_N_UNDF:
514 name = "UND";
515 break;
516 case BFD_MACH_O_N_ABS:
517 name = "ABS";
518 break;
519 case BFD_MACH_O_N_INDR:
520 name = "INDR";
521 break;
522 case BFD_MACH_O_N_PBUD:
523 name = "PBUD";
524 break;
525 case BFD_MACH_O_N_SECT:
526 name = "SECT";
527 break;
528 default:
529 name = "???";
530 break;
532 if (name == NULL)
533 name = "";
534 fprintf (file, " %02x %-6s %02x %04x",
535 asym->n_type, name, asym->n_sect, asym->n_desc);
536 if ((asym->n_type & BFD_MACH_O_N_STAB) == 0
537 && (asym->n_type & BFD_MACH_O_N_TYPE) == BFD_MACH_O_N_SECT)
538 fprintf (file, " %-5s", symbol->section->name);
539 fprintf (file, " %s", symbol->name);
543 static void
544 bfd_mach_o_convert_architecture (bfd_mach_o_cpu_type mtype,
545 bfd_mach_o_cpu_subtype msubtype ATTRIBUTE_UNUSED,
546 enum bfd_architecture *type,
547 unsigned long *subtype)
549 *subtype = bfd_arch_unknown;
551 switch (mtype)
553 case BFD_MACH_O_CPU_TYPE_VAX: *type = bfd_arch_vax; break;
554 case BFD_MACH_O_CPU_TYPE_MC680x0: *type = bfd_arch_m68k; break;
555 case BFD_MACH_O_CPU_TYPE_I386:
556 *type = bfd_arch_i386;
557 *subtype = bfd_mach_i386_i386;
558 break;
559 case BFD_MACH_O_CPU_TYPE_X86_64:
560 *type = bfd_arch_i386;
561 *subtype = bfd_mach_x86_64;
562 break;
563 case BFD_MACH_O_CPU_TYPE_MIPS: *type = bfd_arch_mips; break;
564 case BFD_MACH_O_CPU_TYPE_MC98000: *type = bfd_arch_m98k; break;
565 case BFD_MACH_O_CPU_TYPE_HPPA: *type = bfd_arch_hppa; break;
566 case BFD_MACH_O_CPU_TYPE_ARM: *type = bfd_arch_arm; break;
567 case BFD_MACH_O_CPU_TYPE_MC88000: *type = bfd_arch_m88k; break;
568 case BFD_MACH_O_CPU_TYPE_SPARC:
569 *type = bfd_arch_sparc;
570 *subtype = bfd_mach_sparc;
571 break;
572 case BFD_MACH_O_CPU_TYPE_I860: *type = bfd_arch_i860; break;
573 case BFD_MACH_O_CPU_TYPE_ALPHA: *type = bfd_arch_alpha; break;
574 case BFD_MACH_O_CPU_TYPE_POWERPC:
575 *type = bfd_arch_powerpc;
576 *subtype = bfd_mach_ppc;
577 break;
578 case BFD_MACH_O_CPU_TYPE_POWERPC_64:
579 *type = bfd_arch_powerpc;
580 *subtype = bfd_mach_ppc64;
581 break;
582 default:
583 *type = bfd_arch_unknown;
584 break;
588 static bfd_boolean
589 bfd_mach_o_write_header (bfd *abfd, bfd_mach_o_header *header)
591 unsigned char buf[32];
592 unsigned int size;
594 size = mach_o_wide_p (header) ?
595 BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE;
597 bfd_h_put_32 (abfd, header->magic, buf + 0);
598 bfd_h_put_32 (abfd, header->cputype, buf + 4);
599 bfd_h_put_32 (abfd, header->cpusubtype, buf + 8);
600 bfd_h_put_32 (abfd, header->filetype, buf + 12);
601 bfd_h_put_32 (abfd, header->ncmds, buf + 16);
602 bfd_h_put_32 (abfd, header->sizeofcmds, buf + 20);
603 bfd_h_put_32 (abfd, header->flags, buf + 24);
605 if (mach_o_wide_p (header))
606 bfd_h_put_32 (abfd, header->reserved, buf + 28);
608 if (bfd_seek (abfd, 0, SEEK_SET) != 0
609 || bfd_bwrite ((PTR) buf, size, abfd) != size)
610 return FALSE;
612 return TRUE;
615 static int
616 bfd_mach_o_write_thread (bfd *abfd, bfd_mach_o_load_command *command)
618 bfd_mach_o_thread_command *cmd = &command->command.thread;
619 unsigned int i;
620 unsigned char buf[8];
621 unsigned int offset;
622 unsigned int nflavours;
624 BFD_ASSERT ((command->type == BFD_MACH_O_LC_THREAD)
625 || (command->type == BFD_MACH_O_LC_UNIXTHREAD));
627 offset = 8;
628 nflavours = 0;
629 for (i = 0; i < cmd->nflavours; i++)
631 BFD_ASSERT ((cmd->flavours[i].size % 4) == 0);
632 BFD_ASSERT (cmd->flavours[i].offset == (command->offset + offset + 8));
634 bfd_h_put_32 (abfd, cmd->flavours[i].flavour, buf);
635 bfd_h_put_32 (abfd, (cmd->flavours[i].size / 4), buf + 4);
637 if (bfd_seek (abfd, command->offset + offset, SEEK_SET) != 0
638 || bfd_bwrite ((PTR) buf, 8, abfd) != 8)
639 return -1;
641 offset += cmd->flavours[i].size + 8;
644 return 0;
647 long
648 bfd_mach_o_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
649 asection *asect)
651 return (asect->reloc_count + 1) * sizeof (arelent *);
654 static int
655 bfd_mach_o_canonicalize_one_reloc (bfd *abfd, char *buf,
656 arelent *res, asymbol **syms)
658 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
659 bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
660 bfd_mach_o_reloc_info reloc;
661 bfd_vma addr;
662 bfd_vma symnum;
663 asymbol **sym;
665 addr = bfd_get_32 (abfd, buf + 0);
666 symnum = bfd_get_32 (abfd, buf + 4);
668 if (addr & BFD_MACH_O_SR_SCATTERED)
670 unsigned int j;
672 /* Scattered relocation.
673 Extract section and offset from r_value. */
674 res->sym_ptr_ptr = NULL;
675 res->addend = 0;
676 for (j = 0; j < mdata->nsects; j++)
678 bfd_mach_o_section *sect = mdata->sections[j];
679 if (symnum >= sect->addr && symnum < sect->addr + sect->size)
681 res->sym_ptr_ptr = sect->bfdsection->symbol_ptr_ptr;
682 res->addend = symnum - sect->addr;
683 break;
686 res->address = BFD_MACH_O_GET_SR_ADDRESS (addr);
687 reloc.r_type = BFD_MACH_O_GET_SR_TYPE (addr);
688 reloc.r_length = BFD_MACH_O_GET_SR_LENGTH (addr);
689 reloc.r_pcrel = addr & BFD_MACH_O_SR_PCREL;
690 reloc.r_scattered = 1;
692 else
694 unsigned int num = BFD_MACH_O_GET_R_SYMBOLNUM (symnum);
695 res->addend = 0;
696 res->address = addr;
697 if (symnum & BFD_MACH_O_R_EXTERN)
698 sym = syms + num;
699 else
701 BFD_ASSERT (num != 0);
702 BFD_ASSERT (num <= mdata->nsects);
703 sym = mdata->sections[num - 1]->bfdsection->symbol_ptr_ptr;
705 res->sym_ptr_ptr = sym;
706 reloc.r_type = BFD_MACH_O_GET_R_TYPE (symnum);
707 reloc.r_length = BFD_MACH_O_GET_R_LENGTH (symnum);
708 reloc.r_pcrel = (symnum & BFD_MACH_O_R_PCREL) ? 1 : 0;
709 reloc.r_scattered = 0;
712 if (!(*bed->_bfd_mach_o_swap_reloc_in)(res, &reloc))
713 return -1;
714 return 0;
717 static int
718 bfd_mach_o_canonicalize_relocs (bfd *abfd, unsigned long filepos,
719 unsigned long count,
720 arelent *res, asymbol **syms)
722 unsigned long i;
723 char *native_relocs;
724 bfd_size_type native_size;
726 /* Allocate and read relocs. */
727 native_size = count * BFD_MACH_O_RELENT_SIZE;
728 native_relocs = bfd_malloc (native_size);
729 if (native_relocs == NULL)
730 return -1;
732 if (bfd_seek (abfd, filepos, SEEK_SET) != 0
733 || bfd_bread (native_relocs, native_size, abfd) != native_size)
734 goto err;
736 for (i = 0; i < count; i++)
738 char *buf = native_relocs + BFD_MACH_O_RELENT_SIZE * i;
740 if (bfd_mach_o_canonicalize_one_reloc (abfd, buf, &res[i], syms) < 0)
741 goto err;
743 free (native_relocs);
744 return i;
745 err:
746 free (native_relocs);
747 return -1;
750 long
751 bfd_mach_o_canonicalize_reloc (bfd *abfd, asection *asect,
752 arelent **rels, asymbol **syms)
754 bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
755 unsigned long i;
756 arelent *res;
758 if (asect->reloc_count == 0)
759 return 0;
761 /* No need to go further if we don't know how to read relocs. */
762 if (bed->_bfd_mach_o_swap_reloc_in == NULL)
763 return 0;
765 res = bfd_malloc (asect->reloc_count * sizeof (arelent));
766 if (res == NULL)
767 return -1;
769 if (bfd_mach_o_canonicalize_relocs (abfd, asect->rel_filepos,
770 asect->reloc_count, res, syms) < 0)
772 free (res);
773 return -1;
776 for (i = 0; i < asect->reloc_count; i++)
777 rels[i] = &res[i];
778 rels[i] = NULL;
779 asect->relocation = res;
781 return i;
784 long
785 bfd_mach_o_get_dynamic_reloc_upper_bound (bfd *abfd)
787 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
789 if (mdata->dysymtab == NULL)
790 return 1;
791 return (mdata->dysymtab->nextrel + mdata->dysymtab->nlocrel)
792 * sizeof (arelent *);
795 long
796 bfd_mach_o_canonicalize_dynamic_reloc (bfd *abfd, arelent **rels,
797 struct bfd_symbol **syms)
799 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
800 bfd_mach_o_dysymtab_command *dysymtab = mdata->dysymtab;
801 bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
802 unsigned long i;
803 arelent *res;
805 if (dysymtab == NULL)
806 return 0;
807 if (dysymtab->nextrel == 0 && dysymtab->nlocrel == 0)
808 return 0;
810 /* No need to go further if we don't know how to read relocs. */
811 if (bed->_bfd_mach_o_swap_reloc_in == NULL)
812 return 0;
814 res = bfd_malloc ((dysymtab->nextrel + dysymtab->nlocrel) * sizeof (arelent));
815 if (res == NULL)
816 return -1;
818 if (bfd_mach_o_canonicalize_relocs (abfd, dysymtab->extreloff,
819 dysymtab->nextrel, res, syms) < 0)
821 free (res);
822 return -1;
825 if (bfd_mach_o_canonicalize_relocs (abfd, dysymtab->locreloff,
826 dysymtab->nlocrel,
827 res + dysymtab->nextrel, syms) < 0)
829 free (res);
830 return -1;
833 for (i = 0; i < dysymtab->nextrel + dysymtab->nlocrel; i++)
834 rels[i] = &res[i];
835 rels[i] = NULL;
836 return i;
839 static bfd_boolean
840 bfd_mach_o_write_relocs (bfd *abfd, bfd_mach_o_section *section)
842 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
843 unsigned int i;
844 arelent **entries;
845 asection *sec;
846 bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
848 sec = section->bfdsection;
849 if (sec->reloc_count == 0)
850 return TRUE;
852 if (bed->_bfd_mach_o_swap_reloc_out == NULL)
853 return TRUE;
855 /* Allocate relocation room. */
856 mdata->filelen = FILE_ALIGN(mdata->filelen, 2);
857 section->nreloc = sec->reloc_count;
858 sec->rel_filepos = mdata->filelen;
859 section->reloff = sec->rel_filepos;
860 mdata->filelen += sec->reloc_count * BFD_MACH_O_RELENT_SIZE;
862 if (bfd_seek (abfd, section->reloff, SEEK_SET) != 0)
863 return FALSE;
865 /* Convert and write. */
866 entries = section->bfdsection->orelocation;
867 for (i = 0; i < section->nreloc; i++)
869 arelent *rel = entries[i];
870 char buf[8];
871 bfd_mach_o_reloc_info info, *pinfo = &info;
873 /* Convert relocation to an intermediate representation. */
874 if (!(*bed->_bfd_mach_o_swap_reloc_out) (rel, pinfo))
875 return FALSE;
877 /* Lower the relocation info. */
878 if (pinfo->r_scattered)
880 unsigned long v;
882 v = BFD_MACH_O_SR_SCATTERED
883 | (pinfo->r_pcrel ? BFD_MACH_O_SR_PCREL : 0)
884 | BFD_MACH_O_SET_SR_LENGTH(pinfo->r_length)
885 | BFD_MACH_O_SET_SR_TYPE(pinfo->r_type)
886 | BFD_MACH_O_SET_SR_ADDRESS(pinfo->r_address);
887 bfd_put_32 (abfd, v, buf);
888 bfd_put_32 (abfd, pinfo->r_value, buf + 4);
890 else
892 unsigned long v;
894 bfd_put_32 (abfd, pinfo->r_address, buf);
895 v = BFD_MACH_O_SET_R_SYMBOLNUM (pinfo->r_value)
896 | (pinfo->r_pcrel ? BFD_MACH_O_R_PCREL : 0)
897 | BFD_MACH_O_SET_R_LENGTH (pinfo->r_length)
898 | (pinfo->r_extern ? BFD_MACH_O_R_EXTERN : 0)
899 | BFD_MACH_O_SET_R_TYPE (pinfo->r_type);
900 bfd_put_32 (abfd, v, buf + 4);
903 if (bfd_bwrite ((PTR) buf, BFD_MACH_O_RELENT_SIZE, abfd)
904 != BFD_MACH_O_RELENT_SIZE)
905 return FALSE;
907 return TRUE;
910 static int
911 bfd_mach_o_write_section_32 (bfd *abfd, bfd_mach_o_section *section)
913 unsigned char buf[BFD_MACH_O_SECTION_SIZE];
915 memcpy (buf, section->sectname, 16);
916 memcpy (buf + 16, section->segname, 16);
917 bfd_h_put_32 (abfd, section->addr, buf + 32);
918 bfd_h_put_32 (abfd, section->size, buf + 36);
919 bfd_h_put_32 (abfd, section->offset, buf + 40);
920 bfd_h_put_32 (abfd, section->align, buf + 44);
921 bfd_h_put_32 (abfd, section->reloff, buf + 48);
922 bfd_h_put_32 (abfd, section->nreloc, buf + 52);
923 bfd_h_put_32 (abfd, section->flags, buf + 56);
924 bfd_h_put_32 (abfd, section->reserved1, buf + 60);
925 bfd_h_put_32 (abfd, section->reserved2, buf + 64);
927 if (bfd_bwrite ((PTR) buf, BFD_MACH_O_SECTION_SIZE, abfd)
928 != BFD_MACH_O_SECTION_SIZE)
929 return -1;
931 return 0;
934 static int
935 bfd_mach_o_write_section_64 (bfd *abfd, bfd_mach_o_section *section)
937 unsigned char buf[BFD_MACH_O_SECTION_64_SIZE];
939 memcpy (buf, section->sectname, 16);
940 memcpy (buf + 16, section->segname, 16);
941 bfd_h_put_64 (abfd, section->addr, buf + 32);
942 bfd_h_put_64 (abfd, section->size, buf + 40);
943 bfd_h_put_32 (abfd, section->offset, buf + 48);
944 bfd_h_put_32 (abfd, section->align, buf + 52);
945 bfd_h_put_32 (abfd, section->reloff, buf + 56);
946 bfd_h_put_32 (abfd, section->nreloc, buf + 60);
947 bfd_h_put_32 (abfd, section->flags, buf + 64);
948 bfd_h_put_32 (abfd, section->reserved1, buf + 68);
949 bfd_h_put_32 (abfd, section->reserved2, buf + 72);
950 bfd_h_put_32 (abfd, section->reserved3, buf + 76);
952 if (bfd_bwrite ((PTR) buf, BFD_MACH_O_SECTION_64_SIZE, abfd)
953 != BFD_MACH_O_SECTION_64_SIZE)
954 return -1;
956 return 0;
959 static int
960 bfd_mach_o_write_segment_32 (bfd *abfd, bfd_mach_o_load_command *command)
962 unsigned char buf[BFD_MACH_O_LC_SEGMENT_SIZE];
963 bfd_mach_o_segment_command *seg = &command->command.segment;
964 unsigned long i;
966 BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT);
968 for (i = 0; i < seg->nsects; i++)
969 if (!bfd_mach_o_write_relocs (abfd, &seg->sections[i]))
970 return -1;
972 memcpy (buf, seg->segname, 16);
973 bfd_h_put_32 (abfd, seg->vmaddr, buf + 16);
974 bfd_h_put_32 (abfd, seg->vmsize, buf + 20);
975 bfd_h_put_32 (abfd, seg->fileoff, buf + 24);
976 bfd_h_put_32 (abfd, seg->filesize, buf + 28);
977 bfd_h_put_32 (abfd, seg->maxprot, buf + 32);
978 bfd_h_put_32 (abfd, seg->initprot, buf + 36);
979 bfd_h_put_32 (abfd, seg->nsects, buf + 40);
980 bfd_h_put_32 (abfd, seg->flags, buf + 44);
982 if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
983 || (bfd_bwrite ((PTR) buf, BFD_MACH_O_LC_SEGMENT_SIZE - 8, abfd)
984 != BFD_MACH_O_LC_SEGMENT_SIZE - 8))
985 return -1;
987 for (i = 0; i < seg->nsects; i++)
988 if (bfd_mach_o_write_section_32 (abfd, &seg->sections[i]))
989 return -1;
991 return 0;
994 static int
995 bfd_mach_o_write_segment_64 (bfd *abfd, bfd_mach_o_load_command *command)
997 unsigned char buf[BFD_MACH_O_LC_SEGMENT_64_SIZE];
998 bfd_mach_o_segment_command *seg = &command->command.segment;
999 unsigned long i;
1001 BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT_64);
1003 for (i = 0; i < seg->nsects; i++)
1004 if (!bfd_mach_o_write_relocs (abfd, &seg->sections[i]))
1005 return -1;
1007 memcpy (buf, seg->segname, 16);
1008 bfd_h_put_64 (abfd, seg->vmaddr, buf + 16);
1009 bfd_h_put_64 (abfd, seg->vmsize, buf + 24);
1010 bfd_h_put_64 (abfd, seg->fileoff, buf + 32);
1011 bfd_h_put_64 (abfd, seg->filesize, buf + 40);
1012 bfd_h_put_32 (abfd, seg->maxprot, buf + 48);
1013 bfd_h_put_32 (abfd, seg->initprot, buf + 52);
1014 bfd_h_put_32 (abfd, seg->nsects, buf + 56);
1015 bfd_h_put_32 (abfd, seg->flags, buf + 60);
1017 if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
1018 || (bfd_bwrite ((PTR) buf, BFD_MACH_O_LC_SEGMENT_64_SIZE - 8, abfd)
1019 != BFD_MACH_O_LC_SEGMENT_64_SIZE - 8))
1020 return -1;
1022 for (i = 0; i < seg->nsects; i++)
1023 if (bfd_mach_o_write_section_64 (abfd, &seg->sections[i]))
1024 return -1;
1026 return 0;
1029 static bfd_boolean
1030 bfd_mach_o_write_symtab (bfd *abfd, bfd_mach_o_load_command *command)
1032 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
1033 bfd_mach_o_symtab_command *sym = &command->command.symtab;
1034 unsigned char buf[16];
1035 unsigned long i;
1036 unsigned int wide = bfd_mach_o_wide_p (abfd);
1037 unsigned int symlen = wide ? BFD_MACH_O_NLIST_64_SIZE : BFD_MACH_O_NLIST_SIZE;
1038 struct bfd_strtab_hash *strtab;
1039 asymbol **symbols = bfd_get_outsymbols (abfd);
1041 BFD_ASSERT (command->type == BFD_MACH_O_LC_SYMTAB);
1043 /* Write the symbols first. */
1044 mdata->filelen = FILE_ALIGN(mdata->filelen, wide ? 3 : 2);
1045 sym->symoff = mdata->filelen;
1046 if (bfd_seek (abfd, sym->symoff, SEEK_SET) != 0)
1047 return FALSE;
1049 sym->nsyms = bfd_get_symcount (abfd);
1050 mdata->filelen += sym->nsyms * symlen;
1052 strtab = _bfd_stringtab_init ();
1053 if (strtab == NULL)
1054 return FALSE;
1056 for (i = 0; i < sym->nsyms; i++)
1058 unsigned char buf[16];
1059 bfd_size_type index;
1060 bfd_mach_o_asymbol *s = (bfd_mach_o_asymbol *)symbols[i];
1062 /* Compute name index. */
1063 /* An index of 0 always means the empty string. */
1064 if (s->symbol.name == 0 || s->symbol.name[0] == '\0')
1065 index = 0;
1066 else
1068 index = _bfd_stringtab_add (strtab, s->symbol.name, TRUE, FALSE);
1069 if (index == (bfd_size_type) -1)
1070 goto err;
1072 bfd_h_put_32 (abfd, index, buf);
1073 bfd_h_put_8 (abfd, s->n_type, buf + 4);
1074 bfd_h_put_8 (abfd, s->n_sect, buf + 5);
1075 bfd_h_put_16 (abfd, s->n_desc, buf + 6);
1076 if (wide)
1077 bfd_h_put_64 (abfd, s->symbol.section->vma + s->symbol.value, buf + 8);
1078 else
1079 bfd_h_put_32 (abfd, s->symbol.section->vma + s->symbol.value, buf + 8);
1081 if (bfd_bwrite ((PTR) buf, symlen, abfd) != symlen)
1082 goto err;
1084 sym->strsize = _bfd_stringtab_size (strtab);
1085 sym->stroff = mdata->filelen;
1086 mdata->filelen += sym->strsize;
1088 if (_bfd_stringtab_emit (abfd, strtab) != TRUE)
1089 goto err;
1090 _bfd_stringtab_free (strtab);
1092 /* The command. */
1093 bfd_h_put_32 (abfd, sym->symoff, buf);
1094 bfd_h_put_32 (abfd, sym->nsyms, buf + 4);
1095 bfd_h_put_32 (abfd, sym->stroff, buf + 8);
1096 bfd_h_put_32 (abfd, sym->strsize, buf + 12);
1098 if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
1099 || bfd_bwrite ((PTR) buf, 16, abfd) != 16)
1100 return FALSE;
1102 return TRUE;
1104 err:
1105 _bfd_stringtab_free (strtab);
1106 return FALSE;
1109 /* Process the symbols and generate Mach-O specific fields.
1110 Number them. */
1112 static bfd_boolean
1113 bfd_mach_o_mangle_symbols (bfd *abfd)
1115 unsigned long i;
1116 asymbol **symbols = bfd_get_outsymbols (abfd);
1118 for (i = 0; i < bfd_get_symcount (abfd); i++)
1120 bfd_mach_o_asymbol *s = (bfd_mach_o_asymbol *)symbols[i];
1122 if (s->n_type == BFD_MACH_O_N_UNDF && !(s->symbol.flags & BSF_DEBUGGING))
1124 /* As genuine Mach-O symbols type shouldn't be N_UNDF (undefined
1125 symbols should be N_UNDEF | N_EXT), we suppose the back-end
1126 values haven't been set. */
1127 if (s->symbol.section == bfd_abs_section_ptr)
1128 s->n_type = BFD_MACH_O_N_ABS;
1129 else if (s->symbol.section == bfd_und_section_ptr)
1131 s->n_type = BFD_MACH_O_N_UNDF;
1132 if (s->symbol.flags & BSF_WEAK)
1133 s->n_desc |= BFD_MACH_O_N_WEAK_REF;
1135 else if (s->symbol.section == bfd_com_section_ptr)
1136 s->n_type = BFD_MACH_O_N_UNDF | BFD_MACH_O_N_EXT;
1137 else
1138 s->n_type = BFD_MACH_O_N_SECT;
1140 if (s->symbol.flags & BSF_GLOBAL)
1141 s->n_type |= BFD_MACH_O_N_EXT;
1144 /* Compute section index. */
1145 if (s->symbol.section != bfd_abs_section_ptr
1146 && s->symbol.section != bfd_und_section_ptr
1147 && s->symbol.section != bfd_com_section_ptr)
1148 s->n_sect = s->symbol.section->target_index;
1150 /* Number symbols. */
1151 s->symbol.udata.i = i;
1153 return TRUE;
1156 bfd_boolean
1157 bfd_mach_o_write_contents (bfd *abfd)
1159 unsigned int i;
1160 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
1162 if (mdata->header.ncmds == 0)
1163 if (!bfd_mach_o_build_commands (abfd))
1164 return FALSE;
1166 /* Now write header information. */
1167 if (mdata->header.filetype == 0)
1169 if (abfd->flags & EXEC_P)
1170 mdata->header.filetype = BFD_MACH_O_MH_EXECUTE;
1171 else if (abfd->flags & DYNAMIC)
1172 mdata->header.filetype = BFD_MACH_O_MH_DYLIB;
1173 else
1174 mdata->header.filetype = BFD_MACH_O_MH_OBJECT;
1176 if (!bfd_mach_o_write_header (abfd, &mdata->header))
1177 return FALSE;
1179 /* Assign a number to each symbols. */
1180 if (!bfd_mach_o_mangle_symbols (abfd))
1181 return FALSE;
1183 for (i = 0; i < mdata->header.ncmds; i++)
1185 unsigned char buf[8];
1186 bfd_mach_o_load_command *cur = &mdata->commands[i];
1187 unsigned long typeflag;
1189 typeflag = cur->type | (cur->type_required ? BFD_MACH_O_LC_REQ_DYLD : 0);
1191 bfd_h_put_32 (abfd, typeflag, buf);
1192 bfd_h_put_32 (abfd, cur->len, buf + 4);
1194 if (bfd_seek (abfd, cur->offset, SEEK_SET) != 0
1195 || bfd_bwrite ((PTR) buf, 8, abfd) != 8)
1196 return FALSE;
1198 switch (cur->type)
1200 case BFD_MACH_O_LC_SEGMENT:
1201 if (bfd_mach_o_write_segment_32 (abfd, cur) != 0)
1202 return FALSE;
1203 break;
1204 case BFD_MACH_O_LC_SEGMENT_64:
1205 if (bfd_mach_o_write_segment_64 (abfd, cur) != 0)
1206 return FALSE;
1207 break;
1208 case BFD_MACH_O_LC_SYMTAB:
1209 if (!bfd_mach_o_write_symtab (abfd, cur))
1210 return FALSE;
1211 break;
1212 case BFD_MACH_O_LC_SYMSEG:
1213 break;
1214 case BFD_MACH_O_LC_THREAD:
1215 case BFD_MACH_O_LC_UNIXTHREAD:
1216 if (bfd_mach_o_write_thread (abfd, cur) != 0)
1217 return FALSE;
1218 break;
1219 case BFD_MACH_O_LC_LOADFVMLIB:
1220 case BFD_MACH_O_LC_IDFVMLIB:
1221 case BFD_MACH_O_LC_IDENT:
1222 case BFD_MACH_O_LC_FVMFILE:
1223 case BFD_MACH_O_LC_PREPAGE:
1224 case BFD_MACH_O_LC_DYSYMTAB:
1225 case BFD_MACH_O_LC_LOAD_DYLIB:
1226 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB:
1227 case BFD_MACH_O_LC_ID_DYLIB:
1228 case BFD_MACH_O_LC_REEXPORT_DYLIB:
1229 case BFD_MACH_O_LC_LOAD_DYLINKER:
1230 case BFD_MACH_O_LC_ID_DYLINKER:
1231 case BFD_MACH_O_LC_PREBOUND_DYLIB:
1232 case BFD_MACH_O_LC_ROUTINES:
1233 case BFD_MACH_O_LC_SUB_FRAMEWORK:
1234 break;
1235 default:
1236 fprintf (stderr,
1237 "unable to write unknown load command 0x%lx\n",
1238 (unsigned long) cur->type);
1239 return FALSE;
1243 return TRUE;
1246 /* Build Mach-O load commands from the sections. */
1248 bfd_boolean
1249 bfd_mach_o_build_commands (bfd *abfd)
1251 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
1252 unsigned int wide = mach_o_wide_p (&mdata->header);
1253 bfd_mach_o_segment_command *seg;
1254 bfd_mach_o_section *sections;
1255 asection *sec;
1256 bfd_mach_o_load_command *cmd;
1257 bfd_mach_o_load_command *symtab_cmd;
1258 int target_index;
1260 /* Return now if commands are already built. */
1261 if (mdata->header.ncmds)
1262 return FALSE;
1264 /* Very simple version: 1 command (segment) containing all sections. */
1265 mdata->header.ncmds = 2;
1266 mdata->commands = bfd_alloc (abfd, mdata->header.ncmds
1267 * sizeof (bfd_mach_o_load_command));
1268 if (mdata->commands == NULL)
1269 return FALSE;
1270 cmd = &mdata->commands[0];
1271 seg = &cmd->command.segment;
1273 seg->nsects = bfd_count_sections (abfd);
1274 sections = bfd_alloc (abfd, seg->nsects * sizeof (bfd_mach_o_section));
1275 if (sections == NULL)
1276 return FALSE;
1277 seg->sections = sections;
1279 /* Set segment command. */
1280 if (wide)
1282 cmd->type = BFD_MACH_O_LC_SEGMENT_64;
1283 cmd->offset = BFD_MACH_O_HEADER_64_SIZE;
1284 cmd->len = BFD_MACH_O_LC_SEGMENT_64_SIZE
1285 + BFD_MACH_O_SECTION_64_SIZE * seg->nsects;
1287 else
1289 cmd->type = BFD_MACH_O_LC_SEGMENT;
1290 cmd->offset = BFD_MACH_O_HEADER_SIZE;
1291 cmd->len = BFD_MACH_O_LC_SEGMENT_SIZE
1292 + BFD_MACH_O_SECTION_SIZE * seg->nsects;
1294 cmd->type_required = FALSE;
1295 mdata->header.sizeofcmds = cmd->len;
1296 mdata->filelen = cmd->offset + cmd->len;
1298 /* Set symtab command. */
1299 symtab_cmd = &mdata->commands[1];
1301 symtab_cmd->type = BFD_MACH_O_LC_SYMTAB;
1302 symtab_cmd->offset = cmd->offset + cmd->len;
1303 symtab_cmd->len = 6 * 4;
1304 symtab_cmd->type_required = FALSE;
1306 mdata->header.sizeofcmds += symtab_cmd->len;
1307 mdata->filelen += symtab_cmd->len;
1309 /* Fill segment command. */
1310 memset (seg->segname, 0, sizeof (seg->segname));
1311 seg->vmaddr = 0;
1312 seg->fileoff = mdata->filelen;
1313 seg->filesize = 0;
1314 seg->maxprot = BFD_MACH_O_PROT_READ | BFD_MACH_O_PROT_WRITE
1315 | BFD_MACH_O_PROT_EXECUTE;
1316 seg->initprot = seg->maxprot;
1317 seg->flags = 0;
1319 /* Create Mach-O sections. */
1320 target_index = 0;
1321 for (sec = abfd->sections; sec; sec = sec->next)
1323 sections->bfdsection = sec;
1324 bfd_mach_o_convert_section_name_to_mach_o (abfd, sec, sections);
1325 sections->addr = bfd_get_section_vma (abfd, sec);
1326 sections->size = bfd_get_section_size (sec);
1327 sections->align = bfd_get_section_alignment (abfd, sec);
1329 if (sections->size != 0)
1331 mdata->filelen = FILE_ALIGN (mdata->filelen, sections->align);
1332 sections->offset = mdata->filelen;
1334 else
1335 sections->offset = 0;
1336 sections->reloff = 0;
1337 sections->nreloc = 0;
1338 sections->reserved1 = 0;
1339 sections->reserved2 = 0;
1340 sections->reserved3 = 0;
1342 sec->filepos = sections->offset;
1343 sec->target_index = ++target_index;
1345 mdata->filelen += sections->size;
1346 sections++;
1348 seg->filesize = mdata->filelen - seg->fileoff;
1349 seg->vmsize = seg->filesize;
1351 return TRUE;
1354 /* Set the contents of a section. */
1356 bfd_boolean
1357 bfd_mach_o_set_section_contents (bfd *abfd,
1358 asection *section,
1359 const void * location,
1360 file_ptr offset,
1361 bfd_size_type count)
1363 file_ptr pos;
1365 /* This must be done first, because bfd_set_section_contents is
1366 going to set output_has_begun to TRUE. */
1367 if (! abfd->output_has_begun && ! bfd_mach_o_build_commands (abfd))
1368 return FALSE;
1370 if (count == 0)
1371 return TRUE;
1373 pos = section->filepos + offset;
1374 if (bfd_seek (abfd, pos, SEEK_SET) != 0
1375 || bfd_bwrite (location, count, abfd) != count)
1376 return FALSE;
1378 return TRUE;
1382 bfd_mach_o_sizeof_headers (bfd *a ATTRIBUTE_UNUSED,
1383 struct bfd_link_info *info ATTRIBUTE_UNUSED)
1385 return 0;
1388 /* Make an empty symbol. This is required only because
1389 bfd_make_section_anyway wants to create a symbol for the section. */
1391 asymbol *
1392 bfd_mach_o_make_empty_symbol (bfd *abfd)
1394 asymbol *new_symbol;
1396 new_symbol = bfd_zalloc (abfd, sizeof (bfd_mach_o_asymbol));
1397 if (new_symbol == NULL)
1398 return new_symbol;
1399 new_symbol->the_bfd = abfd;
1400 new_symbol->udata.i = 0;
1401 return new_symbol;
1404 static bfd_boolean
1405 bfd_mach_o_read_header (bfd *abfd, bfd_mach_o_header *header)
1407 unsigned char buf[32];
1408 unsigned int size;
1409 bfd_vma (*get32) (const void *) = NULL;
1411 /* Just read the magic number. */
1412 if (bfd_seek (abfd, 0, SEEK_SET) != 0
1413 || bfd_bread ((PTR) buf, 4, abfd) != 4)
1414 return FALSE;
1416 if (bfd_getb32 (buf) == BFD_MACH_O_MH_MAGIC)
1418 header->byteorder = BFD_ENDIAN_BIG;
1419 header->magic = BFD_MACH_O_MH_MAGIC;
1420 header->version = 1;
1421 get32 = bfd_getb32;
1423 else if (bfd_getl32 (buf) == BFD_MACH_O_MH_MAGIC)
1425 header->byteorder = BFD_ENDIAN_LITTLE;
1426 header->magic = BFD_MACH_O_MH_MAGIC;
1427 header->version = 1;
1428 get32 = bfd_getl32;
1430 else if (bfd_getb32 (buf) == BFD_MACH_O_MH_MAGIC_64)
1432 header->byteorder = BFD_ENDIAN_BIG;
1433 header->magic = BFD_MACH_O_MH_MAGIC_64;
1434 header->version = 2;
1435 get32 = bfd_getb32;
1437 else if (bfd_getl32 (buf) == BFD_MACH_O_MH_MAGIC_64)
1439 header->byteorder = BFD_ENDIAN_LITTLE;
1440 header->magic = BFD_MACH_O_MH_MAGIC_64;
1441 header->version = 2;
1442 get32 = bfd_getl32;
1444 else
1446 header->byteorder = BFD_ENDIAN_UNKNOWN;
1447 return FALSE;
1450 /* Once the size of the header is known, read the full header. */
1451 size = mach_o_wide_p (header) ?
1452 BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE;
1454 if (bfd_seek (abfd, 0, SEEK_SET) != 0
1455 || bfd_bread ((PTR) buf, size, abfd) != size)
1456 return FALSE;
1458 header->cputype = (*get32) (buf + 4);
1459 header->cpusubtype = (*get32) (buf + 8);
1460 header->filetype = (*get32) (buf + 12);
1461 header->ncmds = (*get32) (buf + 16);
1462 header->sizeofcmds = (*get32) (buf + 20);
1463 header->flags = (*get32) (buf + 24);
1465 if (mach_o_wide_p (header))
1466 header->reserved = (*get32) (buf + 28);
1468 return TRUE;
1471 static asection *
1472 bfd_mach_o_make_bfd_section (bfd *abfd, bfd_mach_o_section *section,
1473 unsigned long prot)
1475 asection *bfdsec;
1476 char *sname;
1477 flagword flags;
1479 sname = bfd_mach_o_convert_section_name_to_bfd (abfd, section);
1480 if (sname == NULL)
1481 return NULL;
1483 if (section->flags & BFD_MACH_O_S_ATTR_DEBUG)
1484 flags = SEC_HAS_CONTENTS | SEC_DEBUGGING;
1485 else
1487 flags = SEC_ALLOC;
1488 if ((section->flags & BFD_MACH_O_SECTION_TYPE_MASK)
1489 != BFD_MACH_O_S_ZEROFILL)
1491 flags |= SEC_HAS_CONTENTS | SEC_LOAD;
1492 if (prot & BFD_MACH_O_PROT_EXECUTE)
1493 flags |= SEC_CODE;
1494 if (prot & BFD_MACH_O_PROT_WRITE)
1495 flags |= SEC_DATA;
1496 else if (prot & BFD_MACH_O_PROT_READ)
1497 flags |= SEC_READONLY;
1500 if (section->nreloc != 0)
1501 flags |= SEC_RELOC;
1503 bfdsec = bfd_make_section_anyway_with_flags (abfd, sname, flags);
1504 if (bfdsec == NULL)
1505 return NULL;
1507 bfdsec->vma = section->addr;
1508 bfdsec->lma = section->addr;
1509 bfdsec->size = section->size;
1510 bfdsec->filepos = section->offset;
1511 bfdsec->alignment_power = section->align;
1512 bfdsec->segment_mark = 0;
1513 bfdsec->reloc_count = section->nreloc;
1514 bfdsec->rel_filepos = section->reloff;
1516 return bfdsec;
1519 static int
1520 bfd_mach_o_read_section_32 (bfd *abfd,
1521 bfd_mach_o_section *section,
1522 unsigned int offset,
1523 unsigned long prot)
1525 unsigned char buf[BFD_MACH_O_SECTION_SIZE];
1527 if (bfd_seek (abfd, offset, SEEK_SET) != 0
1528 || (bfd_bread ((PTR) buf, BFD_MACH_O_SECTION_SIZE, abfd)
1529 != BFD_MACH_O_SECTION_SIZE))
1530 return -1;
1532 memcpy (section->sectname, buf, 16);
1533 section->sectname[16] = '\0';
1534 memcpy (section->segname, buf + 16, 16);
1535 section->segname[16] = '\0';
1536 section->addr = bfd_h_get_32 (abfd, buf + 32);
1537 section->size = bfd_h_get_32 (abfd, buf + 36);
1538 section->offset = bfd_h_get_32 (abfd, buf + 40);
1539 section->align = bfd_h_get_32 (abfd, buf + 44);
1540 section->reloff = bfd_h_get_32 (abfd, buf + 48);
1541 section->nreloc = bfd_h_get_32 (abfd, buf + 52);
1542 section->flags = bfd_h_get_32 (abfd, buf + 56);
1543 section->reserved1 = bfd_h_get_32 (abfd, buf + 60);
1544 section->reserved2 = bfd_h_get_32 (abfd, buf + 64);
1545 section->reserved3 = 0;
1546 section->bfdsection = bfd_mach_o_make_bfd_section (abfd, section, prot);
1548 if (section->bfdsection == NULL)
1549 return -1;
1551 return 0;
1554 static int
1555 bfd_mach_o_read_section_64 (bfd *abfd,
1556 bfd_mach_o_section *section,
1557 unsigned int offset,
1558 unsigned long prot)
1560 unsigned char buf[BFD_MACH_O_SECTION_64_SIZE];
1562 if (bfd_seek (abfd, offset, SEEK_SET) != 0
1563 || (bfd_bread ((PTR) buf, BFD_MACH_O_SECTION_64_SIZE, abfd)
1564 != BFD_MACH_O_SECTION_64_SIZE))
1565 return -1;
1567 memcpy (section->sectname, buf, 16);
1568 section->sectname[16] = '\0';
1569 memcpy (section->segname, buf + 16, 16);
1570 section->segname[16] = '\0';
1571 section->addr = bfd_h_get_64 (abfd, buf + 32);
1572 section->size = bfd_h_get_64 (abfd, buf + 40);
1573 section->offset = bfd_h_get_32 (abfd, buf + 48);
1574 section->align = bfd_h_get_32 (abfd, buf + 52);
1575 section->reloff = bfd_h_get_32 (abfd, buf + 56);
1576 section->nreloc = bfd_h_get_32 (abfd, buf + 60);
1577 section->flags = bfd_h_get_32 (abfd, buf + 64);
1578 section->reserved1 = bfd_h_get_32 (abfd, buf + 68);
1579 section->reserved2 = bfd_h_get_32 (abfd, buf + 72);
1580 section->reserved3 = bfd_h_get_32 (abfd, buf + 76);
1581 section->bfdsection = bfd_mach_o_make_bfd_section (abfd, section, prot);
1583 if (section->bfdsection == NULL)
1584 return -1;
1586 return 0;
1589 static int
1590 bfd_mach_o_read_section (bfd *abfd,
1591 bfd_mach_o_section *section,
1592 unsigned int offset,
1593 unsigned long prot,
1594 unsigned int wide)
1596 if (wide)
1597 return bfd_mach_o_read_section_64 (abfd, section, offset, prot);
1598 else
1599 return bfd_mach_o_read_section_32 (abfd, section, offset, prot);
1602 static int
1603 bfd_mach_o_read_symtab_symbol (bfd *abfd,
1604 bfd_mach_o_symtab_command *sym,
1605 bfd_mach_o_asymbol *s,
1606 unsigned long i)
1608 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
1609 unsigned int wide = mach_o_wide_p (&mdata->header);
1610 unsigned int symwidth =
1611 wide ? BFD_MACH_O_NLIST_64_SIZE : BFD_MACH_O_NLIST_SIZE;
1612 unsigned int symoff = sym->symoff + (i * symwidth);
1613 unsigned char buf[16];
1614 unsigned char type = -1;
1615 unsigned char section = -1;
1616 short desc = -1;
1617 symvalue value = -1;
1618 unsigned long stroff = -1;
1619 unsigned int symtype = -1;
1621 BFD_ASSERT (sym->strtab != NULL);
1623 if (bfd_seek (abfd, symoff, SEEK_SET) != 0
1624 || bfd_bread ((PTR) buf, symwidth, abfd) != symwidth)
1626 fprintf (stderr, "bfd_mach_o_read_symtab_symbol: unable to read %d bytes at %lu\n",
1627 symwidth, (unsigned long) symoff);
1628 return -1;
1631 stroff = bfd_h_get_32 (abfd, buf);
1632 type = bfd_h_get_8 (abfd, buf + 4);
1633 symtype = type & BFD_MACH_O_N_TYPE;
1634 section = bfd_h_get_8 (abfd, buf + 5);
1635 desc = bfd_h_get_16 (abfd, buf + 6);
1636 if (wide)
1637 value = bfd_h_get_64 (abfd, buf + 8);
1638 else
1639 value = bfd_h_get_32 (abfd, buf + 8);
1641 if (stroff >= sym->strsize)
1643 fprintf (stderr, "bfd_mach_o_read_symtab_symbol: symbol name out of range (%lu >= %lu)\n",
1644 (unsigned long) stroff, (unsigned long) sym->strsize);
1645 return -1;
1648 s->symbol.the_bfd = abfd;
1649 s->symbol.name = sym->strtab + stroff;
1650 s->symbol.value = value;
1651 s->symbol.flags = 0x0;
1652 s->symbol.udata.i = 0;
1653 s->n_type = type;
1654 s->n_sect = section;
1655 s->n_desc = desc;
1657 if (type & BFD_MACH_O_N_STAB)
1659 s->symbol.flags |= BSF_DEBUGGING;
1660 s->symbol.section = bfd_und_section_ptr;
1661 switch (type)
1663 case N_FUN:
1664 case N_STSYM:
1665 case N_LCSYM:
1666 case N_BNSYM:
1667 case N_SLINE:
1668 case N_ENSYM:
1669 case N_ECOMM:
1670 case N_ECOML:
1671 case N_GSYM:
1672 if ((section > 0) && (section <= mdata->nsects))
1674 s->symbol.section = mdata->sections[section - 1]->bfdsection;
1675 s->symbol.value =
1676 s->symbol.value - mdata->sections[section - 1]->addr;
1678 break;
1681 else
1683 if (type & BFD_MACH_O_N_PEXT)
1684 s->symbol.flags |= BSF_GLOBAL;
1686 if (type & BFD_MACH_O_N_EXT)
1687 s->symbol.flags |= BSF_GLOBAL;
1689 if (!(type & (BFD_MACH_O_N_PEXT | BFD_MACH_O_N_EXT)))
1690 s->symbol.flags |= BSF_LOCAL;
1692 switch (symtype)
1694 case BFD_MACH_O_N_UNDF:
1695 if (type == (BFD_MACH_O_N_UNDF | BFD_MACH_O_N_EXT)
1696 && s->symbol.value != 0)
1698 /* A common symbol. */
1699 s->symbol.section = bfd_com_section_ptr;
1700 s->symbol.flags = BSF_NO_FLAGS;
1702 else
1704 s->symbol.section = bfd_und_section_ptr;
1705 if (s->n_desc & BFD_MACH_O_N_WEAK_REF)
1706 s->symbol.flags |= BSF_WEAK;
1708 break;
1709 case BFD_MACH_O_N_PBUD:
1710 s->symbol.section = bfd_und_section_ptr;
1711 break;
1712 case BFD_MACH_O_N_ABS:
1713 s->symbol.section = bfd_abs_section_ptr;
1714 break;
1715 case BFD_MACH_O_N_SECT:
1716 if ((section > 0) && (section <= mdata->nsects))
1718 s->symbol.section = mdata->sections[section - 1]->bfdsection;
1719 s->symbol.value =
1720 s->symbol.value - mdata->sections[section - 1]->addr;
1722 else
1724 /* Mach-O uses 0 to mean "no section"; not an error. */
1725 if (section != 0)
1727 fprintf (stderr, "bfd_mach_o_read_symtab_symbol: "
1728 "symbol \"%s\" specified invalid section %d (max %lu): setting to undefined\n",
1729 s->symbol.name, section, mdata->nsects);
1731 s->symbol.section = bfd_und_section_ptr;
1733 break;
1734 case BFD_MACH_O_N_INDR:
1735 fprintf (stderr, "bfd_mach_o_read_symtab_symbol: "
1736 "symbol \"%s\" is unsupported 'indirect' reference: setting to undefined\n",
1737 s->symbol.name);
1738 s->symbol.section = bfd_und_section_ptr;
1739 break;
1740 default:
1741 fprintf (stderr, "bfd_mach_o_read_symtab_symbol: "
1742 "symbol \"%s\" specified invalid type field 0x%x: setting to undefined\n",
1743 s->symbol.name, symtype);
1744 s->symbol.section = bfd_und_section_ptr;
1745 break;
1749 return 0;
1752 static int
1753 bfd_mach_o_read_symtab_strtab (bfd *abfd)
1755 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
1756 bfd_mach_o_symtab_command *sym = mdata->symtab;
1758 /* Fail if there is no symtab. */
1759 if (sym == NULL)
1760 return -1;
1762 /* Success if already loaded. */
1763 if (sym->strtab)
1764 return 0;
1766 if (abfd->flags & BFD_IN_MEMORY)
1768 struct bfd_in_memory *b;
1770 b = (struct bfd_in_memory *) abfd->iostream;
1772 if ((sym->stroff + sym->strsize) > b->size)
1774 bfd_set_error (bfd_error_file_truncated);
1775 return -1;
1777 sym->strtab = (char *) b->buffer + sym->stroff;
1779 else
1781 sym->strtab = bfd_alloc (abfd, sym->strsize);
1782 if (sym->strtab == NULL)
1783 return -1;
1785 if (bfd_seek (abfd, sym->stroff, SEEK_SET) != 0
1786 || bfd_bread ((PTR) sym->strtab, sym->strsize, abfd) != sym->strsize)
1788 bfd_set_error (bfd_error_file_truncated);
1789 return -1;
1793 return 0;
1796 static int
1797 bfd_mach_o_read_symtab_symbols (bfd *abfd)
1799 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
1800 bfd_mach_o_symtab_command *sym = mdata->symtab;
1801 unsigned long i;
1802 int ret;
1804 if (sym->symbols)
1805 return 0;
1807 sym->symbols = bfd_alloc (abfd, sym->nsyms * sizeof (bfd_mach_o_asymbol));
1809 if (sym->symbols == NULL)
1811 fprintf (stderr, "bfd_mach_o_read_symtab_symbols: unable to allocate memory for symbols\n");
1812 return -1;
1815 ret = bfd_mach_o_read_symtab_strtab (abfd);
1816 if (ret != 0)
1817 return ret;
1819 for (i = 0; i < sym->nsyms; i++)
1821 ret = bfd_mach_o_read_symtab_symbol (abfd, sym, &sym->symbols[i], i);
1822 if (ret != 0)
1823 return ret;
1826 return 0;
1830 bfd_mach_o_read_dysymtab_symbol (bfd *abfd,
1831 bfd_mach_o_dysymtab_command *dysym,
1832 bfd_mach_o_symtab_command *sym,
1833 bfd_mach_o_asymbol *s,
1834 unsigned long i)
1836 unsigned long isymoff = dysym->indirectsymoff + (i * 4);
1837 unsigned long symindex;
1838 unsigned char buf[4];
1840 BFD_ASSERT (i < dysym->nindirectsyms);
1842 if (bfd_seek (abfd, isymoff, SEEK_SET) != 0
1843 || bfd_bread ((PTR) buf, 4, abfd) != 4)
1845 fprintf (stderr, "bfd_mach_o_read_dysymtab_symbol: unable to read %lu bytes at %lu\n",
1846 (unsigned long) 4, isymoff);
1847 return -1;
1849 symindex = bfd_h_get_32 (abfd, buf);
1851 return bfd_mach_o_read_symtab_symbol (abfd, sym, s, symindex);
1854 static const char *
1855 bfd_mach_o_i386_flavour_string (unsigned int flavour)
1857 switch ((int) flavour)
1859 case BFD_MACH_O_x86_THREAD_STATE32: return "x86_THREAD_STATE32";
1860 case BFD_MACH_O_x86_FLOAT_STATE32: return "x86_FLOAT_STATE32";
1861 case BFD_MACH_O_x86_EXCEPTION_STATE32: return "x86_EXCEPTION_STATE32";
1862 case BFD_MACH_O_x86_THREAD_STATE64: return "x86_THREAD_STATE64";
1863 case BFD_MACH_O_x86_FLOAT_STATE64: return "x86_FLOAT_STATE64";
1864 case BFD_MACH_O_x86_EXCEPTION_STATE64: return "x86_EXCEPTION_STATE64";
1865 case BFD_MACH_O_x86_THREAD_STATE: return "x86_THREAD_STATE";
1866 case BFD_MACH_O_x86_FLOAT_STATE: return "x86_FLOAT_STATE";
1867 case BFD_MACH_O_x86_EXCEPTION_STATE: return "x86_EXCEPTION_STATE";
1868 case BFD_MACH_O_x86_DEBUG_STATE32: return "x86_DEBUG_STATE32";
1869 case BFD_MACH_O_x86_DEBUG_STATE64: return "x86_DEBUG_STATE64";
1870 case BFD_MACH_O_x86_DEBUG_STATE: return "x86_DEBUG_STATE";
1871 case BFD_MACH_O_x86_THREAD_STATE_NONE: return "x86_THREAD_STATE_NONE";
1872 default: return "UNKNOWN";
1876 static const char *
1877 bfd_mach_o_ppc_flavour_string (unsigned int flavour)
1879 switch ((int) flavour)
1881 case BFD_MACH_O_PPC_THREAD_STATE: return "PPC_THREAD_STATE";
1882 case BFD_MACH_O_PPC_FLOAT_STATE: return "PPC_FLOAT_STATE";
1883 case BFD_MACH_O_PPC_EXCEPTION_STATE: return "PPC_EXCEPTION_STATE";
1884 case BFD_MACH_O_PPC_VECTOR_STATE: return "PPC_VECTOR_STATE";
1885 case BFD_MACH_O_PPC_THREAD_STATE64: return "PPC_THREAD_STATE64";
1886 case BFD_MACH_O_PPC_EXCEPTION_STATE64: return "PPC_EXCEPTION_STATE64";
1887 default: return "UNKNOWN";
1891 static int
1892 bfd_mach_o_read_dylinker (bfd *abfd, bfd_mach_o_load_command *command)
1894 bfd_mach_o_dylinker_command *cmd = &command->command.dylinker;
1895 unsigned char buf[4];
1896 unsigned int nameoff;
1898 BFD_ASSERT ((command->type == BFD_MACH_O_LC_ID_DYLINKER)
1899 || (command->type == BFD_MACH_O_LC_LOAD_DYLINKER));
1901 if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
1902 || bfd_bread ((PTR) buf, 4, abfd) != 4)
1903 return -1;
1905 nameoff = bfd_h_get_32 (abfd, buf + 0);
1907 cmd->name_offset = command->offset + nameoff;
1908 cmd->name_len = command->len - nameoff;
1909 cmd->name_str = bfd_alloc (abfd, cmd->name_len);
1910 if (cmd->name_str == NULL)
1911 return -1;
1912 if (bfd_seek (abfd, cmd->name_offset, SEEK_SET) != 0
1913 || bfd_bread (cmd->name_str, cmd->name_len, abfd) != cmd->name_len)
1914 return -1;
1915 return 0;
1918 static int
1919 bfd_mach_o_read_dylib (bfd *abfd, bfd_mach_o_load_command *command)
1921 bfd_mach_o_dylib_command *cmd = &command->command.dylib;
1922 unsigned char buf[16];
1923 unsigned int nameoff;
1925 switch (command->type)
1927 case BFD_MACH_O_LC_LOAD_DYLIB:
1928 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB:
1929 case BFD_MACH_O_LC_ID_DYLIB:
1930 case BFD_MACH_O_LC_REEXPORT_DYLIB:
1931 break;
1932 default:
1933 BFD_FAIL ();
1934 return -1;
1937 if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
1938 || bfd_bread ((PTR) buf, 16, abfd) != 16)
1939 return -1;
1941 nameoff = bfd_h_get_32 (abfd, buf + 0);
1942 cmd->timestamp = bfd_h_get_32 (abfd, buf + 4);
1943 cmd->current_version = bfd_h_get_32 (abfd, buf + 8);
1944 cmd->compatibility_version = bfd_h_get_32 (abfd, buf + 12);
1946 cmd->name_offset = command->offset + nameoff;
1947 cmd->name_len = command->len - nameoff;
1948 cmd->name_str = bfd_alloc (abfd, cmd->name_len);
1949 if (cmd->name_str == NULL)
1950 return -1;
1951 if (bfd_seek (abfd, cmd->name_offset, SEEK_SET) != 0
1952 || bfd_bread (cmd->name_str, cmd->name_len, abfd) != cmd->name_len)
1953 return -1;
1954 return 0;
1957 static int
1958 bfd_mach_o_read_prebound_dylib (bfd *abfd ATTRIBUTE_UNUSED,
1959 bfd_mach_o_load_command *command ATTRIBUTE_UNUSED)
1961 /* bfd_mach_o_prebound_dylib_command *cmd = &command->command.prebound_dylib; */
1963 BFD_ASSERT (command->type == BFD_MACH_O_LC_PREBOUND_DYLIB);
1964 return 0;
1967 static int
1968 bfd_mach_o_read_thread (bfd *abfd, bfd_mach_o_load_command *command)
1970 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
1971 bfd_mach_o_thread_command *cmd = &command->command.thread;
1972 unsigned char buf[8];
1973 unsigned int offset;
1974 unsigned int nflavours;
1975 unsigned int i;
1977 BFD_ASSERT ((command->type == BFD_MACH_O_LC_THREAD)
1978 || (command->type == BFD_MACH_O_LC_UNIXTHREAD));
1980 /* Count the number of threads. */
1981 offset = 8;
1982 nflavours = 0;
1983 while (offset != command->len)
1985 if (offset >= command->len)
1986 return -1;
1988 if (bfd_seek (abfd, command->offset + offset, SEEK_SET) != 0
1989 || bfd_bread ((PTR) buf, 8, abfd) != 8)
1990 return -1;
1992 offset += 8 + bfd_h_get_32 (abfd, buf + 4) * 4;
1993 nflavours++;
1996 /* Allocate threads. */
1997 cmd->flavours = bfd_alloc
1998 (abfd, nflavours * sizeof (bfd_mach_o_thread_flavour));
1999 if (cmd->flavours == NULL)
2000 return -1;
2001 cmd->nflavours = nflavours;
2003 offset = 8;
2004 nflavours = 0;
2005 while (offset != command->len)
2007 if (offset >= command->len)
2008 return -1;
2010 if (nflavours >= cmd->nflavours)
2011 return -1;
2013 if (bfd_seek (abfd, command->offset + offset, SEEK_SET) != 0
2014 || bfd_bread ((PTR) buf, 8, abfd) != 8)
2015 return -1;
2017 cmd->flavours[nflavours].flavour = bfd_h_get_32 (abfd, buf);
2018 cmd->flavours[nflavours].offset = command->offset + offset + 8;
2019 cmd->flavours[nflavours].size = bfd_h_get_32 (abfd, buf + 4) * 4;
2020 offset += cmd->flavours[nflavours].size + 8;
2021 nflavours++;
2024 for (i = 0; i < nflavours; i++)
2026 asection *bfdsec;
2027 unsigned int snamelen;
2028 char *sname;
2029 const char *flavourstr;
2030 const char *prefix = "LC_THREAD";
2031 unsigned int j = 0;
2033 switch (mdata->header.cputype)
2035 case BFD_MACH_O_CPU_TYPE_POWERPC:
2036 case BFD_MACH_O_CPU_TYPE_POWERPC_64:
2037 flavourstr = bfd_mach_o_ppc_flavour_string (cmd->flavours[i].flavour);
2038 break;
2039 case BFD_MACH_O_CPU_TYPE_I386:
2040 case BFD_MACH_O_CPU_TYPE_X86_64:
2041 flavourstr = bfd_mach_o_i386_flavour_string (cmd->flavours[i].flavour);
2042 break;
2043 default:
2044 flavourstr = "UNKNOWN_ARCHITECTURE";
2045 break;
2048 snamelen = strlen (prefix) + 1 + 20 + 1 + strlen (flavourstr) + 1;
2049 sname = bfd_alloc (abfd, snamelen);
2050 if (sname == NULL)
2051 return -1;
2053 for (;;)
2055 sprintf (sname, "%s.%s.%u", prefix, flavourstr, j);
2056 if (bfd_get_section_by_name (abfd, sname) == NULL)
2057 break;
2058 j++;
2061 bfdsec = bfd_make_section_with_flags (abfd, sname, SEC_HAS_CONTENTS);
2063 bfdsec->vma = 0;
2064 bfdsec->lma = 0;
2065 bfdsec->size = cmd->flavours[i].size;
2066 bfdsec->filepos = cmd->flavours[i].offset;
2067 bfdsec->alignment_power = 0x0;
2069 cmd->section = bfdsec;
2072 return 0;
2075 static int
2076 bfd_mach_o_read_dysymtab (bfd *abfd, bfd_mach_o_load_command *command)
2078 bfd_mach_o_dysymtab_command *cmd = &command->command.dysymtab;
2079 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
2080 unsigned char buf[72];
2082 BFD_ASSERT (command->type == BFD_MACH_O_LC_DYSYMTAB);
2084 if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
2085 || bfd_bread ((PTR) buf, 72, abfd) != 72)
2086 return -1;
2088 cmd->ilocalsym = bfd_h_get_32 (abfd, buf + 0);
2089 cmd->nlocalsym = bfd_h_get_32 (abfd, buf + 4);
2090 cmd->iextdefsym = bfd_h_get_32 (abfd, buf + 8);
2091 cmd->nextdefsym = bfd_h_get_32 (abfd, buf + 12);
2092 cmd->iundefsym = bfd_h_get_32 (abfd, buf + 16);
2093 cmd->nundefsym = bfd_h_get_32 (abfd, buf + 20);
2094 cmd->tocoff = bfd_h_get_32 (abfd, buf + 24);
2095 cmd->ntoc = bfd_h_get_32 (abfd, buf + 28);
2096 cmd->modtaboff = bfd_h_get_32 (abfd, buf + 32);
2097 cmd->nmodtab = bfd_h_get_32 (abfd, buf + 36);
2098 cmd->extrefsymoff = bfd_h_get_32 (abfd, buf + 40);
2099 cmd->nextrefsyms = bfd_h_get_32 (abfd, buf + 44);
2100 cmd->indirectsymoff = bfd_h_get_32 (abfd, buf + 48);
2101 cmd->nindirectsyms = bfd_h_get_32 (abfd, buf + 52);
2102 cmd->extreloff = bfd_h_get_32 (abfd, buf + 56);
2103 cmd->nextrel = bfd_h_get_32 (abfd, buf + 60);
2104 cmd->locreloff = bfd_h_get_32 (abfd, buf + 64);
2105 cmd->nlocrel = bfd_h_get_32 (abfd, buf + 68);
2107 if (cmd->nmodtab != 0)
2109 char buf[56];
2110 unsigned int i;
2111 int wide = bfd_mach_o_wide_p (abfd);
2112 unsigned int module_len = wide ? 56 : 52;
2114 cmd->dylib_module =
2115 bfd_alloc (abfd, cmd->nmodtab * sizeof (bfd_mach_o_dylib_module));
2116 if (cmd->dylib_module == NULL)
2117 return -1;
2119 if (bfd_seek (abfd, cmd->modtaboff, SEEK_SET) != 0)
2120 return -1;
2122 for (i = 0; i < cmd->nmodtab; i++)
2124 bfd_mach_o_dylib_module *module = &cmd->dylib_module[i];
2125 unsigned long v;
2127 if (bfd_bread ((PTR) buf, module_len, abfd) != module_len)
2128 return -1;
2130 module->module_name_idx = bfd_h_get_32 (abfd, buf + 0);
2131 module->iextdefsym = bfd_h_get_32 (abfd, buf + 4);
2132 module->nextdefsym = bfd_h_get_32 (abfd, buf + 8);
2133 module->irefsym = bfd_h_get_32 (abfd, buf + 12);
2134 module->nrefsym = bfd_h_get_32 (abfd, buf + 16);
2135 module->ilocalsym = bfd_h_get_32 (abfd, buf + 20);
2136 module->nlocalsym = bfd_h_get_32 (abfd, buf + 24);
2137 module->iextrel = bfd_h_get_32 (abfd, buf + 28);
2138 module->nextrel = bfd_h_get_32 (abfd, buf + 32);
2139 v = bfd_h_get_32 (abfd, buf +36);
2140 module->iinit = v & 0xffff;
2141 module->iterm = (v >> 16) & 0xffff;
2142 v = bfd_h_get_32 (abfd, buf + 40);
2143 module->ninit = v & 0xffff;
2144 module->nterm = (v >> 16) & 0xffff;
2145 if (wide)
2147 module->objc_module_info_size = bfd_h_get_32 (abfd, buf + 44);
2148 module->objc_module_info_addr = bfd_h_get_64 (abfd, buf + 48);
2150 else
2152 module->objc_module_info_addr = bfd_h_get_32 (abfd, buf + 44);
2153 module->objc_module_info_size = bfd_h_get_32 (abfd, buf + 48);
2158 if (cmd->ntoc != 0)
2160 char buf[8];
2161 unsigned int i;
2163 cmd->dylib_toc = bfd_alloc
2164 (abfd, cmd->ntoc * sizeof (bfd_mach_o_dylib_table_of_content));
2165 if (cmd->dylib_toc == NULL)
2166 return -1;
2168 if (bfd_seek (abfd, cmd->tocoff, SEEK_SET) != 0)
2169 return -1;
2171 for (i = 0; i < cmd->ntoc; i++)
2173 bfd_mach_o_dylib_table_of_content *toc = &cmd->dylib_toc[i];
2175 if (bfd_bread ((PTR) buf, 8, abfd) != 8)
2176 return -1;
2178 toc->symbol_index = bfd_h_get_32 (abfd, buf + 0);
2179 toc->module_index = bfd_h_get_32 (abfd, buf + 4);
2183 if (cmd->nindirectsyms != 0)
2185 char buf[4];
2186 unsigned int i;
2188 cmd->indirect_syms = bfd_alloc
2189 (abfd, cmd->nindirectsyms * sizeof (unsigned int));
2190 if (cmd->indirect_syms == NULL)
2191 return -1;
2193 if (bfd_seek (abfd, cmd->indirectsymoff, SEEK_SET) != 0)
2194 return -1;
2196 for (i = 0; i < cmd->nindirectsyms; i++)
2198 unsigned int *is = &cmd->indirect_syms[i];
2200 if (bfd_bread ((PTR) buf, 4, abfd) != 4)
2201 return -1;
2203 *is = bfd_h_get_32 (abfd, buf + 0);
2207 if (cmd->nextrefsyms != 0)
2209 char buf[4];
2210 unsigned long v;
2211 unsigned int i;
2213 cmd->ext_refs = bfd_alloc
2214 (abfd, cmd->nextrefsyms * sizeof (bfd_mach_o_dylib_reference));
2215 if (cmd->ext_refs == NULL)
2216 return -1;
2218 if (bfd_seek (abfd, cmd->extrefsymoff, SEEK_SET) != 0)
2219 return -1;
2221 for (i = 0; i < cmd->nextrefsyms; i++)
2223 bfd_mach_o_dylib_reference *ref = &cmd->ext_refs[i];
2225 if (bfd_bread ((PTR) buf, 4, abfd) != 4)
2226 return -1;
2228 /* Fields isym and flags are written as bit-fields, thus we need
2229 a specific processing for endianness. */
2230 v = bfd_h_get_32 (abfd, buf + 0);
2231 if (bfd_big_endian (abfd))
2233 ref->isym = (v >> 8) & 0xffffff;
2234 ref->flags = v & 0xff;
2236 else
2238 ref->isym = v & 0xffffff;
2239 ref->flags = (v >> 24) & 0xff;
2244 if (mdata->dysymtab)
2245 return -1;
2246 mdata->dysymtab = cmd;
2248 return 0;
2251 static int
2252 bfd_mach_o_read_symtab (bfd *abfd, bfd_mach_o_load_command *command)
2254 bfd_mach_o_symtab_command *symtab = &command->command.symtab;
2255 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
2256 unsigned char buf[16];
2258 BFD_ASSERT (command->type == BFD_MACH_O_LC_SYMTAB);
2260 if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
2261 || bfd_bread ((PTR) buf, 16, abfd) != 16)
2262 return -1;
2264 symtab->symoff = bfd_h_get_32 (abfd, buf);
2265 symtab->nsyms = bfd_h_get_32 (abfd, buf + 4);
2266 symtab->stroff = bfd_h_get_32 (abfd, buf + 8);
2267 symtab->strsize = bfd_h_get_32 (abfd, buf + 12);
2268 symtab->symbols = NULL;
2269 symtab->strtab = NULL;
2271 if (symtab->nsyms != 0)
2272 abfd->flags |= HAS_SYMS;
2274 if (mdata->symtab)
2275 return -1;
2276 mdata->symtab = symtab;
2277 return 0;
2280 static int
2281 bfd_mach_o_read_uuid (bfd *abfd, bfd_mach_o_load_command *command)
2283 bfd_mach_o_uuid_command *cmd = &command->command.uuid;
2284 asection *bfdsec;
2285 char *sname;
2286 static const char prefix[] = "LC_UUID";
2288 BFD_ASSERT (command->type == BFD_MACH_O_LC_UUID);
2290 if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
2291 || bfd_bread ((PTR) cmd->uuid, 16, abfd) != 16)
2292 return -1;
2294 sname = bfd_alloc (abfd, strlen (prefix) + 1);
2295 if (sname == NULL)
2296 return -1;
2297 strcpy (sname, prefix);
2299 bfdsec = bfd_make_section_anyway_with_flags (abfd, sname, SEC_HAS_CONTENTS);
2300 if (bfdsec == NULL)
2301 return -1;
2303 bfdsec->vma = 0;
2304 bfdsec->lma = 0;
2305 bfdsec->size = command->len - 8;
2306 bfdsec->filepos = command->offset + 8;
2307 bfdsec->alignment_power = 0;
2309 cmd->section = bfdsec;
2311 return 0;
2314 static int
2315 bfd_mach_o_read_linkedit (bfd *abfd, bfd_mach_o_load_command *command)
2317 bfd_mach_o_linkedit_command *cmd = &command->command.linkedit;
2318 char buf[8];
2320 if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
2321 || bfd_bread ((PTR) buf, 8, abfd) != 8)
2322 return -1;
2324 cmd->dataoff = bfd_get_32 (abfd, buf + 0);
2325 cmd->datasize = bfd_get_32 (abfd, buf + 4);
2326 return 0;
2329 static int
2330 bfd_mach_o_read_str (bfd *abfd, bfd_mach_o_load_command *command)
2332 bfd_mach_o_str_command *cmd = &command->command.str;
2333 char buf[4];
2334 unsigned long off;
2336 if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
2337 || bfd_bread ((PTR) buf, 4, abfd) != 4)
2338 return -1;
2340 off = bfd_get_32 (abfd, buf + 0);
2341 cmd->stroff = command->offset + off;
2342 cmd->str_len = command->len - off;
2343 cmd->str = bfd_alloc (abfd, cmd->str_len);
2344 if (cmd->str == NULL)
2345 return -1;
2346 if (bfd_seek (abfd, cmd->stroff, SEEK_SET) != 0
2347 || bfd_bread ((PTR) cmd->str, cmd->str_len, abfd) != cmd->str_len)
2348 return -1;
2349 return 0;
2352 static int
2353 bfd_mach_o_read_dyld_info (bfd *abfd, bfd_mach_o_load_command *command)
2355 bfd_mach_o_dyld_info_command *cmd = &command->command.dyld_info;
2356 char buf[40];
2358 if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
2359 || bfd_bread ((PTR) buf, sizeof (buf), abfd) != sizeof (buf))
2360 return -1;
2362 cmd->rebase_off = bfd_get_32 (abfd, buf + 0);
2363 cmd->rebase_size = bfd_get_32 (abfd, buf + 4);
2364 cmd->bind_off = bfd_get_32 (abfd, buf + 8);
2365 cmd->bind_size = bfd_get_32 (abfd, buf + 12);
2366 cmd->weak_bind_off = bfd_get_32 (abfd, buf + 16);
2367 cmd->weak_bind_size = bfd_get_32 (abfd, buf + 20);
2368 cmd->lazy_bind_off = bfd_get_32 (abfd, buf + 24);
2369 cmd->lazy_bind_size = bfd_get_32 (abfd, buf + 28);
2370 cmd->export_off = bfd_get_32 (abfd, buf + 32);
2371 cmd->export_size = bfd_get_32 (abfd, buf + 36);
2372 return 0;
2375 static int
2376 bfd_mach_o_read_segment (bfd *abfd,
2377 bfd_mach_o_load_command *command,
2378 unsigned int wide)
2380 unsigned char buf[64];
2381 bfd_mach_o_segment_command *seg = &command->command.segment;
2382 unsigned long i;
2384 if (wide)
2386 BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT_64);
2388 if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
2389 || bfd_bread ((PTR) buf, 64, abfd) != 64)
2390 return -1;
2392 memcpy (seg->segname, buf, 16);
2393 seg->segname[16] = '\0';
2395 seg->vmaddr = bfd_h_get_64 (abfd, buf + 16);
2396 seg->vmsize = bfd_h_get_64 (abfd, buf + 24);
2397 seg->fileoff = bfd_h_get_64 (abfd, buf + 32);
2398 seg->filesize = bfd_h_get_64 (abfd, buf + 40);
2399 seg->maxprot = bfd_h_get_32 (abfd, buf + 48);
2400 seg->initprot = bfd_h_get_32 (abfd, buf + 52);
2401 seg->nsects = bfd_h_get_32 (abfd, buf + 56);
2402 seg->flags = bfd_h_get_32 (abfd, buf + 60);
2404 else
2406 BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT);
2408 if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
2409 || bfd_bread ((PTR) buf, 48, abfd) != 48)
2410 return -1;
2412 memcpy (seg->segname, buf, 16);
2413 seg->segname[16] = '\0';
2415 seg->vmaddr = bfd_h_get_32 (abfd, buf + 16);
2416 seg->vmsize = bfd_h_get_32 (abfd, buf + 20);
2417 seg->fileoff = bfd_h_get_32 (abfd, buf + 24);
2418 seg->filesize = bfd_h_get_32 (abfd, buf + 28);
2419 seg->maxprot = bfd_h_get_32 (abfd, buf + 32);
2420 seg->initprot = bfd_h_get_32 (abfd, buf + 36);
2421 seg->nsects = bfd_h_get_32 (abfd, buf + 40);
2422 seg->flags = bfd_h_get_32 (abfd, buf + 44);
2425 if (seg->nsects != 0)
2427 seg->sections = bfd_alloc (abfd, seg->nsects
2428 * sizeof (bfd_mach_o_section));
2429 if (seg->sections == NULL)
2430 return -1;
2432 for (i = 0; i < seg->nsects; i++)
2434 bfd_vma segoff;
2435 if (wide)
2436 segoff = command->offset + BFD_MACH_O_LC_SEGMENT_64_SIZE
2437 + (i * BFD_MACH_O_SECTION_64_SIZE);
2438 else
2439 segoff = command->offset + BFD_MACH_O_LC_SEGMENT_SIZE
2440 + (i * BFD_MACH_O_SECTION_SIZE);
2442 if (bfd_mach_o_read_section
2443 (abfd, &seg->sections[i], segoff, seg->initprot, wide) != 0)
2444 return -1;
2448 return 0;
2451 static int
2452 bfd_mach_o_read_segment_32 (bfd *abfd, bfd_mach_o_load_command *command)
2454 return bfd_mach_o_read_segment (abfd, command, 0);
2457 static int
2458 bfd_mach_o_read_segment_64 (bfd *abfd, bfd_mach_o_load_command *command)
2460 return bfd_mach_o_read_segment (abfd, command, 1);
2463 static int
2464 bfd_mach_o_read_command (bfd *abfd, bfd_mach_o_load_command *command)
2466 unsigned char buf[8];
2468 /* Read command type and length. */
2469 if (bfd_seek (abfd, command->offset, SEEK_SET) != 0
2470 || bfd_bread ((PTR) buf, 8, abfd) != 8)
2471 return -1;
2473 command->type = bfd_h_get_32 (abfd, buf) & ~BFD_MACH_O_LC_REQ_DYLD;
2474 command->type_required = (bfd_h_get_32 (abfd, buf) & BFD_MACH_O_LC_REQ_DYLD
2475 ? TRUE : FALSE);
2476 command->len = bfd_h_get_32 (abfd, buf + 4);
2478 switch (command->type)
2480 case BFD_MACH_O_LC_SEGMENT:
2481 if (bfd_mach_o_read_segment_32 (abfd, command) != 0)
2482 return -1;
2483 break;
2484 case BFD_MACH_O_LC_SEGMENT_64:
2485 if (bfd_mach_o_read_segment_64 (abfd, command) != 0)
2486 return -1;
2487 break;
2488 case BFD_MACH_O_LC_SYMTAB:
2489 if (bfd_mach_o_read_symtab (abfd, command) != 0)
2490 return -1;
2491 break;
2492 case BFD_MACH_O_LC_SYMSEG:
2493 break;
2494 case BFD_MACH_O_LC_THREAD:
2495 case BFD_MACH_O_LC_UNIXTHREAD:
2496 if (bfd_mach_o_read_thread (abfd, command) != 0)
2497 return -1;
2498 break;
2499 case BFD_MACH_O_LC_LOAD_DYLINKER:
2500 case BFD_MACH_O_LC_ID_DYLINKER:
2501 if (bfd_mach_o_read_dylinker (abfd, command) != 0)
2502 return -1;
2503 break;
2504 case BFD_MACH_O_LC_LOAD_DYLIB:
2505 case BFD_MACH_O_LC_ID_DYLIB:
2506 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB:
2507 case BFD_MACH_O_LC_REEXPORT_DYLIB:
2508 if (bfd_mach_o_read_dylib (abfd, command) != 0)
2509 return -1;
2510 break;
2511 case BFD_MACH_O_LC_PREBOUND_DYLIB:
2512 if (bfd_mach_o_read_prebound_dylib (abfd, command) != 0)
2513 return -1;
2514 break;
2515 case BFD_MACH_O_LC_LOADFVMLIB:
2516 case BFD_MACH_O_LC_IDFVMLIB:
2517 case BFD_MACH_O_LC_IDENT:
2518 case BFD_MACH_O_LC_FVMFILE:
2519 case BFD_MACH_O_LC_PREPAGE:
2520 case BFD_MACH_O_LC_ROUTINES:
2521 break;
2522 case BFD_MACH_O_LC_SUB_FRAMEWORK:
2523 case BFD_MACH_O_LC_SUB_UMBRELLA:
2524 case BFD_MACH_O_LC_SUB_LIBRARY:
2525 case BFD_MACH_O_LC_SUB_CLIENT:
2526 case BFD_MACH_O_LC_RPATH:
2527 if (bfd_mach_o_read_str (abfd, command) != 0)
2528 return -1;
2529 break;
2530 case BFD_MACH_O_LC_DYSYMTAB:
2531 if (bfd_mach_o_read_dysymtab (abfd, command) != 0)
2532 return -1;
2533 break;
2534 case BFD_MACH_O_LC_TWOLEVEL_HINTS:
2535 case BFD_MACH_O_LC_PREBIND_CKSUM:
2536 break;
2537 case BFD_MACH_O_LC_UUID:
2538 if (bfd_mach_o_read_uuid (abfd, command) != 0)
2539 return -1;
2540 break;
2541 case BFD_MACH_O_LC_CODE_SIGNATURE:
2542 case BFD_MACH_O_LC_SEGMENT_SPLIT_INFO:
2543 if (bfd_mach_o_read_linkedit (abfd, command) != 0)
2544 return -1;
2545 break;
2546 case BFD_MACH_O_LC_DYLD_INFO:
2547 if (bfd_mach_o_read_dyld_info (abfd, command) != 0)
2548 return -1;
2549 break;
2550 default:
2551 fprintf (stderr, "unable to read unknown load command 0x%lx\n",
2552 (unsigned long) command->type);
2553 break;
2556 return 0;
2559 static void
2560 bfd_mach_o_flatten_sections (bfd *abfd)
2562 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
2563 long csect = 0;
2564 unsigned long i, j;
2566 /* Count total number of sections. */
2567 mdata->nsects = 0;
2569 for (i = 0; i < mdata->header.ncmds; i++)
2571 if (mdata->commands[i].type == BFD_MACH_O_LC_SEGMENT
2572 || mdata->commands[i].type == BFD_MACH_O_LC_SEGMENT_64)
2574 bfd_mach_o_segment_command *seg;
2576 seg = &mdata->commands[i].command.segment;
2577 mdata->nsects += seg->nsects;
2581 /* Allocate sections array. */
2582 mdata->sections = bfd_alloc (abfd,
2583 mdata->nsects * sizeof (bfd_mach_o_section *));
2585 /* Fill the array. */
2586 csect = 0;
2588 for (i = 0; i < mdata->header.ncmds; i++)
2590 if (mdata->commands[i].type == BFD_MACH_O_LC_SEGMENT
2591 || mdata->commands[i].type == BFD_MACH_O_LC_SEGMENT_64)
2593 bfd_mach_o_segment_command *seg;
2595 seg = &mdata->commands[i].command.segment;
2596 BFD_ASSERT (csect + seg->nsects <= mdata->nsects);
2598 for (j = 0; j < seg->nsects; j++)
2599 mdata->sections[csect++] = &seg->sections[j];
2605 bfd_mach_o_scan_start_address (bfd *abfd)
2607 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
2608 bfd_mach_o_thread_command *cmd = NULL;
2609 unsigned long i;
2611 for (i = 0; i < mdata->header.ncmds; i++)
2613 if ((mdata->commands[i].type == BFD_MACH_O_LC_THREAD) ||
2614 (mdata->commands[i].type == BFD_MACH_O_LC_UNIXTHREAD))
2616 if (cmd == NULL)
2617 cmd = &mdata->commands[i].command.thread;
2618 else
2619 return 0;
2623 if (cmd == NULL)
2624 return 0;
2626 for (i = 0; i < cmd->nflavours; i++)
2628 if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_I386)
2629 && (cmd->flavours[i].flavour
2630 == (unsigned long) BFD_MACH_O_x86_THREAD_STATE32))
2632 unsigned char buf[4];
2634 if (bfd_seek (abfd, cmd->flavours[i].offset + 40, SEEK_SET) != 0
2635 || bfd_bread (buf, 4, abfd) != 4)
2636 return -1;
2638 abfd->start_address = bfd_h_get_32 (abfd, buf);
2640 else if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_POWERPC)
2641 && (cmd->flavours[i].flavour == BFD_MACH_O_PPC_THREAD_STATE))
2643 unsigned char buf[4];
2645 if (bfd_seek (abfd, cmd->flavours[i].offset + 0, SEEK_SET) != 0
2646 || bfd_bread (buf, 4, abfd) != 4)
2647 return -1;
2649 abfd->start_address = bfd_h_get_32 (abfd, buf);
2651 else if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_POWERPC_64)
2652 && (cmd->flavours[i].flavour == BFD_MACH_O_PPC_THREAD_STATE64))
2654 unsigned char buf[8];
2656 if (bfd_seek (abfd, cmd->flavours[i].offset + 0, SEEK_SET) != 0
2657 || bfd_bread (buf, 8, abfd) != 8)
2658 return -1;
2660 abfd->start_address = bfd_h_get_64 (abfd, buf);
2662 else if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_X86_64)
2663 && (cmd->flavours[i].flavour == BFD_MACH_O_x86_THREAD_STATE64))
2665 unsigned char buf[8];
2667 if (bfd_seek (abfd, cmd->flavours[i].offset + (16 * 8), SEEK_SET) != 0
2668 || bfd_bread (buf, 8, abfd) != 8)
2669 return -1;
2671 abfd->start_address = bfd_h_get_64 (abfd, buf);
2675 return 0;
2679 bfd_mach_o_scan (bfd *abfd,
2680 bfd_mach_o_header *header,
2681 bfd_mach_o_data_struct *mdata)
2683 unsigned int i;
2684 enum bfd_architecture cputype;
2685 unsigned long cpusubtype;
2686 unsigned int hdrsize;
2688 hdrsize = mach_o_wide_p (header) ?
2689 BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE;
2691 mdata->header = *header;
2693 abfd->flags = abfd->flags & BFD_IN_MEMORY;
2694 switch (header->filetype)
2696 case BFD_MACH_O_MH_OBJECT:
2697 abfd->flags |= HAS_RELOC;
2698 break;
2699 case BFD_MACH_O_MH_EXECUTE:
2700 abfd->flags |= EXEC_P;
2701 break;
2702 case BFD_MACH_O_MH_DYLIB:
2703 case BFD_MACH_O_MH_BUNDLE:
2704 abfd->flags |= DYNAMIC;
2705 break;
2708 abfd->tdata.mach_o_data = mdata;
2710 bfd_mach_o_convert_architecture (header->cputype, header->cpusubtype,
2711 &cputype, &cpusubtype);
2712 if (cputype == bfd_arch_unknown)
2714 fprintf (stderr, "bfd_mach_o_scan: unknown architecture 0x%lx/0x%lx\n",
2715 header->cputype, header->cpusubtype);
2716 return -1;
2719 bfd_set_arch_mach (abfd, cputype, cpusubtype);
2721 if (header->ncmds != 0)
2723 mdata->commands = bfd_alloc
2724 (abfd, header->ncmds * sizeof (bfd_mach_o_load_command));
2725 if (mdata->commands == NULL)
2726 return -1;
2728 for (i = 0; i < header->ncmds; i++)
2730 bfd_mach_o_load_command *cur = &mdata->commands[i];
2732 if (i == 0)
2733 cur->offset = hdrsize;
2734 else
2736 bfd_mach_o_load_command *prev = &mdata->commands[i - 1];
2737 cur->offset = prev->offset + prev->len;
2740 if (bfd_mach_o_read_command (abfd, cur) < 0)
2741 return -1;
2745 if (bfd_mach_o_scan_start_address (abfd) < 0)
2746 return -1;
2748 bfd_mach_o_flatten_sections (abfd);
2749 return 0;
2752 bfd_boolean
2753 bfd_mach_o_mkobject_init (bfd *abfd)
2755 bfd_mach_o_data_struct *mdata = NULL;
2757 mdata = bfd_alloc (abfd, sizeof (bfd_mach_o_data_struct));
2758 if (mdata == NULL)
2759 return FALSE;
2760 abfd->tdata.mach_o_data = mdata;
2762 mdata->header.magic = 0;
2763 mdata->header.cputype = 0;
2764 mdata->header.cpusubtype = 0;
2765 mdata->header.filetype = 0;
2766 mdata->header.ncmds = 0;
2767 mdata->header.sizeofcmds = 0;
2768 mdata->header.flags = 0;
2769 mdata->header.byteorder = BFD_ENDIAN_UNKNOWN;
2770 mdata->commands = NULL;
2771 mdata->nsects = 0;
2772 mdata->sections = NULL;
2774 return TRUE;
2777 const bfd_target *
2778 bfd_mach_o_header_p (bfd *abfd,
2779 bfd_mach_o_filetype filetype,
2780 bfd_mach_o_cpu_type cputype)
2782 struct bfd_preserve preserve;
2783 bfd_mach_o_header header;
2785 preserve.marker = NULL;
2786 if (!bfd_mach_o_read_header (abfd, &header))
2787 goto wrong;
2789 if (! (header.byteorder == BFD_ENDIAN_BIG
2790 || header.byteorder == BFD_ENDIAN_LITTLE))
2792 fprintf (stderr, "unknown header byte-order value 0x%lx\n",
2793 (unsigned long) header.byteorder);
2794 goto wrong;
2797 if (! ((header.byteorder == BFD_ENDIAN_BIG
2798 && abfd->xvec->byteorder == BFD_ENDIAN_BIG
2799 && abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
2800 || (header.byteorder == BFD_ENDIAN_LITTLE
2801 && abfd->xvec->byteorder == BFD_ENDIAN_LITTLE
2802 && abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)))
2803 goto wrong;
2805 /* Check cputype and filetype.
2806 In case of wildcard, do not accept magics that are handled by existing
2807 targets. */
2808 if (cputype)
2810 if (header.cputype != cputype)
2811 goto wrong;
2813 else
2815 switch (header.cputype)
2817 case BFD_MACH_O_CPU_TYPE_I386:
2818 /* Handled by mach-o-i386 */
2819 goto wrong;
2820 default:
2821 break;
2824 if (filetype)
2826 if (header.filetype != filetype)
2827 goto wrong;
2829 else
2831 switch (header.filetype)
2833 case BFD_MACH_O_MH_CORE:
2834 /* Handled by core_p */
2835 goto wrong;
2836 default:
2837 break;
2841 preserve.marker = bfd_zalloc (abfd, sizeof (bfd_mach_o_data_struct));
2842 if (preserve.marker == NULL
2843 || !bfd_preserve_save (abfd, &preserve))
2844 goto fail;
2846 if (bfd_mach_o_scan (abfd, &header,
2847 (bfd_mach_o_data_struct *) preserve.marker) != 0)
2848 goto wrong;
2850 bfd_preserve_finish (abfd, &preserve);
2851 return abfd->xvec;
2853 wrong:
2854 bfd_set_error (bfd_error_wrong_format);
2856 fail:
2857 if (preserve.marker != NULL)
2858 bfd_preserve_restore (abfd, &preserve);
2859 return NULL;
2862 static const bfd_target *
2863 bfd_mach_o_gen_object_p (bfd *abfd)
2865 return bfd_mach_o_header_p (abfd, 0, 0);
2868 static const bfd_target *
2869 bfd_mach_o_gen_core_p (bfd *abfd)
2871 return bfd_mach_o_header_p (abfd, BFD_MACH_O_MH_CORE, 0);
2874 typedef struct mach_o_fat_archentry
2876 unsigned long cputype;
2877 unsigned long cpusubtype;
2878 unsigned long offset;
2879 unsigned long size;
2880 unsigned long align;
2881 } mach_o_fat_archentry;
2883 typedef struct mach_o_fat_data_struct
2885 unsigned long magic;
2886 unsigned long nfat_arch;
2887 mach_o_fat_archentry *archentries;
2888 } mach_o_fat_data_struct;
2890 const bfd_target *
2891 bfd_mach_o_archive_p (bfd *abfd)
2893 mach_o_fat_data_struct *adata = NULL;
2894 unsigned char buf[20];
2895 unsigned long i;
2897 if (bfd_seek (abfd, 0, SEEK_SET) != 0
2898 || bfd_bread ((PTR) buf, 8, abfd) != 8)
2899 goto error;
2901 adata = bfd_alloc (abfd, sizeof (mach_o_fat_data_struct));
2902 if (adata == NULL)
2903 goto error;
2905 adata->magic = bfd_getb32 (buf);
2906 adata->nfat_arch = bfd_getb32 (buf + 4);
2907 if (adata->magic != 0xcafebabe)
2908 goto error;
2909 /* Avoid matching Java bytecode files, which have the same magic number.
2910 In the Java bytecode file format this field contains the JVM version,
2911 which starts at 43.0. */
2912 if (adata->nfat_arch > 30)
2913 goto error;
2915 adata->archentries =
2916 bfd_alloc (abfd, adata->nfat_arch * sizeof (mach_o_fat_archentry));
2917 if (adata->archentries == NULL)
2918 goto error;
2920 for (i = 0; i < adata->nfat_arch; i++)
2922 if (bfd_seek (abfd, 8 + 20 * i, SEEK_SET) != 0
2923 || bfd_bread ((PTR) buf, 20, abfd) != 20)
2924 goto error;
2925 adata->archentries[i].cputype = bfd_getb32 (buf);
2926 adata->archentries[i].cpusubtype = bfd_getb32 (buf + 4);
2927 adata->archentries[i].offset = bfd_getb32 (buf + 8);
2928 adata->archentries[i].size = bfd_getb32 (buf + 12);
2929 adata->archentries[i].align = bfd_getb32 (buf + 16);
2932 abfd->tdata.mach_o_fat_data = adata;
2933 return abfd->xvec;
2935 error:
2936 if (adata != NULL)
2937 bfd_release (abfd, adata);
2938 bfd_set_error (bfd_error_wrong_format);
2939 return NULL;
2942 bfd *
2943 bfd_mach_o_openr_next_archived_file (bfd *archive, bfd *prev)
2945 mach_o_fat_data_struct *adata;
2946 mach_o_fat_archentry *entry = NULL;
2947 unsigned long i;
2948 bfd *nbfd;
2949 enum bfd_architecture arch_type;
2950 unsigned long arch_subtype;
2952 adata = (mach_o_fat_data_struct *) archive->tdata.mach_o_fat_data;
2953 BFD_ASSERT (adata != NULL);
2955 /* Find index of previous entry. */
2956 if (prev == NULL)
2957 i = 0; /* Start at first one. */
2958 else
2960 for (i = 0; i < adata->nfat_arch; i++)
2962 if (adata->archentries[i].offset == prev->origin)
2963 break;
2966 if (i == adata->nfat_arch)
2968 /* Not found. */
2969 bfd_set_error (bfd_error_bad_value);
2970 return NULL;
2972 i++; /* Get next entry. */
2975 if (i >= adata->nfat_arch)
2977 bfd_set_error (bfd_error_no_more_archived_files);
2978 return NULL;
2981 entry = &adata->archentries[i];
2982 nbfd = _bfd_new_bfd_contained_in (archive);
2983 if (nbfd == NULL)
2984 return NULL;
2986 nbfd->origin = entry->offset;
2988 bfd_mach_o_convert_architecture (entry->cputype, entry->cpusubtype,
2989 &arch_type, &arch_subtype);
2990 /* Create the member filename.
2991 Use FILENAME:ARCH_NAME. */
2993 char *s = NULL;
2994 const char *arch_name;
2995 size_t arch_file_len = strlen (bfd_get_filename (archive));
2997 arch_name = bfd_printable_arch_mach (arch_type, arch_subtype);
2998 s = bfd_malloc (arch_file_len + 1 + strlen (arch_name) + 1);
2999 if (s == NULL)
3000 return NULL;
3001 memcpy (s, bfd_get_filename (archive), arch_file_len);
3002 s[arch_file_len] = ':';
3003 strcpy (s + arch_file_len + 1, arch_name);
3004 nbfd->filename = s;
3006 nbfd->iostream = NULL;
3007 bfd_set_arch_mach (nbfd, arch_type, arch_subtype);
3009 return nbfd;
3012 /* If ABFD format is FORMAT and architecture is ARCH, return it.
3013 If ABFD is a fat image containing a member that corresponds to FORMAT
3014 and ARCH, returns it.
3015 In other case, returns NULL.
3016 This function allows transparent uses of fat images. */
3017 bfd *
3018 bfd_mach_o_fat_extract (bfd *abfd,
3019 bfd_format format,
3020 const bfd_arch_info_type *arch)
3022 bfd *res;
3023 mach_o_fat_data_struct *adata;
3024 unsigned int i;
3026 if (bfd_check_format (abfd, format))
3028 if (bfd_get_arch_info (abfd) == arch)
3029 return abfd;
3030 return NULL;
3032 if (!bfd_check_format (abfd, bfd_archive)
3033 || abfd->xvec != &mach_o_fat_vec)
3034 return NULL;
3036 /* This is a Mach-O fat image. */
3037 adata = (mach_o_fat_data_struct *) abfd->tdata.mach_o_fat_data;
3038 BFD_ASSERT (adata != NULL);
3040 for (i = 0; i < adata->nfat_arch; i++)
3042 struct mach_o_fat_archentry *e = &adata->archentries[i];
3043 enum bfd_architecture cpu_type;
3044 unsigned long cpu_subtype;
3046 bfd_mach_o_convert_architecture (e->cputype, e->cpusubtype,
3047 &cpu_type, &cpu_subtype);
3048 if (cpu_type != arch->arch || cpu_subtype != arch->mach)
3049 continue;
3051 /* The architecture is found. */
3052 res = _bfd_new_bfd_contained_in (abfd);
3053 if (res == NULL)
3054 return NULL;
3056 res->origin = e->offset;
3058 res->filename = strdup (abfd->filename);
3059 res->iostream = NULL;
3061 if (bfd_check_format (res, format))
3063 BFD_ASSERT (bfd_get_arch_info (res) == arch);
3064 return res;
3066 bfd_close (res);
3067 return NULL;
3070 return NULL;
3074 bfd_mach_o_lookup_section (bfd *abfd,
3075 asection *section,
3076 bfd_mach_o_load_command **mcommand,
3077 bfd_mach_o_section **msection)
3079 struct mach_o_data_struct *md = bfd_mach_o_get_data (abfd);
3080 unsigned int i, j, num;
3082 bfd_mach_o_load_command *ncmd = NULL;
3083 bfd_mach_o_section *nsect = NULL;
3085 BFD_ASSERT (mcommand != NULL);
3086 BFD_ASSERT (msection != NULL);
3088 num = 0;
3089 for (i = 0; i < md->header.ncmds; i++)
3091 struct bfd_mach_o_load_command *cmd = &md->commands[i];
3092 struct bfd_mach_o_segment_command *seg = NULL;
3094 if (cmd->type != BFD_MACH_O_LC_SEGMENT
3095 || cmd->type != BFD_MACH_O_LC_SEGMENT_64)
3096 continue;
3097 seg = &cmd->command.segment;
3099 for (j = 0; j < seg->nsects; j++)
3101 struct bfd_mach_o_section *sect = &seg->sections[j];
3103 if (sect->bfdsection == section)
3105 if (num == 0)
3107 nsect = sect;
3108 ncmd = cmd;
3110 num++;
3115 *mcommand = ncmd;
3116 *msection = nsect;
3117 return num;
3121 bfd_mach_o_lookup_command (bfd *abfd,
3122 bfd_mach_o_load_command_type type,
3123 bfd_mach_o_load_command **mcommand)
3125 struct mach_o_data_struct *md = bfd_mach_o_get_data (abfd);
3126 bfd_mach_o_load_command *ncmd = NULL;
3127 unsigned int i, num;
3129 BFD_ASSERT (md != NULL);
3130 BFD_ASSERT (mcommand != NULL);
3132 num = 0;
3133 for (i = 0; i < md->header.ncmds; i++)
3135 struct bfd_mach_o_load_command *cmd = &md->commands[i];
3137 if (cmd->type != type)
3138 continue;
3140 if (num == 0)
3141 ncmd = cmd;
3142 num++;
3145 *mcommand = ncmd;
3146 return num;
3149 unsigned long
3150 bfd_mach_o_stack_addr (enum bfd_mach_o_cpu_type type)
3152 switch (type)
3154 case BFD_MACH_O_CPU_TYPE_MC680x0:
3155 return 0x04000000;
3156 case BFD_MACH_O_CPU_TYPE_MC88000:
3157 return 0xffffe000;
3158 case BFD_MACH_O_CPU_TYPE_POWERPC:
3159 return 0xc0000000;
3160 case BFD_MACH_O_CPU_TYPE_I386:
3161 return 0xc0000000;
3162 case BFD_MACH_O_CPU_TYPE_SPARC:
3163 return 0xf0000000;
3164 case BFD_MACH_O_CPU_TYPE_I860:
3165 return 0;
3166 case BFD_MACH_O_CPU_TYPE_HPPA:
3167 return 0xc0000000 - 0x04000000;
3168 default:
3169 return 0;
3173 typedef struct bfd_mach_o_xlat_name
3175 const char *name;
3176 unsigned long val;
3178 bfd_mach_o_xlat_name;
3180 static void
3181 bfd_mach_o_print_flags (const bfd_mach_o_xlat_name *table,
3182 unsigned long val,
3183 FILE *file)
3185 int first = 1;
3187 for (; table->name; table++)
3189 if (table->val & val)
3191 if (!first)
3192 fprintf (file, "+");
3193 fprintf (file, "%s", table->name);
3194 val &= ~table->val;
3195 first = 0;
3198 if (val)
3200 if (!first)
3201 fprintf (file, "+");
3202 fprintf (file, "0x%lx", val);
3203 return;
3205 if (first)
3206 fprintf (file, "-");
3209 static const char *
3210 bfd_mach_o_get_name (const bfd_mach_o_xlat_name *table, unsigned long val)
3212 for (; table->name; table++)
3213 if (table->val == val)
3214 return table->name;
3215 return "*UNKNOWN*";
3218 static bfd_mach_o_xlat_name bfd_mach_o_cpu_name[] =
3220 { "vax", BFD_MACH_O_CPU_TYPE_VAX },
3221 { "mc680x0", BFD_MACH_O_CPU_TYPE_MC680x0 },
3222 { "i386", BFD_MACH_O_CPU_TYPE_I386 },
3223 { "mips", BFD_MACH_O_CPU_TYPE_MIPS },
3224 { "mc98000", BFD_MACH_O_CPU_TYPE_MC98000 },
3225 { "hppa", BFD_MACH_O_CPU_TYPE_HPPA },
3226 { "arm", BFD_MACH_O_CPU_TYPE_ARM },
3227 { "mc88000", BFD_MACH_O_CPU_TYPE_MC88000 },
3228 { "sparc", BFD_MACH_O_CPU_TYPE_SPARC },
3229 { "i860", BFD_MACH_O_CPU_TYPE_I860 },
3230 { "alpha", BFD_MACH_O_CPU_TYPE_ALPHA },
3231 { "powerpc", BFD_MACH_O_CPU_TYPE_POWERPC },
3232 { "powerpc_64", BFD_MACH_O_CPU_TYPE_POWERPC_64 },
3233 { "x86_64", BFD_MACH_O_CPU_TYPE_X86_64 },
3234 { NULL, 0}
3237 static bfd_mach_o_xlat_name bfd_mach_o_filetype_name[] =
3239 { "object", BFD_MACH_O_MH_OBJECT },
3240 { "execute", BFD_MACH_O_MH_EXECUTE },
3241 { "fvmlib", BFD_MACH_O_MH_FVMLIB },
3242 { "core", BFD_MACH_O_MH_CORE },
3243 { "preload", BFD_MACH_O_MH_PRELOAD },
3244 { "dylib", BFD_MACH_O_MH_DYLIB },
3245 { "dylinker", BFD_MACH_O_MH_DYLINKER },
3246 { "bundle", BFD_MACH_O_MH_BUNDLE },
3247 { "dylib_stub", BFD_MACH_O_MH_DYLIB_STUB },
3248 { "dym", BFD_MACH_O_MH_DSYM },
3249 { "kext_bundle", BFD_MACH_O_MH_KEXT_BUNDLE },
3250 { NULL, 0}
3253 static bfd_mach_o_xlat_name bfd_mach_o_header_flags_name[] =
3255 { "noundefs", BFD_MACH_O_MH_NOUNDEFS },
3256 { "incrlink", BFD_MACH_O_MH_INCRLINK },
3257 { "dyldlink", BFD_MACH_O_MH_DYLDLINK },
3258 { "bindatload", BFD_MACH_O_MH_BINDATLOAD },
3259 { "prebound", BFD_MACH_O_MH_PREBOUND },
3260 { "split_segs", BFD_MACH_O_MH_SPLIT_SEGS },
3261 { "lazy_init", BFD_MACH_O_MH_LAZY_INIT },
3262 { "twolevel", BFD_MACH_O_MH_TWOLEVEL },
3263 { "force_flat", BFD_MACH_O_MH_FORCE_FLAT },
3264 { "nomultidefs", BFD_MACH_O_MH_NOMULTIDEFS },
3265 { "nofixprebinding", BFD_MACH_O_MH_NOFIXPREBINDING },
3266 { "prebindable", BFD_MACH_O_MH_PREBINDABLE },
3267 { "allmodsbound", BFD_MACH_O_MH_ALLMODSBOUND },
3268 { "subsections_via_symbols", BFD_MACH_O_MH_SUBSECTIONS_VIA_SYMBOLS },
3269 { "canonical", BFD_MACH_O_MH_CANONICAL },
3270 { "weak_defines", BFD_MACH_O_MH_WEAK_DEFINES },
3271 { "binds_to_weak", BFD_MACH_O_MH_BINDS_TO_WEAK },
3272 { "allow_stack_execution", BFD_MACH_O_MH_ALLOW_STACK_EXECUTION },
3273 { "root_safe", BFD_MACH_O_MH_ROOT_SAFE },
3274 { "setuid_safe", BFD_MACH_O_MH_SETUID_SAFE },
3275 { "no_reexported_dylibs", BFD_MACH_O_MH_NO_REEXPORTED_DYLIBS },
3276 { "pie", BFD_MACH_O_MH_PIE },
3277 { NULL, 0}
3280 static bfd_mach_o_xlat_name bfd_mach_o_section_type_name[] =
3282 { "regular", BFD_MACH_O_S_REGULAR},
3283 { "zerofill", BFD_MACH_O_S_ZEROFILL},
3284 { "cstring_literals", BFD_MACH_O_S_CSTRING_LITERALS},
3285 { "4byte_literals", BFD_MACH_O_S_4BYTE_LITERALS},
3286 { "8byte_literals", BFD_MACH_O_S_8BYTE_LITERALS},
3287 { "literal_pointers", BFD_MACH_O_S_LITERAL_POINTERS},
3288 { "non_lazy_symbol_pointers", BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS},
3289 { "lazy_symbol_pointers", BFD_MACH_O_S_LAZY_SYMBOL_POINTERS},
3290 { "symbol_stubs", BFD_MACH_O_S_SYMBOL_STUBS},
3291 { "mod_init_func_pointers", BFD_MACH_O_S_MOD_INIT_FUNC_POINTERS},
3292 { "mod_fini_func_pointers", BFD_MACH_O_S_MOD_FINI_FUNC_POINTERS},
3293 { "coalesced", BFD_MACH_O_S_COALESCED},
3294 { "gb_zerofill", BFD_MACH_O_S_GB_ZEROFILL},
3295 { "interposing", BFD_MACH_O_S_INTERPOSING},
3296 { "16byte_literals", BFD_MACH_O_S_16BYTE_LITERALS},
3297 { "dtrace_dof", BFD_MACH_O_S_DTRACE_DOF},
3298 { "lazy_dylib_symbol_pointers", BFD_MACH_O_S_LAZY_DYLIB_SYMBOL_POINTERS},
3299 { NULL, 0}
3302 static bfd_mach_o_xlat_name bfd_mach_o_section_attribute_name[] =
3304 { "loc_reloc", BFD_MACH_O_S_ATTR_LOC_RELOC },
3305 { "ext_reloc", BFD_MACH_O_S_ATTR_EXT_RELOC },
3306 { "some_instructions", BFD_MACH_O_S_ATTR_SOME_INSTRUCTIONS },
3307 { "debug", BFD_MACH_O_S_ATTR_DEBUG },
3308 { "modifying_code", BFD_MACH_O_S_SELF_MODIFYING_CODE },
3309 { "live_support", BFD_MACH_O_S_ATTR_LIVE_SUPPORT },
3310 { "no_dead_strip", BFD_MACH_O_S_ATTR_NO_DEAD_STRIP },
3311 { "strip_static_syms", BFD_MACH_O_S_ATTR_STRIP_STATIC_SYMS },
3312 { "no_toc", BFD_MACH_O_S_ATTR_NO_TOC },
3313 { "pure_instructions", BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS },
3314 { NULL, 0}
3317 static bfd_mach_o_xlat_name bfd_mach_o_load_command_name[] =
3319 { "segment", BFD_MACH_O_LC_SEGMENT},
3320 { "symtab", BFD_MACH_O_LC_SYMTAB},
3321 { "symseg", BFD_MACH_O_LC_SYMSEG},
3322 { "thread", BFD_MACH_O_LC_THREAD},
3323 { "unixthread", BFD_MACH_O_LC_UNIXTHREAD},
3324 { "loadfvmlib", BFD_MACH_O_LC_LOADFVMLIB},
3325 { "idfvmlib", BFD_MACH_O_LC_IDFVMLIB},
3326 { "ident", BFD_MACH_O_LC_IDENT},
3327 { "fvmfile", BFD_MACH_O_LC_FVMFILE},
3328 { "prepage", BFD_MACH_O_LC_PREPAGE},
3329 { "dysymtab", BFD_MACH_O_LC_DYSYMTAB},
3330 { "load_dylib", BFD_MACH_O_LC_LOAD_DYLIB},
3331 { "id_dylib", BFD_MACH_O_LC_ID_DYLIB},
3332 { "load_dylinker", BFD_MACH_O_LC_LOAD_DYLINKER},
3333 { "id_dylinker", BFD_MACH_O_LC_ID_DYLINKER},
3334 { "prebound_dylib", BFD_MACH_O_LC_PREBOUND_DYLIB},
3335 { "routines", BFD_MACH_O_LC_ROUTINES},
3336 { "sub_framework", BFD_MACH_O_LC_SUB_FRAMEWORK},
3337 { "sub_umbrella", BFD_MACH_O_LC_SUB_UMBRELLA},
3338 { "sub_client", BFD_MACH_O_LC_SUB_CLIENT},
3339 { "sub_library", BFD_MACH_O_LC_SUB_LIBRARY},
3340 { "twolevel_hints", BFD_MACH_O_LC_TWOLEVEL_HINTS},
3341 { "prebind_cksum", BFD_MACH_O_LC_PREBIND_CKSUM},
3342 { "load_weak_dylib", BFD_MACH_O_LC_LOAD_WEAK_DYLIB},
3343 { "segment_64", BFD_MACH_O_LC_SEGMENT_64},
3344 { "routines_64", BFD_MACH_O_LC_ROUTINES_64},
3345 { "uuid", BFD_MACH_O_LC_UUID},
3346 { "rpath", BFD_MACH_O_LC_RPATH},
3347 { "code_signature", BFD_MACH_O_LC_CODE_SIGNATURE},
3348 { "segment_split_info", BFD_MACH_O_LC_SEGMENT_SPLIT_INFO},
3349 { "reexport_dylib", BFD_MACH_O_LC_REEXPORT_DYLIB},
3350 { "lazy_load_dylib", BFD_MACH_O_LC_LAZY_LOAD_DYLIB},
3351 { "encryption_info", BFD_MACH_O_LC_ENCRYPTION_INFO},
3352 { "dyld_info", BFD_MACH_O_LC_DYLD_INFO},
3353 { NULL, 0}
3356 static void
3357 bfd_mach_o_print_private_header (bfd *abfd, FILE *file)
3359 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
3360 bfd_mach_o_header *h = &mdata->header;
3362 fputs (_("Mach-O header:\n"), file);
3363 fprintf (file, _(" magic : %08lx\n"), h->magic);
3364 fprintf (file, _(" cputype : %08lx (%s)\n"), h->cputype,
3365 bfd_mach_o_get_name (bfd_mach_o_cpu_name, h->cputype));
3366 fprintf (file, _(" cpusubtype: %08lx\n"), h->cpusubtype);
3367 fprintf (file, _(" filetype : %08lx (%s)\n"),
3368 h->filetype,
3369 bfd_mach_o_get_name (bfd_mach_o_filetype_name, h->filetype));
3370 fprintf (file, _(" ncmds : %08lx (%lu)\n"), h->ncmds, h->ncmds);
3371 fprintf (file, _(" sizeofcmds: %08lx\n"), h->sizeofcmds);
3372 fprintf (file, _(" flags : %08lx ("), h->flags);
3373 bfd_mach_o_print_flags (bfd_mach_o_header_flags_name, h->flags, file);
3374 fputs (_(")\n"), file);
3375 fprintf (file, _(" reserved : %08x\n"), h->reserved);
3378 static void
3379 bfd_mach_o_print_section_map (bfd *abfd, FILE *file)
3381 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
3382 unsigned int i, j;
3383 unsigned int sec_nbr = 0;
3385 fputs (_("Segments and Sections:\n"), file);
3386 fputs (_(" #: Segment name Section name Address\n"), file);
3388 for (i = 0; i < mdata->header.ncmds; i++)
3390 bfd_mach_o_segment_command *seg;
3392 if (mdata->commands[i].type != BFD_MACH_O_LC_SEGMENT
3393 && mdata->commands[i].type != BFD_MACH_O_LC_SEGMENT_64)
3394 continue;
3396 seg = &mdata->commands[i].command.segment;
3398 fprintf (file, "[Segment %-16s ", seg->segname);
3399 fprintf_vma (file, seg->vmaddr);
3400 fprintf (file, "-");
3401 fprintf_vma (file, seg->vmaddr + seg->vmsize - 1);
3402 fputc (' ', file);
3403 fputc (seg->initprot & BFD_MACH_O_PROT_READ ? 'r' : '-', file);
3404 fputc (seg->initprot & BFD_MACH_O_PROT_WRITE ? 'w' : '-', file);
3405 fputc (seg->initprot & BFD_MACH_O_PROT_EXECUTE ? 'x' : '-', file);
3406 fprintf (file, "]\n");
3407 for (j = 0; j < seg->nsects; j++)
3409 bfd_mach_o_section *sec = &seg->sections[j];
3410 fprintf (file, "%02u: %-16s %-16s ", ++sec_nbr,
3411 sec->segname, sec->sectname);
3412 fprintf_vma (file, sec->addr);
3413 fprintf (file, " ");
3414 fprintf_vma (file, sec->size);
3415 fprintf (file, " %08lx\n", sec->flags);
3420 static void
3421 bfd_mach_o_print_section (bfd *abfd ATTRIBUTE_UNUSED,
3422 bfd_mach_o_section *sec, FILE *file)
3424 fprintf (file, " Section: %-16s %-16s (bfdname: %s)\n",
3425 sec->sectname, sec->segname, sec->bfdsection->name);
3426 fprintf (file, " addr: ");
3427 fprintf_vma (file, sec->addr);
3428 fprintf (file, " size: ");
3429 fprintf_vma (file, sec->size);
3430 fprintf (file, " offset: ");
3431 fprintf_vma (file, sec->offset);
3432 fprintf (file, "\n");
3433 fprintf (file, " align: %ld", sec->align);
3434 fprintf (file, " nreloc: %lu reloff: ", sec->nreloc);
3435 fprintf_vma (file, sec->reloff);
3436 fprintf (file, "\n");
3437 fprintf (file, " flags: %08lx (type: %s", sec->flags,
3438 bfd_mach_o_get_name (bfd_mach_o_section_type_name,
3439 sec->flags & BFD_MACH_O_SECTION_TYPE_MASK));
3440 fprintf (file, " attr: ");
3441 bfd_mach_o_print_flags (bfd_mach_o_section_attribute_name,
3442 sec->flags & BFD_MACH_O_SECTION_ATTRIBUTES_MASK,
3443 file);
3444 fprintf (file, ")\n");
3445 switch (sec->flags & BFD_MACH_O_SECTION_TYPE_MASK)
3447 case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS:
3448 case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS:
3449 case BFD_MACH_O_S_SYMBOL_STUBS:
3450 fprintf (file, " first indirect sym: %lu", sec->reserved1);
3451 fprintf (file, " (%u entries)",
3452 bfd_mach_o_section_get_nbr_indirect (abfd, sec));
3453 break;
3454 default:
3455 fprintf (file, " reserved1: 0x%lx", sec->reserved1);
3456 break;
3458 switch (sec->flags & BFD_MACH_O_SECTION_TYPE_MASK)
3460 case BFD_MACH_O_S_SYMBOL_STUBS:
3461 fprintf (file, " stub size: %lu", sec->reserved2);
3462 break;
3463 default:
3464 fprintf (file, " reserved2: 0x%lx", sec->reserved2);
3465 break;
3467 fprintf (file, " reserved3: 0x%lx\n", sec->reserved3);
3470 static void
3471 bfd_mach_o_print_segment (bfd *abfd ATTRIBUTE_UNUSED,
3472 bfd_mach_o_load_command *cmd, FILE *file)
3474 bfd_mach_o_segment_command *seg = &cmd->command.segment;
3475 unsigned int i;
3477 fprintf (file, " name: %s\n", *seg->segname ? seg->segname : "*none*");
3478 fprintf (file, " vmaddr: ");
3479 fprintf_vma (file, seg->vmaddr);
3480 fprintf (file, " vmsize: ");
3481 fprintf_vma (file, seg->vmsize);
3482 fprintf (file, "\n");
3483 fprintf (file, " fileoff: ");
3484 fprintf_vma (file, seg->fileoff);
3485 fprintf (file, " filesize: ");
3486 fprintf_vma (file, (bfd_vma)seg->filesize);
3487 fprintf (file, " endoff: ");
3488 fprintf_vma (file, (bfd_vma)(seg->fileoff + seg->filesize));
3489 fprintf (file, "\n");
3490 fprintf (file, " nsects: %lu ", seg->nsects);
3491 fprintf (file, " flags: %lx\n", seg->flags);
3492 for (i = 0; i < seg->nsects; i++)
3493 bfd_mach_o_print_section (abfd, &seg->sections[i], file);
3496 static void
3497 bfd_mach_o_print_dysymtab (bfd *abfd ATTRIBUTE_UNUSED,
3498 bfd_mach_o_load_command *cmd, FILE *file)
3500 bfd_mach_o_dysymtab_command *dysymtab = &cmd->command.dysymtab;
3501 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
3502 unsigned int i;
3504 fprintf (file, " local symbols: idx: %10lu num: %-8lu",
3505 dysymtab->ilocalsym, dysymtab->nlocalsym);
3506 fprintf (file, " (nxtidx: %lu)\n",
3507 dysymtab->ilocalsym + dysymtab->nlocalsym);
3508 fprintf (file, " external symbols: idx: %10lu num: %-8lu",
3509 dysymtab->iextdefsym, dysymtab->nextdefsym);
3510 fprintf (file, " (nxtidx: %lu)\n",
3511 dysymtab->iextdefsym + dysymtab->nextdefsym);
3512 fprintf (file, " undefined symbols: idx: %10lu num: %-8lu",
3513 dysymtab->iundefsym, dysymtab->nundefsym);
3514 fprintf (file, " (nxtidx: %lu)\n",
3515 dysymtab->iundefsym + dysymtab->nundefsym);
3516 fprintf (file, " table of content: off: 0x%08lx num: %-8lu",
3517 dysymtab->tocoff, dysymtab->ntoc);
3518 fprintf (file, " (endoff: 0x%08lx)\n",
3519 dysymtab->tocoff
3520 + dysymtab->ntoc * BFD_MACH_O_TABLE_OF_CONTENT_SIZE);
3521 fprintf (file, " module table: off: 0x%08lx num: %-8lu",
3522 dysymtab->modtaboff, dysymtab->nmodtab);
3523 fprintf (file, " (endoff: 0x%08lx)\n",
3524 dysymtab->modtaboff + dysymtab->nmodtab
3525 * (mach_o_wide_p (&mdata->header) ?
3526 BFD_MACH_O_DYLIB_MODULE_64_SIZE : BFD_MACH_O_DYLIB_MODULE_SIZE));
3527 fprintf (file, " external reference table: off: 0x%08lx num: %-8lu",
3528 dysymtab->extrefsymoff, dysymtab->nextrefsyms);
3529 fprintf (file, " (endoff: 0x%08lx)\n",
3530 dysymtab->extrefsymoff
3531 + dysymtab->nextrefsyms * BFD_MACH_O_REFERENCE_SIZE);
3532 fprintf (file, " indirect symbol table: off: 0x%08lx num: %-8lu",
3533 dysymtab->indirectsymoff, dysymtab->nindirectsyms);
3534 fprintf (file, " (endoff: 0x%08lx)\n",
3535 dysymtab->indirectsymoff
3536 + dysymtab->nindirectsyms * BFD_MACH_O_INDIRECT_SYMBOL_SIZE);
3537 fprintf (file, " external relocation table: off: 0x%08lx num: %-8lu",
3538 dysymtab->extreloff, dysymtab->nextrel);
3539 fprintf (file, " (endoff: 0x%08lx)\n",
3540 dysymtab->extreloff + dysymtab->nextrel * BFD_MACH_O_RELENT_SIZE);
3541 fprintf (file, " local relocation table: off: 0x%08lx num: %-8lu",
3542 dysymtab->locreloff, dysymtab->nlocrel);
3543 fprintf (file, " (endoff: 0x%08lx)\n",
3544 dysymtab->locreloff + dysymtab->nlocrel * BFD_MACH_O_RELENT_SIZE);
3546 if (dysymtab->ntoc > 0
3547 || dysymtab->nindirectsyms > 0
3548 || dysymtab->nextrefsyms > 0)
3550 /* Try to read the symbols to display the toc or indirect symbols. */
3551 bfd_mach_o_read_symtab_symbols (abfd);
3553 else if (dysymtab->nmodtab > 0)
3555 /* Try to read the strtab to display modules name. */
3556 bfd_mach_o_read_symtab_strtab (abfd);
3559 for (i = 0; i < dysymtab->nmodtab; i++)
3561 bfd_mach_o_dylib_module *module = &dysymtab->dylib_module[i];
3562 fprintf (file, " module %u:\n", i);
3563 fprintf (file, " name: %lu", module->module_name_idx);
3564 if (mdata->symtab && mdata->symtab->strtab)
3565 fprintf (file, ": %s",
3566 mdata->symtab->strtab + module->module_name_idx);
3567 fprintf (file, "\n");
3568 fprintf (file, " extdefsym: idx: %8lu num: %lu\n",
3569 module->iextdefsym, module->nextdefsym);
3570 fprintf (file, " refsym: idx: %8lu num: %lu\n",
3571 module->irefsym, module->nrefsym);
3572 fprintf (file, " localsym: idx: %8lu num: %lu\n",
3573 module->ilocalsym, module->nlocalsym);
3574 fprintf (file, " extrel: idx: %8lu num: %lu\n",
3575 module->iextrel, module->nextrel);
3576 fprintf (file, " init: idx: %8u num: %u\n",
3577 module->iinit, module->ninit);
3578 fprintf (file, " term: idx: %8u num: %u\n",
3579 module->iterm, module->nterm);
3580 fprintf (file, " objc_module_info: addr: ");
3581 fprintf_vma (file, module->objc_module_info_addr);
3582 fprintf (file, " size: %lu\n", module->objc_module_info_size);
3585 if (dysymtab->ntoc > 0)
3587 bfd_mach_o_symtab_command *symtab = mdata->symtab;
3589 fprintf (file, " table of content: (symbol/module)\n");
3590 for (i = 0; i < dysymtab->ntoc; i++)
3592 bfd_mach_o_dylib_table_of_content *toc = &dysymtab->dylib_toc[i];
3594 fprintf (file, " %4u: ", i);
3595 if (symtab && symtab->symbols && toc->symbol_index < symtab->nsyms)
3597 const char *name = symtab->symbols[toc->symbol_index].symbol.name;
3598 fprintf (file, "%s (%lu)", name ? name : "*invalid*",
3599 toc->symbol_index);
3601 else
3602 fprintf (file, "%lu", toc->symbol_index);
3604 fprintf (file, " / ");
3605 if (symtab && symtab->strtab
3606 && toc->module_index < dysymtab->nmodtab)
3608 bfd_mach_o_dylib_module *mod;
3609 mod = &dysymtab->dylib_module[toc->module_index];
3610 fprintf (file, "%s (%lu)",
3611 symtab->strtab + mod->module_name_idx,
3612 toc->module_index);
3614 else
3615 fprintf (file, "%lu", toc->module_index);
3617 fprintf (file, "\n");
3621 if (dysymtab->nindirectsyms != 0)
3623 fprintf (file, " indirect symbols:\n");
3625 for (i = 0; i < mdata->nsects; i++)
3627 bfd_mach_o_section *sec = mdata->sections[i];
3628 unsigned int j, first, last;
3629 bfd_mach_o_symtab_command *symtab = mdata->symtab;
3630 bfd_vma addr;
3631 bfd_vma entry_size;
3633 switch (sec->flags & BFD_MACH_O_SECTION_TYPE_MASK)
3635 case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS:
3636 case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS:
3637 case BFD_MACH_O_S_SYMBOL_STUBS:
3638 first = sec->reserved1;
3639 last = first + bfd_mach_o_section_get_nbr_indirect (abfd, sec);
3640 addr = sec->addr;
3641 entry_size = bfd_mach_o_section_get_entry_size (abfd, sec);
3642 fprintf (file, " for section %s.%s:\n",
3643 sec->segname, sec->sectname);
3644 for (j = first; j < last; j++)
3646 unsigned int isym = dysymtab->indirect_syms[j];
3648 fprintf (file, " ");
3649 fprintf_vma (file, addr);
3650 fprintf (file, " %5u: 0x%08x", j, isym);
3651 if (isym & BFD_MACH_O_INDIRECT_SYMBOL_LOCAL)
3652 fprintf (file, " LOCAL");
3653 if (isym & BFD_MACH_O_INDIRECT_SYMBOL_ABS)
3654 fprintf (file, " ABSOLUTE");
3655 if (symtab && symtab->symbols
3656 && isym < symtab->nsyms
3657 && symtab->symbols[isym].symbol.name)
3658 fprintf (file, " %s", symtab->symbols[isym].symbol.name);
3659 fprintf (file, "\n");
3660 addr += entry_size;
3662 break;
3663 default:
3664 break;
3668 if (dysymtab->nextrefsyms > 0)
3670 bfd_mach_o_symtab_command *symtab = mdata->symtab;
3672 fprintf (file, " external reference table: (symbol flags)\n");
3673 for (i = 0; i < dysymtab->nextrefsyms; i++)
3675 bfd_mach_o_dylib_reference *ref = &dysymtab->ext_refs[i];
3677 fprintf (file, " %4u: %5lu 0x%02lx", i, ref->isym, ref->flags);
3678 if (symtab && symtab->symbols
3679 && ref->isym < symtab->nsyms
3680 && symtab->symbols[ref->isym].symbol.name)
3681 fprintf (file, " %s", symtab->symbols[ref->isym].symbol.name);
3682 fprintf (file, "\n");
3688 static void
3689 bfd_mach_o_print_dyld_info (bfd *abfd ATTRIBUTE_UNUSED,
3690 bfd_mach_o_load_command *cmd, FILE *file)
3692 bfd_mach_o_dyld_info_command *info = &cmd->command.dyld_info;
3694 fprintf (file, " rebase: off: 0x%08x size: %-8u\n",
3695 info->rebase_off, info->rebase_size);
3696 fprintf (file, " bind: off: 0x%08x size: %-8u\n",
3697 info->bind_off, info->bind_size);
3698 fprintf (file, " weak bind: off: 0x%08x size: %-8u\n",
3699 info->weak_bind_off, info->weak_bind_size);
3700 fprintf (file, " lazy bind: off: 0x%08x size: %-8u\n",
3701 info->lazy_bind_off, info->lazy_bind_size);
3702 fprintf (file, " export: off: 0x%08x size: %-8u\n",
3703 info->export_off, info->export_size);
3706 bfd_boolean
3707 bfd_mach_o_bfd_print_private_bfd_data (bfd *abfd, PTR ptr)
3709 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
3710 FILE *file = (FILE *) ptr;
3711 unsigned int i;
3713 bfd_mach_o_print_private_header (abfd, file);
3714 fputc ('\n', file);
3716 for (i = 0; i < mdata->header.ncmds; i++)
3718 bfd_mach_o_load_command *cmd = &mdata->commands[i];
3720 fprintf (file, "Load command %s:",
3721 bfd_mach_o_get_name (bfd_mach_o_load_command_name, cmd->type));
3722 switch (cmd->type)
3724 case BFD_MACH_O_LC_SEGMENT:
3725 case BFD_MACH_O_LC_SEGMENT_64:
3726 bfd_mach_o_print_segment (abfd, cmd, file);
3727 break;
3728 case BFD_MACH_O_LC_UUID:
3730 bfd_mach_o_uuid_command *uuid = &cmd->command.uuid;
3731 unsigned int i;
3733 for (i = 0; i < sizeof (uuid->uuid); i++)
3734 fprintf (file, " %02x", uuid->uuid[i]);
3735 fputc ('\n', file);
3737 break;
3738 case BFD_MACH_O_LC_LOAD_DYLIB:
3739 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB:
3740 case BFD_MACH_O_LC_REEXPORT_DYLIB:
3741 case BFD_MACH_O_LC_ID_DYLIB:
3743 bfd_mach_o_dylib_command *dylib = &cmd->command.dylib;
3744 fprintf (file, " %s\n", dylib->name_str);
3745 fprintf (file, " time stamp: 0x%08lx\n",
3746 dylib->timestamp);
3747 fprintf (file, " current version: 0x%08lx\n",
3748 dylib->current_version);
3749 fprintf (file, " comptibility version: 0x%08lx\n",
3750 dylib->compatibility_version);
3751 break;
3753 case BFD_MACH_O_LC_LOAD_DYLINKER:
3754 case BFD_MACH_O_LC_ID_DYLINKER:
3755 fprintf (file, " %s\n", cmd->command.dylinker.name_str);
3756 break;
3757 case BFD_MACH_O_LC_SYMTAB:
3759 bfd_mach_o_symtab_command *symtab = &cmd->command.symtab;
3760 fprintf (file,
3761 "\n"
3762 " symoff: 0x%08x nsyms: %8u (endoff: 0x%08x)\n",
3763 symtab->symoff, symtab->nsyms,
3764 symtab->symoff + symtab->nsyms
3765 * (mach_o_wide_p (&mdata->header)
3766 ? BFD_MACH_O_NLIST_64_SIZE : BFD_MACH_O_NLIST_SIZE));
3767 fprintf (file,
3768 " stroff: 0x%08x strsize: %8u (endoff: 0x%08x)\n",
3769 symtab->stroff, symtab->strsize,
3770 symtab->stroff + symtab->strsize);
3771 break;
3773 case BFD_MACH_O_LC_DYSYMTAB:
3774 fprintf (file, "\n");
3775 bfd_mach_o_print_dysymtab (abfd, cmd, file);
3776 break;
3777 case BFD_MACH_O_LC_CODE_SIGNATURE:
3778 case BFD_MACH_O_LC_SEGMENT_SPLIT_INFO:
3780 bfd_mach_o_linkedit_command *linkedit = &cmd->command.linkedit;
3781 fprintf
3782 (file, "\n"
3783 " dataoff: 0x%08lx datasize: 0x%08lx (endoff: 0x%08lx)\n",
3784 linkedit->dataoff, linkedit->datasize,
3785 linkedit->dataoff + linkedit->datasize);
3786 break;
3788 case BFD_MACH_O_LC_SUB_FRAMEWORK:
3789 case BFD_MACH_O_LC_SUB_UMBRELLA:
3790 case BFD_MACH_O_LC_SUB_LIBRARY:
3791 case BFD_MACH_O_LC_SUB_CLIENT:
3792 case BFD_MACH_O_LC_RPATH:
3794 bfd_mach_o_str_command *str = &cmd->command.str;
3795 fprintf (file, " %s\n", str->str);
3796 break;
3798 case BFD_MACH_O_LC_THREAD:
3799 case BFD_MACH_O_LC_UNIXTHREAD:
3801 bfd_mach_o_thread_command *thread = &cmd->command.thread;
3802 unsigned int j;
3803 bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
3805 fprintf (file, " nflavours: %lu\n", thread->nflavours);
3806 for (j = 0; j < thread->nflavours; j++)
3808 bfd_mach_o_thread_flavour *flavour = &thread->flavours[j];
3810 fprintf (file, " %2u: flavour: 0x%08lx offset: 0x%08lx"
3811 " size: 0x%08lx\n",
3812 j, flavour->flavour, flavour->offset,
3813 flavour->size);
3814 if (bed->_bfd_mach_o_print_thread)
3816 char *buf = bfd_malloc (flavour->size);
3818 if (buf
3819 && bfd_seek (abfd, flavour->offset, SEEK_SET) == 0
3820 && (bfd_bread (buf, flavour->size, abfd)
3821 == flavour->size))
3822 (*bed->_bfd_mach_o_print_thread)(abfd, flavour,
3823 file, buf);
3824 free (buf);
3827 break;
3829 case BFD_MACH_O_LC_DYLD_INFO:
3830 fprintf (file, "\n");
3831 bfd_mach_o_print_dyld_info (abfd, cmd, file);
3832 break;
3833 default:
3834 fprintf (file, "\n");
3835 break;
3837 fputc ('\n', file);
3840 bfd_mach_o_print_section_map (abfd, file);
3842 return TRUE;
3846 bfd_mach_o_core_fetch_environment (bfd *abfd,
3847 unsigned char **rbuf,
3848 unsigned int *rlen)
3850 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
3851 unsigned long stackaddr = bfd_mach_o_stack_addr (mdata->header.cputype);
3852 unsigned int i = 0;
3854 for (i = 0; i < mdata->header.ncmds; i++)
3856 bfd_mach_o_load_command *cur = &mdata->commands[i];
3857 bfd_mach_o_segment_command *seg = NULL;
3859 if (cur->type != BFD_MACH_O_LC_SEGMENT)
3860 continue;
3862 seg = &cur->command.segment;
3864 if ((seg->vmaddr + seg->vmsize) == stackaddr)
3866 unsigned long start = seg->fileoff;
3867 unsigned long end = seg->fileoff + seg->filesize;
3868 unsigned char *buf = bfd_malloc (1024);
3869 unsigned long size = 1024;
3871 for (;;)
3873 bfd_size_type nread = 0;
3874 unsigned long offset;
3875 int found_nonnull = 0;
3877 if (size > (end - start))
3878 size = (end - start);
3880 buf = bfd_realloc_or_free (buf, size);
3881 if (buf == NULL)
3882 return -1;
3884 if (bfd_seek (abfd, end - size, SEEK_SET) != 0)
3886 free (buf);
3887 return -1;
3890 nread = bfd_bread (buf, size, abfd);
3892 if (nread != size)
3894 free (buf);
3895 return -1;
3898 for (offset = 4; offset <= size; offset += 4)
3900 unsigned long val;
3902 val = *((unsigned long *) (buf + size - offset));
3903 if (! found_nonnull)
3905 if (val != 0)
3906 found_nonnull = 1;
3908 else if (val == 0x0)
3910 unsigned long bottom;
3911 unsigned long top;
3913 bottom = seg->fileoff + seg->filesize - offset;
3914 top = seg->fileoff + seg->filesize - 4;
3915 *rbuf = bfd_malloc (top - bottom);
3916 *rlen = top - bottom;
3918 memcpy (*rbuf, buf + size - *rlen, *rlen);
3919 free (buf);
3920 return 0;
3924 if (size == (end - start))
3925 break;
3927 size *= 2;
3930 free (buf);
3934 return -1;
3937 char *
3938 bfd_mach_o_core_file_failing_command (bfd *abfd)
3940 unsigned char *buf = NULL;
3941 unsigned int len = 0;
3942 int ret = -1;
3944 ret = bfd_mach_o_core_fetch_environment (abfd, &buf, &len);
3945 if (ret < 0)
3946 return NULL;
3948 return (char *) buf;
3952 bfd_mach_o_core_file_failing_signal (bfd *abfd ATTRIBUTE_UNUSED)
3954 return 0;
3957 #define bfd_mach_o_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
3958 #define bfd_mach_o_bfd_reloc_name_lookup _bfd_norelocs_bfd_reloc_name_lookup
3960 #define bfd_mach_o_swap_reloc_in NULL
3961 #define bfd_mach_o_swap_reloc_out NULL
3962 #define bfd_mach_o_print_thread NULL
3964 #define TARGET_NAME mach_o_be_vec
3965 #define TARGET_STRING "mach-o-be"
3966 #define TARGET_BIG_ENDIAN 1
3967 #define TARGET_ARCHIVE 0
3968 #include "mach-o-target.c"
3970 #undef TARGET_NAME
3971 #undef TARGET_STRING
3972 #undef TARGET_BIG_ENDIAN
3973 #undef TARGET_ARCHIVE
3975 #define TARGET_NAME mach_o_le_vec
3976 #define TARGET_STRING "mach-o-le"
3977 #define TARGET_BIG_ENDIAN 0
3978 #define TARGET_ARCHIVE 0
3980 #include "mach-o-target.c"
3982 #undef TARGET_NAME
3983 #undef TARGET_STRING
3984 #undef TARGET_BIG_ENDIAN
3985 #undef TARGET_ARCHIVE
3987 #define TARGET_NAME mach_o_fat_vec
3988 #define TARGET_STRING "mach-o-fat"
3989 #define TARGET_BIG_ENDIAN 1
3990 #define TARGET_ARCHIVE 1
3992 #include "mach-o-target.c"
3994 #undef TARGET_NAME
3995 #undef TARGET_STRING
3996 #undef TARGET_BIG_ENDIAN
3997 #undef TARGET_ARCHIVE