Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 1 | //===- llvm/CodeGen/TargetLoweringObjectFileImpl.cpp - Object File Info ---===// |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements classes used to handle lowerings specific to common |
| 11 | // object file formats. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/SmallString.h" |
Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallVector.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/StringExtras.h" |
Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/StringRef.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/Triple.h" |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 21 | #include "llvm/BinaryFormat/COFF.h" |
| 22 | #include "llvm/BinaryFormat/Dwarf.h" |
| 23 | #include "llvm/BinaryFormat/ELF.h" |
| 24 | #include "llvm/BinaryFormat/MachO.h" |
Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineModuleInfoImpls.h" |
Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 27 | #include "llvm/IR/Comdat.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 28 | #include "llvm/IR/Constants.h" |
| 29 | #include "llvm/IR/DataLayout.h" |
| 30 | #include "llvm/IR/DerivedTypes.h" |
| 31 | #include "llvm/IR/Function.h" |
Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 32 | #include "llvm/IR/GlobalAlias.h" |
| 33 | #include "llvm/IR/GlobalObject.h" |
| 34 | #include "llvm/IR/GlobalValue.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 35 | #include "llvm/IR/GlobalVariable.h" |
Rafael Espindola | 894843c | 2014-01-07 21:19:40 +0000 | [diff] [blame] | 36 | #include "llvm/IR/Mangler.h" |
Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 37 | #include "llvm/IR/Metadata.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 38 | #include "llvm/IR/Module.h" |
Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 39 | #include "llvm/IR/Type.h" |
Peter Collingbourne | 94d7786 | 2015-11-03 23:40:03 +0000 | [diff] [blame] | 40 | #include "llvm/MC/MCAsmInfo.h" |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 41 | #include "llvm/MC/MCContext.h" |
| 42 | #include "llvm/MC/MCExpr.h" |
Chris Lattner | 87cffa9 | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 43 | #include "llvm/MC/MCSectionCOFF.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 44 | #include "llvm/MC/MCSectionELF.h" |
| 45 | #include "llvm/MC/MCSectionMachO.h" |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 46 | #include "llvm/MC/MCSectionWasm.h" |
Rafael Espindola | a83b177 | 2011-04-16 03:51:21 +0000 | [diff] [blame] | 47 | #include "llvm/MC/MCStreamer.h" |
Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 48 | #include "llvm/MC/MCSymbol.h" |
Rafael Espindola | a869576 | 2015-06-02 00:25:12 +0000 | [diff] [blame] | 49 | #include "llvm/MC/MCSymbolELF.h" |
Bruno Cardoso Lopes | 618c67a | 2015-03-06 13:49:05 +0000 | [diff] [blame] | 50 | #include "llvm/MC/MCValue.h" |
Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 51 | #include "llvm/MC/SectionKind.h" |
Xinliang David Li | 5f04f92 | 2016-01-14 18:09:45 +0000 | [diff] [blame] | 52 | #include "llvm/ProfileData/InstrProf.h" |
Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 53 | #include "llvm/Support/Casting.h" |
| 54 | #include "llvm/Support/CodeGen.h" |
Martin Storsjo | 04b6844 | 2017-11-28 08:07:18 +0000 | [diff] [blame] | 55 | #include "llvm/Support/Format.h" |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 56 | #include "llvm/Support/ErrorHandling.h" |
| 57 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | 442f784 | 2014-03-04 10:07:28 +0000 | [diff] [blame] | 58 | #include "llvm/Target/TargetMachine.h" |
Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 59 | #include <cassert> |
| 60 | #include <string> |
| 61 | |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 62 | using namespace llvm; |
Anton Korobeynikov | 31a9212 | 2010-02-21 20:28:15 +0000 | [diff] [blame] | 63 | using namespace dwarf; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 64 | |
Peter Collingbourne | 89061b2 | 2017-06-12 20:10:48 +0000 | [diff] [blame] | 65 | static void GetObjCImageInfo(Module &M, unsigned &Version, unsigned &Flags, |
Saleem Abdulrasool | 4c47434 | 2017-06-05 21:26:39 +0000 | [diff] [blame] | 66 | StringRef &Section) { |
Peter Collingbourne | 89061b2 | 2017-06-12 20:10:48 +0000 | [diff] [blame] | 67 | SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags; |
| 68 | M.getModuleFlagsMetadata(ModuleFlags); |
| 69 | |
Saleem Abdulrasool | 4c47434 | 2017-06-05 21:26:39 +0000 | [diff] [blame] | 70 | for (const auto &MFE: ModuleFlags) { |
| 71 | // Ignore flags with 'Require' behaviour. |
| 72 | if (MFE.Behavior == Module::Require) |
| 73 | continue; |
| 74 | |
| 75 | StringRef Key = MFE.Key->getString(); |
| 76 | if (Key == "Objective-C Image Info Version") { |
| 77 | Version = mdconst::extract<ConstantInt>(MFE.Val)->getZExtValue(); |
| 78 | } else if (Key == "Objective-C Garbage Collection" || |
| 79 | Key == "Objective-C GC Only" || |
| 80 | Key == "Objective-C Is Simulated" || |
| 81 | Key == "Objective-C Class Properties" || |
| 82 | Key == "Objective-C Image Swift Version") { |
| 83 | Flags |= mdconst::extract<ConstantInt>(MFE.Val)->getZExtValue(); |
| 84 | } else if (Key == "Objective-C Image Info Section") { |
| 85 | Section = cast<MDString>(MFE.Val)->getString(); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 90 | //===----------------------------------------------------------------------===// |
| 91 | // ELF |
| 92 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 93 | |
Michael J. Spencer | 7bb2767 | 2018-07-16 00:28:24 +0000 | [diff] [blame] | 94 | void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx, |
| 95 | const TargetMachine &TgtM) { |
| 96 | TargetLoweringObjectFile::Initialize(Ctx, TgtM); |
| 97 | TM = &TgtM; |
Reid Kleckner | fce7f73 | 2018-08-09 22:24:04 +0000 | [diff] [blame] | 98 | |
Lei Liu | 901a0a9 | 2018-08-13 06:06:53 +0000 | [diff] [blame] | 99 | CodeModel::Model CM = TgtM.getCodeModel(); |
Reid Kleckner | fce7f73 | 2018-08-09 22:24:04 +0000 | [diff] [blame] | 100 | |
| 101 | switch (TgtM.getTargetTriple().getArch()) { |
| 102 | case Triple::arm: |
| 103 | case Triple::armeb: |
| 104 | case Triple::thumb: |
| 105 | case Triple::thumbeb: |
| 106 | if (Ctx.getAsmInfo()->getExceptionHandlingType() == ExceptionHandling::ARM) |
| 107 | break; |
| 108 | // Fallthrough if not using EHABI |
| 109 | LLVM_FALLTHROUGH; |
| 110 | case Triple::ppc: |
| 111 | case Triple::x86: |
| 112 | PersonalityEncoding = isPositionIndependent() |
| 113 | ? dwarf::DW_EH_PE_indirect | |
| 114 | dwarf::DW_EH_PE_pcrel | |
| 115 | dwarf::DW_EH_PE_sdata4 |
| 116 | : dwarf::DW_EH_PE_absptr; |
| 117 | LSDAEncoding = isPositionIndependent() |
| 118 | ? dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4 |
| 119 | : dwarf::DW_EH_PE_absptr; |
| 120 | TTypeEncoding = isPositionIndependent() |
| 121 | ? dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | |
| 122 | dwarf::DW_EH_PE_sdata4 |
| 123 | : dwarf::DW_EH_PE_absptr; |
| 124 | break; |
| 125 | case Triple::x86_64: |
| 126 | if (isPositionIndependent()) { |
Lei Liu | 901a0a9 | 2018-08-13 06:06:53 +0000 | [diff] [blame] | 127 | PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | |
| 128 | ((CM == CodeModel::Small || CM == CodeModel::Medium) |
| 129 | ? dwarf::DW_EH_PE_sdata4 : dwarf::DW_EH_PE_sdata8); |
Reid Kleckner | fce7f73 | 2018-08-09 22:24:04 +0000 | [diff] [blame] | 130 | LSDAEncoding = dwarf::DW_EH_PE_pcrel | |
Lei Liu | 901a0a9 | 2018-08-13 06:06:53 +0000 | [diff] [blame] | 131 | (CM == CodeModel::Small |
| 132 | ? dwarf::DW_EH_PE_sdata4 : dwarf::DW_EH_PE_sdata8); |
Reid Kleckner | fce7f73 | 2018-08-09 22:24:04 +0000 | [diff] [blame] | 133 | TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | |
Lei Liu | 901a0a9 | 2018-08-13 06:06:53 +0000 | [diff] [blame] | 134 | ((CM == CodeModel::Small || CM == CodeModel::Medium) |
| 135 | ? dwarf::DW_EH_PE_sdata8 : dwarf::DW_EH_PE_sdata4); |
Reid Kleckner | fce7f73 | 2018-08-09 22:24:04 +0000 | [diff] [blame] | 136 | } else { |
| 137 | PersonalityEncoding = |
Lei Liu | 901a0a9 | 2018-08-13 06:06:53 +0000 | [diff] [blame] | 138 | (CM == CodeModel::Small || CM == CodeModel::Medium) |
| 139 | ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr; |
| 140 | LSDAEncoding = (CM == CodeModel::Small) |
| 141 | ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr; |
| 142 | TTypeEncoding = (CM == CodeModel::Small) |
| 143 | ? dwarf::DW_EH_PE_udata4 : dwarf::DW_EH_PE_absptr; |
Reid Kleckner | fce7f73 | 2018-08-09 22:24:04 +0000 | [diff] [blame] | 144 | } |
| 145 | break; |
| 146 | case Triple::hexagon: |
| 147 | PersonalityEncoding = dwarf::DW_EH_PE_absptr; |
| 148 | LSDAEncoding = dwarf::DW_EH_PE_absptr; |
| 149 | TTypeEncoding = dwarf::DW_EH_PE_absptr; |
| 150 | if (isPositionIndependent()) { |
| 151 | PersonalityEncoding |= dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel; |
| 152 | LSDAEncoding |= dwarf::DW_EH_PE_pcrel; |
| 153 | TTypeEncoding |= dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel; |
| 154 | } |
| 155 | break; |
| 156 | case Triple::aarch64: |
| 157 | case Triple::aarch64_be: |
| 158 | // The small model guarantees static code/data size < 4GB, but not where it |
| 159 | // will be in memory. Most of these could end up >2GB away so even a signed |
| 160 | // pc-relative 32-bit address is insufficient, theoretically. |
| 161 | if (isPositionIndependent()) { |
| 162 | PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | |
| 163 | dwarf::DW_EH_PE_sdata8; |
| 164 | LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8; |
| 165 | TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | |
| 166 | dwarf::DW_EH_PE_sdata8; |
| 167 | } else { |
| 168 | PersonalityEncoding = dwarf::DW_EH_PE_absptr; |
| 169 | LSDAEncoding = dwarf::DW_EH_PE_absptr; |
| 170 | TTypeEncoding = dwarf::DW_EH_PE_absptr; |
| 171 | } |
| 172 | break; |
| 173 | case Triple::lanai: |
| 174 | LSDAEncoding = dwarf::DW_EH_PE_absptr; |
| 175 | PersonalityEncoding = dwarf::DW_EH_PE_absptr; |
| 176 | TTypeEncoding = dwarf::DW_EH_PE_absptr; |
| 177 | break; |
| 178 | case Triple::mips: |
| 179 | case Triple::mipsel: |
| 180 | case Triple::mips64: |
| 181 | case Triple::mips64el: |
| 182 | // MIPS uses indirect pointer to refer personality functions and types, so |
| 183 | // that the eh_frame section can be read-only. DW.ref.personality will be |
| 184 | // generated for relocation. |
| 185 | PersonalityEncoding = dwarf::DW_EH_PE_indirect; |
| 186 | // FIXME: The N64 ABI probably ought to use DW_EH_PE_sdata8 but we can't |
| 187 | // identify N64 from just a triple. |
| 188 | TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | |
| 189 | dwarf::DW_EH_PE_sdata4; |
| 190 | // We don't support PC-relative LSDA references in GAS so we use the default |
| 191 | // DW_EH_PE_absptr for those. |
| 192 | |
| 193 | // FreeBSD must be explicit about the data size and using pcrel since it's |
| 194 | // assembler/linker won't do the automatic conversion that the Linux tools |
| 195 | // do. |
| 196 | if (TgtM.getTargetTriple().isOSFreeBSD()) { |
| 197 | PersonalityEncoding |= dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; |
| 198 | LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; |
| 199 | } |
| 200 | break; |
| 201 | case Triple::ppc64: |
| 202 | case Triple::ppc64le: |
| 203 | PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | |
| 204 | dwarf::DW_EH_PE_udata8; |
| 205 | LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8; |
| 206 | TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | |
| 207 | dwarf::DW_EH_PE_udata8; |
| 208 | break; |
| 209 | case Triple::sparcel: |
| 210 | case Triple::sparc: |
| 211 | if (isPositionIndependent()) { |
| 212 | LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; |
| 213 | PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | |
| 214 | dwarf::DW_EH_PE_sdata4; |
| 215 | TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | |
| 216 | dwarf::DW_EH_PE_sdata4; |
| 217 | } else { |
| 218 | LSDAEncoding = dwarf::DW_EH_PE_absptr; |
| 219 | PersonalityEncoding = dwarf::DW_EH_PE_absptr; |
| 220 | TTypeEncoding = dwarf::DW_EH_PE_absptr; |
| 221 | } |
| 222 | break; |
| 223 | case Triple::sparcv9: |
| 224 | LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; |
| 225 | if (isPositionIndependent()) { |
| 226 | PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | |
| 227 | dwarf::DW_EH_PE_sdata4; |
| 228 | TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | |
| 229 | dwarf::DW_EH_PE_sdata4; |
| 230 | } else { |
| 231 | PersonalityEncoding = dwarf::DW_EH_PE_absptr; |
| 232 | TTypeEncoding = dwarf::DW_EH_PE_absptr; |
| 233 | } |
| 234 | break; |
| 235 | case Triple::systemz: |
| 236 | // All currently-defined code models guarantee that 4-byte PC-relative |
| 237 | // values will be in range. |
| 238 | if (isPositionIndependent()) { |
| 239 | PersonalityEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | |
| 240 | dwarf::DW_EH_PE_sdata4; |
| 241 | LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; |
| 242 | TTypeEncoding = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | |
| 243 | dwarf::DW_EH_PE_sdata4; |
| 244 | } else { |
| 245 | PersonalityEncoding = dwarf::DW_EH_PE_absptr; |
| 246 | LSDAEncoding = dwarf::DW_EH_PE_absptr; |
| 247 | TTypeEncoding = dwarf::DW_EH_PE_absptr; |
| 248 | } |
| 249 | break; |
| 250 | default: |
| 251 | break; |
| 252 | } |
Michael J. Spencer | 7bb2767 | 2018-07-16 00:28:24 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Eric Christopher | aadbabc | 2018-04-20 19:07:57 +0000 | [diff] [blame] | 255 | void TargetLoweringObjectFileELF::emitModuleMetadata(MCStreamer &Streamer, |
| 256 | Module &M) const { |
Saleem Abdulrasool | b36fbbc | 2018-01-30 16:29:29 +0000 | [diff] [blame] | 257 | auto &C = getContext(); |
| 258 | |
| 259 | if (NamedMDNode *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) { |
| 260 | auto *S = C.getELFSection(".linker-options", ELF::SHT_LLVM_LINKER_OPTIONS, |
| 261 | ELF::SHF_EXCLUDE); |
| 262 | |
| 263 | Streamer.SwitchSection(S); |
| 264 | |
| 265 | for (const auto &Operand : LinkerOptions->operands()) { |
| 266 | if (cast<MDNode>(Operand)->getNumOperands() != 2) |
| 267 | report_fatal_error("invalid llvm.linker.options"); |
| 268 | for (const auto &Option : cast<MDNode>(Operand)->operands()) { |
| 269 | Streamer.EmitBytes(cast<MDString>(Option)->getString()); |
| 270 | Streamer.EmitIntValue(0, 1); |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | |
Saleem Abdulrasool | 4c47434 | 2017-06-05 21:26:39 +0000 | [diff] [blame] | 275 | unsigned Version = 0; |
| 276 | unsigned Flags = 0; |
| 277 | StringRef Section; |
| 278 | |
Peter Collingbourne | 89061b2 | 2017-06-12 20:10:48 +0000 | [diff] [blame] | 279 | GetObjCImageInfo(M, Version, Flags, Section); |
Michael J. Spencer | 7bb2767 | 2018-07-16 00:28:24 +0000 | [diff] [blame] | 280 | if (!Section.empty()) { |
| 281 | auto *S = C.getELFSection(Section, ELF::SHT_PROGBITS, ELF::SHF_ALLOC); |
| 282 | Streamer.SwitchSection(S); |
| 283 | Streamer.EmitLabel(C.getOrCreateSymbol(StringRef("OBJC_IMAGE_INFO"))); |
| 284 | Streamer.EmitIntValue(Version, 4); |
| 285 | Streamer.EmitIntValue(Flags, 4); |
| 286 | Streamer.AddBlankLine(); |
| 287 | } |
| 288 | |
| 289 | SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags; |
| 290 | M.getModuleFlagsMetadata(ModuleFlags); |
| 291 | |
| 292 | MDNode *CFGProfile = nullptr; |
| 293 | |
| 294 | for (const auto &MFE : ModuleFlags) { |
| 295 | StringRef Key = MFE.Key->getString(); |
| 296 | if (Key == "CG Profile") { |
| 297 | CFGProfile = cast<MDNode>(MFE.Val); |
| 298 | break; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | if (!CFGProfile) |
Saleem Abdulrasool | 4c47434 | 2017-06-05 21:26:39 +0000 | [diff] [blame] | 303 | return; |
| 304 | |
Michael J. Spencer | 7bb2767 | 2018-07-16 00:28:24 +0000 | [diff] [blame] | 305 | auto GetSym = [this](const MDOperand &MDO) -> MCSymbol * { |
| 306 | if (!MDO) |
| 307 | return nullptr; |
| 308 | auto V = cast<ValueAsMetadata>(MDO); |
| 309 | const Function *F = cast<Function>(V->getValue()); |
| 310 | return TM->getSymbol(F); |
| 311 | }; |
| 312 | |
| 313 | for (const auto &Edge : CFGProfile->operands()) { |
| 314 | MDNode *E = cast<MDNode>(Edge); |
| 315 | const MCSymbol *From = GetSym(E->getOperand(0)); |
| 316 | const MCSymbol *To = GetSym(E->getOperand(1)); |
| 317 | // Skip null functions. This can happen if functions are dead stripped after |
| 318 | // the CGProfile pass has been run. |
| 319 | if (!From || !To) |
| 320 | continue; |
| 321 | uint64_t Count = cast<ConstantAsMetadata>(E->getOperand(2)) |
| 322 | ->getValue() |
| 323 | ->getUniqueInteger() |
| 324 | .getZExtValue(); |
| 325 | Streamer.emitCGProfileEntry( |
| 326 | MCSymbolRefExpr::create(From, MCSymbolRefExpr::VK_None, C), |
| 327 | MCSymbolRefExpr::create(To, MCSymbolRefExpr::VK_None, C), Count); |
| 328 | } |
Saleem Abdulrasool | 4c47434 | 2017-06-05 21:26:39 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Rafael Espindola | daeafb4 | 2014-02-19 17:23:20 +0000 | [diff] [blame] | 331 | MCSymbol *TargetLoweringObjectFileELF::getCFIPersonalitySymbol( |
Eric Christopher | 4367c7f | 2016-09-16 07:33:15 +0000 | [diff] [blame] | 332 | const GlobalValue *GV, const TargetMachine &TM, |
Rafael Espindola | daeafb4 | 2014-02-19 17:23:20 +0000 | [diff] [blame] | 333 | MachineModuleInfo *MMI) const { |
Rafael Espindola | ce83fc3 | 2011-04-27 23:17:57 +0000 | [diff] [blame] | 334 | unsigned Encoding = getPersonalityEncoding(); |
Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 335 | if ((Encoding & 0x80) == DW_EH_PE_indirect) |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 336 | return getContext().getOrCreateSymbol(StringRef("DW.ref.") + |
Tim Northover | b64fb45 | 2016-11-22 16:17:20 +0000 | [diff] [blame] | 337 | TM.getSymbol(GV)->getName()); |
Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 338 | if ((Encoding & 0x70) == DW_EH_PE_absptr) |
Tim Northover | b64fb45 | 2016-11-22 16:17:20 +0000 | [diff] [blame] | 339 | return TM.getSymbol(GV); |
Logan Chien | c002981 | 2014-05-30 16:48:56 +0000 | [diff] [blame] | 340 | report_fatal_error("We do not support this DWARF encoding yet!"); |
Rafael Espindola | a83b177 | 2011-04-16 03:51:21 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Mehdi Amini | 5c0fa58 | 2015-07-16 06:04:17 +0000 | [diff] [blame] | 343 | void TargetLoweringObjectFileELF::emitPersonalityValue( |
| 344 | MCStreamer &Streamer, const DataLayout &DL, const MCSymbol *Sym) const { |
Rafael Espindola | 51d2d7a | 2011-06-13 03:09:13 +0000 | [diff] [blame] | 345 | SmallString<64> NameData("DW.ref."); |
| 346 | NameData += Sym->getName(); |
Rafael Espindola | a869576 | 2015-06-02 00:25:12 +0000 | [diff] [blame] | 347 | MCSymbolELF *Label = |
| 348 | cast<MCSymbolELF>(getContext().getOrCreateSymbol(NameData)); |
Rafael Espindola | 3989776 | 2011-04-27 21:29:52 +0000 | [diff] [blame] | 349 | Streamer.EmitSymbolAttribute(Label, MCSA_Hidden); |
| 350 | Streamer.EmitSymbolAttribute(Label, MCSA_Weak); |
Rafael Espindola | 3989776 | 2011-04-27 21:29:52 +0000 | [diff] [blame] | 351 | unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE | ELF::SHF_GROUP; |
Eric Christopher | 36e601c | 2016-07-01 06:07:38 +0000 | [diff] [blame] | 352 | MCSection *Sec = getContext().getELFNamedSection(".data", Label->getName(), |
| 353 | ELF::SHT_PROGBITS, Flags, 0); |
Mehdi Amini | 5c0fa58 | 2015-07-16 06:04:17 +0000 | [diff] [blame] | 354 | unsigned Size = DL.getPointerSize(); |
Rafael Espindola | 3989776 | 2011-04-27 21:29:52 +0000 | [diff] [blame] | 355 | Streamer.SwitchSection(Sec); |
Fangrui Song | e7353446 | 2017-11-15 06:17:32 +0000 | [diff] [blame] | 356 | Streamer.EmitValueToAlignment(DL.getPointerABIAlignment(0)); |
Rafael Espindola | 3989776 | 2011-04-27 21:29:52 +0000 | [diff] [blame] | 357 | Streamer.EmitSymbolAttribute(Label, MCSA_ELF_TypeObject); |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 358 | const MCExpr *E = MCConstantExpr::create(Size, getContext()); |
Rafael Espindola | a869576 | 2015-06-02 00:25:12 +0000 | [diff] [blame] | 359 | Streamer.emitELFSize(Label, E); |
Rafael Espindola | 3989776 | 2011-04-27 21:29:52 +0000 | [diff] [blame] | 360 | Streamer.EmitLabel(Label); |
Rafael Espindola | a83b177 | 2011-04-16 03:51:21 +0000 | [diff] [blame] | 361 | |
Rafael Espindola | 3989776 | 2011-04-27 21:29:52 +0000 | [diff] [blame] | 362 | Streamer.EmitSymbolValue(Sym, Size); |
Rafael Espindola | a83b177 | 2011-04-16 03:51:21 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Rafael Espindola | 15b2669 | 2014-02-09 14:50:44 +0000 | [diff] [blame] | 365 | const MCExpr *TargetLoweringObjectFileELF::getTTypeGlobalReference( |
Eric Christopher | 4367c7f | 2016-09-16 07:33:15 +0000 | [diff] [blame] | 366 | const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM, |
| 367 | MachineModuleInfo *MMI, MCStreamer &Streamer) const { |
Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 368 | if (Encoding & DW_EH_PE_indirect) { |
Anton Korobeynikov | e42af36 | 2012-11-14 01:47:00 +0000 | [diff] [blame] | 369 | MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>(); |
| 370 | |
Eric Christopher | 4367c7f | 2016-09-16 07:33:15 +0000 | [diff] [blame] | 371 | MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, ".DW.stub", TM); |
Anton Korobeynikov | e42af36 | 2012-11-14 01:47:00 +0000 | [diff] [blame] | 372 | |
| 373 | // Add information about the stub reference to ELFMMI so that the stub |
| 374 | // gets emitted by the asmprinter. |
Anton Korobeynikov | e42af36 | 2012-11-14 01:47:00 +0000 | [diff] [blame] | 375 | MachineModuleInfoImpl::StubValueTy &StubSym = ELFMMI.getGVStubEntry(SSym); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 376 | if (!StubSym.getPointer()) { |
Tim Northover | b64fb45 | 2016-11-22 16:17:20 +0000 | [diff] [blame] | 377 | MCSymbol *Sym = TM.getSymbol(GV); |
Anton Korobeynikov | e42af36 | 2012-11-14 01:47:00 +0000 | [diff] [blame] | 378 | StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage()); |
| 379 | } |
| 380 | |
| 381 | return TargetLoweringObjectFile:: |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 382 | getTTypeReference(MCSymbolRefExpr::create(SSym, getContext()), |
Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 383 | Encoding & ~DW_EH_PE_indirect, Streamer); |
Anton Korobeynikov | e42af36 | 2012-11-14 01:47:00 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Eric Christopher | 4367c7f | 2016-09-16 07:33:15 +0000 | [diff] [blame] | 386 | return TargetLoweringObjectFile::getTTypeGlobalReference(GV, Encoding, TM, |
| 387 | MMI, Streamer); |
Anton Korobeynikov | e42af36 | 2012-11-14 01:47:00 +0000 | [diff] [blame] | 388 | } |
| 389 | |
Sam Clegg | 12fd3da | 2017-10-20 21:28:38 +0000 | [diff] [blame] | 390 | static SectionKind getELFKindForNamedSection(StringRef Name, SectionKind K) { |
Eric Christopher | 1f5eb86 | 2018-05-16 20:34:00 +0000 | [diff] [blame] | 391 | // N.B.: The defaults used in here are not the same ones used in MC. |
Rafael Espindola | 0d018b1 | 2011-05-24 03:10:31 +0000 | [diff] [blame] | 392 | // We follow gcc, MC follows gas. For example, given ".section .eh_frame", |
| 393 | // both gas and MC will produce a section with no flags. Given |
Bill Wendling | d163405 | 2012-07-19 00:04:14 +0000 | [diff] [blame] | 394 | // section(".eh_frame") gcc will produce: |
| 395 | // |
| 396 | // .section .eh_frame,"a",@progbits |
Xinliang David Li | 4a5ddf8 | 2017-04-14 17:48:40 +0000 | [diff] [blame] | 397 | |
Vedant Kumar | 1a6a2b6 | 2017-04-15 00:09:57 +0000 | [diff] [blame] | 398 | if (Name == getInstrProfSectionName(IPSK_covmap, Triple::ELF, |
| 399 | /*AddSegmentInfo=*/false)) |
Xinliang David Li | 5f04f92 | 2016-01-14 18:09:45 +0000 | [diff] [blame] | 400 | return SectionKind::getMetadata(); |
| 401 | |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 402 | if (Name.empty() || Name[0] != '.') return K; |
| 403 | |
Eric Christopher | f31e91e | 2018-05-18 02:39:57 +0000 | [diff] [blame] | 404 | // Default implementation based on some magic section names. |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 405 | if (Name == ".bss" || |
| 406 | Name.startswith(".bss.") || |
| 407 | Name.startswith(".gnu.linkonce.b.") || |
| 408 | Name.startswith(".llvm.linkonce.b.") || |
| 409 | Name == ".sbss" || |
| 410 | Name.startswith(".sbss.") || |
| 411 | Name.startswith(".gnu.linkonce.sb.") || |
| 412 | Name.startswith(".llvm.linkonce.sb.")) |
| 413 | return SectionKind::getBSS(); |
| 414 | |
| 415 | if (Name == ".tdata" || |
| 416 | Name.startswith(".tdata.") || |
| 417 | Name.startswith(".gnu.linkonce.td.") || |
| 418 | Name.startswith(".llvm.linkonce.td.")) |
| 419 | return SectionKind::getThreadData(); |
| 420 | |
| 421 | if (Name == ".tbss" || |
| 422 | Name.startswith(".tbss.") || |
| 423 | Name.startswith(".gnu.linkonce.tb.") || |
| 424 | Name.startswith(".llvm.linkonce.tb.")) |
| 425 | return SectionKind::getThreadBSS(); |
| 426 | |
| 427 | return K; |
| 428 | } |
| 429 | |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 430 | static unsigned getELFSectionType(StringRef Name, SectionKind K) { |
Petr Hosek | 1290355 | 2016-09-20 20:21:13 +0000 | [diff] [blame] | 431 | // Use SHT_NOTE for section whose name starts with ".note" to allow |
| 432 | // emitting ELF notes from C variable declaration. |
| 433 | // See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77609 |
| 434 | if (Name.startswith(".note")) |
| 435 | return ELF::SHT_NOTE; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 436 | |
| 437 | if (Name == ".init_array") |
Rafael Espindola | aea4958 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 438 | return ELF::SHT_INIT_ARRAY; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 439 | |
| 440 | if (Name == ".fini_array") |
Rafael Espindola | aea4958 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 441 | return ELF::SHT_FINI_ARRAY; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 442 | |
| 443 | if (Name == ".preinit_array") |
Rafael Espindola | aea4958 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 444 | return ELF::SHT_PREINIT_ARRAY; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 445 | |
| 446 | if (K.isBSS() || K.isThreadBSS()) |
Rafael Espindola | aea4958 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 447 | return ELF::SHT_NOBITS; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 448 | |
Rafael Espindola | aea4958 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 449 | return ELF::SHT_PROGBITS; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Rafael Espindola | 8bc9ccc | 2015-02-25 00:52:15 +0000 | [diff] [blame] | 452 | static unsigned getELFSectionFlags(SectionKind K) { |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 453 | unsigned Flags = 0; |
| 454 | |
| 455 | if (!K.isMetadata()) |
Rafael Espindola | 0e7e34e | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 456 | Flags |= ELF::SHF_ALLOC; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 457 | |
| 458 | if (K.isText()) |
Rafael Espindola | 0e7e34e | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 459 | Flags |= ELF::SHF_EXECINSTR; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 460 | |
Prakhar Bahuguna | 52a7dd7 | 2016-12-15 07:59:08 +0000 | [diff] [blame] | 461 | if (K.isExecuteOnly()) |
| 462 | Flags |= ELF::SHF_ARM_PURECODE; |
| 463 | |
Rafael Espindola | c85e0d8 | 2011-06-07 23:26:45 +0000 | [diff] [blame] | 464 | if (K.isWriteable()) |
Rafael Espindola | 0e7e34e | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 465 | Flags |= ELF::SHF_WRITE; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 466 | |
| 467 | if (K.isThreadLocal()) |
Rafael Espindola | 0e7e34e | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 468 | Flags |= ELF::SHF_TLS; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 469 | |
Rafael Espindola | 8bc9ccc | 2015-02-25 00:52:15 +0000 | [diff] [blame] | 470 | if (K.isMergeableCString() || K.isMergeableConst()) |
Rafael Espindola | 0e7e34e | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 471 | Flags |= ELF::SHF_MERGE; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 472 | |
| 473 | if (K.isMergeableCString()) |
Rafael Espindola | 0e7e34e | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 474 | Flags |= ELF::SHF_STRINGS; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 475 | |
| 476 | return Flags; |
| 477 | } |
| 478 | |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 479 | static const Comdat *getELFComdat(const GlobalValue *GV) { |
| 480 | const Comdat *C = GV->getComdat(); |
| 481 | if (!C) |
| 482 | return nullptr; |
| 483 | |
| 484 | if (C->getSelectionKind() != Comdat::Any) |
| 485 | report_fatal_error("ELF COMDATs only support SelectionKind::Any, '" + |
| 486 | C->getName() + "' cannot be lowered."); |
| 487 | |
| 488 | return C; |
| 489 | } |
| 490 | |
Evgeniy Stepanov | 51c962f72 | 2017-03-17 22:17:24 +0000 | [diff] [blame] | 491 | static const MCSymbolELF *getAssociatedSymbol(const GlobalObject *GO, |
| 492 | const TargetMachine &TM) { |
| 493 | MDNode *MD = GO->getMetadata(LLVMContext::MD_associated); |
| 494 | if (!MD) |
| 495 | return nullptr; |
| 496 | |
Evgeniy Stepanov | f7e8acf | 2017-05-08 23:46:20 +0000 | [diff] [blame] | 497 | const MDOperand &Op = MD->getOperand(0); |
| 498 | if (!Op.get()) |
| 499 | return nullptr; |
| 500 | |
| 501 | auto *VM = dyn_cast<ValueAsMetadata>(Op); |
Evgeniy Stepanov | 51c962f72 | 2017-03-17 22:17:24 +0000 | [diff] [blame] | 502 | if (!VM) |
| 503 | report_fatal_error("MD_associated operand is not ValueAsMetadata"); |
| 504 | |
| 505 | GlobalObject *OtherGO = dyn_cast<GlobalObject>(VM->getValue()); |
| 506 | return OtherGO ? dyn_cast<MCSymbolELF>(TM.getSymbol(OtherGO)) : nullptr; |
| 507 | } |
| 508 | |
Nick Desaulniers | 47bab69 | 2018-10-12 16:35:44 +0000 | [diff] [blame] | 509 | static unsigned getEntrySizeForKind(SectionKind Kind) { |
| 510 | if (Kind.isMergeable1ByteCString()) |
| 511 | return 1; |
| 512 | else if (Kind.isMergeable2ByteCString()) |
| 513 | return 2; |
| 514 | else if (Kind.isMergeable4ByteCString()) |
| 515 | return 4; |
| 516 | else if (Kind.isMergeableConst4()) |
| 517 | return 4; |
| 518 | else if (Kind.isMergeableConst8()) |
| 519 | return 8; |
| 520 | else if (Kind.isMergeableConst16()) |
| 521 | return 16; |
| 522 | else if (Kind.isMergeableConst32()) |
| 523 | return 32; |
| 524 | else { |
| 525 | // We shouldn't have mergeable C strings or mergeable constants that we |
| 526 | // didn't handle above. |
| 527 | assert(!Kind.isMergeableCString() && "unknown string width"); |
| 528 | assert(!Kind.isMergeableConst() && "unknown data width"); |
| 529 | return 0; |
| 530 | } |
| 531 | } |
| 532 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 533 | MCSection *TargetLoweringObjectFileELF::getExplicitSectionGlobal( |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 534 | const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { |
| 535 | StringRef SectionName = GO->getSection(); |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 536 | |
Javed Absar | b16d146 | 2017-06-05 10:09:13 +0000 | [diff] [blame] | 537 | // Check if '#pragma clang section' name is applicable. |
| 538 | // Note that pragma directive overrides -ffunction-section, -fdata-section |
| 539 | // and so section name is exactly as user specified and not uniqued. |
| 540 | const GlobalVariable *GV = dyn_cast<GlobalVariable>(GO); |
| 541 | if (GV && GV->hasImplicitSection()) { |
| 542 | auto Attrs = GV->getAttributes(); |
| 543 | if (Attrs.hasAttribute("bss-section") && Kind.isBSS()) { |
| 544 | SectionName = Attrs.getAttribute("bss-section").getValueAsString(); |
| 545 | } else if (Attrs.hasAttribute("rodata-section") && Kind.isReadOnly()) { |
| 546 | SectionName = Attrs.getAttribute("rodata-section").getValueAsString(); |
| 547 | } else if (Attrs.hasAttribute("data-section") && Kind.isData()) { |
| 548 | SectionName = Attrs.getAttribute("data-section").getValueAsString(); |
| 549 | } |
| 550 | } |
| 551 | const Function *F = dyn_cast<Function>(GO); |
| 552 | if (F && F->hasFnAttribute("implicit-section-name")) { |
| 553 | SectionName = F->getFnAttribute("implicit-section-name").getValueAsString(); |
| 554 | } |
| 555 | |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 556 | // Infer section flags from the section name if we can. |
| 557 | Kind = getELFKindForNamedSection(SectionName, Kind); |
| 558 | |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 559 | StringRef Group = ""; |
Rafael Espindola | 8bc9ccc | 2015-02-25 00:52:15 +0000 | [diff] [blame] | 560 | unsigned Flags = getELFSectionFlags(Kind); |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 561 | if (const Comdat *C = getELFComdat(GO)) { |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 562 | Group = C->getName(); |
| 563 | Flags |= ELF::SHF_GROUP; |
| 564 | } |
Evgeniy Stepanov | 51c962f72 | 2017-03-17 22:17:24 +0000 | [diff] [blame] | 565 | |
| 566 | // A section can have at most one associated section. Put each global with |
| 567 | // MD_associated in a unique section. |
| 568 | unsigned UniqueID = MCContext::GenericSectionID; |
| 569 | const MCSymbolELF *AssociatedSymbol = getAssociatedSymbol(GO, TM); |
| 570 | if (AssociatedSymbol) { |
| 571 | UniqueID = NextUniqueID++; |
| 572 | Flags |= ELF::SHF_LINK_ORDER; |
| 573 | } |
| 574 | |
| 575 | MCSectionELF *Section = getContext().getELFSection( |
| 576 | SectionName, getELFSectionType(SectionName, Kind), Flags, |
Nick Desaulniers | 47bab69 | 2018-10-12 16:35:44 +0000 | [diff] [blame] | 577 | getEntrySizeForKind(Kind), Group, UniqueID, AssociatedSymbol); |
Evgeniy Stepanov | 51c962f72 | 2017-03-17 22:17:24 +0000 | [diff] [blame] | 578 | // Make sure that we did not get some other section with incompatible sh_link. |
| 579 | // This should not be possible due to UniqueID code above. |
Eric Christopher | 76cfef4 | 2018-06-21 07:15:14 +0000 | [diff] [blame] | 580 | assert(Section->getAssociatedSymbol() == AssociatedSymbol && |
| 581 | "Associated symbol mismatch between sections"); |
Evgeniy Stepanov | 51c962f72 | 2017-03-17 22:17:24 +0000 | [diff] [blame] | 582 | return Section; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 583 | } |
| 584 | |
Rafael Espindola | 25d2c20 | 2015-02-11 14:44:17 +0000 | [diff] [blame] | 585 | /// Return the section prefix name used by options FunctionsSections and |
| 586 | /// DataSections. |
David Majnemer | 102ff69 | 2014-06-24 16:01:53 +0000 | [diff] [blame] | 587 | static StringRef getSectionPrefixForGlobal(SectionKind Kind) { |
Rafael Espindola | 25d2c20 | 2015-02-11 14:44:17 +0000 | [diff] [blame] | 588 | if (Kind.isText()) |
Rafael Espindola | 68fa249 | 2015-02-17 20:48:01 +0000 | [diff] [blame] | 589 | return ".text"; |
Rafael Espindola | 25d2c20 | 2015-02-11 14:44:17 +0000 | [diff] [blame] | 590 | if (Kind.isReadOnly()) |
Rafael Espindola | 68fa249 | 2015-02-17 20:48:01 +0000 | [diff] [blame] | 591 | return ".rodata"; |
Rafael Espindola | 25d2c20 | 2015-02-11 14:44:17 +0000 | [diff] [blame] | 592 | if (Kind.isBSS()) |
Rafael Espindola | 68fa249 | 2015-02-17 20:48:01 +0000 | [diff] [blame] | 593 | return ".bss"; |
Rafael Espindola | 25d2c20 | 2015-02-11 14:44:17 +0000 | [diff] [blame] | 594 | if (Kind.isThreadData()) |
Rafael Espindola | 68fa249 | 2015-02-17 20:48:01 +0000 | [diff] [blame] | 595 | return ".tdata"; |
Rafael Espindola | 25d2c20 | 2015-02-11 14:44:17 +0000 | [diff] [blame] | 596 | if (Kind.isThreadBSS()) |
Rafael Espindola | 68fa249 | 2015-02-17 20:48:01 +0000 | [diff] [blame] | 597 | return ".tbss"; |
Rafael Espindola | 449711c | 2015-11-18 06:02:15 +0000 | [diff] [blame] | 598 | if (Kind.isData()) |
Rafael Espindola | 68fa249 | 2015-02-17 20:48:01 +0000 | [diff] [blame] | 599 | return ".data"; |
Chris Lattner | 5b212a3 | 2010-04-13 00:36:43 +0000 | [diff] [blame] | 600 | assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); |
Rafael Espindola | 68fa249 | 2015-02-17 20:48:01 +0000 | [diff] [blame] | 601 | return ".data.rel.ro"; |
Chris Lattner | 5b212a3 | 2010-04-13 00:36:43 +0000 | [diff] [blame] | 602 | } |
| 603 | |
Evgeniy Stepanov | 51c962f72 | 2017-03-17 22:17:24 +0000 | [diff] [blame] | 604 | static MCSectionELF *selectELFSectionForGlobal( |
| 605 | MCContext &Ctx, const GlobalObject *GO, SectionKind Kind, Mangler &Mang, |
| 606 | const TargetMachine &TM, bool EmitUniqueSection, unsigned Flags, |
| 607 | unsigned *NextUniqueID, const MCSymbolELF *AssociatedSymbol) { |
Rafael Espindola | 8bc9ccc | 2015-02-25 00:52:15 +0000 | [diff] [blame] | 608 | |
Rafael Espindola | 9075f77 | 2015-02-20 23:28:28 +0000 | [diff] [blame] | 609 | StringRef Group = ""; |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 610 | if (const Comdat *C = getELFComdat(GO)) { |
Rafael Espindola | 9075f77 | 2015-02-20 23:28:28 +0000 | [diff] [blame] | 611 | Flags |= ELF::SHF_GROUP; |
| 612 | Group = C->getName(); |
| 613 | } |
| 614 | |
Eric Christopher | 7a70be6 | 2018-08-01 01:29:30 +0000 | [diff] [blame] | 615 | // Get the section entry size based on the kind. |
| 616 | unsigned EntrySize = getEntrySizeForKind(Kind); |
| 617 | |
Rafael Espindola | 8bc9ccc | 2015-02-25 00:52:15 +0000 | [diff] [blame] | 618 | SmallString<128> Name; |
Rafael Espindola | a05b3b7 | 2015-01-28 17:54:19 +0000 | [diff] [blame] | 619 | if (Kind.isMergeableCString()) { |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 620 | // We also need alignment here. |
| 621 | // FIXME: this is getting the alignment of the character, not the |
| 622 | // alignment of the global! |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 623 | unsigned Align = GO->getParent()->getDataLayout().getPreferredAlignment( |
| 624 | cast<GlobalVariable>(GO)); |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 625 | |
Rafael Espindola | ba31e27 | 2015-01-29 17:33:21 +0000 | [diff] [blame] | 626 | std::string SizeSpec = ".rodata.str" + utostr(EntrySize) + "."; |
Rafael Espindola | 8bc9ccc | 2015-02-25 00:52:15 +0000 | [diff] [blame] | 627 | Name = SizeSpec + utostr(Align); |
| 628 | } else if (Kind.isMergeableConst()) { |
| 629 | Name = ".rodata.cst"; |
| 630 | Name += utostr(EntrySize); |
| 631 | } else { |
| 632 | Name = getSectionPrefixForGlobal(Kind); |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 633 | } |
Dehao Chen | 302b69c | 2016-10-18 20:42:47 +0000 | [diff] [blame] | 634 | |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 635 | if (const auto *F = dyn_cast<Function>(GO)) { |
Dehao Chen | 302b69c | 2016-10-18 20:42:47 +0000 | [diff] [blame] | 636 | const auto &OptionalPrefix = F->getSectionPrefix(); |
| 637 | if (OptionalPrefix) |
| 638 | Name += *OptionalPrefix; |
| 639 | } |
Dehao Chen | f84b630 | 2016-02-23 03:39:24 +0000 | [diff] [blame] | 640 | |
Reid Kleckner | 97837b7 | 2016-05-02 23:22:18 +0000 | [diff] [blame] | 641 | unsigned UniqueID = MCContext::GenericSectionID; |
Eric Christopher | ad36c74 | 2018-08-01 01:03:34 +0000 | [diff] [blame] | 642 | if (EmitUniqueSection) { |
| 643 | if (TM.getUniqueSectionNames()) { |
| 644 | Name.push_back('.'); |
| 645 | TM.getNameWithPrefix(Name, GO, Mang, true /*MayAlwaysUsePrivate*/); |
| 646 | } else { |
| 647 | UniqueID = *NextUniqueID; |
| 648 | (*NextUniqueID)++; |
| 649 | } |
Rafael Espindola | 8ca44f0 | 2015-04-04 18:02:01 +0000 | [diff] [blame] | 650 | } |
Eric Christopher | ad36c74 | 2018-08-01 01:03:34 +0000 | [diff] [blame] | 651 | // Use 0 as the unique ID for execute-only text. |
Prakhar Bahuguna | 52a7dd7 | 2016-12-15 07:59:08 +0000 | [diff] [blame] | 652 | if (Kind.isExecuteOnly()) |
| 653 | UniqueID = 0; |
Rafael Espindola | 4491d0d | 2015-02-26 23:55:11 +0000 | [diff] [blame] | 654 | return Ctx.getELFSection(Name, getELFSectionType(Name, Kind), Flags, |
Evgeniy Stepanov | 51c962f72 | 2017-03-17 22:17:24 +0000 | [diff] [blame] | 655 | EntrySize, Group, UniqueID, AssociatedSymbol); |
Rafael Espindola | 4491d0d | 2015-02-26 23:55:11 +0000 | [diff] [blame] | 656 | } |
| 657 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 658 | MCSection *TargetLoweringObjectFileELF::SelectSectionForGlobal( |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 659 | const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { |
Rafael Espindola | 4491d0d | 2015-02-26 23:55:11 +0000 | [diff] [blame] | 660 | unsigned Flags = getELFSectionFlags(Kind); |
| 661 | |
| 662 | // If we have -ffunction-section or -fdata-section then we should emit the |
| 663 | // global value to a uniqued section specifically for it. |
| 664 | bool EmitUniqueSection = false; |
| 665 | if (!(Flags & ELF::SHF_MERGE) && !Kind.isCommon()) { |
| 666 | if (Kind.isText()) |
| 667 | EmitUniqueSection = TM.getFunctionSections(); |
| 668 | else |
| 669 | EmitUniqueSection = TM.getDataSections(); |
| 670 | } |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 671 | EmitUniqueSection |= GO->hasComdat(); |
Rafael Espindola | 4491d0d | 2015-02-26 23:55:11 +0000 | [diff] [blame] | 672 | |
Evgeniy Stepanov | 51c962f72 | 2017-03-17 22:17:24 +0000 | [diff] [blame] | 673 | const MCSymbolELF *AssociatedSymbol = getAssociatedSymbol(GO, TM); |
| 674 | if (AssociatedSymbol) { |
| 675 | EmitUniqueSection = true; |
| 676 | Flags |= ELF::SHF_LINK_ORDER; |
| 677 | } |
| 678 | |
| 679 | MCSectionELF *Section = selectELFSectionForGlobal( |
| 680 | getContext(), GO, Kind, getMangler(), TM, EmitUniqueSection, Flags, |
| 681 | &NextUniqueID, AssociatedSymbol); |
| 682 | assert(Section->getAssociatedSymbol() == AssociatedSymbol); |
| 683 | return Section; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 684 | } |
| 685 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 686 | MCSection *TargetLoweringObjectFileELF::getSectionForJumpTable( |
Eric Christopher | 4367c7f | 2016-09-16 07:33:15 +0000 | [diff] [blame] | 687 | const Function &F, const TargetMachine &TM) const { |
Rafael Espindola | 29786d4 | 2015-02-12 17:16:46 +0000 | [diff] [blame] | 688 | // If the function can be removed, produce a unique section so that |
| 689 | // the table doesn't prevent the removal. |
| 690 | const Comdat *C = F.getComdat(); |
| 691 | bool EmitUniqueSection = TM.getFunctionSections() || C; |
| 692 | if (!EmitUniqueSection) |
| 693 | return ReadOnlySection; |
| 694 | |
Rafael Espindola | 4491d0d | 2015-02-26 23:55:11 +0000 | [diff] [blame] | 695 | return selectELFSectionForGlobal(getContext(), &F, SectionKind::getReadOnly(), |
Evgeniy Stepanov | 51c962f72 | 2017-03-17 22:17:24 +0000 | [diff] [blame] | 696 | getMangler(), TM, EmitUniqueSection, |
| 697 | ELF::SHF_ALLOC, &NextUniqueID, |
| 698 | /* AssociatedSymbol */ nullptr); |
Rafael Espindola | 29786d4 | 2015-02-12 17:16:46 +0000 | [diff] [blame] | 699 | } |
| 700 | |
Rafael Espindola | df19519 | 2015-02-17 23:34:51 +0000 | [diff] [blame] | 701 | bool TargetLoweringObjectFileELF::shouldPutJumpTableInFunctionSection( |
| 702 | bool UsesLabelDifference, const Function &F) const { |
| 703 | // We can always create relative relocations, so use another section |
| 704 | // that can be marked non-executable. |
| 705 | return false; |
| 706 | } |
| 707 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 708 | /// Given a mergeable constant with the specified size and relocation |
| 709 | /// information, return a section that it should be placed in. |
Mehdi Amini | 5c0fa58 | 2015-07-16 06:04:17 +0000 | [diff] [blame] | 710 | MCSection *TargetLoweringObjectFileELF::getSectionForConstant( |
David Majnemer | a3ea407 | 2016-02-21 01:30:30 +0000 | [diff] [blame] | 711 | const DataLayout &DL, SectionKind Kind, const Constant *C, |
| 712 | unsigned &Align) const { |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 713 | if (Kind.isMergeableConst4() && MergeableConst4Section) |
| 714 | return MergeableConst4Section; |
| 715 | if (Kind.isMergeableConst8() && MergeableConst8Section) |
| 716 | return MergeableConst8Section; |
| 717 | if (Kind.isMergeableConst16() && MergeableConst16Section) |
| 718 | return MergeableConst16Section; |
David Majnemer | 964b70d | 2016-02-22 22:23:11 +0000 | [diff] [blame] | 719 | if (Kind.isMergeableConst32() && MergeableConst32Section) |
| 720 | return MergeableConst32Section; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 721 | if (Kind.isReadOnly()) |
| 722 | return ReadOnlySection; |
| 723 | |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 724 | assert(Kind.isReadOnlyWithRel() && "Unknown section kind"); |
| 725 | return DataRelROSection; |
| 726 | } |
| 727 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 728 | static MCSectionELF *getStaticStructorSection(MCContext &Ctx, bool UseInitArray, |
| 729 | bool IsCtor, unsigned Priority, |
| 730 | const MCSymbol *KeySym) { |
Rafael Espindola | c4b4253 | 2014-09-04 23:03:58 +0000 | [diff] [blame] | 731 | std::string Name; |
| 732 | unsigned Type; |
| 733 | unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE; |
Rafael Espindola | c4b4253 | 2014-09-04 23:03:58 +0000 | [diff] [blame] | 734 | StringRef COMDAT = KeySym ? KeySym->getName() : ""; |
| 735 | |
| 736 | if (KeySym) |
| 737 | Flags |= ELF::SHF_GROUP; |
Anton Korobeynikov | 7722a2d | 2012-01-25 22:24:19 +0000 | [diff] [blame] | 738 | |
Rafael Espindola | ca3e0ee | 2012-06-19 00:48:28 +0000 | [diff] [blame] | 739 | if (UseInitArray) { |
Rafael Espindola | 7c7d7b9 | 2014-09-05 00:02:50 +0000 | [diff] [blame] | 740 | if (IsCtor) { |
| 741 | Type = ELF::SHT_INIT_ARRAY; |
| 742 | Name = ".init_array"; |
| 743 | } else { |
| 744 | Type = ELF::SHT_FINI_ARRAY; |
| 745 | Name = ".fini_array"; |
| 746 | } |
Rafael Espindola | c4b4253 | 2014-09-04 23:03:58 +0000 | [diff] [blame] | 747 | if (Priority != 65535) { |
| 748 | Name += '.'; |
| 749 | Name += utostr(Priority); |
| 750 | } |
Rafael Espindola | ca3e0ee | 2012-06-19 00:48:28 +0000 | [diff] [blame] | 751 | } else { |
Rafael Espindola | c4b4253 | 2014-09-04 23:03:58 +0000 | [diff] [blame] | 752 | // The default scheme is .ctor / .dtor, so we have to invert the priority |
| 753 | // numbering. |
Rafael Espindola | 7c7d7b9 | 2014-09-05 00:02:50 +0000 | [diff] [blame] | 754 | if (IsCtor) |
| 755 | Name = ".ctors"; |
| 756 | else |
| 757 | Name = ".dtors"; |
Martin Storsjo | 04b6844 | 2017-11-28 08:07:18 +0000 | [diff] [blame] | 758 | if (Priority != 65535) |
| 759 | raw_string_ostream(Name) << format(".%05u", 65535 - Priority); |
Rafael Espindola | c4b4253 | 2014-09-04 23:03:58 +0000 | [diff] [blame] | 760 | Type = ELF::SHT_PROGBITS; |
Rafael Espindola | ca3e0ee | 2012-06-19 00:48:28 +0000 | [diff] [blame] | 761 | } |
Rafael Espindola | c4b4253 | 2014-09-04 23:03:58 +0000 | [diff] [blame] | 762 | |
Rafael Espindola | ba31e27 | 2015-01-29 17:33:21 +0000 | [diff] [blame] | 763 | return Ctx.getELFSection(Name, Type, Flags, 0, COMDAT); |
Rafael Espindola | 7c7d7b9 | 2014-09-05 00:02:50 +0000 | [diff] [blame] | 764 | } |
| 765 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 766 | MCSection *TargetLoweringObjectFileELF::getStaticCtorSection( |
Rafael Espindola | 7c7d7b9 | 2014-09-05 00:02:50 +0000 | [diff] [blame] | 767 | unsigned Priority, const MCSymbol *KeySym) const { |
| 768 | return getStaticStructorSection(getContext(), UseInitArray, true, Priority, |
| 769 | KeySym); |
Anton Korobeynikov | 7722a2d | 2012-01-25 22:24:19 +0000 | [diff] [blame] | 770 | } |
| 771 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 772 | MCSection *TargetLoweringObjectFileELF::getStaticDtorSection( |
Rafael Espindola | 0766ae0 | 2014-06-06 19:26:12 +0000 | [diff] [blame] | 773 | unsigned Priority, const MCSymbol *KeySym) const { |
Rafael Espindola | 7c7d7b9 | 2014-09-05 00:02:50 +0000 | [diff] [blame] | 774 | return getStaticStructorSection(getContext(), UseInitArray, false, Priority, |
| 775 | KeySym); |
Rafael Espindola | ca3e0ee | 2012-06-19 00:48:28 +0000 | [diff] [blame] | 776 | } |
| 777 | |
Peter Collingbourne | 265ebd7 | 2016-04-22 20:40:10 +0000 | [diff] [blame] | 778 | const MCExpr *TargetLoweringObjectFileELF::lowerRelativeReference( |
Eric Christopher | 4367c7f | 2016-09-16 07:33:15 +0000 | [diff] [blame] | 779 | const GlobalValue *LHS, const GlobalValue *RHS, |
Peter Collingbourne | 265ebd7 | 2016-04-22 20:40:10 +0000 | [diff] [blame] | 780 | const TargetMachine &TM) const { |
| 781 | // We may only use a PLT-relative relocation to refer to unnamed_addr |
| 782 | // functions. |
Peter Collingbourne | 96efdd6 | 2016-06-14 21:01:22 +0000 | [diff] [blame] | 783 | if (!LHS->hasGlobalUnnamedAddr() || !LHS->getValueType()->isFunctionTy()) |
Peter Collingbourne | 265ebd7 | 2016-04-22 20:40:10 +0000 | [diff] [blame] | 784 | return nullptr; |
| 785 | |
| 786 | // Basic sanity checks. |
| 787 | if (LHS->getType()->getPointerAddressSpace() != 0 || |
| 788 | RHS->getType()->getPointerAddressSpace() != 0 || LHS->isThreadLocal() || |
| 789 | RHS->isThreadLocal()) |
| 790 | return nullptr; |
| 791 | |
| 792 | return MCBinaryExpr::createSub( |
Tim Northover | b64fb45 | 2016-11-22 16:17:20 +0000 | [diff] [blame] | 793 | MCSymbolRefExpr::create(TM.getSymbol(LHS), PLTRelativeVariantKind, |
Peter Collingbourne | 265ebd7 | 2016-04-22 20:40:10 +0000 | [diff] [blame] | 794 | getContext()), |
Tim Northover | b64fb45 | 2016-11-22 16:17:20 +0000 | [diff] [blame] | 795 | MCSymbolRefExpr::create(TM.getSymbol(RHS), getContext()), getContext()); |
Peter Collingbourne | 265ebd7 | 2016-04-22 20:40:10 +0000 | [diff] [blame] | 796 | } |
| 797 | |
Rafael Espindola | ca3e0ee | 2012-06-19 00:48:28 +0000 | [diff] [blame] | 798 | void |
| 799 | TargetLoweringObjectFileELF::InitializeELF(bool UseInitArray_) { |
| 800 | UseInitArray = UseInitArray_; |
Rafael Espindola | 46fa231 | 2016-08-29 12:33:42 +0000 | [diff] [blame] | 801 | MCContext &Ctx = getContext(); |
| 802 | if (!UseInitArray) { |
| 803 | StaticCtorSection = Ctx.getELFSection(".ctors", ELF::SHT_PROGBITS, |
| 804 | ELF::SHF_ALLOC | ELF::SHF_WRITE); |
Rafael Espindola | ca3e0ee | 2012-06-19 00:48:28 +0000 | [diff] [blame] | 805 | |
Rafael Espindola | 46fa231 | 2016-08-29 12:33:42 +0000 | [diff] [blame] | 806 | StaticDtorSection = Ctx.getELFSection(".dtors", ELF::SHT_PROGBITS, |
| 807 | ELF::SHF_ALLOC | ELF::SHF_WRITE); |
| 808 | return; |
| 809 | } |
| 810 | |
| 811 | StaticCtorSection = Ctx.getELFSection(".init_array", ELF::SHT_INIT_ARRAY, |
| 812 | ELF::SHF_WRITE | ELF::SHF_ALLOC); |
| 813 | StaticDtorSection = Ctx.getELFSection(".fini_array", ELF::SHT_FINI_ARRAY, |
| 814 | ELF::SHF_WRITE | ELF::SHF_ALLOC); |
Anton Korobeynikov | 7722a2d | 2012-01-25 22:24:19 +0000 | [diff] [blame] | 815 | } |
| 816 | |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 817 | //===----------------------------------------------------------------------===// |
| 818 | // MachO |
| 819 | //===----------------------------------------------------------------------===// |
| 820 | |
Bruno Cardoso Lopes | 618c67a | 2015-03-06 13:49:05 +0000 | [diff] [blame] | 821 | TargetLoweringObjectFileMachO::TargetLoweringObjectFileMachO() |
| 822 | : TargetLoweringObjectFile() { |
| 823 | SupportIndirectSymViaGOTPCRel = true; |
| 824 | } |
| 825 | |
Rafael Espindola | 46fa231 | 2016-08-29 12:33:42 +0000 | [diff] [blame] | 826 | void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx, |
| 827 | const TargetMachine &TM) { |
| 828 | TargetLoweringObjectFile::Initialize(Ctx, TM); |
Rafael Espindola | 412a529 | 2016-08-29 12:47:22 +0000 | [diff] [blame] | 829 | if (TM.getRelocationModel() == Reloc::Static) { |
Rafael Espindola | 46fa231 | 2016-08-29 12:33:42 +0000 | [diff] [blame] | 830 | StaticCtorSection = Ctx.getMachOSection("__TEXT", "__constructor", 0, |
| 831 | SectionKind::getData()); |
| 832 | StaticDtorSection = Ctx.getMachOSection("__TEXT", "__destructor", 0, |
| 833 | SectionKind::getData()); |
| 834 | } else { |
| 835 | StaticCtorSection = Ctx.getMachOSection("__DATA", "__mod_init_func", |
| 836 | MachO::S_MOD_INIT_FUNC_POINTERS, |
| 837 | SectionKind::getData()); |
| 838 | StaticDtorSection = Ctx.getMachOSection("__DATA", "__mod_term_func", |
| 839 | MachO::S_MOD_TERM_FUNC_POINTERS, |
| 840 | SectionKind::getData()); |
| 841 | } |
Reid Kleckner | fce7f73 | 2018-08-09 22:24:04 +0000 | [diff] [blame] | 842 | |
| 843 | PersonalityEncoding = |
| 844 | dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; |
| 845 | LSDAEncoding = dwarf::DW_EH_PE_pcrel; |
| 846 | TTypeEncoding = |
| 847 | dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4; |
Rafael Espindola | 46fa231 | 2016-08-29 12:33:42 +0000 | [diff] [blame] | 848 | } |
| 849 | |
Eric Christopher | aadbabc | 2018-04-20 19:07:57 +0000 | [diff] [blame] | 850 | void TargetLoweringObjectFileMachO::emitModuleMetadata(MCStreamer &Streamer, |
| 851 | Module &M) const { |
Daniel Dunbar | 9585612 | 2013-01-18 19:37:00 +0000 | [diff] [blame] | 852 | // Emit the linker options if present. |
Peter Collingbourne | 89061b2 | 2017-06-12 20:10:48 +0000 | [diff] [blame] | 853 | if (auto *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) { |
Saleem Abdulrasool | e0f0c0e | 2016-04-30 18:15:34 +0000 | [diff] [blame] | 854 | for (const auto &Option : LinkerOptions->operands()) { |
Daniel Dunbar | 9585612 | 2013-01-18 19:37:00 +0000 | [diff] [blame] | 855 | SmallVector<std::string, 4> StrOptions; |
Saleem Abdulrasool | e0f0c0e | 2016-04-30 18:15:34 +0000 | [diff] [blame] | 856 | for (const auto &Piece : cast<MDNode>(Option)->operands()) |
| 857 | StrOptions.push_back(cast<MDString>(Piece)->getString()); |
Daniel Dunbar | 9585612 | 2013-01-18 19:37:00 +0000 | [diff] [blame] | 858 | Streamer.EmitLinkerOptions(StrOptions); |
| 859 | } |
Bill Wendling | 06df772 | 2012-02-14 21:28:13 +0000 | [diff] [blame] | 860 | } |
| 861 | |
Saleem Abdulrasool | 4c47434 | 2017-06-05 21:26:39 +0000 | [diff] [blame] | 862 | unsigned VersionVal = 0; |
| 863 | unsigned ImageInfoFlags = 0; |
| 864 | StringRef SectionVal; |
Peter Collingbourne | 89061b2 | 2017-06-12 20:10:48 +0000 | [diff] [blame] | 865 | |
| 866 | GetObjCImageInfo(M, VersionVal, ImageInfoFlags, SectionVal); |
Saleem Abdulrasool | 4c47434 | 2017-06-05 21:26:39 +0000 | [diff] [blame] | 867 | |
Bill Wendling | 734909a | 2012-02-15 22:36:15 +0000 | [diff] [blame] | 868 | // The section is mandatory. If we don't have it, then we don't have GC info. |
Saleem Abdulrasool | 4c47434 | 2017-06-05 21:26:39 +0000 | [diff] [blame] | 869 | if (SectionVal.empty()) |
| 870 | return; |
Bill Wendling | 06df772 | 2012-02-14 21:28:13 +0000 | [diff] [blame] | 871 | |
Bill Wendling | 734909a | 2012-02-15 22:36:15 +0000 | [diff] [blame] | 872 | StringRef Segment, Section; |
| 873 | unsigned TAA = 0, StubSize = 0; |
| 874 | bool TAAParsed; |
| 875 | std::string ErrorCode = |
| 876 | MCSectionMachO::ParseSectionSpecifier(SectionVal, Segment, Section, |
| 877 | TAA, TAAParsed, StubSize); |
Bill Wendling | a0009ee | 2012-02-15 22:47:53 +0000 | [diff] [blame] | 878 | if (!ErrorCode.empty()) |
Bill Wendling | 734909a | 2012-02-15 22:36:15 +0000 | [diff] [blame] | 879 | // If invalid, report the error with report_fatal_error. |
Bill Wendling | a0009ee | 2012-02-15 22:47:53 +0000 | [diff] [blame] | 880 | report_fatal_error("Invalid section specifier '" + Section + "': " + |
| 881 | ErrorCode + "."); |
Bill Wendling | 06df772 | 2012-02-14 21:28:13 +0000 | [diff] [blame] | 882 | |
Bill Wendling | 734909a | 2012-02-15 22:36:15 +0000 | [diff] [blame] | 883 | // Get the section. |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 884 | MCSectionMachO *S = getContext().getMachOSection( |
Rafael Espindola | 449711c | 2015-11-18 06:02:15 +0000 | [diff] [blame] | 885 | Segment, Section, TAA, StubSize, SectionKind::getData()); |
Bill Wendling | 734909a | 2012-02-15 22:36:15 +0000 | [diff] [blame] | 886 | Streamer.SwitchSection(S); |
| 887 | Streamer.EmitLabel(getContext(). |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 888 | getOrCreateSymbol(StringRef("L_OBJC_IMAGE_INFO"))); |
Bill Wendling | 06df772 | 2012-02-14 21:28:13 +0000 | [diff] [blame] | 889 | Streamer.EmitIntValue(VersionVal, 4); |
Bill Wendling | f1b14b7 | 2012-04-24 11:03:50 +0000 | [diff] [blame] | 890 | Streamer.EmitIntValue(ImageInfoFlags, 4); |
Bill Wendling | 06df772 | 2012-02-14 21:28:13 +0000 | [diff] [blame] | 891 | Streamer.AddBlankLine(); |
| 892 | } |
| 893 | |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 894 | static void checkMachOComdat(const GlobalValue *GV) { |
| 895 | const Comdat *C = GV->getComdat(); |
| 896 | if (!C) |
| 897 | return; |
| 898 | |
| 899 | report_fatal_error("MachO doesn't support COMDATs, '" + C->getName() + |
| 900 | "' cannot be lowered."); |
| 901 | } |
| 902 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 903 | MCSection *TargetLoweringObjectFileMachO::getExplicitSectionGlobal( |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 904 | const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 905 | // Parse the section specifier and create it if valid. |
| 906 | StringRef Segment, Section; |
Stuart Hastings | 12d5312 | 2011-03-19 02:42:31 +0000 | [diff] [blame] | 907 | unsigned TAA = 0, StubSize = 0; |
| 908 | bool TAAParsed; |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 909 | |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 910 | checkMachOComdat(GO); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 911 | |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 912 | std::string ErrorCode = |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 913 | MCSectionMachO::ParseSectionSpecifier(GO->getSection(), Segment, Section, |
Stuart Hastings | 12d5312 | 2011-03-19 02:42:31 +0000 | [diff] [blame] | 914 | TAA, TAAParsed, StubSize); |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 915 | if (!ErrorCode.empty()) { |
Chris Lattner | 2104b8d | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 916 | // If invalid, report the error with report_fatal_error. |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 917 | report_fatal_error("Global variable '" + GO->getName() + |
Benjamin Kramer | 1f97a5a | 2011-11-15 16:27:03 +0000 | [diff] [blame] | 918 | "' has an invalid section specifier '" + |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 919 | GO->getSection() + "': " + ErrorCode + "."); |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | // Get the section. |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 923 | MCSectionMachO *S = |
| 924 | getContext().getMachOSection(Segment, Section, TAA, StubSize, Kind); |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 925 | |
Stuart Hastings | b4863a4 | 2011-02-21 17:27:17 +0000 | [diff] [blame] | 926 | // If TAA wasn't set by ParseSectionSpecifier() above, |
| 927 | // use the value returned by getMachOSection() as a default. |
Stuart Hastings | 12d5312 | 2011-03-19 02:42:31 +0000 | [diff] [blame] | 928 | if (!TAAParsed) |
Stuart Hastings | b4863a4 | 2011-02-21 17:27:17 +0000 | [diff] [blame] | 929 | TAA = S->getTypeAndAttributes(); |
| 930 | |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 931 | // Okay, now that we got the section, verify that the TAA & StubSize agree. |
| 932 | // If the user declared multiple globals with different section flags, we need |
| 933 | // to reject it here. |
| 934 | if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) { |
Chris Lattner | 2104b8d | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 935 | // If invalid, report the error with report_fatal_error. |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 936 | report_fatal_error("Global variable '" + GO->getName() + |
Benjamin Kramer | 1f97a5a | 2011-11-15 16:27:03 +0000 | [diff] [blame] | 937 | "' section type or attributes does not match previous" |
| 938 | " section specifier"); |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | return S; |
| 942 | } |
| 943 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 944 | MCSection *TargetLoweringObjectFileMachO::SelectSectionForGlobal( |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 945 | const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { |
| 946 | checkMachOComdat(GO); |
Bill Wendling | 25b61db | 2013-11-17 10:53:13 +0000 | [diff] [blame] | 947 | |
| 948 | // Handle thread local data. |
| 949 | if (Kind.isThreadBSS()) return TLSBSSSection; |
| 950 | if (Kind.isThreadData()) return TLSDataSection; |
| 951 | |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 952 | if (Kind.isText()) |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 953 | return GO->isWeakForLinker() ? TextCoalSection : TextSection; |
Arnold Schwaighofer | c31c2de | 2013-08-08 21:04:16 +0000 | [diff] [blame] | 954 | |
| 955 | // If this is weak/linkonce, put this in a coalescable section, either in text |
| 956 | // or data depending on if it is writable. |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 957 | if (GO->isWeakForLinker()) { |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 958 | if (Kind.isReadOnly()) |
Arnold Schwaighofer | c31c2de | 2013-08-08 21:04:16 +0000 | [diff] [blame] | 959 | return ConstTextCoalSection; |
Steven Wu | d0804aa | 2018-04-10 20:16:35 +0000 | [diff] [blame] | 960 | if (Kind.isReadOnlyWithRel()) |
| 961 | return ConstDataCoalSection; |
Arnold Schwaighofer | c31c2de | 2013-08-08 21:04:16 +0000 | [diff] [blame] | 962 | return DataCoalSection; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | // FIXME: Alignment check should be handled by section classifier. |
Chris Lattner | ef2f804 | 2010-03-07 04:28:09 +0000 | [diff] [blame] | 966 | if (Kind.isMergeable1ByteCString() && |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 967 | GO->getParent()->getDataLayout().getPreferredAlignment( |
| 968 | cast<GlobalVariable>(GO)) < 32) |
Chris Lattner | ef2f804 | 2010-03-07 04:28:09 +0000 | [diff] [blame] | 969 | return CStringSection; |
Michael J. Spencer | fbdab0d | 2010-10-27 18:52:20 +0000 | [diff] [blame] | 970 | |
Chris Lattner | ef2f804 | 2010-03-07 04:28:09 +0000 | [diff] [blame] | 971 | // Do not put 16-bit arrays in the UString section if they have an |
| 972 | // externally visible label, this runs into issues with certain linker |
| 973 | // versions. |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 974 | if (Kind.isMergeable2ByteCString() && !GO->hasExternalLinkage() && |
| 975 | GO->getParent()->getDataLayout().getPreferredAlignment( |
| 976 | cast<GlobalVariable>(GO)) < 32) |
Chris Lattner | ef2f804 | 2010-03-07 04:28:09 +0000 | [diff] [blame] | 977 | return UStringSection; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 978 | |
Rafael Espindola | b43d51d | 2014-08-28 20:13:31 +0000 | [diff] [blame] | 979 | // With MachO only variables whose corresponding symbol starts with 'l' or |
| 980 | // '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] | 981 | if (GO->hasPrivateLinkage() && Kind.isMergeableConst()) { |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 982 | if (Kind.isMergeableConst4()) |
| 983 | return FourByteConstantSection; |
| 984 | if (Kind.isMergeableConst8()) |
| 985 | return EightByteConstantSection; |
Rafael Espindola | 1f3de49 | 2014-02-13 23:16:11 +0000 | [diff] [blame] | 986 | if (Kind.isMergeableConst16()) |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 987 | return SixteenByteConstantSection; |
| 988 | } |
| 989 | |
| 990 | // Otherwise, if it is readonly, but not something we can specially optimize, |
| 991 | // just drop it in .const. |
| 992 | if (Kind.isReadOnly()) |
| 993 | return ReadOnlySection; |
| 994 | |
| 995 | // If this is marked const, put it into a const section. But if the dynamic |
| 996 | // linker needs to write to it, put it in the data segment. |
| 997 | if (Kind.isReadOnlyWithRel()) |
| 998 | return ConstDataSection; |
| 999 | |
| 1000 | // Put zero initialized globals with strong external linkage in the |
| 1001 | // DATA, __common section with the .zerofill directive. |
| 1002 | if (Kind.isBSSExtern()) |
| 1003 | return DataCommonSection; |
| 1004 | |
| 1005 | // Put zero initialized globals with local linkage in __DATA,__bss directive |
| 1006 | // with the .zerofill directive (aka .lcomm). |
| 1007 | if (Kind.isBSSLocal()) |
| 1008 | return DataBSSSection; |
Michael J. Spencer | fbdab0d | 2010-10-27 18:52:20 +0000 | [diff] [blame] | 1009 | |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 1010 | // Otherwise, just drop the variable in the normal data section. |
| 1011 | return DataSection; |
| 1012 | } |
| 1013 | |
Mehdi Amini | 5c0fa58 | 2015-07-16 06:04:17 +0000 | [diff] [blame] | 1014 | MCSection *TargetLoweringObjectFileMachO::getSectionForConstant( |
David Majnemer | a3ea407 | 2016-02-21 01:30:30 +0000 | [diff] [blame] | 1015 | const DataLayout &DL, SectionKind Kind, const Constant *C, |
| 1016 | unsigned &Align) const { |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 1017 | // If this constant requires a relocation, we have to put it in the data |
| 1018 | // segment, not in the text segment. |
Rafael Espindola | 449711c | 2015-11-18 06:02:15 +0000 | [diff] [blame] | 1019 | if (Kind.isData() || Kind.isReadOnlyWithRel()) |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 1020 | return ConstDataSection; |
| 1021 | |
| 1022 | if (Kind.isMergeableConst4()) |
| 1023 | return FourByteConstantSection; |
| 1024 | if (Kind.isMergeableConst8()) |
| 1025 | return EightByteConstantSection; |
Rafael Espindola | 1f3de49 | 2014-02-13 23:16:11 +0000 | [diff] [blame] | 1026 | if (Kind.isMergeableConst16()) |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 1027 | return SixteenByteConstantSection; |
| 1028 | return ReadOnlySection; // .const |
| 1029 | } |
| 1030 | |
Rafael Espindola | 15b2669 | 2014-02-09 14:50:44 +0000 | [diff] [blame] | 1031 | const MCExpr *TargetLoweringObjectFileMachO::getTTypeGlobalReference( |
Eric Christopher | 4367c7f | 2016-09-16 07:33:15 +0000 | [diff] [blame] | 1032 | const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM, |
| 1033 | MachineModuleInfo *MMI, MCStreamer &Streamer) const { |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 1034 | // The mach-o version of this method defaults to returning a stub reference. |
| 1035 | |
Anton Korobeynikov | 31a9212 | 2010-02-21 20:28:15 +0000 | [diff] [blame] | 1036 | if (Encoding & DW_EH_PE_indirect) { |
| 1037 | MachineModuleInfoMachO &MachOMMI = |
| 1038 | MMI->getObjFileInfo<MachineModuleInfoMachO>(); |
| 1039 | |
Eric Christopher | 4367c7f | 2016-09-16 07:33:15 +0000 | [diff] [blame] | 1040 | MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", TM); |
Anton Korobeynikov | 31a9212 | 2010-02-21 20:28:15 +0000 | [diff] [blame] | 1041 | |
| 1042 | // Add information about the stub reference to MachOMMI so that the stub |
| 1043 | // gets emitted by the asmprinter. |
Rafael Espindola | 712f957 | 2016-05-17 16:01:32 +0000 | [diff] [blame] | 1044 | MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1045 | if (!StubSym.getPointer()) { |
Tim Northover | b64fb45 | 2016-11-22 16:17:20 +0000 | [diff] [blame] | 1046 | MCSymbol *Sym = TM.getSymbol(GV); |
Chris Lattner | 2ea586b | 2010-03-15 20:37:38 +0000 | [diff] [blame] | 1047 | StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage()); |
Anton Korobeynikov | 31a9212 | 2010-02-21 20:28:15 +0000 | [diff] [blame] | 1048 | } |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 1049 | |
| 1050 | return TargetLoweringObjectFile:: |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1051 | getTTypeReference(MCSymbolRefExpr::create(SSym, getContext()), |
Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 1052 | Encoding & ~DW_EH_PE_indirect, Streamer); |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
Eric Christopher | 4367c7f | 2016-09-16 07:33:15 +0000 | [diff] [blame] | 1055 | return TargetLoweringObjectFile::getTTypeGlobalReference(GV, Encoding, TM, |
| 1056 | MMI, Streamer); |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 1057 | } |
| 1058 | |
Rafael Espindola | daeafb4 | 2014-02-19 17:23:20 +0000 | [diff] [blame] | 1059 | MCSymbol *TargetLoweringObjectFileMachO::getCFIPersonalitySymbol( |
Eric Christopher | 4367c7f | 2016-09-16 07:33:15 +0000 | [diff] [blame] | 1060 | const GlobalValue *GV, const TargetMachine &TM, |
Rafael Espindola | daeafb4 | 2014-02-19 17:23:20 +0000 | [diff] [blame] | 1061 | MachineModuleInfo *MMI) const { |
Rafael Espindola | 0870434 | 2011-04-27 23:08:15 +0000 | [diff] [blame] | 1062 | // The mach-o version of this method defaults to returning a stub reference. |
| 1063 | MachineModuleInfoMachO &MachOMMI = |
| 1064 | MMI->getObjFileInfo<MachineModuleInfoMachO>(); |
| 1065 | |
Eric Christopher | 4367c7f | 2016-09-16 07:33:15 +0000 | [diff] [blame] | 1066 | MCSymbol *SSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr", TM); |
Rafael Espindola | 0870434 | 2011-04-27 23:08:15 +0000 | [diff] [blame] | 1067 | |
| 1068 | // Add information about the stub reference to MachOMMI so that the stub |
| 1069 | // gets emitted by the asmprinter. |
Bill Wendling | e4cc332 | 2011-11-29 01:43:20 +0000 | [diff] [blame] | 1070 | MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(SSym); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1071 | if (!StubSym.getPointer()) { |
Tim Northover | b64fb45 | 2016-11-22 16:17:20 +0000 | [diff] [blame] | 1072 | MCSymbol *Sym = TM.getSymbol(GV); |
Rafael Espindola | 0870434 | 2011-04-27 23:08:15 +0000 | [diff] [blame] | 1073 | StubSym = MachineModuleInfoImpl::StubValueTy(Sym, !GV->hasLocalLinkage()); |
| 1074 | } |
| 1075 | |
| 1076 | return SSym; |
| 1077 | } |
| 1078 | |
Bruno Cardoso Lopes | 618c67a | 2015-03-06 13:49:05 +0000 | [diff] [blame] | 1079 | const MCExpr *TargetLoweringObjectFileMachO::getIndirectSymViaGOTPCRel( |
| 1080 | const MCSymbol *Sym, const MCValue &MV, int64_t Offset, |
| 1081 | MachineModuleInfo *MMI, MCStreamer &Streamer) const { |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 1082 | // 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] | 1083 | // as 64-bit do, we replace the GOT equivalent by accessing the final symbol |
| 1084 | // through a non_lazy_ptr stub instead. One advantage is that it allows the |
| 1085 | // computation of deltas to final external symbols. Example: |
| 1086 | // |
| 1087 | // _extgotequiv: |
| 1088 | // .long _extfoo |
| 1089 | // |
| 1090 | // _delta: |
| 1091 | // .long _extgotequiv-_delta |
| 1092 | // |
| 1093 | // is transformed to: |
| 1094 | // |
| 1095 | // _delta: |
| 1096 | // .long L_extfoo$non_lazy_ptr-(_delta+0) |
| 1097 | // |
| 1098 | // .section __IMPORT,__pointers,non_lazy_symbol_pointers |
| 1099 | // L_extfoo$non_lazy_ptr: |
| 1100 | // .indirect_symbol _extfoo |
| 1101 | // .long 0 |
| 1102 | // |
| 1103 | MachineModuleInfoMachO &MachOMMI = |
| 1104 | MMI->getObjFileInfo<MachineModuleInfoMachO>(); |
| 1105 | MCContext &Ctx = getContext(); |
| 1106 | |
| 1107 | // The offset must consider the original displacement from the base symbol |
| 1108 | // since 32-bit targets don't have a GOTPCREL to fold the PC displacement. |
| 1109 | Offset = -MV.getConstant(); |
| 1110 | const MCSymbol *BaseSym = &MV.getSymB()->getSymbol(); |
| 1111 | |
| 1112 | // Access the final symbol via sym$non_lazy_ptr and generate the appropriated |
| 1113 | // non_lazy_ptr stubs. |
| 1114 | SmallString<128> Name; |
| 1115 | StringRef Suffix = "$non_lazy_ptr"; |
Mehdi Amini | 5c0fa58 | 2015-07-16 06:04:17 +0000 | [diff] [blame] | 1116 | Name += MMI->getModule()->getDataLayout().getPrivateGlobalPrefix(); |
Bruno Cardoso Lopes | 618c67a | 2015-03-06 13:49:05 +0000 | [diff] [blame] | 1117 | Name += Sym->getName(); |
| 1118 | Name += Suffix; |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 1119 | MCSymbol *Stub = Ctx.getOrCreateSymbol(Name); |
Bruno Cardoso Lopes | 618c67a | 2015-03-06 13:49:05 +0000 | [diff] [blame] | 1120 | |
| 1121 | MachineModuleInfoImpl::StubValueTy &StubSym = MachOMMI.getGVStubEntry(Stub); |
| 1122 | if (!StubSym.getPointer()) |
| 1123 | StubSym = MachineModuleInfoImpl:: |
| 1124 | StubValueTy(const_cast<MCSymbol *>(Sym), true /* access indirectly */); |
| 1125 | |
| 1126 | const MCExpr *BSymExpr = |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1127 | MCSymbolRefExpr::create(BaseSym, MCSymbolRefExpr::VK_None, Ctx); |
Bruno Cardoso Lopes | 618c67a | 2015-03-06 13:49:05 +0000 | [diff] [blame] | 1128 | const MCExpr *LHS = |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1129 | MCSymbolRefExpr::create(Stub, MCSymbolRefExpr::VK_None, Ctx); |
Bruno Cardoso Lopes | 618c67a | 2015-03-06 13:49:05 +0000 | [diff] [blame] | 1130 | |
| 1131 | if (!Offset) |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1132 | return MCBinaryExpr::createSub(LHS, BSymExpr, Ctx); |
Bruno Cardoso Lopes | 618c67a | 2015-03-06 13:49:05 +0000 | [diff] [blame] | 1133 | |
| 1134 | const MCExpr *RHS = |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1135 | MCBinaryExpr::createAdd(BSymExpr, MCConstantExpr::create(Offset, Ctx), Ctx); |
| 1136 | return MCBinaryExpr::createSub(LHS, RHS, Ctx); |
Bruno Cardoso Lopes | 618c67a | 2015-03-06 13:49:05 +0000 | [diff] [blame] | 1137 | } |
| 1138 | |
Peter Collingbourne | 94d7786 | 2015-11-03 23:40:03 +0000 | [diff] [blame] | 1139 | static bool canUsePrivateLabel(const MCAsmInfo &AsmInfo, |
| 1140 | const MCSection &Section) { |
| 1141 | if (!AsmInfo.isSectionAtomizableBySymbols(Section)) |
| 1142 | return true; |
| 1143 | |
| 1144 | // If it is not dead stripped, it is safe to use private labels. |
| 1145 | const MCSectionMachO &SMO = cast<MCSectionMachO>(Section); |
| 1146 | if (SMO.hasAttribute(MachO::S_ATTR_NO_DEAD_STRIP)) |
| 1147 | return true; |
| 1148 | |
| 1149 | return false; |
| 1150 | } |
| 1151 | |
| 1152 | void TargetLoweringObjectFileMachO::getNameWithPrefix( |
Eric Christopher | 4367c7f | 2016-09-16 07:33:15 +0000 | [diff] [blame] | 1153 | SmallVectorImpl<char> &OutName, const GlobalValue *GV, |
Peter Collingbourne | 94d7786 | 2015-11-03 23:40:03 +0000 | [diff] [blame] | 1154 | const TargetMachine &TM) const { |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 1155 | bool CannotUsePrivateLabel = true; |
| 1156 | if (auto *GO = GV->getBaseObject()) { |
| 1157 | SectionKind GOKind = TargetLoweringObjectFile::getKindForGlobal(GO, TM); |
| 1158 | const MCSection *TheSection = SectionForGlobal(GO, GOKind, TM); |
| 1159 | CannotUsePrivateLabel = |
| 1160 | !canUsePrivateLabel(*TM.getMCAsmInfo(), *TheSection); |
| 1161 | } |
Eric Christopher | 4367c7f | 2016-09-16 07:33:15 +0000 | [diff] [blame] | 1162 | getMangler().getNameWithPrefix(OutName, GV, CannotUsePrivateLabel); |
Peter Collingbourne | 94d7786 | 2015-11-03 23:40:03 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 1165 | //===----------------------------------------------------------------------===// |
| 1166 | // COFF |
| 1167 | //===----------------------------------------------------------------------===// |
| 1168 | |
Chris Lattner | 87cffa9 | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 1169 | static unsigned |
Renato Golin | ef3eb06 | 2016-06-27 14:42:20 +0000 | [diff] [blame] | 1170 | getCOFFSectionFlags(SectionKind K, const TargetMachine &TM) { |
Chris Lattner | 87cffa9 | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 1171 | unsigned Flags = 0; |
Renato Golin | ef3eb06 | 2016-06-27 14:42:20 +0000 | [diff] [blame] | 1172 | bool isThumb = TM.getTargetTriple().getArch() == Triple::thumb; |
Chris Lattner | 87cffa9 | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 1173 | |
Anton Korobeynikov | e415230 | 2010-07-06 15:24:56 +0000 | [diff] [blame] | 1174 | if (K.isMetadata()) |
Chris Lattner | 0284493 | 2010-05-07 21:49:09 +0000 | [diff] [blame] | 1175 | Flags |= |
Daniel Dunbar | 329d202 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 1176 | COFF::IMAGE_SCN_MEM_DISCARDABLE; |
Chris Lattner | 87cffa9 | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 1177 | else if (K.isText()) |
| 1178 | Flags |= |
Daniel Dunbar | 329d202 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 1179 | COFF::IMAGE_SCN_MEM_EXECUTE | |
Michael J. Spencer | 0f83d96 | 2010-10-27 18:52:29 +0000 | [diff] [blame] | 1180 | COFF::IMAGE_SCN_MEM_READ | |
Renato Golin | ef3eb06 | 2016-06-27 14:42:20 +0000 | [diff] [blame] | 1181 | COFF::IMAGE_SCN_CNT_CODE | |
| 1182 | (isThumb ? COFF::IMAGE_SCN_MEM_16BIT : (COFF::SectionCharacteristics)0); |
David Majnemer | b8dbebb | 2014-09-20 07:31:46 +0000 | [diff] [blame] | 1183 | else if (K.isBSS()) |
Chris Lattner | 0284493 | 2010-05-07 21:49:09 +0000 | [diff] [blame] | 1184 | Flags |= |
Daniel Dunbar | 329d202 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 1185 | COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA | |
| 1186 | COFF::IMAGE_SCN_MEM_READ | |
| 1187 | COFF::IMAGE_SCN_MEM_WRITE; |
Anton Korobeynikov | c6b4017 | 2012-02-11 17:26:53 +0000 | [diff] [blame] | 1188 | else if (K.isThreadLocal()) |
| 1189 | Flags |= |
| 1190 | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | |
| 1191 | COFF::IMAGE_SCN_MEM_READ | |
| 1192 | COFF::IMAGE_SCN_MEM_WRITE; |
David Majnemer | b8dbebb | 2014-09-20 07:31:46 +0000 | [diff] [blame] | 1193 | else if (K.isReadOnly() || K.isReadOnlyWithRel()) |
Chris Lattner | 87cffa9 | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 1194 | Flags |= |
Daniel Dunbar | 329d202 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 1195 | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | |
| 1196 | COFF::IMAGE_SCN_MEM_READ; |
Chris Lattner | 87cffa9 | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 1197 | else if (K.isWriteable()) |
| 1198 | Flags |= |
Daniel Dunbar | 329d202 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 1199 | COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | |
| 1200 | COFF::IMAGE_SCN_MEM_READ | |
| 1201 | COFF::IMAGE_SCN_MEM_WRITE; |
Chris Lattner | 87cffa9 | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 1202 | |
| 1203 | return Flags; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 1204 | } |
| 1205 | |
Benjamin Kramer | 6cbe670 | 2014-07-07 14:47:51 +0000 | [diff] [blame] | 1206 | static const GlobalValue *getComdatGVForCOFF(const GlobalValue *GV) { |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 1207 | const Comdat *C = GV->getComdat(); |
| 1208 | assert(C && "expected GV to have a Comdat!"); |
| 1209 | |
| 1210 | StringRef ComdatGVName = C->getName(); |
| 1211 | const GlobalValue *ComdatGV = GV->getParent()->getNamedValue(ComdatGVName); |
| 1212 | if (!ComdatGV) |
| 1213 | report_fatal_error("Associative COMDAT symbol '" + ComdatGVName + |
| 1214 | "' does not exist."); |
| 1215 | |
| 1216 | if (ComdatGV->getComdat() != C) |
| 1217 | report_fatal_error("Associative COMDAT symbol '" + ComdatGVName + |
Hans Wennborg | c0f0c51 | 2014-09-19 01:14:56 +0000 | [diff] [blame] | 1218 | "' is not a key for its COMDAT."); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 1219 | |
| 1220 | return ComdatGV; |
| 1221 | } |
| 1222 | |
| 1223 | static int getSelectionForCOFF(const GlobalValue *GV) { |
| 1224 | if (const Comdat *C = GV->getComdat()) { |
| 1225 | const GlobalValue *ComdatKey = getComdatGVForCOFF(GV); |
| 1226 | if (const auto *GA = dyn_cast<GlobalAlias>(ComdatKey)) |
| 1227 | ComdatKey = GA->getBaseObject(); |
| 1228 | if (ComdatKey == GV) { |
| 1229 | switch (C->getSelectionKind()) { |
| 1230 | case Comdat::Any: |
| 1231 | return COFF::IMAGE_COMDAT_SELECT_ANY; |
| 1232 | case Comdat::ExactMatch: |
| 1233 | return COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH; |
| 1234 | case Comdat::Largest: |
| 1235 | return COFF::IMAGE_COMDAT_SELECT_LARGEST; |
| 1236 | case Comdat::NoDuplicates: |
| 1237 | return COFF::IMAGE_COMDAT_SELECT_NODUPLICATES; |
| 1238 | case Comdat::SameSize: |
| 1239 | return COFF::IMAGE_COMDAT_SELECT_SAME_SIZE; |
| 1240 | } |
| 1241 | } else { |
| 1242 | return COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE; |
| 1243 | } |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 1244 | } |
| 1245 | return 0; |
| 1246 | } |
| 1247 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 1248 | MCSection *TargetLoweringObjectFileCOFF::getExplicitSectionGlobal( |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 1249 | const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { |
Michael J. Spencer | f1aef75 | 2012-11-13 22:04:09 +0000 | [diff] [blame] | 1250 | int Selection = 0; |
Renato Golin | ef3eb06 | 2016-06-27 14:42:20 +0000 | [diff] [blame] | 1251 | unsigned Characteristics = getCOFFSectionFlags(Kind, TM); |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 1252 | StringRef Name = GO->getSection(); |
Rafael Espindola | 2d30ae2 | 2013-11-27 01:18:37 +0000 | [diff] [blame] | 1253 | StringRef COMDATSymName = ""; |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 1254 | if (GO->hasComdat()) { |
| 1255 | Selection = getSelectionForCOFF(GO); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 1256 | const GlobalValue *ComdatGV; |
| 1257 | if (Selection == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 1258 | ComdatGV = getComdatGVForCOFF(GO); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 1259 | else |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 1260 | ComdatGV = GO; |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 1261 | |
| 1262 | if (!ComdatGV->hasPrivateLinkage()) { |
Tim Northover | b64fb45 | 2016-11-22 16:17:20 +0000 | [diff] [blame] | 1263 | MCSymbol *Sym = TM.getSymbol(ComdatGV); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 1264 | COMDATSymName = Sym->getName(); |
| 1265 | Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT; |
| 1266 | } else { |
| 1267 | Selection = 0; |
| 1268 | } |
Michael J. Spencer | f1aef75 | 2012-11-13 22:04:09 +0000 | [diff] [blame] | 1269 | } |
Reid Kleckner | 97837b7 | 2016-05-02 23:22:18 +0000 | [diff] [blame] | 1270 | |
| 1271 | return getContext().getCOFFSection(Name, Characteristics, Kind, COMDATSymName, |
Nico Rieck | a37acf7 | 2013-07-06 12:13:10 +0000 | [diff] [blame] | 1272 | Selection); |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 1273 | } |
| 1274 | |
Reid Kleckner | 13c9ee6 | 2018-06-21 20:27:38 +0000 | [diff] [blame] | 1275 | static StringRef getCOFFSectionNameForUniqueGlobal(SectionKind Kind) { |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 1276 | if (Kind.isText()) |
Rafael Espindola | 2d30ae2 | 2013-11-27 01:18:37 +0000 | [diff] [blame] | 1277 | return ".text"; |
David Majnemer | a9bdb32 | 2014-04-08 22:33:40 +0000 | [diff] [blame] | 1278 | if (Kind.isBSS()) |
Rafael Espindola | 2d30ae2 | 2013-11-27 01:18:37 +0000 | [diff] [blame] | 1279 | return ".bss"; |
| 1280 | if (Kind.isThreadLocal()) |
Rafael Espindola | 3c8e147 | 2013-11-27 15:52:11 +0000 | [diff] [blame] | 1281 | return ".tls$"; |
David Majnemer | 597be2d | 2014-09-22 20:39:23 +0000 | [diff] [blame] | 1282 | if (Kind.isReadOnly() || Kind.isReadOnlyWithRel()) |
| 1283 | return ".rdata"; |
| 1284 | return ".data"; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 1285 | } |
| 1286 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 1287 | MCSection *TargetLoweringObjectFileCOFF::SelectSectionForGlobal( |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 1288 | const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { |
David Majnemer | 9338984 | 2014-03-23 17:47:39 +0000 | [diff] [blame] | 1289 | // If we have -ffunction-sections then we should emit the global value to a |
| 1290 | // uniqued section specifically for it. |
David Majnemer | 273bff4 | 2014-03-25 06:14:26 +0000 | [diff] [blame] | 1291 | bool EmitUniquedSection; |
| 1292 | if (Kind.isText()) |
| 1293 | EmitUniquedSection = TM.getFunctionSections(); |
| 1294 | else |
| 1295 | EmitUniquedSection = TM.getDataSections(); |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 1296 | |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 1297 | if ((EmitUniquedSection && !Kind.isCommon()) || GO->hasComdat()) { |
Reid Kleckner | 13c9ee6 | 2018-06-21 20:27:38 +0000 | [diff] [blame] | 1298 | SmallString<256> Name = getCOFFSectionNameForUniqueGlobal(Kind); |
| 1299 | |
Renato Golin | ef3eb06 | 2016-06-27 14:42:20 +0000 | [diff] [blame] | 1300 | unsigned Characteristics = getCOFFSectionFlags(Kind, TM); |
Chris Lattner | 0284493 | 2010-05-07 21:49:09 +0000 | [diff] [blame] | 1301 | |
Daniel Dunbar | 329d202 | 2010-07-01 20:07:24 +0000 | [diff] [blame] | 1302 | Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT; |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 1303 | int Selection = getSelectionForCOFF(GO); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 1304 | if (!Selection) |
| 1305 | Selection = COFF::IMAGE_COMDAT_SELECT_NODUPLICATES; |
| 1306 | const GlobalValue *ComdatGV; |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 1307 | if (GO->hasComdat()) |
| 1308 | ComdatGV = getComdatGVForCOFF(GO); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 1309 | else |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 1310 | ComdatGV = GO; |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 1311 | |
Reid Kleckner | 97837b7 | 2016-05-02 23:22:18 +0000 | [diff] [blame] | 1312 | unsigned UniqueID = MCContext::GenericSectionID; |
| 1313 | if (EmitUniquedSection) |
| 1314 | UniqueID = NextUniqueID++; |
| 1315 | |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 1316 | if (!ComdatGV->hasPrivateLinkage()) { |
Tim Northover | b64fb45 | 2016-11-22 16:17:20 +0000 | [diff] [blame] | 1317 | MCSymbol *Sym = TM.getSymbol(ComdatGV); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 1318 | StringRef COMDATSymName = Sym->getName(); |
Reid Kleckner | 2ef4866 | 2018-06-21 23:06:33 +0000 | [diff] [blame] | 1319 | |
| 1320 | // Append "$symbol" to the section name when targetting mingw. The ld.bfd |
| 1321 | // COFF linker will not properly handle comdats otherwise. |
| 1322 | if (getTargetTriple().isWindowsGNUEnvironment()) |
| 1323 | raw_svector_ostream(Name) << '$' << COMDATSymName; |
| 1324 | |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 1325 | return getContext().getCOFFSection(Name, Characteristics, Kind, |
Reid Kleckner | 97837b7 | 2016-05-02 23:22:18 +0000 | [diff] [blame] | 1326 | COMDATSymName, Selection, UniqueID); |
David Majnemer | 7db449a | 2015-03-17 23:54:51 +0000 | [diff] [blame] | 1327 | } else { |
| 1328 | SmallString<256> TmpData; |
Peter Collingbourne | 6733564 | 2016-10-24 19:23:39 +0000 | [diff] [blame] | 1329 | getMangler().getNameWithPrefix(TmpData, GO, /*CannotUsePrivateLabel=*/true); |
David Majnemer | 7db449a | 2015-03-17 23:54:51 +0000 | [diff] [blame] | 1330 | return getContext().getCOFFSection(Name, Characteristics, Kind, TmpData, |
Reid Kleckner | 97837b7 | 2016-05-02 23:22:18 +0000 | [diff] [blame] | 1331 | Selection, UniqueID); |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 1332 | } |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 1333 | } |
| 1334 | |
| 1335 | if (Kind.isText()) |
David Majnemer | 3d96acb | 2013-08-13 01:23:53 +0000 | [diff] [blame] | 1336 | return TextSection; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 1337 | |
Anton Korobeynikov | c6b4017 | 2012-02-11 17:26:53 +0000 | [diff] [blame] | 1338 | if (Kind.isThreadLocal()) |
David Majnemer | 3d96acb | 2013-08-13 01:23:53 +0000 | [diff] [blame] | 1339 | return TLSDataSection; |
Anton Korobeynikov | c6b4017 | 2012-02-11 17:26:53 +0000 | [diff] [blame] | 1340 | |
David Majnemer | 597be2d | 2014-09-22 20:39:23 +0000 | [diff] [blame] | 1341 | if (Kind.isReadOnly() || Kind.isReadOnlyWithRel()) |
David Majnemer | f76d6b3 | 2013-08-08 01:50:52 +0000 | [diff] [blame] | 1342 | return ReadOnlySection; |
| 1343 | |
David Majnemer | a9bdb32 | 2014-04-08 22:33:40 +0000 | [diff] [blame] | 1344 | // Note: we claim that common symbols are put in BSSSection, but they are |
| 1345 | // really emitted with the magic .comm directive, which creates a symbol table |
| 1346 | // entry but not a section. |
| 1347 | if (Kind.isBSS() || Kind.isCommon()) |
David Majnemer | 3d96acb | 2013-08-13 01:23:53 +0000 | [diff] [blame] | 1348 | return BSSSection; |
| 1349 | |
| 1350 | return DataSection; |
Anton Korobeynikov | ab663a0 | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 1351 | } |
| 1352 | |
David Majnemer | 7db449a | 2015-03-17 23:54:51 +0000 | [diff] [blame] | 1353 | void TargetLoweringObjectFileCOFF::getNameWithPrefix( |
Eric Christopher | 4367c7f | 2016-09-16 07:33:15 +0000 | [diff] [blame] | 1354 | SmallVectorImpl<char> &OutName, const GlobalValue *GV, |
Peter Collingbourne | 94d7786 | 2015-11-03 23:40:03 +0000 | [diff] [blame] | 1355 | const TargetMachine &TM) const { |
| 1356 | bool CannotUsePrivateLabel = false; |
David Majnemer | 7db449a | 2015-03-17 23:54:51 +0000 | [diff] [blame] | 1357 | if (GV->hasPrivateLinkage() && |
| 1358 | ((isa<Function>(GV) && TM.getFunctionSections()) || |
| 1359 | (isa<GlobalVariable>(GV) && TM.getDataSections()))) |
| 1360 | CannotUsePrivateLabel = true; |
| 1361 | |
Eric Christopher | 4367c7f | 2016-09-16 07:33:15 +0000 | [diff] [blame] | 1362 | getMangler().getNameWithPrefix(OutName, GV, CannotUsePrivateLabel); |
David Majnemer | 7db449a | 2015-03-17 23:54:51 +0000 | [diff] [blame] | 1363 | } |
| 1364 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 1365 | MCSection *TargetLoweringObjectFileCOFF::getSectionForJumpTable( |
Eric Christopher | 4367c7f | 2016-09-16 07:33:15 +0000 | [diff] [blame] | 1366 | const Function &F, const TargetMachine &TM) const { |
Rafael Espindola | ab447e4 | 2015-03-11 19:58:37 +0000 | [diff] [blame] | 1367 | // If the function can be removed, produce a unique section so that |
| 1368 | // the table doesn't prevent the removal. |
| 1369 | const Comdat *C = F.getComdat(); |
| 1370 | bool EmitUniqueSection = TM.getFunctionSections() || C; |
| 1371 | if (!EmitUniqueSection) |
| 1372 | return ReadOnlySection; |
| 1373 | |
| 1374 | // FIXME: we should produce a symbol for F instead. |
| 1375 | if (F.hasPrivateLinkage()) |
| 1376 | return ReadOnlySection; |
| 1377 | |
Tim Northover | b64fb45 | 2016-11-22 16:17:20 +0000 | [diff] [blame] | 1378 | MCSymbol *Sym = TM.getSymbol(&F); |
Rafael Espindola | ab447e4 | 2015-03-11 19:58:37 +0000 | [diff] [blame] | 1379 | StringRef COMDATSymName = Sym->getName(); |
| 1380 | |
| 1381 | SectionKind Kind = SectionKind::getReadOnly(); |
Reid Kleckner | 13c9ee6 | 2018-06-21 20:27:38 +0000 | [diff] [blame] | 1382 | StringRef SecName = getCOFFSectionNameForUniqueGlobal(Kind); |
Renato Golin | ef3eb06 | 2016-06-27 14:42:20 +0000 | [diff] [blame] | 1383 | unsigned Characteristics = getCOFFSectionFlags(Kind, TM); |
Rafael Espindola | ab447e4 | 2015-03-11 19:58:37 +0000 | [diff] [blame] | 1384 | Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT; |
Reid Kleckner | 97837b7 | 2016-05-02 23:22:18 +0000 | [diff] [blame] | 1385 | unsigned UniqueID = NextUniqueID++; |
Rafael Espindola | ab447e4 | 2015-03-11 19:58:37 +0000 | [diff] [blame] | 1386 | |
Reid Kleckner | 13c9ee6 | 2018-06-21 20:27:38 +0000 | [diff] [blame] | 1387 | return getContext().getCOFFSection( |
| 1388 | SecName, Characteristics, Kind, COMDATSymName, |
| 1389 | COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE, UniqueID); |
Rafael Espindola | ab447e4 | 2015-03-11 19:58:37 +0000 | [diff] [blame] | 1390 | } |
| 1391 | |
Eric Christopher | aadbabc | 2018-04-20 19:07:57 +0000 | [diff] [blame] | 1392 | void TargetLoweringObjectFileCOFF::emitModuleMetadata(MCStreamer &Streamer, |
| 1393 | Module &M) const { |
Peter Collingbourne | 89061b2 | 2017-06-12 20:10:48 +0000 | [diff] [blame] | 1394 | if (NamedMDNode *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) { |
Saleem Abdulrasool | e0f0c0e | 2016-04-30 18:15:34 +0000 | [diff] [blame] | 1395 | // Emit the linker options to the linker .drectve section. According to the |
| 1396 | // spec, this section is a space-separated string containing flags for |
| 1397 | // linker. |
| 1398 | MCSection *Sec = getDrectveSection(); |
| 1399 | Streamer.SwitchSection(Sec); |
| 1400 | for (const auto &Option : LinkerOptions->operands()) { |
| 1401 | for (const auto &Piece : cast<MDNode>(Option)->operands()) { |
| 1402 | // Lead with a space for consistency with our dllexport implementation. |
| 1403 | std::string Directive(" "); |
| 1404 | Directive.append(cast<MDString>(Piece)->getString()); |
| 1405 | Streamer.EmitBytes(Directive); |
| 1406 | } |
Reid Kleckner | d973ca3 | 2013-04-25 19:34:41 +0000 | [diff] [blame] | 1407 | } |
| 1408 | } |
Saleem Abdulrasool | 4c47434 | 2017-06-05 21:26:39 +0000 | [diff] [blame] | 1409 | |
| 1410 | unsigned Version = 0; |
| 1411 | unsigned Flags = 0; |
| 1412 | StringRef Section; |
| 1413 | |
Peter Collingbourne | 89061b2 | 2017-06-12 20:10:48 +0000 | [diff] [blame] | 1414 | GetObjCImageInfo(M, Version, Flags, Section); |
Saleem Abdulrasool | 4c47434 | 2017-06-05 21:26:39 +0000 | [diff] [blame] | 1415 | if (Section.empty()) |
| 1416 | return; |
| 1417 | |
| 1418 | auto &C = getContext(); |
| 1419 | auto *S = C.getCOFFSection( |
| 1420 | Section, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ, |
| 1421 | SectionKind::getReadOnly()); |
| 1422 | Streamer.SwitchSection(S); |
| 1423 | Streamer.EmitLabel(C.getOrCreateSymbol(StringRef("OBJC_IMAGE_INFO"))); |
| 1424 | Streamer.EmitIntValue(Version, 4); |
| 1425 | Streamer.EmitIntValue(Flags, 4); |
| 1426 | Streamer.AddBlankLine(); |
Reid Kleckner | d973ca3 | 2013-04-25 19:34:41 +0000 | [diff] [blame] | 1427 | } |
Reid Kleckner | fceb76f | 2014-05-16 20:39:27 +0000 | [diff] [blame] | 1428 | |
Rafael Espindola | 46fa231 | 2016-08-29 12:33:42 +0000 | [diff] [blame] | 1429 | void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx, |
| 1430 | const TargetMachine &TM) { |
| 1431 | TargetLoweringObjectFile::Initialize(Ctx, TM); |
| 1432 | const Triple &T = TM.getTargetTriple(); |
| 1433 | if (T.isKnownWindowsMSVCEnvironment() || T.isWindowsItaniumEnvironment()) { |
| 1434 | StaticCtorSection = |
| 1435 | Ctx.getCOFFSection(".CRT$XCU", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | |
| 1436 | COFF::IMAGE_SCN_MEM_READ, |
| 1437 | SectionKind::getReadOnly()); |
| 1438 | StaticDtorSection = |
| 1439 | Ctx.getCOFFSection(".CRT$XTX", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | |
| 1440 | COFF::IMAGE_SCN_MEM_READ, |
| 1441 | SectionKind::getReadOnly()); |
| 1442 | } else { |
| 1443 | StaticCtorSection = Ctx.getCOFFSection( |
| 1444 | ".ctors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | |
| 1445 | COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE, |
| 1446 | SectionKind::getData()); |
| 1447 | StaticDtorSection = Ctx.getCOFFSection( |
| 1448 | ".dtors", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | |
| 1449 | COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE, |
| 1450 | SectionKind::getData()); |
| 1451 | } |
| 1452 | } |
| 1453 | |
Martin Storsjo | 04b6844 | 2017-11-28 08:07:18 +0000 | [diff] [blame] | 1454 | static MCSectionCOFF *getCOFFStaticStructorSection(MCContext &Ctx, |
| 1455 | const Triple &T, bool IsCtor, |
| 1456 | unsigned Priority, |
| 1457 | const MCSymbol *KeySym, |
| 1458 | MCSectionCOFF *Default) { |
Reid Kleckner | f803b23 | 2018-09-07 23:07:55 +0000 | [diff] [blame] | 1459 | if (T.isKnownWindowsMSVCEnvironment() || T.isWindowsItaniumEnvironment()) { |
| 1460 | // If the priority is the default, use .CRT$XCU, possibly associative. |
| 1461 | if (Priority == 65535) |
| 1462 | return Ctx.getAssociativeCOFFSection(Default, KeySym, 0); |
| 1463 | |
| 1464 | // Otherwise, we need to compute a new section name. Low priorities should |
| 1465 | // run earlier. The linker will sort sections ASCII-betically, and we need a |
| 1466 | // string that sorts between .CRT$XCA and .CRT$XCU. In the general case, we |
| 1467 | // make a name like ".CRT$XCT12345", since that runs before .CRT$XCU. Really |
| 1468 | // low priorities need to sort before 'L', since the CRT uses that |
| 1469 | // internally, so we use ".CRT$XCA00001" for them. |
| 1470 | SmallString<24> Name; |
| 1471 | raw_svector_ostream OS(Name); |
| 1472 | OS << ".CRT$XC" << (Priority < 200 ? 'A' : 'T') << format("%05u", Priority); |
| 1473 | MCSectionCOFF *Sec = Ctx.getCOFFSection( |
| 1474 | Name, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ, |
| 1475 | SectionKind::getReadOnly()); |
| 1476 | return Ctx.getAssociativeCOFFSection(Sec, KeySym, 0); |
| 1477 | } |
Martin Storsjo | 04b6844 | 2017-11-28 08:07:18 +0000 | [diff] [blame] | 1478 | |
| 1479 | std::string Name = IsCtor ? ".ctors" : ".dtors"; |
| 1480 | if (Priority != 65535) |
| 1481 | raw_string_ostream(Name) << format(".%05u", 65535 - Priority); |
| 1482 | |
| 1483 | return Ctx.getAssociativeCOFFSection( |
| 1484 | Ctx.getCOFFSection(Name, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | |
| 1485 | COFF::IMAGE_SCN_MEM_READ | |
| 1486 | COFF::IMAGE_SCN_MEM_WRITE, |
| 1487 | SectionKind::getData()), |
| 1488 | KeySym, 0); |
| 1489 | } |
| 1490 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 1491 | MCSection *TargetLoweringObjectFileCOFF::getStaticCtorSection( |
Rafael Espindola | 0766ae0 | 2014-06-06 19:26:12 +0000 | [diff] [blame] | 1492 | unsigned Priority, const MCSymbol *KeySym) const { |
Martin Storsjo | 04b6844 | 2017-11-28 08:07:18 +0000 | [diff] [blame] | 1493 | return getCOFFStaticStructorSection(getContext(), getTargetTriple(), true, |
| 1494 | Priority, KeySym, |
| 1495 | cast<MCSectionCOFF>(StaticCtorSection)); |
Reid Kleckner | fceb76f | 2014-05-16 20:39:27 +0000 | [diff] [blame] | 1496 | } |
| 1497 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 1498 | MCSection *TargetLoweringObjectFileCOFF::getStaticDtorSection( |
Rafael Espindola | 0766ae0 | 2014-06-06 19:26:12 +0000 | [diff] [blame] | 1499 | unsigned Priority, const MCSymbol *KeySym) const { |
Martin Storsjo | 04b6844 | 2017-11-28 08:07:18 +0000 | [diff] [blame] | 1500 | return getCOFFStaticStructorSection(getContext(), getTargetTriple(), false, |
| 1501 | Priority, KeySym, |
| 1502 | cast<MCSectionCOFF>(StaticDtorSection)); |
Reid Kleckner | fceb76f | 2014-05-16 20:39:27 +0000 | [diff] [blame] | 1503 | } |
Peter Collingbourne | aef3659 | 2015-06-29 22:04:09 +0000 | [diff] [blame] | 1504 | |
| 1505 | void TargetLoweringObjectFileCOFF::emitLinkerFlagsForGlobal( |
Eric Christopher | 4367c7f | 2016-09-16 07:33:15 +0000 | [diff] [blame] | 1506 | raw_ostream &OS, const GlobalValue *GV) const { |
Bob Haarman | dd4ebc1 | 2017-02-02 23:00:49 +0000 | [diff] [blame] | 1507 | emitLinkerFlagsForGlobalCOFF(OS, GV, getTargetTriple(), getMangler()); |
Peter Collingbourne | aef3659 | 2015-06-29 22:04:09 +0000 | [diff] [blame] | 1508 | } |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 1509 | |
Saleem Abdulrasool | 99f479a | 2018-01-20 00:28:02 +0000 | [diff] [blame] | 1510 | void TargetLoweringObjectFileCOFF::emitLinkerFlagsForUsed( |
| 1511 | raw_ostream &OS, const GlobalValue *GV) const { |
| 1512 | emitLinkerFlagsForUsedCOFF(OS, GV, getTargetTriple(), getMangler()); |
| 1513 | } |
| 1514 | |
Reid Kleckner | 98117a4 | 2018-06-12 18:56:05 +0000 | [diff] [blame] | 1515 | const MCExpr *TargetLoweringObjectFileCOFF::lowerRelativeReference( |
| 1516 | const GlobalValue *LHS, const GlobalValue *RHS, |
| 1517 | const TargetMachine &TM) const { |
| 1518 | const Triple &T = TM.getTargetTriple(); |
| 1519 | if (!T.isKnownWindowsMSVCEnvironment() && |
| 1520 | !T.isWindowsItaniumEnvironment() && |
| 1521 | !T.isWindowsCoreCLREnvironment()) |
| 1522 | return nullptr; |
| 1523 | |
| 1524 | // Our symbols should exist in address space zero, cowardly no-op if |
| 1525 | // otherwise. |
| 1526 | if (LHS->getType()->getPointerAddressSpace() != 0 || |
| 1527 | RHS->getType()->getPointerAddressSpace() != 0) |
| 1528 | return nullptr; |
| 1529 | |
| 1530 | // Both ptrtoint instructions must wrap global objects: |
| 1531 | // - Only global variables are eligible for image relative relocations. |
| 1532 | // - The subtrahend refers to the special symbol __ImageBase, a GlobalVariable. |
| 1533 | // We expect __ImageBase to be a global variable without a section, externally |
| 1534 | // defined. |
| 1535 | // |
| 1536 | // It should look something like this: @__ImageBase = external constant i8 |
| 1537 | if (!isa<GlobalObject>(LHS) || !isa<GlobalVariable>(RHS) || |
| 1538 | LHS->isThreadLocal() || RHS->isThreadLocal() || |
| 1539 | RHS->getName() != "__ImageBase" || !RHS->hasExternalLinkage() || |
| 1540 | cast<GlobalVariable>(RHS)->hasInitializer() || RHS->hasSection()) |
| 1541 | return nullptr; |
| 1542 | |
| 1543 | return MCSymbolRefExpr::create(TM.getSymbol(LHS), |
| 1544 | MCSymbolRefExpr::VK_COFF_IMGREL32, |
| 1545 | getContext()); |
| 1546 | } |
| 1547 | |
| 1548 | static std::string APIntToHexString(const APInt &AI) { |
| 1549 | unsigned Width = (AI.getBitWidth() / 8) * 2; |
| 1550 | std::string HexString = utohexstr(AI.getLimitedValue(), /*LowerCase=*/true); |
| 1551 | unsigned Size = HexString.size(); |
| 1552 | assert(Width >= Size && "hex string is too large!"); |
| 1553 | HexString.insert(HexString.begin(), Width - Size, '0'); |
| 1554 | |
| 1555 | return HexString; |
| 1556 | } |
| 1557 | |
| 1558 | static std::string scalarConstantToHexString(const Constant *C) { |
| 1559 | Type *Ty = C->getType(); |
| 1560 | if (isa<UndefValue>(C)) { |
| 1561 | return APIntToHexString(APInt::getNullValue(Ty->getPrimitiveSizeInBits())); |
| 1562 | } else if (const auto *CFP = dyn_cast<ConstantFP>(C)) { |
| 1563 | return APIntToHexString(CFP->getValueAPF().bitcastToAPInt()); |
| 1564 | } else if (const auto *CI = dyn_cast<ConstantInt>(C)) { |
| 1565 | return APIntToHexString(CI->getValue()); |
| 1566 | } else { |
| 1567 | unsigned NumElements; |
| 1568 | if (isa<VectorType>(Ty)) |
| 1569 | NumElements = Ty->getVectorNumElements(); |
| 1570 | else |
| 1571 | NumElements = Ty->getArrayNumElements(); |
| 1572 | std::string HexString; |
| 1573 | for (int I = NumElements - 1, E = -1; I != E; --I) |
| 1574 | HexString += scalarConstantToHexString(C->getAggregateElement(I)); |
| 1575 | return HexString; |
| 1576 | } |
| 1577 | } |
| 1578 | |
| 1579 | MCSection *TargetLoweringObjectFileCOFF::getSectionForConstant( |
| 1580 | const DataLayout &DL, SectionKind Kind, const Constant *C, |
| 1581 | unsigned &Align) const { |
Martin Storsjo | 9dafd6f | 2018-07-26 10:48:20 +0000 | [diff] [blame] | 1582 | if (Kind.isMergeableConst() && C && |
| 1583 | getContext().getAsmInfo()->hasCOFFComdatConstants()) { |
| 1584 | // This creates comdat sections with the given symbol name, but unless |
| 1585 | // AsmPrinter::GetCPISymbol actually makes the symbol global, the symbol |
| 1586 | // will be created with a null storage class, which makes GNU binutils |
| 1587 | // error out. |
Reid Kleckner | 98117a4 | 2018-06-12 18:56:05 +0000 | [diff] [blame] | 1588 | const unsigned Characteristics = COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | |
| 1589 | COFF::IMAGE_SCN_MEM_READ | |
| 1590 | COFF::IMAGE_SCN_LNK_COMDAT; |
| 1591 | std::string COMDATSymName; |
| 1592 | if (Kind.isMergeableConst4()) { |
| 1593 | if (Align <= 4) { |
| 1594 | COMDATSymName = "__real@" + scalarConstantToHexString(C); |
| 1595 | Align = 4; |
| 1596 | } |
| 1597 | } else if (Kind.isMergeableConst8()) { |
| 1598 | if (Align <= 8) { |
| 1599 | COMDATSymName = "__real@" + scalarConstantToHexString(C); |
| 1600 | Align = 8; |
| 1601 | } |
| 1602 | } else if (Kind.isMergeableConst16()) { |
| 1603 | // FIXME: These may not be appropriate for non-x86 architectures. |
| 1604 | if (Align <= 16) { |
| 1605 | COMDATSymName = "__xmm@" + scalarConstantToHexString(C); |
| 1606 | Align = 16; |
| 1607 | } |
| 1608 | } else if (Kind.isMergeableConst32()) { |
| 1609 | if (Align <= 32) { |
| 1610 | COMDATSymName = "__ymm@" + scalarConstantToHexString(C); |
| 1611 | Align = 32; |
| 1612 | } |
| 1613 | } |
| 1614 | |
| 1615 | if (!COMDATSymName.empty()) |
| 1616 | return getContext().getCOFFSection(".rdata", Characteristics, Kind, |
| 1617 | COMDATSymName, |
| 1618 | COFF::IMAGE_COMDAT_SELECT_ANY); |
| 1619 | } |
| 1620 | |
| 1621 | return TargetLoweringObjectFile::getSectionForConstant(DL, Kind, C, Align); |
| 1622 | } |
| 1623 | |
| 1624 | |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 1625 | //===----------------------------------------------------------------------===// |
| 1626 | // Wasm |
| 1627 | //===----------------------------------------------------------------------===// |
| 1628 | |
Sam Clegg | ea7cace | 2018-01-09 23:43:14 +0000 | [diff] [blame] | 1629 | static const Comdat *getWasmComdat(const GlobalValue *GV) { |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1630 | const Comdat *C = GV->getComdat(); |
| 1631 | if (!C) |
Sam Clegg | ea7cace | 2018-01-09 23:43:14 +0000 | [diff] [blame] | 1632 | return nullptr; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1633 | |
Sam Clegg | ea7cace | 2018-01-09 23:43:14 +0000 | [diff] [blame] | 1634 | if (C->getSelectionKind() != Comdat::Any) |
| 1635 | report_fatal_error("WebAssembly COMDATs only support " |
| 1636 | "SelectionKind::Any, '" + C->getName() + "' cannot be " |
| 1637 | "lowered."); |
| 1638 | |
| 1639 | return C; |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1640 | } |
| 1641 | |
Sam Clegg | e1694f9 | 2017-12-07 02:55:51 +0000 | [diff] [blame] | 1642 | static SectionKind getWasmKindForNamedSection(StringRef Name, SectionKind K) { |
| 1643 | // If we're told we have function data, then use that. |
| 1644 | if (K.isText()) |
| 1645 | return SectionKind::getText(); |
| 1646 | |
| 1647 | // Otherwise, ignore whatever section type the generic impl detected and use |
| 1648 | // a plain data section. |
| 1649 | return SectionKind::getData(); |
| 1650 | } |
| 1651 | |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 1652 | MCSection *TargetLoweringObjectFileWasm::getExplicitSectionGlobal( |
| 1653 | const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { |
Sam Clegg | 277f898 | 2018-06-14 18:48:19 +0000 | [diff] [blame] | 1654 | // We don't support explict section names for functions in the wasm object |
| 1655 | // format. Each function has to be in its own unique section. |
| 1656 | if (isa<Function>(GO)) { |
| 1657 | return SelectSectionForGlobal(GO, Kind, TM); |
| 1658 | } |
| 1659 | |
Sam Clegg | 759631c | 2017-09-15 20:54:59 +0000 | [diff] [blame] | 1660 | StringRef Name = GO->getSection(); |
Sam Clegg | ea7cace | 2018-01-09 23:43:14 +0000 | [diff] [blame] | 1661 | |
Sam Clegg | e1694f9 | 2017-12-07 02:55:51 +0000 | [diff] [blame] | 1662 | Kind = getWasmKindForNamedSection(Name, Kind); |
Sam Clegg | ea7cace | 2018-01-09 23:43:14 +0000 | [diff] [blame] | 1663 | |
| 1664 | StringRef Group = ""; |
| 1665 | if (const Comdat *C = getWasmComdat(GO)) { |
| 1666 | Group = C->getName(); |
| 1667 | } |
| 1668 | |
| 1669 | return getContext().getWasmSection(Name, Kind, Group, |
| 1670 | MCContext::GenericSectionID); |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 1671 | } |
| 1672 | |
Sam Clegg | 2176a9f | 2017-09-12 18:31:24 +0000 | [diff] [blame] | 1673 | static MCSectionWasm *selectWasmSectionForGlobal( |
| 1674 | MCContext &Ctx, const GlobalObject *GO, SectionKind Kind, Mangler &Mang, |
| 1675 | const TargetMachine &TM, bool EmitUniqueSection, unsigned *NextUniqueID) { |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1676 | StringRef Group = ""; |
Sam Clegg | ea7cace | 2018-01-09 23:43:14 +0000 | [diff] [blame] | 1677 | if (const Comdat *C = getWasmComdat(GO)) { |
| 1678 | Group = C->getName(); |
| 1679 | } |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1680 | |
| 1681 | bool UniqueSectionNames = TM.getUniqueSectionNames(); |
| 1682 | SmallString<128> Name = getSectionPrefixForGlobal(Kind); |
| 1683 | |
| 1684 | if (const auto *F = dyn_cast<Function>(GO)) { |
| 1685 | const auto &OptionalPrefix = F->getSectionPrefix(); |
| 1686 | if (OptionalPrefix) |
| 1687 | Name += *OptionalPrefix; |
| 1688 | } |
| 1689 | |
| 1690 | if (EmitUniqueSection && UniqueSectionNames) { |
| 1691 | Name.push_back('.'); |
| 1692 | TM.getNameWithPrefix(Name, GO, Mang, true); |
| 1693 | } |
| 1694 | unsigned UniqueID = MCContext::GenericSectionID; |
| 1695 | if (EmitUniqueSection && !UniqueSectionNames) { |
| 1696 | UniqueID = *NextUniqueID; |
| 1697 | (*NextUniqueID)++; |
| 1698 | } |
Sam Clegg | 12fd3da | 2017-10-20 21:28:38 +0000 | [diff] [blame] | 1699 | return Ctx.getWasmSection(Name, Kind, Group, UniqueID); |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1700 | } |
| 1701 | |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 1702 | MCSection *TargetLoweringObjectFileWasm::SelectSectionForGlobal( |
| 1703 | const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const { |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1704 | |
| 1705 | if (Kind.isCommon()) |
| 1706 | report_fatal_error("mergable sections not supported yet on wasm"); |
| 1707 | |
| 1708 | // If we have -ffunction-section or -fdata-section then we should emit the |
| 1709 | // global value to a uniqued section specifically for it. |
| 1710 | bool EmitUniqueSection = false; |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 1711 | if (Kind.isText()) |
Dan Gohman | d934cb8 | 2017-02-24 23:18:00 +0000 | [diff] [blame] | 1712 | EmitUniqueSection = TM.getFunctionSections(); |
| 1713 | else |
| 1714 | EmitUniqueSection = TM.getDataSections(); |
| 1715 | EmitUniqueSection |= GO->hasComdat(); |
| 1716 | |
| 1717 | return selectWasmSectionForGlobal(getContext(), GO, Kind, getMangler(), TM, |
Sam Clegg | 2176a9f | 2017-09-12 18:31:24 +0000 | [diff] [blame] | 1718 | EmitUniqueSection, &NextUniqueID); |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 1719 | } |
| 1720 | |
| 1721 | bool TargetLoweringObjectFileWasm::shouldPutJumpTableInFunctionSection( |
| 1722 | bool UsesLabelDifference, const Function &F) const { |
| 1723 | // We can always create relative relocations, so use another section |
| 1724 | // that can be marked non-executable. |
| 1725 | return false; |
| 1726 | } |
| 1727 | |
| 1728 | const MCExpr *TargetLoweringObjectFileWasm::lowerRelativeReference( |
| 1729 | const GlobalValue *LHS, const GlobalValue *RHS, |
| 1730 | const TargetMachine &TM) const { |
| 1731 | // We may only use a PLT-relative relocation to refer to unnamed_addr |
| 1732 | // functions. |
| 1733 | if (!LHS->hasGlobalUnnamedAddr() || !LHS->getValueType()->isFunctionTy()) |
| 1734 | return nullptr; |
| 1735 | |
| 1736 | // Basic sanity checks. |
| 1737 | if (LHS->getType()->getPointerAddressSpace() != 0 || |
| 1738 | RHS->getType()->getPointerAddressSpace() != 0 || LHS->isThreadLocal() || |
| 1739 | RHS->isThreadLocal()) |
| 1740 | return nullptr; |
| 1741 | |
| 1742 | return MCBinaryExpr::createSub( |
| 1743 | MCSymbolRefExpr::create(TM.getSymbol(LHS), MCSymbolRefExpr::VK_None, |
| 1744 | getContext()), |
| 1745 | MCSymbolRefExpr::create(TM.getSymbol(RHS), getContext()), getContext()); |
| 1746 | } |
| 1747 | |
Sam Clegg | b2b019f | 2017-10-03 11:20:28 +0000 | [diff] [blame] | 1748 | void TargetLoweringObjectFileWasm::InitializeWasm() { |
| 1749 | StaticCtorSection = |
Sam Clegg | 12fd3da | 2017-10-20 21:28:38 +0000 | [diff] [blame] | 1750 | getContext().getWasmSection(".init_array", SectionKind::getData()); |
Sam Clegg | bafe690 | 2017-12-15 00:17:10 +0000 | [diff] [blame] | 1751 | } |
| 1752 | |
| 1753 | MCSection *TargetLoweringObjectFileWasm::getStaticCtorSection( |
| 1754 | unsigned Priority, const MCSymbol *KeySym) const { |
| 1755 | return Priority == UINT16_MAX ? |
| 1756 | StaticCtorSection : |
| 1757 | getContext().getWasmSection(".init_array." + utostr(Priority), |
| 1758 | SectionKind::getData()); |
| 1759 | } |
| 1760 | |
| 1761 | MCSection *TargetLoweringObjectFileWasm::getStaticDtorSection( |
| 1762 | unsigned Priority, const MCSymbol *KeySym) const { |
| 1763 | llvm_unreachable("@llvm.global_dtors should have been lowered already"); |
| 1764 | return nullptr; |
Dan Gohman | 18eafb6 | 2017-02-22 01:23:18 +0000 | [diff] [blame] | 1765 | } |