Wasm.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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 implementation -----------------*- 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 declares the WasmObjectFile class, which implements the ObjectFile
  15. // interface for Wasm files.
  16. //
  17. // See: https://github.com/WebAssembly/design/blob/master/BinaryEncoding.md
  18. //
  19. //===----------------------------------------------------------------------===//
  20. #ifndef LLVM_OBJECT_WASM_H
  21. #define LLVM_OBJECT_WASM_H
  22. #include "llvm/ADT/ArrayRef.h"
  23. #include "llvm/ADT/StringRef.h"
  24. #include "llvm/BinaryFormat/Wasm.h"
  25. #include "llvm/Config/llvm-config.h"
  26. #include "llvm/MC/MCSymbolWasm.h"
  27. #include "llvm/Object/Binary.h"
  28. #include "llvm/Object/ObjectFile.h"
  29. #include "llvm/Support/Error.h"
  30. #include "llvm/Support/MemoryBuffer.h"
  31. #include <cstddef>
  32. #include <cstdint>
  33. #include <vector>
  34. namespace llvm {
  35. namespace object {
  36. class WasmSymbol {
  37. public:
  38. WasmSymbol(const wasm::WasmSymbolInfo &Info,
  39. const wasm::WasmGlobalType *GlobalType,
  40. const wasm::WasmTableType *TableType,
  41. const wasm::WasmEventType *EventType,
  42. const wasm::WasmSignature *Signature)
  43. : Info(Info), GlobalType(GlobalType), TableType(TableType),
  44. EventType(EventType), Signature(Signature) {}
  45. const wasm::WasmSymbolInfo &Info;
  46. const wasm::WasmGlobalType *GlobalType;
  47. const wasm::WasmTableType *TableType;
  48. const wasm::WasmEventType *EventType;
  49. const wasm::WasmSignature *Signature;
  50. bool isTypeFunction() const {
  51. return Info.Kind == wasm::WASM_SYMBOL_TYPE_FUNCTION;
  52. }
  53. bool isTypeTable() const { return Info.Kind == wasm::WASM_SYMBOL_TYPE_TABLE; }
  54. bool isTypeData() const { return Info.Kind == wasm::WASM_SYMBOL_TYPE_DATA; }
  55. bool isTypeGlobal() const {
  56. return Info.Kind == wasm::WASM_SYMBOL_TYPE_GLOBAL;
  57. }
  58. bool isTypeSection() const {
  59. return Info.Kind == wasm::WASM_SYMBOL_TYPE_SECTION;
  60. }
  61. bool isTypeEvent() const { return Info.Kind == wasm::WASM_SYMBOL_TYPE_EVENT; }
  62. bool isDefined() const { return !isUndefined(); }
  63. bool isUndefined() const {
  64. return (Info.Flags & wasm::WASM_SYMBOL_UNDEFINED) != 0;
  65. }
  66. bool isBindingWeak() const {
  67. return getBinding() == wasm::WASM_SYMBOL_BINDING_WEAK;
  68. }
  69. bool isBindingGlobal() const {
  70. return getBinding() == wasm::WASM_SYMBOL_BINDING_GLOBAL;
  71. }
  72. bool isBindingLocal() const {
  73. return getBinding() == wasm::WASM_SYMBOL_BINDING_LOCAL;
  74. }
  75. unsigned getBinding() const {
  76. return Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK;
  77. }
  78. bool isHidden() const {
  79. return getVisibility() == wasm::WASM_SYMBOL_VISIBILITY_HIDDEN;
  80. }
  81. unsigned getVisibility() const {
  82. return Info.Flags & wasm::WASM_SYMBOL_VISIBILITY_MASK;
  83. }
  84. void print(raw_ostream &Out) const;
  85. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  86. LLVM_DUMP_METHOD void dump() const;
  87. #endif
  88. };
  89. struct WasmSection {
  90. WasmSection() = default;
  91. uint32_t Type = 0; // Section type (See below)
  92. uint32_t Offset = 0; // Offset with in the file
  93. StringRef Name; // Section name (User-defined sections only)
  94. uint32_t Comdat = UINT32_MAX; // From the "comdat info" section
  95. ArrayRef<uint8_t> Content; // Section content
  96. std::vector<wasm::WasmRelocation> Relocations; // Relocations for this section
  97. };
  98. struct WasmSegment {
  99. uint32_t SectionOffset;
  100. wasm::WasmDataSegment Data;
  101. };
  102. class WasmObjectFile : public ObjectFile {
  103. public:
  104. WasmObjectFile(MemoryBufferRef Object, Error &Err);
  105. const wasm::WasmObjectHeader &getHeader() const;
  106. const WasmSymbol &getWasmSymbol(const DataRefImpl &Symb) const;
  107. const WasmSymbol &getWasmSymbol(const SymbolRef &Symbol) const;
  108. const WasmSection &getWasmSection(const SectionRef &Section) const;
  109. const wasm::WasmRelocation &getWasmRelocation(const RelocationRef &Ref) const;
  110. static bool classof(const Binary *v) { return v->isWasm(); }
  111. const wasm::WasmDylinkInfo &dylinkInfo() const { return DylinkInfo; }
  112. const wasm::WasmProducerInfo &getProducerInfo() const { return ProducerInfo; }
  113. ArrayRef<wasm::WasmFeatureEntry> getTargetFeatures() const {
  114. return TargetFeatures;
  115. }
  116. ArrayRef<wasm::WasmSignature> types() const { return Signatures; }
  117. ArrayRef<uint32_t> functionTypes() const { return FunctionTypes; }
  118. ArrayRef<wasm::WasmImport> imports() const { return Imports; }
  119. ArrayRef<wasm::WasmTable> tables() const { return Tables; }
  120. ArrayRef<wasm::WasmLimits> memories() const { return Memories; }
  121. ArrayRef<wasm::WasmGlobal> globals() const { return Globals; }
  122. ArrayRef<wasm::WasmEvent> events() const { return Events; }
  123. ArrayRef<wasm::WasmExport> exports() const { return Exports; }
  124. ArrayRef<WasmSymbol> syms() const { return Symbols; }
  125. const wasm::WasmLinkingData &linkingData() const { return LinkingData; }
  126. uint32_t getNumberOfSymbols() const { return Symbols.size(); }
  127. ArrayRef<wasm::WasmElemSegment> elements() const { return ElemSegments; }
  128. ArrayRef<WasmSegment> dataSegments() const { return DataSegments; }
  129. ArrayRef<wasm::WasmFunction> functions() const { return Functions; }
  130. ArrayRef<wasm::WasmDebugName> debugNames() const { return DebugNames; }
  131. uint32_t startFunction() const { return StartFunction; }
  132. uint32_t getNumImportedGlobals() const { return NumImportedGlobals; }
  133. uint32_t getNumImportedTables() const { return NumImportedTables; }
  134. uint32_t getNumImportedFunctions() const { return NumImportedFunctions; }
  135. uint32_t getNumImportedEvents() const { return NumImportedEvents; }
  136. uint32_t getNumSections() const { return Sections.size(); }
  137. void moveSymbolNext(DataRefImpl &Symb) const override;
  138. Expected<uint32_t> getSymbolFlags(DataRefImpl Symb) const override;
  139. basic_symbol_iterator symbol_begin() const override;
  140. basic_symbol_iterator symbol_end() const override;
  141. Expected<StringRef> getSymbolName(DataRefImpl Symb) const override;
  142. Expected<uint64_t> getSymbolAddress(DataRefImpl Symb) const override;
  143. uint64_t getWasmSymbolValue(const WasmSymbol &Sym) const;
  144. uint64_t getSymbolValueImpl(DataRefImpl Symb) const override;
  145. uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
  146. uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
  147. Expected<SymbolRef::Type> getSymbolType(DataRefImpl Symb) const override;
  148. Expected<section_iterator> getSymbolSection(DataRefImpl Symb) const override;
  149. uint32_t getSymbolSectionId(SymbolRef Sym) const;
  150. // Overrides from SectionRef.
  151. void moveSectionNext(DataRefImpl &Sec) const override;
  152. Expected<StringRef> getSectionName(DataRefImpl Sec) const override;
  153. uint64_t getSectionAddress(DataRefImpl Sec) const override;
  154. uint64_t getSectionIndex(DataRefImpl Sec) const override;
  155. uint64_t getSectionSize(DataRefImpl Sec) const override;
  156. Expected<ArrayRef<uint8_t>>
  157. getSectionContents(DataRefImpl Sec) const override;
  158. uint64_t getSectionAlignment(DataRefImpl Sec) const override;
  159. bool isSectionCompressed(DataRefImpl Sec) const override;
  160. bool isSectionText(DataRefImpl Sec) const override;
  161. bool isSectionData(DataRefImpl Sec) const override;
  162. bool isSectionBSS(DataRefImpl Sec) const override;
  163. bool isSectionVirtual(DataRefImpl Sec) const override;
  164. relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
  165. relocation_iterator section_rel_end(DataRefImpl Sec) const override;
  166. // Overrides from RelocationRef.
  167. void moveRelocationNext(DataRefImpl &Rel) const override;
  168. uint64_t getRelocationOffset(DataRefImpl Rel) const override;
  169. symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
  170. uint64_t getRelocationType(DataRefImpl Rel) const override;
  171. void getRelocationTypeName(DataRefImpl Rel,
  172. SmallVectorImpl<char> &Result) const override;
  173. section_iterator section_begin() const override;
  174. section_iterator section_end() const override;
  175. uint8_t getBytesInAddress() const override;
  176. StringRef getFileFormatName() const override;
  177. Triple::ArchType getArch() const override;
  178. SubtargetFeatures getFeatures() const override;
  179. bool isRelocatableObject() const override;
  180. bool isSharedObject() const;
  181. struct ReadContext {
  182. const uint8_t *Start;
  183. const uint8_t *Ptr;
  184. const uint8_t *End;
  185. };
  186. private:
  187. bool isValidFunctionIndex(uint32_t Index) const;
  188. bool isDefinedFunctionIndex(uint32_t Index) const;
  189. bool isValidGlobalIndex(uint32_t Index) const;
  190. bool isValidTableIndex(uint32_t Index) const;
  191. bool isDefinedGlobalIndex(uint32_t Index) const;
  192. bool isDefinedTableIndex(uint32_t Index) const;
  193. bool isValidEventIndex(uint32_t Index) const;
  194. bool isDefinedEventIndex(uint32_t Index) const;
  195. bool isValidFunctionSymbol(uint32_t Index) const;
  196. bool isValidTableSymbol(uint32_t Index) const;
  197. bool isValidGlobalSymbol(uint32_t Index) const;
  198. bool isValidEventSymbol(uint32_t Index) const;
  199. bool isValidDataSymbol(uint32_t Index) const;
  200. bool isValidSectionSymbol(uint32_t Index) const;
  201. wasm::WasmFunction &getDefinedFunction(uint32_t Index);
  202. const wasm::WasmFunction &getDefinedFunction(uint32_t Index) const;
  203. wasm::WasmGlobal &getDefinedGlobal(uint32_t Index);
  204. wasm::WasmEvent &getDefinedEvent(uint32_t Index);
  205. const WasmSection &getWasmSection(DataRefImpl Ref) const;
  206. const wasm::WasmRelocation &getWasmRelocation(DataRefImpl Ref) const;
  207. uint32_t getSymbolSectionIdImpl(const WasmSymbol &Symb) const;
  208. Error parseSection(WasmSection &Sec);
  209. Error parseCustomSection(WasmSection &Sec, ReadContext &Ctx);
  210. // Standard section types
  211. Error parseTypeSection(ReadContext &Ctx);
  212. Error parseImportSection(ReadContext &Ctx);
  213. Error parseFunctionSection(ReadContext &Ctx);
  214. Error parseTableSection(ReadContext &Ctx);
  215. Error parseMemorySection(ReadContext &Ctx);
  216. Error parseEventSection(ReadContext &Ctx);
  217. Error parseGlobalSection(ReadContext &Ctx);
  218. Error parseExportSection(ReadContext &Ctx);
  219. Error parseStartSection(ReadContext &Ctx);
  220. Error parseElemSection(ReadContext &Ctx);
  221. Error parseCodeSection(ReadContext &Ctx);
  222. Error parseDataSection(ReadContext &Ctx);
  223. Error parseDataCountSection(ReadContext &Ctx);
  224. // Custom section types
  225. Error parseDylinkSection(ReadContext &Ctx);
  226. Error parseNameSection(ReadContext &Ctx);
  227. Error parseLinkingSection(ReadContext &Ctx);
  228. Error parseLinkingSectionSymtab(ReadContext &Ctx);
  229. Error parseLinkingSectionComdat(ReadContext &Ctx);
  230. Error parseProducersSection(ReadContext &Ctx);
  231. Error parseTargetFeaturesSection(ReadContext &Ctx);
  232. Error parseRelocSection(StringRef Name, ReadContext &Ctx);
  233. wasm::WasmObjectHeader Header;
  234. std::vector<WasmSection> Sections;
  235. wasm::WasmDylinkInfo DylinkInfo;
  236. wasm::WasmProducerInfo ProducerInfo;
  237. std::vector<wasm::WasmFeatureEntry> TargetFeatures;
  238. std::vector<wasm::WasmSignature> Signatures;
  239. std::vector<uint32_t> FunctionTypes;
  240. std::vector<wasm::WasmTable> Tables;
  241. std::vector<wasm::WasmLimits> Memories;
  242. std::vector<wasm::WasmGlobal> Globals;
  243. std::vector<wasm::WasmEvent> Events;
  244. std::vector<wasm::WasmImport> Imports;
  245. std::vector<wasm::WasmExport> Exports;
  246. std::vector<wasm::WasmElemSegment> ElemSegments;
  247. std::vector<WasmSegment> DataSegments;
  248. llvm::Optional<size_t> DataCount;
  249. std::vector<wasm::WasmFunction> Functions;
  250. std::vector<WasmSymbol> Symbols;
  251. std::vector<wasm::WasmDebugName> DebugNames;
  252. uint32_t StartFunction = -1;
  253. bool HasLinkingSection = false;
  254. bool HasDylinkSection = false;
  255. bool SeenCodeSection = false;
  256. bool HasMemory64 = false;
  257. wasm::WasmLinkingData LinkingData;
  258. uint32_t NumImportedGlobals = 0;
  259. uint32_t NumImportedTables = 0;
  260. uint32_t NumImportedFunctions = 0;
  261. uint32_t NumImportedEvents = 0;
  262. uint32_t CodeSection = 0;
  263. uint32_t DataSection = 0;
  264. uint32_t EventSection = 0;
  265. uint32_t GlobalSection = 0;
  266. uint32_t TableSection = 0;
  267. };
  268. class WasmSectionOrderChecker {
  269. public:
  270. // We define orders for all core wasm sections and known custom sections.
  271. enum : int {
  272. // Sentinel, must be zero
  273. WASM_SEC_ORDER_NONE = 0,
  274. // Core sections
  275. WASM_SEC_ORDER_TYPE,
  276. WASM_SEC_ORDER_IMPORT,
  277. WASM_SEC_ORDER_FUNCTION,
  278. WASM_SEC_ORDER_TABLE,
  279. WASM_SEC_ORDER_MEMORY,
  280. WASM_SEC_ORDER_EVENT,
  281. WASM_SEC_ORDER_GLOBAL,
  282. WASM_SEC_ORDER_EXPORT,
  283. WASM_SEC_ORDER_START,
  284. WASM_SEC_ORDER_ELEM,
  285. WASM_SEC_ORDER_DATACOUNT,
  286. WASM_SEC_ORDER_CODE,
  287. WASM_SEC_ORDER_DATA,
  288. // Custom sections
  289. // "dylink" should be the very first section in the module
  290. WASM_SEC_ORDER_DYLINK,
  291. // "linking" section requires DATA section in order to validate data symbols
  292. WASM_SEC_ORDER_LINKING,
  293. // Must come after "linking" section in order to validate reloc indexes.
  294. WASM_SEC_ORDER_RELOC,
  295. // "name" section must appear after DATA. Comes after "linking" to allow
  296. // symbol table to set default function name.
  297. WASM_SEC_ORDER_NAME,
  298. // "producers" section must appear after "name" section.
  299. WASM_SEC_ORDER_PRODUCERS,
  300. // "target_features" section must appear after producers section
  301. WASM_SEC_ORDER_TARGET_FEATURES,
  302. // Must be last
  303. WASM_NUM_SEC_ORDERS
  304. };
  305. // Sections that may or may not be present, but cannot be predecessors
  306. static int DisallowedPredecessors[WASM_NUM_SEC_ORDERS][WASM_NUM_SEC_ORDERS];
  307. bool isValidSectionOrder(unsigned ID, StringRef CustomSectionName = "");
  308. private:
  309. bool Seen[WASM_NUM_SEC_ORDERS] = {}; // Sections that have been seen already
  310. // Returns -1 for unknown sections.
  311. int getSectionOrder(unsigned ID, StringRef CustomSectionName = "");
  312. };
  313. } // end namespace object
  314. inline raw_ostream &operator<<(raw_ostream &OS, const object::WasmSymbol &Sym) {
  315. Sym.print(OS);
  316. return OS;
  317. }
  318. } // end namespace llvm
  319. #endif // LLVM_OBJECT_WASM_H
  320. #ifdef __GNUC__
  321. #pragma GCC diagnostic pop
  322. #endif