Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 1 | //===-- llvm/CodeGen/TargetLoweringObjectFileImpl.cpp - Object File Info --===// |
| 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 | // This file implements classes used to handle lowerings specific to common |
| 11 | // object file formats. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/SmallString.h" |
| 17 | #include "llvm/ADT/StringExtras.h" |
| 18 | #include "llvm/ADT/Triple.h" |
| 19 | #include "llvm/CodeGen/MachineModuleInfoImpls.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Constants.h" |
| 21 | #include "llvm/IR/DataLayout.h" |
| 22 | #include "llvm/IR/DerivedTypes.h" |
| 23 | #include "llvm/IR/Function.h" |
| 24 | #include "llvm/IR/GlobalVariable.h" |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 25 | #include "llvm/IR/Mangler.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 26 | #include "llvm/IR/Module.h" |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 27 | #include "llvm/MC/MCContext.h" |
| 28 | #include "llvm/MC/MCExpr.h" |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 29 | #include "llvm/MC/MCSectionCOFF.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 30 | #include "llvm/MC/MCSectionELF.h" |
| 31 | #include "llvm/MC/MCSectionMachO.h" |
Rafael Espindola | 30deafc | 2011-04-16 03:51:21 +0000 | [diff] [blame] | 32 | #include "llvm/MC/MCStreamer.h" |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 33 | #include "llvm/MC/MCSymbol.h" |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Dwarf.h" |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 35 | #include "llvm/Support/ELF.h" |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 36 | #include "llvm/Support/ErrorHandling.h" |
| 37 | #include "llvm/Support/raw_ostream.h" |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 38 | #include "llvm/Target/TargetLowering.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 39 | #include "llvm/Target/TargetMachine.h" |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 40 | using namespace llvm; |
Anton Korobeynikov | 293d592 | 2010-02-21 20:28:15 +0000 | [diff] [blame] | 41 | using namespace dwarf; |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 42 | |
| 43 | //===----------------------------------------------------------------------===// |
| 44 | // ELF |
| 45 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 46 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 47 | MCSymbol *TargetLoweringObjectFileELF::getCFIPersonalitySymbol( |
| 48 | const GlobalValue *GV, Mangler &Mang, const TargetMachine &TM, |
| 49 | MachineModuleInfo *MMI) const { |
Rafael Espindola | 60246a9 | 2011-04-27 23:17:57 +0000 | [diff] [blame] | 50 | unsigned Encoding = getPersonalityEncoding(); |
Rafael Espindola | 7afec9c | 2011-04-27 23:08:15 +0000 | [diff] [blame] | 51 | switch (Encoding & 0x70) { |
| 52 | default: |
| 53 | report_fatal_error("We do not support this DWARF encoding yet!"); |
| 54 | case dwarf::DW_EH_PE_absptr: |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 55 | return TM.getSymbol(GV, Mang); |
Rafael Espindola | 7afec9c | 2011-04-27 23:08:15 +0000 | [diff] [blame] | 56 | case dwarf::DW_EH_PE_pcrel: { |
Rafael Espindola | fb66f47 | 2011-06-13 03:09:13 +0000 | [diff] [blame] | 57 | return getContext().GetOrCreateSymbol(StringRef("DW.ref.") + |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 58 | TM.getSymbol(GV, Mang)->getName()); |
Rafael Espindola | 7afec9c | 2011-04-27 23:08:15 +0000 | [diff] [blame] | 59 | } |
| 60 | } |
Rafael Espindola | 30deafc | 2011-04-16 03:51:21 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | void TargetLoweringObjectFileELF::emitPersonalityValue(MCStreamer &Streamer, |
| 64 | const TargetMachine &TM, |
Rafael Espindola | 7afec9c | 2011-04-27 23:08:15 +0000 | [diff] [blame] | 65 | const MCSymbol *Sym) const { |
Rafael Espindola | fb66f47 | 2011-06-13 03:09:13 +0000 | [diff] [blame] | 66 | SmallString<64> NameData("DW.ref."); |
| 67 | NameData += Sym->getName(); |
| 68 | MCSymbol *Label = getContext().GetOrCreateSymbol(NameData); |
Rafael Espindola | 018e38c | 2011-04-27 21:29:52 +0000 | [diff] [blame] | 69 | Streamer.EmitSymbolAttribute(Label, MCSA_Hidden); |
| 70 | Streamer.EmitSymbolAttribute(Label, MCSA_Weak); |
Rafael Espindola | fb66f47 | 2011-06-13 03:09:13 +0000 | [diff] [blame] | 71 | StringRef Prefix = ".data."; |
| 72 | NameData.insert(NameData.begin(), Prefix.begin(), Prefix.end()); |
Rafael Espindola | 018e38c | 2011-04-27 21:29:52 +0000 | [diff] [blame] | 73 | unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE | ELF::SHF_GROUP; |
| 74 | const MCSection *Sec = getContext().getELFSection(NameData, |
| 75 | ELF::SHT_PROGBITS, |
| 76 | Flags, |
| 77 | SectionKind::getDataRel(), |
| 78 | 0, Label->getName()); |
Chandler Carruth | 426c2bf | 2012-11-01 09:14:31 +0000 | [diff] [blame] | 79 | unsigned Size = TM.getDataLayout()->getPointerSize(); |
Rafael Espindola | 018e38c | 2011-04-27 21:29:52 +0000 | [diff] [blame] | 80 | Streamer.SwitchSection(Sec); |
Chandler Carruth | 426c2bf | 2012-11-01 09:14:31 +0000 | [diff] [blame] | 81 | Streamer.EmitValueToAlignment(TM.getDataLayout()->getPointerABIAlignment()); |
Rafael Espindola | 018e38c | 2011-04-27 21:29:52 +0000 | [diff] [blame] | 82 | Streamer.EmitSymbolAttribute(Label, MCSA_ELF_TypeObject); |
David Chisnall | ca5b752 | 2012-02-17 16:05:50 +0000 | [diff] [blame] | 83 | const MCExpr *E = MCConstantExpr::Create(Size, getContext()); |
Rafael Espindola | 018e38c | 2011-04-27 21:29:52 +0000 | [diff] [blame] | 84 | Streamer.EmitELFSize(Label, E); |
| 85 | Streamer.EmitLabel(Label); |
Rafael Espindola | 30deafc | 2011-04-16 03:51:21 +0000 | [diff] [blame] | 86 | |
Rafael Espindola | 018e38c | 2011-04-27 21:29:52 +0000 | [diff] [blame] | 87 | Streamer.EmitSymbolValue(Sym, Size); |
Rafael Espindola | 30deafc | 2011-04-16 03:51:21 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 90 | const MCExpr *TargetLoweringObjectFileELF::getTTypeGlobalReference( |
| 91 | const GlobalValue *GV, unsigned Encoding, Mangler &Mang, |
| 92 | const TargetMachine &TM, MachineModuleInfo *MMI, |
| 93 | MCStreamer &Streamer) const { |
Anton Korobeynikov | 25efd6d | 2012-11-14 01:47:00 +0000 | [diff] [blame] | 94 | |
| 95 | if (Encoding & dwarf::DW_EH_PE_indirect) { |
| 96 | MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>(); |
| 97 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 98 | MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, ".DW.stub", Mang, TM); |
Anton Korobeynikov | 25efd6d | 2012-11-14 01:47:00 +0000 | [diff] [blame] | 99 | |
| 100 | // Add information about the stub reference to ELFMMI so that the stub |
| 101 | // gets emitted by the asmprinter. |
Anton Korobeynikov | 25efd6d | 2012-11-14 01:47:00 +0000 | [diff] [blame] | 102 | MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym); |
| 103 | if (StubSym.getPointer() == 0) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 104 | MCSymbol *Sym = TM.getSymbol(GV, Mang); |
Anton Korobeynikov | 25efd6d | 2012-11-14 01:47:00 +0000 | [diff] [blame] | 105 | StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage()); |
| 106 | } |
| 107 | |
| 108 | return TargetLoweringObjectFile:: |
| 109 | getTTypeReference(MCSymbolRefExpr::Create(SSym, getContext()), |
| 110 | Encoding & ~dwarf::DW_EH_PE_indirect, Streamer); |
| 111 | } |
| 112 | |
| 113 | return TargetLoweringObjectFile:: |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 114 | getTTypeGlobalReference(GV, Encoding, Mang, TM, MMI, Streamer); |
Anton Korobeynikov | 25efd6d | 2012-11-14 01:47:00 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 117 | static SectionKind |
| 118 | getELFKindForNamedSection(StringRef Name, SectionKind K) { |
Rafael Espindola | e665798 | 2011-05-24 03:10:31 +0000 | [diff] [blame] | 119 | // N.B.: The defaults used in here are no the same ones used in MC. |
| 120 | // We follow gcc, MC follows gas. For example, given ".section .eh_frame", |
| 121 | // both gas and MC will produce a section with no flags. Given |
Bill Wendling | 96cb112 | 2012-07-19 00:04:14 +0000 | [diff] [blame] | 122 | // section(".eh_frame") gcc will produce: |
| 123 | // |
| 124 | // .section .eh_frame,"a",@progbits |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 125 | if (Name.empty() || Name[0] != '.') return K; |
| 126 | |
| 127 | // Some lame default implementation based on some magic section names. |
| 128 | if (Name == ".bss" || |
| 129 | Name.startswith(".bss.") || |
| 130 | Name.startswith(".gnu.linkonce.b.") || |
| 131 | Name.startswith(".llvm.linkonce.b.") || |
| 132 | Name == ".sbss" || |
| 133 | Name.startswith(".sbss.") || |
| 134 | Name.startswith(".gnu.linkonce.sb.") || |
| 135 | Name.startswith(".llvm.linkonce.sb.")) |
| 136 | return SectionKind::getBSS(); |
| 137 | |
| 138 | if (Name == ".tdata" || |
| 139 | Name.startswith(".tdata.") || |
| 140 | Name.startswith(".gnu.linkonce.td.") || |
| 141 | Name.startswith(".llvm.linkonce.td.")) |
| 142 | return SectionKind::getThreadData(); |
| 143 | |
| 144 | if (Name == ".tbss" || |
| 145 | Name.startswith(".tbss.") || |
| 146 | Name.startswith(".gnu.linkonce.tb.") || |
| 147 | Name.startswith(".llvm.linkonce.tb.")) |
| 148 | return SectionKind::getThreadBSS(); |
| 149 | |
| 150 | return K; |
| 151 | } |
| 152 | |
| 153 | |
| 154 | static unsigned getELFSectionType(StringRef Name, SectionKind K) { |
| 155 | |
| 156 | if (Name == ".init_array") |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 157 | return ELF::SHT_INIT_ARRAY; |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 158 | |
| 159 | if (Name == ".fini_array") |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 160 | return ELF::SHT_FINI_ARRAY; |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 161 | |
| 162 | if (Name == ".preinit_array") |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 163 | return ELF::SHT_PREINIT_ARRAY; |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 164 | |
| 165 | if (K.isBSS() || K.isThreadBSS()) |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 166 | return ELF::SHT_NOBITS; |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 167 | |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 168 | return ELF::SHT_PROGBITS; |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | |
| 172 | static unsigned |
| 173 | getELFSectionFlags(SectionKind K) { |
| 174 | unsigned Flags = 0; |
| 175 | |
| 176 | if (!K.isMetadata()) |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 177 | Flags |= ELF::SHF_ALLOC; |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 178 | |
| 179 | if (K.isText()) |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 180 | Flags |= ELF::SHF_EXECINSTR; |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 181 | |
Rafael Espindola | d846e3f | 2011-06-07 23:26:45 +0000 | [diff] [blame] | 182 | if (K.isWriteable()) |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 183 | Flags |= ELF::SHF_WRITE; |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 184 | |
| 185 | if (K.isThreadLocal()) |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 186 | Flags |= ELF::SHF_TLS; |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 187 | |
| 188 | // K.isMergeableConst() is left out to honour PR4650 |
| 189 | if (K.isMergeableCString() || K.isMergeableConst4() || |
| 190 | K.isMergeableConst8() || K.isMergeableConst16()) |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 191 | Flags |= ELF::SHF_MERGE; |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 192 | |
| 193 | if (K.isMergeableCString()) |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 194 | Flags |= ELF::SHF_STRINGS; |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 195 | |
| 196 | return Flags; |
| 197 | } |
| 198 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 199 | const MCSection *TargetLoweringObjectFileELF::getExplicitSectionGlobal( |
| 200 | const GlobalValue *GV, SectionKind Kind, Mangler &Mang, |
| 201 | const TargetMachine &TM) const { |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 202 | StringRef SectionName = GV->getSection(); |
| 203 | |
| 204 | // Infer section flags from the section name if we can. |
| 205 | Kind = getELFKindForNamedSection(SectionName, Kind); |
| 206 | |
Chris Lattner | 287df1b | 2010-04-08 21:34:17 +0000 | [diff] [blame] | 207 | return getContext().getELFSection(SectionName, |
| 208 | getELFSectionType(SectionName, Kind), |
Rafael Espindola | 34be396 | 2010-11-09 23:42:07 +0000 | [diff] [blame] | 209 | getELFSectionFlags(Kind), Kind); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Chris Lattner | 43ac721 | 2010-04-13 00:36:43 +0000 | [diff] [blame] | 212 | /// getSectionPrefixForGlobal - Return the section prefix name used by options |
| 213 | /// FunctionsSections and DataSections. |
| 214 | static const char *getSectionPrefixForGlobal(SectionKind Kind) { |
| 215 | if (Kind.isText()) return ".text."; |
| 216 | if (Kind.isReadOnly()) return ".rodata."; |
Anton Korobeynikov | 1d2d5a0 | 2012-02-23 10:36:04 +0000 | [diff] [blame] | 217 | if (Kind.isBSS()) return ".bss."; |
Chris Lattner | 43ac721 | 2010-04-13 00:36:43 +0000 | [diff] [blame] | 218 | |
| 219 | if (Kind.isThreadData()) return ".tdata."; |
| 220 | if (Kind.isThreadBSS()) return ".tbss."; |
| 221 | |
| 222 | if (Kind.isDataNoRel()) return ".data."; |
| 223 | if (Kind.isDataRelLocal()) return ".data.rel.local."; |
| 224 | if (Kind.isDataRel()) return ".data.rel."; |
| 225 | if (Kind.isReadOnlyWithRelLocal()) return ".data.rel.ro.local."; |
| 226 | |
| 227 | assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); |
| 228 | return ".data.rel.ro."; |
| 229 | } |
| 230 | |
| 231 | |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 232 | const MCSection *TargetLoweringObjectFileELF:: |
| 233 | SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 234 | Mangler &Mang, const TargetMachine &TM) const { |
Chris Lattner | 43ac721 | 2010-04-13 00:36:43 +0000 | [diff] [blame] | 235 | // If we have -ffunction-section or -fdata-section then we should emit the |
| 236 | // global value to a uniqued section specifically for it. |
| 237 | bool EmitUniquedSection; |
| 238 | if (Kind.isText()) |
| 239 | EmitUniquedSection = TM.getFunctionSections(); |
Michael J. Spencer | 579d7a3 | 2010-10-27 18:52:20 +0000 | [diff] [blame] | 240 | else |
Chris Lattner | 43ac721 | 2010-04-13 00:36:43 +0000 | [diff] [blame] | 241 | EmitUniquedSection = TM.getDataSections(); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 242 | |
| 243 | // If this global is linkonce/weak and the target handles this by emitting it |
| 244 | // into a 'uniqued' section name, create and return the section now. |
Chris Lattner | 43ac721 | 2010-04-13 00:36:43 +0000 | [diff] [blame] | 245 | if ((GV->isWeakForLinker() || EmitUniquedSection) && |
Anton Korobeynikov | 1d2d5a0 | 2012-02-23 10:36:04 +0000 | [diff] [blame] | 246 | !Kind.isCommon()) { |
Chris Lattner | 43ac721 | 2010-04-13 00:36:43 +0000 | [diff] [blame] | 247 | const char *Prefix; |
Rafael Espindola | 5d618ef | 2011-02-14 22:23:49 +0000 | [diff] [blame] | 248 | Prefix = getSectionPrefixForGlobal(Kind); |
Chris Lattner | 43ac721 | 2010-04-13 00:36:43 +0000 | [diff] [blame] | 249 | |
Chris Lattner | 4c6741f | 2010-03-15 20:37:38 +0000 | [diff] [blame] | 250 | SmallString<128> Name(Prefix, Prefix+strlen(Prefix)); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 251 | TM.getNameWithPrefix(Name, GV, Mang, true); |
| 252 | |
Rafael Espindola | 5d618ef | 2011-02-14 22:23:49 +0000 | [diff] [blame] | 253 | StringRef Group = ""; |
| 254 | unsigned Flags = getELFSectionFlags(Kind); |
| 255 | if (GV->isWeakForLinker()) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 256 | Group = Name.substr(strlen(Prefix)); |
Rafael Espindola | 5d618ef | 2011-02-14 22:23:49 +0000 | [diff] [blame] | 257 | Flags |= ELF::SHF_GROUP; |
| 258 | } |
| 259 | |
Chris Lattner | 287df1b | 2010-04-08 21:34:17 +0000 | [diff] [blame] | 260 | return getContext().getELFSection(Name.str(), |
| 261 | getELFSectionType(Name.str(), Kind), |
Rafael Espindola | 5d618ef | 2011-02-14 22:23:49 +0000 | [diff] [blame] | 262 | Flags, Kind, 0, Group); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | if (Kind.isText()) return TextSection; |
| 266 | |
| 267 | if (Kind.isMergeable1ByteCString() || |
| 268 | Kind.isMergeable2ByteCString() || |
| 269 | Kind.isMergeable4ByteCString()) { |
| 270 | |
| 271 | // We also need alignment here. |
| 272 | // FIXME: this is getting the alignment of the character, not the |
| 273 | // alignment of the global! |
| 274 | unsigned Align = |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 275 | TM.getDataLayout()->getPreferredAlignment(cast<GlobalVariable>(GV)); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 276 | |
| 277 | const char *SizeSpec = ".rodata.str1."; |
| 278 | if (Kind.isMergeable2ByteCString()) |
| 279 | SizeSpec = ".rodata.str2."; |
| 280 | else if (Kind.isMergeable4ByteCString()) |
| 281 | SizeSpec = ".rodata.str4."; |
| 282 | else |
| 283 | assert(Kind.isMergeable1ByteCString() && "unknown string width"); |
| 284 | |
| 285 | |
| 286 | std::string Name = SizeSpec + utostr(Align); |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 287 | return getContext().getELFSection(Name, ELF::SHT_PROGBITS, |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 288 | ELF::SHF_ALLOC | |
| 289 | ELF::SHF_MERGE | |
| 290 | ELF::SHF_STRINGS, |
Chris Lattner | 287df1b | 2010-04-08 21:34:17 +0000 | [diff] [blame] | 291 | Kind); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | if (Kind.isMergeableConst()) { |
| 295 | if (Kind.isMergeableConst4() && MergeableConst4Section) |
| 296 | return MergeableConst4Section; |
| 297 | if (Kind.isMergeableConst8() && MergeableConst8Section) |
| 298 | return MergeableConst8Section; |
| 299 | if (Kind.isMergeableConst16() && MergeableConst16Section) |
| 300 | return MergeableConst16Section; |
| 301 | return ReadOnlySection; // .const |
| 302 | } |
| 303 | |
| 304 | if (Kind.isReadOnly()) return ReadOnlySection; |
| 305 | |
| 306 | if (Kind.isThreadData()) return TLSDataSection; |
| 307 | if (Kind.isThreadBSS()) return TLSBSSSection; |
| 308 | |
| 309 | // Note: we claim that common symbols are put in BSSSection, but they are |
| 310 | // really emitted with the magic .comm directive, which creates a symbol table |
| 311 | // entry but not a section. |
| 312 | if (Kind.isBSS() || Kind.isCommon()) return BSSSection; |
| 313 | |
| 314 | if (Kind.isDataNoRel()) return DataSection; |
| 315 | if (Kind.isDataRelLocal()) return DataRelLocalSection; |
| 316 | if (Kind.isDataRel()) return DataRelSection; |
| 317 | if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection; |
| 318 | |
| 319 | assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); |
| 320 | return DataRelROSection; |
| 321 | } |
| 322 | |
| 323 | /// getSectionForConstant - Given a mergeable constant with the |
| 324 | /// specified size and relocation information, return a section that it |
| 325 | /// should be placed in. |
| 326 | const MCSection *TargetLoweringObjectFileELF:: |
| 327 | getSectionForConstant(SectionKind Kind) const { |
| 328 | if (Kind.isMergeableConst4() && MergeableConst4Section) |
| 329 | return MergeableConst4Section; |
| 330 | if (Kind.isMergeableConst8() && MergeableConst8Section) |
| 331 | return MergeableConst8Section; |
| 332 | if (Kind.isMergeableConst16() && MergeableConst16Section) |
| 333 | return MergeableConst16Section; |
| 334 | if (Kind.isReadOnly()) |
| 335 | return ReadOnlySection; |
| 336 | |
| 337 | if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection; |
| 338 | assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); |
| 339 | return DataRelROSection; |
| 340 | } |
| 341 | |
Anton Korobeynikov | 4a99f59 | 2012-01-25 22:24:19 +0000 | [diff] [blame] | 342 | const MCSection * |
| 343 | TargetLoweringObjectFileELF::getStaticCtorSection(unsigned Priority) const { |
| 344 | // The default scheme is .ctor / .dtor, so we have to invert the priority |
| 345 | // numbering. |
| 346 | if (Priority == 65535) |
| 347 | return StaticCtorSection; |
| 348 | |
Rafael Espindola | d6b43a3 | 2012-06-19 00:48:28 +0000 | [diff] [blame] | 349 | if (UseInitArray) { |
| 350 | std::string Name = std::string(".init_array.") + utostr(Priority); |
| 351 | return getContext().getELFSection(Name, ELF::SHT_INIT_ARRAY, |
| 352 | ELF::SHF_ALLOC | ELF::SHF_WRITE, |
| 353 | SectionKind::getDataRel()); |
| 354 | } else { |
| 355 | std::string Name = std::string(".ctors.") + utostr(65535 - Priority); |
| 356 | return getContext().getELFSection(Name, ELF::SHT_PROGBITS, |
| 357 | ELF::SHF_ALLOC |ELF::SHF_WRITE, |
| 358 | SectionKind::getDataRel()); |
| 359 | } |
Anton Korobeynikov | 4a99f59 | 2012-01-25 22:24:19 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | const MCSection * |
| 363 | TargetLoweringObjectFileELF::getStaticDtorSection(unsigned Priority) const { |
| 364 | // The default scheme is .ctor / .dtor, so we have to invert the priority |
| 365 | // numbering. |
| 366 | if (Priority == 65535) |
| 367 | return StaticDtorSection; |
| 368 | |
Rafael Espindola | d6b43a3 | 2012-06-19 00:48:28 +0000 | [diff] [blame] | 369 | if (UseInitArray) { |
| 370 | std::string Name = std::string(".fini_array.") + utostr(Priority); |
| 371 | return getContext().getELFSection(Name, ELF::SHT_FINI_ARRAY, |
| 372 | ELF::SHF_ALLOC | ELF::SHF_WRITE, |
| 373 | SectionKind::getDataRel()); |
| 374 | } else { |
| 375 | std::string Name = std::string(".dtors.") + utostr(65535 - Priority); |
| 376 | return getContext().getELFSection(Name, ELF::SHT_PROGBITS, |
| 377 | ELF::SHF_ALLOC |ELF::SHF_WRITE, |
| 378 | SectionKind::getDataRel()); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | void |
| 383 | TargetLoweringObjectFileELF::InitializeELF(bool UseInitArray_) { |
| 384 | UseInitArray = UseInitArray_; |
| 385 | if (!UseInitArray) |
| 386 | return; |
| 387 | |
| 388 | StaticCtorSection = |
| 389 | getContext().getELFSection(".init_array", ELF::SHT_INIT_ARRAY, |
| 390 | ELF::SHF_WRITE | |
| 391 | ELF::SHF_ALLOC, |
| 392 | SectionKind::getDataRel()); |
| 393 | StaticDtorSection = |
| 394 | getContext().getELFSection(".fini_array", ELF::SHT_FINI_ARRAY, |
| 395 | ELF::SHF_WRITE | |
| 396 | ELF::SHF_ALLOC, |
| 397 | SectionKind::getDataRel()); |
Anton Korobeynikov | 4a99f59 | 2012-01-25 22:24:19 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 400 | //===----------------------------------------------------------------------===// |
| 401 | // MachO |
| 402 | //===----------------------------------------------------------------------===// |
| 403 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 404 | /// getDepLibFromLinkerOpt - Extract the dependent library name from a linker |
| 405 | /// option string. Returns StringRef() if the option does not specify a library. |
| 406 | StringRef TargetLoweringObjectFileMachO:: |
| 407 | getDepLibFromLinkerOpt(StringRef LinkerOption) const { |
| 408 | const char *LibCmd = "-l"; |
| 409 | if (LinkerOption.startswith(LibCmd)) |
| 410 | return LinkerOption.substr(strlen(LibCmd)); |
| 411 | return StringRef(); |
| 412 | } |
| 413 | |
Daniel Dunbar | 6d49b68 | 2013-01-18 19:37:00 +0000 | [diff] [blame] | 414 | /// emitModuleFlags - Perform code emission for module flags. |
Bill Wendling | b464d3f | 2012-02-14 21:28:13 +0000 | [diff] [blame] | 415 | void TargetLoweringObjectFileMachO:: |
Bill Wendling | 057d521 | 2012-02-15 22:36:15 +0000 | [diff] [blame] | 416 | emitModuleFlags(MCStreamer &Streamer, |
| 417 | ArrayRef<Module::ModuleFlagEntry> ModuleFlags, |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 418 | Mangler &Mang, const TargetMachine &TM) const { |
Bill Wendling | b464d3f | 2012-02-14 21:28:13 +0000 | [diff] [blame] | 419 | unsigned VersionVal = 0; |
Bill Wendling | adb082c | 2012-04-24 11:03:50 +0000 | [diff] [blame] | 420 | unsigned ImageInfoFlags = 0; |
Daniel Dunbar | 6d49b68 | 2013-01-18 19:37:00 +0000 | [diff] [blame] | 421 | MDNode *LinkerOptions = 0; |
Bill Wendling | 057d521 | 2012-02-15 22:36:15 +0000 | [diff] [blame] | 422 | StringRef SectionVal; |
Bill Wendling | b464d3f | 2012-02-14 21:28:13 +0000 | [diff] [blame] | 423 | |
Bill Wendling | 057d521 | 2012-02-15 22:36:15 +0000 | [diff] [blame] | 424 | for (ArrayRef<Module::ModuleFlagEntry>::iterator |
| 425 | i = ModuleFlags.begin(), e = ModuleFlags.end(); i != e; ++i) { |
| 426 | const Module::ModuleFlagEntry &MFE = *i; |
Bill Wendling | b464d3f | 2012-02-14 21:28:13 +0000 | [diff] [blame] | 427 | |
| 428 | // Ignore flags with 'Require' behavior. |
Bill Wendling | 057d521 | 2012-02-15 22:36:15 +0000 | [diff] [blame] | 429 | if (MFE.Behavior == Module::Require) |
Bill Wendling | b464d3f | 2012-02-14 21:28:13 +0000 | [diff] [blame] | 430 | continue; |
| 431 | |
Bill Wendling | 057d521 | 2012-02-15 22:36:15 +0000 | [diff] [blame] | 432 | StringRef Key = MFE.Key->getString(); |
| 433 | Value *Val = MFE.Val; |
Bill Wendling | b464d3f | 2012-02-14 21:28:13 +0000 | [diff] [blame] | 434 | |
Daniel Dunbar | 6d49b68 | 2013-01-18 19:37:00 +0000 | [diff] [blame] | 435 | if (Key == "Objective-C Image Info Version") { |
Bill Wendling | b464d3f | 2012-02-14 21:28:13 +0000 | [diff] [blame] | 436 | VersionVal = cast<ConstantInt>(Val)->getZExtValue(); |
Daniel Dunbar | 6d49b68 | 2013-01-18 19:37:00 +0000 | [diff] [blame] | 437 | } else if (Key == "Objective-C Garbage Collection" || |
| 438 | Key == "Objective-C GC Only" || |
| 439 | Key == "Objective-C Is Simulated") { |
Bill Wendling | adb082c | 2012-04-24 11:03:50 +0000 | [diff] [blame] | 440 | ImageInfoFlags |= cast<ConstantInt>(Val)->getZExtValue(); |
Daniel Dunbar | 6d49b68 | 2013-01-18 19:37:00 +0000 | [diff] [blame] | 441 | } else if (Key == "Objective-C Image Info Section") { |
Bill Wendling | 057d521 | 2012-02-15 22:36:15 +0000 | [diff] [blame] | 442 | SectionVal = cast<MDString>(Val)->getString(); |
Daniel Dunbar | 6d49b68 | 2013-01-18 19:37:00 +0000 | [diff] [blame] | 443 | } else if (Key == "Linker Options") { |
| 444 | LinkerOptions = cast<MDNode>(Val); |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | // Emit the linker options if present. |
| 449 | if (LinkerOptions) { |
| 450 | for (unsigned i = 0, e = LinkerOptions->getNumOperands(); i != e; ++i) { |
| 451 | MDNode *MDOptions = cast<MDNode>(LinkerOptions->getOperand(i)); |
| 452 | SmallVector<std::string, 4> StrOptions; |
| 453 | |
| 454 | // Convert to strings. |
| 455 | for (unsigned ii = 0, ie = MDOptions->getNumOperands(); ii != ie; ++ii) { |
| 456 | MDString *MDOption = cast<MDString>(MDOptions->getOperand(ii)); |
| 457 | StrOptions.push_back(MDOption->getString()); |
| 458 | } |
| 459 | |
| 460 | Streamer.EmitLinkerOptions(StrOptions); |
| 461 | } |
Bill Wendling | b464d3f | 2012-02-14 21:28:13 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Bill Wendling | 057d521 | 2012-02-15 22:36:15 +0000 | [diff] [blame] | 464 | // The section is mandatory. If we don't have it, then we don't have GC info. |
| 465 | if (SectionVal.empty()) return; |
Bill Wendling | b464d3f | 2012-02-14 21:28:13 +0000 | [diff] [blame] | 466 | |
Bill Wendling | 057d521 | 2012-02-15 22:36:15 +0000 | [diff] [blame] | 467 | StringRef Segment, Section; |
| 468 | unsigned TAA = 0, StubSize = 0; |
| 469 | bool TAAParsed; |
| 470 | std::string ErrorCode = |
| 471 | MCSectionMachO::ParseSectionSpecifier(SectionVal, Segment, Section, |
| 472 | TAA, TAAParsed, StubSize); |
Bill Wendling | 2de3ff5 | 2012-02-15 22:47:53 +0000 | [diff] [blame] | 473 | if (!ErrorCode.empty()) |
Bill Wendling | 057d521 | 2012-02-15 22:36:15 +0000 | [diff] [blame] | 474 | // If invalid, report the error with report_fatal_error. |
Bill Wendling | 2de3ff5 | 2012-02-15 22:47:53 +0000 | [diff] [blame] | 475 | report_fatal_error("Invalid section specifier '" + Section + "': " + |
| 476 | ErrorCode + "."); |
Bill Wendling | b464d3f | 2012-02-14 21:28:13 +0000 | [diff] [blame] | 477 | |
Bill Wendling | 057d521 | 2012-02-15 22:36:15 +0000 | [diff] [blame] | 478 | // Get the section. |
| 479 | const MCSectionMachO *S = |
| 480 | getContext().getMachOSection(Segment, Section, TAA, StubSize, |
Bill Wendling | 2de3ff5 | 2012-02-15 22:47:53 +0000 | [diff] [blame] | 481 | SectionKind::getDataNoRel()); |
Bill Wendling | 057d521 | 2012-02-15 22:36:15 +0000 | [diff] [blame] | 482 | Streamer.SwitchSection(S); |
| 483 | Streamer.EmitLabel(getContext(). |
| 484 | GetOrCreateSymbol(StringRef("L_OBJC_IMAGE_INFO"))); |
Bill Wendling | b464d3f | 2012-02-14 21:28:13 +0000 | [diff] [blame] | 485 | Streamer.EmitIntValue(VersionVal, 4); |
Bill Wendling | adb082c | 2012-04-24 11:03:50 +0000 | [diff] [blame] | 486 | Streamer.EmitIntValue(ImageInfoFlags, 4); |
Bill Wendling | b464d3f | 2012-02-14 21:28:13 +0000 | [diff] [blame] | 487 | Streamer.AddBlankLine(); |
| 488 | } |
| 489 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 490 | const MCSection *TargetLoweringObjectFileMachO::getExplicitSectionGlobal( |
| 491 | const GlobalValue *GV, SectionKind Kind, Mangler &Mang, |
| 492 | const TargetMachine &TM) const { |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 493 | // Parse the section specifier and create it if valid. |
| 494 | StringRef Segment, Section; |
Stuart Hastings | 65c8bca | 2011-03-19 02:42:31 +0000 | [diff] [blame] | 495 | unsigned TAA = 0, StubSize = 0; |
| 496 | bool TAAParsed; |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 497 | std::string ErrorCode = |
| 498 | MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section, |
Stuart Hastings | 65c8bca | 2011-03-19 02:42:31 +0000 | [diff] [blame] | 499 | TAA, TAAParsed, StubSize); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 500 | if (!ErrorCode.empty()) { |
Chris Lattner | 75361b6 | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 501 | // If invalid, report the error with report_fatal_error. |
Benjamin Kramer | a7b0cb7 | 2011-11-15 16:27:03 +0000 | [diff] [blame] | 502 | report_fatal_error("Global variable '" + GV->getName() + |
| 503 | "' has an invalid section specifier '" + |
| 504 | GV->getSection() + "': " + ErrorCode + "."); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | // Get the section. |
| 508 | const MCSectionMachO *S = |
Chris Lattner | 2277221 | 2010-04-08 20:40:11 +0000 | [diff] [blame] | 509 | getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 510 | |
Stuart Hastings | 6ad82d8 | 2011-02-21 17:27:17 +0000 | [diff] [blame] | 511 | // If TAA wasn't set by ParseSectionSpecifier() above, |
| 512 | // use the value returned by getMachOSection() as a default. |
Stuart Hastings | 65c8bca | 2011-03-19 02:42:31 +0000 | [diff] [blame] | 513 | if (!TAAParsed) |
Stuart Hastings | 6ad82d8 | 2011-02-21 17:27:17 +0000 | [diff] [blame] | 514 | TAA = S->getTypeAndAttributes(); |
| 515 | |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 516 | // Okay, now that we got the section, verify that the TAA & StubSize agree. |
| 517 | // If the user declared multiple globals with different section flags, we need |
| 518 | // to reject it here. |
| 519 | if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) { |
Chris Lattner | 75361b6 | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 520 | // If invalid, report the error with report_fatal_error. |
Benjamin Kramer | a7b0cb7 | 2011-11-15 16:27:03 +0000 | [diff] [blame] | 521 | report_fatal_error("Global variable '" + GV->getName() + |
| 522 | "' section type or attributes does not match previous" |
| 523 | " section specifier"); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | return S; |
| 527 | } |
| 528 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 529 | bool TargetLoweringObjectFileMachO::isSectionAtomizableBySymbols( |
| 530 | const MCSection &Section) const { |
| 531 | const MCSectionMachO &SMO = static_cast<const MCSectionMachO&>(Section); |
| 532 | |
| 533 | // Sections holding 1 byte strings are atomized based on the data |
| 534 | // they contain. |
| 535 | // Sections holding 2 byte strings require symbols in order to be |
| 536 | // atomized. |
| 537 | // There is no dedicated section for 4 byte strings. |
| 538 | if (SMO.getKind().isMergeable1ByteCString()) |
| 539 | return false; |
| 540 | |
| 541 | if (SMO.getSegmentName() == "__DATA" && |
| 542 | SMO.getSectionName() == "__cfstring") |
| 543 | return false; |
| 544 | |
| 545 | switch (SMO.getType()) { |
| 546 | default: |
| 547 | return true; |
| 548 | |
| 549 | // These sections are atomized at the element boundaries without using |
| 550 | // symbols. |
| 551 | case MachO::S_4BYTE_LITERALS: |
| 552 | case MachO::S_8BYTE_LITERALS: |
| 553 | case MachO::S_16BYTE_LITERALS: |
| 554 | case MachO::S_LITERAL_POINTERS: |
| 555 | case MachO::S_NON_LAZY_SYMBOL_POINTERS: |
| 556 | case MachO::S_LAZY_SYMBOL_POINTERS: |
| 557 | case MachO::S_MOD_INIT_FUNC_POINTERS: |
| 558 | case MachO::S_MOD_TERM_FUNC_POINTERS: |
| 559 | case MachO::S_INTERPOSING: |
| 560 | return false; |
| 561 | } |
| 562 | } |
| 563 | |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 564 | const MCSection *TargetLoweringObjectFileMachO:: |
| 565 | SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 566 | Mangler &Mang, const TargetMachine &TM) const { |
Bill Wendling | fe128c6 | 2013-11-17 10:53:13 +0000 | [diff] [blame] | 567 | |
| 568 | // Handle thread local data. |
| 569 | if (Kind.isThreadBSS()) return TLSBSSSection; |
| 570 | if (Kind.isThreadData()) return TLSDataSection; |
| 571 | |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 572 | if (Kind.isText()) |
Arnold Schwaighofer | 9b19dfc | 2013-08-08 21:04:16 +0000 | [diff] [blame] | 573 | return GV->isWeakForLinker() ? TextCoalSection : TextSection; |
| 574 | |
| 575 | // If this is weak/linkonce, put this in a coalescable section, either in text |
| 576 | // or data depending on if it is writable. |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 577 | if (GV->isWeakForLinker()) { |
| 578 | if (Kind.isReadOnly()) |
Arnold Schwaighofer | 9b19dfc | 2013-08-08 21:04:16 +0000 | [diff] [blame] | 579 | return ConstTextCoalSection; |
| 580 | return DataCoalSection; |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | // FIXME: Alignment check should be handled by section classifier. |
Chris Lattner | 98f15d2 | 2010-03-07 04:28:09 +0000 | [diff] [blame] | 584 | if (Kind.isMergeable1ByteCString() && |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 585 | TM.getDataLayout()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32) |
Chris Lattner | 98f15d2 | 2010-03-07 04:28:09 +0000 | [diff] [blame] | 586 | return CStringSection; |
Michael J. Spencer | 579d7a3 | 2010-10-27 18:52:20 +0000 | [diff] [blame] | 587 | |
Chris Lattner | 98f15d2 | 2010-03-07 04:28:09 +0000 | [diff] [blame] | 588 | // Do not put 16-bit arrays in the UString section if they have an |
| 589 | // externally visible label, this runs into issues with certain linker |
| 590 | // versions. |
| 591 | if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() && |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 592 | TM.getDataLayout()->getPreferredAlignment(cast<GlobalVariable>(GV)) < 32) |
Chris Lattner | 98f15d2 | 2010-03-07 04:28:09 +0000 | [diff] [blame] | 593 | return UStringSection; |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 594 | |
| 595 | if (Kind.isMergeableConst()) { |
| 596 | if (Kind.isMergeableConst4()) |
| 597 | return FourByteConstantSection; |
| 598 | if (Kind.isMergeableConst8()) |
| 599 | return EightByteConstantSection; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 600 | if (Kind.isMergeableConst16()) |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 601 | return SixteenByteConstantSection; |
| 602 | } |
| 603 | |
| 604 | // Otherwise, if it is readonly, but not something we can specially optimize, |
| 605 | // just drop it in .const. |
| 606 | if (Kind.isReadOnly()) |
| 607 | return ReadOnlySection; |
| 608 | |
| 609 | // If this is marked const, put it into a const section. But if the dynamic |
| 610 | // linker needs to write to it, put it in the data segment. |
| 611 | if (Kind.isReadOnlyWithRel()) |
| 612 | return ConstDataSection; |
| 613 | |
| 614 | // Put zero initialized globals with strong external linkage in the |
| 615 | // DATA, __common section with the .zerofill directive. |
| 616 | if (Kind.isBSSExtern()) |
| 617 | return DataCommonSection; |
| 618 | |
| 619 | // Put zero initialized globals with local linkage in __DATA,__bss directive |
| 620 | // with the .zerofill directive (aka .lcomm). |
| 621 | if (Kind.isBSSLocal()) |
| 622 | return DataBSSSection; |
Michael J. Spencer | 579d7a3 | 2010-10-27 18:52:20 +0000 | [diff] [blame] | 623 | |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 624 | // Otherwise, just drop the variable in the normal data section. |
| 625 | return DataSection; |
| 626 | } |
| 627 | |
| 628 | const MCSection * |
| 629 | TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const { |
| 630 | // If this constant requires a relocation, we have to put it in the data |
| 631 | // segment, not in the text segment. |
| 632 | if (Kind.isDataRel() || Kind.isReadOnlyWithRel()) |
| 633 | return ConstDataSection; |
| 634 | |
| 635 | if (Kind.isMergeableConst4()) |
| 636 | return FourByteConstantSection; |
| 637 | if (Kind.isMergeableConst8()) |
| 638 | return EightByteConstantSection; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 639 | if (Kind.isMergeableConst16()) |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 640 | return SixteenByteConstantSection; |
| 641 | return ReadOnlySection; // .const |
| 642 | } |
| 643 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 644 | const MCExpr *TargetLoweringObjectFileMachO::getTTypeGlobalReference( |
| 645 | const GlobalValue *GV, unsigned Encoding, Mangler &Mang, |
| 646 | const TargetMachine &TM, MachineModuleInfo *MMI, |
| 647 | MCStreamer &Streamer) const { |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 648 | // The mach-o version of this method defaults to returning a stub reference. |
| 649 | |
Anton Korobeynikov | 293d592 | 2010-02-21 20:28:15 +0000 | [diff] [blame] | 650 | if (Encoding & DW_EH_PE_indirect) { |
| 651 | MachineModuleInfoMachO &MachOMMI = |
| 652 | MMI->getObjFileInfo<MachineModuleInfoMachO>(); |
| 653 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 654 | MCSymbol *SSym = |
| 655 | getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", Mang, TM); |
Anton Korobeynikov | 293d592 | 2010-02-21 20:28:15 +0000 | [diff] [blame] | 656 | |
| 657 | // Add information about the stub reference to MachOMMI so that the stub |
| 658 | // gets emitted by the asmprinter. |
Bill Wendling | 6712154 | 2011-10-24 23:05:43 +0000 | [diff] [blame] | 659 | MachineModuleInfoImpl::StubValueTy &StubSym = |
| 660 | GV->hasHiddenVisibility() ? MachOMMI.getHiddenGVStubEntry(SSym) : |
| 661 | MachOMMI.getGVStubEntry(SSym); |
Bill Wendling | cebae36 | 2010-03-10 22:34:10 +0000 | [diff] [blame] | 662 | if (StubSym.getPointer() == 0) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 663 | MCSymbol *Sym = TM.getSymbol(GV, Mang); |
Chris Lattner | 4c6741f | 2010-03-15 20:37:38 +0000 | [diff] [blame] | 664 | StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage()); |
Anton Korobeynikov | 293d592 | 2010-02-21 20:28:15 +0000 | [diff] [blame] | 665 | } |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 666 | |
| 667 | return TargetLoweringObjectFile:: |
Anton Korobeynikov | 25efd6d | 2012-11-14 01:47:00 +0000 | [diff] [blame] | 668 | getTTypeReference(MCSymbolRefExpr::Create(SSym, getContext()), |
| 669 | Encoding & ~dwarf::DW_EH_PE_indirect, Streamer); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 670 | } |
| 671 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 672 | return TargetLoweringObjectFile::getTTypeGlobalReference(GV, Encoding, Mang, |
| 673 | TM, MMI, Streamer); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 674 | } |
| 675 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 676 | MCSymbol *TargetLoweringObjectFileMachO::getCFIPersonalitySymbol( |
| 677 | const GlobalValue *GV, Mangler &Mang, const TargetMachine &TM, |
| 678 | MachineModuleInfo *MMI) const { |
Rafael Espindola | 7afec9c | 2011-04-27 23:08:15 +0000 | [diff] [blame] | 679 | // The mach-o version of this method defaults to returning a stub reference. |
| 680 | MachineModuleInfoMachO &MachOMMI = |
| 681 | MMI->getObjFileInfo<MachineModuleInfoMachO>(); |
| 682 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 683 | MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", Mang, TM); |
Rafael Espindola | 7afec9c | 2011-04-27 23:08:15 +0000 | [diff] [blame] | 684 | |
| 685 | // Add information about the stub reference to MachOMMI so that the stub |
| 686 | // gets emitted by the asmprinter. |
Bill Wendling | d7c2494 | 2011-11-29 01:43:20 +0000 | [diff] [blame] | 687 | MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym); |
Rafael Espindola | 7afec9c | 2011-04-27 23:08:15 +0000 | [diff] [blame] | 688 | if (StubSym.getPointer() == 0) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 689 | MCSymbol *Sym = TM.getSymbol(GV, Mang); |
Rafael Espindola | 7afec9c | 2011-04-27 23:08:15 +0000 | [diff] [blame] | 690 | StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage()); |
| 691 | } |
| 692 | |
| 693 | return SSym; |
| 694 | } |
| 695 | |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 696 | //===----------------------------------------------------------------------===// |
| 697 | // COFF |
| 698 | //===----------------------------------------------------------------------===// |
| 699 | |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 700 | static unsigned |
| 701 | getCOFFSectionFlags(SectionKind K) { |
| 702 | unsigned Flags = 0; |
| 703 | |
Anton Korobeynikov | 36335be | 2010-07-06 15:24:56 +0000 | [diff] [blame] | 704 | if (K.isMetadata()) |
Chris Lattner | 6e5ce28 | 2010-05-07 21:49:09 +0000 | [diff] [blame] | 705 | Flags |= |
Daniel Dunbar | 9461058 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 706 | COFF::IMAGE_SCN_MEM_DISCARDABLE; |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 707 | else if (K.isText()) |
| 708 | Flags |= |
Daniel Dunbar | 9461058 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 709 | COFF::IMAGE_SCN_MEM_EXECUTE | |
Michael J. Spencer | 3931b54 | 2010-10-27 18:52:29 +0000 | [diff] [blame] | 710 | COFF::IMAGE_SCN_MEM_READ | |
Daniel Dunbar | 9461058 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 711 | COFF::IMAGE_SCN_CNT_CODE; |
Chris Lattner | 6e5ce28 | 2010-05-07 21:49:09 +0000 | [diff] [blame] | 712 | else if (K.isBSS ()) |
| 713 | Flags |= |
Daniel Dunbar | 9461058 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 714 | COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA | |
| 715 | COFF::IMAGE_SCN_MEM_READ | |
| 716 | COFF::IMAGE_SCN_MEM_WRITE; |
Anton Korobeynikov | d4a19b6 | 2012-02-11 17:26:53 +0000 | [diff] [blame] | 717 | else if (K.isThreadLocal()) |
| 718 | Flags |= |
| 719 | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | |
| 720 | COFF::IMAGE_SCN_MEM_READ | |
| 721 | COFF::IMAGE_SCN_MEM_WRITE; |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 722 | else if (K.isReadOnly()) |
| 723 | Flags |= |
Daniel Dunbar | 9461058 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 724 | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | |
| 725 | COFF::IMAGE_SCN_MEM_READ; |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 726 | else if (K.isWriteable()) |
| 727 | Flags |= |
Daniel Dunbar | 9461058 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 728 | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | |
| 729 | COFF::IMAGE_SCN_MEM_READ | |
| 730 | COFF::IMAGE_SCN_MEM_WRITE; |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 731 | |
| 732 | return Flags; |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 733 | } |
| 734 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 735 | const MCSection *TargetLoweringObjectFileCOFF::getExplicitSectionGlobal( |
| 736 | const GlobalValue *GV, SectionKind Kind, Mangler &Mang, |
| 737 | const TargetMachine &TM) const { |
Michael J. Spencer | 4de5872 | 2012-11-13 22:04:09 +0000 | [diff] [blame] | 738 | int Selection = 0; |
| 739 | unsigned Characteristics = getCOFFSectionFlags(Kind); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 740 | StringRef Name = GV->getSection(); |
| 741 | StringRef COMDATSymName = ""; |
Michael J. Spencer | 4de5872 | 2012-11-13 22:04:09 +0000 | [diff] [blame] | 742 | if (GV->isWeakForLinker()) { |
| 743 | Selection = COFF::IMAGE_COMDAT_SELECT_ANY; |
| 744 | Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 745 | MCSymbol *Sym = TM.getSymbol(GV, Mang); |
| 746 | COMDATSymName = Sym->getName(); |
Michael J. Spencer | 4de5872 | 2012-11-13 22:04:09 +0000 | [diff] [blame] | 747 | } |
| 748 | return getContext().getCOFFSection(Name, |
| 749 | Characteristics, |
Nico Rieck | 8064628 | 2013-07-06 12:13:10 +0000 | [diff] [blame] | 750 | Kind, |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 751 | COMDATSymName, |
Nico Rieck | 8064628 | 2013-07-06 12:13:10 +0000 | [diff] [blame] | 752 | Selection); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 753 | } |
| 754 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 755 | static const char *getCOFFSectionNameForUniqueGlobal(SectionKind Kind) { |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 756 | if (Kind.isText()) |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 757 | return ".text"; |
Chris Lattner | 6e5ce28 | 2010-05-07 21:49:09 +0000 | [diff] [blame] | 758 | if (Kind.isBSS ()) |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 759 | return ".bss"; |
| 760 | if (Kind.isThreadLocal()) |
| 761 | return ".tls$"; |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 762 | if (Kind.isWriteable()) |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 763 | return ".data"; |
| 764 | return ".rdata"; |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | |
| 768 | const MCSection *TargetLoweringObjectFileCOFF:: |
| 769 | SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 770 | Mangler &Mang, const TargetMachine &TM) const { |
| 771 | // If we have -ffunction-sections then we should emit the global value to a |
| 772 | // uniqued section specifically for it. |
| 773 | bool EmitUniquedSection; |
| 774 | if (Kind.isText()) |
| 775 | EmitUniquedSection = TM.getFunctionSections(); |
| 776 | else |
| 777 | EmitUniquedSection = TM.getDataSections(); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 778 | |
| 779 | // If this global is linkonce/weak and the target handles this by emitting it |
| 780 | // into a 'uniqued' section name, create and return the section now. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 781 | // Section names depend on the name of the symbol which is not feasible if the |
| 782 | // symbol has private linkage. |
| 783 | if ((GV->isWeakForLinker() || EmitUniquedSection) && |
| 784 | !GV->hasPrivateLinkage()) { |
| 785 | const char *Name = getCOFFSectionNameForUniqueGlobal(Kind); |
Chris Lattner | 6e5ce28 | 2010-05-07 21:49:09 +0000 | [diff] [blame] | 786 | unsigned Characteristics = getCOFFSectionFlags(Kind); |
| 787 | |
Daniel Dunbar | 9461058 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 788 | Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 789 | MCSymbol *Sym = TM.getSymbol(GV, Mang); |
| 790 | return getContext().getCOFFSection( |
| 791 | Name, Characteristics, Kind, Sym->getName(), |
| 792 | GV->isWeakForLinker() ? COFF::IMAGE_COMDAT_SELECT_ANY |
| 793 | : COFF::IMAGE_COMDAT_SELECT_NODUPLICATES); |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | if (Kind.isText()) |
David Majnemer | 6aa9315 | 2013-08-13 01:23:53 +0000 | [diff] [blame] | 797 | return TextSection; |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 798 | |
Anton Korobeynikov | d4a19b6 | 2012-02-11 17:26:53 +0000 | [diff] [blame] | 799 | if (Kind.isThreadLocal()) |
David Majnemer | 6aa9315 | 2013-08-13 01:23:53 +0000 | [diff] [blame] | 800 | return TLSDataSection; |
Anton Korobeynikov | d4a19b6 | 2012-02-11 17:26:53 +0000 | [diff] [blame] | 801 | |
David Majnemer | 6aa9315 | 2013-08-13 01:23:53 +0000 | [diff] [blame] | 802 | if (Kind.isReadOnly()) |
David Majnemer | 9706d43 | 2013-08-08 01:50:52 +0000 | [diff] [blame] | 803 | return ReadOnlySection; |
| 804 | |
David Majnemer | 6aa9315 | 2013-08-13 01:23:53 +0000 | [diff] [blame] | 805 | if (Kind.isBSS()) |
| 806 | return BSSSection; |
| 807 | |
| 808 | return DataSection; |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 809 | } |
| 810 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 811 | StringRef TargetLoweringObjectFileCOFF:: |
| 812 | getDepLibFromLinkerOpt(StringRef LinkerOption) const { |
| 813 | const char *LibCmd = "/DEFAULTLIB:"; |
| 814 | if (LinkerOption.startswith(LibCmd)) |
| 815 | return LinkerOption.substr(strlen(LibCmd)); |
| 816 | return StringRef(); |
| 817 | } |
| 818 | |
Reid Kleckner | 4b55815 | 2013-04-25 19:34:41 +0000 | [diff] [blame] | 819 | void TargetLoweringObjectFileCOFF:: |
| 820 | emitModuleFlags(MCStreamer &Streamer, |
| 821 | ArrayRef<Module::ModuleFlagEntry> ModuleFlags, |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 822 | Mangler &Mang, const TargetMachine &TM) const { |
Reid Kleckner | 4b55815 | 2013-04-25 19:34:41 +0000 | [diff] [blame] | 823 | MDNode *LinkerOptions = 0; |
| 824 | |
| 825 | // Look for the "Linker Options" flag, since it's the only one we support. |
| 826 | for (ArrayRef<Module::ModuleFlagEntry>::iterator |
| 827 | i = ModuleFlags.begin(), e = ModuleFlags.end(); i != e; ++i) { |
| 828 | const Module::ModuleFlagEntry &MFE = *i; |
| 829 | StringRef Key = MFE.Key->getString(); |
| 830 | Value *Val = MFE.Val; |
| 831 | if (Key == "Linker Options") { |
| 832 | LinkerOptions = cast<MDNode>(Val); |
| 833 | break; |
| 834 | } |
| 835 | } |
| 836 | if (!LinkerOptions) |
| 837 | return; |
| 838 | |
| 839 | // Emit the linker options to the linker .drectve section. According to the |
| 840 | // spec, this section is a space-separated string containing flags for linker. |
| 841 | const MCSection *Sec = getDrectveSection(); |
| 842 | Streamer.SwitchSection(Sec); |
| 843 | for (unsigned i = 0, e = LinkerOptions->getNumOperands(); i != e; ++i) { |
| 844 | MDNode *MDOptions = cast<MDNode>(LinkerOptions->getOperand(i)); |
| 845 | for (unsigned ii = 0, ie = MDOptions->getNumOperands(); ii != ie; ++ii) { |
| 846 | MDString *MDOption = cast<MDString>(MDOptions->getOperand(ii)); |
| 847 | StringRef Op = MDOption->getString(); |
| 848 | // Lead with a space for consistency with our dllexport implementation. |
| 849 | std::string Escaped(" "); |
| 850 | if (Op.find(" ") != StringRef::npos) { |
| 851 | // The PE-COFF spec says args with spaces must be quoted. It doesn't say |
| 852 | // how to escape quotes, but it probably uses this algorithm: |
| 853 | // http://msdn.microsoft.com/en-us/library/17w5ykft(v=vs.85).aspx |
| 854 | // FIXME: Reuse escaping code from Support/Windows/Program.inc |
| 855 | Escaped.push_back('\"'); |
| 856 | Escaped.append(Op); |
| 857 | Escaped.push_back('\"'); |
| 858 | } else { |
| 859 | Escaped.append(Op); |
| 860 | } |
| 861 | Streamer.EmitBytes(Escaped); |
| 862 | } |
| 863 | } |
| 864 | } |