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;
122 XCoreTargetObjectFile::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);
134 const MCSection *XCoreTargetObjectFile::
135 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, Mangler &Mang,
136 const TargetMachine &TM) const{
138 bool UseCPRel = GV->isLocalLinkage(GV->getLinkage());
140 if (Kind.isText()) return TextSection;
142 if (Kind.isMergeable1ByteCString()) return CStringSection;
143 if (Kind.isMergeableConst4()) return MergeableConst4Section;
144 if (Kind.isMergeableConst8()) return MergeableConst8Section;
145 if (Kind.isMergeableConst16()) return MergeableConst16Section;
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
153 if (Kind.isBSS() || Kind.isCommon())return BSSSection;
154 if (Kind.isDataRel()) return DataSection;
155 if (Kind.isReadOnlyWithRel()) return DataRelROSection;
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;
164 assert((Kind.isThreadLocal() || Kind.isCommon()) && "Unknown section kind");
165 report_fatal_error("Target does not support TLS or Common sections");
168 const MCSection *XCoreTargetObjectFile::
169 getSectionForConstant(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;