daily update
[binutils.git] / bfd / mach-o.c
blobafad9fdc120a5342268d91e34fcba510b0bfef36
1 /* Mach-O support for BFD.
2 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
22 #include "sysdep.h"
23 #include "mach-o.h"
24 #include "bfd.h"
25 #include "libbfd.h"
26 #include "libiberty.h"
27 #include "aout/stab_gnu.h"
28 #include <ctype.h>
30 #ifndef BFD_IO_FUNCS
31 #define BFD_IO_FUNCS 0
32 #endif
34 #define bfd_mach_o_mkarchive _bfd_noarchive_mkarchive
35 #define bfd_mach_o_read_ar_hdr _bfd_noarchive_read_ar_hdr
36 #define bfd_mach_o_slurp_armap _bfd_noarchive_slurp_armap
37 #define bfd_mach_o_slurp_extended_name_table _bfd_noarchive_slurp_extended_name_table
38 #define bfd_mach_o_construct_extended_name_table _bfd_noarchive_construct_extended_name_table
39 #define bfd_mach_o_truncate_arname _bfd_noarchive_truncate_arname
40 #define bfd_mach_o_write_armap _bfd_noarchive_write_armap
41 #define bfd_mach_o_get_elt_at_index _bfd_noarchive_get_elt_at_index
42 #define bfd_mach_o_generic_stat_arch_elt _bfd_noarchive_generic_stat_arch_elt
43 #define bfd_mach_o_update_armap_timestamp _bfd_noarchive_update_armap_timestamp
44 #define bfd_mach_o_close_and_cleanup _bfd_generic_close_and_cleanup
45 #define bfd_mach_o_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
46 #define bfd_mach_o_new_section_hook _bfd_generic_new_section_hook
47 #define bfd_mach_o_get_section_contents_in_window _bfd_generic_get_section_contents_in_window
48 #define bfd_mach_o_bfd_is_local_label_name _bfd_nosymbols_bfd_is_local_label_name
49 #define bfd_mach_o_bfd_is_target_special_symbol ((bfd_boolean (*) (bfd *, asymbol *)) bfd_false)
50 #define bfd_mach_o_bfd_is_local_label_name _bfd_nosymbols_bfd_is_local_label_name
51 #define bfd_mach_o_get_lineno _bfd_nosymbols_get_lineno
52 #define bfd_mach_o_find_nearest_line _bfd_nosymbols_find_nearest_line
53 #define bfd_mach_o_find_inliner_info _bfd_nosymbols_find_inliner_info
54 #define bfd_mach_o_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
55 #define bfd_mach_o_read_minisymbols _bfd_generic_read_minisymbols
56 #define bfd_mach_o_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
57 #define bfd_mach_o_bfd_get_relocated_section_contents bfd_generic_get_relocated_section_contents
58 #define bfd_mach_o_bfd_relax_section bfd_generic_relax_section
59 #define bfd_mach_o_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
60 #define bfd_mach_o_bfd_link_hash_table_free _bfd_generic_link_hash_table_free
61 #define bfd_mach_o_bfd_link_add_symbols _bfd_generic_link_add_symbols
62 #define bfd_mach_o_bfd_link_just_syms _bfd_generic_link_just_syms
63 #define bfd_mach_o_bfd_final_link _bfd_generic_final_link
64 #define bfd_mach_o_bfd_link_split_section _bfd_generic_link_split_section
65 #define bfd_mach_o_set_arch_mach bfd_default_set_arch_mach
66 #define bfd_mach_o_bfd_merge_private_bfd_data _bfd_generic_bfd_merge_private_bfd_data
67 #define bfd_mach_o_bfd_set_private_flags _bfd_generic_bfd_set_private_flags
68 #define bfd_mach_o_get_section_contents _bfd_generic_get_section_contents
69 #define bfd_mach_o_set_section_contents _bfd_generic_set_section_contents
70 #define bfd_mach_o_bfd_gc_sections bfd_generic_gc_sections
71 #define bfd_mach_o_bfd_merge_sections bfd_generic_merge_sections
72 #define bfd_mach_o_bfd_is_group_section bfd_generic_is_group_section
73 #define bfd_mach_o_bfd_discard_group bfd_generic_discard_group
74 #define bfd_mach_o_section_already_linked _bfd_generic_section_already_linked
75 #define bfd_mach_o_bfd_copy_private_header_data _bfd_generic_bfd_copy_private_header_data
76 #define bfd_mach_o_core_file_matches_executable_p generic_core_file_matches_executable_p
78 static unsigned int
79 bfd_mach_o_version (bfd *abfd)
81 bfd_mach_o_data_struct *mdata = NULL;
83 BFD_ASSERT (bfd_mach_o_valid (abfd));
84 mdata = abfd->tdata.mach_o_data;
86 return mdata->header.version;
89 bfd_boolean
90 bfd_mach_o_valid (bfd *abfd)
92 if (abfd == NULL || abfd->xvec == NULL)
93 return 0;
95 if (! ((abfd->xvec == &mach_o_be_vec)
96 || (abfd->xvec == &mach_o_le_vec)
97 || (abfd->xvec == &mach_o_fat_vec)))
98 return 0;
100 if (abfd->tdata.mach_o_data == NULL)
101 return 0;
102 return 1;
105 /* Copy any private info we understand from the input symbol
106 to the output symbol. */
108 static bfd_boolean
109 bfd_mach_o_bfd_copy_private_symbol_data (bfd *ibfd ATTRIBUTE_UNUSED,
110 asymbol *isymbol ATTRIBUTE_UNUSED,
111 bfd *obfd ATTRIBUTE_UNUSED,
112 asymbol *osymbol ATTRIBUTE_UNUSED)
114 return TRUE;
117 /* Copy any private info we understand from the input section
118 to the output section. */
120 static bfd_boolean
121 bfd_mach_o_bfd_copy_private_section_data (bfd *ibfd ATTRIBUTE_UNUSED,
122 asection *isection ATTRIBUTE_UNUSED,
123 bfd *obfd ATTRIBUTE_UNUSED,
124 asection *osection ATTRIBUTE_UNUSED)
126 return TRUE;
129 /* Copy any private info we understand from the input bfd
130 to the output bfd. */
132 static bfd_boolean
133 bfd_mach_o_bfd_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
135 BFD_ASSERT (bfd_mach_o_valid (ibfd));
136 BFD_ASSERT (bfd_mach_o_valid (obfd));
138 obfd->tdata.mach_o_data = ibfd->tdata.mach_o_data;
139 obfd->tdata.mach_o_data->ibfd = ibfd;
140 return TRUE;
143 static long
144 bfd_mach_o_count_symbols (bfd *abfd)
146 bfd_mach_o_data_struct *mdata = NULL;
147 long nsyms = 0;
148 unsigned long i;
150 BFD_ASSERT (bfd_mach_o_valid (abfd));
151 mdata = abfd->tdata.mach_o_data;
153 for (i = 0; i < mdata->header.ncmds; i++)
154 if (mdata->commands[i].type == BFD_MACH_O_LC_SYMTAB)
156 bfd_mach_o_symtab_command *sym = &mdata->commands[i].command.symtab;
157 nsyms += sym->nsyms;
160 return nsyms;
163 static long
164 bfd_mach_o_get_symtab_upper_bound (bfd *abfd)
166 long nsyms = bfd_mach_o_count_symbols (abfd);
168 if (nsyms < 0)
169 return nsyms;
171 return ((nsyms + 1) * sizeof (asymbol *));
174 static long
175 bfd_mach_o_canonicalize_symtab (bfd *abfd, asymbol **alocation)
177 bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
178 long nsyms = bfd_mach_o_count_symbols (abfd);
179 asymbol **csym = alocation;
180 unsigned long i, j;
182 if (nsyms < 0)
183 return nsyms;
185 for (i = 0; i < mdata->header.ncmds; i++)
187 if (mdata->commands[i].type == BFD_MACH_O_LC_SYMTAB)
189 bfd_mach_o_symtab_command *sym = &mdata->commands[i].command.symtab;
191 if (bfd_mach_o_scan_read_symtab_symbols (abfd, &mdata->commands[i].command.symtab) != 0)
193 fprintf (stderr, "bfd_mach_o_canonicalize_symtab: unable to load symbols for section %lu\n", i);
194 return 0;
197 BFD_ASSERT (sym->symbols != NULL);
199 for (j = 0; j < sym->nsyms; j++)
201 BFD_ASSERT (csym < (alocation + nsyms));
202 *csym++ = &sym->symbols[j];
207 *csym++ = NULL;
209 return nsyms;
212 static void
213 bfd_mach_o_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
214 asymbol *symbol,
215 symbol_info *ret)
217 bfd_symbol_info (symbol, ret);
220 static void
221 bfd_mach_o_print_symbol (bfd *abfd,
222 PTR afile,
223 asymbol *symbol,
224 bfd_print_symbol_type how)
226 FILE *file = (FILE *) afile;
227 unsigned char ntype;
228 unsigned char nsect;
229 unsigned int ndesc;
230 const char *name;
232 switch (how)
234 case bfd_print_symbol_name:
235 fprintf (file, "%s", symbol->name);
236 break;
237 default:
238 bfd_print_symbol_vandf (abfd, (PTR) file, symbol);
239 ntype = BFD_MACH_O_SYM_NTYPE (symbol);
240 nsect = BFD_MACH_O_SYM_NSECT (symbol);
241 ndesc = BFD_MACH_O_SYM_NDESC (symbol);
242 if (ntype & BFD_MACH_O_N_STAB)
243 name = bfd_get_stab_name (ntype);
244 else
245 switch (ntype & BFD_MACH_O_N_TYPE)
247 case BFD_MACH_O_N_UNDF:
248 name = "UND";
249 break;
250 case BFD_MACH_O_N_ABS:
251 name = "ABS";
252 break;
253 case BFD_MACH_O_N_INDR:
254 name = "INDR";
255 break;
256 case BFD_MACH_O_N_PBUD:
257 name = "PBUD";
258 break;
259 case BFD_MACH_O_N_SECT:
260 name = "SECT";
261 break;
262 default:
263 name = "???";
264 break;
266 if (name == NULL)
267 name = "";
268 fprintf (file, " %02x %-6s %02x %04x", ntype, name, nsect, ndesc);
269 if ((ntype & BFD_MACH_O_N_STAB) == 0
270 && (ntype & BFD_MACH_O_N_TYPE) == BFD_MACH_O_N_SECT)
271 fprintf (file, " %-5s", symbol->section->name);
272 fprintf (file, " %s", symbol->name);
276 static void
277 bfd_mach_o_convert_architecture (bfd_mach_o_cpu_type mtype,
278 bfd_mach_o_cpu_subtype msubtype ATTRIBUTE_UNUSED,
279 enum bfd_architecture *type,
280 unsigned long *subtype)
282 *subtype = bfd_arch_unknown;
284 switch (mtype)
286 case BFD_MACH_O_CPU_TYPE_VAX: *type = bfd_arch_vax; break;
287 case BFD_MACH_O_CPU_TYPE_MC680x0: *type = bfd_arch_m68k; break;
288 case BFD_MACH_O_CPU_TYPE_I386:
289 *type = bfd_arch_i386;
290 *subtype = bfd_mach_i386_i386;
291 break;
292 case BFD_MACH_O_CPU_TYPE_X86_64:
293 *type = bfd_arch_i386;
294 *subtype = bfd_mach_x86_64;
295 break;
296 case BFD_MACH_O_CPU_TYPE_MIPS: *type = bfd_arch_mips; break;
297 case BFD_MACH_O_CPU_TYPE_MC98000: *type = bfd_arch_m98k; break;
298 case BFD_MACH_O_CPU_TYPE_HPPA: *type = bfd_arch_hppa; break;
299 case BFD_MACH_O_CPU_TYPE_ARM: *type = bfd_arch_arm; break;
300 case BFD_MACH_O_CPU_TYPE_MC88000: *type = bfd_arch_m88k; break;
301 case BFD_MACH_O_CPU_TYPE_SPARC:
302 *type = bfd_arch_sparc;
303 *subtype = bfd_mach_sparc;
304 break;
305 case BFD_MACH_O_CPU_TYPE_I860: *type = bfd_arch_i860; break;
306 case BFD_MACH_O_CPU_TYPE_ALPHA: *type = bfd_arch_alpha; break;
307 case BFD_MACH_O_CPU_TYPE_POWERPC:
308 *type = bfd_arch_powerpc;
309 *subtype = bfd_mach_ppc;
310 break;
311 case BFD_MACH_O_CPU_TYPE_POWERPC_64:
312 *type = bfd_arch_powerpc;
313 *subtype = bfd_mach_ppc64;
314 break;
315 default:
316 *type = bfd_arch_unknown;
317 break;
321 static int
322 bfd_mach_o_write_header (bfd *abfd, bfd_mach_o_header *header)
324 unsigned char buf[32];
325 unsigned int size;
327 size = (header->version == 2) ? 32 : 28;
329 bfd_h_put_32 (abfd, header->magic, buf + 0);
330 bfd_h_put_32 (abfd, header->cputype, buf + 4);
331 bfd_h_put_32 (abfd, header->cpusubtype, buf + 8);
332 bfd_h_put_32 (abfd, header->filetype, buf + 12);
333 bfd_h_put_32 (abfd, header->ncmds, buf + 16);
334 bfd_h_put_32 (abfd, header->sizeofcmds, buf + 20);
335 bfd_h_put_32 (abfd, header->flags, buf + 24);
337 if (header->version == 2)
338 bfd_h_put_32 (abfd, header->reserved, buf + 28);
340 bfd_seek (abfd, 0, SEEK_SET);
341 if (bfd_bwrite ((PTR) buf, size, abfd) != size)
342 return -1;
344 return 0;
347 static int
348 bfd_mach_o_scan_write_thread (bfd *abfd, bfd_mach_o_load_command *command)
350 bfd_mach_o_thread_command *cmd = &command->command.thread;
351 unsigned int i;
352 unsigned char buf[8];
353 bfd_vma offset;
354 unsigned int nflavours;
356 BFD_ASSERT ((command->type == BFD_MACH_O_LC_THREAD)
357 || (command->type == BFD_MACH_O_LC_UNIXTHREAD));
359 offset = 8;
360 nflavours = 0;
361 for (i = 0; i < cmd->nflavours; i++)
363 BFD_ASSERT ((cmd->flavours[i].size % 4) == 0);
364 BFD_ASSERT (cmd->flavours[i].offset == (command->offset + offset + 8));
366 bfd_h_put_32 (abfd, cmd->flavours[i].flavour, buf);
367 bfd_h_put_32 (abfd, (cmd->flavours[i].size / 4), buf + 4);
369 bfd_seek (abfd, command->offset + offset, SEEK_SET);
370 if (bfd_bwrite ((PTR) buf, 8, abfd) != 8)
371 return -1;
373 offset += cmd->flavours[i].size + 8;
376 return 0;
379 static int
380 bfd_mach_o_scan_write_section_32 (bfd *abfd,
381 bfd_mach_o_section *section,
382 bfd_vma offset)
384 unsigned char buf[68];
386 memcpy (buf, section->sectname, 16);
387 memcpy (buf + 16, section->segname, 16);
388 bfd_h_put_32 (abfd, section->addr, buf + 32);
389 bfd_h_put_32 (abfd, section->size, buf + 36);
390 bfd_h_put_32 (abfd, section->offset, buf + 40);
391 bfd_h_put_32 (abfd, section->align, buf + 44);
392 bfd_h_put_32 (abfd, section->reloff, buf + 48);
393 bfd_h_put_32 (abfd, section->nreloc, buf + 52);
394 bfd_h_put_32 (abfd, section->flags, buf + 56);
395 bfd_h_put_32 (abfd, section->reserved1, buf + 60);
396 bfd_h_put_32 (abfd, section->reserved2, buf + 64);
398 bfd_seek (abfd, offset, SEEK_SET);
399 if (bfd_bwrite ((PTR) buf, 68, abfd) != 68)
400 return -1;
402 return 0;
405 static int
406 bfd_mach_o_scan_write_section_64 (bfd *abfd,
407 bfd_mach_o_section *section,
408 bfd_vma offset)
410 unsigned char buf[80];
412 memcpy (buf, section->sectname, 16);
413 memcpy (buf + 16, section->segname, 16);
414 bfd_h_put_64 (abfd, section->addr, buf + 32);
415 bfd_h_put_64 (abfd, section->size, buf + 40);
416 bfd_h_put_32 (abfd, section->offset, buf + 48);
417 bfd_h_put_32 (abfd, section->align, buf + 52);
418 bfd_h_put_32 (abfd, section->reloff, buf + 56);
419 bfd_h_put_32 (abfd, section->nreloc, buf + 60);
420 bfd_h_put_32 (abfd, section->flags, buf + 64);
421 bfd_h_put_32 (abfd, section->reserved1, buf + 68);
422 bfd_h_put_32 (abfd, section->reserved2, buf + 72);
423 bfd_h_put_32 (abfd, section->reserved3, buf + 76);
425 bfd_seek (abfd, offset, SEEK_SET);
426 if (bfd_bwrite ((PTR) buf, 80, abfd) != 80)
427 return -1;
429 return 0;
432 static int
433 bfd_mach_o_scan_write_section (bfd *abfd,
434 bfd_mach_o_section *section,
435 bfd_vma offset,
436 unsigned int wide)
438 if (wide)
439 return bfd_mach_o_scan_write_section_64 (abfd, section, offset);
440 else
441 return bfd_mach_o_scan_write_section_32 (abfd, section, offset);
444 static int
445 bfd_mach_o_scan_write_segment (bfd *abfd,
446 bfd_mach_o_load_command *command,
447 unsigned int wide)
449 unsigned char buf[64];
450 bfd_mach_o_segment_command *seg = &command->command.segment;
451 unsigned long i;
453 if (wide)
455 BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT_64);
457 memcpy (buf, seg->segname, 16);
459 bfd_h_put_64 (abfd, seg->vmaddr, buf + 16);
460 bfd_h_put_64 (abfd, seg->vmsize, buf + 24);
461 bfd_h_put_64 (abfd, seg->fileoff, buf + 32);
462 bfd_h_put_64 (abfd, seg->filesize, buf + 40);
463 bfd_h_put_32 (abfd, seg->maxprot, buf + 48);
464 bfd_h_put_32 (abfd, seg->initprot, buf + 52);
465 bfd_h_put_32 (abfd, seg->nsects, buf + 56);
466 bfd_h_put_32 (abfd, seg->flags, buf + 60);
468 bfd_seek (abfd, command->offset + 8, SEEK_SET);
469 if (bfd_bwrite ((PTR) buf, 64, abfd) != 64)
470 return -1;
472 else
474 BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT);
476 memcpy (buf, seg->segname, 16);
478 bfd_h_put_32 (abfd, seg->vmaddr, buf + 16);
479 bfd_h_put_32 (abfd, seg->vmsize, buf + 20);
480 bfd_h_put_32 (abfd, seg->fileoff, buf + 24);
481 bfd_h_put_32 (abfd, seg->filesize, buf + 28);
482 bfd_h_put_32 (abfd, seg->maxprot, buf + 32);
483 bfd_h_put_32 (abfd, seg->initprot, buf + 36);
484 bfd_h_put_32 (abfd, seg->nsects, buf + 40);
485 bfd_h_put_32 (abfd, seg->flags, buf + 44);
487 bfd_seek (abfd, command->offset + 8, SEEK_SET);
488 if (bfd_bwrite ((PTR) buf, 48, abfd) != 48)
489 return -1;
493 char buf[1024];
494 bfd_vma nbytes = seg->filesize;
495 bfd_vma curoff = seg->fileoff;
497 while (nbytes > 0)
499 bfd_vma thiswrite = nbytes;
501 if (thiswrite > 1024)
502 thiswrite = 1024;
504 bfd_seek (abfd, curoff, SEEK_SET);
505 if (bfd_bread ((PTR) buf, thiswrite, abfd) != thiswrite)
506 return -1;
508 bfd_seek (abfd, curoff, SEEK_SET);
509 if (bfd_bwrite ((PTR) buf, thiswrite, abfd) != thiswrite)
510 return -1;
512 nbytes -= thiswrite;
513 curoff += thiswrite;
517 for (i = 0; i < seg->nsects; i++)
519 bfd_vma segoff;
520 if (wide)
521 segoff = command->offset + 64 + 8 + (i * 80);
522 else
523 segoff = command->offset + 48 + 8 + (i * 68);
525 if (bfd_mach_o_scan_write_section
526 (abfd, &seg->sections[i], segoff, wide) != 0)
527 return -1;
530 return 0;
533 static int
534 bfd_mach_o_scan_write_segment_32 (bfd *abfd, bfd_mach_o_load_command *command)
536 return bfd_mach_o_scan_write_segment (abfd, command, 0);
539 static int
540 bfd_mach_o_scan_write_segment_64 (bfd *abfd, bfd_mach_o_load_command *command)
542 return bfd_mach_o_scan_write_segment (abfd, command, 1);
545 static int
546 bfd_mach_o_scan_write_symtab_symbols (bfd *abfd, bfd_mach_o_load_command *command)
548 bfd_mach_o_symtab_command *sym = &command->command.symtab;
549 asymbol *s = NULL;
550 unsigned long i;
552 for (i = 0; i < sym->nsyms; i++)
554 unsigned char buf[12];
555 bfd_vma symoff = sym->symoff + (i * 12);
556 unsigned char ntype = 0;
557 unsigned char nsect = 0;
558 short ndesc = 0;
560 s = &sym->symbols[i];
562 /* Instead just set from the stored values. */
563 ntype = BFD_MACH_O_SYM_NTYPE (s);
564 nsect = BFD_MACH_O_SYM_NSECT (s);
565 ndesc = BFD_MACH_O_SYM_NDESC (s);
567 bfd_h_put_32 (abfd, s->name - sym->strtab, buf);
568 bfd_h_put_8 (abfd, ntype, buf + 4);
569 bfd_h_put_8 (abfd, nsect, buf + 5);
570 bfd_h_put_16 (abfd, ndesc, buf + 6);
571 bfd_h_put_32 (abfd, s->section->vma + s->value, buf + 8);
573 bfd_seek (abfd, symoff, SEEK_SET);
574 if (bfd_bwrite ((PTR) buf, 12, abfd) != 12)
576 fprintf (stderr, "bfd_mach_o_scan_write_symtab_symbols: unable to write %d bytes at %lu\n",
577 12, (unsigned long) symoff);
578 return -1;
582 return 0;
585 static int
586 bfd_mach_o_scan_write_symtab (bfd *abfd, bfd_mach_o_load_command *command)
588 bfd_mach_o_symtab_command *seg = &command->command.symtab;
589 unsigned char buf[16];
591 BFD_ASSERT (command->type == BFD_MACH_O_LC_SYMTAB);
593 bfd_h_put_32 (abfd, seg->symoff, buf);
594 bfd_h_put_32 (abfd, seg->nsyms, buf + 4);
595 bfd_h_put_32 (abfd, seg->stroff, buf + 8);
596 bfd_h_put_32 (abfd, seg->strsize, buf + 12);
598 bfd_seek (abfd, command->offset + 8, SEEK_SET);
599 if (bfd_bwrite ((PTR) buf, 16, abfd) != 16)
600 return -1;
602 if (bfd_mach_o_scan_write_symtab_symbols (abfd, command) != 0)
603 return -1;
605 return 0;
608 static bfd_boolean
609 bfd_mach_o_write_contents (bfd *abfd)
611 unsigned int i;
612 asection *s;
614 bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
616 /* Write data sections first in case they overlap header data to be
617 written later. */
619 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
622 /* Now write header information. */
623 if (bfd_mach_o_write_header (abfd, &mdata->header) != 0)
624 return FALSE;
626 for (i = 0; i < mdata->header.ncmds; i++)
628 unsigned char buf[8];
629 bfd_mach_o_load_command *cur = &mdata->commands[i];
630 unsigned long typeflag;
632 typeflag = cur->type_required ? cur->type & BFD_MACH_O_LC_REQ_DYLD : cur->type;
634 bfd_h_put_32 (abfd, typeflag, buf);
635 bfd_h_put_32 (abfd, cur->len, buf + 4);
637 bfd_seek (abfd, cur->offset, SEEK_SET);
638 if (bfd_bwrite ((PTR) buf, 8, abfd) != 8)
639 return FALSE;
641 switch (cur->type)
643 case BFD_MACH_O_LC_SEGMENT:
644 if (bfd_mach_o_scan_write_segment_32 (abfd, cur) != 0)
645 return FALSE;
646 break;
647 case BFD_MACH_O_LC_SEGMENT_64:
648 if (bfd_mach_o_scan_write_segment_64 (abfd, cur) != 0)
649 return FALSE;
650 break;
651 case BFD_MACH_O_LC_SYMTAB:
652 if (bfd_mach_o_scan_write_symtab (abfd, cur) != 0)
653 return FALSE;
654 break;
655 case BFD_MACH_O_LC_SYMSEG:
656 break;
657 case BFD_MACH_O_LC_THREAD:
658 case BFD_MACH_O_LC_UNIXTHREAD:
659 if (bfd_mach_o_scan_write_thread (abfd, cur) != 0)
660 return FALSE;
661 break;
662 case BFD_MACH_O_LC_LOADFVMLIB:
663 case BFD_MACH_O_LC_IDFVMLIB:
664 case BFD_MACH_O_LC_IDENT:
665 case BFD_MACH_O_LC_FVMFILE:
666 case BFD_MACH_O_LC_PREPAGE:
667 case BFD_MACH_O_LC_DYSYMTAB:
668 case BFD_MACH_O_LC_LOAD_DYLIB:
669 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB:
670 case BFD_MACH_O_LC_ID_DYLIB:
671 case BFD_MACH_O_LC_LOAD_DYLINKER:
672 case BFD_MACH_O_LC_ID_DYLINKER:
673 case BFD_MACH_O_LC_PREBOUND_DYLIB:
674 case BFD_MACH_O_LC_ROUTINES:
675 case BFD_MACH_O_LC_SUB_FRAMEWORK:
676 break;
677 default:
678 fprintf (stderr,
679 "unable to write unknown load command 0x%lx\n",
680 (unsigned long) cur->type);
681 return FALSE;
685 return TRUE;
688 static int
689 bfd_mach_o_sizeof_headers (bfd *a ATTRIBUTE_UNUSED,
690 struct bfd_link_info *info ATTRIBUTE_UNUSED)
692 return 0;
695 /* Make an empty symbol. This is required only because
696 bfd_make_section_anyway wants to create a symbol for the section. */
698 static asymbol *
699 bfd_mach_o_make_empty_symbol (bfd *abfd)
701 asymbol *new;
703 new = bfd_zalloc (abfd, sizeof (* new));
704 if (new == NULL)
705 return new;
706 new->the_bfd = abfd;
707 return new;
710 static int
711 bfd_mach_o_read_header (bfd *abfd, bfd_mach_o_header *header)
713 unsigned char buf[32];
714 unsigned int size;
715 bfd_vma (*get32) (const void *) = NULL;
717 bfd_seek (abfd, 0, SEEK_SET);
719 /* Just read the magic number. */
720 if (bfd_bread ((PTR) buf, 4, abfd) != 4)
721 return -1;
723 if (bfd_getb32 (buf) == 0xfeedface)
725 header->byteorder = BFD_ENDIAN_BIG;
726 header->magic = 0xfeedface;
727 header->version = 1;
728 get32 = bfd_getb32;
730 else if (bfd_getl32 (buf) == 0xfeedface)
732 header->byteorder = BFD_ENDIAN_LITTLE;
733 header->magic = 0xfeedface;
734 header->version = 1;
735 get32 = bfd_getl32;
737 else if (bfd_getb32 (buf) == 0xfeedfacf)
739 header->byteorder = BFD_ENDIAN_BIG;
740 header->magic = 0xfeedfacf;
741 header->version = 2;
742 get32 = bfd_getb32;
744 else if (bfd_getl32 (buf) == 0xfeedfacf)
746 header->byteorder = BFD_ENDIAN_LITTLE;
747 header->magic = 0xfeedfacf;
748 header->version = 2;
749 get32 = bfd_getl32;
751 else
753 header->byteorder = BFD_ENDIAN_UNKNOWN;
754 return -1;
757 /* Once the size of the header is known, read the full header. */
758 size = (header->version == 2) ? 32 : 28;
760 bfd_seek (abfd, 0, SEEK_SET);
761 if (bfd_bread ((PTR) buf, size, abfd) != size)
762 return -1;
764 header->cputype = (*get32) (buf + 4);
765 header->cpusubtype = (*get32) (buf + 8);
766 header->filetype = (*get32) (buf + 12);
767 header->ncmds = (*get32) (buf + 16);
768 header->sizeofcmds = (*get32) (buf + 20);
769 header->flags = (*get32) (buf + 24);
771 if (header->version == 2)
772 header->reserved = (*get32) (buf + 28);
774 return 0;
777 static asection *
778 bfd_mach_o_make_bfd_section (bfd *abfd, bfd_mach_o_section *section,
779 unsigned long prot)
781 asection *bfdsec;
782 char *sname;
783 const char *prefix = "LC_SEGMENT";
784 unsigned int snamelen;
785 flagword flags;
787 snamelen = strlen (prefix) + 1
788 + strlen (section->segname) + 1
789 + strlen (section->sectname) + 1;
791 sname = bfd_alloc (abfd, snamelen);
792 if (sname == NULL)
793 return NULL;
795 /* Use canonical dwarf section names for dwarf sections. */
796 if (strcmp (section->segname, "__DWARF") == 0
797 && strncmp (section->sectname, "__", 2) == 0)
798 sprintf (sname, ".%s", section->sectname + 2);
799 else
800 sprintf (sname, "%s.%s.%s", prefix, section->segname, section->sectname);
802 if (section->flags & BFD_MACH_O_S_ATTR_DEBUG)
803 flags = SEC_HAS_CONTENTS | SEC_DEBUGGING;
804 else
806 flags = SEC_ALLOC;
807 if ((section->flags & BFD_MACH_O_SECTION_TYPE_MASK)
808 != BFD_MACH_O_S_ZEROFILL)
810 flags |= SEC_HAS_CONTENTS | SEC_LOAD;
811 if (prot & BFD_MACH_O_PROT_EXECUTE)
812 flags |= SEC_CODE;
813 if (prot & BFD_MACH_O_PROT_WRITE)
814 flags |= SEC_DATA;
815 else if (prot & BFD_MACH_O_PROT_READ)
816 flags |= SEC_READONLY;
819 bfdsec = bfd_make_section_anyway_with_flags (abfd, sname, flags);
820 if (bfdsec == NULL)
821 return NULL;
823 bfdsec->vma = section->addr;
824 bfdsec->lma = section->addr;
825 bfdsec->size = section->size;
826 bfdsec->filepos = section->offset;
827 bfdsec->alignment_power = section->align;
828 bfdsec->segment_mark = 0;
830 return bfdsec;
833 static int
834 bfd_mach_o_scan_read_section_32 (bfd *abfd,
835 bfd_mach_o_section *section,
836 bfd_vma offset,
837 unsigned long prot)
839 unsigned char buf[68];
841 bfd_seek (abfd, offset, SEEK_SET);
842 if (bfd_bread ((PTR) buf, 68, abfd) != 68)
843 return -1;
845 memcpy (section->sectname, buf, 16);
846 section->sectname[16] = '\0';
847 memcpy (section->segname, buf + 16, 16);
848 section->segname[16] = '\0';
849 section->addr = bfd_h_get_32 (abfd, buf + 32);
850 section->size = bfd_h_get_32 (abfd, buf + 36);
851 section->offset = bfd_h_get_32 (abfd, buf + 40);
852 section->align = bfd_h_get_32 (abfd, buf + 44);
853 section->reloff = bfd_h_get_32 (abfd, buf + 48);
854 section->nreloc = bfd_h_get_32 (abfd, buf + 52);
855 section->flags = bfd_h_get_32 (abfd, buf + 56);
856 section->reserved1 = bfd_h_get_32 (abfd, buf + 60);
857 section->reserved2 = bfd_h_get_32 (abfd, buf + 64);
858 section->reserved3 = 0;
859 section->bfdsection = bfd_mach_o_make_bfd_section (abfd, section, prot);
861 if (section->bfdsection == NULL)
862 return -1;
864 return 0;
867 static int
868 bfd_mach_o_scan_read_section_64 (bfd *abfd,
869 bfd_mach_o_section *section,
870 bfd_vma offset,
871 unsigned long prot)
873 unsigned char buf[80];
875 bfd_seek (abfd, offset, SEEK_SET);
876 if (bfd_bread ((PTR) buf, 80, abfd) != 80)
877 return -1;
879 memcpy (section->sectname, buf, 16);
880 section->sectname[16] = '\0';
881 memcpy (section->segname, buf + 16, 16);
882 section->segname[16] = '\0';
883 section->addr = bfd_h_get_64 (abfd, buf + 32);
884 section->size = bfd_h_get_64 (abfd, buf + 40);
885 section->offset = bfd_h_get_32 (abfd, buf + 48);
886 section->align = bfd_h_get_32 (abfd, buf + 52);
887 section->reloff = bfd_h_get_32 (abfd, buf + 56);
888 section->nreloc = bfd_h_get_32 (abfd, buf + 60);
889 section->flags = bfd_h_get_32 (abfd, buf + 64);
890 section->reserved1 = bfd_h_get_32 (abfd, buf + 68);
891 section->reserved2 = bfd_h_get_32 (abfd, buf + 72);
892 section->reserved3 = bfd_h_get_32 (abfd, buf + 76);
893 section->bfdsection = bfd_mach_o_make_bfd_section (abfd, section, prot);
895 if (section->bfdsection == NULL)
896 return -1;
898 return 0;
901 static int
902 bfd_mach_o_scan_read_section (bfd *abfd,
903 bfd_mach_o_section *section,
904 bfd_vma offset,
905 unsigned long prot,
906 unsigned int wide)
908 if (wide)
909 return bfd_mach_o_scan_read_section_64 (abfd, section, offset, prot);
910 else
911 return bfd_mach_o_scan_read_section_32 (abfd, section, offset, prot);
915 bfd_mach_o_scan_read_symtab_symbol (bfd *abfd,
916 bfd_mach_o_symtab_command *sym,
917 asymbol *s,
918 unsigned long i)
920 bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
921 unsigned int wide = (mdata->header.version == 2);
922 unsigned int symwidth = wide ? 16 : 12;
923 bfd_vma symoff = sym->symoff + (i * symwidth);
924 unsigned char buf[16];
925 unsigned char type = -1;
926 unsigned char section = -1;
927 short desc = -1;
928 symvalue value = -1;
929 unsigned long stroff = -1;
930 unsigned int symtype = -1;
932 BFD_ASSERT (sym->strtab != NULL);
934 bfd_seek (abfd, symoff, SEEK_SET);
935 if (bfd_bread ((PTR) buf, symwidth, abfd) != symwidth)
937 fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: unable to read %d bytes at %lu\n",
938 symwidth, (unsigned long) symoff);
939 return -1;
942 stroff = bfd_h_get_32 (abfd, buf);
943 type = bfd_h_get_8 (abfd, buf + 4);
944 symtype = (type & 0x0e);
945 section = bfd_h_get_8 (abfd, buf + 5);
946 desc = bfd_h_get_16 (abfd, buf + 6);
947 if (wide)
948 value = bfd_h_get_64 (abfd, buf + 8);
949 else
950 value = bfd_h_get_32 (abfd, buf + 8);
952 if (stroff >= sym->strsize)
954 fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: symbol name out of range (%lu >= %lu)\n",
955 (unsigned long) stroff, (unsigned long) sym->strsize);
956 return -1;
959 s->the_bfd = abfd;
960 s->name = sym->strtab + stroff;
961 s->value = value;
962 s->udata.i = (type << 24) | (section << 16) | desc;
963 s->flags = 0x0;
965 if (type & BFD_MACH_O_N_STAB)
967 s->flags |= BSF_DEBUGGING;
968 s->section = bfd_und_section_ptr;
969 switch (type)
971 case N_FUN:
972 case N_STSYM:
973 case N_LCSYM:
974 case N_BNSYM:
975 case N_SLINE:
976 case N_ENSYM:
977 case N_ECOMM:
978 case N_ECOML:
979 case N_GSYM:
980 if ((section > 0) && (section <= mdata->nsects))
982 s->section = mdata->sections[section - 1]->bfdsection;
983 s->value = s->value - mdata->sections[section - 1]->addr;
985 break;
988 else
990 if (type & BFD_MACH_O_N_PEXT)
991 s->flags |= BSF_GLOBAL;
993 if (type & BFD_MACH_O_N_EXT)
994 s->flags |= BSF_GLOBAL;
996 if (!(type & (BFD_MACH_O_N_PEXT | BFD_MACH_O_N_EXT)))
997 s->flags |= BSF_LOCAL;
999 switch (symtype)
1001 case BFD_MACH_O_N_UNDF:
1002 s->section = bfd_und_section_ptr;
1003 break;
1004 case BFD_MACH_O_N_PBUD:
1005 s->section = bfd_und_section_ptr;
1006 break;
1007 case BFD_MACH_O_N_ABS:
1008 s->section = bfd_abs_section_ptr;
1009 break;
1010 case BFD_MACH_O_N_SECT:
1011 if ((section > 0) && (section <= mdata->nsects))
1013 s->section = mdata->sections[section - 1]->bfdsection;
1014 s->value = s->value - mdata->sections[section - 1]->addr;
1016 else
1018 /* Mach-O uses 0 to mean "no section"; not an error. */
1019 if (section != 0)
1021 fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: "
1022 "symbol \"%s\" specified invalid section %d (max %lu): setting to undefined\n",
1023 s->name, section, mdata->nsects);
1025 s->section = bfd_und_section_ptr;
1027 break;
1028 case BFD_MACH_O_N_INDR:
1029 fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: "
1030 "symbol \"%s\" is unsupported 'indirect' reference: setting to undefined\n",
1031 s->name);
1032 s->section = bfd_und_section_ptr;
1033 break;
1034 default:
1035 fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: "
1036 "symbol \"%s\" specified invalid type field 0x%x: setting to undefined\n",
1037 s->name, symtype);
1038 s->section = bfd_und_section_ptr;
1039 break;
1043 return 0;
1047 bfd_mach_o_scan_read_symtab_strtab (bfd *abfd,
1048 bfd_mach_o_symtab_command *sym)
1050 BFD_ASSERT (sym->strtab == NULL);
1052 if (abfd->flags & BFD_IN_MEMORY)
1054 struct bfd_in_memory *b;
1056 b = (struct bfd_in_memory *) abfd->iostream;
1058 if ((sym->stroff + sym->strsize) > b->size)
1060 bfd_set_error (bfd_error_file_truncated);
1061 return -1;
1063 sym->strtab = (char *) b->buffer + sym->stroff;
1064 return 0;
1067 sym->strtab = bfd_alloc (abfd, sym->strsize);
1068 if (sym->strtab == NULL)
1069 return -1;
1071 bfd_seek (abfd, sym->stroff, SEEK_SET);
1072 if (bfd_bread ((PTR) sym->strtab, sym->strsize, abfd) != sym->strsize)
1074 fprintf (stderr, "bfd_mach_o_scan_read_symtab_strtab: unable to read %lu bytes at %lu\n",
1075 sym->strsize, sym->stroff);
1076 return -1;
1079 return 0;
1083 bfd_mach_o_scan_read_symtab_symbols (bfd *abfd,
1084 bfd_mach_o_symtab_command *sym)
1086 unsigned long i;
1087 int ret;
1089 BFD_ASSERT (sym->symbols == NULL);
1090 sym->symbols = bfd_alloc (abfd, sym->nsyms * sizeof (asymbol));
1092 if (sym->symbols == NULL)
1094 fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbols: unable to allocate memory for symbols\n");
1095 return -1;
1098 ret = bfd_mach_o_scan_read_symtab_strtab (abfd, sym);
1099 if (ret != 0)
1100 return ret;
1102 for (i = 0; i < sym->nsyms; i++)
1104 ret = bfd_mach_o_scan_read_symtab_symbol (abfd, sym, &sym->symbols[i], i);
1105 if (ret != 0)
1106 return ret;
1109 return 0;
1113 bfd_mach_o_scan_read_dysymtab_symbol (bfd *abfd,
1114 bfd_mach_o_dysymtab_command *dysym,
1115 bfd_mach_o_symtab_command *sym,
1116 asymbol *s,
1117 unsigned long i)
1119 unsigned long isymoff = dysym->indirectsymoff + (i * 4);
1120 unsigned long symindex;
1121 unsigned char buf[4];
1123 BFD_ASSERT (i < dysym->nindirectsyms);
1125 bfd_seek (abfd, isymoff, SEEK_SET);
1126 if (bfd_bread ((PTR) buf, 4, abfd) != 4)
1128 fprintf (stderr, "bfd_mach_o_scan_read_dysymtab_symbol: unable to read %lu bytes at %lu\n",
1129 (unsigned long) 4, isymoff);
1130 return -1;
1132 symindex = bfd_h_get_32 (abfd, buf);
1134 return bfd_mach_o_scan_read_symtab_symbol (abfd, sym, s, symindex);
1137 static const char *
1138 bfd_mach_o_i386_flavour_string (unsigned int flavour)
1140 switch ((int) flavour)
1142 case BFD_MACH_O_x86_THREAD_STATE32: return "x86_THREAD_STATE32";
1143 case BFD_MACH_O_x86_FLOAT_STATE32: return "x86_FLOAT_STATE32";
1144 case BFD_MACH_O_x86_EXCEPTION_STATE32: return "x86_EXCEPTION_STATE32";
1145 case BFD_MACH_O_x86_THREAD_STATE64: return "x86_THREAD_STATE64";
1146 case BFD_MACH_O_x86_FLOAT_STATE64: return "x86_FLOAT_STATE64";
1147 case BFD_MACH_O_x86_EXCEPTION_STATE64: return "x86_EXCEPTION_STATE64";
1148 case BFD_MACH_O_x86_THREAD_STATE: return "x86_THREAD_STATE";
1149 case BFD_MACH_O_x86_FLOAT_STATE: return "x86_FLOAT_STATE";
1150 case BFD_MACH_O_x86_EXCEPTION_STATE: return "x86_EXCEPTION_STATE";
1151 case BFD_MACH_O_x86_DEBUG_STATE32: return "x86_DEBUG_STATE32";
1152 case BFD_MACH_O_x86_DEBUG_STATE64: return "x86_DEBUG_STATE64";
1153 case BFD_MACH_O_x86_DEBUG_STATE: return "x86_DEBUG_STATE";
1154 case BFD_MACH_O_THREAD_STATE_NONE: return "THREAD_STATE_NONE";
1155 default: return "UNKNOWN";
1159 static const char *
1160 bfd_mach_o_ppc_flavour_string (unsigned int flavour)
1162 switch ((int) flavour)
1164 case BFD_MACH_O_PPC_THREAD_STATE: return "PPC_THREAD_STATE";
1165 case BFD_MACH_O_PPC_FLOAT_STATE: return "PPC_FLOAT_STATE";
1166 case BFD_MACH_O_PPC_EXCEPTION_STATE: return "PPC_EXCEPTION_STATE";
1167 case BFD_MACH_O_PPC_VECTOR_STATE: return "PPC_VECTOR_STATE";
1168 default: return "UNKNOWN";
1172 static int
1173 bfd_mach_o_scan_read_dylinker (bfd *abfd,
1174 bfd_mach_o_load_command *command)
1176 bfd_mach_o_dylinker_command *cmd = &command->command.dylinker;
1177 unsigned char buf[4];
1178 unsigned int nameoff;
1179 asection *bfdsec;
1180 char *sname;
1181 const char *prefix;
1183 BFD_ASSERT ((command->type == BFD_MACH_O_LC_ID_DYLINKER)
1184 || (command->type == BFD_MACH_O_LC_LOAD_DYLINKER));
1186 bfd_seek (abfd, command->offset + 8, SEEK_SET);
1187 if (bfd_bread ((PTR) buf, 4, abfd) != 4)
1188 return -1;
1190 nameoff = bfd_h_get_32 (abfd, buf + 0);
1192 cmd->name_offset = command->offset + nameoff;
1193 cmd->name_len = command->len - nameoff;
1195 if (command->type == BFD_MACH_O_LC_LOAD_DYLINKER)
1196 prefix = "LC_LOAD_DYLINKER";
1197 else if (command->type == BFD_MACH_O_LC_ID_DYLINKER)
1198 prefix = "LC_ID_DYLINKER";
1199 else
1200 abort ();
1202 sname = bfd_alloc (abfd, strlen (prefix) + 1);
1203 if (sname == NULL)
1204 return -1;
1205 strcpy (sname, prefix);
1207 bfdsec = bfd_make_section_anyway_with_flags (abfd, sname, SEC_HAS_CONTENTS);
1208 if (bfdsec == NULL)
1209 return -1;
1211 bfdsec->vma = 0;
1212 bfdsec->lma = 0;
1213 bfdsec->size = command->len - 8;
1214 bfdsec->filepos = command->offset + 8;
1215 bfdsec->alignment_power = 0;
1217 cmd->section = bfdsec;
1219 return 0;
1222 static int
1223 bfd_mach_o_scan_read_dylib (bfd *abfd, bfd_mach_o_load_command *command)
1225 bfd_mach_o_dylib_command *cmd = &command->command.dylib;
1226 unsigned char buf[16];
1227 unsigned int nameoff;
1228 asection *bfdsec;
1229 char *sname;
1230 const char *prefix;
1232 BFD_ASSERT ((command->type == BFD_MACH_O_LC_ID_DYLIB)
1233 || (command->type == BFD_MACH_O_LC_LOAD_DYLIB)
1234 || (command->type == BFD_MACH_O_LC_LOAD_WEAK_DYLIB));
1236 bfd_seek (abfd, command->offset + 8, SEEK_SET);
1237 if (bfd_bread ((PTR) buf, 16, abfd) != 16)
1238 return -1;
1240 nameoff = bfd_h_get_32 (abfd, buf + 0);
1241 cmd->timestamp = bfd_h_get_32 (abfd, buf + 4);
1242 cmd->current_version = bfd_h_get_32 (abfd, buf + 8);
1243 cmd->compatibility_version = bfd_h_get_32 (abfd, buf + 12);
1245 cmd->name_offset = command->offset + nameoff;
1246 cmd->name_len = command->len - nameoff;
1248 if (command->type == BFD_MACH_O_LC_LOAD_DYLIB)
1249 prefix = "LC_LOAD_DYLIB";
1250 else if (command->type == BFD_MACH_O_LC_LOAD_WEAK_DYLIB)
1251 prefix = "LC_LOAD_WEAK_DYLIB";
1252 else if (command->type == BFD_MACH_O_LC_ID_DYLIB)
1253 prefix = "LC_ID_DYLIB";
1254 else
1255 abort ();
1257 sname = bfd_alloc (abfd, strlen (prefix) + 1);
1258 if (sname == NULL)
1259 return -1;
1260 strcpy (sname, prefix);
1262 bfdsec = bfd_make_section_anyway_with_flags (abfd, sname, SEC_HAS_CONTENTS);
1263 if (bfdsec == NULL)
1264 return -1;
1266 bfdsec->vma = 0;
1267 bfdsec->lma = 0;
1268 bfdsec->size = command->len - 8;
1269 bfdsec->filepos = command->offset + 8;
1270 bfdsec->alignment_power = 0;
1272 cmd->section = bfdsec;
1274 return 0;
1277 static int
1278 bfd_mach_o_scan_read_prebound_dylib (bfd *abfd ATTRIBUTE_UNUSED,
1279 bfd_mach_o_load_command *command ATTRIBUTE_UNUSED)
1281 /* bfd_mach_o_prebound_dylib_command *cmd = &command->command.prebound_dylib; */
1283 BFD_ASSERT (command->type == BFD_MACH_O_LC_PREBOUND_DYLIB);
1284 return 0;
1287 static int
1288 bfd_mach_o_scan_read_thread (bfd *abfd, bfd_mach_o_load_command *command)
1290 bfd_mach_o_data_struct *mdata = NULL;
1291 bfd_mach_o_thread_command *cmd = &command->command.thread;
1292 unsigned char buf[8];
1293 bfd_vma offset;
1294 unsigned int nflavours;
1295 unsigned int i;
1297 BFD_ASSERT ((command->type == BFD_MACH_O_LC_THREAD)
1298 || (command->type == BFD_MACH_O_LC_UNIXTHREAD));
1300 BFD_ASSERT (bfd_mach_o_valid (abfd));
1301 mdata = abfd->tdata.mach_o_data;
1303 offset = 8;
1304 nflavours = 0;
1305 while (offset != command->len)
1307 if (offset >= command->len)
1308 return -1;
1310 bfd_seek (abfd, command->offset + offset, SEEK_SET);
1312 if (bfd_bread ((PTR) buf, 8, abfd) != 8)
1313 return -1;
1315 offset += 8 + bfd_h_get_32 (abfd, buf + 4) * 4;
1316 nflavours++;
1319 cmd->flavours = bfd_alloc (abfd, nflavours * sizeof (bfd_mach_o_thread_flavour));
1320 if (cmd->flavours == NULL)
1321 return -1;
1322 cmd->nflavours = nflavours;
1324 offset = 8;
1325 nflavours = 0;
1326 while (offset != command->len)
1328 if (offset >= command->len)
1329 return -1;
1331 if (nflavours >= cmd->nflavours)
1332 return -1;
1334 bfd_seek (abfd, command->offset + offset, SEEK_SET);
1336 if (bfd_bread ((PTR) buf, 8, abfd) != 8)
1337 return -1;
1339 cmd->flavours[nflavours].flavour = bfd_h_get_32 (abfd, buf);
1340 cmd->flavours[nflavours].offset = command->offset + offset + 8;
1341 cmd->flavours[nflavours].size = bfd_h_get_32 (abfd, buf + 4) * 4;
1342 offset += cmd->flavours[nflavours].size + 8;
1343 nflavours++;
1346 for (i = 0; i < nflavours; i++)
1348 asection *bfdsec;
1349 unsigned int snamelen;
1350 char *sname;
1351 const char *flavourstr;
1352 const char *prefix = "LC_THREAD";
1353 unsigned int j = 0;
1355 switch (mdata->header.cputype)
1357 case BFD_MACH_O_CPU_TYPE_POWERPC:
1358 case BFD_MACH_O_CPU_TYPE_POWERPC_64:
1359 flavourstr = bfd_mach_o_ppc_flavour_string (cmd->flavours[i].flavour);
1360 break;
1361 case BFD_MACH_O_CPU_TYPE_I386:
1362 case BFD_MACH_O_CPU_TYPE_X86_64:
1363 flavourstr = bfd_mach_o_i386_flavour_string (cmd->flavours[i].flavour);
1364 break;
1365 default:
1366 flavourstr = "UNKNOWN_ARCHITECTURE";
1367 break;
1370 snamelen = strlen (prefix) + 1 + 20 + 1 + strlen (flavourstr) + 1;
1371 sname = bfd_alloc (abfd, snamelen);
1372 if (sname == NULL)
1373 return -1;
1375 for (;;)
1377 sprintf (sname, "%s.%s.%u", prefix, flavourstr, j);
1378 if (bfd_get_section_by_name (abfd, sname) == NULL)
1379 break;
1380 j++;
1383 bfdsec = bfd_make_section_with_flags (abfd, sname, SEC_HAS_CONTENTS);
1385 bfdsec->vma = 0;
1386 bfdsec->lma = 0;
1387 bfdsec->size = cmd->flavours[i].size;
1388 bfdsec->filepos = cmd->flavours[i].offset;
1389 bfdsec->alignment_power = 0x0;
1391 cmd->section = bfdsec;
1394 return 0;
1397 static int
1398 bfd_mach_o_scan_read_dysymtab (bfd *abfd, bfd_mach_o_load_command *command)
1400 bfd_mach_o_dysymtab_command *seg = &command->command.dysymtab;
1401 unsigned char buf[72];
1403 BFD_ASSERT (command->type == BFD_MACH_O_LC_DYSYMTAB);
1405 bfd_seek (abfd, command->offset + 8, SEEK_SET);
1406 if (bfd_bread ((PTR) buf, 72, abfd) != 72)
1407 return -1;
1409 seg->ilocalsym = bfd_h_get_32 (abfd, buf + 0);
1410 seg->nlocalsym = bfd_h_get_32 (abfd, buf + 4);
1411 seg->iextdefsym = bfd_h_get_32 (abfd, buf + 8);
1412 seg->nextdefsym = bfd_h_get_32 (abfd, buf + 12);
1413 seg->iundefsym = bfd_h_get_32 (abfd, buf + 16);
1414 seg->nundefsym = bfd_h_get_32 (abfd, buf + 20);
1415 seg->tocoff = bfd_h_get_32 (abfd, buf + 24);
1416 seg->ntoc = bfd_h_get_32 (abfd, buf + 28);
1417 seg->modtaboff = bfd_h_get_32 (abfd, buf + 32);
1418 seg->nmodtab = bfd_h_get_32 (abfd, buf + 36);
1419 seg->extrefsymoff = bfd_h_get_32 (abfd, buf + 40);
1420 seg->nextrefsyms = bfd_h_get_32 (abfd, buf + 44);
1421 seg->indirectsymoff = bfd_h_get_32 (abfd, buf + 48);
1422 seg->nindirectsyms = bfd_h_get_32 (abfd, buf + 52);
1423 seg->extreloff = bfd_h_get_32 (abfd, buf + 56);
1424 seg->nextrel = bfd_h_get_32 (abfd, buf + 60);
1425 seg->locreloff = bfd_h_get_32 (abfd, buf + 64);
1426 seg->nlocrel = bfd_h_get_32 (abfd, buf + 68);
1428 return 0;
1431 static int
1432 bfd_mach_o_scan_read_symtab (bfd *abfd, bfd_mach_o_load_command *command)
1434 bfd_mach_o_symtab_command *seg = &command->command.symtab;
1435 unsigned char buf[16];
1436 asection *bfdsec;
1437 char *sname;
1438 const char *prefix = "LC_SYMTAB.stabs";
1439 int nlist_size = (bfd_mach_o_version (abfd) > 1) ? 16 : 12;
1441 BFD_ASSERT (command->type == BFD_MACH_O_LC_SYMTAB);
1443 bfd_seek (abfd, command->offset + 8, SEEK_SET);
1444 if (bfd_bread ((PTR) buf, 16, abfd) != 16)
1445 return -1;
1447 seg->symoff = bfd_h_get_32 (abfd, buf);
1448 seg->nsyms = bfd_h_get_32 (abfd, buf + 4);
1449 seg->stroff = bfd_h_get_32 (abfd, buf + 8);
1450 seg->strsize = bfd_h_get_32 (abfd, buf + 12);
1451 seg->symbols = NULL;
1452 seg->strtab = NULL;
1454 sname = bfd_alloc (abfd, strlen (prefix) + 1);
1455 if (sname == NULL)
1456 return -1;
1457 strcpy (sname, prefix);
1459 bfdsec = bfd_make_section_anyway_with_flags (abfd, sname, SEC_HAS_CONTENTS);
1460 if (bfdsec == NULL)
1461 return -1;
1463 bfdsec->vma = 0;
1464 bfdsec->lma = 0;
1465 bfdsec->size = seg->nsyms * nlist_size;
1466 bfdsec->filepos = seg->symoff;
1467 bfdsec->alignment_power = 0;
1469 seg->stabs_segment = bfdsec;
1471 if (seg->nsyms != 0)
1472 abfd->flags |= HAS_SYMS;
1474 prefix = "LC_SYMTAB.stabstr";
1475 sname = bfd_alloc (abfd, strlen (prefix) + 1);
1476 if (sname == NULL)
1477 return -1;
1478 strcpy (sname, prefix);
1480 bfdsec = bfd_make_section_anyway_with_flags (abfd, sname, SEC_HAS_CONTENTS);
1481 if (bfdsec == NULL)
1482 return -1;
1484 bfdsec->vma = 0;
1485 bfdsec->lma = 0;
1486 bfdsec->size = seg->strsize;
1487 bfdsec->filepos = seg->stroff;
1488 bfdsec->alignment_power = 0;
1490 seg->stabstr_segment = bfdsec;
1492 return 0;
1495 static int
1496 bfd_mach_o_scan_read_uuid (bfd *abfd, bfd_mach_o_load_command *command)
1498 bfd_mach_o_uuid_command *cmd = &command->command.uuid;
1499 asection *bfdsec;
1500 char *sname;
1501 static const char prefix[] = "LC_UUID";
1503 BFD_ASSERT (command->type == BFD_MACH_O_LC_UUID);
1505 bfd_seek (abfd, command->offset + 8, SEEK_SET);
1506 if (bfd_bread ((PTR) cmd->uuid, 16, abfd) != 16)
1507 return -1;
1509 sname = bfd_alloc (abfd, strlen (prefix) + 1);
1510 if (sname == NULL)
1511 return -1;
1512 strcpy (sname, prefix);
1514 bfdsec = bfd_make_section_anyway_with_flags (abfd, sname, SEC_HAS_CONTENTS);
1515 if (bfdsec == NULL)
1516 return -1;
1518 bfdsec->vma = 0;
1519 bfdsec->lma = 0;
1520 bfdsec->size = command->len - 8;
1521 bfdsec->filepos = command->offset + 8;
1522 bfdsec->alignment_power = 0;
1524 cmd->section = bfdsec;
1526 return 0;
1529 static int
1530 bfd_mach_o_scan_read_segment (bfd *abfd,
1531 bfd_mach_o_load_command *command,
1532 unsigned int wide)
1534 unsigned char buf[64];
1535 bfd_mach_o_segment_command *seg = &command->command.segment;
1536 unsigned long i;
1537 asection *bfdsec;
1538 char *sname;
1539 const char *prefix = "LC_SEGMENT";
1540 unsigned int snamelen;
1542 if (wide)
1544 BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT_64);
1546 bfd_seek (abfd, command->offset + 8, SEEK_SET);
1547 if (bfd_bread ((PTR) buf, 64, abfd) != 64)
1548 return -1;
1550 memcpy (seg->segname, buf, 16);
1551 seg->segname[16] = '\0';
1553 seg->vmaddr = bfd_h_get_64 (abfd, buf + 16);
1554 seg->vmsize = bfd_h_get_64 (abfd, buf + 24);
1555 seg->fileoff = bfd_h_get_64 (abfd, buf + 32);
1556 seg->filesize = bfd_h_get_64 (abfd, buf + 40);
1557 seg->maxprot = bfd_h_get_32 (abfd, buf + 48);
1558 seg->initprot = bfd_h_get_32 (abfd, buf + 52);
1559 seg->nsects = bfd_h_get_32 (abfd, buf + 56);
1560 seg->flags = bfd_h_get_32 (abfd, buf + 60);
1562 else
1564 BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT);
1566 bfd_seek (abfd, command->offset + 8, SEEK_SET);
1567 if (bfd_bread ((PTR) buf, 48, abfd) != 48)
1568 return -1;
1570 memcpy (seg->segname, buf, 16);
1571 seg->segname[16] = '\0';
1573 seg->vmaddr = bfd_h_get_32 (abfd, buf + 16);
1574 seg->vmsize = bfd_h_get_32 (abfd, buf + 20);
1575 seg->fileoff = bfd_h_get_32 (abfd, buf + 24);
1576 seg->filesize = bfd_h_get_32 (abfd, buf + 28);
1577 seg->maxprot = bfd_h_get_32 (abfd, buf + 32);
1578 seg->initprot = bfd_h_get_32 (abfd, buf + 36);
1579 seg->nsects = bfd_h_get_32 (abfd, buf + 40);
1580 seg->flags = bfd_h_get_32 (abfd, buf + 44);
1583 snamelen = strlen (prefix) + 1 + strlen (seg->segname) + 1;
1584 sname = bfd_alloc (abfd, snamelen);
1585 if (sname == NULL)
1586 return -1;
1587 sprintf (sname, "%s.%s", prefix, seg->segname);
1589 bfdsec = bfd_make_section_anyway (abfd, sname);
1590 if (bfdsec == NULL)
1591 return -1;
1593 bfdsec->vma = seg->vmaddr;
1594 bfdsec->lma = seg->vmaddr;
1595 bfdsec->size = seg->filesize;
1596 bfdsec->filepos = seg->fileoff;
1597 bfdsec->alignment_power = 0x0;
1598 bfdsec->flags = SEC_HAS_CONTENTS;
1599 bfdsec->segment_mark = 1;
1601 seg->segment = bfdsec;
1603 if (seg->nsects != 0)
1605 seg->sections = bfd_alloc (abfd, seg->nsects * sizeof (bfd_mach_o_section));
1606 if (seg->sections == NULL)
1607 return -1;
1609 for (i = 0; i < seg->nsects; i++)
1611 bfd_vma segoff;
1612 if (wide)
1613 segoff = command->offset + 64 + 8 + (i * 80);
1614 else
1615 segoff = command->offset + 48 + 8 + (i * 68);
1617 if (bfd_mach_o_scan_read_section
1618 (abfd, &seg->sections[i], segoff, seg->initprot, wide) != 0)
1619 return -1;
1623 return 0;
1626 static int
1627 bfd_mach_o_scan_read_segment_32 (bfd *abfd, bfd_mach_o_load_command *command)
1629 return bfd_mach_o_scan_read_segment (abfd, command, 0);
1632 static int
1633 bfd_mach_o_scan_read_segment_64 (bfd *abfd, bfd_mach_o_load_command *command)
1635 return bfd_mach_o_scan_read_segment (abfd, command, 1);
1638 static int
1639 bfd_mach_o_scan_read_command (bfd *abfd, bfd_mach_o_load_command *command)
1641 unsigned char buf[8];
1643 bfd_seek (abfd, command->offset, SEEK_SET);
1644 if (bfd_bread ((PTR) buf, 8, abfd) != 8)
1645 return -1;
1647 command->type = (bfd_h_get_32 (abfd, buf) & ~BFD_MACH_O_LC_REQ_DYLD);
1648 command->type_required = (bfd_h_get_32 (abfd, buf) & BFD_MACH_O_LC_REQ_DYLD
1649 ? 1 : 0);
1650 command->len = bfd_h_get_32 (abfd, buf + 4);
1652 switch (command->type)
1654 case BFD_MACH_O_LC_SEGMENT:
1655 if (bfd_mach_o_scan_read_segment_32 (abfd, command) != 0)
1656 return -1;
1657 break;
1658 case BFD_MACH_O_LC_SEGMENT_64:
1659 if (bfd_mach_o_scan_read_segment_64 (abfd, command) != 0)
1660 return -1;
1661 break;
1662 case BFD_MACH_O_LC_SYMTAB:
1663 if (bfd_mach_o_scan_read_symtab (abfd, command) != 0)
1664 return -1;
1665 break;
1666 case BFD_MACH_O_LC_SYMSEG:
1667 break;
1668 case BFD_MACH_O_LC_THREAD:
1669 case BFD_MACH_O_LC_UNIXTHREAD:
1670 if (bfd_mach_o_scan_read_thread (abfd, command) != 0)
1671 return -1;
1672 break;
1673 case BFD_MACH_O_LC_LOAD_DYLINKER:
1674 case BFD_MACH_O_LC_ID_DYLINKER:
1675 if (bfd_mach_o_scan_read_dylinker (abfd, command) != 0)
1676 return -1;
1677 break;
1678 case BFD_MACH_O_LC_LOAD_DYLIB:
1679 case BFD_MACH_O_LC_ID_DYLIB:
1680 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB:
1681 if (bfd_mach_o_scan_read_dylib (abfd, command) != 0)
1682 return -1;
1683 break;
1684 case BFD_MACH_O_LC_PREBOUND_DYLIB:
1685 if (bfd_mach_o_scan_read_prebound_dylib (abfd, command) != 0)
1686 return -1;
1687 break;
1688 case BFD_MACH_O_LC_LOADFVMLIB:
1689 case BFD_MACH_O_LC_IDFVMLIB:
1690 case BFD_MACH_O_LC_IDENT:
1691 case BFD_MACH_O_LC_FVMFILE:
1692 case BFD_MACH_O_LC_PREPAGE:
1693 case BFD_MACH_O_LC_ROUTINES:
1694 case BFD_MACH_O_LC_SUB_FRAMEWORK:
1695 break;
1696 case BFD_MACH_O_LC_DYSYMTAB:
1697 if (bfd_mach_o_scan_read_dysymtab (abfd, command) != 0)
1698 return -1;
1699 break;
1700 case BFD_MACH_O_LC_SUB_UMBRELLA:
1701 case BFD_MACH_O_LC_SUB_CLIENT:
1702 case BFD_MACH_O_LC_SUB_LIBRARY:
1703 case BFD_MACH_O_LC_TWOLEVEL_HINTS:
1704 case BFD_MACH_O_LC_PREBIND_CKSUM:
1705 break;
1706 case BFD_MACH_O_LC_UUID:
1707 if (bfd_mach_o_scan_read_uuid (abfd, command) != 0)
1708 return -1;
1709 break;
1710 case BFD_MACH_O_LC_CODE_SIGNATURE:
1711 break;
1712 default:
1713 fprintf (stderr, "unable to read unknown load command 0x%lx\n",
1714 (unsigned long) command->type);
1715 break;
1718 return 0;
1721 static void
1722 bfd_mach_o_flatten_sections (bfd *abfd)
1724 bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
1725 long csect = 0;
1726 unsigned long i, j;
1728 /* Count total number of sections. */
1729 mdata->nsects = 0;
1731 for (i = 0; i < mdata->header.ncmds; i++)
1733 if (mdata->commands[i].type == BFD_MACH_O_LC_SEGMENT
1734 || mdata->commands[i].type == BFD_MACH_O_LC_SEGMENT_64)
1736 bfd_mach_o_segment_command *seg;
1738 seg = &mdata->commands[i].command.segment;
1739 mdata->nsects += seg->nsects;
1743 /* Allocate sections array. */
1744 mdata->sections = bfd_alloc (abfd,
1745 mdata->nsects * sizeof (bfd_mach_o_section *));
1747 /* Fill the array. */
1748 csect = 0;
1750 for (i = 0; i < mdata->header.ncmds; i++)
1752 if (mdata->commands[i].type == BFD_MACH_O_LC_SEGMENT
1753 || mdata->commands[i].type == BFD_MACH_O_LC_SEGMENT_64)
1755 bfd_mach_o_segment_command *seg;
1757 seg = &mdata->commands[i].command.segment;
1758 BFD_ASSERT (csect + seg->nsects <= mdata->nsects);
1760 for (j = 0; j < seg->nsects; j++)
1761 mdata->sections[csect++] = &seg->sections[j];
1767 bfd_mach_o_scan_start_address (bfd *abfd)
1769 bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
1770 bfd_mach_o_thread_command *cmd = NULL;
1771 unsigned long i;
1773 for (i = 0; i < mdata->header.ncmds; i++)
1775 if ((mdata->commands[i].type == BFD_MACH_O_LC_THREAD) ||
1776 (mdata->commands[i].type == BFD_MACH_O_LC_UNIXTHREAD))
1778 if (cmd == NULL)
1779 cmd = &mdata->commands[i].command.thread;
1780 else
1781 return 0;
1785 if (cmd == NULL)
1786 return 0;
1788 for (i = 0; i < cmd->nflavours; i++)
1790 if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_I386)
1791 && (cmd->flavours[i].flavour
1792 == (unsigned long) BFD_MACH_O_x86_THREAD_STATE32))
1794 unsigned char buf[4];
1796 bfd_seek (abfd, cmd->flavours[i].offset + 40, SEEK_SET);
1798 if (bfd_bread (buf, 4, abfd) != 4)
1799 return -1;
1801 abfd->start_address = bfd_h_get_32 (abfd, buf);
1803 else if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_POWERPC)
1804 && (cmd->flavours[i].flavour == BFD_MACH_O_PPC_THREAD_STATE))
1806 unsigned char buf[4];
1808 bfd_seek (abfd, cmd->flavours[i].offset + 0, SEEK_SET);
1810 if (bfd_bread (buf, 4, abfd) != 4)
1811 return -1;
1813 abfd->start_address = bfd_h_get_32 (abfd, buf);
1815 else if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_POWERPC_64)
1816 && (cmd->flavours[i].flavour == BFD_MACH_O_PPC_THREAD_STATE_64))
1818 unsigned char buf[8];
1820 bfd_seek (abfd, cmd->flavours[i].offset + 0, SEEK_SET);
1822 if (bfd_bread (buf, 8, abfd) != 8)
1823 return -1;
1825 abfd->start_address = bfd_h_get_64 (abfd, buf);
1827 else if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_X86_64)
1828 && (cmd->flavours[i].flavour == BFD_MACH_O_x86_THREAD_STATE64))
1830 unsigned char buf[8];
1832 bfd_seek (abfd, cmd->flavours[i].offset + (16 * 8), SEEK_SET);
1834 if (bfd_bread (buf, 8, abfd) != 8)
1835 return -1;
1837 abfd->start_address = bfd_h_get_64 (abfd, buf);
1841 return 0;
1845 bfd_mach_o_scan (bfd *abfd,
1846 bfd_mach_o_header *header,
1847 bfd_mach_o_data_struct *mdata)
1849 unsigned int i;
1850 enum bfd_architecture cputype;
1851 unsigned long cpusubtype;
1852 unsigned int hdrsize;
1854 hdrsize = (header->version == 2) ? 32 : 28;
1856 mdata->header = *header;
1857 mdata->symbols = NULL;
1859 abfd->flags = abfd->flags & (BFD_IN_MEMORY | BFD_IO_FUNCS);
1860 switch (header->filetype)
1862 case BFD_MACH_O_MH_OBJECT:
1863 abfd->flags |= HAS_RELOC;
1864 break;
1865 case BFD_MACH_O_MH_EXECUTE:
1866 abfd->flags |= EXEC_P;
1867 break;
1868 case BFD_MACH_O_MH_DYLIB:
1869 case BFD_MACH_O_MH_BUNDLE:
1870 abfd->flags |= DYNAMIC;
1871 break;
1874 abfd->tdata.mach_o_data = mdata;
1876 bfd_mach_o_convert_architecture (header->cputype, header->cpusubtype,
1877 &cputype, &cpusubtype);
1878 if (cputype == bfd_arch_unknown)
1880 fprintf (stderr, "bfd_mach_o_scan: unknown architecture 0x%lx/0x%lx\n",
1881 header->cputype, header->cpusubtype);
1882 return -1;
1885 bfd_set_arch_mach (abfd, cputype, cpusubtype);
1887 if (header->ncmds != 0)
1889 mdata->commands = bfd_alloc (abfd, header->ncmds * sizeof (bfd_mach_o_load_command));
1890 if (mdata->commands == NULL)
1891 return -1;
1893 for (i = 0; i < header->ncmds; i++)
1895 bfd_mach_o_load_command *cur = &mdata->commands[i];
1897 if (i == 0)
1898 cur->offset = hdrsize;
1899 else
1901 bfd_mach_o_load_command *prev = &mdata->commands[i - 1];
1902 cur->offset = prev->offset + prev->len;
1905 if (bfd_mach_o_scan_read_command (abfd, cur) < 0)
1906 return -1;
1910 if (bfd_mach_o_scan_start_address (abfd) < 0)
1911 return -1;
1913 bfd_mach_o_flatten_sections (abfd);
1914 return 0;
1917 bfd_boolean
1918 bfd_mach_o_mkobject (bfd *abfd)
1920 bfd_mach_o_data_struct *mdata = NULL;
1922 mdata = bfd_alloc (abfd, sizeof (bfd_mach_o_data_struct));
1923 if (mdata == NULL)
1924 return FALSE;
1925 abfd->tdata.mach_o_data = mdata;
1927 mdata->header.magic = 0;
1928 mdata->header.cputype = 0;
1929 mdata->header.cpusubtype = 0;
1930 mdata->header.filetype = 0;
1931 mdata->header.ncmds = 0;
1932 mdata->header.sizeofcmds = 0;
1933 mdata->header.flags = 0;
1934 mdata->header.byteorder = BFD_ENDIAN_UNKNOWN;
1935 mdata->commands = NULL;
1936 mdata->nsymbols = 0;
1937 mdata->symbols = NULL;
1938 mdata->nsects = 0;
1939 mdata->sections = NULL;
1940 mdata->ibfd = NULL;
1942 return TRUE;
1945 const bfd_target *
1946 bfd_mach_o_object_p (bfd *abfd)
1948 struct bfd_preserve preserve;
1949 bfd_mach_o_header header;
1951 preserve.marker = NULL;
1952 if (bfd_mach_o_read_header (abfd, &header) != 0)
1953 goto wrong;
1955 if (! (header.byteorder == BFD_ENDIAN_BIG
1956 || header.byteorder == BFD_ENDIAN_LITTLE))
1958 fprintf (stderr, "unknown header byte-order value 0x%lx\n",
1959 (unsigned long) header.byteorder);
1960 goto wrong;
1963 if (! ((header.byteorder == BFD_ENDIAN_BIG
1964 && abfd->xvec->byteorder == BFD_ENDIAN_BIG
1965 && abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
1966 || (header.byteorder == BFD_ENDIAN_LITTLE
1967 && abfd->xvec->byteorder == BFD_ENDIAN_LITTLE
1968 && abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)))
1969 goto wrong;
1971 preserve.marker = bfd_zalloc (abfd, sizeof (bfd_mach_o_data_struct));
1972 if (preserve.marker == NULL
1973 || !bfd_preserve_save (abfd, &preserve))
1974 goto fail;
1976 if (bfd_mach_o_scan (abfd, &header,
1977 (bfd_mach_o_data_struct *) preserve.marker) != 0)
1978 goto wrong;
1980 bfd_preserve_finish (abfd, &preserve);
1981 return abfd->xvec;
1983 wrong:
1984 bfd_set_error (bfd_error_wrong_format);
1986 fail:
1987 if (preserve.marker != NULL)
1988 bfd_preserve_restore (abfd, &preserve);
1989 return NULL;
1992 const bfd_target *
1993 bfd_mach_o_core_p (bfd *abfd)
1995 struct bfd_preserve preserve;
1996 bfd_mach_o_header header;
1998 preserve.marker = NULL;
1999 if (bfd_mach_o_read_header (abfd, &header) != 0)
2000 goto wrong;
2002 if (! (header.byteorder == BFD_ENDIAN_BIG
2003 || header.byteorder == BFD_ENDIAN_LITTLE))
2005 fprintf (stderr, "unknown header byte-order value 0x%lx\n",
2006 (unsigned long) header.byteorder);
2007 abort ();
2010 if (! ((header.byteorder == BFD_ENDIAN_BIG
2011 && abfd->xvec->byteorder == BFD_ENDIAN_BIG
2012 && abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
2013 || (header.byteorder == BFD_ENDIAN_LITTLE
2014 && abfd->xvec->byteorder == BFD_ENDIAN_LITTLE
2015 && abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)))
2016 goto wrong;
2018 if (header.filetype != BFD_MACH_O_MH_CORE)
2019 goto wrong;
2021 preserve.marker = bfd_zalloc (abfd, sizeof (bfd_mach_o_data_struct));
2022 if (preserve.marker == NULL
2023 || !bfd_preserve_save (abfd, &preserve))
2024 goto fail;
2026 if (bfd_mach_o_scan (abfd, &header,
2027 (bfd_mach_o_data_struct *) preserve.marker) != 0)
2028 goto wrong;
2030 bfd_preserve_finish (abfd, &preserve);
2031 return abfd->xvec;
2033 wrong:
2034 bfd_set_error (bfd_error_wrong_format);
2036 fail:
2037 if (preserve.marker != NULL)
2038 bfd_preserve_restore (abfd, &preserve);
2039 return NULL;
2042 typedef struct mach_o_fat_archentry
2044 unsigned long cputype;
2045 unsigned long cpusubtype;
2046 unsigned long offset;
2047 unsigned long size;
2048 unsigned long align;
2049 } mach_o_fat_archentry;
2051 typedef struct mach_o_fat_data_struct
2053 unsigned long magic;
2054 unsigned long nfat_arch;
2055 mach_o_fat_archentry *archentries;
2056 } mach_o_fat_data_struct;
2058 const bfd_target *
2059 bfd_mach_o_archive_p (bfd *abfd)
2061 mach_o_fat_data_struct *adata = NULL;
2062 unsigned char buf[20];
2063 unsigned long i;
2065 bfd_seek (abfd, 0, SEEK_SET);
2066 if (bfd_bread ((PTR) buf, 8, abfd) != 8)
2067 goto error;
2069 adata = bfd_alloc (abfd, sizeof (mach_o_fat_data_struct));
2070 if (adata == NULL)
2071 goto error;
2073 adata->magic = bfd_getb32 (buf);
2074 adata->nfat_arch = bfd_getb32 (buf + 4);
2075 if (adata->magic != 0xcafebabe)
2076 goto error;
2078 adata->archentries =
2079 bfd_alloc (abfd, adata->nfat_arch * sizeof (mach_o_fat_archentry));
2080 if (adata->archentries == NULL)
2081 goto error;
2083 for (i = 0; i < adata->nfat_arch; i++)
2085 bfd_seek (abfd, 8 + 20 * i, SEEK_SET);
2087 if (bfd_bread ((PTR) buf, 20, abfd) != 20)
2088 goto error;
2089 adata->archentries[i].cputype = bfd_getb32 (buf);
2090 adata->archentries[i].cpusubtype = bfd_getb32 (buf + 4);
2091 adata->archentries[i].offset = bfd_getb32 (buf + 8);
2092 adata->archentries[i].size = bfd_getb32 (buf + 12);
2093 adata->archentries[i].align = bfd_getb32 (buf + 16);
2096 abfd->tdata.mach_o_fat_data = adata;
2097 return abfd->xvec;
2099 error:
2100 if (adata != NULL)
2101 bfd_release (abfd, adata);
2102 bfd_set_error (bfd_error_wrong_format);
2103 return NULL;
2106 bfd *
2107 bfd_mach_o_openr_next_archived_file (bfd *archive, bfd *prev)
2109 mach_o_fat_data_struct *adata;
2110 mach_o_fat_archentry *entry = NULL;
2111 unsigned long i;
2112 bfd *nbfd;
2113 const char *arch_name;
2114 enum bfd_architecture arch_type;
2115 unsigned long arch_subtype;
2116 char *s = NULL;
2118 adata = (mach_o_fat_data_struct *) archive->tdata.mach_o_fat_data;
2119 BFD_ASSERT (adata != NULL);
2121 /* Find index of previous entry. */
2122 if (prev == NULL)
2123 i = 0; /* Start at first one. */
2124 else
2126 for (i = 0; i < adata->nfat_arch; i++)
2128 if (adata->archentries[i].offset == prev->origin)
2129 break;
2132 if (i == adata->nfat_arch)
2134 /* Not found. */
2135 bfd_set_error (bfd_error_bad_value);
2136 return NULL;
2138 i++; /* Get next entry. */
2141 if (i >= adata->nfat_arch)
2143 bfd_set_error (bfd_error_no_more_archived_files);
2144 return NULL;
2147 entry = &adata->archentries[i];
2148 nbfd = _bfd_new_bfd_contained_in (archive);
2149 if (nbfd == NULL)
2150 return NULL;
2152 nbfd->origin = entry->offset;
2154 bfd_mach_o_convert_architecture (entry->cputype, entry->cpusubtype,
2155 &arch_type, &arch_subtype);
2156 arch_name = bfd_printable_arch_mach (arch_type, arch_subtype);
2157 s = bfd_malloc (strlen (arch_name) + 1);
2158 if (s == NULL)
2159 return NULL;
2160 strcpy (s, arch_name);
2161 nbfd->filename = s;
2162 nbfd->iostream = NULL;
2164 return nbfd;
2168 bfd_mach_o_lookup_section (bfd *abfd,
2169 asection *section,
2170 bfd_mach_o_load_command **mcommand,
2171 bfd_mach_o_section **msection)
2173 struct mach_o_data_struct *md = abfd->tdata.mach_o_data;
2174 unsigned int i, j, num;
2176 bfd_mach_o_load_command *ncmd = NULL;
2177 bfd_mach_o_section *nsect = NULL;
2179 BFD_ASSERT (mcommand != NULL);
2180 BFD_ASSERT (msection != NULL);
2182 num = 0;
2183 for (i = 0; i < md->header.ncmds; i++)
2185 struct bfd_mach_o_load_command *cmd = &md->commands[i];
2186 struct bfd_mach_o_segment_command *seg = NULL;
2188 if (cmd->type != BFD_MACH_O_LC_SEGMENT
2189 || cmd->type != BFD_MACH_O_LC_SEGMENT_64)
2190 continue;
2191 seg = &cmd->command.segment;
2193 if (seg->segment == section)
2195 if (num == 0)
2196 ncmd = cmd;
2197 num++;
2200 for (j = 0; j < seg->nsects; j++)
2202 struct bfd_mach_o_section *sect = &seg->sections[j];
2204 if (sect->bfdsection == section)
2206 if (num == 0)
2207 nsect = sect;
2208 num++;
2213 *mcommand = ncmd;
2214 *msection = nsect;
2215 return num;
2219 bfd_mach_o_lookup_command (bfd *abfd,
2220 bfd_mach_o_load_command_type type,
2221 bfd_mach_o_load_command **mcommand)
2223 struct mach_o_data_struct *md = NULL;
2224 bfd_mach_o_load_command *ncmd = NULL;
2225 unsigned int i, num;
2227 md = abfd->tdata.mach_o_data;
2229 BFD_ASSERT (md != NULL);
2230 BFD_ASSERT (mcommand != NULL);
2232 num = 0;
2233 for (i = 0; i < md->header.ncmds; i++)
2235 struct bfd_mach_o_load_command *cmd = &md->commands[i];
2237 if (cmd->type != type)
2238 continue;
2240 if (num == 0)
2241 ncmd = cmd;
2242 num++;
2245 *mcommand = ncmd;
2246 return num;
2249 unsigned long
2250 bfd_mach_o_stack_addr (enum bfd_mach_o_cpu_type type)
2252 switch (type)
2254 case BFD_MACH_O_CPU_TYPE_MC680x0:
2255 return 0x04000000;
2256 case BFD_MACH_O_CPU_TYPE_MC88000:
2257 return 0xffffe000;
2258 case BFD_MACH_O_CPU_TYPE_POWERPC:
2259 return 0xc0000000;
2260 case BFD_MACH_O_CPU_TYPE_I386:
2261 return 0xc0000000;
2262 case BFD_MACH_O_CPU_TYPE_SPARC:
2263 return 0xf0000000;
2264 case BFD_MACH_O_CPU_TYPE_I860:
2265 return 0;
2266 case BFD_MACH_O_CPU_TYPE_HPPA:
2267 return 0xc0000000 - 0x04000000;
2268 default:
2269 return 0;
2273 static bfd_boolean
2274 bfd_mach_o_bfd_print_private_bfd_data (bfd *abfd, PTR ptr)
2276 bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
2277 FILE *file = (FILE *) ptr;
2278 unsigned int i, j;
2279 unsigned int sec_nbr = 0;
2281 fprintf (file, _("Segments and Sections:\n"));
2282 fprintf (file, _(" #: Segment name Section name Address\n"));
2284 for (i = 0; i < mdata->header.ncmds; i++)
2286 bfd_mach_o_segment_command *seg;
2288 if (mdata->commands[i].type != BFD_MACH_O_LC_SEGMENT
2289 && mdata->commands[i].type != BFD_MACH_O_LC_SEGMENT_64)
2290 continue;
2292 seg = &mdata->commands[i].command.segment;
2294 fprintf (file, "[Segment %-16s ", seg->segname);
2295 fprintf_vma (file, seg->vmaddr);
2296 fprintf (file, "-");
2297 fprintf_vma (file, seg->vmaddr + seg->vmsize - 1);
2298 fputc (' ', file);
2299 fputc (seg->initprot & BFD_MACH_O_PROT_READ ? 'r' : '-', file);
2300 fputc (seg->initprot & BFD_MACH_O_PROT_WRITE ? 'w' : '-', file);
2301 fputc (seg->initprot & BFD_MACH_O_PROT_EXECUTE ? 'x' : '-', file);
2302 fprintf (file, "]\n");
2303 for (j = 0; j < seg->nsects; j++)
2305 bfd_mach_o_section *sec = &seg->sections[j];
2306 fprintf (file, "%02u: %-16s %-16s ", ++sec_nbr,
2307 sec->segname, sec->sectname);
2308 fprintf_vma (file, sec->addr);
2309 fprintf (file, " ");
2310 fprintf_vma (file, sec->size);
2311 fprintf (file, " %08lx\n", sec->flags);
2315 for (i = 0; i < mdata->header.ncmds; i++)
2317 bfd_mach_o_load_command *cmd = &mdata->commands[i];
2319 switch (cmd->type)
2321 case BFD_MACH_O_LC_SEGMENT:
2322 case BFD_MACH_O_LC_SEGMENT_64:
2323 break;
2324 case BFD_MACH_O_LC_UUID:
2326 bfd_mach_o_uuid_command *uuid = &cmd->command.uuid;
2327 unsigned int i;
2329 fprintf (file, "\n"
2330 "UUID:");
2331 for (i = 0; i < sizeof (uuid->uuid); i++)
2332 fprintf (file, " %02x", uuid->uuid[i]);
2333 fputc ('\n', file);
2335 break;
2336 case BFD_MACH_O_LC_LOAD_DYLIB:
2338 bfd_mach_o_dylib_command *dylib = &cmd->command.dylib;
2339 bfd_byte *data = NULL;
2341 if (! bfd_malloc_and_get_section (abfd, dylib->section, &data))
2343 if (data != NULL)
2344 free (data);
2345 break;
2347 fprintf (file, "\n"
2348 "LOAD_DYLIB: %s\n",
2349 data + dylib->name_offset - cmd->offset - 8);
2350 fprintf (file, " time stamp: 0x%08lx\n",
2351 dylib->timestamp);
2352 fprintf (file, " current version: 0x%08lx\n",
2353 dylib->current_version);
2354 fprintf (file, " comptibility version: 0x%08lx\n",
2355 dylib->compatibility_version);
2356 free (data);
2357 break;
2359 case BFD_MACH_O_LC_LOAD_DYLINKER:
2361 bfd_mach_o_dylinker_command *linker = &cmd->command.dylinker;
2362 bfd_byte *data = NULL;
2364 if (! bfd_malloc_and_get_section (abfd, linker->section, &data))
2366 if (data != NULL)
2367 free (data);
2368 break;
2370 fprintf (file, "\n"
2371 "LOAD_DYLINKER: %s\n",
2372 data + linker->name_offset - cmd->offset - 8);
2373 free (data);
2374 break;
2376 case BFD_MACH_O_LC_SYMTAB:
2378 bfd_mach_o_symtab_command *symtab = &cmd->command.symtab;
2379 fprintf (file, "\n"
2380 "LC_SYMTAB: nsyms: %lu, strsize: %lu\n",
2381 symtab->nsyms, symtab->strsize);
2382 break;
2384 case BFD_MACH_O_LC_DYSYMTAB:
2386 bfd_mach_o_dysymtab_command *dysymtab = &cmd->command.dysymtab;
2387 fprintf (file, "\n"
2388 "LC_DYSYMTAB:\n"
2389 " local symbols: index: %lu number: %lu\n",
2390 dysymtab->ilocalsym, dysymtab->nlocalsym);
2391 fprintf (file,
2392 " external symbols: index: %lu number: %lu\n",
2393 dysymtab->iextdefsym, dysymtab->nextdefsym);
2394 fprintf (file,
2395 " undefined symbols: index: %lu number: %lu\n",
2396 dysymtab->iundefsym, dysymtab->nundefsym);
2397 fprintf (file,
2398 " ntoc: offset: %lu number: %lu\n",
2399 dysymtab->tocoff, dysymtab->ntoc);
2400 fprintf (file,
2401 " module table: offset: %lu number: %lu\n",
2402 dysymtab->modtaboff, dysymtab->nmodtab);
2403 break;
2405 default:
2406 fprintf (file, "LC_%d\n", cmd->type);
2407 break;
2411 return TRUE;
2415 bfd_mach_o_core_fetch_environment (bfd *abfd,
2416 unsigned char **rbuf,
2417 unsigned int *rlen)
2419 bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
2420 unsigned long stackaddr = bfd_mach_o_stack_addr (mdata->header.cputype);
2421 unsigned int i = 0;
2423 for (i = 0; i < mdata->header.ncmds; i++)
2425 bfd_mach_o_load_command *cur = &mdata->commands[i];
2426 bfd_mach_o_segment_command *seg = NULL;
2428 if (cur->type != BFD_MACH_O_LC_SEGMENT)
2429 continue;
2431 seg = &cur->command.segment;
2433 if ((seg->vmaddr + seg->vmsize) == stackaddr)
2435 unsigned long start = seg->fileoff;
2436 unsigned long end = seg->fileoff + seg->filesize;
2437 unsigned char *buf = bfd_malloc (1024);
2438 unsigned long size = 1024;
2440 for (;;)
2442 bfd_size_type nread = 0;
2443 unsigned long offset;
2444 int found_nonnull = 0;
2446 if (size > (end - start))
2447 size = (end - start);
2449 buf = bfd_realloc_or_free (buf, size);
2450 if (buf == NULL)
2451 return -1;
2453 bfd_seek (abfd, end - size, SEEK_SET);
2454 nread = bfd_bread (buf, size, abfd);
2456 if (nread != size)
2458 free (buf);
2459 return -1;
2462 for (offset = 4; offset <= size; offset += 4)
2464 unsigned long val;
2466 val = *((unsigned long *) (buf + size - offset));
2467 if (! found_nonnull)
2469 if (val != 0)
2470 found_nonnull = 1;
2472 else if (val == 0x0)
2474 unsigned long bottom;
2475 unsigned long top;
2477 bottom = seg->fileoff + seg->filesize - offset;
2478 top = seg->fileoff + seg->filesize - 4;
2479 *rbuf = bfd_malloc (top - bottom);
2480 *rlen = top - bottom;
2482 memcpy (*rbuf, buf + size - *rlen, *rlen);
2483 free (buf);
2484 return 0;
2488 if (size == (end - start))
2489 break;
2491 size *= 2;
2494 free (buf);
2498 return -1;
2501 char *
2502 bfd_mach_o_core_file_failing_command (bfd *abfd)
2504 unsigned char *buf = NULL;
2505 unsigned int len = 0;
2506 int ret = -1;
2508 ret = bfd_mach_o_core_fetch_environment (abfd, &buf, &len);
2509 if (ret < 0)
2510 return NULL;
2512 return (char *) buf;
2516 bfd_mach_o_core_file_failing_signal (bfd *abfd ATTRIBUTE_UNUSED)
2518 return 0;
2521 #define TARGET_NAME mach_o_be_vec
2522 #define TARGET_STRING "mach-o-be"
2523 #define TARGET_BIG_ENDIAN 1
2524 #define TARGET_ARCHIVE 0
2526 #include "mach-o-target.c"
2528 #undef TARGET_NAME
2529 #undef TARGET_STRING
2530 #undef TARGET_BIG_ENDIAN
2531 #undef TARGET_ARCHIVE
2533 #define TARGET_NAME mach_o_le_vec
2534 #define TARGET_STRING "mach-o-le"
2535 #define TARGET_BIG_ENDIAN 0
2536 #define TARGET_ARCHIVE 0
2538 #include "mach-o-target.c"
2540 #undef TARGET_NAME
2541 #undef TARGET_STRING
2542 #undef TARGET_BIG_ENDIAN
2543 #undef TARGET_ARCHIVE
2545 #define TARGET_NAME mach_o_fat_vec
2546 #define TARGET_STRING "mach-o-fat"
2547 #define TARGET_BIG_ENDIAN 1
2548 #define TARGET_ARCHIVE 1
2550 #include "mach-o-target.c"
2552 #undef TARGET_NAME
2553 #undef TARGET_STRING
2554 #undef TARGET_BIG_ENDIAN
2555 #undef TARGET_ARCHIVE