blob: 05c46f5c97a5ca21eb9855995a950ec1a25aa89d [file] [log] [blame]
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001//===-- MipsTargetObjectFile.cpp - Mips object files ----------------------===//
Chris Lattnerb71b9092009-08-13 06:28:06 +00002//
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//
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00008//===----------------------------------------------------------------------===//
Chris Lattnerb71b9092009-08-13 06:28:06 +00009
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"
Rafael Espindolac85dca62011-01-23 04:28:49 +000019#include "llvm/Support/ELF.h"
Chris Lattnerb71b9092009-08-13 06:28:06 +000020using namespace llvm;
21
22static cl::opt<unsigned>
23SSThreshold("mips-ssection-threshold", cl::Hidden,
24 cl::desc("Small data and bss section threshold size (default=8)"),
25 cl::init(8));
26
27void MipsTargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &TM){
28 TargetLoweringObjectFileELF::Initialize(Ctx, TM);
Che-Liang Chiou36919ac2010-09-28 09:55:24 +000029
Chris Lattnerb71b9092009-08-13 06:28:06 +000030 SmallDataSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +000031 getContext().getELFSection(".sdata", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000032 ELF::SHF_WRITE |ELF::SHF_ALLOC,
Chris Lattner287df1b2010-04-08 21:34:17 +000033 SectionKind::getDataRel());
Che-Liang Chiou36919ac2010-09-28 09:55:24 +000034
Chris Lattnerb71b9092009-08-13 06:28:06 +000035 SmallBSSSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +000036 getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000037 ELF::SHF_WRITE |ELF::SHF_ALLOC,
Chris Lattner287df1b2010-04-08 21:34:17 +000038 SectionKind::getBSS());
Che-Liang Chiou36919ac2010-09-28 09:55:24 +000039
Chris Lattnerb71b9092009-08-13 06:28:06 +000040}
41
Che-Liang Chiou36919ac2010-09-28 09:55:24 +000042// A address must be loaded from a small section if its size is less than the
43// small section size threshold. Data in this section must be addressed using
Chris Lattnerb71b9092009-08-13 06:28:06 +000044// gp_rel operator.
45static bool IsInSmallSection(uint64_t Size) {
46 return Size > 0 && Size <= SSThreshold;
47}
48
49bool MipsTargetObjectFile::IsGlobalInSmallSection(const GlobalValue *GV,
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000050 const TargetMachine &TM) const {
Chris Lattnerb71b9092009-08-13 06:28:06 +000051 if (GV->isDeclaration() || GV->hasAvailableExternallyLinkage())
52 return false;
Che-Liang Chiou36919ac2010-09-28 09:55:24 +000053
Chris Lattnerb71b9092009-08-13 06:28:06 +000054 return IsGlobalInSmallSection(GV, TM, getKindForGlobal(GV, TM));
55}
56
57/// IsGlobalInSmallSection - Return true if this global address should be
58/// placed into small data/bss section.
59bool MipsTargetObjectFile::
60IsGlobalInSmallSection(const GlobalValue *GV, const TargetMachine &TM,
61 SectionKind Kind) const {
Bruno Cardoso Lopes0046de02009-11-19 05:28:18 +000062
63 // Only use small section for non linux targets.
64 const MipsSubtarget &Subtarget = TM.getSubtarget<MipsSubtarget>();
65 if (Subtarget.isLinux())
66 return false;
67
Chris Lattnerb71b9092009-08-13 06:28:06 +000068 // Only global variables, not functions.
69 const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GV);
70 if (!GVA)
71 return false;
Che-Liang Chiou36919ac2010-09-28 09:55:24 +000072
Chris Lattnerb71b9092009-08-13 06:28:06 +000073 // We can only do this for datarel or BSS objects for now.
74 if (!Kind.isBSS() && !Kind.isDataRel())
75 return false;
Che-Liang Chiou36919ac2010-09-28 09:55:24 +000076
Chris Lattnerb71b9092009-08-13 06:28:06 +000077 // If this is a internal constant string, there is a special
78 // section for it, but not in small data/bss.
79 if (Kind.isMergeable1ByteCString())
80 return false;
81
Chris Lattnerdb125cf2011-07-18 04:54:35 +000082 Type *Ty = GV->getType()->getElementType();
Chris Lattnerb71b9092009-08-13 06:28:06 +000083 return IsInSmallSection(TM.getTargetData()->getTypeAllocSize(Ty));
84}
85
86
87
88const MCSection *MipsTargetObjectFile::
89SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
90 Mangler *Mang, const TargetMachine &TM) const {
91 // TODO: Could also support "weak" symbols as well with ".gnu.linkonce.s.*"
92 // sections?
Che-Liang Chiou36919ac2010-09-28 09:55:24 +000093
Chris Lattnerb71b9092009-08-13 06:28:06 +000094 // Handle Small Section classification here.
95 if (Kind.isBSS() && IsGlobalInSmallSection(GV, TM, Kind))
96 return SmallBSSSection;
97 if (Kind.isDataNoRel() && IsGlobalInSmallSection(GV, TM, Kind))
98 return SmallDataSection;
Che-Liang Chiou36919ac2010-09-28 09:55:24 +000099
Chris Lattnerb71b9092009-08-13 06:28:06 +0000100 // Otherwise, we work the same as ELF.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000101 return TargetLoweringObjectFileELF::SelectSectionForGlobal(GV, Kind, Mang,TM);
Chris Lattnerb71b9092009-08-13 06:28:06 +0000102}