1 /* Mach-O support for BFD.
2 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
26 #include "libiberty.h"
27 #include "aout/stab_gnu.h"
31 #define BFD_IO_FUNCS 0
34 #define bfd_mach_o_mkarchive _bfd_noarchive_mkarchive
35 #define bfd_mach_o_read_ar_hdr _bfd_noarchive_read_ar_hdr
36 #define bfd_mach_o_slurp_armap _bfd_noarchive_slurp_armap
37 #define bfd_mach_o_slurp_extended_name_table _bfd_noarchive_slurp_extended_name_table
38 #define bfd_mach_o_construct_extended_name_table _bfd_noarchive_construct_extended_name_table
39 #define bfd_mach_o_truncate_arname _bfd_noarchive_truncate_arname
40 #define bfd_mach_o_write_armap _bfd_noarchive_write_armap
41 #define bfd_mach_o_get_elt_at_index _bfd_noarchive_get_elt_at_index
42 #define bfd_mach_o_generic_stat_arch_elt _bfd_noarchive_generic_stat_arch_elt
43 #define bfd_mach_o_update_armap_timestamp _bfd_noarchive_update_armap_timestamp
44 #define bfd_mach_o_close_and_cleanup _bfd_generic_close_and_cleanup
45 #define bfd_mach_o_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
46 #define bfd_mach_o_new_section_hook _bfd_generic_new_section_hook
47 #define bfd_mach_o_get_section_contents_in_window _bfd_generic_get_section_contents_in_window
48 #define bfd_mach_o_bfd_is_local_label_name _bfd_nosymbols_bfd_is_local_label_name
49 #define bfd_mach_o_bfd_is_target_special_symbol ((bfd_boolean (*) (bfd *, asymbol *)) bfd_false)
50 #define bfd_mach_o_bfd_is_local_label_name _bfd_nosymbols_bfd_is_local_label_name
51 #define bfd_mach_o_get_lineno _bfd_nosymbols_get_lineno
52 #define bfd_mach_o_find_nearest_line _bfd_nosymbols_find_nearest_line
53 #define bfd_mach_o_find_inliner_info _bfd_nosymbols_find_inliner_info
54 #define bfd_mach_o_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
55 #define bfd_mach_o_read_minisymbols _bfd_generic_read_minisymbols
56 #define bfd_mach_o_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
57 #define bfd_mach_o_bfd_get_relocated_section_contents bfd_generic_get_relocated_section_contents
58 #define bfd_mach_o_bfd_relax_section bfd_generic_relax_section
59 #define bfd_mach_o_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
60 #define bfd_mach_o_bfd_link_hash_table_free _bfd_generic_link_hash_table_free
61 #define bfd_mach_o_bfd_link_add_symbols _bfd_generic_link_add_symbols
62 #define bfd_mach_o_bfd_link_just_syms _bfd_generic_link_just_syms
63 #define bfd_mach_o_bfd_final_link _bfd_generic_final_link
64 #define bfd_mach_o_bfd_link_split_section _bfd_generic_link_split_section
65 #define bfd_mach_o_set_arch_mach bfd_default_set_arch_mach
66 #define bfd_mach_o_bfd_merge_private_bfd_data _bfd_generic_bfd_merge_private_bfd_data
67 #define bfd_mach_o_bfd_set_private_flags _bfd_generic_bfd_set_private_flags
68 #define bfd_mach_o_get_section_contents _bfd_generic_get_section_contents
69 #define bfd_mach_o_set_section_contents _bfd_generic_set_section_contents
70 #define bfd_mach_o_bfd_gc_sections bfd_generic_gc_sections
71 #define bfd_mach_o_bfd_merge_sections bfd_generic_merge_sections
72 #define bfd_mach_o_bfd_is_group_section bfd_generic_is_group_section
73 #define bfd_mach_o_bfd_discard_group bfd_generic_discard_group
74 #define bfd_mach_o_section_already_linked _bfd_generic_section_already_linked
75 #define bfd_mach_o_bfd_copy_private_header_data _bfd_generic_bfd_copy_private_header_data
76 #define bfd_mach_o_core_file_matches_executable_p generic_core_file_matches_executable_p
79 bfd_mach_o_version (bfd *abfd)
81 bfd_mach_o_data_struct *mdata = NULL;
83 BFD_ASSERT (bfd_mach_o_valid (abfd));
84 mdata = abfd->tdata.mach_o_data;
86 return mdata->header.version;
90 bfd_mach_o_valid (bfd *abfd)
92 if (abfd == NULL || abfd->xvec == NULL)
95 if (! ((abfd->xvec == &mach_o_be_vec)
96 || (abfd->xvec == &mach_o_le_vec)
97 || (abfd->xvec == &mach_o_fat_vec)))
100 if (abfd->tdata.mach_o_data == NULL)
105 /* Copy any private info we understand from the input symbol
106 to the output symbol. */
109 bfd_mach_o_bfd_copy_private_symbol_data (bfd *ibfd ATTRIBUTE_UNUSED,
110 asymbol *isymbol ATTRIBUTE_UNUSED,
111 bfd *obfd ATTRIBUTE_UNUSED,
112 asymbol *osymbol ATTRIBUTE_UNUSED)
117 /* Copy any private info we understand from the input section
118 to the output section. */
121 bfd_mach_o_bfd_copy_private_section_data (bfd *ibfd ATTRIBUTE_UNUSED,
122 asection *isection ATTRIBUTE_UNUSED,
123 bfd *obfd ATTRIBUTE_UNUSED,
124 asection *osection ATTRIBUTE_UNUSED)
129 /* Copy any private info we understand from the input bfd
130 to the output bfd. */
133 bfd_mach_o_bfd_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
135 BFD_ASSERT (bfd_mach_o_valid (ibfd));
136 BFD_ASSERT (bfd_mach_o_valid (obfd));
138 obfd->tdata.mach_o_data = ibfd->tdata.mach_o_data;
139 obfd->tdata.mach_o_data->ibfd = ibfd;
144 bfd_mach_o_count_symbols (bfd *abfd)
146 bfd_mach_o_data_struct *mdata = NULL;
150 BFD_ASSERT (bfd_mach_o_valid (abfd));
151 mdata = abfd->tdata.mach_o_data;
153 for (i = 0; i < mdata->header.ncmds; i++)
154 if (mdata->commands[i].type == BFD_MACH_O_LC_SYMTAB)
156 bfd_mach_o_symtab_command *sym = &mdata->commands[i].command.symtab;
164 bfd_mach_o_get_symtab_upper_bound (bfd *abfd)
166 long nsyms = bfd_mach_o_count_symbols (abfd);
171 return ((nsyms + 1) * sizeof (asymbol *));
175 bfd_mach_o_canonicalize_symtab (bfd *abfd, asymbol **alocation)
177 bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
178 long nsyms = bfd_mach_o_count_symbols (abfd);
179 asymbol **csym = alocation;
185 for (i = 0; i < mdata->header.ncmds; i++)
187 if (mdata->commands[i].type == BFD_MACH_O_LC_SYMTAB)
189 bfd_mach_o_symtab_command *sym = &mdata->commands[i].command.symtab;
191 if (bfd_mach_o_scan_read_symtab_symbols (abfd, &mdata->commands[i].command.symtab) != 0)
193 fprintf (stderr, "bfd_mach_o_canonicalize_symtab: unable to load symbols for section %lu\n", i);
197 BFD_ASSERT (sym->symbols != NULL);
199 for (j = 0; j < sym->nsyms; j++)
201 BFD_ASSERT (csym < (alocation + nsyms));
202 *csym++ = &sym->symbols[j];
213 bfd_mach_o_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
217 bfd_symbol_info (symbol, ret);
221 bfd_mach_o_print_symbol (bfd *abfd,
224 bfd_print_symbol_type how)
226 FILE *file = (FILE *) afile;
234 case bfd_print_symbol_name:
235 fprintf (file, "%s", symbol->name);
238 bfd_print_symbol_vandf (abfd, (PTR) file, symbol);
239 ntype = BFD_MACH_O_SYM_NTYPE (symbol);
240 nsect = BFD_MACH_O_SYM_NSECT (symbol);
241 ndesc = BFD_MACH_O_SYM_NDESC (symbol);
242 if (ntype & BFD_MACH_O_N_STAB)
243 name = bfd_get_stab_name (ntype);
245 switch (ntype & BFD_MACH_O_N_TYPE)
247 case BFD_MACH_O_N_UNDF:
250 case BFD_MACH_O_N_ABS:
253 case BFD_MACH_O_N_INDR:
256 case BFD_MACH_O_N_PBUD:
259 case BFD_MACH_O_N_SECT:
268 fprintf (file, " %02x %-6s %02x %04x", ntype, name, nsect, ndesc);
269 if ((ntype & BFD_MACH_O_N_STAB) == 0
270 && (ntype & BFD_MACH_O_N_TYPE) == BFD_MACH_O_N_SECT)
271 fprintf (file, " %-5s", symbol->section->name);
272 fprintf (file, " %s", symbol->name);
277 bfd_mach_o_convert_architecture (bfd_mach_o_cpu_type mtype,
278 bfd_mach_o_cpu_subtype msubtype ATTRIBUTE_UNUSED,
279 enum bfd_architecture *type,
280 unsigned long *subtype)
282 *subtype = bfd_arch_unknown;
286 case BFD_MACH_O_CPU_TYPE_VAX: *type = bfd_arch_vax; break;
287 case BFD_MACH_O_CPU_TYPE_MC680x0: *type = bfd_arch_m68k; break;
288 case BFD_MACH_O_CPU_TYPE_I386:
289 *type = bfd_arch_i386;
290 *subtype = bfd_mach_i386_i386;
292 case BFD_MACH_O_CPU_TYPE_X86_64:
293 *type = bfd_arch_i386;
294 *subtype = bfd_mach_x86_64;
296 case BFD_MACH_O_CPU_TYPE_MIPS: *type = bfd_arch_mips; break;
297 case BFD_MACH_O_CPU_TYPE_MC98000: *type = bfd_arch_m98k; break;
298 case BFD_MACH_O_CPU_TYPE_HPPA: *type = bfd_arch_hppa; break;
299 case BFD_MACH_O_CPU_TYPE_ARM: *type = bfd_arch_arm; break;
300 case BFD_MACH_O_CPU_TYPE_MC88000: *type = bfd_arch_m88k; break;
301 case BFD_MACH_O_CPU_TYPE_SPARC:
302 *type = bfd_arch_sparc;
303 *subtype = bfd_mach_sparc;
305 case BFD_MACH_O_CPU_TYPE_I860: *type = bfd_arch_i860; break;
306 case BFD_MACH_O_CPU_TYPE_ALPHA: *type = bfd_arch_alpha; break;
307 case BFD_MACH_O_CPU_TYPE_POWERPC:
308 *type = bfd_arch_powerpc;
309 *subtype = bfd_mach_ppc;
311 case BFD_MACH_O_CPU_TYPE_POWERPC_64:
312 *type = bfd_arch_powerpc;
313 *subtype = bfd_mach_ppc64;
316 *type = bfd_arch_unknown;
322 bfd_mach_o_write_header (bfd *abfd, bfd_mach_o_header *header)
324 unsigned char buf[32];
327 size = (header->version == 2) ? 32 : 28;
329 bfd_h_put_32 (abfd, header->magic, buf + 0);
330 bfd_h_put_32 (abfd, header->cputype, buf + 4);
331 bfd_h_put_32 (abfd, header->cpusubtype, buf + 8);
332 bfd_h_put_32 (abfd, header->filetype, buf + 12);
333 bfd_h_put_32 (abfd, header->ncmds, buf + 16);
334 bfd_h_put_32 (abfd, header->sizeofcmds, buf + 20);
335 bfd_h_put_32 (abfd, header->flags, buf + 24);
337 if (header->version == 2)
338 bfd_h_put_32 (abfd, header->reserved, buf + 28);
340 bfd_seek (abfd, 0, SEEK_SET);
341 if (bfd_bwrite ((PTR) buf, size, abfd) != size)
348 bfd_mach_o_scan_write_thread (bfd *abfd, bfd_mach_o_load_command *command)
350 bfd_mach_o_thread_command *cmd = &command->command.thread;
352 unsigned char buf[8];
354 unsigned int nflavours;
356 BFD_ASSERT ((command->type == BFD_MACH_O_LC_THREAD)
357 || (command->type == BFD_MACH_O_LC_UNIXTHREAD));
361 for (i = 0; i < cmd->nflavours; i++)
363 BFD_ASSERT ((cmd->flavours[i].size % 4) == 0);
364 BFD_ASSERT (cmd->flavours[i].offset == (command->offset + offset + 8));
366 bfd_h_put_32 (abfd, cmd->flavours[i].flavour, buf);
367 bfd_h_put_32 (abfd, (cmd->flavours[i].size / 4), buf + 4);
369 bfd_seek (abfd, command->offset + offset, SEEK_SET);
370 if (bfd_bwrite ((PTR) buf, 8, abfd) != 8)
373 offset += cmd->flavours[i].size + 8;
380 bfd_mach_o_scan_write_section_32 (bfd *abfd,
381 bfd_mach_o_section *section,
384 unsigned char buf[68];
386 memcpy (buf, section->sectname, 16);
387 memcpy (buf + 16, section->segname, 16);
388 bfd_h_put_32 (abfd, section->addr, buf + 32);
389 bfd_h_put_32 (abfd, section->size, buf + 36);
390 bfd_h_put_32 (abfd, section->offset, buf + 40);
391 bfd_h_put_32 (abfd, section->align, buf + 44);
392 bfd_h_put_32 (abfd, section->reloff, buf + 48);
393 bfd_h_put_32 (abfd, section->nreloc, buf + 52);
394 bfd_h_put_32 (abfd, section->flags, buf + 56);
395 bfd_h_put_32 (abfd, section->reserved1, buf + 60);
396 bfd_h_put_32 (abfd, section->reserved2, buf + 64);
398 bfd_seek (abfd, offset, SEEK_SET);
399 if (bfd_bwrite ((PTR) buf, 68, abfd) != 68)
406 bfd_mach_o_scan_write_section_64 (bfd *abfd,
407 bfd_mach_o_section *section,
410 unsigned char buf[80];
412 memcpy (buf, section->sectname, 16);
413 memcpy (buf + 16, section->segname, 16);
414 bfd_h_put_64 (abfd, section->addr, buf + 32);
415 bfd_h_put_64 (abfd, section->size, buf + 40);
416 bfd_h_put_32 (abfd, section->offset, buf + 48);
417 bfd_h_put_32 (abfd, section->align, buf + 52);
418 bfd_h_put_32 (abfd, section->reloff, buf + 56);
419 bfd_h_put_32 (abfd, section->nreloc, buf + 60);
420 bfd_h_put_32 (abfd, section->flags, buf + 64);
421 bfd_h_put_32 (abfd, section->reserved1, buf + 68);
422 bfd_h_put_32 (abfd, section->reserved2, buf + 72);
423 bfd_h_put_32 (abfd, section->reserved3, buf + 76);
425 bfd_seek (abfd, offset, SEEK_SET);
426 if (bfd_bwrite ((PTR) buf, 80, abfd) != 80)
433 bfd_mach_o_scan_write_section (bfd *abfd,
434 bfd_mach_o_section *section,
439 return bfd_mach_o_scan_write_section_64 (abfd, section, offset);
441 return bfd_mach_o_scan_write_section_32 (abfd, section, offset);
445 bfd_mach_o_scan_write_segment (bfd *abfd,
446 bfd_mach_o_load_command *command,
449 unsigned char buf[64];
450 bfd_mach_o_segment_command *seg = &command->command.segment;
455 BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT_64);
457 memcpy (buf, seg->segname, 16);
459 bfd_h_put_64 (abfd, seg->vmaddr, buf + 16);
460 bfd_h_put_64 (abfd, seg->vmsize, buf + 24);
461 bfd_h_put_64 (abfd, seg->fileoff, buf + 32);
462 bfd_h_put_64 (abfd, seg->filesize, buf + 40);
463 bfd_h_put_32 (abfd, seg->maxprot, buf + 48);
464 bfd_h_put_32 (abfd, seg->initprot, buf + 52);
465 bfd_h_put_32 (abfd, seg->nsects, buf + 56);
466 bfd_h_put_32 (abfd, seg->flags, buf + 60);
468 bfd_seek (abfd, command->offset + 8, SEEK_SET);
469 if (bfd_bwrite ((PTR) buf, 64, abfd) != 64)
474 BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT);
476 memcpy (buf, seg->segname, 16);
478 bfd_h_put_32 (abfd, seg->vmaddr, buf + 16);
479 bfd_h_put_32 (abfd, seg->vmsize, buf + 20);
480 bfd_h_put_32 (abfd, seg->fileoff, buf + 24);
481 bfd_h_put_32 (abfd, seg->filesize, buf + 28);
482 bfd_h_put_32 (abfd, seg->maxprot, buf + 32);
483 bfd_h_put_32 (abfd, seg->initprot, buf + 36);
484 bfd_h_put_32 (abfd, seg->nsects, buf + 40);
485 bfd_h_put_32 (abfd, seg->flags, buf + 44);
487 bfd_seek (abfd, command->offset + 8, SEEK_SET);
488 if (bfd_bwrite ((PTR) buf, 48, abfd) != 48)
494 bfd_vma nbytes = seg->filesize;
495 bfd_vma curoff = seg->fileoff;
499 bfd_vma thiswrite = nbytes;
501 if (thiswrite > 1024)
504 bfd_seek (abfd, curoff, SEEK_SET);
505 if (bfd_bread ((PTR) buf, thiswrite, abfd) != thiswrite)
508 bfd_seek (abfd, curoff, SEEK_SET);
509 if (bfd_bwrite ((PTR) buf, thiswrite, abfd) != thiswrite)
517 for (i = 0; i < seg->nsects; i++)
521 segoff = command->offset + 64 + 8 + (i * 80);
523 segoff = command->offset + 48 + 8 + (i * 68);
525 if (bfd_mach_o_scan_write_section
526 (abfd, &seg->sections[i], segoff, wide) != 0)
534 bfd_mach_o_scan_write_segment_32 (bfd *abfd, bfd_mach_o_load_command *command)
536 return bfd_mach_o_scan_write_segment (abfd, command, 0);
540 bfd_mach_o_scan_write_segment_64 (bfd *abfd, bfd_mach_o_load_command *command)
542 return bfd_mach_o_scan_write_segment (abfd, command, 1);
546 bfd_mach_o_scan_write_symtab_symbols (bfd *abfd, bfd_mach_o_load_command *command)
548 bfd_mach_o_symtab_command *sym = &command->command.symtab;
552 for (i = 0; i < sym->nsyms; i++)
554 unsigned char buf[12];
555 bfd_vma symoff = sym->symoff + (i * 12);
556 unsigned char ntype = 0;
557 unsigned char nsect = 0;
560 s = &sym->symbols[i];
562 /* Instead just set from the stored values. */
563 ntype = BFD_MACH_O_SYM_NTYPE (s);
564 nsect = BFD_MACH_O_SYM_NSECT (s);
565 ndesc = BFD_MACH_O_SYM_NDESC (s);
567 bfd_h_put_32 (abfd, s->name - sym->strtab, buf);
568 bfd_h_put_8 (abfd, ntype, buf + 4);
569 bfd_h_put_8 (abfd, nsect, buf + 5);
570 bfd_h_put_16 (abfd, ndesc, buf + 6);
571 bfd_h_put_32 (abfd, s->section->vma + s->value, buf + 8);
573 bfd_seek (abfd, symoff, SEEK_SET);
574 if (bfd_bwrite ((PTR) buf, 12, abfd) != 12)
576 fprintf (stderr, "bfd_mach_o_scan_write_symtab_symbols: unable to write %d bytes at %lu\n",
577 12, (unsigned long) symoff);
586 bfd_mach_o_scan_write_symtab (bfd *abfd, bfd_mach_o_load_command *command)
588 bfd_mach_o_symtab_command *seg = &command->command.symtab;
589 unsigned char buf[16];
591 BFD_ASSERT (command->type == BFD_MACH_O_LC_SYMTAB);
593 bfd_h_put_32 (abfd, seg->symoff, buf);
594 bfd_h_put_32 (abfd, seg->nsyms, buf + 4);
595 bfd_h_put_32 (abfd, seg->stroff, buf + 8);
596 bfd_h_put_32 (abfd, seg->strsize, buf + 12);
598 bfd_seek (abfd, command->offset + 8, SEEK_SET);
599 if (bfd_bwrite ((PTR) buf, 16, abfd) != 16)
602 if (bfd_mach_o_scan_write_symtab_symbols (abfd, command) != 0)
609 bfd_mach_o_write_contents (bfd *abfd)
614 bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
616 /* Write data sections first in case they overlap header data to be
619 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
622 /* Now write header information. */
623 if (bfd_mach_o_write_header (abfd, &mdata->header) != 0)
626 for (i = 0; i < mdata->header.ncmds; i++)
628 unsigned char buf[8];
629 bfd_mach_o_load_command *cur = &mdata->commands[i];
630 unsigned long typeflag;
632 typeflag = cur->type_required ? cur->type & BFD_MACH_O_LC_REQ_DYLD : cur->type;
634 bfd_h_put_32 (abfd, typeflag, buf);
635 bfd_h_put_32 (abfd, cur->len, buf + 4);
637 bfd_seek (abfd, cur->offset, SEEK_SET);
638 if (bfd_bwrite ((PTR) buf, 8, abfd) != 8)
643 case BFD_MACH_O_LC_SEGMENT:
644 if (bfd_mach_o_scan_write_segment_32 (abfd, cur) != 0)
647 case BFD_MACH_O_LC_SEGMENT_64:
648 if (bfd_mach_o_scan_write_segment_64 (abfd, cur) != 0)
651 case BFD_MACH_O_LC_SYMTAB:
652 if (bfd_mach_o_scan_write_symtab (abfd, cur) != 0)
655 case BFD_MACH_O_LC_SYMSEG:
657 case BFD_MACH_O_LC_THREAD:
658 case BFD_MACH_O_LC_UNIXTHREAD:
659 if (bfd_mach_o_scan_write_thread (abfd, cur) != 0)
662 case BFD_MACH_O_LC_LOADFVMLIB:
663 case BFD_MACH_O_LC_IDFVMLIB:
664 case BFD_MACH_O_LC_IDENT:
665 case BFD_MACH_O_LC_FVMFILE:
666 case BFD_MACH_O_LC_PREPAGE:
667 case BFD_MACH_O_LC_DYSYMTAB:
668 case BFD_MACH_O_LC_LOAD_DYLIB:
669 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB:
670 case BFD_MACH_O_LC_ID_DYLIB:
671 case BFD_MACH_O_LC_LOAD_DYLINKER:
672 case BFD_MACH_O_LC_ID_DYLINKER:
673 case BFD_MACH_O_LC_PREBOUND_DYLIB:
674 case BFD_MACH_O_LC_ROUTINES:
675 case BFD_MACH_O_LC_SUB_FRAMEWORK:
679 "unable to write unknown load command 0x%lx\n",
680 (unsigned long) cur->type);
689 bfd_mach_o_sizeof_headers (bfd *a ATTRIBUTE_UNUSED,
690 struct bfd_link_info *info ATTRIBUTE_UNUSED)
695 /* Make an empty symbol. This is required only because
696 bfd_make_section_anyway wants to create a symbol for the section. */
699 bfd_mach_o_make_empty_symbol (bfd *abfd)
703 new = bfd_zalloc (abfd, sizeof (* new));
711 bfd_mach_o_read_header (bfd *abfd, bfd_mach_o_header *header)
713 unsigned char buf[32];
715 bfd_vma (*get32) (const void *) = NULL;
717 bfd_seek (abfd, 0, SEEK_SET);
719 /* Just read the magic number. */
720 if (bfd_bread ((PTR) buf, 4, abfd) != 4)
723 if (bfd_getb32 (buf) == 0xfeedface)
725 header->byteorder = BFD_ENDIAN_BIG;
726 header->magic = 0xfeedface;
730 else if (bfd_getl32 (buf) == 0xfeedface)
732 header->byteorder = BFD_ENDIAN_LITTLE;
733 header->magic = 0xfeedface;
737 else if (bfd_getb32 (buf) == 0xfeedfacf)
739 header->byteorder = BFD_ENDIAN_BIG;
740 header->magic = 0xfeedfacf;
744 else if (bfd_getl32 (buf) == 0xfeedfacf)
746 header->byteorder = BFD_ENDIAN_LITTLE;
747 header->magic = 0xfeedfacf;
753 header->byteorder = BFD_ENDIAN_UNKNOWN;
757 /* Once the size of the header is known, read the full header. */
758 size = (header->version == 2) ? 32 : 28;
760 bfd_seek (abfd, 0, SEEK_SET);
761 if (bfd_bread ((PTR) buf, size, abfd) != size)
764 header->cputype = (*get32) (buf + 4);
765 header->cpusubtype = (*get32) (buf + 8);
766 header->filetype = (*get32) (buf + 12);
767 header->ncmds = (*get32) (buf + 16);
768 header->sizeofcmds = (*get32) (buf + 20);
769 header->flags = (*get32) (buf + 24);
771 if (header->version == 2)
772 header->reserved = (*get32) (buf + 28);
778 bfd_mach_o_make_bfd_section (bfd *abfd, bfd_mach_o_section *section,
783 const char *prefix = "LC_SEGMENT";
784 unsigned int snamelen;
787 snamelen = strlen (prefix) + 1
788 + strlen (section->segname) + 1
789 + strlen (section->sectname) + 1;
791 sname = bfd_alloc (abfd, snamelen);
795 /* Use canonical dwarf section names for dwarf sections. */
796 if (strcmp (section->segname, "__DWARF") == 0
797 && strncmp (section->sectname, "__", 2) == 0)
798 sprintf (sname, ".%s", section->sectname + 2);
800 sprintf (sname, "%s.%s.%s", prefix, section->segname, section->sectname);
802 if (section->flags & BFD_MACH_O_S_ATTR_DEBUG)
803 flags = SEC_HAS_CONTENTS | SEC_DEBUGGING;
807 if ((section->flags & BFD_MACH_O_SECTION_TYPE_MASK)
808 != BFD_MACH_O_S_ZEROFILL)
810 flags |= SEC_HAS_CONTENTS | SEC_LOAD;
811 if (prot & BFD_MACH_O_PROT_EXECUTE)
813 if (prot & BFD_MACH_O_PROT_WRITE)
815 else if (prot & BFD_MACH_O_PROT_READ)
816 flags |= SEC_READONLY;
819 bfdsec = bfd_make_section_anyway_with_flags (abfd, sname, flags);
823 bfdsec->vma = section->addr;
824 bfdsec->lma = section->addr;
825 bfdsec->size = section->size;
826 bfdsec->filepos = section->offset;
827 bfdsec->alignment_power = section->align;
828 bfdsec->segment_mark = 0;
834 bfd_mach_o_scan_read_section_32 (bfd *abfd,
835 bfd_mach_o_section *section,
839 unsigned char buf[68];
841 bfd_seek (abfd, offset, SEEK_SET);
842 if (bfd_bread ((PTR) buf, 68, abfd) != 68)
845 memcpy (section->sectname, buf, 16);
846 section->sectname[16] = '\0';
847 memcpy (section->segname, buf + 16, 16);
848 section->segname[16] = '\0';
849 section->addr = bfd_h_get_32 (abfd, buf + 32);
850 section->size = bfd_h_get_32 (abfd, buf + 36);
851 section->offset = bfd_h_get_32 (abfd, buf + 40);
852 section->align = bfd_h_get_32 (abfd, buf + 44);
853 section->reloff = bfd_h_get_32 (abfd, buf + 48);
854 section->nreloc = bfd_h_get_32 (abfd, buf + 52);
855 section->flags = bfd_h_get_32 (abfd, buf + 56);
856 section->reserved1 = bfd_h_get_32 (abfd, buf + 60);
857 section->reserved2 = bfd_h_get_32 (abfd, buf + 64);
858 section->reserved3 = 0;
859 section->bfdsection = bfd_mach_o_make_bfd_section (abfd, section, prot);
861 if (section->bfdsection == NULL)
868 bfd_mach_o_scan_read_section_64 (bfd *abfd,
869 bfd_mach_o_section *section,
873 unsigned char buf[80];
875 bfd_seek (abfd, offset, SEEK_SET);
876 if (bfd_bread ((PTR) buf, 80, abfd) != 80)
879 memcpy (section->sectname, buf, 16);
880 section->sectname[16] = '\0';
881 memcpy (section->segname, buf + 16, 16);
882 section->segname[16] = '\0';
883 section->addr = bfd_h_get_64 (abfd, buf + 32);
884 section->size = bfd_h_get_64 (abfd, buf + 40);
885 section->offset = bfd_h_get_32 (abfd, buf + 48);
886 section->align = bfd_h_get_32 (abfd, buf + 52);
887 section->reloff = bfd_h_get_32 (abfd, buf + 56);
888 section->nreloc = bfd_h_get_32 (abfd, buf + 60);
889 section->flags = bfd_h_get_32 (abfd, buf + 64);
890 section->reserved1 = bfd_h_get_32 (abfd, buf + 68);
891 section->reserved2 = bfd_h_get_32 (abfd, buf + 72);
892 section->reserved3 = bfd_h_get_32 (abfd, buf + 76);
893 section->bfdsection = bfd_mach_o_make_bfd_section (abfd, section, prot);
895 if (section->bfdsection == NULL)
902 bfd_mach_o_scan_read_section (bfd *abfd,
903 bfd_mach_o_section *section,
909 return bfd_mach_o_scan_read_section_64 (abfd, section, offset, prot);
911 return bfd_mach_o_scan_read_section_32 (abfd, section, offset, prot);
915 bfd_mach_o_scan_read_symtab_symbol (bfd *abfd,
916 bfd_mach_o_symtab_command *sym,
920 bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
921 unsigned int wide = (mdata->header.version == 2);
922 unsigned int symwidth = wide ? 16 : 12;
923 bfd_vma symoff = sym->symoff + (i * symwidth);
924 unsigned char buf[16];
925 unsigned char type = -1;
926 unsigned char section = -1;
929 unsigned long stroff = -1;
930 unsigned int symtype = -1;
932 BFD_ASSERT (sym->strtab != NULL);
934 bfd_seek (abfd, symoff, SEEK_SET);
935 if (bfd_bread ((PTR) buf, symwidth, abfd) != symwidth)
937 fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: unable to read %d bytes at %lu\n",
938 symwidth, (unsigned long) symoff);
942 stroff = bfd_h_get_32 (abfd, buf);
943 type = bfd_h_get_8 (abfd, buf + 4);
944 symtype = (type & 0x0e);
945 section = bfd_h_get_8 (abfd, buf + 5);
946 desc = bfd_h_get_16 (abfd, buf + 6);
948 value = bfd_h_get_64 (abfd, buf + 8);
950 value = bfd_h_get_32 (abfd, buf + 8);
952 if (stroff >= sym->strsize)
954 fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: symbol name out of range (%lu >= %lu)\n",
955 (unsigned long) stroff, (unsigned long) sym->strsize);
960 s->name = sym->strtab + stroff;
962 s->udata.i = (type << 24) | (section << 16) | desc;
965 if (type & BFD_MACH_O_N_STAB)
967 s->flags |= BSF_DEBUGGING;
968 s->section = bfd_und_section_ptr;
980 if ((section > 0) && (section <= mdata->nsects))
982 s->section = mdata->sections[section - 1]->bfdsection;
983 s->value = s->value - mdata->sections[section - 1]->addr;
990 if (type & BFD_MACH_O_N_PEXT)
991 s->flags |= BSF_GLOBAL;
993 if (type & BFD_MACH_O_N_EXT)
994 s->flags |= BSF_GLOBAL;
996 if (!(type & (BFD_MACH_O_N_PEXT | BFD_MACH_O_N_EXT)))
997 s->flags |= BSF_LOCAL;
1001 case BFD_MACH_O_N_UNDF:
1002 s->section = bfd_und_section_ptr;
1004 case BFD_MACH_O_N_PBUD:
1005 s->section = bfd_und_section_ptr;
1007 case BFD_MACH_O_N_ABS:
1008 s->section = bfd_abs_section_ptr;
1010 case BFD_MACH_O_N_SECT:
1011 if ((section > 0) && (section <= mdata->nsects))
1013 s->section = mdata->sections[section - 1]->bfdsection;
1014 s->value = s->value - mdata->sections[section - 1]->addr;
1018 /* Mach-O uses 0 to mean "no section"; not an error. */
1021 fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: "
1022 "symbol \"%s\" specified invalid section %d (max %lu): setting to undefined\n",
1023 s->name, section, mdata->nsects);
1025 s->section = bfd_und_section_ptr;
1028 case BFD_MACH_O_N_INDR:
1029 fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: "
1030 "symbol \"%s\" is unsupported 'indirect' reference: setting to undefined\n",
1032 s->section = bfd_und_section_ptr;
1035 fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: "
1036 "symbol \"%s\" specified invalid type field 0x%x: setting to undefined\n",
1038 s->section = bfd_und_section_ptr;
1047 bfd_mach_o_scan_read_symtab_strtab (bfd *abfd,
1048 bfd_mach_o_symtab_command *sym)
1050 BFD_ASSERT (sym->strtab == NULL);
1052 if (abfd->flags & BFD_IN_MEMORY)
1054 struct bfd_in_memory *b;
1056 b = (struct bfd_in_memory *) abfd->iostream;
1058 if ((sym->stroff + sym->strsize) > b->size)
1060 bfd_set_error (bfd_error_file_truncated);
1063 sym->strtab = (char *) b->buffer + sym->stroff;
1067 sym->strtab = bfd_alloc (abfd, sym->strsize);
1068 if (sym->strtab == NULL)
1071 bfd_seek (abfd, sym->stroff, SEEK_SET);
1072 if (bfd_bread ((PTR) sym->strtab, sym->strsize, abfd) != sym->strsize)
1074 fprintf (stderr, "bfd_mach_o_scan_read_symtab_strtab: unable to read %lu bytes at %lu\n",
1075 sym->strsize, sym->stroff);
1083 bfd_mach_o_scan_read_symtab_symbols (bfd *abfd,
1084 bfd_mach_o_symtab_command *sym)
1089 BFD_ASSERT (sym->symbols == NULL);
1090 sym->symbols = bfd_alloc (abfd, sym->nsyms * sizeof (asymbol));
1092 if (sym->symbols == NULL)
1094 fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbols: unable to allocate memory for symbols\n");
1098 ret = bfd_mach_o_scan_read_symtab_strtab (abfd, sym);
1102 for (i = 0; i < sym->nsyms; i++)
1104 ret = bfd_mach_o_scan_read_symtab_symbol (abfd, sym, &sym->symbols[i], i);
1113 bfd_mach_o_scan_read_dysymtab_symbol (bfd *abfd,
1114 bfd_mach_o_dysymtab_command *dysym,
1115 bfd_mach_o_symtab_command *sym,
1119 unsigned long isymoff = dysym->indirectsymoff + (i * 4);
1120 unsigned long symindex;
1121 unsigned char buf[4];
1123 BFD_ASSERT (i < dysym->nindirectsyms);
1125 bfd_seek (abfd, isymoff, SEEK_SET);
1126 if (bfd_bread ((PTR) buf, 4, abfd) != 4)
1128 fprintf (stderr, "bfd_mach_o_scan_read_dysymtab_symbol: unable to read %lu bytes at %lu\n",
1129 (unsigned long) 4, isymoff);
1132 symindex = bfd_h_get_32 (abfd, buf);
1134 return bfd_mach_o_scan_read_symtab_symbol (abfd, sym, s, symindex);
1138 bfd_mach_o_i386_flavour_string (unsigned int flavour)
1140 switch ((int) flavour)
1142 case BFD_MACH_O_x86_THREAD_STATE32: return "x86_THREAD_STATE32";
1143 case BFD_MACH_O_x86_FLOAT_STATE32: return "x86_FLOAT_STATE32";
1144 case BFD_MACH_O_x86_EXCEPTION_STATE32: return "x86_EXCEPTION_STATE32";
1145 case BFD_MACH_O_x86_THREAD_STATE64: return "x86_THREAD_STATE64";
1146 case BFD_MACH_O_x86_FLOAT_STATE64: return "x86_FLOAT_STATE64";
1147 case BFD_MACH_O_x86_EXCEPTION_STATE64: return "x86_EXCEPTION_STATE64";
1148 case BFD_MACH_O_x86_THREAD_STATE: return "x86_THREAD_STATE";
1149 case BFD_MACH_O_x86_FLOAT_STATE: return "x86_FLOAT_STATE";
1150 case BFD_MACH_O_x86_EXCEPTION_STATE: return "x86_EXCEPTION_STATE";
1151 case BFD_MACH_O_x86_DEBUG_STATE32: return "x86_DEBUG_STATE32";
1152 case BFD_MACH_O_x86_DEBUG_STATE64: return "x86_DEBUG_STATE64";
1153 case BFD_MACH_O_x86_DEBUG_STATE: return "x86_DEBUG_STATE";
1154 case BFD_MACH_O_THREAD_STATE_NONE: return "THREAD_STATE_NONE";
1155 default: return "UNKNOWN";
1160 bfd_mach_o_ppc_flavour_string (unsigned int flavour)
1162 switch ((int) flavour)
1164 case BFD_MACH_O_PPC_THREAD_STATE: return "PPC_THREAD_STATE";
1165 case BFD_MACH_O_PPC_FLOAT_STATE: return "PPC_FLOAT_STATE";
1166 case BFD_MACH_O_PPC_EXCEPTION_STATE: return "PPC_EXCEPTION_STATE";
1167 case BFD_MACH_O_PPC_VECTOR_STATE: return "PPC_VECTOR_STATE";
1168 default: return "UNKNOWN";
1173 bfd_mach_o_scan_read_dylinker (bfd *abfd,
1174 bfd_mach_o_load_command *command)
1176 bfd_mach_o_dylinker_command *cmd = &command->command.dylinker;
1177 unsigned char buf[4];
1178 unsigned int nameoff;
1183 BFD_ASSERT ((command->type == BFD_MACH_O_LC_ID_DYLINKER)
1184 || (command->type == BFD_MACH_O_LC_LOAD_DYLINKER));
1186 bfd_seek (abfd, command->offset + 8, SEEK_SET);
1187 if (bfd_bread ((PTR) buf, 4, abfd) != 4)
1190 nameoff = bfd_h_get_32 (abfd, buf + 0);
1192 cmd->name_offset = command->offset + nameoff;
1193 cmd->name_len = command->len - nameoff;
1195 if (command->type == BFD_MACH_O_LC_LOAD_DYLINKER)
1196 prefix = "LC_LOAD_DYLINKER";
1197 else if (command->type == BFD_MACH_O_LC_ID_DYLINKER)
1198 prefix = "LC_ID_DYLINKER";
1202 sname = bfd_alloc (abfd, strlen (prefix) + 1);
1205 strcpy (sname, prefix);
1207 bfdsec = bfd_make_section_anyway_with_flags (abfd, sname, SEC_HAS_CONTENTS);
1213 bfdsec->size = command->len - 8;
1214 bfdsec->filepos = command->offset + 8;
1215 bfdsec->alignment_power = 0;
1217 cmd->section = bfdsec;
1223 bfd_mach_o_scan_read_dylib (bfd *abfd, bfd_mach_o_load_command *command)
1225 bfd_mach_o_dylib_command *cmd = &command->command.dylib;
1226 unsigned char buf[16];
1227 unsigned int nameoff;
1232 BFD_ASSERT ((command->type == BFD_MACH_O_LC_ID_DYLIB)
1233 || (command->type == BFD_MACH_O_LC_LOAD_DYLIB)
1234 || (command->type == BFD_MACH_O_LC_LOAD_WEAK_DYLIB));
1236 bfd_seek (abfd, command->offset + 8, SEEK_SET);
1237 if (bfd_bread ((PTR) buf, 16, abfd) != 16)
1240 nameoff = bfd_h_get_32 (abfd, buf + 0);
1241 cmd->timestamp = bfd_h_get_32 (abfd, buf + 4);
1242 cmd->current_version = bfd_h_get_32 (abfd, buf + 8);
1243 cmd->compatibility_version = bfd_h_get_32 (abfd, buf + 12);
1245 cmd->name_offset = command->offset + nameoff;
1246 cmd->name_len = command->len - nameoff;
1248 if (command->type == BFD_MACH_O_LC_LOAD_DYLIB)
1249 prefix = "LC_LOAD_DYLIB";
1250 else if (command->type == BFD_MACH_O_LC_LOAD_WEAK_DYLIB)
1251 prefix = "LC_LOAD_WEAK_DYLIB";
1252 else if (command->type == BFD_MACH_O_LC_ID_DYLIB)
1253 prefix = "LC_ID_DYLIB";
1257 sname = bfd_alloc (abfd, strlen (prefix) + 1);
1260 strcpy (sname, prefix);
1262 bfdsec = bfd_make_section_anyway_with_flags (abfd, sname, SEC_HAS_CONTENTS);
1268 bfdsec->size = command->len - 8;
1269 bfdsec->filepos = command->offset + 8;
1270 bfdsec->alignment_power = 0;
1272 cmd->section = bfdsec;
1278 bfd_mach_o_scan_read_prebound_dylib (bfd *abfd ATTRIBUTE_UNUSED,
1279 bfd_mach_o_load_command *command ATTRIBUTE_UNUSED)
1281 /* bfd_mach_o_prebound_dylib_command *cmd = &command->command.prebound_dylib; */
1283 BFD_ASSERT (command->type == BFD_MACH_O_LC_PREBOUND_DYLIB);
1288 bfd_mach_o_scan_read_thread (bfd *abfd, bfd_mach_o_load_command *command)
1290 bfd_mach_o_data_struct *mdata = NULL;
1291 bfd_mach_o_thread_command *cmd = &command->command.thread;
1292 unsigned char buf[8];
1294 unsigned int nflavours;
1297 BFD_ASSERT ((command->type == BFD_MACH_O_LC_THREAD)
1298 || (command->type == BFD_MACH_O_LC_UNIXTHREAD));
1300 BFD_ASSERT (bfd_mach_o_valid (abfd));
1301 mdata = abfd->tdata.mach_o_data;
1305 while (offset != command->len)
1307 if (offset >= command->len)
1310 bfd_seek (abfd, command->offset + offset, SEEK_SET);
1312 if (bfd_bread ((PTR) buf, 8, abfd) != 8)
1315 offset += 8 + bfd_h_get_32 (abfd, buf + 4) * 4;
1319 cmd->flavours = bfd_alloc (abfd, nflavours * sizeof (bfd_mach_o_thread_flavour));
1320 if (cmd->flavours == NULL)
1322 cmd->nflavours = nflavours;
1326 while (offset != command->len)
1328 if (offset >= command->len)
1331 if (nflavours >= cmd->nflavours)
1334 bfd_seek (abfd, command->offset + offset, SEEK_SET);
1336 if (bfd_bread ((PTR) buf, 8, abfd) != 8)
1339 cmd->flavours[nflavours].flavour = bfd_h_get_32 (abfd, buf);
1340 cmd->flavours[nflavours].offset = command->offset + offset + 8;
1341 cmd->flavours[nflavours].size = bfd_h_get_32 (abfd, buf + 4) * 4;
1342 offset += cmd->flavours[nflavours].size + 8;
1346 for (i = 0; i < nflavours; i++)
1349 unsigned int snamelen;
1351 const char *flavourstr;
1352 const char *prefix = "LC_THREAD";
1355 switch (mdata->header.cputype)
1357 case BFD_MACH_O_CPU_TYPE_POWERPC:
1358 case BFD_MACH_O_CPU_TYPE_POWERPC_64:
1359 flavourstr = bfd_mach_o_ppc_flavour_string (cmd->flavours[i].flavour);
1361 case BFD_MACH_O_CPU_TYPE_I386:
1362 case BFD_MACH_O_CPU_TYPE_X86_64:
1363 flavourstr = bfd_mach_o_i386_flavour_string (cmd->flavours[i].flavour);
1366 flavourstr = "UNKNOWN_ARCHITECTURE";
1370 snamelen = strlen (prefix) + 1 + 20 + 1 + strlen (flavourstr) + 1;
1371 sname = bfd_alloc (abfd, snamelen);
1377 sprintf (sname, "%s.%s.%u", prefix, flavourstr, j);
1378 if (bfd_get_section_by_name (abfd, sname) == NULL)
1383 bfdsec = bfd_make_section_with_flags (abfd, sname, SEC_HAS_CONTENTS);
1387 bfdsec->size = cmd->flavours[i].size;
1388 bfdsec->filepos = cmd->flavours[i].offset;
1389 bfdsec->alignment_power = 0x0;
1391 cmd->section = bfdsec;
1398 bfd_mach_o_scan_read_dysymtab (bfd *abfd, bfd_mach_o_load_command *command)
1400 bfd_mach_o_dysymtab_command *seg = &command->command.dysymtab;
1401 unsigned char buf[72];
1403 BFD_ASSERT (command->type == BFD_MACH_O_LC_DYSYMTAB);
1405 bfd_seek (abfd, command->offset + 8, SEEK_SET);
1406 if (bfd_bread ((PTR) buf, 72, abfd) != 72)
1409 seg->ilocalsym = bfd_h_get_32 (abfd, buf + 0);
1410 seg->nlocalsym = bfd_h_get_32 (abfd, buf + 4);
1411 seg->iextdefsym = bfd_h_get_32 (abfd, buf + 8);
1412 seg->nextdefsym = bfd_h_get_32 (abfd, buf + 12);
1413 seg->iundefsym = bfd_h_get_32 (abfd, buf + 16);
1414 seg->nundefsym = bfd_h_get_32 (abfd, buf + 20);
1415 seg->tocoff = bfd_h_get_32 (abfd, buf + 24);
1416 seg->ntoc = bfd_h_get_32 (abfd, buf + 28);
1417 seg->modtaboff = bfd_h_get_32 (abfd, buf + 32);
1418 seg->nmodtab = bfd_h_get_32 (abfd, buf + 36);
1419 seg->extrefsymoff = bfd_h_get_32 (abfd, buf + 40);
1420 seg->nextrefsyms = bfd_h_get_32 (abfd, buf + 44);
1421 seg->indirectsymoff = bfd_h_get_32 (abfd, buf + 48);
1422 seg->nindirectsyms = bfd_h_get_32 (abfd, buf + 52);
1423 seg->extreloff = bfd_h_get_32 (abfd, buf + 56);
1424 seg->nextrel = bfd_h_get_32 (abfd, buf + 60);
1425 seg->locreloff = bfd_h_get_32 (abfd, buf + 64);
1426 seg->nlocrel = bfd_h_get_32 (abfd, buf + 68);
1432 bfd_mach_o_scan_read_symtab (bfd *abfd, bfd_mach_o_load_command *command)
1434 bfd_mach_o_symtab_command *seg = &command->command.symtab;
1435 unsigned char buf[16];
1438 const char *prefix = "LC_SYMTAB.stabs";
1439 int nlist_size = (bfd_mach_o_version (abfd) > 1) ? 16 : 12;
1441 BFD_ASSERT (command->type == BFD_MACH_O_LC_SYMTAB);
1443 bfd_seek (abfd, command->offset + 8, SEEK_SET);
1444 if (bfd_bread ((PTR) buf, 16, abfd) != 16)
1447 seg->symoff = bfd_h_get_32 (abfd, buf);
1448 seg->nsyms = bfd_h_get_32 (abfd, buf + 4);
1449 seg->stroff = bfd_h_get_32 (abfd, buf + 8);
1450 seg->strsize = bfd_h_get_32 (abfd, buf + 12);
1451 seg->symbols = NULL;
1454 sname = bfd_alloc (abfd, strlen (prefix) + 1);
1457 strcpy (sname, prefix);
1459 bfdsec = bfd_make_section_anyway_with_flags (abfd, sname, SEC_HAS_CONTENTS);
1465 bfdsec->size = seg->nsyms * nlist_size;
1466 bfdsec->filepos = seg->symoff;
1467 bfdsec->alignment_power = 0;
1469 seg->stabs_segment = bfdsec;
1471 if (seg->nsyms != 0)
1472 abfd->flags |= HAS_SYMS;
1474 prefix = "LC_SYMTAB.stabstr";
1475 sname = bfd_alloc (abfd, strlen (prefix) + 1);
1478 strcpy (sname, prefix);
1480 bfdsec = bfd_make_section_anyway_with_flags (abfd, sname, SEC_HAS_CONTENTS);
1486 bfdsec->size = seg->strsize;
1487 bfdsec->filepos = seg->stroff;
1488 bfdsec->alignment_power = 0;
1490 seg->stabstr_segment = bfdsec;
1496 bfd_mach_o_scan_read_uuid (bfd *abfd, bfd_mach_o_load_command *command)
1498 bfd_mach_o_uuid_command *cmd = &command->command.uuid;
1501 static const char prefix[] = "LC_UUID";
1503 BFD_ASSERT (command->type == BFD_MACH_O_LC_UUID);
1505 bfd_seek (abfd, command->offset + 8, SEEK_SET);
1506 if (bfd_bread ((PTR) cmd->uuid, 16, abfd) != 16)
1509 sname = bfd_alloc (abfd, strlen (prefix) + 1);
1512 strcpy (sname, prefix);
1514 bfdsec = bfd_make_section_anyway_with_flags (abfd, sname, SEC_HAS_CONTENTS);
1520 bfdsec->size = command->len - 8;
1521 bfdsec->filepos = command->offset + 8;
1522 bfdsec->alignment_power = 0;
1524 cmd->section = bfdsec;
1530 bfd_mach_o_scan_read_segment (bfd *abfd,
1531 bfd_mach_o_load_command *command,
1534 unsigned char buf[64];
1535 bfd_mach_o_segment_command *seg = &command->command.segment;
1539 const char *prefix = "LC_SEGMENT";
1540 unsigned int snamelen;
1544 BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT_64);
1546 bfd_seek (abfd, command->offset + 8, SEEK_SET);
1547 if (bfd_bread ((PTR) buf, 64, abfd) != 64)
1550 memcpy (seg->segname, buf, 16);
1551 seg->segname[16] = '\0';
1553 seg->vmaddr = bfd_h_get_64 (abfd, buf + 16);
1554 seg->vmsize = bfd_h_get_64 (abfd, buf + 24);
1555 seg->fileoff = bfd_h_get_64 (abfd, buf + 32);
1556 seg->filesize = bfd_h_get_64 (abfd, buf + 40);
1557 seg->maxprot = bfd_h_get_32 (abfd, buf + 48);
1558 seg->initprot = bfd_h_get_32 (abfd, buf + 52);
1559 seg->nsects = bfd_h_get_32 (abfd, buf + 56);
1560 seg->flags = bfd_h_get_32 (abfd, buf + 60);
1564 BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT);
1566 bfd_seek (abfd, command->offset + 8, SEEK_SET);
1567 if (bfd_bread ((PTR) buf, 48, abfd) != 48)
1570 memcpy (seg->segname, buf, 16);
1571 seg->segname[16] = '\0';
1573 seg->vmaddr = bfd_h_get_32 (abfd, buf + 16);
1574 seg->vmsize = bfd_h_get_32 (abfd, buf + 20);
1575 seg->fileoff = bfd_h_get_32 (abfd, buf + 24);
1576 seg->filesize = bfd_h_get_32 (abfd, buf + 28);
1577 seg->maxprot = bfd_h_get_32 (abfd, buf + 32);
1578 seg->initprot = bfd_h_get_32 (abfd, buf + 36);
1579 seg->nsects = bfd_h_get_32 (abfd, buf + 40);
1580 seg->flags = bfd_h_get_32 (abfd, buf + 44);
1583 snamelen = strlen (prefix) + 1 + strlen (seg->segname) + 1;
1584 sname = bfd_alloc (abfd, snamelen);
1587 sprintf (sname, "%s.%s", prefix, seg->segname);
1589 bfdsec = bfd_make_section_anyway (abfd, sname);
1593 bfdsec->vma = seg->vmaddr;
1594 bfdsec->lma = seg->vmaddr;
1595 bfdsec->size = seg->filesize;
1596 bfdsec->filepos = seg->fileoff;
1597 bfdsec->alignment_power = 0x0;
1598 bfdsec->flags = SEC_HAS_CONTENTS;
1599 bfdsec->segment_mark = 1;
1601 seg->segment = bfdsec;
1603 if (seg->nsects != 0)
1605 seg->sections = bfd_alloc (abfd, seg->nsects * sizeof (bfd_mach_o_section));
1606 if (seg->sections == NULL)
1609 for (i = 0; i < seg->nsects; i++)
1613 segoff = command->offset + 64 + 8 + (i * 80);
1615 segoff = command->offset + 48 + 8 + (i * 68);
1617 if (bfd_mach_o_scan_read_section
1618 (abfd, &seg->sections[i], segoff, seg->initprot, wide) != 0)
1627 bfd_mach_o_scan_read_segment_32 (bfd *abfd, bfd_mach_o_load_command *command)
1629 return bfd_mach_o_scan_read_segment (abfd, command, 0);
1633 bfd_mach_o_scan_read_segment_64 (bfd *abfd, bfd_mach_o_load_command *command)
1635 return bfd_mach_o_scan_read_segment (abfd, command, 1);
1639 bfd_mach_o_scan_read_command (bfd *abfd, bfd_mach_o_load_command *command)
1641 unsigned char buf[8];
1643 bfd_seek (abfd, command->offset, SEEK_SET);
1644 if (bfd_bread ((PTR) buf, 8, abfd) != 8)
1647 command->type = (bfd_h_get_32 (abfd, buf) & ~BFD_MACH_O_LC_REQ_DYLD);
1648 command->type_required = (bfd_h_get_32 (abfd, buf) & BFD_MACH_O_LC_REQ_DYLD
1650 command->len = bfd_h_get_32 (abfd, buf + 4);
1652 switch (command->type)
1654 case BFD_MACH_O_LC_SEGMENT:
1655 if (bfd_mach_o_scan_read_segment_32 (abfd, command) != 0)
1658 case BFD_MACH_O_LC_SEGMENT_64:
1659 if (bfd_mach_o_scan_read_segment_64 (abfd, command) != 0)
1662 case BFD_MACH_O_LC_SYMTAB:
1663 if (bfd_mach_o_scan_read_symtab (abfd, command) != 0)
1666 case BFD_MACH_O_LC_SYMSEG:
1668 case BFD_MACH_O_LC_THREAD:
1669 case BFD_MACH_O_LC_UNIXTHREAD:
1670 if (bfd_mach_o_scan_read_thread (abfd, command) != 0)
1673 case BFD_MACH_O_LC_LOAD_DYLINKER:
1674 case BFD_MACH_O_LC_ID_DYLINKER:
1675 if (bfd_mach_o_scan_read_dylinker (abfd, command) != 0)
1678 case BFD_MACH_O_LC_LOAD_DYLIB:
1679 case BFD_MACH_O_LC_ID_DYLIB:
1680 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB:
1681 if (bfd_mach_o_scan_read_dylib (abfd, command) != 0)
1684 case BFD_MACH_O_LC_PREBOUND_DYLIB:
1685 if (bfd_mach_o_scan_read_prebound_dylib (abfd, command) != 0)
1688 case BFD_MACH_O_LC_LOADFVMLIB:
1689 case BFD_MACH_O_LC_IDFVMLIB:
1690 case BFD_MACH_O_LC_IDENT:
1691 case BFD_MACH_O_LC_FVMFILE:
1692 case BFD_MACH_O_LC_PREPAGE:
1693 case BFD_MACH_O_LC_ROUTINES:
1694 case BFD_MACH_O_LC_SUB_FRAMEWORK:
1696 case BFD_MACH_O_LC_DYSYMTAB:
1697 if (bfd_mach_o_scan_read_dysymtab (abfd, command) != 0)
1700 case BFD_MACH_O_LC_SUB_UMBRELLA:
1701 case BFD_MACH_O_LC_SUB_CLIENT:
1702 case BFD_MACH_O_LC_SUB_LIBRARY:
1703 case BFD_MACH_O_LC_TWOLEVEL_HINTS:
1704 case BFD_MACH_O_LC_PREBIND_CKSUM:
1706 case BFD_MACH_O_LC_UUID:
1707 if (bfd_mach_o_scan_read_uuid (abfd, command) != 0)
1710 case BFD_MACH_O_LC_CODE_SIGNATURE:
1713 fprintf (stderr, "unable to read unknown load command 0x%lx\n",
1714 (unsigned long) command->type);
1722 bfd_mach_o_flatten_sections (bfd *abfd)
1724 bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
1728 /* Count total number of sections. */
1731 for (i = 0; i < mdata->header.ncmds; i++)
1733 if (mdata->commands[i].type == BFD_MACH_O_LC_SEGMENT
1734 || mdata->commands[i].type == BFD_MACH_O_LC_SEGMENT_64)
1736 bfd_mach_o_segment_command *seg;
1738 seg = &mdata->commands[i].command.segment;
1739 mdata->nsects += seg->nsects;
1743 /* Allocate sections array. */
1744 mdata->sections = bfd_alloc (abfd,
1745 mdata->nsects * sizeof (bfd_mach_o_section *));
1747 /* Fill the array. */
1750 for (i = 0; i < mdata->header.ncmds; i++)
1752 if (mdata->commands[i].type == BFD_MACH_O_LC_SEGMENT
1753 || mdata->commands[i].type == BFD_MACH_O_LC_SEGMENT_64)
1755 bfd_mach_o_segment_command *seg;
1757 seg = &mdata->commands[i].command.segment;
1758 BFD_ASSERT (csect + seg->nsects <= mdata->nsects);
1760 for (j = 0; j < seg->nsects; j++)
1761 mdata->sections[csect++] = &seg->sections[j];
1767 bfd_mach_o_scan_start_address (bfd *abfd)
1769 bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
1770 bfd_mach_o_thread_command *cmd = NULL;
1773 for (i = 0; i < mdata->header.ncmds; i++)
1775 if ((mdata->commands[i].type == BFD_MACH_O_LC_THREAD) ||
1776 (mdata->commands[i].type == BFD_MACH_O_LC_UNIXTHREAD))
1779 cmd = &mdata->commands[i].command.thread;
1788 for (i = 0; i < cmd->nflavours; i++)
1790 if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_I386)
1791 && (cmd->flavours[i].flavour
1792 == (unsigned long) BFD_MACH_O_x86_THREAD_STATE32))
1794 unsigned char buf[4];
1796 bfd_seek (abfd, cmd->flavours[i].offset + 40, SEEK_SET);
1798 if (bfd_bread (buf, 4, abfd) != 4)
1801 abfd->start_address = bfd_h_get_32 (abfd, buf);
1803 else if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_POWERPC)
1804 && (cmd->flavours[i].flavour == BFD_MACH_O_PPC_THREAD_STATE))
1806 unsigned char buf[4];
1808 bfd_seek (abfd, cmd->flavours[i].offset + 0, SEEK_SET);
1810 if (bfd_bread (buf, 4, abfd) != 4)
1813 abfd->start_address = bfd_h_get_32 (abfd, buf);
1815 else if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_POWERPC_64)
1816 && (cmd->flavours[i].flavour == BFD_MACH_O_PPC_THREAD_STATE_64))
1818 unsigned char buf[8];
1820 bfd_seek (abfd, cmd->flavours[i].offset + 0, SEEK_SET);
1822 if (bfd_bread (buf, 8, abfd) != 8)
1825 abfd->start_address = bfd_h_get_64 (abfd, buf);
1827 else if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_X86_64)
1828 && (cmd->flavours[i].flavour == BFD_MACH_O_x86_THREAD_STATE64))
1830 unsigned char buf[8];
1832 bfd_seek (abfd, cmd->flavours[i].offset + (16 * 8), SEEK_SET);
1834 if (bfd_bread (buf, 8, abfd) != 8)
1837 abfd->start_address = bfd_h_get_64 (abfd, buf);
1845 bfd_mach_o_scan (bfd *abfd,
1846 bfd_mach_o_header *header,
1847 bfd_mach_o_data_struct *mdata)
1850 enum bfd_architecture cputype;
1851 unsigned long cpusubtype;
1852 unsigned int hdrsize;
1854 hdrsize = (header->version == 2) ? 32 : 28;
1856 mdata->header = *header;
1857 mdata->symbols = NULL;
1859 abfd->flags = abfd->flags & (BFD_IN_MEMORY | BFD_IO_FUNCS);
1860 switch (header->filetype)
1862 case BFD_MACH_O_MH_OBJECT:
1863 abfd->flags |= HAS_RELOC;
1865 case BFD_MACH_O_MH_EXECUTE:
1866 abfd->flags |= EXEC_P;
1868 case BFD_MACH_O_MH_DYLIB:
1869 case BFD_MACH_O_MH_BUNDLE:
1870 abfd->flags |= DYNAMIC;
1874 abfd->tdata.mach_o_data = mdata;
1876 bfd_mach_o_convert_architecture (header->cputype, header->cpusubtype,
1877 &cputype, &cpusubtype);
1878 if (cputype == bfd_arch_unknown)
1880 fprintf (stderr, "bfd_mach_o_scan: unknown architecture 0x%lx/0x%lx\n",
1881 header->cputype, header->cpusubtype);
1885 bfd_set_arch_mach (abfd, cputype, cpusubtype);
1887 if (header->ncmds != 0)
1889 mdata->commands = bfd_alloc (abfd, header->ncmds * sizeof (bfd_mach_o_load_command));
1890 if (mdata->commands == NULL)
1893 for (i = 0; i < header->ncmds; i++)
1895 bfd_mach_o_load_command *cur = &mdata->commands[i];
1898 cur->offset = hdrsize;
1901 bfd_mach_o_load_command *prev = &mdata->commands[i - 1];
1902 cur->offset = prev->offset + prev->len;
1905 if (bfd_mach_o_scan_read_command (abfd, cur) < 0)
1910 if (bfd_mach_o_scan_start_address (abfd) < 0)
1913 bfd_mach_o_flatten_sections (abfd);
1918 bfd_mach_o_mkobject (bfd *abfd)
1920 bfd_mach_o_data_struct *mdata = NULL;
1922 mdata = bfd_alloc (abfd, sizeof (bfd_mach_o_data_struct));
1925 abfd->tdata.mach_o_data = mdata;
1927 mdata->header.magic = 0;
1928 mdata->header.cputype = 0;
1929 mdata->header.cpusubtype = 0;
1930 mdata->header.filetype = 0;
1931 mdata->header.ncmds = 0;
1932 mdata->header.sizeofcmds = 0;
1933 mdata->header.flags = 0;
1934 mdata->header.byteorder = BFD_ENDIAN_UNKNOWN;
1935 mdata->commands = NULL;
1936 mdata->nsymbols = 0;
1937 mdata->symbols = NULL;
1939 mdata->sections = NULL;
1946 bfd_mach_o_object_p (bfd *abfd)
1948 struct bfd_preserve preserve;
1949 bfd_mach_o_header header;
1951 preserve.marker = NULL;
1952 if (bfd_mach_o_read_header (abfd, &header) != 0)
1955 if (! (header.byteorder == BFD_ENDIAN_BIG
1956 || header.byteorder == BFD_ENDIAN_LITTLE))
1958 fprintf (stderr, "unknown header byte-order value 0x%lx\n",
1959 (unsigned long) header.byteorder);
1963 if (! ((header.byteorder == BFD_ENDIAN_BIG
1964 && abfd->xvec->byteorder == BFD_ENDIAN_BIG
1965 && abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
1966 || (header.byteorder == BFD_ENDIAN_LITTLE
1967 && abfd->xvec->byteorder == BFD_ENDIAN_LITTLE
1968 && abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)))
1971 preserve.marker = bfd_zalloc (abfd, sizeof (bfd_mach_o_data_struct));
1972 if (preserve.marker == NULL
1973 || !bfd_preserve_save (abfd, &preserve))
1976 if (bfd_mach_o_scan (abfd, &header,
1977 (bfd_mach_o_data_struct *) preserve.marker) != 0)
1980 bfd_preserve_finish (abfd, &preserve);
1984 bfd_set_error (bfd_error_wrong_format);
1987 if (preserve.marker != NULL)
1988 bfd_preserve_restore (abfd, &preserve);
1993 bfd_mach_o_core_p (bfd *abfd)
1995 struct bfd_preserve preserve;
1996 bfd_mach_o_header header;
1998 preserve.marker = NULL;
1999 if (bfd_mach_o_read_header (abfd, &header) != 0)
2002 if (! (header.byteorder == BFD_ENDIAN_BIG
2003 || header.byteorder == BFD_ENDIAN_LITTLE))
2005 fprintf (stderr, "unknown header byte-order value 0x%lx\n",
2006 (unsigned long) header.byteorder);
2010 if (! ((header.byteorder == BFD_ENDIAN_BIG
2011 && abfd->xvec->byteorder == BFD_ENDIAN_BIG
2012 && abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
2013 || (header.byteorder == BFD_ENDIAN_LITTLE
2014 && abfd->xvec->byteorder == BFD_ENDIAN_LITTLE
2015 && abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)))
2018 if (header.filetype != BFD_MACH_O_MH_CORE)
2021 preserve.marker = bfd_zalloc (abfd, sizeof (bfd_mach_o_data_struct));
2022 if (preserve.marker == NULL
2023 || !bfd_preserve_save (abfd, &preserve))
2026 if (bfd_mach_o_scan (abfd, &header,
2027 (bfd_mach_o_data_struct *) preserve.marker) != 0)
2030 bfd_preserve_finish (abfd, &preserve);
2034 bfd_set_error (bfd_error_wrong_format);
2037 if (preserve.marker != NULL)
2038 bfd_preserve_restore (abfd, &preserve);
2042 typedef struct mach_o_fat_archentry
2044 unsigned long cputype;
2045 unsigned long cpusubtype;
2046 unsigned long offset;
2048 unsigned long align;
2049 } mach_o_fat_archentry;
2051 typedef struct mach_o_fat_data_struct
2053 unsigned long magic;
2054 unsigned long nfat_arch;
2055 mach_o_fat_archentry *archentries;
2056 } mach_o_fat_data_struct;
2059 bfd_mach_o_archive_p (bfd *abfd)
2061 mach_o_fat_data_struct *adata = NULL;
2062 unsigned char buf[20];
2065 bfd_seek (abfd, 0, SEEK_SET);
2066 if (bfd_bread ((PTR) buf, 8, abfd) != 8)
2069 adata = bfd_alloc (abfd, sizeof (mach_o_fat_data_struct));
2073 adata->magic = bfd_getb32 (buf);
2074 adata->nfat_arch = bfd_getb32 (buf + 4);
2075 if (adata->magic != 0xcafebabe)
2078 adata->archentries =
2079 bfd_alloc (abfd, adata->nfat_arch * sizeof (mach_o_fat_archentry));
2080 if (adata->archentries == NULL)
2083 for (i = 0; i < adata->nfat_arch; i++)
2085 bfd_seek (abfd, 8 + 20 * i, SEEK_SET);
2087 if (bfd_bread ((PTR) buf, 20, abfd) != 20)
2089 adata->archentries[i].cputype = bfd_getb32 (buf);
2090 adata->archentries[i].cpusubtype = bfd_getb32 (buf + 4);
2091 adata->archentries[i].offset = bfd_getb32 (buf + 8);
2092 adata->archentries[i].size = bfd_getb32 (buf + 12);
2093 adata->archentries[i].align = bfd_getb32 (buf + 16);
2096 abfd->tdata.mach_o_fat_data = adata;
2101 bfd_release (abfd, adata);
2102 bfd_set_error (bfd_error_wrong_format);
2107 bfd_mach_o_openr_next_archived_file (bfd *archive, bfd *prev)
2109 mach_o_fat_data_struct *adata;
2110 mach_o_fat_archentry *entry = NULL;
2113 const char *arch_name;
2114 enum bfd_architecture arch_type;
2115 unsigned long arch_subtype;
2118 adata = (mach_o_fat_data_struct *) archive->tdata.mach_o_fat_data;
2119 BFD_ASSERT (adata != NULL);
2121 /* Find index of previous entry. */
2123 i = 0; /* Start at first one. */
2126 for (i = 0; i < adata->nfat_arch; i++)
2128 if (adata->archentries[i].offset == prev->origin)
2132 if (i == adata->nfat_arch)
2135 bfd_set_error (bfd_error_bad_value);
2138 i++; /* Get next entry. */
2141 if (i >= adata->nfat_arch)
2143 bfd_set_error (bfd_error_no_more_archived_files);
2147 entry = &adata->archentries[i];
2148 nbfd = _bfd_new_bfd_contained_in (archive);
2152 nbfd->origin = entry->offset;
2154 bfd_mach_o_convert_architecture (entry->cputype, entry->cpusubtype,
2155 &arch_type, &arch_subtype);
2156 arch_name = bfd_printable_arch_mach (arch_type, arch_subtype);
2157 s = bfd_malloc (strlen (arch_name) + 1);
2160 strcpy (s, arch_name);
2162 nbfd->iostream = NULL;
2168 bfd_mach_o_lookup_section (bfd *abfd,
2170 bfd_mach_o_load_command **mcommand,
2171 bfd_mach_o_section **msection)
2173 struct mach_o_data_struct *md = abfd->tdata.mach_o_data;
2174 unsigned int i, j, num;
2176 bfd_mach_o_load_command *ncmd = NULL;
2177 bfd_mach_o_section *nsect = NULL;
2179 BFD_ASSERT (mcommand != NULL);
2180 BFD_ASSERT (msection != NULL);
2183 for (i = 0; i < md->header.ncmds; i++)
2185 struct bfd_mach_o_load_command *cmd = &md->commands[i];
2186 struct bfd_mach_o_segment_command *seg = NULL;
2188 if (cmd->type != BFD_MACH_O_LC_SEGMENT
2189 || cmd->type != BFD_MACH_O_LC_SEGMENT_64)
2191 seg = &cmd->command.segment;
2193 if (seg->segment == section)
2200 for (j = 0; j < seg->nsects; j++)
2202 struct bfd_mach_o_section *sect = &seg->sections[j];
2204 if (sect->bfdsection == section)
2219 bfd_mach_o_lookup_command (bfd *abfd,
2220 bfd_mach_o_load_command_type type,
2221 bfd_mach_o_load_command **mcommand)
2223 struct mach_o_data_struct *md = NULL;
2224 bfd_mach_o_load_command *ncmd = NULL;
2225 unsigned int i, num;
2227 md = abfd->tdata.mach_o_data;
2229 BFD_ASSERT (md != NULL);
2230 BFD_ASSERT (mcommand != NULL);
2233 for (i = 0; i < md->header.ncmds; i++)
2235 struct bfd_mach_o_load_command *cmd = &md->commands[i];
2237 if (cmd->type != type)
2250 bfd_mach_o_stack_addr (enum bfd_mach_o_cpu_type type)
2254 case BFD_MACH_O_CPU_TYPE_MC680x0:
2256 case BFD_MACH_O_CPU_TYPE_MC88000:
2258 case BFD_MACH_O_CPU_TYPE_POWERPC:
2260 case BFD_MACH_O_CPU_TYPE_I386:
2262 case BFD_MACH_O_CPU_TYPE_SPARC:
2264 case BFD_MACH_O_CPU_TYPE_I860:
2266 case BFD_MACH_O_CPU_TYPE_HPPA:
2267 return 0xc0000000 - 0x04000000;
2274 bfd_mach_o_bfd_print_private_bfd_data (bfd *abfd, PTR ptr)
2276 bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
2277 FILE *file = (FILE *) ptr;
2279 unsigned int sec_nbr = 0;
2281 fprintf (file, _("Segments and Sections:\n"));
2282 fprintf (file, _(" #: Segment name Section name Address\n"));
2284 for (i = 0; i < mdata->header.ncmds; i++)
2286 bfd_mach_o_segment_command *seg;
2288 if (mdata->commands[i].type != BFD_MACH_O_LC_SEGMENT
2289 && mdata->commands[i].type != BFD_MACH_O_LC_SEGMENT_64)
2292 seg = &mdata->commands[i].command.segment;
2294 fprintf (file, "[Segment %-16s ", seg->segname);
2295 fprintf_vma (file, seg->vmaddr);
2296 fprintf (file, "-");
2297 fprintf_vma (file, seg->vmaddr + seg->vmsize - 1);
2299 fputc (seg->initprot & BFD_MACH_O_PROT_READ ? 'r' : '-', file);
2300 fputc (seg->initprot & BFD_MACH_O_PROT_WRITE ? 'w' : '-', file);
2301 fputc (seg->initprot & BFD_MACH_O_PROT_EXECUTE ? 'x' : '-', file);
2302 fprintf (file, "]\n");
2303 for (j = 0; j < seg->nsects; j++)
2305 bfd_mach_o_section *sec = &seg->sections[j];
2306 fprintf (file, "%02u: %-16s %-16s ", ++sec_nbr,
2307 sec->segname, sec->sectname);
2308 fprintf_vma (file, sec->addr);
2309 fprintf (file, " ");
2310 fprintf_vma (file, sec->size);
2311 fprintf (file, " %08lx\n", sec->flags);
2315 for (i = 0; i < mdata->header.ncmds; i++)
2317 bfd_mach_o_load_command *cmd = &mdata->commands[i];
2321 case BFD_MACH_O_LC_SEGMENT:
2322 case BFD_MACH_O_LC_SEGMENT_64:
2324 case BFD_MACH_O_LC_UUID:
2326 bfd_mach_o_uuid_command *uuid = &cmd->command.uuid;
2331 for (i = 0; i < sizeof (uuid->uuid); i++)
2332 fprintf (file, " %02x", uuid->uuid[i]);
2336 case BFD_MACH_O_LC_LOAD_DYLIB:
2338 bfd_mach_o_dylib_command *dylib = &cmd->command.dylib;
2339 bfd_byte *data = NULL;
2341 if (! bfd_malloc_and_get_section (abfd, dylib->section, &data))
2349 data + dylib->name_offset - cmd->offset - 8);
2350 fprintf (file, " time stamp: 0x%08lx\n",
2352 fprintf (file, " current version: 0x%08lx\n",
2353 dylib->current_version);
2354 fprintf (file, " comptibility version: 0x%08lx\n",
2355 dylib->compatibility_version);
2359 case BFD_MACH_O_LC_LOAD_DYLINKER:
2361 bfd_mach_o_dylinker_command *linker = &cmd->command.dylinker;
2362 bfd_byte *data = NULL;
2364 if (! bfd_malloc_and_get_section (abfd, linker->section, &data))
2371 "LOAD_DYLINKER: %s\n",
2372 data + linker->name_offset - cmd->offset - 8);
2376 case BFD_MACH_O_LC_SYMTAB:
2378 bfd_mach_o_symtab_command *symtab = &cmd->command.symtab;
2380 "LC_SYMTAB: nsyms: %lu, strsize: %lu\n",
2381 symtab->nsyms, symtab->strsize);
2384 case BFD_MACH_O_LC_DYSYMTAB:
2386 bfd_mach_o_dysymtab_command *dysymtab = &cmd->command.dysymtab;
2389 " local symbols: index: %lu number: %lu\n",
2390 dysymtab->ilocalsym, dysymtab->nlocalsym);
2392 " external symbols: index: %lu number: %lu\n",
2393 dysymtab->iextdefsym, dysymtab->nextdefsym);
2395 " undefined symbols: index: %lu number: %lu\n",
2396 dysymtab->iundefsym, dysymtab->nundefsym);
2398 " ntoc: offset: %lu number: %lu\n",
2399 dysymtab->tocoff, dysymtab->ntoc);
2401 " module table: offset: %lu number: %lu\n",
2402 dysymtab->modtaboff, dysymtab->nmodtab);
2406 fprintf (file, "LC_%d\n", cmd->type);
2415 bfd_mach_o_core_fetch_environment (bfd *abfd,
2416 unsigned char **rbuf,
2419 bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
2420 unsigned long stackaddr = bfd_mach_o_stack_addr (mdata->header.cputype);
2423 for (i = 0; i < mdata->header.ncmds; i++)
2425 bfd_mach_o_load_command *cur = &mdata->commands[i];
2426 bfd_mach_o_segment_command *seg = NULL;
2428 if (cur->type != BFD_MACH_O_LC_SEGMENT)
2431 seg = &cur->command.segment;
2433 if ((seg->vmaddr + seg->vmsize) == stackaddr)
2435 unsigned long start = seg->fileoff;
2436 unsigned long end = seg->fileoff + seg->filesize;
2437 unsigned char *buf = bfd_malloc (1024);
2438 unsigned long size = 1024;
2442 bfd_size_type nread = 0;
2443 unsigned long offset;
2444 int found_nonnull = 0;
2446 if (size > (end - start))
2447 size = (end - start);
2449 buf = bfd_realloc_or_free (buf, size);
2453 bfd_seek (abfd, end - size, SEEK_SET);
2454 nread = bfd_bread (buf, size, abfd);
2462 for (offset = 4; offset <= size; offset += 4)
2466 val = *((unsigned long *) (buf + size - offset));
2467 if (! found_nonnull)
2472 else if (val == 0x0)
2474 unsigned long bottom;
2477 bottom = seg->fileoff + seg->filesize - offset;
2478 top = seg->fileoff + seg->filesize - 4;
2479 *rbuf = bfd_malloc (top - bottom);
2480 *rlen = top - bottom;
2482 memcpy (*rbuf, buf + size - *rlen, *rlen);
2488 if (size == (end - start))
2502 bfd_mach_o_core_file_failing_command (bfd *abfd)
2504 unsigned char *buf = NULL;
2505 unsigned int len = 0;
2508 ret = bfd_mach_o_core_fetch_environment (abfd, &buf, &len);
2512 return (char *) buf;
2516 bfd_mach_o_core_file_failing_signal (bfd *abfd ATTRIBUTE_UNUSED)
2521 #define TARGET_NAME mach_o_be_vec
2522 #define TARGET_STRING "mach-o-be"
2523 #define TARGET_BIG_ENDIAN 1
2524 #define TARGET_ARCHIVE 0
2526 #include "mach-o-target.c"
2529 #undef TARGET_STRING
2530 #undef TARGET_BIG_ENDIAN
2531 #undef TARGET_ARCHIVE
2533 #define TARGET_NAME mach_o_le_vec
2534 #define TARGET_STRING "mach-o-le"
2535 #define TARGET_BIG_ENDIAN 0
2536 #define TARGET_ARCHIVE 0
2538 #include "mach-o-target.c"
2541 #undef TARGET_STRING
2542 #undef TARGET_BIG_ENDIAN
2543 #undef TARGET_ARCHIVE
2545 #define TARGET_NAME mach_o_fat_vec
2546 #define TARGET_STRING "mach-o-fat"
2547 #define TARGET_BIG_ENDIAN 1
2548 #define TARGET_ARCHIVE 1
2550 #include "mach-o-target.c"
2553 #undef TARGET_STRING
2554 #undef TARGET_BIG_ENDIAN
2555 #undef TARGET_ARCHIVE