29 #include <system_error>
31 #define DEBUG_TYPE "wasm-object"
34 using namespace object;
39 auto ObjectFile = llvm::make_unique<WasmObjectFile>(Buffer, Err);
41 return std::move(Err);
46 #define VARINT7_MAX ((1<<7)-1)
47 #define VARINT7_MIN (-(1<<7))
48 #define VARUINT7_MAX (1<<7)
49 #define VARUINT1_MAX (1)
55 Ptr +=
sizeof(Result);
61 memcpy(&Result, Ptr,
sizeof(Result));
62 Ptr +=
sizeof(Result);
68 memcpy(&Result, Ptr,
sizeof(Result));
69 Ptr +=
sizeof(Result);
96 assert(result <= VARUINT1_MAX && result >= 0);
114 assert(result <= INT32_MAX && result >= INT32_MIN);
120 assert(result <= UINT32_MAX);
152 return make_error<GenericBinaryError>(
"Invalid opcode in init_expr",
158 return make_error<GenericBinaryError>(
"Invalid init_expr",
181 const uint8_t *Start) {
183 Section.Offset = Ptr - Start;
187 return make_error<StringError>(
"Zero length section",
202 Err = make_error<StringError>(
"Bad magic number",
206 const uint8_t *
Ptr = getPtr(4);
209 Err = make_error<StringError>(
"Bad version number",
214 const uint8_t *
Eof = getPtr(
getData().size());
219 if ((Err = parseSection(Sec)))
222 Sections.push_back(Sec);
231 return parseCustomSection(Sec, Start, End);
233 return parseTypeSection(Start, End);
235 return parseImportSection(Start, End);
237 return parseFunctionSection(Start, End);
239 return parseTableSection(Start, End);
241 return parseMemorySection(Start, End);
243 return parseGlobalSection(Start, End);
245 return parseExportSection(Start, End);
247 return parseStartSection(Start, End);
249 return parseElemSection(Start, End);
251 return parseCodeSection(Start, End);
253 return parseDataSection(Start, End);
255 return make_error<GenericBinaryError>(
"Bad section type",
260 Error WasmObjectFile::parseNameSection(
const uint8_t *
Ptr,
const uint8_t *End) {
264 const uint8_t *SubSectionEnd = Ptr +
Size;
272 Symbols.emplace_back(Name,
274 Sections.size(), Index);
284 if (Ptr != SubSectionEnd)
285 return make_error<GenericBinaryError>(
"Name sub-section ended prematurely",
290 return make_error<GenericBinaryError>(
"Name section ended prematurely",
295 Error WasmObjectFile::parseLinkingSection(
const uint8_t *Ptr,
296 const uint8_t *End) {
297 HasLinkingSection =
true;
301 const uint8_t *SubSectionEnd = Ptr +
Size;
307 DEBUG(
dbgs() <<
"reading syminfo: " << Symbol <<
"\n");
309 auto iter = SymbolMap.
find(Symbol);
310 if (iter == SymbolMap.
end()) {
311 return make_error<GenericBinaryError>(
312 "Invalid symbol name in linking section: " +
Symbol,
316 assert(SymIndex < Symbols.size());
317 Symbols[SymIndex].Flags =
Flags;
318 DEBUG(
dbgs() <<
"Set symbol flags index:"
319 << SymIndex <<
" name:"
320 << Symbols[SymIndex].
Name <<
" exptected:"
321 << Symbol <<
" flags: " << Flags <<
"\n");
336 if (Ptr != SubSectionEnd)
337 return make_error<GenericBinaryError>(
341 return make_error<GenericBinaryError>(
"Linking section ended prematurely",
346 WasmSection* WasmObjectFile::findCustomSectionByName(
StringRef Name) {
347 for (WasmSection&
Section : Sections) {
354 WasmSection* WasmObjectFile::findSectionByType(
uint32_t Type) {
356 for (WasmSection&
Section : Sections) {
363 Error WasmObjectFile::parseRelocSection(
StringRef Name,
const uint8_t *Ptr,
364 const uint8_t *End) {
366 WasmSection*
Section =
nullptr;
369 Section = findCustomSectionByName(Name);
371 Section = findSectionByType(SectionCode);
374 return make_error<GenericBinaryError>(
"Invalid section code",
377 while (RelocCount--) {
379 memset(&Reloc, 0,
sizeof(Reloc));
383 switch (Reloc.
Type) {
384 case wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
385 case wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB:
386 case wasm::R_WEBASSEMBLY_TABLE_INDEX_I32:
387 case wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB:
388 case wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
390 case wasm::R_WEBASSEMBLY_GLOBAL_ADDR_LEB:
391 case wasm::R_WEBASSEMBLY_GLOBAL_ADDR_SLEB:
392 case wasm::R_WEBASSEMBLY_GLOBAL_ADDR_I32:
396 return make_error<GenericBinaryError>(
"Bad relocation type: " +
400 Section->Relocations.push_back(Reloc);
403 return make_error<GenericBinaryError>(
"Reloc section ended prematurely",
408 Error WasmObjectFile::parseCustomSection(WasmSection &Sec,
409 const uint8_t *Ptr,
const uint8_t *End) {
411 if (Sec.Name ==
"name") {
412 if (
Error Err = parseNameSection(Ptr, End))
414 }
else if (Sec.Name ==
"linking") {
415 if (
Error Err = parseLinkingSection(Ptr, End))
417 }
else if (Sec.Name.startswith(
"reloc.")) {
418 if (
Error Err = parseRelocSection(Sec.Name, Ptr, End))
424 Error WasmObjectFile::parseTypeSection(
const uint8_t *Ptr,
const uint8_t *End) {
426 Signatures.reserve(Count);
432 return make_error<GenericBinaryError>(
"Invalid signature type",
437 while (ParamCount--) {
443 if (ReturnCount != 1) {
444 return make_error<GenericBinaryError>(
449 Signatures.push_back(Sig);
452 return make_error<GenericBinaryError>(
"Type section ended prematurely",
457 Error WasmObjectFile::parseImportSection(
const uint8_t *Ptr,
const uint8_t *End) {
459 Imports.reserve(Count);
460 for (
uint32_t i = 0; i < Count; i++) {
471 DEBUG(
dbgs() <<
"Adding import: " << Symbols.back()
472 <<
" sym index:" << Symbols.size() <<
"\n");
480 DEBUG(
dbgs() <<
"Adding import: " << Symbols.back()
481 <<
" sym index:" << Symbols.size() <<
"\n");
489 return make_error<GenericBinaryError>(
"Invalid table element type",
494 return make_error<GenericBinaryError>(
497 Imports.push_back(Im);
500 return make_error<GenericBinaryError>(
"Import section ended prematurely",
505 Error WasmObjectFile::parseFunctionSection(
const uint8_t *Ptr,
const uint8_t *End) {
507 FunctionTypes.reserve(Count);
512 return make_error<GenericBinaryError>(
"Function section ended prematurely",
517 Error WasmObjectFile::parseTableSection(
const uint8_t *Ptr,
const uint8_t *End) {
519 Tables.reserve(Count);
523 return make_error<GenericBinaryError>(
"Invalid table element type",
528 return make_error<GenericBinaryError>(
"Table section ended prematurely",
533 Error WasmObjectFile::parseMemorySection(
const uint8_t *Ptr,
const uint8_t *End) {
535 Memories.reserve(Count);
540 return make_error<GenericBinaryError>(
"Memory section ended prematurely",
545 Error WasmObjectFile::parseGlobalSection(
const uint8_t *Ptr,
const uint8_t *End) {
547 Globals.reserve(Count);
554 Globals.push_back(Global);
557 return make_error<GenericBinaryError>(
"Global section ended prematurely",
562 Error WasmObjectFile::parseExportSection(
const uint8_t *Ptr,
const uint8_t *End) {
564 Exports.reserve(Count);
565 for (
uint32_t i = 0; i < Count; i++) {
571 bool MakeSymbol =
false;
585 return make_error<GenericBinaryError>(
591 Symbols.emplace_back(Ex.
Name, ExportType,
593 DEBUG(
dbgs() <<
"Adding export: " << Symbols.back()
594 <<
" sym index:" << Symbols.size() <<
"\n");
596 uint32_t SymIndex = Pair.first->second;
597 Symbols[SymIndex] = WasmSymbol(Ex.
Name, ExportType, Sections.
size(), i);
598 DEBUG(
dbgs() <<
"Replacing existing symbol: " << Symbols[SymIndex]
599 <<
" sym index:" << SymIndex <<
"\n");
602 Exports.push_back(Ex);
605 return make_error<GenericBinaryError>(
"Export section ended prematurely",
610 Error WasmObjectFile::parseStartSection(
const uint8_t *Ptr,
const uint8_t *End) {
612 if (StartFunction >= FunctionTypes.size())
613 return make_error<GenericBinaryError>(
"Invalid start function",
618 Error WasmObjectFile::parseCodeSection(
const uint8_t *Ptr,
const uint8_t *End) {
620 if (FunctionCount != FunctionTypes.size()) {
621 return make_error<GenericBinaryError>(
"Invalid function count",
627 while (FunctionCount--) {
630 const uint8_t *FunctionEnd = Ptr + FunctionSize;
633 Function.
Locals.reserve(NumLocalDecls);
634 while (NumLocalDecls--) {
638 Function.
Locals.push_back(Decl);
644 assert(Ptr == FunctionEnd);
645 Functions.push_back(Function);
648 return make_error<GenericBinaryError>(
"Code section ended prematurely",
653 Error WasmObjectFile::parseElemSection(
const uint8_t *Ptr,
const uint8_t *End) {
655 ElemSegments.reserve(Count);
660 return make_error<GenericBinaryError>(
"Invalid TableIndex",
669 ElemSegments.push_back(Segment);
672 return make_error<GenericBinaryError>(
"Elem section ended prematurely",
677 Error WasmObjectFile::parseDataSection(
const uint8_t *Ptr,
const uint8_t *End) {
678 const uint8_t *Start =
Ptr;
680 DataSegments.reserve(Count);
688 Segment.SectionOffset = Ptr - Start;
690 DataSegments.push_back(Segment);
693 return make_error<GenericBinaryError>(
"Data section ended prematurely",
698 const uint8_t *WasmObjectFile::getPtr(
size_t Offset)
const {
712 DEBUG(
dbgs() <<
"getSymbolFlags: ptr=" << &Sym <<
" " << Sym <<
"\n");
746 Ref.
d.
a = Symbols.size();
751 return Symbols[Symb.
d.
a];
822 case wasm::WASM_SEC_##X: \
844 return std::error_code();
865 return std::error_code();
892 RelocRef.
d.
a = Ref.
d.
a;
900 RelocRef.
d.
a = Ref.
d.
a;
930 #define WASM_RELOC(name, value) \
936 #include "llvm/BinaryFormat/WasmRelocs/WebAssembly.def"
952 Ref.
d.
a = Sections.size();
967 return HasLinkingSection;
972 return Sections[Ref.
d.
a];
987 assert(Ref.d.a < Sections.size());
988 const WasmSection& Sec = Sections[Ref.d.a];
989 assert(Ref.d.b < Sec.Relocations.size());
990 return Sec.Relocations[Ref.d.b];
bool isSectionText(DataRefImpl Sec) const override
uint64_t getRelocationType(DataRefImpl Rel) const override
uint64_t getSectionAddress(DataRefImpl Sec) const override
uint64_t getSymbolValueImpl(DataRefImpl Symb) const override
StringRef getFileFormatName() const override
uint64_t getSectionSize(DataRefImpl Sec) const override
static uint8_t readVaruint7(const uint8_t *&Ptr)
section_iterator section_begin() const override
static Error readInitExpr(wasm::WasmInitExpr &Expr, const uint8_t *&Ptr)
const wasm::WasmRelocation & getWasmRelocation(const RelocationRef &Ref) const
static wasm::WasmLimits readLimits(const uint8_t *&Ptr)
iterator find(StringRef Key)
This class is the base class for all object file types.
uint8_t getBytesInAddress() const override
The number of bytes used to represent an address in this object file format.
uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override
unsigned getArch() const override
std::error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const override
std::error_code getSectionName(DataRefImpl Sec, StringRef &Res) const override
static StringRef readString(const uint8_t *&Ptr)
void getRelocationTypeName(DataRefImpl Rel, SmallVectorImpl< char > &Result) const override
static int64_t readVarint64(const uint8_t *&Ptr)
std::vector< wasm::WasmRelocation > Relocations
uint64_t getRelocationOffset(DataRefImpl Rel) const override
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
uint32_t getSymbolAlignment(DataRefImpl Symb) const override
const WasmSection & getWasmSection(const SectionRef &Section) const
static int32_t readFloat32(const uint8_t *&Ptr)
int64_t decodeSLEB128(const uint8_t *p, unsigned *n=nullptr, const uint8_t *end=nullptr, const char **error=nullptr)
Utility function to decode a SLEB128 value.
StringRef getData() const
Tagged union holding either a T or a Error.
static Error readSection(WasmSection &Section, const uint8_t *&Ptr, const uint8_t *Start)
static int64_t readLEB128(const uint8_t *&Ptr)
const uint32_t WasmVersion
content_iterator< SectionRef > section_iterator
static uint32_t readVaruint32(const uint8_t *&Ptr)
bool isSectionBitcode(DataRefImpl Sec) const override
Expected< SymbolRef::Type > getSymbolType(DataRefImpl Symb) const override
uint64_t getSectionIndex(DataRefImpl Sec) const override
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
size_t size() const
size - Get the array size.
void moveSectionNext(DataRefImpl &Sec) const override
bool isRelocatableObject() const override
True if this is a relocatable object (.o/.obj).
std::vector< uint32_t > Functions
static int64_t readFloat64(const uint8_t *&Ptr)
ArrayRef< uint8_t > Content
The instances of the Type class are immutable: once they are created, they are never changed...
section_iterator section_end() const override
static uint8_t readOpcode(const uint8_t *&Ptr)
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
const wasm::WasmObjectHeader & getHeader() const
uint64_t decodeULEB128(const uint8_t *p, unsigned *n=nullptr, const uint8_t *end=nullptr, const char **error=nullptr)
Utility function to decode a ULEB128 value.
static int32_t readVarint32(const uint8_t *&Ptr)
static const unsigned End
uint64_t getSectionAlignment(DataRefImpl Sec) const override
union llvm::wasm::WasmInitExpr::@125 Value
bool isSectionBSS(DataRefImpl Sec) const override
static uint8_t readVaruint1(const uint8_t *&Ptr)
struct llvm::object::DataRefImpl::@230 d
bool isSectionData(DataRefImpl Sec) const override
basic_symbol_iterator symbol_begin() const override
static Expected< std::unique_ptr< WasmObjectFile > > createWasmObjectFile(MemoryBufferRef Object)
std::pair< iterator, bool > try_emplace(StringRef Key, ArgsTy &&...Args)
Emplace a new element for the specified key into the map if the key isn't already in the map...
SubtargetFeatures getFeatures() const override
static int8_t readVarint7(const uint8_t *&Ptr)
bool isSectionCompressed(DataRefImpl Sec) const override
static ErrorSuccess success()
Create a success value.
static uint64_t readULEB128(const uint8_t *&Ptr)
const WasmSymbol & getWasmSymbol(const DataRefImpl &Symb) const
friend class RelocationRef
void moveSymbolNext(DataRefImpl &Symb) const override
symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Manages the enabling and disabling of subtarget specific features.
Helper for Errors used as out-parameters.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
This is a value type class that represents a single symbol in the list of symbols in the object file...
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
llvm_unreachable("Unknown RegBank!")
static std::vector< std::string > Flags
Expected< StringRef > getSymbolName(DataRefImpl Symb) const override
void moveRelocationNext(DataRefImpl &Rel) const override
uint64_t getSymbolValue(DataRefImpl Symb) const
uint32_t read32le(const void *P)
This is a value type class that represents a single symbol in the list of symbols in the object file...
static wasm::WasmTable readTable(const uint8_t *&Ptr)
Expected< uint64_t > getSymbolAddress(DataRefImpl Symb) const override
static uint32_t readUint32(const uint8_t *&Ptr)
relocation_iterator section_rel_begin(DataRefImpl Sec) const override
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
std::vector< int32_t > ParamTypes
Lightweight error class with error context and mandatory checking.
content_iterator< RelocationRef > relocation_iterator
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
StringRef - Represent a constant reference to a string, i.e.
bool isSectionVirtual(DataRefImpl Sec) const override
Expected< section_iterator > getSymbolSection(DataRefImpl Symb) const override
uint32_t getSymbolFlags(DataRefImpl Symb) const override
static uint8_t readUint8(const uint8_t *&Ptr)
std::vector< WasmLocalDecl > Locals
relocation_iterator section_rel_end(DataRefImpl Sec) const override
WasmObjectFile(MemoryBufferRef Object, Error &Err)
basic_symbol_iterator symbol_end() const override