1/* Mach-O support for BFD.
2   Copyright (C) 2011-2022 Free Software Foundation, Inc.
3
4   This file is part of BFD, the Binary File Descriptor library.
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 3 of the License, or
9   (at your option) any later version.
10
11   This program is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with this program; if not, write to the Free Software
18   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19   MA 02110-1301, USA.  */
20
21#ifndef _MACH_O_EXTERNAL_H
22#define _MACH_O_EXTERNAL_H
23
24struct mach_o_header_external
25{
26  unsigned char magic[4];	/* Magic number.  */
27  unsigned char cputype[4];	/* CPU that this object is for.  */
28  unsigned char cpusubtype[4];	/* CPU subtype.  */
29  unsigned char filetype[4];	/* Type of file.  */
30  unsigned char ncmds[4];	/* Number of load commands.  */
31  unsigned char sizeofcmds[4];	/* Total size of load commands.  */
32  unsigned char flags[4];	/* Flags.  */
33  unsigned char reserved[4];	/* Reserved (on 64-bit version only).  */
34};
35
36#define BFD_MACH_O_HEADER_SIZE 28
37#define BFD_MACH_O_HEADER_64_SIZE 32
38
39/* 32-bit section header.  */
40
41struct mach_o_section_32_external
42{
43  unsigned char sectname[16];   /* Section name.  */
44  unsigned char segname[16];    /* Segment that the section belongs to.  */
45  unsigned char addr[4];        /* Address of this section in memory.  */
46  unsigned char size[4];        /* Size in bytes of this section.  */
47  unsigned char offset[4];      /* File offset of this section.  */
48  unsigned char align[4];       /* log2 of this section's alignment.  */
49  unsigned char reloff[4];      /* File offset of this section's relocs.  */
50  unsigned char nreloc[4];      /* Number of relocs for this section.  */
51  unsigned char flags[4];       /* Section flags/attributes.  */
52  unsigned char reserved1[4];
53  unsigned char reserved2[4];
54};
55#define BFD_MACH_O_SECTION_SIZE 68
56
57/* 64-bit section header.  */
58
59struct mach_o_section_64_external
60{
61  unsigned char sectname[16];   /* Section name.  */
62  unsigned char segname[16];    /* Segment that the section belongs to.  */
63  unsigned char addr[8];        /* Address of this section in memory.  */
64  unsigned char size[8];        /* Size in bytes of this section.  */
65  unsigned char offset[4];      /* File offset of this section.  */
66  unsigned char align[4];       /* log2 of this section's alignment.  */
67  unsigned char reloff[4];      /* File offset of this section's relocs.  */
68  unsigned char nreloc[4];      /* Number of relocs for this section.  */
69  unsigned char flags[4];       /* Section flags/attributes.  */
70  unsigned char reserved1[4];
71  unsigned char reserved2[4];
72  unsigned char reserved3[4];
73};
74#define BFD_MACH_O_SECTION_64_SIZE 80
75
76struct mach_o_load_command_external
77{
78  unsigned char cmd[4];         /* The type of load command.  */
79  unsigned char cmdsize[4];     /* Size in bytes of entire command.  */
80};
81#define BFD_MACH_O_LC_SIZE 8
82
83struct mach_o_segment_command_32_external
84{
85  unsigned char segname[16];    /* Name of this segment.  */
86  unsigned char vmaddr[4];      /* Virtual memory address of this segment.  */
87  unsigned char vmsize[4];      /* Size there, in bytes.  */
88  unsigned char fileoff[4];     /* Offset in bytes of the data to be mapped.  */
89  unsigned char filesize[4];    /* Size in bytes on disk.  */
90  unsigned char maxprot[4];     /* Maximum permitted vm protection.  */
91  unsigned char initprot[4];    /* Initial vm protection.  */
92  unsigned char nsects[4];      /* Number of sections in this segment.  */
93  unsigned char flags[4];       /* Flags that affect the loading.  */
94};
95#define BFD_MACH_O_LC_SEGMENT_SIZE 56 /* Include the header.  */
96
97struct mach_o_segment_command_64_external
98{
99  unsigned char segname[16];    /* Name of this segment.  */
100  unsigned char vmaddr[8];      /* Virtual memory address of this segment.  */
101  unsigned char vmsize[8];      /* Size there, in bytes.  */
102  unsigned char fileoff[8];     /* Offset in bytes of the data to be mapped.  */
103  unsigned char filesize[8];    /* Size in bytes on disk.  */
104  unsigned char maxprot[4];     /* Maximum permitted vm protection.  */
105  unsigned char initprot[4];    /* Initial vm protection.  */
106  unsigned char nsects[4];      /* Number of sections in this segment.  */
107  unsigned char flags[4];       /* Flags that affect the loading.  */
108};
109#define BFD_MACH_O_LC_SEGMENT_64_SIZE 72 /* Include the header.  */
110
111struct mach_o_reloc_info_external
112{
113  unsigned char r_address[4];
114  unsigned char r_symbolnum[4];
115};
116#define BFD_MACH_O_RELENT_SIZE 8
117
118/* Relocations are based on 'address' being a section offset and an assumption
119   that sections are never more than 2^24-1 bytes in size.  Relocation data
120   also carry information on type/size/PC-relative/extern and whether scattered
121   or not [stored in the MSB of the r_address].  */
122
123#define BFD_MACH_O_SR_SCATTERED		0x80000000
124
125/* For a non-scattered reloc, the relocation info is found in r_symbolnum.
126   Bytes 1 to 3 contain the symbol number (0xffffff, in a non-scattered PAIR).
127   Byte 4 contains the relocation info - but with differing bit-positions
128   dependent on target endian-ness - as below.  */
129
130#define BFD_MACH_O_LE_PCREL		0x01
131#define BFD_MACH_O_LE_LENGTH_SHIFT	1
132#define BFD_MACH_O_LE_EXTERN		0x08
133#define BFD_MACH_O_LE_TYPE_SHIFT	4
134
135#define BFD_MACH_O_BE_PCREL		0x80
136#define BFD_MACH_O_BE_LENGTH_SHIFT	5
137#define BFD_MACH_O_BE_EXTERN		0x10
138#define BFD_MACH_O_BE_TYPE_SHIFT	0
139
140/* The field sizes are the same for both BE and LE.  */
141#define BFD_MACH_O_LENGTH_MASK		0x03
142#define BFD_MACH_O_TYPE_MASK		0x0f
143
144/* For a scattered reloc entry the info is contained in r_address.  There
145   is no need to discriminate on target endian-ness, since the design was
146   arranged to produce the same layout on both.  Scattered relocations are
147   only used for local items, therefore there is no 'extern' field.  */
148
149#define BFD_MACH_O_SR_PCREL		0x40000000
150#define BFD_MACH_O_GET_SR_LENGTH(s)	(((s) >> 28) & 0x3)
151#define BFD_MACH_O_GET_SR_TYPE(s)	(((s) >> 24) & 0x0f)
152#define BFD_MACH_O_GET_SR_ADDRESS(s)	((s) & 0x00ffffff)
153#define BFD_MACH_O_SET_SR_LENGTH(l)	(((l) & 0x3) << 28)
154#define BFD_MACH_O_SET_SR_TYPE(t)	(((t) & 0xf) << 24)
155#define BFD_MACH_O_SET_SR_ADDRESS(s)	((s) & 0x00ffffff)
156
157struct mach_o_symtab_command_external
158{
159  unsigned char symoff[4];	/* File offset of the symbol table.  */
160  unsigned char nsyms[4];	/* Number of symbols.  */
161  unsigned char stroff[4];	/* File offset of the string table.  */
162  unsigned char strsize[4];	/* String table size.  */
163};
164
165struct mach_o_nlist_external
166{
167  unsigned char n_strx[4];
168  unsigned char n_type[1];
169  unsigned char n_sect[1];
170  unsigned char n_desc[2];
171  unsigned char n_value[4];
172};
173#define BFD_MACH_O_NLIST_SIZE 12
174
175struct mach_o_nlist_64_external
176{
177  unsigned char n_strx[4];
178  unsigned char n_type[1];
179  unsigned char n_sect[1];
180  unsigned char n_desc[2];
181  unsigned char n_value[8];
182};
183#define BFD_MACH_O_NLIST_64_SIZE 16
184
185struct mach_o_thread_command_external
186{
187  unsigned char flavour[4];
188  unsigned char count[4];
189};
190
191/* For commands that just have a string or a path.  */
192struct mach_o_str_command_external
193{
194  unsigned char str[4];
195};
196
197struct mach_o_dylib_command_external
198{
199  unsigned char name[4];
200  unsigned char timestamp[4];
201  unsigned char current_version[4];
202  unsigned char compatibility_version[4];
203};
204
205struct mach_o_dysymtab_command_external
206{
207  unsigned char ilocalsym[4];	/* Index of.  */
208  unsigned char nlocalsym[4];	/* Number of.  */
209  unsigned char iextdefsym[4];
210  unsigned char nextdefsym[4];
211  unsigned char iundefsym[4];
212  unsigned char nundefsym[4];
213  unsigned char tocoff[4];
214  unsigned char ntoc[4];
215  unsigned char modtaboff[4];
216  unsigned char nmodtab[4];
217  unsigned char extrefsymoff[4];
218  unsigned char nextrefsyms[4];
219  unsigned char indirectsymoff[4];
220  unsigned char nindirectsyms[4];
221  unsigned char extreloff[4];
222  unsigned char nextrel[4];
223  unsigned char locreloff[4];
224  unsigned char nlocrel[4];
225};
226
227struct mach_o_dylib_module_external
228{
229  unsigned char module_name[4];
230  unsigned char iextdefsym[4];
231  unsigned char nextdefsym[4];
232  unsigned char irefsym[4];
233  unsigned char nrefsym[4];
234  unsigned char ilocalsym[4];
235  unsigned char nlocalsym[4];
236  unsigned char iextrel[4];
237  unsigned char nextrel[4];
238  unsigned char iinit_iterm[4];
239  unsigned char ninit_nterm[4];
240  unsigned char objc_module_info_addr[4];
241  unsigned char objc_module_info_size[4];
242};
243#define BFD_MACH_O_DYLIB_MODULE_SIZE 52
244
245struct mach_o_dylib_module_64_external
246{
247  unsigned char module_name[4];
248  unsigned char iextdefsym[4];
249  unsigned char nextdefsym[4];
250  unsigned char irefsym[4];
251  unsigned char nrefsym[4];
252  unsigned char ilocalsym[4];
253  unsigned char nlocalsym[4];
254  unsigned char iextrel[4];
255  unsigned char nextrel[4];
256  unsigned char iinit_iterm[4];
257  unsigned char ninit_nterm[4];
258  unsigned char objc_module_info_size[4];
259  unsigned char objc_module_info_addr[8];
260};
261#define BFD_MACH_O_DYLIB_MODULE_64_SIZE 56
262
263struct mach_o_dylib_table_of_contents_external
264{
265  unsigned char symbol_index[4];
266  unsigned char module_index[4];
267};
268#define BFD_MACH_O_TABLE_OF_CONTENT_SIZE 8
269
270struct mach_o_linkedit_data_command_external
271{
272  unsigned char dataoff[4];
273  unsigned char datasize[4];
274};
275
276struct mach_o_dyld_info_command_external
277{
278  unsigned char rebase_off[4];
279  unsigned char rebase_size[4];
280  unsigned char bind_off[4];
281  unsigned char bind_size[4];
282  unsigned char weak_bind_off[4];
283  unsigned char weak_bind_size[4];
284  unsigned char lazy_bind_off[4];
285  unsigned char lazy_bind_size[4];
286  unsigned char export_off[4];
287  unsigned char export_size[4];
288};
289
290struct mach_o_prebound_dylib_command_external
291{
292  unsigned char name[4];
293  unsigned char nmodules[4];
294  unsigned char linked_modules[4];
295};
296
297struct mach_o_prebind_cksum_command_external
298{
299  unsigned char cksum[4];
300};
301
302struct mach_o_twolevel_hints_command_external
303{
304  unsigned char offset[4];
305  unsigned char nhints[4];
306};
307
308struct mach_o_version_min_command_external
309{
310  unsigned char version[4];
311  unsigned char sdk[4];
312};
313
314struct mach_o_encryption_info_command_external
315{
316  unsigned char cryptoff[4];	/* File offset of the encrypted area.  */
317  unsigned char cryptsize[4];	/* Size of the encrypted area.  */
318  unsigned char cryptid[4];	/* Encryption method.  */
319};
320
321struct mach_o_encryption_info_64_command_external
322{
323  unsigned char cryptoff[4];	/* File offset of the encrypted area.  */
324  unsigned char cryptsize[4];	/* Size of the encrypted area.  */
325  unsigned char cryptid[4];	/* Encryption method.  */
326  unsigned char pad[4];		/* Pad to make struct size a multiple of 8.  */
327};
328
329struct mach_o_fvmlib_command_external
330{
331  unsigned char name[4];	/* Offset of the name.  */
332  unsigned char minor_version[4];
333  unsigned char header_addr[4];
334};
335
336struct mach_o_entry_point_command_external
337{
338  unsigned char entryoff[8];	/* File offset of the entry point.  */
339  unsigned char stacksize[8];   /* Initial stack size, if no null.  */
340};
341
342struct mach_o_source_version_command_external
343{
344  unsigned char version[8];	/* Version A.B.C.D.E, with 10 bits for B-E,
345				   and 24 bits for A.  */
346};
347
348struct mach_o_note_command_external
349{
350  unsigned char data_owner[16]; /* Owner name for this note.  */
351  unsigned char offset[8];      /* File offset of the note.  */
352  unsigned char size[8];        /* Length of the note.  */
353};
354
355struct mach_o_build_version_command_external
356{
357  unsigned char platform[4];    /* Target platform.  */
358  unsigned char minos[4];       /* X.Y.Z is encoded in nibbles xxxx.yy.zz.  */
359  unsigned char sdk[4];         /* X.Y.Z is encoded in nibbles xxxx.yy.zz.  */
360  unsigned char ntools[4];      /* Number of tool entries following this.  */
361};
362
363/* The LD_DATA_IN_CODE command use a linkedit_data_command that points to
364   a table of entries.  */
365
366struct mach_o_data_in_code_entry_external
367{
368  unsigned char offset[4];	/* Offset from the mach_header.  */
369  unsigned char length[2];	/* Number of bytes.  */
370  unsigned char kind[2];	/* Kind.  See BFD_MACH_O_DICE_ values.  */
371};
372
373struct mach_o_linker_option_command_external
374{
375  unsigned char count[4];	/* Number of strings.  */
376  /* COUNT null terminated UTF-8 strings, with 0 at the end for padding.  */
377};
378
379struct mach_o_fat_header_external
380{
381  unsigned char magic[4];
382  unsigned char nfat_arch[4];	/* Number of components.  */
383};
384
385struct mach_o_fat_arch_external
386{
387  unsigned char cputype[4];
388  unsigned char cpusubtype[4];
389  unsigned char offset[4];	/* File offset of the member.  */
390  unsigned char size[4];	/* Size of the member.  */
391  unsigned char align[4];	/* Power of 2.  */
392};
393
394struct mach_o_dyld_cache_header_external
395{
396  unsigned char magic[16];
397
398  unsigned char mapping_offset[4];
399  unsigned char mapping_count[4];
400  unsigned char images_offset[4];
401  unsigned char images_count[4];
402
403  unsigned char dyld_base_address[8];
404
405  unsigned char code_signature_offset[8];
406  unsigned char code_signature_size[8];
407
408  unsigned char slide_info_offset[8];
409  unsigned char slide_info_size[8];
410
411  unsigned char local_symbols_offset[8];
412  unsigned char local_symbols_size[8];
413};
414
415struct mach_o_dyld_cache_mapping_info_external
416{
417  unsigned char address[8];
418  unsigned char size[8];
419  unsigned char file_offset[8];
420  unsigned char max_prot[4];
421  unsigned char init_prot[4];
422};
423
424struct mach_o_dyld_cache_image_info_external
425{
426  unsigned char address[8];
427  unsigned char mtime[8];
428  unsigned char inode[8];
429  unsigned char path_file_offset[4];
430  unsigned char pad[4];
431};
432
433
434#endif /* _MACH_O_EXTERNAL_H */
435