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