[llvm] r232422 - InstrProf: Fix CoverageMappingReader on big endian
Justin Bogner
mail at justinbogner.com
Mon Mar 16 14:40:18 PDT 2015
Author: bogner
Date: Mon Mar 16 16:40:18 2015
New Revision: 232422
URL: http://llvm.org/viewvc/llvm-project?rev=232422&view=rev
Log:
InstrProf: Fix CoverageMappingReader on big endian
This makes the reader check the endianness of the object file its
given and behave appropriately. For the test I dug up a really old
linker and created a ppc-apple-darwin file for llvm-cov to read.
Added:
llvm/trunk/test/tools/llvm-cov/Inputs/binary-formats.macho32b (with props)
Modified:
llvm/trunk/lib/ProfileData/CoverageMappingReader.cpp
llvm/trunk/test/tools/llvm-cov/binary-formats.c
Modified: llvm/trunk/lib/ProfileData/CoverageMappingReader.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ProfileData/CoverageMappingReader.cpp?rev=232422&r1=232421&r2=232422&view=diff
==============================================================================
--- llvm/trunk/lib/ProfileData/CoverageMappingReader.cpp (original)
+++ llvm/trunk/lib/ProfileData/CoverageMappingReader.cpp Mon Mar 16 16:40:18 2015
@@ -315,7 +315,7 @@ struct SectionData {
};
}
-template <typename T>
+template <typename T, support::endianness Endian>
std::error_code readCoverageMappingData(
SectionData &ProfileNames, StringRef Data,
std::vector<BinaryCoverageReader::ProfileMappingRecord> &Records,
@@ -327,10 +327,10 @@ std::error_code readCoverageMappingData(
for (const char *Buf = Data.data(), *End = Buf + Data.size(); Buf < End;) {
if (Buf + 4 * sizeof(uint32_t) > End)
return instrprof_error::malformed;
- uint32_t NRecords = endian::readNext<uint32_t, little, unaligned>(Buf);
- uint32_t FilenamesSize = endian::readNext<uint32_t, little, unaligned>(Buf);
- uint32_t CoverageSize = endian::readNext<uint32_t, little, unaligned>(Buf);
- uint32_t Version = endian::readNext<uint32_t, little, unaligned>(Buf);
+ uint32_t NRecords = endian::readNext<uint32_t, Endian, unaligned>(Buf);
+ uint32_t FilenamesSize = endian::readNext<uint32_t, Endian, unaligned>(Buf);
+ uint32_t CoverageSize = endian::readNext<uint32_t, Endian, unaligned>(Buf);
+ uint32_t Version = endian::readNext<uint32_t, Endian, unaligned>(Buf);
switch (Version) {
case CoverageMappingVersion1:
@@ -362,10 +362,10 @@ std::error_code readCoverageMappingData(
while (FunBuf < FunEnd) {
// Read the function information
- T NamePtr = endian::readNext<T, little, unaligned>(FunBuf);
- uint32_t NameSize = endian::readNext<uint32_t, little, unaligned>(FunBuf);
- uint32_t DataSize = endian::readNext<uint32_t, little, unaligned>(FunBuf);
- uint64_t FuncHash = endian::readNext<uint64_t, little, unaligned>(FunBuf);
+ T NamePtr = endian::readNext<T, Endian, unaligned>(FunBuf);
+ uint32_t NameSize = endian::readNext<uint32_t, Endian, unaligned>(FunBuf);
+ uint32_t DataSize = endian::readNext<uint32_t, Endian, unaligned>(FunBuf);
+ uint64_t FuncHash = endian::readNext<uint64_t, Endian, unaligned>(FunBuf);
// Now use that to read the coverage data.
if (CovBuf + DataSize > CovEnd)
@@ -397,8 +397,10 @@ static const char *TestingFormatMagic =
static std::error_code loadTestingFormat(StringRef Data,
SectionData &ProfileNames,
StringRef &CoverageMapping,
- uint8_t &BytesInAddress) {
+ uint8_t &BytesInAddress,
+ support::endianness &Endian) {
BytesInAddress = 8;
+ Endian = support::endianness::little;
Data = Data.substr(StringRef(TestingFormatMagic).size());
if (Data.size() < 1)
@@ -428,6 +430,7 @@ static std::error_code loadBinaryFormat(
SectionData &ProfileNames,
StringRef &CoverageMapping,
uint8_t &BytesInAddress,
+ support::endianness &Endian,
Triple::ArchType Arch) {
auto BinOrErr = object::createBinary(ObjectBuffer);
if (std::error_code EC = BinOrErr.getError())
@@ -453,6 +456,8 @@ static std::error_code loadBinaryFormat(
// The coverage uses native pointer sizes for the object it's written in.
BytesInAddress = OF->getBytesInAddress();
+ Endian = OF->isLittleEndian() ? support::endianness::little
+ : support::endianness::big;
// Look for the sections that we are interested in.
int FoundSectionCount = 0;
@@ -489,22 +494,29 @@ BinaryCoverageReader::create(std::unique
SectionData Profile;
StringRef Coverage;
uint8_t BytesInAddress;
+ support::endianness Endian;
std::error_code EC;
if (ObjectBuffer->getBuffer().startswith(TestingFormatMagic))
// This is a special format used for testing.
EC = loadTestingFormat(ObjectBuffer->getBuffer(), Profile, Coverage,
- BytesInAddress);
+ BytesInAddress, Endian);
else
EC = loadBinaryFormat(ObjectBuffer->getMemBufferRef(), Profile, Coverage,
- BytesInAddress, Arch);
+ BytesInAddress, Endian, Arch);
if (EC)
return EC;
- if (BytesInAddress == 4)
- EC = readCoverageMappingData<uint32_t>(
+ if (BytesInAddress == 4 && Endian == support::endianness::little)
+ EC = readCoverageMappingData<uint32_t, support::endianness::little>(
Profile, Coverage, Reader->MappingRecords, Reader->Filenames);
- else if (BytesInAddress == 8)
- EC = readCoverageMappingData<uint64_t>(
+ else if (BytesInAddress == 4 && Endian == support::endianness::big)
+ EC = readCoverageMappingData<uint32_t, support::endianness::big>(
+ Profile, Coverage, Reader->MappingRecords, Reader->Filenames);
+ else if (BytesInAddress == 8 && Endian == support::endianness::little)
+ EC = readCoverageMappingData<uint64_t, support::endianness::little>(
+ Profile, Coverage, Reader->MappingRecords, Reader->Filenames);
+ else if (BytesInAddress == 8 && Endian == support::endianness::big)
+ EC = readCoverageMappingData<uint64_t, support::endianness::big>(
Profile, Coverage, Reader->MappingRecords, Reader->Filenames);
else
return instrprof_error::malformed;
Added: llvm/trunk/test/tools/llvm-cov/Inputs/binary-formats.macho32b
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-cov/Inputs/binary-formats.macho32b?rev=232422&view=auto
==============================================================================
Binary files llvm/trunk/test/tools/llvm-cov/Inputs/binary-formats.macho32b (added) and llvm/trunk/test/tools/llvm-cov/Inputs/binary-formats.macho32b Mon Mar 16 16:40:18 2015 differ
Propchange: llvm/trunk/test/tools/llvm-cov/Inputs/binary-formats.macho32b
------------------------------------------------------------------------------
svn:executable = *
Modified: llvm/trunk/test/tools/llvm-cov/binary-formats.c
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-cov/binary-formats.c?rev=232422&r1=232421&r2=232422&view=diff
==============================================================================
--- llvm/trunk/test/tools/llvm-cov/binary-formats.c (original)
+++ llvm/trunk/test/tools/llvm-cov/binary-formats.c Mon Mar 16 16:40:18 2015
@@ -6,3 +6,4 @@ int main(int argc, const char *argv[]) {
// RUN: llvm-profdata merge %S/Inputs/binary-formats.proftext -o %t.profdata
// RUN: llvm-cov show %S/Inputs/binary-formats.macho32l -instr-profile %t.profdata -no-colors -filename-equivalence %s | FileCheck %s
// RUN: llvm-cov show %S/Inputs/binary-formats.macho64l -instr-profile %t.profdata -no-colors -filename-equivalence %s | FileCheck %s
+// RUN: llvm-cov show %S/Inputs/binary-formats.macho32b -instr-profile %t.profdata -no-colors -filename-equivalence %s | FileCheck %s
More information about the llvm-commits
mailing list