Index: lib/DebugInfo/CodeView/ModuleDebugFileChecksumFragment.cpp =================================================================== --- lib/DebugInfo/CodeView/ModuleDebugFileChecksumFragment.cpp +++ lib/DebugInfo/CodeView/ModuleDebugFileChecksumFragment.cpp @@ -83,6 +83,7 @@ } Error ModuleDebugFileChecksumFragment::commit(BinaryStreamWriter &Writer) { + auto StartingOffset = Writer.getOffset(); for (const auto &FC : Checksums) { FileChecksumEntryHeader Header; Header.ChecksumKind = uint8_t(FC.Kind); @@ -92,8 +93,13 @@ return EC; if (auto EC = Writer.writeArray(makeArrayRef(FC.Checksum))) return EC; - if (auto EC = Writer.padToAlignment(4)) - return EC; + auto Offset = Writer.getOffset(); + auto Padding = 4 - ((Offset - StartingOffset) % 4); + if (Padding < 4) { + Offset += Padding; + Writer.setOffset(Offset); + } + StartingOffset = Offset; } return Error::success(); } Index: lib/DebugInfo/CodeView/ModuleDebugFragmentRecord.cpp =================================================================== --- lib/DebugInfo/CodeView/ModuleDebugFragmentRecord.cpp +++ lib/DebugInfo/CodeView/ModuleDebugFragmentRecord.cpp @@ -69,16 +69,19 @@ Error ModuleDebugFragmentRecordBuilder::commit(BinaryStreamWriter &Writer) { ModuleDebugFragmentHeader Header; + auto SerializedLength = calculateSerializedLength(); + auto ExpectedOffset = Writer.getOffset() + SerializedLength; Header.Kind = uint32_t(Kind); - Header.Length = - calculateSerializedLength() - sizeof(ModuleDebugFragmentHeader); + Header.Length = SerializedLength - sizeof(ModuleDebugFragmentHeader); if (auto EC = Writer.writeObject(Header)) return EC; if (auto EC = Frag.commit(Writer)) return EC; - if (auto EC = Writer.padToAlignment(4)) - return EC; + // Pad if necessary. + if (ExpectedOffset > Writer.getOffset()) + Writer.setOffset(ExpectedOffset); + assert(Writer.getOffset() == ExpectedOffset); return Error::success(); }