bfd/
[external/binutils.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 typedef struct bfd_mach_o_section
45 {
46   /* Fields present in the file.  */
47   char sectname[16 + 1];
48   char segname[16 + 1];
49   bfd_vma addr;
50   bfd_vma size;
51   bfd_vma offset;
52   unsigned long align;
53   bfd_vma reloff;
54   unsigned long nreloc;
55   unsigned long flags;
56   unsigned long reserved1;
57   unsigned long reserved2;
58   unsigned long reserved3;
59
60   /* Corresponding bfd section.  */
61   asection *bfdsection;
62 }
63 bfd_mach_o_section;
64
65 typedef struct bfd_mach_o_segment_command
66 {
67   char segname[16 + 1];
68   bfd_vma vmaddr;
69   bfd_vma vmsize;
70   bfd_vma fileoff;
71   unsigned long filesize;
72   unsigned long maxprot;        /* Maximum permitted protection.  */
73   unsigned long initprot;       /* Initial protection.  */
74   unsigned long nsects;
75   unsigned long flags;
76   bfd_mach_o_section *sections;
77 }
78 bfd_mach_o_segment_command;
79
80 /* Protection flags.  */
81 #define BFD_MACH_O_PROT_READ    0x01
82 #define BFD_MACH_O_PROT_WRITE   0x02
83 #define BFD_MACH_O_PROT_EXECUTE 0x04
84
85 /* Expanded internal representation of a relocation entry.  */
86 typedef struct bfd_mach_o_reloc_info
87 {
88   bfd_vma r_address;
89   bfd_vma r_value;
90   unsigned int r_scattered : 1;
91   unsigned int r_type : 4;
92   unsigned int r_pcrel : 1;
93   unsigned int r_length : 2;
94   unsigned int r_extern : 1;
95 }
96 bfd_mach_o_reloc_info;
97
98 typedef struct bfd_mach_o_asymbol
99 {
100   /* The actual symbol which the rest of BFD works with.  */
101   asymbol symbol;
102
103   /* Fields from Mach-O symbol.  */
104   unsigned char n_type;
105   unsigned char n_sect;
106   unsigned short n_desc;
107 }
108 bfd_mach_o_asymbol;
109
110 typedef struct bfd_mach_o_symtab_command
111 {
112   unsigned int symoff;
113   unsigned int nsyms;
114   unsigned int stroff;
115   unsigned int strsize;
116   bfd_mach_o_asymbol *symbols;
117   char *strtab;
118 }
119 bfd_mach_o_symtab_command;
120
121 /* This is the second set of the symbolic information which is used to support
122    the data structures for the dynamically link editor.
123
124    The original set of symbolic information in the symtab_command which contains
125    the symbol and string tables must also be present when this load command is
126    present.  When this load command is present the symbol table is organized
127    into three groups of symbols:
128        local symbols (static and debugging symbols) - grouped by module
129        defined external symbols - grouped by module (sorted by name if not lib)
130        undefined external symbols (sorted by name)
131    In this load command there are offsets and counts to each of the three groups
132    of symbols.
133
134    This load command contains a the offsets and sizes of the following new
135    symbolic information tables:
136        table of contents
137        module table
138        reference symbol table
139        indirect symbol table
140    The first three tables above (the table of contents, module table and
141    reference symbol table) are only present if the file is a dynamically linked
142    shared library.  For executable and object modules, which are files
143    containing only one module, the information that would be in these three
144    tables is determined as follows:
145        table of contents - the defined external symbols are sorted by name
146        module table - the file contains only one module so everything in the
147                       file is part of the module.
148        reference symbol table - is the defined and undefined external symbols
149
150    For dynamically linked shared library files this load command also contains
151    offsets and sizes to the pool of relocation entries for all sections
152    separated into two groups:
153        external relocation entries
154        local relocation entries
155    For executable and object modules the relocation entries continue to hang
156    off the section structures.  */
157
158 typedef struct bfd_mach_o_dylib_module
159 {
160   /* Index into the string table indicating the name of the module.  */
161   unsigned long module_name_idx;
162   char *module_name;
163
164   /* Index into the symbol table of the first defined external symbol provided
165      by the module.  */
166   unsigned long iextdefsym;
167
168   /* Number of external symbols provided by this module.  */
169   unsigned long nextdefsym;
170
171   /* Index into the external reference table of the first entry
172      provided by this module.  */
173   unsigned long irefsym;
174
175   /* Number of external reference entries provided by this module.  */
176   unsigned long nrefsym;
177
178   /* Index into the symbol table of the first local symbol provided by this
179      module.  */
180   unsigned long ilocalsym;
181
182   /* Number of local symbols provided by this module.  */
183   unsigned long nlocalsym;
184
185   /* Index into the external relocation table of the first entry provided
186      by this module.  */
187   unsigned long iextrel;
188
189   /* Number of external relocation entries provided by this module.  */
190   unsigned long nextrel;
191
192   /* Index in the module initialization section to the pointers for this
193      module.  */
194   unsigned short iinit;
195
196   /* Index in the module termination section to the pointers for this
197      module.  */
198   unsigned short iterm;
199
200   /* Number of pointers in the module initialization for this module.  */
201   unsigned short ninit;
202
203   /* Number of pointers in the module termination for this module.  */
204   unsigned short nterm;
205
206   /* Number of data byte for this module that are used in the __module_info
207      section of the __OBJC segment.  */
208   unsigned long objc_module_info_size;
209
210   /* Statically linked address of the start of the data for this module
211      in the __module_info section of the __OBJC_segment.  */
212   bfd_vma objc_module_info_addr;
213 }
214 bfd_mach_o_dylib_module;
215
216 typedef struct bfd_mach_o_dylib_table_of_content
217 {
218   /* Index into the symbol table to the defined external symbol.  */
219   unsigned long symbol_index;
220
221   /* Index into the module table to the module for this entry.  */
222   unsigned long module_index;
223 }
224 bfd_mach_o_dylib_table_of_content;
225
226 typedef struct bfd_mach_o_dylib_reference
227 {
228   /* Index into the symbol table for the symbol being referenced.  */
229   unsigned long isym;
230
231   /* Type of the reference being made (use REFERENCE_FLAGS constants).  */
232   unsigned long flags;
233 }
234 bfd_mach_o_dylib_reference;
235 #define BFD_MACH_O_REFERENCE_SIZE 4
236
237 typedef struct bfd_mach_o_dysymtab_command
238 {
239   /* The symbols indicated by symoff and nsyms of the LC_SYMTAB load command
240      are grouped into the following three groups:
241        local symbols (further grouped by the module they are from)
242        defined external symbols (further grouped by the module they are from)
243        undefined symbols
244
245      The local symbols are used only for debugging.  The dynamic binding
246      process may have to use them to indicate to the debugger the local
247      symbols for a module that is being bound.
248
249      The last two groups are used by the dynamic binding process to do the
250      binding (indirectly through the module table and the reference symbol
251      table when this is a dynamically linked shared library file).  */
252
253   unsigned long ilocalsym;    /* Index to local symbols.  */
254   unsigned long nlocalsym;    /* Number of local symbols.  */
255   unsigned long iextdefsym;   /* Index to externally defined symbols.  */
256   unsigned long nextdefsym;   /* Number of externally defined symbols.  */
257   unsigned long iundefsym;    /* Index to undefined symbols.  */
258   unsigned long nundefsym;    /* Number of undefined symbols.  */
259
260   /* For the for the dynamic binding process to find which module a symbol
261      is defined in the table of contents is used (analogous to the ranlib
262      structure in an archive) which maps defined external symbols to modules
263      they are defined in.  This exists only in a dynamically linked shared
264      library file.  For executable and object modules the defined external
265      symbols are sorted by name and is use as the table of contents.  */
266
267   unsigned long tocoff;       /* File offset to table of contents.  */
268   unsigned long ntoc;         /* Number of entries in table of contents.  */
269
270   /* To support dynamic binding of "modules" (whole object files) the symbol
271      table must reflect the modules that the file was created from.  This is
272      done by having a module table that has indexes and counts into the merged
273      tables for each module.  The module structure that these two entries
274      refer to is described below.  This exists only in a dynamically linked
275      shared library file.  For executable and object modules the file only
276      contains one module so everything in the file belongs to the module.  */
277
278   unsigned long modtaboff;    /* File offset to module table.  */
279   unsigned long nmodtab;      /* Number of module table entries.  */
280
281   /* To support dynamic module binding the module structure for each module
282      indicates the external references (defined and undefined) each module
283      makes.  For each module there is an offset and a count into the
284      reference symbol table for the symbols that the module references.
285      This exists only in a dynamically linked shared library file.  For
286      executable and object modules the defined external symbols and the
287      undefined external symbols indicates the external references.  */
288
289   unsigned long extrefsymoff;  /* Offset to referenced symbol table.  */
290   unsigned long nextrefsyms;   /* Number of referenced symbol table entries.  */
291
292   /* The sections that contain "symbol pointers" and "routine stubs" have
293      indexes and (implied counts based on the size of the section and fixed
294      size of the entry) into the "indirect symbol" table for each pointer
295      and stub.  For every section of these two types the index into the
296      indirect symbol table is stored in the section header in the field
297      reserved1.  An indirect symbol table entry is simply a 32bit index into
298      the symbol table to the symbol that the pointer or stub is referring to.
299      The indirect symbol table is ordered to match the entries in the section.  */
300
301   unsigned long indirectsymoff; /* File offset to the indirect symbol table.  */
302   unsigned long nindirectsyms;  /* Number of indirect symbol table entries.  */
303
304   /* To support relocating an individual module in a library file quickly the
305      external relocation entries for each module in the library need to be
306      accessed efficiently.  Since the relocation entries can't be accessed
307      through the section headers for a library file they are separated into
308      groups of local and external entries further grouped by module.  In this
309      case the presents of this load command who's extreloff, nextrel,
310      locreloff and nlocrel fields are non-zero indicates that the relocation
311      entries of non-merged sections are not referenced through the section
312      structures (and the reloff and nreloc fields in the section headers are
313      set to zero).
314
315      Since the relocation entries are not accessed through the section headers
316      this requires the r_address field to be something other than a section
317      offset to identify the item to be relocated.  In this case r_address is
318      set to the offset from the vmaddr of the first LC_SEGMENT command.
319
320      The relocation entries are grouped by module and the module table
321      entries have indexes and counts into them for the group of external
322      relocation entries for that the module.
323
324      For sections that are merged across modules there must not be any
325      remaining external relocation entries for them (for merged sections
326      remaining relocation entries must be local).  */
327
328   unsigned long extreloff;    /* Offset to external relocation entries.  */
329   unsigned long nextrel;      /* Number of external relocation entries.  */
330
331   /* All the local relocation entries are grouped together (they are not
332      grouped by their module since they are only used if the object is moved
333      from it statically link edited address).  */
334
335   unsigned long locreloff;    /* Offset to local relocation entries.  */
336   unsigned long nlocrel;      /* Number of local relocation entries.  */
337
338   bfd_mach_o_dylib_module *dylib_module;
339   bfd_mach_o_dylib_table_of_content *dylib_toc;
340   unsigned int *indirect_syms;
341   bfd_mach_o_dylib_reference *ext_refs;
342 }
343 bfd_mach_o_dysymtab_command;
344
345 /* An indirect symbol table entry is simply a 32bit index into the symbol table
346    to the symbol that the pointer or stub is refering to.  Unless it is for a
347    non-lazy symbol pointer section for a defined symbol which strip(1) has
348    removed.  In which case it has the value INDIRECT_SYMBOL_LOCAL.  If the
349    symbol was also absolute INDIRECT_SYMBOL_ABS is or'ed with that.  */
350
351 #define BFD_MACH_O_INDIRECT_SYMBOL_LOCAL 0x80000000
352 #define BFD_MACH_O_INDIRECT_SYMBOL_ABS   0x40000000
353 #define BFD_MACH_O_INDIRECT_SYMBOL_SIZE  4
354
355 /* For LC_THREAD or LC_UNIXTHREAD.  */
356
357 typedef struct bfd_mach_o_thread_flavour
358 {
359   unsigned long flavour;
360   unsigned long offset;
361   unsigned long size;
362 }
363 bfd_mach_o_thread_flavour;
364
365 typedef struct bfd_mach_o_thread_command
366 {
367   unsigned long nflavours;
368   bfd_mach_o_thread_flavour *flavours;
369   asection *section;
370 }
371 bfd_mach_o_thread_command;
372
373 /* For LC_LOAD_DYLINKER and LC_ID_DYLINKER.  */
374
375 typedef struct bfd_mach_o_dylinker_command
376 {
377   unsigned long name_offset;         /* Offset to library's path name.  */
378   unsigned long name_len;            /* Offset to library's path name.  */
379   char *name_str;
380 }
381 bfd_mach_o_dylinker_command;
382
383 /* For LC_LOAD_DYLIB, LC_LOAD_WEAK_DYLIB, LC_ID_DYLIB
384    or LC_REEXPORT_DYLIB.  */
385
386 typedef struct bfd_mach_o_dylib_command
387 {
388   unsigned long name_offset;           /* Offset to library's path name.  */
389   unsigned long name_len;              /* Offset to library's path name.  */
390   unsigned long timestamp;             /* Library's build time stamp.  */
391   unsigned long current_version;       /* Library's current version number.  */
392   unsigned long compatibility_version; /* Library's compatibility vers number.  */
393   char *name_str;
394 }
395 bfd_mach_o_dylib_command;
396
397 /* For LC_PREBOUND_DYLIB.  */
398
399 typedef struct bfd_mach_o_prebound_dylib_command
400 {
401   unsigned long name;                /* Library's path name.  */
402   unsigned long nmodules;            /* Number of modules in library.  */
403   unsigned long linked_modules;      /* Bit vector of linked modules.  */
404 }
405 bfd_mach_o_prebound_dylib_command;
406
407 /* For LC_UUID.  */
408
409 typedef struct bfd_mach_o_uuid_command
410 {
411   unsigned char uuid[16];
412 }
413 bfd_mach_o_uuid_command;
414
415 /* For LC_CODE_SIGNATURE or LC_SEGMENT_SPLIT_INFO.  */
416
417 typedef struct bfd_mach_o_linkedit_command
418 {
419   unsigned long dataoff;
420   unsigned long datasize;
421 }
422 bfd_mach_o_linkedit_command;
423
424 typedef struct bfd_mach_o_str_command
425 {
426   unsigned long stroff;
427   unsigned long str_len;
428   char *str;
429 }
430 bfd_mach_o_str_command;
431
432 typedef struct bfd_mach_o_dyld_info_command
433 {
434   /* File offset and size to rebase info.  */
435   unsigned int rebase_off; 
436   unsigned int rebase_size;
437
438   /* File offset and size of binding info.  */
439   unsigned int bind_off;
440   unsigned int bind_size;
441
442   /* File offset and size of weak binding info.  */
443   unsigned int weak_bind_off;
444   unsigned int weak_bind_size;
445
446   /* File offset and size of lazy binding info.  */
447   unsigned int lazy_bind_off;
448   unsigned int lazy_bind_size;
449
450   /* File offset and size of export info.  */
451   unsigned int export_off;
452   unsigned int export_size;
453 }
454 bfd_mach_o_dyld_info_command;
455
456 typedef struct bfd_mach_o_load_command
457 {
458   bfd_mach_o_load_command_type type;
459   bfd_boolean type_required;
460   unsigned int offset;
461   unsigned int len;
462   union
463   {
464     bfd_mach_o_segment_command segment;
465     bfd_mach_o_symtab_command symtab;
466     bfd_mach_o_dysymtab_command dysymtab;
467     bfd_mach_o_thread_command thread;
468     bfd_mach_o_dylib_command dylib;
469     bfd_mach_o_dylinker_command dylinker;
470     bfd_mach_o_prebound_dylib_command prebound_dylib;
471     bfd_mach_o_uuid_command uuid;
472     bfd_mach_o_linkedit_command linkedit;
473     bfd_mach_o_str_command str;
474     bfd_mach_o_dyld_info_command dyld_info;
475   }
476   command;
477 }
478 bfd_mach_o_load_command;
479
480 typedef struct mach_o_data_struct
481 {
482   /* Mach-O header.  */
483   bfd_mach_o_header header;
484   /* Array of load commands (length is given by header.ncmds).  */
485   bfd_mach_o_load_command *commands;
486
487   /* Flatten array of sections.  The array is 0-based.  */
488   unsigned long nsects;
489   bfd_mach_o_section **sections;
490
491   /* Used while writting: current length of the output file.  This is used
492      to allocate space in the file.  */
493   ufile_ptr filelen;
494
495   /* As symtab is referenced by other load command, it is handy to have
496      a direct access to it.  Also it is not clearly stated, only one symtab
497      is expected.  */
498   bfd_mach_o_symtab_command *symtab;
499   bfd_mach_o_dysymtab_command *dysymtab;
500 }
501 bfd_mach_o_data_struct;
502
503 /* Target specific routines.  */
504 typedef struct bfd_mach_o_backend_data
505 {
506   enum bfd_architecture arch;
507   bfd_boolean (*_bfd_mach_o_swap_reloc_in)(arelent *, bfd_mach_o_reloc_info *);
508   bfd_boolean (*_bfd_mach_o_swap_reloc_out)(arelent *, bfd_mach_o_reloc_info *);
509   bfd_boolean (*_bfd_mach_o_print_thread)(bfd *, bfd_mach_o_thread_flavour *,
510                                           void *, char *);
511 }
512 bfd_mach_o_backend_data;
513
514 #define bfd_mach_o_get_data(abfd) ((abfd)->tdata.mach_o_data)
515 #define bfd_mach_o_get_backend_data(abfd) \
516   ((bfd_mach_o_backend_data*)(abfd)->xvec->backend_data)
517
518 bfd_boolean bfd_mach_o_valid (bfd *);
519 int bfd_mach_o_read_dysymtab_symbol (bfd *, bfd_mach_o_dysymtab_command *, bfd_mach_o_symtab_command *, bfd_mach_o_asymbol *, unsigned long);
520 int bfd_mach_o_scan_start_address (bfd *);
521 int bfd_mach_o_scan (bfd *, bfd_mach_o_header *, bfd_mach_o_data_struct *);
522 bfd_boolean bfd_mach_o_mkobject_init (bfd *);
523 const bfd_target *bfd_mach_o_object_p (bfd *);
524 const bfd_target *bfd_mach_o_core_p (bfd *);
525 const bfd_target *bfd_mach_o_archive_p (bfd *);
526 bfd *bfd_mach_o_openr_next_archived_file (bfd *, bfd *);
527 bfd_boolean bfd_mach_o_set_arch_mach (bfd *, enum bfd_architecture,
528                                       unsigned long);
529 int bfd_mach_o_lookup_section (bfd *, asection *, bfd_mach_o_load_command **, bfd_mach_o_section **);
530 int bfd_mach_o_lookup_command (bfd *, bfd_mach_o_load_command_type, bfd_mach_o_load_command **);
531 bfd_boolean bfd_mach_o_write_contents (bfd *);
532 bfd_boolean bfd_mach_o_bfd_copy_private_symbol_data (bfd *, asymbol *,
533                                                      bfd *, asymbol *);
534 bfd_boolean bfd_mach_o_bfd_copy_private_section_data (bfd *, asection *,
535                                                       bfd *, asection *);
536 bfd_boolean bfd_mach_o_bfd_copy_private_bfd_data (bfd *, bfd *);
537 long bfd_mach_o_get_symtab_upper_bound (bfd *);
538 long bfd_mach_o_canonicalize_symtab (bfd *, asymbol **);
539 long bfd_mach_o_get_synthetic_symtab (bfd *, long, asymbol **, long, 
540                                       asymbol **, asymbol **ret);
541 long bfd_mach_o_get_reloc_upper_bound (bfd *, asection *);
542 long bfd_mach_o_canonicalize_reloc (bfd *, asection *, arelent **, asymbol **);
543 long bfd_mach_o_get_dynamic_reloc_upper_bound (bfd *);
544 long bfd_mach_o_canonicalize_dynamic_reloc (bfd *, arelent **, asymbol **);
545 asymbol *bfd_mach_o_make_empty_symbol (bfd *);
546 void bfd_mach_o_get_symbol_info (bfd *, asymbol *, symbol_info *);
547 void bfd_mach_o_print_symbol (bfd *, PTR, asymbol *, bfd_print_symbol_type);
548 bfd_boolean bfd_mach_o_bfd_print_private_bfd_data (bfd *, PTR);
549 int bfd_mach_o_sizeof_headers (bfd *, struct bfd_link_info *);
550 unsigned long bfd_mach_o_stack_addr (enum bfd_mach_o_cpu_type);
551 int bfd_mach_o_core_fetch_environment (bfd *, unsigned char **, unsigned int *);
552 char *bfd_mach_o_core_file_failing_command (bfd *);
553 int bfd_mach_o_core_file_failing_signal (bfd *);
554 bfd_boolean bfd_mach_o_core_file_matches_executable_p (bfd *, bfd *);
555 bfd *bfd_mach_o_fat_extract (bfd *, bfd_format , const bfd_arch_info_type *);
556 const bfd_target *bfd_mach_o_header_p (bfd *, bfd_mach_o_filetype,
557                                        bfd_mach_o_cpu_type);
558 bfd_boolean bfd_mach_o_build_commands (bfd *);
559 bfd_boolean bfd_mach_o_set_section_contents (bfd *, asection *, const void *,
560                                              file_ptr, bfd_size_type);
561 unsigned int bfd_mach_o_version (bfd *);
562
563 unsigned int bfd_mach_o_get_section_type_from_name (const char *);
564 unsigned int bfd_mach_o_get_section_attribute_from_name (const char *);
565 void bfd_mach_o_normalize_section_name (const char *, const char *,
566                                         const char **, flagword *);
567
568 extern const bfd_target mach_o_fat_vec;
569
570 #endif /* _BFD_MACH_O_H_ */