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