LLVM  14.0.0git
XCoreTargetObjectFile.cpp
Go to the documentation of this file.
1 //===-- XCoreTargetObjectFile.cpp - XCore object files --------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
10 #include "XCoreSubtarget.h"
11 #include "llvm/BinaryFormat/ELF.h"
12 #include "llvm/IR/DataLayout.h"
13 #include "llvm/MC/MCContext.h"
14 #include "llvm/MC/MCSectionELF.h"
16 
17 using namespace llvm;
18 
19 
22 
23  BSSSection = Ctx.getELFSection(".dp.bss", ELF::SHT_NOBITS,
26  BSSSectionLarge = Ctx.getELFSection(".dp.bss.large", ELF::SHT_NOBITS,
32  DataSectionLarge = Ctx.getELFSection(".dp.data.large", ELF::SHT_PROGBITS,
38  DataRelROSectionLarge = Ctx.getELFSection(
39  ".dp.rodata.large", ELF::SHT_PROGBITS,
42  Ctx.getELFSection(".cp.rodata", ELF::SHT_PROGBITS,
44  ReadOnlySectionLarge =
45  Ctx.getELFSection(".cp.rodata.large", ELF::SHT_PROGBITS,
48  ".cp.rodata.cst4", ELF::SHT_PROGBITS,
51  ".cp.rodata.cst8", ELF::SHT_PROGBITS,
54  ".cp.rodata.cst16", ELF::SHT_PROGBITS,
57  Ctx.getELFSection(".cp.rodata.string", ELF::SHT_PROGBITS,
60  // TextSection - see MObjectFileInfo.cpp
61  // StaticCtorSection - see MObjectFileInfo.cpp
62  // StaticDtorSection - see MObjectFileInfo.cpp
63  }
64 
65 static unsigned getXCoreSectionType(SectionKind K) {
66  if (K.isBSS())
67  return ELF::SHT_NOBITS;
68  return ELF::SHT_PROGBITS;
69 }
70 
71 static unsigned getXCoreSectionFlags(SectionKind K, bool IsCPRel) {
72  unsigned Flags = 0;
73 
74  if (!K.isMetadata())
75  Flags |= ELF::SHF_ALLOC;
76 
77  if (K.isText())
78  Flags |= ELF::SHF_EXECINSTR;
79  else if (IsCPRel)
81  else
83 
84  if (K.isWriteable())
85  Flags |= ELF::SHF_WRITE;
86 
87  if (K.isMergeableCString() || K.isMergeableConst4() ||
89  Flags |= ELF::SHF_MERGE;
90 
91  if (K.isMergeableCString())
92  Flags |= ELF::SHF_STRINGS;
93 
94  return Flags;
95 }
96 
98  const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
100  // Infer section flags from the section name if we can.
101  bool IsCPRel = SectionName.startswith(".cp.");
102  if (IsCPRel && !Kind.isReadOnly())
103  report_fatal_error("Using .cp. section for writeable object.");
105  getXCoreSectionFlags(Kind, IsCPRel));
106 }
107 
109  const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
110 
111  bool UseCPRel = GO->hasLocalLinkage();
112 
113  if (Kind.isText()) return TextSection;
114  if (UseCPRel) {
115  if (Kind.isMergeable1ByteCString()) return CStringSection;
116  if (Kind.isMergeableConst4()) return MergeableConst4Section;
117  if (Kind.isMergeableConst8()) return MergeableConst8Section;
118  if (Kind.isMergeableConst16()) return MergeableConst16Section;
119  }
120  Type *ObjType = GO->getValueType();
121  auto &DL = GO->getParent()->getDataLayout();
122  if (TM.getCodeModel() == CodeModel::Small || !ObjType->isSized() ||
123  DL.getTypeAllocSize(ObjType) < CodeModelLargeSize) {
124  if (Kind.isReadOnly()) return UseCPRel? ReadOnlySection
126  if (Kind.isBSS() || Kind.isCommon())return BSSSection;
127  if (Kind.isData())
128  return DataSection;
129  if (Kind.isReadOnlyWithRel()) return DataRelROSection;
130  } else {
131  if (Kind.isReadOnly()) return UseCPRel? ReadOnlySectionLarge
132  : DataRelROSectionLarge;
133  if (Kind.isBSS() || Kind.isCommon())return BSSSectionLarge;
134  if (Kind.isData())
135  return DataSectionLarge;
136  if (Kind.isReadOnlyWithRel()) return DataRelROSectionLarge;
137  }
138 
139  assert((Kind.isThreadLocal() || Kind.isCommon()) && "Unknown section kind");
140  report_fatal_error("Target does not support TLS or Common sections");
141 }
142 
144  const DataLayout &DL, SectionKind Kind, const Constant *C,
145  Align &Alignment) const {
146  if (Kind.isMergeableConst4()) return MergeableConst4Section;
147  if (Kind.isMergeableConst8()) return MergeableConst8Section;
148  if (Kind.isMergeableConst16()) return MergeableConst16Section;
149  assert((Kind.isReadOnly() || Kind.isReadOnlyWithRel()) &&
150  "Unknown section kind");
151  // We assume the size of the object is never greater than CodeModelLargeSize.
152  // To handle CodeModelLargeSize changes to AsmPrinter would be required.
153  return ReadOnlySection;
154 }
llvm::SectionKind::isText
bool isText() const
Definition: SectionKind.h:121
llvm::SectionKind::isWriteable
bool isWriteable() const
Definition: SectionKind.h:147
llvm::Type::isSized
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
Definition: Type.h:274
llvm::SectionKind::isMergeableConst8
bool isMergeableConst8() const
Definition: SectionKind.h:143
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::DataLayout
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:112
llvm::MCObjectFileInfo::MergeableConst16Section
MCSection * MergeableConst16Section
Definition: MCObjectFileInfo.h:185
llvm::MCObjectFileInfo::ReadOnlySection
MCSection * ReadOnlySection
Section that is readonly and can contain arbitrary initialized data.
Definition: MCObjectFileInfo.h:67
llvm::MCContext
Context object for machine code objects.
Definition: MCContext.h:72
llvm::SectionKind::isBSS
bool isBSS() const
Definition: SectionKind.h:163
llvm::GlobalObject::getSection
StringRef getSection() const
Get the custom section of this global if it has one.
Definition: GlobalObject.h:112
MCSectionELF.h
llvm::SectionKind::isMergeableConst16
bool isMergeableConst16() const
Definition: SectionKind.h:144
llvm::ELF::XCORE_SHF_DP_SECTION
@ XCORE_SHF_DP_SECTION
All sections with the "d" flag are grouped together by the linker to form the data section and the dp...
Definition: ELF.h:1034
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
llvm::ELF::SHF_EXECINSTR
@ SHF_EXECINSTR
Definition: ELF.h:991
getXCoreSectionType
static unsigned getXCoreSectionType(SectionKind K)
Definition: XCoreTargetObjectFile.cpp:65
XCoreTargetObjectFile.h
llvm::MCObjectFileInfo::getContext
MCContext & getContext() const
Definition: MCObjectFileInfo.h:235
llvm::ELF::SHT_PROGBITS
@ SHT_PROGBITS
Definition: ELF.h:910
llvm::XCoreTargetObjectFile::Initialize
void Initialize(MCContext &Ctx, const TargetMachine &TM) override
This method must be called before any actual lowering is done.
Definition: XCoreTargetObjectFile.cpp:20
ELF.h
TargetMachine.h
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::MCObjectFileInfo::MergeableConst8Section
MCSection * MergeableConst8Section
Definition: MCObjectFileInfo.h:184
llvm::GlobalObject
Definition: GlobalObject.h:28
MCContext.h
llvm::MCContext::getELFSection
MCSectionELF * getELFSection(const Twine &Section, unsigned Type, unsigned Flags)
Definition: MCContext.h:540
llvm::CodeModel::Small
@ Small
Definition: CodeGen.h:28
llvm::report_fatal_error
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:140
XCoreSubtarget.h
llvm::Align
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
llvm::lltok::Kind
Kind
Definition: LLToken.h:18
llvm::XCoreTargetObjectFile::SelectSectionForGlobal
MCSection * SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const override
Definition: XCoreTargetObjectFile.cpp:108
llvm::TargetLoweringObjectFile::TM
const TargetMachine * TM
Definition: TargetLoweringObjectFile.h:66
llvm::Constant
This is an important base class in LLVM.
Definition: Constant.h:41
llvm::GlobalValue::getParent
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:572
llvm::SectionKind::isMetadata
bool isMetadata() const
Definition: SectionKind.h:119
llvm::MCObjectFileInfo::CStringSection
MCSection * CStringSection
Definition: MCObjectFileInfo.h:200
llvm::ELF::SHF_WRITE
@ SHF_WRITE
Definition: ELF.h:985
llvm::SectionKind::isMergeableCString
bool isMergeableCString() const
Definition: SectionKind.h:130
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::XCoreTargetObjectFile::getSectionForConstant
MCSection * getSectionForConstant(const DataLayout &DL, SectionKind Kind, const Constant *C, Align &Alignment) const override
Given a constant with the SectionKind, return a section that it should be placed in.
Definition: XCoreTargetObjectFile.cpp:143
llvm::TargetMachine
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:79
llvm::GlobalValue::hasLocalLinkage
bool hasLocalLinkage() const
Definition: GlobalValue.h:445
llvm::ELF::SHF_ALLOC
@ SHF_ALLOC
Definition: ELF.h:988
DataLayout.h
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition: AArch64SLSHardening.cpp:76
llvm::MCSection
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:39
llvm::ELF::SHF_STRINGS
@ SHF_STRINGS
Definition: ELF.h:997
llvm::ELF::SHF_MERGE
@ SHF_MERGE
Definition: ELF.h:994
getXCoreSectionFlags
static unsigned getXCoreSectionFlags(SectionKind K, bool IsCPRel)
Definition: XCoreTargetObjectFile.cpp:71
llvm::CodeModelLargeSize
static const unsigned CodeModelLargeSize
Definition: XCoreTargetObjectFile.h:16
llvm::MCObjectFileInfo::DataRelROSection
MCSection * DataRelROSection
Definition: MCObjectFileInfo.h:182
llvm::ELF::XCORE_SHF_CP_SECTION
@ XCORE_SHF_CP_SECTION
All sections with the "c" flag are grouped together by the linker to form the constant pool and the c...
Definition: ELF.h:1039
llvm::SectionKind
SectionKind - This is a simple POD value that classifies the properties of a section.
Definition: SectionKind.h:22
llvm::MCObjectFileInfo::DataSection
MCSection * DataSection
Section directive for standard data.
Definition: MCObjectFileInfo.h:58
llvm::SectionName
Definition: DWARFSection.h:21
llvm::TargetLoweringObjectFileELF::Initialize
void Initialize(MCContext &Ctx, const TargetMachine &TM) override
This method must be called before any actual lowering is done.
Definition: TargetLoweringObjectFileImpl.cpp:116
llvm::TargetMachine::getCodeModel
CodeModel::Model getCodeModel() const
Returns the code model.
Definition: TargetMachine.cpp:74
llvm::MCObjectFileInfo::BSSSection
MCSection * BSSSection
Section that is default initialized to zero.
Definition: MCObjectFileInfo.h:61
llvm::MCObjectFileInfo::MergeableConst4Section
MCSection * MergeableConst4Section
Definition: MCObjectFileInfo.h:183
llvm::SectionKind::isMergeableConst4
bool isMergeableConst4() const
Definition: SectionKind.h:142
llvm::Module::getDataLayout
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.cpp:401
llvm::GlobalValue::getValueType
Type * getValueType() const
Definition: GlobalValue.h:273
TM
const char LLVMTargetMachineRef TM
Definition: PassBuilderBindings.cpp:47
llvm::XCoreTargetObjectFile::getExplicitSectionGlobal
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...
Definition: XCoreTargetObjectFile.cpp:97
llvm::ELF::SHT_NOBITS
@ SHT_NOBITS
Definition: ELF.h:917
llvm::MCObjectFileInfo::TextSection
MCSection * TextSection
Section directive for standard text.
Definition: MCObjectFileInfo.h:55