blob: 405f41981fa31c991f3a793c1b9dae7213f82d88 [file] [log] [blame]
Chris Lattnerb71b9092009-08-13 06:28:06 +00001//===-- MipsTargetObjectFile.cpp - Mips 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 "MipsTargetObjectFile.h"
Bruno Cardoso Lopes0046de02009-11-19 05:28:18 +000011#include "MipsSubtarget.h"
Chris Lattnerb71b9092009-08-13 06:28:06 +000012#include "llvm/DerivedTypes.h"
13#include "llvm/GlobalVariable.h"
Chris Lattner287df1b2010-04-08 21:34:17 +000014#include "llvm/MC/MCContext.h"
Chris Lattnerb71b9092009-08-13 06:28:06 +000015#include "llvm/MC/MCSectionELF.h"
16#include "llvm/Target/TargetData.h"
17#include "llvm/Target/TargetMachine.h"
18#include "llvm/Support/CommandLine.h"
19using namespace llvm;
20
21static cl::opt<unsigned>
22SSThreshold("mips-ssection-threshold", cl::Hidden,
23 cl::desc("Small data and bss section threshold size (default=8)"),
24 cl::init(8));
25
26void MipsTargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &TM){
27 TargetLoweringObjectFileELF::Initialize(Ctx, TM);
28
29 SmallDataSection =
Chris Lattner287df1b2010-04-08 21:34:17 +000030 getContext().getELFSection(".sdata", MCSectionELF::SHT_PROGBITS,
31 MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC,
32 SectionKind::getDataRel());
Chris Lattnerb71b9092009-08-13 06:28:06 +000033
34 SmallBSSSection =
Chris Lattner287df1b2010-04-08 21:34:17 +000035 getContext().getELFSection(".sbss", MCSectionELF::SHT_NOBITS,
36 MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC,
37 SectionKind::getBSS());
Chris Lattnerb71b9092009-08-13 06:28:06 +000038
39}
40
41// A address must be loaded from a small section if its size is less than the
42// small section size threshold. Data in this section must be addressed using
43// gp_rel operator.
44static bool IsInSmallSection(uint64_t Size) {
45 return Size > 0 && Size <= SSThreshold;
46}
47
48bool MipsTargetObjectFile::IsGlobalInSmallSection(const GlobalValue *GV,
49 const TargetMachine &TM) const {
50 if (GV->isDeclaration() || GV->hasAvailableExternallyLinkage())
51 return false;
52
53 return IsGlobalInSmallSection(GV, TM, getKindForGlobal(GV, TM));
54}
55
56/// IsGlobalInSmallSection - Return true if this global address should be
57/// placed into small data/bss section.
58bool MipsTargetObjectFile::
59IsGlobalInSmallSection(const GlobalValue *GV, const TargetMachine &TM,
60 SectionKind Kind) const {
Bruno Cardoso Lopes0046de02009-11-19 05:28:18 +000061
62 // Only use small section for non linux targets.
63 const MipsSubtarget &Subtarget = TM.getSubtarget<MipsSubtarget>();
64 if (Subtarget.isLinux())
65 return false;
66
Chris Lattnerb71b9092009-08-13 06:28:06 +000067 // Only global variables, not functions.
68 const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GV);
69 if (!GVA)
70 return false;
71
72 // We can only do this for datarel or BSS objects for now.
73 if (!Kind.isBSS() && !Kind.isDataRel())
74 return false;
75
76 // If this is a internal constant string, there is a special
77 // section for it, but not in small data/bss.
78 if (Kind.isMergeable1ByteCString())
79 return false;
80
81 const Type *Ty = GV->getType()->getElementType();
82 return IsInSmallSection(TM.getTargetData()->getTypeAllocSize(Ty));
83}
84
85
86
87const MCSection *MipsTargetObjectFile::
88SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
89 Mangler *Mang, const TargetMachine &TM) const {
90 // TODO: Could also support "weak" symbols as well with ".gnu.linkonce.s.*"
91 // sections?
92
93 // Handle Small Section classification here.
94 if (Kind.isBSS() && IsGlobalInSmallSection(GV, TM, Kind))
95 return SmallBSSSection;
96 if (Kind.isDataNoRel() && IsGlobalInSmallSection(GV, TM, Kind))
97 return SmallDataSection;
98
99 // Otherwise, we work the same as ELF.
100 return TargetLoweringObjectFileELF::SelectSectionForGlobal(GV, Kind, Mang,TM);
101}