Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit 36783fb

Browse files
zhuoweiMaxDesiatov
authored andcommitted
WebAssembly: pad __clangast section to 4 bytes
Like Patcheng's https://reviews.llvm.org/D42233 but for Custom Sections instead padding for the padding gods.
1 parent 6eadbe3 commit 36783fb

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

lib/MC/WasmObjectWriter.cpp

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,15 @@ void WasmObjectWriter::startCustomSection(SectionBookkeeping &Section,
372372
Section.PayloadOffset = W.OS.tell();
373373

374374
// Custom sections in wasm also have a string identifier.
375-
writeString(Name);
375+
if (Name != "__clangast") {
376+
writeString(Name);
377+
} else {
378+
// pad section start to nearest 4 bytes for Clang PCH
379+
uint64_t MinLength = Section.PayloadOffset + 5ULL /* min ULEB128 length */ + Name.size();
380+
uint64_t RoundedUpLength = (MinLength + 3ULL) & ~3ULL;
381+
encodeULEB128(Name.size(), W.OS, 5 + (RoundedUpLength - MinLength));
382+
W.OS << Name;
383+
}
376384

377385
// The position where the custom section starts.
378386
Section.ContentsOffset = W.OS.tell();
@@ -1046,8 +1054,16 @@ void WasmObjectWriter::writeCustomSection(WasmCustomSection &CustomSection,
10461054
auto *Sec = CustomSection.Section;
10471055
startCustomSection(Section, CustomSection.Name);
10481056

1049-
Sec->setSectionOffset(W.OS.tell() - Section.ContentsOffset);
1050-
Asm.writeSectionData(W.OS, Sec, Layout);
1057+
if (CustomSection.Name == "__clangast") {
1058+
// pad to nearest 4 bytes
1059+
uint64_t RoundedUp = (Section.ContentsOffset + 3ULL) & ~3ULL;
1060+
for (uint64_t Count = 0; Count < RoundedUp - Section.ContentsOffset; Count++) {
1061+
W.OS << char(0);
1062+
}
1063+
}
1064+
1065+
Sec->setSectionOffset(W.OS.tell() - Section.ContentsOffset);
1066+
Asm.writeSectionData(W.OS, Sec, Layout);
10511067

10521068
CustomSection.OutputContentsOffset = Section.ContentsOffset;
10531069
CustomSection.OutputIndex = Section.Index;
@@ -1365,6 +1381,10 @@ uint64_t WasmObjectWriter::writeObject(MCAssembler &Asm,
13651381
LLVM_DEBUG(dbgs() << " -> function index: " << Index << "\n");
13661382

13671383
} else if (WS.isData()) {
1384+
if (WS.getName() == "__clang_ast")
1385+
continue;
1386+
if (WS.isTemporary() && !WS.getSize())
1387+
continue;
13681388
if (!isInSymtab(WS))
13691389
continue;
13701390

0 commit comments

Comments
 (0)