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" |
Peter Collingbourne | 94d7786 | 2015-11-03 23:40:03 +0000 | [diff] [blame] | 27 | #include "llvm/MC/MCAsmInfo.h" |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 28 | #include "llvm/MC/MCContext.h" |
| 29 | #include "llvm/MC/MCExpr.h" |
Chris Lattner | 87cffa9 | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 30 | #include "llvm/MC/MCSectionCOFF.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 31 | #include "llvm/MC/MCSectionELF.h" |
| 32 | #include "llvm/MC/MCSectionMachO.h" |
Rafael Espindola | a83b177 | 2011-04-16 03:51:21 +0000 | [diff] [blame] | 33 | #include "llvm/MC/MCStreamer.h" |
Rafael Espindola | a869576 | 2015-06-02 00:25:12 +0000 | [diff] [blame] | 34 | #include "llvm/MC/MCSymbolELF.h" |
Bruno Cardoso Lopes | 618c67a | 2015-03-06 13:49:05 +0000 | [diff] [blame] | 35 | #include "llvm/MC/MCValue.h" |
Xinliang David Li | 5f04f92 | 2016-01-14 18:09:45 +0000 | [diff] [blame] | 36 | #include "llvm/ProfileData/InstrProf.h" |
Dehao Chen | f84b630 | 2016-02-23 03:39:24 +0000 | [diff] [blame] | 37 | #include "llvm/ProfileData/ProfileCommon.h" |
Reid Kleckner | 1f13d47 | 2015-09-03 16:41:50 +0000 | [diff] [blame] | 38 | #include "llvm/Support/COFF.h" |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 39 | #include "llvm/Support/Dwarf.h" |
Rafael Espindola | aea4958 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 40 | #include "llvm/Support/ELF.h" |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 41 | #include "llvm/Support/ErrorHandling.h" |
| 42 | #include "llvm/Support/raw_ostream.h" |
Rafael Espindola | daeafb4 | 2014-02-19 17:23:20 +0000 | [diff] [blame] | 43 | #include "llvm/Target/TargetLowering.h" |
Chandler Carruth | 442f784 | 2014-03-04 10:07:28 +0000 | [diff] [blame] | 44 | #include "llvm/Target/TargetMachine.h" |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 45 | #include "llvm/Target/TargetSubtargetInfo.h" |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 46 | using namespace llvm; |
Anton Korobeynikov | 31a9212 | 2010-02-21 20:28:15 +0000 | [diff] [blame] | 47 | using namespace dwarf; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 48 | |
| 49 | //===----------------------------------------------------------------------===// |
| 50 | // ELF |
| 51 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 52 | |
Rafael Espindola | daeafb4 | 2014-02-19 17:23:20 +0000 | [diff] [blame] | 53 | MCSymbol *TargetLoweringObjectFileELF::getCFIPersonalitySymbol( |
| 54 | const GlobalValue *GV, Mangler &Mang, const TargetMachine &TM, |
| 55 | MachineModuleInfo *MMI) const { |
Rafael Espindola | ce83fc3 | 2011-04-27 23:17:57 +0000 | [diff] [blame] | 56 | unsigned Encoding = getPersonalityEncoding(); |
Logan Chien | c002981 | 2014-05-30 16:48:56 +0000 | [diff] [blame] | 57 | if ((Encoding & 0x80) == dwarf::DW_EH_PE_indirect) |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 58 | return getContext().getOrCreateSymbol(StringRef("DW.ref.") + |
Rafael Espindola | a3ad4e6 | 2014-02-19 20:30:41 +0000 | [diff] [blame] | 59 | TM.getSymbol(GV, Mang)->getName()); |
Logan Chien | c002981 | 2014-05-30 16:48:56 +0000 | [diff] [blame] | 60 | if ((Encoding & 0x70) == dwarf::DW_EH_PE_absptr) |
| 61 | return TM.getSymbol(GV, Mang); |
| 62 | report_fatal_error("We do not support this DWARF encoding yet!"); |
Rafael Espindola | a83b177 | 2011-04-16 03:51:21 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Mehdi Amini | 5c0fa58 | 2015-07-16 06:04:17 +0000 | [diff] [blame] | 65 | void TargetLoweringObjectFileELF::emitPersonalityValue( |
| 66 | MCStreamer &Streamer, const DataLayout &DL, const MCSymbol *Sym) const { |
Rafael Espindola | 51d2d7a | 2011-06-13 03:09:13 +0000 | [diff] [blame] | 67 | SmallString<64> NameData("DW.ref."); |
| 68 | NameData += Sym->getName(); |
Rafael Espindola | a869576 | 2015-06-02 00:25:12 +0000 | [diff] [blame] | 69 | MCSymbolELF *Label = |
| 70 | cast<MCSymbolELF>(getContext().getOrCreateSymbol(NameData)); |
Rafael Espindola | 3989776 | 2011-04-27 21:29:52 +0000 | [diff] [blame] | 71 | Streamer.EmitSymbolAttribute(Label, MCSA_Hidden); |
| 72 | Streamer.EmitSymbolAttribute(Label, MCSA_Weak); |
Rafael Espindola | 51d2d7a | 2011-06-13 03:09:13 +0000 | [diff] [blame] | 73 | StringRef Prefix = ".data."; |
| 74 | NameData.insert(NameData.begin(), Prefix.begin(), Prefix.end()); |
Rafael Espindola | 3989776 | 2011-04-27 21:29:52 +0000 | [diff] [blame] | 75 | unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE | ELF::SHF_GROUP; |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 76 | MCSection *Sec = getContext().getELFSection(NameData, ELF::SHT_PROGBITS, |
| 77 | Flags, 0, Label->getName()); |
Mehdi Amini | 5c0fa58 | 2015-07-16 06:04:17 +0000 | [diff] [blame] | 78 | unsigned Size = DL.getPointerSize(); |
Rafael Espindola | 3989776 | 2011-04-27 21:29:52 +0000 | [diff] [blame] | 79 | Streamer.SwitchSection(Sec); |
Mehdi Amini | 5c0fa58 | 2015-07-16 06:04:17 +0000 | [diff] [blame] | 80 | Streamer.EmitValueToAlignment(DL.getPointerABIAlignment()); |
Rafael Espindola | 3989776 | 2011-04-27 21:29:52 +0000 | [diff] [blame] | 81 | Streamer.EmitSymbolAttribute(Label, MCSA_ELF_TypeObject); |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 82 | const MCExpr *E = MCConstantExpr::create(Size, getContext()); |
Rafael Espindola | a869576 | 2015-06-02 00:25:12 +0000 | [diff] [blame] | 83 | Streamer.emitELFSize(Label, E); |
Rafael Espindola | 3989776 | 2011-04-27 21:29:52 +0000 | [diff] [blame] | 84 | Streamer.EmitLabel(Label); |
Rafael Espindola | a83b177 | 2011-04-16 03:51:21 +0000 | [diff] [blame] | 85 | |
Rafael Espindola | 3989776 | 2011-04-27 21:29:52 +0000 | [diff] [blame] | 86 | Streamer.EmitSymbolValue(Sym, Size); |
Rafael Espindola | a83b177 | 2011-04-16 03:51:21 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Rafael Espindola | 15b2669 | 2014-02-09 14:50:44 +0000 | [diff] [blame] | 89 | const MCExpr *TargetLoweringObjectFileELF::getTTypeGlobalReference( |
| 90 | const GlobalValue *GV, unsigned Encoding, Mangler &Mang, |
Rafael Espindola | daeafb4 | 2014-02-19 17:23:20 +0000 | [diff] [blame] | 91 | const TargetMachine &TM, MachineModuleInfo *MMI, |
| 92 | MCStreamer &Streamer) const { |
Anton Korobeynikov | e42af36 | 2012-11-14 01:47:00 +0000 | [diff] [blame] | 93 | |
| 94 | if (Encoding & dwarf::DW_EH_PE_indirect) { |
| 95 | MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>(); |
| 96 | |
Rafael Espindola | daeafb4 | 2014-02-19 17:23:20 +0000 | [diff] [blame] | 97 | MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, ".DW.stub", Mang, TM); |
Anton Korobeynikov | e42af36 | 2012-11-14 01:47:00 +0000 | [diff] [blame] | 98 | |
| 99 | // Add information about the stub reference to ELFMMI so that the stub |
| 100 | // gets emitted by the asmprinter. |
Anton Korobeynikov | e42af36 | 2012-11-14 01:47:00 +0000 | [diff] [blame] | 101 | MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 102 | if (!StubSym.getPointer()) { |
Rafael Espindola | a3ad4e6 | 2014-02-19 20:30:41 +0000 | [diff] [blame] | 103 | MCSymbol *Sym = TM.getSymbol(GV, Mang); |
Anton Korobeynikov | e42af36 | 2012-11-14 01:47:00 +0000 | [diff] [blame] | 104 | StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage()); |
| 105 | } |
| 106 | |
| 107 | return TargetLoweringObjectFile:: |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 108 | getTTypeReference(MCSymbolRefExpr::create(SSym, getContext()), |
Anton Korobeynikov | e42af36 | 2012-11-14 01:47:00 +0000 | [diff] [blame] | 109 | Encoding & ~dwarf::DW_EH_PE_indirect, Streamer); |
| 110 | } |
| 111 | |
Rafael Espindola | daeafb4 | 2014-02-19 17:23:20 +0000 | [diff] [blame] | 112 | return TargetLoweringObjectFile:: |
| 113 | getTTypeGlobalReference(GV, Encoding, Mang, TM, MMI, Streamer); |
Anton Korobeynikov | e42af36 | 2012-11-14 01:47:00 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 116 | static SectionKind |
| 117 | getELFKindForNamedSection(StringRef Name, SectionKind K) { |
Rafael Espindola | 0d018b1 | 2011-05-24 03:10:31 +0000 | [diff] [blame] | 118 | // N.B.: The defaults used in here are no the same ones used in MC. |
| 119 | // We follow gcc, MC follows gas. For example, given ".section .eh_frame", |
| 120 | // both gas and MC will produce a section with no flags. Given |
Bill Wendling | d163405 | 2012-07-19 00:04:14 +0000 | [diff] [blame] | 121 | // section(".eh_frame") gcc will produce: |
| 122 | // |
| 123 | // .section .eh_frame,"a",@progbits |
Xinliang David Li | 5f04f92 | 2016-01-14 18:09:45 +0000 | [diff] [blame] | 124 | |
| 125 | if (Name == getInstrProfCoverageSectionName(false)) |
| 126 | return SectionKind::getMetadata(); |
| 127 | |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 128 | if (Name.empty() || Name[0] != '.') return K; |
| 129 | |
| 130 | // Some lame default implementation based on some magic section names. |
| 131 | if (Name == ".bss" || |
| 132 | Name.startswith(".bss.") || |
| 133 | Name.startswith(".gnu.linkonce.b.") || |
| 134 | Name.startswith(".llvm.linkonce.b.") || |
| 135 | Name == ".sbss" || |
| 136 | Name.startswith(".sbss.") || |
| 137 | Name.startswith(".gnu.linkonce.sb.") || |
| 138 | Name.startswith(".llvm.linkonce.sb.")) |
| 139 | return SectionKind::getBSS(); |
| 140 | |
| 141 | if (Name == ".tdata" || |
| 142 | Name.startswith(".tdata.") || |
| 143 | Name.startswith(".gnu.linkonce.td.") || |
| 144 | Name.startswith(".llvm.linkonce.td.")) |
| 145 | return SectionKind::getThreadData(); |
| 146 | |
| 147 | if (Name == ".tbss" || |
| 148 | Name.startswith(".tbss.") || |
| 149 | Name.startswith(".gnu.linkonce.tb.") || |
| 150 | Name.startswith(".llvm.linkonce.tb.")) |
| 151 | return SectionKind::getThreadBSS(); |
| 152 | |
| 153 | return K; |
| 154 | } |
| 155 | |
| 156 | |
| 157 | static unsigned getELFSectionType(StringRef Name, SectionKind K) { |
| 158 | |
| 159 | if (Name == ".init_array") |
Rafael Espindola | aea4958 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 160 | return ELF::SHT_INIT_ARRAY; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 161 | |
| 162 | if (Name == ".fini_array") |
Rafael Espindola | aea4958 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 163 | return ELF::SHT_FINI_ARRAY; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 164 | |
| 165 | if (Name == ".preinit_array") |
Rafael Espindola | aea4958 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 166 | return ELF::SHT_PREINIT_ARRAY; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 167 | |
| 168 | if (K.isBSS() || K.isThreadBSS()) |
Rafael Espindola | aea4958 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 169 | return ELF::SHT_NOBITS; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 170 | |
Rafael Espindola | aea4958 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 171 | return ELF::SHT_PROGBITS; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Rafael Espindola | 8bc9ccc | 2015-02-25 00:52:15 +0000 | [diff] [blame] | 174 | static unsigned getELFSectionFlags(SectionKind K) { |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 175 | unsigned Flags = 0; |
| 176 | |
| 177 | if (!K.isMetadata()) |
Rafael Espindola | 0e7e34e | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 178 | Flags |= ELF::SHF_ALLOC; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 179 | |
| 180 | if (K.isText()) |
Rafael Espindola | 0e7e34e | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 181 | Flags |= ELF::SHF_EXECINSTR; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 182 | |
Rafael Espindola | c85e0d8 | 2011-06-07 23:26:45 +0000 | [diff] [blame] | 183 | if (K.isWriteable()) |
Rafael Espindola | 0e7e34e | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 184 | Flags |= ELF::SHF_WRITE; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 185 | |
| 186 | if (K.isThreadLocal()) |
Rafael Espindola | 0e7e34e | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 187 | Flags |= ELF::SHF_TLS; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 188 | |
Rafael Espindola | 8bc9ccc | 2015-02-25 00:52:15 +0000 | [diff] [blame] | 189 | if (K.isMergeableCString() || K.isMergeableConst()) |
Rafael Espindola | 0e7e34e | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 190 | Flags |= ELF::SHF_MERGE; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 191 | |
| 192 | if (K.isMergeableCString()) |
Rafael Espindola | 0e7e34e | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 193 | Flags |= ELF::SHF_STRINGS; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 194 | |
| 195 | return Flags; |
| 196 | } |
| 197 | |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 198 | static const Comdat *getELFComdat(const GlobalValue *GV) { |
| 199 | const Comdat *C = GV->getComdat(); |
| 200 | if (!C) |
| 201 | return nullptr; |
| 202 | |
| 203 | if (C->getSelectionKind() != Comdat::Any) |
| 204 | report_fatal_error("ELF COMDATs only support SelectionKind::Any, '" + |
| 205 | C->getName() + "' cannot be lowered."); |
| 206 | |
| 207 | return C; |
| 208 | } |
| 209 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 210 | MCSection *TargetLoweringObjectFileELF::getExplicitSectionGlobal( |
Rafael Espindola | daeafb4 | 2014-02-19 17:23:20 +0000 | [diff] [blame] | 211 | const GlobalValue *GV, SectionKind Kind, Mangler &Mang, |
| 212 | const TargetMachine &TM) const { |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 213 | StringRef SectionName = GV->getSection(); |
| 214 | |
| 215 | // Infer section flags from the section name if we can. |
| 216 | Kind = getELFKindForNamedSection(SectionName, Kind); |
| 217 | |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 218 | StringRef Group = ""; |
Rafael Espindola | 8bc9ccc | 2015-02-25 00:52:15 +0000 | [diff] [blame] | 219 | unsigned Flags = getELFSectionFlags(Kind); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 220 | if (const Comdat *C = getELFComdat(GV)) { |
| 221 | Group = C->getName(); |
| 222 | Flags |= ELF::SHF_GROUP; |
| 223 | } |
Chris Lattner | 80c3459 | 2010-04-08 21:34:17 +0000 | [diff] [blame] | 224 | return getContext().getELFSection(SectionName, |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 225 | getELFSectionType(SectionName, Kind), Flags, |
Rafael Espindola | ba31e27 | 2015-01-29 17:33:21 +0000 | [diff] [blame] | 226 | /*EntrySize=*/0, Group); |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Rafael Espindola | 25d2c20 | 2015-02-11 14:44:17 +0000 | [diff] [blame] | 229 | /// Return the section prefix name used by options FunctionsSections and |
| 230 | /// DataSections. |
David Majnemer | 102ff69 | 2014-06-24 16:01:53 +0000 | [diff] [blame] | 231 | static StringRef getSectionPrefixForGlobal(SectionKind Kind) { |
Rafael Espindola | 25d2c20 | 2015-02-11 14:44:17 +0000 | [diff] [blame] | 232 | if (Kind.isText()) |
Rafael Espindola | 68fa249 | 2015-02-17 20:48:01 +0000 | [diff] [blame] | 233 | return ".text"; |
Rafael Espindola | 25d2c20 | 2015-02-11 14:44:17 +0000 | [diff] [blame] | 234 | if (Kind.isReadOnly()) |
Rafael Espindola | 68fa249 | 2015-02-17 20:48:01 +0000 | [diff] [blame] | 235 | return ".rodata"; |
Rafael Espindola | 25d2c20 | 2015-02-11 14:44:17 +0000 | [diff] [blame] | 236 | if (Kind.isBSS()) |
Rafael Espindola | 68fa249 | 2015-02-17 20:48:01 +0000 | [diff] [blame] | 237 | return ".bss"; |
Rafael Espindola | 25d2c20 | 2015-02-11 14:44:17 +0000 | [diff] [blame] | 238 | if (Kind.isThreadData()) |
Rafael Espindola | 68fa249 | 2015-02-17 20:48:01 +0000 | [diff] [blame] | 239 | return ".tdata"; |
Rafael Espindola | 25d2c20 | 2015-02-11 14:44:17 +0000 | [diff] [blame] | 240 | if (Kind.isThreadBSS()) |
Rafael Espindola | 68fa249 | 2015-02-17 20:48:01 +0000 | [diff] [blame] | 241 | return ".tbss"; |
Rafael Espindola | 449711c | 2015-11-18 06:02:15 +0000 | [diff] [blame] | 242 | if (Kind.isData()) |
Rafael Espindola | 68fa249 | 2015-02-17 20:48:01 +0000 | [diff] [blame] | 243 | return ".data"; |
Chris Lattner | 5b212a3 | 2010-04-13 00:36:43 +0000 | [diff] [blame] | 244 | assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); |
Rafael Espindola | 68fa249 | 2015-02-17 20:48:01 +0000 | [diff] [blame] | 245 | return ".data.rel.ro"; |
Chris Lattner | 5b212a3 | 2010-04-13 00:36:43 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Dehao Chen | f84b630 | 2016-02-23 03:39:24 +0000 | [diff] [blame] | 248 | static cl::opt<bool> GroupFunctionsByHotness( |
| 249 | "group-functions-by-hotness", |
| 250 | llvm::cl::desc("Partition hot/cold functions by sections prefix"), |
| 251 | cl::init(false)); |
| 252 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 253 | static MCSectionELF * |
Rafael Espindola | 8ca44f0 | 2015-04-04 18:02:01 +0000 | [diff] [blame] | 254 | selectELFSectionForGlobal(MCContext &Ctx, const GlobalValue *GV, |
| 255 | SectionKind Kind, Mangler &Mang, |
| 256 | const TargetMachine &TM, bool EmitUniqueSection, |
| 257 | unsigned Flags, unsigned *NextUniqueID) { |
Rafael Espindola | 8bc9ccc | 2015-02-25 00:52:15 +0000 | [diff] [blame] | 258 | unsigned EntrySize = 0; |
| 259 | if (Kind.isMergeableCString()) { |
| 260 | if (Kind.isMergeable2ByteCString()) { |
| 261 | EntrySize = 2; |
| 262 | } else if (Kind.isMergeable4ByteCString()) { |
| 263 | EntrySize = 4; |
| 264 | } else { |
| 265 | EntrySize = 1; |
| 266 | assert(Kind.isMergeable1ByteCString() && "unknown string width"); |
| 267 | } |
| 268 | } else if (Kind.isMergeableConst()) { |
| 269 | if (Kind.isMergeableConst4()) { |
| 270 | EntrySize = 4; |
| 271 | } else if (Kind.isMergeableConst8()) { |
| 272 | EntrySize = 8; |
David Majnemer | 964b70d | 2016-02-22 22:23:11 +0000 | [diff] [blame] | 273 | } else if (Kind.isMergeableConst16()) { |
Rafael Espindola | 8bc9ccc | 2015-02-25 00:52:15 +0000 | [diff] [blame] | 274 | EntrySize = 16; |
David Majnemer | 964b70d | 2016-02-22 22:23:11 +0000 | [diff] [blame] | 275 | } else { |
| 276 | assert(Kind.isMergeableConst32() && "unknown data width"); |
| 277 | EntrySize = 32; |
Rafael Espindola | 8bc9ccc | 2015-02-25 00:52:15 +0000 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | |
Rafael Espindola | 9075f77 | 2015-02-20 23:28:28 +0000 | [diff] [blame] | 281 | StringRef Group = ""; |
Rafael Espindola | 8bc9ccc | 2015-02-25 00:52:15 +0000 | [diff] [blame] | 282 | if (const Comdat *C = getELFComdat(GV)) { |
Rafael Espindola | 9075f77 | 2015-02-20 23:28:28 +0000 | [diff] [blame] | 283 | Flags |= ELF::SHF_GROUP; |
| 284 | Group = C->getName(); |
| 285 | } |
| 286 | |
Rafael Espindola | 8bc9ccc | 2015-02-25 00:52:15 +0000 | [diff] [blame] | 287 | bool UniqueSectionNames = TM.getUniqueSectionNames(); |
| 288 | SmallString<128> Name; |
Rafael Espindola | a05b3b7 | 2015-01-28 17:54:19 +0000 | [diff] [blame] | 289 | if (Kind.isMergeableCString()) { |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 290 | // We also need alignment here. |
| 291 | // FIXME: this is getting the alignment of the character, not the |
| 292 | // alignment of the global! |
Mehdi Amini | 5c0fa58 | 2015-07-16 06:04:17 +0000 | [diff] [blame] | 293 | unsigned Align = GV->getParent()->getDataLayout().getPreferredAlignment( |
| 294 | cast<GlobalVariable>(GV)); |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 295 | |
Rafael Espindola | ba31e27 | 2015-01-29 17:33:21 +0000 | [diff] [blame] | 296 | std::string SizeSpec = ".rodata.str" + utostr(EntrySize) + "."; |
Rafael Espindola | 8bc9ccc | 2015-02-25 00:52:15 +0000 | [diff] [blame] | 297 | Name = SizeSpec + utostr(Align); |
| 298 | } else if (Kind.isMergeableConst()) { |
| 299 | Name = ".rodata.cst"; |
| 300 | Name += utostr(EntrySize); |
| 301 | } else { |
| 302 | Name = getSectionPrefixForGlobal(Kind); |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Dehao Chen | f84b630 | 2016-02-23 03:39:24 +0000 | [diff] [blame] | 305 | if (GroupFunctionsByHotness) { |
| 306 | if (const Function *F = dyn_cast<Function>(GV)) { |
| 307 | if (ProfileSummary::isFunctionHot(F)) { |
| 308 | Name += getHotSectionPrefix(); |
| 309 | } else if (ProfileSummary::isFunctionUnlikely(F)) { |
| 310 | Name += getUnlikelySectionPrefix(); |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | |
Rafael Espindola | 8bc9ccc | 2015-02-25 00:52:15 +0000 | [diff] [blame] | 315 | if (EmitUniqueSection && UniqueSectionNames) { |
| 316 | Name.push_back('.'); |
| 317 | TM.getNameWithPrefix(Name, GV, Mang, true); |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 318 | } |
Rafael Espindola | 8ca44f0 | 2015-04-04 18:02:01 +0000 | [diff] [blame] | 319 | unsigned UniqueID = ~0; |
| 320 | if (EmitUniqueSection && !UniqueSectionNames) { |
| 321 | UniqueID = *NextUniqueID; |
| 322 | (*NextUniqueID)++; |
| 323 | } |
Rafael Espindola | 4491d0d | 2015-02-26 23:55:11 +0000 | [diff] [blame] | 324 | return Ctx.getELFSection(Name, getELFSectionType(Name, Kind), Flags, |
Rafael Espindola | 8ca44f0 | 2015-04-04 18:02:01 +0000 | [diff] [blame] | 325 | EntrySize, Group, UniqueID); |
Rafael Espindola | 4491d0d | 2015-02-26 23:55:11 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 328 | MCSection *TargetLoweringObjectFileELF::SelectSectionForGlobal( |
Rafael Espindola | 4491d0d | 2015-02-26 23:55:11 +0000 | [diff] [blame] | 329 | const GlobalValue *GV, SectionKind Kind, Mangler &Mang, |
| 330 | const TargetMachine &TM) const { |
| 331 | unsigned Flags = getELFSectionFlags(Kind); |
| 332 | |
| 333 | // If we have -ffunction-section or -fdata-section then we should emit the |
| 334 | // global value to a uniqued section specifically for it. |
| 335 | bool EmitUniqueSection = false; |
| 336 | if (!(Flags & ELF::SHF_MERGE) && !Kind.isCommon()) { |
| 337 | if (Kind.isText()) |
| 338 | EmitUniqueSection = TM.getFunctionSections(); |
| 339 | else |
| 340 | EmitUniqueSection = TM.getDataSections(); |
| 341 | } |
| 342 | EmitUniqueSection |= GV->hasComdat(); |
| 343 | |
| 344 | return selectELFSectionForGlobal(getContext(), GV, Kind, Mang, TM, |
Rafael Espindola | 8ca44f0 | 2015-04-04 18:02:01 +0000 | [diff] [blame] | 345 | EmitUniqueSection, Flags, &NextUniqueID); |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 348 | MCSection *TargetLoweringObjectFileELF::getSectionForJumpTable( |
Rafael Espindola | 29786d4 | 2015-02-12 17:16:46 +0000 | [diff] [blame] | 349 | const Function &F, Mangler &Mang, const TargetMachine &TM) const { |
| 350 | // If the function can be removed, produce a unique section so that |
| 351 | // the table doesn't prevent the removal. |
| 352 | const Comdat *C = F.getComdat(); |
| 353 | bool EmitUniqueSection = TM.getFunctionSections() || C; |
| 354 | if (!EmitUniqueSection) |
| 355 | return ReadOnlySection; |
| 356 | |
Rafael Espindola | 4491d0d | 2015-02-26 23:55:11 +0000 | [diff] [blame] | 357 | return selectELFSectionForGlobal(getContext(), &F, SectionKind::getReadOnly(), |
Rafael Espindola | 8ca44f0 | 2015-04-04 18:02:01 +0000 | [diff] [blame] | 358 | Mang, TM, EmitUniqueSection, ELF::SHF_ALLOC, |
| 359 | &NextUniqueID); |
Rafael Espindola | 29786d4 | 2015-02-12 17:16:46 +0000 | [diff] [blame] | 360 | } |
| 361 | |
Rafael Espindola | df19519 | 2015-02-17 23:34:51 +0000 | [diff] [blame] | 362 | bool TargetLoweringObjectFileELF::shouldPutJumpTableInFunctionSection( |
| 363 | bool UsesLabelDifference, const Function &F) const { |
| 364 | // We can always create relative relocations, so use another section |
| 365 | // that can be marked non-executable. |
| 366 | return false; |
| 367 | } |
| 368 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 369 | /// Given a mergeable constant with the specified size and relocation |
| 370 | /// information, return a section that it should be placed in. |
Mehdi Amini | 5c0fa58 | 2015-07-16 06:04:17 +0000 | [diff] [blame] | 371 | MCSection *TargetLoweringObjectFileELF::getSectionForConstant( |
David Majnemer | a3ea407 | 2016-02-21 01:30:30 +0000 | [diff] [blame] | 372 | const DataLayout &DL, SectionKind Kind, const Constant *C, |
| 373 | unsigned &Align) const { |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 374 | if (Kind.isMergeableConst4() && MergeableConst4Section) |
| 375 | return MergeableConst4Section; |
| 376 | if (Kind.isMergeableConst8() && MergeableConst8Section) |
| 377 | return MergeableConst8Section; |
| 378 | if (Kind.isMergeableConst16() && MergeableConst16Section) |
| 379 | return MergeableConst16Section; |
David Majnemer | 964b70d | 2016-02-22 22:23:11 +0000 | [diff] [blame] | 380 | if (Kind.isMergeableConst32() && MergeableConst32Section) |
| 381 | return MergeableConst32Section; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 382 | if (Kind.isReadOnly()) |
| 383 | return ReadOnlySection; |
| 384 | |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 385 | assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); |
| 386 | return DataRelROSection; |
| 387 | } |
| 388 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 389 | static MCSectionELF *getStaticStructorSection(MCContext &Ctx, bool UseInitArray, |
| 390 | bool IsCtor, unsigned Priority, |
| 391 | const MCSymbol *KeySym) { |
Rafael Espindola | c4b4253 | 2014-09-04 23:03:58 +0000 | [diff] [blame] | 392 | std::string Name; |
| 393 | unsigned Type; |
| 394 | unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE; |
Rafael Espindola | c4b4253 | 2014-09-04 23:03:58 +0000 | [diff] [blame] | 395 | StringRef COMDAT = KeySym ? KeySym->getName() : ""; |
| 396 | |
| 397 | if (KeySym) |
| 398 | Flags |= ELF::SHF_GROUP; |
Anton Korobeynikov | 7722a2d | 2012-01-25 22:24:19 +0000 | [diff] [blame] | 399 | |
Rafael Espindola | ca3e0ee | 2012-06-19 00:48:28 +0000 | [diff] [blame] | 400 | if (UseInitArray) { |
Rafael Espindola | 7c7d7b9 | 2014-09-05 00:02:50 +0000 | [diff] [blame] | 401 | if (IsCtor) { |
| 402 | Type = ELF::SHT_INIT_ARRAY; |
| 403 | Name = ".init_array"; |
| 404 | } else { |
| 405 | Type = ELF::SHT_FINI_ARRAY; |
| 406 | Name = ".fini_array"; |
| 407 | } |
Rafael Espindola | c4b4253 | 2014-09-04 23:03:58 +0000 | [diff] [blame] | 408 | if (Priority != 65535) { |
| 409 | Name += '.'; |
| 410 | Name += utostr(Priority); |
| 411 | } |
Rafael Espindola | ca3e0ee | 2012-06-19 00:48:28 +0000 | [diff] [blame] | 412 | } else { |
Rafael Espindola | c4b4253 | 2014-09-04 23:03:58 +0000 | [diff] [blame] | 413 | // The default scheme is .ctor / .dtor, so we have to invert the priority |
| 414 | // numbering. |
Rafael Espindola | 7c7d7b9 | 2014-09-05 00:02:50 +0000 | [diff] [blame] | 415 | if (IsCtor) |
| 416 | Name = ".ctors"; |
| 417 | else |
| 418 | Name = ".dtors"; |
Rafael Espindola | c4b4253 | 2014-09-04 23:03:58 +0000 | [diff] [blame] | 419 | if (Priority != 65535) { |
| 420 | Name += '.'; |
| 421 | Name += utostr(65535 - Priority); |
| 422 | } |
| 423 | Type = ELF::SHT_PROGBITS; |
Rafael Espindola | ca3e0ee | 2012-06-19 00:48:28 +0000 | [diff] [blame] | 424 | } |
Rafael Espindola | c4b4253 | 2014-09-04 23:03:58 +0000 | [diff] [blame] | 425 | |
Rafael Espindola | ba31e27 | 2015-01-29 17:33:21 +0000 | [diff] [blame] | 426 | return Ctx.getELFSection(Name, Type, Flags, 0, COMDAT); |
Rafael Espindola | 7c7d7b9 | 2014-09-05 00:02:50 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 429 | MCSection *TargetLoweringObjectFileELF::getStaticCtorSection( |
Rafael Espindola | 7c7d7b9 | 2014-09-05 00:02:50 +0000 | [diff] [blame] | 430 | unsigned Priority, const MCSymbol *KeySym) const { |
| 431 | return getStaticStructorSection(getContext(), UseInitArray, true, Priority, |
| 432 | KeySym); |
Anton Korobeynikov | 7722a2d | 2012-01-25 22:24:19 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 435 | MCSection *TargetLoweringObjectFileELF::getStaticDtorSection( |
Rafael Espindola | 0766ae0 | 2014-06-06 19:26:12 +0000 | [diff] [blame] | 436 | unsigned Priority, const MCSymbol *KeySym) const { |
Rafael Espindola | 7c7d7b9 | 2014-09-05 00:02:50 +0000 | [diff] [blame] | 437 | return getStaticStructorSection(getContext(), UseInitArray, false, Priority, |
| 438 | KeySym); |
Rafael Espindola | ca3e0ee | 2012-06-19 00:48:28 +0000 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | void |
| 442 | TargetLoweringObjectFileELF::InitializeELF(bool UseInitArray_) { |
| 443 | UseInitArray = UseInitArray_; |
| 444 | if (!UseInitArray) |
| 445 | return; |
| 446 | |
Rafael Espindola | ba31e27 | 2015-01-29 17:33:21 +0000 | [diff] [blame] | 447 | StaticCtorSection = getContext().getELFSection( |
| 448 | ".init_array", ELF::SHT_INIT_ARRAY, ELF::SHF_WRITE | ELF::SHF_ALLOC); |
| 449 | StaticDtorSection = getContext().getELFSection( |
| 450 | ".fini_array", ELF::SHT_FINI_ARRAY, ELF::SHF_WRITE | ELF::SHF_ALLOC); |
Anton Korobeynikov | 7722a2d | 2012-01-25 22:24:19 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 453 | //===----------------------------------------------------------------------===// |
| 454 | // MachO |
| 455 | //===----------------------------------------------------------------------===// |
| 456 | |
Bruno Cardoso Lopes | 618c67a | 2015-03-06 13:49:05 +0000 | [diff] [blame] | 457 | TargetLoweringObjectFileMachO::TargetLoweringObjectFileMachO() |
| 458 | : TargetLoweringObjectFile() { |
| 459 | SupportIndirectSymViaGOTPCRel = true; |
| 460 | } |
| 461 | |
Daniel Dunbar | 9585612 | 2013-01-18 19:37:00 +0000 | [diff] [blame] | 462 | /// emitModuleFlags - Perform code emission for module flags. |
Bill Wendling | 06df772 | 2012-02-14 21:28:13 +0000 | [diff] [blame] | 463 | void TargetLoweringObjectFileMachO:: |
Bill Wendling | 734909a | 2012-02-15 22:36:15 +0000 | [diff] [blame] | 464 | emitModuleFlags(MCStreamer &Streamer, |
| 465 | ArrayRef<Module::ModuleFlagEntry> ModuleFlags, |
Rafael Espindola | fa0f728 | 2014-02-08 14:53:28 +0000 | [diff] [blame] | 466 | Mangler &Mang, const TargetMachine &TM) const { |
Bill Wendling | 06df772 | 2012-02-14 21:28:13 +0000 | [diff] [blame] | 467 | unsigned VersionVal = 0; |
Bill Wendling | f1b14b7 | 2012-04-24 11:03:50 +0000 | [diff] [blame] | 468 | unsigned ImageInfoFlags = 0; |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 469 | MDNode *LinkerOptions = nullptr; |
Bill Wendling | 734909a | 2012-02-15 22:36:15 +0000 | [diff] [blame] | 470 | StringRef SectionVal; |
Bill Wendling | 06df772 | 2012-02-14 21:28:13 +0000 | [diff] [blame] | 471 | |
Bill Wendling | 734909a | 2012-02-15 22:36:15 +0000 | [diff] [blame] | 472 | for (ArrayRef<Module::ModuleFlagEntry>::iterator |
| 473 | i = ModuleFlags.begin(), e = ModuleFlags.end(); i != e; ++i) { |
| 474 | const Module::ModuleFlagEntry &MFE = *i; |
Bill Wendling | 06df772 | 2012-02-14 21:28:13 +0000 | [diff] [blame] | 475 | |
| 476 | // Ignore flags with 'Require' behavior. |
Bill Wendling | 734909a | 2012-02-15 22:36:15 +0000 | [diff] [blame] | 477 | if (MFE.Behavior == Module::Require) |
Bill Wendling | 06df772 | 2012-02-14 21:28:13 +0000 | [diff] [blame] | 478 | continue; |
| 479 | |
Bill Wendling | 734909a | 2012-02-15 22:36:15 +0000 | [diff] [blame] | 480 | StringRef Key = MFE.Key->getString(); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 481 | Metadata *Val = MFE.Val; |
Bill Wendling | 06df772 | 2012-02-14 21:28:13 +0000 | [diff] [blame] | 482 | |
Daniel Dunbar | 9585612 | 2013-01-18 19:37:00 +0000 | [diff] [blame] | 483 | if (Key == "Objective-C Image Info Version") { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 484 | VersionVal = mdconst::extract<ConstantInt>(Val)->getZExtValue(); |
Daniel Dunbar | 9585612 | 2013-01-18 19:37:00 +0000 | [diff] [blame] | 485 | } else if (Key == "Objective-C Garbage Collection" || |
| 486 | Key == "Objective-C GC Only" || |
Manman Ren | c98ec0e | 2014-11-21 19:24:55 +0000 | [diff] [blame] | 487 | Key == "Objective-C Is Simulated" || |
Manman Ren | c77e0ff | 2016-01-29 23:51:00 +0000 | [diff] [blame] | 488 | Key == "Objective-C Class Properties" || |
Manman Ren | c98ec0e | 2014-11-21 19:24:55 +0000 | [diff] [blame] | 489 | Key == "Objective-C Image Swift Version") { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 490 | ImageInfoFlags |= mdconst::extract<ConstantInt>(Val)->getZExtValue(); |
Daniel Dunbar | 9585612 | 2013-01-18 19:37:00 +0000 | [diff] [blame] | 491 | } else if (Key == "Objective-C Image Info Section") { |
Bill Wendling | 734909a | 2012-02-15 22:36:15 +0000 | [diff] [blame] | 492 | SectionVal = cast<MDString>(Val)->getString(); |
Daniel Dunbar | 9585612 | 2013-01-18 19:37:00 +0000 | [diff] [blame] | 493 | } else if (Key == "Linker Options") { |
| 494 | LinkerOptions = cast<MDNode>(Val); |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | // Emit the linker options if present. |
| 499 | if (LinkerOptions) { |
| 500 | for (unsigned i = 0, e = LinkerOptions->getNumOperands(); i != e; ++i) { |
| 501 | MDNode *MDOptions = cast<MDNode>(LinkerOptions->getOperand(i)); |
| 502 | SmallVector<std::string, 4> StrOptions; |
| 503 | |
| 504 | // Convert to strings. |
| 505 | for (unsigned ii = 0, ie = MDOptions->getNumOperands(); ii != ie; ++ii) { |
| 506 | MDString *MDOption = cast<MDString>(MDOptions->getOperand(ii)); |
| 507 | StrOptions.push_back(MDOption->getString()); |
| 508 | } |
| 509 | |
| 510 | Streamer.EmitLinkerOptions(StrOptions); |
| 511 | } |
Bill Wendling | 06df772 | 2012-02-14 21:28:13 +0000 | [diff] [blame] | 512 | } |
| 513 | |
Bill Wendling | 734909a | 2012-02-15 22:36:15 +0000 | [diff] [blame] | 514 | // The section is mandatory. If we don't have it, then we don't have GC info. |
| 515 | if (SectionVal.empty()) return; |
Bill Wendling | 06df772 | 2012-02-14 21:28:13 +0000 | [diff] [blame] | 516 | |
Bill Wendling | 734909a | 2012-02-15 22:36:15 +0000 | [diff] [blame] | 517 | StringRef Segment, Section; |
| 518 | unsigned TAA = 0, StubSize = 0; |
| 519 | bool TAAParsed; |
| 520 | std::string ErrorCode = |
| 521 | MCSectionMachO::ParseSectionSpecifier(SectionVal, Segment, Section, |
| 522 | TAA, TAAParsed, StubSize); |
Bill Wendling | a0009ee | 2012-02-15 22:47:53 +0000 | [diff] [blame] | 523 | if (!ErrorCode.empty()) |
Bill Wendling | 734909a | 2012-02-15 22:36:15 +0000 | [diff] [blame] | 524 | // If invalid, report the error with report_fatal_error. |
Bill Wendling | a0009ee | 2012-02-15 22:47:53 +0000 | [diff] [blame] | 525 | report_fatal_error("Invalid section specifier '" + Section + "': " + |
| 526 | ErrorCode + "."); |
Bill Wendling | 06df772 | 2012-02-14 21:28:13 +0000 | [diff] [blame] | 527 | |
Bill Wendling | 734909a | 2012-02-15 22:36:15 +0000 | [diff] [blame] | 528 | // Get the section. |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 529 | MCSectionMachO *S = getContext().getMachOSection( |
Rafael Espindola | 449711c | 2015-11-18 06:02:15 +0000 | [diff] [blame] | 530 | Segment, Section, TAA, StubSize, SectionKind::getData()); |
Bill Wendling | 734909a | 2012-02-15 22:36:15 +0000 | [diff] [blame] | 531 | Streamer.SwitchSection(S); |
| 532 | Streamer.EmitLabel(getContext(). |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 533 | getOrCreateSymbol(StringRef("L_OBJC_IMAGE_INFO"))); |
Bill Wendling | 06df772 | 2012-02-14 21:28:13 +0000 | [diff] [blame] | 534 | Streamer.EmitIntValue(VersionVal, 4); |
Bill Wendling | f1b14b7 | 2012-04-24 11:03:50 +0000 | [diff] [blame] | 535 | Streamer.EmitIntValue(ImageInfoFlags, 4); |
Bill Wendling | 06df772 | 2012-02-14 21:28:13 +0000 | [diff] [blame] | 536 | Streamer.AddBlankLine(); |
| 537 | } |
| 538 | |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 539 | static void checkMachOComdat(const GlobalValue *GV) { |
| 540 | const Comdat *C = GV->getComdat(); |
| 541 | if (!C) |
| 542 | return; |
| 543 | |
| 544 | report_fatal_error("MachO doesn't support COMDATs, '" + C->getName() + |
| 545 | "' cannot be lowered."); |
| 546 | } |
| 547 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 548 | MCSection *TargetLoweringObjectFileMachO::getExplicitSectionGlobal( |
Rafael Espindola | daeafb4 | 2014-02-19 17:23:20 +0000 | [diff] [blame] | 549 | const GlobalValue *GV, SectionKind Kind, Mangler &Mang, |
| 550 | const TargetMachine &TM) const { |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 551 | // Parse the section specifier and create it if valid. |
| 552 | StringRef Segment, Section; |
Stuart Hastings | 12d5312 | 2011-03-19 02:42:31 +0000 | [diff] [blame] | 553 | unsigned TAA = 0, StubSize = 0; |
| 554 | bool TAAParsed; |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 555 | |
| 556 | checkMachOComdat(GV); |
| 557 | |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 558 | std::string ErrorCode = |
| 559 | MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section, |
Stuart Hastings | 12d5312 | 2011-03-19 02:42:31 +0000 | [diff] [blame] | 560 | TAA, TAAParsed, StubSize); |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 561 | if (!ErrorCode.empty()) { |
Chris Lattner | 2104b8d | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 562 | // If invalid, report the error with report_fatal_error. |
Benjamin Kramer | 1f97a5a | 2011-11-15 16:27:03 +0000 | [diff] [blame] | 563 | report_fatal_error("Global variable '" + GV->getName() + |
| 564 | "' has an invalid section specifier '" + |
| 565 | GV->getSection() + "': " + ErrorCode + "."); |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | // Get the section. |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 569 | MCSectionMachO *S = |
| 570 | getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind); |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 571 | |
Stuart Hastings | b4863a4 | 2011-02-21 17:27:17 +0000 | [diff] [blame] | 572 | // If TAA wasn't set by ParseSectionSpecifier() above, |
| 573 | // use the value returned by getMachOSection() as a default. |
Stuart Hastings | 12d5312 | 2011-03-19 02:42:31 +0000 | [diff] [blame] | 574 | if (!TAAParsed) |
Stuart Hastings | b4863a4 | 2011-02-21 17:27:17 +0000 | [diff] [blame] | 575 | TAA = S->getTypeAndAttributes(); |
| 576 | |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 577 | // Okay, now that we got the section, verify that the TAA & StubSize agree. |
| 578 | // If the user declared multiple globals with different section flags, we need |
| 579 | // to reject it here. |
| 580 | if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) { |
Chris Lattner | 2104b8d | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 581 | // If invalid, report the error with report_fatal_error. |
Benjamin Kramer | 1f97a5a | 2011-11-15 16:27:03 +0000 | [diff] [blame] | 582 | report_fatal_error("Global variable '" + GV->getName() + |
| 583 | "' section type or attributes does not match previous" |
| 584 | " section specifier"); |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | return S; |
| 588 | } |
| 589 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 590 | MCSection *TargetLoweringObjectFileMachO::SelectSectionForGlobal( |
| 591 | const GlobalValue *GV, SectionKind Kind, Mangler &Mang, |
| 592 | const TargetMachine &TM) const { |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 593 | checkMachOComdat(GV); |
Bill Wendling | 25b61db | 2013-11-17 10:53:13 +0000 | [diff] [blame] | 594 | |
| 595 | // Handle thread local data. |
| 596 | if (Kind.isThreadBSS()) return TLSBSSSection; |
| 597 | if (Kind.isThreadData()) return TLSDataSection; |
| 598 | |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 599 | if (Kind.isText()) |
Arnold Schwaighofer | c31c2de | 2013-08-08 21:04:16 +0000 | [diff] [blame] | 600 | return GV->isWeakForLinker() ? TextCoalSection : TextSection; |
| 601 | |
| 602 | // If this is weak/linkonce, put this in a coalescable section, either in text |
| 603 | // or data depending on if it is writable. |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 604 | if (GV->isWeakForLinker()) { |
| 605 | if (Kind.isReadOnly()) |
Arnold Schwaighofer | c31c2de | 2013-08-08 21:04:16 +0000 | [diff] [blame] | 606 | return ConstTextCoalSection; |
| 607 | return DataCoalSection; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | // FIXME: Alignment check should be handled by section classifier. |
Chris Lattner | ef2f804 | 2010-03-07 04:28:09 +0000 | [diff] [blame] | 611 | if (Kind.isMergeable1ByteCString() && |
Mehdi Amini | 5c0fa58 | 2015-07-16 06:04:17 +0000 | [diff] [blame] | 612 | GV->getParent()->getDataLayout().getPreferredAlignment( |
| 613 | cast<GlobalVariable>(GV)) < 32) |
Chris Lattner | ef2f804 | 2010-03-07 04:28:09 +0000 | [diff] [blame] | 614 | return CStringSection; |
Michael J. Spencer | fbdab0d | 2010-10-27 18:52:20 +0000 | [diff] [blame] | 615 | |
Chris Lattner | ef2f804 | 2010-03-07 04:28:09 +0000 | [diff] [blame] | 616 | // Do not put 16-bit arrays in the UString section if they have an |
| 617 | // externally visible label, this runs into issues with certain linker |
| 618 | // versions. |
| 619 | if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() && |
Mehdi Amini | 5c0fa58 | 2015-07-16 06:04:17 +0000 | [diff] [blame] | 620 | GV->getParent()->getDataLayout().getPreferredAlignment( |
| 621 | 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 | |
Rafael Espindola | b43d51d | 2014-08-28 20:13:31 +0000 | [diff] [blame] | 624 | // With MachO only variables whose corresponding symbol starts with 'l' or |
| 625 | // 'L' can be merged, so we only try merging GVs with private linkage. |
| 626 | if (GV->hasPrivateLinkage() && Kind.isMergeableConst()) { |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 627 | if (Kind.isMergeableConst4()) |
| 628 | return FourByteConstantSection; |
| 629 | if (Kind.isMergeableConst8()) |
| 630 | return EightByteConstantSection; |
Rafael Espindola | 1f3de49 | 2014-02-13 23:16:11 +0000 | [diff] [blame] | 631 | if (Kind.isMergeableConst16()) |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 632 | return SixteenByteConstantSection; |
| 633 | } |
| 634 | |
| 635 | // Otherwise, if it is readonly, but not something we can specially optimize, |
| 636 | // just drop it in .const. |
| 637 | if (Kind.isReadOnly()) |
| 638 | return ReadOnlySection; |
| 639 | |
| 640 | // If this is marked const, put it into a const section. But if the dynamic |
| 641 | // linker needs to write to it, put it in the data segment. |
| 642 | if (Kind.isReadOnlyWithRel()) |
| 643 | return ConstDataSection; |
| 644 | |
| 645 | // Put zero initialized globals with strong external linkage in the |
| 646 | // DATA, __common section with the .zerofill directive. |
| 647 | if (Kind.isBSSExtern()) |
| 648 | return DataCommonSection; |
| 649 | |
| 650 | // Put zero initialized globals with local linkage in __DATA,__bss directive |
| 651 | // with the .zerofill directive (aka .lcomm). |
| 652 | if (Kind.isBSSLocal()) |
| 653 | return DataBSSSection; |
Michael J. Spencer | fbdab0d | 2010-10-27 18:52:20 +0000 | [diff] [blame] | 654 | |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 655 | // Otherwise, just drop the variable in the normal data section. |
| 656 | return DataSection; |
| 657 | } |
| 658 | |
Mehdi Amini | 5c0fa58 | 2015-07-16 06:04:17 +0000 | [diff] [blame] | 659 | MCSection *TargetLoweringObjectFileMachO::getSectionForConstant( |
David Majnemer | a3ea407 | 2016-02-21 01:30:30 +0000 | [diff] [blame] | 660 | const DataLayout &DL, SectionKind Kind, const Constant *C, |
| 661 | unsigned &Align) const { |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 662 | // If this constant requires a relocation, we have to put it in the data |
| 663 | // segment, not in the text segment. |
Rafael Espindola | 449711c | 2015-11-18 06:02:15 +0000 | [diff] [blame] | 664 | if (Kind.isData() || Kind.isReadOnlyWithRel()) |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 665 | return ConstDataSection; |
| 666 | |
| 667 | if (Kind.isMergeableConst4()) |
| 668 | return FourByteConstantSection; |
| 669 | if (Kind.isMergeableConst8()) |
| 670 | return EightByteConstantSection; |
Rafael Espindola | 1f3de49 | 2014-02-13 23:16:11 +0000 | [diff] [blame] | 671 | if (Kind.isMergeableConst16()) |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 672 | return SixteenByteConstantSection; |
| 673 | return ReadOnlySection; // .const |
| 674 | } |
| 675 | |
Rafael Espindola | 15b2669 | 2014-02-09 14:50:44 +0000 | [diff] [blame] | 676 | const MCExpr *TargetLoweringObjectFileMachO::getTTypeGlobalReference( |
| 677 | const GlobalValue *GV, unsigned Encoding, Mangler &Mang, |
Rafael Espindola | daeafb4 | 2014-02-19 17:23:20 +0000 | [diff] [blame] | 678 | const TargetMachine &TM, MachineModuleInfo *MMI, |
| 679 | MCStreamer &Streamer) const { |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 680 | // The mach-o version of this method defaults to returning a stub reference. |
| 681 | |
Anton Korobeynikov | 31a9212 | 2010-02-21 20:28:15 +0000 | [diff] [blame] | 682 | if (Encoding & DW_EH_PE_indirect) { |
| 683 | MachineModuleInfoMachO &MachOMMI = |
| 684 | MMI->getObjFileInfo<MachineModuleInfoMachO>(); |
| 685 | |
Rafael Espindola | daeafb4 | 2014-02-19 17:23:20 +0000 | [diff] [blame] | 686 | MCSymbol *SSym = |
| 687 | getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", Mang, TM); |
Anton Korobeynikov | 31a9212 | 2010-02-21 20:28:15 +0000 | [diff] [blame] | 688 | |
| 689 | // Add information about the stub reference to MachOMMI so that the stub |
| 690 | // gets emitted by the asmprinter. |
Bill Wendling | 57e3aaa | 2011-10-24 23:05:43 +0000 | [diff] [blame] | 691 | MachineModuleInfoImpl::StubValueTy &StubSym = |
| 692 | GV->hasHiddenVisibility() ? MachOMMI.getHiddenGVStubEntry(SSym) : |
| 693 | MachOMMI.getGVStubEntry(SSym); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 694 | if (!StubSym.getPointer()) { |
Rafael Espindola | a3ad4e6 | 2014-02-19 20:30:41 +0000 | [diff] [blame] | 695 | MCSymbol *Sym = TM.getSymbol(GV, Mang); |
Chris Lattner | 2ea586b | 2010-03-15 20:37:38 +0000 | [diff] [blame] | 696 | StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage()); |
Anton Korobeynikov | 31a9212 | 2010-02-21 20:28:15 +0000 | [diff] [blame] | 697 | } |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 698 | |
| 699 | return TargetLoweringObjectFile:: |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 700 | getTTypeReference(MCSymbolRefExpr::create(SSym, getContext()), |
Anton Korobeynikov | e42af36 | 2012-11-14 01:47:00 +0000 | [diff] [blame] | 701 | Encoding & ~dwarf::DW_EH_PE_indirect, Streamer); |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 702 | } |
| 703 | |
Rafael Espindola | daeafb4 | 2014-02-19 17:23:20 +0000 | [diff] [blame] | 704 | return TargetLoweringObjectFile::getTTypeGlobalReference(GV, Encoding, Mang, |
| 705 | TM, MMI, Streamer); |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 706 | } |
| 707 | |
Rafael Espindola | daeafb4 | 2014-02-19 17:23:20 +0000 | [diff] [blame] | 708 | MCSymbol *TargetLoweringObjectFileMachO::getCFIPersonalitySymbol( |
| 709 | const GlobalValue *GV, Mangler &Mang, const TargetMachine &TM, |
| 710 | MachineModuleInfo *MMI) const { |
Rafael Espindola | 0870434 | 2011-04-27 23:08:15 +0000 | [diff] [blame] | 711 | // The mach-o version of this method defaults to returning a stub reference. |
| 712 | MachineModuleInfoMachO &MachOMMI = |
| 713 | MMI->getObjFileInfo<MachineModuleInfoMachO>(); |
| 714 | |
Rafael Espindola | daeafb4 | 2014-02-19 17:23:20 +0000 | [diff] [blame] | 715 | MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", Mang, TM); |
Rafael Espindola | 0870434 | 2011-04-27 23:08:15 +0000 | [diff] [blame] | 716 | |
| 717 | // Add information about the stub reference to MachOMMI so that the stub |
| 718 | // gets emitted by the asmprinter. |
Bill Wendling | e4cc332 | 2011-11-29 01:43:20 +0000 | [diff] [blame] | 719 | MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 720 | if (!StubSym.getPointer()) { |
Rafael Espindola | a3ad4e6 | 2014-02-19 20:30:41 +0000 | [diff] [blame] | 721 | MCSymbol *Sym = TM.getSymbol(GV, Mang); |
Rafael Espindola | 0870434 | 2011-04-27 23:08:15 +0000 | [diff] [blame] | 722 | StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage()); |
| 723 | } |
| 724 | |
| 725 | return SSym; |
| 726 | } |
| 727 | |
Bruno Cardoso Lopes | 618c67a | 2015-03-06 13:49:05 +0000 | [diff] [blame] | 728 | const MCExpr *TargetLoweringObjectFileMachO::getIndirectSymViaGOTPCRel( |
| 729 | const MCSymbol *Sym, const MCValue &MV, int64_t Offset, |
| 730 | MachineModuleInfo *MMI, MCStreamer &Streamer) const { |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 731 | // Although MachO 32-bit targets do not explicitly have a GOTPCREL relocation |
Bruno Cardoso Lopes | 618c67a | 2015-03-06 13:49:05 +0000 | [diff] [blame] | 732 | // as 64-bit do, we replace the GOT equivalent by accessing the final symbol |
| 733 | // through a non_lazy_ptr stub instead. One advantage is that it allows the |
| 734 | // computation of deltas to final external symbols. Example: |
| 735 | // |
| 736 | // _extgotequiv: |
| 737 | // .long _extfoo |
| 738 | // |
| 739 | // _delta: |
| 740 | // .long _extgotequiv-_delta |
| 741 | // |
| 742 | // is transformed to: |
| 743 | // |
| 744 | // _delta: |
| 745 | // .long L_extfoo$non_lazy_ptr-(_delta+0) |
| 746 | // |
| 747 | // .section __IMPORT,__pointers,non_lazy_symbol_pointers |
| 748 | // L_extfoo$non_lazy_ptr: |
| 749 | // .indirect_symbol _extfoo |
| 750 | // .long 0 |
| 751 | // |
| 752 | MachineModuleInfoMachO &MachOMMI = |
| 753 | MMI->getObjFileInfo<MachineModuleInfoMachO>(); |
| 754 | MCContext &Ctx = getContext(); |
| 755 | |
| 756 | // The offset must consider the original displacement from the base symbol |
| 757 | // since 32-bit targets don't have a GOTPCREL to fold the PC displacement. |
| 758 | Offset = -MV.getConstant(); |
| 759 | const MCSymbol *BaseSym = &MV.getSymB()->getSymbol(); |
| 760 | |
| 761 | // Access the final symbol via sym$non_lazy_ptr and generate the appropriated |
| 762 | // non_lazy_ptr stubs. |
| 763 | SmallString<128> Name; |
| 764 | StringRef Suffix = "$non_lazy_ptr"; |
Mehdi Amini | 5c0fa58 | 2015-07-16 06:04:17 +0000 | [diff] [blame] | 765 | Name += MMI->getModule()->getDataLayout().getPrivateGlobalPrefix(); |
Bruno Cardoso Lopes | 618c67a | 2015-03-06 13:49:05 +0000 | [diff] [blame] | 766 | Name += Sym->getName(); |
| 767 | Name += Suffix; |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 768 | MCSymbol *Stub = Ctx.getOrCreateSymbol(Name); |
Bruno Cardoso Lopes | 618c67a | 2015-03-06 13:49:05 +0000 | [diff] [blame] | 769 | |
| 770 | MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(Stub); |
| 771 | if (!StubSym.getPointer()) |
| 772 | StubSym = MachineModuleInfoImpl:: |
| 773 | StubValueTy(const_cast<MCSymbol *>(Sym), true /* access indirectly */); |
| 774 | |
| 775 | const MCExpr *BSymExpr = |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 776 | MCSymbolRefExpr::create(BaseSym, MCSymbolRefExpr::VK_None, Ctx); |
Bruno Cardoso Lopes | 618c67a | 2015-03-06 13:49:05 +0000 | [diff] [blame] | 777 | const MCExpr *LHS = |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 778 | MCSymbolRefExpr::create(Stub, MCSymbolRefExpr::VK_None, Ctx); |
Bruno Cardoso Lopes | 618c67a | 2015-03-06 13:49:05 +0000 | [diff] [blame] | 779 | |
| 780 | if (!Offset) |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 781 | return MCBinaryExpr::createSub(LHS, BSymExpr, Ctx); |
Bruno Cardoso Lopes | 618c67a | 2015-03-06 13:49:05 +0000 | [diff] [blame] | 782 | |
| 783 | const MCExpr *RHS = |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 784 | MCBinaryExpr::createAdd(BSymExpr, MCConstantExpr::create(Offset, Ctx), Ctx); |
| 785 | return MCBinaryExpr::createSub(LHS, RHS, Ctx); |
Bruno Cardoso Lopes | 618c67a | 2015-03-06 13:49:05 +0000 | [diff] [blame] | 786 | } |
| 787 | |
Peter Collingbourne | 94d7786 | 2015-11-03 23:40:03 +0000 | [diff] [blame] | 788 | static bool canUsePrivateLabel(const MCAsmInfo &AsmInfo, |
| 789 | const MCSection &Section) { |
| 790 | if (!AsmInfo.isSectionAtomizableBySymbols(Section)) |
| 791 | return true; |
| 792 | |
| 793 | // If it is not dead stripped, it is safe to use private labels. |
| 794 | const MCSectionMachO &SMO = cast<MCSectionMachO>(Section); |
| 795 | if (SMO.hasAttribute(MachO::S_ATTR_NO_DEAD_STRIP)) |
| 796 | return true; |
| 797 | |
| 798 | return false; |
| 799 | } |
| 800 | |
| 801 | void TargetLoweringObjectFileMachO::getNameWithPrefix( |
| 802 | SmallVectorImpl<char> &OutName, const GlobalValue *GV, Mangler &Mang, |
| 803 | const TargetMachine &TM) const { |
| 804 | SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GV, TM); |
| 805 | const MCSection *TheSection = SectionForGlobal(GV, GVKind, Mang, TM); |
| 806 | bool CannotUsePrivateLabel = |
| 807 | !canUsePrivateLabel(*TM.getMCAsmInfo(), *TheSection); |
| 808 | Mang.getNameWithPrefix(OutName, GV, CannotUsePrivateLabel); |
| 809 | } |
| 810 | |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 811 | //===----------------------------------------------------------------------===// |
| 812 | // COFF |
| 813 | //===----------------------------------------------------------------------===// |
| 814 | |
Chris Lattner | 87cffa9 | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 815 | static unsigned |
| 816 | getCOFFSectionFlags(SectionKind K) { |
| 817 | unsigned Flags = 0; |
| 818 | |
Anton Korobeynikov | e415230 | 2010-07-06 15:24:56 +0000 | [diff] [blame] | 819 | if (K.isMetadata()) |
Chris Lattner | 0284493 | 2010-05-07 21:49:09 +0000 | [diff] [blame] | 820 | Flags |= |
Daniel Dunbar | 329d202 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 821 | COFF::IMAGE_SCN_MEM_DISCARDABLE; |
Chris Lattner | 87cffa9 | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 822 | else if (K.isText()) |
| 823 | Flags |= |
Daniel Dunbar | 329d202 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 824 | COFF::IMAGE_SCN_MEM_EXECUTE | |
Michael J. Spencer | 0f83d96 | 2010-10-27 18:52:29 +0000 | [diff] [blame] | 825 | COFF::IMAGE_SCN_MEM_READ | |
Daniel Dunbar | 329d202 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 826 | COFF::IMAGE_SCN_CNT_CODE; |
David Majnemer | b8dbebb | 2014-09-20 07:31:46 +0000 | [diff] [blame] | 827 | else if (K.isBSS()) |
Chris Lattner | 0284493 | 2010-05-07 21:49:09 +0000 | [diff] [blame] | 828 | Flags |= |
Daniel Dunbar | 329d202 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 829 | COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA | |
| 830 | COFF::IMAGE_SCN_MEM_READ | |
| 831 | COFF::IMAGE_SCN_MEM_WRITE; |
Anton Korobeynikov | c6b4017 | 2012-02-11 17:26:53 +0000 | [diff] [blame] | 832 | else if (K.isThreadLocal()) |
| 833 | Flags |= |
| 834 | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | |
| 835 | COFF::IMAGE_SCN_MEM_READ | |
| 836 | COFF::IMAGE_SCN_MEM_WRITE; |
David Majnemer | b8dbebb | 2014-09-20 07:31:46 +0000 | [diff] [blame] | 837 | else if (K.isReadOnly() || K.isReadOnlyWithRel()) |
Chris Lattner | 87cffa9 | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 838 | Flags |= |
Daniel Dunbar | 329d202 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 839 | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | |
| 840 | COFF::IMAGE_SCN_MEM_READ; |
Chris Lattner | 87cffa9 | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 841 | else if (K.isWriteable()) |
| 842 | Flags |= |
Daniel Dunbar | 329d202 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 843 | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | |
| 844 | COFF::IMAGE_SCN_MEM_READ | |
| 845 | COFF::IMAGE_SCN_MEM_WRITE; |
Chris Lattner | 87cffa9 | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 846 | |
| 847 | return Flags; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 848 | } |
| 849 | |
Benjamin Kramer | 6cbe670 | 2014-07-07 14:47:51 +0000 | [diff] [blame] | 850 | static const GlobalValue *getComdatGVForCOFF(const GlobalValue *GV) { |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 851 | const Comdat *C = GV->getComdat(); |
| 852 | assert(C && "expected GV to have a Comdat!"); |
| 853 | |
| 854 | StringRef ComdatGVName = C->getName(); |
| 855 | const GlobalValue *ComdatGV = GV->getParent()->getNamedValue(ComdatGVName); |
| 856 | if (!ComdatGV) |
| 857 | report_fatal_error("Associative COMDAT symbol '" + ComdatGVName + |
| 858 | "' does not exist."); |
| 859 | |
| 860 | if (ComdatGV->getComdat() != C) |
| 861 | report_fatal_error("Associative COMDAT symbol '" + ComdatGVName + |
Hans Wennborg | c0f0c51 | 2014-09-19 01:14:56 +0000 | [diff] [blame] | 862 | "' is not a key for its COMDAT."); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 863 | |
| 864 | return ComdatGV; |
| 865 | } |
| 866 | |
| 867 | static int getSelectionForCOFF(const GlobalValue *GV) { |
| 868 | if (const Comdat *C = GV->getComdat()) { |
| 869 | const GlobalValue *ComdatKey = getComdatGVForCOFF(GV); |
| 870 | if (const auto *GA = dyn_cast<GlobalAlias>(ComdatKey)) |
| 871 | ComdatKey = GA->getBaseObject(); |
| 872 | if (ComdatKey == GV) { |
| 873 | switch (C->getSelectionKind()) { |
| 874 | case Comdat::Any: |
| 875 | return COFF::IMAGE_COMDAT_SELECT_ANY; |
| 876 | case Comdat::ExactMatch: |
| 877 | return COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH; |
| 878 | case Comdat::Largest: |
| 879 | return COFF::IMAGE_COMDAT_SELECT_LARGEST; |
| 880 | case Comdat::NoDuplicates: |
| 881 | return COFF::IMAGE_COMDAT_SELECT_NODUPLICATES; |
| 882 | case Comdat::SameSize: |
| 883 | return COFF::IMAGE_COMDAT_SELECT_SAME_SIZE; |
| 884 | } |
| 885 | } else { |
| 886 | return COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE; |
| 887 | } |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 888 | } |
| 889 | return 0; |
| 890 | } |
| 891 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 892 | MCSection *TargetLoweringObjectFileCOFF::getExplicitSectionGlobal( |
Rafael Espindola | daeafb4 | 2014-02-19 17:23:20 +0000 | [diff] [blame] | 893 | const GlobalValue *GV, SectionKind Kind, Mangler &Mang, |
| 894 | const TargetMachine &TM) const { |
Michael J. Spencer | f1aef75 | 2012-11-13 22:04:09 +0000 | [diff] [blame] | 895 | int Selection = 0; |
| 896 | unsigned Characteristics = getCOFFSectionFlags(Kind); |
Rafael Espindola | 2d30ae2 | 2013-11-27 01:18:37 +0000 | [diff] [blame] | 897 | StringRef Name = GV->getSection(); |
| 898 | StringRef COMDATSymName = ""; |
Rafael Espindola | 12ca34f | 2015-01-19 15:16:06 +0000 | [diff] [blame] | 899 | if (GV->hasComdat()) { |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 900 | Selection = getSelectionForCOFF(GV); |
| 901 | const GlobalValue *ComdatGV; |
| 902 | if (Selection == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) |
| 903 | ComdatGV = getComdatGVForCOFF(GV); |
| 904 | else |
| 905 | ComdatGV = GV; |
| 906 | |
| 907 | if (!ComdatGV->hasPrivateLinkage()) { |
| 908 | MCSymbol *Sym = TM.getSymbol(ComdatGV, Mang); |
| 909 | COMDATSymName = Sym->getName(); |
| 910 | Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT; |
| 911 | } else { |
| 912 | Selection = 0; |
| 913 | } |
Michael J. Spencer | f1aef75 | 2012-11-13 22:04:09 +0000 | [diff] [blame] | 914 | } |
| 915 | return getContext().getCOFFSection(Name, |
| 916 | Characteristics, |
Nico Rieck | a37acf7 | 2013-07-06 12:13:10 +0000 | [diff] [blame] | 917 | Kind, |
Rafael Espindola | 2d30ae2 | 2013-11-27 01:18:37 +0000 | [diff] [blame] | 918 | COMDATSymName, |
Nico Rieck | a37acf7 | 2013-07-06 12:13:10 +0000 | [diff] [blame] | 919 | Selection); |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 920 | } |
| 921 | |
Rafael Espindola | 2d30ae2 | 2013-11-27 01:18:37 +0000 | [diff] [blame] | 922 | static const char *getCOFFSectionNameForUniqueGlobal(SectionKind Kind) { |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 923 | if (Kind.isText()) |
Rafael Espindola | 2d30ae2 | 2013-11-27 01:18:37 +0000 | [diff] [blame] | 924 | return ".text"; |
David Majnemer | a9bdb32 | 2014-04-08 22:33:40 +0000 | [diff] [blame] | 925 | if (Kind.isBSS()) |
Rafael Espindola | 2d30ae2 | 2013-11-27 01:18:37 +0000 | [diff] [blame] | 926 | return ".bss"; |
| 927 | if (Kind.isThreadLocal()) |
Rafael Espindola | 3c8e147 | 2013-11-27 15:52:11 +0000 | [diff] [blame] | 928 | return ".tls$"; |
David Majnemer | 597be2d | 2014-09-22 20:39:23 +0000 | [diff] [blame] | 929 | if (Kind.isReadOnly() || Kind.isReadOnlyWithRel()) |
| 930 | return ".rdata"; |
| 931 | return ".data"; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 932 | } |
| 933 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 934 | MCSection *TargetLoweringObjectFileCOFF::SelectSectionForGlobal( |
| 935 | const GlobalValue *GV, SectionKind Kind, Mangler &Mang, |
| 936 | const TargetMachine &TM) const { |
David Majnemer | 9338984 | 2014-03-23 17:47:39 +0000 | [diff] [blame] | 937 | // If we have -ffunction-sections then we should emit the global value to a |
| 938 | // uniqued section specifically for it. |
David Majnemer | 273bff4 | 2014-03-25 06:14:26 +0000 | [diff] [blame] | 939 | bool EmitUniquedSection; |
| 940 | if (Kind.isText()) |
| 941 | EmitUniquedSection = TM.getFunctionSections(); |
| 942 | else |
| 943 | EmitUniquedSection = TM.getDataSections(); |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 944 | |
Rafael Espindola | 12ca34f | 2015-01-19 15:16:06 +0000 | [diff] [blame] | 945 | if ((EmitUniquedSection && !Kind.isCommon()) || GV->hasComdat()) { |
Rafael Espindola | 2d30ae2 | 2013-11-27 01:18:37 +0000 | [diff] [blame] | 946 | const char *Name = getCOFFSectionNameForUniqueGlobal(Kind); |
Chris Lattner | 0284493 | 2010-05-07 21:49:09 +0000 | [diff] [blame] | 947 | unsigned Characteristics = getCOFFSectionFlags(Kind); |
| 948 | |
Daniel Dunbar | 329d202 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 949 | Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT; |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 950 | int Selection = getSelectionForCOFF(GV); |
| 951 | if (!Selection) |
| 952 | Selection = COFF::IMAGE_COMDAT_SELECT_NODUPLICATES; |
| 953 | const GlobalValue *ComdatGV; |
| 954 | if (GV->hasComdat()) |
| 955 | ComdatGV = getComdatGVForCOFF(GV); |
| 956 | else |
| 957 | ComdatGV = GV; |
| 958 | |
| 959 | if (!ComdatGV->hasPrivateLinkage()) { |
| 960 | MCSymbol *Sym = TM.getSymbol(ComdatGV, Mang); |
| 961 | StringRef COMDATSymName = Sym->getName(); |
| 962 | return getContext().getCOFFSection(Name, Characteristics, Kind, |
| 963 | COMDATSymName, Selection); |
David Majnemer | 7db449a | 2015-03-17 23:54:51 +0000 | [diff] [blame] | 964 | } else { |
| 965 | SmallString<256> TmpData; |
Peter Collingbourne | 94d7786 | 2015-11-03 23:40:03 +0000 | [diff] [blame] | 966 | Mang.getNameWithPrefix(TmpData, GV, /*CannotUsePrivateLabel=*/true); |
David Majnemer | 7db449a | 2015-03-17 23:54:51 +0000 | [diff] [blame] | 967 | return getContext().getCOFFSection(Name, Characteristics, Kind, TmpData, |
| 968 | Selection); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 969 | } |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 970 | } |
| 971 | |
| 972 | if (Kind.isText()) |
David Majnemer | 3d96acb | 2013-08-13 01:23:53 +0000 | [diff] [blame] | 973 | return TextSection; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 974 | |
Anton Korobeynikov | c6b4017 | 2012-02-11 17:26:53 +0000 | [diff] [blame] | 975 | if (Kind.isThreadLocal()) |
David Majnemer | 3d96acb | 2013-08-13 01:23:53 +0000 | [diff] [blame] | 976 | return TLSDataSection; |
Anton Korobeynikov | c6b4017 | 2012-02-11 17:26:53 +0000 | [diff] [blame] | 977 | |
David Majnemer | 597be2d | 2014-09-22 20:39:23 +0000 | [diff] [blame] | 978 | if (Kind.isReadOnly() || Kind.isReadOnlyWithRel()) |
David Majnemer | f76d6b3 | 2013-08-08 01:50:52 +0000 | [diff] [blame] | 979 | return ReadOnlySection; |
| 980 | |
David Majnemer | a9bdb32 | 2014-04-08 22:33:40 +0000 | [diff] [blame] | 981 | // Note: we claim that common symbols are put in BSSSection, but they are |
| 982 | // really emitted with the magic .comm directive, which creates a symbol table |
| 983 | // entry but not a section. |
| 984 | if (Kind.isBSS() || Kind.isCommon()) |
David Majnemer | 3d96acb | 2013-08-13 01:23:53 +0000 | [diff] [blame] | 985 | return BSSSection; |
| 986 | |
| 987 | return DataSection; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 988 | } |
| 989 | |
David Majnemer | 7db449a | 2015-03-17 23:54:51 +0000 | [diff] [blame] | 990 | void TargetLoweringObjectFileCOFF::getNameWithPrefix( |
Peter Collingbourne | 94d7786 | 2015-11-03 23:40:03 +0000 | [diff] [blame] | 991 | SmallVectorImpl<char> &OutName, const GlobalValue *GV, Mangler &Mang, |
| 992 | const TargetMachine &TM) const { |
| 993 | bool CannotUsePrivateLabel = false; |
David Majnemer | 7db449a | 2015-03-17 23:54:51 +0000 | [diff] [blame] | 994 | if (GV->hasPrivateLinkage() && |
| 995 | ((isa<Function>(GV) && TM.getFunctionSections()) || |
| 996 | (isa<GlobalVariable>(GV) && TM.getDataSections()))) |
| 997 | CannotUsePrivateLabel = true; |
| 998 | |
| 999 | Mang.getNameWithPrefix(OutName, GV, CannotUsePrivateLabel); |
| 1000 | } |
| 1001 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 1002 | MCSection *TargetLoweringObjectFileCOFF::getSectionForJumpTable( |
Rafael Espindola | ab447e4 | 2015-03-11 19:58:37 +0000 | [diff] [blame] | 1003 | const Function &F, Mangler &Mang, const TargetMachine &TM) const { |
| 1004 | // If the function can be removed, produce a unique section so that |
| 1005 | // the table doesn't prevent the removal. |
| 1006 | const Comdat *C = F.getComdat(); |
| 1007 | bool EmitUniqueSection = TM.getFunctionSections() || C; |
| 1008 | if (!EmitUniqueSection) |
| 1009 | return ReadOnlySection; |
| 1010 | |
| 1011 | // FIXME: we should produce a symbol for F instead. |
| 1012 | if (F.hasPrivateLinkage()) |
| 1013 | return ReadOnlySection; |
| 1014 | |
| 1015 | MCSymbol *Sym = TM.getSymbol(&F, Mang); |
| 1016 | StringRef COMDATSymName = Sym->getName(); |
| 1017 | |
| 1018 | SectionKind Kind = SectionKind::getReadOnly(); |
| 1019 | const char *Name = getCOFFSectionNameForUniqueGlobal(Kind); |
| 1020 | unsigned Characteristics = getCOFFSectionFlags(Kind); |
| 1021 | Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT; |
| 1022 | |
| 1023 | return getContext().getCOFFSection(Name, Characteristics, Kind, COMDATSymName, |
| 1024 | COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE); |
| 1025 | } |
| 1026 | |
Reid Kleckner | d973ca3 | 2013-04-25 19:34:41 +0000 | [diff] [blame] | 1027 | void TargetLoweringObjectFileCOFF:: |
| 1028 | emitModuleFlags(MCStreamer &Streamer, |
| 1029 | ArrayRef<Module::ModuleFlagEntry> ModuleFlags, |
Rafael Espindola | fa0f728 | 2014-02-08 14:53:28 +0000 | [diff] [blame] | 1030 | Mangler &Mang, const TargetMachine &TM) const { |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1031 | MDNode *LinkerOptions = nullptr; |
Reid Kleckner | d973ca3 | 2013-04-25 19:34:41 +0000 | [diff] [blame] | 1032 | |
| 1033 | // Look for the "Linker Options" flag, since it's the only one we support. |
| 1034 | for (ArrayRef<Module::ModuleFlagEntry>::iterator |
| 1035 | i = ModuleFlags.begin(), e = ModuleFlags.end(); i != e; ++i) { |
| 1036 | const Module::ModuleFlagEntry &MFE = *i; |
| 1037 | StringRef Key = MFE.Key->getString(); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1038 | Metadata *Val = MFE.Val; |
Reid Kleckner | d973ca3 | 2013-04-25 19:34:41 +0000 | [diff] [blame] | 1039 | if (Key == "Linker Options") { |
| 1040 | LinkerOptions = cast<MDNode>(Val); |
| 1041 | break; |
| 1042 | } |
| 1043 | } |
| 1044 | if (!LinkerOptions) |
| 1045 | return; |
| 1046 | |
| 1047 | // Emit the linker options to the linker .drectve section. According to the |
| 1048 | // spec, this section is a space-separated string containing flags for linker. |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 1049 | MCSection *Sec = getDrectveSection(); |
Reid Kleckner | d973ca3 | 2013-04-25 19:34:41 +0000 | [diff] [blame] | 1050 | Streamer.SwitchSection(Sec); |
| 1051 | for (unsigned i = 0, e = LinkerOptions->getNumOperands(); i != e; ++i) { |
| 1052 | MDNode *MDOptions = cast<MDNode>(LinkerOptions->getOperand(i)); |
| 1053 | for (unsigned ii = 0, ie = MDOptions->getNumOperands(); ii != ie; ++ii) { |
| 1054 | MDString *MDOption = cast<MDString>(MDOptions->getOperand(ii)); |
Reid Kleckner | d973ca3 | 2013-04-25 19:34:41 +0000 | [diff] [blame] | 1055 | // Lead with a space for consistency with our dllexport implementation. |
Michael Kuperstein | fc3e626 | 2015-02-16 11:57:17 +0000 | [diff] [blame] | 1056 | std::string Directive(" "); |
| 1057 | Directive.append(MDOption->getString()); |
| 1058 | Streamer.EmitBytes(Directive); |
Reid Kleckner | d973ca3 | 2013-04-25 19:34:41 +0000 | [diff] [blame] | 1059 | } |
| 1060 | } |
| 1061 | } |
Reid Kleckner | fceb76f | 2014-05-16 20:39:27 +0000 | [diff] [blame] | 1062 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 1063 | MCSection *TargetLoweringObjectFileCOFF::getStaticCtorSection( |
Rafael Espindola | 0766ae0 | 2014-06-06 19:26:12 +0000 | [diff] [blame] | 1064 | unsigned Priority, const MCSymbol *KeySym) const { |
Reid Kleckner | 7c4059e | 2014-09-04 17:42:03 +0000 | [diff] [blame] | 1065 | return getContext().getAssociativeCOFFSection( |
| 1066 | cast<MCSectionCOFF>(StaticCtorSection), KeySym); |
Reid Kleckner | fceb76f | 2014-05-16 20:39:27 +0000 | [diff] [blame] | 1067 | } |
| 1068 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 1069 | MCSection *TargetLoweringObjectFileCOFF::getStaticDtorSection( |
Rafael Espindola | 0766ae0 | 2014-06-06 19:26:12 +0000 | [diff] [blame] | 1070 | unsigned Priority, const MCSymbol *KeySym) const { |
Reid Kleckner | 7c4059e | 2014-09-04 17:42:03 +0000 | [diff] [blame] | 1071 | return getContext().getAssociativeCOFFSection( |
| 1072 | cast<MCSectionCOFF>(StaticDtorSection), KeySym); |
Reid Kleckner | fceb76f | 2014-05-16 20:39:27 +0000 | [diff] [blame] | 1073 | } |
Peter Collingbourne | aef3659 | 2015-06-29 22:04:09 +0000 | [diff] [blame] | 1074 | |
| 1075 | void TargetLoweringObjectFileCOFF::emitLinkerFlagsForGlobal( |
| 1076 | raw_ostream &OS, const GlobalValue *GV, const Mangler &Mang) const { |
| 1077 | if (!GV->hasDLLExportStorageClass() || GV->isDeclaration()) |
| 1078 | return; |
| 1079 | |
| 1080 | const Triple &TT = getTargetTriple(); |
| 1081 | |
| 1082 | if (TT.isKnownWindowsMSVCEnvironment()) |
| 1083 | OS << " /EXPORT:"; |
| 1084 | else |
| 1085 | OS << " -export:"; |
| 1086 | |
| 1087 | if (TT.isWindowsGNUEnvironment() || TT.isWindowsCygwinEnvironment()) { |
| 1088 | std::string Flag; |
| 1089 | raw_string_ostream FlagOS(Flag); |
| 1090 | Mang.getNameWithPrefix(FlagOS, GV, false); |
| 1091 | FlagOS.flush(); |
Mehdi Amini | 5c0fa58 | 2015-07-16 06:04:17 +0000 | [diff] [blame] | 1092 | if (Flag[0] == GV->getParent()->getDataLayout().getGlobalPrefix()) |
Peter Collingbourne | aef3659 | 2015-06-29 22:04:09 +0000 | [diff] [blame] | 1093 | OS << Flag.substr(1); |
| 1094 | else |
| 1095 | OS << Flag; |
| 1096 | } else { |
| 1097 | Mang.getNameWithPrefix(OS, GV, false); |
| 1098 | } |
| 1099 | |
| 1100 | if (!GV->getValueType()->isFunctionTy()) { |
| 1101 | if (TT.isKnownWindowsMSVCEnvironment()) |
| 1102 | OS << ",DATA"; |
| 1103 | else |
| 1104 | OS << ",data"; |
| 1105 | } |
| 1106 | } |