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