expose SectionKindForGlobal to curious clients, named as
[oota-llvm.git] / lib / Target / TargetLoweringObjectFile.cpp
index 089143a024c5dc609f76d775e78f552658f2e098..23e549a42b08442140be4b0a328dd8c1fe2e11ec 100644 (file)
@@ -32,11 +32,25 @@ using namespace llvm;
 TargetLoweringObjectFile::TargetLoweringObjectFile() : Ctx(0) {
   TextSection = 0;
   DataSection = 0;
-  BSSSection_ = 0;
+  BSSSection = 0;
   ReadOnlySection = 0;
-  TLSDataSection = 0;
-  TLSBSSSection = 0;
-  CStringSection_ = 0;
+  StaticCtorSection = 0;
+  StaticDtorSection = 0;
+  LSDASection = 0;
+  EHFrameSection = 0;
+
+  DwarfAbbrevSection = 0;
+  DwarfInfoSection = 0;
+  DwarfLineSection = 0;
+  DwarfFrameSection = 0;
+  DwarfPubNamesSection = 0;
+  DwarfPubTypesSection = 0;
+  DwarfDebugInlineSection = 0;
+  DwarfStrSection = 0;
+  DwarfLocSection = 0;
+  DwarfARangesSection = 0;
+  DwarfRangesSection = 0;
+  DwarfMacroInfoSection = 0;
 }
 
 TargetLoweringObjectFile::~TargetLoweringObjectFile() {
@@ -65,47 +79,65 @@ static bool isSuitableForBSS(const GlobalVariable *GV) {
   return true;
 }
 
-static bool isConstantString(const Constant *C) {
+/// IsNullTerminatedString - Return true if the specified constant (which is
+/// known to have a type that is an array of 1/2/4 byte elements) ends with a
+/// nul value and contains no other nuls in it.
+static bool IsNullTerminatedString(const Constant *C) {
+  const ArrayType *ATy = cast<ArrayType>(C->getType());
+  
   // First check: is we have constant array of i8 terminated with zero
-  const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
-  // Check, if initializer is a null-terminated string
-  if (CVA && CVA->isCString())
+  if (const ConstantArray *CVA = dyn_cast<ConstantArray>(C)) {
+    if (ATy->getNumElements() == 0) return false;
+
+    ConstantInt *Null =
+      dyn_cast<ConstantInt>(CVA->getOperand(ATy->getNumElements()-1));
+    if (Null == 0 || Null->getZExtValue() != 0)
+      return false; // Not null terminated.
+    
+    // Verify that the null doesn't occur anywhere else in the string.
+    for (unsigned i = 0, e = ATy->getNumElements()-1; i != e; ++i)
+      // Reject constantexpr elements etc.
+      if (!isa<ConstantInt>(CVA->getOperand(i)) ||
+          CVA->getOperand(i) == Null)
+        return false;
     return true;
+  }
 
   // Another possibility: [1 x i8] zeroinitializer
   if (isa<ConstantAggregateZero>(C))
-    if (const ArrayType *Ty = dyn_cast<ArrayType>(C->getType()))
-      return (Ty->getElementType() == Type::Int8Ty &&
-              Ty->getNumElements() == 1);
+    return ATy->getNumElements() == 1;
 
   return false;
 }
 
-/// SectionKindForGlobal - This is a top-level target-independent classifier for
+/// getKindForGlobal - This is a top-level target-independent classifier for
 /// a global variable.  Given an global variable and information from TM, it
 /// classifies the global in a variety of ways that make various target
 /// implementations simpler.  The target implementation is free to ignore this
 /// extra info of course.
-static SectionKind SectionKindForGlobal(const GlobalValue *GV,
-                                        const TargetMachine &TM) {
+SectionKind TargetLoweringObjectFile::getKindForGlobal(const GlobalValue *GV,
+                                                       const TargetMachine &TM){
+  assert(!GV->isDeclaration() && !GV->hasAvailableExternallyLinkage() &&
+         "Can only be used for global definitions");
+  
   Reloc::Model ReloModel = TM.getRelocationModel();
   
   // Early exit - functions should be always in text sections.
   const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
   if (GVar == 0)
-    return SectionKind::get(SectionKind::Text);
+    return SectionKind::getText();
 
   
   // Handle thread-local data first.
   if (GVar->isThreadLocal()) {
     if (isSuitableForBSS(GVar))
-      return SectionKind::get(SectionKind::ThreadBSS);
-    return SectionKind::get(SectionKind::ThreadData);
+      return SectionKind::getThreadBSS();
+    return SectionKind::getThreadData();
   }
 
   // Variable can be easily put to BSS section.
   if (isSuitableForBSS(GVar))
-    return SectionKind::get(SectionKind::BSS);
+    return SectionKind::getBSS();
 
   Constant *C = GVar->getInitializer();
   
@@ -119,18 +151,32 @@ static SectionKind SectionKindForGlobal(const GlobalValue *GV,
     default: llvm_unreachable("unknown relocation info kind");
     case Constant::NoRelocation:
       // If initializer is a null-terminated string, put it in a "cstring"
-      // section if the target has it.
-      if (isConstantString(C))
-        return SectionKind::get(SectionKind::MergeableCString);
-      
+      // section of the right width.
+      if (const ArrayType *ATy = dyn_cast<ArrayType>(C->getType())) {
+        if (const IntegerType *ITy = 
+              dyn_cast<IntegerType>(ATy->getElementType())) {
+          if ((ITy->getBitWidth() == 8 || ITy->getBitWidth() == 16 ||
+               ITy->getBitWidth() == 32) &&
+              IsNullTerminatedString(C)) {
+            if (ITy->getBitWidth() == 8)
+              return SectionKind::getMergeable1ByteCString();
+            if (ITy->getBitWidth() == 16)
+              return SectionKind::getMergeable2ByteCString();
+                                         
+            assert(ITy->getBitWidth() == 32 && "Unknown width");
+            return SectionKind::getMergeable4ByteCString();
+          }
+        }
+      }
+        
       // Otherwise, just drop it into a mergable constant section.  If we have
       // a section for this size, use it, otherwise use the arbitrary sized
       // mergable section.
       switch (TM.getTargetData()->getTypeAllocSize(C->getType())) {
-      case 4:  return SectionKind::get(SectionKind::MergeableConst4);
-      case 8:  return SectionKind::get(SectionKind::MergeableConst8);
-      case 16: return SectionKind::get(SectionKind::MergeableConst16);
-      default: return SectionKind::get(SectionKind::MergeableConst);
+      case 4:  return SectionKind::getMergeableConst4();
+      case 8:  return SectionKind::getMergeableConst8();
+      case 16: return SectionKind::getMergeableConst16();
+      default: return SectionKind::getMergeableConst();
       }
       
     case Constant::LocalRelocation:
@@ -140,11 +186,11 @@ static SectionKind SectionKindForGlobal(const GlobalValue *GV,
       // the linker doesn't take relocations into consideration when it tries to
       // merge entries in the section.
       if (ReloModel == Reloc::Static)
-        return SectionKind::get(SectionKind::ReadOnly);
+        return SectionKind::getReadOnly();
               
       // Otherwise, the dynamic linker needs to fix it up, put it in the
       // writable data.rel.local section.
-      return SectionKind::get(SectionKind::ReadOnlyWithRelLocal);
+      return SectionKind::getReadOnlyWithRelLocal();
               
     case Constant::GlobalRelocations:
       // In static relocation model, the linker will resolve all addresses, so
@@ -153,11 +199,11 @@ static SectionKind SectionKindForGlobal(const GlobalValue *GV,
       // the linker doesn't take relocations into consideration when it tries to
       // merge entries in the section.
       if (ReloModel == Reloc::Static)
-        return SectionKind::get(SectionKind::ReadOnly);
+        return SectionKind::getReadOnly();
       
       // Otherwise, the dynamic linker needs to fix it up, put it in the
       // writable data.rel section.
-      return SectionKind::get(SectionKind::ReadOnlyWithRel);
+      return SectionKind::getReadOnlyWithRel();
     }
   }
 
@@ -167,16 +213,16 @@ static SectionKind SectionKindForGlobal(const GlobalValue *GV,
   // globals together onto fewer pages, improving the locality of the dynamic
   // linker.
   if (ReloModel == Reloc::Static)
-    return SectionKind::get(SectionKind::DataNoRel);
+    return SectionKind::getDataNoRel();
 
   switch (C->getRelocationInfo()) {
   default: llvm_unreachable("unknown relocation info kind");
   case Constant::NoRelocation:
-    return SectionKind::get(SectionKind::DataNoRel);
+    return SectionKind::getDataNoRel();
   case Constant::LocalRelocation:
-    return SectionKind::get(SectionKind::DataRelLocal);
+    return SectionKind::getDataRelLocal();
   case Constant::GlobalRelocations:
-    return SectionKind::get(SectionKind::DataRel);
+    return SectionKind::getDataRel();
   }
 }
 
@@ -184,13 +230,8 @@ static SectionKind SectionKindForGlobal(const GlobalValue *GV,
 /// the specified global variable or function definition.  This should not
 /// be passed external (or available externally) globals.
 const MCSection *TargetLoweringObjectFile::
-SectionForGlobal(const GlobalValue *GV, Mangler *Mang,
+SectionForGlobal(const GlobalValue *GV, SectionKind Kind, Mangler *Mang,
                  const TargetMachine &TM) const {
-  assert(!GV->isDeclaration() && !GV->hasAvailableExternallyLinkage() &&
-         "Can only be used for global definitions");
-  
-  SectionKind Kind = SectionKindForGlobal(GV, TM);
-  
   // Select section name.
   if (GV->hasSection()) {
     // If the target has special section hacks for specifically named globals,
@@ -211,6 +252,7 @@ SectionForGlobal(const GlobalValue *GV, Mangler *Mang,
   return SelectSectionForGlobal(GV, Kind, Mang, TM);
 }
 
+
 // Lame default implementation. Calculate the section name for global.
 const MCSection *
 TargetLoweringObjectFile::SelectSectionForGlobal(const GlobalValue *GV,
@@ -222,8 +264,8 @@ TargetLoweringObjectFile::SelectSectionForGlobal(const GlobalValue *GV,
   if (Kind.isText())
     return getTextSection();
   
-  if (Kind.isBSS() && BSSSection_ != 0)
-    return BSSSection_;
+  if (Kind.isBSS() && BSSSection != 0)
+    return BSSSection;
   
   if (Kind.isReadOnly() && ReadOnlySection != 0)
     return ReadOnlySection;
@@ -231,12 +273,11 @@ TargetLoweringObjectFile::SelectSectionForGlobal(const GlobalValue *GV,
   return getDataSection();
 }
 
-/// getSectionForMergableConstant - Given a mergable constant with the
+/// getSectionForConstant - Given a mergable constant with the
 /// specified size and relocation information, return a section that it
 /// should be placed in.
 const MCSection *
-TargetLoweringObjectFile::
-getSectionForMergeableConstant(SectionKind Kind) const {
+TargetLoweringObjectFile::getSectionForConstant(SectionKind Kind) const {
   if (Kind.isReadOnly() && ReadOnlySection != 0)
     return ReadOnlySection;
   
@@ -261,50 +302,82 @@ void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx,
                                              const TargetMachine &TM) {
   TargetLoweringObjectFile::Initialize(Ctx, TM);
   if (!HasCrazyBSS)
-    BSSSection_ = getOrCreateSection("\t.bss", true,
-                                     SectionKind::get(SectionKind::BSS));
+    BSSSection = getOrCreateSection("\t.bss", true, SectionKind::getBSS());
   else
     // PPC/Linux doesn't support the .bss directive, it needs .section .bss.
     // FIXME: Does .section .bss work everywhere??
     // FIXME2: this should just be handle by the section printer.  We should get
     // away from syntactic view of the sections and MCSection should just be a
     // semantic view.
-    BSSSection_ = getOrCreateSection("\t.bss", false,
-                                     SectionKind::get(SectionKind::BSS));
+    BSSSection = getOrCreateSection("\t.bss", false, SectionKind::getBSS());
 
     
-  TextSection = getOrCreateSection("\t.text", true,
-                                   SectionKind::get(SectionKind::Text));
-  DataSection = getOrCreateSection("\t.data", true,
-                                   SectionKind::get(SectionKind::DataRel));
+  TextSection = getOrCreateSection("\t.text", true, SectionKind::getText());
+  DataSection = getOrCreateSection("\t.data", true, SectionKind::getDataRel());
   ReadOnlySection =
-    getOrCreateSection("\t.rodata", false,
-                       SectionKind::get(SectionKind::ReadOnly));
+    getOrCreateSection("\t.rodata", false, SectionKind::getReadOnly());
   TLSDataSection =
-    getOrCreateSection("\t.tdata", false,
-                       SectionKind::get(SectionKind::ThreadData));
-  CStringSection_ = getOrCreateSection("\t.rodata.str", true,
-                               SectionKind::get(SectionKind::MergeableCString));
-
+    getOrCreateSection("\t.tdata", false, SectionKind::getThreadData());
+  
   TLSBSSSection = getOrCreateSection("\t.tbss", false, 
-                                     SectionKind::get(SectionKind::ThreadBSS));
+                                     SectionKind::getThreadBSS());
 
   DataRelSection = getOrCreateSection("\t.data.rel", false,
-                                      SectionKind::get(SectionKind::DataRel));
+                                      SectionKind::getDataRel());
   DataRelLocalSection = getOrCreateSection("\t.data.rel.local", false,
-                                   SectionKind::get(SectionKind::DataRelLocal));
+                                   SectionKind::getDataRelLocal());
   DataRelROSection = getOrCreateSection("\t.data.rel.ro", false,
-                                SectionKind::get(SectionKind::ReadOnlyWithRel));
+                                SectionKind::getReadOnlyWithRel());
   DataRelROLocalSection =
     getOrCreateSection("\t.data.rel.ro.local", false,
-                       SectionKind::get(SectionKind::ReadOnlyWithRelLocal));
+                       SectionKind::getReadOnlyWithRelLocal());
     
   MergeableConst4Section = getOrCreateSection(".rodata.cst4", false,
-                                SectionKind::get(SectionKind::MergeableConst4));
+                                SectionKind::getMergeableConst4());
   MergeableConst8Section = getOrCreateSection(".rodata.cst8", false,
-                                SectionKind::get(SectionKind::MergeableConst8));
+                                SectionKind::getMergeableConst8());
   MergeableConst16Section = getOrCreateSection(".rodata.cst16", false,
-                               SectionKind::get(SectionKind::MergeableConst16));
+                               SectionKind::getMergeableConst16());
+
+  StaticCtorSection =
+    getOrCreateSection(".ctors", false, SectionKind::getDataRel());
+  StaticDtorSection =
+    getOrCreateSection(".dtors", false, SectionKind::getDataRel());
+  
+  // Exception Handling Sections.
+  
+  // FIXME: We're emitting LSDA info into a readonly section on ELF, even though
+  // it contains relocatable pointers.  In PIC mode, this is probably a big
+  // runtime hit for C++ apps.  Either the contents of the LSDA need to be
+  // adjusted or this should be a data section.
+  LSDASection =
+    getOrCreateSection(".gcc_except_table", false, SectionKind::getReadOnly());
+  EHFrameSection =
+    getOrCreateSection(".eh_frame", false, SectionKind::getDataRel());
+  
+  // Debug Info Sections.
+  DwarfAbbrevSection = 
+    getOrCreateSection(".debug_abbrev", false, SectionKind::getMetadata());
+  DwarfInfoSection = 
+    getOrCreateSection(".debug_info", false, SectionKind::getMetadata());
+  DwarfLineSection = 
+    getOrCreateSection(".debug_line", false, SectionKind::getMetadata());
+  DwarfFrameSection = 
+    getOrCreateSection(".debug_frame", false, SectionKind::getMetadata());
+  DwarfPubNamesSection = 
+    getOrCreateSection(".debug_pubnames", false, SectionKind::getMetadata());
+  DwarfPubTypesSection = 
+    getOrCreateSection(".debug_pubtypes", false, SectionKind::getMetadata());
+  DwarfStrSection = 
+    getOrCreateSection(".debug_str", false, SectionKind::getMetadata());
+  DwarfLocSection = 
+    getOrCreateSection(".debug_loc", false, SectionKind::getMetadata());
+  DwarfARangesSection = 
+    getOrCreateSection(".debug_aranges", false, SectionKind::getMetadata());
+  DwarfRangesSection = 
+    getOrCreateSection(".debug_ranges", false, SectionKind::getMetadata());
+  DwarfMacroInfoSection = 
+    getOrCreateSection(".debug_macinfo", false, SectionKind::getMetadata());
 }
 
 
@@ -317,19 +390,19 @@ getKindForNamedSection(const char *Name, SectionKind K) const {
       strncmp(Name, ".llvm.linkonce.b.", 17) == 0 ||
       strncmp(Name, ".gnu.linkonce.sb.", 17) == 0 ||
       strncmp(Name, ".llvm.linkonce.sb.", 18) == 0)
-    return SectionKind::get(SectionKind::BSS);
+    return SectionKind::getBSS();
   
   if (strcmp(Name, ".tdata") == 0 ||
       strncmp(Name, ".tdata.", 7) == 0 ||
       strncmp(Name, ".gnu.linkonce.td.", 17) == 0 ||
       strncmp(Name, ".llvm.linkonce.td.", 18) == 0)
-    return SectionKind::get(SectionKind::ThreadData);
+    return SectionKind::getThreadData();
   
   if (strcmp(Name, ".tbss") == 0 ||
       strncmp(Name, ".tbss.", 6) == 0 ||
       strncmp(Name, ".gnu.linkonce.tb.", 17) == 0 ||
       strncmp(Name, ".llvm.linkonce.tb.", 18) == 0)
-    return SectionKind::get(SectionKind::ThreadBSS);
+    return SectionKind::getThreadBSS();
   
   return K;
 }
@@ -345,12 +418,16 @@ getSectionFlagsAsString(SectionKind Kind, SmallVectorImpl<char> &Str) const {
     Str.push_back('x');
   if (Kind.isWriteable())
     Str.push_back('w');
-  if (Kind.isMergeableCString() ||
+  if (Kind.isMergeable1ByteCString() ||
+      Kind.isMergeable2ByteCString() ||
+      Kind.isMergeable4ByteCString() ||
       Kind.isMergeableConst4() ||
       Kind.isMergeableConst8() ||
       Kind.isMergeableConst16())
     Str.push_back('M');
-  if (Kind.isMergeableCString())
+  if (Kind.isMergeable1ByteCString() ||
+      Kind.isMergeable2ByteCString() ||
+      Kind.isMergeable4ByteCString())
     Str.push_back('S');
   if (Kind.isThreadLocal())
     Str.push_back('T');
@@ -372,11 +449,15 @@ getSectionFlagsAsString(SectionKind Kind, SmallVectorImpl<char> &Str) const {
   
   Str.append(KindStr, KindStr+strlen(KindStr));
   
-  if (Kind.isMergeableCString()) {
-    // TODO: Eventually handle multiple byte character strings.  For now, all
-    // mergable C strings are single byte.
+  if (Kind.isMergeable1ByteCString()) {
     Str.push_back(',');
     Str.push_back('1');
+  } else if (Kind.isMergeable2ByteCString()) {
+    Str.push_back(',');
+    Str.push_back('2');
+  } else if (Kind.isMergeable4ByteCString()) {
+    Str.push_back(',');
+    Str.push_back('4');
   } else if (Kind.isMergeableConst4()) {
     Str.push_back(',');
     Str.push_back('4');
@@ -422,8 +503,9 @@ SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
   
   if (Kind.isText()) return TextSection;
   
-  if (Kind.isMergeableCString()) {
-   assert(CStringSection_ && "Should have string section prefix");
+  if (Kind.isMergeable1ByteCString() ||
+      Kind.isMergeable2ByteCString() ||
+      Kind.isMergeable4ByteCString()) {
     
     // We also need alignment here.
     // FIXME: this is getting the alignment of the character, not the
@@ -431,9 +513,17 @@ SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
     unsigned Align = 
       TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV));
     
-    std::string Name = CStringSection_->getName() + "1." + utostr(Align);
-    return getOrCreateSection(Name.c_str(), false,
-                              SectionKind::get(SectionKind::MergeableCString));
+    const char *SizeSpec = ".rodata.str1.";
+    if (Kind.isMergeable2ByteCString())
+      SizeSpec = ".rodata.str2.";
+    else if (Kind.isMergeable4ByteCString())
+      SizeSpec = ".rodata.str4.";
+    else
+      assert(Kind.isMergeable1ByteCString() && "unknown string width");
+    
+    
+    std::string Name = SizeSpec + utostr(Align);
+    return getOrCreateSection(Name.c_str(), false, Kind);
   }
   
   if (Kind.isMergeableConst()) {
@@ -451,7 +541,7 @@ SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
   if (Kind.isThreadData())           return TLSDataSection;
   if (Kind.isThreadBSS())            return TLSBSSSection;
   
-  if (Kind.isBSS())                  return BSSSection_;
+  if (Kind.isBSS())                  return BSSSection;
   
   if (Kind.isDataNoRel())            return DataSection;
   if (Kind.isDataRelLocal())         return DataRelLocalSection;
@@ -462,11 +552,11 @@ SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
   return DataRelROSection;
 }
 
-/// getSectionForMergeableConstant - Given a mergeable constant with the
+/// getSectionForConstant - Given a mergeable constant with the
 /// specified size and relocation information, return a section that it
 /// should be placed in.
 const MCSection *TargetLoweringObjectFileELF::
-getSectionForMergeableConstant(SectionKind Kind) const {
+getSectionForConstant(SectionKind Kind) const {
   if (Kind.isMergeableConst4())
     return MergeableConst4Section;
   if (Kind.isMergeableConst8())
@@ -485,20 +575,30 @@ getSectionForMergeableConstant(SectionKind Kind) const {
 //                                 MachO
 //===----------------------------------------------------------------------===//
 
+const MCSection *TargetLoweringObjectFileMachO::
+getMachOSection(const char *Name, bool isDirective, SectionKind K) {
+  // FOR NOW, Just forward.
+  return getOrCreateSection(Name, isDirective, K);  
+}
+
+
+
 void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
                                                const TargetMachine &TM) {
   TargetLoweringObjectFile::Initialize(Ctx, TM);
   TextSection = getOrCreateSection("\t.text", true,
-                                   SectionKind::get(SectionKind::Text));
+                                   SectionKind::getText());
   DataSection = getOrCreateSection("\t.data", true, 
-                                   SectionKind::get(SectionKind::DataRel));
+                                   SectionKind::getDataRel());
   
-  CStringSection_ = getOrCreateSection("\t.cstring", true,
-                               SectionKind::get(SectionKind::MergeableCString));
+  CStringSection = getOrCreateSection("\t.cstring", true,
+                                      SectionKind::getMergeable1ByteCString());
+  UStringSection = getOrCreateSection("__TEXT,__ustring", false,
+                                      SectionKind::getMergeable2ByteCString());
   FourByteConstantSection = getOrCreateSection("\t.literal4\n", true,
-                                SectionKind::get(SectionKind::MergeableConst4));
+                                             SectionKind::getMergeableConst4());
   EightByteConstantSection = getOrCreateSection("\t.literal8\n", true,
-                                SectionKind::get(SectionKind::MergeableConst8));
+                                             SectionKind::getMergeableConst8());
   
   // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
   // to using it in -static mode.
@@ -506,27 +606,86 @@ void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
       TM.getTargetData()->getPointerSize() == 32)
     SixteenByteConstantSection = 
       getOrCreateSection("\t.literal16\n", true, 
-                         SectionKind::get(SectionKind::MergeableConst16));
+                         SectionKind::getMergeableConst16());
   else
     SixteenByteConstantSection = 0;
   
   ReadOnlySection = getOrCreateSection("\t.const", true, 
-                                       SectionKind::get(SectionKind::ReadOnly));
+                                       SectionKind::getReadOnly());
   
   TextCoalSection =
   getOrCreateSection("\t__TEXT,__textcoal_nt,coalesced,pure_instructions",
-                     false, SectionKind::get(SectionKind::Text));
+                     false, SectionKind::getText());
   ConstTextCoalSection = getOrCreateSection("\t__TEXT,__const_coal,coalesced",
                                             false,
-                                           SectionKind::get(SectionKind::Text));
+                                           SectionKind::getText());
   ConstDataCoalSection = getOrCreateSection("\t__DATA,__const_coal,coalesced",
                                             false, 
-                                          SectionKind::get(SectionKind::Text));
+                                          SectionKind::getText());
   ConstDataSection = getOrCreateSection("\t.const_data", true,
-                                SectionKind::get(SectionKind::ReadOnlyWithRel));
+                                SectionKind::getReadOnlyWithRel());
   DataCoalSection = getOrCreateSection("\t__DATA,__datacoal_nt,coalesced",
                                        false,
-                                       SectionKind::get(SectionKind::DataRel));
+                                       SectionKind::getDataRel());
+
+  if (TM.getRelocationModel() == Reloc::Static) {
+    StaticCtorSection =
+      getOrCreateSection(".constructor", true, SectionKind::getDataRel());
+    StaticDtorSection =
+      getOrCreateSection(".destructor", true, SectionKind::getDataRel());
+  } else {
+    StaticCtorSection =
+      getOrCreateSection(".mod_init_func", true, SectionKind::getDataRel());
+    StaticDtorSection =
+      getOrCreateSection(".mod_term_func", true, SectionKind::getDataRel());
+  }
+  
+  // Exception Handling.
+  LSDASection = getOrCreateSection("__DATA,__gcc_except_tab", false,
+                                   SectionKind::getDataRel());
+  EHFrameSection =
+    getOrCreateSection("__TEXT,__eh_frame,coalesced,no_toc+strip_static_syms"
+                       "+live_support", false, SectionKind::getReadOnly());
+
+  // Debug Information.
+  // FIXME: Don't use 'directive' syntax: need flags for debug/regular??
+  // FIXME: Need __DWARF segment.
+  DwarfAbbrevSection = 
+    getOrCreateSection(".section __DWARF,__debug_abbrev,regular,debug", true,
+                       SectionKind::getMetadata());
+  DwarfInfoSection =  
+    getOrCreateSection(".section __DWARF,__debug_info,regular,debug", true,
+                       SectionKind::getMetadata());
+  DwarfLineSection =  
+    getOrCreateSection(".section __DWARF,__debug_line,regular,debug", true,
+                       SectionKind::getMetadata());
+  DwarfFrameSection =  
+    getOrCreateSection(".section __DWARF,__debug_frame,regular,debug", true,
+                       SectionKind::getMetadata());
+  DwarfPubNamesSection =  
+    getOrCreateSection(".section __DWARF,__debug_pubnames,regular,debug", true,
+                       SectionKind::getMetadata());
+  DwarfPubTypesSection =  
+    getOrCreateSection(".section __DWARF,__debug_pubtypes,regular,debug", true,
+                       SectionKind::getMetadata());
+  DwarfStrSection =  
+    getOrCreateSection(".section __DWARF,__debug_str,regular,debug", true,
+                       SectionKind::getMetadata());
+  DwarfLocSection =  
+    getOrCreateSection(".section __DWARF,__debug_loc,regular,debug", true,
+                       SectionKind::getMetadata());
+  DwarfARangesSection =  
+    getOrCreateSection(".section __DWARF,__debug_aranges,regular,debug", true,
+                       SectionKind::getMetadata());
+  DwarfRangesSection =  
+    getOrCreateSection(".section __DWARF,__debug_ranges,regular,debug", true,
+                       SectionKind::getMetadata());
+  DwarfMacroInfoSection =  
+    getOrCreateSection(".section __DWARF,__debug_macinfo,regular,debug", true,
+                       SectionKind::getMetadata());
+  DwarfDebugInlineSection = 
+    getOrCreateSection(".section __DWARF,__debug_inlined,regular,debug", true,
+                       SectionKind::getMetadata());
 }
 
 const MCSection *TargetLoweringObjectFileMachO::
