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