external.h 14 KB

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