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_mach_o_gen_mkobject
34 #define FILE_ALIGN(off, algn) \
35 (((off) + ((file_ptr) 1 << (algn)) - 1) & ((file_ptr) -1 << (algn)))
37 static int bfd_mach_o_read_symtab_symbols (bfd
*);
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
;
96 static const struct mach_o_section_name_xlat dwarf_section_names_xlat
[] =
98 { ".debug_frame", "__debug_frame", SEC_DEBUGGING
},
99 { ".debug_info", "__debug_info", SEC_DEBUGGING
},
100 { ".debug_abbrev", "__debug_abbrev", SEC_DEBUGGING
},
101 { ".debug_aranges", "__debug_aranges", SEC_DEBUGGING
},
102 { ".debug_macinfo", "__debug_macinfo", SEC_DEBUGGING
},
103 { ".debug_line", "__debug_line", SEC_DEBUGGING
},
104 { ".debug_loc", "__debug_loc", SEC_DEBUGGING
},
105 { ".debug_pubnames", "__debug_pubnames", SEC_DEBUGGING
},
106 { ".debug_pubtypes", "__debug_pubtypes", SEC_DEBUGGING
},
107 { ".debug_str", "__debug_str", SEC_DEBUGGING
},
108 { ".debug_ranges", "__debug_ranges", SEC_DEBUGGING
},
112 static const struct mach_o_section_name_xlat text_section_names_xlat
[] =
114 { ".text", "__text", SEC_CODE
| SEC_LOAD
},
115 { ".const", "__const", SEC_READONLY
| SEC_DATA
| SEC_LOAD
},
116 { ".cstring", "__cstring", SEC_READONLY
| SEC_DATA
| SEC_LOAD
},
117 { ".eh_frame", "__eh_frame", SEC_READONLY
| SEC_LOAD
},
121 static const struct mach_o_section_name_xlat data_section_names_xlat
[] =
123 { ".data", "__data", SEC_DATA
| SEC_LOAD
},
124 { ".const_data", "__const", SEC_DATA
| SEC_LOAD
},
125 { ".dyld", "__dyld", SEC_DATA
| SEC_LOAD
},
126 { ".lazy_symbol_ptr", "__la_symbol_ptr", SEC_DATA
| SEC_LOAD
},
127 { ".non_lazy_symbol_ptr", "__nl_symbol_ptr", SEC_DATA
| SEC_LOAD
},
128 { ".bss", "__bss", SEC_NO_FLAGS
},
132 struct mach_o_segment_name_xlat
135 const struct mach_o_section_name_xlat
*sections
;
138 static const struct mach_o_segment_name_xlat segsec_names_xlat
[] =
140 { "__DWARF", dwarf_section_names_xlat
},
141 { "__TEXT", text_section_names_xlat
},
142 { "__DATA", data_section_names_xlat
},
147 /* Mach-O to bfd names. */
150 bfd_mach_o_convert_section_name_to_bfd (bfd
*abfd
, bfd_mach_o_section
*section
,
151 char **name
, flagword
*flags
)
153 const struct mach_o_segment_name_xlat
*seg
;
156 const char *pfx
= "";
159 *flags
= SEC_NO_FLAGS
;
161 for (seg
= segsec_names_xlat
; seg
->segname
; seg
++)
163 if (strcmp (seg
->segname
, section
->segname
) == 0)
165 const struct mach_o_section_name_xlat
*sec
;
167 for (sec
= seg
->sections
; sec
->mach_o_name
; sec
++)
169 if (strcmp (sec
->mach_o_name
, section
->sectname
) == 0)
171 len
= strlen (sec
->bfd_name
);
172 res
= bfd_alloc (abfd
, len
+ 1);
176 strcpy (res
, sec
->bfd_name
);
185 len
= strlen (section
->segname
) + 1
186 + strlen (section
->sectname
) + 1;
188 /* Put "LC_SEGMENT." prefix if the segment name is weird (ie doesn't start
189 with an underscore. */
190 if (section
->segname
[0] != '_')
192 static const char seg_pfx
[] = "LC_SEGMENT.";
195 len
+= sizeof (seg_pfx
) - 1;
198 res
= bfd_alloc (abfd
, len
);
201 snprintf (res
, len
, "%s%s.%s", pfx
, section
->segname
, section
->sectname
);
205 /* Convert a bfd section name to a Mach-O segment + section name. */
208 bfd_mach_o_convert_section_name_to_mach_o (bfd
*abfd ATTRIBUTE_UNUSED
,
210 bfd_mach_o_section
*section
)
212 const struct mach_o_segment_name_xlat
*seg
;
213 const char *name
= bfd_get_section_name (abfd
, sect
);
219 /* List of well known names. They all start with a dot. */
221 for (seg
= segsec_names_xlat
; seg
->segname
; seg
++)
223 const struct mach_o_section_name_xlat
*sec
;
225 for (sec
= seg
->sections
; sec
->mach_o_name
; sec
++)
227 if (strcmp (sec
->bfd_name
, name
) == 0)
229 strcpy (section
->segname
, seg
->segname
);
230 strcpy (section
->sectname
, sec
->mach_o_name
);
236 /* Strip LC_SEGMENT. prefix. */
237 if (strncmp (name
, "LC_SEGMENT.", 11) == 0)
241 dot
= strchr (name
, '.');
244 /* Try to split name into segment and section names. */
245 if (dot
&& dot
!= name
)
248 seclen
= len
- (dot
+ 1 - name
);
250 if (seglen
< 16 && seclen
< 16)
252 memcpy (section
->segname
, name
, seglen
);
253 section
->segname
[seglen
] = 0;
254 memcpy (section
->sectname
, dot
+ 1, seclen
);
255 section
->sectname
[seclen
] = 0;
262 memcpy (section
->segname
, name
, len
);
263 section
->segname
[len
] = 0;
264 memcpy (section
->sectname
, name
, len
);
265 section
->sectname
[len
] = 0;
268 /* Return the size of an entry for section SEC.
269 Must be called only for symbol pointer section and symbol stubs
273 bfd_mach_o_section_get_entry_size (bfd
*abfd
, bfd_mach_o_section
*sec
)
275 switch (sec
->flags
& BFD_MACH_O_SECTION_TYPE_MASK
)
277 case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS
:
278 case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS
:
279 return bfd_mach_o_wide_p (abfd
) ? 8 : 4;
280 case BFD_MACH_O_S_SYMBOL_STUBS
:
281 return sec
->reserved2
;
288 /* Return the number of indirect symbols for a section.
289 Must be called only for symbol pointer section and symbol stubs
293 bfd_mach_o_section_get_nbr_indirect (bfd
*abfd
, bfd_mach_o_section
*sec
)
297 elsz
= bfd_mach_o_section_get_entry_size (abfd
, sec
);
301 return sec
->size
/ elsz
;
305 /* Copy any private info we understand from the input symbol
306 to the output symbol. */
309 bfd_mach_o_bfd_copy_private_symbol_data (bfd
*ibfd ATTRIBUTE_UNUSED
,
310 asymbol
*isymbol ATTRIBUTE_UNUSED
,
311 bfd
*obfd ATTRIBUTE_UNUSED
,
312 asymbol
*osymbol ATTRIBUTE_UNUSED
)
317 /* Copy any private info we understand from the input section
318 to the output section. */
321 bfd_mach_o_bfd_copy_private_section_data (bfd
*ibfd ATTRIBUTE_UNUSED
,
322 asection
*isection ATTRIBUTE_UNUSED
,
323 bfd
*obfd ATTRIBUTE_UNUSED
,
324 asection
*osection ATTRIBUTE_UNUSED
)
329 /* Copy any private info we understand from the input bfd
330 to the output bfd. */
333 bfd_mach_o_bfd_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
335 if (bfd_get_flavour (ibfd
) != bfd_target_mach_o_flavour
336 || bfd_get_flavour (obfd
) != bfd_target_mach_o_flavour
)
339 BFD_ASSERT (bfd_mach_o_valid (ibfd
));
340 BFD_ASSERT (bfd_mach_o_valid (obfd
));
342 /* FIXME: copy commands. */
347 /* Count the total number of symbols. */
350 bfd_mach_o_count_symbols (bfd
*abfd
)
352 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
354 if (mdata
->symtab
== NULL
)
356 return mdata
->symtab
->nsyms
;
360 bfd_mach_o_get_symtab_upper_bound (bfd
*abfd
)
362 long nsyms
= bfd_mach_o_count_symbols (abfd
);
364 return ((nsyms
+ 1) * sizeof (asymbol
*));
368 bfd_mach_o_canonicalize_symtab (bfd
*abfd
, asymbol
**alocation
)
370 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
371 long nsyms
= bfd_mach_o_count_symbols (abfd
);
372 bfd_mach_o_symtab_command
*sym
= mdata
->symtab
;
378 if (bfd_mach_o_read_symtab_symbols (abfd
) != 0)
381 "bfd_mach_o_canonicalize_symtab: unable to load symbols\n");
385 BFD_ASSERT (sym
->symbols
!= NULL
);
387 for (j
= 0; j
< sym
->nsyms
; j
++)
388 alocation
[j
] = &sym
->symbols
[j
].symbol
;
396 bfd_mach_o_get_synthetic_symtab (bfd
*abfd
,
397 long symcount ATTRIBUTE_UNUSED
,
398 asymbol
**syms ATTRIBUTE_UNUSED
,
399 long dynsymcount ATTRIBUTE_UNUSED
,
400 asymbol
**dynsyms ATTRIBUTE_UNUSED
,
403 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
404 bfd_mach_o_dysymtab_command
*dysymtab
= mdata
->dysymtab
;
405 bfd_mach_o_symtab_command
*symtab
= mdata
->symtab
;
407 unsigned long count
, i
, j
, n
;
414 if (dysymtab
== NULL
|| symtab
== NULL
|| symtab
->symbols
== NULL
)
417 if (dysymtab
->nindirectsyms
== 0)
420 count
= dysymtab
->nindirectsyms
;
421 size
= count
* sizeof (asymbol
) + 1;
423 for (j
= 0; j
< count
; j
++)
425 unsigned int isym
= dysymtab
->indirect_syms
[j
];
427 if (isym
< symtab
->nsyms
&& symtab
->symbols
[isym
].symbol
.name
)
428 size
+= strlen (symtab
->symbols
[isym
].symbol
.name
) + sizeof ("$stub");
431 s
= *ret
= (asymbol
*) bfd_malloc (size
);
434 names
= (char *) (s
+ count
);
439 for (i
= 0; i
< mdata
->nsects
; i
++)
441 bfd_mach_o_section
*sec
= mdata
->sections
[i
];
442 unsigned int first
, last
;
446 switch (sec
->flags
& BFD_MACH_O_SECTION_TYPE_MASK
)
448 case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS
:
449 case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS
:
450 case BFD_MACH_O_S_SYMBOL_STUBS
:
451 first
= sec
->reserved1
;
452 last
= first
+ bfd_mach_o_section_get_nbr_indirect (abfd
, sec
);
454 entry_size
= bfd_mach_o_section_get_entry_size (abfd
, sec
);
455 for (j
= first
; j
< last
; j
++)
457 unsigned int isym
= dysymtab
->indirect_syms
[j
];
459 s
->flags
= BSF_GLOBAL
| BSF_SYNTHETIC
;
460 s
->section
= sec
->bfdsection
;
461 s
->value
= addr
- sec
->addr
;
464 if (isym
< symtab
->nsyms
465 && symtab
->symbols
[isym
].symbol
.name
)
467 const char *sym
= symtab
->symbols
[isym
].symbol
.name
;
472 memcpy (names
, sym
, len
);
474 memcpy (names
, "$stub", sizeof ("$stub"));
475 names
+= sizeof ("$stub");
494 bfd_mach_o_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
498 bfd_symbol_info (symbol
, ret
);
502 bfd_mach_o_print_symbol (bfd
*abfd
,
505 bfd_print_symbol_type how
)
507 FILE *file
= (FILE *) afile
;
509 bfd_mach_o_asymbol
*asym
= (bfd_mach_o_asymbol
*)symbol
;
513 case bfd_print_symbol_name
:
514 fprintf (file
, "%s", symbol
->name
);
517 bfd_print_symbol_vandf (abfd
, (void *) file
, symbol
);
518 if (asym
->n_type
& BFD_MACH_O_N_STAB
)
519 name
= bfd_get_stab_name (asym
->n_type
);
521 switch (asym
->n_type
& BFD_MACH_O_N_TYPE
)
523 case BFD_MACH_O_N_UNDF
:
526 case BFD_MACH_O_N_ABS
:
529 case BFD_MACH_O_N_INDR
:
532 case BFD_MACH_O_N_PBUD
:
535 case BFD_MACH_O_N_SECT
:
544 fprintf (file
, " %02x %-6s %02x %04x",
545 asym
->n_type
, name
, asym
->n_sect
, asym
->n_desc
);
546 if ((asym
->n_type
& BFD_MACH_O_N_STAB
) == 0
547 && (asym
->n_type
& BFD_MACH_O_N_TYPE
) == BFD_MACH_O_N_SECT
)
548 fprintf (file
, " %-5s", symbol
->section
->name
);
549 fprintf (file
, " %s", symbol
->name
);
554 bfd_mach_o_convert_architecture (bfd_mach_o_cpu_type mtype
,
555 bfd_mach_o_cpu_subtype msubtype ATTRIBUTE_UNUSED
,
556 enum bfd_architecture
*type
,
557 unsigned long *subtype
)
559 *subtype
= bfd_arch_unknown
;
563 case BFD_MACH_O_CPU_TYPE_VAX
: *type
= bfd_arch_vax
; break;
564 case BFD_MACH_O_CPU_TYPE_MC680x0
: *type
= bfd_arch_m68k
; break;
565 case BFD_MACH_O_CPU_TYPE_I386
:
566 *type
= bfd_arch_i386
;
567 *subtype
= bfd_mach_i386_i386
;
569 case BFD_MACH_O_CPU_TYPE_X86_64
:
570 *type
= bfd_arch_i386
;
571 *subtype
= bfd_mach_x86_64
;
573 case BFD_MACH_O_CPU_TYPE_MIPS
: *type
= bfd_arch_mips
; break;
574 case BFD_MACH_O_CPU_TYPE_MC98000
: *type
= bfd_arch_m98k
; break;
575 case BFD_MACH_O_CPU_TYPE_HPPA
: *type
= bfd_arch_hppa
; break;
576 case BFD_MACH_O_CPU_TYPE_ARM
: *type
= bfd_arch_arm
; break;
577 case BFD_MACH_O_CPU_TYPE_MC88000
: *type
= bfd_arch_m88k
; break;
578 case BFD_MACH_O_CPU_TYPE_SPARC
:
579 *type
= bfd_arch_sparc
;
580 *subtype
= bfd_mach_sparc
;
582 case BFD_MACH_O_CPU_TYPE_I860
: *type
= bfd_arch_i860
; break;
583 case BFD_MACH_O_CPU_TYPE_ALPHA
: *type
= bfd_arch_alpha
; break;
584 case BFD_MACH_O_CPU_TYPE_POWERPC
:
585 *type
= bfd_arch_powerpc
;
586 *subtype
= bfd_mach_ppc
;
588 case BFD_MACH_O_CPU_TYPE_POWERPC_64
:
589 *type
= bfd_arch_powerpc
;
590 *subtype
= bfd_mach_ppc64
;
593 *type
= bfd_arch_unknown
;
599 bfd_mach_o_write_header (bfd
*abfd
, bfd_mach_o_header
*header
)
601 unsigned char buf
[32];
604 size
= mach_o_wide_p (header
) ?
605 BFD_MACH_O_HEADER_64_SIZE
: BFD_MACH_O_HEADER_SIZE
;
607 bfd_h_put_32 (abfd
, header
->magic
, buf
+ 0);
608 bfd_h_put_32 (abfd
, header
->cputype
, buf
+ 4);
609 bfd_h_put_32 (abfd
, header
->cpusubtype
, buf
+ 8);
610 bfd_h_put_32 (abfd
, header
->filetype
, buf
+ 12);
611 bfd_h_put_32 (abfd
, header
->ncmds
, buf
+ 16);
612 bfd_h_put_32 (abfd
, header
->sizeofcmds
, buf
+ 20);
613 bfd_h_put_32 (abfd
, header
->flags
, buf
+ 24);
615 if (mach_o_wide_p (header
))
616 bfd_h_put_32 (abfd
, header
->reserved
, buf
+ 28);
618 if (bfd_seek (abfd
, 0, SEEK_SET
) != 0
619 || bfd_bwrite ((void *) buf
, size
, abfd
) != size
)
626 bfd_mach_o_write_thread (bfd
*abfd
, bfd_mach_o_load_command
*command
)
628 bfd_mach_o_thread_command
*cmd
= &command
->command
.thread
;
630 unsigned char buf
[8];
632 unsigned int nflavours
;
634 BFD_ASSERT ((command
->type
== BFD_MACH_O_LC_THREAD
)
635 || (command
->type
== BFD_MACH_O_LC_UNIXTHREAD
));
639 for (i
= 0; i
< cmd
->nflavours
; i
++)
641 BFD_ASSERT ((cmd
->flavours
[i
].size
% 4) == 0);
642 BFD_ASSERT (cmd
->flavours
[i
].offset
== (command
->offset
+ offset
+ 8));
644 bfd_h_put_32 (abfd
, cmd
->flavours
[i
].flavour
, buf
);
645 bfd_h_put_32 (abfd
, (cmd
->flavours
[i
].size
/ 4), buf
+ 4);
647 if (bfd_seek (abfd
, command
->offset
+ offset
, SEEK_SET
) != 0
648 || bfd_bwrite ((void *) buf
, 8, abfd
) != 8)
651 offset
+= cmd
->flavours
[i
].size
+ 8;
658 bfd_mach_o_get_reloc_upper_bound (bfd
*abfd ATTRIBUTE_UNUSED
,
661 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
665 bfd_mach_o_canonicalize_one_reloc (bfd
*abfd
, char *buf
,
666 arelent
*res
, asymbol
**syms
)
668 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
669 bfd_mach_o_backend_data
*bed
= bfd_mach_o_get_backend_data (abfd
);
670 bfd_mach_o_reloc_info reloc
;
675 addr
= bfd_get_32 (abfd
, buf
+ 0);
676 symnum
= bfd_get_32 (abfd
, buf
+ 4);
678 if (addr
& BFD_MACH_O_SR_SCATTERED
)
682 /* Scattered relocation.
683 Extract section and offset from r_value. */
684 res
->sym_ptr_ptr
= NULL
;
686 for (j
= 0; j
< mdata
->nsects
; j
++)
688 bfd_mach_o_section
*sect
= mdata
->sections
[j
];
689 if (symnum
>= sect
->addr
&& symnum
< sect
->addr
+ sect
->size
)
691 res
->sym_ptr_ptr
= sect
->bfdsection
->symbol_ptr_ptr
;
692 res
->addend
= symnum
- sect
->addr
;
696 res
->address
= BFD_MACH_O_GET_SR_ADDRESS (addr
);
697 reloc
.r_type
= BFD_MACH_O_GET_SR_TYPE (addr
);
698 reloc
.r_length
= BFD_MACH_O_GET_SR_LENGTH (addr
);
699 reloc
.r_pcrel
= addr
& BFD_MACH_O_SR_PCREL
;
700 reloc
.r_scattered
= 1;
704 unsigned int num
= BFD_MACH_O_GET_R_SYMBOLNUM (symnum
);
707 if (symnum
& BFD_MACH_O_R_EXTERN
)
711 BFD_ASSERT (num
!= 0);
712 BFD_ASSERT (num
<= mdata
->nsects
);
713 sym
= mdata
->sections
[num
- 1]->bfdsection
->symbol_ptr_ptr
;
715 res
->sym_ptr_ptr
= sym
;
716 reloc
.r_type
= BFD_MACH_O_GET_R_TYPE (symnum
);
717 reloc
.r_length
= BFD_MACH_O_GET_R_LENGTH (symnum
);
718 reloc
.r_pcrel
= (symnum
& BFD_MACH_O_R_PCREL
) ? 1 : 0;
719 reloc
.r_scattered
= 0;
722 if (!(*bed
->_bfd_mach_o_swap_reloc_in
)(res
, &reloc
))
728 bfd_mach_o_canonicalize_relocs (bfd
*abfd
, unsigned long filepos
,
730 arelent
*res
, asymbol
**syms
)
734 bfd_size_type native_size
;
736 /* Allocate and read relocs. */
737 native_size
= count
* BFD_MACH_O_RELENT_SIZE
;
738 native_relocs
= bfd_malloc (native_size
);
739 if (native_relocs
== NULL
)
742 if (bfd_seek (abfd
, filepos
, SEEK_SET
) != 0
743 || bfd_bread (native_relocs
, native_size
, abfd
) != native_size
)
746 for (i
= 0; i
< count
; i
++)
748 char *buf
= native_relocs
+ BFD_MACH_O_RELENT_SIZE
* i
;
750 if (bfd_mach_o_canonicalize_one_reloc (abfd
, buf
, &res
[i
], syms
) < 0)
753 free (native_relocs
);
756 free (native_relocs
);
761 bfd_mach_o_canonicalize_reloc (bfd
*abfd
, asection
*asect
,
762 arelent
**rels
, asymbol
**syms
)
764 bfd_mach_o_backend_data
*bed
= bfd_mach_o_get_backend_data (abfd
);
768 if (asect
->reloc_count
== 0)
771 /* No need to go further if we don't know how to read relocs. */
772 if (bed
->_bfd_mach_o_swap_reloc_in
== NULL
)
775 res
= bfd_malloc (asect
->reloc_count
* sizeof (arelent
));
779 if (bfd_mach_o_canonicalize_relocs (abfd
, asect
->rel_filepos
,
780 asect
->reloc_count
, res
, syms
) < 0)
786 for (i
= 0; i
< asect
->reloc_count
; i
++)
789 asect
->relocation
= res
;
795 bfd_mach_o_get_dynamic_reloc_upper_bound (bfd
*abfd
)
797 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
799 if (mdata
->dysymtab
== NULL
)
801 return (mdata
->dysymtab
->nextrel
+ mdata
->dysymtab
->nlocrel
)
802 * sizeof (arelent
*);
806 bfd_mach_o_canonicalize_dynamic_reloc (bfd
*abfd
, arelent
**rels
,
807 struct bfd_symbol
**syms
)
809 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
810 bfd_mach_o_dysymtab_command
*dysymtab
= mdata
->dysymtab
;
811 bfd_mach_o_backend_data
*bed
= bfd_mach_o_get_backend_data (abfd
);
815 if (dysymtab
== NULL
)
817 if (dysymtab
->nextrel
== 0 && dysymtab
->nlocrel
== 0)
820 /* No need to go further if we don't know how to read relocs. */
821 if (bed
->_bfd_mach_o_swap_reloc_in
== NULL
)
824 res
= bfd_malloc ((dysymtab
->nextrel
+ dysymtab
->nlocrel
) * sizeof (arelent
));
828 if (bfd_mach_o_canonicalize_relocs (abfd
, dysymtab
->extreloff
,
829 dysymtab
->nextrel
, res
, syms
) < 0)
835 if (bfd_mach_o_canonicalize_relocs (abfd
, dysymtab
->locreloff
,
837 res
+ dysymtab
->nextrel
, syms
) < 0)
843 for (i
= 0; i
< dysymtab
->nextrel
+ dysymtab
->nlocrel
; i
++)
850 bfd_mach_o_write_relocs (bfd
*abfd
, bfd_mach_o_section
*section
)
852 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
856 bfd_mach_o_backend_data
*bed
= bfd_mach_o_get_backend_data (abfd
);
858 sec
= section
->bfdsection
;
859 if (sec
->reloc_count
== 0)
862 if (bed
->_bfd_mach_o_swap_reloc_out
== NULL
)
865 /* Allocate relocation room. */
866 mdata
->filelen
= FILE_ALIGN(mdata
->filelen
, 2);
867 section
->nreloc
= sec
->reloc_count
;
868 sec
->rel_filepos
= mdata
->filelen
;
869 section
->reloff
= sec
->rel_filepos
;
870 mdata
->filelen
+= sec
->reloc_count
* BFD_MACH_O_RELENT_SIZE
;
872 if (bfd_seek (abfd
, section
->reloff
, SEEK_SET
) != 0)
875 /* Convert and write. */
876 entries
= section
->bfdsection
->orelocation
;
877 for (i
= 0; i
< section
->nreloc
; i
++)
879 arelent
*rel
= entries
[i
];
881 bfd_mach_o_reloc_info info
, *pinfo
= &info
;
883 /* Convert relocation to an intermediate representation. */
884 if (!(*bed
->_bfd_mach_o_swap_reloc_out
) (rel
, pinfo
))
887 /* Lower the relocation info. */
888 if (pinfo
->r_scattered
)
892 v
= BFD_MACH_O_SR_SCATTERED
893 | (pinfo
->r_pcrel
? BFD_MACH_O_SR_PCREL
: 0)
894 | BFD_MACH_O_SET_SR_LENGTH(pinfo
->r_length
)
895 | BFD_MACH_O_SET_SR_TYPE(pinfo
->r_type
)
896 | BFD_MACH_O_SET_SR_ADDRESS(pinfo
->r_address
);
897 bfd_put_32 (abfd
, v
, buf
);
898 bfd_put_32 (abfd
, pinfo
->r_value
, buf
+ 4);
904 bfd_put_32 (abfd
, pinfo
->r_address
, buf
);
905 v
= BFD_MACH_O_SET_R_SYMBOLNUM (pinfo
->r_value
)
906 | (pinfo
->r_pcrel
? BFD_MACH_O_R_PCREL
: 0)
907 | BFD_MACH_O_SET_R_LENGTH (pinfo
->r_length
)
908 | (pinfo
->r_extern
? BFD_MACH_O_R_EXTERN
: 0)
909 | BFD_MACH_O_SET_R_TYPE (pinfo
->r_type
);
910 bfd_put_32 (abfd
, v
, buf
+ 4);
913 if (bfd_bwrite ((void *) buf
, BFD_MACH_O_RELENT_SIZE
, abfd
)
914 != BFD_MACH_O_RELENT_SIZE
)
921 bfd_mach_o_write_section_32 (bfd
*abfd
, bfd_mach_o_section
*section
)
923 unsigned char buf
[BFD_MACH_O_SECTION_SIZE
];
925 memcpy (buf
, section
->sectname
, 16);
926 memcpy (buf
+ 16, section
->segname
, 16);
927 bfd_h_put_32 (abfd
, section
->addr
, buf
+ 32);
928 bfd_h_put_32 (abfd
, section
->size
, buf
+ 36);
929 bfd_h_put_32 (abfd
, section
->offset
, buf
+ 40);
930 bfd_h_put_32 (abfd
, section
->align
, buf
+ 44);
931 bfd_h_put_32 (abfd
, section
->reloff
, buf
+ 48);
932 bfd_h_put_32 (abfd
, section
->nreloc
, buf
+ 52);
933 bfd_h_put_32 (abfd
, section
->flags
, buf
+ 56);
934 bfd_h_put_32 (abfd
, section
->reserved1
, buf
+ 60);
935 bfd_h_put_32 (abfd
, section
->reserved2
, buf
+ 64);
937 if (bfd_bwrite ((void *) buf
, BFD_MACH_O_SECTION_SIZE
, abfd
)
938 != BFD_MACH_O_SECTION_SIZE
)
945 bfd_mach_o_write_section_64 (bfd
*abfd
, bfd_mach_o_section
*section
)
947 unsigned char buf
[BFD_MACH_O_SECTION_64_SIZE
];
949 memcpy (buf
, section
->sectname
, 16);
950 memcpy (buf
+ 16, section
->segname
, 16);
951 bfd_h_put_64 (abfd
, section
->addr
, buf
+ 32);
952 bfd_h_put_64 (abfd
, section
->size
, buf
+ 40);
953 bfd_h_put_32 (abfd
, section
->offset
, buf
+ 48);
954 bfd_h_put_32 (abfd
, section
->align
, buf
+ 52);
955 bfd_h_put_32 (abfd
, section
->reloff
, buf
+ 56);
956 bfd_h_put_32 (abfd
, section
->nreloc
, buf
+ 60);
957 bfd_h_put_32 (abfd
, section
->flags
, buf
+ 64);
958 bfd_h_put_32 (abfd
, section
->reserved1
, buf
+ 68);
959 bfd_h_put_32 (abfd
, section
->reserved2
, buf
+ 72);
960 bfd_h_put_32 (abfd
, section
->reserved3
, buf
+ 76);
962 if (bfd_bwrite ((void *) buf
, BFD_MACH_O_SECTION_64_SIZE
, abfd
)
963 != BFD_MACH_O_SECTION_64_SIZE
)
970 bfd_mach_o_write_segment_32 (bfd
*abfd
, bfd_mach_o_load_command
*command
)
972 unsigned char buf
[BFD_MACH_O_LC_SEGMENT_SIZE
];
973 bfd_mach_o_segment_command
*seg
= &command
->command
.segment
;
976 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SEGMENT
);
978 for (i
= 0; i
< seg
->nsects
; i
++)
979 if (!bfd_mach_o_write_relocs (abfd
, &seg
->sections
[i
]))
982 memcpy (buf
, seg
->segname
, 16);
983 bfd_h_put_32 (abfd
, seg
->vmaddr
, buf
+ 16);
984 bfd_h_put_32 (abfd
, seg
->vmsize
, buf
+ 20);
985 bfd_h_put_32 (abfd
, seg
->fileoff
, buf
+ 24);
986 bfd_h_put_32 (abfd
, seg
->filesize
, buf
+ 28);
987 bfd_h_put_32 (abfd
, seg
->maxprot
, buf
+ 32);
988 bfd_h_put_32 (abfd
, seg
->initprot
, buf
+ 36);
989 bfd_h_put_32 (abfd
, seg
->nsects
, buf
+ 40);
990 bfd_h_put_32 (abfd
, seg
->flags
, buf
+ 44);
992 if (bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
) != 0
993 || (bfd_bwrite ((void *) buf
, BFD_MACH_O_LC_SEGMENT_SIZE
- 8, abfd
)
994 != BFD_MACH_O_LC_SEGMENT_SIZE
- 8))
997 for (i
= 0; i
< seg
->nsects
; i
++)
998 if (bfd_mach_o_write_section_32 (abfd
, &seg
->sections
[i
]))
1005 bfd_mach_o_write_segment_64 (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1007 unsigned char buf
[BFD_MACH_O_LC_SEGMENT_64_SIZE
];
1008 bfd_mach_o_segment_command
*seg
= &command
->command
.segment
;
1011 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SEGMENT_64
);
1013 for (i
= 0; i
< seg
->nsects
; i
++)
1014 if (!bfd_mach_o_write_relocs (abfd
, &seg
->sections
[i
]))
1017 memcpy (buf
, seg
->segname
, 16);
1018 bfd_h_put_64 (abfd
, seg
->vmaddr
, buf
+ 16);
1019 bfd_h_put_64 (abfd
, seg
->vmsize
, buf
+ 24);
1020 bfd_h_put_64 (abfd
, seg
->fileoff
, buf
+ 32);
1021 bfd_h_put_64 (abfd
, seg
->filesize
, buf
+ 40);
1022 bfd_h_put_32 (abfd
, seg
->maxprot
, buf
+ 48);
1023 bfd_h_put_32 (abfd
, seg
->initprot
, buf
+ 52);
1024 bfd_h_put_32 (abfd
, seg
->nsects
, buf
+ 56);
1025 bfd_h_put_32 (abfd
, seg
->flags
, buf
+ 60);
1027 if (bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
) != 0
1028 || (bfd_bwrite ((void *) buf
, BFD_MACH_O_LC_SEGMENT_64_SIZE
- 8, abfd
)
1029 != BFD_MACH_O_LC_SEGMENT_64_SIZE
- 8))
1032 for (i
= 0; i
< seg
->nsects
; i
++)
1033 if (bfd_mach_o_write_section_64 (abfd
, &seg
->sections
[i
]))
1040 bfd_mach_o_write_symtab (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1042 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
1043 bfd_mach_o_symtab_command
*sym
= &command
->command
.symtab
;
1044 unsigned char buf
[16];
1046 unsigned int wide
= bfd_mach_o_wide_p (abfd
);
1047 unsigned int symlen
= wide
? BFD_MACH_O_NLIST_64_SIZE
: BFD_MACH_O_NLIST_SIZE
;
1048 struct bfd_strtab_hash
*strtab
;
1049 asymbol
**symbols
= bfd_get_outsymbols (abfd
);
1051 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SYMTAB
);
1053 /* Write the symbols first. */
1054 mdata
->filelen
= FILE_ALIGN(mdata
->filelen
, wide
? 3 : 2);
1055 sym
->symoff
= mdata
->filelen
;
1056 if (bfd_seek (abfd
, sym
->symoff
, SEEK_SET
) != 0)
1059 sym
->nsyms
= bfd_get_symcount (abfd
);
1060 mdata
->filelen
+= sym
->nsyms
* symlen
;
1062 strtab
= _bfd_stringtab_init ();
1066 for (i
= 0; i
< sym
->nsyms
; i
++)
1068 bfd_size_type str_index
;
1069 bfd_mach_o_asymbol
*s
= (bfd_mach_o_asymbol
*)symbols
[i
];
1071 /* Compute name index. */
1072 /* An index of 0 always means the empty string. */
1073 if (s
->symbol
.name
== 0 || s
->symbol
.name
[0] == '\0')
1077 str_index
= _bfd_stringtab_add (strtab
, s
->symbol
.name
, TRUE
, FALSE
);
1078 if (str_index
== (bfd_size_type
) -1)
1081 bfd_h_put_32 (abfd
, str_index
, buf
);
1082 bfd_h_put_8 (abfd
, s
->n_type
, buf
+ 4);
1083 bfd_h_put_8 (abfd
, s
->n_sect
, buf
+ 5);
1084 bfd_h_put_16 (abfd
, s
->n_desc
, buf
+ 6);
1086 bfd_h_put_64 (abfd
, s
->symbol
.section
->vma
+ s
->symbol
.value
, buf
+ 8);
1088 bfd_h_put_32 (abfd
, s
->symbol
.section
->vma
+ s
->symbol
.value
, buf
+ 8);
1090 if (bfd_bwrite ((void *) buf
, symlen
, abfd
) != symlen
)
1093 sym
->strsize
= _bfd_stringtab_size (strtab
);
1094 sym
->stroff
= mdata
->filelen
;
1095 mdata
->filelen
+= sym
->strsize
;
1097 if (_bfd_stringtab_emit (abfd
, strtab
) != TRUE
)
1099 _bfd_stringtab_free (strtab
);
1102 bfd_h_put_32 (abfd
, sym
->symoff
, buf
);
1103 bfd_h_put_32 (abfd
, sym
->nsyms
, buf
+ 4);
1104 bfd_h_put_32 (abfd
, sym
->stroff
, buf
+ 8);
1105 bfd_h_put_32 (abfd
, sym
->strsize
, buf
+ 12);
1107 if (bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
) != 0
1108 || bfd_bwrite ((void *) buf
, 16, abfd
) != 16)
1114 _bfd_stringtab_free (strtab
);
1118 /* Process the symbols and generate Mach-O specific fields.
1122 bfd_mach_o_mangle_symbols (bfd
*abfd
)
1125 asymbol
**symbols
= bfd_get_outsymbols (abfd
);
1127 for (i
= 0; i
< bfd_get_symcount (abfd
); i
++)
1129 bfd_mach_o_asymbol
*s
= (bfd_mach_o_asymbol
*)symbols
[i
];
1131 if (s
->n_type
== BFD_MACH_O_N_UNDF
&& !(s
->symbol
.flags
& BSF_DEBUGGING
))
1133 /* As genuine Mach-O symbols type shouldn't be N_UNDF (undefined
1134 symbols should be N_UNDEF | N_EXT), we suppose the back-end
1135 values haven't been set. */
1136 if (s
->symbol
.section
== bfd_abs_section_ptr
)
1137 s
->n_type
= BFD_MACH_O_N_ABS
;
1138 else if (s
->symbol
.section
== bfd_und_section_ptr
)
1140 s
->n_type
= BFD_MACH_O_N_UNDF
;
1141 if (s
->symbol
.flags
& BSF_WEAK
)
1142 s
->n_desc
|= BFD_MACH_O_N_WEAK_REF
;
1144 else if (s
->symbol
.section
== bfd_com_section_ptr
)
1145 s
->n_type
= BFD_MACH_O_N_UNDF
| BFD_MACH_O_N_EXT
;
1147 s
->n_type
= BFD_MACH_O_N_SECT
;
1149 if (s
->symbol
.flags
& BSF_GLOBAL
)
1150 s
->n_type
|= BFD_MACH_O_N_EXT
;
1153 /* Compute section index. */
1154 if (s
->symbol
.section
!= bfd_abs_section_ptr
1155 && s
->symbol
.section
!= bfd_und_section_ptr
1156 && s
->symbol
.section
!= bfd_com_section_ptr
)
1157 s
->n_sect
= s
->symbol
.section
->target_index
;
1159 /* Number symbols. */
1160 s
->symbol
.udata
.i
= i
;
1166 bfd_mach_o_write_contents (bfd
*abfd
)
1169 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
1171 if (mdata
->header
.ncmds
== 0)
1172 if (!bfd_mach_o_build_commands (abfd
))
1175 /* Now write header information. */
1176 if (mdata
->header
.filetype
== 0)
1178 if (abfd
->flags
& EXEC_P
)
1179 mdata
->header
.filetype
= BFD_MACH_O_MH_EXECUTE
;
1180 else if (abfd
->flags
& DYNAMIC
)
1181 mdata
->header
.filetype
= BFD_MACH_O_MH_DYLIB
;
1183 mdata
->header
.filetype
= BFD_MACH_O_MH_OBJECT
;
1185 if (!bfd_mach_o_write_header (abfd
, &mdata
->header
))
1188 /* Assign a number to each symbols. */
1189 if (!bfd_mach_o_mangle_symbols (abfd
))
1192 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
1194 unsigned char buf
[8];
1195 bfd_mach_o_load_command
*cur
= &mdata
->commands
[i
];
1196 unsigned long typeflag
;
1198 typeflag
= cur
->type
| (cur
->type_required
? BFD_MACH_O_LC_REQ_DYLD
: 0);
1200 bfd_h_put_32 (abfd
, typeflag
, buf
);
1201 bfd_h_put_32 (abfd
, cur
->len
, buf
+ 4);
1203 if (bfd_seek (abfd
, cur
->offset
, SEEK_SET
) != 0
1204 || bfd_bwrite ((void *) buf
, 8, abfd
) != 8)
1209 case BFD_MACH_O_LC_SEGMENT
:
1210 if (bfd_mach_o_write_segment_32 (abfd
, cur
) != 0)
1213 case BFD_MACH_O_LC_SEGMENT_64
:
1214 if (bfd_mach_o_write_segment_64 (abfd
, cur
) != 0)
1217 case BFD_MACH_O_LC_SYMTAB
:
1218 if (!bfd_mach_o_write_symtab (abfd
, cur
))
1221 case BFD_MACH_O_LC_SYMSEG
:
1223 case BFD_MACH_O_LC_THREAD
:
1224 case BFD_MACH_O_LC_UNIXTHREAD
:
1225 if (bfd_mach_o_write_thread (abfd
, cur
) != 0)
1228 case BFD_MACH_O_LC_LOADFVMLIB
:
1229 case BFD_MACH_O_LC_IDFVMLIB
:
1230 case BFD_MACH_O_LC_IDENT
:
1231 case BFD_MACH_O_LC_FVMFILE
:
1232 case BFD_MACH_O_LC_PREPAGE
:
1233 case BFD_MACH_O_LC_DYSYMTAB
:
1234 case BFD_MACH_O_LC_LOAD_DYLIB
:
1235 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB
:
1236 case BFD_MACH_O_LC_ID_DYLIB
:
1237 case BFD_MACH_O_LC_REEXPORT_DYLIB
:
1238 case BFD_MACH_O_LC_LOAD_DYLINKER
:
1239 case BFD_MACH_O_LC_ID_DYLINKER
:
1240 case BFD_MACH_O_LC_PREBOUND_DYLIB
:
1241 case BFD_MACH_O_LC_ROUTINES
:
1242 case BFD_MACH_O_LC_SUB_FRAMEWORK
:
1246 "unable to write unknown load command 0x%lx\n",
1247 (unsigned long) cur
->type
);
1255 /* Build Mach-O load commands from the sections. */
1258 bfd_mach_o_build_commands (bfd
*abfd
)
1260 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
1261 unsigned int wide
= mach_o_wide_p (&mdata
->header
);
1262 bfd_mach_o_segment_command
*seg
;
1263 bfd_mach_o_section
*sections
;
1265 bfd_mach_o_load_command
*cmd
;
1266 bfd_mach_o_load_command
*symtab_cmd
;
1269 /* Return now if commands are already built. */
1270 if (mdata
->header
.ncmds
)
1273 /* Very simple version: 1 command (segment) containing all sections. */
1274 mdata
->header
.ncmds
= 2;
1275 mdata
->commands
= bfd_alloc (abfd
, mdata
->header
.ncmds
1276 * sizeof (bfd_mach_o_load_command
));
1277 if (mdata
->commands
== NULL
)
1279 cmd
= &mdata
->commands
[0];
1280 seg
= &cmd
->command
.segment
;
1282 seg
->nsects
= bfd_count_sections (abfd
);
1283 sections
= bfd_alloc (abfd
, seg
->nsects
* sizeof (bfd_mach_o_section
));
1284 if (sections
== NULL
)
1286 seg
->sections
= sections
;
1288 /* Set segment command. */
1291 cmd
->type
= BFD_MACH_O_LC_SEGMENT_64
;
1292 cmd
->offset
= BFD_MACH_O_HEADER_64_SIZE
;
1293 cmd
->len
= BFD_MACH_O_LC_SEGMENT_64_SIZE
1294 + BFD_MACH_O_SECTION_64_SIZE
* seg
->nsects
;
1298 cmd
->type
= BFD_MACH_O_LC_SEGMENT
;
1299 cmd
->offset
= BFD_MACH_O_HEADER_SIZE
;
1300 cmd
->len
= BFD_MACH_O_LC_SEGMENT_SIZE
1301 + BFD_MACH_O_SECTION_SIZE
* seg
->nsects
;
1303 cmd
->type_required
= FALSE
;
1304 mdata
->header
.sizeofcmds
= cmd
->len
;
1305 mdata
->filelen
= cmd
->offset
+ cmd
->len
;
1307 /* Set symtab command. */
1308 symtab_cmd
= &mdata
->commands
[1];
1310 symtab_cmd
->type
= BFD_MACH_O_LC_SYMTAB
;
1311 symtab_cmd
->offset
= cmd
->offset
+ cmd
->len
;
1312 symtab_cmd
->len
= 6 * 4;
1313 symtab_cmd
->type_required
= FALSE
;
1315 mdata
->header
.sizeofcmds
+= symtab_cmd
->len
;
1316 mdata
->filelen
+= symtab_cmd
->len
;
1318 /* Fill segment command. */
1319 memset (seg
->segname
, 0, sizeof (seg
->segname
));
1321 seg
->fileoff
= mdata
->filelen
;
1323 seg
->maxprot
= BFD_MACH_O_PROT_READ
| BFD_MACH_O_PROT_WRITE
1324 | BFD_MACH_O_PROT_EXECUTE
;
1325 seg
->initprot
= seg
->maxprot
;
1328 /* Create Mach-O sections. */
1330 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1332 sections
->bfdsection
= sec
;
1333 bfd_mach_o_convert_section_name_to_mach_o (abfd
, sec
, sections
);
1334 sections
->addr
= bfd_get_section_vma (abfd
, sec
);
1335 sections
->size
= bfd_get_section_size (sec
);
1336 sections
->align
= bfd_get_section_alignment (abfd
, sec
);
1338 if (sections
->size
!= 0)
1340 mdata
->filelen
= FILE_ALIGN (mdata
->filelen
, sections
->align
);
1341 sections
->offset
= mdata
->filelen
;
1344 sections
->offset
= 0;
1345 sections
->reloff
= 0;
1346 sections
->nreloc
= 0;
1347 sections
->reserved1
= 0;
1348 sections
->reserved2
= 0;
1349 sections
->reserved3
= 0;
1351 sec
->filepos
= sections
->offset
;
1352 sec
->target_index
= ++target_index
;
1354 mdata
->filelen
+= sections
->size
;
1357 seg
->filesize
= mdata
->filelen
- seg
->fileoff
;
1358 seg
->vmsize
= seg
->filesize
;
1363 /* Set the contents of a section. */
1366 bfd_mach_o_set_section_contents (bfd
*abfd
,
1368 const void * location
,
1370 bfd_size_type count
)
1374 /* This must be done first, because bfd_set_section_contents is
1375 going to set output_has_begun to TRUE. */
1376 if (! abfd
->output_has_begun
&& ! bfd_mach_o_build_commands (abfd
))
1382 pos
= section
->filepos
+ offset
;
1383 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
1384 || bfd_bwrite (location
, count
, abfd
) != count
)
1391 bfd_mach_o_sizeof_headers (bfd
*a ATTRIBUTE_UNUSED
,
1392 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
1397 /* Make an empty symbol. This is required only because
1398 bfd_make_section_anyway wants to create a symbol for the section. */
1401 bfd_mach_o_make_empty_symbol (bfd
*abfd
)
1403 asymbol
*new_symbol
;
1405 new_symbol
= bfd_zalloc (abfd
, sizeof (bfd_mach_o_asymbol
));
1406 if (new_symbol
== NULL
)
1408 new_symbol
->the_bfd
= abfd
;
1409 new_symbol
->udata
.i
= 0;
1414 bfd_mach_o_read_header (bfd
*abfd
, bfd_mach_o_header
*header
)
1416 unsigned char buf
[32];
1418 bfd_vma (*get32
) (const void *) = NULL
;
1420 /* Just read the magic number. */
1421 if (bfd_seek (abfd
, 0, SEEK_SET
) != 0
1422 || bfd_bread ((void *) buf
, 4, abfd
) != 4)
1425 if (bfd_getb32 (buf
) == BFD_MACH_O_MH_MAGIC
)
1427 header
->byteorder
= BFD_ENDIAN_BIG
;
1428 header
->magic
= BFD_MACH_O_MH_MAGIC
;
1429 header
->version
= 1;
1432 else if (bfd_getl32 (buf
) == BFD_MACH_O_MH_MAGIC
)
1434 header
->byteorder
= BFD_ENDIAN_LITTLE
;
1435 header
->magic
= BFD_MACH_O_MH_MAGIC
;
1436 header
->version
= 1;
1439 else if (bfd_getb32 (buf
) == BFD_MACH_O_MH_MAGIC_64
)
1441 header
->byteorder
= BFD_ENDIAN_BIG
;
1442 header
->magic
= BFD_MACH_O_MH_MAGIC_64
;
1443 header
->version
= 2;
1446 else if (bfd_getl32 (buf
) == BFD_MACH_O_MH_MAGIC_64
)
1448 header
->byteorder
= BFD_ENDIAN_LITTLE
;
1449 header
->magic
= BFD_MACH_O_MH_MAGIC_64
;
1450 header
->version
= 2;
1455 header
->byteorder
= BFD_ENDIAN_UNKNOWN
;
1459 /* Once the size of the header is known, read the full header. */
1460 size
= mach_o_wide_p (header
) ?
1461 BFD_MACH_O_HEADER_64_SIZE
: BFD_MACH_O_HEADER_SIZE
;
1463 if (bfd_seek (abfd
, 0, SEEK_SET
) != 0
1464 || bfd_bread ((void *) buf
, size
, abfd
) != size
)
1467 header
->cputype
= (*get32
) (buf
+ 4);
1468 header
->cpusubtype
= (*get32
) (buf
+ 8);
1469 header
->filetype
= (*get32
) (buf
+ 12);
1470 header
->ncmds
= (*get32
) (buf
+ 16);
1471 header
->sizeofcmds
= (*get32
) (buf
+ 20);
1472 header
->flags
= (*get32
) (buf
+ 24);
1474 if (mach_o_wide_p (header
))
1475 header
->reserved
= (*get32
) (buf
+ 28);
1481 bfd_mach_o_make_bfd_section (bfd
*abfd
, bfd_mach_o_section
*section
,
1488 bfd_mach_o_convert_section_name_to_bfd (abfd
, section
, &sname
, &flags
);
1492 if (flags
== SEC_NO_FLAGS
)
1494 /* Try to guess flags. */
1495 if (section
->flags
& BFD_MACH_O_S_ATTR_DEBUG
)
1496 flags
= SEC_DEBUGGING
;
1500 if ((section
->flags
& BFD_MACH_O_SECTION_TYPE_MASK
)
1501 != BFD_MACH_O_S_ZEROFILL
)
1504 if (prot
& BFD_MACH_O_PROT_EXECUTE
)
1506 if (prot
& BFD_MACH_O_PROT_WRITE
)
1508 else if (prot
& BFD_MACH_O_PROT_READ
)
1509 flags
|= SEC_READONLY
;
1515 if ((flags
& SEC_DEBUGGING
) == 0)
1519 if (section
->offset
!= 0)
1520 flags
|= SEC_HAS_CONTENTS
;
1521 if (section
->nreloc
!= 0)
1524 bfdsec
= bfd_make_section_anyway_with_flags (abfd
, sname
, flags
);
1528 bfdsec
->vma
= section
->addr
;
1529 bfdsec
->lma
= section
->addr
;
1530 bfdsec
->size
= section
->size
;
1531 bfdsec
->filepos
= section
->offset
;
1532 bfdsec
->alignment_power
= section
->align
;
1533 bfdsec
->segment_mark
= 0;
1534 bfdsec
->reloc_count
= section
->nreloc
;
1535 bfdsec
->rel_filepos
= section
->reloff
;
1541 bfd_mach_o_read_section_32 (bfd
*abfd
,
1542 bfd_mach_o_section
*section
,
1543 unsigned int offset
,
1546 unsigned char buf
[BFD_MACH_O_SECTION_SIZE
];
1548 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0
1549 || (bfd_bread ((void *) buf
, BFD_MACH_O_SECTION_SIZE
, abfd
)
1550 != BFD_MACH_O_SECTION_SIZE
))
1553 memcpy (section
->sectname
, buf
, 16);
1554 section
->sectname
[16] = '\0';
1555 memcpy (section
->segname
, buf
+ 16, 16);
1556 section
->segname
[16] = '\0';
1557 section
->addr
= bfd_h_get_32 (abfd
, buf
+ 32);
1558 section
->size
= bfd_h_get_32 (abfd
, buf
+ 36);
1559 section
->offset
= bfd_h_get_32 (abfd
, buf
+ 40);
1560 section
->align
= bfd_h_get_32 (abfd
, buf
+ 44);
1561 section
->reloff
= bfd_h_get_32 (abfd
, buf
+ 48);
1562 section
->nreloc
= bfd_h_get_32 (abfd
, buf
+ 52);
1563 section
->flags
= bfd_h_get_32 (abfd
, buf
+ 56);
1564 section
->reserved1
= bfd_h_get_32 (abfd
, buf
+ 60);
1565 section
->reserved2
= bfd_h_get_32 (abfd
, buf
+ 64);
1566 section
->reserved3
= 0;
1567 section
->bfdsection
= bfd_mach_o_make_bfd_section (abfd
, section
, prot
);
1569 if (section
->bfdsection
== NULL
)
1576 bfd_mach_o_read_section_64 (bfd
*abfd
,
1577 bfd_mach_o_section
*section
,
1578 unsigned int offset
,
1581 unsigned char buf
[BFD_MACH_O_SECTION_64_SIZE
];
1583 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0
1584 || (bfd_bread ((void *) buf
, BFD_MACH_O_SECTION_64_SIZE
, abfd
)
1585 != BFD_MACH_O_SECTION_64_SIZE
))
1588 memcpy (section
->sectname
, buf
, 16);
1589 section
->sectname
[16] = '\0';
1590 memcpy (section
->segname
, buf
+ 16, 16);
1591 section
->segname
[16] = '\0';
1592 section
->addr
= bfd_h_get_64 (abfd
, buf
+ 32);
1593 section
->size
= bfd_h_get_64 (abfd
, buf
+ 40);
1594 section
->offset
= bfd_h_get_32 (abfd
, buf
+ 48);
1595 section
->align
= bfd_h_get_32 (abfd
, buf
+ 52);
1596 section
->reloff
= bfd_h_get_32 (abfd
, buf
+ 56);
1597 section
->nreloc
= bfd_h_get_32 (abfd
, buf
+ 60);
1598 section
->flags
= bfd_h_get_32 (abfd
, buf
+ 64);
1599 section
->reserved1
= bfd_h_get_32 (abfd
, buf
+ 68);
1600 section
->reserved2
= bfd_h_get_32 (abfd
, buf
+ 72);
1601 section
->reserved3
= bfd_h_get_32 (abfd
, buf
+ 76);
1602 section
->bfdsection
= bfd_mach_o_make_bfd_section (abfd
, section
, prot
);
1604 if (section
->bfdsection
== NULL
)
1611 bfd_mach_o_read_section (bfd
*abfd
,
1612 bfd_mach_o_section
*section
,
1613 unsigned int offset
,
1618 return bfd_mach_o_read_section_64 (abfd
, section
, offset
, prot
);
1620 return bfd_mach_o_read_section_32 (abfd
, section
, offset
, prot
);
1624 bfd_mach_o_read_symtab_symbol (bfd
*abfd
,
1625 bfd_mach_o_symtab_command
*sym
,
1626 bfd_mach_o_asymbol
*s
,
1629 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
1630 unsigned int wide
= mach_o_wide_p (&mdata
->header
);
1631 unsigned int symwidth
=
1632 wide
? BFD_MACH_O_NLIST_64_SIZE
: BFD_MACH_O_NLIST_SIZE
;
1633 unsigned int symoff
= sym
->symoff
+ (i
* symwidth
);
1634 unsigned char buf
[16];
1635 unsigned char type
= -1;
1636 unsigned char section
= -1;
1638 symvalue value
= -1;
1639 unsigned long stroff
= -1;
1640 unsigned int symtype
= -1;
1642 BFD_ASSERT (sym
->strtab
!= NULL
);
1644 if (bfd_seek (abfd
, symoff
, SEEK_SET
) != 0
1645 || bfd_bread ((void *) buf
, symwidth
, abfd
) != symwidth
)
1647 fprintf (stderr
, "bfd_mach_o_read_symtab_symbol: unable to read %d bytes at %lu\n",
1648 symwidth
, (unsigned long) symoff
);
1652 stroff
= bfd_h_get_32 (abfd
, buf
);
1653 type
= bfd_h_get_8 (abfd
, buf
+ 4);
1654 symtype
= type
& BFD_MACH_O_N_TYPE
;
1655 section
= bfd_h_get_8 (abfd
, buf
+ 5);
1656 desc
= bfd_h_get_16 (abfd
, buf
+ 6);
1658 value
= bfd_h_get_64 (abfd
, buf
+ 8);
1660 value
= bfd_h_get_32 (abfd
, buf
+ 8);
1662 if (stroff
>= sym
->strsize
)
1664 fprintf (stderr
, "bfd_mach_o_read_symtab_symbol: symbol name out of range (%lu >= %lu)\n",
1665 (unsigned long) stroff
, (unsigned long) sym
->strsize
);
1669 s
->symbol
.the_bfd
= abfd
;
1670 s
->symbol
.name
= sym
->strtab
+ stroff
;
1671 s
->symbol
.value
= value
;
1672 s
->symbol
.flags
= 0x0;
1673 s
->symbol
.udata
.i
= 0;
1675 s
->n_sect
= section
;
1678 if (type
& BFD_MACH_O_N_STAB
)
1680 s
->symbol
.flags
|= BSF_DEBUGGING
;
1681 s
->symbol
.section
= bfd_und_section_ptr
;
1693 if ((section
> 0) && (section
<= mdata
->nsects
))
1695 s
->symbol
.section
= mdata
->sections
[section
- 1]->bfdsection
;
1697 s
->symbol
.value
- mdata
->sections
[section
- 1]->addr
;
1704 if (type
& BFD_MACH_O_N_PEXT
)
1705 s
->symbol
.flags
|= BSF_GLOBAL
;
1707 if (type
& BFD_MACH_O_N_EXT
)
1708 s
->symbol
.flags
|= BSF_GLOBAL
;
1710 if (!(type
& (BFD_MACH_O_N_PEXT
| BFD_MACH_O_N_EXT
)))
1711 s
->symbol
.flags
|= BSF_LOCAL
;
1715 case BFD_MACH_O_N_UNDF
:
1716 if (type
== (BFD_MACH_O_N_UNDF
| BFD_MACH_O_N_EXT
)
1717 && s
->symbol
.value
!= 0)
1719 /* A common symbol. */
1720 s
->symbol
.section
= bfd_com_section_ptr
;
1721 s
->symbol
.flags
= BSF_NO_FLAGS
;
1725 s
->symbol
.section
= bfd_und_section_ptr
;
1726 if (s
->n_desc
& BFD_MACH_O_N_WEAK_REF
)
1727 s
->symbol
.flags
|= BSF_WEAK
;
1730 case BFD_MACH_O_N_PBUD
:
1731 s
->symbol
.section
= bfd_und_section_ptr
;
1733 case BFD_MACH_O_N_ABS
:
1734 s
->symbol
.section
= bfd_abs_section_ptr
;
1736 case BFD_MACH_O_N_SECT
:
1737 if ((section
> 0) && (section
<= mdata
->nsects
))
1739 s
->symbol
.section
= mdata
->sections
[section
- 1]->bfdsection
;
1741 s
->symbol
.value
- mdata
->sections
[section
- 1]->addr
;
1745 /* Mach-O uses 0 to mean "no section"; not an error. */
1748 fprintf (stderr
, "bfd_mach_o_read_symtab_symbol: "
1749 "symbol \"%s\" specified invalid section %d (max %lu): setting to undefined\n",
1750 s
->symbol
.name
, section
, mdata
->nsects
);
1752 s
->symbol
.section
= bfd_und_section_ptr
;
1755 case BFD_MACH_O_N_INDR
:
1756 fprintf (stderr
, "bfd_mach_o_read_symtab_symbol: "
1757 "symbol \"%s\" is unsupported 'indirect' reference: setting to undefined\n",
1759 s
->symbol
.section
= bfd_und_section_ptr
;
1762 fprintf (stderr
, "bfd_mach_o_read_symtab_symbol: "
1763 "symbol \"%s\" specified invalid type field 0x%x: setting to undefined\n",
1764 s
->symbol
.name
, symtype
);
1765 s
->symbol
.section
= bfd_und_section_ptr
;
1774 bfd_mach_o_read_symtab_strtab (bfd
*abfd
)
1776 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
1777 bfd_mach_o_symtab_command
*sym
= mdata
->symtab
;
1779 /* Fail if there is no symtab. */
1783 /* Success if already loaded. */
1787 if (abfd
->flags
& BFD_IN_MEMORY
)
1789 struct bfd_in_memory
*b
;
1791 b
= (struct bfd_in_memory
*) abfd
->iostream
;
1793 if ((sym
->stroff
+ sym
->strsize
) > b
->size
)
1795 bfd_set_error (bfd_error_file_truncated
);
1798 sym
->strtab
= (char *) b
->buffer
+ sym
->stroff
;
1802 sym
->strtab
= bfd_alloc (abfd
, sym
->strsize
);
1803 if (sym
->strtab
== NULL
)
1806 if (bfd_seek (abfd
, sym
->stroff
, SEEK_SET
) != 0
1807 || bfd_bread ((void *) sym
->strtab
, sym
->strsize
, abfd
) != sym
->strsize
)
1809 bfd_set_error (bfd_error_file_truncated
);
1818 bfd_mach_o_read_symtab_symbols (bfd
*abfd
)
1820 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
1821 bfd_mach_o_symtab_command
*sym
= mdata
->symtab
;
1828 sym
->symbols
= bfd_alloc (abfd
, sym
->nsyms
* sizeof (bfd_mach_o_asymbol
));
1830 if (sym
->symbols
== NULL
)
1832 fprintf (stderr
, "bfd_mach_o_read_symtab_symbols: unable to allocate memory for symbols\n");
1836 ret
= bfd_mach_o_read_symtab_strtab (abfd
);
1840 for (i
= 0; i
< sym
->nsyms
; i
++)
1842 ret
= bfd_mach_o_read_symtab_symbol (abfd
, sym
, &sym
->symbols
[i
], i
);
1851 bfd_mach_o_read_dysymtab_symbol (bfd
*abfd
,
1852 bfd_mach_o_dysymtab_command
*dysym
,
1853 bfd_mach_o_symtab_command
*sym
,
1854 bfd_mach_o_asymbol
*s
,
1857 unsigned long isymoff
= dysym
->indirectsymoff
+ (i
* 4);
1858 unsigned long sym_index
;
1859 unsigned char buf
[4];
1861 BFD_ASSERT (i
< dysym
->nindirectsyms
);
1863 if (bfd_seek (abfd
, isymoff
, SEEK_SET
) != 0
1864 || bfd_bread ((void *) buf
, 4, abfd
) != 4)
1866 fprintf (stderr
, "bfd_mach_o_read_dysymtab_symbol: unable to read %lu bytes at %lu\n",
1867 (unsigned long) 4, isymoff
);
1870 sym_index
= bfd_h_get_32 (abfd
, buf
);
1872 return bfd_mach_o_read_symtab_symbol (abfd
, sym
, s
, sym_index
);
1876 bfd_mach_o_i386_flavour_string (unsigned int flavour
)
1878 switch ((int) flavour
)
1880 case BFD_MACH_O_x86_THREAD_STATE32
: return "x86_THREAD_STATE32";
1881 case BFD_MACH_O_x86_FLOAT_STATE32
: return "x86_FLOAT_STATE32";
1882 case BFD_MACH_O_x86_EXCEPTION_STATE32
: return "x86_EXCEPTION_STATE32";
1883 case BFD_MACH_O_x86_THREAD_STATE64
: return "x86_THREAD_STATE64";
1884 case BFD_MACH_O_x86_FLOAT_STATE64
: return "x86_FLOAT_STATE64";
1885 case BFD_MACH_O_x86_EXCEPTION_STATE64
: return "x86_EXCEPTION_STATE64";
1886 case BFD_MACH_O_x86_THREAD_STATE
: return "x86_THREAD_STATE";
1887 case BFD_MACH_O_x86_FLOAT_STATE
: return "x86_FLOAT_STATE";
1888 case BFD_MACH_O_x86_EXCEPTION_STATE
: return "x86_EXCEPTION_STATE";
1889 case BFD_MACH_O_x86_DEBUG_STATE32
: return "x86_DEBUG_STATE32";
1890 case BFD_MACH_O_x86_DEBUG_STATE64
: return "x86_DEBUG_STATE64";
1891 case BFD_MACH_O_x86_DEBUG_STATE
: return "x86_DEBUG_STATE";
1892 case BFD_MACH_O_x86_THREAD_STATE_NONE
: return "x86_THREAD_STATE_NONE";
1893 default: return "UNKNOWN";
1898 bfd_mach_o_ppc_flavour_string (unsigned int flavour
)
1900 switch ((int) flavour
)
1902 case BFD_MACH_O_PPC_THREAD_STATE
: return "PPC_THREAD_STATE";
1903 case BFD_MACH_O_PPC_FLOAT_STATE
: return "PPC_FLOAT_STATE";
1904 case BFD_MACH_O_PPC_EXCEPTION_STATE
: return "PPC_EXCEPTION_STATE";
1905 case BFD_MACH_O_PPC_VECTOR_STATE
: return "PPC_VECTOR_STATE";
1906 case BFD_MACH_O_PPC_THREAD_STATE64
: return "PPC_THREAD_STATE64";
1907 case BFD_MACH_O_PPC_EXCEPTION_STATE64
: return "PPC_EXCEPTION_STATE64";
1908 default: return "UNKNOWN";
1913 bfd_mach_o_read_dylinker (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1915 bfd_mach_o_dylinker_command
*cmd
= &command
->command
.dylinker
;
1916 unsigned char buf
[4];
1917 unsigned int nameoff
;
1919 BFD_ASSERT ((command
->type
== BFD_MACH_O_LC_ID_DYLINKER
)
1920 || (command
->type
== BFD_MACH_O_LC_LOAD_DYLINKER
));
1922 if (bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
) != 0
1923 || bfd_bread ((void *) buf
, 4, abfd
) != 4)
1926 nameoff
= bfd_h_get_32 (abfd
, buf
+ 0);
1928 cmd
->name_offset
= command
->offset
+ nameoff
;
1929 cmd
->name_len
= command
->len
- nameoff
;
1930 cmd
->name_str
= bfd_alloc (abfd
, cmd
->name_len
);
1931 if (cmd
->name_str
== NULL
)
1933 if (bfd_seek (abfd
, cmd
->name_offset
, SEEK_SET
) != 0
1934 || bfd_bread (cmd
->name_str
, cmd
->name_len
, abfd
) != cmd
->name_len
)
1940 bfd_mach_o_read_dylib (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1942 bfd_mach_o_dylib_command
*cmd
= &command
->command
.dylib
;
1943 unsigned char buf
[16];
1944 unsigned int nameoff
;
1946 switch (command
->type
)
1948 case BFD_MACH_O_LC_LOAD_DYLIB
:
1949 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB
:
1950 case BFD_MACH_O_LC_ID_DYLIB
:
1951 case BFD_MACH_O_LC_REEXPORT_DYLIB
:
1958 if (bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
) != 0
1959 || bfd_bread ((void *) buf
, 16, abfd
) != 16)
1962 nameoff
= bfd_h_get_32 (abfd
, buf
+ 0);
1963 cmd
->timestamp
= bfd_h_get_32 (abfd
, buf
+ 4);
1964 cmd
->current_version
= bfd_h_get_32 (abfd
, buf
+ 8);
1965 cmd
->compatibility_version
= bfd_h_get_32 (abfd
, buf
+ 12);
1967 cmd
->name_offset
= command
->offset
+ nameoff
;
1968 cmd
->name_len
= command
->len
- nameoff
;
1969 cmd
->name_str
= bfd_alloc (abfd
, cmd
->name_len
);
1970 if (cmd
->name_str
== NULL
)
1972 if (bfd_seek (abfd
, cmd
->name_offset
, SEEK_SET
) != 0
1973 || bfd_bread (cmd
->name_str
, cmd
->name_len
, abfd
) != cmd
->name_len
)
1979 bfd_mach_o_read_prebound_dylib (bfd
*abfd ATTRIBUTE_UNUSED
,
1980 bfd_mach_o_load_command
*command ATTRIBUTE_UNUSED
)
1982 /* bfd_mach_o_prebound_dylib_command *cmd = &command->command.prebound_dylib; */
1984 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_PREBOUND_DYLIB
);
1989 bfd_mach_o_read_thread (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1991 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
1992 bfd_mach_o_thread_command
*cmd
= &command
->command
.thread
;
1993 unsigned char buf
[8];
1994 unsigned int offset
;
1995 unsigned int nflavours
;
1998 BFD_ASSERT ((command
->type
== BFD_MACH_O_LC_THREAD
)
1999 || (command
->type
== BFD_MACH_O_LC_UNIXTHREAD
));
2001 /* Count the number of threads. */
2004 while (offset
!= command
->len
)
2006 if (offset
>= command
->len
)
2009 if (bfd_seek (abfd
, command
->offset
+ offset
, SEEK_SET
) != 0
2010 || bfd_bread ((void *) buf
, 8, abfd
) != 8)
2013 offset
+= 8 + bfd_h_get_32 (abfd
, buf
+ 4) * 4;
2017 /* Allocate threads. */
2018 cmd
->flavours
= bfd_alloc
2019 (abfd
, nflavours
* sizeof (bfd_mach_o_thread_flavour
));
2020 if (cmd
->flavours
== NULL
)
2022 cmd
->nflavours
= nflavours
;
2026 while (offset
!= command
->len
)
2028 if (offset
>= command
->len
)
2031 if (nflavours
>= cmd
->nflavours
)
2034 if (bfd_seek (abfd
, command
->offset
+ offset
, SEEK_SET
) != 0
2035 || bfd_bread ((void *) buf
, 8, abfd
) != 8)
2038 cmd
->flavours
[nflavours
].flavour
= bfd_h_get_32 (abfd
, buf
);
2039 cmd
->flavours
[nflavours
].offset
= command
->offset
+ offset
+ 8;
2040 cmd
->flavours
[nflavours
].size
= bfd_h_get_32 (abfd
, buf
+ 4) * 4;
2041 offset
+= cmd
->flavours
[nflavours
].size
+ 8;
2045 for (i
= 0; i
< nflavours
; i
++)
2048 unsigned int snamelen
;
2050 const char *flavourstr
;
2051 const char *prefix
= "LC_THREAD";
2054 switch (mdata
->header
.cputype
)
2056 case BFD_MACH_O_CPU_TYPE_POWERPC
:
2057 case BFD_MACH_O_CPU_TYPE_POWERPC_64
:
2058 flavourstr
= bfd_mach_o_ppc_flavour_string (cmd
->flavours
[i
].flavour
);
2060 case BFD_MACH_O_CPU_TYPE_I386
:
2061 case BFD_MACH_O_CPU_TYPE_X86_64
:
2062 flavourstr
= bfd_mach_o_i386_flavour_string (cmd
->flavours
[i
].flavour
);
2065 flavourstr
= "UNKNOWN_ARCHITECTURE";
2069 snamelen
= strlen (prefix
) + 1 + 20 + 1 + strlen (flavourstr
) + 1;
2070 sname
= bfd_alloc (abfd
, snamelen
);
2076 sprintf (sname
, "%s.%s.%u", prefix
, flavourstr
, j
);
2077 if (bfd_get_section_by_name (abfd
, sname
) == NULL
)
2082 bfdsec
= bfd_make_section_with_flags (abfd
, sname
, SEC_HAS_CONTENTS
);
2086 bfdsec
->size
= cmd
->flavours
[i
].size
;
2087 bfdsec
->filepos
= cmd
->flavours
[i
].offset
;
2088 bfdsec
->alignment_power
= 0x0;
2090 cmd
->section
= bfdsec
;
2097 bfd_mach_o_read_dysymtab (bfd
*abfd
, bfd_mach_o_load_command
*command
)
2099 bfd_mach_o_dysymtab_command
*cmd
= &command
->command
.dysymtab
;
2100 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
2101 unsigned char buf
[72];
2103 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_DYSYMTAB
);
2105 if (bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
) != 0
2106 || bfd_bread ((void *) buf
, 72, abfd
) != 72)
2109 cmd
->ilocalsym
= bfd_h_get_32 (abfd
, buf
+ 0);
2110 cmd
->nlocalsym
= bfd_h_get_32 (abfd
, buf
+ 4);
2111 cmd
->iextdefsym
= bfd_h_get_32 (abfd
, buf
+ 8);
2112 cmd
->nextdefsym
= bfd_h_get_32 (abfd
, buf
+ 12);
2113 cmd
->iundefsym
= bfd_h_get_32 (abfd
, buf
+ 16);
2114 cmd
->nundefsym
= bfd_h_get_32 (abfd
, buf
+ 20);
2115 cmd
->tocoff
= bfd_h_get_32 (abfd
, buf
+ 24);
2116 cmd
->ntoc
= bfd_h_get_32 (abfd
, buf
+ 28);
2117 cmd
->modtaboff
= bfd_h_get_32 (abfd
, buf
+ 32);
2118 cmd
->nmodtab
= bfd_h_get_32 (abfd
, buf
+ 36);
2119 cmd
->extrefsymoff
= bfd_h_get_32 (abfd
, buf
+ 40);
2120 cmd
->nextrefsyms
= bfd_h_get_32 (abfd
, buf
+ 44);
2121 cmd
->indirectsymoff
= bfd_h_get_32 (abfd
, buf
+ 48);
2122 cmd
->nindirectsyms
= bfd_h_get_32 (abfd
, buf
+ 52);
2123 cmd
->extreloff
= bfd_h_get_32 (abfd
, buf
+ 56);
2124 cmd
->nextrel
= bfd_h_get_32 (abfd
, buf
+ 60);
2125 cmd
->locreloff
= bfd_h_get_32 (abfd
, buf
+ 64);
2126 cmd
->nlocrel
= bfd_h_get_32 (abfd
, buf
+ 68);
2128 if (cmd
->nmodtab
!= 0)
2131 int wide
= bfd_mach_o_wide_p (abfd
);
2132 unsigned int module_len
= wide
? 56 : 52;
2135 bfd_alloc (abfd
, cmd
->nmodtab
* sizeof (bfd_mach_o_dylib_module
));
2136 if (cmd
->dylib_module
== NULL
)
2139 if (bfd_seek (abfd
, cmd
->modtaboff
, SEEK_SET
) != 0)
2142 for (i
= 0; i
< cmd
->nmodtab
; i
++)
2144 bfd_mach_o_dylib_module
*module
= &cmd
->dylib_module
[i
];
2147 if (bfd_bread ((void *) buf
, module_len
, abfd
) != module_len
)
2150 module
->module_name_idx
= bfd_h_get_32 (abfd
, buf
+ 0);
2151 module
->iextdefsym
= bfd_h_get_32 (abfd
, buf
+ 4);
2152 module
->nextdefsym
= bfd_h_get_32 (abfd
, buf
+ 8);
2153 module
->irefsym
= bfd_h_get_32 (abfd
, buf
+ 12);
2154 module
->nrefsym
= bfd_h_get_32 (abfd
, buf
+ 16);
2155 module
->ilocalsym
= bfd_h_get_32 (abfd
, buf
+ 20);
2156 module
->nlocalsym
= bfd_h_get_32 (abfd
, buf
+ 24);
2157 module
->iextrel
= bfd_h_get_32 (abfd
, buf
+ 28);
2158 module
->nextrel
= bfd_h_get_32 (abfd
, buf
+ 32);
2159 v
= bfd_h_get_32 (abfd
, buf
+36);
2160 module
->iinit
= v
& 0xffff;
2161 module
->iterm
= (v
>> 16) & 0xffff;
2162 v
= bfd_h_get_32 (abfd
, buf
+ 40);
2163 module
->ninit
= v
& 0xffff;
2164 module
->nterm
= (v
>> 16) & 0xffff;
2167 module
->objc_module_info_size
= bfd_h_get_32 (abfd
, buf
+ 44);
2168 module
->objc_module_info_addr
= bfd_h_get_64 (abfd
, buf
+ 48);
2172 module
->objc_module_info_addr
= bfd_h_get_32 (abfd
, buf
+ 44);
2173 module
->objc_module_info_size
= bfd_h_get_32 (abfd
, buf
+ 48);
2182 cmd
->dylib_toc
= bfd_alloc
2183 (abfd
, cmd
->ntoc
* sizeof (bfd_mach_o_dylib_table_of_content
));
2184 if (cmd
->dylib_toc
== NULL
)
2187 if (bfd_seek (abfd
, cmd
->tocoff
, SEEK_SET
) != 0)
2190 for (i
= 0; i
< cmd
->ntoc
; i
++)
2192 bfd_mach_o_dylib_table_of_content
*toc
= &cmd
->dylib_toc
[i
];
2194 if (bfd_bread ((void *) buf
, 8, abfd
) != 8)
2197 toc
->symbol_index
= bfd_h_get_32 (abfd
, buf
+ 0);
2198 toc
->module_index
= bfd_h_get_32 (abfd
, buf
+ 4);
2202 if (cmd
->nindirectsyms
!= 0)
2206 cmd
->indirect_syms
= bfd_alloc
2207 (abfd
, cmd
->nindirectsyms
* sizeof (unsigned int));
2208 if (cmd
->indirect_syms
== NULL
)
2211 if (bfd_seek (abfd
, cmd
->indirectsymoff
, SEEK_SET
) != 0)
2214 for (i
= 0; i
< cmd
->nindirectsyms
; i
++)
2216 unsigned int *is
= &cmd
->indirect_syms
[i
];
2218 if (bfd_bread ((void *) buf
, 4, abfd
) != 4)
2221 *is
= bfd_h_get_32 (abfd
, buf
+ 0);
2225 if (cmd
->nextrefsyms
!= 0)
2230 cmd
->ext_refs
= bfd_alloc
2231 (abfd
, cmd
->nextrefsyms
* sizeof (bfd_mach_o_dylib_reference
));
2232 if (cmd
->ext_refs
== NULL
)
2235 if (bfd_seek (abfd
, cmd
->extrefsymoff
, SEEK_SET
) != 0)
2238 for (i
= 0; i
< cmd
->nextrefsyms
; i
++)
2240 bfd_mach_o_dylib_reference
*ref
= &cmd
->ext_refs
[i
];
2242 if (bfd_bread ((void *) buf
, 4, abfd
) != 4)
2245 /* Fields isym and flags are written as bit-fields, thus we need
2246 a specific processing for endianness. */
2247 v
= bfd_h_get_32 (abfd
, buf
+ 0);
2248 if (bfd_big_endian (abfd
))
2250 ref
->isym
= (v
>> 8) & 0xffffff;
2251 ref
->flags
= v
& 0xff;
2255 ref
->isym
= v
& 0xffffff;
2256 ref
->flags
= (v
>> 24) & 0xff;
2261 if (mdata
->dysymtab
)
2263 mdata
->dysymtab
= cmd
;
2269 bfd_mach_o_read_symtab (bfd
*abfd
, bfd_mach_o_load_command
*command
)
2271 bfd_mach_o_symtab_command
*symtab
= &command
->command
.symtab
;
2272 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
2273 unsigned char buf
[16];
2275 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SYMTAB
);
2277 if (bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
) != 0
2278 || bfd_bread ((void *) buf
, 16, abfd
) != 16)
2281 symtab
->symoff
= bfd_h_get_32 (abfd
, buf
);
2282 symtab
->nsyms
= bfd_h_get_32 (abfd
, buf
+ 4);
2283 symtab
->stroff
= bfd_h_get_32 (abfd
, buf
+ 8);
2284 symtab
->strsize
= bfd_h_get_32 (abfd
, buf
+ 12);
2285 symtab
->symbols
= NULL
;
2286 symtab
->strtab
= NULL
;
2288 if (symtab
->nsyms
!= 0)
2289 abfd
->flags
|= HAS_SYMS
;
2293 mdata
->symtab
= symtab
;
2298 bfd_mach_o_read_uuid (bfd
*abfd
, bfd_mach_o_load_command
*command
)
2300 bfd_mach_o_uuid_command
*cmd
= &command
->command
.uuid
;
2302 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_UUID
);
2304 if (bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
) != 0
2305 || bfd_bread ((void *) cmd
->uuid
, 16, abfd
) != 16)
2312 bfd_mach_o_read_linkedit (bfd
*abfd
, bfd_mach_o_load_command
*command
)
2314 bfd_mach_o_linkedit_command
*cmd
= &command
->command
.linkedit
;
2317 if (bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
) != 0
2318 || bfd_bread ((void *) buf
, 8, abfd
) != 8)
2321 cmd
->dataoff
= bfd_get_32 (abfd
, buf
+ 0);
2322 cmd
->datasize
= bfd_get_32 (abfd
, buf
+ 4);
2327 bfd_mach_o_read_str (bfd
*abfd
, bfd_mach_o_load_command
*command
)
2329 bfd_mach_o_str_command
*cmd
= &command
->command
.str
;
2333 if (bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
) != 0
2334 || bfd_bread ((void *) buf
, 4, abfd
) != 4)
2337 off
= bfd_get_32 (abfd
, buf
+ 0);
2338 cmd
->stroff
= command
->offset
+ off
;
2339 cmd
->str_len
= command
->len
- off
;
2340 cmd
->str
= bfd_alloc (abfd
, cmd
->str_len
);
2341 if (cmd
->str
== NULL
)
2343 if (bfd_seek (abfd
, cmd
->stroff
, SEEK_SET
) != 0
2344 || bfd_bread ((void *) cmd
->str
, cmd
->str_len
, abfd
) != cmd
->str_len
)
2350 bfd_mach_o_read_dyld_info (bfd
*abfd
, bfd_mach_o_load_command
*command
)
2352 bfd_mach_o_dyld_info_command
*cmd
= &command
->command
.dyld_info
;
2355 if (bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
) != 0
2356 || bfd_bread ((void *) buf
, sizeof (buf
), abfd
) != sizeof (buf
))
2359 cmd
->rebase_off
= bfd_get_32 (abfd
, buf
+ 0);
2360 cmd
->rebase_size
= bfd_get_32 (abfd
, buf
+ 4);
2361 cmd
->bind_off
= bfd_get_32 (abfd
, buf
+ 8);
2362 cmd
->bind_size
= bfd_get_32 (abfd
, buf
+ 12);
2363 cmd
->weak_bind_off
= bfd_get_32 (abfd
, buf
+ 16);
2364 cmd
->weak_bind_size
= bfd_get_32 (abfd
, buf
+ 20);
2365 cmd
->lazy_bind_off
= bfd_get_32 (abfd
, buf
+ 24);
2366 cmd
->lazy_bind_size
= bfd_get_32 (abfd
, buf
+ 28);
2367 cmd
->export_off
= bfd_get_32 (abfd
, buf
+ 32);
2368 cmd
->export_size
= bfd_get_32 (abfd
, buf
+ 36);
2373 bfd_mach_o_read_segment (bfd
*abfd
,
2374 bfd_mach_o_load_command
*command
,
2377 unsigned char buf
[64];
2378 bfd_mach_o_segment_command
*seg
= &command
->command
.segment
;
2383 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SEGMENT_64
);
2385 if (bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
) != 0
2386 || bfd_bread ((void *) buf
, 64, abfd
) != 64)
2389 memcpy (seg
->segname
, buf
, 16);
2390 seg
->segname
[16] = '\0';
2392 seg
->vmaddr
= bfd_h_get_64 (abfd
, buf
+ 16);
2393 seg
->vmsize
= bfd_h_get_64 (abfd
, buf
+ 24);
2394 seg
->fileoff
= bfd_h_get_64 (abfd
, buf
+ 32);
2395 seg
->filesize
= bfd_h_get_64 (abfd
, buf
+ 40);
2396 seg
->maxprot
= bfd_h_get_32 (abfd
, buf
+ 48);
2397 seg
->initprot
= bfd_h_get_32 (abfd
, buf
+ 52);
2398 seg
->nsects
= bfd_h_get_32 (abfd
, buf
+ 56);
2399 seg
->flags
= bfd_h_get_32 (abfd
, buf
+ 60);
2403 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SEGMENT
);
2405 if (bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
) != 0
2406 || bfd_bread ((void *) buf
, 48, abfd
) != 48)
2409 memcpy (seg
->segname
, buf
, 16);
2410 seg
->segname
[16] = '\0';
2412 seg
->vmaddr
= bfd_h_get_32 (abfd
, buf
+ 16);
2413 seg
->vmsize
= bfd_h_get_32 (abfd
, buf
+ 20);
2414 seg
->fileoff
= bfd_h_get_32 (abfd
, buf
+ 24);
2415 seg
->filesize
= bfd_h_get_32 (abfd
, buf
+ 28);
2416 seg
->maxprot
= bfd_h_get_32 (abfd
, buf
+ 32);
2417 seg
->initprot
= bfd_h_get_32 (abfd
, buf
+ 36);
2418 seg
->nsects
= bfd_h_get_32 (abfd
, buf
+ 40);
2419 seg
->flags
= bfd_h_get_32 (abfd
, buf
+ 44);
2422 if (seg
->nsects
!= 0)
2424 seg
->sections
= bfd_alloc (abfd
, seg
->nsects
2425 * sizeof (bfd_mach_o_section
));
2426 if (seg
->sections
== NULL
)
2429 for (i
= 0; i
< seg
->nsects
; i
++)
2433 segoff
= command
->offset
+ BFD_MACH_O_LC_SEGMENT_64_SIZE
2434 + (i
* BFD_MACH_O_SECTION_64_SIZE
);
2436 segoff
= command
->offset
+ BFD_MACH_O_LC_SEGMENT_SIZE
2437 + (i
* BFD_MACH_O_SECTION_SIZE
);
2439 if (bfd_mach_o_read_section
2440 (abfd
, &seg
->sections
[i
], segoff
, seg
->initprot
, wide
) != 0)
2449 bfd_mach_o_read_segment_32 (bfd
*abfd
, bfd_mach_o_load_command
*command
)
2451 return bfd_mach_o_read_segment (abfd
, command
, 0);
2455 bfd_mach_o_read_segment_64 (bfd
*abfd
, bfd_mach_o_load_command
*command
)
2457 return bfd_mach_o_read_segment (abfd
, command
, 1);
2461 bfd_mach_o_read_command (bfd
*abfd
, bfd_mach_o_load_command
*command
)
2463 unsigned char buf
[8];
2465 /* Read command type and length. */
2466 if (bfd_seek (abfd
, command
->offset
, SEEK_SET
) != 0
2467 || bfd_bread ((void *) buf
, 8, abfd
) != 8)
2470 command
->type
= bfd_h_get_32 (abfd
, buf
) & ~BFD_MACH_O_LC_REQ_DYLD
;
2471 command
->type_required
= (bfd_h_get_32 (abfd
, buf
) & BFD_MACH_O_LC_REQ_DYLD
2473 command
->len
= bfd_h_get_32 (abfd
, buf
+ 4);
2475 switch (command
->type
)
2477 case BFD_MACH_O_LC_SEGMENT
:
2478 if (bfd_mach_o_read_segment_32 (abfd
, command
) != 0)
2481 case BFD_MACH_O_LC_SEGMENT_64
:
2482 if (bfd_mach_o_read_segment_64 (abfd
, command
) != 0)
2485 case BFD_MACH_O_LC_SYMTAB
:
2486 if (bfd_mach_o_read_symtab (abfd
, command
) != 0)
2489 case BFD_MACH_O_LC_SYMSEG
:
2491 case BFD_MACH_O_LC_THREAD
:
2492 case BFD_MACH_O_LC_UNIXTHREAD
:
2493 if (bfd_mach_o_read_thread (abfd
, command
) != 0)
2496 case BFD_MACH_O_LC_LOAD_DYLINKER
:
2497 case BFD_MACH_O_LC_ID_DYLINKER
:
2498 if (bfd_mach_o_read_dylinker (abfd
, command
) != 0)
2501 case BFD_MACH_O_LC_LOAD_DYLIB
:
2502 case BFD_MACH_O_LC_ID_DYLIB
:
2503 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB
:
2504 case BFD_MACH_O_LC_REEXPORT_DYLIB
:
2505 if (bfd_mach_o_read_dylib (abfd
, command
) != 0)
2508 case BFD_MACH_O_LC_PREBOUND_DYLIB
:
2509 if (bfd_mach_o_read_prebound_dylib (abfd
, command
) != 0)
2512 case BFD_MACH_O_LC_LOADFVMLIB
:
2513 case BFD_MACH_O_LC_IDFVMLIB
:
2514 case BFD_MACH_O_LC_IDENT
:
2515 case BFD_MACH_O_LC_FVMFILE
:
2516 case BFD_MACH_O_LC_PREPAGE
:
2517 case BFD_MACH_O_LC_ROUTINES
:
2519 case BFD_MACH_O_LC_SUB_FRAMEWORK
:
2520 case BFD_MACH_O_LC_SUB_UMBRELLA
:
2521 case BFD_MACH_O_LC_SUB_LIBRARY
:
2522 case BFD_MACH_O_LC_SUB_CLIENT
:
2523 case BFD_MACH_O_LC_RPATH
:
2524 if (bfd_mach_o_read_str (abfd
, command
) != 0)
2527 case BFD_MACH_O_LC_DYSYMTAB
:
2528 if (bfd_mach_o_read_dysymtab (abfd
, command
) != 0)
2531 case BFD_MACH_O_LC_TWOLEVEL_HINTS
:
2532 case BFD_MACH_O_LC_PREBIND_CKSUM
:
2534 case BFD_MACH_O_LC_UUID
:
2535 if (bfd_mach_o_read_uuid (abfd
, command
) != 0)
2538 case BFD_MACH_O_LC_CODE_SIGNATURE
:
2539 case BFD_MACH_O_LC_SEGMENT_SPLIT_INFO
:
2540 if (bfd_mach_o_read_linkedit (abfd
, command
) != 0)
2543 case BFD_MACH_O_LC_DYLD_INFO
:
2544 if (bfd_mach_o_read_dyld_info (abfd
, command
) != 0)
2548 fprintf (stderr
, "unable to read unknown load command 0x%lx\n",
2549 (unsigned long) command
->type
);
2557 bfd_mach_o_flatten_sections (bfd
*abfd
)
2559 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
2563 /* Count total number of sections. */
2566 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
2568 if (mdata
->commands
[i
].type
== BFD_MACH_O_LC_SEGMENT
2569 || mdata
->commands
[i
].type
== BFD_MACH_O_LC_SEGMENT_64
)
2571 bfd_mach_o_segment_command
*seg
;
2573 seg
= &mdata
->commands
[i
].command
.segment
;
2574 mdata
->nsects
+= seg
->nsects
;
2578 /* Allocate sections array. */
2579 mdata
->sections
= bfd_alloc (abfd
,
2580 mdata
->nsects
* sizeof (bfd_mach_o_section
*));
2582 /* Fill the array. */
2585 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
2587 if (mdata
->commands
[i
].type
== BFD_MACH_O_LC_SEGMENT
2588 || mdata
->commands
[i
].type
== BFD_MACH_O_LC_SEGMENT_64
)
2590 bfd_mach_o_segment_command
*seg
;
2592 seg
= &mdata
->commands
[i
].command
.segment
;
2593 BFD_ASSERT (csect
+ seg
->nsects
<= mdata
->nsects
);
2595 for (j
= 0; j
< seg
->nsects
; j
++)
2596 mdata
->sections
[csect
++] = &seg
->sections
[j
];
2602 bfd_mach_o_scan_start_address (bfd
*abfd
)
2604 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
2605 bfd_mach_o_thread_command
*cmd
= NULL
;
2608 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
2610 if ((mdata
->commands
[i
].type
== BFD_MACH_O_LC_THREAD
) ||
2611 (mdata
->commands
[i
].type
== BFD_MACH_O_LC_UNIXTHREAD
))
2614 cmd
= &mdata
->commands
[i
].command
.thread
;
2623 for (i
= 0; i
< cmd
->nflavours
; i
++)
2625 if ((mdata
->header
.cputype
== BFD_MACH_O_CPU_TYPE_I386
)
2626 && (cmd
->flavours
[i
].flavour
2627 == (unsigned long) BFD_MACH_O_x86_THREAD_STATE32
))
2629 unsigned char buf
[4];
2631 if (bfd_seek (abfd
, cmd
->flavours
[i
].offset
+ 40, SEEK_SET
) != 0
2632 || bfd_bread (buf
, 4, abfd
) != 4)
2635 abfd
->start_address
= bfd_h_get_32 (abfd
, buf
);
2637 else if ((mdata
->header
.cputype
== BFD_MACH_O_CPU_TYPE_POWERPC
)
2638 && (cmd
->flavours
[i
].flavour
== BFD_MACH_O_PPC_THREAD_STATE
))
2640 unsigned char buf
[4];
2642 if (bfd_seek (abfd
, cmd
->flavours
[i
].offset
+ 0, SEEK_SET
) != 0
2643 || bfd_bread (buf
, 4, abfd
) != 4)
2646 abfd
->start_address
= bfd_h_get_32 (abfd
, buf
);
2648 else if ((mdata
->header
.cputype
== BFD_MACH_O_CPU_TYPE_POWERPC_64
)
2649 && (cmd
->flavours
[i
].flavour
== BFD_MACH_O_PPC_THREAD_STATE64
))
2651 unsigned char buf
[8];
2653 if (bfd_seek (abfd
, cmd
->flavours
[i
].offset
+ 0, SEEK_SET
) != 0
2654 || bfd_bread (buf
, 8, abfd
) != 8)
2657 abfd
->start_address
= bfd_h_get_64 (abfd
, buf
);
2659 else if ((mdata
->header
.cputype
== BFD_MACH_O_CPU_TYPE_X86_64
)
2660 && (cmd
->flavours
[i
].flavour
== BFD_MACH_O_x86_THREAD_STATE64
))
2662 unsigned char buf
[8];
2664 if (bfd_seek (abfd
, cmd
->flavours
[i
].offset
+ (16 * 8), SEEK_SET
) != 0
2665 || bfd_bread (buf
, 8, abfd
) != 8)
2668 abfd
->start_address
= bfd_h_get_64 (abfd
, buf
);
2676 bfd_mach_o_set_arch_mach (bfd
*abfd
,
2677 enum bfd_architecture arch
,
2678 unsigned long machine
)
2680 bfd_mach_o_backend_data
*bed
= bfd_mach_o_get_backend_data (abfd
);
2682 /* If this isn't the right architecture for this backend, and this
2683 isn't the generic backend, fail. */
2684 if (arch
!= bed
->arch
2685 && arch
!= bfd_arch_unknown
2686 && bed
->arch
!= bfd_arch_unknown
)
2689 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
2693 bfd_mach_o_scan (bfd
*abfd
,
2694 bfd_mach_o_header
*header
,
2695 bfd_mach_o_data_struct
*mdata
)
2698 enum bfd_architecture cputype
;
2699 unsigned long cpusubtype
;
2700 unsigned int hdrsize
;
2702 hdrsize
= mach_o_wide_p (header
) ?
2703 BFD_MACH_O_HEADER_64_SIZE
: BFD_MACH_O_HEADER_SIZE
;
2705 mdata
->header
= *header
;
2707 abfd
->flags
= abfd
->flags
& BFD_IN_MEMORY
;
2708 switch (header
->filetype
)
2710 case BFD_MACH_O_MH_OBJECT
:
2711 abfd
->flags
|= HAS_RELOC
;
2713 case BFD_MACH_O_MH_EXECUTE
:
2714 abfd
->flags
|= EXEC_P
;
2716 case BFD_MACH_O_MH_DYLIB
:
2717 case BFD_MACH_O_MH_BUNDLE
:
2718 abfd
->flags
|= DYNAMIC
;
2722 abfd
->tdata
.mach_o_data
= mdata
;
2724 bfd_mach_o_convert_architecture (header
->cputype
, header
->cpusubtype
,
2725 &cputype
, &cpusubtype
);
2726 if (cputype
== bfd_arch_unknown
)
2728 fprintf (stderr
, "bfd_mach_o_scan: unknown architecture 0x%lx/0x%lx\n",
2729 header
->cputype
, header
->cpusubtype
);
2733 bfd_set_arch_mach (abfd
, cputype
, cpusubtype
);
2735 if (header
->ncmds
!= 0)
2737 mdata
->commands
= bfd_alloc
2738 (abfd
, header
->ncmds
* sizeof (bfd_mach_o_load_command
));
2739 if (mdata
->commands
== NULL
)
2742 for (i
= 0; i
< header
->ncmds
; i
++)
2744 bfd_mach_o_load_command
*cur
= &mdata
->commands
[i
];
2747 cur
->offset
= hdrsize
;
2750 bfd_mach_o_load_command
*prev
= &mdata
->commands
[i
- 1];
2751 cur
->offset
= prev
->offset
+ prev
->len
;
2754 if (bfd_mach_o_read_command (abfd
, cur
) < 0)
2759 if (bfd_mach_o_scan_start_address (abfd
) < 0)
2762 bfd_mach_o_flatten_sections (abfd
);
2767 bfd_mach_o_mkobject_init (bfd
*abfd
)
2769 bfd_mach_o_data_struct
*mdata
= NULL
;
2771 mdata
= bfd_alloc (abfd
, sizeof (bfd_mach_o_data_struct
));
2774 abfd
->tdata
.mach_o_data
= mdata
;
2776 mdata
->header
.magic
= 0;
2777 mdata
->header
.cputype
= 0;
2778 mdata
->header
.cpusubtype
= 0;
2779 mdata
->header
.filetype
= 0;
2780 mdata
->header
.ncmds
= 0;
2781 mdata
->header
.sizeofcmds
= 0;
2782 mdata
->header
.flags
= 0;
2783 mdata
->header
.byteorder
= BFD_ENDIAN_UNKNOWN
;
2784 mdata
->commands
= NULL
;
2786 mdata
->sections
= NULL
;
2792 bfd_mach_o_gen_mkobject (bfd
*abfd
)
2794 bfd_mach_o_data_struct
*mdata
;
2796 if (!bfd_mach_o_mkobject_init (abfd
))
2799 mdata
= bfd_mach_o_get_data (abfd
);
2800 mdata
->header
.magic
= BFD_MACH_O_MH_MAGIC
;
2801 mdata
->header
.cputype
= 0;
2802 mdata
->header
.cpusubtype
= 0;
2803 mdata
->header
.byteorder
= abfd
->xvec
->byteorder
;
2804 mdata
->header
.version
= 1;
2810 bfd_mach_o_header_p (bfd
*abfd
,
2811 bfd_mach_o_filetype filetype
,
2812 bfd_mach_o_cpu_type cputype
)
2814 struct bfd_preserve preserve
;
2815 bfd_mach_o_header header
;
2817 preserve
.marker
= NULL
;
2818 if (!bfd_mach_o_read_header (abfd
, &header
))
2821 if (! (header
.byteorder
== BFD_ENDIAN_BIG
2822 || header
.byteorder
== BFD_ENDIAN_LITTLE
))
2824 fprintf (stderr
, "unknown header byte-order value 0x%lx\n",
2825 (unsigned long) header
.byteorder
);
2829 if (! ((header
.byteorder
== BFD_ENDIAN_BIG
2830 && abfd
->xvec
->byteorder
== BFD_ENDIAN_BIG
2831 && abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
2832 || (header
.byteorder
== BFD_ENDIAN_LITTLE
2833 && abfd
->xvec
->byteorder
== BFD_ENDIAN_LITTLE
2834 && abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)))
2837 /* Check cputype and filetype.
2838 In case of wildcard, do not accept magics that are handled by existing
2842 if (header
.cputype
!= cputype
)
2847 switch (header
.cputype
)
2849 case BFD_MACH_O_CPU_TYPE_I386
:
2850 /* Handled by mach-o-i386 */
2858 if (header
.filetype
!= filetype
)
2863 switch (header
.filetype
)
2865 case BFD_MACH_O_MH_CORE
:
2866 /* Handled by core_p */
2873 preserve
.marker
= bfd_zalloc (abfd
, sizeof (bfd_mach_o_data_struct
));
2874 if (preserve
.marker
== NULL
2875 || !bfd_preserve_save (abfd
, &preserve
))
2878 if (bfd_mach_o_scan (abfd
, &header
,
2879 (bfd_mach_o_data_struct
*) preserve
.marker
) != 0)
2882 bfd_preserve_finish (abfd
, &preserve
);
2886 bfd_set_error (bfd_error_wrong_format
);
2889 if (preserve
.marker
!= NULL
)
2890 bfd_preserve_restore (abfd
, &preserve
);
2894 static const bfd_target
*
2895 bfd_mach_o_gen_object_p (bfd
*abfd
)
2897 return bfd_mach_o_header_p (abfd
, 0, 0);
2900 static const bfd_target
*
2901 bfd_mach_o_gen_core_p (bfd
*abfd
)
2903 return bfd_mach_o_header_p (abfd
, BFD_MACH_O_MH_CORE
, 0);
2906 typedef struct mach_o_fat_archentry
2908 unsigned long cputype
;
2909 unsigned long cpusubtype
;
2910 unsigned long offset
;
2912 unsigned long align
;
2913 } mach_o_fat_archentry
;
2915 typedef struct mach_o_fat_data_struct
2917 unsigned long magic
;
2918 unsigned long nfat_arch
;
2919 mach_o_fat_archentry
*archentries
;
2920 } mach_o_fat_data_struct
;
2923 bfd_mach_o_archive_p (bfd
*abfd
)
2925 mach_o_fat_data_struct
*adata
= NULL
;
2926 unsigned char buf
[20];
2929 if (bfd_seek (abfd
, 0, SEEK_SET
) != 0
2930 || bfd_bread ((void *) buf
, 8, abfd
) != 8)
2933 adata
= bfd_alloc (abfd
, sizeof (mach_o_fat_data_struct
));
2937 adata
->magic
= bfd_getb32 (buf
);
2938 adata
->nfat_arch
= bfd_getb32 (buf
+ 4);
2939 if (adata
->magic
!= 0xcafebabe)
2941 /* Avoid matching Java bytecode files, which have the same magic number.
2942 In the Java bytecode file format this field contains the JVM version,
2943 which starts at 43.0. */
2944 if (adata
->nfat_arch
> 30)
2947 adata
->archentries
=
2948 bfd_alloc (abfd
, adata
->nfat_arch
* sizeof (mach_o_fat_archentry
));
2949 if (adata
->archentries
== NULL
)
2952 for (i
= 0; i
< adata
->nfat_arch
; i
++)
2954 if (bfd_seek (abfd
, 8 + 20 * i
, SEEK_SET
) != 0
2955 || bfd_bread ((void *) buf
, 20, abfd
) != 20)
2957 adata
->archentries
[i
].cputype
= bfd_getb32 (buf
);
2958 adata
->archentries
[i
].cpusubtype
= bfd_getb32 (buf
+ 4);
2959 adata
->archentries
[i
].offset
= bfd_getb32 (buf
+ 8);
2960 adata
->archentries
[i
].size
= bfd_getb32 (buf
+ 12);
2961 adata
->archentries
[i
].align
= bfd_getb32 (buf
+ 16);
2964 abfd
->tdata
.mach_o_fat_data
= adata
;
2969 bfd_release (abfd
, adata
);
2970 bfd_set_error (bfd_error_wrong_format
);
2975 bfd_mach_o_openr_next_archived_file (bfd
*archive
, bfd
*prev
)
2977 mach_o_fat_data_struct
*adata
;
2978 mach_o_fat_archentry
*entry
= NULL
;
2981 enum bfd_architecture arch_type
;
2982 unsigned long arch_subtype
;
2984 adata
= (mach_o_fat_data_struct
*) archive
->tdata
.mach_o_fat_data
;
2985 BFD_ASSERT (adata
!= NULL
);
2987 /* Find index of previous entry. */
2989 i
= 0; /* Start at first one. */
2992 for (i
= 0; i
< adata
->nfat_arch
; i
++)
2994 if (adata
->archentries
[i
].offset
== prev
->origin
)
2998 if (i
== adata
->nfat_arch
)
3001 bfd_set_error (bfd_error_bad_value
);
3004 i
++; /* Get next entry. */
3007 if (i
>= adata
->nfat_arch
)
3009 bfd_set_error (bfd_error_no_more_archived_files
);
3013 entry
= &adata
->archentries
[i
];
3014 nbfd
= _bfd_new_bfd_contained_in (archive
);
3018 nbfd
->origin
= entry
->offset
;
3020 bfd_mach_o_convert_architecture (entry
->cputype
, entry
->cpusubtype
,
3021 &arch_type
, &arch_subtype
);
3022 /* Create the member filename.
3023 Use FILENAME:ARCH_NAME. */
3026 const char *arch_name
;
3027 size_t arch_file_len
= strlen (bfd_get_filename (archive
));
3029 arch_name
= bfd_printable_arch_mach (arch_type
, arch_subtype
);
3030 s
= bfd_malloc (arch_file_len
+ 1 + strlen (arch_name
) + 1);
3033 memcpy (s
, bfd_get_filename (archive
), arch_file_len
);
3034 s
[arch_file_len
] = ':';
3035 strcpy (s
+ arch_file_len
+ 1, arch_name
);
3038 nbfd
->iostream
= NULL
;
3039 bfd_set_arch_mach (nbfd
, arch_type
, arch_subtype
);
3044 /* If ABFD format is FORMAT and architecture is ARCH, return it.
3045 If ABFD is a fat image containing a member that corresponds to FORMAT
3046 and ARCH, returns it.
3047 In other case, returns NULL.
3048 This function allows transparent uses of fat images. */
3050 bfd_mach_o_fat_extract (bfd
*abfd
,
3052 const bfd_arch_info_type
*arch
)
3055 mach_o_fat_data_struct
*adata
;
3058 if (bfd_check_format (abfd
, format
))
3060 if (bfd_get_arch_info (abfd
) == arch
)
3064 if (!bfd_check_format (abfd
, bfd_archive
)
3065 || abfd
->xvec
!= &mach_o_fat_vec
)
3068 /* This is a Mach-O fat image. */
3069 adata
= (mach_o_fat_data_struct
*) abfd
->tdata
.mach_o_fat_data
;
3070 BFD_ASSERT (adata
!= NULL
);
3072 for (i
= 0; i
< adata
->nfat_arch
; i
++)
3074 struct mach_o_fat_archentry
*e
= &adata
->archentries
[i
];
3075 enum bfd_architecture cpu_type
;
3076 unsigned long cpu_subtype
;
3078 bfd_mach_o_convert_architecture (e
->cputype
, e
->cpusubtype
,
3079 &cpu_type
, &cpu_subtype
);
3080 if (cpu_type
!= arch
->arch
|| cpu_subtype
!= arch
->mach
)
3083 /* The architecture is found. */
3084 res
= _bfd_new_bfd_contained_in (abfd
);
3088 res
->origin
= e
->offset
;
3090 res
->filename
= strdup (abfd
->filename
);
3091 res
->iostream
= NULL
;
3093 if (bfd_check_format (res
, format
))
3095 BFD_ASSERT (bfd_get_arch_info (res
) == arch
);
3106 bfd_mach_o_lookup_section (bfd
*abfd
,
3108 bfd_mach_o_load_command
**mcommand
,
3109 bfd_mach_o_section
**msection
)
3111 struct mach_o_data_struct
*md
= bfd_mach_o_get_data (abfd
);
3112 unsigned int i
, j
, num
;
3114 bfd_mach_o_load_command
*ncmd
= NULL
;
3115 bfd_mach_o_section
*nsect
= NULL
;
3117 BFD_ASSERT (mcommand
!= NULL
);
3118 BFD_ASSERT (msection
!= NULL
);
3121 for (i
= 0; i
< md
->header
.ncmds
; i
++)
3123 struct bfd_mach_o_load_command
*cmd
= &md
->commands
[i
];
3124 struct bfd_mach_o_segment_command
*seg
= NULL
;
3126 if (cmd
->type
!= BFD_MACH_O_LC_SEGMENT
3127 || cmd
->type
!= BFD_MACH_O_LC_SEGMENT_64
)
3129 seg
= &cmd
->command
.segment
;
3131 for (j
= 0; j
< seg
->nsects
; j
++)
3133 struct bfd_mach_o_section
*sect
= &seg
->sections
[j
];
3135 if (sect
->bfdsection
== section
)
3153 bfd_mach_o_lookup_command (bfd
*abfd
,
3154 bfd_mach_o_load_command_type type
,
3155 bfd_mach_o_load_command
**mcommand
)
3157 struct mach_o_data_struct
*md
= bfd_mach_o_get_data (abfd
);
3158 bfd_mach_o_load_command
*ncmd
= NULL
;
3159 unsigned int i
, num
;
3161 BFD_ASSERT (md
!= NULL
);
3162 BFD_ASSERT (mcommand
!= NULL
);
3165 for (i
= 0; i
< md
->header
.ncmds
; i
++)
3167 struct bfd_mach_o_load_command
*cmd
= &md
->commands
[i
];
3169 if (cmd
->type
!= type
)
3182 bfd_mach_o_stack_addr (enum bfd_mach_o_cpu_type type
)
3186 case BFD_MACH_O_CPU_TYPE_MC680x0
:
3188 case BFD_MACH_O_CPU_TYPE_MC88000
:
3190 case BFD_MACH_O_CPU_TYPE_POWERPC
:
3192 case BFD_MACH_O_CPU_TYPE_I386
:
3194 case BFD_MACH_O_CPU_TYPE_SPARC
:
3196 case BFD_MACH_O_CPU_TYPE_I860
:
3198 case BFD_MACH_O_CPU_TYPE_HPPA
:
3199 return 0xc0000000 - 0x04000000;
3205 typedef struct bfd_mach_o_xlat_name
3210 bfd_mach_o_xlat_name
;
3213 bfd_mach_o_print_flags (const bfd_mach_o_xlat_name
*table
,
3219 for (; table
->name
; table
++)
3221 if (table
->val
& val
)
3224 fprintf (file
, "+");
3225 fprintf (file
, "%s", table
->name
);
3233 fprintf (file
, "+");
3234 fprintf (file
, "0x%lx", val
);
3238 fprintf (file
, "-");
3242 bfd_mach_o_get_name (const bfd_mach_o_xlat_name
*table
, unsigned long val
)
3244 for (; table
->name
; table
++)
3245 if (table
->val
== val
)
3250 static bfd_mach_o_xlat_name bfd_mach_o_cpu_name
[] =
3252 { "vax", BFD_MACH_O_CPU_TYPE_VAX
},
3253 { "mc680x0", BFD_MACH_O_CPU_TYPE_MC680x0
},
3254 { "i386", BFD_MACH_O_CPU_TYPE_I386
},
3255 { "mips", BFD_MACH_O_CPU_TYPE_MIPS
},
3256 { "mc98000", BFD_MACH_O_CPU_TYPE_MC98000
},
3257 { "hppa", BFD_MACH_O_CPU_TYPE_HPPA
},
3258 { "arm", BFD_MACH_O_CPU_TYPE_ARM
},
3259 { "mc88000", BFD_MACH_O_CPU_TYPE_MC88000
},
3260 { "sparc", BFD_MACH_O_CPU_TYPE_SPARC
},
3261 { "i860", BFD_MACH_O_CPU_TYPE_I860
},
3262 { "alpha", BFD_MACH_O_CPU_TYPE_ALPHA
},
3263 { "powerpc", BFD_MACH_O_CPU_TYPE_POWERPC
},
3264 { "powerpc_64", BFD_MACH_O_CPU_TYPE_POWERPC_64
},
3265 { "x86_64", BFD_MACH_O_CPU_TYPE_X86_64
},
3269 static bfd_mach_o_xlat_name bfd_mach_o_filetype_name
[] =
3271 { "object", BFD_MACH_O_MH_OBJECT
},
3272 { "execute", BFD_MACH_O_MH_EXECUTE
},
3273 { "fvmlib", BFD_MACH_O_MH_FVMLIB
},
3274 { "core", BFD_MACH_O_MH_CORE
},
3275 { "preload", BFD_MACH_O_MH_PRELOAD
},
3276 { "dylib", BFD_MACH_O_MH_DYLIB
},
3277 { "dylinker", BFD_MACH_O_MH_DYLINKER
},
3278 { "bundle", BFD_MACH_O_MH_BUNDLE
},
3279 { "dylib_stub", BFD_MACH_O_MH_DYLIB_STUB
},
3280 { "dym", BFD_MACH_O_MH_DSYM
},
3281 { "kext_bundle", BFD_MACH_O_MH_KEXT_BUNDLE
},
3285 static bfd_mach_o_xlat_name bfd_mach_o_header_flags_name
[] =
3287 { "noundefs", BFD_MACH_O_MH_NOUNDEFS
},
3288 { "incrlink", BFD_MACH_O_MH_INCRLINK
},
3289 { "dyldlink", BFD_MACH_O_MH_DYLDLINK
},
3290 { "bindatload", BFD_MACH_O_MH_BINDATLOAD
},
3291 { "prebound", BFD_MACH_O_MH_PREBOUND
},
3292 { "split_segs", BFD_MACH_O_MH_SPLIT_SEGS
},
3293 { "lazy_init", BFD_MACH_O_MH_LAZY_INIT
},
3294 { "twolevel", BFD_MACH_O_MH_TWOLEVEL
},
3295 { "force_flat", BFD_MACH_O_MH_FORCE_FLAT
},
3296 { "nomultidefs", BFD_MACH_O_MH_NOMULTIDEFS
},
3297 { "nofixprebinding", BFD_MACH_O_MH_NOFIXPREBINDING
},
3298 { "prebindable", BFD_MACH_O_MH_PREBINDABLE
},
3299 { "allmodsbound", BFD_MACH_O_MH_ALLMODSBOUND
},
3300 { "subsections_via_symbols", BFD_MACH_O_MH_SUBSECTIONS_VIA_SYMBOLS
},
3301 { "canonical", BFD_MACH_O_MH_CANONICAL
},
3302 { "weak_defines", BFD_MACH_O_MH_WEAK_DEFINES
},
3303 { "binds_to_weak", BFD_MACH_O_MH_BINDS_TO_WEAK
},
3304 { "allow_stack_execution", BFD_MACH_O_MH_ALLOW_STACK_EXECUTION
},
3305 { "root_safe", BFD_MACH_O_MH_ROOT_SAFE
},
3306 { "setuid_safe", BFD_MACH_O_MH_SETUID_SAFE
},
3307 { "no_reexported_dylibs", BFD_MACH_O_MH_NO_REEXPORTED_DYLIBS
},
3308 { "pie", BFD_MACH_O_MH_PIE
},
3312 static bfd_mach_o_xlat_name bfd_mach_o_section_type_name
[] =
3314 { "regular", BFD_MACH_O_S_REGULAR
},
3315 { "zerofill", BFD_MACH_O_S_ZEROFILL
},
3316 { "cstring_literals", BFD_MACH_O_S_CSTRING_LITERALS
},
3317 { "4byte_literals", BFD_MACH_O_S_4BYTE_LITERALS
},
3318 { "8byte_literals", BFD_MACH_O_S_8BYTE_LITERALS
},
3319 { "literal_pointers", BFD_MACH_O_S_LITERAL_POINTERS
},
3320 { "non_lazy_symbol_pointers", BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS
},
3321 { "lazy_symbol_pointers", BFD_MACH_O_S_LAZY_SYMBOL_POINTERS
},
3322 { "symbol_stubs", BFD_MACH_O_S_SYMBOL_STUBS
},
3323 { "mod_init_func_pointers", BFD_MACH_O_S_MOD_INIT_FUNC_POINTERS
},
3324 { "mod_fini_func_pointers", BFD_MACH_O_S_MOD_FINI_FUNC_POINTERS
},
3325 { "coalesced", BFD_MACH_O_S_COALESCED
},
3326 { "gb_zerofill", BFD_MACH_O_S_GB_ZEROFILL
},
3327 { "interposing", BFD_MACH_O_S_INTERPOSING
},
3328 { "16byte_literals", BFD_MACH_O_S_16BYTE_LITERALS
},
3329 { "dtrace_dof", BFD_MACH_O_S_DTRACE_DOF
},
3330 { "lazy_dylib_symbol_pointers", BFD_MACH_O_S_LAZY_DYLIB_SYMBOL_POINTERS
},
3334 static bfd_mach_o_xlat_name bfd_mach_o_section_attribute_name
[] =
3336 { "loc_reloc", BFD_MACH_O_S_ATTR_LOC_RELOC
},
3337 { "ext_reloc", BFD_MACH_O_S_ATTR_EXT_RELOC
},
3338 { "some_instructions", BFD_MACH_O_S_ATTR_SOME_INSTRUCTIONS
},
3339 { "debug", BFD_MACH_O_S_ATTR_DEBUG
},
3340 { "modifying_code", BFD_MACH_O_S_SELF_MODIFYING_CODE
},
3341 { "live_support", BFD_MACH_O_S_ATTR_LIVE_SUPPORT
},
3342 { "no_dead_strip", BFD_MACH_O_S_ATTR_NO_DEAD_STRIP
},
3343 { "strip_static_syms", BFD_MACH_O_S_ATTR_STRIP_STATIC_SYMS
},
3344 { "no_toc", BFD_MACH_O_S_ATTR_NO_TOC
},
3345 { "pure_instructions", BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS
},
3349 static bfd_mach_o_xlat_name bfd_mach_o_load_command_name
[] =
3351 { "segment", BFD_MACH_O_LC_SEGMENT
},
3352 { "symtab", BFD_MACH_O_LC_SYMTAB
},
3353 { "symseg", BFD_MACH_O_LC_SYMSEG
},
3354 { "thread", BFD_MACH_O_LC_THREAD
},
3355 { "unixthread", BFD_MACH_O_LC_UNIXTHREAD
},
3356 { "loadfvmlib", BFD_MACH_O_LC_LOADFVMLIB
},
3357 { "idfvmlib", BFD_MACH_O_LC_IDFVMLIB
},
3358 { "ident", BFD_MACH_O_LC_IDENT
},
3359 { "fvmfile", BFD_MACH_O_LC_FVMFILE
},
3360 { "prepage", BFD_MACH_O_LC_PREPAGE
},
3361 { "dysymtab", BFD_MACH_O_LC_DYSYMTAB
},
3362 { "load_dylib", BFD_MACH_O_LC_LOAD_DYLIB
},
3363 { "id_dylib", BFD_MACH_O_LC_ID_DYLIB
},
3364 { "load_dylinker", BFD_MACH_O_LC_LOAD_DYLINKER
},
3365 { "id_dylinker", BFD_MACH_O_LC_ID_DYLINKER
},
3366 { "prebound_dylib", BFD_MACH_O_LC_PREBOUND_DYLIB
},
3367 { "routines", BFD_MACH_O_LC_ROUTINES
},
3368 { "sub_framework", BFD_MACH_O_LC_SUB_FRAMEWORK
},
3369 { "sub_umbrella", BFD_MACH_O_LC_SUB_UMBRELLA
},
3370 { "sub_client", BFD_MACH_O_LC_SUB_CLIENT
},
3371 { "sub_library", BFD_MACH_O_LC_SUB_LIBRARY
},
3372 { "twolevel_hints", BFD_MACH_O_LC_TWOLEVEL_HINTS
},
3373 { "prebind_cksum", BFD_MACH_O_LC_PREBIND_CKSUM
},
3374 { "load_weak_dylib", BFD_MACH_O_LC_LOAD_WEAK_DYLIB
},
3375 { "segment_64", BFD_MACH_O_LC_SEGMENT_64
},
3376 { "routines_64", BFD_MACH_O_LC_ROUTINES_64
},
3377 { "uuid", BFD_MACH_O_LC_UUID
},
3378 { "rpath", BFD_MACH_O_LC_RPATH
},
3379 { "code_signature", BFD_MACH_O_LC_CODE_SIGNATURE
},
3380 { "segment_split_info", BFD_MACH_O_LC_SEGMENT_SPLIT_INFO
},
3381 { "reexport_dylib", BFD_MACH_O_LC_REEXPORT_DYLIB
},
3382 { "lazy_load_dylib", BFD_MACH_O_LC_LAZY_LOAD_DYLIB
},
3383 { "encryption_info", BFD_MACH_O_LC_ENCRYPTION_INFO
},
3384 { "dyld_info", BFD_MACH_O_LC_DYLD_INFO
},
3389 bfd_mach_o_print_private_header (bfd
*abfd
, FILE *file
)
3391 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
3392 bfd_mach_o_header
*h
= &mdata
->header
;
3394 fputs (_("Mach-O header:\n"), file
);
3395 fprintf (file
, _(" magic : %08lx\n"), h
->magic
);
3396 fprintf (file
, _(" cputype : %08lx (%s)\n"), h
->cputype
,
3397 bfd_mach_o_get_name (bfd_mach_o_cpu_name
, h
->cputype
));
3398 fprintf (file
, _(" cpusubtype: %08lx\n"), h
->cpusubtype
);
3399 fprintf (file
, _(" filetype : %08lx (%s)\n"),
3401 bfd_mach_o_get_name (bfd_mach_o_filetype_name
, h
->filetype
));
3402 fprintf (file
, _(" ncmds : %08lx (%lu)\n"), h
->ncmds
, h
->ncmds
);
3403 fprintf (file
, _(" sizeofcmds: %08lx\n"), h
->sizeofcmds
);
3404 fprintf (file
, _(" flags : %08lx ("), h
->flags
);
3405 bfd_mach_o_print_flags (bfd_mach_o_header_flags_name
, h
->flags
, file
);
3406 fputs (_(")\n"), file
);
3407 fprintf (file
, _(" reserved : %08x\n"), h
->reserved
);
3411 bfd_mach_o_print_section_map (bfd
*abfd
, FILE *file
)
3413 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
3415 unsigned int sec_nbr
= 0;
3417 fputs (_("Segments and Sections:\n"), file
);
3418 fputs (_(" #: Segment name Section name Address\n"), file
);
3420 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
3422 bfd_mach_o_segment_command
*seg
;
3424 if (mdata
->commands
[i
].type
!= BFD_MACH_O_LC_SEGMENT
3425 && mdata
->commands
[i
].type
!= BFD_MACH_O_LC_SEGMENT_64
)
3428 seg
= &mdata
->commands
[i
].command
.segment
;
3430 fprintf (file
, "[Segment %-16s ", seg
->segname
);
3431 fprintf_vma (file
, seg
->vmaddr
);
3432 fprintf (file
, "-");
3433 fprintf_vma (file
, seg
->vmaddr
+ seg
->vmsize
- 1);
3435 fputc (seg
->initprot
& BFD_MACH_O_PROT_READ
? 'r' : '-', file
);
3436 fputc (seg
->initprot
& BFD_MACH_O_PROT_WRITE
? 'w' : '-', file
);
3437 fputc (seg
->initprot
& BFD_MACH_O_PROT_EXECUTE
? 'x' : '-', file
);
3438 fprintf (file
, "]\n");
3439 for (j
= 0; j
< seg
->nsects
; j
++)
3441 bfd_mach_o_section
*sec
= &seg
->sections
[j
];
3442 fprintf (file
, "%02u: %-16s %-16s ", ++sec_nbr
,
3443 sec
->segname
, sec
->sectname
);
3444 fprintf_vma (file
, sec
->addr
);
3445 fprintf (file
, " ");
3446 fprintf_vma (file
, sec
->size
);
3447 fprintf (file
, " %08lx\n", sec
->flags
);
3453 bfd_mach_o_print_section (bfd
*abfd ATTRIBUTE_UNUSED
,
3454 bfd_mach_o_section
*sec
, FILE *file
)
3456 fprintf (file
, " Section: %-16s %-16s (bfdname: %s)\n",
3457 sec
->sectname
, sec
->segname
, sec
->bfdsection
->name
);
3458 fprintf (file
, " addr: ");
3459 fprintf_vma (file
, sec
->addr
);
3460 fprintf (file
, " size: ");
3461 fprintf_vma (file
, sec
->size
);
3462 fprintf (file
, " offset: ");
3463 fprintf_vma (file
, sec
->offset
);
3464 fprintf (file
, "\n");
3465 fprintf (file
, " align: %ld", sec
->align
);
3466 fprintf (file
, " nreloc: %lu reloff: ", sec
->nreloc
);
3467 fprintf_vma (file
, sec
->reloff
);
3468 fprintf (file
, "\n");
3469 fprintf (file
, " flags: %08lx (type: %s", sec
->flags
,
3470 bfd_mach_o_get_name (bfd_mach_o_section_type_name
,
3471 sec
->flags
& BFD_MACH_O_SECTION_TYPE_MASK
));
3472 fprintf (file
, " attr: ");
3473 bfd_mach_o_print_flags (bfd_mach_o_section_attribute_name
,
3474 sec
->flags
& BFD_MACH_O_SECTION_ATTRIBUTES_MASK
,
3476 fprintf (file
, ")\n");
3477 switch (sec
->flags
& BFD_MACH_O_SECTION_TYPE_MASK
)
3479 case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS
:
3480 case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS
:
3481 case BFD_MACH_O_S_SYMBOL_STUBS
:
3482 fprintf (file
, " first indirect sym: %lu", sec
->reserved1
);
3483 fprintf (file
, " (%u entries)",
3484 bfd_mach_o_section_get_nbr_indirect (abfd
, sec
));
3487 fprintf (file
, " reserved1: 0x%lx", sec
->reserved1
);
3490 switch (sec
->flags
& BFD_MACH_O_SECTION_TYPE_MASK
)
3492 case BFD_MACH_O_S_SYMBOL_STUBS
:
3493 fprintf (file
, " stub size: %lu", sec
->reserved2
);
3496 fprintf (file
, " reserved2: 0x%lx", sec
->reserved2
);
3499 fprintf (file
, " reserved3: 0x%lx\n", sec
->reserved3
);
3503 bfd_mach_o_print_segment (bfd
*abfd ATTRIBUTE_UNUSED
,
3504 bfd_mach_o_load_command
*cmd
, FILE *file
)
3506 bfd_mach_o_segment_command
*seg
= &cmd
->command
.segment
;
3509 fprintf (file
, " name: %s\n", *seg
->segname
? seg
->segname
: "*none*");
3510 fprintf (file
, " vmaddr: ");
3511 fprintf_vma (file
, seg
->vmaddr
);
3512 fprintf (file
, " vmsize: ");
3513 fprintf_vma (file
, seg
->vmsize
);
3514 fprintf (file
, "\n");
3515 fprintf (file
, " fileoff: ");
3516 fprintf_vma (file
, seg
->fileoff
);
3517 fprintf (file
, " filesize: ");
3518 fprintf_vma (file
, (bfd_vma
)seg
->filesize
);
3519 fprintf (file
, " endoff: ");
3520 fprintf_vma (file
, (bfd_vma
)(seg
->fileoff
+ seg
->filesize
));
3521 fprintf (file
, "\n");
3522 fprintf (file
, " nsects: %lu ", seg
->nsects
);
3523 fprintf (file
, " flags: %lx\n", seg
->flags
);
3524 for (i
= 0; i
< seg
->nsects
; i
++)
3525 bfd_mach_o_print_section (abfd
, &seg
->sections
[i
], file
);
3529 bfd_mach_o_print_dysymtab (bfd
*abfd ATTRIBUTE_UNUSED
,
3530 bfd_mach_o_load_command
*cmd
, FILE *file
)
3532 bfd_mach_o_dysymtab_command
*dysymtab
= &cmd
->command
.dysymtab
;
3533 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
3536 fprintf (file
, " local symbols: idx: %10lu num: %-8lu",
3537 dysymtab
->ilocalsym
, dysymtab
->nlocalsym
);
3538 fprintf (file
, " (nxtidx: %lu)\n",
3539 dysymtab
->ilocalsym
+ dysymtab
->nlocalsym
);
3540 fprintf (file
, " external symbols: idx: %10lu num: %-8lu",
3541 dysymtab
->iextdefsym
, dysymtab
->nextdefsym
);
3542 fprintf (file
, " (nxtidx: %lu)\n",
3543 dysymtab
->iextdefsym
+ dysymtab
->nextdefsym
);
3544 fprintf (file
, " undefined symbols: idx: %10lu num: %-8lu",
3545 dysymtab
->iundefsym
, dysymtab
->nundefsym
);
3546 fprintf (file
, " (nxtidx: %lu)\n",
3547 dysymtab
->iundefsym
+ dysymtab
->nundefsym
);
3548 fprintf (file
, " table of content: off: 0x%08lx num: %-8lu",
3549 dysymtab
->tocoff
, dysymtab
->ntoc
);
3550 fprintf (file
, " (endoff: 0x%08lx)\n",
3552 + dysymtab
->ntoc
* BFD_MACH_O_TABLE_OF_CONTENT_SIZE
);
3553 fprintf (file
, " module table: off: 0x%08lx num: %-8lu",
3554 dysymtab
->modtaboff
, dysymtab
->nmodtab
);
3555 fprintf (file
, " (endoff: 0x%08lx)\n",
3556 dysymtab
->modtaboff
+ dysymtab
->nmodtab
3557 * (mach_o_wide_p (&mdata
->header
) ?
3558 BFD_MACH_O_DYLIB_MODULE_64_SIZE
: BFD_MACH_O_DYLIB_MODULE_SIZE
));
3559 fprintf (file
, " external reference table: off: 0x%08lx num: %-8lu",
3560 dysymtab
->extrefsymoff
, dysymtab
->nextrefsyms
);
3561 fprintf (file
, " (endoff: 0x%08lx)\n",
3562 dysymtab
->extrefsymoff
3563 + dysymtab
->nextrefsyms
* BFD_MACH_O_REFERENCE_SIZE
);
3564 fprintf (file
, " indirect symbol table: off: 0x%08lx num: %-8lu",
3565 dysymtab
->indirectsymoff
, dysymtab
->nindirectsyms
);
3566 fprintf (file
, " (endoff: 0x%08lx)\n",
3567 dysymtab
->indirectsymoff
3568 + dysymtab
->nindirectsyms
* BFD_MACH_O_INDIRECT_SYMBOL_SIZE
);
3569 fprintf (file
, " external relocation table: off: 0x%08lx num: %-8lu",
3570 dysymtab
->extreloff
, dysymtab
->nextrel
);
3571 fprintf (file
, " (endoff: 0x%08lx)\n",
3572 dysymtab
->extreloff
+ dysymtab
->nextrel
* BFD_MACH_O_RELENT_SIZE
);
3573 fprintf (file
, " local relocation table: off: 0x%08lx num: %-8lu",
3574 dysymtab
->locreloff
, dysymtab
->nlocrel
);
3575 fprintf (file
, " (endoff: 0x%08lx)\n",
3576 dysymtab
->locreloff
+ dysymtab
->nlocrel
* BFD_MACH_O_RELENT_SIZE
);
3578 if (dysymtab
->ntoc
> 0
3579 || dysymtab
->nindirectsyms
> 0
3580 || dysymtab
->nextrefsyms
> 0)
3582 /* Try to read the symbols to display the toc or indirect symbols. */
3583 bfd_mach_o_read_symtab_symbols (abfd
);
3585 else if (dysymtab
->nmodtab
> 0)
3587 /* Try to read the strtab to display modules name. */
3588 bfd_mach_o_read_symtab_strtab (abfd
);
3591 for (i
= 0; i
< dysymtab
->nmodtab
; i
++)
3593 bfd_mach_o_dylib_module
*module
= &dysymtab
->dylib_module
[i
];
3594 fprintf (file
, " module %u:\n", i
);
3595 fprintf (file
, " name: %lu", module
->module_name_idx
);
3596 if (mdata
->symtab
&& mdata
->symtab
->strtab
)
3597 fprintf (file
, ": %s",
3598 mdata
->symtab
->strtab
+ module
->module_name_idx
);
3599 fprintf (file
, "\n");
3600 fprintf (file
, " extdefsym: idx: %8lu num: %lu\n",
3601 module
->iextdefsym
, module
->nextdefsym
);
3602 fprintf (file
, " refsym: idx: %8lu num: %lu\n",
3603 module
->irefsym
, module
->nrefsym
);
3604 fprintf (file
, " localsym: idx: %8lu num: %lu\n",
3605 module
->ilocalsym
, module
->nlocalsym
);
3606 fprintf (file
, " extrel: idx: %8lu num: %lu\n",
3607 module
->iextrel
, module
->nextrel
);
3608 fprintf (file
, " init: idx: %8u num: %u\n",
3609 module
->iinit
, module
->ninit
);
3610 fprintf (file
, " term: idx: %8u num: %u\n",
3611 module
->iterm
, module
->nterm
);
3612 fprintf (file
, " objc_module_info: addr: ");
3613 fprintf_vma (file
, module
->objc_module_info_addr
);
3614 fprintf (file
, " size: %lu\n", module
->objc_module_info_size
);
3617 if (dysymtab
->ntoc
> 0)
3619 bfd_mach_o_symtab_command
*symtab
= mdata
->symtab
;
3621 fprintf (file
, " table of content: (symbol/module)\n");
3622 for (i
= 0; i
< dysymtab
->ntoc
; i
++)
3624 bfd_mach_o_dylib_table_of_content
*toc
= &dysymtab
->dylib_toc
[i
];
3626 fprintf (file
, " %4u: ", i
);
3627 if (symtab
&& symtab
->symbols
&& toc
->symbol_index
< symtab
->nsyms
)
3629 const char *name
= symtab
->symbols
[toc
->symbol_index
].symbol
.name
;
3630 fprintf (file
, "%s (%lu)", name
? name
: "*invalid*",
3634 fprintf (file
, "%lu", toc
->symbol_index
);
3636 fprintf (file
, " / ");
3637 if (symtab
&& symtab
->strtab
3638 && toc
->module_index
< dysymtab
->nmodtab
)
3640 bfd_mach_o_dylib_module
*mod
;
3641 mod
= &dysymtab
->dylib_module
[toc
->module_index
];
3642 fprintf (file
, "%s (%lu)",
3643 symtab
->strtab
+ mod
->module_name_idx
,
3647 fprintf (file
, "%lu", toc
->module_index
);
3649 fprintf (file
, "\n");
3653 if (dysymtab
->nindirectsyms
!= 0)
3655 fprintf (file
, " indirect symbols:\n");
3657 for (i
= 0; i
< mdata
->nsects
; i
++)
3659 bfd_mach_o_section
*sec
= mdata
->sections
[i
];
3660 unsigned int j
, first
, last
;
3661 bfd_mach_o_symtab_command
*symtab
= mdata
->symtab
;
3665 switch (sec
->flags
& BFD_MACH_O_SECTION_TYPE_MASK
)
3667 case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS
:
3668 case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS
:
3669 case BFD_MACH_O_S_SYMBOL_STUBS
:
3670 first
= sec
->reserved1
;
3671 last
= first
+ bfd_mach_o_section_get_nbr_indirect (abfd
, sec
);
3673 entry_size
= bfd_mach_o_section_get_entry_size (abfd
, sec
);
3674 fprintf (file
, " for section %s.%s:\n",
3675 sec
->segname
, sec
->sectname
);
3676 for (j
= first
; j
< last
; j
++)
3678 unsigned int isym
= dysymtab
->indirect_syms
[j
];
3680 fprintf (file
, " ");
3681 fprintf_vma (file
, addr
);
3682 fprintf (file
, " %5u: 0x%08x", j
, isym
);
3683 if (isym
& BFD_MACH_O_INDIRECT_SYMBOL_LOCAL
)
3684 fprintf (file
, " LOCAL");
3685 if (isym
& BFD_MACH_O_INDIRECT_SYMBOL_ABS
)
3686 fprintf (file
, " ABSOLUTE");
3687 if (symtab
&& symtab
->symbols
3688 && isym
< symtab
->nsyms
3689 && symtab
->symbols
[isym
].symbol
.name
)
3690 fprintf (file
, " %s", symtab
->symbols
[isym
].symbol
.name
);
3691 fprintf (file
, "\n");
3700 if (dysymtab
->nextrefsyms
> 0)
3702 bfd_mach_o_symtab_command
*symtab
= mdata
->symtab
;
3704 fprintf (file
, " external reference table: (symbol flags)\n");
3705 for (i
= 0; i
< dysymtab
->nextrefsyms
; i
++)
3707 bfd_mach_o_dylib_reference
*ref
= &dysymtab
->ext_refs
[i
];
3709 fprintf (file
, " %4u: %5lu 0x%02lx", i
, ref
->isym
, ref
->flags
);
3710 if (symtab
&& symtab
->symbols
3711 && ref
->isym
< symtab
->nsyms
3712 && symtab
->symbols
[ref
->isym
].symbol
.name
)
3713 fprintf (file
, " %s", symtab
->symbols
[ref
->isym
].symbol
.name
);
3714 fprintf (file
, "\n");
3721 bfd_mach_o_print_dyld_info (bfd
*abfd ATTRIBUTE_UNUSED
,
3722 bfd_mach_o_load_command
*cmd
, FILE *file
)
3724 bfd_mach_o_dyld_info_command
*info
= &cmd
->command
.dyld_info
;
3726 fprintf (file
, " rebase: off: 0x%08x size: %-8u\n",
3727 info
->rebase_off
, info
->rebase_size
);
3728 fprintf (file
, " bind: off: 0x%08x size: %-8u\n",
3729 info
->bind_off
, info
->bind_size
);
3730 fprintf (file
, " weak bind: off: 0x%08x size: %-8u\n",
3731 info
->weak_bind_off
, info
->weak_bind_size
);
3732 fprintf (file
, " lazy bind: off: 0x%08x size: %-8u\n",
3733 info
->lazy_bind_off
, info
->lazy_bind_size
);
3734 fprintf (file
, " export: off: 0x%08x size: %-8u\n",
3735 info
->export_off
, info
->export_size
);
3739 bfd_mach_o_bfd_print_private_bfd_data (bfd
*abfd
, void * ptr
)
3741 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
3742 FILE *file
= (FILE *) ptr
;
3745 bfd_mach_o_print_private_header (abfd
, file
);
3748 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
3750 bfd_mach_o_load_command
*cmd
= &mdata
->commands
[i
];
3752 fprintf (file
, "Load command %s:",
3753 bfd_mach_o_get_name (bfd_mach_o_load_command_name
, cmd
->type
));
3756 case BFD_MACH_O_LC_SEGMENT
:
3757 case BFD_MACH_O_LC_SEGMENT_64
:
3758 bfd_mach_o_print_segment (abfd
, cmd
, file
);
3760 case BFD_MACH_O_LC_UUID
:
3762 bfd_mach_o_uuid_command
*uuid
= &cmd
->command
.uuid
;
3765 for (j
= 0; j
< sizeof (uuid
->uuid
); j
++)
3766 fprintf (file
, " %02x", uuid
->uuid
[j
]);
3770 case BFD_MACH_O_LC_LOAD_DYLIB
:
3771 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB
:
3772 case BFD_MACH_O_LC_REEXPORT_DYLIB
:
3773 case BFD_MACH_O_LC_ID_DYLIB
:
3775 bfd_mach_o_dylib_command
*dylib
= &cmd
->command
.dylib
;
3776 fprintf (file
, " %s\n", dylib
->name_str
);
3777 fprintf (file
, " time stamp: 0x%08lx\n",
3779 fprintf (file
, " current version: 0x%08lx\n",
3780 dylib
->current_version
);
3781 fprintf (file
, " comptibility version: 0x%08lx\n",
3782 dylib
->compatibility_version
);
3785 case BFD_MACH_O_LC_LOAD_DYLINKER
:
3786 case BFD_MACH_O_LC_ID_DYLINKER
:
3787 fprintf (file
, " %s\n", cmd
->command
.dylinker
.name_str
);
3789 case BFD_MACH_O_LC_SYMTAB
:
3791 bfd_mach_o_symtab_command
*symtab
= &cmd
->command
.symtab
;
3794 " symoff: 0x%08x nsyms: %8u (endoff: 0x%08x)\n",
3795 symtab
->symoff
, symtab
->nsyms
,
3796 symtab
->symoff
+ symtab
->nsyms
3797 * (mach_o_wide_p (&mdata
->header
)
3798 ? BFD_MACH_O_NLIST_64_SIZE
: BFD_MACH_O_NLIST_SIZE
));
3800 " stroff: 0x%08x strsize: %8u (endoff: 0x%08x)\n",
3801 symtab
->stroff
, symtab
->strsize
,
3802 symtab
->stroff
+ symtab
->strsize
);
3805 case BFD_MACH_O_LC_DYSYMTAB
:
3806 fprintf (file
, "\n");
3807 bfd_mach_o_print_dysymtab (abfd
, cmd
, file
);
3809 case BFD_MACH_O_LC_CODE_SIGNATURE
:
3810 case BFD_MACH_O_LC_SEGMENT_SPLIT_INFO
:
3812 bfd_mach_o_linkedit_command
*linkedit
= &cmd
->command
.linkedit
;
3815 " dataoff: 0x%08lx datasize: 0x%08lx (endoff: 0x%08lx)\n",
3816 linkedit
->dataoff
, linkedit
->datasize
,
3817 linkedit
->dataoff
+ linkedit
->datasize
);
3820 case BFD_MACH_O_LC_SUB_FRAMEWORK
:
3821 case BFD_MACH_O_LC_SUB_UMBRELLA
:
3822 case BFD_MACH_O_LC_SUB_LIBRARY
:
3823 case BFD_MACH_O_LC_SUB_CLIENT
:
3824 case BFD_MACH_O_LC_RPATH
:
3826 bfd_mach_o_str_command
*str
= &cmd
->command
.str
;
3827 fprintf (file
, " %s\n", str
->str
);
3830 case BFD_MACH_O_LC_THREAD
:
3831 case BFD_MACH_O_LC_UNIXTHREAD
:
3833 bfd_mach_o_thread_command
*thread
= &cmd
->command
.thread
;
3835 bfd_mach_o_backend_data
*bed
= bfd_mach_o_get_backend_data (abfd
);
3837 fprintf (file
, " nflavours: %lu\n", thread
->nflavours
);
3838 for (j
= 0; j
< thread
->nflavours
; j
++)
3840 bfd_mach_o_thread_flavour
*flavour
= &thread
->flavours
[j
];
3842 fprintf (file
, " %2u: flavour: 0x%08lx offset: 0x%08lx"
3844 j
, flavour
->flavour
, flavour
->offset
,
3846 if (bed
->_bfd_mach_o_print_thread
)
3848 char *buf
= bfd_malloc (flavour
->size
);
3851 && bfd_seek (abfd
, flavour
->offset
, SEEK_SET
) == 0
3852 && (bfd_bread (buf
, flavour
->size
, abfd
)
3854 (*bed
->_bfd_mach_o_print_thread
)(abfd
, flavour
,
3861 case BFD_MACH_O_LC_DYLD_INFO
:
3862 fprintf (file
, "\n");
3863 bfd_mach_o_print_dyld_info (abfd
, cmd
, file
);
3866 fprintf (file
, "\n");
3872 bfd_mach_o_print_section_map (abfd
, file
);
3878 bfd_mach_o_core_fetch_environment (bfd
*abfd
,
3879 unsigned char **rbuf
,
3882 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
3883 unsigned long stackaddr
= bfd_mach_o_stack_addr (mdata
->header
.cputype
);
3886 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
3888 bfd_mach_o_load_command
*cur
= &mdata
->commands
[i
];
3889 bfd_mach_o_segment_command
*seg
= NULL
;
3891 if (cur
->type
!= BFD_MACH_O_LC_SEGMENT
)
3894 seg
= &cur
->command
.segment
;
3896 if ((seg
->vmaddr
+ seg
->vmsize
) == stackaddr
)
3898 unsigned long start
= seg
->fileoff
;
3899 unsigned long end
= seg
->fileoff
+ seg
->filesize
;
3900 unsigned char *buf
= bfd_malloc (1024);
3901 unsigned long size
= 1024;
3905 bfd_size_type nread
= 0;
3906 unsigned long offset
;
3907 int found_nonnull
= 0;
3909 if (size
> (end
- start
))
3910 size
= (end
- start
);
3912 buf
= bfd_realloc_or_free (buf
, size
);
3916 if (bfd_seek (abfd
, end
- size
, SEEK_SET
) != 0)
3922 nread
= bfd_bread (buf
, size
, abfd
);
3930 for (offset
= 4; offset
<= size
; offset
+= 4)
3934 val
= *((unsigned long *) (buf
+ size
- offset
));
3935 if (! found_nonnull
)
3940 else if (val
== 0x0)
3942 unsigned long bottom
;
3945 bottom
= seg
->fileoff
+ seg
->filesize
- offset
;
3946 top
= seg
->fileoff
+ seg
->filesize
- 4;
3947 *rbuf
= bfd_malloc (top
- bottom
);
3948 *rlen
= top
- bottom
;
3950 memcpy (*rbuf
, buf
+ size
- *rlen
, *rlen
);
3956 if (size
== (end
- start
))
3970 bfd_mach_o_core_file_failing_command (bfd
*abfd
)
3972 unsigned char *buf
= NULL
;
3973 unsigned int len
= 0;
3976 ret
= bfd_mach_o_core_fetch_environment (abfd
, &buf
, &len
);
3980 return (char *) buf
;
3984 bfd_mach_o_core_file_failing_signal (bfd
*abfd ATTRIBUTE_UNUSED
)
3989 #define bfd_mach_o_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
3990 #define bfd_mach_o_bfd_reloc_name_lookup _bfd_norelocs_bfd_reloc_name_lookup
3992 #define bfd_mach_o_swap_reloc_in NULL
3993 #define bfd_mach_o_swap_reloc_out NULL
3994 #define bfd_mach_o_print_thread NULL
3996 #define TARGET_NAME mach_o_be_vec
3997 #define TARGET_STRING "mach-o-be"
3998 #define TARGET_ARCHITECTURE bfd_arch_unknown
3999 #define TARGET_BIG_ENDIAN 1
4000 #define TARGET_ARCHIVE 0
4001 #include "mach-o-target.c"
4004 #undef TARGET_STRING
4005 #undef TARGET_ARCHITECTURE
4006 #undef TARGET_BIG_ENDIAN
4007 #undef TARGET_ARCHIVE
4009 #define TARGET_NAME mach_o_le_vec
4010 #define TARGET_STRING "mach-o-le"
4011 #define TARGET_ARCHITECTURE bfd_arch_unknown
4012 #define TARGET_BIG_ENDIAN 0
4013 #define TARGET_ARCHIVE 0
4015 #include "mach-o-target.c"
4018 #undef TARGET_STRING
4019 #undef TARGET_ARCHITECTURE
4020 #undef TARGET_BIG_ENDIAN
4021 #undef TARGET_ARCHIVE
4023 #define TARGET_NAME mach_o_fat_vec
4024 #define TARGET_STRING "mach-o-fat"
4025 #define TARGET_ARCHITECTURE bfd_arch_unknown
4026 #define TARGET_BIG_ENDIAN 1
4027 #define TARGET_ARCHIVE 1
4029 #include "mach-o-target.c"
4032 #undef TARGET_STRING
4033 #undef TARGET_ARCHITECTURE
4034 #undef TARGET_BIG_ENDIAN
4035 #undef TARGET_ARCHIVE