1 /* Mach-O support for BFD.
2 Copyright 1999, 2000, 2001, 2002, 2003, 2005, 2007, 2008, 2009, 2011,
4 Free Software Foundation, Inc.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
23 #ifndef _BFD_MACH_O_H_
24 #define _BFD_MACH_O_H_
27 #include "mach-o/loader.h"
29 typedef struct bfd_mach_o_header
32 unsigned long cputype;
33 unsigned long cpusubtype;
34 unsigned long filetype;
36 unsigned long sizeofcmds;
38 unsigned int reserved;
39 /* Version 1: 32 bits, version 2: 64 bits. */
41 enum bfd_endian byteorder;
45 #define BFD_MACH_O_SEGNAME_SIZE 16
46 #define BFD_MACH_O_SECTNAME_SIZE 16
48 typedef struct bfd_mach_o_section
50 /* Fields present in the file. */
51 char sectname[BFD_MACH_O_SECTNAME_SIZE + 1]; /* Always NUL padded. */
52 char segname[BFD_MACH_O_SEGNAME_SIZE + 1];
60 unsigned long reserved1;
61 unsigned long reserved2;
62 unsigned long reserved3;
64 /* Corresponding bfd section. */
67 /* Simply linked list. */
68 struct bfd_mach_o_section *next;
72 typedef struct bfd_mach_o_segment_command
74 char segname[BFD_MACH_O_SEGNAME_SIZE + 1];
78 unsigned long filesize;
79 unsigned long maxprot; /* Maximum permitted protection. */
80 unsigned long initprot; /* Initial protection. */
84 /* Linked list of sections. */
85 bfd_mach_o_section *sect_head;
86 bfd_mach_o_section *sect_tail;
88 bfd_mach_o_segment_command;
90 /* Protection flags. */
91 #define BFD_MACH_O_PROT_READ 0x01
92 #define BFD_MACH_O_PROT_WRITE 0x02
93 #define BFD_MACH_O_PROT_EXECUTE 0x04
95 /* Expanded internal representation of a relocation entry. */
96 typedef struct bfd_mach_o_reloc_info
100 unsigned int r_scattered : 1;
101 unsigned int r_type : 4;
102 unsigned int r_pcrel : 1;
103 unsigned int r_length : 2;
104 unsigned int r_extern : 1;
106 bfd_mach_o_reloc_info;
108 typedef struct bfd_mach_o_asymbol
110 /* The actual symbol which the rest of BFD works with. */
113 /* Mach-O symbol fields. */
114 unsigned char n_type;
115 unsigned char n_sect;
116 unsigned short n_desc;
120 /* The symbol table is sorted like this:
122 (otherwise in order of generation)
125 (3) external undefined
131 typedef struct bfd_mach_o_symtab_command
136 unsigned int strsize;
137 bfd_mach_o_asymbol *symbols;
140 bfd_mach_o_symtab_command;
142 /* This is the second set of the symbolic information which is used to support
143 the data structures for the dynamically link editor.
145 The original set of symbolic information in the symtab_command which contains
146 the symbol and string tables must also be present when this load command is
147 present. When this load command is present the symbol table is organized
148 into three groups of symbols:
149 local symbols (static and debugging symbols) - grouped by module
150 defined external symbols - grouped by module (sorted by name if not lib)
151 undefined external symbols (sorted by name)
152 In this load command there are offsets and counts to each of the three groups
155 This load command contains a the offsets and sizes of the following new
156 symbolic information tables:
159 reference symbol table
160 indirect symbol table
161 The first three tables above (the table of contents, module table and
162 reference symbol table) are only present if the file is a dynamically linked
163 shared library. For executable and object modules, which are files
164 containing only one module, the information that would be in these three
165 tables is determined as follows:
166 table of contents - the defined external symbols are sorted by name
167 module table - the file contains only one module so everything in the
168 file is part of the module.
169 reference symbol table - is the defined and undefined external symbols
171 For dynamically linked shared library files this load command also contains
172 offsets and sizes to the pool of relocation entries for all sections
173 separated into two groups:
174 external relocation entries
175 local relocation entries
176 For executable and object modules the relocation entries continue to hang
177 off the section structures. */
179 typedef struct bfd_mach_o_dylib_module
181 /* Index into the string table indicating the name of the module. */
182 unsigned long module_name_idx;
185 /* Index into the symbol table of the first defined external symbol provided
187 unsigned long iextdefsym;
189 /* Number of external symbols provided by this module. */
190 unsigned long nextdefsym;
192 /* Index into the external reference table of the first entry
193 provided by this module. */
194 unsigned long irefsym;
196 /* Number of external reference entries provided by this module. */
197 unsigned long nrefsym;
199 /* Index into the symbol table of the first local symbol provided by this
201 unsigned long ilocalsym;
203 /* Number of local symbols provided by this module. */
204 unsigned long nlocalsym;
206 /* Index into the external relocation table of the first entry provided
208 unsigned long iextrel;
210 /* Number of external relocation entries provided by this module. */
211 unsigned long nextrel;
213 /* Index in the module initialization section to the pointers for this
215 unsigned short iinit;
217 /* Index in the module termination section to the pointers for this
219 unsigned short iterm;
221 /* Number of pointers in the module initialization for this module. */
222 unsigned short ninit;
224 /* Number of pointers in the module termination for this module. */
225 unsigned short nterm;
227 /* Number of data byte for this module that are used in the __module_info
228 section of the __OBJC segment. */
229 unsigned long objc_module_info_size;
231 /* Statically linked address of the start of the data for this module
232 in the __module_info section of the __OBJC_segment. */
233 bfd_vma objc_module_info_addr;
235 bfd_mach_o_dylib_module;
237 typedef struct bfd_mach_o_dylib_table_of_content
239 /* Index into the symbol table to the defined external symbol. */
240 unsigned long symbol_index;
242 /* Index into the module table to the module for this entry. */
243 unsigned long module_index;
245 bfd_mach_o_dylib_table_of_content;
247 typedef struct bfd_mach_o_dylib_reference
249 /* Index into the symbol table for the symbol being referenced. */
252 /* Type of the reference being made (use REFERENCE_FLAGS constants). */
255 bfd_mach_o_dylib_reference;
256 #define BFD_MACH_O_REFERENCE_SIZE 4
258 typedef struct bfd_mach_o_dysymtab_command
260 /* The symbols indicated by symoff and nsyms of the LC_SYMTAB load command
261 are grouped into the following three groups:
262 local symbols (further grouped by the module they are from)
263 defined external symbols (further grouped by the module they are from)
266 The local symbols are used only for debugging. The dynamic binding
267 process may have to use them to indicate to the debugger the local
268 symbols for a module that is being bound.
270 The last two groups are used by the dynamic binding process to do the
271 binding (indirectly through the module table and the reference symbol
272 table when this is a dynamically linked shared library file). */
274 unsigned long ilocalsym; /* Index to local symbols. */
275 unsigned long nlocalsym; /* Number of local symbols. */
276 unsigned long iextdefsym; /* Index to externally defined symbols. */
277 unsigned long nextdefsym; /* Number of externally defined symbols. */
278 unsigned long iundefsym; /* Index to undefined symbols. */
279 unsigned long nundefsym; /* Number of undefined symbols. */
281 /* For the for the dynamic binding process to find which module a symbol
282 is defined in the table of contents is used (analogous to the ranlib
283 structure in an archive) which maps defined external symbols to modules
284 they are defined in. This exists only in a dynamically linked shared
285 library file. For executable and object modules the defined external
286 symbols are sorted by name and is use as the table of contents. */
288 unsigned long tocoff; /* File offset to table of contents. */
289 unsigned long ntoc; /* Number of entries in table of contents. */
291 /* To support dynamic binding of "modules" (whole object files) the symbol
292 table must reflect the modules that the file was created from. This is
293 done by having a module table that has indexes and counts into the merged
294 tables for each module. The module structure that these two entries
295 refer to is described below. This exists only in a dynamically linked
296 shared library file. For executable and object modules the file only
297 contains one module so everything in the file belongs to the module. */
299 unsigned long modtaboff; /* File offset to module table. */
300 unsigned long nmodtab; /* Number of module table entries. */
302 /* To support dynamic module binding the module structure for each module
303 indicates the external references (defined and undefined) each module
304 makes. For each module there is an offset and a count into the
305 reference symbol table for the symbols that the module references.
306 This exists only in a dynamically linked shared library file. For
307 executable and object modules the defined external symbols and the
308 undefined external symbols indicates the external references. */
310 unsigned long extrefsymoff; /* Offset to referenced symbol table. */
311 unsigned long nextrefsyms; /* Number of referenced symbol table entries. */
313 /* The sections that contain "symbol pointers" and "routine stubs" have
314 indexes and (implied counts based on the size of the section and fixed
315 size of the entry) into the "indirect symbol" table for each pointer
316 and stub. For every section of these two types the index into the
317 indirect symbol table is stored in the section header in the field
318 reserved1. An indirect symbol table entry is simply a 32bit index into
319 the symbol table to the symbol that the pointer or stub is referring to.
320 The indirect symbol table is ordered to match the entries in the section. */
322 unsigned long indirectsymoff; /* File offset to the indirect symbol table. */
323 unsigned long nindirectsyms; /* Number of indirect symbol table entries. */
325 /* To support relocating an individual module in a library file quickly the
326 external relocation entries for each module in the library need to be
327 accessed efficiently. Since the relocation entries can't be accessed
328 through the section headers for a library file they are separated into
329 groups of local and external entries further grouped by module. In this
330 case the presents of this load command who's extreloff, nextrel,
331 locreloff and nlocrel fields are non-zero indicates that the relocation
332 entries of non-merged sections are not referenced through the section
333 structures (and the reloff and nreloc fields in the section headers are
336 Since the relocation entries are not accessed through the section headers
337 this requires the r_address field to be something other than a section
338 offset to identify the item to be relocated. In this case r_address is
339 set to the offset from the vmaddr of the first LC_SEGMENT command.
341 The relocation entries are grouped by module and the module table
342 entries have indexes and counts into them for the group of external
343 relocation entries for that the module.
345 For sections that are merged across modules there must not be any
346 remaining external relocation entries for them (for merged sections
347 remaining relocation entries must be local). */
349 unsigned long extreloff; /* Offset to external relocation entries. */
350 unsigned long nextrel; /* Number of external relocation entries. */
352 /* All the local relocation entries are grouped together (they are not
353 grouped by their module since they are only used if the object is moved
354 from it statically link edited address). */
356 unsigned long locreloff; /* Offset to local relocation entries. */
357 unsigned long nlocrel; /* Number of local relocation entries. */
359 bfd_mach_o_dylib_module *dylib_module;
360 bfd_mach_o_dylib_table_of_content *dylib_toc;
361 unsigned int *indirect_syms;
362 bfd_mach_o_dylib_reference *ext_refs;
364 bfd_mach_o_dysymtab_command;
366 /* An indirect symbol table entry is simply a 32bit index into the symbol table
367 to the symbol that the pointer or stub is refering to. Unless it is for a
368 non-lazy symbol pointer section for a defined symbol which strip(1) has
369 removed. In which case it has the value INDIRECT_SYMBOL_LOCAL. If the
370 symbol was also absolute INDIRECT_SYMBOL_ABS is or'ed with that. */
372 #define BFD_MACH_O_INDIRECT_SYMBOL_LOCAL 0x80000000
373 #define BFD_MACH_O_INDIRECT_SYMBOL_ABS 0x40000000
374 #define BFD_MACH_O_INDIRECT_SYMBOL_SIZE 4
376 /* For LC_THREAD or LC_UNIXTHREAD. */
378 typedef struct bfd_mach_o_thread_flavour
380 unsigned long flavour;
381 unsigned long offset;
384 bfd_mach_o_thread_flavour;
386 typedef struct bfd_mach_o_thread_command
388 unsigned long nflavours;
389 bfd_mach_o_thread_flavour *flavours;
392 bfd_mach_o_thread_command;
394 /* For LC_LOAD_DYLINKER and LC_ID_DYLINKER. */
396 typedef struct bfd_mach_o_dylinker_command
398 unsigned long name_offset; /* Offset to library's path name. */
399 unsigned long name_len; /* Offset to library's path name. */
402 bfd_mach_o_dylinker_command;
404 /* For LC_LOAD_DYLIB, LC_LOAD_WEAK_DYLIB, LC_ID_DYLIB
405 or LC_REEXPORT_DYLIB. */
407 typedef struct bfd_mach_o_dylib_command
409 unsigned long name_offset; /* Offset to library's path name. */
410 unsigned long name_len; /* Offset to library's path name. */
411 unsigned long timestamp; /* Library's build time stamp. */
412 unsigned long current_version; /* Library's current version number. */
413 unsigned long compatibility_version; /* Library's compatibility vers number. */
416 bfd_mach_o_dylib_command;
418 /* For LC_PREBOUND_DYLIB. */
420 typedef struct bfd_mach_o_prebound_dylib_command
422 unsigned long name; /* Library's path name. */
423 unsigned long nmodules; /* Number of modules in library. */
424 unsigned long linked_modules; /* Bit vector of linked modules. */
426 bfd_mach_o_prebound_dylib_command;
430 typedef struct bfd_mach_o_uuid_command
432 unsigned char uuid[16];
434 bfd_mach_o_uuid_command;
436 /* For LC_CODE_SIGNATURE or LC_SEGMENT_SPLIT_INFO. */
438 typedef struct bfd_mach_o_linkedit_command
440 unsigned long dataoff;
441 unsigned long datasize;
443 bfd_mach_o_linkedit_command;
445 typedef struct bfd_mach_o_str_command
447 unsigned long stroff;
448 unsigned long str_len;
451 bfd_mach_o_str_command;
453 typedef struct bfd_mach_o_dyld_info_command
455 /* File offset and size to rebase info. */
456 unsigned int rebase_off;
457 unsigned int rebase_size;
459 /* File offset and size of binding info. */
460 unsigned int bind_off;
461 unsigned int bind_size;
463 /* File offset and size of weak binding info. */
464 unsigned int weak_bind_off;
465 unsigned int weak_bind_size;
467 /* File offset and size of lazy binding info. */
468 unsigned int lazy_bind_off;
469 unsigned int lazy_bind_size;
471 /* File offset and size of export info. */
472 unsigned int export_off;
473 unsigned int export_size;
475 bfd_mach_o_dyld_info_command;
477 typedef struct bfd_mach_o_version_min_command
482 unsigned int reserved;
484 bfd_mach_o_version_min_command;
486 typedef struct bfd_mach_o_encryption_info_command
488 unsigned int cryptoff;
489 unsigned int cryptsize;
490 unsigned int cryptid;
492 bfd_mach_o_encryption_info_command;
494 typedef struct bfd_mach_o_load_command
496 bfd_mach_o_load_command_type type;
497 bfd_boolean type_required;
502 bfd_mach_o_segment_command segment;
503 bfd_mach_o_symtab_command symtab;
504 bfd_mach_o_dysymtab_command dysymtab;
505 bfd_mach_o_thread_command thread;
506 bfd_mach_o_dylib_command dylib;
507 bfd_mach_o_dylinker_command dylinker;
508 bfd_mach_o_prebound_dylib_command prebound_dylib;
509 bfd_mach_o_uuid_command uuid;
510 bfd_mach_o_linkedit_command linkedit;
511 bfd_mach_o_str_command str;
512 bfd_mach_o_dyld_info_command dyld_info;
513 bfd_mach_o_version_min_command version_min;
514 bfd_mach_o_encryption_info_command encryption_info;
518 bfd_mach_o_load_command;
520 typedef struct mach_o_data_struct
523 bfd_mach_o_header header;
524 /* Array of load commands (length is given by header.ncmds). */
525 bfd_mach_o_load_command *commands;
527 /* Flatten array of sections. The array is 0-based. */
528 unsigned long nsects;
529 bfd_mach_o_section **sections;
531 /* Used while writing: current length of the output file. This is used
532 to allocate space in the file. */
535 /* As symtab is referenced by other load command, it is handy to have
536 a direct access to it. Although it is not clearly stated, only one symtab
538 bfd_mach_o_symtab_command *symtab;
539 bfd_mach_o_dysymtab_command *dysymtab;
541 /* Base values used for building the dysymtab for a single-module object. */
542 unsigned long nlocal;
543 unsigned long ndefext;
544 unsigned long nundefext;
546 /* If this is non-zero, then the pointer below is populated. */
547 unsigned long nindirect;
548 /* A set of synthetic symbols representing the 'indirect' ones in the file.
549 These should be sorted (a) by the section they represent and (b) by the
550 order that they appear within each section. */
551 asymbol **indirect_syms;
553 /* A place to stash dwarf2 info for this bfd. */
554 void *dwarf2_find_line_info;
556 /* BFD of .dSYM file. */
559 /* Cache of dynamic relocs. */
560 arelent *dyn_reloc_cache;
562 bfd_mach_o_data_struct;
564 typedef struct bfd_mach_o_xlat_name
569 bfd_mach_o_xlat_name;
571 /* Target specific routines. */
573 #define bfd_mach_o_get_data(abfd) ((abfd)->tdata.mach_o_data)
574 #define bfd_mach_o_get_backend_data(abfd) \
575 ((bfd_mach_o_backend_data*)(abfd)->xvec->backend_data)
577 /* Get the Mach-O header for section SEC. */
578 #define bfd_mach_o_get_mach_o_section(sec) \
579 ((bfd_mach_o_section *)(sec)->used_by_bfd)
581 bfd_boolean bfd_mach_o_valid (bfd *);
582 bfd_boolean bfd_mach_o_mkobject_init (bfd *);
583 const bfd_target *bfd_mach_o_object_p (bfd *);
584 const bfd_target *bfd_mach_o_core_p (bfd *);
585 const bfd_target *bfd_mach_o_archive_p (bfd *);
586 bfd *bfd_mach_o_openr_next_archived_file (bfd *, bfd *);
587 bfd_boolean bfd_mach_o_set_arch_mach (bfd *, enum bfd_architecture,
589 int bfd_mach_o_lookup_command (bfd *, bfd_mach_o_load_command_type, bfd_mach_o_load_command **);
590 bfd_boolean bfd_mach_o_new_section_hook (bfd *, asection *);
591 bfd_boolean bfd_mach_o_write_contents (bfd *);
592 bfd_boolean bfd_mach_o_bfd_copy_private_symbol_data (bfd *, asymbol *,
594 bfd_boolean bfd_mach_o_bfd_copy_private_section_data (bfd *, asection *,
596 bfd_boolean bfd_mach_o_bfd_copy_private_bfd_data (bfd *, bfd *);
597 bfd_boolean bfd_mach_o_bfd_set_private_flags (bfd *, flagword);
598 long bfd_mach_o_get_symtab_upper_bound (bfd *);
599 long bfd_mach_o_canonicalize_symtab (bfd *, asymbol **);
600 long bfd_mach_o_get_synthetic_symtab (bfd *, long, asymbol **, long,
601 asymbol **, asymbol **ret);
602 long bfd_mach_o_get_reloc_upper_bound (bfd *, asection *);
603 long bfd_mach_o_canonicalize_reloc (bfd *, asection *, arelent **, asymbol **);
604 long bfd_mach_o_get_dynamic_reloc_upper_bound (bfd *);
605 long bfd_mach_o_canonicalize_dynamic_reloc (bfd *, arelent **, asymbol **);
606 asymbol *bfd_mach_o_make_empty_symbol (bfd *);
607 void bfd_mach_o_get_symbol_info (bfd *, asymbol *, symbol_info *);
608 void bfd_mach_o_print_symbol (bfd *, PTR, asymbol *, bfd_print_symbol_type);
609 int bfd_mach_o_sizeof_headers (bfd *, struct bfd_link_info *);
610 unsigned long bfd_mach_o_stack_addr (enum bfd_mach_o_cpu_type);
611 int bfd_mach_o_core_fetch_environment (bfd *, unsigned char **, unsigned int *);
612 char *bfd_mach_o_core_file_failing_command (bfd *);
613 int bfd_mach_o_core_file_failing_signal (bfd *);
614 bfd_boolean bfd_mach_o_core_file_matches_executable_p (bfd *, bfd *);
615 bfd *bfd_mach_o_fat_extract (bfd *, bfd_format , const bfd_arch_info_type *);
616 const bfd_target *bfd_mach_o_header_p (bfd *, bfd_mach_o_filetype,
617 bfd_mach_o_cpu_type);
618 bfd_boolean bfd_mach_o_build_commands (bfd *);
619 bfd_boolean bfd_mach_o_set_section_contents (bfd *, asection *, const void *,
620 file_ptr, bfd_size_type);
621 unsigned int bfd_mach_o_version (bfd *);
623 unsigned int bfd_mach_o_get_section_type_from_name (bfd *, const char *);
624 unsigned int bfd_mach_o_get_section_attribute_from_name (const char *);
626 void bfd_mach_o_convert_section_name_to_bfd (bfd *, const char *, const char *,
627 const char **, flagword *);
628 bfd_boolean bfd_mach_o_find_nearest_line (bfd *, asection *, asymbol **,
629 bfd_vma, const char **,
630 const char **, unsigned int *);
631 bfd_boolean bfd_mach_o_close_and_cleanup (bfd *);
632 bfd_boolean bfd_mach_o_free_cached_info (bfd *);
634 unsigned int bfd_mach_o_section_get_nbr_indirect (bfd *, bfd_mach_o_section *);
635 unsigned int bfd_mach_o_section_get_entry_size (bfd *, bfd_mach_o_section *);
636 bfd_boolean bfd_mach_o_read_symtab_symbols (bfd *);
637 bfd_boolean bfd_mach_o_read_symtab_strtab (bfd *abfd);
639 /* A placeholder in case we need to suppress emitting the dysymtab for some
640 reason (e.g. compatibility with older system versions). */
641 #define bfd_mach_o_should_emit_dysymtab(x) TRUE
643 extern const bfd_mach_o_xlat_name bfd_mach_o_section_attribute_name[];
644 extern const bfd_mach_o_xlat_name bfd_mach_o_section_type_name[];
646 extern const bfd_target mach_o_fat_vec;
648 /* Interfaces between BFD names and Mach-O names. */
650 typedef struct mach_o_section_name_xlat
652 const char *bfd_name;
653 const char *mach_o_name;
655 unsigned int macho_sectype;
656 unsigned int macho_secattr;
657 unsigned int sectalign;
658 } mach_o_section_name_xlat;
660 typedef struct mach_o_segment_name_xlat
663 const mach_o_section_name_xlat *sections;
664 } mach_o_segment_name_xlat;
666 const mach_o_section_name_xlat *
667 bfd_mach_o_section_data_for_mach_sect (bfd *, const char *, const char *);
668 const mach_o_section_name_xlat *
669 bfd_mach_o_section_data_for_bfd_name (bfd *, const char *, const char **);
671 typedef struct bfd_mach_o_backend_data
673 enum bfd_architecture arch;
674 bfd_boolean (*_bfd_mach_o_swap_reloc_in)(arelent *, bfd_mach_o_reloc_info *);
675 bfd_boolean (*_bfd_mach_o_swap_reloc_out)(arelent *, bfd_mach_o_reloc_info *);
676 bfd_boolean (*_bfd_mach_o_print_thread)(bfd *, bfd_mach_o_thread_flavour *,
678 const mach_o_segment_name_xlat *segsec_names_xlat;
679 bfd_boolean (*bfd_mach_o_section_type_valid_for_target) (unsigned long);
681 bfd_mach_o_backend_data;
683 /* Symbol type tests. */
685 #define IS_MACHO_INDIRECT(x) (((x) & BFD_MACH_O_N_TYPE) == BFD_MACH_O_N_INDR)
687 #endif /* _BFD_MACH_O_H_ */