LLVM  3.7.0
XCoreTargetObjectFile.cpp
Go to the documentation of this file.
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"
17 
18 using namespace llvm;
19 
20 
23 
24  BSSSection = Ctx.getELFSection(".dp.bss", ELF::SHT_NOBITS,
27  BSSSectionLarge = Ctx.getELFSection(".dp.bss.large", ELF::SHT_NOBITS,
33  DataSectionLarge = Ctx.getELFSection(".dp.data.large", ELF::SHT_PROGBITS,
39  DataRelROSectionLarge = Ctx.getELFSection(
40  ".dp.rodata.large", ELF::SHT_PROGBITS,
43  Ctx.getELFSection(".cp.rodata", ELF::SHT_PROGBITS,
45  ReadOnlySectionLarge =
46  Ctx.getELFSection(".cp.rodata.large", ELF::SHT_PROGBITS,
49  ".cp.rodata.cst4", ELF::SHT_PROGBITS,
52  ".cp.rodata.cst8", ELF::SHT_PROGBITS,
55  ".cp.rodata.cst16", ELF::SHT_PROGBITS,
58  Ctx.getELFSection(".cp.rodata.string", ELF::SHT_PROGBITS,
61  // TextSection - see MObjectFileInfo.cpp
62  // StaticCtorSection - see MObjectFileInfo.cpp
63  // StaticDtorSection - see MObjectFileInfo.cpp
64  }
65 
66 static unsigned getXCoreSectionType(SectionKind K) {
67  if (K.isBSS())
68  return ELF::SHT_NOBITS;
69  return ELF::SHT_PROGBITS;
70 }
71 
72 static unsigned getXCoreSectionFlags(SectionKind K, bool IsCPRel) {
73  unsigned Flags = 0;
74 
75  if (!K.isMetadata())
76  Flags |= ELF::SHF_ALLOC;
77 
78  if (K.isText())
79  Flags |= ELF::SHF_EXECINSTR;
80  else if (IsCPRel)
82  else
84 
85  if (K.isWriteable())
86  Flags |= ELF::SHF_WRITE;
87 
88  if (K.isMergeableCString() || K.isMergeableConst4() ||
90  Flags |= ELF::SHF_MERGE;
91 
92  if (K.isMergeableCString())
93  Flags |= ELF::SHF_STRINGS;
94 
95  return Flags;
96 }
97 
98 MCSection *
100  SectionKind Kind, Mangler &Mang,
101  const TargetMachine &TM) const {
102  StringRef SectionName = GV->getSection();
103  // Infer section flags from the section name if we can.
104  bool IsCPRel = SectionName.startswith(".cp.");
105  if (IsCPRel && !Kind.isReadOnly())
106  report_fatal_error("Using .cp. section for writeable object.");
107  return getContext().getELFSection(SectionName, getXCoreSectionType(Kind),
108  getXCoreSectionFlags(Kind, IsCPRel));
109 }
110 
111 MCSection *
113  SectionKind Kind, Mangler &Mang,
114  const TargetMachine &TM) const {
115 
116  bool UseCPRel = GV->isLocalLinkage(GV->getLinkage());
117 
118  if (Kind.isText()) return TextSection;
119  if (UseCPRel) {
120  if (Kind.isMergeable1ByteCString()) return CStringSection;
121  if (Kind.isMergeableConst4()) return MergeableConst4Section;
122  if (Kind.isMergeableConst8()) return MergeableConst8Section;
123  if (Kind.isMergeableConst16()) return MergeableConst16Section;
124  }
125  Type *ObjType = GV->getType()->getPointerElementType();
126  if (TM.getCodeModel() == CodeModel::Small || !ObjType->isSized() ||
128  if (Kind.isReadOnly()) return UseCPRel? ReadOnlySection
130  if (Kind.isBSS() || Kind.isCommon())return BSSSection;
131  if (Kind.isDataRel()) return DataSection;
132  if (Kind.isReadOnlyWithRel()) return DataRelROSection;
133  } else {
134  if (Kind.isReadOnly()) return UseCPRel? ReadOnlySectionLarge
135  : DataRelROSectionLarge;
136  if (Kind.isBSS() || Kind.isCommon())return BSSSectionLarge;
137  if (Kind.isDataRel()) return DataSectionLarge;
138  if (Kind.isReadOnlyWithRel()) return DataRelROSectionLarge;
139  }
140 
141  assert((Kind.isThreadLocal() || Kind.isCommon()) && "Unknown section kind");
142  report_fatal_error("Target does not support TLS or Common sections");
143 }
144 
145 MCSection *
147  const Constant *C) 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;
156 }
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:48
MCSection * MergeableConst4Section
LinkageTypes getLinkage() const
Definition: GlobalValue.h:289
MCSectionELF * getELFSection(StringRef Section, unsigned Type, unsigned Flags)
Definition: MCContext.h:311
MCSection * TextSection
Section directive for standard text.
bool isBSS() const
Definition: SectionKind.h:175
bool isReadOnlyWithRel() const
Definition: SectionKind.h:191
static unsigned getXCoreSectionFlags(SectionKind K, bool IsCPRel)
const char * getSection() const
Definition: Globals.cpp:106
void Initialize(MCContext &Ctx, const TargetMachine &TM) override
This method must be called before any actual lowering is done.
bool isMergeableCString() const
Definition: SectionKind.h:144
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const char *reason, bool gen_crash_diag=true)
Reports a serious error, calling any installed error handler.
static bool isLocalLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:226
Type * getPointerElementType() const
Definition: Type.h:366
MCSection * MergeableConst16Section
virtual void Initialize(MCContext &ctx, const TargetMachine &TM)
This method must be called before any actual lowering is done.
MCSection * getSectionForConstant(SectionKind Kind, const Constant *C) const override
Given a constant with the SectionKind, return a section that it should be placed in.
bool isSized(SmallPtrSetImpl< const Type * > *Visited=nullptr) const
isSized - Return true if it makes sense to take the size of this type.
Definition: Type.h:268
static const unsigned CodeModelLargeSize
bool isWriteable() const
Definition: SectionKind.h:160
Context object for machine code objects.
Definition: MCContext.h:48
MCSection * SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, Mangler &Mang, const TargetMachine &TM) const override
MCSection * DataSection
Section directive for standard data.
bool isMergeableConst16() const
Definition: SectionKind.h:158
bool isMetadata() const
Definition: SectionKind.h:136
CodeModel::Model getCodeModel() const
Returns the code model.
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
bool isText() const
Definition: SectionKind.h:137
This is an important base class in LLVM.
Definition: Constant.h:41
MCSection * getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind, Mangler &Mang, const TargetMachine &TM) const override
Targets should implement this method to assign a section to globals with an explicit section specfied...
bool isMergeable1ByteCString() const
Definition: SectionKind.h:148
SectionKind - This is a simple POD value that classifies the properties of a section.
Definition: SectionKind.h:28
bool isThreadLocal() const
Definition: SectionKind.h:164
bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:215
uint64_t getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
Definition: DataLayout.h:388
const DataLayout * getDataLayout() const
Deprecated in 3.7, will be removed in 3.8.
bool isMergeableConst8() const
Definition: SectionKind.h:157
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:185
static unsigned getXCoreSectionType(SectionKind K)
bool isCommon() const
Definition: SectionKind.h:179
const ARM::ArchExtKind Kind
bool isMergeableConst4() const
Definition: SectionKind.h:156
XCORE_SHF_CP_SECTION - All sections with the "c" flag are grouped together by the linker to form the ...
Definition: Support/ELF.h:702
Primary interface to the complete machine description for the target machine.
MCSection * BSSSection
Section that is default initialized to zero.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
MCSection * ReadOnlySection
Section that is readonly and can contain arbitrary initialized data.
bool isDataRel() const
Definition: SectionKind.h:181
bool isReadOnly() const
Definition: SectionKind.h:139
XCORE_SHF_DP_SECTION - All sections with the "d" flag are grouped together by the linker to form the ...
Definition: Support/ELF.h:707
MCSection * MergeableConst8Section