macho.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2009 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifndef GRUB_MACHO_H
  19. #define GRUB_MACHO_H 1
  20. #include <grub/types.h>
  21. /* Multi-architecture header. Always in big-endian. */
  22. struct grub_macho_fat_header
  23. {
  24. grub_uint32_t magic;
  25. grub_uint32_t nfat_arch;
  26. } __attribute__ ((packed));
  27. #define GRUB_MACHO_FAT_MAGIC 0xcafebabe
  28. typedef grub_uint32_t grub_macho_cpu_type_t;
  29. typedef grub_uint32_t grub_macho_cpu_subtype_t;
  30. /* Architecture descriptor. Always in big-endian. */
  31. struct grub_macho_fat_arch
  32. {
  33. grub_macho_cpu_type_t cputype;
  34. grub_macho_cpu_subtype_t cpusubtype;
  35. grub_uint32_t offset;
  36. grub_uint32_t size;
  37. grub_uint32_t align;
  38. } __attribute__ ((packed));
  39. /* File header for 32-bit. Always in native-endian. */
  40. struct grub_macho_header32
  41. {
  42. #define GRUB_MACHO_MAGIC32 0xfeedface
  43. grub_uint32_t magic;
  44. grub_macho_cpu_type_t cputype;
  45. grub_macho_cpu_subtype_t cpusubtype;
  46. grub_uint32_t filetype;
  47. grub_uint32_t ncmds;
  48. grub_uint32_t sizeofcmds;
  49. grub_uint32_t flags;
  50. } __attribute__ ((packed));
  51. /* File header for 64-bit. Always in native-endian. */
  52. struct grub_macho_header64
  53. {
  54. #define GRUB_MACHO_MAGIC64 0xfeedfacf
  55. grub_uint32_t magic;
  56. grub_macho_cpu_type_t cputype;
  57. grub_macho_cpu_subtype_t cpusubtype;
  58. grub_uint32_t filetype;
  59. grub_uint32_t ncmds;
  60. grub_uint32_t sizeofcmds;
  61. grub_uint32_t flags;
  62. grub_uint32_t reserved;
  63. } __attribute__ ((packed));
  64. /* Convenience union. What do we need to load to identify the file type. */
  65. union grub_macho_filestart
  66. {
  67. struct grub_macho_fat_header fat;
  68. struct grub_macho_header32 thin32;
  69. struct grub_macho_header64 thin64;
  70. } __attribute__ ((packed));
  71. /* Common header of Mach-O commands. */
  72. struct grub_macho_cmd
  73. {
  74. grub_uint32_t cmd;
  75. grub_uint32_t cmdsize;
  76. } __attribute__ ((packed));
  77. typedef grub_uint32_t grub_macho_vmprot_t;
  78. /* 32-bit segment command. */
  79. struct grub_macho_segment32
  80. {
  81. #define GRUB_MACHO_CMD_SEGMENT32 1
  82. grub_uint32_t cmd;
  83. grub_uint32_t cmdsize;
  84. grub_uint8_t segname[16];
  85. grub_uint32_t vmaddr;
  86. grub_uint32_t vmsize;
  87. grub_uint32_t fileoff;
  88. grub_uint32_t filesize;
  89. grub_macho_vmprot_t maxprot;
  90. grub_macho_vmprot_t initprot;
  91. grub_uint32_t nsects;
  92. grub_uint32_t flags;
  93. } __attribute__ ((packed));
  94. /* 64-bit segment command. */
  95. struct grub_macho_segment64
  96. {
  97. #define GRUB_MACHO_CMD_SEGMENT64 0x19
  98. grub_uint32_t cmd;
  99. grub_uint32_t cmdsize;
  100. grub_uint8_t segname[16];
  101. grub_uint64_t vmaddr;
  102. grub_uint64_t vmsize;
  103. grub_uint64_t fileoff;
  104. grub_uint64_t filesize;
  105. grub_macho_vmprot_t maxprot;
  106. grub_macho_vmprot_t initprot;
  107. grub_uint32_t nsects;
  108. grub_uint32_t flags;
  109. } __attribute__ ((packed));
  110. #define GRUB_MACHO_CMD_THREAD 5
  111. struct grub_macho_section32
  112. {
  113. char sectname[16];
  114. char segname[16];
  115. grub_uint32_t addr;
  116. grub_uint32_t size;
  117. grub_uint32_t offset;
  118. grub_uint32_t align;
  119. grub_uint32_t reloff;
  120. grub_uint32_t nreloc;
  121. grub_uint32_t flags;
  122. grub_uint32_t reserved1;
  123. grub_uint32_t reserved2;
  124. } __attribute__ ((packed));
  125. struct grub_macho_section64
  126. {
  127. char sectname[16];
  128. char segname[16];
  129. grub_uint64_t addr;
  130. grub_uint64_t size;
  131. grub_uint32_t offset;
  132. grub_uint32_t align;
  133. grub_uint32_t reloff;
  134. grub_uint32_t nreloc;
  135. grub_uint32_t flags;
  136. grub_uint32_t reserved1;
  137. grub_uint32_t reserved2;
  138. grub_uint32_t reserved3;
  139. } __attribute__ ((packed));
  140. struct grub_macho_symtab
  141. {
  142. #define GRUB_MACHO_CMD_SYMTAB 2
  143. grub_uint32_t cmd;
  144. grub_uint32_t cmdsize;
  145. grub_uint32_t symoff;
  146. grub_uint32_t nsyms;
  147. grub_uint32_t stroff;
  148. grub_uint32_t strsize;
  149. } __attribute__ ((packed));
  150. struct grub_macho_dysymtab
  151. {
  152. #define GRUB_MACHO_CMD_DYSYMTAB 11
  153. grub_uint32_t cmd;
  154. grub_uint32_t cmdsize;
  155. grub_uint32_t ilocalsym;
  156. grub_uint32_t nlocalsym;
  157. grub_uint32_t iextdefsym;
  158. grub_uint32_t nextdefsym;
  159. grub_uint32_t iundefsym;
  160. grub_uint32_t nundefsym;
  161. grub_uint32_t tocoff;
  162. grub_uint32_t ntoc;
  163. grub_uint32_t modtaboff;
  164. grub_uint32_t nmodtab;
  165. grub_uint32_t extrefsymoff;
  166. grub_uint32_t nextrefsyms;
  167. grub_uint32_t indirectsymoff;
  168. grub_uint32_t nindirectsyms;
  169. grub_uint32_t extreloff;
  170. grub_uint32_t nextrel;
  171. grub_uint32_t locreloff;
  172. grub_uint32_t nlocrel;
  173. } __attribute__ ((packed));
  174. #define GRUB_MACHO_N_TYPE 0xe
  175. #define GRUB_MACHO_N_EXT 1
  176. #define GRUB_MACHO_N_UNDEF 0
  177. #define GRUB_MACHO_N_SECT 0xe
  178. struct grub_macho_nlist32
  179. {
  180. grub_int32_t n_strx;
  181. grub_uint8_t n_type;
  182. grub_uint8_t n_sect;
  183. grub_int16_t n_desc;
  184. grub_uint32_t n_value;
  185. } __attribute__ ((packed));
  186. struct grub_macho_nlist64
  187. {
  188. grub_uint32_t n_strx;
  189. grub_uint8_t n_type;
  190. grub_uint8_t n_sect;
  191. grub_int16_t n_desc;
  192. grub_uint64_t n_value;
  193. } __attribute__ ((packed));
  194. #ifdef GRUB_TARGET_WORDS_BIGENDIAN
  195. #define GRUB_MACHO_REL_SYMBOLNUM(a) (a >> 8)
  196. #define GRUB_MACHO_REL_PCREL(a) ((a >> 7) & 1)
  197. #define GRUB_MACHO_REL_LENGTH(a) ((a >> 5) & 3)
  198. #define GRUB_MACHO_REL_EXTERN(a) ((a >> 4) & 1)
  199. #define GRUB_MACHO_REL_TYPE(a) (a & 0xf)
  200. #else
  201. #define GRUB_MACHO_REL_SYMBOLNUM(a) (a & 0xffffff)
  202. #define GRUB_MACHO_REL_PCREL(a) ((a >> 24) & 1)
  203. #define GRUB_MACHO_REL_LENGTH(a) ((a >> 25) & 3)
  204. #define GRUB_MACHO_REL_EXTERN(a) ((a >> 27) & 1)
  205. #define GRUB_MACHO_REL_TYPE(a) ((a >> 28) & 0xf)
  206. #endif
  207. struct grub_macho_relocation_info
  208. {
  209. grub_uint32_t r_address;
  210. grub_uint32_t r_info;
  211. };
  212. #define GRUB_MACHO_SREL_SCATTERED(a) ((a >> 31) & 1)
  213. #define GRUB_MACHO_SREL_PCREL(a) ((a >> 30) & 1)
  214. #define GRUB_MACHO_SREL_LENGTH(a) ((a >> 28) & 3)
  215. #define GRUB_MACHO_SREL_TYPE(a) ((a >> 24) & 0xf)
  216. #define GRUB_MACHO_SREL_ADDRESS(a) (a & 0xffffff)
  217. struct grub_macho_scattered_relocation_info
  218. {
  219. grub_uint32_t r_info;
  220. grub_uint32_t r_value;
  221. };
  222. #define GRUB_MACHO_RELOC_VANILLA 0
  223. #define GRUB_MACHO_RELOC_PAIR 1
  224. #define GRUB_MACHO_RELOC_SECTDIFF 2
  225. #define GRUB_MACHO_RELOC_PB_LA_PTR 3
  226. #define GRUB_MACHO_RELOC_LOCAL_SECTDIFF 4
  227. #define GRUB_MACHO_X86_64_RELOC_UNSIGNED 0
  228. #define GRUB_MACHO_X86_64_RELOC_SIGNED 1
  229. #define GRUB_MACHO_X86_64_RELOC_BRANCH 2
  230. #define GRUB_MACHO_X86_64_RELOC_GOT_LD 3
  231. #define GRUB_MACHO_X86_64_RELOC_GOT 4
  232. #define GRUB_MACHO_X86_64_RELOC_SUB 5
  233. #define GRUB_MACHO_X86_64_RELOC_SIGNED1 6
  234. #define GRUB_MACHO_X86_64_RELOC_SIGNED2 7
  235. #define GRUB_MACHO_X86_64_RELOC_SIGNED4 8
  236. #define GRUB_MACHO_PPC_RELOC_BR24 3
  237. #define GRUB_MACHO_PPC_RELOC_HI16 4
  238. #define GRUB_MACHO_PPC_RELOC_LO16 5
  239. #define GRUB_MACHO_PPC_RELOC_HA16 6
  240. #define GRUB_MACHO_PPC_RELOC_SDIFF 8
  241. #define GRUB_MACHO_PPC_RELOC_HI16_SDIFF 10
  242. #define GRUB_MACHO_PPC_RELOC_LO16_SDIFF 11
  243. #define GRUB_MACHO_PPC_RELOC_HA16_SDIFF 12
  244. #define GRUB_MACHO_PPC_RELOC_LOCAL_SDIFF 15
  245. #endif