1 //===-- MBlazeTargetObjectFile.cpp - MBlaze 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 "MBlazeTargetObjectFile.h"
11 #include "MBlazeSubtarget.h"
12 #include "llvm/DerivedTypes.h"
13 #include "llvm/GlobalVariable.h"
14 #include "llvm/MC/MCContext.h"
15 #include "llvm/MC/MCSectionELF.h"
16 #include "llvm/Target/TargetData.h"
17 #include "llvm/Target/TargetMachine.h"
18 #include "llvm/Support/CommandLine.h"
21 void MBlazeTargetObjectFile::
22 Initialize(MCContext &Ctx, const TargetMachine &TM) {
23 TargetLoweringObjectFileELF::Initialize(Ctx, TM);
26 getContext().getELFSection(".sdata", MCSectionELF::SHT_PROGBITS,
27 MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC,
28 SectionKind::getDataRel());
31 getContext().getELFSection(".sbss", MCSectionELF::SHT_NOBITS,
32 MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC,
33 SectionKind::getBSS());
37 // A address must be loaded from a small section if its size is less than the
38 // small section size threshold. Data in this section must be addressed using
40 static bool IsInSmallSection(uint64_t Size) {
41 return Size > 0 && Size <= 8;
44 bool MBlazeTargetObjectFile::
45 IsGlobalInSmallSection(const GlobalValue *GV, const TargetMachine &TM) const {
46 if (GV->isDeclaration() || GV->hasAvailableExternallyLinkage())
49 return IsGlobalInSmallSection(GV, TM, getKindForGlobal(GV, TM));
52 /// IsGlobalInSmallSection - Return true if this global address should be
53 /// placed into small data/bss section.
54 bool MBlazeTargetObjectFile::
55 IsGlobalInSmallSection(const GlobalValue *GV, const TargetMachine &TM,
56 SectionKind Kind) const {
57 // Only global variables, not functions.
58 const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GV);
62 // We can only do this for datarel or BSS objects for now.
63 if (!Kind.isBSS() && !Kind.isDataRel())
66 // If this is a internal constant string, there is a special
67 // section for it, but not in small data/bss.
68 if (Kind.isMergeable1ByteCString())
71 const Type *Ty = GV->getType()->getElementType();
72 return IsInSmallSection(TM.getTargetData()->getTypeAllocSize(Ty));
75 const MCSection *MBlazeTargetObjectFile::
76 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
77 Mangler *Mang, const TargetMachine &TM) const {
78 // TODO: Could also support "weak" symbols as well with ".gnu.linkonce.s.*"
81 // Handle Small Section classification here.
82 if (Kind.isBSS() && IsGlobalInSmallSection(GV, TM, Kind))
83 return SmallBSSSection;
84 if (Kind.isDataNoRel() && IsGlobalInSmallSection(GV, TM, Kind))
85 return SmallDataSection;
87 // Otherwise, we work the same as ELF.
88 return TargetLoweringObjectFileELF::SelectSectionForGlobal(GV, Kind, Mang,TM);