2009-12-04 Rafael Avila de Espindola <espindola@google.com>
[binutils.git] / bfd / mach-o.c
blob766fb3d76d971319d19fd0282ff10ba42bdca81d
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 || !strcmp (section->segname, "__DWARF"))
1486 /* Force flags for dwarf sections. This looks weird but dsym files
1487 have no flags for them and this is important for gdb. */
1488 flags = SEC_HAS_CONTENTS | SEC_DEBUGGING;
1490 else
1492 flags = SEC_ALLOC;
1493 if ((section->flags & BFD_MACH_O_SECTION_TYPE_MASK)
1494 != BFD_MACH_O_S_ZEROFILL)
1496 flags |= SEC_HAS_CONTENTS | SEC_LOAD;
1497 if (prot & BFD_MACH_O_PROT_EXECUTE)
1498 flags |= SEC_CODE;
1499 if (prot & BFD_MACH_O_PROT_WRITE)
1500 flags |= SEC_DATA;
1501 else if (prot & BFD_MACH_O_PROT_READ)
1502 flags |= SEC_READONLY;
1505 if (section->nreloc != 0)
1506 flags |= SEC_RELOC;
1508 bfdsec = bfd_make_section_anyway_with_flags (abfd, sname, flags);
1509 if (bfdsec == NULL)
1510 return NULL;
1512 bfdsec->vma = section->addr;
1513 bfdsec->lma = section->addr;
1514 bfdsec->size = section->size;
1515 bfdsec->filepos = section->offset;
1516 bfdsec->alignment_power = section->align;
1517 bfdsec->segment_mark = 0;
1518 bfdsec->reloc_count = section->nreloc;
1519 bfdsec->rel_filepos = section->reloff;
1521 return bfdsec;
1524 static int
1525 bfd_mach_o_read_section_32 (bfd *abfd,
1526 bfd_mach_o_section *section,
1527 unsigned int offset,
1528 unsigned long prot)
1530 unsigned char buf[BFD_MACH_O_SECTION_SIZE];
1532 if (bfd_seek (abfd, offset, SEEK_SET) != 0
1533 || (bfd_bread ((PTR) buf, BFD_MACH_O_SECTION_SIZE, abfd)
1534 != BFD_MACH_O_SECTION_SIZE))
1535 return -1;
1537 memcpy (section->sectname, buf, 16);
1538 section->sectname[16] = '\0';
1539 memcpy (section->segname, buf + 16, 16);
1540 section->segname[16] = '\0';
1541 section->addr = bfd_h_get_32 (abfd, buf + 32);
1542 section->size = bfd_h_get_32 (abfd, buf + 36);
1543 section->offset = bfd_h_get_32 (abfd, buf + 40);
1544 section->align = bfd_h_get_32 (abfd, buf + 44);
1545 section->reloff = bfd_h_get_32 (abfd, buf + 48);
1546 section->nreloc = bfd_h_get_32 (abfd, buf + 52);
1547 section->flags = bfd_h_get_32 (abfd, buf + 56);
1548 section->reserved1 = bfd_h_get_32 (abfd, buf + 60);
1549 section->reserved2 = bfd_h_get_32 (abfd, buf + 64);
1550 section->reserved3 = 0;
1551 section->bfdsection = bfd_mach_o_make_bfd_section (abfd, section, prot);
1553 if (section->bfdsection == NULL)
1554 return -1;
1556 return 0;
1559 static int
1560 bfd_mach_o_read_section_64 (bfd *abfd,
1561 bfd_mach_o_section *section,
1562 unsigned int offset,
1563 unsigned long prot)
1565 unsigned char buf[BFD_MACH_O_SECTION_64_SIZE];
1567 if (bfd_seek (abfd, offset, SEEK_SET) != 0
1568 || (bfd_bread ((PTR) buf, BFD_MACH_O_SECTION_64_SIZE, abfd)
1569 != BFD_MACH_O_SECTION_64_SIZE))
1570 return -1;
1572 memcpy (section->sectname, buf, 16);
1573 section->sectname[16] = '\0';
1574 memcpy (section->segname, buf + 16, 16);
1575 section->segname[16] = '\0';
1576 section->addr = bfd_h_get_64 (abfd, buf + 32);
1577 section->size = bfd_h_get_64 (abfd, buf + 40);
1578 section->offset = bfd_h_get_32 (abfd, buf + 48);
1579 section->align = bfd_h_get_32 (abfd, buf + 52);
1580 section->reloff = bfd_h_get_32 (abfd, buf + 56);
1581 section->nreloc = bfd_h_get_32 (abfd, buf + 60);
1582 section->flags = bfd_h_get_32 (abfd, buf + 64);
1583 section->reserved1 = bfd_h_get_32 (abfd, buf + 68);
1584 section->reserved2 = bfd_h_get_32 (abfd, buf + 72);
1585 section->reserved3 = bfd_h_get_32 (abfd, buf + 76);
1586 section->bfdsection = bfd_mach_o_make_bfd_section (abfd, section, prot);
1588 if (section->bfdsection == NULL)
1589 return -1;
1591 return 0;
1594 static int
1595 bfd_mach_o_read_section (bfd *abfd,
1596 bfd_mach_o_section *section,
1597 unsigned int offset,
1598 unsigned long prot,
1599 unsigned int wide)
1601 if (wide)
1602 return bfd_mach_o_read_section_64 (abfd, section, offset, prot);
1603 else
1604 return bfd_mach_o_read_section_32 (abfd, section, offset, prot);
1607 static int
1608 bfd_mach_o_read_symtab_symbol (bfd *abfd,
1609 bfd_mach_o_symtab_command *sym,
1610 bfd_mach_o_asymbol *s,
1611 unsigned long i)
1613 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
1614 unsigned int wide = mach_o_wide_p (&mdata->header);
1615 unsigned int symwidth =
1616 wide ? BFD_MACH_O_NLIST_64_SIZE : BFD_MACH_O_NLIST_SIZE;
1617 unsigned int symoff = sym->symoff + (i * symwidth);
1618 unsigned char buf[16];
1619 unsigned char type = -1;
1620 unsigned char section = -1;
1621 short desc = -1;
1622 symvalue value = -1;
1623 unsigned long stroff = -1;
1624 unsigned int symtype = -1;
1626 BFD_ASSERT (sym->strtab != NULL);
1628 if (bfd_seek (abfd, symoff, SEEK_SET) != 0
1629 || bfd_bread ((PTR) buf, symwidth, abfd) != symwidth)
1631 fprintf (stderr, "bfd_mach_o_read_symtab_symbol: unable to read %d bytes at %lu\n",
1632 symwidth, (unsigned long) symoff);
1633 return -1;
1636 stroff = bfd_h_get_32 (abfd, buf);
1637 type = bfd_h_get_8 (abfd, buf + 4);
1638 symtype = type & BFD_MACH_O_N_TYPE;
1639 section = bfd_h_get_8 (abfd, buf + 5);
1640 desc = bfd_h_get_16 (abfd, buf + 6);
1641 if (wide)
1642 value = bfd_h_get_64 (abfd, buf + 8);
1643 else
1644 value = bfd_h_get_32 (abfd, buf + 8);
1646 if (stroff >= sym->strsize)
1648 fprintf (stderr, "bfd_mach_o_read_symtab_symbol: symbol name out of range (%lu >= %lu)\n",
1649 (unsigned long) stroff, (unsigned long) sym->strsize);
1650 return -1;
1653 s->symbol.the_bfd = abfd;
1654 s->symbol.name = sym->strtab + stroff;
1655 s->symbol.value = value;
1656 s->symbol.flags = 0x0;
1657 s->symbol.udata.i = 0;
1658 s->n_type = type;
1659 s->n_sect = section;
1660 s->n_desc = desc;
1662 if (type & BFD_MACH_O_N_STAB)
1664 s->symbol.flags |= BSF_DEBUGGING;
1665 s->symbol.section = bfd_und_section_ptr;
1666 switch (type)
1668 case N_FUN:
1669 case N_STSYM:
1670 case N_LCSYM:
1671 case N_BNSYM:
1672 case N_SLINE:
1673 case N_ENSYM:
1674 case N_ECOMM:
1675 case N_ECOML:
1676 case N_GSYM:
1677 if ((section > 0) && (section <= mdata->nsects))
1679 s->symbol.section = mdata->sections[section - 1]->bfdsection;
1680 s->symbol.value =
1681 s->symbol.value - mdata->sections[section - 1]->addr;
1683 break;
1686 else
1688 if (type & BFD_MACH_O_N_PEXT)
1689 s->symbol.flags |= BSF_GLOBAL;
1691 if (type & BFD_MACH_O_N_EXT)
1692 s->symbol.flags |= BSF_GLOBAL;
1694 if (!(type & (BFD_MACH_O_N_PEXT | BFD_MACH_O_N_EXT)))
1695 s->symbol.flags |= BSF_LOCAL;
1697 switch (symtype)
1699 case BFD_MACH_O_N_UNDF:
1700 if (type == (BFD_MACH_O_N_UNDF | BFD_MACH_O_N_EXT)
1701 && s->symbol.value != 0)
1703 /* A common symbol. */
1704 s->symbol.section = bfd_com_section_ptr;
1705 s->symbol.flags = BSF_NO_FLAGS;
1707 else
1709 s->symbol.section = bfd_und_section_ptr;
1710 if (s->n_desc & BFD_MACH_O_N_WEAK_REF)
1711 s->symbol.flags |= BSF_WEAK;
1713 break;
1714 case BFD_MACH_O_N_PBUD:
1715 s->symbol.section = bfd_und_section_ptr;
1716 break;
1717 case BFD_MACH_O_N_ABS:
1718 s->symbol.section = bfd_abs_section_ptr;
1719 break;
1720 case BFD_MACH_O_N_SECT:
1721 if ((section > 0) && (section <= mdata->nsects))
1723 s->symbol.section = mdata->sections[section - 1]->bfdsection;
1724 s->symbol.value =
1725 s->symbol.value - mdata->sections[section - 1]->addr;
1727 else
1729 /* Mach-O uses 0 to mean "no section"; not an error. */
1730 if (section != 0)
1732 fprintf (stderr, "bfd_mach_o_read_symtab_symbol: "
1733 "symbol \"%s\" specified invalid section %d (max %lu): setting to undefined\n",
1734 s->symbol.name, section, mdata->nsects);
1736 s->symbol.section = bfd_und_section_ptr;
1738 break;
1739 case BFD_MACH_O_N_INDR:
1740 fprintf (stderr, "bfd_mach_o_read_symtab_symbol: "
1741 "symbol \"%s\" is unsupported 'indirect' reference: setting to undefined\n",
1742 s->symbol.name);
1743 s->symbol.section = bfd_und_section_ptr;
1744 break;
1745 default:
1746 fprintf (stderr, "bfd_mach_o_read_symtab_symbol: "
1747 "symbol \"%s\" specified invalid type field 0x%x: setting to undefined\n",
1748 s->symbol.name, symtype);
1749 s->symbol.section = bfd_und_section_ptr;
1750 break;
1754 return 0;
1757 static int
1758 bfd_mach_o_read_symtab_strtab (bfd *abfd)
1760 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
1761 bfd_mach_o_symtab_command *sym = mdata->symtab;
1763 /* Fail if there is no symtab. */
1764 if (sym == NULL)
1765 return -1;
1767 /* Success if already loaded. */
1768 if (sym->strtab)
1769 return 0;
1771 if (abfd->flags & BFD_IN_MEMORY)
1773 struct bfd_in_memory *b;
1775 b = (struct bfd_in_memory *) abfd->iostream;
1777 if ((sym->stroff + sym->strsize) > b->size)
1779 bfd_set_error (bfd_error_file_truncated);
1780 return -1;
1782 sym->strtab = (char *) b->buffer + sym->stroff;
1784 else
1786 sym->strtab = bfd_alloc (abfd, sym->strsize);
1787 if (sym->strtab == NULL)
1788 return -1;
1790 if (bfd_seek (abfd, sym->stroff, SEEK_SET) != 0
1791 || bfd_bread ((PTR) sym->strtab, sym->strsize, abfd) != sym->strsize)
1793 bfd_set_error (bfd_error_file_truncated);
1794 return -1;
1798 return 0;
1801 static int
1802 bfd_mach_o_read_symtab_symbols (bfd *abfd)
1804 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
1805 bfd_mach_o_symtab_command *sym = mdata->symtab;
1806 unsigned long i;
1807 int ret;
1809 if (sym->symbols)
1810 return 0;
1812 sym->symbols = bfd_alloc (abfd, sym->nsyms * sizeof (bfd_mach_o_asymbol));
1814 if (sym->symbols == NULL)
1816 fprintf (stderr, "bfd_mach_o_read_symtab_symbols: unable to allocate memory for symbols\n");
1817 return -1;
1820 ret = bfd_mach_o_read_symtab_strtab (abfd);
1821 if (ret != 0)
1822 return ret;
1824 for (i = 0; i < sym->nsyms; i++)
1826 ret = bfd_mach_o_read_symtab_symbol (abfd, sym, &sym->symbols[i], i);
1827 if (ret != 0)
1828 return ret;
1831 return 0;
1835 bfd_mach_o_read_dysymtab_symbol (bfd *abfd,
1836 bfd_mach_o_dysymtab_command *dysym,
1837 bfd_mach_o_symtab_command *sym,
1838 bfd_mach_o_asymbol *s,
1839 unsigned long i)
1841 unsigned long isymoff = dysym->indirectsymoff + (i * 4);
1842 unsigned long symindex;
1843 unsigned char buf[4];
1845 BFD_ASSERT (i < dysym->nindirectsyms);
1847 if (bfd_seek (abfd, isymoff, SEEK_SET) != 0
1848 || bfd_bread ((PTR) buf, 4, abfd) != 4)
1850 fprintf (stderr, "bfd_mach_o_read_dysymtab_symbol: unable to read %lu bytes at %lu\n",
1851 (unsigned long) 4, isymoff);
1852 return -1;
1854 symindex = bfd_h_get_32 (abfd, buf);
1856 return bfd_mach_o_read_symtab_symbol (abfd, sym, s, symindex);
1859 static const char *
1860 bfd_mach_o_i386_flavour_string (unsigned int flavour)
1862 switch ((int) flavour)
1864 case BFD_MACH_O_x86_THREAD_STATE32: return "x86_THREAD_STATE32";
1865 case BFD_MACH_O_x86_FLOAT_STATE32: return "x86_FLOAT_STATE32";
1866 case BFD_MACH_O_x86_EXCEPTION_STATE32: return "x86_EXCEPTION_STATE32";
1867 case BFD_MACH_O_x86_THREAD_STATE64: return "x86_THREAD_STATE64";
1868 case BFD_MACH_O_x86_FLOAT_STATE64: return "x86_FLOAT_STATE64";
1869 case BFD_MACH_O_x86_EXCEPTION_STATE64: return "x86_EXCEPTION_STATE64";
1870 case BFD_MACH_O_x86_THREAD_STATE: return "x86_THREAD_STATE";
1871 case BFD_MACH_O_x86_FLOAT_STATE: return "x86_FLOAT_STATE";
1872 case BFD_MACH_O_x86_EXCEPTION_STATE: return "x86_EXCEPTION_STATE";
1873 case BFD_MACH_O_x86_DEBUG_STATE32: return "x86_DEBUG_STATE32";
1874 case BFD_MACH_O_x86_DEBUG_STATE64: return "x86_DEBUG_STATE64";
1875 case BFD_MACH_O_x86_DEBUG_STATE: return "x86_DEBUG_STATE";
1876 case BFD_MACH_O_x86_THREAD_STATE_NONE: return "x86_THREAD_STATE_NONE";
1877 default: return "UNKNOWN";
1881 static const char *
1882 bfd_mach_o_ppc_flavour_string (unsigned int flavour)
1884 switch ((int) flavour)
1886 case BFD_MACH_O_PPC_THREAD_STATE: return "PPC_THREAD_STATE";
1887 case BFD_MACH_O_PPC_FLOAT_STATE: return "PPC_FLOAT_STATE";
1888 case BFD_MACH_O_PPC_EXCEPTION_STATE: return "PPC_EXCEPTION_STATE";
1889 case BFD_MACH_O_PPC_VECTOR_STATE: return "PPC_VECTOR_STATE";
1890 case BFD_MACH_O_PPC_THREAD_STATE64: return "PPC_THREAD_STATE64";
1891 case BFD_MACH_O_PPC_EXCEPTION_STATE64: return "PPC_EXCEPTION_STATE64";
1892 default: return "UNKNOWN";
1896 static int
1897 bfd_mach_o_read_dylinker (bfd *abfd, bfd_mach_o_load_command *command)
1899 bfd_mach_o_dylinker_command *cmd = &command->command.dylinker;
1900 unsigned char buf[4];
1901 unsigned int nameoff;
1903 BFD_ASSERT ((command->type == BFD_MACH_O_LC_ID_DYLINKER)
1904 || (command->type == BFD_MACH_O_LC_LOAD_DYLINKER));
1906 if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
1907 || bfd_bread ((PTR) buf, 4, abfd) != 4)
1908 return -1;
1910 nameoff = bfd_h_get_32 (abfd, buf + 0);
1912 cmd->name_offset = command->offset + nameoff;
1913 cmd->name_len = command->len - nameoff;
1914 cmd->name_str = bfd_alloc (abfd, cmd->name_len);
1915 if (cmd->name_str == NULL)
1916 return -1;
1917 if (bfd_seek (abfd, cmd->name_offset, SEEK_SET) != 0
1918 || bfd_bread (cmd->name_str, cmd->name_len, abfd) != cmd->name_len)
1919 return -1;
1920 return 0;
1923 static int
1924 bfd_mach_o_read_dylib (bfd *abfd, bfd_mach_o_load_command *command)
1926 bfd_mach_o_dylib_command *cmd = &command->command.dylib;
1927 unsigned char buf[16];
1928 unsigned int nameoff;
1930 switch (command->type)
1932 case BFD_MACH_O_LC_LOAD_DYLIB:
1933 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB:
1934 case BFD_MACH_O_LC_ID_DYLIB:
1935 case BFD_MACH_O_LC_REEXPORT_DYLIB:
1936 break;
1937 default:
1938 BFD_FAIL ();
1939 return -1;
1942 if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
1943 || bfd_bread ((PTR) buf, 16, abfd) != 16)
1944 return -1;
1946 nameoff = bfd_h_get_32 (abfd, buf + 0);
1947 cmd->timestamp = bfd_h_get_32 (abfd, buf + 4);
1948 cmd->current_version = bfd_h_get_32 (abfd, buf + 8);
1949 cmd->compatibility_version = bfd_h_get_32 (abfd, buf + 12);
1951 cmd->name_offset = command->offset + nameoff;
1952 cmd->name_len = command->len - nameoff;
1953 cmd->name_str = bfd_alloc (abfd, cmd->name_len);
1954 if (cmd->name_str == NULL)
1955 return -1;
1956 if (bfd_seek (abfd, cmd->name_offset, SEEK_SET) != 0
1957 || bfd_bread (cmd->name_str, cmd->name_len, abfd) != cmd->name_len)
1958 return -1;
1959 return 0;
1962 static int
1963 bfd_mach_o_read_prebound_dylib (bfd *abfd ATTRIBUTE_UNUSED,
1964 bfd_mach_o_load_command *command ATTRIBUTE_UNUSED)
1966 /* bfd_mach_o_prebound_dylib_command *cmd = &command->command.prebound_dylib; */
1968 BFD_ASSERT (command->type == BFD_MACH_O_LC_PREBOUND_DYLIB);
1969 return 0;
1972 static int
1973 bfd_mach_o_read_thread (bfd *abfd, bfd_mach_o_load_command *command)
1975 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
1976 bfd_mach_o_thread_command *cmd = &command->command.thread;
1977 unsigned char buf[8];
1978 unsigned int offset;
1979 unsigned int nflavours;
1980 unsigned int i;
1982 BFD_ASSERT ((command->type == BFD_MACH_O_LC_THREAD)
1983 || (command->type == BFD_MACH_O_LC_UNIXTHREAD));
1985 /* Count the number of threads. */
1986 offset = 8;
1987 nflavours = 0;
1988 while (offset != command->len)
1990 if (offset >= command->len)
1991 return -1;
1993 if (bfd_seek (abfd, command->offset + offset, SEEK_SET) != 0
1994 || bfd_bread ((PTR) buf, 8, abfd) != 8)
1995 return -1;
1997 offset += 8 + bfd_h_get_32 (abfd, buf + 4) * 4;
1998 nflavours++;
2001 /* Allocate threads. */
2002 cmd->flavours = bfd_alloc
2003 (abfd, nflavours * sizeof (bfd_mach_o_thread_flavour));
2004 if (cmd->flavours == NULL)
2005 return -1;
2006 cmd->nflavours = nflavours;
2008 offset = 8;
2009 nflavours = 0;
2010 while (offset != command->len)
2012 if (offset >= command->len)
2013 return -1;
2015 if (nflavours >= cmd->nflavours)
2016 return -1;
2018 if (bfd_seek (abfd, command->offset + offset, SEEK_SET) != 0
2019 || bfd_bread ((PTR) buf, 8, abfd) != 8)
2020 return -1;
2022 cmd->flavours[nflavours].flavour = bfd_h_get_32 (abfd, buf);
2023 cmd->flavours[nflavours].offset = command->offset + offset + 8;
2024 cmd->flavours[nflavours].size = bfd_h_get_32 (abfd, buf + 4) * 4;
2025 offset += cmd->flavours[nflavours].size + 8;
2026 nflavours++;
2029 for (i = 0; i < nflavours; i++)
2031 asection *bfdsec;
2032 unsigned int snamelen;
2033 char *sname;
2034 const char *flavourstr;
2035 const char *prefix = "LC_THREAD";
2036 unsigned int j = 0;
2038 switch (mdata->header.cputype)
2040 case BFD_MACH_O_CPU_TYPE_POWERPC:
2041 case BFD_MACH_O_CPU_TYPE_POWERPC_64:
2042 flavourstr = bfd_mach_o_ppc_flavour_string (cmd->flavours[i].flavour);
2043 break;
2044 case BFD_MACH_O_CPU_TYPE_I386:
2045 case BFD_MACH_O_CPU_TYPE_X86_64:
2046 flavourstr = bfd_mach_o_i386_flavour_string (cmd->flavours[i].flavour);
2047 break;
2048 default:
2049 flavourstr = "UNKNOWN_ARCHITECTURE";
2050 break;
2053 snamelen = strlen (prefix) + 1 + 20 + 1 + strlen (flavourstr) + 1;
2054 sname = bfd_alloc (abfd, snamelen);
2055 if (sname == NULL)
2056 return -1;
2058 for (;;)
2060 sprintf (sname, "%s.%s.%u", prefix, flavourstr, j);
2061 if (bfd_get_section_by_name (abfd, sname) == NULL)
2062 break;
2063 j++;
2066 bfdsec = bfd_make_section_with_flags (abfd, sname, SEC_HAS_CONTENTS);
2068 bfdsec->vma = 0;
2069 bfdsec->lma = 0;
2070 bfdsec->size = cmd->flavours[i].size;
2071 bfdsec->filepos = cmd->flavours[i].offset;
2072 bfdsec->alignment_power = 0x0;
2074 cmd->section = bfdsec;
2077 return 0;
2080 static int
2081 bfd_mach_o_read_dysymtab (bfd *abfd, bfd_mach_o_load_command *command)
2083 bfd_mach_o_dysymtab_command *cmd = &command->command.dysymtab;
2084 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
2085 unsigned char buf[72];
2087 BFD_ASSERT (command->type == BFD_MACH_O_LC_DYSYMTAB);
2089 if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
2090 || bfd_bread ((PTR) buf, 72, abfd) != 72)
2091 return -1;
2093 cmd->ilocalsym = bfd_h_get_32 (abfd, buf + 0);
2094 cmd->nlocalsym = bfd_h_get_32 (abfd, buf + 4);
2095 cmd->iextdefsym = bfd_h_get_32 (abfd, buf + 8);
2096 cmd->nextdefsym = bfd_h_get_32 (abfd, buf + 12);
2097 cmd->iundefsym = bfd_h_get_32 (abfd, buf + 16);
2098 cmd->nundefsym = bfd_h_get_32 (abfd, buf + 20);
2099 cmd->tocoff = bfd_h_get_32 (abfd, buf + 24);
2100 cmd->ntoc = bfd_h_get_32 (abfd, buf + 28);
2101 cmd->modtaboff = bfd_h_get_32 (abfd, buf + 32);
2102 cmd->nmodtab = bfd_h_get_32 (abfd, buf + 36);
2103 cmd->extrefsymoff = bfd_h_get_32 (abfd, buf + 40);
2104 cmd->nextrefsyms = bfd_h_get_32 (abfd, buf + 44);
2105 cmd->indirectsymoff = bfd_h_get_32 (abfd, buf + 48);
2106 cmd->nindirectsyms = bfd_h_get_32 (abfd, buf + 52);
2107 cmd->extreloff = bfd_h_get_32 (abfd, buf + 56);
2108 cmd->nextrel = bfd_h_get_32 (abfd, buf + 60);
2109 cmd->locreloff = bfd_h_get_32 (abfd, buf + 64);
2110 cmd->nlocrel = bfd_h_get_32 (abfd, buf + 68);
2112 if (cmd->nmodtab != 0)
2114 char buf[56];
2115 unsigned int i;
2116 int wide = bfd_mach_o_wide_p (abfd);
2117 unsigned int module_len = wide ? 56 : 52;
2119 cmd->dylib_module =
2120 bfd_alloc (abfd, cmd->nmodtab * sizeof (bfd_mach_o_dylib_module));
2121 if (cmd->dylib_module == NULL)
2122 return -1;
2124 if (bfd_seek (abfd, cmd->modtaboff, SEEK_SET) != 0)
2125 return -1;
2127 for (i = 0; i < cmd->nmodtab; i++)
2129 bfd_mach_o_dylib_module *module = &cmd->dylib_module[i];
2130 unsigned long v;
2132 if (bfd_bread ((PTR) buf, module_len, abfd) != module_len)
2133 return -1;
2135 module->module_name_idx = bfd_h_get_32 (abfd, buf + 0);
2136 module->iextdefsym = bfd_h_get_32 (abfd, buf + 4);
2137 module->nextdefsym = bfd_h_get_32 (abfd, buf + 8);
2138 module->irefsym = bfd_h_get_32 (abfd, buf + 12);
2139 module->nrefsym = bfd_h_get_32 (abfd, buf + 16);
2140 module->ilocalsym = bfd_h_get_32 (abfd, buf + 20);
2141 module->nlocalsym = bfd_h_get_32 (abfd, buf + 24);
2142 module->iextrel = bfd_h_get_32 (abfd, buf + 28);
2143 module->nextrel = bfd_h_get_32 (abfd, buf + 32);
2144 v = bfd_h_get_32 (abfd, buf +36);
2145 module->iinit = v & 0xffff;
2146 module->iterm = (v >> 16) & 0xffff;
2147 v = bfd_h_get_32 (abfd, buf + 40);
2148 module->ninit = v & 0xffff;
2149 module->nterm = (v >> 16) & 0xffff;
2150 if (wide)
2152 module->objc_module_info_size = bfd_h_get_32 (abfd, buf + 44);
2153 module->objc_module_info_addr = bfd_h_get_64 (abfd, buf + 48);
2155 else
2157 module->objc_module_info_addr = bfd_h_get_32 (abfd, buf + 44);
2158 module->objc_module_info_size = bfd_h_get_32 (abfd, buf + 48);
2163 if (cmd->ntoc != 0)
2165 char buf[8];
2166 unsigned int i;
2168 cmd->dylib_toc = bfd_alloc
2169 (abfd, cmd->ntoc * sizeof (bfd_mach_o_dylib_table_of_content));
2170 if (cmd->dylib_toc == NULL)
2171 return -1;
2173 if (bfd_seek (abfd, cmd->tocoff, SEEK_SET) != 0)
2174 return -1;
2176 for (i = 0; i < cmd->ntoc; i++)
2178 bfd_mach_o_dylib_table_of_content *toc = &cmd->dylib_toc[i];
2180 if (bfd_bread ((PTR) buf, 8, abfd) != 8)
2181 return -1;
2183 toc->symbol_index = bfd_h_get_32 (abfd, buf + 0);
2184 toc->module_index = bfd_h_get_32 (abfd, buf + 4);
2188 if (cmd->nindirectsyms != 0)
2190 char buf[4];
2191 unsigned int i;
2193 cmd->indirect_syms = bfd_alloc
2194 (abfd, cmd->nindirectsyms * sizeof (unsigned int));
2195 if (cmd->indirect_syms == NULL)
2196 return -1;
2198 if (bfd_seek (abfd, cmd->indirectsymoff, SEEK_SET) != 0)
2199 return -1;
2201 for (i = 0; i < cmd->nindirectsyms; i++)
2203 unsigned int *is = &cmd->indirect_syms[i];
2205 if (bfd_bread ((PTR) buf, 4, abfd) != 4)
2206 return -1;
2208 *is = bfd_h_get_32 (abfd, buf + 0);
2212 if (cmd->nextrefsyms != 0)
2214 char buf[4];
2215 unsigned long v;
2216 unsigned int i;
2218 cmd->ext_refs = bfd_alloc
2219 (abfd, cmd->nextrefsyms * sizeof (bfd_mach_o_dylib_reference));
2220 if (cmd->ext_refs == NULL)
2221 return -1;
2223 if (bfd_seek (abfd, cmd->extrefsymoff, SEEK_SET) != 0)
2224 return -1;
2226 for (i = 0; i < cmd->nextrefsyms; i++)
2228 bfd_mach_o_dylib_reference *ref = &cmd->ext_refs[i];
2230 if (bfd_bread ((PTR) buf, 4, abfd) != 4)
2231 return -1;
2233 /* Fields isym and flags are written as bit-fields, thus we need
2234 a specific processing for endianness. */
2235 v = bfd_h_get_32 (abfd, buf + 0);
2236 if (bfd_big_endian (abfd))
2238 ref->isym = (v >> 8) & 0xffffff;
2239 ref->flags = v & 0xff;
2241 else
2243 ref->isym = v & 0xffffff;
2244 ref->flags = (v >> 24) & 0xff;
2249 if (mdata->dysymtab)
2250 return -1;
2251 mdata->dysymtab = cmd;
2253 return 0;
2256 static int
2257 bfd_mach_o_read_symtab (bfd *abfd, bfd_mach_o_load_command *command)
2259 bfd_mach_o_symtab_command *symtab = &command->command.symtab;
2260 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
2261 unsigned char buf[16];
2263 BFD_ASSERT (command->type == BFD_MACH_O_LC_SYMTAB);
2265 if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
2266 || bfd_bread ((PTR) buf, 16, abfd) != 16)
2267 return -1;
2269 symtab->symoff = bfd_h_get_32 (abfd, buf);
2270 symtab->nsyms = bfd_h_get_32 (abfd, buf + 4);
2271 symtab->stroff = bfd_h_get_32 (abfd, buf + 8);
2272 symtab->strsize = bfd_h_get_32 (abfd, buf + 12);
2273 symtab->symbols = NULL;
2274 symtab->strtab = NULL;
2276 if (symtab->nsyms != 0)
2277 abfd->flags |= HAS_SYMS;
2279 if (mdata->symtab)
2280 return -1;
2281 mdata->symtab = symtab;
2282 return 0;
2285 static int
2286 bfd_mach_o_read_uuid (bfd *abfd, bfd_mach_o_load_command *command)
2288 bfd_mach_o_uuid_command *cmd = &command->command.uuid;
2289 asection *bfdsec;
2290 char *sname;
2291 static const char prefix[] = "LC_UUID";
2293 BFD_ASSERT (command->type == BFD_MACH_O_LC_UUID);
2295 if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
2296 || bfd_bread ((PTR) cmd->uuid, 16, abfd) != 16)
2297 return -1;
2299 sname = bfd_alloc (abfd, strlen (prefix) + 1);
2300 if (sname == NULL)
2301 return -1;
2302 strcpy (sname, prefix);
2304 bfdsec = bfd_make_section_anyway_with_flags (abfd, sname, SEC_HAS_CONTENTS);
2305 if (bfdsec == NULL)
2306 return -1;
2308 bfdsec->vma = 0;
2309 bfdsec->lma = 0;
2310 bfdsec->size = command->len - 8;
2311 bfdsec->filepos = command->offset + 8;
2312 bfdsec->alignment_power = 0;
2314 cmd->section = bfdsec;
2316 return 0;
2319 static int
2320 bfd_mach_o_read_linkedit (bfd *abfd, bfd_mach_o_load_command *command)
2322 bfd_mach_o_linkedit_command *cmd = &command->command.linkedit;
2323 char buf[8];
2325 if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
2326 || bfd_bread ((PTR) buf, 8, abfd) != 8)
2327 return -1;
2329 cmd->dataoff = bfd_get_32 (abfd, buf + 0);
2330 cmd->datasize = bfd_get_32 (abfd, buf + 4);
2331 return 0;
2334 static int
2335 bfd_mach_o_read_str (bfd *abfd, bfd_mach_o_load_command *command)
2337 bfd_mach_o_str_command *cmd = &command->command.str;
2338 char buf[4];
2339 unsigned long off;
2341 if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
2342 || bfd_bread ((PTR) buf, 4, abfd) != 4)
2343 return -1;
2345 off = bfd_get_32 (abfd, buf + 0);
2346 cmd->stroff = command->offset + off;
2347 cmd->str_len = command->len - off;
2348 cmd->str = bfd_alloc (abfd, cmd->str_len);
2349 if (cmd->str == NULL)
2350 return -1;
2351 if (bfd_seek (abfd, cmd->stroff, SEEK_SET) != 0
2352 || bfd_bread ((PTR) cmd->str, cmd->str_len, abfd) != cmd->str_len)
2353 return -1;
2354 return 0;
2357 static int
2358 bfd_mach_o_read_dyld_info (bfd *abfd, bfd_mach_o_load_command *command)
2360 bfd_mach_o_dyld_info_command *cmd = &command->command.dyld_info;
2361 char buf[40];
2363 if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
2364 || bfd_bread ((PTR) buf, sizeof (buf), abfd) != sizeof (buf))
2365 return -1;
2367 cmd->rebase_off = bfd_get_32 (abfd, buf + 0);
2368 cmd->rebase_size = bfd_get_32 (abfd, buf + 4);
2369 cmd->bind_off = bfd_get_32 (abfd, buf + 8);
2370 cmd->bind_size = bfd_get_32 (abfd, buf + 12);
2371 cmd->weak_bind_off = bfd_get_32 (abfd, buf + 16);
2372 cmd->weak_bind_size = bfd_get_32 (abfd, buf + 20);
2373 cmd->lazy_bind_off = bfd_get_32 (abfd, buf + 24);
2374 cmd->lazy_bind_size = bfd_get_32 (abfd, buf + 28);
2375 cmd->export_off = bfd_get_32 (abfd, buf + 32);
2376 cmd->export_size = bfd_get_32 (abfd, buf + 36);
2377 return 0;
2380 static int
2381 bfd_mach_o_read_segment (bfd *abfd,
2382 bfd_mach_o_load_command *command,
2383 unsigned int wide)
2385 unsigned char buf[64];
2386 bfd_mach_o_segment_command *seg = &command->command.segment;
2387 unsigned long i;
2389 if (wide)
2391 BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT_64);
2393 if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
2394 || bfd_bread ((PTR) buf, 64, abfd) != 64)
2395 return -1;
2397 memcpy (seg->segname, buf, 16);
2398 seg->segname[16] = '\0';
2400 seg->vmaddr = bfd_h_get_64 (abfd, buf + 16);
2401 seg->vmsize = bfd_h_get_64 (abfd, buf + 24);
2402 seg->fileoff = bfd_h_get_64 (abfd, buf + 32);
2403 seg->filesize = bfd_h_get_64 (abfd, buf + 40);
2404 seg->maxprot = bfd_h_get_32 (abfd, buf + 48);
2405 seg->initprot = bfd_h_get_32 (abfd, buf + 52);
2406 seg->nsects = bfd_h_get_32 (abfd, buf + 56);
2407 seg->flags = bfd_h_get_32 (abfd, buf + 60);
2409 else
2411 BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT);
2413 if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
2414 || bfd_bread ((PTR) buf, 48, abfd) != 48)
2415 return -1;
2417 memcpy (seg->segname, buf, 16);
2418 seg->segname[16] = '\0';
2420 seg->vmaddr = bfd_h_get_32 (abfd, buf + 16);
2421 seg->vmsize = bfd_h_get_32 (abfd, buf + 20);
2422 seg->fileoff = bfd_h_get_32 (abfd, buf + 24);
2423 seg->filesize = bfd_h_get_32 (abfd, buf + 28);
2424 seg->maxprot = bfd_h_get_32 (abfd, buf + 32);
2425 seg->initprot = bfd_h_get_32 (abfd, buf + 36);
2426 seg->nsects = bfd_h_get_32 (abfd, buf + 40);
2427 seg->flags = bfd_h_get_32 (abfd, buf + 44);
2430 if (seg->nsects != 0)
2432 seg->sections = bfd_alloc (abfd, seg->nsects
2433 * sizeof (bfd_mach_o_section));
2434 if (seg->sections == NULL)
2435 return -1;
2437 for (i = 0; i < seg->nsects; i++)
2439 bfd_vma segoff;
2440 if (wide)
2441 segoff = command->offset + BFD_MACH_O_LC_SEGMENT_64_SIZE
2442 + (i * BFD_MACH_O_SECTION_64_SIZE);
2443 else
2444 segoff = command->offset + BFD_MACH_O_LC_SEGMENT_SIZE
2445 + (i * BFD_MACH_O_SECTION_SIZE);
2447 if (bfd_mach_o_read_section
2448 (abfd, &seg->sections[i], segoff, seg->initprot, wide) != 0)
2449 return -1;
2453 return 0;
2456 static int
2457 bfd_mach_o_read_segment_32 (bfd *abfd, bfd_mach_o_load_command *command)
2459 return bfd_mach_o_read_segment (abfd, command, 0);
2462 static int
2463 bfd_mach_o_read_segment_64 (bfd *abfd, bfd_mach_o_load_command *command)
2465 return bfd_mach_o_read_segment (abfd, command, 1);
2468 static int
2469 bfd_mach_o_read_command (bfd *abfd, bfd_mach_o_load_command *command)
2471 unsigned char buf[8];
2473 /* Read command type and length. */
2474 if (bfd_seek (abfd, command->offset, SEEK_SET) != 0
2475 || bfd_bread ((PTR) buf, 8, abfd) != 8)
2476 return -1;
2478 command->type = bfd_h_get_32 (abfd, buf) & ~BFD_MACH_O_LC_REQ_DYLD;
2479 command->type_required = (bfd_h_get_32 (abfd, buf) & BFD_MACH_O_LC_REQ_DYLD
2480 ? TRUE : FALSE);
2481 command->len = bfd_h_get_32 (abfd, buf + 4);
2483 switch (command->type)
2485 case BFD_MACH_O_LC_SEGMENT:
2486 if (bfd_mach_o_read_segment_32 (abfd, command) != 0)
2487 return -1;
2488 break;
2489 case BFD_MACH_O_LC_SEGMENT_64:
2490 if (bfd_mach_o_read_segment_64 (abfd, command) != 0)
2491 return -1;
2492 break;
2493 case BFD_MACH_O_LC_SYMTAB:
2494 if (bfd_mach_o_read_symtab (abfd, command) != 0)
2495 return -1;
2496 break;
2497 case BFD_MACH_O_LC_SYMSEG:
2498 break;
2499 case BFD_MACH_O_LC_THREAD:
2500 case BFD_MACH_O_LC_UNIXTHREAD:
2501 if (bfd_mach_o_read_thread (abfd, command) != 0)
2502 return -1;
2503 break;
2504 case BFD_MACH_O_LC_LOAD_DYLINKER:
2505 case BFD_MACH_O_LC_ID_DYLINKER:
2506 if (bfd_mach_o_read_dylinker (abfd, command) != 0)
2507 return -1;
2508 break;
2509 case BFD_MACH_O_LC_LOAD_DYLIB:
2510 case BFD_MACH_O_LC_ID_DYLIB:
2511 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB:
2512 case BFD_MACH_O_LC_REEXPORT_DYLIB:
2513 if (bfd_mach_o_read_dylib (abfd, command) != 0)
2514 return -1;
2515 break;
2516 case BFD_MACH_O_LC_PREBOUND_DYLIB:
2517 if (bfd_mach_o_read_prebound_dylib (abfd, command) != 0)
2518 return -1;
2519 break;
2520 case BFD_MACH_O_LC_LOADFVMLIB:
2521 case BFD_MACH_O_LC_IDFVMLIB:
2522 case BFD_MACH_O_LC_IDENT:
2523 case BFD_MACH_O_LC_FVMFILE:
2524 case BFD_MACH_O_LC_PREPAGE:
2525 case BFD_MACH_O_LC_ROUTINES:
2526 break;
2527 case BFD_MACH_O_LC_SUB_FRAMEWORK:
2528 case BFD_MACH_O_LC_SUB_UMBRELLA:
2529 case BFD_MACH_O_LC_SUB_LIBRARY:
2530 case BFD_MACH_O_LC_SUB_CLIENT:
2531 case BFD_MACH_O_LC_RPATH:
2532 if (bfd_mach_o_read_str (abfd, command) != 0)
2533 return -1;
2534 break;
2535 case BFD_MACH_O_LC_DYSYMTAB:
2536 if (bfd_mach_o_read_dysymtab (abfd, command) != 0)
2537 return -1;
2538 break;
2539 case BFD_MACH_O_LC_TWOLEVEL_HINTS:
2540 case BFD_MACH_O_LC_PREBIND_CKSUM:
2541 break;
2542 case BFD_MACH_O_LC_UUID:
2543 if (bfd_mach_o_read_uuid (abfd, command) != 0)
2544 return -1;
2545 break;
2546 case BFD_MACH_O_LC_CODE_SIGNATURE:
2547 case BFD_MACH_O_LC_SEGMENT_SPLIT_INFO:
2548 if (bfd_mach_o_read_linkedit (abfd, command) != 0)
2549 return -1;
2550 break;
2551 case BFD_MACH_O_LC_DYLD_INFO:
2552 if (bfd_mach_o_read_dyld_info (abfd, command) != 0)
2553 return -1;
2554 break;
2555 default:
2556 fprintf (stderr, "unable to read unknown load command 0x%lx\n",
2557 (unsigned long) command->type);
2558 break;
2561 return 0;
2564 static void
2565 bfd_mach_o_flatten_sections (bfd *abfd)
2567 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
2568 long csect = 0;
2569 unsigned long i, j;
2571 /* Count total number of sections. */
2572 mdata->nsects = 0;
2574 for (i = 0; i < mdata->header.ncmds; i++)
2576 if (mdata->commands[i].type == BFD_MACH_O_LC_SEGMENT
2577 || mdata->commands[i].type == BFD_MACH_O_LC_SEGMENT_64)
2579 bfd_mach_o_segment_command *seg;
2581 seg = &mdata->commands[i].command.segment;
2582 mdata->nsects += seg->nsects;
2586 /* Allocate sections array. */
2587 mdata->sections = bfd_alloc (abfd,
2588 mdata->nsects * sizeof (bfd_mach_o_section *));
2590 /* Fill the array. */
2591 csect = 0;
2593 for (i = 0; i < mdata->header.ncmds; i++)
2595 if (mdata->commands[i].type == BFD_MACH_O_LC_SEGMENT
2596 || mdata->commands[i].type == BFD_MACH_O_LC_SEGMENT_64)
2598 bfd_mach_o_segment_command *seg;
2600 seg = &mdata->commands[i].command.segment;
2601 BFD_ASSERT (csect + seg->nsects <= mdata->nsects);
2603 for (j = 0; j < seg->nsects; j++)
2604 mdata->sections[csect++] = &seg->sections[j];
2610 bfd_mach_o_scan_start_address (bfd *abfd)
2612 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
2613 bfd_mach_o_thread_command *cmd = NULL;
2614 unsigned long i;
2616 for (i = 0; i < mdata->header.ncmds; i++)
2618 if ((mdata->commands[i].type == BFD_MACH_O_LC_THREAD) ||
2619 (mdata->commands[i].type == BFD_MACH_O_LC_UNIXTHREAD))
2621 if (cmd == NULL)
2622 cmd = &mdata->commands[i].command.thread;
2623 else
2624 return 0;
2628 if (cmd == NULL)
2629 return 0;
2631 for (i = 0; i < cmd->nflavours; i++)
2633 if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_I386)
2634 && (cmd->flavours[i].flavour
2635 == (unsigned long) BFD_MACH_O_x86_THREAD_STATE32))
2637 unsigned char buf[4];
2639 if (bfd_seek (abfd, cmd->flavours[i].offset + 40, SEEK_SET) != 0
2640 || bfd_bread (buf, 4, abfd) != 4)
2641 return -1;
2643 abfd->start_address = bfd_h_get_32 (abfd, buf);
2645 else if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_POWERPC)
2646 && (cmd->flavours[i].flavour == BFD_MACH_O_PPC_THREAD_STATE))
2648 unsigned char buf[4];
2650 if (bfd_seek (abfd, cmd->flavours[i].offset + 0, SEEK_SET) != 0
2651 || bfd_bread (buf, 4, abfd) != 4)
2652 return -1;
2654 abfd->start_address = bfd_h_get_32 (abfd, buf);
2656 else if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_POWERPC_64)
2657 && (cmd->flavours[i].flavour == BFD_MACH_O_PPC_THREAD_STATE64))
2659 unsigned char buf[8];
2661 if (bfd_seek (abfd, cmd->flavours[i].offset + 0, SEEK_SET) != 0
2662 || bfd_bread (buf, 8, abfd) != 8)
2663 return -1;
2665 abfd->start_address = bfd_h_get_64 (abfd, buf);
2667 else if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_X86_64)
2668 && (cmd->flavours[i].flavour == BFD_MACH_O_x86_THREAD_STATE64))
2670 unsigned char buf[8];
2672 if (bfd_seek (abfd, cmd->flavours[i].offset + (16 * 8), SEEK_SET) != 0
2673 || bfd_bread (buf, 8, abfd) != 8)
2674 return -1;
2676 abfd->start_address = bfd_h_get_64 (abfd, buf);
2680 return 0;
2684 bfd_mach_o_scan (bfd *abfd,
2685 bfd_mach_o_header *header,
2686 bfd_mach_o_data_struct *mdata)
2688 unsigned int i;
2689 enum bfd_architecture cputype;
2690 unsigned long cpusubtype;
2691 unsigned int hdrsize;
2693 hdrsize = mach_o_wide_p (header) ?
2694 BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE;
2696 mdata->header = *header;
2698 abfd->flags = abfd->flags & BFD_IN_MEMORY;
2699 switch (header->filetype)
2701 case BFD_MACH_O_MH_OBJECT:
2702 abfd->flags |= HAS_RELOC;
2703 break;
2704 case BFD_MACH_O_MH_EXECUTE:
2705 abfd->flags |= EXEC_P;
2706 break;
2707 case BFD_MACH_O_MH_DYLIB:
2708 case BFD_MACH_O_MH_BUNDLE:
2709 abfd->flags |= DYNAMIC;
2710 break;
2713 abfd->tdata.mach_o_data = mdata;
2715 bfd_mach_o_convert_architecture (header->cputype, header->cpusubtype,
2716 &cputype, &cpusubtype);
2717 if (cputype == bfd_arch_unknown)
2719 fprintf (stderr, "bfd_mach_o_scan: unknown architecture 0x%lx/0x%lx\n",
2720 header->cputype, header->cpusubtype);
2721 return -1;
2724 bfd_set_arch_mach (abfd, cputype, cpusubtype);
2726 if (header->ncmds != 0)
2728 mdata->commands = bfd_alloc
2729 (abfd, header->ncmds * sizeof (bfd_mach_o_load_command));
2730 if (mdata->commands == NULL)
2731 return -1;
2733 for (i = 0; i < header->ncmds; i++)
2735 bfd_mach_o_load_command *cur = &mdata->commands[i];
2737 if (i == 0)
2738 cur->offset = hdrsize;
2739 else
2741 bfd_mach_o_load_command *prev = &mdata->commands[i - 1];
2742 cur->offset = prev->offset + prev->len;
2745 if (bfd_mach_o_read_command (abfd, cur) < 0)
2746 return -1;
2750 if (bfd_mach_o_scan_start_address (abfd) < 0)
2751 return -1;
2753 bfd_mach_o_flatten_sections (abfd);
2754 return 0;
2757 bfd_boolean
2758 bfd_mach_o_mkobject_init (bfd *abfd)
2760 bfd_mach_o_data_struct *mdata = NULL;
2762 mdata = bfd_alloc (abfd, sizeof (bfd_mach_o_data_struct));
2763 if (mdata == NULL)
2764 return FALSE;
2765 abfd->tdata.mach_o_data = mdata;
2767 mdata->header.magic = 0;
2768 mdata->header.cputype = 0;
2769 mdata->header.cpusubtype = 0;
2770 mdata->header.filetype = 0;
2771 mdata->header.ncmds = 0;
2772 mdata->header.sizeofcmds = 0;
2773 mdata->header.flags = 0;
2774 mdata->header.byteorder = BFD_ENDIAN_UNKNOWN;
2775 mdata->commands = NULL;
2776 mdata->nsects = 0;
2777 mdata->sections = NULL;
2779 return TRUE;
2782 const bfd_target *
2783 bfd_mach_o_header_p (bfd *abfd,
2784 bfd_mach_o_filetype filetype,
2785 bfd_mach_o_cpu_type cputype)
2787 struct bfd_preserve preserve;
2788 bfd_mach_o_header header;
2790 preserve.marker = NULL;
2791 if (!bfd_mach_o_read_header (abfd, &header))
2792 goto wrong;
2794 if (! (header.byteorder == BFD_ENDIAN_BIG
2795 || header.byteorder == BFD_ENDIAN_LITTLE))
2797 fprintf (stderr, "unknown header byte-order value 0x%lx\n",
2798 (unsigned long) header.byteorder);
2799 goto wrong;
2802 if (! ((header.byteorder == BFD_ENDIAN_BIG
2803 && abfd->xvec->byteorder == BFD_ENDIAN_BIG
2804 && abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
2805 || (header.byteorder == BFD_ENDIAN_LITTLE
2806 && abfd->xvec->byteorder == BFD_ENDIAN_LITTLE
2807 && abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)))
2808 goto wrong;
2810 /* Check cputype and filetype.
2811 In case of wildcard, do not accept magics that are handled by existing
2812 targets. */
2813 if (cputype)
2815 if (header.cputype != cputype)
2816 goto wrong;
2818 else
2820 switch (header.cputype)
2822 case BFD_MACH_O_CPU_TYPE_I386:
2823 /* Handled by mach-o-i386 */
2824 goto wrong;
2825 default:
2826 break;
2829 if (filetype)
2831 if (header.filetype != filetype)
2832 goto wrong;
2834 else
2836 switch (header.filetype)
2838 case BFD_MACH_O_MH_CORE:
2839 /* Handled by core_p */
2840 goto wrong;
2841 default:
2842 break;
2846 preserve.marker = bfd_zalloc (abfd, sizeof (bfd_mach_o_data_struct));
2847 if (preserve.marker == NULL
2848 || !bfd_preserve_save (abfd, &preserve))
2849 goto fail;
2851 if (bfd_mach_o_scan (abfd, &header,
2852 (bfd_mach_o_data_struct *) preserve.marker) != 0)
2853 goto wrong;
2855 bfd_preserve_finish (abfd, &preserve);
2856 return abfd->xvec;
2858 wrong:
2859 bfd_set_error (bfd_error_wrong_format);
2861 fail:
2862 if (preserve.marker != NULL)
2863 bfd_preserve_restore (abfd, &preserve);
2864 return NULL;
2867 static const bfd_target *
2868 bfd_mach_o_gen_object_p (bfd *abfd)
2870 return bfd_mach_o_header_p (abfd, 0, 0);
2873 static const bfd_target *
2874 bfd_mach_o_gen_core_p (bfd *abfd)
2876 return bfd_mach_o_header_p (abfd, BFD_MACH_O_MH_CORE, 0);
2879 typedef struct mach_o_fat_archentry
2881 unsigned long cputype;
2882 unsigned long cpusubtype;
2883 unsigned long offset;
2884 unsigned long size;
2885 unsigned long align;
2886 } mach_o_fat_archentry;
2888 typedef struct mach_o_fat_data_struct
2890 unsigned long magic;
2891 unsigned long nfat_arch;
2892 mach_o_fat_archentry *archentries;
2893 } mach_o_fat_data_struct;
2895 const bfd_target *
2896 bfd_mach_o_archive_p (bfd *abfd)
2898 mach_o_fat_data_struct *adata = NULL;
2899 unsigned char buf[20];
2900 unsigned long i;
2902 if (bfd_seek (abfd, 0, SEEK_SET) != 0
2903 || bfd_bread ((PTR) buf, 8, abfd) != 8)
2904 goto error;
2906 adata = bfd_alloc (abfd, sizeof (mach_o_fat_data_struct));
2907 if (adata == NULL)
2908 goto error;
2910 adata->magic = bfd_getb32 (buf);
2911 adata->nfat_arch = bfd_getb32 (buf + 4);
2912 if (adata->magic != 0xcafebabe)
2913 goto error;
2914 /* Avoid matching Java bytecode files, which have the same magic number.
2915 In the Java bytecode file format this field contains the JVM version,
2916 which starts at 43.0. */
2917 if (adata->nfat_arch > 30)
2918 goto error;
2920 adata->archentries =
2921 bfd_alloc (abfd, adata->nfat_arch * sizeof (mach_o_fat_archentry));
2922 if (adata->archentries == NULL)
2923 goto error;
2925 for (i = 0; i < adata->nfat_arch; i++)
2927 if (bfd_seek (abfd, 8 + 20 * i, SEEK_SET) != 0
2928 || bfd_bread ((PTR) buf, 20, abfd) != 20)
2929 goto error;
2930 adata->archentries[i].cputype = bfd_getb32 (buf);
2931 adata->archentries[i].cpusubtype = bfd_getb32 (buf + 4);
2932 adata->archentries[i].offset = bfd_getb32 (buf + 8);
2933 adata->archentries[i].size = bfd_getb32 (buf + 12);
2934 adata->archentries[i].align = bfd_getb32 (buf + 16);
2937 abfd->tdata.mach_o_fat_data = adata;
2938 return abfd->xvec;
2940 error:
2941 if (adata != NULL)
2942 bfd_release (abfd, adata);
2943 bfd_set_error (bfd_error_wrong_format);
2944 return NULL;
2947 bfd *
2948 bfd_mach_o_openr_next_archived_file (bfd *archive, bfd *prev)
2950 mach_o_fat_data_struct *adata;
2951 mach_o_fat_archentry *entry = NULL;
2952 unsigned long i;
2953 bfd *nbfd;
2954 enum bfd_architecture arch_type;
2955 unsigned long arch_subtype;
2957 adata = (mach_o_fat_data_struct *) archive->tdata.mach_o_fat_data;
2958 BFD_ASSERT (adata != NULL);
2960 /* Find index of previous entry. */
2961 if (prev == NULL)
2962 i = 0; /* Start at first one. */
2963 else
2965 for (i = 0; i < adata->nfat_arch; i++)
2967 if (adata->archentries[i].offset == prev->origin)
2968 break;
2971 if (i == adata->nfat_arch)
2973 /* Not found. */
2974 bfd_set_error (bfd_error_bad_value);
2975 return NULL;
2977 i++; /* Get next entry. */
2980 if (i >= adata->nfat_arch)
2982 bfd_set_error (bfd_error_no_more_archived_files);
2983 return NULL;
2986 entry = &adata->archentries[i];
2987 nbfd = _bfd_new_bfd_contained_in (archive);
2988 if (nbfd == NULL)
2989 return NULL;
2991 nbfd->origin = entry->offset;
2993 bfd_mach_o_convert_architecture (entry->cputype, entry->cpusubtype,
2994 &arch_type, &arch_subtype);
2995 /* Create the member filename.
2996 Use FILENAME:ARCH_NAME. */
2998 char *s = NULL;
2999 const char *arch_name;
3000 size_t arch_file_len = strlen (bfd_get_filename (archive));
3002 arch_name = bfd_printable_arch_mach (arch_type, arch_subtype);
3003 s = bfd_malloc (arch_file_len + 1 + strlen (arch_name) + 1);
3004 if (s == NULL)
3005 return NULL;
3006 memcpy (s, bfd_get_filename (archive), arch_file_len);
3007 s[arch_file_len] = ':';
3008 strcpy (s + arch_file_len + 1, arch_name);
3009 nbfd->filename = s;
3011 nbfd->iostream = NULL;
3012 bfd_set_arch_mach (nbfd, arch_type, arch_subtype);
3014 return nbfd;
3017 /* If ABFD format is FORMAT and architecture is ARCH, return it.
3018 If ABFD is a fat image containing a member that corresponds to FORMAT
3019 and ARCH, returns it.
3020 In other case, returns NULL.
3021 This function allows transparent uses of fat images. */
3022 bfd *
3023 bfd_mach_o_fat_extract (bfd *abfd,
3024 bfd_format format,
3025 const bfd_arch_info_type *arch)
3027 bfd *res;
3028 mach_o_fat_data_struct *adata;
3029 unsigned int i;
3031 if (bfd_check_format (abfd, format))
3033 if (bfd_get_arch_info (abfd) == arch)
3034 return abfd;
3035 return NULL;
3037 if (!bfd_check_format (abfd, bfd_archive)
3038 || abfd->xvec != &mach_o_fat_vec)
3039 return NULL;
3041 /* This is a Mach-O fat image. */
3042 adata = (mach_o_fat_data_struct *) abfd->tdata.mach_o_fat_data;
3043 BFD_ASSERT (adata != NULL);
3045 for (i = 0; i < adata->nfat_arch; i++)
3047 struct mach_o_fat_archentry *e = &adata->archentries[i];
3048 enum bfd_architecture cpu_type;
3049 unsigned long cpu_subtype;
3051 bfd_mach_o_convert_architecture (e->cputype, e->cpusubtype,
3052 &cpu_type, &cpu_subtype);
3053 if (cpu_type != arch->arch || cpu_subtype != arch->mach)
3054 continue;
3056 /* The architecture is found. */
3057 res = _bfd_new_bfd_contained_in (abfd);
3058 if (res == NULL)
3059 return NULL;
3061 res->origin = e->offset;
3063 res->filename = strdup (abfd->filename);
3064 res->iostream = NULL;
3066 if (bfd_check_format (res, format))
3068 BFD_ASSERT (bfd_get_arch_info (res) == arch);
3069 return res;
3071 bfd_close (res);
3072 return NULL;
3075 return NULL;
3079 bfd_mach_o_lookup_section (bfd *abfd,
3080 asection *section,
3081 bfd_mach_o_load_command **mcommand,
3082 bfd_mach_o_section **msection)
3084 struct mach_o_data_struct *md = bfd_mach_o_get_data (abfd);
3085 unsigned int i, j, num;
3087 bfd_mach_o_load_command *ncmd = NULL;
3088 bfd_mach_o_section *nsect = NULL;
3090 BFD_ASSERT (mcommand != NULL);
3091 BFD_ASSERT (msection != NULL);
3093 num = 0;
3094 for (i = 0; i < md->header.ncmds; i++)
3096 struct bfd_mach_o_load_command *cmd = &md->commands[i];
3097 struct bfd_mach_o_segment_command *seg = NULL;
3099 if (cmd->type != BFD_MACH_O_LC_SEGMENT
3100 || cmd->type != BFD_MACH_O_LC_SEGMENT_64)
3101 continue;
3102 seg = &cmd->command.segment;
3104 for (j = 0; j < seg->nsects; j++)
3106 struct bfd_mach_o_section *sect = &seg->sections[j];
3108 if (sect->bfdsection == section)
3110 if (num == 0)
3112 nsect = sect;
3113 ncmd = cmd;
3115 num++;
3120 *mcommand = ncmd;
3121 *msection = nsect;
3122 return num;
3126 bfd_mach_o_lookup_command (bfd *abfd,
3127 bfd_mach_o_load_command_type type,
3128 bfd_mach_o_load_command **mcommand)
3130 struct mach_o_data_struct *md = bfd_mach_o_get_data (abfd);
3131 bfd_mach_o_load_command *ncmd = NULL;
3132 unsigned int i, num;
3134 BFD_ASSERT (md != NULL);
3135 BFD_ASSERT (mcommand != NULL);
3137 num = 0;
3138 for (i = 0; i < md->header.ncmds; i++)
3140 struct bfd_mach_o_load_command *cmd = &md->commands[i];
3142 if (cmd->type != type)
3143 continue;
3145 if (num == 0)
3146 ncmd = cmd;
3147 num++;
3150 *mcommand = ncmd;
3151 return num;
3154 unsigned long
3155 bfd_mach_o_stack_addr (enum bfd_mach_o_cpu_type type)
3157 switch (type)
3159 case BFD_MACH_O_CPU_TYPE_MC680x0:
3160 return 0x04000000;
3161 case BFD_MACH_O_CPU_TYPE_MC88000:
3162 return 0xffffe000;
3163 case BFD_MACH_O_CPU_TYPE_POWERPC:
3164 return 0xc0000000;
3165 case BFD_MACH_O_CPU_TYPE_I386:
3166 return 0xc0000000;
3167 case BFD_MACH_O_CPU_TYPE_SPARC:
3168 return 0xf0000000;
3169 case BFD_MACH_O_CPU_TYPE_I860:
3170 return 0;
3171 case BFD_MACH_O_CPU_TYPE_HPPA:
3172 return 0xc0000000 - 0x04000000;
3173 default:
3174 return 0;
3178 typedef struct bfd_mach_o_xlat_name
3180 const char *name;
3181 unsigned long val;
3183 bfd_mach_o_xlat_name;
3185 static void
3186 bfd_mach_o_print_flags (const bfd_mach_o_xlat_name *table,
3187 unsigned long val,
3188 FILE *file)
3190 int first = 1;
3192 for (; table->name; table++)
3194 if (table->val & val)
3196 if (!first)
3197 fprintf (file, "+");
3198 fprintf (file, "%s", table->name);
3199 val &= ~table->val;
3200 first = 0;
3203 if (val)
3205 if (!first)
3206 fprintf (file, "+");
3207 fprintf (file, "0x%lx", val);
3208 return;
3210 if (first)
3211 fprintf (file, "-");
3214 static const char *
3215 bfd_mach_o_get_name (const bfd_mach_o_xlat_name *table, unsigned long val)
3217 for (; table->name; table++)
3218 if (table->val == val)
3219 return table->name;
3220 return "*UNKNOWN*";
3223 static bfd_mach_o_xlat_name bfd_mach_o_cpu_name[] =
3225 { "vax", BFD_MACH_O_CPU_TYPE_VAX },
3226 { "mc680x0", BFD_MACH_O_CPU_TYPE_MC680x0 },
3227 { "i386", BFD_MACH_O_CPU_TYPE_I386 },
3228 { "mips", BFD_MACH_O_CPU_TYPE_MIPS },
3229 { "mc98000", BFD_MACH_O_CPU_TYPE_MC98000 },
3230 { "hppa", BFD_MACH_O_CPU_TYPE_HPPA },
3231 { "arm", BFD_MACH_O_CPU_TYPE_ARM },
3232 { "mc88000", BFD_MACH_O_CPU_TYPE_MC88000 },
3233 { "sparc", BFD_MACH_O_CPU_TYPE_SPARC },
3234 { "i860", BFD_MACH_O_CPU_TYPE_I860 },
3235 { "alpha", BFD_MACH_O_CPU_TYPE_ALPHA },
3236 { "powerpc", BFD_MACH_O_CPU_TYPE_POWERPC },
3237 { "powerpc_64", BFD_MACH_O_CPU_TYPE_POWERPC_64 },
3238 { "x86_64", BFD_MACH_O_CPU_TYPE_X86_64 },
3239 { NULL, 0}
3242 static bfd_mach_o_xlat_name bfd_mach_o_filetype_name[] =
3244 { "object", BFD_MACH_O_MH_OBJECT },
3245 { "execute", BFD_MACH_O_MH_EXECUTE },
3246 { "fvmlib", BFD_MACH_O_MH_FVMLIB },
3247 { "core", BFD_MACH_O_MH_CORE },
3248 { "preload", BFD_MACH_O_MH_PRELOAD },
3249 { "dylib", BFD_MACH_O_MH_DYLIB },
3250 { "dylinker", BFD_MACH_O_MH_DYLINKER },
3251 { "bundle", BFD_MACH_O_MH_BUNDLE },
3252 { "dylib_stub", BFD_MACH_O_MH_DYLIB_STUB },
3253 { "dym", BFD_MACH_O_MH_DSYM },
3254 { "kext_bundle", BFD_MACH_O_MH_KEXT_BUNDLE },
3255 { NULL, 0}
3258 static bfd_mach_o_xlat_name bfd_mach_o_header_flags_name[] =
3260 { "noundefs", BFD_MACH_O_MH_NOUNDEFS },
3261 { "incrlink", BFD_MACH_O_MH_INCRLINK },
3262 { "dyldlink", BFD_MACH_O_MH_DYLDLINK },
3263 { "bindatload", BFD_MACH_O_MH_BINDATLOAD },
3264 { "prebound", BFD_MACH_O_MH_PREBOUND },
3265 { "split_segs", BFD_MACH_O_MH_SPLIT_SEGS },
3266 { "lazy_init", BFD_MACH_O_MH_LAZY_INIT },
3267 { "twolevel", BFD_MACH_O_MH_TWOLEVEL },
3268 { "force_flat", BFD_MACH_O_MH_FORCE_FLAT },
3269 { "nomultidefs", BFD_MACH_O_MH_NOMULTIDEFS },
3270 { "nofixprebinding", BFD_MACH_O_MH_NOFIXPREBINDING },
3271 { "prebindable", BFD_MACH_O_MH_PREBINDABLE },
3272 { "allmodsbound", BFD_MACH_O_MH_ALLMODSBOUND },
3273 { "subsections_via_symbols", BFD_MACH_O_MH_SUBSECTIONS_VIA_SYMBOLS },
3274 { "canonical", BFD_MACH_O_MH_CANONICAL },
3275 { "weak_defines", BFD_MACH_O_MH_WEAK_DEFINES },
3276 { "binds_to_weak", BFD_MACH_O_MH_BINDS_TO_WEAK },
3277 { "allow_stack_execution", BFD_MACH_O_MH_ALLOW_STACK_EXECUTION },
3278 { "root_safe", BFD_MACH_O_MH_ROOT_SAFE },
3279 { "setuid_safe", BFD_MACH_O_MH_SETUID_SAFE },
3280 { "no_reexported_dylibs", BFD_MACH_O_MH_NO_REEXPORTED_DYLIBS },
3281 { "pie", BFD_MACH_O_MH_PIE },
3282 { NULL, 0}
3285 static bfd_mach_o_xlat_name bfd_mach_o_section_type_name[] =
3287 { "regular", BFD_MACH_O_S_REGULAR},
3288 { "zerofill", BFD_MACH_O_S_ZEROFILL},
3289 { "cstring_literals", BFD_MACH_O_S_CSTRING_LITERALS},
3290 { "4byte_literals", BFD_MACH_O_S_4BYTE_LITERALS},
3291 { "8byte_literals", BFD_MACH_O_S_8BYTE_LITERALS},
3292 { "literal_pointers", BFD_MACH_O_S_LITERAL_POINTERS},
3293 { "non_lazy_symbol_pointers", BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS},
3294 { "lazy_symbol_pointers", BFD_MACH_O_S_LAZY_SYMBOL_POINTERS},
3295 { "symbol_stubs", BFD_MACH_O_S_SYMBOL_STUBS},
3296 { "mod_init_func_pointers", BFD_MACH_O_S_MOD_INIT_FUNC_POINTERS},
3297 { "mod_fini_func_pointers", BFD_MACH_O_S_MOD_FINI_FUNC_POINTERS},
3298 { "coalesced", BFD_MACH_O_S_COALESCED},
3299 { "gb_zerofill", BFD_MACH_O_S_GB_ZEROFILL},
3300 { "interposing", BFD_MACH_O_S_INTERPOSING},
3301 { "16byte_literals", BFD_MACH_O_S_16BYTE_LITERALS},
3302 { "dtrace_dof", BFD_MACH_O_S_DTRACE_DOF},
3303 { "lazy_dylib_symbol_pointers", BFD_MACH_O_S_LAZY_DYLIB_SYMBOL_POINTERS},
3304 { NULL, 0}
3307 static bfd_mach_o_xlat_name bfd_mach_o_section_attribute_name[] =
3309 { "loc_reloc", BFD_MACH_O_S_ATTR_LOC_RELOC },
3310 { "ext_reloc", BFD_MACH_O_S_ATTR_EXT_RELOC },
3311 { "some_instructions", BFD_MACH_O_S_ATTR_SOME_INSTRUCTIONS },
3312 { "debug", BFD_MACH_O_S_ATTR_DEBUG },
3313 { "modifying_code", BFD_MACH_O_S_SELF_MODIFYING_CODE },
3314 { "live_support", BFD_MACH_O_S_ATTR_LIVE_SUPPORT },
3315 { "no_dead_strip", BFD_MACH_O_S_ATTR_NO_DEAD_STRIP },
3316 { "strip_static_syms", BFD_MACH_O_S_ATTR_STRIP_STATIC_SYMS },
3317 { "no_toc", BFD_MACH_O_S_ATTR_NO_TOC },
3318 { "pure_instructions", BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS },
3319 { NULL, 0}
3322 static bfd_mach_o_xlat_name bfd_mach_o_load_command_name[] =
3324 { "segment", BFD_MACH_O_LC_SEGMENT},
3325 { "symtab", BFD_MACH_O_LC_SYMTAB},
3326 { "symseg", BFD_MACH_O_LC_SYMSEG},
3327 { "thread", BFD_MACH_O_LC_THREAD},
3328 { "unixthread", BFD_MACH_O_LC_UNIXTHREAD},
3329 { "loadfvmlib", BFD_MACH_O_LC_LOADFVMLIB},
3330 { "idfvmlib", BFD_MACH_O_LC_IDFVMLIB},
3331 { "ident", BFD_MACH_O_LC_IDENT},
3332 { "fvmfile", BFD_MACH_O_LC_FVMFILE},
3333 { "prepage", BFD_MACH_O_LC_PREPAGE},
3334 { "dysymtab", BFD_MACH_O_LC_DYSYMTAB},
3335 { "load_dylib", BFD_MACH_O_LC_LOAD_DYLIB},
3336 { "id_dylib", BFD_MACH_O_LC_ID_DYLIB},
3337 { "load_dylinker", BFD_MACH_O_LC_LOAD_DYLINKER},
3338 { "id_dylinker", BFD_MACH_O_LC_ID_DYLINKER},
3339 { "prebound_dylib", BFD_MACH_O_LC_PREBOUND_DYLIB},
3340 { "routines", BFD_MACH_O_LC_ROUTINES},
3341 { "sub_framework", BFD_MACH_O_LC_SUB_FRAMEWORK},
3342 { "sub_umbrella", BFD_MACH_O_LC_SUB_UMBRELLA},
3343 { "sub_client", BFD_MACH_O_LC_SUB_CLIENT},
3344 { "sub_library", BFD_MACH_O_LC_SUB_LIBRARY},
3345 { "twolevel_hints", BFD_MACH_O_LC_TWOLEVEL_HINTS},
3346 { "prebind_cksum", BFD_MACH_O_LC_PREBIND_CKSUM},
3347 { "load_weak_dylib", BFD_MACH_O_LC_LOAD_WEAK_DYLIB},
3348 { "segment_64", BFD_MACH_O_LC_SEGMENT_64},
3349 { "routines_64", BFD_MACH_O_LC_ROUTINES_64},
3350 { "uuid", BFD_MACH_O_LC_UUID},
3351 { "rpath", BFD_MACH_O_LC_RPATH},
3352 { "code_signature", BFD_MACH_O_LC_CODE_SIGNATURE},
3353 { "segment_split_info", BFD_MACH_O_LC_SEGMENT_SPLIT_INFO},
3354 { "reexport_dylib", BFD_MACH_O_LC_REEXPORT_DYLIB},
3355 { "lazy_load_dylib", BFD_MACH_O_LC_LAZY_LOAD_DYLIB},
3356 { "encryption_info", BFD_MACH_O_LC_ENCRYPTION_INFO},
3357 { "dyld_info", BFD_MACH_O_LC_DYLD_INFO},
3358 { NULL, 0}
3361 static void
3362 bfd_mach_o_print_private_header (bfd *abfd, FILE *file)
3364 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
3365 bfd_mach_o_header *h = &mdata->header;
3367 fputs (_("Mach-O header:\n"), file);
3368 fprintf (file, _(" magic : %08lx\n"), h->magic);
3369 fprintf (file, _(" cputype : %08lx (%s)\n"), h->cputype,
3370 bfd_mach_o_get_name (bfd_mach_o_cpu_name, h->cputype));
3371 fprintf (file, _(" cpusubtype: %08lx\n"), h->cpusubtype);
3372 fprintf (file, _(" filetype : %08lx (%s)\n"),
3373 h->filetype,
3374 bfd_mach_o_get_name (bfd_mach_o_filetype_name, h->filetype));
3375 fprintf (file, _(" ncmds : %08lx (%lu)\n"), h->ncmds, h->ncmds);
3376 fprintf (file, _(" sizeofcmds: %08lx\n"), h->sizeofcmds);
3377 fprintf (file, _(" flags : %08lx ("), h->flags);
3378 bfd_mach_o_print_flags (bfd_mach_o_header_flags_name, h->flags, file);
3379 fputs (_(")\n"), file);
3380 fprintf (file, _(" reserved : %08x\n"), h->reserved);
3383 static void
3384 bfd_mach_o_print_section_map (bfd *abfd, FILE *file)
3386 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
3387 unsigned int i, j;
3388 unsigned int sec_nbr = 0;
3390 fputs (_("Segments and Sections:\n"), file);
3391 fputs (_(" #: Segment name Section name Address\n"), file);
3393 for (i = 0; i < mdata->header.ncmds; i++)
3395 bfd_mach_o_segment_command *seg;
3397 if (mdata->commands[i].type != BFD_MACH_O_LC_SEGMENT
3398 && mdata->commands[i].type != BFD_MACH_O_LC_SEGMENT_64)
3399 continue;
3401 seg = &mdata->commands[i].command.segment;
3403 fprintf (file, "[Segment %-16s ", seg->segname);
3404 fprintf_vma (file, seg->vmaddr);
3405 fprintf (file, "-");
3406 fprintf_vma (file, seg->vmaddr + seg->vmsize - 1);
3407 fputc (' ', file);
3408 fputc (seg->initprot & BFD_MACH_O_PROT_READ ? 'r' : '-', file);
3409 fputc (seg->initprot & BFD_MACH_O_PROT_WRITE ? 'w' : '-', file);
3410 fputc (seg->initprot & BFD_MACH_O_PROT_EXECUTE ? 'x' : '-', file);
3411 fprintf (file, "]\n");
3412 for (j = 0; j < seg->nsects; j++)
3414 bfd_mach_o_section *sec = &seg->sections[j];
3415 fprintf (file, "%02u: %-16s %-16s ", ++sec_nbr,
3416 sec->segname, sec->sectname);
3417 fprintf_vma (file, sec->addr);
3418 fprintf (file, " ");
3419 fprintf_vma (file, sec->size);
3420 fprintf (file, " %08lx\n", sec->flags);
3425 static void
3426 bfd_mach_o_print_section (bfd *abfd ATTRIBUTE_UNUSED,
3427 bfd_mach_o_section *sec, FILE *file)
3429 fprintf (file, " Section: %-16s %-16s (bfdname: %s)\n",
3430 sec->sectname, sec->segname, sec->bfdsection->name);
3431 fprintf (file, " addr: ");
3432 fprintf_vma (file, sec->addr);
3433 fprintf (file, " size: ");
3434 fprintf_vma (file, sec->size);
3435 fprintf (file, " offset: ");
3436 fprintf_vma (file, sec->offset);
3437 fprintf (file, "\n");
3438 fprintf (file, " align: %ld", sec->align);
3439 fprintf (file, " nreloc: %lu reloff: ", sec->nreloc);
3440 fprintf_vma (file, sec->reloff);
3441 fprintf (file, "\n");
3442 fprintf (file, " flags: %08lx (type: %s", sec->flags,
3443 bfd_mach_o_get_name (bfd_mach_o_section_type_name,
3444 sec->flags & BFD_MACH_O_SECTION_TYPE_MASK));
3445 fprintf (file, " attr: ");
3446 bfd_mach_o_print_flags (bfd_mach_o_section_attribute_name,
3447 sec->flags & BFD_MACH_O_SECTION_ATTRIBUTES_MASK,
3448 file);
3449 fprintf (file, ")\n");
3450 switch (sec->flags & BFD_MACH_O_SECTION_TYPE_MASK)
3452 case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS:
3453 case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS:
3454 case BFD_MACH_O_S_SYMBOL_STUBS:
3455 fprintf (file, " first indirect sym: %lu", sec->reserved1);
3456 fprintf (file, " (%u entries)",
3457 bfd_mach_o_section_get_nbr_indirect (abfd, sec));
3458 break;
3459 default:
3460 fprintf (file, " reserved1: 0x%lx", sec->reserved1);
3461 break;
3463 switch (sec->flags & BFD_MACH_O_SECTION_TYPE_MASK)
3465 case BFD_MACH_O_S_SYMBOL_STUBS:
3466 fprintf (file, " stub size: %lu", sec->reserved2);
3467 break;
3468 default:
3469 fprintf (file, " reserved2: 0x%lx", sec->reserved2);
3470 break;
3472 fprintf (file, " reserved3: 0x%lx\n", sec->reserved3);
3475 static void
3476 bfd_mach_o_print_segment (bfd *abfd ATTRIBUTE_UNUSED,
3477 bfd_mach_o_load_command *cmd, FILE *file)
3479 bfd_mach_o_segment_command *seg = &cmd->command.segment;
3480 unsigned int i;
3482 fprintf (file, " name: %s\n", *seg->segname ? seg->segname : "*none*");
3483 fprintf (file, " vmaddr: ");
3484 fprintf_vma (file, seg->vmaddr);
3485 fprintf (file, " vmsize: ");
3486 fprintf_vma (file, seg->vmsize);
3487 fprintf (file, "\n");
3488 fprintf (file, " fileoff: ");
3489 fprintf_vma (file, seg->fileoff);
3490 fprintf (file, " filesize: ");
3491 fprintf_vma (file, (bfd_vma)seg->filesize);
3492 fprintf (file, " endoff: ");
3493 fprintf_vma (file, (bfd_vma)(seg->fileoff + seg->filesize));
3494 fprintf (file, "\n");
3495 fprintf (file, " nsects: %lu ", seg->nsects);
3496 fprintf (file, " flags: %lx\n", seg->flags);
3497 for (i = 0; i < seg->nsects; i++)
3498 bfd_mach_o_print_section (abfd, &seg->sections[i], file);
3501 static void
3502 bfd_mach_o_print_dysymtab (bfd *abfd ATTRIBUTE_UNUSED,
3503 bfd_mach_o_load_command *cmd, FILE *file)
3505 bfd_mach_o_dysymtab_command *dysymtab = &cmd->command.dysymtab;
3506 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
3507 unsigned int i;
3509 fprintf (file, " local symbols: idx: %10lu num: %-8lu",
3510 dysymtab->ilocalsym, dysymtab->nlocalsym);
3511 fprintf (file, " (nxtidx: %lu)\n",
3512 dysymtab->ilocalsym + dysymtab->nlocalsym);
3513 fprintf (file, " external symbols: idx: %10lu num: %-8lu",
3514 dysymtab->iextdefsym, dysymtab->nextdefsym);
3515 fprintf (file, " (nxtidx: %lu)\n",
3516 dysymtab->iextdefsym + dysymtab->nextdefsym);
3517 fprintf (file, " undefined symbols: idx: %10lu num: %-8lu",
3518 dysymtab->iundefsym, dysymtab->nundefsym);
3519 fprintf (file, " (nxtidx: %lu)\n",
3520 dysymtab->iundefsym + dysymtab->nundefsym);
3521 fprintf (file, " table of content: off: 0x%08lx num: %-8lu",
3522 dysymtab->tocoff, dysymtab->ntoc);
3523 fprintf (file, " (endoff: 0x%08lx)\n",
3524 dysymtab->tocoff
3525 + dysymtab->ntoc * BFD_MACH_O_TABLE_OF_CONTENT_SIZE);
3526 fprintf (file, " module table: off: 0x%08lx num: %-8lu",
3527 dysymtab->modtaboff, dysymtab->nmodtab);
3528 fprintf (file, " (endoff: 0x%08lx)\n",
3529 dysymtab->modtaboff + dysymtab->nmodtab
3530 * (mach_o_wide_p (&mdata->header) ?
3531 BFD_MACH_O_DYLIB_MODULE_64_SIZE : BFD_MACH_O_DYLIB_MODULE_SIZE));
3532 fprintf (file, " external reference table: off: 0x%08lx num: %-8lu",
3533 dysymtab->extrefsymoff, dysymtab->nextrefsyms);
3534 fprintf (file, " (endoff: 0x%08lx)\n",
3535 dysymtab->extrefsymoff
3536 + dysymtab->nextrefsyms * BFD_MACH_O_REFERENCE_SIZE);
3537 fprintf (file, " indirect symbol table: off: 0x%08lx num: %-8lu",
3538 dysymtab->indirectsymoff, dysymtab->nindirectsyms);
3539 fprintf (file, " (endoff: 0x%08lx)\n",
3540 dysymtab->indirectsymoff
3541 + dysymtab->nindirectsyms * BFD_MACH_O_INDIRECT_SYMBOL_SIZE);
3542 fprintf (file, " external relocation table: off: 0x%08lx num: %-8lu",
3543 dysymtab->extreloff, dysymtab->nextrel);
3544 fprintf (file, " (endoff: 0x%08lx)\n",
3545 dysymtab->extreloff + dysymtab->nextrel * BFD_MACH_O_RELENT_SIZE);
3546 fprintf (file, " local relocation table: off: 0x%08lx num: %-8lu",
3547 dysymtab->locreloff, dysymtab->nlocrel);
3548 fprintf (file, " (endoff: 0x%08lx)\n",
3549 dysymtab->locreloff + dysymtab->nlocrel * BFD_MACH_O_RELENT_SIZE);
3551 if (dysymtab->ntoc > 0
3552 || dysymtab->nindirectsyms > 0
3553 || dysymtab->nextrefsyms > 0)
3555 /* Try to read the symbols to display the toc or indirect symbols. */
3556 bfd_mach_o_read_symtab_symbols (abfd);
3558 else if (dysymtab->nmodtab > 0)
3560 /* Try to read the strtab to display modules name. */
3561 bfd_mach_o_read_symtab_strtab (abfd);
3564 for (i = 0; i < dysymtab->nmodtab; i++)
3566 bfd_mach_o_dylib_module *module = &dysymtab->dylib_module[i];
3567 fprintf (file, " module %u:\n", i);
3568 fprintf (file, " name: %lu", module->module_name_idx);
3569 if (mdata->symtab && mdata->symtab->strtab)
3570 fprintf (file, ": %s",
3571 mdata->symtab->strtab + module->module_name_idx);
3572 fprintf (file, "\n");
3573 fprintf (file, " extdefsym: idx: %8lu num: %lu\n",
3574 module->iextdefsym, module->nextdefsym);
3575 fprintf (file, " refsym: idx: %8lu num: %lu\n",
3576 module->irefsym, module->nrefsym);
3577 fprintf (file, " localsym: idx: %8lu num: %lu\n",
3578 module->ilocalsym, module->nlocalsym);
3579 fprintf (file, " extrel: idx: %8lu num: %lu\n",
3580 module->iextrel, module->nextrel);
3581 fprintf (file, " init: idx: %8u num: %u\n",
3582 module->iinit, module->ninit);
3583 fprintf (file, " term: idx: %8u num: %u\n",
3584 module->iterm, module->nterm);
3585 fprintf (file, " objc_module_info: addr: ");
3586 fprintf_vma (file, module->objc_module_info_addr);
3587 fprintf (file, " size: %lu\n", module->objc_module_info_size);
3590 if (dysymtab->ntoc > 0)
3592 bfd_mach_o_symtab_command *symtab = mdata->symtab;
3594 fprintf (file, " table of content: (symbol/module)\n");
3595 for (i = 0; i < dysymtab->ntoc; i++)
3597 bfd_mach_o_dylib_table_of_content *toc = &dysymtab->dylib_toc[i];
3599 fprintf (file, " %4u: ", i);
3600 if (symtab && symtab->symbols && toc->symbol_index < symtab->nsyms)
3602 const char *name = symtab->symbols[toc->symbol_index].symbol.name;
3603 fprintf (file, "%s (%lu)", name ? name : "*invalid*",
3604 toc->symbol_index);
3606 else
3607 fprintf (file, "%lu", toc->symbol_index);
3609 fprintf (file, " / ");
3610 if (symtab && symtab->strtab
3611 && toc->module_index < dysymtab->nmodtab)
3613 bfd_mach_o_dylib_module *mod;
3614 mod = &dysymtab->dylib_module[toc->module_index];
3615 fprintf (file, "%s (%lu)",
3616 symtab->strtab + mod->module_name_idx,
3617 toc->module_index);
3619 else
3620 fprintf (file, "%lu", toc->module_index);
3622 fprintf (file, "\n");
3626 if (dysymtab->nindirectsyms != 0)
3628 fprintf (file, " indirect symbols:\n");
3630 for (i = 0; i < mdata->nsects; i++)
3632 bfd_mach_o_section *sec = mdata->sections[i];
3633 unsigned int j, first, last;
3634 bfd_mach_o_symtab_command *symtab = mdata->symtab;
3635 bfd_vma addr;
3636 bfd_vma entry_size;
3638 switch (sec->flags & BFD_MACH_O_SECTION_TYPE_MASK)
3640 case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS:
3641 case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS:
3642 case BFD_MACH_O_S_SYMBOL_STUBS:
3643 first = sec->reserved1;
3644 last = first + bfd_mach_o_section_get_nbr_indirect (abfd, sec);
3645 addr = sec->addr;
3646 entry_size = bfd_mach_o_section_get_entry_size (abfd, sec);
3647 fprintf (file, " for section %s.%s:\n",
3648 sec->segname, sec->sectname);
3649 for (j = first; j < last; j++)
3651 unsigned int isym = dysymtab->indirect_syms[j];
3653 fprintf (file, " ");
3654 fprintf_vma (file, addr);
3655 fprintf (file, " %5u: 0x%08x", j, isym);
3656 if (isym & BFD_MACH_O_INDIRECT_SYMBOL_LOCAL)
3657 fprintf (file, " LOCAL");
3658 if (isym & BFD_MACH_O_INDIRECT_SYMBOL_ABS)
3659 fprintf (file, " ABSOLUTE");
3660 if (symtab && symtab->symbols
3661 && isym < symtab->nsyms
3662 && symtab->symbols[isym].symbol.name)
3663 fprintf (file, " %s", symtab->symbols[isym].symbol.name);
3664 fprintf (file, "\n");
3665 addr += entry_size;
3667 break;
3668 default:
3669 break;
3673 if (dysymtab->nextrefsyms > 0)
3675 bfd_mach_o_symtab_command *symtab = mdata->symtab;
3677 fprintf (file, " external reference table: (symbol flags)\n");
3678 for (i = 0; i < dysymtab->nextrefsyms; i++)
3680 bfd_mach_o_dylib_reference *ref = &dysymtab->ext_refs[i];
3682 fprintf (file, " %4u: %5lu 0x%02lx", i, ref->isym, ref->flags);
3683 if (symtab && symtab->symbols
3684 && ref->isym < symtab->nsyms
3685 && symtab->symbols[ref->isym].symbol.name)
3686 fprintf (file, " %s", symtab->symbols[ref->isym].symbol.name);
3687 fprintf (file, "\n");
3693 static void
3694 bfd_mach_o_print_dyld_info (bfd *abfd ATTRIBUTE_UNUSED,
3695 bfd_mach_o_load_command *cmd, FILE *file)
3697 bfd_mach_o_dyld_info_command *info = &cmd->command.dyld_info;
3699 fprintf (file, " rebase: off: 0x%08x size: %-8u\n",
3700 info->rebase_off, info->rebase_size);
3701 fprintf (file, " bind: off: 0x%08x size: %-8u\n",
3702 info->bind_off, info->bind_size);
3703 fprintf (file, " weak bind: off: 0x%08x size: %-8u\n",
3704 info->weak_bind_off, info->weak_bind_size);
3705 fprintf (file, " lazy bind: off: 0x%08x size: %-8u\n",
3706 info->lazy_bind_off, info->lazy_bind_size);
3707 fprintf (file, " export: off: 0x%08x size: %-8u\n",
3708 info->export_off, info->export_size);
3711 bfd_boolean
3712 bfd_mach_o_bfd_print_private_bfd_data (bfd *abfd, PTR ptr)
3714 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
3715 FILE *file = (FILE *) ptr;
3716 unsigned int i;
3718 bfd_mach_o_print_private_header (abfd, file);
3719 fputc ('\n', file);
3721 for (i = 0; i < mdata->header.ncmds; i++)
3723 bfd_mach_o_load_command *cmd = &mdata->commands[i];
3725 fprintf (file, "Load command %s:",
3726 bfd_mach_o_get_name (bfd_mach_o_load_command_name, cmd->type));
3727 switch (cmd->type)
3729 case BFD_MACH_O_LC_SEGMENT:
3730 case BFD_MACH_O_LC_SEGMENT_64:
3731 bfd_mach_o_print_segment (abfd, cmd, file);
3732 break;
3733 case BFD_MACH_O_LC_UUID:
3735 bfd_mach_o_uuid_command *uuid = &cmd->command.uuid;
3736 unsigned int i;
3738 for (i = 0; i < sizeof (uuid->uuid); i++)
3739 fprintf (file, " %02x", uuid->uuid[i]);
3740 fputc ('\n', file);
3742 break;
3743 case BFD_MACH_O_LC_LOAD_DYLIB:
3744 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB:
3745 case BFD_MACH_O_LC_REEXPORT_DYLIB:
3746 case BFD_MACH_O_LC_ID_DYLIB:
3748 bfd_mach_o_dylib_command *dylib = &cmd->command.dylib;
3749 fprintf (file, " %s\n", dylib->name_str);
3750 fprintf (file, " time stamp: 0x%08lx\n",
3751 dylib->timestamp);
3752 fprintf (file, " current version: 0x%08lx\n",
3753 dylib->current_version);
3754 fprintf (file, " comptibility version: 0x%08lx\n",
3755 dylib->compatibility_version);
3756 break;
3758 case BFD_MACH_O_LC_LOAD_DYLINKER:
3759 case BFD_MACH_O_LC_ID_DYLINKER:
3760 fprintf (file, " %s\n", cmd->command.dylinker.name_str);
3761 break;
3762 case BFD_MACH_O_LC_SYMTAB:
3764 bfd_mach_o_symtab_command *symtab = &cmd->command.symtab;
3765 fprintf (file,
3766 "\n"
3767 " symoff: 0x%08x nsyms: %8u (endoff: 0x%08x)\n",
3768 symtab->symoff, symtab->nsyms,
3769 symtab->symoff + symtab->nsyms
3770 * (mach_o_wide_p (&mdata->header)
3771 ? BFD_MACH_O_NLIST_64_SIZE : BFD_MACH_O_NLIST_SIZE));
3772 fprintf (file,
3773 " stroff: 0x%08x strsize: %8u (endoff: 0x%08x)\n",
3774 symtab->stroff, symtab->strsize,
3775 symtab->stroff + symtab->strsize);
3776 break;
3778 case BFD_MACH_O_LC_DYSYMTAB:
3779 fprintf (file, "\n");
3780 bfd_mach_o_print_dysymtab (abfd, cmd, file);
3781 break;
3782 case BFD_MACH_O_LC_CODE_SIGNATURE:
3783 case BFD_MACH_O_LC_SEGMENT_SPLIT_INFO:
3785 bfd_mach_o_linkedit_command *linkedit = &cmd->command.linkedit;
3786 fprintf
3787 (file, "\n"
3788 " dataoff: 0x%08lx datasize: 0x%08lx (endoff: 0x%08lx)\n",
3789 linkedit->dataoff, linkedit->datasize,
3790 linkedit->dataoff + linkedit->datasize);
3791 break;
3793 case BFD_MACH_O_LC_SUB_FRAMEWORK:
3794 case BFD_MACH_O_LC_SUB_UMBRELLA:
3795 case BFD_MACH_O_LC_SUB_LIBRARY:
3796 case BFD_MACH_O_LC_SUB_CLIENT:
3797 case BFD_MACH_O_LC_RPATH:
3799 bfd_mach_o_str_command *str = &cmd->command.str;
3800 fprintf (file, " %s\n", str->str);
3801 break;
3803 case BFD_MACH_O_LC_THREAD:
3804 case BFD_MACH_O_LC_UNIXTHREAD:
3806 bfd_mach_o_thread_command *thread = &cmd->command.thread;
3807 unsigned int j;
3808 bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
3810 fprintf (file, " nflavours: %lu\n", thread->nflavours);
3811 for (j = 0; j < thread->nflavours; j++)
3813 bfd_mach_o_thread_flavour *flavour = &thread->flavours[j];
3815 fprintf (file, " %2u: flavour: 0x%08lx offset: 0x%08lx"
3816 " size: 0x%08lx\n",
3817 j, flavour->flavour, flavour->offset,
3818 flavour->size);
3819 if (bed->_bfd_mach_o_print_thread)
3821 char *buf = bfd_malloc (flavour->size);
3823 if (buf
3824 && bfd_seek (abfd, flavour->offset, SEEK_SET) == 0
3825 && (bfd_bread (buf, flavour->size, abfd)
3826 == flavour->size))
3827 (*bed->_bfd_mach_o_print_thread)(abfd, flavour,
3828 file, buf);
3829 free (buf);
3832 break;
3834 case BFD_MACH_O_LC_DYLD_INFO:
3835 fprintf (file, "\n");
3836 bfd_mach_o_print_dyld_info (abfd, cmd, file);
3837 break;
3838 default:
3839 fprintf (file, "\n");
3840 break;
3842 fputc ('\n', file);
3845 bfd_mach_o_print_section_map (abfd, file);
3847 return TRUE;
3851 bfd_mach_o_core_fetch_environment (bfd *abfd,
3852 unsigned char **rbuf,
3853 unsigned int *rlen)
3855 bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
3856 unsigned long stackaddr = bfd_mach_o_stack_addr (mdata->header.cputype);
3857 unsigned int i = 0;
3859 for (i = 0; i < mdata->header.ncmds; i++)
3861 bfd_mach_o_load_command *cur = &mdata->commands[i];
3862 bfd_mach_o_segment_command *seg = NULL;
3864 if (cur->type != BFD_MACH_O_LC_SEGMENT)
3865 continue;
3867 seg = &cur->command.segment;
3869 if ((seg->vmaddr + seg->vmsize) == stackaddr)
3871 unsigned long start = seg->fileoff;
3872 unsigned long end = seg->fileoff + seg->filesize;
3873 unsigned char *buf = bfd_malloc (1024);
3874 unsigned long size = 1024;
3876 for (;;)
3878 bfd_size_type nread = 0;
3879 unsigned long offset;
3880 int found_nonnull = 0;
3882 if (size > (end - start))
3883 size = (end - start);
3885 buf = bfd_realloc_or_free (buf, size);
3886 if (buf == NULL)
3887 return -1;
3889 if (bfd_seek (abfd, end - size, SEEK_SET) != 0)
3891 free (buf);
3892 return -1;
3895 nread = bfd_bread (buf, size, abfd);
3897 if (nread != size)
3899 free (buf);
3900 return -1;
3903 for (offset = 4; offset <= size; offset += 4)
3905 unsigned long val;
3907 val = *((unsigned long *) (buf + size - offset));
3908 if (! found_nonnull)
3910 if (val != 0)
3911 found_nonnull = 1;
3913 else if (val == 0x0)
3915 unsigned long bottom;
3916 unsigned long top;
3918 bottom = seg->fileoff + seg->filesize - offset;
3919 top = seg->fileoff + seg->filesize - 4;
3920 *rbuf = bfd_malloc (top - bottom);
3921 *rlen = top - bottom;
3923 memcpy (*rbuf, buf + size - *rlen, *rlen);
3924 free (buf);
3925 return 0;
3929 if (size == (end - start))
3930 break;
3932 size *= 2;
3935 free (buf);
3939 return -1;
3942 char *
3943 bfd_mach_o_core_file_failing_command (bfd *abfd)
3945 unsigned char *buf = NULL;
3946 unsigned int len = 0;
3947 int ret = -1;
3949 ret = bfd_mach_o_core_fetch_environment (abfd, &buf, &len);
3950 if (ret < 0)
3951 return NULL;
3953 return (char *) buf;
3957 bfd_mach_o_core_file_failing_signal (bfd *abfd ATTRIBUTE_UNUSED)
3959 return 0;
3962 #define bfd_mach_o_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
3963 #define bfd_mach_o_bfd_reloc_name_lookup _bfd_norelocs_bfd_reloc_name_lookup
3965 #define bfd_mach_o_swap_reloc_in NULL
3966 #define bfd_mach_o_swap_reloc_out NULL
3967 #define bfd_mach_o_print_thread NULL
3969 #define TARGET_NAME mach_o_be_vec
3970 #define TARGET_STRING "mach-o-be"
3971 #define TARGET_BIG_ENDIAN 1
3972 #define TARGET_ARCHIVE 0
3973 #include "mach-o-target.c"
3975 #undef TARGET_NAME
3976 #undef TARGET_STRING
3977 #undef TARGET_BIG_ENDIAN
3978 #undef TARGET_ARCHIVE
3980 #define TARGET_NAME mach_o_le_vec
3981 #define TARGET_STRING "mach-o-le"
3982 #define TARGET_BIG_ENDIAN 0
3983 #define TARGET_ARCHIVE 0
3985 #include "mach-o-target.c"
3987 #undef TARGET_NAME
3988 #undef TARGET_STRING
3989 #undef TARGET_BIG_ENDIAN
3990 #undef TARGET_ARCHIVE
3992 #define TARGET_NAME mach_o_fat_vec
3993 #define TARGET_STRING "mach-o-fat"
3994 #define TARGET_BIG_ENDIAN 1
3995 #define TARGET_ARCHIVE 1
3997 #include "mach-o-target.c"
3999 #undef TARGET_NAME
4000 #undef TARGET_STRING
4001 #undef TARGET_BIG_ENDIAN
4002 #undef TARGET_ARCHIVE