MipsTargetObjectFile.cpp revision b71b909bc76f48377fc96547d53a088346852600
1//===-- MipsTargetObjectFile.cpp - Mips object files ----------------------===//
2//
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//
8//===----------------------------------------------------------------------===//
9
10#include "MipsTargetObjectFile.h"
11#include "llvm/DerivedTypes.h"
12#include "llvm/GlobalVariable.h"
13#include "llvm/MC/MCSectionELF.h"
14#include "llvm/Target/TargetData.h"
15#include "llvm/Target/TargetMachine.h"
16#include "llvm/Support/CommandLine.h"
17using namespace llvm;
18
19static cl::opt<unsigned>
20SSThreshold("mips-ssection-threshold", cl::Hidden,
21            cl::desc("Small data and bss section threshold size (default=8)"),
22            cl::init(8));
23
24void MipsTargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &TM){
25  TargetLoweringObjectFileELF::Initialize(Ctx, TM);
26
27  SmallDataSection =
28    getELFSection(".sdata", MCSectionELF::SHT_PROGBITS,
29                  MCSectionELF::SHF_WRITE | MCSectionELF::SHF_ALLOC,
30                  SectionKind::getDataRel());
31
32  SmallBSSSection =
33    getELFSection(".sbss", MCSectionELF::SHT_NOBITS,
34                  MCSectionELF::SHF_WRITE | MCSectionELF::SHF_ALLOC,
35                  SectionKind::getBSS());
36
37}
38
39// A address must be loaded from a small section if its size is less than the
40// small section size threshold. Data in this section must be addressed using
41// gp_rel operator.
42static bool IsInSmallSection(uint64_t Size) {
43  return Size > 0 && Size <= SSThreshold;
44}
45
46bool MipsTargetObjectFile::IsGlobalInSmallSection(const GlobalValue *GV,
47                                                const TargetMachine &TM) const {
48  if (GV->isDeclaration() || GV->hasAvailableExternallyLinkage())
49    return false;
50
51  return IsGlobalInSmallSection(GV, TM, getKindForGlobal(GV, TM));
52}
53
54/// IsGlobalInSmallSection - Return true if this global address should be
55/// placed into small data/bss section.
56bool MipsTargetObjectFile::
57IsGlobalInSmallSection(const GlobalValue *GV, const TargetMachine &TM,
58                       SectionKind Kind) const {
59  // Only global variables, not functions.
60  const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GV);
61  if (!GVA)
62    return false;
63
64  // We can only do this for datarel or BSS objects for now.
65  if (!Kind.isBSS() && !Kind.isDataRel())
66    return false;
67
68  // If this is a internal constant string, there is a special
69  // section for it, but not in small data/bss.
70  if (Kind.isMergeable1ByteCString())
71    return false;
72
73  const Type *Ty = GV->getType()->getElementType();
74  return IsInSmallSection(TM.getTargetData()->getTypeAllocSize(Ty));
75}
76
77
78
79const MCSection *MipsTargetObjectFile::
80SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
81                       Mangler *Mang, const TargetMachine &TM) const {
82  // TODO: Could also support "weak" symbols as well with ".gnu.linkonce.s.*"
83  // sections?
84
85  // Handle Small Section classification here.
86  if (Kind.isBSS() && IsGlobalInSmallSection(GV, TM, Kind))
87    return SmallBSSSection;
88  if (Kind.isDataNoRel() && IsGlobalInSmallSection(GV, TM, Kind))
89    return SmallDataSection;
90
91  // Otherwise, we work the same as ELF.
92  return TargetLoweringObjectFileELF::SelectSectionForGlobal(GV, Kind, Mang,TM);
93}
94