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