bfd/
[deliverable/binutils-gdb.git] / bfd / mach-o.h
1 /* Mach-O support for BFD.
2 Copyright 1999, 2000, 2001, 2002, 2003, 2005, 2007, 2008, 2009
3 Free Software Foundation, Inc.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
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.
11
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.
16
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. */
21
22 #ifndef _BFD_MACH_O_H_
23 #define _BFD_MACH_O_H_
24
25 #include "bfd.h"
26 #include "mach-o/loader.h"
27
28 typedef struct bfd_mach_o_header
29 {
30 unsigned long magic;
31 unsigned long cputype;
32 unsigned long cpusubtype;
33 unsigned long filetype;
34 unsigned long ncmds;
35 unsigned long sizeofcmds;
36 unsigned long flags;
37 unsigned int reserved;
38 /* Version 1: 32 bits, version 2: 64 bits. */
39 unsigned int version;
40 enum bfd_endian byteorder;
41 }
42 bfd_mach_o_header;
43
44 #define BFD_MACH_O_SEGNAME_SIZE 16
45 #define BFD_MACH_O_SECTNAME_SIZE 16
46
47 typedef struct bfd_mach_o_section
48 {
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];
52 bfd_vma addr;
53 bfd_vma size;
54 bfd_vma offset;
55 unsigned long align;
56 bfd_vma reloff;
57 unsigned long nreloc;
58 unsigned long flags;
59 unsigned long reserved1;
60 unsigned long reserved2;
61 unsigned long reserved3;
62
63 /* Corresponding bfd section. */
64 asection *bfdsection;
65
66 /* Simply linked list. */
67 struct bfd_mach_o_section *next;
68 }
69 bfd_mach_o_section;
70
71 typedef struct bfd_mach_o_segment_command
72 {
73 char segname[16 + 1];
74 bfd_vma vmaddr;
75 bfd_vma vmsize;
76 bfd_vma fileoff;
77 unsigned long filesize;
78 unsigned long maxprot; /* Maximum permitted protection. */
79 unsigned long initprot; /* Initial protection. */
80 unsigned long nsects;
81 unsigned long flags;
82
83 /* Linked list of sections. */
84 bfd_mach_o_section *sect_head;
85 bfd_mach_o_section *sect_tail;
86 }
87 bfd_mach_o_segment_command;
88
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
93
94 /* Expanded internal representation of a relocation entry. */
95 typedef struct bfd_mach_o_reloc_info
96 {
97 bfd_vma r_address;
98 bfd_vma r_value;
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;
104 }
105 bfd_mach_o_reloc_info;
106
107 typedef struct bfd_mach_o_asymbol
108 {
109 /* The actual symbol which the rest of BFD works with. */
110 asymbol symbol;
111
112 /* Fields from Mach-O symbol. */
113 unsigned char n_type;
114 unsigned char n_sect;
115 unsigned short n_desc;
116 }
117 bfd_mach_o_asymbol;
118
119 typedef struct bfd_mach_o_symtab_command
120 {
121 unsigned int symoff;
122 unsigned int nsyms;
123 unsigned int stroff;
124 unsigned int strsize;
125 bfd_mach_o_asymbol *symbols;
126 char *strtab;
127 }
128 bfd_mach_o_symtab_command;
129
130 /* This is the second set of the symbolic information which is used to support
131 the data structures for the dynamically link editor.
132
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
141 of symbols.
142
143 This load command contains a the offsets and sizes of the following new
144 symbolic information tables:
145 table of contents
146 module table
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
158
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. */
166
167 typedef struct bfd_mach_o_dylib_module
168 {
169 /* Index into the string table indicating the name of the module. */
170 unsigned long module_name_idx;
171 char *module_name;
172
173 /* Index into the symbol table of the first defined external symbol provided
174 by the module. */
175 unsigned long iextdefsym;
176
177 /* Number of external symbols provided by this module. */
178 unsigned long nextdefsym;
179
180 /* Index into the external reference table of the first entry
181 provided by this module. */
182 unsigned long irefsym;
183
184 /* Number of external reference entries provided by this module. */
185 unsigned long nrefsym;
186
187 /* Index into the symbol table of the first local symbol provided by this
188 module. */
189 unsigned long ilocalsym;
190
191 /* Number of local symbols provided by this module. */
192 unsigned long nlocalsym;
193
194 /* Index into the external relocation table of the first entry provided
195 by this module. */
196 unsigned long iextrel;
197
198 /* Number of external relocation entries provided by this module. */
199 unsigned long nextrel;
200
201 /* Index in the module initialization section to the pointers for this
202 module. */
203 unsigned short iinit;
204
205 /* Index in the module termination section to the pointers for this
206 module. */
207 unsigned short iterm;
208
209 /* Number of pointers in the module initialization for this module. */
210 unsigned short ninit;
211
212 /* Number of pointers in the module termination for this module. */
213 unsigned short nterm;
214
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;
218
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;
222 }
223 bfd_mach_o_dylib_module;
224
225 typedef struct bfd_mach_o_dylib_table_of_content
226 {
227 /* Index into the symbol table to the defined external symbol. */
228 unsigned long symbol_index;
229
230 /* Index into the module table to the module for this entry. */
231 unsigned long module_index;
232 }
233 bfd_mach_o_dylib_table_of_content;
234
235 typedef struct bfd_mach_o_dylib_reference
236 {
237 /* Index into the symbol table for the symbol being referenced. */
238 unsigned long isym;
239
240 /* Type of the reference being made (use REFERENCE_FLAGS constants). */
241 unsigned long flags;
242 }
243 bfd_mach_o_dylib_reference;
244 #define BFD_MACH_O_REFERENCE_SIZE 4
245
246 typedef struct bfd_mach_o_dysymtab_command
247 {
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)
252 undefined symbols
253
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.
257
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). */
261
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. */
268
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. */
275
276 unsigned long tocoff; /* File offset to table of contents. */
277 unsigned long ntoc; /* Number of entries in table of contents. */
278
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. */
286
287 unsigned long modtaboff; /* File offset to module table. */
288 unsigned long nmodtab; /* Number of module table entries. */
289
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. */
297
298 unsigned long extrefsymoff; /* Offset to referenced symbol table. */
299 unsigned long nextrefsyms; /* Number of referenced symbol table entries. */
300
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. */
309
310 unsigned long indirectsymoff; /* File offset to the indirect symbol table. */
311 unsigned long nindirectsyms; /* Number of indirect symbol table entries. */
312
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
322 set to zero).
323
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.
328
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.
332
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). */
336
337 unsigned long extreloff; /* Offset to external relocation entries. */
338 unsigned long nextrel; /* Number of external relocation entries. */
339
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). */
343
344 unsigned long locreloff; /* Offset to local relocation entries. */
345 unsigned long nlocrel; /* Number of local relocation entries. */
346
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;
351 }
352 bfd_mach_o_dysymtab_command;
353
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. */
359
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
363
364 /* For LC_THREAD or LC_UNIXTHREAD. */
365
366 typedef struct bfd_mach_o_thread_flavour
367 {
368 unsigned long flavour;
369 unsigned long offset;
370 unsigned long size;
371 }
372 bfd_mach_o_thread_flavour;
373
374 typedef struct bfd_mach_o_thread_command
375 {
376 unsigned long nflavours;
377 bfd_mach_o_thread_flavour *flavours;
378 asection *section;
379 }
380 bfd_mach_o_thread_command;
381
382 /* For LC_LOAD_DYLINKER and LC_ID_DYLINKER. */
383
384 typedef struct bfd_mach_o_dylinker_command
385 {
386 unsigned long name_offset; /* Offset to library's path name. */
387 unsigned long name_len; /* Offset to library's path name. */
388 char *name_str;
389 }
390 bfd_mach_o_dylinker_command;
391
392 /* For LC_LOAD_DYLIB, LC_LOAD_WEAK_DYLIB, LC_ID_DYLIB
393 or LC_REEXPORT_DYLIB. */
394
395 typedef struct bfd_mach_o_dylib_command
396 {
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. */
402 char *name_str;
403 }
404 bfd_mach_o_dylib_command;
405
406 /* For LC_PREBOUND_DYLIB. */
407
408 typedef struct bfd_mach_o_prebound_dylib_command
409 {
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. */
413 }
414 bfd_mach_o_prebound_dylib_command;
415
416 /* For LC_UUID. */
417
418 typedef struct bfd_mach_o_uuid_command
419 {
420 unsigned char uuid[16];
421 }
422 bfd_mach_o_uuid_command;
423
424 /* For LC_CODE_SIGNATURE or LC_SEGMENT_SPLIT_INFO. */
425
426 typedef struct bfd_mach_o_linkedit_command
427 {
428 unsigned long dataoff;
429 unsigned long datasize;
430 }
431 bfd_mach_o_linkedit_command;
432
433 typedef struct bfd_mach_o_str_command
434 {
435 unsigned long stroff;
436 unsigned long str_len;
437 char *str;
438 }
439 bfd_mach_o_str_command;
440
441 typedef struct bfd_mach_o_dyld_info_command
442 {
443 /* File offset and size to rebase info. */
444 unsigned int rebase_off;
445 unsigned int rebase_size;
446
447 /* File offset and size of binding info. */
448 unsigned int bind_off;
449 unsigned int bind_size;
450
451 /* File offset and size of weak binding info. */
452 unsigned int weak_bind_off;
453 unsigned int weak_bind_size;
454
455 /* File offset and size of lazy binding info. */
456 unsigned int lazy_bind_off;
457 unsigned int lazy_bind_size;
458
459 /* File offset and size of export info. */
460 unsigned int export_off;
461 unsigned int export_size;
462 }
463 bfd_mach_o_dyld_info_command;
464
465 typedef struct bfd_mach_o_version_min_command
466 {
467 unsigned char rel;
468 unsigned char maj;
469 unsigned char min;
470 unsigned int reserved;
471 }
472 bfd_mach_o_version_min_command;
473
474 typedef struct bfd_mach_o_load_command
475 {
476 bfd_mach_o_load_command_type type;
477 bfd_boolean type_required;
478 unsigned int offset;
479 unsigned int len;
480 union
481 {
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;
494 }
495 command;
496 }
497 bfd_mach_o_load_command;
498
499 typedef struct mach_o_data_struct
500 {
501 /* Mach-O header. */
502 bfd_mach_o_header header;
503 /* Array of load commands (length is given by header.ncmds). */
504 bfd_mach_o_load_command *commands;
505
506 /* Flatten array of sections. The array is 0-based. */
507 unsigned long nsects;
508 bfd_mach_o_section **sections;
509
510 /* Used while writting: current length of the output file. This is used
511 to allocate space in the file. */
512 ufile_ptr filelen;
513
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
516 is expected. */
517 bfd_mach_o_symtab_command *symtab;
518 bfd_mach_o_dysymtab_command *dysymtab;
519 }
520 bfd_mach_o_data_struct;
521
522 typedef struct bfd_mach_o_xlat_name
523 {
524 const char *name;
525 unsigned long val;
526 }
527 bfd_mach_o_xlat_name;
528
529 /* Target specific routines. */
530 typedef struct bfd_mach_o_backend_data
531 {
532 enum bfd_architecture arch;
533 bfd_boolean (*_bfd_mach_o_swap_reloc_in)(arelent *, bfd_mach_o_reloc_info *);
534 bfd_boolean (*_bfd_mach_o_swap_reloc_out)(arelent *, bfd_mach_o_reloc_info *);
535 bfd_boolean (*_bfd_mach_o_print_thread)(bfd *, bfd_mach_o_thread_flavour *,
536 void *, char *);
537 }
538 bfd_mach_o_backend_data;
539
540 #define bfd_mach_o_get_data(abfd) ((abfd)->tdata.mach_o_data)
541 #define bfd_mach_o_get_backend_data(abfd) \
542 ((bfd_mach_o_backend_data*)(abfd)->xvec->backend_data)
543
544 /* Get the Mach-O header for section SEC. */
545 #define bfd_mach_o_get_mach_o_section(sec) \
546 ((bfd_mach_o_section *)(sec)->used_by_bfd)
547
548 bfd_boolean bfd_mach_o_valid (bfd *);
549 bfd_boolean bfd_mach_o_mkobject_init (bfd *);
550 const bfd_target *bfd_mach_o_object_p (bfd *);
551 const bfd_target *bfd_mach_o_core_p (bfd *);
552 const bfd_target *bfd_mach_o_archive_p (bfd *);
553 bfd *bfd_mach_o_openr_next_archived_file (bfd *, bfd *);
554 bfd_boolean bfd_mach_o_set_arch_mach (bfd *, enum bfd_architecture,
555 unsigned long);
556 int bfd_mach_o_lookup_command (bfd *, bfd_mach_o_load_command_type, bfd_mach_o_load_command **);
557 bfd_boolean bfd_mach_o_new_section_hook (bfd *, asection *);
558 bfd_boolean bfd_mach_o_write_contents (bfd *);
559 bfd_boolean bfd_mach_o_bfd_copy_private_symbol_data (bfd *, asymbol *,
560 bfd *, asymbol *);
561 bfd_boolean bfd_mach_o_bfd_copy_private_section_data (bfd *, asection *,
562 bfd *, asection *);
563 bfd_boolean bfd_mach_o_bfd_copy_private_bfd_data (bfd *, bfd *);
564 long bfd_mach_o_get_symtab_upper_bound (bfd *);
565 long bfd_mach_o_canonicalize_symtab (bfd *, asymbol **);
566 long bfd_mach_o_get_synthetic_symtab (bfd *, long, asymbol **, long,
567 asymbol **, asymbol **ret);
568 long bfd_mach_o_get_reloc_upper_bound (bfd *, asection *);
569 long bfd_mach_o_canonicalize_reloc (bfd *, asection *, arelent **, asymbol **);
570 long bfd_mach_o_get_dynamic_reloc_upper_bound (bfd *);
571 long bfd_mach_o_canonicalize_dynamic_reloc (bfd *, arelent **, asymbol **);
572 asymbol *bfd_mach_o_make_empty_symbol (bfd *);
573 void bfd_mach_o_get_symbol_info (bfd *, asymbol *, symbol_info *);
574 void bfd_mach_o_print_symbol (bfd *, PTR, asymbol *, bfd_print_symbol_type);
575 int bfd_mach_o_sizeof_headers (bfd *, struct bfd_link_info *);
576 unsigned long bfd_mach_o_stack_addr (enum bfd_mach_o_cpu_type);
577 int bfd_mach_o_core_fetch_environment (bfd *, unsigned char **, unsigned int *);
578 char *bfd_mach_o_core_file_failing_command (bfd *);
579 int bfd_mach_o_core_file_failing_signal (bfd *);
580 bfd_boolean bfd_mach_o_core_file_matches_executable_p (bfd *, bfd *);
581 bfd *bfd_mach_o_fat_extract (bfd *, bfd_format , const bfd_arch_info_type *);
582 const bfd_target *bfd_mach_o_header_p (bfd *, bfd_mach_o_filetype,
583 bfd_mach_o_cpu_type);
584 bfd_boolean bfd_mach_o_build_commands (bfd *);
585 bfd_boolean bfd_mach_o_set_section_contents (bfd *, asection *, const void *,
586 file_ptr, bfd_size_type);
587 unsigned int bfd_mach_o_version (bfd *);
588
589 unsigned int bfd_mach_o_get_section_type_from_name (const char *);
590 unsigned int bfd_mach_o_get_section_attribute_from_name (const char *);
591 void bfd_mach_o_normalize_section_name (const char *, const char *,
592 const char **, flagword *);
593
594 unsigned int bfd_mach_o_section_get_nbr_indirect (bfd *, bfd_mach_o_section *);
595 unsigned int bfd_mach_o_section_get_entry_size (bfd *, bfd_mach_o_section *);
596 bfd_boolean bfd_mach_o_read_symtab_symbols (bfd *);
597 bfd_boolean bfd_mach_o_read_symtab_strtab (bfd *abfd);
598
599 extern const bfd_mach_o_xlat_name bfd_mach_o_section_attribute_name[];
600 extern const bfd_mach_o_xlat_name bfd_mach_o_section_type_name[];
601
602 extern const bfd_target mach_o_fat_vec;
603
604 #endif /* _BFD_MACH_O_H_ */
This page took 0.042676 seconds and 5 git commands to generate.