148596154Schristos /* Mach-O support for BFD.
2*1424dfb3Schristos    Copyright (C) 2011-2020 Free Software Foundation, Inc.
348596154Schristos 
448596154Schristos    This file is part of BFD, the Binary File Descriptor library.
548596154Schristos 
648596154Schristos    This program is free software; you can redistribute it and/or modify
748596154Schristos    it under the terms of the GNU General Public License as published by
848596154Schristos    the Free Software Foundation; either version 3 of the License, or
948596154Schristos    (at your option) any later version.
1048596154Schristos 
1148596154Schristos    This program is distributed in the hope that it will be useful,
1248596154Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
1348596154Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1448596154Schristos    GNU General Public License for more details.
1548596154Schristos 
1648596154Schristos    You should have received a copy of the GNU General Public License
1748596154Schristos    along with this program; if not, write to the Free Software
1848596154Schristos    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
1948596154Schristos    MA 02110-1301, USA.  */
2048596154Schristos 
2148596154Schristos #ifndef _MACH_O_EXTERNAL_H
2248596154Schristos #define _MACH_O_EXTERNAL_H
2348596154Schristos 
2448596154Schristos struct mach_o_header_external
2548596154Schristos {
2648596154Schristos   unsigned char magic[4];	/* Magic number.  */
2748596154Schristos   unsigned char cputype[4];	/* CPU that this object is for.  */
2848596154Schristos   unsigned char cpusubtype[4];	/* CPU subtype.  */
2948596154Schristos   unsigned char filetype[4];	/* Type of file.  */
3048596154Schristos   unsigned char ncmds[4];	/* Number of load commands.  */
3148596154Schristos   unsigned char sizeofcmds[4];	/* Total size of load commands.  */
3248596154Schristos   unsigned char flags[4];	/* Flags.  */
3348596154Schristos   unsigned char reserved[4];	/* Reserved (on 64-bit version only).  */
3448596154Schristos };
3548596154Schristos 
3648596154Schristos #define BFD_MACH_O_HEADER_SIZE 28
3748596154Schristos #define BFD_MACH_O_HEADER_64_SIZE 32
3848596154Schristos 
3948596154Schristos /* 32-bit section header.  */
4048596154Schristos 
4148596154Schristos struct mach_o_section_32_external
4248596154Schristos {
4348596154Schristos   unsigned char sectname[16];   /* Section name.  */
4448596154Schristos   unsigned char segname[16];    /* Segment that the section belongs to.  */
4548596154Schristos   unsigned char addr[4];        /* Address of this section in memory.  */
4648596154Schristos   unsigned char size[4];        /* Size in bytes of this section.  */
4748596154Schristos   unsigned char offset[4];      /* File offset of this section.  */
4848596154Schristos   unsigned char align[4];       /* log2 of this section's alignment.  */
4948596154Schristos   unsigned char reloff[4];      /* File offset of this section's relocs.  */
5048596154Schristos   unsigned char nreloc[4];      /* Number of relocs for this section.  */
5148596154Schristos   unsigned char flags[4];       /* Section flags/attributes.  */
5248596154Schristos   unsigned char reserved1[4];
5348596154Schristos   unsigned char reserved2[4];
5448596154Schristos };
5548596154Schristos #define BFD_MACH_O_SECTION_SIZE 68
5648596154Schristos 
5748596154Schristos /* 64-bit section header.  */
5848596154Schristos 
5948596154Schristos struct mach_o_section_64_external
6048596154Schristos {
6148596154Schristos   unsigned char sectname[16];   /* Section name.  */
6248596154Schristos   unsigned char segname[16];    /* Segment that the section belongs to.  */
6348596154Schristos   unsigned char addr[8];        /* Address of this section in memory.  */
6448596154Schristos   unsigned char size[8];        /* Size in bytes of this section.  */
6548596154Schristos   unsigned char offset[4];      /* File offset of this section.  */
6648596154Schristos   unsigned char align[4];       /* log2 of this section's alignment.  */
6748596154Schristos   unsigned char reloff[4];      /* File offset of this section's relocs.  */
6848596154Schristos   unsigned char nreloc[4];      /* Number of relocs for this section.  */
6948596154Schristos   unsigned char flags[4];       /* Section flags/attributes.  */
7048596154Schristos   unsigned char reserved1[4];
7148596154Schristos   unsigned char reserved2[4];
7248596154Schristos   unsigned char reserved3[4];
7348596154Schristos };
7448596154Schristos #define BFD_MACH_O_SECTION_64_SIZE 80
7548596154Schristos 
7648596154Schristos struct mach_o_load_command_external
7748596154Schristos {
7848596154Schristos   unsigned char cmd[4];         /* The type of load command.  */
7948596154Schristos   unsigned char cmdsize[4];     /* Size in bytes of entire command.  */
8048596154Schristos };
8148596154Schristos #define BFD_MACH_O_LC_SIZE 8
8248596154Schristos 
8348596154Schristos struct mach_o_segment_command_32_external
8448596154Schristos {
8548596154Schristos   unsigned char segname[16];    /* Name of this segment.  */
8648596154Schristos   unsigned char vmaddr[4];      /* Virtual memory address of this segment.  */
8748596154Schristos   unsigned char vmsize[4];      /* Size there, in bytes.  */
8848596154Schristos   unsigned char fileoff[4];     /* Offset in bytes of the data to be mapped.  */
8948596154Schristos   unsigned char filesize[4];    /* Size in bytes on disk.  */
9048596154Schristos   unsigned char maxprot[4];     /* Maximum permitted vm protection.  */
9148596154Schristos   unsigned char initprot[4];    /* Initial vm protection.  */
9248596154Schristos   unsigned char nsects[4];      /* Number of sections in this segment.  */
9348596154Schristos   unsigned char flags[4];       /* Flags that affect the loading.  */
9448596154Schristos };
9548596154Schristos #define BFD_MACH_O_LC_SEGMENT_SIZE 56 /* Include the header.  */
9648596154Schristos 
9748596154Schristos struct mach_o_segment_command_64_external
9848596154Schristos {
9948596154Schristos   unsigned char segname[16];    /* Name of this segment.  */
10048596154Schristos   unsigned char vmaddr[8];      /* Virtual memory address of this segment.  */
10148596154Schristos   unsigned char vmsize[8];      /* Size there, in bytes.  */
10248596154Schristos   unsigned char fileoff[8];     /* Offset in bytes of the data to be mapped.  */
10348596154Schristos   unsigned char filesize[8];    /* Size in bytes on disk.  */
10448596154Schristos   unsigned char maxprot[4];     /* Maximum permitted vm protection.  */
10548596154Schristos   unsigned char initprot[4];    /* Initial vm protection.  */
10648596154Schristos   unsigned char nsects[4];      /* Number of sections in this segment.  */
10748596154Schristos   unsigned char flags[4];       /* Flags that affect the loading.  */
10848596154Schristos };
10948596154Schristos #define BFD_MACH_O_LC_SEGMENT_64_SIZE 72 /* Include the header.  */
11048596154Schristos 
11148596154Schristos struct mach_o_reloc_info_external
11248596154Schristos {
11348596154Schristos   unsigned char r_address[4];
11448596154Schristos   unsigned char r_symbolnum[4];
11548596154Schristos };
11648596154Schristos #define BFD_MACH_O_RELENT_SIZE 8
11748596154Schristos 
11848596154Schristos /* Relocations are based on 'address' being a section offset and an assumption
11948596154Schristos    that sections are never more than 2^24-1 bytes in size.  Relocation data
12048596154Schristos    also carry information on type/size/PC-relative/extern and whether scattered
12148596154Schristos    or not [stored in the MSB of the r_address].  */
12248596154Schristos 
12348596154Schristos #define BFD_MACH_O_SR_SCATTERED		0x80000000
12448596154Schristos 
12548596154Schristos /* For a non-scattered reloc, the relocation info is found in r_symbolnum.
12648596154Schristos    Bytes 1 to 3 contain the symbol number (0xffffff, in a non-scattered PAIR).
12748596154Schristos    Byte 4 contains the relocation info - but with differing bit-positions
12848596154Schristos    dependent on target endian-ness - as below.  */
12948596154Schristos 
13048596154Schristos #define BFD_MACH_O_LE_PCREL		0x01
13148596154Schristos #define BFD_MACH_O_LE_LENGTH_SHIFT	1
13248596154Schristos #define BFD_MACH_O_LE_EXTERN		0x08
13348596154Schristos #define BFD_MACH_O_LE_TYPE_SHIFT	4
13448596154Schristos 
13548596154Schristos #define BFD_MACH_O_BE_PCREL		0x80
13648596154Schristos #define BFD_MACH_O_BE_LENGTH_SHIFT	5
13748596154Schristos #define BFD_MACH_O_BE_EXTERN		0x10
13848596154Schristos #define BFD_MACH_O_BE_TYPE_SHIFT	0
13948596154Schristos 
14048596154Schristos /* The field sizes are the same for both BE and LE.  */
14148596154Schristos #define BFD_MACH_O_LENGTH_MASK		0x03
14248596154Schristos #define BFD_MACH_O_TYPE_MASK		0x0f
14348596154Schristos 
14448596154Schristos /* For a scattered reloc entry the info is contained in r_address.  There
14548596154Schristos    is no need to discriminate on target endian-ness, since the design was
14648596154Schristos    arranged to produce the same layout on both.  Scattered relocations are
14748596154Schristos    only used for local items, therefore there is no 'extern' field.  */
14848596154Schristos 
14948596154Schristos #define BFD_MACH_O_SR_PCREL		0x40000000
15048596154Schristos #define BFD_MACH_O_GET_SR_LENGTH(s)	(((s) >> 28) & 0x3)
15148596154Schristos #define BFD_MACH_O_GET_SR_TYPE(s)	(((s) >> 24) & 0x0f)
15248596154Schristos #define BFD_MACH_O_GET_SR_ADDRESS(s)	((s) & 0x00ffffff)
15348596154Schristos #define BFD_MACH_O_SET_SR_LENGTH(l)	(((l) & 0x3) << 28)
15448596154Schristos #define BFD_MACH_O_SET_SR_TYPE(t)	(((t) & 0xf) << 24)
15548596154Schristos #define BFD_MACH_O_SET_SR_ADDRESS(s)	((s) & 0x00ffffff)
15648596154Schristos 
15748596154Schristos struct mach_o_symtab_command_external
15848596154Schristos {
15948596154Schristos   unsigned char symoff[4];	/* File offset of the symbol table.  */
16048596154Schristos   unsigned char nsyms[4];	/* Number of symbols.  */
16148596154Schristos   unsigned char stroff[4];	/* File offset of the string table.  */
16248596154Schristos   unsigned char strsize[4];	/* String table size.  */
16348596154Schristos };
16448596154Schristos 
16548596154Schristos struct mach_o_nlist_external
16648596154Schristos {
16748596154Schristos   unsigned char n_strx[4];
16848596154Schristos   unsigned char n_type[1];
16948596154Schristos   unsigned char n_sect[1];
17048596154Schristos   unsigned char n_desc[2];
17148596154Schristos   unsigned char n_value[4];
17248596154Schristos };
17348596154Schristos #define BFD_MACH_O_NLIST_SIZE 12
17448596154Schristos 
17548596154Schristos struct mach_o_nlist_64_external
17648596154Schristos {
17748596154Schristos   unsigned char n_strx[4];
17848596154Schristos   unsigned char n_type[1];
17948596154Schristos   unsigned char n_sect[1];
18048596154Schristos   unsigned char n_desc[2];
18148596154Schristos   unsigned char n_value[8];
18248596154Schristos };
18348596154Schristos #define BFD_MACH_O_NLIST_64_SIZE 16
18448596154Schristos 
18548596154Schristos struct mach_o_thread_command_external
18648596154Schristos {
18748596154Schristos   unsigned char flavour[4];
18848596154Schristos   unsigned char count[4];
18948596154Schristos };
19048596154Schristos 
19148596154Schristos /* For commands that just have a string or a path.  */
19248596154Schristos struct mach_o_str_command_external
19348596154Schristos {
19448596154Schristos   unsigned char str[4];
19548596154Schristos };
19648596154Schristos 
19748596154Schristos struct mach_o_dylib_command_external
19848596154Schristos {
19948596154Schristos   unsigned char name[4];
20048596154Schristos   unsigned char timestamp[4];
20148596154Schristos   unsigned char current_version[4];
20248596154Schristos   unsigned char compatibility_version[4];
20348596154Schristos };
20448596154Schristos 
20548596154Schristos struct mach_o_dysymtab_command_external
20648596154Schristos {
20748596154Schristos   unsigned char ilocalsym[4];	/* Index of.  */
20848596154Schristos   unsigned char nlocalsym[4];	/* Number of.  */
20948596154Schristos   unsigned char iextdefsym[4];
21048596154Schristos   unsigned char nextdefsym[4];
21148596154Schristos   unsigned char iundefsym[4];
21248596154Schristos   unsigned char nundefsym[4];
21348596154Schristos   unsigned char tocoff[4];
21448596154Schristos   unsigned char ntoc[4];
21548596154Schristos   unsigned char modtaboff[4];
21648596154Schristos   unsigned char nmodtab[4];
21748596154Schristos   unsigned char extrefsymoff[4];
21848596154Schristos   unsigned char nextrefsyms[4];
21948596154Schristos   unsigned char indirectsymoff[4];
22048596154Schristos   unsigned char nindirectsyms[4];
22148596154Schristos   unsigned char extreloff[4];
22248596154Schristos   unsigned char nextrel[4];
22348596154Schristos   unsigned char locreloff[4];
22448596154Schristos   unsigned char nlocrel[4];
22548596154Schristos };
22648596154Schristos 
22748596154Schristos struct mach_o_dylib_module_external
22848596154Schristos {
22948596154Schristos   unsigned char module_name[4];
23048596154Schristos   unsigned char iextdefsym[4];
23148596154Schristos   unsigned char nextdefsym[4];
23248596154Schristos   unsigned char irefsym[4];
23348596154Schristos   unsigned char nrefsym[4];
23448596154Schristos   unsigned char ilocalsym[4];
23548596154Schristos   unsigned char nlocalsym[4];
23648596154Schristos   unsigned char iextrel[4];
23748596154Schristos   unsigned char nextrel[4];
23848596154Schristos   unsigned char iinit_iterm[4];
23948596154Schristos   unsigned char ninit_nterm[4];
24048596154Schristos   unsigned char objc_module_info_addr[4];
24148596154Schristos   unsigned char objc_module_info_size[4];
24248596154Schristos };
24348596154Schristos #define BFD_MACH_O_DYLIB_MODULE_SIZE 52
24448596154Schristos 
24548596154Schristos struct mach_o_dylib_module_64_external
24648596154Schristos {
24748596154Schristos   unsigned char module_name[4];
24848596154Schristos   unsigned char iextdefsym[4];
24948596154Schristos   unsigned char nextdefsym[4];
25048596154Schristos   unsigned char irefsym[4];
25148596154Schristos   unsigned char nrefsym[4];
25248596154Schristos   unsigned char ilocalsym[4];
25348596154Schristos   unsigned char nlocalsym[4];
25448596154Schristos   unsigned char iextrel[4];
25548596154Schristos   unsigned char nextrel[4];
25648596154Schristos   unsigned char iinit_iterm[4];
25748596154Schristos   unsigned char ninit_nterm[4];
25848596154Schristos   unsigned char objc_module_info_size[4];
25948596154Schristos   unsigned char objc_module_info_addr[8];
26048596154Schristos };
26148596154Schristos #define BFD_MACH_O_DYLIB_MODULE_64_SIZE 56
26248596154Schristos 
26348596154Schristos struct mach_o_dylib_table_of_contents_external
26448596154Schristos {
26548596154Schristos   unsigned char symbol_index[4];
26648596154Schristos   unsigned char module_index[4];
26748596154Schristos };
26848596154Schristos #define BFD_MACH_O_TABLE_OF_CONTENT_SIZE 8
26948596154Schristos 
27048596154Schristos struct mach_o_linkedit_data_command_external
27148596154Schristos {
27248596154Schristos   unsigned char dataoff[4];
27348596154Schristos   unsigned char datasize[4];
27448596154Schristos };
27548596154Schristos 
27648596154Schristos struct mach_o_dyld_info_command_external
27748596154Schristos {
27848596154Schristos   unsigned char rebase_off[4];
27948596154Schristos   unsigned char rebase_size[4];
28048596154Schristos   unsigned char bind_off[4];
28148596154Schristos   unsigned char bind_size[4];
28248596154Schristos   unsigned char weak_bind_off[4];
28348596154Schristos   unsigned char weak_bind_size[4];
28448596154Schristos   unsigned char lazy_bind_off[4];
28548596154Schristos   unsigned char lazy_bind_size[4];
28648596154Schristos   unsigned char export_off[4];
28748596154Schristos   unsigned char export_size[4];
28848596154Schristos };
28948596154Schristos 
2903aab8d44Schristos struct mach_o_prebound_dylib_command_external
2913aab8d44Schristos {
2923aab8d44Schristos   unsigned char name[4];
2933aab8d44Schristos   unsigned char nmodules[4];
2943aab8d44Schristos   unsigned char linked_modules[4];
2953aab8d44Schristos };
2963aab8d44Schristos 
2973aab8d44Schristos struct mach_o_prebind_cksum_command_external
2983aab8d44Schristos {
2993aab8d44Schristos   unsigned char cksum[4];
3003aab8d44Schristos };
3013aab8d44Schristos 
3023aab8d44Schristos struct mach_o_twolevel_hints_command_external
3033aab8d44Schristos {
3043aab8d44Schristos   unsigned char offset[4];
3053aab8d44Schristos   unsigned char nhints[4];
3063aab8d44Schristos };
3073aab8d44Schristos 
30848596154Schristos struct mach_o_version_min_command_external
30948596154Schristos {
31048596154Schristos   unsigned char version[4];
31107163879Schristos   unsigned char sdk[4];
31248596154Schristos };
31348596154Schristos 
31448596154Schristos struct mach_o_encryption_info_command_external
31548596154Schristos {
31648596154Schristos   unsigned char cryptoff[4];	/* File offset of the encrypted area.  */
31748596154Schristos   unsigned char cryptsize[4];	/* Size of the encrypted area.  */
31848596154Schristos   unsigned char cryptid[4];	/* Encryption method.  */
31948596154Schristos };
32048596154Schristos 
321c03b94e9Schristos struct mach_o_encryption_info_64_command_external
322c03b94e9Schristos {
323c03b94e9Schristos   unsigned char cryptoff[4];	/* File offset of the encrypted area.  */
324c03b94e9Schristos   unsigned char cryptsize[4];	/* Size of the encrypted area.  */
325c03b94e9Schristos   unsigned char cryptid[4];	/* Encryption method.  */
326c03b94e9Schristos   unsigned char pad[4];		/* Pad to make struct size a multiple of 8.  */
327c03b94e9Schristos };
328c03b94e9Schristos 
32948596154Schristos struct mach_o_fvmlib_command_external
33048596154Schristos {
33148596154Schristos   unsigned char name[4];	/* Offset of the name.  */
33248596154Schristos   unsigned char minor_version[4];
33348596154Schristos   unsigned char header_addr[4];
33448596154Schristos };
33548596154Schristos 
33648596154Schristos struct mach_o_entry_point_command_external
33748596154Schristos {
33848596154Schristos   unsigned char entryoff[8];	/* File offset of the entry point.  */
33948596154Schristos   unsigned char stacksize[8];   /* Initial stack size, if no null.  */
34048596154Schristos };
34148596154Schristos 
34248596154Schristos struct mach_o_source_version_command_external
34348596154Schristos {
34448596154Schristos   unsigned char version[8];	/* Version A.B.C.D.E, with 10 bits for B-E,
34548596154Schristos 				   and 24 bits for A.  */
34648596154Schristos };
34748596154Schristos 
34807163879Schristos struct mach_o_note_command_external
34907163879Schristos {
35007163879Schristos   unsigned char data_owner[16]; /* Owner name for this note.  */
35107163879Schristos   unsigned char offset[8];      /* File offset of the note.  */
35207163879Schristos   unsigned char size[8];        /* Length of the note.  */
35307163879Schristos };
35407163879Schristos 
35507163879Schristos struct mach_o_build_version_command_external
35607163879Schristos {
35707163879Schristos   unsigned char platform[4];    /* Target platform.  */
35807163879Schristos   unsigned char minos[4];       /* X.Y.Z is encoded in nibbles xxxx.yy.zz.  */
35907163879Schristos   unsigned char sdk[4];         /* X.Y.Z is encoded in nibbles xxxx.yy.zz.  */
36007163879Schristos   unsigned char ntools[4];      /* Number of tool entries following this.  */
36107163879Schristos };
36207163879Schristos 
36348596154Schristos /* The LD_DATA_IN_CODE command use a linkedit_data_command that points to
36448596154Schristos    a table of entries.  */
36548596154Schristos 
36648596154Schristos struct mach_o_data_in_code_entry_external
36748596154Schristos {
36848596154Schristos   unsigned char offset[4];	/* Offset from the mach_header.  */
36948596154Schristos   unsigned char length[2];	/* Number of bytes.  */
37048596154Schristos   unsigned char kind[2];	/* Kind.  See BFD_MACH_O_DICE_ values.  */
37148596154Schristos };
37248596154Schristos 
373c03b94e9Schristos struct mach_o_linker_option_command_external
374c03b94e9Schristos {
375c03b94e9Schristos   unsigned char count[4];	/* Number of strings.  */
376c03b94e9Schristos   /* COUNT null terminated UTF-8 strings, with 0 at the end for padding.  */
377c03b94e9Schristos };
378c03b94e9Schristos 
37948596154Schristos struct mach_o_fat_header_external
38048596154Schristos {
38148596154Schristos   unsigned char magic[4];
38248596154Schristos   unsigned char nfat_arch[4];	/* Number of components.  */
38348596154Schristos };
38448596154Schristos 
38548596154Schristos struct mach_o_fat_arch_external
38648596154Schristos {
38748596154Schristos   unsigned char cputype[4];
38848596154Schristos   unsigned char cpusubtype[4];
38948596154Schristos   unsigned char offset[4];	/* File offset of the member.  */
39048596154Schristos   unsigned char size[4];	/* Size of the member.  */
39148596154Schristos   unsigned char align[4];	/* Power of 2.  */
39248596154Schristos };
39348596154Schristos 
394c03b94e9Schristos struct mach_o_dyld_cache_header_external
395c03b94e9Schristos {
396c03b94e9Schristos   unsigned char magic[16];
397c03b94e9Schristos 
398c03b94e9Schristos   unsigned char mapping_offset[4];
399c03b94e9Schristos   unsigned char mapping_count[4];
400c03b94e9Schristos   unsigned char images_offset[4];
401c03b94e9Schristos   unsigned char images_count[4];
402c03b94e9Schristos 
403c03b94e9Schristos   unsigned char dyld_base_address[8];
404c03b94e9Schristos 
405c03b94e9Schristos   unsigned char code_signature_offset[8];
406c03b94e9Schristos   unsigned char code_signature_size[8];
407c03b94e9Schristos 
408c03b94e9Schristos   unsigned char slide_info_offset[8];
409c03b94e9Schristos   unsigned char slide_info_size[8];
410c03b94e9Schristos 
411c03b94e9Schristos   unsigned char local_symbols_offset[8];
412c03b94e9Schristos   unsigned char local_symbols_size[8];
413c03b94e9Schristos };
414c03b94e9Schristos 
415c03b94e9Schristos struct mach_o_dyld_cache_mapping_info_external
416c03b94e9Schristos {
417c03b94e9Schristos   unsigned char address[8];
418c03b94e9Schristos   unsigned char size[8];
419c03b94e9Schristos   unsigned char file_offset[8];
420c03b94e9Schristos   unsigned char max_prot[4];
421c03b94e9Schristos   unsigned char init_prot[4];
422c03b94e9Schristos };
423c03b94e9Schristos 
424c03b94e9Schristos struct mach_o_dyld_cache_image_info_external
425c03b94e9Schristos {
426c03b94e9Schristos   unsigned char address[8];
427c03b94e9Schristos   unsigned char mtime[8];
428c03b94e9Schristos   unsigned char inode[8];
429c03b94e9Schristos   unsigned char path_file_offset[4];
430c03b94e9Schristos   unsigned char pad[4];
431c03b94e9Schristos };
432c03b94e9Schristos 
433c03b94e9Schristos 
43448596154Schristos #endif /* _MACH_O_EXTERNAL_H */
435