Wasm.h 12 KB

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