2012-02-17 16:55:11 +08:00
|
|
|
//===-- MipsTargetObjectFile.cpp - Mips Object Files ----------------------===//
|
2009-08-13 14:28:06 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
2011-04-16 05:51:11 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2009-08-13 14:28:06 +08:00
|
|
|
|
|
|
|
#include "MipsTargetObjectFile.h"
|
2009-11-19 13:28:18 +08:00
|
|
|
#include "MipsSubtarget.h"
|
2015-03-21 11:13:05 +08:00
|
|
|
#include "MipsTargetMachine.h"
|
2017-06-07 11:48:56 +08:00
|
|
|
#include "llvm/BinaryFormat/ELF.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
|
|
|
#include "llvm/IR/GlobalVariable.h"
|
2010-04-09 05:34:17 +08:00
|
|
|
#include "llvm/MC/MCContext.h"
|
2009-08-13 14:28:06 +08:00
|
|
|
#include "llvm/MC/MCSectionELF.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2009-08-13 14:28:06 +08: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 21:20:12 +08: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));
|
|
|
|
|
2017-07-22 01:19:00 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
EmbeddedData("membedded-data", cl::Hidden,
|
|
|
|
cl::desc("MIPS: Try to allocate variables in the following"
|
|
|
|
" sections if possible: .rodata, .sdata, .data ."),
|
|
|
|
cl::init(false));
|
|
|
|
|
2009-08-13 14:28:06 +08:00
|
|
|
void MipsTargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &TM){
|
|
|
|
TargetLoweringObjectFileELF::Initialize(Ctx, TM);
|
2012-09-05 14:17:17 +08:00
|
|
|
InitializeELF(TM.Options.UseInitArray);
|
2010-09-28 17:55:24 +08:00
|
|
|
|
2015-01-30 01:33:21 +08:00
|
|
|
SmallDataSection = getContext().getELFSection(
|
2016-02-03 19:50:22 +08:00
|
|
|
".sdata", ELF::SHT_PROGBITS,
|
|
|
|
ELF::SHF_WRITE | ELF::SHF_ALLOC | ELF::SHF_MIPS_GPREL);
|
2015-01-30 01:33:21 +08:00
|
|
|
|
|
|
|
SmallBSSSection = getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
|
2016-02-03 19:50:22 +08:00
|
|
|
ELF::SHF_WRITE | ELF::SHF_ALLOC |
|
|
|
|
ELF::SHF_MIPS_GPREL);
|
2015-05-27 16:44:01 +08:00
|
|
|
this->TM = &static_cast<const MipsTargetMachine &>(TM);
|
2009-08-13 14:28:06 +08:00
|
|
|
}
|
|
|
|
|
2010-09-28 17:55:24 +08: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 14:28:06 +08:00
|
|
|
// gp_rel operator.
|
|
|
|
static bool IsInSmallSection(uint64_t Size) {
|
2014-11-06 21:20:12 +08:00
|
|
|
// gcc has traditionally not treated zero-sized objects as small data, so this
|
|
|
|
// is effectively part of the ABI.
|
2009-08-13 14:28:06 +08:00
|
|
|
return Size > 0 && Size <= SSThreshold;
|
|
|
|
}
|
|
|
|
|
2014-11-06 21:20:12 +08:00
|
|
|
/// Return true if this global address should be placed into small data/bss
|
|
|
|
/// section.
|
2016-10-25 03:23:39 +08:00
|
|
|
bool MipsTargetObjectFile::IsGlobalInSmallSection(
|
|
|
|
const GlobalObject *GO, const TargetMachine &TM) const {
|
2014-11-06 21:20:12 +08:00
|
|
|
// We first check the case where global is a declaration, because finding
|
|
|
|
// section kind using getKindForGlobal() is only allowed for global
|
|
|
|
// definitions.
|
2016-10-25 03:23:39 +08:00
|
|
|
if (GO->isDeclaration() || GO->hasAvailableExternallyLinkage())
|
|
|
|
return IsGlobalInSmallSectionImpl(GO, TM);
|
2010-09-28 17:55:24 +08:00
|
|
|
|
2016-10-25 03:23:39 +08:00
|
|
|
return IsGlobalInSmallSection(GO, TM, getKindForGlobal(GO, TM));
|
2009-08-13 14:28:06 +08:00
|
|
|
}
|
|
|
|
|
2014-11-06 21:20:12 +08:00
|
|
|
/// Return true if this global address should be placed into small data/bss
|
|
|
|
/// section.
|
2009-08-13 14:28:06 +08:00
|
|
|
bool MipsTargetObjectFile::
|
2016-10-25 03:23:39 +08:00
|
|
|
IsGlobalInSmallSection(const GlobalObject *GO, const TargetMachine &TM,
|
2009-08-13 14:28:06 +08:00
|
|
|
SectionKind Kind) const {
|
2017-07-22 01:19:00 +08:00
|
|
|
return IsGlobalInSmallSectionImpl(GO, TM) &&
|
|
|
|
(Kind.isData() || Kind.isBSS() || Kind.isCommon() ||
|
|
|
|
Kind.isReadOnly());
|
2014-11-06 21:20:12 +08:00
|
|
|
}
|
2009-11-19 13:28:18 +08:00
|
|
|
|
2014-11-06 21:20:12 +08: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::
|
2016-10-25 03:23:39 +08:00
|
|
|
IsGlobalInSmallSectionImpl(const GlobalObject *GO,
|
2014-11-06 21:20:12 +08:00
|
|
|
const TargetMachine &TM) const {
|
2015-03-21 11:13:05 +08:00
|
|
|
const MipsSubtarget &Subtarget =
|
|
|
|
*static_cast<const MipsTargetMachine &>(TM).getSubtargetImpl();
|
2012-08-22 11:18:13 +08:00
|
|
|
|
|
|
|
// Return if small section is not available.
|
|
|
|
if (!Subtarget.useSmallSection())
|
2009-11-19 13:28:18 +08:00
|
|
|
return false;
|
|
|
|
|
2009-08-13 14:28:06 +08:00
|
|
|
// Only global variables, not functions.
|
2016-10-25 03:23:39 +08:00
|
|
|
const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GO);
|
2009-08-13 14:28:06 +08:00
|
|
|
if (!GVA)
|
|
|
|
return false;
|
2010-09-28 17:55:24 +08:00
|
|
|
|
2014-11-06 21:20:12 +08:00
|
|
|
// Enforce -mlocal-sdata.
|
2016-10-25 03:23:39 +08:00
|
|
|
if (!LocalSData && GVA->hasLocalLinkage())
|
2009-08-13 14:28:06 +08:00
|
|
|
return false;
|
2010-09-28 17:55:24 +08:00
|
|
|
|
2014-11-06 21:20:12 +08:00
|
|
|
// Enforce -mextern-sdata.
|
2016-10-25 03:23:39 +08:00
|
|
|
if (!ExternSData && ((GVA->hasExternalLinkage() && GVA->isDeclaration()) ||
|
|
|
|
GVA->hasCommonLinkage()))
|
2009-08-13 14:28:06 +08:00
|
|
|
return false;
|
|
|
|
|
2017-07-22 01:19:00 +08:00
|
|
|
// Enforce -membedded-data.
|
|
|
|
if (EmbeddedData && GVA->isConstant())
|
|
|
|
return false;
|
|
|
|
|
2016-10-25 03:23:39 +08:00
|
|
|
Type *Ty = GVA->getValueType();
|
2015-07-16 14:04:17 +08:00
|
|
|
return IsInSmallSection(
|
2016-10-25 03:23:39 +08:00
|
|
|
GVA->getParent()->getDataLayout().getTypeAllocSize(Ty));
|
2009-08-13 14:28:06 +08:00
|
|
|
}
|
|
|
|
|
2016-09-16 15:33:15 +08:00
|
|
|
MCSection *MipsTargetObjectFile::SelectSectionForGlobal(
|
2016-10-25 03:23:39 +08:00
|
|
|
const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
|
2009-08-13 14:28:06 +08:00
|
|
|
// TODO: Could also support "weak" symbols as well with ".gnu.linkonce.s.*"
|
|
|
|
// sections?
|
2010-09-28 17:55:24 +08:00
|
|
|
|
2009-08-13 14:28:06 +08:00
|
|
|
// Handle Small Section classification here.
|
2016-10-25 03:23:39 +08:00
|
|
|
if (Kind.isBSS() && IsGlobalInSmallSection(GO, TM, Kind))
|
2009-08-13 14:28:06 +08:00
|
|
|
return SmallBSSSection;
|
2016-10-25 03:23:39 +08:00
|
|
|
if (Kind.isData() && IsGlobalInSmallSection(GO, TM, Kind))
|
2009-08-13 14:28:06 +08:00
|
|
|
return SmallDataSection;
|
2017-07-22 01:19:00 +08:00
|
|
|
if (Kind.isReadOnly() && IsGlobalInSmallSection(GO, TM, Kind))
|
|
|
|
return SmallDataSection;
|
2010-09-28 17:55:24 +08:00
|
|
|
|
2009-08-13 14:28:06 +08:00
|
|
|
// Otherwise, we work the same as ELF.
|
2016-10-25 03:23:39 +08:00
|
|
|
return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, Kind, TM);
|
2009-08-13 14:28:06 +08:00
|
|
|
}
|
2014-11-06 21:20:12 +08:00
|
|
|
|
|
|
|
/// Return true if this constant should be placed into small data section.
|
2015-07-16 14:11:10 +08:00
|
|
|
bool MipsTargetObjectFile::IsConstantInSmallSection(
|
|
|
|
const DataLayout &DL, const Constant *CN, const TargetMachine &TM) const {
|
2015-03-21 11:13:05 +08:00
|
|
|
return (static_cast<const MipsTargetMachine &>(TM)
|
|
|
|
.getSubtargetImpl()
|
|
|
|
->useSmallSection() &&
|
2015-07-16 14:11:10 +08:00
|
|
|
LocalSData && IsInSmallSection(DL.getTypeAllocSize(CN->getType())));
|
2014-11-06 21:20:12 +08:00
|
|
|
}
|
|
|
|
|
2015-07-16 14:04:17 +08:00
|
|
|
/// Return true if this constant should be placed into small data section.
|
2016-02-21 09:40:04 +08:00
|
|
|
MCSection *MipsTargetObjectFile::getSectionForConstant(const DataLayout &DL,
|
|
|
|
SectionKind Kind,
|
|
|
|
const Constant *C,
|
|
|
|
unsigned &Align) const {
|
2015-07-16 14:11:10 +08:00
|
|
|
if (IsConstantInSmallSection(DL, C, *TM))
|
2014-11-06 21:20:12 +08:00
|
|
|
return SmallDataSection;
|
|
|
|
|
|
|
|
// Otherwise, we work the same as ELF.
|
2016-02-21 09:40:04 +08:00
|
|
|
return TargetLoweringObjectFileELF::getSectionForConstant(DL, Kind, C, Align);
|
2014-11-06 21:20:12 +08:00
|
|
|
}
|
2017-01-21 01:53:30 +08:00
|
|
|
|
|
|
|
const MCExpr *
|
|
|
|
MipsTargetObjectFile::getDebugThreadLocalSymbol(const MCSymbol *Sym) const {
|
|
|
|
const MCExpr *Expr =
|
|
|
|
MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext());
|
|
|
|
return MCBinaryExpr::createAdd(
|
|
|
|
Expr, MCConstantExpr::create(0x8000, getContext()), getContext());
|
|
|
|
}
|