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