diff options
Diffstat (limited to 'include/llvm/Object/Wasm.h')
-rw-r--r-- | include/llvm/Object/Wasm.h | 137 |
1 files changed, 69 insertions, 68 deletions
diff --git a/include/llvm/Object/Wasm.h b/include/llvm/Object/Wasm.h index 71951d83f3cc..fd34e45feb62 100644 --- a/include/llvm/Object/Wasm.h +++ b/include/llvm/Object/Wasm.h @@ -21,6 +21,7 @@ #include "llvm/ADT/StringRef.h" #include "llvm/ADT/StringMap.h" #include "llvm/BinaryFormat/Wasm.h" +#include "llvm/Config/llvm-config.h" #include "llvm/Object/Binary.h" #include "llvm/Object/ObjectFile.h" #include "llvm/Support/Error.h" @@ -34,61 +35,49 @@ namespace object { class WasmSymbol { public: - enum class SymbolType { - FUNCTION_IMPORT, - FUNCTION_EXPORT, - GLOBAL_IMPORT, - GLOBAL_EXPORT, - DEBUG_FUNCTION_NAME, - }; - - WasmSymbol(StringRef Name, SymbolType Type, uint32_t Section, - uint32_t ElementIndex, uint32_t FunctionType = 0) - : Name(Name), Type(Type), Section(Section), ElementIndex(ElementIndex), - FunctionType(FunctionType) {} + WasmSymbol(const wasm::WasmSymbolInfo &Info, + const wasm::WasmSignature *FunctionType, + const wasm::WasmGlobalType *GlobalType) + : Info(Info), FunctionType(FunctionType), GlobalType(GlobalType) {} - StringRef Name; - SymbolType Type; - uint32_t Section; - uint32_t Flags = 0; + const wasm::WasmSymbolInfo &Info; + const wasm::WasmSignature *FunctionType; + const wasm::WasmGlobalType *GlobalType; - // Index into either the function or global index space. - uint32_t ElementIndex; - - // For function, the type index - uint32_t FunctionType; + bool isTypeFunction() const { + return Info.Kind == wasm::WASM_SYMBOL_TYPE_FUNCTION; + } - // Symbols can be both exported and imported (in the case of the weakly - // defined symbol). In this the import index is stored as AltIndex. - uint32_t AltIndex = 0; - bool HasAltIndex = false; + bool isTypeData() const { return Info.Kind == wasm::WASM_SYMBOL_TYPE_DATA; } - void setAltIndex(uint32_t Index) { - HasAltIndex = true; - AltIndex = Index; + bool isTypeGlobal() const { + return Info.Kind == wasm::WASM_SYMBOL_TYPE_GLOBAL; } - bool isFunction() const { - return Type == WasmSymbol::SymbolType::FUNCTION_IMPORT || - Type == WasmSymbol::SymbolType::FUNCTION_EXPORT || - Type == WasmSymbol::SymbolType::DEBUG_FUNCTION_NAME; + bool isTypeSection() const { + return Info.Kind == wasm::WASM_SYMBOL_TYPE_SECTION; } + bool isDefined() const { return !isUndefined(); } - bool isWeak() const { + bool isUndefined() const { + return (Info.Flags & wasm::WASM_SYMBOL_UNDEFINED) != 0; + } + + bool isBindingWeak() const { return getBinding() == wasm::WASM_SYMBOL_BINDING_WEAK; } - bool isGlobal() const { + bool isBindingGlobal() const { return getBinding() == wasm::WASM_SYMBOL_BINDING_GLOBAL; } - bool isLocal() const { + bool isBindingLocal() const { return getBinding() == wasm::WASM_SYMBOL_BINDING_LOCAL; } unsigned getBinding() const { - return Flags & wasm::WASM_SYMBOL_BINDING_MASK; + return Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK; } bool isHidden() const { @@ -96,16 +85,13 @@ public: } unsigned getVisibility() const { - return Flags & wasm::WASM_SYMBOL_VISIBILITY_MASK; + return Info.Flags & wasm::WASM_SYMBOL_VISIBILITY_MASK; } - void print(raw_ostream &Out) const { - Out << "Name=" << Name << ", Type=" << static_cast<int>(Type) - << ", Flags=" << Flags << " ElemIndex=" << ElementIndex; - } + void print(raw_ostream &Out) const; #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) - LLVM_DUMP_METHOD void dump() const { print(dbgs()); } + LLVM_DUMP_METHOD void dump() const; #endif }; @@ -144,12 +130,16 @@ public: ArrayRef<wasm::WasmLimits> memories() const { return Memories; } ArrayRef<wasm::WasmGlobal> globals() const { return Globals; } ArrayRef<wasm::WasmExport> exports() const { return Exports; } + ArrayRef<WasmSymbol> syms() const { return Symbols; } const wasm::WasmLinkingData& linkingData() const { return LinkingData; } uint32_t getNumberOfSymbols() const { return Symbols.size(); } ArrayRef<wasm::WasmElemSegment> elements() const { return ElemSegments; } ArrayRef<WasmSegment> dataSegments() const { return DataSegments; } ArrayRef<wasm::WasmFunction> functions() const { return Functions; } + ArrayRef<wasm::WasmFunctionName> debugNames() const { return DebugNames; } uint32_t startFunction() const { return StartFunction; } + uint32_t getNumImportedGlobals() const { return NumImportedGlobals; } + uint32_t getNumImportedFunctions() const { return NumImportedFunctions; } void moveSymbolNext(DataRefImpl &Symb) const override; @@ -203,39 +193,50 @@ public: SubtargetFeatures getFeatures() const override; bool isRelocatableObject() const override; + struct ReadContext { + const uint8_t *Start; + const uint8_t *Ptr; + const uint8_t *End; + }; + private: bool isValidFunctionIndex(uint32_t Index) const; + bool isDefinedFunctionIndex(uint32_t Index) const; + bool isValidGlobalIndex(uint32_t Index) const; + bool isDefinedGlobalIndex(uint32_t Index) const; + bool isValidFunctionSymbol(uint32_t Index) const; + bool isValidGlobalSymbol(uint32_t Index) const; + bool isValidDataSymbol(uint32_t Index) const; + bool isValidSectionSymbol(uint32_t Index) const; + wasm::WasmFunction &getDefinedFunction(uint32_t Index); + wasm::WasmGlobal &getDefinedGlobal(uint32_t Index); + const WasmSection &getWasmSection(DataRefImpl Ref) const; const wasm::WasmRelocation &getWasmRelocation(DataRefImpl Ref) const; - WasmSection* findCustomSectionByName(StringRef Name); - WasmSection* findSectionByType(uint32_t Type); - const uint8_t *getPtr(size_t Offset) const; Error parseSection(WasmSection &Sec); - Error parseCustomSection(WasmSection &Sec, const uint8_t *Ptr, - const uint8_t *End); + Error parseCustomSection(WasmSection &Sec, ReadContext &Ctx); // Standard section types - Error parseTypeSection(const uint8_t *Ptr, const uint8_t *End); - Error parseImportSection(const uint8_t *Ptr, const uint8_t *End); - Error parseFunctionSection(const uint8_t *Ptr, const uint8_t *End); - Error parseTableSection(const uint8_t *Ptr, const uint8_t *End); - Error parseMemorySection(const uint8_t *Ptr, const uint8_t *End); - Error parseGlobalSection(const uint8_t *Ptr, const uint8_t *End); - Error parseExportSection(const uint8_t *Ptr, const uint8_t *End); - Error parseStartSection(const uint8_t *Ptr, const uint8_t *End); - Error parseElemSection(const uint8_t *Ptr, const uint8_t *End); - Error parseCodeSection(const uint8_t *Ptr, const uint8_t *End); - Error parseDataSection(const uint8_t *Ptr, const uint8_t *End); + Error parseTypeSection(ReadContext &Ctx); + Error parseImportSection(ReadContext &Ctx); + Error parseFunctionSection(ReadContext &Ctx); + Error parseTableSection(ReadContext &Ctx); + Error parseMemorySection(ReadContext &Ctx); + Error parseGlobalSection(ReadContext &Ctx); + Error parseExportSection(ReadContext &Ctx); + Error parseStartSection(ReadContext &Ctx); + Error parseElemSection(ReadContext &Ctx); + Error parseCodeSection(ReadContext &Ctx); + Error parseDataSection(ReadContext &Ctx); // Custom section types - Error parseNameSection(const uint8_t *Ptr, const uint8_t *End); - Error parseLinkingSection(const uint8_t *Ptr, const uint8_t *End); - Error parseRelocSection(StringRef Name, const uint8_t *Ptr, - const uint8_t *End); - - void populateSymbolTable(); + Error parseNameSection(ReadContext &Ctx); + Error parseLinkingSection(ReadContext &Ctx); + Error parseLinkingSectionSymtab(ReadContext &Ctx); + Error parseLinkingSectionComdat(ReadContext &Ctx); + Error parseRelocSection(StringRef Name, ReadContext &Ctx); wasm::WasmObjectHeader Header; std::vector<WasmSection> Sections; @@ -250,15 +251,15 @@ private: std::vector<WasmSegment> DataSegments; std::vector<wasm::WasmFunction> Functions; std::vector<WasmSymbol> Symbols; + std::vector<wasm::WasmFunctionName> DebugNames; uint32_t StartFunction = -1; bool HasLinkingSection = false; wasm::WasmLinkingData LinkingData; uint32_t NumImportedGlobals = 0; uint32_t NumImportedFunctions = 0; - uint32_t ImportSection = 0; - uint32_t ExportSection = 0; - - StringMap<uint32_t> SymbolMap; + uint32_t CodeSection = 0; + uint32_t DataSection = 0; + uint32_t GlobalSection = 0; }; } // end namespace object |