macho.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /* ----------------------------------------------------------------------- *
  2. *
  3. * Copyright 1996-2018 The NASM Authors - All Rights Reserved
  4. * See the file AUTHORS included with the NASM distribution for
  5. * the specific copyright holders.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following
  9. * conditions are met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * * Redistributions in binary form must reproduce the above
  14. * copyright notice, this list of conditions and the following
  15. * disclaimer in the documentation and/or other materials provided
  16. * with the distribution.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  19. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  20. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  25. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  26. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  29. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  30. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. * ----------------------------------------------------------------------- */
  33. #ifndef OUTPUT_MACHO_H
  34. #define OUTPUT_MACHO_H
  35. #include "compiler.h"
  36. /* Magics */
  37. #define MH_MAGIC 0xfeedface
  38. #define MH_MAGIC_64 0xfeedfacf
  39. /* File types */
  40. #define MH_OBJECT 0x1
  41. /* CPUs */
  42. #define CPU_ARCH_MASK 0xff000000
  43. #define CPU_ARCH_ABI64 0x01000000
  44. #define CPU_TYPE_X86 7
  45. #define CPU_TYPE_I386 CPU_TYPE_X86
  46. #define CPU_TYPE_X86_64 (CPU_TYPE_X86 | CPU_ARCH_ABI64)
  47. #define CPU_SUBTYPE_MASK 0xff000000
  48. #define CPU_SUBTYPE_I386_ALL 3
  49. /* Header flags */
  50. #define MH_SUBSECTIONS_VIA_SYMBOLS 0x00002000
  51. /* Load commands */
  52. #define LC_SEGMENT 0x1
  53. #define LC_SEGMENT_64 0x19
  54. #define LC_SYMTAB 0x2
  55. /* Symbol type bits */
  56. #define N_STAB 0xe0
  57. #define N_PEXT 0x10
  58. #define N_TYPE 0x0e
  59. #define N_EXT 0x01
  60. /* To mask with N_TYPE */
  61. #define N_UNDF 0x00
  62. #define N_ABS 0x02
  63. #define N_INDR 0x0a
  64. #define N_PBUD 0x0c
  65. #define N_SECT 0x0e
  66. /* Section ordinals */
  67. #define NO_SECT 0x00
  68. #define MAX_SECT 0xff
  69. /* Section bits */
  70. #define SECTION_TYPE 0x000000ff
  71. #define SECTION_ATTRIBUTES 0xffffff00
  72. #define SECTION_ATTRIBUTES_USR 0xff000000
  73. #define SECTION_ATTRIBUTES_SYS 0x00ffff00
  74. #define S_REGULAR 0x00
  75. #define S_ZEROFILL 0x01
  76. #define S_CSTRING_LITERALS 0x02
  77. #define S_4BYTE_LITERALS 0x03
  78. #define S_8BYTE_LITERALS 0x04
  79. #define S_LITERAL_POINTERS 0x05
  80. #define S_NON_LAZY_SYMBOL_POINTERS 0x06
  81. #define S_LAZY_SYMBOL_POINTERS 0x07
  82. #define S_SYMBOL_STUBS 0x08
  83. #define S_MOD_INIT_FUNC_POINTERS 0x09
  84. #define S_MOD_TERM_FUNC_POINTERS 0x0a
  85. #define S_COALESCED 0x0b
  86. #define S_GB_ZEROFILL 0x0c
  87. #define S_INTERPOSING 0x0d
  88. #define S_16BYTE_LITERALS 0x0e
  89. #define S_DTRACE_DOF 0x0f
  90. #define S_LAZY_DYLIB_SYMBOL_POINTERS 0x10
  91. #define S_THREAD_LOCAL_REGULAR 0x11
  92. #define S_THREAD_LOCAL_ZEROFILL 0x12
  93. #define S_THREAD_LOCAL_VARIABLES 0x13
  94. #define S_THREAD_LOCAL_VARIABLE_POINTERS 0x14
  95. #define S_THREAD_LOCAL_INIT_FUNCTION_POINTERS 0x15
  96. #define S_ATTR_PURE_INSTRUCTIONS 0x80000000
  97. #define S_ATTR_NO_TOC 0x40000000
  98. #define S_ATTR_STRIP_STATIC_SYMS 0x20000000
  99. #define S_ATTR_NO_DEAD_STRIP 0x10000000
  100. #define S_ATTR_LIVE_SUPPORT 0x08000000
  101. #define S_ATTR_SELF_MODIFYING_CODE 0x04000000
  102. #define S_ATTR_DEBUG 0x02000000
  103. #define S_ATTR_SOME_INSTRUCTIONS 0x00000400
  104. #define S_ATTR_EXT_RELOC 0x00000200
  105. #define S_ATTR_LOC_RELOC 0x00000100
  106. #define INDIRECT_SYMBOL_LOCAL 0x80000000
  107. #define INDIRECT_SYMBOL_ABS 0x40000000
  108. /* Relocation info type */
  109. #define GENERIC_RELOC_VANILLA 0
  110. #define GENERIC_RELOC_PAIR 1
  111. #define GENERIC_RELOC_SECTDIFF 2
  112. #define GENERIC_RELOC_PB_LA_PTR 3
  113. #define GENERIC_RELOC_LOCAL_SECTDIFF 4
  114. #define GENERIC_RELOC_TLV 5
  115. #define X86_64_RELOC_UNSIGNED 0
  116. #define X86_64_RELOC_SIGNED 1
  117. #define X86_64_RELOC_BRANCH 2
  118. #define X86_64_RELOC_GOT_LOAD 3
  119. #define X86_64_RELOC_GOT 4
  120. #define X86_64_RELOC_SUBTRACTOR 5
  121. #define X86_64_RELOC_SIGNED_1 6
  122. #define X86_64_RELOC_SIGNED_2 7
  123. #define X86_64_RELOC_SIGNED_4 8
  124. #define X86_64_RELOC_TLV 9
  125. /* Relocation info */
  126. #define R_ABS 0
  127. #define R_SCATTERED 0x80000000
  128. /* VM permission constants */
  129. #define VM_PROT_NONE 0x00
  130. #define VM_PROT_READ 0x01
  131. #define VM_PROT_WRITE 0x02
  132. #define VM_PROT_EXECUTE 0x04
  133. typedef struct {
  134. uint32_t magic;
  135. uint32_t cputype;
  136. uint32_t cpusubtype;
  137. uint32_t filetype;
  138. uint32_t ncmds;
  139. uint32_t sizeofcmds;
  140. uint32_t flags;
  141. } macho_header_t;
  142. typedef struct {
  143. uint32_t magic;
  144. uint32_t cputype;
  145. uint32_t cpusubtype;
  146. uint32_t filetype;
  147. uint32_t ncmds;
  148. uint32_t sizeofcmds;
  149. uint32_t flags;
  150. uint32_t reserved;
  151. } macho_header_64_t;
  152. typedef struct {
  153. uint32_t cmd;
  154. uint32_t cmdsize;
  155. } macho_load_command_t;
  156. typedef struct {
  157. uint32_t cmd;
  158. uint32_t cmdsize;
  159. char segname[16];
  160. uint32_t vmaddr;
  161. uint32_t vmsize;
  162. uint32_t fileoff;
  163. uint32_t filesize;
  164. uint32_t maxprot;
  165. uint32_t initprot;
  166. uint32_t nsects;
  167. uint32_t flags;
  168. } macho_segment_command_t;
  169. typedef struct {
  170. uint32_t cmd;
  171. uint32_t cmdsize;
  172. char segname[16];
  173. uint64_t vmaddr;
  174. uint64_t vmsize;
  175. uint64_t fileoff;
  176. uint64_t filesize;
  177. uint32_t maxprot;
  178. uint32_t initprot;
  179. uint32_t nsects;
  180. uint32_t flags;
  181. } macho_segment_command_64_t;
  182. typedef struct {
  183. char sectname[16];
  184. char segname[16];
  185. uint32_t addr;
  186. uint32_t size;
  187. uint32_t offset;
  188. uint32_t align;
  189. uint32_t reloff;
  190. uint32_t nreloc;
  191. uint32_t flags;
  192. uint32_t reserved1;
  193. uint32_t reserved2;
  194. } macho_section_t;
  195. typedef struct {
  196. char sectname[16];
  197. char segname[16];
  198. uint64_t addr;
  199. uint64_t size;
  200. uint32_t offset;
  201. uint32_t align;
  202. uint32_t reloff;
  203. uint32_t nreloc;
  204. uint32_t flags;
  205. uint32_t reserved1;
  206. uint32_t reserved2;
  207. uint32_t reserved3;
  208. } macho_section_64_t;
  209. typedef struct {
  210. uint32_t cmd;
  211. uint32_t cmdsize;
  212. uint32_t symoff;
  213. uint32_t nsyms;
  214. uint32_t stroff;
  215. uint32_t strsize;
  216. } macho_symtab_command_t;
  217. typedef struct {
  218. int32_t r_address;
  219. union {
  220. struct {
  221. uint32_t r_symbolnum: 24,
  222. r_pcrel: 1,
  223. r_length: 2,
  224. r_extern: 1,
  225. r_type: 4;
  226. } s;
  227. uint32_t r_raw;
  228. } u;
  229. } macho_relocation_info_t;
  230. typedef struct nlist_base {
  231. uint32_t n_strx;
  232. uint8_t n_type;
  233. uint8_t n_sect;
  234. uint16_t n_desc;
  235. } macho_nlist_base_t;
  236. typedef struct nlist {
  237. uint32_t n_strx;
  238. uint8_t n_type;
  239. uint8_t n_sect;
  240. int16_t n_desc;
  241. uint32_t n_value;
  242. } macho_nlist_t;
  243. typedef struct {
  244. uint32_t n_strx;
  245. uint8_t n_type;
  246. uint8_t n_sect;
  247. uint16_t n_desc;
  248. uint64_t n_value;
  249. } macho_nlist_64_t;
  250. #endif /* OUTPUT_MACHO_H */