Index: include/llvm/BinaryFormat/Wasm.h =================================================================== --- include/llvm/BinaryFormat/Wasm.h +++ include/llvm/BinaryFormat/Wasm.h @@ -150,7 +150,11 @@ uint32_t ElementIndex; // For a data symbols, the address of the data relative to segment. WasmDataReference DataRef; + // For a section symbols, the code of the section (see also SectionName). + uint32_t SectionCode; }; + // Present for a section symbols, when code is a custom section. + StringRef SectionName; }; struct WasmFunctionName { @@ -245,8 +249,9 @@ // Kind codes used in the custom "linking" section in the WASM_SYMBOL_TABLE enum WasmSymbolType : unsigned { WASM_SYMBOL_TYPE_FUNCTION = 0x0, - WASM_SYMBOL_TYPE_DATA = 0x1, - WASM_SYMBOL_TYPE_GLOBAL = 0x2, + WASM_SYMBOL_TYPE_DATA = 0x1, + WASM_SYMBOL_TYPE_GLOBAL = 0x2, + WASM_SYMBOL_TYPE_SECTION = 0x3, }; const unsigned WASM_SYMBOL_BINDING_MASK = 0x3; Index: include/llvm/BinaryFormat/WasmRelocs.def =================================================================== --- include/llvm/BinaryFormat/WasmRelocs.def +++ include/llvm/BinaryFormat/WasmRelocs.def @@ -11,3 +11,5 @@ WASM_RELOC(R_WEBASSEMBLY_MEMORY_ADDR_I32, 5) WASM_RELOC(R_WEBASSEMBLY_TYPE_INDEX_LEB, 6) WASM_RELOC(R_WEBASSEMBLY_GLOBAL_INDEX_LEB, 7) +WASM_RELOC(R_WEBASSEMBLY_FUNCTION_OFFSET_I32, 8) +WASM_RELOC(R_WEBASSEMBLY_SECTION_OFFSET_I32, 9) Index: include/llvm/Object/RelocVisitor.h =================================================================== --- include/llvm/Object/RelocVisitor.h +++ include/llvm/Object/RelocVisitor.h @@ -23,6 +23,7 @@ #include "llvm/Object/ELFObjectFile.h" #include "llvm/Object/MachO.h" #include "llvm/Object/ObjectFile.h" +#include "llvm/Object/Wasm.h" #include "llvm/Support/Casting.h" #include "llvm/Support/ErrorHandling.h" #include @@ -46,6 +47,8 @@ return visitCOFF(Rel, R, Value); if (isa(ObjToVisit)) return visitMachO(Rel, R, Value); + if (isa(ObjToVisit)) + return visitWasm(Rel, R, Value); HasError = true; return 0; @@ -316,6 +319,29 @@ HasError = true; return 0; } + + uint64_t visitWasm(uint32_t Rel, RelocationRef R, uint64_t Value) { + if (ObjToVisit.getArch() == Triple::wasm32) { + switch (Rel) { + case wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB: + case wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB: + case wasm::R_WEBASSEMBLY_TABLE_INDEX_I32: + case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB: + case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB: + case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32: + case wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB: + case wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB: + case wasm::R_WEBASSEMBLY_FUNCTION_OFFSET_I32: + case wasm::R_WEBASSEMBLY_SECTION_OFFSET_I32: + // For wasm section, its offset at 0 -- ignoring Value + // TODO do we want to use absolute section offset in the wasm file? + HasError = false; + return 0; + } + } + HasError = true; + return 0; + } }; } // end namespace object Index: include/llvm/Object/Wasm.h =================================================================== --- include/llvm/Object/Wasm.h +++ include/llvm/Object/Wasm.h @@ -18,8 +18,9 @@ #define LLVM_OBJECT_WASM_H #include "llvm/ADT/ArrayRef.h" -#include "llvm/ADT/StringRef.h" #include "llvm/ADT/StringMap.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/ADT/StringSet.h" #include "llvm/BinaryFormat/Wasm.h" #include "llvm/Object/Binary.h" #include "llvm/Object/ObjectFile.h" @@ -53,6 +54,10 @@ return Info.Kind == wasm::WASM_SYMBOL_TYPE_GLOBAL; } + bool isTypeSection() const { + return Info.Kind == wasm::WASM_SYMBOL_TYPE_SECTION; + } + bool isDefined() const { return !isUndefined(); } bool isUndefined() const { @@ -206,6 +211,7 @@ 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); @@ -255,6 +261,7 @@ std::vector Functions; std::vector Symbols; std::vector DebugNames; + StringSet<> InternedNames; uint32_t StartFunction = -1; bool HasLinkingSection = false; wasm::WasmLinkingData LinkingData; Index: include/llvm/ObjectYAML/WasmYAML.h =================================================================== --- include/llvm/ObjectYAML/WasmYAML.h +++ include/llvm/ObjectYAML/WasmYAML.h @@ -138,7 +138,9 @@ union { uint32_t ElementIndex; wasm::WasmDataReference DataRef; + SectionType SectionType; }; + StringRef SectionName; // present when SectionType is a custom section }; struct InitFunction { Index: lib/MC/WasmObjectWriter.cpp =================================================================== --- lib/MC/WasmObjectWriter.cpp +++ lib/MC/WasmObjectWriter.cpp @@ -48,7 +48,9 @@ struct SectionBookkeeping { // Where the size of the section is written. uint64_t SizeOffset; - // Where the contents of the section starts (after the header). + // Where the section header ends (without custom section name). + uint64_t PayloadOffset; + // Where the contents of the section starts. uint64_t ContentsOffset; }; @@ -150,6 +152,8 @@ case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB: case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB: case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32: + case wasm::R_WEBASSEMBLY_FUNCTION_OFFSET_I32: + case wasm::R_WEBASSEMBLY_SECTION_OFFSET_I32: return true; default: return false; @@ -168,11 +172,15 @@ }; struct WasmCustomSection { + enum WasmCustomSectionKind { Custom, Debug }; + StringRef Name; - const SmallVectorImpl &Contents; + MCSectionWasm *Section; + WasmCustomSectionKind Kind; - WasmCustomSection(StringRef Name, const SmallVectorImpl &Contents) - : Name(Name), Contents(Contents) {} + WasmCustomSection(StringRef Name, MCSectionWasm *Section, + WasmCustomSectionKind Kind) + : Name(Name), Section(Section), Kind(Kind) {} }; #if !defined(NDEBUG) @@ -192,6 +200,9 @@ // Relocations for fixing up references in the data section. std::vector DataRelocations; + // Relocations for fixing up references in the custom sections. + StringMap> CustomSectionsRelocations; + // Index values to use for fixing up call_indirect type indices. // Maps function symbols to the index of the type of the function DenseMap TypeIndices; @@ -204,6 +215,10 @@ DenseMap WasmIndices; // Maps data symbols to the Wasm segment and offset/size with the segment. DenseMap DataLocations; + // Maps section symbols to the section. + DenseMap CustomSectionSymbols; + // Maps section symbols to the output section offset. + DenseMap CustomSectionOutputOffsets; DenseMap FunctionTypeIndices; @@ -236,15 +251,19 @@ void reset() override { CodeRelocations.clear(); DataRelocations.clear(); + CustomSectionsRelocations.clear(); TypeIndices.clear(); SymbolIndices.clear(); WasmIndices.clear(); TableIndices.clear(); DataLocations.clear(); + CustomSections.clear(); FunctionTypeIndices.clear(); FunctionTypes.clear(); Globals.clear(); DataSegments.clear(); + CustomSectionSymbols.clear(); + CustomSectionOutputOffsets.clear(); MCObjectWriter::reset(); NumFunctionImports = 0; NumGlobalImports = 0; @@ -282,11 +301,16 @@ void writeDataSection(); void writeCodeRelocSection(); void writeDataRelocSection(); + void writeCustomRelocSections(); void writeLinkingMetaDataSection( ArrayRef SymbolInfos, ArrayRef> InitFuncs, const std::map> &Comdats); - void writeUserCustomSections(ArrayRef CustomSections); + void writeCustomSections(const MCAssembler &Asm, const MCAsmLayout &Layout, + ArrayRef CustomSections); + void + updateCustomSectionRelocations(const SmallVector &Functions, + const MCAsmLayout &Layout); uint32_t getProvisionalValue(const WasmRelocationEntry &RelEntry); void applyRelocations(ArrayRef Relocations, @@ -318,20 +342,23 @@ // for any 32-bit value; we'll patch it later. encodeULEB128(UINT32_MAX, getStream()); - // The position where the section starts, for measuring its size. - Section.ContentsOffset = getStream().tell(); + // The position where the section header ends, for measuring its size. + Section.PayloadOffset = getStream().tell(); // Custom sections in wasm also have a string identifier. if (SectionId == wasm::WASM_SEC_CUSTOM) { assert(Name); writeString(Name); } + + // The position where the section starts. + Section.ContentsOffset = getStream().tell(); } // Now that the section is complete and we know how big it is, patch up the // section size field at the start of the section. void WasmObjectWriter::endSection(SectionBookkeeping &Section) { - uint64_t Size = getStream().tell() - Section.ContentsOffset; + uint64_t Size = getStream().tell() - Section.PayloadOffset; if (uint32_t(Size) != Size) report_fatal_error("section size does not fit in a uint32_t"); @@ -372,9 +399,13 @@ if (FixupSection.getSectionName().startswith(".init_array")) return; - // TODO(sbc): Add support for debug sections. - if (FixupSection.getKind().isMetadata()) - return; + bool IsDebugFixup = false; + // TODO: Add support for non-debug metadata sections? + if (FixupSection.getKind().isMetadata()) { + if (!FixupSection.getSectionName().startswith(".debug_")) + return; + IsDebugFixup = true; + } if (const MCSymbolRefExpr *RefB = Target.getSymB()) { assert(RefB->getKind() == MCSymbolRefExpr::VK_None && @@ -444,6 +475,22 @@ WasmRelocationEntry Rec(FixupOffset, SymA, C, Type, &FixupSection); DEBUG(dbgs() << "WasmReloc: " << Rec << "\n"); + if (IsDebugFixup) { + switch (Type) { + case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32: + case wasm::R_WEBASSEMBLY_FUNCTION_OFFSET_I32: + case wasm::R_WEBASSEMBLY_SECTION_OFFSET_I32: + break; + default: + report_fatal_error("relocation in debug section supported only for " + "R_WEBASSEMBLY_MEMORY_ADDR_I32 or _OFFSET_I32"); + } + + const auto &FixupSectionName = FixupSection.getSectionName().str(); + CustomSectionsRelocations[FixupSectionName].push_back(Rec); + return; + } + // Relocation other than R_WEBASSEMBLY_TYPE_INDEX_LEB are currently required // to be against a named symbol. // TODO(sbc): Add support for relocations against unnamed temporaries such @@ -521,6 +568,15 @@ report_fatal_error("symbol not found in wasm index space: " + RelEntry.Symbol->getName()); return WasmIndices[RelEntry.Symbol]; + case wasm::R_WEBASSEMBLY_FUNCTION_OFFSET_I32: { + const auto &Section = + static_cast(RelEntry.Symbol->getSection()); + return Section.getSectionOffset() + RelEntry.Addend; + } + case wasm::R_WEBASSEMBLY_SECTION_OFFSET_I32: { + const auto &Section = *CustomSectionSymbols.find(RelEntry.Symbol)->second; + return Section.getSectionOffset() + RelEntry.Addend; + } case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB: case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32: case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB: { @@ -612,6 +668,8 @@ break; case wasm::R_WEBASSEMBLY_TABLE_INDEX_I32: case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32: + case wasm::R_WEBASSEMBLY_FUNCTION_OFFSET_I32: + case wasm::R_WEBASSEMBLY_SECTION_OFFSET_I32: WriteI32(Stream, Value, Offset); break; case wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB: @@ -871,6 +929,25 @@ endSection(Section); } +void WasmObjectWriter::writeCustomRelocSections() { + for (const auto &P : CustomSectionsRelocations) { + auto CustomSectionName = P.first(); + auto CustomSectionRelocations = P.second; + + SectionBookkeeping Section; + startSection(Section, wasm::WASM_SEC_CUSTOM, + ("reloc." + CustomSectionName).str().c_str()); + + encodeULEB128(wasm::WASM_SEC_CUSTOM, getStream()); + writeString(CustomSectionName); + encodeULEB128(CustomSectionRelocations.size(), getStream()); + + writeRelocations(CustomSectionRelocations); + + endSection(Section); + } +} + void WasmObjectWriter::writeLinkingMetaDataSection( ArrayRef SymbolInfos, ArrayRef> InitFuncs, @@ -900,6 +977,11 @@ encodeULEB128(Sym.DataRef.Size, getStream()); } break; + case wasm::WASM_SYMBOL_TYPE_SECTION: + encodeULEB128(Sym.SectionCode, getStream()); + if (Sym.SectionCode == wasm::WASM_SEC_CUSTOM) + writeString(Sym.SectionName); + break; default: llvm_unreachable("unexpected kind"); } @@ -946,17 +1028,70 @@ endSection(Section); } -void WasmObjectWriter::writeUserCustomSections( +void WasmObjectWriter::writeCustomSections( + const MCAssembler &Asm, const MCAsmLayout &Layout, ArrayRef CustomSections) { - for (const auto &CustomSection : CustomSections) { + for (auto &CustomSection : CustomSections) { SectionBookkeeping Section; startSection(Section, wasm::WASM_SEC_CUSTOM, CustomSection.Name.str().c_str()); - writeBytes(CustomSection.Contents); + + auto *Sec = CustomSection.Section; + Sec->setSectionOffset(getStream().tell() - Section.ContentsOffset); + Asm.writeSectionData(Sec, Layout); + + CustomSectionOutputOffsets[Sec] = Section.ContentsOffset; + endSection(Section); } } +void WasmObjectWriter::updateCustomSectionRelocations( + const SmallVector &Functions, const MCAsmLayout &Layout) { + std::map SectionSymbols; + for (const auto &P : CustomSectionSymbols) + SectionSymbols[P.second] = P.first; + std::map FuncSymbols; + for (const auto &FuncInfo : Functions) + FuncSymbols[&FuncInfo.Sym->getSection()] = FuncInfo.Sym; + StringMap CustomSectionsByName; + for (auto &P : CustomSectionSymbols) + CustomSectionsByName[P.second->getSectionName()] = P.second; + + // Patch relocation records for R_WEBASSEMBLY_FUNCTION_OFFSET_I32 and + // R_WEBASSEMBLY_SECTION_OFFSET_I32. The Addend is stuffed the offset from + // the beginning of the function or custom section -- all such relocations + // target the function or custom section starts. + for (auto &P : CustomSectionsRelocations) { + for (WasmRelocationEntry &RelEntry : P.second) { + switch (RelEntry.Type) { + case wasm::R_WEBASSEMBLY_FUNCTION_OFFSET_I32: { + assert(RelEntry.hasAddend()); + auto &Section = + static_cast(RelEntry.Symbol->getSection()); + RelEntry.Addend += Layout.getSymbolOffset(*RelEntry.Symbol); + RelEntry.Symbol = FuncSymbols[&Section]; + break; + } + case wasm::R_WEBASSEMBLY_SECTION_OFFSET_I32: { + assert(RelEntry.hasAddend()); + auto &Section = + static_cast(RelEntry.Symbol->getSection()); + RelEntry.Addend += Layout.getSymbolOffset(*RelEntry.Symbol); + RelEntry.Symbol = SectionSymbols[&Section]; + break; + } + default: + break; + } + } + + // Apply fixups. + const auto &Section = CustomSectionsByName.find(P.first())->second; + applyRelocations(P.second, CustomSectionOutputOffsets[Section]); + } +} + uint32_t WasmObjectWriter::getFunctionType(const MCSymbolWasm& Symbol) { assert(Symbol.isFunction()); assert(TypeIndices.count(&Symbol)); @@ -1063,8 +1198,7 @@ for (MCSection &Sec : Asm) { auto &Section = static_cast(Sec); - if (cast(Sec).getSectionName().startswith( - ".custom_section.")) { + if (Section.getSectionName().startswith(".custom_section.")) { if (Section.getFragmentList().empty()) continue; if (Section.getFragmentList().size() != 1) @@ -1077,8 +1211,8 @@ if (!DataFrag.getFixups().empty()) report_fatal_error("fixups not supported in .custom_section section"); StringRef UserName = Section.getSectionName().substr(16); - const SmallVectorImpl &Contents = DataFrag.getContents(); - CustomSections.push_back(WasmCustomSection(UserName, Contents)); + CustomSections.push_back( + WasmCustomSection(UserName, &Section, WasmCustomSection::Custom)); continue; } @@ -1108,6 +1242,32 @@ } } + // Create symbols for debug/custom sections. + for (MCSection &Sec : Asm) { + auto &DebugSection = static_cast(Sec); + StringRef SectionName = DebugSection.getSectionName(); + + if (!SectionName.startswith(".debug_")) + continue; + + CustomSections.push_back(WasmCustomSection(SectionName, &DebugSection, + WasmCustomSection::Debug)); + + auto SymbolName = (".Lsection_start" + SectionName).str(); + MCSymbolWasm *SectionSym = + cast(Ctx.getOrCreateSymbol(SymbolName)); + CustomSectionSymbols[SectionSym] = &DebugSection; + + wasm::WasmSymbolInfo Info; + Info.Name = SectionSym->getName(); + Info.Kind = wasm::WASM_SYMBOL_TYPE_SECTION; + Info.Flags = 0; + Info.SectionCode = wasm::WASM_SEC_CUSTOM; + Info.SectionName = SectionName; + SymbolIndices[SectionSym] = SymbolInfos.size(); + SymbolInfos.emplace_back(Info); + } + // Populate WasmIndices and DataLocations for defined symbols. for (const MCSymbol &S : Asm.symbols()) { // Ignore unnamed temporary symbols, which aren't ever exported, imported, @@ -1351,13 +1511,14 @@ writeElemSection(TableElems); writeCodeSection(Asm, Layout, Functions); writeDataSection(); - writeUserCustomSections(CustomSections); + writeCustomSections(Asm, Layout, CustomSections); + updateCustomSectionRelocations(Functions, Layout); writeLinkingMetaDataSection(SymbolInfos, InitFuncs, Comdats); writeCodeRelocSection(); writeDataRelocSection(); + writeCustomRelocSections(); // TODO: Translate the .comment section to the output. - // TODO: Translate debug sections to the output. } std::unique_ptr Index: lib/Object/WasmObjectFile.cpp =================================================================== --- lib/Object/WasmObjectFile.cpp +++ lib/Object/WasmObjectFile.cpp @@ -460,6 +460,23 @@ } break; + case wasm::WASM_SYMBOL_TYPE_SECTION: { + Info.SectionCode = readVaruint32(Ptr); + if (Info.SectionCode != wasm::WASM_SEC_CUSTOM) + return make_error("invalid section symbol target", + object_error::parse_failed); + Info.SectionName = readString(Ptr); + if (!findCustomSectionByName(Info.SectionName)) + return make_error( + "referred symbol section " + Info.SectionName + " is missing", + object_error::parse_failed); + + // Generate somewhat unique name for the section symbol. + auto SymbolName = (".Lsection_start" + Info.SectionName).str(); + Info.Name = InternedNames.insert(SymbolName).first->getKey(); + break; + } + default: return make_error("Invalid symbol type", object_error::parse_failed); @@ -591,6 +608,18 @@ object_error::parse_failed); Reloc.Addend = readVarint32(Ptr); break; + case wasm::R_WEBASSEMBLY_FUNCTION_OFFSET_I32: + if (!isValidFunctionSymbol(Reloc.Index)) + return make_error("Bad relocation function index", + object_error::parse_failed); + Reloc.Addend = readVarint32(Ptr); + break; + case wasm::R_WEBASSEMBLY_SECTION_OFFSET_I32: + if (!isValidSectionSymbol(Reloc.Index)) + return make_error("Bad relocation section index", + object_error::parse_failed); + Reloc.Addend = readVarint32(Ptr); + break; default: return make_error("Bad relocation type: " + Twine(Reloc.Type), @@ -602,7 +631,9 @@ // to check that. uint64_t Size = 5; if (Reloc.Type == wasm::R_WEBASSEMBLY_TABLE_INDEX_I32 || - Reloc.Type == wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32) + Reloc.Type == wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32 || + Reloc.Type == wasm::R_WEBASSEMBLY_SECTION_OFFSET_I32 || + Reloc.Type == wasm::R_WEBASSEMBLY_FUNCTION_OFFSET_I32) Size = 4; if (Reloc.Offset + Size > EndOffset) return make_error("Bad relocation offset", @@ -830,6 +861,10 @@ return Index < Symbols.size() && Symbols[Index].isTypeData(); } +bool WasmObjectFile::isValidSectionSymbol(uint32_t Index) const { + return Index < Symbols.size() && Symbols[Index].isTypeSection(); +} + wasm::WasmFunction &WasmObjectFile::getDefinedFunction(uint32_t Index) { assert(isDefinedFunctionIndex(Index)); return Functions[Index - NumImportedFunctions]; @@ -1010,6 +1045,8 @@ assert(Segment.Offset.Opcode == wasm::WASM_OPCODE_I32_CONST); return Segment.Offset.Value.Int32 + Sym.Info.DataRef.Offset; } + case wasm::WASM_SYMBOL_TYPE_SECTION: + return 0; // TODO(yury): section offset from top of the object file? } llvm_unreachable("invalid symbol type"); } @@ -1039,6 +1076,12 @@ return SymbolRef::ST_Other; case wasm::WASM_SYMBOL_TYPE_DATA: return SymbolRef::ST_Data; + case wasm::WASM_SYMBOL_TYPE_SECTION: + if (Sym.Info.SectionCode == wasm::WASM_SEC_CUSTOM && + Sym.Info.SectionName.startswith(".debug_")) + return SymbolRef::ST_Debug; + // Support of non-debug custom sections is not implemented yet. + break; } llvm_unreachable("Unknown WasmSymbol::SymbolType"); @@ -1062,6 +1105,18 @@ case wasm::WASM_SYMBOL_TYPE_DATA: Ref.d.a = DataSection; break; + case wasm::WASM_SYMBOL_TYPE_SECTION: { + assert(Sym.Info.SectionCode == wasm::WASM_SEC_CUSTOM); + // Find index of the referred custom section. + auto Section = std::find_if(Sections.begin(), Sections.end(), + [&](const WasmSection &Sec) -> bool { + return Sec.Type == wasm::WASM_SEC_CUSTOM && + Sec.Name == Sym.Info.SectionName; + }); + assert(Section != Sections.end()); + Ref.d.a = std::distance(Sections.begin(), Section); + break; + } default: llvm_unreachable("Unknown WasmSymbol::SymbolType"); } @@ -1105,9 +1160,18 @@ return Sec.d.a; } +static size_t getPayloadOffset(const WasmSection &Section) { + if (Section.Type != llvm::wasm::WASM_SEC_CUSTOM) + return 0; + const uint8_t *Ptr = Section.Content.data(); + readString(Ptr); + return Ptr - Section.Content.data(); +} + uint64_t WasmObjectFile::getSectionSize(DataRefImpl Sec) const { const WasmSection &S = Sections[Sec.d.a]; - return S.Content.size(); + assert(S.Content.size() >= getPayloadOffset(S)); + return S.Content.size() - getPayloadOffset(S); } std::error_code WasmObjectFile::getSectionContents(DataRefImpl Sec, @@ -1115,8 +1179,11 @@ const WasmSection &S = Sections[Sec.d.a]; // This will never fail since wasm sections can never be empty (user-sections // must have a name and non-user sections each have a defined structure). - Res = StringRef(reinterpret_cast(S.Content.data()), - S.Content.size()); + size_t PayloadOffset = getPayloadOffset(S); + assert(S.Content.size() >= PayloadOffset); + Res = StringRef( + reinterpret_cast(S.Content.data() + PayloadOffset), + S.Content.size() - PayloadOffset); return std::error_code(); } @@ -1166,10 +1233,12 @@ return Rel.Offset; } -symbol_iterator WasmObjectFile::getRelocationSymbol(DataRefImpl Rel) const { - llvm_unreachable("not yet implemented"); - SymbolRef Ref; - return symbol_iterator(Ref); +symbol_iterator WasmObjectFile::getRelocationSymbol(DataRefImpl Ref) const { + const wasm::WasmRelocation &Rel = getWasmRelocation(Ref); + DataRefImpl Sym; + Sym.d.a = Rel.Index; + Sym.d.b = 0; + return symbol_iterator(SymbolRef(Sym, this)); } uint64_t WasmObjectFile::getRelocationType(DataRefImpl Ref) const { Index: lib/ObjectYAML/WasmYAML.cpp =================================================================== --- lib/ObjectYAML/WasmYAML.cpp +++ lib/ObjectYAML/WasmYAML.cpp @@ -404,6 +404,10 @@ IO.mapOptional("Offset", Info.DataRef.Offset, 0u); IO.mapRequired("Size", Info.DataRef.Size); } + } else if (Info.Kind == wasm::WASM_SYMBOL_TYPE_SECTION) { + IO.mapRequired("SectionType", Info.SectionType); + if (Info.SectionType == wasm::WASM_SEC_CUSTOM) + IO.mapRequired("SectionName", Info.SectionName); } else { llvm_unreachable("unsupported symbol kind"); } @@ -438,6 +442,7 @@ ECase(FUNCTION); ECase(DATA); ECase(GLOBAL); + ECase(SECTION); #undef ECase } Index: lib/Target/WebAssembly/MCTargetDesc/WebAssemblyWasmObjectWriter.cpp =================================================================== --- lib/Target/WebAssembly/MCTargetDesc/WebAssemblyWasmObjectWriter.cpp +++ lib/Target/WebAssembly/MCTargetDesc/WebAssemblyWasmObjectWriter.cpp @@ -20,9 +20,10 @@ #include "llvm/MC/MCFixup.h" #include "llvm/MC/MCFixupKindInfo.h" #include "llvm/MC/MCObjectWriter.h" +#include "llvm/MC/MCSectionWasm.h" #include "llvm/MC/MCSymbolWasm.h" -#include "llvm/MC/MCWasmObjectWriter.h" #include "llvm/MC/MCValue.h" +#include "llvm/MC/MCWasmObjectWriter.h" #include "llvm/Support/Casting.h" #include "llvm/Support/ErrorHandling.h" @@ -61,6 +62,25 @@ return RefA && RefA->getKind() == MCSymbolRefExpr::VK_WebAssembly_TYPEINDEX; } +static const MCSection *GetFixupSection(const MCExpr *Expr) { + if (auto SyExp = dyn_cast(Expr)) { + if (SyExp->getSymbol().isInSection()) + return &SyExp->getSymbol().getSection(); + return nullptr; + } + + if (auto BinOp = dyn_cast(Expr)) { + auto SectionLHS = GetFixupSection(BinOp->getLHS()); + auto SectionRHS = GetFixupSection(BinOp->getRHS()); + return SectionLHS == SectionRHS ? nullptr : SectionLHS; + } + + if (auto UnOp = dyn_cast(Expr)) + return GetFixupSection(UnOp->getSubExpr()); + + return nullptr; +} + unsigned WebAssemblyWasmObjectWriter::getRelocType(const MCValue &Target, const MCFixup &Fixup) const { @@ -86,6 +106,13 @@ case FK_Data_4: if (IsFunction) return wasm::R_WEBASSEMBLY_TABLE_INDEX_I32; + if (auto Section = static_cast( + GetFixupSection(Fixup.getValue()))) { + if (Section->getKind().isText()) + return wasm::R_WEBASSEMBLY_FUNCTION_OFFSET_I32; + else if (!Section->isWasmData()) + return wasm::R_WEBASSEMBLY_SECTION_OFFSET_I32; + } return wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32; case FK_Data_8: llvm_unreachable("FK_Data_8 not implemented yet"); Index: tools/llvm-readobj/WasmDumper.cpp =================================================================== --- tools/llvm-readobj/WasmDumper.cpp +++ tools/llvm-readobj/WasmDumper.cpp @@ -84,6 +84,8 @@ case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB: case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB: case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32: + case wasm::R_WEBASSEMBLY_FUNCTION_OFFSET_I32: + case wasm::R_WEBASSEMBLY_SECTION_OFFSET_I32: HasAddend = true; break; default: Index: tools/obj2yaml/wasm2yaml.cpp =================================================================== --- tools/obj2yaml/wasm2yaml.cpp +++ tools/obj2yaml/wasm2yaml.cpp @@ -102,6 +102,10 @@ case wasm::WASM_SYMBOL_TYPE_GLOBAL: Info.ElementIndex = Symbol.ElementIndex; break; + case wasm::WASM_SYMBOL_TYPE_SECTION: + Info.SectionType = Symbol.SectionCode; + Info.SectionName = Symbol.SectionName; + break; } LinkingSec->SymbolTable.emplace_back(Info); } Index: tools/yaml2obj/yaml2wasm.cpp =================================================================== --- tools/yaml2obj/yaml2wasm.cpp +++ tools/yaml2obj/yaml2wasm.cpp @@ -163,6 +163,11 @@ encodeULEB128(Info.DataRef.Size, SubSection.GetStream()); } break; + case wasm::WASM_SYMBOL_TYPE_SECTION: + encodeULEB128(Info.SectionType, SubSection.GetStream()); + if (Info.SectionType == wasm::WASM_SEC_CUSTOM) + writeStringRef(Info.SectionName, SubSection.GetStream()); + break; default: llvm_unreachable("unexpected kind"); } @@ -421,21 +426,31 @@ int WasmWriter::writeRelocSection(raw_ostream &OS, WasmYAML::Section &Sec) { - StringRef Name; switch (Sec.Type) { case wasm::WASM_SEC_CODE: - Name = "reloc.CODE"; + writeStringRef("reloc.CODE", OS); break; case wasm::WASM_SEC_DATA: - Name = "reloc.DATA"; + writeStringRef("reloc.DATA", OS); + break; + case wasm::WASM_SEC_CUSTOM: { + auto CustomSection = dyn_cast(&Sec); + if (!CustomSection->Name.startswith(".debug_")) { + llvm_unreachable("not yet implemented (only for debug sections)"); + return 1; + } + + writeStringRef(("reloc." + CustomSection->Name).str(), OS); break; + } default: llvm_unreachable("not yet implemented"); return 1; } - writeStringRef(Name, OS); writeUint8(OS, Sec.Type); + if (Sec.Type == wasm::WASM_SEC_CUSTOM) + writeStringRef(dyn_cast(&Sec)->Name, OS); encodeULEB128(Sec.Relocations.size(), OS); for (auto Reloc: Sec.Relocations) { @@ -446,6 +461,8 @@ case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB: case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB: case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32: + case wasm::R_WEBASSEMBLY_FUNCTION_OFFSET_I32: + case wasm::R_WEBASSEMBLY_SECTION_OFFSET_I32: encodeULEB128(Reloc.Addend, OS); } }