xref: /openbsd-src/gnu/usr.bin/binutils-2.17/bfd/mach-o.c (revision 3d8817e467ea46cf4772788d6804dd293abfb01a)
1*3d8817e4Smiod /* Mach-O support for BFD.
2*3d8817e4Smiod    Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005
3*3d8817e4Smiod    Free Software Foundation, Inc.
4*3d8817e4Smiod 
5*3d8817e4Smiod    This file is part of BFD, the Binary File Descriptor library.
6*3d8817e4Smiod 
7*3d8817e4Smiod    This program is free software; you can redistribute it and/or modify
8*3d8817e4Smiod    it under the terms of the GNU General Public License as published by
9*3d8817e4Smiod    the Free Software Foundation; either version 2 of the License, or
10*3d8817e4Smiod    (at your option) any later version.
11*3d8817e4Smiod 
12*3d8817e4Smiod    This program is distributed in the hope that it will be useful,
13*3d8817e4Smiod    but WITHOUT ANY WARRANTY; without even the implied warranty of
14*3d8817e4Smiod    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*3d8817e4Smiod    GNU General Public License for more details.
16*3d8817e4Smiod 
17*3d8817e4Smiod    You should have received a copy of the GNU General Public License
18*3d8817e4Smiod    along with this program; if not, write to the Free Software
19*3d8817e4Smiod    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
20*3d8817e4Smiod 
21*3d8817e4Smiod #include "mach-o.h"
22*3d8817e4Smiod #include "bfd.h"
23*3d8817e4Smiod #include "sysdep.h"
24*3d8817e4Smiod #include "libbfd.h"
25*3d8817e4Smiod #include "libiberty.h"
26*3d8817e4Smiod #include <ctype.h>
27*3d8817e4Smiod 
28*3d8817e4Smiod #ifndef BFD_IO_FUNCS
29*3d8817e4Smiod #define BFD_IO_FUNCS 0
30*3d8817e4Smiod #endif
31*3d8817e4Smiod 
32*3d8817e4Smiod #define bfd_mach_o_mkarchive                          _bfd_noarchive_mkarchive
33*3d8817e4Smiod #define bfd_mach_o_read_ar_hdr                        _bfd_noarchive_read_ar_hdr
34*3d8817e4Smiod #define bfd_mach_o_slurp_armap                        _bfd_noarchive_slurp_armap
35*3d8817e4Smiod #define bfd_mach_o_slurp_extended_name_table          _bfd_noarchive_slurp_extended_name_table
36*3d8817e4Smiod #define bfd_mach_o_construct_extended_name_table      _bfd_noarchive_construct_extended_name_table
37*3d8817e4Smiod #define bfd_mach_o_truncate_arname                    _bfd_noarchive_truncate_arname
38*3d8817e4Smiod #define bfd_mach_o_write_armap                        _bfd_noarchive_write_armap
39*3d8817e4Smiod #define bfd_mach_o_get_elt_at_index                   _bfd_noarchive_get_elt_at_index
40*3d8817e4Smiod #define bfd_mach_o_generic_stat_arch_elt              _bfd_noarchive_generic_stat_arch_elt
41*3d8817e4Smiod #define bfd_mach_o_update_armap_timestamp             _bfd_noarchive_update_armap_timestamp
42*3d8817e4Smiod #define	bfd_mach_o_close_and_cleanup                  _bfd_generic_close_and_cleanup
43*3d8817e4Smiod #define bfd_mach_o_bfd_free_cached_info               _bfd_generic_bfd_free_cached_info
44*3d8817e4Smiod #define bfd_mach_o_new_section_hook                   _bfd_generic_new_section_hook
45*3d8817e4Smiod #define bfd_mach_o_get_section_contents_in_window     _bfd_generic_get_section_contents_in_window
46*3d8817e4Smiod #define bfd_mach_o_bfd_is_local_label_name            _bfd_nosymbols_bfd_is_local_label_name
47*3d8817e4Smiod #define bfd_mach_o_bfd_is_target_special_symbol       ((bfd_boolean (*) (bfd *, asymbol *)) bfd_false)
48*3d8817e4Smiod #define bfd_mach_o_bfd_is_local_label_name            _bfd_nosymbols_bfd_is_local_label_name
49*3d8817e4Smiod #define bfd_mach_o_get_lineno                         _bfd_nosymbols_get_lineno
50*3d8817e4Smiod #define bfd_mach_o_find_nearest_line                  _bfd_nosymbols_find_nearest_line
51*3d8817e4Smiod #define bfd_mach_o_find_inliner_info                  _bfd_nosymbols_find_inliner_info
52*3d8817e4Smiod #define bfd_mach_o_bfd_make_debug_symbol              _bfd_nosymbols_bfd_make_debug_symbol
53*3d8817e4Smiod #define bfd_mach_o_read_minisymbols                   _bfd_generic_read_minisymbols
54*3d8817e4Smiod #define bfd_mach_o_minisymbol_to_symbol               _bfd_generic_minisymbol_to_symbol
55*3d8817e4Smiod #define bfd_mach_o_get_reloc_upper_bound              _bfd_norelocs_get_reloc_upper_bound
56*3d8817e4Smiod #define bfd_mach_o_canonicalize_reloc                 _bfd_norelocs_canonicalize_reloc
57*3d8817e4Smiod #define bfd_mach_o_bfd_reloc_type_lookup              _bfd_norelocs_bfd_reloc_type_lookup
58*3d8817e4Smiod #define bfd_mach_o_bfd_get_relocated_section_contents bfd_generic_get_relocated_section_contents
59*3d8817e4Smiod #define bfd_mach_o_bfd_relax_section                  bfd_generic_relax_section
60*3d8817e4Smiod #define bfd_mach_o_bfd_link_hash_table_create         _bfd_generic_link_hash_table_create
61*3d8817e4Smiod #define bfd_mach_o_bfd_link_hash_table_free           _bfd_generic_link_hash_table_free
62*3d8817e4Smiod #define bfd_mach_o_bfd_link_add_symbols               _bfd_generic_link_add_symbols
63*3d8817e4Smiod #define bfd_mach_o_bfd_link_just_syms                 _bfd_generic_link_just_syms
64*3d8817e4Smiod #define bfd_mach_o_bfd_final_link                     _bfd_generic_final_link
65*3d8817e4Smiod #define bfd_mach_o_bfd_link_split_section             _bfd_generic_link_split_section
66*3d8817e4Smiod #define bfd_mach_o_set_arch_mach                      bfd_default_set_arch_mach
67*3d8817e4Smiod #define bfd_mach_o_bfd_merge_private_bfd_data         _bfd_generic_bfd_merge_private_bfd_data
68*3d8817e4Smiod #define bfd_mach_o_bfd_set_private_flags              _bfd_generic_bfd_set_private_flags
69*3d8817e4Smiod #define bfd_mach_o_bfd_print_private_bfd_data         _bfd_generic_bfd_print_private_bfd_data
70*3d8817e4Smiod #define bfd_mach_o_get_section_contents               _bfd_generic_get_section_contents
71*3d8817e4Smiod #define bfd_mach_o_set_section_contents               _bfd_generic_set_section_contents
72*3d8817e4Smiod #define bfd_mach_o_bfd_gc_sections                    bfd_generic_gc_sections
73*3d8817e4Smiod #define bfd_mach_o_bfd_merge_sections                 bfd_generic_merge_sections
74*3d8817e4Smiod #define bfd_mach_o_bfd_is_group_section               bfd_generic_is_group_section
75*3d8817e4Smiod #define bfd_mach_o_bfd_discard_group                  bfd_generic_discard_group
76*3d8817e4Smiod #define bfd_mach_o_section_already_linked             _bfd_generic_section_already_linked
77*3d8817e4Smiod #define bfd_mach_o_bfd_copy_private_header_data       _bfd_generic_bfd_copy_private_header_data
78*3d8817e4Smiod #define bfd_mach_o_core_file_matches_executable_p     generic_core_file_matches_executable_p
79*3d8817e4Smiod 
80*3d8817e4Smiod 
81*3d8817e4Smiod /* The flags field of a section structure is separated into two parts a section
82*3d8817e4Smiod    type and section attributes.  The section types are mutually exclusive (it
83*3d8817e4Smiod    can only have one type) but the section attributes are not (it may have more
84*3d8817e4Smiod    than one attribute).  */
85*3d8817e4Smiod 
86*3d8817e4Smiod #define SECTION_TYPE             0x000000ff     /* 256 section types.  */
87*3d8817e4Smiod #define SECTION_ATTRIBUTES       0xffffff00     /*  24 section attributes.  */
88*3d8817e4Smiod 
89*3d8817e4Smiod /* Constants for the section attributes part of the flags field of a section
90*3d8817e4Smiod    structure.  */
91*3d8817e4Smiod 
92*3d8817e4Smiod #define SECTION_ATTRIBUTES_USR   0xff000000     /* User-settable attributes.  */
93*3d8817e4Smiod #define S_ATTR_PURE_INSTRUCTIONS 0x80000000     /* Section contains only true machine instructions.  */
94*3d8817e4Smiod #define SECTION_ATTRIBUTES_SYS   0x00ffff00     /* System setable attributes.  */
95*3d8817e4Smiod #define S_ATTR_SOME_INSTRUCTIONS 0x00000400     /* Section contains some machine instructions.  */
96*3d8817e4Smiod #define S_ATTR_EXT_RELOC         0x00000200     /* Section has external relocation entries.  */
97*3d8817e4Smiod #define S_ATTR_LOC_RELOC         0x00000100     /* Section has local relocation entries.  */
98*3d8817e4Smiod 
99*3d8817e4Smiod #define N_STAB 0xe0
100*3d8817e4Smiod #define N_TYPE 0x1e
101*3d8817e4Smiod #define N_EXT  0x01
102*3d8817e4Smiod #define N_UNDF 0x0
103*3d8817e4Smiod #define N_ABS  0x2
104*3d8817e4Smiod #define N_SECT 0xe
105*3d8817e4Smiod #define N_INDR 0xa
106*3d8817e4Smiod 
107*3d8817e4Smiod bfd_boolean
bfd_mach_o_valid(bfd * abfd)108*3d8817e4Smiod bfd_mach_o_valid (bfd *abfd)
109*3d8817e4Smiod {
110*3d8817e4Smiod   if (abfd == NULL || abfd->xvec == NULL)
111*3d8817e4Smiod     return 0;
112*3d8817e4Smiod 
113*3d8817e4Smiod   if (! ((abfd->xvec == &mach_o_be_vec)
114*3d8817e4Smiod 	 || (abfd->xvec == &mach_o_le_vec)
115*3d8817e4Smiod 	 || (abfd->xvec == &mach_o_fat_vec)))
116*3d8817e4Smiod     return 0;
117*3d8817e4Smiod 
118*3d8817e4Smiod   if (abfd->tdata.mach_o_data == NULL)
119*3d8817e4Smiod     return 0;
120*3d8817e4Smiod   return 1;
121*3d8817e4Smiod }
122*3d8817e4Smiod 
123*3d8817e4Smiod /* Copy any private info we understand from the input symbol
124*3d8817e4Smiod    to the output symbol.  */
125*3d8817e4Smiod 
126*3d8817e4Smiod static bfd_boolean
bfd_mach_o_bfd_copy_private_symbol_data(bfd * ibfd ATTRIBUTE_UNUSED,asymbol * isymbol ATTRIBUTE_UNUSED,bfd * obfd ATTRIBUTE_UNUSED,asymbol * osymbol ATTRIBUTE_UNUSED)127*3d8817e4Smiod bfd_mach_o_bfd_copy_private_symbol_data (bfd *ibfd ATTRIBUTE_UNUSED,
128*3d8817e4Smiod 					 asymbol *isymbol ATTRIBUTE_UNUSED,
129*3d8817e4Smiod 					 bfd *obfd ATTRIBUTE_UNUSED,
130*3d8817e4Smiod 					 asymbol *osymbol ATTRIBUTE_UNUSED)
131*3d8817e4Smiod {
132*3d8817e4Smiod   return TRUE;
133*3d8817e4Smiod }
134*3d8817e4Smiod 
135*3d8817e4Smiod /* Copy any private info we understand from the input section
136*3d8817e4Smiod    to the output section.  */
137*3d8817e4Smiod 
138*3d8817e4Smiod static bfd_boolean
bfd_mach_o_bfd_copy_private_section_data(bfd * ibfd ATTRIBUTE_UNUSED,asection * isection ATTRIBUTE_UNUSED,bfd * obfd ATTRIBUTE_UNUSED,asection * osection ATTRIBUTE_UNUSED)139*3d8817e4Smiod bfd_mach_o_bfd_copy_private_section_data (bfd *ibfd ATTRIBUTE_UNUSED,
140*3d8817e4Smiod 					  asection *isection ATTRIBUTE_UNUSED,
141*3d8817e4Smiod 					  bfd *obfd ATTRIBUTE_UNUSED,
142*3d8817e4Smiod 					  asection *osection ATTRIBUTE_UNUSED)
143*3d8817e4Smiod {
144*3d8817e4Smiod   return TRUE;
145*3d8817e4Smiod }
146*3d8817e4Smiod 
147*3d8817e4Smiod /* Copy any private info we understand from the input bfd
148*3d8817e4Smiod    to the output bfd.  */
149*3d8817e4Smiod 
150*3d8817e4Smiod static bfd_boolean
bfd_mach_o_bfd_copy_private_bfd_data(bfd * ibfd,bfd * obfd)151*3d8817e4Smiod bfd_mach_o_bfd_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
152*3d8817e4Smiod {
153*3d8817e4Smiod   BFD_ASSERT (bfd_mach_o_valid (ibfd));
154*3d8817e4Smiod   BFD_ASSERT (bfd_mach_o_valid (obfd));
155*3d8817e4Smiod 
156*3d8817e4Smiod   obfd->tdata.mach_o_data = ibfd->tdata.mach_o_data;
157*3d8817e4Smiod   obfd->tdata.mach_o_data->ibfd = ibfd;
158*3d8817e4Smiod   return TRUE;
159*3d8817e4Smiod }
160*3d8817e4Smiod 
161*3d8817e4Smiod static long
bfd_mach_o_count_symbols(bfd * abfd)162*3d8817e4Smiod bfd_mach_o_count_symbols (bfd *abfd)
163*3d8817e4Smiod {
164*3d8817e4Smiod   bfd_mach_o_data_struct *mdata = NULL;
165*3d8817e4Smiod   long nsyms = 0;
166*3d8817e4Smiod   unsigned long i;
167*3d8817e4Smiod 
168*3d8817e4Smiod   BFD_ASSERT (bfd_mach_o_valid (abfd));
169*3d8817e4Smiod   mdata = abfd->tdata.mach_o_data;
170*3d8817e4Smiod 
171*3d8817e4Smiod   for (i = 0; i < mdata->header.ncmds; i++)
172*3d8817e4Smiod     if (mdata->commands[i].type == BFD_MACH_O_LC_SYMTAB)
173*3d8817e4Smiod       {
174*3d8817e4Smiod 	bfd_mach_o_symtab_command *sym = &mdata->commands[i].command.symtab;
175*3d8817e4Smiod 	nsyms += sym->nsyms;
176*3d8817e4Smiod       }
177*3d8817e4Smiod 
178*3d8817e4Smiod   return nsyms;
179*3d8817e4Smiod }
180*3d8817e4Smiod 
181*3d8817e4Smiod static long
bfd_mach_o_get_symtab_upper_bound(bfd * abfd)182*3d8817e4Smiod bfd_mach_o_get_symtab_upper_bound (bfd *abfd)
183*3d8817e4Smiod {
184*3d8817e4Smiod   long nsyms = bfd_mach_o_count_symbols (abfd);
185*3d8817e4Smiod 
186*3d8817e4Smiod   if (nsyms < 0)
187*3d8817e4Smiod     return nsyms;
188*3d8817e4Smiod 
189*3d8817e4Smiod   return ((nsyms + 1) * sizeof (asymbol *));
190*3d8817e4Smiod }
191*3d8817e4Smiod 
192*3d8817e4Smiod static long
bfd_mach_o_canonicalize_symtab(bfd * abfd,asymbol ** alocation)193*3d8817e4Smiod bfd_mach_o_canonicalize_symtab (bfd *abfd, asymbol **alocation)
194*3d8817e4Smiod {
195*3d8817e4Smiod   bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
196*3d8817e4Smiod   long nsyms = bfd_mach_o_count_symbols (abfd);
197*3d8817e4Smiod   asymbol **csym = alocation;
198*3d8817e4Smiod   unsigned long i, j;
199*3d8817e4Smiod 
200*3d8817e4Smiod   if (nsyms < 0)
201*3d8817e4Smiod     return nsyms;
202*3d8817e4Smiod 
203*3d8817e4Smiod   for (i = 0; i < mdata->header.ncmds; i++)
204*3d8817e4Smiod     {
205*3d8817e4Smiod       if (mdata->commands[i].type == BFD_MACH_O_LC_SYMTAB)
206*3d8817e4Smiod 	{
207*3d8817e4Smiod 	  bfd_mach_o_symtab_command *sym = &mdata->commands[i].command.symtab;
208*3d8817e4Smiod 
209*3d8817e4Smiod 	  if (bfd_mach_o_scan_read_symtab_symbols (abfd, &mdata->commands[i].command.symtab) != 0)
210*3d8817e4Smiod 	    {
211*3d8817e4Smiod 	      fprintf (stderr, "bfd_mach_o_canonicalize_symtab: unable to load symbols for section %lu\n", i);
212*3d8817e4Smiod 	      return 0;
213*3d8817e4Smiod 	    }
214*3d8817e4Smiod 
215*3d8817e4Smiod 	  BFD_ASSERT (sym->symbols != NULL);
216*3d8817e4Smiod 
217*3d8817e4Smiod 	  for (j = 0; j < sym->nsyms; j++)
218*3d8817e4Smiod 	    {
219*3d8817e4Smiod 	      BFD_ASSERT (csym < (alocation + nsyms));
220*3d8817e4Smiod 	      *csym++ = &sym->symbols[j];
221*3d8817e4Smiod 	    }
222*3d8817e4Smiod 	}
223*3d8817e4Smiod     }
224*3d8817e4Smiod 
225*3d8817e4Smiod   *csym++ = NULL;
226*3d8817e4Smiod 
227*3d8817e4Smiod   return nsyms;
228*3d8817e4Smiod }
229*3d8817e4Smiod 
230*3d8817e4Smiod static void
bfd_mach_o_get_symbol_info(bfd * abfd ATTRIBUTE_UNUSED,asymbol * symbol,symbol_info * ret)231*3d8817e4Smiod bfd_mach_o_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
232*3d8817e4Smiod 			    asymbol *symbol,
233*3d8817e4Smiod 			    symbol_info *ret)
234*3d8817e4Smiod {
235*3d8817e4Smiod   bfd_symbol_info (symbol, ret);
236*3d8817e4Smiod }
237*3d8817e4Smiod 
238*3d8817e4Smiod static void
bfd_mach_o_print_symbol(bfd * abfd,PTR afile,asymbol * symbol,bfd_print_symbol_type how)239*3d8817e4Smiod bfd_mach_o_print_symbol (bfd *abfd,
240*3d8817e4Smiod 			 PTR afile,
241*3d8817e4Smiod 			 asymbol *symbol,
242*3d8817e4Smiod 			 bfd_print_symbol_type how)
243*3d8817e4Smiod {
244*3d8817e4Smiod   FILE *file = (FILE *) afile;
245*3d8817e4Smiod 
246*3d8817e4Smiod   switch (how)
247*3d8817e4Smiod     {
248*3d8817e4Smiod     case bfd_print_symbol_name:
249*3d8817e4Smiod       fprintf (file, "%s", symbol->name);
250*3d8817e4Smiod       break;
251*3d8817e4Smiod     default:
252*3d8817e4Smiod       bfd_print_symbol_vandf (abfd, (PTR) file, symbol);
253*3d8817e4Smiod       fprintf (file, " %-5s %s", symbol->section->name, symbol->name);
254*3d8817e4Smiod     }
255*3d8817e4Smiod }
256*3d8817e4Smiod 
257*3d8817e4Smiod static void
bfd_mach_o_convert_architecture(bfd_mach_o_cpu_type mtype,bfd_mach_o_cpu_subtype msubtype ATTRIBUTE_UNUSED,enum bfd_architecture * type,unsigned long * subtype)258*3d8817e4Smiod bfd_mach_o_convert_architecture (bfd_mach_o_cpu_type mtype,
259*3d8817e4Smiod 				 bfd_mach_o_cpu_subtype msubtype ATTRIBUTE_UNUSED,
260*3d8817e4Smiod 				 enum bfd_architecture *type,
261*3d8817e4Smiod 				 unsigned long *subtype)
262*3d8817e4Smiod {
263*3d8817e4Smiod   *subtype = bfd_arch_unknown;
264*3d8817e4Smiod 
265*3d8817e4Smiod   switch (mtype)
266*3d8817e4Smiod     {
267*3d8817e4Smiod     case BFD_MACH_O_CPU_TYPE_VAX: *type = bfd_arch_vax; break;
268*3d8817e4Smiod     case BFD_MACH_O_CPU_TYPE_MC680x0: *type = bfd_arch_m68k; break;
269*3d8817e4Smiod     case BFD_MACH_O_CPU_TYPE_I386: *type = bfd_arch_i386; break;
270*3d8817e4Smiod     case BFD_MACH_O_CPU_TYPE_MIPS: *type = bfd_arch_mips; break;
271*3d8817e4Smiod     case BFD_MACH_O_CPU_TYPE_MC98000: *type = bfd_arch_m98k; break;
272*3d8817e4Smiod     case BFD_MACH_O_CPU_TYPE_HPPA: *type = bfd_arch_hppa; break;
273*3d8817e4Smiod     case BFD_MACH_O_CPU_TYPE_ARM: *type = bfd_arch_arm; break;
274*3d8817e4Smiod     case BFD_MACH_O_CPU_TYPE_MC88000: *type = bfd_arch_m88k; break;
275*3d8817e4Smiod     case BFD_MACH_O_CPU_TYPE_SPARC: *type = bfd_arch_sparc; break;
276*3d8817e4Smiod     case BFD_MACH_O_CPU_TYPE_I860: *type = bfd_arch_i860; break;
277*3d8817e4Smiod     case BFD_MACH_O_CPU_TYPE_ALPHA: *type = bfd_arch_alpha; break;
278*3d8817e4Smiod     case BFD_MACH_O_CPU_TYPE_POWERPC: *type = bfd_arch_powerpc; break;
279*3d8817e4Smiod     default: *type = bfd_arch_unknown; break;
280*3d8817e4Smiod     }
281*3d8817e4Smiod 
282*3d8817e4Smiod   switch (*type)
283*3d8817e4Smiod     {
284*3d8817e4Smiod     case bfd_arch_i386: *subtype = bfd_mach_i386_i386; break;
285*3d8817e4Smiod     case bfd_arch_sparc: *subtype = bfd_mach_sparc; break;
286*3d8817e4Smiod     default:
287*3d8817e4Smiod       *subtype = bfd_arch_unknown;
288*3d8817e4Smiod     }
289*3d8817e4Smiod }
290*3d8817e4Smiod 
291*3d8817e4Smiod static int
bfd_mach_o_write_header(bfd * abfd,bfd_mach_o_header * header)292*3d8817e4Smiod bfd_mach_o_write_header (bfd *abfd, bfd_mach_o_header *header)
293*3d8817e4Smiod {
294*3d8817e4Smiod   unsigned char buf[28];
295*3d8817e4Smiod 
296*3d8817e4Smiod   bfd_h_put_32 (abfd, header->magic, buf + 0);
297*3d8817e4Smiod   bfd_h_put_32 (abfd, header->cputype, buf + 4);
298*3d8817e4Smiod   bfd_h_put_32 (abfd, header->cpusubtype, buf + 8);
299*3d8817e4Smiod   bfd_h_put_32 (abfd, header->filetype, buf + 12);
300*3d8817e4Smiod   bfd_h_put_32 (abfd, header->ncmds, buf + 16);
301*3d8817e4Smiod   bfd_h_put_32 (abfd, header->sizeofcmds, buf + 20);
302*3d8817e4Smiod   bfd_h_put_32 (abfd, header->flags, buf + 24);
303*3d8817e4Smiod 
304*3d8817e4Smiod   bfd_seek (abfd, 0, SEEK_SET);
305*3d8817e4Smiod   if (bfd_bwrite ((PTR) buf, 28, abfd) != 28)
306*3d8817e4Smiod     return -1;
307*3d8817e4Smiod 
308*3d8817e4Smiod   return 0;
309*3d8817e4Smiod }
310*3d8817e4Smiod 
311*3d8817e4Smiod static int
bfd_mach_o_scan_write_thread(bfd * abfd,bfd_mach_o_load_command * command)312*3d8817e4Smiod bfd_mach_o_scan_write_thread (bfd *abfd,
313*3d8817e4Smiod 			      bfd_mach_o_load_command *command)
314*3d8817e4Smiod {
315*3d8817e4Smiod   bfd_mach_o_thread_command *cmd = &command->command.thread;
316*3d8817e4Smiod   unsigned int i;
317*3d8817e4Smiod   unsigned char buf[8];
318*3d8817e4Smiod   bfd_vma offset;
319*3d8817e4Smiod   unsigned int nflavours;
320*3d8817e4Smiod 
321*3d8817e4Smiod   BFD_ASSERT ((command->type == BFD_MACH_O_LC_THREAD)
322*3d8817e4Smiod 	      || (command->type == BFD_MACH_O_LC_UNIXTHREAD));
323*3d8817e4Smiod 
324*3d8817e4Smiod   offset = 8;
325*3d8817e4Smiod   nflavours = 0;
326*3d8817e4Smiod   for (i = 0; i < cmd->nflavours; i++)
327*3d8817e4Smiod     {
328*3d8817e4Smiod       BFD_ASSERT ((cmd->flavours[i].size % 4) == 0);
329*3d8817e4Smiod       BFD_ASSERT (cmd->flavours[i].offset == (command->offset + offset + 8));
330*3d8817e4Smiod 
331*3d8817e4Smiod       bfd_h_put_32 (abfd, cmd->flavours[i].flavour, buf);
332*3d8817e4Smiod       bfd_h_put_32 (abfd, (cmd->flavours[i].size / 4), buf + 4);
333*3d8817e4Smiod 
334*3d8817e4Smiod       bfd_seek (abfd, command->offset + offset, SEEK_SET);
335*3d8817e4Smiod       if (bfd_bwrite ((PTR) buf, 8, abfd) != 8)
336*3d8817e4Smiod 	return -1;
337*3d8817e4Smiod 
338*3d8817e4Smiod       offset += cmd->flavours[i].size + 8;
339*3d8817e4Smiod     }
340*3d8817e4Smiod 
341*3d8817e4Smiod   return 0;
342*3d8817e4Smiod }
343*3d8817e4Smiod 
344*3d8817e4Smiod static int
bfd_mach_o_scan_write_section(bfd * abfd,bfd_mach_o_section * section,bfd_vma offset)345*3d8817e4Smiod bfd_mach_o_scan_write_section (bfd *abfd,
346*3d8817e4Smiod 			       bfd_mach_o_section *section,
347*3d8817e4Smiod 			       bfd_vma offset)
348*3d8817e4Smiod {
349*3d8817e4Smiod   unsigned char buf[68];
350*3d8817e4Smiod 
351*3d8817e4Smiod   memcpy (buf, section->sectname, 16);
352*3d8817e4Smiod   memcpy (buf + 16, section->segname, 16);
353*3d8817e4Smiod   bfd_h_put_32 (abfd, section->addr, buf + 32);
354*3d8817e4Smiod   bfd_h_put_32 (abfd, section->size, buf + 36);
355*3d8817e4Smiod   bfd_h_put_32 (abfd, section->offset, buf + 40);
356*3d8817e4Smiod   bfd_h_put_32 (abfd, section->align, buf + 44);
357*3d8817e4Smiod   bfd_h_put_32 (abfd, section->reloff, buf + 48);
358*3d8817e4Smiod   bfd_h_put_32 (abfd, section->nreloc, buf + 52);
359*3d8817e4Smiod   bfd_h_put_32 (abfd, section->flags, buf + 56);
360*3d8817e4Smiod   /* bfd_h_put_32 (abfd, section->reserved1, buf + 60); */
361*3d8817e4Smiod   /* bfd_h_put_32 (abfd, section->reserved2, buf + 64); */
362*3d8817e4Smiod 
363*3d8817e4Smiod   bfd_seek (abfd, offset, SEEK_SET);
364*3d8817e4Smiod   if (bfd_bwrite ((PTR) buf, 68, abfd) != 68)
365*3d8817e4Smiod     return -1;
366*3d8817e4Smiod 
367*3d8817e4Smiod   return 0;
368*3d8817e4Smiod }
369*3d8817e4Smiod 
370*3d8817e4Smiod static int
bfd_mach_o_scan_write_segment(bfd * abfd,bfd_mach_o_load_command * command)371*3d8817e4Smiod bfd_mach_o_scan_write_segment (bfd *abfd, bfd_mach_o_load_command *command)
372*3d8817e4Smiod {
373*3d8817e4Smiod   unsigned char buf[48];
374*3d8817e4Smiod   bfd_mach_o_segment_command *seg = &command->command.segment;
375*3d8817e4Smiod   unsigned long i;
376*3d8817e4Smiod 
377*3d8817e4Smiod   BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT);
378*3d8817e4Smiod 
379*3d8817e4Smiod   memcpy (buf, seg->segname, 16);
380*3d8817e4Smiod   bfd_h_put_32 (abfd, seg->vmaddr, buf + 16);
381*3d8817e4Smiod   bfd_h_put_32 (abfd, seg->vmsize, buf + 20);
382*3d8817e4Smiod   bfd_h_put_32 (abfd, seg->fileoff, buf + 24);
383*3d8817e4Smiod   bfd_h_put_32 (abfd, seg->filesize, buf + 28);
384*3d8817e4Smiod   bfd_h_put_32 (abfd, 0 /* seg->maxprot */, buf + 32);
385*3d8817e4Smiod   bfd_h_put_32 (abfd, 0 /* seg->initprot */, buf + 36);
386*3d8817e4Smiod   bfd_h_put_32 (abfd, seg->nsects, buf + 40);
387*3d8817e4Smiod   bfd_h_put_32 (abfd, seg->flags, buf + 44);
388*3d8817e4Smiod 
389*3d8817e4Smiod   bfd_seek (abfd, command->offset + 8, SEEK_SET);
390*3d8817e4Smiod   if (bfd_bwrite ((PTR) buf, 48, abfd) != 48)
391*3d8817e4Smiod     return -1;
392*3d8817e4Smiod 
393*3d8817e4Smiod   {
394*3d8817e4Smiod     char buf[1024];
395*3d8817e4Smiod     bfd_vma nbytes = seg->filesize;
396*3d8817e4Smiod     bfd_vma curoff = seg->fileoff;
397*3d8817e4Smiod 
398*3d8817e4Smiod     while (nbytes > 0)
399*3d8817e4Smiod       {
400*3d8817e4Smiod 	bfd_vma thisread = nbytes;
401*3d8817e4Smiod 
402*3d8817e4Smiod 	if (thisread > 1024)
403*3d8817e4Smiod 	  thisread = 1024;
404*3d8817e4Smiod 
405*3d8817e4Smiod 	bfd_seek (abfd, curoff, SEEK_SET);
406*3d8817e4Smiod 	if (bfd_bread ((PTR) buf, thisread, abfd) != thisread)
407*3d8817e4Smiod 	  return -1;
408*3d8817e4Smiod 
409*3d8817e4Smiod 	bfd_seek (abfd, curoff, SEEK_SET);
410*3d8817e4Smiod 	if (bfd_bwrite ((PTR) buf, thisread, abfd) != thisread)
411*3d8817e4Smiod 	  return -1;
412*3d8817e4Smiod 
413*3d8817e4Smiod 	nbytes -= thisread;
414*3d8817e4Smiod 	curoff += thisread;
415*3d8817e4Smiod       }
416*3d8817e4Smiod   }
417*3d8817e4Smiod 
418*3d8817e4Smiod   for (i = 0; i < seg->nsects; i++)
419*3d8817e4Smiod     {
420*3d8817e4Smiod       bfd_vma segoff = command->offset + 48 + 8 + (i * 68);
421*3d8817e4Smiod 
422*3d8817e4Smiod       if (bfd_mach_o_scan_write_section (abfd, &seg->sections[i], segoff) != 0)
423*3d8817e4Smiod 	return -1;
424*3d8817e4Smiod     }
425*3d8817e4Smiod 
426*3d8817e4Smiod   return 0;
427*3d8817e4Smiod }
428*3d8817e4Smiod 
429*3d8817e4Smiod static int
bfd_mach_o_scan_write_symtab_symbols(bfd * abfd,bfd_mach_o_load_command * command)430*3d8817e4Smiod bfd_mach_o_scan_write_symtab_symbols (bfd *abfd,
431*3d8817e4Smiod 				      bfd_mach_o_load_command *command)
432*3d8817e4Smiod {
433*3d8817e4Smiod   bfd_mach_o_symtab_command *sym = &command->command.symtab;
434*3d8817e4Smiod   asymbol *s = NULL;
435*3d8817e4Smiod   unsigned long i;
436*3d8817e4Smiod 
437*3d8817e4Smiod   for (i = 0; i < sym->nsyms; i++)
438*3d8817e4Smiod     {
439*3d8817e4Smiod       unsigned char buf[12];
440*3d8817e4Smiod       bfd_vma symoff = sym->symoff + (i * 12);
441*3d8817e4Smiod       unsigned char ntype = 0;
442*3d8817e4Smiod       unsigned char nsect = 0;
443*3d8817e4Smiod       short ndesc = 0;
444*3d8817e4Smiod 
445*3d8817e4Smiod       s = &sym->symbols[i];
446*3d8817e4Smiod 
447*3d8817e4Smiod       /* Instead just set from the stored values.  */
448*3d8817e4Smiod       ntype = (s->udata.i >> 24) & 0xff;
449*3d8817e4Smiod       nsect = (s->udata.i >> 16) & 0xff;
450*3d8817e4Smiod       ndesc = s->udata.i & 0xffff;
451*3d8817e4Smiod 
452*3d8817e4Smiod       bfd_h_put_32 (abfd, s->name - sym->strtab, buf);
453*3d8817e4Smiod       bfd_h_put_8 (abfd, ntype, buf + 4);
454*3d8817e4Smiod       bfd_h_put_8 (abfd, nsect, buf + 5);
455*3d8817e4Smiod       bfd_h_put_16 (abfd, ndesc, buf + 6);
456*3d8817e4Smiod       bfd_h_put_32 (abfd, s->section->vma + s->value, buf + 8);
457*3d8817e4Smiod 
458*3d8817e4Smiod       bfd_seek (abfd, symoff, SEEK_SET);
459*3d8817e4Smiod       if (bfd_bwrite ((PTR) buf, 12, abfd) != 12)
460*3d8817e4Smiod 	{
461*3d8817e4Smiod 	  fprintf (stderr, "bfd_mach_o_scan_write_symtab_symbols: unable to write %d bytes at %lu\n",
462*3d8817e4Smiod 		   12, (unsigned long) symoff);
463*3d8817e4Smiod 	  return -1;
464*3d8817e4Smiod 	}
465*3d8817e4Smiod     }
466*3d8817e4Smiod 
467*3d8817e4Smiod   return 0;
468*3d8817e4Smiod }
469*3d8817e4Smiod 
470*3d8817e4Smiod static int
bfd_mach_o_scan_write_symtab(bfd * abfd,bfd_mach_o_load_command * command)471*3d8817e4Smiod bfd_mach_o_scan_write_symtab (bfd *abfd, bfd_mach_o_load_command *command)
472*3d8817e4Smiod {
473*3d8817e4Smiod   bfd_mach_o_symtab_command *seg = &command->command.symtab;
474*3d8817e4Smiod   unsigned char buf[16];
475*3d8817e4Smiod 
476*3d8817e4Smiod   BFD_ASSERT (command->type == BFD_MACH_O_LC_SYMTAB);
477*3d8817e4Smiod 
478*3d8817e4Smiod   bfd_h_put_32 (abfd, seg->symoff, buf);
479*3d8817e4Smiod   bfd_h_put_32 (abfd, seg->nsyms, buf + 4);
480*3d8817e4Smiod   bfd_h_put_32 (abfd, seg->stroff, buf + 8);
481*3d8817e4Smiod   bfd_h_put_32 (abfd, seg->strsize, buf + 12);
482*3d8817e4Smiod 
483*3d8817e4Smiod   bfd_seek (abfd, command->offset + 8, SEEK_SET);
484*3d8817e4Smiod   if (bfd_bwrite ((PTR) buf, 16, abfd) != 16)
485*3d8817e4Smiod     return -1;
486*3d8817e4Smiod 
487*3d8817e4Smiod   if (bfd_mach_o_scan_write_symtab_symbols (abfd, command) != 0)
488*3d8817e4Smiod     return -1;
489*3d8817e4Smiod 
490*3d8817e4Smiod   return 0;
491*3d8817e4Smiod }
492*3d8817e4Smiod 
493*3d8817e4Smiod static bfd_boolean
bfd_mach_o_write_contents(bfd * abfd)494*3d8817e4Smiod bfd_mach_o_write_contents (bfd *abfd)
495*3d8817e4Smiod {
496*3d8817e4Smiod   unsigned int i;
497*3d8817e4Smiod   asection *s;
498*3d8817e4Smiod 
499*3d8817e4Smiod   bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
500*3d8817e4Smiod 
501*3d8817e4Smiod   /* Write data sections first in case they overlap header data to be
502*3d8817e4Smiod      written later.  */
503*3d8817e4Smiod 
504*3d8817e4Smiod   for (s = abfd->sections; s != (asection *) NULL; s = s->next)
505*3d8817e4Smiod     ;
506*3d8817e4Smiod 
507*3d8817e4Smiod   /* Now write header information.  */
508*3d8817e4Smiod   if (bfd_mach_o_write_header (abfd, &mdata->header) != 0)
509*3d8817e4Smiod     return FALSE;
510*3d8817e4Smiod 
511*3d8817e4Smiod   for (i = 0; i < mdata->header.ncmds; i++)
512*3d8817e4Smiod     {
513*3d8817e4Smiod       unsigned char buf[8];
514*3d8817e4Smiod       bfd_mach_o_load_command *cur = &mdata->commands[i];
515*3d8817e4Smiod       unsigned long typeflag;
516*3d8817e4Smiod 
517*3d8817e4Smiod       typeflag = cur->type_required ? cur->type & BFD_MACH_O_LC_REQ_DYLD : cur->type;
518*3d8817e4Smiod 
519*3d8817e4Smiod       bfd_h_put_32 (abfd, typeflag, buf);
520*3d8817e4Smiod       bfd_h_put_32 (abfd, cur->len, buf + 4);
521*3d8817e4Smiod 
522*3d8817e4Smiod       bfd_seek (abfd, cur->offset, SEEK_SET);
523*3d8817e4Smiod       if (bfd_bwrite ((PTR) buf, 8, abfd) != 8)
524*3d8817e4Smiod 	return FALSE;
525*3d8817e4Smiod 
526*3d8817e4Smiod       switch (cur->type)
527*3d8817e4Smiod 	{
528*3d8817e4Smiod 	case BFD_MACH_O_LC_SEGMENT:
529*3d8817e4Smiod 	  if (bfd_mach_o_scan_write_segment (abfd, cur) != 0)
530*3d8817e4Smiod 	    return FALSE;
531*3d8817e4Smiod 	  break;
532*3d8817e4Smiod 	case BFD_MACH_O_LC_SYMTAB:
533*3d8817e4Smiod 	  if (bfd_mach_o_scan_write_symtab (abfd, cur) != 0)
534*3d8817e4Smiod 	    return FALSE;
535*3d8817e4Smiod 	  break;
536*3d8817e4Smiod 	case BFD_MACH_O_LC_SYMSEG:
537*3d8817e4Smiod 	  break;
538*3d8817e4Smiod 	case BFD_MACH_O_LC_THREAD:
539*3d8817e4Smiod 	case BFD_MACH_O_LC_UNIXTHREAD:
540*3d8817e4Smiod 	  if (bfd_mach_o_scan_write_thread (abfd, cur) != 0)
541*3d8817e4Smiod 	    return FALSE;
542*3d8817e4Smiod 	  break;
543*3d8817e4Smiod 	case BFD_MACH_O_LC_LOADFVMLIB:
544*3d8817e4Smiod 	case BFD_MACH_O_LC_IDFVMLIB:
545*3d8817e4Smiod 	case BFD_MACH_O_LC_IDENT:
546*3d8817e4Smiod 	case BFD_MACH_O_LC_FVMFILE:
547*3d8817e4Smiod 	case BFD_MACH_O_LC_PREPAGE:
548*3d8817e4Smiod 	case BFD_MACH_O_LC_DYSYMTAB:
549*3d8817e4Smiod 	case BFD_MACH_O_LC_LOAD_DYLIB:
550*3d8817e4Smiod 	case BFD_MACH_O_LC_LOAD_WEAK_DYLIB:
551*3d8817e4Smiod 	case BFD_MACH_O_LC_ID_DYLIB:
552*3d8817e4Smiod 	case BFD_MACH_O_LC_LOAD_DYLINKER:
553*3d8817e4Smiod 	case BFD_MACH_O_LC_ID_DYLINKER:
554*3d8817e4Smiod 	case BFD_MACH_O_LC_PREBOUND_DYLIB:
555*3d8817e4Smiod 	case BFD_MACH_O_LC_ROUTINES:
556*3d8817e4Smiod 	case BFD_MACH_O_LC_SUB_FRAMEWORK:
557*3d8817e4Smiod 	  break;
558*3d8817e4Smiod 	default:
559*3d8817e4Smiod 	  fprintf (stderr,
560*3d8817e4Smiod 		   "unable to write unknown load command 0x%lx\n",
561*3d8817e4Smiod 		   (long) cur->type);
562*3d8817e4Smiod 	  return FALSE;
563*3d8817e4Smiod 	}
564*3d8817e4Smiod     }
565*3d8817e4Smiod 
566*3d8817e4Smiod   return TRUE;
567*3d8817e4Smiod }
568*3d8817e4Smiod 
569*3d8817e4Smiod static int
bfd_mach_o_sizeof_headers(bfd * a ATTRIBUTE_UNUSED,bfd_boolean b ATTRIBUTE_UNUSED)570*3d8817e4Smiod bfd_mach_o_sizeof_headers (bfd *a ATTRIBUTE_UNUSED,
571*3d8817e4Smiod 			   bfd_boolean b ATTRIBUTE_UNUSED)
572*3d8817e4Smiod {
573*3d8817e4Smiod   return 0;
574*3d8817e4Smiod }
575*3d8817e4Smiod 
576*3d8817e4Smiod /* Make an empty symbol.  This is required only because
577*3d8817e4Smiod    bfd_make_section_anyway wants to create a symbol for the section.  */
578*3d8817e4Smiod 
579*3d8817e4Smiod static asymbol *
bfd_mach_o_make_empty_symbol(bfd * abfd)580*3d8817e4Smiod bfd_mach_o_make_empty_symbol (bfd *abfd)
581*3d8817e4Smiod {
582*3d8817e4Smiod   asymbol *new;
583*3d8817e4Smiod 
584*3d8817e4Smiod   new = bfd_zalloc (abfd, sizeof (* new));
585*3d8817e4Smiod   if (new == NULL)
586*3d8817e4Smiod     return new;
587*3d8817e4Smiod   new->the_bfd = abfd;
588*3d8817e4Smiod   return new;
589*3d8817e4Smiod }
590*3d8817e4Smiod 
591*3d8817e4Smiod static int
bfd_mach_o_read_header(bfd * abfd,bfd_mach_o_header * header)592*3d8817e4Smiod bfd_mach_o_read_header (bfd *abfd, bfd_mach_o_header *header)
593*3d8817e4Smiod {
594*3d8817e4Smiod   unsigned char buf[28];
595*3d8817e4Smiod   bfd_vma (*get32) (const void *) = NULL;
596*3d8817e4Smiod 
597*3d8817e4Smiod   bfd_seek (abfd, 0, SEEK_SET);
598*3d8817e4Smiod 
599*3d8817e4Smiod   if (bfd_bread ((PTR) buf, 28, abfd) != 28)
600*3d8817e4Smiod     return -1;
601*3d8817e4Smiod 
602*3d8817e4Smiod   if (bfd_getb32 (buf) == 0xfeedface)
603*3d8817e4Smiod     {
604*3d8817e4Smiod       header->byteorder = BFD_ENDIAN_BIG;
605*3d8817e4Smiod       header->magic = 0xfeedface;
606*3d8817e4Smiod       get32 = bfd_getb32;
607*3d8817e4Smiod     }
608*3d8817e4Smiod   else if (bfd_getl32 (buf) == 0xfeedface)
609*3d8817e4Smiod     {
610*3d8817e4Smiod       header->byteorder = BFD_ENDIAN_LITTLE;
611*3d8817e4Smiod       header->magic = 0xfeedface;
612*3d8817e4Smiod       get32 = bfd_getl32;
613*3d8817e4Smiod     }
614*3d8817e4Smiod   else
615*3d8817e4Smiod     {
616*3d8817e4Smiod       header->byteorder = BFD_ENDIAN_UNKNOWN;
617*3d8817e4Smiod       return -1;
618*3d8817e4Smiod     }
619*3d8817e4Smiod 
620*3d8817e4Smiod   header->cputype = (*get32) (buf + 4);
621*3d8817e4Smiod   header->cpusubtype = (*get32) (buf + 8);
622*3d8817e4Smiod   header->filetype = (*get32) (buf + 12);
623*3d8817e4Smiod   header->ncmds = (*get32) (buf + 16);
624*3d8817e4Smiod   header->sizeofcmds = (*get32) (buf + 20);
625*3d8817e4Smiod   header->flags = (*get32) (buf + 24);
626*3d8817e4Smiod 
627*3d8817e4Smiod   return 0;
628*3d8817e4Smiod }
629*3d8817e4Smiod 
630*3d8817e4Smiod static asection *
bfd_mach_o_make_bfd_section(bfd * abfd,bfd_mach_o_section * section)631*3d8817e4Smiod bfd_mach_o_make_bfd_section (bfd *abfd, bfd_mach_o_section *section)
632*3d8817e4Smiod {
633*3d8817e4Smiod   asection *bfdsec;
634*3d8817e4Smiod   char *sname;
635*3d8817e4Smiod   const char *prefix = "LC_SEGMENT";
636*3d8817e4Smiod   unsigned int snamelen;
637*3d8817e4Smiod 
638*3d8817e4Smiod   snamelen = strlen (prefix) + 1
639*3d8817e4Smiod     + strlen (section->segname) + 1
640*3d8817e4Smiod     + strlen (section->sectname) + 1;
641*3d8817e4Smiod 
642*3d8817e4Smiod   sname = bfd_alloc (abfd, snamelen);
643*3d8817e4Smiod   if (sname == NULL)
644*3d8817e4Smiod     return NULL;
645*3d8817e4Smiod   sprintf (sname, "%s.%s.%s", prefix, section->segname, section->sectname);
646*3d8817e4Smiod 
647*3d8817e4Smiod   bfdsec = bfd_make_section_anyway (abfd, sname);
648*3d8817e4Smiod   if (bfdsec == NULL)
649*3d8817e4Smiod     return NULL;
650*3d8817e4Smiod 
651*3d8817e4Smiod   bfdsec->vma = section->addr;
652*3d8817e4Smiod   bfdsec->lma = section->addr;
653*3d8817e4Smiod   bfdsec->size = section->size;
654*3d8817e4Smiod   bfdsec->filepos = section->offset;
655*3d8817e4Smiod   bfdsec->alignment_power = section->align;
656*3d8817e4Smiod 
657*3d8817e4Smiod   if (section->flags & BFD_MACH_O_S_ZEROFILL)
658*3d8817e4Smiod     bfdsec->flags = SEC_ALLOC;
659*3d8817e4Smiod   else
660*3d8817e4Smiod     bfdsec->flags = SEC_HAS_CONTENTS | SEC_LOAD | SEC_ALLOC | SEC_CODE;
661*3d8817e4Smiod 
662*3d8817e4Smiod   return bfdsec;
663*3d8817e4Smiod }
664*3d8817e4Smiod 
665*3d8817e4Smiod static int
bfd_mach_o_scan_read_section(bfd * abfd,bfd_mach_o_section * section,bfd_vma offset)666*3d8817e4Smiod bfd_mach_o_scan_read_section (bfd *abfd,
667*3d8817e4Smiod 			      bfd_mach_o_section *section,
668*3d8817e4Smiod 			      bfd_vma offset)
669*3d8817e4Smiod {
670*3d8817e4Smiod   unsigned char buf[68];
671*3d8817e4Smiod 
672*3d8817e4Smiod   bfd_seek (abfd, offset, SEEK_SET);
673*3d8817e4Smiod   if (bfd_bread ((PTR) buf, 68, abfd) != 68)
674*3d8817e4Smiod     return -1;
675*3d8817e4Smiod 
676*3d8817e4Smiod   memcpy (section->sectname, buf, 16);
677*3d8817e4Smiod   section->sectname[16] = '\0';
678*3d8817e4Smiod   memcpy (section->segname, buf + 16, 16);
679*3d8817e4Smiod   section->segname[16] = '\0';
680*3d8817e4Smiod   section->addr = bfd_h_get_32 (abfd, buf + 32);
681*3d8817e4Smiod   section->size = bfd_h_get_32 (abfd, buf + 36);
682*3d8817e4Smiod   section->offset = bfd_h_get_32 (abfd, buf + 40);
683*3d8817e4Smiod   section->align = bfd_h_get_32 (abfd, buf + 44);
684*3d8817e4Smiod   section->reloff = bfd_h_get_32 (abfd, buf + 48);
685*3d8817e4Smiod   section->nreloc = bfd_h_get_32 (abfd, buf + 52);
686*3d8817e4Smiod   section->flags = bfd_h_get_32 (abfd, buf + 56);
687*3d8817e4Smiod   section->reserved1 = bfd_h_get_32 (abfd, buf + 60);
688*3d8817e4Smiod   section->reserved2 = bfd_h_get_32 (abfd, buf + 64);
689*3d8817e4Smiod   section->bfdsection = bfd_mach_o_make_bfd_section (abfd, section);
690*3d8817e4Smiod 
691*3d8817e4Smiod   if (section->bfdsection == NULL)
692*3d8817e4Smiod     return -1;
693*3d8817e4Smiod 
694*3d8817e4Smiod   return 0;
695*3d8817e4Smiod }
696*3d8817e4Smiod 
697*3d8817e4Smiod int
bfd_mach_o_scan_read_symtab_symbol(bfd * abfd,bfd_mach_o_symtab_command * sym,asymbol * s,unsigned long i)698*3d8817e4Smiod bfd_mach_o_scan_read_symtab_symbol (bfd *abfd,
699*3d8817e4Smiod 				    bfd_mach_o_symtab_command *sym,
700*3d8817e4Smiod 				    asymbol *s,
701*3d8817e4Smiod 				    unsigned long i)
702*3d8817e4Smiod {
703*3d8817e4Smiod   bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
704*3d8817e4Smiod   bfd_vma symoff = sym->symoff + (i * 12);
705*3d8817e4Smiod   unsigned char buf[12];
706*3d8817e4Smiod   unsigned char type = -1;
707*3d8817e4Smiod   unsigned char section = -1;
708*3d8817e4Smiod   short desc = -1;
709*3d8817e4Smiod   unsigned long value = -1;
710*3d8817e4Smiod   unsigned long stroff = -1;
711*3d8817e4Smiod   unsigned int symtype = -1;
712*3d8817e4Smiod 
713*3d8817e4Smiod   BFD_ASSERT (sym->strtab != NULL);
714*3d8817e4Smiod 
715*3d8817e4Smiod   bfd_seek (abfd, symoff, SEEK_SET);
716*3d8817e4Smiod   if (bfd_bread ((PTR) buf, 12, abfd) != 12)
717*3d8817e4Smiod     {
718*3d8817e4Smiod       fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: unable to read %d bytes at %lu\n",
719*3d8817e4Smiod 	       12, (unsigned long) symoff);
720*3d8817e4Smiod       return -1;
721*3d8817e4Smiod     }
722*3d8817e4Smiod 
723*3d8817e4Smiod   stroff = bfd_h_get_32 (abfd, buf);
724*3d8817e4Smiod   type = bfd_h_get_8 (abfd, buf + 4);
725*3d8817e4Smiod   symtype = (type & 0x0e);
726*3d8817e4Smiod   section = bfd_h_get_8 (abfd, buf + 5) - 1;
727*3d8817e4Smiod   desc = bfd_h_get_16 (abfd, buf + 6);
728*3d8817e4Smiod   value = bfd_h_get_32 (abfd, buf + 8);
729*3d8817e4Smiod 
730*3d8817e4Smiod   if (stroff >= sym->strsize)
731*3d8817e4Smiod     {
732*3d8817e4Smiod       fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: symbol name out of range (%lu >= %lu)\n",
733*3d8817e4Smiod 	       (unsigned long) stroff, (unsigned long) sym->strsize);
734*3d8817e4Smiod       return -1;
735*3d8817e4Smiod     }
736*3d8817e4Smiod 
737*3d8817e4Smiod   s->the_bfd = abfd;
738*3d8817e4Smiod   s->name = sym->strtab + stroff;
739*3d8817e4Smiod   s->value = value;
740*3d8817e4Smiod   s->udata.i = (type << 24) | (section << 16) | desc;
741*3d8817e4Smiod   s->flags = 0x0;
742*3d8817e4Smiod 
743*3d8817e4Smiod   if (type & BFD_MACH_O_N_STAB)
744*3d8817e4Smiod     {
745*3d8817e4Smiod       s->flags |= BSF_DEBUGGING;
746*3d8817e4Smiod       s->section = bfd_und_section_ptr;
747*3d8817e4Smiod     }
748*3d8817e4Smiod   else
749*3d8817e4Smiod     {
750*3d8817e4Smiod       if (type & BFD_MACH_O_N_PEXT)
751*3d8817e4Smiod 	{
752*3d8817e4Smiod 	  type &= ~BFD_MACH_O_N_PEXT;
753*3d8817e4Smiod 	  s->flags |= BSF_GLOBAL;
754*3d8817e4Smiod 	}
755*3d8817e4Smiod 
756*3d8817e4Smiod       if (type & BFD_MACH_O_N_EXT)
757*3d8817e4Smiod 	{
758*3d8817e4Smiod 	  type &= ~BFD_MACH_O_N_EXT;
759*3d8817e4Smiod 	  s->flags |= BSF_GLOBAL;
760*3d8817e4Smiod 	}
761*3d8817e4Smiod 
762*3d8817e4Smiod       switch (symtype)
763*3d8817e4Smiod 	{
764*3d8817e4Smiod 	case BFD_MACH_O_N_UNDF:
765*3d8817e4Smiod 	  s->section = bfd_und_section_ptr;
766*3d8817e4Smiod 	  break;
767*3d8817e4Smiod 	case BFD_MACH_O_N_PBUD:
768*3d8817e4Smiod 	  s->section = bfd_und_section_ptr;
769*3d8817e4Smiod 	  break;
770*3d8817e4Smiod 	case BFD_MACH_O_N_ABS:
771*3d8817e4Smiod 	  s->section = bfd_abs_section_ptr;
772*3d8817e4Smiod 	  break;
773*3d8817e4Smiod 	case BFD_MACH_O_N_SECT:
774*3d8817e4Smiod 	  if ((section > 0) && (section <= mdata->nsects))
775*3d8817e4Smiod 	    {
776*3d8817e4Smiod 	      s->section = mdata->sections[section - 1]->bfdsection;
777*3d8817e4Smiod 	      s->value = s->value - mdata->sections[section - 1]->addr;
778*3d8817e4Smiod 	    }
779*3d8817e4Smiod 	  else
780*3d8817e4Smiod 	    {
781*3d8817e4Smiod 	      /* Mach-O uses 0 to mean "no section"; not an error.  */
782*3d8817e4Smiod 	      if (section != 0)
783*3d8817e4Smiod 		{
784*3d8817e4Smiod 		  fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: "
785*3d8817e4Smiod 			   "symbol \"%s\" specified invalid section %d (max %lu): setting to undefined\n",
786*3d8817e4Smiod 			   s->name, section, mdata->nsects);
787*3d8817e4Smiod 		}
788*3d8817e4Smiod 	      s->section = bfd_und_section_ptr;
789*3d8817e4Smiod 	    }
790*3d8817e4Smiod 	  break;
791*3d8817e4Smiod 	case BFD_MACH_O_N_INDR:
792*3d8817e4Smiod 	  fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: "
793*3d8817e4Smiod 		   "symbol \"%s\" is unsupported 'indirect' reference: setting to undefined\n",
794*3d8817e4Smiod 		   s->name);
795*3d8817e4Smiod 	  s->section = bfd_und_section_ptr;
796*3d8817e4Smiod 	  break;
797*3d8817e4Smiod 	default:
798*3d8817e4Smiod 	  fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: "
799*3d8817e4Smiod 		   "symbol \"%s\" specified invalid type field 0x%x: setting to undefined\n",
800*3d8817e4Smiod 		   s->name, symtype);
801*3d8817e4Smiod 	  s->section = bfd_und_section_ptr;
802*3d8817e4Smiod 	  break;
803*3d8817e4Smiod 	}
804*3d8817e4Smiod     }
805*3d8817e4Smiod 
806*3d8817e4Smiod   return 0;
807*3d8817e4Smiod }
808*3d8817e4Smiod 
809*3d8817e4Smiod int
bfd_mach_o_scan_read_symtab_strtab(bfd * abfd,bfd_mach_o_symtab_command * sym)810*3d8817e4Smiod bfd_mach_o_scan_read_symtab_strtab (bfd *abfd,
811*3d8817e4Smiod 				    bfd_mach_o_symtab_command *sym)
812*3d8817e4Smiod {
813*3d8817e4Smiod   BFD_ASSERT (sym->strtab == NULL);
814*3d8817e4Smiod 
815*3d8817e4Smiod   if (abfd->flags & BFD_IN_MEMORY)
816*3d8817e4Smiod     {
817*3d8817e4Smiod       struct bfd_in_memory *b;
818*3d8817e4Smiod 
819*3d8817e4Smiod       b = (struct bfd_in_memory *) abfd->iostream;
820*3d8817e4Smiod 
821*3d8817e4Smiod       if ((sym->stroff + sym->strsize) > b->size)
822*3d8817e4Smiod 	{
823*3d8817e4Smiod 	  bfd_set_error (bfd_error_file_truncated);
824*3d8817e4Smiod 	  return -1;
825*3d8817e4Smiod 	}
826*3d8817e4Smiod       sym->strtab = (char *) b->buffer + sym->stroff;
827*3d8817e4Smiod       return 0;
828*3d8817e4Smiod     }
829*3d8817e4Smiod 
830*3d8817e4Smiod   sym->strtab = bfd_alloc (abfd, sym->strsize);
831*3d8817e4Smiod   if (sym->strtab == NULL)
832*3d8817e4Smiod     return -1;
833*3d8817e4Smiod 
834*3d8817e4Smiod   bfd_seek (abfd, sym->stroff, SEEK_SET);
835*3d8817e4Smiod   if (bfd_bread ((PTR) sym->strtab, sym->strsize, abfd) != sym->strsize)
836*3d8817e4Smiod     {
837*3d8817e4Smiod       fprintf (stderr, "bfd_mach_o_scan_read_symtab_strtab: unable to read %lu bytes at %lu\n",
838*3d8817e4Smiod 	       sym->strsize, sym->stroff);
839*3d8817e4Smiod       return -1;
840*3d8817e4Smiod     }
841*3d8817e4Smiod 
842*3d8817e4Smiod   return 0;
843*3d8817e4Smiod }
844*3d8817e4Smiod 
845*3d8817e4Smiod int
bfd_mach_o_scan_read_symtab_symbols(bfd * abfd,bfd_mach_o_symtab_command * sym)846*3d8817e4Smiod bfd_mach_o_scan_read_symtab_symbols (bfd *abfd,
847*3d8817e4Smiod 				     bfd_mach_o_symtab_command *sym)
848*3d8817e4Smiod {
849*3d8817e4Smiod   unsigned long i;
850*3d8817e4Smiod   int ret;
851*3d8817e4Smiod 
852*3d8817e4Smiod   BFD_ASSERT (sym->symbols == NULL);
853*3d8817e4Smiod   sym->symbols = bfd_alloc (abfd, sym->nsyms * sizeof (asymbol));
854*3d8817e4Smiod 
855*3d8817e4Smiod   if (sym->symbols == NULL)
856*3d8817e4Smiod     {
857*3d8817e4Smiod       fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbols: unable to allocate memory for symbols\n");
858*3d8817e4Smiod       return -1;
859*3d8817e4Smiod     }
860*3d8817e4Smiod 
861*3d8817e4Smiod   ret = bfd_mach_o_scan_read_symtab_strtab (abfd, sym);
862*3d8817e4Smiod   if (ret != 0)
863*3d8817e4Smiod     return ret;
864*3d8817e4Smiod 
865*3d8817e4Smiod   for (i = 0; i < sym->nsyms; i++)
866*3d8817e4Smiod     {
867*3d8817e4Smiod       ret = bfd_mach_o_scan_read_symtab_symbol (abfd, sym, &sym->symbols[i], i);
868*3d8817e4Smiod       if (ret != 0)
869*3d8817e4Smiod 	return ret;
870*3d8817e4Smiod     }
871*3d8817e4Smiod 
872*3d8817e4Smiod   return 0;
873*3d8817e4Smiod }
874*3d8817e4Smiod 
875*3d8817e4Smiod int
bfd_mach_o_scan_read_dysymtab_symbol(bfd * abfd,bfd_mach_o_dysymtab_command * dysym,bfd_mach_o_symtab_command * sym,asymbol * s,unsigned long i)876*3d8817e4Smiod bfd_mach_o_scan_read_dysymtab_symbol (bfd *abfd,
877*3d8817e4Smiod 				      bfd_mach_o_dysymtab_command *dysym,
878*3d8817e4Smiod 				      bfd_mach_o_symtab_command *sym,
879*3d8817e4Smiod 				      asymbol *s,
880*3d8817e4Smiod 				      unsigned long i)
881*3d8817e4Smiod {
882*3d8817e4Smiod   unsigned long isymoff = dysym->indirectsymoff + (i * 4);
883*3d8817e4Smiod   unsigned long symindex;
884*3d8817e4Smiod   unsigned char buf[4];
885*3d8817e4Smiod 
886*3d8817e4Smiod   BFD_ASSERT (i < dysym->nindirectsyms);
887*3d8817e4Smiod 
888*3d8817e4Smiod   bfd_seek (abfd, isymoff, SEEK_SET);
889*3d8817e4Smiod   if (bfd_bread ((PTR) buf, 4, abfd) != 4)
890*3d8817e4Smiod     {
891*3d8817e4Smiod       fprintf (stderr, "bfd_mach_o_scan_read_dysymtab_symbol: unable to read %lu bytes at %lu\n",
892*3d8817e4Smiod 	       (unsigned long) 4, isymoff);
893*3d8817e4Smiod       return -1;
894*3d8817e4Smiod     }
895*3d8817e4Smiod   symindex = bfd_h_get_32 (abfd, buf);
896*3d8817e4Smiod 
897*3d8817e4Smiod   return bfd_mach_o_scan_read_symtab_symbol (abfd, sym, s, symindex);
898*3d8817e4Smiod }
899*3d8817e4Smiod 
900*3d8817e4Smiod static const char *
bfd_mach_o_i386_flavour_string(unsigned int flavour)901*3d8817e4Smiod bfd_mach_o_i386_flavour_string (unsigned int flavour)
902*3d8817e4Smiod {
903*3d8817e4Smiod   switch ((int) flavour)
904*3d8817e4Smiod     {
905*3d8817e4Smiod     case BFD_MACH_O_i386_NEW_THREAD_STATE: return "i386_NEW_THREAD_STATE";
906*3d8817e4Smiod     case BFD_MACH_O_i386_FLOAT_STATE: return "i386_FLOAT_STATE";
907*3d8817e4Smiod     case BFD_MACH_O_i386_ISA_PORT_MAP_STATE: return "i386_ISA_PORT_MAP_STATE";
908*3d8817e4Smiod     case BFD_MACH_O_i386_V86_ASSIST_STATE: return "i386_V86_ASSIST_STATE";
909*3d8817e4Smiod     case BFD_MACH_O_i386_REGS_SEGS_STATE: return "i386_REGS_SEGS_STATE";
910*3d8817e4Smiod     case BFD_MACH_O_i386_THREAD_SYSCALL_STATE: return "i386_THREAD_SYSCALL_STATE";
911*3d8817e4Smiod     case BFD_MACH_O_i386_THREAD_STATE_NONE: return "i386_THREAD_STATE_NONE";
912*3d8817e4Smiod     case BFD_MACH_O_i386_SAVED_STATE: return "i386_SAVED_STATE";
913*3d8817e4Smiod     case BFD_MACH_O_i386_THREAD_STATE: return "i386_THREAD_STATE";
914*3d8817e4Smiod     case BFD_MACH_O_i386_THREAD_FPSTATE: return "i386_THREAD_FPSTATE";
915*3d8817e4Smiod     case BFD_MACH_O_i386_THREAD_EXCEPTSTATE: return "i386_THREAD_EXCEPTSTATE";
916*3d8817e4Smiod     case BFD_MACH_O_i386_THREAD_CTHREADSTATE: return "i386_THREAD_CTHREADSTATE";
917*3d8817e4Smiod     default: return "UNKNOWN";
918*3d8817e4Smiod     }
919*3d8817e4Smiod }
920*3d8817e4Smiod 
921*3d8817e4Smiod static const char *
bfd_mach_o_ppc_flavour_string(unsigned int flavour)922*3d8817e4Smiod bfd_mach_o_ppc_flavour_string (unsigned int flavour)
923*3d8817e4Smiod {
924*3d8817e4Smiod   switch ((int) flavour)
925*3d8817e4Smiod     {
926*3d8817e4Smiod     case BFD_MACH_O_PPC_THREAD_STATE: return "PPC_THREAD_STATE";
927*3d8817e4Smiod     case BFD_MACH_O_PPC_FLOAT_STATE: return "PPC_FLOAT_STATE";
928*3d8817e4Smiod     case BFD_MACH_O_PPC_EXCEPTION_STATE: return "PPC_EXCEPTION_STATE";
929*3d8817e4Smiod     case BFD_MACH_O_PPC_VECTOR_STATE: return "PPC_VECTOR_STATE";
930*3d8817e4Smiod     default: return "UNKNOWN";
931*3d8817e4Smiod     }
932*3d8817e4Smiod }
933*3d8817e4Smiod 
934*3d8817e4Smiod static int
bfd_mach_o_scan_read_dylinker(bfd * abfd,bfd_mach_o_load_command * command)935*3d8817e4Smiod bfd_mach_o_scan_read_dylinker (bfd *abfd,
936*3d8817e4Smiod 			       bfd_mach_o_load_command *command)
937*3d8817e4Smiod {
938*3d8817e4Smiod   bfd_mach_o_dylinker_command *cmd = &command->command.dylinker;
939*3d8817e4Smiod   unsigned char buf[4];
940*3d8817e4Smiod   unsigned int nameoff;
941*3d8817e4Smiod   asection *bfdsec;
942*3d8817e4Smiod   char *sname;
943*3d8817e4Smiod   const char *prefix;
944*3d8817e4Smiod 
945*3d8817e4Smiod   BFD_ASSERT ((command->type == BFD_MACH_O_LC_ID_DYLINKER)
946*3d8817e4Smiod 	      || (command->type == BFD_MACH_O_LC_LOAD_DYLINKER));
947*3d8817e4Smiod 
948*3d8817e4Smiod   bfd_seek (abfd, command->offset + 8, SEEK_SET);
949*3d8817e4Smiod   if (bfd_bread ((PTR) buf, 4, abfd) != 4)
950*3d8817e4Smiod     return -1;
951*3d8817e4Smiod 
952*3d8817e4Smiod   nameoff = bfd_h_get_32 (abfd, buf + 0);
953*3d8817e4Smiod 
954*3d8817e4Smiod   cmd->name_offset = command->offset + nameoff;
955*3d8817e4Smiod   cmd->name_len = command->len - nameoff;
956*3d8817e4Smiod 
957*3d8817e4Smiod   if (command->type == BFD_MACH_O_LC_LOAD_DYLINKER)
958*3d8817e4Smiod     prefix = "LC_LOAD_DYLINKER";
959*3d8817e4Smiod   else if (command->type == BFD_MACH_O_LC_ID_DYLINKER)
960*3d8817e4Smiod     prefix = "LC_ID_DYLINKER";
961*3d8817e4Smiod   else
962*3d8817e4Smiod     abort ();
963*3d8817e4Smiod 
964*3d8817e4Smiod   sname = bfd_alloc (abfd, strlen (prefix) + 1);
965*3d8817e4Smiod   if (sname == NULL)
966*3d8817e4Smiod     return -1;
967*3d8817e4Smiod   strcpy (sname, prefix);
968*3d8817e4Smiod 
969*3d8817e4Smiod   bfdsec = bfd_make_section_anyway (abfd, sname);
970*3d8817e4Smiod   if (bfdsec == NULL)
971*3d8817e4Smiod     return -1;
972*3d8817e4Smiod 
973*3d8817e4Smiod   bfdsec->vma = 0;
974*3d8817e4Smiod   bfdsec->lma = 0;
975*3d8817e4Smiod   bfdsec->size = command->len - 8;
976*3d8817e4Smiod   bfdsec->filepos = command->offset + 8;
977*3d8817e4Smiod   bfdsec->alignment_power = 0;
978*3d8817e4Smiod   bfdsec->flags = SEC_HAS_CONTENTS;
979*3d8817e4Smiod 
980*3d8817e4Smiod   cmd->section = bfdsec;
981*3d8817e4Smiod 
982*3d8817e4Smiod   return 0;
983*3d8817e4Smiod }
984*3d8817e4Smiod 
985*3d8817e4Smiod static int
bfd_mach_o_scan_read_dylib(bfd * abfd,bfd_mach_o_load_command * command)986*3d8817e4Smiod bfd_mach_o_scan_read_dylib (bfd *abfd, bfd_mach_o_load_command *command)
987*3d8817e4Smiod {
988*3d8817e4Smiod   bfd_mach_o_dylib_command *cmd = &command->command.dylib;
989*3d8817e4Smiod   unsigned char buf[16];
990*3d8817e4Smiod   unsigned int nameoff;
991*3d8817e4Smiod   asection *bfdsec;
992*3d8817e4Smiod   char *sname;
993*3d8817e4Smiod   const char *prefix;
994*3d8817e4Smiod 
995*3d8817e4Smiod   BFD_ASSERT ((command->type == BFD_MACH_O_LC_ID_DYLIB)
996*3d8817e4Smiod 	      || (command->type == BFD_MACH_O_LC_LOAD_DYLIB)
997*3d8817e4Smiod 	      || (command->type == BFD_MACH_O_LC_LOAD_WEAK_DYLIB));
998*3d8817e4Smiod 
999*3d8817e4Smiod   bfd_seek (abfd, command->offset + 8, SEEK_SET);
1000*3d8817e4Smiod   if (bfd_bread ((PTR) buf, 16, abfd) != 16)
1001*3d8817e4Smiod     return -1;
1002*3d8817e4Smiod 
1003*3d8817e4Smiod   nameoff = bfd_h_get_32 (abfd, buf + 0);
1004*3d8817e4Smiod   cmd->timestamp = bfd_h_get_32 (abfd, buf + 4);
1005*3d8817e4Smiod   cmd->current_version = bfd_h_get_32 (abfd, buf + 8);
1006*3d8817e4Smiod   cmd->compatibility_version = bfd_h_get_32 (abfd, buf + 12);
1007*3d8817e4Smiod 
1008*3d8817e4Smiod   cmd->name_offset = command->offset + nameoff;
1009*3d8817e4Smiod   cmd->name_len = command->len - nameoff;
1010*3d8817e4Smiod 
1011*3d8817e4Smiod   if (command->type == BFD_MACH_O_LC_LOAD_DYLIB)
1012*3d8817e4Smiod     prefix = "LC_LOAD_DYLIB";
1013*3d8817e4Smiod   else if (command->type == BFD_MACH_O_LC_LOAD_WEAK_DYLIB)
1014*3d8817e4Smiod     prefix = "LC_LOAD_WEAK_DYLIB";
1015*3d8817e4Smiod   else if (command->type == BFD_MACH_O_LC_ID_DYLIB)
1016*3d8817e4Smiod     prefix = "LC_ID_DYLIB";
1017*3d8817e4Smiod   else
1018*3d8817e4Smiod     abort ();
1019*3d8817e4Smiod 
1020*3d8817e4Smiod   sname = bfd_alloc (abfd, strlen (prefix) + 1);
1021*3d8817e4Smiod   if (sname == NULL)
1022*3d8817e4Smiod     return -1;
1023*3d8817e4Smiod   strcpy (sname, prefix);
1024*3d8817e4Smiod 
1025*3d8817e4Smiod   bfdsec = bfd_make_section_anyway (abfd, sname);
1026*3d8817e4Smiod   if (bfdsec == NULL)
1027*3d8817e4Smiod     return -1;
1028*3d8817e4Smiod 
1029*3d8817e4Smiod   bfdsec->vma = 0;
1030*3d8817e4Smiod   bfdsec->lma = 0;
1031*3d8817e4Smiod   bfdsec->size = command->len - 8;
1032*3d8817e4Smiod   bfdsec->filepos = command->offset + 8;
1033*3d8817e4Smiod   bfdsec->alignment_power = 0;
1034*3d8817e4Smiod   bfdsec->flags = SEC_HAS_CONTENTS;
1035*3d8817e4Smiod 
1036*3d8817e4Smiod   cmd->section = bfdsec;
1037*3d8817e4Smiod 
1038*3d8817e4Smiod   return 0;
1039*3d8817e4Smiod }
1040*3d8817e4Smiod 
1041*3d8817e4Smiod static int
bfd_mach_o_scan_read_prebound_dylib(bfd * abfd ATTRIBUTE_UNUSED,bfd_mach_o_load_command * command ATTRIBUTE_UNUSED)1042*3d8817e4Smiod bfd_mach_o_scan_read_prebound_dylib (bfd *abfd ATTRIBUTE_UNUSED,
1043*3d8817e4Smiod 				     bfd_mach_o_load_command *command ATTRIBUTE_UNUSED)
1044*3d8817e4Smiod {
1045*3d8817e4Smiod   /* bfd_mach_o_prebound_dylib_command *cmd = &command->command.prebound_dylib; */
1046*3d8817e4Smiod 
1047*3d8817e4Smiod   BFD_ASSERT (command->type == BFD_MACH_O_LC_PREBOUND_DYLIB);
1048*3d8817e4Smiod   return 0;
1049*3d8817e4Smiod }
1050*3d8817e4Smiod 
1051*3d8817e4Smiod static int
bfd_mach_o_scan_read_thread(bfd * abfd,bfd_mach_o_load_command * command)1052*3d8817e4Smiod bfd_mach_o_scan_read_thread (bfd *abfd, bfd_mach_o_load_command *command)
1053*3d8817e4Smiod {
1054*3d8817e4Smiod   bfd_mach_o_data_struct *mdata = NULL;
1055*3d8817e4Smiod   bfd_mach_o_thread_command *cmd = &command->command.thread;
1056*3d8817e4Smiod   unsigned char buf[8];
1057*3d8817e4Smiod   bfd_vma offset;
1058*3d8817e4Smiod   unsigned int nflavours;
1059*3d8817e4Smiod   unsigned int i;
1060*3d8817e4Smiod 
1061*3d8817e4Smiod   BFD_ASSERT ((command->type == BFD_MACH_O_LC_THREAD)
1062*3d8817e4Smiod 	      || (command->type == BFD_MACH_O_LC_UNIXTHREAD));
1063*3d8817e4Smiod 
1064*3d8817e4Smiod   BFD_ASSERT (bfd_mach_o_valid (abfd));
1065*3d8817e4Smiod   mdata = abfd->tdata.mach_o_data;
1066*3d8817e4Smiod 
1067*3d8817e4Smiod   offset = 8;
1068*3d8817e4Smiod   nflavours = 0;
1069*3d8817e4Smiod   while (offset != command->len)
1070*3d8817e4Smiod     {
1071*3d8817e4Smiod       if (offset >= command->len)
1072*3d8817e4Smiod 	return -1;
1073*3d8817e4Smiod 
1074*3d8817e4Smiod       bfd_seek (abfd, command->offset + offset, SEEK_SET);
1075*3d8817e4Smiod 
1076*3d8817e4Smiod       if (bfd_bread ((PTR) buf, 8, abfd) != 8)
1077*3d8817e4Smiod 	return -1;
1078*3d8817e4Smiod 
1079*3d8817e4Smiod       offset += 8 + bfd_h_get_32 (abfd, buf + 4) * 4;
1080*3d8817e4Smiod       nflavours++;
1081*3d8817e4Smiod     }
1082*3d8817e4Smiod 
1083*3d8817e4Smiod   cmd->flavours = bfd_alloc (abfd, nflavours * sizeof (bfd_mach_o_thread_flavour));
1084*3d8817e4Smiod   if (cmd->flavours == NULL)
1085*3d8817e4Smiod     return -1;
1086*3d8817e4Smiod   cmd->nflavours = nflavours;
1087*3d8817e4Smiod 
1088*3d8817e4Smiod   offset = 8;
1089*3d8817e4Smiod   nflavours = 0;
1090*3d8817e4Smiod   while (offset != command->len)
1091*3d8817e4Smiod     {
1092*3d8817e4Smiod       if (offset >= command->len)
1093*3d8817e4Smiod 	return -1;
1094*3d8817e4Smiod 
1095*3d8817e4Smiod       if (nflavours >= cmd->nflavours)
1096*3d8817e4Smiod 	return -1;
1097*3d8817e4Smiod 
1098*3d8817e4Smiod       bfd_seek (abfd, command->offset + offset, SEEK_SET);
1099*3d8817e4Smiod 
1100*3d8817e4Smiod       if (bfd_bread ((PTR) buf, 8, abfd) != 8)
1101*3d8817e4Smiod 	return -1;
1102*3d8817e4Smiod 
1103*3d8817e4Smiod       cmd->flavours[nflavours].flavour = bfd_h_get_32 (abfd, buf);
1104*3d8817e4Smiod       cmd->flavours[nflavours].offset = command->offset + offset + 8;
1105*3d8817e4Smiod       cmd->flavours[nflavours].size = bfd_h_get_32 (abfd, buf + 4) * 4;
1106*3d8817e4Smiod       offset += cmd->flavours[nflavours].size + 8;
1107*3d8817e4Smiod       nflavours++;
1108*3d8817e4Smiod     }
1109*3d8817e4Smiod 
1110*3d8817e4Smiod   for (i = 0; i < nflavours; i++)
1111*3d8817e4Smiod     {
1112*3d8817e4Smiod       asection *bfdsec;
1113*3d8817e4Smiod       unsigned int snamelen;
1114*3d8817e4Smiod       char *sname;
1115*3d8817e4Smiod       const char *flavourstr;
1116*3d8817e4Smiod       const char *prefix = "LC_THREAD";
1117*3d8817e4Smiod       unsigned int j = 0;
1118*3d8817e4Smiod 
1119*3d8817e4Smiod       switch (mdata->header.cputype)
1120*3d8817e4Smiod 	{
1121*3d8817e4Smiod 	case BFD_MACH_O_CPU_TYPE_POWERPC:
1122*3d8817e4Smiod 	  flavourstr = bfd_mach_o_ppc_flavour_string (cmd->flavours[i].flavour);
1123*3d8817e4Smiod 	  break;
1124*3d8817e4Smiod 	case BFD_MACH_O_CPU_TYPE_I386:
1125*3d8817e4Smiod 	  flavourstr = bfd_mach_o_i386_flavour_string (cmd->flavours[i].flavour);
1126*3d8817e4Smiod 	  break;
1127*3d8817e4Smiod 	default:
1128*3d8817e4Smiod 	  flavourstr = "UNKNOWN_ARCHITECTURE";
1129*3d8817e4Smiod 	  break;
1130*3d8817e4Smiod 	}
1131*3d8817e4Smiod 
1132*3d8817e4Smiod       snamelen = strlen (prefix) + 1 + 20 + 1 + strlen (flavourstr) + 1;
1133*3d8817e4Smiod       sname = bfd_alloc (abfd, snamelen);
1134*3d8817e4Smiod       if (sname == NULL)
1135*3d8817e4Smiod 	return -1;
1136*3d8817e4Smiod 
1137*3d8817e4Smiod       for (;;)
1138*3d8817e4Smiod 	{
1139*3d8817e4Smiod 	  sprintf (sname, "%s.%s.%u", prefix, flavourstr, j);
1140*3d8817e4Smiod 	  if (bfd_get_section_by_name (abfd, sname) == NULL)
1141*3d8817e4Smiod 	    break;
1142*3d8817e4Smiod 	  j++;
1143*3d8817e4Smiod 	}
1144*3d8817e4Smiod 
1145*3d8817e4Smiod       bfdsec = bfd_make_section (abfd, sname);
1146*3d8817e4Smiod 
1147*3d8817e4Smiod       bfdsec->vma = 0;
1148*3d8817e4Smiod       bfdsec->lma = 0;
1149*3d8817e4Smiod       bfdsec->size = cmd->flavours[i].size;
1150*3d8817e4Smiod       bfdsec->filepos = cmd->flavours[i].offset;
1151*3d8817e4Smiod       bfdsec->alignment_power = 0x0;
1152*3d8817e4Smiod       bfdsec->flags = SEC_HAS_CONTENTS;
1153*3d8817e4Smiod 
1154*3d8817e4Smiod       cmd->section = bfdsec;
1155*3d8817e4Smiod     }
1156*3d8817e4Smiod 
1157*3d8817e4Smiod   return 0;
1158*3d8817e4Smiod }
1159*3d8817e4Smiod 
1160*3d8817e4Smiod static int
bfd_mach_o_scan_read_dysymtab(bfd * abfd,bfd_mach_o_load_command * command)1161*3d8817e4Smiod bfd_mach_o_scan_read_dysymtab (bfd *abfd, bfd_mach_o_load_command *command)
1162*3d8817e4Smiod {
1163*3d8817e4Smiod   bfd_mach_o_dysymtab_command *seg = &command->command.dysymtab;
1164*3d8817e4Smiod   unsigned char buf[72];
1165*3d8817e4Smiod 
1166*3d8817e4Smiod   BFD_ASSERT (command->type == BFD_MACH_O_LC_DYSYMTAB);
1167*3d8817e4Smiod 
1168*3d8817e4Smiod   bfd_seek (abfd, command->offset + 8, SEEK_SET);
1169*3d8817e4Smiod   if (bfd_bread ((PTR) buf, 72, abfd) != 72)
1170*3d8817e4Smiod     return -1;
1171*3d8817e4Smiod 
1172*3d8817e4Smiod   seg->ilocalsym = bfd_h_get_32 (abfd, buf + 0);
1173*3d8817e4Smiod   seg->nlocalsym = bfd_h_get_32 (abfd, buf + 4);
1174*3d8817e4Smiod   seg->iextdefsym = bfd_h_get_32 (abfd, buf + 8);
1175*3d8817e4Smiod   seg->nextdefsym = bfd_h_get_32 (abfd, buf + 12);
1176*3d8817e4Smiod   seg->iundefsym = bfd_h_get_32 (abfd, buf + 16);
1177*3d8817e4Smiod   seg->nundefsym = bfd_h_get_32 (abfd, buf + 20);
1178*3d8817e4Smiod   seg->tocoff = bfd_h_get_32 (abfd, buf + 24);
1179*3d8817e4Smiod   seg->ntoc = bfd_h_get_32 (abfd, buf + 28);
1180*3d8817e4Smiod   seg->modtaboff = bfd_h_get_32 (abfd, buf + 32);
1181*3d8817e4Smiod   seg->nmodtab = bfd_h_get_32 (abfd, buf + 36);
1182*3d8817e4Smiod   seg->extrefsymoff = bfd_h_get_32 (abfd, buf + 40);
1183*3d8817e4Smiod   seg->nextrefsyms = bfd_h_get_32 (abfd, buf + 44);
1184*3d8817e4Smiod   seg->indirectsymoff = bfd_h_get_32 (abfd, buf + 48);
1185*3d8817e4Smiod   seg->nindirectsyms = bfd_h_get_32 (abfd, buf + 52);
1186*3d8817e4Smiod   seg->extreloff = bfd_h_get_32 (abfd, buf + 56);
1187*3d8817e4Smiod   seg->nextrel = bfd_h_get_32 (abfd, buf + 60);
1188*3d8817e4Smiod   seg->locreloff = bfd_h_get_32 (abfd, buf + 64);
1189*3d8817e4Smiod   seg->nlocrel = bfd_h_get_32 (abfd, buf + 68);
1190*3d8817e4Smiod 
1191*3d8817e4Smiod   return 0;
1192*3d8817e4Smiod }
1193*3d8817e4Smiod 
1194*3d8817e4Smiod static int
bfd_mach_o_scan_read_symtab(bfd * abfd,bfd_mach_o_load_command * command)1195*3d8817e4Smiod bfd_mach_o_scan_read_symtab (bfd *abfd, bfd_mach_o_load_command *command)
1196*3d8817e4Smiod {
1197*3d8817e4Smiod   bfd_mach_o_symtab_command *seg = &command->command.symtab;
1198*3d8817e4Smiod   unsigned char buf[16];
1199*3d8817e4Smiod   asection *bfdsec;
1200*3d8817e4Smiod   char *sname;
1201*3d8817e4Smiod   const char *prefix = "LC_SYMTAB.stabs";
1202*3d8817e4Smiod 
1203*3d8817e4Smiod   BFD_ASSERT (command->type == BFD_MACH_O_LC_SYMTAB);
1204*3d8817e4Smiod 
1205*3d8817e4Smiod   bfd_seek (abfd, command->offset + 8, SEEK_SET);
1206*3d8817e4Smiod   if (bfd_bread ((PTR) buf, 16, abfd) != 16)
1207*3d8817e4Smiod     return -1;
1208*3d8817e4Smiod 
1209*3d8817e4Smiod   seg->symoff = bfd_h_get_32 (abfd, buf);
1210*3d8817e4Smiod   seg->nsyms = bfd_h_get_32 (abfd, buf + 4);
1211*3d8817e4Smiod   seg->stroff = bfd_h_get_32 (abfd, buf + 8);
1212*3d8817e4Smiod   seg->strsize = bfd_h_get_32 (abfd, buf + 12);
1213*3d8817e4Smiod   seg->symbols = NULL;
1214*3d8817e4Smiod   seg->strtab = NULL;
1215*3d8817e4Smiod 
1216*3d8817e4Smiod   sname = bfd_alloc (abfd, strlen (prefix) + 1);
1217*3d8817e4Smiod   if (sname == NULL)
1218*3d8817e4Smiod     return -1;
1219*3d8817e4Smiod   strcpy (sname, prefix);
1220*3d8817e4Smiod 
1221*3d8817e4Smiod   bfdsec = bfd_make_section_anyway (abfd, sname);
1222*3d8817e4Smiod   if (bfdsec == NULL)
1223*3d8817e4Smiod     return -1;
1224*3d8817e4Smiod 
1225*3d8817e4Smiod   bfdsec->vma = 0;
1226*3d8817e4Smiod   bfdsec->lma = 0;
1227*3d8817e4Smiod   bfdsec->size = seg->nsyms * 12;
1228*3d8817e4Smiod   bfdsec->filepos = seg->symoff;
1229*3d8817e4Smiod   bfdsec->alignment_power = 0;
1230*3d8817e4Smiod   bfdsec->flags = SEC_HAS_CONTENTS;
1231*3d8817e4Smiod 
1232*3d8817e4Smiod   seg->stabs_segment = bfdsec;
1233*3d8817e4Smiod 
1234*3d8817e4Smiod   prefix = "LC_SYMTAB.stabstr";
1235*3d8817e4Smiod   sname = bfd_alloc (abfd, strlen (prefix) + 1);
1236*3d8817e4Smiod   if (sname == NULL)
1237*3d8817e4Smiod     return -1;
1238*3d8817e4Smiod   strcpy (sname, prefix);
1239*3d8817e4Smiod 
1240*3d8817e4Smiod   bfdsec = bfd_make_section_anyway (abfd, sname);
1241*3d8817e4Smiod   if (bfdsec == NULL)
1242*3d8817e4Smiod     return -1;
1243*3d8817e4Smiod 
1244*3d8817e4Smiod   bfdsec->vma = 0;
1245*3d8817e4Smiod   bfdsec->lma = 0;
1246*3d8817e4Smiod   bfdsec->size = seg->strsize;
1247*3d8817e4Smiod   bfdsec->filepos = seg->stroff;
1248*3d8817e4Smiod   bfdsec->alignment_power = 0;
1249*3d8817e4Smiod   bfdsec->flags = SEC_HAS_CONTENTS;
1250*3d8817e4Smiod 
1251*3d8817e4Smiod   seg->stabstr_segment = bfdsec;
1252*3d8817e4Smiod 
1253*3d8817e4Smiod   return 0;
1254*3d8817e4Smiod }
1255*3d8817e4Smiod 
1256*3d8817e4Smiod static int
bfd_mach_o_scan_read_segment(bfd * abfd,bfd_mach_o_load_command * command)1257*3d8817e4Smiod bfd_mach_o_scan_read_segment (bfd *abfd, bfd_mach_o_load_command *command)
1258*3d8817e4Smiod {
1259*3d8817e4Smiod   unsigned char buf[48];
1260*3d8817e4Smiod   bfd_mach_o_segment_command *seg = &command->command.segment;
1261*3d8817e4Smiod   unsigned long i;
1262*3d8817e4Smiod   asection *bfdsec;
1263*3d8817e4Smiod   char *sname;
1264*3d8817e4Smiod   const char *prefix = "LC_SEGMENT";
1265*3d8817e4Smiod   unsigned int snamelen;
1266*3d8817e4Smiod 
1267*3d8817e4Smiod   BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT);
1268*3d8817e4Smiod 
1269*3d8817e4Smiod   bfd_seek (abfd, command->offset + 8, SEEK_SET);
1270*3d8817e4Smiod   if (bfd_bread ((PTR) buf, 48, abfd) != 48)
1271*3d8817e4Smiod     return -1;
1272*3d8817e4Smiod 
1273*3d8817e4Smiod   memcpy (seg->segname, buf, 16);
1274*3d8817e4Smiod   seg->vmaddr = bfd_h_get_32 (abfd, buf + 16);
1275*3d8817e4Smiod   seg->vmsize = bfd_h_get_32 (abfd, buf + 20);
1276*3d8817e4Smiod   seg->fileoff = bfd_h_get_32 (abfd, buf + 24);
1277*3d8817e4Smiod   seg->filesize = bfd_h_get_32 (abfd, buf +  28);
1278*3d8817e4Smiod   /* seg->maxprot = bfd_h_get_32 (abfd, buf + 32); */
1279*3d8817e4Smiod   /* seg->initprot = bfd_h_get_32 (abfd, buf + 36); */
1280*3d8817e4Smiod   seg->nsects = bfd_h_get_32 (abfd, buf + 40);
1281*3d8817e4Smiod   seg->flags = bfd_h_get_32 (abfd, buf + 44);
1282*3d8817e4Smiod 
1283*3d8817e4Smiod   snamelen = strlen (prefix) + 1 + strlen (seg->segname) + 1;
1284*3d8817e4Smiod   sname = bfd_alloc (abfd, snamelen);
1285*3d8817e4Smiod   if (sname == NULL)
1286*3d8817e4Smiod     return -1;
1287*3d8817e4Smiod   sprintf (sname, "%s.%s", prefix, seg->segname);
1288*3d8817e4Smiod 
1289*3d8817e4Smiod   bfdsec = bfd_make_section_anyway (abfd, sname);
1290*3d8817e4Smiod   if (bfdsec == NULL)
1291*3d8817e4Smiod     return -1;
1292*3d8817e4Smiod 
1293*3d8817e4Smiod   bfdsec->vma = seg->vmaddr;
1294*3d8817e4Smiod   bfdsec->lma = seg->vmaddr;
1295*3d8817e4Smiod   bfdsec->size = seg->filesize;
1296*3d8817e4Smiod   bfdsec->filepos = seg->fileoff;
1297*3d8817e4Smiod   bfdsec->alignment_power = 0x0;
1298*3d8817e4Smiod   bfdsec->flags = SEC_HAS_CONTENTS | SEC_LOAD | SEC_ALLOC | SEC_CODE;
1299*3d8817e4Smiod 
1300*3d8817e4Smiod   seg->segment = bfdsec;
1301*3d8817e4Smiod 
1302*3d8817e4Smiod   if (seg->nsects != 0)
1303*3d8817e4Smiod     {
1304*3d8817e4Smiod       seg->sections = bfd_alloc (abfd, seg->nsects * sizeof (bfd_mach_o_section));
1305*3d8817e4Smiod       if (seg->sections == NULL)
1306*3d8817e4Smiod 	return -1;
1307*3d8817e4Smiod 
1308*3d8817e4Smiod       for (i = 0; i < seg->nsects; i++)
1309*3d8817e4Smiod 	{
1310*3d8817e4Smiod 	  bfd_vma segoff = command->offset + 48 + 8 + (i * 68);
1311*3d8817e4Smiod 
1312*3d8817e4Smiod 	  if (bfd_mach_o_scan_read_section (abfd, &seg->sections[i],
1313*3d8817e4Smiod 					    segoff) != 0)
1314*3d8817e4Smiod 	    return -1;
1315*3d8817e4Smiod 	}
1316*3d8817e4Smiod     }
1317*3d8817e4Smiod 
1318*3d8817e4Smiod   return 0;
1319*3d8817e4Smiod }
1320*3d8817e4Smiod 
1321*3d8817e4Smiod static int
bfd_mach_o_scan_read_command(bfd * abfd,bfd_mach_o_load_command * command)1322*3d8817e4Smiod bfd_mach_o_scan_read_command (bfd *abfd, bfd_mach_o_load_command *command)
1323*3d8817e4Smiod {
1324*3d8817e4Smiod   unsigned char buf[8];
1325*3d8817e4Smiod 
1326*3d8817e4Smiod   bfd_seek (abfd, command->offset, SEEK_SET);
1327*3d8817e4Smiod   if (bfd_bread ((PTR) buf, 8, abfd) != 8)
1328*3d8817e4Smiod     return -1;
1329*3d8817e4Smiod 
1330*3d8817e4Smiod   command->type = (bfd_h_get_32 (abfd, buf) & ~BFD_MACH_O_LC_REQ_DYLD);
1331*3d8817e4Smiod   command->type_required = (bfd_h_get_32 (abfd, buf) & BFD_MACH_O_LC_REQ_DYLD
1332*3d8817e4Smiod 			    ? 1 : 0);
1333*3d8817e4Smiod   command->len = bfd_h_get_32 (abfd, buf + 4);
1334*3d8817e4Smiod 
1335*3d8817e4Smiod   switch (command->type)
1336*3d8817e4Smiod     {
1337*3d8817e4Smiod     case BFD_MACH_O_LC_SEGMENT:
1338*3d8817e4Smiod       if (bfd_mach_o_scan_read_segment (abfd, command) != 0)
1339*3d8817e4Smiod 	return -1;
1340*3d8817e4Smiod       break;
1341*3d8817e4Smiod     case BFD_MACH_O_LC_SYMTAB:
1342*3d8817e4Smiod       if (bfd_mach_o_scan_read_symtab (abfd, command) != 0)
1343*3d8817e4Smiod 	return -1;
1344*3d8817e4Smiod       break;
1345*3d8817e4Smiod     case BFD_MACH_O_LC_SYMSEG:
1346*3d8817e4Smiod       break;
1347*3d8817e4Smiod     case BFD_MACH_O_LC_THREAD:
1348*3d8817e4Smiod     case BFD_MACH_O_LC_UNIXTHREAD:
1349*3d8817e4Smiod       if (bfd_mach_o_scan_read_thread (abfd, command) != 0)
1350*3d8817e4Smiod 	return -1;
1351*3d8817e4Smiod       break;
1352*3d8817e4Smiod     case BFD_MACH_O_LC_LOAD_DYLINKER:
1353*3d8817e4Smiod     case BFD_MACH_O_LC_ID_DYLINKER:
1354*3d8817e4Smiod       if (bfd_mach_o_scan_read_dylinker (abfd, command) != 0)
1355*3d8817e4Smiod 	return -1;
1356*3d8817e4Smiod       break;
1357*3d8817e4Smiod     case BFD_MACH_O_LC_LOAD_DYLIB:
1358*3d8817e4Smiod     case BFD_MACH_O_LC_ID_DYLIB:
1359*3d8817e4Smiod     case BFD_MACH_O_LC_LOAD_WEAK_DYLIB:
1360*3d8817e4Smiod       if (bfd_mach_o_scan_read_dylib (abfd, command) != 0)
1361*3d8817e4Smiod 	return -1;
1362*3d8817e4Smiod       break;
1363*3d8817e4Smiod     case BFD_MACH_O_LC_PREBOUND_DYLIB:
1364*3d8817e4Smiod       if (bfd_mach_o_scan_read_prebound_dylib (abfd, command) != 0)
1365*3d8817e4Smiod 	return -1;
1366*3d8817e4Smiod       break;
1367*3d8817e4Smiod     case BFD_MACH_O_LC_LOADFVMLIB:
1368*3d8817e4Smiod     case BFD_MACH_O_LC_IDFVMLIB:
1369*3d8817e4Smiod     case BFD_MACH_O_LC_IDENT:
1370*3d8817e4Smiod     case BFD_MACH_O_LC_FVMFILE:
1371*3d8817e4Smiod     case BFD_MACH_O_LC_PREPAGE:
1372*3d8817e4Smiod     case BFD_MACH_O_LC_ROUTINES:
1373*3d8817e4Smiod     case BFD_MACH_O_LC_SUB_FRAMEWORK:
1374*3d8817e4Smiod       break;
1375*3d8817e4Smiod     case BFD_MACH_O_LC_DYSYMTAB:
1376*3d8817e4Smiod       if (bfd_mach_o_scan_read_dysymtab (abfd, command) != 0)
1377*3d8817e4Smiod 	return -1;
1378*3d8817e4Smiod       break;
1379*3d8817e4Smiod     case BFD_MACH_O_LC_SUB_UMBRELLA:
1380*3d8817e4Smiod     case BFD_MACH_O_LC_SUB_CLIENT:
1381*3d8817e4Smiod     case BFD_MACH_O_LC_SUB_LIBRARY:
1382*3d8817e4Smiod     case BFD_MACH_O_LC_TWOLEVEL_HINTS:
1383*3d8817e4Smiod     case BFD_MACH_O_LC_PREBIND_CKSUM:
1384*3d8817e4Smiod       break;
1385*3d8817e4Smiod     default:
1386*3d8817e4Smiod       fprintf (stderr, "unable to read unknown load command 0x%lx\n",
1387*3d8817e4Smiod 	       (unsigned long) command->type);
1388*3d8817e4Smiod       break;
1389*3d8817e4Smiod     }
1390*3d8817e4Smiod 
1391*3d8817e4Smiod   return 0;
1392*3d8817e4Smiod }
1393*3d8817e4Smiod 
1394*3d8817e4Smiod static void
bfd_mach_o_flatten_sections(bfd * abfd)1395*3d8817e4Smiod bfd_mach_o_flatten_sections (bfd *abfd)
1396*3d8817e4Smiod {
1397*3d8817e4Smiod   bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
1398*3d8817e4Smiod   long csect = 0;
1399*3d8817e4Smiod   unsigned long i, j;
1400*3d8817e4Smiod 
1401*3d8817e4Smiod   mdata->nsects = 0;
1402*3d8817e4Smiod 
1403*3d8817e4Smiod   for (i = 0; i < mdata->header.ncmds; i++)
1404*3d8817e4Smiod     {
1405*3d8817e4Smiod       if (mdata->commands[i].type == BFD_MACH_O_LC_SEGMENT)
1406*3d8817e4Smiod 	{
1407*3d8817e4Smiod 	  bfd_mach_o_segment_command *seg;
1408*3d8817e4Smiod 
1409*3d8817e4Smiod 	  seg = &mdata->commands[i].command.segment;
1410*3d8817e4Smiod 	  mdata->nsects += seg->nsects;
1411*3d8817e4Smiod 	}
1412*3d8817e4Smiod     }
1413*3d8817e4Smiod 
1414*3d8817e4Smiod   mdata->sections = bfd_alloc (abfd,
1415*3d8817e4Smiod 			       mdata->nsects * sizeof (bfd_mach_o_section *));
1416*3d8817e4Smiod   csect = 0;
1417*3d8817e4Smiod 
1418*3d8817e4Smiod   for (i = 0; i < mdata->header.ncmds; i++)
1419*3d8817e4Smiod     {
1420*3d8817e4Smiod       if (mdata->commands[i].type == BFD_MACH_O_LC_SEGMENT)
1421*3d8817e4Smiod 	{
1422*3d8817e4Smiod 	  bfd_mach_o_segment_command *seg;
1423*3d8817e4Smiod 
1424*3d8817e4Smiod 	  seg = &mdata->commands[i].command.segment;
1425*3d8817e4Smiod 	  BFD_ASSERT (csect + seg->nsects <= mdata->nsects);
1426*3d8817e4Smiod 
1427*3d8817e4Smiod 	  for (j = 0; j < seg->nsects; j++)
1428*3d8817e4Smiod 	    mdata->sections[csect++] = &seg->sections[j];
1429*3d8817e4Smiod 	}
1430*3d8817e4Smiod     }
1431*3d8817e4Smiod }
1432*3d8817e4Smiod 
1433*3d8817e4Smiod int
bfd_mach_o_scan_start_address(bfd * abfd)1434*3d8817e4Smiod bfd_mach_o_scan_start_address (bfd *abfd)
1435*3d8817e4Smiod {
1436*3d8817e4Smiod   bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
1437*3d8817e4Smiod   bfd_mach_o_thread_command *cmd = NULL;
1438*3d8817e4Smiod   unsigned long i;
1439*3d8817e4Smiod 
1440*3d8817e4Smiod   for (i = 0; i < mdata->header.ncmds; i++)
1441*3d8817e4Smiod     {
1442*3d8817e4Smiod       if ((mdata->commands[i].type == BFD_MACH_O_LC_THREAD) ||
1443*3d8817e4Smiod 	  (mdata->commands[i].type == BFD_MACH_O_LC_UNIXTHREAD))
1444*3d8817e4Smiod 	{
1445*3d8817e4Smiod 	  if (cmd == NULL)
1446*3d8817e4Smiod 	    cmd = &mdata->commands[i].command.thread;
1447*3d8817e4Smiod 	  else
1448*3d8817e4Smiod 	    return 0;
1449*3d8817e4Smiod 	}
1450*3d8817e4Smiod     }
1451*3d8817e4Smiod 
1452*3d8817e4Smiod   if (cmd == NULL)
1453*3d8817e4Smiod     return 0;
1454*3d8817e4Smiod 
1455*3d8817e4Smiod   for (i = 0; i < cmd->nflavours; i++)
1456*3d8817e4Smiod     {
1457*3d8817e4Smiod       if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_I386)
1458*3d8817e4Smiod 	  && (cmd->flavours[i].flavour
1459*3d8817e4Smiod 	      == (unsigned long) BFD_MACH_O_i386_THREAD_STATE))
1460*3d8817e4Smiod 	{
1461*3d8817e4Smiod 	  unsigned char buf[4];
1462*3d8817e4Smiod 
1463*3d8817e4Smiod 	  bfd_seek (abfd, cmd->flavours[i].offset + 40, SEEK_SET);
1464*3d8817e4Smiod 
1465*3d8817e4Smiod 	  if (bfd_bread (buf, 4, abfd) != 4)
1466*3d8817e4Smiod 	    return -1;
1467*3d8817e4Smiod 
1468*3d8817e4Smiod 	  abfd->start_address = bfd_h_get_32 (abfd, buf);
1469*3d8817e4Smiod 	}
1470*3d8817e4Smiod       else if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_POWERPC)
1471*3d8817e4Smiod 	       && (cmd->flavours[i].flavour == BFD_MACH_O_PPC_THREAD_STATE))
1472*3d8817e4Smiod 	{
1473*3d8817e4Smiod 	  unsigned char buf[4];
1474*3d8817e4Smiod 
1475*3d8817e4Smiod 	  bfd_seek (abfd, cmd->flavours[i].offset + 0, SEEK_SET);
1476*3d8817e4Smiod 
1477*3d8817e4Smiod 	  if (bfd_bread (buf, 4, abfd) != 4)
1478*3d8817e4Smiod 	    return -1;
1479*3d8817e4Smiod 
1480*3d8817e4Smiod 	  abfd->start_address = bfd_h_get_32 (abfd, buf);
1481*3d8817e4Smiod 	}
1482*3d8817e4Smiod     }
1483*3d8817e4Smiod 
1484*3d8817e4Smiod   return 0;
1485*3d8817e4Smiod }
1486*3d8817e4Smiod 
1487*3d8817e4Smiod int
bfd_mach_o_scan(bfd * abfd,bfd_mach_o_header * header,bfd_mach_o_data_struct * mdata)1488*3d8817e4Smiod bfd_mach_o_scan (bfd *abfd,
1489*3d8817e4Smiod 		 bfd_mach_o_header *header,
1490*3d8817e4Smiod 		 bfd_mach_o_data_struct *mdata)
1491*3d8817e4Smiod {
1492*3d8817e4Smiod   unsigned int i;
1493*3d8817e4Smiod   enum bfd_architecture cputype;
1494*3d8817e4Smiod   unsigned long cpusubtype;
1495*3d8817e4Smiod 
1496*3d8817e4Smiod   mdata->header = *header;
1497*3d8817e4Smiod   mdata->symbols = NULL;
1498*3d8817e4Smiod 
1499*3d8817e4Smiod   abfd->flags = (abfd->xvec->object_flags
1500*3d8817e4Smiod 		 | (abfd->flags & (BFD_IN_MEMORY | BFD_IO_FUNCS)));
1501*3d8817e4Smiod   abfd->tdata.mach_o_data = mdata;
1502*3d8817e4Smiod 
1503*3d8817e4Smiod   bfd_mach_o_convert_architecture (header->cputype, header->cpusubtype,
1504*3d8817e4Smiod 				   &cputype, &cpusubtype);
1505*3d8817e4Smiod   if (cputype == bfd_arch_unknown)
1506*3d8817e4Smiod     {
1507*3d8817e4Smiod       fprintf (stderr, "bfd_mach_o_scan: unknown architecture 0x%lx/0x%lx\n",
1508*3d8817e4Smiod 	       header->cputype, header->cpusubtype);
1509*3d8817e4Smiod       return -1;
1510*3d8817e4Smiod     }
1511*3d8817e4Smiod 
1512*3d8817e4Smiod   bfd_set_arch_mach (abfd, cputype, cpusubtype);
1513*3d8817e4Smiod 
1514*3d8817e4Smiod   if (header->ncmds != 0)
1515*3d8817e4Smiod     {
1516*3d8817e4Smiod       mdata->commands = bfd_alloc (abfd, header->ncmds * sizeof (bfd_mach_o_load_command));
1517*3d8817e4Smiod       if (mdata->commands == NULL)
1518*3d8817e4Smiod 	return -1;
1519*3d8817e4Smiod 
1520*3d8817e4Smiod       for (i = 0; i < header->ncmds; i++)
1521*3d8817e4Smiod 	{
1522*3d8817e4Smiod 	  bfd_mach_o_load_command *cur = &mdata->commands[i];
1523*3d8817e4Smiod 
1524*3d8817e4Smiod 	  if (i == 0)
1525*3d8817e4Smiod 	    cur->offset = 28;
1526*3d8817e4Smiod 	  else
1527*3d8817e4Smiod 	    {
1528*3d8817e4Smiod 	      bfd_mach_o_load_command *prev = &mdata->commands[i - 1];
1529*3d8817e4Smiod 	      cur->offset = prev->offset + prev->len;
1530*3d8817e4Smiod 	    }
1531*3d8817e4Smiod 
1532*3d8817e4Smiod 	  if (bfd_mach_o_scan_read_command (abfd, cur) < 0)
1533*3d8817e4Smiod 	    return -1;
1534*3d8817e4Smiod 	}
1535*3d8817e4Smiod     }
1536*3d8817e4Smiod 
1537*3d8817e4Smiod   if (bfd_mach_o_scan_start_address (abfd) < 0)
1538*3d8817e4Smiod     return -1;
1539*3d8817e4Smiod 
1540*3d8817e4Smiod   bfd_mach_o_flatten_sections (abfd);
1541*3d8817e4Smiod   return 0;
1542*3d8817e4Smiod }
1543*3d8817e4Smiod 
1544*3d8817e4Smiod bfd_boolean
bfd_mach_o_mkobject(bfd * abfd)1545*3d8817e4Smiod bfd_mach_o_mkobject (bfd *abfd)
1546*3d8817e4Smiod {
1547*3d8817e4Smiod   bfd_mach_o_data_struct *mdata = NULL;
1548*3d8817e4Smiod 
1549*3d8817e4Smiod   mdata = bfd_alloc (abfd, sizeof (bfd_mach_o_data_struct));
1550*3d8817e4Smiod   if (mdata == NULL)
1551*3d8817e4Smiod     return FALSE;
1552*3d8817e4Smiod   abfd->tdata.mach_o_data = mdata;
1553*3d8817e4Smiod 
1554*3d8817e4Smiod   mdata->header.magic = 0;
1555*3d8817e4Smiod   mdata->header.cputype = 0;
1556*3d8817e4Smiod   mdata->header.cpusubtype = 0;
1557*3d8817e4Smiod   mdata->header.filetype = 0;
1558*3d8817e4Smiod   mdata->header.ncmds = 0;
1559*3d8817e4Smiod   mdata->header.sizeofcmds = 0;
1560*3d8817e4Smiod   mdata->header.flags = 0;
1561*3d8817e4Smiod   mdata->header.byteorder = BFD_ENDIAN_UNKNOWN;
1562*3d8817e4Smiod   mdata->commands = NULL;
1563*3d8817e4Smiod   mdata->nsymbols = 0;
1564*3d8817e4Smiod   mdata->symbols = NULL;
1565*3d8817e4Smiod   mdata->nsects = 0;
1566*3d8817e4Smiod   mdata->sections = NULL;
1567*3d8817e4Smiod   mdata->ibfd = NULL;
1568*3d8817e4Smiod 
1569*3d8817e4Smiod   return TRUE;
1570*3d8817e4Smiod }
1571*3d8817e4Smiod 
1572*3d8817e4Smiod const bfd_target *
bfd_mach_o_object_p(bfd * abfd)1573*3d8817e4Smiod bfd_mach_o_object_p (bfd *abfd)
1574*3d8817e4Smiod {
1575*3d8817e4Smiod   struct bfd_preserve preserve;
1576*3d8817e4Smiod   bfd_mach_o_header header;
1577*3d8817e4Smiod 
1578*3d8817e4Smiod   preserve.marker = NULL;
1579*3d8817e4Smiod   if (bfd_mach_o_read_header (abfd, &header) != 0)
1580*3d8817e4Smiod     goto wrong;
1581*3d8817e4Smiod 
1582*3d8817e4Smiod   if (! (header.byteorder == BFD_ENDIAN_BIG
1583*3d8817e4Smiod 	 || header.byteorder == BFD_ENDIAN_LITTLE))
1584*3d8817e4Smiod     {
1585*3d8817e4Smiod       fprintf (stderr, "unknown header byte-order value 0x%lx\n",
1586*3d8817e4Smiod 	       (long) header.byteorder);
1587*3d8817e4Smiod       goto wrong;
1588*3d8817e4Smiod     }
1589*3d8817e4Smiod 
1590*3d8817e4Smiod   if (! ((header.byteorder == BFD_ENDIAN_BIG
1591*3d8817e4Smiod 	  && abfd->xvec->byteorder == BFD_ENDIAN_BIG
1592*3d8817e4Smiod 	  && abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
1593*3d8817e4Smiod 	 || (header.byteorder == BFD_ENDIAN_LITTLE
1594*3d8817e4Smiod 	     && abfd->xvec->byteorder == BFD_ENDIAN_LITTLE
1595*3d8817e4Smiod 	     && abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)))
1596*3d8817e4Smiod     goto wrong;
1597*3d8817e4Smiod 
1598*3d8817e4Smiod   preserve.marker = bfd_zalloc (abfd, sizeof (bfd_mach_o_data_struct));
1599*3d8817e4Smiod   if (preserve.marker == NULL
1600*3d8817e4Smiod       || !bfd_preserve_save (abfd, &preserve))
1601*3d8817e4Smiod     goto fail;
1602*3d8817e4Smiod 
1603*3d8817e4Smiod   if (bfd_mach_o_scan (abfd, &header,
1604*3d8817e4Smiod 		       (bfd_mach_o_data_struct *) preserve.marker) != 0)
1605*3d8817e4Smiod     goto wrong;
1606*3d8817e4Smiod 
1607*3d8817e4Smiod   bfd_preserve_finish (abfd, &preserve);
1608*3d8817e4Smiod   return abfd->xvec;
1609*3d8817e4Smiod 
1610*3d8817e4Smiod  wrong:
1611*3d8817e4Smiod   bfd_set_error (bfd_error_wrong_format);
1612*3d8817e4Smiod 
1613*3d8817e4Smiod  fail:
1614*3d8817e4Smiod   if (preserve.marker != NULL)
1615*3d8817e4Smiod     bfd_preserve_restore (abfd, &preserve);
1616*3d8817e4Smiod   return NULL;
1617*3d8817e4Smiod }
1618*3d8817e4Smiod 
1619*3d8817e4Smiod const bfd_target *
bfd_mach_o_core_p(bfd * abfd)1620*3d8817e4Smiod bfd_mach_o_core_p (bfd *abfd)
1621*3d8817e4Smiod {
1622*3d8817e4Smiod   struct bfd_preserve preserve;
1623*3d8817e4Smiod   bfd_mach_o_header header;
1624*3d8817e4Smiod 
1625*3d8817e4Smiod   preserve.marker = NULL;
1626*3d8817e4Smiod   if (bfd_mach_o_read_header (abfd, &header) != 0)
1627*3d8817e4Smiod     goto wrong;
1628*3d8817e4Smiod 
1629*3d8817e4Smiod   if (! (header.byteorder == BFD_ENDIAN_BIG
1630*3d8817e4Smiod 	 || header.byteorder == BFD_ENDIAN_LITTLE))
1631*3d8817e4Smiod     {
1632*3d8817e4Smiod       fprintf (stderr, "unknown header byte-order value 0x%lx\n",
1633*3d8817e4Smiod 	       (long) header.byteorder);
1634*3d8817e4Smiod       abort ();
1635*3d8817e4Smiod     }
1636*3d8817e4Smiod 
1637*3d8817e4Smiod   if (! ((header.byteorder == BFD_ENDIAN_BIG
1638*3d8817e4Smiod 	  && abfd->xvec->byteorder == BFD_ENDIAN_BIG
1639*3d8817e4Smiod 	  && abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
1640*3d8817e4Smiod 	 || (header.byteorder == BFD_ENDIAN_LITTLE
1641*3d8817e4Smiod 	     && abfd->xvec->byteorder == BFD_ENDIAN_LITTLE
1642*3d8817e4Smiod 	     && abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)))
1643*3d8817e4Smiod     goto wrong;
1644*3d8817e4Smiod 
1645*3d8817e4Smiod   if (header.filetype != BFD_MACH_O_MH_CORE)
1646*3d8817e4Smiod     goto wrong;
1647*3d8817e4Smiod 
1648*3d8817e4Smiod   preserve.marker = bfd_zalloc (abfd, sizeof (bfd_mach_o_data_struct));
1649*3d8817e4Smiod   if (preserve.marker == NULL
1650*3d8817e4Smiod       || !bfd_preserve_save (abfd, &preserve))
1651*3d8817e4Smiod     goto fail;
1652*3d8817e4Smiod 
1653*3d8817e4Smiod   if (bfd_mach_o_scan (abfd, &header,
1654*3d8817e4Smiod 		       (bfd_mach_o_data_struct *) preserve.marker) != 0)
1655*3d8817e4Smiod     goto wrong;
1656*3d8817e4Smiod 
1657*3d8817e4Smiod   bfd_preserve_finish (abfd, &preserve);
1658*3d8817e4Smiod   return abfd->xvec;
1659*3d8817e4Smiod 
1660*3d8817e4Smiod  wrong:
1661*3d8817e4Smiod   bfd_set_error (bfd_error_wrong_format);
1662*3d8817e4Smiod 
1663*3d8817e4Smiod  fail:
1664*3d8817e4Smiod   if (preserve.marker != NULL)
1665*3d8817e4Smiod     bfd_preserve_restore (abfd, &preserve);
1666*3d8817e4Smiod   return NULL;
1667*3d8817e4Smiod }
1668*3d8817e4Smiod 
1669*3d8817e4Smiod typedef struct mach_o_fat_archentry
1670*3d8817e4Smiod {
1671*3d8817e4Smiod   unsigned long cputype;
1672*3d8817e4Smiod   unsigned long cpusubtype;
1673*3d8817e4Smiod   unsigned long offset;
1674*3d8817e4Smiod   unsigned long size;
1675*3d8817e4Smiod   unsigned long align;
1676*3d8817e4Smiod   bfd *abfd;
1677*3d8817e4Smiod } mach_o_fat_archentry;
1678*3d8817e4Smiod 
1679*3d8817e4Smiod typedef struct mach_o_fat_data_struct
1680*3d8817e4Smiod {
1681*3d8817e4Smiod   unsigned long magic;
1682*3d8817e4Smiod   unsigned long nfat_arch;
1683*3d8817e4Smiod   mach_o_fat_archentry *archentries;
1684*3d8817e4Smiod } mach_o_fat_data_struct;
1685*3d8817e4Smiod 
1686*3d8817e4Smiod const bfd_target *
bfd_mach_o_archive_p(bfd * abfd)1687*3d8817e4Smiod bfd_mach_o_archive_p (bfd *abfd)
1688*3d8817e4Smiod {
1689*3d8817e4Smiod   mach_o_fat_data_struct *adata = NULL;
1690*3d8817e4Smiod   unsigned char buf[20];
1691*3d8817e4Smiod   unsigned long i;
1692*3d8817e4Smiod 
1693*3d8817e4Smiod   bfd_seek (abfd, 0, SEEK_SET);
1694*3d8817e4Smiod   if (bfd_bread ((PTR) buf, 8, abfd) != 8)
1695*3d8817e4Smiod     goto error;
1696*3d8817e4Smiod 
1697*3d8817e4Smiod   adata = bfd_alloc (abfd, sizeof (mach_o_fat_data_struct));
1698*3d8817e4Smiod   if (adata == NULL)
1699*3d8817e4Smiod     goto error;
1700*3d8817e4Smiod 
1701*3d8817e4Smiod   adata->magic = bfd_getb32 (buf);
1702*3d8817e4Smiod   adata->nfat_arch = bfd_getb32 (buf + 4);
1703*3d8817e4Smiod   if (adata->magic != 0xcafebabe)
1704*3d8817e4Smiod     goto error;
1705*3d8817e4Smiod 
1706*3d8817e4Smiod   adata->archentries =
1707*3d8817e4Smiod     bfd_alloc (abfd, adata->nfat_arch * sizeof (mach_o_fat_archentry));
1708*3d8817e4Smiod   if (adata->archentries == NULL)
1709*3d8817e4Smiod     goto error;
1710*3d8817e4Smiod 
1711*3d8817e4Smiod   for (i = 0; i < adata->nfat_arch; i++)
1712*3d8817e4Smiod     {
1713*3d8817e4Smiod       bfd_seek (abfd, 8 + 20 * i, SEEK_SET);
1714*3d8817e4Smiod 
1715*3d8817e4Smiod       if (bfd_bread ((PTR) buf, 20, abfd) != 20)
1716*3d8817e4Smiod 	goto error;
1717*3d8817e4Smiod       adata->archentries[i].cputype = bfd_getb32 (buf);
1718*3d8817e4Smiod       adata->archentries[i].cpusubtype = bfd_getb32 (buf + 4);
1719*3d8817e4Smiod       adata->archentries[i].offset = bfd_getb32 (buf + 8);
1720*3d8817e4Smiod       adata->archentries[i].size = bfd_getb32 (buf + 12);
1721*3d8817e4Smiod       adata->archentries[i].align = bfd_getb32 (buf + 16);
1722*3d8817e4Smiod       adata->archentries[i].abfd = NULL;
1723*3d8817e4Smiod     }
1724*3d8817e4Smiod 
1725*3d8817e4Smiod   abfd->tdata.mach_o_fat_data = adata;
1726*3d8817e4Smiod   return abfd->xvec;
1727*3d8817e4Smiod 
1728*3d8817e4Smiod  error:
1729*3d8817e4Smiod   if (adata != NULL)
1730*3d8817e4Smiod     bfd_release (abfd, adata);
1731*3d8817e4Smiod   bfd_set_error (bfd_error_wrong_format);
1732*3d8817e4Smiod   return NULL;
1733*3d8817e4Smiod }
1734*3d8817e4Smiod 
1735*3d8817e4Smiod bfd *
bfd_mach_o_openr_next_archived_file(bfd * archive,bfd * prev)1736*3d8817e4Smiod bfd_mach_o_openr_next_archived_file (bfd *archive, bfd *prev)
1737*3d8817e4Smiod {
1738*3d8817e4Smiod   mach_o_fat_data_struct *adata;
1739*3d8817e4Smiod   mach_o_fat_archentry *entry = NULL;
1740*3d8817e4Smiod   unsigned long i;
1741*3d8817e4Smiod 
1742*3d8817e4Smiod   adata = (mach_o_fat_data_struct *) archive->tdata.mach_o_fat_data;
1743*3d8817e4Smiod   BFD_ASSERT (adata != NULL);
1744*3d8817e4Smiod 
1745*3d8817e4Smiod   /* Find index of previous entry.  */
1746*3d8817e4Smiod   if (prev == NULL)
1747*3d8817e4Smiod     i = 0;	/* Start at first one.  */
1748*3d8817e4Smiod   else
1749*3d8817e4Smiod     {
1750*3d8817e4Smiod       for (i = 0; i < adata->nfat_arch; i++)
1751*3d8817e4Smiod 	{
1752*3d8817e4Smiod 	  if (adata->archentries[i].abfd == prev)
1753*3d8817e4Smiod 	    break;
1754*3d8817e4Smiod 	}
1755*3d8817e4Smiod 
1756*3d8817e4Smiod       if (i == adata->nfat_arch)
1757*3d8817e4Smiod 	{
1758*3d8817e4Smiod 	  /* Not found.  */
1759*3d8817e4Smiod 	  bfd_set_error (bfd_error_bad_value);
1760*3d8817e4Smiod 	  return NULL;
1761*3d8817e4Smiod 	}
1762*3d8817e4Smiod     i++;	/* Get next entry.  */
1763*3d8817e4Smiod   }
1764*3d8817e4Smiod 
1765*3d8817e4Smiod   if (i >= adata->nfat_arch)
1766*3d8817e4Smiod     {
1767*3d8817e4Smiod       bfd_set_error (bfd_error_no_more_archived_files);
1768*3d8817e4Smiod       return NULL;
1769*3d8817e4Smiod     }
1770*3d8817e4Smiod 
1771*3d8817e4Smiod   entry = &adata->archentries[i];
1772*3d8817e4Smiod   if (entry->abfd == NULL)
1773*3d8817e4Smiod     {
1774*3d8817e4Smiod       bfd *nbfd = _bfd_new_bfd_contained_in (archive);
1775*3d8817e4Smiod       char *s = NULL;
1776*3d8817e4Smiod 
1777*3d8817e4Smiod       if (nbfd == NULL)
1778*3d8817e4Smiod 	return NULL;
1779*3d8817e4Smiod 
1780*3d8817e4Smiod       nbfd->origin = entry->offset;
1781*3d8817e4Smiod       s = bfd_malloc (strlen (archive->filename) + 1);
1782*3d8817e4Smiod       if (s == NULL)
1783*3d8817e4Smiod 	return NULL;
1784*3d8817e4Smiod       strcpy (s, archive->filename);
1785*3d8817e4Smiod       nbfd->filename = s;
1786*3d8817e4Smiod       nbfd->iostream = NULL;
1787*3d8817e4Smiod       entry->abfd = nbfd;
1788*3d8817e4Smiod     }
1789*3d8817e4Smiod 
1790*3d8817e4Smiod   return entry->abfd;
1791*3d8817e4Smiod }
1792*3d8817e4Smiod 
1793*3d8817e4Smiod int
bfd_mach_o_lookup_section(bfd * abfd,asection * section,bfd_mach_o_load_command ** mcommand,bfd_mach_o_section ** msection)1794*3d8817e4Smiod bfd_mach_o_lookup_section (bfd *abfd,
1795*3d8817e4Smiod 			   asection *section,
1796*3d8817e4Smiod 			   bfd_mach_o_load_command **mcommand,
1797*3d8817e4Smiod 			   bfd_mach_o_section **msection)
1798*3d8817e4Smiod {
1799*3d8817e4Smiod   struct mach_o_data_struct *md = abfd->tdata.mach_o_data;
1800*3d8817e4Smiod   unsigned int i, j, num;
1801*3d8817e4Smiod 
1802*3d8817e4Smiod   bfd_mach_o_load_command *ncmd = NULL;
1803*3d8817e4Smiod   bfd_mach_o_section *nsect = NULL;
1804*3d8817e4Smiod 
1805*3d8817e4Smiod   BFD_ASSERT (mcommand != NULL);
1806*3d8817e4Smiod   BFD_ASSERT (msection != NULL);
1807*3d8817e4Smiod 
1808*3d8817e4Smiod   num = 0;
1809*3d8817e4Smiod   for (i = 0; i < md->header.ncmds; i++)
1810*3d8817e4Smiod     {
1811*3d8817e4Smiod       struct bfd_mach_o_load_command *cmd = &md->commands[i];
1812*3d8817e4Smiod       struct bfd_mach_o_segment_command *seg = NULL;
1813*3d8817e4Smiod 
1814*3d8817e4Smiod       if (cmd->type != BFD_MACH_O_LC_SEGMENT)
1815*3d8817e4Smiod 	continue;
1816*3d8817e4Smiod       seg = &cmd->command.segment;
1817*3d8817e4Smiod 
1818*3d8817e4Smiod       if (seg->segment == section)
1819*3d8817e4Smiod 	{
1820*3d8817e4Smiod 	  if (num == 0)
1821*3d8817e4Smiod 	    ncmd = cmd;
1822*3d8817e4Smiod 	  num++;
1823*3d8817e4Smiod 	}
1824*3d8817e4Smiod 
1825*3d8817e4Smiod       for (j = 0; j < seg->nsects; j++)
1826*3d8817e4Smiod 	{
1827*3d8817e4Smiod 	  struct bfd_mach_o_section *sect = &seg->sections[j];
1828*3d8817e4Smiod 
1829*3d8817e4Smiod 	  if (sect->bfdsection == section)
1830*3d8817e4Smiod 	    {
1831*3d8817e4Smiod 	      if (num == 0)
1832*3d8817e4Smiod 		nsect = sect;
1833*3d8817e4Smiod 	      num++;
1834*3d8817e4Smiod 	    }
1835*3d8817e4Smiod 	}
1836*3d8817e4Smiod     }
1837*3d8817e4Smiod 
1838*3d8817e4Smiod   *mcommand = ncmd;
1839*3d8817e4Smiod   *msection = nsect;
1840*3d8817e4Smiod   return num;
1841*3d8817e4Smiod }
1842*3d8817e4Smiod 
1843*3d8817e4Smiod int
bfd_mach_o_lookup_command(bfd * abfd,bfd_mach_o_load_command_type type,bfd_mach_o_load_command ** mcommand)1844*3d8817e4Smiod bfd_mach_o_lookup_command (bfd *abfd,
1845*3d8817e4Smiod 			   bfd_mach_o_load_command_type type,
1846*3d8817e4Smiod 			   bfd_mach_o_load_command **mcommand)
1847*3d8817e4Smiod {
1848*3d8817e4Smiod   struct mach_o_data_struct *md = NULL;
1849*3d8817e4Smiod   bfd_mach_o_load_command *ncmd = NULL;
1850*3d8817e4Smiod   unsigned int i, num;
1851*3d8817e4Smiod 
1852*3d8817e4Smiod   md = abfd->tdata.mach_o_data;
1853*3d8817e4Smiod 
1854*3d8817e4Smiod   BFD_ASSERT (md != NULL);
1855*3d8817e4Smiod   BFD_ASSERT (mcommand != NULL);
1856*3d8817e4Smiod 
1857*3d8817e4Smiod   num = 0;
1858*3d8817e4Smiod   for (i = 0; i < md->header.ncmds; i++)
1859*3d8817e4Smiod     {
1860*3d8817e4Smiod       struct bfd_mach_o_load_command *cmd = &md->commands[i];
1861*3d8817e4Smiod 
1862*3d8817e4Smiod       if (cmd->type != type)
1863*3d8817e4Smiod 	continue;
1864*3d8817e4Smiod 
1865*3d8817e4Smiod       if (num == 0)
1866*3d8817e4Smiod 	ncmd = cmd;
1867*3d8817e4Smiod       num++;
1868*3d8817e4Smiod     }
1869*3d8817e4Smiod 
1870*3d8817e4Smiod   *mcommand = ncmd;
1871*3d8817e4Smiod   return num;
1872*3d8817e4Smiod }
1873*3d8817e4Smiod 
1874*3d8817e4Smiod unsigned long
bfd_mach_o_stack_addr(enum bfd_mach_o_cpu_type type)1875*3d8817e4Smiod bfd_mach_o_stack_addr (enum bfd_mach_o_cpu_type type)
1876*3d8817e4Smiod {
1877*3d8817e4Smiod   switch (type)
1878*3d8817e4Smiod     {
1879*3d8817e4Smiod     case BFD_MACH_O_CPU_TYPE_MC680x0:
1880*3d8817e4Smiod       return 0x04000000;
1881*3d8817e4Smiod     case BFD_MACH_O_CPU_TYPE_MC88000:
1882*3d8817e4Smiod       return 0xffffe000;
1883*3d8817e4Smiod     case BFD_MACH_O_CPU_TYPE_POWERPC:
1884*3d8817e4Smiod       return 0xc0000000;
1885*3d8817e4Smiod     case BFD_MACH_O_CPU_TYPE_I386:
1886*3d8817e4Smiod       return 0xc0000000;
1887*3d8817e4Smiod     case BFD_MACH_O_CPU_TYPE_SPARC:
1888*3d8817e4Smiod       return 0xf0000000;
1889*3d8817e4Smiod     case BFD_MACH_O_CPU_TYPE_I860:
1890*3d8817e4Smiod       return 0;
1891*3d8817e4Smiod     case BFD_MACH_O_CPU_TYPE_HPPA:
1892*3d8817e4Smiod       return 0xc0000000 - 0x04000000;
1893*3d8817e4Smiod     default:
1894*3d8817e4Smiod       return 0;
1895*3d8817e4Smiod     }
1896*3d8817e4Smiod }
1897*3d8817e4Smiod 
1898*3d8817e4Smiod int
bfd_mach_o_core_fetch_environment(bfd * abfd,unsigned char ** rbuf,unsigned int * rlen)1899*3d8817e4Smiod bfd_mach_o_core_fetch_environment (bfd *abfd,
1900*3d8817e4Smiod 				   unsigned char **rbuf,
1901*3d8817e4Smiod 				   unsigned int *rlen)
1902*3d8817e4Smiod {
1903*3d8817e4Smiod   bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
1904*3d8817e4Smiod   unsigned long stackaddr = bfd_mach_o_stack_addr (mdata->header.cputype);
1905*3d8817e4Smiod   unsigned int i = 0;
1906*3d8817e4Smiod 
1907*3d8817e4Smiod   for (i = 0; i < mdata->header.ncmds; i++)
1908*3d8817e4Smiod     {
1909*3d8817e4Smiod       bfd_mach_o_load_command *cur = &mdata->commands[i];
1910*3d8817e4Smiod       bfd_mach_o_segment_command *seg = NULL;
1911*3d8817e4Smiod 
1912*3d8817e4Smiod       if (cur->type != BFD_MACH_O_LC_SEGMENT)
1913*3d8817e4Smiod 	continue;
1914*3d8817e4Smiod 
1915*3d8817e4Smiod       seg = &cur->command.segment;
1916*3d8817e4Smiod 
1917*3d8817e4Smiod       if ((seg->vmaddr + seg->vmsize) == stackaddr)
1918*3d8817e4Smiod 	{
1919*3d8817e4Smiod 	  unsigned long start = seg->fileoff;
1920*3d8817e4Smiod 	  unsigned long end = seg->fileoff + seg->filesize;
1921*3d8817e4Smiod 	  unsigned char *buf = bfd_malloc (1024);
1922*3d8817e4Smiod 	  unsigned long size = 1024;
1923*3d8817e4Smiod 
1924*3d8817e4Smiod 	  for (;;)
1925*3d8817e4Smiod 	    {
1926*3d8817e4Smiod 	      bfd_size_type nread = 0;
1927*3d8817e4Smiod 	      unsigned long offset;
1928*3d8817e4Smiod 	      int found_nonnull = 0;
1929*3d8817e4Smiod 
1930*3d8817e4Smiod 	      if (size > (end - start))
1931*3d8817e4Smiod 		size = (end - start);
1932*3d8817e4Smiod 
1933*3d8817e4Smiod 	      buf = bfd_realloc (buf, size);
1934*3d8817e4Smiod 
1935*3d8817e4Smiod 	      bfd_seek (abfd, end - size, SEEK_SET);
1936*3d8817e4Smiod 	      nread = bfd_bread (buf, size, abfd);
1937*3d8817e4Smiod 
1938*3d8817e4Smiod 	      if (nread != size)
1939*3d8817e4Smiod 		return -1;
1940*3d8817e4Smiod 
1941*3d8817e4Smiod 	      for (offset = 4; offset <= size; offset += 4)
1942*3d8817e4Smiod 		{
1943*3d8817e4Smiod 		  unsigned long val;
1944*3d8817e4Smiod 
1945*3d8817e4Smiod 		  val = *((unsigned long *) (buf + size - offset));
1946*3d8817e4Smiod 		  if (! found_nonnull)
1947*3d8817e4Smiod 		    {
1948*3d8817e4Smiod 		      if (val != 0)
1949*3d8817e4Smiod 			found_nonnull = 1;
1950*3d8817e4Smiod 		    }
1951*3d8817e4Smiod 		  else if (val == 0x0)
1952*3d8817e4Smiod 		    {
1953*3d8817e4Smiod 		      unsigned long bottom;
1954*3d8817e4Smiod 		      unsigned long top;
1955*3d8817e4Smiod 
1956*3d8817e4Smiod 		      bottom = seg->fileoff + seg->filesize - offset;
1957*3d8817e4Smiod 		      top = seg->fileoff + seg->filesize - 4;
1958*3d8817e4Smiod 		      *rbuf = bfd_malloc (top - bottom);
1959*3d8817e4Smiod 		      *rlen = top - bottom;
1960*3d8817e4Smiod 
1961*3d8817e4Smiod 		      memcpy (*rbuf, buf + size - *rlen, *rlen);
1962*3d8817e4Smiod 		      return 0;
1963*3d8817e4Smiod 		    }
1964*3d8817e4Smiod 		}
1965*3d8817e4Smiod 
1966*3d8817e4Smiod 	      if (size == (end - start))
1967*3d8817e4Smiod 		break;
1968*3d8817e4Smiod 
1969*3d8817e4Smiod 	      size *= 2;
1970*3d8817e4Smiod 	    }
1971*3d8817e4Smiod 	}
1972*3d8817e4Smiod     }
1973*3d8817e4Smiod 
1974*3d8817e4Smiod   return -1;
1975*3d8817e4Smiod }
1976*3d8817e4Smiod 
1977*3d8817e4Smiod char *
bfd_mach_o_core_file_failing_command(bfd * abfd)1978*3d8817e4Smiod bfd_mach_o_core_file_failing_command (bfd *abfd)
1979*3d8817e4Smiod {
1980*3d8817e4Smiod   unsigned char *buf = NULL;
1981*3d8817e4Smiod   unsigned int len = 0;
1982*3d8817e4Smiod   int ret = -1;
1983*3d8817e4Smiod 
1984*3d8817e4Smiod   ret = bfd_mach_o_core_fetch_environment (abfd, &buf, &len);
1985*3d8817e4Smiod   if (ret < 0)
1986*3d8817e4Smiod     return NULL;
1987*3d8817e4Smiod 
1988*3d8817e4Smiod   return (char *) buf;
1989*3d8817e4Smiod }
1990*3d8817e4Smiod 
1991*3d8817e4Smiod int
bfd_mach_o_core_file_failing_signal(bfd * abfd ATTRIBUTE_UNUSED)1992*3d8817e4Smiod bfd_mach_o_core_file_failing_signal (bfd *abfd ATTRIBUTE_UNUSED)
1993*3d8817e4Smiod {
1994*3d8817e4Smiod   return 0;
1995*3d8817e4Smiod }
1996*3d8817e4Smiod 
1997*3d8817e4Smiod #define TARGET_NAME 		mach_o_be_vec
1998*3d8817e4Smiod #define TARGET_STRING     	"mach-o-be"
1999*3d8817e4Smiod #define TARGET_BIG_ENDIAN 	1
2000*3d8817e4Smiod #define TARGET_ARCHIVE 		0
2001*3d8817e4Smiod 
2002*3d8817e4Smiod #include "mach-o-target.c"
2003*3d8817e4Smiod 
2004*3d8817e4Smiod #undef TARGET_NAME
2005*3d8817e4Smiod #undef TARGET_STRING
2006*3d8817e4Smiod #undef TARGET_BIG_ENDIAN
2007*3d8817e4Smiod #undef TARGET_ARCHIVE
2008*3d8817e4Smiod 
2009*3d8817e4Smiod #define TARGET_NAME 		mach_o_le_vec
2010*3d8817e4Smiod #define TARGET_STRING 		"mach-o-le"
2011*3d8817e4Smiod #define TARGET_BIG_ENDIAN 	0
2012*3d8817e4Smiod #define TARGET_ARCHIVE 		0
2013*3d8817e4Smiod 
2014*3d8817e4Smiod #include "mach-o-target.c"
2015*3d8817e4Smiod 
2016*3d8817e4Smiod #undef TARGET_NAME
2017*3d8817e4Smiod #undef TARGET_STRING
2018*3d8817e4Smiod #undef TARGET_BIG_ENDIAN
2019*3d8817e4Smiod #undef TARGET_ARCHIVE
2020*3d8817e4Smiod 
2021*3d8817e4Smiod #define TARGET_NAME 		mach_o_fat_vec
2022*3d8817e4Smiod #define TARGET_STRING 		"mach-o-fat"
2023*3d8817e4Smiod #define TARGET_BIG_ENDIAN 	1
2024*3d8817e4Smiod #define TARGET_ARCHIVE 		1
2025*3d8817e4Smiod 
2026*3d8817e4Smiod #include "mach-o-target.c"
2027*3d8817e4Smiod 
2028*3d8817e4Smiod #undef TARGET_NAME
2029*3d8817e4Smiod #undef TARGET_STRING
2030*3d8817e4Smiod #undef TARGET_BIG_ENDIAN
2031*3d8817e4Smiod #undef TARGET_ARCHIVE
2032