1 //===-- XCoreTargetObjectFile.cpp - XCore object files --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
21 void XCoreTargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &TM){
22 TargetLoweringObjectFileELF::Initialize(Ctx, TM);
25 Ctx.getELFSection(".dp.bss", ELF::SHT_NOBITS,
26 ELF::SHF_ALLOC | ELF::SHF_WRITE |
27 ELF::XCORE_SHF_DP_SECTION,
28 SectionKind::getBSS());
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());
35 Ctx.getELFSection(".dp.data", ELF::SHT_PROGBITS,
36 ELF::SHF_ALLOC | ELF::SHF_WRITE |
37 ELF::XCORE_SHF_DP_SECTION,
38 SectionKind::getDataRel());
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());
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());
55 Ctx.getELFSection(".cp.rodata", ELF::SHT_PROGBITS,
57 ELF::XCORE_SHF_CP_SECTION,
58 SectionKind::getReadOnlyWithRel());
59 ReadOnlySectionLarge =
60 Ctx.getELFSection(".cp.rodata.large", ELF::SHT_PROGBITS,
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());
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
89 static unsigned getXCoreSectionType(SectionKind K) {
91 return ELF::SHT_NOBITS;
92 return ELF::SHT_PROGBITS;
95 static unsigned getXCoreSectionFlags(SectionKind K, bool IsCPRel) {
99 Flags |= ELF::SHF_ALLOC;
102 Flags |= ELF::SHF_EXECINSTR;
104 Flags |= ELF::XCORE_SHF_CP_SECTION;
106 Flags |= ELF::XCORE_SHF_DP_SECTION;
109 Flags |= ELF::SHF_WRITE;
111 if (K.isMergeableCString() || K.isMergeableConst4() ||
112 K.isMergeableConst8() || K.isMergeableConst16())
113 Flags |= ELF::SHF_MERGE;
115 if (K.isMergeableCString())
116 Flags |= ELF::SHF_STRINGS;
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 bool IsCPRel = SectionName.startswith(".cp.");
127 if (IsCPRel && !Kind.isReadOnly())
128 report_fatal_error("Using .cp. section for writeable object.");
129 return getContext().getELFSection(SectionName, getXCoreSectionType(Kind),
130 getXCoreSectionFlags(Kind, IsCPRel), Kind);
133 const MCSection *XCoreTargetObjectFile::
134 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, Mangler &Mang,
135 const TargetMachine &TM) const{
137 bool UseCPRel = GV->isLocalLinkage(GV->getLinkage());
139 if (Kind.isText()) return TextSection;
141 if (Kind.isMergeable1ByteCString()) return CStringSection;
142 if (Kind.isMergeableConst4()) return MergeableConst4Section;
143 if (Kind.isMergeableConst8()) return MergeableConst8Section;
144 if (Kind.isMergeableConst16()) return MergeableConst16Section;
146 Type *ObjType = GV->getType()->getPointerElementType();
147 if (TM.getCodeModel() == CodeModel::Small ||
148 !ObjType->isSized() ||
149 TM.getDataLayout()->getTypeAllocSize(ObjType) < CodeModelLargeSize) {
150 if (Kind.isReadOnly()) return UseCPRel? ReadOnlySection
152 if (Kind.isBSS() || Kind.isCommon())return BSSSection;
153 if (Kind.isDataRel()) return DataSection;
154 if (Kind.isReadOnlyWithRel()) return DataRelROSection;
156 if (Kind.isReadOnly()) return UseCPRel? ReadOnlySectionLarge
157 : DataRelROSectionLarge;
158 if (Kind.isBSS() || Kind.isCommon())return BSSSectionLarge;
159 if (Kind.isDataRel()) return DataSectionLarge;
160 if (Kind.isReadOnlyWithRel()) return DataRelROSectionLarge;
163 assert((Kind.isThreadLocal() || Kind.isCommon()) && "Unknown section kind");
164 report_fatal_error("Target does not support TLS or Common sections");
167 const MCSection *XCoreTargetObjectFile::
168 getSectionForConstant(SectionKind Kind) const {
169 if (Kind.isMergeableConst4()) return MergeableConst4Section;
170 if (Kind.isMergeableConst8()) return MergeableConst8Section;
171 if (Kind.isMergeableConst16()) return MergeableConst16Section;
172 assert((Kind.isReadOnly() || Kind.isReadOnlyWithRel()) &&
173 "Unknown section kind");
174 // We assume the size of the object is never greater than CodeModelLargeSize.
175 // To handle CodeModelLargeSize changes to AsmPrinter would be required.
176 return ReadOnlySection;