blob: 4c748c5b57cde4efa2b507274b9b8d3e5e626c0a [file] [log] [blame]
Jia Liuc5707112012-02-17 08:55: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"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000012#include "llvm/IR/DataLayout.h"
13#include "llvm/IR/DerivedTypes.h"
14#include "llvm/IR/GlobalVariable.h"
Chris Lattner287df1b2010-04-08 21:34:17 +000015#include "llvm/MC/MCContext.h"
Chris Lattnerb71b9092009-08-13 06:28:06 +000016#include "llvm/MC/MCSectionELF.h"
Chris Lattnerb71b9092009-08-13 06:28:06 +000017#include "llvm/Support/CommandLine.h"
Rafael Espindolac85dca62011-01-23 04:28:49 +000018#include "llvm/Support/ELF.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000019#include "llvm/Target/TargetMachine.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);
Logan Chienfd91d8d2012-09-05 06:17:17 +000029 InitializeELF(TM.Options.UseInitArray);
Che-Liang Chiou36919ac2010-09-28 09:55:24 +000030
Chris Lattnerb71b9092009-08-13 06:28:06 +000031 SmallDataSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +000032 getContext().getELFSection(".sdata", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000033 ELF::SHF_WRITE |ELF::SHF_ALLOC,
Chris Lattner287df1b2010-04-08 21:34:17 +000034 SectionKind::getDataRel());
Che-Liang Chiou36919ac2010-09-28 09:55:24 +000035
Chris Lattnerb71b9092009-08-13 06:28:06 +000036 SmallBSSSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +000037 getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000038 ELF::SHF_WRITE |ELF::SHF_ALLOC,
Chris Lattner287df1b2010-04-08 21:34:17 +000039 SectionKind::getBSS());
Che-Liang Chiou36919ac2010-09-28 09:55:24 +000040
Jack Carterc91cbb92013-01-18 21:20:38 +000041 // Register info information
42 const MipsSubtarget &Subtarget = TM.getSubtarget<MipsSubtarget>();
43 if (Subtarget.isABI_N64() || Subtarget.isABI_N32())
44 ReginfoSection =
45 getContext().getELFSection(".MIPS.options",
46 ELF::SHT_MIPS_OPTIONS,
47 ELF::SHF_ALLOC |ELF::SHF_MIPS_NOSTRIP,
48 SectionKind::getMetadata());
49 else
50 ReginfoSection =
51 getContext().getELFSection(".reginfo",
52 ELF::SHT_MIPS_REGINFO,
53 ELF::SHF_ALLOC,
54 SectionKind::getMetadata());
Chris Lattnerb71b9092009-08-13 06:28:06 +000055}
56
Che-Liang Chiou36919ac2010-09-28 09:55:24 +000057// A address must be loaded from a small section if its size is less than the
58// small section size threshold. Data in this section must be addressed using
Chris Lattnerb71b9092009-08-13 06:28:06 +000059// gp_rel operator.
60static bool IsInSmallSection(uint64_t Size) {
61 return Size > 0 && Size <= SSThreshold;
62}
63
64bool MipsTargetObjectFile::IsGlobalInSmallSection(const GlobalValue *GV,
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000065 const TargetMachine &TM) const {
Chris Lattnerb71b9092009-08-13 06:28:06 +000066 if (GV->isDeclaration() || GV->hasAvailableExternallyLinkage())
67 return false;
Che-Liang Chiou36919ac2010-09-28 09:55:24 +000068
Chris Lattnerb71b9092009-08-13 06:28:06 +000069 return IsGlobalInSmallSection(GV, TM, getKindForGlobal(GV, TM));
70}
71
72/// IsGlobalInSmallSection - Return true if this global address should be
73/// placed into small data/bss section.
74bool MipsTargetObjectFile::
75IsGlobalInSmallSection(const GlobalValue *GV, const TargetMachine &TM,
76 SectionKind Kind) const {
Bruno Cardoso Lopes0046de02009-11-19 05:28:18 +000077
Bruno Cardoso Lopes0046de02009-11-19 05:28:18 +000078 const MipsSubtarget &Subtarget = TM.getSubtarget<MipsSubtarget>();
Akira Hatanakae7338cd2012-08-22 03:18:13 +000079
80 // Return if small section is not available.
81 if (!Subtarget.useSmallSection())
Bruno Cardoso Lopes0046de02009-11-19 05:28:18 +000082 return false;
83
Chris Lattnerb71b9092009-08-13 06:28:06 +000084 // Only global variables, not functions.
85 const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GV);
86 if (!GVA)
87 return false;
Che-Liang Chiou36919ac2010-09-28 09:55:24 +000088
Chris Lattnerb71b9092009-08-13 06:28:06 +000089 // We can only do this for datarel or BSS objects for now.
90 if (!Kind.isBSS() && !Kind.isDataRel())
91 return false;
Che-Liang Chiou36919ac2010-09-28 09:55:24 +000092
Chris Lattnerb71b9092009-08-13 06:28:06 +000093 // If this is a internal constant string, there is a special
94 // section for it, but not in small data/bss.
95 if (Kind.isMergeable1ByteCString())
96 return false;
97
Chris Lattnerdb125cf2011-07-18 04:54:35 +000098 Type *Ty = GV->getType()->getElementType();
Micah Villmow3574eca2012-10-08 16:38:25 +000099 return IsInSmallSection(TM.getDataLayout()->getTypeAllocSize(Ty));
Chris Lattnerb71b9092009-08-13 06:28:06 +0000100}
101
102
103
104const MCSection *MipsTargetObjectFile::
105SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
106 Mangler *Mang, const TargetMachine &TM) const {
107 // TODO: Could also support "weak" symbols as well with ".gnu.linkonce.s.*"
108 // sections?
Che-Liang Chiou36919ac2010-09-28 09:55:24 +0000109
Chris Lattnerb71b9092009-08-13 06:28:06 +0000110 // Handle Small Section classification here.
111 if (Kind.isBSS() && IsGlobalInSmallSection(GV, TM, Kind))
112 return SmallBSSSection;
113 if (Kind.isDataNoRel() && IsGlobalInSmallSection(GV, TM, Kind))
114 return SmallDataSection;
Che-Liang Chiou36919ac2010-09-28 09:55:24 +0000115
Chris Lattnerb71b9092009-08-13 06:28:06 +0000116 // Otherwise, we work the same as ELF.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +0000117 return TargetLoweringObjectFileELF::SelectSectionForGlobal(GV, Kind, Mang,TM);
Chris Lattnerb71b9092009-08-13 06:28:06 +0000118}