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