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/BinaryFormat/ELF.h"
13 #include "llvm/IR/DataLayout.h"
14 #include "llvm/MC/MCContext.h"
15 #include "llvm/MC/MCSectionELF.h"
16 #include "llvm/Target/TargetMachine.h"
21 void XCoreTargetObjectFile::Initialize(MCContext
&Ctx
, const TargetMachine
&TM
){
22 TargetLoweringObjectFileELF::Initialize(Ctx
, TM
);
24 BSSSection
= Ctx
.getELFSection(".dp.bss", ELF::SHT_NOBITS
,
25 ELF::SHF_ALLOC
| ELF::SHF_WRITE
|
26 ELF::XCORE_SHF_DP_SECTION
);
27 BSSSectionLarge
= Ctx
.getELFSection(".dp.bss.large", ELF::SHT_NOBITS
,
28 ELF::SHF_ALLOC
| ELF::SHF_WRITE
|
29 ELF::XCORE_SHF_DP_SECTION
);
30 DataSection
= Ctx
.getELFSection(".dp.data", ELF::SHT_PROGBITS
,
31 ELF::SHF_ALLOC
| ELF::SHF_WRITE
|
32 ELF::XCORE_SHF_DP_SECTION
);
33 DataSectionLarge
= Ctx
.getELFSection(".dp.data.large", ELF::SHT_PROGBITS
,
34 ELF::SHF_ALLOC
| ELF::SHF_WRITE
|
35 ELF::XCORE_SHF_DP_SECTION
);
36 DataRelROSection
= Ctx
.getELFSection(".dp.rodata", ELF::SHT_PROGBITS
,
37 ELF::SHF_ALLOC
| ELF::SHF_WRITE
|
38 ELF::XCORE_SHF_DP_SECTION
);
39 DataRelROSectionLarge
= Ctx
.getELFSection(
40 ".dp.rodata.large", ELF::SHT_PROGBITS
,
41 ELF::SHF_ALLOC
| ELF::SHF_WRITE
| ELF::XCORE_SHF_DP_SECTION
);
43 Ctx
.getELFSection(".cp.rodata", ELF::SHT_PROGBITS
,
44 ELF::SHF_ALLOC
| ELF::XCORE_SHF_CP_SECTION
);
45 ReadOnlySectionLarge
=
46 Ctx
.getELFSection(".cp.rodata.large", ELF::SHT_PROGBITS
,
47 ELF::SHF_ALLOC
| ELF::XCORE_SHF_CP_SECTION
);
48 MergeableConst4Section
= Ctx
.getELFSection(
49 ".cp.rodata.cst4", ELF::SHT_PROGBITS
,
50 ELF::SHF_ALLOC
| ELF::SHF_MERGE
| ELF::XCORE_SHF_CP_SECTION
, 4, "");
51 MergeableConst8Section
= Ctx
.getELFSection(
52 ".cp.rodata.cst8", ELF::SHT_PROGBITS
,
53 ELF::SHF_ALLOC
| ELF::SHF_MERGE
| ELF::XCORE_SHF_CP_SECTION
, 8, "");
54 MergeableConst16Section
= Ctx
.getELFSection(
55 ".cp.rodata.cst16", ELF::SHT_PROGBITS
,
56 ELF::SHF_ALLOC
| ELF::SHF_MERGE
| ELF::XCORE_SHF_CP_SECTION
, 16, "");
58 Ctx
.getELFSection(".cp.rodata.string", ELF::SHT_PROGBITS
,
59 ELF::SHF_ALLOC
| ELF::SHF_MERGE
| ELF::SHF_STRINGS
|
60 ELF::XCORE_SHF_CP_SECTION
);
61 // TextSection - see MObjectFileInfo.cpp
62 // StaticCtorSection - see MObjectFileInfo.cpp
63 // StaticDtorSection - see MObjectFileInfo.cpp
66 static unsigned getXCoreSectionType(SectionKind K
) {
68 return ELF::SHT_NOBITS
;
69 return ELF::SHT_PROGBITS
;
72 static unsigned getXCoreSectionFlags(SectionKind K
, bool IsCPRel
) {
76 Flags
|= ELF::SHF_ALLOC
;
79 Flags
|= ELF::SHF_EXECINSTR
;
81 Flags
|= ELF::XCORE_SHF_CP_SECTION
;
83 Flags
|= ELF::XCORE_SHF_DP_SECTION
;
86 Flags
|= ELF::SHF_WRITE
;
88 if (K
.isMergeableCString() || K
.isMergeableConst4() ||
89 K
.isMergeableConst8() || K
.isMergeableConst16())
90 Flags
|= ELF::SHF_MERGE
;
92 if (K
.isMergeableCString())
93 Flags
|= ELF::SHF_STRINGS
;
98 MCSection
*XCoreTargetObjectFile::getExplicitSectionGlobal(
99 const GlobalObject
*GO
, SectionKind Kind
, const TargetMachine
&TM
) const {
100 StringRef SectionName
= GO
->getSection();
101 // Infer section flags from the section name if we can.
102 bool IsCPRel
= SectionName
.startswith(".cp.");
103 if (IsCPRel
&& !Kind
.isReadOnly())
104 report_fatal_error("Using .cp. section for writeable object.");
105 return getContext().getELFSection(SectionName
, getXCoreSectionType(Kind
),
106 getXCoreSectionFlags(Kind
, IsCPRel
));
109 MCSection
*XCoreTargetObjectFile::SelectSectionForGlobal(
110 const GlobalObject
*GO
, SectionKind Kind
, const TargetMachine
&TM
) const {
112 bool UseCPRel
= GO
->hasLocalLinkage();
114 if (Kind
.isText()) return TextSection
;
116 if (Kind
.isMergeable1ByteCString()) return CStringSection
;
117 if (Kind
.isMergeableConst4()) return MergeableConst4Section
;
118 if (Kind
.isMergeableConst8()) return MergeableConst8Section
;
119 if (Kind
.isMergeableConst16()) return MergeableConst16Section
;
121 Type
*ObjType
= GO
->getValueType();
122 auto &DL
= GO
->getParent()->getDataLayout();
123 if (TM
.getCodeModel() == CodeModel::Small
|| !ObjType
->isSized() ||
124 DL
.getTypeAllocSize(ObjType
) < CodeModelLargeSize
) {
125 if (Kind
.isReadOnly()) return UseCPRel
? ReadOnlySection
127 if (Kind
.isBSS() || Kind
.isCommon())return BSSSection
;
130 if (Kind
.isReadOnlyWithRel()) return DataRelROSection
;
132 if (Kind
.isReadOnly()) return UseCPRel
? ReadOnlySectionLarge
133 : DataRelROSectionLarge
;
134 if (Kind
.isBSS() || Kind
.isCommon())return BSSSectionLarge
;
136 return DataSectionLarge
;
137 if (Kind
.isReadOnlyWithRel()) return DataRelROSectionLarge
;
140 assert((Kind
.isThreadLocal() || Kind
.isCommon()) && "Unknown section kind");
141 report_fatal_error("Target does not support TLS or Common sections");
144 MCSection
*XCoreTargetObjectFile::getSectionForConstant(const DataLayout
&DL
,
147 unsigned &Align
) const {
148 if (Kind
.isMergeableConst4()) return MergeableConst4Section
;
149 if (Kind
.isMergeableConst8()) return MergeableConst8Section
;
150 if (Kind
.isMergeableConst16()) return MergeableConst16Section
;
151 assert((Kind
.isReadOnly() || Kind
.isReadOnlyWithRel()) &&
152 "Unknown section kind");
153 // We assume the size of the object is never greater than CodeModelLargeSize.
154 // To handle CodeModelLargeSize changes to AsmPrinter would be required.
155 return ReadOnlySection
;