Skip to content
This repository was archived by the owner on Oct 24, 2019. It is now read-only.

Commit de2966b

Browse files
committed
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 502e753 commit de2966b

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

lib/MC/WasmObjectWriter.cpp

+19-1
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)