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