MipsTargetObjectFile.cpp revision ebe69fe11e48d322045d5949c83283927a0d790b
1c5707112e7635d1dd2f2cc9c4f42e79a51302ccaJia Liu//===-- MipsTargetObjectFile.cpp - Mips Object Files ----------------------===//
2b71b909bc76f48377fc96547d53a088346852600Chris Lattner//
3b71b909bc76f48377fc96547d53a088346852600Chris Lattner//                     The LLVM Compiler Infrastructure
4b71b909bc76f48377fc96547d53a088346852600Chris Lattner//
5b71b909bc76f48377fc96547d53a088346852600Chris Lattner// This file is distributed under the University of Illinois Open Source
6b71b909bc76f48377fc96547d53a088346852600Chris Lattner// License. See LICENSE.TXT for details.
7b71b909bc76f48377fc96547d53a088346852600Chris Lattner//
84552c9a3b34ad9b2085635266348d0d9b95514a6Akira Hatanaka//===----------------------------------------------------------------------===//
9b71b909bc76f48377fc96547d53a088346852600Chris Lattner
10b71b909bc76f48377fc96547d53a088346852600Chris Lattner#include "MipsTargetObjectFile.h"
110046de0550f781b7739ffd5a6fe138cb306a1cf4Bruno Cardoso Lopes#include "MipsSubtarget.h"
120b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/DataLayout.h"
130b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/DerivedTypes.h"
140b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/GlobalVariable.h"
15287df1bc0309962770b6c176f2d143795dd3cc2fChris Lattner#include "llvm/MC/MCContext.h"
16b71b909bc76f48377fc96547d53a088346852600Chris Lattner#include "llvm/MC/MCSectionELF.h"
17b71b909bc76f48377fc96547d53a088346852600Chris Lattner#include "llvm/Support/CommandLine.h"
18c85dca66e68c9fa6ffa8471c64113b12d8d94fb1Rafael Espindola#include "llvm/Support/ELF.h"
19d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Target/TargetMachine.h"
20b71b909bc76f48377fc96547d53a088346852600Chris Lattnerusing namespace llvm;
21b71b909bc76f48377fc96547d53a088346852600Chris Lattner
22b71b909bc76f48377fc96547d53a088346852600Chris Lattnerstatic cl::opt<unsigned>
23b71b909bc76f48377fc96547d53a088346852600Chris LattnerSSThreshold("mips-ssection-threshold", cl::Hidden,
24b71b909bc76f48377fc96547d53a088346852600Chris Lattner            cl::desc("Small data and bss section threshold size (default=8)"),
25b71b909bc76f48377fc96547d53a088346852600Chris Lattner            cl::init(8));
26b71b909bc76f48377fc96547d53a088346852600Chris Lattner
2737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinesstatic cl::opt<bool>
2837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen HinesLocalSData("mlocal-sdata", cl::Hidden,
2937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines           cl::desc("MIPS: Use gp_rel for object-local data."),
3037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines           cl::init(true));
3137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
3237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinesstatic cl::opt<bool>
3337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen HinesExternSData("mextern-sdata", cl::Hidden,
3437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines            cl::desc("MIPS: Use gp_rel for data that is not defined by the "
3537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines                     "current object."),
3637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines            cl::init(true));
3737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
38b71b909bc76f48377fc96547d53a088346852600Chris Lattnervoid MipsTargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &TM){
39b71b909bc76f48377fc96547d53a088346852600Chris Lattner  TargetLoweringObjectFileELF::Initialize(Ctx, TM);
40fd91d8dd7e18107b35c7332b92d636420517e3cbLogan Chien  InitializeELF(TM.Options.UseInitArray);
4136919ac8a57a691f07129bb903fe32ae182b68c2Che-Liang Chiou
42ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  SmallDataSection = getContext().getELFSection(
43ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines      ".sdata", ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
44ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
45ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  SmallBSSSection = getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
46ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines                                               ELF::SHF_WRITE | ELF::SHF_ALLOC);
4737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  this->TM = &TM;
48b71b909bc76f48377fc96547d53a088346852600Chris Lattner}
49b71b909bc76f48377fc96547d53a088346852600Chris Lattner
5036919ac8a57a691f07129bb903fe32ae182b68c2Che-Liang Chiou// A address must be loaded from a small section if its size is less than the
5136919ac8a57a691f07129bb903fe32ae182b68c2Che-Liang Chiou// small section size threshold. Data in this section must be addressed using
52b71b909bc76f48377fc96547d53a088346852600Chris Lattner// gp_rel operator.
53b71b909bc76f48377fc96547d53a088346852600Chris Lattnerstatic bool IsInSmallSection(uint64_t Size) {
5437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  // gcc has traditionally not treated zero-sized objects as small data, so this
5537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  // is effectively part of the ABI.
56b71b909bc76f48377fc96547d53a088346852600Chris Lattner  return Size > 0 && Size <= SSThreshold;
57b71b909bc76f48377fc96547d53a088346852600Chris Lattner}
58b71b909bc76f48377fc96547d53a088346852600Chris Lattner
5937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines/// Return true if this global address should be placed into small data/bss
6037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines/// section.
6137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinesbool MipsTargetObjectFile::
6237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen HinesIsGlobalInSmallSection(const GlobalValue *GV, const TargetMachine &TM) const {
6337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  // We first check the case where global is a declaration, because finding
6437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  // section kind using getKindForGlobal() is only allowed for global
6537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  // definitions.
66b71b909bc76f48377fc96547d53a088346852600Chris Lattner  if (GV->isDeclaration() || GV->hasAvailableExternallyLinkage())
6737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    return IsGlobalInSmallSectionImpl(GV, TM);
6836919ac8a57a691f07129bb903fe32ae182b68c2Che-Liang Chiou
69b71b909bc76f48377fc96547d53a088346852600Chris Lattner  return IsGlobalInSmallSection(GV, TM, getKindForGlobal(GV, TM));
70b71b909bc76f48377fc96547d53a088346852600Chris Lattner}
71b71b909bc76f48377fc96547d53a088346852600Chris Lattner
7237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines/// Return true if this global address should be placed into small data/bss
7337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines/// section.
74b71b909bc76f48377fc96547d53a088346852600Chris Lattnerbool MipsTargetObjectFile::
75b71b909bc76f48377fc96547d53a088346852600Chris LattnerIsGlobalInSmallSection(const GlobalValue *GV, const TargetMachine &TM,
76b71b909bc76f48377fc96547d53a088346852600Chris Lattner                       SectionKind Kind) const {
7737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  return (IsGlobalInSmallSectionImpl(GV, TM) &&
7837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines          (Kind.isDataRel() || Kind.isBSS() || Kind.isCommon()));
7937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines}
800046de0550f781b7739ffd5a6fe138cb306a1cf4Bruno Cardoso Lopes
8137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines/// Return true if this global address should be placed into small data/bss
8237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines/// section. This method does all the work, except for checking the section
8337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines/// kind.
8437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinesbool MipsTargetObjectFile::
8537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen HinesIsGlobalInSmallSectionImpl(const GlobalValue *GV,
8637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines                           const TargetMachine &TM) const {
870046de0550f781b7739ffd5a6fe138cb306a1cf4Bruno Cardoso Lopes  const MipsSubtarget &Subtarget = TM.getSubtarget<MipsSubtarget>();
88e7338cd550a4ccde6796d2987b482ea9f0e239efAkira Hatanaka
89e7338cd550a4ccde6796d2987b482ea9f0e239efAkira Hatanaka  // Return if small section is not available.
90e7338cd550a4ccde6796d2987b482ea9f0e239efAkira Hatanaka  if (!Subtarget.useSmallSection())
910046de0550f781b7739ffd5a6fe138cb306a1cf4Bruno Cardoso Lopes    return false;
920046de0550f781b7739ffd5a6fe138cb306a1cf4Bruno Cardoso Lopes
93b71b909bc76f48377fc96547d53a088346852600Chris Lattner  // Only global variables, not functions.
94b71b909bc76f48377fc96547d53a088346852600Chris Lattner  const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GV);
95b71b909bc76f48377fc96547d53a088346852600Chris Lattner  if (!GVA)
96b71b909bc76f48377fc96547d53a088346852600Chris Lattner    return false;
9736919ac8a57a691f07129bb903fe32ae182b68c2Che-Liang Chiou
9837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  // Enforce -mlocal-sdata.
9937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  if (!LocalSData && GV->hasLocalLinkage())
100b71b909bc76f48377fc96547d53a088346852600Chris Lattner    return false;
10136919ac8a57a691f07129bb903fe32ae182b68c2Che-Liang Chiou
10237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  // Enforce -mextern-sdata.
10337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  if (!ExternSData && ((GV->hasExternalLinkage() && GV->isDeclaration()) ||
10437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines                       GV->hasCommonLinkage()))
105b71b909bc76f48377fc96547d53a088346852600Chris Lattner    return false;
106b71b909bc76f48377fc96547d53a088346852600Chris Lattner
107db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  Type *Ty = GV->getType()->getElementType();
108ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  return IsInSmallSection(TM.getDataLayout()->getTypeAllocSize(Ty));
109b71b909bc76f48377fc96547d53a088346852600Chris Lattner}
110b71b909bc76f48377fc96547d53a088346852600Chris Lattner
111b71b909bc76f48377fc96547d53a088346852600Chris Lattnerconst MCSection *MipsTargetObjectFile::
112b71b909bc76f48377fc96547d53a088346852600Chris LattnerSelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
11336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                       Mangler &Mang, const TargetMachine &TM) const {
114b71b909bc76f48377fc96547d53a088346852600Chris Lattner  // TODO: Could also support "weak" symbols as well with ".gnu.linkonce.s.*"
115b71b909bc76f48377fc96547d53a088346852600Chris Lattner  // sections?
11636919ac8a57a691f07129bb903fe32ae182b68c2Che-Liang Chiou
117b71b909bc76f48377fc96547d53a088346852600Chris Lattner  // Handle Small Section classification here.
118b71b909bc76f48377fc96547d53a088346852600Chris Lattner  if (Kind.isBSS() && IsGlobalInSmallSection(GV, TM, Kind))
119b71b909bc76f48377fc96547d53a088346852600Chris Lattner    return SmallBSSSection;
12037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  if (Kind.isDataRel() && IsGlobalInSmallSection(GV, TM, Kind))
121b71b909bc76f48377fc96547d53a088346852600Chris Lattner    return SmallDataSection;
12236919ac8a57a691f07129bb903fe32ae182b68c2Che-Liang Chiou
123b71b909bc76f48377fc96547d53a088346852600Chris Lattner  // Otherwise, we work the same as ELF.
1244552c9a3b34ad9b2085635266348d0d9b95514a6Akira Hatanaka  return TargetLoweringObjectFileELF::SelectSectionForGlobal(GV, Kind, Mang,TM);
125b71b909bc76f48377fc96547d53a088346852600Chris Lattner}
12637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
12737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines/// Return true if this constant should be placed into small data section.
12837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinesbool MipsTargetObjectFile::
12937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen HinesIsConstantInSmallSection(const Constant *CN, const TargetMachine &TM) const {
130ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  return (
131ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines      TM.getSubtarget<MipsSubtarget>().useSmallSection() && LocalSData &&
132ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines      IsInSmallSection(TM.getDataLayout()->getTypeAllocSize(CN->getType())));
13337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines}
13437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
13537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinesconst MCSection *MipsTargetObjectFile::
13637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen HinesgetSectionForConstant(SectionKind Kind, const Constant *C) const {
13737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  if (IsConstantInSmallSection(C, *TM))
13837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    return SmallDataSection;
13937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
14037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  // Otherwise, we work the same as ELF.
14137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  return TargetLoweringObjectFileELF::getSectionForConstant(Kind, C);
14237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines}
143