Wasm.h 13 KB

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