diff --git a/compiler-rt/lib/profile/InstrProfilingMerge.c b/compiler-rt/lib/profile/InstrProfilingMerge.c --- a/compiler-rt/lib/profile/InstrProfilingMerge.c +++ b/compiler-rt/lib/profile/InstrProfilingMerge.c @@ -44,9 +44,10 @@ /* Check profile header only for now */ __llvm_profile_header *Header = (__llvm_profile_header *)ProfileData; __llvm_profile_data *SrcDataStart, *SrcDataEnd, *SrcData, *DstData; + int BinaryIdsPadding = __llvm_profile_get_num_padding_bytes(Header->BinaryIdsSize); SrcDataStart = (__llvm_profile_data *)(ProfileData + sizeof(__llvm_profile_header) + - Header->BinaryIdsSize); + Header->BinaryIdsSize + BinaryIdsPadding); SrcDataEnd = SrcDataStart + Header->DataSize; if (ProfileSize < sizeof(__llvm_profile_header)) @@ -101,9 +102,10 @@ const char *SrcValueProfDataStart, *SrcValueProfData; uintptr_t CountersDelta = Header->CountersDelta; + int BinaryIdsPadding = __llvm_profile_get_num_padding_bytes(Header->BinaryIdsSize); SrcDataStart = (__llvm_profile_data *)(ProfileData + sizeof(__llvm_profile_header) + - Header->BinaryIdsSize); + Header->BinaryIdsSize + BinaryIdsPadding); SrcDataEnd = SrcDataStart + Header->DataSize; SrcCountersStart = (uint64_t *)SrcDataEnd; SrcNameStart = (const char *)(SrcCountersStart + Header->CountersSize); diff --git a/compiler-rt/lib/profile/InstrProfilingWriter.c b/compiler-rt/lib/profile/InstrProfilingWriter.c --- a/compiler-rt/lib/profile/InstrProfilingWriter.c +++ b/compiler-rt/lib/profile/InstrProfilingWriter.c @@ -295,16 +295,18 @@ return -1; /* Write the binary id lengths and data. */ - if (__llvm_write_binary_ids(Writer) == -1) + int BinaryIdsSize; + if ((BinaryIdsSize = __llvm_write_binary_ids(Writer)) == -1) return -1; /* Write the profile data. */ ProfDataIOVec IOVecData[] = { + {NULL, sizeof(uint8_t), __llvm_profile_get_num_padding_bytes(BinaryIdsSize), 1}, {DataBegin, sizeof(__llvm_profile_data), DataSize, 0}, {NULL, sizeof(uint8_t), PaddingBytesBeforeCounters, 1}, {CountersBegin, sizeof(uint64_t), CountersSize, 0}, {NULL, sizeof(uint8_t), PaddingBytesAfterCounters, 1}, - {SkipNameDataWrite ? NULL : NamesBegin, sizeof(uint8_t), NamesSize, 0}, + {/*SkipNameDataWrite ? NULL :*/ NamesBegin, sizeof(uint8_t), NamesSize, 0}, {NULL, sizeof(uint8_t), PaddingBytesAfterNames, 1}}; if (Writer->Write(Writer, IOVecData, sizeof(IOVecData) / sizeof(*IOVecData))) return -1; diff --git a/compiler-rt/test/profile/Linux/corrupted-profile.c b/compiler-rt/test/profile/Linux/corrupted-profile.c --- a/compiler-rt/test/profile/Linux/corrupted-profile.c +++ b/compiler-rt/test/profile/Linux/corrupted-profile.c @@ -1,6 +1,6 @@ // RUN: rm -f %t.profraw // RUN: touch %t.profraw -// RUN: %clang_profgen -o %t %s +// RUN: %clang_profgen -Wl,--build-id=none -o %t %s // RUN: %t %t.profraw // RUN: %t %t.profraw modifyfile // RUN: cp %t.profraw %t.profraw.old diff --git a/llvm/lib/ProfileData/InstrProfReader.cpp b/llvm/lib/ProfileData/InstrProfReader.cpp --- a/llvm/lib/ProfileData/InstrProfReader.cpp +++ b/llvm/lib/ProfileData/InstrProfReader.cpp @@ -380,7 +380,9 @@ auto PaddingSize = getNumPaddingBytes(NamesSize); // Profile data starts after profile header and binary ids if exist. - ptrdiff_t DataOffset = sizeof(RawInstrProf::Header) + BinaryIdsSize; + ptrdiff_t DataOffset = sizeof(RawInstrProf::Header) + BinaryIdsSize + + getNumPaddingBytes(BinaryIdsSize); + ptrdiff_t CountersOffset = DataOffset + DataSizeInBytes + PaddingBytesBeforeCounters; ptrdiff_t NamesOffset = CountersOffset + (sizeof(uint64_t) * CountersSize) +