[Profile] PE binary coverage bug fix

PR/32584

Differential Revision: https://reviews.llvm.org/D32023

llvm-svn: 300277
diff --git a/llvm/lib/ProfileData/InstrProf.cpp b/llvm/lib/ProfileData/InstrProf.cpp
index 0ec3fce..d66d25e 100644
--- a/llvm/lib/ProfileData/InstrProf.cpp
+++ b/llvm/lib/ProfileData/InstrProf.cpp
@@ -136,8 +136,92 @@
   return *ErrorCategory;
 }
 
+namespace {
+
+enum InstrProfSectKind {
+#define INSTR_PROF_SECT_ENTRY(Kind, SectName, SectNameCommon, SectNameCoff,    \
+                              Prefix)                                          \
+  Kind,
+#include "llvm/ProfileData/InstrProfData.inc"
+};
+
+const char *InstrProfSectName[] = {
+#define INSTR_PROF_SECT_ENTRY(Kind, SectName, SectNameCommon, SectNameCoff,    \
+                              Prefix)                                          \
+  SectName,
+#include "llvm/ProfileData/InstrProfData.inc"
+};
+
+const char *InstrProfSectNameCommon[] = {
+#define INSTR_PROF_SECT_ENTRY(Kind, SectName, SectNameCommon, SectNameCoff,    \
+                              Prefix)                                          \
+  SectNameCommon,
+#include "llvm/ProfileData/InstrProfData.inc"
+};
+
+const char *InstrProfSectNameCoff[] = {
+#define INSTR_PROF_SECT_ENTRY(Kind, SectName, SectNameCommon, SectNameCoff,    \
+                              Prefix)                                          \
+  SectNameCoff,
+#include "llvm/ProfileData/InstrProfData.inc"
+};
+
+const char *InstrProfSectNamePrefix[] = {
+#define INSTR_PROF_SECT_ENTRY(Kind, SectName, SectNameCommon, SectNameCoff,    \
+                              Prefix)                                          \
+  Prefix,
+#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 SectName;
+  if (Triple(M->getTargetTriple()).isOSBinFormatCOFF())
+    SectName = InstrProfSectNameCoff[Kind];
+  else
+    SectName = InstrProfSectNameCommon[Kind];
+
+  if (AddSegment) {
+    SectName = InstrProfSectNamePrefix[Kind] + SectName;
+    if (Kind == IPSK_data) {
+      SectName += ",regular,live_support";
+    }
+  }
+  return SectName;
+}
+
+} // namespace
+
 namespace llvm {
 
+std::string getInstrProfCountersSectionName(const Module *M) {
+  return getInstrProfSectionName(M, IPSK_cnts);
+}
+
+std::string getInstrProfNameSectionName(const Module *M) {
+  return getInstrProfSectionName(M, IPSK_name);
+}
+
+std::string getInstrProfDataSectionName(const Module *M) {
+  return getInstrProfSectionName(M, IPSK_data);
+}
+
+std::string getInstrProfValuesSectionName(const Module *M) {
+  return getInstrProfSectionName(M, IPSK_vals);
+}
+
+std::string getInstrProfVNodesSectionName(const Module *M) {
+  return getInstrProfSectionName(M, IPSK_vnodes);
+}
+
+std::string getInstrProfCoverageSectionName(const Module *M) {
+  return getInstrProfSectionName(M, IPSK_covmap);
+}
+
 void SoftInstrProfErrors::addError(instrprof_error IE) {
   if (IE == instrprof_error::success)
     return;