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