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 { @@ -247,6 +251,7 @@ WASM_SYMBOL_TYPE_FUNCTION = 0x0, 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,30 @@ 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 @@ -20,6 +20,7 @@ #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/StringRef.h" #include "llvm/ADT/StringMap.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/DebugInfo/DWARF/DWARFContext.cpp =================================================================== --- lib/DebugInfo/DWARF/DWARFContext.cpp +++ lib/DebugInfo/DWARF/DWARFContext.cpp @@ -1142,8 +1142,10 @@ // containing the symbol being targeted. In either case, // we need to perform the same computation. if (L && RSec != Obj.section_end()) - if (uint64_t SectionLoadAddress = L->getSectionLoadAddress(*RSec)) + if (uint64_t SectionLoadAddress = L->getSectionLoadAddress(*RSec)) { + dbgs() << Ret.Address << "---" << SectionLoadAddress << " " << RSec->getAddress() << "\n"; Ret.Address += SectionLoadAddress - RSec->getAddress(); + } if (CacheIt != Cache.end()) CacheIt->second = Ret; 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; }; @@ -131,6 +133,15 @@ uint32_t Index; }; +// Information to store symbols that point to Wasm sections content offsets. +struct WasmSectionEntry { + const MCSymbolWasm *Sym; + const MCSection* Section; + + WasmSectionEntry(const MCSymbolWasm *Sym, const MCSection* Section) + : Sym(Sym), Section(Section) {} +}; + // Information about a single relocation. struct WasmRelocationEntry { uint64_t Offset; // Where is the relocation. @@ -150,6 +161,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; @@ -184,6 +197,9 @@ // Relocations for fixing up references in the data section. std::vector DataRelocations; + // Relocations for fixing up references in the custom sections. + StringMap< std::vector > 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; @@ -196,12 +212,15 @@ DenseMap WasmIndices; // Maps data symbols to the Wasm segment and offset/size with the segment. DenseMap DataLocations; + // Maps section symbols to the section. + DenseMap CustomSections; DenseMap FunctionTypeIndices; SmallVector FunctionTypes; SmallVector Globals; SmallVector DataSegments; + DenseMap CustomSectionContents; unsigned NumFunctionImports = 0; unsigned NumGlobalImports = 0; @@ -227,15 +246,18 @@ 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(); + CustomSectionContents.clear(); MCObjectWriter::reset(); NumFunctionImports = 0; NumGlobalImports = 0; @@ -273,11 +295,16 @@ void writeDataSection(); void writeCodeRelocSection(); void writeDataRelocSection(); + void writeCustomRelocSections(); void writeLinkingMetaDataSection( ArrayRef SymbolInfos, ArrayRef> InitFuncs, const std::map> &Comdats); + void writeDebugSections(const MCAssembler &Asm, + const MCAsmLayout &Layout); + void updateCustomSectionRelocations( + const SmallVector &Functions, const MCAsmLayout &Layout); uint32_t getProvisionalValue(const WasmRelocationEntry &RelEntry); void applyRelocations(ArrayRef Relocations, uint64_t ContentsOffset); @@ -308,20 +335,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(StringRef(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"); @@ -362,9 +392,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 && @@ -434,6 +468,27 @@ 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(); + auto Relocs = CustomSectionsRelocations.find(FixupSectionName); + if (Relocs == CustomSectionsRelocations.end()) { + CustomSectionsRelocations[FixupSectionName] = std::vector(); + Relocs = CustomSectionsRelocations.find(FixupSectionName); + } + Relocs->second.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 @@ -511,6 +566,14 @@ 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 = *CustomSections.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: { @@ -602,6 +665,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: @@ -861,6 +926,24 @@ 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, @@ -890,6 +973,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"); } @@ -936,6 +1024,71 @@ endSection(Section); } +void WasmObjectWriter::writeDebugSections(const MCAssembler &Asm, + const MCAsmLayout &Layout) { + for (MCSection &Sec : Asm) { + auto &DebugSection = static_cast(Sec); + StringRef SectionName = DebugSection.getSectionName(); + + if (!SectionName.startswith(".debug_")) + continue; + + SectionBookkeeping Section; + startSection(Section, wasm::WASM_SEC_CUSTOM, SectionName.str().c_str()); + + DebugSection.setSectionOffset(getStream().tell() - Section.ContentsOffset); + Asm.writeSectionData(&DebugSection, Layout); + + CustomSectionContents[&DebugSection] = Section.ContentsOffset; + + endSection(Section); + } +} + +void WasmObjectWriter::updateCustomSectionRelocations( + const SmallVector &Functions, const MCAsmLayout &Layout) { + std::map SectionSymbols; + for (const auto& P: CustomSections) + SectionSymbols[P.second] = P.first; + std::map FuncSymbols; + for (const auto& FuncInfo: Functions) + FuncSymbols[&FuncInfo.Sym->getSection()] = FuncInfo.Sym; + StringMap CustomSectionsByName; + for (auto &P: CustomSections) + 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, CustomSectionContents[Section]); + } +} + uint32_t WasmObjectWriter::getFunctionType(const MCSymbolWasm& Symbol) { assert(Symbol.isFunction()); assert(TypeIndices.count(&Symbol)); @@ -1067,6 +1220,28 @@ } } + // Create symbols for debug/custom sections. + for (const MCSection &Sec : Asm) { + const auto &DebugSection = static_cast(Sec); + StringRef SectionName = DebugSection.getSectionName(); + + if (!SectionName.startswith(".debug_")) + continue; + + auto SymbolName = (".Lsection_start" + SectionName).str(); + MCSymbolWasm *SectionSym = cast(Ctx.getOrCreateSymbol(SymbolName)); + CustomSections[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, @@ -1310,12 +1485,14 @@ writeElemSection(TableElems); writeCodeSection(Asm, Layout, Functions); writeDataSection(); + writeDebugSections(Asm, Layout); + 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,10 @@ 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 +1232,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 @@ -21,6 +21,7 @@ #include "llvm/MC/MCFixupKindInfo.h" #include "llvm/MC/MCObjectWriter.h" #include "llvm/MC/MCSymbolWasm.h" +#include "llvm/MC/MCSectionWasm.h" #include "llvm/MC/MCWasmObjectWriter.h" #include "llvm/MC/MCValue.h" #include "llvm/Support/Casting.h" @@ -61,6 +62,22 @@ return RefA && RefA->getKind() == MCSymbolRefExpr::VK_WebAssembly_TYPEINDEX; } +static const MCSection* GetFixupSection(const MCExpr *Expr) { + if (auto SyExp = dyn_cast(Expr)) + return &SyExp->getSymbol().getSection(); + + 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 +103,12 @@ 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); } }