diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp --- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp +++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp @@ -1696,6 +1696,10 @@ if (K.isText()) return SectionKind::getText(); + // Clang precompiled header data isn't needed at runtime; use custom section + if (Name == "__clangast") + return SectionKind::getMetadata(); + // Otherwise, ignore whatever section type the generic impl detected and use // a plain data section. return SectionKind::getData(); diff --git a/llvm/lib/MC/WasmObjectWriter.cpp b/llvm/lib/MC/WasmObjectWriter.cpp --- a/llvm/lib/MC/WasmObjectWriter.cpp +++ b/llvm/lib/MC/WasmObjectWriter.cpp @@ -373,7 +373,16 @@ Section.PayloadOffset = W.OS.tell(); // Custom sections in wasm also have a string identifier. - writeString(Name); + if (Name != "__clangast") { + writeString(Name); + } else { + // pad section start to nearest 4 bytes for Clang PCH + uint64_t MinLength = + Section.PayloadOffset + 5ULL /* min ULEB128 length */ + Name.size(); + uint64_t RoundedUpLength = (MinLength + 3ULL) & ~3ULL; + encodeULEB128(Name.size(), W.OS, 5 + (RoundedUpLength - MinLength)); + W.OS << Name; + } // The position where the custom section starts. Section.ContentsOffset = W.OS.tell(); @@ -1099,6 +1108,10 @@ if (Sym.isSection()) return false; + // Clang's precompiled headers are in a separate custom section + if (Sym.getName() == "__clang_ast") + return false; + return true; }