blob: a476c5c3244aef4b87281929e63504d01c029ccd [file] [log] [blame]
Jia Liu9f610112012-02-17 08:55:11 +00001//===-- MipsTargetObjectFile.cpp - Mips Object Files ----------------------===//
Chris Lattner68535f72009-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 Hatanakae2489122011-04-15 21:51:11 +00008//===----------------------------------------------------------------------===//
Chris Lattner68535f72009-08-13 06:28:06 +00009
10#include "MipsTargetObjectFile.h"
Bruno Cardoso Lopes8bd87232009-11-19 05:28:18 +000011#include "MipsSubtarget.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000012#include "llvm/IR/DataLayout.h"
13#include "llvm/IR/DerivedTypes.h"
14#include "llvm/IR/GlobalVariable.h"
Chris Lattner80c34592010-04-08 21:34:17 +000015#include "llvm/MC/MCContext.h"
Chris Lattner68535f72009-08-13 06:28:06 +000016#include "llvm/MC/MCSectionELF.h"
Chris Lattner68535f72009-08-13 06:28:06 +000017#include "llvm/Support/CommandLine.h"
Rafael Espindolaaea49582011-01-23 04:28:49 +000018#include "llvm/Support/ELF.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/Target/TargetMachine.h"
Chris Lattner68535f72009-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);
Logan Chieneeaaf652012-09-05 06:17:17 +000029 InitializeELF(TM.Options.UseInitArray);
Che-Liang Chioubb033892010-09-28 09:55:24 +000030
Chris Lattner68535f72009-08-13 06:28:06 +000031 SmallDataSection =
Rafael Espindolaaea49582011-01-23 04:28:49 +000032 getContext().getELFSection(".sdata", ELF::SHT_PROGBITS,
Rafael Espindola0e7e34e2011-01-23 04:43:11 +000033 ELF::SHF_WRITE |ELF::SHF_ALLOC,
Chris Lattner80c34592010-04-08 21:34:17 +000034 SectionKind::getDataRel());
Che-Liang Chioubb033892010-09-28 09:55:24 +000035
Chris Lattner68535f72009-08-13 06:28:06 +000036 SmallBSSSection =
Rafael Espindolaaea49582011-01-23 04:28:49 +000037 getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
Rafael Espindola0e7e34e2011-01-23 04:43:11 +000038 ELF::SHF_WRITE |ELF::SHF_ALLOC,
Chris Lattner80c34592010-04-08 21:34:17 +000039 SectionKind::getBSS());
Chris Lattner68535f72009-08-13 06:28:06 +000040}
41
Che-Liang Chioubb033892010-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 Lattner68535f72009-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 Hatanakae2489122011-04-15 21:51:11 +000050 const TargetMachine &TM) const {
Chris Lattner68535f72009-08-13 06:28:06 +000051 if (GV->isDeclaration() || GV->hasAvailableExternallyLinkage())
52 return false;
Che-Liang Chioubb033892010-09-28 09:55:24 +000053
Chris Lattner68535f72009-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 Lopes8bd87232009-11-19 05:28:18 +000062
Bruno Cardoso Lopes8bd87232009-11-19 05:28:18 +000063 const MipsSubtarget &Subtarget = TM.getSubtarget<MipsSubtarget>();
Akira Hatanakaad495022012-08-22 03:18:13 +000064
65 // Return if small section is not available.
66 if (!Subtarget.useSmallSection())
Bruno Cardoso Lopes8bd87232009-11-19 05:28:18 +000067 return false;
68
Chris Lattner68535f72009-08-13 06:28:06 +000069 // Only global variables, not functions.
70 const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GV);
71 if (!GVA)
72 return false;
Che-Liang Chioubb033892010-09-28 09:55:24 +000073
Chris Lattner68535f72009-08-13 06:28:06 +000074 // We can only do this for datarel or BSS objects for now.
75 if (!Kind.isBSS() && !Kind.isDataRel())
76 return false;
Che-Liang Chioubb033892010-09-28 09:55:24 +000077
Chris Lattner68535f72009-08-13 06:28:06 +000078 // If this is a internal constant string, there is a special
79 // section for it, but not in small data/bss.
80 if (Kind.isMergeable1ByteCString())
81 return false;
82
Chris Lattner229907c2011-07-18 04:54:35 +000083 Type *Ty = GV->getType()->getElementType();
Micah Villmowcdfe20b2012-10-08 16:38:25 +000084 return IsInSmallSection(TM.getDataLayout()->getTypeAllocSize(Ty));
Chris Lattner68535f72009-08-13 06:28:06 +000085}
86
87
88
89const MCSection *MipsTargetObjectFile::
90SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Eric Christopher2037caf2014-01-28 00:49:26 +000091 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattner68535f72009-08-13 06:28:06 +000092 // TODO: Could also support "weak" symbols as well with ".gnu.linkonce.s.*"
93 // sections?
Che-Liang Chioubb033892010-09-28 09:55:24 +000094
Chris Lattner68535f72009-08-13 06:28:06 +000095 // Handle Small Section classification here.
96 if (Kind.isBSS() && IsGlobalInSmallSection(GV, TM, Kind))
97 return SmallBSSSection;
98 if (Kind.isDataNoRel() && IsGlobalInSmallSection(GV, TM, Kind))
99 return SmallDataSection;
Che-Liang Chioubb033892010-09-28 09:55:24 +0000100
Chris Lattner68535f72009-08-13 06:28:06 +0000101 // Otherwise, we work the same as ELF.
Akira Hatanakae2489122011-04-15 21:51:11 +0000102 return TargetLoweringObjectFileELF::SelectSectionForGlobal(GV, Kind, Mang,TM);
Chris Lattner68535f72009-08-13 06:28:06 +0000103}