llvm.org GIT mirror llvm / 017cb3b
Revert "[WebAssembly] Parse llvm.ident into producers section" This reverts commit eccdbba3a02a33e13b5262e92200a33e2ead873d. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@351410 91177308-0d34-0410-b5e6-96231b3b80d8 Thomas Lively 7 months ago
14 changed file(s) with 22 addition(s) and 280 deletion(s). Raw diff Collapse all Expand all
4040 uint32_t TableSize; // Table size in elements
4141 uint32_t TableAlignment; // P2 alignment of table
4242 std::vector Needed; // Shared library depenedencies
43 };
44
45 struct WasmProducerInfo {
46 std::vector> Languages;
47 std::vector> Tools;
48 std::vector> SDKs;
4943 };
5044
5145 struct WasmExport {
129129 static bool classof(const Binary *v) { return v->isWasm(); }
130130
131131 const wasm::WasmDylinkInfo &dylinkInfo() const { return DylinkInfo; }
132 const wasm::WasmProducerInfo &getProducerInfo() const { return ProducerInfo; }
133132 ArrayRef types() const { return Signatures; }
134133 ArrayRef functionTypes() const { return FunctionTypes; }
135134 ArrayRef imports() const { return Imports; }
149148 uint32_t getNumImportedGlobals() const { return NumImportedGlobals; }
150149 uint32_t getNumImportedFunctions() const { return NumImportedFunctions; }
151150 uint32_t getNumImportedEvents() const { return NumImportedEvents; }
151
152152 void moveSymbolNext(DataRefImpl &Symb) const override;
153153
154154 uint32_t getSymbolFlags(DataRefImpl Symb) const override;
251251 Error parseLinkingSection(ReadContext &Ctx);
252252 Error parseLinkingSectionSymtab(ReadContext &Ctx);
253253 Error parseLinkingSectionComdat(ReadContext &Ctx);
254 Error parseProducersSection(ReadContext &Ctx);
255254 Error parseRelocSection(StringRef Name, ReadContext &Ctx);
256255
257256 wasm::WasmObjectHeader Header;
258257 std::vector Sections;
259258 wasm::WasmDylinkInfo DylinkInfo;
260 wasm::WasmProducerInfo ProducerInfo;
261259 std::vector Signatures;
262260 std::vector FunctionTypes;
263261 std::vector Tables;
122122 StringRef Name;
123123 };
124124
125 struct ProducerEntry {
126 std::string Name;
127 std::string Version;
128 };
129
130125 struct SegmentInfo {
131126 uint32_t Index;
132127 StringRef Name;
226221 std::vector SegmentInfos;
227222 std::vector InitFunctions;
228223 std::vector Comdats;
229 };
230
231 struct ProducersSection : CustomSection {
232 ProducersSection() : CustomSection("producers") {}
233
234 static bool classof(const Section *S) {
235 auto C = dyn_cast(S);
236 return C && C->Name == "producers";
237 }
238
239 std::vector Languages;
240 std::vector Tools;
241 std::vector SDKs;
242224 };
243225
244226 struct TypeSection : Section {
383365 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::LocalDecl)
384366 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::Relocation)
385367 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::NameEntry)
386 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::ProducerEntry)
387368 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::SegmentInfo)
388369 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::SymbolInfo)
389370 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::WasmYAML::InitFunction)
462443 static void mapping(IO &IO, WasmYAML::NameEntry &NameEntry);
463444 };
464445
465 template <> struct MappingTraits {
466 static void mapping(IO &IO, WasmYAML::ProducerEntry &ProducerEntry);
467 };
468
469446 template <> struct MappingTraits {
470447 static void mapping(IO &IO, WasmYAML::SegmentInfo &SegmentInfo);
471448 };
223223 // Stores output data (index, relocations, content offset) for custom
224224 // section.
225225 std::vector CustomSections;
226 std::unique_ptr ProducersSection;
227226 // Relocations for fixing up references in the custom sections.
228227 DenseMap>
229228 CustomSectionsRelocations;
265264 WasmIndices.clear();
266265 TableIndices.clear();
267266 DataLocations.clear();
268 CustomSections.clear();
269 ProducersSection.reset();
270267 CustomSectionsRelocations.clear();
271268 SignatureIndices.clear();
272269 Signatures.clear();
313310 ArrayRef SymbolInfos,
314311 ArrayRef> InitFuncs,
315312 const std::map> &Comdats);
316 void writeCustomSection(WasmCustomSection &CustomSection,
317 const MCAssembler &Asm, const MCAsmLayout &Layout);
313 void writeCustomSections(const MCAssembler &Asm, const MCAsmLayout &Layout);
318314 void writeCustomRelocSections();
319315 void
320316 updateCustomSectionRelocations(const SmallVector &Functions,
10481044 endSection(Section);
10491045 }
10501046
1051 void WasmObjectWriter::writeCustomSection(WasmCustomSection &CustomSection,
1052 const MCAssembler &Asm,
1053 const MCAsmLayout &Layout) {
1054 SectionBookkeeping Section;
1055 auto *Sec = CustomSection.Section;
1056 startCustomSection(Section, CustomSection.Name);
1057
1058 Sec->setSectionOffset(W.OS.tell() - Section.ContentsOffset);
1059 Asm.writeSectionData(W.OS, Sec, Layout);
1060
1061 CustomSection.OutputContentsOffset = Section.ContentsOffset;
1062 CustomSection.OutputIndex = Section.Index;
1063
1064 endSection(Section);
1065
1066 // Apply fixups.
1067 auto &Relocations = CustomSectionsRelocations[CustomSection.Section];
1068 applyRelocations(Relocations, CustomSection.OutputContentsOffset);
1047 void WasmObjectWriter::writeCustomSections(const MCAssembler &Asm,
1048 const MCAsmLayout &Layout) {
1049 for (auto &CustomSection : CustomSections) {
1050 SectionBookkeeping Section;
1051 auto *Sec = CustomSection.Section;
1052 startCustomSection(Section, CustomSection.Name);
1053
1054 Sec->setSectionOffset(W.OS.tell() - Section.ContentsOffset);
1055 Asm.writeSectionData(W.OS, Sec, Layout);
1056
1057 CustomSection.OutputContentsOffset = Section.ContentsOffset;
1058 CustomSection.OutputIndex = Section.Index;
1059
1060 endSection(Section);
1061
1062 // Apply fixups.
1063 auto &Relocations = CustomSectionsRelocations[CustomSection.Section];
1064 applyRelocations(Relocations, CustomSection.OutputContentsOffset);
1065 }
10691066 }
10701067
10711068 uint32_t WasmObjectWriter::getFunctionType(const MCSymbolWasm &Symbol) {
12841281 report_fatal_error("section name and begin symbol should match: " +
12851282 Twine(SectionName));
12861283 }
1287
1288 // Separate out the producers section
1289 if (Name == "producers") {
1290 ProducersSection = llvm::make_unique(Name, &Section);
1291 continue;
1292 }
1293
12941284 CustomSections.emplace_back(Name, &Section);
12951285 }
12961286 }
15791569 writeElemSection(TableElems);
15801570 writeCodeSection(Asm, Layout, Functions);
15811571 writeDataSection();
1582 for (auto &CustomSection : CustomSections)
1583 writeCustomSection(CustomSection, Asm, Layout);
1572 writeCustomSections(Asm, Layout);
15841573 writeLinkingMetaDataSection(SymbolInfos, InitFuncs, Comdats);
15851574 writeRelocSection(CodeSectionIndex, "CODE", CodeRelocations);
15861575 writeRelocSection(DataSectionIndex, "DATA", DataRelocations);
15871576 writeCustomRelocSections();
1588 if (ProducersSection)
1589 writeCustomSection(*ProducersSection, Asm, Layout);
15901577
15911578 // TODO: Translate the .comment section to the output.
15921579 return W.OS.tell() - StartOffset;
99 #include "llvm/ADT/ArrayRef.h"
1010 #include "llvm/ADT/DenseSet.h"
1111 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/ADT/SmallSet.h"
1312 #include "llvm/ADT/StringRef.h"
1413 #include "llvm/ADT/StringSet.h"
1514 #include "llvm/ADT/Triple.h"
659658 return Error::success();
660659 }
661660
662 Error WasmObjectFile::parseProducersSection(ReadContext &Ctx) {
663 llvm::SmallSet FieldsSeen;
664 uint32_t Fields = readVaruint32(Ctx);
665 for (size_t i = 0; i < Fields; ++i) {
666 StringRef FieldName = readString(Ctx);
667 if (!FieldsSeen.insert(FieldName).second)
668 return make_error(
669 "Producers section does not have unique fields",
670 object_error::parse_failed);
671 std::vector> *ProducerVec = nullptr;
672 if (FieldName == "language") {
673 ProducerVec = &ProducerInfo.Languages;
674 } else if (FieldName == "processed-by") {
675 ProducerVec = &ProducerInfo.Tools;
676 } else if (FieldName == "sdk") {
677 ProducerVec = &ProducerInfo.SDKs;
678 } else {
679 return make_error(
680 "Producers section field is not named one of language, processed-by, "
681 "or sdk",
682 object_error::parse_failed);
683 }
684 uint32_t ValueCount = readVaruint32(Ctx);
685 llvm::SmallSet ProducersSeen;
686 for (size_t j = 0; j < ValueCount; ++j) {
687 StringRef Name = readString(Ctx);
688 StringRef Version = readString(Ctx);
689 if (!ProducersSeen.insert(Name).second) {
690 return make_error(
691 "Producers section contains repeated producer",
692 object_error::parse_failed);
693 }
694 ProducerVec->emplace_back(Name, Version);
695 }
696 }
697 if (Ctx.Ptr != Ctx.End)
698 return make_error("Producers section ended prematurely",
699 object_error::parse_failed);
700 return Error::success();
701 }
702
703661 Error WasmObjectFile::parseRelocSection(StringRef Name, ReadContext &Ctx) {
704662 uint32_t SectionIndex = readVaruint32(Ctx);
705663 if (SectionIndex >= Sections.size())
797755 return Err;
798756 } else if (Sec.Name == "linking") {
799757 if (Error Err = parseLinkingSection(Ctx))
800 return Err;
801 } else if (Sec.Name == "producers") {
802 if (Error Err = parseProducersSection(Ctx))
803758 return Err;
804759 } else if (Sec.Name.startswith("reloc.")) {
805760 if (Error Err = parseRelocSection(Sec.Name, Ctx))
7171 IO.mapOptional("SegmentInfo", Section.SegmentInfos);
7272 IO.mapOptional("InitFunctions", Section.InitFunctions);
7373 IO.mapOptional("Comdats", Section.Comdats);
74 }
75
76 static void sectionMapping(IO &IO, WasmYAML::ProducersSection &Section) {
77 commonSectionMapping(IO, Section);
78 IO.mapRequired("Name", Section.Name);
79 IO.mapOptional("Languages", Section.Languages);
80 IO.mapOptional("Tools", Section.Tools);
81 IO.mapOptional("SDKs", Section.SDKs);
8274 }
8375
8476 static void sectionMapping(IO &IO, WasmYAML::CustomSection &Section) {
176168 if (!IO.outputting())
177169 Section.reset(new WasmYAML::NameSection());
178170 sectionMapping(IO, *cast(Section.get()));
179 } else if (SectionName == "producers") {
180 if (!IO.outputting())
181 Section.reset(new WasmYAML::ProducersSection());
182 sectionMapping(IO, *cast(Section.get()));
183171 } else {
184172 if (!IO.outputting())
185173 Section.reset(new WasmYAML::CustomSection(SectionName));
302290 IO &IO, WasmYAML::NameEntry &NameEntry) {
303291 IO.mapRequired("Index", NameEntry.Index);
304292 IO.mapRequired("Name", NameEntry.Name);
305 }
306
307 void MappingTraits::mapping(
308 IO &IO, WasmYAML::ProducerEntry &ProducerEntry) {
309 IO.mapRequired("Name", ProducerEntry.Name);
310 IO.mapRequired("Version", ProducerEntry.Version);
311293 }
312294
313295 void MappingTraits::mapping(
2121 #include "WebAssemblyMCInstLower.h"
2222 #include "WebAssemblyMachineFunctionInfo.h"
2323 #include "WebAssemblyRegisterInfo.h"
24 #include "llvm/ADT/SmallSet.h"
2524 #include "llvm/ADT/StringExtras.h"
2625 #include "llvm/CodeGen/Analysis.h"
2726 #include "llvm/CodeGen/AsmPrinter.h"
145144 OutStreamer->EmitBytes(Contents->getString());
146145 OutStreamer->PopSection();
147146 }
148 }
149
150 if (const NamedMDNode *Ident = M.getNamedMetadata("llvm.ident")) {
151 llvm::SmallSet SeenTools;
152 llvm::SmallVector, 4> Tools;
153 for (size_t i = 0, e = Ident->getNumOperands(); i < e; ++i) {
154 const auto *S = cast(Ident->getOperand(i)->getOperand(0));
155 std::pair Field = S->getString().split("version");
156 StringRef Name = Field.first.trim();
157 StringRef Version = Field.second.trim();
158 if (!SeenTools.insert(Name).second)
159 continue;
160 Tools.emplace_back(Name, Version);
161 }
162 MCSectionWasm *Producers = OutContext.getWasmSection(
163 ".custom_section.producers", SectionKind::getMetadata());
164 OutStreamer->PushSection();
165 OutStreamer->SwitchSection(Producers);
166 OutStreamer->EmitULEB128IntValue(1);
167 OutStreamer->EmitULEB128IntValue(strlen("processed-by"));
168 OutStreamer->EmitBytes("processed-by");
169 OutStreamer->EmitULEB128IntValue(Tools.size());
170 for (auto &Tool : Tools) {
171 OutStreamer->EmitULEB128IntValue(Tool.first.size());
172 OutStreamer->EmitBytes(Tool.first);
173 OutStreamer->EmitULEB128IntValue(Tool.second.size());
174 OutStreamer->EmitBytes(Tool.second);
175 }
176 OutStreamer->PopSection();
177147 }
178148 }
179149
99 !2 = !{ !"green", !"qux" }
1010 !wasm.custom_sections = !{ !0, !1, !2 }
1111
12 !llvm.ident = !{!3}
13 !3 = !{!"clang version 123"}
14
1512 ; CHECK: .section .custom_section.red,"",@
1613 ; CHECK-NEXT: .ascii "foo"
1714
2017
2118 ; CHECK: .section .custom_section.green,"",@
2219 ; CHECK-NEXT: .ascii "qux"
23
24 ; CHECK: .section .custom_section.producers,"",@
25 ; CHECK-NEXT: .int8 1
26 ; CHECK-NEXT: .int8 12
27 ; CHECK-NEXT: .ascii "processed-by"
28 ; CHECK-NEXT: .int8 1
29 ; CHECK-NEXT: .int8 5
30 ; CHECK-NEXT: .ascii "clang"
31 ; CHECK-NEXT: .int8 3
32 ; CHECK-NEXT: .ascii "123"
77 !1 = !{ !"green", !"bar" }
88 !2 = !{ !"green", !"qux" }
99 !wasm.custom_sections = !{ !0, !1, !2 }
10
11 !3 = !{ !"clang version 123"}
12 !llvm.ident = !{!3}
1310
1411 ; CHECK: Section {
1512 ; CHECK: Type: CUSTOM (0x0)
2320 ; CHECK: Offset: 85
2421 ; CHECK: Name: green
2522 ; CHECK: }
26 ; CHECK: Section {
27 ; CHECK: Type: CUSTOM (0x0)
28 ; CHECK: Size: 25
29 ; CHECK: Offset: 118
30 ; CHECK: Name: producers
31 ; CHECK: }
122122 ; CHECK-NEXT: Size: 6
123123 ; CHECK-NEXT: Offset: 991
124124 ; CHECK-NEXT: Name: reloc..debug_line
125 ; CHECK-NEXT: }
126 ; CHECK-NEXT: Section {
127 ; CHECK-NEXT: Type: CUSTOM (0x0)
128 ; CHECK-NEXT: Size: 62
129 ; CHECK-NEXT: Offset: 1021
130 ; CHECK-NEXT: Name: producers
131125 ; CHECK-NEXT: }
132126 ; CHECK-NEXT:]
133127 ; CHECK-NEXT:Relocations [
+0
-29
test/ObjectYAML/wasm/producers_section.yaml less more
None # RUN: yaml2obj %s | obj2yaml | FileCheck %s
1 --- !WASM
2 FileHeader:
3 Version: 0x00000001
4 Sections:
5 - Type: CUSTOM
6 Name: producers
7 Languages:
8 - Name: C++
9 Version: C++11
10 Tools:
11 - Name: clang
12 Version: 123
13 SDKs:
14 - Name: emscripten
15 Version: 9001
16 ...
17 # CHECK: Sections:
18 # CHECK: - Type: CUSTOM
19 # CHECK: Name: producers
20 # CHECK: Languages:
21 # CHECK: - Name: 'C++'
22 # CHECK: Version: 'C++11'
23 # CHECK: Tools:
24 # CHECK: - Name: clang
25 # CHECK: Version: '123'
26 # CHECK: SDKs:
27 # CHECK: - Name: emscripten
28 # CHECK: Version: '9001'
+0
-14
test/ObjectYAML/wasm/producers_section_repeat.yaml less more
None # RUN: yaml2obj %s | not obj2yaml 2>&1 | FileCheck %s
1 --- !WASM
2 FileHeader:
3 Version: 0x00000001
4 Sections:
5 - Type: CUSTOM
6 Name: producers
7 Languages:
8 - Name: C++
9 Version: C++11
10 - Name: C++
11 Version: C++17
12 ...
13 # CHECK: Producers section contains repeated producer
132132 }
133133
134134 CustomSec = std::move(LinkingSec);
135 } else if (WasmSec.Name == "producers") {
136 std::unique_ptr ProducersSec =
137 make_unique();
138 const llvm::wasm::WasmProducerInfo &Info = Obj.getProducerInfo();
139 for (auto &E : Info.Languages) {
140 WasmYAML::ProducerEntry Producer;
141 Producer.Name = E.first;
142 Producer.Version = E.second;
143 ProducersSec->Languages.push_back(Producer);
144 }
145 for (auto &E : Info.Tools) {
146 WasmYAML::ProducerEntry Producer;
147 Producer.Name = E.first;
148 Producer.Version = E.second;
149 ProducersSec->Tools.push_back(Producer);
150 }
151 for (auto &E : Info.SDKs) {
152 WasmYAML::ProducerEntry Producer;
153 Producer.Name = E.first;
154 Producer.Version = E.second;
155 ProducersSec->SDKs.push_back(Producer);
156 }
157 CustomSec = std::move(ProducersSec);
158135 } else {
159136 CustomSec = make_unique(WasmSec.Name);
160137 }
4848 int writeSectionContent(raw_ostream &OS, WasmYAML::DylinkSection &Section);
4949 int writeSectionContent(raw_ostream &OS, WasmYAML::NameSection &Section);
5050 int writeSectionContent(raw_ostream &OS, WasmYAML::LinkingSection &Section);
51 int writeSectionContent(raw_ostream &OS, WasmYAML::ProducersSection &Section);
5251 WasmYAML::Object &Obj;
5352 uint32_t NumImportedFunctions = 0;
5453 uint32_t NumImportedGlobals = 0;
256255 }
257256
258257 int WasmWriter::writeSectionContent(raw_ostream &OS,
259 WasmYAML::ProducersSection &Section) {
260 writeStringRef(Section.Name, OS);
261 int Fields = int(!Section.Languages.empty()) + int(!Section.Tools.empty()) +
262 int(!Section.SDKs.empty());
263 if (Fields == 0)
264 return 0;
265 encodeULEB128(Fields, OS);
266 for (auto &Field : {std::make_pair(StringRef("language"), &Section.Languages),
267 std::make_pair(StringRef("processed-by"), &Section.Tools),
268 std::make_pair(StringRef("sdk"), &Section.SDKs)}) {
269 if (Field.second->empty())
270 continue;
271 writeStringRef(Field.first, OS);
272 encodeULEB128(Field.second->size(), OS);
273 for (auto &Entry : *Field.second) {
274 writeStringRef(Entry.Name, OS);
275 writeStringRef(Entry.Version, OS);
276 }
277 }
278 return 0;
279 }
280
281 int WasmWriter::writeSectionContent(raw_ostream &OS,
282258 WasmYAML::CustomSection &Section) {
283259 if (auto S = dyn_cast(&Section)) {
284260 if (auto Err = writeSectionContent(OS, *S))
287263 if (auto Err = writeSectionContent(OS, *S))
288264 return Err;
289265 } else if (auto S = dyn_cast(&Section)) {
290 if (auto Err = writeSectionContent(OS, *S))
291 return Err;
292 } else if (auto S = dyn_cast(&Section)) {
293266 if (auto Err = writeSectionContent(OS, *S))
294267 return Err;
295268 } else {