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