Wasm.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- Wasm.h - Wasm object file format -------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines manifest constants for the wasm object file format.
  15. // See: https://github.com/WebAssembly/design/blob/master/BinaryEncoding.md
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_BINARYFORMAT_WASM_H
  19. #define LLVM_BINARYFORMAT_WASM_H
  20. #include "llvm/ADT/ArrayRef.h"
  21. #include "llvm/ADT/Optional.h"
  22. #include "llvm/ADT/SmallVector.h"
  23. #include "llvm/ADT/StringRef.h"
  24. namespace llvm {
  25. namespace wasm {
  26. // Object file magic string.
  27. const char WasmMagic[] = {'\0', 'a', 's', 'm'};
  28. // Wasm binary format version
  29. const uint32_t WasmVersion = 0x1;
  30. // Wasm linking metadata version
  31. const uint32_t WasmMetadataVersion = 0x2;
  32. // Wasm uses a 64k page size
  33. const uint32_t WasmPageSize = 65536;
  34. struct WasmObjectHeader {
  35. StringRef Magic;
  36. uint32_t Version;
  37. };
  38. struct WasmDylinkInfo {
  39. uint32_t MemorySize; // Memory size in bytes
  40. uint32_t MemoryAlignment; // P2 alignment of memory
  41. uint32_t TableSize; // Table size in elements
  42. uint32_t TableAlignment; // P2 alignment of table
  43. std::vector<StringRef> Needed; // Shared library dependencies
  44. };
  45. struct WasmProducerInfo {
  46. std::vector<std::pair<std::string, std::string>> Languages;
  47. std::vector<std::pair<std::string, std::string>> Tools;
  48. std::vector<std::pair<std::string, std::string>> SDKs;
  49. };
  50. struct WasmFeatureEntry {
  51. uint8_t Prefix;
  52. std::string Name;
  53. };
  54. struct WasmExport {
  55. StringRef Name;
  56. uint8_t Kind;
  57. uint32_t Index;
  58. };
  59. struct WasmLimits {
  60. uint8_t Flags;
  61. uint64_t Initial;
  62. uint64_t Maximum;
  63. };
  64. struct WasmTableType {
  65. uint8_t ElemType;
  66. WasmLimits Limits;
  67. };
  68. struct WasmTable {
  69. uint32_t Index;
  70. WasmTableType Type;
  71. StringRef SymbolName; // from the "linking" section
  72. };
  73. struct WasmInitExpr {
  74. uint8_t Opcode;
  75. union {
  76. int32_t Int32;
  77. int64_t Int64;
  78. uint32_t Float32;
  79. uint64_t Float64;
  80. uint32_t Global;
  81. } Value;
  82. };
  83. struct WasmGlobalType {
  84. uint8_t Type;
  85. bool Mutable;
  86. };
  87. struct WasmGlobal {
  88. uint32_t Index;
  89. WasmGlobalType Type;
  90. WasmInitExpr InitExpr;
  91. StringRef SymbolName; // from the "linking" section
  92. };
  93. struct WasmEventType {
  94. // Kind of event. Currently only WASM_EVENT_ATTRIBUTE_EXCEPTION is possible.
  95. uint32_t Attribute;
  96. uint32_t SigIndex;
  97. };
  98. struct WasmEvent {
  99. uint32_t Index;
  100. WasmEventType Type;
  101. StringRef SymbolName; // from the "linking" section
  102. };
  103. struct WasmImport {
  104. StringRef Module;
  105. StringRef Field;
  106. uint8_t Kind;
  107. union {
  108. uint32_t SigIndex;
  109. WasmGlobalType Global;
  110. WasmTableType Table;
  111. WasmLimits Memory;
  112. WasmEventType Event;
  113. };
  114. };
  115. struct WasmLocalDecl {
  116. uint8_t Type;
  117. uint32_t Count;
  118. };
  119. struct WasmFunction {
  120. uint32_t Index;
  121. std::vector<WasmLocalDecl> Locals;
  122. ArrayRef<uint8_t> Body;
  123. uint32_t CodeSectionOffset;
  124. uint32_t Size;
  125. uint32_t CodeOffset; // start of Locals and Body
  126. Optional<StringRef> ExportName; // from the "export" section
  127. StringRef SymbolName; // from the "linking" section
  128. StringRef DebugName; // from the "name" section
  129. uint32_t Comdat; // from the "comdat info" section
  130. };
  131. struct WasmDataSegment {
  132. uint32_t InitFlags;
  133. // Present if InitFlags & WASM_DATA_SEGMENT_HAS_MEMINDEX.
  134. uint32_t MemoryIndex;
  135. // Present if InitFlags & WASM_DATA_SEGMENT_IS_PASSIVE == 0.
  136. WasmInitExpr Offset;
  137. ArrayRef<uint8_t> Content;
  138. StringRef Name; // from the "segment info" section
  139. uint32_t Alignment;
  140. uint32_t LinkerFlags;
  141. uint32_t Comdat; // from the "comdat info" section
  142. };
  143. struct WasmElemSegment {
  144. uint32_t TableIndex;
  145. WasmInitExpr Offset;
  146. std::vector<uint32_t> Functions;
  147. };
  148. // Represents the location of a Wasm data symbol within a WasmDataSegment, as
  149. // the index of the segment, and the offset and size within the segment.
  150. struct WasmDataReference {
  151. uint32_t Segment;
  152. uint64_t Offset;
  153. uint64_t Size;
  154. };
  155. struct WasmRelocation {
  156. uint8_t Type; // The type of the relocation.
  157. uint32_t Index; // Index into either symbol or type index space.
  158. uint64_t Offset; // Offset from the start of the section.
  159. int64_t Addend; // A value to add to the symbol.
  160. };
  161. struct WasmInitFunc {
  162. uint32_t Priority;
  163. uint32_t Symbol;
  164. };
  165. struct WasmSymbolInfo {
  166. StringRef Name;
  167. uint8_t Kind;
  168. uint32_t Flags;
  169. // For undefined symbols the module of the import
  170. Optional<StringRef> ImportModule;
  171. // For undefined symbols the name of the import
  172. Optional<StringRef> ImportName;
  173. // For symbols to be exported from the final module
  174. Optional<StringRef> ExportName;
  175. union {
  176. // For function, table, or global symbols, the index in function, table, or
  177. // global index space.
  178. uint32_t ElementIndex;
  179. // For a data symbols, the address of the data relative to segment.
  180. WasmDataReference DataRef;
  181. };
  182. };
  183. enum class NameType {
  184. FUNCTION,
  185. GLOBAL,
  186. DATA_SEGMENT,
  187. };
  188. struct WasmDebugName {
  189. NameType Type;
  190. uint32_t Index;
  191. StringRef Name;
  192. };
  193. struct WasmLinkingData {
  194. uint32_t Version;
  195. std::vector<WasmInitFunc> InitFunctions;
  196. std::vector<StringRef> Comdats;
  197. std::vector<WasmSymbolInfo> SymbolTable;
  198. };
  199. enum : unsigned {
  200. WASM_SEC_CUSTOM = 0, // Custom / User-defined section
  201. WASM_SEC_TYPE = 1, // Function signature declarations
  202. WASM_SEC_IMPORT = 2, // Import declarations
  203. WASM_SEC_FUNCTION = 3, // Function declarations
  204. WASM_SEC_TABLE = 4, // Indirect function table and other tables
  205. WASM_SEC_MEMORY = 5, // Memory attributes
  206. WASM_SEC_GLOBAL = 6, // Global declarations
  207. WASM_SEC_EXPORT = 7, // Exports
  208. WASM_SEC_START = 8, // Start function declaration
  209. WASM_SEC_ELEM = 9, // Elements section
  210. WASM_SEC_CODE = 10, // Function bodies (code)
  211. WASM_SEC_DATA = 11, // Data segments
  212. WASM_SEC_DATACOUNT = 12, // Data segment count
  213. WASM_SEC_EVENT = 13 // Event declarations
  214. };
  215. // Type immediate encodings used in various contexts.
  216. enum : unsigned {
  217. WASM_TYPE_I32 = 0x7F,
  218. WASM_TYPE_I64 = 0x7E,
  219. WASM_TYPE_F32 = 0x7D,
  220. WASM_TYPE_F64 = 0x7C,
  221. WASM_TYPE_V128 = 0x7B,
  222. WASM_TYPE_FUNCREF = 0x70,
  223. WASM_TYPE_EXTERNREF = 0x6F,
  224. WASM_TYPE_FUNC = 0x60,
  225. WASM_TYPE_NORESULT = 0x40, // for blocks with no result values
  226. };
  227. // Kinds of externals (for imports and exports).
  228. enum : unsigned {
  229. WASM_EXTERNAL_FUNCTION = 0x0,
  230. WASM_EXTERNAL_TABLE = 0x1,
  231. WASM_EXTERNAL_MEMORY = 0x2,
  232. WASM_EXTERNAL_GLOBAL = 0x3,
  233. WASM_EXTERNAL_EVENT = 0x4,
  234. };
  235. // Opcodes used in initializer expressions.
  236. enum : unsigned {
  237. WASM_OPCODE_END = 0x0b,
  238. WASM_OPCODE_CALL = 0x10,
  239. WASM_OPCODE_LOCAL_GET = 0x20,
  240. WASM_OPCODE_LOCAL_SET = 0x21,
  241. WASM_OPCODE_GLOBAL_GET = 0x23,
  242. WASM_OPCODE_GLOBAL_SET = 0x24,
  243. WASM_OPCODE_I32_STORE = 0x36,
  244. WASM_OPCODE_I64_STORE = 0x37,
  245. WASM_OPCODE_I32_CONST = 0x41,
  246. WASM_OPCODE_I64_CONST = 0x42,
  247. WASM_OPCODE_F32_CONST = 0x43,
  248. WASM_OPCODE_F64_CONST = 0x44,
  249. WASM_OPCODE_I32_ADD = 0x6a,
  250. WASM_OPCODE_I64_ADD = 0x7c,
  251. WASM_OPCODE_REF_NULL = 0xd0,
  252. };
  253. // Opcodes used in synthetic functions.
  254. enum : unsigned {
  255. WASM_OPCODE_IF = 0x04,
  256. WASM_OPCODE_ELSE = 0x05,
  257. WASM_OPCODE_DROP = 0x1a,
  258. WASM_OPCODE_MISC_PREFIX = 0xfc,
  259. WASM_OPCODE_MEMORY_INIT = 0x08,
  260. WASM_OPCODE_DATA_DROP = 0x09,
  261. WASM_OPCODE_ATOMICS_PREFIX = 0xfe,
  262. WASM_OPCODE_ATOMIC_NOTIFY = 0x00,
  263. WASM_OPCODE_I32_ATOMIC_WAIT = 0x01,
  264. WASM_OPCODE_I32_ATOMIC_STORE = 0x17,
  265. WASM_OPCODE_I32_RMW_CMPXCHG = 0x48,
  266. };
  267. enum : unsigned {
  268. WASM_LIMITS_FLAG_NONE = 0x0,
  269. WASM_LIMITS_FLAG_HAS_MAX = 0x1,
  270. WASM_LIMITS_FLAG_IS_SHARED = 0x2,
  271. WASM_LIMITS_FLAG_IS_64 = 0x4,
  272. };
  273. enum : unsigned {
  274. WASM_DATA_SEGMENT_IS_PASSIVE = 0x01,
  275. WASM_DATA_SEGMENT_HAS_MEMINDEX = 0x02,
  276. };
  277. // Feature policy prefixes used in the custom "target_features" section
  278. enum : uint8_t {
  279. WASM_FEATURE_PREFIX_USED = '+',
  280. WASM_FEATURE_PREFIX_REQUIRED = '=',
  281. WASM_FEATURE_PREFIX_DISALLOWED = '-',
  282. };
  283. // Kind codes used in the custom "name" section
  284. enum : unsigned {
  285. WASM_NAMES_FUNCTION = 1,
  286. WASM_NAMES_LOCAL = 2,
  287. WASM_NAMES_GLOBAL = 7,
  288. WASM_NAMES_DATA_SEGMENT = 9,
  289. };
  290. // Kind codes used in the custom "linking" section
  291. enum : unsigned {
  292. WASM_SEGMENT_INFO = 0x5,
  293. WASM_INIT_FUNCS = 0x6,
  294. WASM_COMDAT_INFO = 0x7,
  295. WASM_SYMBOL_TABLE = 0x8,
  296. };
  297. // Kind codes used in the custom "linking" section in the WASM_COMDAT_INFO
  298. enum : unsigned {
  299. WASM_COMDAT_DATA = 0x0,
  300. WASM_COMDAT_FUNCTION = 0x1,
  301. // GLOBAL, EVENT, and TABLE are in here but LLVM doesn't use them yet.
  302. WASM_COMDAT_SECTION = 0x5,
  303. };
  304. // Kind codes used in the custom "linking" section in the WASM_SYMBOL_TABLE
  305. enum WasmSymbolType : unsigned {
  306. WASM_SYMBOL_TYPE_FUNCTION = 0x0,
  307. WASM_SYMBOL_TYPE_DATA = 0x1,
  308. WASM_SYMBOL_TYPE_GLOBAL = 0x2,
  309. WASM_SYMBOL_TYPE_SECTION = 0x3,
  310. WASM_SYMBOL_TYPE_EVENT = 0x4,
  311. WASM_SYMBOL_TYPE_TABLE = 0x5,
  312. };
  313. // Kinds of event attributes.
  314. enum WasmEventAttribute : unsigned {
  315. WASM_EVENT_ATTRIBUTE_EXCEPTION = 0x0,
  316. };
  317. const unsigned WASM_SYMBOL_BINDING_MASK = 0x3;
  318. const unsigned WASM_SYMBOL_VISIBILITY_MASK = 0xc;
  319. const unsigned WASM_SYMBOL_BINDING_GLOBAL = 0x0;
  320. const unsigned WASM_SYMBOL_BINDING_WEAK = 0x1;
  321. const unsigned WASM_SYMBOL_BINDING_LOCAL = 0x2;
  322. const unsigned WASM_SYMBOL_VISIBILITY_DEFAULT = 0x0;
  323. const unsigned WASM_SYMBOL_VISIBILITY_HIDDEN = 0x4;
  324. const unsigned WASM_SYMBOL_UNDEFINED = 0x10;
  325. const unsigned WASM_SYMBOL_EXPORTED = 0x20;
  326. const unsigned WASM_SYMBOL_EXPLICIT_NAME = 0x40;
  327. const unsigned WASM_SYMBOL_NO_STRIP = 0x80;
  328. #define WASM_RELOC(name, value) name = value,
  329. enum : unsigned {
  330. #include "WasmRelocs.def"
  331. };
  332. #undef WASM_RELOC
  333. // Subset of types that a value can have
  334. enum class ValType {
  335. I32 = WASM_TYPE_I32,
  336. I64 = WASM_TYPE_I64,
  337. F32 = WASM_TYPE_F32,
  338. F64 = WASM_TYPE_F64,
  339. V128 = WASM_TYPE_V128,
  340. FUNCREF = WASM_TYPE_FUNCREF,
  341. EXTERNREF = WASM_TYPE_EXTERNREF,
  342. };
  343. struct WasmSignature {
  344. SmallVector<ValType, 1> Returns;
  345. SmallVector<ValType, 4> Params;
  346. // Support empty and tombstone instances, needed by DenseMap.
  347. enum { Plain, Empty, Tombstone } State = Plain;
  348. WasmSignature(SmallVector<ValType, 1> &&InReturns,
  349. SmallVector<ValType, 4> &&InParams)
  350. : Returns(InReturns), Params(InParams) {}
  351. WasmSignature() = default;
  352. };
  353. // Useful comparison operators
  354. inline bool operator==(const WasmSignature &LHS, const WasmSignature &RHS) {
  355. return LHS.State == RHS.State && LHS.Returns == RHS.Returns &&
  356. LHS.Params == RHS.Params;
  357. }
  358. inline bool operator!=(const WasmSignature &LHS, const WasmSignature &RHS) {
  359. return !(LHS == RHS);
  360. }
  361. inline bool operator==(const WasmGlobalType &LHS, const WasmGlobalType &RHS) {
  362. return LHS.Type == RHS.Type && LHS.Mutable == RHS.Mutable;
  363. }
  364. inline bool operator!=(const WasmGlobalType &LHS, const WasmGlobalType &RHS) {
  365. return !(LHS == RHS);
  366. }
  367. std::string toString(WasmSymbolType type);
  368. std::string relocTypetoString(uint32_t type);
  369. bool relocTypeHasAddend(uint32_t type);
  370. } // end namespace wasm
  371. } // end namespace llvm
  372. #endif
  373. #ifdef __GNUC__
  374. #pragma GCC diagnostic pop
  375. #endif