1 /* Mach-O support for BFD.
2 Copyright 1999, 2000, 2001, 2002, 2003, 2005, 2007, 2008, 2009, 2011
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
22 #ifndef _BFD_MACH_O_H_
23 #define _BFD_MACH_O_H_
26 #include "mach-o/loader.h"
28 typedef struct bfd_mach_o_header
31 unsigned long cputype;
32 unsigned long cpusubtype;
33 unsigned long filetype;
35 unsigned long sizeofcmds;
37 unsigned int reserved;
38 /* Version 1: 32 bits, version 2: 64 bits. */
40 enum bfd_endian byteorder;
44 #define BFD_MACH_O_SEGNAME_SIZE 16
45 #define BFD_MACH_O_SECTNAME_SIZE 16
47 typedef struct bfd_mach_o_section
49 /* Fields present in the file. */
50 char sectname[BFD_MACH_O_SECTNAME_SIZE + 1]; /* Always NUL padded. */
51 char segname[BFD_MACH_O_SEGNAME_SIZE + 1];
59 unsigned long reserved1;
60 unsigned long reserved2;
61 unsigned long reserved3;
63 /* Corresponding bfd section. */
66 /* Simply linked list. */
67 struct bfd_mach_o_section *next;
71 typedef struct bfd_mach_o_segment_command
73 char segname[BFD_MACH_O_SEGNAME_SIZE + 1];
77 unsigned long filesize;
78 unsigned long maxprot; /* Maximum permitted protection. */
79 unsigned long initprot; /* Initial protection. */
83 /* Linked list of sections. */
84 bfd_mach_o_section *sect_head;
85 bfd_mach_o_section *sect_tail;
87 bfd_mach_o_segment_command;
89 /* Protection flags. */
90 #define BFD_MACH_O_PROT_READ 0x01
91 #define BFD_MACH_O_PROT_WRITE 0x02
92 #define BFD_MACH_O_PROT_EXECUTE 0x04
94 /* Expanded internal representation of a relocation entry. */
95 typedef struct bfd_mach_o_reloc_info
99 unsigned int r_scattered : 1;
100 unsigned int r_type : 4;
101 unsigned int r_pcrel : 1;
102 unsigned int r_length : 2;
103 unsigned int r_extern : 1;
105 bfd_mach_o_reloc_info;
107 typedef struct bfd_mach_o_asymbol
109 /* The actual symbol which the rest of BFD works with. */
112 /* Fields from Mach-O symbol. */
113 unsigned char n_type;
114 unsigned char n_sect;
115 unsigned short n_desc;
119 typedef struct bfd_mach_o_symtab_command
124 unsigned int strsize;
125 bfd_mach_o_asymbol *symbols;
128 bfd_mach_o_symtab_command;
130 /* This is the second set of the symbolic information which is used to support
131 the data structures for the dynamically link editor.
133 The original set of symbolic information in the symtab_command which contains
134 the symbol and string tables must also be present when this load command is
135 present. When this load command is present the symbol table is organized
136 into three groups of symbols:
137 local symbols (static and debugging symbols) - grouped by module
138 defined external symbols - grouped by module (sorted by name if not lib)
139 undefined external symbols (sorted by name)
140 In this load command there are offsets and counts to each of the three groups
143 This load command contains a the offsets and sizes of the following new
144 symbolic information tables:
147 reference symbol table
148 indirect symbol table
149 The first three tables above (the table of contents, module table and
150 reference symbol table) are only present if the file is a dynamically linked
151 shared library. For executable and object modules, which are files
152 containing only one module, the information that would be in these three
153 tables is determined as follows:
154 table of contents - the defined external symbols are sorted by name
155 module table - the file contains only one module so everything in the
156 file is part of the module.
157 reference symbol table - is the defined and undefined external symbols
159 For dynamically linked shared library files this load command also contains
160 offsets and sizes to the pool of relocation entries for all sections
161 separated into two groups:
162 external relocation entries
163 local relocation entries
164 For executable and object modules the relocation entries continue to hang
165 off the section structures. */
167 typedef struct bfd_mach_o_dylib_module
169 /* Index into the string table indicating the name of the module. */
170 unsigned long module_name_idx;
173 /* Index into the symbol table of the first defined external symbol provided
175 unsigned long iextdefsym;
177 /* Number of external symbols provided by this module. */
178 unsigned long nextdefsym;
180 /* Index into the external reference table of the first entry
181 provided by this module. */
182 unsigned long irefsym;
184 /* Number of external reference entries provided by this module. */
185 unsigned long nrefsym;
187 /* Index into the symbol table of the first local symbol provided by this
189 unsigned long ilocalsym;
191 /* Number of local symbols provided by this module. */
192 unsigned long nlocalsym;
194 /* Index into the external relocation table of the first entry provided
196 unsigned long iextrel;
198 /* Number of external relocation entries provided by this module. */
199 unsigned long nextrel;
201 /* Index in the module initialization section to the pointers for this
203 unsigned short iinit;
205 /* Index in the module termination section to the pointers for this
207 unsigned short iterm;
209 /* Number of pointers in the module initialization for this module. */
210 unsigned short ninit;
212 /* Number of pointers in the module termination for this module. */
213 unsigned short nterm;
215 /* Number of data byte for this module that are used in the __module_info
216 section of the __OBJC segment. */
217 unsigned long objc_module_info_size;
219 /* Statically linked address of the start of the data for this module
220 in the __module_info section of the __OBJC_segment. */
221 bfd_vma objc_module_info_addr;
223 bfd_mach_o_dylib_module;
225 typedef struct bfd_mach_o_dylib_table_of_content
227 /* Index into the symbol table to the defined external symbol. */
228 unsigned long symbol_index;
230 /* Index into the module table to the module for this entry. */
231 unsigned long module_index;
233 bfd_mach_o_dylib_table_of_content;
235 typedef struct bfd_mach_o_dylib_reference
237 /* Index into the symbol table for the symbol being referenced. */
240 /* Type of the reference being made (use REFERENCE_FLAGS constants). */
243 bfd_mach_o_dylib_reference;
244 #define BFD_MACH_O_REFERENCE_SIZE 4
246 typedef struct bfd_mach_o_dysymtab_command
248 /* The symbols indicated by symoff and nsyms of the LC_SYMTAB load command
249 are grouped into the following three groups:
250 local symbols (further grouped by the module they are from)
251 defined external symbols (further grouped by the module they are from)
254 The local symbols are used only for debugging. The dynamic binding
255 process may have to use them to indicate to the debugger the local
256 symbols for a module that is being bound.
258 The last two groups are used by the dynamic binding process to do the
259 binding (indirectly through the module table and the reference symbol
260 table when this is a dynamically linked shared library file). */
262 unsigned long ilocalsym; /* Index to local symbols. */
263 unsigned long nlocalsym; /* Number of local symbols. */
264 unsigned long iextdefsym; /* Index to externally defined symbols. */
265 unsigned long nextdefsym; /* Number of externally defined symbols. */
266 unsigned long iundefsym; /* Index to undefined symbols. */
267 unsigned long nundefsym; /* Number of undefined symbols. */
269 /* For the for the dynamic binding process to find which module a symbol
270 is defined in the table of contents is used (analogous to the ranlib
271 structure in an archive) which maps defined external symbols to modules
272 they are defined in. This exists only in a dynamically linked shared
273 library file. For executable and object modules the defined external
274 symbols are sorted by name and is use as the table of contents. */
276 unsigned long tocoff; /* File offset to table of contents. */
277 unsigned long ntoc; /* Number of entries in table of contents. */
279 /* To support dynamic binding of "modules" (whole object files) the symbol
280 table must reflect the modules that the file was created from. This is
281 done by having a module table that has indexes and counts into the merged
282 tables for each module. The module structure that these two entries
283 refer to is described below. This exists only in a dynamically linked
284 shared library file. For executable and object modules the file only
285 contains one module so everything in the file belongs to the module. */
287 unsigned long modtaboff; /* File offset to module table. */
288 unsigned long nmodtab; /* Number of module table entries. */
290 /* To support dynamic module binding the module structure for each module
291 indicates the external references (defined and undefined) each module
292 makes. For each module there is an offset and a count into the
293 reference symbol table for the symbols that the module references.
294 This exists only in a dynamically linked shared library file. For
295 executable and object modules the defined external symbols and the
296 undefined external symbols indicates the external references. */
298 unsigned long extrefsymoff; /* Offset to referenced symbol table. */
299 unsigned long nextrefsyms; /* Number of referenced symbol table entries. */
301 /* The sections that contain "symbol pointers" and "routine stubs" have
302 indexes and (implied counts based on the size of the section and fixed
303 size of the entry) into the "indirect symbol" table for each pointer
304 and stub. For every section of these two types the index into the
305 indirect symbol table is stored in the section header in the field
306 reserved1. An indirect symbol table entry is simply a 32bit index into
307 the symbol table to the symbol that the pointer or stub is referring to.
308 The indirect symbol table is ordered to match the entries in the section. */
310 unsigned long indirectsymoff; /* File offset to the indirect symbol table. */
311 unsigned long nindirectsyms; /* Number of indirect symbol table entries. */
313 /* To support relocating an individual module in a library file quickly the
314 external relocation entries for each module in the library need to be
315 accessed efficiently. Since the relocation entries can't be accessed
316 through the section headers for a library file they are separated into
317 groups of local and external entries further grouped by module. In this
318 case the presents of this load command who's extreloff, nextrel,
319 locreloff and nlocrel fields are non-zero indicates that the relocation
320 entries of non-merged sections are not referenced through the section
321 structures (and the reloff and nreloc fields in the section headers are
324 Since the relocation entries are not accessed through the section headers
325 this requires the r_address field to be something other than a section
326 offset to identify the item to be relocated. In this case r_address is
327 set to the offset from the vmaddr of the first LC_SEGMENT command.
329 The relocation entries are grouped by module and the module table
330 entries have indexes and counts into them for the group of external
331 relocation entries for that the module.
333 For sections that are merged across modules there must not be any
334 remaining external relocation entries for them (for merged sections
335 remaining relocation entries must be local). */
337 unsigned long extreloff; /* Offset to external relocation entries. */
338 unsigned long nextrel; /* Number of external relocation entries. */
340 /* All the local relocation entries are grouped together (they are not
341 grouped by their module since they are only used if the object is moved
342 from it statically link edited address). */
344 unsigned long locreloff; /* Offset to local relocation entries. */
345 unsigned long nlocrel; /* Number of local relocation entries. */
347 bfd_mach_o_dylib_module *dylib_module;
348 bfd_mach_o_dylib_table_of_content *dylib_toc;
349 unsigned int *indirect_syms;
350 bfd_mach_o_dylib_reference *ext_refs;
352 bfd_mach_o_dysymtab_command;
354 /* An indirect symbol table entry is simply a 32bit index into the symbol table
355 to the symbol that the pointer or stub is refering to. Unless it is for a
356 non-lazy symbol pointer section for a defined symbol which strip(1) has
357 removed. In which case it has the value INDIRECT_SYMBOL_LOCAL. If the
358 symbol was also absolute INDIRECT_SYMBOL_ABS is or'ed with that. */
360 #define BFD_MACH_O_INDIRECT_SYMBOL_LOCAL 0x80000000
361 #define BFD_MACH_O_INDIRECT_SYMBOL_ABS 0x40000000
362 #define BFD_MACH_O_INDIRECT_SYMBOL_SIZE 4
364 /* For LC_THREAD or LC_UNIXTHREAD. */
366 typedef struct bfd_mach_o_thread_flavour
368 unsigned long flavour;
369 unsigned long offset;
372 bfd_mach_o_thread_flavour;
374 typedef struct bfd_mach_o_thread_command
376 unsigned long nflavours;
377 bfd_mach_o_thread_flavour *flavours;
380 bfd_mach_o_thread_command;
382 /* For LC_LOAD_DYLINKER and LC_ID_DYLINKER. */
384 typedef struct bfd_mach_o_dylinker_command
386 unsigned long name_offset; /* Offset to library's path name. */
387 unsigned long name_len; /* Offset to library's path name. */
390 bfd_mach_o_dylinker_command;
392 /* For LC_LOAD_DYLIB, LC_LOAD_WEAK_DYLIB, LC_ID_DYLIB
393 or LC_REEXPORT_DYLIB. */
395 typedef struct bfd_mach_o_dylib_command
397 unsigned long name_offset; /* Offset to library's path name. */
398 unsigned long name_len; /* Offset to library's path name. */
399 unsigned long timestamp; /* Library's build time stamp. */
400 unsigned long current_version; /* Library's current version number. */
401 unsigned long compatibility_version; /* Library's compatibility vers number. */
404 bfd_mach_o_dylib_command;
406 /* For LC_PREBOUND_DYLIB. */
408 typedef struct bfd_mach_o_prebound_dylib_command
410 unsigned long name; /* Library's path name. */
411 unsigned long nmodules; /* Number of modules in library. */
412 unsigned long linked_modules; /* Bit vector of linked modules. */
414 bfd_mach_o_prebound_dylib_command;
418 typedef struct bfd_mach_o_uuid_command
420 unsigned char uuid[16];
422 bfd_mach_o_uuid_command;
424 /* For LC_CODE_SIGNATURE or LC_SEGMENT_SPLIT_INFO. */
426 typedef struct bfd_mach_o_linkedit_command
428 unsigned long dataoff;
429 unsigned long datasize;
431 bfd_mach_o_linkedit_command;
433 typedef struct bfd_mach_o_str_command
435 unsigned long stroff;
436 unsigned long str_len;
439 bfd_mach_o_str_command;
441 typedef struct bfd_mach_o_dyld_info_command
443 /* File offset and size to rebase info. */
444 unsigned int rebase_off;
445 unsigned int rebase_size;
447 /* File offset and size of binding info. */
448 unsigned int bind_off;
449 unsigned int bind_size;
451 /* File offset and size of weak binding info. */
452 unsigned int weak_bind_off;
453 unsigned int weak_bind_size;
455 /* File offset and size of lazy binding info. */
456 unsigned int lazy_bind_off;
457 unsigned int lazy_bind_size;
459 /* File offset and size of export info. */
460 unsigned int export_off;
461 unsigned int export_size;
463 bfd_mach_o_dyld_info_command;
465 typedef struct bfd_mach_o_version_min_command
470 unsigned int reserved;
472 bfd_mach_o_version_min_command;
474 typedef struct bfd_mach_o_load_command
476 bfd_mach_o_load_command_type type;
477 bfd_boolean type_required;
482 bfd_mach_o_segment_command segment;
483 bfd_mach_o_symtab_command symtab;
484 bfd_mach_o_dysymtab_command dysymtab;
485 bfd_mach_o_thread_command thread;
486 bfd_mach_o_dylib_command dylib;
487 bfd_mach_o_dylinker_command dylinker;
488 bfd_mach_o_prebound_dylib_command prebound_dylib;
489 bfd_mach_o_uuid_command uuid;
490 bfd_mach_o_linkedit_command linkedit;
491 bfd_mach_o_str_command str;
492 bfd_mach_o_dyld_info_command dyld_info;
493 bfd_mach_o_version_min_command version_min;
497 bfd_mach_o_load_command;
499 typedef struct mach_o_data_struct
502 bfd_mach_o_header header;
503 /* Array of load commands (length is given by header.ncmds). */
504 bfd_mach_o_load_command *commands;
506 /* Flatten array of sections. The array is 0-based. */
507 unsigned long nsects;
508 bfd_mach_o_section **sections;
510 /* Used while writting: current length of the output file. This is used
511 to allocate space in the file. */
514 /* As symtab is referenced by other load command, it is handy to have
515 a direct access to it. Although it is not clearly stated, only one symtab
517 bfd_mach_o_symtab_command *symtab;
518 bfd_mach_o_dysymtab_command *dysymtab;
520 /* A place to stash dwarf2 info for this bfd. */
521 void *dwarf2_find_line_info;
523 /* Cache of dynamic relocs. */
524 arelent *dyn_reloc_cache;
526 bfd_mach_o_data_struct;
528 typedef struct bfd_mach_o_xlat_name
533 bfd_mach_o_xlat_name;
535 /* Target specific routines. */
537 #define bfd_mach_o_get_data(abfd) ((abfd)->tdata.mach_o_data)
538 #define bfd_mach_o_get_backend_data(abfd) \
539 ((bfd_mach_o_backend_data*)(abfd)->xvec->backend_data)
541 /* Get the Mach-O header for section SEC. */
542 #define bfd_mach_o_get_mach_o_section(sec) \
543 ((bfd_mach_o_section *)(sec)->used_by_bfd)
545 bfd_boolean bfd_mach_o_valid (bfd *);
546 bfd_boolean bfd_mach_o_mkobject_init (bfd *);
547 const bfd_target *bfd_mach_o_object_p (bfd *);
548 const bfd_target *bfd_mach_o_core_p (bfd *);
549 const bfd_target *bfd_mach_o_archive_p (bfd *);
550 bfd *bfd_mach_o_openr_next_archived_file (bfd *, bfd *);
551 bfd_boolean bfd_mach_o_set_arch_mach (bfd *, enum bfd_architecture,
553 int bfd_mach_o_lookup_command (bfd *, bfd_mach_o_load_command_type, bfd_mach_o_load_command **);
554 bfd_boolean bfd_mach_o_new_section_hook (bfd *, asection *);
555 bfd_boolean bfd_mach_o_write_contents (bfd *);
556 bfd_boolean bfd_mach_o_bfd_copy_private_symbol_data (bfd *, asymbol *,
558 bfd_boolean bfd_mach_o_bfd_copy_private_section_data (bfd *, asection *,
560 bfd_boolean bfd_mach_o_bfd_copy_private_bfd_data (bfd *, bfd *);
561 bfd_boolean bfd_mach_o_bfd_set_private_flags (bfd *, flagword);
562 long bfd_mach_o_get_symtab_upper_bound (bfd *);
563 long bfd_mach_o_canonicalize_symtab (bfd *, asymbol **);
564 long bfd_mach_o_get_synthetic_symtab (bfd *, long, asymbol **, long,
565 asymbol **, asymbol **ret);
566 long bfd_mach_o_get_reloc_upper_bound (bfd *, asection *);
567 long bfd_mach_o_canonicalize_reloc (bfd *, asection *, arelent **, asymbol **);
568 long bfd_mach_o_get_dynamic_reloc_upper_bound (bfd *);
569 long bfd_mach_o_canonicalize_dynamic_reloc (bfd *, arelent **, asymbol **);
570 asymbol *bfd_mach_o_make_empty_symbol (bfd *);
571 void bfd_mach_o_get_symbol_info (bfd *, asymbol *, symbol_info *);
572 void bfd_mach_o_print_symbol (bfd *, PTR, asymbol *, bfd_print_symbol_type);
573 int bfd_mach_o_sizeof_headers (bfd *, struct bfd_link_info *);
574 unsigned long bfd_mach_o_stack_addr (enum bfd_mach_o_cpu_type);
575 int bfd_mach_o_core_fetch_environment (bfd *, unsigned char **, unsigned int *);
576 char *bfd_mach_o_core_file_failing_command (bfd *);
577 int bfd_mach_o_core_file_failing_signal (bfd *);
578 bfd_boolean bfd_mach_o_core_file_matches_executable_p (bfd *, bfd *);
579 bfd *bfd_mach_o_fat_extract (bfd *, bfd_format , const bfd_arch_info_type *);
580 const bfd_target *bfd_mach_o_header_p (bfd *, bfd_mach_o_filetype,
581 bfd_mach_o_cpu_type);
582 bfd_boolean bfd_mach_o_build_commands (bfd *);
583 bfd_boolean bfd_mach_o_set_section_contents (bfd *, asection *, const void *,
584 file_ptr, bfd_size_type);
585 unsigned int bfd_mach_o_version (bfd *);
587 unsigned int bfd_mach_o_get_section_type_from_name (const char *);
588 unsigned int bfd_mach_o_get_section_attribute_from_name (const char *);
590 void bfd_mach_o_convert_section_name_to_bfd (bfd *, const char *, const char *,
591 const char **, flagword *);
592 bfd_boolean bfd_mach_o_find_nearest_line (bfd *, asection *, asymbol **,
593 bfd_vma, const char **,
594 const char **, unsigned int *);
595 bfd_boolean bfd_mach_o_close_and_cleanup (bfd *);
596 bfd_boolean bfd_mach_o_free_cached_info (bfd *);
598 unsigned int bfd_mach_o_section_get_nbr_indirect (bfd *, bfd_mach_o_section *);
599 unsigned int bfd_mach_o_section_get_entry_size (bfd *, bfd_mach_o_section *);
600 bfd_boolean bfd_mach_o_read_symtab_symbols (bfd *);
601 bfd_boolean bfd_mach_o_read_symtab_strtab (bfd *abfd);
603 extern const bfd_mach_o_xlat_name bfd_mach_o_section_attribute_name[];
604 extern const bfd_mach_o_xlat_name bfd_mach_o_section_type_name[];
606 extern const bfd_target mach_o_fat_vec;
608 /* Interfaces between BFD names and Mach-O names. */
610 typedef struct mach_o_section_name_xlat
612 const char *bfd_name;
613 const char *mach_o_name;
615 unsigned int macho_sectype;
616 unsigned int macho_secattr;
617 unsigned int sectalign;
618 } mach_o_section_name_xlat;
620 typedef struct mach_o_segment_name_xlat
623 const mach_o_section_name_xlat *sections;
624 } mach_o_segment_name_xlat;
626 const mach_o_section_name_xlat *
627 bfd_mach_o_section_data_for_mach_sect (bfd *, const char *, const char *);
628 const mach_o_section_name_xlat *
629 bfd_mach_o_section_data_for_bfd_name (bfd *, const char *, const char **);
631 typedef struct bfd_mach_o_backend_data
633 enum bfd_architecture arch;
634 bfd_boolean (*_bfd_mach_o_swap_reloc_in)(arelent *, bfd_mach_o_reloc_info *);
635 bfd_boolean (*_bfd_mach_o_swap_reloc_out)(arelent *, bfd_mach_o_reloc_info *);
636 bfd_boolean (*_bfd_mach_o_print_thread)(bfd *, bfd_mach_o_thread_flavour *,
638 const mach_o_segment_name_xlat *segsec_names_xlat;
640 bfd_mach_o_backend_data;
642 #endif /* _BFD_MACH_O_H_ */