Fix several fixmes and clean up code by sinking *all* section
creation activity into the target-specific subclasses of TLOF.
Before this, globals with explicit sections could be created by
the base class.

1. make getOrCreateSection protected, add a new getExplicitSectionGlobal
   pure virtual method to assign sections to globals with a specified
   section.
2. eliminate getSpecialCasedSectionGlobals, which is now PIC specific.
3. eliminate the getKindForNamedSection virtual method, which is
   now just a static method for ELF.
4. Add implementions of getExplicitSectionGlobal for ELF/PECOFF/Darwin/PIC16.
   They are now all detangled and understandable, woo! :)


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@78319 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Target/PIC16/PIC16TargetObjectFile.cpp b/lib/Target/PIC16/PIC16TargetObjectFile.cpp
index f15f332..b09ad30 100644
--- a/lib/Target/PIC16/PIC16TargetObjectFile.cpp
+++ b/lib/Target/PIC16/PIC16TargetObjectFile.cpp
@@ -242,28 +242,26 @@
 
 /// getSpecialCasedSectionGlobals - Allow the target to completely override
 /// section assignment of a global.
-const MCSection *
-PIC16TargetObjectFile::getSpecialCasedSectionGlobals(const GlobalValue *GV,
-                                                     Mangler *Mang,
-                                                     SectionKind Kind) const {
-  // If GV has a sectin name or section address create that section now.
-  if (GV->hasSection()) {
-    if (const GlobalVariable *GVar = cast<GlobalVariable>(GV)) {
-      std::string SectName = GVar->getSection();
-      // If address for a variable is specified, get the address and create
-      // section.
-      std::string AddrStr = "Address=";
-      if (SectName.compare(0, AddrStr.length(), AddrStr) == 0) {
-        std::string SectAddr = SectName.substr(AddrStr.length());
-        return CreateSectionForGlobal(GVar, Mang, SectAddr);
-      }
-       
-      // Create the section specified with section attribute. 
-      return CreateSectionForGlobal(GVar, Mang);
+const MCSection *PIC16TargetObjectFile::
+getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind, 
+                         Mangler *Mang, const TargetMachine &TM) const {
+  assert(GV->hasSection());
+  
+  if (const GlobalVariable *GVar = cast<GlobalVariable>(GV)) {
+    std::string SectName = GVar->getSection();
+    // If address for a variable is specified, get the address and create
+    // section.
+    std::string AddrStr = "Address=";
+    if (SectName.compare(0, AddrStr.length(), AddrStr) == 0) {
+      std::string SectAddr = SectName.substr(AddrStr.length());
+      return CreateSectionForGlobal(GVar, Mang, SectAddr);
     }
+     
+    // Create the section specified with section attribute. 
+    return CreateSectionForGlobal(GVar, Mang);
   }
 
-  return 0;
+  return getOrCreateSection(GV->getSection().c_str(), false, Kind);
 }
 
 // Create a new section for global variable. If Addr is given then create
diff --git a/lib/Target/PIC16/PIC16TargetObjectFile.h b/lib/Target/PIC16/PIC16TargetObjectFile.h
index 6cc7054..c261108 100644
--- a/lib/Target/PIC16/PIC16TargetObjectFile.h
+++ b/lib/Target/PIC16/PIC16TargetObjectFile.h
@@ -59,11 +59,9 @@
     void Initialize(MCContext &Ctx, const TargetMachine &TM);
 
     
-    /// getSpecialCasedSectionGlobals - Allow the target to completely override
-    /// section assignment of a global.
     virtual const MCSection *
-    getSpecialCasedSectionGlobals(const GlobalValue *GV, Mangler *Mang,
-                                  SectionKind Kind) const;
+    getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind, 
+                             Mangler *Mang, const TargetMachine &TM) const;
     
     virtual const MCSection *SelectSectionForGlobal(const GlobalValue *GV,
                                                     SectionKind Kind,
diff --git a/lib/Target/TargetLoweringObjectFile.cpp b/lib/Target/TargetLoweringObjectFile.cpp
index 23e549a..6aca6da 100644
--- a/lib/Target/TargetLoweringObjectFile.cpp
+++ b/lib/Target/TargetLoweringObjectFile.cpp
@@ -233,20 +233,9 @@
 SectionForGlobal(const GlobalValue *GV, SectionKind Kind, Mangler *Mang,
                  const TargetMachine &TM) const {
   // Select section name.
-  if (GV->hasSection()) {
-    // If the target has special section hacks for specifically named globals,
-    // return them now.
-    if (const MCSection *TS = getSpecialCasedSectionGlobals(GV, Mang, Kind))
-      return TS;
-    
-    // If the target has magic semantics for certain section names, make sure to
-    // pick up the flags.  This allows the user to write things with attribute
-    // section and still get the appropriate section flags printed.
-    Kind = getKindForNamedSection(GV->getSection().c_str(), Kind);
-    
-    return getOrCreateSection(GV->getSection().c_str(), false, Kind);
-  }
-
+  if (GV->hasSection())
+    return getExplicitSectionGlobal(GV, Kind, Mang, TM);
+  
   
   // Use default section depending on the 'type' of global
   return SelectSectionForGlobal(GV, Kind, Mang, TM);
@@ -381,8 +370,8 @@
 }
 
 
-SectionKind TargetLoweringObjectFileELF::
-getKindForNamedSection(const char *Name, SectionKind K) const {
+static SectionKind 
+getELFKindForNamedSection(const char *Name, SectionKind K) {
   if (Name[0] != '.') return K;
   
   // Some lame default implementation based on some magic section names.
@@ -407,6 +396,17 @@
   return K;
 }
 
+const MCSection *TargetLoweringObjectFileELF::
+getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind, 
+                         Mangler *Mang, const TargetMachine &TM) const {
+  // Infer section flags from the section name if we can.
+  Kind = getELFKindForNamedSection(GV->getSection().c_str(), Kind);
+  
+  return getOrCreateSection(GV->getSection().c_str(), false, Kind);
+}
+      
+      
+      
 void TargetLoweringObjectFileELF::
 getSectionFlagsAsString(SectionKind Kind, SmallVectorImpl<char> &Str) const {
   Str.push_back(',');
@@ -689,6 +689,12 @@
 }
 
 const MCSection *TargetLoweringObjectFileMachO::
+getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind, 
+                         Mangler *Mang, const TargetMachine &TM) const {
+  return getOrCreateSection(GV->getSection().c_str(), false, Kind);
+}
+
+const MCSection *TargetLoweringObjectFileMachO::
 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
                        Mangler *Mang, const TargetMachine &TM) const {
   assert(!Kind.isThreadLocal() && "Darwin doesn't support TLS");
@@ -837,6 +843,13 @@
                        true, SectionKind::getMetadata());
 }
 
+const MCSection *TargetLoweringObjectFileCOFF::
+getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind, 
+                         Mangler *Mang, const TargetMachine &TM) const {
+  return getOrCreateSection(GV->getSection().c_str(), false, Kind);
+}
+
+
 void TargetLoweringObjectFileCOFF::
 getSectionFlagsAsString(SectionKind Kind, SmallVectorImpl<char> &Str) const {
   // FIXME: Inefficient.