1 /* Mach-O support for BFD.
2 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4 Free Software Foundation, Inc.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
27 #include "libiberty.h"
28 #include "aout/stab_gnu.h"
31 #define bfd_mach_o_object_p bfd_mach_o_gen_object_p
32 #define bfd_mach_o_core_p bfd_mach_o_gen_core_p
33 #define bfd_mach_o_mkobject bfd_mach_o_gen_mkobject
35 #define FILE_ALIGN(off, algn) \
36 (((off) + ((file_ptr) 1 << (algn)) - 1) & ((file_ptr) -1 << (algn)))
38 static int bfd_mach_o_read_symtab_symbols (bfd
*);
41 bfd_mach_o_version (bfd
*abfd
)
43 bfd_mach_o_data_struct
*mdata
= NULL
;
45 BFD_ASSERT (bfd_mach_o_valid (abfd
));
46 mdata
= bfd_mach_o_get_data (abfd
);
48 return mdata
->header
.version
;
52 bfd_mach_o_valid (bfd
*abfd
)
54 if (abfd
== NULL
|| abfd
->xvec
== NULL
)
57 if (abfd
->xvec
->flavour
!= bfd_target_mach_o_flavour
)
60 if (bfd_mach_o_get_data (abfd
) == NULL
)
65 static INLINE bfd_boolean
66 mach_o_wide_p (bfd_mach_o_header
*header
)
68 switch (header
->version
)
80 static INLINE bfd_boolean
81 bfd_mach_o_wide_p (bfd
*abfd
)
83 return mach_o_wide_p (&bfd_mach_o_get_data (abfd
)->header
);
86 /* Tables to translate well known Mach-O segment/section names to bfd
87 names. Use of canonical names (such as .text or .debug_frame) is required
90 struct mach_o_section_name_xlat
93 const char *mach_o_name
;
97 static const struct mach_o_section_name_xlat dwarf_section_names_xlat
[] =
99 { ".debug_frame", "__debug_frame", SEC_DEBUGGING
},
100 { ".debug_info", "__debug_info", SEC_DEBUGGING
},
101 { ".debug_abbrev", "__debug_abbrev", SEC_DEBUGGING
},
102 { ".debug_aranges", "__debug_aranges", SEC_DEBUGGING
},
103 { ".debug_macinfo", "__debug_macinfo", SEC_DEBUGGING
},
104 { ".debug_line", "__debug_line", SEC_DEBUGGING
},
105 { ".debug_loc", "__debug_loc", SEC_DEBUGGING
},
106 { ".debug_pubnames", "__debug_pubnames", SEC_DEBUGGING
},
107 { ".debug_pubtypes", "__debug_pubtypes", SEC_DEBUGGING
},
108 { ".debug_str", "__debug_str", SEC_DEBUGGING
},
109 { ".debug_ranges", "__debug_ranges", SEC_DEBUGGING
},
113 static const struct mach_o_section_name_xlat text_section_names_xlat
[] =
115 { ".text", "__text", SEC_CODE
| SEC_LOAD
},
116 { ".const", "__const", SEC_READONLY
| SEC_DATA
| SEC_LOAD
},
117 { ".cstring", "__cstring", SEC_READONLY
| SEC_DATA
| SEC_LOAD
},
118 { ".eh_frame", "__eh_frame", SEC_READONLY
| SEC_LOAD
},
122 static const struct mach_o_section_name_xlat data_section_names_xlat
[] =
124 { ".data", "__data", SEC_DATA
| SEC_LOAD
},
125 { ".const_data", "__const", SEC_DATA
| SEC_LOAD
},
126 { ".dyld", "__dyld", SEC_DATA
| SEC_LOAD
},
127 { ".lazy_symbol_ptr", "__la_symbol_ptr", SEC_DATA
| SEC_LOAD
},
128 { ".non_lazy_symbol_ptr", "__nl_symbol_ptr", SEC_DATA
| SEC_LOAD
},
129 { ".bss", "__bss", SEC_NO_FLAGS
},
133 struct mach_o_segment_name_xlat
136 const struct mach_o_section_name_xlat
*sections
;
139 static const struct mach_o_segment_name_xlat segsec_names_xlat
[] =
141 { "__DWARF", dwarf_section_names_xlat
},
142 { "__TEXT", text_section_names_xlat
},
143 { "__DATA", data_section_names_xlat
},
147 /* Mach-O to bfd names. */
150 bfd_mach_o_normalize_section_name (const char *segname
, const char *sectname
,
151 const char **name
, flagword
*flags
)
153 const struct mach_o_segment_name_xlat
*seg
;
156 *flags
= SEC_NO_FLAGS
;
158 for (seg
= segsec_names_xlat
; seg
->segname
; seg
++)
160 if (strcmp (seg
->segname
, segname
) == 0)
162 const struct mach_o_section_name_xlat
*sec
;
164 for (sec
= seg
->sections
; sec
->mach_o_name
; sec
++)
166 if (strcmp (sec
->mach_o_name
, sectname
) == 0)
168 *name
= sec
->bfd_name
;
179 bfd_mach_o_convert_section_name_to_bfd (bfd
*abfd
, bfd_mach_o_section
*section
,
180 const char **name
, flagword
*flags
)
184 const char *pfx
= "";
186 /* First search for a canonical name. */
187 bfd_mach_o_normalize_section_name (section
->segname
, section
->sectname
,
190 /* Return now if found. */
194 len
= strlen (section
->segname
) + 1
195 + strlen (section
->sectname
) + 1;
197 /* Put "LC_SEGMENT." prefix if the segment name is weird (ie doesn't start
198 with an underscore. */
199 if (section
->segname
[0] != '_')
201 static const char seg_pfx
[] = "LC_SEGMENT.";
204 len
+= sizeof (seg_pfx
) - 1;
207 res
= bfd_alloc (abfd
, len
);
210 snprintf (res
, len
, "%s%s.%s", pfx
, section
->segname
, section
->sectname
);
212 *flags
= SEC_NO_FLAGS
;
215 /* Convert a bfd section name to a Mach-O segment + section name. */
218 bfd_mach_o_convert_section_name_to_mach_o (bfd
*abfd ATTRIBUTE_UNUSED
,
220 bfd_mach_o_section
*section
)
222 const struct mach_o_segment_name_xlat
*seg
;
223 const char *name
= bfd_get_section_name (abfd
, sect
);
229 /* List of well known names. They all start with a dot. */
231 for (seg
= segsec_names_xlat
; seg
->segname
; seg
++)
233 const struct mach_o_section_name_xlat
*sec
;
235 for (sec
= seg
->sections
; sec
->mach_o_name
; sec
++)
237 if (strcmp (sec
->bfd_name
, name
) == 0)
239 strcpy (section
->segname
, seg
->segname
);
240 strcpy (section
->sectname
, sec
->mach_o_name
);
246 /* Strip LC_SEGMENT. prefix. */
247 if (strncmp (name
, "LC_SEGMENT.", 11) == 0)
251 dot
= strchr (name
, '.');
254 /* Try to split name into segment and section names. */
255 if (dot
&& dot
!= name
)
258 seclen
= len
- (dot
+ 1 - name
);
260 if (seglen
< 16 && seclen
< 16)
262 memcpy (section
->segname
, name
, seglen
);
263 section
->segname
[seglen
] = 0;
264 memcpy (section
->sectname
, dot
+ 1, seclen
);
265 section
->sectname
[seclen
] = 0;
272 memcpy (section
->segname
, name
, len
);
273 section
->segname
[len
] = 0;
274 memcpy (section
->sectname
, name
, len
);
275 section
->sectname
[len
] = 0;
278 /* Return the size of an entry for section SEC.
279 Must be called only for symbol pointer section and symbol stubs
283 bfd_mach_o_section_get_entry_size (bfd
*abfd
, bfd_mach_o_section
*sec
)
285 switch (sec
->flags
& BFD_MACH_O_SECTION_TYPE_MASK
)
287 case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS
:
288 case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS
:
289 return bfd_mach_o_wide_p (abfd
) ? 8 : 4;
290 case BFD_MACH_O_S_SYMBOL_STUBS
:
291 return sec
->reserved2
;
298 /* Return the number of indirect symbols for a section.
299 Must be called only for symbol pointer section and symbol stubs
303 bfd_mach_o_section_get_nbr_indirect (bfd
*abfd
, bfd_mach_o_section
*sec
)
307 elsz
= bfd_mach_o_section_get_entry_size (abfd
, sec
);
311 return sec
->size
/ elsz
;
315 /* Copy any private info we understand from the input symbol
316 to the output symbol. */
319 bfd_mach_o_bfd_copy_private_symbol_data (bfd
*ibfd ATTRIBUTE_UNUSED
,
320 asymbol
*isymbol ATTRIBUTE_UNUSED
,
321 bfd
*obfd ATTRIBUTE_UNUSED
,
322 asymbol
*osymbol ATTRIBUTE_UNUSED
)
327 /* Copy any private info we understand from the input section
328 to the output section. */
331 bfd_mach_o_bfd_copy_private_section_data (bfd
*ibfd ATTRIBUTE_UNUSED
,
332 asection
*isection ATTRIBUTE_UNUSED
,
333 bfd
*obfd ATTRIBUTE_UNUSED
,
334 asection
*osection ATTRIBUTE_UNUSED
)
339 /* Copy any private info we understand from the input bfd
340 to the output bfd. */
343 bfd_mach_o_bfd_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
345 if (bfd_get_flavour (ibfd
) != bfd_target_mach_o_flavour
346 || bfd_get_flavour (obfd
) != bfd_target_mach_o_flavour
)
349 BFD_ASSERT (bfd_mach_o_valid (ibfd
));
350 BFD_ASSERT (bfd_mach_o_valid (obfd
));
352 /* FIXME: copy commands. */
357 /* Count the total number of symbols. */
360 bfd_mach_o_count_symbols (bfd
*abfd
)
362 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
364 if (mdata
->symtab
== NULL
)
366 return mdata
->symtab
->nsyms
;
370 bfd_mach_o_get_symtab_upper_bound (bfd
*abfd
)
372 long nsyms
= bfd_mach_o_count_symbols (abfd
);
374 return ((nsyms
+ 1) * sizeof (asymbol
*));
378 bfd_mach_o_canonicalize_symtab (bfd
*abfd
, asymbol
**alocation
)
380 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
381 long nsyms
= bfd_mach_o_count_symbols (abfd
);
382 bfd_mach_o_symtab_command
*sym
= mdata
->symtab
;
390 /* Do not try to read symbols if there are none. */
395 if (bfd_mach_o_read_symtab_symbols (abfd
) != 0)
397 (*_bfd_error_handler
) (_("bfd_mach_o_canonicalize_symtab: unable to load symbols"));
401 BFD_ASSERT (sym
->symbols
!= NULL
);
403 for (j
= 0; j
< sym
->nsyms
; j
++)
404 alocation
[j
] = &sym
->symbols
[j
].symbol
;
412 bfd_mach_o_get_synthetic_symtab (bfd
*abfd
,
413 long symcount ATTRIBUTE_UNUSED
,
414 asymbol
**syms ATTRIBUTE_UNUSED
,
415 long dynsymcount ATTRIBUTE_UNUSED
,
416 asymbol
**dynsyms ATTRIBUTE_UNUSED
,
419 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
420 bfd_mach_o_dysymtab_command
*dysymtab
= mdata
->dysymtab
;
421 bfd_mach_o_symtab_command
*symtab
= mdata
->symtab
;
423 unsigned long count
, i
, j
, n
;
430 if (dysymtab
== NULL
|| symtab
== NULL
|| symtab
->symbols
== NULL
)
433 if (dysymtab
->nindirectsyms
== 0)
436 count
= dysymtab
->nindirectsyms
;
437 size
= count
* sizeof (asymbol
) + 1;
439 for (j
= 0; j
< count
; j
++)
441 unsigned int isym
= dysymtab
->indirect_syms
[j
];
443 if (isym
< symtab
->nsyms
&& symtab
->symbols
[isym
].symbol
.name
)
444 size
+= strlen (symtab
->symbols
[isym
].symbol
.name
) + sizeof ("$stub");
447 s
= *ret
= (asymbol
*) bfd_malloc (size
);
450 names
= (char *) (s
+ count
);
455 for (i
= 0; i
< mdata
->nsects
; i
++)
457 bfd_mach_o_section
*sec
= mdata
->sections
[i
];
458 unsigned int first
, last
;
462 switch (sec
->flags
& BFD_MACH_O_SECTION_TYPE_MASK
)
464 case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS
:
465 case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS
:
466 case BFD_MACH_O_S_SYMBOL_STUBS
:
467 first
= sec
->reserved1
;
468 last
= first
+ bfd_mach_o_section_get_nbr_indirect (abfd
, sec
);
470 entry_size
= bfd_mach_o_section_get_entry_size (abfd
, sec
);
471 for (j
= first
; j
< last
; j
++)
473 unsigned int isym
= dysymtab
->indirect_syms
[j
];
475 s
->flags
= BSF_GLOBAL
| BSF_SYNTHETIC
;
476 s
->section
= sec
->bfdsection
;
477 s
->value
= addr
- sec
->addr
;
480 if (isym
< symtab
->nsyms
481 && symtab
->symbols
[isym
].symbol
.name
)
483 const char *sym
= symtab
->symbols
[isym
].symbol
.name
;
488 memcpy (names
, sym
, len
);
490 memcpy (names
, "$stub", sizeof ("$stub"));
491 names
+= sizeof ("$stub");
510 bfd_mach_o_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
514 bfd_symbol_info (symbol
, ret
);
518 bfd_mach_o_print_symbol (bfd
*abfd
,
521 bfd_print_symbol_type how
)
523 FILE *file
= (FILE *) afile
;
525 bfd_mach_o_asymbol
*asym
= (bfd_mach_o_asymbol
*)symbol
;
529 case bfd_print_symbol_name
:
530 fprintf (file
, "%s", symbol
->name
);
533 bfd_print_symbol_vandf (abfd
, (void *) file
, symbol
);
534 if (asym
->n_type
& BFD_MACH_O_N_STAB
)
535 name
= bfd_get_stab_name (asym
->n_type
);
537 switch (asym
->n_type
& BFD_MACH_O_N_TYPE
)
539 case BFD_MACH_O_N_UNDF
:
542 case BFD_MACH_O_N_ABS
:
545 case BFD_MACH_O_N_INDR
:
548 case BFD_MACH_O_N_PBUD
:
551 case BFD_MACH_O_N_SECT
:
560 fprintf (file
, " %02x %-6s %02x %04x",
561 asym
->n_type
, name
, asym
->n_sect
, asym
->n_desc
);
562 if ((asym
->n_type
& BFD_MACH_O_N_STAB
) == 0
563 && (asym
->n_type
& BFD_MACH_O_N_TYPE
) == BFD_MACH_O_N_SECT
)
564 fprintf (file
, " %-5s", symbol
->section
->name
);
565 fprintf (file
, " %s", symbol
->name
);
570 bfd_mach_o_convert_architecture (bfd_mach_o_cpu_type mtype
,
571 bfd_mach_o_cpu_subtype msubtype ATTRIBUTE_UNUSED
,
572 enum bfd_architecture
*type
,
573 unsigned long *subtype
)
575 *subtype
= bfd_arch_unknown
;
579 case BFD_MACH_O_CPU_TYPE_VAX
: *type
= bfd_arch_vax
; break;
580 case BFD_MACH_O_CPU_TYPE_MC680x0
: *type
= bfd_arch_m68k
; break;
581 case BFD_MACH_O_CPU_TYPE_I386
:
582 *type
= bfd_arch_i386
;
583 *subtype
= bfd_mach_i386_i386
;
585 case BFD_MACH_O_CPU_TYPE_X86_64
:
586 *type
= bfd_arch_i386
;
587 *subtype
= bfd_mach_x86_64
;
589 case BFD_MACH_O_CPU_TYPE_MIPS
: *type
= bfd_arch_mips
; break;
590 case BFD_MACH_O_CPU_TYPE_MC98000
: *type
= bfd_arch_m98k
; break;
591 case BFD_MACH_O_CPU_TYPE_HPPA
: *type
= bfd_arch_hppa
; break;
592 case BFD_MACH_O_CPU_TYPE_ARM
: *type
= bfd_arch_arm
; break;
593 case BFD_MACH_O_CPU_TYPE_MC88000
: *type
= bfd_arch_m88k
; break;
594 case BFD_MACH_O_CPU_TYPE_SPARC
:
595 *type
= bfd_arch_sparc
;
596 *subtype
= bfd_mach_sparc
;
598 case BFD_MACH_O_CPU_TYPE_I860
: *type
= bfd_arch_i860
; break;
599 case BFD_MACH_O_CPU_TYPE_ALPHA
: *type
= bfd_arch_alpha
; break;
600 case BFD_MACH_O_CPU_TYPE_POWERPC
:
601 *type
= bfd_arch_powerpc
;
602 *subtype
= bfd_mach_ppc
;
604 case BFD_MACH_O_CPU_TYPE_POWERPC_64
:
605 *type
= bfd_arch_powerpc
;
606 *subtype
= bfd_mach_ppc64
;
609 *type
= bfd_arch_unknown
;
615 bfd_mach_o_write_header (bfd
*abfd
, bfd_mach_o_header
*header
)
617 unsigned char buf
[32];
620 size
= mach_o_wide_p (header
) ?
621 BFD_MACH_O_HEADER_64_SIZE
: BFD_MACH_O_HEADER_SIZE
;
623 bfd_h_put_32 (abfd
, header
->magic
, buf
+ 0);
624 bfd_h_put_32 (abfd
, header
->cputype
, buf
+ 4);
625 bfd_h_put_32 (abfd
, header
->cpusubtype
, buf
+ 8);
626 bfd_h_put_32 (abfd
, header
->filetype
, buf
+ 12);
627 bfd_h_put_32 (abfd
, header
->ncmds
, buf
+ 16);
628 bfd_h_put_32 (abfd
, header
->sizeofcmds
, buf
+ 20);
629 bfd_h_put_32 (abfd
, header
->flags
, buf
+ 24);
631 if (mach_o_wide_p (header
))
632 bfd_h_put_32 (abfd
, header
->reserved
, buf
+ 28);
634 if (bfd_seek (abfd
, 0, SEEK_SET
) != 0
635 || bfd_bwrite ((void *) buf
, size
, abfd
) != size
)
642 bfd_mach_o_write_thread (bfd
*abfd
, bfd_mach_o_load_command
*command
)
644 bfd_mach_o_thread_command
*cmd
= &command
->command
.thread
;
646 unsigned char buf
[8];
649 BFD_ASSERT ((command
->type
== BFD_MACH_O_LC_THREAD
)
650 || (command
->type
== BFD_MACH_O_LC_UNIXTHREAD
));
653 for (i
= 0; i
< cmd
->nflavours
; i
++)
655 BFD_ASSERT ((cmd
->flavours
[i
].size
% 4) == 0);
656 BFD_ASSERT (cmd
->flavours
[i
].offset
== (command
->offset
+ offset
+ 8));
658 bfd_h_put_32 (abfd
, cmd
->flavours
[i
].flavour
, buf
);
659 bfd_h_put_32 (abfd
, (cmd
->flavours
[i
].size
/ 4), buf
+ 4);
661 if (bfd_seek (abfd
, command
->offset
+ offset
, SEEK_SET
) != 0
662 || bfd_bwrite ((void *) buf
, 8, abfd
) != 8)
665 offset
+= cmd
->flavours
[i
].size
+ 8;
672 bfd_mach_o_get_reloc_upper_bound (bfd
*abfd ATTRIBUTE_UNUSED
,
675 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
679 bfd_mach_o_canonicalize_one_reloc (bfd
*abfd
, char *buf
,
680 arelent
*res
, asymbol
**syms
)
682 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
683 bfd_mach_o_backend_data
*bed
= bfd_mach_o_get_backend_data (abfd
);
684 bfd_mach_o_reloc_info reloc
;
689 addr
= bfd_get_32 (abfd
, buf
+ 0);
690 symnum
= bfd_get_32 (abfd
, buf
+ 4);
692 if (addr
& BFD_MACH_O_SR_SCATTERED
)
696 /* Scattered relocation.
697 Extract section and offset from r_value. */
698 res
->sym_ptr_ptr
= NULL
;
700 for (j
= 0; j
< mdata
->nsects
; j
++)
702 bfd_mach_o_section
*sect
= mdata
->sections
[j
];
703 if (symnum
>= sect
->addr
&& symnum
< sect
->addr
+ sect
->size
)
705 res
->sym_ptr_ptr
= sect
->bfdsection
->symbol_ptr_ptr
;
706 res
->addend
= symnum
- sect
->addr
;
710 res
->address
= BFD_MACH_O_GET_SR_ADDRESS (addr
);
711 reloc
.r_type
= BFD_MACH_O_GET_SR_TYPE (addr
);
712 reloc
.r_length
= BFD_MACH_O_GET_SR_LENGTH (addr
);
713 reloc
.r_pcrel
= addr
& BFD_MACH_O_SR_PCREL
;
714 reloc
.r_scattered
= 1;
718 unsigned int num
= BFD_MACH_O_GET_R_SYMBOLNUM (symnum
);
721 if (symnum
& BFD_MACH_O_R_EXTERN
)
728 BFD_ASSERT (num
!= 0);
729 BFD_ASSERT (num
<= mdata
->nsects
);
730 sym
= mdata
->sections
[num
- 1]->bfdsection
->symbol_ptr_ptr
;
731 /* For a symbol defined in section S, the addend (stored in the
732 binary) contains the address of the section. To comply with
733 bfd conventio, substract the section address.
734 Use the address from the header, so that the user can modify
735 the vma of the section. */
736 res
->addend
= -mdata
->sections
[num
- 1]->addr
;
739 res
->sym_ptr_ptr
= sym
;
740 reloc
.r_type
= BFD_MACH_O_GET_R_TYPE (symnum
);
741 reloc
.r_length
= BFD_MACH_O_GET_R_LENGTH (symnum
);
742 reloc
.r_pcrel
= (symnum
& BFD_MACH_O_R_PCREL
) ? 1 : 0;
743 reloc
.r_scattered
= 0;
746 if (!(*bed
->_bfd_mach_o_swap_reloc_in
)(res
, &reloc
))
752 bfd_mach_o_canonicalize_relocs (bfd
*abfd
, unsigned long filepos
,
754 arelent
*res
, asymbol
**syms
)
758 bfd_size_type native_size
;
760 /* Allocate and read relocs. */
761 native_size
= count
* BFD_MACH_O_RELENT_SIZE
;
762 native_relocs
= bfd_malloc (native_size
);
763 if (native_relocs
== NULL
)
766 if (bfd_seek (abfd
, filepos
, SEEK_SET
) != 0
767 || bfd_bread (native_relocs
, native_size
, abfd
) != native_size
)
770 for (i
= 0; i
< count
; i
++)
772 char *buf
= native_relocs
+ BFD_MACH_O_RELENT_SIZE
* i
;
774 if (bfd_mach_o_canonicalize_one_reloc (abfd
, buf
, &res
[i
], syms
) < 0)
777 free (native_relocs
);
780 free (native_relocs
);
785 bfd_mach_o_canonicalize_reloc (bfd
*abfd
, asection
*asect
,
786 arelent
**rels
, asymbol
**syms
)
788 bfd_mach_o_backend_data
*bed
= bfd_mach_o_get_backend_data (abfd
);
792 if (asect
->reloc_count
== 0)
795 /* No need to go further if we don't know how to read relocs. */
796 if (bed
->_bfd_mach_o_swap_reloc_in
== NULL
)
799 res
= bfd_malloc (asect
->reloc_count
* sizeof (arelent
));
803 if (bfd_mach_o_canonicalize_relocs (abfd
, asect
->rel_filepos
,
804 asect
->reloc_count
, res
, syms
) < 0)
810 for (i
= 0; i
< asect
->reloc_count
; i
++)
813 asect
->relocation
= res
;
819 bfd_mach_o_get_dynamic_reloc_upper_bound (bfd
*abfd
)
821 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
823 if (mdata
->dysymtab
== NULL
)
825 return (mdata
->dysymtab
->nextrel
+ mdata
->dysymtab
->nlocrel
)
826 * sizeof (arelent
*);
830 bfd_mach_o_canonicalize_dynamic_reloc (bfd
*abfd
, arelent
**rels
,
831 struct bfd_symbol
**syms
)
833 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
834 bfd_mach_o_dysymtab_command
*dysymtab
= mdata
->dysymtab
;
835 bfd_mach_o_backend_data
*bed
= bfd_mach_o_get_backend_data (abfd
);
839 if (dysymtab
== NULL
)
841 if (dysymtab
->nextrel
== 0 && dysymtab
->nlocrel
== 0)
844 /* No need to go further if we don't know how to read relocs. */
845 if (bed
->_bfd_mach_o_swap_reloc_in
== NULL
)
848 res
= bfd_malloc ((dysymtab
->nextrel
+ dysymtab
->nlocrel
) * sizeof (arelent
));
852 if (bfd_mach_o_canonicalize_relocs (abfd
, dysymtab
->extreloff
,
853 dysymtab
->nextrel
, res
, syms
) < 0)
859 if (bfd_mach_o_canonicalize_relocs (abfd
, dysymtab
->locreloff
,
861 res
+ dysymtab
->nextrel
, syms
) < 0)
867 for (i
= 0; i
< dysymtab
->nextrel
+ dysymtab
->nlocrel
; i
++)
874 bfd_mach_o_write_relocs (bfd
*abfd
, bfd_mach_o_section
*section
)
876 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
880 bfd_mach_o_backend_data
*bed
= bfd_mach_o_get_backend_data (abfd
);
882 sec
= section
->bfdsection
;
883 if (sec
->reloc_count
== 0)
886 if (bed
->_bfd_mach_o_swap_reloc_out
== NULL
)
889 /* Allocate relocation room. */
890 mdata
->filelen
= FILE_ALIGN(mdata
->filelen
, 2);
891 section
->nreloc
= sec
->reloc_count
;
892 sec
->rel_filepos
= mdata
->filelen
;
893 section
->reloff
= sec
->rel_filepos
;
894 mdata
->filelen
+= sec
->reloc_count
* BFD_MACH_O_RELENT_SIZE
;
896 if (bfd_seek (abfd
, section
->reloff
, SEEK_SET
) != 0)
899 /* Convert and write. */
900 entries
= section
->bfdsection
->orelocation
;
901 for (i
= 0; i
< section
->nreloc
; i
++)
903 arelent
*rel
= entries
[i
];
905 bfd_mach_o_reloc_info info
, *pinfo
= &info
;
907 /* Convert relocation to an intermediate representation. */
908 if (!(*bed
->_bfd_mach_o_swap_reloc_out
) (rel
, pinfo
))
911 /* Lower the relocation info. */
912 if (pinfo
->r_scattered
)
916 v
= BFD_MACH_O_SR_SCATTERED
917 | (pinfo
->r_pcrel
? BFD_MACH_O_SR_PCREL
: 0)
918 | BFD_MACH_O_SET_SR_LENGTH(pinfo
->r_length
)
919 | BFD_MACH_O_SET_SR_TYPE(pinfo
->r_type
)
920 | BFD_MACH_O_SET_SR_ADDRESS(pinfo
->r_address
);
921 bfd_put_32 (abfd
, v
, buf
);
922 bfd_put_32 (abfd
, pinfo
->r_value
, buf
+ 4);
928 bfd_put_32 (abfd
, pinfo
->r_address
, buf
);
929 v
= BFD_MACH_O_SET_R_SYMBOLNUM (pinfo
->r_value
)
930 | (pinfo
->r_pcrel
? BFD_MACH_O_R_PCREL
: 0)
931 | BFD_MACH_O_SET_R_LENGTH (pinfo
->r_length
)
932 | (pinfo
->r_extern
? BFD_MACH_O_R_EXTERN
: 0)
933 | BFD_MACH_O_SET_R_TYPE (pinfo
->r_type
);
934 bfd_put_32 (abfd
, v
, buf
+ 4);
937 if (bfd_bwrite ((void *) buf
, BFD_MACH_O_RELENT_SIZE
, abfd
)
938 != BFD_MACH_O_RELENT_SIZE
)
945 bfd_mach_o_write_section_32 (bfd
*abfd
, bfd_mach_o_section
*section
)
947 unsigned char buf
[BFD_MACH_O_SECTION_SIZE
];
949 memcpy (buf
, section
->sectname
, 16);
950 memcpy (buf
+ 16, section
->segname
, 16);
951 bfd_h_put_32 (abfd
, section
->addr
, buf
+ 32);
952 bfd_h_put_32 (abfd
, section
->size
, buf
+ 36);
953 bfd_h_put_32 (abfd
, section
->offset
, buf
+ 40);
954 bfd_h_put_32 (abfd
, section
->align
, buf
+ 44);
955 bfd_h_put_32 (abfd
, section
->reloff
, buf
+ 48);
956 bfd_h_put_32 (abfd
, section
->nreloc
, buf
+ 52);
957 bfd_h_put_32 (abfd
, section
->flags
, buf
+ 56);
958 bfd_h_put_32 (abfd
, section
->reserved1
, buf
+ 60);
959 bfd_h_put_32 (abfd
, section
->reserved2
, buf
+ 64);
961 if (bfd_bwrite ((void *) buf
, BFD_MACH_O_SECTION_SIZE
, abfd
)
962 != BFD_MACH_O_SECTION_SIZE
)
969 bfd_mach_o_write_section_64 (bfd
*abfd
, bfd_mach_o_section
*section
)
971 unsigned char buf
[BFD_MACH_O_SECTION_64_SIZE
];
973 memcpy (buf
, section
->sectname
, 16);
974 memcpy (buf
+ 16, section
->segname
, 16);
975 bfd_h_put_64 (abfd
, section
->addr
, buf
+ 32);
976 bfd_h_put_64 (abfd
, section
->size
, buf
+ 40);
977 bfd_h_put_32 (abfd
, section
->offset
, buf
+ 48);
978 bfd_h_put_32 (abfd
, section
->align
, buf
+ 52);
979 bfd_h_put_32 (abfd
, section
->reloff
, buf
+ 56);
980 bfd_h_put_32 (abfd
, section
->nreloc
, buf
+ 60);
981 bfd_h_put_32 (abfd
, section
->flags
, buf
+ 64);
982 bfd_h_put_32 (abfd
, section
->reserved1
, buf
+ 68);
983 bfd_h_put_32 (abfd
, section
->reserved2
, buf
+ 72);
984 bfd_h_put_32 (abfd
, section
->reserved3
, buf
+ 76);
986 if (bfd_bwrite ((void *) buf
, BFD_MACH_O_SECTION_64_SIZE
, abfd
)
987 != BFD_MACH_O_SECTION_64_SIZE
)
994 bfd_mach_o_write_segment_32 (bfd
*abfd
, bfd_mach_o_load_command
*command
)
996 unsigned char buf
[BFD_MACH_O_LC_SEGMENT_SIZE
];
997 bfd_mach_o_segment_command
*seg
= &command
->command
.segment
;
1000 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SEGMENT
);
1002 for (i
= 0; i
< seg
->nsects
; i
++)
1003 if (!bfd_mach_o_write_relocs (abfd
, &seg
->sections
[i
]))
1006 memcpy (buf
, seg
->segname
, 16);
1007 bfd_h_put_32 (abfd
, seg
->vmaddr
, buf
+ 16);
1008 bfd_h_put_32 (abfd
, seg
->vmsize
, buf
+ 20);
1009 bfd_h_put_32 (abfd
, seg
->fileoff
, buf
+ 24);
1010 bfd_h_put_32 (abfd
, seg
->filesize
, buf
+ 28);
1011 bfd_h_put_32 (abfd
, seg
->maxprot
, buf
+ 32);
1012 bfd_h_put_32 (abfd
, seg
->initprot
, buf
+ 36);
1013 bfd_h_put_32 (abfd
, seg
->nsects
, buf
+ 40);
1014 bfd_h_put_32 (abfd
, seg
->flags
, buf
+ 44);
1016 if (bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
) != 0
1017 || (bfd_bwrite ((void *) buf
, BFD_MACH_O_LC_SEGMENT_SIZE
- 8, abfd
)
1018 != BFD_MACH_O_LC_SEGMENT_SIZE
- 8))
1021 for (i
= 0; i
< seg
->nsects
; i
++)
1022 if (bfd_mach_o_write_section_32 (abfd
, &seg
->sections
[i
]))
1029 bfd_mach_o_write_segment_64 (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1031 unsigned char buf
[BFD_MACH_O_LC_SEGMENT_64_SIZE
];
1032 bfd_mach_o_segment_command
*seg
= &command
->command
.segment
;
1035 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SEGMENT_64
);
1037 for (i
= 0; i
< seg
->nsects
; i
++)
1038 if (!bfd_mach_o_write_relocs (abfd
, &seg
->sections
[i
]))
1041 memcpy (buf
, seg
->segname
, 16);
1042 bfd_h_put_64 (abfd
, seg
->vmaddr
, buf
+ 16);
1043 bfd_h_put_64 (abfd
, seg
->vmsize
, buf
+ 24);
1044 bfd_h_put_64 (abfd
, seg
->fileoff
, buf
+ 32);
1045 bfd_h_put_64 (abfd
, seg
->filesize
, buf
+ 40);
1046 bfd_h_put_32 (abfd
, seg
->maxprot
, buf
+ 48);
1047 bfd_h_put_32 (abfd
, seg
->initprot
, buf
+ 52);
1048 bfd_h_put_32 (abfd
, seg
->nsects
, buf
+ 56);
1049 bfd_h_put_32 (abfd
, seg
->flags
, buf
+ 60);
1051 if (bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
) != 0
1052 || (bfd_bwrite ((void *) buf
, BFD_MACH_O_LC_SEGMENT_64_SIZE
- 8, abfd
)
1053 != BFD_MACH_O_LC_SEGMENT_64_SIZE
- 8))
1056 for (i
= 0; i
< seg
->nsects
; i
++)
1057 if (bfd_mach_o_write_section_64 (abfd
, &seg
->sections
[i
]))
1064 bfd_mach_o_write_symtab (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1066 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
1067 bfd_mach_o_symtab_command
*sym
= &command
->command
.symtab
;
1068 unsigned char buf
[16];
1070 unsigned int wide
= bfd_mach_o_wide_p (abfd
);
1071 unsigned int symlen
= wide
? BFD_MACH_O_NLIST_64_SIZE
: BFD_MACH_O_NLIST_SIZE
;
1072 struct bfd_strtab_hash
*strtab
;
1073 asymbol
**symbols
= bfd_get_outsymbols (abfd
);
1075 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SYMTAB
);
1077 /* Write the symbols first. */
1078 mdata
->filelen
= FILE_ALIGN(mdata
->filelen
, wide
? 3 : 2);
1079 sym
->symoff
= mdata
->filelen
;
1080 if (bfd_seek (abfd
, sym
->symoff
, SEEK_SET
) != 0)
1083 sym
->nsyms
= bfd_get_symcount (abfd
);
1084 mdata
->filelen
+= sym
->nsyms
* symlen
;
1086 strtab
= _bfd_stringtab_init ();
1090 for (i
= 0; i
< sym
->nsyms
; i
++)
1092 bfd_size_type str_index
;
1093 bfd_mach_o_asymbol
*s
= (bfd_mach_o_asymbol
*)symbols
[i
];
1095 /* Compute name index. */
1096 /* An index of 0 always means the empty string. */
1097 if (s
->symbol
.name
== 0 || s
->symbol
.name
[0] == '\0')
1101 str_index
= _bfd_stringtab_add (strtab
, s
->symbol
.name
, TRUE
, FALSE
);
1102 if (str_index
== (bfd_size_type
) -1)
1105 bfd_h_put_32 (abfd
, str_index
, buf
);
1106 bfd_h_put_8 (abfd
, s
->n_type
, buf
+ 4);
1107 bfd_h_put_8 (abfd
, s
->n_sect
, buf
+ 5);
1108 bfd_h_put_16 (abfd
, s
->n_desc
, buf
+ 6);
1110 bfd_h_put_64 (abfd
, s
->symbol
.section
->vma
+ s
->symbol
.value
, buf
+ 8);
1112 bfd_h_put_32 (abfd
, s
->symbol
.section
->vma
+ s
->symbol
.value
, buf
+ 8);
1114 if (bfd_bwrite ((void *) buf
, symlen
, abfd
) != symlen
)
1117 sym
->strsize
= _bfd_stringtab_size (strtab
);
1118 sym
->stroff
= mdata
->filelen
;
1119 mdata
->filelen
+= sym
->strsize
;
1121 if (_bfd_stringtab_emit (abfd
, strtab
) != TRUE
)
1123 _bfd_stringtab_free (strtab
);
1126 bfd_h_put_32 (abfd
, sym
->symoff
, buf
);
1127 bfd_h_put_32 (abfd
, sym
->nsyms
, buf
+ 4);
1128 bfd_h_put_32 (abfd
, sym
->stroff
, buf
+ 8);
1129 bfd_h_put_32 (abfd
, sym
->strsize
, buf
+ 12);
1131 if (bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
) != 0
1132 || bfd_bwrite ((void *) buf
, 16, abfd
) != 16)
1138 _bfd_stringtab_free (strtab
);
1142 /* Process the symbols and generate Mach-O specific fields.
1146 bfd_mach_o_mangle_symbols (bfd
*abfd
)
1149 asymbol
**symbols
= bfd_get_outsymbols (abfd
);
1151 for (i
= 0; i
< bfd_get_symcount (abfd
); i
++)
1153 bfd_mach_o_asymbol
*s
= (bfd_mach_o_asymbol
*)symbols
[i
];
1155 if (s
->n_type
== BFD_MACH_O_N_UNDF
&& !(s
->symbol
.flags
& BSF_DEBUGGING
))
1157 /* As genuine Mach-O symbols type shouldn't be N_UNDF (undefined
1158 symbols should be N_UNDEF | N_EXT), we suppose the back-end
1159 values haven't been set. */
1160 if (s
->symbol
.section
== bfd_abs_section_ptr
)
1161 s
->n_type
= BFD_MACH_O_N_ABS
;
1162 else if (s
->symbol
.section
== bfd_und_section_ptr
)
1164 s
->n_type
= BFD_MACH_O_N_UNDF
;
1165 if (s
->symbol
.flags
& BSF_WEAK
)
1166 s
->n_desc
|= BFD_MACH_O_N_WEAK_REF
;
1168 else if (s
->symbol
.section
== bfd_com_section_ptr
)
1169 s
->n_type
= BFD_MACH_O_N_UNDF
| BFD_MACH_O_N_EXT
;
1171 s
->n_type
= BFD_MACH_O_N_SECT
;
1173 if (s
->symbol
.flags
& BSF_GLOBAL
)
1174 s
->n_type
|= BFD_MACH_O_N_EXT
;
1177 /* Compute section index. */
1178 if (s
->symbol
.section
!= bfd_abs_section_ptr
1179 && s
->symbol
.section
!= bfd_und_section_ptr
1180 && s
->symbol
.section
!= bfd_com_section_ptr
)
1181 s
->n_sect
= s
->symbol
.section
->target_index
;
1183 /* Number symbols. */
1184 s
->symbol
.udata
.i
= i
;
1190 bfd_mach_o_write_contents (bfd
*abfd
)
1193 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
1195 if (mdata
->header
.ncmds
== 0)
1196 if (!bfd_mach_o_build_commands (abfd
))
1199 /* Now write header information. */
1200 if (mdata
->header
.filetype
== 0)
1202 if (abfd
->flags
& EXEC_P
)
1203 mdata
->header
.filetype
= BFD_MACH_O_MH_EXECUTE
;
1204 else if (abfd
->flags
& DYNAMIC
)
1205 mdata
->header
.filetype
= BFD_MACH_O_MH_DYLIB
;
1207 mdata
->header
.filetype
= BFD_MACH_O_MH_OBJECT
;
1209 if (!bfd_mach_o_write_header (abfd
, &mdata
->header
))
1212 /* Assign a number to each symbols. */
1213 if (!bfd_mach_o_mangle_symbols (abfd
))
1216 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
1218 unsigned char buf
[8];
1219 bfd_mach_o_load_command
*cur
= &mdata
->commands
[i
];
1220 unsigned long typeflag
;
1222 typeflag
= cur
->type
| (cur
->type_required
? BFD_MACH_O_LC_REQ_DYLD
: 0);
1224 bfd_h_put_32 (abfd
, typeflag
, buf
);
1225 bfd_h_put_32 (abfd
, cur
->len
, buf
+ 4);
1227 if (bfd_seek (abfd
, cur
->offset
, SEEK_SET
) != 0
1228 || bfd_bwrite ((void *) buf
, 8, abfd
) != 8)
1233 case BFD_MACH_O_LC_SEGMENT
:
1234 if (bfd_mach_o_write_segment_32 (abfd
, cur
) != 0)
1237 case BFD_MACH_O_LC_SEGMENT_64
:
1238 if (bfd_mach_o_write_segment_64 (abfd
, cur
) != 0)
1241 case BFD_MACH_O_LC_SYMTAB
:
1242 if (!bfd_mach_o_write_symtab (abfd
, cur
))
1245 case BFD_MACH_O_LC_SYMSEG
:
1247 case BFD_MACH_O_LC_THREAD
:
1248 case BFD_MACH_O_LC_UNIXTHREAD
:
1249 if (bfd_mach_o_write_thread (abfd
, cur
) != 0)
1252 case BFD_MACH_O_LC_LOADFVMLIB
:
1253 case BFD_MACH_O_LC_IDFVMLIB
:
1254 case BFD_MACH_O_LC_IDENT
:
1255 case BFD_MACH_O_LC_FVMFILE
:
1256 case BFD_MACH_O_LC_PREPAGE
:
1257 case BFD_MACH_O_LC_DYSYMTAB
:
1258 case BFD_MACH_O_LC_LOAD_DYLIB
:
1259 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB
:
1260 case BFD_MACH_O_LC_ID_DYLIB
:
1261 case BFD_MACH_O_LC_REEXPORT_DYLIB
:
1262 case BFD_MACH_O_LC_LOAD_DYLINKER
:
1263 case BFD_MACH_O_LC_ID_DYLINKER
:
1264 case BFD_MACH_O_LC_PREBOUND_DYLIB
:
1265 case BFD_MACH_O_LC_ROUTINES
:
1266 case BFD_MACH_O_LC_SUB_FRAMEWORK
:
1269 (*_bfd_error_handler
) (_("unable to write unknown load command 0x%lx"),
1270 (unsigned long) cur
->type
);
1278 /* Build Mach-O load commands from the sections. */
1281 bfd_mach_o_build_commands (bfd
*abfd
)
1283 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
1284 unsigned int wide
= mach_o_wide_p (&mdata
->header
);
1285 bfd_mach_o_segment_command
*seg
;
1286 bfd_mach_o_section
*sections
;
1288 bfd_mach_o_load_command
*cmd
;
1289 bfd_mach_o_load_command
*symtab_cmd
;
1292 /* Return now if commands are already built. */
1293 if (mdata
->header
.ncmds
)
1296 /* Very simple version: 1 command (segment) containing all sections. */
1297 mdata
->header
.ncmds
= 2;
1298 mdata
->commands
= bfd_alloc (abfd
, mdata
->header
.ncmds
1299 * sizeof (bfd_mach_o_load_command
));
1300 if (mdata
->commands
== NULL
)
1302 cmd
= &mdata
->commands
[0];
1303 seg
= &cmd
->command
.segment
;
1305 seg
->nsects
= bfd_count_sections (abfd
);
1306 sections
= bfd_alloc (abfd
, seg
->nsects
* sizeof (bfd_mach_o_section
));
1307 if (sections
== NULL
)
1309 seg
->sections
= sections
;
1311 /* Set segment command. */
1314 cmd
->type
= BFD_MACH_O_LC_SEGMENT_64
;
1315 cmd
->offset
= BFD_MACH_O_HEADER_64_SIZE
;
1316 cmd
->len
= BFD_MACH_O_LC_SEGMENT_64_SIZE
1317 + BFD_MACH_O_SECTION_64_SIZE
* seg
->nsects
;
1321 cmd
->type
= BFD_MACH_O_LC_SEGMENT
;
1322 cmd
->offset
= BFD_MACH_O_HEADER_SIZE
;
1323 cmd
->len
= BFD_MACH_O_LC_SEGMENT_SIZE
1324 + BFD_MACH_O_SECTION_SIZE
* seg
->nsects
;
1326 cmd
->type_required
= FALSE
;
1327 mdata
->header
.sizeofcmds
= cmd
->len
;
1328 mdata
->filelen
= cmd
->offset
+ cmd
->len
;
1330 /* Set symtab command. */
1331 symtab_cmd
= &mdata
->commands
[1];
1333 symtab_cmd
->type
= BFD_MACH_O_LC_SYMTAB
;
1334 symtab_cmd
->offset
= cmd
->offset
+ cmd
->len
;
1335 symtab_cmd
->len
= 6 * 4;
1336 symtab_cmd
->type_required
= FALSE
;
1338 mdata
->header
.sizeofcmds
+= symtab_cmd
->len
;
1339 mdata
->filelen
+= symtab_cmd
->len
;
1341 /* Fill segment command. */
1342 memset (seg
->segname
, 0, sizeof (seg
->segname
));
1344 seg
->fileoff
= mdata
->filelen
;
1346 seg
->maxprot
= BFD_MACH_O_PROT_READ
| BFD_MACH_O_PROT_WRITE
1347 | BFD_MACH_O_PROT_EXECUTE
;
1348 seg
->initprot
= seg
->maxprot
;
1351 /* Create Mach-O sections. */
1353 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1355 sections
->bfdsection
= sec
;
1356 bfd_mach_o_convert_section_name_to_mach_o (abfd
, sec
, sections
);
1357 sections
->addr
= bfd_get_section_vma (abfd
, sec
);
1358 sections
->size
= bfd_get_section_size (sec
);
1359 sections
->align
= bfd_get_section_alignment (abfd
, sec
);
1361 if (sections
->size
!= 0)
1363 mdata
->filelen
= FILE_ALIGN (mdata
->filelen
, sections
->align
);
1364 sections
->offset
= mdata
->filelen
;
1367 sections
->offset
= 0;
1368 sections
->reloff
= 0;
1369 sections
->nreloc
= 0;
1370 sections
->reserved1
= 0;
1371 sections
->reserved2
= 0;
1372 sections
->reserved3
= 0;
1374 sec
->filepos
= sections
->offset
;
1375 sec
->target_index
= ++target_index
;
1377 mdata
->filelen
+= sections
->size
;
1380 seg
->filesize
= mdata
->filelen
- seg
->fileoff
;
1381 seg
->vmsize
= seg
->filesize
;
1386 /* Set the contents of a section. */
1389 bfd_mach_o_set_section_contents (bfd
*abfd
,
1391 const void * location
,
1393 bfd_size_type count
)
1397 /* This must be done first, because bfd_set_section_contents is
1398 going to set output_has_begun to TRUE. */
1399 if (! abfd
->output_has_begun
&& ! bfd_mach_o_build_commands (abfd
))
1405 pos
= section
->filepos
+ offset
;
1406 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
1407 || bfd_bwrite (location
, count
, abfd
) != count
)
1414 bfd_mach_o_sizeof_headers (bfd
*a ATTRIBUTE_UNUSED
,
1415 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
1420 /* Make an empty symbol. This is required only because
1421 bfd_make_section_anyway wants to create a symbol for the section. */
1424 bfd_mach_o_make_empty_symbol (bfd
*abfd
)
1426 asymbol
*new_symbol
;
1428 new_symbol
= bfd_zalloc (abfd
, sizeof (bfd_mach_o_asymbol
));
1429 if (new_symbol
== NULL
)
1431 new_symbol
->the_bfd
= abfd
;
1432 new_symbol
->udata
.i
= 0;
1437 bfd_mach_o_read_header (bfd
*abfd
, bfd_mach_o_header
*header
)
1439 unsigned char buf
[32];
1441 bfd_vma (*get32
) (const void *) = NULL
;
1443 /* Just read the magic number. */
1444 if (bfd_seek (abfd
, 0, SEEK_SET
) != 0
1445 || bfd_bread ((void *) buf
, 4, abfd
) != 4)
1448 if (bfd_getb32 (buf
) == BFD_MACH_O_MH_MAGIC
)
1450 header
->byteorder
= BFD_ENDIAN_BIG
;
1451 header
->magic
= BFD_MACH_O_MH_MAGIC
;
1452 header
->version
= 1;
1455 else if (bfd_getl32 (buf
) == BFD_MACH_O_MH_MAGIC
)
1457 header
->byteorder
= BFD_ENDIAN_LITTLE
;
1458 header
->magic
= BFD_MACH_O_MH_MAGIC
;
1459 header
->version
= 1;
1462 else if (bfd_getb32 (buf
) == BFD_MACH_O_MH_MAGIC_64
)
1464 header
->byteorder
= BFD_ENDIAN_BIG
;
1465 header
->magic
= BFD_MACH_O_MH_MAGIC_64
;
1466 header
->version
= 2;
1469 else if (bfd_getl32 (buf
) == BFD_MACH_O_MH_MAGIC_64
)
1471 header
->byteorder
= BFD_ENDIAN_LITTLE
;
1472 header
->magic
= BFD_MACH_O_MH_MAGIC_64
;
1473 header
->version
= 2;
1478 header
->byteorder
= BFD_ENDIAN_UNKNOWN
;
1482 /* Once the size of the header is known, read the full header. */
1483 size
= mach_o_wide_p (header
) ?
1484 BFD_MACH_O_HEADER_64_SIZE
: BFD_MACH_O_HEADER_SIZE
;
1486 if (bfd_seek (abfd
, 0, SEEK_SET
) != 0
1487 || bfd_bread ((void *) buf
, size
, abfd
) != size
)
1490 header
->cputype
= (*get32
) (buf
+ 4);
1491 header
->cpusubtype
= (*get32
) (buf
+ 8);
1492 header
->filetype
= (*get32
) (buf
+ 12);
1493 header
->ncmds
= (*get32
) (buf
+ 16);
1494 header
->sizeofcmds
= (*get32
) (buf
+ 20);
1495 header
->flags
= (*get32
) (buf
+ 24);
1497 if (mach_o_wide_p (header
))
1498 header
->reserved
= (*get32
) (buf
+ 28);
1504 bfd_mach_o_make_bfd_section (bfd
*abfd
, bfd_mach_o_section
*section
,
1511 bfd_mach_o_convert_section_name_to_bfd (abfd
, section
, &sname
, &flags
);
1515 if (flags
== SEC_NO_FLAGS
)
1517 /* Try to guess flags. */
1518 if (section
->flags
& BFD_MACH_O_S_ATTR_DEBUG
)
1519 flags
= SEC_DEBUGGING
;
1523 if ((section
->flags
& BFD_MACH_O_SECTION_TYPE_MASK
)
1524 != BFD_MACH_O_S_ZEROFILL
)
1527 if (prot
& BFD_MACH_O_PROT_EXECUTE
)
1529 if (prot
& BFD_MACH_O_PROT_WRITE
)
1531 else if (prot
& BFD_MACH_O_PROT_READ
)
1532 flags
|= SEC_READONLY
;
1538 if ((flags
& SEC_DEBUGGING
) == 0)
1542 if (section
->offset
!= 0)
1543 flags
|= SEC_HAS_CONTENTS
;
1544 if (section
->nreloc
!= 0)
1547 bfdsec
= bfd_make_section_anyway_with_flags (abfd
, sname
, flags
);
1551 bfdsec
->vma
= section
->addr
;
1552 bfdsec
->lma
= section
->addr
;
1553 bfdsec
->size
= section
->size
;
1554 bfdsec
->filepos
= section
->offset
;
1555 bfdsec
->alignment_power
= section
->align
;
1556 bfdsec
->segment_mark
= 0;
1557 bfdsec
->reloc_count
= section
->nreloc
;
1558 bfdsec
->rel_filepos
= section
->reloff
;
1564 bfd_mach_o_read_section_32 (bfd
*abfd
,
1565 bfd_mach_o_section
*section
,
1566 unsigned int offset
,
1569 unsigned char buf
[BFD_MACH_O_SECTION_SIZE
];
1571 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0
1572 || (bfd_bread ((void *) buf
, BFD_MACH_O_SECTION_SIZE
, abfd
)
1573 != BFD_MACH_O_SECTION_SIZE
))
1576 memcpy (section
->sectname
, buf
, 16);
1577 section
->sectname
[16] = '\0';
1578 memcpy (section
->segname
, buf
+ 16, 16);
1579 section
->segname
[16] = '\0';
1580 section
->addr
= bfd_h_get_32 (abfd
, buf
+ 32);
1581 section
->size
= bfd_h_get_32 (abfd
, buf
+ 36);
1582 section
->offset
= bfd_h_get_32 (abfd
, buf
+ 40);
1583 section
->align
= bfd_h_get_32 (abfd
, buf
+ 44);
1584 section
->reloff
= bfd_h_get_32 (abfd
, buf
+ 48);
1585 section
->nreloc
= bfd_h_get_32 (abfd
, buf
+ 52);
1586 section
->flags
= bfd_h_get_32 (abfd
, buf
+ 56);
1587 section
->reserved1
= bfd_h_get_32 (abfd
, buf
+ 60);
1588 section
->reserved2
= bfd_h_get_32 (abfd
, buf
+ 64);
1589 section
->reserved3
= 0;
1590 section
->bfdsection
= bfd_mach_o_make_bfd_section (abfd
, section
, prot
);
1592 if (section
->bfdsection
== NULL
)
1599 bfd_mach_o_read_section_64 (bfd
*abfd
,
1600 bfd_mach_o_section
*section
,
1601 unsigned int offset
,
1604 unsigned char buf
[BFD_MACH_O_SECTION_64_SIZE
];
1606 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0
1607 || (bfd_bread ((void *) buf
, BFD_MACH_O_SECTION_64_SIZE
, abfd
)
1608 != BFD_MACH_O_SECTION_64_SIZE
))
1611 memcpy (section
->sectname
, buf
, 16);
1612 section
->sectname
[16] = '\0';
1613 memcpy (section
->segname
, buf
+ 16, 16);
1614 section
->segname
[16] = '\0';
1615 section
->addr
= bfd_h_get_64 (abfd
, buf
+ 32);
1616 section
->size
= bfd_h_get_64 (abfd
, buf
+ 40);
1617 section
->offset
= bfd_h_get_32 (abfd
, buf
+ 48);
1618 section
->align
= bfd_h_get_32 (abfd
, buf
+ 52);
1619 section
->reloff
= bfd_h_get_32 (abfd
, buf
+ 56);
1620 section
->nreloc
= bfd_h_get_32 (abfd
, buf
+ 60);
1621 section
->flags
= bfd_h_get_32 (abfd
, buf
+ 64);
1622 section
->reserved1
= bfd_h_get_32 (abfd
, buf
+ 68);
1623 section
->reserved2
= bfd_h_get_32 (abfd
, buf
+ 72);
1624 section
->reserved3
= bfd_h_get_32 (abfd
, buf
+ 76);
1625 section
->bfdsection
= bfd_mach_o_make_bfd_section (abfd
, section
, prot
);
1627 if (section
->bfdsection
== NULL
)
1634 bfd_mach_o_read_section (bfd
*abfd
,
1635 bfd_mach_o_section
*section
,
1636 unsigned int offset
,
1641 return bfd_mach_o_read_section_64 (abfd
, section
, offset
, prot
);
1643 return bfd_mach_o_read_section_32 (abfd
, section
, offset
, prot
);
1647 bfd_mach_o_read_symtab_symbol (bfd
*abfd
,
1648 bfd_mach_o_symtab_command
*sym
,
1649 bfd_mach_o_asymbol
*s
,
1652 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
1653 unsigned int wide
= mach_o_wide_p (&mdata
->header
);
1654 unsigned int symwidth
=
1655 wide
? BFD_MACH_O_NLIST_64_SIZE
: BFD_MACH_O_NLIST_SIZE
;
1656 unsigned int symoff
= sym
->symoff
+ (i
* symwidth
);
1657 unsigned char buf
[16];
1658 unsigned char type
= -1;
1659 unsigned char section
= -1;
1661 symvalue value
= -1;
1662 unsigned long stroff
= -1;
1663 unsigned int symtype
= -1;
1665 BFD_ASSERT (sym
->strtab
!= NULL
);
1667 if (bfd_seek (abfd
, symoff
, SEEK_SET
) != 0
1668 || bfd_bread ((void *) buf
, symwidth
, abfd
) != symwidth
)
1670 (*_bfd_error_handler
) (_("bfd_mach_o_read_symtab_symbol: unable to read %d bytes at %lu"),
1671 symwidth
, (unsigned long) symoff
);
1675 stroff
= bfd_h_get_32 (abfd
, buf
);
1676 type
= bfd_h_get_8 (abfd
, buf
+ 4);
1677 symtype
= type
& BFD_MACH_O_N_TYPE
;
1678 section
= bfd_h_get_8 (abfd
, buf
+ 5);
1679 desc
= bfd_h_get_16 (abfd
, buf
+ 6);
1681 value
= bfd_h_get_64 (abfd
, buf
+ 8);
1683 value
= bfd_h_get_32 (abfd
, buf
+ 8);
1685 if (stroff
>= sym
->strsize
)
1687 (*_bfd_error_handler
) (_("bfd_mach_o_read_symtab_symbol: symbol name out of range (%lu >= %lu)"),
1688 (unsigned long) stroff
,
1689 (unsigned long) sym
->strsize
);
1693 s
->symbol
.the_bfd
= abfd
;
1694 s
->symbol
.name
= sym
->strtab
+ stroff
;
1695 s
->symbol
.value
= value
;
1696 s
->symbol
.flags
= 0x0;
1697 s
->symbol
.udata
.i
= 0;
1699 s
->n_sect
= section
;
1702 if (type
& BFD_MACH_O_N_STAB
)
1704 s
->symbol
.flags
|= BSF_DEBUGGING
;
1705 s
->symbol
.section
= bfd_und_section_ptr
;
1717 if ((section
> 0) && (section
<= mdata
->nsects
))
1719 s
->symbol
.section
= mdata
->sections
[section
- 1]->bfdsection
;
1721 s
->symbol
.value
- mdata
->sections
[section
- 1]->addr
;
1728 if (type
& BFD_MACH_O_N_PEXT
)
1729 s
->symbol
.flags
|= BSF_GLOBAL
;
1731 if (type
& BFD_MACH_O_N_EXT
)
1732 s
->symbol
.flags
|= BSF_GLOBAL
;
1734 if (!(type
& (BFD_MACH_O_N_PEXT
| BFD_MACH_O_N_EXT
)))
1735 s
->symbol
.flags
|= BSF_LOCAL
;
1739 case BFD_MACH_O_N_UNDF
:
1740 if (type
== (BFD_MACH_O_N_UNDF
| BFD_MACH_O_N_EXT
)
1741 && s
->symbol
.value
!= 0)
1743 /* A common symbol. */
1744 s
->symbol
.section
= bfd_com_section_ptr
;
1745 s
->symbol
.flags
= BSF_NO_FLAGS
;
1749 s
->symbol
.section
= bfd_und_section_ptr
;
1750 if (s
->n_desc
& BFD_MACH_O_N_WEAK_REF
)
1751 s
->symbol
.flags
|= BSF_WEAK
;
1754 case BFD_MACH_O_N_PBUD
:
1755 s
->symbol
.section
= bfd_und_section_ptr
;
1757 case BFD_MACH_O_N_ABS
:
1758 s
->symbol
.section
= bfd_abs_section_ptr
;
1760 case BFD_MACH_O_N_SECT
:
1761 if ((section
> 0) && (section
<= mdata
->nsects
))
1763 s
->symbol
.section
= mdata
->sections
[section
- 1]->bfdsection
;
1765 s
->symbol
.value
- mdata
->sections
[section
- 1]->addr
;
1769 /* Mach-O uses 0 to mean "no section"; not an error. */
1772 (*_bfd_error_handler
) (_("bfd_mach_o_read_symtab_symbol: "
1773 "symbol \"%s\" specified invalid section %d (max %lu): setting to undefined"),
1774 s
->symbol
.name
, section
, mdata
->nsects
);
1776 s
->symbol
.section
= bfd_und_section_ptr
;
1779 case BFD_MACH_O_N_INDR
:
1780 (*_bfd_error_handler
) (_("bfd_mach_o_read_symtab_symbol: "
1781 "symbol \"%s\" is unsupported 'indirect' reference: setting to undefined"),
1783 s
->symbol
.section
= bfd_und_section_ptr
;
1786 (*_bfd_error_handler
) (_("bfd_mach_o_read_symtab_symbol: "
1787 "symbol \"%s\" specified invalid type field 0x%x: setting to undefined"),
1788 s
->symbol
.name
, symtype
);
1789 s
->symbol
.section
= bfd_und_section_ptr
;
1798 bfd_mach_o_read_symtab_strtab (bfd
*abfd
)
1800 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
1801 bfd_mach_o_symtab_command
*sym
= mdata
->symtab
;
1803 /* Fail if there is no symtab. */
1807 /* Success if already loaded. */
1811 if (abfd
->flags
& BFD_IN_MEMORY
)
1813 struct bfd_in_memory
*b
;
1815 b
= (struct bfd_in_memory
*) abfd
->iostream
;
1817 if ((sym
->stroff
+ sym
->strsize
) > b
->size
)
1819 bfd_set_error (bfd_error_file_truncated
);
1822 sym
->strtab
= (char *) b
->buffer
+ sym
->stroff
;
1826 sym
->strtab
= bfd_alloc (abfd
, sym
->strsize
);
1827 if (sym
->strtab
== NULL
)
1830 if (bfd_seek (abfd
, sym
->stroff
, SEEK_SET
) != 0
1831 || bfd_bread ((void *) sym
->strtab
, sym
->strsize
, abfd
) != sym
->strsize
)
1833 bfd_set_error (bfd_error_file_truncated
);
1842 bfd_mach_o_read_symtab_symbols (bfd
*abfd
)
1844 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
1845 bfd_mach_o_symtab_command
*sym
= mdata
->symtab
;
1849 if (sym
== NULL
|| sym
->symbols
)
1851 /* Return now if there are no symbols or if already loaded. */
1855 sym
->symbols
= bfd_alloc (abfd
, sym
->nsyms
* sizeof (bfd_mach_o_asymbol
));
1857 if (sym
->symbols
== NULL
)
1859 (*_bfd_error_handler
) (_("bfd_mach_o_read_symtab_symbols: unable to allocate memory for symbols"));
1863 ret
= bfd_mach_o_read_symtab_strtab (abfd
);
1867 for (i
= 0; i
< sym
->nsyms
; i
++)
1869 ret
= bfd_mach_o_read_symtab_symbol (abfd
, sym
, &sym
->symbols
[i
], i
);
1878 bfd_mach_o_read_dysymtab_symbol (bfd
*abfd
,
1879 bfd_mach_o_dysymtab_command
*dysym
,
1880 bfd_mach_o_symtab_command
*sym
,
1881 bfd_mach_o_asymbol
*s
,
1884 unsigned long isymoff
= dysym
->indirectsymoff
+ (i
* 4);
1885 unsigned long sym_index
;
1886 unsigned char buf
[4];
1888 BFD_ASSERT (i
< dysym
->nindirectsyms
);
1890 if (bfd_seek (abfd
, isymoff
, SEEK_SET
) != 0
1891 || bfd_bread ((void *) buf
, 4, abfd
) != 4)
1893 (*_bfd_error_handler
) (_("bfd_mach_o_read_dysymtab_symbol: unable to read %lu bytes at %lu"),
1894 (unsigned long) 4, isymoff
);
1897 sym_index
= bfd_h_get_32 (abfd
, buf
);
1899 return bfd_mach_o_read_symtab_symbol (abfd
, sym
, s
, sym_index
);
1903 bfd_mach_o_i386_flavour_string (unsigned int flavour
)
1905 switch ((int) flavour
)
1907 case BFD_MACH_O_x86_THREAD_STATE32
: return "x86_THREAD_STATE32";
1908 case BFD_MACH_O_x86_FLOAT_STATE32
: return "x86_FLOAT_STATE32";
1909 case BFD_MACH_O_x86_EXCEPTION_STATE32
: return "x86_EXCEPTION_STATE32";
1910 case BFD_MACH_O_x86_THREAD_STATE64
: return "x86_THREAD_STATE64";
1911 case BFD_MACH_O_x86_FLOAT_STATE64
: return "x86_FLOAT_STATE64";
1912 case BFD_MACH_O_x86_EXCEPTION_STATE64
: return "x86_EXCEPTION_STATE64";
1913 case BFD_MACH_O_x86_THREAD_STATE
: return "x86_THREAD_STATE";
1914 case BFD_MACH_O_x86_FLOAT_STATE
: return "x86_FLOAT_STATE";
1915 case BFD_MACH_O_x86_EXCEPTION_STATE
: return "x86_EXCEPTION_STATE";
1916 case BFD_MACH_O_x86_DEBUG_STATE32
: return "x86_DEBUG_STATE32";
1917 case BFD_MACH_O_x86_DEBUG_STATE64
: return "x86_DEBUG_STATE64";
1918 case BFD_MACH_O_x86_DEBUG_STATE
: return "x86_DEBUG_STATE";
1919 case BFD_MACH_O_x86_THREAD_STATE_NONE
: return "x86_THREAD_STATE_NONE";
1920 default: return "UNKNOWN";
1925 bfd_mach_o_ppc_flavour_string (unsigned int flavour
)
1927 switch ((int) flavour
)
1929 case BFD_MACH_O_PPC_THREAD_STATE
: return "PPC_THREAD_STATE";
1930 case BFD_MACH_O_PPC_FLOAT_STATE
: return "PPC_FLOAT_STATE";
1931 case BFD_MACH_O_PPC_EXCEPTION_STATE
: return "PPC_EXCEPTION_STATE";
1932 case BFD_MACH_O_PPC_VECTOR_STATE
: return "PPC_VECTOR_STATE";
1933 case BFD_MACH_O_PPC_THREAD_STATE64
: return "PPC_THREAD_STATE64";
1934 case BFD_MACH_O_PPC_EXCEPTION_STATE64
: return "PPC_EXCEPTION_STATE64";
1935 default: return "UNKNOWN";
1940 bfd_mach_o_read_dylinker (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1942 bfd_mach_o_dylinker_command
*cmd
= &command
->command
.dylinker
;
1943 unsigned char buf
[4];
1944 unsigned int nameoff
;
1946 BFD_ASSERT ((command
->type
== BFD_MACH_O_LC_ID_DYLINKER
)
1947 || (command
->type
== BFD_MACH_O_LC_LOAD_DYLINKER
));
1949 if (bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
) != 0
1950 || bfd_bread ((void *) buf
, 4, abfd
) != 4)
1953 nameoff
= bfd_h_get_32 (abfd
, buf
+ 0);
1955 cmd
->name_offset
= command
->offset
+ nameoff
;
1956 cmd
->name_len
= command
->len
- nameoff
;
1957 cmd
->name_str
= bfd_alloc (abfd
, cmd
->name_len
);
1958 if (cmd
->name_str
== NULL
)
1960 if (bfd_seek (abfd
, cmd
->name_offset
, SEEK_SET
) != 0
1961 || bfd_bread (cmd
->name_str
, cmd
->name_len
, abfd
) != cmd
->name_len
)
1967 bfd_mach_o_read_dylib (bfd
*abfd
, bfd_mach_o_load_command
*command
)
1969 bfd_mach_o_dylib_command
*cmd
= &command
->command
.dylib
;
1970 unsigned char buf
[16];
1971 unsigned int nameoff
;
1973 switch (command
->type
)
1975 case BFD_MACH_O_LC_LOAD_DYLIB
:
1976 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB
:
1977 case BFD_MACH_O_LC_ID_DYLIB
:
1978 case BFD_MACH_O_LC_REEXPORT_DYLIB
:
1985 if (bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
) != 0
1986 || bfd_bread ((void *) buf
, 16, abfd
) != 16)
1989 nameoff
= bfd_h_get_32 (abfd
, buf
+ 0);
1990 cmd
->timestamp
= bfd_h_get_32 (abfd
, buf
+ 4);
1991 cmd
->current_version
= bfd_h_get_32 (abfd
, buf
+ 8);
1992 cmd
->compatibility_version
= bfd_h_get_32 (abfd
, buf
+ 12);
1994 cmd
->name_offset
= command
->offset
+ nameoff
;
1995 cmd
->name_len
= command
->len
- nameoff
;
1996 cmd
->name_str
= bfd_alloc (abfd
, cmd
->name_len
);
1997 if (cmd
->name_str
== NULL
)
1999 if (bfd_seek (abfd
, cmd
->name_offset
, SEEK_SET
) != 0
2000 || bfd_bread (cmd
->name_str
, cmd
->name_len
, abfd
) != cmd
->name_len
)
2006 bfd_mach_o_read_prebound_dylib (bfd
*abfd ATTRIBUTE_UNUSED
,
2007 bfd_mach_o_load_command
*command ATTRIBUTE_UNUSED
)
2009 /* bfd_mach_o_prebound_dylib_command *cmd = &command->command.prebound_dylib; */
2011 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_PREBOUND_DYLIB
);
2016 bfd_mach_o_read_thread (bfd
*abfd
, bfd_mach_o_load_command
*command
)
2018 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
2019 bfd_mach_o_thread_command
*cmd
= &command
->command
.thread
;
2020 unsigned char buf
[8];
2021 unsigned int offset
;
2022 unsigned int nflavours
;
2025 BFD_ASSERT ((command
->type
== BFD_MACH_O_LC_THREAD
)
2026 || (command
->type
== BFD_MACH_O_LC_UNIXTHREAD
));
2028 /* Count the number of threads. */
2031 while (offset
!= command
->len
)
2033 if (offset
>= command
->len
)
2036 if (bfd_seek (abfd
, command
->offset
+ offset
, SEEK_SET
) != 0
2037 || bfd_bread ((void *) buf
, 8, abfd
) != 8)
2040 offset
+= 8 + bfd_h_get_32 (abfd
, buf
+ 4) * 4;
2044 /* Allocate threads. */
2045 cmd
->flavours
= bfd_alloc
2046 (abfd
, nflavours
* sizeof (bfd_mach_o_thread_flavour
));
2047 if (cmd
->flavours
== NULL
)
2049 cmd
->nflavours
= nflavours
;
2053 while (offset
!= command
->len
)
2055 if (offset
>= command
->len
)
2058 if (nflavours
>= cmd
->nflavours
)
2061 if (bfd_seek (abfd
, command
->offset
+ offset
, SEEK_SET
) != 0
2062 || bfd_bread ((void *) buf
, 8, abfd
) != 8)
2065 cmd
->flavours
[nflavours
].flavour
= bfd_h_get_32 (abfd
, buf
);
2066 cmd
->flavours
[nflavours
].offset
= command
->offset
+ offset
+ 8;
2067 cmd
->flavours
[nflavours
].size
= bfd_h_get_32 (abfd
, buf
+ 4) * 4;
2068 offset
+= cmd
->flavours
[nflavours
].size
+ 8;
2072 for (i
= 0; i
< nflavours
; i
++)
2075 unsigned int snamelen
;
2077 const char *flavourstr
;
2078 const char *prefix
= "LC_THREAD";
2081 switch (mdata
->header
.cputype
)
2083 case BFD_MACH_O_CPU_TYPE_POWERPC
:
2084 case BFD_MACH_O_CPU_TYPE_POWERPC_64
:
2085 flavourstr
= bfd_mach_o_ppc_flavour_string (cmd
->flavours
[i
].flavour
);
2087 case BFD_MACH_O_CPU_TYPE_I386
:
2088 case BFD_MACH_O_CPU_TYPE_X86_64
:
2089 flavourstr
= bfd_mach_o_i386_flavour_string (cmd
->flavours
[i
].flavour
);
2092 flavourstr
= "UNKNOWN_ARCHITECTURE";
2096 snamelen
= strlen (prefix
) + 1 + 20 + 1 + strlen (flavourstr
) + 1;
2097 sname
= bfd_alloc (abfd
, snamelen
);
2103 sprintf (sname
, "%s.%s.%u", prefix
, flavourstr
, j
);
2104 if (bfd_get_section_by_name (abfd
, sname
) == NULL
)
2109 bfdsec
= bfd_make_section_with_flags (abfd
, sname
, SEC_HAS_CONTENTS
);
2113 bfdsec
->size
= cmd
->flavours
[i
].size
;
2114 bfdsec
->filepos
= cmd
->flavours
[i
].offset
;
2115 bfdsec
->alignment_power
= 0x0;
2117 cmd
->section
= bfdsec
;
2124 bfd_mach_o_read_dysymtab (bfd
*abfd
, bfd_mach_o_load_command
*command
)
2126 bfd_mach_o_dysymtab_command
*cmd
= &command
->command
.dysymtab
;
2127 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
2128 unsigned char buf
[72];
2130 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_DYSYMTAB
);
2132 if (bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
) != 0
2133 || bfd_bread ((void *) buf
, 72, abfd
) != 72)
2136 cmd
->ilocalsym
= bfd_h_get_32 (abfd
, buf
+ 0);
2137 cmd
->nlocalsym
= bfd_h_get_32 (abfd
, buf
+ 4);
2138 cmd
->iextdefsym
= bfd_h_get_32 (abfd
, buf
+ 8);
2139 cmd
->nextdefsym
= bfd_h_get_32 (abfd
, buf
+ 12);
2140 cmd
->iundefsym
= bfd_h_get_32 (abfd
, buf
+ 16);
2141 cmd
->nundefsym
= bfd_h_get_32 (abfd
, buf
+ 20);
2142 cmd
->tocoff
= bfd_h_get_32 (abfd
, buf
+ 24);
2143 cmd
->ntoc
= bfd_h_get_32 (abfd
, buf
+ 28);
2144 cmd
->modtaboff
= bfd_h_get_32 (abfd
, buf
+ 32);
2145 cmd
->nmodtab
= bfd_h_get_32 (abfd
, buf
+ 36);
2146 cmd
->extrefsymoff
= bfd_h_get_32 (abfd
, buf
+ 40);
2147 cmd
->nextrefsyms
= bfd_h_get_32 (abfd
, buf
+ 44);
2148 cmd
->indirectsymoff
= bfd_h_get_32 (abfd
, buf
+ 48);
2149 cmd
->nindirectsyms
= bfd_h_get_32 (abfd
, buf
+ 52);
2150 cmd
->extreloff
= bfd_h_get_32 (abfd
, buf
+ 56);
2151 cmd
->nextrel
= bfd_h_get_32 (abfd
, buf
+ 60);
2152 cmd
->locreloff
= bfd_h_get_32 (abfd
, buf
+ 64);
2153 cmd
->nlocrel
= bfd_h_get_32 (abfd
, buf
+ 68);
2155 if (cmd
->nmodtab
!= 0)
2158 int wide
= bfd_mach_o_wide_p (abfd
);
2159 unsigned int module_len
= wide
? 56 : 52;
2162 bfd_alloc (abfd
, cmd
->nmodtab
* sizeof (bfd_mach_o_dylib_module
));
2163 if (cmd
->dylib_module
== NULL
)
2166 if (bfd_seek (abfd
, cmd
->modtaboff
, SEEK_SET
) != 0)
2169 for (i
= 0; i
< cmd
->nmodtab
; i
++)
2171 bfd_mach_o_dylib_module
*module
= &cmd
->dylib_module
[i
];
2174 if (bfd_bread ((void *) buf
, module_len
, abfd
) != module_len
)
2177 module
->module_name_idx
= bfd_h_get_32 (abfd
, buf
+ 0);
2178 module
->iextdefsym
= bfd_h_get_32 (abfd
, buf
+ 4);
2179 module
->nextdefsym
= bfd_h_get_32 (abfd
, buf
+ 8);
2180 module
->irefsym
= bfd_h_get_32 (abfd
, buf
+ 12);
2181 module
->nrefsym
= bfd_h_get_32 (abfd
, buf
+ 16);
2182 module
->ilocalsym
= bfd_h_get_32 (abfd
, buf
+ 20);
2183 module
->nlocalsym
= bfd_h_get_32 (abfd
, buf
+ 24);
2184 module
->iextrel
= bfd_h_get_32 (abfd
, buf
+ 28);
2185 module
->nextrel
= bfd_h_get_32 (abfd
, buf
+ 32);
2186 v
= bfd_h_get_32 (abfd
, buf
+36);
2187 module
->iinit
= v
& 0xffff;
2188 module
->iterm
= (v
>> 16) & 0xffff;
2189 v
= bfd_h_get_32 (abfd
, buf
+ 40);
2190 module
->ninit
= v
& 0xffff;
2191 module
->nterm
= (v
>> 16) & 0xffff;
2194 module
->objc_module_info_size
= bfd_h_get_32 (abfd
, buf
+ 44);
2195 module
->objc_module_info_addr
= bfd_h_get_64 (abfd
, buf
+ 48);
2199 module
->objc_module_info_addr
= bfd_h_get_32 (abfd
, buf
+ 44);
2200 module
->objc_module_info_size
= bfd_h_get_32 (abfd
, buf
+ 48);
2209 cmd
->dylib_toc
= bfd_alloc
2210 (abfd
, cmd
->ntoc
* sizeof (bfd_mach_o_dylib_table_of_content
));
2211 if (cmd
->dylib_toc
== NULL
)
2214 if (bfd_seek (abfd
, cmd
->tocoff
, SEEK_SET
) != 0)
2217 for (i
= 0; i
< cmd
->ntoc
; i
++)
2219 bfd_mach_o_dylib_table_of_content
*toc
= &cmd
->dylib_toc
[i
];
2221 if (bfd_bread ((void *) buf
, 8, abfd
) != 8)
2224 toc
->symbol_index
= bfd_h_get_32 (abfd
, buf
+ 0);
2225 toc
->module_index
= bfd_h_get_32 (abfd
, buf
+ 4);
2229 if (cmd
->nindirectsyms
!= 0)
2233 cmd
->indirect_syms
= bfd_alloc
2234 (abfd
, cmd
->nindirectsyms
* sizeof (unsigned int));
2235 if (cmd
->indirect_syms
== NULL
)
2238 if (bfd_seek (abfd
, cmd
->indirectsymoff
, SEEK_SET
) != 0)
2241 for (i
= 0; i
< cmd
->nindirectsyms
; i
++)
2243 unsigned int *is
= &cmd
->indirect_syms
[i
];
2245 if (bfd_bread ((void *) buf
, 4, abfd
) != 4)
2248 *is
= bfd_h_get_32 (abfd
, buf
+ 0);
2252 if (cmd
->nextrefsyms
!= 0)
2257 cmd
->ext_refs
= bfd_alloc
2258 (abfd
, cmd
->nextrefsyms
* sizeof (bfd_mach_o_dylib_reference
));
2259 if (cmd
->ext_refs
== NULL
)
2262 if (bfd_seek (abfd
, cmd
->extrefsymoff
, SEEK_SET
) != 0)
2265 for (i
= 0; i
< cmd
->nextrefsyms
; i
++)
2267 bfd_mach_o_dylib_reference
*ref
= &cmd
->ext_refs
[i
];
2269 if (bfd_bread ((void *) buf
, 4, abfd
) != 4)
2272 /* Fields isym and flags are written as bit-fields, thus we need
2273 a specific processing for endianness. */
2274 v
= bfd_h_get_32 (abfd
, buf
+ 0);
2275 if (bfd_big_endian (abfd
))
2277 ref
->isym
= (v
>> 8) & 0xffffff;
2278 ref
->flags
= v
& 0xff;
2282 ref
->isym
= v
& 0xffffff;
2283 ref
->flags
= (v
>> 24) & 0xff;
2288 if (mdata
->dysymtab
)
2290 mdata
->dysymtab
= cmd
;
2296 bfd_mach_o_read_symtab (bfd
*abfd
, bfd_mach_o_load_command
*command
)
2298 bfd_mach_o_symtab_command
*symtab
= &command
->command
.symtab
;
2299 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
2300 unsigned char buf
[16];
2302 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SYMTAB
);
2304 if (bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
) != 0
2305 || bfd_bread ((void *) buf
, 16, abfd
) != 16)
2308 symtab
->symoff
= bfd_h_get_32 (abfd
, buf
);
2309 symtab
->nsyms
= bfd_h_get_32 (abfd
, buf
+ 4);
2310 symtab
->stroff
= bfd_h_get_32 (abfd
, buf
+ 8);
2311 symtab
->strsize
= bfd_h_get_32 (abfd
, buf
+ 12);
2312 symtab
->symbols
= NULL
;
2313 symtab
->strtab
= NULL
;
2315 if (symtab
->nsyms
!= 0)
2316 abfd
->flags
|= HAS_SYMS
;
2320 mdata
->symtab
= symtab
;
2325 bfd_mach_o_read_uuid (bfd
*abfd
, bfd_mach_o_load_command
*command
)
2327 bfd_mach_o_uuid_command
*cmd
= &command
->command
.uuid
;
2329 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_UUID
);
2331 if (bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
) != 0
2332 || bfd_bread ((void *) cmd
->uuid
, 16, abfd
) != 16)
2339 bfd_mach_o_read_linkedit (bfd
*abfd
, bfd_mach_o_load_command
*command
)
2341 bfd_mach_o_linkedit_command
*cmd
= &command
->command
.linkedit
;
2344 if (bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
) != 0
2345 || bfd_bread ((void *) buf
, 8, abfd
) != 8)
2348 cmd
->dataoff
= bfd_get_32 (abfd
, buf
+ 0);
2349 cmd
->datasize
= bfd_get_32 (abfd
, buf
+ 4);
2354 bfd_mach_o_read_str (bfd
*abfd
, bfd_mach_o_load_command
*command
)
2356 bfd_mach_o_str_command
*cmd
= &command
->command
.str
;
2360 if (bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
) != 0
2361 || bfd_bread ((void *) buf
, 4, abfd
) != 4)
2364 off
= bfd_get_32 (abfd
, buf
+ 0);
2365 cmd
->stroff
= command
->offset
+ off
;
2366 cmd
->str_len
= command
->len
- off
;
2367 cmd
->str
= bfd_alloc (abfd
, cmd
->str_len
);
2368 if (cmd
->str
== NULL
)
2370 if (bfd_seek (abfd
, cmd
->stroff
, SEEK_SET
) != 0
2371 || bfd_bread ((void *) cmd
->str
, cmd
->str_len
, abfd
) != cmd
->str_len
)
2377 bfd_mach_o_read_dyld_info (bfd
*abfd
, bfd_mach_o_load_command
*command
)
2379 bfd_mach_o_dyld_info_command
*cmd
= &command
->command
.dyld_info
;
2382 if (bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
) != 0
2383 || bfd_bread ((void *) buf
, sizeof (buf
), abfd
) != sizeof (buf
))
2386 cmd
->rebase_off
= bfd_get_32 (abfd
, buf
+ 0);
2387 cmd
->rebase_size
= bfd_get_32 (abfd
, buf
+ 4);
2388 cmd
->bind_off
= bfd_get_32 (abfd
, buf
+ 8);
2389 cmd
->bind_size
= bfd_get_32 (abfd
, buf
+ 12);
2390 cmd
->weak_bind_off
= bfd_get_32 (abfd
, buf
+ 16);
2391 cmd
->weak_bind_size
= bfd_get_32 (abfd
, buf
+ 20);
2392 cmd
->lazy_bind_off
= bfd_get_32 (abfd
, buf
+ 24);
2393 cmd
->lazy_bind_size
= bfd_get_32 (abfd
, buf
+ 28);
2394 cmd
->export_off
= bfd_get_32 (abfd
, buf
+ 32);
2395 cmd
->export_size
= bfd_get_32 (abfd
, buf
+ 36);
2400 bfd_mach_o_read_segment (bfd
*abfd
,
2401 bfd_mach_o_load_command
*command
,
2404 unsigned char buf
[64];
2405 bfd_mach_o_segment_command
*seg
= &command
->command
.segment
;
2410 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SEGMENT_64
);
2412 if (bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
) != 0
2413 || bfd_bread ((void *) buf
, 64, abfd
) != 64)
2416 memcpy (seg
->segname
, buf
, 16);
2417 seg
->segname
[16] = '\0';
2419 seg
->vmaddr
= bfd_h_get_64 (abfd
, buf
+ 16);
2420 seg
->vmsize
= bfd_h_get_64 (abfd
, buf
+ 24);
2421 seg
->fileoff
= bfd_h_get_64 (abfd
, buf
+ 32);
2422 seg
->filesize
= bfd_h_get_64 (abfd
, buf
+ 40);
2423 seg
->maxprot
= bfd_h_get_32 (abfd
, buf
+ 48);
2424 seg
->initprot
= bfd_h_get_32 (abfd
, buf
+ 52);
2425 seg
->nsects
= bfd_h_get_32 (abfd
, buf
+ 56);
2426 seg
->flags
= bfd_h_get_32 (abfd
, buf
+ 60);
2430 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SEGMENT
);
2432 if (bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
) != 0
2433 || bfd_bread ((void *) buf
, 48, abfd
) != 48)
2436 memcpy (seg
->segname
, buf
, 16);
2437 seg
->segname
[16] = '\0';
2439 seg
->vmaddr
= bfd_h_get_32 (abfd
, buf
+ 16);
2440 seg
->vmsize
= bfd_h_get_32 (abfd
, buf
+ 20);
2441 seg
->fileoff
= bfd_h_get_32 (abfd
, buf
+ 24);
2442 seg
->filesize
= bfd_h_get_32 (abfd
, buf
+ 28);
2443 seg
->maxprot
= bfd_h_get_32 (abfd
, buf
+ 32);
2444 seg
->initprot
= bfd_h_get_32 (abfd
, buf
+ 36);
2445 seg
->nsects
= bfd_h_get_32 (abfd
, buf
+ 40);
2446 seg
->flags
= bfd_h_get_32 (abfd
, buf
+ 44);
2449 if (seg
->nsects
!= 0)
2451 seg
->sections
= bfd_alloc (abfd
, seg
->nsects
2452 * sizeof (bfd_mach_o_section
));
2453 if (seg
->sections
== NULL
)
2456 for (i
= 0; i
< seg
->nsects
; i
++)
2460 segoff
= command
->offset
+ BFD_MACH_O_LC_SEGMENT_64_SIZE
2461 + (i
* BFD_MACH_O_SECTION_64_SIZE
);
2463 segoff
= command
->offset
+ BFD_MACH_O_LC_SEGMENT_SIZE
2464 + (i
* BFD_MACH_O_SECTION_SIZE
);
2466 if (bfd_mach_o_read_section
2467 (abfd
, &seg
->sections
[i
], segoff
, seg
->initprot
, wide
) != 0)
2476 bfd_mach_o_read_segment_32 (bfd
*abfd
, bfd_mach_o_load_command
*command
)
2478 return bfd_mach_o_read_segment (abfd
, command
, 0);
2482 bfd_mach_o_read_segment_64 (bfd
*abfd
, bfd_mach_o_load_command
*command
)
2484 return bfd_mach_o_read_segment (abfd
, command
, 1);
2488 bfd_mach_o_read_command (bfd
*abfd
, bfd_mach_o_load_command
*command
)
2490 unsigned char buf
[8];
2492 /* Read command type and length. */
2493 if (bfd_seek (abfd
, command
->offset
, SEEK_SET
) != 0
2494 || bfd_bread ((void *) buf
, 8, abfd
) != 8)
2497 command
->type
= bfd_h_get_32 (abfd
, buf
) & ~BFD_MACH_O_LC_REQ_DYLD
;
2498 command
->type_required
= (bfd_h_get_32 (abfd
, buf
) & BFD_MACH_O_LC_REQ_DYLD
2500 command
->len
= bfd_h_get_32 (abfd
, buf
+ 4);
2502 switch (command
->type
)
2504 case BFD_MACH_O_LC_SEGMENT
:
2505 if (bfd_mach_o_read_segment_32 (abfd
, command
) != 0)
2508 case BFD_MACH_O_LC_SEGMENT_64
:
2509 if (bfd_mach_o_read_segment_64 (abfd
, command
) != 0)
2512 case BFD_MACH_O_LC_SYMTAB
:
2513 if (bfd_mach_o_read_symtab (abfd
, command
) != 0)
2516 case BFD_MACH_O_LC_SYMSEG
:
2518 case BFD_MACH_O_LC_THREAD
:
2519 case BFD_MACH_O_LC_UNIXTHREAD
:
2520 if (bfd_mach_o_read_thread (abfd
, command
) != 0)
2523 case BFD_MACH_O_LC_LOAD_DYLINKER
:
2524 case BFD_MACH_O_LC_ID_DYLINKER
:
2525 if (bfd_mach_o_read_dylinker (abfd
, command
) != 0)
2528 case BFD_MACH_O_LC_LOAD_DYLIB
:
2529 case BFD_MACH_O_LC_ID_DYLIB
:
2530 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB
:
2531 case BFD_MACH_O_LC_REEXPORT_DYLIB
:
2532 if (bfd_mach_o_read_dylib (abfd
, command
) != 0)
2535 case BFD_MACH_O_LC_PREBOUND_DYLIB
:
2536 if (bfd_mach_o_read_prebound_dylib (abfd
, command
) != 0)
2539 case BFD_MACH_O_LC_LOADFVMLIB
:
2540 case BFD_MACH_O_LC_IDFVMLIB
:
2541 case BFD_MACH_O_LC_IDENT
:
2542 case BFD_MACH_O_LC_FVMFILE
:
2543 case BFD_MACH_O_LC_PREPAGE
:
2544 case BFD_MACH_O_LC_ROUTINES
:
2545 case BFD_MACH_O_LC_ROUTINES_64
:
2547 case BFD_MACH_O_LC_SUB_FRAMEWORK
:
2548 case BFD_MACH_O_LC_SUB_UMBRELLA
:
2549 case BFD_MACH_O_LC_SUB_LIBRARY
:
2550 case BFD_MACH_O_LC_SUB_CLIENT
:
2551 case BFD_MACH_O_LC_RPATH
:
2552 if (bfd_mach_o_read_str (abfd
, command
) != 0)
2555 case BFD_MACH_O_LC_DYSYMTAB
:
2556 if (bfd_mach_o_read_dysymtab (abfd
, command
) != 0)
2559 case BFD_MACH_O_LC_TWOLEVEL_HINTS
:
2560 case BFD_MACH_O_LC_PREBIND_CKSUM
:
2562 case BFD_MACH_O_LC_UUID
:
2563 if (bfd_mach_o_read_uuid (abfd
, command
) != 0)
2566 case BFD_MACH_O_LC_CODE_SIGNATURE
:
2567 case BFD_MACH_O_LC_SEGMENT_SPLIT_INFO
:
2568 if (bfd_mach_o_read_linkedit (abfd
, command
) != 0)
2571 case BFD_MACH_O_LC_DYLD_INFO
:
2572 if (bfd_mach_o_read_dyld_info (abfd
, command
) != 0)
2576 (*_bfd_error_handler
) (_("unable to read unknown load command 0x%lx"),
2577 (unsigned long) command
->type
);
2585 bfd_mach_o_flatten_sections (bfd
*abfd
)
2587 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
2591 /* Count total number of sections. */
2594 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
2596 if (mdata
->commands
[i
].type
== BFD_MACH_O_LC_SEGMENT
2597 || mdata
->commands
[i
].type
== BFD_MACH_O_LC_SEGMENT_64
)
2599 bfd_mach_o_segment_command
*seg
;
2601 seg
= &mdata
->commands
[i
].command
.segment
;
2602 mdata
->nsects
+= seg
->nsects
;
2606 /* Allocate sections array. */
2607 mdata
->sections
= bfd_alloc (abfd
,
2608 mdata
->nsects
* sizeof (bfd_mach_o_section
*));
2610 /* Fill the array. */
2613 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
2615 if (mdata
->commands
[i
].type
== BFD_MACH_O_LC_SEGMENT
2616 || mdata
->commands
[i
].type
== BFD_MACH_O_LC_SEGMENT_64
)
2618 bfd_mach_o_segment_command
*seg
;
2620 seg
= &mdata
->commands
[i
].command
.segment
;
2621 BFD_ASSERT (csect
+ seg
->nsects
<= mdata
->nsects
);
2623 for (j
= 0; j
< seg
->nsects
; j
++)
2624 mdata
->sections
[csect
++] = &seg
->sections
[j
];
2630 bfd_mach_o_scan_start_address (bfd
*abfd
)
2632 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
2633 bfd_mach_o_thread_command
*cmd
= NULL
;
2636 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
2638 if ((mdata
->commands
[i
].type
== BFD_MACH_O_LC_THREAD
) ||
2639 (mdata
->commands
[i
].type
== BFD_MACH_O_LC_UNIXTHREAD
))
2642 cmd
= &mdata
->commands
[i
].command
.thread
;
2651 for (i
= 0; i
< cmd
->nflavours
; i
++)
2653 if ((mdata
->header
.cputype
== BFD_MACH_O_CPU_TYPE_I386
)
2654 && (cmd
->flavours
[i
].flavour
2655 == (unsigned long) BFD_MACH_O_x86_THREAD_STATE32
))
2657 unsigned char buf
[4];
2659 if (bfd_seek (abfd
, cmd
->flavours
[i
].offset
+ 40, SEEK_SET
) != 0
2660 || bfd_bread (buf
, 4, abfd
) != 4)
2663 abfd
->start_address
= bfd_h_get_32 (abfd
, buf
);
2665 else if ((mdata
->header
.cputype
== BFD_MACH_O_CPU_TYPE_POWERPC
)
2666 && (cmd
->flavours
[i
].flavour
== BFD_MACH_O_PPC_THREAD_STATE
))
2668 unsigned char buf
[4];
2670 if (bfd_seek (abfd
, cmd
->flavours
[i
].offset
+ 0, SEEK_SET
) != 0
2671 || bfd_bread (buf
, 4, abfd
) != 4)
2674 abfd
->start_address
= bfd_h_get_32 (abfd
, buf
);
2676 else if ((mdata
->header
.cputype
== BFD_MACH_O_CPU_TYPE_POWERPC_64
)
2677 && (cmd
->flavours
[i
].flavour
== BFD_MACH_O_PPC_THREAD_STATE64
))
2679 unsigned char buf
[8];
2681 if (bfd_seek (abfd
, cmd
->flavours
[i
].offset
+ 0, SEEK_SET
) != 0
2682 || bfd_bread (buf
, 8, abfd
) != 8)
2685 abfd
->start_address
= bfd_h_get_64 (abfd
, buf
);
2687 else if ((mdata
->header
.cputype
== BFD_MACH_O_CPU_TYPE_X86_64
)
2688 && (cmd
->flavours
[i
].flavour
== BFD_MACH_O_x86_THREAD_STATE64
))
2690 unsigned char buf
[8];
2692 if (bfd_seek (abfd
, cmd
->flavours
[i
].offset
+ (16 * 8), SEEK_SET
) != 0
2693 || bfd_bread (buf
, 8, abfd
) != 8)
2696 abfd
->start_address
= bfd_h_get_64 (abfd
, buf
);
2704 bfd_mach_o_set_arch_mach (bfd
*abfd
,
2705 enum bfd_architecture arch
,
2706 unsigned long machine
)
2708 bfd_mach_o_backend_data
*bed
= bfd_mach_o_get_backend_data (abfd
);
2710 /* If this isn't the right architecture for this backend, and this
2711 isn't the generic backend, fail. */
2712 if (arch
!= bed
->arch
2713 && arch
!= bfd_arch_unknown
2714 && bed
->arch
!= bfd_arch_unknown
)
2717 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
2721 bfd_mach_o_scan (bfd
*abfd
,
2722 bfd_mach_o_header
*header
,
2723 bfd_mach_o_data_struct
*mdata
)
2726 enum bfd_architecture cputype
;
2727 unsigned long cpusubtype
;
2728 unsigned int hdrsize
;
2730 hdrsize
= mach_o_wide_p (header
) ?
2731 BFD_MACH_O_HEADER_64_SIZE
: BFD_MACH_O_HEADER_SIZE
;
2733 mdata
->header
= *header
;
2735 abfd
->flags
= abfd
->flags
& BFD_IN_MEMORY
;
2736 switch (header
->filetype
)
2738 case BFD_MACH_O_MH_OBJECT
:
2739 abfd
->flags
|= HAS_RELOC
;
2741 case BFD_MACH_O_MH_EXECUTE
:
2742 abfd
->flags
|= EXEC_P
;
2744 case BFD_MACH_O_MH_DYLIB
:
2745 case BFD_MACH_O_MH_BUNDLE
:
2746 abfd
->flags
|= DYNAMIC
;
2750 abfd
->tdata
.mach_o_data
= mdata
;
2752 bfd_mach_o_convert_architecture (header
->cputype
, header
->cpusubtype
,
2753 &cputype
, &cpusubtype
);
2754 if (cputype
== bfd_arch_unknown
)
2756 (*_bfd_error_handler
) (_("bfd_mach_o_scan: unknown architecture 0x%lx/0x%lx"),
2757 header
->cputype
, header
->cpusubtype
);
2761 bfd_set_arch_mach (abfd
, cputype
, cpusubtype
);
2763 if (header
->ncmds
!= 0)
2765 mdata
->commands
= bfd_alloc
2766 (abfd
, header
->ncmds
* sizeof (bfd_mach_o_load_command
));
2767 if (mdata
->commands
== NULL
)
2770 for (i
= 0; i
< header
->ncmds
; i
++)
2772 bfd_mach_o_load_command
*cur
= &mdata
->commands
[i
];
2775 cur
->offset
= hdrsize
;
2778 bfd_mach_o_load_command
*prev
= &mdata
->commands
[i
- 1];
2779 cur
->offset
= prev
->offset
+ prev
->len
;
2782 if (bfd_mach_o_read_command (abfd
, cur
) < 0)
2787 if (bfd_mach_o_scan_start_address (abfd
) < 0)
2790 bfd_mach_o_flatten_sections (abfd
);
2795 bfd_mach_o_mkobject_init (bfd
*abfd
)
2797 bfd_mach_o_data_struct
*mdata
= NULL
;
2799 mdata
= bfd_alloc (abfd
, sizeof (bfd_mach_o_data_struct
));
2802 abfd
->tdata
.mach_o_data
= mdata
;
2804 mdata
->header
.magic
= 0;
2805 mdata
->header
.cputype
= 0;
2806 mdata
->header
.cpusubtype
= 0;
2807 mdata
->header
.filetype
= 0;
2808 mdata
->header
.ncmds
= 0;
2809 mdata
->header
.sizeofcmds
= 0;
2810 mdata
->header
.flags
= 0;
2811 mdata
->header
.byteorder
= BFD_ENDIAN_UNKNOWN
;
2812 mdata
->commands
= NULL
;
2814 mdata
->sections
= NULL
;
2820 bfd_mach_o_gen_mkobject (bfd
*abfd
)
2822 bfd_mach_o_data_struct
*mdata
;
2824 if (!bfd_mach_o_mkobject_init (abfd
))
2827 mdata
= bfd_mach_o_get_data (abfd
);
2828 mdata
->header
.magic
= BFD_MACH_O_MH_MAGIC
;
2829 mdata
->header
.cputype
= 0;
2830 mdata
->header
.cpusubtype
= 0;
2831 mdata
->header
.byteorder
= abfd
->xvec
->byteorder
;
2832 mdata
->header
.version
= 1;
2838 bfd_mach_o_header_p (bfd
*abfd
,
2839 bfd_mach_o_filetype filetype
,
2840 bfd_mach_o_cpu_type cputype
)
2842 struct bfd_preserve preserve
;
2843 bfd_mach_o_header header
;
2845 preserve
.marker
= NULL
;
2846 if (!bfd_mach_o_read_header (abfd
, &header
))
2849 if (! (header
.byteorder
== BFD_ENDIAN_BIG
2850 || header
.byteorder
== BFD_ENDIAN_LITTLE
))
2852 (*_bfd_error_handler
) (_("unknown header byte-order value 0x%lx"),
2853 (unsigned long) header
.byteorder
);
2857 if (! ((header
.byteorder
== BFD_ENDIAN_BIG
2858 && abfd
->xvec
->byteorder
== BFD_ENDIAN_BIG
2859 && abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
2860 || (header
.byteorder
== BFD_ENDIAN_LITTLE
2861 && abfd
->xvec
->byteorder
== BFD_ENDIAN_LITTLE
2862 && abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)))
2865 /* Check cputype and filetype.
2866 In case of wildcard, do not accept magics that are handled by existing
2870 if (header
.cputype
!= cputype
)
2875 switch (header
.cputype
)
2877 case BFD_MACH_O_CPU_TYPE_I386
:
2878 /* Handled by mach-o-i386 */
2886 if (header
.filetype
!= filetype
)
2891 switch (header
.filetype
)
2893 case BFD_MACH_O_MH_CORE
:
2894 /* Handled by core_p */
2901 preserve
.marker
= bfd_zalloc (abfd
, sizeof (bfd_mach_o_data_struct
));
2902 if (preserve
.marker
== NULL
2903 || !bfd_preserve_save (abfd
, &preserve
))
2906 if (bfd_mach_o_scan (abfd
, &header
,
2907 (bfd_mach_o_data_struct
*) preserve
.marker
) != 0)
2910 bfd_preserve_finish (abfd
, &preserve
);
2914 bfd_set_error (bfd_error_wrong_format
);
2917 if (preserve
.marker
!= NULL
)
2918 bfd_preserve_restore (abfd
, &preserve
);
2922 static const bfd_target
*
2923 bfd_mach_o_gen_object_p (bfd
*abfd
)
2925 return bfd_mach_o_header_p (abfd
, 0, 0);
2928 static const bfd_target
*
2929 bfd_mach_o_gen_core_p (bfd
*abfd
)
2931 return bfd_mach_o_header_p (abfd
, BFD_MACH_O_MH_CORE
, 0);
2934 typedef struct mach_o_fat_archentry
2936 unsigned long cputype
;
2937 unsigned long cpusubtype
;
2938 unsigned long offset
;
2940 unsigned long align
;
2941 } mach_o_fat_archentry
;
2943 typedef struct mach_o_fat_data_struct
2945 unsigned long magic
;
2946 unsigned long nfat_arch
;
2947 mach_o_fat_archentry
*archentries
;
2948 } mach_o_fat_data_struct
;
2951 bfd_mach_o_archive_p (bfd
*abfd
)
2953 mach_o_fat_data_struct
*adata
= NULL
;
2954 unsigned char buf
[20];
2957 if (bfd_seek (abfd
, 0, SEEK_SET
) != 0
2958 || bfd_bread ((void *) buf
, 8, abfd
) != 8)
2961 adata
= bfd_alloc (abfd
, sizeof (mach_o_fat_data_struct
));
2965 adata
->magic
= bfd_getb32 (buf
);
2966 adata
->nfat_arch
= bfd_getb32 (buf
+ 4);
2967 if (adata
->magic
!= 0xcafebabe)
2969 /* Avoid matching Java bytecode files, which have the same magic number.
2970 In the Java bytecode file format this field contains the JVM version,
2971 which starts at 43.0. */
2972 if (adata
->nfat_arch
> 30)
2975 adata
->archentries
=
2976 bfd_alloc (abfd
, adata
->nfat_arch
* sizeof (mach_o_fat_archentry
));
2977 if (adata
->archentries
== NULL
)
2980 for (i
= 0; i
< adata
->nfat_arch
; i
++)
2982 if (bfd_seek (abfd
, 8 + 20 * i
, SEEK_SET
) != 0
2983 || bfd_bread ((void *) buf
, 20, abfd
) != 20)
2985 adata
->archentries
[i
].cputype
= bfd_getb32 (buf
);
2986 adata
->archentries
[i
].cpusubtype
= bfd_getb32 (buf
+ 4);
2987 adata
->archentries
[i
].offset
= bfd_getb32 (buf
+ 8);
2988 adata
->archentries
[i
].size
= bfd_getb32 (buf
+ 12);
2989 adata
->archentries
[i
].align
= bfd_getb32 (buf
+ 16);
2992 abfd
->tdata
.mach_o_fat_data
= adata
;
2997 bfd_release (abfd
, adata
);
2998 bfd_set_error (bfd_error_wrong_format
);
3003 bfd_mach_o_openr_next_archived_file (bfd
*archive
, bfd
*prev
)
3005 mach_o_fat_data_struct
*adata
;
3006 mach_o_fat_archentry
*entry
= NULL
;
3009 enum bfd_architecture arch_type
;
3010 unsigned long arch_subtype
;
3012 adata
= (mach_o_fat_data_struct
*) archive
->tdata
.mach_o_fat_data
;
3013 BFD_ASSERT (adata
!= NULL
);
3015 /* Find index of previous entry. */
3017 i
= 0; /* Start at first one. */
3020 for (i
= 0; i
< adata
->nfat_arch
; i
++)
3022 if (adata
->archentries
[i
].offset
== prev
->origin
)
3026 if (i
== adata
->nfat_arch
)
3029 bfd_set_error (bfd_error_bad_value
);
3032 i
++; /* Get next entry. */
3035 if (i
>= adata
->nfat_arch
)
3037 bfd_set_error (bfd_error_no_more_archived_files
);
3041 entry
= &adata
->archentries
[i
];
3042 nbfd
= _bfd_new_bfd_contained_in (archive
);
3046 nbfd
->origin
= entry
->offset
;
3048 bfd_mach_o_convert_architecture (entry
->cputype
, entry
->cpusubtype
,
3049 &arch_type
, &arch_subtype
);
3050 /* Create the member filename.
3051 Use FILENAME:ARCH_NAME. */
3054 const char *arch_name
;
3055 size_t arch_file_len
= strlen (bfd_get_filename (archive
));
3057 arch_name
= bfd_printable_arch_mach (arch_type
, arch_subtype
);
3058 s
= bfd_malloc (arch_file_len
+ 1 + strlen (arch_name
) + 1);
3061 memcpy (s
, bfd_get_filename (archive
), arch_file_len
);
3062 s
[arch_file_len
] = ':';
3063 strcpy (s
+ arch_file_len
+ 1, arch_name
);
3066 nbfd
->iostream
= NULL
;
3067 bfd_set_arch_mach (nbfd
, arch_type
, arch_subtype
);
3072 /* If ABFD format is FORMAT and architecture is ARCH, return it.
3073 If ABFD is a fat image containing a member that corresponds to FORMAT
3074 and ARCH, returns it.
3075 In other case, returns NULL.
3076 This function allows transparent uses of fat images. */
3078 bfd_mach_o_fat_extract (bfd
*abfd
,
3080 const bfd_arch_info_type
*arch
)
3083 mach_o_fat_data_struct
*adata
;
3086 if (bfd_check_format (abfd
, format
))
3088 if (bfd_get_arch_info (abfd
) == arch
)
3092 if (!bfd_check_format (abfd
, bfd_archive
)
3093 || abfd
->xvec
!= &mach_o_fat_vec
)
3096 /* This is a Mach-O fat image. */
3097 adata
= (mach_o_fat_data_struct
*) abfd
->tdata
.mach_o_fat_data
;
3098 BFD_ASSERT (adata
!= NULL
);
3100 for (i
= 0; i
< adata
->nfat_arch
; i
++)
3102 struct mach_o_fat_archentry
*e
= &adata
->archentries
[i
];
3103 enum bfd_architecture cpu_type
;
3104 unsigned long cpu_subtype
;
3106 bfd_mach_o_convert_architecture (e
->cputype
, e
->cpusubtype
,
3107 &cpu_type
, &cpu_subtype
);
3108 if (cpu_type
!= arch
->arch
|| cpu_subtype
!= arch
->mach
)
3111 /* The architecture is found. */
3112 res
= _bfd_new_bfd_contained_in (abfd
);
3116 res
->origin
= e
->offset
;
3118 res
->filename
= strdup (abfd
->filename
);
3119 res
->iostream
= NULL
;
3121 if (bfd_check_format (res
, format
))
3123 BFD_ASSERT (bfd_get_arch_info (res
) == arch
);
3134 bfd_mach_o_lookup_section (bfd
*abfd
,
3136 bfd_mach_o_load_command
**mcommand
,
3137 bfd_mach_o_section
**msection
)
3139 struct mach_o_data_struct
*md
= bfd_mach_o_get_data (abfd
);
3140 unsigned int i
, j
, num
;
3142 bfd_mach_o_load_command
*ncmd
= NULL
;
3143 bfd_mach_o_section
*nsect
= NULL
;
3145 BFD_ASSERT (mcommand
!= NULL
);
3146 BFD_ASSERT (msection
!= NULL
);
3149 for (i
= 0; i
< md
->header
.ncmds
; i
++)
3151 struct bfd_mach_o_load_command
*cmd
= &md
->commands
[i
];
3152 struct bfd_mach_o_segment_command
*seg
= NULL
;
3154 if (cmd
->type
!= BFD_MACH_O_LC_SEGMENT
3155 || cmd
->type
!= BFD_MACH_O_LC_SEGMENT_64
)
3157 seg
= &cmd
->command
.segment
;
3159 for (j
= 0; j
< seg
->nsects
; j
++)
3161 struct bfd_mach_o_section
*sect
= &seg
->sections
[j
];
3163 if (sect
->bfdsection
== section
)
3181 bfd_mach_o_lookup_command (bfd
*abfd
,
3182 bfd_mach_o_load_command_type type
,
3183 bfd_mach_o_load_command
**mcommand
)
3185 struct mach_o_data_struct
*md
= bfd_mach_o_get_data (abfd
);
3186 bfd_mach_o_load_command
*ncmd
= NULL
;
3187 unsigned int i
, num
;
3189 BFD_ASSERT (md
!= NULL
);
3190 BFD_ASSERT (mcommand
!= NULL
);
3193 for (i
= 0; i
< md
->header
.ncmds
; i
++)
3195 struct bfd_mach_o_load_command
*cmd
= &md
->commands
[i
];
3197 if (cmd
->type
!= type
)
3210 bfd_mach_o_stack_addr (enum bfd_mach_o_cpu_type type
)
3214 case BFD_MACH_O_CPU_TYPE_MC680x0
:
3216 case BFD_MACH_O_CPU_TYPE_MC88000
:
3218 case BFD_MACH_O_CPU_TYPE_POWERPC
:
3220 case BFD_MACH_O_CPU_TYPE_I386
:
3222 case BFD_MACH_O_CPU_TYPE_SPARC
:
3224 case BFD_MACH_O_CPU_TYPE_I860
:
3226 case BFD_MACH_O_CPU_TYPE_HPPA
:
3227 return 0xc0000000 - 0x04000000;
3233 typedef struct bfd_mach_o_xlat_name
3238 bfd_mach_o_xlat_name
;
3241 bfd_mach_o_print_flags (const bfd_mach_o_xlat_name
*table
,
3247 for (; table
->name
; table
++)
3249 if (table
->val
& val
)
3252 fprintf (file
, "+");
3253 fprintf (file
, "%s", table
->name
);
3261 fprintf (file
, "+");
3262 fprintf (file
, "0x%lx", val
);
3266 fprintf (file
, "-");
3270 bfd_mach_o_get_name (const bfd_mach_o_xlat_name
*table
, unsigned long val
)
3272 for (; table
->name
; table
++)
3273 if (table
->val
== val
)
3278 static bfd_mach_o_xlat_name bfd_mach_o_cpu_name
[] =
3280 { "vax", BFD_MACH_O_CPU_TYPE_VAX
},
3281 { "mc680x0", BFD_MACH_O_CPU_TYPE_MC680x0
},
3282 { "i386", BFD_MACH_O_CPU_TYPE_I386
},
3283 { "mips", BFD_MACH_O_CPU_TYPE_MIPS
},
3284 { "mc98000", BFD_MACH_O_CPU_TYPE_MC98000
},
3285 { "hppa", BFD_MACH_O_CPU_TYPE_HPPA
},
3286 { "arm", BFD_MACH_O_CPU_TYPE_ARM
},
3287 { "mc88000", BFD_MACH_O_CPU_TYPE_MC88000
},
3288 { "sparc", BFD_MACH_O_CPU_TYPE_SPARC
},
3289 { "i860", BFD_MACH_O_CPU_TYPE_I860
},
3290 { "alpha", BFD_MACH_O_CPU_TYPE_ALPHA
},
3291 { "powerpc", BFD_MACH_O_CPU_TYPE_POWERPC
},
3292 { "powerpc_64", BFD_MACH_O_CPU_TYPE_POWERPC_64
},
3293 { "x86_64", BFD_MACH_O_CPU_TYPE_X86_64
},
3297 static bfd_mach_o_xlat_name bfd_mach_o_filetype_name
[] =
3299 { "object", BFD_MACH_O_MH_OBJECT
},
3300 { "execute", BFD_MACH_O_MH_EXECUTE
},
3301 { "fvmlib", BFD_MACH_O_MH_FVMLIB
},
3302 { "core", BFD_MACH_O_MH_CORE
},
3303 { "preload", BFD_MACH_O_MH_PRELOAD
},
3304 { "dylib", BFD_MACH_O_MH_DYLIB
},
3305 { "dylinker", BFD_MACH_O_MH_DYLINKER
},
3306 { "bundle", BFD_MACH_O_MH_BUNDLE
},
3307 { "dylib_stub", BFD_MACH_O_MH_DYLIB_STUB
},
3308 { "dym", BFD_MACH_O_MH_DSYM
},
3309 { "kext_bundle", BFD_MACH_O_MH_KEXT_BUNDLE
},
3313 static bfd_mach_o_xlat_name bfd_mach_o_header_flags_name
[] =
3315 { "noundefs", BFD_MACH_O_MH_NOUNDEFS
},
3316 { "incrlink", BFD_MACH_O_MH_INCRLINK
},
3317 { "dyldlink", BFD_MACH_O_MH_DYLDLINK
},
3318 { "bindatload", BFD_MACH_O_MH_BINDATLOAD
},
3319 { "prebound", BFD_MACH_O_MH_PREBOUND
},
3320 { "split_segs", BFD_MACH_O_MH_SPLIT_SEGS
},
3321 { "lazy_init", BFD_MACH_O_MH_LAZY_INIT
},
3322 { "twolevel", BFD_MACH_O_MH_TWOLEVEL
},
3323 { "force_flat", BFD_MACH_O_MH_FORCE_FLAT
},
3324 { "nomultidefs", BFD_MACH_O_MH_NOMULTIDEFS
},
3325 { "nofixprebinding", BFD_MACH_O_MH_NOFIXPREBINDING
},
3326 { "prebindable", BFD_MACH_O_MH_PREBINDABLE
},
3327 { "allmodsbound", BFD_MACH_O_MH_ALLMODSBOUND
},
3328 { "subsections_via_symbols", BFD_MACH_O_MH_SUBSECTIONS_VIA_SYMBOLS
},
3329 { "canonical", BFD_MACH_O_MH_CANONICAL
},
3330 { "weak_defines", BFD_MACH_O_MH_WEAK_DEFINES
},
3331 { "binds_to_weak", BFD_MACH_O_MH_BINDS_TO_WEAK
},
3332 { "allow_stack_execution", BFD_MACH_O_MH_ALLOW_STACK_EXECUTION
},
3333 { "root_safe", BFD_MACH_O_MH_ROOT_SAFE
},
3334 { "setuid_safe", BFD_MACH_O_MH_SETUID_SAFE
},
3335 { "no_reexported_dylibs", BFD_MACH_O_MH_NO_REEXPORTED_DYLIBS
},
3336 { "pie", BFD_MACH_O_MH_PIE
},
3340 static bfd_mach_o_xlat_name bfd_mach_o_section_type_name
[] =
3342 { "regular", BFD_MACH_O_S_REGULAR
},
3343 { "zerofill", BFD_MACH_O_S_ZEROFILL
},
3344 { "cstring_literals", BFD_MACH_O_S_CSTRING_LITERALS
},
3345 { "4byte_literals", BFD_MACH_O_S_4BYTE_LITERALS
},
3346 { "8byte_literals", BFD_MACH_O_S_8BYTE_LITERALS
},
3347 { "literal_pointers", BFD_MACH_O_S_LITERAL_POINTERS
},
3348 { "non_lazy_symbol_pointers", BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS
},
3349 { "lazy_symbol_pointers", BFD_MACH_O_S_LAZY_SYMBOL_POINTERS
},
3350 { "symbol_stubs", BFD_MACH_O_S_SYMBOL_STUBS
},
3351 { "mod_init_func_pointers", BFD_MACH_O_S_MOD_INIT_FUNC_POINTERS
},
3352 { "mod_fini_func_pointers", BFD_MACH_O_S_MOD_FINI_FUNC_POINTERS
},
3353 { "coalesced", BFD_MACH_O_S_COALESCED
},
3354 { "gb_zerofill", BFD_MACH_O_S_GB_ZEROFILL
},
3355 { "interposing", BFD_MACH_O_S_INTERPOSING
},
3356 { "16byte_literals", BFD_MACH_O_S_16BYTE_LITERALS
},
3357 { "dtrace_dof", BFD_MACH_O_S_DTRACE_DOF
},
3358 { "lazy_dylib_symbol_pointers", BFD_MACH_O_S_LAZY_DYLIB_SYMBOL_POINTERS
},
3362 static bfd_mach_o_xlat_name bfd_mach_o_section_attribute_name
[] =
3364 { "loc_reloc", BFD_MACH_O_S_ATTR_LOC_RELOC
},
3365 { "ext_reloc", BFD_MACH_O_S_ATTR_EXT_RELOC
},
3366 { "some_instructions", BFD_MACH_O_S_ATTR_SOME_INSTRUCTIONS
},
3367 { "debug", BFD_MACH_O_S_ATTR_DEBUG
},
3368 { "modifying_code", BFD_MACH_O_S_SELF_MODIFYING_CODE
},
3369 { "live_support", BFD_MACH_O_S_ATTR_LIVE_SUPPORT
},
3370 { "no_dead_strip", BFD_MACH_O_S_ATTR_NO_DEAD_STRIP
},
3371 { "strip_static_syms", BFD_MACH_O_S_ATTR_STRIP_STATIC_SYMS
},
3372 { "no_toc", BFD_MACH_O_S_ATTR_NO_TOC
},
3373 { "pure_instructions", BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS
},
3377 static bfd_mach_o_xlat_name bfd_mach_o_load_command_name
[] =
3379 { "segment", BFD_MACH_O_LC_SEGMENT
},
3380 { "symtab", BFD_MACH_O_LC_SYMTAB
},
3381 { "symseg", BFD_MACH_O_LC_SYMSEG
},
3382 { "thread", BFD_MACH_O_LC_THREAD
},
3383 { "unixthread", BFD_MACH_O_LC_UNIXTHREAD
},
3384 { "loadfvmlib", BFD_MACH_O_LC_LOADFVMLIB
},
3385 { "idfvmlib", BFD_MACH_O_LC_IDFVMLIB
},
3386 { "ident", BFD_MACH_O_LC_IDENT
},
3387 { "fvmfile", BFD_MACH_O_LC_FVMFILE
},
3388 { "prepage", BFD_MACH_O_LC_PREPAGE
},
3389 { "dysymtab", BFD_MACH_O_LC_DYSYMTAB
},
3390 { "load_dylib", BFD_MACH_O_LC_LOAD_DYLIB
},
3391 { "id_dylib", BFD_MACH_O_LC_ID_DYLIB
},
3392 { "load_dylinker", BFD_MACH_O_LC_LOAD_DYLINKER
},
3393 { "id_dylinker", BFD_MACH_O_LC_ID_DYLINKER
},
3394 { "prebound_dylib", BFD_MACH_O_LC_PREBOUND_DYLIB
},
3395 { "routines", BFD_MACH_O_LC_ROUTINES
},
3396 { "sub_framework", BFD_MACH_O_LC_SUB_FRAMEWORK
},
3397 { "sub_umbrella", BFD_MACH_O_LC_SUB_UMBRELLA
},
3398 { "sub_client", BFD_MACH_O_LC_SUB_CLIENT
},
3399 { "sub_library", BFD_MACH_O_LC_SUB_LIBRARY
},
3400 { "twolevel_hints", BFD_MACH_O_LC_TWOLEVEL_HINTS
},
3401 { "prebind_cksum", BFD_MACH_O_LC_PREBIND_CKSUM
},
3402 { "load_weak_dylib", BFD_MACH_O_LC_LOAD_WEAK_DYLIB
},
3403 { "segment_64", BFD_MACH_O_LC_SEGMENT_64
},
3404 { "routines_64", BFD_MACH_O_LC_ROUTINES_64
},
3405 { "uuid", BFD_MACH_O_LC_UUID
},
3406 { "rpath", BFD_MACH_O_LC_RPATH
},
3407 { "code_signature", BFD_MACH_O_LC_CODE_SIGNATURE
},
3408 { "segment_split_info", BFD_MACH_O_LC_SEGMENT_SPLIT_INFO
},
3409 { "reexport_dylib", BFD_MACH_O_LC_REEXPORT_DYLIB
},
3410 { "lazy_load_dylib", BFD_MACH_O_LC_LAZY_LOAD_DYLIB
},
3411 { "encryption_info", BFD_MACH_O_LC_ENCRYPTION_INFO
},
3412 { "dyld_info", BFD_MACH_O_LC_DYLD_INFO
},
3416 /* Get the section type from NAME. Return -1 if NAME is unknown. */
3419 bfd_mach_o_get_section_type_from_name (const char *name
)
3421 bfd_mach_o_xlat_name
*x
;
3423 for (x
= bfd_mach_o_section_type_name
; x
->name
; x
++)
3424 if (strcmp (x
->name
, name
) == 0)
3426 return (unsigned int)-1;
3429 /* Get the section attribute from NAME. Return -1 if NAME is unknown. */
3432 bfd_mach_o_get_section_attribute_from_name (const char *name
)
3434 bfd_mach_o_xlat_name
*x
;
3436 for (x
= bfd_mach_o_section_attribute_name
; x
->name
; x
++)
3437 if (strcmp (x
->name
, name
) == 0)
3439 return (unsigned int)-1;
3443 bfd_mach_o_print_private_header (bfd
*abfd
, FILE *file
)
3445 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
3446 bfd_mach_o_header
*h
= &mdata
->header
;
3448 fputs (_("Mach-O header:\n"), file
);
3449 fprintf (file
, _(" magic : %08lx\n"), h
->magic
);
3450 fprintf (file
, _(" cputype : %08lx (%s)\n"), h
->cputype
,
3451 bfd_mach_o_get_name (bfd_mach_o_cpu_name
, h
->cputype
));
3452 fprintf (file
, _(" cpusubtype: %08lx\n"), h
->cpusubtype
);
3453 fprintf (file
, _(" filetype : %08lx (%s)\n"),
3455 bfd_mach_o_get_name (bfd_mach_o_filetype_name
, h
->filetype
));
3456 fprintf (file
, _(" ncmds : %08lx (%lu)\n"), h
->ncmds
, h
->ncmds
);
3457 fprintf (file
, _(" sizeofcmds: %08lx\n"), h
->sizeofcmds
);
3458 fprintf (file
, _(" flags : %08lx ("), h
->flags
);
3459 bfd_mach_o_print_flags (bfd_mach_o_header_flags_name
, h
->flags
, file
);
3460 fputs (_(")\n"), file
);
3461 fprintf (file
, _(" reserved : %08x\n"), h
->reserved
);
3465 bfd_mach_o_print_section_map (bfd
*abfd
, FILE *file
)
3467 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
3469 unsigned int sec_nbr
= 0;
3471 fputs (_("Segments and Sections:\n"), file
);
3472 fputs (_(" #: Segment name Section name Address\n"), file
);
3474 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
3476 bfd_mach_o_segment_command
*seg
;
3478 if (mdata
->commands
[i
].type
!= BFD_MACH_O_LC_SEGMENT
3479 && mdata
->commands
[i
].type
!= BFD_MACH_O_LC_SEGMENT_64
)
3482 seg
= &mdata
->commands
[i
].command
.segment
;
3484 fprintf (file
, "[Segment %-16s ", seg
->segname
);
3485 fprintf_vma (file
, seg
->vmaddr
);
3486 fprintf (file
, "-");
3487 fprintf_vma (file
, seg
->vmaddr
+ seg
->vmsize
- 1);
3489 fputc (seg
->initprot
& BFD_MACH_O_PROT_READ
? 'r' : '-', file
);
3490 fputc (seg
->initprot
& BFD_MACH_O_PROT_WRITE
? 'w' : '-', file
);
3491 fputc (seg
->initprot
& BFD_MACH_O_PROT_EXECUTE
? 'x' : '-', file
);
3492 fprintf (file
, "]\n");
3493 for (j
= 0; j
< seg
->nsects
; j
++)
3495 bfd_mach_o_section
*sec
= &seg
->sections
[j
];
3496 fprintf (file
, "%02u: %-16s %-16s ", ++sec_nbr
,
3497 sec
->segname
, sec
->sectname
);
3498 fprintf_vma (file
, sec
->addr
);
3499 fprintf (file
, " ");
3500 fprintf_vma (file
, sec
->size
);
3501 fprintf (file
, " %08lx\n", sec
->flags
);
3507 bfd_mach_o_print_section (bfd
*abfd ATTRIBUTE_UNUSED
,
3508 bfd_mach_o_section
*sec
, FILE *file
)
3510 fprintf (file
, " Section: %-16s %-16s (bfdname: %s)\n",
3511 sec
->sectname
, sec
->segname
, sec
->bfdsection
->name
);
3512 fprintf (file
, " addr: ");
3513 fprintf_vma (file
, sec
->addr
);
3514 fprintf (file
, " size: ");
3515 fprintf_vma (file
, sec
->size
);
3516 fprintf (file
, " offset: ");
3517 fprintf_vma (file
, sec
->offset
);
3518 fprintf (file
, "\n");
3519 fprintf (file
, " align: %ld", sec
->align
);
3520 fprintf (file
, " nreloc: %lu reloff: ", sec
->nreloc
);
3521 fprintf_vma (file
, sec
->reloff
);
3522 fprintf (file
, "\n");
3523 fprintf (file
, " flags: %08lx (type: %s", sec
->flags
,
3524 bfd_mach_o_get_name (bfd_mach_o_section_type_name
,
3525 sec
->flags
& BFD_MACH_O_SECTION_TYPE_MASK
));
3526 fprintf (file
, " attr: ");
3527 bfd_mach_o_print_flags (bfd_mach_o_section_attribute_name
,
3528 sec
->flags
& BFD_MACH_O_SECTION_ATTRIBUTES_MASK
,
3530 fprintf (file
, ")\n");
3531 switch (sec
->flags
& BFD_MACH_O_SECTION_TYPE_MASK
)
3533 case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS
:
3534 case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS
:
3535 case BFD_MACH_O_S_SYMBOL_STUBS
:
3536 fprintf (file
, " first indirect sym: %lu", sec
->reserved1
);
3537 fprintf (file
, " (%u entries)",
3538 bfd_mach_o_section_get_nbr_indirect (abfd
, sec
));
3541 fprintf (file
, " reserved1: 0x%lx", sec
->reserved1
);
3544 switch (sec
->flags
& BFD_MACH_O_SECTION_TYPE_MASK
)
3546 case BFD_MACH_O_S_SYMBOL_STUBS
:
3547 fprintf (file
, " stub size: %lu", sec
->reserved2
);
3550 fprintf (file
, " reserved2: 0x%lx", sec
->reserved2
);
3553 fprintf (file
, " reserved3: 0x%lx\n", sec
->reserved3
);
3557 bfd_mach_o_print_segment (bfd
*abfd ATTRIBUTE_UNUSED
,
3558 bfd_mach_o_load_command
*cmd
, FILE *file
)
3560 bfd_mach_o_segment_command
*seg
= &cmd
->command
.segment
;
3563 fprintf (file
, " name: %s\n", *seg
->segname
? seg
->segname
: "*none*");
3564 fprintf (file
, " vmaddr: ");
3565 fprintf_vma (file
, seg
->vmaddr
);
3566 fprintf (file
, " vmsize: ");
3567 fprintf_vma (file
, seg
->vmsize
);
3568 fprintf (file
, "\n");
3569 fprintf (file
, " fileoff: ");
3570 fprintf_vma (file
, seg
->fileoff
);
3571 fprintf (file
, " filesize: ");
3572 fprintf_vma (file
, (bfd_vma
)seg
->filesize
);
3573 fprintf (file
, " endoff: ");
3574 fprintf_vma (file
, (bfd_vma
)(seg
->fileoff
+ seg
->filesize
));
3575 fprintf (file
, "\n");
3576 fprintf (file
, " nsects: %lu ", seg
->nsects
);
3577 fprintf (file
, " flags: %lx\n", seg
->flags
);
3578 for (i
= 0; i
< seg
->nsects
; i
++)
3579 bfd_mach_o_print_section (abfd
, &seg
->sections
[i
], file
);
3583 bfd_mach_o_print_dysymtab (bfd
*abfd ATTRIBUTE_UNUSED
,
3584 bfd_mach_o_load_command
*cmd
, FILE *file
)
3586 bfd_mach_o_dysymtab_command
*dysymtab
= &cmd
->command
.dysymtab
;
3587 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
3590 fprintf (file
, " local symbols: idx: %10lu num: %-8lu",
3591 dysymtab
->ilocalsym
, dysymtab
->nlocalsym
);
3592 fprintf (file
, " (nxtidx: %lu)\n",
3593 dysymtab
->ilocalsym
+ dysymtab
->nlocalsym
);
3594 fprintf (file
, " external symbols: idx: %10lu num: %-8lu",
3595 dysymtab
->iextdefsym
, dysymtab
->nextdefsym
);
3596 fprintf (file
, " (nxtidx: %lu)\n",
3597 dysymtab
->iextdefsym
+ dysymtab
->nextdefsym
);
3598 fprintf (file
, " undefined symbols: idx: %10lu num: %-8lu",
3599 dysymtab
->iundefsym
, dysymtab
->nundefsym
);
3600 fprintf (file
, " (nxtidx: %lu)\n",
3601 dysymtab
->iundefsym
+ dysymtab
->nundefsym
);
3602 fprintf (file
, " table of content: off: 0x%08lx num: %-8lu",
3603 dysymtab
->tocoff
, dysymtab
->ntoc
);
3604 fprintf (file
, " (endoff: 0x%08lx)\n",
3606 + dysymtab
->ntoc
* BFD_MACH_O_TABLE_OF_CONTENT_SIZE
);
3607 fprintf (file
, " module table: off: 0x%08lx num: %-8lu",
3608 dysymtab
->modtaboff
, dysymtab
->nmodtab
);
3609 fprintf (file
, " (endoff: 0x%08lx)\n",
3610 dysymtab
->modtaboff
+ dysymtab
->nmodtab
3611 * (mach_o_wide_p (&mdata
->header
) ?
3612 BFD_MACH_O_DYLIB_MODULE_64_SIZE
: BFD_MACH_O_DYLIB_MODULE_SIZE
));
3613 fprintf (file
, " external reference table: off: 0x%08lx num: %-8lu",
3614 dysymtab
->extrefsymoff
, dysymtab
->nextrefsyms
);
3615 fprintf (file
, " (endoff: 0x%08lx)\n",
3616 dysymtab
->extrefsymoff
3617 + dysymtab
->nextrefsyms
* BFD_MACH_O_REFERENCE_SIZE
);
3618 fprintf (file
, " indirect symbol table: off: 0x%08lx num: %-8lu",
3619 dysymtab
->indirectsymoff
, dysymtab
->nindirectsyms
);
3620 fprintf (file
, " (endoff: 0x%08lx)\n",
3621 dysymtab
->indirectsymoff
3622 + dysymtab
->nindirectsyms
* BFD_MACH_O_INDIRECT_SYMBOL_SIZE
);
3623 fprintf (file
, " external relocation table: off: 0x%08lx num: %-8lu",
3624 dysymtab
->extreloff
, dysymtab
->nextrel
);
3625 fprintf (file
, " (endoff: 0x%08lx)\n",
3626 dysymtab
->extreloff
+ dysymtab
->nextrel
* BFD_MACH_O_RELENT_SIZE
);
3627 fprintf (file
, " local relocation table: off: 0x%08lx num: %-8lu",
3628 dysymtab
->locreloff
, dysymtab
->nlocrel
);
3629 fprintf (file
, " (endoff: 0x%08lx)\n",
3630 dysymtab
->locreloff
+ dysymtab
->nlocrel
* BFD_MACH_O_RELENT_SIZE
);
3632 if (dysymtab
->ntoc
> 0
3633 || dysymtab
->nindirectsyms
> 0
3634 || dysymtab
->nextrefsyms
> 0)
3636 /* Try to read the symbols to display the toc or indirect symbols. */
3637 bfd_mach_o_read_symtab_symbols (abfd
);
3639 else if (dysymtab
->nmodtab
> 0)
3641 /* Try to read the strtab to display modules name. */
3642 bfd_mach_o_read_symtab_strtab (abfd
);
3645 for (i
= 0; i
< dysymtab
->nmodtab
; i
++)
3647 bfd_mach_o_dylib_module
*module
= &dysymtab
->dylib_module
[i
];
3648 fprintf (file
, " module %u:\n", i
);
3649 fprintf (file
, " name: %lu", module
->module_name_idx
);
3650 if (mdata
->symtab
&& mdata
->symtab
->strtab
)
3651 fprintf (file
, ": %s",
3652 mdata
->symtab
->strtab
+ module
->module_name_idx
);
3653 fprintf (file
, "\n");
3654 fprintf (file
, " extdefsym: idx: %8lu num: %lu\n",
3655 module
->iextdefsym
, module
->nextdefsym
);
3656 fprintf (file
, " refsym: idx: %8lu num: %lu\n",
3657 module
->irefsym
, module
->nrefsym
);
3658 fprintf (file
, " localsym: idx: %8lu num: %lu\n",
3659 module
->ilocalsym
, module
->nlocalsym
);
3660 fprintf (file
, " extrel: idx: %8lu num: %lu\n",
3661 module
->iextrel
, module
->nextrel
);
3662 fprintf (file
, " init: idx: %8u num: %u\n",
3663 module
->iinit
, module
->ninit
);
3664 fprintf (file
, " term: idx: %8u num: %u\n",
3665 module
->iterm
, module
->nterm
);
3666 fprintf (file
, " objc_module_info: addr: ");
3667 fprintf_vma (file
, module
->objc_module_info_addr
);
3668 fprintf (file
, " size: %lu\n", module
->objc_module_info_size
);
3671 if (dysymtab
->ntoc
> 0)
3673 bfd_mach_o_symtab_command
*symtab
= mdata
->symtab
;
3675 fprintf (file
, " table of content: (symbol/module)\n");
3676 for (i
= 0; i
< dysymtab
->ntoc
; i
++)
3678 bfd_mach_o_dylib_table_of_content
*toc
= &dysymtab
->dylib_toc
[i
];
3680 fprintf (file
, " %4u: ", i
);
3681 if (symtab
&& symtab
->symbols
&& toc
->symbol_index
< symtab
->nsyms
)
3683 const char *name
= symtab
->symbols
[toc
->symbol_index
].symbol
.name
;
3684 fprintf (file
, "%s (%lu)", name
? name
: "*invalid*",
3688 fprintf (file
, "%lu", toc
->symbol_index
);
3690 fprintf (file
, " / ");
3691 if (symtab
&& symtab
->strtab
3692 && toc
->module_index
< dysymtab
->nmodtab
)
3694 bfd_mach_o_dylib_module
*mod
;
3695 mod
= &dysymtab
->dylib_module
[toc
->module_index
];
3696 fprintf (file
, "%s (%lu)",
3697 symtab
->strtab
+ mod
->module_name_idx
,
3701 fprintf (file
, "%lu", toc
->module_index
);
3703 fprintf (file
, "\n");
3707 if (dysymtab
->nindirectsyms
!= 0)
3709 fprintf (file
, " indirect symbols:\n");
3711 for (i
= 0; i
< mdata
->nsects
; i
++)
3713 bfd_mach_o_section
*sec
= mdata
->sections
[i
];
3714 unsigned int j
, first
, last
;
3715 bfd_mach_o_symtab_command
*symtab
= mdata
->symtab
;
3719 switch (sec
->flags
& BFD_MACH_O_SECTION_TYPE_MASK
)
3721 case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS
:
3722 case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS
:
3723 case BFD_MACH_O_S_SYMBOL_STUBS
:
3724 first
= sec
->reserved1
;
3725 last
= first
+ bfd_mach_o_section_get_nbr_indirect (abfd
, sec
);
3727 entry_size
= bfd_mach_o_section_get_entry_size (abfd
, sec
);
3728 fprintf (file
, " for section %s.%s:\n",
3729 sec
->segname
, sec
->sectname
);
3730 for (j
= first
; j
< last
; j
++)
3732 unsigned int isym
= dysymtab
->indirect_syms
[j
];
3734 fprintf (file
, " ");
3735 fprintf_vma (file
, addr
);
3736 fprintf (file
, " %5u: 0x%08x", j
, isym
);
3737 if (isym
& BFD_MACH_O_INDIRECT_SYMBOL_LOCAL
)
3738 fprintf (file
, " LOCAL");
3739 if (isym
& BFD_MACH_O_INDIRECT_SYMBOL_ABS
)
3740 fprintf (file
, " ABSOLUTE");
3741 if (symtab
&& symtab
->symbols
3742 && isym
< symtab
->nsyms
3743 && symtab
->symbols
[isym
].symbol
.name
)
3744 fprintf (file
, " %s", symtab
->symbols
[isym
].symbol
.name
);
3745 fprintf (file
, "\n");
3754 if (dysymtab
->nextrefsyms
> 0)
3756 bfd_mach_o_symtab_command
*symtab
= mdata
->symtab
;
3758 fprintf (file
, " external reference table: (symbol flags)\n");
3759 for (i
= 0; i
< dysymtab
->nextrefsyms
; i
++)
3761 bfd_mach_o_dylib_reference
*ref
= &dysymtab
->ext_refs
[i
];
3763 fprintf (file
, " %4u: %5lu 0x%02lx", i
, ref
->isym
, ref
->flags
);
3764 if (symtab
&& symtab
->symbols
3765 && ref
->isym
< symtab
->nsyms
3766 && symtab
->symbols
[ref
->isym
].symbol
.name
)
3767 fprintf (file
, " %s", symtab
->symbols
[ref
->isym
].symbol
.name
);
3768 fprintf (file
, "\n");
3775 bfd_mach_o_print_dyld_info (bfd
*abfd ATTRIBUTE_UNUSED
,
3776 bfd_mach_o_load_command
*cmd
, FILE *file
)
3778 bfd_mach_o_dyld_info_command
*info
= &cmd
->command
.dyld_info
;
3780 fprintf (file
, " rebase: off: 0x%08x size: %-8u\n",
3781 info
->rebase_off
, info
->rebase_size
);
3782 fprintf (file
, " bind: off: 0x%08x size: %-8u\n",
3783 info
->bind_off
, info
->bind_size
);
3784 fprintf (file
, " weak bind: off: 0x%08x size: %-8u\n",
3785 info
->weak_bind_off
, info
->weak_bind_size
);
3786 fprintf (file
, " lazy bind: off: 0x%08x size: %-8u\n",
3787 info
->lazy_bind_off
, info
->lazy_bind_size
);
3788 fprintf (file
, " export: off: 0x%08x size: %-8u\n",
3789 info
->export_off
, info
->export_size
);
3793 bfd_mach_o_bfd_print_private_bfd_data (bfd
*abfd
, void * ptr
)
3795 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
3796 FILE *file
= (FILE *) ptr
;
3799 bfd_mach_o_print_private_header (abfd
, file
);
3802 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
3804 bfd_mach_o_load_command
*cmd
= &mdata
->commands
[i
];
3806 fprintf (file
, "Load command %s:",
3807 bfd_mach_o_get_name (bfd_mach_o_load_command_name
, cmd
->type
));
3810 case BFD_MACH_O_LC_SEGMENT
:
3811 case BFD_MACH_O_LC_SEGMENT_64
:
3812 bfd_mach_o_print_segment (abfd
, cmd
, file
);
3814 case BFD_MACH_O_LC_UUID
:
3816 bfd_mach_o_uuid_command
*uuid
= &cmd
->command
.uuid
;
3819 for (j
= 0; j
< sizeof (uuid
->uuid
); j
++)
3820 fprintf (file
, " %02x", uuid
->uuid
[j
]);
3824 case BFD_MACH_O_LC_LOAD_DYLIB
:
3825 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB
:
3826 case BFD_MACH_O_LC_REEXPORT_DYLIB
:
3827 case BFD_MACH_O_LC_ID_DYLIB
:
3829 bfd_mach_o_dylib_command
*dylib
= &cmd
->command
.dylib
;
3830 fprintf (file
, " %s\n", dylib
->name_str
);
3831 fprintf (file
, " time stamp: 0x%08lx\n",
3833 fprintf (file
, " current version: 0x%08lx\n",
3834 dylib
->current_version
);
3835 fprintf (file
, " comptibility version: 0x%08lx\n",
3836 dylib
->compatibility_version
);
3839 case BFD_MACH_O_LC_LOAD_DYLINKER
:
3840 case BFD_MACH_O_LC_ID_DYLINKER
:
3841 fprintf (file
, " %s\n", cmd
->command
.dylinker
.name_str
);
3843 case BFD_MACH_O_LC_SYMTAB
:
3845 bfd_mach_o_symtab_command
*symtab
= &cmd
->command
.symtab
;
3848 " symoff: 0x%08x nsyms: %8u (endoff: 0x%08x)\n",
3849 symtab
->symoff
, symtab
->nsyms
,
3850 symtab
->symoff
+ symtab
->nsyms
3851 * (mach_o_wide_p (&mdata
->header
)
3852 ? BFD_MACH_O_NLIST_64_SIZE
: BFD_MACH_O_NLIST_SIZE
));
3854 " stroff: 0x%08x strsize: %8u (endoff: 0x%08x)\n",
3855 symtab
->stroff
, symtab
->strsize
,
3856 symtab
->stroff
+ symtab
->strsize
);
3859 case BFD_MACH_O_LC_DYSYMTAB
:
3860 fprintf (file
, "\n");
3861 bfd_mach_o_print_dysymtab (abfd
, cmd
, file
);
3863 case BFD_MACH_O_LC_CODE_SIGNATURE
:
3864 case BFD_MACH_O_LC_SEGMENT_SPLIT_INFO
:
3866 bfd_mach_o_linkedit_command
*linkedit
= &cmd
->command
.linkedit
;
3869 " dataoff: 0x%08lx datasize: 0x%08lx (endoff: 0x%08lx)\n",
3870 linkedit
->dataoff
, linkedit
->datasize
,
3871 linkedit
->dataoff
+ linkedit
->datasize
);
3874 case BFD_MACH_O_LC_SUB_FRAMEWORK
:
3875 case BFD_MACH_O_LC_SUB_UMBRELLA
:
3876 case BFD_MACH_O_LC_SUB_LIBRARY
:
3877 case BFD_MACH_O_LC_SUB_CLIENT
:
3878 case BFD_MACH_O_LC_RPATH
:
3880 bfd_mach_o_str_command
*str
= &cmd
->command
.str
;
3881 fprintf (file
, " %s\n", str
->str
);
3884 case BFD_MACH_O_LC_THREAD
:
3885 case BFD_MACH_O_LC_UNIXTHREAD
:
3887 bfd_mach_o_thread_command
*thread
= &cmd
->command
.thread
;
3889 bfd_mach_o_backend_data
*bed
= bfd_mach_o_get_backend_data (abfd
);
3891 fprintf (file
, " nflavours: %lu\n", thread
->nflavours
);
3892 for (j
= 0; j
< thread
->nflavours
; j
++)
3894 bfd_mach_o_thread_flavour
*flavour
= &thread
->flavours
[j
];
3896 fprintf (file
, " %2u: flavour: 0x%08lx offset: 0x%08lx"
3898 j
, flavour
->flavour
, flavour
->offset
,
3900 if (bed
->_bfd_mach_o_print_thread
)
3902 char *buf
= bfd_malloc (flavour
->size
);
3905 && bfd_seek (abfd
, flavour
->offset
, SEEK_SET
) == 0
3906 && (bfd_bread (buf
, flavour
->size
, abfd
)
3908 (*bed
->_bfd_mach_o_print_thread
)(abfd
, flavour
,
3915 case BFD_MACH_O_LC_DYLD_INFO
:
3916 fprintf (file
, "\n");
3917 bfd_mach_o_print_dyld_info (abfd
, cmd
, file
);
3920 fprintf (file
, "\n");
3926 bfd_mach_o_print_section_map (abfd
, file
);
3932 bfd_mach_o_core_fetch_environment (bfd
*abfd
,
3933 unsigned char **rbuf
,
3936 bfd_mach_o_data_struct
*mdata
= bfd_mach_o_get_data (abfd
);
3937 unsigned long stackaddr
= bfd_mach_o_stack_addr (mdata
->header
.cputype
);
3940 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
3942 bfd_mach_o_load_command
*cur
= &mdata
->commands
[i
];
3943 bfd_mach_o_segment_command
*seg
= NULL
;
3945 if (cur
->type
!= BFD_MACH_O_LC_SEGMENT
)
3948 seg
= &cur
->command
.segment
;
3950 if ((seg
->vmaddr
+ seg
->vmsize
) == stackaddr
)
3952 unsigned long start
= seg
->fileoff
;
3953 unsigned long end
= seg
->fileoff
+ seg
->filesize
;
3954 unsigned char *buf
= bfd_malloc (1024);
3955 unsigned long size
= 1024;
3959 bfd_size_type nread
= 0;
3960 unsigned long offset
;
3961 int found_nonnull
= 0;
3963 if (size
> (end
- start
))
3964 size
= (end
- start
);
3966 buf
= bfd_realloc_or_free (buf
, size
);
3970 if (bfd_seek (abfd
, end
- size
, SEEK_SET
) != 0)
3976 nread
= bfd_bread (buf
, size
, abfd
);
3984 for (offset
= 4; offset
<= size
; offset
+= 4)
3988 val
= *((unsigned long *) (buf
+ size
- offset
));
3989 if (! found_nonnull
)
3994 else if (val
== 0x0)
3996 unsigned long bottom
;
3999 bottom
= seg
->fileoff
+ seg
->filesize
- offset
;
4000 top
= seg
->fileoff
+ seg
->filesize
- 4;
4001 *rbuf
= bfd_malloc (top
- bottom
);
4002 *rlen
= top
- bottom
;
4004 memcpy (*rbuf
, buf
+ size
- *rlen
, *rlen
);
4010 if (size
== (end
- start
))
4024 bfd_mach_o_core_file_failing_command (bfd
*abfd
)
4026 unsigned char *buf
= NULL
;
4027 unsigned int len
= 0;
4030 ret
= bfd_mach_o_core_fetch_environment (abfd
, &buf
, &len
);
4034 return (char *) buf
;
4038 bfd_mach_o_core_file_failing_signal (bfd
*abfd ATTRIBUTE_UNUSED
)
4043 #define bfd_mach_o_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
4044 #define bfd_mach_o_bfd_reloc_name_lookup _bfd_norelocs_bfd_reloc_name_lookup
4046 #define bfd_mach_o_swap_reloc_in NULL
4047 #define bfd_mach_o_swap_reloc_out NULL
4048 #define bfd_mach_o_print_thread NULL
4050 #define TARGET_NAME mach_o_be_vec
4051 #define TARGET_STRING "mach-o-be"
4052 #define TARGET_ARCHITECTURE bfd_arch_unknown
4053 #define TARGET_BIG_ENDIAN 1
4054 #define TARGET_ARCHIVE 0
4055 #include "mach-o-target.c"
4058 #undef TARGET_STRING
4059 #undef TARGET_ARCHITECTURE
4060 #undef TARGET_BIG_ENDIAN
4061 #undef TARGET_ARCHIVE
4063 #define TARGET_NAME mach_o_le_vec
4064 #define TARGET_STRING "mach-o-le"
4065 #define TARGET_ARCHITECTURE bfd_arch_unknown
4066 #define TARGET_BIG_ENDIAN 0
4067 #define TARGET_ARCHIVE 0
4069 #include "mach-o-target.c"
4072 #undef TARGET_STRING
4073 #undef TARGET_ARCHITECTURE
4074 #undef TARGET_BIG_ENDIAN
4075 #undef TARGET_ARCHIVE
4077 /* Not yet handled: creating an archive. */
4078 #define bfd_mach_o_mkarchive _bfd_noarchive_mkarchive
4081 #define bfd_mach_o_read_ar_hdr _bfd_noarchive_read_ar_hdr
4082 #define bfd_mach_o_write_ar_hdr _bfd_noarchive_write_ar_hdr
4083 #define bfd_mach_o_slurp_armap _bfd_noarchive_slurp_armap
4084 #define bfd_mach_o_slurp_extended_name_table _bfd_noarchive_slurp_extended_name_table
4085 #define bfd_mach_o_construct_extended_name_table _bfd_noarchive_construct_extended_name_table
4086 #define bfd_mach_o_truncate_arname _bfd_noarchive_truncate_arname
4087 #define bfd_mach_o_write_armap _bfd_noarchive_write_armap
4088 #define bfd_mach_o_get_elt_at_index _bfd_noarchive_get_elt_at_index
4089 #define bfd_mach_o_generic_stat_arch_elt _bfd_noarchive_generic_stat_arch_elt
4090 #define bfd_mach_o_update_armap_timestamp _bfd_noarchive_update_armap_timestamp
4092 #define TARGET_NAME mach_o_fat_vec
4093 #define TARGET_STRING "mach-o-fat"
4094 #define TARGET_ARCHITECTURE bfd_arch_unknown
4095 #define TARGET_BIG_ENDIAN 1
4096 #define TARGET_ARCHIVE 1
4098 #include "mach-o-target.c"
4101 #undef TARGET_STRING
4102 #undef TARGET_ARCHITECTURE
4103 #undef TARGET_BIG_ENDIAN
4104 #undef TARGET_ARCHIVE