2012-02-17 08:55:11 +00:00
|
|
|
//===-- MipsTargetObjectFile.cpp - Mips Object Files ----------------------===//
|
2009-08-13 06:28:06 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
2011-04-15 21:51:11 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2009-08-13 06:28:06 +00:00
|
|
|
|
|
|
|
#include "MipsTargetObjectFile.h"
|
2009-11-19 05:28:18 +00:00
|
|
|
#include "MipsSubtarget.h"
|
2015-03-21 03:13:05 +00:00
|
|
|
#include "MipsTargetMachine.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
|
|
|
#include "llvm/IR/GlobalVariable.h"
|
2010-04-08 21:34:17 +00:00
|
|
|
#include "llvm/MC/MCContext.h"
|
2009-08-13 06:28:06 +00:00
|
|
|
#include "llvm/MC/MCSectionELF.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
2011-01-23 04:28:49 +00:00
|
|
|
#include "llvm/Support/ELF.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2009-08-13 06:28:06 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
static cl::opt<unsigned>
|
|
|
|
SSThreshold("mips-ssection-threshold", cl::Hidden,
|
|
|
|
cl::desc("Small data and bss section threshold size (default=8)"),
|
|
|
|
cl::init(8));
|
|
|
|
|
2014-11-06 13:20:12 +00:00
|
|
|
static cl::opt<bool>
|
|
|
|
LocalSData("mlocal-sdata", cl::Hidden,
|
|
|
|
cl::desc("MIPS: Use gp_rel for object-local data."),
|
|
|
|
cl::init(true));
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
ExternSData("mextern-sdata", cl::Hidden,
|
|
|
|
cl::desc("MIPS: Use gp_rel for data that is not defined by the "
|
|
|
|
"current object."),
|
|
|
|
cl::init(true));
|
|
|
|
|
2009-08-13 06:28:06 +00:00
|
|
|
void MipsTargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &TM){
|
|
|
|
TargetLoweringObjectFileELF::Initialize(Ctx, TM);
|
2012-09-05 06:17:17 +00:00
|
|
|
InitializeELF(TM.Options.UseInitArray);
|
2010-09-28 09:55:24 +00:00
|
|
|
|
2015-01-29 17:33:21 +00:00
|
|
|
SmallDataSection = getContext().getELFSection(
|
|
|
|
".sdata", ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
|
|
|
|
|
|
|
|
SmallBSSSection = getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
|
|
|
|
ELF::SHF_WRITE | ELF::SHF_ALLOC);
|
2015-03-21 03:13:05 +00:00
|
|
|
this->TM = &static_cast<const MipsTargetMachine &>(TM);
|
2009-08-13 06:28:06 +00:00
|
|
|
}
|
|
|
|
|
2010-09-28 09:55:24 +00:00
|
|
|
// A address must be loaded from a small section if its size is less than the
|
|
|
|
// small section size threshold. Data in this section must be addressed using
|
2009-08-13 06:28:06 +00:00
|
|
|
// gp_rel operator.
|
|
|
|
static bool IsInSmallSection(uint64_t Size) {
|
2014-11-06 13:20:12 +00:00
|
|
|
// gcc has traditionally not treated zero-sized objects as small data, so this
|
|
|
|
// is effectively part of the ABI.
|
2009-08-13 06:28:06 +00:00
|
|
|
return Size > 0 && Size <= SSThreshold;
|
|
|
|
}
|
|
|
|
|
2014-11-06 13:20:12 +00:00
|
|
|
/// Return true if this global address should be placed into small data/bss
|
|
|
|
/// section.
|
|
|
|
bool MipsTargetObjectFile::
|
|
|
|
IsGlobalInSmallSection(const GlobalValue *GV, const TargetMachine &TM) const {
|
|
|
|
// We first check the case where global is a declaration, because finding
|
|
|
|
// section kind using getKindForGlobal() is only allowed for global
|
|
|
|
// definitions.
|
2009-08-13 06:28:06 +00:00
|
|
|
if (GV->isDeclaration() || GV->hasAvailableExternallyLinkage())
|
2014-11-06 13:20:12 +00:00
|
|
|
return IsGlobalInSmallSectionImpl(GV, TM);
|
2010-09-28 09:55:24 +00:00
|
|
|
|
2009-08-13 06:28:06 +00:00
|
|
|
return IsGlobalInSmallSection(GV, TM, getKindForGlobal(GV, TM));
|
|
|
|
}
|
|
|
|
|
2014-11-06 13:20:12 +00:00
|
|
|
/// Return true if this global address should be placed into small data/bss
|
|
|
|
/// section.
|
2009-08-13 06:28:06 +00:00
|
|
|
bool MipsTargetObjectFile::
|
|
|
|
IsGlobalInSmallSection(const GlobalValue *GV, const TargetMachine &TM,
|
|
|
|
SectionKind Kind) const {
|
2014-11-06 13:20:12 +00:00
|
|
|
return (IsGlobalInSmallSectionImpl(GV, TM) &&
|
|
|
|
(Kind.isDataRel() || Kind.isBSS() || Kind.isCommon()));
|
|
|
|
}
|
2009-11-19 05:28:18 +00:00
|
|
|
|
2014-11-06 13:20:12 +00:00
|
|
|
/// Return true if this global address should be placed into small data/bss
|
|
|
|
/// section. This method does all the work, except for checking the section
|
|
|
|
/// kind.
|
|
|
|
bool MipsTargetObjectFile::
|
|
|
|
IsGlobalInSmallSectionImpl(const GlobalValue *GV,
|
|
|
|
const TargetMachine &TM) const {
|
2015-03-21 03:13:05 +00:00
|
|
|
const MipsSubtarget &Subtarget =
|
|
|
|
*static_cast<const MipsTargetMachine &>(TM).getSubtargetImpl();
|
2012-08-22 03:18:13 +00:00
|
|
|
|
|
|
|
// Return if small section is not available.
|
|
|
|
if (!Subtarget.useSmallSection())
|
2009-11-19 05:28:18 +00:00
|
|
|
return false;
|
|
|
|
|
2009-08-13 06:28:06 +00:00
|
|
|
// Only global variables, not functions.
|
|
|
|
const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GV);
|
|
|
|
if (!GVA)
|
|
|
|
return false;
|
2010-09-28 09:55:24 +00:00
|
|
|
|
2014-11-06 13:20:12 +00:00
|
|
|
// Enforce -mlocal-sdata.
|
|
|
|
if (!LocalSData && GV->hasLocalLinkage())
|
2009-08-13 06:28:06 +00:00
|
|
|
return false;
|
2010-09-28 09:55:24 +00:00
|
|
|
|
2014-11-06 13:20:12 +00:00
|
|
|
// Enforce -mextern-sdata.
|
|
|
|
if (!ExternSData && ((GV->hasExternalLinkage() && GV->isDeclaration()) ||
|
|
|
|
GV->hasCommonLinkage()))
|
2009-08-13 06:28:06 +00:00
|
|
|
return false;
|
|
|
|
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *Ty = GV->getType()->getElementType();
|
2015-01-26 19:03:15 +00:00
|
|
|
return IsInSmallSection(TM.getDataLayout()->getTypeAllocSize(Ty));
|
2009-08-13 06:28:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const MCSection *MipsTargetObjectFile::
|
|
|
|
SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
|
2014-02-08 14:53:28 +00:00
|
|
|
Mangler &Mang, const TargetMachine &TM) const {
|
2009-08-13 06:28:06 +00:00
|
|
|
// TODO: Could also support "weak" symbols as well with ".gnu.linkonce.s.*"
|
|
|
|
// sections?
|
2010-09-28 09:55:24 +00:00
|
|
|
|
2009-08-13 06:28:06 +00:00
|
|
|
// Handle Small Section classification here.
|
|
|
|
if (Kind.isBSS() && IsGlobalInSmallSection(GV, TM, Kind))
|
|
|
|
return SmallBSSSection;
|
2014-11-06 13:20:12 +00:00
|
|
|
if (Kind.isDataRel() && IsGlobalInSmallSection(GV, TM, Kind))
|
2009-08-13 06:28:06 +00:00
|
|
|
return SmallDataSection;
|
2010-09-28 09:55:24 +00:00
|
|
|
|
2009-08-13 06:28:06 +00:00
|
|
|
// Otherwise, we work the same as ELF.
|
2011-04-15 21:51:11 +00:00
|
|
|
return TargetLoweringObjectFileELF::SelectSectionForGlobal(GV, Kind, Mang,TM);
|
2009-08-13 06:28:06 +00:00
|
|
|
}
|
2014-11-06 13:20:12 +00:00
|
|
|
|
|
|
|
/// Return true if this constant should be placed into small data section.
|
|
|
|
bool MipsTargetObjectFile::
|
|
|
|
IsConstantInSmallSection(const Constant *CN, const TargetMachine &TM) const {
|
2015-03-21 03:13:05 +00:00
|
|
|
return (static_cast<const MipsTargetMachine &>(TM)
|
|
|
|
.getSubtargetImpl()
|
|
|
|
->useSmallSection() &&
|
|
|
|
LocalSData && IsInSmallSection(TM.getDataLayout()->getTypeAllocSize(
|
|
|
|
CN->getType())));
|
2014-11-06 13:20:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const MCSection *MipsTargetObjectFile::
|
|
|
|
getSectionForConstant(SectionKind Kind, const Constant *C) const {
|
|
|
|
if (IsConstantInSmallSection(C, *TM))
|
|
|
|
return SmallDataSection;
|
|
|
|
|
|
|
|
// Otherwise, we work the same as ELF.
|
|
|
|
return TargetLoweringObjectFileELF::getSectionForConstant(Kind, C);
|
|
|
|
}
|