TargetLoweringObjectFile.cpp revision b808588a3a5febe931896b3779d159ba90d836f7
1f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner//===-- llvm/Target/TargetLoweringObjectFile.cpp - Object File Info -------===//
2f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner//
3f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner//                     The LLVM Compiler Infrastructure
4f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner//
5f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner// This file is distributed under the University of Illinois Open Source
6f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner// License. See LICENSE.TXT for details.
7f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner//
8f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner//===----------------------------------------------------------------------===//
9f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner//
10f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner// This file implements classes used to handle lowerings specific to common
11f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner// object file formats.
12f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner//
13f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner//===----------------------------------------------------------------------===//
14f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
15f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner#include "llvm/Target/TargetLoweringObjectFile.h"
16f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner#include "llvm/Constants.h"
17f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner#include "llvm/DerivedTypes.h"
18ffef8acc3e3398bdd04e947c7949befdd52faf86Dan Gohman#include "llvm/Function.h"
19f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner#include "llvm/GlobalVariable.h"
20a87dea4f8c546ca748f1777a8d1cabcc06515d91Chris Lattner#include "llvm/MC/MCContext.h"
21f9bdeddb96043559c61f176f8077e3b91a0c544fChris Lattner#include "llvm/MC/MCSectionMachO.h"
22b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes#include "llvm/MC/MCSectionELF.h"
23f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner#include "llvm/Target/TargetData.h"
245277b22687d3513dd29d5a9c8510cac740f933f6Chris Lattner#include "llvm/Target/TargetMachine.h"
25f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner#include "llvm/Target/TargetOptions.h"
26a87dea4f8c546ca748f1777a8d1cabcc06515d91Chris Lattner#include "llvm/Support/Mangler.h"
275dc47ff03975b9adde9dd833db2b646eb4295710Chris Lattner#include "llvm/ADT/SmallString.h"
28f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner#include "llvm/ADT/StringExtras.h"
29f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattnerusing namespace llvm;
30f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
31f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner//===----------------------------------------------------------------------===//
32f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner//                              Generic Code
33f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner//===----------------------------------------------------------------------===//
34f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
35a87dea4f8c546ca748f1777a8d1cabcc06515d91Chris LattnerTargetLoweringObjectFile::TargetLoweringObjectFile() : Ctx(0) {
36f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  TextSection = 0;
37f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  DataSection = 0;
38824583844a8f334dd261894a3fac7ad476531667Chris Lattner  BSSSection = 0;
39f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  ReadOnlySection = 0;
4080ec2792b2b271eca55743a3cc4c8bca214fa705Chris Lattner  StaticCtorSection = 0;
4180ec2792b2b271eca55743a3cc4c8bca214fa705Chris Lattner  StaticDtorSection = 0;
42d5bbb07ec806e6fa1e804afd7073987fdacc83e4Chris Lattner  LSDASection = 0;
4335039ac24163e99cfab161620a9fb41f944a63d5Chris Lattner  EHFrameSection = 0;
4418a4c16726db2b8874c7b84d04650dda80746074Chris Lattner
4518a4c16726db2b8874c7b84d04650dda80746074Chris Lattner  DwarfAbbrevSection = 0;
4618a4c16726db2b8874c7b84d04650dda80746074Chris Lattner  DwarfInfoSection = 0;
4718a4c16726db2b8874c7b84d04650dda80746074Chris Lattner  DwarfLineSection = 0;
4818a4c16726db2b8874c7b84d04650dda80746074Chris Lattner  DwarfFrameSection = 0;
4918a4c16726db2b8874c7b84d04650dda80746074Chris Lattner  DwarfPubNamesSection = 0;
5018a4c16726db2b8874c7b84d04650dda80746074Chris Lattner  DwarfPubTypesSection = 0;
5118a4c16726db2b8874c7b84d04650dda80746074Chris Lattner  DwarfDebugInlineSection = 0;
5218a4c16726db2b8874c7b84d04650dda80746074Chris Lattner  DwarfStrSection = 0;
5318a4c16726db2b8874c7b84d04650dda80746074Chris Lattner  DwarfLocSection = 0;
5418a4c16726db2b8874c7b84d04650dda80746074Chris Lattner  DwarfARangesSection = 0;
5518a4c16726db2b8874c7b84d04650dda80746074Chris Lattner  DwarfRangesSection = 0;
5618a4c16726db2b8874c7b84d04650dda80746074Chris Lattner  DwarfMacroInfoSection = 0;
57f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner}
58f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
59f0144127b98425d214e59e4a1a4b342b78e3642bChris LattnerTargetLoweringObjectFile::~TargetLoweringObjectFile() {
60f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner}
61f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
62f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattnerstatic bool isSuitableForBSS(const GlobalVariable *GV) {
63f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  Constant *C = GV->getInitializer();
64f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
65f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  // Must have zero initializer.
66f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  if (!C->isNullValue())
67f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner    return false;
68f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
69f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  // Leave constant zeros in readonly constant sections, so they can be shared.
70f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  if (GV->isConstant())
71f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner    return false;
72f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
73f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  // If the global has an explicit section specified, don't put it in BSS.
74f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  if (!GV->getSection().empty())
75f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner    return false;
76f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
77f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  // If -nozero-initialized-in-bss is specified, don't ever use BSS.
78f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  if (NoZerosInBSS)
79f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner    return false;
80f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
81f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  // Otherwise, put it in BSS!
82f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  return true;
83f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner}
84f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
851850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattner/// IsNullTerminatedString - Return true if the specified constant (which is
861850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattner/// known to have a type that is an array of 1/2/4 byte elements) ends with a
871850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattner/// nul value and contains no other nuls in it.
881850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattnerstatic bool IsNullTerminatedString(const Constant *C) {
891850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattner  const ArrayType *ATy = cast<ArrayType>(C->getType());
901850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattner
91f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  // First check: is we have constant array of i8 terminated with zero
921850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattner  if (const ConstantArray *CVA = dyn_cast<ConstantArray>(C)) {
931850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattner    if (ATy->getNumElements() == 0) return false;
941850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattner
951850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattner    ConstantInt *Null =
961850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattner      dyn_cast<ConstantInt>(CVA->getOperand(ATy->getNumElements()-1));
971850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattner    if (Null == 0 || Null->getZExtValue() != 0)
981850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattner      return false; // Not null terminated.
991850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattner
1001850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattner    // Verify that the null doesn't occur anywhere else in the string.
1011850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattner    for (unsigned i = 0, e = ATy->getNumElements()-1; i != e; ++i)
1021850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattner      // Reject constantexpr elements etc.
1031850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattner      if (!isa<ConstantInt>(CVA->getOperand(i)) ||
1041850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattner          CVA->getOperand(i) == Null)
1051850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattner        return false;
106f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner    return true;
1071850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattner  }
108f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
109f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  // Another possibility: [1 x i8] zeroinitializer
110f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  if (isa<ConstantAggregateZero>(C))
1111850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattner    return ATy->getNumElements() == 1;
112f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
113f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  return false;
114f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner}
115f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
11658bed8fc29b6e55e7014dcb537808043c946cd73Chris Lattner/// getKindForGlobal - This is a top-level target-independent classifier for
117968ff1196768c0b6dbcc5508025a2923bfa73fabChris Lattner/// a global variable.  Given an global variable and information from TM, it
118968ff1196768c0b6dbcc5508025a2923bfa73fabChris Lattner/// classifies the global in a variety of ways that make various target
119968ff1196768c0b6dbcc5508025a2923bfa73fabChris Lattner/// implementations simpler.  The target implementation is free to ignore this
120968ff1196768c0b6dbcc5508025a2923bfa73fabChris Lattner/// extra info of course.
12158bed8fc29b6e55e7014dcb537808043c946cd73Chris LattnerSectionKind TargetLoweringObjectFile::getKindForGlobal(const GlobalValue *GV,
12258bed8fc29b6e55e7014dcb537808043c946cd73Chris Lattner                                                       const TargetMachine &TM){
12358bed8fc29b6e55e7014dcb537808043c946cd73Chris Lattner  assert(!GV->isDeclaration() && !GV->hasAvailableExternallyLinkage() &&
12458bed8fc29b6e55e7014dcb537808043c946cd73Chris Lattner         "Can only be used for global definitions");
12558bed8fc29b6e55e7014dcb537808043c946cd73Chris Lattner
126f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  Reloc::Model ReloModel = TM.getRelocationModel();
127f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
128f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  // Early exit - functions should be always in text sections.
129f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
130f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  if (GVar == 0)
1312798119ab4d7e0b42812b3acdf37821f40dee627Chris Lattner    return SectionKind::getText();
132f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
133f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  // Handle thread-local data first.
134f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  if (GVar->isThreadLocal()) {
135f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner    if (isSuitableForBSS(GVar))
1362798119ab4d7e0b42812b3acdf37821f40dee627Chris Lattner      return SectionKind::getThreadBSS();
1372798119ab4d7e0b42812b3acdf37821f40dee627Chris Lattner    return SectionKind::getThreadData();
138f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  }
139f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
140f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  // Variable can be easily put to BSS section.
141f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  if (isSuitableForBSS(GVar))
1422798119ab4d7e0b42812b3acdf37821f40dee627Chris Lattner    return SectionKind::getBSS();
143f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
144f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  Constant *C = GVar->getInitializer();
145f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
146f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  // If the global is marked constant, we can put it into a mergable section,
147f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  // a mergable string section, or general .data if it contains relocations.
148f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  if (GVar->isConstant()) {
149f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner    // If the initializer for the global contains something that requires a
150f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner    // relocation, then we may have to drop this into a wriable data section
151f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner    // even though it is marked const.
152f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner    switch (C->getRelocationInfo()) {
153f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner    default: llvm_unreachable("unknown relocation info kind");
154f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner    case Constant::NoRelocation:
155f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner      // If initializer is a null-terminated string, put it in a "cstring"
1561850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattner      // section of the right width.
1571850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattner      if (const ArrayType *ATy = dyn_cast<ArrayType>(C->getType())) {
1581850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattner        if (const IntegerType *ITy =
1591850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattner              dyn_cast<IntegerType>(ATy->getElementType())) {
1601850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattner          if ((ITy->getBitWidth() == 8 || ITy->getBitWidth() == 16 ||
1611850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattner               ITy->getBitWidth() == 32) &&
1621850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattner              IsNullTerminatedString(C)) {
1631850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattner            if (ITy->getBitWidth() == 8)
1641850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattner              return SectionKind::getMergeable1ByteCString();
1651850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattner            if (ITy->getBitWidth() == 16)
1661850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattner              return SectionKind::getMergeable2ByteCString();
1671850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattner
1681850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattner            assert(ITy->getBitWidth() == 32 && "Unknown width");
1691850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattner            return SectionKind::getMergeable4ByteCString();
1701850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattner          }
1711850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattner        }
1721850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattner      }
1733b24c0172069a2546cd095e4b91f8b88c1ea0722Chris Lattner
174f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner      // Otherwise, just drop it into a mergable constant section.  If we have
175f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner      // a section for this size, use it, otherwise use the arbitrary sized
176f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner      // mergable section.
177f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner      switch (TM.getTargetData()->getTypeAllocSize(C->getType())) {
1782798119ab4d7e0b42812b3acdf37821f40dee627Chris Lattner      case 4:  return SectionKind::getMergeableConst4();
1792798119ab4d7e0b42812b3acdf37821f40dee627Chris Lattner      case 8:  return SectionKind::getMergeableConst8();
1802798119ab4d7e0b42812b3acdf37821f40dee627Chris Lattner      case 16: return SectionKind::getMergeableConst16();
1812798119ab4d7e0b42812b3acdf37821f40dee627Chris Lattner      default: return SectionKind::getMergeableConst();
182f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner      }
183f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
184f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner    case Constant::LocalRelocation:
185f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner      // In static relocation model, the linker will resolve all addresses, so
186f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner      // the relocation entries will actually be constants by the time the app
187f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner      // starts up.  However, we can't put this into a mergable section, because
188f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner      // the linker doesn't take relocations into consideration when it tries to
189f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner      // merge entries in the section.
190f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner      if (ReloModel == Reloc::Static)
1912798119ab4d7e0b42812b3acdf37821f40dee627Chris Lattner        return SectionKind::getReadOnly();
192f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
193f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner      // Otherwise, the dynamic linker needs to fix it up, put it in the
194f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner      // writable data.rel.local section.
1952798119ab4d7e0b42812b3acdf37821f40dee627Chris Lattner      return SectionKind::getReadOnlyWithRelLocal();
196f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
197f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner    case Constant::GlobalRelocations:
198f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner      // In static relocation model, the linker will resolve all addresses, so
199f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner      // the relocation entries will actually be constants by the time the app
200f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner      // starts up.  However, we can't put this into a mergable section, because
201f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner      // the linker doesn't take relocations into consideration when it tries to
202f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner      // merge entries in the section.
203f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner      if (ReloModel == Reloc::Static)
2042798119ab4d7e0b42812b3acdf37821f40dee627Chris Lattner        return SectionKind::getReadOnly();
205f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
206f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner      // Otherwise, the dynamic linker needs to fix it up, put it in the
207f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner      // writable data.rel section.
2082798119ab4d7e0b42812b3acdf37821f40dee627Chris Lattner      return SectionKind::getReadOnlyWithRel();
209f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner    }
210f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  }
211f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
212f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  // Okay, this isn't a constant.  If the initializer for the global is going
213f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  // to require a runtime relocation by the dynamic linker, put it into a more
214f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  // specific section to improve startup time of the app.  This coalesces these
215f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  // globals together onto fewer pages, improving the locality of the dynamic
216f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  // linker.
217f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  if (ReloModel == Reloc::Static)
2182798119ab4d7e0b42812b3acdf37821f40dee627Chris Lattner    return SectionKind::getDataNoRel();
219f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
220f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  switch (C->getRelocationInfo()) {
221f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  default: llvm_unreachable("unknown relocation info kind");
222f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  case Constant::NoRelocation:
2232798119ab4d7e0b42812b3acdf37821f40dee627Chris Lattner    return SectionKind::getDataNoRel();
224f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  case Constant::LocalRelocation:
2252798119ab4d7e0b42812b3acdf37821f40dee627Chris Lattner    return SectionKind::getDataRelLocal();
226f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  case Constant::GlobalRelocations:
2272798119ab4d7e0b42812b3acdf37821f40dee627Chris Lattner    return SectionKind::getDataRel();
228f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  }
229f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner}
230f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
231f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner/// SectionForGlobal - This method computes the appropriate section to emit
232f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner/// the specified global variable or function definition.  This should not
233f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner/// be passed external (or available externally) globals.
234a87dea4f8c546ca748f1777a8d1cabcc06515d91Chris Lattnerconst MCSection *TargetLoweringObjectFile::
23558bed8fc29b6e55e7014dcb537808043c946cd73Chris LattnerSectionForGlobal(const GlobalValue *GV, SectionKind Kind, Mangler *Mang,
236e53a600f065075731d0aeb9dc8f4f3d75f5a05f8Chris Lattner                 const TargetMachine &TM) const {
237f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  // Select section name.
23824f654c8a4d14066233480f683d3b0dececf374aChris Lattner  if (GV->hasSection())
23924f654c8a4d14066233480f683d3b0dececf374aChris Lattner    return getExplicitSectionGlobal(GV, Kind, Mang, TM);
24024f654c8a4d14066233480f683d3b0dececf374aChris Lattner
241f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
242f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  // Use default section depending on the 'type' of global
243f9650c061ee89ac55740555530ca5c2842c28738Chris Lattner  return SelectSectionForGlobal(GV, Kind, Mang, TM);
244f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner}
245f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
24658bed8fc29b6e55e7014dcb537808043c946cd73Chris Lattner
247f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner// Lame default implementation. Calculate the section name for global.
248a87dea4f8c546ca748f1777a8d1cabcc06515d91Chris Lattnerconst MCSection *
249f0144127b98425d214e59e4a1a4b342b78e3642bChris LattnerTargetLoweringObjectFile::SelectSectionForGlobal(const GlobalValue *GV,
250f9650c061ee89ac55740555530ca5c2842c28738Chris Lattner                                                 SectionKind Kind,
251e53a600f065075731d0aeb9dc8f4f3d75f5a05f8Chris Lattner                                                 Mangler *Mang,
252f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner                                                 const TargetMachine &TM) const{
253f9650c061ee89ac55740555530ca5c2842c28738Chris Lattner  assert(!Kind.isThreadLocal() && "Doesn't support TLS");
254f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
255f9650c061ee89ac55740555530ca5c2842c28738Chris Lattner  if (Kind.isText())
256f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner    return getTextSection();
257f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
258824583844a8f334dd261894a3fac7ad476531667Chris Lattner  if (Kind.isBSS() && BSSSection != 0)
259824583844a8f334dd261894a3fac7ad476531667Chris Lattner    return BSSSection;
260f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
261f9650c061ee89ac55740555530ca5c2842c28738Chris Lattner  if (Kind.isReadOnly() && ReadOnlySection != 0)
262f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner    return ReadOnlySection;
263f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
264f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  return getDataSection();
265f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner}
266f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
26783d77faf6e8fc2c1c2377d037283dc162d8667a1Chris Lattner/// getSectionForConstant - Given a mergable constant with the
268f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner/// specified size and relocation information, return a section that it
269f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner/// should be placed in.
270a87dea4f8c546ca748f1777a8d1cabcc06515d91Chris Lattnerconst MCSection *
27183d77faf6e8fc2c1c2377d037283dc162d8667a1Chris LattnerTargetLoweringObjectFile::getSectionForConstant(SectionKind Kind) const {
272f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  if (Kind.isReadOnly() && ReadOnlySection != 0)
273f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner    return ReadOnlySection;
274f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
275f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  return DataSection;
276f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner}
277f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
278f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
279f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
280f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner//===----------------------------------------------------------------------===//
281f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner//                                  ELF
282f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner//===----------------------------------------------------------------------===//
28338cff389af1d78bd80df0479ef258493e0c5897eChris Lattnertypedef StringMap<const MCSectionELF*> ELFUniqueMapTy;
28438cff389af1d78bd80df0479ef258493e0c5897eChris Lattner
28538cff389af1d78bd80df0479ef258493e0c5897eChris LattnerTargetLoweringObjectFileELF::~TargetLoweringObjectFileELF() {
28638cff389af1d78bd80df0479ef258493e0c5897eChris Lattner  // If we have the section uniquing map, free it.
28738cff389af1d78bd80df0479ef258493e0c5897eChris Lattner  delete (ELFUniqueMapTy*)UniquingMap;
28838cff389af1d78bd80df0479ef258493e0c5897eChris Lattner}
289f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
290fbf1d271e6a7157c1b5432e84d5633f63869b5a8Chris Lattnerconst MCSection *TargetLoweringObjectFileELF::
291b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso LopesgetELFSection(StringRef Section, unsigned Type, unsigned Flags,
292b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes              SectionKind Kind, bool IsExplicit) const {
29338cff389af1d78bd80df0479ef258493e0c5897eChris Lattner  if (UniquingMap == 0)
29438cff389af1d78bd80df0479ef258493e0c5897eChris Lattner    UniquingMap = new ELFUniqueMapTy();
29538cff389af1d78bd80df0479ef258493e0c5897eChris Lattner  ELFUniqueMapTy &Map = *(ELFUniqueMapTy*)UniquingMap;
29638cff389af1d78bd80df0479ef258493e0c5897eChris Lattner
29738cff389af1d78bd80df0479ef258493e0c5897eChris Lattner  // Do the lookup, if we have a hit, return it.
298b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes  const MCSectionELF *&Entry = Map[Section];
29938cff389af1d78bd80df0479ef258493e0c5897eChris Lattner  if (Entry) return Entry;
30038cff389af1d78bd80df0479ef258493e0c5897eChris Lattner
301b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes  return Entry = MCSectionELF::Create(Section, Type, Flags, Kind, HasCrazyBSS,
302b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes                                      IsExplicit, getContext());
303fbf1d271e6a7157c1b5432e84d5633f63869b5a8Chris Lattner}
304fbf1d271e6a7157c1b5432e84d5633f63869b5a8Chris Lattner
305f26e03bc7e30162197641406e37e662a15d80f7eChris Lattnervoid TargetLoweringObjectFileELF::Initialize(MCContext &Ctx,
306f26e03bc7e30162197641406e37e662a15d80f7eChris Lattner                                             const TargetMachine &TM) {
307a87dea4f8c546ca748f1777a8d1cabcc06515d91Chris Lattner  TargetLoweringObjectFile::Initialize(Ctx, TM);
308f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
309b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes  BSSSection =
310b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes    getELFSection(".bss", MCSectionELF::SHT_NOBITS,
311b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes                  MCSectionELF::SHF_WRITE | MCSectionELF::SHF_ALLOC,
312b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes                  SectionKind::getBSS());
313f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
314b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes  TextSection =
315b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes    getELFSection(".text", MCSectionELF::SHT_PROGBITS,
316b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes                  MCSectionELF::SHF_EXECINSTR | MCSectionELF::SHF_ALLOC,
317b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes                  SectionKind::getText());
318b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes
319b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes  DataSection =
320b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes    getELFSection(".data", MCSectionELF::SHT_PROGBITS,
321b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes                  MCSectionELF::SHF_WRITE | MCSectionELF::SHF_ALLOC,
322b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes                  SectionKind::getDataRel());
323b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes
324b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes  ReadOnlySection =
325b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes    getELFSection(".rodata", MCSectionELF::SHT_PROGBITS,
326b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes                  MCSectionELF::SHF_ALLOC,
327b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes                  SectionKind::getReadOnly());
328b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes
329b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes  TLSDataSection =
330b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes    getELFSection(".tdata", MCSectionELF::SHT_PROGBITS,
331b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes                  MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_TLS |
332b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes                  MCSectionELF::SHF_WRITE, SectionKind::getThreadData());
333b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes
334b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes  TLSBSSSection =
335b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes    getELFSection(".tbss", MCSectionELF::SHT_NOBITS,
336b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes                  MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_TLS |
337b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes                  MCSectionELF::SHF_WRITE, SectionKind::getThreadBSS());
338b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes
339b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes  DataRelSection =
340b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes    getELFSection(".data.rel", MCSectionELF::SHT_PROGBITS,
341b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes                  MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
342b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes                  SectionKind::getDataRel());
343b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes
344b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes  DataRelLocalSection =
345b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes    getELFSection(".data.rel.local", MCSectionELF::SHT_PROGBITS,
346b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes                  MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
347b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes                  SectionKind::getDataRelLocal());
348b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes
349b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes  DataRelROSection =
350b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes    getELFSection(".data.rel.ro", MCSectionELF::SHT_PROGBITS,
351b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes                  MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
352b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes                  SectionKind::getReadOnlyWithRel());
353b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes
354b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes  DataRelROLocalSection =
355b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes    getELFSection(".data.rel.ro.local", MCSectionELF::SHT_PROGBITS,
356b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes                  MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
357b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes                  SectionKind::getReadOnlyWithRelLocal());
358f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
359b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes  MergeableConst4Section =
360b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes    getELFSection(".rodata.cst4", MCSectionELF::SHT_PROGBITS,
361b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes                  MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE,
362b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes                  SectionKind::getMergeableConst4());
363b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes
364b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes  MergeableConst8Section =
365b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes    getELFSection(".rodata.cst8", MCSectionELF::SHT_PROGBITS,
366b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes                  MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE,
367b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes                  SectionKind::getMergeableConst8());
368b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes
369b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes  MergeableConst16Section =
370b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes    getELFSection(".rodata.cst16", MCSectionELF::SHT_PROGBITS,
371b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes                  MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE,
372b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes                  SectionKind::getMergeableConst16());
37380ec2792b2b271eca55743a3cc4c8bca214fa705Chris Lattner
37480ec2792b2b271eca55743a3cc4c8bca214fa705Chris Lattner  StaticCtorSection =
375b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes    getELFSection(".ctors", MCSectionELF::SHT_PROGBITS,
376b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes                  MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
377b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes                  SectionKind::getDataRel());
378b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes
37980ec2792b2b271eca55743a3cc4c8bca214fa705Chris Lattner  StaticDtorSection =
380b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes    getELFSection(".dtors", MCSectionELF::SHT_PROGBITS,
381b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes                  MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
382b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes                  SectionKind::getDataRel());
383d5bbb07ec806e6fa1e804afd7073987fdacc83e4Chris Lattner
38418a4c16726db2b8874c7b84d04650dda80746074Chris Lattner  // Exception Handling Sections.
385d5bbb07ec806e6fa1e804afd7073987fdacc83e4Chris Lattner
386d5bbb07ec806e6fa1e804afd7073987fdacc83e4Chris Lattner  // FIXME: We're emitting LSDA info into a readonly section on ELF, even though
387d5bbb07ec806e6fa1e804afd7073987fdacc83e4Chris Lattner  // it contains relocatable pointers.  In PIC mode, this is probably a big
388d5bbb07ec806e6fa1e804afd7073987fdacc83e4Chris Lattner  // runtime hit for C++ apps.  Either the contents of the LSDA need to be
389d5bbb07ec806e6fa1e804afd7073987fdacc83e4Chris Lattner  // adjusted or this should be a data section.
390d5bbb07ec806e6fa1e804afd7073987fdacc83e4Chris Lattner  LSDASection =
391b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes    getELFSection(".gcc_except_table", MCSectionELF::SHT_PROGBITS,
392b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes                  MCSectionELF::SHF_ALLOC, SectionKind::getReadOnly());
39335039ac24163e99cfab161620a9fb41f944a63d5Chris Lattner  EHFrameSection =
394b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes    getELFSection(".eh_frame", MCSectionELF::SHT_PROGBITS,
395b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes                  MCSectionELF::SHF_ALLOC, SectionKind::getDataRel());
39618a4c16726db2b8874c7b84d04650dda80746074Chris Lattner
39718a4c16726db2b8874c7b84d04650dda80746074Chris Lattner  // Debug Info Sections.
39818a4c16726db2b8874c7b84d04650dda80746074Chris Lattner  DwarfAbbrevSection =
399b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes    getELFSection(".debug_abbrev", MCSectionELF::SHT_PROGBITS, 0,
400b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes                  SectionKind::getMetadata());
40118a4c16726db2b8874c7b84d04650dda80746074Chris Lattner  DwarfInfoSection =
402b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes    getELFSection(".debug_info", MCSectionELF::SHT_PROGBITS, 0,
403b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes                  SectionKind::getMetadata());
40418a4c16726db2b8874c7b84d04650dda80746074Chris Lattner  DwarfLineSection =
405b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes    getELFSection(".debug_line", MCSectionELF::SHT_PROGBITS, 0,
406b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes                  SectionKind::getMetadata());
40718a4c16726db2b8874c7b84d04650dda80746074Chris Lattner  DwarfFrameSection =
408b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes    getELFSection(".debug_frame", MCSectionELF::SHT_PROGBITS, 0,
409b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes                  SectionKind::getMetadata());
41018a4c16726db2b8874c7b84d04650dda80746074Chris Lattner  DwarfPubNamesSection =
411b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes    getELFSection(".debug_pubnames", MCSectionELF::SHT_PROGBITS, 0,
412b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes                  SectionKind::getMetadata());
41318a4c16726db2b8874c7b84d04650dda80746074Chris Lattner  DwarfPubTypesSection =
414b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes    getELFSection(".debug_pubtypes", MCSectionELF::SHT_PROGBITS, 0,
415b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes                  SectionKind::getMetadata());
41618a4c16726db2b8874c7b84d04650dda80746074Chris Lattner  DwarfStrSection =
417b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes    getELFSection(".debug_str", MCSectionELF::SHT_PROGBITS, 0,
418b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes                  SectionKind::getMetadata());
41918a4c16726db2b8874c7b84d04650dda80746074Chris Lattner  DwarfLocSection =
420b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes    getELFSection(".debug_loc", MCSectionELF::SHT_PROGBITS, 0,
421b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes                  SectionKind::getMetadata());
42218a4c16726db2b8874c7b84d04650dda80746074Chris Lattner  DwarfARangesSection =
423b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes    getELFSection(".debug_aranges", MCSectionELF::SHT_PROGBITS, 0,
424b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes                  SectionKind::getMetadata());
42518a4c16726db2b8874c7b84d04650dda80746074Chris Lattner  DwarfRangesSection =
426b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes    getELFSection(".debug_ranges", MCSectionELF::SHT_PROGBITS, 0,
427b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes                  SectionKind::getMetadata());
42818a4c16726db2b8874c7b84d04650dda80746074Chris Lattner  DwarfMacroInfoSection =
429b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes    getELFSection(".debug_macinfo", MCSectionELF::SHT_PROGBITS, 0,
430b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes                  SectionKind::getMetadata());
431f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner}
432f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
433f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
43424f654c8a4d14066233480f683d3b0dececf374aChris Lattnerstatic SectionKind
43524f654c8a4d14066233480f683d3b0dececf374aChris LattnergetELFKindForNamedSection(const char *Name, SectionKind K) {
436f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  if (Name[0] != '.') return K;
437f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
438f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  // Some lame default implementation based on some magic section names.
439f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  if (strncmp(Name, ".gnu.linkonce.b.", 16) == 0 ||
440f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner      strncmp(Name, ".llvm.linkonce.b.", 17) == 0 ||
441f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner      strncmp(Name, ".gnu.linkonce.sb.", 17) == 0 ||
442f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner      strncmp(Name, ".llvm.linkonce.sb.", 18) == 0)
4432798119ab4d7e0b42812b3acdf37821f40dee627Chris Lattner    return SectionKind::getBSS();
444f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
445f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  if (strcmp(Name, ".tdata") == 0 ||
446f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner      strncmp(Name, ".tdata.", 7) == 0 ||
447f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner      strncmp(Name, ".gnu.linkonce.td.", 17) == 0 ||
448f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner      strncmp(Name, ".llvm.linkonce.td.", 18) == 0)
4492798119ab4d7e0b42812b3acdf37821f40dee627Chris Lattner    return SectionKind::getThreadData();
450f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
451f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  if (strcmp(Name, ".tbss") == 0 ||
452f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner      strncmp(Name, ".tbss.", 6) == 0 ||
453f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner      strncmp(Name, ".gnu.linkonce.tb.", 17) == 0 ||
454f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner      strncmp(Name, ".llvm.linkonce.tb.", 18) == 0)
4552798119ab4d7e0b42812b3acdf37821f40dee627Chris Lattner    return SectionKind::getThreadBSS();
456f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
457f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  return K;
458f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner}
459f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
460b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes
461b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopesstatic unsigned
462b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso LopesgetELFSectionType(const char *Name, SectionKind K) {
463b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes
464b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes  if (strncmp(Name, ".init_array", 11) == 0)
465b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes    return MCSectionELF::SHT_INIT_ARRAY;
466b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes
467b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes  if (strncmp(Name, ".fini_array", 11) == 0)
468b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes    return MCSectionELF::SHT_FINI_ARRAY;
469b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes
470b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes  if (strncmp(Name, ".preinit_array", 14) == 0)
471b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes    return MCSectionELF::SHT_PREINIT_ARRAY;
472b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes
473b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes  if (K.isBSS() || K.isThreadBSS())
474b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes    return MCSectionELF::SHT_NOBITS;
475b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes
476b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes  return MCSectionELF::SHT_PROGBITS;
477b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes}
478b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes
479b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes
480b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopesstatic unsigned
481b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso LopesgetELFSectionFlags(SectionKind K) {
482b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes  unsigned Flags = 0;
483b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes
484b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes  if (!K.isMetadata())
485b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes    Flags |= MCSectionELF::SHF_ALLOC;
486b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes
487b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes  if (K.isWriteable())
488b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes    Flags |= MCSectionELF::SHF_WRITE;
489b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes
490b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes  if (K.isThreadLocal())
491b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes    Flags |= MCSectionELF::SHF_TLS;
492b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes
493b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes  // K.isMergeableConst() is left out to honour PR4650
494b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes  if (K.isMergeableCString() || K.isMergeableConst4() ||
495b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes      K.isMergeableConst8() || K.isMergeableConst16())
496b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes    Flags |= MCSectionELF::SHF_MERGE;
497b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes
498b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes  if (K.isMergeableCString())
499b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes    Flags |= MCSectionELF::SHF_STRINGS;
500b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes
501b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes  return Flags;
502b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes}
503b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes
504b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes
50524f654c8a4d14066233480f683d3b0dececf374aChris Lattnerconst MCSection *TargetLoweringObjectFileELF::
50624f654c8a4d14066233480f683d3b0dececf374aChris LattnergetExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
50724f654c8a4d14066233480f683d3b0dececf374aChris Lattner                         Mangler *Mang, const TargetMachine &TM) const {
508b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes  const char *SectionName = GV->getSection().c_str();
509b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes
51024f654c8a4d14066233480f683d3b0dececf374aChris Lattner  // Infer section flags from the section name if we can.
511b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes  Kind = getELFKindForNamedSection(SectionName, Kind);
51224f654c8a4d14066233480f683d3b0dececf374aChris Lattner
513b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes  return getELFSection(SectionName,
514b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes                       getELFSectionType(SectionName, Kind),
515b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes                       getELFSectionFlags(Kind), Kind, true);
51624f654c8a4d14066233480f683d3b0dececf374aChris Lattner}
517f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
518f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattnerstatic const char *getSectionPrefixForUniqueGlobal(SectionKind Kind) {
519f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  if (Kind.isText())                 return ".gnu.linkonce.t.";
520f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  if (Kind.isReadOnly())             return ".gnu.linkonce.r.";
521f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
522f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  if (Kind.isThreadData())           return ".gnu.linkonce.td.";
523f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  if (Kind.isThreadBSS())            return ".gnu.linkonce.tb.";
524f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
525f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  if (Kind.isBSS())                  return ".gnu.linkonce.b.";
526f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  if (Kind.isDataNoRel())            return ".gnu.linkonce.d.";
527f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  if (Kind.isDataRelLocal())         return ".gnu.linkonce.d.rel.local.";
528f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  if (Kind.isDataRel())              return ".gnu.linkonce.d.rel.";
529f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  if (Kind.isReadOnlyWithRelLocal()) return ".gnu.linkonce.d.rel.ro.local.";
530f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
531f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
532f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  return ".gnu.linkonce.d.rel.ro.";
533f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner}
534f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
535a87dea4f8c546ca748f1777a8d1cabcc06515d91Chris Lattnerconst MCSection *TargetLoweringObjectFileELF::
536f9650c061ee89ac55740555530ca5c2842c28738Chris LattnerSelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
537e53a600f065075731d0aeb9dc8f4f3d75f5a05f8Chris Lattner                       Mangler *Mang, const TargetMachine &TM) const {
538f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
539f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  // If this global is linkonce/weak and the target handles this by emitting it
540f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  // into a 'uniqued' section name, create and return the section now.
54127602b82c2a12b9f99c1f7fcbfb4be5ba97dbd7dChris Lattner  if (GV->isWeakForLinker()) {
542f9650c061ee89ac55740555530ca5c2842c28738Chris Lattner    const char *Prefix = getSectionPrefixForUniqueGlobal(Kind);
543b8f396bdbb06b585b480ada31ec8c2ab35916a55Chris Lattner    std::string Name = Mang->makeNameProper(GV->getNameStr());
544b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes
545b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes    return getELFSection((Prefix+Name).c_str(),
546b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes                         getELFSectionType((Prefix+Name).c_str(), Kind),
547b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes                         getELFSectionFlags(Kind),
548b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes                         Kind);
549f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  }
550f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
551f9650c061ee89ac55740555530ca5c2842c28738Chris Lattner  if (Kind.isText()) return TextSection;
552e53a600f065075731d0aeb9dc8f4f3d75f5a05f8Chris Lattner
5533b24c0172069a2546cd095e4b91f8b88c1ea0722Chris Lattner  if (Kind.isMergeable1ByteCString() ||
5543b24c0172069a2546cd095e4b91f8b88c1ea0722Chris Lattner      Kind.isMergeable2ByteCString() ||
5553b24c0172069a2546cd095e4b91f8b88c1ea0722Chris Lattner      Kind.isMergeable4ByteCString()) {
556f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
557067fe1ae5488764afb8d8fdafba65899dc221887Chris Lattner    // We also need alignment here.
558067fe1ae5488764afb8d8fdafba65899dc221887Chris Lattner    // FIXME: this is getting the alignment of the character, not the
559067fe1ae5488764afb8d8fdafba65899dc221887Chris Lattner    // alignment of the global!
560067fe1ae5488764afb8d8fdafba65899dc221887Chris Lattner    unsigned Align =
561067fe1ae5488764afb8d8fdafba65899dc221887Chris Lattner      TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV));
562f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
5637e88a50428377813606c66ac47111d9c3ea44febChris Lattner    const char *SizeSpec = ".rodata.str1.";
5643b24c0172069a2546cd095e4b91f8b88c1ea0722Chris Lattner    if (Kind.isMergeable2ByteCString())
5657e88a50428377813606c66ac47111d9c3ea44febChris Lattner      SizeSpec = ".rodata.str2.";
5663b24c0172069a2546cd095e4b91f8b88c1ea0722Chris Lattner    else if (Kind.isMergeable4ByteCString())
5677e88a50428377813606c66ac47111d9c3ea44febChris Lattner      SizeSpec = ".rodata.str4.";
5683b24c0172069a2546cd095e4b91f8b88c1ea0722Chris Lattner    else
5693b24c0172069a2546cd095e4b91f8b88c1ea0722Chris Lattner      assert(Kind.isMergeable1ByteCString() && "unknown string width");
5703b24c0172069a2546cd095e4b91f8b88c1ea0722Chris Lattner
5713b24c0172069a2546cd095e4b91f8b88c1ea0722Chris Lattner
5727e88a50428377813606c66ac47111d9c3ea44febChris Lattner    std::string Name = SizeSpec + utostr(Align);
573b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes    return getELFSection(Name.c_str(), MCSectionELF::SHT_PROGBITS,
574b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes                         MCSectionELF::SHF_ALLOC |
575b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes                         MCSectionELF::SHF_MERGE |
576b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes                         MCSectionELF::SHF_STRINGS,
577b808588a3a5febe931896b3779d159ba90d836f7Bruno Cardoso Lopes                         Kind);
578f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  }
579f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
580f9650c061ee89ac55740555530ca5c2842c28738Chris Lattner  if (Kind.isMergeableConst()) {
581f9650c061ee89ac55740555530ca5c2842c28738Chris Lattner    if (Kind.isMergeableConst4())
582f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner      return MergeableConst4Section;
583f9650c061ee89ac55740555530ca5c2842c28738Chris Lattner    if (Kind.isMergeableConst8())
584f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner      return MergeableConst8Section;
585f9650c061ee89ac55740555530ca5c2842c28738Chris Lattner    if (Kind.isMergeableConst16())
586f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner      return MergeableConst16Section;
587f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner    return ReadOnlySection;  // .const
588f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  }
589f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
590f9650c061ee89ac55740555530ca5c2842c28738Chris Lattner  if (Kind.isReadOnly())             return ReadOnlySection;
591f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
592f9650c061ee89ac55740555530ca5c2842c28738Chris Lattner  if (Kind.isThreadData())           return TLSDataSection;
593f9650c061ee89ac55740555530ca5c2842c28738Chris Lattner  if (Kind.isThreadBSS())            return TLSBSSSection;
594f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
595824583844a8f334dd261894a3fac7ad476531667Chris Lattner  if (Kind.isBSS())                  return BSSSection;
596f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
597f9650c061ee89ac55740555530ca5c2842c28738Chris Lattner  if (Kind.isDataNoRel())            return DataSection;
598f9650c061ee89ac55740555530ca5c2842c28738Chris Lattner  if (Kind.isDataRelLocal())         return DataRelLocalSection;
599f9650c061ee89ac55740555530ca5c2842c28738Chris Lattner  if (Kind.isDataRel())              return DataRelSection;
600f9650c061ee89ac55740555530ca5c2842c28738Chris Lattner  if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
601f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
602f9650c061ee89ac55740555530ca5c2842c28738Chris Lattner  assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
603f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  return DataRelROSection;
604f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner}
605f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
60683d77faf6e8fc2c1c2377d037283dc162d8667a1Chris Lattner/// getSectionForConstant - Given a mergeable constant with the
607f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner/// specified size and relocation information, return a section that it
608f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner/// should be placed in.
609a87dea4f8c546ca748f1777a8d1cabcc06515d91Chris Lattnerconst MCSection *TargetLoweringObjectFileELF::
61083d77faf6e8fc2c1c2377d037283dc162d8667a1Chris LattnergetSectionForConstant(SectionKind Kind) const {
611f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  if (Kind.isMergeableConst4())
612f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner    return MergeableConst4Section;
613f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  if (Kind.isMergeableConst8())
614f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner    return MergeableConst8Section;
615f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  if (Kind.isMergeableConst16())
616f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner    return MergeableConst16Section;
617f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  if (Kind.isReadOnly())
618f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner    return ReadOnlySection;
619f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
620f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
621f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
622f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  return DataRelROSection;
623f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner}
624f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
625f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner//===----------------------------------------------------------------------===//
626f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner//                                 MachO
627f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner//===----------------------------------------------------------------------===//
628f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
6295dc47ff03975b9adde9dd833db2b646eb4295710Chris Lattnertypedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
6305dc47ff03975b9adde9dd833db2b646eb4295710Chris Lattner
6315dc47ff03975b9adde9dd833db2b646eb4295710Chris LattnerTargetLoweringObjectFileMachO::~TargetLoweringObjectFileMachO() {
6325dc47ff03975b9adde9dd833db2b646eb4295710Chris Lattner  // If we have the MachO uniquing map, free it.
6335dc47ff03975b9adde9dd833db2b646eb4295710Chris Lattner  delete (MachOUniqueMapTy*)UniquingMap;
6345dc47ff03975b9adde9dd833db2b646eb4295710Chris Lattner}
6350c0cb7123346beab4e0d3ad6ce9570560b14971eChris Lattner
6365dc47ff03975b9adde9dd833db2b646eb4295710Chris Lattner
6375dc47ff03975b9adde9dd833db2b646eb4295710Chris Lattnerconst MCSectionMachO *TargetLoweringObjectFileMachO::
638d3c4486f46226fb328ceb16d55927472fb3e0f6dChris LattnergetMachOSection(const StringRef &Segment, const StringRef &Section,
639ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner                unsigned TypeAndAttributes,
640ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner                unsigned Reserved2, SectionKind Kind) const {
6415dc47ff03975b9adde9dd833db2b646eb4295710Chris Lattner  // We unique sections by their segment/section pair.  The returned section
6425dc47ff03975b9adde9dd833db2b646eb4295710Chris Lattner  // may not have the same flags as the requested section, if so this should be
6435dc47ff03975b9adde9dd833db2b646eb4295710Chris Lattner  // diagnosed by the client as an error.
6445dc47ff03975b9adde9dd833db2b646eb4295710Chris Lattner
6455dc47ff03975b9adde9dd833db2b646eb4295710Chris Lattner  // Create the map if it doesn't already exist.
6465dc47ff03975b9adde9dd833db2b646eb4295710Chris Lattner  if (UniquingMap == 0)
6475dc47ff03975b9adde9dd833db2b646eb4295710Chris Lattner    UniquingMap = new MachOUniqueMapTy();
6485dc47ff03975b9adde9dd833db2b646eb4295710Chris Lattner  MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)UniquingMap;
6495dc47ff03975b9adde9dd833db2b646eb4295710Chris Lattner
6505dc47ff03975b9adde9dd833db2b646eb4295710Chris Lattner  // Form the name to look up.
6515dc47ff03975b9adde9dd833db2b646eb4295710Chris Lattner  SmallString<64> Name;
6525dc47ff03975b9adde9dd833db2b646eb4295710Chris Lattner  Name.append(Segment.begin(), Segment.end());
6535dc47ff03975b9adde9dd833db2b646eb4295710Chris Lattner  Name.push_back(',');
6545dc47ff03975b9adde9dd833db2b646eb4295710Chris Lattner  Name.append(Section.begin(), Section.end());
6555dc47ff03975b9adde9dd833db2b646eb4295710Chris Lattner
6565dc47ff03975b9adde9dd833db2b646eb4295710Chris Lattner  // Do the lookup, if we have a hit, return it.
6575dc47ff03975b9adde9dd833db2b646eb4295710Chris Lattner  const MCSectionMachO *&Entry = Map[StringRef(Name.data(), Name.size())];
6585dc47ff03975b9adde9dd833db2b646eb4295710Chris Lattner  if (Entry) return Entry;
6595dc47ff03975b9adde9dd833db2b646eb4295710Chris Lattner
6605dc47ff03975b9adde9dd833db2b646eb4295710Chris Lattner  // Otherwise, return a new section.
6615dc47ff03975b9adde9dd833db2b646eb4295710Chris Lattner  return Entry = MCSectionMachO::Create(Segment, Section, TypeAndAttributes,
6625dc47ff03975b9adde9dd833db2b646eb4295710Chris Lattner                                        Reserved2, Kind, getContext());
663fbf1d271e6a7157c1b5432e84d5633f63869b5a8Chris Lattner}
664fbf1d271e6a7157c1b5432e84d5633f63869b5a8Chris Lattner
66511e9657eeb76dff6baaab1cbac0b1fb7e1abb439Chris Lattner
666f26e03bc7e30162197641406e37e662a15d80f7eChris Lattnervoid TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
667f26e03bc7e30162197641406e37e662a15d80f7eChris Lattner                                               const TargetMachine &TM) {
668a87dea4f8c546ca748f1777a8d1cabcc06515d91Chris Lattner  TargetLoweringObjectFile::Initialize(Ctx, TM);
669ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner
670ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner  TextSection // .text
671ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner    = getMachOSection("__TEXT", "__text",
672ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner                      MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
673ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner                      SectionKind::getText());
674ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner  DataSection // .data
675ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner    = getMachOSection("__DATA", "__data", 0, SectionKind::getDataRel());
676ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner
677ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner  CStringSection // .cstring
678ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner    = getMachOSection("__TEXT", "__cstring", MCSectionMachO::S_CSTRING_LITERALS,
679ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner                      SectionKind::getMergeable1ByteCString());
680ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner  UStringSection
681ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner    = getMachOSection("__TEXT","__ustring", 0,
682ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner                      SectionKind::getMergeable2ByteCString());
683ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner  FourByteConstantSection // .literal4
684ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner    = getMachOSection("__TEXT", "__literal4", MCSectionMachO::S_4BYTE_LITERALS,
685ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner                      SectionKind::getMergeableConst4());
686ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner  EightByteConstantSection // .literal8
687ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner    = getMachOSection("__TEXT", "__literal8", MCSectionMachO::S_8BYTE_LITERALS,
688ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner                      SectionKind::getMergeableConst8());
6894bb253c60f895131371aa2ad1bfa5a2bea213f78Chris Lattner
6904bb253c60f895131371aa2ad1bfa5a2bea213f78Chris Lattner  // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
6914bb253c60f895131371aa2ad1bfa5a2bea213f78Chris Lattner  // to using it in -static mode.
692ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner  SixteenByteConstantSection = 0;
6934bb253c60f895131371aa2ad1bfa5a2bea213f78Chris Lattner  if (TM.getRelocationModel() != Reloc::Static &&
6944bb253c60f895131371aa2ad1bfa5a2bea213f78Chris Lattner      TM.getTargetData()->getPointerSize() == 32)
695ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner    SixteenByteConstantSection =   // .literal16
696ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner      getMachOSection("__TEXT", "__literal16",MCSectionMachO::S_16BYTE_LITERALS,
6970c0cb7123346beab4e0d3ad6ce9570560b14971eChris Lattner                      SectionKind::getMergeableConst16());
698ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner
699ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner  ReadOnlySection  // .const
700ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner    = getMachOSection("__TEXT", "__const", 0, SectionKind::getReadOnly());
701ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner
702ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner  TextCoalSection
703ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner    = getMachOSection("__TEXT", "__textcoal_nt",
704ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner                      MCSectionMachO::S_COALESCED |
705ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner                      MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
706ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner                      SectionKind::getText());
707ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner  ConstTextCoalSection
708ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner    = getMachOSection("__TEXT", "__const_coal", MCSectionMachO::S_COALESCED,
709ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner                      SectionKind::getText());
710ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner  ConstDataCoalSection
711ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner    = getMachOSection("__DATA","__const_coal", MCSectionMachO::S_COALESCED,
712ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner                      SectionKind::getText());
713ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner  ConstDataSection  // .const_data
714ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner    = getMachOSection("__DATA", "__const", 0,
715ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner                      SectionKind::getReadOnlyWithRel());
716ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner  DataCoalSection
717ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner    = getMachOSection("__DATA","__datacoal_nt", MCSectionMachO::S_COALESCED,
718ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner                      SectionKind::getDataRel());
71980ec2792b2b271eca55743a3cc4c8bca214fa705Chris Lattner
720e309cfa0d8945af47dd798357549c815164d53d6Chris Lattner
721e309cfa0d8945af47dd798357549c815164d53d6Chris Lattner  LazySymbolPointerSection
722e309cfa0d8945af47dd798357549c815164d53d6Chris Lattner    = getMachOSection("__DATA", "__la_symbol_ptr",
723e309cfa0d8945af47dd798357549c815164d53d6Chris Lattner                      MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
724e309cfa0d8945af47dd798357549c815164d53d6Chris Lattner                      SectionKind::getMetadata());
725e309cfa0d8945af47dd798357549c815164d53d6Chris Lattner  NonLazySymbolPointerSection
726e309cfa0d8945af47dd798357549c815164d53d6Chris Lattner    = getMachOSection("__DATA", "__nl_symbol_ptr",
727e309cfa0d8945af47dd798357549c815164d53d6Chris Lattner                      MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
728e309cfa0d8945af47dd798357549c815164d53d6Chris Lattner                      SectionKind::getMetadata());
729e309cfa0d8945af47dd798357549c815164d53d6Chris Lattner
73080ec2792b2b271eca55743a3cc4c8bca214fa705Chris Lattner  if (TM.getRelocationModel() == Reloc::Static) {
731ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner    StaticCtorSection
732ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner      = getMachOSection("__TEXT", "__constructor", 0,SectionKind::getDataRel());
733ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner    StaticDtorSection
734ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner      = getMachOSection("__TEXT", "__destructor", 0, SectionKind::getDataRel());
73580ec2792b2b271eca55743a3cc4c8bca214fa705Chris Lattner  } else {
736ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner    StaticCtorSection
737ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner      = getMachOSection("__DATA", "__mod_init_func",
738ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner                        MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
739ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner                        SectionKind::getDataRel());
740ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner    StaticDtorSection
741ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner      = getMachOSection("__DATA", "__mod_term_func",
742ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner                        MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
743ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner                        SectionKind::getDataRel());
74480ec2792b2b271eca55743a3cc4c8bca214fa705Chris Lattner  }
74580ec2792b2b271eca55743a3cc4c8bca214fa705Chris Lattner
74618a4c16726db2b8874c7b84d04650dda80746074Chris Lattner  // Exception Handling.
747ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner  LSDASection = getMachOSection("__DATA", "__gcc_except_tab", 0,
7480c0cb7123346beab4e0d3ad6ce9570560b14971eChris Lattner                                SectionKind::getDataRel());
74935039ac24163e99cfab161620a9fb41f944a63d5Chris Lattner  EHFrameSection =
750ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner    getMachOSection("__TEXT", "__eh_frame",
751ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner                    MCSectionMachO::S_COALESCED |
752ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner                    MCSectionMachO::S_ATTR_NO_TOC |
753ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner                    MCSectionMachO::S_ATTR_STRIP_STATIC_SYMS |
754ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner                    MCSectionMachO::S_ATTR_LIVE_SUPPORT,
755ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner                    SectionKind::getReadOnly());
75618a4c16726db2b8874c7b84d04650dda80746074Chris Lattner
75718a4c16726db2b8874c7b84d04650dda80746074Chris Lattner  // Debug Information.
75818a4c16726db2b8874c7b84d04650dda80746074Chris Lattner  DwarfAbbrevSection =
759ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner    getMachOSection("__DWARF", "__debug_abbrev", MCSectionMachO::S_ATTR_DEBUG,
7600c0cb7123346beab4e0d3ad6ce9570560b14971eChris Lattner                    SectionKind::getMetadata());
76118a4c16726db2b8874c7b84d04650dda80746074Chris Lattner  DwarfInfoSection =
762ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner    getMachOSection("__DWARF", "__debug_info", MCSectionMachO::S_ATTR_DEBUG,
7630c0cb7123346beab4e0d3ad6ce9570560b14971eChris Lattner                    SectionKind::getMetadata());
76418a4c16726db2b8874c7b84d04650dda80746074Chris Lattner  DwarfLineSection =
765ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner    getMachOSection("__DWARF", "__debug_line", MCSectionMachO::S_ATTR_DEBUG,
7660c0cb7123346beab4e0d3ad6ce9570560b14971eChris Lattner                    SectionKind::getMetadata());
76718a4c16726db2b8874c7b84d04650dda80746074Chris Lattner  DwarfFrameSection =
768ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner    getMachOSection("__DWARF", "__debug_frame", MCSectionMachO::S_ATTR_DEBUG,
7690c0cb7123346beab4e0d3ad6ce9570560b14971eChris Lattner                    SectionKind::getMetadata());
77018a4c16726db2b8874c7b84d04650dda80746074Chris Lattner  DwarfPubNamesSection =
771ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner    getMachOSection("__DWARF", "__debug_pubnames", MCSectionMachO::S_ATTR_DEBUG,
7720c0cb7123346beab4e0d3ad6ce9570560b14971eChris Lattner                    SectionKind::getMetadata());
77318a4c16726db2b8874c7b84d04650dda80746074Chris Lattner  DwarfPubTypesSection =
774ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner    getMachOSection("__DWARF", "__debug_pubtypes", MCSectionMachO::S_ATTR_DEBUG,
7750c0cb7123346beab4e0d3ad6ce9570560b14971eChris Lattner                    SectionKind::getMetadata());
77618a4c16726db2b8874c7b84d04650dda80746074Chris Lattner  DwarfStrSection =
777ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner    getMachOSection("__DWARF", "__debug_str", MCSectionMachO::S_ATTR_DEBUG,
7780c0cb7123346beab4e0d3ad6ce9570560b14971eChris Lattner                    SectionKind::getMetadata());
77918a4c16726db2b8874c7b84d04650dda80746074Chris Lattner  DwarfLocSection =
780ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner    getMachOSection("__DWARF", "__debug_loc", MCSectionMachO::S_ATTR_DEBUG,
7810c0cb7123346beab4e0d3ad6ce9570560b14971eChris Lattner                    SectionKind::getMetadata());
78218a4c16726db2b8874c7b84d04650dda80746074Chris Lattner  DwarfARangesSection =
783ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner    getMachOSection("__DWARF", "__debug_aranges", MCSectionMachO::S_ATTR_DEBUG,
7840c0cb7123346beab4e0d3ad6ce9570560b14971eChris Lattner                    SectionKind::getMetadata());
78518a4c16726db2b8874c7b84d04650dda80746074Chris Lattner  DwarfRangesSection =
786ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner    getMachOSection("__DWARF", "__debug_ranges", MCSectionMachO::S_ATTR_DEBUG,
7870c0cb7123346beab4e0d3ad6ce9570560b14971eChris Lattner                    SectionKind::getMetadata());
78818a4c16726db2b8874c7b84d04650dda80746074Chris Lattner  DwarfMacroInfoSection =
789ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner    getMachOSection("__DWARF", "__debug_macinfo", MCSectionMachO::S_ATTR_DEBUG,
7900c0cb7123346beab4e0d3ad6ce9570560b14971eChris Lattner                    SectionKind::getMetadata());
79118a4c16726db2b8874c7b84d04650dda80746074Chris Lattner  DwarfDebugInlineSection =
792ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner    getMachOSection("__DWARF", "__debug_inlined", MCSectionMachO::S_ATTR_DEBUG,
7930c0cb7123346beab4e0d3ad6ce9570560b14971eChris Lattner                    SectionKind::getMetadata());
794f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner}
795f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
796a87dea4f8c546ca748f1777a8d1cabcc06515d91Chris Lattnerconst MCSection *TargetLoweringObjectFileMachO::
79724f654c8a4d14066233480f683d3b0dececf374aChris LattnergetExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
79824f654c8a4d14066233480f683d3b0dececf374aChris Lattner                         Mangler *Mang, const TargetMachine &TM) const {
799ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner  // Parse the section specifier and create it if valid.
800ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner  StringRef Segment, Section;
801ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner  unsigned TAA, StubSize;
802ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner  std::string ErrorCode =
803ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner    MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
804ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner                                          TAA, StubSize);
805e309cfa0d8945af47dd798357549c815164d53d6Chris Lattner  if (!ErrorCode.empty()) {
806e309cfa0d8945af47dd798357549c815164d53d6Chris Lattner    // If invalid, report the error with llvm_report_error.
807e309cfa0d8945af47dd798357549c815164d53d6Chris Lattner    llvm_report_error("Global variable '" + GV->getNameStr() +
808e309cfa0d8945af47dd798357549c815164d53d6Chris Lattner                      "' has an invalid section specifier '" + GV->getSection()+
809e309cfa0d8945af47dd798357549c815164d53d6Chris Lattner                      "': " + ErrorCode + ".");
810e309cfa0d8945af47dd798357549c815164d53d6Chris Lattner    // Fall back to dropping it into the data section.
811e309cfa0d8945af47dd798357549c815164d53d6Chris Lattner    return DataSection;
812e309cfa0d8945af47dd798357549c815164d53d6Chris Lattner  }
813ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner
814e309cfa0d8945af47dd798357549c815164d53d6Chris Lattner  // Get the section.
815e309cfa0d8945af47dd798357549c815164d53d6Chris Lattner  const MCSectionMachO *S =
816e309cfa0d8945af47dd798357549c815164d53d6Chris Lattner    getMachOSection(Segment, Section, TAA, StubSize, Kind);
817e309cfa0d8945af47dd798357549c815164d53d6Chris Lattner
818e309cfa0d8945af47dd798357549c815164d53d6Chris Lattner  // Okay, now that we got the section, verify that the TAA & StubSize agree.
819e309cfa0d8945af47dd798357549c815164d53d6Chris Lattner  // If the user declared multiple globals with different section flags, we need
820e309cfa0d8945af47dd798357549c815164d53d6Chris Lattner  // to reject it here.
821e309cfa0d8945af47dd798357549c815164d53d6Chris Lattner  if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
822e309cfa0d8945af47dd798357549c815164d53d6Chris Lattner    // If invalid, report the error with llvm_report_error.
823e309cfa0d8945af47dd798357549c815164d53d6Chris Lattner    llvm_report_error("Global variable '" + GV->getNameStr() +
824e309cfa0d8945af47dd798357549c815164d53d6Chris Lattner                      "' section type or attributes does not match previous"
825e309cfa0d8945af47dd798357549c815164d53d6Chris Lattner                      " section specifier");
826e309cfa0d8945af47dd798357549c815164d53d6Chris Lattner  }
827ff4bc460c52c1f285d8a56da173641bf92d49e3fChris Lattner
828e309cfa0d8945af47dd798357549c815164d53d6Chris Lattner  return S;
82924f654c8a4d14066233480f683d3b0dececf374aChris Lattner}
83024f654c8a4d14066233480f683d3b0dececf374aChris Lattner
83124f654c8a4d14066233480f683d3b0dececf374aChris Lattnerconst MCSection *TargetLoweringObjectFileMachO::
832f9650c061ee89ac55740555530ca5c2842c28738Chris LattnerSelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
833e53a600f065075731d0aeb9dc8f4f3d75f5a05f8Chris Lattner                       Mangler *Mang, const TargetMachine &TM) const {
834f9650c061ee89ac55740555530ca5c2842c28738Chris Lattner  assert(!Kind.isThreadLocal() && "Darwin doesn't support TLS");
835f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
836f9650c061ee89ac55740555530ca5c2842c28738Chris Lattner  if (Kind.isText())
83727602b82c2a12b9f99c1f7fcbfb4be5ba97dbd7dChris Lattner    return GV->isWeakForLinker() ? TextCoalSection : TextSection;
838f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
839f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  // If this is weak/linkonce, put this in a coalescable section, either in text
840f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  // or data depending on if it is writable.
84127602b82c2a12b9f99c1f7fcbfb4be5ba97dbd7dChris Lattner  if (GV->isWeakForLinker()) {
842f9650c061ee89ac55740555530ca5c2842c28738Chris Lattner    if (Kind.isReadOnly())
843f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner      return ConstTextCoalSection;
844f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner    return DataCoalSection;
845f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  }
846f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
847f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  // FIXME: Alignment check should be handled by section classifier.
848ec409759e94cc395e4896ba6ade3fa77200c5cfdChris Lattner  if (Kind.isMergeable1ByteCString() ||
849ec409759e94cc395e4896ba6ade3fa77200c5cfdChris Lattner      Kind.isMergeable2ByteCString()) {
850ec409759e94cc395e4896ba6ade3fa77200c5cfdChris Lattner    if (TM.getTargetData()->getPreferredAlignment(
851ec409759e94cc395e4896ba6ade3fa77200c5cfdChris Lattner                                              cast<GlobalVariable>(GV)) < 32) {
852ec409759e94cc395e4896ba6ade3fa77200c5cfdChris Lattner      if (Kind.isMergeable1ByteCString())
853824583844a8f334dd261894a3fac7ad476531667Chris Lattner        return CStringSection;
854ec409759e94cc395e4896ba6ade3fa77200c5cfdChris Lattner      assert(Kind.isMergeable2ByteCString());
855ec409759e94cc395e4896ba6ade3fa77200c5cfdChris Lattner      return UStringSection;
856f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner    }
857f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  }
858f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
859f9650c061ee89ac55740555530ca5c2842c28738Chris Lattner  if (Kind.isMergeableConst()) {
860f9650c061ee89ac55740555530ca5c2842c28738Chris Lattner    if (Kind.isMergeableConst4())
861f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner      return FourByteConstantSection;
862f9650c061ee89ac55740555530ca5c2842c28738Chris Lattner    if (Kind.isMergeableConst8())
863f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner      return EightByteConstantSection;
864f9650c061ee89ac55740555530ca5c2842c28738Chris Lattner    if (Kind.isMergeableConst16() && SixteenByteConstantSection)
865f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner      return SixteenByteConstantSection;
866f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  }
867ec409759e94cc395e4896ba6ade3fa77200c5cfdChris Lattner
868ec409759e94cc395e4896ba6ade3fa77200c5cfdChris Lattner  // Otherwise, if it is readonly, but not something we can specially optimize,
869ec409759e94cc395e4896ba6ade3fa77200c5cfdChris Lattner  // just drop it in .const.
870f9650c061ee89ac55740555530ca5c2842c28738Chris Lattner  if (Kind.isReadOnly())
871f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner    return ReadOnlySection;
872f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
873f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  // If this is marked const, put it into a const section.  But if the dynamic
874f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  // linker needs to write to it, put it in the data segment.
875f9650c061ee89ac55740555530ca5c2842c28738Chris Lattner  if (Kind.isReadOnlyWithRel())
876f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner    return ConstDataSection;
877f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
878f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  // Otherwise, just drop the variable in the normal data section.
879f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  return DataSection;
880f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner}
881f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
882a87dea4f8c546ca748f1777a8d1cabcc06515d91Chris Lattnerconst MCSection *
88383d77faf6e8fc2c1c2377d037283dc162d8667a1Chris LattnerTargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
884f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  // If this constant requires a relocation, we have to put it in the data
885f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  // segment, not in the text segment.
886f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  if (Kind.isDataRel())
887f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner    return ConstDataSection;
888f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
889f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  if (Kind.isMergeableConst4())
890f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner    return FourByteConstantSection;
891f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  if (Kind.isMergeableConst8())
892f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner    return EightByteConstantSection;
8934bb253c60f895131371aa2ad1bfa5a2bea213f78Chris Lattner  if (Kind.isMergeableConst16() && SixteenByteConstantSection)
894f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner    return SixteenByteConstantSection;
895f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  return ReadOnlySection;  // .const
896f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner}
897f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
89826630c1180502d07c9b2b4a9a4ba06bc5ddf180bChris Lattner/// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
89926630c1180502d07c9b2b4a9a4ba06bc5ddf180bChris Lattner/// not to emit the UsedDirective for some symbols in llvm.used.
90026630c1180502d07c9b2b4a9a4ba06bc5ddf180bChris Lattner// FIXME: REMOVE this (rdar://7071300)
90126630c1180502d07c9b2b4a9a4ba06bc5ddf180bChris Lattnerbool TargetLoweringObjectFileMachO::
90226630c1180502d07c9b2b4a9a4ba06bc5ddf180bChris LattnershouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
90326630c1180502d07c9b2b4a9a4ba06bc5ddf180bChris Lattner  /// On Darwin, internally linked data beginning with "L" or "l" does not have
90426630c1180502d07c9b2b4a9a4ba06bc5ddf180bChris Lattner  /// the directive emitted (this occurs in ObjC metadata).
90526630c1180502d07c9b2b4a9a4ba06bc5ddf180bChris Lattner  if (!GV) return false;
90626630c1180502d07c9b2b4a9a4ba06bc5ddf180bChris Lattner
90726630c1180502d07c9b2b4a9a4ba06bc5ddf180bChris Lattner  // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
90826630c1180502d07c9b2b4a9a4ba06bc5ddf180bChris Lattner  if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
90926630c1180502d07c9b2b4a9a4ba06bc5ddf180bChris Lattner    // FIXME: ObjC metadata is currently emitted as internal symbols that have
91026630c1180502d07c9b2b4a9a4ba06bc5ddf180bChris Lattner    // \1L and \0l prefixes on them.  Fix them to be Private/LinkerPrivate and
91126630c1180502d07c9b2b4a9a4ba06bc5ddf180bChris Lattner    // this horrible hack can go away.
91226630c1180502d07c9b2b4a9a4ba06bc5ddf180bChris Lattner    const std::string &Name = Mang->getMangledName(GV);
91326630c1180502d07c9b2b4a9a4ba06bc5ddf180bChris Lattner    if (Name[0] == 'L' || Name[0] == 'l')
91426630c1180502d07c9b2b4a9a4ba06bc5ddf180bChris Lattner      return false;
91526630c1180502d07c9b2b4a9a4ba06bc5ddf180bChris Lattner  }
91626630c1180502d07c9b2b4a9a4ba06bc5ddf180bChris Lattner
91726630c1180502d07c9b2b4a9a4ba06bc5ddf180bChris Lattner  return true;
91826630c1180502d07c9b2b4a9a4ba06bc5ddf180bChris Lattner}
91926630c1180502d07c9b2b4a9a4ba06bc5ddf180bChris Lattner
92026630c1180502d07c9b2b4a9a4ba06bc5ddf180bChris Lattner
921f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner//===----------------------------------------------------------------------===//
922f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner//                                  COFF
923f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner//===----------------------------------------------------------------------===//
924f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
92538cff389af1d78bd80df0479ef258493e0c5897eChris Lattnertypedef StringMap<const MCSectionCOFF*> COFFUniqueMapTy;
92638cff389af1d78bd80df0479ef258493e0c5897eChris Lattner
92738cff389af1d78bd80df0479ef258493e0c5897eChris LattnerTargetLoweringObjectFileCOFF::~TargetLoweringObjectFileCOFF() {
92838cff389af1d78bd80df0479ef258493e0c5897eChris Lattner  delete (COFFUniqueMapTy*)UniquingMap;
92938cff389af1d78bd80df0479ef258493e0c5897eChris Lattner}
93038cff389af1d78bd80df0479ef258493e0c5897eChris Lattner
9310c0cb7123346beab4e0d3ad6ce9570560b14971eChris Lattner
93211e9657eeb76dff6baaab1cbac0b1fb7e1abb439Chris Lattnerconst MCSection *TargetLoweringObjectFileCOFF::
9330c0cb7123346beab4e0d3ad6ce9570560b14971eChris LattnergetCOFFSection(const char *Name, bool isDirective, SectionKind Kind) const {
93438cff389af1d78bd80df0479ef258493e0c5897eChris Lattner  // Create the map if it doesn't already exist.
93538cff389af1d78bd80df0479ef258493e0c5897eChris Lattner  if (UniquingMap == 0)
93638cff389af1d78bd80df0479ef258493e0c5897eChris Lattner    UniquingMap = new MachOUniqueMapTy();
93738cff389af1d78bd80df0479ef258493e0c5897eChris Lattner  COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)UniquingMap;
93838cff389af1d78bd80df0479ef258493e0c5897eChris Lattner
93938cff389af1d78bd80df0479ef258493e0c5897eChris Lattner  // Do the lookup, if we have a hit, return it.
94038cff389af1d78bd80df0479ef258493e0c5897eChris Lattner  const MCSectionCOFF *&Entry = Map[Name];
94138cff389af1d78bd80df0479ef258493e0c5897eChris Lattner  if (Entry) return Entry;
94238cff389af1d78bd80df0479ef258493e0c5897eChris Lattner
94338cff389af1d78bd80df0479ef258493e0c5897eChris Lattner  return Entry = MCSectionCOFF::Create(Name, isDirective, Kind, getContext());
944fbf1d271e6a7157c1b5432e84d5633f63869b5a8Chris Lattner}
945fbf1d271e6a7157c1b5432e84d5633f63869b5a8Chris Lattner
946f26e03bc7e30162197641406e37e662a15d80f7eChris Lattnervoid TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
947f26e03bc7e30162197641406e37e662a15d80f7eChris Lattner                                              const TargetMachine &TM) {
948a87dea4f8c546ca748f1777a8d1cabcc06515d91Chris Lattner  TargetLoweringObjectFile::Initialize(Ctx, TM);
9490c0cb7123346beab4e0d3ad6ce9570560b14971eChris Lattner  TextSection = getCOFFSection("\t.text", true, SectionKind::getText());
9500c0cb7123346beab4e0d3ad6ce9570560b14971eChris Lattner  DataSection = getCOFFSection("\t.data", true, SectionKind::getDataRel());
95180ec2792b2b271eca55743a3cc4c8bca214fa705Chris Lattner  StaticCtorSection =
9520c0cb7123346beab4e0d3ad6ce9570560b14971eChris Lattner    getCOFFSection(".ctors", false, SectionKind::getDataRel());
95380ec2792b2b271eca55743a3cc4c8bca214fa705Chris Lattner  StaticDtorSection =
9540c0cb7123346beab4e0d3ad6ce9570560b14971eChris Lattner    getCOFFSection(".dtors", false, SectionKind::getDataRel());
95518a4c16726db2b8874c7b84d04650dda80746074Chris Lattner
95618a4c16726db2b8874c7b84d04650dda80746074Chris Lattner
95718a4c16726db2b8874c7b84d04650dda80746074Chris Lattner  // Debug info.
95818a4c16726db2b8874c7b84d04650dda80746074Chris Lattner  // FIXME: Don't use 'directive' mode here.
95918a4c16726db2b8874c7b84d04650dda80746074Chris Lattner  DwarfAbbrevSection =
9600c0cb7123346beab4e0d3ad6ce9570560b14971eChris Lattner    getCOFFSection("\t.section\t.debug_abbrev,\"dr\"",
9610c0cb7123346beab4e0d3ad6ce9570560b14971eChris Lattner                   true, SectionKind::getMetadata());
96218a4c16726db2b8874c7b84d04650dda80746074Chris Lattner  DwarfInfoSection =
9630c0cb7123346beab4e0d3ad6ce9570560b14971eChris Lattner    getCOFFSection("\t.section\t.debug_info,\"dr\"",
9640c0cb7123346beab4e0d3ad6ce9570560b14971eChris Lattner                   true, SectionKind::getMetadata());
96518a4c16726db2b8874c7b84d04650dda80746074Chris Lattner  DwarfLineSection =
9660c0cb7123346beab4e0d3ad6ce9570560b14971eChris Lattner    getCOFFSection("\t.section\t.debug_line,\"dr\"",
9670c0cb7123346beab4e0d3ad6ce9570560b14971eChris Lattner                   true, SectionKind::getMetadata());
96818a4c16726db2b8874c7b84d04650dda80746074Chris Lattner  DwarfFrameSection =
9690c0cb7123346beab4e0d3ad6ce9570560b14971eChris Lattner    getCOFFSection("\t.section\t.debug_frame,\"dr\"",
9700c0cb7123346beab4e0d3ad6ce9570560b14971eChris Lattner                   true, SectionKind::getMetadata());
97118a4c16726db2b8874c7b84d04650dda80746074Chris Lattner  DwarfPubNamesSection =
9720c0cb7123346beab4e0d3ad6ce9570560b14971eChris Lattner    getCOFFSection("\t.section\t.debug_pubnames,\"dr\"",
9730c0cb7123346beab4e0d3ad6ce9570560b14971eChris Lattner                   true, SectionKind::getMetadata());
97418a4c16726db2b8874c7b84d04650dda80746074Chris Lattner  DwarfPubTypesSection =
9750c0cb7123346beab4e0d3ad6ce9570560b14971eChris Lattner    getCOFFSection("\t.section\t.debug_pubtypes,\"dr\"",
9760c0cb7123346beab4e0d3ad6ce9570560b14971eChris Lattner                   true, SectionKind::getMetadata());
97718a4c16726db2b8874c7b84d04650dda80746074Chris Lattner  DwarfStrSection =
9780c0cb7123346beab4e0d3ad6ce9570560b14971eChris Lattner    getCOFFSection("\t.section\t.debug_str,\"dr\"",
9790c0cb7123346beab4e0d3ad6ce9570560b14971eChris Lattner                   true, SectionKind::getMetadata());
98018a4c16726db2b8874c7b84d04650dda80746074Chris Lattner  DwarfLocSection =
9810c0cb7123346beab4e0d3ad6ce9570560b14971eChris Lattner    getCOFFSection("\t.section\t.debug_loc,\"dr\"",
9820c0cb7123346beab4e0d3ad6ce9570560b14971eChris Lattner                   true, SectionKind::getMetadata());
98318a4c16726db2b8874c7b84d04650dda80746074Chris Lattner  DwarfARangesSection =
9840c0cb7123346beab4e0d3ad6ce9570560b14971eChris Lattner    getCOFFSection("\t.section\t.debug_aranges,\"dr\"",
9850c0cb7123346beab4e0d3ad6ce9570560b14971eChris Lattner                   true, SectionKind::getMetadata());
98618a4c16726db2b8874c7b84d04650dda80746074Chris Lattner  DwarfRangesSection =
9870c0cb7123346beab4e0d3ad6ce9570560b14971eChris Lattner    getCOFFSection("\t.section\t.debug_ranges,\"dr\"",
9880c0cb7123346beab4e0d3ad6ce9570560b14971eChris Lattner                   true, SectionKind::getMetadata());
98918a4c16726db2b8874c7b84d04650dda80746074Chris Lattner  DwarfMacroInfoSection =
9900c0cb7123346beab4e0d3ad6ce9570560b14971eChris Lattner    getCOFFSection("\t.section\t.debug_macinfo,\"dr\"",
9910c0cb7123346beab4e0d3ad6ce9570560b14971eChris Lattner                   true, SectionKind::getMetadata());
992f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner}
993f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
99424f654c8a4d14066233480f683d3b0dececf374aChris Lattnerconst MCSection *TargetLoweringObjectFileCOFF::
99524f654c8a4d14066233480f683d3b0dececf374aChris LattnergetExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
99624f654c8a4d14066233480f683d3b0dececf374aChris Lattner                         Mangler *Mang, const TargetMachine &TM) const {
9970c0cb7123346beab4e0d3ad6ce9570560b14971eChris Lattner  return getCOFFSection(GV->getSection().c_str(), false, Kind);
99824f654c8a4d14066233480f683d3b0dececf374aChris Lattner}
99924f654c8a4d14066233480f683d3b0dececf374aChris Lattner
1000f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattnerstatic const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
1001f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  if (Kind.isText())
1002f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner    return ".text$linkonce";
1003f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  if (Kind.isWriteable())
1004f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner    return ".data$linkonce";
1005f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  return ".rdata$linkonce";
1006f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner}
1007f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
1008f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
1009a87dea4f8c546ca748f1777a8d1cabcc06515d91Chris Lattnerconst MCSection *TargetLoweringObjectFileCOFF::
1010f9650c061ee89ac55740555530ca5c2842c28738Chris LattnerSelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
1011e53a600f065075731d0aeb9dc8f4f3d75f5a05f8Chris Lattner                       Mangler *Mang, const TargetMachine &TM) const {
1012f9650c061ee89ac55740555530ca5c2842c28738Chris Lattner  assert(!Kind.isThreadLocal() && "Doesn't support TLS");
1013f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
1014f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  // If this global is linkonce/weak and the target handles this by emitting it
1015f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  // into a 'uniqued' section name, create and return the section now.
101627602b82c2a12b9f99c1f7fcbfb4be5ba97dbd7dChris Lattner  if (GV->isWeakForLinker()) {
1017f9650c061ee89ac55740555530ca5c2842c28738Chris Lattner    const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
1018968ff1196768c0b6dbcc5508025a2923bfa73fabChris Lattner    std::string Name = Mang->makeNameProper(GV->getNameStr());
10190c0cb7123346beab4e0d3ad6ce9570560b14971eChris Lattner    return getCOFFSection((Prefix+Name).c_str(), false, Kind);
1020f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  }
1021f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
1022f9650c061ee89ac55740555530ca5c2842c28738Chris Lattner  if (Kind.isText())
1023f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner    return getTextSection();
1024f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
1025f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner  return getDataSection();
1026f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner}
1027f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner
1028