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
34 #define FILE_ALIGN(off, algn) \
35 (((off) + ((file_ptr) 1 << (algn)) - 1) & ((file_ptr) -1 << (algn)))
37 static int bfd_mach_o_scan_read_symtab_symbols (bfd
*);
40 bfd_mach_o_version (bfd
*abfd
)
42 bfd_mach_o_data_struct
*mdata
= NULL
;
44 BFD_ASSERT (bfd_mach_o_valid (abfd
));
45 mdata
= bfd_mach_o_get_data (abfd
);
47 return mdata
->header
.version
;
51 bfd_mach_o_valid (bfd
*abfd
)
53 if (abfd
== NULL
|| abfd
->xvec
== NULL
)
56 if (abfd
->xvec
->flavour
!= bfd_target_mach_o_flavour
)
59 if (bfd_mach_o_get_data (abfd
) == NULL
)
64 static INLINE bfd_boolean
65 mach_o_wide_p (bfd_mach_o_header
*header
)
67 switch (header
->version
)
79 static INLINE bfd_boolean
80 bfd_mach_o_wide_p (bfd
*abfd
)
82 return mach_o_wide_p (&bfd_mach_o_get_data (abfd
)->header
);
85 /* Tables to translate well known Mach-O segment/section names to bfd
86 names. Use of canonical names (such as .text or .debug_frame) is required
89 struct mach_o_section_name_xlat
92 const char *mach_o_name
;
95 static const struct mach_o_section_name_xlat dwarf_section_names_xlat
[] =
97 { ".debug_frame", "__debug_frame" },
98 { ".debug_info", "__debug_info" },
99 { ".debug_abbrev", "__debug_abbrev" },
100 { ".debug_aranges", "__debug_aranges" },
101 { ".debug_macinfo", "__debug_macinfo" },
102 { ".debug_line", "__debug_line" },
103 { ".debug_loc", "__debug_loc" },
104 { ".debug_pubnames", "__debug_pubnames" },
105 { ".debug_pubtypes", "__debug_pubtypes" },
106 { ".debug_str", "__debug_str" },
107 { ".debug_ranges", "__debug_ranges" },
111 static const struct mach_o_section_name_xlat text_section_names_xlat
[] =
113 { ".text", "__text" },
114 { ".const", "__const" },
115 { ".cstring", "__cstring" },
116 { ".eh_frame", "__eh_frame" },
120 static const struct mach_o_section_name_xlat data_section_names_xlat
[] =
122 { ".data", "__data" },
127 struct mach_o_segment_name_xlat
130 const struct mach_o_section_name_xlat
*sections
;
133 static const struct mach_o_segment_name_xlat segsec_names_xlat
[] =
135 { "__DWARF", dwarf_section_names_xlat
},
136 { "__TEXT", text_section_names_xlat
},
137 { "__DATA", data_section_names_xlat
},
142 /* Mach-O to bfd names. */
145 bfd_mach_o_convert_section_name_to_bfd (bfd
*abfd
, bfd_mach_o_section
*section
)
147 const struct mach_o_segment_name_xlat
*seg
;
150 const char *pfx
= "";
152 for (seg
= segsec_names_xlat
; seg
->segname
; seg
++)
154 if (strcmp (seg
->segname
, section
->segname
) == 0)
156 const struct mach_o_section_name_xlat
*sec
;
158 for (sec
= seg
->sections
; sec
->mach_o_name
; sec
++)
160 if (strcmp (sec
->mach_o_name
, section
->sectname
) == 0)
162 len
= strlen (sec
->bfd_name
);
163 res
= bfd_alloc (abfd
, len
+ 1);
167 strcpy (res
, sec
->bfd_name
);
174 len
= strlen (section
->segname
) + 1
175 + strlen (section
->sectname
) + 1;
177 /* Put "LC_SEGMENT." prefix if the segment name is weird (ie doesn't start
178 with an underscore. */
179 if (section
->segname
[0] != '_')
181 static const char seg_pfx
[] = "LC_SEGMENT.";
184 len
+= sizeof (seg_pfx
) - 1;
187 res
= bfd_alloc (abfd
, len
);
190 snprintf (res
, len
, "%s%s.%s", pfx
, section
->segname
, section
->sectname
);
194 /* Convert a bfd section name to a Mach-O segment + section name. */
197 bfd_mach_o_convert_section_name_to_mach_o (bfd
*abfd ATTRIBUTE_UNUSED
,
199 bfd_mach_o_section
*section
)
201 const struct mach_o_segment_name_xlat
*seg
;
202 const char *name
= bfd_get_section_name (abfd
, sect
);
208 /* List of well known names. They all start with a dot. */
210 for (seg
= segsec_names_xlat
; seg
->segname
; seg
++)
212 const struct mach_o_section_name_xlat
*sec
;
214 for (sec
= seg
->sections
; sec
->mach_o_name
; sec
++)
216 if (strcmp (sec
->bfd_name
, name
) == 0)
218 strcpy (section
->segname
, seg
->segname
);
219 strcpy (section
->sectname
, sec
->mach_o_name
);
225 /* Strip LC_SEGMENT. prefix. */
226 if (strncmp (name
, "LC_SEGMENT.", 11) == 0)
230 dot
= strchr (name
, '.');
233 /* Try to split name into segment and section names. */
234 if (dot
&& dot
!= name
)
237 seclen
= len
- (dot
+ 1 - name
);
239 if (seglen
< 16 && seclen
< 16)
241 memcpy (section
->segname
, name
, seglen
);
242 section
->segname
[seglen
] = 0;
243 memcpy (section
->sectname
, dot
+ 1, seclen
);
244 section
->sectname
[seclen
] = 0;
251 memcpy (section
->segname
, name
, len
);
252 section
->segname
[len
] = 0;
253 memcpy (section
->sectname
, name
, len
);
254 section
->sectname
[len
] = 0;
257 /* Copy any private info we understand from the input symbol
258 to the output symbol. */
261 bfd_mach_o_bfd_copy_private_symbol_data (bfd
*ibfd ATTRIBUTE_UNUSED
,
262 asymbol
*isymbol ATTRIBUTE_UNUSED
,
263 bfd
*obfd ATTRIBUTE_UNUSED
,
264 asymbol
*osymbol ATTRIBUTE_UNUSED
)
269 /* Copy any private info we understand from the input section
270 to the output section. */
273 bfd_mach_o_bfd_copy_private_section_data (bfd
*ibfd ATTRIBUTE_UNUSED
,
274 asection
*isection ATTRIBUTE_UNUSED
,
275 bfd
*obfd ATTRIBUTE_UNUSED
,
276 asection
*osection ATTRIBUTE_UNUSED
)
281 /* Copy any private info we understand from the input bfd
282 to the output bfd. */
285 bfd_mach_o_bfd_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
287 if (bfd_get_flavour (ibfd
) != bfd_target_mach_o_flavour
288 || bfd_get_flavour (obfd
) != bfd_target_mach_o_flavour
)
291 BFD_ASSERT (bfd_mach_o_valid (ibfd
));
292 BFD_ASSERT (bfd_mach_o_valid (obfd
));
294 /* FIXME: copy commands. */
299 /* Count the total number of symbols. */
302 bfd_mach_o_count_symbols (bfd
*abfd
)
304 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
306 if (mdata
->symtab
== NULL
)
308 return mdata
->symtab
->nsyms
;
312 bfd_mach_o_get_symtab_upper_bound (bfd
*abfd
)
314 long nsyms
= bfd_mach_o_count_symbols (abfd
);
316 return ((nsyms
+ 1) * sizeof (asymbol
*));
320 bfd_mach_o_canonicalize_symtab (bfd
*abfd
, asymbol
**alocation
)
322 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
323 long nsyms
= bfd_mach_o_count_symbols (abfd
);
324 bfd_mach_o_symtab_command
*sym
= mdata
->symtab
;
330 if (bfd_mach_o_scan_read_symtab_symbols (abfd
) != 0)
333 "bfd_mach_o_canonicalize_symtab: unable to load symbols\n");
337 BFD_ASSERT (sym
->symbols
!= NULL
);
339 for (j
= 0; j
< sym
->nsyms
; j
++)
340 alocation
[j
] = &sym
->symbols
[j
].symbol
;
348 bfd_mach_o_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
352 bfd_symbol_info (symbol
, ret
);
356 bfd_mach_o_print_symbol (bfd
*abfd
,
359 bfd_print_symbol_type how
)
361 FILE *file
= (FILE *) afile
;
363 bfd_mach_o_asymbol
*asym
= (bfd_mach_o_asymbol
*)symbol
;
367 case bfd_print_symbol_name
:
368 fprintf (file
, "%s", symbol
->name
);
371 bfd_print_symbol_vandf (abfd
, (PTR
) file
, symbol
);
372 if (asym
->n_type
& BFD_MACH_O_N_STAB
)
373 name
= bfd_get_stab_name (asym
->n_type
);
375 switch (asym
->n_type
& BFD_MACH_O_N_TYPE
)
377 case BFD_MACH_O_N_UNDF
:
380 case BFD_MACH_O_N_ABS
:
383 case BFD_MACH_O_N_INDR
:
386 case BFD_MACH_O_N_PBUD
:
389 case BFD_MACH_O_N_SECT
:
398 fprintf (file
, " %02x %-6s %02x %04x",
399 asym
->n_type
, name
, asym
->n_sect
, asym
->n_desc
);
400 if ((asym
->n_type
& BFD_MACH_O_N_STAB
) == 0
401 && (asym
->n_type
& BFD_MACH_O_N_TYPE
) == BFD_MACH_O_N_SECT
)
402 fprintf (file
, " %-5s", symbol
->section
->name
);
403 fprintf (file
, " %s", symbol
->name
);
408 bfd_mach_o_convert_architecture (bfd_mach_o_cpu_type mtype
,
409 bfd_mach_o_cpu_subtype msubtype ATTRIBUTE_UNUSED
,
410 enum bfd_architecture
*type
,
411 unsigned long *subtype
)
413 *subtype
= bfd_arch_unknown
;
417 case BFD_MACH_O_CPU_TYPE_VAX
: *type
= bfd_arch_vax
; break;
418 case BFD_MACH_O_CPU_TYPE_MC680x0
: *type
= bfd_arch_m68k
; break;
419 case BFD_MACH_O_CPU_TYPE_I386
:
420 *type
= bfd_arch_i386
;
421 *subtype
= bfd_mach_i386_i386
;
423 case BFD_MACH_O_CPU_TYPE_X86_64
:
424 *type
= bfd_arch_i386
;
425 *subtype
= bfd_mach_x86_64
;
427 case BFD_MACH_O_CPU_TYPE_MIPS
: *type
= bfd_arch_mips
; break;
428 case BFD_MACH_O_CPU_TYPE_MC98000
: *type
= bfd_arch_m98k
; break;
429 case BFD_MACH_O_CPU_TYPE_HPPA
: *type
= bfd_arch_hppa
; break;
430 case BFD_MACH_O_CPU_TYPE_ARM
: *type
= bfd_arch_arm
; break;
431 case BFD_MACH_O_CPU_TYPE_MC88000
: *type
= bfd_arch_m88k
; break;
432 case BFD_MACH_O_CPU_TYPE_SPARC
:
433 *type
= bfd_arch_sparc
;
434 *subtype
= bfd_mach_sparc
;
436 case BFD_MACH_O_CPU_TYPE_I860
: *type
= bfd_arch_i860
; break;
437 case BFD_MACH_O_CPU_TYPE_ALPHA
: *type
= bfd_arch_alpha
; break;
438 case BFD_MACH_O_CPU_TYPE_POWERPC
:
439 *type
= bfd_arch_powerpc
;
440 *subtype
= bfd_mach_ppc
;
442 case BFD_MACH_O_CPU_TYPE_POWERPC_64
:
443 *type
= bfd_arch_powerpc
;
444 *subtype
= bfd_mach_ppc64
;
447 *type
= bfd_arch_unknown
;
453 bfd_mach_o_write_header (bfd
*abfd
, bfd_mach_o_header
*header
)
455 unsigned char buf
[32];
458 size
= mach_o_wide_p (header
) ?
459 BFD_MACH_O_HEADER_64_SIZE
: BFD_MACH_O_HEADER_SIZE
;
461 bfd_h_put_32 (abfd
, header
->magic
, buf
+ 0);
462 bfd_h_put_32 (abfd
, header
->cputype
, buf
+ 4);
463 bfd_h_put_32 (abfd
, header
->cpusubtype
, buf
+ 8);
464 bfd_h_put_32 (abfd
, header
->filetype
, buf
+ 12);
465 bfd_h_put_32 (abfd
, header
->ncmds
, buf
+ 16);
466 bfd_h_put_32 (abfd
, header
->sizeofcmds
, buf
+ 20);
467 bfd_h_put_32 (abfd
, header
->flags
, buf
+ 24);
469 if (mach_o_wide_p (header
))
470 bfd_h_put_32 (abfd
, header
->reserved
, buf
+ 28);
472 if (bfd_seek (abfd
, 0, SEEK_SET
) != 0
473 || bfd_bwrite ((PTR
) buf
, size
, abfd
) != size
)
480 bfd_mach_o_scan_write_thread (bfd
*abfd
, bfd_mach_o_load_command
*command
)
482 bfd_mach_o_thread_command
*cmd
= &command
->command
.thread
;
484 unsigned char buf
[8];
486 unsigned int nflavours
;
488 BFD_ASSERT ((command
->type
== BFD_MACH_O_LC_THREAD
)
489 || (command
->type
== BFD_MACH_O_LC_UNIXTHREAD
));
493 for (i
= 0; i
< cmd
->nflavours
; i
++)
495 BFD_ASSERT ((cmd
->flavours
[i
].size
% 4) == 0);
496 BFD_ASSERT (cmd
->flavours
[i
].offset
== (command
->offset
+ offset
+ 8));
498 bfd_h_put_32 (abfd
, cmd
->flavours
[i
].flavour
, buf
);
499 bfd_h_put_32 (abfd
, (cmd
->flavours
[i
].size
/ 4), buf
+ 4);
501 if (bfd_seek (abfd
, command
->offset
+ offset
, SEEK_SET
) != 0
502 || bfd_bwrite ((PTR
) buf
, 8, abfd
) != 8)
505 offset
+= cmd
->flavours
[i
].size
+ 8;
512 bfd_mach_o_get_reloc_upper_bound (bfd
*abfd ATTRIBUTE_UNUSED
,
515 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
519 bfd_mach_o_canonicalize_one_reloc (bfd
*abfd
, char *buf
,
520 arelent
*res
, asymbol
**syms
)
522 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
523 bfd_mach_o_backend_data
*bed
= bfd_mach_o_get_backend_data (abfd
);
524 bfd_mach_o_reloc_info reloc
;
529 addr
= bfd_get_32 (abfd
, buf
+ 0);
530 symnum
= bfd_get_32 (abfd
, buf
+ 4);
532 if (addr
& BFD_MACH_O_SR_SCATTERED
)
536 /* Scattered relocation.
537 Extract section and offset from r_value. */
538 res
->sym_ptr_ptr
= NULL
;
540 for (j
= 0; j
< mdata
->nsects
; j
++)
542 bfd_mach_o_section
*sect
= mdata
->sections
[j
];
543 if (symnum
>= sect
->addr
&& symnum
< sect
->addr
+ sect
->size
)
545 res
->sym_ptr_ptr
= sect
->bfdsection
->symbol_ptr_ptr
;
546 res
->addend
= symnum
- sect
->addr
;
550 res
->address
= BFD_MACH_O_GET_SR_ADDRESS (addr
);
551 reloc
.r_type
= BFD_MACH_O_GET_SR_TYPE (addr
);
552 reloc
.r_length
= BFD_MACH_O_GET_SR_LENGTH (addr
);
553 reloc
.r_pcrel
= addr
& BFD_MACH_O_SR_PCREL
;
554 reloc
.r_scattered
= 1;
558 unsigned int num
= BFD_MACH_O_GET_R_SYMBOLNUM (symnum
);
561 if (symnum
& BFD_MACH_O_R_EXTERN
)
565 BFD_ASSERT (num
!= 0);
566 BFD_ASSERT (num
<= mdata
->nsects
);
567 sym
= mdata
->sections
[num
- 1]->bfdsection
->symbol_ptr_ptr
;
569 res
->sym_ptr_ptr
= sym
;
570 reloc
.r_type
= BFD_MACH_O_GET_R_TYPE (symnum
);
571 reloc
.r_length
= BFD_MACH_O_GET_R_LENGTH (symnum
);
572 reloc
.r_pcrel
= (symnum
& BFD_MACH_O_R_PCREL
) ? 1 : 0;
573 reloc
.r_scattered
= 0;
576 if (!(*bed
->_bfd_mach_o_swap_reloc_in
)(res
, &reloc
))
582 bfd_mach_o_canonicalize_relocs (bfd
*abfd
, unsigned long filepos
,
584 arelent
*res
, asymbol
**syms
)
588 bfd_size_type native_size
;
590 /* Allocate and read relocs. */
591 native_size
= count
* BFD_MACH_O_RELENT_SIZE
;
592 native_relocs
= bfd_malloc (native_size
);
593 if (native_relocs
== NULL
)
596 if (bfd_seek (abfd
, filepos
, SEEK_SET
) != 0
597 || bfd_bread (native_relocs
, native_size
, abfd
) != native_size
)
600 for (i
= 0; i
< count
; i
++)
602 char *buf
= native_relocs
+ BFD_MACH_O_RELENT_SIZE
* i
;
604 if (bfd_mach_o_canonicalize_one_reloc (abfd
, buf
, &res
[i
], syms
) < 0)
607 free (native_relocs
);
610 free (native_relocs
);
615 bfd_mach_o_canonicalize_reloc (bfd
*abfd
, asection
*asect
,
616 arelent
**rels
, asymbol
**syms
)
618 bfd_mach_o_backend_data
*bed
= bfd_mach_o_get_backend_data (abfd
);
622 if (asect
->reloc_count
== 0)
625 /* No need to go further if we don't know how to read relocs. */
626 if (bed
->_bfd_mach_o_swap_reloc_in
== NULL
)
629 res
= bfd_malloc (asect
->reloc_count
* sizeof (arelent
));
633 if (bfd_mach_o_canonicalize_relocs (abfd
, asect
->rel_filepos
,
634 asect
->reloc_count
, res
, syms
) < 0)
640 for (i
= 0; i
< asect
->reloc_count
; i
++)
643 asect
->relocation
= res
;
649 bfd_mach_o_get_dynamic_reloc_upper_bound (bfd
*abfd
)
651 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
653 if (mdata
->dysymtab
== NULL
)
655 return (mdata
->dysymtab
->nextrel
+ mdata
->dysymtab
->nlocrel
)
656 * sizeof (arelent
*);
660 bfd_mach_o_canonicalize_dynamic_reloc (bfd
*abfd
, arelent
**rels
,
661 struct bfd_symbol
**syms
)
663 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
664 bfd_mach_o_dysymtab_command
*dysymtab
= mdata
->dysymtab
;
665 bfd_mach_o_backend_data
*bed
= bfd_mach_o_get_backend_data (abfd
);
669 if (dysymtab
== NULL
)
671 if (dysymtab
->nextrel
== 0 && dysymtab
->nlocrel
== 0)
674 /* No need to go further if we don't know how to read relocs. */
675 if (bed
->_bfd_mach_o_swap_reloc_in
== NULL
)
678 res
= bfd_malloc ((dysymtab
->nextrel
+ dysymtab
->nlocrel
) * sizeof (arelent
));
682 if (bfd_mach_o_canonicalize_relocs (abfd
, dysymtab
->extreloff
,
683 dysymtab
->nextrel
, res
, syms
) < 0)
689 if (bfd_mach_o_canonicalize_relocs (abfd
, dysymtab
->locreloff
,
691 res
+ dysymtab
->nextrel
, syms
) < 0)
697 for (i
= 0; i
< dysymtab
->nextrel
+ dysymtab
->nlocrel
; i
++)
704 bfd_mach_o_scan_write_relocs (bfd
*abfd
, bfd_mach_o_section
*section
)
706 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
710 bfd_mach_o_backend_data
*bed
= bfd_mach_o_get_backend_data (abfd
);
712 sec
= section
->bfdsection
;
713 if (sec
->reloc_count
== 0)
716 if (bed
->_bfd_mach_o_swap_reloc_out
== NULL
)
719 /* Allocate relocation room. */
720 mdata
->filelen
= FILE_ALIGN(mdata
->filelen
, 2);
721 section
->nreloc
= sec
->reloc_count
;
722 sec
->rel_filepos
= mdata
->filelen
;
723 section
->reloff
= sec
->rel_filepos
;
724 mdata
->filelen
+= sec
->reloc_count
* BFD_MACH_O_RELENT_SIZE
;
726 if (bfd_seek (abfd
, section
->reloff
, SEEK_SET
) != 0)
729 /* Convert and write. */
730 entries
= section
->bfdsection
->orelocation
;
731 for (i
= 0; i
< section
->nreloc
; i
++)
733 arelent
*rel
= entries
[i
];
735 bfd_mach_o_reloc_info info
, *pinfo
= &info
;
737 /* Convert relocation to an intermediate representation. */
738 if (!(*bed
->_bfd_mach_o_swap_reloc_out
) (rel
, pinfo
))
741 /* Lower the relocation info. */
742 if (pinfo
->r_scattered
)
746 v
= BFD_MACH_O_SR_SCATTERED
747 | (pinfo
->r_pcrel
? BFD_MACH_O_SR_PCREL
: 0)
748 | BFD_MACH_O_SET_SR_LENGTH(pinfo
->r_length
)
749 | BFD_MACH_O_SET_SR_TYPE(pinfo
->r_type
)
750 | BFD_MACH_O_SET_SR_ADDRESS(pinfo
->r_address
);
751 bfd_put_32 (abfd
, v
, buf
);
752 bfd_put_32 (abfd
, pinfo
->r_value
, buf
+ 4);
758 bfd_put_32 (abfd
, pinfo
->r_address
, buf
);
759 v
= BFD_MACH_O_SET_R_SYMBOLNUM (pinfo
->r_value
)
760 | (pinfo
->r_pcrel
? BFD_MACH_O_R_PCREL
: 0)
761 | BFD_MACH_O_SET_R_LENGTH (pinfo
->r_length
)
762 | (pinfo
->r_extern
? BFD_MACH_O_R_EXTERN
: 0)
763 | BFD_MACH_O_SET_R_TYPE (pinfo
->r_type
);
764 bfd_put_32 (abfd
, v
, buf
+ 4);
767 if (bfd_bwrite ((PTR
) buf
, BFD_MACH_O_RELENT_SIZE
, abfd
)
768 != BFD_MACH_O_RELENT_SIZE
)
775 bfd_mach_o_scan_write_section_32 (bfd
*abfd
, bfd_mach_o_section
*section
)
777 unsigned char buf
[BFD_MACH_O_SECTION_SIZE
];
779 memcpy (buf
, section
->sectname
, 16);
780 memcpy (buf
+ 16, section
->segname
, 16);
781 bfd_h_put_32 (abfd
, section
->addr
, buf
+ 32);
782 bfd_h_put_32 (abfd
, section
->size
, buf
+ 36);
783 bfd_h_put_32 (abfd
, section
->offset
, buf
+ 40);
784 bfd_h_put_32 (abfd
, section
->align
, buf
+ 44);
785 bfd_h_put_32 (abfd
, section
->reloff
, buf
+ 48);
786 bfd_h_put_32 (abfd
, section
->nreloc
, buf
+ 52);
787 bfd_h_put_32 (abfd
, section
->flags
, buf
+ 56);
788 bfd_h_put_32 (abfd
, section
->reserved1
, buf
+ 60);
789 bfd_h_put_32 (abfd
, section
->reserved2
, buf
+ 64);
791 if (bfd_bwrite ((PTR
) buf
, BFD_MACH_O_SECTION_SIZE
, abfd
)
792 != BFD_MACH_O_SECTION_SIZE
)
799 bfd_mach_o_scan_write_section_64 (bfd
*abfd
, bfd_mach_o_section
*section
)
801 unsigned char buf
[BFD_MACH_O_SECTION_64_SIZE
];
803 memcpy (buf
, section
->sectname
, 16);
804 memcpy (buf
+ 16, section
->segname
, 16);
805 bfd_h_put_64 (abfd
, section
->addr
, buf
+ 32);
806 bfd_h_put_64 (abfd
, section
->size
, buf
+ 40);
807 bfd_h_put_32 (abfd
, section
->offset
, buf
+ 48);
808 bfd_h_put_32 (abfd
, section
->align
, buf
+ 52);
809 bfd_h_put_32 (abfd
, section
->reloff
, buf
+ 56);
810 bfd_h_put_32 (abfd
, section
->nreloc
, buf
+ 60);
811 bfd_h_put_32 (abfd
, section
->flags
, buf
+ 64);
812 bfd_h_put_32 (abfd
, section
->reserved1
, buf
+ 68);
813 bfd_h_put_32 (abfd
, section
->reserved2
, buf
+ 72);
814 bfd_h_put_32 (abfd
, section
->reserved3
, buf
+ 76);
816 if (bfd_bwrite ((PTR
) buf
, BFD_MACH_O_SECTION_64_SIZE
, abfd
)
817 != BFD_MACH_O_SECTION_64_SIZE
)
824 bfd_mach_o_scan_write_segment_32 (bfd
*abfd
, bfd_mach_o_load_command
*command
)
826 unsigned char buf
[BFD_MACH_O_LC_SEGMENT_SIZE
];
827 bfd_mach_o_segment_command
*seg
= &command
->command
.segment
;
830 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SEGMENT
);
832 for (i
= 0; i
< seg
->nsects
; i
++)
833 if (!bfd_mach_o_scan_write_relocs (abfd
, &seg
->sections
[i
]))
836 memcpy (buf
, seg
->segname
, 16);
837 bfd_h_put_32 (abfd
, seg
->vmaddr
, buf
+ 16);
838 bfd_h_put_32 (abfd
, seg
->vmsize
, buf
+ 20);
839 bfd_h_put_32 (abfd
, seg
->fileoff
, buf
+ 24);
840 bfd_h_put_32 (abfd
, seg
->filesize
, buf
+ 28);
841 bfd_h_put_32 (abfd
, seg
->maxprot
, buf
+ 32);
842 bfd_h_put_32 (abfd
, seg
->initprot
, buf
+ 36);
843 bfd_h_put_32 (abfd
, seg
->nsects
, buf
+ 40);
844 bfd_h_put_32 (abfd
, seg
->flags
, buf
+ 44);
846 if (bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
) != 0
847 || (bfd_bwrite ((PTR
) buf
, BFD_MACH_O_LC_SEGMENT_SIZE
- 8, abfd
)
848 != BFD_MACH_O_LC_SEGMENT_SIZE
- 8))
851 for (i
= 0; i
< seg
->nsects
; i
++)
852 if (bfd_mach_o_scan_write_section_32 (abfd
, &seg
->sections
[i
]))
859 bfd_mach_o_scan_write_segment_64 (bfd
*abfd
, bfd_mach_o_load_command
*command
)
861 unsigned char buf
[BFD_MACH_O_LC_SEGMENT_64_SIZE
];
862 bfd_mach_o_segment_command
*seg
= &command
->command
.segment
;
865 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SEGMENT_64
);
867 for (i
= 0; i
< seg
->nsects
; i
++)
868 if (!bfd_mach_o_scan_write_relocs (abfd
, &seg
->sections
[i
]))
871 memcpy (buf
, seg
->segname
, 16);
872 bfd_h_put_64 (abfd
, seg
->vmaddr
, buf
+ 16);
873 bfd_h_put_64 (abfd
, seg
->vmsize
, buf
+ 24);
874 bfd_h_put_64 (abfd
, seg
->fileoff
, buf
+ 32);
875 bfd_h_put_64 (abfd
, seg
->filesize
, buf
+ 40);
876 bfd_h_put_32 (abfd
, seg
->maxprot
, buf
+ 48);
877 bfd_h_put_32 (abfd
, seg
->initprot
, buf
+ 52);
878 bfd_h_put_32 (abfd
, seg
->nsects
, buf
+ 56);
879 bfd_h_put_32 (abfd
, seg
->flags
, buf
+ 60);
881 if (bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
) != 0
882 || (bfd_bwrite ((PTR
) buf
, BFD_MACH_O_LC_SEGMENT_64_SIZE
- 8, abfd
)
883 != BFD_MACH_O_LC_SEGMENT_64_SIZE
- 8))
886 for (i
= 0; i
< seg
->nsects
; i
++)
887 if (bfd_mach_o_scan_write_section_64 (abfd
, &seg
->sections
[i
]))
894 bfd_mach_o_scan_write_symtab (bfd
*abfd
, bfd_mach_o_load_command
*command
)
896 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
897 bfd_mach_o_symtab_command
*sym
= &command
->command
.symtab
;
898 unsigned char buf
[16];
900 unsigned int wide
= bfd_mach_o_wide_p (abfd
);
901 unsigned int symlen
= wide
? BFD_MACH_O_NLIST_64_SIZE
: BFD_MACH_O_NLIST_SIZE
;
902 struct bfd_strtab_hash
*strtab
;
903 asymbol
**symbols
= bfd_get_outsymbols (abfd
);
905 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SYMTAB
);
907 /* Write the symbols first. */
908 mdata
->filelen
= FILE_ALIGN(mdata
->filelen
, wide
? 3 : 2);
909 sym
->symoff
= mdata
->filelen
;
910 if (bfd_seek (abfd
, sym
->symoff
, SEEK_SET
) != 0)
913 sym
->nsyms
= bfd_get_symcount (abfd
);
914 mdata
->filelen
+= sym
->nsyms
* symlen
;
916 strtab
= _bfd_stringtab_init ();
920 for (i
= 0; i
< sym
->nsyms
; i
++)
922 unsigned char buf
[16];
924 bfd_mach_o_asymbol
*s
= (bfd_mach_o_asymbol
*)symbols
[i
];
926 /* Compute name index. */
927 /* An index of 0 always means the empty string. */
928 if (s
->symbol
.name
== 0 || s
->symbol
.name
[0] == '\0')
932 index
= _bfd_stringtab_add (strtab
, s
->symbol
.name
, TRUE
, FALSE
);
933 if (index
== (bfd_size_type
) -1)
936 bfd_h_put_32 (abfd
, index
, buf
);
937 bfd_h_put_8 (abfd
, s
->n_type
, buf
+ 4);
938 bfd_h_put_8 (abfd
, s
->n_sect
, buf
+ 5);
939 bfd_h_put_16 (abfd
, s
->n_desc
, buf
+ 6);
941 bfd_h_put_64 (abfd
, s
->symbol
.section
->vma
+ s
->symbol
.value
, buf
+ 8);
943 bfd_h_put_32 (abfd
, s
->symbol
.section
->vma
+ s
->symbol
.value
, buf
+ 8);
945 if (bfd_bwrite ((PTR
) buf
, symlen
, abfd
) != symlen
)
948 sym
->strsize
= _bfd_stringtab_size (strtab
);
949 sym
->stroff
= mdata
->filelen
;
950 mdata
->filelen
+= sym
->strsize
;
952 if (_bfd_stringtab_emit (abfd
, strtab
) != TRUE
)
954 _bfd_stringtab_free (strtab
);
957 bfd_h_put_32 (abfd
, sym
->symoff
, buf
);
958 bfd_h_put_32 (abfd
, sym
->nsyms
, buf
+ 4);
959 bfd_h_put_32 (abfd
, sym
->stroff
, buf
+ 8);
960 bfd_h_put_32 (abfd
, sym
->strsize
, buf
+ 12);
962 if (bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
) != 0
963 || bfd_bwrite ((PTR
) buf
, 16, abfd
) != 16)
969 _bfd_stringtab_free (strtab
);
973 /* Process the symbols and generate Mach-O specific fields.
977 bfd_mach_o_mangle_symbols (bfd
*abfd
)
980 asymbol
**symbols
= bfd_get_outsymbols (abfd
);
982 for (i
= 0; i
< bfd_get_symcount (abfd
); i
++)
984 bfd_mach_o_asymbol
*s
= (bfd_mach_o_asymbol
*)symbols
[i
];
986 if (s
->n_type
== BFD_MACH_O_N_UNDF
&& !(s
->symbol
.flags
& BSF_DEBUGGING
))
988 /* As genuine Mach-O symbols type shouldn't be N_UNDF (undefined
989 symbols should be N_UNDEF | N_EXT), we suppose the back-end
990 values haven't been set. */
991 if (s
->symbol
.section
== bfd_abs_section_ptr
)
992 s
->n_type
= BFD_MACH_O_N_ABS
;
993 else if (s
->symbol
.section
== bfd_und_section_ptr
)
995 s
->n_type
= BFD_MACH_O_N_UNDF
;
996 if (s
->symbol
.flags
& BSF_WEAK
)
997 s
->n_desc
|= BFD_MACH_O_N_WEAK_REF
;
999 else if (s
->symbol
.section
== bfd_com_section_ptr
)
1000 s
->n_type
= BFD_MACH_O_N_UNDF
| BFD_MACH_O_N_EXT
;
1002 s
->n_type
= BFD_MACH_O_N_SECT
;
1004 if (s
->symbol
.flags
& BSF_GLOBAL
)
1005 s
->n_type
|= BFD_MACH_O_N_EXT
;
1008 /* Compute section index. */
1009 if (s
->symbol
.section
!= bfd_abs_section_ptr
1010 && s
->symbol
.section
!= bfd_und_section_ptr
1011 && s
->symbol
.section
!= bfd_com_section_ptr
)
1012 s
->n_sect
= s
->symbol
.section
->target_index
;
1014 /* Number symbols. */
1015 s
->symbol
.udata
.i
= i
;
1021 bfd_mach_o_write_contents (bfd
*abfd
)
1024 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
1026 if (mdata
->header
.ncmds
== 0)
1027 if (!bfd_mach_o_build_commands (abfd
))
1030 /* Now write header information. */
1031 if (mdata
->header
.filetype
== 0)
1033 if (abfd
->flags
& EXEC_P
)
1034 mdata
->header
.filetype
= BFD_MACH_O_MH_EXECUTE
;
1035 else if (abfd
->flags
& DYNAMIC
)
1036 mdata
->header
.filetype
= BFD_MACH_O_MH_DYLIB
;
1038 mdata
->header
.filetype
= BFD_MACH_O_MH_OBJECT
;
1040 if (!bfd_mach_o_write_header (abfd
, &mdata
->header
))
1043 /* Assign a number to each symbols. */
1044 if (!bfd_mach_o_mangle_symbols (abfd
))
1047 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
1049 unsigned char buf
[8];
1050 bfd_mach_o_load_command
*cur
= &mdata
->commands
[i
];
1051 unsigned long typeflag
;
1053 typeflag
= cur
->type
| (cur
->type_required
? BFD_MACH_O_LC_REQ_DYLD
: 0);
1055 bfd_h_put_32 (abfd
, typeflag
, buf
);
1056 bfd_h_put_32 (abfd
, cur
->len
, buf
+ 4);
1058 if (bfd_seek (abfd
, cur
->offset
, SEEK_SET
) != 0
1059 || bfd_bwrite ((PTR
) buf
, 8, abfd
) != 8)
1064 case BFD_MACH_O_LC_SEGMENT
:
1065 if (bfd_mach_o_scan_write_segment_32 (abfd
, cur
) != 0)
1068 case BFD_MACH_O_LC_SEGMENT_64
:
1069 if (bfd_mach_o_scan_write_segment_64 (abfd
, cur
) != 0)
1072 case BFD_MACH_O_LC_SYMTAB
:
1073 if (!bfd_mach_o_scan_write_symtab (abfd
, cur
))
1076 case BFD_MACH_O_LC_SYMSEG
:
1078 case BFD_MACH_O_LC_THREAD
:
1079 case BFD_MACH_O_LC_UNIXTHREAD
:
1080 if (bfd_mach_o_scan_write_thread (abfd
, cur
) != 0)
1083 case BFD_MACH_O_LC_LOADFVMLIB
:
1084 case BFD_MACH_O_LC_IDFVMLIB
:
1085 case BFD_MACH_O_LC_IDENT
:
1086 case BFD_MACH_O_LC_FVMFILE
:
1087 case BFD_MACH_O_LC_PREPAGE
:
1088 case BFD_MACH_O_LC_DYSYMTAB
:
1089 case BFD_MACH_O_LC_LOAD_DYLIB
:
1090 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB
:
1091 case BFD_MACH_O_LC_ID_DYLIB
:
1092 case BFD_MACH_O_LC_REEXPORT_DYLIB
:
1093 case BFD_MACH_O_LC_LOAD_DYLINKER
:
1094 case BFD_MACH_O_LC_ID_DYLINKER
:
1095 case BFD_MACH_O_LC_PREBOUND_DYLIB
:
1096 case BFD_MACH_O_LC_ROUTINES
:
1097 case BFD_MACH_O_LC_SUB_FRAMEWORK
:
1101 "unable to write unknown load command 0x%lx\n",
1102 (unsigned long) cur
->type
);
1110 /* Build Mach-O load commands from the sections. */
1113 bfd_mach_o_build_commands (bfd
*abfd
)
1115 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
1116 unsigned int wide
= mach_o_wide_p (&mdata
->header
);
1117 bfd_mach_o_segment_command
*seg
;
1118 bfd_mach_o_section
*sections
;
1120 bfd_mach_o_load_command
*cmd
;
1121 bfd_mach_o_load_command
*symtab_cmd
;
1124 /* Return now if commands are already built. */
1125 if (mdata
->header
.ncmds
)
1128 /* Very simple version: 1 command (segment) containing all sections. */
1129 mdata
->header
.ncmds
= 2;
1130 mdata
->commands
= bfd_alloc (abfd
, mdata
->header
.ncmds
1131 * sizeof (bfd_mach_o_load_command
));
1132 if (mdata
->commands
== NULL
)
1134 cmd
= &mdata
->commands
[0];
1135 seg
= &cmd
->command
.segment
;
1137 seg
->nsects
= bfd_count_sections (abfd
);
1138 sections
= bfd_alloc (abfd
, seg
->nsects
* sizeof (bfd_mach_o_section
));
1139 if (sections
== NULL
)
1141 seg
->sections
= sections
;
1143 /* Set segment command. */
1146 cmd
->type
= BFD_MACH_O_LC_SEGMENT_64
;
1147 cmd
->offset
= BFD_MACH_O_HEADER_64_SIZE
;
1148 cmd
->len
= BFD_MACH_O_LC_SEGMENT_64_SIZE
1149 + BFD_MACH_O_SECTION_64_SIZE
* seg
->nsects
;
1153 cmd
->type
= BFD_MACH_O_LC_SEGMENT
;
1154 cmd
->offset
= BFD_MACH_O_HEADER_SIZE
;
1155 cmd
->len
= BFD_MACH_O_LC_SEGMENT_SIZE
1156 + BFD_MACH_O_SECTION_SIZE
* seg
->nsects
;
1158 cmd
->type_required
= FALSE
;
1159 mdata
->header
.sizeofcmds
= cmd
->len
;
1160 mdata
->filelen
= cmd
->offset
+ cmd
->len
;
1162 /* Set symtab command. */
1163 symtab_cmd
= &mdata
->commands
[1];
1165 symtab_cmd
->type
= BFD_MACH_O_LC_SYMTAB
;
1166 symtab_cmd
->offset
= cmd
->offset
+ cmd
->len
;
1167 symtab_cmd
->len
= 6 * 4;
1168 symtab_cmd
->type_required
= FALSE
;
1170 mdata
->header
.sizeofcmds
+= symtab_cmd
->len
;
1171 mdata
->filelen
+= symtab_cmd
->len
;
1173 /* Fill segment command. */
1174 memset (seg
->segname
, 0, sizeof (seg
->segname
));
1176 seg
->fileoff
= mdata
->filelen
;
1178 seg
->maxprot
= BFD_MACH_O_PROT_READ
| BFD_MACH_O_PROT_WRITE
1179 | BFD_MACH_O_PROT_EXECUTE
;
1180 seg
->initprot
= seg
->maxprot
;
1183 /* Create Mach-O sections. */
1185 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1187 sections
->bfdsection
= sec
;
1188 bfd_mach_o_convert_section_name_to_mach_o (abfd
, sec
, sections
);
1189 sections
->addr
= bfd_get_section_vma (abfd
, sec
);
1190 sections
->size
= bfd_get_section_size (sec
);
1191 sections
->align
= bfd_get_section_alignment (abfd
, sec
);
1193 if (sections
->size
!= 0)
1195 mdata
->filelen
= FILE_ALIGN (mdata
->filelen
, sections
->align
);
1196 sections
->offset
= mdata
->filelen
;
1199 sections
->offset
= 0;
1200 sections
->reloff
= 0;
1201 sections
->nreloc
= 0;
1202 sections
->reserved1
= 0;
1203 sections
->reserved2
= 0;
1204 sections
->reserved3
= 0;
1206 sec
->filepos
= sections
->offset
;
1207 sec
->target_index
= ++target_index
;
1209 mdata
->filelen
+= sections
->size
;
1212 seg
->filesize
= mdata
->filelen
- seg
->fileoff
;
1213 seg
->vmsize
= seg
->filesize
;
1218 /* Set the contents of a section. */
1221 bfd_mach_o_set_section_contents (bfd
*abfd
,
1223 const void * location
,
1225 bfd_size_type count
)
1229 /* This must be done first, because bfd_set_section_contents is
1230 going to set output_has_begun to TRUE. */
1231 if (! abfd
->output_has_begun
&& ! bfd_mach_o_build_commands (abfd
))
1237 pos
= section
->filepos
+ offset
;
1238 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
1239 || bfd_bwrite (location
, count
, abfd
) != count
)
1246 bfd_mach_o_sizeof_headers (bfd
*a ATTRIBUTE_UNUSED
,
1247 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
1252 /* Make an empty symbol. This is required only because
1253 bfd_make_section_anyway wants to create a symbol for the section. */
1256 bfd_mach_o_make_empty_symbol (bfd
*abfd
)
1260 new = bfd_zalloc (abfd
, sizeof (bfd_mach_o_asymbol
));
1263 new->the_bfd
= abfd
;
1269 bfd_mach_o_read_header (bfd
*abfd
, bfd_mach_o_header
*header
)
1271 unsigned char buf
[32];
1273 bfd_vma (*get32
) (const void *) = NULL
;
1275 /* Just read the magic number. */
1276 if (bfd_seek (abfd
, 0, SEEK_SET
) != 0
1277 || bfd_bread ((PTR
) buf
, 4, abfd
) != 4)
1280 if (bfd_getb32 (buf
) == BFD_MACH_O_MH_MAGIC
)
1282 header
->byteorder
= BFD_ENDIAN_BIG
;
1283 header
->magic
= BFD_MACH_O_MH_MAGIC
;
1284 header
->version
= 1;
1287 else if (bfd_getl32 (buf
) == BFD_MACH_O_MH_MAGIC
)
1289 header
->byteorder
= BFD_ENDIAN_LITTLE
;
1290 header
->magic
= BFD_MACH_O_MH_MAGIC
;
1291 header
->version
= 1;
1294 else if (bfd_getb32 (buf
) == BFD_MACH_O_MH_MAGIC_64
)
1296 header
->byteorder
= BFD_ENDIAN_BIG
;
1297 header
->magic
= BFD_MACH_O_MH_MAGIC_64
;
1298 header
->version
= 2;
1301 else if (bfd_getl32 (buf
) == BFD_MACH_O_MH_MAGIC_64
)
1303 header
->byteorder
= BFD_ENDIAN_LITTLE
;
1304 header
->magic
= BFD_MACH_O_MH_MAGIC_64
;
1305 header
->version
= 2;
1310 header
->byteorder
= BFD_ENDIAN_UNKNOWN
;
1314 /* Once the size of the header is known, read the full header. */
1315 size
= mach_o_wide_p (header
) ?
1316 BFD_MACH_O_HEADER_64_SIZE
: BFD_MACH_O_HEADER_SIZE
;
1318 if (bfd_seek (abfd
, 0, SEEK_SET
) != 0
1319 || bfd_bread ((PTR
) buf
, size
, abfd
) != size
)
1322 header
->cputype
= (*get32
) (buf
+ 4);
1323 header
->cpusubtype
= (*get32
) (buf
+ 8);
1324 header
->filetype
= (*get32
) (buf
+ 12);
1325 header
->ncmds
= (*get32
) (buf
+ 16);
1326 header
->sizeofcmds
= (*get32
) (buf
+ 20);
1327 header
->flags
= (*get32
) (buf
+ 24);
1329 if (mach_o_wide_p (header
))
1330 header
->reserved
= (*get32
) (buf
+ 28);
1336 bfd_mach_o_make_bfd_section (bfd
*abfd
, bfd_mach_o_section
*section
,
1343 sname
= bfd_mach_o_convert_section_name_to_bfd (abfd
, section
);
1347 if (section
->flags
& BFD_MACH_O_S_ATTR_DEBUG
)
1348 flags
= SEC_HAS_CONTENTS
| SEC_DEBUGGING
;
1352 if ((section
->flags
& BFD_MACH_O_SECTION_TYPE_MASK
)
1353 != BFD_MACH_O_S_ZEROFILL
)
1355 flags
|= SEC_HAS_CONTENTS
| SEC_LOAD
;
1356 if (prot
& BFD_MACH_O_PROT_EXECUTE
)
1358 if (prot
& BFD_MACH_O_PROT_WRITE
)
1360 else if (prot
& BFD_MACH_O_PROT_READ
)
1361 flags
|= SEC_READONLY
;
1364 if (section
->nreloc
!= 0)
1367 bfdsec
= bfd_make_section_anyway_with_flags (abfd
, sname
, flags
);
1371 bfdsec
->vma
= section
->addr
;
1372 bfdsec
->lma
= section
->addr
;
1373 bfdsec
->size
= section
->size
;
1374 bfdsec
->filepos
= section
->offset
;
1375 bfdsec
->alignment_power
= section
->align
;
1376 bfdsec
->segment_mark
= 0;
1377 bfdsec
->reloc_count
= section
->nreloc
;
1378 bfdsec
->rel_filepos
= section
->reloff
;
1384 bfd_mach_o_scan_read_section_32 (bfd
*abfd
,
1385 bfd_mach_o_section
*section
,
1386 unsigned int offset
,
1389 unsigned char buf
[BFD_MACH_O_SECTION_SIZE
];
1391 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0
1392 || (bfd_bread ((PTR
) buf
, BFD_MACH_O_SECTION_SIZE
, abfd
)
1393 != BFD_MACH_O_SECTION_SIZE
))
1396 memcpy (section
->sectname
, buf
, 16);
1397 section
->sectname
[16] = '\0';
1398 memcpy (section
->segname
, buf
+ 16, 16);
1399 section
->segname
[16] = '\0';
1400 section
->addr
= bfd_h_get_32 (abfd
, buf
+ 32);
1401 section
->size
= bfd_h_get_32 (abfd
, buf
+ 36);
1402 section
->offset
= bfd_h_get_32 (abfd
, buf
+ 40);
1403 section
->align
= bfd_h_get_32 (abfd
, buf
+ 44);
1404 section
->reloff
= bfd_h_get_32 (abfd
, buf
+ 48);
1405 section
->nreloc
= bfd_h_get_32 (abfd
, buf
+ 52);
1406 section
->flags
= bfd_h_get_32 (abfd
, buf
+ 56);
1407 section
->reserved1
= bfd_h_get_32 (abfd
, buf
+ 60);
1408 section
->reserved2
= bfd_h_get_32 (abfd
, buf
+ 64);
1409 section
->reserved3
= 0;
1410 section
->bfdsection
= bfd_mach_o_make_bfd_section (abfd
, section
, prot
);
1412 if (section
->bfdsection
== NULL
)
1419 bfd_mach_o_scan_read_section_64 (bfd
*abfd
,
1420 bfd_mach_o_section
*section
,
1421 unsigned int offset
,
1424 unsigned char buf
[BFD_MACH_O_SECTION_64_SIZE
];
1426 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0
1427 || (bfd_bread ((PTR
) buf
, BFD_MACH_O_SECTION_64_SIZE
, abfd
)
1428 != BFD_MACH_O_SECTION_64_SIZE
))
1431 memcpy (section
->sectname
, buf
, 16);
1432 section
->sectname
[16] = '\0';
1433 memcpy (section
->segname
, buf
+ 16, 16);
1434 section
->segname
[16] = '\0';
1435 section
->addr
= bfd_h_get_64 (abfd
, buf
+ 32);
1436 section
->size
= bfd_h_get_64 (abfd
, buf
+ 40);
1437 section
->offset
= bfd_h_get_32 (abfd
, buf
+ 48);
1438 section
->align
= bfd_h_get_32 (abfd
, buf
+ 52);
1439 section
->reloff
= bfd_h_get_32 (abfd
, buf
+ 56);
1440 section
->nreloc
= bfd_h_get_32 (abfd
, buf
+ 60);
1441 section
->flags
= bfd_h_get_32 (abfd
, buf
+ 64);
1442 section
->reserved1
= bfd_h_get_32 (abfd
, buf
+ 68);
1443 section
->reserved2
= bfd_h_get_32 (abfd
, buf
+ 72);
1444 section
->reserved3
= bfd_h_get_32 (abfd
, buf
+ 76);
1445 section
->bfdsection
= bfd_mach_o_make_bfd_section (abfd
, section
, prot
);
1447 if (section
->bfdsection
== NULL
)
1454 bfd_mach_o_scan_read_section (bfd
*abfd
,
1455 bfd_mach_o_section
*section
,
1456 unsigned int offset
,
1461 return bfd_mach_o_scan_read_section_64 (abfd
, section
, offset
, prot
);
1463 return bfd_mach_o_scan_read_section_32 (abfd
, section
, offset
, prot
);
1467 bfd_mach_o_scan_read_symtab_symbol (bfd
*abfd
,
1468 bfd_mach_o_symtab_command
*sym
,
1469 bfd_mach_o_asymbol
*s
,
1472 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
1473 unsigned int wide
= mach_o_wide_p (&mdata
->header
);
1474 unsigned int symwidth
=
1475 wide
? BFD_MACH_O_NLIST_64_SIZE
: BFD_MACH_O_NLIST_SIZE
;
1476 unsigned int symoff
= sym
->symoff
+ (i
* symwidth
);
1477 unsigned char buf
[16];
1478 unsigned char type
= -1;
1479 unsigned char section
= -1;
1481 symvalue value
= -1;
1482 unsigned long stroff
= -1;
1483 unsigned int symtype
= -1;
1485 BFD_ASSERT (sym
->strtab
!= NULL
);
1487 if (bfd_seek (abfd
, symoff
, SEEK_SET
) != 0
1488 || bfd_bread ((PTR
) buf
, symwidth
, abfd
) != symwidth
)
1490 fprintf (stderr
, "bfd_mach_o_scan_read_symtab_symbol: unable to read %d bytes at %lu\n",
1491 symwidth
, (unsigned long) symoff
);
1495 stroff
= bfd_h_get_32 (abfd
, buf
);
1496 type
= bfd_h_get_8 (abfd
, buf
+ 4);
1497 symtype
= type
& BFD_MACH_O_N_TYPE
;
1498 section
= bfd_h_get_8 (abfd
, buf
+ 5);
1499 desc
= bfd_h_get_16 (abfd
, buf
+ 6);
1501 value
= bfd_h_get_64 (abfd
, buf
+ 8);
1503 value
= bfd_h_get_32 (abfd
, buf
+ 8);
1505 if (stroff
>= sym
->strsize
)
1507 fprintf (stderr
, "bfd_mach_o_scan_read_symtab_symbol: symbol name out of range (%lu >= %lu)\n",
1508 (unsigned long) stroff
, (unsigned long) sym
->strsize
);
1512 s
->symbol
.the_bfd
= abfd
;
1513 s
->symbol
.name
= sym
->strtab
+ stroff
;
1514 s
->symbol
.value
= value
;
1515 s
->symbol
.flags
= 0x0;
1516 s
->symbol
.udata
.i
= 0;
1518 s
->n_sect
= section
;
1521 if (type
& BFD_MACH_O_N_STAB
)
1523 s
->symbol
.flags
|= BSF_DEBUGGING
;
1524 s
->symbol
.section
= bfd_und_section_ptr
;
1536 if ((section
> 0) && (section
<= mdata
->nsects
))
1538 s
->symbol
.section
= mdata
->sections
[section
- 1]->bfdsection
;
1540 s
->symbol
.value
- mdata
->sections
[section
- 1]->addr
;
1547 if (type
& BFD_MACH_O_N_PEXT
)
1548 s
->symbol
.flags
|= BSF_GLOBAL
;
1550 if (type
& BFD_MACH_O_N_EXT
)
1551 s
->symbol
.flags
|= BSF_GLOBAL
;
1553 if (!(type
& (BFD_MACH_O_N_PEXT
| BFD_MACH_O_N_EXT
)))
1554 s
->symbol
.flags
|= BSF_LOCAL
;
1558 case BFD_MACH_O_N_UNDF
:
1559 if (type
== (BFD_MACH_O_N_UNDF
| BFD_MACH_O_N_EXT
)
1560 && s
->symbol
.value
!= 0)
1562 /* A common symbol. */
1563 s
->symbol
.section
= bfd_com_section_ptr
;
1564 s
->symbol
.flags
= BSF_NO_FLAGS
;
1568 s
->symbol
.section
= bfd_und_section_ptr
;
1569 if (s
->n_desc
& BFD_MACH_O_N_WEAK_REF
)
1570 s
->symbol
.flags
|= BSF_WEAK
;
1573 case BFD_MACH_O_N_PBUD
:
1574 s
->symbol
.section
= bfd_und_section_ptr
;
1576 case BFD_MACH_O_N_ABS
:
1577 s
->symbol
.section
= bfd_abs_section_ptr
;
1579 case BFD_MACH_O_N_SECT
:
1580 if ((section
> 0) && (section
<= mdata
->nsects
))
1582 s
->symbol
.section
= mdata
->sections
[section
- 1]->bfdsection
;
1584 s
->symbol
.value
- mdata
->sections
[section
- 1]->addr
;
1588 /* Mach-O uses 0 to mean "no section"; not an error. */
1591 fprintf (stderr
, "bfd_mach_o_scan_read_symtab_symbol: "
1592 "symbol \"%s\" specified invalid section %d (max %lu): setting to undefined\n",
1593 s
->symbol
.name
, section
, mdata
->nsects
);
1595 s
->symbol
.section
= bfd_und_section_ptr
;
1598 case BFD_MACH_O_N_INDR
:
1599 fprintf (stderr
, "bfd_mach_o_scan_read_symtab_symbol: "
1600 "symbol \"%s\" is unsupported 'indirect' reference: setting to undefined\n",
1602 s
->symbol
.section
= bfd_und_section_ptr
;
1605 fprintf (stderr
, "bfd_mach_o_scan_read_symtab_symbol: "
1606 "symbol \"%s\" specified invalid type field 0x%x: setting to undefined\n",
1607 s
->symbol
.name
, symtype
);
1608 s
->symbol
.section
= bfd_und_section_ptr
;
1617 bfd_mach_o_scan_read_symtab_strtab (bfd
*abfd
)
1619 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
1620 bfd_mach_o_symtab_command
*sym
= mdata
->symtab
;
1622 /* Fail if there is no symtab. */
1626 /* Success if already loaded. */
1630 if (abfd
->flags
& BFD_IN_MEMORY
)
1632 struct bfd_in_memory
*b
;
1634 b
= (struct bfd_in_memory
*) abfd
->iostream
;
1636 if ((sym
->stroff
+ sym
->strsize
) > b
->size
)
1638 bfd_set_error (bfd_error_file_truncated
);
1641 sym
->strtab
= (char *) b
->buffer
+ sym
->stroff
;
1645 sym
->strtab
= bfd_alloc (abfd
, sym
->strsize
);
1646 if (sym
->strtab
== NULL
)
1649 if (bfd_seek (abfd
, sym
->stroff
, SEEK_SET
) != 0
1650 || bfd_bread ((PTR
) sym
->strtab
, sym
->strsize
, abfd
) != sym
->strsize
)
1652 bfd_set_error (bfd_error_file_truncated
);
1661 bfd_mach_o_scan_read_symtab_symbols (bfd
*abfd
)
1663 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
1664 bfd_mach_o_symtab_command
*sym
= mdata
->symtab
;
1671 sym
->symbols
= bfd_alloc (abfd
, sym
->nsyms
* sizeof (bfd_mach_o_asymbol
));
1673 if (sym
->symbols
== NULL
)
1675 fprintf (stderr
, "bfd_mach_o_scan_read_symtab_symbols: unable to allocate memory for symbols\n");
1679 ret
= bfd_mach_o_scan_read_symtab_strtab (abfd
);
1683 for (i
= 0; i
< sym
->nsyms
; i
++)
1685 ret
= bfd_mach_o_scan_read_symtab_symbol (abfd
, sym
, &sym
->symbols
[i
], i
);
1694 bfd_mach_o_scan_read_dysymtab_symbol (bfd
*abfd
,
1695 bfd_mach_o_dysymtab_command
*dysym
,
1696 bfd_mach_o_symtab_command
*sym
,
1697 bfd_mach_o_asymbol
*s
,
1700 unsigned long isymoff
= dysym
->indirectsymoff
+ (i
* 4);
1701 unsigned long symindex
;
1702 unsigned char buf
[4];
1704 BFD_ASSERT (i
< dysym
->nindirectsyms
);
1706 if (bfd_seek (abfd
, isymoff
, SEEK_SET
) != 0
1707 || bfd_bread ((PTR
) buf
, 4, abfd
) != 4)
1709 fprintf (stderr
, "bfd_mach_o_scan_read_dysymtab_symbol: unable to read %lu bytes at %lu\n",
1710 (unsigned long) 4, isymoff
);
1713 symindex
= bfd_h_get_32 (abfd
, buf
);
1715 return bfd_mach_o_scan_read_symtab_symbol (abfd
, sym
, s
, symindex
);
1719 bfd_mach_o_i386_flavour_string (unsigned int flavour
)
1721 switch ((int) flavour
)
1723 case BFD_MACH_O_x86_THREAD_STATE32
: return "x86_THREAD_STATE32";
1724 case BFD_MACH_O_x86_FLOAT_STATE32
: return "x86_FLOAT_STATE32";
1725 case BFD_MACH_O_x86_EXCEPTION_STATE32
: return "x86_EXCEPTION_STATE32";
1726 case BFD_MACH_O_x86_THREAD_STATE64
: return "x86_THREAD_STATE64";
1727 case BFD_MACH_O_x86_FLOAT_STATE64
: return "x86_FLOAT_STATE64";
1728 case BFD_MACH_O_x86_EXCEPTION_STATE64
: return "x86_EXCEPTION_STATE64";
1729 case BFD_MACH_O_x86_THREAD_STATE
: return "x86_THREAD_STATE";
1730 case BFD_MACH_O_x86_FLOAT_STATE
: return "x86_FLOAT_STATE";
1731 case BFD_MACH_O_x86_EXCEPTION_STATE
: return "x86_EXCEPTION_STATE";
1732 case BFD_MACH_O_x86_DEBUG_STATE32
: return "x86_DEBUG_STATE32";
1733 case BFD_MACH_O_x86_DEBUG_STATE64
: return "x86_DEBUG_STATE64";
1734 case BFD_MACH_O_x86_DEBUG_STATE
: return "x86_DEBUG_STATE";
1735 case BFD_MACH_O_x86_THREAD_STATE_NONE
: return "x86_THREAD_STATE_NONE";
1736 default: return "UNKNOWN";
1741 bfd_mach_o_ppc_flavour_string (unsigned int flavour
)
1743 switch ((int) flavour
)
1745 case BFD_MACH_O_PPC_THREAD_STATE
: return "PPC_THREAD_STATE";
1746 case BFD_MACH_O_PPC_FLOAT_STATE
: return "PPC_FLOAT_STATE";
1747 case BFD_MACH_O_PPC_EXCEPTION_STATE
: return "PPC_EXCEPTION_STATE";
1748 case BFD_MACH_O_PPC_VECTOR_STATE
: return "PPC_VECTOR_STATE";
1749 case BFD_MACH_O_PPC_THREAD_STATE64
: return "PPC_THREAD_STATE64";
1750 case BFD_MACH_O_PPC_EXCEPTION_STATE64
: return "PPC_EXCEPTION_STATE64";
1751 default: return "UNKNOWN";
1756 bfd_mach_o_scan_read_dylinker (bfd
*abfd
,
1757 bfd_mach_o_load_command
*command
)
1759 bfd_mach_o_dylinker_command
*cmd
= &command
->command
.dylinker
;
1760 unsigned char buf
[4];
1761 unsigned int nameoff
;
1763 BFD_ASSERT ((command
->type
== BFD_MACH_O_LC_ID_DYLINKER
)
1764 || (command
->type
== BFD_MACH_O_LC_LOAD_DYLINKER
));
1766 if (bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
) != 0
1767 || bfd_bread ((PTR
) buf
, 4, abfd
) != 4)
1770 nameoff
= bfd_h_get_32 (abfd
, buf
+ 0);
1772 cmd
->name_offset
= command
->offset
+ nameoff
;
1773 cmd
->name_len
= command
->len
- nameoff
;
1774 cmd
->name_str
= bfd_alloc (abfd
, cmd
->name_len
);
1775 if (cmd
->name_str
== NULL
)
1777 if (bfd_seek (abfd
, cmd
->name_offset
, SEEK_SET
) != 0
1778 || bfd_bread (cmd
->name_str
, cmd
->name_len
, abfd
) != cmd
->name_len
)
1784 bfd_mach_o_scan_read_dylib (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1786 bfd_mach_o_dylib_command
*cmd
= &command
->command
.dylib
;
1787 unsigned char buf
[16];
1788 unsigned int nameoff
;
1790 switch (command
->type
)
1792 case BFD_MACH_O_LC_LOAD_DYLIB
:
1793 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB
:
1794 case BFD_MACH_O_LC_ID_DYLIB
:
1795 case BFD_MACH_O_LC_REEXPORT_DYLIB
:
1802 if (bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
) != 0
1803 || bfd_bread ((PTR
) buf
, 16, abfd
) != 16)
1806 nameoff
= bfd_h_get_32 (abfd
, buf
+ 0);
1807 cmd
->timestamp
= bfd_h_get_32 (abfd
, buf
+ 4);
1808 cmd
->current_version
= bfd_h_get_32 (abfd
, buf
+ 8);
1809 cmd
->compatibility_version
= bfd_h_get_32 (abfd
, buf
+ 12);
1811 cmd
->name_offset
= command
->offset
+ nameoff
;
1812 cmd
->name_len
= command
->len
- nameoff
;
1813 cmd
->name_str
= bfd_alloc (abfd
, cmd
->name_len
);
1814 if (cmd
->name_str
== NULL
)
1816 if (bfd_seek (abfd
, cmd
->name_offset
, SEEK_SET
) != 0
1817 || bfd_bread (cmd
->name_str
, cmd
->name_len
, abfd
) != cmd
->name_len
)
1823 bfd_mach_o_scan_read_prebound_dylib (bfd
*abfd ATTRIBUTE_UNUSED
,
1824 bfd_mach_o_load_command
*command ATTRIBUTE_UNUSED
)
1826 /* bfd_mach_o_prebound_dylib_command *cmd = &command->command.prebound_dylib; */
1828 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_PREBOUND_DYLIB
);
1833 bfd_mach_o_scan_read_thread (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1835 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
1836 bfd_mach_o_thread_command
*cmd
= &command
->command
.thread
;
1837 unsigned char buf
[8];
1838 unsigned int offset
;
1839 unsigned int nflavours
;
1842 BFD_ASSERT ((command
->type
== BFD_MACH_O_LC_THREAD
)
1843 || (command
->type
== BFD_MACH_O_LC_UNIXTHREAD
));
1845 /* Count the number of threads. */
1848 while (offset
!= command
->len
)
1850 if (offset
>= command
->len
)
1853 if (bfd_seek (abfd
, command
->offset
+ offset
, SEEK_SET
) != 0
1854 || bfd_bread ((PTR
) buf
, 8, abfd
) != 8)
1857 offset
+= 8 + bfd_h_get_32 (abfd
, buf
+ 4) * 4;
1861 /* Allocate threads. */
1862 cmd
->flavours
= bfd_alloc
1863 (abfd
, nflavours
* sizeof (bfd_mach_o_thread_flavour
));
1864 if (cmd
->flavours
== NULL
)
1866 cmd
->nflavours
= nflavours
;
1870 while (offset
!= command
->len
)
1872 if (offset
>= command
->len
)
1875 if (nflavours
>= cmd
->nflavours
)
1878 if (bfd_seek (abfd
, command
->offset
+ offset
, SEEK_SET
) != 0
1879 || bfd_bread ((PTR
) buf
, 8, abfd
) != 8)
1882 cmd
->flavours
[nflavours
].flavour
= bfd_h_get_32 (abfd
, buf
);
1883 cmd
->flavours
[nflavours
].offset
= command
->offset
+ offset
+ 8;
1884 cmd
->flavours
[nflavours
].size
= bfd_h_get_32 (abfd
, buf
+ 4) * 4;
1885 offset
+= cmd
->flavours
[nflavours
].size
+ 8;
1889 for (i
= 0; i
< nflavours
; i
++)
1892 unsigned int snamelen
;
1894 const char *flavourstr
;
1895 const char *prefix
= "LC_THREAD";
1898 switch (mdata
->header
.cputype
)
1900 case BFD_MACH_O_CPU_TYPE_POWERPC
:
1901 case BFD_MACH_O_CPU_TYPE_POWERPC_64
:
1902 flavourstr
= bfd_mach_o_ppc_flavour_string (cmd
->flavours
[i
].flavour
);
1904 case BFD_MACH_O_CPU_TYPE_I386
:
1905 case BFD_MACH_O_CPU_TYPE_X86_64
:
1906 flavourstr
= bfd_mach_o_i386_flavour_string (cmd
->flavours
[i
].flavour
);
1909 flavourstr
= "UNKNOWN_ARCHITECTURE";
1913 snamelen
= strlen (prefix
) + 1 + 20 + 1 + strlen (flavourstr
) + 1;
1914 sname
= bfd_alloc (abfd
, snamelen
);
1920 sprintf (sname
, "%s.%s.%u", prefix
, flavourstr
, j
);
1921 if (bfd_get_section_by_name (abfd
, sname
) == NULL
)
1926 bfdsec
= bfd_make_section_with_flags (abfd
, sname
, SEC_HAS_CONTENTS
);
1930 bfdsec
->size
= cmd
->flavours
[i
].size
;
1931 bfdsec
->filepos
= cmd
->flavours
[i
].offset
;
1932 bfdsec
->alignment_power
= 0x0;
1934 cmd
->section
= bfdsec
;
1941 bfd_mach_o_scan_read_dysymtab (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1943 bfd_mach_o_dysymtab_command
*cmd
= &command
->command
.dysymtab
;
1944 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
1945 unsigned char buf
[72];
1947 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_DYSYMTAB
);
1949 if (bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
) != 0
1950 || bfd_bread ((PTR
) buf
, 72, abfd
) != 72)
1953 cmd
->ilocalsym
= bfd_h_get_32 (abfd
, buf
+ 0);
1954 cmd
->nlocalsym
= bfd_h_get_32 (abfd
, buf
+ 4);
1955 cmd
->iextdefsym
= bfd_h_get_32 (abfd
, buf
+ 8);
1956 cmd
->nextdefsym
= bfd_h_get_32 (abfd
, buf
+ 12);
1957 cmd
->iundefsym
= bfd_h_get_32 (abfd
, buf
+ 16);
1958 cmd
->nundefsym
= bfd_h_get_32 (abfd
, buf
+ 20);
1959 cmd
->tocoff
= bfd_h_get_32 (abfd
, buf
+ 24);
1960 cmd
->ntoc
= bfd_h_get_32 (abfd
, buf
+ 28);
1961 cmd
->modtaboff
= bfd_h_get_32 (abfd
, buf
+ 32);
1962 cmd
->nmodtab
= bfd_h_get_32 (abfd
, buf
+ 36);
1963 cmd
->extrefsymoff
= bfd_h_get_32 (abfd
, buf
+ 40);
1964 cmd
->nextrefsyms
= bfd_h_get_32 (abfd
, buf
+ 44);
1965 cmd
->indirectsymoff
= bfd_h_get_32 (abfd
, buf
+ 48);
1966 cmd
->nindirectsyms
= bfd_h_get_32 (abfd
, buf
+ 52);
1967 cmd
->extreloff
= bfd_h_get_32 (abfd
, buf
+ 56);
1968 cmd
->nextrel
= bfd_h_get_32 (abfd
, buf
+ 60);
1969 cmd
->locreloff
= bfd_h_get_32 (abfd
, buf
+ 64);
1970 cmd
->nlocrel
= bfd_h_get_32 (abfd
, buf
+ 68);
1972 if (cmd
->nmodtab
!= 0)
1976 int wide
= bfd_mach_o_wide_p (abfd
);
1977 unsigned int module_len
= wide
? 56 : 52;
1980 bfd_alloc (abfd
, cmd
->nmodtab
* sizeof (bfd_mach_o_dylib_module
));
1981 if (cmd
->dylib_module
== NULL
)
1984 if (bfd_seek (abfd
, cmd
->modtaboff
, SEEK_SET
) != 0)
1987 for (i
= 0; i
< cmd
->nmodtab
; i
++)
1989 bfd_mach_o_dylib_module
*module
= &cmd
->dylib_module
[i
];
1992 if (bfd_bread ((PTR
) buf
, module_len
, abfd
) != module_len
)
1995 module
->module_name_idx
= bfd_h_get_32 (abfd
, buf
+ 0);
1996 module
->iextdefsym
= bfd_h_get_32 (abfd
, buf
+ 4);
1997 module
->nextdefsym
= bfd_h_get_32 (abfd
, buf
+ 8);
1998 module
->irefsym
= bfd_h_get_32 (abfd
, buf
+ 12);
1999 module
->nrefsym
= bfd_h_get_32 (abfd
, buf
+ 16);
2000 module
->ilocalsym
= bfd_h_get_32 (abfd
, buf
+ 20);
2001 module
->nlocalsym
= bfd_h_get_32 (abfd
, buf
+ 24);
2002 module
->iextrel
= bfd_h_get_32 (abfd
, buf
+ 28);
2003 module
->nextrel
= bfd_h_get_32 (abfd
, buf
+ 32);
2004 v
= bfd_h_get_32 (abfd
, buf
+36);
2005 module
->iinit
= v
& 0xffff;
2006 module
->iterm
= (v
>> 16) & 0xffff;
2007 v
= bfd_h_get_32 (abfd
, buf
+ 40);
2008 module
->ninit
= v
& 0xffff;
2009 module
->nterm
= (v
>> 16) & 0xffff;
2012 module
->objc_module_info_size
= bfd_h_get_32 (abfd
, buf
+ 44);
2013 module
->objc_module_info_addr
= bfd_h_get_64 (abfd
, buf
+ 48);
2017 module
->objc_module_info_addr
= bfd_h_get_32 (abfd
, buf
+ 44);
2018 module
->objc_module_info_size
= bfd_h_get_32 (abfd
, buf
+ 48);
2028 cmd
->dylib_toc
= bfd_alloc
2029 (abfd
, cmd
->ntoc
* sizeof (bfd_mach_o_dylib_table_of_content
));
2030 if (cmd
->dylib_toc
== NULL
)
2033 if (bfd_seek (abfd
, cmd
->tocoff
, SEEK_SET
) != 0)
2036 for (i
= 0; i
< cmd
->ntoc
; i
++)
2038 bfd_mach_o_dylib_table_of_content
*toc
= &cmd
->dylib_toc
[i
];
2040 if (bfd_bread ((PTR
) buf
, 8, abfd
) != 8)
2043 toc
->symbol_index
= bfd_h_get_32 (abfd
, buf
+ 0);
2044 toc
->module_index
= bfd_h_get_32 (abfd
, buf
+ 4);
2048 if (cmd
->nindirectsyms
!= 0)
2053 cmd
->indirect_syms
= bfd_alloc
2054 (abfd
, cmd
->nindirectsyms
* sizeof (unsigned int));
2055 if (cmd
->indirect_syms
== NULL
)
2058 if (bfd_seek (abfd
, cmd
->indirectsymoff
, SEEK_SET
) != 0)
2061 for (i
= 0; i
< cmd
->nindirectsyms
; i
++)
2063 unsigned int *is
= &cmd
->indirect_syms
[i
];
2065 if (bfd_bread ((PTR
) buf
, 4, abfd
) != 4)
2068 *is
= bfd_h_get_32 (abfd
, buf
+ 0);
2072 if (cmd
->nextrefsyms
!= 0)
2078 cmd
->ext_refs
= bfd_alloc
2079 (abfd
, cmd
->nextrefsyms
* sizeof (bfd_mach_o_dylib_reference
));
2080 if (cmd
->ext_refs
== NULL
)
2083 if (bfd_seek (abfd
, cmd
->extrefsymoff
, SEEK_SET
) != 0)
2086 for (i
= 0; i
< cmd
->nextrefsyms
; i
++)
2088 bfd_mach_o_dylib_reference
*ref
= &cmd
->ext_refs
[i
];
2090 if (bfd_bread ((PTR
) buf
, 4, abfd
) != 4)
2093 /* Fields isym and flags are written as bit-fields, thus we need
2094 a specific processing for endianness. */
2095 v
= bfd_h_get_32 (abfd
, buf
+ 0);
2096 if (bfd_big_endian (abfd
))
2098 ref
->isym
= (v
>> 8) & 0xffffff;
2099 ref
->flags
= v
& 0xff;
2103 ref
->isym
= v
& 0xffffff;
2104 ref
->flags
= (v
>> 24) & 0xff;
2109 if (mdata
->dysymtab
)
2111 mdata
->dysymtab
= cmd
;
2117 bfd_mach_o_scan_read_symtab (bfd
*abfd
, bfd_mach_o_load_command
*command
)
2119 bfd_mach_o_symtab_command
*symtab
= &command
->command
.symtab
;
2120 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
2121 unsigned char buf
[16];
2123 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SYMTAB
);
2125 if (bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
) != 0
2126 || bfd_bread ((PTR
) buf
, 16, abfd
) != 16)
2129 symtab
->symoff
= bfd_h_get_32 (abfd
, buf
);
2130 symtab
->nsyms
= bfd_h_get_32 (abfd
, buf
+ 4);
2131 symtab
->stroff
= bfd_h_get_32 (abfd
, buf
+ 8);
2132 symtab
->strsize
= bfd_h_get_32 (abfd
, buf
+ 12);
2133 symtab
->symbols
= NULL
;
2134 symtab
->strtab
= NULL
;
2136 if (symtab
->nsyms
!= 0)
2137 abfd
->flags
|= HAS_SYMS
;
2141 mdata
->symtab
= symtab
;
2146 bfd_mach_o_scan_read_uuid (bfd
*abfd
, bfd_mach_o_load_command
*command
)
2148 bfd_mach_o_uuid_command
*cmd
= &command
->command
.uuid
;
2151 static const char prefix
[] = "LC_UUID";
2153 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_UUID
);
2155 if (bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
) != 0
2156 || bfd_bread ((PTR
) cmd
->uuid
, 16, abfd
) != 16)
2159 sname
= bfd_alloc (abfd
, strlen (prefix
) + 1);
2162 strcpy (sname
, prefix
);
2164 bfdsec
= bfd_make_section_anyway_with_flags (abfd
, sname
, SEC_HAS_CONTENTS
);
2170 bfdsec
->size
= command
->len
- 8;
2171 bfdsec
->filepos
= command
->offset
+ 8;
2172 bfdsec
->alignment_power
= 0;
2174 cmd
->section
= bfdsec
;
2180 bfd_mach_o_scan_read_linkedit (bfd
*abfd
, bfd_mach_o_load_command
*command
)
2182 bfd_mach_o_linkedit_command
*cmd
= &command
->command
.linkedit
;
2185 if (bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
) != 0
2186 || bfd_bread ((PTR
) buf
, 8, abfd
) != 8)
2189 cmd
->dataoff
= bfd_get_32 (abfd
, buf
+ 0);
2190 cmd
->datasize
= bfd_get_32 (abfd
, buf
+ 4);
2195 bfd_mach_o_scan_read_str (bfd
*abfd
, bfd_mach_o_load_command
*command
)
2197 bfd_mach_o_str_command
*cmd
= &command
->command
.str
;
2201 if (bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
) != 0
2202 || bfd_bread ((PTR
) buf
, 4, abfd
) != 4)
2205 off
= bfd_get_32 (abfd
, buf
+ 0);
2206 cmd
->stroff
= command
->offset
+ off
;
2207 cmd
->str_len
= command
->len
- off
;
2208 cmd
->str
= bfd_alloc (abfd
, cmd
->str_len
);
2209 if (cmd
->str
== NULL
)
2211 if (bfd_seek (abfd
, cmd
->stroff
, SEEK_SET
) != 0
2212 || bfd_bread ((PTR
) cmd
->str
, cmd
->str_len
, abfd
) != cmd
->str_len
)
2218 bfd_mach_o_scan_read_segment (bfd
*abfd
,
2219 bfd_mach_o_load_command
*command
,
2222 unsigned char buf
[64];
2223 bfd_mach_o_segment_command
*seg
= &command
->command
.segment
;
2228 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SEGMENT_64
);
2230 if (bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
) != 0
2231 || bfd_bread ((PTR
) buf
, 64, abfd
) != 64)
2234 memcpy (seg
->segname
, buf
, 16);
2235 seg
->segname
[16] = '\0';
2237 seg
->vmaddr
= bfd_h_get_64 (abfd
, buf
+ 16);
2238 seg
->vmsize
= bfd_h_get_64 (abfd
, buf
+ 24);
2239 seg
->fileoff
= bfd_h_get_64 (abfd
, buf
+ 32);
2240 seg
->filesize
= bfd_h_get_64 (abfd
, buf
+ 40);
2241 seg
->maxprot
= bfd_h_get_32 (abfd
, buf
+ 48);
2242 seg
->initprot
= bfd_h_get_32 (abfd
, buf
+ 52);
2243 seg
->nsects
= bfd_h_get_32 (abfd
, buf
+ 56);
2244 seg
->flags
= bfd_h_get_32 (abfd
, buf
+ 60);
2248 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SEGMENT
);
2250 if (bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
) != 0
2251 || bfd_bread ((PTR
) buf
, 48, abfd
) != 48)
2254 memcpy (seg
->segname
, buf
, 16);
2255 seg
->segname
[16] = '\0';
2257 seg
->vmaddr
= bfd_h_get_32 (abfd
, buf
+ 16);
2258 seg
->vmsize
= bfd_h_get_32 (abfd
, buf
+ 20);
2259 seg
->fileoff
= bfd_h_get_32 (abfd
, buf
+ 24);
2260 seg
->filesize
= bfd_h_get_32 (abfd
, buf
+ 28);
2261 seg
->maxprot
= bfd_h_get_32 (abfd
, buf
+ 32);
2262 seg
->initprot
= bfd_h_get_32 (abfd
, buf
+ 36);
2263 seg
->nsects
= bfd_h_get_32 (abfd
, buf
+ 40);
2264 seg
->flags
= bfd_h_get_32 (abfd
, buf
+ 44);
2267 if (seg
->nsects
!= 0)
2269 seg
->sections
= bfd_alloc (abfd
, seg
->nsects
2270 * sizeof (bfd_mach_o_section
));
2271 if (seg
->sections
== NULL
)
2274 for (i
= 0; i
< seg
->nsects
; i
++)
2278 segoff
= command
->offset
+ BFD_MACH_O_LC_SEGMENT_64_SIZE
2279 + (i
* BFD_MACH_O_SECTION_64_SIZE
);
2281 segoff
= command
->offset
+ BFD_MACH_O_LC_SEGMENT_SIZE
2282 + (i
* BFD_MACH_O_SECTION_SIZE
);
2284 if (bfd_mach_o_scan_read_section
2285 (abfd
, &seg
->sections
[i
], segoff
, seg
->initprot
, wide
) != 0)
2294 bfd_mach_o_scan_read_segment_32 (bfd
*abfd
, bfd_mach_o_load_command
*command
)
2296 return bfd_mach_o_scan_read_segment (abfd
, command
, 0);
2300 bfd_mach_o_scan_read_segment_64 (bfd
*abfd
, bfd_mach_o_load_command
*command
)
2302 return bfd_mach_o_scan_read_segment (abfd
, command
, 1);
2306 bfd_mach_o_scan_read_command (bfd
*abfd
, bfd_mach_o_load_command
*command
)
2308 unsigned char buf
[8];
2310 /* Read command type and length. */
2311 if (bfd_seek (abfd
, command
->offset
, SEEK_SET
) != 0
2312 || bfd_bread ((PTR
) buf
, 8, abfd
) != 8)
2315 command
->type
= bfd_h_get_32 (abfd
, buf
) & ~BFD_MACH_O_LC_REQ_DYLD
;
2316 command
->type_required
= (bfd_h_get_32 (abfd
, buf
) & BFD_MACH_O_LC_REQ_DYLD
2318 command
->len
= bfd_h_get_32 (abfd
, buf
+ 4);
2320 switch (command
->type
)
2322 case BFD_MACH_O_LC_SEGMENT
:
2323 if (bfd_mach_o_scan_read_segment_32 (abfd
, command
) != 0)
2326 case BFD_MACH_O_LC_SEGMENT_64
:
2327 if (bfd_mach_o_scan_read_segment_64 (abfd
, command
) != 0)
2330 case BFD_MACH_O_LC_SYMTAB
:
2331 if (bfd_mach_o_scan_read_symtab (abfd
, command
) != 0)
2334 case BFD_MACH_O_LC_SYMSEG
:
2336 case BFD_MACH_O_LC_THREAD
:
2337 case BFD_MACH_O_LC_UNIXTHREAD
:
2338 if (bfd_mach_o_scan_read_thread (abfd
, command
) != 0)
2341 case BFD_MACH_O_LC_LOAD_DYLINKER
:
2342 case BFD_MACH_O_LC_ID_DYLINKER
:
2343 if (bfd_mach_o_scan_read_dylinker (abfd
, command
) != 0)
2346 case BFD_MACH_O_LC_LOAD_DYLIB
:
2347 case BFD_MACH_O_LC_ID_DYLIB
:
2348 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB
:
2349 case BFD_MACH_O_LC_REEXPORT_DYLIB
:
2350 if (bfd_mach_o_scan_read_dylib (abfd
, command
) != 0)
2353 case BFD_MACH_O_LC_PREBOUND_DYLIB
:
2354 if (bfd_mach_o_scan_read_prebound_dylib (abfd
, command
) != 0)
2357 case BFD_MACH_O_LC_LOADFVMLIB
:
2358 case BFD_MACH_O_LC_IDFVMLIB
:
2359 case BFD_MACH_O_LC_IDENT
:
2360 case BFD_MACH_O_LC_FVMFILE
:
2361 case BFD_MACH_O_LC_PREPAGE
:
2362 case BFD_MACH_O_LC_ROUTINES
:
2364 case BFD_MACH_O_LC_SUB_FRAMEWORK
:
2365 case BFD_MACH_O_LC_SUB_UMBRELLA
:
2366 case BFD_MACH_O_LC_SUB_LIBRARY
:
2367 case BFD_MACH_O_LC_SUB_CLIENT
:
2368 if (bfd_mach_o_scan_read_str (abfd
, command
) != 0)
2371 case BFD_MACH_O_LC_DYSYMTAB
:
2372 if (bfd_mach_o_scan_read_dysymtab (abfd
, command
) != 0)
2375 case BFD_MACH_O_LC_TWOLEVEL_HINTS
:
2376 case BFD_MACH_O_LC_PREBIND_CKSUM
:
2378 case BFD_MACH_O_LC_UUID
:
2379 if (bfd_mach_o_scan_read_uuid (abfd
, command
) != 0)
2382 case BFD_MACH_O_LC_CODE_SIGNATURE
:
2383 case BFD_MACH_O_LC_SEGMENT_SPLIT_INFO
:
2384 if (bfd_mach_o_scan_read_linkedit (abfd
, command
) != 0)
2388 fprintf (stderr
, "unable to read unknown load command 0x%lx\n",
2389 (unsigned long) command
->type
);
2397 bfd_mach_o_flatten_sections (bfd
*abfd
)
2399 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
2403 /* Count total number of sections. */
2406 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
2408 if (mdata
->commands
[i
].type
== BFD_MACH_O_LC_SEGMENT
2409 || mdata
->commands
[i
].type
== BFD_MACH_O_LC_SEGMENT_64
)
2411 bfd_mach_o_segment_command
*seg
;
2413 seg
= &mdata
->commands
[i
].command
.segment
;
2414 mdata
->nsects
+= seg
->nsects
;
2418 /* Allocate sections array. */
2419 mdata
->sections
= bfd_alloc (abfd
,
2420 mdata
->nsects
* sizeof (bfd_mach_o_section
*));
2422 /* Fill the array. */
2425 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
2427 if (mdata
->commands
[i
].type
== BFD_MACH_O_LC_SEGMENT
2428 || mdata
->commands
[i
].type
== BFD_MACH_O_LC_SEGMENT_64
)
2430 bfd_mach_o_segment_command
*seg
;
2432 seg
= &mdata
->commands
[i
].command
.segment
;
2433 BFD_ASSERT (csect
+ seg
->nsects
<= mdata
->nsects
);
2435 for (j
= 0; j
< seg
->nsects
; j
++)
2436 mdata
->sections
[csect
++] = &seg
->sections
[j
];
2442 bfd_mach_o_scan_start_address (bfd
*abfd
)
2444 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
2445 bfd_mach_o_thread_command
*cmd
= NULL
;
2448 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
2450 if ((mdata
->commands
[i
].type
== BFD_MACH_O_LC_THREAD
) ||
2451 (mdata
->commands
[i
].type
== BFD_MACH_O_LC_UNIXTHREAD
))
2454 cmd
= &mdata
->commands
[i
].command
.thread
;
2463 for (i
= 0; i
< cmd
->nflavours
; i
++)
2465 if ((mdata
->header
.cputype
== BFD_MACH_O_CPU_TYPE_I386
)
2466 && (cmd
->flavours
[i
].flavour
2467 == (unsigned long) BFD_MACH_O_x86_THREAD_STATE32
))
2469 unsigned char buf
[4];
2471 if (bfd_seek (abfd
, cmd
->flavours
[i
].offset
+ 40, SEEK_SET
) != 0
2472 || bfd_bread (buf
, 4, abfd
) != 4)
2475 abfd
->start_address
= bfd_h_get_32 (abfd
, buf
);
2477 else if ((mdata
->header
.cputype
== BFD_MACH_O_CPU_TYPE_POWERPC
)
2478 && (cmd
->flavours
[i
].flavour
== BFD_MACH_O_PPC_THREAD_STATE
))
2480 unsigned char buf
[4];
2482 if (bfd_seek (abfd
, cmd
->flavours
[i
].offset
+ 0, SEEK_SET
) != 0
2483 || bfd_bread (buf
, 4, abfd
) != 4)
2486 abfd
->start_address
= bfd_h_get_32 (abfd
, buf
);
2488 else if ((mdata
->header
.cputype
== BFD_MACH_O_CPU_TYPE_POWERPC_64
)
2489 && (cmd
->flavours
[i
].flavour
== BFD_MACH_O_PPC_THREAD_STATE64
))
2491 unsigned char buf
[8];
2493 if (bfd_seek (abfd
, cmd
->flavours
[i
].offset
+ 0, SEEK_SET
) != 0
2494 || bfd_bread (buf
, 8, abfd
) != 8)
2497 abfd
->start_address
= bfd_h_get_64 (abfd
, buf
);
2499 else if ((mdata
->header
.cputype
== BFD_MACH_O_CPU_TYPE_X86_64
)
2500 && (cmd
->flavours
[i
].flavour
== BFD_MACH_O_x86_THREAD_STATE64
))
2502 unsigned char buf
[8];
2504 if (bfd_seek (abfd
, cmd
->flavours
[i
].offset
+ (16 * 8), SEEK_SET
) != 0
2505 || bfd_bread (buf
, 8, abfd
) != 8)
2508 abfd
->start_address
= bfd_h_get_64 (abfd
, buf
);
2516 bfd_mach_o_scan (bfd
*abfd
,
2517 bfd_mach_o_header
*header
,
2518 bfd_mach_o_data_struct
*mdata
)
2521 enum bfd_architecture cputype
;
2522 unsigned long cpusubtype
;
2523 unsigned int hdrsize
;
2525 hdrsize
= mach_o_wide_p (header
) ?
2526 BFD_MACH_O_HEADER_64_SIZE
: BFD_MACH_O_HEADER_SIZE
;
2528 mdata
->header
= *header
;
2530 abfd
->flags
= abfd
->flags
& BFD_IN_MEMORY
;
2531 switch (header
->filetype
)
2533 case BFD_MACH_O_MH_OBJECT
:
2534 abfd
->flags
|= HAS_RELOC
;
2536 case BFD_MACH_O_MH_EXECUTE
:
2537 abfd
->flags
|= EXEC_P
;
2539 case BFD_MACH_O_MH_DYLIB
:
2540 case BFD_MACH_O_MH_BUNDLE
:
2541 abfd
->flags
|= DYNAMIC
;
2545 abfd
->tdata
.mach_o_data
= mdata
;
2547 bfd_mach_o_convert_architecture (header
->cputype
, header
->cpusubtype
,
2548 &cputype
, &cpusubtype
);
2549 if (cputype
== bfd_arch_unknown
)
2551 fprintf (stderr
, "bfd_mach_o_scan: unknown architecture 0x%lx/0x%lx\n",
2552 header
->cputype
, header
->cpusubtype
);
2556 bfd_set_arch_mach (abfd
, cputype
, cpusubtype
);
2558 if (header
->ncmds
!= 0)
2560 mdata
->commands
= bfd_alloc
2561 (abfd
, header
->ncmds
* sizeof (bfd_mach_o_load_command
));
2562 if (mdata
->commands
== NULL
)
2565 for (i
= 0; i
< header
->ncmds
; i
++)
2567 bfd_mach_o_load_command
*cur
= &mdata
->commands
[i
];
2570 cur
->offset
= hdrsize
;
2573 bfd_mach_o_load_command
*prev
= &mdata
->commands
[i
- 1];
2574 cur
->offset
= prev
->offset
+ prev
->len
;
2577 if (bfd_mach_o_scan_read_command (abfd
, cur
) < 0)
2582 if (bfd_mach_o_scan_start_address (abfd
) < 0)
2585 bfd_mach_o_flatten_sections (abfd
);
2590 bfd_mach_o_mkobject_init (bfd
*abfd
)
2592 bfd_mach_o_data_struct
*mdata
= NULL
;
2594 mdata
= bfd_alloc (abfd
, sizeof (bfd_mach_o_data_struct
));
2597 abfd
->tdata
.mach_o_data
= mdata
;
2599 mdata
->header
.magic
= 0;
2600 mdata
->header
.cputype
= 0;
2601 mdata
->header
.cpusubtype
= 0;
2602 mdata
->header
.filetype
= 0;
2603 mdata
->header
.ncmds
= 0;
2604 mdata
->header
.sizeofcmds
= 0;
2605 mdata
->header
.flags
= 0;
2606 mdata
->header
.byteorder
= BFD_ENDIAN_UNKNOWN
;
2607 mdata
->commands
= NULL
;
2609 mdata
->sections
= NULL
;
2615 bfd_mach_o_header_p (bfd
*abfd
,
2616 bfd_mach_o_filetype filetype
,
2617 bfd_mach_o_cpu_type cputype
)
2619 struct bfd_preserve preserve
;
2620 bfd_mach_o_header header
;
2622 preserve
.marker
= NULL
;
2623 if (!bfd_mach_o_read_header (abfd
, &header
))
2626 if (! (header
.byteorder
== BFD_ENDIAN_BIG
2627 || header
.byteorder
== BFD_ENDIAN_LITTLE
))
2629 fprintf (stderr
, "unknown header byte-order value 0x%lx\n",
2630 (unsigned long) header
.byteorder
);
2634 if (! ((header
.byteorder
== BFD_ENDIAN_BIG
2635 && abfd
->xvec
->byteorder
== BFD_ENDIAN_BIG
2636 && abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
2637 || (header
.byteorder
== BFD_ENDIAN_LITTLE
2638 && abfd
->xvec
->byteorder
== BFD_ENDIAN_LITTLE
2639 && abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)))
2642 /* Check cputype and filetype.
2643 In case of wildcard, do not accept magics that are handled by existing
2647 if (header
.cputype
!= cputype
)
2652 switch (header
.cputype
)
2654 case BFD_MACH_O_CPU_TYPE_I386
:
2655 /* Handled by mach-o-i386 */
2663 if (header
.filetype
!= filetype
)
2668 switch (header
.filetype
)
2670 case BFD_MACH_O_MH_CORE
:
2671 /* Handled by core_p */
2678 preserve
.marker
= bfd_zalloc (abfd
, sizeof (bfd_mach_o_data_struct
));
2679 if (preserve
.marker
== NULL
2680 || !bfd_preserve_save (abfd
, &preserve
))
2683 if (bfd_mach_o_scan (abfd
, &header
,
2684 (bfd_mach_o_data_struct
*) preserve
.marker
) != 0)
2687 bfd_preserve_finish (abfd
, &preserve
);
2691 bfd_set_error (bfd_error_wrong_format
);
2694 if (preserve
.marker
!= NULL
)
2695 bfd_preserve_restore (abfd
, &preserve
);
2699 static const bfd_target
*
2700 bfd_mach_o_gen_object_p (bfd
*abfd
)
2702 return bfd_mach_o_header_p (abfd
, 0, 0);
2705 static const bfd_target
*
2706 bfd_mach_o_gen_core_p (bfd
*abfd
)
2708 return bfd_mach_o_header_p (abfd
, BFD_MACH_O_MH_CORE
, 0);
2711 typedef struct mach_o_fat_archentry
2713 unsigned long cputype
;
2714 unsigned long cpusubtype
;
2715 unsigned long offset
;
2717 unsigned long align
;
2718 } mach_o_fat_archentry
;
2720 typedef struct mach_o_fat_data_struct
2722 unsigned long magic
;
2723 unsigned long nfat_arch
;
2724 mach_o_fat_archentry
*archentries
;
2725 } mach_o_fat_data_struct
;
2728 bfd_mach_o_archive_p (bfd
*abfd
)
2730 mach_o_fat_data_struct
*adata
= NULL
;
2731 unsigned char buf
[20];
2734 if (bfd_seek (abfd
, 0, SEEK_SET
) != 0
2735 || bfd_bread ((PTR
) buf
, 8, abfd
) != 8)
2738 adata
= bfd_alloc (abfd
, sizeof (mach_o_fat_data_struct
));
2742 adata
->magic
= bfd_getb32 (buf
);
2743 adata
->nfat_arch
= bfd_getb32 (buf
+ 4);
2744 if (adata
->magic
!= 0xcafebabe)
2746 /* Avoid matching Java bytecode files, which have the same magic number.
2747 In the Java bytecode file format this field contains the JVM version,
2748 which starts at 43.0. */
2749 if (adata
->nfat_arch
> 30)
2752 adata
->archentries
=
2753 bfd_alloc (abfd
, adata
->nfat_arch
* sizeof (mach_o_fat_archentry
));
2754 if (adata
->archentries
== NULL
)
2757 for (i
= 0; i
< adata
->nfat_arch
; i
++)
2759 if (bfd_seek (abfd
, 8 + 20 * i
, SEEK_SET
) != 0
2760 || bfd_bread ((PTR
) buf
, 20, abfd
) != 20)
2762 adata
->archentries
[i
].cputype
= bfd_getb32 (buf
);
2763 adata
->archentries
[i
].cpusubtype
= bfd_getb32 (buf
+ 4);
2764 adata
->archentries
[i
].offset
= bfd_getb32 (buf
+ 8);
2765 adata
->archentries
[i
].size
= bfd_getb32 (buf
+ 12);
2766 adata
->archentries
[i
].align
= bfd_getb32 (buf
+ 16);
2769 abfd
->tdata
.mach_o_fat_data
= adata
;
2774 bfd_release (abfd
, adata
);
2775 bfd_set_error (bfd_error_wrong_format
);
2780 bfd_mach_o_openr_next_archived_file (bfd
*archive
, bfd
*prev
)
2782 mach_o_fat_data_struct
*adata
;
2783 mach_o_fat_archentry
*entry
= NULL
;
2786 enum bfd_architecture arch_type
;
2787 unsigned long arch_subtype
;
2789 adata
= (mach_o_fat_data_struct
*) archive
->tdata
.mach_o_fat_data
;
2790 BFD_ASSERT (adata
!= NULL
);
2792 /* Find index of previous entry. */
2794 i
= 0; /* Start at first one. */
2797 for (i
= 0; i
< adata
->nfat_arch
; i
++)
2799 if (adata
->archentries
[i
].offset
== prev
->origin
)
2803 if (i
== adata
->nfat_arch
)
2806 bfd_set_error (bfd_error_bad_value
);
2809 i
++; /* Get next entry. */
2812 if (i
>= adata
->nfat_arch
)
2814 bfd_set_error (bfd_error_no_more_archived_files
);
2818 entry
= &adata
->archentries
[i
];
2819 nbfd
= _bfd_new_bfd_contained_in (archive
);
2823 nbfd
->origin
= entry
->offset
;
2825 bfd_mach_o_convert_architecture (entry
->cputype
, entry
->cpusubtype
,
2826 &arch_type
, &arch_subtype
);
2827 /* Create the member filename.
2828 Use FILENAME:ARCH_NAME. */
2831 const char *arch_name
;
2832 size_t arch_file_len
= strlen (bfd_get_filename (archive
));
2834 arch_name
= bfd_printable_arch_mach (arch_type
, arch_subtype
);
2835 s
= bfd_malloc (arch_file_len
+ 1 + strlen (arch_name
) + 1);
2838 memcpy (s
, bfd_get_filename (archive
), arch_file_len
);
2839 s
[arch_file_len
] = ':';
2840 strcpy (s
+ arch_file_len
+ 1, arch_name
);
2843 nbfd
->iostream
= NULL
;
2844 bfd_set_arch_mach (nbfd
, arch_type
, arch_subtype
);
2849 /* If ABFD format is FORMAT and architecture is ARCH, return it.
2850 If ABFD is a fat image containing a member that corresponds to FORMAT
2851 and ARCH, returns it.
2852 In other case, returns NULL.
2853 This function allows transparent uses of fat images. */
2855 bfd_mach_o_fat_extract (bfd
*abfd
,
2857 const bfd_arch_info_type
*arch
)
2860 mach_o_fat_data_struct
*adata
;
2863 if (bfd_check_format (abfd
, format
))
2865 if (bfd_get_arch_info (abfd
) == arch
)
2869 if (!bfd_check_format (abfd
, bfd_archive
)
2870 || abfd
->xvec
!= &mach_o_fat_vec
)
2873 /* This is a Mach-O fat image. */
2874 adata
= (mach_o_fat_data_struct
*) abfd
->tdata
.mach_o_fat_data
;
2875 BFD_ASSERT (adata
!= NULL
);
2877 for (i
= 0; i
< adata
->nfat_arch
; i
++)
2879 struct mach_o_fat_archentry
*e
= &adata
->archentries
[i
];
2880 enum bfd_architecture cpu_type
;
2881 unsigned long cpu_subtype
;
2883 bfd_mach_o_convert_architecture (e
->cputype
, e
->cpusubtype
,
2884 &cpu_type
, &cpu_subtype
);
2885 if (cpu_type
!= arch
->arch
|| cpu_subtype
!= arch
->mach
)
2888 /* The architecture is found. */
2889 res
= _bfd_new_bfd_contained_in (abfd
);
2893 res
->origin
= e
->offset
;
2895 res
->filename
= strdup (abfd
->filename
);
2896 res
->iostream
= NULL
;
2898 if (bfd_check_format (res
, format
))
2900 BFD_ASSERT (bfd_get_arch_info (res
) == arch
);
2911 bfd_mach_o_lookup_section (bfd
*abfd
,
2913 bfd_mach_o_load_command
**mcommand
,
2914 bfd_mach_o_section
**msection
)
2916 struct mach_o_data_struct
*md
= bfd_mach_o_get_data (abfd
);
2917 unsigned int i
, j
, num
;
2919 bfd_mach_o_load_command
*ncmd
= NULL
;
2920 bfd_mach_o_section
*nsect
= NULL
;
2922 BFD_ASSERT (mcommand
!= NULL
);
2923 BFD_ASSERT (msection
!= NULL
);
2926 for (i
= 0; i
< md
->header
.ncmds
; i
++)
2928 struct bfd_mach_o_load_command
*cmd
= &md
->commands
[i
];
2929 struct bfd_mach_o_segment_command
*seg
= NULL
;
2931 if (cmd
->type
!= BFD_MACH_O_LC_SEGMENT
2932 || cmd
->type
!= BFD_MACH_O_LC_SEGMENT_64
)
2934 seg
= &cmd
->command
.segment
;
2936 for (j
= 0; j
< seg
->nsects
; j
++)
2938 struct bfd_mach_o_section
*sect
= &seg
->sections
[j
];
2940 if (sect
->bfdsection
== section
)
2958 bfd_mach_o_lookup_command (bfd
*abfd
,
2959 bfd_mach_o_load_command_type type
,
2960 bfd_mach_o_load_command
**mcommand
)
2962 struct mach_o_data_struct
*md
= bfd_mach_o_get_data (abfd
);
2963 bfd_mach_o_load_command
*ncmd
= NULL
;
2964 unsigned int i
, num
;
2966 BFD_ASSERT (md
!= NULL
);
2967 BFD_ASSERT (mcommand
!= NULL
);
2970 for (i
= 0; i
< md
->header
.ncmds
; i
++)
2972 struct bfd_mach_o_load_command
*cmd
= &md
->commands
[i
];
2974 if (cmd
->type
!= type
)
2987 bfd_mach_o_stack_addr (enum bfd_mach_o_cpu_type type
)
2991 case BFD_MACH_O_CPU_TYPE_MC680x0
:
2993 case BFD_MACH_O_CPU_TYPE_MC88000
:
2995 case BFD_MACH_O_CPU_TYPE_POWERPC
:
2997 case BFD_MACH_O_CPU_TYPE_I386
:
2999 case BFD_MACH_O_CPU_TYPE_SPARC
:
3001 case BFD_MACH_O_CPU_TYPE_I860
:
3003 case BFD_MACH_O_CPU_TYPE_HPPA
:
3004 return 0xc0000000 - 0x04000000;
3010 typedef struct bfd_mach_o_xlat_name
3015 bfd_mach_o_xlat_name
;
3018 bfd_mach_o_print_flags (const bfd_mach_o_xlat_name
*table
,
3024 for (; table
->name
; table
++)
3026 if (table
->val
& val
)
3029 fprintf (file
, "+");
3030 fprintf (file
, "%s", table
->name
);
3038 fprintf (file
, "+");
3039 fprintf (file
, "0x%lx", val
);
3043 fprintf (file
, "-");
3047 bfd_mach_o_get_name (const bfd_mach_o_xlat_name
*table
, unsigned long val
)
3049 for (; table
->name
; table
++)
3050 if (table
->val
== val
)
3055 static bfd_mach_o_xlat_name bfd_mach_o_cpu_name
[] =
3057 { "vax", BFD_MACH_O_CPU_TYPE_VAX
},
3058 { "mc680x0", BFD_MACH_O_CPU_TYPE_MC680x0
},
3059 { "i386", BFD_MACH_O_CPU_TYPE_I386
},
3060 { "mips", BFD_MACH_O_CPU_TYPE_MIPS
},
3061 { "mc98000", BFD_MACH_O_CPU_TYPE_MC98000
},
3062 { "hppa", BFD_MACH_O_CPU_TYPE_HPPA
},
3063 { "arm", BFD_MACH_O_CPU_TYPE_ARM
},
3064 { "mc88000", BFD_MACH_O_CPU_TYPE_MC88000
},
3065 { "sparc", BFD_MACH_O_CPU_TYPE_SPARC
},
3066 { "i860", BFD_MACH_O_CPU_TYPE_I860
},
3067 { "alpha", BFD_MACH_O_CPU_TYPE_ALPHA
},
3068 { "powerpc", BFD_MACH_O_CPU_TYPE_POWERPC
},
3069 { "powerpc_64", BFD_MACH_O_CPU_TYPE_POWERPC_64
},
3070 { "x86_64", BFD_MACH_O_CPU_TYPE_X86_64
},
3074 static bfd_mach_o_xlat_name bfd_mach_o_filetype_name
[] =
3076 { "object", BFD_MACH_O_MH_OBJECT
},
3077 { "execute", BFD_MACH_O_MH_EXECUTE
},
3078 { "fvmlib", BFD_MACH_O_MH_FVMLIB
},
3079 { "core", BFD_MACH_O_MH_CORE
},
3080 { "preload", BFD_MACH_O_MH_PRELOAD
},
3081 { "dylib", BFD_MACH_O_MH_DYLIB
},
3082 { "dylinker", BFD_MACH_O_MH_DYLINKER
},
3083 { "bundle", BFD_MACH_O_MH_BUNDLE
},
3087 static bfd_mach_o_xlat_name bfd_mach_o_header_flags_name
[] =
3089 { "noundefs", BFD_MACH_O_MH_NOUNDEFS
},
3090 { "incrlink", BFD_MACH_O_MH_INCRLINK
},
3091 { "dyldlink", BFD_MACH_O_MH_DYLDLINK
},
3092 { "bindatload", BFD_MACH_O_MH_BINDATLOAD
},
3093 { "prebound", BFD_MACH_O_MH_PREBOUND
},
3094 { "split_segs", BFD_MACH_O_MH_SPLIT_SEGS
},
3095 { "lazy_init", BFD_MACH_O_MH_LAZY_INIT
},
3096 { "twolevel", BFD_MACH_O_MH_TWOLEVEL
},
3097 { "force_flat", BFD_MACH_O_MH_FORCE_FLAT
},
3098 { "nomultidefs", BFD_MACH_O_MH_NOMULTIDEFS
},
3099 { "nofixprebinding", BFD_MACH_O_MH_NOFIXPREBINDING
},
3100 { "prebindable", BFD_MACH_O_MH_PREBINDABLE
},
3101 { "allmodsbound", BFD_MACH_O_MH_ALLMODSBOUND
},
3102 { "subsections_via_symbols", BFD_MACH_O_MH_SUBSECTIONS_VIA_SYMBOLS
},
3103 { "canonical", BFD_MACH_O_MH_CANONICAL
},
3104 { "weak_defines", BFD_MACH_O_MH_WEAK_DEFINES
},
3105 { "binds_to_weak", BFD_MACH_O_MH_BINDS_TO_WEAK
},
3106 { "allow_stack_execution", BFD_MACH_O_MH_ALLOW_STACK_EXECUTION
},
3107 { "root_safe", BFD_MACH_O_MH_ROOT_SAFE
},
3108 { "setuid_safe", BFD_MACH_O_MH_SETUID_SAFE
},
3109 { "no_reexported_dylibs", BFD_MACH_O_MH_NO_REEXPORTED_DYLIBS
},
3110 { "pie", BFD_MACH_O_MH_PIE
},
3114 static bfd_mach_o_xlat_name bfd_mach_o_section_type_name
[] =
3116 { "regular", BFD_MACH_O_S_REGULAR
},
3117 { "zerofill", BFD_MACH_O_S_ZEROFILL
},
3118 { "cstring_literals", BFD_MACH_O_S_CSTRING_LITERALS
},
3119 { "4byte_literals", BFD_MACH_O_S_4BYTE_LITERALS
},
3120 { "8byte_literals", BFD_MACH_O_S_8BYTE_LITERALS
},
3121 { "literal_pointers", BFD_MACH_O_S_LITERAL_POINTERS
},
3122 { "non_lazy_symbol_pointers", BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS
},
3123 { "lazy_symbol_pointers", BFD_MACH_O_S_LAZY_SYMBOL_POINTERS
},
3124 { "symbol_stubs", BFD_MACH_O_S_SYMBOL_STUBS
},
3125 { "mod_init_func_pointers", BFD_MACH_O_S_MOD_INIT_FUNC_POINTERS
},
3126 { "mod_fini_func_pointers", BFD_MACH_O_S_MOD_FINI_FUNC_POINTERS
},
3127 { "coalesced", BFD_MACH_O_S_COALESCED
},
3128 { "gb_zerofill", BFD_MACH_O_S_GB_ZEROFILL
},
3129 { "interposing", BFD_MACH_O_S_INTERPOSING
},
3130 { "16byte_literals", BFD_MACH_O_S_16BYTE_LITERALS
},
3131 { "dtrace_dof", BFD_MACH_O_S_DTRACE_DOF
},
3132 { "lazy_dylib_symbol_pointers", BFD_MACH_O_S_LAZY_DYLIB_SYMBOL_POINTERS
},
3136 static bfd_mach_o_xlat_name bfd_mach_o_section_attribute_name
[] =
3138 { "loc_reloc", BFD_MACH_O_S_ATTR_LOC_RELOC
},
3139 { "ext_reloc", BFD_MACH_O_S_ATTR_EXT_RELOC
},
3140 { "some_instructions", BFD_MACH_O_S_ATTR_SOME_INSTRUCTIONS
},
3141 { "debug", BFD_MACH_O_S_ATTR_DEBUG
},
3142 { "modifying_code", BFD_MACH_O_S_SELF_MODIFYING_CODE
},
3143 { "live_support", BFD_MACH_O_S_ATTR_LIVE_SUPPORT
},
3144 { "no_dead_strip", BFD_MACH_O_S_ATTR_NO_DEAD_STRIP
},
3145 { "strip_static_syms", BFD_MACH_O_S_ATTR_STRIP_STATIC_SYMS
},
3146 { "no_toc", BFD_MACH_O_S_ATTR_NO_TOC
},
3147 { "pure_instructions", BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS
},
3151 static bfd_mach_o_xlat_name bfd_mach_o_load_command_name
[] =
3153 { "segment", BFD_MACH_O_LC_SEGMENT
},
3154 { "symtab", BFD_MACH_O_LC_SYMTAB
},
3155 { "symseg", BFD_MACH_O_LC_SYMSEG
},
3156 { "thread", BFD_MACH_O_LC_THREAD
},
3157 { "unixthread", BFD_MACH_O_LC_UNIXTHREAD
},
3158 { "loadfvmlib", BFD_MACH_O_LC_LOADFVMLIB
},
3159 { "idfvmlib", BFD_MACH_O_LC_IDFVMLIB
},
3160 { "ident", BFD_MACH_O_LC_IDENT
},
3161 { "fvmfile", BFD_MACH_O_LC_FVMFILE
},
3162 { "prepage", BFD_MACH_O_LC_PREPAGE
},
3163 { "dysymtab", BFD_MACH_O_LC_DYSYMTAB
},
3164 { "load_dylib", BFD_MACH_O_LC_LOAD_DYLIB
},
3165 { "id_dylib", BFD_MACH_O_LC_ID_DYLIB
},
3166 { "load_dylinker", BFD_MACH_O_LC_LOAD_DYLINKER
},
3167 { "id_dylinker", BFD_MACH_O_LC_ID_DYLINKER
},
3168 { "prebound_dylib", BFD_MACH_O_LC_PREBOUND_DYLIB
},
3169 { "routines", BFD_MACH_O_LC_ROUTINES
},
3170 { "sub_framework", BFD_MACH_O_LC_SUB_FRAMEWORK
},
3171 { "sub_umbrella", BFD_MACH_O_LC_SUB_UMBRELLA
},
3172 { "sub_client", BFD_MACH_O_LC_SUB_CLIENT
},
3173 { "sub_library", BFD_MACH_O_LC_SUB_LIBRARY
},
3174 { "twolevel_hints", BFD_MACH_O_LC_TWOLEVEL_HINTS
},
3175 { "prebind_cksum", BFD_MACH_O_LC_PREBIND_CKSUM
},
3176 { "load_weak_dylib", BFD_MACH_O_LC_LOAD_WEAK_DYLIB
},
3177 { "segment_64", BFD_MACH_O_LC_SEGMENT_64
},
3178 { "routines_64", BFD_MACH_O_LC_ROUTINES_64
},
3179 { "uuid", BFD_MACH_O_LC_UUID
},
3180 { "rpath", BFD_MACH_O_LC_RPATH
},
3181 { "code_signature", BFD_MACH_O_LC_CODE_SIGNATURE
},
3182 { "segment_split_info", BFD_MACH_O_LC_SEGMENT_SPLIT_INFO
},
3183 { "reexport_dylib", BFD_MACH_O_LC_REEXPORT_DYLIB
},
3184 { "lazy_load_dylib", BFD_MACH_O_LC_LAZY_LOAD_DYLIB
},
3185 { "encryption_info", BFD_MACH_O_LC_ENCRYPTION_INFO
},
3190 bfd_mach_o_print_private_header (bfd
*abfd
, FILE *file
)
3192 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
3193 bfd_mach_o_header
*h
= &mdata
->header
;
3195 fprintf (file
, _("Mach-O header:\n"));
3196 fprintf (file
, _(" magic : %08lx\n"), h
->magic
);
3197 fprintf (file
, _(" cputype : %08lx (%s)\n"), h
->cputype
,
3198 bfd_mach_o_get_name (bfd_mach_o_cpu_name
, h
->cputype
));
3199 fprintf (file
, _(" cpusubtype: %08lx\n"), h
->cpusubtype
);
3200 fprintf (file
, _(" filetype : %08lx (%s)\n"),
3202 bfd_mach_o_get_name (bfd_mach_o_filetype_name
, h
->filetype
));
3203 fprintf (file
, _(" ncmds : %08lx\n"), h
->ncmds
);
3204 fprintf (file
, _(" sizeofcmds: %08lx\n"), h
->sizeofcmds
);
3205 fprintf (file
, _(" flags : %08lx ("), h
->flags
);
3206 bfd_mach_o_print_flags (bfd_mach_o_header_flags_name
, h
->flags
, file
);
3207 fprintf (file
, _(")\n"));
3208 fprintf (file
, _(" reserved : %08x\n"), h
->reserved
);
3212 bfd_mach_o_print_section_map (bfd
*abfd
, FILE *file
)
3214 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
3216 unsigned int sec_nbr
= 0;
3218 fprintf (file
, _("Segments and Sections:\n"));
3219 fprintf (file
, _(" #: Segment name Section name Address\n"));
3221 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
3223 bfd_mach_o_segment_command
*seg
;
3225 if (mdata
->commands
[i
].type
!= BFD_MACH_O_LC_SEGMENT
3226 && mdata
->commands
[i
].type
!= BFD_MACH_O_LC_SEGMENT_64
)
3229 seg
= &mdata
->commands
[i
].command
.segment
;
3231 fprintf (file
, "[Segment %-16s ", seg
->segname
);
3232 fprintf_vma (file
, seg
->vmaddr
);
3233 fprintf (file
, "-");
3234 fprintf_vma (file
, seg
->vmaddr
+ seg
->vmsize
- 1);
3236 fputc (seg
->initprot
& BFD_MACH_O_PROT_READ
? 'r' : '-', file
);
3237 fputc (seg
->initprot
& BFD_MACH_O_PROT_WRITE
? 'w' : '-', file
);
3238 fputc (seg
->initprot
& BFD_MACH_O_PROT_EXECUTE
? 'x' : '-', file
);
3239 fprintf (file
, "]\n");
3240 for (j
= 0; j
< seg
->nsects
; j
++)
3242 bfd_mach_o_section
*sec
= &seg
->sections
[j
];
3243 fprintf (file
, "%02u: %-16s %-16s ", ++sec_nbr
,
3244 sec
->segname
, sec
->sectname
);
3245 fprintf_vma (file
, sec
->addr
);
3246 fprintf (file
, " ");
3247 fprintf_vma (file
, sec
->size
);
3248 fprintf (file
, " %08lx\n", sec
->flags
);
3253 /* Return the number of indirect symbols for a section.
3254 Must be called only for symbol pointer section and symbol stubs
3258 bfd_mach_o_section_get_nbr_indirect (bfd
*abfd
, bfd_mach_o_section
*sec
)
3262 switch (sec
->flags
& BFD_MACH_O_SECTION_TYPE_MASK
)
3264 case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS
:
3265 case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS
:
3266 elsz
= bfd_mach_o_wide_p (abfd
) ? 8 : 4;
3267 return sec
->size
/ elsz
;
3268 case BFD_MACH_O_S_SYMBOL_STUBS
:
3269 elsz
= sec
->reserved2
;
3271 return sec
->size
/ elsz
;
3281 bfd_mach_o_print_section (bfd
*abfd ATTRIBUTE_UNUSED
,
3282 bfd_mach_o_section
*sec
, FILE *file
)
3284 fprintf (file
, " Section: %-16s %-16s (bfdname: %s)\n",
3285 sec
->sectname
, sec
->segname
, sec
->bfdsection
->name
);
3286 fprintf (file
, " addr: ");
3287 fprintf_vma (file
, sec
->addr
);
3288 fprintf (file
, " size: ");
3289 fprintf_vma (file
, sec
->size
);
3290 fprintf (file
, " offset: ");
3291 fprintf_vma (file
, sec
->offset
);
3292 fprintf (file
, "\n");
3293 fprintf (file
, " align: %ld", sec
->align
);
3294 fprintf (file
, " nreloc: %lu reloff: ", sec
->nreloc
);
3295 fprintf_vma (file
, sec
->reloff
);
3296 fprintf (file
, "\n");
3297 fprintf (file
, " flags: %08lx (type: %s", sec
->flags
,
3298 bfd_mach_o_get_name (bfd_mach_o_section_type_name
,
3299 sec
->flags
& BFD_MACH_O_SECTION_TYPE_MASK
));
3300 fprintf (file
, " attr: ");
3301 bfd_mach_o_print_flags (bfd_mach_o_section_attribute_name
,
3302 sec
->flags
& BFD_MACH_O_SECTION_ATTRIBUTES_MASK
,
3304 fprintf (file
, ")\n");
3305 switch (sec
->flags
& BFD_MACH_O_SECTION_TYPE_MASK
)
3307 case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS
:
3308 case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS
:
3309 case BFD_MACH_O_S_SYMBOL_STUBS
:
3310 fprintf (file
, " first indirect sym: %lu", sec
->reserved1
);
3311 fprintf (file
, " (%u entries)",
3312 bfd_mach_o_section_get_nbr_indirect (abfd
, sec
));
3315 fprintf (file
, " reserved1: 0x%lx", sec
->reserved1
);
3318 switch (sec
->flags
& BFD_MACH_O_SECTION_TYPE_MASK
)
3320 case BFD_MACH_O_S_SYMBOL_STUBS
:
3321 fprintf (file
, " stub size: %lu", sec
->reserved2
);
3324 fprintf (file
, " reserved2: 0x%lx", sec
->reserved2
);
3327 fprintf (file
, " reserved3: 0x%lx\n", sec
->reserved3
);
3331 bfd_mach_o_print_segment (bfd
*abfd ATTRIBUTE_UNUSED
,
3332 bfd_mach_o_load_command
*cmd
, FILE *file
)
3334 bfd_mach_o_segment_command
*seg
= &cmd
->command
.segment
;
3337 fprintf (file
, " name: %s\n", *seg
->segname
? seg
->segname
: "*none*");
3338 fprintf (file
, " vmaddr: ");
3339 fprintf_vma (file
, seg
->vmaddr
);
3340 fprintf (file
, " vmsize: ");
3341 fprintf_vma (file
, seg
->vmsize
);
3342 fprintf (file
, "\n");
3343 fprintf (file
, " fileoff: ");
3344 fprintf_vma (file
, seg
->fileoff
);
3345 fprintf (file
, " filesize: ");
3346 fprintf_vma (file
, (bfd_vma
)seg
->filesize
);
3347 fprintf (file
, " endoff: ");
3348 fprintf_vma (file
, (bfd_vma
)(seg
->fileoff
+ seg
->filesize
));
3349 fprintf (file
, "\n");
3350 fprintf (file
, " nsects: %lu ", seg
->nsects
);
3351 fprintf (file
, " flags: %lx\n", seg
->flags
);
3352 for (i
= 0; i
< seg
->nsects
; i
++)
3353 bfd_mach_o_print_section (abfd
, &seg
->sections
[i
], file
);
3357 bfd_mach_o_print_dysymtab (bfd
*abfd ATTRIBUTE_UNUSED
,
3358 bfd_mach_o_load_command
*cmd
, FILE *file
)
3360 bfd_mach_o_dysymtab_command
*dysymtab
= &cmd
->command
.dysymtab
;
3361 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
3364 fprintf (file
, " local symbols: idx: %10lu num: %-8lu",
3365 dysymtab
->ilocalsym
, dysymtab
->nlocalsym
);
3366 fprintf (file
, " (nxtidx: %lu)\n",
3367 dysymtab
->ilocalsym
+ dysymtab
->nlocalsym
);
3368 fprintf (file
, " external symbols: idx: %10lu num: %-8lu",
3369 dysymtab
->iextdefsym
, dysymtab
->nextdefsym
);
3370 fprintf (file
, " (nxtidx: %lu)\n",
3371 dysymtab
->iextdefsym
+ dysymtab
->nextdefsym
);
3372 fprintf (file
, " undefined symbols: idx: %10lu num: %-8lu",
3373 dysymtab
->iundefsym
, dysymtab
->nundefsym
);
3374 fprintf (file
, " (nxtidx: %lu)\n",
3375 dysymtab
->iundefsym
+ dysymtab
->nundefsym
);
3376 fprintf (file
, " table of content: off: 0x%08lx num: %-8lu",
3377 dysymtab
->tocoff
, dysymtab
->ntoc
);
3378 fprintf (file
, " (endoff: 0x%08lx)\n",
3380 + dysymtab
->ntoc
* BFD_MACH_O_TABLE_OF_CONTENT_SIZE
);
3381 fprintf (file
, " module table: off: 0x%08lx num: %-8lu",
3382 dysymtab
->modtaboff
, dysymtab
->nmodtab
);
3383 fprintf (file
, " (endoff: 0x%08lx)\n",
3384 dysymtab
->modtaboff
+ dysymtab
->nmodtab
3385 * (mach_o_wide_p (&mdata
->header
) ?
3386 BFD_MACH_O_DYLIB_MODULE_64_SIZE
: BFD_MACH_O_DYLIB_MODULE_SIZE
));
3387 fprintf (file
, " external reference table: off: 0x%08lx num: %-8lu",
3388 dysymtab
->extrefsymoff
, dysymtab
->nextrefsyms
);
3389 fprintf (file
, " (endoff: 0x%08lx)\n",
3390 dysymtab
->extrefsymoff
3391 + dysymtab
->nextrefsyms
* BFD_MACH_O_REFERENCE_SIZE
);
3392 fprintf (file
, " indirect symbol table: off: 0x%08lx num: %-8lu",
3393 dysymtab
->indirectsymoff
, dysymtab
->nindirectsyms
);
3394 fprintf (file
, " (endoff: 0x%08lx)\n",
3395 dysymtab
->indirectsymoff
3396 + dysymtab
->nindirectsyms
* BFD_MACH_O_INDIRECT_SYMBOL_SIZE
);
3397 fprintf (file
, " external relocation table: off: 0x%08lx num: %-8lu",
3398 dysymtab
->extreloff
, dysymtab
->nextrel
);
3399 fprintf (file
, " (endoff: 0x%08lx)\n",
3400 dysymtab
->extreloff
+ dysymtab
->nextrel
* BFD_MACH_O_RELENT_SIZE
);
3401 fprintf (file
, " local relocation table: off: 0x%08lx num: %-8lu",
3402 dysymtab
->locreloff
, dysymtab
->nlocrel
);
3403 fprintf (file
, " (endoff: 0x%08lx)\n",
3404 dysymtab
->locreloff
+ dysymtab
->nlocrel
* BFD_MACH_O_RELENT_SIZE
);
3406 if (dysymtab
->ntoc
> 0
3407 || dysymtab
->nindirectsyms
> 0
3408 || dysymtab
->nextrefsyms
> 0)
3410 /* Try to read the symbols to display the toc or indirect symbols. */
3411 bfd_mach_o_scan_read_symtab_symbols (abfd
);
3413 else if (dysymtab
->nmodtab
> 0)
3415 /* Try to read the strtab to display modules name. */
3416 bfd_mach_o_scan_read_symtab_strtab (abfd
);
3419 for (i
= 0; i
< dysymtab
->nmodtab
; i
++)
3421 bfd_mach_o_dylib_module
*module
= &dysymtab
->dylib_module
[i
];
3422 fprintf (file
, " module %u:\n", i
);
3423 fprintf (file
, " name: %lu", module
->module_name_idx
);
3424 if (mdata
->symtab
&& mdata
->symtab
->strtab
)
3425 fprintf (file
, ": %s",
3426 mdata
->symtab
->strtab
+ module
->module_name_idx
);
3427 fprintf (file
, "\n");
3428 fprintf (file
, " extdefsym: idx: %8lu num: %lu\n",
3429 module
->iextdefsym
, module
->nextdefsym
);
3430 fprintf (file
, " refsym: idx: %8lu num: %lu\n",
3431 module
->irefsym
, module
->nrefsym
);
3432 fprintf (file
, " localsym: idx: %8lu num: %lu\n",
3433 module
->ilocalsym
, module
->nlocalsym
);
3434 fprintf (file
, " extrel: idx: %8lu num: %lu\n",
3435 module
->iextrel
, module
->nextrel
);
3436 fprintf (file
, " init: idx: %8u num: %u\n",
3437 module
->iinit
, module
->ninit
);
3438 fprintf (file
, " term: idx: %8u num: %u\n",
3439 module
->iterm
, module
->nterm
);
3440 fprintf (file
, " objc_module_info: addr: ");
3441 fprintf_vma (file
, module
->objc_module_info_addr
);
3442 fprintf (file
, " size: %lu\n", module
->objc_module_info_size
);
3445 if (dysymtab
->ntoc
> 0)
3447 bfd_mach_o_symtab_command
*symtab
= mdata
->symtab
;
3449 fprintf (file
, " table of content: (symbol/module)\n");
3450 for (i
= 0; i
< dysymtab
->ntoc
; i
++)
3452 bfd_mach_o_dylib_table_of_content
*toc
= &dysymtab
->dylib_toc
[i
];
3454 fprintf (file
, " %4u: ", i
);
3455 if (symtab
&& symtab
->symbols
&& toc
->symbol_index
< symtab
->nsyms
)
3457 const char *name
= symtab
->symbols
[toc
->symbol_index
].symbol
.name
;
3458 fprintf (file
, "%s (%lu)", name
? name
: "*invalid*",
3462 fprintf (file
, "%lu", toc
->symbol_index
);
3464 fprintf (file
, " / ");
3465 if (symtab
&& symtab
->strtab
3466 && toc
->module_index
< dysymtab
->nmodtab
)
3468 bfd_mach_o_dylib_module
*mod
;
3469 mod
= &dysymtab
->dylib_module
[toc
->module_index
];
3470 fprintf (file
, "%s (%lu)",
3471 symtab
->strtab
+ mod
->module_name_idx
,
3475 fprintf (file
, "%lu", toc
->module_index
);
3477 fprintf (file
, "\n");
3481 if (dysymtab
->nindirectsyms
!= 0)
3483 fprintf (file
, " indirect symbols:\n");
3485 for (i
= 0; i
< mdata
->nsects
; i
++)
3487 bfd_mach_o_section
*sec
= mdata
->sections
[i
];
3488 unsigned int j
, first
, last
;
3489 bfd_mach_o_symtab_command
*symtab
= mdata
->symtab
;
3491 switch (sec
->flags
& BFD_MACH_O_SECTION_TYPE_MASK
)
3493 case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS
:
3494 case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS
:
3495 case BFD_MACH_O_S_SYMBOL_STUBS
:
3496 first
= sec
->reserved1
;
3497 last
= first
+ bfd_mach_o_section_get_nbr_indirect (abfd
, sec
);
3498 fprintf (file
, " for section %s.%s:\n",
3499 sec
->segname
, sec
->sectname
);
3500 for (j
= first
; j
< last
; j
++)
3502 unsigned int isym
= dysymtab
->indirect_syms
[j
];
3504 fprintf (file
, " %5u: 0x%08x (%u)", j
, isym
, isym
);
3505 if (isym
& BFD_MACH_O_INDIRECT_SYMBOL_LOCAL
)
3506 fprintf (file
, " LOCAL");
3507 if (isym
& BFD_MACH_O_INDIRECT_SYMBOL_ABS
)
3508 fprintf (file
, " ABSOLUTE");
3509 if (symtab
&& symtab
->symbols
3510 && isym
< symtab
->nsyms
3511 && symtab
->symbols
[isym
].symbol
.name
)
3512 fprintf (file
, " %s", symtab
->symbols
[isym
].symbol
.name
);
3513 fprintf (file
, "\n");
3521 if (dysymtab
->nextrefsyms
> 0)
3523 bfd_mach_o_symtab_command
*symtab
= mdata
->symtab
;
3525 fprintf (file
, " external reference table: (symbol flags)\n");
3526 for (i
= 0; i
< dysymtab
->nextrefsyms
; i
++)
3528 bfd_mach_o_dylib_reference
*ref
= &dysymtab
->ext_refs
[i
];
3530 fprintf (file
, " %4u: %5lu 0x%02lx", i
, ref
->isym
, ref
->flags
);
3531 if (symtab
&& symtab
->symbols
3532 && ref
->isym
< symtab
->nsyms
3533 && symtab
->symbols
[ref
->isym
].symbol
.name
)
3534 fprintf (file
, " %s", symtab
->symbols
[ref
->isym
].symbol
.name
);
3535 fprintf (file
, "\n");
3542 bfd_mach_o_bfd_print_private_bfd_data (bfd
*abfd
, PTR ptr
)
3544 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
3545 FILE *file
= (FILE *) ptr
;
3548 bfd_mach_o_print_private_header (abfd
, file
);
3551 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
3553 bfd_mach_o_load_command
*cmd
= &mdata
->commands
[i
];
3555 fprintf (file
, "Load command %s:",
3556 bfd_mach_o_get_name (bfd_mach_o_load_command_name
, cmd
->type
));
3559 case BFD_MACH_O_LC_SEGMENT
:
3560 case BFD_MACH_O_LC_SEGMENT_64
:
3561 bfd_mach_o_print_segment (abfd
, cmd
, file
);
3563 case BFD_MACH_O_LC_UUID
:
3565 bfd_mach_o_uuid_command
*uuid
= &cmd
->command
.uuid
;
3568 for (i
= 0; i
< sizeof (uuid
->uuid
); i
++)
3569 fprintf (file
, " %02x", uuid
->uuid
[i
]);
3573 case BFD_MACH_O_LC_LOAD_DYLIB
:
3574 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB
:
3575 case BFD_MACH_O_LC_REEXPORT_DYLIB
:
3576 case BFD_MACH_O_LC_ID_DYLIB
:
3578 bfd_mach_o_dylib_command
*dylib
= &cmd
->command
.dylib
;
3579 fprintf (file
, " %s\n", dylib
->name_str
);
3580 fprintf (file
, " time stamp: 0x%08lx\n",
3582 fprintf (file
, " current version: 0x%08lx\n",
3583 dylib
->current_version
);
3584 fprintf (file
, " comptibility version: 0x%08lx\n",
3585 dylib
->compatibility_version
);
3588 case BFD_MACH_O_LC_LOAD_DYLINKER
:
3589 case BFD_MACH_O_LC_ID_DYLINKER
:
3590 fprintf (file
, " %s\n", cmd
->command
.dylinker
.name_str
);
3592 case BFD_MACH_O_LC_SYMTAB
:
3594 bfd_mach_o_symtab_command
*symtab
= &cmd
->command
.symtab
;
3597 " symoff: 0x%08x nsyms: %8u (endoff: 0x%08x)\n",
3598 symtab
->symoff
, symtab
->nsyms
,
3599 symtab
->symoff
+ symtab
->nsyms
3600 * (mach_o_wide_p (&mdata
->header
)
3601 ? BFD_MACH_O_NLIST_64_SIZE
: BFD_MACH_O_NLIST_SIZE
));
3603 " stroff: 0x%08x strsize: %8u (endoff: 0x%08x)\n",
3604 symtab
->stroff
, symtab
->strsize
,
3605 symtab
->stroff
+ symtab
->strsize
);
3608 case BFD_MACH_O_LC_DYSYMTAB
:
3609 fprintf (file
, "\n");
3610 bfd_mach_o_print_dysymtab (abfd
, cmd
, file
);
3612 case BFD_MACH_O_LC_CODE_SIGNATURE
:
3613 case BFD_MACH_O_LC_SEGMENT_SPLIT_INFO
:
3615 bfd_mach_o_linkedit_command
*linkedit
= &cmd
->command
.linkedit
;
3618 " dataoff: 0x%08lx datasize: 0x%08lx (endoff: 0x%08lx)\n",
3619 linkedit
->dataoff
, linkedit
->datasize
,
3620 linkedit
->dataoff
+ linkedit
->datasize
);
3623 case BFD_MACH_O_LC_SUB_FRAMEWORK
:
3624 case BFD_MACH_O_LC_SUB_UMBRELLA
:
3625 case BFD_MACH_O_LC_SUB_LIBRARY
:
3626 case BFD_MACH_O_LC_SUB_CLIENT
:
3628 bfd_mach_o_str_command
*str
= &cmd
->command
.str
;
3629 fprintf (file
, " %s\n", str
->str
);
3632 case BFD_MACH_O_LC_THREAD
:
3633 case BFD_MACH_O_LC_UNIXTHREAD
:
3635 bfd_mach_o_thread_command
*thread
= &cmd
->command
.thread
;
3637 bfd_mach_o_backend_data
*bed
= bfd_mach_o_get_backend_data (abfd
);
3639 fprintf (file
, " nflavours: %lu\n", thread
->nflavours
);
3640 for (j
= 0; j
< thread
->nflavours
; j
++)
3642 bfd_mach_o_thread_flavour
*flavour
= &thread
->flavours
[j
];
3644 fprintf (file
, " %2u: flavour: 0x%08lx offset: 0x%08lx"
3646 j
, flavour
->flavour
, flavour
->offset
,
3648 if (bed
->_bfd_mach_o_print_thread
)
3650 char *buf
= bfd_malloc (flavour
->size
);
3653 && bfd_seek (abfd
, flavour
->offset
, SEEK_SET
) == 0
3654 && (bfd_bread (buf
, flavour
->size
, abfd
)
3656 (*bed
->_bfd_mach_o_print_thread
)(abfd
, flavour
,
3664 fprintf (file
, "\n");
3670 bfd_mach_o_print_section_map (abfd
, file
);
3676 bfd_mach_o_core_fetch_environment (bfd
*abfd
,
3677 unsigned char **rbuf
,
3680 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
3681 unsigned long stackaddr
= bfd_mach_o_stack_addr (mdata
->header
.cputype
);
3684 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
3686 bfd_mach_o_load_command
*cur
= &mdata
->commands
[i
];
3687 bfd_mach_o_segment_command
*seg
= NULL
;
3689 if (cur
->type
!= BFD_MACH_O_LC_SEGMENT
)
3692 seg
= &cur
->command
.segment
;
3694 if ((seg
->vmaddr
+ seg
->vmsize
) == stackaddr
)
3696 unsigned long start
= seg
->fileoff
;
3697 unsigned long end
= seg
->fileoff
+ seg
->filesize
;
3698 unsigned char *buf
= bfd_malloc (1024);
3699 unsigned long size
= 1024;
3703 bfd_size_type nread
= 0;
3704 unsigned long offset
;
3705 int found_nonnull
= 0;
3707 if (size
> (end
- start
))
3708 size
= (end
- start
);
3710 buf
= bfd_realloc_or_free (buf
, size
);
3714 if (bfd_seek (abfd
, end
- size
, SEEK_SET
) != 0)
3720 nread
= bfd_bread (buf
, size
, abfd
);
3728 for (offset
= 4; offset
<= size
; offset
+= 4)
3732 val
= *((unsigned long *) (buf
+ size
- offset
));
3733 if (! found_nonnull
)
3738 else if (val
== 0x0)
3740 unsigned long bottom
;
3743 bottom
= seg
->fileoff
+ seg
->filesize
- offset
;
3744 top
= seg
->fileoff
+ seg
->filesize
- 4;
3745 *rbuf
= bfd_malloc (top
- bottom
);
3746 *rlen
= top
- bottom
;
3748 memcpy (*rbuf
, buf
+ size
- *rlen
, *rlen
);
3754 if (size
== (end
- start
))
3768 bfd_mach_o_core_file_failing_command (bfd
*abfd
)
3770 unsigned char *buf
= NULL
;
3771 unsigned int len
= 0;
3774 ret
= bfd_mach_o_core_fetch_environment (abfd
, &buf
, &len
);
3778 return (char *) buf
;
3782 bfd_mach_o_core_file_failing_signal (bfd
*abfd ATTRIBUTE_UNUSED
)
3787 #define bfd_mach_o_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
3788 #define bfd_mach_o_bfd_reloc_name_lookup _bfd_norelocs_bfd_reloc_name_lookup
3790 #define bfd_mach_o_swap_reloc_in NULL
3791 #define bfd_mach_o_swap_reloc_out NULL
3792 #define bfd_mach_o_print_thread NULL
3794 #define TARGET_NAME mach_o_be_vec
3795 #define TARGET_STRING "mach-o-be"
3796 #define TARGET_BIG_ENDIAN 1
3797 #define TARGET_ARCHIVE 0
3798 #include "mach-o-target.c"
3801 #undef TARGET_STRING
3802 #undef TARGET_BIG_ENDIAN
3803 #undef TARGET_ARCHIVE
3805 #define TARGET_NAME mach_o_le_vec
3806 #define TARGET_STRING "mach-o-le"
3807 #define TARGET_BIG_ENDIAN 0
3808 #define TARGET_ARCHIVE 0
3810 #include "mach-o-target.c"
3813 #undef TARGET_STRING
3814 #undef TARGET_BIG_ENDIAN
3815 #undef TARGET_ARCHIVE
3817 #define TARGET_NAME mach_o_fat_vec
3818 #define TARGET_STRING "mach-o-fat"
3819 #define TARGET_BIG_ENDIAN 1
3820 #define TARGET_ARCHIVE 1
3822 #include "mach-o-target.c"
3825 #undef TARGET_STRING
3826 #undef TARGET_BIG_ENDIAN
3827 #undef TARGET_ARCHIVE