Index: clang/lib/CodeGen/CoverageMappingGen.cpp =================================================================== --- clang/lib/CodeGen/CoverageMappingGen.cpp +++ clang/lib/CodeGen/CoverageMappingGen.cpp @@ -962,7 +962,8 @@ }; std::string getCoverageSection(const CodeGenModule &CGM) { - return llvm::getInstrProfCoverageSectionName(&CGM.getModule()); + return llvm::getInstrProfCoverageSectionName( + CGM.getContext().getTargetInfo().getTriple().getObjectFormat()); } std::string normalizeFilename(StringRef Filename) { Index: llvm/include/llvm/Object/ObjectFile.h =================================================================== --- llvm/include/llvm/Object/ObjectFile.h +++ llvm/include/llvm/Object/ObjectFile.h @@ -15,6 +15,7 @@ #define LLVM_OBJECT_OBJECTFILE_H #include "llvm/ADT/StringRef.h" +#include "llvm/ADT/Triple.h" #include "llvm/MC/SubtargetFeature.h" #include "llvm/Object/SymbolicFile.h" #include "llvm/Support/DataTypes.h" @@ -269,6 +270,8 @@ virtual SubtargetFeatures getFeatures() const = 0; virtual void setARMSubArch(Triple &TheTriple) const { } + Triple::ObjectFormatType getTripleObjectFormat() const; + /// Returns platform-specific object flags, if any. virtual std::error_code getPlatformFlags(unsigned &Result) const { Result = 0; Index: llvm/include/llvm/ProfileData/InstrProf.h =================================================================== --- llvm/include/llvm/ProfileData/InstrProf.h +++ llvm/include/llvm/ProfileData/InstrProf.h @@ -20,6 +20,7 @@ #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/StringRef.h" #include "llvm/ADT/StringSet.h" +#include "llvm/ADT/Triple.h" #include "llvm/IR/GlobalValue.h" #include "llvm/IR/ProfileSummary.h" #include "llvm/ProfileData/InstrProfData.inc" @@ -56,27 +57,27 @@ /// Return the name of data section containing profile counter variables. /// If M is null, the target platform is assumed to be the same as /// the host machine, and the segment prefix will not be added. -std::string getInstrProfCountersSectionName(const Module *M = nullptr); +std::string getInstrProfCountersSectionName(Triple::ObjectFormatType OF); /// Return the name of data section containing names of instrumented /// functions. If M is null, the target platform is assumed to be the same as /// the host machine, nor will segment prefix be added. -std::string getInstrProfNameSectionName(const Module *M = nullptr); +std::string getInstrProfNameSectionName(Triple::ObjectFormatType OF); /// Return the name of the data section containing per-function control /// data. If M is null, the target platform is assumed to be the same as /// the host machine, and the segment prefix will not be added. -std::string getInstrProfDataSectionName(const Module *M = nullptr); +std::string getInstrProfDataSectionName(Triple::ObjectFormatType OF); /// Return the name of data section containing pointers to value profile /// counters/nodes. If M is null, the target platform is assumed to be /// the same as the host machine, and the segment prefix will not be added. -std::string getInstrProfValuesSectionName(const Module *M = nullptr); +std::string getInstrProfValuesSectionName(Triple::ObjectFormatType OF); /// Return the name of data section containing nodes holdling value /// profiling data. If M is null, the target platform is assumed to be /// the same as the host machine, and the segment prefix will not be added. -std::string getInstrProfVNodesSectionName(const Module *M = nullptr); +std::string getInstrProfVNodesSectionName(Triple::ObjectFormatType OF); /// Return the name profile runtime entry point to do value profiling /// for a given site. @@ -91,7 +92,7 @@ /// Return the name of the section containing function coverage mapping /// data. -std::string getInstrProfCoverageSectionName(const Module *M = nullptr); +std::string getInstrProfCoverageSectionName(Triple::ObjectFormatType OF); /// Return the name prefix of variables containing instrumented function names. inline StringRef getInstrProfNameVarPrefix() { return "__profn_"; } Index: llvm/include/llvm/Transforms/InstrProfiling.h =================================================================== --- llvm/include/llvm/Transforms/InstrProfiling.h +++ llvm/include/llvm/Transforms/InstrProfiling.h @@ -43,6 +43,7 @@ private: InstrProfOptions Options; Module *M; + Triple TT; const TargetLibraryInfo *TLI; struct PerFunctionProfileData { uint32_t NumValueSites[IPVK_Last + 1]; Index: llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp =================================================================== --- llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp +++ llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp @@ -132,10 +132,7 @@ // section(".eh_frame") gcc will produce: // // .section .eh_frame,"a",@progbits - - // TODO: to support Win->ELF cross compilation with coverage properly, - // need to pass the module pointer to the following call. - if (Name == getInstrProfCoverageSectionName()) + if (Name == getInstrProfCoverageSectionName(Triple::ELF)) return SectionKind::getMetadata(); if (Name.empty() || Name[0] != '.') return K; Index: llvm/lib/Object/ObjectFile.cpp =================================================================== --- llvm/lib/Object/ObjectFile.cpp +++ llvm/lib/Object/ObjectFile.cpp @@ -11,6 +11,7 @@ // //===----------------------------------------------------------------------===// +#include "llvm/ADT/Triple.h" #include "llvm/Object/ObjectFile.h" #include "llvm/Object/COFF.h" #include "llvm/Object/MachO.h" @@ -48,6 +49,16 @@ return getSymbolValueImpl(Ref); } +Triple::ObjectFormatType ObjectFile::getTripleObjectFormat() const { + if (isCOFF()) + return Triple::COFF; + if (isMachO()) + return Triple::MachO; + if (isELF()) + return Triple::ELF; + return Triple::UnknownObjectFormat; +} + std::error_code ObjectFile::printSymbolName(raw_ostream &OS, DataRefImpl Symb) const { Expected Name = getSymbolName(Symb); Index: llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp =================================================================== --- llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp +++ llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp @@ -647,16 +647,18 @@ Endian = OF->isLittleEndian() ? support::endianness::little : support::endianness::big; - // Look for the sections that we are interested in. - // TODO: with the current getInstrProfXXXSectionName interfaces, the - // the coverage reader host tool is limited to read coverage section on - // binaries with compatible profile section naming scheme as the host - // platform. Currently, COFF format binaries have different section - // naming scheme from the all the rest. - auto NamesSection = lookupSection(*OF, getInstrProfNameSectionName()); + // Look for the sections that we are interested in. We want the section name + // without the MachO segment, so use the "unknown" object file type to get the + // unprefixed, unsuffixed section name. + auto ObjFormat = OF->getTripleObjectFormat(); + if (ObjFormat == Triple::MachO) + ObjFormat = Triple::UnknownObjectFormat; + auto NamesSection = + lookupSection(*OF, getInstrProfNameSectionName(ObjFormat)); if (auto E = NamesSection.takeError()) return E; - auto CoverageSection = lookupSection(*OF, getInstrProfCoverageSectionName()); + auto CoverageSection = + lookupSection(*OF, getInstrProfCoverageSectionName(ObjFormat)); if (auto E = CoverageSection.takeError()) return E; Index: llvm/lib/ProfileData/InstrProf.cpp =================================================================== --- llvm/lib/ProfileData/InstrProf.cpp +++ llvm/lib/ProfileData/InstrProf.cpp @@ -173,19 +173,15 @@ #include "llvm/ProfileData/InstrProfData.inc" }; -std::string getInstrProfSectionName(const Module *M, InstrProfSectKind Kind) { - - if (!M) - return InstrProfSectName[Kind]; - - bool AddSegment = Triple(M->getTargetTriple()).isOSBinFormatMachO(); +std::string getInstrProfSectionName(Triple::ObjectFormatType OF, + InstrProfSectKind Kind) { std::string SectName; - if (Triple(M->getTargetTriple()).isOSBinFormatCOFF()) + if (OF == Triple::COFF) SectName = InstrProfSectNameCoff[Kind]; else SectName = InstrProfSectNameCommon[Kind]; - if (AddSegment) { + if (OF == Triple::MachO) { SectName = InstrProfSectNamePrefix[Kind] + SectName; if (Kind == IPSK_data) { SectName += ",regular,live_support"; @@ -198,28 +194,28 @@ namespace llvm { -std::string getInstrProfCountersSectionName(const Module *M) { - return getInstrProfSectionName(M, IPSK_cnts); +std::string getInstrProfCountersSectionName(Triple::ObjectFormatType OF) { + return getInstrProfSectionName(OF, IPSK_cnts); } -std::string getInstrProfNameSectionName(const Module *M) { - return getInstrProfSectionName(M, IPSK_name); +std::string getInstrProfNameSectionName(Triple::ObjectFormatType OF) { + return getInstrProfSectionName(OF, IPSK_name); } -std::string getInstrProfDataSectionName(const Module *M) { - return getInstrProfSectionName(M, IPSK_data); +std::string getInstrProfDataSectionName(Triple::ObjectFormatType OF) { + return getInstrProfSectionName(OF, IPSK_data); } -std::string getInstrProfValuesSectionName(const Module *M) { - return getInstrProfSectionName(M, IPSK_vals); +std::string getInstrProfValuesSectionName(Triple::ObjectFormatType OF) { + return getInstrProfSectionName(OF, IPSK_vals); } -std::string getInstrProfVNodesSectionName(const Module *M) { - return getInstrProfSectionName(M, IPSK_vnodes); +std::string getInstrProfVNodesSectionName(Triple::ObjectFormatType OF) { + return getInstrProfSectionName(OF, IPSK_vnodes); } -std::string getInstrProfCoverageSectionName(const Module *M) { - return getInstrProfSectionName(M, IPSK_covmap); +std::string getInstrProfCoverageSectionName(Triple::ObjectFormatType OF) { + return getInstrProfSectionName(OF, IPSK_covmap); } void SoftInstrProfErrors::addError(instrprof_error IE) { Index: llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp =================================================================== --- llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp +++ llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp @@ -141,27 +141,27 @@ } bool InstrProfiling::isMachO() const { - return Triple(M->getTargetTriple()).isOSBinFormatMachO(); + return TT.isOSBinFormatMachO(); } /// Get the section name for the counter variables. std::string InstrProfiling::getCountersSection() const { - return getInstrProfCountersSectionName(M); + return getInstrProfCountersSectionName(TT.getObjectFormat()); } /// Get the section name for the name variables. std::string InstrProfiling::getNameSection() const { - return getInstrProfNameSectionName(M); + return getInstrProfNameSectionName(TT.getObjectFormat()); } /// Get the section name for the profile data variables. std::string InstrProfiling::getDataSection() const { - return getInstrProfDataSectionName(M); + return getInstrProfDataSectionName(TT.getObjectFormat()); } /// Get the section name for the coverage mapping data. std::string InstrProfiling::getCoverageSection() const { - return getInstrProfCoverageSectionName(M); + return getInstrProfCoverageSectionName(TT.getObjectFormat()); } static InstrProfIncrementInst *castToIncrementInst(Instruction *Instr) { @@ -182,6 +182,7 @@ UsedVars.clear(); getMemOPSizeRangeFromOption(MemOPSizeRange, MemOPSizeRangeStart, MemOPSizeRangeLast); + TT = Triple(M.getTargetTriple()); // We did not know how many value sites there would be inside // the instrumented function. This is counting the number of instrumented @@ -462,7 +463,8 @@ Constant::getNullValue(ValuesTy), getVarName(Inc, getInstrProfValuesVarPrefix())); ValuesVar->setVisibility(NamePtr->getVisibility()); - ValuesVar->setSection(getInstrProfValuesSectionName(M)); + ValuesVar->setSection( + getInstrProfValuesSectionName(TT.getObjectFormat())); ValuesVar->setAlignment(8); ValuesVar->setComdat(ProfileVarsComdat); ValuesPtrExpr = @@ -557,7 +559,7 @@ auto *VNodesVar = new GlobalVariable( *M, VNodesTy, false, GlobalValue::PrivateLinkage, Constant::getNullValue(VNodesTy), getInstrProfVNodesVarName()); - VNodesVar->setSection(getInstrProfVNodesSectionName(M)); + VNodesVar->setSection(getInstrProfVNodesSectionName(TT.getObjectFormat())); UsedVars.push_back(VNodesVar); } @@ -676,7 +678,6 @@ GlobalVariable *ProfileNameVar = new GlobalVariable( *M, ProfileNameConst->getType(), true, GlobalValue::WeakAnyLinkage, ProfileNameConst, INSTR_PROF_QUOTE(INSTR_PROF_PROFILE_NAME_VAR)); - Triple TT(M->getTargetTriple()); if (TT.supportsCOMDAT()) { ProfileNameVar->setLinkage(GlobalValue::ExternalLinkage); ProfileNameVar->setComdat(M->getOrInsertComdat( Index: llvm/lib/Transforms/Instrumentation/ThreadSanitizer.cpp =================================================================== --- llvm/lib/Transforms/Instrumentation/ThreadSanitizer.cpp +++ llvm/lib/Transforms/Instrumentation/ThreadSanitizer.cpp @@ -280,7 +280,8 @@ if (GV->hasSection()) { StringRef SectionName = GV->getSection(); // Check if the global is in the PGO counters section. - if (SectionName.endswith(getInstrProfCountersSectionName(M))) + auto OF = Triple(M->getTargetTriple()).getObjectFormat(); + if (SectionName.endswith(getInstrProfCountersSectionName(OF))) return false; } Index: llvm/tools/llvm-cov/TestingSupport.cpp =================================================================== --- llvm/tools/llvm-cov/TestingSupport.cpp +++ llvm/tools/llvm-cov/TestingSupport.cpp @@ -46,20 +46,16 @@ } // Look for the sections that we are interested in. + auto ObjFormat = OF->getTripleObjectFormat(); int FoundSectionCount = 0; SectionRef ProfileNames, CoverageMapping; for (const auto &Section : OF->sections()) { StringRef Name; if (Section.getName(Name)) return 1; - // TODO: with the current getInstrProfXXXSectionName interfaces, the - // the host tool is limited to read coverage section on - // binaries with compatible profile section naming scheme as the host - // platform. Currently, COFF format binaries have different section - // naming scheme from the all the rest. - if (Name == llvm::getInstrProfNameSectionName()) { + if (Name == llvm::getInstrProfNameSectionName(ObjFormat)) { ProfileNames = Section; - } else if (Name == llvm::getInstrProfCoverageSectionName()) { + } else if (Name == llvm::getInstrProfCoverageSectionName(ObjFormat)) { CoverageMapping = Section; } else continue;