XCoreTargetObjectFile.cpp revision 36b56886974eae4f9c5ebc96befd3e7bfe5de338
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
18using namespace llvm;
19
20
21void 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  DataRelROSection =
45    Ctx.getELFSection(".dp.rodata", ELF::SHT_PROGBITS,
46                      ELF::SHF_ALLOC | ELF::SHF_WRITE |
47                      ELF::XCORE_SHF_DP_SECTION,
48                      SectionKind::getReadOnlyWithRel());
49  DataRelROSectionLarge =
50    Ctx.getELFSection(".dp.rodata.large", ELF::SHT_PROGBITS,
51                      ELF::SHF_ALLOC | ELF::SHF_WRITE |
52                      ELF::XCORE_SHF_DP_SECTION,
53                      SectionKind::getReadOnlyWithRel());
54  ReadOnlySection =
55    Ctx.getELFSection(".cp.rodata", ELF::SHT_PROGBITS,
56                      ELF::SHF_ALLOC |
57                      ELF::XCORE_SHF_CP_SECTION,
58                      SectionKind::getReadOnlyWithRel());
59  ReadOnlySectionLarge =
60    Ctx.getELFSection(".cp.rodata.large", ELF::SHT_PROGBITS,
61                      ELF::SHF_ALLOC |
62                      ELF::XCORE_SHF_CP_SECTION,
63                      SectionKind::getReadOnlyWithRel());
64  MergeableConst4Section =
65    Ctx.getELFSection(".cp.rodata.cst4", ELF::SHT_PROGBITS,
66                      ELF::SHF_ALLOC | ELF::SHF_MERGE |
67                      ELF::XCORE_SHF_CP_SECTION,
68                      SectionKind::getMergeableConst4());
69  MergeableConst8Section =
70    Ctx.getELFSection(".cp.rodata.cst8", ELF::SHT_PROGBITS,
71                      ELF::SHF_ALLOC | ELF::SHF_MERGE |
72                      ELF::XCORE_SHF_CP_SECTION,
73                      SectionKind::getMergeableConst8());
74  MergeableConst16Section =
75    Ctx.getELFSection(".cp.rodata.cst16", ELF::SHT_PROGBITS,
76                      ELF::SHF_ALLOC | ELF::SHF_MERGE |
77                      ELF::XCORE_SHF_CP_SECTION,
78                      SectionKind::getMergeableConst16());
79  CStringSection =
80    Ctx.getELFSection(".cp.rodata.string", ELF::SHT_PROGBITS,
81                      ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::SHF_STRINGS |
82                      ELF::XCORE_SHF_CP_SECTION,
83                      SectionKind::getReadOnlyWithRel());
84  // TextSection       - see MObjectFileInfo.cpp
85  // StaticCtorSection - see MObjectFileInfo.cpp
86  // StaticDtorSection - see MObjectFileInfo.cpp
87 }
88
89static unsigned getXCoreSectionType(SectionKind K) {
90  if (K.isBSS())
91    return ELF::SHT_NOBITS;
92  return ELF::SHT_PROGBITS;
93}
94
95static unsigned getXCoreSectionFlags(SectionKind K, bool IsCPRel) {
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 (IsCPRel)
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
121const MCSection *
122XCoreTargetObjectFile::getExplicitSectionGlobal(const GlobalValue *GV,
123                                                SectionKind Kind, Mangler &Mang,
124                                                const TargetMachine &TM) const {
125  StringRef SectionName = GV->getSection();
126  // Infer section flags from the section name if we can.
127  bool IsCPRel = SectionName.startswith(".cp.");
128  if (IsCPRel && !Kind.isReadOnly())
129    report_fatal_error("Using .cp. section for writeable object.");
130  return getContext().getELFSection(SectionName, getXCoreSectionType(Kind),
131                                    getXCoreSectionFlags(Kind, IsCPRel), Kind);
132}
133
134const MCSection *XCoreTargetObjectFile::
135SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, Mangler &Mang,
136                       const TargetMachine &TM) const{
137
138  bool UseCPRel = GV->isLocalLinkage(GV->getLinkage());
139
140  if (Kind.isText())                    return TextSection;
141  if (UseCPRel) {
142    if (Kind.isMergeable1ByteCString()) return CStringSection;
143    if (Kind.isMergeableConst4())       return MergeableConst4Section;
144    if (Kind.isMergeableConst8())       return MergeableConst8Section;
145    if (Kind.isMergeableConst16())      return MergeableConst16Section;
146  }
147  Type *ObjType = GV->getType()->getPointerElementType();
148  if (TM.getCodeModel() == CodeModel::Small ||
149      !ObjType->isSized() ||
150      TM.getDataLayout()->getTypeAllocSize(ObjType) < CodeModelLargeSize) {
151    if (Kind.isReadOnly())              return UseCPRel? ReadOnlySection
152                                                       : DataRelROSection;
153    if (Kind.isBSS() || Kind.isCommon())return BSSSection;
154    if (Kind.isDataRel())               return DataSection;
155    if (Kind.isReadOnlyWithRel())       return DataRelROSection;
156  } else {
157    if (Kind.isReadOnly())              return UseCPRel? ReadOnlySectionLarge
158                                                       : DataRelROSectionLarge;
159    if (Kind.isBSS() || Kind.isCommon())return BSSSectionLarge;
160    if (Kind.isDataRel())               return DataSectionLarge;
161    if (Kind.isReadOnlyWithRel())       return DataRelROSectionLarge;
162  }
163
164  assert((Kind.isThreadLocal() || Kind.isCommon()) && "Unknown section kind");
165  report_fatal_error("Target does not support TLS or Common sections");
166}
167
168const MCSection *XCoreTargetObjectFile::
169getSectionForConstant(SectionKind Kind) const {
170  if (Kind.isMergeableConst4())           return MergeableConst4Section;
171  if (Kind.isMergeableConst8())           return MergeableConst8Section;
172  if (Kind.isMergeableConst16())          return MergeableConst16Section;
173  assert((Kind.isReadOnly() || Kind.isReadOnlyWithRel()) &&
174         "Unknown section kind");
175  // We assume the size of the object is never greater than CodeModelLargeSize.
176  // To handle CodeModelLargeSize changes to AsmPrinter would be required.
177  return ReadOnlySection;
178}
179