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