LLVM  8.0.1
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/BinaryFormat/ELF.h"
13 #include "llvm/IR/DataLayout.h"
14 #include "llvm/MC/MCContext.h"
15 #include "llvm/MC/MCSectionELF.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 
99  const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
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));
107 }
108 
110  const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
111 
112  bool UseCPRel = GO->hasLocalLinkage();
113 
114  if (Kind.isText()) return TextSection;
115  if (UseCPRel) {
116  if (Kind.isMergeable1ByteCString()) return CStringSection;
117  if (Kind.isMergeableConst4()) return MergeableConst4Section;
118  if (Kind.isMergeableConst8()) return MergeableConst8Section;
119  if (Kind.isMergeableConst16()) return MergeableConst16Section;
120  }
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;
128  if (Kind.isData())
129  return DataSection;
130  if (Kind.isReadOnlyWithRel()) return DataRelROSection;
131  } else {
132  if (Kind.isReadOnly()) return UseCPRel? ReadOnlySectionLarge
133  : DataRelROSectionLarge;
134  if (Kind.isBSS() || Kind.isCommon())return BSSSectionLarge;
135  if (Kind.isData())
136  return DataSectionLarge;
137  if (Kind.isReadOnlyWithRel()) return DataRelROSectionLarge;
138  }
139 
140  assert((Kind.isThreadLocal() || Kind.isCommon()) && "Unknown section kind");
141  report_fatal_error("Target does not support TLS or Common sections");
142 }
143 
146  const Constant *C,
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;
156 }
uint64_t CallInst * C
StringRef getSection() const
Get the custom section of this global if it has one.
Definition: GlobalObject.h:90
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:39
MCSection * MergeableConst4Section
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:111
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
bool hasLocalLinkage() const
Definition: GlobalValue.h:436
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:140
This class represents lattice values for constants.
Definition: AllocatorList.h:24
bool isWriteable() const
Definition: SectionKind.h:145
All sections with the "d" flag are grouped together by the linker to form the data section and the dp...
Definition: ELF.h:926
bool isSized(SmallPtrSetImpl< Type *> *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
Definition: Type.h:265
MCSection * TextSection
Section directive for standard text.
bool isMergeableConst8() const
Definition: SectionKind.h:141
bool isMergeableCString() const
Definition: SectionKind.h:128
static unsigned getXCoreSectionFlags(SectionKind K, bool IsCPRel)
All sections with the "c" flag are grouped together by the linker to form the constant pool and the c...
Definition: ELF.h:931
void Initialize(MCContext &Ctx, const TargetMachine &TM) override
This method must be called before any actual lowering is done.
MCSection * SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const override
const DataLayout & getDataLayout() const
Get the data layout for the module&#39;s target platform.
Definition: Module.cpp:371
MCSection * MergeableConst16Section
MCSection * getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const override
Targets should implement this method to assign a section to globals with an explicit section specfied...
static const unsigned CodeModelLargeSize
Context object for machine code objects.
Definition: MCContext.h:63
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:267
bool isText() const
Definition: SectionKind.h:119
MCSection * DataSection
Section directive for standard data.
bool isReadOnlyWithRel() const
Definition: SectionKind.h:168
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
This is an important base class in LLVM.
Definition: Constant.h:42
SectionKind - This is a simple POD value that classifies the properties of a section.
Definition: SectionKind.h:23
bool isMergeableConst16() const
Definition: SectionKind.h:142
bool isBSS() const
Definition: SectionKind.h:160
bool isMergeableConst4() const
Definition: SectionKind.h:140
bool isCommon() const
Definition: SectionKind.h:164
bool isMetadata() const
Definition: SectionKind.h:117
MCSection * getSectionForConstant(const DataLayout &DL, SectionKind Kind, const Constant *C, unsigned &Align) const override
Given a constant with the SectionKind, return a section that it should be placed in.
CodeModel::Model getCodeModel() const
Returns the code model.
bool isReadOnly() const
Definition: SectionKind.h:123
static unsigned getXCoreSectionType(SectionKind K)
Type * getValueType() const
Definition: GlobalValue.h:276
const unsigned Kind
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
MCSectionELF * getELFSection(const Twine &Section, unsigned Type, unsigned Flags)
Definition: MCContext.h:389
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:566
bool isData() const
Definition: SectionKind.h:166
bool isThreadLocal() const
Definition: SectionKind.h:149
bool isMergeable1ByteCString() const
Definition: SectionKind.h:132
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:59
MCSection * BSSSection
Section that is default initialized to zero.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
MCSection * ReadOnlySection
Section that is readonly and can contain arbitrary initialized data.
void Initialize(MCContext &Ctx, const TargetMachine &TM) override
This method must be called before any actual lowering is done.
MCSection * MergeableConst8Section