1 /* Mach-O support for BFD.
2 Copyright 1999, 2000, 2001, 2002, 2003, 2004
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 2 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 #include "libiberty.h"
29 #define BFD_IO_FUNCS 0
32 #define bfd_mach_o_mkarchive _bfd_noarchive_mkarchive
33 #define bfd_mach_o_read_ar_hdr _bfd_noarchive_read_ar_hdr
34 #define bfd_mach_o_slurp_armap _bfd_noarchive_slurp_armap
35 #define bfd_mach_o_slurp_extended_name_table _bfd_noarchive_slurp_extended_name_table
36 #define bfd_mach_o_construct_extended_name_table _bfd_noarchive_construct_extended_name_table
37 #define bfd_mach_o_truncate_arname _bfd_noarchive_truncate_arname
38 #define bfd_mach_o_write_armap _bfd_noarchive_write_armap
39 #define bfd_mach_o_get_elt_at_index _bfd_noarchive_get_elt_at_index
40 #define bfd_mach_o_generic_stat_arch_elt _bfd_noarchive_generic_stat_arch_elt
41 #define bfd_mach_o_update_armap_timestamp _bfd_noarchive_update_armap_timestamp
42 #define bfd_mach_o_close_and_cleanup _bfd_generic_close_and_cleanup
43 #define bfd_mach_o_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
44 #define bfd_mach_o_new_section_hook _bfd_generic_new_section_hook
45 #define bfd_mach_o_get_section_contents_in_window _bfd_generic_get_section_contents_in_window
46 #define bfd_mach_o_bfd_is_local_label_name _bfd_nosymbols_bfd_is_local_label_name
47 #define bfd_mach_o_get_lineno _bfd_nosymbols_get_lineno
48 #define bfd_mach_o_find_nearest_line _bfd_nosymbols_find_nearest_line
49 #define bfd_mach_o_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
50 #define bfd_mach_o_read_minisymbols _bfd_generic_read_minisymbols
51 #define bfd_mach_o_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
52 #define bfd_mach_o_get_reloc_upper_bound _bfd_norelocs_get_reloc_upper_bound
53 #define bfd_mach_o_canonicalize_reloc _bfd_norelocs_canonicalize_reloc
54 #define bfd_mach_o_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
55 #define bfd_mach_o_bfd_get_relocated_section_contents bfd_generic_get_relocated_section_contents
56 #define bfd_mach_o_bfd_relax_section bfd_generic_relax_section
57 #define bfd_mach_o_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
58 #define bfd_mach_o_bfd_link_hash_table_free _bfd_generic_link_hash_table_free
59 #define bfd_mach_o_bfd_link_add_symbols _bfd_generic_link_add_symbols
60 #define bfd_mach_o_bfd_link_just_syms _bfd_generic_link_just_syms
61 #define bfd_mach_o_bfd_final_link _bfd_generic_final_link
62 #define bfd_mach_o_bfd_link_split_section _bfd_generic_link_split_section
63 #define bfd_mach_o_set_arch_mach bfd_default_set_arch_mach
64 #define bfd_mach_o_bfd_merge_private_bfd_data _bfd_generic_bfd_merge_private_bfd_data
65 #define bfd_mach_o_bfd_set_private_flags _bfd_generic_bfd_set_private_flags
66 #define bfd_mach_o_bfd_print_private_bfd_data _bfd_generic_bfd_print_private_bfd_data
67 #define bfd_mach_o_get_section_contents _bfd_generic_get_section_contents
68 #define bfd_mach_o_set_section_contents _bfd_generic_set_section_contents
69 #define bfd_mach_o_bfd_gc_sections bfd_generic_gc_sections
70 #define bfd_mach_o_bfd_merge_sections bfd_generic_merge_sections
71 #define bfd_mach_o_bfd_is_group_section bfd_generic_is_group_section
72 #define bfd_mach_o_bfd_discard_group bfd_generic_discard_group
74 static bfd_boolean bfd_mach_o_bfd_copy_private_symbol_data
75 PARAMS ((bfd
*, asymbol
*, bfd
*, asymbol
*));
76 static bfd_boolean bfd_mach_o_bfd_copy_private_section_data
77 PARAMS ((bfd
*, asection
*, bfd
*, asection
*));
78 static bfd_boolean bfd_mach_o_bfd_copy_private_bfd_data
79 PARAMS ((bfd
*, bfd
*));
80 static long bfd_mach_o_count_symbols
82 static long bfd_mach_o_get_symtab_upper_bound
84 static long bfd_mach_o_canonicalize_symtab
85 PARAMS ((bfd
*, asymbol
**));
86 static void bfd_mach_o_get_symbol_info
87 PARAMS ((bfd
*, asymbol
*, symbol_info
*));
88 static void bfd_mach_o_print_symbol
89 PARAMS ((bfd
*, PTR
, asymbol
*, bfd_print_symbol_type
));
90 static void bfd_mach_o_convert_architecture
91 PARAMS ((bfd_mach_o_cpu_type
, bfd_mach_o_cpu_subtype
,
92 enum bfd_architecture
*, unsigned long *));
93 static bfd_boolean bfd_mach_o_write_contents
95 static int bfd_mach_o_sizeof_headers
96 PARAMS ((bfd
*, bfd_boolean
));
97 static asymbol
* bfd_mach_o_make_empty_symbol
99 static int bfd_mach_o_write_header
100 PARAMS ((bfd
*, bfd_mach_o_header
*));
101 static int bfd_mach_o_read_header
102 PARAMS ((bfd
*, bfd_mach_o_header
*));
103 static asection
* bfd_mach_o_make_bfd_section
104 PARAMS ((bfd
*, bfd_mach_o_section
*));
105 static int bfd_mach_o_scan_read_section
106 PARAMS ((bfd
*, bfd_mach_o_section
*, bfd_vma
));
107 static int bfd_mach_o_scan_write_section
108 PARAMS ((bfd
*, bfd_mach_o_section
*, bfd_vma
));
109 static int bfd_mach_o_scan_write_symtab_symbols
110 PARAMS ((bfd
*, bfd_mach_o_load_command
*));
111 static int bfd_mach_o_scan_write_thread
112 PARAMS ((bfd
*, bfd_mach_o_load_command
*));
113 static int bfd_mach_o_scan_read_dylinker
114 PARAMS ((bfd
*, bfd_mach_o_load_command
*));
115 static int bfd_mach_o_scan_read_dylib
116 PARAMS ((bfd
*, bfd_mach_o_load_command
*));
117 static int bfd_mach_o_scan_read_prebound_dylib
118 PARAMS ((bfd
*, bfd_mach_o_load_command
*));
119 static int bfd_mach_o_scan_read_thread
120 PARAMS ((bfd
*, bfd_mach_o_load_command
*));
121 static int bfd_mach_o_scan_write_symtab
122 PARAMS ((bfd
*, bfd_mach_o_load_command
*));
123 static int bfd_mach_o_scan_read_dysymtab
124 PARAMS ((bfd
*, bfd_mach_o_load_command
*));
125 static int bfd_mach_o_scan_read_symtab
126 PARAMS ((bfd
*, bfd_mach_o_load_command
*));
127 static int bfd_mach_o_scan_read_segment
128 PARAMS ((bfd
*, bfd_mach_o_load_command
*));
129 static int bfd_mach_o_scan_write_segment
130 PARAMS ((bfd
*, bfd_mach_o_load_command
*));
131 static int bfd_mach_o_scan_read_command
132 PARAMS ((bfd
*, bfd_mach_o_load_command
*));
133 static void bfd_mach_o_flatten_sections
135 static const char * bfd_mach_o_i386_flavour_string
136 PARAMS ((unsigned int));
137 static const char * bfd_mach_o_ppc_flavour_string
138 PARAMS ((unsigned int));
140 /* The flags field of a section structure is separated into two parts a section
141 type and section attributes. The section types are mutually exclusive (it
142 can only have one type) but the section attributes are not (it may have more
143 than one attribute). */
145 #define SECTION_TYPE 0x000000ff /* 256 section types. */
146 #define SECTION_ATTRIBUTES 0xffffff00 /* 24 section attributes. */
148 /* Constants for the section attributes part of the flags field of a section
151 #define SECTION_ATTRIBUTES_USR 0xff000000 /* User-settable attributes. */
152 #define S_ATTR_PURE_INSTRUCTIONS 0x80000000 /* Section contains only true machine instructions. */
153 #define SECTION_ATTRIBUTES_SYS 0x00ffff00 /* System setable attributes. */
154 #define S_ATTR_SOME_INSTRUCTIONS 0x00000400 /* Section contains some machine instructions. */
155 #define S_ATTR_EXT_RELOC 0x00000200 /* Section has external relocation entries. */
156 #define S_ATTR_LOC_RELOC 0x00000100 /* Section has local relocation entries. */
167 bfd_mach_o_valid (abfd
)
170 if (abfd
== NULL
|| abfd
->xvec
== NULL
)
173 if (! ((abfd
->xvec
== &mach_o_be_vec
)
174 || (abfd
->xvec
== &mach_o_le_vec
)
175 || (abfd
->xvec
== &mach_o_fat_vec
)))
178 if (abfd
->tdata
.mach_o_data
== NULL
)
183 /* Copy any private info we understand from the input symbol
184 to the output symbol. */
187 bfd_mach_o_bfd_copy_private_symbol_data (ibfd
, isymbol
, obfd
, osymbol
)
188 bfd
*ibfd ATTRIBUTE_UNUSED
;
189 asymbol
*isymbol ATTRIBUTE_UNUSED
;
190 bfd
*obfd ATTRIBUTE_UNUSED
;
191 asymbol
*osymbol ATTRIBUTE_UNUSED
;
196 /* Copy any private info we understand from the input section
197 to the output section. */
200 bfd_mach_o_bfd_copy_private_section_data (ibfd
, isection
, obfd
, osection
)
201 bfd
*ibfd ATTRIBUTE_UNUSED
;
202 asection
*isection ATTRIBUTE_UNUSED
;
203 bfd
*obfd ATTRIBUTE_UNUSED
;
204 asection
*osection ATTRIBUTE_UNUSED
;
209 /* Copy any private info we understand from the input bfd
210 to the output bfd. */
213 bfd_mach_o_bfd_copy_private_bfd_data (ibfd
, obfd
)
217 BFD_ASSERT (bfd_mach_o_valid (ibfd
));
218 BFD_ASSERT (bfd_mach_o_valid (obfd
));
220 obfd
->tdata
.mach_o_data
= ibfd
->tdata
.mach_o_data
;
221 obfd
->tdata
.mach_o_data
->ibfd
= ibfd
;
226 bfd_mach_o_count_symbols (abfd
)
229 bfd_mach_o_data_struct
*mdata
= NULL
;
233 BFD_ASSERT (bfd_mach_o_valid (abfd
));
234 mdata
= abfd
->tdata
.mach_o_data
;
236 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
237 if (mdata
->commands
[i
].type
== BFD_MACH_O_LC_SYMTAB
)
239 bfd_mach_o_symtab_command
*sym
= &mdata
->commands
[i
].command
.symtab
;
247 bfd_mach_o_get_symtab_upper_bound (abfd
)
250 long nsyms
= bfd_mach_o_count_symbols (abfd
);
255 return ((nsyms
+ 1) * sizeof (asymbol
*));
259 bfd_mach_o_canonicalize_symtab (abfd
, alocation
)
263 bfd_mach_o_data_struct
*mdata
= abfd
->tdata
.mach_o_data
;
264 long nsyms
= bfd_mach_o_count_symbols (abfd
);
265 asymbol
**csym
= alocation
;
271 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
273 if (mdata
->commands
[i
].type
== BFD_MACH_O_LC_SYMTAB
)
275 bfd_mach_o_symtab_command
*sym
= &mdata
->commands
[i
].command
.symtab
;
277 if (bfd_mach_o_scan_read_symtab_symbols (abfd
, &mdata
->commands
[i
].command
.symtab
) != 0)
279 fprintf (stderr
, "bfd_mach_o_canonicalize_symtab: unable to load symbols for section %lu\n", i
);
283 BFD_ASSERT (sym
->symbols
!= NULL
);
285 for (j
= 0; j
< sym
->nsyms
; j
++)
287 BFD_ASSERT (csym
< (alocation
+ nsyms
));
288 *csym
++ = &sym
->symbols
[j
];
299 bfd_mach_o_get_symbol_info (abfd
, symbol
, ret
)
300 bfd
*abfd ATTRIBUTE_UNUSED
;
304 bfd_symbol_info (symbol
, ret
);
308 bfd_mach_o_print_symbol (abfd
, afile
, symbol
, how
)
312 bfd_print_symbol_type how
;
314 FILE *file
= (FILE *) afile
;
318 case bfd_print_symbol_name
:
319 fprintf (file
, "%s", symbol
->name
);
322 bfd_print_symbol_vandf (abfd
, (PTR
) file
, symbol
);
323 fprintf (file
, " %-5s %s", symbol
->section
->name
, symbol
->name
);
328 bfd_mach_o_convert_architecture (mtype
, msubtype
, type
, subtype
)
329 bfd_mach_o_cpu_type mtype
;
330 bfd_mach_o_cpu_subtype msubtype ATTRIBUTE_UNUSED
;
331 enum bfd_architecture
*type
;
332 unsigned long *subtype
;
334 *subtype
= bfd_arch_unknown
;
338 case BFD_MACH_O_CPU_TYPE_VAX
: *type
= bfd_arch_vax
; break;
339 case BFD_MACH_O_CPU_TYPE_MC680x0
: *type
= bfd_arch_m68k
; break;
340 case BFD_MACH_O_CPU_TYPE_I386
: *type
= bfd_arch_i386
; break;
341 case BFD_MACH_O_CPU_TYPE_MIPS
: *type
= bfd_arch_mips
; break;
342 case BFD_MACH_O_CPU_TYPE_MC98000
: *type
= bfd_arch_m98k
; break;
343 case BFD_MACH_O_CPU_TYPE_HPPA
: *type
= bfd_arch_hppa
; break;
344 case BFD_MACH_O_CPU_TYPE_ARM
: *type
= bfd_arch_arm
; break;
345 case BFD_MACH_O_CPU_TYPE_MC88000
: *type
= bfd_arch_m88k
; break;
346 case BFD_MACH_O_CPU_TYPE_SPARC
: *type
= bfd_arch_sparc
; break;
347 case BFD_MACH_O_CPU_TYPE_I860
: *type
= bfd_arch_i860
; break;
348 case BFD_MACH_O_CPU_TYPE_ALPHA
: *type
= bfd_arch_alpha
; break;
349 case BFD_MACH_O_CPU_TYPE_POWERPC
: *type
= bfd_arch_powerpc
; break;
350 default: *type
= bfd_arch_unknown
; break;
355 case bfd_arch_i386
: *subtype
= bfd_mach_i386_i386
; break;
356 case bfd_arch_sparc
: *subtype
= bfd_mach_sparc
; break;
358 *subtype
= bfd_arch_unknown
;
363 bfd_mach_o_write_contents (abfd
)
369 bfd_mach_o_data_struct
*mdata
= abfd
->tdata
.mach_o_data
;
371 /* Write data sections first in case they overlap header data to be
374 for (s
= abfd
->sections
; s
!= (asection
*) NULL
; s
= s
->next
)
378 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
380 bfd_mach_o_load_command
*cur
= &mdata
->commands
[i
];
381 if (cur
->type
!= BFD_MACH_O_LC_SEGMENT
)
385 bfd_mach_o_segment_command
*seg
= &cur
->command
.segment
;
387 bfd_vma nbytes
= seg
->filesize
;
388 bfd_vma curoff
= seg
->fileoff
;
392 bfd_vma thisread
= nbytes
;
397 bfd_seek (abfd
, curoff
, SEEK_SET
);
398 if (bfd_bread ((PTR
) buf
, thisread
, abfd
) != thisread
)
401 bfd_seek (abfd
, curoff
, SEEK_SET
);
402 if (bfd_bwrite ((PTR
) buf
, thisread
, abfd
) != thisread
)
412 /* Now write header information. */
413 if (bfd_mach_o_write_header (abfd
, &mdata
->header
) != 0)
416 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
418 unsigned char buf
[8];
419 bfd_mach_o_load_command
*cur
= &mdata
->commands
[i
];
420 unsigned long typeflag
;
422 typeflag
= cur
->type_required
? cur
->type
& BFD_MACH_O_LC_REQ_DYLD
: cur
->type
;
424 bfd_h_put_32 (abfd
, typeflag
, buf
);
425 bfd_h_put_32 (abfd
, cur
->len
, buf
+ 4);
427 bfd_seek (abfd
, cur
->offset
, SEEK_SET
);
428 if (bfd_bwrite ((PTR
) buf
, 8, abfd
) != 8)
433 case BFD_MACH_O_LC_SEGMENT
:
434 if (bfd_mach_o_scan_write_segment (abfd
, cur
) != 0)
437 case BFD_MACH_O_LC_SYMTAB
:
438 if (bfd_mach_o_scan_write_symtab (abfd
, cur
) != 0)
441 case BFD_MACH_O_LC_SYMSEG
:
443 case BFD_MACH_O_LC_THREAD
:
444 case BFD_MACH_O_LC_UNIXTHREAD
:
445 if (bfd_mach_o_scan_write_thread (abfd
, cur
) != 0)
448 case BFD_MACH_O_LC_LOADFVMLIB
:
449 case BFD_MACH_O_LC_IDFVMLIB
:
450 case BFD_MACH_O_LC_IDENT
:
451 case BFD_MACH_O_LC_FVMFILE
:
452 case BFD_MACH_O_LC_PREPAGE
:
453 case BFD_MACH_O_LC_DYSYMTAB
:
454 case BFD_MACH_O_LC_LOAD_DYLIB
:
455 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB
:
456 case BFD_MACH_O_LC_ID_DYLIB
:
457 case BFD_MACH_O_LC_LOAD_DYLINKER
:
458 case BFD_MACH_O_LC_ID_DYLINKER
:
459 case BFD_MACH_O_LC_PREBOUND_DYLIB
:
460 case BFD_MACH_O_LC_ROUTINES
:
461 case BFD_MACH_O_LC_SUB_FRAMEWORK
:
465 "unable to write unknown load command 0x%lx\n",
475 bfd_mach_o_sizeof_headers (a
, b
)
476 bfd
*a ATTRIBUTE_UNUSED
;
477 bfd_boolean b ATTRIBUTE_UNUSED
;
482 /* Make an empty symbol. This is required only because
483 bfd_make_section_anyway wants to create a symbol for the section. */
486 bfd_mach_o_make_empty_symbol (abfd
)
491 new = (asymbol
*) bfd_zalloc (abfd
, sizeof (asymbol
));
499 bfd_mach_o_write_header (abfd
, header
)
501 bfd_mach_o_header
*header
;
503 unsigned char buf
[28];
505 bfd_h_put_32 (abfd
, header
->magic
, buf
+ 0);
506 bfd_h_put_32 (abfd
, header
->cputype
, buf
+ 4);
507 bfd_h_put_32 (abfd
, header
->cpusubtype
, buf
+ 8);
508 bfd_h_put_32 (abfd
, header
->filetype
, buf
+ 12);
509 bfd_h_put_32 (abfd
, header
->ncmds
, buf
+ 16);
510 bfd_h_put_32 (abfd
, header
->sizeofcmds
, buf
+ 20);
511 bfd_h_put_32 (abfd
, header
->flags
, buf
+ 24);
513 bfd_seek (abfd
, 0, SEEK_SET
);
514 if (bfd_bwrite ((PTR
) buf
, 28, abfd
) != 28)
521 bfd_mach_o_read_header (abfd
, header
)
523 bfd_mach_o_header
*header
;
525 unsigned char buf
[28];
526 bfd_vma (*get32
) (const void *) = NULL
;
528 bfd_seek (abfd
, 0, SEEK_SET
);
530 if (bfd_bread ((PTR
) buf
, 28, abfd
) != 28)
533 if (bfd_getb32 (buf
) == 0xfeedface)
535 header
->byteorder
= BFD_ENDIAN_BIG
;
536 header
->magic
= 0xfeedface;
539 else if (bfd_getl32 (buf
) == 0xfeedface)
541 header
->byteorder
= BFD_ENDIAN_LITTLE
;
542 header
->magic
= 0xfeedface;
547 header
->byteorder
= BFD_ENDIAN_UNKNOWN
;
551 header
->cputype
= (*get32
) (buf
+ 4);
552 header
->cpusubtype
= (*get32
) (buf
+ 8);
553 header
->filetype
= (*get32
) (buf
+ 12);
554 header
->ncmds
= (*get32
) (buf
+ 16);
555 header
->sizeofcmds
= (*get32
) (buf
+ 20);
556 header
->flags
= (*get32
) (buf
+ 24);
562 bfd_mach_o_make_bfd_section (abfd
, section
)
564 bfd_mach_o_section
*section
;
568 const char *prefix
= "LC_SEGMENT";
569 unsigned int snamelen
;
571 snamelen
= strlen (prefix
) + 1
572 + strlen (section
->segname
) + 1
573 + strlen (section
->sectname
) + 1;
575 sname
= (char *) bfd_alloc (abfd
, snamelen
);
578 sprintf (sname
, "%s.%s.%s", prefix
, section
->segname
, section
->sectname
);
580 bfdsec
= bfd_make_section_anyway (abfd
, sname
);
584 bfdsec
->vma
= section
->addr
;
585 bfdsec
->lma
= section
->addr
;
586 bfdsec
->_raw_size
= section
->size
;
587 bfdsec
->filepos
= section
->offset
;
588 bfdsec
->alignment_power
= section
->align
;
590 if (section
->flags
& BFD_MACH_O_S_ZEROFILL
)
591 bfdsec
->flags
= SEC_ALLOC
;
593 bfdsec
->flags
= SEC_HAS_CONTENTS
| SEC_LOAD
| SEC_ALLOC
| SEC_CODE
;
599 bfd_mach_o_scan_read_section (abfd
, section
, offset
)
601 bfd_mach_o_section
*section
;
604 unsigned char buf
[68];
606 bfd_seek (abfd
, offset
, SEEK_SET
);
607 if (bfd_bread ((PTR
) buf
, 68, abfd
) != 68)
610 memcpy (section
->sectname
, buf
, 16);
611 section
->sectname
[16] = '\0';
612 memcpy (section
->segname
, buf
+ 16, 16);
613 section
->segname
[16] = '\0';
614 section
->addr
= bfd_h_get_32 (abfd
, buf
+ 32);
615 section
->size
= bfd_h_get_32 (abfd
, buf
+ 36);
616 section
->offset
= bfd_h_get_32 (abfd
, buf
+ 40);
617 section
->align
= bfd_h_get_32 (abfd
, buf
+ 44);
618 section
->reloff
= bfd_h_get_32 (abfd
, buf
+ 48);
619 section
->nreloc
= bfd_h_get_32 (abfd
, buf
+ 52);
620 section
->flags
= bfd_h_get_32 (abfd
, buf
+ 56);
621 section
->reserved1
= bfd_h_get_32 (abfd
, buf
+ 60);
622 section
->reserved2
= bfd_h_get_32 (abfd
, buf
+ 64);
623 section
->bfdsection
= bfd_mach_o_make_bfd_section (abfd
, section
);
625 if (section
->bfdsection
== NULL
)
632 bfd_mach_o_scan_write_section (abfd
, section
, offset
)
634 bfd_mach_o_section
*section
;
637 unsigned char buf
[68];
639 memcpy (buf
, section
->sectname
, 16);
640 memcpy (buf
+ 16, section
->segname
, 16);
641 bfd_h_put_32 (abfd
, section
->addr
, buf
+ 32);
642 bfd_h_put_32 (abfd
, section
->size
, buf
+ 36);
643 bfd_h_put_32 (abfd
, section
->offset
, buf
+ 40);
644 bfd_h_put_32 (abfd
, section
->align
, buf
+ 44);
645 bfd_h_put_32 (abfd
, section
->reloff
, buf
+ 48);
646 bfd_h_put_32 (abfd
, section
->nreloc
, buf
+ 52);
647 bfd_h_put_32 (abfd
, section
->flags
, buf
+ 56);
648 /* bfd_h_put_32 (abfd, section->reserved1, buf + 60); */
649 /* bfd_h_put_32 (abfd, section->reserved2, buf + 64); */
651 bfd_seek (abfd
, offset
, SEEK_SET
);
652 if (bfd_bwrite ((PTR
) buf
, 68, abfd
) != 68)
659 bfd_mach_o_scan_write_symtab_symbols (abfd
, command
)
661 bfd_mach_o_load_command
*command
;
663 bfd_mach_o_symtab_command
*sym
= &command
->command
.symtab
;
667 for (i
= 0; i
< sym
->nsyms
; i
++)
669 unsigned char buf
[12];
670 bfd_vma symoff
= sym
->symoff
+ (i
* 12);
671 unsigned char ntype
= 0;
672 unsigned char nsect
= 0;
675 s
= &sym
->symbols
[i
];
677 /* Don't set this from the symbol information; use stored values. */
679 if (s
->flags
& BSF_GLOBAL
)
681 if (s
->flags
& BSF_DEBUGGING
)
684 if (s
->section
== bfd_und_section_ptr
)
686 else if (s
->section
== bfd_abs_section_ptr
)
692 /* Instead just set from the stored values. */
693 ntype
= (s
->udata
.i
>> 24) & 0xff;
694 nsect
= (s
->udata
.i
>> 16) & 0xff;
695 ndesc
= s
->udata
.i
& 0xffff;
697 bfd_h_put_32 (abfd
, s
->name
- sym
->strtab
, buf
);
698 bfd_h_put_8 (abfd
, ntype
, buf
+ 4);
699 bfd_h_put_8 (abfd
, nsect
, buf
+ 5);
700 bfd_h_put_16 (abfd
, ndesc
, buf
+ 6);
701 bfd_h_put_32 (abfd
, s
->section
->vma
+ s
->value
, buf
+ 8);
703 bfd_seek (abfd
, symoff
, SEEK_SET
);
704 if (bfd_bwrite ((PTR
) buf
, 12, abfd
) != 12)
706 fprintf (stderr
, "bfd_mach_o_scan_write_symtab_symbols: unable to write %d bytes at %lu\n",
707 12, (unsigned long) symoff
);
716 bfd_mach_o_scan_read_symtab_symbol (abfd
, sym
, s
, i
)
718 bfd_mach_o_symtab_command
*sym
;
722 bfd_mach_o_data_struct
*mdata
= abfd
->tdata
.mach_o_data
;
723 bfd_vma symoff
= sym
->symoff
+ (i
* 12);
724 unsigned char buf
[12];
725 unsigned char type
= -1;
726 unsigned char section
= -1;
728 unsigned long value
= -1;
729 unsigned long stroff
= -1;
730 unsigned int symtype
= -1;
732 BFD_ASSERT (sym
->strtab
!= NULL
);
734 bfd_seek (abfd
, symoff
, SEEK_SET
);
735 if (bfd_bread ((PTR
) buf
, 12, abfd
) != 12)
737 fprintf (stderr
, "bfd_mach_o_scan_read_symtab_symbol: unable to read %d bytes at %lu\n",
738 12, (unsigned long) symoff
);
742 stroff
= bfd_h_get_32 (abfd
, buf
);
743 type
= bfd_h_get_8 (abfd
, buf
+ 4);
744 symtype
= (type
& 0x0e);
745 section
= bfd_h_get_8 (abfd
, buf
+ 5) - 1;
746 desc
= bfd_h_get_16 (abfd
, buf
+ 6);
747 value
= bfd_h_get_32 (abfd
, buf
+ 8);
749 if (stroff
>= sym
->strsize
)
751 fprintf (stderr
, "bfd_mach_o_scan_read_symtab_symbol: symbol name out of range (%lu >= %lu)\n",
752 (unsigned long) stroff
, (unsigned long) sym
->strsize
);
757 s
->name
= sym
->strtab
+ stroff
;
759 s
->udata
.i
= (type
<< 24) | (section
<< 16) | desc
;
762 if (type
& BFD_MACH_O_N_STAB
)
764 s
->flags
|= BSF_DEBUGGING
;
765 s
->section
= bfd_und_section_ptr
;
769 if (type
& BFD_MACH_O_N_PEXT
)
771 type
&= ~BFD_MACH_O_N_PEXT
;
772 s
->flags
|= BSF_GLOBAL
;
775 if (type
& BFD_MACH_O_N_EXT
)
777 type
&= ~BFD_MACH_O_N_EXT
;
778 s
->flags
|= BSF_GLOBAL
;
783 case BFD_MACH_O_N_UNDF
:
784 s
->section
= bfd_und_section_ptr
;
786 case BFD_MACH_O_N_PBUD
:
787 s
->section
= bfd_und_section_ptr
;
789 case BFD_MACH_O_N_ABS
:
790 s
->section
= bfd_abs_section_ptr
;
792 case BFD_MACH_O_N_SECT
:
793 if ((section
> 0) && (section
<= mdata
->nsects
))
795 s
->section
= mdata
->sections
[section
- 1]->bfdsection
;
796 s
->value
= s
->value
- mdata
->sections
[section
- 1]->addr
;
800 /* Mach-O uses 0 to mean "no section"; not an error. */
803 fprintf (stderr
, "bfd_mach_o_scan_read_symtab_symbol: "
804 "symbol \"%s\" specified invalid section %d (max %lu): setting to undefined\n",
805 s
->name
, section
, mdata
->nsects
);
807 s
->section
= bfd_und_section_ptr
;
810 case BFD_MACH_O_N_INDR
:
811 fprintf (stderr
, "bfd_mach_o_scan_read_symtab_symbol: "
812 "symbol \"%s\" is unsupported 'indirect' reference: setting to undefined\n",
814 s
->section
= bfd_und_section_ptr
;
817 fprintf (stderr
, "bfd_mach_o_scan_read_symtab_symbol: "
818 "symbol \"%s\" specified invalid type field 0x%x: setting to undefined\n",
820 s
->section
= bfd_und_section_ptr
;
829 bfd_mach_o_scan_read_symtab_strtab (abfd
, sym
)
831 bfd_mach_o_symtab_command
*sym
;
833 BFD_ASSERT (sym
->strtab
== NULL
);
835 if (abfd
->flags
& BFD_IN_MEMORY
)
837 struct bfd_in_memory
*b
;
839 b
= (struct bfd_in_memory
*) abfd
->iostream
;
841 if ((sym
->stroff
+ sym
->strsize
) > b
->size
)
843 bfd_set_error (bfd_error_file_truncated
);
846 sym
->strtab
= b
->buffer
+ sym
->stroff
;
850 sym
->strtab
= bfd_alloc (abfd
, sym
->strsize
);
851 if (sym
->strtab
== NULL
)
854 bfd_seek (abfd
, sym
->stroff
, SEEK_SET
);
855 if (bfd_bread ((PTR
) sym
->strtab
, sym
->strsize
, abfd
) != sym
->strsize
)
857 fprintf (stderr
, "bfd_mach_o_scan_read_symtab_strtab: unable to read %lu bytes at %lu\n",
858 sym
->strsize
, sym
->stroff
);
866 bfd_mach_o_scan_read_symtab_symbols (abfd
, sym
)
868 bfd_mach_o_symtab_command
*sym
;
873 BFD_ASSERT (sym
->symbols
== NULL
);
874 sym
->symbols
= bfd_alloc (abfd
, sym
->nsyms
* sizeof (asymbol
));
876 if (sym
->symbols
== NULL
)
878 fprintf (stderr
, "bfd_mach_o_scan_read_symtab_symbols: unable to allocate memory for symbols\n");
882 ret
= bfd_mach_o_scan_read_symtab_strtab (abfd
, sym
);
886 for (i
= 0; i
< sym
->nsyms
; i
++)
888 ret
= bfd_mach_o_scan_read_symtab_symbol (abfd
, sym
, &sym
->symbols
[i
], i
);
897 bfd_mach_o_scan_read_dysymtab_symbol (abfd
, dysym
, sym
, s
, i
)
899 bfd_mach_o_dysymtab_command
*dysym
;
900 bfd_mach_o_symtab_command
*sym
;
904 unsigned long isymoff
= dysym
->indirectsymoff
+ (i
* 4);
905 unsigned long symindex
;
906 unsigned char buf
[4];
908 BFD_ASSERT (i
< dysym
->nindirectsyms
);
910 bfd_seek (abfd
, isymoff
, SEEK_SET
);
911 if (bfd_bread ((PTR
) buf
, 4, abfd
) != 4)
913 fprintf (stderr
, "bfd_mach_o_scan_read_dysymtab_symbol: unable to read %lu bytes at %lu\n",
914 (unsigned long) 4, isymoff
);
917 symindex
= bfd_h_get_32 (abfd
, buf
);
919 return bfd_mach_o_scan_read_symtab_symbol (abfd
, sym
, s
, symindex
);
923 bfd_mach_o_i386_flavour_string (flavour
)
924 unsigned int flavour
;
926 switch ((int) flavour
)
928 case BFD_MACH_O_i386_NEW_THREAD_STATE
: return "i386_NEW_THREAD_STATE";
929 case BFD_MACH_O_i386_FLOAT_STATE
: return "i386_FLOAT_STATE";
930 case BFD_MACH_O_i386_ISA_PORT_MAP_STATE
: return "i386_ISA_PORT_MAP_STATE";
931 case BFD_MACH_O_i386_V86_ASSIST_STATE
: return "i386_V86_ASSIST_STATE";
932 case BFD_MACH_O_i386_REGS_SEGS_STATE
: return "i386_REGS_SEGS_STATE";
933 case BFD_MACH_O_i386_THREAD_SYSCALL_STATE
: return "i386_THREAD_SYSCALL_STATE";
934 case BFD_MACH_O_i386_THREAD_STATE_NONE
: return "i386_THREAD_STATE_NONE";
935 case BFD_MACH_O_i386_SAVED_STATE
: return "i386_SAVED_STATE";
936 case BFD_MACH_O_i386_THREAD_STATE
: return "i386_THREAD_STATE";
937 case BFD_MACH_O_i386_THREAD_FPSTATE
: return "i386_THREAD_FPSTATE";
938 case BFD_MACH_O_i386_THREAD_EXCEPTSTATE
: return "i386_THREAD_EXCEPTSTATE";
939 case BFD_MACH_O_i386_THREAD_CTHREADSTATE
: return "i386_THREAD_CTHREADSTATE";
940 default: return "UNKNOWN";
945 bfd_mach_o_ppc_flavour_string (flavour
)
946 unsigned int flavour
;
948 switch ((int) flavour
)
950 case BFD_MACH_O_PPC_THREAD_STATE
: return "PPC_THREAD_STATE";
951 case BFD_MACH_O_PPC_FLOAT_STATE
: return "PPC_FLOAT_STATE";
952 case BFD_MACH_O_PPC_EXCEPTION_STATE
: return "PPC_EXCEPTION_STATE";
953 case BFD_MACH_O_PPC_VECTOR_STATE
: return "PPC_VECTOR_STATE";
954 default: return "UNKNOWN";
959 bfd_mach_o_scan_write_thread (abfd
, command
)
961 bfd_mach_o_load_command
*command
;
963 bfd_mach_o_thread_command
*cmd
= &command
->command
.thread
;
965 unsigned char buf
[8];
967 unsigned int nflavours
;
969 BFD_ASSERT ((command
->type
== BFD_MACH_O_LC_THREAD
)
970 || (command
->type
== BFD_MACH_O_LC_UNIXTHREAD
));
974 for (i
= 0; i
< cmd
->nflavours
; i
++)
976 BFD_ASSERT ((cmd
->flavours
[i
].size
% 4) == 0);
977 BFD_ASSERT (cmd
->flavours
[i
].offset
== (command
->offset
+ offset
+ 8));
979 bfd_h_put_32 (abfd
, cmd
->flavours
[i
].flavour
, buf
);
980 bfd_h_put_32 (abfd
, (cmd
->flavours
[i
].size
/ 4), buf
+ 4);
982 bfd_seek (abfd
, command
->offset
+ offset
, SEEK_SET
);
983 if (bfd_bwrite ((PTR
) buf
, 8, abfd
) != 8)
986 offset
+= cmd
->flavours
[i
].size
+ 8;
993 bfd_mach_o_scan_read_dylinker (abfd
, command
)
995 bfd_mach_o_load_command
*command
;
997 bfd_mach_o_dylinker_command
*cmd
= &command
->command
.dylinker
;
998 unsigned char buf
[4];
999 unsigned int nameoff
;
1004 BFD_ASSERT ((command
->type
== BFD_MACH_O_LC_ID_DYLINKER
)
1005 || (command
->type
== BFD_MACH_O_LC_LOAD_DYLINKER
));
1007 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
1008 if (bfd_bread ((PTR
) buf
, 4, abfd
) != 4)
1011 nameoff
= bfd_h_get_32 (abfd
, buf
+ 0);
1013 cmd
->name_offset
= command
->offset
+ nameoff
;
1014 cmd
->name_len
= command
->len
- nameoff
;
1016 if (command
->type
== BFD_MACH_O_LC_LOAD_DYLINKER
)
1017 prefix
= "LC_LOAD_DYLINKER";
1018 else if (command
->type
== BFD_MACH_O_LC_ID_DYLINKER
)
1019 prefix
= "LC_ID_DYLINKER";
1023 sname
= (char *) bfd_alloc (abfd
, strlen (prefix
) + 1);
1026 strcpy (sname
, prefix
);
1028 bfdsec
= bfd_make_section_anyway (abfd
, sname
);
1034 bfdsec
->_raw_size
= command
->len
- 8;
1035 bfdsec
->filepos
= command
->offset
+ 8;
1036 bfdsec
->alignment_power
= 0;
1037 bfdsec
->flags
= SEC_HAS_CONTENTS
;
1039 cmd
->section
= bfdsec
;
1045 bfd_mach_o_scan_read_dylib (abfd
, command
)
1047 bfd_mach_o_load_command
*command
;
1049 bfd_mach_o_dylib_command
*cmd
= &command
->command
.dylib
;
1050 unsigned char buf
[16];
1051 unsigned int nameoff
;
1056 BFD_ASSERT ((command
->type
== BFD_MACH_O_LC_ID_DYLIB
)
1057 || (command
->type
== BFD_MACH_O_LC_LOAD_DYLIB
)
1058 || (command
->type
== BFD_MACH_O_LC_LOAD_WEAK_DYLIB
));
1060 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
1061 if (bfd_bread ((PTR
) buf
, 16, abfd
) != 16)
1064 nameoff
= bfd_h_get_32 (abfd
, buf
+ 0);
1065 cmd
->timestamp
= bfd_h_get_32 (abfd
, buf
+ 4);
1066 cmd
->current_version
= bfd_h_get_32 (abfd
, buf
+ 8);
1067 cmd
->compatibility_version
= bfd_h_get_32 (abfd
, buf
+ 12);
1069 cmd
->name_offset
= command
->offset
+ nameoff
;
1070 cmd
->name_len
= command
->len
- nameoff
;
1072 if (command
->type
== BFD_MACH_O_LC_LOAD_DYLIB
)
1073 prefix
= "LC_LOAD_DYLIB";
1074 else if (command
->type
== BFD_MACH_O_LC_LOAD_WEAK_DYLIB
)
1075 prefix
= "LC_LOAD_WEAK_DYLIB";
1076 else if (command
->type
== BFD_MACH_O_LC_ID_DYLIB
)
1077 prefix
= "LC_ID_DYLIB";
1081 sname
= (char *) bfd_alloc (abfd
, strlen (prefix
) + 1);
1084 strcpy (sname
, prefix
);
1086 bfdsec
= bfd_make_section_anyway (abfd
, sname
);
1092 bfdsec
->_raw_size
= command
->len
- 8;
1093 bfdsec
->filepos
= command
->offset
+ 8;
1094 bfdsec
->alignment_power
= 0;
1095 bfdsec
->flags
= SEC_HAS_CONTENTS
;
1097 cmd
->section
= bfdsec
;
1103 bfd_mach_o_scan_read_prebound_dylib (abfd
, command
)
1104 bfd
*abfd ATTRIBUTE_UNUSED
;
1105 bfd_mach_o_load_command
*command ATTRIBUTE_UNUSED
;
1107 /* bfd_mach_o_prebound_dylib_command *cmd = &command->command.prebound_dylib; */
1109 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_PREBOUND_DYLIB
);
1114 bfd_mach_o_scan_read_thread (abfd
, command
)
1116 bfd_mach_o_load_command
*command
;
1118 bfd_mach_o_data_struct
*mdata
= NULL
;
1119 bfd_mach_o_thread_command
*cmd
= &command
->command
.thread
;
1120 unsigned char buf
[8];
1122 unsigned int nflavours
;
1125 BFD_ASSERT ((command
->type
== BFD_MACH_O_LC_THREAD
)
1126 || (command
->type
== BFD_MACH_O_LC_UNIXTHREAD
));
1128 BFD_ASSERT (bfd_mach_o_valid (abfd
));
1129 mdata
= abfd
->tdata
.mach_o_data
;
1133 while (offset
!= command
->len
)
1135 if (offset
>= command
->len
)
1138 bfd_seek (abfd
, command
->offset
+ offset
, SEEK_SET
);
1140 if (bfd_bread ((PTR
) buf
, 8, abfd
) != 8)
1143 offset
+= 8 + bfd_h_get_32 (abfd
, buf
+ 4) * 4;
1148 ((bfd_mach_o_thread_flavour
*)
1149 bfd_alloc (abfd
, nflavours
* sizeof (bfd_mach_o_thread_flavour
)));
1150 if (cmd
->flavours
== NULL
)
1152 cmd
->nflavours
= nflavours
;
1156 while (offset
!= command
->len
)
1158 if (offset
>= command
->len
)
1161 if (nflavours
>= cmd
->nflavours
)
1164 bfd_seek (abfd
, command
->offset
+ offset
, SEEK_SET
);
1166 if (bfd_bread ((PTR
) buf
, 8, abfd
) != 8)
1169 cmd
->flavours
[nflavours
].flavour
= bfd_h_get_32 (abfd
, buf
);
1170 cmd
->flavours
[nflavours
].offset
= command
->offset
+ offset
+ 8;
1171 cmd
->flavours
[nflavours
].size
= bfd_h_get_32 (abfd
, buf
+ 4) * 4;
1172 offset
+= cmd
->flavours
[nflavours
].size
+ 8;
1176 for (i
= 0; i
< nflavours
; i
++)
1179 unsigned int snamelen
;
1181 const char *flavourstr
;
1182 const char *prefix
= "LC_THREAD";
1185 switch (mdata
->header
.cputype
)
1187 case BFD_MACH_O_CPU_TYPE_POWERPC
:
1188 flavourstr
= bfd_mach_o_ppc_flavour_string (cmd
->flavours
[i
].flavour
);
1190 case BFD_MACH_O_CPU_TYPE_I386
:
1191 flavourstr
= bfd_mach_o_i386_flavour_string (cmd
->flavours
[i
].flavour
);
1194 flavourstr
= "UNKNOWN_ARCHITECTURE";
1198 snamelen
= strlen (prefix
) + 1 + 20 + 1 + strlen (flavourstr
) + 1;
1199 sname
= (char *) bfd_alloc (abfd
, snamelen
);
1205 sprintf (sname
, "%s.%s.%u", prefix
, flavourstr
, j
);
1206 if (bfd_get_section_by_name (abfd
, sname
) == NULL
)
1211 bfdsec
= bfd_make_section (abfd
, sname
);
1215 bfdsec
->_raw_size
= cmd
->flavours
[i
].size
;
1216 bfdsec
->filepos
= cmd
->flavours
[i
].offset
;
1217 bfdsec
->alignment_power
= 0x0;
1218 bfdsec
->flags
= SEC_HAS_CONTENTS
;
1220 cmd
->section
= bfdsec
;
1227 bfd_mach_o_scan_write_symtab (abfd
, command
)
1229 bfd_mach_o_load_command
*command
;
1231 bfd_mach_o_symtab_command
*seg
= &command
->command
.symtab
;
1232 unsigned char buf
[16];
1234 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SYMTAB
);
1236 bfd_h_put_32 (abfd
, seg
->symoff
, buf
);
1237 bfd_h_put_32 (abfd
, seg
->nsyms
, buf
+ 4);
1238 bfd_h_put_32 (abfd
, seg
->stroff
, buf
+ 8);
1239 bfd_h_put_32 (abfd
, seg
->strsize
, buf
+ 12);
1241 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
1242 if (bfd_bwrite ((PTR
) buf
, 16, abfd
) != 16)
1245 if (bfd_mach_o_scan_write_symtab_symbols (abfd
, command
) != 0)
1252 bfd_mach_o_scan_read_dysymtab (abfd
, command
)
1254 bfd_mach_o_load_command
*command
;
1256 bfd_mach_o_dysymtab_command
*seg
= &command
->command
.dysymtab
;
1257 unsigned char buf
[72];
1259 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_DYSYMTAB
);
1261 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
1262 if (bfd_bread ((PTR
) buf
, 72, abfd
) != 72)
1265 seg
->ilocalsym
= bfd_h_get_32 (abfd
, buf
+ 0);
1266 seg
->nlocalsym
= bfd_h_get_32 (abfd
, buf
+ 4);
1267 seg
->iextdefsym
= bfd_h_get_32 (abfd
, buf
+ 8);
1268 seg
->nextdefsym
= bfd_h_get_32 (abfd
, buf
+ 12);
1269 seg
->iundefsym
= bfd_h_get_32 (abfd
, buf
+ 16);
1270 seg
->nundefsym
= bfd_h_get_32 (abfd
, buf
+ 20);
1271 seg
->tocoff
= bfd_h_get_32 (abfd
, buf
+ 24);
1272 seg
->ntoc
= bfd_h_get_32 (abfd
, buf
+ 28);
1273 seg
->modtaboff
= bfd_h_get_32 (abfd
, buf
+ 32);
1274 seg
->nmodtab
= bfd_h_get_32 (abfd
, buf
+ 36);
1275 seg
->extrefsymoff
= bfd_h_get_32 (abfd
, buf
+ 40);
1276 seg
->nextrefsyms
= bfd_h_get_32 (abfd
, buf
+ 44);
1277 seg
->indirectsymoff
= bfd_h_get_32 (abfd
, buf
+ 48);
1278 seg
->nindirectsyms
= bfd_h_get_32 (abfd
, buf
+ 52);
1279 seg
->extreloff
= bfd_h_get_32 (abfd
, buf
+ 56);
1280 seg
->nextrel
= bfd_h_get_32 (abfd
, buf
+ 60);
1281 seg
->locreloff
= bfd_h_get_32 (abfd
, buf
+ 64);
1282 seg
->nlocrel
= bfd_h_get_32 (abfd
, buf
+ 68);
1288 bfd_mach_o_scan_read_symtab (abfd
, command
)
1290 bfd_mach_o_load_command
*command
;
1292 bfd_mach_o_symtab_command
*seg
= &command
->command
.symtab
;
1293 unsigned char buf
[16];
1296 const char *prefix
= "LC_SYMTAB.stabs";
1298 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SYMTAB
);
1300 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
1301 if (bfd_bread ((PTR
) buf
, 16, abfd
) != 16)
1304 seg
->symoff
= bfd_h_get_32 (abfd
, buf
);
1305 seg
->nsyms
= bfd_h_get_32 (abfd
, buf
+ 4);
1306 seg
->stroff
= bfd_h_get_32 (abfd
, buf
+ 8);
1307 seg
->strsize
= bfd_h_get_32 (abfd
, buf
+ 12);
1308 seg
->symbols
= NULL
;
1311 sname
= (char *) bfd_alloc (abfd
, strlen (prefix
) + 1);
1314 strcpy (sname
, prefix
);
1316 bfdsec
= bfd_make_section_anyway (abfd
, sname
);
1322 bfdsec
->_raw_size
= seg
->nsyms
* 12;
1323 bfdsec
->filepos
= seg
->symoff
;
1324 bfdsec
->alignment_power
= 0;
1325 bfdsec
->flags
= SEC_HAS_CONTENTS
;
1327 seg
->stabs_segment
= bfdsec
;
1329 prefix
= "LC_SYMTAB.stabstr";
1330 sname
= (char *) bfd_alloc (abfd
, strlen (prefix
) + 1);
1333 strcpy (sname
, prefix
);
1335 bfdsec
= bfd_make_section_anyway (abfd
, sname
);
1341 bfdsec
->_raw_size
= seg
->strsize
;
1342 bfdsec
->filepos
= seg
->stroff
;
1343 bfdsec
->alignment_power
= 0;
1344 bfdsec
->flags
= SEC_HAS_CONTENTS
;
1346 seg
->stabstr_segment
= bfdsec
;
1352 bfd_mach_o_scan_read_segment (abfd
, command
)
1354 bfd_mach_o_load_command
*command
;
1356 unsigned char buf
[48];
1357 bfd_mach_o_segment_command
*seg
= &command
->command
.segment
;
1361 const char *prefix
= "LC_SEGMENT";
1362 unsigned int snamelen
;
1364 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SEGMENT
);
1366 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
1367 if (bfd_bread ((PTR
) buf
, 48, abfd
) != 48)
1370 memcpy (seg
->segname
, buf
, 16);
1371 seg
->vmaddr
= bfd_h_get_32 (abfd
, buf
+ 16);
1372 seg
->vmsize
= bfd_h_get_32 (abfd
, buf
+ 20);
1373 seg
->fileoff
= bfd_h_get_32 (abfd
, buf
+ 24);
1374 seg
->filesize
= bfd_h_get_32 (abfd
, buf
+ 28);
1375 /* seg->maxprot = bfd_h_get_32 (abfd, buf + 32); */
1376 /* seg->initprot = bfd_h_get_32 (abfd, buf + 36); */
1377 seg
->nsects
= bfd_h_get_32 (abfd
, buf
+ 40);
1378 seg
->flags
= bfd_h_get_32 (abfd
, buf
+ 44);
1380 snamelen
= strlen (prefix
) + 1 + strlen (seg
->segname
) + 1;
1381 sname
= (char *) bfd_alloc (abfd
, snamelen
);
1384 sprintf (sname
, "%s.%s", prefix
, seg
->segname
);
1386 bfdsec
= bfd_make_section_anyway (abfd
, sname
);
1390 bfdsec
->vma
= seg
->vmaddr
;
1391 bfdsec
->lma
= seg
->vmaddr
;
1392 bfdsec
->_raw_size
= seg
->filesize
;
1393 bfdsec
->filepos
= seg
->fileoff
;
1394 bfdsec
->alignment_power
= 0x0;
1395 bfdsec
->flags
= SEC_HAS_CONTENTS
| SEC_LOAD
| SEC_ALLOC
| SEC_CODE
;
1397 seg
->segment
= bfdsec
;
1399 if (seg
->nsects
!= 0)
1402 ((bfd_mach_o_section
*)
1403 bfd_alloc (abfd
, seg
->nsects
* sizeof (bfd_mach_o_section
)));
1404 if (seg
->sections
== NULL
)
1407 for (i
= 0; i
< seg
->nsects
; i
++)
1409 bfd_vma segoff
= command
->offset
+ 48 + 8 + (i
* 68);
1411 if (bfd_mach_o_scan_read_section (abfd
, &seg
->sections
[i
],
1421 bfd_mach_o_scan_write_segment (abfd
, command
)
1423 bfd_mach_o_load_command
*command
;
1425 unsigned char buf
[48];
1426 bfd_mach_o_segment_command
*seg
= &command
->command
.segment
;
1429 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SEGMENT
);
1431 memcpy (buf
, seg
->segname
, 16);
1432 bfd_h_put_32 (abfd
, seg
->vmaddr
, buf
+ 16);
1433 bfd_h_put_32 (abfd
, seg
->vmsize
, buf
+ 20);
1434 bfd_h_put_32 (abfd
, seg
->fileoff
, buf
+ 24);
1435 bfd_h_put_32 (abfd
, seg
->filesize
, buf
+ 28);
1436 bfd_h_put_32 (abfd
, 0 /* seg->maxprot */, buf
+ 32);
1437 bfd_h_put_32 (abfd
, 0 /* seg->initprot */, buf
+ 36);
1438 bfd_h_put_32 (abfd
, seg
->nsects
, buf
+ 40);
1439 bfd_h_put_32 (abfd
, seg
->flags
, buf
+ 44);
1441 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
1442 if (bfd_bwrite ((PTR
) buf
, 48, abfd
) != 48)
1447 bfd_vma nbytes
= seg
->filesize
;
1448 bfd_vma curoff
= seg
->fileoff
;
1452 bfd_vma thisread
= nbytes
;
1454 if (thisread
> 1024)
1457 bfd_seek (abfd
, curoff
, SEEK_SET
);
1458 if (bfd_bread ((PTR
) buf
, thisread
, abfd
) != thisread
)
1461 bfd_seek (abfd
, curoff
, SEEK_SET
);
1462 if (bfd_bwrite ((PTR
) buf
, thisread
, abfd
) != thisread
)
1470 for (i
= 0; i
< seg
->nsects
; i
++)
1472 bfd_vma segoff
= command
->offset
+ 48 + 8 + (i
* 68);
1474 if (bfd_mach_o_scan_write_section (abfd
, &seg
->sections
[i
], segoff
) != 0)
1482 bfd_mach_o_scan_read_command (abfd
, command
)
1484 bfd_mach_o_load_command
*command
;
1486 unsigned char buf
[8];
1488 bfd_seek (abfd
, command
->offset
, SEEK_SET
);
1489 if (bfd_bread ((PTR
) buf
, 8, abfd
) != 8)
1492 command
->type
= (bfd_h_get_32 (abfd
, buf
) & ~BFD_MACH_O_LC_REQ_DYLD
);
1493 command
->type_required
= (bfd_h_get_32 (abfd
, buf
) & BFD_MACH_O_LC_REQ_DYLD
1495 command
->len
= bfd_h_get_32 (abfd
, buf
+ 4);
1497 switch (command
->type
)
1499 case BFD_MACH_O_LC_SEGMENT
:
1500 if (bfd_mach_o_scan_read_segment (abfd
, command
) != 0)
1503 case BFD_MACH_O_LC_SYMTAB
:
1504 if (bfd_mach_o_scan_read_symtab (abfd
, command
) != 0)
1507 case BFD_MACH_O_LC_SYMSEG
:
1509 case BFD_MACH_O_LC_THREAD
:
1510 case BFD_MACH_O_LC_UNIXTHREAD
:
1511 if (bfd_mach_o_scan_read_thread (abfd
, command
) != 0)
1514 case BFD_MACH_O_LC_LOAD_DYLINKER
:
1515 case BFD_MACH_O_LC_ID_DYLINKER
:
1516 if (bfd_mach_o_scan_read_dylinker (abfd
, command
) != 0)
1519 case BFD_MACH_O_LC_LOAD_DYLIB
:
1520 case BFD_MACH_O_LC_ID_DYLIB
:
1521 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB
:
1522 if (bfd_mach_o_scan_read_dylib (abfd
, command
) != 0)
1525 case BFD_MACH_O_LC_PREBOUND_DYLIB
:
1526 if (bfd_mach_o_scan_read_prebound_dylib (abfd
, command
) != 0)
1529 case BFD_MACH_O_LC_LOADFVMLIB
:
1530 case BFD_MACH_O_LC_IDFVMLIB
:
1531 case BFD_MACH_O_LC_IDENT
:
1532 case BFD_MACH_O_LC_FVMFILE
:
1533 case BFD_MACH_O_LC_PREPAGE
:
1534 case BFD_MACH_O_LC_ROUTINES
:
1535 case BFD_MACH_O_LC_SUB_FRAMEWORK
:
1537 case BFD_MACH_O_LC_DYSYMTAB
:
1538 if (bfd_mach_o_scan_read_dysymtab (abfd
, command
) != 0)
1541 case BFD_MACH_O_LC_SUB_UMBRELLA
:
1542 case BFD_MACH_O_LC_SUB_CLIENT
:
1543 case BFD_MACH_O_LC_SUB_LIBRARY
:
1544 case BFD_MACH_O_LC_TWOLEVEL_HINTS
:
1545 case BFD_MACH_O_LC_PREBIND_CKSUM
:
1548 fprintf (stderr
, "unable to read unknown load command 0x%lx\n",
1549 (unsigned long) command
->type
);
1557 bfd_mach_o_flatten_sections (abfd
)
1560 bfd_mach_o_data_struct
*mdata
= abfd
->tdata
.mach_o_data
;
1566 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
1568 if (mdata
->commands
[i
].type
== BFD_MACH_O_LC_SEGMENT
)
1570 bfd_mach_o_segment_command
*seg
;
1572 seg
= &mdata
->commands
[i
].command
.segment
;
1573 mdata
->nsects
+= seg
->nsects
;
1577 mdata
->sections
= bfd_alloc (abfd
,
1578 mdata
->nsects
* sizeof (bfd_mach_o_section
*));
1581 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
1583 if (mdata
->commands
[i
].type
== BFD_MACH_O_LC_SEGMENT
)
1585 bfd_mach_o_segment_command
*seg
;
1587 seg
= &mdata
->commands
[i
].command
.segment
;
1588 BFD_ASSERT (csect
+ seg
->nsects
<= mdata
->nsects
);
1590 for (j
= 0; j
< seg
->nsects
; j
++)
1591 mdata
->sections
[csect
++] = &seg
->sections
[j
];
1597 bfd_mach_o_scan_start_address (abfd
)
1600 bfd_mach_o_data_struct
*mdata
= abfd
->tdata
.mach_o_data
;
1601 bfd_mach_o_thread_command
*cmd
= NULL
;
1604 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
1606 if ((mdata
->commands
[i
].type
== BFD_MACH_O_LC_THREAD
) ||
1607 (mdata
->commands
[i
].type
== BFD_MACH_O_LC_UNIXTHREAD
))
1610 cmd
= &mdata
->commands
[i
].command
.thread
;
1619 for (i
= 0; i
< cmd
->nflavours
; i
++)
1621 if ((mdata
->header
.cputype
== BFD_MACH_O_CPU_TYPE_I386
)
1622 && (cmd
->flavours
[i
].flavour
1623 == (unsigned long) BFD_MACH_O_i386_THREAD_STATE
))
1625 unsigned char buf
[4];
1627 bfd_seek (abfd
, cmd
->flavours
[i
].offset
+ 40, SEEK_SET
);
1629 if (bfd_bread (buf
, 4, abfd
) != 4)
1632 abfd
->start_address
= bfd_h_get_32 (abfd
, buf
);
1634 else if ((mdata
->header
.cputype
== BFD_MACH_O_CPU_TYPE_POWERPC
)
1635 && (cmd
->flavours
[i
].flavour
== BFD_MACH_O_PPC_THREAD_STATE
))
1637 unsigned char buf
[4];
1639 bfd_seek (abfd
, cmd
->flavours
[i
].offset
+ 0, SEEK_SET
);
1641 if (bfd_bread (buf
, 4, abfd
) != 4)
1644 abfd
->start_address
= bfd_h_get_32 (abfd
, buf
);
1652 bfd_mach_o_scan (abfd
, header
, mdata
)
1654 bfd_mach_o_header
*header
;
1655 bfd_mach_o_data_struct
*mdata
;
1658 enum bfd_architecture cputype
;
1659 unsigned long cpusubtype
;
1661 mdata
->header
= *header
;
1662 mdata
->symbols
= NULL
;
1664 abfd
->flags
= (abfd
->xvec
->object_flags
1665 | (abfd
->flags
& (BFD_IN_MEMORY
| BFD_IO_FUNCS
)));
1666 abfd
->tdata
.mach_o_data
= mdata
;
1668 bfd_mach_o_convert_architecture (header
->cputype
, header
->cpusubtype
,
1669 &cputype
, &cpusubtype
);
1670 if (cputype
== bfd_arch_unknown
)
1672 fprintf (stderr
, "bfd_mach_o_scan: unknown architecture 0x%lx/0x%lx\n",
1673 header
->cputype
, header
->cpusubtype
);
1677 bfd_set_arch_mach (abfd
, cputype
, cpusubtype
);
1679 if (header
->ncmds
!= 0)
1682 ((bfd_mach_o_load_command
*)
1683 bfd_alloc (abfd
, header
->ncmds
* sizeof (bfd_mach_o_load_command
)));
1684 if (mdata
->commands
== NULL
)
1687 for (i
= 0; i
< header
->ncmds
; i
++)
1689 bfd_mach_o_load_command
*cur
= &mdata
->commands
[i
];
1695 bfd_mach_o_load_command
*prev
= &mdata
->commands
[i
- 1];
1696 cur
->offset
= prev
->offset
+ prev
->len
;
1699 if (bfd_mach_o_scan_read_command (abfd
, cur
) < 0)
1704 if (bfd_mach_o_scan_start_address (abfd
) < 0)
1707 fprintf (stderr
, "bfd_mach_o_scan: unable to scan start address: %s\n",
1708 bfd_errmsg (bfd_get_error ()));
1709 abfd
->tdata
.mach_o_data
= NULL
;
1714 bfd_mach_o_flatten_sections (abfd
);
1720 bfd_mach_o_mkobject (abfd
)
1723 bfd_mach_o_data_struct
*mdata
= NULL
;
1725 mdata
= ((bfd_mach_o_data_struct
*)
1726 bfd_alloc (abfd
, sizeof (bfd_mach_o_data_struct
)));
1729 abfd
->tdata
.mach_o_data
= mdata
;
1731 mdata
->header
.magic
= 0;
1732 mdata
->header
.cputype
= 0;
1733 mdata
->header
.cpusubtype
= 0;
1734 mdata
->header
.filetype
= 0;
1735 mdata
->header
.ncmds
= 0;
1736 mdata
->header
.sizeofcmds
= 0;
1737 mdata
->header
.flags
= 0;
1738 mdata
->header
.byteorder
= BFD_ENDIAN_UNKNOWN
;
1739 mdata
->commands
= NULL
;
1740 mdata
->nsymbols
= 0;
1741 mdata
->symbols
= NULL
;
1743 mdata
->sections
= NULL
;
1750 bfd_mach_o_object_p (abfd
)
1753 struct bfd_preserve preserve
;
1754 bfd_mach_o_header header
;
1756 preserve
.marker
= NULL
;
1757 if (bfd_mach_o_read_header (abfd
, &header
) != 0)
1760 if (! (header
.byteorder
== BFD_ENDIAN_BIG
1761 || header
.byteorder
== BFD_ENDIAN_LITTLE
))
1763 fprintf (stderr
, "unknown header byte-order value 0x%lx\n",
1764 (long) header
.byteorder
);
1768 if (! ((header
.byteorder
== BFD_ENDIAN_BIG
1769 && abfd
->xvec
->byteorder
== BFD_ENDIAN_BIG
1770 && abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
1771 || (header
.byteorder
== BFD_ENDIAN_LITTLE
1772 && abfd
->xvec
->byteorder
== BFD_ENDIAN_LITTLE
1773 && abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)))
1776 preserve
.marker
= bfd_zalloc (abfd
, sizeof (bfd_mach_o_data_struct
));
1777 if (preserve
.marker
== NULL
1778 || !bfd_preserve_save (abfd
, &preserve
))
1781 if (bfd_mach_o_scan (abfd
, &header
,
1782 (bfd_mach_o_data_struct
*) preserve
.marker
) != 0)
1785 bfd_preserve_finish (abfd
, &preserve
);
1789 bfd_set_error (bfd_error_wrong_format
);
1792 if (preserve
.marker
!= NULL
)
1793 bfd_preserve_restore (abfd
, &preserve
);
1798 bfd_mach_o_core_p (abfd
)
1801 struct bfd_preserve preserve
;
1802 bfd_mach_o_header header
;
1804 preserve
.marker
= NULL
;
1805 if (bfd_mach_o_read_header (abfd
, &header
) != 0)
1808 if (! (header
.byteorder
== BFD_ENDIAN_BIG
1809 || header
.byteorder
== BFD_ENDIAN_LITTLE
))
1811 fprintf (stderr
, "unknown header byte-order value 0x%lx\n",
1812 (long) header
.byteorder
);
1816 if (! ((header
.byteorder
== BFD_ENDIAN_BIG
1817 && abfd
->xvec
->byteorder
== BFD_ENDIAN_BIG
1818 && abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
1819 || (header
.byteorder
== BFD_ENDIAN_LITTLE
1820 && abfd
->xvec
->byteorder
== BFD_ENDIAN_LITTLE
1821 && abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)))
1824 if (header
.filetype
!= BFD_MACH_O_MH_CORE
)
1827 preserve
.marker
= bfd_zalloc (abfd
, sizeof (bfd_mach_o_data_struct
));
1828 if (preserve
.marker
== NULL
1829 || !bfd_preserve_save (abfd
, &preserve
))
1832 if (bfd_mach_o_scan (abfd
, &header
,
1833 (bfd_mach_o_data_struct
*) preserve
.marker
) != 0)
1836 bfd_preserve_finish (abfd
, &preserve
);
1840 bfd_set_error (bfd_error_wrong_format
);
1843 if (preserve
.marker
!= NULL
)
1844 bfd_preserve_restore (abfd
, &preserve
);
1848 typedef struct mach_o_fat_archentry
1850 unsigned long cputype
;
1851 unsigned long cpusubtype
;
1852 unsigned long offset
;
1854 unsigned long align
;
1856 } mach_o_fat_archentry
;
1858 typedef struct mach_o_fat_data_struct
1860 unsigned long magic
;
1861 unsigned long nfat_arch
;
1862 mach_o_fat_archentry
*archentries
;
1863 } mach_o_fat_data_struct
;
1866 bfd_mach_o_archive_p (abfd
)
1869 mach_o_fat_data_struct
*adata
= NULL
;
1870 unsigned char buf
[20];
1873 bfd_seek (abfd
, 0, SEEK_SET
);
1874 if (bfd_bread ((PTR
) buf
, 8, abfd
) != 8)
1877 adata
= (mach_o_fat_data_struct
*)
1878 bfd_alloc (abfd
, sizeof (mach_o_fat_data_struct
));
1882 adata
->magic
= bfd_getb32 (buf
);
1883 adata
->nfat_arch
= bfd_getb32 (buf
+ 4);
1884 if (adata
->magic
!= 0xcafebabe)
1887 adata
->archentries
= (mach_o_fat_archentry
*)
1888 bfd_alloc (abfd
, adata
->nfat_arch
* sizeof (mach_o_fat_archentry
));
1889 if (adata
->archentries
== NULL
)
1892 for (i
= 0; i
< adata
->nfat_arch
; i
++)
1894 bfd_seek (abfd
, 8 + 20 * i
, SEEK_SET
);
1896 if (bfd_bread ((PTR
) buf
, 20, abfd
) != 20)
1898 adata
->archentries
[i
].cputype
= bfd_getb32 (buf
);
1899 adata
->archentries
[i
].cpusubtype
= bfd_getb32 (buf
+ 4);
1900 adata
->archentries
[i
].offset
= bfd_getb32 (buf
+ 8);
1901 adata
->archentries
[i
].size
= bfd_getb32 (buf
+ 12);
1902 adata
->archentries
[i
].align
= bfd_getb32 (buf
+ 16);
1903 adata
->archentries
[i
].abfd
= NULL
;
1906 abfd
->tdata
.mach_o_fat_data
= adata
;
1911 bfd_release (abfd
, adata
);
1912 bfd_set_error (bfd_error_wrong_format
);
1917 bfd_mach_o_openr_next_archived_file (archive
, prev
)
1921 mach_o_fat_data_struct
*adata
;
1922 mach_o_fat_archentry
*entry
= NULL
;
1925 adata
= (mach_o_fat_data_struct
*) archive
->tdata
.mach_o_fat_data
;
1926 BFD_ASSERT (adata
!= NULL
);
1928 /* Find index of previous entry. */
1930 i
= 0; /* Start at first one. */
1933 for (i
= 0; i
< adata
->nfat_arch
; i
++)
1935 if (adata
->archentries
[i
].abfd
== prev
)
1939 if (i
== adata
->nfat_arch
)
1942 bfd_set_error (bfd_error_bad_value
);
1945 i
++; /* Get next entry. */
1948 if (i
>= adata
->nfat_arch
)
1950 bfd_set_error (bfd_error_no_more_archived_files
);
1954 entry
= &adata
->archentries
[i
];
1955 if (entry
->abfd
== NULL
)
1957 bfd
*nbfd
= _bfd_new_bfd_contained_in (archive
);
1963 nbfd
->origin
= entry
->offset
;
1964 s
= bfd_malloc (strlen (archive
->filename
) + 1);
1967 strcpy (s
, archive
->filename
);
1969 nbfd
->iostream
= NULL
;
1977 bfd_mach_o_lookup_section (abfd
, section
, mcommand
, msection
)
1980 bfd_mach_o_load_command
**mcommand
;
1981 bfd_mach_o_section
**msection
;
1983 struct mach_o_data_struct
*md
= abfd
->tdata
.mach_o_data
;
1984 unsigned int i
, j
, num
;
1986 bfd_mach_o_load_command
*ncmd
= NULL
;
1987 bfd_mach_o_section
*nsect
= NULL
;
1989 BFD_ASSERT (mcommand
!= NULL
);
1990 BFD_ASSERT (msection
!= NULL
);
1993 for (i
= 0; i
< md
->header
.ncmds
; i
++)
1995 struct bfd_mach_o_load_command
*cmd
= &md
->commands
[i
];
1996 struct bfd_mach_o_segment_command
*seg
= NULL
;
1998 if (cmd
->type
!= BFD_MACH_O_LC_SEGMENT
)
2000 seg
= &cmd
->command
.segment
;
2002 if (seg
->segment
== section
)
2009 for (j
= 0; j
< seg
->nsects
; j
++)
2011 struct bfd_mach_o_section
*sect
= &seg
->sections
[j
];
2013 if (sect
->bfdsection
== section
)
2028 bfd_mach_o_lookup_command (abfd
, type
, mcommand
)
2030 bfd_mach_o_load_command_type type
;
2031 bfd_mach_o_load_command
**mcommand
;
2033 struct mach_o_data_struct
*md
= NULL
;
2034 bfd_mach_o_load_command
*ncmd
= NULL
;
2035 unsigned int i
, num
;
2037 md
= abfd
->tdata
.mach_o_data
;
2039 BFD_ASSERT (md
!= NULL
);
2040 BFD_ASSERT (mcommand
!= NULL
);
2043 for (i
= 0; i
< md
->header
.ncmds
; i
++)
2045 struct bfd_mach_o_load_command
*cmd
= &md
->commands
[i
];
2047 if (cmd
->type
!= type
)
2060 bfd_mach_o_stack_addr (type
)
2061 enum bfd_mach_o_cpu_type type
;
2065 case BFD_MACH_O_CPU_TYPE_MC680x0
:
2067 case BFD_MACH_O_CPU_TYPE_MC88000
:
2069 case BFD_MACH_O_CPU_TYPE_POWERPC
:
2071 case BFD_MACH_O_CPU_TYPE_I386
:
2073 case BFD_MACH_O_CPU_TYPE_SPARC
:
2075 case BFD_MACH_O_CPU_TYPE_I860
:
2077 case BFD_MACH_O_CPU_TYPE_HPPA
:
2078 return 0xc0000000 - 0x04000000;
2085 bfd_mach_o_core_fetch_environment (abfd
, rbuf
, rlen
)
2087 unsigned char **rbuf
;
2090 bfd_mach_o_data_struct
*mdata
= abfd
->tdata
.mach_o_data
;
2091 unsigned long stackaddr
= bfd_mach_o_stack_addr (mdata
->header
.cputype
);
2094 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
2096 bfd_mach_o_load_command
*cur
= &mdata
->commands
[i
];
2097 bfd_mach_o_segment_command
*seg
= NULL
;
2099 if (cur
->type
!= BFD_MACH_O_LC_SEGMENT
)
2102 seg
= &cur
->command
.segment
;
2104 if ((seg
->vmaddr
+ seg
->vmsize
) == stackaddr
)
2106 unsigned long start
= seg
->fileoff
;
2107 unsigned long end
= seg
->fileoff
+ seg
->filesize
;
2108 unsigned char *buf
= bfd_malloc (1024);
2109 unsigned long size
= 1024;
2113 bfd_size_type nread
= 0;
2114 unsigned long offset
;
2115 int found_nonnull
= 0;
2117 if (size
> (end
- start
))
2118 size
= (end
- start
);
2120 buf
= bfd_realloc (buf
, size
);
2122 bfd_seek (abfd
, end
- size
, SEEK_SET
);
2123 nread
= bfd_bread (buf
, size
, abfd
);
2128 for (offset
= 4; offset
<= size
; offset
+= 4)
2132 val
= *((unsigned long *) (buf
+ size
- offset
));
2133 if (! found_nonnull
)
2138 else if (val
== 0x0)
2140 unsigned long bottom
;
2143 bottom
= seg
->fileoff
+ seg
->filesize
- offset
;
2144 top
= seg
->fileoff
+ seg
->filesize
- 4;
2145 *rbuf
= bfd_malloc (top
- bottom
);
2146 *rlen
= top
- bottom
;
2148 memcpy (*rbuf
, buf
+ size
- *rlen
, *rlen
);
2153 if (size
== (end
- start
))
2165 bfd_mach_o_core_file_failing_command (abfd
)
2168 unsigned char *buf
= NULL
;
2169 unsigned int len
= 0;
2172 ret
= bfd_mach_o_core_fetch_environment (abfd
, &buf
, &len
);
2180 bfd_mach_o_core_file_failing_signal (abfd
)
2181 bfd
*abfd ATTRIBUTE_UNUSED
;
2187 bfd_mach_o_core_file_matches_executable_p (core_bfd
, exec_bfd
)
2188 bfd
*core_bfd ATTRIBUTE_UNUSED
;
2189 bfd
*exec_bfd ATTRIBUTE_UNUSED
;
2194 #define TARGET_NAME mach_o_be_vec
2195 #define TARGET_STRING "mach-o-be"
2196 #define TARGET_BIG_ENDIAN 1
2197 #define TARGET_ARCHIVE 0
2199 #include "mach-o-target.c"
2202 #undef TARGET_STRING
2203 #undef TARGET_BIG_ENDIAN
2204 #undef TARGET_ARCHIVE
2206 #define TARGET_NAME mach_o_le_vec
2207 #define TARGET_STRING "mach-o-le"
2208 #define TARGET_BIG_ENDIAN 0
2209 #define TARGET_ARCHIVE 0
2211 #include "mach-o-target.c"
2214 #undef TARGET_STRING
2215 #undef TARGET_BIG_ENDIAN
2216 #undef TARGET_ARCHIVE
2218 #define TARGET_NAME mach_o_fat_vec
2219 #define TARGET_STRING "mach-o-fat"
2220 #define TARGET_BIG_ENDIAN 1
2221 #define TARGET_ARCHIVE 1
2223 #include "mach-o-target.c"
2226 #undef TARGET_STRING
2227 #undef TARGET_BIG_ENDIAN
2228 #undef TARGET_ARCHIVE