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