Put each jump table in an independent section if the function is too.
[oota-llvm.git] / lib / CodeGen / TargetLoweringObjectFileImpl.cpp
index 41fb0645c813ac8e052557f1b3db10af17d01e25..0a49f5ff9fe365126aff5ee84b47b67b0c898831 100644 (file)
@@ -71,7 +71,6 @@ void TargetLoweringObjectFileELF::emitPersonalityValue(MCStreamer &Streamer,
   const MCSection *Sec = getContext().getELFSection(NameData,
                                                     ELF::SHT_PROGBITS,
                                                     Flags,
-                                                    SectionKind::getDataRel(),
                                                     0, Label->getName());
   unsigned Size = TM.getDataLayout()->getPointerSize();
   Streamer.SwitchSection(Sec);
@@ -167,7 +166,7 @@ static unsigned getELFSectionType(StringRef Name, SectionKind K) {
 
 
 static unsigned
-getELFSectionFlags(SectionKind K) {
+getELFSectionFlags(SectionKind K, bool InCOMDAT) {
   unsigned Flags = 0;
 
   if (!K.isMetadata())
@@ -182,9 +181,10 @@ getELFSectionFlags(SectionKind K) {
   if (K.isThreadLocal())
     Flags |= ELF::SHF_TLS;
 
-  // K.isMergeableConst() is left out to honour PR4650
-  if (K.isMergeableCString() || K.isMergeableConst4() ||
-      K.isMergeableConst8() || K.isMergeableConst16())
+  // FIXME: There is nothing in ELF preventing an SHF_MERGE from being
+  // in a comdat. We just avoid it for now because we don't print
+  // those .sections correctly.
+  if (!InCOMDAT && (K.isMergeableCString() || K.isMergeableConst()))
     Flags |= ELF::SHF_MERGE;
 
   if (K.isMergeableCString())
@@ -214,31 +214,37 @@ const MCSection *TargetLoweringObjectFileELF::getExplicitSectionGlobal(
   Kind = getELFKindForNamedSection(SectionName, Kind);
 
   StringRef Group = "";
-  unsigned Flags = getELFSectionFlags(Kind);
+  unsigned Flags = getELFSectionFlags(Kind, GV->hasComdat());
   if (const Comdat *C = getELFComdat(GV)) {
     Group = C->getName();
     Flags |= ELF::SHF_GROUP;
   }
   return getContext().getELFSection(SectionName,
                                     getELFSectionType(SectionName, Kind), Flags,
-                                    Kind, /*EntrySize=*/0, Group);
+                                    /*EntrySize=*/0, Group);
 }
 
-/// getSectionPrefixForGlobal - Return the section prefix name used by options
-/// FunctionsSections and DataSections.
+/// Return the section prefix name used by options FunctionsSections and
+/// DataSections.
 static StringRef getSectionPrefixForGlobal(SectionKind Kind) {
-  if (Kind.isText())                 return ".text.";
-  if (Kind.isReadOnly())             return ".rodata.";
-  if (Kind.isBSS())                  return ".bss.";
-
-  if (Kind.isThreadData())           return ".tdata.";
-  if (Kind.isThreadBSS())            return ".tbss.";
-
-  if (Kind.isDataNoRel())            return ".data.";
-  if (Kind.isDataRelLocal())         return ".data.rel.local.";
-  if (Kind.isDataRel())              return ".data.rel.";
-  if (Kind.isReadOnlyWithRelLocal()) return ".data.rel.ro.local.";
-
+  if (Kind.isText())
+    return ".text.";
+  if (Kind.isReadOnly())
+    return ".rodata.";
+  if (Kind.isBSS())
+    return ".bss.";
+  if (Kind.isThreadData())
+    return ".tdata.";
+  if (Kind.isThreadBSS())
+    return ".tbss.";
+  if (Kind.isDataNoRel())
+    return ".data.";
+  if (Kind.isDataRelLocal())
+    return ".data.rel.local.";
+  if (Kind.isDataRel())
+    return ".data.rel.";
+  if (Kind.isReadOnlyWithRelLocal())
+    return ".data.rel.ro.local.";
   assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
   return ".data.rel.ro.";
 }
@@ -246,7 +252,7 @@ static StringRef getSectionPrefixForGlobal(SectionKind Kind) {
 const MCSection *TargetLoweringObjectFileELF::
 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
                        Mangler &Mang, const TargetMachine &TM) const {
-  unsigned Flags = getELFSectionFlags(Kind);
+  unsigned Flags = getELFSectionFlags(Kind, GV->hasComdat());
 
   // If we have -ffunction-section or -fdata-section then we should emit the
   // global value to a uniqued section specifically for it.
@@ -270,9 +276,8 @@ SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
       Group = C->getName();
     }
 
-    return getContext().getELFSection(Name.str(),
-                                      getELFSectionType(Name.str(), Kind),
-                                      Flags, Kind, 0, Group);
+    return getContext().getELFSection(
+        Name.str(), getELFSectionType(Name.str(), Kind), Flags, 0, Group);
   }
 
   if (Kind.isText()) return TextSection;
@@ -285,21 +290,19 @@ SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
     unsigned Align =
         TM.getDataLayout()->getPreferredAlignment(cast<GlobalVariable>(GV));
 
-    const char *SizeSpec = ".rodata.str1.";
+    unsigned EntrySize = 1;
     if (Kind.isMergeable2ByteCString())
-      SizeSpec = ".rodata.str2.";
+      EntrySize = 2;
     else if (Kind.isMergeable4ByteCString())
-      SizeSpec = ".rodata.str4.";
+      EntrySize = 4;
     else
       assert(Kind.isMergeable1ByteCString() && "unknown string width");
 
-
+    std::string SizeSpec = ".rodata.str" + utostr(EntrySize) + ".";
     std::string Name = SizeSpec + utostr(Align);
-    return getContext().getELFSection(Name, ELF::SHT_PROGBITS,
-                                      ELF::SHF_ALLOC |
-                                      ELF::SHF_MERGE |
-                                      ELF::SHF_STRINGS,
-                                      Kind);
+    return getContext().getELFSection(
+        Name, ELF::SHT_PROGBITS,
+        ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::SHF_STRINGS, EntrySize, "");
   }
 
   if (Kind.isMergeableConst()) {
@@ -331,6 +334,28 @@ SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
   return DataRelROSection;
 }
 
+const MCSection *TargetLoweringObjectFileELF::getSectionForJumpTable(
+    const Function &F, Mangler &Mang, const TargetMachine &TM) const {
+  // If the function can be removed, produce a unique section so that
+  // the table doesn't prevent the removal.
+  const Comdat *C = F.getComdat();
+  bool EmitUniqueSection = TM.getFunctionSections() || C;
+  if (!EmitUniqueSection)
+    return ReadOnlySection;
+
+  SmallString<128> Name(".rodata.");
+  TM.getNameWithPrefix(Name, &F, Mang, true);
+
+  unsigned Flags = ELF::SHF_ALLOC;
+  StringRef Group = "";
+  if (C) {
+    Flags |= ELF::SHF_GROUP;
+    Group = C->getName();
+  }
+
+  return getContext().getELFSection(Name, ELF::SHT_PROGBITS, Flags, 0, Group);
+}
+
 /// getSectionForConstant - Given a mergeable constant with the
 /// specified size and relocation information, return a section that it
 /// should be placed in.
@@ -359,7 +384,6 @@ static const MCSectionELF *getStaticStructorSection(MCContext &Ctx,
   std::string Name;
   unsigned Type;
   unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE;
-  SectionKind Kind = SectionKind::getDataRel();
   StringRef COMDAT = KeySym ? KeySym->getName() : "";
 
   if (KeySym)
@@ -391,7 +415,7 @@ static const MCSectionELF *getStaticStructorSection(MCContext &Ctx,
     Type = ELF::SHT_PROGBITS;
   }
 
-  return Ctx.getELFSection(Name, Type, Flags, Kind, 0, COMDAT);
+  return Ctx.getELFSection(Name, Type, Flags, 0, COMDAT);
 }
 
 const MCSection *TargetLoweringObjectFileELF::getStaticCtorSection(
@@ -412,16 +436,10 @@ TargetLoweringObjectFileELF::InitializeELF(bool UseInitArray_) {
   if (!UseInitArray)
     return;
 
-  StaticCtorSection =
-    getContext().getELFSection(".init_array", ELF::SHT_INIT_ARRAY,
-                               ELF::SHF_WRITE |
-                               ELF::SHF_ALLOC,
-                               SectionKind::getDataRel());
-  StaticDtorSection =
-    getContext().getELFSection(".fini_array", ELF::SHT_FINI_ARRAY,
-                               ELF::SHF_WRITE |
-                               ELF::SHF_ALLOC,
-                               SectionKind::getDataRel());
+  StaticCtorSection = getContext().getELFSection(
+      ".init_array", ELF::SHT_INIT_ARRAY, ELF::SHF_WRITE | ELF::SHF_ALLOC);
+  StaticDtorSection = getContext().getELFSection(
+      ".fini_array", ELF::SHT_FINI_ARRAY, ELF::SHF_WRITE | ELF::SHF_ALLOC);
 }
 
 //===----------------------------------------------------------------------===//