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