[lld] r351412 - [WebAssembly] Merge producers section

Thomas Lively via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 16 18:29:41 PST 2019


Author: tlively
Date: Wed Jan 16 18:29:41 2019
New Revision: 351412

URL: http://llvm.org/viewvc/llvm-project?rev=351412&view=rev
Log:
[WebAssembly] Merge producers section

Modified:
    lld/trunk/wasm/InputFiles.cpp
    lld/trunk/wasm/InputFiles.h
    lld/trunk/wasm/Writer.cpp

Modified: lld/trunk/wasm/InputFiles.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/InputFiles.cpp?rev=351412&r1=351411&r2=351412&view=diff
==============================================================================
--- lld/trunk/wasm/InputFiles.cpp (original)
+++ lld/trunk/wasm/InputFiles.cpp Wed Jan 16 18:29:41 2019
@@ -240,6 +240,8 @@ void ObjFile::parse() {
       CustomSections.emplace_back(make<InputSection>(Section, this));
       CustomSections.back()->setRelocations(Section.Relocations);
       CustomSectionsByIndex[SectionIndex] = CustomSections.back();
+      if (Section.Name == "producers")
+        ProducersSection = &Section;
     }
     SectionIndex++;
   }

Modified: lld/trunk/wasm/InputFiles.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/InputFiles.h?rev=351412&r1=351411&r2=351412&view=diff
==============================================================================
--- lld/trunk/wasm/InputFiles.h (original)
+++ lld/trunk/wasm/InputFiles.h Wed Jan 16 18:29:41 2019
@@ -99,6 +99,7 @@ public:
 
   const WasmSection *CodeSection = nullptr;
   const WasmSection *DataSection = nullptr;
+  const WasmSection *ProducersSection = nullptr;
 
   // Maps input type indices to output type indices
   std::vector<uint32_t> TypeMap;
@@ -139,7 +140,7 @@ public:
 };
 
 // Will report a fatal() error if the input buffer is not a valid bitcode
-// or was object file.
+// or wasm object file.
 InputFile *createObjectFile(MemoryBufferRef MB);
 
 // Opens a given file.

Modified: lld/trunk/wasm/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/Writer.cpp?rev=351412&r1=351411&r2=351412&view=diff
==============================================================================
--- lld/trunk/wasm/Writer.cpp (original)
+++ lld/trunk/wasm/Writer.cpp Wed Jan 16 18:29:41 2019
@@ -21,6 +21,7 @@
 #include "lld/Common/Strings.h"
 #include "lld/Common/Threads.h"
 #include "llvm/ADT/DenseSet.h"
+#include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringMap.h"
 #include "llvm/BinaryFormat/Wasm.h"
 #include "llvm/Object/WasmTraits.h"
@@ -95,6 +96,7 @@ private:
   void createRelocSections();
   void createLinkingSection();
   void createNameSection();
+  void createProducersSection();
 
   void writeHeader();
   void writeSections();
@@ -327,7 +329,8 @@ void Writer::calculateCustomSections() {
       StringRef Name = Section->getName();
       // These custom sections are known the linker and synthesized rather than
       // blindly copied
-      if (Name == "linking" || Name == "name" || Name.startswith("reloc."))
+      if (Name == "linking" || Name == "name" || Name == "producers" ||
+          Name.startswith("reloc."))
         continue;
       // .. or it is a debug section
       if (StripDebug && Name.startswith(".debug_"))
@@ -633,6 +636,45 @@ void Writer::createNameSection() {
   Sub.writeTo(Section->getStream());
 }
 
+void Writer::createProducersSection() {
+  SmallVector<std::pair<std::string, std::string>, 8> Languages;
+  SmallVector<std::pair<std::string, std::string>, 8> Tools;
+  SmallVector<std::pair<std::string, std::string>, 8> SDKs;
+  for (ObjFile *File : Symtab->ObjectFiles) {
+    const WasmProducerInfo &Info = File->getWasmObj()->getProducerInfo();
+    for (auto &Producers : {std::make_pair(&Info.Languages, &Languages),
+                            std::make_pair(&Info.Tools, &Tools),
+                            std::make_pair(&Info.SDKs, &SDKs)})
+      for (auto &Producer : *Producers.first)
+        if (Producers.second->end() ==
+            std::find_if(Producers.second->begin(), Producers.second->end(),
+                         [&](std::pair<std::string, std::string> Seen) {
+                           return Seen.first == Producer.first;
+                         }))
+          Producers.second->push_back(Producer);
+  }
+  int FieldCount =
+      int(!Languages.empty()) + int(!Tools.empty()) + int(!SDKs.empty());
+  if (FieldCount == 0)
+    return;
+  SyntheticSection *Section =
+      createSyntheticSection(WASM_SEC_CUSTOM, "producers");
+  auto &OS = Section->getStream();
+  writeUleb128(OS, FieldCount, "field count");
+  for (auto &Field :
+       {std::make_pair("language", Languages),
+        std::make_pair("processed-by", Tools), std::make_pair("sdk", SDKs)}) {
+    if (Field.second.empty())
+      continue;
+    writeStr(OS, Field.first, "field name");
+    writeUleb128(OS, Field.second.size(), "number of entries");
+    for (auto &Entry : Field.second) {
+      writeStr(OS, Entry.first, "producer name");
+      writeStr(OS, Entry.second, "producer version");
+    }
+  }
+}
+
 void Writer::writeHeader() {
   memcpy(Buffer->getBufferStart(), Header.data(), Header.size());
 }
@@ -772,9 +814,13 @@ void Writer::createSections() {
     createLinkingSection();
     createRelocSections();
   }
+
   if (!Config->StripDebug && !Config->StripAll)
     createNameSection();
 
+  if (!Config->StripAll)
+    createProducersSection();
+
   for (OutputSection *S : OutputSections) {
     S->setOffset(FileSize);
     S->finalizeContents();




More information about the llvm-commits mailing list