@@ -359,7 +359,15 @@ void WasmObjectWriter::startCustomSection(SectionBookkeeping &Section,
359359 Section.PayloadOffset = W.OS .tell ();
360360
361361 // Custom sections in wasm also have a string identifier.
362- writeString (Name);
362+ if (Name != " __clangast" ) {
363+ writeString (Name);
364+ } else {
365+ // pad section start to nearest 4 bytes for Clang PCH
366+ uint64_t MinLength = Section.PayloadOffset + 5ULL /* min ULEB128 length */ + Name.size ();
367+ uint64_t RoundedUpLength = (MinLength + 3ULL ) & ~3ULL ;
368+ encodeULEB128 (Name.size (), W.OS , 5 + (RoundedUpLength - MinLength));
369+ W.OS << Name;
370+ }
363371
364372 // The position where the custom section starts.
365373 Section.ContentsOffset = W.OS .tell ();
@@ -1052,6 +1060,14 @@ void WasmObjectWriter::writeCustomSections(const MCAssembler &Asm,
10521060 auto *Sec = CustomSection.Section ;
10531061 startCustomSection (Section, CustomSection.Name );
10541062
1063+ if (CustomSection.Name == " __clangast" ) {
1064+ // pad to nearest 4 bytes
1065+ uint64_t RoundedUp = (Section.ContentsOffset + 3ULL ) & ~3ULL ;
1066+ for (uint64_t Count = 0 ; Count < RoundedUp - Section.ContentsOffset ; Count++) {
1067+ W.OS << char (0 );
1068+ }
1069+ }
1070+
10551071 Sec->setSectionOffset (W.OS .tell () - Section.ContentsOffset );
10561072 Asm.writeSectionData (W.OS , Sec, Layout);
10571073
@@ -1342,6 +1358,8 @@ uint64_t WasmObjectWriter::writeObject(MCAssembler &Asm,
13421358 LLVM_DEBUG (dbgs () << " -> function index: " << Index << " \n " );
13431359
13441360 } else if (WS.isData ()) {
1361+ if (WS.getName () == " __clang_ast" )
1362+ continue ;
13451363 if (WS.isTemporary () && !WS.getSize ())
13461364 continue ;
13471365
0 commit comments