diff options
Diffstat (limited to 'contrib/llvm-project/llvm/lib/MC/WasmObjectWriter.cpp')
-rw-r--r-- | contrib/llvm-project/llvm/lib/MC/WasmObjectWriter.cpp | 79 |
1 files changed, 57 insertions, 22 deletions
diff --git a/contrib/llvm-project/llvm/lib/MC/WasmObjectWriter.cpp b/contrib/llvm-project/llvm/lib/MC/WasmObjectWriter.cpp index 0dc5c9111db2..636c1d238932 100644 --- a/contrib/llvm-project/llvm/lib/MC/WasmObjectWriter.cpp +++ b/contrib/llvm-project/llvm/lib/MC/WasmObjectWriter.cpp @@ -292,6 +292,8 @@ private: W->OS << Str; } + void writeStringWithAlignment(const StringRef Str, unsigned Alignment); + void writeI32(int32_t val) { char Buffer[4]; support::endian::write32le(Buffer, val); @@ -317,7 +319,7 @@ private: uint32_t writeCodeSection(const MCAssembler &Asm, const MCAsmLayout &Layout, ArrayRef<WasmFunction> Functions); uint32_t writeDataSection(const MCAsmLayout &Layout); - void writeTagSection(ArrayRef<wasm::WasmTagType> Tags); + void writeTagSection(ArrayRef<uint32_t> TagTypes); void writeGlobalSection(ArrayRef<wasm::WasmGlobal> Globals); void writeTableSection(ArrayRef<wasm::WasmTable> Tables); void writeRelocSection(uint32_t SectionIndex, StringRef Name, @@ -362,6 +364,28 @@ void WasmObjectWriter::startSection(SectionBookkeeping &Section, Section.Index = SectionCount++; } +// Write a string with extra paddings for trailing alignment +// TODO: support alignment at asm and llvm level? +void WasmObjectWriter::writeStringWithAlignment(const StringRef Str, + unsigned Alignment) { + + // Calculate the encoded size of str length and add pads based on it and + // alignment. + raw_null_ostream NullOS; + uint64_t StrSizeLength = encodeULEB128(Str.size(), NullOS); + uint64_t Offset = W->OS.tell() + StrSizeLength + Str.size(); + uint64_t Paddings = offsetToAlignment(Offset, Align(Alignment)); + Offset += Paddings; + + // LEB128 greater than 5 bytes is invalid + assert((StrSizeLength + Paddings) <= 5 && "too long string to align"); + + encodeSLEB128(Str.size(), W->OS, StrSizeLength + Paddings); + W->OS << Str; + + assert(W->OS.tell() == Offset && "invalid padding"); +} + void WasmObjectWriter::startCustomSection(SectionBookkeeping &Section, StringRef Name) { LLVM_DEBUG(dbgs() << "startCustomSection " << Name << "\n"); @@ -371,7 +395,12 @@ void WasmObjectWriter::startCustomSection(SectionBookkeeping &Section, Section.PayloadOffset = W->OS.tell(); // Custom sections in wasm also have a string identifier. - writeString(Name); + if (Name != "__clangast") { + writeString(Name); + } else { + // The on-disk hashtable in clangast needs to be aligned by 4 bytes. + writeStringWithAlignment(Name, 4); + } // The position where the custom section starts. Section.ContentsOffset = W->OS.tell(); @@ -565,8 +594,14 @@ void WasmObjectWriter::recordRelocation(MCAssembler &Asm, SymA->setUsedInReloc(); } - if (RefA->getKind() == MCSymbolRefExpr::VK_GOT) + switch (RefA->getKind()) { + case MCSymbolRefExpr::VK_GOT: + case MCSymbolRefExpr::VK_WASM_GOT_TLS: SymA->setUsedInGOT(); + break; + default: + break; + } WasmRelocationEntry Rec(FixupOffset, SymA, C, Type, &FixupSection); LLVM_DEBUG(dbgs() << "WasmReloc: " << Rec << "\n"); @@ -825,8 +860,8 @@ void WasmObjectWriter::writeImportSection(ArrayRef<wasm::WasmImport> Imports, encodeULEB128(NumElements, W->OS); // initial break; case wasm::WASM_EXTERNAL_TAG: - W->OS << char(Import.Tag.Attribute); - encodeULEB128(Import.Tag.SigIndex, W->OS); + W->OS << char(0); // Reserved 'attribute' field + encodeULEB128(Import.SigIndex, W->OS); break; default: llvm_unreachable("unsupported import kind"); @@ -850,17 +885,17 @@ void WasmObjectWriter::writeFunctionSection(ArrayRef<WasmFunction> Functions) { endSection(Section); } -void WasmObjectWriter::writeTagSection(ArrayRef<wasm::WasmTagType> Tags) { - if (Tags.empty()) +void WasmObjectWriter::writeTagSection(ArrayRef<uint32_t> TagTypes) { + if (TagTypes.empty()) return; SectionBookkeeping Section; startSection(Section, wasm::WASM_SEC_TAG); - encodeULEB128(Tags.size(), W->OS); - for (const wasm::WasmTagType &Tag : Tags) { - W->OS << char(Tag.Attribute); - encodeULEB128(Tag.SigIndex, W->OS); + encodeULEB128(TagTypes.size(), W->OS); + for (uint32_t Index : TagTypes) { + W->OS << char(0); // Reserved 'attribute' field + encodeULEB128(Index, W->OS); } endSection(Section); @@ -1052,7 +1087,7 @@ uint32_t WasmObjectWriter::writeDataSection(const MCAsmLayout &Layout) { void WasmObjectWriter::writeRelocSection( uint32_t SectionIndex, StringRef Name, std::vector<WasmRelocationEntry> &Relocs) { - // See: https://github.com/WebAssembly/tool-conventions/blob/master/Linking.md + // See: https://github.com/WebAssembly/tool-conventions/blob/main/Linking.md // for descriptions of the reloc sections. if (Relocs.empty()) @@ -1340,8 +1375,7 @@ void WasmObjectWriter::prepareImports( Import.Module = WS.getImportModule(); Import.Field = WS.getImportName(); Import.Kind = wasm::WASM_EXTERNAL_TAG; - Import.Tag.Attribute = wasm::WASM_TAG_ATTRIBUTE_EXCEPTION; - Import.Tag.SigIndex = getTagType(WS); + Import.SigIndex = getTagType(WS); Imports.push_back(Import); assert(WasmIndices.count(&WS) == 0); WasmIndices[&WS] = NumTagImports++; @@ -1409,7 +1443,7 @@ uint64_t WasmObjectWriter::writeOneObject(MCAssembler &Asm, SmallVector<uint32_t, 4> TableElems; SmallVector<wasm::WasmImport, 4> Imports; SmallVector<wasm::WasmExport, 4> Exports; - SmallVector<wasm::WasmTagType, 1> Tags; + SmallVector<uint32_t, 2> TagTypes; SmallVector<wasm::WasmGlobal, 1> Globals; SmallVector<wasm::WasmTable, 1> Tables; SmallVector<wasm::WasmSymbolInfo, 4> SymbolInfos; @@ -1644,16 +1678,15 @@ uint64_t WasmObjectWriter::writeOneObject(MCAssembler &Asm, LLVM_DEBUG(dbgs() << " -> table index: " << WasmIndices.find(&WS)->second << "\n"); } else if (WS.isTag()) { - // C++ exception symbol (__cpp_exception) + // C++ exception symbol (__cpp_exception) or longjmp symbol + // (__c_longjmp) unsigned Index; if (WS.isDefined()) { - Index = NumTagImports + Tags.size(); - wasm::WasmTagType Tag; - Tag.SigIndex = getTagType(WS); - Tag.Attribute = wasm::WASM_TAG_ATTRIBUTE_EXCEPTION; + Index = NumTagImports + TagTypes.size(); + uint32_t SigIndex = getTagType(WS); assert(WasmIndices.count(&WS) == 0); WasmIndices[&WS] = Index; - Tags.push_back(Tag); + TagTypes.push_back(SigIndex); } else { // An import; the index was assigned above. assert(WasmIndices.count(&WS) > 0); @@ -1747,6 +1780,8 @@ uint64_t WasmObjectWriter::writeOneObject(MCAssembler &Asm, Flags |= wasm::WASM_SYMBOL_EXPLICIT_NAME; if (WS.hasExportName()) Flags |= wasm::WASM_SYMBOL_EXPORTED; + if (WS.isTLS()) + Flags |= wasm::WASM_SYMBOL_TLS; wasm::WasmSymbolInfo Info; Info.Name = WS.getName(); @@ -1869,7 +1904,7 @@ uint64_t WasmObjectWriter::writeOneObject(MCAssembler &Asm, writeFunctionSection(Functions); writeTableSection(Tables); // Skip the "memory" section; we import the memory instead. - writeTagSection(Tags); + writeTagSection(TagTypes); writeGlobalSection(Globals); writeExportSection(Exports); const MCSymbol *IndirectFunctionTable = |