1 //===-- MBlazeTargetObjectFile.cpp - MBlaze 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 "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"
19 #include "llvm/Support/ELF.h"
20 using namespace llvm;
21
22 void MBlazeTargetObjectFile::
Initialize(MCContext & Ctx,const TargetMachine & TM)23 Initialize(MCContext &Ctx, const TargetMachine &TM) {
24 TargetLoweringObjectFileELF::Initialize(Ctx, TM);
25
26 SmallDataSection =
27 getContext().getELFSection(".sdata", ELF::SHT_PROGBITS,
28 ELF::SHF_WRITE |ELF::SHF_ALLOC,
29 SectionKind::getDataRel());
30
31 SmallBSSSection =
32 getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
33 ELF::SHF_WRITE |ELF::SHF_ALLOC,
34 SectionKind::getBSS());
35
36 }
37
38 // A address must be loaded from a small section if its size is less than the
39 // small section size threshold. Data in this section must be addressed using
40 // gp_rel operator.
IsInSmallSection(uint64_t Size)41 static bool IsInSmallSection(uint64_t Size) {
42 return Size > 0 && Size <= 8;
43 }
44
45 bool MBlazeTargetObjectFile::
IsGlobalInSmallSection(const GlobalValue * GV,const TargetMachine & TM) const46 IsGlobalInSmallSection(const GlobalValue *GV, const TargetMachine &TM) const {
47 if (GV->isDeclaration() || GV->hasAvailableExternallyLinkage())
48 return false;
49
50 return IsGlobalInSmallSection(GV, TM, getKindForGlobal(GV, TM));
51 }
52
53 /// IsGlobalInSmallSection - Return true if this global address should be
54 /// placed into small data/bss section.
55 bool MBlazeTargetObjectFile::
IsGlobalInSmallSection(const GlobalValue * GV,const TargetMachine & TM,SectionKind Kind) const56 IsGlobalInSmallSection(const GlobalValue *GV, const TargetMachine &TM,
57 SectionKind Kind) const {
58 // Only global variables, not functions.
59 const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GV);
60 if (!GVA)
61 return false;
62
63 // We can only do this for datarel or BSS objects for now.
64 if (!Kind.isBSS() && !Kind.isDataRel())
65 return false;
66
67 // If this is a internal constant string, there is a special
68 // section for it, but not in small data/bss.
69 if (Kind.isMergeable1ByteCString())
70 return false;
71
72 Type *Ty = GV->getType()->getElementType();
73 return IsInSmallSection(TM.getTargetData()->getTypeAllocSize(Ty));
74 }
75
76 const MCSection *MBlazeTargetObjectFile::
SelectSectionForGlobal(const GlobalValue * GV,SectionKind Kind,Mangler * Mang,const TargetMachine & TM) const77 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
78 Mangler *Mang, const TargetMachine &TM) const {
79 // TODO: Could also support "weak" symbols as well with ".gnu.linkonce.s.*"
80 // sections?
81
82 // Handle Small Section classification here.
83 if (Kind.isBSS() && IsGlobalInSmallSection(GV, TM, Kind))
84 return SmallBSSSection;
85 if (Kind.isDataNoRel() && IsGlobalInSmallSection(GV, TM, Kind))
86 return SmallDataSection;
87
88 // Otherwise, we work the same as ELF.
89 return TargetLoweringObjectFileELF::SelectSectionForGlobal(GV, Kind, Mang,TM);
90 }
91