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