@@ -546,18 +705,15 @@ SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
   }
   
   // FIXME: Alignment check should be handled by section classifier.
-  if (Kind.isMergeableCString()) {
-    Constant *C = cast<GlobalVariable>(GV)->getInitializer();
-    const Type *Ty = cast<ArrayType>(C->getType())->getElementType();
-    const TargetData &TD = *TM.getTargetData();
-    unsigned Size = TD.getTypeAllocSize(Ty);
-    if (Size) {
-      unsigned Align = TD.getPreferredAlignment(cast<GlobalVariable>(GV));
-      if (Align <= 32)
-        return CStringSection_;
+  if (Kind.isMergeable1ByteCString() ||
+      Kind.isMergeable2ByteCString()) {
+    if (TM.getTargetData()->getPreferredAlignment(
+                                              cast<GlobalVariable>(GV)) < 32) {
+      if (Kind.isMergeable1ByteCString())
+        return CStringSection;
+      assert(Kind.isMergeable2ByteCString());
+      return UStringSection;
     }
-    
-    return ReadOnlySection;
   }
   
   if (Kind.isMergeableConst()) {
@@ -567,11 +723,10 @@ SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
       return EightByteConstantSection;
     if (Kind.isMergeableConst16() && SixteenByteConstantSection)
       return SixteenByteConstantSection;
-    return ReadOnlySection;  // .const
   }
-  
-  // FIXME: ROData -> const in -static mode that is relocatable but they happen
-  // by the static linker.  Why not mergeable?
+
+  // Otherwise, if it is readonly, but not something we can specially optimize,
+  // just drop it in .const.
   if (Kind.isReadOnly())
     return ReadOnlySection;
 
@@ -585,8 +740,7 @@ SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
 }
 
 const MCSection *
-TargetLoweringObjectFileMachO::
-getSectionForMergeableConstant(SectionKind Kind) const {
+TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
   // If this constant requires a relocation, we have to put it in the data
   // segment, not in the text segment.
   if (Kind.isDataRel())
@@ -628,13 +782,59 @@ shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
 //                                  COFF
 //===----------------------------------------------------------------------===//
 
+const MCSection *TargetLoweringObjectFileCOFF::
+getCOFFSection(const char *Name, bool isDirective, SectionKind K) {
+  return getOrCreateSection(Name, isDirective, K);
+}
+
 void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
                                               const TargetMachine &TM) {
   TargetLoweringObjectFile::Initialize(Ctx, TM);
   TextSection = getOrCreateSection("\t.text", true,
-                                   SectionKind::get(SectionKind::Text));
+                                   SectionKind::getText());
   DataSection = getOrCreateSection("\t.data", true,
-                                   SectionKind::get(SectionKind::DataRel));
+                                   SectionKind::getDataRel());
+  StaticCtorSection =
+    getOrCreateSection(".ctors", false, SectionKind::getDataRel());
+  StaticDtorSection =
+    getOrCreateSection(".dtors", false, SectionKind::getDataRel());
+  
+  
+  // Debug info.
+  // FIXME: Don't use 'directive' mode here.
+  DwarfAbbrevSection =  
+    getOrCreateSection("\t.section\t.debug_abbrev,\"dr\"",
+                       true, SectionKind::getMetadata());
+  DwarfInfoSection =    
+    getOrCreateSection("\t.section\t.debug_info,\"dr\"",
+                       true, SectionKind::getMetadata());
+  DwarfLineSection =    
+    getOrCreateSection("\t.section\t.debug_line,\"dr\"",
+                       true, SectionKind::getMetadata());
+  DwarfFrameSection =   
+    getOrCreateSection("\t.section\t.debug_frame,\"dr\"",
+                       true, SectionKind::getMetadata());
+  DwarfPubNamesSection =
+    getOrCreateSection("\t.section\t.debug_pubnames,\"dr\"",
+                       true, SectionKind::getMetadata());
+  DwarfPubTypesSection =
+    getOrCreateSection("\t.section\t.debug_pubtypes,\"dr\"",
+                       true, SectionKind::getMetadata());
+  DwarfStrSection =     
+    getOrCreateSection("\t.section\t.debug_str,\"dr\"",
+                       true, SectionKind::getMetadata());
+  DwarfLocSection =     
+    getOrCreateSection("\t.section\t.debug_loc,\"dr\"",
+                       true, SectionKind::getMetadata());
+  DwarfARangesSection = 
+    getOrCreateSection("\t.section\t.debug_aranges,\"dr\"",
+                       true, SectionKind::getMetadata());
+  DwarfRangesSection =  
+    getOrCreateSection("\t.section\t.debug_ranges,\"dr\"",
+                       true, SectionKind::getMetadata());
+  DwarfMacroInfoSection = 
+    getOrCreateSection("\t.section\t.debug_macinfo,\"dr\"",
+                       true, SectionKind::getMetadata());
 }
 
 void TargetLoweringObjectFileCOFF::
@@ -675,9 +875,6 @@ SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
   if (Kind.isText())
     return getTextSection();
   
-  if (Kind.isReadOnly() && ReadOnlySection != 0)
-    return ReadOnlySection;
-  
   return getDataSection();
 }