Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 1 | //===- llvm/CodeGen/TargetLoweringObjectFileImpl.cpp - Object File Info ---===// |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 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 | |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/SmallString.h" |
Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallVector.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/StringExtras.h" |
Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/StringRef.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/Triple.h" |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 21 | #include "llvm/BinaryFormat/COFF.h" |
| 22 | #include "llvm/BinaryFormat/Dwarf.h" |
| 23 | #include "llvm/BinaryFormat/ELF.h" |
| 24 | #include "llvm/BinaryFormat/MachO.h" |
Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineModuleInfoImpls.h" |
Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 27 | #include "llvm/IR/Comdat.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 28 | #include "llvm/IR/Constants.h" |
| 29 | #include "llvm/IR/DataLayout.h" |
| 30 | #include "llvm/IR/DerivedTypes.h" |
| 31 | #include "llvm/IR/Function.h" |
Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 32 | #include "llvm/IR/GlobalAlias.h" |
| 33 | #include "llvm/IR/GlobalObject.h" |
| 34 | #include "llvm/IR/GlobalValue.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 35 | #include "llvm/IR/GlobalVariable.h" |
Rafael Espindola | 894843c | 2014-01-07 21:19:40 +0000 | [diff] [blame] | 36 | #include "llvm/IR/Mangler.h" |
Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 37 | #include "llvm/IR/Metadata.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 38 | #include "llvm/IR/Module.h" |
Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 39 | #include "llvm/IR/Type.h" |
Peter Collingbourne | 94d7786 | 2015-11-03 23:40:03 +0000 | [diff] [blame] | 40 | #include "llvm/MC/MCAsmInfo.h" |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 41 | #include "llvm/MC/MCContext.h" |
| 42 | #include "llvm/MC/MCExpr.h" |
Chris Lattner | 87cffa9 | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 43 | #include "llvm/MC/MCSectionCOFF.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 44 | #include "llvm/MC/MCSectionELF.h" |
| 45 | #include "llvm/MC/MCSectionMachO.h" |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 46 | #include "llvm/MC/MCSectionWasm.h" |
Rafael Espindola | a83b177 | 2011-04-16 03:51:21 +0000 | [diff] [blame] | 47 | #include "llvm/MC/MCStreamer.h" |
Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 48 | #include "llvm/MC/MCSymbol.h" |
Rafael Espindola | a869576 | 2015-06-02 00:25:12 +0000 | [diff] [blame] | 49 | #include "llvm/MC/MCSymbolELF.h" |
Bruno Cardoso Lopes | 618c67a | 2015-03-06 13:49:05 +0000 | [diff] [blame] | 50 | #include "llvm/MC/MCValue.h" |
Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 51 | #include "llvm/MC/SectionKind.h" |
Xinliang David Li | 5f04f92 | 2016-01-14 18:09:45 +0000 | [diff] [blame] | 52 | #include "llvm/ProfileData/InstrProf.h" |
Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 53 | #include "llvm/Support/Casting.h" |
| 54 | #include "llvm/Support/CodeGen.h" |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 55 | #include "llvm/Support/ErrorHandling.h" |
| 56 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | 442f784 | 2014-03-04 10:07:28 +0000 | [diff] [blame] | 57 | #include "llvm/Target/TargetMachine.h" |
Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 58 | #include <cassert> |
| 59 | #include <string> |
| 60 | |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 61 | using namespace llvm; |
Anton Korobeynikov | 31a9212 | 2010-02-21 20:28:15 +0000 | [diff] [blame] | 62 | using namespace dwarf; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 63 | |
Peter Collingbourne | 89061b2 | 2017-06-12 20:10:48 +0000 | [diff] [blame^] | 64 | static void GetObjCImageInfo(Module &M, unsigned &Version, unsigned &Flags, |
Saleem Abdulrasool | 4c47434 | 2017-06-05 21:26:39 +0000 | [diff] [blame] | 65 | StringRef &Section) { |
Peter Collingbourne | 89061b2 | 2017-06-12 20:10:48 +0000 | [diff] [blame^] | 66 | SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags; |
| 67 | M.getModuleFlagsMetadata(ModuleFlags); |
| 68 | |
Saleem Abdulrasool | 4c47434 | 2017-06-05 21:26:39 +0000 | [diff] [blame] | 69 | for (const auto &MFE: ModuleFlags) { |
| 70 | // Ignore flags with 'Require' behaviour. |
| 71 | if (MFE.Behavior == Module::Require) |
| 72 | continue; |
| 73 | |
| 74 | StringRef Key = MFE.Key->getString(); |
| 75 | if (Key == "Objective-C Image Info Version") { |
| 76 | Version = mdconst::extract<ConstantInt>(MFE.Val)->getZExtValue(); |
| 77 | } else if (Key == "Objective-C Garbage Collection" || |
| 78 | Key == "Objective-C GC Only" || |
| 79 | Key == "Objective-C Is Simulated" || |
| 80 | Key == "Objective-C Class Properties" || |
| 81 | Key == "Objective-C Image Swift Version") { |
| 82 | Flags |= mdconst::extract<ConstantInt>(MFE.Val)->getZExtValue(); |
| 83 | } else if (Key == "Objective-C Image Info Section") { |
| 84 | Section = cast<MDString>(MFE.Val)->getString(); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 89 | //===----------------------------------------------------------------------===// |
| 90 | // ELF |
| 91 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 92 | |
Peter Collingbourne | 89061b2 | 2017-06-12 20:10:48 +0000 | [diff] [blame^] | 93 | void TargetLoweringObjectFileELF::emitModuleMetadata( |
| 94 | MCStreamer &Streamer, Module &M, const TargetMachine &TM) const { |
Saleem Abdulrasool | 4c47434 | 2017-06-05 21:26:39 +0000 | [diff] [blame] | 95 | unsigned Version = 0; |
| 96 | unsigned Flags = 0; |
| 97 | StringRef Section; |
| 98 | |
Peter Collingbourne | 89061b2 | 2017-06-12 20:10:48 +0000 | [diff] [blame^] | 99 | GetObjCImageInfo(M, Version, Flags, Section); |
Saleem Abdulrasool | 4c47434 | 2017-06-05 21:26:39 +0000 | [diff] [blame] | 100 | if (Section.empty()) |
| 101 | return; |
| 102 | |
| 103 | auto &C = getContext(); |
| 104 | auto *S = C.getELFSection(Section, ELF::SHT_PROGBITS, ELF::SHF_ALLOC); |
| 105 | Streamer.SwitchSection(S); |
| 106 | Streamer.EmitLabel(C.getOrCreateSymbol(StringRef("OBJC_IMAGE_INFO"))); |
| 107 | Streamer.EmitIntValue(Version, 4); |
| 108 | Streamer.EmitIntValue(Flags, 4); |
| 109 | Streamer.AddBlankLine(); |
| 110 | } |
| 111 | |
Rafael Espindola | daeafb4 | 2014-02-19 17:23:20 +0000 | [diff] [blame] | 112 | MCSymbol *TargetLoweringObjectFileELF::getCFIPersonalitySymbol( |
Eric Christopher | 4367c7f | 2016-09-16 07:33:15 +0000 | [diff] [blame] | 113 | const GlobalValue *GV, const TargetMachine &TM, |
Rafael Espindola | daeafb4 | 2014-02-19 17:23:20 +0000 | [diff] [blame] | 114 | MachineModuleInfo *MMI) const { |
Rafael Espindola | ce83fc3 | 2011-04-27 23:17:57 +0000 | [diff] [blame] | 115 | unsigned Encoding = getPersonalityEncoding(); |
Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 116 | if ((Encoding & 0x80) == DW_EH_PE_indirect) |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 117 | return getContext().getOrCreateSymbol(StringRef("DW.ref.") + |
Tim Northover | b64fb45 | 2016-11-22 16:17:20 +0000 | [diff] [blame] | 118 | TM.getSymbol(GV)->getName()); |
Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 119 | if ((Encoding & 0x70) == DW_EH_PE_absptr) |
Tim Northover | b64fb45 | 2016-11-22 16:17:20 +0000 | [diff] [blame] | 120 | return TM.getSymbol(GV); |
Logan Chien | c002981 | 2014-05-30 16:48:56 +0000 | [diff] [blame] | 121 | report_fatal_error("We do not support this DWARF encoding yet!"); |
Rafael Espindola | a83b177 | 2011-04-16 03:51:21 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Mehdi Amini | 5c0fa58 | 2015-07-16 06:04:17 +0000 | [diff] [blame] | 124 | void TargetLoweringObjectFileELF::emitPersonalityValue( |
| 125 | MCStreamer &Streamer, const DataLayout &DL, const MCSymbol *Sym) const { |
Rafael Espindola | 51d2d7a | 2011-06-13 03:09:13 +0000 | [diff] [blame] | 126 | SmallString<64> NameData("DW.ref."); |
| 127 | NameData += Sym->getName(); |
Rafael Espindola | a869576 | 2015-06-02 00:25:12 +0000 | [diff] [blame] | 128 | MCSymbolELF *Label = |
| 129 | cast<MCSymbolELF>(getContext().getOrCreateSymbol(NameData)); |
Rafael Espindola | 3989776 | 2011-04-27 21:29:52 +0000 | [diff] [blame] | 130 | Streamer.EmitSymbolAttribute(Label, MCSA_Hidden); |
| 131 | Streamer.EmitSymbolAttribute(Label, MCSA_Weak); |
Rafael Espindola | 3989776 | 2011-04-27 21:29:52 +0000 | [diff] [blame] | 132 | unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE | ELF::SHF_GROUP; |
Eric Christopher | 36e601c | 2016-07-01 06:07:38 +0000 | [diff] [blame] | 133 | MCSection *Sec = getContext().getELFNamedSection(".data", Label->getName(), |
| 134 | ELF::SHT_PROGBITS, Flags, 0); |
Mehdi Amini | 5c0fa58 | 2015-07-16 06:04:17 +0000 | [diff] [blame] | 135 | unsigned Size = DL.getPointerSize(); |
Rafael Espindola | 3989776 | 2011-04-27 21:29:52 +0000 | [diff] [blame] | 136 | Streamer.SwitchSection(Sec); |
Mehdi Amini | 5c0fa58 | 2015-07-16 06:04:17 +0000 | [diff] [blame] | 137 | Streamer.EmitValueToAlignment(DL.getPointerABIAlignment()); |
Rafael Espindola | 3989776 | 2011-04-27 21:29:52 +0000 | [diff] [blame] | 138 | Streamer.EmitSymbolAttribute(Label, MCSA_ELF_TypeObject); |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 139 | const MCExpr *E = MCConstantExpr::create(Size, getContext()); |
Rafael Espindola | a869576 | 2015-06-02 00:25:12 +0000 | [diff] [blame] | 140 | Streamer.emitELFSize(Label, E); |
Rafael Espindola | 3989776 | 2011-04-27 21:29:52 +0000 | [diff] [blame] | 141 | Streamer.EmitLabel(Label); |
Rafael Espindola | a83b177 | 2011-04-16 03:51:21 +0000 | [diff] [blame] | 142 | |
Rafael Espindola | 3989776 | 2011-04-27 21:29:52 +0000 | [diff] [blame] | 143 | Streamer.EmitSymbolValue(Sym, Size); |
Rafael Espindola | a83b177 | 2011-04-16 03:51:21 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Rafael Espindola | 15b2669 | 2014-02-09 14:50:44 +0000 | [diff] [blame] | 146 | const MCExpr *TargetLoweringObjectFileELF::getTTypeGlobalReference( |
Eric Christopher | 4367c7f | 2016-09-16 07:33:15 +0000 | [diff] [blame] | 147 | const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM, |
| 148 | MachineModuleInfo *MMI, MCStreamer &Streamer) const { |
Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 149 | if (Encoding & DW_EH_PE_indirect) { |
Anton Korobeynikov | e42af36 | 2012-11-14 01:47:00 +0000 | [diff] [blame] | 150 | MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>(); |
| 151 | |
Eric Christopher | 4367c7f | 2016-09-16 07:33:15 +0000 | [diff] [blame] | 152 | MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, ".DW.stub", TM); |
Anton Korobeynikov | e42af36 | 2012-11-14 01:47:00 +0000 | [diff] [blame] | 153 | |
| 154 | // Add information about the stub reference to ELFMMI so that the stub |
| 155 | // gets emitted by the asmprinter. |
Anton Korobeynikov | e42af36 | 2012-11-14 01:47:00 +0000 | [diff] [blame] | 156 | MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 157 | if (!StubSym.getPointer()) { |
Tim Northover | b64fb45 | 2016-11-22 16:17:20 +0000 | [diff] [blame] | 158 | MCSymbol *Sym = TM.getSymbol(GV); |
Anton Korobeynikov | e42af36 | 2012-11-14 01:47:00 +0000 | [diff] [blame] | 159 | StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage()); |
| 160 | } |
| 161 | |
| 162 | return TargetLoweringObjectFile:: |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 163 | getTTypeReference(MCSymbolRefExpr::create(SSym, getContext()), |
Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 164 | Encoding & ~DW_EH_PE_indirect, Streamer); |
Anton Korobeynikov | e42af36 | 2012-11-14 01:47:00 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Eric Christopher | 4367c7f | 2016-09-16 07:33:15 +0000 | [diff] [blame] | 167 | return TargetLoweringObjectFile::getTTypeGlobalReference(GV, Encoding, TM, |
| 168 | MMI, Streamer); |
Anton Korobeynikov | e42af36 | 2012-11-14 01:47:00 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 171 | static SectionKind |
| 172 | getELFKindForNamedSection(StringRef Name, SectionKind K) { |
Rafael Espindola | 0d018b1 | 2011-05-24 03:10:31 +0000 | [diff] [blame] | 173 | // N.B.: The defaults used in here are no the same ones used in MC. |
| 174 | // We follow gcc, MC follows gas. For example, given ".section .eh_frame", |
| 175 | // both gas and MC will produce a section with no flags. Given |
Bill Wendling | d163405 | 2012-07-19 00:04:14 +0000 | [diff] [blame] | 176 | // section(".eh_frame") gcc will produce: |
| 177 | // |
| 178 | // .section .eh_frame,"a",@progbits |
Xinliang David Li | 4a5ddf8 | 2017-04-14 17:48:40 +0000 | [diff] [blame] | 179 | |
Vedant Kumar | 1a6a2b6 | 2017-04-15 00:09:57 +0000 | [diff] [blame] | 180 | if (Name == getInstrProfSectionName(IPSK_covmap, Triple::ELF, |
| 181 | /*AddSegmentInfo=*/false)) |
Xinliang David Li | 5f04f92 | 2016-01-14 18:09:45 +0000 | [diff] [blame] | 182 | return SectionKind::getMetadata(); |
| 183 | |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 184 | if (Name.empty() || Name[0] != '.') return K; |
| 185 | |
| 186 | // Some lame default implementation based on some magic section names. |
| 187 | if (Name == ".bss" || |
| 188 | Name.startswith(".bss.") || |
| 189 | Name.startswith(".gnu.linkonce.b.") || |
| 190 | Name.startswith(".llvm.linkonce.b.") || |
| 191 | Name == ".sbss" || |
| 192 | Name.startswith(".sbss.") || |
| 193 | Name.startswith(".gnu.linkonce.sb.") || |
| 194 | Name.startswith(".llvm.linkonce.sb.")) |
| 195 | return SectionKind::getBSS(); |
| 196 | |
| 197 | if (Name == ".tdata" || |
| 198 | Name.startswith(".tdata.") || |
| 199 | Name.startswith(".gnu.linkonce.td.") || |
| 200 | Name.startswith(".llvm.linkonce.td.")) |
| 201 | return SectionKind::getThreadData(); |
| 202 | |
| 203 | if (Name == ".tbss" || |
| 204 | Name.startswith(".tbss.") || |
| 205 | Name.startswith(".gnu.linkonce.tb.") || |
| 206 | Name.startswith(".llvm.linkonce.tb.")) |
| 207 | return SectionKind::getThreadBSS(); |
| 208 | |
| 209 | return K; |
| 210 | } |
| 211 | |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 212 | static unsigned getELFSectionType(StringRef Name, SectionKind K) { |
Petr Hosek | 1290355 | 2016-09-20 20:21:13 +0000 | [diff] [blame] | 213 | // Use SHT_NOTE for section whose name starts with ".note" to allow |
| 214 | // emitting ELF notes from C variable declaration. |
| 215 | // See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77609 |
| 216 | if (Name.startswith(".note")) |
| 217 | return ELF::SHT_NOTE; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 218 | |
| 219 | if (Name == ".init_array") |
Rafael Espindola | aea4958 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 220 | return ELF::SHT_INIT_ARRAY; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 221 | |
| 222 | if (Name == ".fini_array") |
Rafael Espindola | aea4958 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 223 | return ELF::SHT_FINI_ARRAY; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 224 | |
| 225 | if (Name == ".preinit_array") |
Rafael Espindola | aea4958 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 226 | return ELF::SHT_PREINIT_ARRAY; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 227 | |
| 228 | if (K.isBSS() || K.isThreadBSS()) |
Rafael Espindola | aea4958 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 229 | return ELF::SHT_NOBITS; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 230 | |
Rafael Espindola | aea4958 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 231 | return ELF::SHT_PROGBITS; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Rafael Espindola | 8bc9ccc | 2015-02-25 00:52:15 +0000 | [diff] [blame] | 234 | static unsigned getELFSectionFlags(SectionKind K) { |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 235 | unsigned Flags = 0; |
| 236 | |
| 237 | if (!K.isMetadata()) |
Rafael Espindola | 0e7e34e | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 238 | Flags |= ELF::SHF_ALLOC; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 239 | |
| 240 | if (K.isText()) |
Rafael Espindola | 0e7e34e | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 241 | Flags |= ELF::SHF_EXECINSTR; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 242 | |
Prakhar Bahuguna | 52a7dd7 | 2016-12-15 07:59:08 +0000 | [diff] [blame] | 243 | if (K.isExecuteOnly()) |
| 244 | Flags |= ELF::SHF_ARM_PURECODE; |
| 245 | |
Rafael Espindola | c85e0d8 | 2011-06-07 23:26:45 +0000 | [diff] [blame] | 246 | if (K.isWriteable()) |
Rafael Espindola | 0e7e34e | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 247 | Flags |= ELF::SHF_WRITE; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 248 | |
| 249 | if (K.isThreadLocal()) |
Rafael Espindola | 0e7e34e | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 250 | Flags |= ELF::SHF_TLS; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 251 | |
Rafael Espindola | 8bc9ccc | 2015-02-25 00:52:15 +0000 | [diff] [blame] | 252 | if (K.isMergeableCString() || K.isMergeableConst()) |
Rafael Espindola | 0e7e34e | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 253 | Flags |= ELF::SHF_MERGE; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 254 | |
| 255 | if (K.isMergeableCString()) |
Rafael Espindola | 0e7e34e | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 256 | Flags |= ELF::SHF_STRINGS; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 257 | |
| 258 | return Flags; |
| 259 | } |
| 260 | |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 261 | static const Comdat *getELFComdat(const GlobalValue *GV) { |
| 262 | const Comdat *C = GV->getComdat(); |
| 263 | if (!C) |
| 264 | return nullptr; |
| 265 | |
| 266 | if (C->getSelectionKind() != Comdat::Any) |
| 267 | report_fatal_error("ELF COMDATs only support SelectionKind::Any, '" + |
| 268 | C->getName() + "' cannot be lowered."); |
| 269 | |
| 270 | return C; |
| 271 | } |
| 272 | |
Evgeniy Stepanov | 51c962f72 | 2017-03-17 22:17:24 +0000 | [diff] [blame] | 273 | static const MCSymbolELF *getAssociatedSymbol(const GlobalObject *GO, |
| 274 | const TargetMachine &TM) { |
| 275 | MDNode *MD = GO->getMetadata(LLVMContext::MD_associated); |
| 276 | if (!MD) |
| 277 | return nullptr; |
| 278 | |
Evgeniy Stepanov | f7e8acf | 2017-05-08 23:46:20 +0000 | [diff] [blame] | 279 | const MDOperand &Op = MD->getOperand(0); |
| 280 | if (!Op.get()) |
| 281 | return nullptr; |
| 282 | |
| 283 | auto *VM = dyn_cast<ValueAsMetadata>(Op); |
Evgeniy Stepanov | 51c962f72 | 2017-03-17 22:17:24 +0000 | [diff] [blame] | 284 | if (!VM) |
| 285 | report_fatal_error("MD_associated operand is not ValueAsMetadata"); |
| 286 | |
| 287 | GlobalObject *OtherGO = dyn_cast<GlobalObject>(VM->getValue()); |
| 288 | return OtherGO ? dyn_cast<MCSymbolELF>(TM.getSymbol(OtherGO)) : nullptr; |
| 289 | } |
| 290 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 291 | MCSection *TargetLoweringObjectFileELF::getExplicitSectionGlobal( |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 292 | const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { |
| 293 | StringRef SectionName = GO->getSection(); |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 294 | |
Javed Absar | b16d146 | 2017-06-05 10:09:13 +0000 | [diff] [blame] | 295 | // Check if '#pragma clang section' name is applicable. |
| 296 | // Note that pragma directive overrides -ffunction-section, -fdata-section |
| 297 | // and so section name is exactly as user specified and not uniqued. |
| 298 | const GlobalVariable *GV = dyn_cast<GlobalVariable>(GO); |
| 299 | if (GV && GV->hasImplicitSection()) { |
| 300 | auto Attrs = GV->getAttributes(); |
| 301 | if (Attrs.hasAttribute("bss-section") && Kind.isBSS()) { |
| 302 | SectionName = Attrs.getAttribute("bss-section").getValueAsString(); |
| 303 | } else if (Attrs.hasAttribute("rodata-section") && Kind.isReadOnly()) { |
| 304 | SectionName = Attrs.getAttribute("rodata-section").getValueAsString(); |
| 305 | } else if (Attrs.hasAttribute("data-section") && Kind.isData()) { |
| 306 | SectionName = Attrs.getAttribute("data-section").getValueAsString(); |
| 307 | } |
| 308 | } |
| 309 | const Function *F = dyn_cast<Function>(GO); |
| 310 | if (F && F->hasFnAttribute("implicit-section-name")) { |
| 311 | SectionName = F->getFnAttribute("implicit-section-name").getValueAsString(); |
| 312 | } |
| 313 | |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 314 | // Infer section flags from the section name if we can. |
| 315 | Kind = getELFKindForNamedSection(SectionName, Kind); |
| 316 | |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 317 | StringRef Group = ""; |
Rafael Espindola | 8bc9ccc | 2015-02-25 00:52:15 +0000 | [diff] [blame] | 318 | unsigned Flags = getELFSectionFlags(Kind); |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 319 | if (const Comdat *C = getELFComdat(GO)) { |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 320 | Group = C->getName(); |
| 321 | Flags |= ELF::SHF_GROUP; |
| 322 | } |
Evgeniy Stepanov | 51c962f72 | 2017-03-17 22:17:24 +0000 | [diff] [blame] | 323 | |
| 324 | // A section can have at most one associated section. Put each global with |
| 325 | // MD_associated in a unique section. |
| 326 | unsigned UniqueID = MCContext::GenericSectionID; |
| 327 | const MCSymbolELF *AssociatedSymbol = getAssociatedSymbol(GO, TM); |
| 328 | if (AssociatedSymbol) { |
| 329 | UniqueID = NextUniqueID++; |
| 330 | Flags |= ELF::SHF_LINK_ORDER; |
| 331 | } |
| 332 | |
| 333 | MCSectionELF *Section = getContext().getELFSection( |
| 334 | SectionName, getELFSectionType(SectionName, Kind), Flags, |
| 335 | /*EntrySize=*/0, Group, UniqueID, AssociatedSymbol); |
| 336 | // Make sure that we did not get some other section with incompatible sh_link. |
| 337 | // This should not be possible due to UniqueID code above. |
| 338 | assert(Section->getAssociatedSymbol() == AssociatedSymbol); |
| 339 | return Section; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Rafael Espindola | 25d2c20 | 2015-02-11 14:44:17 +0000 | [diff] [blame] | 342 | /// Return the section prefix name used by options FunctionsSections and |
| 343 | /// DataSections. |
David Majnemer | 102ff69 | 2014-06-24 16:01:53 +0000 | [diff] [blame] | 344 | static StringRef getSectionPrefixForGlobal(SectionKind Kind) { |
Rafael Espindola | 25d2c20 | 2015-02-11 14:44:17 +0000 | [diff] [blame] | 345 | if (Kind.isText()) |
Rafael Espindola | 68fa249 | 2015-02-17 20:48:01 +0000 | [diff] [blame] | 346 | return ".text"; |
Rafael Espindola | 25d2c20 | 2015-02-11 14:44:17 +0000 | [diff] [blame] | 347 | if (Kind.isReadOnly()) |
Rafael Espindola | 68fa249 | 2015-02-17 20:48:01 +0000 | [diff] [blame] | 348 | return ".rodata"; |
Rafael Espindola | 25d2c20 | 2015-02-11 14:44:17 +0000 | [diff] [blame] | 349 | if (Kind.isBSS()) |
Rafael Espindola | 68fa249 | 2015-02-17 20:48:01 +0000 | [diff] [blame] | 350 | return ".bss"; |
Rafael Espindola | 25d2c20 | 2015-02-11 14:44:17 +0000 | [diff] [blame] | 351 | if (Kind.isThreadData()) |
Rafael Espindola | 68fa249 | 2015-02-17 20:48:01 +0000 | [diff] [blame] | 352 | return ".tdata"; |
Rafael Espindola | 25d2c20 | 2015-02-11 14:44:17 +0000 | [diff] [blame] | 353 | if (Kind.isThreadBSS()) |
Rafael Espindola | 68fa249 | 2015-02-17 20:48:01 +0000 | [diff] [blame] | 354 | return ".tbss"; |
Rafael Espindola | 449711c | 2015-11-18 06:02:15 +0000 | [diff] [blame] | 355 | if (Kind.isData()) |
Rafael Espindola | 68fa249 | 2015-02-17 20:48:01 +0000 | [diff] [blame] | 356 | return ".data"; |
Chris Lattner | 5b212a3 | 2010-04-13 00:36:43 +0000 | [diff] [blame] | 357 | assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); |
Rafael Espindola | 68fa249 | 2015-02-17 20:48:01 +0000 | [diff] [blame] | 358 | return ".data.rel.ro"; |
Chris Lattner | 5b212a3 | 2010-04-13 00:36:43 +0000 | [diff] [blame] | 359 | } |
| 360 | |
Evgeniy Stepanov | 51c962f72 | 2017-03-17 22:17:24 +0000 | [diff] [blame] | 361 | static MCSectionELF *selectELFSectionForGlobal( |
| 362 | MCContext &Ctx, const GlobalObject *GO, SectionKind Kind, Mangler &Mang, |
| 363 | const TargetMachine &TM, bool EmitUniqueSection, unsigned Flags, |
| 364 | unsigned *NextUniqueID, const MCSymbolELF *AssociatedSymbol) { |
Rafael Espindola | 8bc9ccc | 2015-02-25 00:52:15 +0000 | [diff] [blame] | 365 | unsigned EntrySize = 0; |
| 366 | if (Kind.isMergeableCString()) { |
| 367 | if (Kind.isMergeable2ByteCString()) { |
| 368 | EntrySize = 2; |
| 369 | } else if (Kind.isMergeable4ByteCString()) { |
| 370 | EntrySize = 4; |
| 371 | } else { |
| 372 | EntrySize = 1; |
| 373 | assert(Kind.isMergeable1ByteCString() && "unknown string width"); |
| 374 | } |
| 375 | } else if (Kind.isMergeableConst()) { |
| 376 | if (Kind.isMergeableConst4()) { |
| 377 | EntrySize = 4; |
| 378 | } else if (Kind.isMergeableConst8()) { |
| 379 | EntrySize = 8; |
David Majnemer | 964b70d | 2016-02-22 22:23:11 +0000 | [diff] [blame] | 380 | } else if (Kind.isMergeableConst16()) { |
Rafael Espindola | 8bc9ccc | 2015-02-25 00:52:15 +0000 | [diff] [blame] | 381 | EntrySize = 16; |
David Majnemer | 964b70d | 2016-02-22 22:23:11 +0000 | [diff] [blame] | 382 | } else { |
| 383 | assert(Kind.isMergeableConst32() && "unknown data width"); |
| 384 | EntrySize = 32; |
Rafael Espindola | 8bc9ccc | 2015-02-25 00:52:15 +0000 | [diff] [blame] | 385 | } |
| 386 | } |
| 387 | |
Rafael Espindola | 9075f77 | 2015-02-20 23:28:28 +0000 | [diff] [blame] | 388 | StringRef Group = ""; |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 389 | if (const Comdat *C = getELFComdat(GO)) { |
Rafael Espindola | 9075f77 | 2015-02-20 23:28:28 +0000 | [diff] [blame] | 390 | Flags |= ELF::SHF_GROUP; |
| 391 | Group = C->getName(); |
| 392 | } |
| 393 | |
Rafael Espindola | 8bc9ccc | 2015-02-25 00:52:15 +0000 | [diff] [blame] | 394 | bool UniqueSectionNames = TM.getUniqueSectionNames(); |
| 395 | SmallString<128> Name; |
Rafael Espindola | a05b3b7 | 2015-01-28 17:54:19 +0000 | [diff] [blame] | 396 | if (Kind.isMergeableCString()) { |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 397 | // We also need alignment here. |
| 398 | // FIXME: this is getting the alignment of the character, not the |
| 399 | // alignment of the global! |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 400 | unsigned Align = GO->getParent()->getDataLayout().getPreferredAlignment( |
| 401 | cast<GlobalVariable>(GO)); |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 402 | |
Rafael Espindola | ba31e27 | 2015-01-29 17:33:21 +0000 | [diff] [blame] | 403 | std::string SizeSpec = ".rodata.str" + utostr(EntrySize) + "."; |
Rafael Espindola | 8bc9ccc | 2015-02-25 00:52:15 +0000 | [diff] [blame] | 404 | Name = SizeSpec + utostr(Align); |
| 405 | } else if (Kind.isMergeableConst()) { |
| 406 | Name = ".rodata.cst"; |
| 407 | Name += utostr(EntrySize); |
| 408 | } else { |
| 409 | Name = getSectionPrefixForGlobal(Kind); |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 410 | } |
Dehao Chen | 302b69c | 2016-10-18 20:42:47 +0000 | [diff] [blame] | 411 | |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 412 | if (const auto *F = dyn_cast<Function>(GO)) { |
Dehao Chen | 302b69c | 2016-10-18 20:42:47 +0000 | [diff] [blame] | 413 | const auto &OptionalPrefix = F->getSectionPrefix(); |
| 414 | if (OptionalPrefix) |
| 415 | Name += *OptionalPrefix; |
| 416 | } |
Dehao Chen | f84b630 | 2016-02-23 03:39:24 +0000 | [diff] [blame] | 417 | |
Rafael Espindola | 8bc9ccc | 2015-02-25 00:52:15 +0000 | [diff] [blame] | 418 | if (EmitUniqueSection && UniqueSectionNames) { |
| 419 | Name.push_back('.'); |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 420 | TM.getNameWithPrefix(Name, GO, Mang, true); |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 421 | } |
Reid Kleckner | 97837b7 | 2016-05-02 23:22:18 +0000 | [diff] [blame] | 422 | unsigned UniqueID = MCContext::GenericSectionID; |
Rafael Espindola | 8ca44f0 | 2015-04-04 18:02:01 +0000 | [diff] [blame] | 423 | if (EmitUniqueSection && !UniqueSectionNames) { |
| 424 | UniqueID = *NextUniqueID; |
| 425 | (*NextUniqueID)++; |
| 426 | } |
Prakhar Bahuguna | 52a7dd7 | 2016-12-15 07:59:08 +0000 | [diff] [blame] | 427 | // Use 0 as the unique ID for execute-only text |
| 428 | if (Kind.isExecuteOnly()) |
| 429 | UniqueID = 0; |
Rafael Espindola | 4491d0d | 2015-02-26 23:55:11 +0000 | [diff] [blame] | 430 | return Ctx.getELFSection(Name, getELFSectionType(Name, Kind), Flags, |
Evgeniy Stepanov | 51c962f72 | 2017-03-17 22:17:24 +0000 | [diff] [blame] | 431 | EntrySize, Group, UniqueID, AssociatedSymbol); |
Rafael Espindola | 4491d0d | 2015-02-26 23:55:11 +0000 | [diff] [blame] | 432 | } |
| 433 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 434 | MCSection *TargetLoweringObjectFileELF::SelectSectionForGlobal( |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 435 | const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { |
Rafael Espindola | 4491d0d | 2015-02-26 23:55:11 +0000 | [diff] [blame] | 436 | unsigned Flags = getELFSectionFlags(Kind); |
| 437 | |
| 438 | // If we have -ffunction-section or -fdata-section then we should emit the |
| 439 | // global value to a uniqued section specifically for it. |
| 440 | bool EmitUniqueSection = false; |
| 441 | if (!(Flags & ELF::SHF_MERGE) && !Kind.isCommon()) { |
| 442 | if (Kind.isText()) |
| 443 | EmitUniqueSection = TM.getFunctionSections(); |
| 444 | else |
| 445 | EmitUniqueSection = TM.getDataSections(); |
| 446 | } |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 447 | EmitUniqueSection |= GO->hasComdat(); |
Rafael Espindola | 4491d0d | 2015-02-26 23:55:11 +0000 | [diff] [blame] | 448 | |
Evgeniy Stepanov | 51c962f72 | 2017-03-17 22:17:24 +0000 | [diff] [blame] | 449 | const MCSymbolELF *AssociatedSymbol = getAssociatedSymbol(GO, TM); |
| 450 | if (AssociatedSymbol) { |
| 451 | EmitUniqueSection = true; |
| 452 | Flags |= ELF::SHF_LINK_ORDER; |
| 453 | } |
| 454 | |
| 455 | MCSectionELF *Section = selectELFSectionForGlobal( |
| 456 | getContext(), GO, Kind, getMangler(), TM, EmitUniqueSection, Flags, |
| 457 | &NextUniqueID, AssociatedSymbol); |
| 458 | assert(Section->getAssociatedSymbol() == AssociatedSymbol); |
| 459 | return Section; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 462 | MCSection *TargetLoweringObjectFileELF::getSectionForJumpTable( |
Eric Christopher | 4367c7f | 2016-09-16 07:33:15 +0000 | [diff] [blame] | 463 | const Function &F, const TargetMachine &TM) const { |
Rafael Espindola | 29786d4 | 2015-02-12 17:16:46 +0000 | [diff] [blame] | 464 | // If the function can be removed, produce a unique section so that |
| 465 | // the table doesn't prevent the removal. |
| 466 | const Comdat *C = F.getComdat(); |
| 467 | bool EmitUniqueSection = TM.getFunctionSections() || C; |
| 468 | if (!EmitUniqueSection) |
| 469 | return ReadOnlySection; |
| 470 | |
Rafael Espindola | 4491d0d | 2015-02-26 23:55:11 +0000 | [diff] [blame] | 471 | return selectELFSectionForGlobal(getContext(), &F, SectionKind::getReadOnly(), |
Evgeniy Stepanov | 51c962f72 | 2017-03-17 22:17:24 +0000 | [diff] [blame] | 472 | getMangler(), TM, EmitUniqueSection, |
| 473 | ELF::SHF_ALLOC, &NextUniqueID, |
| 474 | /* AssociatedSymbol */ nullptr); |
Rafael Espindola | 29786d4 | 2015-02-12 17:16:46 +0000 | [diff] [blame] | 475 | } |
| 476 | |
Rafael Espindola | df19519 | 2015-02-17 23:34:51 +0000 | [diff] [blame] | 477 | bool TargetLoweringObjectFileELF::shouldPutJumpTableInFunctionSection( |
| 478 | bool UsesLabelDifference, const Function &F) const { |
| 479 | // We can always create relative relocations, so use another section |
| 480 | // that can be marked non-executable. |
| 481 | return false; |
| 482 | } |
| 483 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 484 | /// Given a mergeable constant with the specified size and relocation |
| 485 | /// information, return a section that it should be placed in. |
Mehdi Amini | 5c0fa58 | 2015-07-16 06:04:17 +0000 | [diff] [blame] | 486 | MCSection *TargetLoweringObjectFileELF::getSectionForConstant( |
David Majnemer | a3ea407 | 2016-02-21 01:30:30 +0000 | [diff] [blame] | 487 | const DataLayout &DL, SectionKind Kind, const Constant *C, |
| 488 | unsigned &Align) const { |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 489 | if (Kind.isMergeableConst4() && MergeableConst4Section) |
| 490 | return MergeableConst4Section; |
| 491 | if (Kind.isMergeableConst8() && MergeableConst8Section) |
| 492 | return MergeableConst8Section; |
| 493 | if (Kind.isMergeableConst16() && MergeableConst16Section) |
| 494 | return MergeableConst16Section; |
David Majnemer | 964b70d | 2016-02-22 22:23:11 +0000 | [diff] [blame] | 495 | if (Kind.isMergeableConst32() && MergeableConst32Section) |
| 496 | return MergeableConst32Section; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 497 | if (Kind.isReadOnly()) |
| 498 | return ReadOnlySection; |
| 499 | |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 500 | assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); |
| 501 | return DataRelROSection; |
| 502 | } |
| 503 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 504 | static MCSectionELF *getStaticStructorSection(MCContext &Ctx, bool UseInitArray, |
| 505 | bool IsCtor, unsigned Priority, |
| 506 | const MCSymbol *KeySym) { |
Rafael Espindola | c4b4253 | 2014-09-04 23:03:58 +0000 | [diff] [blame] | 507 | std::string Name; |
| 508 | unsigned Type; |
| 509 | unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE; |
Rafael Espindola | c4b4253 | 2014-09-04 23:03:58 +0000 | [diff] [blame] | 510 | StringRef COMDAT = KeySym ? KeySym->getName() : ""; |
| 511 | |
| 512 | if (KeySym) |
| 513 | Flags |= ELF::SHF_GROUP; |
Anton Korobeynikov | 7722a2d | 2012-01-25 22:24:19 +0000 | [diff] [blame] | 514 | |
Rafael Espindola | ca3e0ee | 2012-06-19 00:48:28 +0000 | [diff] [blame] | 515 | if (UseInitArray) { |
Rafael Espindola | 7c7d7b9 | 2014-09-05 00:02:50 +0000 | [diff] [blame] | 516 | if (IsCtor) { |
| 517 | Type = ELF::SHT_INIT_ARRAY; |
| 518 | Name = ".init_array"; |
| 519 | } else { |
| 520 | Type = ELF::SHT_FINI_ARRAY; |
| 521 | Name = ".fini_array"; |
| 522 | } |
Rafael Espindola | c4b4253 | 2014-09-04 23:03:58 +0000 | [diff] [blame] | 523 | if (Priority != 65535) { |
| 524 | Name += '.'; |
| 525 | Name += utostr(Priority); |
| 526 | } |
Rafael Espindola | ca3e0ee | 2012-06-19 00:48:28 +0000 | [diff] [blame] | 527 | } else { |
Rafael Espindola | c4b4253 | 2014-09-04 23:03:58 +0000 | [diff] [blame] | 528 | // The default scheme is .ctor / .dtor, so we have to invert the priority |
| 529 | // numbering. |
Rafael Espindola | 7c7d7b9 | 2014-09-05 00:02:50 +0000 | [diff] [blame] | 530 | if (IsCtor) |
| 531 | Name = ".ctors"; |
| 532 | else |
| 533 | Name = ".dtors"; |
Rafael Espindola | c4b4253 | 2014-09-04 23:03:58 +0000 | [diff] [blame] | 534 | if (Priority != 65535) { |
| 535 | Name += '.'; |
| 536 | Name += utostr(65535 - Priority); |
| 537 | } |
| 538 | Type = ELF::SHT_PROGBITS; |
Rafael Espindola | ca3e0ee | 2012-06-19 00:48:28 +0000 | [diff] [blame] | 539 | } |
Rafael Espindola | c4b4253 | 2014-09-04 23:03:58 +0000 | [diff] [blame] | 540 | |
Rafael Espindola | ba31e27 | 2015-01-29 17:33:21 +0000 | [diff] [blame] | 541 | return Ctx.getELFSection(Name, Type, Flags, 0, COMDAT); |
Rafael Espindola | 7c7d7b9 | 2014-09-05 00:02:50 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 544 | MCSection *TargetLoweringObjectFileELF::getStaticCtorSection( |
Rafael Espindola | 7c7d7b9 | 2014-09-05 00:02:50 +0000 | [diff] [blame] | 545 | unsigned Priority, const MCSymbol *KeySym) const { |
| 546 | return getStaticStructorSection(getContext(), UseInitArray, true, Priority, |
| 547 | KeySym); |
Anton Korobeynikov | 7722a2d | 2012-01-25 22:24:19 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 550 | MCSection *TargetLoweringObjectFileELF::getStaticDtorSection( |
Rafael Espindola | 0766ae0 | 2014-06-06 19:26:12 +0000 | [diff] [blame] | 551 | unsigned Priority, const MCSymbol *KeySym) const { |
Rafael Espindola | 7c7d7b9 | 2014-09-05 00:02:50 +0000 | [diff] [blame] | 552 | return getStaticStructorSection(getContext(), UseInitArray, false, Priority, |
| 553 | KeySym); |
Rafael Espindola | ca3e0ee | 2012-06-19 00:48:28 +0000 | [diff] [blame] | 554 | } |
| 555 | |
Peter Collingbourne | 265ebd7 | 2016-04-22 20:40:10 +0000 | [diff] [blame] | 556 | const MCExpr *TargetLoweringObjectFileELF::lowerRelativeReference( |
Eric Christopher | 4367c7f | 2016-09-16 07:33:15 +0000 | [diff] [blame] | 557 | const GlobalValue *LHS, const GlobalValue *RHS, |
Peter Collingbourne | 265ebd7 | 2016-04-22 20:40:10 +0000 | [diff] [blame] | 558 | const TargetMachine &TM) const { |
| 559 | // We may only use a PLT-relative relocation to refer to unnamed_addr |
| 560 | // functions. |
Peter Collingbourne | 96efdd6 | 2016-06-14 21:01:22 +0000 | [diff] [blame] | 561 | if (!LHS->hasGlobalUnnamedAddr() || !LHS->getValueType()->isFunctionTy()) |
Peter Collingbourne | 265ebd7 | 2016-04-22 20:40:10 +0000 | [diff] [blame] | 562 | return nullptr; |
| 563 | |
| 564 | // Basic sanity checks. |
| 565 | if (LHS->getType()->getPointerAddressSpace() != 0 || |
| 566 | RHS->getType()->getPointerAddressSpace() != 0 || LHS->isThreadLocal() || |
| 567 | RHS->isThreadLocal()) |
| 568 | return nullptr; |
| 569 | |
| 570 | return MCBinaryExpr::createSub( |
Tim Northover | b64fb45 | 2016-11-22 16:17:20 +0000 | [diff] [blame] | 571 | MCSymbolRefExpr::create(TM.getSymbol(LHS), PLTRelativeVariantKind, |
Peter Collingbourne | 265ebd7 | 2016-04-22 20:40:10 +0000 | [diff] [blame] | 572 | getContext()), |
Tim Northover | b64fb45 | 2016-11-22 16:17:20 +0000 | [diff] [blame] | 573 | MCSymbolRefExpr::create(TM.getSymbol(RHS), getContext()), getContext()); |
Peter Collingbourne | 265ebd7 | 2016-04-22 20:40:10 +0000 | [diff] [blame] | 574 | } |
| 575 | |
Rafael Espindola | ca3e0ee | 2012-06-19 00:48:28 +0000 | [diff] [blame] | 576 | void |
| 577 | TargetLoweringObjectFileELF::InitializeELF(bool UseInitArray_) { |
| 578 | UseInitArray = UseInitArray_; |
Rafael Espindola | 46fa231 | 2016-08-29 12:33:42 +0000 | [diff] [blame] | 579 | MCContext &Ctx = getContext(); |
| 580 | if (!UseInitArray) { |
| 581 | StaticCtorSection = Ctx.getELFSection(".ctors", ELF::SHT_PROGBITS, |
| 582 | ELF::SHF_ALLOC | ELF::SHF_WRITE); |
Rafael Espindola | ca3e0ee | 2012-06-19 00:48:28 +0000 | [diff] [blame] | 583 | |
Rafael Espindola | 46fa231 | 2016-08-29 12:33:42 +0000 | [diff] [blame] | 584 | StaticDtorSection = Ctx.getELFSection(".dtors", ELF::SHT_PROGBITS, |
| 585 | ELF::SHF_ALLOC | ELF::SHF_WRITE); |
| 586 | return; |
| 587 | } |
| 588 | |
| 589 | StaticCtorSection = Ctx.getELFSection(".init_array", ELF::SHT_INIT_ARRAY, |
| 590 | ELF::SHF_WRITE | ELF::SHF_ALLOC); |
| 591 | StaticDtorSection = Ctx.getELFSection(".fini_array", ELF::SHT_FINI_ARRAY, |
| 592 | ELF::SHF_WRITE | ELF::SHF_ALLOC); |
Anton Korobeynikov | 7722a2d | 2012-01-25 22:24:19 +0000 | [diff] [blame] | 593 | } |
| 594 | |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 595 | //===----------------------------------------------------------------------===// |
| 596 | // MachO |
| 597 | //===----------------------------------------------------------------------===// |
| 598 | |
Bruno Cardoso Lopes | 618c67a | 2015-03-06 13:49:05 +0000 | [diff] [blame] | 599 | TargetLoweringObjectFileMachO::TargetLoweringObjectFileMachO() |
| 600 | : TargetLoweringObjectFile() { |
| 601 | SupportIndirectSymViaGOTPCRel = true; |
| 602 | } |
| 603 | |
Rafael Espindola | 46fa231 | 2016-08-29 12:33:42 +0000 | [diff] [blame] | 604 | void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx, |
| 605 | const TargetMachine &TM) { |
| 606 | TargetLoweringObjectFile::Initialize(Ctx, TM); |
Rafael Espindola | 412a529 | 2016-08-29 12:47:22 +0000 | [diff] [blame] | 607 | if (TM.getRelocationModel() == Reloc::Static) { |
Rafael Espindola | 46fa231 | 2016-08-29 12:33:42 +0000 | [diff] [blame] | 608 | StaticCtorSection = Ctx.getMachOSection("__TEXT", "__constructor", 0, |
| 609 | SectionKind::getData()); |
| 610 | StaticDtorSection = Ctx.getMachOSection("__TEXT", "__destructor", 0, |
| 611 | SectionKind::getData()); |
| 612 | } else { |
| 613 | StaticCtorSection = Ctx.getMachOSection("__DATA", "__mod_init_func", |
| 614 | MachO::S_MOD_INIT_FUNC_POINTERS, |
| 615 | SectionKind::getData()); |
| 616 | StaticDtorSection = Ctx.getMachOSection("__DATA", "__mod_term_func", |
| 617 | MachO::S_MOD_TERM_FUNC_POINTERS, |
| 618 | SectionKind::getData()); |
| 619 | } |
| 620 | } |
| 621 | |
Peter Collingbourne | 89061b2 | 2017-06-12 20:10:48 +0000 | [diff] [blame^] | 622 | void TargetLoweringObjectFileMachO::emitModuleMetadata( |
| 623 | MCStreamer &Streamer, Module &M, const TargetMachine &TM) const { |
Daniel Dunbar | 9585612 | 2013-01-18 19:37:00 +0000 | [diff] [blame] | 624 | // Emit the linker options if present. |
Peter Collingbourne | 89061b2 | 2017-06-12 20:10:48 +0000 | [diff] [blame^] | 625 | if (auto *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) { |
Saleem Abdulrasool | e0f0c0e | 2016-04-30 18:15:34 +0000 | [diff] [blame] | 626 | for (const auto &Option : LinkerOptions->operands()) { |
Daniel Dunbar | 9585612 | 2013-01-18 19:37:00 +0000 | [diff] [blame] | 627 | SmallVector<std::string, 4> StrOptions; |
Saleem Abdulrasool | e0f0c0e | 2016-04-30 18:15:34 +0000 | [diff] [blame] | 628 | for (const auto &Piece : cast<MDNode>(Option)->operands()) |
| 629 | StrOptions.push_back(cast<MDString>(Piece)->getString()); |
Daniel Dunbar | 9585612 | 2013-01-18 19:37:00 +0000 | [diff] [blame] | 630 | Streamer.EmitLinkerOptions(StrOptions); |
| 631 | } |
Bill Wendling | 06df772 | 2012-02-14 21:28:13 +0000 | [diff] [blame] | 632 | } |
| 633 | |
Saleem Abdulrasool | 4c47434 | 2017-06-05 21:26:39 +0000 | [diff] [blame] | 634 | unsigned VersionVal = 0; |
| 635 | unsigned ImageInfoFlags = 0; |
| 636 | StringRef SectionVal; |
Peter Collingbourne | 89061b2 | 2017-06-12 20:10:48 +0000 | [diff] [blame^] | 637 | |
| 638 | GetObjCImageInfo(M, VersionVal, ImageInfoFlags, SectionVal); |
Saleem Abdulrasool | 4c47434 | 2017-06-05 21:26:39 +0000 | [diff] [blame] | 639 | |
Bill Wendling | 734909a | 2012-02-15 22:36:15 +0000 | [diff] [blame] | 640 | // The section is mandatory. If we don't have it, then we don't have GC info. |
Saleem Abdulrasool | 4c47434 | 2017-06-05 21:26:39 +0000 | [diff] [blame] | 641 | if (SectionVal.empty()) |
| 642 | return; |
Bill Wendling | 06df772 | 2012-02-14 21:28:13 +0000 | [diff] [blame] | 643 | |
Bill Wendling | 734909a | 2012-02-15 22:36:15 +0000 | [diff] [blame] | 644 | StringRef Segment, Section; |
| 645 | unsigned TAA = 0, StubSize = 0; |
| 646 | bool TAAParsed; |
| 647 | std::string ErrorCode = |
| 648 | MCSectionMachO::ParseSectionSpecifier(SectionVal, Segment, Section, |
| 649 | TAA, TAAParsed, StubSize); |
Bill Wendling | a0009ee | 2012-02-15 22:47:53 +0000 | [diff] [blame] | 650 | if (!ErrorCode.empty()) |
Bill Wendling | 734909a | 2012-02-15 22:36:15 +0000 | [diff] [blame] | 651 | // If invalid, report the error with report_fatal_error. |
Bill Wendling | a0009ee | 2012-02-15 22:47:53 +0000 | [diff] [blame] | 652 | report_fatal_error("Invalid section specifier '" + Section + "': " + |
| 653 | ErrorCode + "."); |
Bill Wendling | 06df772 | 2012-02-14 21:28:13 +0000 | [diff] [blame] | 654 | |
Bill Wendling | 734909a | 2012-02-15 22:36:15 +0000 | [diff] [blame] | 655 | // Get the section. |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 656 | MCSectionMachO *S = getContext().getMachOSection( |
Rafael Espindola | 449711c | 2015-11-18 06:02:15 +0000 | [diff] [blame] | 657 | Segment, Section, TAA, StubSize, SectionKind::getData()); |
Bill Wendling | 734909a | 2012-02-15 22:36:15 +0000 | [diff] [blame] | 658 | Streamer.SwitchSection(S); |
| 659 | Streamer.EmitLabel(getContext(). |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 660 | getOrCreateSymbol(StringRef("L_OBJC_IMAGE_INFO"))); |
Bill Wendling | 06df772 | 2012-02-14 21:28:13 +0000 | [diff] [blame] | 661 | Streamer.EmitIntValue(VersionVal, 4); |
Bill Wendling | f1b14b7 | 2012-04-24 11:03:50 +0000 | [diff] [blame] | 662 | Streamer.EmitIntValue(ImageInfoFlags, 4); |
Bill Wendling | 06df772 | 2012-02-14 21:28:13 +0000 | [diff] [blame] | 663 | Streamer.AddBlankLine(); |
| 664 | } |
| 665 | |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 666 | static void checkMachOComdat(const GlobalValue *GV) { |
| 667 | const Comdat *C = GV->getComdat(); |
| 668 | if (!C) |
| 669 | return; |
| 670 | |
| 671 | report_fatal_error("MachO doesn't support COMDATs, '" + C->getName() + |
| 672 | "' cannot be lowered."); |
| 673 | } |
| 674 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 675 | MCSection *TargetLoweringObjectFileMachO::getExplicitSectionGlobal( |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 676 | const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 677 | // Parse the section specifier and create it if valid. |
| 678 | StringRef Segment, Section; |
Stuart Hastings | 12d5312 | 2011-03-19 02:42:31 +0000 | [diff] [blame] | 679 | unsigned TAA = 0, StubSize = 0; |
| 680 | bool TAAParsed; |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 681 | |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 682 | checkMachOComdat(GO); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 683 | |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 684 | std::string ErrorCode = |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 685 | MCSectionMachO::ParseSectionSpecifier(GO->getSection(), Segment, Section, |
Stuart Hastings | 12d5312 | 2011-03-19 02:42:31 +0000 | [diff] [blame] | 686 | TAA, TAAParsed, StubSize); |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 687 | if (!ErrorCode.empty()) { |
Chris Lattner | 2104b8d | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 688 | // If invalid, report the error with report_fatal_error. |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 689 | report_fatal_error("Global variable '" + GO->getName() + |
Benjamin Kramer | 1f97a5a | 2011-11-15 16:27:03 +0000 | [diff] [blame] | 690 | "' has an invalid section specifier '" + |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 691 | GO->getSection() + "': " + ErrorCode + "."); |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | // Get the section. |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 695 | MCSectionMachO *S = |
| 696 | getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind); |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 697 | |
Stuart Hastings | b4863a4 | 2011-02-21 17:27:17 +0000 | [diff] [blame] | 698 | // If TAA wasn't set by ParseSectionSpecifier() above, |
| 699 | // use the value returned by getMachOSection() as a default. |
Stuart Hastings | 12d5312 | 2011-03-19 02:42:31 +0000 | [diff] [blame] | 700 | if (!TAAParsed) |
Stuart Hastings | b4863a4 | 2011-02-21 17:27:17 +0000 | [diff] [blame] | 701 | TAA = S->getTypeAndAttributes(); |
| 702 | |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 703 | // Okay, now that we got the section, verify that the TAA & StubSize agree. |
| 704 | // If the user declared multiple globals with different section flags, we need |
| 705 | // to reject it here. |
| 706 | if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) { |
Chris Lattner | 2104b8d | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 707 | // If invalid, report the error with report_fatal_error. |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 708 | report_fatal_error("Global variable '" + GO->getName() + |
Benjamin Kramer | 1f97a5a | 2011-11-15 16:27:03 +0000 | [diff] [blame] | 709 | "' section type or attributes does not match previous" |
| 710 | " section specifier"); |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | return S; |
| 714 | } |
| 715 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 716 | MCSection *TargetLoweringObjectFileMachO::SelectSectionForGlobal( |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 717 | const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { |
| 718 | checkMachOComdat(GO); |
Bill Wendling | 25b61db | 2013-11-17 10:53:13 +0000 | [diff] [blame] | 719 | |
| 720 | // Handle thread local data. |
| 721 | if (Kind.isThreadBSS()) return TLSBSSSection; |
| 722 | if (Kind.isThreadData()) return TLSDataSection; |
| 723 | |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 724 | if (Kind.isText()) |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 725 | return GO->isWeakForLinker() ? TextCoalSection : TextSection; |
Arnold Schwaighofer | c31c2de | 2013-08-08 21:04:16 +0000 | [diff] [blame] | 726 | |
| 727 | // If this is weak/linkonce, put this in a coalescable section, either in text |
| 728 | // or data depending on if it is writable. |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 729 | if (GO->isWeakForLinker()) { |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 730 | if (Kind.isReadOnly()) |
Arnold Schwaighofer | c31c2de | 2013-08-08 21:04:16 +0000 | [diff] [blame] | 731 | return ConstTextCoalSection; |
| 732 | return DataCoalSection; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | // FIXME: Alignment check should be handled by section classifier. |
Chris Lattner | ef2f804 | 2010-03-07 04:28:09 +0000 | [diff] [blame] | 736 | if (Kind.isMergeable1ByteCString() && |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 737 | GO->getParent()->getDataLayout().getPreferredAlignment( |
| 738 | cast<GlobalVariable>(GO)) < 32) |
Chris Lattner | ef2f804 | 2010-03-07 04:28:09 +0000 | [diff] [blame] | 739 | return CStringSection; |
Michael J. Spencer | fbdab0d | 2010-10-27 18:52:20 +0000 | [diff] [blame] | 740 | |
Chris Lattner | ef2f804 | 2010-03-07 04:28:09 +0000 | [diff] [blame] | 741 | // Do not put 16-bit arrays in the UString section if they have an |
| 742 | // externally visible label, this runs into issues with certain linker |
| 743 | // versions. |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 744 | if (Kind.isMergeable2ByteCString() && !GO->hasExternalLinkage() && |
| 745 | GO->getParent()->getDataLayout().getPreferredAlignment( |
| 746 | cast<GlobalVariable>(GO)) < 32) |
Chris Lattner | ef2f804 | 2010-03-07 04:28:09 +0000 | [diff] [blame] | 747 | return UStringSection; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 748 | |
Rafael Espindola | b43d51d | 2014-08-28 20:13:31 +0000 | [diff] [blame] | 749 | // With MachO only variables whose corresponding symbol starts with 'l' or |
| 750 | // 'L' can be merged, so we only try merging GVs with private linkage. |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 751 | if (GO->hasPrivateLinkage() && Kind.isMergeableConst()) { |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 752 | if (Kind.isMergeableConst4()) |
| 753 | return FourByteConstantSection; |
| 754 | if (Kind.isMergeableConst8()) |
| 755 | return EightByteConstantSection; |
Rafael Espindola | 1f3de49 | 2014-02-13 23:16:11 +0000 | [diff] [blame] | 756 | if (Kind.isMergeableConst16()) |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 757 | return SixteenByteConstantSection; |
| 758 | } |
| 759 | |
| 760 | // Otherwise, if it is readonly, but not something we can specially optimize, |
| 761 | // just drop it in .const. |
| 762 | if (Kind.isReadOnly()) |
| 763 | return ReadOnlySection; |
| 764 | |
| 765 | // If this is marked const, put it into a const section. But if the dynamic |
| 766 | // linker needs to write to it, put it in the data segment. |
| 767 | if (Kind.isReadOnlyWithRel()) |
| 768 | return ConstDataSection; |
| 769 | |
| 770 | // Put zero initialized globals with strong external linkage in the |
| 771 | // DATA, __common section with the .zerofill directive. |
| 772 | if (Kind.isBSSExtern()) |
| 773 | return DataCommonSection; |
| 774 | |
| 775 | // Put zero initialized globals with local linkage in __DATA,__bss directive |
| 776 | // with the .zerofill directive (aka .lcomm). |
| 777 | if (Kind.isBSSLocal()) |
| 778 | return DataBSSSection; |
Michael J. Spencer | fbdab0d | 2010-10-27 18:52:20 +0000 | [diff] [blame] | 779 | |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 780 | // Otherwise, just drop the variable in the normal data section. |
| 781 | return DataSection; |
| 782 | } |
| 783 | |
Mehdi Amini | 5c0fa58 | 2015-07-16 06:04:17 +0000 | [diff] [blame] | 784 | MCSection *TargetLoweringObjectFileMachO::getSectionForConstant( |
David Majnemer | a3ea407 | 2016-02-21 01:30:30 +0000 | [diff] [blame] | 785 | const DataLayout &DL, SectionKind Kind, const Constant *C, |
| 786 | unsigned &Align) const { |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 787 | // If this constant requires a relocation, we have to put it in the data |
| 788 | // segment, not in the text segment. |
Rafael Espindola | 449711c | 2015-11-18 06:02:15 +0000 | [diff] [blame] | 789 | if (Kind.isData() || Kind.isReadOnlyWithRel()) |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 790 | return ConstDataSection; |
| 791 | |
| 792 | if (Kind.isMergeableConst4()) |
| 793 | return FourByteConstantSection; |
| 794 | if (Kind.isMergeableConst8()) |
| 795 | return EightByteConstantSection; |
Rafael Espindola | 1f3de49 | 2014-02-13 23:16:11 +0000 | [diff] [blame] | 796 | if (Kind.isMergeableConst16()) |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 797 | return SixteenByteConstantSection; |
| 798 | return ReadOnlySection; // .const |
| 799 | } |
| 800 | |
Rafael Espindola | 15b2669 | 2014-02-09 14:50:44 +0000 | [diff] [blame] | 801 | const MCExpr *TargetLoweringObjectFileMachO::getTTypeGlobalReference( |
Eric Christopher | 4367c7f | 2016-09-16 07:33:15 +0000 | [diff] [blame] | 802 | const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM, |
| 803 | MachineModuleInfo *MMI, MCStreamer &Streamer) const { |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 804 | // The mach-o version of this method defaults to returning a stub reference. |
| 805 | |
Anton Korobeynikov | 31a9212 | 2010-02-21 20:28:15 +0000 | [diff] [blame] | 806 | if (Encoding & DW_EH_PE_indirect) { |
| 807 | MachineModuleInfoMachO &MachOMMI = |
| 808 | MMI->getObjFileInfo<MachineModuleInfoMachO>(); |
| 809 | |
Eric Christopher | 4367c7f | 2016-09-16 07:33:15 +0000 | [diff] [blame] | 810 | MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", TM); |
Anton Korobeynikov | 31a9212 | 2010-02-21 20:28:15 +0000 | [diff] [blame] | 811 | |
| 812 | // Add information about the stub reference to MachOMMI so that the stub |
| 813 | // gets emitted by the asmprinter. |
Rafael Espindola | 712f957 | 2016-05-17 16:01:32 +0000 | [diff] [blame] | 814 | MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 815 | if (!StubSym.getPointer()) { |
Tim Northover | b64fb45 | 2016-11-22 16:17:20 +0000 | [diff] [blame] | 816 | MCSymbol *Sym = TM.getSymbol(GV); |
Chris Lattner | 2ea586b | 2010-03-15 20:37:38 +0000 | [diff] [blame] | 817 | StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage()); |
Anton Korobeynikov | 31a9212 | 2010-02-21 20:28:15 +0000 | [diff] [blame] | 818 | } |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 819 | |
| 820 | return TargetLoweringObjectFile:: |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 821 | getTTypeReference(MCSymbolRefExpr::create(SSym, getContext()), |
Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 822 | Encoding & ~DW_EH_PE_indirect, Streamer); |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 823 | } |
| 824 | |
Eric Christopher | 4367c7f | 2016-09-16 07:33:15 +0000 | [diff] [blame] | 825 | return TargetLoweringObjectFile::getTTypeGlobalReference(GV, Encoding, TM, |
| 826 | MMI, Streamer); |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 827 | } |
| 828 | |
Rafael Espindola | daeafb4 | 2014-02-19 17:23:20 +0000 | [diff] [blame] | 829 | MCSymbol *TargetLoweringObjectFileMachO::getCFIPersonalitySymbol( |
Eric Christopher | 4367c7f | 2016-09-16 07:33:15 +0000 | [diff] [blame] | 830 | const GlobalValue *GV, const TargetMachine &TM, |
Rafael Espindola | daeafb4 | 2014-02-19 17:23:20 +0000 | [diff] [blame] | 831 | MachineModuleInfo *MMI) const { |
Rafael Espindola | 0870434 | 2011-04-27 23:08:15 +0000 | [diff] [blame] | 832 | // The mach-o version of this method defaults to returning a stub reference. |
| 833 | MachineModuleInfoMachO &MachOMMI = |
| 834 | MMI->getObjFileInfo<MachineModuleInfoMachO>(); |
| 835 | |
Eric Christopher | 4367c7f | 2016-09-16 07:33:15 +0000 | [diff] [blame] | 836 | MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", TM); |
Rafael Espindola | 0870434 | 2011-04-27 23:08:15 +0000 | [diff] [blame] | 837 | |
| 838 | // Add information about the stub reference to MachOMMI so that the stub |
| 839 | // gets emitted by the asmprinter. |
Bill Wendling | e4cc332 | 2011-11-29 01:43:20 +0000 | [diff] [blame] | 840 | MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 841 | if (!StubSym.getPointer()) { |
Tim Northover | b64fb45 | 2016-11-22 16:17:20 +0000 | [diff] [blame] | 842 | MCSymbol *Sym = TM.getSymbol(GV); |
Rafael Espindola | 0870434 | 2011-04-27 23:08:15 +0000 | [diff] [blame] | 843 | StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage()); |
| 844 | } |
| 845 | |
| 846 | return SSym; |
| 847 | } |
| 848 | |
Bruno Cardoso Lopes | 618c67a | 2015-03-06 13:49:05 +0000 | [diff] [blame] | 849 | const MCExpr *TargetLoweringObjectFileMachO::getIndirectSymViaGOTPCRel( |
| 850 | const MCSymbol *Sym, const MCValue &MV, int64_t Offset, |
| 851 | MachineModuleInfo *MMI, MCStreamer &Streamer) const { |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 852 | // 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] | 853 | // as 64-bit do, we replace the GOT equivalent by accessing the final symbol |
| 854 | // through a non_lazy_ptr stub instead. One advantage is that it allows the |
| 855 | // computation of deltas to final external symbols. Example: |
| 856 | // |
| 857 | // _extgotequiv: |
| 858 | // .long _extfoo |
| 859 | // |
| 860 | // _delta: |
| 861 | // .long _extgotequiv-_delta |
| 862 | // |
| 863 | // is transformed to: |
| 864 | // |
| 865 | // _delta: |
| 866 | // .long L_extfoo$non_lazy_ptr-(_delta+0) |
| 867 | // |
| 868 | // .section __IMPORT,__pointers,non_lazy_symbol_pointers |
| 869 | // L_extfoo$non_lazy_ptr: |
| 870 | // .indirect_symbol _extfoo |
| 871 | // .long 0 |
| 872 | // |
| 873 | MachineModuleInfoMachO &MachOMMI = |
| 874 | MMI->getObjFileInfo<MachineModuleInfoMachO>(); |
| 875 | MCContext &Ctx = getContext(); |
| 876 | |
| 877 | // The offset must consider the original displacement from the base symbol |
| 878 | // since 32-bit targets don't have a GOTPCREL to fold the PC displacement. |
| 879 | Offset = -MV.getConstant(); |
| 880 | const MCSymbol *BaseSym = &MV.getSymB()->getSymbol(); |
| 881 | |
| 882 | // Access the final symbol via sym$non_lazy_ptr and generate the appropriated |
| 883 | // non_lazy_ptr stubs. |
| 884 | SmallString<128> Name; |
| 885 | StringRef Suffix = "$non_lazy_ptr"; |
Mehdi Amini | 5c0fa58 | 2015-07-16 06:04:17 +0000 | [diff] [blame] | 886 | Name += MMI->getModule()->getDataLayout().getPrivateGlobalPrefix(); |
Bruno Cardoso Lopes | 618c67a | 2015-03-06 13:49:05 +0000 | [diff] [blame] | 887 | Name += Sym->getName(); |
| 888 | Name += Suffix; |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 889 | MCSymbol *Stub = Ctx.getOrCreateSymbol(Name); |
Bruno Cardoso Lopes | 618c67a | 2015-03-06 13:49:05 +0000 | [diff] [blame] | 890 | |
| 891 | MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(Stub); |
| 892 | if (!StubSym.getPointer()) |
| 893 | StubSym = MachineModuleInfoImpl:: |
| 894 | StubValueTy(const_cast<MCSymbol *>(Sym), true /* access indirectly */); |
| 895 | |
| 896 | const MCExpr *BSymExpr = |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 897 | MCSymbolRefExpr::create(BaseSym, MCSymbolRefExpr::VK_None, Ctx); |
Bruno Cardoso Lopes | 618c67a | 2015-03-06 13:49:05 +0000 | [diff] [blame] | 898 | const MCExpr *LHS = |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 899 | MCSymbolRefExpr::create(Stub, MCSymbolRefExpr::VK_None, Ctx); |
Bruno Cardoso Lopes | 618c67a | 2015-03-06 13:49:05 +0000 | [diff] [blame] | 900 | |
| 901 | if (!Offset) |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 902 | return MCBinaryExpr::createSub(LHS, BSymExpr, Ctx); |
Bruno Cardoso Lopes | 618c67a | 2015-03-06 13:49:05 +0000 | [diff] [blame] | 903 | |
| 904 | const MCExpr *RHS = |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 905 | MCBinaryExpr::createAdd(BSymExpr, MCConstantExpr::create(Offset, Ctx), Ctx); |
| 906 | return MCBinaryExpr::createSub(LHS, RHS, Ctx); |
Bruno Cardoso Lopes | 618c67a | 2015-03-06 13:49:05 +0000 | [diff] [blame] | 907 | } |
| 908 | |
Peter Collingbourne | 94d7786 | 2015-11-03 23:40:03 +0000 | [diff] [blame] | 909 | static bool canUsePrivateLabel(const MCAsmInfo &AsmInfo, |
| 910 | const MCSection &Section) { |
| 911 | if (!AsmInfo.isSectionAtomizableBySymbols(Section)) |
| 912 | return true; |
| 913 | |
| 914 | // If it is not dead stripped, it is safe to use private labels. |
| 915 | const MCSectionMachO &SMO = cast<MCSectionMachO>(Section); |
| 916 | if (SMO.hasAttribute(MachO::S_ATTR_NO_DEAD_STRIP)) |
| 917 | return true; |
| 918 | |
| 919 | return false; |
| 920 | } |
| 921 | |
| 922 | void TargetLoweringObjectFileMachO::getNameWithPrefix( |
Eric Christopher | 4367c7f | 2016-09-16 07:33:15 +0000 | [diff] [blame] | 923 | SmallVectorImpl<char> &OutName, const GlobalValue *GV, |
Peter Collingbourne | 94d7786 | 2015-11-03 23:40:03 +0000 | [diff] [blame] | 924 | const TargetMachine &TM) const { |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 925 | bool CannotUsePrivateLabel = true; |
| 926 | if (auto *GO = GV->getBaseObject()) { |
| 927 | SectionKind GOKind = TargetLoweringObjectFile::getKindForGlobal(GO, TM); |
| 928 | const MCSection *TheSection = SectionForGlobal(GO, GOKind, TM); |
| 929 | CannotUsePrivateLabel = |
| 930 | !canUsePrivateLabel(*TM.getMCAsmInfo(), *TheSection); |
| 931 | } |
Eric Christopher | 4367c7f | 2016-09-16 07:33:15 +0000 | [diff] [blame] | 932 | getMangler().getNameWithPrefix(OutName, GV, CannotUsePrivateLabel); |
Peter Collingbourne | 94d7786 | 2015-11-03 23:40:03 +0000 | [diff] [blame] | 933 | } |
| 934 | |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 935 | //===----------------------------------------------------------------------===// |
| 936 | // COFF |
| 937 | //===----------------------------------------------------------------------===// |
| 938 | |
Chris Lattner | 87cffa9 | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 939 | static unsigned |
Renato Golin | ef3eb06 | 2016-06-27 14:42:20 +0000 | [diff] [blame] | 940 | getCOFFSectionFlags(SectionKind K, const TargetMachine &TM) { |
Chris Lattner | 87cffa9 | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 941 | unsigned Flags = 0; |
Renato Golin | ef3eb06 | 2016-06-27 14:42:20 +0000 | [diff] [blame] | 942 | bool isThumb = TM.getTargetTriple().getArch() == Triple::thumb; |
Chris Lattner | 87cffa9 | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 943 | |
Anton Korobeynikov | e415230 | 2010-07-06 15:24:56 +0000 | [diff] [blame] | 944 | if (K.isMetadata()) |
Chris Lattner | 0284493 | 2010-05-07 21:49:09 +0000 | [diff] [blame] | 945 | Flags |= |
Daniel Dunbar | 329d202 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 946 | COFF::IMAGE_SCN_MEM_DISCARDABLE; |
Chris Lattner | 87cffa9 | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 947 | else if (K.isText()) |
| 948 | Flags |= |
Daniel Dunbar | 329d202 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 949 | COFF::IMAGE_SCN_MEM_EXECUTE | |
Michael J. Spencer | 0f83d96 | 2010-10-27 18:52:29 +0000 | [diff] [blame] | 950 | COFF::IMAGE_SCN_MEM_READ | |
Renato Golin | ef3eb06 | 2016-06-27 14:42:20 +0000 | [diff] [blame] | 951 | COFF::IMAGE_SCN_CNT_CODE | |
| 952 | (isThumb ? COFF::IMAGE_SCN_MEM_16BIT : (COFF::SectionCharacteristics)0); |
David Majnemer | b8dbebb | 2014-09-20 07:31:46 +0000 | [diff] [blame] | 953 | else if (K.isBSS()) |
Chris Lattner | 0284493 | 2010-05-07 21:49:09 +0000 | [diff] [blame] | 954 | Flags |= |
Daniel Dunbar | 329d202 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 955 | COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA | |
| 956 | COFF::IMAGE_SCN_MEM_READ | |
| 957 | COFF::IMAGE_SCN_MEM_WRITE; |
Anton Korobeynikov | c6b4017 | 2012-02-11 17:26:53 +0000 | [diff] [blame] | 958 | else if (K.isThreadLocal()) |
| 959 | Flags |= |
| 960 | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | |
| 961 | COFF::IMAGE_SCN_MEM_READ | |
| 962 | COFF::IMAGE_SCN_MEM_WRITE; |
David Majnemer | b8dbebb | 2014-09-20 07:31:46 +0000 | [diff] [blame] | 963 | else if (K.isReadOnly() || K.isReadOnlyWithRel()) |
Chris Lattner | 87cffa9 | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 964 | Flags |= |
Daniel Dunbar | 329d202 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 965 | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | |
| 966 | COFF::IMAGE_SCN_MEM_READ; |
Chris Lattner | 87cffa9 | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 967 | else if (K.isWriteable()) |
| 968 | Flags |= |
Daniel Dunbar | 329d202 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 969 | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | |
| 970 | COFF::IMAGE_SCN_MEM_READ | |
| 971 | COFF::IMAGE_SCN_MEM_WRITE; |
Chris Lattner | 87cffa9 | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 972 | |
| 973 | return Flags; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 974 | } |
| 975 | |
Benjamin Kramer | 6cbe670 | 2014-07-07 14:47:51 +0000 | [diff] [blame] | 976 | static const GlobalValue *getComdatGVForCOFF(const GlobalValue *GV) { |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 977 | const Comdat *C = GV->getComdat(); |
| 978 | assert(C && "expected GV to have a Comdat!"); |
| 979 | |
| 980 | StringRef ComdatGVName = C->getName(); |
| 981 | const GlobalValue *ComdatGV = GV->getParent()->getNamedValue(ComdatGVName); |
| 982 | if (!ComdatGV) |
| 983 | report_fatal_error("Associative COMDAT symbol '" + ComdatGVName + |
| 984 | "' does not exist."); |
| 985 | |
| 986 | if (ComdatGV->getComdat() != C) |
| 987 | report_fatal_error("Associative COMDAT symbol '" + ComdatGVName + |
Hans Wennborg | c0f0c51 | 2014-09-19 01:14:56 +0000 | [diff] [blame] | 988 | "' is not a key for its COMDAT."); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 989 | |
| 990 | return ComdatGV; |
| 991 | } |
| 992 | |
| 993 | static int getSelectionForCOFF(const GlobalValue *GV) { |
| 994 | if (const Comdat *C = GV->getComdat()) { |
| 995 | const GlobalValue *ComdatKey = getComdatGVForCOFF(GV); |
| 996 | if (const auto *GA = dyn_cast<GlobalAlias>(ComdatKey)) |
| 997 | ComdatKey = GA->getBaseObject(); |
| 998 | if (ComdatKey == GV) { |
| 999 | switch (C->getSelectionKind()) { |
| 1000 | case Comdat::Any: |
| 1001 | return COFF::IMAGE_COMDAT_SELECT_ANY; |
| 1002 | case Comdat::ExactMatch: |
| 1003 | return COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH; |
| 1004 | case Comdat::Largest: |
| 1005 | return COFF::IMAGE_COMDAT_SELECT_LARGEST; |
| 1006 | case Comdat::NoDuplicates: |
| 1007 | return COFF::IMAGE_COMDAT_SELECT_NODUPLICATES; |
| 1008 | case Comdat::SameSize: |
| 1009 | return COFF::IMAGE_COMDAT_SELECT_SAME_SIZE; |
| 1010 | } |
| 1011 | } else { |
| 1012 | return COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE; |
| 1013 | } |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 1014 | } |
| 1015 | return 0; |
| 1016 | } |
| 1017 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 1018 | MCSection *TargetLoweringObjectFileCOFF::getExplicitSectionGlobal( |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 1019 | const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { |
Michael J. Spencer | f1aef75 | 2012-11-13 22:04:09 +0000 | [diff] [blame] | 1020 | int Selection = 0; |
Renato Golin | ef3eb06 | 2016-06-27 14:42:20 +0000 | [diff] [blame] | 1021 | unsigned Characteristics = getCOFFSectionFlags(Kind, TM); |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 1022 | StringRef Name = GO->getSection(); |
Rafael Espindola | 2d30ae2 | 2013-11-27 01:18:37 +0000 | [diff] [blame] | 1023 | StringRef COMDATSymName = ""; |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 1024 | if (GO->hasComdat()) { |
| 1025 | Selection = getSelectionForCOFF(GO); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 1026 | const GlobalValue *ComdatGV; |
| 1027 | if (Selection == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 1028 | ComdatGV = getComdatGVForCOFF(GO); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 1029 | else |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 1030 | ComdatGV = GO; |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 1031 | |
| 1032 | if (!ComdatGV->hasPrivateLinkage()) { |
Tim Northover | b64fb45 | 2016-11-22 16:17:20 +0000 | [diff] [blame] | 1033 | MCSymbol *Sym = TM.getSymbol(ComdatGV); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 1034 | COMDATSymName = Sym->getName(); |
| 1035 | Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT; |
| 1036 | } else { |
| 1037 | Selection = 0; |
| 1038 | } |
Michael J. Spencer | f1aef75 | 2012-11-13 22:04:09 +0000 | [diff] [blame] | 1039 | } |
Reid Kleckner | 97837b7 | 2016-05-02 23:22:18 +0000 | [diff] [blame] | 1040 | |
| 1041 | return getContext().getCOFFSection(Name, Characteristics, Kind, COMDATSymName, |
Nico Rieck | a37acf7 | 2013-07-06 12:13:10 +0000 | [diff] [blame] | 1042 | Selection); |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 1043 | } |
| 1044 | |
Rafael Espindola | 2d30ae2 | 2013-11-27 01:18:37 +0000 | [diff] [blame] | 1045 | static const char *getCOFFSectionNameForUniqueGlobal(SectionKind Kind) { |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 1046 | if (Kind.isText()) |
Rafael Espindola | 2d30ae2 | 2013-11-27 01:18:37 +0000 | [diff] [blame] | 1047 | return ".text"; |
David Majnemer | a9bdb32 | 2014-04-08 22:33:40 +0000 | [diff] [blame] | 1048 | if (Kind.isBSS()) |
Rafael Espindola | 2d30ae2 | 2013-11-27 01:18:37 +0000 | [diff] [blame] | 1049 | return ".bss"; |
| 1050 | if (Kind.isThreadLocal()) |
Rafael Espindola | 3c8e147 | 2013-11-27 15:52:11 +0000 | [diff] [blame] | 1051 | return ".tls$"; |
David Majnemer | 597be2d | 2014-09-22 20:39:23 +0000 | [diff] [blame] | 1052 | if (Kind.isReadOnly() || Kind.isReadOnlyWithRel()) |
| 1053 | return ".rdata"; |
| 1054 | return ".data"; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 1057 | MCSection *TargetLoweringObjectFileCOFF::SelectSectionForGlobal( |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 1058 | const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { |
David Majnemer | 9338984 | 2014-03-23 17:47:39 +0000 | [diff] [blame] | 1059 | // If we have -ffunction-sections then we should emit the global value to a |
| 1060 | // uniqued section specifically for it. |
David Majnemer | 273bff4 | 2014-03-25 06:14:26 +0000 | [diff] [blame] | 1061 | bool EmitUniquedSection; |
| 1062 | if (Kind.isText()) |
| 1063 | EmitUniquedSection = TM.getFunctionSections(); |
| 1064 | else |
| 1065 | EmitUniquedSection = TM.getDataSections(); |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 1066 | |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 1067 | if ((EmitUniquedSection && !Kind.isCommon()) || GO->hasComdat()) { |
Rafael Espindola | 2d30ae2 | 2013-11-27 01:18:37 +0000 | [diff] [blame] | 1068 | const char *Name = getCOFFSectionNameForUniqueGlobal(Kind); |
Renato Golin | ef3eb06 | 2016-06-27 14:42:20 +0000 | [diff] [blame] | 1069 | unsigned Characteristics = getCOFFSectionFlags(Kind, TM); |
Chris Lattner | 0284493 | 2010-05-07 21:49:09 +0000 | [diff] [blame] | 1070 | |
Daniel Dunbar | 329d202 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 1071 | Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT; |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 1072 | int Selection = getSelectionForCOFF(GO); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 1073 | if (!Selection) |
| 1074 | Selection = COFF::IMAGE_COMDAT_SELECT_NODUPLICATES; |
| 1075 | const GlobalValue *ComdatGV; |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 1076 | if (GO->hasComdat()) |
| 1077 | ComdatGV = getComdatGVForCOFF(GO); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 1078 | else |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 1079 | ComdatGV = GO; |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 1080 | |
Reid Kleckner | 97837b7 | 2016-05-02 23:22:18 +0000 | [diff] [blame] | 1081 | unsigned UniqueID = MCContext::GenericSectionID; |
| 1082 | if (EmitUniquedSection) |
| 1083 | UniqueID = NextUniqueID++; |
| 1084 | |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 1085 | if (!ComdatGV->hasPrivateLinkage()) { |
Tim Northover | b64fb45 | 2016-11-22 16:17:20 +0000 | [diff] [blame] | 1086 | MCSymbol *Sym = TM.getSymbol(ComdatGV); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 1087 | StringRef COMDATSymName = Sym->getName(); |
| 1088 | return getContext().getCOFFSection(Name, Characteristics, Kind, |
Reid Kleckner | 97837b7 | 2016-05-02 23:22:18 +0000 | [diff] [blame] | 1089 | COMDATSymName, Selection, UniqueID); |
David Majnemer | 7db449a | 2015-03-17 23:54:51 +0000 | [diff] [blame] | 1090 | } else { |
| 1091 | SmallString<256> TmpData; |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 1092 | getMangler().getNameWithPrefix(TmpData, GO, /*CannotUsePrivateLabel=*/true); |
David Majnemer | 7db449a | 2015-03-17 23:54:51 +0000 | [diff] [blame] | 1093 | return getContext().getCOFFSection(Name, Characteristics, Kind, TmpData, |
Reid Kleckner | 97837b7 | 2016-05-02 23:22:18 +0000 | [diff] [blame] | 1094 | Selection, UniqueID); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 1095 | } |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 1096 | } |
| 1097 | |
| 1098 | if (Kind.isText()) |
David Majnemer | 3d96acb | 2013-08-13 01:23:53 +0000 | [diff] [blame] | 1099 | return TextSection; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 1100 | |
Anton Korobeynikov | c6b4017 | 2012-02-11 17:26:53 +0000 | [diff] [blame] | 1101 | if (Kind.isThreadLocal()) |
David Majnemer | 3d96acb | 2013-08-13 01:23:53 +0000 | [diff] [blame] | 1102 | return TLSDataSection; |
Anton Korobeynikov | c6b4017 | 2012-02-11 17:26:53 +0000 | [diff] [blame] | 1103 | |
David Majnemer | 597be2d | 2014-09-22 20:39:23 +0000 | [diff] [blame] | 1104 | if (Kind.isReadOnly() || Kind.isReadOnlyWithRel()) |
David Majnemer | f76d6b3 | 2013-08-08 01:50:52 +0000 | [diff] [blame] | 1105 | return ReadOnlySection; |
| 1106 | |
David Majnemer | a9bdb32 | 2014-04-08 22:33:40 +0000 | [diff] [blame] | 1107 | // Note: we claim that common symbols are put in BSSSection, but they are |
| 1108 | // really emitted with the magic .comm directive, which creates a symbol table |
| 1109 | // entry but not a section. |
| 1110 | if (Kind.isBSS() || Kind.isCommon()) |
David Majnemer | 3d96acb | 2013-08-13 01:23:53 +0000 | [diff] [blame] | 1111 | return BSSSection; |
| 1112 | |
| 1113 | return DataSection; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 1114 | } |
| 1115 | |
David Majnemer | 7db449a | 2015-03-17 23:54:51 +0000 | [diff] [blame] | 1116 | void TargetLoweringObjectFileCOFF::getNameWithPrefix( |
Eric Christopher | 4367c7f | 2016-09-16 07:33:15 +0000 | [diff] [blame] | 1117 | SmallVectorImpl<char> &OutName, const GlobalValue *GV, |
Peter Collingbourne | 94d7786 | 2015-11-03 23:40:03 +0000 | [diff] [blame] | 1118 | const TargetMachine &TM) const { |
| 1119 | bool CannotUsePrivateLabel = false; |
David Majnemer | 7db449a | 2015-03-17 23:54:51 +0000 | [diff] [blame] | 1120 | if (GV->hasPrivateLinkage() && |
| 1121 | ((isa<Function>(GV) && TM.getFunctionSections()) || |
| 1122 | (isa<GlobalVariable>(GV) && TM.getDataSections()))) |
| 1123 | CannotUsePrivateLabel = true; |
| 1124 | |
Eric Christopher | 4367c7f | 2016-09-16 07:33:15 +0000 | [diff] [blame] | 1125 | getMangler().getNameWithPrefix(OutName, GV, CannotUsePrivateLabel); |
David Majnemer | 7db449a | 2015-03-17 23:54:51 +0000 | [diff] [blame] | 1126 | } |
| 1127 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 1128 | MCSection *TargetLoweringObjectFileCOFF::getSectionForJumpTable( |
Eric Christopher | 4367c7f | 2016-09-16 07:33:15 +0000 | [diff] [blame] | 1129 | const Function &F, const TargetMachine &TM) const { |
Rafael Espindola | ab447e4 | 2015-03-11 19:58:37 +0000 | [diff] [blame] | 1130 | // If the function can be removed, produce a unique section so that |
| 1131 | // the table doesn't prevent the removal. |
| 1132 | const Comdat *C = F.getComdat(); |
| 1133 | bool EmitUniqueSection = TM.getFunctionSections() || C; |
| 1134 | if (!EmitUniqueSection) |
| 1135 | return ReadOnlySection; |
| 1136 | |
| 1137 | // FIXME: we should produce a symbol for F instead. |
| 1138 | if (F.hasPrivateLinkage()) |
| 1139 | return ReadOnlySection; |
| 1140 | |
Tim Northover | b64fb45 | 2016-11-22 16:17:20 +0000 | [diff] [blame] | 1141 | MCSymbol *Sym = TM.getSymbol(&F); |
Rafael Espindola | ab447e4 | 2015-03-11 19:58:37 +0000 | [diff] [blame] | 1142 | StringRef COMDATSymName = Sym->getName(); |
| 1143 | |
| 1144 | SectionKind Kind = SectionKind::getReadOnly(); |
| 1145 | const char *Name = getCOFFSectionNameForUniqueGlobal(Kind); |
Renato Golin | ef3eb06 | 2016-06-27 14:42:20 +0000 | [diff] [blame] | 1146 | unsigned Characteristics = getCOFFSectionFlags(Kind, TM); |
Rafael Espindola | ab447e4 | 2015-03-11 19:58:37 +0000 | [diff] [blame] | 1147 | Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT; |
Reid Kleckner | 97837b7 | 2016-05-02 23:22:18 +0000 | [diff] [blame] | 1148 | unsigned UniqueID = NextUniqueID++; |
Rafael Espindola | ab447e4 | 2015-03-11 19:58:37 +0000 | [diff] [blame] | 1149 | |
| 1150 | return getContext().getCOFFSection(Name, Characteristics, Kind, COMDATSymName, |
Reid Kleckner | 97837b7 | 2016-05-02 23:22:18 +0000 | [diff] [blame] | 1151 | COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE, UniqueID); |
Rafael Espindola | ab447e4 | 2015-03-11 19:58:37 +0000 | [diff] [blame] | 1152 | } |
| 1153 | |
Peter Collingbourne | 89061b2 | 2017-06-12 20:10:48 +0000 | [diff] [blame^] | 1154 | void TargetLoweringObjectFileCOFF::emitModuleMetadata( |
| 1155 | MCStreamer &Streamer, Module &M, const TargetMachine &TM) const { |
| 1156 | if (NamedMDNode *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) { |
Saleem Abdulrasool | e0f0c0e | 2016-04-30 18:15:34 +0000 | [diff] [blame] | 1157 | // Emit the linker options to the linker .drectve section. According to the |
| 1158 | // spec, this section is a space-separated string containing flags for |
| 1159 | // linker. |
| 1160 | MCSection *Sec = getDrectveSection(); |
| 1161 | Streamer.SwitchSection(Sec); |
| 1162 | for (const auto &Option : LinkerOptions->operands()) { |
| 1163 | for (const auto &Piece : cast<MDNode>(Option)->operands()) { |
| 1164 | // Lead with a space for consistency with our dllexport implementation. |
| 1165 | std::string Directive(" "); |
| 1166 | Directive.append(cast<MDString>(Piece)->getString()); |
| 1167 | Streamer.EmitBytes(Directive); |
| 1168 | } |
Reid Kleckner | d973ca3 | 2013-04-25 19:34:41 +0000 | [diff] [blame] | 1169 | } |
| 1170 | } |
Saleem Abdulrasool | 4c47434 | 2017-06-05 21:26:39 +0000 | [diff] [blame] | 1171 | |
| 1172 | unsigned Version = 0; |
| 1173 | unsigned Flags = 0; |
| 1174 | StringRef Section; |
| 1175 | |
Peter Collingbourne | 89061b2 | 2017-06-12 20:10:48 +0000 | [diff] [blame^] | 1176 | GetObjCImageInfo(M, Version, Flags, Section); |
Saleem Abdulrasool | 4c47434 | 2017-06-05 21:26:39 +0000 | [diff] [blame] | 1177 | if (Section.empty()) |
| 1178 | return; |
| 1179 | |
| 1180 | auto &C = getContext(); |
| 1181 | auto *S = C.getCOFFSection( |
| 1182 | Section, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ, |
| 1183 | SectionKind::getReadOnly()); |
| 1184 | Streamer.SwitchSection(S); |
| 1185 | Streamer.EmitLabel(C.getOrCreateSymbol(StringRef("OBJC_IMAGE_INFO"))); |
| 1186 | Streamer.EmitIntValue(Version, 4); |
| 1187 | Streamer.EmitIntValue(Flags, 4); |
| 1188 | Streamer.AddBlankLine(); |
Reid Kleckner | d973ca3 | 2013-04-25 19:34:41 +0000 | [diff] [blame] | 1189 | } |
Reid Kleckner | fceb76f | 2014-05-16 20:39:27 +0000 | [diff] [blame] | 1190 | |
Rafael Espindola | 46fa231 | 2016-08-29 12:33:42 +0000 | [diff] [blame] | 1191 | void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx, |
| 1192 | const TargetMachine &TM) { |
| 1193 | TargetLoweringObjectFile::Initialize(Ctx, TM); |
| 1194 | const Triple &T = TM.getTargetTriple(); |
| 1195 | if (T.isKnownWindowsMSVCEnvironment() || T.isWindowsItaniumEnvironment()) { |
| 1196 | StaticCtorSection = |
| 1197 | Ctx.getCOFFSection(".CRT$XCU", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | |
| 1198 | COFF::IMAGE_SCN_MEM_READ, |
| 1199 | SectionKind::getReadOnly()); |
| 1200 | StaticDtorSection = |
| 1201 | Ctx.getCOFFSection(".CRT$XTX", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | |
| 1202 | COFF::IMAGE_SCN_MEM_READ, |
| 1203 | SectionKind::getReadOnly()); |
| 1204 | } else { |
| 1205 | StaticCtorSection = Ctx.getCOFFSection( |
| 1206 | ".ctors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | |
| 1207 | COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE, |
| 1208 | SectionKind::getData()); |
| 1209 | StaticDtorSection = Ctx.getCOFFSection( |
| 1210 | ".dtors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | |
| 1211 | COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE, |
| 1212 | SectionKind::getData()); |
| 1213 | } |
| 1214 | } |
| 1215 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 1216 | MCSection *TargetLoweringObjectFileCOFF::getStaticCtorSection( |
Rafael Espindola | 0766ae0 | 2014-06-06 19:26:12 +0000 | [diff] [blame] | 1217 | unsigned Priority, const MCSymbol *KeySym) const { |
Reid Kleckner | 7c4059e | 2014-09-04 17:42:03 +0000 | [diff] [blame] | 1218 | return getContext().getAssociativeCOFFSection( |
Reid Kleckner | 97837b7 | 2016-05-02 23:22:18 +0000 | [diff] [blame] | 1219 | cast<MCSectionCOFF>(StaticCtorSection), KeySym, 0); |
Reid Kleckner | fceb76f | 2014-05-16 20:39:27 +0000 | [diff] [blame] | 1220 | } |
| 1221 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 1222 | MCSection *TargetLoweringObjectFileCOFF::getStaticDtorSection( |
Rafael Espindola | 0766ae0 | 2014-06-06 19:26:12 +0000 | [diff] [blame] | 1223 | unsigned Priority, const MCSymbol *KeySym) const { |
Reid Kleckner | 7c4059e | 2014-09-04 17:42:03 +0000 | [diff] [blame] | 1224 | return getContext().getAssociativeCOFFSection( |
Reid Kleckner | 97837b7 | 2016-05-02 23:22:18 +0000 | [diff] [blame] | 1225 | cast<MCSectionCOFF>(StaticDtorSection), KeySym, 0); |
Reid Kleckner | fceb76f | 2014-05-16 20:39:27 +0000 | [diff] [blame] | 1226 | } |
Peter Collingbourne | aef3659 | 2015-06-29 22:04:09 +0000 | [diff] [blame] | 1227 | |
| 1228 | void TargetLoweringObjectFileCOFF::emitLinkerFlagsForGlobal( |
Eric Christopher | 4367c7f | 2016-09-16 07:33:15 +0000 | [diff] [blame] | 1229 | raw_ostream &OS, const GlobalValue *GV) const { |
Bob Haarman | dd4ebc1 | 2017-02-02 23:00:49 +0000 | [diff] [blame] | 1230 | emitLinkerFlagsForGlobalCOFF(OS, GV, getTargetTriple(), getMangler()); |
Peter Collingbourne | aef3659 | 2015-06-29 22:04:09 +0000 | [diff] [blame] | 1231 | } |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 1232 | |
| 1233 | //===----------------------------------------------------------------------===// |
| 1234 | // Wasm |
| 1235 | //===----------------------------------------------------------------------===// |
| 1236 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1237 | static const Comdat *getWasmComdat(const GlobalValue *GV) { |
| 1238 | const Comdat *C = GV->getComdat(); |
| 1239 | if (!C) |
| 1240 | return nullptr; |
| 1241 | |
| 1242 | if (C->getSelectionKind() != Comdat::Any) |
| 1243 | report_fatal_error("Wasm COMDATs only support SelectionKind::Any, '" + |
| 1244 | C->getName() + "' cannot be lowered."); |
| 1245 | |
| 1246 | return C; |
| 1247 | } |
| 1248 | |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 1249 | MCSection *TargetLoweringObjectFileWasm::getExplicitSectionGlobal( |
| 1250 | const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { |
| 1251 | llvm_unreachable("getExplicitSectionGlobal not yet implemented"); |
| 1252 | return nullptr; |
| 1253 | } |
| 1254 | |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1255 | static MCSectionWasm * |
| 1256 | selectWasmSectionForGlobal(MCContext &Ctx, const GlobalObject *GO, |
| 1257 | SectionKind Kind, Mangler &Mang, |
| 1258 | const TargetMachine &TM, bool EmitUniqueSection, |
| 1259 | unsigned Flags, unsigned *NextUniqueID) { |
| 1260 | StringRef Group = ""; |
| 1261 | if (getWasmComdat(GO)) |
| 1262 | llvm_unreachable("comdat not yet supported for wasm"); |
| 1263 | |
| 1264 | bool UniqueSectionNames = TM.getUniqueSectionNames(); |
| 1265 | SmallString<128> Name = getSectionPrefixForGlobal(Kind); |
| 1266 | |
| 1267 | if (const auto *F = dyn_cast<Function>(GO)) { |
| 1268 | const auto &OptionalPrefix = F->getSectionPrefix(); |
| 1269 | if (OptionalPrefix) |
| 1270 | Name += *OptionalPrefix; |
| 1271 | } |
| 1272 | |
| 1273 | if (EmitUniqueSection && UniqueSectionNames) { |
| 1274 | Name.push_back('.'); |
| 1275 | TM.getNameWithPrefix(Name, GO, Mang, true); |
| 1276 | } |
| 1277 | unsigned UniqueID = MCContext::GenericSectionID; |
| 1278 | if (EmitUniqueSection && !UniqueSectionNames) { |
| 1279 | UniqueID = *NextUniqueID; |
| 1280 | (*NextUniqueID)++; |
| 1281 | } |
| 1282 | return Ctx.getWasmSection(Name, /*Type=*/0, Flags, |
| 1283 | Group, UniqueID); |
| 1284 | } |
| 1285 | |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 1286 | MCSection *TargetLoweringObjectFileWasm::SelectSectionForGlobal( |
| 1287 | const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1288 | |
| 1289 | if (Kind.isCommon()) |
| 1290 | report_fatal_error("mergable sections not supported yet on wasm"); |
| 1291 | |
| 1292 | // If we have -ffunction-section or -fdata-section then we should emit the |
| 1293 | // global value to a uniqued section specifically for it. |
| 1294 | bool EmitUniqueSection = false; |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 1295 | if (Kind.isText()) |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1296 | EmitUniqueSection = TM.getFunctionSections(); |
| 1297 | else |
| 1298 | EmitUniqueSection = TM.getDataSections(); |
| 1299 | EmitUniqueSection |= GO->hasComdat(); |
| 1300 | |
| 1301 | return selectWasmSectionForGlobal(getContext(), GO, Kind, getMangler(), TM, |
| 1302 | EmitUniqueSection, /*Flags=*/0, |
| 1303 | &NextUniqueID); |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 1304 | } |
| 1305 | |
| 1306 | bool TargetLoweringObjectFileWasm::shouldPutJumpTableInFunctionSection( |
| 1307 | bool UsesLabelDifference, const Function &F) const { |
| 1308 | // We can always create relative relocations, so use another section |
| 1309 | // that can be marked non-executable. |
| 1310 | return false; |
| 1311 | } |
| 1312 | |
| 1313 | const MCExpr *TargetLoweringObjectFileWasm::lowerRelativeReference( |
| 1314 | const GlobalValue *LHS, const GlobalValue *RHS, |
| 1315 | const TargetMachine &TM) const { |
| 1316 | // We may only use a PLT-relative relocation to refer to unnamed_addr |
| 1317 | // functions. |
| 1318 | if (!LHS->hasGlobalUnnamedAddr() || !LHS->getValueType()->isFunctionTy()) |
| 1319 | return nullptr; |
| 1320 | |
| 1321 | // Basic sanity checks. |
| 1322 | if (LHS->getType()->getPointerAddressSpace() != 0 || |
| 1323 | RHS->getType()->getPointerAddressSpace() != 0 || LHS->isThreadLocal() || |
| 1324 | RHS->isThreadLocal()) |
| 1325 | return nullptr; |
| 1326 | |
| 1327 | return MCBinaryExpr::createSub( |
| 1328 | MCSymbolRefExpr::create(TM.getSymbol(LHS), MCSymbolRefExpr::VK_None, |
| 1329 | getContext()), |
| 1330 | MCSymbolRefExpr::create(TM.getSymbol(RHS), getContext()), getContext()); |
| 1331 | } |
| 1332 | |
| 1333 | void |
| 1334 | TargetLoweringObjectFileWasm::InitializeWasm() { |
| 1335 | // TODO: Initialize StaticCtorSection and StaticDtorSection. |
| 1336 | } |