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