2011-04-15 21:51: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"
|
2009-08-13 06:28:06 +00:00
|
|
|
#include "llvm/DerivedTypes.h"
|
|
|
|
#include "llvm/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/Target/TargetData.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
2011-01-23 04:28:49 +00:00
|
|
|
#include "llvm/Support/ELF.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));
|
|
|
|
|
|
|
|
void MipsTargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &TM){
|
|
|
|
TargetLoweringObjectFileELF::Initialize(Ctx, TM);
|
2010-09-28 09:55:24 +00:00
|
|
|
|
2009-08-13 06:28:06 +00:00
|
|
|
SmallDataSection =
|
2011-01-23 04:28:49 +00:00
|
|
|
getContext().getELFSection(".sdata", ELF::SHT_PROGBITS,
|
2011-01-23 04:43:11 +00:00
|
|
|
ELF::SHF_WRITE |ELF::SHF_ALLOC,
|
2010-04-08 21:34:17 +00:00
|
|
|
SectionKind::getDataRel());
|
2010-09-28 09:55:24 +00:00
|
|
|
|
2009-08-13 06:28:06 +00:00
|
|
|
SmallBSSSection =
|
2011-01-23 04:28:49 +00:00
|
|
|
getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
|
2011-01-23 04:43:11 +00:00
|
|
|
ELF::SHF_WRITE |ELF::SHF_ALLOC,
|
2010-04-08 21:34:17 +00:00
|
|
|
SectionKind::getBSS());
|
2010-09-28 09:55:24 +00:00
|
|
|
|
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) {
|
|
|
|
return Size > 0 && Size <= SSThreshold;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MipsTargetObjectFile::IsGlobalInSmallSection(const GlobalValue *GV,
|
2011-04-15 21:51:11 +00:00
|
|
|
const TargetMachine &TM) const {
|
2009-08-13 06:28:06 +00:00
|
|
|
if (GV->isDeclaration() || GV->hasAvailableExternallyLinkage())
|
|
|
|
return false;
|
2010-09-28 09:55:24 +00:00
|
|
|
|
2009-08-13 06:28:06 +00:00
|
|
|
return IsGlobalInSmallSection(GV, TM, getKindForGlobal(GV, TM));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// IsGlobalInSmallSection - Return true if this global address should be
|
|
|
|
/// placed into small data/bss section.
|
|
|
|
bool MipsTargetObjectFile::
|
|
|
|
IsGlobalInSmallSection(const GlobalValue *GV, const TargetMachine &TM,
|
|
|
|
SectionKind Kind) const {
|
2009-11-19 05:28:18 +00:00
|
|
|
|
|
|
|
// Only use small section for non linux targets.
|
|
|
|
const MipsSubtarget &Subtarget = TM.getSubtarget<MipsSubtarget>();
|
|
|
|
if (Subtarget.isLinux())
|
|
|
|
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
|
|
|
|
2009-08-13 06:28:06 +00:00
|
|
|
// We can only do this for datarel or BSS objects for now.
|
|
|
|
if (!Kind.isBSS() && !Kind.isDataRel())
|
|
|
|
return false;
|
2010-09-28 09:55:24 +00:00
|
|
|
|
2009-08-13 06:28:06 +00:00
|
|
|
// If this is a internal constant string, there is a special
|
|
|
|
// section for it, but not in small data/bss.
|
|
|
|
if (Kind.isMergeable1ByteCString())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const Type *Ty = GV->getType()->getElementType();
|
|
|
|
return IsInSmallSection(TM.getTargetData()->getTypeAllocSize(Ty));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const MCSection *MipsTargetObjectFile::
|
|
|
|
SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
|
|
|
|
Mangler *Mang, const TargetMachine &TM) const {
|
|
|
|
// 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;
|
|
|
|
if (Kind.isDataNoRel() && IsGlobalInSmallSection(GV, TM, Kind))
|
|
|
|
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
|
|
|
}
|