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
73 #define bfd_mach_o_section_already_linked \
74 _bfd_generic_section_already_linked
75 #define bfd_mach_o_bfd_copy_private_header_data _bfd_generic_bfd_copy_private_header_data
77 static bfd_boolean bfd_mach_o_bfd_copy_private_symbol_data
78 PARAMS ((bfd
*, asymbol
*, bfd
*, asymbol
*));
79 static bfd_boolean bfd_mach_o_bfd_copy_private_section_data
80 PARAMS ((bfd
*, asection
*, bfd
*, asection
*));
81 static bfd_boolean bfd_mach_o_bfd_copy_private_bfd_data
82 PARAMS ((bfd
*, bfd
*));
83 static long bfd_mach_o_count_symbols
85 static long bfd_mach_o_get_symtab_upper_bound
87 static long bfd_mach_o_canonicalize_symtab
88 PARAMS ((bfd
*, asymbol
**));
89 static void bfd_mach_o_get_symbol_info
90 PARAMS ((bfd
*, asymbol
*, symbol_info
*));
91 static void bfd_mach_o_print_symbol
92 PARAMS ((bfd
*, PTR
, asymbol
*, bfd_print_symbol_type
));
93 static void bfd_mach_o_convert_architecture
94 PARAMS ((bfd_mach_o_cpu_type
, bfd_mach_o_cpu_subtype
,
95 enum bfd_architecture
*, unsigned long *));
96 static bfd_boolean bfd_mach_o_write_contents
98 static int bfd_mach_o_sizeof_headers
99 PARAMS ((bfd
*, bfd_boolean
));
100 static asymbol
* bfd_mach_o_make_empty_symbol
102 static int bfd_mach_o_write_header
103 PARAMS ((bfd
*, bfd_mach_o_header
*));
104 static int bfd_mach_o_read_header
105 PARAMS ((bfd
*, bfd_mach_o_header
*));
106 static asection
* bfd_mach_o_make_bfd_section
107 PARAMS ((bfd
*, bfd_mach_o_section
*));
108 static int bfd_mach_o_scan_read_section
109 PARAMS ((bfd
*, bfd_mach_o_section
*, bfd_vma
));
110 static int bfd_mach_o_scan_write_section
111 PARAMS ((bfd
*, bfd_mach_o_section
*, bfd_vma
));
112 static int bfd_mach_o_scan_write_symtab_symbols
113 PARAMS ((bfd
*, bfd_mach_o_load_command
*));
114 static int bfd_mach_o_scan_write_thread
115 PARAMS ((bfd
*, bfd_mach_o_load_command
*));
116 static int bfd_mach_o_scan_read_dylinker
117 PARAMS ((bfd
*, bfd_mach_o_load_command
*));
118 static int bfd_mach_o_scan_read_dylib
119 PARAMS ((bfd
*, bfd_mach_o_load_command
*));
120 static int bfd_mach_o_scan_read_prebound_dylib
121 PARAMS ((bfd
*, bfd_mach_o_load_command
*));
122 static int bfd_mach_o_scan_read_thread
123 PARAMS ((bfd
*, bfd_mach_o_load_command
*));
124 static int bfd_mach_o_scan_write_symtab
125 PARAMS ((bfd
*, bfd_mach_o_load_command
*));
126 static int bfd_mach_o_scan_read_dysymtab
127 PARAMS ((bfd
*, bfd_mach_o_load_command
*));
128 static int bfd_mach_o_scan_read_symtab
129 PARAMS ((bfd
*, bfd_mach_o_load_command
*));
130 static int bfd_mach_o_scan_read_segment
131 PARAMS ((bfd
*, bfd_mach_o_load_command
*));
132 static int bfd_mach_o_scan_write_segment
133 PARAMS ((bfd
*, bfd_mach_o_load_command
*));
134 static int bfd_mach_o_scan_read_command
135 PARAMS ((bfd
*, bfd_mach_o_load_command
*));
136 static void bfd_mach_o_flatten_sections
138 static const char * bfd_mach_o_i386_flavour_string
139 PARAMS ((unsigned int));
140 static const char * bfd_mach_o_ppc_flavour_string
141 PARAMS ((unsigned int));
143 /* The flags field of a section structure is separated into two parts a section
144 type and section attributes. The section types are mutually exclusive (it
145 can only have one type) but the section attributes are not (it may have more
146 than one attribute). */
148 #define SECTION_TYPE 0x000000ff /* 256 section types. */
149 #define SECTION_ATTRIBUTES 0xffffff00 /* 24 section attributes. */
151 /* Constants for the section attributes part of the flags field of a section
154 #define SECTION_ATTRIBUTES_USR 0xff000000 /* User-settable attributes. */
155 #define S_ATTR_PURE_INSTRUCTIONS 0x80000000 /* Section contains only true machine instructions. */
156 #define SECTION_ATTRIBUTES_SYS 0x00ffff00 /* System setable attributes. */
157 #define S_ATTR_SOME_INSTRUCTIONS 0x00000400 /* Section contains some machine instructions. */
158 #define S_ATTR_EXT_RELOC 0x00000200 /* Section has external relocation entries. */
159 #define S_ATTR_LOC_RELOC 0x00000100 /* Section has local relocation entries. */
170 bfd_mach_o_valid (abfd
)
173 if (abfd
== NULL
|| abfd
->xvec
== NULL
)
176 if (! ((abfd
->xvec
== &mach_o_be_vec
)
177 || (abfd
->xvec
== &mach_o_le_vec
)
178 || (abfd
->xvec
== &mach_o_fat_vec
)))
181 if (abfd
->tdata
.mach_o_data
== NULL
)
186 /* Copy any private info we understand from the input symbol
187 to the output symbol. */
190 bfd_mach_o_bfd_copy_private_symbol_data (ibfd
, isymbol
, obfd
, osymbol
)
191 bfd
*ibfd ATTRIBUTE_UNUSED
;
192 asymbol
*isymbol ATTRIBUTE_UNUSED
;
193 bfd
*obfd ATTRIBUTE_UNUSED
;
194 asymbol
*osymbol ATTRIBUTE_UNUSED
;
199 /* Copy any private info we understand from the input section
200 to the output section. */
203 bfd_mach_o_bfd_copy_private_section_data (ibfd
, isection
, obfd
, osection
)
204 bfd
*ibfd ATTRIBUTE_UNUSED
;
205 asection
*isection ATTRIBUTE_UNUSED
;
206 bfd
*obfd ATTRIBUTE_UNUSED
;
207 asection
*osection ATTRIBUTE_UNUSED
;
212 /* Copy any private info we understand from the input bfd
213 to the output bfd. */
216 bfd_mach_o_bfd_copy_private_bfd_data (ibfd
, obfd
)
220 BFD_ASSERT (bfd_mach_o_valid (ibfd
));
221 BFD_ASSERT (bfd_mach_o_valid (obfd
));
223 obfd
->tdata
.mach_o_data
= ibfd
->tdata
.mach_o_data
;
224 obfd
->tdata
.mach_o_data
->ibfd
= ibfd
;
229 bfd_mach_o_count_symbols (abfd
)
232 bfd_mach_o_data_struct
*mdata
= NULL
;
236 BFD_ASSERT (bfd_mach_o_valid (abfd
));
237 mdata
= abfd
->tdata
.mach_o_data
;
239 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
240 if (mdata
->commands
[i
].type
== BFD_MACH_O_LC_SYMTAB
)
242 bfd_mach_o_symtab_command
*sym
= &mdata
->commands
[i
].command
.symtab
;
250 bfd_mach_o_get_symtab_upper_bound (abfd
)
253 long nsyms
= bfd_mach_o_count_symbols (abfd
);
258 return ((nsyms
+ 1) * sizeof (asymbol
*));
262 bfd_mach_o_canonicalize_symtab (abfd
, alocation
)
266 bfd_mach_o_data_struct
*mdata
= abfd
->tdata
.mach_o_data
;
267 long nsyms
= bfd_mach_o_count_symbols (abfd
);
268 asymbol
**csym
= alocation
;
274 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
276 if (mdata
->commands
[i
].type
== BFD_MACH_O_LC_SYMTAB
)
278 bfd_mach_o_symtab_command
*sym
= &mdata
->commands
[i
].command
.symtab
;
280 if (bfd_mach_o_scan_read_symtab_symbols (abfd
, &mdata
->commands
[i
].command
.symtab
) != 0)
282 fprintf (stderr
, "bfd_mach_o_canonicalize_symtab: unable to load symbols for section %lu\n", i
);
286 BFD_ASSERT (sym
->symbols
!= NULL
);
288 for (j
= 0; j
< sym
->nsyms
; j
++)
290 BFD_ASSERT (csym
< (alocation
+ nsyms
));
291 *csym
++ = &sym
->symbols
[j
];
302 bfd_mach_o_get_symbol_info (abfd
, symbol
, ret
)
303 bfd
*abfd ATTRIBUTE_UNUSED
;
307 bfd_symbol_info (symbol
, ret
);
311 bfd_mach_o_print_symbol (abfd
, afile
, symbol
, how
)
315 bfd_print_symbol_type how
;
317 FILE *file
= (FILE *) afile
;
321 case bfd_print_symbol_name
:
322 fprintf (file
, "%s", symbol
->name
);
325 bfd_print_symbol_vandf (abfd
, (PTR
) file
, symbol
);
326 fprintf (file
, " %-5s %s", symbol
->section
->name
, symbol
->name
);
331 bfd_mach_o_convert_architecture (mtype
, msubtype
, type
, subtype
)
332 bfd_mach_o_cpu_type mtype
;
333 bfd_mach_o_cpu_subtype msubtype ATTRIBUTE_UNUSED
;
334 enum bfd_architecture
*type
;
335 unsigned long *subtype
;
337 *subtype
= bfd_arch_unknown
;
341 case BFD_MACH_O_CPU_TYPE_VAX
: *type
= bfd_arch_vax
; break;
342 case BFD_MACH_O_CPU_TYPE_MC680x0
: *type
= bfd_arch_m68k
; break;
343 case BFD_MACH_O_CPU_TYPE_I386
: *type
= bfd_arch_i386
; break;
344 case BFD_MACH_O_CPU_TYPE_MIPS
: *type
= bfd_arch_mips
; break;
345 case BFD_MACH_O_CPU_TYPE_MC98000
: *type
= bfd_arch_m98k
; break;
346 case BFD_MACH_O_CPU_TYPE_HPPA
: *type
= bfd_arch_hppa
; break;
347 case BFD_MACH_O_CPU_TYPE_ARM
: *type
= bfd_arch_arm
; break;
348 case BFD_MACH_O_CPU_TYPE_MC88000
: *type
= bfd_arch_m88k
; break;
349 case BFD_MACH_O_CPU_TYPE_SPARC
: *type
= bfd_arch_sparc
; break;
350 case BFD_MACH_O_CPU_TYPE_I860
: *type
= bfd_arch_i860
; break;
351 case BFD_MACH_O_CPU_TYPE_ALPHA
: *type
= bfd_arch_alpha
; break;
352 case BFD_MACH_O_CPU_TYPE_POWERPC
: *type
= bfd_arch_powerpc
; break;
353 default: *type
= bfd_arch_unknown
; break;
358 case bfd_arch_i386
: *subtype
= bfd_mach_i386_i386
; break;
359 case bfd_arch_sparc
: *subtype
= bfd_mach_sparc
; break;
361 *subtype
= bfd_arch_unknown
;
366 bfd_mach_o_write_contents (abfd
)
372 bfd_mach_o_data_struct
*mdata
= abfd
->tdata
.mach_o_data
;
374 /* Write data sections first in case they overlap header data to be
377 for (s
= abfd
->sections
; s
!= (asection
*) NULL
; s
= s
->next
)
381 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
383 bfd_mach_o_load_command
*cur
= &mdata
->commands
[i
];
384 if (cur
->type
!= BFD_MACH_O_LC_SEGMENT
)
388 bfd_mach_o_segment_command
*seg
= &cur
->command
.segment
;
390 bfd_vma nbytes
= seg
->filesize
;
391 bfd_vma curoff
= seg
->fileoff
;
395 bfd_vma thisread
= nbytes
;
400 bfd_seek (abfd
, curoff
, SEEK_SET
);
401 if (bfd_bread ((PTR
) buf
, thisread
, abfd
) != thisread
)
404 bfd_seek (abfd
, curoff
, SEEK_SET
);
405 if (bfd_bwrite ((PTR
) buf
, thisread
, abfd
) != thisread
)
415 /* Now write header information. */
416 if (bfd_mach_o_write_header (abfd
, &mdata
->header
) != 0)
419 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
421 unsigned char buf
[8];
422 bfd_mach_o_load_command
*cur
= &mdata
->commands
[i
];
423 unsigned long typeflag
;
425 typeflag
= cur
->type_required
? cur
->type
& BFD_MACH_O_LC_REQ_DYLD
: cur
->type
;
427 bfd_h_put_32 (abfd
, typeflag
, buf
);
428 bfd_h_put_32 (abfd
, cur
->len
, buf
+ 4);
430 bfd_seek (abfd
, cur
->offset
, SEEK_SET
);
431 if (bfd_bwrite ((PTR
) buf
, 8, abfd
) != 8)
436 case BFD_MACH_O_LC_SEGMENT
:
437 if (bfd_mach_o_scan_write_segment (abfd
, cur
) != 0)
440 case BFD_MACH_O_LC_SYMTAB
:
441 if (bfd_mach_o_scan_write_symtab (abfd
, cur
) != 0)
444 case BFD_MACH_O_LC_SYMSEG
:
446 case BFD_MACH_O_LC_THREAD
:
447 case BFD_MACH_O_LC_UNIXTHREAD
:
448 if (bfd_mach_o_scan_write_thread (abfd
, cur
) != 0)
451 case BFD_MACH_O_LC_LOADFVMLIB
:
452 case BFD_MACH_O_LC_IDFVMLIB
:
453 case BFD_MACH_O_LC_IDENT
:
454 case BFD_MACH_O_LC_FVMFILE
:
455 case BFD_MACH_O_LC_PREPAGE
:
456 case BFD_MACH_O_LC_DYSYMTAB
:
457 case BFD_MACH_O_LC_LOAD_DYLIB
:
458 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB
:
459 case BFD_MACH_O_LC_ID_DYLIB
:
460 case BFD_MACH_O_LC_LOAD_DYLINKER
:
461 case BFD_MACH_O_LC_ID_DYLINKER
:
462 case BFD_MACH_O_LC_PREBOUND_DYLIB
:
463 case BFD_MACH_O_LC_ROUTINES
:
464 case BFD_MACH_O_LC_SUB_FRAMEWORK
:
468 "unable to write unknown load command 0x%lx\n",
478 bfd_mach_o_sizeof_headers (a
, b
)
479 bfd
*a ATTRIBUTE_UNUSED
;
480 bfd_boolean b ATTRIBUTE_UNUSED
;
485 /* Make an empty symbol. This is required only because
486 bfd_make_section_anyway wants to create a symbol for the section. */
489 bfd_mach_o_make_empty_symbol (abfd
)
494 new = (asymbol
*) bfd_zalloc (abfd
, sizeof (asymbol
));
502 bfd_mach_o_write_header (abfd
, header
)
504 bfd_mach_o_header
*header
;
506 unsigned char buf
[28];
508 bfd_h_put_32 (abfd
, header
->magic
, buf
+ 0);
509 bfd_h_put_32 (abfd
, header
->cputype
, buf
+ 4);
510 bfd_h_put_32 (abfd
, header
->cpusubtype
, buf
+ 8);
511 bfd_h_put_32 (abfd
, header
->filetype
, buf
+ 12);
512 bfd_h_put_32 (abfd
, header
->ncmds
, buf
+ 16);
513 bfd_h_put_32 (abfd
, header
->sizeofcmds
, buf
+ 20);
514 bfd_h_put_32 (abfd
, header
->flags
, buf
+ 24);
516 bfd_seek (abfd
, 0, SEEK_SET
);
517 if (bfd_bwrite ((PTR
) buf
, 28, abfd
) != 28)
524 bfd_mach_o_read_header (abfd
, header
)
526 bfd_mach_o_header
*header
;
528 unsigned char buf
[28];
529 bfd_vma (*get32
) (const void *) = NULL
;
531 bfd_seek (abfd
, 0, SEEK_SET
);
533 if (bfd_bread ((PTR
) buf
, 28, abfd
) != 28)
536 if (bfd_getb32 (buf
) == 0xfeedface)
538 header
->byteorder
= BFD_ENDIAN_BIG
;
539 header
->magic
= 0xfeedface;
542 else if (bfd_getl32 (buf
) == 0xfeedface)
544 header
->byteorder
= BFD_ENDIAN_LITTLE
;
545 header
->magic
= 0xfeedface;
550 header
->byteorder
= BFD_ENDIAN_UNKNOWN
;
554 header
->cputype
= (*get32
) (buf
+ 4);
555 header
->cpusubtype
= (*get32
) (buf
+ 8);
556 header
->filetype
= (*get32
) (buf
+ 12);
557 header
->ncmds
= (*get32
) (buf
+ 16);
558 header
->sizeofcmds
= (*get32
) (buf
+ 20);
559 header
->flags
= (*get32
) (buf
+ 24);
565 bfd_mach_o_make_bfd_section (abfd
, section
)
567 bfd_mach_o_section
*section
;
571 const char *prefix
= "LC_SEGMENT";
572 unsigned int snamelen
;
574 snamelen
= strlen (prefix
) + 1
575 + strlen (section
->segname
) + 1
576 + strlen (section
->sectname
) + 1;
578 sname
= (char *) bfd_alloc (abfd
, snamelen
);
581 sprintf (sname
, "%s.%s.%s", prefix
, section
->segname
, section
->sectname
);
583 bfdsec
= bfd_make_section_anyway (abfd
, sname
);
587 bfdsec
->vma
= section
->addr
;
588 bfdsec
->lma
= section
->addr
;
589 bfdsec
->size
= section
->size
;
590 bfdsec
->filepos
= section
->offset
;
591 bfdsec
->alignment_power
= section
->align
;
593 if (section
->flags
& BFD_MACH_O_S_ZEROFILL
)
594 bfdsec
->flags
= SEC_ALLOC
;
596 bfdsec
->flags
= SEC_HAS_CONTENTS
| SEC_LOAD
| SEC_ALLOC
| SEC_CODE
;
602 bfd_mach_o_scan_read_section (abfd
, section
, offset
)
604 bfd_mach_o_section
*section
;
607 unsigned char buf
[68];
609 bfd_seek (abfd
, offset
, SEEK_SET
);
610 if (bfd_bread ((PTR
) buf
, 68, abfd
) != 68)
613 memcpy (section
->sectname
, buf
, 16);
614 section
->sectname
[16] = '\0';
615 memcpy (section
->segname
, buf
+ 16, 16);
616 section
->segname
[16] = '\0';
617 section
->addr
= bfd_h_get_32 (abfd
, buf
+ 32);
618 section
->size
= bfd_h_get_32 (abfd
, buf
+ 36);
619 section
->offset
= bfd_h_get_32 (abfd
, buf
+ 40);
620 section
->align
= bfd_h_get_32 (abfd
, buf
+ 44);
621 section
->reloff
= bfd_h_get_32 (abfd
, buf
+ 48);
622 section
->nreloc
= bfd_h_get_32 (abfd
, buf
+ 52);
623 section
->flags
= bfd_h_get_32 (abfd
, buf
+ 56);
624 section
->reserved1
= bfd_h_get_32 (abfd
, buf
+ 60);
625 section
->reserved2
= bfd_h_get_32 (abfd
, buf
+ 64);
626 section
->bfdsection
= bfd_mach_o_make_bfd_section (abfd
, section
);
628 if (section
->bfdsection
== NULL
)
635 bfd_mach_o_scan_write_section (abfd
, section
, offset
)
637 bfd_mach_o_section
*section
;
640 unsigned char buf
[68];
642 memcpy (buf
, section
->sectname
, 16);
643 memcpy (buf
+ 16, section
->segname
, 16);
644 bfd_h_put_32 (abfd
, section
->addr
, buf
+ 32);
645 bfd_h_put_32 (abfd
, section
->size
, buf
+ 36);
646 bfd_h_put_32 (abfd
, section
->offset
, buf
+ 40);
647 bfd_h_put_32 (abfd
, section
->align
, buf
+ 44);
648 bfd_h_put_32 (abfd
, section
->reloff
, buf
+ 48);
649 bfd_h_put_32 (abfd
, section
->nreloc
, buf
+ 52);
650 bfd_h_put_32 (abfd
, section
->flags
, buf
+ 56);
651 /* bfd_h_put_32 (abfd, section->reserved1, buf + 60); */
652 /* bfd_h_put_32 (abfd, section->reserved2, buf + 64); */
654 bfd_seek (abfd
, offset
, SEEK_SET
);
655 if (bfd_bwrite ((PTR
) buf
, 68, abfd
) != 68)
662 bfd_mach_o_scan_write_symtab_symbols (abfd
, command
)
664 bfd_mach_o_load_command
*command
;
666 bfd_mach_o_symtab_command
*sym
= &command
->command
.symtab
;
670 for (i
= 0; i
< sym
->nsyms
; i
++)
672 unsigned char buf
[12];
673 bfd_vma symoff
= sym
->symoff
+ (i
* 12);
674 unsigned char ntype
= 0;
675 unsigned char nsect
= 0;
678 s
= &sym
->symbols
[i
];
680 /* Don't set this from the symbol information; use stored values. */
682 if (s
->flags
& BSF_GLOBAL
)
684 if (s
->flags
& BSF_DEBUGGING
)
687 if (s
->section
== bfd_und_section_ptr
)
689 else if (s
->section
== bfd_abs_section_ptr
)
695 /* Instead just set from the stored values. */
696 ntype
= (s
->udata
.i
>> 24) & 0xff;
697 nsect
= (s
->udata
.i
>> 16) & 0xff;
698 ndesc
= s
->udata
.i
& 0xffff;
700 bfd_h_put_32 (abfd
, s
->name
- sym
->strtab
, buf
);
701 bfd_h_put_8 (abfd
, ntype
, buf
+ 4);
702 bfd_h_put_8 (abfd
, nsect
, buf
+ 5);
703 bfd_h_put_16 (abfd
, ndesc
, buf
+ 6);
704 bfd_h_put_32 (abfd
, s
->section
->vma
+ s
->value
, buf
+ 8);
706 bfd_seek (abfd
, symoff
, SEEK_SET
);
707 if (bfd_bwrite ((PTR
) buf
, 12, abfd
) != 12)
709 fprintf (stderr
, "bfd_mach_o_scan_write_symtab_symbols: unable to write %d bytes at %lu\n",
710 12, (unsigned long) symoff
);
719 bfd_mach_o_scan_read_symtab_symbol (abfd
, sym
, s
, i
)
721 bfd_mach_o_symtab_command
*sym
;
725 bfd_mach_o_data_struct
*mdata
= abfd
->tdata
.mach_o_data
;
726 bfd_vma symoff
= sym
->symoff
+ (i
* 12);
727 unsigned char buf
[12];
728 unsigned char type
= -1;
729 unsigned char section
= -1;
731 unsigned long value
= -1;
732 unsigned long stroff
= -1;
733 unsigned int symtype
= -1;
735 BFD_ASSERT (sym
->strtab
!= NULL
);
737 bfd_seek (abfd
, symoff
, SEEK_SET
);
738 if (bfd_bread ((PTR
) buf
, 12, abfd
) != 12)
740 fprintf (stderr
, "bfd_mach_o_scan_read_symtab_symbol: unable to read %d bytes at %lu\n",
741 12, (unsigned long) symoff
);
745 stroff
= bfd_h_get_32 (abfd
, buf
);
746 type
= bfd_h_get_8 (abfd
, buf
+ 4);
747 symtype
= (type
& 0x0e);
748 section
= bfd_h_get_8 (abfd
, buf
+ 5) - 1;
749 desc
= bfd_h_get_16 (abfd
, buf
+ 6);
750 value
= bfd_h_get_32 (abfd
, buf
+ 8);
752 if (stroff
>= sym
->strsize
)
754 fprintf (stderr
, "bfd_mach_o_scan_read_symtab_symbol: symbol name out of range (%lu >= %lu)\n",
755 (unsigned long) stroff
, (unsigned long) sym
->strsize
);
760 s
->name
= sym
->strtab
+ stroff
;
762 s
->udata
.i
= (type
<< 24) | (section
<< 16) | desc
;
765 if (type
& BFD_MACH_O_N_STAB
)
767 s
->flags
|= BSF_DEBUGGING
;
768 s
->section
= bfd_und_section_ptr
;
772 if (type
& BFD_MACH_O_N_PEXT
)
774 type
&= ~BFD_MACH_O_N_PEXT
;
775 s
->flags
|= BSF_GLOBAL
;
778 if (type
& BFD_MACH_O_N_EXT
)
780 type
&= ~BFD_MACH_O_N_EXT
;
781 s
->flags
|= BSF_GLOBAL
;
786 case BFD_MACH_O_N_UNDF
:
787 s
->section
= bfd_und_section_ptr
;
789 case BFD_MACH_O_N_PBUD
:
790 s
->section
= bfd_und_section_ptr
;
792 case BFD_MACH_O_N_ABS
:
793 s
->section
= bfd_abs_section_ptr
;
795 case BFD_MACH_O_N_SECT
:
796 if ((section
> 0) && (section
<= mdata
->nsects
))
798 s
->section
= mdata
->sections
[section
- 1]->bfdsection
;
799 s
->value
= s
->value
- mdata
->sections
[section
- 1]->addr
;
803 /* Mach-O uses 0 to mean "no section"; not an error. */
806 fprintf (stderr
, "bfd_mach_o_scan_read_symtab_symbol: "
807 "symbol \"%s\" specified invalid section %d (max %lu): setting to undefined\n",
808 s
->name
, section
, mdata
->nsects
);
810 s
->section
= bfd_und_section_ptr
;
813 case BFD_MACH_O_N_INDR
:
814 fprintf (stderr
, "bfd_mach_o_scan_read_symtab_symbol: "
815 "symbol \"%s\" is unsupported 'indirect' reference: setting to undefined\n",
817 s
->section
= bfd_und_section_ptr
;
820 fprintf (stderr
, "bfd_mach_o_scan_read_symtab_symbol: "
821 "symbol \"%s\" specified invalid type field 0x%x: setting to undefined\n",
823 s
->section
= bfd_und_section_ptr
;
832 bfd_mach_o_scan_read_symtab_strtab (abfd
, sym
)
834 bfd_mach_o_symtab_command
*sym
;
836 BFD_ASSERT (sym
->strtab
== NULL
);
838 if (abfd
->flags
& BFD_IN_MEMORY
)
840 struct bfd_in_memory
*b
;
842 b
= (struct bfd_in_memory
*) abfd
->iostream
;
844 if ((sym
->stroff
+ sym
->strsize
) > b
->size
)
846 bfd_set_error (bfd_error_file_truncated
);
849 sym
->strtab
= b
->buffer
+ sym
->stroff
;
853 sym
->strtab
= bfd_alloc (abfd
, sym
->strsize
);
854 if (sym
->strtab
== NULL
)
857 bfd_seek (abfd
, sym
->stroff
, SEEK_SET
);
858 if (bfd_bread ((PTR
) sym
->strtab
, sym
->strsize
, abfd
) != sym
->strsize
)
860 fprintf (stderr
, "bfd_mach_o_scan_read_symtab_strtab: unable to read %lu bytes at %lu\n",
861 sym
->strsize
, sym
->stroff
);
869 bfd_mach_o_scan_read_symtab_symbols (abfd
, sym
)
871 bfd_mach_o_symtab_command
*sym
;
876 BFD_ASSERT (sym
->symbols
== NULL
);
877 sym
->symbols
= bfd_alloc (abfd
, sym
->nsyms
* sizeof (asymbol
));
879 if (sym
->symbols
== NULL
)
881 fprintf (stderr
, "bfd_mach_o_scan_read_symtab_symbols: unable to allocate memory for symbols\n");
885 ret
= bfd_mach_o_scan_read_symtab_strtab (abfd
, sym
);
889 for (i
= 0; i
< sym
->nsyms
; i
++)
891 ret
= bfd_mach_o_scan_read_symtab_symbol (abfd
, sym
, &sym
->symbols
[i
], i
);
900 bfd_mach_o_scan_read_dysymtab_symbol (abfd
, dysym
, sym
, s
, i
)
902 bfd_mach_o_dysymtab_command
*dysym
;
903 bfd_mach_o_symtab_command
*sym
;
907 unsigned long isymoff
= dysym
->indirectsymoff
+ (i
* 4);
908 unsigned long symindex
;
909 unsigned char buf
[4];
911 BFD_ASSERT (i
< dysym
->nindirectsyms
);
913 bfd_seek (abfd
, isymoff
, SEEK_SET
);
914 if (bfd_bread ((PTR
) buf
, 4, abfd
) != 4)
916 fprintf (stderr
, "bfd_mach_o_scan_read_dysymtab_symbol: unable to read %lu bytes at %lu\n",
917 (unsigned long) 4, isymoff
);
920 symindex
= bfd_h_get_32 (abfd
, buf
);
922 return bfd_mach_o_scan_read_symtab_symbol (abfd
, sym
, s
, symindex
);
926 bfd_mach_o_i386_flavour_string (flavour
)
927 unsigned int flavour
;
929 switch ((int) flavour
)
931 case BFD_MACH_O_i386_NEW_THREAD_STATE
: return "i386_NEW_THREAD_STATE";
932 case BFD_MACH_O_i386_FLOAT_STATE
: return "i386_FLOAT_STATE";
933 case BFD_MACH_O_i386_ISA_PORT_MAP_STATE
: return "i386_ISA_PORT_MAP_STATE";
934 case BFD_MACH_O_i386_V86_ASSIST_STATE
: return "i386_V86_ASSIST_STATE";
935 case BFD_MACH_O_i386_REGS_SEGS_STATE
: return "i386_REGS_SEGS_STATE";
936 case BFD_MACH_O_i386_THREAD_SYSCALL_STATE
: return "i386_THREAD_SYSCALL_STATE";
937 case BFD_MACH_O_i386_THREAD_STATE_NONE
: return "i386_THREAD_STATE_NONE";
938 case BFD_MACH_O_i386_SAVED_STATE
: return "i386_SAVED_STATE";
939 case BFD_MACH_O_i386_THREAD_STATE
: return "i386_THREAD_STATE";
940 case BFD_MACH_O_i386_THREAD_FPSTATE
: return "i386_THREAD_FPSTATE";
941 case BFD_MACH_O_i386_THREAD_EXCEPTSTATE
: return "i386_THREAD_EXCEPTSTATE";
942 case BFD_MACH_O_i386_THREAD_CTHREADSTATE
: return "i386_THREAD_CTHREADSTATE";
943 default: return "UNKNOWN";
948 bfd_mach_o_ppc_flavour_string (flavour
)
949 unsigned int flavour
;
951 switch ((int) flavour
)
953 case BFD_MACH_O_PPC_THREAD_STATE
: return "PPC_THREAD_STATE";
954 case BFD_MACH_O_PPC_FLOAT_STATE
: return "PPC_FLOAT_STATE";
955 case BFD_MACH_O_PPC_EXCEPTION_STATE
: return "PPC_EXCEPTION_STATE";
956 case BFD_MACH_O_PPC_VECTOR_STATE
: return "PPC_VECTOR_STATE";
957 default: return "UNKNOWN";
962 bfd_mach_o_scan_write_thread (abfd
, command
)
964 bfd_mach_o_load_command
*command
;
966 bfd_mach_o_thread_command
*cmd
= &command
->command
.thread
;
968 unsigned char buf
[8];
970 unsigned int nflavours
;
972 BFD_ASSERT ((command
->type
== BFD_MACH_O_LC_THREAD
)
973 || (command
->type
== BFD_MACH_O_LC_UNIXTHREAD
));
977 for (i
= 0; i
< cmd
->nflavours
; i
++)
979 BFD_ASSERT ((cmd
->flavours
[i
].size
% 4) == 0);
980 BFD_ASSERT (cmd
->flavours
[i
].offset
== (command
->offset
+ offset
+ 8));
982 bfd_h_put_32 (abfd
, cmd
->flavours
[i
].flavour
, buf
);
983 bfd_h_put_32 (abfd
, (cmd
->flavours
[i
].size
/ 4), buf
+ 4);
985 bfd_seek (abfd
, command
->offset
+ offset
, SEEK_SET
);
986 if (bfd_bwrite ((PTR
) buf
, 8, abfd
) != 8)
989 offset
+= cmd
->flavours
[i
].size
+ 8;
996 bfd_mach_o_scan_read_dylinker (abfd
, command
)
998 bfd_mach_o_load_command
*command
;
1000 bfd_mach_o_dylinker_command
*cmd
= &command
->command
.dylinker
;
1001 unsigned char buf
[4];
1002 unsigned int nameoff
;
1007 BFD_ASSERT ((command
->type
== BFD_MACH_O_LC_ID_DYLINKER
)
1008 || (command
->type
== BFD_MACH_O_LC_LOAD_DYLINKER
));
1010 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
1011 if (bfd_bread ((PTR
) buf
, 4, abfd
) != 4)
1014 nameoff
= bfd_h_get_32 (abfd
, buf
+ 0);
1016 cmd
->name_offset
= command
->offset
+ nameoff
;
1017 cmd
->name_len
= command
->len
- nameoff
;
1019 if (command
->type
== BFD_MACH_O_LC_LOAD_DYLINKER
)
1020 prefix
= "LC_LOAD_DYLINKER";
1021 else if (command
->type
== BFD_MACH_O_LC_ID_DYLINKER
)
1022 prefix
= "LC_ID_DYLINKER";
1026 sname
= (char *) bfd_alloc (abfd
, strlen (prefix
) + 1);
1029 strcpy (sname
, prefix
);
1031 bfdsec
= bfd_make_section_anyway (abfd
, sname
);
1037 bfdsec
->size
= command
->len
- 8;
1038 bfdsec
->filepos
= command
->offset
+ 8;
1039 bfdsec
->alignment_power
= 0;
1040 bfdsec
->flags
= SEC_HAS_CONTENTS
;
1042 cmd
->section
= bfdsec
;
1048 bfd_mach_o_scan_read_dylib (abfd
, command
)
1050 bfd_mach_o_load_command
*command
;
1052 bfd_mach_o_dylib_command
*cmd
= &command
->command
.dylib
;
1053 unsigned char buf
[16];
1054 unsigned int nameoff
;
1059 BFD_ASSERT ((command
->type
== BFD_MACH_O_LC_ID_DYLIB
)
1060 || (command
->type
== BFD_MACH_O_LC_LOAD_DYLIB
)
1061 || (command
->type
== BFD_MACH_O_LC_LOAD_WEAK_DYLIB
));
1063 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
1064 if (bfd_bread ((PTR
) buf
, 16, abfd
) != 16)
1067 nameoff
= bfd_h_get_32 (abfd
, buf
+ 0);
1068 cmd
->timestamp
= bfd_h_get_32 (abfd
, buf
+ 4);
1069 cmd
->current_version
= bfd_h_get_32 (abfd
, buf
+ 8);
1070 cmd
->compatibility_version
= bfd_h_get_32 (abfd
, buf
+ 12);
1072 cmd
->name_offset
= command
->offset
+ nameoff
;
1073 cmd
->name_len
= command
->len
- nameoff
;
1075 if (command
->type
== BFD_MACH_O_LC_LOAD_DYLIB
)
1076 prefix
= "LC_LOAD_DYLIB";
1077 else if (command
->type
== BFD_MACH_O_LC_LOAD_WEAK_DYLIB
)
1078 prefix
= "LC_LOAD_WEAK_DYLIB";
1079 else if (command
->type
== BFD_MACH_O_LC_ID_DYLIB
)
1080 prefix
= "LC_ID_DYLIB";
1084 sname
= (char *) bfd_alloc (abfd
, strlen (prefix
) + 1);
1087 strcpy (sname
, prefix
);
1089 bfdsec
= bfd_make_section_anyway (abfd
, sname
);
1095 bfdsec
->size
= command
->len
- 8;
1096 bfdsec
->filepos
= command
->offset
+ 8;
1097 bfdsec
->alignment_power
= 0;
1098 bfdsec
->flags
= SEC_HAS_CONTENTS
;
1100 cmd
->section
= bfdsec
;
1106 bfd_mach_o_scan_read_prebound_dylib (abfd
, command
)
1107 bfd
*abfd ATTRIBUTE_UNUSED
;
1108 bfd_mach_o_load_command
*command ATTRIBUTE_UNUSED
;
1110 /* bfd_mach_o_prebound_dylib_command *cmd = &command->command.prebound_dylib; */
1112 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_PREBOUND_DYLIB
);
1117 bfd_mach_o_scan_read_thread (abfd
, command
)
1119 bfd_mach_o_load_command
*command
;
1121 bfd_mach_o_data_struct
*mdata
= NULL
;
1122 bfd_mach_o_thread_command
*cmd
= &command
->command
.thread
;
1123 unsigned char buf
[8];
1125 unsigned int nflavours
;
1128 BFD_ASSERT ((command
->type
== BFD_MACH_O_LC_THREAD
)
1129 || (command
->type
== BFD_MACH_O_LC_UNIXTHREAD
));
1131 BFD_ASSERT (bfd_mach_o_valid (abfd
));
1132 mdata
= abfd
->tdata
.mach_o_data
;
1136 while (offset
!= command
->len
)
1138 if (offset
>= command
->len
)
1141 bfd_seek (abfd
, command
->offset
+ offset
, SEEK_SET
);
1143 if (bfd_bread ((PTR
) buf
, 8, abfd
) != 8)
1146 offset
+= 8 + bfd_h_get_32 (abfd
, buf
+ 4) * 4;
1151 ((bfd_mach_o_thread_flavour
*)
1152 bfd_alloc (abfd
, nflavours
* sizeof (bfd_mach_o_thread_flavour
)));
1153 if (cmd
->flavours
== NULL
)
1155 cmd
->nflavours
= nflavours
;
1159 while (offset
!= command
->len
)
1161 if (offset
>= command
->len
)
1164 if (nflavours
>= cmd
->nflavours
)
1167 bfd_seek (abfd
, command
->offset
+ offset
, SEEK_SET
);
1169 if (bfd_bread ((PTR
) buf
, 8, abfd
) != 8)
1172 cmd
->flavours
[nflavours
].flavour
= bfd_h_get_32 (abfd
, buf
);
1173 cmd
->flavours
[nflavours
].offset
= command
->offset
+ offset
+ 8;
1174 cmd
->flavours
[nflavours
].size
= bfd_h_get_32 (abfd
, buf
+ 4) * 4;
1175 offset
+= cmd
->flavours
[nflavours
].size
+ 8;
1179 for (i
= 0; i
< nflavours
; i
++)
1182 unsigned int snamelen
;
1184 const char *flavourstr
;
1185 const char *prefix
= "LC_THREAD";
1188 switch (mdata
->header
.cputype
)
1190 case BFD_MACH_O_CPU_TYPE_POWERPC
:
1191 flavourstr
= bfd_mach_o_ppc_flavour_string (cmd
->flavours
[i
].flavour
);
1193 case BFD_MACH_O_CPU_TYPE_I386
:
1194 flavourstr
= bfd_mach_o_i386_flavour_string (cmd
->flavours
[i
].flavour
);
1197 flavourstr
= "UNKNOWN_ARCHITECTURE";
1201 snamelen
= strlen (prefix
) + 1 + 20 + 1 + strlen (flavourstr
) + 1;
1202 sname
= (char *) bfd_alloc (abfd
, snamelen
);
1208 sprintf (sname
, "%s.%s.%u", prefix
, flavourstr
, j
);
1209 if (bfd_get_section_by_name (abfd
, sname
) == NULL
)
1214 bfdsec
= bfd_make_section (abfd
, sname
);
1218 bfdsec
->size
= cmd
->flavours
[i
].size
;
1219 bfdsec
->filepos
= cmd
->flavours
[i
].offset
;
1220 bfdsec
->alignment_power
= 0x0;
1221 bfdsec
->flags
= SEC_HAS_CONTENTS
;
1223 cmd
->section
= bfdsec
;
1230 bfd_mach_o_scan_write_symtab (abfd
, command
)
1232 bfd_mach_o_load_command
*command
;
1234 bfd_mach_o_symtab_command
*seg
= &command
->command
.symtab
;
1235 unsigned char buf
[16];
1237 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SYMTAB
);
1239 bfd_h_put_32 (abfd
, seg
->symoff
, buf
);
1240 bfd_h_put_32 (abfd
, seg
->nsyms
, buf
+ 4);
1241 bfd_h_put_32 (abfd
, seg
->stroff
, buf
+ 8);
1242 bfd_h_put_32 (abfd
, seg
->strsize
, buf
+ 12);
1244 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
1245 if (bfd_bwrite ((PTR
) buf
, 16, abfd
) != 16)
1248 if (bfd_mach_o_scan_write_symtab_symbols (abfd
, command
) != 0)
1255 bfd_mach_o_scan_read_dysymtab (abfd
, command
)
1257 bfd_mach_o_load_command
*command
;
1259 bfd_mach_o_dysymtab_command
*seg
= &command
->command
.dysymtab
;
1260 unsigned char buf
[72];
1262 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_DYSYMTAB
);
1264 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
1265 if (bfd_bread ((PTR
) buf
, 72, abfd
) != 72)
1268 seg
->ilocalsym
= bfd_h_get_32 (abfd
, buf
+ 0);
1269 seg
->nlocalsym
= bfd_h_get_32 (abfd
, buf
+ 4);
1270 seg
->iextdefsym
= bfd_h_get_32 (abfd
, buf
+ 8);
1271 seg
->nextdefsym
= bfd_h_get_32 (abfd
, buf
+ 12);
1272 seg
->iundefsym
= bfd_h_get_32 (abfd
, buf
+ 16);
1273 seg
->nundefsym
= bfd_h_get_32 (abfd
, buf
+ 20);
1274 seg
->tocoff
= bfd_h_get_32 (abfd
, buf
+ 24);
1275 seg
->ntoc
= bfd_h_get_32 (abfd
, buf
+ 28);
1276 seg
->modtaboff
= bfd_h_get_32 (abfd
, buf
+ 32);
1277 seg
->nmodtab
= bfd_h_get_32 (abfd
, buf
+ 36);
1278 seg
->extrefsymoff
= bfd_h_get_32 (abfd
, buf
+ 40);
1279 seg
->nextrefsyms
= bfd_h_get_32 (abfd
, buf
+ 44);
1280 seg
->indirectsymoff
= bfd_h_get_32 (abfd
, buf
+ 48);
1281 seg
->nindirectsyms
= bfd_h_get_32 (abfd
, buf
+ 52);
1282 seg
->extreloff
= bfd_h_get_32 (abfd
, buf
+ 56);
1283 seg
->nextrel
= bfd_h_get_32 (abfd
, buf
+ 60);
1284 seg
->locreloff
= bfd_h_get_32 (abfd
, buf
+ 64);
1285 seg
->nlocrel
= bfd_h_get_32 (abfd
, buf
+ 68);
1291 bfd_mach_o_scan_read_symtab (abfd
, command
)
1293 bfd_mach_o_load_command
*command
;
1295 bfd_mach_o_symtab_command
*seg
= &command
->command
.symtab
;
1296 unsigned char buf
[16];
1299 const char *prefix
= "LC_SYMTAB.stabs";
1301 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SYMTAB
);
1303 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
1304 if (bfd_bread ((PTR
) buf
, 16, abfd
) != 16)
1307 seg
->symoff
= bfd_h_get_32 (abfd
, buf
);
1308 seg
->nsyms
= bfd_h_get_32 (abfd
, buf
+ 4);
1309 seg
->stroff
= bfd_h_get_32 (abfd
, buf
+ 8);
1310 seg
->strsize
= bfd_h_get_32 (abfd
, buf
+ 12);
1311 seg
->symbols
= NULL
;
1314 sname
= (char *) bfd_alloc (abfd
, strlen (prefix
) + 1);
1317 strcpy (sname
, prefix
);
1319 bfdsec
= bfd_make_section_anyway (abfd
, sname
);
1325 bfdsec
->size
= seg
->nsyms
* 12;
1326 bfdsec
->filepos
= seg
->symoff
;
1327 bfdsec
->alignment_power
= 0;
1328 bfdsec
->flags
= SEC_HAS_CONTENTS
;
1330 seg
->stabs_segment
= bfdsec
;
1332 prefix
= "LC_SYMTAB.stabstr";
1333 sname
= (char *) bfd_alloc (abfd
, strlen (prefix
) + 1);
1336 strcpy (sname
, prefix
);
1338 bfdsec
= bfd_make_section_anyway (abfd
, sname
);
1344 bfdsec
->size
= seg
->strsize
;
1345 bfdsec
->filepos
= seg
->stroff
;
1346 bfdsec
->alignment_power
= 0;
1347 bfdsec
->flags
= SEC_HAS_CONTENTS
;
1349 seg
->stabstr_segment
= bfdsec
;
1355 bfd_mach_o_scan_read_segment (abfd
, command
)
1357 bfd_mach_o_load_command
*command
;
1359 unsigned char buf
[48];
1360 bfd_mach_o_segment_command
*seg
= &command
->command
.segment
;
1364 const char *prefix
= "LC_SEGMENT";
1365 unsigned int snamelen
;
1367 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SEGMENT
);
1369 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
1370 if (bfd_bread ((PTR
) buf
, 48, abfd
) != 48)
1373 memcpy (seg
->segname
, buf
, 16);
1374 seg
->vmaddr
= bfd_h_get_32 (abfd
, buf
+ 16);
1375 seg
->vmsize
= bfd_h_get_32 (abfd
, buf
+ 20);
1376 seg
->fileoff
= bfd_h_get_32 (abfd
, buf
+ 24);
1377 seg
->filesize
= bfd_h_get_32 (abfd
, buf
+ 28);
1378 /* seg->maxprot = bfd_h_get_32 (abfd, buf + 32); */
1379 /* seg->initprot = bfd_h_get_32 (abfd, buf + 36); */
1380 seg
->nsects
= bfd_h_get_32 (abfd
, buf
+ 40);
1381 seg
->flags
= bfd_h_get_32 (abfd
, buf
+ 44);
1383 snamelen
= strlen (prefix
) + 1 + strlen (seg
->segname
) + 1;
1384 sname
= (char *) bfd_alloc (abfd
, snamelen
);
1387 sprintf (sname
, "%s.%s", prefix
, seg
->segname
);
1389 bfdsec
= bfd_make_section_anyway (abfd
, sname
);
1393 bfdsec
->vma
= seg
->vmaddr
;
1394 bfdsec
->lma
= seg
->vmaddr
;
1395 bfdsec
->size
= seg
->filesize
;
1396 bfdsec
->filepos
= seg
->fileoff
;
1397 bfdsec
->alignment_power
= 0x0;
1398 bfdsec
->flags
= SEC_HAS_CONTENTS
| SEC_LOAD
| SEC_ALLOC
| SEC_CODE
;
1400 seg
->segment
= bfdsec
;
1402 if (seg
->nsects
!= 0)
1405 ((bfd_mach_o_section
*)
1406 bfd_alloc (abfd
, seg
->nsects
* sizeof (bfd_mach_o_section
)));
1407 if (seg
->sections
== NULL
)
1410 for (i
= 0; i
< seg
->nsects
; i
++)
1412 bfd_vma segoff
= command
->offset
+ 48 + 8 + (i
* 68);
1414 if (bfd_mach_o_scan_read_section (abfd
, &seg
->sections
[i
],
1424 bfd_mach_o_scan_write_segment (abfd
, command
)
1426 bfd_mach_o_load_command
*command
;
1428 unsigned char buf
[48];
1429 bfd_mach_o_segment_command
*seg
= &command
->command
.segment
;
1432 BFD_ASSERT (command
->type
== BFD_MACH_O_LC_SEGMENT
);
1434 memcpy (buf
, seg
->segname
, 16);
1435 bfd_h_put_32 (abfd
, seg
->vmaddr
, buf
+ 16);
1436 bfd_h_put_32 (abfd
, seg
->vmsize
, buf
+ 20);
1437 bfd_h_put_32 (abfd
, seg
->fileoff
, buf
+ 24);
1438 bfd_h_put_32 (abfd
, seg
->filesize
, buf
+ 28);
1439 bfd_h_put_32 (abfd
, 0 /* seg->maxprot */, buf
+ 32);
1440 bfd_h_put_32 (abfd
, 0 /* seg->initprot */, buf
+ 36);
1441 bfd_h_put_32 (abfd
, seg
->nsects
, buf
+ 40);
1442 bfd_h_put_32 (abfd
, seg
->flags
, buf
+ 44);
1444 bfd_seek (abfd
, command
->offset
+ 8, SEEK_SET
);
1445 if (bfd_bwrite ((PTR
) buf
, 48, abfd
) != 48)
1450 bfd_vma nbytes
= seg
->filesize
;
1451 bfd_vma curoff
= seg
->fileoff
;
1455 bfd_vma thisread
= nbytes
;
1457 if (thisread
> 1024)
1460 bfd_seek (abfd
, curoff
, SEEK_SET
);
1461 if (bfd_bread ((PTR
) buf
, thisread
, abfd
) != thisread
)
1464 bfd_seek (abfd
, curoff
, SEEK_SET
);
1465 if (bfd_bwrite ((PTR
) buf
, thisread
, abfd
) != thisread
)
1473 for (i
= 0; i
< seg
->nsects
; i
++)
1475 bfd_vma segoff
= command
->offset
+ 48 + 8 + (i
* 68);
1477 if (bfd_mach_o_scan_write_section (abfd
, &seg
->sections
[i
], segoff
) != 0)
1485 bfd_mach_o_scan_read_command (abfd
, command
)
1487 bfd_mach_o_load_command
*command
;
1489 unsigned char buf
[8];
1491 bfd_seek (abfd
, command
->offset
, SEEK_SET
);
1492 if (bfd_bread ((PTR
) buf
, 8, abfd
) != 8)
1495 command
->type
= (bfd_h_get_32 (abfd
, buf
) & ~BFD_MACH_O_LC_REQ_DYLD
);
1496 command
->type_required
= (bfd_h_get_32 (abfd
, buf
) & BFD_MACH_O_LC_REQ_DYLD
1498 command
->len
= bfd_h_get_32 (abfd
, buf
+ 4);
1500 switch (command
->type
)
1502 case BFD_MACH_O_LC_SEGMENT
:
1503 if (bfd_mach_o_scan_read_segment (abfd
, command
) != 0)
1506 case BFD_MACH_O_LC_SYMTAB
:
1507 if (bfd_mach_o_scan_read_symtab (abfd
, command
) != 0)
1510 case BFD_MACH_O_LC_SYMSEG
:
1512 case BFD_MACH_O_LC_THREAD
:
1513 case BFD_MACH_O_LC_UNIXTHREAD
:
1514 if (bfd_mach_o_scan_read_thread (abfd
, command
) != 0)
1517 case BFD_MACH_O_LC_LOAD_DYLINKER
:
1518 case BFD_MACH_O_LC_ID_DYLINKER
:
1519 if (bfd_mach_o_scan_read_dylinker (abfd
, command
) != 0)
1522 case BFD_MACH_O_LC_LOAD_DYLIB
:
1523 case BFD_MACH_O_LC_ID_DYLIB
:
1524 case BFD_MACH_O_LC_LOAD_WEAK_DYLIB
:
1525 if (bfd_mach_o_scan_read_dylib (abfd
, command
) != 0)
1528 case BFD_MACH_O_LC_PREBOUND_DYLIB
:
1529 if (bfd_mach_o_scan_read_prebound_dylib (abfd
, command
) != 0)
1532 case BFD_MACH_O_LC_LOADFVMLIB
:
1533 case BFD_MACH_O_LC_IDFVMLIB
:
1534 case BFD_MACH_O_LC_IDENT
:
1535 case BFD_MACH_O_LC_FVMFILE
:
1536 case BFD_MACH_O_LC_PREPAGE
:
1537 case BFD_MACH_O_LC_ROUTINES
:
1538 case BFD_MACH_O_LC_SUB_FRAMEWORK
:
1540 case BFD_MACH_O_LC_DYSYMTAB
:
1541 if (bfd_mach_o_scan_read_dysymtab (abfd
, command
) != 0)
1544 case BFD_MACH_O_LC_SUB_UMBRELLA
:
1545 case BFD_MACH_O_LC_SUB_CLIENT
:
1546 case BFD_MACH_O_LC_SUB_LIBRARY
:
1547 case BFD_MACH_O_LC_TWOLEVEL_HINTS
:
1548 case BFD_MACH_O_LC_PREBIND_CKSUM
:
1551 fprintf (stderr
, "unable to read unknown load command 0x%lx\n",
1552 (unsigned long) command
->type
);
1560 bfd_mach_o_flatten_sections (abfd
)
1563 bfd_mach_o_data_struct
*mdata
= abfd
->tdata
.mach_o_data
;
1569 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
1571 if (mdata
->commands
[i
].type
== BFD_MACH_O_LC_SEGMENT
)
1573 bfd_mach_o_segment_command
*seg
;
1575 seg
= &mdata
->commands
[i
].command
.segment
;
1576 mdata
->nsects
+= seg
->nsects
;
1580 mdata
->sections
= bfd_alloc (abfd
,
1581 mdata
->nsects
* sizeof (bfd_mach_o_section
*));
1584 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
1586 if (mdata
->commands
[i
].type
== BFD_MACH_O_LC_SEGMENT
)
1588 bfd_mach_o_segment_command
*seg
;
1590 seg
= &mdata
->commands
[i
].command
.segment
;
1591 BFD_ASSERT (csect
+ seg
->nsects
<= mdata
->nsects
);
1593 for (j
= 0; j
< seg
->nsects
; j
++)
1594 mdata
->sections
[csect
++] = &seg
->sections
[j
];
1600 bfd_mach_o_scan_start_address (abfd
)
1603 bfd_mach_o_data_struct
*mdata
= abfd
->tdata
.mach_o_data
;
1604 bfd_mach_o_thread_command
*cmd
= NULL
;
1607 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
1609 if ((mdata
->commands
[i
].type
== BFD_MACH_O_LC_THREAD
) ||
1610 (mdata
->commands
[i
].type
== BFD_MACH_O_LC_UNIXTHREAD
))
1613 cmd
= &mdata
->commands
[i
].command
.thread
;
1622 for (i
= 0; i
< cmd
->nflavours
; i
++)
1624 if ((mdata
->header
.cputype
== BFD_MACH_O_CPU_TYPE_I386
)
1625 && (cmd
->flavours
[i
].flavour
1626 == (unsigned long) BFD_MACH_O_i386_THREAD_STATE
))
1628 unsigned char buf
[4];
1630 bfd_seek (abfd
, cmd
->flavours
[i
].offset
+ 40, SEEK_SET
);
1632 if (bfd_bread (buf
, 4, abfd
) != 4)
1635 abfd
->start_address
= bfd_h_get_32 (abfd
, buf
);
1637 else if ((mdata
->header
.cputype
== BFD_MACH_O_CPU_TYPE_POWERPC
)
1638 && (cmd
->flavours
[i
].flavour
== BFD_MACH_O_PPC_THREAD_STATE
))
1640 unsigned char buf
[4];
1642 bfd_seek (abfd
, cmd
->flavours
[i
].offset
+ 0, SEEK_SET
);
1644 if (bfd_bread (buf
, 4, abfd
) != 4)
1647 abfd
->start_address
= bfd_h_get_32 (abfd
, buf
);
1655 bfd_mach_o_scan (abfd
, header
, mdata
)
1657 bfd_mach_o_header
*header
;
1658 bfd_mach_o_data_struct
*mdata
;
1661 enum bfd_architecture cputype
;
1662 unsigned long cpusubtype
;
1664 mdata
->header
= *header
;
1665 mdata
->symbols
= NULL
;
1667 abfd
->flags
= (abfd
->xvec
->object_flags
1668 | (abfd
->flags
& (BFD_IN_MEMORY
| BFD_IO_FUNCS
)));
1669 abfd
->tdata
.mach_o_data
= mdata
;
1671 bfd_mach_o_convert_architecture (header
->cputype
, header
->cpusubtype
,
1672 &cputype
, &cpusubtype
);
1673 if (cputype
== bfd_arch_unknown
)
1675 fprintf (stderr
, "bfd_mach_o_scan: unknown architecture 0x%lx/0x%lx\n",
1676 header
->cputype
, header
->cpusubtype
);
1680 bfd_set_arch_mach (abfd
, cputype
, cpusubtype
);
1682 if (header
->ncmds
!= 0)
1685 ((bfd_mach_o_load_command
*)
1686 bfd_alloc (abfd
, header
->ncmds
* sizeof (bfd_mach_o_load_command
)));
1687 if (mdata
->commands
== NULL
)
1690 for (i
= 0; i
< header
->ncmds
; i
++)
1692 bfd_mach_o_load_command
*cur
= &mdata
->commands
[i
];
1698 bfd_mach_o_load_command
*prev
= &mdata
->commands
[i
- 1];
1699 cur
->offset
= prev
->offset
+ prev
->len
;
1702 if (bfd_mach_o_scan_read_command (abfd
, cur
) < 0)
1707 if (bfd_mach_o_scan_start_address (abfd
) < 0)
1710 fprintf (stderr
, "bfd_mach_o_scan: unable to scan start address: %s\n",
1711 bfd_errmsg (bfd_get_error ()));
1712 abfd
->tdata
.mach_o_data
= NULL
;
1717 bfd_mach_o_flatten_sections (abfd
);
1723 bfd_mach_o_mkobject (abfd
)
1726 bfd_mach_o_data_struct
*mdata
= NULL
;
1728 mdata
= ((bfd_mach_o_data_struct
*)
1729 bfd_alloc (abfd
, sizeof (bfd_mach_o_data_struct
)));
1732 abfd
->tdata
.mach_o_data
= mdata
;
1734 mdata
->header
.magic
= 0;
1735 mdata
->header
.cputype
= 0;
1736 mdata
->header
.cpusubtype
= 0;
1737 mdata
->header
.filetype
= 0;
1738 mdata
->header
.ncmds
= 0;
1739 mdata
->header
.sizeofcmds
= 0;
1740 mdata
->header
.flags
= 0;
1741 mdata
->header
.byteorder
= BFD_ENDIAN_UNKNOWN
;
1742 mdata
->commands
= NULL
;
1743 mdata
->nsymbols
= 0;
1744 mdata
->symbols
= NULL
;
1746 mdata
->sections
= NULL
;
1753 bfd_mach_o_object_p (abfd
)
1756 struct bfd_preserve preserve
;
1757 bfd_mach_o_header header
;
1759 preserve
.marker
= NULL
;
1760 if (bfd_mach_o_read_header (abfd
, &header
) != 0)
1763 if (! (header
.byteorder
== BFD_ENDIAN_BIG
1764 || header
.byteorder
== BFD_ENDIAN_LITTLE
))
1766 fprintf (stderr
, "unknown header byte-order value 0x%lx\n",
1767 (long) header
.byteorder
);
1771 if (! ((header
.byteorder
== BFD_ENDIAN_BIG
1772 && abfd
->xvec
->byteorder
== BFD_ENDIAN_BIG
1773 && abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
1774 || (header
.byteorder
== BFD_ENDIAN_LITTLE
1775 && abfd
->xvec
->byteorder
== BFD_ENDIAN_LITTLE
1776 && abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)))
1779 preserve
.marker
= bfd_zalloc (abfd
, sizeof (bfd_mach_o_data_struct
));
1780 if (preserve
.marker
== NULL
1781 || !bfd_preserve_save (abfd
, &preserve
))
1784 if (bfd_mach_o_scan (abfd
, &header
,
1785 (bfd_mach_o_data_struct
*) preserve
.marker
) != 0)
1788 bfd_preserve_finish (abfd
, &preserve
);
1792 bfd_set_error (bfd_error_wrong_format
);
1795 if (preserve
.marker
!= NULL
)
1796 bfd_preserve_restore (abfd
, &preserve
);
1801 bfd_mach_o_core_p (abfd
)
1804 struct bfd_preserve preserve
;
1805 bfd_mach_o_header header
;
1807 preserve
.marker
= NULL
;
1808 if (bfd_mach_o_read_header (abfd
, &header
) != 0)
1811 if (! (header
.byteorder
== BFD_ENDIAN_BIG
1812 || header
.byteorder
== BFD_ENDIAN_LITTLE
))
1814 fprintf (stderr
, "unknown header byte-order value 0x%lx\n",
1815 (long) header
.byteorder
);
1819 if (! ((header
.byteorder
== BFD_ENDIAN_BIG
1820 && abfd
->xvec
->byteorder
== BFD_ENDIAN_BIG
1821 && abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
1822 || (header
.byteorder
== BFD_ENDIAN_LITTLE
1823 && abfd
->xvec
->byteorder
== BFD_ENDIAN_LITTLE
1824 && abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)))
1827 if (header
.filetype
!= BFD_MACH_O_MH_CORE
)
1830 preserve
.marker
= bfd_zalloc (abfd
, sizeof (bfd_mach_o_data_struct
));
1831 if (preserve
.marker
== NULL
1832 || !bfd_preserve_save (abfd
, &preserve
))
1835 if (bfd_mach_o_scan (abfd
, &header
,
1836 (bfd_mach_o_data_struct
*) preserve
.marker
) != 0)
1839 bfd_preserve_finish (abfd
, &preserve
);
1843 bfd_set_error (bfd_error_wrong_format
);
1846 if (preserve
.marker
!= NULL
)
1847 bfd_preserve_restore (abfd
, &preserve
);
1851 typedef struct mach_o_fat_archentry
1853 unsigned long cputype
;
1854 unsigned long cpusubtype
;
1855 unsigned long offset
;
1857 unsigned long align
;
1859 } mach_o_fat_archentry
;
1861 typedef struct mach_o_fat_data_struct
1863 unsigned long magic
;
1864 unsigned long nfat_arch
;
1865 mach_o_fat_archentry
*archentries
;
1866 } mach_o_fat_data_struct
;
1869 bfd_mach_o_archive_p (abfd
)
1872 mach_o_fat_data_struct
*adata
= NULL
;
1873 unsigned char buf
[20];
1876 bfd_seek (abfd
, 0, SEEK_SET
);
1877 if (bfd_bread ((PTR
) buf
, 8, abfd
) != 8)
1880 adata
= (mach_o_fat_data_struct
*)
1881 bfd_alloc (abfd
, sizeof (mach_o_fat_data_struct
));
1885 adata
->magic
= bfd_getb32 (buf
);
1886 adata
->nfat_arch
= bfd_getb32 (buf
+ 4);
1887 if (adata
->magic
!= 0xcafebabe)
1890 adata
->archentries
= (mach_o_fat_archentry
*)
1891 bfd_alloc (abfd
, adata
->nfat_arch
* sizeof (mach_o_fat_archentry
));
1892 if (adata
->archentries
== NULL
)
1895 for (i
= 0; i
< adata
->nfat_arch
; i
++)
1897 bfd_seek (abfd
, 8 + 20 * i
, SEEK_SET
);
1899 if (bfd_bread ((PTR
) buf
, 20, abfd
) != 20)
1901 adata
->archentries
[i
].cputype
= bfd_getb32 (buf
);
1902 adata
->archentries
[i
].cpusubtype
= bfd_getb32 (buf
+ 4);
1903 adata
->archentries
[i
].offset
= bfd_getb32 (buf
+ 8);
1904 adata
->archentries
[i
].size
= bfd_getb32 (buf
+ 12);
1905 adata
->archentries
[i
].align
= bfd_getb32 (buf
+ 16);
1906 adata
->archentries
[i
].abfd
= NULL
;
1909 abfd
->tdata
.mach_o_fat_data
= adata
;
1914 bfd_release (abfd
, adata
);
1915 bfd_set_error (bfd_error_wrong_format
);
1920 bfd_mach_o_openr_next_archived_file (archive
, prev
)
1924 mach_o_fat_data_struct
*adata
;
1925 mach_o_fat_archentry
*entry
= NULL
;
1928 adata
= (mach_o_fat_data_struct
*) archive
->tdata
.mach_o_fat_data
;
1929 BFD_ASSERT (adata
!= NULL
);
1931 /* Find index of previous entry. */
1933 i
= 0; /* Start at first one. */
1936 for (i
= 0; i
< adata
->nfat_arch
; i
++)
1938 if (adata
->archentries
[i
].abfd
== prev
)
1942 if (i
== adata
->nfat_arch
)
1945 bfd_set_error (bfd_error_bad_value
);
1948 i
++; /* Get next entry. */
1951 if (i
>= adata
->nfat_arch
)
1953 bfd_set_error (bfd_error_no_more_archived_files
);
1957 entry
= &adata
->archentries
[i
];
1958 if (entry
->abfd
== NULL
)
1960 bfd
*nbfd
= _bfd_new_bfd_contained_in (archive
);
1966 nbfd
->origin
= entry
->offset
;
1967 s
= bfd_malloc (strlen (archive
->filename
) + 1);
1970 strcpy (s
, archive
->filename
);
1972 nbfd
->iostream
= NULL
;
1980 bfd_mach_o_lookup_section (abfd
, section
, mcommand
, msection
)
1983 bfd_mach_o_load_command
**mcommand
;
1984 bfd_mach_o_section
**msection
;
1986 struct mach_o_data_struct
*md
= abfd
->tdata
.mach_o_data
;
1987 unsigned int i
, j
, num
;
1989 bfd_mach_o_load_command
*ncmd
= NULL
;
1990 bfd_mach_o_section
*nsect
= NULL
;
1992 BFD_ASSERT (mcommand
!= NULL
);
1993 BFD_ASSERT (msection
!= NULL
);
1996 for (i
= 0; i
< md
->header
.ncmds
; i
++)
1998 struct bfd_mach_o_load_command
*cmd
= &md
->commands
[i
];
1999 struct bfd_mach_o_segment_command
*seg
= NULL
;
2001 if (cmd
->type
!= BFD_MACH_O_LC_SEGMENT
)
2003 seg
= &cmd
->command
.segment
;
2005 if (seg
->segment
== section
)
2012 for (j
= 0; j
< seg
->nsects
; j
++)
2014 struct bfd_mach_o_section
*sect
= &seg
->sections
[j
];
2016 if (sect
->bfdsection
== section
)
2031 bfd_mach_o_lookup_command (abfd
, type
, mcommand
)
2033 bfd_mach_o_load_command_type type
;
2034 bfd_mach_o_load_command
**mcommand
;
2036 struct mach_o_data_struct
*md
= NULL
;
2037 bfd_mach_o_load_command
*ncmd
= NULL
;
2038 unsigned int i
, num
;
2040 md
= abfd
->tdata
.mach_o_data
;
2042 BFD_ASSERT (md
!= NULL
);
2043 BFD_ASSERT (mcommand
!= NULL
);
2046 for (i
= 0; i
< md
->header
.ncmds
; i
++)
2048 struct bfd_mach_o_load_command
*cmd
= &md
->commands
[i
];
2050 if (cmd
->type
!= type
)
2063 bfd_mach_o_stack_addr (type
)
2064 enum bfd_mach_o_cpu_type type
;
2068 case BFD_MACH_O_CPU_TYPE_MC680x0
:
2070 case BFD_MACH_O_CPU_TYPE_MC88000
:
2072 case BFD_MACH_O_CPU_TYPE_POWERPC
:
2074 case BFD_MACH_O_CPU_TYPE_I386
:
2076 case BFD_MACH_O_CPU_TYPE_SPARC
:
2078 case BFD_MACH_O_CPU_TYPE_I860
:
2080 case BFD_MACH_O_CPU_TYPE_HPPA
:
2081 return 0xc0000000 - 0x04000000;
2088 bfd_mach_o_core_fetch_environment (abfd
, rbuf
, rlen
)
2090 unsigned char **rbuf
;
2093 bfd_mach_o_data_struct
*mdata
= abfd
->tdata
.mach_o_data
;
2094 unsigned long stackaddr
= bfd_mach_o_stack_addr (mdata
->header
.cputype
);
2097 for (i
= 0; i
< mdata
->header
.ncmds
; i
++)
2099 bfd_mach_o_load_command
*cur
= &mdata
->commands
[i
];
2100 bfd_mach_o_segment_command
*seg
= NULL
;
2102 if (cur
->type
!= BFD_MACH_O_LC_SEGMENT
)
2105 seg
= &cur
->command
.segment
;
2107 if ((seg
->vmaddr
+ seg
->vmsize
) == stackaddr
)
2109 unsigned long start
= seg
->fileoff
;
2110 unsigned long end
= seg
->fileoff
+ seg
->filesize
;
2111 unsigned char *buf
= bfd_malloc (1024);
2112 unsigned long size
= 1024;
2116 bfd_size_type nread
= 0;
2117 unsigned long offset
;
2118 int found_nonnull
= 0;
2120 if (size
> (end
- start
))
2121 size
= (end
- start
);
2123 buf
= bfd_realloc (buf
, size
);
2125 bfd_seek (abfd
, end
- size
, SEEK_SET
);
2126 nread
= bfd_bread (buf
, size
, abfd
);
2131 for (offset
= 4; offset
<= size
; offset
+= 4)
2135 val
= *((unsigned long *) (buf
+ size
- offset
));
2136 if (! found_nonnull
)
2141 else if (val
== 0x0)
2143 unsigned long bottom
;
2146 bottom
= seg
->fileoff
+ seg
->filesize
- offset
;
2147 top
= seg
->fileoff
+ seg
->filesize
- 4;
2148 *rbuf
= bfd_malloc (top
- bottom
);
2149 *rlen
= top
- bottom
;
2151 memcpy (*rbuf
, buf
+ size
- *rlen
, *rlen
);
2156 if (size
== (end
- start
))
2168 bfd_mach_o_core_file_failing_command (abfd
)
2171 unsigned char *buf
= NULL
;
2172 unsigned int len
= 0;
2175 ret
= bfd_mach_o_core_fetch_environment (abfd
, &buf
, &len
);
2183 bfd_mach_o_core_file_failing_signal (abfd
)
2184 bfd
*abfd ATTRIBUTE_UNUSED
;
2190 bfd_mach_o_core_file_matches_executable_p (core_bfd
, exec_bfd
)
2191 bfd
*core_bfd ATTRIBUTE_UNUSED
;
2192 bfd
*exec_bfd ATTRIBUTE_UNUSED
;
2197 #define TARGET_NAME mach_o_be_vec
2198 #define TARGET_STRING "mach-o-be"
2199 #define TARGET_BIG_ENDIAN 1
2200 #define TARGET_ARCHIVE 0
2202 #include "mach-o-target.c"
2205 #undef TARGET_STRING
2206 #undef TARGET_BIG_ENDIAN
2207 #undef TARGET_ARCHIVE
2209 #define TARGET_NAME mach_o_le_vec
2210 #define TARGET_STRING "mach-o-le"
2211 #define TARGET_BIG_ENDIAN 0
2212 #define TARGET_ARCHIVE 0
2214 #include "mach-o-target.c"
2217 #undef TARGET_STRING
2218 #undef TARGET_BIG_ENDIAN
2219 #undef TARGET_ARCHIVE
2221 #define TARGET_NAME mach_o_fat_vec
2222 #define TARGET_STRING "mach-o-fat"
2223 #define TARGET_BIG_ENDIAN 1
2224 #define TARGET_ARCHIVE 1
2226 #include "mach-o-target.c"
2229 #undef TARGET_STRING
2230 #undef TARGET_BIG_ENDIAN
2231 #undef TARGET_ARCHIVE