diff --git a/compiler-rt/lib/profile/InstrProfilingPlatformLinux.c b/compiler-rt/lib/profile/InstrProfilingPlatformLinux.c --- a/compiler-rt/lib/profile/InstrProfilingPlatformLinux.c +++ b/compiler-rt/lib/profile/InstrProfilingPlatformLinux.c @@ -163,6 +163,29 @@ return TotalBinaryIdsSize; } +/** + * Helper function that writes binary ids into profiles, but also takes into + * account padding after the ids to ensure the next section is 8-byte aligned. + * If an error happens while writing, return -1. + */ +static int WriteBinaryIdsWithPadding(ProfDataWriter *Writer, const ElfW(Nhdr) * Note, + const ElfW(Nhdr) * NotesEnd) { + int BinaryIdsSize = WriteBinaryIds(Writer, Note, NotesEnd); + if (BinaryIdsSize == -1) + return -1; + + uint8_t BinaryIdsPadding = __llvm_profile_get_num_padding_bytes(BinaryIdsSize); + if (Writer) { + ProfDataIOVec BinaryIdIOVec[] = { + {NULL, sizeof(uint8_t), BinaryIdsPadding, 1} + }; + if (Writer->Write(Writer, BinaryIdIOVec, sizeof(BinaryIdIOVec) / sizeof(*BinaryIdIOVec))) + return -1; + } + + return BinaryIdsSize + BinaryIdsPadding; +} + /* * Write binary ids into profiles if writer is given. * Return the total size of binary ids. @@ -185,7 +208,7 @@ (const ElfW(Nhdr) *)((uintptr_t)ElfHeader + ProgramHeader[I].p_offset); const ElfW(Nhdr) *NotesEnd = (const ElfW(Nhdr) *)((const char *)(Note) + ProgramHeader[I].p_filesz); - return WriteBinaryIds(Writer, Note, NotesEnd); + return WriteBinaryIdsWithPadding(Writer, Note, NotesEnd); } return 0;