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