blob: a9c152dc95c468e0f1cee6218162da58200355c5 [file] [log] [blame]
Wesley Pecka70f28c2010-02-23 19:15:24 +00001//===-- MBlazeTargetObjectFile.cpp - MBlaze 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 "MBlazeTargetObjectFile.h"
11#include "MBlazeSubtarget.h"
12#include "llvm/DerivedTypes.h"
13#include "llvm/GlobalVariable.h"
Chris Lattner287df1b2010-04-08 21:34:17 +000014#include "llvm/MC/MCContext.h"
Wesley Pecka70f28c2010-02-23 19:15:24 +000015#include "llvm/MC/MCSectionELF.h"
16#include "llvm/Target/TargetData.h"
17#include "llvm/Target/TargetMachine.h"
18#include "llvm/Support/CommandLine.h"
Rafael Espindolac85dca62011-01-23 04:28:49 +000019#include "llvm/Support/ELF.h"
Wesley Pecka70f28c2010-02-23 19:15:24 +000020using namespace llvm;
21
22void MBlazeTargetObjectFile::
23Initialize(MCContext &Ctx, const TargetMachine &TM) {
24 TargetLoweringObjectFileELF::Initialize(Ctx, TM);
25
26 SmallDataSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +000027 getContext().getELFSection(".sdata", ELF::SHT_PROGBITS,
Chris Lattner287df1b2010-04-08 21:34:17 +000028 MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC,
29 SectionKind::getDataRel());
Wesley Pecka70f28c2010-02-23 19:15:24 +000030
31 SmallBSSSection =
Rafael Espindolac85dca62011-01-23 04:28:49 +000032 getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
Chris Lattner287df1b2010-04-08 21:34:17 +000033 MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC,
34 SectionKind::getBSS());
Wesley Pecka70f28c2010-02-23 19:15:24 +000035
36}
37
38// A address must be loaded from a small section if its size is less than the
39// small section size threshold. Data in this section must be addressed using
40// gp_rel operator.
41static bool IsInSmallSection(uint64_t Size) {
42 return Size > 0 && Size <= 8;
43}
44
45bool MBlazeTargetObjectFile::
46IsGlobalInSmallSection(const GlobalValue *GV, const TargetMachine &TM) const {
47 if (GV->isDeclaration() || GV->hasAvailableExternallyLinkage())
48 return false;
49
50 return IsGlobalInSmallSection(GV, TM, getKindForGlobal(GV, TM));
51}
52
53/// IsGlobalInSmallSection - Return true if this global address should be
54/// placed into small data/bss section.
55bool MBlazeTargetObjectFile::
56IsGlobalInSmallSection(const GlobalValue *GV, const TargetMachine &TM,
57 SectionKind Kind) const {
58 // Only global variables, not functions.
59 const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GV);
60 if (!GVA)
61 return false;
62
63 // We can only do this for datarel or BSS objects for now.
64 if (!Kind.isBSS() && !Kind.isDataRel())
65 return false;
66
67 // If this is a internal constant string, there is a special
68 // section for it, but not in small data/bss.
69 if (Kind.isMergeable1ByteCString())
70 return false;
71
72 const Type *Ty = GV->getType()->getElementType();
73 return IsInSmallSection(TM.getTargetData()->getTypeAllocSize(Ty));
74}
75
76const MCSection *MBlazeTargetObjectFile::
77SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
78 Mangler *Mang, const TargetMachine &TM) const {
79 // TODO: Could also support "weak" symbols as well with ".gnu.linkonce.s.*"
80 // sections?
81
82 // Handle Small Section classification here.
83 if (Kind.isBSS() && IsGlobalInSmallSection(GV, TM, Kind))
84 return SmallBSSSection;
85 if (Kind.isDataNoRel() && IsGlobalInSmallSection(GV, TM, Kind))
86 return SmallDataSection;
87
88 // Otherwise, we work the same as ELF.
89 return TargetLoweringObjectFileELF::SelectSectionForGlobal(GV, Kind, Mang,TM);
90}