diff options
author | Thomas Lively <tlively@google.com> | 2019-01-16 23:46:14 +0000 |
---|---|---|
committer | Thomas Lively <tlively@google.com> | 2019-01-16 23:46:14 +0000 |
commit | a56c23c5ba561126f64488fbda0566fc92c26e58 (patch) | |
tree | 33176c5c495f6c9e88bdd6c4dd68bae8dac3d080 /llvm/lib | |
parent | c7700127ae79dec3b6fe0c1773824da56352e194 (diff) | |
download | bcm5719-llvm-a56c23c5ba561126f64488fbda0566fc92c26e58.tar.gz bcm5719-llvm-a56c23c5ba561126f64488fbda0566fc92c26e58.zip |
[WebAssembly] Parse llvm.ident into producers section
Summary:
Everything before the word "version" is the tool, and everything after
the word "version" is the version.
Reviewers: aheejin, dschuff
Subscribers: sbc100, jgravelle-google, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D56742
llvm-svn: 351399
Diffstat (limited to 'llvm/lib')
-rw-r--r-- | llvm/lib/MC/WasmObjectWriter.cpp | 47 | ||||
-rw-r--r-- | llvm/lib/Object/WasmObjectFile.cpp | 45 | ||||
-rw-r--r-- | llvm/lib/ObjectYAML/WasmYAML.cpp | 18 | ||||
-rw-r--r-- | llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp | 30 |
4 files changed, 123 insertions, 17 deletions
diff --git a/llvm/lib/MC/WasmObjectWriter.cpp b/llvm/lib/MC/WasmObjectWriter.cpp index 0cca3757be9..8e45b2f3e0c 100644 --- a/llvm/lib/MC/WasmObjectWriter.cpp +++ b/llvm/lib/MC/WasmObjectWriter.cpp @@ -224,6 +224,7 @@ class WasmObjectWriter : public MCObjectWriter { // Stores output data (index, relocations, content offset) for custom // section. std::vector<WasmCustomSection> CustomSections; + std::unique_ptr<WasmCustomSection> ProducersSection; // Relocations for fixing up references in the custom sections. DenseMap<const MCSectionWasm *, std::vector<WasmRelocationEntry>> CustomSectionsRelocations; @@ -265,6 +266,8 @@ private: WasmIndices.clear(); TableIndices.clear(); DataLocations.clear(); + CustomSections.clear(); + ProducersSection.reset(); CustomSectionsRelocations.clear(); SignatureIndices.clear(); Signatures.clear(); @@ -311,7 +314,8 @@ private: ArrayRef<wasm::WasmSymbolInfo> SymbolInfos, ArrayRef<std::pair<uint16_t, uint32_t>> InitFuncs, const std::map<StringRef, std::vector<WasmComdatEntry>> &Comdats); - void writeCustomSections(const MCAssembler &Asm, const MCAsmLayout &Layout); + void writeCustomSection(WasmCustomSection &CustomSection, + const MCAssembler &Asm, const MCAsmLayout &Layout); void writeCustomRelocSections(); void updateCustomSectionRelocations(const SmallVector<WasmFunction, 4> &Functions, @@ -1045,25 +1049,24 @@ void WasmObjectWriter::writeLinkingMetaDataSection( endSection(Section); } -void WasmObjectWriter::writeCustomSections(const MCAssembler &Asm, - const MCAsmLayout &Layout) { - for (auto &CustomSection : CustomSections) { - SectionBookkeeping Section; - auto *Sec = CustomSection.Section; - startCustomSection(Section, CustomSection.Name); +void WasmObjectWriter::writeCustomSection(WasmCustomSection &CustomSection, + const MCAssembler &Asm, + const MCAsmLayout &Layout) { + SectionBookkeeping Section; + auto *Sec = CustomSection.Section; + startCustomSection(Section, CustomSection.Name); - Sec->setSectionOffset(W.OS.tell() - Section.ContentsOffset); - Asm.writeSectionData(W.OS, Sec, Layout); + Sec->setSectionOffset(W.OS.tell() - Section.ContentsOffset); + Asm.writeSectionData(W.OS, Sec, Layout); - CustomSection.OutputContentsOffset = Section.ContentsOffset; - CustomSection.OutputIndex = Section.Index; + CustomSection.OutputContentsOffset = Section.ContentsOffset; + CustomSection.OutputIndex = Section.Index; - endSection(Section); + endSection(Section); - // Apply fixups. - auto &Relocations = CustomSectionsRelocations[CustomSection.Section]; - applyRelocations(Relocations, CustomSection.OutputContentsOffset); - } + // Apply fixups. + auto &Relocations = CustomSectionsRelocations[CustomSection.Section]; + applyRelocations(Relocations, CustomSection.OutputContentsOffset); } uint32_t WasmObjectWriter::getFunctionType(const MCSymbolWasm &Symbol) { @@ -1282,6 +1285,13 @@ uint64_t WasmObjectWriter::writeObject(MCAssembler &Asm, report_fatal_error("section name and begin symbol should match: " + Twine(SectionName)); } + + // Separate out the producers section + if (Name == "producers") { + ProducersSection = llvm::make_unique<WasmCustomSection>(Name, &Section); + continue; + } + CustomSections.emplace_back(Name, &Section); } } @@ -1570,11 +1580,14 @@ uint64_t WasmObjectWriter::writeObject(MCAssembler &Asm, writeElemSection(TableElems); writeCodeSection(Asm, Layout, Functions); writeDataSection(); - writeCustomSections(Asm, Layout); + for (auto &CustomSection : CustomSections) + writeCustomSection(CustomSection, Asm, Layout); writeLinkingMetaDataSection(SymbolInfos, InitFuncs, Comdats); writeRelocSection(CodeSectionIndex, "CODE", CodeRelocations); writeRelocSection(DataSectionIndex, "DATA", DataRelocations); writeCustomRelocSections(); + if (ProducersSection) + writeCustomSection(*ProducersSection, Asm, Layout); // TODO: Translate the .comment section to the output. return W.OS.tell() - StartOffset; diff --git a/llvm/lib/Object/WasmObjectFile.cpp b/llvm/lib/Object/WasmObjectFile.cpp index d84cb48c9fb..fd6ad9bdefe 100644 --- a/llvm/lib/Object/WasmObjectFile.cpp +++ b/llvm/lib/Object/WasmObjectFile.cpp @@ -10,6 +10,7 @@ #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/DenseSet.h" #include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/SmallSet.h" #include "llvm/ADT/StringRef.h" #include "llvm/ADT/StringSet.h" #include "llvm/ADT/Triple.h" @@ -659,6 +660,47 @@ Error WasmObjectFile::parseLinkingSectionComdat(ReadContext &Ctx) { return Error::success(); } +Error WasmObjectFile::parseProducersSection(ReadContext &Ctx) { + llvm::SmallSet<StringRef, 3> FieldsSeen; + uint32_t Fields = readVaruint32(Ctx); + for (size_t i = 0; i < Fields; ++i) { + StringRef FieldName = readString(Ctx); + if (!FieldsSeen.insert(FieldName).second) + return make_error<GenericBinaryError>( + "Producers section does not have unique fields", + object_error::parse_failed); + std::vector<std::pair<std::string, std::string>> *ProducerVec = nullptr; + if (FieldName == "language") { + ProducerVec = &ProducerInfo.Languages; + } else if (FieldName == "processed-by") { + ProducerVec = &ProducerInfo.Tools; + } else if (FieldName == "sdk") { + ProducerVec = &ProducerInfo.SDKs; + } else { + return make_error<GenericBinaryError>( + "Producers section field is not named one of language, processed-by, " + "or sdk", + object_error::parse_failed); + } + uint32_t ValueCount = readVaruint32(Ctx); + llvm::SmallSet<StringRef, 8> ProducersSeen; + for (size_t j = 0; j < ValueCount; ++j) { + StringRef Name = readString(Ctx); + StringRef Version = readString(Ctx); + if (!ProducersSeen.insert(Name).second) { + return make_error<GenericBinaryError>( + "Producers section contains repeated producer", + object_error::parse_failed); + } + ProducerVec->emplace_back(Name, Version); + } + } + if (Ctx.Ptr != Ctx.End) + return make_error<GenericBinaryError>("Producers section ended prematurely", + object_error::parse_failed); + return Error::success(); +} + Error WasmObjectFile::parseRelocSection(StringRef Name, ReadContext &Ctx) { uint32_t SectionIndex = readVaruint32(Ctx); if (SectionIndex >= Sections.size()) @@ -757,6 +799,9 @@ Error WasmObjectFile::parseCustomSection(WasmSection &Sec, ReadContext &Ctx) { } else if (Sec.Name == "linking") { if (Error Err = parseLinkingSection(Ctx)) return Err; + } else if (Sec.Name == "producers") { + if (Error Err = parseProducersSection(Ctx)) + return Err; } else if (Sec.Name.startswith("reloc.")) { if (Error Err = parseRelocSection(Sec.Name, Ctx)) return Err; diff --git a/llvm/lib/ObjectYAML/WasmYAML.cpp b/llvm/lib/ObjectYAML/WasmYAML.cpp index 47bf853e0d3..b45c8ce65eb 100644 --- a/llvm/lib/ObjectYAML/WasmYAML.cpp +++ b/llvm/lib/ObjectYAML/WasmYAML.cpp @@ -74,6 +74,14 @@ static void sectionMapping(IO &IO, WasmYAML::LinkingSection &Section) { IO.mapOptional("Comdats", Section.Comdats); } +static void sectionMapping(IO &IO, WasmYAML::ProducersSection &Section) { + commonSectionMapping(IO, Section); + IO.mapRequired("Name", Section.Name); + IO.mapOptional("Languages", Section.Languages); + IO.mapOptional("Tools", Section.Tools); + IO.mapOptional("SDKs", Section.SDKs); +} + static void sectionMapping(IO &IO, WasmYAML::CustomSection &Section) { commonSectionMapping(IO, Section); IO.mapRequired("Name", Section.Name); @@ -169,6 +177,10 @@ void MappingTraits<std::unique_ptr<WasmYAML::Section>>::mapping( if (!IO.outputting()) Section.reset(new WasmYAML::NameSection()); sectionMapping(IO, *cast<WasmYAML::NameSection>(Section.get())); + } else if (SectionName == "producers") { + if (!IO.outputting()) + Section.reset(new WasmYAML::ProducersSection()); + sectionMapping(IO, *cast<WasmYAML::ProducersSection>(Section.get())); } else { if (!IO.outputting()) Section.reset(new WasmYAML::CustomSection(SectionName)); @@ -293,6 +305,12 @@ void MappingTraits<WasmYAML::NameEntry>::mapping( IO.mapRequired("Name", NameEntry.Name); } +void MappingTraits<WasmYAML::ProducerEntry>::mapping( + IO &IO, WasmYAML::ProducerEntry &ProducerEntry) { + IO.mapRequired("Name", ProducerEntry.Name); + IO.mapRequired("Version", ProducerEntry.Version); +} + void MappingTraits<WasmYAML::SegmentInfo>::mapping( IO &IO, WasmYAML::SegmentInfo &SegmentInfo) { IO.mapRequired("Index", SegmentInfo.Index); diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp index c4f03dfa7f9..6d5647a7ace 100644 --- a/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp +++ b/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp @@ -22,6 +22,7 @@ #include "WebAssemblyMCInstLower.h" #include "WebAssemblyMachineFunctionInfo.h" #include "WebAssemblyRegisterInfo.h" +#include "llvm/ADT/SmallSet.h" #include "llvm/ADT/StringExtras.h" #include "llvm/CodeGen/Analysis.h" #include "llvm/CodeGen/AsmPrinter.h" @@ -146,6 +147,35 @@ void WebAssemblyAsmPrinter::EmitEndOfAsmFile(Module &M) { OutStreamer->PopSection(); } } + + if (const NamedMDNode *Ident = M.getNamedMetadata("llvm.ident")) { + llvm::SmallSet<StringRef, 4> SeenTools; + llvm::SmallVector<std::pair<StringRef, StringRef>, 4> Tools; + for (size_t i = 0, e = Ident->getNumOperands(); i < e; ++i) { + const auto *S = cast<MDString>(Ident->getOperand(i)->getOperand(0)); + std::pair<StringRef, StringRef> Field = S->getString().split("version"); + StringRef Name = Field.first.trim(); + StringRef Version = Field.second.trim(); + if (!SeenTools.insert(Name).second) + continue; + Tools.emplace_back(Name, Version); + } + MCSectionWasm *Producers = OutContext.getWasmSection( + ".custom_section.producers", SectionKind::getMetadata()); + OutStreamer->PushSection(); + OutStreamer->SwitchSection(Producers); + OutStreamer->EmitULEB128IntValue(1); + OutStreamer->EmitULEB128IntValue(strlen("processed-by")); + OutStreamer->EmitBytes("processed-by"); + OutStreamer->EmitULEB128IntValue(Tools.size()); + for (auto &Tool : Tools) { + OutStreamer->EmitULEB128IntValue(Tool.first.size()); + OutStreamer->EmitBytes(Tool.first); + OutStreamer->EmitULEB128IntValue(Tool.second.size()); + OutStreamer->EmitBytes(Tool.second); + } + OutStreamer->PopSection(); + } } void WebAssemblyAsmPrinter::EmitConstantPool() { |