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