1 /* Mach-O support for BFD.
2 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
26 #include "libiberty.h"
27 #include "aout/stab_gnu.h"
30 #define bfd_mach_o_object_p bfd_mach_o_gen_object_p
31 #define bfd_mach_o_core_p bfd_mach_o_gen_core_p
32 #define bfd_mach_o_mkobject bfd_false
35 bfd_mach_o_version (bfd
*abfd
)
37 bfd_mach_o_data_struct
*mdata
= NULL
;
39 BFD_ASSERT (bfd_mach_o_valid (abfd
));
40 mdata
= abfd
->tdata
.mach_o_data
;
42 return mdata
->header
.version
;
46 bfd_mach_o_valid (bfd
*abfd
)
48 if (abfd
== NULL
|| abfd
->xvec
== NULL
)
51 if (abfd
->xvec
->flavour
!= bfd_target_mach_o_flavour
)
54 if (abfd
->tdata
.mach_o_data
== NULL
)
59 /* Tables to translate well known Mach-O segment/section names to bfd
60 names. Use of canonical names (such as .text or .debug_frame) is required
63 struct mach_o_section_name_xlat
66 const char *mach_o_name
;
69 static const struct mach_o_section_name_xlat dwarf_section_names_xlat
[] =
71 { ".debug_frame", "__debug_frame" },
72 { ".debug_info", "__debug_info" },
73 { ".debug_abbrev", "__debug_abbrev" },
74 { ".debug_aranges", "__debug_aranges" },
75 { ".debug_macinfo", "__debug_macinfo" },
76 { ".debug_line", "__debug_line" },
77 { ".debug_loc", "__debug_loc" },
78 { ".debug_pubnames", "__debug_pubnames" },
79 { ".debug_pubtypes", "__debug_pubtypes" },
80 { ".debug_str", "__debug_str" },
81 { ".debug_ranges", "__debug_ranges" },
85 static const struct mach_o_section_name_xlat text_section_names_xlat
[] =
87 { ".text", "__text" },
88 { ".cstring", "__cstring" },
89 { ".eh_frame", "__eh_frame" },
93 static const struct mach_o_section_name_xlat data_section_names_xlat
[] =
95 { ".data", "__data" },
100 struct mach_o_segment_name_xlat
103 const struct mach_o_section_name_xlat
*sections
;
106 static const struct mach_o_segment_name_xlat segsec_names_xlat
[] =
108 { "__DWARF", dwarf_section_names_xlat
},
109 { "__TEXT", text_section_names_xlat
},
110 { "__DATA", data_section_names_xlat
},
115 /* Mach-O to bfd names. */
118 bfd_mach_o_convert_section_name_to_bfd (bfd
*abfd
, bfd_mach_o_section
*section
)
120 const struct mach_o_segment_name_xlat
*seg
;
124 for (seg
= segsec_names_xlat
; seg
->segname
; seg
++)
126 if (strcmp (seg
->segname
, section
->segname
) == 0)
128 const struct mach_o_section_name_xlat
*sec
;
130 for (sec
= seg
->sections
; sec
->mach_o_name
; sec
++)
132 if (strcmp (sec
->mach_o_name
, section
->sectname
) == 0)
134 len
= strlen (sec
->bfd_name
);
135 res
= bfd_alloc (abfd
, len
+ 1);
139 strcpy (res
, sec
->bfd_name
);
146 len
= sizeof ("LC_SEGMENT") - 1 + 1
147 + strlen (section
->segname
) + 1
148 + strlen (section
->sectname
) + 1;
150 res
= bfd_alloc (abfd
, len
);
153 snprintf (res
, len
, "LC_SEGMENT.%s.%s", section
->segname
, section
->sectname
);
157 /* Convert a bfd sectio name to a Mach-O segment + section name. */
160 bfd_mach_o_convert_section_name_to_mach_o (bfd
*abfd ATTRIBUTE_UNUSED
,
162 bfd_mach_o_section
*section
)
164 const struct mach_o_segment_name_xlat
*seg
;
165 const char *name
= bfd_get_section_name (abfd
, sect
);
171 /* List of well known names. */
172 for (seg
= segsec_names_xlat
; seg
->segname
; seg
++)
174 const struct mach_o_section_name_xlat
*sec
;
176 for (sec
= seg
->sections
; sec
->mach_o_name
; sec
++)
178 if (strcmp (sec
->bfd_name
, name
) == 0)
180 strcpy (section
->segname
, seg
->segname
);
181 strcpy (section
->sectname
, sec
->mach_o_name
);
187 /* Strip LC_SEGMENT. prefix. */
188 if (strncmp (name
, "LC_SEGMENT.", 11) == 0)
192 dot
= strchr (name
, '.');
195 /* Try to split name into segment and section names. */
196 if (dot
&& dot
!= name
)
199 seclen
= len
- (dot
+ 1 - name
);
201 if (seglen
< 16 && seclen
< 16)
203 memcpy (section
->segname
, name
, seglen
);
204 section
->segname
[seglen
] = 0;
205 memcpy (section
->sectname
, dot
+ 1, seclen
);
206 section
->sectname
[seclen
] = 0;
213 memcpy (section
->segname
, name
, len
);
214 section
->segname
[len
] = 0;
215 memcpy (section
->sectname
, name
, len
);
216 section
->sectname
[len
] = 0;
219 /* Copy any private info we understand from the input symbol
220 to the output symbol. */
223 bfd_mach_o_bfd_copy_private_symbol_data (bfd
*ibfd ATTRIBUTE_UNUSED
,
224 asymbol
*isymbol ATTRIBUTE_UNUSED
,
225 bfd
*obfd ATTRIBUTE_UNUSED
,
226 asymbol
*osymbol ATTRIBUTE_UNUSED
)
231 /* Copy any private info we understand from the input section
232 to the output section. */
235 bfd_mach_o_bfd_copy_private_section_data (bfd
*ibfd ATTRIBUTE_UNUSED
,
236 asection
*isection ATTRIBUTE_UNUSED
,
237 bfd
*obfd ATTRIBUTE_UNUSED
,
238 asection
*osection ATTRIBUTE_UNUSED
)
243 /* Copy any private info we understand from the input bfd
244 to the output bfd. */
247 bfd_mach_o_bfd_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
249 if (bfd_get_flavour (ibfd
) != bfd_target_mach_o_flavour
250 || bfd_get_flavour (obfd
) != bfd_target_mach_o_flavour
)
253 BFD_ASSERT (bfd_mach_o_valid (ibfd
));
254 BFD_ASSERT (bfd_mach_o_valid (obfd
));
256 /* FIXME: copy commands. */
261 /* Count the total number of symbols. Traverse all sections. */
264 bfd_mach_o_count_symbols (bfd
*abfd
)
266 bfd_mach_o_data_struct
*mdata
= NULL
;
270 BFD_ASSERT (bfd_mach_o_valid (abfd
));
271 mdata
= abfd
->tdata
.mach_o_data
;
273 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
274 if (mdata
->commands
[i
].type
== BFD_MACH_O_LC_SYMTAB
)
276 bfd_mach_o_symtab_command
*sym
= &mdata
->commands
[i
].command
.symtab
;
284 bfd_mach_o_get_symtab_upper_bound (bfd
*abfd
)
286 long nsyms
= bfd_mach_o_count_symbols (abfd
);
291 return ((nsyms
+ 1) * sizeof (asymbol
*));
295 bfd_mach_o_canonicalize_symtab (bfd
*abfd
, asymbol
**alocation
)
297 bfd_mach_o_data_struct
*mdata
= abfd
->tdata
.mach_o_data
;
298 long nsyms
= bfd_mach_o_count_symbols (abfd
);
299 asymbol
**csym
= alocation
;
305 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
307 if (mdata
->commands
[i
].type
== BFD_MACH_O_LC_SYMTAB
)
309 bfd_mach_o_symtab_command
*sym
= &mdata
->commands
[i
].command
.symtab
;
311 if (bfd_mach_o_scan_read_symtab_symbols (abfd
, &mdata
->commands
[i
].command
.symtab
) != 0)
313 fprintf (stderr
, "bfd_mach_o_canonicalize_symtab: unable to load symbols for section %lu\n", i
);
317 BFD_ASSERT (sym
->symbols
!= NULL
);
319 for (j
= 0; j
< sym
->nsyms
; j
++)
321 BFD_ASSERT (csym
< (alocation
+ nsyms
));
322 *csym
++ = &sym
->symbols
[j
];
333 bfd_mach_o_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
337 bfd_symbol_info (symbol
, ret
);
341 bfd_mach_o_print_symbol (bfd
*abfd
,
344 bfd_print_symbol_type how
)
346 FILE *file
= (FILE *) afile
;
354 case bfd_print_symbol_name
:
355 fprintf (file
, "%s", symbol
->name
);
358 bfd_print_symbol_vandf (abfd
, (PTR
) file
, symbol
);
359 ntype
= BFD_MACH_O_SYM_NTYPE (symbol
);
360 nsect
= BFD_MACH_O_SYM_NSECT (symbol
);
361 ndesc
= BFD_MACH_O_SYM_NDESC (symbol
);
362 if (ntype
& BFD_MACH_O_N_STAB
)
363 name
= bfd_get_stab_name (ntype
);
365 switch (ntype
& BFD_MACH_O_N_TYPE
)
367 case BFD_MACH_O_N_UNDF
:
370 case BFD_MACH_O_N_ABS
:
373 case BFD_MACH_O_N_INDR
:
376 case BFD_MACH_O_N_PBUD
:
379 case BFD_MACH_O_N_SECT
:
388 fprintf (file
, " %02x %-6s %02x %04x", ntype
, name
, nsect
, ndesc
);
389 if ((ntype
& BFD_MACH_O_N_STAB
) == 0
390 && (ntype
& BFD_MACH_O_N_TYPE
) == BFD_MACH_O_N_SECT
)
391 fprintf (file
, " %-5s", symbol
->section
->name
);
392 fprintf (file
, " %s", symbol
->name
);
397 bfd_mach_o_convert_architecture (bfd_mach_o_cpu_type mtype
,
398 bfd_mach_o_cpu_subtype msubtype ATTRIBUTE_UNUSED
,
399 enum bfd_architecture
*type
,
400 unsigned long *subtype
)
402 *subtype
= bfd_arch_unknown
;
406 case BFD_MACH_O_CPU_TYPE_VAX
: *type
= bfd_arch_vax
; break;
407 case BFD_MACH_O_CPU_TYPE_MC680x0
: *type
= bfd_arch_m68k
; break;
408 case BFD_MACH_O_CPU_TYPE_I386
:
409 *type
= bfd_arch_i386
;
410 *subtype
= bfd_mach_i386_i386
;
412 case BFD_MACH_O_CPU_TYPE_X86_64
:
413 *type
= bfd_arch_i386
;
414 *subtype
= bfd_mach_x86_64
;
416 case BFD_MACH_O_CPU_TYPE_MIPS
: *type
= bfd_arch_mips
; break;
417 case BFD_MACH_O_CPU_TYPE_MC98000
: *type
= bfd_arch_m98k
; break;
418 case BFD_MACH_O_CPU_TYPE_HPPA
: *type
= bfd_arch_hppa
; break;
419 case BFD_MACH_O_CPU_TYPE_ARM
: *type
= bfd_arch_arm
; break;
420 case BFD_MACH_O_CPU_TYPE_MC88000
: *type
= bfd_arch_m88k
; break;
421 case BFD_MACH_O_CPU_TYPE_SPARC
:
422 *type
= bfd_arch_sparc
;
423 *subtype
= bfd_mach_sparc
;
425 case BFD_MACH_O_CPU_TYPE_I860
: *type
= bfd_arch_i860
; break;
426 case BFD_MACH_O_CPU_TYPE_ALPHA
: *type
= bfd_arch_alpha
; break;
427 case BFD_MACH_O_CPU_TYPE_POWERPC
:
428 *type
= bfd_arch_powerpc
;
429 *subtype
= bfd_mach_ppc
;
431 case BFD_MACH_O_CPU_TYPE_POWERPC_64
:
432 *type
= bfd_arch_powerpc
;
433 *subtype
= bfd_mach_ppc64
;
436 *type
= bfd_arch_unknown
;
442 bfd_mach_o_write_header (bfd
*abfd
, bfd_mach_o_header
*header
)
444 unsigned char buf
[32];
447 size
= (header
->version
== 2) ?
448 BFD_MACH_O_HEADER_64_SIZE
: BFD_MACH_O_HEADER_SIZE
;
450 bfd_h_put_32 (abfd
, header
->magic
, buf
+ 0);
451 bfd_h_put_32 (abfd
, header
->cputype
, buf
+ 4);
452 bfd_h_put_32 (abfd
, header
->cpusubtype
, buf
+ 8);
453 bfd_h_put_32 (abfd
, header
->filetype
, buf
+ 12);
454 bfd_h_put_32 (abfd
, header
->ncmds
, buf
+ 16);
455 bfd_h_put_32 (abfd
, header
->sizeofcmds
, buf
+ 20);
456 bfd_h_put_32 (abfd
, header
->flags
, buf
+ 24);
458 if (header
->version
== 2)
459 bfd_h_put_32 (abfd
, header
->reserved
, buf
+ 28);
461 bfd_seek (abfd
, 0, SEEK_SET
);
462 if (bfd_bwrite ((PTR
) buf
, size
, abfd
) != size
)
469 bfd_mach_o_scan_write_thread (bfd
*abfd
, bfd_mach_o_load_command
*command
)
471 bfd_mach_o_thread_command
*cmd
= &command
->command
.thread
;
473 unsigned char buf
[8];
475 unsigned int nflavours
;
477 BFD_ASSERT ((command
->type
== BFD_MACH_O_LC_THREAD
)
478 || (command
->type
== BFD_MACH_O_LC_UNIXTHREAD
));
482 for (i
= 0; i
< cmd
->nflavours
; i
++)
484 BFD_ASSERT ((cmd
->flavours
[i
].size
% 4) == 0);
485 BFD_ASSERT (cmd
->flavours
[i
].offset
== (command
->offset
+ offset
+ 8));
487 bfd_h_put_32 (abfd
, cmd
->flavours
[i
].flavour
, buf
);
488 bfd_h_put_32 (abfd
, (cmd
->flavours
[i
].size
/ 4), buf
+ 4);
490 bfd_seek (abfd
, command
->offset
+ offset
, SEEK_SET
);
491 if (bfd_bwrite ((PTR
) buf
, 8, abfd
) != 8)
494 offset
+= cmd
->flavours
[i
].size
+ 8;
501 bfd_mach_o_scan_write_section_32 (bfd
*abfd
,
502 bfd_mach_o_section
*section
,
505 unsigned char buf
[BFD_MACH_O_SECTION_SIZE
];
507 memcpy (buf
, section
->sectname
, 16);
508 memcpy (buf
+ 16, section
->segname
, 16);
509 bfd_h_put_32 (abfd
, section
->addr
, buf
+ 32);
510 bfd_h_put_32 (abfd
, section
->size
, buf
+ 36);
511 bfd_h_put_32 (abfd
, section
->offset
, buf
+ 40);
512 bfd_h_put_32 (abfd
, section
->align
, buf
+ 44);
513 bfd_h_put_32 (abfd
, section
->reloff
, buf
+ 48);
514 bfd_h_put_32 (abfd
, section
->nreloc
, buf
+ 52);
515 bfd_h_put_32 (abfd
, section
->flags
, buf
+ 56);
516 bfd_h_put_32 (abfd
, section
->reserved1
, buf
+ 60);
517 bfd_h_put_32 (abfd
, section
->reserved2
, buf
+ 64);
519 bfd_seek (abfd
, offset
, SEEK_SET
);
520 if (bfd_bwrite ((PTR
) buf
, BFD_MACH_O_SECTION_SIZE
, abfd
)
521 != BFD_MACH_O_SECTION_SIZE
)
528 bfd_mach_o_scan_write_section_64 (bfd
*abfd
,
529 bfd_mach_o_section
*section
,
532 unsigned char buf
[BFD_MACH_O_SECTION_64_SIZE
];
534 memcpy (buf
, section
->sectname
, 16);
535 memcpy (buf
+ 16, section
->segname
, 16);
536 bfd_h_put_64 (abfd
, section
->addr
, buf
+ 32);
537 bfd_h_put_64 (abfd
, section
->size
, buf
+ 40);
538 bfd_h_put_32 (abfd
, section
->offset
, buf
+ 48);
539 bfd_h_put_32 (abfd
, section
->align
, buf
+ 52);
540 bfd_h_put_32 (abfd
, section
->reloff
, buf
+ 56);
541 bfd_h_put_32 (abfd
, section
->nreloc
, buf
+ 60);
542 bfd_h_put_32 (abfd
, section
->flags
, buf
+ 64);
543 bfd_h_put_32 (abfd
, section
->reserved1
, buf
+ 68);
544 bfd_h_put_32 (abfd
, section
->reserved2
, buf
+ 72);
545 bfd_h_put_32 (abfd
, section
->reserved3
, buf
+ 76);
547 bfd_seek (abfd
, offset
, SEEK_SET
);
548 if (bfd_bwrite ((PTR
) buf
, BFD_MACH_O_SECTION_64_SIZE
, abfd
)
549 != BFD_MACH_O_SECTION_64_SIZE
)
556 bfd_mach_o_scan_write_section (bfd
*abfd
,
557 bfd_mach_o_section
*section
,
562 return bfd_mach_o_scan_write_section_64 (abfd
, section
, offset
);
564 return bfd_mach_o_scan_write_section_32 (abfd
, section
, offset
);
568 bfd_mach_o_scan_write_segment (bfd
*abfd
,
569 bfd_mach_o_load_command
*command
,
572 unsigned char buf
[64];
573 bfd_mach_o_segment_command
*seg
= &command
->command
.segment
;
578 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SEGMENT_64
);
580 memcpy (buf
, seg
->segname
, 16);
582 bfd_h_put_64 (abfd
, seg
->vmaddr
, buf
+ 16);
583 bfd_h_put_64 (abfd
, seg
->vmsize
, buf
+ 24);
584 bfd_h_put_64 (abfd
, seg
->fileoff
, buf
+ 32);
585 bfd_h_put_64 (abfd
, seg
->filesize
, buf
+ 40);
586 bfd_h_put_32 (abfd
, seg
->maxprot
, buf
+ 48);
587 bfd_h_put_32 (abfd
, seg
->initprot
, buf
+ 52);
588 bfd_h_put_32 (abfd
, seg
->nsects
, buf
+ 56);
589 bfd_h_put_32 (abfd
, seg
->flags
, buf
+ 60);
591 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
592 if (bfd_bwrite ((PTR
) buf
, 64, abfd
) != 64)
597 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SEGMENT
);
599 memcpy (buf
, seg
->segname
, 16);
601 bfd_h_put_32 (abfd
, seg
->vmaddr
, buf
+ 16);
602 bfd_h_put_32 (abfd
, seg
->vmsize
, buf
+ 20);
603 bfd_h_put_32 (abfd
, seg
->fileoff
, buf
+ 24);
604 bfd_h_put_32 (abfd
, seg
->filesize
, buf
+ 28);
605 bfd_h_put_32 (abfd
, seg
->maxprot
, buf
+ 32);
606 bfd_h_put_32 (abfd
, seg
->initprot
, buf
+ 36);
607 bfd_h_put_32 (abfd
, seg
->nsects
, buf
+ 40);
608 bfd_h_put_32 (abfd
, seg
->flags
, buf
+ 44);
610 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
611 if (bfd_bwrite ((PTR
) buf
, 48, abfd
) != 48)
615 for (i
= 0; i
< seg
->nsects
; i
++)
619 segoff
= command
->offset
+ BFD_MACH_O_LC_SEGMENT_64_SIZE
620 + (i
* BFD_MACH_O_SECTION_64_SIZE
);
622 segoff
= command
->offset
+ BFD_MACH_O_LC_SEGMENT_SIZE
623 + (i
* BFD_MACH_O_SECTION_SIZE
);
625 if (bfd_mach_o_scan_write_section
626 (abfd
, &seg
->sections
[i
], segoff
, wide
) != 0)
634 bfd_mach_o_scan_write_segment_32 (bfd
*abfd
, bfd_mach_o_load_command
*command
)
636 return bfd_mach_o_scan_write_segment (abfd
, command
, 0);
640 bfd_mach_o_scan_write_segment_64 (bfd
*abfd
, bfd_mach_o_load_command
*command
)
642 return bfd_mach_o_scan_write_segment (abfd
, command
, 1);
646 bfd_mach_o_scan_write_symtab_symbols (bfd
*abfd
, bfd_mach_o_load_command
*command
)
648 bfd_mach_o_symtab_command
*sym
= &command
->command
.symtab
;
652 for (i
= 0; i
< sym
->nsyms
; i
++)
654 unsigned char buf
[12];
655 bfd_vma symoff
= sym
->symoff
+ (i
* 12);
656 unsigned char ntype
= 0;
657 unsigned char nsect
= 0;
660 s
= &sym
->symbols
[i
];
662 /* Instead just set from the stored values. */
663 ntype
= BFD_MACH_O_SYM_NTYPE (s
);
664 nsect
= BFD_MACH_O_SYM_NSECT (s
);
665 ndesc
= BFD_MACH_O_SYM_NDESC (s
);
667 bfd_h_put_32 (abfd
, s
->name
- sym
->strtab
, buf
);
668 bfd_h_put_8 (abfd
, ntype
, buf
+ 4);
669 bfd_h_put_8 (abfd
, nsect
, buf
+ 5);
670 bfd_h_put_16 (abfd
, ndesc
, buf
+ 6);
671 bfd_h_put_32 (abfd
, s
->section
->vma
+ s
->value
, buf
+ 8);
673 bfd_seek (abfd
, symoff
, SEEK_SET
);
674 if (bfd_bwrite ((PTR
) buf
, 12, abfd
) != 12)
676 fprintf (stderr
, "bfd_mach_o_scan_write_symtab_symbols: unable to write %d bytes at %lu\n",
677 12, (unsigned long) symoff
);
686 bfd_mach_o_scan_write_symtab (bfd
*abfd
, bfd_mach_o_load_command
*command
)
688 bfd_mach_o_symtab_command
*seg
= &command
->command
.symtab
;
689 unsigned char buf
[16];
691 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SYMTAB
);
693 bfd_h_put_32 (abfd
, seg
->symoff
, buf
);
694 bfd_h_put_32 (abfd
, seg
->nsyms
, buf
+ 4);
695 bfd_h_put_32 (abfd
, seg
->stroff
, buf
+ 8);
696 bfd_h_put_32 (abfd
, seg
->strsize
, buf
+ 12);
698 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
699 if (bfd_bwrite ((PTR
) buf
, 16, abfd
) != 16)
702 if (bfd_mach_o_scan_write_symtab_symbols (abfd
, command
) != 0)
709 bfd_mach_o_write_contents (bfd
*abfd
)
712 bfd_mach_o_data_struct
*mdata
= abfd
->tdata
.mach_o_data
;
714 /* Now write header information. */
715 if (!bfd_mach_o_write_header (abfd
, &mdata
->header
))
718 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
720 unsigned char buf
[8];
721 bfd_mach_o_load_command
*cur
= &mdata
->commands
[i
];
722 unsigned long typeflag
;
724 typeflag
= cur
->type
| (cur
->type_required
? BFD_MACH_O_LC_REQ_DYLD
: 0);
726 bfd_h_put_32 (abfd
, typeflag
, buf
);
727 bfd_h_put_32 (abfd
, cur
->len
, buf
+ 4);
729 bfd_seek (abfd
, cur
->offset
, SEEK_SET
);
730 if (bfd_bwrite ((PTR
) buf
, 8, abfd
) != 8)
735 case BFD_MACH_O_LC_SEGMENT
:
736 if (bfd_mach_o_scan_write_segment_32 (abfd
, cur
) != 0)
739 case BFD_MACH_O_LC_SEGMENT_64
:
740 if (bfd_mach_o_scan_write_segment_64 (abfd
, cur
) != 0)
743 case BFD_MACH_O_LC_SYMTAB
:
744 if (bfd_mach_o_scan_write_symtab (abfd
, cur
) != 0)
747 case BFD_MACH_O_LC_SYMSEG
:
749 case BFD_MACH_O_LC_THREAD
:
750 case BFD_MACH_O_LC_UNIXTHREAD
:
751 if (bfd_mach_o_scan_write_thread (abfd
, cur
) != 0)
754 case BFD_MACH_O_LC_LOADFVMLIB
:
755 case BFD_MACH_O_LC_IDFVMLIB
:
756 case BFD_MACH_O_LC_IDENT
:
757 case BFD_MACH_O_LC_FVMFILE
:
758 case BFD_MACH_O_LC_PREPAGE
:
759 case BFD_MACH_O_LC_DYSYMTAB
:
760 case BFD_MACH_O_LC_LOAD_DYLIB
:
761 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB
:
762 case BFD_MACH_O_LC_ID_DYLIB
:
763 case BFD_MACH_O_LC_LOAD_DYLINKER
:
764 case BFD_MACH_O_LC_ID_DYLINKER
:
765 case BFD_MACH_O_LC_PREBOUND_DYLIB
:
766 case BFD_MACH_O_LC_ROUTINES
:
767 case BFD_MACH_O_LC_SUB_FRAMEWORK
:
771 "unable to write unknown load command 0x%lx\n",
772 (unsigned long) cur
->type
);
780 /* Build Mach-O load commands from the sections. */
783 bfd_mach_o_build_commands (bfd
*abfd
)
785 bfd_mach_o_data_struct
*mdata
= bfd_get_mach_o_data (abfd
);
786 unsigned int wide
= (mdata
->header
.version
== 2);
787 bfd_mach_o_segment_command
*seg
;
788 bfd_mach_o_section
*sections
;
792 /* Return now if commands are already built. */
793 if (mdata
->header
.ncmds
)
796 /* Very simple version: 1 command (segment) containing all sections. */
797 mdata
->header
.ncmds
= 1;
798 mdata
->commands
= bfd_alloc (abfd
, mdata
->header
.ncmds
799 * sizeof (bfd_mach_o_load_command
));
800 if (mdata
->commands
== NULL
)
802 seg
= &mdata
->commands
[0].command
.segment
;
803 seg
->nsects
= bfd_count_sections (abfd
);
804 sections
= bfd_alloc (abfd
, seg
->nsects
* sizeof (bfd_mach_o_section
));
805 if (sections
== NULL
)
807 seg
->sections
= sections
;
809 /* Set segment command. */
812 mdata
->commands
[0].type
= BFD_MACH_O_LC_SEGMENT_64
;
813 mdata
->commands
[0].offset
= BFD_MACH_O_HEADER_64_SIZE
;
814 mdata
->commands
[0].len
= BFD_MACH_O_LC_SEGMENT_64_SIZE
815 + BFD_MACH_O_SECTION_64_SIZE
* seg
->nsects
;
819 mdata
->commands
[0].type
= BFD_MACH_O_LC_SEGMENT
;
820 mdata
->commands
[0].offset
= BFD_MACH_O_HEADER_SIZE
;
821 mdata
->commands
[0].len
= BFD_MACH_O_LC_SEGMENT_SIZE
822 + BFD_MACH_O_SECTION_SIZE
* seg
->nsects
;
824 mdata
->commands
[0].type_required
= FALSE
;
825 mdata
->header
.sizeofcmds
= mdata
->commands
[0].len
;
827 filepos
= mdata
->commands
[0].offset
+ mdata
->commands
[0].len
;
829 memset (seg
->segname
, 0, sizeof (seg
->segname
));
831 seg
->fileoff
= filepos
;
833 seg
->maxprot
= BFD_MACH_O_PROT_READ
| BFD_MACH_O_PROT_WRITE
834 | BFD_MACH_O_PROT_EXECUTE
;
835 seg
->initprot
= seg
->maxprot
;
838 /* Create Mach-O sections. */
839 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
841 sections
->bfdsection
= sec
;
842 bfd_mach_o_convert_section_name_to_mach_o (abfd
, sec
, sections
);
843 sections
->addr
= bfd_get_section_vma (abfd
, sec
);
844 sections
->size
= bfd_get_section_size (sec
);
845 sections
->align
= bfd_get_section_alignment (abfd
, sec
);
847 filepos
= (filepos
+ ((file_ptr
) 1 << sections
->align
) - 1)
848 & ((file_ptr
) -1 << sections
->align
);
849 sections
->offset
= filepos
;
850 sections
->reloff
= 0;
851 sections
->nreloc
= 0;
852 sections
->reserved1
= 0;
853 sections
->reserved2
= 0;
854 sections
->reserved3
= 0;
856 sec
->filepos
= filepos
;
858 filepos
+= sections
->size
;
861 seg
->filesize
= filepos
- seg
->fileoff
;
862 seg
->vmsize
= seg
->filesize
;
867 /* Set the contents of a section. */
870 bfd_mach_o_set_section_contents (bfd
*abfd
,
872 const void * location
,
878 /* This must be done first, because bfd_set_section_contents is
879 going to set output_has_begun to TRUE. */
880 if (! abfd
->output_has_begun
&& ! bfd_mach_o_build_commands (abfd
))
886 pos
= section
->filepos
+ offset
;
887 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
888 || bfd_bwrite (location
, count
, abfd
) != count
)
895 bfd_mach_o_sizeof_headers (bfd
*a ATTRIBUTE_UNUSED
,
896 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
901 /* Make an empty symbol. This is required only because
902 bfd_make_section_anyway wants to create a symbol for the section. */
905 bfd_mach_o_make_empty_symbol (bfd
*abfd
)
909 new = bfd_zalloc (abfd
, sizeof (* new));
917 bfd_mach_o_read_header (bfd
*abfd
, bfd_mach_o_header
*header
)
919 unsigned char buf
[32];
921 bfd_vma (*get32
) (const void *) = NULL
;
923 /* Just read the magic number. */
924 bfd_seek (abfd
, 0, SEEK_SET
);
925 if (bfd_bread ((PTR
) buf
, 4, abfd
) != 4)
928 if (bfd_getb32 (buf
) == BFD_MACH_O_MH_MAGIC
)
930 header
->byteorder
= BFD_ENDIAN_BIG
;
931 header
->magic
= BFD_MACH_O_MH_MAGIC
;
935 else if (bfd_getl32 (buf
) == BFD_MACH_O_MH_MAGIC
)
937 header
->byteorder
= BFD_ENDIAN_LITTLE
;
938 header
->magic
= BFD_MACH_O_MH_MAGIC
;
942 else if (bfd_getb32 (buf
) == BFD_MACH_O_MH_MAGIC_64
)
944 header
->byteorder
= BFD_ENDIAN_BIG
;
945 header
->magic
= BFD_MACH_O_MH_MAGIC_64
;
949 else if (bfd_getl32 (buf
) == BFD_MACH_O_MH_MAGIC_64
)
951 header
->byteorder
= BFD_ENDIAN_LITTLE
;
952 header
->magic
= BFD_MACH_O_MH_MAGIC_64
;
958 header
->byteorder
= BFD_ENDIAN_UNKNOWN
;
962 /* Once the size of the header is known, read the full header. */
963 size
= (header
->version
== 2) ?
964 BFD_MACH_O_HEADER_64_SIZE
: BFD_MACH_O_HEADER_SIZE
;
966 bfd_seek (abfd
, 0, SEEK_SET
);
967 if (bfd_bread ((PTR
) buf
, size
, abfd
) != size
)
970 header
->cputype
= (*get32
) (buf
+ 4);
971 header
->cpusubtype
= (*get32
) (buf
+ 8);
972 header
->filetype
= (*get32
) (buf
+ 12);
973 header
->ncmds
= (*get32
) (buf
+ 16);
974 header
->sizeofcmds
= (*get32
) (buf
+ 20);
975 header
->flags
= (*get32
) (buf
+ 24);
977 if (header
->version
== 2)
978 header
->reserved
= (*get32
) (buf
+ 28);
984 bfd_mach_o_make_bfd_section (bfd
*abfd
, bfd_mach_o_section
*section
,
991 sname
= bfd_mach_o_convert_section_name_to_bfd (abfd
, section
);
995 if (section
->flags
& BFD_MACH_O_S_ATTR_DEBUG
)
996 flags
= SEC_HAS_CONTENTS
| SEC_DEBUGGING
;
1000 if ((section
->flags
& BFD_MACH_O_SECTION_TYPE_MASK
)
1001 != BFD_MACH_O_S_ZEROFILL
)
1003 flags
|= SEC_HAS_CONTENTS
| SEC_LOAD
;
1004 if (prot
& BFD_MACH_O_PROT_EXECUTE
)
1006 if (prot
& BFD_MACH_O_PROT_WRITE
)
1008 else if (prot
& BFD_MACH_O_PROT_READ
)
1009 flags
|= SEC_READONLY
;
1012 bfdsec
= bfd_make_section_anyway_with_flags (abfd
, sname
, flags
);
1016 bfdsec
->vma
= section
->addr
;
1017 bfdsec
->lma
= section
->addr
;
1018 bfdsec
->size
= section
->size
;
1019 bfdsec
->filepos
= section
->offset
;
1020 bfdsec
->alignment_power
= section
->align
;
1021 bfdsec
->segment_mark
= 0;
1027 bfd_mach_o_scan_read_section_32 (bfd
*abfd
,
1028 bfd_mach_o_section
*section
,
1032 unsigned char buf
[BFD_MACH_O_SECTION_SIZE
];
1034 bfd_seek (abfd
, offset
, SEEK_SET
);
1035 if (bfd_bread ((PTR
) buf
, BFD_MACH_O_SECTION_SIZE
, abfd
)
1036 != BFD_MACH_O_SECTION_SIZE
)
1039 memcpy (section
->sectname
, buf
, 16);
1040 section
->sectname
[16] = '\0';
1041 memcpy (section
->segname
, buf
+ 16, 16);
1042 section
->segname
[16] = '\0';
1043 section
->addr
= bfd_h_get_32 (abfd
, buf
+ 32);
1044 section
->size
= bfd_h_get_32 (abfd
, buf
+ 36);
1045 section
->offset
= bfd_h_get_32 (abfd
, buf
+ 40);
1046 section
->align
= bfd_h_get_32 (abfd
, buf
+ 44);
1047 section
->reloff
= bfd_h_get_32 (abfd
, buf
+ 48);
1048 section
->nreloc
= bfd_h_get_32 (abfd
, buf
+ 52);
1049 section
->flags
= bfd_h_get_32 (abfd
, buf
+ 56);
1050 section
->reserved1
= bfd_h_get_32 (abfd
, buf
+ 60);
1051 section
->reserved2
= bfd_h_get_32 (abfd
, buf
+ 64);
1052 section
->reserved3
= 0;
1053 section
->bfdsection
= bfd_mach_o_make_bfd_section (abfd
, section
, prot
);
1055 if (section
->bfdsection
== NULL
)
1062 bfd_mach_o_scan_read_section_64 (bfd
*abfd
,
1063 bfd_mach_o_section
*section
,
1067 unsigned char buf
[BFD_MACH_O_SECTION_64_SIZE
];
1069 bfd_seek (abfd
, offset
, SEEK_SET
);
1070 if (bfd_bread ((PTR
) buf
, BFD_MACH_O_SECTION_64_SIZE
, abfd
)
1071 != BFD_MACH_O_SECTION_64_SIZE
)
1074 memcpy (section
->sectname
, buf
, 16);
1075 section
->sectname
[16] = '\0';
1076 memcpy (section
->segname
, buf
+ 16, 16);
1077 section
->segname
[16] = '\0';
1078 section
->addr
= bfd_h_get_64 (abfd
, buf
+ 32);
1079 section
->size
= bfd_h_get_64 (abfd
, buf
+ 40);
1080 section
->offset
= bfd_h_get_32 (abfd
, buf
+ 48);
1081 section
->align
= bfd_h_get_32 (abfd
, buf
+ 52);
1082 section
->reloff
= bfd_h_get_32 (abfd
, buf
+ 56);
1083 section
->nreloc
= bfd_h_get_32 (abfd
, buf
+ 60);
1084 section
->flags
= bfd_h_get_32 (abfd
, buf
+ 64);
1085 section
->reserved1
= bfd_h_get_32 (abfd
, buf
+ 68);
1086 section
->reserved2
= bfd_h_get_32 (abfd
, buf
+ 72);
1087 section
->reserved3
= bfd_h_get_32 (abfd
, buf
+ 76);
1088 section
->bfdsection
= bfd_mach_o_make_bfd_section (abfd
, section
, prot
);
1090 if (section
->bfdsection
== NULL
)
1097 bfd_mach_o_scan_read_section (bfd
*abfd
,
1098 bfd_mach_o_section
*section
,
1104 return bfd_mach_o_scan_read_section_64 (abfd
, section
, offset
, prot
);
1106 return bfd_mach_o_scan_read_section_32 (abfd
, section
, offset
, prot
);
1110 bfd_mach_o_scan_read_symtab_symbol (bfd
*abfd
,
1111 bfd_mach_o_symtab_command
*sym
,
1115 bfd_mach_o_data_struct
*mdata
= abfd
->tdata
.mach_o_data
;
1116 unsigned int wide
= (mdata
->header
.version
== 2);
1117 unsigned int symwidth
= wide
? 16 : 12;
1118 bfd_vma symoff
= sym
->symoff
+ (i
* symwidth
);
1119 unsigned char buf
[16];
1120 unsigned char type
= -1;
1121 unsigned char section
= -1;
1123 symvalue value
= -1;
1124 unsigned long stroff
= -1;
1125 unsigned int symtype
= -1;
1127 BFD_ASSERT (sym
->strtab
!= NULL
);
1129 bfd_seek (abfd
, symoff
, SEEK_SET
);
1130 if (bfd_bread ((PTR
) buf
, symwidth
, abfd
) != symwidth
)
1132 fprintf (stderr
, "bfd_mach_o_scan_read_symtab_symbol: unable to read %d bytes at %lu\n",
1133 symwidth
, (unsigned long) symoff
);
1137 stroff
= bfd_h_get_32 (abfd
, buf
);
1138 type
= bfd_h_get_8 (abfd
, buf
+ 4);
1139 symtype
= (type
& 0x0e);
1140 section
= bfd_h_get_8 (abfd
, buf
+ 5);
1141 desc
= bfd_h_get_16 (abfd
, buf
+ 6);
1143 value
= bfd_h_get_64 (abfd
, buf
+ 8);
1145 value
= bfd_h_get_32 (abfd
, buf
+ 8);
1147 if (stroff
>= sym
->strsize
)
1149 fprintf (stderr
, "bfd_mach_o_scan_read_symtab_symbol: symbol name out of range (%lu >= %lu)\n",
1150 (unsigned long) stroff
, (unsigned long) sym
->strsize
);
1155 s
->name
= sym
->strtab
+ stroff
;
1157 s
->udata
.i
= (type
<< 24) | (section
<< 16) | desc
;
1160 if (type
& BFD_MACH_O_N_STAB
)
1162 s
->flags
|= BSF_DEBUGGING
;
1163 s
->section
= bfd_und_section_ptr
;
1175 if ((section
> 0) && (section
<= mdata
->nsects
))
1177 s
->section
= mdata
->sections
[section
- 1]->bfdsection
;
1178 s
->value
= s
->value
- mdata
->sections
[section
- 1]->addr
;
1185 if (type
& BFD_MACH_O_N_PEXT
)
1186 s
->flags
|= BSF_GLOBAL
;
1188 if (type
& BFD_MACH_O_N_EXT
)
1189 s
->flags
|= BSF_GLOBAL
;
1191 if (!(type
& (BFD_MACH_O_N_PEXT
| BFD_MACH_O_N_EXT
)))
1192 s
->flags
|= BSF_LOCAL
;
1196 case BFD_MACH_O_N_UNDF
:
1197 s
->section
= bfd_und_section_ptr
;
1199 case BFD_MACH_O_N_PBUD
:
1200 s
->section
= bfd_und_section_ptr
;
1202 case BFD_MACH_O_N_ABS
:
1203 s
->section
= bfd_abs_section_ptr
;
1205 case BFD_MACH_O_N_SECT
:
1206 if ((section
> 0) && (section
<= mdata
->nsects
))
1208 s
->section
= mdata
->sections
[section
- 1]->bfdsection
;
1209 s
->value
= s
->value
- mdata
->sections
[section
- 1]->addr
;
1213 /* Mach-O uses 0 to mean "no section"; not an error. */
1216 fprintf (stderr
, "bfd_mach_o_scan_read_symtab_symbol: "
1217 "symbol \"%s\" specified invalid section %d (max %lu): setting to undefined\n",
1218 s
->name
, section
, mdata
->nsects
);
1220 s
->section
= bfd_und_section_ptr
;
1223 case BFD_MACH_O_N_INDR
:
1224 fprintf (stderr
, "bfd_mach_o_scan_read_symtab_symbol: "
1225 "symbol \"%s\" is unsupported 'indirect' reference: setting to undefined\n",
1227 s
->section
= bfd_und_section_ptr
;
1230 fprintf (stderr
, "bfd_mach_o_scan_read_symtab_symbol: "
1231 "symbol \"%s\" specified invalid type field 0x%x: setting to undefined\n",
1233 s
->section
= bfd_und_section_ptr
;
1242 bfd_mach_o_scan_read_symtab_strtab (bfd
*abfd
,
1243 bfd_mach_o_symtab_command
*sym
)
1245 BFD_ASSERT (sym
->strtab
== NULL
);
1247 if (abfd
->flags
& BFD_IN_MEMORY
)
1249 struct bfd_in_memory
*b
;
1251 b
= (struct bfd_in_memory
*) abfd
->iostream
;
1253 if ((sym
->stroff
+ sym
->strsize
) > b
->size
)
1255 bfd_set_error (bfd_error_file_truncated
);
1258 sym
->strtab
= (char *) b
->buffer
+ sym
->stroff
;
1262 sym
->strtab
= bfd_alloc (abfd
, sym
->strsize
);
1263 if (sym
->strtab
== NULL
)
1266 bfd_seek (abfd
, sym
->stroff
, SEEK_SET
);
1267 if (bfd_bread ((PTR
) sym
->strtab
, sym
->strsize
, abfd
) != sym
->strsize
)
1269 fprintf (stderr
, "bfd_mach_o_scan_read_symtab_strtab: unable to read %lu bytes at %lu\n",
1270 sym
->strsize
, sym
->stroff
);
1278 bfd_mach_o_scan_read_symtab_symbols (bfd
*abfd
,
1279 bfd_mach_o_symtab_command
*sym
)
1284 BFD_ASSERT (sym
->symbols
== NULL
);
1285 sym
->symbols
= bfd_alloc (abfd
, sym
->nsyms
* sizeof (asymbol
));
1287 if (sym
->symbols
== NULL
)
1289 fprintf (stderr
, "bfd_mach_o_scan_read_symtab_symbols: unable to allocate memory for symbols\n");
1293 ret
= bfd_mach_o_scan_read_symtab_strtab (abfd
, sym
);
1297 for (i
= 0; i
< sym
->nsyms
; i
++)
1299 ret
= bfd_mach_o_scan_read_symtab_symbol (abfd
, sym
, &sym
->symbols
[i
], i
);
1308 bfd_mach_o_scan_read_dysymtab_symbol (bfd
*abfd
,
1309 bfd_mach_o_dysymtab_command
*dysym
,
1310 bfd_mach_o_symtab_command
*sym
,
1314 unsigned long isymoff
= dysym
->indirectsymoff
+ (i
* 4);
1315 unsigned long symindex
;
1316 unsigned char buf
[4];
1318 BFD_ASSERT (i
< dysym
->nindirectsyms
);
1320 bfd_seek (abfd
, isymoff
, SEEK_SET
);
1321 if (bfd_bread ((PTR
) buf
, 4, abfd
) != 4)
1323 fprintf (stderr
, "bfd_mach_o_scan_read_dysymtab_symbol: unable to read %lu bytes at %lu\n",
1324 (unsigned long) 4, isymoff
);
1327 symindex
= bfd_h_get_32 (abfd
, buf
);
1329 return bfd_mach_o_scan_read_symtab_symbol (abfd
, sym
, s
, symindex
);
1333 bfd_mach_o_i386_flavour_string (unsigned int flavour
)
1335 switch ((int) flavour
)
1337 case BFD_MACH_O_x86_THREAD_STATE32
: return "x86_THREAD_STATE32";
1338 case BFD_MACH_O_x86_FLOAT_STATE32
: return "x86_FLOAT_STATE32";
1339 case BFD_MACH_O_x86_EXCEPTION_STATE32
: return "x86_EXCEPTION_STATE32";
1340 case BFD_MACH_O_x86_THREAD_STATE64
: return "x86_THREAD_STATE64";
1341 case BFD_MACH_O_x86_FLOAT_STATE64
: return "x86_FLOAT_STATE64";
1342 case BFD_MACH_O_x86_EXCEPTION_STATE64
: return "x86_EXCEPTION_STATE64";
1343 case BFD_MACH_O_x86_THREAD_STATE
: return "x86_THREAD_STATE";
1344 case BFD_MACH_O_x86_FLOAT_STATE
: return "x86_FLOAT_STATE";
1345 case BFD_MACH_O_x86_EXCEPTION_STATE
: return "x86_EXCEPTION_STATE";
1346 case BFD_MACH_O_x86_DEBUG_STATE32
: return "x86_DEBUG_STATE32";
1347 case BFD_MACH_O_x86_DEBUG_STATE64
: return "x86_DEBUG_STATE64";
1348 case BFD_MACH_O_x86_DEBUG_STATE
: return "x86_DEBUG_STATE";
1349 case BFD_MACH_O_THREAD_STATE_NONE
: return "THREAD_STATE_NONE";
1350 default: return "UNKNOWN";
1355 bfd_mach_o_ppc_flavour_string (unsigned int flavour
)
1357 switch ((int) flavour
)
1359 case BFD_MACH_O_PPC_THREAD_STATE
: return "PPC_THREAD_STATE";
1360 case BFD_MACH_O_PPC_FLOAT_STATE
: return "PPC_FLOAT_STATE";
1361 case BFD_MACH_O_PPC_EXCEPTION_STATE
: return "PPC_EXCEPTION_STATE";
1362 case BFD_MACH_O_PPC_VECTOR_STATE
: return "PPC_VECTOR_STATE";
1363 default: return "UNKNOWN";
1368 bfd_mach_o_scan_read_dylinker (bfd
*abfd
,
1369 bfd_mach_o_load_command
*command
)
1371 bfd_mach_o_dylinker_command
*cmd
= &command
->command
.dylinker
;
1372 unsigned char buf
[4];
1373 unsigned int nameoff
;
1378 BFD_ASSERT ((command
->type
== BFD_MACH_O_LC_ID_DYLINKER
)
1379 || (command
->type
== BFD_MACH_O_LC_LOAD_DYLINKER
));
1381 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
1382 if (bfd_bread ((PTR
) buf
, 4, abfd
) != 4)
1385 nameoff
= bfd_h_get_32 (abfd
, buf
+ 0);
1387 cmd
->name_offset
= command
->offset
+ nameoff
;
1388 cmd
->name_len
= command
->len
- nameoff
;
1390 if (command
->type
== BFD_MACH_O_LC_LOAD_DYLINKER
)
1391 prefix
= "LC_LOAD_DYLINKER";
1392 else if (command
->type
== BFD_MACH_O_LC_ID_DYLINKER
)
1393 prefix
= "LC_ID_DYLINKER";
1397 sname
= bfd_alloc (abfd
, strlen (prefix
) + 1);
1400 strcpy (sname
, prefix
);
1402 bfdsec
= bfd_make_section_anyway_with_flags (abfd
, sname
, SEC_HAS_CONTENTS
);
1408 bfdsec
->size
= command
->len
- nameoff
;
1409 bfdsec
->filepos
= command
->offset
+ nameoff
;
1410 bfdsec
->alignment_power
= 0;
1412 cmd
->section
= bfdsec
;
1418 bfd_mach_o_scan_read_dylib (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1420 bfd_mach_o_dylib_command
*cmd
= &command
->command
.dylib
;
1421 unsigned char buf
[16];
1422 unsigned int nameoff
;
1427 BFD_ASSERT ((command
->type
== BFD_MACH_O_LC_ID_DYLIB
)
1428 || (command
->type
== BFD_MACH_O_LC_LOAD_DYLIB
)
1429 || (command
->type
== BFD_MACH_O_LC_LOAD_WEAK_DYLIB
));
1431 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
1432 if (bfd_bread ((PTR
) buf
, 16, abfd
) != 16)
1435 nameoff
= bfd_h_get_32 (abfd
, buf
+ 0);
1436 cmd
->timestamp
= bfd_h_get_32 (abfd
, buf
+ 4);
1437 cmd
->current_version
= bfd_h_get_32 (abfd
, buf
+ 8);
1438 cmd
->compatibility_version
= bfd_h_get_32 (abfd
, buf
+ 12);
1440 cmd
->name_offset
= command
->offset
+ nameoff
;
1441 cmd
->name_len
= command
->len
- nameoff
;
1443 if (command
->type
== BFD_MACH_O_LC_LOAD_DYLIB
)
1444 prefix
= "LC_LOAD_DYLIB";
1445 else if (command
->type
== BFD_MACH_O_LC_LOAD_WEAK_DYLIB
)
1446 prefix
= "LC_LOAD_WEAK_DYLIB";
1447 else if (command
->type
== BFD_MACH_O_LC_ID_DYLIB
)
1448 prefix
= "LC_ID_DYLIB";
1452 sname
= bfd_alloc (abfd
, strlen (prefix
) + 1);
1455 strcpy (sname
, prefix
);
1457 bfdsec
= bfd_make_section_anyway_with_flags (abfd
, sname
, SEC_HAS_CONTENTS
);
1463 bfdsec
->size
= command
->len
- 8;
1464 bfdsec
->filepos
= command
->offset
+ 8;
1465 bfdsec
->alignment_power
= 0;
1467 cmd
->section
= bfdsec
;
1473 bfd_mach_o_scan_read_prebound_dylib (bfd
*abfd ATTRIBUTE_UNUSED
,
1474 bfd_mach_o_load_command
*command ATTRIBUTE_UNUSED
)
1476 /* bfd_mach_o_prebound_dylib_command *cmd = &command->command.prebound_dylib; */
1478 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_PREBOUND_DYLIB
);
1483 bfd_mach_o_scan_read_thread (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1485 bfd_mach_o_data_struct
*mdata
= NULL
;
1486 bfd_mach_o_thread_command
*cmd
= &command
->command
.thread
;
1487 unsigned char buf
[8];
1489 unsigned int nflavours
;
1492 BFD_ASSERT ((command
->type
== BFD_MACH_O_LC_THREAD
)
1493 || (command
->type
== BFD_MACH_O_LC_UNIXTHREAD
));
1495 BFD_ASSERT (bfd_mach_o_valid (abfd
));
1496 mdata
= abfd
->tdata
.mach_o_data
;
1500 while (offset
!= command
->len
)
1502 if (offset
>= command
->len
)
1505 bfd_seek (abfd
, command
->offset
+ offset
, SEEK_SET
);
1507 if (bfd_bread ((PTR
) buf
, 8, abfd
) != 8)
1510 offset
+= 8 + bfd_h_get_32 (abfd
, buf
+ 4) * 4;
1514 cmd
->flavours
= bfd_alloc (abfd
, nflavours
* sizeof (bfd_mach_o_thread_flavour
));
1515 if (cmd
->flavours
== NULL
)
1517 cmd
->nflavours
= nflavours
;
1521 while (offset
!= command
->len
)
1523 if (offset
>= command
->len
)
1526 if (nflavours
>= cmd
->nflavours
)
1529 bfd_seek (abfd
, command
->offset
+ offset
, SEEK_SET
);
1531 if (bfd_bread ((PTR
) buf
, 8, abfd
) != 8)
1534 cmd
->flavours
[nflavours
].flavour
= bfd_h_get_32 (abfd
, buf
);
1535 cmd
->flavours
[nflavours
].offset
= command
->offset
+ offset
+ 8;
1536 cmd
->flavours
[nflavours
].size
= bfd_h_get_32 (abfd
, buf
+ 4) * 4;
1537 offset
+= cmd
->flavours
[nflavours
].size
+ 8;
1541 for (i
= 0; i
< nflavours
; i
++)
1544 unsigned int snamelen
;
1546 const char *flavourstr
;
1547 const char *prefix
= "LC_THREAD";
1550 switch (mdata
->header
.cputype
)
1552 case BFD_MACH_O_CPU_TYPE_POWERPC
:
1553 case BFD_MACH_O_CPU_TYPE_POWERPC_64
:
1554 flavourstr
= bfd_mach_o_ppc_flavour_string (cmd
->flavours
[i
].flavour
);
1556 case BFD_MACH_O_CPU_TYPE_I386
:
1557 case BFD_MACH_O_CPU_TYPE_X86_64
:
1558 flavourstr
= bfd_mach_o_i386_flavour_string (cmd
->flavours
[i
].flavour
);
1561 flavourstr
= "UNKNOWN_ARCHITECTURE";
1565 snamelen
= strlen (prefix
) + 1 + 20 + 1 + strlen (flavourstr
) + 1;
1566 sname
= bfd_alloc (abfd
, snamelen
);
1572 sprintf (sname
, "%s.%s.%u", prefix
, flavourstr
, j
);
1573 if (bfd_get_section_by_name (abfd
, sname
) == NULL
)
1578 bfdsec
= bfd_make_section_with_flags (abfd
, sname
, SEC_HAS_CONTENTS
);
1582 bfdsec
->size
= cmd
->flavours
[i
].size
;
1583 bfdsec
->filepos
= cmd
->flavours
[i
].offset
;
1584 bfdsec
->alignment_power
= 0x0;
1586 cmd
->section
= bfdsec
;
1593 bfd_mach_o_scan_read_dysymtab (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1595 bfd_mach_o_dysymtab_command
*seg
= &command
->command
.dysymtab
;
1596 unsigned char buf
[72];
1598 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_DYSYMTAB
);
1600 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
1601 if (bfd_bread ((PTR
) buf
, 72, abfd
) != 72)
1604 seg
->ilocalsym
= bfd_h_get_32 (abfd
, buf
+ 0);
1605 seg
->nlocalsym
= bfd_h_get_32 (abfd
, buf
+ 4);
1606 seg
->iextdefsym
= bfd_h_get_32 (abfd
, buf
+ 8);
1607 seg
->nextdefsym
= bfd_h_get_32 (abfd
, buf
+ 12);
1608 seg
->iundefsym
= bfd_h_get_32 (abfd
, buf
+ 16);
1609 seg
->nundefsym
= bfd_h_get_32 (abfd
, buf
+ 20);
1610 seg
->tocoff
= bfd_h_get_32 (abfd
, buf
+ 24);
1611 seg
->ntoc
= bfd_h_get_32 (abfd
, buf
+ 28);
1612 seg
->modtaboff
= bfd_h_get_32 (abfd
, buf
+ 32);
1613 seg
->nmodtab
= bfd_h_get_32 (abfd
, buf
+ 36);
1614 seg
->extrefsymoff
= bfd_h_get_32 (abfd
, buf
+ 40);
1615 seg
->nextrefsyms
= bfd_h_get_32 (abfd
, buf
+ 44);
1616 seg
->indirectsymoff
= bfd_h_get_32 (abfd
, buf
+ 48);
1617 seg
->nindirectsyms
= bfd_h_get_32 (abfd
, buf
+ 52);
1618 seg
->extreloff
= bfd_h_get_32 (abfd
, buf
+ 56);
1619 seg
->nextrel
= bfd_h_get_32 (abfd
, buf
+ 60);
1620 seg
->locreloff
= bfd_h_get_32 (abfd
, buf
+ 64);
1621 seg
->nlocrel
= bfd_h_get_32 (abfd
, buf
+ 68);
1627 bfd_mach_o_scan_read_symtab (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1629 bfd_mach_o_symtab_command
*seg
= &command
->command
.symtab
;
1630 unsigned char buf
[16];
1633 const char *prefix
= "LC_SYMTAB.stabs";
1634 int nlist_size
= (bfd_mach_o_version (abfd
) > 1) ? 16 : 12;
1636 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SYMTAB
);
1638 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
1639 if (bfd_bread ((PTR
) buf
, 16, abfd
) != 16)
1642 seg
->symoff
= bfd_h_get_32 (abfd
, buf
);
1643 seg
->nsyms
= bfd_h_get_32 (abfd
, buf
+ 4);
1644 seg
->stroff
= bfd_h_get_32 (abfd
, buf
+ 8);
1645 seg
->strsize
= bfd_h_get_32 (abfd
, buf
+ 12);
1646 seg
->symbols
= NULL
;
1649 sname
= bfd_alloc (abfd
, strlen (prefix
) + 1);
1652 strcpy (sname
, prefix
);
1654 bfdsec
= bfd_make_section_anyway_with_flags (abfd
, sname
, SEC_HAS_CONTENTS
);
1660 bfdsec
->size
= seg
->nsyms
* nlist_size
;
1661 bfdsec
->filepos
= seg
->symoff
;
1662 bfdsec
->alignment_power
= 0;
1664 seg
->stabs_segment
= bfdsec
;
1666 if (seg
->nsyms
!= 0)
1667 abfd
->flags
|= HAS_SYMS
;
1669 prefix
= "LC_SYMTAB.stabstr";
1670 sname
= bfd_alloc (abfd
, strlen (prefix
) + 1);
1673 strcpy (sname
, prefix
);
1675 bfdsec
= bfd_make_section_anyway_with_flags (abfd
, sname
, SEC_HAS_CONTENTS
);
1681 bfdsec
->size
= seg
->strsize
;
1682 bfdsec
->filepos
= seg
->stroff
;
1683 bfdsec
->alignment_power
= 0;
1685 seg
->stabstr_segment
= bfdsec
;
1691 bfd_mach_o_scan_read_uuid (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1693 bfd_mach_o_uuid_command
*cmd
= &command
->command
.uuid
;
1696 static const char prefix
[] = "LC_UUID";
1698 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_UUID
);
1700 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
1701 if (bfd_bread ((PTR
) cmd
->uuid
, 16, abfd
) != 16)
1704 sname
= bfd_alloc (abfd
, strlen (prefix
) + 1);
1707 strcpy (sname
, prefix
);
1709 bfdsec
= bfd_make_section_anyway_with_flags (abfd
, sname
, SEC_HAS_CONTENTS
);
1715 bfdsec
->size
= command
->len
- 8;
1716 bfdsec
->filepos
= command
->offset
+ 8;
1717 bfdsec
->alignment_power
= 0;
1719 cmd
->section
= bfdsec
;
1725 bfd_mach_o_scan_read_segment (bfd
*abfd
,
1726 bfd_mach_o_load_command
*command
,
1729 unsigned char buf
[64];
1730 bfd_mach_o_segment_command
*seg
= &command
->command
.segment
;
1735 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SEGMENT_64
);
1737 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
1738 if (bfd_bread ((PTR
) buf
, 64, abfd
) != 64)
1741 memcpy (seg
->segname
, buf
, 16);
1742 seg
->segname
[16] = '\0';
1744 seg
->vmaddr
= bfd_h_get_64 (abfd
, buf
+ 16);
1745 seg
->vmsize
= bfd_h_get_64 (abfd
, buf
+ 24);
1746 seg
->fileoff
= bfd_h_get_64 (abfd
, buf
+ 32);
1747 seg
->filesize
= bfd_h_get_64 (abfd
, buf
+ 40);
1748 seg
->maxprot
= bfd_h_get_32 (abfd
, buf
+ 48);
1749 seg
->initprot
= bfd_h_get_32 (abfd
, buf
+ 52);
1750 seg
->nsects
= bfd_h_get_32 (abfd
, buf
+ 56);
1751 seg
->flags
= bfd_h_get_32 (abfd
, buf
+ 60);
1755 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SEGMENT
);
1757 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
1758 if (bfd_bread ((PTR
) buf
, 48, abfd
) != 48)
1761 memcpy (seg
->segname
, buf
, 16);
1762 seg
->segname
[16] = '\0';
1764 seg
->vmaddr
= bfd_h_get_32 (abfd
, buf
+ 16);
1765 seg
->vmsize
= bfd_h_get_32 (abfd
, buf
+ 20);
1766 seg
->fileoff
= bfd_h_get_32 (abfd
, buf
+ 24);
1767 seg
->filesize
= bfd_h_get_32 (abfd
, buf
+ 28);
1768 seg
->maxprot
= bfd_h_get_32 (abfd
, buf
+ 32);
1769 seg
->initprot
= bfd_h_get_32 (abfd
, buf
+ 36);
1770 seg
->nsects
= bfd_h_get_32 (abfd
, buf
+ 40);
1771 seg
->flags
= bfd_h_get_32 (abfd
, buf
+ 44);
1774 if (seg
->nsects
!= 0)
1776 seg
->sections
= bfd_alloc (abfd
, seg
->nsects
1777 * sizeof (bfd_mach_o_section
));
1778 if (seg
->sections
== NULL
)
1781 for (i
= 0; i
< seg
->nsects
; i
++)
1785 segoff
= command
->offset
+ BFD_MACH_O_LC_SEGMENT_64_SIZE
1786 + (i
* BFD_MACH_O_SECTION_64_SIZE
);
1788 segoff
= command
->offset
+ BFD_MACH_O_LC_SEGMENT_SIZE
1789 + (i
* BFD_MACH_O_SECTION_SIZE
);
1791 if (bfd_mach_o_scan_read_section
1792 (abfd
, &seg
->sections
[i
], segoff
, seg
->initprot
, wide
) != 0)
1801 bfd_mach_o_scan_read_segment_32 (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1803 return bfd_mach_o_scan_read_segment (abfd
, command
, 0);
1807 bfd_mach_o_scan_read_segment_64 (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1809 return bfd_mach_o_scan_read_segment (abfd
, command
, 1);
1813 bfd_mach_o_scan_read_command (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1815 unsigned char buf
[8];
1817 bfd_seek (abfd
, command
->offset
, SEEK_SET
);
1818 if (bfd_bread ((PTR
) buf
, 8, abfd
) != 8)
1821 command
->type
= bfd_h_get_32 (abfd
, buf
) & ~BFD_MACH_O_LC_REQ_DYLD
;
1822 command
->type_required
= (bfd_h_get_32 (abfd
, buf
) & BFD_MACH_O_LC_REQ_DYLD
1824 command
->len
= bfd_h_get_32 (abfd
, buf
+ 4);
1826 switch (command
->type
)
1828 case BFD_MACH_O_LC_SEGMENT
:
1829 if (bfd_mach_o_scan_read_segment_32 (abfd
, command
) != 0)
1832 case BFD_MACH_O_LC_SEGMENT_64
:
1833 if (bfd_mach_o_scan_read_segment_64 (abfd
, command
) != 0)
1836 case BFD_MACH_O_LC_SYMTAB
:
1837 if (bfd_mach_o_scan_read_symtab (abfd
, command
) != 0)
1840 case BFD_MACH_O_LC_SYMSEG
:
1842 case BFD_MACH_O_LC_THREAD
:
1843 case BFD_MACH_O_LC_UNIXTHREAD
:
1844 if (bfd_mach_o_scan_read_thread (abfd
, command
) != 0)
1847 case BFD_MACH_O_LC_LOAD_DYLINKER
:
1848 case BFD_MACH_O_LC_ID_DYLINKER
:
1849 if (bfd_mach_o_scan_read_dylinker (abfd
, command
) != 0)
1852 case BFD_MACH_O_LC_LOAD_DYLIB
:
1853 case BFD_MACH_O_LC_ID_DYLIB
:
1854 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB
:
1855 if (bfd_mach_o_scan_read_dylib (abfd
, command
) != 0)
1858 case BFD_MACH_O_LC_PREBOUND_DYLIB
:
1859 if (bfd_mach_o_scan_read_prebound_dylib (abfd
, command
) != 0)
1862 case BFD_MACH_O_LC_LOADFVMLIB
:
1863 case BFD_MACH_O_LC_IDFVMLIB
:
1864 case BFD_MACH_O_LC_IDENT
:
1865 case BFD_MACH_O_LC_FVMFILE
:
1866 case BFD_MACH_O_LC_PREPAGE
:
1867 case BFD_MACH_O_LC_ROUTINES
:
1868 case BFD_MACH_O_LC_SUB_FRAMEWORK
:
1870 case BFD_MACH_O_LC_DYSYMTAB
:
1871 if (bfd_mach_o_scan_read_dysymtab (abfd
, command
) != 0)
1874 case BFD_MACH_O_LC_SUB_UMBRELLA
:
1875 case BFD_MACH_O_LC_SUB_CLIENT
:
1876 case BFD_MACH_O_LC_SUB_LIBRARY
:
1877 case BFD_MACH_O_LC_TWOLEVEL_HINTS
:
1878 case BFD_MACH_O_LC_PREBIND_CKSUM
:
1880 case BFD_MACH_O_LC_UUID
:
1881 if (bfd_mach_o_scan_read_uuid (abfd
, command
) != 0)
1884 case BFD_MACH_O_LC_CODE_SIGNATURE
:
1885 case BFD_MACH_O_LC_SEGMENT_SPLIT_INFO
:
1886 case BFD_MACH_O_LC_REEXPORT_DYLIB
:
1889 fprintf (stderr
, "unable to read unknown load command 0x%lx\n",
1890 (unsigned long) command
->type
);
1898 bfd_mach_o_flatten_sections (bfd
*abfd
)
1900 bfd_mach_o_data_struct
*mdata
= abfd
->tdata
.mach_o_data
;
1904 /* Count total number of sections. */
1907 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
1909 if (mdata
->commands
[i
].type
== BFD_MACH_O_LC_SEGMENT
1910 || mdata
->commands
[i
].type
== BFD_MACH_O_LC_SEGMENT_64
)
1912 bfd_mach_o_segment_command
*seg
;
1914 seg
= &mdata
->commands
[i
].command
.segment
;
1915 mdata
->nsects
+= seg
->nsects
;
1919 /* Allocate sections array. */
1920 mdata
->sections
= bfd_alloc (abfd
,
1921 mdata
->nsects
* sizeof (bfd_mach_o_section
*));
1923 /* Fill the array. */
1926 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
1928 if (mdata
->commands
[i
].type
== BFD_MACH_O_LC_SEGMENT
1929 || mdata
->commands
[i
].type
== BFD_MACH_O_LC_SEGMENT_64
)
1931 bfd_mach_o_segment_command
*seg
;
1933 seg
= &mdata
->commands
[i
].command
.segment
;
1934 BFD_ASSERT (csect
+ seg
->nsects
<= mdata
->nsects
);
1936 for (j
= 0; j
< seg
->nsects
; j
++)
1937 mdata
->sections
[csect
++] = &seg
->sections
[j
];
1943 bfd_mach_o_scan_start_address (bfd
*abfd
)
1945 bfd_mach_o_data_struct
*mdata
= abfd
->tdata
.mach_o_data
;
1946 bfd_mach_o_thread_command
*cmd
= NULL
;
1949 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
1951 if ((mdata
->commands
[i
].type
== BFD_MACH_O_LC_THREAD
) ||
1952 (mdata
->commands
[i
].type
== BFD_MACH_O_LC_UNIXTHREAD
))
1955 cmd
= &mdata
->commands
[i
].command
.thread
;
1964 for (i
= 0; i
< cmd
->nflavours
; i
++)
1966 if ((mdata
->header
.cputype
== BFD_MACH_O_CPU_TYPE_I386
)
1967 && (cmd
->flavours
[i
].flavour
1968 == (unsigned long) BFD_MACH_O_x86_THREAD_STATE32
))
1970 unsigned char buf
[4];
1972 bfd_seek (abfd
, cmd
->flavours
[i
].offset
+ 40, SEEK_SET
);
1974 if (bfd_bread (buf
, 4, abfd
) != 4)
1977 abfd
->start_address
= bfd_h_get_32 (abfd
, buf
);
1979 else if ((mdata
->header
.cputype
== BFD_MACH_O_CPU_TYPE_POWERPC
)
1980 && (cmd
->flavours
[i
].flavour
== BFD_MACH_O_PPC_THREAD_STATE
))
1982 unsigned char buf
[4];
1984 bfd_seek (abfd
, cmd
->flavours
[i
].offset
+ 0, SEEK_SET
);
1986 if (bfd_bread (buf
, 4, abfd
) != 4)
1989 abfd
->start_address
= bfd_h_get_32 (abfd
, buf
);
1991 else if ((mdata
->header
.cputype
== BFD_MACH_O_CPU_TYPE_POWERPC_64
)
1992 && (cmd
->flavours
[i
].flavour
== BFD_MACH_O_PPC_THREAD_STATE_64
))
1994 unsigned char buf
[8];
1996 bfd_seek (abfd
, cmd
->flavours
[i
].offset
+ 0, SEEK_SET
);
1998 if (bfd_bread (buf
, 8, abfd
) != 8)
2001 abfd
->start_address
= bfd_h_get_64 (abfd
, buf
);
2003 else if ((mdata
->header
.cputype
== BFD_MACH_O_CPU_TYPE_X86_64
)
2004 && (cmd
->flavours
[i
].flavour
== BFD_MACH_O_x86_THREAD_STATE64
))
2006 unsigned char buf
[8];
2008 bfd_seek (abfd
, cmd
->flavours
[i
].offset
+ (16 * 8), SEEK_SET
);
2010 if (bfd_bread (buf
, 8, abfd
) != 8)
2013 abfd
->start_address
= bfd_h_get_64 (abfd
, buf
);
2021 bfd_mach_o_scan (bfd
*abfd
,
2022 bfd_mach_o_header
*header
,
2023 bfd_mach_o_data_struct
*mdata
)
2026 enum bfd_architecture cputype
;
2027 unsigned long cpusubtype
;
2028 unsigned int hdrsize
;
2030 hdrsize
= (header
->version
== 2) ?
2031 BFD_MACH_O_HEADER_64_SIZE
: BFD_MACH_O_HEADER_SIZE
;
2033 mdata
->header
= *header
;
2034 mdata
->symbols
= NULL
;
2036 abfd
->flags
= abfd
->flags
& BFD_IN_MEMORY
;
2037 switch (header
->filetype
)
2039 case BFD_MACH_O_MH_OBJECT
:
2040 abfd
->flags
|= HAS_RELOC
;
2042 case BFD_MACH_O_MH_EXECUTE
:
2043 abfd
->flags
|= EXEC_P
;
2045 case BFD_MACH_O_MH_DYLIB
:
2046 case BFD_MACH_O_MH_BUNDLE
:
2047 abfd
->flags
|= DYNAMIC
;
2051 abfd
->tdata
.mach_o_data
= mdata
;
2053 bfd_mach_o_convert_architecture (header
->cputype
, header
->cpusubtype
,
2054 &cputype
, &cpusubtype
);
2055 if (cputype
== bfd_arch_unknown
)
2057 fprintf (stderr
, "bfd_mach_o_scan: unknown architecture 0x%lx/0x%lx\n",
2058 header
->cputype
, header
->cpusubtype
);
2062 bfd_set_arch_mach (abfd
, cputype
, cpusubtype
);
2064 if (header
->ncmds
!= 0)
2066 mdata
->commands
= bfd_alloc (abfd
, header
->ncmds
* sizeof (bfd_mach_o_load_command
));
2067 if (mdata
->commands
== NULL
)
2070 for (i
= 0; i
< header
->ncmds
; i
++)
2072 bfd_mach_o_load_command
*cur
= &mdata
->commands
[i
];
2075 cur
->offset
= hdrsize
;
2078 bfd_mach_o_load_command
*prev
= &mdata
->commands
[i
- 1];
2079 cur
->offset
= prev
->offset
+ prev
->len
;
2082 if (bfd_mach_o_scan_read_command (abfd
, cur
) < 0)
2087 if (bfd_mach_o_scan_start_address (abfd
) < 0)
2090 bfd_mach_o_flatten_sections (abfd
);
2095 bfd_mach_o_mkobject_init (bfd
*abfd
)
2097 bfd_mach_o_data_struct
*mdata
= NULL
;
2099 mdata
= bfd_alloc (abfd
, sizeof (bfd_mach_o_data_struct
));
2102 abfd
->tdata
.mach_o_data
= mdata
;
2104 mdata
->header
.magic
= 0;
2105 mdata
->header
.cputype
= 0;
2106 mdata
->header
.cpusubtype
= 0;
2107 mdata
->header
.filetype
= 0;
2108 mdata
->header
.ncmds
= 0;
2109 mdata
->header
.sizeofcmds
= 0;
2110 mdata
->header
.flags
= 0;
2111 mdata
->header
.byteorder
= BFD_ENDIAN_UNKNOWN
;
2112 mdata
->commands
= NULL
;
2113 mdata
->nsymbols
= 0;
2114 mdata
->symbols
= NULL
;
2116 mdata
->sections
= NULL
;
2123 bfd_mach_o_header_p (bfd
*abfd
,
2124 bfd_mach_o_filetype filetype
,
2125 bfd_mach_o_cpu_type cputype
)
2127 struct bfd_preserve preserve
;
2128 bfd_mach_o_header header
;
2130 preserve
.marker
= NULL
;
2131 if (!bfd_mach_o_read_header (abfd
, &header
))
2134 if (! (header
.byteorder
== BFD_ENDIAN_BIG
2135 || header
.byteorder
== BFD_ENDIAN_LITTLE
))
2137 fprintf (stderr
, "unknown header byte-order value 0x%lx\n",
2138 (unsigned long) header
.byteorder
);
2142 if (! ((header
.byteorder
== BFD_ENDIAN_BIG
2143 && abfd
->xvec
->byteorder
== BFD_ENDIAN_BIG
2144 && abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
2145 || (header
.byteorder
== BFD_ENDIAN_LITTLE
2146 && abfd
->xvec
->byteorder
== BFD_ENDIAN_LITTLE
2147 && abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)))
2150 /* Check cputype and filetype.
2151 In case of wildcard, do not accept magics that are handled by existing
2155 if (header
.cputype
!= cputype
)
2160 switch (header
.cputype
)
2162 case BFD_MACH_O_CPU_TYPE_I386
:
2163 /* Handled by mach-o-i386 */
2171 if (header
.filetype
!= filetype
)
2176 switch (header
.filetype
)
2178 case BFD_MACH_O_MH_CORE
:
2179 /* Handled by core_p */
2186 preserve
.marker
= bfd_zalloc (abfd
, sizeof (bfd_mach_o_data_struct
));
2187 if (preserve
.marker
== NULL
2188 || !bfd_preserve_save (abfd
, &preserve
))
2191 if (bfd_mach_o_scan (abfd
, &header
,
2192 (bfd_mach_o_data_struct
*) preserve
.marker
) != 0)
2195 bfd_preserve_finish (abfd
, &preserve
);
2199 bfd_set_error (bfd_error_wrong_format
);
2202 if (preserve
.marker
!= NULL
)
2203 bfd_preserve_restore (abfd
, &preserve
);
2207 static const bfd_target
*
2208 bfd_mach_o_gen_object_p (bfd
*abfd
)
2210 return bfd_mach_o_header_p (abfd
, 0, 0);
2213 static const bfd_target
*
2214 bfd_mach_o_gen_core_p (bfd
*abfd
)
2216 return bfd_mach_o_header_p (abfd
, BFD_MACH_O_MH_CORE
, 0);
2219 typedef struct mach_o_fat_archentry
2221 unsigned long cputype
;
2222 unsigned long cpusubtype
;
2223 unsigned long offset
;
2225 unsigned long align
;
2226 } mach_o_fat_archentry
;
2228 typedef struct mach_o_fat_data_struct
2230 unsigned long magic
;
2231 unsigned long nfat_arch
;
2232 mach_o_fat_archentry
*archentries
;
2233 } mach_o_fat_data_struct
;
2236 bfd_mach_o_archive_p (bfd
*abfd
)
2238 mach_o_fat_data_struct
*adata
= NULL
;
2239 unsigned char buf
[20];
2242 bfd_seek (abfd
, 0, SEEK_SET
);
2243 if (bfd_bread ((PTR
) buf
, 8, abfd
) != 8)
2246 adata
= bfd_alloc (abfd
, sizeof (mach_o_fat_data_struct
));
2250 adata
->magic
= bfd_getb32 (buf
);
2251 adata
->nfat_arch
= bfd_getb32 (buf
+ 4);
2252 if (adata
->magic
!= 0xcafebabe)
2254 /* Avoid matching Java bytecode files, which have the same magic number.
2255 In the Java bytecode file format this field contains the JVM version,
2256 which starts at 43.0. */
2257 if (adata
->nfat_arch
> 30)
2260 adata
->archentries
=
2261 bfd_alloc (abfd
, adata
->nfat_arch
* sizeof (mach_o_fat_archentry
));
2262 if (adata
->archentries
== NULL
)
2265 for (i
= 0; i
< adata
->nfat_arch
; i
++)
2267 bfd_seek (abfd
, 8 + 20 * i
, SEEK_SET
);
2269 if (bfd_bread ((PTR
) buf
, 20, abfd
) != 20)
2271 adata
->archentries
[i
].cputype
= bfd_getb32 (buf
);
2272 adata
->archentries
[i
].cpusubtype
= bfd_getb32 (buf
+ 4);
2273 adata
->archentries
[i
].offset
= bfd_getb32 (buf
+ 8);
2274 adata
->archentries
[i
].size
= bfd_getb32 (buf
+ 12);
2275 adata
->archentries
[i
].align
= bfd_getb32 (buf
+ 16);
2278 abfd
->tdata
.mach_o_fat_data
= adata
;
2283 bfd_release (abfd
, adata
);
2284 bfd_set_error (bfd_error_wrong_format
);
2289 bfd_mach_o_openr_next_archived_file (bfd
*archive
, bfd
*prev
)
2291 mach_o_fat_data_struct
*adata
;
2292 mach_o_fat_archentry
*entry
= NULL
;
2295 enum bfd_architecture arch_type
;
2296 unsigned long arch_subtype
;
2298 adata
= (mach_o_fat_data_struct
*) archive
->tdata
.mach_o_fat_data
;
2299 BFD_ASSERT (adata
!= NULL
);
2301 /* Find index of previous entry. */
2303 i
= 0; /* Start at first one. */
2306 for (i
= 0; i
< adata
->nfat_arch
; i
++)
2308 if (adata
->archentries
[i
].offset
== prev
->origin
)
2312 if (i
== adata
->nfat_arch
)
2315 bfd_set_error (bfd_error_bad_value
);
2318 i
++; /* Get next entry. */
2321 if (i
>= adata
->nfat_arch
)
2323 bfd_set_error (bfd_error_no_more_archived_files
);
2327 entry
= &adata
->archentries
[i
];
2328 nbfd
= _bfd_new_bfd_contained_in (archive
);
2332 nbfd
->origin
= entry
->offset
;
2334 bfd_mach_o_convert_architecture (entry
->cputype
, entry
->cpusubtype
,
2335 &arch_type
, &arch_subtype
);
2336 /* Create the member filename.
2337 Use FILENAME:ARCH_NAME. */
2340 const char *arch_name
;
2341 size_t arch_file_len
= strlen (bfd_get_filename (archive
));
2343 arch_name
= bfd_printable_arch_mach (arch_type
, arch_subtype
);
2344 s
= bfd_malloc (arch_file_len
+ 1 + strlen (arch_name
) + 1);
2347 memcpy (s
, bfd_get_filename (archive
), arch_file_len
);
2348 s
[arch_file_len
] = ':';
2349 strcpy (s
+ arch_file_len
+ 1, arch_name
);
2352 nbfd
->iostream
= NULL
;
2353 bfd_set_arch_mach (nbfd
, arch_type
, arch_subtype
);
2358 /* If ABFD format is FORMAT and architecture is ARCH, return it.
2359 If ABFD is a fat image containing a member that corresponds to FORMAT
2360 and ARCH, returns it.
2361 In other case, returns NULL.
2362 This function allows transparent uses of fat images. */
2364 bfd_mach_o_fat_extract (bfd
*abfd
,
2366 const bfd_arch_info_type
*arch
)
2369 mach_o_fat_data_struct
*adata
;
2372 if (bfd_check_format (abfd
, format
))
2374 if (bfd_get_arch_info (abfd
) == arch
)
2378 if (!bfd_check_format (abfd
, bfd_archive
)
2379 || abfd
->xvec
!= &mach_o_fat_vec
)
2382 /* This is a Mach-O fat image. */
2383 adata
= (mach_o_fat_data_struct
*) abfd
->tdata
.mach_o_fat_data
;
2384 BFD_ASSERT (adata
!= NULL
);
2386 for (i
= 0; i
< adata
->nfat_arch
; i
++)
2388 struct mach_o_fat_archentry
*e
= &adata
->archentries
[i
];
2389 enum bfd_architecture cpu_type
;
2390 unsigned long cpu_subtype
;
2392 bfd_mach_o_convert_architecture (e
->cputype
, e
->cpusubtype
,
2393 &cpu_type
, &cpu_subtype
);
2394 if (cpu_type
!= arch
->arch
|| cpu_subtype
!= arch
->mach
)
2397 /* The architecture is found. */
2398 res
= _bfd_new_bfd_contained_in (abfd
);
2402 res
->origin
= e
->offset
;
2404 res
->filename
= strdup (abfd
->filename
);
2405 res
->iostream
= NULL
;
2407 if (bfd_check_format (res
, format
))
2409 BFD_ASSERT (bfd_get_arch_info (res
) == arch
);
2420 bfd_mach_o_lookup_section (bfd
*abfd
,
2422 bfd_mach_o_load_command
**mcommand
,
2423 bfd_mach_o_section
**msection
)
2425 struct mach_o_data_struct
*md
= abfd
->tdata
.mach_o_data
;
2426 unsigned int i
, j
, num
;
2428 bfd_mach_o_load_command
*ncmd
= NULL
;
2429 bfd_mach_o_section
*nsect
= NULL
;
2431 BFD_ASSERT (mcommand
!= NULL
);
2432 BFD_ASSERT (msection
!= NULL
);
2435 for (i
= 0; i
< md
->header
.ncmds
; i
++)
2437 struct bfd_mach_o_load_command
*cmd
= &md
->commands
[i
];
2438 struct bfd_mach_o_segment_command
*seg
= NULL
;
2440 if (cmd
->type
!= BFD_MACH_O_LC_SEGMENT
2441 || cmd
->type
!= BFD_MACH_O_LC_SEGMENT_64
)
2443 seg
= &cmd
->command
.segment
;
2445 if (seg
->segment
== section
)
2452 for (j
= 0; j
< seg
->nsects
; j
++)
2454 struct bfd_mach_o_section
*sect
= &seg
->sections
[j
];
2456 if (sect
->bfdsection
== section
)
2471 bfd_mach_o_lookup_command (bfd
*abfd
,
2472 bfd_mach_o_load_command_type type
,
2473 bfd_mach_o_load_command
**mcommand
)
2475 struct mach_o_data_struct
*md
= NULL
;
2476 bfd_mach_o_load_command
*ncmd
= NULL
;
2477 unsigned int i
, num
;
2479 md
= abfd
->tdata
.mach_o_data
;
2481 BFD_ASSERT (md
!= NULL
);
2482 BFD_ASSERT (mcommand
!= NULL
);
2485 for (i
= 0; i
< md
->header
.ncmds
; i
++)
2487 struct bfd_mach_o_load_command
*cmd
= &md
->commands
[i
];
2489 if (cmd
->type
!= type
)
2502 bfd_mach_o_stack_addr (enum bfd_mach_o_cpu_type type
)
2506 case BFD_MACH_O_CPU_TYPE_MC680x0
:
2508 case BFD_MACH_O_CPU_TYPE_MC88000
:
2510 case BFD_MACH_O_CPU_TYPE_POWERPC
:
2512 case BFD_MACH_O_CPU_TYPE_I386
:
2514 case BFD_MACH_O_CPU_TYPE_SPARC
:
2516 case BFD_MACH_O_CPU_TYPE_I860
:
2518 case BFD_MACH_O_CPU_TYPE_HPPA
:
2519 return 0xc0000000 - 0x04000000;
2526 bfd_mach_o_bfd_print_private_bfd_data (bfd
*abfd
, PTR ptr
)
2528 bfd_mach_o_data_struct
*mdata
= abfd
->tdata
.mach_o_data
;
2529 FILE *file
= (FILE *) ptr
;
2531 unsigned int sec_nbr
= 0;
2533 fprintf (file
, _("Segments and Sections:\n"));
2534 fprintf (file
, _(" #: Segment name Section name Address\n"));
2536 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
2538 bfd_mach_o_segment_command
*seg
;
2540 if (mdata
->commands
[i
].type
!= BFD_MACH_O_LC_SEGMENT
2541 && mdata
->commands
[i
].type
!= BFD_MACH_O_LC_SEGMENT_64
)
2544 seg
= &mdata
->commands
[i
].command
.segment
;
2546 fprintf (file
, "[Segment %-16s ", seg
->segname
);
2547 fprintf_vma (file
, seg
->vmaddr
);
2548 fprintf (file
, "-");
2549 fprintf_vma (file
, seg
->vmaddr
+ seg
->vmsize
- 1);
2551 fputc (seg
->initprot
& BFD_MACH_O_PROT_READ
? 'r' : '-', file
);
2552 fputc (seg
->initprot
& BFD_MACH_O_PROT_WRITE
? 'w' : '-', file
);
2553 fputc (seg
->initprot
& BFD_MACH_O_PROT_EXECUTE
? 'x' : '-', file
);
2554 fprintf (file
, "]\n");
2555 for (j
= 0; j
< seg
->nsects
; j
++)
2557 bfd_mach_o_section
*sec
= &seg
->sections
[j
];
2558 fprintf (file
, "%02u: %-16s %-16s ", ++sec_nbr
,
2559 sec
->segname
, sec
->sectname
);
2560 fprintf_vma (file
, sec
->addr
);
2561 fprintf (file
, " ");
2562 fprintf_vma (file
, sec
->size
);
2563 fprintf (file
, " %08lx\n", sec
->flags
);
2567 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
2569 bfd_mach_o_load_command
*cmd
= &mdata
->commands
[i
];
2573 case BFD_MACH_O_LC_SEGMENT
:
2574 case BFD_MACH_O_LC_SEGMENT_64
:
2576 case BFD_MACH_O_LC_UUID
:
2578 bfd_mach_o_uuid_command
*uuid
= &cmd
->command
.uuid
;
2583 for (i
= 0; i
< sizeof (uuid
->uuid
); i
++)
2584 fprintf (file
, " %02x", uuid
->uuid
[i
]);
2588 case BFD_MACH_O_LC_LOAD_DYLIB
:
2590 bfd_mach_o_dylib_command
*dylib
= &cmd
->command
.dylib
;
2591 bfd_byte
*data
= NULL
;
2593 if (! bfd_malloc_and_get_section (abfd
, dylib
->section
, &data
))
2601 data
+ dylib
->name_offset
- cmd
->offset
- 8);
2602 fprintf (file
, " time stamp: 0x%08lx\n",
2604 fprintf (file
, " current version: 0x%08lx\n",
2605 dylib
->current_version
);
2606 fprintf (file
, " comptibility version: 0x%08lx\n",
2607 dylib
->compatibility_version
);
2611 case BFD_MACH_O_LC_LOAD_DYLINKER
:
2613 bfd_mach_o_dylinker_command
*linker
= &cmd
->command
.dylinker
;
2614 bfd_byte
*data
= NULL
;
2616 if (! bfd_malloc_and_get_section (abfd
, linker
->section
, &data
))
2623 "LOAD_DYLINKER: %s\n",
2624 data
+ linker
->name_offset
- cmd
->offset
- 8);
2628 case BFD_MACH_O_LC_SYMTAB
:
2630 bfd_mach_o_symtab_command
*symtab
= &cmd
->command
.symtab
;
2632 "LC_SYMTAB: nsyms: %lu, strsize: %lu\n",
2633 symtab
->nsyms
, symtab
->strsize
);
2636 case BFD_MACH_O_LC_DYSYMTAB
:
2638 bfd_mach_o_dysymtab_command
*dysymtab
= &cmd
->command
.dysymtab
;
2641 " local symbols: index: %lu number: %lu\n",
2642 dysymtab
->ilocalsym
, dysymtab
->nlocalsym
);
2644 " external symbols: index: %lu number: %lu\n",
2645 dysymtab
->iextdefsym
, dysymtab
->nextdefsym
);
2647 " undefined symbols: index: %lu number: %lu\n",
2648 dysymtab
->iundefsym
, dysymtab
->nundefsym
);
2650 " ntoc: offset: %lu number: %lu\n",
2651 dysymtab
->tocoff
, dysymtab
->ntoc
);
2653 " module table: offset: %lu number: %lu\n",
2654 dysymtab
->modtaboff
, dysymtab
->nmodtab
);
2658 fprintf (file
, "LC_%d\n", cmd
->type
);
2667 bfd_mach_o_core_fetch_environment (bfd
*abfd
,
2668 unsigned char **rbuf
,
2671 bfd_mach_o_data_struct
*mdata
= abfd
->tdata
.mach_o_data
;
2672 unsigned long stackaddr
= bfd_mach_o_stack_addr (mdata
->header
.cputype
);
2675 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
2677 bfd_mach_o_load_command
*cur
= &mdata
->commands
[i
];
2678 bfd_mach_o_segment_command
*seg
= NULL
;
2680 if (cur
->type
!= BFD_MACH_O_LC_SEGMENT
)
2683 seg
= &cur
->command
.segment
;
2685 if ((seg
->vmaddr
+ seg
->vmsize
) == stackaddr
)
2687 unsigned long start
= seg
->fileoff
;
2688 unsigned long end
= seg
->fileoff
+ seg
->filesize
;
2689 unsigned char *buf
= bfd_malloc (1024);
2690 unsigned long size
= 1024;
2694 bfd_size_type nread
= 0;
2695 unsigned long offset
;
2696 int found_nonnull
= 0;
2698 if (size
> (end
- start
))
2699 size
= (end
- start
);
2701 buf
= bfd_realloc_or_free (buf
, size
);
2705 bfd_seek (abfd
, end
- size
, SEEK_SET
);
2706 nread
= bfd_bread (buf
, size
, abfd
);
2714 for (offset
= 4; offset
<= size
; offset
+= 4)
2718 val
= *((unsigned long *) (buf
+ size
- offset
));
2719 if (! found_nonnull
)
2724 else if (val
== 0x0)
2726 unsigned long bottom
;
2729 bottom
= seg
->fileoff
+ seg
->filesize
- offset
;
2730 top
= seg
->fileoff
+ seg
->filesize
- 4;
2731 *rbuf
= bfd_malloc (top
- bottom
);
2732 *rlen
= top
- bottom
;
2734 memcpy (*rbuf
, buf
+ size
- *rlen
, *rlen
);
2740 if (size
== (end
- start
))
2754 bfd_mach_o_core_file_failing_command (bfd
*abfd
)
2756 unsigned char *buf
= NULL
;
2757 unsigned int len
= 0;
2760 ret
= bfd_mach_o_core_fetch_environment (abfd
, &buf
, &len
);
2764 return (char *) buf
;
2768 bfd_mach_o_core_file_failing_signal (bfd
*abfd ATTRIBUTE_UNUSED
)
2773 #define TARGET_NAME mach_o_be_vec
2774 #define TARGET_STRING "mach-o-be"
2775 #define TARGET_BIG_ENDIAN 1
2776 #define TARGET_ARCHIVE 0
2778 #include "mach-o-target.c"
2781 #undef TARGET_STRING
2782 #undef TARGET_BIG_ENDIAN
2783 #undef TARGET_ARCHIVE
2785 #define TARGET_NAME mach_o_le_vec
2786 #define TARGET_STRING "mach-o-le"
2787 #define TARGET_BIG_ENDIAN 0
2788 #define TARGET_ARCHIVE 0
2790 #include "mach-o-target.c"
2793 #undef TARGET_STRING
2794 #undef TARGET_BIG_ENDIAN
2795 #undef TARGET_ARCHIVE
2797 #define TARGET_NAME mach_o_fat_vec
2798 #define TARGET_STRING "mach-o-fat"
2799 #define TARGET_BIG_ENDIAN 1
2800 #define TARGET_ARCHIVE 1
2802 #include "mach-o-target.c"
2805 #undef TARGET_STRING
2806 #undef TARGET_BIG_ENDIAN
2807 #undef TARGET_ARCHIVE