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