Revert r199871 and replace it with a simple check in the debug info
[oota-llvm.git] / lib / Target / XCore / XCoreTargetObjectFile.cpp
1 //===-- XCoreTargetObjectFile.cpp - XCore object files --------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "XCoreTargetObjectFile.h"
11 #include "XCoreSubtarget.h"
12 #include "llvm/IR/DataLayout.h"
13 #include "llvm/MC/MCContext.h"
14 #include "llvm/MC/MCSectionELF.h"
15 #include "llvm/Support/ELF.h"
16 #include "llvm/Target/TargetMachine.h"
17
18 using namespace llvm;
19
20
21 void XCoreTargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &TM){
22   TargetLoweringObjectFileELF::Initialize(Ctx, TM);
23
24   BSSSection =
25     Ctx.getELFSection(".dp.bss", ELF::SHT_NOBITS,
26                       ELF::SHF_ALLOC | ELF::SHF_WRITE |
27                       ELF::XCORE_SHF_DP_SECTION,
28                       SectionKind::getBSS());
29   BSSSectionLarge =
30     Ctx.getELFSection(".dp.bss.large", ELF::SHT_NOBITS,
31                       ELF::SHF_ALLOC | ELF::SHF_WRITE |
32                       ELF::XCORE_SHF_DP_SECTION,
33                       SectionKind::getBSS());
34   DataSection =
35     Ctx.getELFSection(".dp.data", ELF::SHT_PROGBITS,
36                       ELF::SHF_ALLOC | ELF::SHF_WRITE |
37                       ELF::XCORE_SHF_DP_SECTION,
38                       SectionKind::getDataRel());
39   DataSectionLarge =
40     Ctx.getELFSection(".dp.data.large", ELF::SHT_PROGBITS,
41                       ELF::SHF_ALLOC | ELF::SHF_WRITE |
42                       ELF::XCORE_SHF_DP_SECTION,
43                       SectionKind::getDataRel());
44   // This is the wrong place to decide if const data should be placed
45   // in the .cp or .dp section.
46   // Ideally we should set up DataRelROSection to use the '.dp.'' and use this
47   // for const data, unless the front end explicitly states a '.cp.'' section.
48   ReadOnlySection =
49     Ctx.getELFSection(".cp.rodata", ELF::SHT_PROGBITS,
50                       ELF::SHF_ALLOC |
51                       ELF::XCORE_SHF_CP_SECTION,
52                       SectionKind::getReadOnlyWithRel());
53   ReadOnlySectionLarge =
54     Ctx.getELFSection(".cp.rodata.large", ELF::SHT_PROGBITS,
55                       ELF::SHF_ALLOC |
56                       ELF::XCORE_SHF_CP_SECTION,
57                       SectionKind::getReadOnlyWithRel());
58   MergeableConst4Section = 
59     Ctx.getELFSection(".cp.rodata.cst4", ELF::SHT_PROGBITS,
60                       ELF::SHF_ALLOC | ELF::SHF_MERGE |
61                       ELF::XCORE_SHF_CP_SECTION,
62                       SectionKind::getMergeableConst4());
63   MergeableConst8Section = 
64     Ctx.getELFSection(".cp.rodata.cst8", ELF::SHT_PROGBITS,
65                       ELF::SHF_ALLOC | ELF::SHF_MERGE |
66                       ELF::XCORE_SHF_CP_SECTION,
67                       SectionKind::getMergeableConst8());
68   MergeableConst16Section = 
69     Ctx.getELFSection(".cp.rodata.cst16", ELF::SHT_PROGBITS,
70                       ELF::SHF_ALLOC | ELF::SHF_MERGE |
71                       ELF::XCORE_SHF_CP_SECTION,
72                       SectionKind::getMergeableConst16());
73   CStringSection =
74     Ctx.getELFSection(".cp.rodata.string", ELF::SHT_PROGBITS,
75                       ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::SHF_STRINGS |
76                       ELF::XCORE_SHF_CP_SECTION,
77                       SectionKind::getReadOnlyWithRel());
78   // TextSection       - see MObjectFileInfo.cpp
79   // StaticCtorSection - see MObjectFileInfo.cpp
80   // StaticDtorSection - see MObjectFileInfo.cpp
81  }
82
83 static SectionKind getXCoreKindForNamedSection(StringRef Name, SectionKind K) {
84   if (Name.startswith(".cp."))
85     return SectionKind::getReadOnly();
86   return K;
87 }
88
89 static unsigned getXCoreSectionType(SectionKind K) {
90   if (K.isBSS())
91     return ELF::SHT_NOBITS;
92   return ELF::SHT_PROGBITS;
93 }
94
95 static unsigned getXCoreSectionFlags(SectionKind K) {
96   unsigned Flags = 0;
97
98   if (!K.isMetadata())
99     Flags |= ELF::SHF_ALLOC;
100
101   if (K.isText())
102     Flags |= ELF::SHF_EXECINSTR;
103   else if (K.isReadOnly())
104     Flags |= ELF::XCORE_SHF_CP_SECTION;
105   else
106     Flags |= ELF::XCORE_SHF_DP_SECTION;
107
108   if (K.isWriteable())
109     Flags |= ELF::SHF_WRITE;
110
111   if (K.isMergeableCString() || K.isMergeableConst4() ||
112       K.isMergeableConst8() || K.isMergeableConst16())
113     Flags |= ELF::SHF_MERGE;
114
115   if (K.isMergeableCString())
116     Flags |= ELF::SHF_STRINGS;
117
118   return Flags;
119 }
120
121 const MCSection *XCoreTargetObjectFile::
122 getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
123                          Mangler *Mang, const TargetMachine &TM) const {
124   StringRef SectionName = GV->getSection();
125   // Infer section flags from the section name if we can.
126   Kind = getXCoreKindForNamedSection(SectionName, Kind);
127   return getContext().getELFSection(SectionName, getXCoreSectionType(Kind),
128                                     getXCoreSectionFlags(Kind), Kind);
129 }
130
131 const MCSection *XCoreTargetObjectFile::
132 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, Mangler *Mang,
133                        const TargetMachine &TM) const{
134   if (Kind.isText())                      return TextSection;
135   if (Kind.isMergeable1ByteCString())     return CStringSection;
136   if (Kind.isMergeableConst4())           return MergeableConst4Section;
137   if (Kind.isMergeableConst8())           return MergeableConst8Section;
138   if (Kind.isMergeableConst16())          return MergeableConst16Section;
139
140   Type *ObjType = GV->getType()->getPointerElementType();
141   if (TM.getCodeModel() == CodeModel::Small ||
142       !ObjType->isSized() ||
143       TM.getDataLayout()->getTypeAllocSize(ObjType) < CodeModelLargeSize) {
144     if (Kind.isReadOnly())                return ReadOnlySection;
145     if (Kind.isBSS())                     return BSSSection;
146     if (Kind.isDataRel())                 return DataSection;
147     if (Kind.isReadOnlyWithRel())         return ReadOnlySection;
148   } else {
149     if (Kind.isReadOnly())                return ReadOnlySectionLarge;
150     if (Kind.isBSS())                     return BSSSectionLarge;
151     if (Kind.isDataRel())                 return DataSectionLarge;
152     if (Kind.isReadOnlyWithRel())         return ReadOnlySectionLarge;
153   }
154
155   assert((Kind.isThreadLocal() || Kind.isCommon()) && "Unknown section kind");
156   report_fatal_error("Target does not support TLS or Common sections");
157 }
158
159 const MCSection *XCoreTargetObjectFile::
160 getSectionForConstant(SectionKind Kind) const {
161   if (Kind.isMergeableConst4())           return MergeableConst4Section;
162   if (Kind.isMergeableConst8())           return MergeableConst8Section;
163   if (Kind.isMergeableConst16())          return MergeableConst16Section;
164   assert((Kind.isReadOnly() || Kind.isReadOnlyWithRel()) &&
165          "Unknown section kind");
166   // We assume the size of the object is never greater than CodeModelLargeSize.
167   // To handle CodeModelLargeSize changes to AsmPrinter would be required.
168   return ReadOnlySection;
169 }