<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - [WebAssembly] When compiling a fairly large C++ project, clang generates invalid precompiled module file"
   href="https://bugs.llvm.org/show_bug.cgi?id=35928">35928</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>[WebAssembly] When compiling a fairly large C++ project, clang generates invalid precompiled module file
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libraries
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>Backend: WebAssembly
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>patrickyccheng@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=19667" name="attach_19667" title="An example of the generated .pcm file">attachment 19667</a> <a href="attachment.cgi?id=19667&action=edit" title="An example of the generated .pcm file">[details]</a></span>
An example of the generated .pcm file

The existing C++ project uses -module-cache-path flag when compiling.

When porting to WebAssembly with -target wasm32-unknown-unknown-wasm

I am getting:
error: file 'XXXXX.pcm' is not a valid precompiled module file

Looks like in this case, the PCM file is generated in WASM Object Format.

And when trying to read the generated file, clang was not able to find the
'__clangast' section.

Here is the file that contains the code that read and write the __clangast
section:

<a href="https://reviews.llvm.org/source/clang/browse/cfe/trunk/lib/CodeGen/ObjectFilePCHContainerOperations.cpp">https://reviews.llvm.org/source/clang/browse/cfe/trunk/lib/CodeGen/ObjectFilePCHContainerOperations.cpp</a>

Write:
    auto *ASTSym = new llvm::GlobalVariable(
        *M, Ty, /*constant*/ true, llvm::GlobalVariable::InternalLinkage, Data,
        "__clang_ast");
    // The on-disk hashtable needs to be aligned.
    ASTSym->setAlignment(8);
...
      ASTSym->setSection("__CLANG,__clangast");

Read:
    // OF is a llvm::ObjectFile
      for (auto &Section : OF->sections()) {
        StringRef Name;
        Section.getName(Name);
        if ((!IsCOFF && Name == "__clangast") || (IsCOFF && Name ==
"clangast")) {

Specifically, ObjectFilePCHContainerReader::ExtractPCH was not able to find the
'__clangast' section.

Here is my "fix" in clang, but expose some of internals of WasmObjectFile, so I
am not sure if this is correct.

I attached one of the generated PCM file for reference.

---

>From 05a85d9c22d2024d6e5d4dcb044690774e658feb Mon Sep 17 00:00:00 2001
From: Patrick Cheng <<a href="mailto:patrickyccheng@gmail.com">patrickyccheng@gmail.com</a>>
Date: Fri, 12 Jan 2018 08:27:23 -0800
Subject: [PATCH] Hack to fix "error: file 'XXXXX.pcm' is not a valid
 precompiled module file" when using -target=wasm32-none-none-wasm

---
 lib/CodeGen/ObjectFilePCHContainerOperations.cpp | 32 ++++++++++++++++++------
 1 file changed, 24 insertions(+), 8 deletions(-)

diff --git a/lib/CodeGen/ObjectFilePCHContainerOperations.cpp
b/lib/CodeGen/ObjectFilePCHContainerOperations.cpp
index 150b64f..f4ac8e9 100644
--- a/lib/CodeGen/ObjectFilePCHContainerOperations.cpp
+++ b/lib/CodeGen/ObjectFilePCHContainerOperations.cpp
@@ -31,6 +31,7 @@
 #include "llvm/IR/Module.h"
 #include "llvm/Object/COFF.h"
 #include "llvm/Object/ObjectFile.h"
+#include "llvm/Object/Wasm.h"
 #include "llvm/Support/Path.h"
 #include "llvm/Support/TargetRegistry.h"
 #include <memory>
@@ -331,14 +332,29 @@
ObjectFilePCHContainerReader::ExtractPCH(llvm::MemoryBufferRef Buffer) const {
   auto OFOrErr = llvm::object::ObjectFile::createObjectFile(Buffer);
   if (OFOrErr) {
     auto &OF = OFOrErr.get();
-    bool IsCOFF = isa<llvm::object::COFFObjectFile>(*OF);
-    // Find the clang AST section in the container.
-    for (auto &Section : OF->sections()) {
-      StringRef Name;
-      Section.getName(Name);
-      if ((!IsCOFF && Name == "__clangast") || (IsCOFF && Name == "clangast"))
{
-        Section.getContents(PCH);
-        return PCH;
+    bool IsWASM = isa<llvm::object::WasmObjectFile>(*OF);
+    if (IsWASM) {
+      auto &WF = (std::unique_ptr<llvm::object::WasmObjectFile> &)(OF);
+      const auto &dataSegments = WF->dataSegments();
+      uint32_t count = dataSegments.size();
+      for (uint32_t i = 0; i < count; i++) {
+        auto wasmDataSegment = dataSegments[i].Data;
+        if (wasmDataSegment.Name == "__clangast") {
+          PCH = StringRef(reinterpret_cast<const char
*>(wasmDataSegment.Content.data()),
+                          wasmDataSegment.Content.size());
+          return PCH;
+        }
+      }
+    } else {
+      bool IsCOFF = isa<llvm::object::COFFObjectFile>(*OF);
+      // Find the clang AST section in the container.
+      for (auto &Section : OF->sections()) {
+        StringRef Name;
+        Section.getName(Name);
+        if ((!IsCOFF && Name == "__clangast") || (IsCOFF && Name ==
"clangast")) {
+          Section.getContents(PCH);
+          return PCH;
+        }
       }
     }
   }
-- 
2.10.1</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>