Skip to content

Commit 2a0868f

Browse files
committed
[WebAssembly] Merge producers section
llvm-svn: 351412
1 parent a9906c1 commit 2a0868f

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

lld/wasm/InputFiles.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,8 @@ void ObjFile::parse() {
240240
CustomSections.emplace_back(make<InputSection>(Section, this));
241241
CustomSections.back()->setRelocations(Section.Relocations);
242242
CustomSectionsByIndex[SectionIndex] = CustomSections.back();
243+
if (Section.Name == "producers")
244+
ProducersSection = &Section;
243245
}
244246
SectionIndex++;
245247
}

lld/wasm/InputFiles.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ class ObjFile : public InputFile {
9999

100100
const WasmSection *CodeSection = nullptr;
101101
const WasmSection *DataSection = nullptr;
102+
const WasmSection *ProducersSection = nullptr;
102103

103104
// Maps input type indices to output type indices
104105
std::vector<uint32_t> TypeMap;
@@ -139,7 +140,7 @@ class BitcodeFile : public InputFile {
139140
};
140141

141142
// Will report a fatal() error if the input buffer is not a valid bitcode
142-
// or was object file.
143+
// or wasm object file.
143144
InputFile *createObjectFile(MemoryBufferRef MB);
144145

145146
// Opens a given file.

lld/wasm/Writer.cpp

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "lld/Common/Strings.h"
2222
#include "lld/Common/Threads.h"
2323
#include "llvm/ADT/DenseSet.h"
24+
#include "llvm/ADT/SmallVector.h"
2425
#include "llvm/ADT/StringMap.h"
2526
#include "llvm/BinaryFormat/Wasm.h"
2627
#include "llvm/Object/WasmTraits.h"
@@ -95,6 +96,7 @@ class Writer {
9596
void createRelocSections();
9697
void createLinkingSection();
9798
void createNameSection();
99+
void createProducersSection();
98100

99101
void writeHeader();
100102
void writeSections();
@@ -327,7 +329,8 @@ void Writer::calculateCustomSections() {
327329
StringRef Name = Section->getName();
328330
// These custom sections are known the linker and synthesized rather than
329331
// blindly copied
330-
if (Name == "linking" || Name == "name" || Name.startswith("reloc."))
332+
if (Name == "linking" || Name == "name" || Name == "producers" ||
333+
Name.startswith("reloc."))
331334
continue;
332335
// .. or it is a debug section
333336
if (StripDebug && Name.startswith(".debug_"))
@@ -633,6 +636,45 @@ void Writer::createNameSection() {
633636
Sub.writeTo(Section->getStream());
634637
}
635638

639+
void Writer::createProducersSection() {
640+
SmallVector<std::pair<std::string, std::string>, 8> Languages;
641+
SmallVector<std::pair<std::string, std::string>, 8> Tools;
642+
SmallVector<std::pair<std::string, std::string>, 8> SDKs;
643+
for (ObjFile *File : Symtab->ObjectFiles) {
644+
const WasmProducerInfo &Info = File->getWasmObj()->getProducerInfo();
645+
for (auto &Producers : {std::make_pair(&Info.Languages, &Languages),
646+
std::make_pair(&Info.Tools, &Tools),
647+
std::make_pair(&Info.SDKs, &SDKs)})
648+
for (auto &Producer : *Producers.first)
649+
if (Producers.second->end() ==
650+
std::find_if(Producers.second->begin(), Producers.second->end(),
651+
[&](std::pair<std::string, std::string> Seen) {
652+
return Seen.first == Producer.first;
653+
}))
654+
Producers.second->push_back(Producer);
655+
}
656+
int FieldCount =
657+
int(!Languages.empty()) + int(!Tools.empty()) + int(!SDKs.empty());
658+
if (FieldCount == 0)
659+
return;
660+
SyntheticSection *Section =
661+
createSyntheticSection(WASM_SEC_CUSTOM, "producers");
662+
auto &OS = Section->getStream();
663+
writeUleb128(OS, FieldCount, "field count");
664+
for (auto &Field :
665+
{std::make_pair("language", Languages),
666+
std::make_pair("processed-by", Tools), std::make_pair("sdk", SDKs)}) {
667+
if (Field.second.empty())
668+
continue;
669+
writeStr(OS, Field.first, "field name");
670+
writeUleb128(OS, Field.second.size(), "number of entries");
671+
for (auto &Entry : Field.second) {
672+
writeStr(OS, Entry.first, "producer name");
673+
writeStr(OS, Entry.second, "producer version");
674+
}
675+
}
676+
}
677+
636678
void Writer::writeHeader() {
637679
memcpy(Buffer->getBufferStart(), Header.data(), Header.size());
638680
}
@@ -772,9 +814,13 @@ void Writer::createSections() {
772814
createLinkingSection();
773815
createRelocSections();
774816
}
817+
775818
if (!Config->StripDebug && !Config->StripAll)
776819
createNameSection();
777820

821+
if (!Config->StripAll)
822+
createProducersSection();
823+
778824
for (OutputSection *S : OutputSections) {
779825
S->setOffset(FileSize);
780826
S->finalizeContents();

0 commit comments

Comments
 (0)