Jia Liu | c570711 | 2012-02-17 08:55:11 +0000 | [diff] [blame] | 1 | //===-- MipsTargetObjectFile.cpp - Mips Object Files ----------------------===// |
Chris Lattner | b71b909 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 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 | // |
Akira Hatanaka | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | b71b909 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 9 | |
| 10 | #include "MipsTargetObjectFile.h" |
Bruno Cardoso Lopes | 0046de0 | 2009-11-19 05:28:18 +0000 | [diff] [blame] | 11 | #include "MipsSubtarget.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 12 | #include "llvm/IR/DataLayout.h" |
| 13 | #include "llvm/IR/DerivedTypes.h" |
| 14 | #include "llvm/IR/GlobalVariable.h" |
Chris Lattner | 287df1b | 2010-04-08 21:34:17 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCContext.h" |
Chris Lattner | b71b909 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCSectionELF.h" |
Chris Lattner | b71b909 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 17 | #include "llvm/Support/CommandLine.h" |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 18 | #include "llvm/Support/ELF.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 19 | #include "llvm/Target/TargetMachine.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); |
Logan Chien | fd91d8d | 2012-09-05 06:17:17 +0000 | [diff] [blame] | 29 | InitializeELF(TM.Options.UseInitArray); |
Che-Liang Chiou | 36919ac | 2010-09-28 09:55:24 +0000 | [diff] [blame] | 30 | |
Chris Lattner | b71b909 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 31 | SmallDataSection = |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 32 | getContext().getELFSection(".sdata", ELF::SHT_PROGBITS, |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 33 | ELF::SHF_WRITE |ELF::SHF_ALLOC, |
Chris Lattner | 287df1b | 2010-04-08 21:34:17 +0000 | [diff] [blame] | 34 | SectionKind::getDataRel()); |
Che-Liang Chiou | 36919ac | 2010-09-28 09:55:24 +0000 | [diff] [blame] | 35 | |
Chris Lattner | b71b909 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 36 | SmallBSSSection = |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 37 | getContext().getELFSection(".sbss", ELF::SHT_NOBITS, |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 38 | ELF::SHF_WRITE |ELF::SHF_ALLOC, |
Chris Lattner | 287df1b | 2010-04-08 21:34:17 +0000 | [diff] [blame] | 39 | SectionKind::getBSS()); |
Che-Liang Chiou | 36919ac | 2010-09-28 09:55:24 +0000 | [diff] [blame] | 40 | |
Jack Carter | c91cbb9 | 2013-01-18 21:20:38 +0000 | [diff] [blame] | 41 | // Register info information |
| 42 | const MipsSubtarget &Subtarget = TM.getSubtarget<MipsSubtarget>(); |
| 43 | if (Subtarget.isABI_N64() || Subtarget.isABI_N32()) |
| 44 | ReginfoSection = |
| 45 | getContext().getELFSection(".MIPS.options", |
| 46 | ELF::SHT_MIPS_OPTIONS, |
| 47 | ELF::SHF_ALLOC |ELF::SHF_MIPS_NOSTRIP, |
| 48 | SectionKind::getMetadata()); |
| 49 | else |
| 50 | ReginfoSection = |
| 51 | getContext().getELFSection(".reginfo", |
| 52 | ELF::SHT_MIPS_REGINFO, |
| 53 | ELF::SHF_ALLOC, |
| 54 | SectionKind::getMetadata()); |
Chris Lattner | b71b909 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Che-Liang Chiou | 36919ac | 2010-09-28 09:55:24 +0000 | [diff] [blame] | 57 | // A address must be loaded from a small section if its size is less than the |
| 58 | // small section size threshold. Data in this section must be addressed using |
Chris Lattner | b71b909 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 59 | // gp_rel operator. |
| 60 | static bool IsInSmallSection(uint64_t Size) { |
| 61 | return Size > 0 && Size <= SSThreshold; |
| 62 | } |
| 63 | |
| 64 | bool MipsTargetObjectFile::IsGlobalInSmallSection(const GlobalValue *GV, |
Akira Hatanaka | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 65 | const TargetMachine &TM) const { |
Chris Lattner | b71b909 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 66 | if (GV->isDeclaration() || GV->hasAvailableExternallyLinkage()) |
| 67 | return false; |
Che-Liang Chiou | 36919ac | 2010-09-28 09:55:24 +0000 | [diff] [blame] | 68 | |
Chris Lattner | b71b909 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 69 | return IsGlobalInSmallSection(GV, TM, getKindForGlobal(GV, TM)); |
| 70 | } |
| 71 | |
| 72 | /// IsGlobalInSmallSection - Return true if this global address should be |
| 73 | /// placed into small data/bss section. |
| 74 | bool MipsTargetObjectFile:: |
| 75 | IsGlobalInSmallSection(const GlobalValue *GV, const TargetMachine &TM, |
| 76 | SectionKind Kind) const { |
Bruno Cardoso Lopes | 0046de0 | 2009-11-19 05:28:18 +0000 | [diff] [blame] | 77 | |
Bruno Cardoso Lopes | 0046de0 | 2009-11-19 05:28:18 +0000 | [diff] [blame] | 78 | const MipsSubtarget &Subtarget = TM.getSubtarget<MipsSubtarget>(); |
Akira Hatanaka | e7338cd | 2012-08-22 03:18:13 +0000 | [diff] [blame] | 79 | |
| 80 | // Return if small section is not available. |
| 81 | if (!Subtarget.useSmallSection()) |
Bruno Cardoso Lopes | 0046de0 | 2009-11-19 05:28:18 +0000 | [diff] [blame] | 82 | return false; |
| 83 | |
Chris Lattner | b71b909 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 84 | // Only global variables, not functions. |
| 85 | const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GV); |
| 86 | if (!GVA) |
| 87 | return false; |
Che-Liang Chiou | 36919ac | 2010-09-28 09:55:24 +0000 | [diff] [blame] | 88 | |
Chris Lattner | b71b909 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 89 | // We can only do this for datarel or BSS objects for now. |
| 90 | if (!Kind.isBSS() && !Kind.isDataRel()) |
| 91 | return false; |
Che-Liang Chiou | 36919ac | 2010-09-28 09:55:24 +0000 | [diff] [blame] | 92 | |
Chris Lattner | b71b909 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 93 | // If this is a internal constant string, there is a special |
| 94 | // section for it, but not in small data/bss. |
| 95 | if (Kind.isMergeable1ByteCString()) |
| 96 | return false; |
| 97 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 98 | Type *Ty = GV->getType()->getElementType(); |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 99 | return IsInSmallSection(TM.getDataLayout()->getTypeAllocSize(Ty)); |
Chris Lattner | b71b909 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | |
| 103 | |
| 104 | const MCSection *MipsTargetObjectFile:: |
| 105 | SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, |
| 106 | Mangler *Mang, const TargetMachine &TM) const { |
| 107 | // TODO: Could also support "weak" symbols as well with ".gnu.linkonce.s.*" |
| 108 | // sections? |
Che-Liang Chiou | 36919ac | 2010-09-28 09:55:24 +0000 | [diff] [blame] | 109 | |
Chris Lattner | b71b909 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 110 | // Handle Small Section classification here. |
| 111 | if (Kind.isBSS() && IsGlobalInSmallSection(GV, TM, Kind)) |
| 112 | return SmallBSSSection; |
| 113 | if (Kind.isDataNoRel() && IsGlobalInSmallSection(GV, TM, Kind)) |
| 114 | return SmallDataSection; |
Che-Liang Chiou | 36919ac | 2010-09-28 09:55:24 +0000 | [diff] [blame] | 115 | |
Chris Lattner | b71b909 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 116 | // Otherwise, we work the same as ELF. |
Akira Hatanaka | 4552c9a | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 117 | return TargetLoweringObjectFileELF::SelectSectionForGlobal(GV, Kind, Mang,TM); |
Chris Lattner | b71b909 | 2009-08-13 06:28:06 +0000 | [diff] [blame] | 118 | } |